2 * Copyright (C) 1995-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * Added redirect stuff and a LOT of bug fixes. (mcn@EnGarde.com)
9 #if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
14 # include <sys/ptimers.h>
16 #include <sys/errno.h>
17 #include <sys/types.h>
18 #include <sys/param.h>
21 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
23 # include "opt_ipfilter_log.h"
25 #if !defined(_KERNEL) && !defined(KERNEL)
30 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
31 # include <sys/filio.h>
32 # include <sys/fcntl.h>
34 # include <sys/ioctl.h>
36 #include <sys/fcntl.h>
38 # include <sys/protosw.h>
40 #include <sys/socket.h>
41 #if defined(_KERNEL) && !defined(linux)
42 # include <sys/systm.h>
44 #if !defined(__SVR4) && !defined(__svr4__)
46 # include <sys/mbuf.h>
49 # include <sys/filio.h>
50 # include <sys/byteorder.h>
52 # include <sys/dditypes.h>
54 # include <sys/stream.h>
55 # include <sys/kmem.h>
57 #if __FreeBSD_version >= 300000
58 # include <sys/queue.h>
61 #if __FreeBSD_version >= 300000
62 # include <net/if_var.h>
63 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
64 # include "opt_ipfilter.h"
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
76 # ifdef IFF_DRVRLOCK /* IRIX6 */
77 #include <sys/hashing.h>
78 #include <netinet/in_var.h>
84 # include <vpn/ipsec.h>
85 extern struct ifnet vpnif;
89 # include <netinet/ip_var.h>
90 # include <netinet/tcp_fsm.h>
92 #include <netinet/tcp.h>
93 #include <netinet/udp.h>
94 #include <netinet/ip_icmp.h>
95 #include "netinet/ip_compat.h"
96 #include <netinet/tcpip.h>
97 #include "netinet/ip_fil.h"
98 #include "netinet/ip_nat.h"
99 #include "netinet/ip_frag.h"
100 #include "netinet/ip_state.h"
101 #include "netinet/ip_proxy.h"
102 #if (__FreeBSD_version >= 300000)
103 # include <sys/malloc.h>
106 # define MIN(a,b) (((a)<(b))?(a):(b))
109 #define SOCKADDR_IN struct sockaddr_in
112 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
113 /* static const char rcsid[] = "@(#)$Id: ip_nat.c,v 2.37.2.44 2001/07/21 07:17:22 darrenr Exp $"; */
114 static const char rcsid[] = "@(#)$FreeBSD$";
117 nat_t **nat_table[2] = { NULL, NULL },
118 *nat_instances = NULL;
119 ipnat_t *nat_list = NULL;
120 u_int ipf_nattable_sz = NAT_TABLE_SZ;
121 u_int ipf_natrules_sz = NAT_SIZE;
122 u_int ipf_rdrrules_sz = RDR_SIZE;
123 u_int ipf_hostmap_sz = HOSTMAP_SIZE;
124 u_32_t nat_masks = 0;
125 u_32_t rdr_masks = 0;
126 ipnat_t **nat_rules = NULL;
127 ipnat_t **rdr_rules = NULL;
128 hostmap_t **maptable = NULL;
130 u_long fr_defnatage = DEF_NAT_AGE,
131 fr_defnaticmpage = 6; /* 3 seconds */
134 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
135 extern kmutex_t ipf_rw;
136 extern KRWLOCK_T ipf_nat;
139 static int nat_flushtable __P((void));
140 static void nat_addnat __P((struct ipnat *));
141 static void nat_addrdr __P((struct ipnat *));
142 static void nat_delete __P((struct nat *));
143 static void nat_delrdr __P((struct ipnat *));
144 static void nat_delnat __P((struct ipnat *));
145 static int fr_natgetent __P((caddr_t));
146 static int fr_natgetsz __P((caddr_t));
147 static int fr_natputent __P((caddr_t));
148 static void nat_tabmove __P((fr_info_t *, nat_t *));
149 static int nat_match __P((fr_info_t *, ipnat_t *, ip_t *));
150 static hostmap_t *nat_hostmap __P((ipnat_t *, struct in_addr,
152 static void nat_hostmapdel __P((struct hostmap *));
157 KMALLOCS(nat_table[0], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
158 if (nat_table[0] != NULL)
159 bzero((char *)nat_table[0], ipf_nattable_sz * sizeof(nat_t *));
163 KMALLOCS(nat_table[1], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
164 if (nat_table[1] != NULL)
165 bzero((char *)nat_table[1], ipf_nattable_sz * sizeof(nat_t *));
169 KMALLOCS(nat_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_natrules_sz);
170 if (nat_rules != NULL)
171 bzero((char *)nat_rules, ipf_natrules_sz * sizeof(ipnat_t *));
175 KMALLOCS(rdr_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_rdrrules_sz);
176 if (rdr_rules != NULL)
177 bzero((char *)rdr_rules, ipf_rdrrules_sz * sizeof(ipnat_t *));
181 KMALLOCS(maptable, hostmap_t **, sizeof(hostmap_t *) * ipf_hostmap_sz);
182 if (maptable != NULL)
183 bzero((char *)maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
190 static void nat_addrdr(n)
198 k = countbits(n->in_outmsk);
199 if ((k >= 0) && (k != 32))
201 j = (n->in_outip & n->in_outmsk);
202 hv = NAT_HASH_FN(j, 0, ipf_rdrrules_sz);
205 np = &(*np)->in_rnext;
212 static void nat_addnat(n)
220 k = countbits(n->in_inmsk);
221 if ((k >= 0) && (k != 32))
223 j = (n->in_inip & n->in_inmsk);
224 hv = NAT_HASH_FN(j, 0, ipf_natrules_sz);
227 np = &(*np)->in_mnext;
234 static void nat_delrdr(n)
238 n->in_rnext->in_prnext = n->in_prnext;
239 *n->in_prnext = n->in_rnext;
243 static void nat_delnat(n)
247 n->in_mnext->in_pmnext = n->in_pmnext;
248 *n->in_pmnext = n->in_mnext;
253 * check if an ip address has already been allocated for a given mapping that
254 * is not doing port based translation.
256 * Must be called with ipf_nat held as a write lock.
258 static struct hostmap *nat_hostmap(np, real, map)
266 hv = real.s_addr % HOSTMAP_SIZE;
267 for (hm = maptable[hv]; hm; hm = hm->hm_next)
268 if ((hm->hm_realip.s_addr == real.s_addr) &&
269 (np == hm->hm_ipnat)) {
274 KMALLOC(hm, hostmap_t *);
276 hm->hm_next = maptable[hv];
277 hm->hm_pnext = maptable + hv;
279 maptable[hv]->hm_pnext = &hm->hm_next;
282 hm->hm_realip = real;
291 * Must be called with ipf_nat held as a write lock.
293 static void nat_hostmapdel(hm)
296 ATOMIC_DEC32(hm->hm_ref);
297 if (hm->hm_ref == 0) {
299 hm->hm_next->hm_pnext = hm->hm_pnext;
300 *hm->hm_pnext = hm->hm_next;
306 void fix_outcksum(fin, sp, n)
311 register u_short sumshort;
312 register u_32_t sum1;
316 else if (n & NAT_HW_CKSUM) {
319 n = (n & 0xffff) + (n >> 16);
323 sum1 = (~ntohs(*sp)) & 0xffff;
325 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
327 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
328 sumshort = ~(u_short)sum1;
329 *(sp) = htons(sumshort);
333 void fix_incksum(fin, sp, n)
338 register u_short sumshort;
339 register u_32_t sum1;
343 else if (n & NAT_HW_CKSUM) {
346 n = (n & 0xffff) + (n >> 16);
351 sum1 = (~(*sp)) & 0xffff;
353 sum1 = (~ntohs(*sp)) & 0xffff;
355 sum1 += ~(n) & 0xffff;
356 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
358 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
359 sumshort = ~(u_short)sum1;
360 *(sp) = htons(sumshort);
365 * fix_datacksum is used *only* for the adjustments of checksums in the data
366 * section of an IP packet.
368 * The only situation in which you need to do this is when NAT'ing an
369 * ICMP error message. Such a message, contains in its body the IP header
370 * of the original IP packet, that causes the error.
372 * You can't use fix_incksum or fix_outcksum in that case, because for the
373 * kernel the data section of the ICMP error is just data, and no special
374 * processing like hardware cksum or ntohs processing have been done by the
375 * kernel on the data section.
377 void fix_datacksum(sp, n)
381 register u_short sumshort;
382 register u_32_t sum1;
387 sum1 = (~ntohs(*sp)) & 0xffff;
389 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
391 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
392 sumshort = ~(u_short)sum1;
393 *(sp) = htons(sumshort);
397 * How the NAT is organised and works.
399 * Inside (interface y) NAT Outside (interface x)
400 * -------------------- -+- -------------------------------------
401 * Packet going | out, processsed by ip_natout() for x
402 * ------------> | ------------>
403 * src=10.1.1.1 | src=192.1.1.1
405 * | in, processed by ip_natin() for x
406 * <------------ | <------------
407 * dst=10.1.1.1 | dst=192.1.1.1
408 * -------------------- -+- -------------------------------------
409 * ip_natout() - changes ip_src and if required, sport
410 * - creates a new mapping, if required.
411 * ip_natin() - changes ip_dst and if required, dport
413 * In the NAT table, internal source is recorded as "in" and externally
418 * Handle ioctls which manipulate the NAT.
420 int nat_ioctl(data, cmd, mode)
421 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
429 register ipnat_t *nat, *nt, *n = NULL, **np = NULL;
430 int error = 0, ret, arg, getlock;
434 #if (BSD >= 199306) && defined(_KERNEL)
435 if ((securelevel >= 3) && (mode & FWRITE))
439 nat = NULL; /* XXX gcc -Wuninitialized */
440 KMALLOC(nt, ipnat_t *);
441 getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
442 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
443 if (mode & NAT_SYSSPACE) {
444 bcopy(data, (char *)&natd, sizeof(natd));
447 error = IRCOPYPTR(data, (char *)&natd, sizeof(natd));
449 } else if (cmd == SIOCIPFFL) { /* SIOCFLNAT & SIOCCNATL */
450 error = IRCOPY(data, (char *)&arg, sizeof(arg));
459 * For add/delete, look to see if the NAT entry is already present
462 WRITE_ENTER(&ipf_nat);
463 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
465 nat->in_flags &= IPN_USERFLAGS;
466 if ((nat->in_redir & NAT_MAPBLK) == 0) {
467 if ((nat->in_flags & IPN_SPLIT) == 0)
468 nat->in_inip &= nat->in_inmsk;
469 if ((nat->in_flags & IPN_IPRANGE) == 0)
470 nat->in_outip &= nat->in_outmsk;
472 for (np = &nat_list; (n = *np); np = &n->in_next)
473 if (!bcmp((char *)&nat->in_flags, (char *)&n->in_flags,
475 if (n->in_redir == NAT_REDIRECT &&
476 n->in_pnext != nat->in_pnext)
489 if (!(mode & FWRITE))
492 tmp = ipflog_clear(IPL_LOGNAT);
493 IWCOPY((char *)&tmp, (char *)data, sizeof(tmp));
499 if (!(mode & FWRITE)) {
513 bcopy((char *)nat, (char *)n, sizeof(*n));
514 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
516 n->in_ifp = (void *)-1;
517 if (n->in_plabel[0] != '\0') {
518 n->in_apr = appr_lookup(n->in_p, n->in_plabel);
527 if (n->in_redir & NAT_REDIRECT) {
528 n->in_flags &= ~IPN_NOTDST;
531 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
532 n->in_flags &= ~IPN_NOTSRC;
537 if (n->in_redir & NAT_MAPBLK)
538 n->in_space = USABLE_PORTS * ~ntohl(n->in_outmsk);
539 else if (n->in_flags & IPN_AUTOPORTMAP)
540 n->in_space = USABLE_PORTS * ~ntohl(n->in_inmsk);
541 else if (n->in_flags & IPN_IPRANGE)
542 n->in_space = ntohl(n->in_outmsk) - ntohl(n->in_outip);
543 else if (n->in_flags & IPN_SPLIT)
546 n->in_space = ~ntohl(n->in_outmsk);
548 * Calculate the number of valid IP addresses in the output
549 * mapping range. In all cases, the range is inclusive of
550 * the start and ending IP addresses.
551 * If to a CIDR address, lose 2: broadcast + network address
553 * If to a range, add one.
554 * If to a single IP address, set to 1.
557 if ((n->in_flags & IPN_IPRANGE) != 0)
563 if ((n->in_outmsk != 0xffffffff) && (n->in_outmsk != 0) &&
564 ((n->in_flags & (IPN_IPRANGE|IPN_SPLIT)) == 0))
565 n->in_nip = ntohl(n->in_outip) + 1;
566 else if ((n->in_flags & IPN_SPLIT) &&
567 (n->in_redir & NAT_REDIRECT))
568 n->in_nip = ntohl(n->in_inip);
570 n->in_nip = ntohl(n->in_outip);
571 if (n->in_redir & NAT_MAP) {
572 n->in_pnext = ntohs(n->in_pmin);
574 * Multiply by the number of ports made available.
576 if (ntohs(n->in_pmax) >= ntohs(n->in_pmin)) {
577 n->in_space *= (ntohs(n->in_pmax) -
578 ntohs(n->in_pmin) + 1);
580 * Because two different sources can map to
581 * different destinations but use the same
583 * If the result is smaller than in_space, then
584 * we may have wrapped around 32bits.
587 if ((i != 0) && (i != 0xffffffff)) {
588 j = n->in_space * (~ntohl(i) + 1);
589 if (j >= n->in_space)
592 n->in_space = 0xffffffff;
596 * If no protocol is specified, multiple by 256.
598 if ((n->in_flags & IPN_TCPUDP) == 0) {
599 j = n->in_space * 256;
600 if (j >= n->in_space)
603 n->in_space = 0xffffffff;
606 /* Otherwise, these fields are preset */
608 nat_stats.ns_rules++;
611 if (!(mode & FWRITE)) {
620 if (n->in_redir & NAT_REDIRECT)
622 if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
624 if (nat_list == NULL) {
631 appr_free(n->in_apr);
633 nat_stats.ns_rules--;
635 n->in_flags |= IPN_DELETE;
641 MUTEX_DOWNGRADE(&ipf_nat);
642 nat_stats.ns_table[0] = nat_table[0];
643 nat_stats.ns_table[1] = nat_table[1];
644 nat_stats.ns_list = nat_list;
645 nat_stats.ns_maptable = maptable;
646 nat_stats.ns_nattab_sz = ipf_nattable_sz;
647 nat_stats.ns_rultab_sz = ipf_natrules_sz;
648 nat_stats.ns_rdrtab_sz = ipf_rdrrules_sz;
649 nat_stats.ns_hostmap_sz = ipf_hostmap_sz;
650 nat_stats.ns_instances = nat_instances;
651 nat_stats.ns_apslist = ap_sess_list;
652 error = IWCOPYPTR((char *)&nat_stats, (char *)data,
659 MUTEX_DOWNGRADE(&ipf_nat);
660 error = IRCOPYPTR((char *)data, (char *)&nl, sizeof(nl));
664 if (nat_lookupredir(&nl)) {
665 error = IWCOPYPTR((char *)&nl, (char *)data,
671 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
672 if (!(mode & FWRITE)) {
678 ret = nat_flushtable();
680 ret = nat_clearlist();
683 MUTEX_DOWNGRADE(&ipf_nat);
685 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
691 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
693 error = IWCOPY((caddr_t)&fr_nat_lock, data,
694 sizeof(fr_nat_lock));
702 error = fr_natputent(data);
708 error = fr_natgetsz(data);
714 error = fr_natgetent(data);
720 arg = (int)iplused[IPL_LOGNAT];
721 MUTEX_DOWNGRADE(&ipf_nat);
722 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
732 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
740 static int fr_natgetsz(data)
748 error = IRCOPY(data, (caddr_t)&ng, sizeof(ng));
757 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
764 * Make sure the pointer we're copying from exists in the
765 * current list of entries. Security precaution to prevent
766 * copying of random kernel data.
768 for (n = nat_instances; n; n = n->nat_next)
775 ng.ng_sz = sizeof(nat_save_t);
777 if ((aps != NULL) && (aps->aps_data != 0)) {
778 ng.ng_sz += sizeof(ap_session_t);
779 ng.ng_sz += aps->aps_psiz;
782 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
789 static int fr_natgetent(data)
792 nat_save_t ipn, *ipnp, *ipnn = NULL;
793 register nat_t *n, *nat;
797 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
800 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
808 if (nat_instances == NULL)
814 * Make sure the pointer we're copying from exists in the
815 * current list of entries. Security precaution to prevent
816 * copying of random kernel data.
818 for (n = nat_instances; n; n = n->nat_next)
825 ipn.ipn_next = nat->nat_next;
827 bcopy((char *)nat, (char *)&ipn.ipn_nat, sizeof(ipn.ipn_nat));
828 ipn.ipn_nat.nat_data = NULL;
831 bcopy((char *)nat->nat_ptr, (char *)&ipn.ipn_ipnat,
832 sizeof(ipn.ipn_ipnat));
836 bcopy((char *)nat->nat_fr, (char *)&ipn.ipn_rule,
837 sizeof(ipn.ipn_rule));
839 if ((aps = nat->nat_aps)) {
840 ipn.ipn_dsize = sizeof(*aps);
842 ipn.ipn_dsize += aps->aps_psiz;
843 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + ipn.ipn_dsize);
846 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
848 bcopy((char *)aps, (char *)ipnn->ipn_data, sizeof(*aps));
850 bcopy(aps->aps_data, ipnn->ipn_data + sizeof(*aps),
852 ipnn->ipn_dsize += aps->aps_psiz;
854 error = IWCOPY((caddr_t)ipnn, ipnp,
855 sizeof(ipn) + ipn.ipn_dsize);
858 KFREES(ipnn, sizeof(*ipnn) + ipn.ipn_dsize);
860 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
868 static int fr_natputent(data)
871 nat_save_t ipn, *ipnp, *ipnn = NULL;
872 register nat_t *n, *nat;
879 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
882 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
887 KMALLOCS(ipnn, nat_save_t *, sizeof(ipn) + ipn.ipn_dsize);
890 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
891 error = IRCOPY((caddr_t)ipnp, (caddr_t)ipn.ipn_data,
900 KMALLOC(nat, nat_t *);
906 bcopy((char *)&ipn.ipn_nat, (char *)nat, sizeof(*nat));
908 * Initialize all these so that nat_delete() doesn't cause a crash.
910 nat->nat_phnext[0] = NULL;
911 nat->nat_phnext[1] = NULL;
919 nat->nat_data = NULL;
920 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
923 * Restore the rule associated with this nat session
926 KMALLOC(in, ipnat_t *);
932 bcopy((char *)&ipn.ipn_ipnat, (char *)in, sizeof(*in));
934 in->in_flags |= IPN_DELETE;
937 in->in_prnext = NULL;
939 in->in_pmnext = NULL;
940 in->in_ifp = GETUNIT(in->in_ifname, 4);
941 if (in->in_plabel[0] != '\0') {
942 in->in_apr = appr_lookup(in->in_p, in->in_plabel);
947 * Restore ap_session_t structure. Include the private data allocated
951 KMALLOC(aps, ap_session_t *);
957 aps->aps_next = ap_sess_list;
959 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
961 aps->aps_apr = in->in_apr;
963 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
964 if (aps->aps_data == NULL) {
968 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
972 aps->aps_data = NULL;
977 * If there was a filtering rule associated with this entry then
978 * build up a new one.
981 if (nat->nat_flags & FI_NEWFR) {
982 KMALLOC(fr, frentry_t *);
988 bcopy((char *)&ipn.ipn_fr, (char *)fr, sizeof(*fr));
989 ipn.ipn_nat.nat_fr = fr;
990 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
996 for (n = nat_instances; n; n = n->nat_next)
1007 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1012 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1020 * Delete a nat entry from the various lists and table.
1022 static void nat_delete(natd)
1027 if (natd->nat_flags & FI_WILDP)
1028 nat_stats.ns_wilds--;
1029 if (natd->nat_hnext[0])
1030 natd->nat_hnext[0]->nat_phnext[0] = natd->nat_phnext[0];
1031 *natd->nat_phnext[0] = natd->nat_hnext[0];
1032 if (natd->nat_hnext[1])
1033 natd->nat_hnext[1]->nat_phnext[1] = natd->nat_phnext[1];
1034 *natd->nat_phnext[1] = natd->nat_hnext[1];
1035 if (natd->nat_me != NULL)
1036 *natd->nat_me = NULL;
1038 if (natd->nat_fr != NULL) {
1039 ATOMIC_DEC32(natd->nat_fr->fr_ref);
1042 if (natd->nat_hm != NULL)
1043 nat_hostmapdel(natd->nat_hm);
1046 * If there is an active reference from the nat entry to its parent
1047 * rule, decrement the rule's reference count and free it too if no
1048 * longer being used.
1050 ipn = natd->nat_ptr;
1054 if (!ipn->in_use && (ipn->in_flags & IPN_DELETE)) {
1056 appr_free(ipn->in_apr);
1058 nat_stats.ns_rules--;
1062 MUTEX_DESTROY(&natd->nat_lock);
1064 * If there's a fragment table entry too for this nat entry, then
1065 * dereference that as well.
1067 ipfr_forget((void *)natd);
1068 aps_free(natd->nat_aps);
1069 nat_stats.ns_inuse--;
1075 * nat_flushtable - clear the NAT table of all mapping entries.
1076 * (this is for the dynamic mappings)
1078 static int nat_flushtable()
1080 register nat_t *nat, **natp;
1084 * ALL NAT mappings deleted, so lets just make the deletions
1087 if (nat_table[0] != NULL)
1088 bzero((char *)nat_table[0],
1089 sizeof(nat_table[0]) * ipf_nattable_sz);
1090 if (nat_table[1] != NULL)
1091 bzero((char *)nat_table[1],
1092 sizeof(nat_table[1]) * ipf_nattable_sz);
1094 for (natp = &nat_instances; (nat = *natp); ) {
1095 *natp = nat->nat_next;
1097 nat_log(nat, NL_FLUSH);
1102 nat_stats.ns_inuse = 0;
1108 * nat_clearlist - delete all rules in the active NAT mapping list.
1109 * (this is for NAT/RDR rules)
1113 register ipnat_t *n, **np = &nat_list;
1116 if (nat_rules != NULL)
1117 bzero((char *)nat_rules, sizeof(*nat_rules) * ipf_natrules_sz);
1118 if (rdr_rules != NULL)
1119 bzero((char *)rdr_rules, sizeof(*rdr_rules) * ipf_rdrrules_sz);
1125 appr_free(n->in_apr);
1127 nat_stats.ns_rules--;
1129 n->in_flags |= IPN_DELETE;
1141 * Create a new NAT table entry.
1142 * NOTE: Assumes write lock on ipf_nat has been obtained already.
1143 * If you intend on changing this, beware: appr_new() may call nat_new()
1146 nat_t *nat_new(fin, ip, np, natsave, flags, direction)
1154 register u_32_t sum1, sum2, sumd, l;
1155 u_short port = 0, sport = 0, dport = 0, nport = 0;
1156 struct in_addr in, inb;
1157 u_short nflags, sp, dp;
1158 tcphdr_t *tcp = NULL;
1159 hostmap_t *hm = NULL;
1161 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1162 qif_t *qf = fin->fin_qif;
1165 nflags = flags & np->in_flags;
1166 if (flags & IPN_TCPUDP) {
1167 tcp = (tcphdr_t *)fin->fin_dp;
1168 sport = htons(fin->fin_data[0]);
1169 dport = htons(fin->fin_data[1]);
1172 /* Give me a new nat */
1173 KMALLOC(nat, nat_t *);
1175 nat_stats.ns_memfail++;
1179 bzero((char *)nat, sizeof(*nat));
1180 nat->nat_flags = flags;
1181 if (flags & FI_WILDP)
1182 nat_stats.ns_wilds++;
1184 * Search the current table for a match.
1186 if (direction == NAT_OUTBOUND) {
1188 * Values at which the search for a free resouce starts.
1194 * If it's an outbound packet which doesn't match any existing
1195 * record, then create a new port
1199 st_port = np->in_pnext;
1203 in.s_addr = htonl(np->in_nip);
1206 * Check to see if there is an existing NAT
1207 * setup for this IP address pair.
1209 hm = nat_hostmap(np, fin->fin_src, in);
1211 in.s_addr = hm->hm_mapip.s_addr;
1212 } else if ((l == 1) && (hm != NULL)) {
1216 in.s_addr = ntohl(in.s_addr);
1220 if ((np->in_outmsk == 0xffffffff) &&
1221 (np->in_pnext == 0)) {
1226 if (np->in_redir & NAT_MAPBLK) {
1227 if ((l >= np->in_ppip) || ((l > 0) &&
1228 !(flags & IPN_TCPUDP)))
1231 * map-block - Calculate destination address.
1233 in.s_addr = ntohl(fin->fin_saddr);
1234 in.s_addr &= ntohl(~np->in_inmsk);
1235 inb.s_addr = in.s_addr;
1236 in.s_addr /= np->in_ippip;
1237 in.s_addr &= ntohl(~np->in_outmsk);
1238 in.s_addr += ntohl(np->in_outip);
1240 * Calculate destination port.
1242 if ((flags & IPN_TCPUDP) &&
1243 (np->in_ppip != 0)) {
1244 port = ntohs(sport) + l;
1245 port %= np->in_ppip;
1246 port += np->in_ppip *
1247 (inb.s_addr % np->in_ippip);
1248 port += MAPBLK_MINPORT;
1251 } else if (!np->in_outip &&
1252 (np->in_outmsk == 0xffffffff)) {
1254 * 0/32 - use the interface's IP address.
1257 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1259 in.s_addr = ntohl(in.s_addr);
1260 } else if (!np->in_outip && !np->in_outmsk) {
1262 * 0/0 - use the original source address/port.
1266 in.s_addr = ntohl(fin->fin_saddr);
1267 } else if ((np->in_outmsk != 0xffffffff) &&
1268 (np->in_pnext == 0) &&
1269 ((l > 0) || (hm == NULL)))
1273 if ((nflags & IPN_TCPUDP) &&
1274 ((np->in_redir & NAT_MAPBLK) == 0) &&
1275 (np->in_flags & IPN_AUTOPORTMAP)) {
1276 if ((l > 0) && (l % np->in_ppip == 0)) {
1277 if (l > np->in_space) {
1279 } else if ((l > np->in_ppip) &&
1280 np->in_outmsk != 0xffffffff)
1283 if (np->in_ppip != 0) {
1284 port = ntohs(sport);
1285 port += (l % np->in_ppip);
1286 port %= np->in_ppip;
1287 port += np->in_ppip *
1288 (ntohl(fin->fin_saddr) %
1290 port += MAPBLK_MINPORT;
1293 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1294 (nflags & IPN_TCPUDP) &&
1295 (np->in_pnext != 0)) {
1296 port = htons(np->in_pnext++);
1297 if (np->in_pnext > ntohs(np->in_pmax)) {
1298 np->in_pnext = ntohs(np->in_pmin);
1299 if (np->in_outmsk != 0xffffffff)
1304 if (np->in_flags & IPN_IPRANGE) {
1305 if (np->in_nip > ntohl(np->in_outmsk))
1306 np->in_nip = ntohl(np->in_outip);
1308 if ((np->in_outmsk != 0xffffffff) &&
1309 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1310 ntohl(np->in_outip))
1311 np->in_nip = ntohl(np->in_outip) + 1;
1314 if (!port && (flags & IPN_TCPUDP))
1318 * Here we do a lookup of the connection as seen from
1319 * the outside. If an IP# pair already exists, try
1320 * again. So if you have A->B becomes C->B, you can
1321 * also have D->E become C->E but not D->B causing
1322 * another C->B. Also take protocol and ports into
1323 * account when determining whether a pre-existing
1324 * NAT setup will cause an external conflict where
1325 * this is appropriate.
1327 inb.s_addr = htonl(in.s_addr);
1328 sp = fin->fin_data[0];
1329 dp = fin->fin_data[1];
1330 fin->fin_data[0] = fin->fin_data[1];
1331 fin->fin_data[1] = htons(port);
1332 natl = nat_inlookup(fin, flags & ~FI_WILDP,
1333 (u_int)fin->fin_p, fin->fin_dst,
1335 fin->fin_data[0] = sp;
1336 fin->fin_data[1] = dp;
1339 * Has the search wrapped around and come back to the
1342 if ((natl != NULL) &&
1343 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1344 (np->in_nip != 0) && (st_ip == np->in_nip))
1347 } while (natl != NULL);
1349 if (np->in_space > 0)
1352 /* Setup the NAT table */
1353 nat->nat_inip = fin->fin_src;
1354 nat->nat_outip.s_addr = htonl(in.s_addr);
1355 nat->nat_oip = fin->fin_dst;
1356 if (nat->nat_hm == NULL)
1357 nat->nat_hm = nat_hostmap(np, fin->fin_src,
1360 sum1 = LONG_SUM(ntohl(fin->fin_saddr)) + ntohs(sport);
1361 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1363 if (flags & IPN_TCPUDP) {
1364 nat->nat_inport = sport;
1365 nat->nat_outport = port; /* sport */
1366 nat->nat_oport = dport;
1370 * Otherwise, it's an inbound packet. Most likely, we don't
1371 * want to rewrite source ports and source addresses. Instead,
1372 * we want to rewrite to a fixed internal address and fixed
1375 if (np->in_flags & IPN_SPLIT) {
1376 in.s_addr = np->in_nip;
1377 if (np->in_inip == htonl(in.s_addr))
1378 np->in_nip = ntohl(np->in_inmsk);
1380 np->in_nip = ntohl(np->in_inip);
1381 if (np->in_flags & IPN_ROUNDR) {
1387 in.s_addr = ntohl(np->in_inip);
1388 if (np->in_flags & IPN_ROUNDR) {
1397 * Whilst not optimized for the case where
1398 * pmin == pmax, the gain is not significant.
1400 if (np->in_pmin != np->in_pmax) {
1401 nport = ntohs(dport) - ntohs(np->in_pmin) +
1402 ntohs(np->in_pnext);
1403 nport = ntohs(nport);
1405 nport = np->in_pnext;
1409 * When the redirect-to address is set to 0.0.0.0, just
1410 * assume a blank `forwarding' of the packet.
1413 in.s_addr = ntohl(fin->fin_daddr);
1415 nat->nat_inip.s_addr = htonl(in.s_addr);
1416 nat->nat_outip = fin->fin_dst;
1417 nat->nat_oip = fin->fin_src;
1419 sum1 = LONG_SUM(ntohl(fin->fin_daddr)) + ntohs(dport);
1420 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1422 if (flags & IPN_TCPUDP) {
1423 nat->nat_inport = nport;
1424 nat->nat_outport = dport;
1425 nat->nat_oport = sport;
1429 CALC_SUMD(sum1, sum2, sumd);
1430 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1431 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1432 if ((flags & IPN_TCPUDP) && dohwcksum &&
1433 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1434 if (direction == NAT_OUTBOUND)
1435 sum1 = LONG_SUM(ntohl(in.s_addr));
1437 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1438 sum1 += LONG_SUM(ntohl(fin->fin_daddr));
1439 sum1 += IPPROTO_TCP;
1440 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1441 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1444 nat->nat_sumd[1] = nat->nat_sumd[0];
1446 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1447 if (direction == NAT_OUTBOUND)
1448 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1450 sum1 = LONG_SUM(ntohl(fin->fin_daddr));
1452 sum2 = LONG_SUM(in.s_addr);
1454 CALC_SUMD(sum1, sum2, sumd);
1455 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1457 nat->nat_ipsumd = nat->nat_sumd[0];
1459 in.s_addr = htonl(in.s_addr);
1461 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1463 nat->nat_me = natsave;
1464 nat->nat_dir = direction;
1465 nat->nat_ifp = fin->fin_ifp;
1467 nat->nat_p = fin->fin_p;
1470 nat->nat_fr = fin->fin_fr;
1471 if (nat->nat_fr != NULL) {
1472 ATOMIC_INC32(nat->nat_fr->fr_ref);
1474 if (direction == NAT_OUTBOUND) {
1475 if (flags & IPN_TCPUDP)
1476 tcp->th_sport = port;
1478 if (flags & IPN_TCPUDP)
1479 tcp->th_dport = nport;
1484 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
1485 (tcp != NULL && dport == np->in_dport)))
1486 (void) appr_new(fin, ip, nat);
1490 nat_log(nat, (u_int)np->in_redir);
1494 nat_stats.ns_badnat++;
1495 if ((hm = nat->nat_hm) != NULL)
1503 * Insert a NAT entry into the hash tables for searching and add it to the
1504 * list of active NAT entries. Adjust global counters when complete.
1506 void nat_insert(nat)
1512 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1514 nat->nat_age = fr_defnatage;
1515 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1516 if (nat->nat_ifname[0] !='\0') {
1517 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1520 nat->nat_next = nat_instances;
1521 nat_instances = nat;
1523 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1524 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1526 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1528 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1530 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1533 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_inip.s_addr,
1535 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_outip.s_addr,
1539 natp = &nat_table[0][hv1];
1541 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1542 nat->nat_phnext[0] = natp;
1543 nat->nat_hnext[0] = *natp;
1546 natp = &nat_table[1][hv2];
1548 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1549 nat->nat_phnext[1] = natp;
1550 nat->nat_hnext[1] = *natp;
1553 nat_stats.ns_added++;
1554 nat_stats.ns_inuse++;
1558 nat_t *nat_icmplookup(ip, fin, dir)
1564 tcphdr_t *tcp = NULL;
1566 int flags = 0, type, minlen;
1568 icmp = (icmphdr_t *)fin->fin_dp;
1570 * Does it at least have the return (basic) IP header ?
1571 * Only a basic IP header (no options) should be with an ICMP error
1574 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1576 type = icmp->icmp_type;
1578 * If it's not an error type, then return.
1580 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1581 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1582 (type != ICMP_PARAMPROB))
1585 oip = (ip_t *)((char *)fin->fin_dp + 8);
1586 minlen = (oip->ip_hl << 2);
1587 if (minlen < sizeof(ip_t))
1589 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1592 * Is the buffer big enough for all of it ? It's the size of the IP
1593 * header claimed in the encapsulated part which is of concern. It
1594 * may be too big to be in this buffer but not so big that it's
1595 * outside the ICMP packet, leading to TCP deref's causing problems.
1596 * This is possible because we don't know how big oip_hl is when we
1597 * do the pullup early in fr_check() and thus can't gaurantee it is
1606 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1609 m = *(mb_t **)fin->fin_mp;
1610 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1611 (char *)ip + m->m_len)
1617 if (oip->ip_p == IPPROTO_TCP)
1619 else if (oip->ip_p == IPPROTO_UDP)
1621 if (flags & IPN_TCPUDP) {
1625 minlen += 8; /* + 64bits of data to get ports */
1626 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1629 data[0] = fin->fin_data[0];
1630 data[1] = fin->fin_data[1];
1631 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1632 fin->fin_data[0] = ntohs(tcp->th_dport);
1633 fin->fin_data[1] = ntohs(tcp->th_sport);
1635 if (dir == NAT_INBOUND) {
1636 nat = nat_inlookup(fin, flags, (u_int)oip->ip_p,
1637 oip->ip_dst, oip->ip_src, 0);
1639 nat = nat_outlookup(fin, flags, (u_int)oip->ip_p,
1640 oip->ip_dst, oip->ip_src, 0);
1642 fin->fin_data[0] = data[0];
1643 fin->fin_data[1] = data[1];
1646 if (dir == NAT_INBOUND)
1647 return nat_inlookup(fin, 0, (u_int)oip->ip_p,
1648 oip->ip_dst, oip->ip_src, 0);
1650 return nat_outlookup(fin, 0, (u_int)oip->ip_p,
1651 oip->ip_dst, oip->ip_src, 0);
1656 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1657 * packet gets correctly recognised.
1659 nat_t *nat_icmp(ip, fin, nflags, dir)
1665 u_32_t sum1, sum2, sumd, sumd2 = 0;
1674 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1677 * nat_icmplookup() will return NULL for `defective' packets.
1679 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1683 *nflags = IPN_ICMPERR;
1684 icmp = (icmphdr_t *)fin->fin_dp;
1685 oip = (ip_t *)&icmp->icmp_ip;
1686 if (oip->ip_p == IPPROTO_TCP)
1688 else if (oip->ip_p == IPPROTO_UDP)
1690 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1691 dlen = ip->ip_len - ((char *)udp - (char *)ip);
1693 * XXX - what if this is bogus hl and we go off the end ?
1694 * In this case, nat_icmplookup() will have returned NULL.
1696 tcp = (tcphdr_t *)udp;
1699 * Need to adjust ICMP header to include the real IP#'s and
1700 * port #'s. Only apply a checksum change relative to the
1701 * IP address change as it will be modified again in ip_natout
1702 * for both address and port. Two checksum changes are
1703 * necessary for the two header address changes. Be careful
1704 * to only modify the checksum once for the port # and twice
1710 * Fix the IP addresses in the offending IP packet. You also need
1711 * to adjust the IP header checksum of that offending IP packet
1712 * and the ICMP checksum of the ICMP error message itself.
1714 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1715 * in the pseudo header that is used to compute the UDP resp. TCP
1716 * checksum. So, we must compensate that as well. Even worse, the
1717 * change in the UDP and TCP checksums require yet another
1718 * adjustment of the ICMP checksum of the ICMP error message.
1722 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1723 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1727 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1728 in = nat->nat_outip;
1732 sum2 = LONG_SUM(ntohl(in.s_addr));
1734 CALC_SUMD(sum1, sum2, sumd);
1736 if (nat->nat_dir == NAT_OUTBOUND) {
1738 * Fix IP checksum of the offending IP packet to adjust for
1739 * the change in the IP address.
1741 * Normally, you would expect that the ICMP checksum of the
1742 * ICMP error message needs to be adjusted as well for the
1743 * IP address change in oip.
1744 * However, this is a NOP, because the ICMP checksum is
1745 * calculated over the complete ICMP packet, which includes the
1746 * changed oip IP addresses and oip->ip_sum. However, these
1747 * two changes cancel each other out (if the delta for
1748 * the IP address is x, then the delta for ip_sum is minus x),
1749 * so no change in the icmp_cksum is necessary.
1751 * Be careful that nat_dir refers to the direction of the
1752 * offending IP packet (oip), not to its ICMP response (icmp)
1754 fix_datacksum(&oip->ip_sum, sumd);
1757 * Fix UDP pseudo header checksum to compensate for the
1758 * IP address change.
1760 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1762 * The UDP checksum is optional, only adjust it
1763 * if it has been set.
1765 sum1 = ntohs(udp->uh_sum);
1766 fix_datacksum(&udp->uh_sum, sumd);
1767 sum2 = ntohs(udp->uh_sum);
1770 * Fix ICMP checksum to compensate the UDP
1771 * checksum adjustment.
1773 CALC_SUMD(sum1, sum2, sumd);
1778 * Fix TCP pseudo header checksum to compensate for the
1779 * IP address change. Before we can do the change, we
1780 * must make sure that oip is sufficient large to hold
1781 * the TCP checksum (normally it does not!).
1783 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1785 sum1 = ntohs(tcp->th_sum);
1786 fix_datacksum(&tcp->th_sum, sumd);
1787 sum2 = ntohs(tcp->th_sum);
1790 * Fix ICMP checksum to compensate the TCP
1791 * checksum adjustment.
1793 CALC_SUMD(sum1, sum2, sumd);
1799 * Fix IP checksum of the offending IP packet to adjust for
1800 * the change in the IP address.
1802 * Normally, you would expect that the ICMP checksum of the
1803 * ICMP error message needs to be adjusted as well for the
1804 * IP address change in oip.
1805 * However, this is a NOP, because the ICMP checksum is
1806 * calculated over the complete ICMP packet, which includes the
1807 * changed oip IP addresses and oip->ip_sum. However, these
1808 * two changes cancel each other out (if the delta for
1809 * the IP address is x, then the delta for ip_sum is minus x),
1810 * so no change in the icmp_cksum is necessary.
1812 * Be careful that nat_dir refers to the direction of the
1813 * offending IP packet (oip), not to its ICMP response (icmp)
1815 fix_datacksum(&oip->ip_sum, sumd);
1817 /* XXX FV : without having looked at Solaris source code, it seems unlikely
1818 * that SOLARIS would compensate this in the kernel (a body of an IP packet
1819 * in the data section of an ICMP packet). I have the feeling that this should
1820 * be unconditional, but I'm not in a position to check.
1822 #if !SOLARIS && !defined(__sgi)
1824 * Fix UDP pseudo header checksum to compensate for the
1825 * IP address change.
1827 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1829 * The UDP checksum is optional, only adjust it
1830 * if it has been set
1832 sum1 = ntohs(udp->uh_sum);
1833 fix_datacksum(&udp->uh_sum, sumd);
1834 sum2 = ntohs(udp->uh_sum);
1837 * Fix ICMP checksum to compensate the UDP
1838 * checksum adjustment.
1840 CALC_SUMD(sum1, sum2, sumd);
1845 * Fix TCP pseudo header checksum to compensate for the
1846 * IP address change. Before we can do the change, we
1847 * must make sure that oip is sufficient large to hold
1848 * the TCP checksum (normally it does not!).
1850 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1852 sum1 = ntohs(tcp->th_sum);
1853 fix_datacksum(&tcp->th_sum, sumd);
1854 sum2 = ntohs(tcp->th_sum);
1857 * Fix ICMP checksum to compensate the TCP
1858 * checksum adjustment.
1860 CALC_SUMD(sum1, sum2, sumd);
1866 if ((flags & IPN_TCPUDP) != 0) {
1869 * For offending TCP/UDP IP packets, translate the ports as
1870 * well, based on the NAT specification. Of course such
1871 * a change must be reflected in the ICMP checksum as well.
1873 * Advance notice : Now it becomes complicated :-)
1875 * Since the port fields are part of the TCP/UDP checksum
1876 * of the offending IP packet, you need to adjust that checksum
1877 * as well... but, if you change, you must change the icmp
1878 * checksum *again*, to reflect that change.
1880 * To further complicate: the TCP checksum is not in the first
1881 * 8 bytes of the offending ip packet, so it most likely is not
1882 * available. Some OSses like Solaris return enough bytes to
1883 * include the TCP checksum. So we have to check if the
1884 * ip->ip_len actually holds the TCP checksum of the oip!
1887 if (nat->nat_oport == tcp->th_dport) {
1888 if (tcp->th_sport != nat->nat_inport) {
1890 * Fix ICMP checksum to compensate port
1893 sum1 = ntohs(tcp->th_sport);
1894 sum2 = ntohs(nat->nat_inport);
1895 CALC_SUMD(sum1, sum2, sumd);
1897 tcp->th_sport = nat->nat_inport;
1900 * Fix udp checksum to compensate port
1901 * adjustment. NOTE : the offending IP packet
1902 * flows the other direction compared to the
1905 * The UDP checksum is optional, only adjust
1906 * it if it has been set.
1908 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1910 sum1 = ntohs(udp->uh_sum);
1911 fix_datacksum(&udp->uh_sum, sumd);
1912 sum2 = ntohs(udp->uh_sum);
1915 * Fix ICMP checksum to
1916 * compensate UDP checksum
1919 CALC_SUMD(sum1, sum2, sumd);
1924 * Fix tcp checksum (if present) to compensate
1925 * port adjustment. NOTE : the offending IP
1926 * packet flows the other direction compared to
1929 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1931 sum1 = ntohs(tcp->th_sum);
1932 fix_datacksum(&tcp->th_sum, sumd);
1933 sum2 = ntohs(tcp->th_sum);
1936 * Fix ICMP checksum to
1937 * compensate TCP checksum
1940 CALC_SUMD(sum1, sum2, sumd);
1945 if (tcp->th_dport != nat->nat_outport) {
1947 * Fix ICMP checksum to compensate port
1950 sum1 = ntohs(tcp->th_dport);
1951 sum2 = ntohs(nat->nat_outport);
1952 CALC_SUMD(sum1, sum2, sumd);
1954 tcp->th_dport = nat->nat_outport;
1957 * Fix udp checksum to compensate port
1958 * adjustment. NOTE : the offending IP
1959 * packet flows the other direction compared
1960 * to the ICMP message.
1962 * The UDP checksum is optional, only adjust
1963 * it if it has been set.
1965 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1967 sum1 = ntohs(udp->uh_sum);
1968 fix_datacksum(&udp->uh_sum, sumd);
1969 sum2 = ntohs(udp->uh_sum);
1972 * Fix ICMP checksum to compensate
1973 * UDP checksum adjustment.
1975 CALC_SUMD(sum1, sum2, sumd);
1980 * Fix tcp checksum (if present) to compensate
1981 * port adjustment. NOTE : the offending IP
1982 * packet flows the other direction compared to
1985 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1987 sum1 = ntohs(tcp->th_sum);
1988 fix_datacksum(&tcp->th_sum, sumd);
1989 sum2 = ntohs(tcp->th_sum);
1992 * Fix ICMP checksum to compensate
1993 * UDP checksum adjustment.
1995 CALC_SUMD(sum1, sum2, sumd);
2001 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
2002 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
2003 if (nat->nat_dir == NAT_OUTBOUND) {
2004 fix_outcksum(fin, &icmp->icmp_cksum, sumd2);
2006 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
2010 if (oip->ip_p == IPPROTO_ICMP)
2011 nat->nat_age = fr_defnaticmpage;
2017 * NB: these lookups don't lock access to the list, it assume it has already
2021 * Lookup a nat entry based on the mapped destination ip address/port and
2022 * real source address/port. We use this lookup when receiving a packet,
2023 * we're looking for a table entry, based on the destination address.
2024 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2026 nat_t *nat_inlookup(fin, flags, p, src, mapdst, rw)
2028 register u_int flags, p;
2029 struct in_addr src , mapdst;
2032 register u_short sport, dport;
2033 register nat_t *nat;
2034 register int nflags;
2035 register u_32_t dst;
2044 dst = mapdst.s_addr;
2045 if (flags & IPN_TCPUDP) {
2046 sport = htons(fin->fin_data[0]);
2047 dport = htons(fin->fin_data[1]);
2053 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
2054 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
2055 nat = nat_table[1][hv];
2056 for (; nat; nat = nat->nat_hnext[1]) {
2057 nflags = nat->nat_flags;
2058 if ((!ifp || ifp == nat->nat_ifp) &&
2059 nat->nat_oip.s_addr == src.s_addr &&
2060 nat->nat_outip.s_addr == dst &&
2061 ((p == 0) || (p == nat->nat_p))) {
2066 if (nat->nat_oport != sport)
2068 if (nat->nat_outport != dport)
2076 if ((ipn != NULL) && (nat->nat_aps != NULL))
2077 if (appr_match(fin, nat) != 0)
2082 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2085 RWLOCK_EXIT(&ipf_nat);
2087 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
2088 hv = NAT_HASH_FN(src.s_addr, dst, ipf_nattable_sz);
2090 WRITE_ENTER(&ipf_nat);
2092 nat = nat_table[1][hv];
2093 for (; nat; nat = nat->nat_hnext[1]) {
2094 nflags = nat->nat_flags;
2095 if (ifp && ifp != nat->nat_ifp)
2097 if (!(nflags & FI_WILDP))
2099 if (nat->nat_oip.s_addr != src.s_addr ||
2100 nat->nat_outip.s_addr != dst)
2102 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
2103 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
2104 nat_tabmove(fin, nat);
2109 MUTEX_DOWNGRADE(&ipf_nat);
2116 * This function is only called for TCP/UDP NAT table entries where the
2117 * original was placed in the table without hashing on the ports and we now
2118 * want to include hashing on port numbers.
2120 static void nat_tabmove(fin, nat)
2124 register u_short sport, dport;
2128 nflags = nat->nat_flags;
2130 sport = ntohs(fin->fin_data[0]);
2131 dport = ntohs(fin->fin_data[1]);
2134 * Remove the NAT entry from the old location
2136 if (nat->nat_hnext[0])
2137 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2138 *nat->nat_phnext[0] = nat->nat_hnext[0];
2140 if (nat->nat_hnext[1])
2141 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2142 *nat->nat_phnext[1] = nat->nat_hnext[1];
2145 * Add into the NAT table in the new position
2147 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2148 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2149 natp = &nat_table[0][hv];
2151 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2152 nat->nat_phnext[0] = natp;
2153 nat->nat_hnext[0] = *natp;
2156 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2157 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2158 natp = &nat_table[1][hv];
2160 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2161 nat->nat_phnext[1] = natp;
2162 nat->nat_hnext[1] = *natp;
2168 * Lookup a nat entry based on the source 'real' ip address/port and
2169 * destination address/port. We use this lookup when sending a packet out,
2170 * we're looking for a table entry, based on the source address.
2171 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2173 nat_t *nat_outlookup(fin, flags, p, src, dst, rw)
2175 register u_int flags, p;
2176 struct in_addr src , dst;
2179 register u_short sport, dport;
2180 register nat_t *nat;
2181 register int nflags;
2189 if (flags & IPN_TCPUDP) {
2190 sport = ntohs(fin->fin_data[0]);
2191 dport = ntohs(fin->fin_data[1]);
2197 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2198 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2199 nat = nat_table[0][hv];
2200 for (; nat; nat = nat->nat_hnext[0]) {
2201 nflags = nat->nat_flags;
2203 if ((!ifp || ifp == nat->nat_ifp) &&
2204 nat->nat_inip.s_addr == srcip &&
2205 nat->nat_oip.s_addr == dst.s_addr &&
2206 ((p == 0) || (p == nat->nat_p))) {
2211 if (nat->nat_oport != dport)
2213 if (nat->nat_inport != sport)
2221 if ((ipn != NULL) && (nat->nat_aps != NULL))
2222 if (appr_match(fin, nat) != 0)
2227 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2230 RWLOCK_EXIT(&ipf_nat);
2233 hv = NAT_HASH_FN(dst.s_addr, srcip, ipf_nattable_sz);
2235 WRITE_ENTER(&ipf_nat);
2237 nat = nat_table[0][hv];
2238 for (; nat; nat = nat->nat_hnext[0]) {
2239 nflags = nat->nat_flags;
2240 if (ifp && ifp != nat->nat_ifp)
2242 if (!(nflags & FI_WILDP))
2244 if ((nat->nat_inip.s_addr != srcip) ||
2245 (nat->nat_oip.s_addr != dst.s_addr))
2247 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2248 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2249 nat_tabmove(fin, nat);
2254 MUTEX_DOWNGRADE(&ipf_nat);
2261 * Lookup the NAT tables to search for a matching redirect
2263 nat_t *nat_lookupredir(np)
2264 register natlookup_t *np;
2269 bzero((char *)&fi, sizeof(fi));
2270 fi.fin_data[0] = np->nl_inport;
2271 fi.fin_data[1] = np->nl_outport;
2274 * If nl_inip is non null, this is a lookup based on the real
2275 * ip address. Else, we use the fake.
2277 if ((nat = nat_outlookup(&fi, np->nl_flags, 0, np->nl_inip,
2278 np->nl_outip, 0))) {
2279 np->nl_realip = nat->nat_outip;
2280 np->nl_realport = nat->nat_outport;
2286 static int nat_match(fin, np, ip)
2296 if (np->in_p && fin->fin_p != np->in_p)
2299 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2301 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2302 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2304 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2305 ^ ((np->in_flags & IPN_NOTDST) != 0))
2308 if (!(np->in_redir & NAT_REDIRECT))
2310 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2311 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2313 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2314 ^ ((np->in_flags & IPN_NOTDST) != 0))
2319 if (!(fin->fin_fl & FI_TCPUDP) ||
2320 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2321 if (ft->ftu_scmp || ft->ftu_dcmp)
2326 return fr_tcpudpchk(ft, fin);
2331 * Packets going out on the external interface go through this.
2332 * Here, the source address requires alteration, if anything.
2334 int ip_natout(ip, fin)
2338 register ipnat_t *np = NULL;
2339 register u_32_t ipa;
2340 tcphdr_t *tcp = NULL;
2341 u_short sport = 0, dport = 0, *csump = NULL;
2342 int natadd = 1, i, icmpset = 1;
2343 u_int nflags = 0, hv, msk;
2350 if (nat_list == NULL || (fr_nat_lock))
2353 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2354 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1) {
2355 sifp = fin->fin_ifp;
2356 fin->fin_ifp = fr->fr_tif.fd_ifp;
2358 sifp = fin->fin_ifp;
2361 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2362 if (fin->fin_p == IPPROTO_TCP)
2364 else if (fin->fin_p == IPPROTO_UDP)
2366 if ((nflags & IPN_TCPUDP)) {
2367 tcp = (tcphdr_t *)fin->fin_dp;
2368 sport = tcp->th_sport;
2369 dport = tcp->th_dport;
2373 ipa = fin->fin_saddr;
2375 READ_ENTER(&ipf_nat);
2377 if ((fin->fin_p == IPPROTO_ICMP) &&
2378 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2380 else if ((fin->fin_fl & FI_FRAG) &&
2381 (nat = ipfr_nat_knownfrag(ip, fin)))
2383 else if ((nat = nat_outlookup(fin, nflags|FI_WILDP|FI_WILDA,
2384 (u_int)fin->fin_p, fin->fin_src,
2385 fin->fin_dst, 0))) {
2386 nflags = nat->nat_flags;
2387 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2388 if ((nflags & FI_W_SPORT) &&
2389 (nat->nat_inport != sport))
2390 nat->nat_inport = sport;
2391 if ((nflags & FI_W_DPORT) &&
2392 (nat->nat_oport != dport))
2393 nat->nat_oport = dport;
2395 if (nat->nat_outport == 0)
2396 nat->nat_outport = sport;
2397 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2398 nflags = nat->nat_flags;
2399 nat_stats.ns_wilds--;
2402 RWLOCK_EXIT(&ipf_nat);
2407 WRITE_ENTER(&ipf_nat);
2409 * If there is no current entry in the nat table for this IP#,
2410 * create one for it (if there is a matching rule).
2413 iph = ipa & htonl(msk);
2414 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2415 for (np = nat_rules[hv]; np; np = np->in_mnext)
2417 if (np->in_ifp && (np->in_ifp != ifp))
2419 if ((np->in_flags & IPN_RF) &&
2420 !(np->in_flags & nflags))
2422 if (np->in_flags & IPN_FILTER) {
2423 if (!nat_match(fin, np, ip))
2425 } else if ((ipa & np->in_inmsk) != np->in_inip)
2427 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2429 nat = nat_new(fin, ip, np, NULL,
2430 (u_int)nflags, NAT_OUTBOUND);
2436 if ((np == NULL) && (i > 0)) {
2440 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2444 MUTEX_DOWNGRADE(&ipf_nat);
2448 * NOTE: ipf_nat must now only be held as a read lock
2452 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2453 ipfr_nat_newfrag(ip, fin, 0, nat);
2454 MUTEX_ENTER(&nat->nat_lock);
2455 if (fin->fin_p != IPPROTO_TCP) {
2456 if (np && np->in_age[1])
2457 nat->nat_age = np->in_age[1];
2458 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2459 nat->nat_age = fr_defnaticmpage;
2461 nat->nat_age = fr_defnatage;
2463 nat->nat_bytes += ip->ip_len;
2465 MUTEX_EXIT(&nat->nat_lock);
2468 * Fix up checksums, not by recalculating them, but
2469 * simply computing adjustments.
2471 if (nflags == IPN_ICMPERR) {
2472 u_32_t s1, s2, sumd;
2474 s1 = LONG_SUM(ntohl(fin->fin_saddr));
2475 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2476 CALC_SUMD(s1, s2, sumd);
2478 if (nat->nat_dir == NAT_OUTBOUND)
2479 fix_outcksum(fin, &ip->ip_sum, sumd);
2481 fix_incksum(fin, &ip->ip_sum, sumd);
2483 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
2485 if (nat->nat_dir == NAT_OUTBOUND)
2486 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2488 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2492 * Only change the packet contents, not what is filtered upon.
2494 ip->ip_src = nat->nat_outip;
2496 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2498 if ((nat->nat_outport != 0) && (tcp != NULL)) {
2499 tcp->th_sport = nat->nat_outport;
2500 fin->fin_data[0] = ntohs(tcp->th_sport);
2503 if (fin->fin_p == IPPROTO_TCP) {
2504 csump = &tcp->th_sum;
2505 MUTEX_ENTER(&nat->nat_lock);
2506 fr_tcp_age(&nat->nat_age,
2507 nat->nat_tcpstate, fin, 1, 0);
2508 if (nat->nat_age < fr_defnaticmpage)
2509 nat->nat_age = fr_defnaticmpage;
2511 else if (nat->nat_age > fr_defnatage)
2512 nat->nat_age = fr_defnatage;
2515 * Increase this because we may have
2516 * "keep state" following this too and
2517 * packet storms can occur if this is
2518 * removed too quickly.
2520 if (nat->nat_age == fr_tcpclosed)
2521 nat->nat_age = fr_tcplastack;
2522 MUTEX_EXIT(&nat->nat_lock);
2523 } else if (fin->fin_p == IPPROTO_UDP) {
2524 udphdr_t *udp = (udphdr_t *)tcp;
2527 csump = &udp->uh_sum;
2531 if (nat->nat_dir == NAT_OUTBOUND)
2532 fix_outcksum(fin, csump,
2535 fix_incksum(fin, csump,
2540 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2541 (tcp != NULL && dport == np->in_dport))) {
2542 i = appr_check(ip, fin, nat);
2547 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2548 RWLOCK_EXIT(&ipf_nat); /* READ */
2549 fin->fin_ifp = sifp;
2552 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2553 fin->fin_ifp = sifp;
2559 * Packets coming in from the external interface go through this.
2560 * Here, the destination address requires alteration, if anything.
2562 int ip_natin(ip, fin)
2566 register struct in_addr src;
2567 register struct in_addr in;
2568 register ipnat_t *np;
2569 u_short sport = 0, dport = 0, *csump = NULL;
2570 u_int nflags = 0, natadd = 1, hv, msk;
2571 struct ifnet *ifp = fin->fin_ifp;
2572 tcphdr_t *tcp = NULL;
2577 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2580 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2581 if (fin->fin_p == IPPROTO_TCP)
2583 else if (fin->fin_p == IPPROTO_UDP)
2585 if ((nflags & IPN_TCPUDP)) {
2586 tcp = (tcphdr_t *)fin->fin_dp;
2587 sport = tcp->th_sport;
2588 dport = tcp->th_dport;
2593 /* make sure the source address is to be redirected */
2596 READ_ENTER(&ipf_nat);
2598 if ((fin->fin_p == IPPROTO_ICMP) &&
2599 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2601 else if ((fin->fin_fl & FI_FRAG) &&
2602 (nat = ipfr_nat_knownfrag(ip, fin)))
2604 else if ((nat = nat_inlookup(fin, nflags|FI_WILDP|FI_WILDA,
2605 (u_int)fin->fin_p, fin->fin_src, in, 0))) {
2606 nflags = nat->nat_flags;
2607 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2608 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2609 nat->nat_oport = sport;
2610 if ((nat->nat_outport != dport) &&
2611 (nflags & FI_W_SPORT))
2612 nat->nat_outport = dport;
2613 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2614 nflags = nat->nat_flags;
2615 nat_stats.ns_wilds--;
2618 RWLOCK_EXIT(&ipf_nat);
2623 WRITE_ENTER(&ipf_nat);
2625 * If there is no current entry in the nat table for this IP#,
2626 * create one for it (if there is a matching rule).
2629 iph = in.s_addr & htonl(msk);
2630 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2631 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2632 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2633 (np->in_p && (np->in_p != fin->fin_p)) ||
2634 (np->in_flags && !(nflags & np->in_flags)))
2636 if (np->in_flags & IPN_FILTER) {
2637 if (!nat_match(fin, np, ip))
2639 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2641 if ((!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2642 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2643 (ntohs(dport) >= ntohs(np->in_pmin)))))
2644 if ((nat = nat_new(fin, ip, np, NULL, nflags,
2651 if ((np == NULL) && (i > 0)) {
2655 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2659 MUTEX_DOWNGRADE(&ipf_nat);
2663 * NOTE: ipf_nat must now only be held as a read lock
2667 fin->fin_fr = nat->nat_fr;
2668 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2669 ipfr_nat_newfrag(ip, fin, 0, nat);
2670 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2671 (tcp != NULL && sport == np->in_dport))) {
2672 i = appr_check(ip, fin, nat);
2674 RWLOCK_EXIT(&ipf_nat);
2679 MUTEX_ENTER(&nat->nat_lock);
2680 if (fin->fin_p != IPPROTO_TCP) {
2681 if (np && np->in_age[0])
2682 nat->nat_age = np->in_age[0];
2683 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2684 nat->nat_age = fr_defnaticmpage;
2686 nat->nat_age = fr_defnatage;
2688 nat->nat_bytes += ip->ip_len;
2690 MUTEX_EXIT(&nat->nat_lock);
2691 ip->ip_dst = nat->nat_inip;
2692 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2695 * Fix up checksums, not by recalculating them, but
2696 * simply computing adjustments.
2698 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
2699 if (nat->nat_dir == NAT_OUTBOUND)
2700 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2702 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2704 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2706 if ((nat->nat_inport != 0) && (tcp != NULL)) {
2707 tcp->th_dport = nat->nat_inport;
2708 fin->fin_data[1] = ntohs(tcp->th_dport);
2711 if (fin->fin_p == IPPROTO_TCP) {
2712 csump = &tcp->th_sum;
2713 MUTEX_ENTER(&nat->nat_lock);
2714 fr_tcp_age(&nat->nat_age,
2715 nat->nat_tcpstate, fin, 0, 0);
2716 if (nat->nat_age < fr_defnaticmpage)
2717 nat->nat_age = fr_defnaticmpage;
2719 else if (nat->nat_age > fr_defnatage)
2720 nat->nat_age = fr_defnatage;
2723 * Increase this because we may have
2724 * "keep state" following this too and
2725 * packet storms can occur if this is
2726 * removed too quickly.
2728 if (nat->nat_age == fr_tcpclosed)
2729 nat->nat_age = fr_tcplastack;
2730 MUTEX_EXIT(&nat->nat_lock);
2731 } else if (fin->fin_p == IPPROTO_UDP) {
2732 udphdr_t *udp = (udphdr_t *)tcp;
2735 csump = &udp->uh_sum;
2739 if (nat->nat_dir == NAT_OUTBOUND)
2740 fix_incksum(fin, csump,
2743 fix_outcksum(fin, csump,
2747 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2748 RWLOCK_EXIT(&ipf_nat); /* READ */
2751 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2757 * Free all memory used by NAT structures allocated at runtime.
2761 WRITE_ENTER(&ipf_nat);
2762 (void) nat_clearlist();
2763 (void) nat_flushtable();
2764 RWLOCK_EXIT(&ipf_nat);
2766 if (nat_table[0] != NULL) {
2767 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2768 nat_table[0] = NULL;
2770 if (nat_table[1] != NULL) {
2771 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2772 nat_table[1] = NULL;
2774 if (nat_rules != NULL) {
2775 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2778 if (rdr_rules != NULL) {
2779 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2782 if (maptable != NULL) {
2783 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2790 * Slowly expire held state for NAT entries. Timeouts are set in
2791 * expectation of this being called twice per second.
2795 register struct nat *nat, **natp;
2796 #if defined(_KERNEL) && !SOLARIS
2801 WRITE_ENTER(&ipf_nat);
2802 for (natp = &nat_instances; (nat = *natp); ) {
2805 natp = &nat->nat_next;
2808 *natp = nat->nat_next;
2810 nat_log(nat, NL_EXPIRE);
2813 nat_stats.ns_expire++;
2815 RWLOCK_EXIT(&ipf_nat);
2822 void ip_natsync(ifp)
2825 register ipnat_t *n;
2826 register nat_t *nat;
2827 register u_32_t sum1, sum2, sumd;
2831 #if defined(_KERNEL) && !SOLARIS
2836 * Change IP addresses for NAT sessions for any protocol except TCP
2837 * since it will break the TCP connection anyway.
2840 WRITE_ENTER(&ipf_nat);
2841 for (nat = nat_instances; nat; nat = nat->nat_next)
2842 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2843 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2844 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2845 ifp2 = nat->nat_ifp;
2847 * Change the map-to address to be the same as the
2850 sum1 = nat->nat_outip.s_addr;
2851 if (fr_ifpaddr(4, ifp2, &in) != -1)
2852 nat->nat_outip = in;
2853 sum2 = nat->nat_outip.s_addr;
2858 * Readjust the checksum adjustment to take into
2859 * account the new IP#.
2861 CALC_SUMD(sum1, sum2, sumd);
2862 /* XXX - dont change for TCP when solaris does
2863 * hardware checksumming.
2865 sumd += nat->nat_sumd[0];
2866 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2867 nat->nat_sumd[1] = nat->nat_sumd[0];
2870 for (n = nat_list; (n != NULL); n = n->in_next)
2871 if (n->in_ifp == ifp) {
2872 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2874 n->in_ifp = (void *)-1;
2876 RWLOCK_EXIT(&ipf_nat);
2882 void nat_log(nat, type)
2890 int rulen, types[1];
2892 natl.nl_inip = nat->nat_inip;
2893 natl.nl_outip = nat->nat_outip;
2894 natl.nl_origip = nat->nat_oip;
2895 natl.nl_bytes = nat->nat_bytes;
2896 natl.nl_pkts = nat->nat_pkts;
2897 natl.nl_origport = nat->nat_oport;
2898 natl.nl_inport = nat->nat_inport;
2899 natl.nl_outport = nat->nat_outport;
2900 natl.nl_p = nat->nat_p;
2901 natl.nl_type = type;
2904 if (nat->nat_ptr != NULL) {
2905 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2906 if (np == nat->nat_ptr) {
2907 natl.nl_rule = rulen;
2913 sizes[0] = sizeof(natl);
2916 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);
2921 #if defined(__OpenBSD__)
2922 void nat_ifdetach(ifp)