1*43a7c3efSYan Zhai /* SPDX-License-Identifier: GPL-2.0 */
2*43a7c3efSYan Zhai 
3*43a7c3efSYan Zhai #ifndef __LWT_HELPERS_H
4*43a7c3efSYan Zhai #define __LWT_HELPERS_H
5*43a7c3efSYan Zhai 
6*43a7c3efSYan Zhai #include <time.h>
7*43a7c3efSYan Zhai #include <net/if.h>
8*43a7c3efSYan Zhai #include <linux/if_tun.h>
9*43a7c3efSYan Zhai #include <linux/icmp.h>
10*43a7c3efSYan Zhai 
11*43a7c3efSYan Zhai #include "test_progs.h"
12*43a7c3efSYan Zhai 
13*43a7c3efSYan Zhai #define log_err(MSG, ...) \
14*43a7c3efSYan Zhai 	fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
15*43a7c3efSYan Zhai 		__FILE__, __LINE__, strerror(errno), ##__VA_ARGS__)
16*43a7c3efSYan Zhai 
17*43a7c3efSYan Zhai #define RUN_TEST(name)                                                        \
18*43a7c3efSYan Zhai 	({                                                                    \
19*43a7c3efSYan Zhai 		if (test__start_subtest(#name))                               \
20*43a7c3efSYan Zhai 			if (ASSERT_OK(netns_create(), "netns_create")) {      \
21*43a7c3efSYan Zhai 				struct nstoken *token = open_netns(NETNS);    \
22*43a7c3efSYan Zhai 				if (ASSERT_OK_PTR(token, "setns")) {          \
23*43a7c3efSYan Zhai 					test_ ## name();                      \
24*43a7c3efSYan Zhai 					close_netns(token);                   \
25*43a7c3efSYan Zhai 				}                                             \
26*43a7c3efSYan Zhai 				netns_delete();                               \
27*43a7c3efSYan Zhai 			}                                                     \
28*43a7c3efSYan Zhai 	})
29*43a7c3efSYan Zhai 
30*43a7c3efSYan Zhai #define NETNS "ns_lwt"
31*43a7c3efSYan Zhai 
netns_create(void)32*43a7c3efSYan Zhai static inline int netns_create(void)
33*43a7c3efSYan Zhai {
34*43a7c3efSYan Zhai 	return system("ip netns add " NETNS);
35*43a7c3efSYan Zhai }
36*43a7c3efSYan Zhai 
netns_delete(void)37*43a7c3efSYan Zhai static inline int netns_delete(void)
38*43a7c3efSYan Zhai {
39*43a7c3efSYan Zhai 	return system("ip netns del " NETNS ">/dev/null 2>&1");
40*43a7c3efSYan Zhai }
41*43a7c3efSYan Zhai 
open_tuntap(const char * dev_name,bool need_mac)42*43a7c3efSYan Zhai static int open_tuntap(const char *dev_name, bool need_mac)
43*43a7c3efSYan Zhai {
44*43a7c3efSYan Zhai 	int err = 0;
45*43a7c3efSYan Zhai 	struct ifreq ifr;
46*43a7c3efSYan Zhai 	int fd = open("/dev/net/tun", O_RDWR);
47*43a7c3efSYan Zhai 
48*43a7c3efSYan Zhai 	if (!ASSERT_GT(fd, 0, "open(/dev/net/tun)"))
49*43a7c3efSYan Zhai 		return -1;
50*43a7c3efSYan Zhai 
51*43a7c3efSYan Zhai 	ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN);
52*43a7c3efSYan Zhai 	memcpy(ifr.ifr_name, dev_name, IFNAMSIZ);
53*43a7c3efSYan Zhai 
54*43a7c3efSYan Zhai 	err = ioctl(fd, TUNSETIFF, &ifr);
55*43a7c3efSYan Zhai 	if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) {
56*43a7c3efSYan Zhai 		close(fd);
57*43a7c3efSYan Zhai 		return -1;
58*43a7c3efSYan Zhai 	}
59*43a7c3efSYan Zhai 
60*43a7c3efSYan Zhai 	err = fcntl(fd, F_SETFL, O_NONBLOCK);
61*43a7c3efSYan Zhai 	if (!ASSERT_OK(err, "fcntl(O_NONBLOCK)")) {
62*43a7c3efSYan Zhai 		close(fd);
63*43a7c3efSYan Zhai 		return -1;
64*43a7c3efSYan Zhai 	}
65*43a7c3efSYan Zhai 
66*43a7c3efSYan Zhai 	return fd;
67*43a7c3efSYan Zhai }
68*43a7c3efSYan Zhai 
69*43a7c3efSYan Zhai #define ICMP_PAYLOAD_SIZE     100
70*43a7c3efSYan Zhai 
71*43a7c3efSYan Zhai /* Match an ICMP packet with payload len ICMP_PAYLOAD_SIZE */
__expect_icmp_ipv4(char * buf,ssize_t len)72*43a7c3efSYan Zhai static int __expect_icmp_ipv4(char *buf, ssize_t len)
73*43a7c3efSYan Zhai {
74*43a7c3efSYan Zhai 	struct iphdr *ip = (struct iphdr *)buf;
75*43a7c3efSYan Zhai 	struct icmphdr *icmp = (struct icmphdr *)(ip + 1);
76*43a7c3efSYan Zhai 	ssize_t min_header_len = sizeof(*ip) + sizeof(*icmp);
77*43a7c3efSYan Zhai 
78*43a7c3efSYan Zhai 	if (len < min_header_len)
79*43a7c3efSYan Zhai 		return -1;
80*43a7c3efSYan Zhai 
81*43a7c3efSYan Zhai 	if (ip->protocol != IPPROTO_ICMP)
82*43a7c3efSYan Zhai 		return -1;
83*43a7c3efSYan Zhai 
84*43a7c3efSYan Zhai 	if (icmp->type != ICMP_ECHO)
85*43a7c3efSYan Zhai 		return -1;
86*43a7c3efSYan Zhai 
87*43a7c3efSYan Zhai 	return len == ICMP_PAYLOAD_SIZE + min_header_len;
88*43a7c3efSYan Zhai }
89*43a7c3efSYan Zhai 
90*43a7c3efSYan Zhai typedef int (*filter_t) (char *, ssize_t);
91*43a7c3efSYan Zhai 
92*43a7c3efSYan Zhai /* wait_for_packet - wait for a packet that matches the filter
93*43a7c3efSYan Zhai  *
94*43a7c3efSYan Zhai  * @fd: tun fd/packet socket to read packet
95*43a7c3efSYan Zhai  * @filter: filter function, returning 1 if matches
96*43a7c3efSYan Zhai  * @timeout: timeout to wait for the packet
97*43a7c3efSYan Zhai  *
98*43a7c3efSYan Zhai  * Returns 1 if a matching packet is read, 0 if timeout expired, -1 on error.
99*43a7c3efSYan Zhai  */
wait_for_packet(int fd,filter_t filter,struct timeval * timeout)100*43a7c3efSYan Zhai static int wait_for_packet(int fd, filter_t filter, struct timeval *timeout)
101*43a7c3efSYan Zhai {
102*43a7c3efSYan Zhai 	char buf[4096];
103*43a7c3efSYan Zhai 	int max_retry = 5; /* in case we read some spurious packets */
104*43a7c3efSYan Zhai 	fd_set fds;
105*43a7c3efSYan Zhai 
106*43a7c3efSYan Zhai 	FD_ZERO(&fds);
107*43a7c3efSYan Zhai 	while (max_retry--) {
108*43a7c3efSYan Zhai 		/* Linux modifies timeout arg... So make a copy */
109*43a7c3efSYan Zhai 		struct timeval copied_timeout = *timeout;
110*43a7c3efSYan Zhai 		ssize_t ret = -1;
111*43a7c3efSYan Zhai 
112*43a7c3efSYan Zhai 		FD_SET(fd, &fds);
113*43a7c3efSYan Zhai 
114*43a7c3efSYan Zhai 		ret = select(1 + fd, &fds, NULL, NULL, &copied_timeout);
115*43a7c3efSYan Zhai 		if (ret <= 0) {
116*43a7c3efSYan Zhai 			if (errno == EINTR)
117*43a7c3efSYan Zhai 				continue;
118*43a7c3efSYan Zhai 			else if (errno == EAGAIN || ret == 0)
119*43a7c3efSYan Zhai 				return 0;
120*43a7c3efSYan Zhai 
121*43a7c3efSYan Zhai 			log_err("select failed");
122*43a7c3efSYan Zhai 			return -1;
123*43a7c3efSYan Zhai 		}
124*43a7c3efSYan Zhai 
125*43a7c3efSYan Zhai 		ret = read(fd, buf, sizeof(buf));
126*43a7c3efSYan Zhai 
127*43a7c3efSYan Zhai 		if (ret <= 0) {
128*43a7c3efSYan Zhai 			log_err("read(dev): %ld", ret);
129*43a7c3efSYan Zhai 			return -1;
130*43a7c3efSYan Zhai 		}
131*43a7c3efSYan Zhai 
132*43a7c3efSYan Zhai 		if (filter && filter(buf, ret) > 0)
133*43a7c3efSYan Zhai 			return 1;
134*43a7c3efSYan Zhai 	}
135*43a7c3efSYan Zhai 
136*43a7c3efSYan Zhai 	return 0;
137*43a7c3efSYan Zhai }
138*43a7c3efSYan Zhai 
139*43a7c3efSYan Zhai #endif /* __LWT_HELPERS_H */
140