Path: blob/main/tests/sys/netinet/libalias/util.h
102428 views
/*1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright 2021 Lutz Donnerhacke4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*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 above12* copyright notice, this list of conditions and the following13* disclaimer in the documentation and/or other materials provided14* with the distribution.15* 3. Neither the name of the copyright holder nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND20* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,21* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF22* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS24* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED26* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON28* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR29* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF30* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/33#include <sys/types.h>3435#include <arpa/inet.h>36#include <netinet/ip.h>37#include <netinet/udp.h>3839#ifndef _UTIL_H40#define _UTIL_H4142/* common ip ranges */43extern struct in_addr masq, pub, pub2, prv1, prv2, prv3, cgn, ext, ANY_ADDR;4445int randcmp(const void *a, const void *b);46void hexdump(void *p, size_t len);47struct ip * ip_packet(u_char protocol, size_t len);48struct udphdr * set_udp(struct ip *p, u_short sport, u_short dport);4950static inline int51addr_eq(struct in_addr a, struct in_addr b)52{53return a.s_addr == b.s_addr;54}5556#define a2h(a) ntohl(a.s_addr)5758static inline int59rand_range(int min, int max)60{61return min + rand()%(max - min);62}6364#define NAT_CHECK(pip, src, dst, msq) do { \65int res; \66int len = ntohs(pip->ip_len); \67pip->ip_src = src; \68pip->ip_dst = dst; \69res = LibAliasOut(la, pip, len); \70ATF_CHECK_MSG(res == PKT_ALIAS_OK, \71">%d< not met PKT_ALIAS_OK", res); \72ATF_CHECK(addr_eq(msq, pip->ip_src)); \73ATF_CHECK(addr_eq(dst, pip->ip_dst)); \74} while(0)7576#define NAT_FAIL(pip, src, dst) do { \77int res; \78int len = ntohs(pip->ip_len); \79pip->ip_src = src; \80pip->ip_dst = dst; \81res = LibAliasOut(la, pip, len); \82ATF_CHECK_MSG(res != PKT_ALIAS_OK, \83">%d< not met !PKT_ALIAS_OK", res); \84ATF_CHECK(addr_eq(src, pip->ip_src)); \85ATF_CHECK(addr_eq(dst, pip->ip_dst)); \86} while(0)8788#define UNNAT_CHECK(pip, src, dst, rel) do { \89int res; \90int len = ntohs(pip->ip_len); \91pip->ip_src = src; \92pip->ip_dst = dst; \93res = LibAliasIn(la, pip, len); \94ATF_CHECK_MSG(res == PKT_ALIAS_OK, \95">%d< not met PKT_ALIAS_OK", res); \96ATF_CHECK(addr_eq(src, pip->ip_src)); \97ATF_CHECK(addr_eq(rel, pip->ip_dst)); \98} while(0)99100#define UNNAT_FAIL(pip, src, dst) do { \101int res; \102int len = ntohs(pip->ip_len); \103pip->ip_src = src; \104pip->ip_dst = dst; \105res = LibAliasIn(la, pip, len); \106ATF_CHECK_MSG(res != PKT_ALIAS_OK, \107">%d< not met !PKT_ALIAS_OK", res); \108ATF_CHECK(addr_eq(src, pip->ip_src)); \109ATF_CHECK(addr_eq(dst, pip->ip_dst)); \110} while(0)111112#define UDP_NAT_CHECK(p, u, si, sp, di, dp, mi) do { \113u = set_udp(p, (sp), (dp)); \114NAT_CHECK(p, (si), (di), (mi)); \115ATF_CHECK(u->uh_dport == htons(dp)); \116} while(0)117118#define UDP_NAT_FAIL(p, u, si, sp, di, dp) do { \119u = set_udp(p, (sp), (dp)); \120NAT_FAIL(p, (si), (di)); \121} while(0)122123#define UDP_UNNAT_CHECK(p, u, si, sp, mi, mp, di, dp) \124do { \125u = set_udp(p, (sp), (mp)); \126UNNAT_CHECK(p, (si), (mi), (di)); \127ATF_CHECK(u->uh_sport == htons(sp)); \128ATF_CHECK(u->uh_dport == htons(dp)); \129} while(0)130131#define UDP_UNNAT_FAIL(p, u, si, sp, mi, mp) do { \132u = set_udp(p, (sp), (mp)); \133UNNAT_FAIL(p, (si), (mi)); \134} while(0)135136#endif /* _UTIL_H */137138139