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>
18 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
20 # include "opt_ipfilter_log.h"
22 #if !defined(_KERNEL) && !defined(KERNEL)
27 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
28 # include <sys/filio.h>
29 # include <sys/fcntl.h>
31 # include <sys/ioctl.h>
33 #include <sys/fcntl.h>
36 # include <sys/protosw.h>
38 #include <sys/socket.h>
39 #if defined(_KERNEL) && !defined(linux)
40 # include <sys/systm.h>
42 #if !defined(__SVR4) && !defined(__svr4__)
44 # include <sys/mbuf.h>
47 # include <sys/filio.h>
48 # include <sys/byteorder.h>
50 # include <sys/dditypes.h>
52 # include <sys/stream.h>
53 # include <sys/kmem.h>
55 #if __FreeBSD_version >= 300000
56 # include <sys/queue.h>
59 #if __FreeBSD_version >= 300000
60 # include <net/if_var.h>
61 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
62 # include "opt_ipfilter.h"
68 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
74 # ifdef IFF_DRVRLOCK /* IRIX6 */
75 #include <sys/hashing.h>
76 #include <netinet/in_var.h>
82 # include <vpn/ipsec.h>
83 extern struct ifnet vpnif;
87 # include <netinet/ip_var.h>
88 # include <netinet/tcp_fsm.h>
90 #include <netinet/tcp.h>
91 #include <netinet/udp.h>
92 #include <netinet/ip_icmp.h>
93 #include "netinet/ip_compat.h"
94 #include <netinet/tcpip.h>
95 #include "netinet/ip_fil.h"
96 #include "netinet/ip_proxy.h"
97 #include "netinet/ip_nat.h"
98 #include "netinet/ip_frag.h"
99 #include "netinet/ip_state.h"
100 #if (__FreeBSD_version >= 300000)
101 # include <sys/malloc.h>
104 # define MIN(a,b) (((a)<(b))?(a):(b))
107 #define SOCKADDR_IN struct sockaddr_in
110 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
111 /* static const char rcsid[] = "@(#)$Id: ip_nat.c,v 2.37.2.44 2001/07/21 07:17:22 darrenr Exp $"; */
112 static const char rcsid[] = "@(#)$FreeBSD$";
115 nat_t **nat_table[2] = { NULL, NULL },
116 *nat_instances = NULL;
117 ipnat_t *nat_list = NULL;
118 u_int ipf_nattable_sz = NAT_TABLE_SZ;
119 u_int ipf_natrules_sz = NAT_SIZE;
120 u_int ipf_rdrrules_sz = RDR_SIZE;
121 u_int ipf_hostmap_sz = HOSTMAP_SIZE;
122 u_32_t nat_masks = 0;
123 u_32_t rdr_masks = 0;
124 ipnat_t **nat_rules = NULL;
125 ipnat_t **rdr_rules = NULL;
126 hostmap_t **maptable = NULL;
128 u_long fr_defnatage = DEF_NAT_AGE,
129 fr_defnaticmpage = 6; /* 3 seconds */
132 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
133 extern kmutex_t ipf_rw;
134 extern KRWLOCK_T ipf_nat;
137 static int nat_flushtable __P((void));
138 static int nat_clearlist __P((void));
139 static void nat_addnat __P((struct ipnat *));
140 static void nat_addrdr __P((struct ipnat *));
141 static void nat_delete __P((struct nat *));
142 static void nat_delrdr __P((struct ipnat *));
143 static void nat_delnat __P((struct ipnat *));
144 static int fr_natgetent __P((caddr_t));
145 static int fr_natgetsz __P((caddr_t));
146 static int fr_natputent __P((caddr_t));
147 static void nat_tabmove __P((nat_t *, u_32_t));
148 static int nat_match __P((fr_info_t *, ipnat_t *, ip_t *));
149 static hostmap_t *nat_hostmap __P((ipnat_t *, struct in_addr,
151 static void nat_hostmapdel __P((struct hostmap *));
156 KMALLOCS(nat_table[0], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
157 if (nat_table[0] != NULL)
158 bzero((char *)nat_table[0], ipf_nattable_sz * sizeof(nat_t *));
162 KMALLOCS(nat_table[1], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
163 if (nat_table[1] != NULL)
164 bzero((char *)nat_table[1], ipf_nattable_sz * sizeof(nat_t *));
168 KMALLOCS(nat_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_natrules_sz);
169 if (nat_rules != NULL)
170 bzero((char *)nat_rules, ipf_natrules_sz * sizeof(ipnat_t *));
174 KMALLOCS(rdr_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_rdrrules_sz);
175 if (rdr_rules != NULL)
176 bzero((char *)rdr_rules, ipf_rdrrules_sz * sizeof(ipnat_t *));
180 KMALLOCS(maptable, hostmap_t **, sizeof(hostmap_t *) * ipf_hostmap_sz);
181 if (maptable != NULL)
182 bzero((char *)maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
189 static void nat_addrdr(n)
197 k = countbits(n->in_outmsk);
198 if ((k >= 0) && (k != 32))
200 j = (n->in_outip & n->in_outmsk);
201 hv = NAT_HASH_FN(j, 0, ipf_rdrrules_sz);
204 np = &(*np)->in_rnext;
211 static void nat_addnat(n)
219 k = countbits(n->in_inmsk);
220 if ((k >= 0) && (k != 32))
222 j = (n->in_inip & n->in_inmsk);
223 hv = NAT_HASH_FN(j, 0, ipf_natrules_sz);
226 np = &(*np)->in_mnext;
233 static void nat_delrdr(n)
237 n->in_rnext->in_prnext = n->in_prnext;
238 *n->in_prnext = n->in_rnext;
242 static void nat_delnat(n)
246 n->in_mnext->in_pmnext = n->in_pmnext;
247 *n->in_pmnext = n->in_mnext;
252 * check if an ip address has already been allocated for a given mapping that
253 * is not doing port based translation.
255 * Must be called with ipf_nat held as a write lock.
257 static struct hostmap *nat_hostmap(np, real, map)
265 hv = real.s_addr % HOSTMAP_SIZE;
266 for (hm = maptable[hv]; hm; hm = hm->hm_next)
267 if ((hm->hm_realip.s_addr == real.s_addr) &&
268 (np == hm->hm_ipnat)) {
273 KMALLOC(hm, hostmap_t *);
275 hm->hm_next = maptable[hv];
276 hm->hm_pnext = maptable + hv;
278 maptable[hv]->hm_pnext = &hm->hm_next;
281 hm->hm_realip = real;
290 * Must be called with ipf_nat held as a write lock.
292 static void nat_hostmapdel(hm)
295 ATOMIC_DEC32(hm->hm_ref);
296 if (hm->hm_ref == 0) {
298 hm->hm_next->hm_pnext = hm->hm_pnext;
299 *hm->hm_pnext = hm->hm_next;
305 void fix_outcksum(fin, sp, n)
310 register u_short sumshort;
311 register u_32_t sum1;
315 else if (n & NAT_HW_CKSUM) {
318 n = (n & 0xffff) + (n >> 16);
322 sum1 = (~ntohs(*sp)) & 0xffff;
324 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
326 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
327 sumshort = ~(u_short)sum1;
328 *(sp) = htons(sumshort);
332 void fix_incksum(fin, sp, n)
337 register u_short sumshort;
338 register u_32_t sum1;
342 else if (n & NAT_HW_CKSUM) {
345 n = (n & 0xffff) + (n >> 16);
350 sum1 = (~(*sp)) & 0xffff;
352 sum1 = (~ntohs(*sp)) & 0xffff;
354 sum1 += ~(n) & 0xffff;
355 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
357 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
358 sumshort = ~(u_short)sum1;
359 *(sp) = htons(sumshort);
364 * fix_datacksum is used *only* for the adjustments of checksums in the data
365 * section of an IP packet.
367 * The only situation in which you need to do this is when NAT'ing an
368 * ICMP error message. Such a message, contains in its body the IP header
369 * of the original IP packet, that causes the error.
371 * You can't use fix_incksum or fix_outcksum in that case, because for the
372 * kernel the data section of the ICMP error is just data, and no special
373 * processing like hardware cksum or ntohs processing have been done by the
374 * kernel on the data section.
376 void fix_datacksum(sp, n)
380 register u_short sumshort;
381 register u_32_t sum1;
386 sum1 = (~ntohs(*sp)) & 0xffff;
388 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
390 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
391 sumshort = ~(u_short)sum1;
392 *(sp) = htons(sumshort);
396 * How the NAT is organised and works.
398 * Inside (interface y) NAT Outside (interface x)
399 * -------------------- -+- -------------------------------------
400 * Packet going | out, processsed by ip_natout() for x
401 * ------------> | ------------>
402 * src=10.1.1.1 | src=192.1.1.1
404 * | in, processed by ip_natin() for x
405 * <------------ | <------------
406 * dst=10.1.1.1 | dst=192.1.1.1
407 * -------------------- -+- -------------------------------------
408 * ip_natout() - changes ip_src and if required, sport
409 * - creates a new mapping, if required.
410 * ip_natin() - changes ip_dst and if required, dport
412 * In the NAT table, internal source is recorded as "in" and externally
417 * Handle ioctls which manipulate the NAT.
419 int nat_ioctl(data, cmd, mode)
420 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
428 register ipnat_t *nat, *nt, *n = NULL, **np = NULL;
429 int error = 0, ret, arg;
433 #if (BSD >= 199306) && defined(_KERNEL)
434 if ((securelevel >= 2) && (mode & FWRITE))
438 nat = NULL; /* XXX gcc -Wuninitialized */
439 KMALLOC(nt, ipnat_t *);
440 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT))
441 error = IRCOPYPTR(data, (char *)&natd, sizeof(natd));
442 else if (cmd == SIOCIPFFL) { /* SIOCFLNAT & SIOCCNATL */
443 error = IRCOPY(data, (char *)&arg, sizeof(arg));
452 * For add/delete, look to see if the NAT entry is already present
454 WRITE_ENTER(&ipf_nat);
455 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
457 nat->in_flags &= IPN_USERFLAGS;
458 if ((nat->in_redir & NAT_MAPBLK) == 0) {
459 if ((nat->in_flags & IPN_SPLIT) == 0)
460 nat->in_inip &= nat->in_inmsk;
461 if ((nat->in_flags & IPN_IPRANGE) == 0)
462 nat->in_outip &= nat->in_outmsk;
464 for (np = &nat_list; (n = *np); np = &n->in_next)
465 if (!bcmp((char *)&nat->in_flags, (char *)&n->in_flags,
477 if (!(mode & FWRITE))
480 tmp = ipflog_clear(IPL_LOGNAT);
481 IWCOPY((char *)&tmp, (char *)data, sizeof(tmp));
487 if (!(mode & FWRITE)) {
501 bcopy((char *)nat, (char *)n, sizeof(*n));
502 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
504 n->in_ifp = (void *)-1;
505 if (n->in_plabel[0] != '\0') {
506 n->in_apr = appr_match(n->in_p, n->in_plabel);
515 if (n->in_redir & NAT_REDIRECT) {
516 n->in_flags &= ~IPN_NOTDST;
519 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
520 n->in_flags &= ~IPN_NOTSRC;
525 if (n->in_redir & NAT_MAPBLK)
526 n->in_space = USABLE_PORTS * ~ntohl(n->in_outmsk);
527 else if (n->in_flags & IPN_AUTOPORTMAP)
528 n->in_space = USABLE_PORTS * ~ntohl(n->in_inmsk);
529 else if (n->in_flags & IPN_IPRANGE)
530 n->in_space = ntohl(n->in_outmsk) - ntohl(n->in_outip);
531 else if (n->in_flags & IPN_SPLIT)
534 n->in_space = ~ntohl(n->in_outmsk);
536 * Calculate the number of valid IP addresses in the output
537 * mapping range. In all cases, the range is inclusive of
538 * the start and ending IP addresses.
539 * If to a CIDR address, lose 2: broadcast + network address
541 * If to a range, add one.
542 * If to a single IP address, set to 1.
545 if ((n->in_flags & IPN_IPRANGE) != 0)
551 if ((n->in_outmsk != 0xffffffff) && (n->in_outmsk != 0) &&
552 ((n->in_flags & (IPN_IPRANGE|IPN_SPLIT)) == 0))
553 n->in_nip = ntohl(n->in_outip) + 1;
554 else if ((n->in_flags & IPN_SPLIT) &&
555 (n->in_redir & NAT_REDIRECT))
556 n->in_nip = ntohl(n->in_inip);
558 n->in_nip = ntohl(n->in_outip);
559 if (n->in_redir & NAT_MAP) {
560 n->in_pnext = ntohs(n->in_pmin);
562 * Multiply by the number of ports made available.
564 if (ntohs(n->in_pmax) >= ntohs(n->in_pmin)) {
565 n->in_space *= (ntohs(n->in_pmax) -
566 ntohs(n->in_pmin) + 1);
568 * Because two different sources can map to
569 * different destinations but use the same
571 * If the result is smaller than in_space, then
572 * we may have wrapped around 32bits.
575 if ((i != 0) && (i != 0xffffffff)) {
576 j = n->in_space * (~ntohl(i) + 1);
577 if (j >= n->in_space)
580 n->in_space = 0xffffffff;
584 * If no protocol is specified, multiple by 256.
586 if ((n->in_flags & IPN_TCPUDP) == 0) {
587 j = n->in_space * 256;
588 if (j >= n->in_space)
591 n->in_space = 0xffffffff;
594 /* Otherwise, these fields are preset */
596 nat_stats.ns_rules++;
599 if (!(mode & FWRITE)) {
608 if (n->in_redir & NAT_REDIRECT)
610 if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
612 if (nat_list == NULL) {
619 appr_free(n->in_apr);
621 nat_stats.ns_rules--;
623 n->in_flags |= IPN_DELETE;
629 MUTEX_DOWNGRADE(&ipf_nat);
630 nat_stats.ns_table[0] = nat_table[0];
631 nat_stats.ns_table[1] = nat_table[1];
632 nat_stats.ns_list = nat_list;
633 nat_stats.ns_maptable = maptable;
634 nat_stats.ns_nattab_sz = ipf_nattable_sz;
635 nat_stats.ns_rultab_sz = ipf_natrules_sz;
636 nat_stats.ns_rdrtab_sz = ipf_rdrrules_sz;
637 nat_stats.ns_hostmap_sz = ipf_hostmap_sz;
638 nat_stats.ns_instances = nat_instances;
639 nat_stats.ns_apslist = ap_sess_list;
640 error = IWCOPYPTR((char *)&nat_stats, (char *)data,
647 MUTEX_DOWNGRADE(&ipf_nat);
648 error = IRCOPYPTR((char *)data, (char *)&nl, sizeof(nl));
652 if (nat_lookupredir(&nl)) {
653 error = IWCOPYPTR((char *)&nl, (char *)data,
659 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
660 if (!(mode & FWRITE)) {
666 ret = nat_flushtable();
668 ret = nat_clearlist();
671 MUTEX_DOWNGRADE(&ipf_nat);
673 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
679 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
681 error = IWCOPY((caddr_t)&fr_nat_lock, data,
682 sizeof(fr_nat_lock));
690 error = fr_natputent(data);
696 error = fr_natgetsz(data);
702 error = fr_natgetent(data);
708 arg = (int)iplused[IPL_LOGNAT];
709 MUTEX_DOWNGRADE(&ipf_nat);
710 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
719 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
727 static int fr_natgetsz(data)
735 error = IRCOPY(data, (caddr_t)&ng, sizeof(ng));
744 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
751 * Make sure the pointer we're copying from exists in the
752 * current list of entries. Security precaution to prevent
753 * copying of random kernel data.
755 for (n = nat_instances; n; n = n->nat_next)
762 ng.ng_sz = sizeof(nat_save_t);
764 if ((aps != NULL) && (aps->aps_data != 0)) {
765 ng.ng_sz += sizeof(ap_session_t);
766 ng.ng_sz += aps->aps_psiz;
769 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
776 static int fr_natgetent(data)
779 nat_save_t ipn, *ipnp, *ipnn = NULL;
780 register nat_t *n, *nat;
784 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
787 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
795 if (nat_instances == NULL)
801 * Make sure the pointer we're copying from exists in the
802 * current list of entries. Security precaution to prevent
803 * copying of random kernel data.
805 for (n = nat_instances; n; n = n->nat_next)
812 ipn.ipn_next = nat->nat_next;
814 bcopy((char *)nat, (char *)&ipn.ipn_nat, sizeof(ipn.ipn_nat));
815 ipn.ipn_nat.nat_data = NULL;
818 bcopy((char *)nat->nat_ptr, (char *)&ipn.ipn_ipnat,
819 sizeof(ipn.ipn_ipnat));
823 bcopy((char *)nat->nat_fr, (char *)&ipn.ipn_rule,
824 sizeof(ipn.ipn_rule));
826 if ((aps = nat->nat_aps)) {
827 ipn.ipn_dsize = sizeof(*aps);
829 ipn.ipn_dsize += aps->aps_psiz;
830 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + ipn.ipn_dsize);
833 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
835 bcopy((char *)aps, ipnn->ipn_data, sizeof(*aps));
837 bcopy(aps->aps_data, ipnn->ipn_data + sizeof(*aps),
839 ipnn->ipn_dsize += aps->aps_psiz;
841 error = IWCOPY((caddr_t)ipnn, ipnp,
842 sizeof(ipn) + ipn.ipn_dsize);
845 KFREES(ipnn, sizeof(*ipnn) + ipn.ipn_dsize);
847 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
855 static int fr_natputent(data)
858 nat_save_t ipn, *ipnp, *ipnn = NULL;
859 register nat_t *n, *nat;
866 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
869 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
874 KMALLOCS(ipnn, nat_save_t *, sizeof(ipn) + ipn.ipn_dsize);
877 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
878 error = IRCOPY((caddr_t)ipnp, (caddr_t)ipn.ipn_data,
887 KMALLOC(nat, nat_t *);
893 bcopy((char *)&ipn.ipn_nat, (char *)nat, sizeof(*nat));
895 * Initialize all these so that nat_delete() doesn't cause a crash.
897 nat->nat_phnext[0] = NULL;
898 nat->nat_phnext[1] = NULL;
906 nat->nat_data = NULL;
909 * Restore the rule associated with this nat session
912 KMALLOC(in, ipnat_t *);
918 bcopy((char *)&ipn.ipn_ipnat, (char *)in, sizeof(*in));
920 in->in_flags |= IPN_DELETE;
923 in->in_prnext = NULL;
925 in->in_pmnext = NULL;
926 in->in_ifp = GETUNIT(in->in_ifname, 4);
927 if (in->in_plabel[0] != '\0') {
928 in->in_apr = appr_match(in->in_p, in->in_plabel);
933 * Restore ap_session_t structure. Include the private data allocated
937 KMALLOC(aps, ap_session_t *);
943 aps->aps_next = ap_sess_list;
945 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
947 aps->aps_apr = in->in_apr;
949 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
950 if (aps->aps_data == NULL) {
954 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
958 aps->aps_data = NULL;
963 * If there was a filtering rule associated with this entry then
964 * build up a new one.
967 if (nat->nat_flags & FI_NEWFR) {
968 KMALLOC(fr, frentry_t *);
974 bcopy((char *)&ipn.ipn_fr, (char *)fr, sizeof(*fr));
975 ipn.ipn_nat.nat_fr = fr;
976 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
982 for (n = nat_instances; n; n = n->nat_next)
993 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
998 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1006 * Delete a nat entry from the various lists and table.
1008 static void nat_delete(natd)
1013 if (natd->nat_flags & FI_WILDP)
1014 nat_stats.ns_wilds--;
1015 if (natd->nat_hnext[0])
1016 natd->nat_hnext[0]->nat_phnext[0] = natd->nat_phnext[0];
1017 *natd->nat_phnext[0] = natd->nat_hnext[0];
1018 if (natd->nat_hnext[1])
1019 natd->nat_hnext[1]->nat_phnext[1] = natd->nat_phnext[1];
1020 *natd->nat_phnext[1] = natd->nat_hnext[1];
1022 if (natd->nat_fr != NULL) {
1023 ATOMIC_DEC32(natd->nat_fr->fr_ref);
1026 if (natd->nat_hm != NULL)
1027 nat_hostmapdel(natd->nat_hm);
1030 * If there is an active reference from the nat entry to its parent
1031 * rule, decrement the rule's reference count and free it too if no
1032 * longer being used.
1034 ipn = natd->nat_ptr;
1038 if (!ipn->in_use && (ipn->in_flags & IPN_DELETE)) {
1040 appr_free(ipn->in_apr);
1042 nat_stats.ns_rules--;
1046 MUTEX_DESTROY(&natd->nat_lock);
1048 * If there's a fragment table entry too for this nat entry, then
1049 * dereference that as well.
1051 ipfr_forget((void *)natd);
1052 aps_free(natd->nat_aps);
1053 nat_stats.ns_inuse--;
1059 * nat_flushtable - clear the NAT table of all mapping entries.
1061 static int nat_flushtable()
1063 register nat_t *nat, **natp;
1067 * ALL NAT mappings deleted, so lets just make the deletions
1070 if (nat_table[0] != NULL)
1071 bzero((char *)nat_table[0],
1072 sizeof(nat_table[0]) * ipf_nattable_sz);
1073 if (nat_table[1] != NULL)
1074 bzero((char *)nat_table[1],
1075 sizeof(nat_table[1]) * ipf_nattable_sz);
1077 for (natp = &nat_instances; (nat = *natp); ) {
1078 *natp = nat->nat_next;
1080 nat_log(nat, NL_FLUSH);
1085 nat_stats.ns_inuse = 0;
1091 * nat_clearlist - delete all rules in the active NAT mapping list.
1093 static int nat_clearlist()
1095 register ipnat_t *n, **np = &nat_list;
1098 if (nat_rules != NULL)
1099 bzero((char *)nat_rules, sizeof(*nat_rules) * ipf_natrules_sz);
1100 if (rdr_rules != NULL)
1101 bzero((char *)rdr_rules, sizeof(*rdr_rules) * ipf_rdrrules_sz);
1107 appr_free(n->in_apr);
1109 nat_stats.ns_rules--;
1111 n->in_flags |= IPN_DELETE;
1123 * Create a new NAT table entry.
1124 * NOTE: assumes write lock on ipf_nat has been obtained already.
1126 nat_t *nat_new(np, ip, fin, flags, direction)
1133 register u_32_t sum1, sum2, sumd, l;
1134 u_short port = 0, sport = 0, dport = 0, nport = 0;
1135 struct in_addr in, inb;
1136 tcphdr_t *tcp = NULL;
1137 hostmap_t *hm = NULL;
1140 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1141 qif_t *qf = fin->fin_qif;
1144 nflags = flags & np->in_flags;
1145 if (flags & IPN_TCPUDP) {
1146 tcp = (tcphdr_t *)fin->fin_dp;
1147 sport = tcp->th_sport;
1148 dport = tcp->th_dport;
1151 /* Give me a new nat */
1152 KMALLOC(nat, nat_t *);
1154 nat_stats.ns_memfail++;
1158 bzero((char *)nat, sizeof(*nat));
1159 nat->nat_flags = flags;
1160 if (flags & FI_WILDP)
1161 nat_stats.ns_wilds++;
1163 * Search the current table for a match.
1165 if (direction == NAT_OUTBOUND) {
1167 * Values at which the search for a free resouce starts.
1173 * If it's an outbound packet which doesn't match any existing
1174 * record, then create a new port
1178 st_port = np->in_pnext;
1182 in.s_addr = htonl(np->in_nip);
1185 * Check to see if there is an existing NAT
1186 * setup for this IP address pair.
1188 hm = nat_hostmap(np, ip->ip_src, in);
1190 in.s_addr = hm->hm_mapip.s_addr;
1191 } else if ((l == 1) && (hm != NULL)) {
1195 in.s_addr = ntohl(in.s_addr);
1199 if ((np->in_outmsk == 0xffffffff) &&
1200 (np->in_pnext == 0)) {
1205 if (np->in_redir & NAT_MAPBLK) {
1206 if ((l >= np->in_ppip) || ((l > 0) &&
1207 !(flags & IPN_TCPUDP)))
1210 * map-block - Calculate destination address.
1212 in.s_addr = ntohl(ip->ip_src.s_addr);
1213 in.s_addr &= ntohl(~np->in_inmsk);
1214 inb.s_addr = in.s_addr;
1215 in.s_addr /= np->in_ippip;
1216 in.s_addr &= ntohl(~np->in_outmsk);
1217 in.s_addr += ntohl(np->in_outip);
1219 * Calculate destination port.
1221 if ((flags & IPN_TCPUDP) &&
1222 (np->in_ppip != 0)) {
1223 port = ntohs(sport) + l;
1224 port %= np->in_ppip;
1225 port += np->in_ppip *
1226 (inb.s_addr % np->in_ippip);
1227 port += MAPBLK_MINPORT;
1230 } else if (!np->in_outip &&
1231 (np->in_outmsk == 0xffffffff)) {
1233 * 0/32 - use the interface's IP address.
1236 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1238 in.s_addr = ntohl(in.s_addr);
1239 } else if (!np->in_outip && !np->in_outmsk) {
1241 * 0/0 - use the original source address/port.
1245 in.s_addr = ntohl(ip->ip_src.s_addr);
1246 } else if ((np->in_outmsk != 0xffffffff) &&
1247 (np->in_pnext == 0) &&
1248 ((l > 0) || (hm == NULL)))
1252 if ((nflags & IPN_TCPUDP) &&
1253 ((np->in_redir & NAT_MAPBLK) == 0) &&
1254 (np->in_flags & IPN_AUTOPORTMAP)) {
1255 if ((l > 0) && (l % np->in_ppip == 0)) {
1256 if (l > np->in_space) {
1258 } else if ((l > np->in_ppip) &&
1259 np->in_outmsk != 0xffffffff)
1262 if (np->in_ppip != 0) {
1263 port = ntohs(sport);
1264 port += (l % np->in_ppip);
1265 port %= np->in_ppip;
1266 port += np->in_ppip *
1267 (ntohl(ip->ip_src.s_addr) %
1269 port += MAPBLK_MINPORT;
1272 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1273 (nflags & IPN_TCPUDP) &&
1274 (np->in_pnext != 0)) {
1275 port = htons(np->in_pnext++);
1276 if (np->in_pnext > ntohs(np->in_pmax)) {
1277 np->in_pnext = ntohs(np->in_pmin);
1278 if (np->in_outmsk != 0xffffffff)
1283 if (np->in_flags & IPN_IPRANGE) {
1284 if (np->in_nip > ntohl(np->in_outmsk))
1285 np->in_nip = ntohl(np->in_outip);
1287 if ((np->in_outmsk != 0xffffffff) &&
1288 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1289 ntohl(np->in_outip))
1290 np->in_nip = ntohl(np->in_outip) + 1;
1293 if (!port && (flags & IPN_TCPUDP))
1297 * Here we do a lookup of the connection as seen from
1298 * the outside. If an IP# pair already exists, try
1299 * again. So if you have A->B becomes C->B, you can
1300 * also have D->E become C->E but not D->B causing
1301 * another C->B. Also take protocol and ports into
1302 * account when determining whether a pre-existing
1303 * NAT setup will cause an external conflict where
1304 * this is appropriate.
1306 inb.s_addr = htonl(in.s_addr);
1307 natl = nat_inlookup(fin->fin_ifp, flags & ~FI_WILDP,
1308 (u_int)ip->ip_p, ip->ip_dst, inb,
1309 (port << 16) | dport, 1);
1312 * Has the search wrapped around and come back to the
1315 if ((natl != NULL) &&
1316 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1317 (np->in_nip != 0) && (st_ip == np->in_nip))
1320 } while (natl != NULL);
1322 if (np->in_space > 0)
1325 /* Setup the NAT table */
1326 nat->nat_inip = ip->ip_src;
1327 nat->nat_outip.s_addr = htonl(in.s_addr);
1328 nat->nat_oip = ip->ip_dst;
1329 if (nat->nat_hm == NULL)
1330 nat->nat_hm = nat_hostmap(np, ip->ip_src,
1333 sum1 = LONG_SUM(ntohl(ip->ip_src.s_addr)) + ntohs(sport);
1334 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1336 if (flags & IPN_TCPUDP) {
1337 nat->nat_inport = sport;
1338 nat->nat_outport = port; /* sport */
1339 nat->nat_oport = dport;
1343 * Otherwise, it's an inbound packet. Most likely, we don't
1344 * want to rewrite source ports and source addresses. Instead,
1345 * we want to rewrite to a fixed internal address and fixed
1348 if (np->in_flags & IPN_SPLIT) {
1349 in.s_addr = np->in_nip;
1350 if (np->in_inip == htonl(in.s_addr))
1351 np->in_nip = ntohl(np->in_inmsk);
1353 np->in_nip = ntohl(np->in_inip);
1354 if (np->in_flags & IPN_ROUNDR) {
1360 in.s_addr = ntohl(np->in_inip);
1361 if (np->in_flags & IPN_ROUNDR) {
1370 * Whilst not optimized for the case where
1371 * pmin == pmax, the gain is not significant.
1373 nport = ntohs(dport) - ntohs(np->in_pmin) +
1374 ntohs(np->in_pnext);
1375 nport = htons(nport);
1379 * When the redirect-to address is set to 0.0.0.0, just
1380 * assume a blank `forwarding' of the packet. We don't
1381 * setup any translation for this either.
1383 if (in.s_addr == 0) {
1386 in.s_addr = ntohl(ip->ip_dst.s_addr);
1389 nat->nat_inip.s_addr = htonl(in.s_addr);
1390 nat->nat_outip = ip->ip_dst;
1391 nat->nat_oip = ip->ip_src;
1393 sum1 = LONG_SUM(ntohl(ip->ip_dst.s_addr)) + ntohs(dport);
1394 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1396 if (flags & IPN_TCPUDP) {
1397 nat->nat_inport = nport;
1398 nat->nat_outport = dport;
1399 nat->nat_oport = sport;
1403 CALC_SUMD(sum1, sum2, sumd);
1404 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1405 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1406 if ((flags & IPN_TCPUDP) && dohwcksum &&
1407 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1408 if (direction == NAT_OUTBOUND)
1409 sum1 = LONG_SUM(ntohl(in.s_addr));
1411 sum1 = LONG_SUM(ntohl(ip->ip_src.s_addr));
1412 sum1 += LONG_SUM(ntohl(ip->ip_dst.s_addr));
1413 sum1 += IPPROTO_TCP;
1414 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1415 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1418 nat->nat_sumd[1] = nat->nat_sumd[0];
1420 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1421 if (direction == NAT_OUTBOUND)
1422 sum1 = LONG_SUM(ntohl(ip->ip_src.s_addr));
1424 sum1 = LONG_SUM(ntohl(ip->ip_dst.s_addr));
1426 sum2 = LONG_SUM(in.s_addr);
1428 CALC_SUMD(sum1, sum2, sumd);
1429 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1431 nat->nat_ipsumd = nat->nat_sumd[0];
1433 in.s_addr = htonl(in.s_addr);
1436 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1440 nat->nat_dir = direction;
1441 nat->nat_ifp = fin->fin_ifp;
1443 nat->nat_p = ip->ip_p;
1446 nat->nat_fr = fin->fin_fr;
1447 if (nat->nat_fr != NULL) {
1448 ATOMIC_INC32(nat->nat_fr->fr_ref);
1450 if (direction == NAT_OUTBOUND) {
1451 if (flags & IPN_TCPUDP)
1452 tcp->th_sport = port;
1454 if (flags & IPN_TCPUDP)
1455 tcp->th_dport = nport;
1459 nat_log(nat, (u_int)np->in_redir);
1463 nat_stats.ns_badnat++;
1464 if ((hm = nat->nat_hm) != NULL)
1471 void nat_insert(nat)
1477 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1479 nat->nat_age = fr_defnatage;
1480 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1481 if (nat->nat_ifname[0] !='\0') {
1482 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1485 nat->nat_next = nat_instances;
1486 nat_instances = nat;
1488 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1489 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1491 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1493 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1495 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1498 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, 0, 0xffffffff);
1499 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1, ipf_nattable_sz);
1500 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, 0, 0xffffffff);
1501 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2, ipf_nattable_sz);
1504 natp = &nat_table[0][hv1];
1506 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1507 nat->nat_phnext[0] = natp;
1508 nat->nat_hnext[0] = *natp;
1511 natp = &nat_table[1][hv2];
1513 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1514 nat->nat_phnext[1] = natp;
1515 nat->nat_hnext[1] = *natp;
1518 nat_stats.ns_added++;
1519 nat_stats.ns_inuse++;
1523 nat_t *nat_icmplookup(ip, fin, dir)
1529 tcphdr_t *tcp = NULL;
1531 int flags = 0, type, minlen;
1533 icmp = (icmphdr_t *)fin->fin_dp;
1535 * Does it at least have the return (basic) IP header ?
1536 * Only a basic IP header (no options) should be with an ICMP error
1539 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1541 type = icmp->icmp_type;
1543 * If it's not an error type, then return.
1545 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1546 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1547 (type != ICMP_PARAMPROB))
1550 oip = (ip_t *)((char *)fin->fin_dp + 8);
1551 minlen = (oip->ip_hl << 2);
1552 if (minlen < sizeof(ip_t))
1554 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1557 * Is the buffer big enough for all of it ? It's the size of the IP
1558 * header claimed in the encapsulated part which is of concern. It
1559 * may be too big to be in this buffer but not so big that it's
1560 * outside the ICMP packet, leading to TCP deref's causing problems.
1561 * This is possible because we don't know how big oip_hl is when we
1562 * do the pullup early in fr_check() and thus can't gaurantee it is
1571 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1574 m = *(mb_t **)fin->fin_mp;
1575 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1576 (char *)ip + m->m_len)
1582 if (oip->ip_p == IPPROTO_TCP)
1584 else if (oip->ip_p == IPPROTO_UDP)
1586 if (flags & IPN_TCPUDP) {
1587 minlen += 8; /* + 64bits of data to get ports */
1588 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1590 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1591 if (dir == NAT_INBOUND)
1592 return nat_inlookup(fin->fin_ifp, flags,
1593 (u_int)oip->ip_p, oip->ip_dst, oip->ip_src,
1594 (tcp->th_sport << 16) | tcp->th_dport, 0);
1596 return nat_outlookup(fin->fin_ifp, flags,
1597 (u_int)oip->ip_p, oip->ip_dst, oip->ip_src,
1598 (tcp->th_sport << 16) | tcp->th_dport, 0);
1600 if (dir == NAT_INBOUND)
1601 return nat_inlookup(fin->fin_ifp, 0, (u_int)oip->ip_p,
1602 oip->ip_dst, oip->ip_src, 0, 0);
1604 return nat_outlookup(fin->fin_ifp, 0, (u_int)oip->ip_p,
1605 oip->ip_dst, oip->ip_src, 0, 0);
1610 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1611 * packet gets correctly recognised.
1613 nat_t *nat_icmp(ip, fin, nflags, dir)
1619 u_32_t sum1, sum2, sumd, sumd2 = 0;
1627 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1630 * nat_icmplookup() will return NULL for `defective' packets.
1632 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1634 *nflags = IPN_ICMPERR;
1635 icmp = (icmphdr_t *)fin->fin_dp;
1636 oip = (ip_t *)&icmp->icmp_ip;
1637 if (oip->ip_p == IPPROTO_TCP)
1639 else if (oip->ip_p == IPPROTO_UDP)
1641 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1643 * Need to adjust ICMP header to include the real IP#'s and
1644 * port #'s. Only apply a checksum change relative to the
1645 * IP address change as it will be modified again in ip_natout
1646 * for both address and port. Two checksum changes are
1647 * necessary for the two header address changes. Be careful
1648 * to only modify the checksum once for the port # and twice
1654 * Fix the IP addresses in the offending IP packet. You also need
1655 * to adjust the IP header checksum of that offending IP packet
1656 * and the ICMP checksum of the ICMP error message itself.
1658 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1659 * in the pseudo header that is used to compute the UDP resp. TCP
1660 * checksum. So, we must compensate that as well. Even worse, the
1661 * change in the UDP and TCP checksums require yet another
1662 * adjustment of the ICMP checksum of the ICMP error message.
1664 * For the moment we forget about TCP, because that checksum is not
1665 * in the first 8 bytes, so it will not be available in most cases.
1668 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1669 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1673 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1674 in = nat->nat_outip;
1678 sum2 = LONG_SUM(ntohl(in.s_addr));
1680 CALC_SUMD(sum1, sum2, sumd);
1682 if (nat->nat_dir == NAT_OUTBOUND) {
1684 * Fix IP checksum of the offending IP packet to adjust for
1685 * the change in the IP address.
1687 * Normally, you would expect that the ICMP checksum of the
1688 * ICMP error message needs to be adjusted as well for the
1689 * IP address change in oip.
1690 * However, this is a NOP, because the ICMP checksum is
1691 * calculated over the complete ICMP packet, which includes the
1692 * changed oip IP addresses and oip->ip_sum. However, these
1693 * two changes cancel each other out (if the delta for
1694 * the IP address is x, then the delta for ip_sum is minus x),
1695 * so no change in the icmp_cksum is necessary.
1697 * Be careful that nat_dir refers to the direction of the
1698 * offending IP packet (oip), not to its ICMP response (icmp)
1700 fix_datacksum(&oip->ip_sum, sumd);
1703 * Fix UDP pseudo header checksum to compensate for the
1704 * IP address change.
1706 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1708 * The UDP checksum is optional, only adjust it
1709 * if it has been set.
1711 sum1 = ntohs(udp->uh_sum);
1712 fix_datacksum(&udp->uh_sum, sumd);
1713 sum2 = ntohs(udp->uh_sum);
1716 * Fix ICMP checksum to compensate the UDP
1717 * checksum adjustment.
1719 CALC_SUMD(sum1, sum2, sumd);
1725 * Fix TCP pseudo header checksum to compensate for the
1726 * IP address change. Before we can do the change, we
1727 * must make sure that oip is sufficient large to hold
1728 * the TCP checksum (normally it does not!).
1730 if (oip->ip_p == IPPROTO_TCP) {
1737 * Fix IP checksum of the offending IP packet to adjust for
1738 * the change in the IP address.
1740 * Normally, you would expect that the ICMP checksum of the
1741 * ICMP error message needs to be adjusted as well for the
1742 * IP address change in oip.
1743 * However, this is a NOP, because the ICMP checksum is
1744 * calculated over the complete ICMP packet, which includes the
1745 * changed oip IP addresses and oip->ip_sum. However, these
1746 * two changes cancel each other out (if the delta for
1747 * the IP address is x, then the delta for ip_sum is minus x),
1748 * so no change in the icmp_cksum is necessary.
1750 * Be careful that nat_dir refers to the direction of the
1751 * offending IP packet (oip), not to its ICMP response (icmp)
1753 fix_datacksum(&oip->ip_sum, sumd);
1755 /* XXX FV : without having looked at Solaris source code, it seems unlikely
1756 * that SOLARIS would compensate this in the kernel (a body of an IP packet
1757 * in the data section of an ICMP packet). I have the feeling that this should
1758 * be unconditional, but I'm not in a position to check.
1760 #if !SOLARIS && !defined(__sgi)
1762 * Fix UDP pseudo header checksum to compensate for the
1763 * IP address change.
1765 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1767 * The UDP checksum is optional, only adjust it
1768 * if it has been set
1770 sum1 = ntohs(udp->uh_sum);
1771 fix_datacksum(&udp->uh_sum, sumd);
1772 sum2 = ntohs(udp->uh_sum);
1775 * Fix ICMP checksum to compensate the UDP
1776 * checksum adjustment.
1778 CALC_SUMD(sum1, sum2, sumd);
1784 * Fix TCP pseudo header checksum to compensate for the
1785 * IP address change. Before we can do the change, we
1786 * must make sure that oip is sufficient large to hold
1787 * the TCP checksum (normally it does not!).
1789 if (oip->ip_p == IPPROTO_TCP) {
1797 if ((flags & IPN_TCPUDP) != 0) {
1801 * XXX - what if this is bogus hl and we go off the end ?
1802 * In this case, nat_icmpinlookup() will have returned NULL.
1804 tcp = (tcphdr_t *)udp;
1808 * For offending TCP/UDP IP packets, translate the ports as
1809 * well, based on the NAT specification. Of course such
1810 * a change must be reflected in the ICMP checksum as well.
1812 * Advance notice : Now it becomes complicated :-)
1814 * Since the port fields are part of the TCP/UDP checksum
1815 * of the offending IP packet, you need to adjust that checksum
1816 * as well... but, if you change, you must change the icmp
1817 * checksum *again*, to reflect that change.
1819 * To further complicate: the TCP checksum is not in the first
1820 * 8 bytes of the offending ip packet, so it most likely is not
1821 * available (we might have to fix that if the encounter a
1822 * device that returns more than 8 data bytes on icmp error)
1825 if (nat->nat_oport == tcp->th_dport) {
1826 if (tcp->th_sport != nat->nat_inport) {
1828 * Fix ICMP checksum to compensate port
1831 sum1 = ntohs(tcp->th_sport);
1832 sum2 = ntohs(nat->nat_inport);
1833 CALC_SUMD(sum1, sum2, sumd);
1835 tcp->th_sport = nat->nat_inport;
1838 * Fix udp checksum to compensate port
1839 * adjustment. NOTE : the offending IP packet
1840 * flows the other direction compared to the
1843 * The UDP checksum is optional, only adjust
1844 * it if it has been set.
1846 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1848 sum1 = ntohs(udp->uh_sum);
1849 fix_datacksum(&udp->uh_sum, sumd);
1850 sum2 = ntohs(udp->uh_sum);
1853 * Fix ICMP checksum to
1854 * compensate UDP checksum
1857 CALC_SUMD(sum1, sum2, sumd);
1862 if (tcp->th_dport != nat->nat_outport) {
1864 * Fix ICMP checksum to compensate port
1867 sum1 = ntohs(tcp->th_dport);
1868 sum2 = ntohs(nat->nat_outport);
1869 CALC_SUMD(sum1, sum2, sumd);
1871 tcp->th_dport = nat->nat_outport;
1874 * Fix udp checksum to compensate port
1875 * adjustment. NOTE : the offending IP
1876 * packet flows the other direction compared
1877 * to the ICMP message.
1879 * The UDP checksum is optional, only adjust
1880 * it if it has been set.
1882 if (oip->ip_p == IPPROTO_UDP && 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);
1898 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1899 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1900 if (nat->nat_dir == NAT_OUTBOUND) {
1901 fix_outcksum(fin, &icmp->icmp_cksum, sumd2);
1903 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
1907 if (oip->ip_p == IPPROTO_ICMP)
1908 nat->nat_age = fr_defnaticmpage;
1914 * NB: these lookups don't lock access to the list, it assume it has already
1918 * Lookup a nat entry based on the mapped destination ip address/port and
1919 * real source address/port. We use this lookup when receiving a packet,
1920 * we're looking for a table entry, based on the destination address.
1921 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
1923 nat_t *nat_inlookup(ifp, flags, p, src, mapdst, ports, rw)
1925 register u_int flags, p;
1926 struct in_addr src , mapdst;
1930 register u_short sport, dport;
1931 register nat_t *nat;
1932 register int nflags;
1933 register u_32_t dst;
1936 dst = mapdst.s_addr;
1937 dport = ports >> 16;
1938 sport = ports & 0xffff;
1939 flags &= IPN_TCPUDP;
1941 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
1942 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
1943 nat = nat_table[1][hv];
1944 for (; nat; nat = nat->nat_hnext[1]) {
1945 nflags = nat->nat_flags;
1946 if ((!ifp || ifp == nat->nat_ifp) &&
1947 nat->nat_oip.s_addr == src.s_addr &&
1948 nat->nat_outip.s_addr == dst &&
1949 (((p == 0) && (flags == (nat->nat_flags & IPN_TCPUDP)))
1950 || (p == nat->nat_p)) && (!flags ||
1951 (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
1952 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT)))))
1955 if (!nat_stats.ns_wilds || !(flags & IPN_TCPUDP))
1958 RWLOCK_EXIT(&ipf_nat);
1960 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
1961 hv = NAT_HASH_FN(src.s_addr, hv, ipf_nattable_sz);
1963 WRITE_ENTER(&ipf_nat);
1965 nat = nat_table[1][hv];
1966 for (; nat; nat = nat->nat_hnext[1]) {
1967 nflags = nat->nat_flags;
1968 if (ifp && ifp != nat->nat_ifp)
1970 if (!(nflags & IPN_TCPUDP))
1972 if (!(nflags & FI_WILDP))
1974 if (nat->nat_oip.s_addr != src.s_addr ||
1975 nat->nat_outip.s_addr != dst)
1977 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
1978 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
1979 nat_tabmove(nat, ports);
1984 MUTEX_DOWNGRADE(&ipf_nat);
1991 * This function is only called for TCP/UDP NAT table entries where the
1992 * original was placed in the table without hashing on the ports and we now
1993 * want to include hashing on port numbers.
1995 static void nat_tabmove(nat, ports)
1999 register u_short sport, dport;
2003 dport = ports >> 16;
2004 sport = ports & 0xffff;
2006 if (nat->nat_oport == dport) {
2007 nat->nat_inport = sport;
2008 nat->nat_outport = sport;
2012 * Remove the NAT entry from the old location
2014 if (nat->nat_hnext[0])
2015 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2016 *nat->nat_phnext[0] = nat->nat_hnext[0];
2018 if (nat->nat_hnext[1])
2019 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2020 *nat->nat_phnext[1] = nat->nat_hnext[1];
2023 * Add into the NAT table in the new position
2025 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2026 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2027 natp = &nat_table[0][hv];
2029 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2030 nat->nat_phnext[0] = natp;
2031 nat->nat_hnext[0] = *natp;
2034 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2035 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2036 natp = &nat_table[1][hv];
2038 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2039 nat->nat_phnext[1] = natp;
2040 nat->nat_hnext[1] = *natp;
2046 * Lookup a nat entry based on the source 'real' ip address/port and
2047 * destination address/port. We use this lookup when sending a packet out,
2048 * we're looking for a table entry, based on the source address.
2049 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2051 nat_t *nat_outlookup(ifp, flags, p, src, dst, ports, rw)
2053 register u_int flags, p;
2054 struct in_addr src , dst;
2058 register u_short sport, dport;
2059 register nat_t *nat;
2060 register int nflags;
2064 sport = ports & 0xffff;
2065 dport = ports >> 16;
2066 flags &= IPN_TCPUDP;
2069 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2070 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2071 nat = nat_table[0][hv];
2072 for (; nat; nat = nat->nat_hnext[0]) {
2073 nflags = nat->nat_flags;
2075 if ((!ifp || ifp == nat->nat_ifp) &&
2076 nat->nat_inip.s_addr == srcip &&
2077 nat->nat_oip.s_addr == dst.s_addr &&
2078 (((p == 0) && (flags == (nflags & IPN_TCPUDP)))
2079 || (p == nat->nat_p)) && (!flags ||
2080 ((nat->nat_inport == sport || nflags & FI_W_SPORT) &&
2081 (nat->nat_oport == dport || nflags & FI_W_DPORT))))
2084 if (!nat_stats.ns_wilds || !(flags & IPN_TCPUDP))
2087 RWLOCK_EXIT(&ipf_nat);
2089 hv = NAT_HASH_FN(srcip, 0, ipf_nattable_sz);
2090 hv = NAT_HASH_FN(dst.s_addr, hv, ipf_nattable_sz);
2092 WRITE_ENTER(&ipf_nat);
2094 nat = nat_table[0][hv];
2095 for (; nat; nat = nat->nat_hnext[0]) {
2096 nflags = nat->nat_flags;
2097 if (ifp && ifp != nat->nat_ifp)
2099 if (!(nflags & IPN_TCPUDP))
2101 if (!(nflags & FI_WILDP))
2103 if ((nat->nat_inip.s_addr != srcip) ||
2104 (nat->nat_oip.s_addr != dst.s_addr))
2106 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2107 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2108 nat_tabmove(nat, ports);
2113 MUTEX_DOWNGRADE(&ipf_nat);
2120 * Lookup the NAT tables to search for a matching redirect
2122 nat_t *nat_lookupredir(np)
2123 register natlookup_t *np;
2128 ports = (np->nl_outport << 16) | np->nl_inport;
2130 * If nl_inip is non null, this is a lookup based on the real
2131 * ip address. Else, we use the fake.
2133 if ((nat = nat_outlookup(NULL, np->nl_flags, 0, np->nl_inip,
2134 np->nl_outip, ports, 0))) {
2135 np->nl_realip = nat->nat_outip;
2136 np->nl_realport = nat->nat_outport;
2142 static int nat_match(fin, np, ip)
2152 if (np->in_p && ip->ip_p != np->in_p)
2155 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2157 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2158 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2160 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2161 ^ ((np->in_flags & IPN_NOTDST) != 0))
2164 if (!(np->in_redir & NAT_REDIRECT))
2166 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2167 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2169 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2170 ^ ((np->in_flags & IPN_NOTDST) != 0))
2175 if (!(fin->fin_fl & FI_TCPUDP) ||
2176 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2177 if (ft->ftu_scmp || ft->ftu_dcmp)
2182 return fr_tcpudpchk(ft, fin);
2187 * Packets going out on the external interface go through this.
2188 * Here, the source address requires alteration, if anything.
2190 int ip_natout(ip, fin)
2194 register ipnat_t *np = NULL;
2195 register u_32_t ipa;
2196 tcphdr_t *tcp = NULL;
2197 u_short sport = 0, dport = 0, *csump = NULL;
2198 int natadd = 1, i, icmpset = 1;
2199 u_int nflags = 0, hv, msk;
2205 if (nat_list == NULL || (fr_nat_lock))
2208 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2209 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1)
2210 ifp = fr->fr_tif.fd_ifp;
2214 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2215 if (ip->ip_p == IPPROTO_TCP)
2217 else if (ip->ip_p == IPPROTO_UDP)
2219 if ((nflags & IPN_TCPUDP)) {
2220 tcp = (tcphdr_t *)fin->fin_dp;
2221 sport = tcp->th_sport;
2222 dport = tcp->th_dport;
2226 ipa = ip->ip_src.s_addr;
2228 READ_ENTER(&ipf_nat);
2230 if ((ip->ip_p == IPPROTO_ICMP) &&
2231 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2233 else if ((fin->fin_fl & FI_FRAG) &&
2234 (nat = ipfr_nat_knownfrag(ip, fin)))
2236 else if ((nat = nat_outlookup(ifp, nflags, (u_int)ip->ip_p,
2237 ip->ip_src, ip->ip_dst,
2238 (dport << 16) | sport, 0))) {
2239 nflags = nat->nat_flags;
2240 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2241 if ((nflags & FI_W_SPORT) &&
2242 (nat->nat_inport != sport))
2243 nat->nat_inport = sport;
2244 else if ((nflags & FI_W_DPORT) &&
2245 (nat->nat_oport != dport))
2246 nat->nat_oport = dport;
2247 if (nat->nat_outport == 0)
2248 nat->nat_outport = sport;
2249 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2250 nflags = nat->nat_flags;
2251 nat_stats.ns_wilds--;
2254 RWLOCK_EXIT(&ipf_nat);
2255 WRITE_ENTER(&ipf_nat);
2257 * If there is no current entry in the nat table for this IP#,
2258 * create one for it (if there is a matching rule).
2263 iph = ipa & htonl(msk);
2264 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2265 for (np = nat_rules[hv]; np; np = np->in_mnext)
2267 if (np->in_ifp && (np->in_ifp != ifp))
2269 if ((np->in_flags & IPN_RF) &&
2270 !(np->in_flags & nflags))
2272 if (np->in_flags & IPN_FILTER) {
2273 if (!nat_match(fin, np, ip))
2275 } else if ((ipa & np->in_inmsk) != np->in_inip)
2277 if (np->in_redir & (NAT_MAP|NAT_MAPBLK)) {
2278 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2281 * If it's a redirection, then we don't want to
2282 * create new outgoing port stuff.
2283 * Redirections are only for incoming
2286 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2288 if ((nat = nat_new(np, ip, fin, (u_int)nflags,
2295 if ((np == NULL) && (i > 0)) {
2299 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2303 MUTEX_DOWNGRADE(&ipf_nat);
2307 * NOTE: ipf_nat must now only be held as a read lock
2311 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2312 ipfr_nat_newfrag(ip, fin, 0, nat);
2313 MUTEX_ENTER(&nat->nat_lock);
2314 nat->nat_age = fr_defnatage;
2315 nat->nat_bytes += ip->ip_len;
2317 MUTEX_EXIT(&nat->nat_lock);
2320 * Fix up checksums, not by recalculating them, but
2321 * simply computing adjustments.
2323 if (nflags == IPN_ICMPERR) {
2324 u_32_t s1, s2, sumd;
2326 s1 = LONG_SUM(ntohl(ip->ip_src.s_addr));
2327 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2328 CALC_SUMD(s1, s2, sumd);
2330 if (nat->nat_dir == NAT_OUTBOUND)
2331 fix_incksum(fin, &ip->ip_sum, sumd);
2333 fix_outcksum(fin, &ip->ip_sum, sumd);
2335 #if SOLARIS || defined(__sgi)
2337 if (nat->nat_dir == NAT_OUTBOUND)
2338 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2340 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2343 ip->ip_src = nat->nat_outip;
2345 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2347 if ((nat->nat_outport != 0) && (nflags & IPN_TCPUDP)) {
2348 tcp->th_sport = nat->nat_outport;
2349 fin->fin_data[0] = ntohs(tcp->th_sport);
2352 if (ip->ip_p == IPPROTO_TCP) {
2353 csump = &tcp->th_sum;
2354 MUTEX_ENTER(&nat->nat_lock);
2355 fr_tcp_age(&nat->nat_age,
2356 nat->nat_tcpstate, fin, 1);
2357 if (nat->nat_age < fr_defnaticmpage)
2358 nat->nat_age = fr_defnaticmpage;
2360 else if (nat->nat_age > fr_defnatage)
2361 nat->nat_age = fr_defnatage;
2364 * Increase this because we may have
2365 * "keep state" following this too and
2366 * packet storms can occur if this is
2367 * removed too quickly.
2369 if (nat->nat_age == fr_tcpclosed)
2370 nat->nat_age = fr_tcplastack;
2371 MUTEX_EXIT(&nat->nat_lock);
2372 } else if (ip->ip_p == IPPROTO_UDP) {
2373 udphdr_t *udp = (udphdr_t *)tcp;
2376 csump = &udp->uh_sum;
2377 } else if (ip->ip_p == IPPROTO_ICMP) {
2379 nat->nat_age = fr_defnaticmpage;
2383 if (nat->nat_dir == NAT_OUTBOUND)
2384 fix_outcksum(fin, csump, nat->nat_sumd[1]);
2386 fix_incksum(fin, csump, nat->nat_sumd[1]);
2390 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
2391 (tcp != NULL && dport == np->in_dport))) {
2392 i = appr_check(ip, fin, nat);
2397 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2398 RWLOCK_EXIT(&ipf_nat); /* READ */
2401 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2407 * Packets coming in from the external interface go through this.
2408 * Here, the destination address requires alteration, if anything.
2410 int ip_natin(ip, fin)
2414 register struct in_addr src;
2415 register struct in_addr in;
2416 register ipnat_t *np;
2417 u_short sport = 0, dport = 0, *csump = NULL;
2418 u_int nflags = 0, natadd = 1, hv, msk;
2419 struct ifnet *ifp = fin->fin_ifp;
2420 tcphdr_t *tcp = NULL;
2425 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2428 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2429 if (ip->ip_p == IPPROTO_TCP)
2431 else if (ip->ip_p == IPPROTO_UDP)
2433 if ((nflags & IPN_TCPUDP)) {
2434 tcp = (tcphdr_t *)fin->fin_dp;
2435 dport = tcp->th_dport;
2436 sport = tcp->th_sport;
2441 /* make sure the source address is to be redirected */
2444 READ_ENTER(&ipf_nat);
2446 if ((ip->ip_p == IPPROTO_ICMP) &&
2447 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2449 else if ((fin->fin_fl & FI_FRAG) &&
2450 (nat = ipfr_nat_knownfrag(ip, fin)))
2452 else if ((nat = nat_inlookup(fin->fin_ifp, nflags, (u_int)ip->ip_p,
2453 ip->ip_src, in, (dport << 16) | sport,
2455 nflags = nat->nat_flags;
2456 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2457 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2458 nat->nat_oport = sport;
2459 else if ((nat->nat_outport != dport) &&
2460 (nflags & FI_W_SPORT))
2461 nat->nat_outport = dport;
2462 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2463 nflags = nat->nat_flags;
2464 nat_stats.ns_wilds--;
2467 RWLOCK_EXIT(&ipf_nat);
2468 WRITE_ENTER(&ipf_nat);
2470 * If there is no current entry in the nat table for this IP#,
2471 * create one for it (if there is a matching rule).
2476 iph = in.s_addr & htonl(msk);
2477 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2478 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2479 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2480 (np->in_p && (np->in_p != ip->ip_p)) ||
2481 (np->in_flags && !(nflags & np->in_flags)))
2483 if (np->in_flags & IPN_FILTER) {
2484 if (!nat_match(fin, np, ip))
2486 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2488 if ((np->in_redir & NAT_REDIRECT) &&
2489 (!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2490 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2491 (ntohs(dport) >= ntohs(np->in_pmin)))))
2492 if ((nat = nat_new(np, ip, fin, nflags,
2499 if ((np == NULL) && (i > 0)) {
2503 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2507 MUTEX_DOWNGRADE(&ipf_nat);
2511 * NOTE: ipf_nat must now only be held as a read lock
2515 fin->fin_fr = nat->nat_fr;
2516 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2517 ipfr_nat_newfrag(ip, fin, 0, nat);
2518 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
2519 (tcp != NULL && sport == np->in_dport))) {
2520 i = appr_check(ip, fin, nat);
2522 RWLOCK_EXIT(&ipf_nat);
2527 MUTEX_ENTER(&nat->nat_lock);
2528 if (nflags != IPN_ICMPERR)
2529 nat->nat_age = fr_defnatage;
2531 nat->nat_bytes += ip->ip_len;
2533 MUTEX_EXIT(&nat->nat_lock);
2534 ip->ip_dst = nat->nat_inip;
2535 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2538 * Fix up checksums, not by recalculating them, but
2539 * simply computing adjustments.
2541 #if SOLARIS || defined(__sgi)
2542 if (nat->nat_dir == NAT_OUTBOUND)
2543 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2545 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2547 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2549 if ((nat->nat_inport != 0) && (nflags & IPN_TCPUDP)) {
2550 tcp->th_dport = nat->nat_inport;
2551 fin->fin_data[1] = ntohs(tcp->th_dport);
2554 if (ip->ip_p == IPPROTO_TCP) {
2555 csump = &tcp->th_sum;
2556 MUTEX_ENTER(&nat->nat_lock);
2557 fr_tcp_age(&nat->nat_age,
2558 nat->nat_tcpstate, fin, 0);
2559 if (nat->nat_age < fr_defnaticmpage)
2560 nat->nat_age = fr_defnaticmpage;
2562 else if (nat->nat_age > fr_defnatage)
2563 nat->nat_age = fr_defnatage;
2566 * Increase this because we may have
2567 * "keep state" following this too and
2568 * packet storms can occur if this is
2569 * removed too quickly.
2571 if (nat->nat_age == fr_tcpclosed)
2572 nat->nat_age = fr_tcplastack;
2573 MUTEX_EXIT(&nat->nat_lock);
2574 } else if (ip->ip_p == IPPROTO_UDP) {
2575 udphdr_t *udp = (udphdr_t *)tcp;
2578 csump = &udp->uh_sum;
2579 } else if (ip->ip_p == IPPROTO_ICMP) {
2581 nat->nat_age = fr_defnaticmpage;
2585 if (nat->nat_dir == NAT_OUTBOUND)
2586 fix_incksum(fin, csump, nat->nat_sumd[0]);
2588 fix_outcksum(fin, csump, nat->nat_sumd[0]);
2591 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2592 RWLOCK_EXIT(&ipf_nat); /* READ */
2595 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2601 * Free all memory used by NAT structures allocated at runtime.
2605 WRITE_ENTER(&ipf_nat);
2606 (void) nat_clearlist();
2607 (void) nat_flushtable();
2608 RWLOCK_EXIT(&ipf_nat);
2610 if (nat_table[0] != NULL) {
2611 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2612 nat_table[0] = NULL;
2614 if (nat_table[1] != NULL) {
2615 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2616 nat_table[1] = NULL;
2618 if (nat_rules != NULL) {
2619 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2622 if (rdr_rules != NULL) {
2623 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2626 if (maptable != NULL) {
2627 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2634 * Slowly expire held state for NAT entries. Timeouts are set in
2635 * expectation of this being called twice per second.
2639 register struct nat *nat, **natp;
2640 #if defined(_KERNEL) && !SOLARIS
2645 WRITE_ENTER(&ipf_nat);
2646 for (natp = &nat_instances; (nat = *natp); ) {
2649 natp = &nat->nat_next;
2652 *natp = nat->nat_next;
2654 nat_log(nat, NL_EXPIRE);
2657 nat_stats.ns_expire++;
2659 RWLOCK_EXIT(&ipf_nat);
2666 void ip_natsync(ifp)
2669 register ipnat_t *n;
2670 register nat_t *nat;
2671 register u_32_t sum1, sum2, sumd;
2675 #if defined(_KERNEL) && !SOLARIS
2680 * Change IP addresses for NAT sessions for any protocol except TCP
2681 * since it will break the TCP connection anyway.
2684 WRITE_ENTER(&ipf_nat);
2685 for (nat = nat_instances; nat; nat = nat->nat_next)
2686 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2687 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2688 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2689 ifp2 = nat->nat_ifp;
2691 * Change the map-to address to be the same as the
2694 sum1 = nat->nat_outip.s_addr;
2695 if (fr_ifpaddr(4, ifp2, &in) != -1)
2696 nat->nat_outip = in;
2697 sum2 = nat->nat_outip.s_addr;
2702 * Readjust the checksum adjustment to take into
2703 * account the new IP#.
2705 CALC_SUMD(sum1, sum2, sumd);
2706 /* XXX - dont change for TCP when solaris does
2707 * hardware checksumming.
2709 sumd += nat->nat_sumd[0];
2710 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2711 nat->nat_sumd[1] = nat->nat_sumd[0];
2714 for (n = nat_list; (n != NULL); n = n->in_next)
2715 if (n->in_ifp == ifp) {
2716 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2718 n->in_ifp = (void *)-1;
2720 RWLOCK_EXIT(&ipf_nat);
2726 void nat_log(nat, type)
2734 int rulen, types[1];
2736 natl.nl_inip = nat->nat_inip;
2737 natl.nl_outip = nat->nat_outip;
2738 natl.nl_origip = nat->nat_oip;
2739 natl.nl_bytes = nat->nat_bytes;
2740 natl.nl_pkts = nat->nat_pkts;
2741 natl.nl_origport = nat->nat_oport;
2742 natl.nl_inport = nat->nat_inport;
2743 natl.nl_outport = nat->nat_outport;
2744 natl.nl_p = nat->nat_p;
2745 natl.nl_type = type;
2748 if (nat->nat_ptr != NULL) {
2749 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2750 if (np == nat->nat_ptr) {
2751 natl.nl_rule = rulen;
2757 sizes[0] = sizeof(natl);
2760 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);