2 * Copyright (c) 2018 The FreeBSD Foundation
4 * This software was developed by Mark Johnston under sponsorship from
5 * the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip_var.h>
60 update_cksum(struct ip *ip)
67 cksump = (uint16_t *)ip;
68 for (cksum = 0, i = 0; i < sizeof(*ip) / sizeof(*cksump); cksump++, i++)
69 cksum += ntohs(*cksump);
70 cksum = (cksum >> 16) + (cksum & 0xffff);
71 cksum = ~(cksum + (cksum >> 16));
72 ip->ip_sum = htons((uint16_t)cksum);
75 static struct lopacket *
76 alloc_lopacket(in_addr_t dstaddr, size_t payloadlen)
79 struct lopacket *packet;
82 pktlen = sizeof(*packet) + payloadlen;
83 packet = malloc(pktlen);
84 ATF_REQUIRE(packet != NULL);
86 memset(packet, 0, pktlen);
87 packet->family = AF_INET;
90 ip->ip_hl = sizeof(struct ip) >> 2;
93 ip->ip_len = htons(sizeof(*ip) + payloadlen);
97 ip->ip_p = IPPROTO_IP;
99 ip->ip_src.s_addr = dstaddr;
100 ip->ip_dst.s_addr = dstaddr;
107 free_lopacket(struct lopacket *packet)
114 write_lopacket(int bpffd, struct lopacket *packet)
120 len = sizeof(packet->family) + ntohs(packet->hdr.ip_len);
121 n = write(bpffd, packet, len);
122 ATF_REQUIRE_MSG(n >= 0, "packet write failed: %s", strerror(errno));
123 ATF_REQUIRE_MSG((size_t)n == len, "wrote %zd bytes instead of %zu",
127 * Loopback packets are dispatched asynchronously, give netisr some
131 ts.tv_nsec = 5000000; /* 5ms */
132 (void)nanosleep(&ts, NULL);
136 open_lobpf(in_addr_t *addrp)
139 struct ifaddrs *ifa, *ifap;
142 fd = open("/dev/bpf0", O_RDWR);
143 if (fd < 0 && errno == ENOENT)
144 atf_tc_skip("no BPF device available");
145 ATF_REQUIRE_MSG(fd >= 0, "open(/dev/bpf0): %s", strerror(errno));
147 error = getifaddrs(&ifap);
148 ATF_REQUIRE(error == 0);
149 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
150 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0 &&
151 ifa->ifa_addr->sa_family == AF_INET)
154 atf_tc_skip("no loopback address found");
156 memset(&ifr, 0, sizeof(ifr));
157 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
158 error = ioctl(fd, BIOCSETIF, &ifr);
159 ATF_REQUIRE_MSG(error == 0, "ioctl(BIOCSETIF): %s", strerror(errno));
161 *addrp = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr.s_addr;
169 get_ipstat(struct ipstat *stat)
174 memset(stat, 0, sizeof(*stat));
176 error = sysctlbyname("net.inet.ip.stats", stat, &len, NULL, 0);
177 ATF_REQUIRE_MSG(error == 0, "sysctl(net.inet.ip.stats) failed: %s",
179 ATF_REQUIRE(len == sizeof(*stat));
182 #define CHECK_IP_COUNTER(oldp, newp, counter) \
183 ATF_REQUIRE_MSG((oldp)->ips_ ## counter < (newp)->ips_ ## counter, \
184 "ips_" #counter " wasn't incremented (%ju vs. %ju)", \
185 (uintmax_t)old.ips_ ## counter, (uintmax_t)new.ips_## counter);
188 * Make sure a fragment with MF set doesn't come after the last fragment of a
189 * packet. Make sure that multiple fragments with MF clear have the same offset
192 ATF_TC(ip_reass__multiple_last_fragments);
193 ATF_TC_HEAD(ip_reass__multiple_last_fragments, tc)
195 atf_tc_set_md_var(tc, "require.user", "root");
197 ATF_TC_BODY(ip_reass__multiple_last_fragments, tc)
199 struct ipstat old, new;
201 struct lopacket *packet1, *packet2, *packet3, *packet4;
206 fd = open_lobpf(&addr);
207 ipid = arc4random_uniform(UINT16_MAX + 1);
209 packet1 = alloc_lopacket(addr, 16);
212 ip->ip_off = htons(0x10);
215 packet2 = alloc_lopacket(addr, 16);
218 ip->ip_off = htons(0x20);
221 packet3 = alloc_lopacket(addr, 16);
224 ip->ip_off = htons(0x8);
227 packet4 = alloc_lopacket(addr, 32);
230 ip->ip_off = htons(0x10);
233 write_lopacket(fd, packet1);
235 /* packet2 comes after packet1. */
237 write_lopacket(fd, packet2);
239 CHECK_IP_COUNTER(&old, &new, fragdropped);
241 /* packet2 comes after packet1 and has MF set. */
242 packet2->hdr.ip_off = htons(IP_MF | 0x20);
243 update_cksum(&packet2->hdr);
245 write_lopacket(fd, packet2);
247 CHECK_IP_COUNTER(&old, &new, fragdropped);
249 /* packet3 comes before packet1 but overlaps. */
251 write_lopacket(fd, packet3);
253 CHECK_IP_COUNTER(&old, &new, fragdropped);
255 /* packet4 has the same offset as packet1 but is longer. */
257 write_lopacket(fd, packet4);
259 CHECK_IP_COUNTER(&old, &new, fragdropped);
262 ATF_REQUIRE(error == 0);
263 free_lopacket(packet1);
264 free_lopacket(packet2);
265 free_lopacket(packet3);
266 free_lopacket(packet4);
270 * Make sure that we reject zero-length fragments.
272 ATF_TC(ip_reass__zero_length_fragment);
273 ATF_TC_HEAD(ip_reass__zero_length_fragment, tc)
275 atf_tc_set_md_var(tc, "require.user", "root");
277 ATF_TC_BODY(ip_reass__zero_length_fragment, tc)
279 struct ipstat old, new;
281 struct lopacket *packet1, *packet2;
286 fd = open_lobpf(&addr);
287 ipid = arc4random_uniform(UINT16_MAX + 1);
290 * Create two packets, one with MF set, one without.
292 packet1 = alloc_lopacket(addr, 0);
295 ip->ip_off = htons(IP_MF | 0x10);
298 packet2 = alloc_lopacket(addr, 0);
301 ip->ip_off = htons(0x10);
305 write_lopacket(fd, packet1);
307 CHECK_IP_COUNTER(&old, &new, toosmall);
308 CHECK_IP_COUNTER(&old, &new, fragdropped);
311 write_lopacket(fd, packet2);
313 CHECK_IP_COUNTER(&old, &new, toosmall);
314 CHECK_IP_COUNTER(&old, &new, fragdropped);
317 ATF_REQUIRE(error == 0);
318 free_lopacket(packet1);
319 free_lopacket(packet2);
322 ATF_TC(ip_reass__large_fragment);
323 ATF_TC_HEAD(ip_reass__large_fragment, tc)
325 atf_tc_set_md_var(tc, "require.user", "root");
327 ATF_TC_BODY(ip_reass__large_fragment, tc)
329 struct ipstat old, new;
331 struct lopacket *packet1, *packet2;
336 fd = open_lobpf(&addr);
337 ipid = arc4random_uniform(UINT16_MAX + 1);
340 * Create two packets, one with MF set, one without.
342 * 16 + (0x1fff << 3) > IP_MAXPACKET, so these should fail the check.
344 packet1 = alloc_lopacket(addr, 16);
347 ip->ip_off = htons(IP_MF | 0x1fff);
350 packet2 = alloc_lopacket(addr, 16);
353 ip->ip_off = htons(0x1fff);
357 write_lopacket(fd, packet1);
359 CHECK_IP_COUNTER(&old, &new, toolong);
360 CHECK_IP_COUNTER(&old, &new, fragdropped);
363 write_lopacket(fd, packet2);
365 CHECK_IP_COUNTER(&old, &new, toolong);
366 CHECK_IP_COUNTER(&old, &new, fragdropped);
369 ATF_REQUIRE(error == 0);
370 free_lopacket(packet1);
371 free_lopacket(packet2);
376 ATF_TP_ADD_TC(tp, ip_reass__multiple_last_fragments);
377 ATF_TP_ADD_TC(tp, ip_reass__zero_length_fragment);
378 ATF_TP_ADD_TC(tp, ip_reass__large_fragment);
380 return (atf_no_error());