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/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip_var.h>
59 update_cksum(struct ip *ip)
66 cksump = (uint16_t *)ip;
67 for (cksum = 0, i = 0; i < sizeof(*ip) / sizeof(*cksump); cksump++, i++)
68 cksum += ntohs(*cksump);
69 cksum = (cksum >> 16) + (cksum & 0xffff);
70 cksum = ~(cksum + (cksum >> 16));
71 ip->ip_sum = htons((uint16_t)cksum);
74 static struct lopacket *
75 alloc_lopacket(in_addr_t dstaddr, size_t payloadlen)
78 struct lopacket *packet;
81 pktlen = sizeof(*packet) + payloadlen;
82 packet = malloc(pktlen);
83 ATF_REQUIRE(packet != NULL);
85 memset(packet, 0, pktlen);
86 packet->family = AF_INET;
89 ip->ip_hl = sizeof(struct ip) >> 2;
92 ip->ip_len = htons(sizeof(*ip) + payloadlen);
96 ip->ip_p = IPPROTO_IP;
98 ip->ip_src.s_addr = dstaddr;
99 ip->ip_dst.s_addr = dstaddr;
106 free_lopacket(struct lopacket *packet)
113 write_lopacket(int bpffd, struct lopacket *packet)
119 len = sizeof(packet->family) + ntohs(packet->hdr.ip_len);
120 n = write(bpffd, packet, len);
121 ATF_REQUIRE_MSG(n >= 0, "packet write failed: %s", strerror(errno));
122 ATF_REQUIRE_MSG((size_t)n == len, "wrote %zd bytes instead of %zu",
126 * Loopback packets are dispatched asynchronously, give netisr some
130 ts.tv_nsec = 5000000; /* 5ms */
131 (void)nanosleep(&ts, NULL);
135 open_lobpf(in_addr_t *addrp)
138 struct ifaddrs *ifa, *ifap;
141 fd = open("/dev/bpf0", O_RDWR);
142 if (fd < 0 && errno == ENOENT)
143 atf_tc_skip("no BPF device available");
144 ATF_REQUIRE_MSG(fd >= 0, "open(/dev/bpf0): %s", strerror(errno));
146 error = getifaddrs(&ifap);
147 ATF_REQUIRE(error == 0);
148 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
149 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0 &&
150 ifa->ifa_addr->sa_family == AF_INET)
153 atf_tc_skip("no loopback address found");
155 memset(&ifr, 0, sizeof(ifr));
156 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
157 error = ioctl(fd, BIOCSETIF, &ifr);
158 ATF_REQUIRE_MSG(error == 0, "ioctl(BIOCSETIF): %s", strerror(errno));
160 *addrp = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr.s_addr;
168 get_ipstat(struct ipstat *stat)
173 memset(stat, 0, sizeof(*stat));
175 error = sysctlbyname("net.inet.ip.stats", stat, &len, NULL, 0);
176 ATF_REQUIRE_MSG(error == 0, "sysctl(net.inet.ip.stats) failed: %s",
178 ATF_REQUIRE(len == sizeof(*stat));
181 #define CHECK_IP_COUNTER(oldp, newp, counter) \
182 ATF_REQUIRE_MSG((oldp)->ips_ ## counter < (newp)->ips_ ## counter, \
183 "ips_" #counter " wasn't incremented (%ju vs. %ju)", \
184 (uintmax_t)old.ips_ ## counter, (uintmax_t)new.ips_## counter);
187 * Make sure a fragment with MF set doesn't come after the last fragment of a
188 * packet. Make sure that multiple fragments with MF clear have the same offset
191 ATF_TC(ip_reass__multiple_last_fragments);
192 ATF_TC_HEAD(ip_reass__multiple_last_fragments, tc)
194 atf_tc_set_md_var(tc, "require.user", "root");
196 ATF_TC_BODY(ip_reass__multiple_last_fragments, tc)
198 struct ipstat old, new;
200 struct lopacket *packet1, *packet2, *packet3, *packet4;
205 fd = open_lobpf(&addr);
206 ipid = arc4random_uniform(UINT16_MAX + 1);
208 packet1 = alloc_lopacket(addr, 16);
211 ip->ip_off = htons(0x10);
214 packet2 = alloc_lopacket(addr, 16);
217 ip->ip_off = htons(0x20);
220 packet3 = alloc_lopacket(addr, 16);
223 ip->ip_off = htons(0x8);
226 packet4 = alloc_lopacket(addr, 32);
229 ip->ip_off = htons(0x10);
232 write_lopacket(fd, packet1);
234 /* packet2 comes after packet1. */
236 write_lopacket(fd, packet2);
238 CHECK_IP_COUNTER(&old, &new, fragdropped);
240 /* packet2 comes after packet1 and has MF set. */
241 packet2->hdr.ip_off = htons(IP_MF | 0x20);
242 update_cksum(&packet2->hdr);
244 write_lopacket(fd, packet2);
246 CHECK_IP_COUNTER(&old, &new, fragdropped);
248 /* packet3 comes before packet1 but overlaps. */
250 write_lopacket(fd, packet3);
252 CHECK_IP_COUNTER(&old, &new, fragdropped);
254 /* packet4 has the same offset as packet1 but is longer. */
256 write_lopacket(fd, packet4);
258 CHECK_IP_COUNTER(&old, &new, fragdropped);
261 ATF_REQUIRE(error == 0);
262 free_lopacket(packet1);
263 free_lopacket(packet2);
264 free_lopacket(packet3);
265 free_lopacket(packet4);
269 * Make sure that we reject zero-length fragments.
271 ATF_TC(ip_reass__zero_length_fragment);
272 ATF_TC_HEAD(ip_reass__zero_length_fragment, tc)
274 atf_tc_set_md_var(tc, "require.user", "root");
276 ATF_TC_BODY(ip_reass__zero_length_fragment, tc)
278 struct ipstat old, new;
280 struct lopacket *packet1, *packet2;
285 fd = open_lobpf(&addr);
286 ipid = arc4random_uniform(UINT16_MAX + 1);
289 * Create two packets, one with MF set, one without.
291 packet1 = alloc_lopacket(addr, 0);
294 ip->ip_off = htons(IP_MF | 0x10);
297 packet2 = alloc_lopacket(addr, 0);
300 ip->ip_off = htons(0x10);
304 write_lopacket(fd, packet1);
306 CHECK_IP_COUNTER(&old, &new, toosmall);
307 CHECK_IP_COUNTER(&old, &new, fragdropped);
310 write_lopacket(fd, packet2);
312 CHECK_IP_COUNTER(&old, &new, toosmall);
313 CHECK_IP_COUNTER(&old, &new, fragdropped);
316 ATF_REQUIRE(error == 0);
317 free_lopacket(packet1);
318 free_lopacket(packet2);
321 ATF_TC(ip_reass__large_fragment);
322 ATF_TC_HEAD(ip_reass__large_fragment, tc)
324 atf_tc_set_md_var(tc, "require.user", "root");
326 ATF_TC_BODY(ip_reass__large_fragment, tc)
328 struct ipstat old, new;
330 struct lopacket *packet1, *packet2;
335 fd = open_lobpf(&addr);
336 ipid = arc4random_uniform(UINT16_MAX + 1);
339 * Create two packets, one with MF set, one without.
341 * 16 + (0x1fff << 3) > IP_MAXPACKET, so these should fail the check.
343 packet1 = alloc_lopacket(addr, 16);
346 ip->ip_off = htons(IP_MF | 0x1fff);
349 packet2 = alloc_lopacket(addr, 16);
352 ip->ip_off = htons(0x1fff);
356 write_lopacket(fd, packet1);
358 CHECK_IP_COUNTER(&old, &new, toolong);
359 CHECK_IP_COUNTER(&old, &new, fragdropped);
362 write_lopacket(fd, packet2);
364 CHECK_IP_COUNTER(&old, &new, toolong);
365 CHECK_IP_COUNTER(&old, &new, fragdropped);
368 ATF_REQUIRE(error == 0);
369 free_lopacket(packet1);
370 free_lopacket(packet2);
375 ATF_TP_ADD_TC(tp, ip_reass__multiple_last_fragments);
376 ATF_TP_ADD_TC(tp, ip_reass__zero_length_fragment);
377 ATF_TP_ADD_TC(tp, ip_reass__large_fragment);
379 return (atf_no_error());