1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/* $OpenBSD: inet_nat64.c,v 1.1 2011/10/13 18:23:40 claudio Exp $ */
/* $vantronix: inet_nat64.c,v 1.2 2011/02/28 14:57:58 mike Exp $ */
/*
* Copyright (c) 2011 Reyk Floeter <reyk@vantronix.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/socket.h>
#ifdef _KERNEL
#include <sys/mbuf.h>
#include <netinet/in.h>
#else
#include <netinet/in.h>
#include <errno.h>
#endif
union inet_nat64_addr {
u_int32_t u32[4];
u_int8_t u8[16];
};
u_int32_t inet_nat64_mask(u_int32_t, u_int32_t, u_int8_t);
int inet_nat64(int, const void *, void *, const void *, u_int8_t);
int inet_nat64_inet(const void *, void *, const void *, u_int8_t);
int inet_nat64_inet6(const void *, void *, const void *, u_int8_t);
int inet_nat46(int, const void *, void *, const void *, u_int8_t);
int inet_nat46_inet(const void *, void *, const void *, u_int8_t);
int inet_nat46_inet6(const void *, void *, const void *, u_int8_t);
u_int32_t
inet_nat64_mask(u_int32_t src, u_int32_t pfx, u_int8_t pfxlen)
{
u_int32_t u32;
if (pfxlen == 0)
return (src);
else if (pfxlen > 32)
pfxlen = 32;
u32 =
(src & ~htonl(0xffffffff << (32 - pfxlen))) |
(pfx & htonl(0xffffffff << (32 - pfxlen)));
return (u32);
}
int
inet_nat64(int af, const void *src, void *dst,
const void *pfx, u_int8_t pfxlen)
{
switch (af) {
case AF_INET:
return (inet_nat64_inet(src, dst, pfx, pfxlen));
case AF_INET6:
return (inet_nat64_inet6(src, dst, pfx, pfxlen));
default:
#ifndef _KERNEL
errno = EAFNOSUPPORT;
#endif
return (-1);
}
/* NOTREACHED */
}
int
inet_nat64_inet(const void *src, void *dst, const void *pfx, u_int8_t pfxlen)
{
const union inet_nat64_addr *s = src;
const union inet_nat64_addr *p = pfx;
union inet_nat64_addr *d = dst;
int i, j;
switch (pfxlen) {
case 32:
case 40:
case 48:
case 56:
case 64:
case 96:
i = pfxlen / 8;
break;
default:
if (pfxlen < 96 || pfxlen > 128) {
#ifndef _KERNEL
errno = EINVAL;
#endif
return (-1);
}
/* as an extension, mask out any other bits */
d->u32[0] = inet_nat64_mask(s->u32[3], p->u32[3],
(u_int8_t)(32 - (128 - pfxlen)));
return (0);
}
/* fill the octets with the source and skip reserved octet 8 */
for (j = 0; j < 4; j++) {
if (i == 8)
i++;
d->u8[j] = s->u8[i++];
}
return (0);
}
int
inet_nat64_inet6(const void *src, void *dst, const void *pfx, u_int8_t pfxlen)
{
const union inet_nat64_addr *s = src;
const union inet_nat64_addr *p = pfx;
union inet_nat64_addr *d = dst;
int i, j;
/* first copy the prefix octets to the destination */
*d = *p;
switch (pfxlen) {
case 32:
case 40:
case 48:
case 56:
case 64:
case 96:
i = pfxlen / 8;
break;
default:
if (pfxlen < 96 || pfxlen > 128) {
#ifndef _KERNEL
errno = EINVAL;
#endif
return (-1);
}
/* as an extension, mask out any other bits */
d->u32[3] = inet_nat64_mask(s->u32[0], p->u32[3],
(u_int8_t)(32 - (128 - pfxlen)));
return (0);
}
/* octet 8 is reserved and must be set to zero */
d->u8[8] = 0;
/* fill the other octets with the source and skip octet 8 */
for (j = 0; j < 4; j++) {
if (i == 8)
i++;
d->u8[i++] = s->u8[j];
}
return (0);
}
int
inet_nat46(int af, const void *src, void *dst,
const void *pfx, u_int8_t pfxlen)
{
if (pfxlen > 32) {
#ifndef _KERNEL
errno = EINVAL;
#endif
return (-1);
}
switch (af) {
case AF_INET:
return (inet_nat46_inet(src, dst, pfx, pfxlen));
case AF_INET6:
return (inet_nat46_inet6(src, dst, pfx, pfxlen));
default:
#ifndef _KERNEL
errno = EAFNOSUPPORT;
#endif
return (-1);
}
/* NOTREACHED */
}
int
inet_nat46_inet(const void *src, void *dst, const void *pfx, u_int8_t pfxlen)
{
const union inet_nat64_addr *s = src;
const union inet_nat64_addr *p = pfx;
union inet_nat64_addr *d = dst;
/* set the remaining bits to the source */
d->u32[0] = inet_nat64_mask(s->u32[3], p->u32[0], pfxlen);
return (0);
}
int
inet_nat46_inet6(const void *src, void *dst, const void *pfx, u_int8_t pfxlen)
{
const union inet_nat64_addr *s = src;
const union inet_nat64_addr *p = pfx;
union inet_nat64_addr *d = dst;
/* set the initial octets to zero */
d->u32[0] = d->u32[1] = d->u32[2] = 0;
/* now set the remaining bits to the source */
d->u32[3] = inet_nat64_mask(s->u32[0], p->u32[0], pfxlen);
return (0);
}
|