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)
8 * @(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed
9 * @(#)$Id: ip_nat.c,v 2.37.2.70 2002/08/28 12:45:48 darrenr Exp $
10 * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_nat.c,v 1.22.2.8 2004/07/04 09:24:39 darrenr Exp $
11 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_nat.c,v 1.11 2005/06/05 12:17:46 corecode Exp $
13 #if (defined(__DragonFly__) || defined(__FreeBSD__)) && defined(KERNEL) && !defined(_KERNEL)
17 #if defined(__sgi) && (IRIX > 602)
18 # include <sys/ptimers.h>
20 #include <sys/errno.h>
21 #include <sys/types.h>
22 #include <sys/param.h>
25 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
27 # include "opt_ipfilter_log.h"
29 #if !defined(_KERNEL) && !defined(KERNEL)
34 #if (defined(KERNEL) || defined(_KERNEL)) && (defined(__DragonFly__) || __FreeBSD_version >= 220000)
35 # include <sys/filio.h>
36 # include <sys/fcntl.h>
38 # include <sys/ioctl.h>
40 #include <sys/fcntl.h>
42 # include <sys/protosw.h>
44 #include <sys/socket.h>
45 #if defined(_KERNEL) && !defined(linux)
46 # include <sys/systm.h>
48 #if !defined(__SVR4) && !defined(__svr4__)
50 # include <sys/mbuf.h>
53 # include <sys/filio.h>
54 # include <sys/byteorder.h>
56 # include <sys/dditypes.h>
58 # include <sys/stream.h>
59 # include <sys/kmem.h>
61 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
62 # include <sys/queue.h>
64 #if defined(__DragonFly__) && defined(_KERNEL)
65 # include <sys/thread2.h>
68 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
69 # include <net/if_var.h>
70 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
71 # include "opt_ipfilter.h"
77 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
83 # ifdef IFF_DRVRLOCK /* IRIX6 */
84 #include <sys/hashing.h>
85 #include <netinet/in_var.h>
91 # include <vpn/ipsec.h>
92 extern struct ifnet vpnif;
96 # include <netinet/ip_var.h>
97 # include <netinet/tcp_fsm.h>
99 #include <netinet/tcp.h>
100 #include <netinet/udp.h>
101 #include <netinet/ip_icmp.h>
102 #include "ip_compat.h"
103 #include <netinet/tcpip.h>
107 #include "ip_state.h"
108 #include "ip_proxy.h"
109 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
110 # include <sys/malloc.h>
113 # define MIN(a,b) (((a)<(b))?(a):(b))
116 #define SOCKADDR_IN struct sockaddr_in
118 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
120 nat_t **nat_table[2] = { NULL, NULL },
121 *nat_instances = NULL;
122 ipnat_t *nat_list = NULL;
123 u_int ipf_nattable_max = NAT_TABLE_MAX;
124 u_int ipf_nattable_sz = NAT_TABLE_SZ;
125 u_int ipf_natrules_sz = NAT_SIZE;
126 u_int ipf_rdrrules_sz = RDR_SIZE;
127 u_int ipf_hostmap_sz = HOSTMAP_SIZE;
128 u_32_t nat_masks = 0;
129 u_32_t rdr_masks = 0;
130 ipnat_t **nat_rules = NULL;
131 ipnat_t **rdr_rules = NULL;
132 hostmap_t **maptable = NULL;
134 u_long fr_defnatage = DEF_NAT_AGE,
135 fr_defnaticmpage = 6; /* 3 seconds */
138 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
139 extern kmutex_t ipf_rw;
140 extern KRWLOCK_T ipf_nat;
143 static int nat_flushtable (void);
144 static void nat_addnat (struct ipnat *);
145 static void nat_addrdr (struct ipnat *);
146 static void nat_delete (struct nat *);
147 static void nat_delrdr (struct ipnat *);
148 static void nat_delnat (struct ipnat *);
149 static int fr_natgetent (caddr_t);
150 static int fr_natgetsz (caddr_t);
151 static int fr_natputent (caddr_t);
152 static void nat_tabmove (fr_info_t *, nat_t *);
153 static int nat_match (fr_info_t *, ipnat_t *, ip_t *);
154 static hostmap_t *nat_hostmap (ipnat_t *, struct in_addr,
156 static void nat_hostmapdel (struct hostmap *);
157 static void nat_mssclamp (tcphdr_t *, u_32_t, fr_info_t *, u_short *);
162 KMALLOCS(nat_table[0], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
163 if (nat_table[0] != NULL)
164 bzero((char *)nat_table[0], ipf_nattable_sz * sizeof(nat_t *));
168 KMALLOCS(nat_table[1], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
169 if (nat_table[1] != NULL)
170 bzero((char *)nat_table[1], ipf_nattable_sz * sizeof(nat_t *));
174 KMALLOCS(nat_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_natrules_sz);
175 if (nat_rules != NULL)
176 bzero((char *)nat_rules, ipf_natrules_sz * sizeof(ipnat_t *));
180 KMALLOCS(rdr_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_rdrrules_sz);
181 if (rdr_rules != NULL)
182 bzero((char *)rdr_rules, ipf_rdrrules_sz * sizeof(ipnat_t *));
186 KMALLOCS(maptable, hostmap_t **, sizeof(hostmap_t *) * ipf_hostmap_sz);
187 if (maptable != NULL)
188 bzero((char *)maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
195 static void nat_addrdr(n)
203 k = countbits(n->in_outmsk);
204 if ((k >= 0) && (k != 32))
206 j = (n->in_outip & n->in_outmsk);
207 hv = NAT_HASH_FN(j, 0, ipf_rdrrules_sz);
210 np = &(*np)->in_rnext;
217 static void nat_addnat(n)
225 k = countbits(n->in_inmsk);
226 if ((k >= 0) && (k != 32))
228 j = (n->in_inip & n->in_inmsk);
229 hv = NAT_HASH_FN(j, 0, ipf_natrules_sz);
232 np = &(*np)->in_mnext;
239 static void nat_delrdr(n)
243 n->in_rnext->in_prnext = n->in_prnext;
244 *n->in_prnext = n->in_rnext;
248 static void nat_delnat(n)
252 n->in_mnext->in_pmnext = n->in_pmnext;
253 *n->in_pmnext = n->in_mnext;
258 * check if an ip address has already been allocated for a given mapping that
259 * is not doing port based translation.
261 * Must be called with ipf_nat held as a write lock.
263 static struct hostmap *nat_hostmap(np, real, map)
271 hv = real.s_addr % HOSTMAP_SIZE;
272 for (hm = maptable[hv]; hm; hm = hm->hm_next)
273 if ((hm->hm_realip.s_addr == real.s_addr) &&
274 (np == hm->hm_ipnat)) {
279 KMALLOC(hm, hostmap_t *);
281 hm->hm_next = maptable[hv];
282 hm->hm_pnext = maptable + hv;
284 maptable[hv]->hm_pnext = &hm->hm_next;
287 hm->hm_realip = real;
296 * Must be called with ipf_nat held as a write lock.
298 static void nat_hostmapdel(hm)
301 ATOMIC_DEC32(hm->hm_ref);
302 if (hm->hm_ref == 0) {
304 hm->hm_next->hm_pnext = hm->hm_pnext;
305 *hm->hm_pnext = hm->hm_next;
311 void fix_outcksum(fin, sp, n)
321 else if (n & NAT_HW_CKSUM) {
324 n = (n & 0xffff) + (n >> 16);
328 sum1 = (~ntohs(*sp)) & 0xffff;
330 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
332 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
333 sumshort = ~(u_short)sum1;
334 *(sp) = htons(sumshort);
338 void fix_incksum(fin, sp, n)
348 else if (n & NAT_HW_CKSUM) {
351 n = (n & 0xffff) + (n >> 16);
356 sum1 = (~(*sp)) & 0xffff;
358 sum1 = (~ntohs(*sp)) & 0xffff;
360 sum1 += ~(n) & 0xffff;
361 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
363 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
364 sumshort = ~(u_short)sum1;
365 *(sp) = htons(sumshort);
370 * fix_datacksum is used *only* for the adjustments of checksums in the data
371 * section of an IP packet.
373 * The only situation in which you need to do this is when NAT'ing an
374 * ICMP error message. Such a message, contains in its body the IP header
375 * of the original IP packet, that causes the error.
377 * You can't use fix_incksum or fix_outcksum in that case, because for the
378 * kernel the data section of the ICMP error is just data, and no special
379 * processing like hardware cksum or ntohs processing have been done by the
380 * kernel on the data section.
382 void fix_datacksum(sp, n)
392 sum1 = (~ntohs(*sp)) & 0xffff;
394 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
396 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
397 sumshort = ~(u_short)sum1;
398 *(sp) = htons(sumshort);
402 * How the NAT is organised and works.
404 * Inside (interface y) NAT Outside (interface x)
405 * -------------------- -+- -------------------------------------
406 * Packet going | out, processsed by ip_natout() for x
407 * ------------> | ------------>
408 * src=10.1.1.1 | src=192.1.1.1
410 * | in, processed by ip_natin() for x
411 * <------------ | <------------
412 * dst=10.1.1.1 | dst=192.1.1.1
413 * -------------------- -+- -------------------------------------
414 * ip_natout() - changes ip_src and if required, sport
415 * - creates a new mapping, if required.
416 * ip_natin() - changes ip_dst and if required, dport
418 * In the NAT table, internal source is recorded as "in" and externally
423 * Handle ioctls which manipulate the NAT.
425 int nat_ioctl(data, cmd, mode)
426 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
434 ipnat_t *nat, *nt, *n = NULL, **np = NULL;
435 int error = 0, ret, arg, getlock;
439 #if (BSD >= 199306) && defined(_KERNEL)
440 if ((securelevel >= 3) && (mode & FWRITE))
444 nat = NULL; /* XXX gcc -Wuninitialized */
445 KMALLOC(nt, ipnat_t *);
446 getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
447 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
448 if (mode & NAT_SYSSPACE) {
449 bcopy(data, (char *)&natd, sizeof(natd));
452 error = IRCOPYPTR(data, (char *)&natd, sizeof(natd));
454 } else if (cmd == SIOCIPFFL) { /* SIOCFLNAT & SIOCCNATL */
455 error = IRCOPY(data, (char *)&arg, sizeof(arg));
464 * For add/delete, look to see if the NAT entry is already present
467 WRITE_ENTER(&ipf_nat);
469 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
471 nat->in_flags &= IPN_USERFLAGS;
472 if ((nat->in_redir & NAT_MAPBLK) == 0) {
473 if ((nat->in_flags & IPN_SPLIT) == 0)
474 nat->in_inip &= nat->in_inmsk;
475 if ((nat->in_flags & IPN_IPRANGE) == 0)
476 nat->in_outip &= nat->in_outmsk;
478 for (np = &nat_list; (n = *np); np = &n->in_next)
479 if (!bcmp((char *)&nat->in_flags, (char *)&n->in_flags,
481 if (n->in_redir == NAT_REDIRECT &&
482 n->in_pnext != nat->in_pnext)
495 if (!(mode & FWRITE))
498 tmp = ipflog_clear(IPL_LOGNAT);
499 IWCOPY((char *)&tmp, (char *)data, sizeof(tmp));
505 if (!(mode & FWRITE)) {
519 bcopy((char *)nat, (char *)n, sizeof(*n));
520 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
522 n->in_ifp = (void *)-1;
523 if (n->in_plabel[0] != '\0') {
524 n->in_apr = appr_lookup(n->in_p, n->in_plabel);
533 if (n->in_redir & NAT_REDIRECT) {
534 n->in_flags &= ~IPN_NOTDST;
537 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
538 n->in_flags &= ~IPN_NOTSRC;
543 if (n->in_redir & NAT_MAPBLK)
544 n->in_space = USABLE_PORTS * ~ntohl(n->in_outmsk);
545 else if (n->in_flags & IPN_AUTOPORTMAP)
546 n->in_space = USABLE_PORTS * ~ntohl(n->in_inmsk);
547 else if (n->in_flags & IPN_IPRANGE)
548 n->in_space = ntohl(n->in_outmsk) - ntohl(n->in_outip);
549 else if (n->in_flags & IPN_SPLIT)
552 n->in_space = ~ntohl(n->in_outmsk);
554 * Calculate the number of valid IP addresses in the output
555 * mapping range. In all cases, the range is inclusive of
556 * the start and ending IP addresses.
557 * If to a CIDR address, lose 2: broadcast + network address
559 * If to a range, add one.
560 * If to a single IP address, set to 1.
563 if ((n->in_flags & IPN_IPRANGE) != 0)
569 if ((n->in_outmsk != 0xffffffff) && (n->in_outmsk != 0) &&
570 ((n->in_flags & (IPN_IPRANGE|IPN_SPLIT)) == 0))
571 n->in_nip = ntohl(n->in_outip) + 1;
572 else if ((n->in_flags & IPN_SPLIT) &&
573 (n->in_redir & NAT_REDIRECT))
574 n->in_nip = ntohl(n->in_inip);
576 n->in_nip = ntohl(n->in_outip);
577 if (n->in_redir & NAT_MAP) {
578 n->in_pnext = ntohs(n->in_pmin);
580 * Multiply by the number of ports made available.
582 if (ntohs(n->in_pmax) >= ntohs(n->in_pmin)) {
583 n->in_space *= (ntohs(n->in_pmax) -
584 ntohs(n->in_pmin) + 1);
586 * Because two different sources can map to
587 * different destinations but use the same
589 * If the result is smaller than in_space, then
590 * we may have wrapped around 32bits.
593 if ((i != 0) && (i != 0xffffffff)) {
594 j = n->in_space * (~ntohl(i) + 1);
595 if (j >= n->in_space)
598 n->in_space = 0xffffffff;
602 * If no protocol is specified, multiple by 256.
604 if ((n->in_flags & IPN_TCPUDP) == 0) {
605 j = n->in_space * 256;
606 if (j >= n->in_space)
609 n->in_space = 0xffffffff;
612 /* Otherwise, these fields are preset */
614 nat_stats.ns_rules++;
617 if (!(mode & FWRITE)) {
626 if (n->in_redir & NAT_REDIRECT)
628 if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
630 if (nat_list == NULL) {
637 appr_free(n->in_apr);
639 nat_stats.ns_rules--;
641 n->in_flags |= IPN_DELETE;
647 MUTEX_DOWNGRADE(&ipf_nat);
648 nat_stats.ns_table[0] = nat_table[0];
649 nat_stats.ns_table[1] = nat_table[1];
650 nat_stats.ns_list = nat_list;
651 nat_stats.ns_maptable = maptable;
652 nat_stats.ns_nattab_sz = ipf_nattable_sz;
653 nat_stats.ns_rultab_sz = ipf_natrules_sz;
654 nat_stats.ns_rdrtab_sz = ipf_rdrrules_sz;
655 nat_stats.ns_hostmap_sz = ipf_hostmap_sz;
656 nat_stats.ns_instances = nat_instances;
657 nat_stats.ns_apslist = ap_sess_list;
658 error = IWCOPYPTR((char *)&nat_stats, (char *)data,
665 MUTEX_DOWNGRADE(&ipf_nat);
666 error = IRCOPYPTR((char *)data, (char *)&nl, sizeof(nl));
670 if (nat_lookupredir(&nl)) {
671 error = IWCOPYPTR((char *)&nl, (char *)data,
677 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
678 if (!(mode & FWRITE)) {
684 ret = nat_flushtable();
686 ret = nat_clearlist();
689 MUTEX_DOWNGRADE(&ipf_nat);
691 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
697 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
699 error = IWCOPY((caddr_t)&fr_nat_lock, data,
700 sizeof(fr_nat_lock));
708 error = fr_natputent(data);
714 error = fr_natgetsz(data);
720 error = fr_natgetent(data);
726 arg = (int)iplused[IPL_LOGNAT];
727 MUTEX_DOWNGRADE(&ipf_nat);
728 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
738 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
747 static int fr_natgetsz(data)
755 error = IRCOPY(data, (caddr_t)&ng, sizeof(ng));
764 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
771 * Make sure the pointer we're copying from exists in the
772 * current list of entries. Security precaution to prevent
773 * copying of random kernel data.
775 for (n = nat_instances; n; n = n->nat_next)
782 ng.ng_sz = sizeof(nat_save_t);
784 if ((aps != NULL) && (aps->aps_data != 0)) {
785 ng.ng_sz += sizeof(ap_session_t);
786 ng.ng_sz += aps->aps_psiz;
787 if (aps->aps_psiz > 4) /* XXX - sizeof(ipn_data) */
791 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
798 static int fr_natgetent(data)
801 nat_save_t ipn, *ipnp, *ipnn = NULL;
807 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
810 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
818 if (nat_instances == NULL)
824 * Make sure the pointer we're copying from exists in the
825 * current list of entries. Security precaution to prevent
826 * copying of random kernel data.
828 for (n = nat_instances; n; n = n->nat_next)
835 ipn.ipn_next = nat->nat_next;
836 bcopy((char *)nat, (char *)&ipn.ipn_nat, sizeof(ipn.ipn_nat));
837 ipn.ipn_nat.nat_data = NULL;
840 bcopy((char *)nat->nat_ptr, (char *)&ipn.ipn_ipnat,
841 sizeof(ipn.ipn_ipnat));
845 bcopy((char *)nat->nat_fr, (char *)&ipn.ipn_rule,
846 sizeof(ipn.ipn_rule));
848 if ((aps = nat->nat_aps)) {
851 dsz += aps->aps_psiz;
853 if (dsz > sizeof(ipn.ipn_data))
854 dsz -= sizeof(ipn.ipn_data);
855 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + dsz);
858 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
860 bcopy((char *)aps, (char *)ipnn->ipn_data, sizeof(*aps));
862 bcopy(aps->aps_data, ipnn->ipn_data + sizeof(*aps),
865 error = IWCOPY((caddr_t)ipnn, ipnp,
869 KFREES(ipnn, sizeof(*ipnn) + dsz);
872 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
880 static int fr_natputent(data)
883 nat_save_t ipn, *ipnp, *ipnn = NULL;
891 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
894 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
899 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + ipn.ipn_dsize);
902 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
903 error = IRCOPY((caddr_t)ipnp + offsetof(nat_save_t, ipn_data),
904 (caddr_t)ipnn->ipn_data, ipn.ipn_dsize);
912 KMALLOC(nat, nat_t *);
918 bcopy((char *)&ipn.ipn_nat, (char *)nat, sizeof(*nat));
920 * Initialize all these so that nat_delete() doesn't cause a crash.
922 nat->nat_phnext[0] = NULL;
923 nat->nat_phnext[1] = NULL;
931 nat->nat_data = NULL;
932 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
935 * Restore the rule associated with this nat session
938 KMALLOC(in, ipnat_t *);
944 bcopy((char *)&ipn.ipn_ipnat, (char *)in, sizeof(*in));
946 in->in_flags |= IPN_DELETE;
949 in->in_prnext = NULL;
951 in->in_pmnext = NULL;
952 in->in_ifp = GETUNIT(in->in_ifname, 4);
953 if (in->in_plabel[0] != '\0') {
954 in->in_apr = appr_lookup(in->in_p, in->in_plabel);
959 * Restore ap_session_t structure. Include the private data allocated
963 KMALLOC(aps, ap_session_t *);
969 aps->aps_next = ap_sess_list;
971 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
973 aps->aps_apr = in->in_apr;
975 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
976 if (aps->aps_data == NULL) {
980 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
984 aps->aps_data = NULL;
989 * If there was a filtering rule associated with this entry then
990 * build up a new one.
993 if (nat->nat_flags & FI_NEWFR) {
994 KMALLOC(fr, frentry_t *);
1000 bcopy((char *)&ipn.ipn_fr, (char *)fr, sizeof(*fr));
1001 ipn.ipn_nat.nat_fr = fr;
1002 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
1008 for (n = nat_instances; n; n = n->nat_next)
1009 if (n->nat_fr == fr)
1019 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1024 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1032 * Delete a nat entry from the various lists and table.
1034 static void nat_delete(natd)
1039 if (natd->nat_flags & FI_WILDP)
1040 nat_stats.ns_wilds--;
1041 if (natd->nat_hnext[0])
1042 natd->nat_hnext[0]->nat_phnext[0] = natd->nat_phnext[0];
1043 *natd->nat_phnext[0] = natd->nat_hnext[0];
1044 if (natd->nat_hnext[1])
1045 natd->nat_hnext[1]->nat_phnext[1] = natd->nat_phnext[1];
1046 *natd->nat_phnext[1] = natd->nat_hnext[1];
1047 if (natd->nat_me != NULL)
1048 *natd->nat_me = NULL;
1050 if (natd->nat_fr != NULL) {
1051 ATOMIC_DEC32(natd->nat_fr->fr_ref);
1054 if (natd->nat_hm != NULL)
1055 nat_hostmapdel(natd->nat_hm);
1058 * If there is an active reference from the nat entry to its parent
1059 * rule, decrement the rule's reference count and free it too if no
1060 * longer being used.
1062 ipn = natd->nat_ptr;
1066 if (!ipn->in_use && (ipn->in_flags & IPN_DELETE)) {
1068 appr_free(ipn->in_apr);
1070 nat_stats.ns_rules--;
1074 MUTEX_DESTROY(&natd->nat_lock);
1076 * If there's a fragment table entry too for this nat entry, then
1077 * dereference that as well.
1079 ipfr_forgetnat((void *)natd);
1080 aps_free(natd->nat_aps);
1081 nat_stats.ns_inuse--;
1087 * nat_flushtable - clear the NAT table of all mapping entries.
1088 * (this is for the dynamic mappings)
1090 static int nat_flushtable()
1096 * ALL NAT mappings deleted, so lets just make the deletions
1099 if (nat_table[0] != NULL)
1100 bzero((char *)nat_table[0],
1101 sizeof(nat_table[0]) * ipf_nattable_sz);
1102 if (nat_table[1] != NULL)
1103 bzero((char *)nat_table[1],
1104 sizeof(nat_table[1]) * ipf_nattable_sz);
1106 for (natp = &nat_instances; (nat = *natp); ) {
1107 *natp = nat->nat_next;
1109 nat_log(nat, NL_FLUSH);
1114 nat_stats.ns_inuse = 0;
1120 * nat_clearlist - delete all rules in the active NAT mapping list.
1121 * (this is for NAT/RDR rules)
1125 ipnat_t *n, **np = &nat_list;
1128 if (nat_rules != NULL)
1129 bzero((char *)nat_rules, sizeof(*nat_rules) * ipf_natrules_sz);
1130 if (rdr_rules != NULL)
1131 bzero((char *)rdr_rules, sizeof(*rdr_rules) * ipf_rdrrules_sz);
1137 appr_free(n->in_apr);
1139 nat_stats.ns_rules--;
1141 n->in_flags |= IPN_DELETE;
1153 * Create a new NAT table entry.
1154 * NOTE: Assumes write lock on ipf_nat has been obtained already.
1155 * If you intend on changing this, beware: appr_new() may call nat_new()
1158 nat_t *nat_new(fin, ip, np, natsave, flags, direction)
1166 u_32_t sum1, sum2, sumd, l;
1167 u_short port = 0, sport = 0, dport = 0, nport = 0;
1168 struct in_addr in, inb;
1169 u_short nflags, sp, dp;
1170 tcphdr_t *tcp = NULL;
1171 hostmap_t *hm = NULL;
1173 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1174 qif_t *qf = fin->fin_qif;
1177 if (nat_stats.ns_inuse >= ipf_nattable_max) {
1178 nat_stats.ns_memfail++;
1182 nflags = flags & np->in_flags;
1183 if (flags & IPN_TCPUDP) {
1184 tcp = (tcphdr_t *)fin->fin_dp;
1185 sport = htons(fin->fin_data[0]);
1186 dport = htons(fin->fin_data[1]);
1189 /* Give me a new nat */
1190 KMALLOC(nat, nat_t *);
1192 nat_stats.ns_memfail++;
1194 * Try to automatically tune the max # of entries in the
1195 * table allowed to be less than what will cause kmem_alloc()
1196 * to fail and try to eliminate panics due to out of memory
1197 * conditions arising.
1199 if (ipf_nattable_max > ipf_nattable_sz) {
1200 ipf_nattable_max = nat_stats.ns_inuse - 100;
1201 printf("ipf_nattable_max reduced to %d\n",
1207 bzero((char *)nat, sizeof(*nat));
1208 nat->nat_tcpstate[0] = TCPS_CLOSED;
1209 nat->nat_tcpstate[1] = TCPS_CLOSED;
1210 nat->nat_flags = flags;
1211 if (flags & FI_WILDP)
1212 nat_stats.ns_wilds++;
1214 * Search the current table for a match.
1216 if (direction == NAT_OUTBOUND) {
1218 * Values at which the search for a free resouce starts.
1224 * If it's an outbound packet which doesn't match any existing
1225 * record, then create a new port
1229 st_port = np->in_pnext;
1233 in.s_addr = htonl(np->in_nip);
1236 * Check to see if there is an existing NAT
1237 * setup for this IP address pair.
1239 hm = nat_hostmap(np, fin->fin_src, in);
1241 in.s_addr = hm->hm_mapip.s_addr;
1242 } else if ((l == 1) && (hm != NULL)) {
1246 in.s_addr = ntohl(in.s_addr);
1250 if ((np->in_outmsk == 0xffffffff) &&
1251 (np->in_pnext == 0)) {
1256 if (np->in_redir & NAT_MAPBLK) {
1257 if ((l >= np->in_ppip) || ((l > 0) &&
1258 !(flags & IPN_TCPUDP)))
1261 * map-block - Calculate destination address.
1263 in.s_addr = ntohl(fin->fin_saddr);
1264 in.s_addr &= ntohl(~np->in_inmsk);
1265 inb.s_addr = in.s_addr;
1266 in.s_addr /= np->in_ippip;
1267 in.s_addr &= ntohl(~np->in_outmsk);
1268 in.s_addr += ntohl(np->in_outip);
1270 * Calculate destination port.
1272 if ((flags & IPN_TCPUDP) &&
1273 (np->in_ppip != 0)) {
1274 port = ntohs(sport) + l;
1275 port %= np->in_ppip;
1276 port += np->in_ppip *
1277 (inb.s_addr % np->in_ippip);
1278 port += MAPBLK_MINPORT;
1281 } else if (!np->in_outip &&
1282 (np->in_outmsk == 0xffffffff)) {
1284 * 0/32 - use the interface's IP address.
1287 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1289 in.s_addr = ntohl(in.s_addr);
1290 } else if (!np->in_outip && !np->in_outmsk) {
1292 * 0/0 - use the original source address/port.
1296 in.s_addr = ntohl(fin->fin_saddr);
1297 } else if ((np->in_outmsk != 0xffffffff) &&
1298 (np->in_pnext == 0) &&
1299 ((l > 0) || (hm == NULL)))
1303 if ((nflags & IPN_TCPUDP) &&
1304 ((np->in_redir & NAT_MAPBLK) == 0) &&
1305 (np->in_flags & IPN_AUTOPORTMAP)) {
1306 if ((l > 0) && (l % np->in_ppip == 0)) {
1307 if (l > np->in_space) {
1309 } else if ((l > np->in_ppip) &&
1310 np->in_outmsk != 0xffffffff)
1313 if (np->in_ppip != 0) {
1314 port = ntohs(sport);
1315 port += (l % np->in_ppip);
1316 port %= np->in_ppip;
1317 port += np->in_ppip *
1318 (ntohl(fin->fin_saddr) %
1320 port += MAPBLK_MINPORT;
1323 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1324 (nflags & IPN_TCPUDP) &&
1325 (np->in_pnext != 0)) {
1326 port = htons(np->in_pnext++);
1327 if (np->in_pnext > ntohs(np->in_pmax)) {
1328 np->in_pnext = ntohs(np->in_pmin);
1329 if (np->in_outmsk != 0xffffffff)
1334 if (np->in_flags & IPN_IPRANGE) {
1335 if (np->in_nip > ntohl(np->in_outmsk))
1336 np->in_nip = ntohl(np->in_outip);
1338 if ((np->in_outmsk != 0xffffffff) &&
1339 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1340 ntohl(np->in_outip))
1341 np->in_nip = ntohl(np->in_outip) + 1;
1344 if (!port && (flags & IPN_TCPUDP))
1348 * Here we do a lookup of the connection as seen from
1349 * the outside. If an IP# pair already exists, try
1350 * again. So if you have A->B becomes C->B, you can
1351 * also have D->E become C->E but not D->B causing
1352 * another C->B. Also take protocol and ports into
1353 * account when determining whether a pre-existing
1354 * NAT setup will cause an external conflict where
1355 * this is appropriate.
1357 inb.s_addr = htonl(in.s_addr);
1358 sp = fin->fin_data[0];
1359 dp = fin->fin_data[1];
1360 fin->fin_data[0] = fin->fin_data[1];
1361 fin->fin_data[1] = htons(port);
1362 natl = nat_inlookup(fin, flags & ~FI_WILDP,
1363 (u_int)fin->fin_p, fin->fin_dst,
1365 fin->fin_data[0] = sp;
1366 fin->fin_data[1] = dp;
1369 * Has the search wrapped around and come back to the
1372 if ((natl != NULL) &&
1373 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1374 (np->in_nip != 0) && (st_ip == np->in_nip))
1377 } while (natl != NULL);
1379 if (np->in_space > 0)
1382 /* Setup the NAT table */
1383 nat->nat_inip = fin->fin_src;
1384 nat->nat_outip.s_addr = htonl(in.s_addr);
1385 nat->nat_oip = fin->fin_dst;
1386 if (nat->nat_hm == NULL)
1387 nat->nat_hm = nat_hostmap(np, fin->fin_src,
1390 sum1 = LONG_SUM(ntohl(fin->fin_saddr)) + ntohs(sport);
1391 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1393 if (flags & IPN_TCPUDP) {
1394 nat->nat_inport = sport;
1395 nat->nat_outport = port; /* sport */
1396 nat->nat_oport = dport;
1400 * Otherwise, it's an inbound packet. Most likely, we don't
1401 * want to rewrite source ports and source addresses. Instead,
1402 * we want to rewrite to a fixed internal address and fixed
1405 if (np->in_flags & IPN_SPLIT) {
1406 in.s_addr = np->in_nip;
1407 if (np->in_inip == htonl(in.s_addr))
1408 np->in_nip = ntohl(np->in_inmsk);
1410 np->in_nip = ntohl(np->in_inip);
1411 if (np->in_flags & IPN_ROUNDR) {
1417 in.s_addr = ntohl(np->in_inip);
1418 if (np->in_flags & IPN_ROUNDR) {
1427 * Whilst not optimized for the case where
1428 * pmin == pmax, the gain is not significant.
1430 if (np->in_pmin != np->in_pmax) {
1431 nport = ntohs(dport) - ntohs(np->in_pmin) +
1432 ntohs(np->in_pnext);
1433 nport = ntohs(nport);
1435 nport = np->in_pnext;
1439 * When the redirect-to address is set to 0.0.0.0, just
1440 * assume a blank `forwarding' of the packet.
1443 in.s_addr = ntohl(fin->fin_daddr);
1445 nat->nat_inip.s_addr = htonl(in.s_addr);
1446 nat->nat_outip = fin->fin_dst;
1447 nat->nat_oip = fin->fin_src;
1449 sum1 = LONG_SUM(ntohl(fin->fin_daddr)) + ntohs(dport);
1450 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1452 if (flags & IPN_TCPUDP) {
1453 nat->nat_inport = nport;
1454 nat->nat_outport = dport;
1455 nat->nat_oport = sport;
1459 CALC_SUMD(sum1, sum2, sumd);
1460 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1461 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1462 if ((flags & IPN_TCP) && dohwcksum &&
1463 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1464 if (direction == NAT_OUTBOUND)
1465 sum1 = LONG_SUM(ntohl(in.s_addr));
1467 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1468 sum1 += LONG_SUM(ntohl(fin->fin_daddr));
1469 sum1 += IPPROTO_TCP;
1470 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1471 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1474 nat->nat_sumd[1] = nat->nat_sumd[0];
1476 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1477 if (direction == NAT_OUTBOUND)
1478 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1480 sum1 = LONG_SUM(ntohl(fin->fin_daddr));
1482 sum2 = LONG_SUM(in.s_addr);
1484 CALC_SUMD(sum1, sum2, sumd);
1485 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1487 nat->nat_ipsumd = nat->nat_sumd[0];
1489 in.s_addr = htonl(in.s_addr);
1491 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1493 nat->nat_me = natsave;
1494 nat->nat_dir = direction;
1495 nat->nat_ifp = fin->fin_ifp;
1497 nat->nat_p = fin->fin_p;
1500 nat->nat_mssclamp = np->in_mssclamp;
1501 nat->nat_fr = fin->fin_fr;
1502 if (nat->nat_fr != NULL) {
1503 ATOMIC_INC32(nat->nat_fr->fr_ref);
1505 if (direction == NAT_OUTBOUND) {
1506 if (flags & IPN_TCPUDP)
1507 tcp->th_sport = port;
1509 if (flags & IPN_TCPUDP)
1510 tcp->th_dport = nport;
1515 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
1516 (tcp != NULL && dport == np->in_dport)))
1517 (void) appr_new(fin, ip, nat);
1521 nat_log(nat, (u_int)np->in_redir);
1525 nat_stats.ns_badnat++;
1526 if ((hm = nat->nat_hm) != NULL)
1534 * Insert a NAT entry into the hash tables for searching and add it to the
1535 * list of active NAT entries. Adjust global counters when complete.
1537 void nat_insert(nat)
1543 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1545 nat->nat_age = fr_defnatage;
1546 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1547 if (nat->nat_ifname[0] !='\0') {
1548 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1551 nat->nat_next = nat_instances;
1552 nat_instances = nat;
1554 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1555 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1557 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1559 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1561 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1564 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_inip.s_addr,
1566 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_outip.s_addr,
1570 natp = &nat_table[0][hv1];
1572 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1573 nat->nat_phnext[0] = natp;
1574 nat->nat_hnext[0] = *natp;
1577 natp = &nat_table[1][hv2];
1579 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1580 nat->nat_phnext[1] = natp;
1581 nat->nat_hnext[1] = *natp;
1584 nat_stats.ns_added++;
1585 nat_stats.ns_inuse++;
1589 nat_t *nat_icmplookup(ip, fin, dir)
1595 tcphdr_t *tcp = NULL;
1597 int flags = 0, type, minlen;
1599 icmp = (icmphdr_t *)fin->fin_dp;
1601 * Does it at least have the return (basic) IP header ?
1602 * Only a basic IP header (no options) should be with an ICMP error
1605 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1607 type = icmp->icmp_type;
1609 * If it's not an error type, then return.
1611 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1612 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1613 (type != ICMP_PARAMPROB))
1616 oip = (ip_t *)((char *)fin->fin_dp + 8);
1617 minlen = (oip->ip_hl << 2);
1618 if (minlen < sizeof(ip_t))
1620 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1623 * Is the buffer big enough for all of it ? It's the size of the IP
1624 * header claimed in the encapsulated part which is of concern. It
1625 * may be too big to be in this buffer but not so big that it's
1626 * outside the ICMP packet, leading to TCP deref's causing problems.
1627 * This is possible because we don't know how big oip_hl is when we
1628 * do the pullup early in fr_check() and thus can't gaurantee it is
1637 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1640 m = *(mb_t **)fin->fin_mp;
1641 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1642 (char *)ip + m->m_len)
1648 if (oip->ip_p == IPPROTO_TCP)
1650 else if (oip->ip_p == IPPROTO_UDP)
1652 if (flags & IPN_TCPUDP) {
1656 minlen += 8; /* + 64bits of data to get ports */
1657 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1660 data[0] = fin->fin_data[0];
1661 data[1] = fin->fin_data[1];
1662 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1663 fin->fin_data[0] = ntohs(tcp->th_dport);
1664 fin->fin_data[1] = ntohs(tcp->th_sport);
1666 if (dir == NAT_INBOUND) {
1667 nat = nat_inlookup(fin, flags, (u_int)oip->ip_p,
1668 oip->ip_dst, oip->ip_src, 0);
1670 nat = nat_outlookup(fin, flags, (u_int)oip->ip_p,
1671 oip->ip_dst, oip->ip_src, 0);
1673 fin->fin_data[0] = data[0];
1674 fin->fin_data[1] = data[1];
1677 if (dir == NAT_INBOUND)
1678 return nat_inlookup(fin, 0, (u_int)oip->ip_p,
1679 oip->ip_dst, oip->ip_src, 0);
1681 return nat_outlookup(fin, 0, (u_int)oip->ip_p,
1682 oip->ip_dst, oip->ip_src, 0);
1687 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1688 * packet gets correctly recognised.
1690 nat_t *nat_icmp(ip, fin, nflags, dir)
1696 u_32_t sum1, sum2, sumd, sumd2 = 0;
1705 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1708 * nat_icmplookup() will return NULL for `defective' packets.
1710 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1715 *nflags = IPN_ICMPERR;
1716 icmp = (icmphdr_t *)fin->fin_dp;
1717 oip = (ip_t *)&icmp->icmp_ip;
1718 if (oip->ip_p == IPPROTO_TCP)
1720 else if (oip->ip_p == IPPROTO_UDP)
1722 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1723 dlen = ip->ip_len - ((char *)udp - (char *)ip);
1725 * XXX - what if this is bogus hl and we go off the end ?
1726 * In this case, nat_icmplookup() will have returned NULL.
1728 tcp = (tcphdr_t *)udp;
1731 * Need to adjust ICMP header to include the real IP#'s and
1732 * port #'s. Only apply a checksum change relative to the
1733 * IP address change as it will be modified again in ip_natout
1734 * for both address and port. Two checksum changes are
1735 * necessary for the two header address changes. Be careful
1736 * to only modify the checksum once for the port # and twice
1742 * Fix the IP addresses in the offending IP packet. You also need
1743 * to adjust the IP header checksum of that offending IP packet
1744 * and the ICMP checksum of the ICMP error message itself.
1746 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1747 * in the pseudo header that is used to compute the UDP resp. TCP
1748 * checksum. So, we must compensate that as well. Even worse, the
1749 * change in the UDP and TCP checksums require yet another
1750 * adjustment of the ICMP checksum of the ICMP error message.
1754 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1755 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1759 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1760 in = nat->nat_outip;
1764 sum2 = LONG_SUM(ntohl(in.s_addr));
1766 CALC_SUMD(sum1, sum2, sumd);
1769 * Fix IP checksum of the offending IP packet to adjust for
1770 * the change in the IP address.
1772 * Normally, you would expect that the ICMP checksum of the
1773 * ICMP error message needs to be adjusted as well for the
1774 * IP address change in oip.
1775 * However, this is a NOP, because the ICMP checksum is
1776 * calculated over the complete ICMP packet, which includes the
1777 * changed oip IP addresses and oip->ip_sum. However, these
1778 * two changes cancel each other out (if the delta for
1779 * the IP address is x, then the delta for ip_sum is minus x),
1780 * so no change in the icmp_cksum is necessary.
1782 * Be careful that nat_dir refers to the direction of the
1783 * offending IP packet (oip), not to its ICMP response (icmp)
1785 fix_datacksum(&oip->ip_sum, sumd);
1786 /* Fix icmp cksum : IP Addr + Cksum */
1789 * Fix UDP pseudo header checksum to compensate for the
1790 * IP address change.
1792 if ((oip->ip_p == IPPROTO_UDP) && (dlen >= 8) && udp->uh_sum) {
1794 * The UDP checksum is optional, only adjust it
1795 * if it has been set.
1797 sum1 = ntohs(udp->uh_sum);
1798 fix_datacksum(&udp->uh_sum, sumd);
1799 sum2 = ntohs(udp->uh_sum);
1802 * Fix ICMP checksum to compensate the UDP
1803 * checksum adjustment.
1806 CALC_SUMD(sum1, sum2, sumd);
1811 * Fix TCP pseudo header checksum to compensate for the
1812 * IP address change. Before we can do the change, we
1813 * must make sure that oip is sufficient large to hold
1814 * the TCP checksum (normally it does not!).
1816 else if ((oip->ip_p == IPPROTO_TCP) && (dlen >= 18)) {
1817 sum1 = ntohs(tcp->th_sum);
1818 fix_datacksum(&tcp->th_sum, sumd);
1819 sum2 = ntohs(tcp->th_sum);
1822 * Fix ICMP checksum to compensate the TCP
1823 * checksum adjustment.
1826 CALC_SUMD(sum1, sum2, sumd);
1829 sumd2 = (sumd >> 16);
1830 if (nat->nat_dir == NAT_OUTBOUND)
1836 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
1839 * For offending TCP/UDP IP packets, translate the ports as
1840 * well, based on the NAT specification. Of course such
1841 * a change must be reflected in the ICMP checksum as well.
1843 * Advance notice : Now it becomes complicated :-)
1845 * Since the port fields are part of the TCP/UDP checksum
1846 * of the offending IP packet, you need to adjust that checksum
1847 * as well... but, if you change, you must change the icmp
1848 * checksum *again*, to reflect that change.
1850 * To further complicate: the TCP checksum is not in the first
1851 * 8 bytes of the offending ip packet, so it most likely is not
1852 * available. Some OSses like Solaris return enough bytes to
1853 * include the TCP checksum. So we have to check if the
1854 * ip->ip_len actually holds the TCP checksum of the oip!
1856 if (nat->nat_oport == tcp->th_dport) {
1857 if (tcp->th_sport != nat->nat_inport) {
1859 * Fix ICMP checksum to compensate port
1862 sum1 = ntohs(nat->nat_inport);
1863 sum2 = ntohs(tcp->th_sport);
1864 tcp->th_sport = nat->nat_inport;
1867 * Fix udp checksum to compensate port
1868 * adjustment. NOTE : the offending IP packet
1869 * flows the other direction compared to the
1872 * The UDP checksum is optional, only adjust
1873 * it if it has been set.
1875 if ((oip->ip_p == IPPROTO_UDP) &&
1876 (dlen >= 8) && udp->uh_sum) {
1880 sum1 = ntohs(udp->uh_sum);
1881 fix_datacksum(&udp->uh_sum, sumd);
1882 sum2 = ntohs(udp->uh_sum);
1885 * Fix ICMP checksum to compensate
1886 * UDP checksum adjustment.
1888 CALC_SUMD(sum1, sum2, sumd);
1893 * Fix tcp checksum (if present) to compensate
1894 * port adjustment. NOTE : the offending IP
1895 * packet flows the other direction compared to
1898 if (oip->ip_p == IPPROTO_TCP) {
1903 sum1 = ntohs(tcp->th_sum);
1904 fix_datacksum(&tcp->th_sum,
1906 sum2 = ntohs(tcp->th_sum);
1909 * Fix ICMP checksum to
1910 * compensate TCP checksum
1913 CALC_SUMD(sum1, sum2, sumd);
1916 sumd = sum2 - sum1 + 1;
1921 } else if (tcp->th_dport != nat->nat_outport) {
1923 * Fix ICMP checksum to compensate port
1926 sum1 = ntohs(nat->nat_outport);
1927 sum2 = ntohs(tcp->th_dport);
1928 tcp->th_dport = nat->nat_outport;
1931 * Fix udp checksum to compensate port
1932 * adjustment. NOTE : the offending IP
1933 * packet flows the other direction compared
1934 * to the ICMP message.
1936 * The UDP checksum is optional, only adjust
1937 * it if it has been set.
1939 if ((oip->ip_p == IPPROTO_UDP) &&
1940 (dlen >= 8) && udp->uh_sum) {
1944 sum1 = ntohs(udp->uh_sum);
1945 fix_datacksum(&udp->uh_sum, sumd);
1946 sum2 = ntohs(udp->uh_sum);
1949 * Fix ICMP checksum to compensate
1950 * UDP checksum adjustment.
1952 CALC_SUMD(sum1, sum2, sumd);
1956 * Fix tcp checksum (if present) to compensate
1957 * port adjustment. NOTE : the offending IP
1958 * packet flows the other direction compared to
1961 if (oip->ip_p == IPPROTO_TCP) {
1966 sum1 = ntohs(tcp->th_sum);
1967 fix_datacksum(&tcp->th_sum, sumd);
1968 sum2 = ntohs(tcp->th_sum);
1971 * Fix ICMP checksum to compensate
1972 * UDP checksum adjustment.
1974 CALC_SUMD(sum1, sum2, sumd);
1977 if (nat->nat_dir == NAT_OUTBOUND)
1984 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1985 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1986 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
1989 if (oip->ip_p == IPPROTO_ICMP)
1990 nat->nat_age = fr_defnaticmpage;
1996 * NB: these lookups don't lock access to the list, it assume it has already
2000 * Lookup a nat entry based on the mapped destination ip address/port and
2001 * real source address/port. We use this lookup when receiving a packet,
2002 * we're looking for a table entry, based on the destination address.
2003 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2005 nat_t *nat_inlookup(fin, flags, p, src, mapdst, rw)
2008 struct in_addr src , mapdst;
2011 u_short sport, dport;
2023 dst = mapdst.s_addr;
2024 if (flags & IPN_TCPUDP) {
2025 sport = htons(fin->fin_data[0]);
2026 dport = htons(fin->fin_data[1]);
2032 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
2033 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
2034 nat = nat_table[1][hv];
2035 for (; nat; nat = nat->nat_hnext[1]) {
2036 nflags = nat->nat_flags;
2037 if ((!ifp || ifp == nat->nat_ifp) &&
2038 nat->nat_oip.s_addr == src.s_addr &&
2039 nat->nat_outip.s_addr == dst &&
2040 ((p == 0) || (p == nat->nat_p))) {
2045 if (nat->nat_oport != sport)
2047 if (nat->nat_outport != dport)
2055 if ((ipn != NULL) && (nat->nat_aps != NULL))
2056 if (appr_match(fin, nat) != 0)
2061 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2064 RWLOCK_EXIT(&ipf_nat);
2066 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
2067 hv = NAT_HASH_FN(src.s_addr, dst, ipf_nattable_sz);
2069 WRITE_ENTER(&ipf_nat);
2071 nat = nat_table[1][hv];
2072 for (; nat; nat = nat->nat_hnext[1]) {
2073 nflags = nat->nat_flags;
2074 if (ifp && ifp != nat->nat_ifp)
2076 if (!(nflags & FI_WILDP))
2078 if (nat->nat_oip.s_addr != src.s_addr ||
2079 nat->nat_outip.s_addr != dst)
2081 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
2082 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
2083 nat_tabmove(fin, nat);
2088 MUTEX_DOWNGRADE(&ipf_nat);
2095 * This function is only called for TCP/UDP NAT table entries where the
2096 * original was placed in the table without hashing on the ports and we now
2097 * want to include hashing on port numbers.
2099 static void nat_tabmove(fin, nat)
2103 u_short sport, dport;
2107 nflags = nat->nat_flags;
2109 sport = ntohs(fin->fin_data[0]);
2110 dport = ntohs(fin->fin_data[1]);
2113 * Remove the NAT entry from the old location
2115 if (nat->nat_hnext[0])
2116 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2117 *nat->nat_phnext[0] = nat->nat_hnext[0];
2119 if (nat->nat_hnext[1])
2120 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2121 *nat->nat_phnext[1] = nat->nat_hnext[1];
2124 * Add into the NAT table in the new position
2126 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2127 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2128 natp = &nat_table[0][hv];
2130 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2131 nat->nat_phnext[0] = natp;
2132 nat->nat_hnext[0] = *natp;
2135 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2136 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2137 natp = &nat_table[1][hv];
2139 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2140 nat->nat_phnext[1] = natp;
2141 nat->nat_hnext[1] = *natp;
2147 * Lookup a nat entry based on the source 'real' ip address/port and
2148 * destination address/port. We use this lookup when sending a packet out,
2149 * we're looking for a table entry, based on the source address.
2150 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2152 nat_t *nat_outlookup(fin, flags, p, src, dst, rw)
2155 struct in_addr src , dst;
2158 u_short sport, dport;
2168 if (flags & IPN_TCPUDP) {
2169 sport = ntohs(fin->fin_data[0]);
2170 dport = ntohs(fin->fin_data[1]);
2176 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2177 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2178 nat = nat_table[0][hv];
2179 for (; nat; nat = nat->nat_hnext[0]) {
2180 nflags = nat->nat_flags;
2182 if ((!ifp || ifp == nat->nat_ifp) &&
2183 nat->nat_inip.s_addr == srcip &&
2184 nat->nat_oip.s_addr == dst.s_addr &&
2185 ((p == 0) || (p == nat->nat_p))) {
2190 if (nat->nat_oport != dport)
2192 if (nat->nat_inport != sport)
2200 if ((ipn != NULL) && (nat->nat_aps != NULL))
2201 if (appr_match(fin, nat) != 0)
2206 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2209 RWLOCK_EXIT(&ipf_nat);
2212 hv = NAT_HASH_FN(dst.s_addr, srcip, ipf_nattable_sz);
2214 WRITE_ENTER(&ipf_nat);
2216 nat = nat_table[0][hv];
2217 for (; nat; nat = nat->nat_hnext[0]) {
2218 nflags = nat->nat_flags;
2219 if (ifp && ifp != nat->nat_ifp)
2221 if (!(nflags & FI_WILDP))
2223 if ((nat->nat_inip.s_addr != srcip) ||
2224 (nat->nat_oip.s_addr != dst.s_addr))
2226 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2227 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2228 nat_tabmove(fin, nat);
2233 MUTEX_DOWNGRADE(&ipf_nat);
2240 * Lookup the NAT tables to search for a matching redirect
2242 nat_t *nat_lookupredir(np)
2248 bzero((char *)&fi, sizeof(fi));
2249 fi.fin_data[0] = ntohs(np->nl_inport);
2250 fi.fin_data[1] = ntohs(np->nl_outport);
2253 * If nl_inip is non null, this is a lookup based on the real
2254 * ip address. Else, we use the fake.
2256 if ((nat = nat_outlookup(&fi, np->nl_flags, 0, np->nl_inip,
2257 np->nl_outip, 0))) {
2258 np->nl_realip = nat->nat_outip;
2259 np->nl_realport = nat->nat_outport;
2265 static int nat_match(fin, np, ip)
2275 if (np->in_p && fin->fin_p != np->in_p)
2278 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2280 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2281 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2283 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2284 ^ ((np->in_flags & IPN_NOTDST) != 0))
2287 if (!(np->in_redir & NAT_REDIRECT))
2289 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2290 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2292 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2293 ^ ((np->in_flags & IPN_NOTDST) != 0))
2298 if (!(fin->fin_fl & FI_TCPUDP) ||
2299 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2300 if (ft->ftu_scmp || ft->ftu_dcmp)
2305 return fr_tcpudpchk(ft, fin);
2310 * Packets going out on the external interface go through this.
2311 * Here, the source address requires alteration, if anything.
2313 int ip_natout(ip, fin)
2319 tcphdr_t *tcp = NULL;
2320 u_short sport = 0, dport = 0, *csump = NULL;
2321 int natadd = 1, i, icmpset = 1;
2322 u_int nflags = 0, hv, msk;
2329 if (nat_list == NULL || (fr_nat_lock))
2332 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2333 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1) {
2334 sifp = fin->fin_ifp;
2335 fin->fin_ifp = fr->fr_tif.fd_ifp;
2337 sifp = fin->fin_ifp;
2340 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2341 if (fin->fin_p == IPPROTO_TCP)
2343 else if (fin->fin_p == IPPROTO_UDP)
2345 if ((nflags & IPN_TCPUDP)) {
2346 tcp = (tcphdr_t *)fin->fin_dp;
2347 sport = tcp->th_sport;
2348 dport = tcp->th_dport;
2352 ipa = fin->fin_saddr;
2354 READ_ENTER(&ipf_nat);
2356 if ((fin->fin_p == IPPROTO_ICMP) &&
2357 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2359 else if ((fin->fin_fl & FI_FRAG) &&
2360 (nat = ipfr_nat_knownfrag(ip, fin)))
2362 else if ((nat = nat_outlookup(fin, nflags|FI_WILDP|FI_WILDA,
2363 (u_int)fin->fin_p, fin->fin_src,
2364 fin->fin_dst, 0))) {
2365 nflags = nat->nat_flags;
2366 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2367 if ((nflags & FI_W_SPORT) &&
2368 (nat->nat_inport != sport))
2369 nat->nat_inport = sport;
2370 if ((nflags & FI_W_DPORT) &&
2371 (nat->nat_oport != dport))
2372 nat->nat_oport = dport;
2374 if (nat->nat_outport == 0)
2375 nat->nat_outport = sport;
2376 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2377 nflags = nat->nat_flags;
2378 nat_stats.ns_wilds--;
2381 RWLOCK_EXIT(&ipf_nat);
2386 WRITE_ENTER(&ipf_nat);
2388 * If there is no current entry in the nat table for this IP#,
2389 * create one for it (if there is a matching rule).
2392 iph = ipa & htonl(msk);
2393 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2394 for (np = nat_rules[hv]; np; np = np->in_mnext)
2396 if (np->in_ifp && (np->in_ifp != ifp))
2398 if ((np->in_flags & IPN_RF) &&
2399 !(np->in_flags & nflags))
2401 if (np->in_flags & IPN_FILTER) {
2402 if (!nat_match(fin, np, ip))
2404 } else if ((ipa & np->in_inmsk) != np->in_inip)
2406 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2408 nat = nat_new(fin, ip, np, NULL,
2409 (u_int)nflags, NAT_OUTBOUND);
2415 if ((np == NULL) && (i > 0)) {
2419 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2423 MUTEX_DOWNGRADE(&ipf_nat);
2427 * NOTE: ipf_nat must now only be held as a read lock
2431 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2432 ipfr_nat_newfrag(ip, fin, nat);
2433 MUTEX_ENTER(&nat->nat_lock);
2434 if (fin->fin_p != IPPROTO_TCP) {
2435 if (np && np->in_age[1])
2436 nat->nat_age = np->in_age[1];
2437 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2438 nat->nat_age = fr_defnaticmpage;
2440 nat->nat_age = fr_defnatage;
2442 nat->nat_bytes += ip->ip_len;
2444 MUTEX_EXIT(&nat->nat_lock);
2447 * Fix up checksums, not by recalculating them, but
2448 * simply computing adjustments.
2450 if (nflags == IPN_ICMPERR) {
2451 u_32_t s1, s2, sumd;
2453 s1 = LONG_SUM(ntohl(fin->fin_saddr));
2454 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2455 CALC_SUMD(s1, s2, sumd);
2456 fix_outcksum(fin, &ip->ip_sum, sumd);
2458 #if (SOLARIS || defined(__sgi)) || !defined(_KERNEL)
2460 if (nat->nat_dir == NAT_OUTBOUND)
2461 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2463 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2467 * Only change the packet contents, not what is filtered upon.
2469 ip->ip_src = nat->nat_outip;
2471 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2473 if ((nat->nat_outport != 0) && (tcp != NULL)) {
2474 tcp->th_sport = nat->nat_outport;
2475 fin->fin_data[0] = ntohs(tcp->th_sport);
2478 if (fin->fin_p == IPPROTO_TCP) {
2479 csump = &tcp->th_sum;
2480 MUTEX_ENTER(&nat->nat_lock);
2481 fr_tcp_age(&nat->nat_age,
2482 nat->nat_tcpstate, fin, 1, 0);
2483 if (nat->nat_age < fr_defnaticmpage)
2484 nat->nat_age = fr_defnaticmpage;
2486 else if ((!np || !np->in_age[1]) &&
2487 (nat->nat_age > fr_defnatage))
2488 nat->nat_age = fr_defnatage;
2491 * Increase this because we may have
2492 * "keep state" following this too and
2493 * packet storms can occur if this is
2494 * removed too quickly.
2496 if (nat->nat_age == fr_tcpclosed)
2497 nat->nat_age = fr_tcplastack;
2500 * Do a MSS CLAMPING on a SYN packet,
2501 * only deal IPv4 for now.
2503 if (nat->nat_mssclamp &&
2504 (tcp->th_flags & TH_SYN) != 0)
2505 nat_mssclamp(tcp, nat->nat_mssclamp,
2508 MUTEX_EXIT(&nat->nat_lock);
2509 } else if (fin->fin_p == IPPROTO_UDP) {
2510 udphdr_t *udp = (udphdr_t *)tcp;
2513 csump = &udp->uh_sum;
2517 if (nat->nat_dir == NAT_OUTBOUND)
2518 fix_outcksum(fin, csump,
2521 fix_incksum(fin, csump,
2526 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2527 (tcp != NULL && dport == np->in_dport))) {
2528 i = appr_check(ip, fin, nat);
2535 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2536 RWLOCK_EXIT(&ipf_nat); /* READ */
2537 fin->fin_ifp = sifp;
2540 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2541 fin->fin_ifp = sifp;
2547 * Packets coming in from the external interface go through this.
2548 * Here, the destination address requires alteration, if anything.
2550 int ip_natin(ip, fin)
2557 u_short sport = 0, dport = 0, *csump = NULL;
2558 u_int nflags = 0, natadd = 1, hv, msk;
2559 struct ifnet *ifp = fin->fin_ifp;
2560 tcphdr_t *tcp = NULL;
2565 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2568 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2569 if (fin->fin_p == IPPROTO_TCP)
2571 else if (fin->fin_p == IPPROTO_UDP)
2573 if ((nflags & IPN_TCPUDP)) {
2574 tcp = (tcphdr_t *)fin->fin_dp;
2575 sport = tcp->th_sport;
2576 dport = tcp->th_dport;
2581 /* make sure the source address is to be redirected */
2584 READ_ENTER(&ipf_nat);
2586 if ((fin->fin_p == IPPROTO_ICMP) &&
2587 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2589 else if ((fin->fin_fl & FI_FRAG) &&
2590 (nat = ipfr_nat_knownfrag(ip, fin)))
2592 else if ((nat = nat_inlookup(fin, nflags|FI_WILDP|FI_WILDA,
2593 (u_int)fin->fin_p, fin->fin_src, in, 0))) {
2594 nflags = nat->nat_flags;
2595 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2596 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2597 nat->nat_oport = sport;
2598 if ((nat->nat_outport != dport) &&
2599 (nflags & FI_W_SPORT))
2600 nat->nat_outport = dport;
2601 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2602 nflags = nat->nat_flags;
2603 nat_stats.ns_wilds--;
2606 RWLOCK_EXIT(&ipf_nat);
2611 WRITE_ENTER(&ipf_nat);
2613 * If there is no current entry in the nat table for this IP#,
2614 * create one for it (if there is a matching rule).
2617 iph = in.s_addr & htonl(msk);
2618 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2619 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2620 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2621 (np->in_p && (np->in_p != fin->fin_p)) ||
2622 (np->in_flags && !(nflags & np->in_flags)))
2624 if (np->in_flags & IPN_FILTER) {
2625 if (!nat_match(fin, np, ip))
2627 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2629 if ((!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2630 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2631 (ntohs(dport) >= ntohs(np->in_pmin)))))
2632 if ((nat = nat_new(fin, ip, np, NULL, nflags,
2639 if ((np == NULL) && (i > 0)) {
2643 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2647 MUTEX_DOWNGRADE(&ipf_nat);
2651 * NOTE: ipf_nat must now only be held as a read lock
2655 fin->fin_fr = nat->nat_fr;
2656 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2657 ipfr_nat_newfrag(ip, fin, nat);
2658 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2659 (tcp != NULL && sport == np->in_dport))) {
2660 i = appr_check(ip, fin, nat);
2663 RWLOCK_EXIT(&ipf_nat);
2668 MUTEX_ENTER(&nat->nat_lock);
2669 if (fin->fin_p != IPPROTO_TCP) {
2670 if (np && np->in_age[0])
2671 nat->nat_age = np->in_age[0];
2672 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2673 nat->nat_age = fr_defnaticmpage;
2675 nat->nat_age = fr_defnatage;
2677 nat->nat_bytes += ip->ip_len;
2679 MUTEX_EXIT(&nat->nat_lock);
2682 * Fix up checksums, not by recalculating them, but
2683 * simply computing adjustments.
2685 if (nat->nat_dir == NAT_OUTBOUND)
2686 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2688 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2690 ip->ip_dst = nat->nat_inip;
2691 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2693 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2695 if ((nat->nat_inport != 0) && (tcp != NULL)) {
2696 tcp->th_dport = nat->nat_inport;
2697 fin->fin_data[1] = ntohs(tcp->th_dport);
2700 if (fin->fin_p == IPPROTO_TCP) {
2701 csump = &tcp->th_sum;
2702 MUTEX_ENTER(&nat->nat_lock);
2703 fr_tcp_age(&nat->nat_age,
2704 nat->nat_tcpstate, fin, 0, 0);
2705 if (nat->nat_age < fr_defnaticmpage)
2706 nat->nat_age = fr_defnaticmpage;
2708 else if ((!np || !np->in_age[0]) &&
2709 (nat->nat_age > fr_defnatage))
2710 nat->nat_age = fr_defnatage;
2713 * Increase this because we may have
2714 * "keep state" following this too and
2715 * packet storms can occur if this is
2716 * removed too quickly.
2718 if (nat->nat_age == fr_tcpclosed)
2719 nat->nat_age = fr_tcplastack;
2721 * Do a MSS CLAMPING on a SYN packet,
2722 * only deal IPv4 for now.
2724 if (nat->nat_mssclamp &&
2725 (tcp->th_flags & TH_SYN) != 0)
2726 nat_mssclamp(tcp, nat->nat_mssclamp,
2729 MUTEX_EXIT(&nat->nat_lock);
2730 } else if (fin->fin_p == IPPROTO_UDP) {
2731 udphdr_t *udp = (udphdr_t *)tcp;
2734 csump = &udp->uh_sum;
2738 if (nat->nat_dir == NAT_OUTBOUND)
2739 fix_incksum(fin, csump,
2742 fix_outcksum(fin, csump,
2746 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2747 RWLOCK_EXIT(&ipf_nat); /* READ */
2750 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2756 * Free all memory used by NAT structures allocated at runtime.
2760 WRITE_ENTER(&ipf_nat);
2761 (void) nat_clearlist();
2762 (void) nat_flushtable();
2763 RWLOCK_EXIT(&ipf_nat);
2765 if (nat_table[0] != NULL) {
2766 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2767 nat_table[0] = NULL;
2769 if (nat_table[1] != NULL) {
2770 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2771 nat_table[1] = NULL;
2773 if (nat_rules != NULL) {
2774 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2777 if (rdr_rules != NULL) {
2778 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2781 if (maptable != NULL) {
2782 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2789 * Slowly expire held state for NAT entries. Timeouts are set in
2790 * expectation of this being called twice per second.
2794 struct nat *nat, **natp;
2795 #if defined(_KERNEL) && !SOLARIS && !defined(__DragonFly__)
2800 WRITE_ENTER(&ipf_nat);
2801 for (natp = &nat_instances; (nat = *natp); ) {
2804 natp = &nat->nat_next;
2807 *natp = nat->nat_next;
2809 nat_log(nat, NL_EXPIRE);
2812 nat_stats.ns_expire++;
2814 RWLOCK_EXIT(&ipf_nat);
2821 void ip_natsync(ifp)
2826 u_32_t sum1, sum2, sumd;
2830 #if defined(_KERNEL) && !SOLARIS && !defined(__DragonFly__)
2835 * Change IP addresses for NAT sessions for any protocol except TCP
2836 * since it will break the TCP connection anyway.
2839 WRITE_ENTER(&ipf_nat);
2840 for (nat = nat_instances; nat; nat = nat->nat_next)
2841 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2842 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2843 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2844 ifp2 = nat->nat_ifp;
2846 * Change the map-to address to be the same as the
2849 sum1 = nat->nat_outip.s_addr;
2850 if (fr_ifpaddr(4, ifp2, &in) != -1)
2851 nat->nat_outip = in;
2852 sum2 = nat->nat_outip.s_addr;
2857 * Readjust the checksum adjustment to take into
2858 * account the new IP#.
2860 CALC_SUMD(sum1, sum2, sumd);
2861 /* XXX - dont change for TCP when solaris does
2862 * hardware checksumming.
2864 sumd += nat->nat_sumd[0];
2865 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2866 nat->nat_sumd[1] = nat->nat_sumd[0];
2869 for (n = nat_list; (n != NULL); n = n->in_next)
2870 if (n->in_ifp == ifp) {
2871 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2873 n->in_ifp = (void *)-1;
2875 RWLOCK_EXIT(&ipf_nat);
2881 void nat_log(nat, type)
2894 natl.nl_inip = nat->nat_inip;
2895 natl.nl_outip = nat->nat_outip;
2896 natl.nl_origip = nat->nat_oip;
2897 natl.nl_bytes = nat->nat_bytes;
2898 natl.nl_pkts = nat->nat_pkts;
2899 natl.nl_origport = nat->nat_oport;
2900 natl.nl_inport = nat->nat_inport;
2901 natl.nl_outport = nat->nat_outport;
2902 natl.nl_p = nat->nat_p;
2903 natl.nl_type = type;
2906 if (nat->nat_ptr != NULL) {
2907 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2908 if (np == nat->nat_ptr) {
2909 natl.nl_rule = rulen;
2915 sizes[0] = sizeof(natl);
2918 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);
2923 #if defined(__OpenBSD__)
2924 void nat_ifdetach(ifp)
2934 * Check for MSS option and clamp it if necessary.
2936 static void nat_mssclamp(tcp, maxmss, fin, csump)
2942 u_char *cp, *ep, opt;
2947 hlen = tcp->th_off << 2;
2948 if (hlen > sizeof(*tcp)) {
2949 cp = (u_char *)tcp + sizeof(*tcp);
2950 ep = (u_char *)tcp + hlen;
2954 if (opt == TCPOPT_EOL)
2956 else if (opt == TCPOPT_NOP) {
2964 if (&cp[advance] > ep)
2970 bcopy(&cp[2], &v, sizeof(v));
2974 bcopy(&v, &cp[2], sizeof(v));
2975 CALC_SUMD(mss, maxmss, sumd);
2976 fix_outcksum(fin, csump, sumd);
2980 /* ignore unknown options */