Deleted Added
full compact
ip_fw_nat.c (195699) ip_fw_nat.c (195727)
1/*-
2 * Copyright (c) 2008 Paolo Pisati
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2008 Paolo Pisati
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw_nat.c 195699 2009-07-14 22:48:30Z rwatson $");
28__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw_nat.c 195727 2009-07-16 21:13:04Z rwatson $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/condvar.h>
33#include <sys/eventhandler.h>
34#include <sys/malloc.h>
35#include <sys/mbuf.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/jail.h>
39#include <sys/module.h>
40#include <sys/priv.h>
41#include <sys/proc.h>
42#include <sys/rwlock.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/sysctl.h>
46#include <sys/syslog.h>
47#include <sys/ucred.h>
48#include <sys/vimage.h>
49
50#include <netinet/libalias/alias.h>
51#include <netinet/libalias/alias_local.h>
52
53#define IPFW_INTERNAL /* Access to protected data structures in ip_fw.h. */
54
55#include <net/if.h>
56#include <netinet/in.h>
57#include <netinet/ip.h>
58#include <netinet/ip_var.h>
59#include <netinet/ip_icmp.h>
60#include <netinet/ip_fw.h>
61#include <netinet/tcp.h>
62#include <netinet/tcp_timer.h>
63#include <netinet/tcp_var.h>
64#include <netinet/tcpip.h>
65#include <netinet/udp.h>
66#include <netinet/udp_var.h>
67
68#include <machine/in_cksum.h> /* XXX for in_cksum */
69
70MALLOC_DECLARE(M_IPFW);
71
72static VNET_DEFINE(eventhandler_tag, ifaddr_event_tag);
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/condvar.h>
33#include <sys/eventhandler.h>
34#include <sys/malloc.h>
35#include <sys/mbuf.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/jail.h>
39#include <sys/module.h>
40#include <sys/priv.h>
41#include <sys/proc.h>
42#include <sys/rwlock.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/sysctl.h>
46#include <sys/syslog.h>
47#include <sys/ucred.h>
48#include <sys/vimage.h>
49
50#include <netinet/libalias/alias.h>
51#include <netinet/libalias/alias_local.h>
52
53#define IPFW_INTERNAL /* Access to protected data structures in ip_fw.h. */
54
55#include <net/if.h>
56#include <netinet/in.h>
57#include <netinet/ip.h>
58#include <netinet/ip_var.h>
59#include <netinet/ip_icmp.h>
60#include <netinet/ip_fw.h>
61#include <netinet/tcp.h>
62#include <netinet/tcp_timer.h>
63#include <netinet/tcp_var.h>
64#include <netinet/tcpip.h>
65#include <netinet/udp.h>
66#include <netinet/udp_var.h>
67
68#include <machine/in_cksum.h> /* XXX for in_cksum */
69
70MALLOC_DECLARE(M_IPFW);
71
72static VNET_DEFINE(eventhandler_tag, ifaddr_event_tag);
73#define V_ifaddr_event_tag VNET_GET(ifaddr_event_tag)
73#define V_ifaddr_event_tag VNET(ifaddr_event_tag)
74
75extern ipfw_nat_t *ipfw_nat_ptr;
76extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
77extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
78extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
79extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
80
81static void
82ifaddr_change(void *arg __unused, struct ifnet *ifp)
83{
84 struct cfg_nat *ptr;
85 struct ifaddr *ifa;
86
87 IPFW_WLOCK(&V_layer3_chain);
88 /* Check every nat entry... */
89 LIST_FOREACH(ptr, &V_layer3_chain.nat, _next) {
90 /* ...using nic 'ifp->if_xname' as dynamic alias address. */
91 if (strncmp(ptr->if_name, ifp->if_xname, IF_NAMESIZE) == 0) {
92 if_addr_rlock(ifp);
93 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
94 if (ifa->ifa_addr == NULL)
95 continue;
96 if (ifa->ifa_addr->sa_family != AF_INET)
97 continue;
98 ptr->ip = ((struct sockaddr_in *)
99 (ifa->ifa_addr))->sin_addr;
100 LibAliasSetAddress(ptr->lib, ptr->ip);
101 }
102 if_addr_runlock(ifp);
103 }
104 }
105 IPFW_WUNLOCK(&V_layer3_chain);
106}
107
108static void
109flush_nat_ptrs(const int i)
110{
111 struct ip_fw *rule;
112
113 IPFW_WLOCK_ASSERT(&V_layer3_chain);
114 for (rule = V_layer3_chain.rules; rule; rule = rule->next) {
115 ipfw_insn_nat *cmd = (ipfw_insn_nat *)ACTION_PTR(rule);
116 if (cmd->o.opcode != O_NAT)
117 continue;
118 if (cmd->nat != NULL && cmd->nat->id == i)
119 cmd->nat = NULL;
120 }
121}
122
123#define HOOK_NAT(b, p) do { \
124 IPFW_WLOCK_ASSERT(&V_layer3_chain); \
125 LIST_INSERT_HEAD(b, p, _next); \
126 } while (0)
127
128#define UNHOOK_NAT(p) do { \
129 IPFW_WLOCK_ASSERT(&V_layer3_chain); \
130 LIST_REMOVE(p, _next); \
131 } while (0)
132
133#define HOOK_REDIR(b, p) do { \
134 LIST_INSERT_HEAD(b, p, _next); \
135 } while (0)
136
137#define HOOK_SPOOL(b, p) do { \
138 LIST_INSERT_HEAD(b, p, _next); \
139 } while (0)
140
141static void
142del_redir_spool_cfg(struct cfg_nat *n, struct redir_chain *head)
143{
144 struct cfg_redir *r, *tmp_r;
145 struct cfg_spool *s, *tmp_s;
146 int i, num;
147
148 LIST_FOREACH_SAFE(r, head, _next, tmp_r) {
149 num = 1; /* Number of alias_link to delete. */
150 switch (r->mode) {
151 case REDIR_PORT:
152 num = r->pport_cnt;
153 /* FALLTHROUGH */
154 case REDIR_ADDR:
155 case REDIR_PROTO:
156 /* Delete all libalias redirect entry. */
157 for (i = 0; i < num; i++)
158 LibAliasRedirectDelete(n->lib, r->alink[i]);
159 /* Del spool cfg if any. */
160 LIST_FOREACH_SAFE(s, &r->spool_chain, _next, tmp_s) {
161 LIST_REMOVE(s, _next);
162 free(s, M_IPFW);
163 }
164 free(r->alink, M_IPFW);
165 LIST_REMOVE(r, _next);
166 free(r, M_IPFW);
167 break;
168 default:
169 printf("unknown redirect mode: %u\n", r->mode);
170 /* XXX - panic?!?!? */
171 break;
172 }
173 }
174}
175
176static int
177add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
178{
179 struct cfg_redir *r, *ser_r;
180 struct cfg_spool *s, *ser_s;
181 int cnt, off, i;
182 char *panic_err;
183
184 for (cnt = 0, off = 0; cnt < ptr->redir_cnt; cnt++) {
185 ser_r = (struct cfg_redir *)&buf[off];
186 r = malloc(SOF_REDIR, M_IPFW, M_WAITOK | M_ZERO);
187 memcpy(r, ser_r, SOF_REDIR);
188 LIST_INIT(&r->spool_chain);
189 off += SOF_REDIR;
190 r->alink = malloc(sizeof(struct alias_link *) * r->pport_cnt,
191 M_IPFW, M_WAITOK | M_ZERO);
192 switch (r->mode) {
193 case REDIR_ADDR:
194 r->alink[0] = LibAliasRedirectAddr(ptr->lib, r->laddr,
195 r->paddr);
196 break;
197 case REDIR_PORT:
198 for (i = 0 ; i < r->pport_cnt; i++) {
199 /* If remotePort is all ports, set it to 0. */
200 u_short remotePortCopy = r->rport + i;
201 if (r->rport_cnt == 1 && r->rport == 0)
202 remotePortCopy = 0;
203 r->alink[i] = LibAliasRedirectPort(ptr->lib,
204 r->laddr, htons(r->lport + i), r->raddr,
205 htons(remotePortCopy), r->paddr,
206 htons(r->pport + i), r->proto);
207 if (r->alink[i] == NULL) {
208 r->alink[0] = NULL;
209 break;
210 }
211 }
212 break;
213 case REDIR_PROTO:
214 r->alink[0] = LibAliasRedirectProto(ptr->lib ,r->laddr,
215 r->raddr, r->paddr, r->proto);
216 break;
217 default:
218 printf("unknown redirect mode: %u\n", r->mode);
219 break;
220 }
221 if (r->alink[0] == NULL) {
222 panic_err = "LibAliasRedirect* returned NULL";
223 goto bad;
224 } else /* LSNAT handling. */
225 for (i = 0; i < r->spool_cnt; i++) {
226 ser_s = (struct cfg_spool *)&buf[off];
227 s = malloc(SOF_REDIR, M_IPFW,
228 M_WAITOK | M_ZERO);
229 memcpy(s, ser_s, SOF_SPOOL);
230 LibAliasAddServer(ptr->lib, r->alink[0],
231 s->addr, htons(s->port));
232 off += SOF_SPOOL;
233 /* Hook spool entry. */
234 HOOK_SPOOL(&r->spool_chain, s);
235 }
236 /* And finally hook this redir entry. */
237 HOOK_REDIR(&ptr->redir_chain, r);
238 }
239 return (1);
240bad:
241 /* something really bad happened: panic! */
242 panic("%s\n", panic_err);
243}
244
245static int
246ipfw_nat(struct ip_fw_args *args, struct cfg_nat *t, struct mbuf *m)
247{
248 struct mbuf *mcl;
249 struct ip *ip;
250 /* XXX - libalias duct tape */
251 int ldt, retval;
252 char *c;
253
254 ldt = 0;
255 retval = 0;
256 if ((mcl = m_megapullup(m, m->m_pkthdr.len)) ==
257 NULL)
258 goto badnat;
259 ip = mtod(mcl, struct ip *);
260 if (args->eh == NULL) {
261 ip->ip_len = htons(ip->ip_len);
262 ip->ip_off = htons(ip->ip_off);
263 }
264
265 /*
266 * XXX - Libalias checksum offload 'duct tape':
267 *
268 * locally generated packets have only
269 * pseudo-header checksum calculated
270 * and libalias will screw it[1], so
271 * mark them for later fix. Moreover
272 * there are cases when libalias
273 * modify tcp packet data[2], mark it
274 * for later fix too.
275 *
276 * [1] libalias was never meant to run
277 * in kernel, so it doesn't have any
278 * knowledge about checksum
279 * offloading, and it expects a packet
280 * with a full internet
281 * checksum. Unfortunately, packets
282 * generated locally will have just the
283 * pseudo header calculated, and when
284 * libalias tries to adjust the
285 * checksum it will actually screw it.
286 *
287 * [2] when libalias modify tcp's data
288 * content, full TCP checksum has to
289 * be recomputed: the problem is that
290 * libalias doesn't have any idea
291 * about checksum offloading To
292 * workaround this, we do not do
293 * checksumming in LibAlias, but only
294 * mark the packets in th_x2 field. If
295 * we receive a marked packet, we
296 * calculate correct checksum for it
297 * aware of offloading. Why such a
298 * terrible hack instead of
299 * recalculating checksum for each
300 * packet? Because the previous
301 * checksum was not checked!
302 * Recalculating checksums for EVERY
303 * packet will hide ALL transmission
304 * errors. Yes, marked packets still
305 * suffer from this problem. But,
306 * sigh, natd(8) has this problem,
307 * too.
308 *
309 * TODO: -make libalias mbuf aware (so
310 * it can handle delayed checksum and tso)
311 */
312
313 if (mcl->m_pkthdr.rcvif == NULL &&
314 mcl->m_pkthdr.csum_flags &
315 CSUM_DELAY_DATA)
316 ldt = 1;
317
318 c = mtod(mcl, char *);
319 if (args->oif == NULL)
320 retval = LibAliasIn(t->lib, c,
321 mcl->m_len + M_TRAILINGSPACE(mcl));
322 else
323 retval = LibAliasOut(t->lib, c,
324 mcl->m_len + M_TRAILINGSPACE(mcl));
325 if (retval == PKT_ALIAS_RESPOND) {
326 m->m_flags |= M_SKIP_FIREWALL;
327 retval = PKT_ALIAS_OK;
328 }
329 if (retval != PKT_ALIAS_OK &&
330 retval != PKT_ALIAS_FOUND_HEADER_FRAGMENT) {
331 /* XXX - should i add some logging? */
332 m_free(mcl);
333 badnat:
334 args->m = NULL;
335 return (IP_FW_DENY);
336 }
337 mcl->m_pkthdr.len = mcl->m_len =
338 ntohs(ip->ip_len);
339
340 /*
341 * XXX - libalias checksum offload
342 * 'duct tape' (see above)
343 */
344
345 if ((ip->ip_off & htons(IP_OFFMASK)) == 0 &&
346 ip->ip_p == IPPROTO_TCP) {
347 struct tcphdr *th;
348
349 th = (struct tcphdr *)(ip + 1);
350 if (th->th_x2)
351 ldt = 1;
352 }
353
354 if (ldt) {
355 struct tcphdr *th;
356 struct udphdr *uh;
357 u_short cksum;
358
359 ip->ip_len = ntohs(ip->ip_len);
360 cksum = in_pseudo(
361 ip->ip_src.s_addr,
362 ip->ip_dst.s_addr,
363 htons(ip->ip_p + ip->ip_len - (ip->ip_hl << 2))
364 );
365
366 switch (ip->ip_p) {
367 case IPPROTO_TCP:
368 th = (struct tcphdr *)(ip + 1);
369 /*
370 * Maybe it was set in
371 * libalias...
372 */
373 th->th_x2 = 0;
374 th->th_sum = cksum;
375 mcl->m_pkthdr.csum_data =
376 offsetof(struct tcphdr, th_sum);
377 break;
378 case IPPROTO_UDP:
379 uh = (struct udphdr *)(ip + 1);
380 uh->uh_sum = cksum;
381 mcl->m_pkthdr.csum_data =
382 offsetof(struct udphdr, uh_sum);
383 break;
384 }
385 /*
386 * No hw checksum offloading: do it
387 * by ourself.
388 */
389 if ((mcl->m_pkthdr.csum_flags &
390 CSUM_DELAY_DATA) == 0) {
391 in_delayed_cksum(mcl);
392 mcl->m_pkthdr.csum_flags &=
393 ~CSUM_DELAY_DATA;
394 }
395 ip->ip_len = htons(ip->ip_len);
396 }
397
398 if (args->eh == NULL) {
399 ip->ip_len = ntohs(ip->ip_len);
400 ip->ip_off = ntohs(ip->ip_off);
401 }
402
403 args->m = mcl;
404 return (IP_FW_NAT);
405}
406
407static int
408ipfw_nat_cfg(struct sockopt *sopt)
409{
410 struct cfg_nat *ptr, *ser_n;
411 char *buf;
412
413 buf = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
414 sooptcopyin(sopt, buf, NAT_BUF_LEN,
415 sizeof(struct cfg_nat));
416 ser_n = (struct cfg_nat *)buf;
417
418 /*
419 * Find/create nat rule.
420 */
421 IPFW_WLOCK(&V_layer3_chain);
422 LOOKUP_NAT(V_layer3_chain, ser_n->id, ptr);
423 if (ptr == NULL) {
424 /* New rule: allocate and init new instance. */
425 ptr = malloc(sizeof(struct cfg_nat),
426 M_IPFW, M_NOWAIT | M_ZERO);
427 if (ptr == NULL) {
428 IPFW_WUNLOCK(&V_layer3_chain);
429 free(buf, M_IPFW);
430 return (ENOSPC);
431 }
432 ptr->lib = LibAliasInit(NULL);
433 if (ptr->lib == NULL) {
434 IPFW_WUNLOCK(&V_layer3_chain);
435 free(ptr, M_IPFW);
436 free(buf, M_IPFW);
437 return (EINVAL);
438 }
439 LIST_INIT(&ptr->redir_chain);
440 } else {
441 /* Entry already present: temporarly unhook it. */
442 UNHOOK_NAT(ptr);
443 flush_nat_ptrs(ser_n->id);
444 }
445 IPFW_WUNLOCK(&V_layer3_chain);
446
447 /*
448 * Basic nat configuration.
449 */
450 ptr->id = ser_n->id;
451 /*
452 * XXX - what if this rule doesn't nat any ip and just
453 * redirect?
454 * do we set aliasaddress to 0.0.0.0?
455 */
456 ptr->ip = ser_n->ip;
457 ptr->redir_cnt = ser_n->redir_cnt;
458 ptr->mode = ser_n->mode;
459 LibAliasSetMode(ptr->lib, ser_n->mode, ser_n->mode);
460 LibAliasSetAddress(ptr->lib, ptr->ip);
461 memcpy(ptr->if_name, ser_n->if_name, IF_NAMESIZE);
462
463 /*
464 * Redir and LSNAT configuration.
465 */
466 /* Delete old cfgs. */
467 del_redir_spool_cfg(ptr, &ptr->redir_chain);
468 /* Add new entries. */
469 add_redir_spool_cfg(&buf[(sizeof(struct cfg_nat))], ptr);
470 free(buf, M_IPFW);
471 IPFW_WLOCK(&V_layer3_chain);
472 HOOK_NAT(&V_layer3_chain.nat, ptr);
473 IPFW_WUNLOCK(&V_layer3_chain);
474 return (0);
475}
476
477static int
478ipfw_nat_del(struct sockopt *sopt)
479{
480 struct cfg_nat *ptr;
481 int i;
482
483 sooptcopyin(sopt, &i, sizeof i, sizeof i);
484 IPFW_WLOCK(&V_layer3_chain);
485 LOOKUP_NAT(V_layer3_chain, i, ptr);
486 if (ptr == NULL) {
487 IPFW_WUNLOCK(&V_layer3_chain);
488 return (EINVAL);
489 }
490 UNHOOK_NAT(ptr);
491 flush_nat_ptrs(i);
492 IPFW_WUNLOCK(&V_layer3_chain);
493 del_redir_spool_cfg(ptr, &ptr->redir_chain);
494 LibAliasUninit(ptr->lib);
495 free(ptr, M_IPFW);
496 return (0);
497}
498
499static int
500ipfw_nat_get_cfg(struct sockopt *sopt)
501{
502 uint8_t *data;
503 struct cfg_nat *n;
504 struct cfg_redir *r;
505 struct cfg_spool *s;
506 int nat_cnt, off;
507
508 nat_cnt = 0;
509 off = sizeof(nat_cnt);
510
511 data = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
512 IPFW_RLOCK(&V_layer3_chain);
513 /* Serialize all the data. */
514 LIST_FOREACH(n, &V_layer3_chain.nat, _next) {
515 nat_cnt++;
516 if (off + SOF_NAT < NAT_BUF_LEN) {
517 bcopy(n, &data[off], SOF_NAT);
518 off += SOF_NAT;
519 LIST_FOREACH(r, &n->redir_chain, _next) {
520 if (off + SOF_REDIR < NAT_BUF_LEN) {
521 bcopy(r, &data[off],
522 SOF_REDIR);
523 off += SOF_REDIR;
524 LIST_FOREACH(s, &r->spool_chain,
525 _next) {
526 if (off + SOF_SPOOL <
527 NAT_BUF_LEN) {
528 bcopy(s, &data[off],
529 SOF_SPOOL);
530 off += SOF_SPOOL;
531 } else
532 goto nospace;
533 }
534 } else
535 goto nospace;
536 }
537 } else
538 goto nospace;
539 }
540 bcopy(&nat_cnt, data, sizeof(nat_cnt));
541 IPFW_RUNLOCK(&V_layer3_chain);
542 sooptcopyout(sopt, data, NAT_BUF_LEN);
543 free(data, M_IPFW);
544 return (0);
545nospace:
546 IPFW_RUNLOCK(&V_layer3_chain);
547 printf("serialized data buffer not big enough:"
548 "please increase NAT_BUF_LEN\n");
549 free(data, M_IPFW);
550 return (ENOSPC);
551}
552
553static int
554ipfw_nat_get_log(struct sockopt *sopt)
555{
556 uint8_t *data;
557 struct cfg_nat *ptr;
558 int i, size, cnt, sof;
559
560 data = NULL;
561 sof = LIBALIAS_BUF_SIZE;
562 cnt = 0;
563
564 IPFW_RLOCK(&V_layer3_chain);
565 size = i = 0;
566 LIST_FOREACH(ptr, &V_layer3_chain.nat, _next) {
567 if (ptr->lib->logDesc == NULL)
568 continue;
569 cnt++;
570 size = cnt * (sof + sizeof(int));
571 data = realloc(data, size, M_IPFW, M_NOWAIT | M_ZERO);
572 if (data == NULL) {
573 IPFW_RUNLOCK(&V_layer3_chain);
574 return (ENOSPC);
575 }
576 bcopy(&ptr->id, &data[i], sizeof(int));
577 i += sizeof(int);
578 bcopy(ptr->lib->logDesc, &data[i], sof);
579 i += sof;
580 }
581 IPFW_RUNLOCK(&V_layer3_chain);
582 sooptcopyout(sopt, data, size);
583 free(data, M_IPFW);
584 return(0);
585}
586
587static void
588ipfw_nat_init(void)
589{
590
591 IPFW_WLOCK(&V_layer3_chain);
592 /* init ipfw hooks */
593 ipfw_nat_ptr = ipfw_nat;
594 ipfw_nat_cfg_ptr = ipfw_nat_cfg;
595 ipfw_nat_del_ptr = ipfw_nat_del;
596 ipfw_nat_get_cfg_ptr = ipfw_nat_get_cfg;
597 ipfw_nat_get_log_ptr = ipfw_nat_get_log;
598 IPFW_WUNLOCK(&V_layer3_chain);
599 V_ifaddr_event_tag = EVENTHANDLER_REGISTER(ifaddr_event, ifaddr_change,
600 NULL, EVENTHANDLER_PRI_ANY);
601}
602
603static void
604ipfw_nat_destroy(void)
605{
606 struct ip_fw *rule;
607 struct cfg_nat *ptr, *ptr_temp;
608
609 IPFW_WLOCK(&V_layer3_chain);
610 LIST_FOREACH_SAFE(ptr, &V_layer3_chain.nat, _next, ptr_temp) {
611 LIST_REMOVE(ptr, _next);
612 del_redir_spool_cfg(ptr, &ptr->redir_chain);
613 LibAliasUninit(ptr->lib);
614 free(ptr, M_IPFW);
615 }
616 EVENTHANDLER_DEREGISTER(ifaddr_event, V_ifaddr_event_tag);
617 /* flush all nat ptrs */
618 for (rule = V_layer3_chain.rules; rule; rule = rule->next) {
619 ipfw_insn_nat *cmd = (ipfw_insn_nat *)ACTION_PTR(rule);
620 if (cmd->o.opcode == O_NAT)
621 cmd->nat = NULL;
622 }
623 /* deregister ipfw_nat */
624 ipfw_nat_ptr = NULL;
625 IPFW_WUNLOCK(&V_layer3_chain);
626}
627
628static int
629ipfw_nat_modevent(module_t mod, int type, void *unused)
630{
631 int err = 0;
632
633 switch (type) {
634 case MOD_LOAD:
635 ipfw_nat_init();
636 break;
637
638 case MOD_UNLOAD:
639 ipfw_nat_destroy();
640 break;
641
642 default:
643 return EOPNOTSUPP;
644 break;
645 }
646 return err;
647}
648
649static moduledata_t ipfw_nat_mod = {
650 "ipfw_nat",
651 ipfw_nat_modevent,
652 0
653};
654
655DECLARE_MODULE(ipfw_nat, ipfw_nat_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
656MODULE_DEPEND(ipfw_nat, libalias, 1, 1, 1);
657MODULE_DEPEND(ipfw_nat, ipfw, 2, 2, 2);
658MODULE_VERSION(ipfw_nat, 1);
74
75extern ipfw_nat_t *ipfw_nat_ptr;
76extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
77extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
78extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
79extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
80
81static void
82ifaddr_change(void *arg __unused, struct ifnet *ifp)
83{
84 struct cfg_nat *ptr;
85 struct ifaddr *ifa;
86
87 IPFW_WLOCK(&V_layer3_chain);
88 /* Check every nat entry... */
89 LIST_FOREACH(ptr, &V_layer3_chain.nat, _next) {
90 /* ...using nic 'ifp->if_xname' as dynamic alias address. */
91 if (strncmp(ptr->if_name, ifp->if_xname, IF_NAMESIZE) == 0) {
92 if_addr_rlock(ifp);
93 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
94 if (ifa->ifa_addr == NULL)
95 continue;
96 if (ifa->ifa_addr->sa_family != AF_INET)
97 continue;
98 ptr->ip = ((struct sockaddr_in *)
99 (ifa->ifa_addr))->sin_addr;
100 LibAliasSetAddress(ptr->lib, ptr->ip);
101 }
102 if_addr_runlock(ifp);
103 }
104 }
105 IPFW_WUNLOCK(&V_layer3_chain);
106}
107
108static void
109flush_nat_ptrs(const int i)
110{
111 struct ip_fw *rule;
112
113 IPFW_WLOCK_ASSERT(&V_layer3_chain);
114 for (rule = V_layer3_chain.rules; rule; rule = rule->next) {
115 ipfw_insn_nat *cmd = (ipfw_insn_nat *)ACTION_PTR(rule);
116 if (cmd->o.opcode != O_NAT)
117 continue;
118 if (cmd->nat != NULL && cmd->nat->id == i)
119 cmd->nat = NULL;
120 }
121}
122
123#define HOOK_NAT(b, p) do { \
124 IPFW_WLOCK_ASSERT(&V_layer3_chain); \
125 LIST_INSERT_HEAD(b, p, _next); \
126 } while (0)
127
128#define UNHOOK_NAT(p) do { \
129 IPFW_WLOCK_ASSERT(&V_layer3_chain); \
130 LIST_REMOVE(p, _next); \
131 } while (0)
132
133#define HOOK_REDIR(b, p) do { \
134 LIST_INSERT_HEAD(b, p, _next); \
135 } while (0)
136
137#define HOOK_SPOOL(b, p) do { \
138 LIST_INSERT_HEAD(b, p, _next); \
139 } while (0)
140
141static void
142del_redir_spool_cfg(struct cfg_nat *n, struct redir_chain *head)
143{
144 struct cfg_redir *r, *tmp_r;
145 struct cfg_spool *s, *tmp_s;
146 int i, num;
147
148 LIST_FOREACH_SAFE(r, head, _next, tmp_r) {
149 num = 1; /* Number of alias_link to delete. */
150 switch (r->mode) {
151 case REDIR_PORT:
152 num = r->pport_cnt;
153 /* FALLTHROUGH */
154 case REDIR_ADDR:
155 case REDIR_PROTO:
156 /* Delete all libalias redirect entry. */
157 for (i = 0; i < num; i++)
158 LibAliasRedirectDelete(n->lib, r->alink[i]);
159 /* Del spool cfg if any. */
160 LIST_FOREACH_SAFE(s, &r->spool_chain, _next, tmp_s) {
161 LIST_REMOVE(s, _next);
162 free(s, M_IPFW);
163 }
164 free(r->alink, M_IPFW);
165 LIST_REMOVE(r, _next);
166 free(r, M_IPFW);
167 break;
168 default:
169 printf("unknown redirect mode: %u\n", r->mode);
170 /* XXX - panic?!?!? */
171 break;
172 }
173 }
174}
175
176static int
177add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
178{
179 struct cfg_redir *r, *ser_r;
180 struct cfg_spool *s, *ser_s;
181 int cnt, off, i;
182 char *panic_err;
183
184 for (cnt = 0, off = 0; cnt < ptr->redir_cnt; cnt++) {
185 ser_r = (struct cfg_redir *)&buf[off];
186 r = malloc(SOF_REDIR, M_IPFW, M_WAITOK | M_ZERO);
187 memcpy(r, ser_r, SOF_REDIR);
188 LIST_INIT(&r->spool_chain);
189 off += SOF_REDIR;
190 r->alink = malloc(sizeof(struct alias_link *) * r->pport_cnt,
191 M_IPFW, M_WAITOK | M_ZERO);
192 switch (r->mode) {
193 case REDIR_ADDR:
194 r->alink[0] = LibAliasRedirectAddr(ptr->lib, r->laddr,
195 r->paddr);
196 break;
197 case REDIR_PORT:
198 for (i = 0 ; i < r->pport_cnt; i++) {
199 /* If remotePort is all ports, set it to 0. */
200 u_short remotePortCopy = r->rport + i;
201 if (r->rport_cnt == 1 && r->rport == 0)
202 remotePortCopy = 0;
203 r->alink[i] = LibAliasRedirectPort(ptr->lib,
204 r->laddr, htons(r->lport + i), r->raddr,
205 htons(remotePortCopy), r->paddr,
206 htons(r->pport + i), r->proto);
207 if (r->alink[i] == NULL) {
208 r->alink[0] = NULL;
209 break;
210 }
211 }
212 break;
213 case REDIR_PROTO:
214 r->alink[0] = LibAliasRedirectProto(ptr->lib ,r->laddr,
215 r->raddr, r->paddr, r->proto);
216 break;
217 default:
218 printf("unknown redirect mode: %u\n", r->mode);
219 break;
220 }
221 if (r->alink[0] == NULL) {
222 panic_err = "LibAliasRedirect* returned NULL";
223 goto bad;
224 } else /* LSNAT handling. */
225 for (i = 0; i < r->spool_cnt; i++) {
226 ser_s = (struct cfg_spool *)&buf[off];
227 s = malloc(SOF_REDIR, M_IPFW,
228 M_WAITOK | M_ZERO);
229 memcpy(s, ser_s, SOF_SPOOL);
230 LibAliasAddServer(ptr->lib, r->alink[0],
231 s->addr, htons(s->port));
232 off += SOF_SPOOL;
233 /* Hook spool entry. */
234 HOOK_SPOOL(&r->spool_chain, s);
235 }
236 /* And finally hook this redir entry. */
237 HOOK_REDIR(&ptr->redir_chain, r);
238 }
239 return (1);
240bad:
241 /* something really bad happened: panic! */
242 panic("%s\n", panic_err);
243}
244
245static int
246ipfw_nat(struct ip_fw_args *args, struct cfg_nat *t, struct mbuf *m)
247{
248 struct mbuf *mcl;
249 struct ip *ip;
250 /* XXX - libalias duct tape */
251 int ldt, retval;
252 char *c;
253
254 ldt = 0;
255 retval = 0;
256 if ((mcl = m_megapullup(m, m->m_pkthdr.len)) ==
257 NULL)
258 goto badnat;
259 ip = mtod(mcl, struct ip *);
260 if (args->eh == NULL) {
261 ip->ip_len = htons(ip->ip_len);
262 ip->ip_off = htons(ip->ip_off);
263 }
264
265 /*
266 * XXX - Libalias checksum offload 'duct tape':
267 *
268 * locally generated packets have only
269 * pseudo-header checksum calculated
270 * and libalias will screw it[1], so
271 * mark them for later fix. Moreover
272 * there are cases when libalias
273 * modify tcp packet data[2], mark it
274 * for later fix too.
275 *
276 * [1] libalias was never meant to run
277 * in kernel, so it doesn't have any
278 * knowledge about checksum
279 * offloading, and it expects a packet
280 * with a full internet
281 * checksum. Unfortunately, packets
282 * generated locally will have just the
283 * pseudo header calculated, and when
284 * libalias tries to adjust the
285 * checksum it will actually screw it.
286 *
287 * [2] when libalias modify tcp's data
288 * content, full TCP checksum has to
289 * be recomputed: the problem is that
290 * libalias doesn't have any idea
291 * about checksum offloading To
292 * workaround this, we do not do
293 * checksumming in LibAlias, but only
294 * mark the packets in th_x2 field. If
295 * we receive a marked packet, we
296 * calculate correct checksum for it
297 * aware of offloading. Why such a
298 * terrible hack instead of
299 * recalculating checksum for each
300 * packet? Because the previous
301 * checksum was not checked!
302 * Recalculating checksums for EVERY
303 * packet will hide ALL transmission
304 * errors. Yes, marked packets still
305 * suffer from this problem. But,
306 * sigh, natd(8) has this problem,
307 * too.
308 *
309 * TODO: -make libalias mbuf aware (so
310 * it can handle delayed checksum and tso)
311 */
312
313 if (mcl->m_pkthdr.rcvif == NULL &&
314 mcl->m_pkthdr.csum_flags &
315 CSUM_DELAY_DATA)
316 ldt = 1;
317
318 c = mtod(mcl, char *);
319 if (args->oif == NULL)
320 retval = LibAliasIn(t->lib, c,
321 mcl->m_len + M_TRAILINGSPACE(mcl));
322 else
323 retval = LibAliasOut(t->lib, c,
324 mcl->m_len + M_TRAILINGSPACE(mcl));
325 if (retval == PKT_ALIAS_RESPOND) {
326 m->m_flags |= M_SKIP_FIREWALL;
327 retval = PKT_ALIAS_OK;
328 }
329 if (retval != PKT_ALIAS_OK &&
330 retval != PKT_ALIAS_FOUND_HEADER_FRAGMENT) {
331 /* XXX - should i add some logging? */
332 m_free(mcl);
333 badnat:
334 args->m = NULL;
335 return (IP_FW_DENY);
336 }
337 mcl->m_pkthdr.len = mcl->m_len =
338 ntohs(ip->ip_len);
339
340 /*
341 * XXX - libalias checksum offload
342 * 'duct tape' (see above)
343 */
344
345 if ((ip->ip_off & htons(IP_OFFMASK)) == 0 &&
346 ip->ip_p == IPPROTO_TCP) {
347 struct tcphdr *th;
348
349 th = (struct tcphdr *)(ip + 1);
350 if (th->th_x2)
351 ldt = 1;
352 }
353
354 if (ldt) {
355 struct tcphdr *th;
356 struct udphdr *uh;
357 u_short cksum;
358
359 ip->ip_len = ntohs(ip->ip_len);
360 cksum = in_pseudo(
361 ip->ip_src.s_addr,
362 ip->ip_dst.s_addr,
363 htons(ip->ip_p + ip->ip_len - (ip->ip_hl << 2))
364 );
365
366 switch (ip->ip_p) {
367 case IPPROTO_TCP:
368 th = (struct tcphdr *)(ip + 1);
369 /*
370 * Maybe it was set in
371 * libalias...
372 */
373 th->th_x2 = 0;
374 th->th_sum = cksum;
375 mcl->m_pkthdr.csum_data =
376 offsetof(struct tcphdr, th_sum);
377 break;
378 case IPPROTO_UDP:
379 uh = (struct udphdr *)(ip + 1);
380 uh->uh_sum = cksum;
381 mcl->m_pkthdr.csum_data =
382 offsetof(struct udphdr, uh_sum);
383 break;
384 }
385 /*
386 * No hw checksum offloading: do it
387 * by ourself.
388 */
389 if ((mcl->m_pkthdr.csum_flags &
390 CSUM_DELAY_DATA) == 0) {
391 in_delayed_cksum(mcl);
392 mcl->m_pkthdr.csum_flags &=
393 ~CSUM_DELAY_DATA;
394 }
395 ip->ip_len = htons(ip->ip_len);
396 }
397
398 if (args->eh == NULL) {
399 ip->ip_len = ntohs(ip->ip_len);
400 ip->ip_off = ntohs(ip->ip_off);
401 }
402
403 args->m = mcl;
404 return (IP_FW_NAT);
405}
406
407static int
408ipfw_nat_cfg(struct sockopt *sopt)
409{
410 struct cfg_nat *ptr, *ser_n;
411 char *buf;
412
413 buf = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
414 sooptcopyin(sopt, buf, NAT_BUF_LEN,
415 sizeof(struct cfg_nat));
416 ser_n = (struct cfg_nat *)buf;
417
418 /*
419 * Find/create nat rule.
420 */
421 IPFW_WLOCK(&V_layer3_chain);
422 LOOKUP_NAT(V_layer3_chain, ser_n->id, ptr);
423 if (ptr == NULL) {
424 /* New rule: allocate and init new instance. */
425 ptr = malloc(sizeof(struct cfg_nat),
426 M_IPFW, M_NOWAIT | M_ZERO);
427 if (ptr == NULL) {
428 IPFW_WUNLOCK(&V_layer3_chain);
429 free(buf, M_IPFW);
430 return (ENOSPC);
431 }
432 ptr->lib = LibAliasInit(NULL);
433 if (ptr->lib == NULL) {
434 IPFW_WUNLOCK(&V_layer3_chain);
435 free(ptr, M_IPFW);
436 free(buf, M_IPFW);
437 return (EINVAL);
438 }
439 LIST_INIT(&ptr->redir_chain);
440 } else {
441 /* Entry already present: temporarly unhook it. */
442 UNHOOK_NAT(ptr);
443 flush_nat_ptrs(ser_n->id);
444 }
445 IPFW_WUNLOCK(&V_layer3_chain);
446
447 /*
448 * Basic nat configuration.
449 */
450 ptr->id = ser_n->id;
451 /*
452 * XXX - what if this rule doesn't nat any ip and just
453 * redirect?
454 * do we set aliasaddress to 0.0.0.0?
455 */
456 ptr->ip = ser_n->ip;
457 ptr->redir_cnt = ser_n->redir_cnt;
458 ptr->mode = ser_n->mode;
459 LibAliasSetMode(ptr->lib, ser_n->mode, ser_n->mode);
460 LibAliasSetAddress(ptr->lib, ptr->ip);
461 memcpy(ptr->if_name, ser_n->if_name, IF_NAMESIZE);
462
463 /*
464 * Redir and LSNAT configuration.
465 */
466 /* Delete old cfgs. */
467 del_redir_spool_cfg(ptr, &ptr->redir_chain);
468 /* Add new entries. */
469 add_redir_spool_cfg(&buf[(sizeof(struct cfg_nat))], ptr);
470 free(buf, M_IPFW);
471 IPFW_WLOCK(&V_layer3_chain);
472 HOOK_NAT(&V_layer3_chain.nat, ptr);
473 IPFW_WUNLOCK(&V_layer3_chain);
474 return (0);
475}
476
477static int
478ipfw_nat_del(struct sockopt *sopt)
479{
480 struct cfg_nat *ptr;
481 int i;
482
483 sooptcopyin(sopt, &i, sizeof i, sizeof i);
484 IPFW_WLOCK(&V_layer3_chain);
485 LOOKUP_NAT(V_layer3_chain, i, ptr);
486 if (ptr == NULL) {
487 IPFW_WUNLOCK(&V_layer3_chain);
488 return (EINVAL);
489 }
490 UNHOOK_NAT(ptr);
491 flush_nat_ptrs(i);
492 IPFW_WUNLOCK(&V_layer3_chain);
493 del_redir_spool_cfg(ptr, &ptr->redir_chain);
494 LibAliasUninit(ptr->lib);
495 free(ptr, M_IPFW);
496 return (0);
497}
498
499static int
500ipfw_nat_get_cfg(struct sockopt *sopt)
501{
502 uint8_t *data;
503 struct cfg_nat *n;
504 struct cfg_redir *r;
505 struct cfg_spool *s;
506 int nat_cnt, off;
507
508 nat_cnt = 0;
509 off = sizeof(nat_cnt);
510
511 data = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
512 IPFW_RLOCK(&V_layer3_chain);
513 /* Serialize all the data. */
514 LIST_FOREACH(n, &V_layer3_chain.nat, _next) {
515 nat_cnt++;
516 if (off + SOF_NAT < NAT_BUF_LEN) {
517 bcopy(n, &data[off], SOF_NAT);
518 off += SOF_NAT;
519 LIST_FOREACH(r, &n->redir_chain, _next) {
520 if (off + SOF_REDIR < NAT_BUF_LEN) {
521 bcopy(r, &data[off],
522 SOF_REDIR);
523 off += SOF_REDIR;
524 LIST_FOREACH(s, &r->spool_chain,
525 _next) {
526 if (off + SOF_SPOOL <
527 NAT_BUF_LEN) {
528 bcopy(s, &data[off],
529 SOF_SPOOL);
530 off += SOF_SPOOL;
531 } else
532 goto nospace;
533 }
534 } else
535 goto nospace;
536 }
537 } else
538 goto nospace;
539 }
540 bcopy(&nat_cnt, data, sizeof(nat_cnt));
541 IPFW_RUNLOCK(&V_layer3_chain);
542 sooptcopyout(sopt, data, NAT_BUF_LEN);
543 free(data, M_IPFW);
544 return (0);
545nospace:
546 IPFW_RUNLOCK(&V_layer3_chain);
547 printf("serialized data buffer not big enough:"
548 "please increase NAT_BUF_LEN\n");
549 free(data, M_IPFW);
550 return (ENOSPC);
551}
552
553static int
554ipfw_nat_get_log(struct sockopt *sopt)
555{
556 uint8_t *data;
557 struct cfg_nat *ptr;
558 int i, size, cnt, sof;
559
560 data = NULL;
561 sof = LIBALIAS_BUF_SIZE;
562 cnt = 0;
563
564 IPFW_RLOCK(&V_layer3_chain);
565 size = i = 0;
566 LIST_FOREACH(ptr, &V_layer3_chain.nat, _next) {
567 if (ptr->lib->logDesc == NULL)
568 continue;
569 cnt++;
570 size = cnt * (sof + sizeof(int));
571 data = realloc(data, size, M_IPFW, M_NOWAIT | M_ZERO);
572 if (data == NULL) {
573 IPFW_RUNLOCK(&V_layer3_chain);
574 return (ENOSPC);
575 }
576 bcopy(&ptr->id, &data[i], sizeof(int));
577 i += sizeof(int);
578 bcopy(ptr->lib->logDesc, &data[i], sof);
579 i += sof;
580 }
581 IPFW_RUNLOCK(&V_layer3_chain);
582 sooptcopyout(sopt, data, size);
583 free(data, M_IPFW);
584 return(0);
585}
586
587static void
588ipfw_nat_init(void)
589{
590
591 IPFW_WLOCK(&V_layer3_chain);
592 /* init ipfw hooks */
593 ipfw_nat_ptr = ipfw_nat;
594 ipfw_nat_cfg_ptr = ipfw_nat_cfg;
595 ipfw_nat_del_ptr = ipfw_nat_del;
596 ipfw_nat_get_cfg_ptr = ipfw_nat_get_cfg;
597 ipfw_nat_get_log_ptr = ipfw_nat_get_log;
598 IPFW_WUNLOCK(&V_layer3_chain);
599 V_ifaddr_event_tag = EVENTHANDLER_REGISTER(ifaddr_event, ifaddr_change,
600 NULL, EVENTHANDLER_PRI_ANY);
601}
602
603static void
604ipfw_nat_destroy(void)
605{
606 struct ip_fw *rule;
607 struct cfg_nat *ptr, *ptr_temp;
608
609 IPFW_WLOCK(&V_layer3_chain);
610 LIST_FOREACH_SAFE(ptr, &V_layer3_chain.nat, _next, ptr_temp) {
611 LIST_REMOVE(ptr, _next);
612 del_redir_spool_cfg(ptr, &ptr->redir_chain);
613 LibAliasUninit(ptr->lib);
614 free(ptr, M_IPFW);
615 }
616 EVENTHANDLER_DEREGISTER(ifaddr_event, V_ifaddr_event_tag);
617 /* flush all nat ptrs */
618 for (rule = V_layer3_chain.rules; rule; rule = rule->next) {
619 ipfw_insn_nat *cmd = (ipfw_insn_nat *)ACTION_PTR(rule);
620 if (cmd->o.opcode == O_NAT)
621 cmd->nat = NULL;
622 }
623 /* deregister ipfw_nat */
624 ipfw_nat_ptr = NULL;
625 IPFW_WUNLOCK(&V_layer3_chain);
626}
627
628static int
629ipfw_nat_modevent(module_t mod, int type, void *unused)
630{
631 int err = 0;
632
633 switch (type) {
634 case MOD_LOAD:
635 ipfw_nat_init();
636 break;
637
638 case MOD_UNLOAD:
639 ipfw_nat_destroy();
640 break;
641
642 default:
643 return EOPNOTSUPP;
644 break;
645 }
646 return err;
647}
648
649static moduledata_t ipfw_nat_mod = {
650 "ipfw_nat",
651 ipfw_nat_modevent,
652 0
653};
654
655DECLARE_MODULE(ipfw_nat, ipfw_nat_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
656MODULE_DEPEND(ipfw_nat, libalias, 1, 1, 1);
657MODULE_DEPEND(ipfw_nat, ipfw, 2, 2, 2);
658MODULE_VERSION(ipfw_nat, 1);