Path: blob/main/tests/sys/netinet/ip6_v4mapped_test.c
39483 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2022 Michael J. Karels.4* Copyright (c) 2020 Netflix, Inc.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/*30* This test is derived from tcp_connect_port_test.c.31*/3233#include <sys/param.h>34#include <sys/socket.h>35#include <sys/stat.h>36#include <sys/sysctl.h>3738#include <netinet/in.h>3940#include <err.h>41#include <errno.h>42#include <fcntl.h>43#include <netdb.h>44#include <stdio.h>45#include <stdlib.h>46#include <unistd.h>4748#include <atf-c.h>4950#define SYSCTLBAKFILE "tmp.net.inet.ip.portrange.values"5152#define PORT_FIRST 10000 /* normal default */53#define PORT_LAST 1000354#define LOOPS 10 /* 5 should be enough */5556struct portrange {57int first;58int last;59};6061/*62* Set first and last ports in the ipport range. Save the old values63* of the sysctls so they can be restored later.64*/65static void66set_portrange(void)67{68int error, fd, first_new, last_new;69struct portrange save_ports;70size_t sysctlsz;7172/*73* Pre-emptively unlink our restoration file, so we will do no74* restoration on error.75*/76unlink(SYSCTLBAKFILE);7778/*79* Set the net.inet.ip.portrange.{first,last} sysctls. Save the80* old values so we can restore them.81*/82first_new = PORT_FIRST;83sysctlsz = sizeof(save_ports.first);84error = sysctlbyname("net.inet.ip.portrange.first", &save_ports.first,85&sysctlsz, &first_new, sizeof(first_new));86if (error) {87warn("sysctlbyname(\"net.inet.ip.portrange.first\") "88"failed");89atf_tc_skip("Unable to set sysctl");90}91if (sysctlsz != sizeof(save_ports.first)) {92fprintf(stderr, "Error: unexpected sysctl value size "93"(expected %zu, actual %zu)\n", sizeof(save_ports.first),94sysctlsz);95goto restore_sysctl;96}9798last_new = PORT_LAST;99sysctlsz = sizeof(save_ports.last);100error = sysctlbyname("net.inet.ip.portrange.last", &save_ports.last,101&sysctlsz, &last_new, sizeof(last_new));102if (error) {103warn("sysctlbyname(\"net.inet.ip.portrange.last\") "104"failed");105atf_tc_skip("Unable to set sysctl");106}107if (sysctlsz != sizeof(save_ports.last)) {108fprintf(stderr, "Error: unexpected sysctl value size "109"(expected %zu, actual %zu)\n", sizeof(save_ports.last),110sysctlsz);111goto restore_sysctl;112}113114/* Open the backup file, write the contents, and close it. */115fd = open(SYSCTLBAKFILE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL,116S_IRUSR|S_IWUSR);117if (fd < 0) {118warn("error opening sysctl backup file");119goto restore_sysctl;120}121error = write(fd, &save_ports, sizeof(save_ports));122if (error < 0) {123warn("error writing saved value to sysctl backup file");124goto cleanup_and_restore;125}126if (error != (int)sizeof(save_ports)) {127fprintf(stderr,128"Error writing saved value to sysctl backup file: "129"(expected %zu, actual %d)\n", sizeof(save_ports), error);130goto cleanup_and_restore;131}132error = close(fd);133if (error) {134warn("error closing sysctl backup file");135cleanup_and_restore:136(void)close(fd);137(void)unlink(SYSCTLBAKFILE);138restore_sysctl:139sysctlsz = sizeof(save_ports.first);140(void)sysctlbyname("net.inet.ip.portrange.first", NULL,141NULL, &save_ports.first, sysctlsz);142sysctlsz = sizeof(save_ports.last);143(void)sysctlbyname("net.inet.ip.portrange.last", NULL,144NULL, &save_ports.last, sysctlsz);145atf_tc_skip("Error setting sysctl");146}147}148149/*150* Restore the sysctl values from the backup file and delete the backup file.151*/152static void153restore_portrange(void)154{155int error, fd;156struct portrange save_ports;157158/* Open the backup file, read the contents, close it, and delete it. */159fd = open(SYSCTLBAKFILE, O_RDONLY);160if (fd < 0) {161warn("error opening sysctl backup file");162return;163}164error = read(fd, &save_ports, sizeof(save_ports));165if (error < 0) {166warn("error reading saved values from sysctl backup file");167return;168}169if (error != (int)sizeof(save_ports)) {170fprintf(stderr,171"Error reading saved values from sysctl backup file: "172"(expected %zu, actual %d)\n", sizeof(save_ports), error);173return;174}175error = close(fd);176if (error)177warn("error closing sysctl backup file");178error = unlink(SYSCTLBAKFILE);179if (error)180warn("error removing sysctl backup file");181182/* Restore the saved sysctl values. */183error = sysctlbyname("net.inet.ip.portrange.first", NULL, NULL,184&save_ports.first, sizeof(save_ports.first));185if (error)186warn("sysctlbyname(\"net.inet.ip.portrange.first\") "187"failed while restoring value");188error = sysctlbyname("net.inet.ip.portrange.last", NULL, NULL,189&save_ports.last, sizeof(save_ports.last));190if (error)191warn("sysctlbyname(\"net.inet.ip.portrange.last\") "192"failed while restoring value");193}194195ATF_TC_WITH_CLEANUP(tcp_v4mapped_bind);196ATF_TC_HEAD(tcp_v4mapped_bind, tc)197{198/* root is only required for sysctls (setup and cleanup). */199atf_tc_set_md_var(tc, "require.user", "root");200atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");201atf_tc_set_md_var(tc, "descr",202"Check local port assignment with bind and mapped V4 addresses");203}204/*205* Create a listening IPv4 socket, then connect to it repeatedly using a206* bound IPv6 socket using a v4 mapped address. With a small port range,207* this should fail on a bind() call with EADDRNOTAVAIL. However, in208* previous systems, the bind() would succeed, binding a duplicate port,209* and then the connect would fail with EADDRINUSE. Make sure we get210* the right error.211*/212ATF_TC_BODY(tcp_v4mapped_bind, tc)213{214union {215struct sockaddr saddr;216struct sockaddr_in saddr4;217struct sockaddr_in6 saddr6;218} su_clnt, su_srvr, su_mapped;219struct addrinfo ai_hint, *aip;220socklen_t salen;221int csock, error, i, lsock, off = 0;222bool got_bind_error = false;223224/*225* Set the net.inet.ip.portrange.{first,last} sysctls to use a small226* range, allowing us to generate port exhaustion quickly.227*/228set_portrange();229230/* Setup the listen socket. */231lsock = socket(PF_INET, SOCK_STREAM, 0);232ATF_REQUIRE_MSG(lsock >= 0, "socket() for listen socket failed: %s",233strerror(errno));234235memset(&su_srvr.saddr4, 0, sizeof(su_srvr.saddr4));236su_srvr.saddr4.sin_family = AF_INET;237error = bind(lsock, &su_srvr.saddr, sizeof(su_srvr.saddr4));238ATF_REQUIRE_MSG(error == 0, "bind() failed: %s", strerror(errno));239error = listen(lsock, LOOPS + 1);240ATF_REQUIRE_MSG(error == 0, "listen() failed: %s", strerror(errno));241242/* Get the address of the listen socket. */243salen = sizeof(su_srvr);244error = getsockname(lsock, &su_srvr.saddr, &salen);245ATF_REQUIRE_MSG(error == 0,246"getsockname() for listen socket failed: %s",247strerror(errno));248ATF_REQUIRE_MSG(salen == sizeof(struct sockaddr_in),249"unexpected sockaddr size");250ATF_REQUIRE_MSG(su_srvr.saddr.sa_len == sizeof(struct sockaddr_in),251"unexpected sa_len size");252253/* Set up destination address for client sockets. */254memset(&ai_hint, 0, sizeof(ai_hint));255ai_hint.ai_family = AF_INET6;256ai_hint.ai_flags = AI_NUMERICHOST | AI_V4MAPPED;257error = getaddrinfo("127.0.0.1", NULL, &ai_hint, &aip);258ATF_REQUIRE_MSG(error == 0, "getaddrinfo: %s", gai_strerror(error));259memcpy(&su_mapped.saddr6, aip->ai_addr, sizeof(su_mapped.saddr6));260su_mapped.saddr6.sin6_port = su_srvr.saddr4.sin_port;261freeaddrinfo(aip);262263/* Set up address to bind for client sockets (unspecified). */264memset(&su_clnt.saddr6, 0, sizeof(su_clnt.saddr6));265su_clnt.saddr6.sin6_family = AF_INET6;266267/* Open connections in a loop. */268for (i = 0; i < LOOPS; i++) {269csock = socket(PF_INET6, SOCK_STREAM, 0);270ATF_REQUIRE_MSG(csock >= 0,271"socket() for client socket %d failed: %s",272i, strerror(errno));273error = setsockopt(csock, IPPROTO_IPV6, IPV6_V6ONLY, &off,274sizeof(off));275ATF_REQUIRE_MSG(error == 0,276"setsockopt(IPV6_ONLY = 0) failed: %s", strerror(errno));277278/*279* A bind would not be necessary for operation, but280* provokes the error.281*/282error = bind(csock, &su_clnt.saddr, sizeof(su_clnt.saddr6));283if (error != 0) {284if (errno == EADDRNOTAVAIL) { /* Success, expected */285got_bind_error = true;286break;287}288ATF_REQUIRE_MSG(error == 0,289"client bind %d failed: %s", i, strerror(errno));290}291292error = connect(csock, &su_mapped.saddr, su_mapped.saddr.sa_len);293if (error != 0 && errno == EADDRINUSE) {294/* This is the specific error we were looking for. */295atf_tc_fail("client connect %d failed, "296" client had duplicate port: %s",297i, strerror(errno));298}299ATF_REQUIRE_MSG(error == 0,300"connect() for client socket %d failed: %s",301i, strerror(errno));302303/*304* We don't accept the new socket from the server socket305* or close the client socket, as we want the ports to306* remain busy. The range is small enough that this is307* not a problem.308*/309}310ATF_REQUIRE_MSG(i >= 1, "No successful connections");311ATF_REQUIRE_MSG(got_bind_error == true, "No expected bind error");312313ATF_REQUIRE(close(lsock) == 0);314}315ATF_TC_CLEANUP(tcp_v4mapped_bind, tc)316{317restore_portrange();318}319320ATF_TC(udp_v4mapped_sendto);321ATF_TC_HEAD(udp_v4mapped_sendto, tc)322{323atf_tc_set_md_var(tc, "descr",324"Validate sendto() with a v4-mapped address and a v6-only socket");325}326ATF_TC_BODY(udp_v4mapped_sendto, tc)327{328struct addrinfo ai_hint, *aip;329struct sockaddr_in sin;330struct sockaddr_in6 sin6;331ssize_t n;332socklen_t salen;333int error, ls, s, zero;334short port;335char ch;336337ls = socket(PF_INET, SOCK_DGRAM, 0);338ATF_REQUIRE(ls >= 0);339340memset(&ai_hint, 0, sizeof(ai_hint));341ai_hint.ai_family = AF_INET;342ai_hint.ai_flags = AI_NUMERICHOST;343error = getaddrinfo("127.0.0.1", NULL, &ai_hint, &aip);344ATF_REQUIRE_MSG(error == 0, "getaddrinfo: %s", gai_strerror(error));345memcpy(&sin, aip->ai_addr, sizeof(sin));346347error = bind(ls, (struct sockaddr *)&sin, sizeof(sin));348ATF_REQUIRE_MSG(error == 0, "bind: %s", strerror(errno));349salen = sizeof(sin);350error = getsockname(ls, (struct sockaddr *)&sin, &salen);351ATF_REQUIRE_MSG(error == 0,352"getsockname() for listen socket failed: %s", strerror(errno));353ATF_REQUIRE_MSG(salen == sizeof(struct sockaddr_in),354"unexpected sockaddr size");355port = sin.sin_port;356357s = socket(PF_INET6, SOCK_DGRAM, 0);358ATF_REQUIRE(s >= 0);359360memset(&ai_hint, 0, sizeof(ai_hint));361ai_hint.ai_family = AF_INET6;362ai_hint.ai_flags = AI_NUMERICHOST | AI_V4MAPPED;363error = getaddrinfo("127.0.0.1", NULL, &ai_hint, &aip);364ATF_REQUIRE_MSG(error == 0, "getaddrinfo: %s", gai_strerror(error));365memcpy(&sin6, aip->ai_addr, sizeof(sin6));366sin6.sin6_port = port;367freeaddrinfo(aip);368369ch = 0x42;370n = sendto(s, &ch, 1, 0, (struct sockaddr *)&sin6, sizeof(sin6));371ATF_REQUIRE_ERRNO(EINVAL, n == -1);372373zero = 0;374error = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero));375ATF_REQUIRE_MSG(error == 0,376"setsockopt(IPV6_V6ONLY) failed: %s", strerror(errno));377378ch = 0x42;379n = sendto(s, &ch, 1, 0, (struct sockaddr *)&sin6, sizeof(sin6));380ATF_REQUIRE_MSG(n == 1, "sendto() failed: %s", strerror(errno));381382ch = 0;383n = recv(ls, &ch, 1, 0);384ATF_REQUIRE_MSG(n == 1, "recv() failed: %s", strerror(errno));385ATF_REQUIRE(ch == 0x42);386387ATF_REQUIRE(close(s) == 0);388ATF_REQUIRE(close(ls) == 0);389}390391ATF_TP_ADD_TCS(tp)392{393ATF_TP_ADD_TC(tp, tcp_v4mapped_bind);394ATF_TP_ADD_TC(tp, udp_v4mapped_sendto);395396return (atf_no_error());397}398399400