2 * Copyright (C) 1995-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * Added redirect stuff and a LOT of bug fixes. (mcn@EnGarde.com)
9 #if defined(__FreeBSD__) && defined(KERNEL) && !defined(_KERNEL)
14 # include <sys/ptimers.h>
16 #include <sys/errno.h>
17 #include <sys/types.h>
18 #include <sys/param.h>
21 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
23 # include "opt_ipfilter_log.h"
25 #if !defined(_KERNEL) && !defined(KERNEL)
30 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
31 # include <sys/filio.h>
32 # include <sys/fcntl.h>
34 # include <sys/ioctl.h>
36 #include <sys/fcntl.h>
38 # include <sys/protosw.h>
40 #include <sys/socket.h>
41 #if defined(_KERNEL) && !defined(linux)
42 # include <sys/systm.h>
44 #if !defined(__SVR4) && !defined(__svr4__)
46 # include <sys/mbuf.h>
49 # include <sys/filio.h>
50 # include <sys/byteorder.h>
52 # include <sys/dditypes.h>
54 # include <sys/stream.h>
55 # include <sys/kmem.h>
57 #if __FreeBSD_version >= 300000
58 # include <sys/queue.h>
61 #if __FreeBSD_version >= 300000
62 # include <net/if_var.h>
63 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
64 # include "opt_ipfilter.h"
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
76 # ifdef IFF_DRVRLOCK /* IRIX6 */
77 #include <sys/hashing.h>
78 #include <netinet/in_var.h>
84 # include <vpn/ipsec.h>
85 extern struct ifnet vpnif;
89 # include <netinet/ip_var.h>
90 # include <netinet/tcp_fsm.h>
92 #include <netinet/tcp.h>
93 #include <netinet/udp.h>
94 #include <netinet/ip_icmp.h>
95 #include "netinet/ip_compat.h"
96 #include <netinet/tcpip.h>
97 #include "netinet/ip_fil.h"
98 #include "netinet/ip_nat.h"
99 #include "netinet/ip_frag.h"
100 #include "netinet/ip_state.h"
101 #include "netinet/ip_proxy.h"
102 #if (__FreeBSD_version >= 300000)
103 # include <sys/malloc.h>
106 # define MIN(a,b) (((a)<(b))?(a):(b))
109 #define SOCKADDR_IN struct sockaddr_in
112 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
113 /* static const char rcsid[] = "@(#)$Id: ip_nat.c,v 2.37.2.44 2001/07/21 07:17:22 darrenr Exp $"; */
114 static const char rcsid[] = "@(#)$FreeBSD$";
117 nat_t **nat_table[2] = { NULL, NULL },
118 *nat_instances = NULL;
119 ipnat_t *nat_list = NULL;
120 u_int ipf_nattable_sz = NAT_TABLE_SZ;
121 u_int ipf_natrules_sz = NAT_SIZE;
122 u_int ipf_rdrrules_sz = RDR_SIZE;
123 u_int ipf_hostmap_sz = HOSTMAP_SIZE;
124 u_32_t nat_masks = 0;
125 u_32_t rdr_masks = 0;
126 ipnat_t **nat_rules = NULL;
127 ipnat_t **rdr_rules = NULL;
128 hostmap_t **maptable = NULL;
130 u_long fr_defnatage = DEF_NAT_AGE,
131 fr_defnaticmpage = 6; /* 3 seconds */
134 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
135 extern kmutex_t ipf_rw;
136 extern KRWLOCK_T ipf_nat;
139 static int nat_flushtable __P((void));
140 static void nat_addnat __P((struct ipnat *));
141 static void nat_addrdr __P((struct ipnat *));
142 static void nat_delete __P((struct nat *));
143 static void nat_delrdr __P((struct ipnat *));
144 static void nat_delnat __P((struct ipnat *));
145 static int fr_natgetent __P((caddr_t));
146 static int fr_natgetsz __P((caddr_t));
147 static int fr_natputent __P((caddr_t));
148 static void nat_tabmove __P((fr_info_t *, nat_t *));
149 static int nat_match __P((fr_info_t *, ipnat_t *, ip_t *));
150 static hostmap_t *nat_hostmap __P((ipnat_t *, struct in_addr,
152 static void nat_hostmapdel __P((struct hostmap *));
157 KMALLOCS(nat_table[0], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
158 if (nat_table[0] != NULL)
159 bzero((char *)nat_table[0], ipf_nattable_sz * sizeof(nat_t *));
163 KMALLOCS(nat_table[1], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
164 if (nat_table[1] != NULL)
165 bzero((char *)nat_table[1], ipf_nattable_sz * sizeof(nat_t *));
169 KMALLOCS(nat_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_natrules_sz);
170 if (nat_rules != NULL)
171 bzero((char *)nat_rules, ipf_natrules_sz * sizeof(ipnat_t *));
175 KMALLOCS(rdr_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_rdrrules_sz);
176 if (rdr_rules != NULL)
177 bzero((char *)rdr_rules, ipf_rdrrules_sz * sizeof(ipnat_t *));
181 KMALLOCS(maptable, hostmap_t **, sizeof(hostmap_t *) * ipf_hostmap_sz);
182 if (maptable != NULL)
183 bzero((char *)maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
190 static void nat_addrdr(n)
198 k = countbits(n->in_outmsk);
199 if ((k >= 0) && (k != 32))
201 j = (n->in_outip & n->in_outmsk);
202 hv = NAT_HASH_FN(j, 0, ipf_rdrrules_sz);
205 np = &(*np)->in_rnext;
212 static void nat_addnat(n)
220 k = countbits(n->in_inmsk);
221 if ((k >= 0) && (k != 32))
223 j = (n->in_inip & n->in_inmsk);
224 hv = NAT_HASH_FN(j, 0, ipf_natrules_sz);
227 np = &(*np)->in_mnext;
234 static void nat_delrdr(n)
238 n->in_rnext->in_prnext = n->in_prnext;
239 *n->in_prnext = n->in_rnext;
243 static void nat_delnat(n)
247 n->in_mnext->in_pmnext = n->in_pmnext;
248 *n->in_pmnext = n->in_mnext;
253 * check if an ip address has already been allocated for a given mapping that
254 * is not doing port based translation.
256 * Must be called with ipf_nat held as a write lock.
258 static struct hostmap *nat_hostmap(np, real, map)
266 hv = real.s_addr % HOSTMAP_SIZE;
267 for (hm = maptable[hv]; hm; hm = hm->hm_next)
268 if ((hm->hm_realip.s_addr == real.s_addr) &&
269 (np == hm->hm_ipnat)) {
274 KMALLOC(hm, hostmap_t *);
276 hm->hm_next = maptable[hv];
277 hm->hm_pnext = maptable + hv;
279 maptable[hv]->hm_pnext = &hm->hm_next;
282 hm->hm_realip = real;
291 * Must be called with ipf_nat held as a write lock.
293 static void nat_hostmapdel(hm)
296 ATOMIC_DEC32(hm->hm_ref);
297 if (hm->hm_ref == 0) {
299 hm->hm_next->hm_pnext = hm->hm_pnext;
300 *hm->hm_pnext = hm->hm_next;
306 void fix_outcksum(fin, sp, n)
311 register u_short sumshort;
312 register u_32_t sum1;
316 else if (n & NAT_HW_CKSUM) {
319 n = (n & 0xffff) + (n >> 16);
323 sum1 = (~ntohs(*sp)) & 0xffff;
325 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
327 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
328 sumshort = ~(u_short)sum1;
329 *(sp) = htons(sumshort);
333 void fix_incksum(fin, sp, n)
338 register u_short sumshort;
339 register u_32_t sum1;
343 else if (n & NAT_HW_CKSUM) {
346 n = (n & 0xffff) + (n >> 16);
351 sum1 = (~(*sp)) & 0xffff;
353 sum1 = (~ntohs(*sp)) & 0xffff;
355 sum1 += ~(n) & 0xffff;
356 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
358 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
359 sumshort = ~(u_short)sum1;
360 *(sp) = htons(sumshort);
365 * fix_datacksum is used *only* for the adjustments of checksums in the data
366 * section of an IP packet.
368 * The only situation in which you need to do this is when NAT'ing an
369 * ICMP error message. Such a message, contains in its body the IP header
370 * of the original IP packet, that causes the error.
372 * You can't use fix_incksum or fix_outcksum in that case, because for the
373 * kernel the data section of the ICMP error is just data, and no special
374 * processing like hardware cksum or ntohs processing have been done by the
375 * kernel on the data section.
377 void fix_datacksum(sp, n)
381 register u_short sumshort;
382 register u_32_t sum1;
387 sum1 = (~ntohs(*sp)) & 0xffff;
389 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
391 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
392 sumshort = ~(u_short)sum1;
393 *(sp) = htons(sumshort);
397 * How the NAT is organised and works.
399 * Inside (interface y) NAT Outside (interface x)
400 * -------------------- -+- -------------------------------------
401 * Packet going | out, processsed by ip_natout() for x
402 * ------------> | ------------>
403 * src=10.1.1.1 | src=192.1.1.1
405 * | in, processed by ip_natin() for x
406 * <------------ | <------------
407 * dst=10.1.1.1 | dst=192.1.1.1
408 * -------------------- -+- -------------------------------------
409 * ip_natout() - changes ip_src and if required, sport
410 * - creates a new mapping, if required.
411 * ip_natin() - changes ip_dst and if required, dport
413 * In the NAT table, internal source is recorded as "in" and externally
418 * Handle ioctls which manipulate the NAT.
420 int nat_ioctl(data, cmd, mode)
421 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
429 register ipnat_t *nat, *nt, *n = NULL, **np = NULL;
430 int error = 0, ret, arg, getlock;
434 #if (BSD >= 199306) && defined(_KERNEL)
435 if ((securelevel >= 3) && (mode & FWRITE))
439 nat = NULL; /* XXX gcc -Wuninitialized */
440 KMALLOC(nt, ipnat_t *);
441 getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
442 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
443 if (mode & NAT_SYSSPACE) {
444 bcopy(data, (char *)&natd, sizeof(natd));
447 error = IRCOPYPTR(data, (char *)&natd, sizeof(natd));
449 } else if (cmd == SIOCIPFFL) { /* SIOCFLNAT & SIOCCNATL */
450 error = IRCOPY(data, (char *)&arg, sizeof(arg));
459 * For add/delete, look to see if the NAT entry is already present
462 WRITE_ENTER(&ipf_nat);
463 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
465 nat->in_flags &= IPN_USERFLAGS;
466 if ((nat->in_redir & NAT_MAPBLK) == 0) {
467 if ((nat->in_flags & IPN_SPLIT) == 0)
468 nat->in_inip &= nat->in_inmsk;
469 if ((nat->in_flags & IPN_IPRANGE) == 0)
470 nat->in_outip &= nat->in_outmsk;
472 for (np = &nat_list; (n = *np); np = &n->in_next)
473 if (!bcmp((char *)&nat->in_flags, (char *)&n->in_flags,
485 if (!(mode & FWRITE))
488 tmp = ipflog_clear(IPL_LOGNAT);
489 IWCOPY((char *)&tmp, (char *)data, sizeof(tmp));
495 if (!(mode & FWRITE)) {
509 bcopy((char *)nat, (char *)n, sizeof(*n));
510 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
512 n->in_ifp = (void *)-1;
513 if (n->in_plabel[0] != '\0') {
514 n->in_apr = appr_lookup(n->in_p, n->in_plabel);
523 if (n->in_redir & NAT_REDIRECT) {
524 n->in_flags &= ~IPN_NOTDST;
527 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
528 n->in_flags &= ~IPN_NOTSRC;
533 if (n->in_redir & NAT_MAPBLK)
534 n->in_space = USABLE_PORTS * ~ntohl(n->in_outmsk);
535 else if (n->in_flags & IPN_AUTOPORTMAP)
536 n->in_space = USABLE_PORTS * ~ntohl(n->in_inmsk);
537 else if (n->in_flags & IPN_IPRANGE)
538 n->in_space = ntohl(n->in_outmsk) - ntohl(n->in_outip);
539 else if (n->in_flags & IPN_SPLIT)
542 n->in_space = ~ntohl(n->in_outmsk);
544 * Calculate the number of valid IP addresses in the output
545 * mapping range. In all cases, the range is inclusive of
546 * the start and ending IP addresses.
547 * If to a CIDR address, lose 2: broadcast + network address
549 * If to a range, add one.
550 * If to a single IP address, set to 1.
553 if ((n->in_flags & IPN_IPRANGE) != 0)
559 if ((n->in_outmsk != 0xffffffff) && (n->in_outmsk != 0) &&
560 ((n->in_flags & (IPN_IPRANGE|IPN_SPLIT)) == 0))
561 n->in_nip = ntohl(n->in_outip) + 1;
562 else if ((n->in_flags & IPN_SPLIT) &&
563 (n->in_redir & NAT_REDIRECT))
564 n->in_nip = ntohl(n->in_inip);
566 n->in_nip = ntohl(n->in_outip);
567 if (n->in_redir & NAT_MAP) {
568 n->in_pnext = ntohs(n->in_pmin);
570 * Multiply by the number of ports made available.
572 if (ntohs(n->in_pmax) >= ntohs(n->in_pmin)) {
573 n->in_space *= (ntohs(n->in_pmax) -
574 ntohs(n->in_pmin) + 1);
576 * Because two different sources can map to
577 * different destinations but use the same
579 * If the result is smaller than in_space, then
580 * we may have wrapped around 32bits.
583 if ((i != 0) && (i != 0xffffffff)) {
584 j = n->in_space * (~ntohl(i) + 1);
585 if (j >= n->in_space)
588 n->in_space = 0xffffffff;
592 * If no protocol is specified, multiple by 256.
594 if ((n->in_flags & IPN_TCPUDP) == 0) {
595 j = n->in_space * 256;
596 if (j >= n->in_space)
599 n->in_space = 0xffffffff;
602 /* Otherwise, these fields are preset */
604 nat_stats.ns_rules++;
607 if (!(mode & FWRITE)) {
616 if (n->in_redir & NAT_REDIRECT)
618 if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
620 if (nat_list == NULL) {
627 appr_free(n->in_apr);
629 nat_stats.ns_rules--;
631 n->in_flags |= IPN_DELETE;
637 MUTEX_DOWNGRADE(&ipf_nat);
638 nat_stats.ns_table[0] = nat_table[0];
639 nat_stats.ns_table[1] = nat_table[1];
640 nat_stats.ns_list = nat_list;
641 nat_stats.ns_maptable = maptable;
642 nat_stats.ns_nattab_sz = ipf_nattable_sz;
643 nat_stats.ns_rultab_sz = ipf_natrules_sz;
644 nat_stats.ns_rdrtab_sz = ipf_rdrrules_sz;
645 nat_stats.ns_hostmap_sz = ipf_hostmap_sz;
646 nat_stats.ns_instances = nat_instances;
647 nat_stats.ns_apslist = ap_sess_list;
648 error = IWCOPYPTR((char *)&nat_stats, (char *)data,
655 MUTEX_DOWNGRADE(&ipf_nat);
656 error = IRCOPYPTR((char *)data, (char *)&nl, sizeof(nl));
660 if (nat_lookupredir(&nl)) {
661 error = IWCOPYPTR((char *)&nl, (char *)data,
667 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
668 if (!(mode & FWRITE)) {
674 ret = nat_flushtable();
676 ret = nat_clearlist();
679 MUTEX_DOWNGRADE(&ipf_nat);
681 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
687 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
689 error = IWCOPY((caddr_t)&fr_nat_lock, data,
690 sizeof(fr_nat_lock));
698 error = fr_natputent(data);
704 error = fr_natgetsz(data);
710 error = fr_natgetent(data);
716 arg = (int)iplused[IPL_LOGNAT];
717 MUTEX_DOWNGRADE(&ipf_nat);
718 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
728 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
736 static int fr_natgetsz(data)
744 error = IRCOPY(data, (caddr_t)&ng, sizeof(ng));
753 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
760 * Make sure the pointer we're copying from exists in the
761 * current list of entries. Security precaution to prevent
762 * copying of random kernel data.
764 for (n = nat_instances; n; n = n->nat_next)
771 ng.ng_sz = sizeof(nat_save_t);
773 if ((aps != NULL) && (aps->aps_data != 0)) {
774 ng.ng_sz += sizeof(ap_session_t);
775 ng.ng_sz += aps->aps_psiz;
778 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
785 static int fr_natgetent(data)
788 nat_save_t ipn, *ipnp, *ipnn = NULL;
789 register nat_t *n, *nat;
793 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
796 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
804 if (nat_instances == NULL)
810 * Make sure the pointer we're copying from exists in the
811 * current list of entries. Security precaution to prevent
812 * copying of random kernel data.
814 for (n = nat_instances; n; n = n->nat_next)
821 ipn.ipn_next = nat->nat_next;
823 bcopy((char *)nat, (char *)&ipn.ipn_nat, sizeof(ipn.ipn_nat));
824 ipn.ipn_nat.nat_data = NULL;
827 bcopy((char *)nat->nat_ptr, (char *)&ipn.ipn_ipnat,
828 sizeof(ipn.ipn_ipnat));
832 bcopy((char *)nat->nat_fr, (char *)&ipn.ipn_rule,
833 sizeof(ipn.ipn_rule));
835 if ((aps = nat->nat_aps)) {
836 ipn.ipn_dsize = sizeof(*aps);
838 ipn.ipn_dsize += aps->aps_psiz;
839 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + ipn.ipn_dsize);
842 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
844 bcopy((char *)aps, (char *)ipnn->ipn_data, sizeof(*aps));
846 bcopy(aps->aps_data, ipnn->ipn_data + sizeof(*aps),
848 ipnn->ipn_dsize += aps->aps_psiz;
850 error = IWCOPY((caddr_t)ipnn, ipnp,
851 sizeof(ipn) + ipn.ipn_dsize);
854 KFREES(ipnn, sizeof(*ipnn) + ipn.ipn_dsize);
856 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
864 static int fr_natputent(data)
867 nat_save_t ipn, *ipnp, *ipnn = NULL;
868 register nat_t *n, *nat;
875 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
878 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
883 KMALLOCS(ipnn, nat_save_t *, sizeof(ipn) + ipn.ipn_dsize);
886 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
887 error = IRCOPY((caddr_t)ipnp, (caddr_t)ipn.ipn_data,
896 KMALLOC(nat, nat_t *);
902 bcopy((char *)&ipn.ipn_nat, (char *)nat, sizeof(*nat));
904 * Initialize all these so that nat_delete() doesn't cause a crash.
906 nat->nat_phnext[0] = NULL;
907 nat->nat_phnext[1] = NULL;
915 nat->nat_data = NULL;
916 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
919 * Restore the rule associated with this nat session
922 KMALLOC(in, ipnat_t *);
928 bcopy((char *)&ipn.ipn_ipnat, (char *)in, sizeof(*in));
930 in->in_flags |= IPN_DELETE;
933 in->in_prnext = NULL;
935 in->in_pmnext = NULL;
936 in->in_ifp = GETUNIT(in->in_ifname, 4);
937 if (in->in_plabel[0] != '\0') {
938 in->in_apr = appr_lookup(in->in_p, in->in_plabel);
943 * Restore ap_session_t structure. Include the private data allocated
947 KMALLOC(aps, ap_session_t *);
953 aps->aps_next = ap_sess_list;
955 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
957 aps->aps_apr = in->in_apr;
959 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
960 if (aps->aps_data == NULL) {
964 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
968 aps->aps_data = NULL;
973 * If there was a filtering rule associated with this entry then
974 * build up a new one.
977 if (nat->nat_flags & FI_NEWFR) {
978 KMALLOC(fr, frentry_t *);
984 bcopy((char *)&ipn.ipn_fr, (char *)fr, sizeof(*fr));
985 ipn.ipn_nat.nat_fr = fr;
986 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
992 for (n = nat_instances; n; n = n->nat_next)
1003 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1008 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1016 * Delete a nat entry from the various lists and table.
1018 static void nat_delete(natd)
1023 if (natd->nat_flags & FI_WILDP)
1024 nat_stats.ns_wilds--;
1025 if (natd->nat_hnext[0])
1026 natd->nat_hnext[0]->nat_phnext[0] = natd->nat_phnext[0];
1027 *natd->nat_phnext[0] = natd->nat_hnext[0];
1028 if (natd->nat_hnext[1])
1029 natd->nat_hnext[1]->nat_phnext[1] = natd->nat_phnext[1];
1030 *natd->nat_phnext[1] = natd->nat_hnext[1];
1031 if (natd->nat_me != NULL)
1032 *natd->nat_me = NULL;
1034 if (natd->nat_fr != NULL) {
1035 ATOMIC_DEC32(natd->nat_fr->fr_ref);
1038 if (natd->nat_hm != NULL)
1039 nat_hostmapdel(natd->nat_hm);
1042 * If there is an active reference from the nat entry to its parent
1043 * rule, decrement the rule's reference count and free it too if no
1044 * longer being used.
1046 ipn = natd->nat_ptr;
1050 if (!ipn->in_use && (ipn->in_flags & IPN_DELETE)) {
1052 appr_free(ipn->in_apr);
1054 nat_stats.ns_rules--;
1058 MUTEX_DESTROY(&natd->nat_lock);
1060 * If there's a fragment table entry too for this nat entry, then
1061 * dereference that as well.
1063 ipfr_forget((void *)natd);
1064 aps_free(natd->nat_aps);
1065 nat_stats.ns_inuse--;
1071 * nat_flushtable - clear the NAT table of all mapping entries.
1072 * (this is for the dynamic mappings)
1074 static int nat_flushtable()
1076 register nat_t *nat, **natp;
1080 * ALL NAT mappings deleted, so lets just make the deletions
1083 if (nat_table[0] != NULL)
1084 bzero((char *)nat_table[0],
1085 sizeof(nat_table[0]) * ipf_nattable_sz);
1086 if (nat_table[1] != NULL)
1087 bzero((char *)nat_table[1],
1088 sizeof(nat_table[1]) * ipf_nattable_sz);
1090 for (natp = &nat_instances; (nat = *natp); ) {
1091 *natp = nat->nat_next;
1093 nat_log(nat, NL_FLUSH);
1098 nat_stats.ns_inuse = 0;
1104 * nat_clearlist - delete all rules in the active NAT mapping list.
1105 * (this is for NAT/RDR rules)
1109 register ipnat_t *n, **np = &nat_list;
1112 if (nat_rules != NULL)
1113 bzero((char *)nat_rules, sizeof(*nat_rules) * ipf_natrules_sz);
1114 if (rdr_rules != NULL)
1115 bzero((char *)rdr_rules, sizeof(*rdr_rules) * ipf_rdrrules_sz);
1121 appr_free(n->in_apr);
1123 nat_stats.ns_rules--;
1125 n->in_flags |= IPN_DELETE;
1137 * Create a new NAT table entry.
1138 * NOTE: Assumes write lock on ipf_nat has been obtained already.
1139 * If you intend on changing this, beware: appr_new() may call nat_new()
1142 nat_t *nat_new(fin, ip, np, natsave, flags, direction)
1150 register u_32_t sum1, sum2, sumd, l;
1151 u_short port = 0, sport = 0, dport = 0, nport = 0;
1152 struct in_addr in, inb;
1153 u_short nflags, sp, dp;
1154 tcphdr_t *tcp = NULL;
1155 hostmap_t *hm = NULL;
1157 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1158 qif_t *qf = fin->fin_qif;
1161 nflags = flags & np->in_flags;
1162 if (flags & IPN_TCPUDP) {
1163 tcp = (tcphdr_t *)fin->fin_dp;
1164 sport = htons(fin->fin_data[0]);
1165 dport = htons(fin->fin_data[1]);
1168 /* Give me a new nat */
1169 KMALLOC(nat, nat_t *);
1171 nat_stats.ns_memfail++;
1175 bzero((char *)nat, sizeof(*nat));
1176 nat->nat_flags = flags;
1177 if (flags & FI_WILDP)
1178 nat_stats.ns_wilds++;
1180 * Search the current table for a match.
1182 if (direction == NAT_OUTBOUND) {
1184 * Values at which the search for a free resouce starts.
1190 * If it's an outbound packet which doesn't match any existing
1191 * record, then create a new port
1195 st_port = np->in_pnext;
1199 in.s_addr = htonl(np->in_nip);
1202 * Check to see if there is an existing NAT
1203 * setup for this IP address pair.
1205 hm = nat_hostmap(np, fin->fin_src, in);
1207 in.s_addr = hm->hm_mapip.s_addr;
1208 } else if ((l == 1) && (hm != NULL)) {
1212 in.s_addr = ntohl(in.s_addr);
1216 if ((np->in_outmsk == 0xffffffff) &&
1217 (np->in_pnext == 0)) {
1222 if (np->in_redir & NAT_MAPBLK) {
1223 if ((l >= np->in_ppip) || ((l > 0) &&
1224 !(flags & IPN_TCPUDP)))
1227 * map-block - Calculate destination address.
1229 in.s_addr = ntohl(fin->fin_saddr);
1230 in.s_addr &= ntohl(~np->in_inmsk);
1231 inb.s_addr = in.s_addr;
1232 in.s_addr /= np->in_ippip;
1233 in.s_addr &= ntohl(~np->in_outmsk);
1234 in.s_addr += ntohl(np->in_outip);
1236 * Calculate destination port.
1238 if ((flags & IPN_TCPUDP) &&
1239 (np->in_ppip != 0)) {
1240 port = ntohs(sport) + l;
1241 port %= np->in_ppip;
1242 port += np->in_ppip *
1243 (inb.s_addr % np->in_ippip);
1244 port += MAPBLK_MINPORT;
1247 } else if (!np->in_outip &&
1248 (np->in_outmsk == 0xffffffff)) {
1250 * 0/32 - use the interface's IP address.
1253 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1255 in.s_addr = ntohl(in.s_addr);
1256 } else if (!np->in_outip && !np->in_outmsk) {
1258 * 0/0 - use the original source address/port.
1262 in.s_addr = ntohl(fin->fin_saddr);
1263 } else if ((np->in_outmsk != 0xffffffff) &&
1264 (np->in_pnext == 0) &&
1265 ((l > 0) || (hm == NULL)))
1269 if ((nflags & IPN_TCPUDP) &&
1270 ((np->in_redir & NAT_MAPBLK) == 0) &&
1271 (np->in_flags & IPN_AUTOPORTMAP)) {
1272 if ((l > 0) && (l % np->in_ppip == 0)) {
1273 if (l > np->in_space) {
1275 } else if ((l > np->in_ppip) &&
1276 np->in_outmsk != 0xffffffff)
1279 if (np->in_ppip != 0) {
1280 port = ntohs(sport);
1281 port += (l % np->in_ppip);
1282 port %= np->in_ppip;
1283 port += np->in_ppip *
1284 (ntohl(fin->fin_saddr) %
1286 port += MAPBLK_MINPORT;
1289 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1290 (nflags & IPN_TCPUDP) &&
1291 (np->in_pnext != 0)) {
1292 port = htons(np->in_pnext++);
1293 if (np->in_pnext > ntohs(np->in_pmax)) {
1294 np->in_pnext = ntohs(np->in_pmin);
1295 if (np->in_outmsk != 0xffffffff)
1300 if (np->in_flags & IPN_IPRANGE) {
1301 if (np->in_nip > ntohl(np->in_outmsk))
1302 np->in_nip = ntohl(np->in_outip);
1304 if ((np->in_outmsk != 0xffffffff) &&
1305 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1306 ntohl(np->in_outip))
1307 np->in_nip = ntohl(np->in_outip) + 1;
1310 if (!port && (flags & IPN_TCPUDP))
1314 * Here we do a lookup of the connection as seen from
1315 * the outside. If an IP# pair already exists, try
1316 * again. So if you have A->B becomes C->B, you can
1317 * also have D->E become C->E but not D->B causing
1318 * another C->B. Also take protocol and ports into
1319 * account when determining whether a pre-existing
1320 * NAT setup will cause an external conflict where
1321 * this is appropriate.
1323 inb.s_addr = htonl(in.s_addr);
1324 sp = fin->fin_data[0];
1325 dp = fin->fin_data[1];
1326 fin->fin_data[0] = fin->fin_data[1];
1327 fin->fin_data[1] = htons(port);
1328 natl = nat_inlookup(fin, flags & ~FI_WILDP,
1329 (u_int)fin->fin_p, fin->fin_dst,
1331 fin->fin_data[0] = sp;
1332 fin->fin_data[1] = dp;
1335 * Has the search wrapped around and come back to the
1338 if ((natl != NULL) &&
1339 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1340 (np->in_nip != 0) && (st_ip == np->in_nip))
1343 } while (natl != NULL);
1345 if (np->in_space > 0)
1348 /* Setup the NAT table */
1349 nat->nat_inip = fin->fin_src;
1350 nat->nat_outip.s_addr = htonl(in.s_addr);
1351 nat->nat_oip = fin->fin_dst;
1352 if (nat->nat_hm == NULL)
1353 nat->nat_hm = nat_hostmap(np, fin->fin_src,
1356 sum1 = LONG_SUM(ntohl(fin->fin_saddr)) + ntohs(sport);
1357 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1359 if (flags & IPN_TCPUDP) {
1360 nat->nat_inport = sport;
1361 nat->nat_outport = port; /* sport */
1362 nat->nat_oport = dport;
1366 * Otherwise, it's an inbound packet. Most likely, we don't
1367 * want to rewrite source ports and source addresses. Instead,
1368 * we want to rewrite to a fixed internal address and fixed
1371 if (np->in_flags & IPN_SPLIT) {
1372 in.s_addr = np->in_nip;
1373 if (np->in_inip == htonl(in.s_addr))
1374 np->in_nip = ntohl(np->in_inmsk);
1376 np->in_nip = ntohl(np->in_inip);
1377 if (np->in_flags & IPN_ROUNDR) {
1383 in.s_addr = ntohl(np->in_inip);
1384 if (np->in_flags & IPN_ROUNDR) {
1393 * Whilst not optimized for the case where
1394 * pmin == pmax, the gain is not significant.
1396 if (np->in_pmin != np->in_pmax) {
1397 nport = ntohs(dport) - ntohs(np->in_pmin) +
1398 ntohs(np->in_pnext);
1399 nport = ntohs(nport);
1401 nport = np->in_pnext;
1405 * When the redirect-to address is set to 0.0.0.0, just
1406 * assume a blank `forwarding' of the packet.
1409 in.s_addr = ntohl(fin->fin_daddr);
1411 nat->nat_inip.s_addr = htonl(in.s_addr);
1412 nat->nat_outip = fin->fin_dst;
1413 nat->nat_oip = fin->fin_src;
1415 sum1 = LONG_SUM(ntohl(fin->fin_daddr)) + ntohs(dport);
1416 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1418 if (flags & IPN_TCPUDP) {
1419 nat->nat_inport = nport;
1420 nat->nat_outport = dport;
1421 nat->nat_oport = sport;
1425 CALC_SUMD(sum1, sum2, sumd);
1426 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1427 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1428 if ((flags & IPN_TCPUDP) && dohwcksum &&
1429 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1430 if (direction == NAT_OUTBOUND)
1431 sum1 = LONG_SUM(ntohl(in.s_addr));
1433 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1434 sum1 += LONG_SUM(ntohl(fin->fin_daddr));
1435 sum1 += IPPROTO_TCP;
1436 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1437 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1440 nat->nat_sumd[1] = nat->nat_sumd[0];
1442 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1443 if (direction == NAT_OUTBOUND)
1444 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1446 sum1 = LONG_SUM(ntohl(fin->fin_daddr));
1448 sum2 = LONG_SUM(in.s_addr);
1450 CALC_SUMD(sum1, sum2, sumd);
1451 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1453 nat->nat_ipsumd = nat->nat_sumd[0];
1455 in.s_addr = htonl(in.s_addr);
1457 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1459 nat->nat_me = natsave;
1460 nat->nat_dir = direction;
1461 nat->nat_ifp = fin->fin_ifp;
1463 nat->nat_p = fin->fin_p;
1466 nat->nat_fr = fin->fin_fr;
1467 if (nat->nat_fr != NULL) {
1468 ATOMIC_INC32(nat->nat_fr->fr_ref);
1470 if (direction == NAT_OUTBOUND) {
1471 if (flags & IPN_TCPUDP)
1472 tcp->th_sport = port;
1474 if (flags & IPN_TCPUDP)
1475 tcp->th_dport = nport;
1480 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
1481 (tcp != NULL && dport == np->in_dport)))
1482 (void) appr_new(fin, ip, nat);
1486 nat_log(nat, (u_int)np->in_redir);
1490 nat_stats.ns_badnat++;
1491 if ((hm = nat->nat_hm) != NULL)
1499 * Insert a NAT entry into the hash tables for searching and add it to the
1500 * list of active NAT entries. Adjust global counters when complete.
1502 void nat_insert(nat)
1508 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1510 nat->nat_age = fr_defnatage;
1511 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1512 if (nat->nat_ifname[0] !='\0') {
1513 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1516 nat->nat_next = nat_instances;
1517 nat_instances = nat;
1519 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1520 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1522 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1524 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1526 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1529 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_inip.s_addr,
1531 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_outip.s_addr,
1535 natp = &nat_table[0][hv1];
1537 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1538 nat->nat_phnext[0] = natp;
1539 nat->nat_hnext[0] = *natp;
1542 natp = &nat_table[1][hv2];
1544 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1545 nat->nat_phnext[1] = natp;
1546 nat->nat_hnext[1] = *natp;
1549 nat_stats.ns_added++;
1550 nat_stats.ns_inuse++;
1554 nat_t *nat_icmplookup(ip, fin, dir)
1560 tcphdr_t *tcp = NULL;
1562 int flags = 0, type, minlen;
1564 icmp = (icmphdr_t *)fin->fin_dp;
1566 * Does it at least have the return (basic) IP header ?
1567 * Only a basic IP header (no options) should be with an ICMP error
1570 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1572 type = icmp->icmp_type;
1574 * If it's not an error type, then return.
1576 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1577 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1578 (type != ICMP_PARAMPROB))
1581 oip = (ip_t *)((char *)fin->fin_dp + 8);
1582 minlen = (oip->ip_hl << 2);
1583 if (minlen < sizeof(ip_t))
1585 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1588 * Is the buffer big enough for all of it ? It's the size of the IP
1589 * header claimed in the encapsulated part which is of concern. It
1590 * may be too big to be in this buffer but not so big that it's
1591 * outside the ICMP packet, leading to TCP deref's causing problems.
1592 * This is possible because we don't know how big oip_hl is when we
1593 * do the pullup early in fr_check() and thus can't gaurantee it is
1602 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1605 m = *(mb_t **)fin->fin_mp;
1606 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1607 (char *)ip + m->m_len)
1613 if (oip->ip_p == IPPROTO_TCP)
1615 else if (oip->ip_p == IPPROTO_UDP)
1617 if (flags & IPN_TCPUDP) {
1621 minlen += 8; /* + 64bits of data to get ports */
1622 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1625 data[0] = fin->fin_data[0];
1626 data[1] = fin->fin_data[1];
1627 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1628 fin->fin_data[0] = ntohs(tcp->th_dport);
1629 fin->fin_data[1] = ntohs(tcp->th_sport);
1631 if (dir == NAT_INBOUND) {
1632 nat = nat_inlookup(fin, flags, (u_int)oip->ip_p,
1633 oip->ip_dst, oip->ip_src, 0);
1635 nat = nat_outlookup(fin, flags, (u_int)oip->ip_p,
1636 oip->ip_dst, oip->ip_src, 0);
1638 fin->fin_data[0] = data[0];
1639 fin->fin_data[1] = data[1];
1642 if (dir == NAT_INBOUND)
1643 return nat_inlookup(fin, 0, (u_int)oip->ip_p,
1644 oip->ip_dst, oip->ip_src, 0);
1646 return nat_outlookup(fin, 0, (u_int)oip->ip_p,
1647 oip->ip_dst, oip->ip_src, 0);
1652 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1653 * packet gets correctly recognised.
1655 nat_t *nat_icmp(ip, fin, nflags, dir)
1661 u_32_t sum1, sum2, sumd, sumd2 = 0;
1670 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1673 * nat_icmplookup() will return NULL for `defective' packets.
1675 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1679 *nflags = IPN_ICMPERR;
1680 icmp = (icmphdr_t *)fin->fin_dp;
1681 oip = (ip_t *)&icmp->icmp_ip;
1682 if (oip->ip_p == IPPROTO_TCP)
1684 else if (oip->ip_p == IPPROTO_UDP)
1686 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1687 dlen = ip->ip_len - ((char *)udp - (char *)ip);
1689 * XXX - what if this is bogus hl and we go off the end ?
1690 * In this case, nat_icmplookup() will have returned NULL.
1692 tcp = (tcphdr_t *)udp;
1695 * Need to adjust ICMP header to include the real IP#'s and
1696 * port #'s. Only apply a checksum change relative to the
1697 * IP address change as it will be modified again in ip_natout
1698 * for both address and port. Two checksum changes are
1699 * necessary for the two header address changes. Be careful
1700 * to only modify the checksum once for the port # and twice
1706 * Fix the IP addresses in the offending IP packet. You also need
1707 * to adjust the IP header checksum of that offending IP packet
1708 * and the ICMP checksum of the ICMP error message itself.
1710 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1711 * in the pseudo header that is used to compute the UDP resp. TCP
1712 * checksum. So, we must compensate that as well. Even worse, the
1713 * change in the UDP and TCP checksums require yet another
1714 * adjustment of the ICMP checksum of the ICMP error message.
1718 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1719 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1723 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1724 in = nat->nat_outip;
1728 sum2 = LONG_SUM(ntohl(in.s_addr));
1730 CALC_SUMD(sum1, sum2, sumd);
1732 if (nat->nat_dir == NAT_OUTBOUND) {
1734 * Fix IP checksum of the offending IP packet to adjust for
1735 * the change in the IP address.
1737 * Normally, you would expect that the ICMP checksum of the
1738 * ICMP error message needs to be adjusted as well for the
1739 * IP address change in oip.
1740 * However, this is a NOP, because the ICMP checksum is
1741 * calculated over the complete ICMP packet, which includes the
1742 * changed oip IP addresses and oip->ip_sum. However, these
1743 * two changes cancel each other out (if the delta for
1744 * the IP address is x, then the delta for ip_sum is minus x),
1745 * so no change in the icmp_cksum is necessary.
1747 * Be careful that nat_dir refers to the direction of the
1748 * offending IP packet (oip), not to its ICMP response (icmp)
1750 fix_datacksum(&oip->ip_sum, sumd);
1753 * Fix UDP pseudo header checksum to compensate for the
1754 * IP address change.
1756 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1758 * The UDP checksum is optional, only adjust it
1759 * if it has been set.
1761 sum1 = ntohs(udp->uh_sum);
1762 fix_datacksum(&udp->uh_sum, sumd);
1763 sum2 = ntohs(udp->uh_sum);
1766 * Fix ICMP checksum to compensate the UDP
1767 * checksum adjustment.
1769 CALC_SUMD(sum1, sum2, sumd);
1775 * Fix TCP pseudo header checksum to compensate for the
1776 * IP address change. Before we can do the change, we
1777 * must make sure that oip is sufficient large to hold
1778 * the TCP checksum (normally it does not!).
1780 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1782 sum1 = ntohs(tcp->th_sum);
1783 fix_datacksum(&tcp->th_sum, sumd);
1784 sum2 = ntohs(tcp->th_sum);
1787 * Fix ICMP checksum to compensate the TCP
1788 * checksum adjustment.
1790 CALC_SUMD(sum1, sum2, sumd);
1797 * Fix IP checksum of the offending IP packet to adjust for
1798 * the change in the IP address.
1800 * Normally, you would expect that the ICMP checksum of the
1801 * ICMP error message needs to be adjusted as well for the
1802 * IP address change in oip.
1803 * However, this is a NOP, because the ICMP checksum is
1804 * calculated over the complete ICMP packet, which includes the
1805 * changed oip IP addresses and oip->ip_sum. However, these
1806 * two changes cancel each other out (if the delta for
1807 * the IP address is x, then the delta for ip_sum is minus x),
1808 * so no change in the icmp_cksum is necessary.
1810 * Be careful that nat_dir refers to the direction of the
1811 * offending IP packet (oip), not to its ICMP response (icmp)
1813 fix_datacksum(&oip->ip_sum, sumd);
1815 /* XXX FV : without having looked at Solaris source code, it seems unlikely
1816 * that SOLARIS would compensate this in the kernel (a body of an IP packet
1817 * in the data section of an ICMP packet). I have the feeling that this should
1818 * be unconditional, but I'm not in a position to check.
1820 #if !SOLARIS && !defined(__sgi)
1822 * Fix UDP pseudo header checksum to compensate for the
1823 * IP address change.
1825 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1827 * The UDP checksum is optional, only adjust it
1828 * if it has been set
1830 sum1 = ntohs(udp->uh_sum);
1831 fix_datacksum(&udp->uh_sum, sumd);
1832 sum2 = ntohs(udp->uh_sum);
1835 * Fix ICMP checksum to compensate the UDP
1836 * checksum adjustment.
1838 CALC_SUMD(sum1, sum2, sumd);
1844 * Fix TCP pseudo header checksum to compensate for the
1845 * IP address change. Before we can do the change, we
1846 * must make sure that oip is sufficient large to hold
1847 * the TCP checksum (normally it does not!).
1849 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1851 sum1 = ntohs(tcp->th_sum);
1852 fix_datacksum(&tcp->th_sum, sumd);
1853 sum2 = ntohs(tcp->th_sum);
1856 * Fix ICMP checksum to compensate the TCP
1857 * checksum adjustment.
1859 CALC_SUMD(sum1, sum2, sumd);
1867 if ((flags & IPN_TCPUDP) != 0) {
1870 * For offending TCP/UDP IP packets, translate the ports as
1871 * well, based on the NAT specification. Of course such
1872 * a change must be reflected in the ICMP checksum as well.
1874 * Advance notice : Now it becomes complicated :-)
1876 * Since the port fields are part of the TCP/UDP checksum
1877 * of the offending IP packet, you need to adjust that checksum
1878 * as well... but, if you change, you must change the icmp
1879 * checksum *again*, to reflect that change.
1881 * To further complicate: the TCP checksum is not in the first
1882 * 8 bytes of the offending ip packet, so it most likely is not
1883 * available. Some OSses like Solaris return enough bytes to
1884 * include the TCP checksum. So we have to check if the
1885 * ip->ip_len actually holds the TCP checksum of the oip!
1888 if (nat->nat_oport == tcp->th_dport) {
1889 if (tcp->th_sport != nat->nat_inport) {
1891 * Fix ICMP checksum to compensate port
1894 sum1 = ntohs(tcp->th_sport);
1895 sum2 = ntohs(nat->nat_inport);
1896 CALC_SUMD(sum1, sum2, sumd);
1898 tcp->th_sport = nat->nat_inport;
1901 * Fix udp checksum to compensate port
1902 * adjustment. NOTE : the offending IP packet
1903 * flows the other direction compared to the
1906 * The UDP checksum is optional, only adjust
1907 * it if it has been set.
1909 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1911 sum1 = ntohs(udp->uh_sum);
1912 fix_datacksum(&udp->uh_sum, sumd);
1913 sum2 = ntohs(udp->uh_sum);
1916 * Fix ICMP checksum to
1917 * compensate UDP checksum
1920 CALC_SUMD(sum1, sum2, sumd);
1925 * Fix tcp checksum (if present) to compensate
1926 * port adjustment. NOTE : the offending IP
1927 * packet flows the other direction compared to
1930 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1932 sum1 = ntohs(tcp->th_sum);
1933 fix_datacksum(&tcp->th_sum, sumd);
1934 sum2 = ntohs(tcp->th_sum);
1937 * Fix ICMP checksum to
1938 * compensate TCP checksum
1941 CALC_SUMD(sum1, sum2, sumd);
1946 if (tcp->th_dport != nat->nat_outport) {
1948 * Fix ICMP checksum to compensate port
1951 sum1 = ntohs(tcp->th_dport);
1952 sum2 = ntohs(nat->nat_outport);
1953 CALC_SUMD(sum1, sum2, sumd);
1955 tcp->th_dport = nat->nat_outport;
1958 * Fix udp checksum to compensate port
1959 * adjustment. NOTE : the offending IP
1960 * packet flows the other direction compared
1961 * to the ICMP message.
1963 * The UDP checksum is optional, only adjust
1964 * it if it has been set.
1966 if (oip->ip_p == IPPROTO_UDP && udp->uh_sum) {
1968 sum1 = ntohs(udp->uh_sum);
1969 fix_datacksum(&udp->uh_sum, sumd);
1970 sum2 = ntohs(udp->uh_sum);
1973 * Fix ICMP checksum to compensate
1974 * UDP checksum adjustment.
1976 CALC_SUMD(sum1, sum2, sumd);
1981 * Fix tcp checksum (if present) to compensate
1982 * port adjustment. NOTE : the offending IP
1983 * packet flows the other direction compared to
1986 if (oip->ip_p == IPPROTO_TCP && dlen >= 18) {
1988 sum1 = ntohs(tcp->th_sum);
1989 fix_datacksum(&tcp->th_sum, sumd);
1990 sum2 = ntohs(tcp->th_sum);
1993 * Fix ICMP checksum to compensate
1994 * UDP checksum adjustment.
1996 CALC_SUMD(sum1, sum2, sumd);
2002 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
2003 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
2004 if (nat->nat_dir == NAT_OUTBOUND) {
2005 fix_outcksum(fin, &icmp->icmp_cksum, sumd2);
2007 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
2011 if (oip->ip_p == IPPROTO_ICMP)
2012 nat->nat_age = fr_defnaticmpage;
2018 * NB: these lookups don't lock access to the list, it assume it has already
2022 * Lookup a nat entry based on the mapped destination ip address/port and
2023 * real source address/port. We use this lookup when receiving a packet,
2024 * we're looking for a table entry, based on the destination address.
2025 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2027 nat_t *nat_inlookup(fin, flags, p, src, mapdst, rw)
2029 register u_int flags, p;
2030 struct in_addr src , mapdst;
2033 register u_short sport, dport;
2034 register nat_t *nat;
2035 register int nflags;
2036 register u_32_t dst;
2045 dst = mapdst.s_addr;
2046 if (flags & IPN_TCPUDP) {
2047 sport = htons(fin->fin_data[0]);
2048 dport = htons(fin->fin_data[1]);
2054 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
2055 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
2056 nat = nat_table[1][hv];
2057 for (; nat; nat = nat->nat_hnext[1]) {
2058 nflags = nat->nat_flags;
2059 if ((!ifp || ifp == nat->nat_ifp) &&
2060 nat->nat_oip.s_addr == src.s_addr &&
2061 nat->nat_outip.s_addr == dst &&
2062 ((p == 0) || (p == nat->nat_p))) {
2067 if (nat->nat_oport != sport)
2069 if (nat->nat_outport != dport)
2077 if ((ipn != NULL) && (nat->nat_aps != NULL))
2078 if (appr_match(fin, nat) != 0)
2083 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2086 RWLOCK_EXIT(&ipf_nat);
2088 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
2089 hv = NAT_HASH_FN(src.s_addr, dst, ipf_nattable_sz);
2091 WRITE_ENTER(&ipf_nat);
2093 nat = nat_table[1][hv];
2094 for (; nat; nat = nat->nat_hnext[1]) {
2095 nflags = nat->nat_flags;
2096 if (ifp && ifp != nat->nat_ifp)
2098 if (!(nflags & FI_WILDP))
2100 if (nat->nat_oip.s_addr != src.s_addr ||
2101 nat->nat_outip.s_addr != dst)
2103 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
2104 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
2105 nat_tabmove(fin, nat);
2110 MUTEX_DOWNGRADE(&ipf_nat);
2117 * This function is only called for TCP/UDP NAT table entries where the
2118 * original was placed in the table without hashing on the ports and we now
2119 * want to include hashing on port numbers.
2121 static void nat_tabmove(fin, nat)
2125 register u_short sport, dport;
2129 nflags = nat->nat_flags;
2131 sport = ntohs(fin->fin_data[0]);
2132 dport = ntohs(fin->fin_data[1]);
2135 * Remove the NAT entry from the old location
2137 if (nat->nat_hnext[0])
2138 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2139 *nat->nat_phnext[0] = nat->nat_hnext[0];
2141 if (nat->nat_hnext[1])
2142 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2143 *nat->nat_phnext[1] = nat->nat_hnext[1];
2146 * Add into the NAT table in the new position
2148 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2149 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2150 natp = &nat_table[0][hv];
2152 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2153 nat->nat_phnext[0] = natp;
2154 nat->nat_hnext[0] = *natp;
2157 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2158 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2159 natp = &nat_table[1][hv];
2161 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2162 nat->nat_phnext[1] = natp;
2163 nat->nat_hnext[1] = *natp;
2169 * Lookup a nat entry based on the source 'real' ip address/port and
2170 * destination address/port. We use this lookup when sending a packet out,
2171 * we're looking for a table entry, based on the source address.
2172 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2174 nat_t *nat_outlookup(fin, flags, p, src, dst, rw)
2176 register u_int flags, p;
2177 struct in_addr src , dst;
2180 register u_short sport, dport;
2181 register nat_t *nat;
2182 register int nflags;
2190 if (flags & IPN_TCPUDP) {
2191 sport = ntohs(fin->fin_data[0]);
2192 dport = ntohs(fin->fin_data[1]);
2198 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2199 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2200 nat = nat_table[0][hv];
2201 for (; nat; nat = nat->nat_hnext[0]) {
2202 nflags = nat->nat_flags;
2204 if ((!ifp || ifp == nat->nat_ifp) &&
2205 nat->nat_inip.s_addr == srcip &&
2206 nat->nat_oip.s_addr == dst.s_addr &&
2207 ((p == 0) || (p == nat->nat_p))) {
2212 if (nat->nat_oport != dport)
2214 if (nat->nat_inport != sport)
2222 if ((ipn != NULL) && (nat->nat_aps != NULL))
2223 if (appr_match(fin, nat) != 0)
2228 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2231 RWLOCK_EXIT(&ipf_nat);
2234 hv = NAT_HASH_FN(dst.s_addr, srcip, ipf_nattable_sz);
2236 WRITE_ENTER(&ipf_nat);
2238 nat = nat_table[0][hv];
2239 for (; nat; nat = nat->nat_hnext[0]) {
2240 nflags = nat->nat_flags;
2241 if (ifp && ifp != nat->nat_ifp)
2243 if (!(nflags & FI_WILDP))
2245 if ((nat->nat_inip.s_addr != srcip) ||
2246 (nat->nat_oip.s_addr != dst.s_addr))
2248 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2249 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2250 nat_tabmove(fin, nat);
2255 MUTEX_DOWNGRADE(&ipf_nat);
2262 * Lookup the NAT tables to search for a matching redirect
2264 nat_t *nat_lookupredir(np)
2265 register natlookup_t *np;
2270 bzero((char *)&fi, sizeof(fi));
2271 fi.fin_data[0] = np->nl_inport;
2272 fi.fin_data[1] = np->nl_outport;
2275 * If nl_inip is non null, this is a lookup based on the real
2276 * ip address. Else, we use the fake.
2278 if ((nat = nat_outlookup(&fi, np->nl_flags, 0, np->nl_inip,
2279 np->nl_outip, 0))) {
2280 np->nl_realip = nat->nat_outip;
2281 np->nl_realport = nat->nat_outport;
2287 static int nat_match(fin, np, ip)
2297 if (np->in_p && fin->fin_p != np->in_p)
2300 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2302 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2303 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2305 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2306 ^ ((np->in_flags & IPN_NOTDST) != 0))
2309 if (!(np->in_redir & NAT_REDIRECT))
2311 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2312 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2314 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2315 ^ ((np->in_flags & IPN_NOTDST) != 0))
2320 if (!(fin->fin_fl & FI_TCPUDP) ||
2321 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2322 if (ft->ftu_scmp || ft->ftu_dcmp)
2327 return fr_tcpudpchk(ft, fin);
2332 * Packets going out on the external interface go through this.
2333 * Here, the source address requires alteration, if anything.
2335 int ip_natout(ip, fin)
2339 register ipnat_t *np = NULL;
2340 register u_32_t ipa;
2341 tcphdr_t *tcp = NULL;
2342 u_short sport = 0, dport = 0, *csump = NULL;
2343 int natadd = 1, i, icmpset = 1;
2344 u_int nflags = 0, hv, msk;
2351 if (nat_list == NULL || (fr_nat_lock))
2354 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2355 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1) {
2356 sifp = fin->fin_ifp;
2357 fin->fin_ifp = fr->fr_tif.fd_ifp;
2359 sifp = fin->fin_ifp;
2362 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2363 if (fin->fin_p == IPPROTO_TCP)
2365 else if (fin->fin_p == IPPROTO_UDP)
2367 if ((nflags & IPN_TCPUDP)) {
2368 tcp = (tcphdr_t *)fin->fin_dp;
2369 sport = tcp->th_sport;
2370 dport = tcp->th_dport;
2374 ipa = fin->fin_saddr;
2376 READ_ENTER(&ipf_nat);
2378 if ((fin->fin_p == IPPROTO_ICMP) &&
2379 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2381 else if ((fin->fin_fl & FI_FRAG) &&
2382 (nat = ipfr_nat_knownfrag(ip, fin)))
2384 else if ((nat = nat_outlookup(fin, nflags|FI_WILDP|FI_WILDA,
2385 (u_int)fin->fin_p, fin->fin_src,
2386 fin->fin_dst, 0))) {
2387 nflags = nat->nat_flags;
2388 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2389 if ((nflags & FI_W_SPORT) &&
2390 (nat->nat_inport != sport))
2391 nat->nat_inport = sport;
2392 if ((nflags & FI_W_DPORT) &&
2393 (nat->nat_oport != dport))
2394 nat->nat_oport = dport;
2396 if (nat->nat_outport == 0)
2397 nat->nat_outport = sport;
2398 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2399 nflags = nat->nat_flags;
2400 nat_stats.ns_wilds--;
2403 RWLOCK_EXIT(&ipf_nat);
2408 WRITE_ENTER(&ipf_nat);
2410 * If there is no current entry in the nat table for this IP#,
2411 * create one for it (if there is a matching rule).
2414 iph = ipa & htonl(msk);
2415 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2416 for (np = nat_rules[hv]; np; np = np->in_mnext)
2418 if (np->in_ifp && (np->in_ifp != ifp))
2420 if ((np->in_flags & IPN_RF) &&
2421 !(np->in_flags & nflags))
2423 if (np->in_flags & IPN_FILTER) {
2424 if (!nat_match(fin, np, ip))
2426 } else if ((ipa & np->in_inmsk) != np->in_inip)
2428 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2430 nat = nat_new(fin, ip, np, NULL,
2431 (u_int)nflags, NAT_OUTBOUND);
2437 if ((np == NULL) && (i > 0)) {
2441 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2445 MUTEX_DOWNGRADE(&ipf_nat);
2449 * NOTE: ipf_nat must now only be held as a read lock
2453 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2454 ipfr_nat_newfrag(ip, fin, 0, nat);
2455 MUTEX_ENTER(&nat->nat_lock);
2456 if (fin->fin_p != IPPROTO_TCP) {
2457 if (np && np->in_age[1])
2458 nat->nat_age = np->in_age[1];
2459 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2460 nat->nat_age = fr_defnaticmpage;
2462 nat->nat_age = fr_defnatage;
2464 nat->nat_bytes += ip->ip_len;
2466 MUTEX_EXIT(&nat->nat_lock);
2469 * Fix up checksums, not by recalculating them, but
2470 * simply computing adjustments.
2472 if (nflags == IPN_ICMPERR) {
2473 u_32_t s1, s2, sumd;
2475 s1 = LONG_SUM(ntohl(fin->fin_saddr));
2476 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2477 CALC_SUMD(s1, s2, sumd);
2479 if (nat->nat_dir == NAT_OUTBOUND)
2480 fix_outcksum(fin, &ip->ip_sum, sumd);
2482 fix_incksum(fin, &ip->ip_sum, sumd);
2484 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
2486 if (nat->nat_dir == NAT_OUTBOUND)
2487 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2489 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2493 * Only change the packet contents, not what is filtered upon.
2495 ip->ip_src = nat->nat_outip;
2497 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2499 if ((nat->nat_outport != 0) && (tcp != NULL)) {
2500 tcp->th_sport = nat->nat_outport;
2501 fin->fin_data[0] = ntohs(tcp->th_sport);
2504 if (fin->fin_p == IPPROTO_TCP) {
2505 csump = &tcp->th_sum;
2506 MUTEX_ENTER(&nat->nat_lock);
2507 fr_tcp_age(&nat->nat_age,
2508 nat->nat_tcpstate, fin, 1, 0);
2509 if (nat->nat_age < fr_defnaticmpage)
2510 nat->nat_age = fr_defnaticmpage;
2512 else if (nat->nat_age > fr_defnatage)
2513 nat->nat_age = fr_defnatage;
2516 * Increase this because we may have
2517 * "keep state" following this too and
2518 * packet storms can occur if this is
2519 * removed too quickly.
2521 if (nat->nat_age == fr_tcpclosed)
2522 nat->nat_age = fr_tcplastack;
2523 MUTEX_EXIT(&nat->nat_lock);
2524 } else if (fin->fin_p == IPPROTO_UDP) {
2525 udphdr_t *udp = (udphdr_t *)tcp;
2528 csump = &udp->uh_sum;
2532 if (nat->nat_dir == NAT_OUTBOUND)
2533 fix_outcksum(fin, csump,
2536 fix_incksum(fin, csump,
2541 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2542 (tcp != NULL && dport == np->in_dport))) {
2543 i = appr_check(ip, fin, nat);
2548 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2549 RWLOCK_EXIT(&ipf_nat); /* READ */
2550 fin->fin_ifp = sifp;
2553 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2554 fin->fin_ifp = sifp;
2560 * Packets coming in from the external interface go through this.
2561 * Here, the destination address requires alteration, if anything.
2563 int ip_natin(ip, fin)
2567 register struct in_addr src;
2568 register struct in_addr in;
2569 register ipnat_t *np;
2570 u_short sport = 0, dport = 0, *csump = NULL;
2571 u_int nflags = 0, natadd = 1, hv, msk;
2572 struct ifnet *ifp = fin->fin_ifp;
2573 tcphdr_t *tcp = NULL;
2578 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2581 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2582 if (fin->fin_p == IPPROTO_TCP)
2584 else if (fin->fin_p == IPPROTO_UDP)
2586 if ((nflags & IPN_TCPUDP)) {
2587 tcp = (tcphdr_t *)fin->fin_dp;
2588 sport = tcp->th_sport;
2589 dport = tcp->th_dport;
2594 /* make sure the source address is to be redirected */
2597 READ_ENTER(&ipf_nat);
2599 if ((fin->fin_p == IPPROTO_ICMP) &&
2600 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2602 else if ((fin->fin_fl & FI_FRAG) &&
2603 (nat = ipfr_nat_knownfrag(ip, fin)))
2605 else if ((nat = nat_inlookup(fin, nflags|FI_WILDP|FI_WILDA,
2606 (u_int)fin->fin_p, fin->fin_src, in, 0))) {
2607 nflags = nat->nat_flags;
2608 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2609 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2610 nat->nat_oport = sport;
2611 if ((nat->nat_outport != dport) &&
2612 (nflags & FI_W_SPORT))
2613 nat->nat_outport = dport;
2614 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2615 nflags = nat->nat_flags;
2616 nat_stats.ns_wilds--;
2619 RWLOCK_EXIT(&ipf_nat);
2624 WRITE_ENTER(&ipf_nat);
2626 * If there is no current entry in the nat table for this IP#,
2627 * create one for it (if there is a matching rule).
2630 iph = in.s_addr & htonl(msk);
2631 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2632 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2633 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2634 (np->in_p && (np->in_p != fin->fin_p)) ||
2635 (np->in_flags && !(nflags & np->in_flags)))
2637 if (np->in_flags & IPN_FILTER) {
2638 if (!nat_match(fin, np, ip))
2640 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2642 if ((!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2643 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2644 (ntohs(dport) >= ntohs(np->in_pmin)))))
2645 if ((nat = nat_new(fin, ip, np, NULL, nflags,
2652 if ((np == NULL) && (i > 0)) {
2656 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2660 MUTEX_DOWNGRADE(&ipf_nat);
2664 * NOTE: ipf_nat must now only be held as a read lock
2668 fin->fin_fr = nat->nat_fr;
2669 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2670 ipfr_nat_newfrag(ip, fin, 0, nat);
2671 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2672 (tcp != NULL && sport == np->in_dport))) {
2673 i = appr_check(ip, fin, nat);
2675 RWLOCK_EXIT(&ipf_nat);
2680 MUTEX_ENTER(&nat->nat_lock);
2681 if (fin->fin_p != IPPROTO_TCP) {
2682 if (np && np->in_age[0])
2683 nat->nat_age = np->in_age[0];
2684 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2685 nat->nat_age = fr_defnaticmpage;
2687 nat->nat_age = fr_defnatage;
2689 nat->nat_bytes += ip->ip_len;
2691 MUTEX_EXIT(&nat->nat_lock);
2692 ip->ip_dst = nat->nat_inip;
2693 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2696 * Fix up checksums, not by recalculating them, but
2697 * simply computing adjustments.
2699 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
2700 if (nat->nat_dir == NAT_OUTBOUND)
2701 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2703 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2705 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2707 if ((nat->nat_inport != 0) && (tcp != NULL)) {
2708 tcp->th_dport = nat->nat_inport;
2709 fin->fin_data[1] = ntohs(tcp->th_dport);
2712 if (fin->fin_p == IPPROTO_TCP) {
2713 csump = &tcp->th_sum;
2714 MUTEX_ENTER(&nat->nat_lock);
2715 fr_tcp_age(&nat->nat_age,
2716 nat->nat_tcpstate, fin, 0, 0);
2717 if (nat->nat_age < fr_defnaticmpage)
2718 nat->nat_age = fr_defnaticmpage;
2720 else if (nat->nat_age > fr_defnatage)
2721 nat->nat_age = fr_defnatage;
2724 * Increase this because we may have
2725 * "keep state" following this too and
2726 * packet storms can occur if this is
2727 * removed too quickly.
2729 if (nat->nat_age == fr_tcpclosed)
2730 nat->nat_age = fr_tcplastack;
2731 MUTEX_EXIT(&nat->nat_lock);
2732 } else if (fin->fin_p == IPPROTO_UDP) {
2733 udphdr_t *udp = (udphdr_t *)tcp;
2736 csump = &udp->uh_sum;
2740 if (nat->nat_dir == NAT_OUTBOUND)
2741 fix_incksum(fin, csump,
2744 fix_outcksum(fin, csump,
2748 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2749 RWLOCK_EXIT(&ipf_nat); /* READ */
2752 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2758 * Free all memory used by NAT structures allocated at runtime.
2762 WRITE_ENTER(&ipf_nat);
2763 (void) nat_clearlist();
2764 (void) nat_flushtable();
2765 RWLOCK_EXIT(&ipf_nat);
2767 if (nat_table[0] != NULL) {
2768 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2769 nat_table[0] = NULL;
2771 if (nat_table[1] != NULL) {
2772 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2773 nat_table[1] = NULL;
2775 if (nat_rules != NULL) {
2776 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2779 if (rdr_rules != NULL) {
2780 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2783 if (maptable != NULL) {
2784 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2791 * Slowly expire held state for NAT entries. Timeouts are set in
2792 * expectation of this being called twice per second.
2796 register struct nat *nat, **natp;
2797 #if defined(_KERNEL) && !SOLARIS
2802 WRITE_ENTER(&ipf_nat);
2803 for (natp = &nat_instances; (nat = *natp); ) {
2806 natp = &nat->nat_next;
2809 *natp = nat->nat_next;
2811 nat_log(nat, NL_EXPIRE);
2814 nat_stats.ns_expire++;
2816 RWLOCK_EXIT(&ipf_nat);
2823 void ip_natsync(ifp)
2826 register ipnat_t *n;
2827 register nat_t *nat;
2828 register u_32_t sum1, sum2, sumd;
2832 #if defined(_KERNEL) && !SOLARIS
2837 * Change IP addresses for NAT sessions for any protocol except TCP
2838 * since it will break the TCP connection anyway.
2841 WRITE_ENTER(&ipf_nat);
2842 for (nat = nat_instances; nat; nat = nat->nat_next)
2843 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2844 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2845 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2846 ifp2 = nat->nat_ifp;
2848 * Change the map-to address to be the same as the
2851 sum1 = nat->nat_outip.s_addr;
2852 if (fr_ifpaddr(4, ifp2, &in) != -1)
2853 nat->nat_outip = in;
2854 sum2 = nat->nat_outip.s_addr;
2859 * Readjust the checksum adjustment to take into
2860 * account the new IP#.
2862 CALC_SUMD(sum1, sum2, sumd);
2863 /* XXX - dont change for TCP when solaris does
2864 * hardware checksumming.
2866 sumd += nat->nat_sumd[0];
2867 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2868 nat->nat_sumd[1] = nat->nat_sumd[0];
2871 for (n = nat_list; (n != NULL); n = n->in_next)
2872 if (n->in_ifp == ifp) {
2873 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2875 n->in_ifp = (void *)-1;
2877 RWLOCK_EXIT(&ipf_nat);
2883 void nat_log(nat, type)
2891 int rulen, types[1];
2893 natl.nl_inip = nat->nat_inip;
2894 natl.nl_outip = nat->nat_outip;
2895 natl.nl_origip = nat->nat_oip;
2896 natl.nl_bytes = nat->nat_bytes;
2897 natl.nl_pkts = nat->nat_pkts;
2898 natl.nl_origport = nat->nat_oport;
2899 natl.nl_inport = nat->nat_inport;
2900 natl.nl_outport = nat->nat_outport;
2901 natl.nl_p = nat->nat_p;
2902 natl.nl_type = type;
2905 if (nat->nat_ptr != NULL) {
2906 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2907 if (np == nat->nat_ptr) {
2908 natl.nl_rule = rulen;
2914 sizes[0] = sizeof(natl);
2917 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);
2922 #if defined(__OpenBSD__)
2923 void nat_ifdetach(ifp)