4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 #if defined(KERNEL) || defined(_KERNEL)
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
19 #if defined(_KERNEL) && \
20 (defined(__NetBSD_Version) && (__NetBSD_Version >= 399002000))
21 # include <sys/kauth.h>
34 #if defined(_KERNEL) && \
35 defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)
36 # include <sys/filio.h>
37 # include <sys/fcntl.h>
39 # include <sys/ioctl.h>
42 # include <sys/fcntl.h>
45 # include <sys/protosw.h>
47 #include <sys/socket.h>
49 # include <sys/systm.h>
50 # if !defined(__SVR4) && !defined(__svr4__)
51 # include <sys/mbuf.h>
54 #if defined(__SVR4) || defined(__svr4__)
55 # include <sys/filio.h>
56 # include <sys/byteorder.h>
58 # include <sys/dditypes.h>
60 # include <sys/stream.h>
61 # include <sys/kmem.h>
63 #if __FreeBSD_version >= 300000
64 # include <sys/queue.h>
67 #if __FreeBSD_version >= 300000
68 # include <net/if_var.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
79 # include <vpn/ipsec.h>
80 extern struct ifnet vpnif;
84 # include <netinet/ip_var.h>
86 #include <netinet/tcp.h>
87 #include <netinet/udp.h>
88 #include <netinet/ip_icmp.h>
89 #include "netinet/ip_compat.h"
90 #include <netinet/tcpip.h>
91 #include "netinet/ipl.h"
92 #include "netinet/ip_fil.h"
93 #include "netinet/ip_nat.h"
94 #include "netinet/ip_frag.h"
95 #include "netinet/ip_state.h"
96 #include "netinet/ip_proxy.h"
97 #include "netinet/ip_lookup.h"
98 #include "netinet/ip_dstlist.h"
99 #include "netinet/ip_sync.h"
100 #if FREEBSD_GE_REV(300000)
101 # include <sys/malloc.h>
104 # include <sys/md5.h>
108 /* END OF INCLUDES */
111 #define SOCKADDR_IN struct sockaddr_in
114 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
115 static const char rcsid[] = "@(#)$FreeBSD$";
116 /* static const char rcsid[] = "@(#)$Id: ip_nat.c,v 2.195.2.102 2007/10/16 10:08:10 darrenr Exp $"; */
120 #define NATFSUM(n,v,f) ((v) == 4 ? (n)->f.in4.s_addr : (n)->f.i6[0] + \
121 (n)->f.i6[1] + (n)->f.i6[2] + (n)->f.i6[3])
122 #define NBUMP(x) softn->(x)++
123 #define NBUMPD(x, y) do { \
127 #define NBUMPSIDE(y,x) softn->ipf_nat_stats.ns_side[y].x++
128 #define NBUMPSIDED(y,x) do { softn->ipf_nat_stats.ns_side[y].x++; \
130 #define NBUMPSIDEX(y,x,z) \
131 do { softn->ipf_nat_stats.ns_side[y].x++; \
133 #define NBUMPSIDEDF(y,x)do { softn->ipf_nat_stats.ns_side[y].x++; \
134 DT1(x, fr_info_t *, fin); } while (0)
136 frentry_t ipfnatblock;
138 static ipftuneable_t ipf_nat_tuneables[] = {
140 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_lock) },
142 stsizeof(ipf_nat_softc_t, ipf_nat_lock),
143 IPFT_RDONLY, NULL, NULL },
144 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_sz) },
145 "nat_table_size", 1, 0x7fffffff,
146 stsizeof(ipf_nat_softc_t, ipf_nat_table_sz),
147 0, NULL, ipf_nat_rehash },
148 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_max) },
149 "nat_table_max", 1, 0x7fffffff,
150 stsizeof(ipf_nat_softc_t, ipf_nat_table_max),
152 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maprules_sz) },
153 "nat_rules_size", 1, 0x7fffffff,
154 stsizeof(ipf_nat_softc_t, ipf_nat_maprules_sz),
155 0, NULL, ipf_nat_rehash_rules },
156 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_rdrrules_sz) },
157 "rdr_rules_size", 1, 0x7fffffff,
158 stsizeof(ipf_nat_softc_t, ipf_nat_rdrrules_sz),
159 0, NULL, ipf_nat_rehash_rules },
160 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_hostmap_sz) },
161 "hostmap_size", 1, 0x7fffffff,
162 stsizeof(ipf_nat_softc_t, ipf_nat_hostmap_sz),
163 0, NULL, ipf_nat_hostmap_rehash },
164 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maxbucket) },
165 "nat_maxbucket",1, 0x7fffffff,
166 stsizeof(ipf_nat_softc_t, ipf_nat_maxbucket),
168 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_logging) },
170 stsizeof(ipf_nat_softc_t, ipf_nat_logging),
172 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_doflush) },
174 stsizeof(ipf_nat_softc_t, ipf_nat_doflush),
176 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_low) },
177 "nat_table_wm_low", 1, 99,
178 stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_low),
180 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_high) },
181 "nat_table_wm_high", 2, 100,
182 stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_high),
190 /* ======================================================================== */
191 /* How the NAT is organised and works. */
193 /* Inside (interface y) NAT Outside (interface x) */
194 /* -------------------- -+- ------------------------------------- */
195 /* Packet going | out, processsed by ipf_nat_checkout() for x */
196 /* ------------> | ------------> */
197 /* src=10.1.1.1 | src=192.1.1.1 */
199 /* | in, processed by ipf_nat_checkin() for x */
200 /* <------------ | <------------ */
201 /* dst=10.1.1.1 | dst=192.1.1.1 */
202 /* -------------------- -+- ------------------------------------- */
203 /* ipf_nat_checkout() - changes ip_src and if required, sport */
204 /* - creates a new mapping, if required. */
205 /* ipf_nat_checkin() - changes ip_dst and if required, dport */
207 /* In the NAT table, internal source is recorded as "in" and externally */
209 /* ======================================================================== */
212 #if SOLARIS && !defined(INSTANCES)
213 extern int pfil_delayed_copy;
216 static int ipf_nat_flush_entry __P((ipf_main_softc_t *, void *));
217 static int ipf_nat_getent __P((ipf_main_softc_t *, caddr_t, int));
218 static int ipf_nat_getsz __P((ipf_main_softc_t *, caddr_t, int));
219 static int ipf_nat_putent __P((ipf_main_softc_t *, caddr_t, int));
220 static void ipf_nat_addmap __P((ipf_nat_softc_t *, ipnat_t *));
221 static void ipf_nat_addrdr __P((ipf_nat_softc_t *, ipnat_t *));
222 static int ipf_nat_builddivertmp __P((ipf_nat_softc_t *, ipnat_t *));
223 static int ipf_nat_clearlist __P((ipf_main_softc_t *, ipf_nat_softc_t *));
224 static int ipf_nat_cmp_rules __P((ipnat_t *, ipnat_t *));
225 static int ipf_nat_decap __P((fr_info_t *, nat_t *));
226 static void ipf_nat_delrule __P((ipf_main_softc_t *, ipf_nat_softc_t *,
228 static int ipf_nat_extraflush __P((ipf_main_softc_t *, ipf_nat_softc_t *, int));
229 static int ipf_nat_finalise __P((fr_info_t *, nat_t *));
230 static int ipf_nat_flushtable __P((ipf_main_softc_t *, ipf_nat_softc_t *));
231 static int ipf_nat_getnext __P((ipf_main_softc_t *, ipftoken_t *,
232 ipfgeniter_t *, ipfobj_t *));
233 static int ipf_nat_gettable __P((ipf_main_softc_t *, ipf_nat_softc_t *,
235 static hostmap_t *ipf_nat_hostmap __P((ipf_nat_softc_t *, ipnat_t *,
236 struct in_addr, struct in_addr,
237 struct in_addr, u_32_t));
238 static int ipf_nat_icmpquerytype __P((int));
239 static int ipf_nat_iterator __P((ipf_main_softc_t *, ipftoken_t *,
240 ipfgeniter_t *, ipfobj_t *));
241 static int ipf_nat_match __P((fr_info_t *, ipnat_t *));
242 static int ipf_nat_matcharray __P((nat_t *, int *, u_long));
243 static int ipf_nat_matchflush __P((ipf_main_softc_t *, ipf_nat_softc_t *,
245 static void ipf_nat_mssclamp __P((tcphdr_t *, u_32_t, fr_info_t *,
247 static int ipf_nat_newmap __P((fr_info_t *, nat_t *, natinfo_t *));
248 static int ipf_nat_newdivert __P((fr_info_t *, nat_t *, natinfo_t *));
249 static int ipf_nat_newrdr __P((fr_info_t *, nat_t *, natinfo_t *));
250 static int ipf_nat_newrewrite __P((fr_info_t *, nat_t *, natinfo_t *));
251 static int ipf_nat_nextaddr __P((fr_info_t *, nat_addr_t *, u_32_t *,
253 static int ipf_nat_nextaddrinit __P((ipf_main_softc_t *, char *,
254 nat_addr_t *, int, void *));
255 static int ipf_nat_resolverule __P((ipf_main_softc_t *, ipnat_t *));
256 static int ipf_nat_ruleaddrinit __P((ipf_main_softc_t *,
257 ipf_nat_softc_t *, ipnat_t *));
258 static void ipf_nat_rule_fini __P((ipf_main_softc_t *, ipnat_t *));
259 static int ipf_nat_rule_init __P((ipf_main_softc_t *, ipf_nat_softc_t *,
261 static int ipf_nat_siocaddnat __P((ipf_main_softc_t *, ipf_nat_softc_t *,
263 static void ipf_nat_siocdelnat __P((ipf_main_softc_t *, ipf_nat_softc_t *,
265 static void ipf_nat_tabmove __P((ipf_nat_softc_t *, nat_t *));
267 /* ------------------------------------------------------------------------ */
268 /* Function: ipf_nat_main_load */
269 /* Returns: int - 0 == success, -1 == failure */
270 /* Parameters: Nil */
272 /* The only global NAT structure that needs to be initialised is the filter */
273 /* rule that is used with blocking packets. */
274 /* ------------------------------------------------------------------------ */
278 bzero((char *)&ipfnatblock, sizeof(ipfnatblock));
279 ipfnatblock.fr_flags = FR_BLOCK|FR_QUICK;
280 ipfnatblock.fr_ref = 1;
286 /* ------------------------------------------------------------------------ */
287 /* Function: ipf_nat_main_unload */
288 /* Returns: int - 0 == success, -1 == failure */
289 /* Parameters: Nil */
291 /* A null-op function that exists as a placeholder so that the flow in */
292 /* other functions is obvious. */
293 /* ------------------------------------------------------------------------ */
295 ipf_nat_main_unload()
301 /* ------------------------------------------------------------------------ */
302 /* Function: ipf_nat_soft_create */
303 /* Returns: void * - NULL = failure, else pointer to NAT context */
304 /* Parameters: softc(I) - pointer to soft context main structure */
306 /* Allocate the initial soft context structure for NAT and populate it with */
307 /* some default values. Creating the tables is left until we call _init so */
308 /* that sizes can be changed before we get under way. */
309 /* ------------------------------------------------------------------------ */
311 ipf_nat_soft_create(softc)
312 ipf_main_softc_t *softc;
314 ipf_nat_softc_t *softn;
316 KMALLOC(softn, ipf_nat_softc_t *);
320 bzero((char *)softn, sizeof(*softn));
322 softn->ipf_nat_tune = ipf_tune_array_copy(softn,
323 sizeof(ipf_nat_tuneables),
325 if (softn->ipf_nat_tune == NULL) {
326 ipf_nat_soft_destroy(softc, softn);
329 if (ipf_tune_array_link(softc, softn->ipf_nat_tune) == -1) {
330 ipf_nat_soft_destroy(softc, softn);
334 softn->ipf_nat_list_tail = &softn->ipf_nat_list;
336 softn->ipf_nat_table_max = NAT_TABLE_MAX;
337 softn->ipf_nat_table_sz = NAT_TABLE_SZ;
338 softn->ipf_nat_maprules_sz = NAT_SIZE;
339 softn->ipf_nat_rdrrules_sz = RDR_SIZE;
340 softn->ipf_nat_hostmap_sz = HOSTMAP_SIZE;
341 softn->ipf_nat_doflush = 0;
343 softn->ipf_nat_logging = 1;
345 softn->ipf_nat_logging = 0;
348 softn->ipf_nat_defage = DEF_NAT_AGE;
349 softn->ipf_nat_defipage = IPF_TTLVAL(60);
350 softn->ipf_nat_deficmpage = IPF_TTLVAL(3);
351 softn->ipf_nat_table_wm_high = 99;
352 softn->ipf_nat_table_wm_low = 90;
357 /* ------------------------------------------------------------------------ */
358 /* Function: ipf_nat_soft_destroy */
360 /* Parameters: softc(I) - pointer to soft context main structure */
362 /* ------------------------------------------------------------------------ */
364 ipf_nat_soft_destroy(softc, arg)
365 ipf_main_softc_t *softc;
368 ipf_nat_softc_t *softn = arg;
370 if (softn->ipf_nat_tune != NULL) {
371 ipf_tune_array_unlink(softc, softn->ipf_nat_tune);
372 KFREES(softn->ipf_nat_tune, sizeof(ipf_nat_tuneables));
373 softn->ipf_nat_tune = NULL;
380 /* ------------------------------------------------------------------------ */
381 /* Function: ipf_nat_init */
382 /* Returns: int - 0 == success, -1 == failure */
383 /* Parameters: softc(I) - pointer to soft context main structure */
385 /* Initialise all of the NAT locks, tables and other structures. */
386 /* ------------------------------------------------------------------------ */
388 ipf_nat_soft_init(softc, arg)
389 ipf_main_softc_t *softc;
392 ipf_nat_softc_t *softn = arg;
396 KMALLOCS(softn->ipf_nat_table[0], nat_t **, \
397 sizeof(nat_t *) * softn->ipf_nat_table_sz);
399 if (softn->ipf_nat_table[0] != NULL) {
400 bzero((char *)softn->ipf_nat_table[0],
401 softn->ipf_nat_table_sz * sizeof(nat_t *));
406 KMALLOCS(softn->ipf_nat_table[1], nat_t **, \
407 sizeof(nat_t *) * softn->ipf_nat_table_sz);
409 if (softn->ipf_nat_table[1] != NULL) {
410 bzero((char *)softn->ipf_nat_table[1],
411 softn->ipf_nat_table_sz * sizeof(nat_t *));
416 KMALLOCS(softn->ipf_nat_map_rules, ipnat_t **, \
417 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
419 if (softn->ipf_nat_map_rules != NULL) {
420 bzero((char *)softn->ipf_nat_map_rules,
421 softn->ipf_nat_maprules_sz * sizeof(ipnat_t *));
426 KMALLOCS(softn->ipf_nat_rdr_rules, ipnat_t **, \
427 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
429 if (softn->ipf_nat_rdr_rules != NULL) {
430 bzero((char *)softn->ipf_nat_rdr_rules,
431 softn->ipf_nat_rdrrules_sz * sizeof(ipnat_t *));
436 KMALLOCS(softn->ipf_hm_maptable, hostmap_t **, \
437 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
439 if (softn->ipf_hm_maptable != NULL) {
440 bzero((char *)softn->ipf_hm_maptable,
441 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
445 softn->ipf_hm_maplist = NULL;
447 KMALLOCS(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, u_int *,
448 softn->ipf_nat_table_sz * sizeof(u_int));
450 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen == NULL) {
453 bzero((char *)softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
454 softn->ipf_nat_table_sz * sizeof(u_int));
456 KMALLOCS(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, u_int *,
457 softn->ipf_nat_table_sz * sizeof(u_int));
459 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen == NULL) {
463 bzero((char *)softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
464 softn->ipf_nat_table_sz * sizeof(u_int));
466 if (softn->ipf_nat_maxbucket == 0) {
467 for (i = softn->ipf_nat_table_sz; i > 0; i >>= 1)
468 softn->ipf_nat_maxbucket++;
469 softn->ipf_nat_maxbucket *= 2;
472 ipf_sttab_init(softc, softn->ipf_nat_tcptq);
474 * Increase this because we may have "keep state" following this too
475 * and packet storms can occur if this is removed too quickly.
477 softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
478 softn->ipf_nat_tcptq[IPF_TCP_NSTATES - 1].ifq_next =
479 &softn->ipf_nat_udptq;
481 IPFTQ_INIT(&softn->ipf_nat_udptq, softn->ipf_nat_defage,
482 "nat ipftq udp tab");
483 softn->ipf_nat_udptq.ifq_next = &softn->ipf_nat_udpacktq;
485 IPFTQ_INIT(&softn->ipf_nat_udpacktq, softn->ipf_nat_defage,
486 "nat ipftq udpack tab");
487 softn->ipf_nat_udpacktq.ifq_next = &softn->ipf_nat_icmptq;
489 IPFTQ_INIT(&softn->ipf_nat_icmptq, softn->ipf_nat_deficmpage,
490 "nat icmp ipftq tab");
491 softn->ipf_nat_icmptq.ifq_next = &softn->ipf_nat_icmpacktq;
493 IPFTQ_INIT(&softn->ipf_nat_icmpacktq, softn->ipf_nat_defage,
494 "nat icmpack ipftq tab");
495 softn->ipf_nat_icmpacktq.ifq_next = &softn->ipf_nat_iptq;
497 IPFTQ_INIT(&softn->ipf_nat_iptq, softn->ipf_nat_defipage,
499 softn->ipf_nat_iptq.ifq_next = &softn->ipf_nat_pending;
501 IPFTQ_INIT(&softn->ipf_nat_pending, 1, "nat pending ipftq tab");
502 softn->ipf_nat_pending.ifq_next = NULL;
504 for (i = 0, tq = softn->ipf_nat_tcptq; i < IPF_TCP_NSTATES; i++, tq++) {
505 if (tq->ifq_ttl < softn->ipf_nat_deficmpage)
506 tq->ifq_ttl = softn->ipf_nat_deficmpage;
508 else if (tq->ifq_ttl > softn->ipf_nat_defage)
509 tq->ifq_ttl = softn->ipf_nat_defage;
514 * Increase this because we may have "keep state" following
515 * this too and packet storms can occur if this is removed
518 softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
520 MUTEX_INIT(&softn->ipf_nat_new, "ipf nat new mutex");
521 MUTEX_INIT(&softn->ipf_nat_io, "ipf nat io mutex");
523 softn->ipf_nat_inited = 1;
529 /* ------------------------------------------------------------------------ */
530 /* Function: ipf_nat_soft_fini */
532 /* Parameters: softc(I) - pointer to soft context main structure */
534 /* Free all memory used by NAT structures allocated at runtime. */
535 /* ------------------------------------------------------------------------ */
537 ipf_nat_soft_fini(softc, arg)
538 ipf_main_softc_t *softc;
541 ipf_nat_softc_t *softn = arg;
542 ipftq_t *ifq, *ifqnext;
544 (void) ipf_nat_clearlist(softc, softn);
545 (void) ipf_nat_flushtable(softc, softn);
548 * Proxy timeout queues are not cleaned here because although they
549 * exist on the NAT list, ipf_proxy_unload is called after unload
550 * and the proxies actually are responsible for them being created.
551 * Should the proxy timeouts have their own list? There's no real
552 * justification as this is the only complication.
554 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
555 ifqnext = ifq->ifq_next;
556 if (ipf_deletetimeoutqueue(ifq) == 0)
557 ipf_freetimeoutqueue(softc, ifq);
560 if (softn->ipf_nat_table[0] != NULL) {
561 KFREES(softn->ipf_nat_table[0],
562 sizeof(nat_t *) * softn->ipf_nat_table_sz);
563 softn->ipf_nat_table[0] = NULL;
565 if (softn->ipf_nat_table[1] != NULL) {
566 KFREES(softn->ipf_nat_table[1],
567 sizeof(nat_t *) * softn->ipf_nat_table_sz);
568 softn->ipf_nat_table[1] = NULL;
570 if (softn->ipf_nat_map_rules != NULL) {
571 KFREES(softn->ipf_nat_map_rules,
572 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
573 softn->ipf_nat_map_rules = NULL;
575 if (softn->ipf_nat_rdr_rules != NULL) {
576 KFREES(softn->ipf_nat_rdr_rules,
577 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
578 softn->ipf_nat_rdr_rules = NULL;
580 if (softn->ipf_hm_maptable != NULL) {
581 KFREES(softn->ipf_hm_maptable,
582 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
583 softn->ipf_hm_maptable = NULL;
585 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
586 KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
587 sizeof(u_int) * softn->ipf_nat_table_sz);
588 softn->ipf_nat_stats.ns_side[0].ns_bucketlen = NULL;
590 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
591 KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
592 sizeof(u_int) * softn->ipf_nat_table_sz);
593 softn->ipf_nat_stats.ns_side[1].ns_bucketlen = NULL;
596 if (softn->ipf_nat_inited == 1) {
597 softn->ipf_nat_inited = 0;
598 ipf_sttab_destroy(softn->ipf_nat_tcptq);
600 MUTEX_DESTROY(&softn->ipf_nat_new);
601 MUTEX_DESTROY(&softn->ipf_nat_io);
603 MUTEX_DESTROY(&softn->ipf_nat_udptq.ifq_lock);
604 MUTEX_DESTROY(&softn->ipf_nat_udpacktq.ifq_lock);
605 MUTEX_DESTROY(&softn->ipf_nat_icmptq.ifq_lock);
606 MUTEX_DESTROY(&softn->ipf_nat_icmpacktq.ifq_lock);
607 MUTEX_DESTROY(&softn->ipf_nat_iptq.ifq_lock);
608 MUTEX_DESTROY(&softn->ipf_nat_pending.ifq_lock);
615 /* ------------------------------------------------------------------------ */
616 /* Function: ipf_nat_setlock */
618 /* Parameters: arg(I) - pointer to soft state information */
619 /* tmp(I) - new lock value */
621 /* Set the "lock status" of NAT to the value in tmp. */
622 /* ------------------------------------------------------------------------ */
624 ipf_nat_setlock(arg, tmp)
628 ipf_nat_softc_t *softn = arg;
630 softn->ipf_nat_lock = tmp;
634 /* ------------------------------------------------------------------------ */
635 /* Function: ipf_nat_addrdr */
637 /* Parameters: n(I) - pointer to NAT rule to add */
639 /* Adds a redirect rule to the hash table of redirect rules and the list of */
640 /* loaded NAT rules. Updates the bitmask indicating which netmasks are in */
641 /* use by redirect rules. */
642 /* ------------------------------------------------------------------------ */
644 ipf_nat_addrdr(softn, n)
645 ipf_nat_softc_t *softn;
654 if (n->in_odstatype == FRI_NORMAL) {
655 k = count4bits(n->in_odstmsk);
656 ipf_inet_mask_add(k, &softn->ipf_nat_rdr_mask);
657 j = (n->in_odstaddr & n->in_odstmsk);
658 rhv = NAT_HASH_FN(j, 0, 0xffffffff);
660 ipf_inet_mask_add(0, &softn->ipf_nat_rdr_mask);
664 hv = rhv % softn->ipf_nat_rdrrules_sz;
665 np = softn->ipf_nat_rdr_rules + hv;
667 np = &(*np)->in_rnext;
676 /* ------------------------------------------------------------------------ */
677 /* Function: ipf_nat_addmap */
679 /* Parameters: n(I) - pointer to NAT rule to add */
681 /* Adds a NAT map rule to the hash table of rules and the list of loaded */
682 /* NAT rules. Updates the bitmask indicating which netmasks are in use by */
683 /* redirect rules. */
684 /* ------------------------------------------------------------------------ */
686 ipf_nat_addmap(softn, n)
687 ipf_nat_softc_t *softn;
696 if (n->in_osrcatype == FRI_NORMAL) {
697 k = count4bits(n->in_osrcmsk);
698 ipf_inet_mask_add(k, &softn->ipf_nat_map_mask);
699 j = (n->in_osrcaddr & n->in_osrcmsk);
700 rhv = NAT_HASH_FN(j, 0, 0xffffffff);
702 ipf_inet_mask_add(0, &softn->ipf_nat_map_mask);
706 hv = rhv % softn->ipf_nat_maprules_sz;
707 np = softn->ipf_nat_map_rules + hv;
709 np = &(*np)->in_mnext;
718 /* ------------------------------------------------------------------------ */
719 /* Function: ipf_nat_delrdr */
721 /* Parameters: n(I) - pointer to NAT rule to delete */
723 /* Removes a redirect rule from the hash table of redirect rules. */
724 /* ------------------------------------------------------------------------ */
726 ipf_nat_delrdr(softn, n)
727 ipf_nat_softc_t *softn;
730 if (n->in_odstatype == FRI_NORMAL) {
731 int k = count4bits(n->in_odstmsk);
732 ipf_inet_mask_del(k, &softn->ipf_nat_rdr_mask);
734 ipf_inet_mask_del(0, &softn->ipf_nat_rdr_mask);
737 n->in_rnext->in_prnext = n->in_prnext;
738 *n->in_prnext = n->in_rnext;
743 /* ------------------------------------------------------------------------ */
744 /* Function: ipf_nat_delmap */
746 /* Parameters: n(I) - pointer to NAT rule to delete */
748 /* Removes a NAT map rule from the hash table of NAT map rules. */
749 /* ------------------------------------------------------------------------ */
751 ipf_nat_delmap(softn, n)
752 ipf_nat_softc_t *softn;
755 if (n->in_osrcatype == FRI_NORMAL) {
756 int k = count4bits(n->in_osrcmsk);
757 ipf_inet_mask_del(k, &softn->ipf_nat_map_mask);
759 ipf_inet_mask_del(0, &softn->ipf_nat_map_mask);
761 if (n->in_mnext != NULL)
762 n->in_mnext->in_pmnext = n->in_pmnext;
763 *n->in_pmnext = n->in_mnext;
768 /* ------------------------------------------------------------------------ */
769 /* Function: ipf_nat_hostmap */
770 /* Returns: struct hostmap* - NULL if no hostmap could be created, */
771 /* else a pointer to the hostmapping to use */
772 /* Parameters: np(I) - pointer to NAT rule */
773 /* real(I) - real IP address */
774 /* map(I) - mapped IP address */
775 /* port(I) - destination port number */
776 /* Write Locks: ipf_nat */
778 /* Check if an ip address has already been allocated for a given mapping */
779 /* that is not doing port based translation. If is not yet allocated, then */
780 /* create a new entry if a non-NULL NAT rule pointer has been supplied. */
781 /* ------------------------------------------------------------------------ */
782 static struct hostmap *
783 ipf_nat_hostmap(softn, np, src, dst, map, port)
784 ipf_nat_softc_t *softn;
794 hv = (src.s_addr ^ dst.s_addr);
798 hv %= softn->ipf_nat_hostmap_sz;
799 for (hm = softn->ipf_hm_maptable[hv]; hm; hm = hm->hm_hnext)
800 if ((hm->hm_osrcip.s_addr == src.s_addr) &&
801 (hm->hm_odstip.s_addr == dst.s_addr) &&
802 ((np == NULL) || (np == hm->hm_ipnat)) &&
803 ((port == 0) || (port == hm->hm_port))) {
804 softn->ipf_nat_stats.ns_hm_addref++;
810 softn->ipf_nat_stats.ns_hm_nullnp++;
814 KMALLOC(hm, hostmap_t *);
816 hm->hm_next = softn->ipf_hm_maplist;
817 hm->hm_pnext = &softn->ipf_hm_maplist;
818 if (softn->ipf_hm_maplist != NULL)
819 softn->ipf_hm_maplist->hm_pnext = &hm->hm_next;
820 softn->ipf_hm_maplist = hm;
821 hm->hm_hnext = softn->ipf_hm_maptable[hv];
822 hm->hm_phnext = softn->ipf_hm_maptable + hv;
823 if (softn->ipf_hm_maptable[hv] != NULL)
824 softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
825 softn->ipf_hm_maptable[hv] = hm;
831 hm->hm_ndstip.s_addr = 0;
836 softn->ipf_nat_stats.ns_hm_new++;
838 softn->ipf_nat_stats.ns_hm_newfail++;
844 /* ------------------------------------------------------------------------ */
845 /* Function: ipf_nat_hostmapdel */
847 /* Parameters: hmp(I) - pointer to hostmap structure pointer */
848 /* Write Locks: ipf_nat */
850 /* Decrement the references to this hostmap structure by one. If this */
851 /* reaches zero then remove it and free it. */
852 /* ------------------------------------------------------------------------ */
854 ipf_nat_hostmapdel(softc, hmp)
855 ipf_main_softc_t *softc;
856 struct hostmap **hmp;
864 if (hm->hm_ref == 0) {
865 ipf_nat_rule_deref(softc, &hm->hm_ipnat);
867 hm->hm_hnext->hm_phnext = hm->hm_phnext;
868 *hm->hm_phnext = hm->hm_hnext;
870 hm->hm_next->hm_pnext = hm->hm_pnext;
871 *hm->hm_pnext = hm->hm_next;
877 /* ------------------------------------------------------------------------ */
878 /* Function: ipf_fix_outcksum */
880 /* Parameters: fin(I) - pointer to packet information */
881 /* sp(I) - location of 16bit checksum to update */
882 /* n((I) - amount to adjust checksum by */
884 /* Adjusts the 16bit checksum by "n" for packets going out. */
885 /* ------------------------------------------------------------------------ */
887 ipf_fix_outcksum(cksum, sp, n, partial)
904 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
908 sum1 = (~ntohs(*sp)) & 0xffff;
910 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
912 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
913 sumshort = ~(u_short)sum1;
914 *(sp) = htons(sumshort);
918 /* ------------------------------------------------------------------------ */
919 /* Function: ipf_fix_incksum */
921 /* Parameters: fin(I) - pointer to packet information */
922 /* sp(I) - location of 16bit checksum to update */
923 /* n((I) - amount to adjust checksum by */
925 /* Adjusts the 16bit checksum by "n" for packets going in. */
926 /* ------------------------------------------------------------------------ */
928 ipf_fix_incksum(cksum, sp, n, partial)
945 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
950 sum1 = (~ntohs(*sp)) & 0xffff;
951 sum1 += ~(n) & 0xffff;
952 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
954 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
955 sumshort = ~(u_short)sum1;
956 *(sp) = htons(sumshort);
960 /* ------------------------------------------------------------------------ */
961 /* Function: ipf_fix_datacksum */
963 /* Parameters: sp(I) - location of 16bit checksum to update */
964 /* n((I) - amount to adjust checksum by */
966 /* Fix_datacksum is used *only* for the adjustments of checksums in the */
967 /* data section of an IP packet. */
969 /* The only situation in which you need to do this is when NAT'ing an */
970 /* ICMP error message. Such a message, contains in its body the IP header */
971 /* of the original IP packet, that causes the error. */
973 /* You can't use fix_incksum or fix_outcksum in that case, because for the */
974 /* kernel the data section of the ICMP error is just data, and no special */
975 /* processing like hardware cksum or ntohs processing have been done by the */
976 /* kernel on the data section. */
977 /* ------------------------------------------------------------------------ */
979 ipf_fix_datacksum(sp, n)
989 sum1 = (~ntohs(*sp)) & 0xffff;
991 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
993 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
994 sumshort = ~(u_short)sum1;
995 *(sp) = htons(sumshort);
999 /* ------------------------------------------------------------------------ */
1000 /* Function: ipf_nat_ioctl */
1001 /* Returns: int - 0 == success, != 0 == failure */
1002 /* Parameters: softc(I) - pointer to soft context main structure */
1003 /* data(I) - pointer to ioctl data */
1004 /* cmd(I) - ioctl command integer */
1005 /* mode(I) - file mode bits used with open */
1006 /* uid(I) - uid of calling process */
1007 /* ctx(I) - pointer used as key for finding context */
1009 /* Processes an ioctl call made to operate on the IP Filter NAT device. */
1010 /* ------------------------------------------------------------------------ */
1012 ipf_nat_ioctl(softc, data, cmd, mode, uid, ctx)
1013 ipf_main_softc_t *softc;
1019 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1020 int error = 0, ret, arg, getlock;
1021 ipnat_t *nat, *nt, *n;
1025 #if BSD_GE_YEAR(199306) && defined(_KERNEL)
1026 # if NETBSD_GE_REV(399002000)
1027 if ((mode & FWRITE) &&
1028 kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_FIREWALL,
1029 KAUTH_REQ_NETWORK_FIREWALL_FW,
1032 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 500034)
1033 if (securelevel_ge(curthread->td_ucred, 3) && (mode & FWRITE))
1035 if ((securelevel >= 3) && (mode & FWRITE))
1044 #if defined(__osf__) && defined(_KERNEL)
1047 getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
1054 if ((cmd == (ioctlcmd_t)SIOCADNAT) || (cmd == (ioctlcmd_t)SIOCRMNAT) ||
1055 (cmd == (ioctlcmd_t)SIOCPURGENAT)) {
1056 if (mode & NAT_SYSSPACE) {
1057 bcopy(data, (char *)&natd, sizeof(natd));
1061 bzero(&natd, sizeof(natd));
1062 error = ipf_inobj(softc, data, NULL, &natd,
1067 if (natd.in_size < sizeof(ipnat_t)) {
1071 KMALLOCS(nt, ipnat_t *, natd.in_size);
1077 bzero(nt, natd.in_size);
1078 error = ipf_inobjsz(softc, data, nt, IPFOBJ_IPNAT,
1086 * For add/delete, look to see if the NAT entry is
1089 nat->in_flags &= IPN_USERFLAGS;
1090 if ((nat->in_redir & NAT_MAPBLK) == 0) {
1091 if (nat->in_osrcatype == FRI_NORMAL ||
1092 nat->in_osrcatype == FRI_NONE)
1093 nat->in_osrcaddr &= nat->in_osrcmsk;
1094 if (nat->in_odstatype == FRI_NORMAL ||
1095 nat->in_odstatype == FRI_NONE)
1096 nat->in_odstaddr &= nat->in_odstmsk;
1097 if ((nat->in_flags & (IPN_SPLIT|IPN_SIPRANGE)) == 0) {
1098 if (nat->in_nsrcatype == FRI_NORMAL)
1099 nat->in_nsrcaddr &= nat->in_nsrcmsk;
1100 if (nat->in_ndstatype == FRI_NORMAL)
1101 nat->in_ndstaddr &= nat->in_ndstmsk;
1105 error = ipf_nat_rule_init(softc, softn, nat);
1109 MUTEX_ENTER(&softn->ipf_nat_io);
1110 for (n = softn->ipf_nat_list; n != NULL; n = n->in_next)
1111 if (ipf_nat_cmp_rules(nat, n) == 0)
1122 if (!(mode & FWRITE)) {
1126 tmp = ipf_log_clear(softc, IPL_LOGNAT);
1127 error = BCOPYOUT(&tmp, data, sizeof(tmp));
1137 if (!(mode & FWRITE)) {
1141 error = BCOPYIN(data, &softn->ipf_nat_logging,
1142 sizeof(softn->ipf_nat_logging));
1149 error = BCOPYOUT(&softn->ipf_nat_logging, data,
1150 sizeof(softn->ipf_nat_logging));
1158 arg = ipf_log_bytesused(softc, IPL_LOGNAT);
1159 error = BCOPYOUT(&arg, data, sizeof(arg));
1167 if (!(mode & FWRITE)) {
1170 } else if (n != NULL) {
1171 natd.in_flineno = n->in_flineno;
1172 (void) ipf_outobj(softc, data, &natd, IPFOBJ_IPNAT);
1175 } else if (nt == NULL) {
1180 MUTEX_EXIT(&softn->ipf_nat_io);
1184 bcopy((char *)nat, (char *)nt, sizeof(*n));
1185 error = ipf_nat_siocaddnat(softc, softn, nt, getlock);
1186 MUTEX_EXIT(&softn->ipf_nat_io);
1195 if (!(mode & FWRITE)) {
1199 } else if (n == NULL) {
1205 MUTEX_EXIT(&softn->ipf_nat_io);
1208 if (cmd == (ioctlcmd_t)SIOCPURGENAT) {
1209 error = ipf_outobjsz(softc, data, n, IPFOBJ_IPNAT,
1212 MUTEX_EXIT(&softn->ipf_nat_io);
1215 n->in_flags |= IPN_PURGE;
1217 ipf_nat_siocdelnat(softc, softn, n, getlock);
1219 MUTEX_EXIT(&softn->ipf_nat_io);
1225 natstat_t *nsp = &softn->ipf_nat_stats;
1227 nsp->ns_side[0].ns_table = softn->ipf_nat_table[0];
1228 nsp->ns_side[1].ns_table = softn->ipf_nat_table[1];
1229 nsp->ns_list = softn->ipf_nat_list;
1230 nsp->ns_maptable = softn->ipf_hm_maptable;
1231 nsp->ns_maplist = softn->ipf_hm_maplist;
1232 nsp->ns_nattab_sz = softn->ipf_nat_table_sz;
1233 nsp->ns_nattab_max = softn->ipf_nat_table_max;
1234 nsp->ns_rultab_sz = softn->ipf_nat_maprules_sz;
1235 nsp->ns_rdrtab_sz = softn->ipf_nat_rdrrules_sz;
1236 nsp->ns_hostmap_sz = softn->ipf_nat_hostmap_sz;
1237 nsp->ns_instances = softn->ipf_nat_instances;
1238 nsp->ns_ticks = softc->ipf_ticks;
1239 #ifdef IPFILTER_LOGGING
1240 nsp->ns_log_ok = ipf_log_logok(softc, IPF_LOGNAT);
1241 nsp->ns_log_fail = ipf_log_failures(softc, IPF_LOGNAT);
1244 nsp->ns_log_fail = 0;
1246 error = ipf_outobj(softc, data, nsp, IPFOBJ_NATSTAT);
1254 error = ipf_inobj(softc, data, NULL, &nl, IPFOBJ_NATLOOKUP);
1259 READ_ENTER(&softc->ipf_nat);
1265 ptr = ipf_nat_lookupredir(&nl);
1269 ptr = ipf_nat6_lookupredir(&nl);
1278 RWLOCK_EXIT(&softc->ipf_nat);
1281 error = ipf_outobj(softc, data, &nl,
1291 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
1292 if (!(mode & FWRITE)) {
1298 WRITE_ENTER(&softc->ipf_nat);
1301 error = BCOPYIN(data, &arg, sizeof(arg));
1307 ret = ipf_nat_flushtable(softc, softn);
1309 ret = ipf_nat_clearlist(softc, softn);
1311 ret = ipf_nat_extraflush(softc, softn, arg);
1312 ipf_proxy_flush(softc->ipf_proxy_soft, arg);
1316 RWLOCK_EXIT(&softc->ipf_nat);
1319 error = BCOPYOUT(&ret, data, sizeof(ret));
1323 case SIOCMATCHFLUSH :
1324 if (!(mode & FWRITE)) {
1330 WRITE_ENTER(&softc->ipf_nat);
1333 error = ipf_nat_matchflush(softc, softn, data);
1336 RWLOCK_EXIT(&softc->ipf_nat);
1341 error = ipf_proxy_ioctl(softc, data, cmd, mode, ctx);
1345 if (!(mode & FWRITE)) {
1349 error = ipf_lock(data, &softn->ipf_nat_lock);
1354 if ((mode & FWRITE) != 0) {
1355 error = ipf_nat_putent(softc, data, getlock);
1363 if (softn->ipf_nat_lock) {
1364 error = ipf_nat_getsz(softc, data, getlock);
1372 if (softn->ipf_nat_lock) {
1373 error = ipf_nat_getent(softc, data, getlock);
1386 error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER);
1391 token = ipf_token_find(softc, iter.igi_type, uid, ctx);
1392 if (token != NULL) {
1393 error = ipf_nat_iterator(softc, token, &iter, &obj);
1394 WRITE_ENTER(&softc->ipf_tokens);
1395 ipf_token_deref(softc, token);
1396 RWLOCK_EXIT(&softc->ipf_tokens);
1402 case SIOCIPFDELTOK :
1403 error = BCOPYIN(data, &arg, sizeof(arg));
1406 error = ipf_token_del(softc, arg, uid, ctx);
1415 error = ipf_outobj(softc, data, softn->ipf_nat_tcptq,
1420 error = ipf_nat_gettable(softc, softn, data);
1430 ipf_nat_rule_fini(softc, nat);
1432 KFREES(nt, nt->in_size);
1437 /* ------------------------------------------------------------------------ */
1438 /* Function: ipf_nat_siocaddnat */
1439 /* Returns: int - 0 == success, != 0 == failure */
1440 /* Parameters: softc(I) - pointer to soft context main structure */
1441 /* softn(I) - pointer to NAT context structure */
1442 /* n(I) - pointer to new NAT rule */
1443 /* np(I) - pointer to where to insert new NAT rule */
1444 /* getlock(I) - flag indicating if lock on is held */
1445 /* Mutex Locks: ipf_nat_io */
1447 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */
1448 /* from information passed to the kernel, then add it to the appropriate */
1449 /* NAT rule table(s). */
1450 /* ------------------------------------------------------------------------ */
1452 ipf_nat_siocaddnat(softc, softn, n, getlock)
1453 ipf_main_softc_t *softc;
1454 ipf_nat_softc_t *softn;
1460 if (ipf_nat_resolverule(softc, n) != 0) {
1465 if ((n->in_age[0] == 0) && (n->in_age[1] != 0)) {
1470 if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) {
1472 * Prerecord whether or not the destination of the divert
1473 * is local or not to the interface the packet is going
1476 n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1],
1477 n->in_ifps[1], &n->in_ndstip6);
1481 WRITE_ENTER(&softc->ipf_nat);
1484 n->in_pnext = softn->ipf_nat_list_tail;
1486 softn->ipf_nat_list_tail = &n->in_next;
1489 if (n->in_redir & NAT_REDIRECT) {
1490 n->in_flags &= ~IPN_NOTDST;
1494 ipf_nat_addrdr(softn, n);
1498 ipf_nat6_addrdr(softn, n);
1504 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_rdr);
1507 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
1508 n->in_flags &= ~IPN_NOTSRC;
1512 ipf_nat_addmap(softn, n);
1516 ipf_nat6_addmap(softn, n);
1522 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_map);
1525 if (n->in_age[0] != 0)
1526 n->in_tqehead[0] = ipf_addtimeoutqueue(softc,
1527 &softn->ipf_nat_utqe,
1530 if (n->in_age[1] != 0)
1531 n->in_tqehead[1] = ipf_addtimeoutqueue(softc,
1532 &softn->ipf_nat_utqe,
1535 MUTEX_INIT(&n->in_lock, "ipnat rule lock");
1538 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
1539 #if SOLARIS && !defined(INSTANCES)
1540 pfil_delayed_copy = 0;
1543 RWLOCK_EXIT(&softc->ipf_nat); /* WRITE */
1550 /* ------------------------------------------------------------------------ */
1551 /* Function: ipf_nat_ruleaddrinit */
1552 /* Parameters: softc(I) - pointer to soft context main structure */
1553 /* softn(I) - pointer to NAT context structure */
1554 /* n(I) - pointer to NAT rule */
1556 /* Initialise all of the NAT address structures in a NAT rule. */
1557 /* ------------------------------------------------------------------------ */
1559 ipf_nat_ruleaddrinit(softc, softn, n)
1560 ipf_main_softc_t *softc;
1561 ipf_nat_softc_t *softn;
1566 if ((n->in_ndst.na_atype == FRI_LOOKUP) &&
1567 (n->in_ndst.na_type != IPLT_DSTLIST)) {
1571 if ((n->in_nsrc.na_atype == FRI_LOOKUP) &&
1572 (n->in_nsrc.na_type != IPLT_DSTLIST)) {
1577 if (n->in_redir == NAT_BIMAP) {
1578 n->in_ndstaddr = n->in_osrcaddr;
1579 n->in_ndstmsk = n->in_osrcmsk;
1580 n->in_odstaddr = n->in_nsrcaddr;
1581 n->in_odstmsk = n->in_nsrcmsk;
1585 if (n->in_redir & NAT_REDIRECT)
1590 * Initialise all of the address fields.
1592 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc, 1,
1597 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst, 1,
1602 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc, 1,
1607 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst, 1,
1612 if (n->in_redir & NAT_DIVERTUDP)
1613 ipf_nat_builddivertmp(softn, n);
1619 /* ------------------------------------------------------------------------ */
1620 /* Function: ipf_nat_resolvrule */
1622 /* Parameters: softc(I) - pointer to soft context main structure */
1623 /* n(I) - pointer to NAT rule */
1625 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */
1626 /* from information passed to the kernel, then add it to the appropriate */
1627 /* NAT rule table(s). */
1628 /* ------------------------------------------------------------------------ */
1630 ipf_nat_resolverule(softc, n)
1631 ipf_main_softc_t *softc;
1638 n->in_ifps[0] = ipf_resolvenic(softc, base + n->in_ifnames[0],
1641 if (n->in_ifnames[1] == -1) {
1642 n->in_ifnames[1] = n->in_ifnames[0];
1643 n->in_ifps[1] = n->in_ifps[0];
1645 n->in_ifps[1] = ipf_resolvenic(softc, base + n->in_ifnames[1],
1649 if (n->in_plabel != -1) {
1650 if (n->in_redir & NAT_REDIRECT)
1651 n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
1653 base + n->in_plabel);
1655 n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
1657 base + n->in_plabel);
1658 if (n->in_apr == NULL)
1665 /* ------------------------------------------------------------------------ */
1666 /* Function: ipf_nat_siocdelnat */
1667 /* Returns: int - 0 == success, != 0 == failure */
1668 /* Parameters: softc(I) - pointer to soft context main structure */
1669 /* softn(I) - pointer to NAT context structure */
1670 /* n(I) - pointer to new NAT rule */
1671 /* getlock(I) - flag indicating if lock on is held */
1672 /* Mutex Locks: ipf_nat_io */
1674 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */
1675 /* from information passed to the kernel, then add it to the appropriate */
1676 /* NAT rule table(s). */
1677 /* ------------------------------------------------------------------------ */
1679 ipf_nat_siocdelnat(softc, softn, n, getlock)
1680 ipf_main_softc_t *softc;
1681 ipf_nat_softc_t *softn;
1686 WRITE_ENTER(&softc->ipf_nat);
1689 ipf_nat_delrule(softc, softn, n, 1);
1692 RWLOCK_EXIT(&softc->ipf_nat); /* READ/WRITE */
1697 /* ------------------------------------------------------------------------ */
1698 /* Function: ipf_nat_getsz */
1699 /* Returns: int - 0 == success, != 0 is the error value. */
1700 /* Parameters: softc(I) - pointer to soft context main structure */
1701 /* data(I) - pointer to natget structure with kernel */
1702 /* pointer get the size of. */
1703 /* getlock(I) - flag indicating whether or not the caller */
1704 /* holds a lock on ipf_nat */
1706 /* Handle SIOCSTGSZ. */
1707 /* Return the size of the nat list entry to be copied back to user space. */
1708 /* The size of the entry is stored in the ng_sz field and the enture natget */
1709 /* structure is copied back to the user. */
1710 /* ------------------------------------------------------------------------ */
1712 ipf_nat_getsz(softc, data, getlock)
1713 ipf_main_softc_t *softc;
1717 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1723 error = BCOPYIN(data, &ng, sizeof(ng));
1730 READ_ENTER(&softc->ipf_nat);
1735 nat = softn->ipf_nat_instances;
1738 * Empty list so the size returned is 0. Simple.
1742 RWLOCK_EXIT(&softc->ipf_nat);
1744 error = BCOPYOUT(&ng, data, sizeof(ng));
1753 * Make sure the pointer we're copying from exists in the
1754 * current list of entries. Security precaution to prevent
1755 * copying of random kernel data.
1757 for (n = softn->ipf_nat_instances; n; n = n->nat_next)
1762 RWLOCK_EXIT(&softc->ipf_nat);
1770 * Incluse any space required for proxy data structures.
1772 ng.ng_sz = sizeof(nat_save_t);
1775 ng.ng_sz += sizeof(ap_session_t) - 4;
1776 if (aps->aps_data != 0)
1777 ng.ng_sz += aps->aps_psiz;
1780 RWLOCK_EXIT(&softc->ipf_nat);
1783 error = BCOPYOUT(&ng, data, sizeof(ng));
1792 /* ------------------------------------------------------------------------ */
1793 /* Function: ipf_nat_getent */
1794 /* Returns: int - 0 == success, != 0 is the error value. */
1795 /* Parameters: softc(I) - pointer to soft context main structure */
1796 /* data(I) - pointer to natget structure with kernel pointer*/
1797 /* to NAT structure to copy out. */
1798 /* getlock(I) - flag indicating whether or not the caller */
1799 /* holds a lock on ipf_nat */
1801 /* Handle SIOCSTGET. */
1802 /* Copies out NAT entry to user space. Any additional data held for a */
1803 /* proxy is also copied, as to is the NAT rule which was responsible for it */
1804 /* ------------------------------------------------------------------------ */
1806 ipf_nat_getent(softc, data, getlock)
1807 ipf_main_softc_t *softc;
1811 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1814 nat_save_t *ipn, ipns;
1817 error = ipf_inobj(softc, data, NULL, &ipns, IPFOBJ_NATSAVE);
1821 if ((ipns.ipn_dsize < sizeof(ipns)) || (ipns.ipn_dsize > 81920)) {
1826 KMALLOCS(ipn, nat_save_t *, ipns.ipn_dsize);
1833 READ_ENTER(&softc->ipf_nat);
1836 ipn->ipn_dsize = ipns.ipn_dsize;
1837 nat = ipns.ipn_next;
1839 nat = softn->ipf_nat_instances;
1841 if (softn->ipf_nat_instances == NULL) {
1849 * Make sure the pointer we're copying from exists in the
1850 * current list of entries. Security precaution to prevent
1851 * copying of random kernel data.
1853 for (n = softn->ipf_nat_instances; n; n = n->nat_next)
1862 ipn->ipn_next = nat->nat_next;
1865 * Copy the NAT structure.
1867 bcopy((char *)nat, &ipn->ipn_nat, sizeof(*nat));
1870 * If we have a pointer to the NAT rule it belongs to, save that too.
1872 if (nat->nat_ptr != NULL)
1873 bcopy((char *)nat->nat_ptr, (char *)&ipn->ipn_ipnat,
1874 ipn->ipn_ipnat.in_size);
1877 * If we also know the NAT entry has an associated filter rule,
1880 if (nat->nat_fr != NULL)
1881 bcopy((char *)nat->nat_fr, (char *)&ipn->ipn_fr,
1882 sizeof(ipn->ipn_fr));
1885 * Last but not least, if there is an application proxy session set
1886 * up for this NAT entry, then copy that out too, including any
1887 * private data saved along side it by the proxy.
1890 outsize = ipn->ipn_dsize - sizeof(*ipn) + sizeof(ipn->ipn_data);
1894 if (outsize < sizeof(*aps)) {
1901 bcopy((char *)aps, s, sizeof(*aps));
1903 outsize -= sizeof(*aps);
1904 if ((aps->aps_data != NULL) && (outsize >= aps->aps_psiz))
1905 bcopy(aps->aps_data, s, aps->aps_psiz);
1912 error = ipf_outobjsz(softc, data, ipn, IPFOBJ_NATSAVE,
1918 KFREES(ipn, ipns.ipn_dsize);
1921 RWLOCK_EXIT(&softc->ipf_nat);
1927 /* ------------------------------------------------------------------------ */
1928 /* Function: ipf_nat_putent */
1929 /* Returns: int - 0 == success, != 0 is the error value. */
1930 /* Parameters: softc(I) - pointer to soft context main structure */
1931 /* data(I) - pointer to natget structure with NAT */
1932 /* structure information to load into the kernel */
1933 /* getlock(I) - flag indicating whether or not a write lock */
1934 /* on is already held. */
1936 /* Handle SIOCSTPUT. */
1937 /* Loads a NAT table entry from user space, including a NAT rule, proxy and */
1938 /* firewall rule data structures, if pointers to them indicate so. */
1939 /* ------------------------------------------------------------------------ */
1941 ipf_nat_putent(softc, data, getlock)
1942 ipf_main_softc_t *softc;
1946 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1947 nat_save_t ipn, *ipnn;
1955 error = ipf_inobj(softc, data, NULL, &ipn, IPFOBJ_NATSAVE);
1960 * Initialise early because of code at junkput label.
1970 * New entry, copy in the rest of the NAT entry if it's size is more
1971 * than just the nat_t structure.
1973 if (ipn.ipn_dsize > sizeof(ipn)) {
1974 if (ipn.ipn_dsize > 81920) {
1980 KMALLOCS(ipnn, nat_save_t *, ipn.ipn_dsize);
1986 bzero(ipnn, ipn.ipn_dsize);
1987 error = ipf_inobjsz(softc, data, ipnn, IPFOBJ_NATSAVE,
1995 KMALLOC(nat, nat_t *);
2002 bcopy((char *)&ipnn->ipn_nat, (char *)nat, sizeof(*nat));
2004 switch (nat->nat_v[0])
2013 error = EPROTONOSUPPORT;
2019 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
2021 bzero((char *)nat, offsetof(struct nat, nat_tqe));
2022 nat->nat_tqe.tqe_pnext = NULL;
2023 nat->nat_tqe.tqe_next = NULL;
2024 nat->nat_tqe.tqe_ifq = NULL;
2025 nat->nat_tqe.tqe_parent = nat;
2028 * Restore the rule associated with this nat session
2030 in = ipnn->ipn_nat.nat_ptr;
2032 KMALLOCS(in, ipnat_t *, ipnn->ipn_ipnat.in_size);
2039 bcopy((char *)&ipnn->ipn_ipnat, (char *)in,
2040 ipnn->ipn_ipnat.in_size);
2042 in->in_flags |= IPN_DELETE;
2044 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
2046 if (ipf_nat_resolverule(softc, in) != 0) {
2054 * Check that the NAT entry doesn't already exist in the kernel.
2056 * For NAT_OUTBOUND, we're lookup for a duplicate MAP entry. To do
2057 * this, we check to see if the inbound combination of addresses and
2058 * ports is already known. Similar logic is applied for NAT_INBOUND.
2061 bzero((char *)&fin, sizeof(fin));
2062 fin.fin_v = nat->nat_v[0];
2063 fin.fin_p = nat->nat_pr[0];
2064 fin.fin_rev = nat->nat_rev;
2065 fin.fin_ifp = nat->nat_ifps[0];
2066 fin.fin_data[0] = ntohs(nat->nat_ndport);
2067 fin.fin_data[1] = ntohs(nat->nat_nsport);
2069 switch (nat->nat_dir)
2072 case NAT_DIVERTOUT :
2074 READ_ENTER(&softc->ipf_nat);
2077 fin.fin_v = nat->nat_v[1];
2078 if (nat->nat_v[1] == 4) {
2079 n = ipf_nat_inlookup(&fin, nat->nat_flags, fin.fin_p,
2080 nat->nat_ndstip, nat->nat_nsrcip);
2082 } else if (nat->nat_v[1] == 6) {
2083 n = ipf_nat6_inlookup(&fin, nat->nat_flags, fin.fin_p,
2084 &nat->nat_ndst6.in6,
2085 &nat->nat_nsrc6.in6);
2090 RWLOCK_EXIT(&softc->ipf_nat);
2102 READ_ENTER(&softc->ipf_nat);
2105 if (fin.fin_v == 4) {
2106 n = ipf_nat_outlookup(&fin, nat->nat_flags, fin.fin_p,
2110 } else if (fin.fin_v == 6) {
2111 n = ipf_nat6_outlookup(&fin, nat->nat_flags, fin.fin_p,
2112 &nat->nat_ndst6.in6,
2113 &nat->nat_nsrc6.in6);
2118 RWLOCK_EXIT(&softc->ipf_nat);
2134 * Restore ap_session_t structure. Include the private data allocated
2139 KMALLOC(aps, ap_session_t *);
2146 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
2148 aps->aps_apr = in->in_apr;
2150 aps->aps_apr = NULL;
2151 if (aps->aps_psiz != 0) {
2152 if (aps->aps_psiz > 81920) {
2157 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
2158 if (aps->aps_data == NULL) {
2163 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
2167 aps->aps_data = NULL;
2172 * If there was a filtering rule associated with this entry then
2173 * build up a new one.
2177 if ((nat->nat_flags & SI_NEWFR) != 0) {
2178 KMALLOC(fr, frentry_t *);
2185 ipnn->ipn_nat.nat_fr = fr;
2187 (void) ipf_outobj(softc, data, ipnn, IPFOBJ_NATSAVE);
2188 bcopy((char *)&ipnn->ipn_fr, (char *)fr, sizeof(*fr));
2193 fr->fr_type = FR_T_NONE;
2195 MUTEX_NUKE(&fr->fr_lock);
2196 MUTEX_INIT(&fr->fr_lock, "nat-filter rule lock");
2199 READ_ENTER(&softc->ipf_nat);
2201 for (n = softn->ipf_nat_instances; n; n = n->nat_next)
2202 if (n->nat_fr == fr)
2206 MUTEX_ENTER(&fr->fr_lock);
2208 MUTEX_EXIT(&fr->fr_lock);
2211 RWLOCK_EXIT(&softc->ipf_nat);
2223 KFREES(ipnn, ipn.ipn_dsize);
2228 WRITE_ENTER(&softc->ipf_nat);
2232 error = ipf_nat_finalise(&fin, nat);
2235 error = ipf_nat6_finalise(&fin, nat);
2239 RWLOCK_EXIT(&softc->ipf_nat);
2250 (void) ipf_derefrule(softc, &fr);
2253 if ((ipnn != NULL) && (ipnn != &ipn)) {
2254 KFREES(ipnn, ipn.ipn_dsize);
2258 if (aps->aps_data != NULL) {
2259 KFREES(aps->aps_data, aps->aps_psiz);
2265 ipf_proxy_deref(in->in_apr);
2266 KFREES(in, in->in_size);
2274 /* ------------------------------------------------------------------------ */
2275 /* Function: ipf_nat_delete */
2277 /* Parameters: softc(I) - pointer to soft context main structure */
2278 /* nat(I) - pointer to NAT structure to delete */
2279 /* logtype(I) - type of LOG record to create before deleting */
2280 /* Write Lock: ipf_nat */
2282 /* Delete a nat entry from the various lists and table. If NAT logging is */
2283 /* enabled then generate a NAT log record for this event. */
2284 /* ------------------------------------------------------------------------ */
2286 ipf_nat_delete(softc, nat, logtype)
2287 ipf_main_softc_t *softc;
2291 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2292 int madeorphan = 0, bkt, removed = 0;
2293 nat_stat_side_t *nss;
2296 if (logtype != 0 && softn->ipf_nat_logging != 0)
2297 ipf_nat_log(softc, softn, nat, logtype);
2300 * Take it as a general indication that all the pointers are set if
2303 if (nat->nat_pnext != NULL) {
2306 bkt = nat->nat_hv[0] % softn->ipf_nat_table_sz;
2307 nss = &softn->ipf_nat_stats.ns_side[0];
2308 if (nss->ns_bucketlen[bkt] > 0)
2309 nss->ns_bucketlen[bkt]--;
2310 if (nss->ns_bucketlen[bkt] == 0) {
2314 bkt = nat->nat_hv[1] % softn->ipf_nat_table_sz;
2315 nss = &softn->ipf_nat_stats.ns_side[1];
2316 if (nss->ns_bucketlen[bkt] > 0)
2317 nss->ns_bucketlen[bkt]--;
2318 if (nss->ns_bucketlen[bkt] == 0) {
2322 *nat->nat_pnext = nat->nat_next;
2323 if (nat->nat_next != NULL) {
2324 nat->nat_next->nat_pnext = nat->nat_pnext;
2325 nat->nat_next = NULL;
2327 nat->nat_pnext = NULL;
2329 *nat->nat_phnext[0] = nat->nat_hnext[0];
2330 if (nat->nat_hnext[0] != NULL) {
2331 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2332 nat->nat_hnext[0] = NULL;
2334 nat->nat_phnext[0] = NULL;
2336 *nat->nat_phnext[1] = nat->nat_hnext[1];
2337 if (nat->nat_hnext[1] != NULL) {
2338 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2339 nat->nat_hnext[1] = NULL;
2341 nat->nat_phnext[1] = NULL;
2343 if ((nat->nat_flags & SI_WILDP) != 0) {
2344 ATOMIC_DEC32(softn->ipf_nat_stats.ns_wilds);
2349 if (nat->nat_me != NULL) {
2350 *nat->nat_me = NULL;
2353 ASSERT(nat->nat_ref >= 0);
2356 if (nat->nat_tqe.tqe_ifq != NULL) {
2358 * No call to ipf_freetimeoutqueue() is made here, they are
2359 * garbage collected in ipf_nat_expire().
2361 (void) ipf_deletequeueentry(&nat->nat_tqe);
2364 if (nat->nat_sync) {
2365 ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
2366 nat->nat_sync = NULL;
2369 if (logtype == NL_EXPIRE)
2370 softn->ipf_nat_stats.ns_expire++;
2372 MUTEX_ENTER(&nat->nat_lock);
2374 * NL_DESTROY should only be passed in when we've got nat_ref >= 2.
2375 * This happens when a nat'd packet is blocked and we want to throw
2376 * away the NAT session.
2378 if (logtype == NL_DESTROY) {
2379 if (nat->nat_ref > 2) {
2381 MUTEX_EXIT(&nat->nat_lock);
2383 softn->ipf_nat_stats.ns_orphans++;
2386 } else if (nat->nat_ref > 1) {
2388 MUTEX_EXIT(&nat->nat_lock);
2389 if (madeorphan == 1)
2390 softn->ipf_nat_stats.ns_orphans++;
2393 ASSERT(nat->nat_ref >= 0);
2394 MUTEX_EXIT(&nat->nat_lock);
2398 if (madeorphan == 0)
2399 softn->ipf_nat_stats.ns_orphans--;
2402 * At this point, nat_ref can be either 0 or -1
2404 softn->ipf_nat_stats.ns_proto[nat->nat_pr[0]]--;
2406 if (nat->nat_fr != NULL) {
2407 (void) ipf_derefrule(softc, &nat->nat_fr);
2410 if (nat->nat_hm != NULL) {
2411 ipf_nat_hostmapdel(softc, &nat->nat_hm);
2415 * If there is an active reference from the nat entry to its parent
2416 * rule, decrement the rule's reference count and free it too if no
2417 * longer being used.
2420 nat->nat_ptr = NULL;
2424 ipf_nat_rule_deref(softc, &ipn);
2427 if (nat->nat_aps != NULL) {
2428 ipf_proxy_free(softc, nat->nat_aps);
2429 nat->nat_aps = NULL;
2432 MUTEX_DESTROY(&nat->nat_lock);
2434 softn->ipf_nat_stats.ns_active--;
2437 * If there's a fragment table entry too for this nat entry, then
2438 * dereference that as well. This is after nat_lock is released
2441 ipf_frag_natforget(softc, (void *)nat);
2447 /* ------------------------------------------------------------------------ */
2448 /* Function: ipf_nat_flushtable */
2449 /* Returns: int - number of NAT rules deleted */
2450 /* Parameters: softc(I) - pointer to soft context main structure */
2451 /* softn(I) - pointer to NAT context structure */
2452 /* Write Lock: ipf_nat */
2454 /* Deletes all currently active NAT sessions. In deleting each NAT entry a */
2455 /* log record should be emitted in ipf_nat_delete() if NAT logging is */
2457 /* ------------------------------------------------------------------------ */
2459 * nat_flushtable - clear the NAT table of all mapping entries.
2462 ipf_nat_flushtable(softc, softn)
2463 ipf_main_softc_t *softc;
2464 ipf_nat_softc_t *softn;
2470 * ALL NAT mappings deleted, so lets just make the deletions
2473 if (softn->ipf_nat_table[0] != NULL)
2474 bzero((char *)softn->ipf_nat_table[0],
2475 sizeof(softn->ipf_nat_table[0]) *
2476 softn->ipf_nat_table_sz);
2477 if (softn->ipf_nat_table[1] != NULL)
2478 bzero((char *)softn->ipf_nat_table[1],
2479 sizeof(softn->ipf_nat_table[1]) *
2480 softn->ipf_nat_table_sz);
2482 while ((nat = softn->ipf_nat_instances) != NULL) {
2483 ipf_nat_delete(softc, nat, NL_FLUSH);
2491 /* ------------------------------------------------------------------------ */
2492 /* Function: ipf_nat_clearlist */
2493 /* Returns: int - number of NAT/RDR rules deleted */
2494 /* Parameters: softc(I) - pointer to soft context main structure */
2495 /* softn(I) - pointer to NAT context structure */
2497 /* Delete all rules in the current list of rules. There is nothing elegant */
2498 /* about this cleanup: simply free all entries on the list of rules and */
2499 /* clear out the tables used for hashed NAT rule lookups. */
2500 /* ------------------------------------------------------------------------ */
2502 ipf_nat_clearlist(softc, softn)
2503 ipf_main_softc_t *softc;
2504 ipf_nat_softc_t *softn;
2509 if (softn->ipf_nat_map_rules != NULL) {
2510 bzero((char *)softn->ipf_nat_map_rules,
2511 sizeof(*softn->ipf_nat_map_rules) *
2512 softn->ipf_nat_maprules_sz);
2514 if (softn->ipf_nat_rdr_rules != NULL) {
2515 bzero((char *)softn->ipf_nat_rdr_rules,
2516 sizeof(*softn->ipf_nat_rdr_rules) *
2517 softn->ipf_nat_rdrrules_sz);
2520 while ((n = softn->ipf_nat_list) != NULL) {
2521 ipf_nat_delrule(softc, softn, n, 0);
2524 #if SOLARIS && !defined(INSTANCES)
2525 pfil_delayed_copy = 1;
2531 /* ------------------------------------------------------------------------ */
2532 /* Function: ipf_nat_delrule */
2534 /* Parameters: softc(I) - pointer to soft context main structure */
2535 /* softn(I) - pointer to NAT context structure */
2536 /* np(I) - pointer to NAT rule to delete */
2537 /* purge(I) - 1 == allow purge, 0 == prevent purge */
2538 /* Locks: WRITE(ipf_nat) */
2540 /* Preventing "purge" from occuring is allowed because when all of the NAT */
2541 /* rules are being removed, allowing the "purge" to walk through the list */
2542 /* of NAT sessions, possibly multiple times, would be a large performance */
2543 /* hit, on the order of O(N^2). */
2544 /* ------------------------------------------------------------------------ */
2546 ipf_nat_delrule(softc, softn, np, purge)
2547 ipf_main_softc_t *softc;
2548 ipf_nat_softc_t *softn;
2553 if (np->in_pnext != NULL) {
2554 *np->in_pnext = np->in_next;
2555 if (np->in_next != NULL)
2556 np->in_next->in_pnext = np->in_pnext;
2557 if (softn->ipf_nat_list_tail == &np->in_next)
2558 softn->ipf_nat_list_tail = np->in_pnext;
2561 if ((purge == 1) && ((np->in_flags & IPN_PURGE) != 0)) {
2565 for (next = softn->ipf_nat_instances; (nat = next) != NULL;) {
2566 next = nat->nat_next;
2567 if (nat->nat_ptr == np)
2568 ipf_nat_delete(softc, nat, NL_PURGE);
2572 if ((np->in_flags & IPN_DELETE) == 0) {
2573 if (np->in_redir & NAT_REDIRECT) {
2574 switch (np->in_v[0])
2577 ipf_nat_delrdr(softn, np);
2581 ipf_nat6_delrdr(softn, np);
2586 if (np->in_redir & (NAT_MAPBLK|NAT_MAP)) {
2587 switch (np->in_v[0])
2590 ipf_nat_delmap(softn, np);
2594 ipf_nat6_delmap(softn, np);
2601 np->in_flags |= IPN_DELETE;
2602 ipf_nat_rule_deref(softc, &np);
2606 /* ------------------------------------------------------------------------ */
2607 /* Function: ipf_nat_newmap */
2608 /* Returns: int - -1 == error, 0 == success */
2609 /* Parameters: fin(I) - pointer to packet information */
2610 /* nat(I) - pointer to NAT entry */
2611 /* ni(I) - pointer to structure with misc. information needed */
2612 /* to create new NAT entry. */
2614 /* Given an empty NAT structure, populate it with new information about a */
2615 /* new NAT session, as defined by the matching NAT rule. */
2616 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
2617 /* to the new IP address for the translation. */
2618 /* ------------------------------------------------------------------------ */
2620 ipf_nat_newmap(fin, nat, ni)
2625 ipf_main_softc_t *softc = fin->fin_main_soft;
2626 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2627 u_short st_port, dport, sport, port, sp, dp;
2628 struct in_addr in, inb;
2637 * If it's an outbound packet which doesn't match any existing
2638 * record, then create a new port
2643 st_ip = np->in_snip;
2644 st_port = np->in_spnext;
2645 flags = nat->nat_flags;
2647 if (flags & IPN_ICMPQUERY) {
2648 sport = fin->fin_data[1];
2651 sport = htons(fin->fin_data[0]);
2652 dport = htons(fin->fin_data[1]);
2656 * Do a loop until we either run out of entries to try or we find
2657 * a NAT mapping that isn't currently being used. This is done
2658 * because the change to the source is not (usually) being fixed.
2662 in.s_addr = htonl(np->in_snip);
2665 * Check to see if there is an existing NAT
2666 * setup for this IP address pair.
2668 hm = ipf_nat_hostmap(softn, np, fin->fin_src,
2669 fin->fin_dst, in, 0);
2671 in.s_addr = hm->hm_nsrcip.s_addr;
2672 } else if ((l == 1) && (hm != NULL)) {
2673 ipf_nat_hostmapdel(softc, &hm);
2675 in.s_addr = ntohl(in.s_addr);
2679 if ((np->in_nsrcmsk == 0xffffffff) && (np->in_spnext == 0)) {
2681 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_1);
2682 DT4(ns_exhausted_1, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2687 if (np->in_redir == NAT_BIMAP &&
2688 np->in_osrcmsk == np->in_nsrcmsk) {
2690 * map the address block in a 1:1 fashion
2692 in.s_addr = np->in_nsrcaddr;
2693 in.s_addr |= fin->fin_saddr & ~np->in_osrcmsk;
2694 in.s_addr = ntohl(in.s_addr);
2696 } else if (np->in_redir & NAT_MAPBLK) {
2697 if ((l >= np->in_ppip) || ((l > 0) &&
2698 !(flags & IPN_TCPUDP))) {
2699 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_2);
2700 DT4(ns_exhausted_2, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2704 * map-block - Calculate destination address.
2706 in.s_addr = ntohl(fin->fin_saddr);
2707 in.s_addr &= ntohl(~np->in_osrcmsk);
2708 inb.s_addr = in.s_addr;
2709 in.s_addr /= np->in_ippip;
2710 in.s_addr &= ntohl(~np->in_nsrcmsk);
2711 in.s_addr += ntohl(np->in_nsrcaddr);
2713 * Calculate destination port.
2715 if ((flags & IPN_TCPUDP) &&
2716 (np->in_ppip != 0)) {
2717 port = ntohs(sport) + l;
2718 port %= np->in_ppip;
2719 port += np->in_ppip *
2720 (inb.s_addr % np->in_ippip);
2721 port += MAPBLK_MINPORT;
2725 } else if ((np->in_nsrcaddr == 0) &&
2726 (np->in_nsrcmsk == 0xffffffff)) {
2730 * 0/32 - use the interface's IP address.
2733 ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
2734 &in6, NULL) == -1) {
2735 NBUMPSIDEX(1, ns_new_ifpaddr, ns_new_ifpaddr_1);
2736 DT4(ns_new_ifpaddr_1, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2739 in.s_addr = ntohl(in6.in4.s_addr);
2741 } else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
2743 * 0/0 - use the original source address/port.
2746 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_3);
2747 DT4(ns_exhausted_3, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2750 in.s_addr = ntohl(fin->fin_saddr);
2752 } else if ((np->in_nsrcmsk != 0xffffffff) &&
2753 (np->in_spnext == 0) && ((l > 0) || (hm == NULL)))
2758 if ((flags & IPN_TCPUDP) &&
2759 ((np->in_redir & NAT_MAPBLK) == 0) &&
2760 (np->in_flags & IPN_AUTOPORTMAP)) {
2762 * "ports auto" (without map-block)
2764 if ((l > 0) && (l % np->in_ppip == 0)) {
2765 if ((l > np->in_ppip) &&
2766 np->in_nsrcmsk != 0xffffffff)
2769 if (np->in_ppip != 0) {
2770 port = ntohs(sport);
2771 port += (l % np->in_ppip);
2772 port %= np->in_ppip;
2773 port += np->in_ppip *
2774 (ntohl(fin->fin_saddr) %
2776 port += MAPBLK_MINPORT;
2780 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
2781 (flags & IPN_TCPUDPICMP) && (np->in_spnext != 0)) {
2783 * Standard port translation. Select next port.
2785 if (np->in_flags & IPN_SEQUENTIAL) {
2786 port = np->in_spnext;
2788 port = ipf_random() % (np->in_spmax -
2790 port += np->in_spmin;
2795 if (np->in_spnext > np->in_spmax) {
2796 np->in_spnext = np->in_spmin;
2797 if (np->in_nsrcmsk != 0xffffffff)
2802 if (np->in_flags & IPN_SIPRANGE) {
2803 if (np->in_snip > ntohl(np->in_nsrcmsk))
2804 np->in_snip = ntohl(np->in_nsrcaddr);
2806 if ((np->in_nsrcmsk != 0xffffffff) &&
2807 ((np->in_snip + 1) & ntohl(np->in_nsrcmsk)) >
2808 ntohl(np->in_nsrcaddr))
2809 np->in_snip = ntohl(np->in_nsrcaddr) + 1;
2812 if ((port == 0) && (flags & (IPN_TCPUDPICMP|IPN_ICMPQUERY)))
2816 * Here we do a lookup of the connection as seen from
2817 * the outside. If an IP# pair already exists, try
2818 * again. So if you have A->B becomes C->B, you can
2819 * also have D->E become C->E but not D->B causing
2820 * another C->B. Also take protocol and ports into
2821 * account when determining whether a pre-existing
2822 * NAT setup will cause an external conflict where
2823 * this is appropriate.
2825 inb.s_addr = htonl(in.s_addr);
2826 sp = fin->fin_data[0];
2827 dp = fin->fin_data[1];
2828 fin->fin_data[0] = fin->fin_data[1];
2829 fin->fin_data[1] = ntohs(port);
2830 natl = ipf_nat_inlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
2831 (u_int)fin->fin_p, fin->fin_dst, inb);
2832 fin->fin_data[0] = sp;
2833 fin->fin_data[1] = dp;
2836 * Has the search wrapped around and come back to the
2839 if ((natl != NULL) &&
2840 (np->in_spnext != 0) && (st_port == np->in_spnext) &&
2841 (np->in_snip != 0) && (st_ip == np->in_snip)) {
2842 NBUMPSIDED(1, ns_wrap);
2843 DT4(ns_wrap, fr_info_t *, fin, nat_t *, nat, natinfo_t *, ni, ipnat_t *, np);
2847 } while (natl != NULL);
2849 /* Setup the NAT table */
2850 nat->nat_osrcip = fin->fin_src;
2851 nat->nat_nsrcaddr = htonl(in.s_addr);
2852 nat->nat_odstip = fin->fin_dst;
2853 nat->nat_ndstip = fin->fin_dst;
2854 if (nat->nat_hm == NULL)
2855 nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
2856 fin->fin_dst, nat->nat_nsrcip,
2859 if (flags & IPN_TCPUDP) {
2860 nat->nat_osport = sport;
2861 nat->nat_nsport = port; /* sport */
2862 nat->nat_odport = dport;
2863 nat->nat_ndport = dport;
2864 ((tcphdr_t *)fin->fin_dp)->th_sport = port;
2865 } else if (flags & IPN_ICMPQUERY) {
2866 nat->nat_oicmpid = fin->fin_data[1];
2867 ((icmphdr_t *)fin->fin_dp)->icmp_id = port;
2868 nat->nat_nicmpid = port;
2874 /* ------------------------------------------------------------------------ */
2875 /* Function: ipf_nat_newrdr */
2876 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */
2877 /* allow rule to be moved if IPN_ROUNDR is set. */
2878 /* Parameters: fin(I) - pointer to packet information */
2879 /* nat(I) - pointer to NAT entry */
2880 /* ni(I) - pointer to structure with misc. information needed */
2881 /* to create new NAT entry. */
2883 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
2884 /* to the new IP address for the translation. */
2885 /* ------------------------------------------------------------------------ */
2887 ipf_nat_newrdr(fin, nat, ni)
2892 ipf_main_softc_t *softc = fin->fin_main_soft;
2893 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2894 u_short nport, dport, sport;
2895 struct in_addr in, inb;
2907 flags = nat->nat_flags;
2909 if (flags & IPN_ICMPQUERY) {
2910 dport = fin->fin_data[1];
2913 sport = htons(fin->fin_data[0]);
2914 dport = htons(fin->fin_data[1]);
2917 /* TRACE sport, dport */
2921 * If the matching rule has IPN_STICKY set, then we want to have the
2922 * same rule kick in as before. Why would this happen? If you have
2923 * a collection of rdr rules with "round-robin sticky", the current
2924 * packet might match a different one to the previous connection but
2925 * we want the same destination to be used.
2927 if (((np->in_flags & (IPN_ROUNDR|IPN_SPLIT)) != 0) &&
2928 ((np->in_flags & IPN_STICKY) != 0)) {
2929 hm = ipf_nat_hostmap(softn, NULL, fin->fin_src, fin->fin_dst,
2932 in.s_addr = ntohl(hm->hm_ndstip.s_addr);
2936 ipf_nat_hostmapdel(softc, &hm);
2941 * Otherwise, it's an inbound packet. Most likely, we don't
2942 * want to rewrite source ports and source addresses. Instead,
2943 * we want to rewrite to a fixed internal address and fixed
2946 if (np->in_flags & IPN_SPLIT) {
2947 in.s_addr = np->in_dnip;
2948 inb.s_addr = htonl(in.s_addr);
2950 if ((np->in_flags & (IPN_ROUNDR|IPN_STICKY)) == IPN_STICKY) {
2951 hm = ipf_nat_hostmap(softn, NULL, fin->fin_src,
2952 fin->fin_dst, inb, (u_32_t)dport);
2954 in.s_addr = hm->hm_ndstip.s_addr;
2959 if (hm == NULL || hm->hm_ref == 1) {
2960 if (np->in_ndstaddr == htonl(in.s_addr)) {
2961 np->in_dnip = ntohl(np->in_ndstmsk);
2964 np->in_dnip = ntohl(np->in_ndstaddr);
2968 ipf_nat_hostmapdel(softc, &hm);
2970 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
2974 * 0/32 - use the interface's IP address.
2976 if (ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
2977 &in6, NULL) == -1) {
2978 NBUMPSIDEX(0, ns_new_ifpaddr, ns_new_ifpaddr_2);
2979 DT3(ns_new_ifpaddr_2, fr_info_t *, fin, nat_t *, nat, natinfo_t, ni);
2982 in.s_addr = ntohl(in6.in4.s_addr);
2984 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk== 0)) {
2986 * 0/0 - use the original destination address/port.
2988 in.s_addr = ntohl(fin->fin_daddr);
2990 } else if (np->in_redir == NAT_BIMAP &&
2991 np->in_ndstmsk == np->in_odstmsk) {
2993 * map the address block in a 1:1 fashion
2995 in.s_addr = np->in_ndstaddr;
2996 in.s_addr |= fin->fin_daddr & ~np->in_ndstmsk;
2997 in.s_addr = ntohl(in.s_addr);
2999 in.s_addr = ntohl(np->in_ndstaddr);
3002 if ((np->in_dpnext == 0) || ((flags & NAT_NOTRULEPORT) != 0))
3006 * Whilst not optimized for the case where
3007 * pmin == pmax, the gain is not significant.
3009 if (((np->in_flags & IPN_FIXEDDPORT) == 0) &&
3010 (np->in_odport != np->in_dtop)) {
3011 nport = ntohs(dport) - np->in_odport + np->in_dpmax;
3012 nport = htons(nport);
3014 nport = htons(np->in_dpnext);
3016 if (np->in_dpnext > np->in_dpmax)
3017 np->in_dpnext = np->in_dpmin;
3022 * When the redirect-to address is set to 0.0.0.0, just
3023 * assume a blank `forwarding' of the packet. We don't
3024 * setup any translation for this either.
3026 if (in.s_addr == 0) {
3027 if (nport == dport) {
3028 NBUMPSIDED(0, ns_xlate_null);
3031 in.s_addr = ntohl(fin->fin_daddr);
3035 * Check to see if this redirect mapping already exists and if
3036 * it does, return "failure" (allowing it to be created will just
3037 * cause one or both of these "connections" to stop working.)
3039 inb.s_addr = htonl(in.s_addr);
3040 sp = fin->fin_data[0];
3041 dp = fin->fin_data[1];
3042 fin->fin_data[1] = fin->fin_data[0];
3043 fin->fin_data[0] = ntohs(nport);
3044 natl = ipf_nat_outlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
3045 (u_int)fin->fin_p, inb, fin->fin_src);
3046 fin->fin_data[0] = sp;
3047 fin->fin_data[1] = dp;
3049 DT2(ns_new_xlate_exists, fr_info_t *, fin, nat_t *, natl);
3050 NBUMPSIDE(0, ns_xlate_exists);
3054 inb.s_addr = htonl(in.s_addr);
3055 nat->nat_ndstaddr = htonl(in.s_addr);
3056 nat->nat_odstip = fin->fin_dst;
3057 nat->nat_nsrcip = fin->fin_src;
3058 nat->nat_osrcip = fin->fin_src;
3059 if ((nat->nat_hm == NULL) && ((np->in_flags & IPN_STICKY) != 0))
3060 nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
3061 fin->fin_dst, inb, (u_32_t)dport);
3063 if (flags & IPN_TCPUDP) {
3064 nat->nat_odport = dport;
3065 nat->nat_ndport = nport;
3066 nat->nat_osport = sport;
3067 nat->nat_nsport = sport;
3068 ((tcphdr_t *)fin->fin_dp)->th_dport = nport;
3069 } else if (flags & IPN_ICMPQUERY) {
3070 nat->nat_oicmpid = fin->fin_data[1];
3071 ((icmphdr_t *)fin->fin_dp)->icmp_id = nport;
3072 nat->nat_nicmpid = nport;
3078 /* ------------------------------------------------------------------------ */
3079 /* Function: ipf_nat_add */
3080 /* Returns: nat_t* - NULL == failure to create new NAT structure, */
3081 /* else pointer to new NAT structure */
3082 /* Parameters: fin(I) - pointer to packet information */
3083 /* np(I) - pointer to NAT rule */
3084 /* natsave(I) - pointer to where to store NAT struct pointer */
3085 /* flags(I) - flags describing the current packet */
3086 /* direction(I) - direction of packet (in/out) */
3087 /* Write Lock: ipf_nat */
3089 /* Attempts to create a new NAT entry. Does not actually change the packet */
3092 /* This fucntion is in three main parts: (1) deal with creating a new NAT */
3093 /* structure for a "MAP" rule (outgoing NAT translation); (2) deal with */
3094 /* creating a new NAT structure for a "RDR" rule (incoming NAT translation) */
3095 /* and (3) building that structure and putting it into the NAT table(s). */
3097 /* NOTE: natsave should NOT be used top point back to an ipstate_t struct */
3098 /* as it can result in memory being corrupted. */
3099 /* ------------------------------------------------------------------------ */
3101 ipf_nat_add(fin, np, natsave, flags, direction)
3108 ipf_main_softc_t *softc = fin->fin_main_soft;
3109 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3110 hostmap_t *hm = NULL;
3117 nsp = &softn->ipf_nat_stats;
3119 if ((nsp->ns_active * 100 / softn->ipf_nat_table_max) >
3120 softn->ipf_nat_table_wm_high) {
3121 softn->ipf_nat_doflush = 1;
3124 if (nsp->ns_active >= softn->ipf_nat_table_max) {
3125 NBUMPSIDED(fin->fin_out, ns_table_max);
3126 DT2(ns_table_max, nat_stat_t *, nsp, ipf_nat_softc_t *, softn);
3131 nflags = np->in_flags & flags;
3132 nflags &= NAT_FROMRULE;
3138 /* Give me a new nat */
3139 KMALLOC(nat, nat_t *);
3142 NBUMPSIDED(fin->fin_out, ns_memfail);
3144 * Try to automatically tune the max # of entries in the
3145 * table allowed to be less than what will cause kmem_alloc()
3146 * to fail and try to eliminate panics due to out of memory
3147 * conditions arising.
3149 if ((softn->ipf_nat_table_max > softn->ipf_nat_table_sz) &&
3150 (nsp->ns_active > 100)) {
3151 softn->ipf_nat_table_max = nsp->ns_active - 100;
3152 printf("table_max reduced to %d\n",
3153 softn->ipf_nat_table_max);
3158 if (flags & IPN_ICMPQUERY) {
3160 * In the ICMP query NAT code, we translate the ICMP id fields
3161 * to make them unique. This is indepedent of the ICMP type
3162 * (e.g. in the unlikely event that a host sends an echo and
3163 * an tstamp request with the same id, both packets will have
3164 * their ip address/id field changed in the same way).
3166 /* The icmp_id field is used by the sender to identify the
3167 * process making the icmp request. (the receiver justs
3168 * copies it back in its response). So, it closely matches
3169 * the concept of source port. We overlay sport, so we can
3170 * maximally reuse the existing code.
3172 ni.nai_sport = fin->fin_data[1];
3176 bzero((char *)nat, sizeof(*nat));
3177 nat->nat_flags = flags;
3178 nat->nat_redir = np->in_redir;
3179 nat->nat_dir = direction;
3180 nat->nat_pr[0] = fin->fin_p;
3181 nat->nat_pr[1] = fin->fin_p;
3184 * Search the current table for a match and create a new mapping
3185 * if there is none found.
3187 if (np->in_redir & NAT_DIVERTUDP) {
3188 move = ipf_nat_newdivert(fin, nat, &ni);
3190 } else if (np->in_redir & NAT_REWRITE) {
3191 move = ipf_nat_newrewrite(fin, nat, &ni);
3193 } else if (direction == NAT_OUTBOUND) {
3195 * We can now arrange to call this for the same connection
3196 * because ipf_nat_new doesn't protect the code path into
3199 natl = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
3200 fin->fin_src, fin->fin_dst);
3207 move = ipf_nat_newmap(fin, nat, &ni);
3210 * NAT_INBOUND is used for redirects rules
3212 natl = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
3213 fin->fin_src, fin->fin_dst);
3220 move = ipf_nat_newrdr(fin, nat, &ni);
3227 nat->nat_mssclamp = np->in_mssclamp;
3228 nat->nat_me = natsave;
3229 nat->nat_fr = fin->fin_fr;
3230 nat->nat_rev = fin->fin_rev;
3232 nat->nat_dlocal = np->in_dlocal;
3234 if ((np->in_apr != NULL) && ((nat->nat_flags & NAT_SLAVE) == 0)) {
3235 if (ipf_proxy_new(fin, nat) == -1) {
3236 NBUMPSIDED(fin->fin_out, ns_appr_fail);
3237 DT3(ns_appr_fail, fr_info_t *, fin, nat_t *, nat, ipnat_t *, np);
3242 nat->nat_ifps[0] = np->in_ifps[0];
3243 if (np->in_ifps[0] != NULL) {
3244 COPYIFNAME(np->in_v[0], np->in_ifps[0], nat->nat_ifnames[0]);
3247 nat->nat_ifps[1] = np->in_ifps[1];
3248 if (np->in_ifps[1] != NULL) {
3249 COPYIFNAME(np->in_v[1], np->in_ifps[1], nat->nat_ifnames[1]);
3252 if (ipf_nat_finalise(fin, nat) == -1) {
3258 if ((move == 1) && (np->in_flags & IPN_ROUNDR)) {
3259 if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_REDIRECT) {
3260 ipf_nat_delrdr(softn, np);
3261 ipf_nat_addrdr(softn, np);
3262 } else if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_MAP) {
3263 ipf_nat_delmap(softn, np);
3264 ipf_nat_addmap(softn, np);
3268 if (flags & SI_WILDP)
3270 nsp->ns_proto[nat->nat_pr[0]]++;
3274 DT3(ns_badnatnew, fr_info_t *, fin, nat_t *, nat, ipnat_t *, np);
3275 NBUMPSIDE(fin->fin_out, ns_badnatnew);
3276 if ((hm = nat->nat_hm) != NULL)
3277 ipf_nat_hostmapdel(softc, &hm);
3281 if (nat != NULL && np != NULL)
3283 if (natsave != NULL)
3289 /* ------------------------------------------------------------------------ */
3290 /* Function: ipf_nat_finalise */
3291 /* Returns: int - 0 == sucess, -1 == failure */
3292 /* Parameters: fin(I) - pointer to packet information */
3293 /* nat(I) - pointer to NAT entry */
3294 /* Write Lock: ipf_nat */
3296 /* This is the tail end of constructing a new NAT entry and is the same */
3297 /* for both IPv4 and IPv6. */
3298 /* ------------------------------------------------------------------------ */
3301 ipf_nat_finalise(fin, nat)
3305 ipf_main_softc_t *softc = fin->fin_main_soft;
3306 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3307 u_32_t sum1, sum2, sumd;
3310 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_M_CTL_MAGIC)
3311 qpktinfo_t *qpi = fin->fin_qpi;
3314 flags = nat->nat_flags;
3316 switch (nat->nat_pr[0])
3319 sum1 = LONG_SUM(ntohs(nat->nat_oicmpid));
3320 sum2 = LONG_SUM(ntohs(nat->nat_nicmpid));
3321 CALC_SUMD(sum1, sum2, sumd);
3322 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
3327 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr) + \
3328 ntohs(nat->nat_osport));
3329 sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr) + \
3330 ntohs(nat->nat_nsport));
3331 CALC_SUMD(sum1, sum2, sumd);
3332 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
3334 sum1 = LONG_SUM(ntohl(nat->nat_odstaddr) + \
3335 ntohs(nat->nat_odport));
3336 sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr) + \
3337 ntohs(nat->nat_ndport));
3338 CALC_SUMD(sum1, sum2, sumd);
3339 nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
3344 * Compute the partial checksum, just in case.
3345 * This is only ever placed into outbound packets so care needs
3346 * to be taken over which pair of addresses are used.
3348 if (nat->nat_dir == NAT_OUTBOUND) {
3349 sum1 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
3350 sum1 += LONG_SUM(ntohl(nat->nat_ndstaddr));
3352 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr));
3353 sum1 += LONG_SUM(ntohl(nat->nat_odstaddr));
3355 sum1 += nat->nat_pr[1];
3356 nat->nat_sumd[1] = (sum1 & 0xffff) + (sum1 >> 16);
3358 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr));
3359 sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
3360 CALC_SUMD(sum1, sum2, sumd);
3361 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
3363 sum1 = LONG_SUM(ntohl(nat->nat_odstaddr));
3364 sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
3365 CALC_SUMD(sum1, sum2, sumd);
3366 nat->nat_ipsumd += (sumd & 0xffff) + (sumd >> 16);
3371 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
3372 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
3375 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
3376 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
3379 if ((nat->nat_flags & SI_CLONE) == 0)
3380 nat->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, nat);
3382 if (ipf_nat_insert(softc, softn, nat) == 0) {
3383 if (softn->ipf_nat_logging)
3384 ipf_nat_log(softc, softn, nat, NL_NEW);
3387 MUTEX_ENTER(&fr->fr_lock);
3389 MUTEX_EXIT(&fr->fr_lock);
3394 NBUMPSIDED(fin->fin_out, ns_unfinalised);
3395 DT2(ns_unfinalised, fr_info_t *, fin, nat_t *, nat);
3397 * nat_insert failed, so cleanup time...
3399 if (nat->nat_sync != NULL)
3400 ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
3405 /* ------------------------------------------------------------------------ */
3406 /* Function: ipf_nat_insert */
3407 /* Returns: int - 0 == sucess, -1 == failure */
3408 /* Parameters: softc(I) - pointer to soft context main structure */
3409 /* softn(I) - pointer to NAT context structure */
3410 /* nat(I) - pointer to NAT structure */
3411 /* Write Lock: ipf_nat */
3413 /* Insert a NAT entry into the hash tables for searching and add it to the */
3414 /* list of active NAT entries. Adjust global counters when complete. */
3415 /* ------------------------------------------------------------------------ */
3417 ipf_nat_insert(softc, softn, nat)
3418 ipf_main_softc_t *softc;
3419 ipf_nat_softc_t *softn;
3427 * Try and return an error as early as possible, so calculate the hash
3428 * entry numbers first and then proceed.
3430 if ((nat->nat_flags & (SI_W_SPORT|SI_W_DPORT)) == 0) {
3431 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
3432 sp = nat->nat_osport;
3433 dp = nat->nat_odport;
3434 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
3436 dp = nat->nat_oicmpid;
3441 hv0 = NAT_HASH_FN(nat->nat_osrcaddr, sp, 0xffffffff);
3442 hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0 + dp, 0xffffffff);
3444 * TRACE nat_osrcaddr, nat_osport, nat_odstaddr,
3448 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
3449 sp = nat->nat_nsport;
3450 dp = nat->nat_ndport;
3451 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
3453 dp = nat->nat_nicmpid;
3458 hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, sp, 0xffffffff);
3459 hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1 + dp, 0xffffffff);
3461 * TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr,
3465 hv0 = NAT_HASH_FN(nat->nat_osrcaddr, 0, 0xffffffff);
3466 hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0, 0xffffffff);
3467 /* TRACE nat_osrcaddr, nat_odstaddr, hv0 */
3469 hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, 0, 0xffffffff);
3470 hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1, 0xffffffff);
3471 /* TRACE nat_nsrcaddr, nat_ndstaddr, hv1 */
3474 nat->nat_hv[0] = hv0;
3475 nat->nat_hv[1] = hv1;
3477 MUTEX_INIT(&nat->nat_lock, "nat entry lock");
3480 nat->nat_ref = nat->nat_me ? 2 : 1;
3482 nat->nat_ifnames[0][LIFNAMSIZ - 1] = '\0';
3483 nat->nat_ifps[0] = ipf_resolvenic(softc, nat->nat_ifnames[0], 4);
3485 if (nat->nat_ifnames[1][0] != '\0') {
3486 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
3487 nat->nat_ifps[1] = ipf_resolvenic(softc,
3488 nat->nat_ifnames[1], 4);
3489 } else if (in->in_ifnames[1] != -1) {
3492 name = in->in_names + in->in_ifnames[1];
3493 if (name[1] != '\0' && name[0] != '-' && name[0] != '*') {
3494 (void) strncpy(nat->nat_ifnames[1],
3495 nat->nat_ifnames[0], LIFNAMSIZ);
3496 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
3497 nat->nat_ifps[1] = nat->nat_ifps[0];
3500 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
3501 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
3503 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
3504 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
3507 return ipf_nat_hashtab_add(softc, softn, nat);
3511 /* ------------------------------------------------------------------------ */
3512 /* Function: ipf_nat_hashtab_add */
3513 /* Parameters: softc(I) - pointer to soft context main structure */
3514 /* softn(I) - pointer to NAT context structure */
3515 /* nat(I) - pointer to NAT structure */
3517 /* Handle the insertion of a NAT entry into the table/list. */
3518 /* ------------------------------------------------------------------------ */
3520 ipf_nat_hashtab_add(softc, softn, nat)
3521 ipf_main_softc_t *softc;
3522 ipf_nat_softc_t *softn;
3529 hv0 = nat->nat_hv[0] % softn->ipf_nat_table_sz;
3530 hv1 = nat->nat_hv[1] % softn->ipf_nat_table_sz;
3532 if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_DIVERTIN) {
3540 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0] >=
3541 softn->ipf_nat_maxbucket) {
3542 DT1(ns_bucket_max_0, int,
3543 softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0]);
3544 NBUMPSIDE(0, ns_bucket_max);
3548 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1] >=
3549 softn->ipf_nat_maxbucket) {
3550 DT1(ns_bucket_max_1, int,
3551 softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1]);
3552 NBUMPSIDE(1, ns_bucket_max);
3557 * The ordering of operations in the list and hash table insertion
3558 * is very important. The last operation for each task should be
3559 * to update the top of the list, after all the "nexts" have been
3560 * done so that walking the list while it is being done does not
3561 * find strange pointers.
3563 * Global list of NAT instances
3565 nat->nat_next = softn->ipf_nat_instances;
3566 nat->nat_pnext = &softn->ipf_nat_instances;
3567 if (softn->ipf_nat_instances)
3568 softn->ipf_nat_instances->nat_pnext = &nat->nat_next;
3569 softn->ipf_nat_instances = nat;
3572 * Inbound hash table.
3574 natp = &softn->ipf_nat_table[0][hv0];
3575 nat->nat_phnext[0] = natp;
3576 nat->nat_hnext[0] = *natp;
3578 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
3580 NBUMPSIDE(0, ns_inuse);
3583 NBUMPSIDE(0, ns_bucketlen[hv0]);
3586 * Outbound hash table.
3588 natp = &softn->ipf_nat_table[1][hv1];
3589 nat->nat_phnext[1] = natp;
3590 nat->nat_hnext[1] = *natp;
3592 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
3594 NBUMPSIDE(1, ns_inuse);
3597 NBUMPSIDE(1, ns_bucketlen[hv1]);
3599 ipf_nat_setqueue(softc, softn, nat);
3601 if (nat->nat_dir & NAT_OUTBOUND) {
3602 NBUMPSIDE(1, ns_added);
3604 NBUMPSIDE(0, ns_added);
3606 softn->ipf_nat_stats.ns_active++;
3611 /* ------------------------------------------------------------------------ */
3612 /* Function: ipf_nat_icmperrorlookup */
3613 /* Returns: nat_t* - point to matching NAT structure */
3614 /* Parameters: fin(I) - pointer to packet information */
3615 /* dir(I) - direction of packet (in/out) */
3617 /* Check if the ICMP error message is related to an existing TCP, UDP or */
3618 /* ICMP query nat entry. It is assumed that the packet is already of the */
3619 /* the required length. */
3620 /* ------------------------------------------------------------------------ */
3622 ipf_nat_icmperrorlookup(fin, dir)
3626 ipf_main_softc_t *softc = fin->fin_main_soft;
3627 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3628 int flags = 0, type, minlen;
3629 icmphdr_t *icmp, *orgicmp;
3630 nat_stat_side_t *nside;
3631 tcphdr_t *tcp = NULL;
3638 type = icmp->icmp_type;
3639 nside = &softn->ipf_nat_stats.ns_side[fin->fin_out];
3641 * Does it at least have the return (basic) IP header ?
3642 * Only a basic IP header (no options) should be with an ICMP error
3643 * header. Also, if it's not an error type, then return.
3645 if ((fin->fin_hlen != sizeof(ip_t)) || !(fin->fin_flx & FI_ICMPERR)) {
3646 ATOMIC_INCL(nside->ns_icmp_basic);
3653 oip = (ip_t *)((char *)fin->fin_dp + 8);
3654 minlen = IP_HL(oip) << 2;
3655 if ((minlen < sizeof(ip_t)) ||
3656 (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen)) {
3657 ATOMIC_INCL(nside->ns_icmp_size);
3662 * Is the buffer big enough for all of it ? It's the size of the IP
3663 * header claimed in the encapsulated part which is of concern. It
3664 * may be too big to be in this buffer but not so big that it's
3665 * outside the ICMP packet, leading to TCP deref's causing problems.
3666 * This is possible because we don't know how big oip_hl is when we
3667 * do the pullup early in ipf_check() and thus can't gaurantee it is
3670 #ifdef ipf_nat_KERNEL
3675 # if defined(MENTAT)
3676 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
3677 (char *)m->b_wptr) {
3678 ATOMIC_INCL(nside->ns_icmp_mbuf);
3682 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
3683 (char *)fin->fin_ip + M_LEN(m)) {
3684 ATOMIC_INCL(nside->ns_icmp_mbuf);
3691 if (fin->fin_daddr != oip->ip_src.s_addr) {
3692 ATOMIC_INCL(nside->ns_icmp_address);
3697 if (p == IPPROTO_TCP)
3699 else if (p == IPPROTO_UDP)
3701 else if (p == IPPROTO_ICMP) {
3702 orgicmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2));
3704 /* see if this is related to an ICMP query */
3705 if (ipf_nat_icmpquerytype(orgicmp->icmp_type)) {
3706 data[0] = fin->fin_data[0];
3707 data[1] = fin->fin_data[1];
3708 fin->fin_data[0] = 0;
3709 fin->fin_data[1] = orgicmp->icmp_id;
3711 flags = IPN_ICMPERR|IPN_ICMPQUERY;
3713 * NOTE : dir refers to the direction of the original
3714 * ip packet. By definition the icmp error
3715 * message flows in the opposite direction.
3717 if (dir == NAT_INBOUND)
3718 nat = ipf_nat_inlookup(fin, flags, p,
3722 nat = ipf_nat_outlookup(fin, flags, p,
3725 fin->fin_data[0] = data[0];
3726 fin->fin_data[1] = data[1];
3731 if (flags & IPN_TCPUDP) {
3732 minlen += 8; /* + 64bits of data to get ports */
3733 /* TRACE (fin,minlen) */
3734 if (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen) {
3735 ATOMIC_INCL(nside->ns_icmp_short);
3739 data[0] = fin->fin_data[0];
3740 data[1] = fin->fin_data[1];
3741 tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2));
3742 fin->fin_data[0] = ntohs(tcp->th_dport);
3743 fin->fin_data[1] = ntohs(tcp->th_sport);
3745 if (dir == NAT_INBOUND) {
3746 nat = ipf_nat_inlookup(fin, flags, p, oip->ip_dst,
3749 nat = ipf_nat_outlookup(fin, flags, p, oip->ip_dst,
3752 fin->fin_data[0] = data[0];
3753 fin->fin_data[1] = data[1];
3756 if (dir == NAT_INBOUND)
3757 nat = ipf_nat_inlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
3759 nat = ipf_nat_outlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
3765 /* ------------------------------------------------------------------------ */
3766 /* Function: ipf_nat_icmperror */
3767 /* Returns: nat_t* - point to matching NAT structure */
3768 /* Parameters: fin(I) - pointer to packet information */
3769 /* nflags(I) - NAT flags for this packet */
3770 /* dir(I) - direction of packet (in/out) */
3772 /* Fix up an ICMP packet which is an error message for an existing NAT */
3773 /* session. This will correct both packet header data and checksums. */
3775 /* This should *ONLY* be used for incoming ICMP error packets to make sure */
3776 /* a NAT'd ICMP packet gets correctly recognised. */
3777 /* ------------------------------------------------------------------------ */
3779 ipf_nat_icmperror(fin, nflags, dir)
3784 ipf_main_softc_t *softc = fin->fin_main_soft;
3785 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3786 u_32_t sum1, sum2, sumd, sumd2;
3787 struct in_addr a1, a2, a3, a4;
3788 int flags, dlen, odst;
3796 if ((fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
3797 NBUMPSIDED(fin->fin_out, ns_icmp_short);
3802 * ipf_nat_icmperrorlookup() will return NULL for `defective' packets.
3804 if ((fin->fin_v != 4) || !(nat = ipf_nat_icmperrorlookup(fin, dir))) {
3805 NBUMPSIDED(fin->fin_out, ns_icmp_notfound);
3813 *nflags = IPN_ICMPERR;
3815 oip = (ip_t *)&icmp->icmp_ip;
3816 dp = (((char *)oip) + (IP_HL(oip) << 2));
3817 if (oip->ip_p == IPPROTO_TCP) {
3818 tcp = (tcphdr_t *)dp;
3819 csump = (u_short *)&tcp->th_sum;
3821 } else if (oip->ip_p == IPPROTO_UDP) {
3824 udp = (udphdr_t *)dp;
3825 tcp = (tcphdr_t *)dp;
3826 csump = (u_short *)&udp->uh_sum;
3828 } else if (oip->ip_p == IPPROTO_ICMP)
3829 flags = IPN_ICMPQUERY;
3830 dlen = fin->fin_plen - ((char *)dp - (char *)fin->fin_ip);
3833 * Need to adjust ICMP header to include the real IP#'s and
3834 * port #'s. Only apply a checksum change relative to the
3835 * IP address change as it will be modified again in ipf_nat_checkout
3836 * for both address and port. Two checksum changes are
3837 * necessary for the two header address changes. Be careful
3838 * to only modify the checksum once for the port # and twice
3844 * Fix the IP addresses in the offending IP packet. You also need
3845 * to adjust the IP header checksum of that offending IP packet.
3847 * Normally, you would expect that the ICMP checksum of the
3848 * ICMP error message needs to be adjusted as well for the
3849 * IP address change in oip.
3850 * However, this is a NOP, because the ICMP checksum is
3851 * calculated over the complete ICMP packet, which includes the
3852 * changed oip IP addresses and oip->ip_sum. However, these
3853 * two changes cancel each other out (if the delta for
3854 * the IP address is x, then the delta for ip_sum is minus x),
3855 * so no change in the icmp_cksum is necessary.
3859 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
3860 * - response to outgoing packet (a,b)=>(c,b) (OIP_SRC=c,OIP_DST=b)
3861 * - OIP_SRC(c)=nat_newsrcip, OIP_DST(b)=nat_newdstip
3862 *=> OIP_SRC(c)=nat_oldsrcip, OIP_DST(b)=nat_olddstip
3864 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
3865 * - response to outgoing packet (c,a)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3866 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip
3867 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3869 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
3870 * - response to outgoing packet (a,b)=>(c,d) (OIP_SRC=c,OIP_DST=d)
3871 * - OIP_SRC(c)=nat_newsrcip, OIP_DST(d)=nat_newdstip
3872 *=> OIP_SRC(c)=nat_oldsrcip, OIP_DST(d)=nat_olddstip
3874 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
3875 * - response to outgoing packet (d,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3876 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip
3877 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3881 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
3882 * - response to incoming packet (b,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3883 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip
3884 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3886 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
3887 * - response to incoming packet (a,b)=>(a,c) (OIP_SRC=a,OIP_DST=c)
3888 * - OIP_SRC(a)=nat_newsrcip, OIP_DST(c)=nat_newdstip
3889 *=> OIP_SRC(a)=nat_oldsrcip, OIP_DST(c)=nat_olddstip
3891 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
3892 * - response to incoming packet (d,c)=>(b,a) (OIP_SRC=c,OIP_DST=d)
3893 * - OIP_SRC(c)=nat_olddstip, OIP_DST(d)=nat_oldsrcip
3894 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3896 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
3897 * - response to incoming packet (a,b)=>(c,d) (OIP_SRC=b,OIP_DST=a)
3898 * - OIP_SRC(b)=nat_newsrcip, OIP_DST(a)=nat_newdstip
3899 *=> OIP_SRC(a)=nat_oldsrcip, OIP_DST(c)=nat_olddstip
3902 if (((fin->fin_out == 0) && ((nat->nat_redir & NAT_MAP) != 0)) ||
3903 ((fin->fin_out == 1) && ((nat->nat_redir & NAT_REDIRECT) != 0))) {
3904 a1.s_addr = ntohl(nat->nat_osrcaddr);
3905 a4.s_addr = ntohl(oip->ip_src.s_addr);
3906 a3.s_addr = ntohl(nat->nat_odstaddr);
3907 a2.s_addr = ntohl(oip->ip_dst.s_addr);
3908 oip->ip_src.s_addr = htonl(a1.s_addr);
3909 oip->ip_dst.s_addr = htonl(a3.s_addr);
3912 a1.s_addr = ntohl(nat->nat_ndstaddr);
3913 a2.s_addr = ntohl(oip->ip_dst.s_addr);
3914 a3.s_addr = ntohl(nat->nat_nsrcaddr);
3915 a4.s_addr = ntohl(oip->ip_src.s_addr);
3916 oip->ip_dst.s_addr = htonl(a3.s_addr);
3917 oip->ip_src.s_addr = htonl(a1.s_addr);
3923 CALC_SUMD(a2.s_addr, a3.s_addr, sum1);
3924 CALC_SUMD(a4.s_addr, a1.s_addr, sum2);
3927 ipf_fix_datacksum(&oip->ip_sum, sumd);
3934 * Fix UDP pseudo header checksum to compensate for the
3935 * IP address change.
3937 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
3938 u_32_t sum3, sum4, sumt;
3942 * For offending TCP/UDP IP packets, translate the ports as
3943 * well, based on the NAT specification. Of course such
3944 * a change may be reflected in the ICMP checksum as well.
3946 * Since the port fields are part of the TCP/UDP checksum
3947 * of the offending IP packet, you need to adjust that checksum
3948 * as well... except that the change in the port numbers should
3949 * be offset by the checksum change. However, the TCP/UDP
3950 * checksum will also need to change if there has been an
3951 * IP address change.
3954 sum1 = ntohs(nat->nat_osport);
3955 sum4 = ntohs(tcp->th_sport);
3956 sum3 = ntohs(nat->nat_odport);
3957 sum2 = ntohs(tcp->th_dport);
3959 tcp->th_sport = htons(sum1);
3960 tcp->th_dport = htons(sum3);
3962 sum1 = ntohs(nat->nat_ndport);
3963 sum2 = ntohs(tcp->th_dport);
3964 sum3 = ntohs(nat->nat_nsport);
3965 sum4 = ntohs(tcp->th_sport);
3967 tcp->th_dport = htons(sum3);
3968 tcp->th_sport = htons(sum1);
3970 CALC_SUMD(sum4, sum1, sumt);
3972 CALC_SUMD(sum2, sum3, sumt);
3975 if (sumd != 0 || sumd2 != 0) {
3977 * At this point, sumd is the delta to apply to the
3978 * TCP/UDP header, given the changes in both the IP
3979 * address and the ports and sumd2 is the delta to
3980 * apply to the ICMP header, given the IP address
3981 * change delta that may need to be applied to the
3982 * TCP/UDP checksum instead.
3984 * If we will both the IP and TCP/UDP checksums
3985 * then the ICMP checksum changes by the address
3986 * delta applied to the TCP/UDP checksum. If we
3987 * do not change the TCP/UDP checksum them we
3988 * apply the delta in ports to the ICMP checksum.
3990 if (oip->ip_p == IPPROTO_UDP) {
3991 if ((dlen >= 8) && (*csump != 0)) {
3992 ipf_fix_datacksum(csump, sumd);
3994 CALC_SUMD(sum1, sum4, sumd2);
3995 CALC_SUMD(sum3, sum2, sumt);
3998 } else if (oip->ip_p == IPPROTO_TCP) {
4000 ipf_fix_datacksum(csump, sumd);
4002 CALC_SUMD(sum1, sum4, sumd2);
4003 CALC_SUMD(sum3, sum2, sumt);
4008 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
4009 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
4010 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
4011 ipf_fix_incksum(0, &icmp->icmp_cksum, sumd2, 0);
4014 } else if (((flags & IPN_ICMPQUERY) != 0) && (dlen >= 8)) {
4018 * XXX - what if this is bogus hl and we go off the end ?
4019 * In this case, ipf_nat_icmperrorlookup() will have
4022 orgicmp = (icmphdr_t *)dp;
4025 if (orgicmp->icmp_id != nat->nat_osport) {
4028 * Fix ICMP checksum (of the offening ICMP
4029 * query packet) to compensate the change
4030 * in the ICMP id of the offending ICMP
4033 * Since you modify orgicmp->icmp_id with
4034 * a delta (say x) and you compensate that
4035 * in origicmp->icmp_cksum with a delta
4036 * minus x, you don't have to adjust the
4037 * overall icmp->icmp_cksum
4039 sum1 = ntohs(orgicmp->icmp_id);
4040 sum2 = ntohs(nat->nat_oicmpid);
4041 CALC_SUMD(sum1, sum2, sumd);
4042 orgicmp->icmp_id = nat->nat_oicmpid;
4043 ipf_fix_datacksum(&orgicmp->icmp_cksum, sumd);
4045 } /* nat_dir == NAT_INBOUND is impossible for icmp queries */
4052 * MAP-IN MAP-OUT RDR-IN RDR-OUT
4053 * osrc X == src == src X
4054 * odst X == dst == dst X
4055 * nsrc == dst X X == dst
4056 * ndst == src X X == src
4057 * MAP = NAT_OUTBOUND, RDR = NAT_INBOUND
4060 * NB: these lookups don't lock access to the list, it assumed that it has
4061 * already been done!
4063 /* ------------------------------------------------------------------------ */
4064 /* Function: ipf_nat_inlookup */
4065 /* Returns: nat_t* - NULL == no match, */
4066 /* else pointer to matching NAT entry */
4067 /* Parameters: fin(I) - pointer to packet information */
4068 /* flags(I) - NAT flags for this packet */
4069 /* p(I) - protocol for this packet */
4070 /* src(I) - source IP address */
4071 /* mapdst(I) - destination IP address */
4073 /* Lookup a nat entry based on the mapped destination ip address/port and */
4074 /* real source address/port. We use this lookup when receiving a packet, */
4075 /* we're looking for a table entry, based on the destination address. */
4077 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */
4079 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */
4080 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */
4082 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */
4083 /* the packet is of said protocol */
4084 /* ------------------------------------------------------------------------ */
4086 ipf_nat_inlookup(fin, flags, p, src, mapdst)
4089 struct in_addr src , mapdst;
4091 ipf_main_softc_t *softc = fin->fin_main_soft;
4092 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4093 u_short sport, dport;
4105 dst = mapdst.s_addr;
4106 sflags = flags & NAT_TCPUDPICMP;
4112 sport = htons(fin->fin_data[0]);
4113 dport = htons(fin->fin_data[1]);
4117 dport = fin->fin_data[1];
4126 if ((flags & SI_WILDP) != 0)
4127 goto find_in_wild_ports;
4129 rhv = NAT_HASH_FN(dst, dport, 0xffffffff);
4130 rhv = NAT_HASH_FN(src.s_addr, rhv + sport, 0xffffffff);
4131 hv = rhv % softn->ipf_nat_table_sz;
4132 nat = softn->ipf_nat_table[1][hv];
4133 /* TRACE dst, dport, src, sport, hv, nat */
4135 for (; nat; nat = nat->nat_hnext[1]) {
4136 if (nat->nat_ifps[0] != NULL) {
4137 if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
4141 if (nat->nat_pr[0] != p)
4144 switch (nat->nat_dir)
4148 if (nat->nat_v[0] != 4)
4150 if (nat->nat_osrcaddr != src.s_addr ||
4151 nat->nat_odstaddr != dst)
4153 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4154 if (nat->nat_osport != sport)
4156 if (nat->nat_odport != dport)
4159 } else if (p == IPPROTO_ICMP) {
4160 if (nat->nat_osport != dport) {
4165 case NAT_DIVERTOUT :
4166 if (nat->nat_dlocal)
4169 if (nat->nat_v[1] != 4)
4171 if (nat->nat_dlocal)
4173 if (nat->nat_dlocal)
4175 if (nat->nat_ndstaddr != src.s_addr ||
4176 nat->nat_nsrcaddr != dst)
4178 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4179 if (nat->nat_ndport != sport)
4181 if (nat->nat_nsport != dport)
4184 } else if (p == IPPROTO_ICMP) {
4185 if (nat->nat_osport != dport) {
4193 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4195 if ((ipn != NULL) && (nat->nat_aps != NULL))
4196 if (ipf_proxy_match(fin, nat) != 0)
4199 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
4200 nat->nat_ifps[0] = ifp;
4201 nat->nat_mtu[0] = GETIFMTU_4(ifp);
4207 * So if we didn't find it but there are wildcard members in the hash
4208 * table, go back and look for them. We do this search and update here
4209 * because it is modifying the NAT table and we want to do this only
4210 * for the first packet that matches. The exception, of course, is
4211 * for "dummy" (FI_IGNORE) lookups.
4214 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
4215 NBUMPSIDEX(0, ns_lookup_miss, ns_lookup_miss_0);
4218 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
4219 NBUMPSIDEX(0, ns_lookup_nowild, ns_lookup_nowild_0);
4223 RWLOCK_EXIT(&softc->ipf_nat);
4225 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
4226 hv = NAT_HASH_FN(src.s_addr, hv, softn->ipf_nat_table_sz);
4227 WRITE_ENTER(&softc->ipf_nat);
4229 nat = softn->ipf_nat_table[1][hv];
4230 /* TRACE dst, src, hv, nat */
4231 for (; nat; nat = nat->nat_hnext[1]) {
4232 if (nat->nat_ifps[0] != NULL) {
4233 if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
4237 if (nat->nat_pr[0] != fin->fin_p)
4240 switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
4243 if (nat->nat_v[0] != 4)
4245 if (nat->nat_osrcaddr != src.s_addr ||
4246 nat->nat_odstaddr != dst)
4250 if (nat->nat_v[1] != 4)
4252 if (nat->nat_ndstaddr != src.s_addr ||
4253 nat->nat_nsrcaddr != dst)
4258 nflags = nat->nat_flags;
4259 if (!(nflags & (NAT_TCPUDP|SI_WILDP)))
4262 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nflags,
4263 NAT_INBOUND) == 1) {
4264 if ((fin->fin_flx & FI_IGNORE) != 0)
4266 if ((nflags & SI_CLONE) != 0) {
4267 nat = ipf_nat_clone(fin, nat);
4271 MUTEX_ENTER(&softn->ipf_nat_new);
4272 softn->ipf_nat_stats.ns_wilds--;
4273 MUTEX_EXIT(&softn->ipf_nat_new);
4276 if (nat->nat_dir == NAT_INBOUND) {
4277 if (nat->nat_osport == 0) {
4278 nat->nat_osport = sport;
4279 nat->nat_nsport = sport;
4281 if (nat->nat_odport == 0) {
4282 nat->nat_odport = dport;
4283 nat->nat_ndport = dport;
4285 } else if (nat->nat_dir == NAT_OUTBOUND) {
4286 if (nat->nat_osport == 0) {
4287 nat->nat_osport = dport;
4288 nat->nat_nsport = dport;
4290 if (nat->nat_odport == 0) {
4291 nat->nat_odport = sport;
4292 nat->nat_ndport = sport;
4295 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
4296 nat->nat_ifps[0] = ifp;
4297 nat->nat_mtu[0] = GETIFMTU_4(ifp);
4299 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
4300 ipf_nat_tabmove(softn, nat);
4305 MUTEX_DOWNGRADE(&softc->ipf_nat);
4308 NBUMPSIDE(0, ns_lookup_miss);
4314 /* ------------------------------------------------------------------------ */
4315 /* Function: ipf_nat_tabmove */
4317 /* Parameters: softn(I) - pointer to NAT context structure */
4318 /* nat(I) - pointer to NAT structure */
4319 /* Write Lock: ipf_nat */
4321 /* This function is only called for TCP/UDP NAT table entries where the */
4322 /* original was placed in the table without hashing on the ports and we now */
4323 /* want to include hashing on port numbers. */
4324 /* ------------------------------------------------------------------------ */
4326 ipf_nat_tabmove(softn, nat)
4327 ipf_nat_softc_t *softn;
4330 u_int hv0, hv1, rhv0, rhv1;
4334 if (nat->nat_flags & SI_CLONE)
4337 nsp = &softn->ipf_nat_stats;
4339 * Remove the NAT entry from the old location
4341 if (nat->nat_hnext[0])
4342 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
4343 *nat->nat_phnext[0] = nat->nat_hnext[0];
4344 nsp->ns_side[0].ns_bucketlen[nat->nat_hv[0] %
4345 softn->ipf_nat_table_sz]--;
4347 if (nat->nat_hnext[1])
4348 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
4349 *nat->nat_phnext[1] = nat->nat_hnext[1];
4350 nsp->ns_side[1].ns_bucketlen[nat->nat_hv[1] %
4351 softn->ipf_nat_table_sz]--;
4354 * Add into the NAT table in the new position
4356 rhv0 = NAT_HASH_FN(nat->nat_osrcaddr, nat->nat_osport, 0xffffffff);
4357 rhv0 = NAT_HASH_FN(nat->nat_odstaddr, rhv0 + nat->nat_odport,
4359 rhv1 = NAT_HASH_FN(nat->nat_nsrcaddr, nat->nat_nsport, 0xffffffff);
4360 rhv1 = NAT_HASH_FN(nat->nat_ndstaddr, rhv1 + nat->nat_ndport,
4363 hv0 = rhv0 % softn->ipf_nat_table_sz;
4364 hv1 = rhv1 % softn->ipf_nat_table_sz;
4366 if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_DIVERTIN) {
4374 /* TRACE nat_osrcaddr, nat_osport, nat_odstaddr, nat_odport, hv0 */
4375 /* TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr, nat_ndport, hv1 */
4377 nat->nat_hv[0] = rhv0;
4378 natp = &softn->ipf_nat_table[0][hv0];
4380 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
4381 nat->nat_phnext[0] = natp;
4382 nat->nat_hnext[0] = *natp;
4384 nsp->ns_side[0].ns_bucketlen[hv0]++;
4386 nat->nat_hv[1] = rhv1;
4387 natp = &softn->ipf_nat_table[1][hv1];
4389 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
4390 nat->nat_phnext[1] = natp;
4391 nat->nat_hnext[1] = *natp;
4393 nsp->ns_side[1].ns_bucketlen[hv1]++;
4397 /* ------------------------------------------------------------------------ */
4398 /* Function: ipf_nat_outlookup */
4399 /* Returns: nat_t* - NULL == no match, */
4400 /* else pointer to matching NAT entry */
4401 /* Parameters: fin(I) - pointer to packet information */
4402 /* flags(I) - NAT flags for this packet */
4403 /* p(I) - protocol for this packet */
4404 /* src(I) - source IP address */
4405 /* dst(I) - destination IP address */
4406 /* rw(I) - 1 == write lock on held, 0 == read lock. */
4408 /* Lookup a nat entry based on the source 'real' ip address/port and */
4409 /* destination address/port. We use this lookup when sending a packet out, */
4410 /* we're looking for a table entry, based on the source address. */
4412 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */
4414 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */
4415 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */
4417 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */
4418 /* the packet is of said protocol */
4419 /* ------------------------------------------------------------------------ */
4421 ipf_nat_outlookup(fin, flags, p, src, dst)
4424 struct in_addr src , dst;
4426 ipf_main_softc_t *softc = fin->fin_main_soft;
4427 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4428 u_short sport, dport;
4436 sflags = flags & IPN_TCPUDPICMP;
4442 sport = htons(fin->fin_data[0]);
4443 dport = htons(fin->fin_data[1]);
4447 dport = fin->fin_data[1];
4455 if ((flags & SI_WILDP) != 0)
4456 goto find_out_wild_ports;
4458 hv = NAT_HASH_FN(src.s_addr, sport, 0xffffffff);
4459 hv = NAT_HASH_FN(dst.s_addr, hv + dport, softn->ipf_nat_table_sz);
4460 nat = softn->ipf_nat_table[0][hv];
4462 /* TRACE src, sport, dst, dport, hv, nat */
4464 for (; nat; nat = nat->nat_hnext[0]) {
4465 if (nat->nat_ifps[1] != NULL) {
4466 if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
4470 if (nat->nat_pr[1] != p)
4473 switch (nat->nat_dir)
4477 if (nat->nat_v[1] != 4)
4479 if (nat->nat_ndstaddr != src.s_addr ||
4480 nat->nat_nsrcaddr != dst.s_addr)
4483 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4484 if (nat->nat_ndport != sport)
4486 if (nat->nat_nsport != dport)
4489 } else if (p == IPPROTO_ICMP) {
4490 if (nat->nat_osport != dport) {
4496 case NAT_DIVERTOUT :
4497 if (nat->nat_v[0] != 4)
4499 if (nat->nat_osrcaddr != src.s_addr ||
4500 nat->nat_odstaddr != dst.s_addr)
4503 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4504 if (nat->nat_odport != dport)
4506 if (nat->nat_osport != sport)
4509 } else if (p == IPPROTO_ICMP) {
4510 if (nat->nat_osport != dport) {
4518 if ((ipn != NULL) && (nat->nat_aps != NULL))
4519 if (ipf_proxy_match(fin, nat) != 0)
4522 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
4523 nat->nat_ifps[1] = ifp;
4524 nat->nat_mtu[1] = GETIFMTU_4(ifp);
4530 * So if we didn't find it but there are wildcard members in the hash
4531 * table, go back and look for them. We do this search and update here
4532 * because it is modifying the NAT table and we want to do this only
4533 * for the first packet that matches. The exception, of course, is
4534 * for "dummy" (FI_IGNORE) lookups.
4536 find_out_wild_ports:
4537 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
4538 NBUMPSIDEX(1, ns_lookup_miss, ns_lookup_miss_1);
4541 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
4542 NBUMPSIDEX(1, ns_lookup_nowild, ns_lookup_nowild_1);
4546 RWLOCK_EXIT(&softc->ipf_nat);
4548 hv = NAT_HASH_FN(src.s_addr, 0, 0xffffffff);
4549 hv = NAT_HASH_FN(dst.s_addr, hv, softn->ipf_nat_table_sz);
4551 WRITE_ENTER(&softc->ipf_nat);
4553 nat = softn->ipf_nat_table[0][hv];
4554 for (; nat; nat = nat->nat_hnext[0]) {
4555 if (nat->nat_ifps[1] != NULL) {
4556 if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
4560 if (nat->nat_pr[1] != fin->fin_p)
4563 switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
4566 if (nat->nat_v[1] != 4)
4568 if (nat->nat_ndstaddr != src.s_addr ||
4569 nat->nat_nsrcaddr != dst.s_addr)
4573 if (nat->nat_v[0] != 4)
4575 if (nat->nat_osrcaddr != src.s_addr ||
4576 nat->nat_odstaddr != dst.s_addr)
4581 if (!(nat->nat_flags & (NAT_TCPUDP|SI_WILDP)))
4584 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nat->nat_flags,
4585 NAT_OUTBOUND) == 1) {
4586 if ((fin->fin_flx & FI_IGNORE) != 0)
4588 if ((nat->nat_flags & SI_CLONE) != 0) {
4589 nat = ipf_nat_clone(fin, nat);
4593 MUTEX_ENTER(&softn->ipf_nat_new);
4594 softn->ipf_nat_stats.ns_wilds--;
4595 MUTEX_EXIT(&softn->ipf_nat_new);
4598 if (nat->nat_dir == NAT_OUTBOUND) {
4599 if (nat->nat_osport == 0) {
4600 nat->nat_osport = sport;
4601 nat->nat_nsport = sport;
4603 if (nat->nat_odport == 0) {
4604 nat->nat_odport = dport;
4605 nat->nat_ndport = dport;
4607 } else if (nat->nat_dir == NAT_INBOUND) {
4608 if (nat->nat_osport == 0) {
4609 nat->nat_osport = dport;
4610 nat->nat_nsport = dport;
4612 if (nat->nat_odport == 0) {
4613 nat->nat_odport = sport;
4614 nat->nat_ndport = sport;
4617 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
4618 nat->nat_ifps[1] = ifp;
4619 nat->nat_mtu[1] = GETIFMTU_4(ifp);
4621 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
4622 ipf_nat_tabmove(softn, nat);
4627 MUTEX_DOWNGRADE(&softc->ipf_nat);
4630 NBUMPSIDE(1, ns_lookup_miss);
4636 /* ------------------------------------------------------------------------ */
4637 /* Function: ipf_nat_lookupredir */
4638 /* Returns: nat_t* - NULL == no match, */
4639 /* else pointer to matching NAT entry */
4640 /* Parameters: np(I) - pointer to description of packet to find NAT table */
4643 /* Lookup the NAT tables to search for a matching redirect */
4644 /* The contents of natlookup_t should imitate those found in a packet that */
4645 /* would be translated - ie a packet coming in for RDR or going out for MAP.*/
4646 /* We can do the lookup in one of two ways, imitating an inbound or */
4647 /* outbound packet. By default we assume outbound, unless IPN_IN is set. */
4648 /* For IN, the fields are set as follows: */
4649 /* nl_real* = source information */
4650 /* nl_out* = destination information (translated) */
4651 /* For an out packet, the fields are set like this: */
4652 /* nl_in* = source information (untranslated) */
4653 /* nl_out* = destination information (translated) */
4654 /* ------------------------------------------------------------------------ */
4656 ipf_nat_lookupredir(np)
4662 bzero((char *)&fi, sizeof(fi));
4663 if (np->nl_flags & IPN_IN) {
4664 fi.fin_data[0] = ntohs(np->nl_realport);
4665 fi.fin_data[1] = ntohs(np->nl_outport);
4667 fi.fin_data[0] = ntohs(np->nl_inport);
4668 fi.fin_data[1] = ntohs(np->nl_outport);
4670 if (np->nl_flags & IPN_TCP)
4671 fi.fin_p = IPPROTO_TCP;
4672 else if (np->nl_flags & IPN_UDP)
4673 fi.fin_p = IPPROTO_UDP;
4674 else if (np->nl_flags & (IPN_ICMPERR|IPN_ICMPQUERY))
4675 fi.fin_p = IPPROTO_ICMP;
4678 * We can do two sorts of lookups:
4679 * - IPN_IN: we have the `real' and `out' address, look for `in'.
4680 * - default: we have the `in' and `out' address, look for `real'.
4682 if (np->nl_flags & IPN_IN) {
4683 if ((nat = ipf_nat_inlookup(&fi, np->nl_flags, fi.fin_p,
4684 np->nl_realip, np->nl_outip))) {
4685 np->nl_inip = nat->nat_odstip;
4686 np->nl_inport = nat->nat_odport;
4690 * If nl_inip is non null, this is a lookup based on the real
4691 * ip address. Else, we use the fake.
4693 if ((nat = ipf_nat_outlookup(&fi, np->nl_flags, fi.fin_p,
4694 np->nl_inip, np->nl_outip))) {
4696 if ((np->nl_flags & IPN_FINDFORWARD) != 0) {
4698 bzero((char *)&fin, sizeof(fin));
4699 fin.fin_p = nat->nat_pr[0];
4700 fin.fin_data[0] = ntohs(nat->nat_ndport);
4701 fin.fin_data[1] = ntohs(nat->nat_nsport);
4702 if (ipf_nat_inlookup(&fin, np->nl_flags,
4703 fin.fin_p, nat->nat_ndstip,
4704 nat->nat_nsrcip) != NULL) {
4705 np->nl_flags &= ~IPN_FINDFORWARD;
4709 np->nl_realip = nat->nat_odstip;
4710 np->nl_realport = nat->nat_odport;
4718 /* ------------------------------------------------------------------------ */
4719 /* Function: ipf_nat_match */
4720 /* Returns: int - 0 == no match, 1 == match */
4721 /* Parameters: fin(I) - pointer to packet information */
4722 /* np(I) - pointer to NAT rule */
4724 /* Pull the matching of a packet against a NAT rule out of that complex */
4725 /* loop inside ipf_nat_checkin() and lay it out properly in its own function. */
4726 /* ------------------------------------------------------------------------ */
4728 ipf_nat_match(fin, np)
4732 ipf_main_softc_t *softc = fin->fin_main_soft;
4737 switch (np->in_osrcatype)
4740 match = ((fin->fin_saddr & np->in_osrcmsk) != np->in_osrcaddr);
4743 match = (*np->in_osrcfunc)(softc, np->in_osrcptr,
4744 4, &fin->fin_saddr, fin->fin_plen);
4747 match ^= ((np->in_flags & IPN_NOTSRC) != 0);
4752 switch (np->in_odstatype)
4755 match = ((fin->fin_daddr & np->in_odstmsk) != np->in_odstaddr);
4758 match = (*np->in_odstfunc)(softc, np->in_odstptr,
4759 4, &fin->fin_daddr, fin->fin_plen);
4763 match ^= ((np->in_flags & IPN_NOTDST) != 0);
4768 if (!(fin->fin_flx & FI_TCPUDP) ||
4769 (fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
4770 if (ft->ftu_scmp || ft->ftu_dcmp)
4775 return ipf_tcpudpchk(&fin->fin_fi, ft);
4779 /* ------------------------------------------------------------------------ */
4780 /* Function: ipf_nat_update */
4782 /* Parameters: fin(I) - pointer to packet information */
4783 /* nat(I) - pointer to NAT structure */
4785 /* Updates the lifetime of a NAT table entry for non-TCP packets. Must be */
4786 /* called with fin_rev updated - i.e. after calling ipf_nat_proto(). */
4788 /* This *MUST* be called after ipf_nat_proto() as it expects fin_rev to */
4789 /* already be set. */
4790 /* ------------------------------------------------------------------------ */
4792 ipf_nat_update(fin, nat)
4796 ipf_main_softc_t *softc = fin->fin_main_soft;
4797 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4798 ipftq_t *ifq, *ifq2;
4800 ipnat_t *np = nat->nat_ptr;
4802 tqe = &nat->nat_tqe;
4806 * We allow over-riding of NAT timeouts from NAT rules, even for
4807 * TCP, however, if it is TCP and there is no rule timeout set,
4808 * then do not update the timeout here.
4811 np->in_bytes[fin->fin_rev] += fin->fin_plen;
4812 ifq2 = np->in_tqehead[fin->fin_rev];
4817 if (nat->nat_pr[0] == IPPROTO_TCP && ifq2 == NULL) {
4818 (void) ipf_tcp_age(&nat->nat_tqe, fin, softn->ipf_nat_tcptq,
4822 if (nat->nat_pr[0] == IPPROTO_UDP)
4823 ifq2 = fin->fin_rev ? &softn->ipf_nat_udpacktq :
4824 &softn->ipf_nat_udptq;
4825 else if (nat->nat_pr[0] == IPPROTO_ICMP ||
4826 nat->nat_pr[0] == IPPROTO_ICMPV6)
4827 ifq2 = fin->fin_rev ? &softn->ipf_nat_icmpacktq:
4828 &softn->ipf_nat_icmptq;
4830 ifq2 = &softn->ipf_nat_iptq;
4833 ipf_movequeue(softc->ipf_ticks, tqe, ifq, ifq2);
4838 /* ------------------------------------------------------------------------ */
4839 /* Function: ipf_nat_checkout */
4840 /* Returns: int - -1 == packet failed NAT checks so block it, */
4841 /* 0 == no packet translation occurred, */
4842 /* 1 == packet was successfully translated. */
4843 /* Parameters: fin(I) - pointer to packet information */
4844 /* passp(I) - pointer to filtering result flags */
4846 /* Check to see if an outcoming packet should be changed. ICMP packets are */
4847 /* first checked to see if they match an existing entry (if an error), */
4848 /* otherwise a search of the current NAT table is made. If neither results */
4849 /* in a match then a search for a matching NAT rule is made. Create a new */
4850 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */
4851 /* packet header(s) as required. */
4852 /* ------------------------------------------------------------------------ */
4854 ipf_nat_checkout(fin, passp)
4858 ipnat_t *np = NULL, *npnext;
4859 struct ifnet *ifp, *sifp;
4860 ipf_main_softc_t *softc;
4861 ipf_nat_softc_t *softn;
4862 icmphdr_t *icmp = NULL;
4863 tcphdr_t *tcp = NULL;
4864 int rval, natfailed;
4871 if (fin->fin_v == 6) {
4873 return ipf_nat6_checkout(fin, passp);
4879 softc = fin->fin_main_soft;
4880 softn = softc->ipf_nat_soft;
4882 if (softn->ipf_nat_lock != 0)
4884 if (softn->ipf_nat_stats.ns_rules == 0 &&
4885 softn->ipf_nat_instances == NULL)
4890 sifp = fin->fin_ifp;
4892 ifp = fr->fr_tifs[fin->fin_rev].fd_ptr;
4893 if ((ifp != NULL) && (ifp != (void *)-1))
4898 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
4911 * This is an incoming packet, so the destination is
4912 * the icmp_id and the source port equals 0
4914 if ((fin->fin_flx & FI_ICMPQUERY) != 0)
4915 nflags = IPN_ICMPQUERY;
4921 if ((nflags & IPN_TCPUDP))
4925 ipa = fin->fin_saddr;
4927 READ_ENTER(&softc->ipf_nat);
4929 if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
4930 (nat = ipf_nat_icmperror(fin, &nflags, NAT_OUTBOUND)))
4932 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
4934 else if ((nat = ipf_nat_outlookup(fin, nflags|NAT_SEARCH,
4935 (u_int)fin->fin_p, fin->fin_src,
4937 nflags = nat->nat_flags;
4938 } else if (fin->fin_off == 0) {
4939 u_32_t hv, msk, nmsk = 0;
4942 * If there is no current entry in the nat table for this IP#,
4943 * create one for it (if there is a matching rule).
4946 msk = softn->ipf_nat_map_active_masks[nmsk];
4948 hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_maprules_sz);
4950 for (np = softn->ipf_nat_map_rules[hv]; np; np = npnext) {
4951 npnext = np->in_mnext;
4952 if ((np->in_ifps[1] && (np->in_ifps[1] != ifp)))
4954 if (np->in_v[0] != 4)
4956 if (np->in_pr[1] && (np->in_pr[1] != fin->fin_p))
4958 if ((np->in_flags & IPN_RF) &&
4959 !(np->in_flags & nflags))
4961 if (np->in_flags & IPN_FILTER) {
4962 switch (ipf_nat_match(fin, np))
4973 } else if ((ipa & np->in_osrcmsk) != np->in_osrcaddr)
4977 !ipf_matchtag(&np->in_tag, &fr->fr_nattag))
4980 if (np->in_plabel != -1) {
4981 if (((np->in_flags & IPN_FILTER) == 0) &&
4982 (np->in_odport != fin->fin_data[1]))
4984 if (ipf_proxy_ok(fin, tcp, np) == 0)
4988 if (np->in_flags & IPN_NO) {
4992 MUTEX_ENTER(&softn->ipf_nat_new);
4994 * If we've matched a round-robin rule but it has
4995 * moved in the list since we got it, start over as
4996 * this is now no longer correct.
4998 if (npnext != np->in_mnext) {
4999 if ((np->in_flags & IPN_ROUNDR) != 0) {
5000 MUTEX_EXIT(&softn->ipf_nat_new);
5001 goto retry_roundrobin;
5003 npnext = np->in_mnext;
5006 nat = ipf_nat_add(fin, np, NULL, nflags, NAT_OUTBOUND);
5007 MUTEX_EXIT(&softn->ipf_nat_new);
5014 if ((np == NULL) && (nmsk < softn->ipf_nat_map_max)) {
5021 rval = ipf_nat_out(fin, nat, natadd, nflags);
5023 MUTEX_ENTER(&nat->nat_lock);
5024 ipf_nat_update(fin, nat);
5025 nat->nat_bytes[1] += fin->fin_plen;
5027 fin->fin_pktnum = nat->nat_pkts[1];
5028 MUTEX_EXIT(&nat->nat_lock);
5033 RWLOCK_EXIT(&softc->ipf_nat);
5038 /* ipf_nat_match() failure */
5041 /* retry_roundrobin loop failure */
5044 /* proxy failure detected by ipf_nat_out() */
5045 if (passp != NULL) {
5046 DT2(frb_natv4out, fr_info_t *, fin, int, rval);
5047 NBUMPSIDED(1, ns_drop);
5049 fin->fin_reason = FRB_NATV4;
5051 fin->fin_flx |= FI_BADNAT;
5052 NBUMPSIDED(1, ns_badnat);
5053 rval = -1; /* We only return -1 on error. */
5056 NBUMPSIDE(1, ns_ignored);
5059 NBUMPSIDE(1, ns_translated);
5062 fin->fin_ifp = sifp;
5066 /* ------------------------------------------------------------------------ */
5067 /* Function: ipf_nat_out */
5068 /* Returns: int - -1 == packet failed NAT checks so block it, */
5069 /* 1 == packet was successfully translated. */
5070 /* Parameters: fin(I) - pointer to packet information */
5071 /* nat(I) - pointer to NAT structure */
5072 /* natadd(I) - flag indicating if it is safe to add frag cache */
5073 /* nflags(I) - NAT flags set for this packet */
5075 /* Translate a packet coming "out" on an interface. */
5076 /* ------------------------------------------------------------------------ */
5078 ipf_nat_out(fin, nat, natadd, nflags)
5084 ipf_main_softc_t *softc = fin->fin_main_soft;
5085 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5096 if ((natadd != 0) && (fin->fin_flx & FI_FRAG) && (np != NULL))
5097 (void) ipf_frag_natnew(softc, fin, 0, nat);
5100 * Fix up checksums, not by recalculating them, but
5101 * simply computing adjustments.
5102 * This is only done for STREAMS based IP implementations where the
5103 * checksum has already been calculated by IP. In all other cases,
5104 * IPFilter is called before the checksum needs calculating so there
5105 * is no call to modify whatever is in the header now.
5107 if (nflags == IPN_ICMPERR) {
5108 u_32_t s1, s2, sumd, msumd;
5110 s1 = LONG_SUM(ntohl(fin->fin_saddr));
5111 if (nat->nat_dir == NAT_OUTBOUND) {
5112 s2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
5114 s2 = LONG_SUM(ntohl(nat->nat_odstaddr));
5116 CALC_SUMD(s1, s2, sumd);
5119 s1 = LONG_SUM(ntohl(fin->fin_daddr));
5120 if (nat->nat_dir == NAT_OUTBOUND) {
5121 s2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
5123 s2 = LONG_SUM(ntohl(nat->nat_osrcaddr));
5125 CALC_SUMD(s1, s2, sumd);
5128 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, msumd, 0);
5130 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5131 defined(linux) || defined(BRIDGE_IPF) || defined(__FreeBSD__)
5134 * Strictly speaking, this isn't necessary on BSD
5135 * kernels because they do checksum calculation after
5136 * this code has run BUT if ipfilter is being used
5137 * to do NAT as a bridge, that code doesn't exist.
5139 switch (nat->nat_dir)
5142 ipf_fix_outcksum(fin->fin_cksum & FI_CK_L4PART,
5143 &fin->fin_ip->ip_sum,
5144 nat->nat_ipsumd, 0);
5148 ipf_fix_incksum(fin->fin_cksum & FI_CK_L4PART,
5149 &fin->fin_ip->ip_sum,
5150 nat->nat_ipsumd, 0);
5160 * Address assignment is after the checksum modification because
5161 * we are using the address in the packet for determining the
5162 * correct checksum offset (the ICMP error could be coming from
5165 switch (nat->nat_dir)
5168 fin->fin_ip->ip_src = nat->nat_nsrcip;
5169 fin->fin_saddr = nat->nat_nsrcaddr;
5170 fin->fin_ip->ip_dst = nat->nat_ndstip;
5171 fin->fin_daddr = nat->nat_ndstaddr;
5175 fin->fin_ip->ip_src = nat->nat_odstip;
5176 fin->fin_saddr = nat->nat_ndstaddr;
5177 fin->fin_ip->ip_dst = nat->nat_osrcip;
5178 fin->fin_daddr = nat->nat_nsrcaddr;
5185 skip = ipf_nat_decap(fin, nat);
5187 NBUMPSIDED(1, ns_decap_fail);
5193 #if defined(MENTAT) && defined(_KERNEL)
5200 if (m->m_flags & M_PKTHDR)
5201 m->m_pkthdr.len -= skip;
5205 MUTEX_ENTER(&nat->nat_lock);
5206 ipf_nat_update(fin, nat);
5207 MUTEX_EXIT(&nat->nat_lock);
5208 fin->fin_flx |= FI_NATED;
5209 if (np != NULL && np->in_tag.ipt_num[0] != 0)
5210 fin->fin_nattag = &np->in_tag;
5215 case NAT_DIVERTOUT :
5217 u_32_t s1, s2, sumd;
5222 m = M_DUP(np->in_divmp);
5224 NBUMPSIDED(1, ns_divert_dup);
5228 ip = MTOD(m, ip_t *);
5229 ip->ip_id = htons(ipf_nextipid(fin));
5230 s2 = ntohs(ip->ip_id);
5233 ip->ip_len = ntohs(ip->ip_len);
5234 ip->ip_len += fin->fin_plen;
5235 ip->ip_len = htons(ip->ip_len);
5236 s2 += ntohs(ip->ip_len);
5237 CALC_SUMD(s1, s2, sumd);
5239 uh = (udphdr_t *)(ip + 1);
5240 uh->uh_ulen += fin->fin_plen;
5241 uh->uh_ulen = htons(uh->uh_ulen);
5242 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5243 defined(linux) || defined(BRIDGE_IPF) || defined(__FreeBSD__)
5244 ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0);
5249 fin->fin_src = ip->ip_src;
5250 fin->fin_dst = ip->ip_dst;
5252 fin->fin_plen += sizeof(ip_t) + 8; /* UDP + IPv4 hdr */
5253 fin->fin_dlen += sizeof(ip_t) + 8; /* UDP + IPv4 hdr */
5255 nflags &= ~IPN_TCPUDPICMP;
5264 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5267 if ((nat->nat_nsport != 0) && (nflags & IPN_TCPUDP)) {
5270 switch (nat->nat_dir)
5273 tcp->th_sport = nat->nat_nsport;
5274 fin->fin_data[0] = ntohs(nat->nat_nsport);
5275 tcp->th_dport = nat->nat_ndport;
5276 fin->fin_data[1] = ntohs(nat->nat_ndport);
5280 tcp->th_sport = nat->nat_odport;
5281 fin->fin_data[0] = ntohs(nat->nat_odport);
5282 tcp->th_dport = nat->nat_osport;
5283 fin->fin_data[1] = ntohs(nat->nat_osport);
5288 if ((nat->nat_nsport != 0) && (nflags & IPN_ICMPQUERY)) {
5290 icmp->icmp_id = nat->nat_nicmpid;
5293 csump = ipf_nat_proto(fin, nat, nflags);
5296 * The above comments do not hold for layer 4 (or higher)
5299 if (csump != NULL) {
5300 if (nat->nat_dir == NAT_OUTBOUND)
5301 ipf_fix_outcksum(fin->fin_cksum, csump,
5306 ipf_fix_incksum(fin->fin_cksum, csump,
5313 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
5314 /* ------------------------------------------------------------- */
5315 /* A few quick notes: */
5316 /* Following are test conditions prior to calling the */
5317 /* ipf_proxy_check routine. */
5319 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */
5320 /* with a redirect rule, we attempt to match the packet's */
5321 /* source port against in_dport, otherwise we'd compare the */
5322 /* packet's destination. */
5323 /* ------------------------------------------------------------- */
5324 if ((np != NULL) && (np->in_apr != NULL)) {
5325 i = ipf_proxy_check(fin, nat);
5328 } else if (i == -1) {
5329 NBUMPSIDED(1, ns_ipf_proxy_fail);
5334 fin->fin_flx |= FI_NATED;
5339 /* ------------------------------------------------------------------------ */
5340 /* Function: ipf_nat_checkin */
5341 /* Returns: int - -1 == packet failed NAT checks so block it, */
5342 /* 0 == no packet translation occurred, */
5343 /* 1 == packet was successfully translated. */
5344 /* Parameters: fin(I) - pointer to packet information */
5345 /* passp(I) - pointer to filtering result flags */
5347 /* Check to see if an incoming packet should be changed. ICMP packets are */
5348 /* first checked to see if they match an existing entry (if an error), */
5349 /* otherwise a search of the current NAT table is made. If neither results */
5350 /* in a match then a search for a matching NAT rule is made. Create a new */
5351 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */
5352 /* packet header(s) as required. */
5353 /* ------------------------------------------------------------------------ */
5355 ipf_nat_checkin(fin, passp)
5359 ipf_main_softc_t *softc;
5360 ipf_nat_softc_t *softn;
5361 u_int nflags, natadd;
5362 ipnat_t *np, *npnext;
5363 int rval, natfailed;
5372 softc = fin->fin_main_soft;
5373 softn = softc->ipf_nat_soft;
5375 if (softn->ipf_nat_lock != 0)
5377 if (softn->ipf_nat_stats.ns_rules == 0 &&
5378 softn->ipf_nat_instances == NULL)
5389 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5402 * This is an incoming packet, so the destination is
5403 * the icmp_id and the source port equals 0
5405 if ((fin->fin_flx & FI_ICMPQUERY) != 0) {
5406 nflags = IPN_ICMPQUERY;
5407 dport = icmp->icmp_id;
5413 if ((nflags & IPN_TCPUDP)) {
5415 dport = fin->fin_data[1];
5421 READ_ENTER(&softc->ipf_nat);
5423 if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
5424 (nat = ipf_nat_icmperror(fin, &nflags, NAT_INBOUND)))
5426 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
5428 else if ((nat = ipf_nat_inlookup(fin, nflags|NAT_SEARCH,
5430 fin->fin_src, in))) {
5431 nflags = nat->nat_flags;
5432 } else if (fin->fin_off == 0) {
5433 u_32_t hv, msk, rmsk = 0;
5436 * If there is no current entry in the nat table for this IP#,
5437 * create one for it (if there is a matching rule).
5440 msk = softn->ipf_nat_rdr_active_masks[rmsk];
5441 iph = in.s_addr & msk;
5442 hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_rdrrules_sz);
5444 /* TRACE (iph,msk,rmsk,hv,softn->ipf_nat_rdrrules_sz) */
5445 for (np = softn->ipf_nat_rdr_rules[hv]; np; np = npnext) {
5446 npnext = np->in_rnext;
5447 if (np->in_ifps[0] && (np->in_ifps[0] != ifp))
5449 if (np->in_v[0] != 4)
5451 if (np->in_pr[0] && (np->in_pr[0] != fin->fin_p))
5453 if ((np->in_flags & IPN_RF) && !(np->in_flags & nflags))
5455 if (np->in_flags & IPN_FILTER) {
5456 switch (ipf_nat_match(fin, np))
5468 if ((in.s_addr & np->in_odstmsk) !=
5471 if (np->in_odport &&
5472 ((np->in_dtop < dport) ||
5473 (dport < np->in_odport)))
5477 if (np->in_plabel != -1) {
5478 if (!ipf_proxy_ok(fin, tcp, np)) {
5483 if (np->in_flags & IPN_NO) {
5488 MUTEX_ENTER(&softn->ipf_nat_new);
5490 * If we've matched a round-robin rule but it has
5491 * moved in the list since we got it, start over as
5492 * this is now no longer correct.
5494 if (npnext != np->in_rnext) {
5495 if ((np->in_flags & IPN_ROUNDR) != 0) {
5496 MUTEX_EXIT(&softn->ipf_nat_new);
5497 goto retry_roundrobin;
5499 npnext = np->in_rnext;
5502 nat = ipf_nat_add(fin, np, NULL, nflags, NAT_INBOUND);
5503 MUTEX_EXIT(&softn->ipf_nat_new);
5510 if ((np == NULL) && (rmsk < softn->ipf_nat_rdr_max)) {
5517 rval = ipf_nat_in(fin, nat, natadd, nflags);
5519 MUTEX_ENTER(&nat->nat_lock);
5520 ipf_nat_update(fin, nat);
5521 nat->nat_bytes[0] += fin->fin_plen;
5523 fin->fin_pktnum = nat->nat_pkts[0];
5524 MUTEX_EXIT(&nat->nat_lock);
5529 RWLOCK_EXIT(&softc->ipf_nat);
5534 /* ipf_nat_match() failure */
5537 /* retry_roundrobin loop failure */
5540 /* proxy failure detected by ipf_nat_in() */
5541 if (passp != NULL) {
5542 DT2(frb_natv4in, fr_info_t *, fin, int, rval);
5543 NBUMPSIDED(0, ns_drop);
5545 fin->fin_reason = FRB_NATV4;
5547 fin->fin_flx |= FI_BADNAT;
5548 NBUMPSIDED(0, ns_badnat);
5549 rval = -1; /* We only return -1 on error. */
5552 NBUMPSIDE(0, ns_ignored);
5555 NBUMPSIDE(0, ns_translated);
5562 /* ------------------------------------------------------------------------ */
5563 /* Function: ipf_nat_in */
5564 /* Returns: int - -1 == packet failed NAT checks so block it, */
5565 /* 1 == packet was successfully translated. */
5566 /* Parameters: fin(I) - pointer to packet information */
5567 /* nat(I) - pointer to NAT structure */
5568 /* natadd(I) - flag indicating if it is safe to add frag cache */
5569 /* nflags(I) - NAT flags set for this packet */
5570 /* Locks Held: ipf_nat(READ) */
5572 /* Translate a packet coming "in" on an interface. */
5573 /* ------------------------------------------------------------------------ */
5575 ipf_nat_in(fin, nat, natadd, nflags)
5581 ipf_main_softc_t *softc = fin->fin_main_soft;
5582 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5583 u_32_t sumd, ipsumd, sum1, sum2;
5592 fin->fin_fr = nat->nat_fr;
5595 if ((natadd != 0) && (fin->fin_flx & FI_FRAG))
5596 (void) ipf_frag_natnew(softc, fin, 0, nat);
5598 /* ------------------------------------------------------------- */
5599 /* A few quick notes: */
5600 /* Following are test conditions prior to calling the */
5601 /* ipf_proxy_check routine. */
5603 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */
5604 /* with a map rule, we attempt to match the packet's */
5605 /* source port against in_dport, otherwise we'd compare the */
5606 /* packet's destination. */
5607 /* ------------------------------------------------------------- */
5608 if (np->in_apr != NULL) {
5609 i = ipf_proxy_check(fin, nat);
5611 NBUMPSIDED(0, ns_ipf_proxy_fail);
5617 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
5619 ipsumd = nat->nat_ipsumd;
5621 * Fix up checksums, not by recalculating them, but
5622 * simply computing adjustments.
5623 * Why only do this for some platforms on inbound packets ?
5624 * Because for those that it is done, IP processing is yet to happen
5625 * and so the IPv4 header checksum has not yet been evaluated.
5626 * Perhaps it should always be done for the benefit of things like
5627 * fast forwarding (so that it doesn't need to be recomputed) but with
5628 * header checksum offloading, perhaps it is a moot point.
5631 switch (nat->nat_dir)
5634 if ((fin->fin_flx & FI_ICMPERR) == 0) {
5635 fin->fin_ip->ip_src = nat->nat_nsrcip;
5636 fin->fin_saddr = nat->nat_nsrcaddr;
5638 sum1 = nat->nat_osrcaddr;
5639 sum2 = nat->nat_nsrcaddr;
5640 CALC_SUMD(sum1, sum2, sumd);
5643 fin->fin_ip->ip_dst = nat->nat_ndstip;
5644 fin->fin_daddr = nat->nat_ndstaddr;
5645 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5646 defined(__osf__) || defined(linux)
5647 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, ipsumd, 0);
5652 if ((fin->fin_flx & FI_ICMPERR) == 0) {
5653 fin->fin_ip->ip_src = nat->nat_odstip;
5654 fin->fin_saddr = nat->nat_odstaddr;
5656 sum1 = nat->nat_odstaddr;
5657 sum2 = nat->nat_ndstaddr;
5658 CALC_SUMD(sum1, sum2, sumd);
5661 fin->fin_ip->ip_dst = nat->nat_osrcip;
5662 fin->fin_daddr = nat->nat_osrcaddr;
5663 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5664 defined(__osf__) || defined(linux)
5665 ipf_fix_incksum(0, &fin->fin_ip->ip_sum, ipsumd, 0);
5675 m = M_DUP(np->in_divmp);
5677 NBUMPSIDED(0, ns_divert_dup);
5681 ip = MTOD(m, ip_t *);
5682 ip->ip_id = htons(ipf_nextipid(fin));
5683 sum1 = ntohs(ip->ip_len);
5684 ip->ip_len = ntohs(ip->ip_len);
5685 ip->ip_len += fin->fin_plen;
5686 ip->ip_len = htons(ip->ip_len);
5688 uh = (udphdr_t *)(ip + 1);
5689 uh->uh_ulen += fin->fin_plen;
5690 uh->uh_ulen = htons(uh->uh_ulen);
5692 sum2 = ntohs(ip->ip_id) + ntohs(ip->ip_len);
5693 sum2 += ntohs(ip->ip_off) & IP_DF;
5694 CALC_SUMD(sum1, sum2, sumd);
5696 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5697 defined(__osf__) || defined(linux)
5698 ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0);
5703 fin->fin_plen += sizeof(ip_t) + 8; /* UDP + new IPv4 hdr */
5704 fin->fin_dlen += sizeof(ip_t) + 8; /* UDP + old IPv4 hdr */
5706 nflags &= ~IPN_TCPUDPICMP;
5711 case NAT_DIVERTOUT :
5715 skip = ipf_nat_decap(fin, nat);
5717 NBUMPSIDED(0, ns_decap_fail);
5723 #if defined(MENTAT) && defined(_KERNEL)
5730 if (m->m_flags & M_PKTHDR)
5731 m->m_pkthdr.len -= skip;
5735 ipf_nat_update(fin, nat);
5736 nflags &= ~IPN_TCPUDPICMP;
5737 fin->fin_flx |= FI_NATED;
5738 if (np != NULL && np->in_tag.ipt_num[0] != 0)
5739 fin->fin_nattag = &np->in_tag;
5744 if (nflags & IPN_TCPUDP)
5747 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5750 if ((nat->nat_odport != 0) && (nflags & IPN_TCPUDP)) {
5751 switch (nat->nat_dir)
5754 tcp->th_sport = nat->nat_nsport;
5755 fin->fin_data[0] = ntohs(nat->nat_nsport);
5756 tcp->th_dport = nat->nat_ndport;
5757 fin->fin_data[1] = ntohs(nat->nat_ndport);
5761 tcp->th_sport = nat->nat_odport;
5762 fin->fin_data[0] = ntohs(nat->nat_odport);
5763 tcp->th_dport = nat->nat_osport;
5764 fin->fin_data[1] = ntohs(nat->nat_osport);
5770 if ((nat->nat_odport != 0) && (nflags & IPN_ICMPQUERY)) {
5773 icmp->icmp_id = nat->nat_nicmpid;
5776 csump = ipf_nat_proto(fin, nat, nflags);
5779 * The above comments do not hold for layer 4 (or higher)
5782 if (csump != NULL) {
5783 if (nat->nat_dir == NAT_OUTBOUND)
5784 ipf_fix_incksum(0, csump, nat->nat_sumd[0], 0);
5786 ipf_fix_outcksum(0, csump, nat->nat_sumd[0], 0);
5790 fin->fin_flx |= FI_NATED;
5791 if (np != NULL && np->in_tag.ipt_num[0] != 0)
5792 fin->fin_nattag = &np->in_tag;
5797 /* ------------------------------------------------------------------------ */
5798 /* Function: ipf_nat_proto */
5799 /* Returns: u_short* - pointer to transport header checksum to update, */
5800 /* NULL if the transport protocol is not recognised */
5801 /* as needing a checksum update. */
5802 /* Parameters: fin(I) - pointer to packet information */
5803 /* nat(I) - pointer to NAT structure */
5804 /* nflags(I) - NAT flags set for this packet */
5806 /* Return the pointer to the checksum field for each protocol so understood.*/
5807 /* If support for making other changes to a protocol header is required, */
5808 /* that is not strictly 'address' translation, such as clamping the MSS in */
5809 /* TCP down to a specific value, then do it from here. */
5810 /* ------------------------------------------------------------------------ */
5812 ipf_nat_proto(fin, nat, nflags)
5823 if (fin->fin_out == 0) {
5824 fin->fin_rev = (nat->nat_dir & NAT_OUTBOUND);
5826 fin->fin_rev = ((nat->nat_dir & NAT_OUTBOUND) == 0);
5834 if ((nflags & IPN_TCP) != 0)
5835 csump = &tcp->th_sum;
5838 * Do a MSS CLAMPING on a SYN packet,
5839 * only deal IPv4 for now.
5841 if ((nat->nat_mssclamp != 0) && (tcp->th_flags & TH_SYN) != 0)
5842 ipf_nat_mssclamp(tcp, nat->nat_mssclamp, fin, csump);
5849 if ((nflags & IPN_UDP) != 0) {
5850 if (udp->uh_sum != 0)
5851 csump = &udp->uh_sum;
5858 if ((nflags & IPN_ICMPQUERY) != 0) {
5859 if (icmp->icmp_cksum != 0)
5860 csump = &icmp->icmp_cksum;
5865 case IPPROTO_ICMPV6 :
5867 struct icmp6_hdr *icmp6 = (struct icmp6_hdr *)fin->fin_dp;
5869 icmp6 = fin->fin_dp;
5871 if ((nflags & IPN_ICMPQUERY) != 0) {
5872 if (icmp6->icmp6_cksum != 0)
5873 csump = &icmp6->icmp6_cksum;
5883 /* ------------------------------------------------------------------------ */
5884 /* Function: ipf_nat_expire */
5886 /* Parameters: softc(I) - pointer to soft context main structure */
5888 /* Check all of the timeout queues for entries at the top which need to be */
5890 /* ------------------------------------------------------------------------ */
5892 ipf_nat_expire(softc)
5893 ipf_main_softc_t *softc;
5895 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5896 ipftq_t *ifq, *ifqnext;
5897 ipftqent_t *tqe, *tqn;
5902 WRITE_ENTER(&softc->ipf_nat);
5903 for (ifq = softn->ipf_nat_tcptq, i = 0; ifq != NULL;
5904 ifq = ifq->ifq_next) {
5905 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
5906 if (tqe->tqe_die > softc->ipf_ticks)
5908 tqn = tqe->tqe_next;
5909 ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
5913 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifq->ifq_next) {
5914 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
5915 if (tqe->tqe_die > softc->ipf_ticks)
5917 tqn = tqe->tqe_next;
5918 ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
5922 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
5923 ifqnext = ifq->ifq_next;
5925 if (((ifq->ifq_flags & IFQF_DELETE) != 0) &&
5926 (ifq->ifq_ref == 0)) {
5927 ipf_freetimeoutqueue(softc, ifq);
5931 if (softn->ipf_nat_doflush != 0) {
5932 ipf_nat_extraflush(softc, softn, 2);
5933 softn->ipf_nat_doflush = 0;
5936 RWLOCK_EXIT(&softc->ipf_nat);
5941 /* ------------------------------------------------------------------------ */
5942 /* Function: ipf_nat_sync */
5944 /* Parameters: softc(I) - pointer to soft context main structure */
5945 /* ifp(I) - pointer to network interface */
5947 /* Walk through all of the currently active NAT sessions, looking for those */
5948 /* which need to have their translated address updated. */
5949 /* ------------------------------------------------------------------------ */
5951 ipf_nat_sync(softc, ifp)
5952 ipf_main_softc_t *softc;
5955 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5956 u_32_t sum1, sum2, sumd;
5964 if (softc->ipf_running <= 0)
5968 * Change IP addresses for NAT sessions for any protocol except TCP
5969 * since it will break the TCP connection anyway. The only rules
5970 * which will get changed are those which are "map ... -> 0/32",
5971 * where the rule specifies the address is taken from the interface.
5974 WRITE_ENTER(&softc->ipf_nat);
5976 if (softc->ipf_running <= 0) {
5977 RWLOCK_EXIT(&softc->ipf_nat);
5981 for (nat = softn->ipf_nat_instances; nat; nat = nat->nat_next) {
5982 if ((nat->nat_flags & IPN_TCP) != 0)
5987 if (n->in_v[1] == 4) {
5988 if (n->in_redir & NAT_MAP) {
5989 if ((n->in_nsrcaddr != 0) ||
5990 (n->in_nsrcmsk != 0xffffffff))
5992 } else if (n->in_redir & NAT_REDIRECT) {
5993 if ((n->in_ndstaddr != 0) ||
5994 (n->in_ndstmsk != 0xffffffff))
5999 if (n->in_v[1] == 4) {
6000 if (n->in_redir & NAT_MAP) {
6001 if (!IP6_ISZERO(&n->in_nsrcaddr) ||
6002 !IP6_ISONES(&n->in_nsrcmsk))
6004 } else if (n->in_redir & NAT_REDIRECT) {
6005 if (!IP6_ISZERO(&n->in_ndstaddr) ||
6006 !IP6_ISONES(&n->in_ndstmsk))
6013 if (((ifp == NULL) || (ifp == nat->nat_ifps[0]) ||
6014 (ifp == nat->nat_ifps[1]))) {
6015 nat->nat_ifps[0] = GETIFP(nat->nat_ifnames[0],
6017 if ((nat->nat_ifps[0] != NULL) &&
6018 (nat->nat_ifps[0] != (void *)-1)) {
6019 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
6021 if (nat->nat_ifnames[1][0] != '\0') {
6022 nat->nat_ifps[1] = GETIFP(nat->nat_ifnames[1],
6025 nat->nat_ifps[1] = nat->nat_ifps[0];
6027 if ((nat->nat_ifps[1] != NULL) &&
6028 (nat->nat_ifps[1] != (void *)-1)) {
6029 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
6031 ifp2 = nat->nat_ifps[0];
6036 * Change the map-to address to be the same as the
6039 sum1 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
6040 if (ipf_ifpaddr(softc, nat->nat_v[0], FRI_NORMAL, ifp2,
6042 if (nat->nat_v[0] == 4)
6043 nat->nat_nsrcip = in.in4;
6045 sum2 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
6050 * Readjust the checksum adjustment to take into
6051 * account the new IP#.
6053 CALC_SUMD(sum1, sum2, sumd);
6054 /* XXX - dont change for TCP when solaris does
6055 * hardware checksumming.
6057 sumd += nat->nat_sumd[0];
6058 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
6059 nat->nat_sumd[1] = nat->nat_sumd[0];
6063 for (n = softn->ipf_nat_list; (n != NULL); n = n->in_next) {
6064 char *base = n->in_names;
6066 if ((ifp == NULL) || (n->in_ifps[0] == ifp))
6067 n->in_ifps[0] = ipf_resolvenic(softc,
6068 base + n->in_ifnames[0],
6070 if ((ifp == NULL) || (n->in_ifps[1] == ifp))
6071 n->in_ifps[1] = ipf_resolvenic(softc,
6072 base + n->in_ifnames[1],
6075 if (n->in_redir & NAT_REDIRECT)
6080 if (((ifp == NULL) || (n->in_ifps[idx] == ifp)) &&
6081 (n->in_ifps[idx] != NULL &&
6082 n->in_ifps[idx] != (void *)-1)) {
6084 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc,
6085 0, n->in_ifps[idx]);
6086 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst,
6087 0, n->in_ifps[idx]);
6088 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc,
6089 0, n->in_ifps[idx]);
6090 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst,
6091 0, n->in_ifps[idx]);
6094 RWLOCK_EXIT(&softc->ipf_nat);
6099 /* ------------------------------------------------------------------------ */
6100 /* Function: ipf_nat_icmpquerytype */
6101 /* Returns: int - 1 == success, 0 == failure */
6102 /* Parameters: icmptype(I) - ICMP type number */
6104 /* Tests to see if the ICMP type number passed is a query/response type or */
6106 /* ------------------------------------------------------------------------ */
6108 ipf_nat_icmpquerytype(icmptype)
6113 * For the ICMP query NAT code, it is essential that both the query
6114 * and the reply match on the NAT rule. Because the NAT structure
6115 * does not keep track of the icmptype, and a single NAT structure
6116 * is used for all icmp types with the same src, dest and id, we
6117 * simply define the replies as queries as well. The funny thing is,
6118 * altough it seems silly to call a reply a query, this is exactly
6119 * as it is defined in the IPv4 specification
6123 case ICMP_ECHOREPLY:
6125 /* route advertisement/solicitation is currently unsupported: */
6126 /* it would require rewriting the ICMP data section */
6128 case ICMP_TSTAMPREPLY:
6130 case ICMP_IREQREPLY:
6132 case ICMP_MASKREPLY:
6140 /* ------------------------------------------------------------------------ */
6141 /* Function: nat_log */
6143 /* Parameters: softc(I) - pointer to soft context main structure */
6144 /* softn(I) - pointer to NAT context structure */
6145 /* nat(I) - pointer to NAT structure */
6146 /* action(I) - action related to NAT structure being performed */
6148 /* Creates a NAT log entry. */
6149 /* ------------------------------------------------------------------------ */
6151 ipf_nat_log(softc, softn, nat, action)
6152 ipf_main_softc_t *softc;
6153 ipf_nat_softc_t *softn;
6167 bcopy((char *)&nat->nat_osrc6, (char *)&natl.nl_osrcip,
6168 sizeof(natl.nl_osrcip));
6169 bcopy((char *)&nat->nat_nsrc6, (char *)&natl.nl_nsrcip,
6170 sizeof(natl.nl_nsrcip));
6171 bcopy((char *)&nat->nat_odst6, (char *)&natl.nl_odstip,
6172 sizeof(natl.nl_odstip));
6173 bcopy((char *)&nat->nat_ndst6, (char *)&natl.nl_ndstip,
6174 sizeof(natl.nl_ndstip));
6176 natl.nl_bytes[0] = nat->nat_bytes[0];
6177 natl.nl_bytes[1] = nat->nat_bytes[1];
6178 natl.nl_pkts[0] = nat->nat_pkts[0];
6179 natl.nl_pkts[1] = nat->nat_pkts[1];
6180 natl.nl_odstport = nat->nat_odport;
6181 natl.nl_osrcport = nat->nat_osport;
6182 natl.nl_nsrcport = nat->nat_nsport;
6183 natl.nl_ndstport = nat->nat_ndport;
6184 natl.nl_p[0] = nat->nat_pr[0];
6185 natl.nl_p[1] = nat->nat_pr[1];
6186 natl.nl_v[0] = nat->nat_v[0];
6187 natl.nl_v[1] = nat->nat_v[1];
6188 natl.nl_type = nat->nat_redir;
6189 natl.nl_action = action;
6192 bcopy(nat->nat_ifnames[0], natl.nl_ifnames[0],
6193 sizeof(nat->nat_ifnames[0]));
6194 bcopy(nat->nat_ifnames[1], natl.nl_ifnames[1],
6195 sizeof(nat->nat_ifnames[1]));
6198 if (nat->nat_ptr != NULL) {
6199 for (rulen = 0, np = softn->ipf_nat_list; np != NULL;
6200 np = np->in_next, rulen++)
6201 if (np == nat->nat_ptr) {
6202 natl.nl_rule = rulen;
6208 sizes[0] = sizeof(natl);
6211 (void) ipf_log_items(softc, IPL_LOGNAT, NULL, items, sizes, types, 1);
6216 #if defined(__OpenBSD__)
6217 /* ------------------------------------------------------------------------ */
6218 /* Function: ipf_nat_ifdetach */
6220 /* Parameters: ifp(I) - pointer to network interface */
6222 /* Compatibility interface for OpenBSD to trigger the correct updating of */
6223 /* interface references within IPFilter. */
6224 /* ------------------------------------------------------------------------ */
6226 ipf_nat_ifdetach(ifp)
6229 ipf_main_softc_t *softc;
6231 softc = ipf_get_softc(0);
6239 /* ------------------------------------------------------------------------ */
6240 /* Function: ipf_nat_rule_deref */
6242 /* Parameters: softc(I) - pointer to soft context main structure */
6243 /* inp(I) - pointer to pointer to NAT rule */
6244 /* Write Locks: ipf_nat */
6246 /* Dropping the refernce count for a rule means that whatever held the */
6247 /* pointer to this rule (*inp) is no longer interested in it and when the */
6248 /* reference count drops to zero, any resources allocated for the rule can */
6249 /* be released and the rule itself free'd. */
6250 /* ------------------------------------------------------------------------ */
6252 ipf_nat_rule_deref(softc, inp)
6253 ipf_main_softc_t *softc;
6256 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6265 if (n->in_apr != NULL)
6266 ipf_proxy_deref(n->in_apr);
6268 ipf_nat_rule_fini(softc, n);
6270 if (n->in_redir & NAT_REDIRECT) {
6271 if ((n->in_flags & IPN_PROXYRULE) == 0) {
6272 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_rdr);
6275 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
6276 if ((n->in_flags & IPN_PROXYRULE) == 0) {
6277 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_map);
6281 if (n->in_tqehead[0] != NULL) {
6282 if (ipf_deletetimeoutqueue(n->in_tqehead[0]) == 0) {
6283 ipf_freetimeoutqueue(softc, n->in_tqehead[1]);
6287 if (n->in_tqehead[1] != NULL) {
6288 if (ipf_deletetimeoutqueue(n->in_tqehead[1]) == 0) {
6289 ipf_freetimeoutqueue(softc, n->in_tqehead[1]);
6293 if ((n->in_flags & IPN_PROXYRULE) == 0) {
6294 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules);
6297 MUTEX_DESTROY(&n->in_lock);
6299 KFREES(n, n->in_size);
6301 #if SOLARIS && !defined(INSTANCES)
6302 if (softn->ipf_nat_stats.ns_rules == 0)
6303 pfil_delayed_copy = 1;
6308 /* ------------------------------------------------------------------------ */
6309 /* Function: ipf_nat_deref */
6311 /* Parameters: softc(I) - pointer to soft context main structure */
6312 /* natp(I) - pointer to pointer to NAT table entry */
6314 /* Decrement the reference counter for this NAT table entry and free it if */
6315 /* there are no more things using it. */
6317 /* IF nat_ref == 1 when this function is called, then we have an orphan nat */
6318 /* structure *because* it only gets called on paths _after_ nat_ref has been*/
6319 /* incremented. If nat_ref == 1 then we shouldn't decrement it here */
6320 /* because nat_delete() will do that and send nat_ref to -1. */
6322 /* Holding the lock on nat_lock is required to serialise nat_delete() being */
6323 /* called from a NAT flush ioctl with a deref happening because of a packet.*/
6324 /* ------------------------------------------------------------------------ */
6326 ipf_nat_deref(softc, natp)
6327 ipf_main_softc_t *softc;
6335 MUTEX_ENTER(&nat->nat_lock);
6336 if (nat->nat_ref > 1) {
6338 ASSERT(nat->nat_ref >= 0);
6339 MUTEX_EXIT(&nat->nat_lock);
6342 MUTEX_EXIT(&nat->nat_lock);
6344 WRITE_ENTER(&softc->ipf_nat);
6345 ipf_nat_delete(softc, nat, NL_EXPIRE);
6346 RWLOCK_EXIT(&softc->ipf_nat);
6350 /* ------------------------------------------------------------------------ */
6351 /* Function: ipf_nat_clone */
6352 /* Returns: ipstate_t* - NULL == cloning failed, */
6353 /* else pointer to new state structure */
6354 /* Parameters: fin(I) - pointer to packet information */
6355 /* is(I) - pointer to master state structure */
6356 /* Write Lock: ipf_nat */
6358 /* Create a "duplcate" state table entry from the master. */
6359 /* ------------------------------------------------------------------------ */
6361 ipf_nat_clone(fin, nat)
6365 ipf_main_softc_t *softc = fin->fin_main_soft;
6366 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6371 KMALLOC(clone, nat_t *);
6372 if (clone == NULL) {
6373 NBUMPSIDED(fin->fin_out, ns_clone_nomem);
6376 bcopy((char *)nat, (char *)clone, sizeof(*clone));
6378 MUTEX_NUKE(&clone->nat_lock);
6380 clone->nat_rev = fin->fin_rev;
6381 clone->nat_aps = NULL;
6383 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
6385 clone->nat_tqe.tqe_pnext = NULL;
6386 clone->nat_tqe.tqe_next = NULL;
6387 clone->nat_tqe.tqe_ifq = NULL;
6388 clone->nat_tqe.tqe_parent = clone;
6390 clone->nat_flags &= ~SI_CLONE;
6391 clone->nat_flags |= SI_CLONED;
6394 clone->nat_hm->hm_ref++;
6396 if (ipf_nat_insert(softc, softn, clone) == -1) {
6398 NBUMPSIDED(fin->fin_out, ns_insert_fail);
6402 np = clone->nat_ptr;
6404 if (softn->ipf_nat_logging)
6405 ipf_nat_log(softc, softn, clone, NL_CLONE);
6410 MUTEX_ENTER(&fr->fr_lock);
6412 MUTEX_EXIT(&fr->fr_lock);
6417 * Because the clone is created outside the normal loop of things and
6418 * TCP has special needs in terms of state, initialise the timeout
6419 * state of the new NAT from here.
6421 if (clone->nat_pr[0] == IPPROTO_TCP) {
6422 (void) ipf_tcp_age(&clone->nat_tqe, fin, softn->ipf_nat_tcptq,
6423 clone->nat_flags, 2);
6425 clone->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, clone);
6426 if (softn->ipf_nat_logging)
6427 ipf_nat_log(softc, softn, clone, NL_CLONE);
6432 /* ------------------------------------------------------------------------ */
6433 /* Function: ipf_nat_wildok */
6434 /* Returns: int - 1 == packet's ports match wildcards */
6435 /* 0 == packet's ports don't match wildcards */
6436 /* Parameters: nat(I) - NAT entry */
6437 /* sport(I) - source port */
6438 /* dport(I) - destination port */
6439 /* flags(I) - wildcard flags */
6440 /* dir(I) - packet direction */
6442 /* Use NAT entry and packet direction to determine which combination of */
6443 /* wildcard flags should be used. */
6444 /* ------------------------------------------------------------------------ */
6446 ipf_nat_wildok(nat, sport, dport, flags, dir)
6448 int sport, dport, flags, dir;
6451 * When called by dir is set to
6452 * nat_inlookup NAT_INBOUND (0)
6453 * nat_outlookup NAT_OUTBOUND (1)
6455 * We simply combine the packet's direction in dir with the original
6456 * "intended" direction of that NAT entry in nat->nat_dir to decide
6457 * which combination of wildcard flags to allow.
6459 switch ((dir << 1) | (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND)))
6461 case 3: /* outbound packet / outbound entry */
6462 if (((nat->nat_osport == sport) ||
6463 (flags & SI_W_SPORT)) &&
6464 ((nat->nat_odport == dport) ||
6465 (flags & SI_W_DPORT)))
6468 case 2: /* outbound packet / inbound entry */
6469 if (((nat->nat_osport == dport) ||
6470 (flags & SI_W_SPORT)) &&
6471 ((nat->nat_odport == sport) ||
6472 (flags & SI_W_DPORT)))
6475 case 1: /* inbound packet / outbound entry */
6476 if (((nat->nat_osport == dport) ||
6477 (flags & SI_W_SPORT)) &&
6478 ((nat->nat_odport == sport) ||
6479 (flags & SI_W_DPORT)))
6482 case 0: /* inbound packet / inbound entry */
6483 if (((nat->nat_osport == sport) ||
6484 (flags & SI_W_SPORT)) &&
6485 ((nat->nat_odport == dport) ||
6486 (flags & SI_W_DPORT)))
6497 /* ------------------------------------------------------------------------ */
6498 /* Function: nat_mssclamp */
6500 /* Parameters: tcp(I) - pointer to TCP header */
6501 /* maxmss(I) - value to clamp the TCP MSS to */
6502 /* fin(I) - pointer to packet information */
6503 /* csump(I) - pointer to TCP checksum */
6505 /* Check for MSS option and clamp it if necessary. If found and changed, */
6506 /* then the TCP header checksum will be updated to reflect the change in */
6508 /* ------------------------------------------------------------------------ */
6510 ipf_nat_mssclamp(tcp, maxmss, fin, csump)
6516 u_char *cp, *ep, opt;
6520 hlen = TCP_OFF(tcp) << 2;
6521 if (hlen > sizeof(*tcp)) {
6522 cp = (u_char *)tcp + sizeof(*tcp);
6523 ep = (u_char *)tcp + hlen;
6527 if (opt == TCPOPT_EOL)
6529 else if (opt == TCPOPT_NOP) {
6537 if ((cp + advance > ep) || (advance <= 0))
6544 mss = cp[2] * 256 + cp[3];
6546 cp[2] = maxmss / 256;
6547 cp[3] = maxmss & 0xff;
6548 CALC_SUMD(mss, maxmss, sumd);
6549 ipf_fix_outcksum(0, csump, sumd, 0);
6553 /* ignore unknown options */
6563 /* ------------------------------------------------------------------------ */
6564 /* Function: ipf_nat_setqueue */
6566 /* Parameters: softc(I) - pointer to soft context main structure */
6567 /* softn(I) - pointer to NAT context structure */
6568 /* nat(I)- pointer to NAT structure */
6569 /* Locks: ipf_nat (read or write) */
6571 /* Put the NAT entry on its default queue entry, using rev as a helped in */
6572 /* determining which queue it should be placed on. */
6573 /* ------------------------------------------------------------------------ */
6575 ipf_nat_setqueue(softc, softn, nat)
6576 ipf_main_softc_t *softc;
6577 ipf_nat_softc_t *softn;
6580 ipftq_t *oifq, *nifq;
6581 int rev = nat->nat_rev;
6583 if (nat->nat_ptr != NULL)
6584 nifq = nat->nat_ptr->in_tqehead[rev];
6589 switch (nat->nat_pr[0])
6592 nifq = &softn->ipf_nat_udptq;
6595 nifq = &softn->ipf_nat_icmptq;
6598 nifq = softn->ipf_nat_tcptq +
6599 nat->nat_tqe.tqe_state[rev];
6602 nifq = &softn->ipf_nat_iptq;
6607 oifq = nat->nat_tqe.tqe_ifq;
6609 * If it's currently on a timeout queue, move it from one queue to
6610 * another, else put it on the end of the newly determined queue.
6613 ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq, nifq);
6615 ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe, nifq, nat);
6620 /* ------------------------------------------------------------------------ */
6621 /* Function: nat_getnext */
6622 /* Returns: int - 0 == ok, else error */
6623 /* Parameters: softc(I) - pointer to soft context main structure */
6624 /* t(I) - pointer to ipftoken structure */
6625 /* itp(I) - pointer to ipfgeniter_t structure */
6627 /* Fetch the next nat/ipnat structure pointer from the linked list and */
6628 /* copy it out to the storage space pointed to by itp_data. The next item */
6629 /* in the list to look at is put back in the ipftoken struture. */
6630 /* ------------------------------------------------------------------------ */
6632 ipf_nat_getnext(softc, t, itp, objp)
6633 ipf_main_softc_t *softc;
6638 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6639 hostmap_t *hm, *nexthm = NULL, zerohm;
6640 ipnat_t *ipn, *nextipnat = NULL, zeroipn;
6641 nat_t *nat, *nextnat = NULL, zeronat;
6645 if (itp->igi_nitems != 1) {
6650 READ_ENTER(&softc->ipf_nat);
6652 switch (itp->igi_type)
6654 case IPFGENITER_HOSTMAP :
6657 nexthm = softn->ipf_hm_maplist;
6659 nexthm = hm->hm_next;
6661 if (nexthm != NULL) {
6662 ATOMIC_INC32(nexthm->hm_ref);
6663 t->ipt_data = nexthm;
6665 bzero(&zerohm, sizeof(zerohm));
6669 nnext = nexthm->hm_next;
6672 case IPFGENITER_IPNAT :
6675 nextipnat = softn->ipf_nat_list;
6677 nextipnat = ipn->in_next;
6679 if (nextipnat != NULL) {
6680 ATOMIC_INC32(nextipnat->in_use);
6681 t->ipt_data = nextipnat;
6683 bzero(&zeroipn, sizeof(zeroipn));
6684 nextipnat = &zeroipn;
6687 nnext = nextipnat->in_next;
6690 case IPFGENITER_NAT :
6693 nextnat = softn->ipf_nat_instances;
6695 nextnat = nat->nat_next;
6697 if (nextnat != NULL) {
6698 MUTEX_ENTER(&nextnat->nat_lock);
6700 MUTEX_EXIT(&nextnat->nat_lock);
6701 t->ipt_data = nextnat;
6703 bzero(&zeronat, sizeof(zeronat));
6707 nnext = nextnat->nat_next;
6711 RWLOCK_EXIT(&softc->ipf_nat);
6716 RWLOCK_EXIT(&softc->ipf_nat);
6718 objp->ipfo_ptr = itp->igi_data;
6720 switch (itp->igi_type)
6722 case IPFGENITER_HOSTMAP :
6723 error = COPYOUT(nexthm, objp->ipfo_ptr, sizeof(*nexthm));
6729 WRITE_ENTER(&softc->ipf_nat);
6730 ipf_nat_hostmapdel(softc, &hm);
6731 RWLOCK_EXIT(&softc->ipf_nat);
6735 case IPFGENITER_IPNAT :
6736 objp->ipfo_size = nextipnat->in_size;
6737 objp->ipfo_type = IPFOBJ_IPNAT;
6738 error = ipf_outobjk(softc, objp, nextipnat);
6740 WRITE_ENTER(&softc->ipf_nat);
6741 ipf_nat_rule_deref(softc, &ipn);
6742 RWLOCK_EXIT(&softc->ipf_nat);
6746 case IPFGENITER_NAT :
6747 objp->ipfo_size = sizeof(nat_t);
6748 objp->ipfo_type = IPFOBJ_NAT;
6749 error = ipf_outobjk(softc, objp, nextnat);
6751 ipf_nat_deref(softc, &nat);
6757 ipf_token_mark_complete(t);
6763 /* ------------------------------------------------------------------------ */
6764 /* Function: nat_extraflush */
6765 /* Returns: int - 0 == success, -1 == failure */
6766 /* Parameters: softc(I) - pointer to soft context main structure */
6767 /* softn(I) - pointer to NAT context structure */
6768 /* which(I) - how to flush the active NAT table */
6769 /* Write Locks: ipf_nat */
6771 /* Flush nat tables. Three actions currently defined: */
6772 /* which == 0 : flush all nat table entries */
6773 /* which == 1 : flush TCP connections which have started to close but are */
6774 /* stuck for some reason. */
6775 /* which == 2 : flush TCP connections which have been idle for a long time, */
6776 /* starting at > 4 days idle and working back in successive half-*/
6777 /* days to at most 12 hours old. If this fails to free enough */
6778 /* slots then work backwards in half hour slots to 30 minutes. */
6779 /* If that too fails, then work backwards in 30 second intervals */
6780 /* for the last 30 minutes to at worst 30 seconds idle. */
6781 /* ------------------------------------------------------------------------ */
6783 ipf_nat_extraflush(softc, softn, which)
6784 ipf_main_softc_t *softc;
6785 ipf_nat_softc_t *softn;
6800 softn->ipf_nat_stats.ns_flush_all++;
6802 * Style 0 flush removes everything...
6804 for (natp = &softn->ipf_nat_instances;
6805 ((nat = *natp) != NULL); ) {
6806 ipf_nat_delete(softc, nat, NL_FLUSH);
6812 softn->ipf_nat_stats.ns_flush_closing++;
6814 * Since we're only interested in things that are closing,
6815 * we can start with the appropriate timeout queue.
6817 for (ifq = softn->ipf_nat_tcptq + IPF_TCPS_CLOSE_WAIT;
6818 ifq != NULL; ifq = ifq->ifq_next) {
6820 for (tqn = ifq->ifq_head; tqn != NULL; ) {
6821 nat = tqn->tqe_parent;
6822 tqn = tqn->tqe_next;
6823 if (nat->nat_pr[0] != IPPROTO_TCP ||
6824 nat->nat_pr[1] != IPPROTO_TCP)
6826 ipf_nat_delete(softc, nat, NL_EXPIRE);
6832 * Also need to look through the user defined queues.
6834 for (ifq = softn->ipf_nat_utqe; ifq != NULL;
6835 ifq = ifq->ifq_next) {
6836 for (tqn = ifq->ifq_head; tqn != NULL; ) {
6837 nat = tqn->tqe_parent;
6838 tqn = tqn->tqe_next;
6839 if (nat->nat_pr[0] != IPPROTO_TCP ||
6840 nat->nat_pr[1] != IPPROTO_TCP)
6843 if ((nat->nat_tcpstate[0] >
6844 IPF_TCPS_ESTABLISHED) &&
6845 (nat->nat_tcpstate[1] >
6846 IPF_TCPS_ESTABLISHED)) {
6847 ipf_nat_delete(softc, nat, NL_EXPIRE);
6855 * Args 5-11 correspond to flushing those particular states
6856 * for TCP connections.
6858 case IPF_TCPS_CLOSE_WAIT :
6859 case IPF_TCPS_FIN_WAIT_1 :
6860 case IPF_TCPS_CLOSING :
6861 case IPF_TCPS_LAST_ACK :
6862 case IPF_TCPS_FIN_WAIT_2 :
6863 case IPF_TCPS_TIME_WAIT :
6864 case IPF_TCPS_CLOSED :
6865 softn->ipf_nat_stats.ns_flush_state++;
6866 tqn = softn->ipf_nat_tcptq[which].ifq_head;
6867 while (tqn != NULL) {
6868 nat = tqn->tqe_parent;
6869 tqn = tqn->tqe_next;
6870 ipf_nat_delete(softc, nat, NL_FLUSH);
6879 softn->ipf_nat_stats.ns_flush_timeout++;
6881 * Take a large arbitrary number to mean the number of seconds
6882 * for which which consider to be the maximum value we'll allow
6883 * the expiration to be.
6885 which = IPF_TTLVAL(which);
6886 for (natp = &softn->ipf_nat_instances;
6887 ((nat = *natp) != NULL); ) {
6888 if (softc->ipf_ticks - nat->nat_touched > which) {
6889 ipf_nat_delete(softc, nat, NL_FLUSH);
6892 natp = &nat->nat_next;
6902 softn->ipf_nat_stats.ns_flush_queue++;
6905 * Asked to remove inactive entries because the table is full, try
6906 * again, 3 times, if first attempt failed with a different criteria
6907 * each time. The order tried in must be in decreasing age.
6908 * Another alternative is to implement random drop and drop N entries
6909 * at random until N have been freed up.
6911 if (softc->ipf_ticks - softn->ipf_nat_last_force_flush >
6913 softn->ipf_nat_last_force_flush = softc->ipf_ticks;
6915 removed = ipf_queueflush(softc, ipf_nat_flush_entry,
6916 softn->ipf_nat_tcptq,
6917 softn->ipf_nat_utqe,
6918 &softn->ipf_nat_stats.ns_active,
6919 softn->ipf_nat_table_sz,
6920 softn->ipf_nat_table_wm_low);
6928 /* ------------------------------------------------------------------------ */
6929 /* Function: ipf_nat_flush_entry */
6930 /* Returns: 0 - always succeeds */
6931 /* Parameters: softc(I) - pointer to soft context main structure */
6932 /* entry(I) - pointer to NAT entry */
6933 /* Write Locks: ipf_nat */
6935 /* This function is a stepping stone between ipf_queueflush() and */
6936 /* nat_dlete(). It is used so we can provide a uniform interface via the */
6937 /* ipf_queueflush() function. Since the nat_delete() function returns void */
6938 /* we translate that to mean it always succeeds in deleting something. */
6939 /* ------------------------------------------------------------------------ */
6941 ipf_nat_flush_entry(softc, entry)
6942 ipf_main_softc_t *softc;
6945 ipf_nat_delete(softc, entry, NL_FLUSH);
6950 /* ------------------------------------------------------------------------ */
6951 /* Function: ipf_nat_iterator */
6952 /* Returns: int - 0 == ok, else error */
6953 /* Parameters: softc(I) - pointer to soft context main structure */
6954 /* token(I) - pointer to ipftoken structure */
6955 /* itp(I) - pointer to ipfgeniter_t structure */
6956 /* obj(I) - pointer to data description structure */
6958 /* This function acts as a handler for the SIOCGENITER ioctls that use a */
6959 /* generic structure to iterate through a list. There are three different */
6960 /* linked lists of NAT related information to go through: NAT rules, active */
6961 /* NAT mappings and the NAT fragment cache. */
6962 /* ------------------------------------------------------------------------ */
6964 ipf_nat_iterator(softc, token, itp, obj)
6965 ipf_main_softc_t *softc;
6972 if (itp->igi_data == NULL) {
6977 switch (itp->igi_type)
6979 case IPFGENITER_HOSTMAP :
6980 case IPFGENITER_IPNAT :
6981 case IPFGENITER_NAT :
6982 error = ipf_nat_getnext(softc, token, itp, obj);
6985 case IPFGENITER_NATFRAG :
6986 error = ipf_frag_nat_next(softc, token, itp);
6998 /* ------------------------------------------------------------------------ */
6999 /* Function: ipf_nat_setpending */
7001 /* Parameters: softc(I) - pointer to soft context main structure */
7002 /* nat(I) - pointer to NAT structure */
7003 /* Locks: ipf_nat (read or write) */
7005 /* Put the NAT entry on to the pending queue - this queue has a very short */
7006 /* lifetime where items are put that can't be deleted straight away because */
7007 /* of locking issues but we want to delete them ASAP, anyway. In calling */
7008 /* this function, it is assumed that the owner (if there is one, as shown */
7009 /* by nat_me) is no longer interested in it. */
7010 /* ------------------------------------------------------------------------ */
7012 ipf_nat_setpending(softc, nat)
7013 ipf_main_softc_t *softc;
7016 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7019 oifq = nat->nat_tqe.tqe_ifq;
7021 ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq,
7022 &softn->ipf_nat_pending);
7024 ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe,
7025 &softn->ipf_nat_pending, nat);
7027 if (nat->nat_me != NULL) {
7028 *nat->nat_me = NULL;
7031 ASSERT(nat->nat_ref >= 0);
7036 /* ------------------------------------------------------------------------ */
7037 /* Function: nat_newrewrite */
7038 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */
7039 /* allow rule to be moved if IPN_ROUNDR is set. */
7040 /* Parameters: fin(I) - pointer to packet information */
7041 /* nat(I) - pointer to NAT entry */
7042 /* ni(I) - pointer to structure with misc. information needed */
7043 /* to create new NAT entry. */
7044 /* Write Lock: ipf_nat */
7046 /* This function is responsible for setting up an active NAT session where */
7047 /* we are changing both the source and destination parameters at the same */
7048 /* time. The loop in here works differently to elsewhere - each iteration */
7049 /* is responsible for changing a single parameter that can be incremented. */
7050 /* So one pass may increase the source IP#, next source port, next dest. IP#*/
7051 /* and the last destination port for a total of 4 iterations to try each. */
7052 /* This is done to try and exhaustively use the translation space available.*/
7053 /* ------------------------------------------------------------------------ */
7055 ipf_nat_newrewrite(fin, nat, nai)
7073 flags = nat->nat_flags;
7074 bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
7080 /* TRACE (l, src_search, dst_search, np) */
7081 DT4(ipf_nat_rewrite_1, int, l, int, src_search, int, dst_search, ipnat_t *, np);
7083 if ((src_search == 0) && (np->in_spnext == 0) &&
7084 (dst_search == 0) && (np->in_dpnext == 0)) {
7090 * Find a new source address
7092 if (ipf_nat_nextaddr(fin, &np->in_nsrc, &frnat.fin_saddr,
7093 &frnat.fin_saddr) == -1) {
7097 if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0xffffffff)) {
7099 if (np->in_stepnext == 0)
7100 np->in_stepnext = 1;
7102 } else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
7104 if (np->in_stepnext == 0)
7105 np->in_stepnext = 1;
7107 } else if (np->in_nsrcmsk == 0xffffffff) {
7109 if (np->in_stepnext == 0)
7110 np->in_stepnext = 1;
7112 } else if (np->in_nsrcmsk != 0xffffffff) {
7113 if (np->in_stepnext == 0 && changed == -1) {
7120 if ((flags & IPN_TCPUDPICMP) != 0) {
7121 if (np->in_spnext != 0)
7122 frnat.fin_data[0] = np->in_spnext;
7125 * Standard port translation. Select next port.
7127 if ((flags & IPN_FIXEDSPORT) != 0) {
7128 np->in_stepnext = 2;
7129 } else if ((np->in_stepnext == 1) &&
7130 (changed == -1) && (natl != NULL)) {
7134 if (np->in_spnext > np->in_spmax)
7135 np->in_spnext = np->in_spmin;
7138 np->in_stepnext = 2;
7140 np->in_stepnext &= 0x3;
7143 * Find a new destination address
7145 /* TRACE (fin, np, l, frnat) */
7146 DT4(ipf_nat_rewrite_2, frinfo_t *, fin, ipnat_t *, np, int, l, frinfo_t *, &frnat);
7148 if (ipf_nat_nextaddr(fin, &np->in_ndst, &frnat.fin_daddr,
7149 &frnat.fin_daddr) == -1)
7151 if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
7153 if (np->in_stepnext == 2)
7154 np->in_stepnext = 3;
7156 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0)) {
7158 if (np->in_stepnext == 2)
7159 np->in_stepnext = 3;
7161 } else if (np->in_ndstmsk == 0xffffffff) {
7163 if (np->in_stepnext == 2)
7164 np->in_stepnext = 3;
7166 } else if (np->in_ndstmsk != 0xffffffff) {
7167 if ((np->in_stepnext == 2) && (changed == -1) &&
7175 if ((flags & IPN_TCPUDPICMP) != 0) {
7176 if (np->in_dpnext != 0)
7177 frnat.fin_data[1] = np->in_dpnext;
7180 * Standard port translation. Select next port.
7182 if ((flags & IPN_FIXEDDPORT) != 0) {
7183 np->in_stepnext = 0;
7184 } else if (np->in_stepnext == 3 && changed == -1) {
7188 if (np->in_dpnext > np->in_dpmax)
7189 np->in_dpnext = np->in_dpmin;
7192 if (np->in_stepnext == 3)
7193 np->in_stepnext = 0;
7197 DT1(ipf_nat_rewrite_3, frinfo_t *, &frnat);
7200 * Here we do a lookup of the connection as seen from
7201 * the outside. If an IP# pair already exists, try
7202 * again. So if you have A->B becomes C->B, you can
7203 * also have D->E become C->E but not D->B causing
7204 * another C->B. Also take protocol and ports into
7205 * account when determining whether a pre-existing
7206 * NAT setup will cause an external conflict where
7207 * this is appropriate.
7209 * fin_data[] is swapped around because we are doing a
7210 * lookup of the packet is if it were moving in the opposite
7211 * direction of the one we are working with now.
7213 if (flags & IPN_TCPUDP) {
7214 swap = frnat.fin_data[0];
7215 frnat.fin_data[0] = frnat.fin_data[1];
7216 frnat.fin_data[1] = swap;
7218 if (fin->fin_out == 1) {
7219 natl = ipf_nat_inlookup(&frnat,
7220 flags & ~(SI_WILDP|NAT_SEARCH),
7222 frnat.fin_dst, frnat.fin_src);
7225 natl = ipf_nat_outlookup(&frnat,
7226 flags & ~(SI_WILDP|NAT_SEARCH),
7228 frnat.fin_dst, frnat.fin_src);
7230 if (flags & IPN_TCPUDP) {
7231 swap = frnat.fin_data[0];
7232 frnat.fin_data[0] = frnat.fin_data[1];
7233 frnat.fin_data[1] = swap;
7236 /* TRACE natl, in_stepnext, l */
7237 DT3(ipf_nat_rewrite_2, nat_t *, natl, ipnat_t *, np , int, l);
7239 if ((natl != NULL) && (l > 8)) /* XXX 8 is arbitrary */
7242 np->in_stepnext &= 0x3;
7246 } while (natl != NULL);
7248 nat->nat_osrcip = fin->fin_src;
7249 nat->nat_odstip = fin->fin_dst;
7250 nat->nat_nsrcip = frnat.fin_src;
7251 nat->nat_ndstip = frnat.fin_dst;
7253 if ((flags & IPN_TCPUDP) != 0) {
7254 nat->nat_osport = htons(fin->fin_data[0]);
7255 nat->nat_odport = htons(fin->fin_data[1]);
7256 nat->nat_nsport = htons(frnat.fin_data[0]);
7257 nat->nat_ndport = htons(frnat.fin_data[1]);
7258 } else if ((flags & IPN_ICMPQUERY) != 0) {
7259 nat->nat_oicmpid = fin->fin_data[1];
7260 nat->nat_nicmpid = frnat.fin_data[1];
7267 /* ------------------------------------------------------------------------ */
7268 /* Function: nat_newdivert */
7269 /* Returns: int - -1 == error, 0 == success */
7270 /* Parameters: fin(I) - pointer to packet information */
7271 /* nat(I) - pointer to NAT entry */
7272 /* ni(I) - pointer to structure with misc. information needed */
7273 /* to create new NAT entry. */
7274 /* Write Lock: ipf_nat */
7276 /* Create a new NAT divert session as defined by the NAT rule. This is */
7277 /* somewhat different to other NAT session creation routines because we */
7278 /* do not iterate through either port numbers or IP addresses, searching */
7279 /* for a unique mapping, however, a complimentary duplicate check is made. */
7280 /* ------------------------------------------------------------------------ */
7282 ipf_nat_newdivert(fin, nat, nai)
7287 ipf_main_softc_t *softc = fin->fin_main_soft;
7288 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7295 bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
7298 nat->nat_osrcaddr = fin->fin_saddr;
7299 nat->nat_odstaddr = fin->fin_daddr;
7300 frnat.fin_saddr = htonl(np->in_snip);
7301 frnat.fin_daddr = htonl(np->in_dnip);
7302 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
7303 nat->nat_osport = htons(fin->fin_data[0]);
7304 nat->nat_odport = htons(fin->fin_data[1]);
7305 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
7306 nat->nat_oicmpid = fin->fin_data[1];
7309 if (np->in_redir & NAT_DIVERTUDP) {
7310 frnat.fin_data[0] = np->in_spnext;
7311 frnat.fin_data[1] = np->in_dpnext;
7312 frnat.fin_flx |= FI_TCPUDP;
7315 frnat.fin_flx &= ~FI_TCPUDP;
7319 if (fin->fin_out == 1) {
7320 natl = ipf_nat_inlookup(&frnat, 0, p,
7321 frnat.fin_dst, frnat.fin_src);
7324 natl = ipf_nat_outlookup(&frnat, 0, p,
7325 frnat.fin_dst, frnat.fin_src);
7329 NBUMPSIDED(fin->fin_out, ns_divert_exist);
7330 DT3(ns_divert_exist, fr_info_t *, fin, nat_t *, nat, natinfo_t, nai);
7334 nat->nat_nsrcaddr = frnat.fin_saddr;
7335 nat->nat_ndstaddr = frnat.fin_daddr;
7336 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
7337 nat->nat_nsport = htons(frnat.fin_data[0]);
7338 nat->nat_ndport = htons(frnat.fin_data[1]);
7339 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
7340 nat->nat_nicmpid = frnat.fin_data[1];
7343 nat->nat_pr[fin->fin_out] = fin->fin_p;
7344 nat->nat_pr[1 - fin->fin_out] = p;
7346 if (np->in_redir & NAT_REDIRECT)
7347 nat->nat_dir = NAT_DIVERTIN;
7349 nat->nat_dir = NAT_DIVERTOUT;
7355 /* ------------------------------------------------------------------------ */
7356 /* Function: nat_builddivertmp */
7357 /* Returns: int - -1 == error, 0 == success */
7358 /* Parameters: softn(I) - pointer to NAT context structure */
7359 /* np(I) - pointer to a NAT rule */
7361 /* For divert rules, a skeleton packet representing what will be prepended */
7362 /* to the real packet is created. Even though we don't have the full */
7363 /* packet here, a checksum is calculated that we update later when we */
7364 /* fill in the final details. At present a 0 checksum for UDP is being set */
7365 /* here because it is expected that divert will be used for localhost. */
7366 /* ------------------------------------------------------------------------ */
7368 ipf_nat_builddivertmp(softn, np)
7369 ipf_nat_softc_t *softn;
7376 if ((np->in_redir & NAT_DIVERTUDP) != 0)
7377 len = sizeof(ip_t) + sizeof(udphdr_t);
7381 ALLOC_MB_T(np->in_divmp, len);
7382 if (np->in_divmp == NULL) {
7383 NBUMPD(ipf_nat_stats, ns_divert_build);
7388 * First, the header to get the packet diverted to the new destination
7390 ip = MTOD(np->in_divmp, ip_t *);
7394 if ((np->in_redir & NAT_DIVERTUDP) != 0)
7395 ip->ip_p = IPPROTO_UDP;
7397 ip->ip_p = IPPROTO_IPIP;
7401 ip->ip_len = htons(len);
7403 ip->ip_src.s_addr = htonl(np->in_snip);
7404 ip->ip_dst.s_addr = htonl(np->in_dnip);
7405 ip->ip_sum = ipf_cksum((u_short *)ip, sizeof(*ip));
7407 if (np->in_redir & NAT_DIVERTUDP) {
7408 uh = (udphdr_t *)(ip + 1);
7411 uh->uh_sport = htons(np->in_spnext);
7412 uh->uh_dport = htons(np->in_dpnext);
7419 #define MINDECAP (sizeof(ip_t) + sizeof(udphdr_t) + sizeof(ip_t))
7421 /* ------------------------------------------------------------------------ */
7422 /* Function: nat_decap */
7423 /* Returns: int - -1 == error, 0 == success */
7424 /* Parameters: fin(I) - pointer to packet information */
7425 /* nat(I) - pointer to current NAT session */
7427 /* This function is responsible for undoing a packet's encapsulation in the */
7428 /* reverse of an encap/divert rule. After removing the outer encapsulation */
7429 /* it is necessary to call ipf_makefrip() again so that the contents of 'fin'*/
7430 /* match the "new" packet as it may still be used by IPFilter elsewhere. */
7431 /* We use "dir" here as the basis for some of the expectations about the */
7432 /* outer header. If we return an error, the goal is to leave the original */
7433 /* packet information undisturbed - this falls short at the end where we'd */
7434 /* need to back a backup copy of "fin" - expensive. */
7435 /* ------------------------------------------------------------------------ */
7437 ipf_nat_decap(fin, nat)
7441 ipf_main_softc_t *softc = fin->fin_main_soft;
7442 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7448 if ((fin->fin_flx & FI_ICMPERR) != 0) {
7450 * ICMP packets don't get decapsulated, instead what we need
7451 * to do is change the ICMP reply from including (in the data
7452 * portion for errors) the encapsulated packet that we sent
7453 * out to something that resembles the original packet prior
7454 * to encapsulation. This isn't done here - all we're doing
7455 * here is changing the outer address to ensure that it gets
7456 * targetted back to the correct system.
7459 if (nat->nat_dir & NAT_OUTBOUND) {
7460 u_32_t sum1, sum2, sumd;
7462 sum1 = ntohl(fin->fin_daddr);
7463 sum2 = ntohl(nat->nat_osrcaddr);
7464 CALC_SUMD(sum1, sum2, sumd);
7465 fin->fin_ip->ip_dst = nat->nat_osrcip;
7466 fin->fin_daddr = nat->nat_osrcaddr;
7467 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
7468 defined(__osf__) || defined(linux)
7469 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, sumd, 0);
7476 skip = fin->fin_hlen;
7478 switch (nat->nat_dir)
7481 case NAT_DIVERTOUT :
7482 if (fin->fin_plen < MINDECAP)
7484 skip += sizeof(udphdr_t);
7489 if (fin->fin_plen < (skip + sizeof(ip_t)))
7498 * The aim here is to keep the original packet details in "fin" for
7499 * as long as possible so that returning with an error is for the
7500 * original packet and there is little undoing work to do.
7502 if (M_LEN(m) < skip + sizeof(ip_t)) {
7503 if (ipf_pr_pullup(fin, skip + sizeof(ip_t)) == -1)
7507 hdr = MTOD(fin->fin_m, char *);
7508 fin->fin_ip = (ip_t *)(hdr + skip);
7509 hlen = IP_HL(fin->fin_ip) << 2;
7511 if (ipf_pr_pullup(fin, skip + hlen) == -1) {
7512 NBUMPSIDED(fin->fin_out, ns_decap_pullup);
7516 fin->fin_hlen = hlen;
7517 fin->fin_dlen -= skip;
7518 fin->fin_plen -= skip;
7519 fin->fin_ipoff += skip;
7521 if (ipf_makefrip(hlen, (ip_t *)hdr, fin) == -1) {
7522 NBUMPSIDED(fin->fin_out, ns_decap_bad);
7530 /* ------------------------------------------------------------------------ */
7531 /* Function: nat_nextaddr */
7532 /* Returns: int - -1 == bad input (no new address), */
7533 /* 0 == success and dst has new address */
7534 /* Parameters: fin(I) - pointer to packet information */
7535 /* na(I) - how to generate new address */
7536 /* old(I) - original address being replaced */
7537 /* dst(O) - where to put the new address */
7538 /* Write Lock: ipf_nat */
7540 /* This function uses the contents of the "na" structure, in combination */
7541 /* with "old" to produce a new address to store in "dst". Not all of the */
7542 /* possible uses of "na" will result in a new address. */
7543 /* ------------------------------------------------------------------------ */
7545 ipf_nat_nextaddr(fin, na, old, dst)
7550 ipf_main_softc_t *softc = fin->fin_main_soft;
7551 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7552 u_32_t amin, amax, new;
7557 amin = na->na_addr[0].in4.s_addr;
7559 switch (na->na_atype)
7562 amax = na->na_addr[1].in4.s_addr;
7565 case FRI_NETMASKED :
7569 * Compute the maximum address by adding the inverse of the
7570 * netmask to the minimum address.
7572 amax = ~na->na_addr[1].in4.s_addr;
7579 case FRI_BROADCAST :
7583 DT4(ns_na_atype, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7589 if (na->na_atype == FRI_LOOKUP) {
7590 if (na->na_type == IPLT_DSTLIST) {
7591 error = ipf_dstlist_select_node(fin, na->na_ptr, dst,
7594 NBUMPSIDE(fin->fin_out, ns_badnextaddr);
7595 DT4(ns_badnextaddr_1, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7598 } else if (na->na_atype == IPLT_NONE) {
7600 * 0/0 as the new address means leave it alone.
7602 if (na->na_addr[0].in4.s_addr == 0 &&
7603 na->na_addr[1].in4.s_addr == 0) {
7607 * 0/32 means get the interface's address
7609 } else if (na->na_addr[0].in4.s_addr == 0 &&
7610 na->na_addr[1].in4.s_addr == 0xffffffff) {
7611 if (ipf_ifpaddr(softc, 4, na->na_atype,
7612 fin->fin_ifp, &newip, NULL) == -1) {
7613 NBUMPSIDED(fin->fin_out, ns_ifpaddrfail);
7614 DT4(ns_ifpaddrfail, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7617 new = newip.in4.s_addr;
7619 new = htonl(na->na_nextip);
7625 NBUMPSIDE(fin->fin_out, ns_badnextaddr);
7626 DT4(ns_badnextaddr_2, fr_info_t *, fin, nat_addr_t *, na, u_32_t *, old, u_32_t *, new);
7633 /* ------------------------------------------------------------------------ */
7634 /* Function: nat_nextaddrinit */
7635 /* Returns: int - 0 == success, else error number */
7636 /* Parameters: softc(I) - pointer to soft context main structure */
7637 /* na(I) - NAT address information for generating new addr*/
7638 /* initial(I) - flag indicating if it is the first call for */
7639 /* this "na" structure. */
7640 /* ifp(I) - network interface to derive address */
7641 /* information from. */
7643 /* This function is expected to be called in two scenarious: when a new NAT */
7644 /* rule is loaded into the kernel and when the list of NAT rules is sync'd */
7645 /* up with the valid network interfaces (possibly due to them changing.) */
7646 /* To distinguish between these, the "initial" parameter is used. If it is */
7647 /* 1 then this indicates the rule has just been reloaded and 0 for when we */
7648 /* are updating information. This difference is important because in */
7649 /* instances where we are not updating address information associated with */
7650 /* a network interface, we don't want to disturb what the "next" address to */
7651 /* come out of ipf_nat_nextaddr() will be. */
7652 /* ------------------------------------------------------------------------ */
7654 ipf_nat_nextaddrinit(softc, base, na, initial, ifp)
7655 ipf_main_softc_t *softc;
7662 switch (na->na_atype)
7665 if (na->na_subtype == 0) {
7666 na->na_ptr = ipf_lookup_res_num(softc, IPL_LOGNAT,
7670 } else if (na->na_subtype == 1) {
7671 na->na_ptr = ipf_lookup_res_name(softc, IPL_LOGNAT,
7676 if (na->na_func == NULL) {
7680 if (na->na_ptr == NULL) {
7687 case FRI_BROADCAST :
7689 case FRI_NETMASKED :
7692 (void )ipf_ifpaddr(softc, 4, na->na_atype, ifp,
7693 &na->na_addr[0], &na->na_addr[1]);
7699 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
7703 na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
7707 na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
7715 if (initial && (na->na_atype == FRI_NORMAL)) {
7716 if (na->na_addr[0].in4.s_addr == 0) {
7717 if ((na->na_addr[1].in4.s_addr == 0xffffffff) ||
7718 (na->na_addr[1].in4.s_addr == 0)) {
7723 if (na->na_addr[1].in4.s_addr == 0xffffffff) {
7724 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
7726 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr) + 1;
7734 /* ------------------------------------------------------------------------ */
7735 /* Function: ipf_nat_matchflush */
7736 /* Returns: int - -1 == error, 0 == success */
7737 /* Parameters: softc(I) - pointer to soft context main structure */
7738 /* softn(I) - pointer to NAT context structure */
7739 /* nat(I) - pointer to current NAT session */
7741 /* ------------------------------------------------------------------------ */
7743 ipf_nat_matchflush(softc, softn, data)
7744 ipf_main_softc_t *softc;
7745 ipf_nat_softc_t *softn;
7748 int *array, flushed, error;
7749 nat_t *nat, *natnext;
7752 error = ipf_matcharray_load(softc, data, &obj, &array);
7758 for (nat = softn->ipf_nat_instances; nat != NULL; nat = natnext) {
7759 natnext = nat->nat_next;
7760 if (ipf_nat_matcharray(nat, array, softc->ipf_ticks) == 0) {
7761 ipf_nat_delete(softc, nat, NL_FLUSH);
7766 obj.ipfo_retval = flushed;
7767 error = BCOPYOUT(&obj, data, sizeof(obj));
7769 KFREES(array, array[0] * sizeof(*array));
7775 /* ------------------------------------------------------------------------ */
7776 /* Function: ipf_nat_matcharray */
7777 /* Returns: int - -1 == error, 0 == success */
7778 /* Parameters: fin(I) - pointer to packet information */
7779 /* nat(I) - pointer to current NAT session */
7781 /* ------------------------------------------------------------------------ */
7783 ipf_nat_matcharray(nat, array, ticks)
7794 for (; n > 0; x += 3 + x[2]) {
7795 if (x[0] == IPF_EXP_END)
7804 if (p != 0 && p != nat->nat_pr[1])
7809 case IPF_EXP_IP_PR :
7810 for (i = 0; !e && i < x[2]; i++) {
7811 e |= (nat->nat_pr[1] == x[i + 3]);
7815 case IPF_EXP_IP_SRCADDR :
7816 if (nat->nat_v[0] == 4) {
7817 for (i = 0; !e && i < x[2]; i++) {
7818 e |= ((nat->nat_osrcaddr & x[i + 4]) ==
7822 if (nat->nat_v[1] == 4) {
7823 for (i = 0; !e && i < x[2]; i++) {
7824 e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
7830 case IPF_EXP_IP_DSTADDR :
7831 if (nat->nat_v[0] == 4) {
7832 for (i = 0; !e && i < x[2]; i++) {
7833 e |= ((nat->nat_odstaddr & x[i + 4]) ==
7837 if (nat->nat_v[1] == 4) {
7838 for (i = 0; !e && i < x[2]; i++) {
7839 e |= ((nat->nat_ndstaddr & x[i + 4]) ==
7845 case IPF_EXP_IP_ADDR :
7846 for (i = 0; !e && i < x[2]; i++) {
7847 if (nat->nat_v[0] == 4) {
7848 e |= ((nat->nat_osrcaddr & x[i + 4]) ==
7851 if (nat->nat_v[1] == 4) {
7852 e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
7855 if (nat->nat_v[0] == 4) {
7856 e |= ((nat->nat_odstaddr & x[i + 4]) ==
7859 if (nat->nat_v[1] == 4) {
7860 e |= ((nat->nat_ndstaddr & x[i + 4]) ==
7867 case IPF_EXP_IP6_SRCADDR :
7868 if (nat->nat_v[0] == 6) {
7869 for (i = 0; !e && i < x[3]; i++) {
7870 e |= IP6_MASKEQ(&nat->nat_osrc6,
7871 x + i + 7, x + i + 3);
7874 if (nat->nat_v[1] == 6) {
7875 for (i = 0; !e && i < x[3]; i++) {
7876 e |= IP6_MASKEQ(&nat->nat_nsrc6,
7877 x + i + 7, x + i + 3);
7882 case IPF_EXP_IP6_DSTADDR :
7883 if (nat->nat_v[0] == 6) {
7884 for (i = 0; !e && i < x[3]; i++) {
7885 e |= IP6_MASKEQ(&nat->nat_odst6,
7890 if (nat->nat_v[1] == 6) {
7891 for (i = 0; !e && i < x[3]; i++) {
7892 e |= IP6_MASKEQ(&nat->nat_ndst6,
7899 case IPF_EXP_IP6_ADDR :
7900 for (i = 0; !e && i < x[3]; i++) {
7901 if (nat->nat_v[0] == 6) {
7902 e |= IP6_MASKEQ(&nat->nat_osrc6,
7906 if (nat->nat_v[0] == 6) {
7907 e |= IP6_MASKEQ(&nat->nat_odst6,
7911 if (nat->nat_v[1] == 6) {
7912 e |= IP6_MASKEQ(&nat->nat_nsrc6,
7916 if (nat->nat_v[1] == 6) {
7917 e |= IP6_MASKEQ(&nat->nat_ndst6,
7925 case IPF_EXP_UDP_PORT :
7926 case IPF_EXP_TCP_PORT :
7927 for (i = 0; !e && i < x[2]; i++) {
7928 e |= (nat->nat_nsport == x[i + 3]) ||
7929 (nat->nat_ndport == x[i + 3]);
7933 case IPF_EXP_UDP_SPORT :
7934 case IPF_EXP_TCP_SPORT :
7935 for (i = 0; !e && i < x[2]; i++) {
7936 e |= (nat->nat_nsport == x[i + 3]);
7940 case IPF_EXP_UDP_DPORT :
7941 case IPF_EXP_TCP_DPORT :
7942 for (i = 0; !e && i < x[2]; i++) {
7943 e |= (nat->nat_ndport == x[i + 3]);
7947 case IPF_EXP_TCP_STATE :
7948 for (i = 0; !e && i < x[2]; i++) {
7949 e |= (nat->nat_tcpstate[0] == x[i + 3]) ||
7950 (nat->nat_tcpstate[1] == x[i + 3]);
7954 case IPF_EXP_IDLE_GT :
7955 e |= (ticks - nat->nat_touched > x[3]);
7968 /* ------------------------------------------------------------------------ */
7969 /* Function: ipf_nat_gettable */
7970 /* Returns: int - 0 = success, else error */
7971 /* Parameters: softc(I) - pointer to soft context main structure */
7972 /* softn(I) - pointer to NAT context structure */
7973 /* data(I) - pointer to ioctl data */
7975 /* This function handles ioctl requests for tables of nat information. */
7976 /* At present the only table it deals with is the hash bucket statistics. */
7977 /* ------------------------------------------------------------------------ */
7979 ipf_nat_gettable(softc, softn, data)
7980 ipf_main_softc_t *softc;
7981 ipf_nat_softc_t *softn;
7987 error = ipf_inobj(softc, data, NULL, &table, IPFOBJ_GTABLE);
7991 switch (table.ita_type)
7993 case IPFTABLE_BUCKETS_NATIN :
7994 error = COPYOUT(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
7996 softn->ipf_nat_table_sz * sizeof(u_int));
7999 case IPFTABLE_BUCKETS_NATOUT :
8000 error = COPYOUT(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
8002 softn->ipf_nat_table_sz * sizeof(u_int));
8018 /* ------------------------------------------------------------------------ */
8019 /* Function: ipf_nat_settimeout */
8020 /* Returns: int - 0 = success, else failure */
8021 /* Parameters: softc(I) - pointer to soft context main structure */
8022 /* t(I) - pointer to tunable */
8023 /* p(I) - pointer to new tuning data */
8025 /* Apply the timeout change to the NAT timeout queues. */
8026 /* ------------------------------------------------------------------------ */
8028 ipf_nat_settimeout(softc, t, p)
8029 struct ipf_main_softc_s *softc;
8033 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8035 if (!strncmp(t->ipft_name, "tcp_", 4))
8036 return ipf_settimeout_tcp(t, p, softn->ipf_nat_tcptq);
8038 if (!strcmp(t->ipft_name, "udp_timeout")) {
8039 ipf_apply_timeout(&softn->ipf_nat_udptq, p->ipftu_int);
8040 } else if (!strcmp(t->ipft_name, "udp_ack_timeout")) {
8041 ipf_apply_timeout(&softn->ipf_nat_udpacktq, p->ipftu_int);
8042 } else if (!strcmp(t->ipft_name, "icmp_timeout")) {
8043 ipf_apply_timeout(&softn->ipf_nat_icmptq, p->ipftu_int);
8044 } else if (!strcmp(t->ipft_name, "icmp_ack_timeout")) {
8045 ipf_apply_timeout(&softn->ipf_nat_icmpacktq, p->ipftu_int);
8046 } else if (!strcmp(t->ipft_name, "ip_timeout")) {
8047 ipf_apply_timeout(&softn->ipf_nat_iptq, p->ipftu_int);
8056 /* ------------------------------------------------------------------------ */
8057 /* Function: ipf_nat_rehash */
8058 /* Returns: int - 0 = success, else failure */
8059 /* Parameters: softc(I) - pointer to soft context main structure */
8060 /* t(I) - pointer to tunable */
8061 /* p(I) - pointer to new tuning data */
8063 /* To change the size of the basic NAT table, we need to first allocate the */
8064 /* new tables (lest it fails and we've got nowhere to store all of the NAT */
8065 /* sessions currently active) and then walk through the entire list and */
8066 /* insert them into the table. There are two tables here: an inbound one */
8067 /* and an outbound one. Each NAT entry goes into each table once. */
8068 /* ------------------------------------------------------------------------ */
8070 ipf_nat_rehash(softc, t, p)
8071 ipf_main_softc_t *softc;
8075 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8076 nat_t **newtab[2], *nat, **natp;
8077 u_int *bucketlens[2];
8084 newsize = p->ipftu_int;
8086 * In case there is nothing to do...
8088 if (newsize == softn->ipf_nat_table_sz)
8093 bucketlens[0] = NULL;
8094 bucketlens[1] = NULL;
8096 * 4 tables depend on the NAT table size: the inbound looking table,
8097 * the outbound lookup table and the hash chain length for each.
8099 KMALLOCS(newtab[0], nat_t **, newsize * sizeof(nat_t *));
8100 if (newtab == NULL) {
8105 KMALLOCS(newtab[1], nat_t **, newsize * sizeof(nat_t *));
8106 if (newtab == NULL) {
8111 KMALLOCS(bucketlens[0], u_int *, newsize * sizeof(u_int));
8112 if (bucketlens[0] == NULL) {
8117 KMALLOCS(bucketlens[1], u_int *, newsize * sizeof(u_int));
8118 if (bucketlens[1] == NULL) {
8124 * Recalculate the maximum length based on the new size.
8126 for (maxbucket = 0, i = newsize; i > 0; i >>= 1)
8130 bzero((char *)newtab[0], newsize * sizeof(nat_t *));
8131 bzero((char *)newtab[1], newsize * sizeof(nat_t *));
8132 bzero((char *)bucketlens[0], newsize * sizeof(u_int));
8133 bzero((char *)bucketlens[1], newsize * sizeof(u_int));
8135 WRITE_ENTER(&softc->ipf_nat);
8137 if (softn->ipf_nat_table[0] != NULL) {
8138 KFREES(softn->ipf_nat_table[0],
8139 softn->ipf_nat_table_sz *
8140 sizeof(*softn->ipf_nat_table[0]));
8142 softn->ipf_nat_table[0] = newtab[0];
8144 if (softn->ipf_nat_table[1] != NULL) {
8145 KFREES(softn->ipf_nat_table[1],
8146 softn->ipf_nat_table_sz *
8147 sizeof(*softn->ipf_nat_table[1]));
8149 softn->ipf_nat_table[1] = newtab[1];
8151 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
8152 KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
8153 softn->ipf_nat_table_sz * sizeof(u_int));
8155 softn->ipf_nat_stats.ns_side[0].ns_bucketlen = bucketlens[0];
8157 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
8158 KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
8159 softn->ipf_nat_table_sz * sizeof(u_int));
8161 softn->ipf_nat_stats.ns_side[1].ns_bucketlen = bucketlens[1];
8164 if (softn->ipf_nat_stats.ns_side6[0].ns_bucketlen != NULL) {
8165 KFREES(softn->ipf_nat_stats.ns_side6[0].ns_bucketlen,
8166 softn->ipf_nat_table_sz * sizeof(u_int));
8168 softn->ipf_nat_stats.ns_side6[0].ns_bucketlen = bucketlens[0];
8170 if (softn->ipf_nat_stats.ns_side6[1].ns_bucketlen != NULL) {
8171 KFREES(softn->ipf_nat_stats.ns_side6[1].ns_bucketlen,
8172 softn->ipf_nat_table_sz * sizeof(u_int));
8174 softn->ipf_nat_stats.ns_side6[1].ns_bucketlen = bucketlens[1];
8177 softn->ipf_nat_maxbucket = maxbucket;
8178 softn->ipf_nat_table_sz = newsize;
8180 * Walk through the entire list of NAT table entries and put them
8181 * in the new NAT table, somewhere. Because we have a new table,
8182 * we need to restart the counter of how many chains are in use.
8184 softn->ipf_nat_stats.ns_side[0].ns_inuse = 0;
8185 softn->ipf_nat_stats.ns_side[1].ns_inuse = 0;
8187 softn->ipf_nat_stats.ns_side6[0].ns_inuse = 0;
8188 softn->ipf_nat_stats.ns_side6[1].ns_inuse = 0;
8191 for (nat = softn->ipf_nat_instances; nat != NULL; nat = nat->nat_next) {
8192 nat->nat_hnext[0] = NULL;
8193 nat->nat_phnext[0] = NULL;
8194 hv = nat->nat_hv[0] % softn->ipf_nat_table_sz;
8196 natp = &softn->ipf_nat_table[0][hv];
8198 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
8200 NBUMPSIDE(0, ns_inuse);
8202 nat->nat_phnext[0] = natp;
8203 nat->nat_hnext[0] = *natp;
8205 NBUMPSIDE(0, ns_bucketlen[hv]);
8207 nat->nat_hnext[1] = NULL;
8208 nat->nat_phnext[1] = NULL;
8209 hv = nat->nat_hv[1] % softn->ipf_nat_table_sz;
8211 natp = &softn->ipf_nat_table[1][hv];
8213 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
8215 NBUMPSIDE(1, ns_inuse);
8217 nat->nat_phnext[1] = natp;
8218 nat->nat_hnext[1] = *natp;
8220 NBUMPSIDE(1, ns_bucketlen[hv]);
8222 RWLOCK_EXIT(&softc->ipf_nat);
8227 if (bucketlens[1] != NULL) {
8228 KFREES(bucketlens[0], newsize * sizeof(u_int));
8230 if (bucketlens[0] != NULL) {
8231 KFREES(bucketlens[0], newsize * sizeof(u_int));
8233 if (newtab[0] != NULL) {
8234 KFREES(newtab[0], newsize * sizeof(nat_t *));
8236 if (newtab[1] != NULL) {
8237 KFREES(newtab[1], newsize * sizeof(nat_t *));
8244 /* ------------------------------------------------------------------------ */
8245 /* Function: ipf_nat_rehash_rules */
8246 /* Returns: int - 0 = success, else failure */
8247 /* Parameters: softc(I) - pointer to soft context main structure */
8248 /* t(I) - pointer to tunable */
8249 /* p(I) - pointer to new tuning data */
8251 /* All of the NAT rules hang off of a hash table that is searched with a */
8252 /* hash on address after the netmask is applied. There is a different table*/
8253 /* for both inbound rules (rdr) and outbound (map.) The resizing will only */
8254 /* affect one of these two tables. */
8255 /* ------------------------------------------------------------------------ */
8257 ipf_nat_rehash_rules(softc, t, p)
8258 ipf_main_softc_t *softc;
8262 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8263 ipnat_t **newtab, *np, ***old, **npp;
8268 newsize = p->ipftu_int;
8270 * In case there is nothing to do...
8272 if (newsize == *t->ipft_pint)
8276 * All inbound rules have the NAT_REDIRECT bit set in in_redir and
8277 * all outbound rules have either NAT_MAP or MAT_MAPBLK set.
8278 * This if statement allows for some more generic code to be below,
8279 * rather than two huge gobs of code that almost do the same thing.
8281 if (t->ipft_pint == &softn->ipf_nat_rdrrules_sz) {
8282 old = &softn->ipf_nat_rdr_rules;
8283 mask = NAT_REDIRECT;
8285 old = &softn->ipf_nat_map_rules;
8286 mask = NAT_MAP|NAT_MAPBLK;
8289 KMALLOCS(newtab, ipnat_t **, newsize * sizeof(ipnat_t *));
8290 if (newtab == NULL) {
8295 bzero((char *)newtab, newsize * sizeof(ipnat_t *));
8297 WRITE_ENTER(&softc->ipf_nat);
8300 KFREES(*old, *t->ipft_pint * sizeof(ipnat_t **));
8303 *t->ipft_pint = newsize;
8305 for (np = softn->ipf_nat_list; np != NULL; np = np->in_next) {
8306 if ((np->in_redir & mask) == 0)
8309 if (np->in_redir & NAT_REDIRECT) {
8310 np->in_rnext = NULL;
8311 hv = np->in_hv[0] % newsize;
8312 for (npp = newtab + hv; *npp != NULL; )
8313 npp = &(*npp)->in_rnext;
8314 np->in_prnext = npp;
8317 if (np->in_redir & NAT_MAP) {
8318 np->in_mnext = NULL;
8319 hv = np->in_hv[1] % newsize;
8320 for (npp = newtab + hv; *npp != NULL; )
8321 npp = &(*npp)->in_mnext;
8322 np->in_pmnext = npp;
8327 RWLOCK_EXIT(&softc->ipf_nat);
8333 /* ------------------------------------------------------------------------ */
8334 /* Function: ipf_nat_hostmap_rehash */
8335 /* Returns: int - 0 = success, else failure */
8336 /* Parameters: softc(I) - pointer to soft context main structure */
8337 /* t(I) - pointer to tunable */
8338 /* p(I) - pointer to new tuning data */
8340 /* Allocate and populate a new hash table that will contain a reference to */
8341 /* all of the active IP# translations currently in place. */
8342 /* ------------------------------------------------------------------------ */
8344 ipf_nat_hostmap_rehash(softc, t, p)
8345 ipf_main_softc_t *softc;
8349 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8350 hostmap_t *hm, **newtab;
8354 newsize = p->ipftu_int;
8356 * In case there is nothing to do...
8358 if (newsize == *t->ipft_pint)
8361 KMALLOCS(newtab, hostmap_t **, newsize * sizeof(hostmap_t *));
8362 if (newtab == NULL) {
8367 bzero((char *)newtab, newsize * sizeof(hostmap_t *));
8369 WRITE_ENTER(&softc->ipf_nat);
8370 if (softn->ipf_hm_maptable != NULL) {
8371 KFREES(softn->ipf_hm_maptable,
8372 softn->ipf_nat_hostmap_sz * sizeof(hostmap_t *));
8374 softn->ipf_hm_maptable = newtab;
8375 softn->ipf_nat_hostmap_sz = newsize;
8377 for (hm = softn->ipf_hm_maplist; hm != NULL; hm = hm->hm_next) {
8378 hv = hm->hm_hv % softn->ipf_nat_hostmap_sz;
8379 hm->hm_hnext = softn->ipf_hm_maptable[hv];
8380 hm->hm_phnext = softn->ipf_hm_maptable + hv;
8381 if (softn->ipf_hm_maptable[hv] != NULL)
8382 softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
8383 softn->ipf_hm_maptable[hv] = hm;
8385 RWLOCK_EXIT(&softc->ipf_nat);
8391 /* ------------------------------------------------------------------------ */
8392 /* Function: ipf_nat_add_tq */
8393 /* Parameters: softc(I) - pointer to soft context main structure */
8395 /* ------------------------------------------------------------------------ */
8397 ipf_nat_add_tq(softc, ttl)
8398 ipf_main_softc_t *softc;
8401 ipf_nat_softc_t *softs = softc->ipf_nat_soft;
8403 return ipf_addtimeoutqueue(softc, &softs->ipf_nat_utqe, ttl);
8406 /* ------------------------------------------------------------------------ */
8407 /* Function: ipf_nat_uncreate */
8409 /* Parameters: fin(I) - pointer to packet information */
8411 /* This function is used to remove a NAT entry from the NAT table when we */
8412 /* decide that the create was actually in error. It is thus assumed that */
8413 /* fin_flx will have both FI_NATED and FI_NATNEW set. Because we're dealing */
8414 /* with the translated packet (not the original), we have to reverse the */
8415 /* lookup. Although doing the lookup is expensive (relatively speaking), it */
8416 /* is not anticipated that this will be a frequent occurance for normal */
8417 /* traffic patterns. */
8418 /* ------------------------------------------------------------------------ */
8420 ipf_nat_uncreate(fin)
8423 ipf_main_softc_t *softc = fin->fin_main_soft;
8424 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8441 WRITE_ENTER(&softc->ipf_nat);
8443 if (fin->fin_out == 0) {
8444 nat = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
8445 fin->fin_dst, fin->fin_src);
8447 nat = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
8448 fin->fin_src, fin->fin_dst);
8452 NBUMPSIDE(fin->fin_out, ns_uncreate[0]);
8453 ipf_nat_delete(softc, nat, NL_DESTROY);
8455 NBUMPSIDE(fin->fin_out, ns_uncreate[1]);
8458 RWLOCK_EXIT(&softc->ipf_nat);
8462 /* ------------------------------------------------------------------------ */
8463 /* Function: ipf_nat_cmp_rules */
8464 /* Returns: int - 0 == success, else rules do not match. */
8465 /* Parameters: n1(I) - first rule to compare */
8466 /* n2(I) - first rule to compare */
8468 /* Compare two rules using pointers to each rule. A straight bcmp will not */
8469 /* work as some fields (such as in_dst, in_pkts) actually do change once */
8470 /* the rule has been loaded into the kernel. Whilst this function returns */
8471 /* various non-zero returns, they're strictly to aid in debugging. Use of */
8472 /* this function should simply care if the result is zero or not. */
8473 /* ------------------------------------------------------------------------ */
8475 ipf_nat_cmp_rules(n1, n2)
8478 if (n1->in_size != n2->in_size)
8481 if (bcmp((char *)&n1->in_v, (char *)&n2->in_v,
8482 offsetof(ipnat_t, in_ndst) - offsetof(ipnat_t, in_v)) != 0)
8485 if (bcmp((char *)&n1->in_tuc, (char *)&n2->in_tuc,
8486 n1->in_size - offsetof(ipnat_t, in_tuc)) != 0)
8488 if (n1->in_ndst.na_atype != n2->in_ndst.na_atype)
8490 if (n1->in_ndst.na_function != n2->in_ndst.na_function)
8492 if (bcmp((char *)&n1->in_ndst.na_addr, (char *)&n2->in_ndst.na_addr,
8493 sizeof(n1->in_ndst.na_addr)))
8495 if (n1->in_nsrc.na_atype != n2->in_nsrc.na_atype)
8497 if (n1->in_nsrc.na_function != n2->in_nsrc.na_function)
8499 if (bcmp((char *)&n1->in_nsrc.na_addr, (char *)&n2->in_nsrc.na_addr,
8500 sizeof(n1->in_nsrc.na_addr)))
8502 if (n1->in_odst.na_atype != n2->in_odst.na_atype)
8504 if (n1->in_odst.na_function != n2->in_odst.na_function)
8506 if (bcmp((char *)&n1->in_odst.na_addr, (char *)&n2->in_odst.na_addr,
8507 sizeof(n1->in_odst.na_addr)))
8509 if (n1->in_osrc.na_atype != n2->in_osrc.na_atype)
8511 if (n1->in_osrc.na_function != n2->in_osrc.na_function)
8513 if (bcmp((char *)&n1->in_osrc.na_addr, (char *)&n2->in_osrc.na_addr,
8514 sizeof(n1->in_osrc.na_addr)))
8520 /* ------------------------------------------------------------------------ */
8521 /* Function: ipf_nat_rule_init */
8522 /* Returns: int - 0 == success, else rules do not match. */
8523 /* Parameters: softc(I) - pointer to soft context main structure */
8524 /* softn(I) - pointer to NAT context structure */
8525 /* n(I) - first rule to compare */
8527 /* ------------------------------------------------------------------------ */
8529 ipf_nat_rule_init(softc, softn, n)
8530 ipf_main_softc_t *softc;
8531 ipf_nat_softc_t *softn;
8536 if ((n->in_flags & IPN_SIPRANGE) != 0)
8537 n->in_nsrcatype = FRI_RANGE;
8539 if ((n->in_flags & IPN_DIPRANGE) != 0)
8540 n->in_ndstatype = FRI_RANGE;
8542 if ((n->in_flags & IPN_SPLIT) != 0)
8543 n->in_ndstatype = FRI_SPLIT;
8545 if ((n->in_redir & (NAT_MAP|NAT_REWRITE|NAT_DIVERTUDP)) != 0)
8546 n->in_spnext = n->in_spmin;
8548 if ((n->in_redir & (NAT_REWRITE|NAT_DIVERTUDP)) != 0) {
8549 n->in_dpnext = n->in_dpmin;
8550 } else if (n->in_redir == NAT_REDIRECT) {
8551 n->in_dpnext = n->in_dpmin;
8559 error = ipf_nat_ruleaddrinit(softc, softn, n);
8565 error = ipf_nat6_ruleaddrinit(softc, softn, n);
8574 if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) {
8576 * Prerecord whether or not the destination of the divert
8577 * is local or not to the interface the packet is going
8580 n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1],
8581 n->in_ifps[1], &n->in_ndstip6);
8588 /* ------------------------------------------------------------------------ */
8589 /* Function: ipf_nat_rule_fini */
8590 /* Returns: int - 0 == success, else rules do not match. */
8591 /* Parameters: softc(I) - pointer to soft context main structure */
8592 /* n(I) - rule to work on */
8594 /* This function is used to release any objects that were referenced during */
8595 /* the rule initialisation. This is useful both when free'ing the rule and */
8596 /* when handling ioctls that need to initialise these fields but not */
8597 /* actually use them after the ioctl processing has finished. */
8598 /* ------------------------------------------------------------------------ */
8600 ipf_nat_rule_fini(softc, n)
8601 ipf_main_softc_t *softc;
8604 if (n->in_odst.na_atype == FRI_LOOKUP && n->in_odst.na_ptr != NULL)
8605 ipf_lookup_deref(softc, n->in_odst.na_type, n->in_odst.na_ptr);
8607 if (n->in_osrc.na_atype == FRI_LOOKUP && n->in_osrc.na_ptr != NULL)
8608 ipf_lookup_deref(softc, n->in_osrc.na_type, n->in_osrc.na_ptr);
8610 if (n->in_ndst.na_atype == FRI_LOOKUP && n->in_ndst.na_ptr != NULL)
8611 ipf_lookup_deref(softc, n->in_ndst.na_type, n->in_ndst.na_ptr);
8613 if (n->in_nsrc.na_atype == FRI_LOOKUP && n->in_nsrc.na_ptr != NULL)
8614 ipf_lookup_deref(softc, n->in_nsrc.na_type, n->in_nsrc.na_ptr);
8616 if (n->in_divmp != NULL)
8617 FREE_MB_T(n->in_divmp);