Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/netinet/libalias/util.h
39488 views
1
/*
2
* SPDX-License-Identifier: BSD-3-Clause
3
*
4
* Copyright 2021 Lutz Donnerhacke
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
8
* are met:
9
*
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
13
* copyright notice, this list of conditions and the following
14
* disclaimer in the documentation and/or other materials provided
15
* with the distribution.
16
* 3. Neither the name of the copyright holder nor the names of its
17
* contributors may be used to endorse or promote products derived
18
* from this software without specific prior written permission.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
* SUCH DAMAGE.
33
*/
34
#include <sys/types.h>
35
36
#include <arpa/inet.h>
37
#include <netinet/ip.h>
38
#include <netinet/udp.h>
39
40
#ifndef _UTIL_H
41
#define _UTIL_H
42
43
/* common ip ranges */
44
extern struct in_addr masq, pub, pub2, prv1, prv2, prv3, cgn, ext, ANY_ADDR;
45
46
int randcmp(const void *a, const void *b);
47
void hexdump(void *p, size_t len);
48
struct ip * ip_packet(u_char protocol, size_t len);
49
struct udphdr * set_udp(struct ip *p, u_short sport, u_short dport);
50
51
static inline int
52
addr_eq(struct in_addr a, struct in_addr b)
53
{
54
return a.s_addr == b.s_addr;
55
}
56
57
#define a2h(a) ntohl(a.s_addr)
58
59
static inline int
60
rand_range(int min, int max)
61
{
62
return min + rand()%(max - min);
63
}
64
65
#define NAT_CHECK(pip, src, dst, msq) do { \
66
int res; \
67
int len = ntohs(pip->ip_len); \
68
pip->ip_src = src; \
69
pip->ip_dst = dst; \
70
res = LibAliasOut(la, pip, len); \
71
ATF_CHECK_MSG(res == PKT_ALIAS_OK, \
72
">%d< not met PKT_ALIAS_OK", res); \
73
ATF_CHECK(addr_eq(msq, pip->ip_src)); \
74
ATF_CHECK(addr_eq(dst, pip->ip_dst)); \
75
} while(0)
76
77
#define NAT_FAIL(pip, src, dst) do { \
78
int res; \
79
int len = ntohs(pip->ip_len); \
80
pip->ip_src = src; \
81
pip->ip_dst = dst; \
82
res = LibAliasOut(la, pip, len); \
83
ATF_CHECK_MSG(res != PKT_ALIAS_OK, \
84
">%d< not met !PKT_ALIAS_OK", res); \
85
ATF_CHECK(addr_eq(src, pip->ip_src)); \
86
ATF_CHECK(addr_eq(dst, pip->ip_dst)); \
87
} while(0)
88
89
#define UNNAT_CHECK(pip, src, dst, rel) do { \
90
int res; \
91
int len = ntohs(pip->ip_len); \
92
pip->ip_src = src; \
93
pip->ip_dst = dst; \
94
res = LibAliasIn(la, pip, len); \
95
ATF_CHECK_MSG(res == PKT_ALIAS_OK, \
96
">%d< not met PKT_ALIAS_OK", res); \
97
ATF_CHECK(addr_eq(src, pip->ip_src)); \
98
ATF_CHECK(addr_eq(rel, pip->ip_dst)); \
99
} while(0)
100
101
#define UNNAT_FAIL(pip, src, dst) do { \
102
int res; \
103
int len = ntohs(pip->ip_len); \
104
pip->ip_src = src; \
105
pip->ip_dst = dst; \
106
res = LibAliasIn(la, pip, len); \
107
ATF_CHECK_MSG(res != PKT_ALIAS_OK, \
108
">%d< not met !PKT_ALIAS_OK", res); \
109
ATF_CHECK(addr_eq(src, pip->ip_src)); \
110
ATF_CHECK(addr_eq(dst, pip->ip_dst)); \
111
} while(0)
112
113
#define UDP_NAT_CHECK(p, u, si, sp, di, dp, mi) do { \
114
u = set_udp(p, (sp), (dp)); \
115
NAT_CHECK(p, (si), (di), (mi)); \
116
ATF_CHECK(u->uh_dport == htons(dp)); \
117
} while(0)
118
119
#define UDP_NAT_FAIL(p, u, si, sp, di, dp) do { \
120
u = set_udp(p, (sp), (dp)); \
121
NAT_FAIL(p, (si), (di)); \
122
} while(0)
123
124
#define UDP_UNNAT_CHECK(p, u, si, sp, mi, mp, di, dp) \
125
do { \
126
u = set_udp(p, (sp), (mp)); \
127
UNNAT_CHECK(p, (si), (mi), (di)); \
128
ATF_CHECK(u->uh_sport == htons(sp)); \
129
ATF_CHECK(u->uh_dport == htons(dp)); \
130
} while(0)
131
132
#define UDP_UNNAT_FAIL(p, u, si, sp, mi, mp) do { \
133
u = set_udp(p, (sp), (mp)); \
134
UNNAT_FAIL(p, (si), (mi)); \
135
} while(0)
136
137
#endif /* _UTIL_H */
138
139