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 /* ...using nic 'ifp->if_xname' as dynamic alias address. */
118 if (strncmp(ptr->if_name, ifp->if_xname, IF_NAMESIZE) != 0)
121 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
122 if (ifa->ifa_addr == NULL)
124 if (ifa->ifa_addr->sa_family != AF_INET)
127 ptr->ip = ((struct sockaddr_in *)
128 (ifa->ifa_addr))->sin_addr;
129 LibAliasSetAddress(ptr->lib, ptr->ip);
132 if_addr_runlock(ifp);
134 IPFW_UH_WUNLOCK(chain);
138 * delete the pointers for nat entry ix, or all of them if ix < 0
141 flush_nat_ptrs(struct ip_fw_chain *chain, const int ix)
146 IPFW_WLOCK_ASSERT(chain);
147 for (i = 0; i < chain->n_rules; i++) {
148 cmd = (ipfw_insn_nat *)ipfw_get_action(chain->map[i]);
149 if (cmd->o.opcode == O_NAT && cmd->nat != NULL &&
150 (ix < 0 || cmd->nat->id == ix))
156 del_redir_spool_cfg(struct cfg_nat *n, struct redir_chain *head)
158 struct cfg_redir *r, *tmp_r;
159 struct cfg_spool *s, *tmp_s;
162 LIST_FOREACH_SAFE(r, head, _next, tmp_r) {
163 num = 1; /* Number of alias_link to delete. */
165 case NAT44_REDIR_PORT:
168 case NAT44_REDIR_ADDR:
169 case NAT44_REDIR_PROTO:
170 /* Delete all libalias redirect entry. */
171 for (i = 0; i < num; i++)
172 LibAliasRedirectDelete(n->lib, r->alink[i]);
173 /* Del spool cfg if any. */
174 LIST_FOREACH_SAFE(s, &r->spool_chain, _next, tmp_s) {
175 LIST_REMOVE(s, _next);
178 free(r->alink, M_IPFW);
179 LIST_REMOVE(r, _next);
183 printf("unknown redirect mode: %u\n", r->mode);
184 /* XXX - panic?!?!? */
191 add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
195 struct nat44_cfg_redir *ser_r;
196 struct nat44_cfg_spool *ser_s;
200 for (cnt = 0, off = 0; cnt < ptr->redir_cnt; cnt++) {
201 ser_r = (struct nat44_cfg_redir *)&buf[off];
202 r = malloc(sizeof(*r), M_IPFW, M_WAITOK | M_ZERO);
203 r->mode = ser_r->mode;
204 r->laddr = ser_r->laddr;
205 r->paddr = ser_r->paddr;
206 r->raddr = ser_r->raddr;
207 r->lport = ser_r->lport;
208 r->pport = ser_r->pport;
209 r->rport = ser_r->rport;
210 r->pport_cnt = ser_r->pport_cnt;
211 r->rport_cnt = ser_r->rport_cnt;
212 r->proto = ser_r->proto;
213 r->spool_cnt = ser_r->spool_cnt;
214 //memcpy(r, ser_r, SOF_REDIR);
215 LIST_INIT(&r->spool_chain);
216 off += sizeof(struct nat44_cfg_redir);
217 r->alink = malloc(sizeof(struct alias_link *) * r->pport_cnt,
218 M_IPFW, M_WAITOK | M_ZERO);
220 case NAT44_REDIR_ADDR:
221 r->alink[0] = LibAliasRedirectAddr(ptr->lib, r->laddr,
224 case NAT44_REDIR_PORT:
225 for (i = 0 ; i < r->pport_cnt; i++) {
226 /* If remotePort is all ports, set it to 0. */
227 u_short remotePortCopy = r->rport + i;
228 if (r->rport_cnt == 1 && r->rport == 0)
230 r->alink[i] = LibAliasRedirectPort(ptr->lib,
231 r->laddr, htons(r->lport + i), r->raddr,
232 htons(remotePortCopy), r->paddr,
233 htons(r->pport + i), r->proto);
234 if (r->alink[i] == NULL) {
240 case NAT44_REDIR_PROTO:
241 r->alink[0] = LibAliasRedirectProto(ptr->lib ,r->laddr,
242 r->raddr, r->paddr, r->proto);
245 printf("unknown redirect mode: %u\n", r->mode);
248 if (r->alink[0] == NULL) {
249 printf("LibAliasRedirect* returned NULL\n");
250 free(r->alink, M_IPFW);
254 /* LSNAT handling. */
255 for (i = 0; i < r->spool_cnt; i++) {
256 ser_s = (struct nat44_cfg_spool *)&buf[off];
257 s = malloc(sizeof(*s), M_IPFW, M_WAITOK | M_ZERO);
258 s->addr = ser_s->addr;
259 s->port = ser_s->port;
260 LibAliasAddServer(ptr->lib, r->alink[0],
261 s->addr, htons(s->port));
262 off += sizeof(struct nat44_cfg_spool);
263 /* Hook spool entry. */
264 LIST_INSERT_HEAD(&r->spool_chain, s, _next);
266 /* And finally hook this redir entry. */
267 LIST_INSERT_HEAD(&ptr->redir_chain, r, _next);
274 free_nat_instance(struct cfg_nat *ptr)
277 del_redir_spool_cfg(ptr, &ptr->redir_chain);
278 LibAliasUninit(ptr->lib);
284 * ipfw_nat - perform mbuf header translation.
286 * Note V_layer3_chain has to be locked while calling ipfw_nat() in
287 * 'global' operation mode (t == NULL).
291 ipfw_nat(struct ip_fw_args *args, struct cfg_nat *t, struct mbuf *m)
295 /* XXX - libalias duct tape */
296 int ldt, retval, found;
297 struct ip_fw_chain *chain;
302 mcl = m_megapullup(m, m->m_pkthdr.len);
307 ip = mtod(mcl, struct ip *);
310 * XXX - Libalias checksum offload 'duct tape':
312 * locally generated packets have only pseudo-header checksum
313 * calculated and libalias will break it[1], so mark them for
314 * later fix. Moreover there are cases when libalias modifies
315 * tcp packet data[2], mark them for later fix too.
317 * [1] libalias was never meant to run in kernel, so it does
318 * not have any knowledge about checksum offloading, and
319 * expects a packet with a full internet checksum.
320 * Unfortunately, packets generated locally will have just the
321 * pseudo header calculated, and when libalias tries to adjust
322 * the checksum it will actually compute a wrong value.
324 * [2] when libalias modifies tcp's data content, full TCP
325 * checksum has to be recomputed: the problem is that
326 * libalias does not have any idea about checksum offloading.
327 * To work around this, we do not do checksumming in LibAlias,
328 * but only mark the packets in th_x2 field. If we receive a
329 * marked packet, we calculate correct checksum for it
330 * aware of offloading. Why such a terrible hack instead of
331 * recalculating checksum for each packet?
332 * Because the previous checksum was not checked!
333 * Recalculating checksums for EVERY packet will hide ALL
334 * transmission errors. Yes, marked packets still suffer from
335 * this problem. But, sigh, natd(8) has this problem, too.
337 * TODO: -make libalias mbuf aware (so
338 * it can handle delayed checksum and tso)
341 if (mcl->m_pkthdr.rcvif == NULL &&
342 mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
345 c = mtod(mcl, char *);
347 /* Check if this is 'global' instance */
349 if (args->flags & IPFW_ARGS_IN) {
350 /* Wrong direction, skip processing */
356 chain = &V_layer3_chain;
357 IPFW_RLOCK_ASSERT(chain);
358 /* Check every nat entry... */
359 LIST_FOREACH(t, &chain->nat, _next) {
360 if ((t->mode & PKT_ALIAS_SKIP_GLOBAL) != 0)
362 retval = LibAliasOutTry(t->lib, c,
363 mcl->m_len + M_TRAILINGSPACE(mcl), 0);
364 if (retval == PKT_ALIAS_OK) {
365 /* Nat instance recognises state */
371 /* No instance found, return ignore */
376 if (args->flags & IPFW_ARGS_IN)
377 retval = LibAliasIn(t->lib, c,
378 mcl->m_len + M_TRAILINGSPACE(mcl));
380 retval = LibAliasOut(t->lib, c,
381 mcl->m_len + M_TRAILINGSPACE(mcl));
385 * We drop packet when:
386 * 1. libalias returns PKT_ALIAS_ERROR;
387 * 2. For incoming packets:
388 * a) for unresolved fragments;
389 * b) libalias returns PKT_ALIAS_IGNORED and
390 * PKT_ALIAS_DENY_INCOMING flag is set.
392 if (retval == PKT_ALIAS_ERROR ||
393 ((args->flags & IPFW_ARGS_IN) &&
394 (retval == PKT_ALIAS_UNRESOLVED_FRAGMENT ||
395 (retval == PKT_ALIAS_IGNORED &&
396 (t->mode & PKT_ALIAS_DENY_INCOMING) != 0)))) {
397 /* XXX - should i add some logging? */
403 if (retval == PKT_ALIAS_RESPOND)
404 mcl->m_flags |= M_SKIP_FIREWALL;
405 mcl->m_pkthdr.len = mcl->m_len = ntohs(ip->ip_len);
408 * XXX - libalias checksum offload
409 * 'duct tape' (see above)
412 if ((ip->ip_off & htons(IP_OFFMASK)) == 0 &&
413 ip->ip_p == IPPROTO_TCP) {
416 th = (struct tcphdr *)(ip + 1);
424 uint16_t ip_len, cksum;
426 ip_len = ntohs(ip->ip_len);
427 cksum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
428 htons(ip->ip_p + ip_len - (ip->ip_hl << 2)));
432 th = (struct tcphdr *)(ip + 1);
434 * Maybe it was set in
439 mcl->m_pkthdr.csum_data =
440 offsetof(struct tcphdr, th_sum);
443 uh = (struct udphdr *)(ip + 1);
445 mcl->m_pkthdr.csum_data =
446 offsetof(struct udphdr, uh_sum);
449 /* No hw checksum offloading: do it ourselves */
450 if ((mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA) == 0) {
451 in_delayed_cksum(mcl);
452 mcl->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
459 static struct cfg_nat *
460 lookup_nat(struct nat_list *l, int nat_id)
464 LIST_FOREACH(res, l, _next) {
465 if (res->id == nat_id)
471 static struct cfg_nat *
472 lookup_nat_name(struct nat_list *l, char *name)
478 id = strtol(name, &errptr, 10);
479 if (id == 0 || *errptr != '\0')
482 LIST_FOREACH(res, l, _next) {
489 /* IP_FW3 configuration routines */
492 nat44_config(struct ip_fw_chain *chain, struct nat44_cfg_nat *ucfg)
494 struct cfg_nat *ptr, *tcfg;
498 * Find/create nat rule.
500 IPFW_UH_WLOCK(chain);
501 gencnt = chain->gencnt;
502 ptr = lookup_nat_name(&chain->nat, ucfg->name);
504 IPFW_UH_WUNLOCK(chain);
505 /* New rule: allocate and init new instance. */
506 ptr = malloc(sizeof(struct cfg_nat), M_IPFW, M_WAITOK | M_ZERO);
507 ptr->lib = LibAliasInit(NULL);
508 LIST_INIT(&ptr->redir_chain);
510 /* Entry already present: temporarily unhook it. */
512 LIST_REMOVE(ptr, _next);
513 flush_nat_ptrs(chain, ptr->id);
515 IPFW_UH_WUNLOCK(chain);
519 * Basic nat (re)configuration.
521 ptr->id = strtol(ucfg->name, NULL, 10);
523 * XXX - what if this rule doesn't nat any ip and just
525 * do we set aliasaddress to 0.0.0.0?
528 ptr->redir_cnt = ucfg->redir_cnt;
529 ptr->mode = ucfg->mode;
530 strlcpy(ptr->if_name, ucfg->if_name, sizeof(ptr->if_name));
531 LibAliasSetMode(ptr->lib, ptr->mode, ~0);
532 LibAliasSetAddress(ptr->lib, ptr->ip);
535 * Redir and LSNAT configuration.
537 /* Delete old cfgs. */
538 del_redir_spool_cfg(ptr, &ptr->redir_chain);
539 /* Add new entries. */
540 add_redir_spool_cfg((char *)(ucfg + 1), ptr);
541 IPFW_UH_WLOCK(chain);
543 /* Extra check to avoid race with another ipfw_nat_cfg() */
545 if (gencnt != chain->gencnt)
546 tcfg = lookup_nat_name(&chain->nat, ucfg->name);
549 LIST_REMOVE(tcfg, _next);
550 LIST_INSERT_HEAD(&chain->nat, ptr, _next);
554 IPFW_UH_WUNLOCK(chain);
557 free_nat_instance(ptr);
561 * Creates/configure nat44 instance
562 * Data layout (v0)(current):
563 * Request: [ ipfw_obj_header nat44_cfg_nat .. ]
565 * Returns 0 on success
568 nat44_cfg(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
569 struct sockopt_data *sd)
572 struct nat44_cfg_nat *ucfg;
577 /* Check minimum header size */
578 if (sd->valsize < (sizeof(*oh) + sizeof(*ucfg)))
581 oh = (ipfw_obj_header *)sd->kbuf;
583 /* Basic length checks for TLVs */
584 if (oh->ntlv.head.length != sizeof(oh->ntlv))
587 ucfg = (struct nat44_cfg_nat *)(oh + 1);
589 /* Check if name is properly terminated and looks like number */
590 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name))
592 id = strtol(ucfg->name, &errptr, 10);
593 if (id == 0 || *errptr != '\0')
596 read = sizeof(*oh) + sizeof(*ucfg);
597 /* Check number of redirs */
598 if (sd->valsize < read + ucfg->redir_cnt*sizeof(struct nat44_cfg_redir))
601 nat44_config(chain, ucfg);
606 * Destroys given nat instances.
607 * Data layout (v0)(current):
608 * Request: [ ipfw_obj_header ]
610 * Returns 0 on success
613 nat44_destroy(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
614 struct sockopt_data *sd)
620 /* Check minimum header size */
621 if (sd->valsize < sizeof(*oh))
624 oh = (ipfw_obj_header *)sd->kbuf;
626 /* Basic length checks for TLVs */
627 if (oh->ntlv.head.length != sizeof(oh->ntlv))
631 /* Check if name is properly terminated */
632 if (strnlen(ntlv->name, sizeof(ntlv->name)) == sizeof(ntlv->name))
635 IPFW_UH_WLOCK(chain);
636 ptr = lookup_nat_name(&chain->nat, ntlv->name);
638 IPFW_UH_WUNLOCK(chain);
642 LIST_REMOVE(ptr, _next);
643 flush_nat_ptrs(chain, ptr->id);
645 IPFW_UH_WUNLOCK(chain);
647 free_nat_instance(ptr);
653 export_nat_cfg(struct cfg_nat *ptr, struct nat44_cfg_nat *ucfg)
656 snprintf(ucfg->name, sizeof(ucfg->name), "%d", ptr->id);
658 ucfg->redir_cnt = ptr->redir_cnt;
659 ucfg->mode = ptr->mode;
660 strlcpy(ucfg->if_name, ptr->if_name, sizeof(ucfg->if_name));
664 * Gets config for given nat instance
665 * Data layout (v0)(current):
666 * Request: [ ipfw_obj_header nat44_cfg_nat .. ]
668 * Returns 0 on success
671 nat44_get_cfg(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
672 struct sockopt_data *sd)
675 struct nat44_cfg_nat *ucfg;
679 struct nat44_cfg_redir *ser_r;
680 struct nat44_cfg_spool *ser_s;
683 sz = sizeof(*oh) + sizeof(*ucfg);
684 /* Check minimum header size */
685 if (sd->valsize < sz)
688 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
690 /* Basic length checks for TLVs */
691 if (oh->ntlv.head.length != sizeof(oh->ntlv))
694 ucfg = (struct nat44_cfg_nat *)(oh + 1);
696 /* Check if name is properly terminated */
697 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name))
700 IPFW_UH_RLOCK(chain);
701 ptr = lookup_nat_name(&chain->nat, ucfg->name);
703 IPFW_UH_RUNLOCK(chain);
707 export_nat_cfg(ptr, ucfg);
709 /* Estimate memory amount */
710 sz = sizeof(ipfw_obj_header) + sizeof(struct nat44_cfg_nat);
711 LIST_FOREACH(r, &ptr->redir_chain, _next) {
712 sz += sizeof(struct nat44_cfg_redir);
713 LIST_FOREACH(s, &r->spool_chain, _next)
714 sz += sizeof(struct nat44_cfg_spool);
718 if (sd->valsize < sz) {
721 * Submitted buffer size is not enough.
722 * WE've already filled in @ucfg structure with
723 * relevant info including size, so we
724 * can return. Buffer will be flushed automatically.
726 IPFW_UH_RUNLOCK(chain);
730 /* Size OK, let's copy data */
731 LIST_FOREACH(r, &ptr->redir_chain, _next) {
732 ser_r = (struct nat44_cfg_redir *)ipfw_get_sopt_space(sd,
734 ser_r->mode = r->mode;
735 ser_r->laddr = r->laddr;
736 ser_r->paddr = r->paddr;
737 ser_r->raddr = r->raddr;
738 ser_r->lport = r->lport;
739 ser_r->pport = r->pport;
740 ser_r->rport = r->rport;
741 ser_r->pport_cnt = r->pport_cnt;
742 ser_r->rport_cnt = r->rport_cnt;
743 ser_r->proto = r->proto;
744 ser_r->spool_cnt = r->spool_cnt;
746 LIST_FOREACH(s, &r->spool_chain, _next) {
747 ser_s = (struct nat44_cfg_spool *)ipfw_get_sopt_space(
750 ser_s->addr = s->addr;
751 ser_s->port = s->port;
755 IPFW_UH_RUNLOCK(chain);
761 * Lists all nat44 instances currently available in kernel.
762 * Data layout (v0)(current):
763 * Request: [ ipfw_obj_lheader ]
764 * Reply: [ ipfw_obj_lheader nat44_cfg_nat x N ]
766 * Returns 0 on success
769 nat44_list_nat(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
770 struct sockopt_data *sd)
772 ipfw_obj_lheader *olh;
773 struct nat44_cfg_nat *ucfg;
777 /* Check minimum header size */
778 if (sd->valsize < sizeof(ipfw_obj_lheader))
781 olh = (ipfw_obj_lheader *)ipfw_get_sopt_header(sd, sizeof(*olh));
782 IPFW_UH_RLOCK(chain);
784 LIST_FOREACH(ptr, &chain->nat, _next)
787 olh->count = nat_count;
788 olh->objsize = sizeof(struct nat44_cfg_nat);
789 olh->size = sizeof(*olh) + olh->count * olh->objsize;
791 if (sd->valsize < olh->size) {
792 IPFW_UH_RUNLOCK(chain);
796 LIST_FOREACH(ptr, &chain->nat, _next) {
797 ucfg = (struct nat44_cfg_nat *)ipfw_get_sopt_space(sd,
799 export_nat_cfg(ptr, ucfg);
802 IPFW_UH_RUNLOCK(chain);
808 * Gets log for given nat instance
809 * Data layout (v0)(current):
810 * Request: [ ipfw_obj_header nat44_cfg_nat ]
811 * Reply: [ ipfw_obj_header nat44_cfg_nat LOGBUFFER ]
813 * Returns 0 on success
816 nat44_get_log(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
817 struct sockopt_data *sd)
820 struct nat44_cfg_nat *ucfg;
825 sz = sizeof(*oh) + sizeof(*ucfg);
826 /* Check minimum header size */
827 if (sd->valsize < sz)
830 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
832 /* Basic length checks for TLVs */
833 if (oh->ntlv.head.length != sizeof(oh->ntlv))
836 ucfg = (struct nat44_cfg_nat *)(oh + 1);
838 /* Check if name is properly terminated */
839 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name))
842 IPFW_UH_RLOCK(chain);
843 ptr = lookup_nat_name(&chain->nat, ucfg->name);
845 IPFW_UH_RUNLOCK(chain);
849 if (ptr->lib->logDesc == NULL) {
850 IPFW_UH_RUNLOCK(chain);
854 export_nat_cfg(ptr, ucfg);
856 /* Estimate memory amount */
857 ucfg->size = sizeof(struct nat44_cfg_nat) + LIBALIAS_BUF_SIZE;
858 if (sd->valsize < sz + sizeof(*oh)) {
861 * Submitted buffer size is not enough.
862 * WE've already filled in @ucfg structure with
863 * relevant info including size, so we
864 * can return. Buffer will be flushed automatically.
866 IPFW_UH_RUNLOCK(chain);
870 pbuf = (void *)ipfw_get_sopt_space(sd, LIBALIAS_BUF_SIZE);
871 memcpy(pbuf, ptr->lib->logDesc, LIBALIAS_BUF_SIZE);
873 IPFW_UH_RUNLOCK(chain);
878 static struct ipfw_sopt_handler scodes[] = {
879 { IP_FW_NAT44_XCONFIG, 0, HDIR_SET, nat44_cfg },
880 { IP_FW_NAT44_DESTROY, 0, HDIR_SET, nat44_destroy },
881 { IP_FW_NAT44_XGETCONFIG, 0, HDIR_GET, nat44_get_cfg },
882 { IP_FW_NAT44_LIST_NAT, 0, HDIR_GET, nat44_list_nat },
883 { IP_FW_NAT44_XGETLOG, 0, HDIR_GET, nat44_get_log },
888 * Legacy configuration routines
891 struct cfg_spool_legacy {
892 LIST_ENTRY(cfg_spool_legacy) _next;
897 struct cfg_redir_legacy {
898 LIST_ENTRY(cfg_redir) _next;
900 struct in_addr laddr;
901 struct in_addr paddr;
902 struct in_addr raddr;
909 struct alias_link **alink;
911 LIST_HEAD(, cfg_spool_legacy) spool_chain;
914 struct cfg_nat_legacy {
915 LIST_ENTRY(cfg_nat_legacy) _next;
918 char if_name[IF_NAMESIZE];
920 struct libalias *lib;
922 LIST_HEAD(, cfg_redir_legacy) redir_chain;
926 ipfw_nat_cfg(struct sockopt *sopt)
928 struct cfg_nat_legacy *cfg;
929 struct nat44_cfg_nat *ucfg;
930 struct cfg_redir_legacy *rdir;
931 struct nat44_cfg_redir *urdir;
936 len = sopt->sopt_valsize;
940 * Allocate 2x buffer to store converted structures.
941 * new redir_cfg has shrunk, so we're sure that
942 * new buffer size is enough.
944 buf = malloc(roundup2(len, 8) + len2, M_TEMP, M_WAITOK | M_ZERO);
945 error = sooptcopyin(sopt, buf, len, sizeof(struct cfg_nat_legacy));
949 cfg = (struct cfg_nat_legacy *)buf;
955 ucfg = (struct nat44_cfg_nat *)&buf[roundup2(len, 8)];
956 snprintf(ucfg->name, sizeof(ucfg->name), "%d", cfg->id);
957 strlcpy(ucfg->if_name, cfg->if_name, sizeof(ucfg->if_name));
959 ucfg->mode = cfg->mode;
960 ucfg->redir_cnt = cfg->redir_cnt;
962 if (len < sizeof(*cfg) + cfg->redir_cnt * sizeof(*rdir)) {
967 urdir = (struct nat44_cfg_redir *)(ucfg + 1);
968 rdir = (struct cfg_redir_legacy *)(cfg + 1);
969 for (i = 0; i < cfg->redir_cnt; i++) {
970 urdir->mode = rdir->mode;
971 urdir->laddr = rdir->laddr;
972 urdir->paddr = rdir->paddr;
973 urdir->raddr = rdir->raddr;
974 urdir->lport = rdir->lport;
975 urdir->pport = rdir->pport;
976 urdir->rport = rdir->rport;
977 urdir->pport_cnt = rdir->pport_cnt;
978 urdir->rport_cnt = rdir->rport_cnt;
979 urdir->proto = rdir->proto;
980 urdir->spool_cnt = rdir->spool_cnt;
986 nat44_config(&V_layer3_chain, ucfg);
994 ipfw_nat_del(struct sockopt *sopt)
997 struct ip_fw_chain *chain = &V_layer3_chain;
1000 sooptcopyin(sopt, &i, sizeof i, sizeof i);
1001 /* XXX validate i */
1002 IPFW_UH_WLOCK(chain);
1003 ptr = lookup_nat(&chain->nat, i);
1005 IPFW_UH_WUNLOCK(chain);
1009 LIST_REMOVE(ptr, _next);
1010 flush_nat_ptrs(chain, i);
1011 IPFW_WUNLOCK(chain);
1012 IPFW_UH_WUNLOCK(chain);
1013 free_nat_instance(ptr);
1018 ipfw_nat_get_cfg(struct sockopt *sopt)
1020 struct ip_fw_chain *chain = &V_layer3_chain;
1022 struct cfg_nat_legacy *ucfg;
1023 struct cfg_redir *r;
1024 struct cfg_spool *s;
1025 struct cfg_redir_legacy *ser_r;
1026 struct cfg_spool_legacy *ser_s;
1028 int gencnt, nat_cnt, len, error;
1031 len = sizeof(nat_cnt);
1033 IPFW_UH_RLOCK(chain);
1035 gencnt = chain->gencnt;
1036 /* Estimate memory amount */
1037 LIST_FOREACH(n, &chain->nat, _next) {
1039 len += sizeof(struct cfg_nat_legacy);
1040 LIST_FOREACH(r, &n->redir_chain, _next) {
1041 len += sizeof(struct cfg_redir_legacy);
1042 LIST_FOREACH(s, &r->spool_chain, _next)
1043 len += sizeof(struct cfg_spool_legacy);
1046 IPFW_UH_RUNLOCK(chain);
1048 data = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
1049 bcopy(&nat_cnt, data, sizeof(nat_cnt));
1052 len = sizeof(nat_cnt);
1054 IPFW_UH_RLOCK(chain);
1055 if (gencnt != chain->gencnt) {
1059 /* Serialize all the data. */
1060 LIST_FOREACH(n, &chain->nat, _next) {
1061 ucfg = (struct cfg_nat_legacy *)&data[len];
1064 ucfg->redir_cnt = n->redir_cnt;
1065 ucfg->mode = n->mode;
1066 strlcpy(ucfg->if_name, n->if_name, sizeof(ucfg->if_name));
1067 len += sizeof(struct cfg_nat_legacy);
1068 LIST_FOREACH(r, &n->redir_chain, _next) {
1069 ser_r = (struct cfg_redir_legacy *)&data[len];
1070 ser_r->mode = r->mode;
1071 ser_r->laddr = r->laddr;
1072 ser_r->paddr = r->paddr;
1073 ser_r->raddr = r->raddr;
1074 ser_r->lport = r->lport;
1075 ser_r->pport = r->pport;
1076 ser_r->rport = r->rport;
1077 ser_r->pport_cnt = r->pport_cnt;
1078 ser_r->rport_cnt = r->rport_cnt;
1079 ser_r->proto = r->proto;
1080 ser_r->spool_cnt = r->spool_cnt;
1081 len += sizeof(struct cfg_redir_legacy);
1082 LIST_FOREACH(s, &r->spool_chain, _next) {
1083 ser_s = (struct cfg_spool_legacy *)&data[len];
1084 ser_s->addr = s->addr;
1085 ser_s->port = s->port;
1086 len += sizeof(struct cfg_spool_legacy);
1090 IPFW_UH_RUNLOCK(chain);
1092 error = sooptcopyout(sopt, data, len);
1099 ipfw_nat_get_log(struct sockopt *sopt)
1102 struct cfg_nat *ptr;
1104 struct ip_fw_chain *chain;
1107 chain = &V_layer3_chain;
1110 /* one pass to count, one to copy the data */
1112 LIST_FOREACH(ptr, &chain->nat, _next) {
1113 if (ptr->lib->logDesc == NULL)
1117 size = i * (LIBALIAS_BUF_SIZE + sizeof(int));
1118 data = malloc(size, M_IPFW, M_NOWAIT | M_ZERO);
1120 IPFW_RUNLOCK(chain);
1124 LIST_FOREACH(ptr, &chain->nat, _next) {
1125 if (ptr->lib->logDesc == NULL)
1127 bcopy(&ptr->id, &data[i], sizeof(int));
1129 bcopy(ptr->lib->logDesc, &data[i], LIBALIAS_BUF_SIZE);
1130 i += LIBALIAS_BUF_SIZE;
1132 IPFW_RUNLOCK(chain);
1133 sooptcopyout(sopt, data, size);
1139 vnet_ipfw_nat_init(const void *arg __unused)
1142 V_ipfw_nat_ready = 1;
1147 vnet_ipfw_nat_uninit(const void *arg __unused)
1149 struct cfg_nat *ptr, *ptr_temp;
1150 struct ip_fw_chain *chain;
1152 chain = &V_layer3_chain;
1154 V_ipfw_nat_ready = 0;
1155 LIST_FOREACH_SAFE(ptr, &chain->nat, _next, ptr_temp) {
1156 LIST_REMOVE(ptr, _next);
1157 free_nat_instance(ptr);
1159 flush_nat_ptrs(chain, -1 /* flush all */);
1160 IPFW_WUNLOCK(chain);
1168 /* init ipfw hooks */
1169 ipfw_nat_ptr = ipfw_nat;
1170 lookup_nat_ptr = lookup_nat;
1171 ipfw_nat_cfg_ptr = ipfw_nat_cfg;
1172 ipfw_nat_del_ptr = ipfw_nat_del;
1173 ipfw_nat_get_cfg_ptr = ipfw_nat_get_cfg;
1174 ipfw_nat_get_log_ptr = ipfw_nat_get_log;
1175 IPFW_ADD_SOPT_HANDLER(1, scodes);
1177 ifaddr_event_tag = EVENTHANDLER_REGISTER(ifaddr_event, ifaddr_change,
1178 NULL, EVENTHANDLER_PRI_ANY);
1182 ipfw_nat_destroy(void)
1185 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_event_tag);
1186 /* deregister ipfw_nat */
1187 IPFW_DEL_SOPT_HANDLER(1, scodes);
1188 ipfw_nat_ptr = NULL;
1189 lookup_nat_ptr = NULL;
1190 ipfw_nat_cfg_ptr = NULL;
1191 ipfw_nat_del_ptr = NULL;
1192 ipfw_nat_get_cfg_ptr = NULL;
1193 ipfw_nat_get_log_ptr = NULL;
1197 ipfw_nat_modevent(module_t mod, int type, void *unused)
1215 static moduledata_t ipfw_nat_mod = {
1221 /* Define startup order. */
1222 #define IPFW_NAT_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL
1223 #define IPFW_NAT_MODEVENT_ORDER (SI_ORDER_ANY - 128) /* after ipfw */
1224 #define IPFW_NAT_MODULE_ORDER (IPFW_NAT_MODEVENT_ORDER + 1)
1225 #define IPFW_NAT_VNET_ORDER (IPFW_NAT_MODEVENT_ORDER + 2)
1227 DECLARE_MODULE(ipfw_nat, ipfw_nat_mod, IPFW_NAT_SI_SUB_FIREWALL, SI_ORDER_ANY);
1228 MODULE_DEPEND(ipfw_nat, libalias, 1, 1, 1);
1229 MODULE_DEPEND(ipfw_nat, ipfw, 3, 3, 3);
1230 MODULE_VERSION(ipfw_nat, 1);
1232 SYSINIT(ipfw_nat_init, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_MODULE_ORDER,
1233 ipfw_nat_init, NULL);
1234 VNET_SYSINIT(vnet_ipfw_nat_init, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_VNET_ORDER,
1235 vnet_ipfw_nat_init, NULL);
1237 SYSUNINIT(ipfw_nat_destroy, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_MODULE_ORDER,
1238 ipfw_nat_destroy, NULL);
1239 VNET_SYSUNINIT(vnet_ipfw_nat_uninit, IPFW_NAT_SI_SUB_FIREWALL,
1240 IPFW_NAT_VNET_ORDER, vnet_ipfw_nat_uninit, NULL);