summaryrefslogtreecommitdiff
path: root/tests/netlink_route.c
blob: 9281e9a4b009e3942b3882e377e6326c53a0afc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
 * Copyright (c) 2017-2018 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "test_netlink.h"
#ifdef HAVE_STRUCT_DCBMSG
# include <linux/dcbnl.h>
#endif
#ifdef HAVE_LINUX_FIB_RULES_H
# include <linux/fib_rules.h>
#endif
#ifdef HAVE_LINUX_IF_ADDR_H
# include <linux/if_addr.h>
#endif
#ifdef HAVE_STRUCT_IFADDRLBLMSG
# include <linux/if_addrlabel.h>
#endif
#include <linux/if_arp.h>
#include <linux/if_bridge.h>
#include <linux/ip.h>
#ifdef HAVE_LINUX_NEIGHBOUR_H
# include <linux/neighbour.h>
#endif
#ifdef HAVE_STRUCT_NETCONFMSG
# include <linux/netconf.h>
#endif
#include <linux/rtnetlink.h>

#define TEST_NL_ROUTE(fd_, nlh0_, type_, obj_, print_family_, ...)	\
	do {								\
		/* family and string */					\
		TEST_NETLINK((fd_), (nlh0_),				\
			     type_, NLM_F_REQUEST,			\
			     sizeof(obj_) - 1,				\
			     &(obj_), sizeof(obj_) - 1,			\
			     (print_family_);				\
			     printf(", ...}"));				\
									\
		/* sizeof(obj_) */					\
		TEST_NETLINK((fd_), (nlh0_),				\
			     type_, NLM_F_REQUEST,			\
			     sizeof(obj_), &(obj_), sizeof(obj_),	\
			     (print_family_);				\
			      __VA_ARGS__);				\
									\
		/* short read of sizeof(obj_) */			\
		TEST_NETLINK((fd_), (nlh0_),				\
			     type_, NLM_F_REQUEST,			\
			     sizeof(obj_), &(obj_), sizeof(obj_) - 1,	\
			     (print_family_);				\
			     printf(", %p}",				\
				    NLMSG_DATA(TEST_NETLINK_nlh) + 1));	\
	} while (0)

static void
test_nlmsg_type(const int fd)
{
	long rc;
	struct nlmsghdr nlh = {
		.nlmsg_len = sizeof(nlh),
		.nlmsg_type = RTM_GETLINK,
		.nlmsg_flags = NLM_F_REQUEST,
	};

	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u, type=RTM_GETLINK"
	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
}

static void
test_nlmsg_flags(const int fd)
{
	long rc;
	struct nlmsghdr nlh = {
		.nlmsg_len = sizeof(nlh),
	};

	nlh.nlmsg_type = RTM_GETLINK;
	nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u, type=RTM_GETLINK"
	       ", flags=NLM_F_REQUEST|NLM_F_DUMP, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));

	nlh.nlmsg_type = RTM_DELACTION;
	nlh.nlmsg_flags = NLM_F_ROOT;
	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u, type=RTM_DELACTION"
	       ", flags=NLM_F_ROOT, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));

	nlh.nlmsg_type = RTM_NEWLINK;
	nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_REPLACE;
	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u, type=RTM_NEWLINK"
	       ", flags=NLM_F_ECHO|NLM_F_REPLACE, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));

	nlh.nlmsg_type = RTM_DELLINK;
	nlh.nlmsg_flags = NLM_F_NONREC;
	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u, type=RTM_DELLINK"
	       ", flags=NLM_F_NONREC, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
}

static void
test_nlmsg_done(const int fd)
{
	const int num = 0xabcdefad;
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(num));

	TEST_NETLINK(fd, nlh0, NLMSG_DONE, NLM_F_REQUEST,
		     sizeof(num), &num, sizeof(num),
		     printf("%d", num));
}

static void
test_rtnl_unspec(const int fd)
{
	uint8_t family = 0;
	char buf[sizeof(family) + 4];
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(buf));

	/* unspecified family only */
	TEST_NETLINK_(fd, nlh0,
		      0xffff, "0xffff /* RTM_??? */",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(family), &family, sizeof(family),
		      printf("{family=AF_UNSPEC}"));

	/* unknown family only */
	family = 0xff;
	TEST_NETLINK_(fd, nlh0,
		      0xffff, "0xffff /* RTM_??? */",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(family), &family, sizeof(family),
		      printf("{family=0xff /* AF_??? */}"));

	/* short read of family */
	TEST_NETLINK_(fd, nlh0,
		      0xffff, "0xffff /* RTM_??? */",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(family), &family, sizeof(family) - 1,
		      printf("%p", NLMSG_DATA(TEST_NETLINK_nlh)));

	/* unspecified family and string */
	family = 0;
	memcpy(buf, &family, sizeof(family));
	memcpy(buf + sizeof(family), "1234", 4);
	TEST_NETLINK_(fd, nlh0,
		      0xffff, "0xffff /* RTM_??? */",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(buf), buf, sizeof(buf),
		      printf("{family=AF_UNSPEC, \"\\x31\\x32\\x33\\x34\"}"));

	/* unknown family and string */
	family = 0xfd;
	memcpy(buf, &family, sizeof(family));
	TEST_NETLINK_(fd, nlh0,
		      0xffff, "0xffff /* RTM_??? */",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(buf), buf, sizeof(buf),
		      printf("{family=%#x /* AF_??? */"
			     ", \"\\x31\\x32\\x33\\x34\"}", family));
}

