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.12 2006/12/23 00:27:02 swildner 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
119 # define kprintf printf
122 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
124 nat_t **nat_table[2] = { NULL, NULL },
125 *nat_instances = NULL;
126 ipnat_t *nat_list = NULL;
127 u_int ipf_nattable_max = NAT_TABLE_MAX;
128 u_int ipf_nattable_sz = NAT_TABLE_SZ;
129 u_int ipf_natrules_sz = NAT_SIZE;
130 u_int ipf_rdrrules_sz = RDR_SIZE;
131 u_int ipf_hostmap_sz = HOSTMAP_SIZE;
132 u_32_t nat_masks = 0;
133 u_32_t rdr_masks = 0;
134 ipnat_t **nat_rules = NULL;
135 ipnat_t **rdr_rules = NULL;
136 hostmap_t **maptable = NULL;
138 u_long fr_defnatage = DEF_NAT_AGE,
139 fr_defnaticmpage = 6; /* 3 seconds */
142 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
143 extern kmutex_t ipf_rw;
144 extern KRWLOCK_T ipf_nat;
147 static int nat_flushtable (void);
148 static void nat_addnat (struct ipnat *);
149 static void nat_addrdr (struct ipnat *);
150 static void nat_delete (struct nat *);
151 static void nat_delrdr (struct ipnat *);
152 static void nat_delnat (struct ipnat *);
153 static int fr_natgetent (caddr_t);
154 static int fr_natgetsz (caddr_t);
155 static int fr_natputent (caddr_t);
156 static void nat_tabmove (fr_info_t *, nat_t *);
157 static int nat_match (fr_info_t *, ipnat_t *, ip_t *);
158 static hostmap_t *nat_hostmap (ipnat_t *, struct in_addr,
160 static void nat_hostmapdel (struct hostmap *);
161 static void nat_mssclamp (tcphdr_t *, u_32_t, fr_info_t *, u_short *);
166 KMALLOCS(nat_table[0], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
167 if (nat_table[0] != NULL)
168 bzero((char *)nat_table[0], ipf_nattable_sz * sizeof(nat_t *));
172 KMALLOCS(nat_table[1], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
173 if (nat_table[1] != NULL)
174 bzero((char *)nat_table[1], ipf_nattable_sz * sizeof(nat_t *));
178 KMALLOCS(nat_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_natrules_sz);
179 if (nat_rules != NULL)
180 bzero((char *)nat_rules, ipf_natrules_sz * sizeof(ipnat_t *));
184 KMALLOCS(rdr_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_rdrrules_sz);
185 if (rdr_rules != NULL)
186 bzero((char *)rdr_rules, ipf_rdrrules_sz * sizeof(ipnat_t *));
190 KMALLOCS(maptable, hostmap_t **, sizeof(hostmap_t *) * ipf_hostmap_sz);
191 if (maptable != NULL)
192 bzero((char *)maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
199 static void nat_addrdr(n)
207 k = countbits(n->in_outmsk);
208 if ((k >= 0) && (k != 32))
210 j = (n->in_outip & n->in_outmsk);
211 hv = NAT_HASH_FN(j, 0, ipf_rdrrules_sz);
214 np = &(*np)->in_rnext;
221 static void nat_addnat(n)
229 k = countbits(n->in_inmsk);
230 if ((k >= 0) && (k != 32))
232 j = (n->in_inip & n->in_inmsk);
233 hv = NAT_HASH_FN(j, 0, ipf_natrules_sz);
236 np = &(*np)->in_mnext;
243 static void nat_delrdr(n)
247 n->in_rnext->in_prnext = n->in_prnext;
248 *n->in_prnext = n->in_rnext;
252 static void nat_delnat(n)
256 n->in_mnext->in_pmnext = n->in_pmnext;
257 *n->in_pmnext = n->in_mnext;
262 * check if an ip address has already been allocated for a given mapping that
263 * is not doing port based translation.
265 * Must be called with ipf_nat held as a write lock.
267 static struct hostmap *nat_hostmap(np, real, map)
275 hv = real.s_addr % HOSTMAP_SIZE;
276 for (hm = maptable[hv]; hm; hm = hm->hm_next)
277 if ((hm->hm_realip.s_addr == real.s_addr) &&
278 (np == hm->hm_ipnat)) {
283 KMALLOC(hm, hostmap_t *);
285 hm->hm_next = maptable[hv];
286 hm->hm_pnext = maptable + hv;
288 maptable[hv]->hm_pnext = &hm->hm_next;
291 hm->hm_realip = real;
300 * Must be called with ipf_nat held as a write lock.
302 static void nat_hostmapdel(hm)
305 ATOMIC_DEC32(hm->hm_ref);
306 if (hm->hm_ref == 0) {
308 hm->hm_next->hm_pnext = hm->hm_pnext;
309 *hm->hm_pnext = hm->hm_next;
315 void fix_outcksum(fin, sp, n)
325 else if (n & NAT_HW_CKSUM) {
328 n = (n & 0xffff) + (n >> 16);
332 sum1 = (~ntohs(*sp)) & 0xffff;
334 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
336 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
337 sumshort = ~(u_short)sum1;
338 *(sp) = htons(sumshort);
342 void fix_incksum(fin, sp, n)
352 else if (n & NAT_HW_CKSUM) {
355 n = (n & 0xffff) + (n >> 16);
360 sum1 = (~(*sp)) & 0xffff;
362 sum1 = (~ntohs(*sp)) & 0xffff;
364 sum1 += ~(n) & 0xffff;
365 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
367 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
368 sumshort = ~(u_short)sum1;
369 *(sp) = htons(sumshort);
374 * fix_datacksum is used *only* for the adjustments of checksums in the data
375 * section of an IP packet.
377 * The only situation in which you need to do this is when NAT'ing an
378 * ICMP error message. Such a message, contains in its body the IP header
379 * of the original IP packet, that causes the error.
381 * You can't use fix_incksum or fix_outcksum in that case, because for the
382 * kernel the data section of the ICMP error is just data, and no special
383 * processing like hardware cksum or ntohs processing have been done by the
384 * kernel on the data section.
386 void fix_datacksum(sp, n)
396 sum1 = (~ntohs(*sp)) & 0xffff;
398 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
400 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
401 sumshort = ~(u_short)sum1;
402 *(sp) = htons(sumshort);
406 * How the NAT is organised and works.
408 * Inside (interface y) NAT Outside (interface x)
409 * -------------------- -+- -------------------------------------
410 * Packet going | out, processsed by ip_natout() for x
411 * ------------> | ------------>
412 * src=10.1.1.1 | src=192.1.1.1
414 * | in, processed by ip_natin() for x
415 * <------------ | <------------
416 * dst=10.1.1.1 | dst=192.1.1.1
417 * -------------------- -+- -------------------------------------
418 * ip_natout() - changes ip_src and if required, sport
419 * - creates a new mapping, if required.
420 * ip_natin() - changes ip_dst and if required, dport
422 * In the NAT table, internal source is recorded as "in" and externally
427 * Handle ioctls which manipulate the NAT.
429 int nat_ioctl(data, cmd, mode)
430 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
438 ipnat_t *nat, *nt, *n = NULL, **np = NULL;
439 int error = 0, ret, arg, getlock;
443 #if (BSD >= 199306) && defined(_KERNEL)
444 if ((securelevel >= 3) && (mode & FWRITE))
448 nat = NULL; /* XXX gcc -Wuninitialized */
449 KMALLOC(nt, ipnat_t *);
450 getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
451 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
452 if (mode & NAT_SYSSPACE) {
453 bcopy(data, (char *)&natd, sizeof(natd));
456 error = IRCOPYPTR(data, (char *)&natd, sizeof(natd));
458 } else if (cmd == SIOCIPFFL) { /* SIOCFLNAT & SIOCCNATL */
459 error = IRCOPY(data, (char *)&arg, sizeof(arg));
468 * For add/delete, look to see if the NAT entry is already present
471 WRITE_ENTER(&ipf_nat);
473 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
475 nat->in_flags &= IPN_USERFLAGS;
476 if ((nat->in_redir & NAT_MAPBLK) == 0) {
477 if ((nat->in_flags & IPN_SPLIT) == 0)
478 nat->in_inip &= nat->in_inmsk;
479 if ((nat->in_flags & IPN_IPRANGE) == 0)
480 nat->in_outip &= nat->in_outmsk;
482 for (np = &nat_list; (n = *np); np = &n->in_next)
483 if (!bcmp((char *)&nat->in_flags, (char *)&n->in_flags,
485 if (n->in_redir == NAT_REDIRECT &&
486 n->in_pnext != nat->in_pnext)
499 if (!(mode & FWRITE))
502 tmp = ipflog_clear(IPL_LOGNAT);
503 IWCOPY((char *)&tmp, (char *)data, sizeof(tmp));
509 if (!(mode & FWRITE)) {
523 bcopy((char *)nat, (char *)n, sizeof(*n));
524 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
526 n->in_ifp = (void *)-1;
527 if (n->in_plabel[0] != '\0') {
528 n->in_apr = appr_lookup(n->in_p, n->in_plabel);
537 if (n->in_redir & NAT_REDIRECT) {
538 n->in_flags &= ~IPN_NOTDST;
541 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
542 n->in_flags &= ~IPN_NOTSRC;
547 if (n->in_redir & NAT_MAPBLK)
548 n->in_space = USABLE_PORTS * ~ntohl(n->in_outmsk);
549 else if (n->in_flags & IPN_AUTOPORTMAP)
550 n->in_space = USABLE_PORTS * ~ntohl(n->in_inmsk);
551 else if (n->in_flags & IPN_IPRANGE)
552 n->in_space = ntohl(n->in_outmsk) - ntohl(n->in_outip);
553 else if (n->in_flags & IPN_SPLIT)
556 n->in_space = ~ntohl(n->in_outmsk);
558 * Calculate the number of valid IP addresses in the output
559 * mapping range. In all cases, the range is inclusive of
560 * the start and ending IP addresses.
561 * If to a CIDR address, lose 2: broadcast + network address
563 * If to a range, add one.
564 * If to a single IP address, set to 1.
567 if ((n->in_flags & IPN_IPRANGE) != 0)
573 if ((n->in_outmsk != 0xffffffff) && (n->in_outmsk != 0) &&
574 ((n->in_flags & (IPN_IPRANGE|IPN_SPLIT)) == 0))
575 n->in_nip = ntohl(n->in_outip) + 1;
576 else if ((n->in_flags & IPN_SPLIT) &&
577 (n->in_redir & NAT_REDIRECT))
578 n->in_nip = ntohl(n->in_inip);
580 n->in_nip = ntohl(n->in_outip);
581 if (n->in_redir & NAT_MAP) {
582 n->in_pnext = ntohs(n->in_pmin);
584 * Multiply by the number of ports made available.
586 if (ntohs(n->in_pmax) >= ntohs(n->in_pmin)) {
587 n->in_space *= (ntohs(n->in_pmax) -
588 ntohs(n->in_pmin) + 1);
590 * Because two different sources can map to
591 * different destinations but use the same
593 * If the result is smaller than in_space, then
594 * we may have wrapped around 32bits.
597 if ((i != 0) && (i != 0xffffffff)) {
598 j = n->in_space * (~ntohl(i) + 1);
599 if (j >= n->in_space)
602 n->in_space = 0xffffffff;
606 * If no protocol is specified, multiple by 256.
608 if ((n->in_flags & IPN_TCPUDP) == 0) {
609 j = n->in_space * 256;
610 if (j >= n->in_space)
613 n->in_space = 0xffffffff;
616 /* Otherwise, these fields are preset */
618 nat_stats.ns_rules++;
621 if (!(mode & FWRITE)) {
630 if (n->in_redir & NAT_REDIRECT)
632 if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
634 if (nat_list == NULL) {
641 appr_free(n->in_apr);
643 nat_stats.ns_rules--;
645 n->in_flags |= IPN_DELETE;
651 MUTEX_DOWNGRADE(&ipf_nat);
652 nat_stats.ns_table[0] = nat_table[0];
653 nat_stats.ns_table[1] = nat_table[1];
654 nat_stats.ns_list = nat_list;
655 nat_stats.ns_maptable = maptable;
656 nat_stats.ns_nattab_sz = ipf_nattable_sz;
657 nat_stats.ns_rultab_sz = ipf_natrules_sz;
658 nat_stats.ns_rdrtab_sz = ipf_rdrrules_sz;
659 nat_stats.ns_hostmap_sz = ipf_hostmap_sz;
660 nat_stats.ns_instances = nat_instances;
661 nat_stats.ns_apslist = ap_sess_list;
662 error = IWCOPYPTR((char *)&nat_stats, (char *)data,
669 MUTEX_DOWNGRADE(&ipf_nat);
670 error = IRCOPYPTR((char *)data, (char *)&nl, sizeof(nl));
674 if (nat_lookupredir(&nl)) {
675 error = IWCOPYPTR((char *)&nl, (char *)data,
681 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
682 if (!(mode & FWRITE)) {
688 ret = nat_flushtable();
690 ret = nat_clearlist();
693 MUTEX_DOWNGRADE(&ipf_nat);
695 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
701 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
703 error = IWCOPY((caddr_t)&fr_nat_lock, data,
704 sizeof(fr_nat_lock));
712 error = fr_natputent(data);
718 error = fr_natgetsz(data);
724 error = fr_natgetent(data);
730 arg = (int)iplused[IPL_LOGNAT];
731 MUTEX_DOWNGRADE(&ipf_nat);
732 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
742 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
751 static int fr_natgetsz(data)
759 error = IRCOPY(data, (caddr_t)&ng, sizeof(ng));
768 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
775 * Make sure the pointer we're copying from exists in the
776 * current list of entries. Security precaution to prevent
777 * copying of random kernel data.
779 for (n = nat_instances; n; n = n->nat_next)
786 ng.ng_sz = sizeof(nat_save_t);
788 if ((aps != NULL) && (aps->aps_data != 0)) {
789 ng.ng_sz += sizeof(ap_session_t);
790 ng.ng_sz += aps->aps_psiz;
791 if (aps->aps_psiz > 4) /* XXX - sizeof(ipn_data) */
795 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
802 static int fr_natgetent(data)
805 nat_save_t ipn, *ipnp, *ipnn = NULL;
811 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
814 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
822 if (nat_instances == NULL)
828 * Make sure the pointer we're copying from exists in the
829 * current list of entries. Security precaution to prevent
830 * copying of random kernel data.
832 for (n = nat_instances; n; n = n->nat_next)
839 ipn.ipn_next = nat->nat_next;
840 bcopy((char *)nat, (char *)&ipn.ipn_nat, sizeof(ipn.ipn_nat));
841 ipn.ipn_nat.nat_data = NULL;
844 bcopy((char *)nat->nat_ptr, (char *)&ipn.ipn_ipnat,
845 sizeof(ipn.ipn_ipnat));
849 bcopy((char *)nat->nat_fr, (char *)&ipn.ipn_rule,
850 sizeof(ipn.ipn_rule));
852 if ((aps = nat->nat_aps)) {
855 dsz += aps->aps_psiz;
857 if (dsz > sizeof(ipn.ipn_data))
858 dsz -= sizeof(ipn.ipn_data);
859 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + dsz);
862 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
864 bcopy((char *)aps, (char *)ipnn->ipn_data, sizeof(*aps));
866 bcopy(aps->aps_data, ipnn->ipn_data + sizeof(*aps),
869 error = IWCOPY((caddr_t)ipnn, ipnp,
873 KFREES(ipnn, sizeof(*ipnn) + dsz);
876 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
884 static int fr_natputent(data)
887 nat_save_t ipn, *ipnp, *ipnn = NULL;
895 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
898 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
903 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + ipn.ipn_dsize);
906 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
907 error = IRCOPY((caddr_t)ipnp + offsetof(nat_save_t, ipn_data),
908 (caddr_t)ipnn->ipn_data, ipn.ipn_dsize);
916 KMALLOC(nat, nat_t *);
922 bcopy((char *)&ipn.ipn_nat, (char *)nat, sizeof(*nat));
924 * Initialize all these so that nat_delete() doesn't cause a crash.
926 nat->nat_phnext[0] = NULL;
927 nat->nat_phnext[1] = NULL;
935 nat->nat_data = NULL;
936 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
939 * Restore the rule associated with this nat session
942 KMALLOC(in, ipnat_t *);
948 bcopy((char *)&ipn.ipn_ipnat, (char *)in, sizeof(*in));
950 in->in_flags |= IPN_DELETE;
953 in->in_prnext = NULL;
955 in->in_pmnext = NULL;
956 in->in_ifp = GETUNIT(in->in_ifname, 4);
957 if (in->in_plabel[0] != '\0') {
958 in->in_apr = appr_lookup(in->in_p, in->in_plabel);
963 * Restore ap_session_t structure. Include the private data allocated
967 KMALLOC(aps, ap_session_t *);
973 aps->aps_next = ap_sess_list;
975 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
977 aps->aps_apr = in->in_apr;
979 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
980 if (aps->aps_data == NULL) {
984 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
988 aps->aps_data = NULL;
993 * If there was a filtering rule associated with this entry then
994 * build up a new one.
997 if (nat->nat_flags & FI_NEWFR) {
998 KMALLOC(fr, frentry_t *);
1004 bcopy((char *)&ipn.ipn_fr, (char *)fr, sizeof(*fr));
1005 ipn.ipn_nat.nat_fr = fr;
1006 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
1012 for (n = nat_instances; n; n = n->nat_next)
1013 if (n->nat_fr == fr)
1023 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1028 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1036 * Delete a nat entry from the various lists and table.
1038 static void nat_delete(natd)
1043 if (natd->nat_flags & FI_WILDP)
1044 nat_stats.ns_wilds--;
1045 if (natd->nat_hnext[0])
1046 natd->nat_hnext[0]->nat_phnext[0] = natd->nat_phnext[0];
1047 *natd->nat_phnext[0] = natd->nat_hnext[0];
1048 if (natd->nat_hnext[1])
1049 natd->nat_hnext[1]->nat_phnext[1] = natd->nat_phnext[1];
1050 *natd->nat_phnext[1] = natd->nat_hnext[1];
1051 if (natd->nat_me != NULL)
1052 *natd->nat_me = NULL;
1054 if (natd->nat_fr != NULL) {
1055 ATOMIC_DEC32(natd->nat_fr->fr_ref);
1058 if (natd->nat_hm != NULL)
1059 nat_hostmapdel(natd->nat_hm);
1062 * If there is an active reference from the nat entry to its parent
1063 * rule, decrement the rule's reference count and free it too if no
1064 * longer being used.
1066 ipn = natd->nat_ptr;
1070 if (!ipn->in_use && (ipn->in_flags & IPN_DELETE)) {
1072 appr_free(ipn->in_apr);
1074 nat_stats.ns_rules--;
1078 MUTEX_DESTROY(&natd->nat_lock);
1080 * If there's a fragment table entry too for this nat entry, then
1081 * dereference that as well.
1083 ipfr_forgetnat((void *)natd);
1084 aps_free(natd->nat_aps);
1085 nat_stats.ns_inuse--;
1091 * nat_flushtable - clear the NAT table of all mapping entries.
1092 * (this is for the dynamic mappings)
1094 static int nat_flushtable()
1100 * ALL NAT mappings deleted, so lets just make the deletions
1103 if (nat_table[0] != NULL)
1104 bzero((char *)nat_table[0],
1105 sizeof(nat_table[0]) * ipf_nattable_sz);
1106 if (nat_table[1] != NULL)
1107 bzero((char *)nat_table[1],
1108 sizeof(nat_table[1]) * ipf_nattable_sz);
1110 for (natp = &nat_instances; (nat = *natp); ) {
1111 *natp = nat->nat_next;
1113 nat_log(nat, NL_FLUSH);
1118 nat_stats.ns_inuse = 0;
1124 * nat_clearlist - delete all rules in the active NAT mapping list.
1125 * (this is for NAT/RDR rules)
1129 ipnat_t *n, **np = &nat_list;
1132 if (nat_rules != NULL)
1133 bzero((char *)nat_rules, sizeof(*nat_rules) * ipf_natrules_sz);
1134 if (rdr_rules != NULL)
1135 bzero((char *)rdr_rules, sizeof(*rdr_rules) * ipf_rdrrules_sz);
1141 appr_free(n->in_apr);
1143 nat_stats.ns_rules--;
1145 n->in_flags |= IPN_DELETE;
1157 * Create a new NAT table entry.
1158 * NOTE: Assumes write lock on ipf_nat has been obtained already.
1159 * If you intend on changing this, beware: appr_new() may call nat_new()
1162 nat_t *nat_new(fin, ip, np, natsave, flags, direction)
1170 u_32_t sum1, sum2, sumd, l;
1171 u_short port = 0, sport = 0, dport = 0, nport = 0;
1172 struct in_addr in, inb;
1173 u_short nflags, sp, dp;
1174 tcphdr_t *tcp = NULL;
1175 hostmap_t *hm = NULL;
1177 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1178 qif_t *qf = fin->fin_qif;
1181 if (nat_stats.ns_inuse >= ipf_nattable_max) {
1182 nat_stats.ns_memfail++;
1186 nflags = flags & np->in_flags;
1187 if (flags & IPN_TCPUDP) {
1188 tcp = (tcphdr_t *)fin->fin_dp;
1189 sport = htons(fin->fin_data[0]);
1190 dport = htons(fin->fin_data[1]);
1193 /* Give me a new nat */
1194 KMALLOC(nat, nat_t *);
1196 nat_stats.ns_memfail++;
1198 * Try to automatically tune the max # of entries in the
1199 * table allowed to be less than what will cause kmem_alloc()
1200 * to fail and try to eliminate panics due to out of memory
1201 * conditions arising.
1203 if (ipf_nattable_max > ipf_nattable_sz) {
1204 ipf_nattable_max = nat_stats.ns_inuse - 100;
1205 kprintf("ipf_nattable_max reduced to %d\n",
1211 bzero((char *)nat, sizeof(*nat));
1212 nat->nat_tcpstate[0] = TCPS_CLOSED;
1213 nat->nat_tcpstate[1] = TCPS_CLOSED;
1214 nat->nat_flags = flags;
1215 if (flags & FI_WILDP)
1216 nat_stats.ns_wilds++;
1218 * Search the current table for a match.
1220 if (direction == NAT_OUTBOUND) {
1222 * Values at which the search for a free resouce starts.
1228 * If it's an outbound packet which doesn't match any existing
1229 * record, then create a new port
1233 st_port = np->in_pnext;
1237 in.s_addr = htonl(np->in_nip);
1240 * Check to see if there is an existing NAT
1241 * setup for this IP address pair.
1243 hm = nat_hostmap(np, fin->fin_src, in);
1245 in.s_addr = hm->hm_mapip.s_addr;
1246 } else if ((l == 1) && (hm != NULL)) {
1250 in.s_addr = ntohl(in.s_addr);
1254 if ((np->in_outmsk == 0xffffffff) &&
1255 (np->in_pnext == 0)) {
1260 if (np->in_redir & NAT_MAPBLK) {
1261 if ((l >= np->in_ppip) || ((l > 0) &&
1262 !(flags & IPN_TCPUDP)))
1265 * map-block - Calculate destination address.
1267 in.s_addr = ntohl(fin->fin_saddr);
1268 in.s_addr &= ntohl(~np->in_inmsk);
1269 inb.s_addr = in.s_addr;
1270 in.s_addr /= np->in_ippip;
1271 in.s_addr &= ntohl(~np->in_outmsk);
1272 in.s_addr += ntohl(np->in_outip);
1274 * Calculate destination port.
1276 if ((flags & IPN_TCPUDP) &&
1277 (np->in_ppip != 0)) {
1278 port = ntohs(sport) + l;
1279 port %= np->in_ppip;
1280 port += np->in_ppip *
1281 (inb.s_addr % np->in_ippip);
1282 port += MAPBLK_MINPORT;
1285 } else if (!np->in_outip &&
1286 (np->in_outmsk == 0xffffffff)) {
1288 * 0/32 - use the interface's IP address.
1291 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1293 in.s_addr = ntohl(in.s_addr);
1294 } else if (!np->in_outip && !np->in_outmsk) {
1296 * 0/0 - use the original source address/port.
1300 in.s_addr = ntohl(fin->fin_saddr);
1301 } else if ((np->in_outmsk != 0xffffffff) &&
1302 (np->in_pnext == 0) &&
1303 ((l > 0) || (hm == NULL)))
1307 if ((nflags & IPN_TCPUDP) &&
1308 ((np->in_redir & NAT_MAPBLK) == 0) &&
1309 (np->in_flags & IPN_AUTOPORTMAP)) {
1310 if ((l > 0) && (l % np->in_ppip == 0)) {
1311 if (l > np->in_space) {
1313 } else if ((l > np->in_ppip) &&
1314 np->in_outmsk != 0xffffffff)
1317 if (np->in_ppip != 0) {
1318 port = ntohs(sport);
1319 port += (l % np->in_ppip);
1320 port %= np->in_ppip;
1321 port += np->in_ppip *
1322 (ntohl(fin->fin_saddr) %
1324 port += MAPBLK_MINPORT;
1327 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1328 (nflags & IPN_TCPUDP) &&
1329 (np->in_pnext != 0)) {
1330 port = htons(np->in_pnext++);
1331 if (np->in_pnext > ntohs(np->in_pmax)) {
1332 np->in_pnext = ntohs(np->in_pmin);
1333 if (np->in_outmsk != 0xffffffff)
1338 if (np->in_flags & IPN_IPRANGE) {
1339 if (np->in_nip > ntohl(np->in_outmsk))
1340 np->in_nip = ntohl(np->in_outip);
1342 if ((np->in_outmsk != 0xffffffff) &&
1343 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1344 ntohl(np->in_outip))
1345 np->in_nip = ntohl(np->in_outip) + 1;
1348 if (!port && (flags & IPN_TCPUDP))
1352 * Here we do a lookup of the connection as seen from
1353 * the outside. If an IP# pair already exists, try
1354 * again. So if you have A->B becomes C->B, you can
1355 * also have D->E become C->E but not D->B causing
1356 * another C->B. Also take protocol and ports into
1357 * account when determining whether a pre-existing
1358 * NAT setup will cause an external conflict where
1359 * this is appropriate.
1361 inb.s_addr = htonl(in.s_addr);
1362 sp = fin->fin_data[0];
1363 dp = fin->fin_data[1];
1364 fin->fin_data[0] = fin->fin_data[1];
1365 fin->fin_data[1] = htons(port);
1366 natl = nat_inlookup(fin, flags & ~FI_WILDP,
1367 (u_int)fin->fin_p, fin->fin_dst,
1369 fin->fin_data[0] = sp;
1370 fin->fin_data[1] = dp;
1373 * Has the search wrapped around and come back to the
1376 if ((natl != NULL) &&
1377 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1378 (np->in_nip != 0) && (st_ip == np->in_nip))
1381 } while (natl != NULL);
1383 if (np->in_space > 0)
1386 /* Setup the NAT table */
1387 nat->nat_inip = fin->fin_src;
1388 nat->nat_outip.s_addr = htonl(in.s_addr);
1389 nat->nat_oip = fin->fin_dst;
1390 if (nat->nat_hm == NULL)
1391 nat->nat_hm = nat_hostmap(np, fin->fin_src,
1394 sum1 = LONG_SUM(ntohl(fin->fin_saddr)) + ntohs(sport);
1395 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1397 if (flags & IPN_TCPUDP) {
1398 nat->nat_inport = sport;
1399 nat->nat_outport = port; /* sport */
1400 nat->nat_oport = dport;
1404 * Otherwise, it's an inbound packet. Most likely, we don't
1405 * want to rewrite source ports and source addresses. Instead,
1406 * we want to rewrite to a fixed internal address and fixed
1409 if (np->in_flags & IPN_SPLIT) {
1410 in.s_addr = np->in_nip;
1411 if (np->in_inip == htonl(in.s_addr))
1412 np->in_nip = ntohl(np->in_inmsk);
1414 np->in_nip = ntohl(np->in_inip);
1415 if (np->in_flags & IPN_ROUNDR) {
1421 in.s_addr = ntohl(np->in_inip);
1422 if (np->in_flags & IPN_ROUNDR) {
1431 * Whilst not optimized for the case where
1432 * pmin == pmax, the gain is not significant.
1434 if (np->in_pmin != np->in_pmax) {
1435 nport = ntohs(dport) - ntohs(np->in_pmin) +
1436 ntohs(np->in_pnext);
1437 nport = ntohs(nport);
1439 nport = np->in_pnext;
1443 * When the redirect-to address is set to 0.0.0.0, just
1444 * assume a blank `forwarding' of the packet.
1447 in.s_addr = ntohl(fin->fin_daddr);
1449 nat->nat_inip.s_addr = htonl(in.s_addr);
1450 nat->nat_outip = fin->fin_dst;
1451 nat->nat_oip = fin->fin_src;
1453 sum1 = LONG_SUM(ntohl(fin->fin_daddr)) + ntohs(dport);
1454 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1456 if (flags & IPN_TCPUDP) {
1457 nat->nat_inport = nport;
1458 nat->nat_outport = dport;
1459 nat->nat_oport = sport;
1463 CALC_SUMD(sum1, sum2, sumd);
1464 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1465 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1466 if ((flags & IPN_TCP) && dohwcksum &&
1467 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1468 if (direction == NAT_OUTBOUND)
1469 sum1 = LONG_SUM(ntohl(in.s_addr));
1471 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1472 sum1 += LONG_SUM(ntohl(fin->fin_daddr));
1473 sum1 += IPPROTO_TCP;
1474 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1475 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1478 nat->nat_sumd[1] = nat->nat_sumd[0];
1480 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1481 if (direction == NAT_OUTBOUND)
1482 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1484 sum1 = LONG_SUM(ntohl(fin->fin_daddr));
1486 sum2 = LONG_SUM(in.s_addr);
1488 CALC_SUMD(sum1, sum2, sumd);
1489 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1491 nat->nat_ipsumd = nat->nat_sumd[0];
1493 in.s_addr = htonl(in.s_addr);
1495 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1497 nat->nat_me = natsave;
1498 nat->nat_dir = direction;
1499 nat->nat_ifp = fin->fin_ifp;
1501 nat->nat_p = fin->fin_p;
1504 nat->nat_mssclamp = np->in_mssclamp;
1505 nat->nat_fr = fin->fin_fr;
1506 if (nat->nat_fr != NULL) {
1507 ATOMIC_INC32(nat->nat_fr->fr_ref);
1509 if (direction == NAT_OUTBOUND) {
1510 if (flags & IPN_TCPUDP)
1511 tcp->th_sport = port;
1513 if (flags & IPN_TCPUDP)
1514 tcp->th_dport = nport;
1519 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
1520 (tcp != NULL && dport == np->in_dport)))
1521 (void) appr_new(fin, ip, nat);
1525 nat_log(nat, (u_int)np->in_redir);
1529 nat_stats.ns_badnat++;
1530 if ((hm = nat->nat_hm) != NULL)
1538 * Insert a NAT entry into the hash tables for searching and add it to the
1539 * list of active NAT entries. Adjust global counters when complete.
1541 void nat_insert(nat)
1547 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1549 nat->nat_age = fr_defnatage;
1550 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1551 if (nat->nat_ifname[0] !='\0') {
1552 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1555 nat->nat_next = nat_instances;
1556 nat_instances = nat;
1558 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1559 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1561 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1563 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1565 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1568 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_inip.s_addr,
1570 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_outip.s_addr,
1574 natp = &nat_table[0][hv1];
1576 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1577 nat->nat_phnext[0] = natp;
1578 nat->nat_hnext[0] = *natp;
1581 natp = &nat_table[1][hv2];
1583 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1584 nat->nat_phnext[1] = natp;
1585 nat->nat_hnext[1] = *natp;
1588 nat_stats.ns_added++;
1589 nat_stats.ns_inuse++;
1593 nat_t *nat_icmplookup(ip, fin, dir)
1599 tcphdr_t *tcp = NULL;
1601 int flags = 0, type, minlen;
1603 icmp = (icmphdr_t *)fin->fin_dp;
1605 * Does it at least have the return (basic) IP header ?
1606 * Only a basic IP header (no options) should be with an ICMP error
1609 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1611 type = icmp->icmp_type;
1613 * If it's not an error type, then return.
1615 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1616 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1617 (type != ICMP_PARAMPROB))
1620 oip = (ip_t *)((char *)fin->fin_dp + 8);
1621 minlen = (oip->ip_hl << 2);
1622 if (minlen < sizeof(ip_t))
1624 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1627 * Is the buffer big enough for all of it ? It's the size of the IP
1628 * header claimed in the encapsulated part which is of concern. It
1629 * may be too big to be in this buffer but not so big that it's
1630 * outside the ICMP packet, leading to TCP deref's causing problems.
1631 * This is possible because we don't know how big oip_hl is when we
1632 * do the pullup early in fr_check() and thus can't gaurantee it is
1641 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1644 m = *(mb_t **)fin->fin_mp;
1645 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1646 (char *)ip + m->m_len)
1652 if (oip->ip_p == IPPROTO_TCP)
1654 else if (oip->ip_p == IPPROTO_UDP)
1656 if (flags & IPN_TCPUDP) {
1660 minlen += 8; /* + 64bits of data to get ports */
1661 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1664 data[0] = fin->fin_data[0];
1665 data[1] = fin->fin_data[1];
1666 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1667 fin->fin_data[0] = ntohs(tcp->th_dport);
1668 fin->fin_data[1] = ntohs(tcp->th_sport);
1670 if (dir == NAT_INBOUND) {
1671 nat = nat_inlookup(fin, flags, (u_int)oip->ip_p,
1672 oip->ip_dst, oip->ip_src, 0);
1674 nat = nat_outlookup(fin, flags, (u_int)oip->ip_p,
1675 oip->ip_dst, oip->ip_src, 0);
1677 fin->fin_data[0] = data[0];
1678 fin->fin_data[1] = data[1];
1681 if (dir == NAT_INBOUND)
1682 return nat_inlookup(fin, 0, (u_int)oip->ip_p,
1683 oip->ip_dst, oip->ip_src, 0);
1685 return nat_outlookup(fin, 0, (u_int)oip->ip_p,
1686 oip->ip_dst, oip->ip_src, 0);
1691 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1692 * packet gets correctly recognised.
1694 nat_t *nat_icmp(ip, fin, nflags, dir)
1700 u_32_t sum1, sum2, sumd, sumd2 = 0;
1709 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1712 * nat_icmplookup() will return NULL for `defective' packets.
1714 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1719 *nflags = IPN_ICMPERR;
1720 icmp = (icmphdr_t *)fin->fin_dp;
1721 oip = (ip_t *)&icmp->icmp_ip;
1722 if (oip->ip_p == IPPROTO_TCP)
1724 else if (oip->ip_p == IPPROTO_UDP)
1726 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1727 dlen = ip->ip_len - ((char *)udp - (char *)ip);
1729 * XXX - what if this is bogus hl and we go off the end ?
1730 * In this case, nat_icmplookup() will have returned NULL.
1732 tcp = (tcphdr_t *)udp;
1735 * Need to adjust ICMP header to include the real IP#'s and
1736 * port #'s. Only apply a checksum change relative to the
1737 * IP address change as it will be modified again in ip_natout
1738 * for both address and port. Two checksum changes are
1739 * necessary for the two header address changes. Be careful
1740 * to only modify the checksum once for the port # and twice
1746 * Fix the IP addresses in the offending IP packet. You also need
1747 * to adjust the IP header checksum of that offending IP packet
1748 * and the ICMP checksum of the ICMP error message itself.
1750 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1751 * in the pseudo header that is used to compute the UDP resp. TCP
1752 * checksum. So, we must compensate that as well. Even worse, the
1753 * change in the UDP and TCP checksums require yet another
1754 * adjustment of the ICMP checksum of the ICMP error message.
1758 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1759 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1763 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1764 in = nat->nat_outip;
1768 sum2 = LONG_SUM(ntohl(in.s_addr));
1770 CALC_SUMD(sum1, sum2, sumd);
1773 * Fix IP checksum of the offending IP packet to adjust for
1774 * the change in the IP address.
1776 * Normally, you would expect that the ICMP checksum of the
1777 * ICMP error message needs to be adjusted as well for the
1778 * IP address change in oip.
1779 * However, this is a NOP, because the ICMP checksum is
1780 * calculated over the complete ICMP packet, which includes the
1781 * changed oip IP addresses and oip->ip_sum. However, these
1782 * two changes cancel each other out (if the delta for
1783 * the IP address is x, then the delta for ip_sum is minus x),
1784 * so no change in the icmp_cksum is necessary.
1786 * Be careful that nat_dir refers to the direction of the
1787 * offending IP packet (oip), not to its ICMP response (icmp)
1789 fix_datacksum(&oip->ip_sum, sumd);
1790 /* Fix icmp cksum : IP Addr + Cksum */
1793 * Fix UDP pseudo header checksum to compensate for the
1794 * IP address change.
1796 if ((oip->ip_p == IPPROTO_UDP) && (dlen >= 8) && udp->uh_sum) {
1798 * The UDP checksum is optional, only adjust it
1799 * if it has been set.
1801 sum1 = ntohs(udp->uh_sum);
1802 fix_datacksum(&udp->uh_sum, sumd);
1803 sum2 = ntohs(udp->uh_sum);
1806 * Fix ICMP checksum to compensate the UDP
1807 * checksum adjustment.
1810 CALC_SUMD(sum1, sum2, sumd);
1815 * Fix TCP pseudo header checksum to compensate for the
1816 * IP address change. Before we can do the change, we
1817 * must make sure that oip is sufficient large to hold
1818 * the TCP checksum (normally it does not!).
1820 else if ((oip->ip_p == IPPROTO_TCP) && (dlen >= 18)) {
1821 sum1 = ntohs(tcp->th_sum);
1822 fix_datacksum(&tcp->th_sum, sumd);
1823 sum2 = ntohs(tcp->th_sum);
1826 * Fix ICMP checksum to compensate the TCP
1827 * checksum adjustment.
1830 CALC_SUMD(sum1, sum2, sumd);
1833 sumd2 = (sumd >> 16);
1834 if (nat->nat_dir == NAT_OUTBOUND)
1840 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
1843 * For offending TCP/UDP IP packets, translate the ports as
1844 * well, based on the NAT specification. Of course such
1845 * a change must be reflected in the ICMP checksum as well.
1847 * Advance notice : Now it becomes complicated :-)
1849 * Since the port fields are part of the TCP/UDP checksum
1850 * of the offending IP packet, you need to adjust that checksum
1851 * as well... but, if you change, you must change the icmp
1852 * checksum *again*, to reflect that change.
1854 * To further complicate: the TCP checksum is not in the first
1855 * 8 bytes of the offending ip packet, so it most likely is not
1856 * available. Some OSses like Solaris return enough bytes to
1857 * include the TCP checksum. So we have to check if the
1858 * ip->ip_len actually holds the TCP checksum of the oip!
1860 if (nat->nat_oport == tcp->th_dport) {
1861 if (tcp->th_sport != nat->nat_inport) {
1863 * Fix ICMP checksum to compensate port
1866 sum1 = ntohs(nat->nat_inport);
1867 sum2 = ntohs(tcp->th_sport);
1868 tcp->th_sport = nat->nat_inport;
1871 * Fix udp checksum to compensate port
1872 * adjustment. NOTE : the offending IP packet
1873 * flows the other direction compared to the
1876 * The UDP checksum is optional, only adjust
1877 * it if it has been set.
1879 if ((oip->ip_p == IPPROTO_UDP) &&
1880 (dlen >= 8) && udp->uh_sum) {
1884 sum1 = ntohs(udp->uh_sum);
1885 fix_datacksum(&udp->uh_sum, sumd);
1886 sum2 = ntohs(udp->uh_sum);
1889 * Fix ICMP checksum to compensate
1890 * UDP checksum adjustment.
1892 CALC_SUMD(sum1, sum2, sumd);
1897 * Fix tcp checksum (if present) to compensate
1898 * port adjustment. NOTE : the offending IP
1899 * packet flows the other direction compared to
1902 if (oip->ip_p == IPPROTO_TCP) {
1907 sum1 = ntohs(tcp->th_sum);
1908 fix_datacksum(&tcp->th_sum,
1910 sum2 = ntohs(tcp->th_sum);
1913 * Fix ICMP checksum to
1914 * compensate TCP checksum
1917 CALC_SUMD(sum1, sum2, sumd);
1920 sumd = sum2 - sum1 + 1;
1925 } else if (tcp->th_dport != nat->nat_outport) {
1927 * Fix ICMP checksum to compensate port
1930 sum1 = ntohs(nat->nat_outport);
1931 sum2 = ntohs(tcp->th_dport);
1932 tcp->th_dport = nat->nat_outport;
1935 * Fix udp checksum to compensate port
1936 * adjustment. NOTE : the offending IP
1937 * packet flows the other direction compared
1938 * to the ICMP message.
1940 * The UDP checksum is optional, only adjust
1941 * it if it has been set.
1943 if ((oip->ip_p == IPPROTO_UDP) &&
1944 (dlen >= 8) && udp->uh_sum) {
1948 sum1 = ntohs(udp->uh_sum);
1949 fix_datacksum(&udp->uh_sum, sumd);
1950 sum2 = ntohs(udp->uh_sum);
1953 * Fix ICMP checksum to compensate
1954 * UDP checksum adjustment.
1956 CALC_SUMD(sum1, sum2, sumd);
1960 * Fix tcp checksum (if present) to compensate
1961 * port adjustment. NOTE : the offending IP
1962 * packet flows the other direction compared to
1965 if (oip->ip_p == IPPROTO_TCP) {
1970 sum1 = ntohs(tcp->th_sum);
1971 fix_datacksum(&tcp->th_sum, sumd);
1972 sum2 = ntohs(tcp->th_sum);
1975 * Fix ICMP checksum to compensate
1976 * UDP checksum adjustment.
1978 CALC_SUMD(sum1, sum2, sumd);
1981 if (nat->nat_dir == NAT_OUTBOUND)
1988 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1989 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1990 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
1993 if (oip->ip_p == IPPROTO_ICMP)
1994 nat->nat_age = fr_defnaticmpage;
2000 * NB: these lookups don't lock access to the list, it assume it has already
2004 * Lookup a nat entry based on the mapped destination ip address/port and
2005 * real source address/port. We use this lookup when receiving a packet,
2006 * we're looking for a table entry, based on the destination address.
2007 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2009 nat_t *nat_inlookup(fin, flags, p, src, mapdst, rw)
2012 struct in_addr src , mapdst;
2015 u_short sport, dport;
2027 dst = mapdst.s_addr;
2028 if (flags & IPN_TCPUDP) {
2029 sport = htons(fin->fin_data[0]);
2030 dport = htons(fin->fin_data[1]);
2036 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
2037 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
2038 nat = nat_table[1][hv];
2039 for (; nat; nat = nat->nat_hnext[1]) {
2040 nflags = nat->nat_flags;
2041 if ((!ifp || ifp == nat->nat_ifp) &&
2042 nat->nat_oip.s_addr == src.s_addr &&
2043 nat->nat_outip.s_addr == dst &&
2044 ((p == 0) || (p == nat->nat_p))) {
2049 if (nat->nat_oport != sport)
2051 if (nat->nat_outport != dport)
2059 if ((ipn != NULL) && (nat->nat_aps != NULL))
2060 if (appr_match(fin, nat) != 0)
2065 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2068 RWLOCK_EXIT(&ipf_nat);
2070 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
2071 hv = NAT_HASH_FN(src.s_addr, dst, ipf_nattable_sz);
2073 WRITE_ENTER(&ipf_nat);
2075 nat = nat_table[1][hv];
2076 for (; nat; nat = nat->nat_hnext[1]) {
2077 nflags = nat->nat_flags;
2078 if (ifp && ifp != nat->nat_ifp)
2080 if (!(nflags & FI_WILDP))
2082 if (nat->nat_oip.s_addr != src.s_addr ||
2083 nat->nat_outip.s_addr != dst)
2085 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
2086 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
2087 nat_tabmove(fin, nat);
2092 MUTEX_DOWNGRADE(&ipf_nat);
2099 * This function is only called for TCP/UDP NAT table entries where the
2100 * original was placed in the table without hashing on the ports and we now
2101 * want to include hashing on port numbers.
2103 static void nat_tabmove(fin, nat)
2107 u_short sport, dport;
2111 nflags = nat->nat_flags;
2113 sport = ntohs(fin->fin_data[0]);
2114 dport = ntohs(fin->fin_data[1]);
2117 * Remove the NAT entry from the old location
2119 if (nat->nat_hnext[0])
2120 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2121 *nat->nat_phnext[0] = nat->nat_hnext[0];
2123 if (nat->nat_hnext[1])
2124 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2125 *nat->nat_phnext[1] = nat->nat_hnext[1];
2128 * Add into the NAT table in the new position
2130 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2131 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2132 natp = &nat_table[0][hv];
2134 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2135 nat->nat_phnext[0] = natp;
2136 nat->nat_hnext[0] = *natp;
2139 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2140 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2141 natp = &nat_table[1][hv];
2143 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2144 nat->nat_phnext[1] = natp;
2145 nat->nat_hnext[1] = *natp;
2151 * Lookup a nat entry based on the source 'real' ip address/port and
2152 * destination address/port. We use this lookup when sending a packet out,
2153 * we're looking for a table entry, based on the source address.
2154 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2156 nat_t *nat_outlookup(fin, flags, p, src, dst, rw)
2159 struct in_addr src , dst;
2162 u_short sport, dport;
2172 if (flags & IPN_TCPUDP) {
2173 sport = ntohs(fin->fin_data[0]);
2174 dport = ntohs(fin->fin_data[1]);
2180 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2181 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2182 nat = nat_table[0][hv];
2183 for (; nat; nat = nat->nat_hnext[0]) {
2184 nflags = nat->nat_flags;
2186 if ((!ifp || ifp == nat->nat_ifp) &&
2187 nat->nat_inip.s_addr == srcip &&
2188 nat->nat_oip.s_addr == dst.s_addr &&
2189 ((p == 0) || (p == nat->nat_p))) {
2194 if (nat->nat_oport != dport)
2196 if (nat->nat_inport != sport)
2204 if ((ipn != NULL) && (nat->nat_aps != NULL))
2205 if (appr_match(fin, nat) != 0)
2210 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2213 RWLOCK_EXIT(&ipf_nat);
2216 hv = NAT_HASH_FN(dst.s_addr, srcip, ipf_nattable_sz);
2218 WRITE_ENTER(&ipf_nat);
2220 nat = nat_table[0][hv];
2221 for (; nat; nat = nat->nat_hnext[0]) {
2222 nflags = nat->nat_flags;
2223 if (ifp && ifp != nat->nat_ifp)
2225 if (!(nflags & FI_WILDP))
2227 if ((nat->nat_inip.s_addr != srcip) ||
2228 (nat->nat_oip.s_addr != dst.s_addr))
2230 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2231 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2232 nat_tabmove(fin, nat);
2237 MUTEX_DOWNGRADE(&ipf_nat);
2244 * Lookup the NAT tables to search for a matching redirect
2246 nat_t *nat_lookupredir(np)
2252 bzero((char *)&fi, sizeof(fi));
2253 fi.fin_data[0] = ntohs(np->nl_inport);
2254 fi.fin_data[1] = ntohs(np->nl_outport);
2257 * If nl_inip is non null, this is a lookup based on the real
2258 * ip address. Else, we use the fake.
2260 if ((nat = nat_outlookup(&fi, np->nl_flags, 0, np->nl_inip,
2261 np->nl_outip, 0))) {
2262 np->nl_realip = nat->nat_outip;
2263 np->nl_realport = nat->nat_outport;
2269 static int nat_match(fin, np, ip)
2279 if (np->in_p && fin->fin_p != np->in_p)
2282 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2284 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2285 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2287 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2288 ^ ((np->in_flags & IPN_NOTDST) != 0))
2291 if (!(np->in_redir & NAT_REDIRECT))
2293 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2294 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2296 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2297 ^ ((np->in_flags & IPN_NOTDST) != 0))
2302 if (!(fin->fin_fl & FI_TCPUDP) ||
2303 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2304 if (ft->ftu_scmp || ft->ftu_dcmp)
2309 return fr_tcpudpchk(ft, fin);
2314 * Packets going out on the external interface go through this.
2315 * Here, the source address requires alteration, if anything.
2317 int ip_natout(ip, fin)
2323 tcphdr_t *tcp = NULL;
2324 u_short sport = 0, dport = 0, *csump = NULL;
2325 int natadd = 1, i, icmpset = 1;
2326 u_int nflags = 0, hv, msk;
2333 if (nat_list == NULL || (fr_nat_lock))
2336 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2337 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1) {
2338 sifp = fin->fin_ifp;
2339 fin->fin_ifp = fr->fr_tif.fd_ifp;
2341 sifp = fin->fin_ifp;
2344 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2345 if (fin->fin_p == IPPROTO_TCP)
2347 else if (fin->fin_p == IPPROTO_UDP)
2349 if ((nflags & IPN_TCPUDP)) {
2350 tcp = (tcphdr_t *)fin->fin_dp;
2351 sport = tcp->th_sport;
2352 dport = tcp->th_dport;
2356 ipa = fin->fin_saddr;
2358 READ_ENTER(&ipf_nat);
2360 if ((fin->fin_p == IPPROTO_ICMP) &&
2361 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2363 else if ((fin->fin_fl & FI_FRAG) &&
2364 (nat = ipfr_nat_knownfrag(ip, fin)))
2366 else if ((nat = nat_outlookup(fin, nflags|FI_WILDP|FI_WILDA,
2367 (u_int)fin->fin_p, fin->fin_src,
2368 fin->fin_dst, 0))) {
2369 nflags = nat->nat_flags;
2370 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2371 if ((nflags & FI_W_SPORT) &&
2372 (nat->nat_inport != sport))
2373 nat->nat_inport = sport;
2374 if ((nflags & FI_W_DPORT) &&
2375 (nat->nat_oport != dport))
2376 nat->nat_oport = dport;
2378 if (nat->nat_outport == 0)
2379 nat->nat_outport = sport;
2380 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2381 nflags = nat->nat_flags;
2382 nat_stats.ns_wilds--;
2385 RWLOCK_EXIT(&ipf_nat);
2390 WRITE_ENTER(&ipf_nat);
2392 * If there is no current entry in the nat table for this IP#,
2393 * create one for it (if there is a matching rule).
2396 iph = ipa & htonl(msk);
2397 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2398 for (np = nat_rules[hv]; np; np = np->in_mnext)
2400 if (np->in_ifp && (np->in_ifp != ifp))
2402 if ((np->in_flags & IPN_RF) &&
2403 !(np->in_flags & nflags))
2405 if (np->in_flags & IPN_FILTER) {
2406 if (!nat_match(fin, np, ip))
2408 } else if ((ipa & np->in_inmsk) != np->in_inip)
2410 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2412 nat = nat_new(fin, ip, np, NULL,
2413 (u_int)nflags, NAT_OUTBOUND);
2419 if ((np == NULL) && (i > 0)) {
2423 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2427 MUTEX_DOWNGRADE(&ipf_nat);
2431 * NOTE: ipf_nat must now only be held as a read lock
2435 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2436 ipfr_nat_newfrag(ip, fin, nat);
2437 MUTEX_ENTER(&nat->nat_lock);
2438 if (fin->fin_p != IPPROTO_TCP) {
2439 if (np && np->in_age[1])
2440 nat->nat_age = np->in_age[1];
2441 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2442 nat->nat_age = fr_defnaticmpage;
2444 nat->nat_age = fr_defnatage;
2446 nat->nat_bytes += ip->ip_len;
2448 MUTEX_EXIT(&nat->nat_lock);
2451 * Fix up checksums, not by recalculating them, but
2452 * simply computing adjustments.
2454 if (nflags == IPN_ICMPERR) {
2455 u_32_t s1, s2, sumd;
2457 s1 = LONG_SUM(ntohl(fin->fin_saddr));
2458 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2459 CALC_SUMD(s1, s2, sumd);
2460 fix_outcksum(fin, &ip->ip_sum, sumd);
2462 #if (SOLARIS || defined(__sgi)) || !defined(_KERNEL)
2464 if (nat->nat_dir == NAT_OUTBOUND)
2465 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2467 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2471 * Only change the packet contents, not what is filtered upon.
2473 ip->ip_src = nat->nat_outip;
2475 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2477 if ((nat->nat_outport != 0) && (tcp != NULL)) {
2478 tcp->th_sport = nat->nat_outport;
2479 fin->fin_data[0] = ntohs(tcp->th_sport);
2482 if (fin->fin_p == IPPROTO_TCP) {
2483 csump = &tcp->th_sum;
2484 MUTEX_ENTER(&nat->nat_lock);
2485 fr_tcp_age(&nat->nat_age,
2486 nat->nat_tcpstate, fin, 1, 0);
2487 if (nat->nat_age < fr_defnaticmpage)
2488 nat->nat_age = fr_defnaticmpage;
2490 else if ((!np || !np->in_age[1]) &&
2491 (nat->nat_age > fr_defnatage))
2492 nat->nat_age = fr_defnatage;
2495 * Increase this because we may have
2496 * "keep state" following this too and
2497 * packet storms can occur if this is
2498 * removed too quickly.
2500 if (nat->nat_age == fr_tcpclosed)
2501 nat->nat_age = fr_tcplastack;
2504 * Do a MSS CLAMPING on a SYN packet,
2505 * only deal IPv4 for now.
2507 if (nat->nat_mssclamp &&
2508 (tcp->th_flags & TH_SYN) != 0)
2509 nat_mssclamp(tcp, nat->nat_mssclamp,
2512 MUTEX_EXIT(&nat->nat_lock);
2513 } else if (fin->fin_p == IPPROTO_UDP) {
2514 udphdr_t *udp = (udphdr_t *)tcp;
2517 csump = &udp->uh_sum;
2521 if (nat->nat_dir == NAT_OUTBOUND)
2522 fix_outcksum(fin, csump,
2525 fix_incksum(fin, csump,
2530 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2531 (tcp != NULL && dport == np->in_dport))) {
2532 i = appr_check(ip, fin, nat);
2539 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2540 RWLOCK_EXIT(&ipf_nat); /* READ */
2541 fin->fin_ifp = sifp;
2544 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2545 fin->fin_ifp = sifp;
2551 * Packets coming in from the external interface go through this.
2552 * Here, the destination address requires alteration, if anything.
2554 int ip_natin(ip, fin)
2561 u_short sport = 0, dport = 0, *csump = NULL;
2562 u_int nflags = 0, natadd = 1, hv, msk;
2563 struct ifnet *ifp = fin->fin_ifp;
2564 tcphdr_t *tcp = NULL;
2569 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2572 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2573 if (fin->fin_p == IPPROTO_TCP)
2575 else if (fin->fin_p == IPPROTO_UDP)
2577 if ((nflags & IPN_TCPUDP)) {
2578 tcp = (tcphdr_t *)fin->fin_dp;
2579 sport = tcp->th_sport;
2580 dport = tcp->th_dport;
2585 /* make sure the source address is to be redirected */
2588 READ_ENTER(&ipf_nat);
2590 if ((fin->fin_p == IPPROTO_ICMP) &&
2591 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2593 else if ((fin->fin_fl & FI_FRAG) &&
2594 (nat = ipfr_nat_knownfrag(ip, fin)))
2596 else if ((nat = nat_inlookup(fin, nflags|FI_WILDP|FI_WILDA,
2597 (u_int)fin->fin_p, fin->fin_src, in, 0))) {
2598 nflags = nat->nat_flags;
2599 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2600 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2601 nat->nat_oport = sport;
2602 if ((nat->nat_outport != dport) &&
2603 (nflags & FI_W_SPORT))
2604 nat->nat_outport = dport;
2605 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2606 nflags = nat->nat_flags;
2607 nat_stats.ns_wilds--;
2610 RWLOCK_EXIT(&ipf_nat);
2615 WRITE_ENTER(&ipf_nat);
2617 * If there is no current entry in the nat table for this IP#,
2618 * create one for it (if there is a matching rule).
2621 iph = in.s_addr & htonl(msk);
2622 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2623 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2624 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2625 (np->in_p && (np->in_p != fin->fin_p)) ||
2626 (np->in_flags && !(nflags & np->in_flags)))
2628 if (np->in_flags & IPN_FILTER) {
2629 if (!nat_match(fin, np, ip))
2631 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2633 if ((!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2634 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2635 (ntohs(dport) >= ntohs(np->in_pmin)))))
2636 if ((nat = nat_new(fin, ip, np, NULL, nflags,
2643 if ((np == NULL) && (i > 0)) {
2647 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2651 MUTEX_DOWNGRADE(&ipf_nat);
2655 * NOTE: ipf_nat must now only be held as a read lock
2659 fin->fin_fr = nat->nat_fr;
2660 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2661 ipfr_nat_newfrag(ip, fin, nat);
2662 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2663 (tcp != NULL && sport == np->in_dport))) {
2664 i = appr_check(ip, fin, nat);
2667 RWLOCK_EXIT(&ipf_nat);
2672 MUTEX_ENTER(&nat->nat_lock);
2673 if (fin->fin_p != IPPROTO_TCP) {
2674 if (np && np->in_age[0])
2675 nat->nat_age = np->in_age[0];
2676 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2677 nat->nat_age = fr_defnaticmpage;
2679 nat->nat_age = fr_defnatage;
2681 nat->nat_bytes += ip->ip_len;
2683 MUTEX_EXIT(&nat->nat_lock);
2686 * Fix up checksums, not by recalculating them, but
2687 * simply computing adjustments.
2689 if (nat->nat_dir == NAT_OUTBOUND)
2690 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2692 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2694 ip->ip_dst = nat->nat_inip;
2695 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2697 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2699 if ((nat->nat_inport != 0) && (tcp != NULL)) {
2700 tcp->th_dport = nat->nat_inport;
2701 fin->fin_data[1] = ntohs(tcp->th_dport);
2704 if (fin->fin_p == IPPROTO_TCP) {
2705 csump = &tcp->th_sum;
2706 MUTEX_ENTER(&nat->nat_lock);
2707 fr_tcp_age(&nat->nat_age,
2708 nat->nat_tcpstate, fin, 0, 0);
2709 if (nat->nat_age < fr_defnaticmpage)
2710 nat->nat_age = fr_defnaticmpage;
2712 else if ((!np || !np->in_age[0]) &&
2713 (nat->nat_age > fr_defnatage))
2714 nat->nat_age = fr_defnatage;
2717 * Increase this because we may have
2718 * "keep state" following this too and
2719 * packet storms can occur if this is
2720 * removed too quickly.
2722 if (nat->nat_age == fr_tcpclosed)
2723 nat->nat_age = fr_tcplastack;
2725 * Do a MSS CLAMPING on a SYN packet,
2726 * only deal IPv4 for now.
2728 if (nat->nat_mssclamp &&
2729 (tcp->th_flags & TH_SYN) != 0)
2730 nat_mssclamp(tcp, nat->nat_mssclamp,
2733 MUTEX_EXIT(&nat->nat_lock);
2734 } else if (fin->fin_p == IPPROTO_UDP) {
2735 udphdr_t *udp = (udphdr_t *)tcp;
2738 csump = &udp->uh_sum;
2742 if (nat->nat_dir == NAT_OUTBOUND)
2743 fix_incksum(fin, csump,
2746 fix_outcksum(fin, csump,
2750 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2751 RWLOCK_EXIT(&ipf_nat); /* READ */
2754 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2760 * Free all memory used by NAT structures allocated at runtime.
2764 WRITE_ENTER(&ipf_nat);
2765 (void) nat_clearlist();
2766 (void) nat_flushtable();
2767 RWLOCK_EXIT(&ipf_nat);
2769 if (nat_table[0] != NULL) {
2770 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2771 nat_table[0] = NULL;
2773 if (nat_table[1] != NULL) {
2774 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2775 nat_table[1] = NULL;
2777 if (nat_rules != NULL) {
2778 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2781 if (rdr_rules != NULL) {
2782 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2785 if (maptable != NULL) {
2786 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2793 * Slowly expire held state for NAT entries. Timeouts are set in
2794 * expectation of this being called twice per second.
2798 struct nat *nat, **natp;
2799 #if defined(_KERNEL) && !SOLARIS && !defined(__DragonFly__)
2804 WRITE_ENTER(&ipf_nat);
2805 for (natp = &nat_instances; (nat = *natp); ) {
2808 natp = &nat->nat_next;
2811 *natp = nat->nat_next;
2813 nat_log(nat, NL_EXPIRE);
2816 nat_stats.ns_expire++;
2818 RWLOCK_EXIT(&ipf_nat);
2825 void ip_natsync(ifp)
2830 u_32_t sum1, sum2, sumd;
2834 #if defined(_KERNEL) && !SOLARIS && !defined(__DragonFly__)
2839 * Change IP addresses for NAT sessions for any protocol except TCP
2840 * since it will break the TCP connection anyway.
2843 WRITE_ENTER(&ipf_nat);
2844 for (nat = nat_instances; nat; nat = nat->nat_next)
2845 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2846 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2847 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2848 ifp2 = nat->nat_ifp;
2850 * Change the map-to address to be the same as the
2853 sum1 = nat->nat_outip.s_addr;
2854 if (fr_ifpaddr(4, ifp2, &in) != -1)
2855 nat->nat_outip = in;
2856 sum2 = nat->nat_outip.s_addr;
2861 * Readjust the checksum adjustment to take into
2862 * account the new IP#.
2864 CALC_SUMD(sum1, sum2, sumd);
2865 /* XXX - dont change for TCP when solaris does
2866 * hardware checksumming.
2868 sumd += nat->nat_sumd[0];
2869 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2870 nat->nat_sumd[1] = nat->nat_sumd[0];
2873 for (n = nat_list; (n != NULL); n = n->in_next)
2874 if (n->in_ifp == ifp) {
2875 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2877 n->in_ifp = (void *)-1;
2879 RWLOCK_EXIT(&ipf_nat);
2885 void nat_log(nat, type)
2898 natl.nl_inip = nat->nat_inip;
2899 natl.nl_outip = nat->nat_outip;
2900 natl.nl_origip = nat->nat_oip;
2901 natl.nl_bytes = nat->nat_bytes;
2902 natl.nl_pkts = nat->nat_pkts;
2903 natl.nl_origport = nat->nat_oport;
2904 natl.nl_inport = nat->nat_inport;
2905 natl.nl_outport = nat->nat_outport;
2906 natl.nl_p = nat->nat_p;
2907 natl.nl_type = type;
2910 if (nat->nat_ptr != NULL) {
2911 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2912 if (np == nat->nat_ptr) {
2913 natl.nl_rule = rulen;
2919 sizes[0] = sizeof(natl);
2922 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);
2927 #if defined(__OpenBSD__)
2928 void nat_ifdetach(ifp)
2938 * Check for MSS option and clamp it if necessary.
2940 static void nat_mssclamp(tcp, maxmss, fin, csump)
2946 u_char *cp, *ep, opt;
2951 hlen = tcp->th_off << 2;
2952 if (hlen > sizeof(*tcp)) {
2953 cp = (u_char *)tcp + sizeof(*tcp);
2954 ep = (u_char *)tcp + hlen;
2958 if (opt == TCPOPT_EOL)
2960 else if (opt == TCPOPT_NOP) {
2968 if (&cp[advance] > ep)
2974 bcopy(&cp[2], &v, sizeof(v));
2978 bcopy(&v, &cp[2], sizeof(v));
2979 CALC_SUMD(mss, maxmss, sumd);
2980 fix_outcksum(fin, csump, sumd);
2984 /* ignore unknown options */