Path: blob/main/tests/sys/net/routing/rtsock_config.h
39604 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2019 Alexander V. Chernikov4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#ifndef _NET_ROUTING_RTSOCK_CONFIG_H_28#define _NET_ROUTING_RTSOCK_CONFIG_H_2930#include "params.h"3132struct rtsock_config_options {33int num_interfaces; /* number of interfaces to create */34};3536struct rtsock_test_config {37int ifindex;38char net4_str[INET_ADDRSTRLEN];39char addr4_str[INET_ADDRSTRLEN];40char net6_str[INET6_ADDRSTRLEN];41char addr6_str[INET6_ADDRSTRLEN];42struct sockaddr_in net4;43struct sockaddr_in mask4;44struct sockaddr_in addr4;45struct sockaddr_in6 net6;46struct sockaddr_in6 mask6;47struct sockaddr_in6 addr6;48int plen4;49int plen6;50char *remote_lladdr;51char *ifname;52char **ifnames;53bool autocreated_interface;54int rtsock_fd;55int num_interfaces;56};5758struct rtsock_test_config *59config_setup(const atf_tc_t *tc, struct rtsock_config_options *co)60{61struct rtsock_config_options default_co;62struct rtsock_test_config *c;63char buf[64], *s;64const char *key;65int mask;6667if (co == NULL) {68bzero(&default_co, sizeof(default_co));69co = &default_co;70co->num_interfaces = 1;71}7273c = calloc(1, sizeof(struct rtsock_test_config));74c->rtsock_fd = -1;7576key = atf_tc_get_config_var_wd(tc, "rtsock.v4prefix", "192.0.2.0/24");77strlcpy(buf, key, sizeof(buf));78if ((s = strchr(buf, '/')) == NULL)79return (NULL);80*s++ = '\0';81mask = strtol(s, NULL, 10);82if (mask < 0 || mask > 32)83return (NULL);84c->plen4 = mask;85inet_pton(AF_INET, buf, &c->net4.sin_addr);8687c->net4.sin_len = sizeof(struct sockaddr_in);88c->net4.sin_family = AF_INET;89c->addr4.sin_len = sizeof(struct sockaddr_in);90c->addr4.sin_family = AF_INET;9192sa_fill_mask4(&c->mask4, c->plen4);9394/* Fill in interface IPv4 address. Assume the first address in net */95c->addr4.sin_addr.s_addr = htonl(ntohl(c->net4.sin_addr.s_addr) + 1);96inet_ntop(AF_INET, &c->net4.sin_addr, c->net4_str, INET_ADDRSTRLEN);97inet_ntop(AF_INET, &c->addr4.sin_addr, c->addr4_str, INET_ADDRSTRLEN);9899key = atf_tc_get_config_var_wd(tc, "rtsock.v6prefix", "2001:DB8::/32");100strlcpy(buf, key, sizeof(buf));101if ((s = strchr(buf, '/')) == NULL)102return (NULL);103*s++ = '\0';104mask = strtol(s, NULL, 10);105if (mask < 0 || mask > 128)106return (NULL);107c->plen6 = mask;108109inet_pton(AF_INET6, buf, &c->net6.sin6_addr);110111c->net6.sin6_len = sizeof(struct sockaddr_in6);112c->net6.sin6_family = AF_INET6;113c->addr6.sin6_len = sizeof(struct sockaddr_in6);114c->addr6.sin6_family = AF_INET6;115116sa_fill_mask6(&c->mask6, c->plen6);117118/* Fill in interface IPv6 address. Assume the first address in net */119memcpy(&c->addr6.sin6_addr, &c->net6.sin6_addr, sizeof(struct in6_addr));120#define _s6_addr32 __u6_addr.__u6_addr32121c->addr6.sin6_addr._s6_addr32[3] = htonl(ntohl(c->net6.sin6_addr._s6_addr32[3]) + 1);122#undef _s6_addr32123inet_ntop(AF_INET6, &c->net6.sin6_addr, c->net6_str, INET6_ADDRSTRLEN);124inet_ntop(AF_INET6, &c->addr6.sin6_addr, c->addr6_str, INET6_ADDRSTRLEN);125126ATF_CHECK_ERRNO(0, true);127128if (co->num_interfaces > 0) {129/* Try loading if_epair and if that fails skip the test. */130kldload("if_epair");131ATF_REQUIRE_KERNEL_MODULE("if_epair");132/* Clear errno for the following tests. */133errno = 0;134135c->ifnames = calloc(co->num_interfaces, sizeof(char *));136for (int i = 0; i < co->num_interfaces; i++)137c->ifnames[i] = iface_create("epair");138139c->ifname = c->ifnames[0];140c->ifindex = if_nametoindex(c->ifname);141ATF_REQUIRE_MSG(c->ifindex != 0, "interface %s not found",142c->ifname);143}144c->num_interfaces = co->num_interfaces;145146c->remote_lladdr = strdup(atf_tc_get_config_var_wd(tc,147"rtsock.remote_lladdr", "00:00:5E:00:53:42"));148149return (c);150}151152void153config_generic_cleanup(const atf_tc_t *tc)154{155const char *srcdir = atf_tc_get_config_var(tc, "srcdir");156char cmd[512];157int ret;158159snprintf(cmd, sizeof(cmd), "%s/generic_cleanup.sh", srcdir);160ret = system(cmd);161if (ret != 0)162RLOG("'%s' failed, error %d", cmd, ret);163}164165void166config_describe_root_test(atf_tc_t *tc, char *test_descr)167{168169atf_tc_set_md_var(tc, "descr", test_descr);170// Adding/deleting prefix requires root privileges171atf_tc_set_md_var(tc, "require.user", "root");172}173174#endif175176177