static void
test_rtnl_link(const int fd)
{
	const struct ifinfomsg ifinfo = {
		.ifi_family = AF_UNIX,
		.ifi_type = ARPHRD_LOOPBACK,
		.ifi_index = ifindex_lo(),
		.ifi_flags = IFF_UP,
		.ifi_change = 0xfabcdeba
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(ifinfo));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETLINK, ifinfo,
		      printf("{ifi_family=AF_UNIX"),
		      printf(", ifi_type=ARPHRD_LOOPBACK"
			     ", ifi_index=" IFINDEX_LO_STR
			     ", ifi_flags=IFF_UP");
		      PRINT_FIELD_X(", ", ifinfo, ifi_change);
		      printf("}"));
}

static void
test_rtnl_addr(const int fd)
{
	const struct ifaddrmsg msg = {
		.ifa_family = AF_UNIX,
		.ifa_prefixlen = 0xde,
		.ifa_flags = IFA_F_SECONDARY,
		.ifa_scope = RT_SCOPE_UNIVERSE,
		.ifa_index = ifindex_lo()
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETADDR, msg,
		      printf("{ifa_family=AF_UNIX"),
		      PRINT_FIELD_U(", ", msg, ifa_prefixlen);
		      printf(", ifa_flags=IFA_F_SECONDARY"
			     ", ifa_scope=RT_SCOPE_UNIVERSE"
			     ", ifa_index=" IFINDEX_LO_STR);
		      printf("}"));
}

static void
test_rtnl_route(const int fd)
{
	static const struct rtmsg msg = {
		.rtm_family = AF_UNIX,
		.rtm_dst_len = 0xaf,
		.rtm_src_len = 0xda,
		.rtm_tos = IPTOS_LOWDELAY,
		.rtm_table = RT_TABLE_DEFAULT,
		.rtm_protocol = RTPROT_KERNEL,
		.rtm_scope = RT_SCOPE_UNIVERSE,
		.rtm_type = RTN_LOCAL,
		.rtm_flags = RTM_F_NOTIFY
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETROUTE, msg,
		      printf("{rtm_family=AF_UNIX"),
		      PRINT_FIELD_U(", ", msg, rtm_dst_len);
		      PRINT_FIELD_U(", ", msg, rtm_src_len);
		      printf(", rtm_tos=IPTOS_LOWDELAY"
			     ", rtm_table=RT_TABLE_DEFAULT"
			     ", rtm_protocol=RTPROT_KERNEL"
			     ", rtm_scope=RT_SCOPE_UNIVERSE"
			     ", rtm_type=RTN_LOCAL"
			     ", rtm_flags=RTM_F_NOTIFY}"));
}

#ifdef HAVE_LINUX_FIB_RULES_H
static void
test_rtnl_rule(const int fd)
{
	struct rtmsg msg = {
		.rtm_family = AF_UNIX,
		.rtm_dst_len = 0xaf,
		.rtm_src_len = 0xda,
		.rtm_tos = IPTOS_LOWDELAY,
		.rtm_table = RT_TABLE_UNSPEC,
		.rtm_type = FR_ACT_TO_TBL,
		.rtm_flags = FIB_RULE_INVERT
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETRULE, msg,
		      printf("{family=AF_UNIX"),
		      printf(", dst_len=%u, src_len=%u"
			     ", tos=IPTOS_LOWDELAY"
			     ", table=RT_TABLE_UNSPEC"
			     ", action=FR_ACT_TO_TBL"
			     ", flags=FIB_RULE_INVERT}",
			     msg.rtm_dst_len,
			     msg.rtm_src_len));
}
#endif

static void
test_rtnl_neigh(const int fd)
{
	const struct ndmsg msg = {
		.ndm_family = AF_UNIX,
		.ndm_ifindex = ifindex_lo(),
		.ndm_state = NUD_PERMANENT,
		.ndm_flags = NTF_PROXY,
		.ndm_type = RTN_UNSPEC
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETNEIGH, msg,
		      printf("{ndm_family=AF_UNIX"),
		      printf(", ndm_ifindex=" IFINDEX_LO_STR
			     ", ndm_state=NUD_PERMANENT"
			     ", ndm_flags=NTF_PROXY"
			     ", ndm_type=RTN_UNSPEC}"));
}

