xref: /openbmc/linux/tools/testing/selftests/net/udpgso.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define _GNU_SOURCE
4 
5 #include <stddef.h>
6 #include <arpa/inet.h>
7 #include <error.h>
8 #include <errno.h>
9 #include <net/if.h>
10 #include <linux/in.h>
11 #include <linux/netlink.h>
12 #include <linux/rtnetlink.h>
13 #include <netinet/if_ether.h>
14 #include <netinet/ip.h>
15 #include <netinet/ip6.h>
16 #include <netinet/udp.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 #ifndef ETH_MAX_MTU
29 #define ETH_MAX_MTU	0xFFFFU
30 #endif
31 
32 #ifndef UDP_SEGMENT
33 #define UDP_SEGMENT		103
34 #endif
35 
36 #ifndef UDP_MAX_SEGMENTS
37 #define UDP_MAX_SEGMENTS	(1 << 7UL)
38 #endif
39 
40 #define CONST_MTU_TEST	1500
41 
42 #define CONST_HDRLEN_V4		(sizeof(struct iphdr) + sizeof(struct udphdr))
43 #define CONST_HDRLEN_V6		(sizeof(struct ip6_hdr) + sizeof(struct udphdr))
44 
45 #define CONST_MSS_V4		(CONST_MTU_TEST - CONST_HDRLEN_V4)
46 #define CONST_MSS_V6		(CONST_MTU_TEST - CONST_HDRLEN_V6)
47 
48 #define CONST_MAX_SEGS_V4	(ETH_MAX_MTU / CONST_MSS_V4)
49 #define CONST_MAX_SEGS_V6	(ETH_MAX_MTU / CONST_MSS_V6)
50 
51 static bool		cfg_do_ipv4;
52 static bool		cfg_do_ipv6;
53 static bool		cfg_do_connected;
54 static bool		cfg_do_connectionless;
55 static bool		cfg_do_msgmore;
56 static bool		cfg_do_setsockopt;
57 static int		cfg_specific_test_id = -1;
58 
59 static const char	cfg_ifname[] = "lo";
60 static unsigned short	cfg_port = 9000;
61 
62 static char buf[ETH_MAX_MTU];
63 
64 struct testcase {
65 	int tlen;		/* send() buffer size, may exceed mss */
66 	bool tfail;		/* send() call is expected to fail */
67 	int gso_len;		/* mss after applying gso */
68 	int r_num_mss;		/* recv(): number of calls of full mss */
69 	int r_len_last;		/* recv(): size of last non-mss dgram, if any */
70 };
71 
72 const struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT;
73 const struct in_addr addr4 = { .s_addr = __constant_htonl(INADDR_LOOPBACK + 2) };
74 
75 struct testcase testcases_v4[] = {
76 	{
77 		/* no GSO: send a single byte */
78 		.tlen = 1,
79 		.r_len_last = 1,
80 	},
81 	{
82 		/* no GSO: send a single MSS */
83 		.tlen = CONST_MSS_V4,
84 		.r_num_mss = 1,
85 	},
86 	{
87 		/* no GSO: send a single MSS + 1B: fail */
88 		.tlen = CONST_MSS_V4 + 1,
89 		.tfail = true,
90 	},
91 	{
92 		/* send a single MSS: will fall back to no GSO */
93 		.tlen = CONST_MSS_V4,
94 		.gso_len = CONST_MSS_V4,
95 		.r_num_mss = 1,
96 	},
97 	{
98 		/* datalen <= MSS < gso_len: will fall back to no GSO */
99 		.tlen = CONST_MSS_V4,
100 		.gso_len = CONST_MSS_V4 + 1,
101 		.r_num_mss = 0,
102 		.r_len_last = CONST_MSS_V4,
103 	},
104 	{
105 		/* MSS < datalen < gso_len: fail */
106 		.tlen = CONST_MSS_V4 + 1,
107 		.gso_len = CONST_MSS_V4 + 2,
108 		.tfail = true,
109 	},
110 	{
111 		/* send a single MSS + 1B */
112 		.tlen = CONST_MSS_V4 + 1,
113 		.gso_len = CONST_MSS_V4,
114 		.r_num_mss = 1,
115 		.r_len_last = 1,
116 	},
117 	{
118 		/* send exactly 2 MSS */
119 		.tlen = CONST_MSS_V4 * 2,
120 		.gso_len = CONST_MSS_V4,
121 		.r_num_mss = 2,
122 	},
123 	{
124 		/* send 2 MSS + 1B */
125 		.tlen = (CONST_MSS_V4 * 2) + 1,
126 		.gso_len = CONST_MSS_V4,
127 		.r_num_mss = 2,
128 		.r_len_last = 1,
129 	},
130 	{
131 		/* send MAX segs */
132 		.tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
133 		.gso_len = CONST_MSS_V4,
134 		.r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
135 	},
136 
137 	{
138 		/* send MAX bytes */
139 		.tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
140 		.gso_len = CONST_MSS_V4,
141 		.r_num_mss = CONST_MAX_SEGS_V4,
142 		.r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
143 			      (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
144 	},
145 	{
146 		/* send MAX + 1: fail */
147 		.tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
148 		.gso_len = CONST_MSS_V4,
149 		.tfail = true,
150 	},
151 	{
152 		/* send a single 1B MSS: will fall back to no GSO */
153 		.tlen = 1,
154 		.gso_len = 1,
155 		.r_num_mss = 1,
156 	},
157 	{
158 		/* send 2 1B segments */
159 		.tlen = 2,
160 		.gso_len = 1,
161 		.r_num_mss = 2,
162 	},
163 	{
164 		/* send 2B + 2B + 1B segments */
165 		.tlen = 5,
166 		.gso_len = 2,
167 		.r_num_mss = 2,
168 		.r_len_last = 1,
169 	},
170 	{
171 		/* send max number of min sized segments */
172 		.tlen = UDP_MAX_SEGMENTS,
173 		.gso_len = 1,
174 		.r_num_mss = UDP_MAX_SEGMENTS,
175 	},
176 	{
177 		/* send max number + 1 of min sized segments: fail */
178 		.tlen = UDP_MAX_SEGMENTS + 1,
179 		.gso_len = 1,
180 		.tfail = true,
181 	},
182 	{
183 		/* EOL */
184 	}
185 };
186 
187 #ifndef IP6_MAX_MTU
188 #define IP6_MAX_MTU	(ETH_MAX_MTU + sizeof(struct ip6_hdr))
189 #endif
190 
191 struct testcase testcases_v6[] = {
192 	{
193 		/* no GSO: send a single byte */
194 		.tlen = 1,
195 		.r_len_last = 1,
196 	},
197 	{
198 		/* no GSO: send a single MSS */
199 		.tlen = CONST_MSS_V6,
200 		.r_num_mss = 1,
201 	},
202 	{
203 		/* no GSO: send a single MSS + 1B: fail */
204 		.tlen = CONST_MSS_V6 + 1,
205 		.tfail = true,
206 	},
207 	{
208 		/* send a single MSS: will fall back to no GSO */
209 		.tlen = CONST_MSS_V6,
210 		.gso_len = CONST_MSS_V6,
211 		.r_num_mss = 1,
212 	},
213 	{
214 		/* datalen <= MSS < gso_len: will fall back to no GSO */
215 		.tlen = CONST_MSS_V6,
216 		.gso_len = CONST_MSS_V6 + 1,
217 		.r_num_mss = 0,
218 		.r_len_last = CONST_MSS_V6,
219 	},
220 	{
221 		/* MSS < datalen < gso_len: fail */
222 		.tlen = CONST_MSS_V6 + 1,
223 		.gso_len = CONST_MSS_V6 + 2,
224 		.tfail = true
225 	},
226 	{
227 		/* send a single MSS + 1B */
228 		.tlen = CONST_MSS_V6 + 1,
229 		.gso_len = CONST_MSS_V6,
230 		.r_num_mss = 1,
231 		.r_len_last = 1,
232 	},
233 	{
234 		/* send exactly 2 MSS */
235 		.tlen = CONST_MSS_V6 * 2,
236 		.gso_len = CONST_MSS_V6,
237 		.r_num_mss = 2,
238 	},
239 	{
240 		/* send 2 MSS + 1B */
241 		.tlen = (CONST_MSS_V6 * 2) + 1,
242 		.gso_len = CONST_MSS_V6,
243 		.r_num_mss = 2,
244 		.r_len_last = 1,
245 	},
246 	{
247 		/* send MAX segs */
248 		.tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
249 		.gso_len = CONST_MSS_V6,
250 		.r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
251 	},
252 
253 	{
254 		/* send MAX bytes */
255 		.tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
256 		.gso_len = CONST_MSS_V6,
257 		.r_num_mss = CONST_MAX_SEGS_V6,
258 		.r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
259 			      (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
260 	},
261 	{
262 		/* send MAX + 1: fail */
263 		.tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
264 		.gso_len = CONST_MSS_V6,
265 		.tfail = true,
266 	},
267 	{
268 		/* send a single 1B MSS: will fall back to no GSO */
269 		.tlen = 1,
270 		.gso_len = 1,
271 		.r_num_mss = 1,
272 	},
273 	{
274 		/* send 2 1B segments */
275 		.tlen = 2,
276 		.gso_len = 1,
277 		.r_num_mss = 2,
278 	},
279 	{
280 		/* send 2B + 2B + 1B segments */
281 		.tlen = 5,
282 		.gso_len = 2,
283 		.r_num_mss = 2,
284 		.r_len_last = 1,
285 	},
286 	{
287 		/* send max number of min sized segments */
288 		.tlen = UDP_MAX_SEGMENTS,
289 		.gso_len = 1,
290 		.r_num_mss = UDP_MAX_SEGMENTS,
291 	},
292 	{
293 		/* send max number + 1 of min sized segments: fail */
294 		.tlen = UDP_MAX_SEGMENTS + 1,
295 		.gso_len = 1,
296 		.tfail = true,
297 	},
298 	{
299 		/* EOL */
300 	}
301 };
302 
get_device_mtu(int fd,const char * ifname)303 static unsigned int get_device_mtu(int fd, const char *ifname)
304 {
305 	struct ifreq ifr;
306 
307 	memset(&ifr, 0, sizeof(ifr));
308 
309 	strcpy(ifr.ifr_name, ifname);
310 
311 	if (ioctl(fd, SIOCGIFMTU, &ifr))
312 		error(1, errno, "ioctl get mtu");
313 
314 	return ifr.ifr_mtu;
315 }
316 
__set_device_mtu(int fd,const char * ifname,unsigned int mtu)317 static void __set_device_mtu(int fd, const char *ifname, unsigned int mtu)
318 {
319 	struct ifreq ifr;
320 
321 	memset(&ifr, 0, sizeof(ifr));
322 
323 	ifr.ifr_mtu = mtu;
324 	strcpy(ifr.ifr_name, ifname);
325 
326 	if (ioctl(fd, SIOCSIFMTU, &ifr))
327 		error(1, errno, "ioctl set mtu");
328 }
329 
set_device_mtu(int fd,int mtu)330 static void set_device_mtu(int fd, int mtu)
331 {
332 	int val;
333 
334 	val = get_device_mtu(fd, cfg_ifname);
335 	fprintf(stderr, "device mtu (orig): %u\n", val);
336 
337 	__set_device_mtu(fd, cfg_ifname, mtu);
338 	val = get_device_mtu(fd, cfg_ifname);
339 	if (val != mtu)
340 		error(1, 0, "unable to set device mtu to %u\n", val);
341 
342 	fprintf(stderr, "device mtu (test): %u\n", val);
343 }
344 
set_pmtu_discover(int fd,bool is_ipv4)345 static void set_pmtu_discover(int fd, bool is_ipv4)
346 {
347 	int level, name, val;
348 
349 	if (is_ipv4) {
350 		level	= SOL_IP;
351 		name	= IP_MTU_DISCOVER;
352 		val	= IP_PMTUDISC_DO;
353 	} else {
354 		level	= SOL_IPV6;
355 		name	= IPV6_MTU_DISCOVER;
356 		val	= IPV6_PMTUDISC_DO;
357 	}
358 
359 	if (setsockopt(fd, level, name, &val, sizeof(val)))
360 		error(1, errno, "setsockopt path mtu");
361 }
362 
get_path_mtu(int fd,bool is_ipv4)363 static unsigned int get_path_mtu(int fd, bool is_ipv4)
364 {
365 	socklen_t vallen;
366 	unsigned int mtu;
367 	int ret;
368 
369 	vallen = sizeof(mtu);
370 	if (is_ipv4)
371 		ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
372 	else
373 		ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
374 
375 	if (ret)
376 		error(1, errno, "getsockopt mtu");
377 
378 
379 	fprintf(stderr, "path mtu (read):  %u\n", mtu);
380 	return mtu;
381 }
382 
383 /* very wordy version of system("ip route add dev lo mtu 1500 127.0.0.3/32") */
set_route_mtu(int mtu,bool is_ipv4)384 static void set_route_mtu(int mtu, bool is_ipv4)
385 {
386 	struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
387 	struct nlmsghdr *nh;
388 	struct rtattr *rta;
389 	struct rtmsg *rt;
390 	char data[NLMSG_ALIGN(sizeof(*nh)) +
391 		  NLMSG_ALIGN(sizeof(*rt)) +
392 		  NLMSG_ALIGN(RTA_LENGTH(sizeof(addr6))) +
393 		  NLMSG_ALIGN(RTA_LENGTH(sizeof(int))) +
394 		  NLMSG_ALIGN(RTA_LENGTH(0) + RTA_LENGTH(sizeof(int)))];
395 	int fd, ret, alen, off = 0;
396 
397 	alen = is_ipv4 ? sizeof(addr4) : sizeof(addr6);
398 
399 	fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
400 	if (fd == -1)
401 		error(1, errno, "socket netlink");
402 
403 	memset(data, 0, sizeof(data));
404 
405 	nh = (void *)data;
406 	nh->nlmsg_type = RTM_NEWROUTE;
407 	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
408 	off += NLMSG_ALIGN(sizeof(*nh));
409 
410 	rt = (void *)(data + off);
411 	rt->rtm_family = is_ipv4 ? AF_INET : AF_INET6;
412 	rt->rtm_table = RT_TABLE_MAIN;
413 	rt->rtm_dst_len = alen << 3;
414 	rt->rtm_protocol = RTPROT_BOOT;
415 	rt->rtm_scope = RT_SCOPE_UNIVERSE;
416 	rt->rtm_type = RTN_UNICAST;
417 	off += NLMSG_ALIGN(sizeof(*rt));
418 
419 	rta = (void *)(data + off);
420 	rta->rta_type = RTA_DST;
421 	rta->rta_len = RTA_LENGTH(alen);
422 	if (is_ipv4)
423 		memcpy(RTA_DATA(rta), &addr4, alen);
424 	else
425 		memcpy(RTA_DATA(rta), &addr6, alen);
426 	off += NLMSG_ALIGN(rta->rta_len);
427 
428 	rta = (void *)(data + off);
429 	rta->rta_type = RTA_OIF;
430 	rta->rta_len = RTA_LENGTH(sizeof(int));
431 	*((int *)(RTA_DATA(rta))) = 1; //if_nametoindex("lo");
432 	off += NLMSG_ALIGN(rta->rta_len);
433 
434 	/* MTU is a subtype in a metrics type */
435 	rta = (void *)(data + off);
436 	rta->rta_type = RTA_METRICS;
437 	rta->rta_len = RTA_LENGTH(0) + RTA_LENGTH(sizeof(int));
438 	off += NLMSG_ALIGN(rta->rta_len);
439 
440 	/* now fill MTU subtype. Note that it fits within above rta_len */
441 	rta = (void *)(((char *) rta) + RTA_LENGTH(0));
442 	rta->rta_type = RTAX_MTU;
443 	rta->rta_len = RTA_LENGTH(sizeof(int));
444 	*((int *)(RTA_DATA(rta))) = mtu;
445 
446 	nh->nlmsg_len = off;
447 
448 	ret = sendto(fd, data, off, 0, (void *)&nladdr, sizeof(nladdr));
449 	if (ret != off)
450 		error(1, errno, "send netlink: %uB != %uB\n", ret, off);
451 
452 	if (close(fd))
453 		error(1, errno, "close netlink");
454 
455 	fprintf(stderr, "route mtu (test): %u\n", mtu);
456 }
457 
__send_one(int fd,struct msghdr * msg,int flags)458 static bool __send_one(int fd, struct msghdr *msg, int flags)
459 {
460 	int ret;
461 
462 	ret = sendmsg(fd, msg, flags);
463 	if (ret == -1 &&
464 	    (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
465 		return false;
466 	if (ret == -1)
467 		error(1, errno, "sendmsg");
468 	if (ret != msg->msg_iov->iov_len)
469 		error(1, 0, "sendto: %d != %llu", ret,
470 			(unsigned long long)msg->msg_iov->iov_len);
471 	if (msg->msg_flags)
472 		error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
473 
474 	return true;
475 }
476 
send_one(int fd,int len,int gso_len,struct sockaddr * addr,socklen_t alen)477 static bool send_one(int fd, int len, int gso_len,
478 		     struct sockaddr *addr, socklen_t alen)
479 {
480 	char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
481 	struct msghdr msg = {0};
482 	struct iovec iov = {0};
483 	struct cmsghdr *cm;
484 
485 	iov.iov_base = buf;
486 	iov.iov_len = len;
487 
488 	msg.msg_iov = &iov;
489 	msg.msg_iovlen = 1;
490 
491 	msg.msg_name = addr;
492 	msg.msg_namelen = alen;
493 
494 	if (gso_len && !cfg_do_setsockopt) {
495 		msg.msg_control = control;
496 		msg.msg_controllen = sizeof(control);
497 
498 		cm = CMSG_FIRSTHDR(&msg);
499 		cm->cmsg_level = SOL_UDP;
500 		cm->cmsg_type = UDP_SEGMENT;
501 		cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
502 		*((uint16_t *) CMSG_DATA(cm)) = gso_len;
503 	}
504 
505 	/* If MSG_MORE, send 1 byte followed by remainder */
506 	if (cfg_do_msgmore && len > 1) {
507 		iov.iov_len = 1;
508 		if (!__send_one(fd, &msg, MSG_MORE))
509 			error(1, 0, "send 1B failed");
510 
511 		iov.iov_base++;
512 		iov.iov_len = len - 1;
513 	}
514 
515 	return __send_one(fd, &msg, 0);
516 }
517 
recv_one(int fd,int flags)518 static int recv_one(int fd, int flags)
519 {
520 	int ret;
521 
522 	ret = recv(fd, buf, sizeof(buf), flags);
523 	if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
524 		return 0;
525 	if (ret == -1)
526 		error(1, errno, "recv");
527 
528 	return ret;
529 }
530 
run_one(struct testcase * test,int fdt,int fdr,struct sockaddr * addr,socklen_t alen)531 static void run_one(struct testcase *test, int fdt, int fdr,
532 		    struct sockaddr *addr, socklen_t alen)
533 {
534 	int i, ret, val, mss;
535 	bool sent;
536 
537 	fprintf(stderr, "ipv%d tx:%d gso:%d %s\n",
538 			addr->sa_family == AF_INET ? 4 : 6,
539 			test->tlen, test->gso_len,
540 			test->tfail ? "(fail)" : "");
541 
542 	val = test->gso_len;
543 	if (cfg_do_setsockopt) {
544 		if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
545 			error(1, errno, "setsockopt udp segment");
546 	}
547 
548 	sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
549 	if (sent && test->tfail)
550 		error(1, 0, "send succeeded while expecting failure");
551 	if (!sent && !test->tfail)
552 		error(1, 0, "send failed while expecting success");
553 	if (!sent)
554 		return;
555 
556 	if (test->gso_len)
557 		mss = test->gso_len;
558 	else
559 		mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
560 
561 
562 	/* Recv all full MSS datagrams */
563 	for (i = 0; i < test->r_num_mss; i++) {
564 		ret = recv_one(fdr, 0);
565 		if (ret != mss)
566 			error(1, 0, "recv.%d: %d != %d", i, ret, mss);
567 	}
568 
569 	/* Recv the non-full last datagram, if tlen was not a multiple of mss */
570 	if (test->r_len_last) {
571 		ret = recv_one(fdr, 0);
572 		if (ret != test->r_len_last)
573 			error(1, 0, "recv.%d: %d != %d (last)",
574 			      i, ret, test->r_len_last);
575 	}
576 
577 	/* Verify received all data */
578 	ret = recv_one(fdr, MSG_DONTWAIT);
579 	if (ret)
580 		error(1, 0, "recv: unexpected datagram");
581 }
582 
run_all(int fdt,int fdr,struct sockaddr * addr,socklen_t alen)583 static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
584 {
585 	struct testcase *tests, *test;
586 
587 	tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
588 
589 	for (test = tests; test->tlen; test++) {
590 		/* if a specific test is given, then skip all others */
591 		if (cfg_specific_test_id == -1 ||
592 		    cfg_specific_test_id == test - tests)
593 			run_one(test, fdt, fdr, addr, alen);
594 	}
595 }
596 
run_test(struct sockaddr * addr,socklen_t alen)597 static void run_test(struct sockaddr *addr, socklen_t alen)
598 {
599 	struct timeval tv = { .tv_usec = 100 * 1000 };
600 	int fdr, fdt, val;
601 
602 	fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
603 	if (fdr == -1)
604 		error(1, errno, "socket r");
605 
606 	if (bind(fdr, addr, alen))
607 		error(1, errno, "bind");
608 
609 	/* Have tests fail quickly instead of hang */
610 	if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
611 		error(1, errno, "setsockopt rcv timeout");
612 
613 	fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
614 	if (fdt == -1)
615 		error(1, errno, "socket t");
616 
617 	/* Do not fragment these datagrams: only succeed if GSO works */
618 	set_pmtu_discover(fdt, addr->sa_family == AF_INET);
619 
620 	if (cfg_do_connectionless) {
621 		set_device_mtu(fdt, CONST_MTU_TEST);
622 		run_all(fdt, fdr, addr, alen);
623 	}
624 
625 	if (cfg_do_connected) {
626 		set_device_mtu(fdt, CONST_MTU_TEST + 100);
627 		set_route_mtu(CONST_MTU_TEST, addr->sa_family == AF_INET);
628 
629 		if (connect(fdt, addr, alen))
630 			error(1, errno, "connect");
631 
632 		val = get_path_mtu(fdt, addr->sa_family == AF_INET);
633 		if (val != CONST_MTU_TEST)
634 			error(1, 0, "bad path mtu %u\n", val);
635 
636 		run_all(fdt, fdr, addr, 0 /* use connected addr */);
637 	}
638 
639 	if (close(fdt))
640 		error(1, errno, "close t");
641 	if (close(fdr))
642 		error(1, errno, "close r");
643 }
644 
run_test_v4(void)645 static void run_test_v4(void)
646 {
647 	struct sockaddr_in addr = {0};
648 
649 	addr.sin_family = AF_INET;
650 	addr.sin_port = htons(cfg_port);
651 	addr.sin_addr = addr4;
652 
653 	run_test((void *)&addr, sizeof(addr));
654 }
655 
run_test_v6(void)656 static void run_test_v6(void)
657 {
658 	struct sockaddr_in6 addr = {0};
659 
660 	addr.sin6_family = AF_INET6;
661 	addr.sin6_port = htons(cfg_port);
662 	addr.sin6_addr = addr6;
663 
664 	run_test((void *)&addr, sizeof(addr));
665 }
666 
parse_opts(int argc,char ** argv)667 static void parse_opts(int argc, char **argv)
668 {
669 	int c;
670 
671 	while ((c = getopt(argc, argv, "46cCmst:")) != -1) {
672 		switch (c) {
673 		case '4':
674 			cfg_do_ipv4 = true;
675 			break;
676 		case '6':
677 			cfg_do_ipv6 = true;
678 			break;
679 		case 'c':
680 			cfg_do_connected = true;
681 			break;
682 		case 'C':
683 			cfg_do_connectionless = true;
684 			break;
685 		case 'm':
686 			cfg_do_msgmore = true;
687 			break;
688 		case 's':
689 			cfg_do_setsockopt = true;
690 			break;
691 		case 't':
692 			cfg_specific_test_id = strtoul(optarg, NULL, 0);
693 			break;
694 		default:
695 			error(1, 0, "%s: parse error", argv[0]);
696 		}
697 	}
698 }
699 
main(int argc,char ** argv)700 int main(int argc, char **argv)
701 {
702 	parse_opts(argc, argv);
703 
704 	if (cfg_do_ipv4)
705 		run_test_v4();
706 	if (cfg_do_ipv6)
707 		run_test_v6();
708 
709 	fprintf(stderr, "OK\n");
710 	return 0;
711 }
712