-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathng_sbinat.c
420 lines (357 loc) · 11.2 KB
/
ng_sbinat.c
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*-
* Copyright 2005, Gleb Smirnoff <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/sys/netgraph/ng_sbinat.c,v 1.10.2.2.2.1 2008/11/25 02:59:29 kensmith Exp $
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/limits.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/ctype.h>
#include <sys/errno.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <net/if_var.h>
#include <net/if_vlan_var.h>
#include <net/bpf.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <machine/in_cksum.h>
#include <netinet/libalias/alias.h>
#include <netgraph/ng_message.h>
#include <netgraph/ng_parse.h>
#include "ng_sbinat.h"
#include <netgraph/netgraph.h>
static ng_constructor_t ng_sbinat_constructor;
static ng_rcvmsg_t ng_sbinat_rcvmsg;
static ng_shutdown_t ng_sbinat_shutdown;
static ng_newhook_t ng_sbinat_newhook;
static ng_rcvdata_t ng_sbinat_rcvdata;
static ng_disconnect_t ng_sbinat_disconnect;
//static unsigned int ng_sbinat_translate_flags(unsigned int x);
/* List of commands and how to convert arguments to/from ASCII. */
static const struct ng_cmdlist ng_sbinat_cmdlist[] = {
{
NGM_SBINAT_COOKIE,
NGM_SBINAT_SET_IN_ADDR,
"setinaddr",
&ng_parse_ipaddr_type,
NULL
},
{
NGM_SBINAT_COOKIE,
NGM_SBINAT_SET_OUT_ADDR,
"setoutaddr",
&ng_parse_ipaddr_type,
NULL
},
{ 0 }
};
/* Netgraph node type descriptor. */
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_SBINAT_NODE_TYPE,
.constructor = ng_sbinat_constructor,
.rcvmsg = ng_sbinat_rcvmsg,
.shutdown = ng_sbinat_shutdown,
.newhook = ng_sbinat_newhook,
.rcvdata = ng_sbinat_rcvdata,
.disconnect = ng_sbinat_disconnect,
.cmdlist = ng_sbinat_cmdlist,
};
NETGRAPH_INIT(sbinat, &typestruct);
//MODULE_DEPEND(ng_sbinat, libalias, 1, 1, 1);
/* Information we store for each node. */
struct ng_sbinat_priv {
node_p node; /* back pointer to node */
hook_p in; /* hook for demasquerading */
hook_p out; /* hook for masquerading */
u_int32_t flags;
struct in_addr in_addr;
struct in_addr out_addr;
};
typedef struct ng_sbinat_priv *priv_p;
/* Values of flags */
#define NGSBINAT_CONNECTED 0x1 /* We have both hooks connected */
#define NGSBINAT_ADDR_DEFINED 0x2 /* NGM_SBINAT_SET_IPADDR happened */
static int
ng_sbinat_constructor(node_p node)
{
priv_p priv;
/* Initialize private descriptor. */
MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH,
M_NOWAIT | M_ZERO);
if (priv == NULL)
return (ENOMEM);
/* Link structs together. */
NG_NODE_SET_PRIVATE(node, priv);
priv->node = node;
/*
* libalias is not thread safe, so our node
* must be single threaded.
*/
//NG_NODE_FORCE_WRITER(node);
return (0);
}
static int
ng_sbinat_newhook(node_p node, hook_p hook, const char *name)
{
const priv_p priv = NG_NODE_PRIVATE(node);
if (strcmp(name, NG_SBINAT_HOOK_IN) == 0) {
priv->in = hook;
} else if (strcmp(name, NG_SBINAT_HOOK_OUT) == 0) {
priv->out = hook;
} else
return (EINVAL);
if (priv->out != NULL &&
priv->in != NULL)
priv->flags |= NGSBINAT_CONNECTED;
return(0);
}
static int
ng_sbinat_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
const priv_p priv = NG_NODE_PRIVATE(node);
struct ng_mesg *resp = NULL;
struct ng_mesg *msg;
int error = 0;
NGI_GET_MSG(item, msg);
switch (msg->header.typecookie) {
case NGM_SBINAT_COOKIE:
switch (msg->header.cmd) {
case NGM_SBINAT_SET_IN_ADDR:
{
struct in_addr *const ia = (struct in_addr *)msg->data;
if (msg->header.arglen < sizeof(*ia)) {
error = EINVAL;
break;
}
priv->in_addr = *ia;
}
break;
case NGM_SBINAT_SET_OUT_ADDR:
{
struct in_addr *const ia = (struct in_addr *)msg->data;
if (msg->header.arglen < sizeof(*ia)) {
error = EINVAL;
break;
}
priv->out_addr = *ia;
}
break;
default:
error = EINVAL; /* unknown command */
break;
}
break;
default:
error = EINVAL; /* unknown cookie type */
break;
}
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
#define TCPMSS_ADJUST_CHECKSUM(acc0, cksum) do { \
int acc = acc0 + cksum; \
if (acc < 0) { \
acc = -acc; \
acc = (acc >> 16) + (acc & 0xffff); \
acc += acc >> 16; \
cksum = (u_short) ~acc; \
} else { \
acc = (acc >> 16) + (acc & 0xffff); \
acc += acc >> 16; \
cksum = (u_short) acc; \
} \
} while (0);
void recalc_cksum(struct ip *ip, struct mbuf *m, int accumulate){
TCPMSS_ADJUST_CHECKSUM(accumulate, ip->ip_sum);
switch (ip->ip_p) {
case IPPROTO_TCP:
if( (m->m_pkthdr.csum_flags & CSUM_TCP) == 0 ){
struct tcphdr *th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
TCPMSS_ADJUST_CHECKSUM(accumulate, th->th_sum);
}
break;
case IPPROTO_UDP:
if( (m->m_pkthdr.csum_flags & CSUM_UDP) == 0 ){
struct udphdr *uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
TCPMSS_ADJUST_CHECKSUM(accumulate, uh->uh_sum);
}
break;
}
}
static int
ng_sbinat_rcvdata(hook_p hook, item_p item )
{
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
struct mbuf *m;
struct ip *ip;
int error = 0;
char *c;
int pullup_len = 0;
int accumulate;
/* We have no required hooks. */
if (!(priv->flags & NGSBINAT_CONNECTED)) {
NG_FREE_ITEM(item);
log(LOG_INFO,"sbinat: recvd pkt w/o all connected hooks. dropping.\n");
return (ENXIO);
}
/* We have no alias address yet to do anything. */
//if (!(priv->flags & NGSBINAT_ADDR_DEFINED))
// goto send;
m = NGI_M(item);
if ((m = m_megapullup(m, m->m_pkthdr.len)) == NULL) {
NGI_M(item) = NULL; /* avoid double free */
NG_FREE_ITEM(item);
log(LOG_INFO,"sbinat: returning ENOBUFS at line %d\n", __LINE__);
return (ENOBUFS);
}
NGI_M(item) = m;
struct ether_header *eh;
//uint16_t etype;
#define M_CHECK(length) do { \
pullup_len += length; \
if ((m)->m_pkthdr.len < (pullup_len)) { \
NG_FREE_ITEM(item); \
log(LOG_INFO,"sbinat: returning EINVAL at line %d\n", __LINE__); \
return (EINVAL); \
} \
if ((m)->m_len < (pullup_len) && \
(((m) = m_pullup((m),(pullup_len))) == NULL)) { \
NG_FREE_ITEM(item); \
log(LOG_INFO,"sbinat: returning ENOBUFS at line %d\n", __LINE__); \
return (ENOBUFS); \
} \
} while (0)
M_CHECK(sizeof(struct ether_header));
eh = mtod(m, struct ether_header *);
/* Make sure this is IP frame. */
switch( ntohs(eh->ether_type) ){
case ETHERTYPE_IP:
M_CHECK(sizeof(struct ip));
ip = (struct ip *)(eh + 1);
c = (char *)(eh + 1);
break;
default:
// all other protos, pass them unchanged
goto send;
break;
}
if ((ip->ip_off & htons(IP_OFFMASK)) == 0) {
/*
* In case of IP header with options, we haven't pulled
* up enough, yet.
*/
pullup_len += (ip->ip_hl << 2) - sizeof(struct ip);
switch (ip->ip_p) {
case IPPROTO_TCP:
M_CHECK(sizeof(struct tcphdr));
break;
case IPPROTO_UDP:
M_CHECK(sizeof(struct udphdr));
break;
}
}
if (hook == priv->in) {
if(*(u_int16_t*)(&ip->ip_src) == *(u_int16_t*)(&priv->in_addr)){
accumulate = *(u_int16_t*)(&ip->ip_src);
*(u_int16_t*)(&ip->ip_src) = *(u_int16_t*)(&priv->out_addr);
accumulate -= *(u_int16_t*)(&ip->ip_src);
recalc_cksum(ip,m,accumulate);
}
} else if (hook == priv->out) {
if(*(u_int16_t*)(&ip->ip_dst) == *(u_int16_t*)(&priv->out_addr)){
accumulate = *(u_int16_t*)(&ip->ip_dst);
*(u_int16_t*)(&ip->ip_dst) = *(u_int16_t*)(&priv->in_addr);
accumulate -= *(u_int16_t*)(&ip->ip_dst);
recalc_cksum(ip,m,accumulate);
}
} else {
log(LOG_ERR,"sbinat: unknown hook at line %d\n", __LINE__);
panic("ng_sbinat: unknown hook!\n");
}
m->m_pkthdr.len = m->m_len = ntohs(ip->ip_len) + sizeof(struct ether_header);
send:
if (hook == priv->in)
NG_FWD_ITEM_HOOK(error, item, priv->out);
else
NG_FWD_ITEM_HOOK(error, item, priv->in);
return (error);
}
static int
ng_sbinat_shutdown(node_p node)
{
const priv_p priv = NG_NODE_PRIVATE(node);
NG_NODE_SET_PRIVATE(node, NULL);
NG_NODE_UNREF(node);
FREE(priv, M_NETGRAPH);
return (0);
}
static int
ng_sbinat_disconnect(hook_p hook)
{
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
priv->flags &= ~NGSBINAT_CONNECTED;
if (hook == priv->out)
priv->out = NULL;
if (hook == priv->in)
priv->in = NULL;
if (priv->out == NULL && priv->in == NULL)
ng_rmnode_self(NG_HOOK_NODE(hook));
return (0);
}
/*
static unsigned int
ng_sbinat_translate_flags(unsigned int x)
{
unsigned int res = 0;
if (x & NG_SBINAT_LOG)
res |= PKT_ALIAS_LOG;
if (x & NG_SBINAT_DENY_INCOMING)
res |= PKT_ALIAS_DENY_INCOMING;
if (x & NG_SBINAT_SAME_PORTS)
res |= PKT_ALIAS_SAME_PORTS;
if (x & NG_SBINAT_UNREGISTERED_ONLY)
res |= PKT_ALIAS_UNREGISTERED_ONLY;
if (x & NG_SBINAT_RESET_ON_ADDR_CHANGE)
res |= PKT_ALIAS_RESET_ON_ADDR_CHANGE;
if (x & NG_SBINAT_PROXY_ONLY)
res |= PKT_ALIAS_PROXY_ONLY;
if (x & NG_SBINAT_REVERSE)
res |= PKT_ALIAS_REVERSE;
return (res);
}
*/