2 * Copyright (C) 1995-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * Added redirect stuff and a LOT of bug fixes. (mcn@EnGarde.com)
9 #if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
13 #include <sys/errno.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
17 #if (__FreeBSD_version >= 500028)
18 #include <sys/queue.h>
21 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
23 # include "opt_ipfilter_log.h"
25 #if !defined(_KERNEL) && !defined(KERNEL)
30 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
31 # include <sys/filio.h>
32 # include <sys/fcntl.h>
34 # include <sys/ioctl.h>
36 #include <sys/fcntl.h>
39 # include <sys/protosw.h>
41 #include <sys/socket.h>
42 #if defined(_KERNEL) && !defined(linux)
43 # include <sys/systm.h>
45 #if !defined(__SVR4) && !defined(__svr4__)
47 # include <sys/mbuf.h>
50 # include <sys/filio.h>
51 # include <sys/byteorder.h>
53 # include <sys/dditypes.h>
55 # include <sys/stream.h>
56 # include <sys/kmem.h>
58 #if __FreeBSD_version >= 300000
59 # include <sys/queue.h>
62 #if __FreeBSD_version >= 300000
63 # include <net/if_var.h>
64 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
65 # include "opt_ipfilter.h"
71 #include <net/route.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
77 # ifdef IFF_DRVRLOCK /* IRIX6 */
78 #include <sys/hashing.h>
79 #include <netinet/in_var.h>
85 # include <vpn/ipsec.h>
86 extern struct ifnet vpnif;
90 # include <netinet/ip_var.h>
91 # include <netinet/tcp_fsm.h>
93 #include <netinet/tcp.h>
94 #include <netinet/udp.h>
95 #include <netinet/ip_icmp.h>
96 #include "netinet/ip_compat.h"
97 #include <netinet/tcpip.h>
98 #include "netinet/ip_fil.h"
99 #include "netinet/ip_proxy.h"
100 #include "netinet/ip_nat.h"
101 #include "netinet/ip_frag.h"
102 #include "netinet/ip_state.h"
103 #if (__FreeBSD_version >= 300000)
104 # include <sys/malloc.h>
107 # define MIN(a,b) (((a)<(b))?(a):(b))
110 #define SOCKADDR_IN struct sockaddr_in
113 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
114 /* static const char rcsid[] = "@(#)$Id: ip_nat.c,v 2.37.2.44 2001/07/21 07:17:22 darrenr Exp $"; */
115 static const char rcsid[] = "@(#)$FreeBSD$";
118 nat_t **nat_table[2] = { NULL, NULL },
119 *nat_instances = NULL;
120 ipnat_t *nat_list = NULL;
121 u_int ipf_nattable_sz = NAT_TABLE_SZ;
122 u_int ipf_natrules_sz = NAT_SIZE;
123 u_int ipf_rdrrules_sz = RDR_SIZE;
124 u_int ipf_hostmap_sz = HOSTMAP_SIZE;
125 u_32_t nat_masks = 0;
126 u_32_t rdr_masks = 0;
127 ipnat_t **nat_rules = NULL;
128 ipnat_t **rdr_rules = NULL;
129 hostmap_t **maptable = NULL;
131 u_long fr_defnatage = DEF_NAT_AGE,
132 fr_defnaticmpage = 6; /* 3 seconds */
135 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
136 extern kmutex_t ipf_rw;
137 extern KRWLOCK_T ipf_nat;
140 static int nat_flushtable __P((void));
141 static int nat_clearlist __P((void));
142 static void nat_addnat __P((struct ipnat *));
143 static void nat_addrdr __P((struct ipnat *));
144 static void nat_delete __P((struct nat *));
145 static void nat_delrdr __P((struct ipnat *));
146 static void nat_delnat __P((struct ipnat *));
147 static int fr_natgetent __P((caddr_t));
148 static int fr_natgetsz __P((caddr_t));
149 static int fr_natputent __P((caddr_t));
150 static void nat_tabmove __P((nat_t *, u_32_t));
151 static int nat_match __P((fr_info_t *, ipnat_t *, ip_t *));
152 static hostmap_t *nat_hostmap __P((ipnat_t *, struct in_addr,
154 static void nat_hostmapdel __P((struct hostmap *));
159 KMALLOCS(nat_table[0], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
160 if (nat_table[0] != NULL)
161 bzero((char *)nat_table[0], ipf_nattable_sz * sizeof(nat_t *));
165 KMALLOCS(nat_table[1], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
166 if (nat_table[1] != NULL)
167 bzero((char *)nat_table[1], ipf_nattable_sz * sizeof(nat_t *));
171 KMALLOCS(nat_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_natrules_sz);
172 if (nat_rules != NULL)
173 bzero((char *)nat_rules, ipf_natrules_sz * sizeof(ipnat_t *));
177 KMALLOCS(rdr_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_rdrrules_sz);
178 if (rdr_rules != NULL)
179 bzero((char *)rdr_rules, ipf_rdrrules_sz * sizeof(ipnat_t *));
183 KMALLOCS(maptable, hostmap_t **, sizeof(hostmap_t *) * ipf_hostmap_sz);
184 if (maptable != NULL)
185 bzero((char *)maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
192 static void nat_addrdr(n)
200 k = countbits(n->in_outmsk);
201 if ((k >= 0) && (k != 32))
203 j = (n->in_outip & n->in_outmsk);
204 hv = NAT_HASH_FN(j, 0, ipf_rdrrules_sz);
207 np = &(*np)->in_rnext;
214 static void nat_addnat(n)
222 k = countbits(n->in_inmsk);
223 if ((k >= 0) && (k != 32))
225 j = (n->in_inip & n->in_inmsk);
226 hv = NAT_HASH_FN(j, 0, ipf_natrules_sz);
229 np = &(*np)->in_mnext;
236 static void nat_delrdr(n)
240 n->in_rnext->in_prnext = n->in_prnext;
241 *n->in_prnext = n->in_rnext;
245 static void nat_delnat(n)
249 n->in_mnext->in_pmnext = n->in_pmnext;
250 *n->in_pmnext = n->in_mnext;
255 * check if an ip address has already been allocated for a given mapping that
256 * is not doing port based translation.
258 * Must be called with ipf_nat held as a write lock.
260 static struct hostmap *nat_hostmap(np, real, map)
268 hv = real.s_addr % HOSTMAP_SIZE;
269 for (hm = maptable[hv]; hm; hm = hm->hm_next)
270 if ((hm->hm_realip.s_addr == real.s_addr) &&
271 (np == hm->hm_ipnat)) {
276 KMALLOC(hm, hostmap_t *);
278 hm->hm_next = maptable[hv];
279 hm->hm_pnext = maptable + hv;
281 maptable[hv]->hm_pnext = &hm->hm_next;
284 hm->hm_realip = real;
293 * Must be called with ipf_nat held as a write lock.
295 static void nat_hostmapdel(hm)
298 ATOMIC_DEC32(hm->hm_ref);
299 if (hm->hm_ref == 0) {
301 hm->hm_next->hm_pnext = hm->hm_pnext;
302 *hm->hm_pnext = hm->hm_next;
308 void fix_outcksum(fin, sp, n)
313 register u_short sumshort;
314 register u_32_t sum1;
318 else if (n & NAT_HW_CKSUM) {
321 n = (n & 0xffff) + (n >> 16);
325 sum1 = (~ntohs(*sp)) & 0xffff;
327 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
329 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
330 sumshort = ~(u_short)sum1;
331 *(sp) = htons(sumshort);
335 void fix_incksum(fin, sp, n)
340 register u_short sumshort;
341 register u_32_t sum1;
345 else if (n & NAT_HW_CKSUM) {
348 n = (n & 0xffff) + (n >> 16);
353 sum1 = (~(*sp)) & 0xffff;
355 sum1 = (~ntohs(*sp)) & 0xffff;
357 sum1 += ~(n) & 0xffff;
358 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
360 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
361 sumshort = ~(u_short)sum1;
362 *(sp) = htons(sumshort);
367 * fix_datacksum is used *only* for the adjustments of checksums in the data
368 * section of an IP packet.
370 * The only situation in which you need to do this is when NAT'ing an
371 * ICMP error message. Such a message, contains in its body the IP header
372 * of the original IP packet, that causes the error.
374 * You can't use fix_incksum or fix_outcksum in that case, because for the
375 * kernel the data section of the ICMP error is just data, and no special
376 * processing like hardware cksum or ntohs processing have been done by the
377 * kernel on the data section.
379 void fix_datacksum(sp, n)
383 register u_short sumshort;
384 register u_32_t sum1;
389 sum1 = (~ntohs(*sp)) & 0xffff;
391 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
393 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
394 sumshort = ~(u_short)sum1;
395 *(sp) = htons(sumshort);
399 * How the NAT is organised and works.
401 * Inside (interface y) NAT Outside (interface x)
402 * -------------------- -+- -------------------------------------
403 * Packet going | out, processsed by ip_natout() for x
404 * ------------> | ------------>
405 * src=10.1.1.1 | src=192.1.1.1
407 * | in, processed by ip_natin() for x
408 * <------------ | <------------
409 * dst=10.1.1.1 | dst=192.1.1.1
410 * -------------------- -+- -------------------------------------
411 * ip_natout() - changes ip_src and if required, sport
412 * - creates a new mapping, if required.
413 * ip_natin() - changes ip_dst and if required, dport
415 * In the NAT table, internal source is recorded as "in" and externally
420 * Handle ioctls which manipulate the NAT.
422 int nat_ioctl(data, cmd, mode)
423 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
431 register ipnat_t *nat, *nt, *n = NULL, **np = NULL;
432 int error = 0, ret, arg;
436 #if (BSD >= 199306) && defined(_KERNEL)
437 if ((securelevel >= 3) && (mode & FWRITE))
441 nat = NULL; /* XXX gcc -Wuninitialized */
442 KMALLOC(nt, ipnat_t *);
443 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT))
444 error = IRCOPYPTR(data, (char *)&natd, sizeof(natd));
445 else if (cmd == SIOCIPFFL) { /* SIOCFLNAT & SIOCCNATL */
446 error = IRCOPY(data, (char *)&arg, sizeof(arg));
455 * For add/delete, look to see if the NAT entry is already present
457 WRITE_ENTER(&ipf_nat);
458 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
460 nat->in_flags &= IPN_USERFLAGS;
461 if ((nat->in_redir & NAT_MAPBLK) == 0) {
462 if ((nat->in_flags & IPN_SPLIT) == 0)
463 nat->in_inip &= nat->in_inmsk;
464 if ((nat->in_flags & IPN_IPRANGE) == 0)
465 nat->in_outip &= nat->in_outmsk;
467 for (np = &nat_list; (n = *np); np = &n->in_next)
468 if (!bcmp((char *)&nat->in_flags, (char *)&n->in_flags,
480 if (!(mode & FWRITE))
483 tmp = ipflog_clear(IPL_LOGNAT);
484 IWCOPY((char *)&tmp, (char *)data, sizeof(tmp));
490 if (!(mode & FWRITE)) {
504 bcopy((char *)nat, (char *)n, sizeof(*n));
505 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
507 n->in_ifp = (void *)-1;
508 if (n->in_plabel[0] != '\0') {
509 n->in_apr = appr_match(n->in_p, n->in_plabel);
518 if (n->in_redir & NAT_REDIRECT) {
519 n->in_flags &= ~IPN_NOTDST;
522 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
523 n->in_flags &= ~IPN_NOTSRC;
528 if (n->in_redir & NAT_MAPBLK)
529 n->in_space = USABLE_PORTS * ~ntohl(n->in_outmsk);
530 else if (n->in_flags & IPN_AUTOPORTMAP)
531 n->in_space = USABLE_PORTS * ~ntohl(n->in_inmsk);
532 else if (n->in_flags & IPN_IPRANGE)
533 n->in_space = ntohl(n->in_outmsk) - ntohl(n->in_outip);
534 else if (n->in_flags & IPN_SPLIT)
537 n->in_space = ~ntohl(n->in_outmsk);
539 * Calculate the number of valid IP addresses in the output
540 * mapping range. In all cases, the range is inclusive of
541 * the start and ending IP addresses.
542 * If to a CIDR address, lose 2: broadcast + network address
544 * If to a range, add one.
545 * If to a single IP address, set to 1.
548 if ((n->in_flags & IPN_IPRANGE) != 0)
554 if ((n->in_outmsk != 0xffffffff) && (n->in_outmsk != 0) &&
555 ((n->in_flags & (IPN_IPRANGE|IPN_SPLIT)) == 0))
556 n->in_nip = ntohl(n->in_outip) + 1;
557 else if ((n->in_flags & IPN_SPLIT) &&
558 (n->in_redir & NAT_REDIRECT))
559 n->in_nip = ntohl(n->in_inip);
561 n->in_nip = ntohl(n->in_outip);
562 if (n->in_redir & NAT_MAP) {
563 n->in_pnext = ntohs(n->in_pmin);
565 * Multiply by the number of ports made available.
567 if (ntohs(n->in_pmax) >= ntohs(n->in_pmin)) {
568 n->in_space *= (ntohs(n->in_pmax) -
569 ntohs(n->in_pmin) + 1);
571 * Because two different sources can map to
572 * different destinations but use the same
574 * If the result is smaller than in_space, then
575 * we may have wrapped around 32bits.
578 if ((i != 0) && (i != 0xffffffff)) {
579 j = n->in_space * (~ntohl(i) + 1);
580 if (j >= n->in_space)
583 n->in_space = 0xffffffff;
587 * If no protocol is specified, multiple by 256.
589 if ((n->in_flags & IPN_TCPUDP) == 0) {
590 j = n->in_space * 256;
591 if (j >= n->in_space)
594 n->in_space = 0xffffffff;
597 /* Otherwise, these fields are preset */
599 nat_stats.ns_rules++;
602 if (!(mode & FWRITE)) {
611 if (n->in_redir & NAT_REDIRECT)
613 if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
615 if (nat_list == NULL) {
622 appr_free(n->in_apr);
624 nat_stats.ns_rules--;
626 n->in_flags |= IPN_DELETE;
632 MUTEX_DOWNGRADE(&ipf_nat);
633 nat_stats.ns_table[0] = nat_table[0];
634 nat_stats.ns_table[1] = nat_table[1];
635 nat_stats.ns_list = nat_list;
636 nat_stats.ns_maptable = maptable;
637 nat_stats.ns_nattab_sz = ipf_nattable_sz;
638 nat_stats.ns_rultab_sz = ipf_natrules_sz;
639 nat_stats.ns_rdrtab_sz = ipf_rdrrules_sz;
640 nat_stats.ns_hostmap_sz = ipf_hostmap_sz;
641 nat_stats.ns_instances = nat_instances;
642 nat_stats.ns_apslist = ap_sess_list;
643 error = IWCOPYPTR((char *)&nat_stats, (char *)data,
650 MUTEX_DOWNGRADE(&ipf_nat);
651 error = IRCOPYPTR((char *)data, (char *)&nl, sizeof(nl));
655 if (nat_lookupredir(&nl)) {
656 error = IWCOPYPTR((char *)&nl, (char *)data,
662 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
663 if (!(mode & FWRITE)) {
669 ret = nat_flushtable();
671 ret = nat_clearlist();
674 MUTEX_DOWNGRADE(&ipf_nat);
676 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
682 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
684 error = IWCOPY((caddr_t)&fr_nat_lock, data,
685 sizeof(fr_nat_lock));
693 error = fr_natputent(data);
699 error = fr_natgetsz(data);
705 error = fr_natgetent(data);
711 arg = (int)iplused[IPL_LOGNAT];
712 MUTEX_DOWNGRADE(&ipf_nat);
713 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
722 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
730 static int fr_natgetsz(data)
738 error = IRCOPY(data, (caddr_t)&ng, sizeof(ng));
747 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
754 * Make sure the pointer we're copying from exists in the
755 * current list of entries. Security precaution to prevent
756 * copying of random kernel data.
758 for (n = nat_instances; n; n = n->nat_next)
765 ng.ng_sz = sizeof(nat_save_t);
767 if ((aps != NULL) && (aps->aps_data != 0)) {
768 ng.ng_sz += sizeof(ap_session_t);
769 ng.ng_sz += aps->aps_psiz;
772 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
779 static int fr_natgetent(data)
782 nat_save_t ipn, *ipnp, *ipnn = NULL;
783 register nat_t *n, *nat;
787 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
790 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
798 if (nat_instances == NULL)
804 * Make sure the pointer we're copying from exists in the
805 * current list of entries. Security precaution to prevent
806 * copying of random kernel data.
808 for (n = nat_instances; n; n = n->nat_next)
815 ipn.ipn_next = nat->nat_next;
817 bcopy((char *)nat, (char *)&ipn.ipn_nat, sizeof(ipn.ipn_nat));
818 ipn.ipn_nat.nat_data = NULL;
821 bcopy((char *)nat->nat_ptr, (char *)&ipn.ipn_ipnat,
822 sizeof(ipn.ipn_ipnat));
826 bcopy((char *)nat->nat_fr, (char *)&ipn.ipn_rule,
827 sizeof(ipn.ipn_rule));
829 if ((aps = nat->nat_aps)) {
830 ipn.ipn_dsize = sizeof(*aps);
832 ipn.ipn_dsize += aps->aps_psiz;
833 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + ipn.ipn_dsize);
836 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
838 bcopy((char *)aps, ipnn->ipn_data, sizeof(*aps));
840 bcopy(aps->aps_data, ipnn->ipn_data + sizeof(*aps),
842 ipnn->ipn_dsize += aps->aps_psiz;
844 error = IWCOPY((caddr_t)ipnn, ipnp,
845 sizeof(ipn) + ipn.ipn_dsize);
848 KFREES(ipnn, sizeof(*ipnn) + ipn.ipn_dsize);
850 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
858 static int fr_natputent(data)
861 nat_save_t ipn, *ipnp, *ipnn = NULL;
862 register nat_t *n, *nat;
869 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
872 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
877 KMALLOCS(ipnn, nat_save_t *, sizeof(ipn) + ipn.ipn_dsize);
880 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
881 error = IRCOPY((caddr_t)ipnp, (caddr_t)ipn.ipn_data,
890 KMALLOC(nat, nat_t *);
896 bcopy((char *)&ipn.ipn_nat, (char *)nat, sizeof(*nat));
898 * Initialize all these so that nat_delete() doesn't cause a crash.
900 nat->nat_phnext[0] = NULL;
901 nat->nat_phnext[1] = NULL;
909 nat->nat_data = NULL;
912 * Restore the rule associated with this nat session
915 KMALLOC(in, ipnat_t *);
921 bcopy((char *)&ipn.ipn_ipnat, (char *)in, sizeof(*in));
923 in->in_flags |= IPN_DELETE;
926 in->in_prnext = NULL;
928 in->in_pmnext = NULL;
929 in->in_ifp = GETUNIT(in->in_ifname, 4);
930 if (in->in_plabel[0] != '\0') {
931 in->in_apr = appr_match(in->in_p, in->in_plabel);
936 * Restore ap_session_t structure. Include the private data allocated
940 KMALLOC(aps, ap_session_t *);
946 aps->aps_next = ap_sess_list;
948 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
950 aps->aps_apr = in->in_apr;
952 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
953 if (aps->aps_data == NULL) {
957 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
961 aps->aps_data = NULL;
966 * If there was a filtering rule associated with this entry then
967 * build up a new one.
970 if (nat->nat_flags & FI_NEWFR) {
971 KMALLOC(fr, frentry_t *);
977 bcopy((char *)&ipn.ipn_fr, (char *)fr, sizeof(*fr));
978 ipn.ipn_nat.nat_fr = fr;
979 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
985 for (n = nat_instances; n; n = n->nat_next)
996 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1001 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1009 * Delete a nat entry from the various lists and table.
1011 static void nat_delete(natd)
1016 if (natd->nat_flags & FI_WILDP)
1017 nat_stats.ns_wilds--;
1018 if (natd->nat_hnext[0])
1019 natd->nat_hnext[0]->nat_phnext[0] = natd->nat_phnext[0];
1020 *natd->nat_phnext[0] = natd->nat_hnext[0];
1021 if (natd->nat_hnext[1])
1022 natd->nat_hnext[1]->nat_phnext[1] = natd->nat_phnext[1];
1023 *natd->nat_phnext[1] = natd->nat_hnext[1];
1025 if (natd->nat_fr != NULL) {
1026 ATOMIC_DEC32(natd->nat_fr->fr_ref);
1029 if (natd->nat_hm != NULL)
1030 nat_hostmapdel(natd->nat_hm);
1033 * If there is an active reference from the nat entry to its parent
1034 * rule, decrement the rule's reference count and free it too if no
1035 * longer being used.
1037 ipn = natd->nat_ptr;
1041 if (!ipn->in_use && (ipn->in_flags & IPN_DELETE)) {
1043 appr_free(ipn->in_apr);
1045 nat_stats.ns_rules--;
1049 MUTEX_DESTROY(&natd->nat_lock);
1051 * If there's a fragment table entry too for this nat entry, then
1052 * dereference that as well.
1054 ipfr_forget((void *)natd);
1055 aps_free(natd->nat_aps);
1056 nat_stats.ns_inuse--;
1062 * nat_flushtable - clear the NAT table of all mapping entries.
1064 static int nat_flushtable()
1066 register nat_t *nat, **natp;
1070 * ALL NAT mappings deleted, so lets just make the deletions
1073 if (nat_table[0] != NULL)
1074 bzero((char *)nat_table[0],
1075 sizeof(nat_table[0]) * ipf_nattable_sz);
1076 if (nat_table[1] != NULL)
1077 bzero((char *)nat_table[1],
1078 sizeof(nat_table[1]) * ipf_nattable_sz);
1080 for (natp = &nat_instances; (nat = *natp); ) {
1081 *natp = nat->nat_next;
1083 nat_log(nat, NL_FLUSH);
1088 nat_stats.ns_inuse = 0;
1094 * nat_clearlist - delete all rules in the active NAT mapping list.
1096 static int nat_clearlist()
1098 register ipnat_t *n, **np = &nat_list;
1101 if (nat_rules != NULL)
1102 bzero((char *)nat_rules, sizeof(*nat_rules) * ipf_natrules_sz);
1103 if (rdr_rules != NULL)
1104 bzero((char *)rdr_rules, sizeof(*rdr_rules) * ipf_rdrrules_sz);
1110 appr_free(n->in_apr);
1112 nat_stats.ns_rules--;
1114 n->in_flags |= IPN_DELETE;
1126 * Create a new NAT table entry.
1127 * NOTE: assumes write lock on ipf_nat has been obtained already.
1129 nat_t *nat_new(np, ip, fin, flags, direction)
1136 register u_32_t sum1, sum2, sumd, l;
1137 u_short port = 0, sport = 0, dport = 0, nport = 0;
1138 struct in_addr in, inb;
1139 tcphdr_t *tcp = NULL;
1140 hostmap_t *hm = NULL;
1143 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1144 qif_t *qf = fin->fin_qif;
1147 nflags = flags & np->in_flags;
1148 if (flags & IPN_TCPUDP) {
1149 tcp = (tcphdr_t *)fin->fin_dp;
1150 sport = tcp->th_sport;
1151 dport = tcp->th_dport;
1154 /* Give me a new nat */
1155 KMALLOC(nat, nat_t *);
1157 nat_stats.ns_memfail++;
1161 bzero((char *)nat, sizeof(*nat));
1162 nat->nat_flags = flags;
1163 if (flags & FI_WILDP)
1164 nat_stats.ns_wilds++;
1166 * Search the current table for a match.
1168 if (direction == NAT_OUTBOUND) {
1170 * Values at which the search for a free resouce starts.
1176 * If it's an outbound packet which doesn't match any existing
1177 * record, then create a new port
1181 st_port = np->in_pnext;
1185 in.s_addr = htonl(np->in_nip);
1188 * Check to see if there is an existing NAT
1189 * setup for this IP address pair.
1191 hm = nat_hostmap(np, ip->ip_src, in);
1193 in.s_addr = hm->hm_mapip.s_addr;
1194 } else if ((l == 1) && (hm != NULL)) {
1198 in.s_addr = ntohl(in.s_addr);
1202 if ((np->in_outmsk == 0xffffffff) &&
1203 (np->in_pnext == 0)) {
1208 if (np->in_redir & NAT_MAPBLK) {
1209 if ((l >= np->in_ppip) || ((l > 0) &&
1210 !(flags & IPN_TCPUDP)))
1213 * map-block - Calculate destination address.
1215 in.s_addr = ntohl(ip->ip_src.s_addr);
1216 in.s_addr &= ntohl(~np->in_inmsk);
1217 inb.s_addr = in.s_addr;
1218 in.s_addr /= np->in_ippip;
1219 in.s_addr &= ntohl(~np->in_outmsk);
1220 in.s_addr += ntohl(np->in_outip);
1222 * Calculate destination port.
1224 if ((flags & IPN_TCPUDP) &&
1225 (np->in_ppip != 0)) {
1226 port = ntohs(sport) + l;
1227 port %= np->in_ppip;
1228 port += np->in_ppip *
1229 (inb.s_addr % np->in_ippip);
1230 port += MAPBLK_MINPORT;
1233 } else if (!np->in_outip &&
1234 (np->in_outmsk == 0xffffffff)) {
1236 * 0/32 - use the interface's IP address.
1239 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1241 in.s_addr = ntohl(in.s_addr);
1242 } else if (!np->in_outip && !np->in_outmsk) {
1244 * 0/0 - use the original source address/port.
1248 in.s_addr = ntohl(ip->ip_src.s_addr);
1249 } else if ((np->in_outmsk != 0xffffffff) &&
1250 (np->in_pnext == 0) &&
1251 ((l > 0) || (hm == NULL)))
1255 if ((nflags & IPN_TCPUDP) &&
1256 ((np->in_redir & NAT_MAPBLK) == 0) &&
1257 (np->in_flags & IPN_AUTOPORTMAP)) {
1258 if ((l > 0) && (l % np->in_ppip == 0)) {
1259 if (l > np->in_space) {
1261 } else if ((l > np->in_ppip) &&
1262 np->in_outmsk != 0xffffffff)
1265 if (np->in_ppip != 0) {
1266 port = ntohs(sport);
1267 port += (l % np->in_ppip);
1268 port %= np->in_ppip;
1269 port += np->in_ppip *
1270 (ntohl(ip->ip_src.s_addr) %
1272 port += MAPBLK_MINPORT;
1275 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1276 (nflags & IPN_TCPUDP) &&
1277 (np->in_pnext != 0)) {
1278 port = htons(np->in_pnext++);
1279 if (np->in_pnext > ntohs(np->in_pmax)) {
1280 np->in_pnext = ntohs(np->in_pmin);
1281 if (np->in_outmsk != 0xffffffff)
1286 if (np->in_flags & IPN_IPRANGE) {
1287 if (np->in_nip > ntohl(np->in_outmsk))
1288 np->in_nip = ntohl(np->in_outip);
1290 if ((np->in_outmsk != 0xffffffff) &&
1291 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1292 ntohl(np->in_outip))
1293 np->in_nip = ntohl(np->in_outip) + 1;
1296 if (!port && (flags & IPN_TCPUDP))
1300 * Here we do a lookup of the connection as seen from
1301 * the outside. If an IP# pair already exists, try
1302 * again. So if you have A->B becomes C->B, you can
1303 * also have D->E become C->E but not D->B causing
1304 * another C->B. Also take protocol and ports into
1305 * account when determining whether a pre-existing
1306 * NAT setup will cause an external conflict where
1307 * this is appropriate.
1309 inb.s_addr = htonl(in.s_addr);
1310 natl = nat_inlookup(fin->fin_ifp, flags & ~FI_WILDP,
1311 (u_int)ip->ip_p, ip->ip_dst, inb,
1312 (port << 16) | dport, 1);
1315 * Has the search wrapped around and come back to the
1318 if ((natl != NULL) &&
1319 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1320 (np->in_nip != 0) && (st_ip == np->in_nip))
1323 } while (natl != NULL);
1325 if (np->in_space > 0)
1328 /* Setup the NAT table */
1329 nat->nat_inip = ip->ip_src;
1330 nat->nat_outip.s_addr = htonl(in.s_addr);
1331 nat->nat_oip = ip->ip_dst;
1332 if (nat->nat_hm == NULL)
1333 nat->nat_hm = nat_hostmap(np, ip->ip_src,
1336 sum1 = LONG_SUM(ntohl(ip->ip_src.s_addr)) + ntohs(sport);
1337 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1339 if (flags & IPN_TCPUDP) {
1340 nat->nat_inport = sport;
1341 nat->nat_outport = port; /* sport */
1342 nat->nat_oport = dport;
1346 * Otherwise, it's an inbound packet. Most likely, we don't
1347 * want to rewrite source ports and source addresses. Instead,
1348 * we want to rewrite to a fixed internal address and fixed
1351 if (np->in_flags & IPN_SPLIT) {
1352 in.s_addr = np->in_nip;
1353 if (np->in_inip == htonl(in.s_addr))
1354 np->in_nip = ntohl(np->in_inmsk);
1356 np->in_nip = ntohl(np->in_inip);
1357 if (np->in_flags & IPN_ROUNDR) {
1363 in.s_addr = ntohl(np->in_inip);
1364 if (np->in_flags & IPN_ROUNDR) {
1373 * Whilst not optimized for the case where
1374 * pmin == pmax, the gain is not significant.
1376 nport = ntohs(dport) - ntohs(np->in_pmin) +
1377 ntohs(np->in_pnext);
1378 nport = htons(nport);
1382 * When the redirect-to address is set to 0.0.0.0, just
1383 * assume a blank `forwarding' of the packet. We don't
1384 * setup any translation for this either.
1386 if (in.s_addr == 0) {
1389 in.s_addr = ntohl(ip->ip_dst.s_addr);
1392 nat->nat_inip.s_addr = htonl(in.s_addr);
1393 nat->nat_outip = ip->ip_dst;
1394 nat->nat_oip = ip->ip_src;
1396 sum1 = LONG_SUM(ntohl(ip->ip_dst.s_addr)) + ntohs(dport);
1397 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1399 if (flags & IPN_TCPUDP) {
1400 nat->nat_inport = nport;
1401 nat->nat_outport = dport;
1402 nat->nat_oport = sport;
1406 CALC_SUMD(sum1, sum2, sumd);
1407 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1408 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1409 if ((flags & IPN_TCPUDP) && dohwcksum &&
1410 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1411 if (direction == NAT_OUTBOUND)
1412 sum1 = LONG_SUM(ntohl(in.s_addr));
1414 sum1 = LONG_SUM(ntohl(ip->ip_src.s_addr));
1415 sum1 += LONG_SUM(ntohl(ip->ip_dst.s_addr));
1416 sum1 += IPPROTO_TCP;
1417 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1418 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1421 nat->nat_sumd[1] = nat->nat_sumd[0];
1423 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1424 if (direction == NAT_OUTBOUND)
1425 sum1 = LONG_SUM(ntohl(ip->ip_src.s_addr));
1427 sum1 = LONG_SUM(ntohl(ip->ip_dst.s_addr));
1429 sum2 = LONG_SUM(in.s_addr);
1431 CALC_SUMD(sum1, sum2, sumd);
1432 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1434 nat->nat_ipsumd = nat->nat_sumd[0];
1436 in.s_addr = htonl(in.s_addr);
1439 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1443 nat->nat_dir = direction;
1444 nat->nat_ifp = fin->fin_ifp;
1446 nat->nat_p = ip->ip_p;
1449 nat->nat_fr = fin->fin_fr;
1450 if (nat->nat_fr != NULL) {
1451 ATOMIC_INC32(nat->nat_fr->fr_ref);
1453 if (direction == NAT_OUTBOUND) {
1454 if (flags & IPN_TCPUDP)
1455 tcp->th_sport = port;
1457 if (flags & IPN_TCPUDP)
1458 tcp->th_dport = nport;
1462 nat_log(nat, (u_int)np->in_redir);
1466 nat_stats.ns_badnat++;
1467 if ((hm = nat->nat_hm) != NULL)
1474 void nat_insert(nat)
1480 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1482 nat->nat_age = fr_defnatage;
1483 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1484 if (nat->nat_ifname[0] !='\0') {
1485 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1488 nat->nat_next = nat_instances;
1489 nat_instances = nat;
1491 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1492 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1494 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1496 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1498 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1501 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, 0, 0xffffffff);
1502 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1, ipf_nattable_sz);
1503 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, 0, 0xffffffff);
1504 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2, ipf_nattable_sz);
1507 natp = &nat_table[0][hv1];
1509 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1510 nat->nat_phnext[0] = natp;
1511 nat->nat_hnext[0] = *natp;
1514 natp = &nat_table[1][hv2];
1516 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1517 nat->nat_phnext[1] = natp;
1518 nat->nat_hnext[1] = *natp;
1521 nat_stats.ns_added++;
1522 nat_stats.ns_inuse++;
1526 nat_t *nat_icmplookup(ip, fin, dir)
1532 tcphdr_t *tcp = NULL;
1534 int flags = 0, type, minlen;
1536 icmp = (icmphdr_t *)fin->fin_dp;
1538 * Does it at least have the return (basic) IP header ?
1539 * Only a basic IP header (no options) should be with an ICMP error
1542 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1544 type = icmp->icmp_type;
1546 * If it's not an error type, then return.
1548 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1549 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1550 (type != ICMP_PARAMPROB))
1553 oip = (ip_t *)((char *)fin->fin_dp + 8);
1554 minlen = (oip->ip_hl << 2);
1555 if (minlen < sizeof(ip_t))
1557 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1560 * Is the buffer big enough for all of it ? It's the size of the IP
1561 * header claimed in the encapsulated part which is of concern. It
1562 * may be too big to be in this buffer but not so big that it's
1563 * outside the ICMP packet, leading to TCP deref's causing problems.
1564 * This is possible because we don't know how big oip_hl is when we
1565 * do the pullup early in fr_check() and thus can't gaurantee it is
1574 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1577 m = *(mb_t **)fin->fin_mp;
1578 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1579 (char *)ip + m->m_len)
1585 if (oip->ip_p == IPPROTO_TCP)
1587 else if (oip->ip_p == IPPROTO_UDP)
1589 if (flags & IPN_TCPUDP) {
1590 minlen += 8; /* + 64bits of data to get ports */
1591 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1593 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1594 if (dir == NAT_INBOUND)
1595 return nat_inlookup(fin->fin_ifp, flags,
1596 (u_int)oip->ip_p, oip->ip_dst, oip->ip_src,
1597 (tcp->th_sport << 16) | tcp->th_dport, 0);
1599 return nat_outlookup(fin->fin_ifp, flags,
1600 (u_int)oip->ip_p, oip->ip_dst, oip->ip_src,
1601 (tcp->th_sport << 16) | tcp->th_dport, 0);
1603 if (dir == NAT_INBOUND)
1604 return nat_inlookup(fin->fin_ifp, 0, (u_int)oip->ip_p,
1605 oip->ip_dst, oip->ip_src, 0, 0);
1607 return nat_outlookup(fin->fin_ifp, 0, (u_int)oip->ip_p,
1608 oip->ip_dst, oip->ip_src, 0, 0);
1613 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1614 * packet gets correctly recognised.
1616 nat_t *nat_icmp(ip, fin, nflags, dir)
1622 u_32_t sum1, sum2, sumd, sumd2 = 0;
1630 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1633 * nat_icmplookup() will return NULL for `defective' packets.
1635 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1637 *nflags = IPN_ICMPERR;
1638 icmp = (icmphdr_t *)fin->fin_dp;
1639 oip = (ip_t *)&icmp->icmp_ip;
1640 if (oip->ip_p == IPPROTO_TCP)
1642 else if (oip->ip_p == IPPROTO_UDP)
1644 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1646 * Need to adjust ICMP header to include the real IP#'s and
1647 * port #'s. Only apply a checksum change relative to the
1648 * IP address change as it will be modified again in ip_natout
1649 * for both address and port. Two checksum changes are
1650 * necessary for the two header address changes. Be careful
1651 * to only modify the checksum once for the port # and twice
1657 * Fix the IP addresses in the offending IP packet. You also need
1658 * to adjust the IP header checksum of that offending IP packet
1659 * and the ICMP checksum of the ICMP error message itself.
1661 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1662 * in the pseudo header that is used to compute the UDP resp. TCP
1663 * checksum. So, we must compensate that as well. Even worse, the
1664 * change in the UDP and TCP checksums require yet another
1665 * adjustment of the ICMP checksum of the ICMP error message.
1667 * For the moment we forget about TCP, because that checksum is not
1668 * in the first 8 bytes, so it will not be available in most cases.
1671 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1672 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1676 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1677 in = nat->nat_outip;
1681 sum2 = LONG_SUM(ntohl(in.s_addr));
1683 CALC_SUMD(sum1, sum2, sumd);
1685 if (nat->nat_dir == NAT_OUTBOUND) {
1687 * Fix IP checksum of the offending IP packet to adjust for
1688 * the change in the IP address.
1690 * Normally, you would expect that the ICMP checksum of the
1691 * ICMP error message needs to be adjusted as well for the
1692 * IP address change in oip.
1693 * However, this is a NOP, because the ICMP checksum is
1694 * calculated over the complete ICMP packet, which includes the
1695 * changed oip IP addresses and oip->ip_sum. However, these
1696 * two changes cancel each other out (if the delta for
1697 * the IP address is x, then the delta for ip_sum is minus x),
1698 * so no change in the icmp_cksum is necessary.
1700 * Be careful that nat_dir refers to the direction of the
1701 * offending IP packet (oip), not to its ICMP response (icmp)
1703 fix_datacksum(&oip->ip_sum, sumd);
1706 * Fix UDP pseudo header checksum to compensate for the
1707 * IP address change.
1709 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1711 * The UDP checksum is optional, only adjust it
1712 * if it has been set.
1714 sum1 = ntohs(udp->uh_sum);
1715 fix_datacksum(&udp->uh_sum, sumd);
1716 sum2 = ntohs(udp->uh_sum);
1719 * Fix ICMP checksum to compensate the UDP
1720 * checksum adjustment.
1722 CALC_SUMD(sum1, sum2, sumd);
1728 * Fix TCP pseudo header checksum to compensate for the
1729 * IP address change. Before we can do the change, we
1730 * must make sure that oip is sufficient large to hold
1731 * the TCP checksum (normally it does not!).
1733 if (oip->ip_p == IPPROTO_TCP) {
1740 * Fix IP checksum of the offending IP packet to adjust for
1741 * the change in the IP address.
1743 * Normally, you would expect that the ICMP checksum of the
1744 * ICMP error message needs to be adjusted as well for the
1745 * IP address change in oip.
1746 * However, this is a NOP, because the ICMP checksum is
1747 * calculated over the complete ICMP packet, which includes the
1748 * changed oip IP addresses and oip->ip_sum. However, these
1749 * two changes cancel each other out (if the delta for
1750 * the IP address is x, then the delta for ip_sum is minus x),
1751 * so no change in the icmp_cksum is necessary.
1753 * Be careful that nat_dir refers to the direction of the
1754 * offending IP packet (oip), not to its ICMP response (icmp)
1756 fix_datacksum(&oip->ip_sum, sumd);
1758 /* XXX FV : without having looked at Solaris source code, it seems unlikely
1759 * that SOLARIS would compensate this in the kernel (a body of an IP packet
1760 * in the data section of an ICMP packet). I have the feeling that this should
1761 * be unconditional, but I'm not in a position to check.
1763 #if !SOLARIS && !defined(__sgi)
1765 * Fix UDP pseudo header checksum to compensate for the
1766 * IP address change.
1768 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1770 * The UDP checksum is optional, only adjust it
1771 * if it has been set
1773 sum1 = ntohs(udp->uh_sum);
1774 fix_datacksum(&udp->uh_sum, sumd);
1775 sum2 = ntohs(udp->uh_sum);
1778 * Fix ICMP checksum to compensate the UDP
1779 * checksum adjustment.
1781 CALC_SUMD(sum1, sum2, sumd);
1787 * Fix TCP pseudo header checksum to compensate for the
1788 * IP address change. Before we can do the change, we
1789 * must make sure that oip is sufficient large to hold
1790 * the TCP checksum (normally it does not!).
1792 if (oip->ip_p == IPPROTO_TCP) {
1800 if ((flags & IPN_TCPUDP) != 0) {
1804 * XXX - what if this is bogus hl and we go off the end ?
1805 * In this case, nat_icmpinlookup() will have returned NULL.
1807 tcp = (tcphdr_t *)udp;
1811 * For offending TCP/UDP IP packets, translate the ports as
1812 * well, based on the NAT specification. Of course such
1813 * a change must be reflected in the ICMP checksum as well.
1815 * Advance notice : Now it becomes complicated :-)
1817 * Since the port fields are part of the TCP/UDP checksum
1818 * of the offending IP packet, you need to adjust that checksum
1819 * as well... but, if you change, you must change the icmp
1820 * checksum *again*, to reflect that change.
1822 * To further complicate: the TCP checksum is not in the first
1823 * 8 bytes of the offending ip packet, so it most likely is not
1824 * available (we might have to fix that if the encounter a
1825 * device that returns more than 8 data bytes on icmp error)
1828 if (nat->nat_oport == tcp->th_dport) {
1829 if (tcp->th_sport != nat->nat_inport) {
1831 * Fix ICMP checksum to compensate port
1834 sum1 = ntohs(tcp->th_sport);
1835 sum2 = ntohs(nat->nat_inport);
1836 CALC_SUMD(sum1, sum2, sumd);
1838 tcp->th_sport = nat->nat_inport;
1841 * Fix udp checksum to compensate port
1842 * adjustment. NOTE : the offending IP packet
1843 * flows the other direction compared to the
1846 * The UDP checksum is optional, only adjust
1847 * it if it has been set.
1849 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1851 sum1 = ntohs(udp->uh_sum);
1852 fix_datacksum(&udp->uh_sum, sumd);
1853 sum2 = ntohs(udp->uh_sum);
1856 * Fix ICMP checksum to
1857 * compensate UDP checksum
1860 CALC_SUMD(sum1, sum2, sumd);
1865 if (tcp->th_dport != nat->nat_outport) {
1867 * Fix ICMP checksum to compensate port
1870 sum1 = ntohs(tcp->th_dport);
1871 sum2 = ntohs(nat->nat_outport);
1872 CALC_SUMD(sum1, sum2, sumd);
1874 tcp->th_dport = nat->nat_outport;
1877 * Fix udp checksum to compensate port
1878 * adjustment. NOTE : the offending IP
1879 * packet flows the other direction compared
1880 * to the ICMP message.
1882 * The UDP checksum is optional, only adjust
1883 * it if it has been set.
1885 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1887 sum1 = ntohs(udp->uh_sum);
1888 fix_datacksum(&udp->uh_sum, sumd);
1889 sum2 = ntohs(udp->uh_sum);
1892 * Fix ICMP checksum to compensate
1893 * UDP checksum adjustment.
1895 CALC_SUMD(sum1, sum2, sumd);
1901 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1902 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1903 if (nat->nat_dir == NAT_OUTBOUND) {
1904 fix_outcksum(fin, &icmp->icmp_cksum, sumd2);
1906 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
1910 if (oip->ip_p == IPPROTO_ICMP)
1911 nat->nat_age = fr_defnaticmpage;
1917 * NB: these lookups don't lock access to the list, it assume it has already
1921 * Lookup a nat entry based on the mapped destination ip address/port and
1922 * real source address/port. We use this lookup when receiving a packet,
1923 * we're looking for a table entry, based on the destination address.
1924 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
1926 nat_t *nat_inlookup(ifp, flags, p, src, mapdst, ports, rw)
1928 register u_int flags, p;
1929 struct in_addr src , mapdst;
1933 register u_short sport, dport;
1934 register nat_t *nat;
1935 register int nflags;
1936 register u_32_t dst;
1939 dst = mapdst.s_addr;
1940 dport = ports >> 16;
1941 sport = ports & 0xffff;
1942 flags &= IPN_TCPUDP;
1944 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
1945 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
1946 nat = nat_table[1][hv];
1947 for (; nat; nat = nat->nat_hnext[1]) {
1948 nflags = nat->nat_flags;
1949 if ((!ifp || ifp == nat->nat_ifp) &&
1950 nat->nat_oip.s_addr == src.s_addr &&
1951 nat->nat_outip.s_addr == dst &&
1952 (((p == 0) && (flags == (nat->nat_flags & IPN_TCPUDP)))
1953 || (p == nat->nat_p)) && (!flags ||
1954 (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
1955 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT)))))
1958 if (!nat_stats.ns_wilds || !(flags & IPN_TCPUDP))
1961 RWLOCK_EXIT(&ipf_nat);
1963 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
1964 hv = NAT_HASH_FN(src.s_addr, hv, ipf_nattable_sz);
1966 WRITE_ENTER(&ipf_nat);
1968 nat = nat_table[1][hv];
1969 for (; nat; nat = nat->nat_hnext[1]) {
1970 nflags = nat->nat_flags;
1971 if (ifp && ifp != nat->nat_ifp)
1973 if (!(nflags & IPN_TCPUDP))
1975 if (!(nflags & FI_WILDP))
1977 if (nat->nat_oip.s_addr != src.s_addr ||
1978 nat->nat_outip.s_addr != dst)
1980 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
1981 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
1982 nat_tabmove(nat, ports);
1987 MUTEX_DOWNGRADE(&ipf_nat);
1994 * This function is only called for TCP/UDP NAT table entries where the
1995 * original was placed in the table without hashing on the ports and we now
1996 * want to include hashing on port numbers.
1998 static void nat_tabmove(nat, ports)
2002 register u_short sport, dport;
2006 dport = ports >> 16;
2007 sport = ports & 0xffff;
2009 if (nat->nat_oport == dport) {
2010 nat->nat_inport = sport;
2011 nat->nat_outport = sport;
2015 * Remove the NAT entry from the old location
2017 if (nat->nat_hnext[0])
2018 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2019 *nat->nat_phnext[0] = nat->nat_hnext[0];
2021 if (nat->nat_hnext[1])
2022 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2023 *nat->nat_phnext[1] = nat->nat_hnext[1];
2026 * Add into the NAT table in the new position
2028 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2029 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2030 natp = &nat_table[0][hv];
2032 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2033 nat->nat_phnext[0] = natp;
2034 nat->nat_hnext[0] = *natp;
2037 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2038 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2039 natp = &nat_table[1][hv];
2041 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2042 nat->nat_phnext[1] = natp;
2043 nat->nat_hnext[1] = *natp;
2049 * Lookup a nat entry based on the source 'real' ip address/port and
2050 * destination address/port. We use this lookup when sending a packet out,
2051 * we're looking for a table entry, based on the source address.
2052 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2054 nat_t *nat_outlookup(ifp, flags, p, src, dst, ports, rw)
2056 register u_int flags, p;
2057 struct in_addr src , dst;
2061 register u_short sport, dport;
2062 register nat_t *nat;
2063 register int nflags;
2067 sport = ports & 0xffff;
2068 dport = ports >> 16;
2069 flags &= IPN_TCPUDP;
2072 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2073 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2074 nat = nat_table[0][hv];
2075 for (; nat; nat = nat->nat_hnext[0]) {
2076 nflags = nat->nat_flags;
2078 if ((!ifp || ifp == nat->nat_ifp) &&
2079 nat->nat_inip.s_addr == srcip &&
2080 nat->nat_oip.s_addr == dst.s_addr &&
2081 (((p == 0) && (flags == (nflags & IPN_TCPUDP)))
2082 || (p == nat->nat_p)) && (!flags ||
2083 ((nat->nat_inport == sport || nflags & FI_W_SPORT) &&
2084 (nat->nat_oport == dport || nflags & FI_W_DPORT))))
2087 if (!nat_stats.ns_wilds || !(flags & IPN_TCPUDP))
2090 RWLOCK_EXIT(&ipf_nat);
2092 hv = NAT_HASH_FN(srcip, 0, ipf_nattable_sz);
2093 hv = NAT_HASH_FN(dst.s_addr, hv, ipf_nattable_sz);
2095 WRITE_ENTER(&ipf_nat);
2097 nat = nat_table[0][hv];
2098 for (; nat; nat = nat->nat_hnext[0]) {
2099 nflags = nat->nat_flags;
2100 if (ifp && ifp != nat->nat_ifp)
2102 if (!(nflags & IPN_TCPUDP))
2104 if (!(nflags & FI_WILDP))
2106 if ((nat->nat_inip.s_addr != srcip) ||
2107 (nat->nat_oip.s_addr != dst.s_addr))
2109 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2110 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2111 nat_tabmove(nat, ports);
2116 MUTEX_DOWNGRADE(&ipf_nat);
2123 * Lookup the NAT tables to search for a matching redirect
2125 nat_t *nat_lookupredir(np)
2126 register natlookup_t *np;
2131 ports = (np->nl_outport << 16) | np->nl_inport;
2133 * If nl_inip is non null, this is a lookup based on the real
2134 * ip address. Else, we use the fake.
2136 if ((nat = nat_outlookup(NULL, np->nl_flags, 0, np->nl_inip,
2137 np->nl_outip, ports, 0))) {
2138 np->nl_realip = nat->nat_outip;
2139 np->nl_realport = nat->nat_outport;
2145 static int nat_match(fin, np, ip)
2155 if (np->in_p && ip->ip_p != np->in_p)
2158 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2160 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2161 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2163 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2164 ^ ((np->in_flags & IPN_NOTDST) != 0))
2167 if (!(np->in_redir & NAT_REDIRECT))
2169 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2170 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2172 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2173 ^ ((np->in_flags & IPN_NOTDST) != 0))
2178 if (!(fin->fin_fl & FI_TCPUDP) ||
2179 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2180 if (ft->ftu_scmp || ft->ftu_dcmp)
2185 return fr_tcpudpchk(ft, fin);
2190 * Packets going out on the external interface go through this.
2191 * Here, the source address requires alteration, if anything.
2193 int ip_natout(ip, fin)
2197 register ipnat_t *np = NULL;
2198 register u_32_t ipa;
2199 tcphdr_t *tcp = NULL;
2200 u_short sport = 0, dport = 0, *csump = NULL;
2201 int natadd = 1, i, icmpset = 1;
2202 u_int nflags = 0, hv, msk;
2208 if (nat_list == NULL || (fr_nat_lock))
2211 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2212 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1)
2213 ifp = fr->fr_tif.fd_ifp;
2217 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2218 if (ip->ip_p == IPPROTO_TCP)
2220 else if (ip->ip_p == IPPROTO_UDP)
2222 if ((nflags & IPN_TCPUDP)) {
2223 tcp = (tcphdr_t *)fin->fin_dp;
2224 sport = tcp->th_sport;
2225 dport = tcp->th_dport;
2229 ipa = ip->ip_src.s_addr;
2231 READ_ENTER(&ipf_nat);
2233 if ((ip->ip_p == IPPROTO_ICMP) &&
2234 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2236 else if ((fin->fin_fl & FI_FRAG) &&
2237 (nat = ipfr_nat_knownfrag(ip, fin)))
2239 else if ((nat = nat_outlookup(ifp, nflags, (u_int)ip->ip_p,
2240 ip->ip_src, ip->ip_dst,
2241 (dport << 16) | sport, 0))) {
2242 nflags = nat->nat_flags;
2243 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2244 if ((nflags & FI_W_SPORT) &&
2245 (nat->nat_inport != sport))
2246 nat->nat_inport = sport;
2247 else if ((nflags & FI_W_DPORT) &&
2248 (nat->nat_oport != dport))
2249 nat->nat_oport = dport;
2250 if (nat->nat_outport == 0)
2251 nat->nat_outport = sport;
2252 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2253 nflags = nat->nat_flags;
2254 nat_stats.ns_wilds--;
2257 RWLOCK_EXIT(&ipf_nat);
2258 WRITE_ENTER(&ipf_nat);
2260 * If there is no current entry in the nat table for this IP#,
2261 * create one for it (if there is a matching rule).
2266 iph = ipa & htonl(msk);
2267 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2268 for (np = nat_rules[hv]; np; np = np->in_mnext)
2270 if (np->in_ifp && (np->in_ifp != ifp))
2272 if ((np->in_flags & IPN_RF) &&
2273 !(np->in_flags & nflags))
2275 if (np->in_flags & IPN_FILTER) {
2276 if (!nat_match(fin, np, ip))
2278 } else if ((ipa & np->in_inmsk) != np->in_inip)
2280 if (np->in_redir & (NAT_MAP|NAT_MAPBLK)) {
2281 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2284 * If it's a redirection, then we don't want to
2285 * create new outgoing port stuff.
2286 * Redirections are only for incoming
2289 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2291 if ((nat = nat_new(np, ip, fin, (u_int)nflags,
2298 if ((np == NULL) && (i > 0)) {
2302 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2306 MUTEX_DOWNGRADE(&ipf_nat);
2310 * NOTE: ipf_nat must now only be held as a read lock
2314 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2315 ipfr_nat_newfrag(ip, fin, 0, nat);
2316 MUTEX_ENTER(&nat->nat_lock);
2317 nat->nat_age = fr_defnatage;
2318 nat->nat_bytes += ip->ip_len;
2320 MUTEX_EXIT(&nat->nat_lock);
2323 * Fix up checksums, not by recalculating them, but
2324 * simply computing adjustments.
2326 if (nflags == IPN_ICMPERR) {
2327 u_32_t s1, s2, sumd;
2329 s1 = LONG_SUM(ntohl(ip->ip_src.s_addr));
2330 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2331 CALC_SUMD(s1, s2, sumd);
2333 if (nat->nat_dir == NAT_OUTBOUND)
2334 fix_incksum(fin, &ip->ip_sum, sumd);
2336 fix_outcksum(fin, &ip->ip_sum, sumd);
2338 #if SOLARIS || defined(__sgi)
2340 if (nat->nat_dir == NAT_OUTBOUND)
2341 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2343 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2346 ip->ip_src = nat->nat_outip;
2348 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2350 if ((nat->nat_outport != 0) && (nflags & IPN_TCPUDP)) {
2351 tcp->th_sport = nat->nat_outport;
2352 fin->fin_data[0] = ntohs(tcp->th_sport);
2355 if (ip->ip_p == IPPROTO_TCP) {
2356 csump = &tcp->th_sum;
2357 MUTEX_ENTER(&nat->nat_lock);
2358 fr_tcp_age(&nat->nat_age,
2359 nat->nat_tcpstate, fin, 1);
2360 if (nat->nat_age < fr_defnaticmpage)
2361 nat->nat_age = fr_defnaticmpage;
2363 else if (nat->nat_age > fr_defnatage)
2364 nat->nat_age = fr_defnatage;
2367 * Increase this because we may have
2368 * "keep state" following this too and
2369 * packet storms can occur if this is
2370 * removed too quickly.
2372 if (nat->nat_age == fr_tcpclosed)
2373 nat->nat_age = fr_tcplastack;
2374 MUTEX_EXIT(&nat->nat_lock);
2375 } else if (ip->ip_p == IPPROTO_UDP) {
2376 udphdr_t *udp = (udphdr_t *)tcp;
2379 csump = &udp->uh_sum;
2380 } else if (ip->ip_p == IPPROTO_ICMP) {
2382 nat->nat_age = fr_defnaticmpage;
2386 if (nat->nat_dir == NAT_OUTBOUND)
2387 fix_outcksum(fin, csump, nat->nat_sumd[1]);
2389 fix_incksum(fin, csump, nat->nat_sumd[1]);
2393 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
2394 (tcp != NULL && dport == np->in_dport))) {
2395 i = appr_check(ip, fin, nat);
2400 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2401 RWLOCK_EXIT(&ipf_nat); /* READ */
2404 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2410 * Packets coming in from the external interface go through this.
2411 * Here, the destination address requires alteration, if anything.
2413 int ip_natin(ip, fin)
2417 register struct in_addr src;
2418 register struct in_addr in;
2419 register ipnat_t *np;
2420 u_short sport = 0, dport = 0, *csump = NULL;
2421 u_int nflags = 0, natadd = 1, hv, msk;
2422 struct ifnet *ifp = fin->fin_ifp;
2423 tcphdr_t *tcp = NULL;
2428 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2431 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2432 if (ip->ip_p == IPPROTO_TCP)
2434 else if (ip->ip_p == IPPROTO_UDP)
2436 if ((nflags & IPN_TCPUDP)) {
2437 tcp = (tcphdr_t *)fin->fin_dp;
2438 dport = tcp->th_dport;
2439 sport = tcp->th_sport;
2444 /* make sure the source address is to be redirected */
2447 READ_ENTER(&ipf_nat);
2449 if ((ip->ip_p == IPPROTO_ICMP) &&
2450 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2452 else if ((fin->fin_fl & FI_FRAG) &&
2453 (nat = ipfr_nat_knownfrag(ip, fin)))
2455 else if ((nat = nat_inlookup(fin->fin_ifp, nflags, (u_int)ip->ip_p,
2456 ip->ip_src, in, (dport << 16) | sport,
2458 nflags = nat->nat_flags;
2459 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2460 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2461 nat->nat_oport = sport;
2462 else if ((nat->nat_outport != dport) &&
2463 (nflags & FI_W_SPORT))
2464 nat->nat_outport = dport;
2465 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2466 nflags = nat->nat_flags;
2467 nat_stats.ns_wilds--;
2470 RWLOCK_EXIT(&ipf_nat);
2471 WRITE_ENTER(&ipf_nat);
2473 * If there is no current entry in the nat table for this IP#,
2474 * create one for it (if there is a matching rule).
2479 iph = in.s_addr & htonl(msk);
2480 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2481 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2482 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2483 (np->in_p && (np->in_p != ip->ip_p)) ||
2484 (np->in_flags && !(nflags & np->in_flags)))
2486 if (np->in_flags & IPN_FILTER) {
2487 if (!nat_match(fin, np, ip))
2489 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2491 if ((np->in_redir & NAT_REDIRECT) &&
2492 (!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2493 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2494 (ntohs(dport) >= ntohs(np->in_pmin)))))
2495 if ((nat = nat_new(np, ip, fin, nflags,
2502 if ((np == NULL) && (i > 0)) {
2506 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2510 MUTEX_DOWNGRADE(&ipf_nat);
2514 * NOTE: ipf_nat must now only be held as a read lock
2518 fin->fin_fr = nat->nat_fr;
2519 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2520 ipfr_nat_newfrag(ip, fin, 0, nat);
2521 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
2522 (tcp != NULL && sport == np->in_dport))) {
2523 i = appr_check(ip, fin, nat);
2525 RWLOCK_EXIT(&ipf_nat);
2530 MUTEX_ENTER(&nat->nat_lock);
2531 if (nflags != IPN_ICMPERR)
2532 nat->nat_age = fr_defnatage;
2534 nat->nat_bytes += ip->ip_len;
2536 MUTEX_EXIT(&nat->nat_lock);
2537 ip->ip_dst = nat->nat_inip;
2538 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2541 * Fix up checksums, not by recalculating them, but
2542 * simply computing adjustments.
2544 #if SOLARIS || defined(__sgi)
2545 if (nat->nat_dir == NAT_OUTBOUND)
2546 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2548 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2550 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2552 if ((nat->nat_inport != 0) && (nflags & IPN_TCPUDP)) {
2553 tcp->th_dport = nat->nat_inport;
2554 fin->fin_data[1] = ntohs(tcp->th_dport);
2557 if (ip->ip_p == IPPROTO_TCP) {
2558 csump = &tcp->th_sum;
2559 MUTEX_ENTER(&nat->nat_lock);
2560 fr_tcp_age(&nat->nat_age,
2561 nat->nat_tcpstate, fin, 0);
2562 if (nat->nat_age < fr_defnaticmpage)
2563 nat->nat_age = fr_defnaticmpage;
2565 else if (nat->nat_age > fr_defnatage)
2566 nat->nat_age = fr_defnatage;
2569 * Increase this because we may have
2570 * "keep state" following this too and
2571 * packet storms can occur if this is
2572 * removed too quickly.
2574 if (nat->nat_age == fr_tcpclosed)
2575 nat->nat_age = fr_tcplastack;
2576 MUTEX_EXIT(&nat->nat_lock);
2577 } else if (ip->ip_p == IPPROTO_UDP) {
2578 udphdr_t *udp = (udphdr_t *)tcp;
2581 csump = &udp->uh_sum;
2582 } else if (ip->ip_p == IPPROTO_ICMP) {
2584 nat->nat_age = fr_defnaticmpage;
2588 if (nat->nat_dir == NAT_OUTBOUND)
2589 fix_incksum(fin, csump, nat->nat_sumd[0]);
2591 fix_outcksum(fin, csump, nat->nat_sumd[0]);
2594 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2595 RWLOCK_EXIT(&ipf_nat); /* READ */
2598 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2604 * Free all memory used by NAT structures allocated at runtime.
2608 WRITE_ENTER(&ipf_nat);
2609 (void) nat_clearlist();
2610 (void) nat_flushtable();
2611 RWLOCK_EXIT(&ipf_nat);
2613 if (nat_table[0] != NULL) {
2614 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2615 nat_table[0] = NULL;
2617 if (nat_table[1] != NULL) {
2618 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2619 nat_table[1] = NULL;
2621 if (nat_rules != NULL) {
2622 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2625 if (rdr_rules != NULL) {
2626 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2629 if (maptable != NULL) {
2630 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2637 * Slowly expire held state for NAT entries. Timeouts are set in
2638 * expectation of this being called twice per second.
2642 register struct nat *nat, **natp;
2643 #if defined(_KERNEL) && !SOLARIS
2648 WRITE_ENTER(&ipf_nat);
2649 for (natp = &nat_instances; (nat = *natp); ) {
2652 natp = &nat->nat_next;
2655 *natp = nat->nat_next;
2657 nat_log(nat, NL_EXPIRE);
2660 nat_stats.ns_expire++;
2662 RWLOCK_EXIT(&ipf_nat);
2669 void ip_natsync(ifp)
2672 register ipnat_t *n;
2673 register nat_t *nat;
2674 register u_32_t sum1, sum2, sumd;
2678 #if defined(_KERNEL) && !SOLARIS
2683 * Change IP addresses for NAT sessions for any protocol except TCP
2684 * since it will break the TCP connection anyway.
2687 WRITE_ENTER(&ipf_nat);
2688 for (nat = nat_instances; nat; nat = nat->nat_next)
2689 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2690 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2691 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2692 ifp2 = nat->nat_ifp;
2694 * Change the map-to address to be the same as the
2697 sum1 = nat->nat_outip.s_addr;
2698 if (fr_ifpaddr(4, ifp2, &in) != -1)
2699 nat->nat_outip = in;
2700 sum2 = nat->nat_outip.s_addr;
2705 * Readjust the checksum adjustment to take into
2706 * account the new IP#.
2708 CALC_SUMD(sum1, sum2, sumd);
2709 /* XXX - dont change for TCP when solaris does
2710 * hardware checksumming.
2712 sumd += nat->nat_sumd[0];
2713 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2714 nat->nat_sumd[1] = nat->nat_sumd[0];
2717 for (n = nat_list; (n != NULL); n = n->in_next)
2718 if (n->in_ifp == ifp) {
2719 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2721 n->in_ifp = (void *)-1;
2723 RWLOCK_EXIT(&ipf_nat);
2729 void nat_log(nat, type)
2737 int rulen, types[1];
2739 natl.nl_inip = nat->nat_inip;
2740 natl.nl_outip = nat->nat_outip;
2741 natl.nl_origip = nat->nat_oip;
2742 natl.nl_bytes = nat->nat_bytes;
2743 natl.nl_pkts = nat->nat_pkts;
2744 natl.nl_origport = nat->nat_oport;
2745 natl.nl_inport = nat->nat_inport;
2746 natl.nl_outport = nat->nat_outport;
2747 natl.nl_p = nat->nat_p;
2748 natl.nl_type = type;
2751 if (nat->nat_ptr != NULL) {
2752 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2753 if (np == nat->nat_ptr) {
2754 natl.nl_rule = rulen;
2760 sizes[0] = sizeof(natl);
2763 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);