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