1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@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 static void
init_inet_diag_msg(struct nlmsghdr * const nlh,const unsigned int msg_len)43 init_inet_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
44 {
45 SET_STRUCT(struct nlmsghdr, nlh,
46 .nlmsg_len = msg_len,
47 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
48 .nlmsg_flags = NLM_F_DUMP
49 );
50
51 struct inet_diag_msg *const msg = NLMSG_DATA(nlh);
52 SET_STRUCT(struct inet_diag_msg, msg,
53 .idiag_family = AF_INET,
54 .idiag_state = TCP_LISTEN,
55 .id.idiag_if = ifindex_lo()
56 );
57
58 if (!inet_pton(AF_INET, address, msg->id.idiag_src) ||
59 !inet_pton(AF_INET, address, msg->id.idiag_dst))
60 perror_msg_and_skip("inet_pton");
61 }
62
63 static void
print_inet_diag_msg(const unsigned int msg_len)64 print_inet_diag_msg(const unsigned int msg_len)
65 {
66 printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
67 ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
68 ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
69 ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
70 ", idiag_src=inet_addr(\"%s\")"
71 ", idiag_dst=inet_addr(\"%s\")"
72 ", idiag_if=" IFINDEX_LO_STR
73 ", idiag_cookie=[0, 0]}"
74 ", idiag_expires=0, idiag_rqueue=0, idiag_wqueue=0"
75 ", idiag_uid=0, idiag_inode=0}",
76 msg_len, address, address);
77 }
78
79 static void
print_uint(const unsigned int * p)80 print_uint(const unsigned int *p)
81 {
82 printf("%u", *p);
83 }
84
85 int
main(void)86 main(void)
87 {
88 skip_if_unavailable("/proc/self/fd/");
89
90 const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
91 const unsigned int hdrlen = sizeof(struct inet_diag_msg);
92 void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
93
94 static char pattern[4096];
95 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
96
97 static const struct inet_diag_meminfo minfo = {
98 .idiag_rmem = 0xfadcacdb,
99 .idiag_wmem = 0xbdabcada,
100 .idiag_fmem = 0xbadbfafb,
101 .idiag_tmem = 0xfdacdadf
102 };
103 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
104 init_inet_diag_msg, print_inet_diag_msg,
105 INET_DIAG_MEMINFO, pattern, minfo,
106 PRINT_FIELD_U("{", minfo, idiag_rmem);
107 PRINT_FIELD_U(", ", minfo, idiag_wmem);
108 PRINT_FIELD_U(", ", minfo, idiag_fmem);
109 PRINT_FIELD_U(", ", minfo, idiag_tmem);
110 printf("}"));
111
112 static const struct tcpvegas_info vegas = {
113 .tcpv_enabled = 0xfadcacdb,
114 .tcpv_rttcnt = 0xbdabcada,
115 .tcpv_rtt = 0xbadbfafb,
116 .tcpv_minrtt = 0xfdacdadf
117 };
118 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
119 init_inet_diag_msg, print_inet_diag_msg,
120 INET_DIAG_VEGASINFO, pattern, vegas,
121 PRINT_FIELD_U("{", vegas, tcpv_enabled);
122 PRINT_FIELD_U(", ", vegas, tcpv_rttcnt);
123 PRINT_FIELD_U(", ", vegas, tcpv_rtt);
124 PRINT_FIELD_U(", ", vegas, tcpv_minrtt);
125 printf("}"));
126
127
128 static const struct tcp_dctcp_info dctcp = {
129 .dctcp_enabled = 0xfdac,
130 .dctcp_ce_state = 0xfadc,
131 .dctcp_alpha = 0xbdabcada,
132 .dctcp_ab_ecn = 0xbadbfafb,
133 .dctcp_ab_tot = 0xfdacdadf
134 };
135 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
136 init_inet_diag_msg, print_inet_diag_msg,
137 INET_DIAG_DCTCPINFO, pattern, dctcp,
138 PRINT_FIELD_U("{", dctcp, dctcp_enabled);
139 PRINT_FIELD_U(", ", dctcp, dctcp_ce_state);
140 PRINT_FIELD_U(", ", dctcp, dctcp_alpha);
141 PRINT_FIELD_U(", ", dctcp, dctcp_ab_ecn);
142 PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot);
143 printf("}"));
144
145 static const struct tcp_bbr_info bbr = {
146 .bbr_bw_lo = 0xfdacdadf,
147 .bbr_bw_hi = 0xfadcacdb,
148 .bbr_min_rtt = 0xbdabcada,
149 .bbr_pacing_gain = 0xbadbfafb,
150 .bbr_cwnd_gain = 0xfdacdadf
151 };
152 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
153 init_inet_diag_msg, print_inet_diag_msg,
154 INET_DIAG_BBRINFO, pattern, bbr,
155 PRINT_FIELD_X("{", bbr, bbr_bw_lo);
156 PRINT_FIELD_X(", ", bbr, bbr_bw_hi);
157 PRINT_FIELD_U(", ", bbr, bbr_min_rtt);
158 PRINT_FIELD_U(", ", bbr, bbr_pacing_gain);
159 PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain);
160 printf("}"));
161
162 static const uint32_t mem[] = { 0xaffacbad, 0xffadbcab };
163 TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
164 init_inet_diag_msg, print_inet_diag_msg,
165 INET_DIAG_SKMEMINFO, pattern, mem, print_uint);
166
167 static uint32_t bigmem[SK_MEMINFO_VARS + 1];
168 memcpy(bigmem, pattern, sizeof(bigmem));
169
170 TEST_NLATTR(fd, nlh0, hdrlen, init_inet_diag_msg, print_inet_diag_msg,
171 INET_DIAG_SKMEMINFO, sizeof(bigmem), bigmem, sizeof(bigmem),
172 size_t i;
173 for (i = 0; i < SK_MEMINFO_VARS; ++i) {
174 printf(i ? ", " : "[");
175 print_uint(&bigmem[i]);
176 }
177 printf(", ...]"));
178
179 static const uint32_t mark = 0xabdfadca;
180 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
181 init_inet_diag_msg, print_inet_diag_msg,
182 INET_DIAG_MARK, pattern, mark,
183 printf("%u", mark));
184
185 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
186 init_inet_diag_msg, print_inet_diag_msg,
187 INET_DIAG_CLASS_ID, pattern, mark,
188 printf("%u", mark));
189
190 static const uint8_t shutdown = 0xcd;
191 TEST_NLATTR(fd, nlh0, hdrlen,
192 init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_SHUTDOWN,
193 sizeof(shutdown), &shutdown, sizeof(shutdown),
194 printf("%u", shutdown));
195
196 char *const str = tail_alloc(DEFAULT_STRLEN);
197 fill_memory_ex(str, DEFAULT_STRLEN, '0', 10);
198 TEST_NLATTR(fd, nlh0, hdrlen,
199 init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
200 DEFAULT_STRLEN, str, DEFAULT_STRLEN,
201 printf("\"%.*s\"...", DEFAULT_STRLEN, str));
202 str[DEFAULT_STRLEN - 1] = '\0';
203 TEST_NLATTR(fd, nlh0, hdrlen,
204 init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
205 DEFAULT_STRLEN, str, DEFAULT_STRLEN,
206 printf("\"%s\"", str));
207
208 puts("+++ exited with 0 +++");
209 return 0;
210 }
211