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 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
40 #include <netinet/in.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_var.h>
62 update_cksum(struct ip *ip)
69 cksump = (uint16_t *)ip;
70 for (cksum = 0, i = 0; i < sizeof(*ip) / sizeof(*cksump); cksump++, i++)
71 cksum += ntohs(*cksump);
72 cksum = (cksum >> 16) + (cksum & 0xffff);
73 cksum = ~(cksum + (cksum >> 16));
74 ip->ip_sum = htons((uint16_t)cksum);
77 static struct lopacket *
78 alloc_lopacket(in_addr_t dstaddr, size_t payloadlen)
81 struct lopacket *packet;
84 pktlen = sizeof(*packet) + payloadlen;
85 packet = malloc(pktlen);
86 ATF_REQUIRE(packet != NULL);
88 memset(packet, 0, pktlen);
89 packet->family = AF_INET;
92 ip->ip_hl = sizeof(struct ip) >> 2;
95 ip->ip_len = htons(sizeof(*ip) + payloadlen);
99 ip->ip_p = IPPROTO_IP;
101 ip->ip_src.s_addr = dstaddr;
102 ip->ip_dst.s_addr = dstaddr;
109 free_lopacket(struct lopacket *packet)
116 write_lopacket(int bpffd, struct lopacket *packet)
122 len = sizeof(packet->family) + ntohs(packet->hdr.ip_len);
123 n = write(bpffd, packet, len);
124 ATF_REQUIRE_MSG(n >= 0, "packet write failed: %s", strerror(errno));
125 ATF_REQUIRE_MSG((size_t)n == len, "wrote %zd bytes instead of %zu",
129 * Loopback packets are dispatched asynchronously, give netisr some
133 ts.tv_nsec = 5000000; /* 5ms */
134 (void)nanosleep(&ts, NULL);
138 open_lobpf(in_addr_t *addrp)
141 struct ifaddrs *ifa, *ifap;
144 fd = open("/dev/bpf0", O_RDWR);
145 if (fd < 0 && errno == ENOENT)
146 atf_tc_skip("no BPF device available");
147 ATF_REQUIRE_MSG(fd >= 0, "open(/dev/bpf0): %s", strerror(errno));
149 error = getifaddrs(&ifap);
150 ATF_REQUIRE(error == 0);
151 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
152 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0 &&
153 ifa->ifa_addr->sa_family == AF_INET)
156 atf_tc_skip("no loopback address found");
158 memset(&ifr, 0, sizeof(ifr));
159 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
160 error = ioctl(fd, BIOCSETIF, &ifr);
161 ATF_REQUIRE_MSG(error == 0, "ioctl(BIOCSETIF): %s", strerror(errno));
163 *addrp = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr.s_addr;
171 get_ipstat(struct ipstat *stat)
176 memset(stat, 0, sizeof(*stat));
178 error = sysctlbyname("net.inet.ip.stats", stat, &len, NULL, 0);
179 ATF_REQUIRE_MSG(error == 0, "sysctl(net.inet.ip.stats) failed: %s",
181 ATF_REQUIRE(len == sizeof(*stat));
184 #define CHECK_IP_COUNTER(oldp, newp, counter) \
185 ATF_REQUIRE_MSG((oldp)->ips_ ## counter < (newp)->ips_ ## counter, \
186 "ips_" #counter " wasn't incremented (%ju vs. %ju)", \
187 (uintmax_t)old.ips_ ## counter, (uintmax_t)new.ips_## counter);
190 * Make sure a fragment with MF set doesn't come after the last fragment of a
191 * packet. Make sure that multiple fragments with MF clear have the same offset
194 ATF_TC(ip_reass__multiple_last_fragments);
195 ATF_TC_HEAD(ip_reass__multiple_last_fragments, tc)
197 atf_tc_set_md_var(tc, "require.user", "root");
199 ATF_TC_BODY(ip_reass__multiple_last_fragments, tc)
201 struct ipstat old, new;
203 struct lopacket *packet1, *packet2, *packet3, *packet4;
208 fd = open_lobpf(&addr);
209 ipid = arc4random_uniform(UINT16_MAX + 1);
211 packet1 = alloc_lopacket(addr, 16);
214 ip->ip_off = htons(0x10);
217 packet2 = alloc_lopacket(addr, 16);
220 ip->ip_off = htons(0x20);
223 packet3 = alloc_lopacket(addr, 16);
226 ip->ip_off = htons(0x8);
229 packet4 = alloc_lopacket(addr, 32);
232 ip->ip_off = htons(0x10);
235 write_lopacket(fd, packet1);
237 /* packet2 comes after packet1. */
239 write_lopacket(fd, packet2);
241 CHECK_IP_COUNTER(&old, &new, fragdropped);
243 /* packet2 comes after packet1 and has MF set. */
244 packet2->hdr.ip_off = htons(IP_MF | 0x20);
245 update_cksum(&packet2->hdr);
247 write_lopacket(fd, packet2);
249 CHECK_IP_COUNTER(&old, &new, fragdropped);
251 /* packet3 comes before packet1 but overlaps. */
253 write_lopacket(fd, packet3);
255 CHECK_IP_COUNTER(&old, &new, fragdropped);
257 /* packet4 has the same offset as packet1 but is longer. */
259 write_lopacket(fd, packet4);
261 CHECK_IP_COUNTER(&old, &new, fragdropped);
264 ATF_REQUIRE(error == 0);
265 free_lopacket(packet1);
266 free_lopacket(packet2);
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());