static void
test_rtnl_neightbl(const int fd)
{
	static const struct ndtmsg msg = {
		.ndtm_family = AF_NETLINK
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NETLINK(fd, nlh0,
		     RTM_GETNEIGHTBL, NLM_F_REQUEST,
		     sizeof(msg), &msg, sizeof(msg),
		     printf("{ndtm_family=AF_NETLINK}"));
}

static void
test_rtnl_tc(const int fd)
{
	const struct tcmsg msg = {
		.tcm_family = AF_UNIX,
		.tcm_ifindex = ifindex_lo(),
		.tcm_handle = 0xfadcdafb,
		.tcm_parent = 0xafbcadab,
		.tcm_info = 0xbcaedafa
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETQDISC, msg,
		      printf("{tcm_family=AF_UNIX"),
		      printf(", tcm_ifindex=" IFINDEX_LO_STR);
		      PRINT_FIELD_U(", ", msg, tcm_handle);
		      PRINT_FIELD_U(", ", msg, tcm_parent);
		      PRINT_FIELD_U(", ", msg, tcm_info);
		      printf("}"));
}

static void
test_rtnl_tca(const int fd)
{
	struct tcamsg msg = {
		.tca_family = AF_INET
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NETLINK(fd, nlh0,
		     RTM_GETACTION, NLM_F_REQUEST,
		     sizeof(msg), &msg, sizeof(msg),
		     printf("{tca_family=AF_INET}"));
}

#ifdef HAVE_STRUCT_IFADDRLBLMSG
static void
test_rtnl_addrlabel(const int fd)
{
	const struct ifaddrlblmsg msg = {
		.ifal_family = AF_UNIX,
		.ifal_prefixlen = 0xaf,
		.ifal_flags = 0xbd,
		.ifal_index = ifindex_lo(),
		.ifal_seq = 0xfadcdafb
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETADDRLABEL, msg,
		      printf("{ifal_family=AF_UNIX"),
		      PRINT_FIELD_U(", ", msg, ifal_prefixlen);
		      PRINT_FIELD_U(", ", msg, ifal_flags);
		      printf(", ifal_index=" IFINDEX_LO_STR);
		      PRINT_FIELD_U(", ", msg, ifal_seq);
		      printf("}"));
}
#endif

#ifdef HAVE_STRUCT_DCBMSG
static void
test_rtnl_dcb(const int fd)
{
	static const struct dcbmsg msg = {
		.dcb_family = AF_UNIX,
		.cmd = DCB_CMD_UNDEFINED
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETDCB, msg,
		      printf("{dcb_family=AF_UNIX"),
		      printf(", cmd=DCB_CMD_UNDEFINED}"));
}
#endif

#ifdef HAVE_STRUCT_NETCONFMSG
static void
test_rtnl_netconf(const int fd)
{
	static const struct netconfmsg msg = {
		.ncm_family = AF_INET
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NETLINK(fd, nlh0,
		     RTM_GETNETCONF, NLM_F_REQUEST,
		     sizeof(msg), &msg, sizeof(msg),
		     printf("{ncm_family=AF_INET}"));
}
#endif

#ifdef HAVE_STRUCT_BR_PORT_MSG
static void
test_rtnl_mdb(const int fd)
{
	const struct br_port_msg msg = {
		.family = AF_UNIX,
		.ifindex = ifindex_lo()
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NL_ROUTE(fd, nlh0, RTM_GETMDB, msg,
		      printf("{family=AF_UNIX"),
		      printf(", ifindex=" IFINDEX_LO_STR "}"));
}
#endif

#ifdef RTM_NEWNSID
static void
test_rtnl_nsid(const int fd)
{
	static const struct rtgenmsg msg = {
		.rtgen_family = AF_UNIX
	};
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(msg));

	TEST_NETLINK(fd, nlh0,
		     RTM_GETNSID, NLM_F_REQUEST,
		     sizeof(msg), &msg, sizeof(msg),
		     printf("{rtgen_family=AF_UNIX}"));
}
#endif

int main(void)
{
	skip_if_unavailable("/proc/self/fd/");

	int fd = create_nl_socket(NETLINK_ROUTE);

	test_nlmsg_type(fd);
	test_nlmsg_flags(fd);
	test_nlmsg_done(fd);
	test_rtnl_unspec(fd);
	test_rtnl_link(fd);
	test_rtnl_addr(fd);
	test_rtnl_route(fd);
#ifdef HAVE_LINUX_FIB_RULES_H
	test_rtnl_rule(fd);
#endif
	test_rtnl_neigh(fd);
	test_rtnl_neightbl(fd);
	test_rtnl_tc(fd);
	test_rtnl_tca(fd);
#ifdef HAVE_STRUCT_IFADDRLBLMSG
	test_rtnl_addrlabel(fd);
#endif
#ifdef HAVE_STRUCT_DCBMSG
	test_rtnl_dcb(fd);
#endif
#ifdef HAVE_STRUCT_NETCONFMSG
	test_rtnl_netconf(fd);
#endif
#ifdef HAVE_STRUCT_BR_PORT_MSG
	test_rtnl_mdb(fd);
#endif
#ifdef RTM_NEWNSID
	test_rtnl_nsid(fd);
#endif

	printf("+++ exited with 0 +++\n");

	return 0;
}