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