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.7 2004/07/28 00:22:37 hmp 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_flags = flags;
1206 if (flags & FI_WILDP)
1207 nat_stats.ns_wilds++;
1209 * Search the current table for a match.
1211 if (direction == NAT_OUTBOUND) {
1213 * Values at which the search for a free resouce starts.
1219 * If it's an outbound packet which doesn't match any existing
1220 * record, then create a new port
1224 st_port = np->in_pnext;
1228 in.s_addr = htonl(np->in_nip);
1231 * Check to see if there is an existing NAT
1232 * setup for this IP address pair.
1234 hm = nat_hostmap(np, fin->fin_src, in);
1236 in.s_addr = hm->hm_mapip.s_addr;
1237 } else if ((l == 1) && (hm != NULL)) {
1241 in.s_addr = ntohl(in.s_addr);
1245 if ((np->in_outmsk == 0xffffffff) &&
1246 (np->in_pnext == 0)) {
1251 if (np->in_redir & NAT_MAPBLK) {
1252 if ((l >= np->in_ppip) || ((l > 0) &&
1253 !(flags & IPN_TCPUDP)))
1256 * map-block - Calculate destination address.
1258 in.s_addr = ntohl(fin->fin_saddr);
1259 in.s_addr &= ntohl(~np->in_inmsk);
1260 inb.s_addr = in.s_addr;
1261 in.s_addr /= np->in_ippip;
1262 in.s_addr &= ntohl(~np->in_outmsk);
1263 in.s_addr += ntohl(np->in_outip);
1265 * Calculate destination port.
1267 if ((flags & IPN_TCPUDP) &&
1268 (np->in_ppip != 0)) {
1269 port = ntohs(sport) + l;
1270 port %= np->in_ppip;
1271 port += np->in_ppip *
1272 (inb.s_addr % np->in_ippip);
1273 port += MAPBLK_MINPORT;
1276 } else if (!np->in_outip &&
1277 (np->in_outmsk == 0xffffffff)) {
1279 * 0/32 - use the interface's IP address.
1282 fr_ifpaddr(4, fin->fin_ifp, &in) == -1)
1284 in.s_addr = ntohl(in.s_addr);
1285 } else if (!np->in_outip && !np->in_outmsk) {
1287 * 0/0 - use the original source address/port.
1291 in.s_addr = ntohl(fin->fin_saddr);
1292 } else if ((np->in_outmsk != 0xffffffff) &&
1293 (np->in_pnext == 0) &&
1294 ((l > 0) || (hm == NULL)))
1298 if ((nflags & IPN_TCPUDP) &&
1299 ((np->in_redir & NAT_MAPBLK) == 0) &&
1300 (np->in_flags & IPN_AUTOPORTMAP)) {
1301 if ((l > 0) && (l % np->in_ppip == 0)) {
1302 if (l > np->in_space) {
1304 } else if ((l > np->in_ppip) &&
1305 np->in_outmsk != 0xffffffff)
1308 if (np->in_ppip != 0) {
1309 port = ntohs(sport);
1310 port += (l % np->in_ppip);
1311 port %= np->in_ppip;
1312 port += np->in_ppip *
1313 (ntohl(fin->fin_saddr) %
1315 port += MAPBLK_MINPORT;
1318 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
1319 (nflags & IPN_TCPUDP) &&
1320 (np->in_pnext != 0)) {
1321 port = htons(np->in_pnext++);
1322 if (np->in_pnext > ntohs(np->in_pmax)) {
1323 np->in_pnext = ntohs(np->in_pmin);
1324 if (np->in_outmsk != 0xffffffff)
1329 if (np->in_flags & IPN_IPRANGE) {
1330 if (np->in_nip > ntohl(np->in_outmsk))
1331 np->in_nip = ntohl(np->in_outip);
1333 if ((np->in_outmsk != 0xffffffff) &&
1334 ((np->in_nip + 1) & ntohl(np->in_outmsk)) >
1335 ntohl(np->in_outip))
1336 np->in_nip = ntohl(np->in_outip) + 1;
1339 if (!port && (flags & IPN_TCPUDP))
1343 * Here we do a lookup of the connection as seen from
1344 * the outside. If an IP# pair already exists, try
1345 * again. So if you have A->B becomes C->B, you can
1346 * also have D->E become C->E but not D->B causing
1347 * another C->B. Also take protocol and ports into
1348 * account when determining whether a pre-existing
1349 * NAT setup will cause an external conflict where
1350 * this is appropriate.
1352 inb.s_addr = htonl(in.s_addr);
1353 sp = fin->fin_data[0];
1354 dp = fin->fin_data[1];
1355 fin->fin_data[0] = fin->fin_data[1];
1356 fin->fin_data[1] = htons(port);
1357 natl = nat_inlookup(fin, flags & ~FI_WILDP,
1358 (u_int)fin->fin_p, fin->fin_dst,
1360 fin->fin_data[0] = sp;
1361 fin->fin_data[1] = dp;
1364 * Has the search wrapped around and come back to the
1367 if ((natl != NULL) &&
1368 (np->in_pnext != 0) && (st_port == np->in_pnext) &&
1369 (np->in_nip != 0) && (st_ip == np->in_nip))
1372 } while (natl != NULL);
1374 if (np->in_space > 0)
1377 /* Setup the NAT table */
1378 nat->nat_inip = fin->fin_src;
1379 nat->nat_outip.s_addr = htonl(in.s_addr);
1380 nat->nat_oip = fin->fin_dst;
1381 if (nat->nat_hm == NULL)
1382 nat->nat_hm = nat_hostmap(np, fin->fin_src,
1385 sum1 = LONG_SUM(ntohl(fin->fin_saddr)) + ntohs(sport);
1386 sum2 = LONG_SUM(in.s_addr) + ntohs(port);
1388 if (flags & IPN_TCPUDP) {
1389 nat->nat_inport = sport;
1390 nat->nat_outport = port; /* sport */
1391 nat->nat_oport = dport;
1395 * Otherwise, it's an inbound packet. Most likely, we don't
1396 * want to rewrite source ports and source addresses. Instead,
1397 * we want to rewrite to a fixed internal address and fixed
1400 if (np->in_flags & IPN_SPLIT) {
1401 in.s_addr = np->in_nip;
1402 if (np->in_inip == htonl(in.s_addr))
1403 np->in_nip = ntohl(np->in_inmsk);
1405 np->in_nip = ntohl(np->in_inip);
1406 if (np->in_flags & IPN_ROUNDR) {
1412 in.s_addr = ntohl(np->in_inip);
1413 if (np->in_flags & IPN_ROUNDR) {
1422 * Whilst not optimized for the case where
1423 * pmin == pmax, the gain is not significant.
1425 if (np->in_pmin != np->in_pmax) {
1426 nport = ntohs(dport) - ntohs(np->in_pmin) +
1427 ntohs(np->in_pnext);
1428 nport = ntohs(nport);
1430 nport = np->in_pnext;
1434 * When the redirect-to address is set to 0.0.0.0, just
1435 * assume a blank `forwarding' of the packet.
1438 in.s_addr = ntohl(fin->fin_daddr);
1440 nat->nat_inip.s_addr = htonl(in.s_addr);
1441 nat->nat_outip = fin->fin_dst;
1442 nat->nat_oip = fin->fin_src;
1444 sum1 = LONG_SUM(ntohl(fin->fin_daddr)) + ntohs(dport);
1445 sum2 = LONG_SUM(in.s_addr) + ntohs(nport);
1447 if (flags & IPN_TCPUDP) {
1448 nat->nat_inport = nport;
1449 nat->nat_outport = dport;
1450 nat->nat_oport = sport;
1454 CALC_SUMD(sum1, sum2, sumd);
1455 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1456 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
1457 if ((flags & IPN_TCP) && dohwcksum &&
1458 (qf->qf_ill->ill_ick.ick_magic == ICK_M_CTL_MAGIC)) {
1459 if (direction == NAT_OUTBOUND)
1460 sum1 = LONG_SUM(ntohl(in.s_addr));
1462 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1463 sum1 += LONG_SUM(ntohl(fin->fin_daddr));
1464 sum1 += IPPROTO_TCP;
1465 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
1466 nat->nat_sumd[1] = NAT_HW_CKSUM|(sum1 & 0xffff);
1469 nat->nat_sumd[1] = nat->nat_sumd[0];
1471 if ((flags & IPN_TCPUDP) && ((sport != port) || (dport != nport))) {
1472 if (direction == NAT_OUTBOUND)
1473 sum1 = LONG_SUM(ntohl(fin->fin_saddr));
1475 sum1 = LONG_SUM(ntohl(fin->fin_daddr));
1477 sum2 = LONG_SUM(in.s_addr);
1479 CALC_SUMD(sum1, sum2, sumd);
1480 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
1482 nat->nat_ipsumd = nat->nat_sumd[0];
1484 in.s_addr = htonl(in.s_addr);
1486 strncpy(nat->nat_ifname, IFNAME(fin->fin_ifp), IFNAMSIZ);
1488 nat->nat_me = natsave;
1489 nat->nat_dir = direction;
1490 nat->nat_ifp = fin->fin_ifp;
1492 nat->nat_p = fin->fin_p;
1495 nat->nat_mssclamp = np->in_mssclamp;
1496 nat->nat_fr = fin->fin_fr;
1497 if (nat->nat_fr != NULL) {
1498 ATOMIC_INC32(nat->nat_fr->fr_ref);
1500 if (direction == NAT_OUTBOUND) {
1501 if (flags & IPN_TCPUDP)
1502 tcp->th_sport = port;
1504 if (flags & IPN_TCPUDP)
1505 tcp->th_dport = nport;
1510 if ((np->in_apr != NULL) && (np->in_dport == 0 ||
1511 (tcp != NULL && dport == np->in_dport)))
1512 (void) appr_new(fin, ip, nat);
1516 nat_log(nat, (u_int)np->in_redir);
1520 nat_stats.ns_badnat++;
1521 if ((hm = nat->nat_hm) != NULL)
1529 * Insert a NAT entry into the hash tables for searching and add it to the
1530 * list of active NAT entries. Adjust global counters when complete.
1532 void nat_insert(nat)
1538 MUTEX_INIT(&nat->nat_lock, "nat entry lock", NULL);
1540 nat->nat_age = fr_defnatage;
1541 nat->nat_ifname[sizeof(nat->nat_ifname) - 1] = '\0';
1542 if (nat->nat_ifname[0] !='\0') {
1543 nat->nat_ifp = GETUNIT(nat->nat_ifname, 4);
1546 nat->nat_next = nat_instances;
1547 nat_instances = nat;
1549 if (!(nat->nat_flags & (FI_W_SPORT|FI_W_DPORT))) {
1550 hv1 = NAT_HASH_FN(nat->nat_inip.s_addr, nat->nat_inport,
1552 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, hv1 + nat->nat_oport,
1554 hv2 = NAT_HASH_FN(nat->nat_outip.s_addr, nat->nat_outport,
1556 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, hv2 + nat->nat_oport,
1559 hv1 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_inip.s_addr,
1561 hv2 = NAT_HASH_FN(nat->nat_oip.s_addr, nat->nat_outip.s_addr,
1565 natp = &nat_table[0][hv1];
1567 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
1568 nat->nat_phnext[0] = natp;
1569 nat->nat_hnext[0] = *natp;
1572 natp = &nat_table[1][hv2];
1574 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
1575 nat->nat_phnext[1] = natp;
1576 nat->nat_hnext[1] = *natp;
1579 nat_stats.ns_added++;
1580 nat_stats.ns_inuse++;
1584 nat_t *nat_icmplookup(ip, fin, dir)
1590 tcphdr_t *tcp = NULL;
1592 int flags = 0, type, minlen;
1594 icmp = (icmphdr_t *)fin->fin_dp;
1596 * Does it at least have the return (basic) IP header ?
1597 * Only a basic IP header (no options) should be with an ICMP error
1600 if ((ip->ip_hl != 5) || (ip->ip_len < ICMPERR_MINPKTLEN))
1602 type = icmp->icmp_type;
1604 * If it's not an error type, then return.
1606 if ((type != ICMP_UNREACH) && (type != ICMP_SOURCEQUENCH) &&
1607 (type != ICMP_REDIRECT) && (type != ICMP_TIMXCEED) &&
1608 (type != ICMP_PARAMPROB))
1611 oip = (ip_t *)((char *)fin->fin_dp + 8);
1612 minlen = (oip->ip_hl << 2);
1613 if (minlen < sizeof(ip_t))
1615 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1618 * Is the buffer big enough for all of it ? It's the size of the IP
1619 * header claimed in the encapsulated part which is of concern. It
1620 * may be too big to be in this buffer but not so big that it's
1621 * outside the ICMP packet, leading to TCP deref's causing problems.
1622 * This is possible because we don't know how big oip_hl is when we
1623 * do the pullup early in fr_check() and thus can't gaurantee it is
1632 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > (char *)m->b_wptr)
1635 m = *(mb_t **)fin->fin_mp;
1636 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
1637 (char *)ip + m->m_len)
1643 if (oip->ip_p == IPPROTO_TCP)
1645 else if (oip->ip_p == IPPROTO_UDP)
1647 if (flags & IPN_TCPUDP) {
1651 minlen += 8; /* + 64bits of data to get ports */
1652 if (ip->ip_len < ICMPERR_IPICMPHLEN + minlen)
1655 data[0] = fin->fin_data[0];
1656 data[1] = fin->fin_data[1];
1657 tcp = (tcphdr_t *)((char *)oip + (oip->ip_hl << 2));
1658 fin->fin_data[0] = ntohs(tcp->th_dport);
1659 fin->fin_data[1] = ntohs(tcp->th_sport);
1661 if (dir == NAT_INBOUND) {
1662 nat = nat_inlookup(fin, flags, (u_int)oip->ip_p,
1663 oip->ip_dst, oip->ip_src, 0);
1665 nat = nat_outlookup(fin, flags, (u_int)oip->ip_p,
1666 oip->ip_dst, oip->ip_src, 0);
1668 fin->fin_data[0] = data[0];
1669 fin->fin_data[1] = data[1];
1672 if (dir == NAT_INBOUND)
1673 return nat_inlookup(fin, 0, (u_int)oip->ip_p,
1674 oip->ip_dst, oip->ip_src, 0);
1676 return nat_outlookup(fin, 0, (u_int)oip->ip_p,
1677 oip->ip_dst, oip->ip_src, 0);
1682 * This should *ONLY* be used for incoming packets to make sure a NAT'd ICMP
1683 * packet gets correctly recognised.
1685 nat_t *nat_icmp(ip, fin, nflags, dir)
1691 u_32_t sum1, sum2, sumd, sumd2 = 0;
1700 if ((fin->fin_fl & FI_SHORT) || (fin->fin_off != 0))
1703 * nat_icmplookup() will return NULL for `defective' packets.
1705 if ((ip->ip_v != 4) || !(nat = nat_icmplookup(ip, fin, dir)))
1710 *nflags = IPN_ICMPERR;
1711 icmp = (icmphdr_t *)fin->fin_dp;
1712 oip = (ip_t *)&icmp->icmp_ip;
1713 if (oip->ip_p == IPPROTO_TCP)
1715 else if (oip->ip_p == IPPROTO_UDP)
1717 udp = (udphdr_t *)((((char *)oip) + (oip->ip_hl << 2)));
1718 dlen = ip->ip_len - ((char *)udp - (char *)ip);
1720 * XXX - what if this is bogus hl and we go off the end ?
1721 * In this case, nat_icmplookup() will have returned NULL.
1723 tcp = (tcphdr_t *)udp;
1726 * Need to adjust ICMP header to include the real IP#'s and
1727 * port #'s. Only apply a checksum change relative to the
1728 * IP address change as it will be modified again in ip_natout
1729 * for both address and port. Two checksum changes are
1730 * necessary for the two header address changes. Be careful
1731 * to only modify the checksum once for the port # and twice
1737 * Fix the IP addresses in the offending IP packet. You also need
1738 * to adjust the IP header checksum of that offending IP packet
1739 * and the ICMP checksum of the ICMP error message itself.
1741 * Unfortunately, for UDP and TCP, the IP addresses are also contained
1742 * in the pseudo header that is used to compute the UDP resp. TCP
1743 * checksum. So, we must compensate that as well. Even worse, the
1744 * change in the UDP and TCP checksums require yet another
1745 * adjustment of the ICMP checksum of the ICMP error message.
1749 if (oip->ip_dst.s_addr == nat->nat_oip.s_addr) {
1750 sum1 = LONG_SUM(ntohl(oip->ip_src.s_addr));
1754 sum1 = LONG_SUM(ntohl(oip->ip_dst.s_addr));
1755 in = nat->nat_outip;
1759 sum2 = LONG_SUM(ntohl(in.s_addr));
1761 CALC_SUMD(sum1, sum2, sumd);
1764 * Fix IP checksum of the offending IP packet to adjust for
1765 * the change in the IP address.
1767 * Normally, you would expect that the ICMP checksum of the
1768 * ICMP error message needs to be adjusted as well for the
1769 * IP address change in oip.
1770 * However, this is a NOP, because the ICMP checksum is
1771 * calculated over the complete ICMP packet, which includes the
1772 * changed oip IP addresses and oip->ip_sum. However, these
1773 * two changes cancel each other out (if the delta for
1774 * the IP address is x, then the delta for ip_sum is minus x),
1775 * so no change in the icmp_cksum is necessary.
1777 * Be careful that nat_dir refers to the direction of the
1778 * offending IP packet (oip), not to its ICMP response (icmp)
1780 fix_datacksum(&oip->ip_sum, sumd);
1781 /* Fix icmp cksum : IP Addr + Cksum */
1784 * Fix UDP pseudo header checksum to compensate for the
1785 * IP address change.
1787 if ((oip->ip_p == IPPROTO_UDP) && (dlen >= 8) && udp->uh_sum) {
1789 * The UDP checksum is optional, only adjust it
1790 * if it has been set.
1792 sum1 = ntohs(udp->uh_sum);
1793 fix_datacksum(&udp->uh_sum, sumd);
1794 sum2 = ntohs(udp->uh_sum);
1797 * Fix ICMP checksum to compensate the UDP
1798 * checksum adjustment.
1801 CALC_SUMD(sum1, sum2, sumd);
1806 * Fix TCP pseudo header checksum to compensate for the
1807 * IP address change. Before we can do the change, we
1808 * must make sure that oip is sufficient large to hold
1809 * the TCP checksum (normally it does not!).
1811 else if ((oip->ip_p == IPPROTO_TCP) && (dlen >= 18)) {
1812 sum1 = ntohs(tcp->th_sum);
1813 fix_datacksum(&tcp->th_sum, sumd);
1814 sum2 = ntohs(tcp->th_sum);
1817 * Fix ICMP checksum to compensate the TCP
1818 * checksum adjustment.
1821 CALC_SUMD(sum1, sum2, sumd);
1824 sumd2 = (sumd >> 16);
1825 if (nat->nat_dir == NAT_OUTBOUND)
1831 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
1834 * For offending TCP/UDP IP packets, translate the ports as
1835 * well, based on the NAT specification. Of course such
1836 * a change must be reflected in the ICMP checksum as well.
1838 * Advance notice : Now it becomes complicated :-)
1840 * Since the port fields are part of the TCP/UDP checksum
1841 * of the offending IP packet, you need to adjust that checksum
1842 * as well... but, if you change, you must change the icmp
1843 * checksum *again*, to reflect that change.
1845 * To further complicate: the TCP checksum is not in the first
1846 * 8 bytes of the offending ip packet, so it most likely is not
1847 * available. Some OSses like Solaris return enough bytes to
1848 * include the TCP checksum. So we have to check if the
1849 * ip->ip_len actually holds the TCP checksum of the oip!
1851 if (nat->nat_oport == tcp->th_dport) {
1852 if (tcp->th_sport != nat->nat_inport) {
1854 * Fix ICMP checksum to compensate port
1857 sum1 = ntohs(nat->nat_inport);
1858 sum2 = ntohs(tcp->th_sport);
1859 tcp->th_sport = nat->nat_inport;
1862 * Fix udp checksum to compensate port
1863 * adjustment. NOTE : the offending IP packet
1864 * flows the other direction compared to the
1867 * The UDP checksum is optional, only adjust
1868 * it if it has been set.
1870 if ((oip->ip_p == IPPROTO_UDP) &&
1871 (dlen >= 8) && udp->uh_sum) {
1875 sum1 = ntohs(udp->uh_sum);
1876 fix_datacksum(&udp->uh_sum, sumd);
1877 sum2 = ntohs(udp->uh_sum);
1880 * Fix ICMP checksum to compensate
1881 * UDP checksum adjustment.
1883 CALC_SUMD(sum1, sum2, sumd);
1888 * Fix tcp checksum (if present) to compensate
1889 * port adjustment. NOTE : the offending IP
1890 * packet flows the other direction compared to
1893 if (oip->ip_p == IPPROTO_TCP) {
1898 sum1 = ntohs(tcp->th_sum);
1899 fix_datacksum(&tcp->th_sum,
1901 sum2 = ntohs(tcp->th_sum);
1904 * Fix ICMP checksum to
1905 * compensate TCP checksum
1908 CALC_SUMD(sum1, sum2, sumd);
1911 sumd = sum2 - sum1 + 1;
1916 } else if (tcp->th_dport != nat->nat_outport) {
1918 * Fix ICMP checksum to compensate port
1921 sum1 = ntohs(nat->nat_outport);
1922 sum2 = ntohs(tcp->th_dport);
1923 tcp->th_dport = nat->nat_outport;
1926 * Fix udp checksum to compensate port
1927 * adjustment. NOTE : the offending IP
1928 * packet flows the other direction compared
1929 * to the ICMP message.
1931 * The UDP checksum is optional, only adjust
1932 * it if it has been set.
1934 if ((oip->ip_p == IPPROTO_UDP) &&
1935 (dlen >= 8) && udp->uh_sum) {
1939 sum1 = ntohs(udp->uh_sum);
1940 fix_datacksum(&udp->uh_sum, sumd);
1941 sum2 = ntohs(udp->uh_sum);
1944 * Fix ICMP checksum to compensate
1945 * UDP checksum adjustment.
1947 CALC_SUMD(sum1, sum2, sumd);
1951 * Fix tcp checksum (if present) to compensate
1952 * port adjustment. NOTE : the offending IP
1953 * packet flows the other direction compared to
1956 if (oip->ip_p == IPPROTO_TCP) {
1961 sum1 = ntohs(tcp->th_sum);
1962 fix_datacksum(&tcp->th_sum, sumd);
1963 sum2 = ntohs(tcp->th_sum);
1966 * Fix ICMP checksum to compensate
1967 * UDP checksum adjustment.
1969 CALC_SUMD(sum1, sum2, sumd);
1972 if (nat->nat_dir == NAT_OUTBOUND)
1979 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1980 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1981 fix_incksum(fin, &icmp->icmp_cksum, sumd2);
1984 if (oip->ip_p == IPPROTO_ICMP)
1985 nat->nat_age = fr_defnaticmpage;
1991 * NB: these lookups don't lock access to the list, it assume it has already
1995 * Lookup a nat entry based on the mapped destination ip address/port and
1996 * real source address/port. We use this lookup when receiving a packet,
1997 * we're looking for a table entry, based on the destination address.
1998 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2000 nat_t *nat_inlookup(fin, flags, p, src, mapdst, rw)
2003 struct in_addr src , mapdst;
2006 u_short sport, dport;
2018 dst = mapdst.s_addr;
2019 if (flags & IPN_TCPUDP) {
2020 sport = htons(fin->fin_data[0]);
2021 dport = htons(fin->fin_data[1]);
2027 hv = NAT_HASH_FN(dst, dport, 0xffffffff);
2028 hv = NAT_HASH_FN(src.s_addr, hv + sport, ipf_nattable_sz);
2029 nat = nat_table[1][hv];
2030 for (; nat; nat = nat->nat_hnext[1]) {
2031 nflags = nat->nat_flags;
2032 if ((!ifp || ifp == nat->nat_ifp) &&
2033 nat->nat_oip.s_addr == src.s_addr &&
2034 nat->nat_outip.s_addr == dst &&
2035 ((p == 0) || (p == nat->nat_p))) {
2040 if (nat->nat_oport != sport)
2042 if (nat->nat_outport != dport)
2050 if ((ipn != NULL) && (nat->nat_aps != NULL))
2051 if (appr_match(fin, nat) != 0)
2056 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2059 RWLOCK_EXIT(&ipf_nat);
2061 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
2062 hv = NAT_HASH_FN(src.s_addr, dst, ipf_nattable_sz);
2064 WRITE_ENTER(&ipf_nat);
2066 nat = nat_table[1][hv];
2067 for (; nat; nat = nat->nat_hnext[1]) {
2068 nflags = nat->nat_flags;
2069 if (ifp && ifp != nat->nat_ifp)
2071 if (!(nflags & FI_WILDP))
2073 if (nat->nat_oip.s_addr != src.s_addr ||
2074 nat->nat_outip.s_addr != dst)
2076 if (((nat->nat_oport == sport) || (nflags & FI_W_DPORT)) &&
2077 ((nat->nat_outport == dport) || (nflags & FI_W_SPORT))) {
2078 nat_tabmove(fin, nat);
2083 MUTEX_DOWNGRADE(&ipf_nat);
2090 * This function is only called for TCP/UDP NAT table entries where the
2091 * original was placed in the table without hashing on the ports and we now
2092 * want to include hashing on port numbers.
2094 static void nat_tabmove(fin, nat)
2098 u_short sport, dport;
2102 nflags = nat->nat_flags;
2104 sport = ntohs(fin->fin_data[0]);
2105 dport = ntohs(fin->fin_data[1]);
2108 * Remove the NAT entry from the old location
2110 if (nat->nat_hnext[0])
2111 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2112 *nat->nat_phnext[0] = nat->nat_hnext[0];
2114 if (nat->nat_hnext[1])
2115 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2116 *nat->nat_phnext[1] = nat->nat_hnext[1];
2119 * Add into the NAT table in the new position
2121 hv = NAT_HASH_FN(nat->nat_inip.s_addr, sport, 0xffffffff);
2122 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2123 natp = &nat_table[0][hv];
2125 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2126 nat->nat_phnext[0] = natp;
2127 nat->nat_hnext[0] = *natp;
2130 hv = NAT_HASH_FN(nat->nat_outip.s_addr, sport, 0xffffffff);
2131 hv = NAT_HASH_FN(nat->nat_oip.s_addr, hv + dport, ipf_nattable_sz);
2132 natp = &nat_table[1][hv];
2134 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2135 nat->nat_phnext[1] = natp;
2136 nat->nat_hnext[1] = *natp;
2142 * Lookup a nat entry based on the source 'real' ip address/port and
2143 * destination address/port. We use this lookup when sending a packet out,
2144 * we're looking for a table entry, based on the source address.
2145 * NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY.
2147 nat_t *nat_outlookup(fin, flags, p, src, dst, rw)
2150 struct in_addr src , dst;
2153 u_short sport, dport;
2163 if (flags & IPN_TCPUDP) {
2164 sport = ntohs(fin->fin_data[0]);
2165 dport = ntohs(fin->fin_data[1]);
2171 hv = NAT_HASH_FN(srcip, sport, 0xffffffff);
2172 hv = NAT_HASH_FN(dst.s_addr, hv + dport, ipf_nattable_sz);
2173 nat = nat_table[0][hv];
2174 for (; nat; nat = nat->nat_hnext[0]) {
2175 nflags = nat->nat_flags;
2177 if ((!ifp || ifp == nat->nat_ifp) &&
2178 nat->nat_inip.s_addr == srcip &&
2179 nat->nat_oip.s_addr == dst.s_addr &&
2180 ((p == 0) || (p == nat->nat_p))) {
2185 if (nat->nat_oport != dport)
2187 if (nat->nat_inport != sport)
2195 if ((ipn != NULL) && (nat->nat_aps != NULL))
2196 if (appr_match(fin, nat) != 0)
2201 if (!nat_stats.ns_wilds || !(flags & FI_WILDP))
2204 RWLOCK_EXIT(&ipf_nat);
2207 hv = NAT_HASH_FN(dst.s_addr, srcip, ipf_nattable_sz);
2209 WRITE_ENTER(&ipf_nat);
2211 nat = nat_table[0][hv];
2212 for (; nat; nat = nat->nat_hnext[0]) {
2213 nflags = nat->nat_flags;
2214 if (ifp && ifp != nat->nat_ifp)
2216 if (!(nflags & FI_WILDP))
2218 if ((nat->nat_inip.s_addr != srcip) ||
2219 (nat->nat_oip.s_addr != dst.s_addr))
2221 if (((nat->nat_inport == sport) || (nflags & FI_W_SPORT)) &&
2222 ((nat->nat_oport == dport) || (nflags & FI_W_DPORT))) {
2223 nat_tabmove(fin, nat);
2228 MUTEX_DOWNGRADE(&ipf_nat);
2235 * Lookup the NAT tables to search for a matching redirect
2237 nat_t *nat_lookupredir(np)
2243 bzero((char *)&fi, sizeof(fi));
2244 fi.fin_data[0] = ntohs(np->nl_inport);
2245 fi.fin_data[1] = ntohs(np->nl_outport);
2248 * If nl_inip is non null, this is a lookup based on the real
2249 * ip address. Else, we use the fake.
2251 if ((nat = nat_outlookup(&fi, np->nl_flags, 0, np->nl_inip,
2252 np->nl_outip, 0))) {
2253 np->nl_realip = nat->nat_outip;
2254 np->nl_realport = nat->nat_outport;
2260 static int nat_match(fin, np, ip)
2270 if (np->in_p && fin->fin_p != np->in_p)
2273 if (!(np->in_redir & (NAT_MAP|NAT_MAPBLK)))
2275 if (((fin->fin_fi.fi_saddr & np->in_inmsk) != np->in_inip)
2276 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2278 if (((fin->fin_fi.fi_daddr & np->in_srcmsk) != np->in_srcip)
2279 ^ ((np->in_flags & IPN_NOTDST) != 0))
2282 if (!(np->in_redir & NAT_REDIRECT))
2284 if (((fin->fin_fi.fi_saddr & np->in_srcmsk) != np->in_srcip)
2285 ^ ((np->in_flags & IPN_NOTSRC) != 0))
2287 if (((fin->fin_fi.fi_daddr & np->in_outmsk) != np->in_outip)
2288 ^ ((np->in_flags & IPN_NOTDST) != 0))
2293 if (!(fin->fin_fl & FI_TCPUDP) ||
2294 (fin->fin_fl & FI_SHORT) || (fin->fin_off != 0)) {
2295 if (ft->ftu_scmp || ft->ftu_dcmp)
2300 return fr_tcpudpchk(ft, fin);
2305 * Packets going out on the external interface go through this.
2306 * Here, the source address requires alteration, if anything.
2308 int ip_natout(ip, fin)
2314 tcphdr_t *tcp = NULL;
2315 u_short sport = 0, dport = 0, *csump = NULL;
2316 int natadd = 1, i, icmpset = 1;
2317 u_int nflags = 0, hv, msk;
2324 if (nat_list == NULL || (fr_nat_lock))
2327 if ((fr = fin->fin_fr) && !(fr->fr_flags & FR_DUP) &&
2328 fr->fr_tif.fd_ifp && fr->fr_tif.fd_ifp != (void *)-1) {
2329 sifp = fin->fin_ifp;
2330 fin->fin_ifp = fr->fr_tif.fd_ifp;
2332 sifp = fin->fin_ifp;
2335 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2336 if (fin->fin_p == IPPROTO_TCP)
2338 else if (fin->fin_p == IPPROTO_UDP)
2340 if ((nflags & IPN_TCPUDP)) {
2341 tcp = (tcphdr_t *)fin->fin_dp;
2342 sport = tcp->th_sport;
2343 dport = tcp->th_dport;
2347 ipa = fin->fin_saddr;
2349 READ_ENTER(&ipf_nat);
2351 if ((fin->fin_p == IPPROTO_ICMP) &&
2352 (nat = nat_icmp(ip, fin, &nflags, NAT_OUTBOUND)))
2354 else if ((fin->fin_fl & FI_FRAG) &&
2355 (nat = ipfr_nat_knownfrag(ip, fin)))
2357 else if ((nat = nat_outlookup(fin, nflags|FI_WILDP|FI_WILDA,
2358 (u_int)fin->fin_p, fin->fin_src,
2359 fin->fin_dst, 0))) {
2360 nflags = nat->nat_flags;
2361 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2362 if ((nflags & FI_W_SPORT) &&
2363 (nat->nat_inport != sport))
2364 nat->nat_inport = sport;
2365 if ((nflags & FI_W_DPORT) &&
2366 (nat->nat_oport != dport))
2367 nat->nat_oport = dport;
2369 if (nat->nat_outport == 0)
2370 nat->nat_outport = sport;
2371 nat->nat_flags &= ~(FI_W_DPORT|FI_W_SPORT);
2372 nflags = nat->nat_flags;
2373 nat_stats.ns_wilds--;
2376 RWLOCK_EXIT(&ipf_nat);
2381 WRITE_ENTER(&ipf_nat);
2383 * If there is no current entry in the nat table for this IP#,
2384 * create one for it (if there is a matching rule).
2387 iph = ipa & htonl(msk);
2388 hv = NAT_HASH_FN(iph, 0, ipf_natrules_sz);
2389 for (np = nat_rules[hv]; np; np = np->in_mnext)
2391 if (np->in_ifp && (np->in_ifp != ifp))
2393 if ((np->in_flags & IPN_RF) &&
2394 !(np->in_flags & nflags))
2396 if (np->in_flags & IPN_FILTER) {
2397 if (!nat_match(fin, np, ip))
2399 } else if ((ipa & np->in_inmsk) != np->in_inip)
2401 if (*np->in_plabel && !appr_ok(ip, tcp, np))
2403 nat = nat_new(fin, ip, np, NULL,
2404 (u_int)nflags, NAT_OUTBOUND);
2410 if ((np == NULL) && (i > 0)) {
2414 } while ((i >= 0) && ((nat_masks & (1 << i)) == 0));
2418 MUTEX_DOWNGRADE(&ipf_nat);
2422 * NOTE: ipf_nat must now only be held as a read lock
2426 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2427 ipfr_nat_newfrag(ip, fin, nat);
2428 MUTEX_ENTER(&nat->nat_lock);
2429 if (fin->fin_p != IPPROTO_TCP) {
2430 if (np && np->in_age[1])
2431 nat->nat_age = np->in_age[1];
2432 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2433 nat->nat_age = fr_defnaticmpage;
2435 nat->nat_age = fr_defnatage;
2437 nat->nat_bytes += ip->ip_len;
2439 MUTEX_EXIT(&nat->nat_lock);
2442 * Fix up checksums, not by recalculating them, but
2443 * simply computing adjustments.
2445 if (nflags == IPN_ICMPERR) {
2446 u_32_t s1, s2, sumd;
2448 s1 = LONG_SUM(ntohl(fin->fin_saddr));
2449 s2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
2450 CALC_SUMD(s1, s2, sumd);
2451 fix_outcksum(fin, &ip->ip_sum, sumd);
2453 #if (SOLARIS || defined(__sgi)) || !defined(_KERNEL)
2455 if (nat->nat_dir == NAT_OUTBOUND)
2456 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2458 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2462 * Only change the packet contents, not what is filtered upon.
2464 ip->ip_src = nat->nat_outip;
2466 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2468 if ((nat->nat_outport != 0) && (tcp != NULL)) {
2469 tcp->th_sport = nat->nat_outport;
2470 fin->fin_data[0] = ntohs(tcp->th_sport);
2473 if (fin->fin_p == IPPROTO_TCP) {
2474 csump = &tcp->th_sum;
2475 MUTEX_ENTER(&nat->nat_lock);
2476 fr_tcp_age(&nat->nat_age,
2477 nat->nat_tcpstate, fin, 1, 0);
2478 if (nat->nat_age < fr_defnaticmpage)
2479 nat->nat_age = fr_defnaticmpage;
2481 else if ((!np || !np->in_age[1]) &&
2482 (nat->nat_age > fr_defnatage))
2483 nat->nat_age = fr_defnatage;
2486 * Increase this because we may have
2487 * "keep state" following this too and
2488 * packet storms can occur if this is
2489 * removed too quickly.
2491 if (nat->nat_age == fr_tcpclosed)
2492 nat->nat_age = fr_tcplastack;
2495 * Do a MSS CLAMPING on a SYN packet,
2496 * only deal IPv4 for now.
2498 if (nat->nat_mssclamp &&
2499 (tcp->th_flags & TH_SYN) != 0)
2500 nat_mssclamp(tcp, nat->nat_mssclamp,
2503 MUTEX_EXIT(&nat->nat_lock);
2504 } else if (fin->fin_p == IPPROTO_UDP) {
2505 udphdr_t *udp = (udphdr_t *)tcp;
2508 csump = &udp->uh_sum;
2512 if (nat->nat_dir == NAT_OUTBOUND)
2513 fix_outcksum(fin, csump,
2516 fix_incksum(fin, csump,
2521 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2522 (tcp != NULL && dport == np->in_dport))) {
2523 i = appr_check(ip, fin, nat);
2530 ATOMIC_INCL(nat_stats.ns_mapped[1]);
2531 RWLOCK_EXIT(&ipf_nat); /* READ */
2532 fin->fin_ifp = sifp;
2535 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2536 fin->fin_ifp = sifp;
2542 * Packets coming in from the external interface go through this.
2543 * Here, the destination address requires alteration, if anything.
2545 int ip_natin(ip, fin)
2552 u_short sport = 0, dport = 0, *csump = NULL;
2553 u_int nflags = 0, natadd = 1, hv, msk;
2554 struct ifnet *ifp = fin->fin_ifp;
2555 tcphdr_t *tcp = NULL;
2560 if ((nat_list == NULL) || (ip->ip_v != 4) || (fr_nat_lock))
2563 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2564 if (fin->fin_p == IPPROTO_TCP)
2566 else if (fin->fin_p == IPPROTO_UDP)
2568 if ((nflags & IPN_TCPUDP)) {
2569 tcp = (tcphdr_t *)fin->fin_dp;
2570 sport = tcp->th_sport;
2571 dport = tcp->th_dport;
2576 /* make sure the source address is to be redirected */
2579 READ_ENTER(&ipf_nat);
2581 if ((fin->fin_p == IPPROTO_ICMP) &&
2582 (nat = nat_icmp(ip, fin, &nflags, NAT_INBOUND)))
2584 else if ((fin->fin_fl & FI_FRAG) &&
2585 (nat = ipfr_nat_knownfrag(ip, fin)))
2587 else if ((nat = nat_inlookup(fin, nflags|FI_WILDP|FI_WILDA,
2588 (u_int)fin->fin_p, fin->fin_src, in, 0))) {
2589 nflags = nat->nat_flags;
2590 if ((nflags & (FI_W_SPORT|FI_W_DPORT)) != 0) {
2591 if ((nat->nat_oport != sport) && (nflags & FI_W_DPORT))
2592 nat->nat_oport = sport;
2593 if ((nat->nat_outport != dport) &&
2594 (nflags & FI_W_SPORT))
2595 nat->nat_outport = dport;
2596 nat->nat_flags &= ~(FI_W_SPORT|FI_W_DPORT);
2597 nflags = nat->nat_flags;
2598 nat_stats.ns_wilds--;
2601 RWLOCK_EXIT(&ipf_nat);
2606 WRITE_ENTER(&ipf_nat);
2608 * If there is no current entry in the nat table for this IP#,
2609 * create one for it (if there is a matching rule).
2612 iph = in.s_addr & htonl(msk);
2613 hv = NAT_HASH_FN(iph, 0, ipf_rdrrules_sz);
2614 for (np = rdr_rules[hv]; np; np = np->in_rnext) {
2615 if ((np->in_ifp && (np->in_ifp != ifp)) ||
2616 (np->in_p && (np->in_p != fin->fin_p)) ||
2617 (np->in_flags && !(nflags & np->in_flags)))
2619 if (np->in_flags & IPN_FILTER) {
2620 if (!nat_match(fin, np, ip))
2622 } else if ((in.s_addr & np->in_outmsk) != np->in_outip)
2624 if ((!np->in_pmin || (np->in_flags & IPN_FILTER) ||
2625 ((ntohs(np->in_pmax) >= ntohs(dport)) &&
2626 (ntohs(dport) >= ntohs(np->in_pmin)))))
2627 if ((nat = nat_new(fin, ip, np, NULL, nflags,
2634 if ((np == NULL) && (i > 0)) {
2638 } while ((i >= 0) && ((rdr_masks & (1 << i)) == 0));
2642 MUTEX_DOWNGRADE(&ipf_nat);
2646 * NOTE: ipf_nat must now only be held as a read lock
2650 fin->fin_fr = nat->nat_fr;
2651 if (natadd && (fin->fin_fl & FI_FRAG) && np)
2652 ipfr_nat_newfrag(ip, fin, nat);
2653 if (np && (np->in_apr != NULL) && (np->in_dport == 0 ||
2654 (tcp != NULL && sport == np->in_dport))) {
2655 i = appr_check(ip, fin, nat);
2658 RWLOCK_EXIT(&ipf_nat);
2663 MUTEX_ENTER(&nat->nat_lock);
2664 if (fin->fin_p != IPPROTO_TCP) {
2665 if (np && np->in_age[0])
2666 nat->nat_age = np->in_age[0];
2667 else if (!icmpset && (fin->fin_p == IPPROTO_ICMP))
2668 nat->nat_age = fr_defnaticmpage;
2670 nat->nat_age = fr_defnatage;
2672 nat->nat_bytes += ip->ip_len;
2674 MUTEX_EXIT(&nat->nat_lock);
2677 * Fix up checksums, not by recalculating them, but
2678 * simply computing adjustments.
2680 if (nat->nat_dir == NAT_OUTBOUND)
2681 fix_incksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2683 fix_outcksum(fin, &ip->ip_sum, nat->nat_ipsumd);
2685 ip->ip_dst = nat->nat_inip;
2686 fin->fin_fi.fi_daddr = nat->nat_inip.s_addr;
2688 if ((fin->fin_off == 0) && !(fin->fin_fl & FI_SHORT)) {
2690 if ((nat->nat_inport != 0) && (tcp != NULL)) {
2691 tcp->th_dport = nat->nat_inport;
2692 fin->fin_data[1] = ntohs(tcp->th_dport);
2695 if (fin->fin_p == IPPROTO_TCP) {
2696 csump = &tcp->th_sum;
2697 MUTEX_ENTER(&nat->nat_lock);
2698 fr_tcp_age(&nat->nat_age,
2699 nat->nat_tcpstate, fin, 0, 0);
2700 if (nat->nat_age < fr_defnaticmpage)
2701 nat->nat_age = fr_defnaticmpage;
2703 else if ((!np || !np->in_age[0]) &&
2704 (nat->nat_age > fr_defnatage))
2705 nat->nat_age = fr_defnatage;
2708 * Increase this because we may have
2709 * "keep state" following this too and
2710 * packet storms can occur if this is
2711 * removed too quickly.
2713 if (nat->nat_age == fr_tcpclosed)
2714 nat->nat_age = fr_tcplastack;
2716 * Do a MSS CLAMPING on a SYN packet,
2717 * only deal IPv4 for now.
2719 if (nat->nat_mssclamp &&
2720 (tcp->th_flags & TH_SYN) != 0)
2721 nat_mssclamp(tcp, nat->nat_mssclamp,
2724 MUTEX_EXIT(&nat->nat_lock);
2725 } else if (fin->fin_p == IPPROTO_UDP) {
2726 udphdr_t *udp = (udphdr_t *)tcp;
2729 csump = &udp->uh_sum;
2733 if (nat->nat_dir == NAT_OUTBOUND)
2734 fix_incksum(fin, csump,
2737 fix_outcksum(fin, csump,
2741 ATOMIC_INCL(nat_stats.ns_mapped[0]);
2742 RWLOCK_EXIT(&ipf_nat); /* READ */
2745 RWLOCK_EXIT(&ipf_nat); /* READ/WRITE */
2751 * Free all memory used by NAT structures allocated at runtime.
2755 WRITE_ENTER(&ipf_nat);
2756 (void) nat_clearlist();
2757 (void) nat_flushtable();
2758 RWLOCK_EXIT(&ipf_nat);
2760 if (nat_table[0] != NULL) {
2761 KFREES(nat_table[0], sizeof(nat_t *) * ipf_nattable_sz);
2762 nat_table[0] = NULL;
2764 if (nat_table[1] != NULL) {
2765 KFREES(nat_table[1], sizeof(nat_t *) * ipf_nattable_sz);
2766 nat_table[1] = NULL;
2768 if (nat_rules != NULL) {
2769 KFREES(nat_rules, sizeof(ipnat_t *) * ipf_natrules_sz);
2772 if (rdr_rules != NULL) {
2773 KFREES(rdr_rules, sizeof(ipnat_t *) * ipf_rdrrules_sz);
2776 if (maptable != NULL) {
2777 KFREES(maptable, sizeof(hostmap_t *) * ipf_hostmap_sz);
2784 * Slowly expire held state for NAT entries. Timeouts are set in
2785 * expectation of this being called twice per second.
2789 struct nat *nat, **natp;
2790 #if defined(_KERNEL) && !SOLARIS
2795 WRITE_ENTER(&ipf_nat);
2796 for (natp = &nat_instances; (nat = *natp); ) {
2799 natp = &nat->nat_next;
2802 *natp = nat->nat_next;
2804 nat_log(nat, NL_EXPIRE);
2807 nat_stats.ns_expire++;
2809 RWLOCK_EXIT(&ipf_nat);
2816 void ip_natsync(ifp)
2821 u_32_t sum1, sum2, sumd;
2825 #if defined(_KERNEL) && !SOLARIS
2830 * Change IP addresses for NAT sessions for any protocol except TCP
2831 * since it will break the TCP connection anyway.
2834 WRITE_ENTER(&ipf_nat);
2835 for (nat = nat_instances; nat; nat = nat->nat_next)
2836 if (((ifp == NULL) || (ifp == nat->nat_ifp)) &&
2837 !(nat->nat_flags & IPN_TCP) && (np = nat->nat_ptr) &&
2838 (np->in_outmsk == 0xffffffff) && !np->in_nip) {
2839 ifp2 = nat->nat_ifp;
2841 * Change the map-to address to be the same as the
2844 sum1 = nat->nat_outip.s_addr;
2845 if (fr_ifpaddr(4, ifp2, &in) != -1)
2846 nat->nat_outip = in;
2847 sum2 = nat->nat_outip.s_addr;
2852 * Readjust the checksum adjustment to take into
2853 * account the new IP#.
2855 CALC_SUMD(sum1, sum2, sumd);
2856 /* XXX - dont change for TCP when solaris does
2857 * hardware checksumming.
2859 sumd += nat->nat_sumd[0];
2860 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
2861 nat->nat_sumd[1] = nat->nat_sumd[0];
2864 for (n = nat_list; (n != NULL); n = n->in_next)
2865 if (n->in_ifp == ifp) {
2866 n->in_ifp = (void *)GETUNIT(n->in_ifname, 4);
2868 n->in_ifp = (void *)-1;
2870 RWLOCK_EXIT(&ipf_nat);
2876 void nat_log(nat, type)
2884 int rulen, types[1];
2886 natl.nl_inip = nat->nat_inip;
2887 natl.nl_outip = nat->nat_outip;
2888 natl.nl_origip = nat->nat_oip;
2889 natl.nl_bytes = nat->nat_bytes;
2890 natl.nl_pkts = nat->nat_pkts;
2891 natl.nl_origport = nat->nat_oport;
2892 natl.nl_inport = nat->nat_inport;
2893 natl.nl_outport = nat->nat_outport;
2894 natl.nl_p = nat->nat_p;
2895 natl.nl_type = type;
2898 if (nat->nat_ptr != NULL) {
2899 for (rulen = 0, np = nat_list; np; np = np->in_next, rulen++)
2900 if (np == nat->nat_ptr) {
2901 natl.nl_rule = rulen;
2907 sizes[0] = sizeof(natl);
2910 (void) ipllog(IPL_LOGNAT, NULL, items, sizes, types, 1);
2915 #if defined(__OpenBSD__)
2916 void nat_ifdetach(ifp)
2926 * Check for MSS option and clamp it if necessary.
2928 static void nat_mssclamp(tcp, maxmss, fin, csump)
2934 u_char *cp, *ep, opt;
2939 hlen = tcp->th_off << 2;
2940 if (hlen > sizeof(*tcp)) {
2941 cp = (u_char *)tcp + sizeof(*tcp);
2942 ep = (u_char *)tcp + hlen;
2946 if (opt == TCPOPT_EOL)
2948 else if (opt == TCPOPT_NOP) {
2956 if (&cp[advance] > ep)
2962 bcopy(&cp[2], &v, sizeof(v));
2966 bcopy(&v, &cp[2], sizeof(v));
2967 CALC_SUMD(mss, maxmss, sumd);
2968 fix_outcksum(fin, csump, sumd);
2972 /* ignore unknown options */