2 * Copyright (C) 1995-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * Added redirect stuff and a LOT of bug fixes. (mcn@EnGarde.com)
8 * @(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed
9 * @(#)$Id: ip_nat.c,v 2.37.2.70 2002/08/28 12:45:48 darrenr Exp $
10 * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_nat.c,v 1.22.2.8 2004/07/04 09:24:39 darrenr Exp $
11 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_nat.c,v 1.9 2005/01/07 20:15:47 dillon Exp $
13 #if (defined(__DragonFly__) || defined(__FreeBSD__)) && defined(KERNEL) && !defined(_KERNEL)
17 #if defined(__sgi) && (IRIX > 602)
18 # include <sys/ptimers.h>
20 #include <sys/errno.h>
21 #include <sys/types.h>
22 #include <sys/param.h>
25 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
27 # include "opt_ipfilter_log.h"
29 #if !defined(_KERNEL) && !defined(KERNEL)
34 #if (defined(KERNEL) || defined(_KERNEL)) && (defined(__DragonFly__) || __FreeBSD_version >= 220000)
35 # include <sys/filio.h>
36 # include <sys/fcntl.h>
38 # include <sys/ioctl.h>
40 #include <sys/fcntl.h>
42 # include <sys/protosw.h>
44 #include <sys/socket.h>
45 #if defined(_KERNEL) && !defined(linux)
46 # include <sys/systm.h>
48 #if !defined(__SVR4) && !defined(__svr4__)
50 # include <sys/mbuf.h>
53 # include <sys/filio.h>
54 # include <sys/byteorder.h>
56 # include <sys/dditypes.h>
58 # include <sys/stream.h>
59 # include <sys/kmem.h>
61 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
62 # include <sys/queue.h>
65 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
66 # include <net/if_var.h>
67 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
68 # include "opt_ipfilter.h"
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
80 # ifdef IFF_DRVRLOCK /* IRIX6 */
81 #include <sys/hashing.h>
82 #include <netinet/in_var.h>
88 # include <vpn/ipsec.h>
89 extern struct ifnet vpnif;
93 # include <netinet/ip_var.h>
94 # include <netinet/tcp_fsm.h>
96 #include <netinet/tcp.h>
97 #include <netinet/udp.h>
98 #include <netinet/ip_icmp.h>
99 #include "ip_compat.h"
100 #include <netinet/tcpip.h>
104 #include "ip_state.h"
105 #include "ip_proxy.h"
106 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
107 # include <sys/malloc.h>
110 # define MIN(a,b) (((a)<(b))?(a):(b))
113 #define SOCKADDR_IN struct sockaddr_in
115 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
117 nat_t **nat_table[2] = { NULL, NULL },
118 *nat_instances = NULL;
119 ipnat_t *nat_list = NULL;
120 u_int ipf_nattable_max = NAT_TABLE_MAX;
121 u_int ipf_nattable_sz = NAT_TABLE_SZ;
122 u_int ipf_natrules_sz = NAT_SIZE;
123 u_int ipf_rdrrules_sz = RDR_SIZE;
124 u_int ipf_hostmap_sz = HOSTMAP_SIZE;
125 u_32_t nat_masks = 0;
126 u_32_t rdr_masks = 0;
127 ipnat_t **nat_rules = NULL;
128 ipnat_t **rdr_rules = NULL;
129 hostmap_t **maptable = NULL;
131 u_long fr_defnatage = DEF_NAT_AGE,
132 fr_defnaticmpage = 6; /* 3 seconds */
135 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
136 extern kmutex_t ipf_rw;
137 extern KRWLOCK_T ipf_nat;
140 static int nat_flushtable (void);
141 static void nat_addnat (struct ipnat *);
142 static void nat_addrdr (struct ipnat *);
143 static void nat_delete (struct nat *);
144 static void nat_delrdr (struct ipnat *);
145 static void nat_delnat (struct ipnat *);
146 static int fr_natgetent (caddr_t);
147 static int fr_natgetsz (caddr_t);
148 static int fr_natputent (caddr_t);
149 static void nat_tabmove (fr_info_t *, nat_t *);
150 static int nat_match (fr_info_t *, ipnat_t *, ip_t *);
151 static hostmap_t *nat_hostmap (ipnat_t *, struct in_addr,
153 static void nat_hostmapdel (struct hostmap *);
154 static void nat_mssclamp (tcphdr_t *, u_32_t, fr_info_t *, u_short *);
159 KMALLOCS(nat_table[0], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
160 if (nat_table[0] != NULL)
161 bzero((char *)nat_table[0], ipf_nattable_sz * sizeof(nat_t *));
165 KMALLOCS(nat_table[1], nat_t **, sizeof(nat_t *) * ipf_nattable_sz);
166 if (nat_table[1] != NULL)
167 bzero((char *)nat_table[1], ipf_nattable_sz * sizeof(nat_t *));
171 KMALLOCS(nat_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_natrules_sz);
172 if (nat_rules != NULL)
173 bzero((char *)nat_rules, ipf_natrules_sz * sizeof(ipnat_t *));
177 KMALLOCS(rdr_rules, ipnat_t **, sizeof(ipnat_t *) * ipf_rdrrules_sz);
178 if (rdr_rules != NULL)
179 bzero((char *)rdr_rules, ipf_rdrrules_sz * sizeof(ipnat_t *));
183 KMALLOCS(maptable, hostmap_t **, sizeof(hostmap_t *) * ipf_hostmap_sz);
184 if (maptable != NULL)
185 bzero((char *)maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
192 static void nat_addrdr(n)
200 k = countbits(n->in_outmsk);
201 if ((k >= 0) && (k != 32))
203 j = (n->in_outip & n->in_outmsk);
204 hv = NAT_HASH_FN(j, 0, ipf_rdrrules_sz);
207 np = &(*np)->in_rnext;
214 static void nat_addnat(n)
222 k = countbits(n->in_inmsk);
223 if ((k >= 0) && (k != 32))
225 j = (n->in_inip & n->in_inmsk);
226 hv = NAT_HASH_FN(j, 0, ipf_natrules_sz);
229 np = &(*np)->in_mnext;
236 static void nat_delrdr(n)
240 n->in_rnext->in_prnext = n->in_prnext;
241 *n->in_prnext = n->in_rnext;
245 static void nat_delnat(n)
249 n->in_mnext->in_pmnext = n->in_pmnext;
250 *n->in_pmnext = n->in_mnext;
255 * check if an ip address has already been allocated for a given mapping that
256 * is not doing port based translation.
258 * Must be called with ipf_nat held as a write lock.
260 static struct hostmap *nat_hostmap(np, real, map)
268 hv = real.s_addr % HOSTMAP_SIZE;
269 for (hm = maptable[hv]; hm; hm = hm->hm_next)
270 if ((hm->hm_realip.s_addr == real.s_addr) &&
271 (np == hm->hm_ipnat)) {
276 KMALLOC(hm, hostmap_t *);
278 hm->hm_next = maptable[hv];
279 hm->hm_pnext = maptable + hv;
281 maptable[hv]->hm_pnext = &hm->hm_next;
284 hm->hm_realip = real;
293 * Must be called with ipf_nat held as a write lock.
295 static void nat_hostmapdel(hm)
298 ATOMIC_DEC32(hm->hm_ref);
299 if (hm->hm_ref == 0) {
301 hm->hm_next->hm_pnext = hm->hm_pnext;
302 *hm->hm_pnext = hm->hm_next;
308 void fix_outcksum(fin, sp, n)
318 else if (n & NAT_HW_CKSUM) {
321 n = (n & 0xffff) + (n >> 16);
325 sum1 = (~ntohs(*sp)) & 0xffff;
327 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
329 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
330 sumshort = ~(u_short)sum1;
331 *(sp) = htons(sumshort);
335 void fix_incksum(fin, sp, n)
345 else if (n & NAT_HW_CKSUM) {
348 n = (n & 0xffff) + (n >> 16);
353 sum1 = (~(*sp)) & 0xffff;
355 sum1 = (~ntohs(*sp)) & 0xffff;
357 sum1 += ~(n) & 0xffff;
358 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
360 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
361 sumshort = ~(u_short)sum1;
362 *(sp) = htons(sumshort);
367 * fix_datacksum is used *only* for the adjustments of checksums in the data
368 * section of an IP packet.
370 * The only situation in which you need to do this is when NAT'ing an
371 * ICMP error message. Such a message, contains in its body the IP header
372 * of the original IP packet, that causes the error.
374 * You can't use fix_incksum or fix_outcksum in that case, because for the
375 * kernel the data section of the ICMP error is just data, and no special
376 * processing like hardware cksum or ntohs processing have been done by the
377 * kernel on the data section.
379 void fix_datacksum(sp, n)
389 sum1 = (~ntohs(*sp)) & 0xffff;
391 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
393 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
394 sumshort = ~(u_short)sum1;
395 *(sp) = htons(sumshort);
399 * How the NAT is organised and works.
401 * Inside (interface y) NAT Outside (interface x)
402 * -------------------- -+- -------------------------------------
403 * Packet going | out, processsed by ip_natout() for x
404 * ------------> | ------------>
405 * src=10.1.1.1 | src=192.1.1.1
407 * | in, processed by ip_natin() for x
408 * <------------ | <------------
409 * dst=10.1.1.1 | dst=192.1.1.1
410 * -------------------- -+- -------------------------------------
411 * ip_natout() - changes ip_src and if required, sport
412 * - creates a new mapping, if required.
413 * ip_natin() - changes ip_dst and if required, dport
415 * In the NAT table, internal source is recorded as "in" and externally
420 * Handle ioctls which manipulate the NAT.
422 int nat_ioctl(data, cmd, mode)
423 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
431 ipnat_t *nat, *nt, *n = NULL, **np = NULL;
432 int error = 0, ret, arg, getlock;
436 #if (BSD >= 199306) && defined(_KERNEL)
437 if ((securelevel >= 3) && (mode & FWRITE))
441 nat = NULL; /* XXX gcc -Wuninitialized */
442 KMALLOC(nt, ipnat_t *);
443 getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
444 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
445 if (mode & NAT_SYSSPACE) {
446 bcopy(data, (char *)&natd, sizeof(natd));
449 error = IRCOPYPTR(data, (char *)&natd, sizeof(natd));
451 } else if (cmd == SIOCIPFFL) { /* SIOCFLNAT & SIOCCNATL */
452 error = IRCOPY(data, (char *)&arg, sizeof(arg));
461 * For add/delete, look to see if the NAT entry is already present
464 WRITE_ENTER(&ipf_nat);
466 if ((cmd == SIOCADNAT) || (cmd == SIOCRMNAT)) {
468 nat->in_flags &= IPN_USERFLAGS;
469 if ((nat->in_redir & NAT_MAPBLK) == 0) {
470 if ((nat->in_flags & IPN_SPLIT) == 0)
471 nat->in_inip &= nat->in_inmsk;
472 if ((nat->in_flags & IPN_IPRANGE) == 0)
473 nat->in_outip &= nat->in_outmsk;
475 for (np = &nat_list; (n = *np); np = &n->in_next)
476 if (!bcmp((char *)&nat->in_flags, (char *)&n->in_flags,
478 if (n->in_redir == NAT_REDIRECT &&
479 n->in_pnext != nat->in_pnext)
492 if (!(mode & FWRITE))
495 tmp = ipflog_clear(IPL_LOGNAT);
496 IWCOPY((char *)&tmp, (char *)data, sizeof(tmp));
502 if (!(mode & FWRITE)) {
516 bcopy((char *)nat, (char *)n, sizeof(*n));
517 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
519 n->in_ifp = (void *)-1;
520 if (n->in_plabel[0] != '\0') {
521 n->in_apr = appr_lookup(n->in_p, n->in_plabel);
530 if (n->in_redir & NAT_REDIRECT) {
531 n->in_flags &= ~IPN_NOTDST;
534 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
535 n->in_flags &= ~IPN_NOTSRC;
540 if (n->in_redir & NAT_MAPBLK)
541 n->in_space = USABLE_PORTS * ~ntohl(n->in_outmsk);
542 else if (n->in_flags & IPN_AUTOPORTMAP)
543 n->in_space = USABLE_PORTS * ~ntohl(n->in_inmsk);
544 else if (n->in_flags & IPN_IPRANGE)
545 n->in_space = ntohl(n->in_outmsk) - ntohl(n->in_outip);
546 else if (n->in_flags & IPN_SPLIT)
549 n->in_space = ~ntohl(n->in_outmsk);
551 * Calculate the number of valid IP addresses in the output
552 * mapping range. In all cases, the range is inclusive of
553 * the start and ending IP addresses.
554 * If to a CIDR address, lose 2: broadcast + network address
556 * If to a range, add one.
557 * If to a single IP address, set to 1.
560 if ((n->in_flags & IPN_IPRANGE) != 0)
566 if ((n->in_outmsk != 0xffffffff) && (n->in_outmsk != 0) &&
567 ((n->in_flags & (IPN_IPRANGE|IPN_SPLIT)) == 0))
568 n->in_nip = ntohl(n->in_outip) + 1;
569 else if ((n->in_flags & IPN_SPLIT) &&
570 (n->in_redir & NAT_REDIRECT))
571 n->in_nip = ntohl(n->in_inip);
573 n->in_nip = ntohl(n->in_outip);
574 if (n->in_redir & NAT_MAP) {
575 n->in_pnext = ntohs(n->in_pmin);
577 * Multiply by the number of ports made available.
579 if (ntohs(n->in_pmax) >= ntohs(n->in_pmin)) {
580 n->in_space *= (ntohs(n->in_pmax) -
581 ntohs(n->in_pmin) + 1);
583 * Because two different sources can map to
584 * different destinations but use the same
586 * If the result is smaller than in_space, then
587 * we may have wrapped around 32bits.
590 if ((i != 0) && (i != 0xffffffff)) {
591 j = n->in_space * (~ntohl(i) + 1);
592 if (j >= n->in_space)
595 n->in_space = 0xffffffff;
599 * If no protocol is specified, multiple by 256.
601 if ((n->in_flags & IPN_TCPUDP) == 0) {
602 j = n->in_space * 256;
603 if (j >= n->in_space)
606 n->in_space = 0xffffffff;
609 /* Otherwise, these fields are preset */
611 nat_stats.ns_rules++;
614 if (!(mode & FWRITE)) {
623 if (n->in_redir & NAT_REDIRECT)
625 if (n->in_redir & (NAT_MAPBLK|NAT_MAP))
627 if (nat_list == NULL) {
634 appr_free(n->in_apr);
636 nat_stats.ns_rules--;
638 n->in_flags |= IPN_DELETE;
644 MUTEX_DOWNGRADE(&ipf_nat);
645 nat_stats.ns_table[0] = nat_table[0];
646 nat_stats.ns_table[1] = nat_table[1];
647 nat_stats.ns_list = nat_list;
648 nat_stats.ns_maptable = maptable;
649 nat_stats.ns_nattab_sz = ipf_nattable_sz;
650 nat_stats.ns_rultab_sz = ipf_natrules_sz;
651 nat_stats.ns_rdrtab_sz = ipf_rdrrules_sz;
652 nat_stats.ns_hostmap_sz = ipf_hostmap_sz;
653 nat_stats.ns_instances = nat_instances;
654 nat_stats.ns_apslist = ap_sess_list;
655 error = IWCOPYPTR((char *)&nat_stats, (char *)data,
662 MUTEX_DOWNGRADE(&ipf_nat);
663 error = IRCOPYPTR((char *)data, (char *)&nl, sizeof(nl));
667 if (nat_lookupredir(&nl)) {
668 error = IWCOPYPTR((char *)&nl, (char *)data,
674 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
675 if (!(mode & FWRITE)) {
681 ret = nat_flushtable();
683 ret = nat_clearlist();
686 MUTEX_DOWNGRADE(&ipf_nat);
688 error = IWCOPY((caddr_t)&ret, data, sizeof(ret));
694 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
696 error = IWCOPY((caddr_t)&fr_nat_lock, data,
697 sizeof(fr_nat_lock));
705 error = fr_natputent(data);
711 error = fr_natgetsz(data);
717 error = fr_natgetent(data);
723 arg = (int)iplused[IPL_LOGNAT];
724 MUTEX_DOWNGRADE(&ipf_nat);
725 error = IWCOPY((caddr_t)&arg, (caddr_t)data, sizeof(arg));
735 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
744 static int fr_natgetsz(data)
752 error = IRCOPY(data, (caddr_t)&ng, sizeof(ng));
761 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
768 * Make sure the pointer we're copying from exists in the
769 * current list of entries. Security precaution to prevent
770 * copying of random kernel data.
772 for (n = nat_instances; n; n = n->nat_next)
779 ng.ng_sz = sizeof(nat_save_t);
781 if ((aps != NULL) && (aps->aps_data != 0)) {
782 ng.ng_sz += sizeof(ap_session_t);
783 ng.ng_sz += aps->aps_psiz;
784 if (aps->aps_psiz > 4) /* XXX - sizeof(ipn_data) */
788 error = IWCOPY((caddr_t)&ng, data, sizeof(ng));
795 static int fr_natgetent(data)
798 nat_save_t ipn, *ipnp, *ipnn = NULL;
804 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
807 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
815 if (nat_instances == NULL)
821 * Make sure the pointer we're copying from exists in the
822 * current list of entries. Security precaution to prevent
823 * copying of random kernel data.
825 for (n = nat_instances; n; n = n->nat_next)
832 ipn.ipn_next = nat->nat_next;
833 bcopy((char *)nat, (char *)&ipn.ipn_nat, sizeof(ipn.ipn_nat));
834 ipn.ipn_nat.nat_data = NULL;
837 bcopy((char *)nat->nat_ptr, (char *)&ipn.ipn_ipnat,
838 sizeof(ipn.ipn_ipnat));
842 bcopy((char *)nat->nat_fr, (char *)&ipn.ipn_rule,
843 sizeof(ipn.ipn_rule));
845 if ((aps = nat->nat_aps)) {
848 dsz += aps->aps_psiz;
850 if (dsz > sizeof(ipn.ipn_data))
851 dsz -= sizeof(ipn.ipn_data);
852 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + dsz);
855 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
857 bcopy((char *)aps, (char *)ipnn->ipn_data, sizeof(*aps));
859 bcopy(aps->aps_data, ipnn->ipn_data + sizeof(*aps),
862 error = IWCOPY((caddr_t)ipnn, ipnp,
866 KFREES(ipnn, sizeof(*ipnn) + dsz);
869 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
877 static int fr_natputent(data)
880 nat_save_t ipn, *ipnp, *ipnn = NULL;
888 error = IRCOPY(data, (caddr_t)&ipnp, sizeof(ipnp));
891 error = IRCOPY((caddr_t)ipnp, (caddr_t)&ipn, sizeof(ipn));
896 KMALLOCS(ipnn, nat_save_t *, sizeof(*ipnn) + ipn.ipn_dsize);
899 bcopy((char *)&ipn, (char *)ipnn, sizeof(ipn));
900 error = IRCOPY((caddr_t)ipnp + offsetof(nat_save_t, ipn_data),
901 (caddr_t)ipnn->ipn_data, ipn.ipn_dsize);
909 KMALLOC(nat, nat_t *);
915 bcopy((char *)&ipn.ipn_nat, (char *)nat, sizeof(*nat));
917 * Initialize all these so that nat_delete() doesn't cause a crash.
919 nat->nat_phnext[0] = NULL;
920 nat->nat_phnext[1] = NULL;
928 nat->nat_data = NULL;
929 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
932 * Restore the rule associated with this nat session
935 KMALLOC(in, ipnat_t *);
941 bcopy((char *)&ipn.ipn_ipnat, (char *)in, sizeof(*in));
943 in->in_flags |= IPN_DELETE;
946 in->in_prnext = NULL;
948 in->in_pmnext = NULL;
949 in->in_ifp = GETUNIT(in->in_ifname, 4);
950 if (in->in_plabel[0] != '\0') {
951 in->in_apr = appr_lookup(in->in_p, in->in_plabel);
956 * Restore ap_session_t structure. Include the private data allocated
960 KMALLOC(aps, ap_session_t *);
966 aps->aps_next = ap_sess_list;
968 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
970 aps->aps_apr = in->in_apr;
972 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
973 if (aps->aps_data == NULL) {
977 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
981 aps->aps_data = NULL;
986 * If there was a filtering rule associated with this entry then
987 * build up a new one.
990 if (nat->nat_flags & FI_NEWFR) {
991 KMALLOC(fr, frentry_t *);
997 bcopy((char *)&ipn.ipn_fr, (char *)fr, sizeof(*fr));
998 ipn.ipn_nat.nat_fr = fr;
999 error = IWCOPY((caddr_t)&ipn, ipnp, sizeof(ipn));
1005 for (n = nat_instances; n; n = n->nat_next)
1006 if (n->nat_fr == fr)
1016 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1021 KFREES(ipnn, sizeof(ipn) + ipn.ipn_dsize);
1029 * Delete a nat entry from the various lists and table.
1031 static void nat_delete(natd)
1036 if (natd->nat_flags & FI_WILDP)
1037 nat_stats.ns_wilds--;
1038 if (natd->nat_hnext[0])
1039 natd->nat_hnext[0]->nat_phnext[0] = natd->nat_phnext[0];
1040 *natd->nat_phnext[0] = natd->nat_hnext[0];
1041 if (natd->nat_hnext[1])
1042 natd->nat_hnext[1]->nat_phnext[1] = natd->nat_phnext[1];
1043 *natd->nat_phnext[1] = natd->nat_hnext[1];
1044 if (natd->nat_me != NULL)
1045 *natd->nat_me = NULL;
1047 if (natd->nat_fr != NULL) {
1048 ATOMIC_DEC32(natd->nat_fr->fr_ref);
1051 if (natd->nat_hm != NULL)
1052 nat_hostmapdel(natd->nat_hm);
1055 * If there is an active reference from the nat entry to its parent
1056 * rule, decrement the rule's reference count and free it too if no
1057 * longer being used.
1059 ipn = natd->nat_ptr;
1063 if (!ipn->in_use && (ipn->in_flags & IPN_DELETE)) {
1065 appr_free(ipn->in_apr);
1067 nat_stats.ns_rules--;
1071 MUTEX_DESTROY(&natd->nat_lock);
1073 * If there's a fragment table entry too for this nat entry, then
1074 * dereference that as well.
1076 ipfr_forgetnat((void *)natd);
1077 aps_free(natd->nat_aps);
1078 nat_stats.ns_inuse--;
1084 * nat_flushtable - clear the NAT table of all mapping entries.
1085 * (this is for the dynamic mappings)
1087 static int nat_flushtable()
1093 * ALL NAT mappings deleted, so lets just make the deletions
1096 if (nat_table[0] != NULL)
1097 bzero((char *)nat_table[0],
1098 sizeof(nat_table[0]) * ipf_nattable_sz);
1099 if (nat_table[1] != NULL)
1100 bzero((char *)nat_table[1],
1101 sizeof(nat_table[1]) * ipf_nattable_sz);
1103 for (natp = &nat_instances; (nat = *natp); ) {
1104 *natp = nat->nat_next;
1106 nat_log(nat, NL_FLUSH);
1111 nat_stats.ns_inuse = 0;
1117 * nat_clearlist - delete all rules in the active NAT mapping list.
1118 * (this is for NAT/RDR rules)
1122 ipnat_t *n, **np = &nat_list;
1125 if (nat_rules != NULL)
1126 bzero((char *)nat_rules, sizeof(*nat_rules) * ipf_natrules_sz);
1127 if (rdr_rules != NULL)
1128 bzero((char *)rdr_rules, sizeof(*rdr_rules) * ipf_rdrrules_sz);
1134 appr_free(n->in_apr);
1136 nat_stats.ns_rules--;
1138 n->in_flags |= IPN_DELETE;
1150 * Create a new NAT table entry.
1151 * NOTE: Assumes write lock on ipf_nat has been obtained already.
1152 * If you intend on changing this, beware: appr_new() may call nat_new()
1155 nat_t *nat_new(fin, ip, np, natsave, flags, direction)
1163 u_32_t sum1, sum2, sumd, l;
1164 u_short port = 0, sport = 0, dport = 0, nport = 0;
1165 struct in_addr in, inb;
1166 u_short nflags, sp, dp;
1167 tcphdr_t *tcp = NULL;
1168 hostmap_t *hm = NULL;
1170 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1171 qif_t *qf = fin->fin_qif;
1174 if (nat_stats.ns_inuse >= ipf_nattable_max) {
1175 nat_stats.ns_memfail++;
1179 nflags = flags & np->in_flags;
1180 if (flags & IPN_TCPUDP) {
1181 tcp = (tcphdr_t *)fin->fin_dp;
1182 sport = htons(fin->fin_data[0]);
1183 dport = htons(fin->fin_data[1]);
1186 /* Give me a new nat */
1187 KMALLOC(nat, nat_t *);
1189 nat_stats.ns_memfail++;
1191 * Try to automatically tune the max # of entries in the
1192 * table allowed to be less than what will cause kmem_alloc()
1193 * to fail and try to eliminate panics due to out of memory
1194 * conditions arising.
1196 if (ipf_nattable_max > ipf_nattable_sz) {
1197 ipf_nattable_max = nat_stats.ns_inuse - 100;
1198 printf("ipf_nattable_max reduced to %d\n",
1204 bzero((char *)nat, sizeof(*nat));
1205 nat->nat_tcpstate[0] = TCPS_CLOSED;
1206 nat->nat_tcpstate[1] = TCPS_CLOSED;
1207 nat->nat_flags = flags;
1208 if (flags & FI_WILDP)
1209 nat_stats.ns_wilds++;
1211 * Search the current table for a match.
1213 if (direction == NAT_OUTBOUND) {
1215 * Values at which the search for a free resouce starts.
1221 * If it's an outbound packet which doesn't match any existing
1222 * record, then create a new port
1226 st_port = np->in_pnext;
1230 in.s_addr = htonl(np->in_nip);
1233 * Check to see if there is an existing NAT
1234 * setup for this IP address pair.
1236 hm = nat_hostmap(np, fin->fin_src, in);
1238 in.s_addr = hm->hm_mapip.s_addr;
1239 } else if ((l == 1) && (hm != NULL)) {
1243 in.s_addr = ntohl(in.s_addr);
1247 if ((np->in_outmsk == 0xffffffff) &&
1248 (np->in_pnext == 0)) {
1253 if (np->in_redir & NAT_MAPBLK) {
1254 if ((l >= np->in_ppip) || ((l > 0) &&
1255 !(flags & IPN_TCPUDP)))
1258 * map-block - Calculate destination address.
1260 in.s_addr = ntohl(fin->fin_saddr);
1261 in.s_addr &= ntohl(~np->in_inmsk);
1262 inb.s_addr = in.s_addr;
1263 in.s_addr /= np->in_ippip;
1264 in.s_addr &= ntohl(~np->in_outmsk);
1265 in.s_addr += ntohl(np->in_outip);
1267 * Calculate destination port.
1269 if ((flags & IPN_TCPUDP) &&
1270 (np->in_ppip != 0)) {
1271 port = ntohs(sport) + l;
1272 port %= np->in_ppip;
1273 port += np->in_ppip *
1274 (inb.s_addr % np->in_ippip);
1275 port += MAPBLK_MINPORT;
1278 } else if (!np->in_outip &&
1279 (np->in_outmsk == 0xffffffff)) {
1281 * 0/32 - use the interface's IP address.
1284 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1286 in.s_addr = ntohl(in.s_addr);
1287 } else if (!np->in_outip && !np->in_outmsk) {
1289 * 0/0 - use the original source address/port.
1293 in.s_addr = ntohl(fin->fin_saddr);
1294 } else if ((np->in_outmsk != 0xffffffff) &&
1295 (np->in_pnext == 0) &&
1296 ((l > 0) || (hm == NULL)))
1300 if ((nflags & IPN_TCPUDP) &&
1301 ((np->in_redir & NAT_MAPBLK) == 0) &&
1302 (np->in_flags & IPN_AUTOPORTMAP)) {
1303 if ((l > 0) && (l % np->in_ppip == 0)) {
1304 if (l > np->in_space) {
1306 } else if ((l > np->in_ppip) &&
1307 np->in_outmsk != 0xffffffff)
1310 if (np->in_ppip != 0) {
1311 port = ntohs(sport);
1312 port += (l % np->in_ppip);
1313 port %= np->in_ppip;
1314 port += np->in_ppip *
1315 (ntohl(fin->fin_saddr) %
1317 port += MAPBLK_MINPORT;
1320 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1321 (nflags & IPN_TCPUDP) &&
1322 (np->in_pnext != 0)) {
1323 port = htons(np->in_pnext++);
1324 if (np->in_pnext > ntohs(np->in_pmax)) {
1325 np->in_pnext = ntohs(np->in_pmin);
1326 if (np->in_outmsk != 0xffffffff)
1331 if (np->in_flags & IPN_IPRANGE) {
1332 if (np->in_nip > ntohl(np->in_outmsk))
1333 np->in_nip = ntohl(np->in_outip);
1335 if ((np->in_outmsk != 0xffffffff) &&
1336 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1337 ntohl(np->in_outip))
1338 np->in_nip = ntohl(np->in_outip) + 1;
1341 if (!port && (flags & IPN_TCPUDP))
1345 * Here we do a lookup of the connection as seen from
1346 * the outside. If an IP# pair already exists, try
1347 * again. So if you have A->B becomes C->B, you can
1348 * also have D->E become C->E but not D->B causing
1349 * another C->B. Also take protocol and ports into
1350 * account when determining whether a pre-existing
1351 * NAT setup will cause an external conflict where
1352 * this is appropriate.
1354 inb.s_addr = htonl(in.s_addr);
1355 sp = fin->fin_data[0];
1356 dp = fin->fin_data[1];
1357 fin->fin_data[0] = fin->fin_data[1];
1358 fin->fin_data[1] = htons(port);
1359 natl = nat_inlookup(fin, flags & ~FI_WILDP,
1360 (u_int)fin->fin_p, fin->fin_dst,
1362 fin->fin_data[0] = sp;
1363 fin->fin_data[1] = dp;
1366 * Has the search wrapped around and come back to the
1369 if ((natl != NULL) &&
1370 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1371 (np->in_nip != 0) && (st_ip == np->in_nip))
1374 } while (natl != NULL);
1376 if (np->in_space > 0)
1379 /* Setup the NAT table */
1380 nat->nat_inip = fin->fin_src;
1381 nat->nat_outip.s_addr = htonl(in.s_addr);
1382 nat->nat_oip = fin->fin_dst;
1383 if (nat->nat_hm == NULL)
1384 nat->nat_hm = nat_hostmap(np, fin->fin_src,
1387 sum1 = LONG_SUM(ntohl(fin->fin_saddr)) + ntohs(sport);
1388 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1390 if (flags & IPN_TCPUDP) {
1391 nat->nat_inport = sport;
1392 nat->nat_outport = port; /* sport */
1393 nat->nat_oport = dport;
1397 * Otherwise, it's an inbound packet. Most likely, we don't
1398 * want to rewrite source ports and source addresses. Instead,
1399 * we want to rewrite to a fixed internal address and fixed
1402 if (np->in_flags & IPN_SPLIT) {
1403 in.s_addr = np->in_nip;
1404 if (np->in_inip == htonl(in.s_addr))
1405 np->in_nip = ntohl(np->in_inmsk);
1407 np->in_nip = ntohl(np->in_inip);
1408 if (np->in_flags & IPN_ROUNDR) {
1414 in.s_addr = ntohl(np->in_inip);
1415 if (np->in_flags & IPN_ROUNDR) {
1424 * Whilst not optimized for the case where
1425 * pmin == pmax, the gain is not significant.
1427 if (np->in_pmin != np->in_pmax) {
1428 nport = ntohs(dport) - ntohs(np->in_pmin) +
1429 ntohs(np->in_pnext);
1430 nport = ntohs(nport);
1432 nport = np->in_pnext;
1436 * When the redirect-to address is set to 0.0.0.0, just
1437 * assume a blank `forwarding' of the packet.
1440 in.s_addr = ntohl(fin->fin_daddr);
1442 nat->nat_inip.s_addr = htonl(in.s_addr);
1443 nat->nat_outip = fin->fin_dst;
1444 nat->nat_oip = fin->fin_src;
1446 sum1 = LONG_SUM(ntohl(fin->fin_daddr)) + ntohs(dport);
1447 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1449 if (flags & IPN_TCPUDP) {
1450 nat->nat_inport = nport;
1451 nat->nat_outport = dport;
1452 nat->nat_oport = sport;
1456 CALC_SUMD(sum1, sum2, sumd);
1457 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1458 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1459 if ((flags & IPN_TCP) && dohwcksum &&
1460 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1461 if (direction == NAT_OUTBOUND)
1462 sum1 = LONG_SUM(ntohl(in.s_addr));
1464 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1465 sum1 += LONG_SUM(ntohl(fin->fin_daddr));
1466 sum1 += IPPROTO_TCP;
1467 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1468 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1471 nat->nat_sumd[1] = nat->nat_sumd[0];
1473 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1474 if (direction == NAT_OUTBOUND)
1475 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1477 sum1 = LONG_SUM(ntohl(fin->fin_daddr));
1479 sum2 = LONG_SUM(in.s_addr);
1481 CALC_SUMD(sum1, sum2, sumd);
1482 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1484 nat->nat_ipsumd = nat->nat_sumd[0];
1486 in.s_addr = htonl(in.s_addr);
1488 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1490 nat->nat_me = natsave;
1491 nat->nat_dir = direction;
1492 nat->nat_ifp = fin->fin_ifp;
1494 nat->nat_p = fin->fin_p;
1497 nat->nat_mssclamp = np->in_mssclamp;
1498 nat->nat_fr = fin->fin_fr;
1499 if (nat->nat_fr != NULL) {
1500 ATOMIC_INC32(nat->nat_fr->fr_ref);
1502 if (direction == NAT_OUTBOUND) {
1503 if (flags & IPN_TCPUDP)
1504 tcp->th_sport = port;
1506 if (flags & IPN_TCPUDP)
1507 tcp->th_dport = nport;
1512 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
1513 (tcp != NULL && dport == np->in_dport)))
1514 (void) appr_new(fin, ip, nat);
1518 nat_log(nat, (u_int)np->in_redir);
1522 nat_stats.ns_badnat++;
1523 if ((hm = nat->nat_hm) != NULL)
1531 * Insert a NAT entry into the hash tables for searching and add it to the
1532 * list of active NAT entries. Adjust global counters when complete.
1534 void nat_insert(nat)
1540 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1542 nat->nat_age = fr_defnatage;
1543 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1544 if (nat->nat_ifname[0] !='\0') {
1545 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1548 nat->nat_next = nat_instances;
1549 nat_instances = nat;
1551 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1552 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1554 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1556 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1558 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1561 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_inip.s_addr,
1563 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_outip.s_addr,
1567 natp = &nat_table[0][hv1];
1569 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1570 nat->nat_phnext[0] = natp;
1571 nat->nat_hnext[0] = *natp;
1574 natp = &nat_table[1][hv2];
1576 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1577 nat->nat_phnext[1] = natp;
1578 nat->nat_hnext[1] = *natp;
1581 nat_stats.ns_added++;
1582 nat_stats.ns_inuse++;
1586 nat_t *nat_icmplookup(ip, fin, dir)
1592 tcphdr_t *tcp = NULL;
1594 int flags = 0, type, minlen;
1596 icmp = (icmphdr_t *)fin->fin_dp;
1598 * Does it at least have the return (basic) IP header ?
1599 * Only a basic IP header (no options) should be with an ICMP error
1602 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1604 type = icmp->icmp_type;
1606 * If it's not an error type, then return.
1608 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1609 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1610 (type != ICMP_PARAMPROB))
1613 oip = (ip_t *)((char *)fin->fin_dp + 8);
1614 minlen = (oip->ip_hl << 2);
1615 if (minlen < sizeof(ip_t))
1617 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1620 * Is the buffer big enough for all of it ? It's the size of the IP
1621 * header claimed in the encapsulated part which is of concern. It
1622 * may be too big to be in this buffer but not so big that it's
1623 * outside the ICMP packet, leading to TCP deref's causing problems.
1624 * This is possible because we don't know how big oip_hl is when we
1625 * do the pullup early in fr_check() and thus can't gaurantee it is
1634 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1637 m = *(mb_t **)fin->fin_mp;
1638 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1639 (char *)ip + m->m_len)
1645 if (oip->ip_p == IPPROTO_TCP)
1647 else if (oip->ip_p == IPPROTO_UDP)
1649 if (flags & IPN_TCPUDP) {
1653 minlen += 8; /* + 64bits of data to get ports */
1654 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1657 data[0] = fin->fin_data[0];
1658 data[1] = fin->fin_data[1];
1659 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1660 fin->fin_data[0] = ntohs(tcp->th_dport);
1661 fin->fin_data[1] = ntohs(tcp->th_sport);
1663 if (dir == NAT_INBOUND) {
1664 nat = nat_inlookup(fin, flags, (u_int)oip->ip_p,
1665 oip->ip_dst, oip->ip_src, 0);
1667 nat = nat_outlookup(fin, flags, (u_int)oip->ip_p,
1668 oip->ip_dst, oip->ip_src, 0);
1670 fin->fin_data[0] = data[0];
1671 fin->fin_data[1] = data[1];
1674 if (dir == NAT_INBOUND)
1675 return nat_inlookup(fin, 0, (u_int)oip->ip_p,
1676 oip->ip_dst, oip->ip_src, 0);
1678 return nat_outlookup(fin, 0, (u_int)oip->ip_p,
1679 oip->ip_dst, oip->ip_src, 0);
1684 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1685 * packet gets correctly recognised.
1687 nat_t *nat_icmp(ip, fin, nflags, dir)
1693 u_32_t sum1, sum2, sumd, sumd2 = 0;
1702 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1705 * nat_icmplookup() will return NULL for `defective' packets.
1707 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1712 *nflags = IPN_ICMPERR;
1713 icmp = (icmphdr_t *)fin->fin_dp;
1714 oip = (ip_t *)&icmp->icmp_ip;
1715 if (oip->ip_p == IPPROTO_TCP)
1717 else if (oip->ip_p == IPPROTO_UDP)
1719 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1720 dlen = ip->ip_len - ((char *)udp - (char *)ip);
1722 * XXX - what if this is bogus hl and we go off the end ?
1723 * In this case, nat_icmplookup() will have returned NULL.
1725 tcp = (tcphdr_t *)udp;
1728 * Need to adjust ICMP header to include the real IP#'s and
1729 * port #'s. Only apply a checksum change relative to the
1730 * IP address change as it will be modified again in ip_natout
1731 * for both address and port. Two checksum changes are
1732 * necessary for the two header address changes. Be careful
1733 * to only modify the checksum once for the port # and twice
1739 * Fix the IP addresses in the offending IP packet. You also need
1740 * to adjust the IP header checksum of that offending IP packet
1741 * and the ICMP checksum of the ICMP error message itself.
1743 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1744 * in the pseudo header that is used to compute the UDP resp. TCP
1745 * checksum. So, we must compensate that as well. Even worse, the
1746 * change in the UDP and TCP checksums require yet another
1747 * adjustment of the ICMP checksum of the ICMP error message.
1751 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1752 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1756 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1757 in = nat->nat_outip;
1761 sum2 = LONG_SUM(ntohl(in.s_addr));
1763 CALC_SUMD(sum1, sum2, sumd);
1766 * Fix IP checksum of the offending IP packet to adjust for
1767 * the change in the IP address.
1769 * Normally, you would expect that the ICMP checksum of the
1770 * ICMP error message needs to be adjusted as well for the
1771 * IP address change in oip.
1772 * However, this is a NOP, because the ICMP checksum is
1773 * calculated over the complete ICMP packet, which includes the
1774 * changed oip IP addresses and oip->ip_sum. However, these
1775 * two changes cancel each other out (if the delta for
1776 * the IP address is x, then the delta for ip_sum is minus x),
1777 * so no change in the icmp_cksum is necessary.
1779 * Be careful that nat_dir refers to the direction of the
1780 * offending IP packet (oip), not to its ICMP response (icmp)
1782 fix_datacksum(&oip->ip_sum, sumd);
1783 /* Fix icmp cksum : IP Addr + Cksum */
1786 * Fix UDP pseudo header checksum to compensate for the
1787 * IP address change.
1789 if ((oip->ip_p == IPPROTO_UDP) && (dlen >= 8) && udp->uh_sum) {
1791 * The UDP checksum is optional, only adjust it
1792 * if it has been set.
1794 sum1 = ntohs(udp->uh_sum);
1795 fix_datacksum(&udp->uh_sum, sumd);
1796 sum2 = ntohs(udp->uh_sum);
1799 * Fix ICMP checksum to compensate the UDP
1800 * checksum adjustment.
1803 CALC_SUMD(sum1, sum2, sumd);
1808 * Fix TCP pseudo header checksum to compensate for the
1809 * IP address change. Before we can do the change, we
1810 * must make sure that oip is sufficient large to hold
1811 * the TCP checksum (normally it does not!).
1813 else if ((oip->ip_p == IPPROTO_TCP) && (dlen >= 18)) {
1814 sum1 = ntohs(tcp->th_sum);
1815 fix_datacksum(&tcp->th_sum, sumd);
1816 sum2 = ntohs(tcp->th_sum);
1819 * Fix ICMP checksum to compensate the TCP
1820 * checksum adjustment.
1823 CALC_SUMD(sum1, sum2, sumd);
1826 sumd2 = (sumd >> 16);
1827 if (nat->nat_dir == NAT_OUTBOUND)
1833 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
1836 * For offending TCP/UDP IP packets, translate the ports as
1837 * well, based on the NAT specification. Of course such
1838 * a change must be reflected in the ICMP checksum as well.
1840 * Advance notice : Now it becomes complicated :-)
1842 * Since the port fields are part of the TCP/UDP checksum
1843 * of the offending IP packet, you need to adjust that checksum
1844 * as well... but, if you change, you must change the icmp
1845 * checksum *again*, to reflect that change.
1847 * To further complicate: the TCP checksum is not in the first
1848 * 8 bytes of the offending ip packet, so it most likely is not
1849 * available. Some OSses like Solaris return enough bytes to
1850 * include the TCP checksum. So we have to check if the
1851 * ip->ip_len actually holds the TCP checksum of the oip!
1853 if (nat->nat_oport == tcp->th_dport) {
1854 if (tcp->th_sport != nat->nat_inport) {
1856 * Fix ICMP checksum to compensate port
1859 sum1 = ntohs(nat->nat_inport);
1860 sum2 = ntohs(tcp->th_sport);
1861 tcp->th_sport = nat->nat_inport;
1864 * Fix udp checksum to compensate port
1865 * adjustment. NOTE : the offending IP packet
1866 * flows the other direction compared to the
1869 * The UDP checksum is optional, only adjust
1870 * it if it has been set.
1872 if ((oip->ip_p == IPPROTO_UDP) &&
1873 (dlen >= 8) && udp->uh_sum) {
1877 sum1 = ntohs(udp->uh_sum);
1878 fix_datacksum(&udp->uh_sum, sumd);
1879 sum2 = ntohs(udp->uh_sum);
1882 * Fix ICMP checksum to compensate
1883 * UDP checksum adjustment.
1885 CALC_SUMD(sum1, sum2, sumd);
1890 * Fix tcp checksum (if present) to compensate
1891 * port adjustment. NOTE : the offending IP
1892 * packet flows the other direction compared to
1895 if (oip->ip_p == IPPROTO_TCP) {
1900 sum1 = ntohs(tcp->th_sum);
1901 fix_datacksum(&tcp->th_sum,
1903 sum2 = ntohs(tcp->th_sum);
1906 * Fix ICMP checksum to
1907 * compensate TCP checksum
1910 CALC_SUMD(sum1, sum2, sumd);
1913 sumd = sum2 - sum1 + 1;
1918 } else if (tcp->th_dport != nat->nat_outport) {
1920 * Fix ICMP checksum to compensate port
1923 sum1 = ntohs(nat->nat_outport);
1924 sum2 = ntohs(tcp->th_dport);
1925 tcp->th_dport = nat->nat_outport;
1928 * Fix udp checksum to compensate port
1929 * adjustment. NOTE : the offending IP
1930 * packet flows the other direction compared
1931 * to the ICMP message.
1933 * The UDP checksum is optional, only adjust
1934 * it if it has been set.
1936 if ((oip->ip_p == IPPROTO_UDP) &&
1937 (dlen >= 8) && udp->uh_sum) {
1941 sum1 = ntohs(udp->uh_sum);
1942 fix_datacksum(&udp->uh_sum, sumd);
1943 sum2 = ntohs(udp->uh_sum);
1946 * Fix ICMP checksum to compensate
1947 * UDP checksum adjustment.
1949 CALC_SUMD(sum1, sum2, sumd);
1953 * Fix tcp checksum (if present) to compensate
1954 * port adjustment. NOTE : the offending IP
1955 * packet flows the other direction compared to
1958 if (oip->ip_p == IPPROTO_TCP) {
1963 sum1 = ntohs(tcp->th_sum);
1964 fix_datacksum(&tcp->th_sum, sumd);
1965 sum2 = ntohs(tcp->th_sum);
1968 * Fix ICMP checksum to compensate
1969 * UDP checksum adjustment.
1971 CALC_SUMD(sum1, sum2, sumd);
1974 if (nat->nat_dir == NAT_OUTBOUND)
1981 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1982 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1983 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
1986 if (oip->ip_p == IPPROTO_ICMP)
1987 nat->nat_age = fr_defnaticmpage;
1993 * NB: these lookups don't lock access to the list, it assume it has already
1997 * Lookup a nat entry based on the mapped destination ip address/port and
1998 * real source address/port. We use this lookup when receiving a packet,
1999 * we're looking for a table entry, based on the destination address.
2000 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2002 nat_t *nat_inlookup(fin, flags, p, src, mapdst, rw)
2005 struct in_addr src , mapdst;
2008 u_short sport, dport;
2020 dst = mapdst.s_addr;
2021 if (flags & IPN_TCPUDP) {
2022 sport = htons(fin->fin_data[0]);
2023 dport = htons(fin->fin_data[1]);
2029 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
2030 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
2031 nat = nat_table[1][hv];
2032 for (; nat; nat = nat->nat_hnext[1]) {
2033 nflags = nat->nat_flags;
2034 if ((!ifp || ifp == nat->nat_ifp) &&
2035 nat->nat_oip.s_addr == src.s_addr &&
2036 nat->nat_outip.s_addr == dst &&
2037 ((p == 0) || (p == nat->nat_p))) {
2042 if (nat->nat_oport != sport)
2044 if (nat->nat_outport != dport)
2052 if ((ipn != NULL) && (nat->nat_aps != NULL))
2053 if (appr_match(fin, nat) != 0)
2058 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2061 RWLOCK_EXIT(&ipf_nat);
2063 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
2064 hv = NAT_HASH_FN(src.s_addr, dst, ipf_nattable_sz);
2066 WRITE_ENTER(&ipf_nat);
2068 nat = nat_table[1][hv];
2069 for (; nat; nat = nat->nat_hnext[1]) {
2070 nflags = nat->nat_flags;
2071 if (ifp && ifp != nat->nat_ifp)
2073 if (!(nflags & FI_WILDP))
2075 if (nat->nat_oip.s_addr != src.s_addr ||
2076 nat->nat_outip.s_addr != dst)
2078 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
2079 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
2080 nat_tabmove(fin, nat);
2085 MUTEX_DOWNGRADE(&ipf_nat);
2092 * This function is only called for TCP/UDP NAT table entries where the
2093 * original was placed in the table without hashing on the ports and we now
2094 * want to include hashing on port numbers.
2096 static void nat_tabmove(fin, nat)
2100 u_short sport, dport;
2104 nflags = nat->nat_flags;
2106 sport = ntohs(fin->fin_data[0]);
2107 dport = ntohs(fin->fin_data[1]);
2110 * Remove the NAT entry from the old location
2112 if (nat->nat_hnext[0])
2113 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2114 *nat->nat_phnext[0] = nat->nat_hnext[0];
2116 if (nat->nat_hnext[1])
2117 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2118 *nat->nat_phnext[1] = nat->nat_hnext[1];
2121 * Add into the NAT table in the new position
2123 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2124 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2125 natp = &nat_table[0][hv];
2127 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2128 nat->nat_phnext[0] = natp;
2129 nat->nat_hnext[0] = *natp;
2132 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2133 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2134 natp = &nat_table[1][hv];
2136 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2137 nat->nat_phnext[1] = natp;
2138 nat->nat_hnext[1] = *natp;
2144 * Lookup a nat entry based on the source 'real' ip address/port and
2145 * destination address/port. We use this lookup when sending a packet out,
2146 * we're looking for a table entry, based on the source address.
2147 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2149 nat_t *nat_outlookup(fin, flags, p, src, dst, rw)
2152 struct in_addr src , dst;
2155 u_short sport, dport;
2165 if (flags & IPN_TCPUDP) {
2166 sport = ntohs(fin->fin_data[0]);
2167 dport = ntohs(fin->fin_data[1]);
2173 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2174 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2175 nat = nat_table[0][hv];
2176 for (; nat; nat = nat->nat_hnext[0]) {
2177 nflags = nat->nat_flags;
2179 if ((!ifp || ifp == nat->nat_ifp) &&
2180 nat->nat_inip.s_addr == srcip &&
2181 nat->nat_oip.s_addr == dst.s_addr &&
2182 ((p == 0) || (p == nat->nat_p))) {
2187 if (nat->nat_oport != dport)
2189 if (nat->nat_inport != sport)
2197 if ((ipn != NULL) && (nat->nat_aps != NULL))
2198 if (appr_match(fin, nat) != 0)
2203 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2206 RWLOCK_EXIT(&ipf_nat);
2209 hv = NAT_HASH_FN(dst.s_addr, srcip, ipf_nattable_sz);
2211 WRITE_ENTER(&ipf_nat);
2213 nat = nat_table[0][hv];
2214 for (; nat; nat = nat->nat_hnext[0]) {
2215 nflags = nat->nat_flags;
2216 if (ifp && ifp != nat->nat_ifp)
2218 if (!(nflags & FI_WILDP))
2220 if ((nat->nat_inip.s_addr != srcip) ||
2221 (nat->nat_oip.s_addr != dst.s_addr))
2223 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2224 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2225 nat_tabmove(fin, nat);
2230 MUTEX_DOWNGRADE(&ipf_nat);
2237 * Lookup the NAT tables to search for a matching redirect
2239 nat_t *nat_lookupredir(np)
2245 bzero((char *)&fi, sizeof(fi));
2246 fi.fin_data[0] = ntohs(np->nl_inport);
2247 fi.fin_data[1] = ntohs(np->nl_outport);
2250 * If nl_inip is non null, this is a lookup based on the real
2251 * ip address. Else, we use the fake.
2253 if ((nat = nat_outlookup(&fi, np->nl_flags, 0, np->nl_inip,
2254 np->nl_outip, 0))) {
2255 np->nl_realip = nat->nat_outip;
2256 np->nl_realport = nat->nat_outport;
2262 static int nat_match(fin, np, ip)
2272 if (np->in_p && fin->fin_p != np->in_p)
2275 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2277 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2278 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2280 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2281 ^ ((np->in_flags & IPN_NOTDST) != 0))
2284 if (!(np->in_redir & NAT_REDIRECT))
2286 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2287 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2289 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2290 ^ ((np->in_flags & IPN_NOTDST) != 0))
2295 if (!(fin->fin_fl & FI_TCPUDP) ||
2296 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2297 if (ft->ftu_scmp || ft->ftu_dcmp)
2302 return fr_tcpudpchk(ft, fin);
2307 * Packets going out on the external interface go through this.
2308 * Here, the source address requires alteration, if anything.
2310 int ip_natout(ip, fin)
2316 tcphdr_t *tcp = NULL;
2317 u_short sport = 0, dport = 0, *csump = NULL;
2318 int natadd = 1, i, icmpset = 1;
2319 u_int nflags = 0, hv, msk;
2326 if (nat_list == NULL || (fr_nat_lock))
2329 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2330 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1) {
2331 sifp = fin->fin_ifp;
2332 fin->fin_ifp = fr->fr_tif.fd_ifp;
2334 sifp = fin->fin_ifp;
2337 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2338 if (fin->fin_p == IPPROTO_TCP)
2340 else if (fin->fin_p == IPPROTO_UDP)
2342 if ((nflags & IPN_TCPUDP)) {
2343 tcp = (tcphdr_t *)fin->fin_dp;
2344 sport = tcp->th_sport;
2345 dport = tcp->th_dport;
2349 ipa = fin->fin_saddr;
2351 READ_ENTER(&ipf_nat);
2353 if ((fin->fin_p == IPPROTO_ICMP) &&
2354 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2356 else if ((fin->fin_fl & FI_FRAG) &&
2357 (nat = ipfr_nat_knownfrag(ip, fin)))
2359 else if ((nat = nat_outlookup(fin, nflags|FI_WILDP|FI_WILDA,
2360 (u_int)fin->fin_p, fin->fin_src,
2361 fin->fin_dst, 0))) {
2362 nflags = nat->nat_flags;
2363 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2364 if ((nflags & FI_W_SPORT) &&
2365 (nat->nat_inport != sport))
2366 nat->nat_inport = sport;
2367 if ((nflags & FI_W_DPORT) &&
2368 (nat->nat_oport != dport))
2369 nat->nat_oport = dport;
2371 if (nat->nat_outport == 0)
2372 nat->nat_outport = sport;
2373 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2374 nflags = nat->nat_flags;
2375 nat_stats.ns_wilds--;
2378 RWLOCK_EXIT(&ipf_nat);
2383 WRITE_ENTER(&ipf_nat);
2385 * If there is no current entry in the nat table for this IP#,
2386 * create one for it (if there is a matching rule).
2389 iph = ipa & htonl(msk);
2390 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2391 for (np = nat_rules[hv]; np; np = np->in_mnext)
2393 if (np->in_ifp && (np->in_ifp != ifp))
2395 if ((np->in_flags & IPN_RF) &&
2396 !(np->in_flags & nflags))
2398 if (np->in_flags & IPN_FILTER) {
2399 if (!nat_match(fin, np, ip))
2401 } else if ((ipa & np->in_inmsk) != np->in_inip)
2403 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2405 nat = nat_new(fin, ip, np, NULL,
2406 (u_int)nflags, NAT_OUTBOUND);
2412 if ((np == NULL) && (i > 0)) {
2416 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2420 MUTEX_DOWNGRADE(&ipf_nat);
2424 * NOTE: ipf_nat must now only be held as a read lock
2428 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2429 ipfr_nat_newfrag(ip, fin, nat);
2430 MUTEX_ENTER(&nat->nat_lock);
2431 if (fin->fin_p != IPPROTO_TCP) {
2432 if (np && np->in_age[1])
2433 nat->nat_age = np->in_age[1];
2434 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2435 nat->nat_age = fr_defnaticmpage;
2437 nat->nat_age = fr_defnatage;
2439 nat->nat_bytes += ip->ip_len;
2441 MUTEX_EXIT(&nat->nat_lock);
2444 * Fix up checksums, not by recalculating them, but
2445 * simply computing adjustments.
2447 if (nflags == IPN_ICMPERR) {
2448 u_32_t s1, s2, sumd;
2450 s1 = LONG_SUM(ntohl(fin->fin_saddr));
2451 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2452 CALC_SUMD(s1, s2, sumd);
2453 fix_outcksum(fin, &ip->ip_sum, sumd);
2455 #if (SOLARIS || defined(__sgi)) || !defined(_KERNEL)
2457 if (nat->nat_dir == NAT_OUTBOUND)
2458 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2460 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2464 * Only change the packet contents, not what is filtered upon.
2466 ip->ip_src = nat->nat_outip;
2468 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2470 if ((nat->nat_outport != 0) && (tcp != NULL)) {
2471 tcp->th_sport = nat->nat_outport;
2472 fin->fin_data[0] = ntohs(tcp->th_sport);
2475 if (fin->fin_p == IPPROTO_TCP) {
2476 csump = &tcp->th_sum;
2477 MUTEX_ENTER(&nat->nat_lock);
2478 fr_tcp_age(&nat->nat_age,
2479 nat->nat_tcpstate, fin, 1, 0);
2480 if (nat->nat_age < fr_defnaticmpage)
2481 nat->nat_age = fr_defnaticmpage;
2483 else if ((!np || !np->in_age[1]) &&
2484 (nat->nat_age > fr_defnatage))
2485 nat->nat_age = fr_defnatage;
2488 * Increase this because we may have
2489 * "keep state" following this too and
2490 * packet storms can occur if this is
2491 * removed too quickly.
2493 if (nat->nat_age == fr_tcpclosed)
2494 nat->nat_age = fr_tcplastack;
2497 * Do a MSS CLAMPING on a SYN packet,
2498 * only deal IPv4 for now.
2500 if (nat->nat_mssclamp &&
2501 (tcp->th_flags & TH_SYN) != 0)
2502 nat_mssclamp(tcp, nat->nat_mssclamp,
2505 MUTEX_EXIT(&nat->nat_lock);
2506 } else if (fin->fin_p == IPPROTO_UDP) {
2507 udphdr_t *udp = (udphdr_t *)tcp;
2510 csump = &udp->uh_sum;
2514 if (nat->nat_dir == NAT_OUTBOUND)
2515 fix_outcksum(fin, csump,
2518 fix_incksum(fin, csump,
2523 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2524 (tcp != NULL && dport == np->in_dport))) {
2525 i = appr_check(ip, fin, nat);
2532 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2533 RWLOCK_EXIT(&ipf_nat); /* READ */
2534 fin->fin_ifp = sifp;
2537 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2538 fin->fin_ifp = sifp;
2544 * Packets coming in from the external interface go through this.
2545 * Here, the destination address requires alteration, if anything.
2547 int ip_natin(ip, fin)
2554 u_short sport = 0, dport = 0, *csump = NULL;
2555 u_int nflags = 0, natadd = 1, hv, msk;
2556 struct ifnet *ifp = fin->fin_ifp;
2557 tcphdr_t *tcp = NULL;
2562 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2565 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2566 if (fin->fin_p == IPPROTO_TCP)
2568 else if (fin->fin_p == IPPROTO_UDP)
2570 if ((nflags & IPN_TCPUDP)) {
2571 tcp = (tcphdr_t *)fin->fin_dp;
2572 sport = tcp->th_sport;
2573 dport = tcp->th_dport;
2578 /* make sure the source address is to be redirected */
2581 READ_ENTER(&ipf_nat);
2583 if ((fin->fin_p == IPPROTO_ICMP) &&
2584 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2586 else if ((fin->fin_fl & FI_FRAG) &&
2587 (nat = ipfr_nat_knownfrag(ip, fin)))
2589 else if ((nat = nat_inlookup(fin, nflags|FI_WILDP|FI_WILDA,
2590 (u_int)fin->fin_p, fin->fin_src, in, 0))) {
2591 nflags = nat->nat_flags;
2592 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2593 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2594 nat->nat_oport = sport;
2595 if ((nat->nat_outport != dport) &&
2596 (nflags & FI_W_SPORT))
2597 nat->nat_outport = dport;
2598 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2599 nflags = nat->nat_flags;
2600 nat_stats.ns_wilds--;
2603 RWLOCK_EXIT(&ipf_nat);
2608 WRITE_ENTER(&ipf_nat);
2610 * If there is no current entry in the nat table for this IP#,
2611 * create one for it (if there is a matching rule).
2614 iph = in.s_addr & htonl(msk);
2615 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2616 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2617 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2618 (np->in_p && (np->in_p != fin->fin_p)) ||
2619 (np->in_flags && !(nflags & np->in_flags)))
2621 if (np->in_flags & IPN_FILTER) {
2622 if (!nat_match(fin, np, ip))
2624 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2626 if ((!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2627 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2628 (ntohs(dport) >= ntohs(np->in_pmin)))))
2629 if ((nat = nat_new(fin, ip, np, NULL, nflags,
2636 if ((np == NULL) && (i > 0)) {
2640 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2644 MUTEX_DOWNGRADE(&ipf_nat);
2648 * NOTE: ipf_nat must now only be held as a read lock
2652 fin->fin_fr = nat->nat_fr;
2653 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2654 ipfr_nat_newfrag(ip, fin, nat);
2655 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2656 (tcp != NULL && sport == np->in_dport))) {
2657 i = appr_check(ip, fin, nat);
2660 RWLOCK_EXIT(&ipf_nat);
2665 MUTEX_ENTER(&nat->nat_lock);
2666 if (fin->fin_p != IPPROTO_TCP) {
2667 if (np && np->in_age[0])
2668 nat->nat_age = np->in_age[0];
2669 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2670 nat->nat_age = fr_defnaticmpage;
2672 nat->nat_age = fr_defnatage;
2674 nat->nat_bytes += ip->ip_len;
2676 MUTEX_EXIT(&nat->nat_lock);
2679 * Fix up checksums, not by recalculating them, but
2680 * simply computing adjustments.
2682 if (nat->nat_dir == NAT_OUTBOUND)
2683 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2685 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2687 ip->ip_dst = nat->nat_inip;
2688 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2690 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2692 if ((nat->nat_inport != 0) && (tcp != NULL)) {
2693 tcp->th_dport = nat->nat_inport;
2694 fin->fin_data[1] = ntohs(tcp->th_dport);
2697 if (fin->fin_p == IPPROTO_TCP) {
2698 csump = &tcp->th_sum;
2699 MUTEX_ENTER(&nat->nat_lock);
2700 fr_tcp_age(&nat->nat_age,
2701 nat->nat_tcpstate, fin, 0, 0);
2702 if (nat->nat_age < fr_defnaticmpage)
2703 nat->nat_age = fr_defnaticmpage;
2705 else if ((!np || !np->in_age[0]) &&
2706 (nat->nat_age > fr_defnatage))
2707 nat->nat_age = fr_defnatage;
2710 * Increase this because we may have
2711 * "keep state" following this too and
2712 * packet storms can occur if this is
2713 * removed too quickly.
2715 if (nat->nat_age == fr_tcpclosed)
2716 nat->nat_age = fr_tcplastack;
2718 * Do a MSS CLAMPING on a SYN packet,
2719 * only deal IPv4 for now.
2721 if (nat->nat_mssclamp &&
2722 (tcp->th_flags & TH_SYN) != 0)
2723 nat_mssclamp(tcp, nat->nat_mssclamp,
2726 MUTEX_EXIT(&nat->nat_lock);
2727 } else if (fin->fin_p == IPPROTO_UDP) {
2728 udphdr_t *udp = (udphdr_t *)tcp;
2731 csump = &udp->uh_sum;
2735 if (nat->nat_dir == NAT_OUTBOUND)
2736 fix_incksum(fin, csump,
2739 fix_outcksum(fin, csump,
2743 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2744 RWLOCK_EXIT(&ipf_nat); /* READ */
2747 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2753 * Free all memory used by NAT structures allocated at runtime.
2757 WRITE_ENTER(&ipf_nat);
2758 (void) nat_clearlist();
2759 (void) nat_flushtable();
2760 RWLOCK_EXIT(&ipf_nat);
2762 if (nat_table[0] != NULL) {
2763 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2764 nat_table[0] = NULL;
2766 if (nat_table[1] != NULL) {
2767 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2768 nat_table[1] = NULL;
2770 if (nat_rules != NULL) {
2771 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2774 if (rdr_rules != NULL) {
2775 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2778 if (maptable != NULL) {
2779 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2786 * Slowly expire held state for NAT entries. Timeouts are set in
2787 * expectation of this being called twice per second.
2791 struct nat *nat, **natp;
2792 #if defined(_KERNEL) && !SOLARIS
2797 WRITE_ENTER(&ipf_nat);
2798 for (natp = &nat_instances; (nat = *natp); ) {
2801 natp = &nat->nat_next;
2804 *natp = nat->nat_next;
2806 nat_log(nat, NL_EXPIRE);
2809 nat_stats.ns_expire++;
2811 RWLOCK_EXIT(&ipf_nat);
2818 void ip_natsync(ifp)
2823 u_32_t sum1, sum2, sumd;
2827 #if defined(_KERNEL) && !SOLARIS
2832 * Change IP addresses for NAT sessions for any protocol except TCP
2833 * since it will break the TCP connection anyway.
2836 WRITE_ENTER(&ipf_nat);
2837 for (nat = nat_instances; nat; nat = nat->nat_next)
2838 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2839 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2840 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2841 ifp2 = nat->nat_ifp;
2843 * Change the map-to address to be the same as the
2846 sum1 = nat->nat_outip.s_addr;
2847 if (fr_ifpaddr(4, ifp2, &in) != -1)
2848 nat->nat_outip = in;
2849 sum2 = nat->nat_outip.s_addr;
2854 * Readjust the checksum adjustment to take into
2855 * account the new IP#.
2857 CALC_SUMD(sum1, sum2, sumd);
2858 /* XXX - dont change for TCP when solaris does
2859 * hardware checksumming.
2861 sumd += nat->nat_sumd[0];
2862 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2863 nat->nat_sumd[1] = nat->nat_sumd[0];
2866 for (n = nat_list; (n != NULL); n = n->in_next)
2867 if (n->in_ifp == ifp) {
2868 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2870 n->in_ifp = (void *)-1;
2872 RWLOCK_EXIT(&ipf_nat);
2878 void nat_log(nat, type)
2891 natl.nl_inip = nat->nat_inip;
2892 natl.nl_outip = nat->nat_outip;
2893 natl.nl_origip = nat->nat_oip;
2894 natl.nl_bytes = nat->nat_bytes;
2895 natl.nl_pkts = nat->nat_pkts;
2896 natl.nl_origport = nat->nat_oport;
2897 natl.nl_inport = nat->nat_inport;
2898 natl.nl_outport = nat->nat_outport;
2899 natl.nl_p = nat->nat_p;
2900 natl.nl_type = type;
2903 if (nat->nat_ptr != NULL) {
2904 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2905 if (np == nat->nat_ptr) {
2906 natl.nl_rule = rulen;
2912 sizes[0] = sizeof(natl);
2915 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);
2920 #if defined(__OpenBSD__)
2921 void nat_ifdetach(ifp)
2931 * Check for MSS option and clamp it if necessary.
2933 static void nat_mssclamp(tcp, maxmss, fin, csump)
2939 u_char *cp, *ep, opt;
2944 hlen = tcp->th_off << 2;
2945 if (hlen > sizeof(*tcp)) {
2946 cp = (u_char *)tcp + sizeof(*tcp);
2947 ep = (u_char *)tcp + hlen;
2951 if (opt == TCPOPT_EOL)
2953 else if (opt == TCPOPT_NOP) {
2961 if (&cp[advance] > ep)
2967 bcopy(&cp[2], &v, sizeof(v));
2971 bcopy(&v, &cp[2], sizeof(v));
2972 CALC_SUMD(mss, maxmss, sumd);
2973 fix_outcksum(fin, csump, sumd);
2977 /* ignore unknown options */