Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) gmail.com>
      3  * Copyright (c) 2017 The strace developers.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "tests.h"
     30 
     31 #include <stdio.h>
     32 #include <string.h>
     33 #include <arpa/inet.h>
     34 #include <net/if.h>
     35 #include <netinet/tcp.h>
     36 #include "test_nlattr.h"
     37 #include <linux/inet_diag.h>
     38 #include <linux/sock_diag.h>
     39 
     40 static const char address[] = "10.11.12.13";
     41 
     42 #ifdef HAVE_IF_INDEXTONAME
     43 # define IFINDEX_LO	(if_nametoindex("lo"))
     44 #else
     45 # define IFINDEX_LO	1
     46 #endif
     47 
     48 static void
     49 init_inet_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
     50 {
     51 	SET_STRUCT(struct nlmsghdr, nlh,
     52 		.nlmsg_len = msg_len,
     53 		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
     54 		.nlmsg_flags = NLM_F_DUMP
     55 	);
     56 
     57 	struct inet_diag_msg *const msg = NLMSG_DATA(nlh);
     58 	SET_STRUCT(struct inet_diag_msg, msg,
     59 		.idiag_family = AF_INET,
     60 		.idiag_state = TCP_LISTEN,
     61 		.id.idiag_if = IFINDEX_LO
     62 	);
     63 
     64 	if (!inet_pton(AF_INET, address, msg->id.idiag_src) ||
     65 	    !inet_pton(AF_INET, address, msg->id.idiag_dst))
     66 		perror_msg_and_skip("inet_pton");
     67 }
     68 
     69 static void
     70 print_inet_diag_msg(const unsigned int msg_len)
     71 {
     72 	printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
     73 	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
     74 	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
     75 	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
     76 	       ", inet_pton(AF_INET, \"%s\", &idiag_src)"
     77 	       ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
     78 	       ", idiag_if=if_nametoindex(\"lo\"), idiag_cookie=[0, 0]}"
     79 	       ", idiag_expires=0, idiag_rqueue=0, idiag_wqueue=0"
     80 	       ", idiag_uid=0, idiag_inode=0}",
     81 	       msg_len, address, address);
     82 }
     83 
     84 static void
     85 print_uint(const unsigned int *p)
     86 {
     87 	printf("%u", *p);
     88 }
     89 
     90 int
     91 main(void)
     92 {
     93 	skip_if_unavailable("/proc/self/fd/");
     94 
     95 	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
     96 	const unsigned int hdrlen = sizeof(struct inet_diag_msg);
     97 	void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
     98 
     99 	static char pattern[4096];
    100 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
    101 
    102 	static const struct inet_diag_meminfo minfo = {
    103 		.idiag_rmem = 0xfadcacdb,
    104 		.idiag_wmem = 0xbdabcada,
    105 		.idiag_fmem = 0xbadbfafb,
    106 		.idiag_tmem = 0xfdacdadf
    107 	};
    108 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    109 			   init_inet_diag_msg, print_inet_diag_msg,
    110 			   INET_DIAG_MEMINFO, pattern, minfo,
    111 			   PRINT_FIELD_U("{", minfo, idiag_rmem);
    112 			   PRINT_FIELD_U(", ", minfo, idiag_wmem);
    113 			   PRINT_FIELD_U(", ", minfo, idiag_fmem);
    114 			   PRINT_FIELD_U(", ", minfo, idiag_tmem);
    115 			   printf("}"));
    116 
    117 	static const struct tcpvegas_info vegas = {
    118 		.tcpv_enabled = 0xfadcacdb,
    119 		.tcpv_rttcnt = 0xbdabcada,
    120 		.tcpv_rtt = 0xbadbfafb,
    121 		.tcpv_minrtt = 0xfdacdadf
    122 	};
    123 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    124 			   init_inet_diag_msg, print_inet_diag_msg,
    125 			   INET_DIAG_VEGASINFO, pattern, vegas,
    126 			   PRINT_FIELD_U("{", vegas, tcpv_enabled);
    127 			   PRINT_FIELD_U(", ", vegas, tcpv_rttcnt);
    128 			   PRINT_FIELD_U(", ", vegas, tcpv_rtt);
    129 			   PRINT_FIELD_U(", ", vegas, tcpv_minrtt);
    130 			   printf("}"));
    131 
    132 
    133 	static const struct tcp_dctcp_info dctcp = {
    134 		.dctcp_enabled = 0xfdac,
    135 		.dctcp_ce_state = 0xfadc,
    136 		.dctcp_alpha = 0xbdabcada,
    137 		.dctcp_ab_ecn = 0xbadbfafb,
    138 		.dctcp_ab_tot = 0xfdacdadf
    139 	};
    140 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    141 			   init_inet_diag_msg, print_inet_diag_msg,
    142 			   INET_DIAG_DCTCPINFO, pattern, dctcp,
    143 			   PRINT_FIELD_U("{", dctcp, dctcp_enabled);
    144 			   PRINT_FIELD_U(", ", dctcp, dctcp_ce_state);
    145 			   PRINT_FIELD_U(", ", dctcp, dctcp_alpha);
    146 			   PRINT_FIELD_U(", ", dctcp, dctcp_ab_ecn);
    147 			   PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot);
    148 			   printf("}"));
    149 
    150 	static const struct tcp_bbr_info bbr = {
    151 		.bbr_bw_lo = 0xfdacdadf,
    152 		.bbr_bw_hi = 0xfadcacdb,
    153 		.bbr_min_rtt = 0xbdabcada,
    154 		.bbr_pacing_gain = 0xbadbfafb,
    155 		.bbr_cwnd_gain = 0xfdacdadf
    156 	};
    157 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    158 			   init_inet_diag_msg, print_inet_diag_msg,
    159 			   INET_DIAG_BBRINFO, pattern, bbr,
    160 			   PRINT_FIELD_X("{", bbr, bbr_bw_lo);
    161 			   PRINT_FIELD_X(", ", bbr, bbr_bw_hi);
    162 			   PRINT_FIELD_U(", ", bbr, bbr_min_rtt);
    163 			   PRINT_FIELD_U(", ", bbr, bbr_pacing_gain);
    164 			   PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain);
    165 			   printf("}"));
    166 
    167 	static const uint32_t mem[] = { 0xaffacbad, 0xffadbcab };
    168 	TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
    169 			  init_inet_diag_msg, print_inet_diag_msg,
    170 			  INET_DIAG_SKMEMINFO, pattern, mem, print_uint);
    171 
    172 	static const uint32_t mark = 0xabdfadca;
    173 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    174 			   init_inet_diag_msg, print_inet_diag_msg,
    175 			   INET_DIAG_MARK, pattern, mark,
    176 			   printf("%u", mark));
    177 
    178 	static const uint8_t shutdown = 0xcd;
    179 	TEST_NLATTR(fd, nlh0, hdrlen,
    180 		    init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_SHUTDOWN,
    181 		    sizeof(shutdown), &shutdown, sizeof(shutdown),
    182 		    printf("%u", shutdown));
    183 
    184 	char *const str = tail_alloc(DEFAULT_STRLEN);
    185 	fill_memory_ex(str, DEFAULT_STRLEN, '0', 10);
    186 	TEST_NLATTR(fd, nlh0, hdrlen,
    187 		    init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
    188 		    DEFAULT_STRLEN, str, DEFAULT_STRLEN,
    189 		    printf("\"%.*s\"...", DEFAULT_STRLEN, str));
    190 	str[DEFAULT_STRLEN - 1] = '\0';
    191 	TEST_NLATTR(fd, nlh0, hdrlen,
    192 		    init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
    193 		    DEFAULT_STRLEN, str, DEFAULT_STRLEN,
    194 		    printf("\"%s\"", str));
    195 
    196 	puts("+++ exited with 0 +++");
    197 	return 0;
    198 }
    199