Path: blob/main/tests/sys/netinet/ip_reass_test.c
101184 views
/*-1* Copyright (c) 2018 The FreeBSD Foundation2*3* This software was developed by Mark Johnston under sponsorship from4* the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are8* met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in13* the documentation and/or other materials provided with the14* distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/ioctl.h>31#include <sys/socket.h>32#include <sys/sysctl.h>3334#include <net/bpf.h>35#include <net/if.h>36#include <netinet/in.h>37#include <netinet/ip.h>38#include <netinet/ip_var.h>3940#include <err.h>41#include <errno.h>42#include <fcntl.h>43#include <ifaddrs.h>44#include <stdint.h>45#include <stdlib.h>46#include <time.h>47#include <unistd.h>4849#include <atf-c.h>5051struct lopacket {52u_int family;53struct ip hdr;54char payload[];55};5657static void58update_cksum(struct ip *ip)59{60size_t i;61uint32_t cksum;62uint8_t *cksump;63uint16_t tmp;6465ip->ip_sum = 0;66cksump = (char *)ip;67for (cksum = 0, i = 0; i < sizeof(*ip) / sizeof(uint16_t); i++) {68tmp = *cksump++;69tmp = tmp << 8 | *cksump++;70cksum += ntohs(tmp);71}72cksum = (cksum >> 16) + (cksum & 0xffff);73cksum = ~(cksum + (cksum >> 16));74ip->ip_sum = htons((uint16_t)cksum);75}7677static struct lopacket *78alloc_lopacket(in_addr_t dstaddr, size_t payloadlen)79{80struct ip *ip;81struct lopacket *packet;82size_t pktlen;8384pktlen = sizeof(*packet) + payloadlen;85packet = malloc(pktlen);86ATF_REQUIRE(packet != NULL);8788memset(packet, 0, pktlen);89packet->family = AF_INET;9091ip = &packet->hdr;92ip->ip_hl = sizeof(struct ip) >> 2;93ip->ip_v = 4;94ip->ip_tos = 0;95ip->ip_len = htons(sizeof(*ip) + payloadlen);96ip->ip_id = 0;97ip->ip_off = 0;98ip->ip_ttl = 1;99ip->ip_p = IPPROTO_IP;100ip->ip_sum = 0;101ip->ip_src.s_addr = dstaddr;102ip->ip_dst.s_addr = dstaddr;103update_cksum(ip);104105return (packet);106}107108static void109free_lopacket(struct lopacket *packet)110{111112free(packet);113}114115static void116write_lopacket(int bpffd, struct lopacket *packet)117{118struct timespec ts;119ssize_t n;120size_t len;121122len = sizeof(packet->family) + ntohs(packet->hdr.ip_len);123n = write(bpffd, packet, len);124ATF_REQUIRE_MSG(n >= 0, "packet write failed: %s", strerror(errno));125ATF_REQUIRE_MSG((size_t)n == len, "wrote %zd bytes instead of %zu",126n, len);127128/*129* Loopback packets are dispatched asynchronously, give netisr some130* time.131*/132ts.tv_sec = 0;133ts.tv_nsec = 5000000; /* 5ms */134(void)nanosleep(&ts, NULL);135}136137static int138open_lobpf(in_addr_t *addrp)139{140struct ifreq ifr;141struct ifaddrs *ifa, *ifap;142int error, fd;143144fd = open("/dev/bpf0", O_RDWR);145if (fd < 0 && errno == ENOENT)146atf_tc_skip("no BPF device available");147ATF_REQUIRE_MSG(fd >= 0, "open(/dev/bpf0): %s", strerror(errno));148149error = getifaddrs(&ifap);150ATF_REQUIRE(error == 0);151for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)152if ((ifa->ifa_flags & IFF_LOOPBACK) != 0 &&153ifa->ifa_addr->sa_family == AF_INET)154break;155if (ifa == NULL)156atf_tc_skip("no loopback address found");157158memset(&ifr, 0, sizeof(ifr));159strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);160error = ioctl(fd, BIOCSETIF, &ifr);161ATF_REQUIRE_MSG(error == 0, "ioctl(BIOCSETIF): %s", strerror(errno));162163*addrp = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr.s_addr;164165freeifaddrs(ifap);166167return (fd);168}169170static void171get_ipstat(struct ipstat *stat)172{173size_t len;174int error;175176memset(stat, 0, sizeof(*stat));177len = sizeof(*stat);178error = sysctlbyname("net.inet.ip.stats", stat, &len, NULL, 0);179ATF_REQUIRE_MSG(error == 0, "sysctl(net.inet.ip.stats) failed: %s",180strerror(errno));181ATF_REQUIRE(len == sizeof(*stat));182}183184#define CHECK_IP_COUNTER(oldp, newp, counter) \185ATF_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);188189/*190* Make sure a fragment with MF set doesn't come after the last fragment of a191* packet. Make sure that multiple fragments with MF clear have the same offset192* and length.193*/194ATF_TC(ip_reass__multiple_last_fragments);195ATF_TC_HEAD(ip_reass__multiple_last_fragments, tc)196{197atf_tc_set_md_var(tc, "require.user", "root");198}199ATF_TC_BODY(ip_reass__multiple_last_fragments, tc)200{201struct ipstat old, new;202struct ip *ip;203struct lopacket *packet1, *packet2, *packet3, *packet4;204in_addr_t addr;205int error, fd;206uint16_t ipid;207208fd = open_lobpf(&addr);209ipid = arc4random_uniform(UINT16_MAX + 1);210211packet1 = alloc_lopacket(addr, 16);212ip = &packet1->hdr;213ip->ip_id = ipid;214ip->ip_off = htons(0x10);215update_cksum(ip);216217packet2 = alloc_lopacket(addr, 16);218ip = &packet2->hdr;219ip->ip_id = ipid;220ip->ip_off = htons(0x20);221update_cksum(ip);222223packet3 = alloc_lopacket(addr, 16);224ip = &packet3->hdr;225ip->ip_id = ipid;226ip->ip_off = htons(0x8);227update_cksum(ip);228229packet4 = alloc_lopacket(addr, 32);230ip = &packet4->hdr;231ip->ip_id = ipid;232ip->ip_off = htons(0x10);233update_cksum(ip);234235write_lopacket(fd, packet1);236237/* packet2 comes after packet1. */238get_ipstat(&old);239write_lopacket(fd, packet2);240get_ipstat(&new);241CHECK_IP_COUNTER(&old, &new, fragdropped);242243/* packet2 comes after packet1 and has MF set. */244packet2->hdr.ip_off = htons(IP_MF | 0x20);245update_cksum(&packet2->hdr);246get_ipstat(&old);247write_lopacket(fd, packet2);248get_ipstat(&new);249CHECK_IP_COUNTER(&old, &new, fragdropped);250251/* packet3 comes before packet1 but overlaps. */252get_ipstat(&old);253write_lopacket(fd, packet3);254get_ipstat(&new);255CHECK_IP_COUNTER(&old, &new, fragdropped);256257/* packet4 has the same offset as packet1 but is longer. */258get_ipstat(&old);259write_lopacket(fd, packet4);260get_ipstat(&new);261CHECK_IP_COUNTER(&old, &new, fragdropped);262263error = close(fd);264ATF_REQUIRE(error == 0);265free_lopacket(packet1);266free_lopacket(packet2);267free_lopacket(packet3);268free_lopacket(packet4);269}270271/*272* Make sure that we reject zero-length fragments.273*/274ATF_TC(ip_reass__zero_length_fragment);275ATF_TC_HEAD(ip_reass__zero_length_fragment, tc)276{277atf_tc_set_md_var(tc, "require.user", "root");278}279ATF_TC_BODY(ip_reass__zero_length_fragment, tc)280{281struct ipstat old, new;282struct ip *ip;283struct lopacket *packet1, *packet2;284in_addr_t addr;285int error, fd;286uint16_t ipid;287288fd = open_lobpf(&addr);289ipid = arc4random_uniform(UINT16_MAX + 1);290291/*292* Create two packets, one with MF set, one without.293*/294packet1 = alloc_lopacket(addr, 0);295ip = &packet1->hdr;296ip->ip_id = ipid;297ip->ip_off = htons(IP_MF | 0x10);298update_cksum(ip);299300packet2 = alloc_lopacket(addr, 0);301ip = &packet2->hdr;302ip->ip_id = ~ipid;303ip->ip_off = htons(0x10);304update_cksum(ip);305306get_ipstat(&old);307write_lopacket(fd, packet1);308get_ipstat(&new);309CHECK_IP_COUNTER(&old, &new, toosmall);310CHECK_IP_COUNTER(&old, &new, fragdropped);311312get_ipstat(&old);313write_lopacket(fd, packet2);314get_ipstat(&new);315CHECK_IP_COUNTER(&old, &new, toosmall);316CHECK_IP_COUNTER(&old, &new, fragdropped);317318error = close(fd);319ATF_REQUIRE(error == 0);320free_lopacket(packet1);321free_lopacket(packet2);322}323324ATF_TC(ip_reass__large_fragment);325ATF_TC_HEAD(ip_reass__large_fragment, tc)326{327atf_tc_set_md_var(tc, "require.user", "root");328}329ATF_TC_BODY(ip_reass__large_fragment, tc)330{331struct ipstat old, new;332struct ip *ip;333struct lopacket *packet1, *packet2;334in_addr_t addr;335int error, fd;336uint16_t ipid;337338fd = open_lobpf(&addr);339ipid = arc4random_uniform(UINT16_MAX + 1);340341/*342* Create two packets, one with MF set, one without.343*344* 16 + (0x1fff << 3) > IP_MAXPACKET, so these should fail the check.345*/346packet1 = alloc_lopacket(addr, 16);347ip = &packet1->hdr;348ip->ip_id = ipid;349ip->ip_off = htons(IP_MF | 0x1fff);350update_cksum(ip);351352packet2 = alloc_lopacket(addr, 16);353ip = &packet2->hdr;354ip->ip_id = ipid;355ip->ip_off = htons(0x1fff);356update_cksum(ip);357358get_ipstat(&old);359write_lopacket(fd, packet1);360get_ipstat(&new);361CHECK_IP_COUNTER(&old, &new, toolong);362CHECK_IP_COUNTER(&old, &new, fragdropped);363364get_ipstat(&old);365write_lopacket(fd, packet2);366get_ipstat(&new);367CHECK_IP_COUNTER(&old, &new, toolong);368CHECK_IP_COUNTER(&old, &new, fragdropped);369370error = close(fd);371ATF_REQUIRE(error == 0);372free_lopacket(packet1);373free_lopacket(packet2);374}375376ATF_TP_ADD_TCS(tp)377{378ATF_TP_ADD_TC(tp, ip_reass__multiple_last_fragments);379ATF_TP_ADD_TC(tp, ip_reass__zero_length_fragment);380ATF_TP_ADD_TC(tp, ip_reass__large_fragment);381382return (atf_no_error());383}384385386