2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Paolo Pisati
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/eventhandler.h>
35 #include <sys/malloc.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/rwlock.h>
41 #include <sys/rmlock.h>
43 #include <netinet/libalias/alias.h>
44 #include <netinet/libalias/alias_local.h>
47 #include <net/if_var.h>
48 #include <netinet/in.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/ip_fw.h>
52 #include <netinet/tcp.h>
53 #include <netinet/udp.h>
55 #include <netpfil/ipfw/ip_fw_private.h>
57 #include <machine/in_cksum.h> /* XXX for in_cksum */
60 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */
65 /* Nat redirect configuration. */
67 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */
68 uint16_t mode; /* type of redirect mode */
69 uint16_t proto; /* protocol: tcp/udp */
70 struct in_addr laddr; /* local ip address */
71 struct in_addr paddr; /* public ip address */
72 struct in_addr raddr; /* remote ip address */
73 uint16_t lport; /* local port */
74 uint16_t pport; /* public port */
75 uint16_t rport; /* remote port */
76 uint16_t pport_cnt; /* number of public ports */
77 uint16_t rport_cnt; /* number of remote ports */
78 struct alias_link **alink;
79 u_int16_t spool_cnt; /* num of entry in spool chain */
80 /* chain of spool instances */
81 LIST_HEAD(spool_chain, cfg_spool) spool_chain;
84 /* Nat configuration data struct. */
86 /* chain of nat instances */
87 LIST_ENTRY(cfg_nat) _next;
89 struct in_addr ip; /* nat ip address */
90 struct libalias *lib; /* libalias instance */
91 int mode; /* aliasing mode */
92 int redir_cnt; /* number of entry in spool chain */
93 /* chain of redir instances */
94 LIST_HEAD(redir_chain, cfg_redir) redir_chain;
95 char if_name[IF_NAMESIZE]; /* interface name */
98 static eventhandler_tag ifaddr_event_tag;
101 ifaddr_change(void *arg __unused, struct ifnet *ifp)
105 struct ip_fw_chain *chain;
107 KASSERT(curvnet == ifp->if_vnet,
108 ("curvnet(%p) differs from iface vnet(%p)", curvnet, ifp->if_vnet));
110 if (V_ipfw_vnet_ready == 0 || V_ipfw_nat_ready == 0)
113 chain = &V_layer3_chain;
114 IPFW_UH_WLOCK(chain);
115 /* Check every nat entry... */
116 LIST_FOREACH(ptr, &chain->nat, _next) {
117 struct epoch_tracker et;
119 /* ...using nic 'ifp->if_xname' as dynamic alias address. */
120 if (strncmp(ptr->if_name, ifp->if_xname, IF_NAMESIZE) != 0)
123 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
124 if (ifa->ifa_addr == NULL)
126 if (ifa->ifa_addr->sa_family != AF_INET)
129 ptr->ip = ((struct sockaddr_in *)
130 (ifa->ifa_addr))->sin_addr;
131 LibAliasSetAddress(ptr->lib, ptr->ip);
136 IPFW_UH_WUNLOCK(chain);
140 * delete the pointers for nat entry ix, or all of them if ix < 0
143 flush_nat_ptrs(struct ip_fw_chain *chain, const int ix)
148 IPFW_WLOCK_ASSERT(chain);
149 for (i = 0; i < chain->n_rules; i++) {
150 cmd = (ipfw_insn_nat *)ipfw_get_action(chain->map[i]);
151 if (cmd->o.opcode == O_NAT && cmd->nat != NULL &&
152 (ix < 0 || cmd->nat->id == ix))
158 del_redir_spool_cfg(struct cfg_nat *n, struct redir_chain *head)
160 struct cfg_redir *r, *tmp_r;
161 struct cfg_spool *s, *tmp_s;
164 LIST_FOREACH_SAFE(r, head, _next, tmp_r) {
165 num = 1; /* Number of alias_link to delete. */
167 case NAT44_REDIR_PORT:
170 case NAT44_REDIR_ADDR:
171 case NAT44_REDIR_PROTO:
172 /* Delete all libalias redirect entry. */
173 for (i = 0; i < num; i++)
174 LibAliasRedirectDelete(n->lib, r->alink[i]);
175 /* Del spool cfg if any. */
176 LIST_FOREACH_SAFE(s, &r->spool_chain, _next, tmp_s) {
177 LIST_REMOVE(s, _next);
180 free(r->alink, M_IPFW);
181 LIST_REMOVE(r, _next);
185 printf("unknown redirect mode: %u\n", r->mode);
186 /* XXX - panic?!?!? */
193 add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
197 struct nat44_cfg_redir *ser_r;
198 struct nat44_cfg_spool *ser_s;
202 for (cnt = 0, off = 0; cnt < ptr->redir_cnt; cnt++) {
203 ser_r = (struct nat44_cfg_redir *)&buf[off];
204 r = malloc(sizeof(*r), M_IPFW, M_WAITOK | M_ZERO);
205 r->mode = ser_r->mode;
206 r->laddr = ser_r->laddr;
207 r->paddr = ser_r->paddr;
208 r->raddr = ser_r->raddr;
209 r->lport = ser_r->lport;
210 r->pport = ser_r->pport;
211 r->rport = ser_r->rport;
212 r->pport_cnt = ser_r->pport_cnt;
213 r->rport_cnt = ser_r->rport_cnt;
214 r->proto = ser_r->proto;
215 r->spool_cnt = ser_r->spool_cnt;
216 //memcpy(r, ser_r, SOF_REDIR);
217 LIST_INIT(&r->spool_chain);
218 off += sizeof(struct nat44_cfg_redir);
219 r->alink = malloc(sizeof(struct alias_link *) * r->pport_cnt,
220 M_IPFW, M_WAITOK | M_ZERO);
222 case NAT44_REDIR_ADDR:
223 r->alink[0] = LibAliasRedirectAddr(ptr->lib, r->laddr,
226 case NAT44_REDIR_PORT:
227 for (i = 0 ; i < r->pport_cnt; i++) {
228 /* If remotePort is all ports, set it to 0. */
229 u_short remotePortCopy = r->rport + i;
230 if (r->rport_cnt == 1 && r->rport == 0)
232 r->alink[i] = LibAliasRedirectPort(ptr->lib,
233 r->laddr, htons(r->lport + i), r->raddr,
234 htons(remotePortCopy), r->paddr,
235 htons(r->pport + i), r->proto);
236 if (r->alink[i] == NULL) {
242 case NAT44_REDIR_PROTO:
243 r->alink[0] = LibAliasRedirectProto(ptr->lib ,r->laddr,
244 r->raddr, r->paddr, r->proto);
247 printf("unknown redirect mode: %u\n", r->mode);
250 if (r->alink[0] == NULL) {
251 printf("LibAliasRedirect* returned NULL\n");
252 free(r->alink, M_IPFW);
256 /* LSNAT handling. */
257 for (i = 0; i < r->spool_cnt; i++) {
258 ser_s = (struct nat44_cfg_spool *)&buf[off];
259 s = malloc(sizeof(*s), M_IPFW, M_WAITOK | M_ZERO);
260 s->addr = ser_s->addr;
261 s->port = ser_s->port;
262 LibAliasAddServer(ptr->lib, r->alink[0],
263 s->addr, htons(s->port));
264 off += sizeof(struct nat44_cfg_spool);
265 /* Hook spool entry. */
266 LIST_INSERT_HEAD(&r->spool_chain, s, _next);
268 /* And finally hook this redir entry. */
269 LIST_INSERT_HEAD(&ptr->redir_chain, r, _next);
276 free_nat_instance(struct cfg_nat *ptr)
279 del_redir_spool_cfg(ptr, &ptr->redir_chain);
280 LibAliasUninit(ptr->lib);
286 * ipfw_nat - perform mbuf header translation.
288 * Note V_layer3_chain has to be locked while calling ipfw_nat() in
289 * 'global' operation mode (t == NULL).
293 ipfw_nat(struct ip_fw_args *args, struct cfg_nat *t, struct mbuf *m)
297 /* XXX - libalias duct tape */
298 int ldt, retval, found;
299 struct ip_fw_chain *chain;
304 mcl = m_megapullup(m, m->m_pkthdr.len);
309 ip = mtod(mcl, struct ip *);
312 * XXX - Libalias checksum offload 'duct tape':
314 * locally generated packets have only pseudo-header checksum
315 * calculated and libalias will break it[1], so mark them for
316 * later fix. Moreover there are cases when libalias modifies
317 * tcp packet data[2], mark them for later fix too.
319 * [1] libalias was never meant to run in kernel, so it does
320 * not have any knowledge about checksum offloading, and
321 * expects a packet with a full internet checksum.
322 * Unfortunately, packets generated locally will have just the
323 * pseudo header calculated, and when libalias tries to adjust
324 * the checksum it will actually compute a wrong value.
326 * [2] when libalias modifies tcp's data content, full TCP
327 * checksum has to be recomputed: the problem is that
328 * libalias does not have any idea about checksum offloading.
329 * To work around this, we do not do checksumming in LibAlias,
330 * but only mark the packets in th_x2 field. If we receive a
331 * marked packet, we calculate correct checksum for it
332 * aware of offloading. Why such a terrible hack instead of
333 * recalculating checksum for each packet?
334 * Because the previous checksum was not checked!
335 * Recalculating checksums for EVERY packet will hide ALL
336 * transmission errors. Yes, marked packets still suffer from
337 * this problem. But, sigh, natd(8) has this problem, too.
339 * TODO: -make libalias mbuf aware (so
340 * it can handle delayed checksum and tso)
343 if (mcl->m_pkthdr.rcvif == NULL &&
344 mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
347 c = mtod(mcl, char *);
349 /* Check if this is 'global' instance */
351 if (args->flags & IPFW_ARGS_IN) {
352 /* Wrong direction, skip processing */
358 chain = &V_layer3_chain;
359 IPFW_RLOCK_ASSERT(chain);
360 /* Check every nat entry... */
361 LIST_FOREACH(t, &chain->nat, _next) {
362 if ((t->mode & PKT_ALIAS_SKIP_GLOBAL) != 0)
364 retval = LibAliasOutTry(t->lib, c,
365 mcl->m_len + M_TRAILINGSPACE(mcl), 0);
366 if (retval == PKT_ALIAS_OK) {
367 /* Nat instance recognises state */
373 /* No instance found, return ignore */
378 if (args->flags & IPFW_ARGS_IN)
379 retval = LibAliasIn(t->lib, c,
380 mcl->m_len + M_TRAILINGSPACE(mcl));
382 retval = LibAliasOut(t->lib, c,
383 mcl->m_len + M_TRAILINGSPACE(mcl));
387 * We drop packet when:
388 * 1. libalias returns PKT_ALIAS_ERROR;
389 * 2. For incoming packets:
390 * a) for unresolved fragments;
391 * b) libalias returns PKT_ALIAS_IGNORED and
392 * PKT_ALIAS_DENY_INCOMING flag is set.
394 if (retval == PKT_ALIAS_ERROR ||
395 ((args->flags & IPFW_ARGS_IN) &&
396 (retval == PKT_ALIAS_UNRESOLVED_FRAGMENT ||
397 (retval == PKT_ALIAS_IGNORED &&
398 (t->mode & PKT_ALIAS_DENY_INCOMING) != 0)))) {
399 /* XXX - should i add some logging? */
405 if (retval == PKT_ALIAS_RESPOND)
406 mcl->m_flags |= M_SKIP_FIREWALL;
407 mcl->m_pkthdr.len = mcl->m_len = ntohs(ip->ip_len);
410 * XXX - libalias checksum offload
411 * 'duct tape' (see above)
414 if ((ip->ip_off & htons(IP_OFFMASK)) == 0 &&
415 ip->ip_p == IPPROTO_TCP) {
418 th = (struct tcphdr *)(ip + 1);
426 uint16_t ip_len, cksum;
428 ip_len = ntohs(ip->ip_len);
429 cksum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
430 htons(ip->ip_p + ip_len - (ip->ip_hl << 2)));
434 th = (struct tcphdr *)(ip + 1);
436 * Maybe it was set in
441 mcl->m_pkthdr.csum_data =
442 offsetof(struct tcphdr, th_sum);
445 uh = (struct udphdr *)(ip + 1);
447 mcl->m_pkthdr.csum_data =
448 offsetof(struct udphdr, uh_sum);
451 /* No hw checksum offloading: do it ourselves */
452 if ((mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA) == 0) {
453 in_delayed_cksum(mcl);
454 mcl->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
461 static struct cfg_nat *
462 lookup_nat(struct nat_list *l, int nat_id)
466 LIST_FOREACH(res, l, _next) {
467 if (res->id == nat_id)
473 static struct cfg_nat *
474 lookup_nat_name(struct nat_list *l, char *name)
480 id = strtol(name, &errptr, 10);
481 if (id == 0 || *errptr != '\0')
484 LIST_FOREACH(res, l, _next) {
491 /* IP_FW3 configuration routines */
494 nat44_config(struct ip_fw_chain *chain, struct nat44_cfg_nat *ucfg)
496 struct cfg_nat *ptr, *tcfg;
500 * Find/create nat rule.
502 IPFW_UH_WLOCK(chain);
503 gencnt = chain->gencnt;
504 ptr = lookup_nat_name(&chain->nat, ucfg->name);
506 IPFW_UH_WUNLOCK(chain);
507 /* New rule: allocate and init new instance. */
508 ptr = malloc(sizeof(struct cfg_nat), M_IPFW, M_WAITOK | M_ZERO);
509 ptr->lib = LibAliasInit(NULL);
510 LIST_INIT(&ptr->redir_chain);
512 /* Entry already present: temporarily unhook it. */
514 LIST_REMOVE(ptr, _next);
515 flush_nat_ptrs(chain, ptr->id);
517 IPFW_UH_WUNLOCK(chain);
521 * Basic nat (re)configuration.
523 ptr->id = strtol(ucfg->name, NULL, 10);
525 * XXX - what if this rule doesn't nat any ip and just
527 * do we set aliasaddress to 0.0.0.0?
530 ptr->redir_cnt = ucfg->redir_cnt;
531 ptr->mode = ucfg->mode;
532 strlcpy(ptr->if_name, ucfg->if_name, sizeof(ptr->if_name));
533 LibAliasSetMode(ptr->lib, ptr->mode, ~0);
534 LibAliasSetAddress(ptr->lib, ptr->ip);
537 * Redir and LSNAT configuration.
539 /* Delete old cfgs. */
540 del_redir_spool_cfg(ptr, &ptr->redir_chain);
541 /* Add new entries. */
542 add_redir_spool_cfg((char *)(ucfg + 1), ptr);
543 IPFW_UH_WLOCK(chain);
545 /* Extra check to avoid race with another ipfw_nat_cfg() */
547 if (gencnt != chain->gencnt)
548 tcfg = lookup_nat_name(&chain->nat, ucfg->name);
551 LIST_REMOVE(tcfg, _next);
552 LIST_INSERT_HEAD(&chain->nat, ptr, _next);
556 IPFW_UH_WUNLOCK(chain);
559 free_nat_instance(ptr);
563 * Creates/configure nat44 instance
564 * Data layout (v0)(current):
565 * Request: [ ipfw_obj_header nat44_cfg_nat .. ]
567 * Returns 0 on success
570 nat44_cfg(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
571 struct sockopt_data *sd)
574 struct nat44_cfg_nat *ucfg;
579 /* Check minimum header size */
580 if (sd->valsize < (sizeof(*oh) + sizeof(*ucfg)))
583 oh = (ipfw_obj_header *)sd->kbuf;
585 /* Basic length checks for TLVs */
586 if (oh->ntlv.head.length != sizeof(oh->ntlv))
589 ucfg = (struct nat44_cfg_nat *)(oh + 1);
591 /* Check if name is properly terminated and looks like number */
592 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name))
594 id = strtol(ucfg->name, &errptr, 10);
595 if (id == 0 || *errptr != '\0')
598 read = sizeof(*oh) + sizeof(*ucfg);
599 /* Check number of redirs */
600 if (sd->valsize < read + ucfg->redir_cnt*sizeof(struct nat44_cfg_redir))
603 nat44_config(chain, ucfg);
608 * Destroys given nat instances.
609 * Data layout (v0)(current):
610 * Request: [ ipfw_obj_header ]
612 * Returns 0 on success
615 nat44_destroy(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
616 struct sockopt_data *sd)
622 /* Check minimum header size */
623 if (sd->valsize < sizeof(*oh))
626 oh = (ipfw_obj_header *)sd->kbuf;
628 /* Basic length checks for TLVs */
629 if (oh->ntlv.head.length != sizeof(oh->ntlv))
633 /* Check if name is properly terminated */
634 if (strnlen(ntlv->name, sizeof(ntlv->name)) == sizeof(ntlv->name))
637 IPFW_UH_WLOCK(chain);
638 ptr = lookup_nat_name(&chain->nat, ntlv->name);
640 IPFW_UH_WUNLOCK(chain);
644 LIST_REMOVE(ptr, _next);
645 flush_nat_ptrs(chain, ptr->id);
647 IPFW_UH_WUNLOCK(chain);
649 free_nat_instance(ptr);
655 export_nat_cfg(struct cfg_nat *ptr, struct nat44_cfg_nat *ucfg)
658 snprintf(ucfg->name, sizeof(ucfg->name), "%d", ptr->id);
660 ucfg->redir_cnt = ptr->redir_cnt;
661 ucfg->mode = ptr->mode;
662 strlcpy(ucfg->if_name, ptr->if_name, sizeof(ucfg->if_name));
666 * Gets config for given nat instance
667 * Data layout (v0)(current):
668 * Request: [ ipfw_obj_header nat44_cfg_nat .. ]
670 * Returns 0 on success
673 nat44_get_cfg(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
674 struct sockopt_data *sd)
677 struct nat44_cfg_nat *ucfg;
681 struct nat44_cfg_redir *ser_r;
682 struct nat44_cfg_spool *ser_s;
685 sz = sizeof(*oh) + sizeof(*ucfg);
686 /* Check minimum header size */
687 if (sd->valsize < sz)
690 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
692 /* Basic length checks for TLVs */
693 if (oh->ntlv.head.length != sizeof(oh->ntlv))
696 ucfg = (struct nat44_cfg_nat *)(oh + 1);
698 /* Check if name is properly terminated */
699 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name))
702 IPFW_UH_RLOCK(chain);
703 ptr = lookup_nat_name(&chain->nat, ucfg->name);
705 IPFW_UH_RUNLOCK(chain);
709 export_nat_cfg(ptr, ucfg);
711 /* Estimate memory amount */
712 sz = sizeof(ipfw_obj_header) + sizeof(struct nat44_cfg_nat);
713 LIST_FOREACH(r, &ptr->redir_chain, _next) {
714 sz += sizeof(struct nat44_cfg_redir);
715 LIST_FOREACH(s, &r->spool_chain, _next)
716 sz += sizeof(struct nat44_cfg_spool);
720 if (sd->valsize < sz) {
723 * Submitted buffer size is not enough.
724 * WE've already filled in @ucfg structure with
725 * relevant info including size, so we
726 * can return. Buffer will be flushed automatically.
728 IPFW_UH_RUNLOCK(chain);
732 /* Size OK, let's copy data */
733 LIST_FOREACH(r, &ptr->redir_chain, _next) {
734 ser_r = (struct nat44_cfg_redir *)ipfw_get_sopt_space(sd,
736 ser_r->mode = r->mode;
737 ser_r->laddr = r->laddr;
738 ser_r->paddr = r->paddr;
739 ser_r->raddr = r->raddr;
740 ser_r->lport = r->lport;
741 ser_r->pport = r->pport;
742 ser_r->rport = r->rport;
743 ser_r->pport_cnt = r->pport_cnt;
744 ser_r->rport_cnt = r->rport_cnt;
745 ser_r->proto = r->proto;
746 ser_r->spool_cnt = r->spool_cnt;
748 LIST_FOREACH(s, &r->spool_chain, _next) {
749 ser_s = (struct nat44_cfg_spool *)ipfw_get_sopt_space(
752 ser_s->addr = s->addr;
753 ser_s->port = s->port;
757 IPFW_UH_RUNLOCK(chain);
763 * Lists all nat44 instances currently available in kernel.
764 * Data layout (v0)(current):
765 * Request: [ ipfw_obj_lheader ]
766 * Reply: [ ipfw_obj_lheader nat44_cfg_nat x N ]
768 * Returns 0 on success
771 nat44_list_nat(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
772 struct sockopt_data *sd)
774 ipfw_obj_lheader *olh;
775 struct nat44_cfg_nat *ucfg;
779 /* Check minimum header size */
780 if (sd->valsize < sizeof(ipfw_obj_lheader))
783 olh = (ipfw_obj_lheader *)ipfw_get_sopt_header(sd, sizeof(*olh));
784 IPFW_UH_RLOCK(chain);
786 LIST_FOREACH(ptr, &chain->nat, _next)
789 olh->count = nat_count;
790 olh->objsize = sizeof(struct nat44_cfg_nat);
791 olh->size = sizeof(*olh) + olh->count * olh->objsize;
793 if (sd->valsize < olh->size) {
794 IPFW_UH_RUNLOCK(chain);
798 LIST_FOREACH(ptr, &chain->nat, _next) {
799 ucfg = (struct nat44_cfg_nat *)ipfw_get_sopt_space(sd,
801 export_nat_cfg(ptr, ucfg);
804 IPFW_UH_RUNLOCK(chain);
810 * Gets log for given nat instance
811 * Data layout (v0)(current):
812 * Request: [ ipfw_obj_header nat44_cfg_nat ]
813 * Reply: [ ipfw_obj_header nat44_cfg_nat LOGBUFFER ]
815 * Returns 0 on success
818 nat44_get_log(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
819 struct sockopt_data *sd)
822 struct nat44_cfg_nat *ucfg;
827 sz = sizeof(*oh) + sizeof(*ucfg);
828 /* Check minimum header size */
829 if (sd->valsize < sz)
832 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
834 /* Basic length checks for TLVs */
835 if (oh->ntlv.head.length != sizeof(oh->ntlv))
838 ucfg = (struct nat44_cfg_nat *)(oh + 1);
840 /* Check if name is properly terminated */
841 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name))
844 IPFW_UH_RLOCK(chain);
845 ptr = lookup_nat_name(&chain->nat, ucfg->name);
847 IPFW_UH_RUNLOCK(chain);
851 if (ptr->lib->logDesc == NULL) {
852 IPFW_UH_RUNLOCK(chain);
856 export_nat_cfg(ptr, ucfg);
858 /* Estimate memory amount */
859 ucfg->size = sizeof(struct nat44_cfg_nat) + LIBALIAS_BUF_SIZE;
860 if (sd->valsize < sz + sizeof(*oh)) {
863 * Submitted buffer size is not enough.
864 * WE've already filled in @ucfg structure with
865 * relevant info including size, so we
866 * can return. Buffer will be flushed automatically.
868 IPFW_UH_RUNLOCK(chain);
872 pbuf = (void *)ipfw_get_sopt_space(sd, LIBALIAS_BUF_SIZE);
873 memcpy(pbuf, ptr->lib->logDesc, LIBALIAS_BUF_SIZE);
875 IPFW_UH_RUNLOCK(chain);
880 static struct ipfw_sopt_handler scodes[] = {
881 { IP_FW_NAT44_XCONFIG, 0, HDIR_SET, nat44_cfg },
882 { IP_FW_NAT44_DESTROY, 0, HDIR_SET, nat44_destroy },
883 { IP_FW_NAT44_XGETCONFIG, 0, HDIR_GET, nat44_get_cfg },
884 { IP_FW_NAT44_LIST_NAT, 0, HDIR_GET, nat44_list_nat },
885 { IP_FW_NAT44_XGETLOG, 0, HDIR_GET, nat44_get_log },
890 * Legacy configuration routines
893 struct cfg_spool_legacy {
894 LIST_ENTRY(cfg_spool_legacy) _next;
899 struct cfg_redir_legacy {
900 LIST_ENTRY(cfg_redir) _next;
902 struct in_addr laddr;
903 struct in_addr paddr;
904 struct in_addr raddr;
911 struct alias_link **alink;
913 LIST_HEAD(, cfg_spool_legacy) spool_chain;
916 struct cfg_nat_legacy {
917 LIST_ENTRY(cfg_nat_legacy) _next;
920 char if_name[IF_NAMESIZE];
922 struct libalias *lib;
924 LIST_HEAD(, cfg_redir_legacy) redir_chain;
928 ipfw_nat_cfg(struct sockopt *sopt)
930 struct cfg_nat_legacy *cfg;
931 struct nat44_cfg_nat *ucfg;
932 struct cfg_redir_legacy *rdir;
933 struct nat44_cfg_redir *urdir;
938 len = sopt->sopt_valsize;
942 * Allocate 2x buffer to store converted structures.
943 * new redir_cfg has shrunk, so we're sure that
944 * new buffer size is enough.
946 buf = malloc(roundup2(len, 8) + len2, M_TEMP, M_WAITOK | M_ZERO);
947 error = sooptcopyin(sopt, buf, len, sizeof(struct cfg_nat_legacy));
951 cfg = (struct cfg_nat_legacy *)buf;
957 ucfg = (struct nat44_cfg_nat *)&buf[roundup2(len, 8)];
958 snprintf(ucfg->name, sizeof(ucfg->name), "%d", cfg->id);
959 strlcpy(ucfg->if_name, cfg->if_name, sizeof(ucfg->if_name));
961 ucfg->mode = cfg->mode;
962 ucfg->redir_cnt = cfg->redir_cnt;
964 if (len < sizeof(*cfg) + cfg->redir_cnt * sizeof(*rdir)) {
969 urdir = (struct nat44_cfg_redir *)(ucfg + 1);
970 rdir = (struct cfg_redir_legacy *)(cfg + 1);
971 for (i = 0; i < cfg->redir_cnt; i++) {
972 urdir->mode = rdir->mode;
973 urdir->laddr = rdir->laddr;
974 urdir->paddr = rdir->paddr;
975 urdir->raddr = rdir->raddr;
976 urdir->lport = rdir->lport;
977 urdir->pport = rdir->pport;
978 urdir->rport = rdir->rport;
979 urdir->pport_cnt = rdir->pport_cnt;
980 urdir->rport_cnt = rdir->rport_cnt;
981 urdir->proto = rdir->proto;
982 urdir->spool_cnt = rdir->spool_cnt;
988 nat44_config(&V_layer3_chain, ucfg);
996 ipfw_nat_del(struct sockopt *sopt)
999 struct ip_fw_chain *chain = &V_layer3_chain;
1002 sooptcopyin(sopt, &i, sizeof i, sizeof i);
1003 /* XXX validate i */
1004 IPFW_UH_WLOCK(chain);
1005 ptr = lookup_nat(&chain->nat, i);
1007 IPFW_UH_WUNLOCK(chain);
1011 LIST_REMOVE(ptr, _next);
1012 flush_nat_ptrs(chain, i);
1013 IPFW_WUNLOCK(chain);
1014 IPFW_UH_WUNLOCK(chain);
1015 free_nat_instance(ptr);
1020 ipfw_nat_get_cfg(struct sockopt *sopt)
1022 struct ip_fw_chain *chain = &V_layer3_chain;
1024 struct cfg_nat_legacy *ucfg;
1025 struct cfg_redir *r;
1026 struct cfg_spool *s;
1027 struct cfg_redir_legacy *ser_r;
1028 struct cfg_spool_legacy *ser_s;
1030 int gencnt, nat_cnt, len, error;
1033 len = sizeof(nat_cnt);
1035 IPFW_UH_RLOCK(chain);
1037 gencnt = chain->gencnt;
1038 /* Estimate memory amount */
1039 LIST_FOREACH(n, &chain->nat, _next) {
1041 len += sizeof(struct cfg_nat_legacy);
1042 LIST_FOREACH(r, &n->redir_chain, _next) {
1043 len += sizeof(struct cfg_redir_legacy);
1044 LIST_FOREACH(s, &r->spool_chain, _next)
1045 len += sizeof(struct cfg_spool_legacy);
1048 IPFW_UH_RUNLOCK(chain);
1050 data = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
1051 bcopy(&nat_cnt, data, sizeof(nat_cnt));
1054 len = sizeof(nat_cnt);
1056 IPFW_UH_RLOCK(chain);
1057 if (gencnt != chain->gencnt) {
1061 /* Serialize all the data. */
1062 LIST_FOREACH(n, &chain->nat, _next) {
1063 ucfg = (struct cfg_nat_legacy *)&data[len];
1066 ucfg->redir_cnt = n->redir_cnt;
1067 ucfg->mode = n->mode;
1068 strlcpy(ucfg->if_name, n->if_name, sizeof(ucfg->if_name));
1069 len += sizeof(struct cfg_nat_legacy);
1070 LIST_FOREACH(r, &n->redir_chain, _next) {
1071 ser_r = (struct cfg_redir_legacy *)&data[len];
1072 ser_r->mode = r->mode;
1073 ser_r->laddr = r->laddr;
1074 ser_r->paddr = r->paddr;
1075 ser_r->raddr = r->raddr;
1076 ser_r->lport = r->lport;
1077 ser_r->pport = r->pport;
1078 ser_r->rport = r->rport;
1079 ser_r->pport_cnt = r->pport_cnt;
1080 ser_r->rport_cnt = r->rport_cnt;
1081 ser_r->proto = r->proto;
1082 ser_r->spool_cnt = r->spool_cnt;
1083 len += sizeof(struct cfg_redir_legacy);
1084 LIST_FOREACH(s, &r->spool_chain, _next) {
1085 ser_s = (struct cfg_spool_legacy *)&data[len];
1086 ser_s->addr = s->addr;
1087 ser_s->port = s->port;
1088 len += sizeof(struct cfg_spool_legacy);
1092 IPFW_UH_RUNLOCK(chain);
1094 error = sooptcopyout(sopt, data, len);
1101 ipfw_nat_get_log(struct sockopt *sopt)
1104 struct cfg_nat *ptr;
1106 struct ip_fw_chain *chain;
1109 chain = &V_layer3_chain;
1112 /* one pass to count, one to copy the data */
1114 LIST_FOREACH(ptr, &chain->nat, _next) {
1115 if (ptr->lib->logDesc == NULL)
1119 size = i * (LIBALIAS_BUF_SIZE + sizeof(int));
1120 data = malloc(size, M_IPFW, M_NOWAIT | M_ZERO);
1122 IPFW_RUNLOCK(chain);
1126 LIST_FOREACH(ptr, &chain->nat, _next) {
1127 if (ptr->lib->logDesc == NULL)
1129 bcopy(&ptr->id, &data[i], sizeof(int));
1131 bcopy(ptr->lib->logDesc, &data[i], LIBALIAS_BUF_SIZE);
1132 i += LIBALIAS_BUF_SIZE;
1134 IPFW_RUNLOCK(chain);
1135 sooptcopyout(sopt, data, size);
1141 vnet_ipfw_nat_init(const void *arg __unused)
1144 V_ipfw_nat_ready = 1;
1149 vnet_ipfw_nat_uninit(const void *arg __unused)
1151 struct cfg_nat *ptr, *ptr_temp;
1152 struct ip_fw_chain *chain;
1154 chain = &V_layer3_chain;
1156 V_ipfw_nat_ready = 0;
1157 LIST_FOREACH_SAFE(ptr, &chain->nat, _next, ptr_temp) {
1158 LIST_REMOVE(ptr, _next);
1159 free_nat_instance(ptr);
1161 flush_nat_ptrs(chain, -1 /* flush all */);
1162 IPFW_WUNLOCK(chain);
1170 /* init ipfw hooks */
1171 ipfw_nat_ptr = ipfw_nat;
1172 lookup_nat_ptr = lookup_nat;
1173 ipfw_nat_cfg_ptr = ipfw_nat_cfg;
1174 ipfw_nat_del_ptr = ipfw_nat_del;
1175 ipfw_nat_get_cfg_ptr = ipfw_nat_get_cfg;
1176 ipfw_nat_get_log_ptr = ipfw_nat_get_log;
1177 IPFW_ADD_SOPT_HANDLER(1, scodes);
1179 ifaddr_event_tag = EVENTHANDLER_REGISTER(ifaddr_event, ifaddr_change,
1180 NULL, EVENTHANDLER_PRI_ANY);
1184 ipfw_nat_destroy(void)
1187 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_event_tag);
1188 /* deregister ipfw_nat */
1189 IPFW_DEL_SOPT_HANDLER(1, scodes);
1190 ipfw_nat_ptr = NULL;
1191 lookup_nat_ptr = NULL;
1192 ipfw_nat_cfg_ptr = NULL;
1193 ipfw_nat_del_ptr = NULL;
1194 ipfw_nat_get_cfg_ptr = NULL;
1195 ipfw_nat_get_log_ptr = NULL;
1199 ipfw_nat_modevent(module_t mod, int type, void *unused)
1217 static moduledata_t ipfw_nat_mod = {
1223 /* Define startup order. */
1224 #define IPFW_NAT_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL
1225 #define IPFW_NAT_MODEVENT_ORDER (SI_ORDER_ANY - 128) /* after ipfw */
1226 #define IPFW_NAT_MODULE_ORDER (IPFW_NAT_MODEVENT_ORDER + 1)
1227 #define IPFW_NAT_VNET_ORDER (IPFW_NAT_MODEVENT_ORDER + 2)
1229 DECLARE_MODULE(ipfw_nat, ipfw_nat_mod, IPFW_NAT_SI_SUB_FIREWALL, SI_ORDER_ANY);
1230 MODULE_DEPEND(ipfw_nat, libalias, 1, 1, 1);
1231 MODULE_DEPEND(ipfw_nat, ipfw, 3, 3, 3);
1232 MODULE_VERSION(ipfw_nat, 1);
1234 SYSINIT(ipfw_nat_init, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_MODULE_ORDER,
1235 ipfw_nat_init, NULL);
1236 VNET_SYSINIT(vnet_ipfw_nat_init, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_VNET_ORDER,
1237 vnet_ipfw_nat_init, NULL);
1239 SYSUNINIT(ipfw_nat_destroy, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_MODULE_ORDER,
1240 ipfw_nat_destroy, NULL);
1241 VNET_SYSUNINIT(vnet_ipfw_nat_uninit, IPFW_NAT_SI_SUB_FIREWALL,
1242 IPFW_NAT_VNET_ORDER, vnet_ipfw_nat_uninit, NULL);