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