ip_nat.c revision 1.13.2.2.2.1 1 /* $NetBSD: ip_nat.c,v 1.13.2.2.2.1 2017/01/18 08:46:45 skrll Exp $ */
2
3 /*
4 * Copyright (C) 2012 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 */
8 #if defined(KERNEL) || defined(_KERNEL)
9 # undef KERNEL
10 # undef KERNEL
11 # define KERNEL 1
12 # define KERNEL 1
13 #endif
14 #include <sys/errno.h>
15 #include <sys/types.h>
16 #include <sys/param.h>
17 #include <sys/time.h>
18 #include <sys/file.h>
19 #if defined(_KERNEL) && \
20 (defined(__NetBSD_Version) && (__NetBSD_Version >= 399002000))
21 # include <sys/kauth.h>
22 #endif
23 #if !defined(_KERNEL)
24 # include <stdio.h>
25 # include <string.h>
26 # include <stdlib.h>
27 # define KERNEL
28 # ifdef _OpenBSD__
29 struct file;
30 # endif
31 # include <sys/uio.h>
32 # undef KERNEL
33 #endif
34 #if defined(_KERNEL) && \
35 defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)
36 # include <sys/filio.h>
37 # include <sys/fcntl.h>
38 #else
39 # include <sys/ioctl.h>
40 #endif
41 #if !defined(AIX)
42 # include <sys/fcntl.h>
43 #endif
44 #if !defined(linux)
45 # include <sys/protosw.h>
46 #endif
47 #include <sys/socket.h>
48 #if defined(_KERNEL)
49 # include <sys/systm.h>
50 # if !defined(__SVR4) && !defined(__svr4__)
51 # include <sys/mbuf.h>
52 # endif
53 #endif
54 #if defined(__SVR4) || defined(__svr4__)
55 # include <sys/filio.h>
56 # include <sys/byteorder.h>
57 # ifdef KERNEL
58 # include <sys/dditypes.h>
59 # endif
60 # include <sys/stream.h>
61 # include <sys/kmem.h>
62 #endif
63 #if _FreeBSD_version >= 300000
64 # include <sys/queue.h>
65 #endif
66 #include <net/if.h>
67 #if _FreeBSD_version >= 300000
68 # include <net/if_var.h>
69 #endif
70 #ifdef sun
71 # include <net/af.h>
72 #endif
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76
77 #ifdef RFC1825
78 # include <vpn/md5.h>
79 # include <vpn/ipsec.h>
80 extern struct ifnet vpnif;
81 #endif
82
83 #if !defined(linux)
84 # include <netinet/ip_var.h>
85 #endif
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>
102 #endif
103 #ifdef HAS_SYS_MD5_H
104 # include <sys/md5.h>
105 #else
106 # include "md5.h"
107 #endif
108 /* END OF INCLUDES */
109
110 #undef SOCKADDR_IN
111 #define SOCKADDR_IN struct sockaddr_in
112
113 #if !defined(lint)
114 #if defined(__NetBSD__)
115 #include <sys/cdefs.h>
116 __KERNEL_RCSID(0, "$NetBSD: ip_nat.c,v 1.13.2.2.2.1 2017/01/18 08:46:45 skrll Exp $");
117 #else
118 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed";
119 static const char rcsid[] = "@(#)Id: ip_nat.c,v 1.1.1.2 2012/07/22 13:45:27 darrenr Exp";
120 #endif
121 #endif
122
123
124 #define NATFSUM(n,v,f) ((v) == 4 ? (n)->f.in4.s_addr : (n)->f.i6[0] + \
125 (n)->f.i6[1] + (n)->f.i6[2] + (n)->f.i6[3])
126 #define NBUMP(x) softn->(x)++
127 #define NBUMPD(x, y) do { \
128 softn->x.y++; \
129 DT(y); \
130 } while (0)
131 #define NBUMPSIDE(y,x) softn->ipf_nat_stats.ns_side[y].x++
132 #define NBUMPSIDED(y,x) do { softn->ipf_nat_stats.ns_side[y].x++; \
133 DT(x); } while (0)
134 #define NBUMPSIDEX(y,x,z) \
135 do { softn->ipf_nat_stats.ns_side[y].x++; \
136 DT(z); } while (0)
137 #define NBUMPSIDEDF(y,x)do { softn->ipf_nat_stats.ns_side[y].x++; \
138 DT1(x, fr_info_t *, fin); } while (0)
139
140 frentry_t ipfnatblock;
141
142 static ipftuneable_t ipf_nat_tuneables[] = {
143 /* nat */
144 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_lock) },
145 "nat_lock", 0, 1,
146 stsizeof(ipf_nat_softc_t, ipf_nat_lock),
147 IPFT_RDONLY, NULL, NULL },
148 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_sz) },
149 "nat_table_size", 1, 0x7fffffff,
150 stsizeof(ipf_nat_softc_t, ipf_nat_table_sz),
151 0, NULL, ipf_nat_rehash },
152 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_max) },
153 "nat_table_max", 1, 0x7fffffff,
154 stsizeof(ipf_nat_softc_t, ipf_nat_table_max),
155 0, NULL, NULL },
156 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maprules_sz) },
157 "nat_rules_size", 1, 0x7fffffff,
158 stsizeof(ipf_nat_softc_t, ipf_nat_maprules_sz),
159 0, NULL, ipf_nat_rehash_rules },
160 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_rdrrules_sz) },
161 "rdr_rules_size", 1, 0x7fffffff,
162 stsizeof(ipf_nat_softc_t, ipf_nat_rdrrules_sz),
163 0, NULL, ipf_nat_rehash_rules },
164 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_hostmap_sz) },
165 "hostmap_size", 1, 0x7fffffff,
166 stsizeof(ipf_nat_softc_t, ipf_nat_hostmap_sz),
167 0, NULL, ipf_nat_hostmap_rehash },
168 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maxbucket) },
169 "nat_maxbucket",1, 0x7fffffff,
170 stsizeof(ipf_nat_softc_t, ipf_nat_maxbucket),
171 0, NULL, NULL },
172 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_logging) },
173 "nat_logging", 0, 1,
174 stsizeof(ipf_nat_softc_t, ipf_nat_logging),
175 0, NULL, NULL },
176 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_doflush) },
177 "nat_doflush", 0, 1,
178 stsizeof(ipf_nat_softc_t, ipf_nat_doflush),
179 0, NULL, NULL },
180 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_low) },
181 "nat_table_wm_low", 1, 99,
182 stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_low),
183 0, NULL, NULL },
184 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_high) },
185 "nat_table_wm_high", 2, 100,
186 stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_high),
187 0, NULL, NULL },
188 { { 0 },
189 NULL, 0, 0,
190 0,
191 0, NULL, NULL }
192 };
193
194 /* ======================================================================== */
195 /* How the NAT is organised and works. */
196 /* */
197 /* Inside (interface y) NAT Outside (interface x) */
198 /* -------------------- -+- ------------------------------------- */
199 /* Packet going | out, processsed by ipf_nat_checkout() for x */
200 /* ------------> | ------------> */
201 /* src=10.1.1.1 | src=192.1.1.1 */
202 /* | */
203 /* | in, processed by ipf_nat_checkin() for x */
204 /* <------------ | <------------ */
205 /* dst=10.1.1.1 | dst=192.1.1.1 */
206 /* -------------------- -+- ------------------------------------- */
207 /* ipf_nat_checkout() - changes ip_src and if required, sport */
208 /* - creates a new mapping, if required. */
209 /* ipf_nat_checkin() - changes ip_dst and if required, dport */
210 /* */
211 /* In the NAT table, internal source is recorded as "in" and externally */
212 /* seen as "out". */
213 /* ======================================================================== */
214
215
216 #if SOLARIS && !defined(INSTANCES)
217 extern int pfil_delayed_copy;
218 #endif
219
220 static int ipf_nat_flush_entry(ipf_main_softc_t *, void *);
221 static int ipf_nat_getent(ipf_main_softc_t *, void *, int);
222 static int ipf_nat_getsz(ipf_main_softc_t *, void *, int);
223 static int ipf_nat_putent(ipf_main_softc_t *, void *, int);
224 static void ipf_nat_addmap(ipf_nat_softc_t *, ipnat_t *);
225 static void ipf_nat_addrdr(ipf_nat_softc_t *, ipnat_t *);
226 static int ipf_nat_builddivertmp(ipf_nat_softc_t *, ipnat_t *);
227 static int ipf_nat_clearlist(ipf_main_softc_t *, ipf_nat_softc_t *);
228 static int ipf_nat_cmp_rules(ipnat_t *, ipnat_t *);
229 static int ipf_nat_decap(fr_info_t *, nat_t *);
230 static void ipf_nat_delrule(ipf_main_softc_t *, ipf_nat_softc_t *,
231 ipnat_t *, int);
232 static int ipf_nat_extraflush(ipf_main_softc_t *, ipf_nat_softc_t *, int);
233 static int ipf_nat_finalise(fr_info_t *, nat_t *);
234 static int ipf_nat_flushtable(ipf_main_softc_t *, ipf_nat_softc_t *);
235 static int ipf_nat_getnext(ipf_main_softc_t *, ipftoken_t *,
236 ipfgeniter_t *, ipfobj_t *);
237 static int ipf_nat_gettable(ipf_main_softc_t *, ipf_nat_softc_t *, char *);
238 static hostmap_t *ipf_nat_hostmap(ipf_nat_softc_t *, ipnat_t *,
239 struct in_addr, struct in_addr,
240 struct in_addr, u_32_t);
241 static int ipf_nat_icmpquerytype(int);
242 static int ipf_nat_iterator(ipf_main_softc_t *, ipftoken_t *,
243 ipfgeniter_t *, ipfobj_t *);
244 static int ipf_nat_match(fr_info_t *, ipnat_t *);
245 static int ipf_nat_matcharray(nat_t *, int *, u_long);
246 static int ipf_nat_matchflush(ipf_main_softc_t *, ipf_nat_softc_t *,
247 void *);
248 static void ipf_nat_mssclamp(tcphdr_t *, u_32_t, fr_info_t *, u_short *);
249 static int ipf_nat_newmap(fr_info_t *, nat_t *, natinfo_t *);
250 static int ipf_nat_newdivert(fr_info_t *, nat_t *, natinfo_t *);
251 static int ipf_nat_newrdr(fr_info_t *, nat_t *, natinfo_t *);
252 static int ipf_nat_newrewrite(fr_info_t *, nat_t *, natinfo_t *);
253 static int ipf_nat_nextaddr(fr_info_t *, nat_addr_t *, u_32_t *, u_32_t *);
254 static int ipf_nat_nextaddrinit(ipf_main_softc_t *, char *,
255 nat_addr_t *, int, void *);
256 static int ipf_nat_resolverule(ipf_main_softc_t *, ipnat_t *);
257 static int ipf_nat_ruleaddrinit(ipf_main_softc_t *,
258 ipf_nat_softc_t *, ipnat_t *);
259 static void ipf_nat_rule_fini(ipf_main_softc_t *, ipnat_t *);
260 static int ipf_nat_rule_init(ipf_main_softc_t *, ipf_nat_softc_t *,
261 ipnat_t *);
262 static int ipf_nat_siocaddnat(ipf_main_softc_t *, ipf_nat_softc_t *,
263 ipnat_t *, int);
264 static void ipf_nat_siocdelnat(ipf_main_softc_t *, ipf_nat_softc_t *,
265 ipnat_t *, int);
266 static void ipf_nat_tabmove(ipf_nat_softc_t *, nat_t *);
267
268 /* ------------------------------------------------------------------------ */
269 /* Function: ipf_nat_main_load */
270 /* Returns: int - 0 == success, -1 == failure */
271 /* Parameters: Nil */
272 /* */
273 /* The only global NAT structure that needs to be initialised is the filter */
274 /* rule that is used with blocking packets. */
275 /* ------------------------------------------------------------------------ */
276 int
277 ipf_nat_main_load(void)
278 {
279 bzero((char *)&ipfnatblock, sizeof(ipfnatblock));
280 ipfnatblock.fr_flags = FR_BLOCK|FR_QUICK;
281 ipfnatblock.fr_ref = 1;
282
283 return 0;
284 }
285
286
287 /* ------------------------------------------------------------------------ */
288 /* Function: ipf_nat_main_unload */
289 /* Returns: int - 0 == success, -1 == failure */
290 /* Parameters: Nil */
291 /* */
292 /* A null-op function that exists as a placeholder so that the flow in */
293 /* other functions is obvious. */
294 /* ------------------------------------------------------------------------ */
295 int
296 ipf_nat_main_unload(void)
297 {
298 return 0;
299 }
300
301
302 /* ------------------------------------------------------------------------ */
303 /* Function: ipf_nat_soft_create */
304 /* Returns: void * - NULL = failure, else pointer to NAT context */
305 /* Parameters: softc(I) - pointer to soft context main structure */
306 /* */
307 /* Allocate the initial soft context structure for NAT and populate it with */
308 /* some default values. Creating the tables is left until we call _init so */
309 /* that sizes can be changed before we get under way. */
310 /* ------------------------------------------------------------------------ */
311 void *
312 ipf_nat_soft_create(ipf_main_softc_t *softc)
313 {
314 ipf_nat_softc_t *softn;
315
316 KMALLOC(softn, ipf_nat_softc_t *);
317 if (softn == NULL)
318 return NULL;
319
320 bzero((char *)softn, sizeof(*softn));
321
322 softn->ipf_nat_tune = ipf_tune_array_copy(softn,
323 sizeof(ipf_nat_tuneables),
324 ipf_nat_tuneables);
325 if (softn->ipf_nat_tune == NULL) {
326 ipf_nat_soft_destroy(softc, softn);
327 return NULL;
328 }
329 if (ipf_tune_array_link(softc, softn->ipf_nat_tune) == -1) {
330 ipf_nat_soft_destroy(softc, softn);
331 return NULL;
332 }
333
334 softn->ipf_nat_list_tail = &softn->ipf_nat_list;
335
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;
342 #ifdef IPFILTER_LOG
343 softn->ipf_nat_logging = 1;
344 #else
345 softn->ipf_nat_logging = 0;
346 #endif
347
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;
353
354 return softn;
355 }
356
357 /* ------------------------------------------------------------------------ */
358 /* Function: ipf_nat_soft_destroy */
359 /* Returns: Nil */
360 /* Parameters: softc(I) - pointer to soft context main structure */
361 /* */
362 /* ------------------------------------------------------------------------ */
363 void
364 ipf_nat_soft_destroy(ipf_main_softc_t *softc, void *arg)
365 {
366 ipf_nat_softc_t *softn = arg;
367
368 if (softn->ipf_nat_tune != NULL) {
369 ipf_tune_array_unlink(softc, softn->ipf_nat_tune);
370 KFREES(softn->ipf_nat_tune, sizeof(ipf_nat_tuneables));
371 softn->ipf_nat_tune = NULL;
372 }
373
374 KFREE(softn);
375 }
376
377
378 /* ------------------------------------------------------------------------ */
379 /* Function: ipf_nat_init */
380 /* Returns: int - 0 == success, -1 == failure */
381 /* Parameters: softc(I) - pointer to soft context main structure */
382 /* */
383 /* Initialise all of the NAT locks, tables and other structures. */
384 /* ------------------------------------------------------------------------ */
385 int
386 ipf_nat_soft_init(ipf_main_softc_t *softc, void *arg)
387 {
388 ipf_nat_softc_t *softn = arg;
389 ipftq_t *tq;
390 int i;
391
392 KMALLOCS(softn->ipf_nat_table[0], nat_t **, \
393 sizeof(nat_t *) * softn->ipf_nat_table_sz);
394
395 if (softn->ipf_nat_table[0] != NULL) {
396 bzero((char *)softn->ipf_nat_table[0],
397 softn->ipf_nat_table_sz * sizeof(nat_t *));
398 } else {
399 return -1;
400 }
401
402 KMALLOCS(softn->ipf_nat_table[1], nat_t **, \
403 sizeof(nat_t *) * softn->ipf_nat_table_sz);
404
405 if (softn->ipf_nat_table[1] != NULL) {
406 bzero((char *)softn->ipf_nat_table[1],
407 softn->ipf_nat_table_sz * sizeof(nat_t *));
408 } else {
409 return -2;
410 }
411
412 KMALLOCS(softn->ipf_nat_map_rules, ipnat_t **, \
413 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
414
415 if (softn->ipf_nat_map_rules != NULL) {
416 bzero((char *)softn->ipf_nat_map_rules,
417 softn->ipf_nat_maprules_sz * sizeof(ipnat_t *));
418 } else {
419 return -3;
420 }
421
422 KMALLOCS(softn->ipf_nat_rdr_rules, ipnat_t **, \
423 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
424
425 if (softn->ipf_nat_rdr_rules != NULL) {
426 bzero((char *)softn->ipf_nat_rdr_rules,
427 softn->ipf_nat_rdrrules_sz * sizeof(ipnat_t *));
428 } else {
429 return -4;
430 }
431
432 KMALLOCS(softn->ipf_hm_maptable, hostmap_t **, \
433 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
434
435 if (softn->ipf_hm_maptable != NULL) {
436 bzero((char *)softn->ipf_hm_maptable,
437 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
438 } else {
439 return -5;
440 }
441 softn->ipf_hm_maplist = NULL;
442
443 KMALLOCS(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, u_int *,
444 softn->ipf_nat_table_sz * sizeof(u_int));
445
446 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen == NULL) {
447 return -6;
448 }
449 bzero((char *)softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
450 softn->ipf_nat_table_sz * sizeof(u_int));
451
452 KMALLOCS(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, u_int *,
453 softn->ipf_nat_table_sz * sizeof(u_int));
454
455 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen == NULL) {
456 return -7;
457 }
458
459 bzero((char *)softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
460 softn->ipf_nat_table_sz * sizeof(u_int));
461
462 if (softn->ipf_nat_maxbucket == 0) {
463 for (i = softn->ipf_nat_table_sz; i > 0; i >>= 1)
464 softn->ipf_nat_maxbucket++;
465 softn->ipf_nat_maxbucket *= 2;
466 }
467
468 ipf_sttab_init(softc, softn->ipf_nat_tcptq);
469 /*
470 * Increase this because we may have "keep state" following this too
471 * and packet storms can occur if this is removed too quickly.
472 */
473 softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
474 softn->ipf_nat_tcptq[IPF_TCP_NSTATES - 1].ifq_next =
475 &softn->ipf_nat_udptq;
476
477 IPFTQ_INIT(&softn->ipf_nat_udptq, softn->ipf_nat_defage,
478 "nat ipftq udp tab");
479 softn->ipf_nat_udptq.ifq_next = &softn->ipf_nat_udpacktq;
480
481 IPFTQ_INIT(&softn->ipf_nat_udpacktq, softn->ipf_nat_defage,
482 "nat ipftq udpack tab");
483 softn->ipf_nat_udpacktq.ifq_next = &softn->ipf_nat_icmptq;
484
485 IPFTQ_INIT(&softn->ipf_nat_icmptq, softn->ipf_nat_deficmpage,
486 "nat icmp ipftq tab");
487 softn->ipf_nat_icmptq.ifq_next = &softn->ipf_nat_icmpacktq;
488
489 IPFTQ_INIT(&softn->ipf_nat_icmpacktq, softn->ipf_nat_defage,
490 "nat icmpack ipftq tab");
491 softn->ipf_nat_icmpacktq.ifq_next = &softn->ipf_nat_iptq;
492
493 IPFTQ_INIT(&softn->ipf_nat_iptq, softn->ipf_nat_defipage,
494 "nat ip ipftq tab");
495 softn->ipf_nat_iptq.ifq_next = &softn->ipf_nat_pending;
496
497 IPFTQ_INIT(&softn->ipf_nat_pending, 1, "nat pending ipftq tab");
498 softn->ipf_nat_pending.ifq_next = NULL;
499
500 for (i = 0, tq = softn->ipf_nat_tcptq; i < IPF_TCP_NSTATES; i++, tq++) {
501 if (tq->ifq_ttl < softn->ipf_nat_deficmpage)
502 tq->ifq_ttl = softn->ipf_nat_deficmpage;
503 #ifdef LARGE_NAT
504 else if (tq->ifq_ttl > softn->ipf_nat_defage)
505 tq->ifq_ttl = softn->ipf_nat_defage;
506 #endif
507 }
508
509 /*
510 * Increase this because we may have "keep state" following
511 * this too and packet storms can occur if this is removed
512 * too quickly.
513 */
514 softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack;
515
516 MUTEX_INIT(&softn->ipf_nat_new, "ipf nat new mutex");
517 MUTEX_INIT(&softn->ipf_nat_io, "ipf nat io mutex");
518
519 softn->ipf_nat_inited = 1;
520
521 return 0;
522 }
523
524
525 /* ------------------------------------------------------------------------ */
526 /* Function: ipf_nat_soft_fini */
527 /* Returns: Nil */
528 /* Parameters: softc(I) - pointer to soft context main structure */
529 /* */
530 /* Free all memory used by NAT structures allocated at runtime. */
531 /* ------------------------------------------------------------------------ */
532 int
533 ipf_nat_soft_fini(ipf_main_softc_t *softc, void *arg)
534 {
535 ipf_nat_softc_t *softn = arg;
536 ipftq_t *ifq, *ifqnext;
537
538 (void) ipf_nat_clearlist(softc, softn);
539 (void) ipf_nat_flushtable(softc, softn);
540
541 /*
542 * Proxy timeout queues are not cleaned here because although they
543 * exist on the NAT list, ipf_proxy_unload is called after unload
544 * and the proxies actually are responsible for them being created.
545 * Should the proxy timeouts have their own list? There's no real
546 * justification as this is the only complication.
547 */
548 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
549 ifqnext = ifq->ifq_next;
550 if (ipf_deletetimeoutqueue(ifq) == 0)
551 ipf_freetimeoutqueue(softc, ifq);
552 }
553
554 if (softn->ipf_nat_table[0] != NULL) {
555 KFREES(softn->ipf_nat_table[0],
556 sizeof(nat_t *) * softn->ipf_nat_table_sz);
557 softn->ipf_nat_table[0] = NULL;
558 }
559 if (softn->ipf_nat_table[1] != NULL) {
560 KFREES(softn->ipf_nat_table[1],
561 sizeof(nat_t *) * softn->ipf_nat_table_sz);
562 softn->ipf_nat_table[1] = NULL;
563 }
564 if (softn->ipf_nat_map_rules != NULL) {
565 KFREES(softn->ipf_nat_map_rules,
566 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz);
567 softn->ipf_nat_map_rules = NULL;
568 }
569 if (softn->ipf_nat_rdr_rules != NULL) {
570 KFREES(softn->ipf_nat_rdr_rules,
571 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz);
572 softn->ipf_nat_rdr_rules = NULL;
573 }
574 if (softn->ipf_hm_maptable != NULL) {
575 KFREES(softn->ipf_hm_maptable,
576 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz);
577 softn->ipf_hm_maptable = NULL;
578 }
579 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
580 KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
581 sizeof(u_int) * softn->ipf_nat_table_sz);
582 softn->ipf_nat_stats.ns_side[0].ns_bucketlen = NULL;
583 }
584 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
585 KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
586 sizeof(u_int) * softn->ipf_nat_table_sz);
587 softn->ipf_nat_stats.ns_side[1].ns_bucketlen = NULL;
588 }
589
590 if (softn->ipf_nat_inited == 1) {
591 softn->ipf_nat_inited = 0;
592 ipf_sttab_destroy(softn->ipf_nat_tcptq);
593
594 MUTEX_DESTROY(&softn->ipf_nat_new);
595 MUTEX_DESTROY(&softn->ipf_nat_io);
596
597 MUTEX_DESTROY(&softn->ipf_nat_udptq.ifq_lock);
598 MUTEX_DESTROY(&softn->ipf_nat_udpacktq.ifq_lock);
599 MUTEX_DESTROY(&softn->ipf_nat_icmptq.ifq_lock);
600 MUTEX_DESTROY(&softn->ipf_nat_icmpacktq.ifq_lock);
601 MUTEX_DESTROY(&softn->ipf_nat_iptq.ifq_lock);
602 MUTEX_DESTROY(&softn->ipf_nat_pending.ifq_lock);
603 }
604
605 return 0;
606 }
607
608
609 /* ------------------------------------------------------------------------ */
610 /* Function: ipf_nat_setlock */
611 /* Returns: Nil */
612 /* Parameters: arg(I) - pointer to soft state information */
613 /* tmp(I) - new lock value */
614 /* */
615 /* Set the "lock status" of NAT to the value in tmp. */
616 /* ------------------------------------------------------------------------ */
617 void
618 ipf_nat_setlock(void *arg, int tmp)
619 {
620 ipf_nat_softc_t *softn = arg;
621
622 softn->ipf_nat_lock = tmp;
623 }
624
625
626 /* ------------------------------------------------------------------------ */
627 /* Function: ipf_nat_addrdr */
628 /* Returns: Nil */
629 /* Parameters: n(I) - pointer to NAT rule to add */
630 /* */
631 /* Adds a redirect rule to the hash table of redirect rules and the list of */
632 /* loaded NAT rules. Updates the bitmask indicating which netmasks are in */
633 /* use by redirect rules. */
634 /* ------------------------------------------------------------------------ */
635 static void
636 ipf_nat_addrdr(ipf_nat_softc_t *softn, ipnat_t *n)
637 {
638 ipnat_t **np;
639 u_32_t j;
640 u_int hv;
641 u_int rhv;
642 int k;
643
644 if (n->in_odstatype == FRI_NORMAL) {
645 k = count4bits(n->in_odstmsk);
646 ipf_inet_mask_add(k, &softn->ipf_nat_rdr_mask);
647 j = (n->in_odstaddr & n->in_odstmsk);
648 rhv = NAT_HASH_FN(j, 0, 0xffffffff);
649 } else {
650 ipf_inet_mask_add(0, &softn->ipf_nat_rdr_mask);
651 j = 0;
652 rhv = 0;
653 }
654 hv = rhv % softn->ipf_nat_rdrrules_sz;
655 np = softn->ipf_nat_rdr_rules + hv;
656 while (*np != NULL)
657 np = &(*np)->in_rnext;
658 n->in_rnext = NULL;
659 n->in_prnext = np;
660 n->in_hv[0] = hv;
661 n->in_use++;
662 *np = n;
663 }
664
665
666 /* ------------------------------------------------------------------------ */
667 /* Function: ipf_nat_addmap */
668 /* Returns: Nil */
669 /* Parameters: n(I) - pointer to NAT rule to add */
670 /* */
671 /* Adds a NAT map rule to the hash table of rules and the list of loaded */
672 /* NAT rules. Updates the bitmask indicating which netmasks are in use by */
673 /* redirect rules. */
674 /* ------------------------------------------------------------------------ */
675 static void
676 ipf_nat_addmap(ipf_nat_softc_t *softn, ipnat_t *n)
677 {
678 ipnat_t **np;
679 u_32_t j;
680 u_int hv;
681 u_int rhv;
682 int k;
683
684 if (n->in_osrcatype == FRI_NORMAL) {
685 k = count4bits(n->in_osrcmsk);
686 ipf_inet_mask_add(k, &softn->ipf_nat_map_mask);
687 j = (n->in_osrcaddr & n->in_osrcmsk);
688 rhv = NAT_HASH_FN(j, 0, 0xffffffff);
689 } else {
690 ipf_inet_mask_add(0, &softn->ipf_nat_map_mask);
691 j = 0;
692 rhv = 0;
693 }
694 hv = rhv % softn->ipf_nat_maprules_sz;
695 np = softn->ipf_nat_map_rules + hv;
696 while (*np != NULL)
697 np = &(*np)->in_mnext;
698 n->in_mnext = NULL;
699 n->in_pmnext = np;
700 n->in_hv[1] = rhv;
701 n->in_use++;
702 *np = n;
703 }
704
705
706 /* ------------------------------------------------------------------------ */
707 /* Function: ipf_nat_delrdr */
708 /* Returns: Nil */
709 /* Parameters: n(I) - pointer to NAT rule to delete */
710 /* */
711 /* Removes a redirect rule from the hash table of redirect rules. */
712 /* ------------------------------------------------------------------------ */
713 void
714 ipf_nat_delrdr(ipf_nat_softc_t *softn, ipnat_t *n)
715 {
716 if (n->in_odstatype == FRI_NORMAL) {
717 int k = count4bits(n->in_odstmsk);
718 ipf_inet_mask_del(k, &softn->ipf_nat_rdr_mask);
719 } else {
720 ipf_inet_mask_del(0, &softn->ipf_nat_rdr_mask);
721 }
722 if (n->in_rnext)
723 n->in_rnext->in_prnext = n->in_prnext;
724 *n->in_prnext = n->in_rnext;
725 n->in_use--;
726 }
727
728
729 /* ------------------------------------------------------------------------ */
730 /* Function: ipf_nat_delmap */
731 /* Returns: Nil */
732 /* Parameters: n(I) - pointer to NAT rule to delete */
733 /* */
734 /* Removes a NAT map rule from the hash table of NAT map rules. */
735 /* ------------------------------------------------------------------------ */
736 void
737 ipf_nat_delmap(ipf_nat_softc_t *softn, ipnat_t *n)
738 {
739 if (n->in_osrcatype == FRI_NORMAL) {
740 int k = count4bits(n->in_osrcmsk);
741 ipf_inet_mask_del(k, &softn->ipf_nat_map_mask);
742 } else {
743 ipf_inet_mask_del(0, &softn->ipf_nat_map_mask);
744 }
745 if (n->in_mnext != NULL)
746 n->in_mnext->in_pmnext = n->in_pmnext;
747 *n->in_pmnext = n->in_mnext;
748 n->in_use--;
749 }
750
751
752 /* ------------------------------------------------------------------------ */
753 /* Function: ipf_nat_hostmap */
754 /* Returns: struct hostmap* - NULL if no hostmap could be created, */
755 /* else a pointer to the hostmapping to use */
756 /* Parameters: np(I) - pointer to NAT rule */
757 /* real(I) - real IP address */
758 /* map(I) - mapped IP address */
759 /* port(I) - destination port number */
760 /* Write Locks: ipf_nat */
761 /* */
762 /* Check if an ip address has already been allocated for a given mapping */
763 /* that is not doing port based translation. If is not yet allocated, then */
764 /* create a new entry if a non-NULL NAT rule pointer has been supplied. */
765 /* ------------------------------------------------------------------------ */
766 static struct hostmap *
767 ipf_nat_hostmap(ipf_nat_softc_t *softn, ipnat_t *np, struct in_addr src,
768 struct in_addr dst, struct in_addr map, u_32_t port)
769 {
770 hostmap_t *hm;
771 u_int hv, rhv;
772
773 hv = (src.s_addr ^ dst.s_addr);
774 hv += src.s_addr;
775 hv += dst.s_addr;
776 rhv = hv;
777 hv %= softn->ipf_nat_hostmap_sz;
778 for (hm = softn->ipf_hm_maptable[hv]; hm; hm = hm->hm_hnext)
779 if ((hm->hm_osrcip.s_addr == src.s_addr) &&
780 (hm->hm_odstip.s_addr == dst.s_addr) &&
781 ((np == NULL) || (np == hm->hm_ipnat)) &&
782 ((port == 0) || (port == hm->hm_port))) {
783 softn->ipf_nat_stats.ns_hm_addref++;
784 hm->hm_ref++;
785 return hm;
786 }
787
788 if (np == NULL) {
789 softn->ipf_nat_stats.ns_hm_nullnp++;
790 return NULL;
791 }
792
793 KMALLOC(hm, hostmap_t *);
794 if (hm) {
795 hm->hm_next = softn->ipf_hm_maplist;
796 hm->hm_pnext = &softn->ipf_hm_maplist;
797 if (softn->ipf_hm_maplist != NULL)
798 softn->ipf_hm_maplist->hm_pnext = &hm->hm_next;
799 softn->ipf_hm_maplist = hm;
800 hm->hm_hnext = softn->ipf_hm_maptable[hv];
801 hm->hm_phnext = softn->ipf_hm_maptable + hv;
802 if (softn->ipf_hm_maptable[hv] != NULL)
803 softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
804 softn->ipf_hm_maptable[hv] = hm;
805 hm->hm_ipnat = np;
806 np->in_use++;
807 hm->hm_osrcip = src;
808 hm->hm_odstip = dst;
809 hm->hm_nsrcip = map;
810 hm->hm_ndstip.s_addr = 0;
811 hm->hm_ref = 1;
812 hm->hm_port = port;
813 hm->hm_hv = rhv;
814 hm->hm_v = 4;
815 softn->ipf_nat_stats.ns_hm_new++;
816 } else {
817 softn->ipf_nat_stats.ns_hm_newfail++;
818 }
819 return hm;
820 }
821
822
823 /* ------------------------------------------------------------------------ */
824 /* Function: ipf_nat_hostmapdel */
825 /* Returns: Nil */
826 /* Parameters: hmp(I) - pointer to hostmap structure pointer */
827 /* Write Locks: ipf_nat */
828 /* */
829 /* Decrement the references to this hostmap structure by one. If this */
830 /* reaches zero then remove it and free it. */
831 /* ------------------------------------------------------------------------ */
832 void
833 ipf_nat_hostmapdel(ipf_main_softc_t *softc, struct hostmap **hmp)
834 {
835 struct hostmap *hm;
836
837 hm = *hmp;
838 *hmp = NULL;
839
840 hm->hm_ref--;
841 if (hm->hm_ref == 0) {
842 ipf_nat_rule_deref(softc, &hm->hm_ipnat);
843 if (hm->hm_hnext)
844 hm->hm_hnext->hm_phnext = hm->hm_phnext;
845 *hm->hm_phnext = hm->hm_hnext;
846 if (hm->hm_next)
847 hm->hm_next->hm_pnext = hm->hm_pnext;
848 *hm->hm_pnext = hm->hm_next;
849 KFREE(hm);
850 }
851 }
852
853
854 /* ------------------------------------------------------------------------ */
855 /* Function: ipf_fix_outcksum */
856 /* Returns: Nil */
857 /* Parameters: fin(I) - pointer to packet information */
858 /* sp(I) - location of 16bit checksum to update */
859 /* n((I) - amount to adjust checksum by */
860 /* */
861 /* Adjusts the 16bit checksum by "n" for packets going out. */
862 /* ------------------------------------------------------------------------ */
863 void
864 ipf_fix_outcksum(int cksum, u_short *sp, u_32_t n, u_32_t partial)
865 {
866 u_short sumshort;
867 u_32_t sum1;
868
869 if (n == 0)
870 return;
871
872 if (cksum == 4) {
873 *sp = 0;
874 return;
875 }
876 if (cksum == 2) {
877 sum1 = partial;
878 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
879 *sp = htons(sum1);
880 return;
881 }
882 sum1 = (~ntohs(*sp)) & 0xffff;
883 sum1 += (n);
884 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
885 /* Again */
886 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
887 sumshort = ~(u_short)sum1;
888 *(sp) = htons(sumshort);
889 }
890
891
892 /* ------------------------------------------------------------------------ */
893 /* Function: ipf_fix_incksum */
894 /* Returns: Nil */
895 /* Parameters: fin(I) - pointer to packet information */
896 /* sp(I) - location of 16bit checksum to update */
897 /* n((I) - amount to adjust checksum by */
898 /* */
899 /* Adjusts the 16bit checksum by "n" for packets going in. */
900 /* ------------------------------------------------------------------------ */
901 void
902 ipf_fix_incksum(int cksum, u_short *sp, u_32_t n, u_32_t partial)
903 {
904 u_short sumshort;
905 u_32_t sum1;
906
907 if (n == 0)
908 return;
909
910 if (cksum == 4) {
911 *sp = 0;
912 return;
913 }
914 if (cksum == 2) {
915 sum1 = partial;
916 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
917 *sp = htons(sum1);
918 return;
919 }
920
921 sum1 = (~ntohs(*sp)) & 0xffff;
922 sum1 += ~(n) & 0xffff;
923 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
924 /* Again */
925 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
926 sumshort = ~(u_short)sum1;
927 *(sp) = htons(sumshort);
928 }
929
930
931 /* ------------------------------------------------------------------------ */
932 /* Function: ipf_fix_datacksum */
933 /* Returns: Nil */
934 /* Parameters: sp(I) - location of 16bit checksum to update */
935 /* n((I) - amount to adjust checksum by */
936 /* */
937 /* Fix_datacksum is used *only* for the adjustments of checksums in the */
938 /* data section of an IP packet. */
939 /* */
940 /* The only situation in which you need to do this is when NAT'ing an */
941 /* ICMP error message. Such a message, contains in its body the IP header */
942 /* of the original IP packet, that causes the error. */
943 /* */
944 /* You can't use fix_incksum or fix_outcksum in that case, because for the */
945 /* kernel the data section of the ICMP error is just data, and no special */
946 /* processing like hardware cksum or ntohs processing have been done by the */
947 /* kernel on the data section. */
948 /* ------------------------------------------------------------------------ */
949 void
950 ipf_fix_datacksum(u_short *sp, u_32_t n)
951 {
952 u_short sumshort;
953 u_32_t sum1;
954
955 if (n == 0)
956 return;
957
958 sum1 = (~ntohs(*sp)) & 0xffff;
959 sum1 += (n);
960 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
961 /* Again */
962 sum1 = (sum1 >> 16) + (sum1 & 0xffff);
963 sumshort = ~(u_short)sum1;
964 *(sp) = htons(sumshort);
965 }
966
967
968 /* ------------------------------------------------------------------------ */
969 /* Function: ipf_nat_ioctl */
970 /* Returns: int - 0 == success, != 0 == failure */
971 /* Parameters: softc(I) - pointer to soft context main structure */
972 /* data(I) - pointer to ioctl data */
973 /* cmd(I) - ioctl command integer */
974 /* mode(I) - file mode bits used with open */
975 /* uid(I) - uid of calling process */
976 /* ctx(I) - pointer used as key for finding context */
977 /* */
978 /* Processes an ioctl call made to operate on the IP Filter NAT device. */
979 /* ------------------------------------------------------------------------ */
980 int
981 ipf_nat_ioctl(ipf_main_softc_t *softc, void *data, ioctlcmd_t cmd, int mode,
982 int uid, void *ctx)
983 {
984 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
985 int error = 0, ret, arg, getlock;
986 ipnat_t *nat, *nt, *n;
987 ipnat_t natd;
988 SPL_INT(s);
989
990 #if BSD_GE_YEAR(199306) && defined(_KERNEL)
991 # if NETBSD_GE_REV(399002000)
992 if ((mode & FWRITE) &&
993 kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_FIREWALL,
994 KAUTH_REQ_NETWORK_FIREWALL_FW,
995 NULL, NULL, NULL))
996 # else
997 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 500034)
998 if (securelevel_ge(curthread->td_ucred, 3) && (mode & FWRITE))
999 # else
1000 if ((securelevel >= 3) && (mode & FWRITE))
1001 # endif
1002 # endif
1003 {
1004 IPFERROR(60001);
1005 return EPERM;
1006 }
1007 #endif
1008
1009 #if defined(__osf__) && defined(_KERNEL)
1010 getlock = 0;
1011 #else
1012 getlock = (mode & NAT_LOCKHELD) ? 0 : 1;
1013 #endif
1014
1015 n = NULL;
1016 nt = NULL;
1017 nat = NULL;
1018
1019 if ((cmd == (ioctlcmd_t)SIOCADNAT) || (cmd == (ioctlcmd_t)SIOCRMNAT) ||
1020 (cmd == (ioctlcmd_t)SIOCPURGENAT)) {
1021 if (mode & NAT_SYSSPACE) {
1022 bcopy(data, (char *)&natd, sizeof(natd));
1023 nat = &natd;
1024 error = 0;
1025 } else {
1026 bzero(&natd, sizeof(natd));
1027 error = ipf_inobj(softc, data, NULL, &natd,
1028 IPFOBJ_IPNAT);
1029 if (error != 0)
1030 goto done;
1031
1032 if (natd.in_size < sizeof(ipnat_t)) {
1033 error = EINVAL;
1034 goto done;
1035 }
1036 KMALLOCS(nt, ipnat_t *, natd.in_size);
1037 if (nt == NULL) {
1038 IPFERROR(60070);
1039 error = ENOMEM;
1040 goto done;
1041 }
1042 bzero(nt, natd.in_size);
1043 error = ipf_inobjsz(softc, data, nt, IPFOBJ_IPNAT,
1044 natd.in_size);
1045 if (error)
1046 goto done;
1047 nat = nt;
1048 }
1049
1050 /*
1051 * For add/delete, look to see if the NAT entry is
1052 * already present
1053 */
1054 nat->in_flags &= IPN_USERFLAGS;
1055 if ((nat->in_redir & NAT_MAPBLK) == 0) {
1056 if (nat->in_osrcatype == FRI_NORMAL ||
1057 nat->in_osrcatype == FRI_NONE)
1058 nat->in_osrcaddr &= nat->in_osrcmsk;
1059 if (nat->in_odstatype == FRI_NORMAL ||
1060 nat->in_odstatype == FRI_NONE)
1061 nat->in_odstaddr &= nat->in_odstmsk;
1062 if ((nat->in_flags & (IPN_SPLIT|IPN_SIPRANGE)) == 0) {
1063 if (nat->in_nsrcatype == FRI_NORMAL)
1064 nat->in_nsrcaddr &= nat->in_nsrcmsk;
1065 if (nat->in_ndstatype == FRI_NORMAL)
1066 nat->in_ndstaddr &= nat->in_ndstmsk;
1067 }
1068 }
1069
1070 error = ipf_nat_rule_init(softc, softn, nat);
1071 if (error != 0)
1072 goto done;
1073
1074 MUTEX_ENTER(&softn->ipf_nat_io);
1075 for (n = softn->ipf_nat_list; n != NULL; n = n->in_next)
1076 if (ipf_nat_cmp_rules(nat, n) == 0)
1077 break;
1078 }
1079
1080 switch (cmd)
1081 {
1082 #ifdef IPFILTER_LOG
1083 case SIOCIPFFB :
1084 {
1085 int tmp;
1086
1087 if (!(mode & FWRITE)) {
1088 IPFERROR(60002);
1089 error = EPERM;
1090 } else {
1091 tmp = ipf_log_clear(softc, IPL_LOGNAT);
1092 error = BCOPYOUT(&tmp, data, sizeof(tmp));
1093 if (error != 0) {
1094 IPFERROR(60057);
1095 error = EFAULT;
1096 }
1097 }
1098 break;
1099 }
1100
1101 case SIOCSETLG :
1102 if (!(mode & FWRITE)) {
1103 IPFERROR(60003);
1104 error = EPERM;
1105 } else {
1106 error = BCOPYIN(data, &softn->ipf_nat_logging,
1107 sizeof(softn->ipf_nat_logging));
1108 if (error != 0)
1109 error = EFAULT;
1110 }
1111 break;
1112
1113 case SIOCGETLG :
1114 error = BCOPYOUT(&softn->ipf_nat_logging, data,
1115 sizeof(softn->ipf_nat_logging));
1116 if (error != 0) {
1117 IPFERROR(60004);
1118 error = EFAULT;
1119 }
1120 break;
1121
1122 case FIONREAD :
1123 arg = ipf_log_bytesused(softc, IPL_LOGNAT);
1124 error = BCOPYOUT(&arg, data, sizeof(arg));
1125 if (error != 0) {
1126 IPFERROR(60005);
1127 error = EFAULT;
1128 }
1129 break;
1130 #endif
1131 case SIOCADNAT :
1132 if (!(mode & FWRITE)) {
1133 IPFERROR(60006);
1134 error = EPERM;
1135 } else if (n != NULL) {
1136 natd.in_flineno = n->in_flineno;
1137 (void) ipf_outobj(softc, data, &natd, IPFOBJ_IPNAT);
1138 IPFERROR(60007);
1139 error = EEXIST;
1140 } else if (nt == NULL) {
1141 IPFERROR(60008);
1142 error = ENOMEM;
1143 }
1144 if (error != 0) {
1145 MUTEX_EXIT(&softn->ipf_nat_io);
1146 break;
1147 }
1148 if (nat != nt)
1149 bcopy((char *)nat, (char *)nt, sizeof(*n));
1150 error = ipf_nat_siocaddnat(softc, softn, nt, getlock);
1151 MUTEX_EXIT(&softn->ipf_nat_io);
1152 if (error == 0) {
1153 nat = NULL;
1154 nt = NULL;
1155 }
1156 break;
1157
1158 case SIOCRMNAT :
1159 case SIOCPURGENAT :
1160 if (!(mode & FWRITE)) {
1161 IPFERROR(60009);
1162 error = EPERM;
1163 n = NULL;
1164 } else if (n == NULL) {
1165 IPFERROR(60010);
1166 error = ESRCH;
1167 }
1168
1169 if (error != 0) {
1170 MUTEX_EXIT(&softn->ipf_nat_io);
1171 break;
1172 }
1173 if (cmd == (ioctlcmd_t)SIOCPURGENAT) {
1174 error = ipf_outobjsz(softc, data, n, IPFOBJ_IPNAT,
1175 n->in_size);
1176 if (error) {
1177 MUTEX_EXIT(&softn->ipf_nat_io);
1178 goto done;
1179 }
1180 n->in_flags |= IPN_PURGE;
1181 }
1182 ipf_nat_siocdelnat(softc, softn, n, getlock);
1183
1184 MUTEX_EXIT(&softn->ipf_nat_io);
1185 n = NULL;
1186 break;
1187
1188 case SIOCGNATS :
1189 {
1190 natstat_t *nsp = &softn->ipf_nat_stats;
1191
1192 nsp->ns_side[0].ns_table = softn->ipf_nat_table[0];
1193 nsp->ns_side[1].ns_table = softn->ipf_nat_table[1];
1194 nsp->ns_list = softn->ipf_nat_list;
1195 nsp->ns_maptable = softn->ipf_hm_maptable;
1196 nsp->ns_maplist = softn->ipf_hm_maplist;
1197 nsp->ns_nattab_sz = softn->ipf_nat_table_sz;
1198 nsp->ns_nattab_max = softn->ipf_nat_table_max;
1199 nsp->ns_rultab_sz = softn->ipf_nat_maprules_sz;
1200 nsp->ns_rdrtab_sz = softn->ipf_nat_rdrrules_sz;
1201 nsp->ns_hostmap_sz = softn->ipf_nat_hostmap_sz;
1202 nsp->ns_instances = softn->ipf_nat_instances;
1203 nsp->ns_ticks = softc->ipf_ticks;
1204 #ifdef IPFILTER_LOGGING
1205 nsp->ns_log_ok = ipf_log_logok(softc, IPF_LOGNAT);
1206 nsp->ns_log_fail = ipf_log_failures(softc, IPF_LOGNAT);
1207 #else
1208 nsp->ns_log_ok = 0;
1209 nsp->ns_log_fail = 0;
1210 #endif
1211 error = ipf_outobj(softc, data, nsp, IPFOBJ_NATSTAT);
1212 break;
1213 }
1214
1215 case SIOCGNATL :
1216 {
1217 natlookup_t nl;
1218
1219 error = ipf_inobj(softc, data, NULL, &nl, IPFOBJ_NATLOOKUP);
1220 if (error == 0) {
1221 void *ptr;
1222
1223 if (getlock) {
1224 READ_ENTER(&softc->ipf_nat);
1225 }
1226
1227 switch (nl.nl_v)
1228 {
1229 case 4 :
1230 ptr = ipf_nat_lookupredir(softc, &nl);
1231 break;
1232 #ifdef USE_INET6
1233 case 6 :
1234 ptr = ipf_nat6_lookupredir(softc, &nl);
1235 break;
1236 #endif
1237 default:
1238 ptr = NULL;
1239 break;
1240 }
1241
1242 if (getlock) {
1243 RWLOCK_EXIT(&softc->ipf_nat);
1244 }
1245 if (ptr != NULL) {
1246 error = ipf_outobj(softc, data, &nl,
1247 IPFOBJ_NATLOOKUP);
1248 } else {
1249 IPFERROR(60011);
1250 error = ESRCH;
1251 }
1252 }
1253 break;
1254 }
1255
1256 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */
1257 if (!(mode & FWRITE)) {
1258 IPFERROR(60012);
1259 error = EPERM;
1260 break;
1261 }
1262 if (getlock) {
1263 WRITE_ENTER(&softc->ipf_nat);
1264 }
1265
1266 error = BCOPYIN(data, &arg, sizeof(arg));
1267 if (error != 0) {
1268 IPFERROR(60013);
1269 error = EFAULT;
1270 } else {
1271 if (arg == 0)
1272 ret = ipf_nat_flushtable(softc, softn);
1273 else if (arg == 1)
1274 ret = ipf_nat_clearlist(softc, softn);
1275 else
1276 ret = ipf_nat_extraflush(softc, softn, arg);
1277 ipf_proxy_flush(softc->ipf_proxy_soft, arg);
1278 }
1279
1280 if (getlock) {
1281 RWLOCK_EXIT(&softc->ipf_nat);
1282 }
1283 if (error == 0) {
1284 error = BCOPYOUT(&ret, data, sizeof(ret));
1285 }
1286 break;
1287
1288 case SIOCMATCHFLUSH :
1289 if (!(mode & FWRITE)) {
1290 IPFERROR(60014);
1291 error = EPERM;
1292 break;
1293 }
1294 if (getlock) {
1295 WRITE_ENTER(&softc->ipf_nat);
1296 }
1297
1298 error = ipf_nat_matchflush(softc, softn, data);
1299
1300 if (getlock) {
1301 RWLOCK_EXIT(&softc->ipf_nat);
1302 }
1303 break;
1304
1305 case SIOCPROXY :
1306 error = ipf_proxy_ioctl(softc, data, cmd, mode, ctx);
1307 break;
1308
1309 case SIOCSTLCK :
1310 if (!(mode & FWRITE)) {
1311 IPFERROR(60015);
1312 error = EPERM;
1313 } else {
1314 error = ipf_lock(data, &softn->ipf_nat_lock);
1315 }
1316 break;
1317
1318 case SIOCSTPUT :
1319 if ((mode & FWRITE) != 0) {
1320 error = ipf_nat_putent(softc, data, getlock);
1321 } else {
1322 IPFERROR(60016);
1323 error = EACCES;
1324 }
1325 break;
1326
1327 case SIOCSTGSZ :
1328 if (softn->ipf_nat_lock) {
1329 error = ipf_nat_getsz(softc, data, getlock);
1330 } else {
1331 IPFERROR(60017);
1332 error = EACCES;
1333 }
1334 break;
1335
1336 case SIOCSTGET :
1337 if (softn->ipf_nat_lock) {
1338 error = ipf_nat_getent(softc, data, getlock);
1339 } else {
1340 IPFERROR(60018);
1341 error = EACCES;
1342 }
1343 break;
1344
1345 case SIOCGENITER :
1346 {
1347 ipfgeniter_t iter;
1348 ipftoken_t *token;
1349 ipfobj_t obj;
1350
1351 error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER);
1352 if (error != 0)
1353 break;
1354
1355 SPL_SCHED(s);
1356 token = ipf_token_find(softc, iter.igi_type, uid, ctx);
1357 if (token != NULL) {
1358 error = ipf_nat_iterator(softc, token, &iter, &obj);
1359 WRITE_ENTER(&softc->ipf_tokens);
1360 ipf_token_deref(softc, token);
1361 RWLOCK_EXIT(&softc->ipf_tokens);
1362 }
1363 SPL_X(s);
1364 break;
1365 }
1366
1367 case SIOCIPFDELTOK :
1368 error = BCOPYIN(data, &arg, sizeof(arg));
1369 if (error == 0) {
1370 SPL_SCHED(s);
1371 error = ipf_token_del(softc, arg, uid, ctx);
1372 SPL_X(s);
1373 } else {
1374 IPFERROR(60019);
1375 error = EFAULT;
1376 }
1377 break;
1378
1379 case SIOCGTQTAB :
1380 error = ipf_outobj(softc, data, softn->ipf_nat_tcptq,
1381 IPFOBJ_STATETQTAB);
1382 break;
1383
1384 case SIOCGTABL :
1385 error = ipf_nat_gettable(softc, softn, data);
1386 break;
1387
1388 default :
1389 IPFERROR(60020);
1390 error = EINVAL;
1391 break;
1392 }
1393 done:
1394 if (nat != NULL)
1395 ipf_nat_rule_fini(softc, nat);
1396 if (nt != NULL)
1397 KFREES(nt, nt->in_size);
1398 return error;
1399 }
1400
1401
1402 /* ------------------------------------------------------------------------ */
1403 /* Function: ipf_nat_siocaddnat */
1404 /* Returns: int - 0 == success, != 0 == failure */
1405 /* Parameters: softc(I) - pointer to soft context main structure */
1406 /* softn(I) - pointer to NAT context structure */
1407 /* n(I) - pointer to new NAT rule */
1408 /* np(I) - pointer to where to insert new NAT rule */
1409 /* getlock(I) - flag indicating if lock on is held */
1410 /* Mutex Locks: ipf_nat_io */
1411 /* */
1412 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */
1413 /* from information passed to the kernel, then add it to the appropriate */
1414 /* NAT rule table(s). */
1415 /* ------------------------------------------------------------------------ */
1416 static int
1417 ipf_nat_siocaddnat(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n,
1418 int getlock)
1419 {
1420 int error = 0;
1421
1422 if (ipf_nat_resolverule(softc, n) != 0) {
1423 IPFERROR(60022);
1424 return ENOENT;
1425 }
1426
1427 if ((n->in_age[0] == 0) && (n->in_age[1] != 0)) {
1428 IPFERROR(60023);
1429 return EINVAL;
1430 }
1431
1432 if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) {
1433 /*
1434 * Prerecord whether or not the destination of the divert
1435 * is local or not to the interface the packet is going
1436 * to be sent out.
1437 */
1438 n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1],
1439 n->in_ifps[1], &n->in_ndstip6);
1440 }
1441
1442 if (getlock) {
1443 WRITE_ENTER(&softc->ipf_nat);
1444 }
1445 n->in_next = NULL;
1446 n->in_pnext = softn->ipf_nat_list_tail;
1447 *n->in_pnext = n;
1448 softn->ipf_nat_list_tail = &n->in_next;
1449 n->in_use++;
1450
1451 if (n->in_redir & NAT_REDIRECT) {
1452 n->in_flags &= ~IPN_NOTDST;
1453 switch (n->in_v[0])
1454 {
1455 case 4 :
1456 ipf_nat_addrdr(softn, n);
1457 break;
1458 #ifdef USE_INET6
1459 case 6 :
1460 ipf_nat6_addrdr(softn, n);
1461 break;
1462 #endif
1463 default :
1464 break;
1465 }
1466 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_rdr);
1467 }
1468
1469 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
1470 n->in_flags &= ~IPN_NOTSRC;
1471 switch (n->in_v[0])
1472 {
1473 case 4 :
1474 ipf_nat_addmap(softn, n);
1475 break;
1476 #ifdef USE_INET6
1477 case 6 :
1478 ipf_nat6_addmap(softn, n);
1479 break;
1480 #endif
1481 default :
1482 break;
1483 }
1484 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_map);
1485 }
1486
1487 if (n->in_age[0] != 0)
1488 n->in_tqehead[0] = ipf_addtimeoutqueue(softc,
1489 &softn->ipf_nat_utqe,
1490 n->in_age[0]);
1491
1492 if (n->in_age[1] != 0)
1493 n->in_tqehead[1] = ipf_addtimeoutqueue(softc,
1494 &softn->ipf_nat_utqe,
1495 n->in_age[1]);
1496
1497 MUTEX_INIT(&n->in_lock, "ipnat rule lock");
1498
1499 n = NULL;
1500 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
1501 #if SOLARIS && !defined(INSTANCES)
1502 pfil_delayed_copy = 0;
1503 #endif
1504 if (getlock) {
1505 RWLOCK_EXIT(&softc->ipf_nat); /* WRITE */
1506 }
1507
1508 return error;
1509 }
1510
1511
1512 /* ------------------------------------------------------------------------ */
1513 /* Function: ipf_nat_ruleaddrinit */
1514 /* Parameters: softc(I) - pointer to soft context main structure */
1515 /* softn(I) - pointer to NAT context structure */
1516 /* n(I) - pointer to NAT rule */
1517 /* */
1518 /* Initialise all of the NAT address structures in a NAT rule. */
1519 /* ------------------------------------------------------------------------ */
1520 static int
1521 ipf_nat_ruleaddrinit(ipf_main_softc_t *softc, ipf_nat_softc_t *softn,
1522 ipnat_t *n)
1523 {
1524 int idx, error;
1525
1526 if ((n->in_ndst.na_atype == FRI_LOOKUP) &&
1527 (n->in_ndst.na_type != IPLT_DSTLIST)) {
1528 IPFERROR(60071);
1529 return EINVAL;
1530 }
1531 if ((n->in_nsrc.na_atype == FRI_LOOKUP) &&
1532 (n->in_nsrc.na_type != IPLT_DSTLIST)) {
1533 IPFERROR(60069);
1534 return EINVAL;
1535 }
1536
1537 if (n->in_redir == NAT_BIMAP) {
1538 n->in_ndstaddr = n->in_osrcaddr;
1539 n->in_ndstmsk = n->in_osrcmsk;
1540 n->in_odstaddr = n->in_nsrcaddr;
1541 n->in_odstmsk = n->in_nsrcmsk;
1542
1543 }
1544
1545 if (n->in_redir & NAT_REDIRECT)
1546 idx = 1;
1547 else
1548 idx = 0;
1549 /*
1550 * Initialise all of the address fields.
1551 */
1552 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc, 1,
1553 n->in_ifps[idx]);
1554 if (error != 0)
1555 return error;
1556
1557 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst, 1,
1558 n->in_ifps[idx]);
1559 if (error != 0)
1560 return error;
1561
1562 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc, 1,
1563 n->in_ifps[idx]);
1564 if (error != 0)
1565 return error;
1566
1567 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst, 1,
1568 n->in_ifps[idx]);
1569 if (error != 0)
1570 return error;
1571
1572 if (n->in_redir & NAT_DIVERTUDP)
1573 ipf_nat_builddivertmp(softn, n);
1574
1575 return 0;
1576 }
1577
1578
1579 /* ------------------------------------------------------------------------ */
1580 /* Function: ipf_nat_resolvrule */
1581 /* Returns: Nil */
1582 /* Parameters: softc(I) - pointer to soft context main structure */
1583 /* n(I) - pointer to NAT rule */
1584 /* */
1585 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */
1586 /* from information passed to the kernel, then add it to the appropriate */
1587 /* NAT rule table(s). */
1588 /* ------------------------------------------------------------------------ */
1589 static int
1590 ipf_nat_resolverule(ipf_main_softc_t *softc, ipnat_t *n)
1591 {
1592 char *base;
1593
1594 base = n->in_names;
1595
1596 n->in_ifps[0] = ipf_resolvenic(softc, base + n->in_ifnames[0],
1597 n->in_v[0]);
1598
1599 if (n->in_ifnames[1] == -1) {
1600 n->in_ifnames[1] = n->in_ifnames[0];
1601 n->in_ifps[1] = n->in_ifps[0];
1602 } else {
1603 n->in_ifps[1] = ipf_resolvenic(softc, base + n->in_ifnames[1],
1604 n->in_v[1]);
1605 }
1606
1607 if (n->in_plabel != -1) {
1608 if (n->in_redir & NAT_REDIRECT)
1609 n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
1610 n->in_pr[0],
1611 base + n->in_plabel);
1612 else
1613 n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft,
1614 n->in_pr[1],
1615 base + n->in_plabel);
1616 if (n->in_apr == NULL)
1617 return -1;
1618 }
1619 return 0;
1620 }
1621
1622
1623 /* ------------------------------------------------------------------------ */
1624 /* Function: ipf_nat_siocdelnat */
1625 /* Returns: int - 0 == success, != 0 == failure */
1626 /* Parameters: softc(I) - pointer to soft context main structure */
1627 /* softn(I) - pointer to NAT context structure */
1628 /* n(I) - pointer to new NAT rule */
1629 /* getlock(I) - flag indicating if lock on is held */
1630 /* Mutex Locks: ipf_nat_io */
1631 /* */
1632 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */
1633 /* from information passed to the kernel, then add it to the appropriate */
1634 /* NAT rule table(s). */
1635 /* ------------------------------------------------------------------------ */
1636 static void
1637 ipf_nat_siocdelnat(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n,
1638 int getlock)
1639 {
1640 #ifdef IPF_NAT6
1641 int i;
1642 #endif
1643
1644 if (getlock) {
1645 WRITE_ENTER(&softc->ipf_nat);
1646 }
1647
1648 ipf_nat_delrule(softc, softn, n, 1);
1649
1650 if (getlock) {
1651 RWLOCK_EXIT(&softc->ipf_nat); /* READ/WRITE */
1652 }
1653 }
1654
1655
1656 /* ------------------------------------------------------------------------ */
1657 /* Function: ipf_nat_getsz */
1658 /* Returns: int - 0 == success, != 0 is the error value. */
1659 /* Parameters: softc(I) - pointer to soft context main structure */
1660 /* data(I) - pointer to natget structure with kernel */
1661 /* pointer get the size of. */
1662 /* getlock(I) - flag indicating whether or not the caller */
1663 /* holds a lock on ipf_nat */
1664 /* */
1665 /* Handle SIOCSTGSZ. */
1666 /* Return the size of the nat list entry to be copied back to user space. */
1667 /* The size of the entry is stored in the ng_sz field and the enture natget */
1668 /* structure is copied back to the user. */
1669 /* ------------------------------------------------------------------------ */
1670 static int
1671 ipf_nat_getsz(ipf_main_softc_t *softc, void *data, int getlock)
1672 {
1673 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1674 ap_session_t *aps;
1675 nat_t *nat, *n;
1676 natget_t ng;
1677 int error;
1678
1679 error = BCOPYIN(data, &ng, sizeof(ng));
1680 if (error != 0) {
1681 IPFERROR(60024);
1682 return EFAULT;
1683 }
1684
1685 if (getlock) {
1686 READ_ENTER(&softc->ipf_nat);
1687 }
1688
1689 nat = ng.ng_ptr;
1690 if (!nat) {
1691 nat = softn->ipf_nat_instances;
1692 ng.ng_sz = 0;
1693 /*
1694 * Empty list so the size returned is 0. Simple.
1695 */
1696 if (nat == NULL) {
1697 if (getlock) {
1698 RWLOCK_EXIT(&softc->ipf_nat);
1699 }
1700 error = BCOPYOUT(&ng, data, sizeof(ng));
1701 if (error != 0) {
1702 IPFERROR(60025);
1703 return EFAULT;
1704 }
1705 return 0;
1706 }
1707 } else {
1708 /*
1709 * Make sure the pointer we're copying from exists in the
1710 * current list of entries. Security precaution to prevent
1711 * copying of random kernel data.
1712 */
1713 for (n = softn->ipf_nat_instances; n; n = n->nat_next)
1714 if (n == nat)
1715 break;
1716 if (n == NULL) {
1717 if (getlock) {
1718 RWLOCK_EXIT(&softc->ipf_nat);
1719 }
1720 IPFERROR(60026);
1721 return ESRCH;
1722 }
1723 }
1724
1725 /*
1726 * Incluse any space required for proxy data structures.
1727 */
1728 ng.ng_sz = sizeof(nat_save_t);
1729 aps = nat->nat_aps;
1730 if (aps != NULL) {
1731 ng.ng_sz += sizeof(ap_session_t) - 4;
1732 if (aps->aps_data != 0)
1733 ng.ng_sz += aps->aps_psiz;
1734 }
1735 if (getlock) {
1736 RWLOCK_EXIT(&softc->ipf_nat);
1737 }
1738
1739 error = BCOPYOUT(&ng, data, sizeof(ng));
1740 if (error != 0) {
1741 IPFERROR(60027);
1742 return EFAULT;
1743 }
1744 return 0;
1745 }
1746
1747
1748 /* ------------------------------------------------------------------------ */
1749 /* Function: ipf_nat_getent */
1750 /* Returns: int - 0 == success, != 0 is the error value. */
1751 /* Parameters: softc(I) - pointer to soft context main structure */
1752 /* data(I) - pointer to natget structure with kernel pointer*/
1753 /* to NAT structure to copy out. */
1754 /* getlock(I) - flag indicating whether or not the caller */
1755 /* holds a lock on ipf_nat */
1756 /* */
1757 /* Handle SIOCSTGET. */
1758 /* Copies out NAT entry to user space. Any additional data held for a */
1759 /* proxy is also copied, as to is the NAT rule which was responsible for it */
1760 /* ------------------------------------------------------------------------ */
1761 static int
1762 ipf_nat_getent(ipf_main_softc_t *softc, void *data, int getlock)
1763 {
1764 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1765 int error, outsize;
1766 ap_session_t *aps;
1767 nat_save_t *ipn, ipns;
1768 nat_t *n, *nat;
1769
1770 error = ipf_inobj(softc, data, NULL, &ipns, IPFOBJ_NATSAVE);
1771 if (error != 0)
1772 return error;
1773
1774 if ((ipns.ipn_dsize < sizeof(ipns)) || (ipns.ipn_dsize > 81920)) {
1775 IPFERROR(60028);
1776 return EINVAL;
1777 }
1778
1779 KMALLOCS(ipn, nat_save_t *, ipns.ipn_dsize);
1780 if (ipn == NULL) {
1781 IPFERROR(60029);
1782 return ENOMEM;
1783 }
1784
1785 if (getlock) {
1786 READ_ENTER(&softc->ipf_nat);
1787 }
1788
1789 ipn->ipn_dsize = ipns.ipn_dsize;
1790 nat = ipns.ipn_next;
1791 if (nat == NULL) {
1792 nat = softn->ipf_nat_instances;
1793 if (nat == NULL) {
1794 if (softn->ipf_nat_instances == NULL) {
1795 IPFERROR(60030);
1796 error = ENOENT;
1797 }
1798 goto finished;
1799 }
1800 } else {
1801 /*
1802 * Make sure the pointer we're copying from exists in the
1803 * current list of entries. Security precaution to prevent
1804 * copying of random kernel data.
1805 */
1806 for (n = softn->ipf_nat_instances; n; n = n->nat_next)
1807 if (n == nat)
1808 break;
1809 if (n == NULL) {
1810 IPFERROR(60031);
1811 error = ESRCH;
1812 goto finished;
1813 }
1814 }
1815 ipn->ipn_next = nat->nat_next;
1816
1817 /*
1818 * Copy the NAT structure.
1819 */
1820 bcopy((char *)nat, &ipn->ipn_nat, sizeof(*nat));
1821
1822 /*
1823 * If we have a pointer to the NAT rule it belongs to, save that too.
1824 */
1825 if (nat->nat_ptr != NULL)
1826 bcopy((char *)nat->nat_ptr, (char *)&ipn->ipn_ipnat,
1827 ipn->ipn_ipnat.in_size);
1828
1829 /*
1830 * If we also know the NAT entry has an associated filter rule,
1831 * save that too.
1832 */
1833 if (nat->nat_fr != NULL)
1834 bcopy((char *)nat->nat_fr, (char *)&ipn->ipn_fr,
1835 sizeof(ipn->ipn_fr));
1836
1837 /*
1838 * Last but not least, if there is an application proxy session set
1839 * up for this NAT entry, then copy that out too, including any
1840 * private data saved along side it by the proxy.
1841 */
1842 aps = nat->nat_aps;
1843 outsize = ipn->ipn_dsize - sizeof(*ipn) + sizeof(ipn->ipn_data);
1844 if (aps != NULL) {
1845 char *s;
1846
1847 if (outsize < sizeof(*aps)) {
1848 IPFERROR(60032);
1849 error = ENOBUFS;
1850 goto finished;
1851 }
1852
1853 s = ipn->ipn_data;
1854 bcopy((char *)aps, s, sizeof(*aps));
1855 s += sizeof(*aps);
1856 outsize -= sizeof(*aps);
1857 if ((aps->aps_data != NULL) && (outsize >= aps->aps_psiz))
1858 bcopy(aps->aps_data, s, aps->aps_psiz);
1859 else {
1860 IPFERROR(60033);
1861 error = ENOBUFS;
1862 }
1863 }
1864 if (error == 0) {
1865 if (getlock) {
1866 READ_ENTER(&softc->ipf_nat);
1867 getlock = 0;
1868 }
1869 error = ipf_outobjsz(softc, data, ipn, IPFOBJ_NATSAVE,
1870 ipns.ipn_dsize);
1871 }
1872
1873 finished:
1874 if (getlock) {
1875 READ_ENTER(&softc->ipf_nat);
1876 }
1877 if (ipn != NULL) {
1878 KFREES(ipn, ipns.ipn_dsize);
1879 }
1880 return error;
1881 }
1882
1883
1884 /* ------------------------------------------------------------------------ */
1885 /* Function: ipf_nat_putent */
1886 /* Returns: int - 0 == success, != 0 is the error value. */
1887 /* Parameters: softc(I) - pointer to soft context main structure */
1888 /* data(I) - pointer to natget structure with NAT */
1889 /* structure information to load into the kernel */
1890 /* getlock(I) - flag indicating whether or not a write lock */
1891 /* on is already held. */
1892 /* */
1893 /* Handle SIOCSTPUT. */
1894 /* Loads a NAT table entry from user space, including a NAT rule, proxy and */
1895 /* firewall rule data structures, if pointers to them indicate so. */
1896 /* ------------------------------------------------------------------------ */
1897 static int
1898 ipf_nat_putent(ipf_main_softc_t *softc, void *data, int getlock)
1899 {
1900 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1901 nat_save_t *ipn, *ipnn;
1902 ap_session_t *aps;
1903 nat_t *n, *nat;
1904 frentry_t *fr;
1905 fr_info_t *fin;
1906 ipnat_t *in;
1907 int error;
1908
1909 KMALLOC(ipn, nat_save_t *);
1910 if (ipn == NULL)
1911 return ENOMEM;
1912 error = ipf_inobj(softc, data, NULL, ipn, IPFOBJ_NATSAVE);
1913 if (error != 0)
1914 return error;
1915
1916 /*
1917 * Initialise early because of code at junkput label.
1918 */
1919 n = NULL;
1920 in = NULL;
1921 aps = NULL;
1922 nat = NULL;
1923 ipnn = NULL;
1924 fin = NULL;
1925 fr = NULL;
1926
1927 /*
1928 * New entry, copy in the rest of the NAT entry if it's size is more
1929 * than just the nat_t structure.
1930 */
1931 if (ipn->ipn_dsize > sizeof(*ipn)) {
1932 if (ipn->ipn_dsize > 81920) {
1933 IPFERROR(60034);
1934 error = ENOMEM;
1935 goto junkput;
1936 }
1937
1938 KMALLOCS(ipnn, nat_save_t *, ipn->ipn_dsize);
1939 if (ipnn == NULL) {
1940 IPFERROR(60035);
1941 return ENOMEM;
1942 }
1943
1944 bzero(ipnn, ipn->ipn_dsize);
1945 error = ipf_inobjsz(softc, data, ipnn, IPFOBJ_NATSAVE,
1946 ipn->ipn_dsize);
1947 if (error != 0) {
1948 goto junkput;
1949 }
1950 } else
1951 ipnn = ipn;
1952
1953 KMALLOC(nat, nat_t *);
1954 if (nat == NULL) {
1955 IPFERROR(60037);
1956 error = ENOMEM;
1957 goto junkput;
1958 }
1959
1960 bcopy((char *)&ipnn->ipn_nat, (char *)nat, sizeof(*nat));
1961
1962 switch (nat->nat_v[0])
1963 {
1964 case 4:
1965 #ifdef USE_IENT6
1966 case 6 :
1967 #endif
1968 break;
1969 default :
1970 IPFERROR(60061);
1971 error = EPROTONOSUPPORT;
1972 goto junkput;
1973 /*NOTREACHED*/
1974 }
1975
1976 /*
1977 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
1978 */
1979 bzero((char *)nat, offsetof(struct nat, nat_tqe));
1980 nat->nat_tqe.tqe_pnext = NULL;
1981 nat->nat_tqe.tqe_next = NULL;
1982 nat->nat_tqe.tqe_ifq = NULL;
1983 nat->nat_tqe.tqe_parent = nat;
1984
1985 /*
1986 * Restore the rule associated with this nat session
1987 */
1988 in = ipnn->ipn_nat.nat_ptr;
1989 if (in != NULL) {
1990 KMALLOCS(in, ipnat_t *, ipnn->ipn_ipnat.in_size);
1991 nat->nat_ptr = in;
1992 if (in == NULL) {
1993 IPFERROR(60038);
1994 error = ENOMEM;
1995 goto junkput;
1996 }
1997 bcopy((char *)&ipnn->ipn_ipnat, (char *)in,
1998 ipnn->ipn_ipnat.in_size);
1999 in->in_use = 1;
2000 in->in_flags |= IPN_DELETE;
2001
2002 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules);
2003
2004 if (ipf_nat_resolverule(softc, in) != 0) {
2005 IPFERROR(60039);
2006 error = ESRCH;
2007 goto junkput;
2008 }
2009 }
2010
2011 /*
2012 * Check that the NAT entry doesn't already exist in the kernel.
2013 *
2014 * For NAT_OUTBOUND, we're lookup for a duplicate MAP entry. To do
2015 * this, we check to see if the inbound combination of addresses and
2016 * ports is already known. Similar logic is applied for NAT_INBOUND.
2017 *
2018 */
2019 KMALLOC(fin, fr_info_t *);
2020 if (fin == NULL) {
2021 error = ENOMEM;
2022 goto junkput;
2023 }
2024 bzero(fin, sizeof(*fin));
2025 fin->fin_v = nat->nat_v[0];
2026 fin->fin_p = nat->nat_pr[0];
2027 fin->fin_rev = nat->nat_rev;
2028 fin->fin_ifp = nat->nat_ifps[0];
2029 fin->fin_data[0] = ntohs(nat->nat_ndport);
2030 fin->fin_data[1] = ntohs(nat->nat_nsport);
2031
2032 switch (nat->nat_dir)
2033 {
2034 case NAT_OUTBOUND :
2035 case NAT_DIVERTOUT :
2036 if (getlock) {
2037 READ_ENTER(&softc->ipf_nat);
2038 }
2039
2040 fin->fin_v = nat->nat_v[1];
2041 if (nat->nat_v[1] == 4) {
2042 n = ipf_nat_inlookup(fin, nat->nat_flags, fin->fin_p,
2043 nat->nat_ndstip, nat->nat_nsrcip);
2044 #ifdef USE_INET6
2045 } else if (nat->nat_v[1] == 6) {
2046 n = ipf_nat6_inlookup(fin, nat->nat_flags, fin->fin_p,
2047 &nat->nat_ndst6.in6,
2048 &nat->nat_nsrc6.in6);
2049 #endif
2050 }
2051
2052 if (getlock) {
2053 RWLOCK_EXIT(&softc->ipf_nat);
2054 }
2055 if (n != NULL) {
2056 IPFERROR(60040);
2057 error = EEXIST;
2058 goto junkput;
2059 }
2060 break;
2061
2062 case NAT_INBOUND :
2063 case NAT_DIVERTIN :
2064 if (getlock) {
2065 READ_ENTER(&softc->ipf_nat);
2066 }
2067
2068 if (fin->fin_v == 4) {
2069 n = ipf_nat_outlookup(fin, nat->nat_flags, fin->fin_p,
2070 nat->nat_ndstip,
2071 nat->nat_nsrcip);
2072 #ifdef USE_INET6
2073 } else if (fin->fin_v == 6) {
2074 n = ipf_nat6_outlookup(fin, nat->nat_flags, fin->fin_p,
2075 &nat->nat_ndst6.in6,
2076 &nat->nat_nsrc6.in6);
2077 #endif
2078 }
2079
2080 if (getlock) {
2081 RWLOCK_EXIT(&softc->ipf_nat);
2082 }
2083 if (n != NULL) {
2084 IPFERROR(60041);
2085 error = EEXIST;
2086 goto junkput;
2087 }
2088 break;
2089
2090 default :
2091 IPFERROR(60042);
2092 error = EINVAL;
2093 goto junkput;
2094 }
2095
2096 /*
2097 * Restore ap_session_t structure. Include the private data allocated
2098 * if it was there.
2099 */
2100 aps = nat->nat_aps;
2101 if (aps != NULL) {
2102 KMALLOC(aps, ap_session_t *);
2103 nat->nat_aps = aps;
2104 if (aps == NULL) {
2105 IPFERROR(60043);
2106 error = ENOMEM;
2107 goto junkput;
2108 }
2109 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps));
2110 if (in != NULL)
2111 aps->aps_apr = in->in_apr;
2112 else
2113 aps->aps_apr = NULL;
2114 if (aps->aps_psiz != 0) {
2115 if (aps->aps_psiz > 81920) {
2116 IPFERROR(60044);
2117 error = ENOMEM;
2118 goto junkput;
2119 }
2120 KMALLOCS(aps->aps_data, void *, aps->aps_psiz);
2121 if (aps->aps_data == NULL) {
2122 IPFERROR(60045);
2123 error = ENOMEM;
2124 goto junkput;
2125 }
2126 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data,
2127 aps->aps_psiz);
2128 } else {
2129 aps->aps_psiz = 0;
2130 aps->aps_data = NULL;
2131 }
2132 }
2133
2134 /*
2135 * If there was a filtering rule associated with this entry then
2136 * build up a new one.
2137 */
2138 fr = nat->nat_fr;
2139 if (fr != NULL) {
2140 if ((nat->nat_flags & SI_NEWFR) != 0) {
2141 KMALLOC(fr, frentry_t *);
2142 nat->nat_fr = fr;
2143 if (fr == NULL) {
2144 IPFERROR(60046);
2145 error = ENOMEM;
2146 goto junkput;
2147 }
2148 ipnn->ipn_nat.nat_fr = fr;
2149 fr->fr_ref = 1;
2150 (void) ipf_outobj(softc, data, ipnn, IPFOBJ_NATSAVE);
2151 bcopy((char *)&ipnn->ipn_fr, (char *)fr, sizeof(*fr));
2152
2153 fr->fr_ref = 1;
2154 fr->fr_dsize = 0;
2155 fr->fr_data = NULL;
2156 fr->fr_type = FR_T_NONE;
2157
2158 MUTEX_NUKE(&fr->fr_lock);
2159 MUTEX_INIT(&fr->fr_lock, "nat-filter rule lock");
2160 } else {
2161 if (getlock) {
2162 READ_ENTER(&softc->ipf_nat);
2163 }
2164 for (n = softn->ipf_nat_instances; n; n = n->nat_next)
2165 if (n->nat_fr == fr)
2166 break;
2167
2168 if (n != NULL) {
2169 MUTEX_ENTER(&fr->fr_lock);
2170 fr->fr_ref++;
2171 MUTEX_EXIT(&fr->fr_lock);
2172 }
2173 if (getlock) {
2174 RWLOCK_EXIT(&softc->ipf_nat);
2175 }
2176
2177 if (n == NULL) {
2178 IPFERROR(60047);
2179 error = ESRCH;
2180 goto junkput;
2181 }
2182 }
2183 }
2184
2185 if (ipnn != ipn) {
2186 KFREES(ipnn, ipn->ipn_dsize);
2187 ipnn = NULL;
2188 }
2189
2190 if (getlock) {
2191 WRITE_ENTER(&softc->ipf_nat);
2192 }
2193
2194 if (fin->fin_v == 4)
2195 error = ipf_nat_finalise(fin, nat);
2196 #ifdef USE_INET6
2197 else
2198 error = ipf_nat6_finalise(fin, nat);
2199 #endif
2200
2201 if (getlock) {
2202 RWLOCK_EXIT(&softc->ipf_nat);
2203 }
2204
2205 if (error == 0)
2206 return 0;
2207
2208 IPFERROR(60048);
2209 error = ENOMEM;
2210
2211 junkput:
2212 if (fin != NULL)
2213 KFREE(fin);
2214 if (fr != NULL) {
2215 (void) ipf_derefrule(softc, &fr);
2216 }
2217
2218 if ((ipnn != NULL) && (ipnn != ipn)) {
2219 KFREES(ipnn, ipn->ipn_dsize);
2220 }
2221 if (ipn != NULL)
2222 KFREE(ipn);
2223 if (nat != NULL) {
2224 if (aps != NULL) {
2225 if (aps->aps_data != NULL) {
2226 KFREES(aps->aps_data, aps->aps_psiz);
2227 }
2228 KFREE(aps);
2229 }
2230 if (in != NULL) {
2231 if (in->in_apr)
2232 ipf_proxy_deref(in->in_apr);
2233 KFREES(in, in->in_size);
2234 }
2235 KFREE(nat);
2236 }
2237 return error;
2238 }
2239
2240
2241 /* ------------------------------------------------------------------------ */
2242 /* Function: ipf_nat_delete */
2243 /* Returns: Nil */
2244 /* Parameters: softc(I) - pointer to soft context main structure */
2245 /* nat(I) - pointer to NAT structure to delete */
2246 /* logtype(I) - type of LOG record to create before deleting */
2247 /* Write Lock: ipf_nat */
2248 /* */
2249 /* Delete a nat entry from the various lists and table. If NAT logging is */
2250 /* enabled then generate a NAT log record for this event. */
2251 /* ------------------------------------------------------------------------ */
2252 void
2253 ipf_nat_delete(ipf_main_softc_t *softc, struct nat *nat, int logtype)
2254 {
2255 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2256 int madeorphan = 0, removed = 0;
2257 u_int bkt;
2258 nat_stat_side_t *nss;
2259 struct ipnat *ipn;
2260
2261 if (logtype != 0 && softn->ipf_nat_logging != 0)
2262 ipf_nat_log(softc, softn, nat, logtype);
2263
2264 /*
2265 * Take it as a general indication that all the pointers are set if
2266 * nat_pnext is set.
2267 */
2268 if (nat->nat_pnext != NULL) {
2269 removed = 1;
2270
2271 bkt = nat->nat_hv[0] % softn->ipf_nat_table_sz;
2272 nss = &softn->ipf_nat_stats.ns_side[0];
2273 ASSERT(nss->ns_bucketlen[bkt] > 0);
2274 nss->ns_bucketlen[bkt]--;
2275 if (nss->ns_bucketlen[bkt] == 0) {
2276 nss->ns_inuse--;
2277 }
2278
2279 bkt = nat->nat_hv[1] % softn->ipf_nat_table_sz;
2280 nss = &softn->ipf_nat_stats.ns_side[1];
2281 ASSERT(nss->ns_bucketlen[bkt] > 0);
2282 nss->ns_bucketlen[bkt]--;
2283 if (nss->ns_bucketlen[bkt] == 0) {
2284 nss->ns_inuse--;
2285 }
2286
2287 *nat->nat_pnext = nat->nat_next;
2288 if (nat->nat_next != NULL) {
2289 nat->nat_next->nat_pnext = nat->nat_pnext;
2290 nat->nat_next = NULL;
2291 }
2292 nat->nat_pnext = NULL;
2293
2294 *nat->nat_phnext[0] = nat->nat_hnext[0];
2295 if (nat->nat_hnext[0] != NULL) {
2296 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2297 nat->nat_hnext[0] = NULL;
2298 }
2299 nat->nat_phnext[0] = NULL;
2300
2301 *nat->nat_phnext[1] = nat->nat_hnext[1];
2302 if (nat->nat_hnext[1] != NULL) {
2303 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2304 nat->nat_hnext[1] = NULL;
2305 }
2306 nat->nat_phnext[1] = NULL;
2307
2308 if ((nat->nat_flags & SI_WILDP) != 0) {
2309 ATOMIC_DEC32(softn->ipf_nat_stats.ns_wilds);
2310 }
2311 madeorphan = 1;
2312 }
2313
2314 if (nat->nat_me != NULL) {
2315 *nat->nat_me = NULL;
2316 nat->nat_me = NULL;
2317 nat->nat_ref--;
2318 ASSERT(nat->nat_ref >= 0);
2319 }
2320
2321 if (nat->nat_tqe.tqe_ifq != NULL) {
2322 /*
2323 * No call to ipf_freetimeoutqueue() is made here, they are
2324 * garbage collected in ipf_nat_expire().
2325 */
2326 (void) ipf_deletequeueentry(&nat->nat_tqe);
2327 }
2328
2329 if (nat->nat_sync) {
2330 ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
2331 nat->nat_sync = NULL;
2332 }
2333
2334 if (logtype == NL_EXPIRE)
2335 softn->ipf_nat_stats.ns_expire++;
2336
2337 MUTEX_ENTER(&nat->nat_lock);
2338 /*
2339 * NL_DESTROY should only be passed in when we've got nat_ref >= 2.
2340 * This happens when a nat'd packet is blocked and we want to throw
2341 * away the NAT session.
2342 */
2343 if (logtype == NL_DESTROY) {
2344 if (nat->nat_ref > 2) {
2345 nat->nat_ref -= 2;
2346 MUTEX_EXIT(&nat->nat_lock);
2347 if (removed)
2348 softn->ipf_nat_stats.ns_orphans++;
2349 return;
2350 }
2351 } else if (nat->nat_ref > 1) {
2352 nat->nat_ref--;
2353 MUTEX_EXIT(&nat->nat_lock);
2354 if (madeorphan == 1)
2355 softn->ipf_nat_stats.ns_orphans++;
2356 return;
2357 }
2358 ASSERT(nat->nat_ref >= 0);
2359 MUTEX_EXIT(&nat->nat_lock);
2360
2361 nat->nat_ref = 0;
2362
2363 if (madeorphan == 0)
2364 softn->ipf_nat_stats.ns_orphans--;
2365
2366 /*
2367 * At this point, nat_ref can be either 0 or -1
2368 */
2369 softn->ipf_nat_stats.ns_proto[nat->nat_pr[0]]--;
2370
2371 if (nat->nat_fr != NULL) {
2372 (void) ipf_derefrule(softc, &nat->nat_fr);
2373 }
2374
2375 if (nat->nat_hm != NULL) {
2376 ipf_nat_hostmapdel(softc, &nat->nat_hm);
2377 }
2378
2379 /*
2380 * If there is an active reference from the nat entry to its parent
2381 * rule, decrement the rule's reference count and free it too if no
2382 * longer being used.
2383 */
2384 ipn = nat->nat_ptr;
2385 nat->nat_ptr = NULL;
2386
2387 if (ipn != NULL) {
2388 ipn->in_space++;
2389 ipf_nat_rule_deref(softc, &ipn);
2390 }
2391
2392 if (nat->nat_aps != NULL) {
2393 ipf_proxy_free(softc, nat->nat_aps);
2394 nat->nat_aps = NULL;
2395 }
2396
2397 MUTEX_DESTROY(&nat->nat_lock);
2398
2399 softn->ipf_nat_stats.ns_active--;
2400
2401 /*
2402 * If there's a fragment table entry too for this nat entry, then
2403 * dereference that as well. This is after nat_lock is released
2404 * because of Tru64.
2405 */
2406 ipf_frag_natforget(softc, (void *)nat);
2407
2408 KFREE(nat);
2409 }
2410
2411
2412 /* ------------------------------------------------------------------------ */
2413 /* Function: ipf_nat_flushtable */
2414 /* Returns: int - number of NAT rules deleted */
2415 /* Parameters: softc(I) - pointer to soft context main structure */
2416 /* softn(I) - pointer to NAT context structure */
2417 /* Write Lock: ipf_nat */
2418 /* */
2419 /* Deletes all currently active NAT sessions. In deleting each NAT entry a */
2420 /* log record should be emitted in ipf_nat_delete() if NAT logging is */
2421 /* enabled. */
2422 /* ------------------------------------------------------------------------ */
2423 /*
2424 * nat_flushtable - clear the NAT table of all mapping entries.
2425 */
2426 static int
2427 ipf_nat_flushtable(ipf_main_softc_t *softc, ipf_nat_softc_t *softn)
2428 {
2429 nat_t *nat;
2430 int j = 0;
2431
2432 /*
2433 * ALL NAT mappings deleted, so lets just make the deletions
2434 * quicker.
2435 */
2436 if (softn->ipf_nat_table[0] != NULL)
2437 bzero((char *)softn->ipf_nat_table[0],
2438 sizeof(softn->ipf_nat_table[0]) *
2439 softn->ipf_nat_table_sz);
2440 if (softn->ipf_nat_table[1] != NULL)
2441 bzero((char *)softn->ipf_nat_table[1],
2442 sizeof(softn->ipf_nat_table[1]) *
2443 softn->ipf_nat_table_sz);
2444
2445 while ((nat = softn->ipf_nat_instances) != NULL) {
2446 ipf_nat_delete(softc, nat, NL_FLUSH);
2447 j++;
2448 }
2449
2450 return j;
2451 }
2452
2453
2454 /* ------------------------------------------------------------------------ */
2455 /* Function: ipf_nat_clearlist */
2456 /* Returns: int - number of NAT/RDR rules deleted */
2457 /* Parameters: softc(I) - pointer to soft context main structure */
2458 /* softn(I) - pointer to NAT context structure */
2459 /* */
2460 /* Delete all rules in the current list of rules. There is nothing elegant */
2461 /* about this cleanup: simply free all entries on the list of rules and */
2462 /* clear out the tables used for hashed NAT rule lookups. */
2463 /* ------------------------------------------------------------------------ */
2464 static int
2465 ipf_nat_clearlist(ipf_main_softc_t *softc, ipf_nat_softc_t *softn)
2466 {
2467 ipnat_t *n;
2468 int i = 0;
2469
2470 if (softn->ipf_nat_map_rules != NULL) {
2471 bzero((char *)softn->ipf_nat_map_rules,
2472 sizeof(*softn->ipf_nat_map_rules) *
2473 softn->ipf_nat_maprules_sz);
2474 }
2475 if (softn->ipf_nat_rdr_rules != NULL) {
2476 bzero((char *)softn->ipf_nat_rdr_rules,
2477 sizeof(*softn->ipf_nat_rdr_rules) *
2478 softn->ipf_nat_rdrrules_sz);
2479 }
2480
2481 while ((n = softn->ipf_nat_list) != NULL) {
2482 ipf_nat_delrule(softc, softn, n, 0);
2483 i++;
2484 }
2485 #if SOLARIS && !defined(INSTANCES)
2486 pfil_delayed_copy = 1;
2487 #endif
2488 return i;
2489 }
2490
2491
2492 /* ------------------------------------------------------------------------ */
2493 /* Function: ipf_nat_delrule */
2494 /* Returns: Nil */
2495 /* Parameters: softc(I) - pointer to soft context main structure */
2496 /* softn(I) - pointer to NAT context structure */
2497 /* np(I) - pointer to NAT rule to delete */
2498 /* purge(I) - 1 == allow purge, 0 == prevent purge */
2499 /* Locks: WRITE(ipf_nat) */
2500 /* */
2501 /* Preventing "purge" from occuring is allowed because when all of the NAT */
2502 /* rules are being removed, allowing the "purge" to walk through the list */
2503 /* of NAT sessions, possibly multiple times, would be a large performance */
2504 /* hit, on the order of O(N^2). */
2505 /* ------------------------------------------------------------------------ */
2506 static void
2507 ipf_nat_delrule(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *np,
2508 int purge)
2509 {
2510
2511 if (np->in_pnext != NULL) {
2512 *np->in_pnext = np->in_next;
2513 if (np->in_next != NULL)
2514 np->in_next->in_pnext = np->in_pnext;
2515 if (softn->ipf_nat_list_tail == &np->in_next)
2516 softn->ipf_nat_list_tail = np->in_pnext;
2517 }
2518
2519 if ((purge == 1) && ((np->in_flags & IPN_PURGE) != 0)) {
2520 nat_t *next;
2521 nat_t *nat;
2522
2523 for (next = softn->ipf_nat_instances; (nat = next) != NULL;) {
2524 next = nat->nat_next;
2525 if (nat->nat_ptr == np)
2526 ipf_nat_delete(softc, nat, NL_PURGE);
2527 }
2528 }
2529
2530 if ((np->in_flags & IPN_DELETE) == 0) {
2531 if (np->in_redir & NAT_REDIRECT) {
2532 switch (np->in_v[0])
2533 {
2534 case 4 :
2535 ipf_nat_delrdr(softn, np);
2536 break;
2537 #ifdef USE_INET6
2538 case 6 :
2539 ipf_nat6_delrdr(softn, np);
2540 break;
2541 #endif
2542 }
2543 }
2544 if (np->in_redir & (NAT_MAPBLK|NAT_MAP)) {
2545 switch (np->in_v[0])
2546 {
2547 case 4 :
2548 ipf_nat_delmap(softn, np);
2549 break;
2550 #ifdef USE_INET6
2551 case 6 :
2552 ipf_nat6_delmap(softn, np);
2553 break;
2554 #endif
2555 }
2556 }
2557 }
2558
2559 np->in_flags |= IPN_DELETE;
2560 ipf_nat_rule_deref(softc, &np);
2561 }
2562
2563
2564 /* ------------------------------------------------------------------------ */
2565 /* Function: ipf_nat_newmap */
2566 /* Returns: int - -1 == error, 0 == success */
2567 /* Parameters: fin(I) - pointer to packet information */
2568 /* nat(I) - pointer to NAT entry */
2569 /* ni(I) - pointer to structure with misc. information needed */
2570 /* to create new NAT entry. */
2571 /* */
2572 /* Given an empty NAT structure, populate it with new information about a */
2573 /* new NAT session, as defined by the matching NAT rule. */
2574 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
2575 /* to the new IP address for the translation. */
2576 /* ------------------------------------------------------------------------ */
2577 static int
2578 ipf_nat_newmap(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
2579 {
2580 ipf_main_softc_t *softc = fin->fin_main_soft;
2581 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2582 u_short st_port, dport, sport, port, sp, dp;
2583 struct in_addr in, inb;
2584 hostmap_t *hm;
2585 u_32_t flags;
2586 u_32_t st_ip;
2587 ipnat_t *np;
2588 nat_t *natl;
2589 int l;
2590
2591 /*
2592 * If it's an outbound packet which doesn't match any existing
2593 * record, then create a new port
2594 */
2595 l = 0;
2596 hm = NULL;
2597 np = ni->nai_np;
2598 st_ip = np->in_snip;
2599 st_port = np->in_spnext;
2600 flags = nat->nat_flags;
2601
2602 if (flags & IPN_ICMPQUERY) {
2603 sport = fin->fin_data[1];
2604 dport = 0;
2605 } else {
2606 sport = htons(fin->fin_data[0]);
2607 dport = htons(fin->fin_data[1]);
2608 }
2609
2610 /*
2611 * Do a loop until we either run out of entries to try or we find
2612 * a NAT mapping that isn't currently being used. This is done
2613 * because the change to the source is not (usually) being fixed.
2614 */
2615 do {
2616 port = 0;
2617 in.s_addr = htonl(np->in_snip);
2618 if (l == 0) {
2619 /*
2620 * Check to see if there is an existing NAT
2621 * setup for this IP address pair.
2622 */
2623 hm = ipf_nat_hostmap(softn, np, fin->fin_src,
2624 fin->fin_dst, in, 0);
2625 if (hm != NULL)
2626 in.s_addr = hm->hm_nsrcip.s_addr;
2627 } else if ((l == 1) && (hm != NULL)) {
2628 ipf_nat_hostmapdel(softc, &hm);
2629 }
2630 in.s_addr = ntohl(in.s_addr);
2631
2632 nat->nat_hm = hm;
2633
2634 if ((np->in_nsrcmsk == 0xffffffff) && (np->in_spnext == 0)) {
2635 if (l > 0) {
2636 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_1);
2637 return -1;
2638 }
2639 }
2640
2641 if (np->in_redir == NAT_BIMAP &&
2642 np->in_osrcmsk == np->in_nsrcmsk) {
2643 /*
2644 * map the address block in a 1:1 fashion
2645 */
2646 in.s_addr = np->in_nsrcaddr;
2647 in.s_addr |= fin->fin_saddr & ~np->in_osrcmsk;
2648 in.s_addr = ntohl(in.s_addr);
2649
2650 } else if (np->in_redir & NAT_MAPBLK) {
2651 if ((l >= np->in_ppip) || ((l > 0) &&
2652 !(flags & IPN_TCPUDP))) {
2653 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_2);
2654 return -1;
2655 }
2656 /*
2657 * map-block - Calculate destination address.
2658 */
2659 in.s_addr = ntohl(fin->fin_saddr);
2660 in.s_addr &= ntohl(~np->in_osrcmsk);
2661 inb.s_addr = in.s_addr;
2662 in.s_addr /= np->in_ippip;
2663 in.s_addr &= ntohl(~np->in_nsrcmsk);
2664 in.s_addr += ntohl(np->in_nsrcaddr);
2665 /*
2666 * Calculate destination port.
2667 */
2668 if ((flags & IPN_TCPUDP) &&
2669 (np->in_ppip != 0)) {
2670 port = ntohs(sport) + l;
2671 port %= np->in_ppip;
2672 port += np->in_ppip *
2673 (inb.s_addr % np->in_ippip);
2674 port += MAPBLK_MINPORT;
2675 port = htons(port);
2676 }
2677
2678 } else if ((np->in_nsrcaddr == 0) &&
2679 (np->in_nsrcmsk == 0xffffffff)) {
2680 i6addr_t in6;
2681
2682 /*
2683 * 0/32 - use the interface's IP address.
2684 */
2685 if ((l > 0) ||
2686 ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
2687 &in6, NULL) == -1) {
2688 NBUMPSIDEX(1, ns_new_ifpaddr, ns_new_ifpaddr_1);
2689 return -1;
2690 }
2691 in.s_addr = ntohl(in6.in4.s_addr);
2692
2693 } else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
2694 /*
2695 * 0/0 - use the original source address/port.
2696 */
2697 if (l > 0) {
2698 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_3);
2699 return -1;
2700 }
2701 in.s_addr = ntohl(fin->fin_saddr);
2702
2703 } else if ((np->in_nsrcmsk != 0xffffffff) &&
2704 (np->in_spnext == 0) && ((l > 0) || (hm == NULL)))
2705 np->in_snip++;
2706
2707 natl = NULL;
2708
2709 if ((flags & IPN_TCPUDP) &&
2710 ((np->in_redir & NAT_MAPBLK) == 0) &&
2711 (np->in_flags & IPN_AUTOPORTMAP)) {
2712 /*
2713 * "ports auto" (without map-block)
2714 */
2715 if ((l > 0) && np->in_ppip && (l % np->in_ppip == 0)) {
2716 if (l > np->in_space) {
2717 return -1;
2718 } else if ((l > np->in_ppip) &&
2719 np->in_nsrcmsk != 0xffffffff)
2720 np->in_snip++;
2721 }
2722 if (np->in_ppip != 0) {
2723 port = ntohs(sport);
2724 port += (l % np->in_ppip);
2725 port %= np->in_ppip;
2726 port += np->in_ppip *
2727 (ntohl(fin->fin_saddr) %
2728 np->in_ippip);
2729 port += MAPBLK_MINPORT;
2730 port = htons(port);
2731 }
2732
2733 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
2734 (flags & IPN_TCPUDPICMP) && (np->in_spnext != 0)) {
2735 /*
2736 * Standard port translation. Select next port.
2737 */
2738 if (np->in_flags & IPN_SEQUENTIAL) {
2739 port = np->in_spnext;
2740 } else {
2741 port = ipf_random() % (np->in_spmax -
2742 np->in_spmin + 1);
2743 port += np->in_spmin;
2744 }
2745 port = htons(port);
2746 np->in_spnext++;
2747
2748 if (np->in_spnext > np->in_spmax) {
2749 np->in_spnext = np->in_spmin;
2750 if (np->in_nsrcmsk != 0xffffffff)
2751 np->in_snip++;
2752 }
2753 }
2754
2755 if (np->in_flags & IPN_SIPRANGE) {
2756 if (np->in_snip > ntohl(np->in_nsrcmsk))
2757 np->in_snip = ntohl(np->in_nsrcaddr);
2758 } else {
2759 if ((np->in_nsrcmsk != 0xffffffff) &&
2760 ((np->in_snip + 1) & ntohl(np->in_nsrcmsk)) >
2761 ntohl(np->in_nsrcaddr))
2762 np->in_snip = ntohl(np->in_nsrcaddr) + 1;
2763 }
2764
2765 if ((port == 0) && (flags & (IPN_TCPUDPICMP|IPN_ICMPQUERY)))
2766 port = sport;
2767
2768 /*
2769 * Here we do a lookup of the connection as seen from
2770 * the outside. If an IP# pair already exists, try
2771 * again. So if you have A->B becomes C->B, you can
2772 * also have D->E become C->E but not D->B causing
2773 * another C->B. Also take protocol and ports into
2774 * account when determining whether a pre-existing
2775 * NAT setup will cause an external conflict where
2776 * this is appropriate.
2777 */
2778 inb.s_addr = htonl(in.s_addr);
2779 sp = fin->fin_data[0];
2780 dp = fin->fin_data[1];
2781 fin->fin_data[0] = fin->fin_data[1];
2782 fin->fin_data[1] = ntohs(port);
2783 natl = ipf_nat_inlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
2784 (u_int)fin->fin_p, fin->fin_dst, inb);
2785 fin->fin_data[0] = sp;
2786 fin->fin_data[1] = dp;
2787
2788 /*
2789 * Has the search wrapped around and come back to the
2790 * start ?
2791 */
2792 if ((natl != NULL) &&
2793 (np->in_spnext != 0) && (st_port == np->in_spnext) &&
2794 (np->in_snip != 0) && (st_ip == np->in_snip)) {
2795 NBUMPSIDED(1, ns_wrap);
2796 return -1;
2797 }
2798 l++;
2799 } while (natl != NULL);
2800
2801 /* Setup the NAT table */
2802 nat->nat_osrcip = fin->fin_src;
2803 nat->nat_nsrcaddr = htonl(in.s_addr);
2804 nat->nat_odstip = fin->fin_dst;
2805 nat->nat_ndstip = fin->fin_dst;
2806 if (nat->nat_hm == NULL)
2807 nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
2808 fin->fin_dst, nat->nat_nsrcip,
2809 0);
2810
2811 if (flags & IPN_TCPUDP) {
2812 nat->nat_osport = sport;
2813 nat->nat_nsport = port; /* sport */
2814 nat->nat_odport = dport;
2815 nat->nat_ndport = dport;
2816 ((tcphdr_t *)fin->fin_dp)->th_sport = port;
2817 } else if (flags & IPN_ICMPQUERY) {
2818 nat->nat_oicmpid = fin->fin_data[1];
2819 ((icmphdr_t *)fin->fin_dp)->icmp_id = port;
2820 nat->nat_nicmpid = port;
2821 }
2822 return 0;
2823 }
2824
2825
2826 /* ------------------------------------------------------------------------ */
2827 /* Function: ipf_nat_newrdr */
2828 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */
2829 /* allow rule to be moved if IPN_ROUNDR is set. */
2830 /* Parameters: fin(I) - pointer to packet information */
2831 /* nat(I) - pointer to NAT entry */
2832 /* ni(I) - pointer to structure with misc. information needed */
2833 /* to create new NAT entry. */
2834 /* */
2835 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
2836 /* to the new IP address for the translation. */
2837 /* ------------------------------------------------------------------------ */
2838 static int
2839 ipf_nat_newrdr(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
2840 {
2841 ipf_main_softc_t *softc = fin->fin_main_soft;
2842 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2843 u_short nport, dport, sport;
2844 struct in_addr in, inb;
2845 u_short sp, dp;
2846 hostmap_t *hm;
2847 u_32_t flags;
2848 ipnat_t *np;
2849 nat_t *natl;
2850 int move;
2851
2852 move = 1;
2853 hm = NULL;
2854 in.s_addr = 0;
2855 np = ni->nai_np;
2856 flags = nat->nat_flags;
2857
2858 if (flags & IPN_ICMPQUERY) {
2859 dport = fin->fin_data[1];
2860 sport = 0;
2861 } else {
2862 sport = htons(fin->fin_data[0]);
2863 dport = htons(fin->fin_data[1]);
2864 }
2865
2866 /* TRACE sport, dport */
2867
2868
2869 /*
2870 * If the matching rule has IPN_STICKY set, then we want to have the
2871 * same rule kick in as before. Why would this happen? If you have
2872 * a collection of rdr rules with "round-robin sticky", the current
2873 * packet might match a different one to the previous connection but
2874 * we want the same destination to be used.
2875 */
2876 if (((np->in_flags & (IPN_ROUNDR|IPN_SPLIT)) != 0) &&
2877 ((np->in_flags & IPN_STICKY) != 0)) {
2878 hm = ipf_nat_hostmap(softn, NULL, fin->fin_src, fin->fin_dst,
2879 in, (u_32_t)dport);
2880 if (hm != NULL) {
2881 in.s_addr = ntohl(hm->hm_ndstip.s_addr);
2882 np = hm->hm_ipnat;
2883 ni->nai_np = np;
2884 move = 0;
2885 ipf_nat_hostmapdel(softc, &hm);
2886 }
2887 }
2888
2889 /*
2890 * Otherwise, it's an inbound packet. Most likely, we don't
2891 * want to rewrite source ports and source addresses. Instead,
2892 * we want to rewrite to a fixed internal address and fixed
2893 * internal port.
2894 */
2895 if (np->in_flags & IPN_SPLIT) {
2896 in.s_addr = np->in_dnip;
2897 inb.s_addr = htonl(in.s_addr);
2898
2899 if ((np->in_flags & (IPN_ROUNDR|IPN_STICKY)) == IPN_STICKY) {
2900 hm = ipf_nat_hostmap(softn, NULL, fin->fin_src,
2901 fin->fin_dst, inb, (u_32_t)dport);
2902 if (hm != NULL) {
2903 in.s_addr = hm->hm_ndstip.s_addr;
2904 move = 0;
2905 }
2906 }
2907
2908 if (hm == NULL || hm->hm_ref == 1) {
2909 if (np->in_ndstaddr == htonl(in.s_addr)) {
2910 np->in_dnip = ntohl(np->in_ndstmsk);
2911 move = 0;
2912 } else {
2913 np->in_dnip = ntohl(np->in_ndstaddr);
2914 }
2915 }
2916 if (hm != NULL)
2917 ipf_nat_hostmapdel(softc, &hm);
2918
2919 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
2920 i6addr_t in6;
2921
2922 /*
2923 * 0/32 - use the interface's IP address.
2924 */
2925 if (ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp,
2926 &in6, NULL) == -1) {
2927 NBUMPSIDEX(0, ns_new_ifpaddr, ns_new_ifpaddr_2);
2928 return -1;
2929 }
2930 in.s_addr = ntohl(in6.in4.s_addr);
2931
2932 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk== 0)) {
2933 /*
2934 * 0/0 - use the original destination address/port.
2935 */
2936 in.s_addr = ntohl(fin->fin_daddr);
2937
2938 } else if (np->in_redir == NAT_BIMAP &&
2939 np->in_ndstmsk == np->in_odstmsk) {
2940 /*
2941 * map the address block in a 1:1 fashion
2942 */
2943 in.s_addr = np->in_ndstaddr;
2944 in.s_addr |= fin->fin_daddr & ~np->in_ndstmsk;
2945 in.s_addr = ntohl(in.s_addr);
2946 } else {
2947 in.s_addr = ntohl(np->in_ndstaddr);
2948 }
2949
2950 if ((np->in_dpnext == 0) || ((flags & NAT_NOTRULEPORT) != 0))
2951 nport = dport;
2952 else {
2953 /*
2954 * Whilst not optimized for the case where
2955 * pmin == pmax, the gain is not significant.
2956 */
2957 if (((np->in_flags & IPN_FIXEDDPORT) == 0) &&
2958 (np->in_odport != np->in_dtop)) {
2959 nport = ntohs(dport) - np->in_odport + np->in_dpmax;
2960 nport = htons(nport);
2961 } else {
2962 nport = htons(np->in_dpnext);
2963 np->in_dpnext++;
2964 if (np->in_dpnext > np->in_dpmax)
2965 np->in_dpnext = np->in_dpmin;
2966 }
2967 }
2968
2969 /*
2970 * When the redirect-to address is set to 0.0.0.0, just
2971 * assume a blank `forwarding' of the packet. We don't
2972 * setup any translation for this either.
2973 */
2974 if (in.s_addr == 0) {
2975 if (nport == dport) {
2976 NBUMPSIDED(0, ns_xlate_null);
2977 return -1;
2978 }
2979 in.s_addr = ntohl(fin->fin_daddr);
2980 }
2981
2982 /*
2983 * Check to see if this redirect mapping already exists and if
2984 * it does, return "failure" (allowing it to be created will just
2985 * cause one or both of these "connections" to stop working.)
2986 */
2987 inb.s_addr = htonl(in.s_addr);
2988 sp = fin->fin_data[0];
2989 dp = fin->fin_data[1];
2990 fin->fin_data[1] = fin->fin_data[0];
2991 fin->fin_data[0] = ntohs(nport);
2992 natl = ipf_nat_outlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
2993 (u_int)fin->fin_p, inb, fin->fin_src);
2994 fin->fin_data[0] = sp;
2995 fin->fin_data[1] = dp;
2996 if (natl != NULL) {
2997 DT2(ns_new_xlate_exists, fr_info_t *, fin, nat_t *, natl);
2998 NBUMPSIDE(0, ns_xlate_exists);
2999 return -1;
3000 }
3001
3002 nat->nat_ndstaddr = htonl(in.s_addr);
3003 nat->nat_odstip = fin->fin_dst;
3004 nat->nat_nsrcip = fin->fin_src;
3005 nat->nat_osrcip = fin->fin_src;
3006 if ((nat->nat_hm == NULL) && ((np->in_flags & IPN_STICKY) != 0))
3007 nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src,
3008 fin->fin_dst, inb, (u_32_t)dport);
3009
3010 if (flags & IPN_TCPUDP) {
3011 nat->nat_odport = dport;
3012 nat->nat_ndport = nport;
3013 nat->nat_osport = sport;
3014 nat->nat_nsport = sport;
3015 ((tcphdr_t *)fin->fin_dp)->th_dport = nport;
3016 } else if (flags & IPN_ICMPQUERY) {
3017 nat->nat_oicmpid = fin->fin_data[1];
3018 ((icmphdr_t *)fin->fin_dp)->icmp_id = nport;
3019 nat->nat_nicmpid = nport;
3020 }
3021
3022 return move;
3023 }
3024
3025 /* ------------------------------------------------------------------------ */
3026 /* Function: ipf_nat_add */
3027 /* Returns: nat_t* - NULL == failure to create new NAT structure, */
3028 /* else pointer to new NAT structure */
3029 /* Parameters: fin(I) - pointer to packet information */
3030 /* np(I) - pointer to NAT rule */
3031 /* natsave(I) - pointer to where to store NAT struct pointer */
3032 /* flags(I) - flags describing the current packet */
3033 /* direction(I) - direction of packet (in/out) */
3034 /* Write Lock: ipf_nat */
3035 /* */
3036 /* Attempts to create a new NAT entry. Does not actually change the packet */
3037 /* in any way. */
3038 /* */
3039 /* This function is in three main parts: (1) deal with creating a new NAT */
3040 /* structure for a "MAP" rule (outgoing NAT translation); (2) deal with */
3041 /* creating a new NAT structure for a "RDR" rule (incoming NAT translation) */
3042 /* and (3) building that structure and putting it into the NAT table(s). */
3043 /* */
3044 /* NOTE: natsave should NOT be used to point back to an ipstate_t struct */
3045 /* as it can result in memory being corrupted. */
3046 /* ------------------------------------------------------------------------ */
3047 nat_t *
3048 ipf_nat_add(fr_info_t *fin, ipnat_t *np, nat_t **natsave, u_int flags,
3049 int direction)
3050 {
3051 ipf_main_softc_t *softc = fin->fin_main_soft;
3052 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3053 hostmap_t *hm = NULL;
3054 nat_t *nat, *natl;
3055 natstat_t *nsp;
3056 u_int nflags;
3057 natinfo_t ni;
3058 int move;
3059
3060 memset(&ni, 0, sizeof ni); /* XXX gcc */
3061 nsp = &softn->ipf_nat_stats;
3062
3063 if ((nsp->ns_active * 100 / softn->ipf_nat_table_max) >
3064 softn->ipf_nat_table_wm_high) {
3065 softn->ipf_nat_doflush = 1;
3066 }
3067
3068 if (nsp->ns_active >= softn->ipf_nat_table_max) {
3069 NBUMPSIDED(fin->fin_out, ns_table_max);
3070 return NULL;
3071 }
3072
3073 move = 1;
3074 nflags = np->in_flags & flags;
3075 nflags &= NAT_FROMRULE;
3076
3077 ni.nai_np = np;
3078 ni.nai_dport = 0;
3079 ni.nai_sport = 0;
3080
3081 /* Give me a new nat */
3082 KMALLOC(nat, nat_t *);
3083 if (nat == NULL) {
3084 NBUMPSIDED(fin->fin_out, ns_memfail);
3085 /*
3086 * Try to automatically tune the max # of entries in the
3087 * table allowed to be less than what will cause kmem_alloc()
3088 * to fail and try to eliminate panics due to out of memory
3089 * conditions arising.
3090 */
3091 if ((softn->ipf_nat_table_max > softn->ipf_nat_table_sz) &&
3092 (nsp->ns_active > 100)) {
3093 softn->ipf_nat_table_max = nsp->ns_active - 100;
3094 printf("table_max reduced to %d\n",
3095 softn->ipf_nat_table_max);
3096 }
3097 return NULL;
3098 }
3099
3100 if (flags & IPN_ICMPQUERY) {
3101 /*
3102 * In the ICMP query NAT code, we translate the ICMP id fields
3103 * to make them unique. This is indepedent of the ICMP type
3104 * (e.g. in the unlikely event that a host sends an echo and
3105 * an tstamp request with the same id, both packets will have
3106 * their ip address/id field changed in the same way).
3107 */
3108 /* The icmp_id field is used by the sender to identify the
3109 * process making the icmp request. (the receiver justs
3110 * copies it back in its response). So, it closely matches
3111 * the concept of source port. We overlay sport, so we can
3112 * maximally reuse the existing code.
3113 */
3114 ni.nai_sport = fin->fin_data[1];
3115 ni.nai_dport = 0;
3116 }
3117
3118 bzero((char *)nat, sizeof(*nat));
3119 nat->nat_flags = flags;
3120 nat->nat_redir = np->in_redir;
3121 nat->nat_dir = direction;
3122 nat->nat_pr[0] = fin->fin_p;
3123 nat->nat_pr[1] = fin->fin_p;
3124
3125 /*
3126 * Search the current table for a match and create a new mapping
3127 * if there is none found.
3128 */
3129 if (np->in_redir & NAT_DIVERTUDP) {
3130 move = ipf_nat_newdivert(fin, nat, &ni);
3131
3132 } else if (np->in_redir & NAT_REWRITE) {
3133 move = ipf_nat_newrewrite(fin, nat, &ni);
3134
3135 } else if (direction == NAT_OUTBOUND) {
3136 /*
3137 * We can now arrange to call this for the same connection
3138 * because ipf_nat_new doesn't protect the code path into
3139 * this function.
3140 */
3141 natl = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
3142 fin->fin_src, fin->fin_dst);
3143 if (natl != NULL) {
3144 KFREE(nat);
3145 nat = natl;
3146 goto done;
3147 }
3148
3149 move = ipf_nat_newmap(fin, nat, &ni);
3150 } else {
3151 /*
3152 * NAT_INBOUND is used for redirects rules
3153 */
3154 natl = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
3155 fin->fin_src, fin->fin_dst);
3156 if (natl != NULL) {
3157 KFREE(nat);
3158 nat = natl;
3159 goto done;
3160 }
3161
3162 move = ipf_nat_newrdr(fin, nat, &ni);
3163 }
3164 if (move == -1)
3165 goto badnat;
3166
3167 np = ni.nai_np;
3168
3169 nat->nat_mssclamp = np->in_mssclamp;
3170 nat->nat_me = natsave;
3171 nat->nat_fr = fin->fin_fr;
3172 nat->nat_rev = fin->fin_rev;
3173 nat->nat_ptr = np;
3174 nat->nat_dlocal = np->in_dlocal;
3175
3176 if ((np->in_apr != NULL) && ((nat->nat_flags & NAT_SLAVE) == 0)) {
3177 if (ipf_proxy_new(fin, nat) == -1) {
3178 NBUMPSIDED(fin->fin_out, ns_appr_fail);
3179 goto badnat;
3180 }
3181 }
3182
3183 nat->nat_ifps[0] = np->in_ifps[0];
3184 if (np->in_ifps[0] != NULL) {
3185 COPYIFNAME(np->in_v[0], np->in_ifps[0], nat->nat_ifnames[0]);
3186 }
3187
3188 nat->nat_ifps[1] = np->in_ifps[1];
3189 if (np->in_ifps[1] != NULL) {
3190 COPYIFNAME(np->in_v[1], np->in_ifps[1], nat->nat_ifnames[1]);
3191 }
3192
3193 if (ipf_nat_finalise(fin, nat) == -1) {
3194 goto badnat;
3195 }
3196
3197 np->in_use++;
3198
3199 if ((move == 1) && (np->in_flags & IPN_ROUNDR)) {
3200 if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_REDIRECT) {
3201 ipf_nat_delrdr(softn, np);
3202 ipf_nat_addrdr(softn, np);
3203 } else if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_MAP) {
3204 ipf_nat_delmap(softn, np);
3205 ipf_nat_addmap(softn, np);
3206 }
3207 }
3208
3209 if (flags & SI_WILDP)
3210 nsp->ns_wilds++;
3211 nsp->ns_proto[nat->nat_pr[0]]++;
3212
3213 goto done;
3214 badnat:
3215 DT2(ns_badnatnew, fr_info_t *, fin, nat_t *, nat);
3216 NBUMPSIDE(fin->fin_out, ns_badnatnew);
3217 if ((hm = nat->nat_hm) != NULL)
3218 ipf_nat_hostmapdel(softc, &hm);
3219 KFREE(nat);
3220 nat = NULL;
3221 done:
3222 if (nat != NULL && np != NULL)
3223 np->in_hits++;
3224 if (natsave != NULL)
3225 *natsave = nat;
3226 return nat;
3227 }
3228
3229
3230 /* ------------------------------------------------------------------------ */
3231 /* Function: ipf_nat_finalise */
3232 /* Returns: int - 0 == sucess, -1 == failure */
3233 /* Parameters: fin(I) - pointer to packet information */
3234 /* nat(I) - pointer to NAT entry */
3235 /* Write Lock: ipf_nat */
3236 /* */
3237 /* This is the tail end of constructing a new NAT entry and is the same */
3238 /* for both IPv4 and IPv6. */
3239 /* ------------------------------------------------------------------------ */
3240 /*ARGSUSED*/
3241 static int
3242 ipf_nat_finalise(fr_info_t *fin, nat_t *nat)
3243 {
3244 ipf_main_softc_t *softc = fin->fin_main_soft;
3245 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3246 u_32_t sum1, sum2, sumd;
3247 frentry_t *fr;
3248 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_M_CTL_MAGIC)
3249 qpktinfo_t *qpi = fin->fin_qpi;
3250 #endif
3251
3252 switch (nat->nat_pr[0])
3253 {
3254 case IPPROTO_ICMP :
3255 sum1 = LONG_SUM(ntohs(nat->nat_oicmpid));
3256 sum2 = LONG_SUM(ntohs(nat->nat_nicmpid));
3257 CALC_SUMD(sum1, sum2, sumd);
3258 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
3259
3260 break;
3261
3262 default :
3263 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr) + \
3264 ntohs(nat->nat_osport));
3265 sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr) + \
3266 ntohs(nat->nat_nsport));
3267 CALC_SUMD(sum1, sum2, sumd);
3268 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
3269
3270 sum1 = LONG_SUM(ntohl(nat->nat_odstaddr) + \
3271 ntohs(nat->nat_odport));
3272 sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr) + \
3273 ntohs(nat->nat_ndport));
3274 CALC_SUMD(sum1, sum2, sumd);
3275 nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
3276 break;
3277 }
3278
3279 /*
3280 * Compute the partial checksum, just in case.
3281 * This is only ever placed into outbound packets so care needs
3282 * to be taken over which pair of addresses are used.
3283 */
3284 if (nat->nat_dir == NAT_OUTBOUND) {
3285 sum1 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
3286 sum1 += LONG_SUM(ntohl(nat->nat_ndstaddr));
3287 } else {
3288 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr));
3289 sum1 += LONG_SUM(ntohl(nat->nat_odstaddr));
3290 }
3291 sum1 += nat->nat_pr[1];
3292 nat->nat_sumd[1] = (sum1 & 0xffff) + (sum1 >> 16);
3293
3294 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr));
3295 sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
3296 CALC_SUMD(sum1, sum2, sumd);
3297 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
3298
3299 sum1 = LONG_SUM(ntohl(nat->nat_odstaddr));
3300 sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
3301 CALC_SUMD(sum1, sum2, sumd);
3302 nat->nat_ipsumd += (sumd & 0xffff) + (sumd >> 16);
3303
3304 nat->nat_v[0] = 4;
3305 nat->nat_v[1] = 4;
3306
3307 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
3308 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
3309 }
3310
3311 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
3312 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
3313 }
3314
3315 if ((nat->nat_flags & SI_CLONE) == 0)
3316 nat->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, nat);
3317
3318 if (ipf_nat_insert(softc, softn, nat) == 0) {
3319 if (softn->ipf_nat_logging)
3320 ipf_nat_log(softc, softn, nat, NL_NEW);
3321 fr = nat->nat_fr;
3322 if (fr != NULL) {
3323 MUTEX_ENTER(&fr->fr_lock);
3324 fr->fr_ref++;
3325 MUTEX_EXIT(&fr->fr_lock);
3326 }
3327 return 0;
3328 }
3329
3330 NBUMPSIDED(fin->fin_out, ns_unfinalised);
3331 /*
3332 * nat_insert failed, so cleanup time...
3333 */
3334 if (nat->nat_sync != NULL)
3335 ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
3336 return -1;
3337 }
3338
3339
3340 /* ------------------------------------------------------------------------ */
3341 /* Function: ipf_nat_insert */
3342 /* Returns: int - 0 == sucess, -1 == failure */
3343 /* Parameters: softc(I) - pointer to soft context main structure */
3344 /* softn(I) - pointer to NAT context structure */
3345 /* nat(I) - pointer to NAT structure */
3346 /* Write Lock: ipf_nat */
3347 /* */
3348 /* Insert a NAT entry into the hash tables for searching and add it to the */
3349 /* list of active NAT entries. Adjust global counters when complete. */
3350 /* ------------------------------------------------------------------------ */
3351 int
3352 ipf_nat_insert(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat)
3353 {
3354 u_int hv0, hv1;
3355 u_int sp, dp;
3356 ipnat_t *in;
3357 int ret;
3358
3359 /*
3360 * Try and return an error as early as possible, so calculate the hash
3361 * entry numbers first and then proceed.
3362 */
3363 if ((nat->nat_flags & (SI_W_SPORT|SI_W_DPORT)) == 0) {
3364 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
3365 sp = nat->nat_osport;
3366 dp = nat->nat_odport;
3367 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
3368 sp = 0;
3369 dp = nat->nat_oicmpid;
3370 } else {
3371 sp = 0;
3372 dp = 0;
3373 }
3374 hv0 = NAT_HASH_FN(nat->nat_osrcaddr, sp, 0xffffffff);
3375 hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0 + dp, 0xffffffff);
3376 /*
3377 * TRACE nat_osrcaddr, nat_osport, nat_odstaddr,
3378 * nat_odport, hv0
3379 */
3380
3381 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
3382 sp = nat->nat_nsport;
3383 dp = nat->nat_ndport;
3384 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
3385 sp = 0;
3386 dp = nat->nat_nicmpid;
3387 } else {
3388 sp = 0;
3389 dp = 0;
3390 }
3391 hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, sp, 0xffffffff);
3392 hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1 + dp, 0xffffffff);
3393 /*
3394 * TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr,
3395 * nat_ndport, hv1
3396 */
3397 } else {
3398 hv0 = NAT_HASH_FN(nat->nat_osrcaddr, 0, 0xffffffff);
3399 hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0, 0xffffffff);
3400 /* TRACE nat_osrcaddr, nat_odstaddr, hv0 */
3401
3402 hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, 0, 0xffffffff);
3403 hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1, 0xffffffff);
3404 /* TRACE nat_nsrcaddr, nat_ndstaddr, hv1 */
3405 }
3406
3407 if ((nat->nat_dir & NAT_OUTBOUND) == NAT_OUTBOUND) {
3408 nat->nat_hv[0] = hv0;
3409 nat->nat_hv[1] = hv1;
3410 } else {
3411 nat->nat_hv[0] = hv1;
3412 nat->nat_hv[1] = hv0;
3413 }
3414
3415 MUTEX_INIT(&nat->nat_lock, "nat entry lock");
3416
3417 in = nat->nat_ptr;
3418 nat->nat_ref = nat->nat_me ? 2 : 1;
3419
3420 nat->nat_ifnames[0][LIFNAMSIZ - 1] = '\0';
3421 nat->nat_ifps[0] = ipf_resolvenic(softc, nat->nat_ifnames[0], 4);
3422
3423 if (nat->nat_ifnames[1][0] != '\0') {
3424 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
3425 nat->nat_ifps[1] = ipf_resolvenic(softc,
3426 nat->nat_ifnames[1], 4);
3427 } else if (in->in_ifnames[1] != -1) {
3428 char *name;
3429
3430 name = in->in_names + in->in_ifnames[1];
3431 if (name[1] != '\0' && name[0] != '-' && name[0] != '*') {
3432 (void) strncpy(nat->nat_ifnames[1],
3433 nat->nat_ifnames[0], LIFNAMSIZ);
3434 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
3435 nat->nat_ifps[1] = nat->nat_ifps[0];
3436 }
3437 }
3438 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
3439 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
3440 }
3441 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
3442 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
3443 }
3444
3445 ret = ipf_nat_hashtab_add(softc, softn, nat);
3446 if (ret != 0)
3447 MUTEX_DESTROY(&nat->nat_lock);
3448 return ret;
3449 }
3450
3451
3452 /* ------------------------------------------------------------------------ */
3453 /* Function: ipf_nat_hashtab_add */
3454 /* Returns: int - 0 == sucess, -1 == failure */
3455 /* Parameters: softc(I) - pointer to soft context main structure */
3456 /* softn(I) - pointer to NAT context structure */
3457 /* nat(I) - pointer to NAT structure */
3458 /* Write Lock: ipf_nat */
3459 /* */
3460 /* Handle the insertion of a NAT entry into the table/list. */
3461 /* ------------------------------------------------------------------------ */
3462 int
3463 ipf_nat_hashtab_add(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat)
3464 {
3465 nat_t **natp;
3466 u_int hv0;
3467 u_int hv1;
3468
3469 hv0 = nat->nat_hv[0] % softn->ipf_nat_table_sz;
3470 hv1 = nat->nat_hv[1] % softn->ipf_nat_table_sz;
3471
3472 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0] >=
3473 softn->ipf_nat_maxbucket) {
3474 DT1(ns_bucket_max_0, int,
3475 softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0]);
3476 NBUMPSIDE(0, ns_bucket_max);
3477 return -1;
3478 }
3479
3480 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1] >=
3481 softn->ipf_nat_maxbucket) {
3482 DT1(ns_bucket_max_1, int,
3483 softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1]);
3484 NBUMPSIDE(1, ns_bucket_max);
3485 return -1;
3486 }
3487
3488 /*
3489 * The ordering of operations in the list and hash table insertion
3490 * is very important. The last operation for each task should be
3491 * to update the top of the list, after all the "nexts" have been
3492 * done so that walking the list while it is being done does not
3493 * find strange pointers.
3494 *
3495 * Global list of NAT instances
3496 */
3497 nat->nat_next = softn->ipf_nat_instances;
3498 nat->nat_pnext = &softn->ipf_nat_instances;
3499 if (softn->ipf_nat_instances)
3500 softn->ipf_nat_instances->nat_pnext = &nat->nat_next;
3501 softn->ipf_nat_instances = nat;
3502
3503 /*
3504 * Inbound hash table.
3505 */
3506 natp = &softn->ipf_nat_table[0][hv0];
3507 nat->nat_phnext[0] = natp;
3508 nat->nat_hnext[0] = *natp;
3509 if (*natp) {
3510 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
3511 } else {
3512 NBUMPSIDE(0, ns_inuse);
3513 }
3514 *natp = nat;
3515 NBUMPSIDE(0, ns_bucketlen[hv0]);
3516
3517 /*
3518 * Outbound hash table.
3519 */
3520 natp = &softn->ipf_nat_table[1][hv1];
3521 nat->nat_phnext[1] = natp;
3522 nat->nat_hnext[1] = *natp;
3523 if (*natp)
3524 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
3525 else {
3526 NBUMPSIDE(1, ns_inuse);
3527 }
3528 *natp = nat;
3529 NBUMPSIDE(1, ns_bucketlen[hv1]);
3530
3531 ipf_nat_setqueue(softc, softn, nat);
3532
3533 if (nat->nat_dir & NAT_OUTBOUND) {
3534 NBUMPSIDE(1, ns_added);
3535 } else {
3536 NBUMPSIDE(0, ns_added);
3537 }
3538 softn->ipf_nat_stats.ns_active++;
3539 return 0;
3540 }
3541
3542
3543 /* ------------------------------------------------------------------------ */
3544 /* Function: ipf_nat_icmperrorlookup */
3545 /* Returns: nat_t* - point to matching NAT structure */
3546 /* Parameters: fin(I) - pointer to packet information */
3547 /* dir(I) - direction of packet (in/out) */
3548 /* */
3549 /* Check if the ICMP error message is related to an existing TCP, UDP or */
3550 /* ICMP query nat entry. It is assumed that the packet is already of the */
3551 /* the required length. */
3552 /* ------------------------------------------------------------------------ */
3553 nat_t *
3554 ipf_nat_icmperrorlookup(fr_info_t *fin, int dir)
3555 {
3556 ipf_main_softc_t *softc = fin->fin_main_soft;
3557 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3558 int flags = 0, minlen;
3559 icmphdr_t *orgicmp;
3560 nat_stat_side_t *nside;
3561 tcphdr_t *tcp = NULL;
3562 u_short data[2];
3563 nat_t *nat;
3564 ip_t *oip;
3565 u_int p;
3566
3567 nside = &softn->ipf_nat_stats.ns_side[fin->fin_out];
3568 /*
3569 * Does it at least have the return (basic) IP header ?
3570 * Only a basic IP header (no options) should be with an ICMP error
3571 * header. Also, if it's not an error type, then return.
3572 */
3573 if ((fin->fin_hlen != sizeof(ip_t)) || !(fin->fin_flx & FI_ICMPERR)) {
3574 ATOMIC_INCL(nside->ns_icmp_basic);
3575 return NULL;
3576 }
3577
3578 /*
3579 * Check packet size
3580 */
3581 oip = (ip_t *)((char *)fin->fin_dp + 8);
3582 minlen = IP_HL(oip) << 2;
3583 if ((minlen < sizeof(ip_t)) ||
3584 (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen)) {
3585 ATOMIC_INCL(nside->ns_icmp_size);
3586 return NULL;
3587 }
3588
3589 /*
3590 * Is the buffer big enough for all of it ? It's the size of the IP
3591 * header claimed in the encapsulated part which is of concern. It
3592 * may be too big to be in this buffer but not so big that it's
3593 * outside the ICMP packet, leading to TCP deref's causing problems.
3594 * This is possible because we don't know how big oip_hl is when we
3595 * do the pullup early in ipf_check() and thus can't gaurantee it is
3596 * all here now.
3597 */
3598 #ifdef ipf_nat_KERNEL
3599 {
3600 mb_t *m;
3601
3602 m = fin->fin_m;
3603 # if defined(MENTAT)
3604 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
3605 (char *)m->b_wptr) {
3606 ATOMIC_INCL(nside->ns_icmp_mbuf);
3607 return NULL;
3608 }
3609 # else
3610 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN >
3611 (char *)fin->fin_ip + M_LEN(m)) {
3612 ATOMIC_INCL(nside->ns_icmp_mbuf);
3613 return NULL;
3614 }
3615 # endif
3616 }
3617 #endif
3618
3619 if (fin->fin_daddr != oip->ip_src.s_addr) {
3620 ATOMIC_INCL(nside->ns_icmp_address);
3621 return NULL;
3622 }
3623
3624 p = oip->ip_p;
3625 if (p == IPPROTO_TCP)
3626 flags = IPN_TCP;
3627 else if (p == IPPROTO_UDP)
3628 flags = IPN_UDP;
3629 else if (p == IPPROTO_ICMP) {
3630 orgicmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2));
3631
3632 /* see if this is related to an ICMP query */
3633 if (ipf_nat_icmpquerytype(orgicmp->icmp_type)) {
3634 data[0] = fin->fin_data[0];
3635 data[1] = fin->fin_data[1];
3636 fin->fin_data[0] = 0;
3637 fin->fin_data[1] = orgicmp->icmp_id;
3638
3639 flags = IPN_ICMPERR|IPN_ICMPQUERY;
3640 /*
3641 * NOTE : dir refers to the direction of the original
3642 * ip packet. By definition the icmp error
3643 * message flows in the opposite direction.
3644 */
3645 if (dir == NAT_INBOUND)
3646 nat = ipf_nat_inlookup(fin, flags, p,
3647 oip->ip_dst,
3648 oip->ip_src);
3649 else
3650 nat = ipf_nat_outlookup(fin, flags, p,
3651 oip->ip_dst,
3652 oip->ip_src);
3653 fin->fin_data[0] = data[0];
3654 fin->fin_data[1] = data[1];
3655 return nat;
3656 }
3657 }
3658
3659 if (flags & IPN_TCPUDP) {
3660 minlen += 8; /* + 64bits of data to get ports */
3661 /* TRACE (fin,minlen) */
3662 if (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen) {
3663 ATOMIC_INCL(nside->ns_icmp_short);
3664 return NULL;
3665 }
3666
3667 data[0] = fin->fin_data[0];
3668 data[1] = fin->fin_data[1];
3669 tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2));
3670 fin->fin_data[0] = ntohs(tcp->th_dport);
3671 fin->fin_data[1] = ntohs(tcp->th_sport);
3672
3673 if (dir == NAT_INBOUND) {
3674 nat = ipf_nat_inlookup(fin, flags, p, oip->ip_dst,
3675 oip->ip_src);
3676 } else {
3677 nat = ipf_nat_outlookup(fin, flags, p, oip->ip_dst,
3678 oip->ip_src);
3679 }
3680 fin->fin_data[0] = data[0];
3681 fin->fin_data[1] = data[1];
3682 return nat;
3683 }
3684 if (dir == NAT_INBOUND)
3685 nat = ipf_nat_inlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
3686 else
3687 nat = ipf_nat_outlookup(fin, 0, p, oip->ip_dst, oip->ip_src);
3688
3689 return nat;
3690 }
3691
3692
3693 /* ------------------------------------------------------------------------ */
3694 /* Function: ipf_nat_icmperror */
3695 /* Returns: nat_t* - point to matching NAT structure */
3696 /* Parameters: fin(I) - pointer to packet information */
3697 /* nflags(I) - NAT flags for this packet */
3698 /* dir(I) - direction of packet (in/out) */
3699 /* */
3700 /* Fix up an ICMP packet which is an error message for an existing NAT */
3701 /* session. This will correct both packet header data and checksums. */
3702 /* */
3703 /* This should *ONLY* be used for incoming ICMP error packets to make sure */
3704 /* a NAT'd ICMP packet gets correctly recognised. */
3705 /* ------------------------------------------------------------------------ */
3706 nat_t *
3707 ipf_nat_icmperror(fr_info_t *fin, u_int *nflags, int dir)
3708 {
3709 ipf_main_softc_t *softc = fin->fin_main_soft;
3710 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3711 u_32_t sum1, sum2, sumd, sumd2;
3712 struct in_addr a1, a2, a3, a4;
3713 int flags, dlen, odst;
3714 icmphdr_t *icmp;
3715 u_short *csump;
3716 tcphdr_t *tcp;
3717 nat_t *nat;
3718 ip_t *oip;
3719 void *dp;
3720
3721 if ((fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
3722 NBUMPSIDED(fin->fin_out, ns_icmp_short);
3723 return NULL;
3724 }
3725
3726 /*
3727 * ipf_nat_icmperrorlookup() will return NULL for `defective' packets.
3728 */
3729 if ((fin->fin_v != 4) || !(nat = ipf_nat_icmperrorlookup(fin, dir))) {
3730 NBUMPSIDED(fin->fin_out, ns_icmp_notfound);
3731 return NULL;
3732 }
3733
3734 tcp = NULL;
3735 csump = NULL;
3736 flags = 0;
3737 sumd2 = 0;
3738 *nflags = IPN_ICMPERR;
3739 icmp = fin->fin_dp;
3740 oip = (ip_t *)&icmp->icmp_ip;
3741 dp = (((char *)oip) + (IP_HL(oip) << 2));
3742 if (oip->ip_p == IPPROTO_TCP) {
3743 tcp = (tcphdr_t *)dp;
3744 csump = (u_short *)&tcp->th_sum;
3745 flags = IPN_TCP;
3746 } else if (oip->ip_p == IPPROTO_UDP) {
3747 udphdr_t *udp;
3748
3749 udp = (udphdr_t *)dp;
3750 tcp = (tcphdr_t *)dp;
3751 csump = (u_short *)&udp->uh_sum;
3752 flags = IPN_UDP;
3753 } else if (oip->ip_p == IPPROTO_ICMP)
3754 flags = IPN_ICMPQUERY;
3755 dlen = fin->fin_plen - ((char *)dp - (char *)fin->fin_ip);
3756
3757 /*
3758 * Need to adjust ICMP header to include the real IP#'s and
3759 * port #'s. Only apply a checksum change relative to the
3760 * IP address change as it will be modified again in ipf_nat_checkout
3761 * for both address and port. Two checksum changes are
3762 * necessary for the two header address changes. Be careful
3763 * to only modify the checksum once for the port # and twice
3764 * for the IP#.
3765 */
3766
3767 /*
3768 * Step 1
3769 * Fix the IP addresses in the offending IP packet. You also need
3770 * to adjust the IP header checksum of that offending IP packet.
3771 *
3772 * Normally, you would expect that the ICMP checksum of the
3773 * ICMP error message needs to be adjusted as well for the
3774 * IP address change in oip.
3775 * However, this is a NOP, because the ICMP checksum is
3776 * calculated over the complete ICMP packet, which includes the
3777 * changed oip IP addresses and oip->ip_sum. However, these
3778 * two changes cancel each other out (if the delta for
3779 * the IP address is x, then the delta for ip_sum is minus x),
3780 * so no change in the icmp_cksum is necessary.
3781 *
3782 * Inbound ICMP
3783 * ------------
3784 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
3785 * - response to outgoing packet (a,b)=>(c,b) (OIP_SRC=c,OIP_DST=b)
3786 * - OIP_SRC(c)=nat_newsrcip, OIP_DST(b)=nat_newdstip
3787 *=> OIP_SRC(c)=nat_oldsrcip, OIP_DST(b)=nat_olddstip
3788 *
3789 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
3790 * - response to outgoing packet (c,a)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3791 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip
3792 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3793 *
3794 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
3795 * - response to outgoing packet (a,b)=>(c,d) (OIP_SRC=c,OIP_DST=d)
3796 * - OIP_SRC(c)=nat_newsrcip, OIP_DST(d)=nat_newdstip
3797 *=> OIP_SRC(c)=nat_oldsrcip, OIP_DST(d)=nat_olddstip
3798 *
3799 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
3800 * - response to outgoing packet (d,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3801 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip
3802 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3803 *
3804 * Outbound ICMP
3805 * -------------
3806 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
3807 * - response to incoming packet (b,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
3808 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip
3809 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3810 *
3811 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
3812 * - response to incoming packet (a,b)=>(a,c) (OIP_SRC=a,OIP_DST=c)
3813 * - OIP_SRC(a)=nat_newsrcip, OIP_DST(c)=nat_newdstip
3814 *=> OIP_SRC(a)=nat_oldsrcip, OIP_DST(c)=nat_olddstip
3815 *
3816 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
3817 * - response to incoming packet (d,c)=>(b,a) (OIP_SRC=c,OIP_DST=d)
3818 * - OIP_SRC(c)=nat_olddstip, OIP_DST(d)=nat_oldsrcip
3819 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip
3820 *
3821 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
3822 * - response to incoming packet (a,b)=>(c,d) (OIP_SRC=b,OIP_DST=a)
3823 * - OIP_SRC(b)=nat_newsrcip, OIP_DST(a)=nat_newdstip
3824 *=> OIP_SRC(a)=nat_oldsrcip, OIP_DST(c)=nat_olddstip
3825 */
3826
3827 if (((fin->fin_out == 0) && ((nat->nat_redir & NAT_MAP) != 0)) ||
3828 ((fin->fin_out == 1) && ((nat->nat_redir & NAT_REDIRECT) != 0))) {
3829 a1.s_addr = ntohl(nat->nat_osrcaddr);
3830 a4.s_addr = ntohl(oip->ip_src.s_addr);
3831 a3.s_addr = ntohl(nat->nat_odstaddr);
3832 a2.s_addr = ntohl(oip->ip_dst.s_addr);
3833 oip->ip_src.s_addr = htonl(a1.s_addr);
3834 oip->ip_dst.s_addr = htonl(a3.s_addr);
3835 odst = 1;
3836 } else {
3837 a1.s_addr = ntohl(nat->nat_ndstaddr);
3838 a2.s_addr = ntohl(oip->ip_dst.s_addr);
3839 a3.s_addr = ntohl(nat->nat_nsrcaddr);
3840 a4.s_addr = ntohl(oip->ip_src.s_addr);
3841 oip->ip_dst.s_addr = htonl(a3.s_addr);
3842 oip->ip_src.s_addr = htonl(a1.s_addr);
3843 odst = 0;
3844 }
3845 sum1 = 0;
3846 sum2 = 0;
3847 sumd = 0;
3848 CALC_SUMD(a2.s_addr, a3.s_addr, sum1);
3849 CALC_SUMD(a4.s_addr, a1.s_addr, sum2);
3850 sumd = sum2 + sum1;
3851 if (sumd != 0)
3852 ipf_fix_datacksum(&oip->ip_sum, sumd);
3853
3854 sumd2 = sumd;
3855 sum1 = 0;
3856 sum2 = 0;
3857
3858 /*
3859 * Fix UDP pseudo header checksum to compensate for the
3860 * IP address change.
3861 */
3862 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
3863 u_32_t sum3, sum4, sumt;
3864
3865 /*
3866 * Step 2 :
3867 * For offending TCP/UDP IP packets, translate the ports as
3868 * well, based on the NAT specification. Of course such
3869 * a change may be reflected in the ICMP checksum as well.
3870 *
3871 * Since the port fields are part of the TCP/UDP checksum
3872 * of the offending IP packet, you need to adjust that checksum
3873 * as well... except that the change in the port numbers should
3874 * be offset by the checksum change. However, the TCP/UDP
3875 * checksum will also need to change if there has been an
3876 * IP address change.
3877 */
3878 if (odst == 1) {
3879 sum1 = ntohs(nat->nat_osport);
3880 sum4 = ntohs(tcp->th_sport);
3881 sum3 = ntohs(nat->nat_odport);
3882 sum2 = ntohs(tcp->th_dport);
3883
3884 tcp->th_sport = htons(sum1);
3885 tcp->th_dport = htons(sum3);
3886 } else {
3887 sum1 = ntohs(nat->nat_ndport);
3888 sum2 = ntohs(tcp->th_dport);
3889 sum3 = ntohs(nat->nat_nsport);
3890 sum4 = ntohs(tcp->th_sport);
3891
3892 tcp->th_dport = htons(sum3);
3893 tcp->th_sport = htons(sum1);
3894 }
3895 CALC_SUMD(sum4, sum1, sumt);
3896 sumd += sumt;
3897 CALC_SUMD(sum2, sum3, sumt);
3898 sumd += sumt;
3899
3900 if (sumd != 0 || sumd2 != 0) {
3901 /*
3902 * At this point, sumd is the delta to apply to the
3903 * TCP/UDP header, given the changes in both the IP
3904 * address and the ports and sumd2 is the delta to
3905 * apply to the ICMP header, given the IP address
3906 * change delta that may need to be applied to the
3907 * TCP/UDP checksum instead.
3908 *
3909 * If we will both the IP and TCP/UDP checksums
3910 * then the ICMP checksum changes by the address
3911 * delta applied to the TCP/UDP checksum. If we
3912 * do not change the TCP/UDP checksum them we
3913 * apply the delta in ports to the ICMP checksum.
3914 */
3915 if (oip->ip_p == IPPROTO_UDP) {
3916 if ((dlen >= 8) && (*csump != 0)) {
3917 ipf_fix_datacksum(csump, sumd);
3918 } else {
3919 CALC_SUMD(sum1, sum4, sumd2);
3920 CALC_SUMD(sum3, sum2, sumt);
3921 sumd2 += sumt;
3922 }
3923 } else if (oip->ip_p == IPPROTO_TCP) {
3924 if (dlen >= 18) {
3925 ipf_fix_datacksum(csump, sumd);
3926 } else {
3927 CALC_SUMD(sum1, sum4, sumd2);
3928 CALC_SUMD(sum3, sum2, sumt);
3929 sumd2 += sumt;
3930 }
3931 }
3932 if (sumd2 != 0) {
3933 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
3934 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
3935 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
3936 ipf_fix_incksum(0, &icmp->icmp_cksum, sumd2, 0);
3937 }
3938 }
3939 } else if (((flags & IPN_ICMPQUERY) != 0) && (dlen >= 8)) {
3940 icmphdr_t *orgicmp;
3941
3942 /*
3943 * XXX - what if this is bogus hl and we go off the end ?
3944 * In this case, ipf_nat_icmperrorlookup() will have
3945 * returned NULL.
3946 */
3947 orgicmp = (icmphdr_t *)dp;
3948
3949 if (odst == 1) {
3950 if (orgicmp->icmp_id != nat->nat_osport) {
3951
3952 /*
3953 * Fix ICMP checksum (of the offening ICMP
3954 * query packet) to compensate the change
3955 * in the ICMP id of the offending ICMP
3956 * packet.
3957 *
3958 * Since you modify orgicmp->icmp_id with
3959 * a delta (say x) and you compensate that
3960 * in origicmp->icmp_cksum with a delta
3961 * minus x, you don't have to adjust the
3962 * overall icmp->icmp_cksum
3963 */
3964 sum1 = ntohs(orgicmp->icmp_id);
3965 sum2 = ntohs(nat->nat_oicmpid);
3966 CALC_SUMD(sum1, sum2, sumd);
3967 orgicmp->icmp_id = nat->nat_oicmpid;
3968 ipf_fix_datacksum(&orgicmp->icmp_cksum, sumd);
3969 }
3970 } /* nat_dir == NAT_INBOUND is impossible for icmp queries */
3971 }
3972 return nat;
3973 }
3974
3975
3976 /*
3977 * MAP-IN MAP-OUT RDR-IN RDR-OUT
3978 * osrc X == src == src X
3979 * odst X == dst == dst X
3980 * nsrc == dst X X == dst
3981 * ndst == src X X == src
3982 * MAP = NAT_OUTBOUND, RDR = NAT_INBOUND
3983 */
3984 /*
3985 * NB: these lookups don't lock access to the list, it assumed that it has
3986 * already been done!
3987 */
3988 /* ------------------------------------------------------------------------ */
3989 /* Function: ipf_nat_inlookup */
3990 /* Returns: nat_t* - NULL == no match, */
3991 /* else pointer to matching NAT entry */
3992 /* Parameters: fin(I) - pointer to packet information */
3993 /* flags(I) - NAT flags for this packet */
3994 /* p(I) - protocol for this packet */
3995 /* src(I) - source IP address */
3996 /* mapdst(I) - destination IP address */
3997 /* */
3998 /* Lookup a nat entry based on the mapped destination ip address/port and */
3999 /* real source address/port. We use this lookup when receiving a packet, */
4000 /* we're looking for a table entry, based on the destination address. */
4001 /* */
4002 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */
4003 /* */
4004 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */
4005 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */
4006 /* */
4007 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */
4008 /* the packet is of said protocol */
4009 /* ------------------------------------------------------------------------ */
4010 nat_t *
4011 ipf_nat_inlookup(fr_info_t *fin, u_int flags, u_int p, struct in_addr src,
4012 struct in_addr mapdst)
4013 {
4014 ipf_main_softc_t *softc = fin->fin_main_soft;
4015 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4016 u_short sport, dport;
4017 ipnat_t *ipn;
4018 nat_t *nat;
4019 int nflags;
4020 u_32_t dst;
4021 void *ifp;
4022 u_int hv, rhv;
4023
4024 ifp = fin->fin_ifp;
4025 dst = mapdst.s_addr;
4026
4027 switch (p)
4028 {
4029 case IPPROTO_TCP :
4030 case IPPROTO_UDP :
4031 sport = htons(fin->fin_data[0]);
4032 dport = htons(fin->fin_data[1]);
4033 break;
4034 case IPPROTO_ICMP :
4035 sport = 0;
4036 dport = fin->fin_data[1];
4037 break;
4038 default :
4039 sport = 0;
4040 dport = 0;
4041 break;
4042 }
4043
4044
4045 if ((flags & SI_WILDP) != 0)
4046 goto find_in_wild_ports;
4047
4048 rhv = NAT_HASH_FN(dst, dport, 0xffffffff);
4049 rhv = NAT_HASH_FN(src.s_addr, rhv + sport, 0xffffffff);
4050 hv = rhv % softn->ipf_nat_table_sz;
4051 nat = softn->ipf_nat_table[1][hv];
4052 /* TRACE dst, dport, src, sport, hv, nat */
4053
4054 for (; nat; nat = nat->nat_hnext[1]) {
4055 if (nat->nat_ifps[0] != NULL) {
4056 if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
4057 continue;
4058 }
4059
4060 if (nat->nat_pr[0] != p)
4061 continue;
4062
4063 switch (nat->nat_dir)
4064 {
4065 case NAT_INBOUND :
4066 case NAT_DIVERTIN :
4067 if (nat->nat_v[0] != 4)
4068 continue;
4069 if (nat->nat_osrcaddr != src.s_addr ||
4070 nat->nat_odstaddr != dst)
4071 continue;
4072 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4073 if (nat->nat_osport != sport)
4074 continue;
4075 if (nat->nat_odport != dport)
4076 continue;
4077
4078 } else if (p == IPPROTO_ICMP) {
4079 if (nat->nat_oicmpid != dport) {
4080 continue;
4081 }
4082 }
4083 break;
4084 case NAT_DIVERTOUT :
4085 if (nat->nat_dlocal)
4086 continue;
4087 case NAT_OUTBOUND :
4088 if (nat->nat_v[1] != 4)
4089 continue;
4090 if (nat->nat_dlocal)
4091 continue;
4092 if (nat->nat_dlocal)
4093 continue;
4094 if (nat->nat_ndstaddr != src.s_addr ||
4095 nat->nat_nsrcaddr != dst)
4096 continue;
4097 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4098 if (nat->nat_ndport != sport)
4099 continue;
4100 if (nat->nat_nsport != dport)
4101 continue;
4102
4103 } else if (p == IPPROTO_ICMP) {
4104 if (nat->nat_nicmpid != dport) {
4105 continue;
4106 }
4107 }
4108 break;
4109 }
4110
4111
4112 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4113 ipn = nat->nat_ptr;
4114 if ((ipn != NULL) && (nat->nat_aps != NULL))
4115 if (ipf_proxy_match(fin, nat) != 0)
4116 continue;
4117 }
4118 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
4119 nat->nat_ifps[0] = ifp;
4120 nat->nat_mtu[0] = GETIFMTU_4(ifp);
4121 }
4122 return nat;
4123 }
4124
4125 /*
4126 * So if we didn't find it but there are wildcard members in the hash
4127 * table, go back and look for them. We do this search and update here
4128 * because it is modifying the NAT table and we want to do this only
4129 * for the first packet that matches. The exception, of course, is
4130 * for "dummy" (FI_IGNORE) lookups.
4131 */
4132 find_in_wild_ports:
4133 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
4134 NBUMPSIDEX(0, ns_lookup_miss, ns_lookup_miss_0);
4135 return NULL;
4136 }
4137 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
4138 NBUMPSIDEX(0, ns_lookup_nowild, ns_lookup_nowild_0);
4139 return NULL;
4140 }
4141
4142 RWLOCK_EXIT(&softc->ipf_nat);
4143
4144 hv = NAT_HASH_FN(dst, 0, 0xffffffff);
4145 hv = NAT_HASH_FN(src.s_addr, hv, softn->ipf_nat_table_sz);
4146 WRITE_ENTER(&softc->ipf_nat);
4147
4148 nat = softn->ipf_nat_table[1][hv];
4149 /* TRACE dst, src, hv, nat */
4150 for (; nat; nat = nat->nat_hnext[1]) {
4151 if (nat->nat_ifps[0] != NULL) {
4152 if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
4153 continue;
4154 }
4155
4156 if (nat->nat_pr[0] != fin->fin_p)
4157 continue;
4158
4159 switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
4160 {
4161 case NAT_INBOUND :
4162 if (nat->nat_v[0] != 4)
4163 continue;
4164 if (nat->nat_osrcaddr != src.s_addr ||
4165 nat->nat_odstaddr != dst)
4166 continue;
4167 break;
4168 case NAT_OUTBOUND :
4169 if (nat->nat_v[1] != 4)
4170 continue;
4171 if (nat->nat_ndstaddr != src.s_addr ||
4172 nat->nat_nsrcaddr != dst)
4173 continue;
4174 break;
4175 }
4176
4177 nflags = nat->nat_flags;
4178 if (!(nflags & (NAT_TCPUDP|SI_WILDP)))
4179 continue;
4180
4181 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nflags,
4182 NAT_INBOUND) == 1) {
4183 if ((fin->fin_flx & FI_IGNORE) != 0)
4184 break;
4185 if ((nflags & SI_CLONE) != 0) {
4186 nat = ipf_nat_clone(fin, nat);
4187 if (nat == NULL)
4188 break;
4189 } else {
4190 MUTEX_ENTER(&softn->ipf_nat_new);
4191 softn->ipf_nat_stats.ns_wilds--;
4192 MUTEX_EXIT(&softn->ipf_nat_new);
4193 }
4194
4195 if (nat->nat_dir == NAT_INBOUND) {
4196 if (nat->nat_osport == 0) {
4197 nat->nat_osport = sport;
4198 nat->nat_nsport = sport;
4199 }
4200 if (nat->nat_odport == 0) {
4201 nat->nat_odport = dport;
4202 nat->nat_ndport = dport;
4203 }
4204 } else if (nat->nat_dir == NAT_OUTBOUND) {
4205 if (nat->nat_osport == 0) {
4206 nat->nat_osport = dport;
4207 nat->nat_nsport = dport;
4208 }
4209 if (nat->nat_odport == 0) {
4210 nat->nat_odport = sport;
4211 nat->nat_ndport = sport;
4212 }
4213 }
4214 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
4215 nat->nat_ifps[0] = ifp;
4216 nat->nat_mtu[0] = GETIFMTU_4(ifp);
4217 }
4218 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
4219 ipf_nat_tabmove(softn, nat);
4220 break;
4221 }
4222 }
4223
4224 MUTEX_DOWNGRADE(&softc->ipf_nat);
4225
4226 if (nat == NULL) {
4227 NBUMPSIDE(0, ns_lookup_miss);
4228 }
4229 return nat;
4230 }
4231
4232
4233 /* ------------------------------------------------------------------------ */
4234 /* Function: ipf_nat_tabmove */
4235 /* Returns: Nil */
4236 /* Parameters: softn(I) - pointer to NAT context structure */
4237 /* nat(I) - pointer to NAT structure */
4238 /* Write Lock: ipf_nat */
4239 /* */
4240 /* This function is only called for TCP/UDP NAT table entries where the */
4241 /* original was placed in the table without hashing on the ports and we now */
4242 /* want to include hashing on port numbers. */
4243 /* ------------------------------------------------------------------------ */
4244 static void
4245 ipf_nat_tabmove(ipf_nat_softc_t *softn, nat_t *nat)
4246 {
4247 u_int hv0, hv1, rhv0, rhv1;
4248 natstat_t *nsp;
4249 nat_t **natp;
4250
4251 if (nat->nat_flags & SI_CLONE)
4252 return;
4253
4254 nsp = &softn->ipf_nat_stats;
4255 /*
4256 * Remove the NAT entry from the old location
4257 */
4258 if (nat->nat_hnext[0])
4259 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
4260 *nat->nat_phnext[0] = nat->nat_hnext[0];
4261 hv0 = nat->nat_hv[0] % softn->ipf_nat_table_sz;
4262 hv1 = nat->nat_hv[1] % softn->ipf_nat_table_sz;
4263
4264 ASSERT(nsp->ns_side[0].ns_bucketlen[hv0] > 0);
4265 nsp->ns_side[0].ns_bucketlen[hv0]--;
4266
4267 if (nat->nat_hnext[1])
4268 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
4269 *nat->nat_phnext[1] = nat->nat_hnext[1];
4270 ASSERT(nsp->ns_side[1].ns_bucketlen[hv1] > 0);
4271 nsp->ns_side[1].ns_bucketlen[hv1]--;
4272
4273 /*
4274 * Add into the NAT table in the new position
4275 */
4276 rhv0 = NAT_HASH_FN(nat->nat_osrcaddr, nat->nat_osport, 0xffffffff);
4277 rhv0 = NAT_HASH_FN(nat->nat_odstaddr, rhv0 + nat->nat_odport,
4278 0xffffffff);
4279 rhv1 = NAT_HASH_FN(nat->nat_nsrcaddr, nat->nat_nsport, 0xffffffff);
4280 rhv1 = NAT_HASH_FN(nat->nat_ndstaddr, rhv1 + nat->nat_ndport,
4281 0xffffffff);
4282
4283 if ((nat->nat_dir & NAT_OUTBOUND) == NAT_OUTBOUND) {
4284 nat->nat_hv[0] = rhv0;
4285 nat->nat_hv[1] = rhv1;
4286 } else {
4287 nat->nat_hv[0] = rhv1;
4288 nat->nat_hv[1] = rhv0;
4289 }
4290
4291 hv0 = nat->nat_hv[0] % softn->ipf_nat_table_sz;
4292 hv1 = nat->nat_hv[1] % softn->ipf_nat_table_sz;
4293
4294 /* TRACE nat_osrcaddr, nat_osport, nat_odstaddr, nat_odport, hv0 */
4295 /* TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr, nat_ndport, hv1 */
4296
4297 natp = &softn->ipf_nat_table[0][hv0];
4298 if (*natp)
4299 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
4300 nat->nat_phnext[0] = natp;
4301 nat->nat_hnext[0] = *natp;
4302 *natp = nat;
4303 nsp->ns_side[0].ns_bucketlen[hv0]++;
4304
4305 natp = &softn->ipf_nat_table[1][hv1];
4306 if (*natp)
4307 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
4308 nat->nat_phnext[1] = natp;
4309 nat->nat_hnext[1] = *natp;
4310 *natp = nat;
4311 nsp->ns_side[1].ns_bucketlen[hv1]++;
4312 }
4313
4314
4315 /* ------------------------------------------------------------------------ */
4316 /* Function: ipf_nat_outlookup */
4317 /* Returns: nat_t* - NULL == no match, */
4318 /* else pointer to matching NAT entry */
4319 /* Parameters: fin(I) - pointer to packet information */
4320 /* flags(I) - NAT flags for this packet */
4321 /* p(I) - protocol for this packet */
4322 /* src(I) - source IP address */
4323 /* dst(I) - destination IP address */
4324 /* rw(I) - 1 == write lock on held, 0 == read lock. */
4325 /* */
4326 /* Lookup a nat entry based on the source 'real' ip address/port and */
4327 /* destination address/port. We use this lookup when sending a packet out, */
4328 /* we're looking for a table entry, based on the source address. */
4329 /* */
4330 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */
4331 /* */
4332 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */
4333 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */
4334 /* */
4335 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */
4336 /* the packet is of said protocol */
4337 /* ------------------------------------------------------------------------ */
4338 nat_t *
4339 ipf_nat_outlookup(fr_info_t *fin, u_int flags, u_int p, struct in_addr src,
4340 struct in_addr dst)
4341 {
4342 ipf_main_softc_t *softc = fin->fin_main_soft;
4343 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4344 u_short sport, dport;
4345 ipnat_t *ipn;
4346 nat_t *nat;
4347 void *ifp;
4348 u_int hv;
4349
4350 ifp = fin->fin_ifp;
4351
4352 switch (p)
4353 {
4354 case IPPROTO_TCP :
4355 case IPPROTO_UDP :
4356 sport = htons(fin->fin_data[0]);
4357 dport = htons(fin->fin_data[1]);
4358 break;
4359 case IPPROTO_ICMP :
4360 sport = 0;
4361 dport = fin->fin_data[1];
4362 break;
4363 default :
4364 sport = 0;
4365 dport = 0;
4366 break;
4367 }
4368
4369 if ((flags & SI_WILDP) != 0)
4370 goto find_out_wild_ports;
4371
4372 hv = NAT_HASH_FN(src.s_addr, sport, 0xffffffff);
4373 hv = NAT_HASH_FN(dst.s_addr, hv + dport, softn->ipf_nat_table_sz);
4374 nat = softn->ipf_nat_table[0][hv];
4375
4376 /* TRACE src, sport, dst, dport, hv, nat */
4377
4378 for (; nat; nat = nat->nat_hnext[0]) {
4379 if (nat->nat_ifps[1] != NULL) {
4380 if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
4381 continue;
4382 }
4383
4384 if (nat->nat_pr[1] != p)
4385 continue;
4386
4387 switch (nat->nat_dir)
4388 {
4389 case NAT_INBOUND :
4390 case NAT_DIVERTIN :
4391 if (nat->nat_v[1] != 4)
4392 continue;
4393 if (nat->nat_ndstaddr != src.s_addr ||
4394 nat->nat_nsrcaddr != dst.s_addr)
4395 continue;
4396
4397 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4398 if (nat->nat_ndport != sport)
4399 continue;
4400 if (nat->nat_nsport != dport)
4401 continue;
4402
4403 } else if (p == IPPROTO_ICMP) {
4404 if (nat->nat_nicmpid != dport) {
4405 continue;
4406 }
4407 }
4408 break;
4409 case NAT_OUTBOUND :
4410 case NAT_DIVERTOUT :
4411 if (nat->nat_v[0] != 4)
4412 continue;
4413 if (nat->nat_osrcaddr != src.s_addr ||
4414 nat->nat_odstaddr != dst.s_addr)
4415 continue;
4416
4417 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
4418 if (nat->nat_odport != dport)
4419 continue;
4420 if (nat->nat_osport != sport)
4421 continue;
4422
4423 } else if (p == IPPROTO_ICMP) {
4424 if (nat->nat_oicmpid != dport) {
4425 continue;
4426 }
4427 }
4428 break;
4429 }
4430
4431 ipn = nat->nat_ptr;
4432 if ((ipn != NULL) && (nat->nat_aps != NULL))
4433 if (ipf_proxy_match(fin, nat) != 0)
4434 continue;
4435
4436 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
4437 nat->nat_ifps[1] = ifp;
4438 nat->nat_mtu[1] = GETIFMTU_4(ifp);
4439 }
4440 return nat;
4441 }
4442
4443 /*
4444 * So if we didn't find it but there are wildcard members in the hash
4445 * table, go back and look for them. We do this search and update here
4446 * because it is modifying the NAT table and we want to do this only
4447 * for the first packet that matches. The exception, of course, is
4448 * for "dummy" (FI_IGNORE) lookups.
4449 */
4450 find_out_wild_ports:
4451 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
4452 NBUMPSIDEX(1, ns_lookup_miss, ns_lookup_miss_1);
4453 return NULL;
4454 }
4455 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
4456 NBUMPSIDEX(1, ns_lookup_nowild, ns_lookup_nowild_1);
4457 return NULL;
4458 }
4459
4460 RWLOCK_EXIT(&softc->ipf_nat);
4461
4462 hv = NAT_HASH_FN(src.s_addr, 0, 0xffffffff);
4463 hv = NAT_HASH_FN(dst.s_addr, hv, softn->ipf_nat_table_sz);
4464
4465 WRITE_ENTER(&softc->ipf_nat);
4466
4467 nat = softn->ipf_nat_table[0][hv];
4468 for (; nat; nat = nat->nat_hnext[0]) {
4469 if (nat->nat_ifps[1] != NULL) {
4470 if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
4471 continue;
4472 }
4473
4474 if (nat->nat_pr[1] != fin->fin_p)
4475 continue;
4476
4477 switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))
4478 {
4479 case NAT_INBOUND :
4480 if (nat->nat_v[1] != 4)
4481 continue;
4482 if (nat->nat_ndstaddr != src.s_addr ||
4483 nat->nat_nsrcaddr != dst.s_addr)
4484 continue;
4485 break;
4486 case NAT_OUTBOUND :
4487 if (nat->nat_v[0] != 4)
4488 continue;
4489 if (nat->nat_osrcaddr != src.s_addr ||
4490 nat->nat_odstaddr != dst.s_addr)
4491 continue;
4492 break;
4493 }
4494
4495 if (!(nat->nat_flags & (NAT_TCPUDP|SI_WILDP)))
4496 continue;
4497
4498 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nat->nat_flags,
4499 NAT_OUTBOUND) == 1) {
4500 if ((fin->fin_flx & FI_IGNORE) != 0)
4501 break;
4502 if ((nat->nat_flags & SI_CLONE) != 0) {
4503 nat = ipf_nat_clone(fin, nat);
4504 if (nat == NULL)
4505 break;
4506 } else {
4507 MUTEX_ENTER(&softn->ipf_nat_new);
4508 softn->ipf_nat_stats.ns_wilds--;
4509 MUTEX_EXIT(&softn->ipf_nat_new);
4510 }
4511
4512 if (nat->nat_dir == NAT_OUTBOUND) {
4513 if (nat->nat_osport == 0) {
4514 nat->nat_osport = sport;
4515 nat->nat_nsport = sport;
4516 }
4517 if (nat->nat_odport == 0) {
4518 nat->nat_odport = dport;
4519 nat->nat_ndport = dport;
4520 }
4521 } else if (nat->nat_dir == NAT_INBOUND) {
4522 if (nat->nat_osport == 0) {
4523 nat->nat_osport = dport;
4524 nat->nat_nsport = dport;
4525 }
4526 if (nat->nat_odport == 0) {
4527 nat->nat_odport = sport;
4528 nat->nat_ndport = sport;
4529 }
4530 }
4531 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
4532 nat->nat_ifps[1] = ifp;
4533 nat->nat_mtu[1] = GETIFMTU_4(ifp);
4534 }
4535 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
4536 ipf_nat_tabmove(softn, nat);
4537 break;
4538 }
4539 }
4540
4541 MUTEX_DOWNGRADE(&softc->ipf_nat);
4542
4543 if (nat == NULL) {
4544 NBUMPSIDE(1, ns_lookup_miss);
4545 }
4546 return nat;
4547 }
4548
4549
4550 /* ------------------------------------------------------------------------ */
4551 /* Function: ipf_nat_lookupredir */
4552 /* Returns: nat_t* - NULL == no match, */
4553 /* else pointer to matching NAT entry */
4554 /* Parameters: np(I) - pointer to description of packet to find NAT table */
4555 /* entry for. */
4556 /* */
4557 /* Lookup the NAT tables to search for a matching redirect */
4558 /* The contents of natlookup_t should imitate those found in a packet that */
4559 /* would be translated - ie a packet coming in for RDR or going out for MAP.*/
4560 /* We can do the lookup in one of two ways, imitating an inbound or */
4561 /* outbound packet. By default we assume outbound, unless IPN_IN is set. */
4562 /* For IN, the fields are set as follows: */
4563 /* nl_real* = source information */
4564 /* nl_out* = destination information (translated) */
4565 /* For an out packet, the fields are set like this: */
4566 /* nl_in* = source information (untranslated) */
4567 /* nl_out* = destination information (translated) */
4568 /* ------------------------------------------------------------------------ */
4569 nat_t *
4570 ipf_nat_lookupredir(ipf_main_softc_t *softc, natlookup_t *np)
4571 {
4572 fr_info_t fi;
4573 nat_t *nat;
4574
4575 bzero((char *)&fi, sizeof(fi));
4576 fi.fin_main_soft = softc;
4577 if (np->nl_flags & IPN_IN) {
4578 fi.fin_data[0] = ntohs(np->nl_realport);
4579 fi.fin_data[1] = ntohs(np->nl_outport);
4580 } else {
4581 fi.fin_data[0] = ntohs(np->nl_inport);
4582 fi.fin_data[1] = ntohs(np->nl_outport);
4583 }
4584 if (np->nl_flags & IPN_TCP)
4585 fi.fin_p = IPPROTO_TCP;
4586 else if (np->nl_flags & IPN_UDP)
4587 fi.fin_p = IPPROTO_UDP;
4588 else if (np->nl_flags & (IPN_ICMPERR|IPN_ICMPQUERY))
4589 fi.fin_p = IPPROTO_ICMP;
4590
4591 /*
4592 * We can do two sorts of lookups:
4593 * - IPN_IN: we have the `real' and `out' address, look for `in'.
4594 * - default: we have the `in' and `out' address, look for `real'.
4595 */
4596 if (np->nl_flags & IPN_IN) {
4597 if ((nat = ipf_nat_inlookup(&fi, np->nl_flags, fi.fin_p,
4598 np->nl_realip, np->nl_outip))) {
4599 np->nl_inip = nat->nat_odstip;
4600 np->nl_inport = nat->nat_odport;
4601 }
4602 } else {
4603 /*
4604 * If nl_inip is non null, this is a lookup based on the real
4605 * ip address. Else, we use the fake.
4606 */
4607 if ((nat = ipf_nat_outlookup(&fi, np->nl_flags, fi.fin_p,
4608 np->nl_inip, np->nl_outip))) {
4609
4610 if ((np->nl_flags & IPN_FINDFORWARD) != 0) {
4611 fr_info_t fin;
4612 bzero((char *)&fin, sizeof(fin));
4613 fin.fin_p = nat->nat_pr[0];
4614 fin.fin_data[0] = ntohs(nat->nat_ndport);
4615 fin.fin_data[1] = ntohs(nat->nat_nsport);
4616 if (ipf_nat_inlookup(&fin, np->nl_flags,
4617 fin.fin_p, nat->nat_ndstip,
4618 nat->nat_nsrcip) != NULL) {
4619 np->nl_flags &= ~IPN_FINDFORWARD;
4620 }
4621 }
4622
4623 np->nl_realip = nat->nat_odstip;
4624 np->nl_realport = nat->nat_odport;
4625 }
4626 }
4627
4628 return nat;
4629 }
4630
4631
4632 /* ------------------------------------------------------------------------ */
4633 /* Function: ipf_nat_match */
4634 /* Returns: int - 0 == no match, 1 == match */
4635 /* Parameters: fin(I) - pointer to packet information */
4636 /* np(I) - pointer to NAT rule */
4637 /* */
4638 /* Pull the matching of a packet against a NAT rule out of that complex */
4639 /* loop inside ipf_nat_checkin() and lay it out properly in its own function. */
4640 /* ------------------------------------------------------------------------ */
4641 static int
4642 ipf_nat_match(fr_info_t *fin, ipnat_t *np)
4643 {
4644 ipf_main_softc_t *softc = fin->fin_main_soft;
4645 frtuc_t *ft;
4646 int match;
4647
4648 match = 0;
4649 switch (np->in_osrcatype)
4650 {
4651 case FRI_NORMAL :
4652 match = ((fin->fin_saddr & np->in_osrcmsk) != np->in_osrcaddr);
4653 break;
4654 case FRI_LOOKUP :
4655 match = (*np->in_osrcfunc)(softc, np->in_osrcptr,
4656 4, &fin->fin_saddr, fin->fin_plen);
4657 break;
4658 }
4659 match ^= ((np->in_flags & IPN_NOTSRC) != 0);
4660 if (match)
4661 return 0;
4662
4663 match = 0;
4664 switch (np->in_odstatype)
4665 {
4666 case FRI_NORMAL :
4667 match = ((fin->fin_daddr & np->in_odstmsk) != np->in_odstaddr);
4668 break;
4669 case FRI_LOOKUP :
4670 match = (*np->in_odstfunc)(softc, np->in_odstptr,
4671 4, &fin->fin_daddr, fin->fin_plen);
4672 break;
4673 }
4674
4675 match ^= ((np->in_flags & IPN_NOTDST) != 0);
4676 if (match)
4677 return 0;
4678
4679 ft = &np->in_tuc;
4680 if (!(fin->fin_flx & FI_TCPUDP) ||
4681 (fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
4682 if (ft->ftu_scmp || ft->ftu_dcmp)
4683 return 0;
4684 return 1;
4685 }
4686
4687 return ipf_tcpudpchk(&fin->fin_fi, ft);
4688 }
4689
4690
4691 /* ------------------------------------------------------------------------ */
4692 /* Function: ipf_nat_update */
4693 /* Returns: Nil */
4694 /* Parameters: fin(I) - pointer to packet information */
4695 /* nat(I) - pointer to NAT structure */
4696 /* */
4697 /* Updates the lifetime of a NAT table entry for non-TCP packets. Must be */
4698 /* called with fin_rev updated - i.e. after calling ipf_nat_proto(). */
4699 /* */
4700 /* This *MUST* be called after ipf_nat_proto() as it expects fin_rev to */
4701 /* already be set. */
4702 /* ------------------------------------------------------------------------ */
4703 void
4704 ipf_nat_update(fr_info_t *fin, nat_t *nat)
4705 {
4706 ipf_main_softc_t *softc = fin->fin_main_soft;
4707 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4708 ipftq_t *ifq, *ifq2;
4709 ipftqent_t *tqe;
4710 ipnat_t *np = nat->nat_ptr;
4711
4712 tqe = &nat->nat_tqe;
4713 ifq = tqe->tqe_ifq;
4714
4715 /*
4716 * We allow over-riding of NAT timeouts from NAT rules, even for
4717 * TCP, however, if it is TCP and there is no rule timeout set,
4718 * then do not update the timeout here.
4719 */
4720 if (np != NULL) {
4721 np->in_bytes[fin->fin_rev] += fin->fin_plen;
4722 ifq2 = np->in_tqehead[fin->fin_rev];
4723 } else {
4724 ifq2 = NULL;
4725 }
4726
4727 if (nat->nat_pr[0] == IPPROTO_TCP && ifq2 == NULL) {
4728 (void) ipf_tcp_age(&nat->nat_tqe, fin, softn->ipf_nat_tcptq,
4729 0, 2);
4730 } else {
4731 if (ifq2 == NULL) {
4732 if (nat->nat_pr[0] == IPPROTO_UDP)
4733 ifq2 = fin->fin_rev ? &softn->ipf_nat_udpacktq :
4734 &softn->ipf_nat_udptq;
4735 else if (nat->nat_pr[0] == IPPROTO_ICMP ||
4736 nat->nat_pr[0] == IPPROTO_ICMPV6)
4737 ifq2 = fin->fin_rev ? &softn->ipf_nat_icmpacktq:
4738 &softn->ipf_nat_icmptq;
4739 else
4740 ifq2 = &softn->ipf_nat_iptq;
4741 }
4742
4743 ipf_movequeue(softc->ipf_ticks, tqe, ifq, ifq2);
4744 }
4745 }
4746
4747
4748 /* ------------------------------------------------------------------------ */
4749 /* Function: ipf_nat_checkout */
4750 /* Returns: int - -1 == packet failed NAT checks so block it, */
4751 /* 0 == no packet translation occurred, */
4752 /* 1 == packet was successfully translated. */
4753 /* Parameters: fin(I) - pointer to packet information */
4754 /* passp(I) - pointer to filtering result flags */
4755 /* */
4756 /* Check to see if an outcoming packet should be changed. ICMP packets are */
4757 /* first checked to see if they match an existing entry (if an error), */
4758 /* otherwise a search of the current NAT table is made. If neither results */
4759 /* in a match then a search for a matching NAT rule is made. Create a new */
4760 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */
4761 /* packet header(s) as required. */
4762 /* ------------------------------------------------------------------------ */
4763 int
4764 ipf_nat_checkout(fr_info_t *fin, u_32_t *passp)
4765 {
4766 ipnat_t *np = NULL, *npnext;
4767 struct ifnet *ifp, *sifp;
4768 ipf_main_softc_t *softc;
4769 ipf_nat_softc_t *softn;
4770 tcphdr_t *tcp = NULL;
4771 int rval, natfailed;
4772 u_int nflags = 0;
4773 u_32_t ipa, iph;
4774 int natadd = 1;
4775 frentry_t *fr;
4776 nat_t *nat;
4777
4778 if (fin->fin_v == 6) {
4779 #ifdef USE_INET6
4780 return ipf_nat6_checkout(fin, passp);
4781 #else
4782 return 0;
4783 #endif
4784 }
4785
4786 softc = fin->fin_main_soft;
4787 softn = softc->ipf_nat_soft;
4788
4789 if (softn->ipf_nat_lock != 0)
4790 return 0;
4791 if (softn->ipf_nat_stats.ns_rules == 0 &&
4792 softn->ipf_nat_instances == NULL)
4793 return 0;
4794
4795 natfailed = 0;
4796 fr = fin->fin_fr;
4797 sifp = fin->fin_ifp;
4798 if (fr != NULL) {
4799 ifp = fr->fr_tifs[fin->fin_rev].fd_ptr;
4800 if ((ifp != NULL) && (ifp != (void *)-1))
4801 fin->fin_ifp = ifp;
4802 }
4803 ifp = fin->fin_ifp;
4804
4805 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
4806 switch (fin->fin_p)
4807 {
4808 case IPPROTO_TCP :
4809 nflags = IPN_TCP;
4810 break;
4811 case IPPROTO_UDP :
4812 nflags = IPN_UDP;
4813 break;
4814 case IPPROTO_ICMP :
4815 /*
4816 * This is an incoming packet, so the destination is
4817 * the icmp_id and the source port equals 0
4818 */
4819 if ((fin->fin_flx & FI_ICMPQUERY) != 0)
4820 nflags = IPN_ICMPQUERY;
4821 break;
4822 default :
4823 break;
4824 }
4825
4826 if ((nflags & IPN_TCPUDP))
4827 tcp = fin->fin_dp;
4828 }
4829
4830 ipa = fin->fin_saddr;
4831
4832 READ_ENTER(&softc->ipf_nat);
4833
4834 if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
4835 (nat = ipf_nat_icmperror(fin, &nflags, NAT_OUTBOUND)))
4836 /*EMPTY*/;
4837 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
4838 natadd = 0;
4839 else if ((nat = ipf_nat_outlookup(fin, nflags|NAT_SEARCH,
4840 (u_int)fin->fin_p, fin->fin_src,
4841 fin->fin_dst))) {
4842 nflags = nat->nat_flags;
4843 } else if (fin->fin_off == 0) {
4844 u_32_t hv, msk, nmsk = 0;
4845
4846 /*
4847 * If there is no current entry in the nat table for this IP#,
4848 * create one for it (if there is a matching rule).
4849 */
4850 maskloop:
4851 msk = softn->ipf_nat_map_active_masks[nmsk];
4852 iph = ipa & msk;
4853 hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_maprules_sz);
4854 retry_roundrobin:
4855 for (np = softn->ipf_nat_map_rules[hv]; np; np = npnext) {
4856 npnext = np->in_mnext;
4857 if ((np->in_ifps[1] && (np->in_ifps[1] != ifp)))
4858 continue;
4859 if (np->in_v[0] != 4)
4860 continue;
4861 if (np->in_pr[1] && (np->in_pr[1] != fin->fin_p))
4862 continue;
4863 if ((np->in_flags & IPN_RF) &&
4864 !(np->in_flags & nflags))
4865 continue;
4866 if (np->in_flags & IPN_FILTER) {
4867 switch (ipf_nat_match(fin, np))
4868 {
4869 case 0 :
4870 continue;
4871 case -1 :
4872 rval = -1;
4873 goto outmatchfail;
4874 case 1 :
4875 default :
4876 break;
4877 }
4878 } else if ((ipa & np->in_osrcmsk) != np->in_osrcaddr)
4879 continue;
4880
4881 if ((fr != NULL) &&
4882 !ipf_matchtag(&np->in_tag, &fr->fr_nattag))
4883 continue;
4884
4885 if (np->in_plabel != -1) {
4886 if (((np->in_flags & IPN_FILTER) == 0) &&
4887 (np->in_odport != fin->fin_data[1]))
4888 continue;
4889 if (ipf_proxy_ok(fin, tcp, np) == 0)
4890 continue;
4891 }
4892
4893 if (np->in_flags & IPN_NO) {
4894 np->in_hits++;
4895 break;
4896 }
4897 MUTEX_ENTER(&softn->ipf_nat_new);
4898 /*
4899 * If we've matched a round-robin rule but it has
4900 * moved in the list since we got it, start over as
4901 * this is now no longer correct.
4902 */
4903 if (npnext != np->in_mnext) {
4904 if ((np->in_flags & IPN_ROUNDR) != 0) {
4905 MUTEX_EXIT(&softn->ipf_nat_new);
4906 goto retry_roundrobin;
4907 }
4908 npnext = np->in_mnext;
4909 }
4910
4911 nat = ipf_nat_add(fin, np, NULL, nflags, NAT_OUTBOUND);
4912 MUTEX_EXIT(&softn->ipf_nat_new);
4913 if (nat != NULL) {
4914 natfailed = 0;
4915 break;
4916 }
4917 natfailed = -1;
4918 }
4919 if ((np == NULL) && (nmsk < softn->ipf_nat_map_max)) {
4920 nmsk++;
4921 goto maskloop;
4922 }
4923 }
4924
4925 if (nat != NULL) {
4926 rval = ipf_nat_out(fin, nat, natadd, nflags);
4927 if (rval == 1) {
4928 MUTEX_ENTER(&nat->nat_lock);
4929 ipf_nat_update(fin, nat);
4930 nat->nat_bytes[1] += fin->fin_plen;
4931 nat->nat_pkts[1]++;
4932 fin->fin_pktnum = nat->nat_pkts[1];
4933 MUTEX_EXIT(&nat->nat_lock);
4934 }
4935 } else
4936 rval = natfailed;
4937 outmatchfail:
4938 RWLOCK_EXIT(&softc->ipf_nat);
4939
4940 switch (rval)
4941 {
4942 case -1 :
4943 if (passp != NULL) {
4944 DT1(frb_natv4out, fr_info_t *, fin);
4945 NBUMPSIDED(1, ns_drop);
4946 *passp = FR_BLOCK;
4947 fin->fin_reason = FRB_NATV4;
4948 }
4949 fin->fin_flx |= FI_BADNAT;
4950 NBUMPSIDED(1, ns_badnat);
4951 break;
4952 case 0 :
4953 NBUMPSIDE(1, ns_ignored);
4954 break;
4955 case 1 :
4956 NBUMPSIDE(1, ns_translated);
4957 break;
4958 }
4959 fin->fin_ifp = sifp;
4960 return rval;
4961 }
4962
4963 /* ------------------------------------------------------------------------ */
4964 /* Function: ipf_nat_out */
4965 /* Returns: int - -1 == packet failed NAT checks so block it, */
4966 /* 1 == packet was successfully translated. */
4967 /* Parameters: fin(I) - pointer to packet information */
4968 /* nat(I) - pointer to NAT structure */
4969 /* natadd(I) - flag indicating if it is safe to add frag cache */
4970 /* nflags(I) - NAT flags set for this packet */
4971 /* */
4972 /* Translate a packet coming "out" on an interface. */
4973 /* ------------------------------------------------------------------------ */
4974 int
4975 ipf_nat_out(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
4976 {
4977 ipf_main_softc_t *softc = fin->fin_main_soft;
4978 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
4979 icmphdr_t *icmp;
4980 tcphdr_t *tcp;
4981 ipnat_t *np;
4982 int skip;
4983 int i;
4984
4985 tcp = NULL;
4986 icmp = NULL;
4987 np = nat->nat_ptr;
4988
4989 if ((natadd != 0) && (fin->fin_flx & FI_FRAG) && (np != NULL))
4990 (void) ipf_frag_natnew(softc, fin, 0, nat);
4991
4992 /*
4993 * Fix up checksums, not by recalculating them, but
4994 * simply computing adjustments.
4995 * This is only done for STREAMS based IP implementations where the
4996 * checksum has already been calculated by IP. In all other cases,
4997 * IPFilter is called before the checksum needs calculating so there
4998 * is no call to modify whatever is in the header now.
4999 */
5000 if (nflags == IPN_ICMPERR) {
5001 u_32_t s1, s2, sumd, msumd;
5002
5003 s1 = LONG_SUM(ntohl(fin->fin_saddr));
5004 if (nat->nat_dir == NAT_OUTBOUND) {
5005 s2 = LONG_SUM(ntohl(nat->nat_nsrcaddr));
5006 } else {
5007 s2 = LONG_SUM(ntohl(nat->nat_odstaddr));
5008 }
5009 CALC_SUMD(s1, s2, sumd);
5010 msumd = sumd;
5011
5012 s1 = LONG_SUM(ntohl(fin->fin_daddr));
5013 if (nat->nat_dir == NAT_OUTBOUND) {
5014 s2 = LONG_SUM(ntohl(nat->nat_ndstaddr));
5015 } else {
5016 s2 = LONG_SUM(ntohl(nat->nat_osrcaddr));
5017 }
5018 CALC_SUMD(s1, s2, sumd);
5019 msumd += sumd;
5020
5021 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, msumd, 0);
5022 }
5023 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5024 defined(linux) || defined(BRIDGE_IPF)
5025 else {
5026 /*
5027 * Strictly speaking, this isn't necessary on BSD
5028 * kernels because they do checksum calculation after
5029 * this code has run BUT if ipfilter is being used
5030 * to do NAT as a bridge, that code doesn't exist.
5031 */
5032 switch (nat->nat_dir)
5033 {
5034 case NAT_OUTBOUND :
5035 ipf_fix_outcksum(fin->fin_cksum & FI_CK_L4PART,
5036 &fin->fin_ip->ip_sum,
5037 nat->nat_ipsumd, 0);
5038 break;
5039
5040 case NAT_INBOUND :
5041 ipf_fix_incksum(fin->fin_cksum & FI_CK_L4PART,
5042 &fin->fin_ip->ip_sum,
5043 nat->nat_ipsumd, 0);
5044 break;
5045
5046 default :
5047 break;
5048 }
5049 }
5050 #endif
5051
5052 /*
5053 * Address assignment is after the checksum modification because
5054 * we are using the address in the packet for determining the
5055 * correct checksum offset (the ICMP error could be coming from
5056 * anyone...)
5057 */
5058 switch (nat->nat_dir)
5059 {
5060 case NAT_OUTBOUND :
5061 fin->fin_ip->ip_src = nat->nat_nsrcip;
5062 fin->fin_saddr = nat->nat_nsrcaddr;
5063 fin->fin_ip->ip_dst = nat->nat_ndstip;
5064 fin->fin_daddr = nat->nat_ndstaddr;
5065 break;
5066
5067 case NAT_INBOUND :
5068 fin->fin_ip->ip_src = nat->nat_odstip;
5069 fin->fin_saddr = nat->nat_ndstaddr;
5070 fin->fin_ip->ip_dst = nat->nat_osrcip;
5071 fin->fin_daddr = nat->nat_nsrcaddr;
5072 break;
5073
5074 case NAT_DIVERTIN :
5075 {
5076 mb_t *m;
5077
5078 skip = ipf_nat_decap(fin, nat);
5079 if (skip <= 0) {
5080 NBUMPSIDED(1, ns_decap_fail);
5081 return -1;
5082 }
5083
5084 m = fin->fin_m;
5085
5086 #if defined(MENTAT) && defined(_KERNEL)
5087 m->b_rptr += skip;
5088 #else
5089 m->m_data += skip;
5090 m->m_len -= skip;
5091
5092 # ifdef M_PKTHDR
5093 if (m->m_flags & M_PKTHDR)
5094 m->m_pkthdr.len -= skip;
5095 # endif
5096 #endif
5097
5098 MUTEX_ENTER(&nat->nat_lock);
5099 ipf_nat_update(fin, nat);
5100 MUTEX_EXIT(&nat->nat_lock);
5101 fin->fin_flx |= FI_NATED;
5102 if (np != NULL && np->in_tag.ipt_num[0] != 0)
5103 fin->fin_nattag = &np->in_tag;
5104 return 1;
5105 /* NOTREACHED */
5106 }
5107
5108 case NAT_DIVERTOUT :
5109 {
5110 u_32_t s1, s2, sumd;
5111 udphdr_t *uh;
5112 ip_t *ip;
5113 mb_t *m;
5114
5115 m = M_DUP(np->in_divmp);
5116 if (m == NULL) {
5117 NBUMPSIDED(1, ns_divert_dup);
5118 return -1;
5119 }
5120
5121 ip = MTOD(m, ip_t *);
5122 ip->ip_id = htons(ipf_nextipid(fin));
5123 s2 = ntohs(ip->ip_id);
5124
5125 s1 = ip->ip_len;
5126 ip->ip_len = ntohs(ip->ip_len);
5127 ip->ip_len += fin->fin_plen;
5128 ip->ip_len = htons(ip->ip_len);
5129 s2 += ntohs(ip->ip_len);
5130 CALC_SUMD(s1, s2, sumd);
5131
5132 uh = (udphdr_t *)(ip + 1);
5133 uh->uh_ulen += fin->fin_plen;
5134 uh->uh_ulen = htons(uh->uh_ulen);
5135 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5136 defined(linux) || defined(BRIDGE_IPF)
5137 ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0);
5138 #endif
5139
5140 PREP_MB_T(fin, m);
5141
5142 fin->fin_src = ip->ip_src;
5143 fin->fin_dst = ip->ip_dst;
5144 fin->fin_ip = ip;
5145 fin->fin_plen += sizeof(ip_t) + 8; /* UDP + IPv4 hdr */
5146 fin->fin_dlen += sizeof(ip_t) + 8; /* UDP + IPv4 hdr */
5147
5148 nflags &= ~IPN_TCPUDPICMP;
5149
5150 break;
5151 }
5152
5153 default :
5154 break;
5155 }
5156
5157 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5158 u_short *csump;
5159
5160 if ((nat->nat_nsport != 0) && (nflags & IPN_TCPUDP)) {
5161 tcp = fin->fin_dp;
5162
5163 switch (nat->nat_dir)
5164 {
5165 case NAT_OUTBOUND :
5166 tcp->th_sport = nat->nat_nsport;
5167 fin->fin_data[0] = ntohs(nat->nat_nsport);
5168 tcp->th_dport = nat->nat_ndport;
5169 fin->fin_data[1] = ntohs(nat->nat_ndport);
5170 break;
5171
5172 case NAT_INBOUND :
5173 tcp->th_sport = nat->nat_odport;
5174 fin->fin_data[0] = ntohs(nat->nat_odport);
5175 tcp->th_dport = nat->nat_osport;
5176 fin->fin_data[1] = ntohs(nat->nat_osport);
5177 break;
5178 }
5179 }
5180
5181 if ((nat->nat_oicmpid != 0) && (nflags & IPN_ICMPQUERY)) {
5182 icmp = fin->fin_dp;
5183
5184 switch (nat->nat_dir)
5185 {
5186 case NAT_OUTBOUND :
5187 icmp->icmp_id = nat->nat_nicmpid;
5188 break;
5189 case NAT_INBOUND :
5190 icmp->icmp_id = nat->nat_oicmpid;
5191 break;
5192 }
5193 }
5194
5195 csump = ipf_nat_proto(fin, nat, nflags);
5196
5197 /*
5198 * The above comments do not hold for layer 4 (or higher)
5199 * checksums...
5200 */
5201 if (csump != NULL) {
5202 if (nat->nat_dir == NAT_OUTBOUND)
5203 ipf_fix_outcksum(fin->fin_cksum, csump,
5204 nat->nat_sumd[0],
5205 nat->nat_sumd[1] +
5206 fin->fin_dlen);
5207 else
5208 ipf_fix_incksum(fin->fin_cksum, csump,
5209 nat->nat_sumd[0],
5210 nat->nat_sumd[1] +
5211 fin->fin_dlen);
5212 }
5213 }
5214
5215 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
5216 /* ------------------------------------------------------------- */
5217 /* A few quick notes: */
5218 /* Following are test conditions prior to calling the */
5219 /* ipf_proxy_check routine. */
5220 /* */
5221 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */
5222 /* with a redirect rule, we attempt to match the packet's */
5223 /* source port against in_dport, otherwise we'd compare the */
5224 /* packet's destination. */
5225 /* ------------------------------------------------------------- */
5226 if ((np != NULL) && (np->in_apr != NULL)) {
5227 i = ipf_proxy_check(fin, nat);
5228 if (i == 0) {
5229 i = 1;
5230 } else if (i == -1) {
5231 NBUMPSIDED(1, ns_ipf_proxy_fail);
5232 }
5233 } else {
5234 i = 1;
5235 }
5236 fin->fin_flx |= FI_NATED;
5237 return i;
5238 }
5239
5240
5241 /* ------------------------------------------------------------------------ */
5242 /* Function: ipf_nat_checkin */
5243 /* Returns: int - -1 == packet failed NAT checks so block it, */
5244 /* 0 == no packet translation occurred, */
5245 /* 1 == packet was successfully translated. */
5246 /* Parameters: fin(I) - pointer to packet information */
5247 /* passp(I) - pointer to filtering result flags */
5248 /* */
5249 /* Check to see if an incoming packet should be changed. ICMP packets are */
5250 /* first checked to see if they match an existing entry (if an error), */
5251 /* otherwise a search of the current NAT table is made. If neither results */
5252 /* in a match then a search for a matching NAT rule is made. Create a new */
5253 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */
5254 /* packet header(s) as required. */
5255 /* ------------------------------------------------------------------------ */
5256 int
5257 ipf_nat_checkin(fr_info_t *fin, u_32_t *passp)
5258 {
5259 ipf_main_softc_t *softc;
5260 ipf_nat_softc_t *softn;
5261 u_int nflags, natadd;
5262 ipnat_t *np, *npnext;
5263 int rval, natfailed;
5264 struct ifnet *ifp;
5265 struct in_addr in;
5266 icmphdr_t *icmp;
5267 tcphdr_t *tcp;
5268 u_short dport;
5269 nat_t *nat;
5270 u_32_t iph;
5271
5272 softc = fin->fin_main_soft;
5273 softn = softc->ipf_nat_soft;
5274
5275 if (softn->ipf_nat_lock != 0)
5276 return 0;
5277 if (softn->ipf_nat_stats.ns_rules == 0 &&
5278 softn->ipf_nat_instances == NULL)
5279 return 0;
5280
5281 tcp = NULL;
5282 icmp = NULL;
5283 dport = 0;
5284 natadd = 1;
5285 nflags = 0;
5286 natfailed = 0;
5287 ifp = fin->fin_ifp;
5288
5289 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5290 switch (fin->fin_p)
5291 {
5292 case IPPROTO_TCP :
5293 nflags = IPN_TCP;
5294 break;
5295 case IPPROTO_UDP :
5296 nflags = IPN_UDP;
5297 break;
5298 case IPPROTO_ICMP :
5299 icmp = fin->fin_dp;
5300
5301 /*
5302 * This is an incoming packet, so the destination is
5303 * the icmp_id and the source port equals 0
5304 */
5305 if ((fin->fin_flx & FI_ICMPQUERY) != 0) {
5306 nflags = IPN_ICMPQUERY;
5307 dport = icmp->icmp_id;
5308 } break;
5309 default :
5310 break;
5311 }
5312
5313 if ((nflags & IPN_TCPUDP)) {
5314 tcp = fin->fin_dp;
5315 dport = fin->fin_data[1];
5316 }
5317 }
5318
5319 in = fin->fin_dst;
5320
5321 READ_ENTER(&softc->ipf_nat);
5322
5323 if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) &&
5324 (nat = ipf_nat_icmperror(fin, &nflags, NAT_INBOUND)))
5325 /*EMPTY*/;
5326 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
5327 natadd = 0;
5328 else if ((nat = ipf_nat_inlookup(fin, nflags|NAT_SEARCH,
5329 (u_int)fin->fin_p,
5330 fin->fin_src, in))) {
5331 nflags = nat->nat_flags;
5332 } else if (fin->fin_off == 0) {
5333 u_32_t hv, msk, rmsk = 0;
5334
5335 /*
5336 * If there is no current entry in the nat table for this IP#,
5337 * create one for it (if there is a matching rule).
5338 */
5339 maskloop:
5340 msk = softn->ipf_nat_rdr_active_masks[rmsk];
5341 iph = in.s_addr & msk;
5342 hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_rdrrules_sz);
5343 retry_roundrobin:
5344 /* TRACE (iph,msk,rmsk,hv,softn->ipf_nat_rdrrules_sz) */
5345 for (np = softn->ipf_nat_rdr_rules[hv]; np; np = npnext) {
5346 npnext = np->in_rnext;
5347 if (np->in_ifps[0] && (np->in_ifps[0] != ifp))
5348 continue;
5349 if (np->in_v[0] != 4)
5350 continue;
5351 if (np->in_pr[0] && (np->in_pr[0] != fin->fin_p))
5352 continue;
5353 if ((np->in_flags & IPN_RF) && !(np->in_flags & nflags))
5354 continue;
5355 if (np->in_flags & IPN_FILTER) {
5356 switch (ipf_nat_match(fin, np))
5357 {
5358 case 0 :
5359 continue;
5360 case -1 :
5361 rval = -1;
5362 goto inmatchfail;
5363 case 1 :
5364 default :
5365 break;
5366 }
5367 } else {
5368 if ((in.s_addr & np->in_odstmsk) !=
5369 np->in_odstaddr)
5370 continue;
5371 if (np->in_odport &&
5372 ((np->in_dtop < dport) ||
5373 (dport < np->in_odport)))
5374 continue;
5375 }
5376
5377 if (np->in_plabel != -1) {
5378 if (!ipf_proxy_ok(fin, tcp, np)) {
5379 continue;
5380 }
5381 }
5382
5383 if (np->in_flags & IPN_NO) {
5384 np->in_hits++;
5385 break;
5386 }
5387
5388 MUTEX_ENTER(&softn->ipf_nat_new);
5389 /*
5390 * If we've matched a round-robin rule but it has
5391 * moved in the list since we got it, start over as
5392 * this is now no longer correct.
5393 */
5394 if (npnext != np->in_rnext) {
5395 if ((np->in_flags & IPN_ROUNDR) != 0) {
5396 MUTEX_EXIT(&softn->ipf_nat_new);
5397 goto retry_roundrobin;
5398 }
5399 npnext = np->in_rnext;
5400 }
5401
5402 nat = ipf_nat_add(fin, np, NULL, nflags, NAT_INBOUND);
5403 MUTEX_EXIT(&softn->ipf_nat_new);
5404 if (nat != NULL) {
5405 natfailed = 0;
5406 break;
5407 }
5408 natfailed = -1;
5409 }
5410 if ((np == NULL) && (rmsk < softn->ipf_nat_rdr_max)) {
5411 rmsk++;
5412 goto maskloop;
5413 }
5414 }
5415
5416 if (nat != NULL) {
5417 rval = ipf_nat_in(fin, nat, natadd, nflags);
5418 if (rval == 1) {
5419 MUTEX_ENTER(&nat->nat_lock);
5420 ipf_nat_update(fin, nat);
5421 nat->nat_bytes[0] += fin->fin_plen;
5422 nat->nat_pkts[0]++;
5423 fin->fin_pktnum = nat->nat_pkts[0];
5424 MUTEX_EXIT(&nat->nat_lock);
5425 }
5426 } else
5427 rval = natfailed;
5428 inmatchfail:
5429 RWLOCK_EXIT(&softc->ipf_nat);
5430
5431 switch (rval)
5432 {
5433 case -1 :
5434 if (passp != NULL) {
5435 DT1(frb_natv4in, fr_info_t *, fin);
5436 NBUMPSIDED(0, ns_drop);
5437 *passp = FR_BLOCK;
5438 fin->fin_reason = FRB_NATV4;
5439 }
5440 fin->fin_flx |= FI_BADNAT;
5441 NBUMPSIDED(0, ns_badnat);
5442 break;
5443 case 0 :
5444 NBUMPSIDE(0, ns_ignored);
5445 break;
5446 case 1 :
5447 NBUMPSIDE(0, ns_translated);
5448 break;
5449 }
5450 return rval;
5451 }
5452
5453
5454 /* ------------------------------------------------------------------------ */
5455 /* Function: ipf_nat_in */
5456 /* Returns: int - -1 == packet failed NAT checks so block it, */
5457 /* 1 == packet was successfully translated. */
5458 /* Parameters: fin(I) - pointer to packet information */
5459 /* nat(I) - pointer to NAT structure */
5460 /* natadd(I) - flag indicating if it is safe to add frag cache */
5461 /* nflags(I) - NAT flags set for this packet */
5462 /* Locks Held: ipf_nat(READ) */
5463 /* */
5464 /* Translate a packet coming "in" on an interface. */
5465 /* ------------------------------------------------------------------------ */
5466 int
5467 ipf_nat_in(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
5468 {
5469 ipf_main_softc_t *softc = fin->fin_main_soft;
5470 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5471 u_32_t sumd, ipsumd, sum1, sum2;
5472 icmphdr_t *icmp;
5473 tcphdr_t *tcp;
5474 ipnat_t *np;
5475 int skip;
5476 int i;
5477
5478 tcp = NULL;
5479 np = nat->nat_ptr;
5480 fin->fin_fr = nat->nat_fr;
5481
5482 if (np != NULL) {
5483 if ((natadd != 0) && (fin->fin_flx & FI_FRAG))
5484 (void) ipf_frag_natnew(softc, fin, 0, nat);
5485
5486 /* ------------------------------------------------------------- */
5487 /* A few quick notes: */
5488 /* Following are test conditions prior to calling the */
5489 /* ipf_proxy_check routine. */
5490 /* */
5491 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */
5492 /* with a map rule, we attempt to match the packet's */
5493 /* source port against in_dport, otherwise we'd compare the */
5494 /* packet's destination. */
5495 /* ------------------------------------------------------------- */
5496 if (np->in_apr != NULL) {
5497 i = ipf_proxy_check(fin, nat);
5498 if (i == -1) {
5499 NBUMPSIDED(0, ns_ipf_proxy_fail);
5500 return -1;
5501 }
5502 }
5503 }
5504
5505 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
5506
5507 ipsumd = nat->nat_ipsumd;
5508 /*
5509 * Fix up checksums, not by recalculating them, but
5510 * simply computing adjustments.
5511 * Why only do this for some platforms on inbound packets ?
5512 * Because for those that it is done, IP processing is yet to happen
5513 * and so the IPv4 header checksum has not yet been evaluated.
5514 * Perhaps it should always be done for the benefit of things like
5515 * fast forwarding (so that it doesn't need to be recomputed) but with
5516 * header checksum offloading, perhaps it is a moot point.
5517 */
5518
5519 switch (nat->nat_dir)
5520 {
5521 case NAT_INBOUND :
5522 if ((fin->fin_flx & FI_ICMPERR) == 0) {
5523 fin->fin_ip->ip_src = nat->nat_nsrcip;
5524 fin->fin_saddr = nat->nat_nsrcaddr;
5525 } else {
5526 sum1 = nat->nat_osrcaddr;
5527 sum2 = nat->nat_nsrcaddr;
5528 CALC_SUMD(sum1, sum2, sumd);
5529 ipsumd -= sumd;
5530 }
5531 fin->fin_ip->ip_dst = nat->nat_ndstip;
5532 fin->fin_daddr = nat->nat_ndstaddr;
5533 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5534 defined(__osf__) || defined(linux)
5535 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, ipsumd, 0);
5536 #endif
5537 break;
5538
5539 case NAT_OUTBOUND :
5540 if ((fin->fin_flx & FI_ICMPERR) == 0) {
5541 fin->fin_ip->ip_src = nat->nat_odstip;
5542 fin->fin_saddr = nat->nat_odstaddr;
5543 } else {
5544 sum1 = nat->nat_odstaddr;
5545 sum2 = nat->nat_ndstaddr;
5546 CALC_SUMD(sum1, sum2, sumd);
5547 ipsumd -= sumd;
5548 }
5549 fin->fin_ip->ip_dst = nat->nat_osrcip;
5550 fin->fin_daddr = nat->nat_osrcaddr;
5551 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5552 defined(__osf__) || defined(linux)
5553 ipf_fix_incksum(0, &fin->fin_ip->ip_sum, ipsumd, 0);
5554 #endif
5555 break;
5556
5557 case NAT_DIVERTIN :
5558 {
5559 udphdr_t *uh;
5560 ip_t *ip;
5561 mb_t *m;
5562
5563 m = M_DUP(np->in_divmp);
5564 if (m == NULL) {
5565 NBUMPSIDED(0, ns_divert_dup);
5566 return -1;
5567 }
5568
5569 ip = MTOD(m, ip_t *);
5570 ip->ip_id = htons(ipf_nextipid(fin));
5571 sum1 = ntohs(ip->ip_len);
5572 ip->ip_len = ntohs(ip->ip_len);
5573 ip->ip_len += fin->fin_plen;
5574 ip->ip_len = htons(ip->ip_len);
5575
5576 uh = (udphdr_t *)(ip + 1);
5577 uh->uh_ulen += fin->fin_plen;
5578 uh->uh_ulen = htons(uh->uh_ulen);
5579
5580 sum2 = ntohs(ip->ip_id) + ntohs(ip->ip_len);
5581 sum2 += ntohs(ip->ip_off) & IP_DF;
5582 CALC_SUMD(sum1, sum2, sumd);
5583
5584 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
5585 defined(__osf__) || defined(linux)
5586 ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0);
5587 #endif
5588 PREP_MB_T(fin, m);
5589
5590 fin->fin_ip = ip;
5591 fin->fin_plen += sizeof(ip_t) + 8; /* UDP + new IPv4 hdr */
5592 fin->fin_dlen += sizeof(ip_t) + 8; /* UDP + old IPv4 hdr */
5593
5594 nflags &= ~IPN_TCPUDPICMP;
5595
5596 break;
5597 }
5598
5599 case NAT_DIVERTOUT :
5600 {
5601 mb_t *m;
5602
5603 skip = ipf_nat_decap(fin, nat);
5604 if (skip <= 0) {
5605 NBUMPSIDED(0, ns_decap_fail);
5606 return -1;
5607 }
5608
5609 m = fin->fin_m;
5610
5611 #if defined(MENTAT) && defined(_KERNEL)
5612 m->b_rptr += skip;
5613 #else
5614 m->m_data += skip;
5615 m->m_len -= skip;
5616
5617 # ifdef M_PKTHDR
5618 if (m->m_flags & M_PKTHDR)
5619 m->m_pkthdr.len -= skip;
5620 # endif
5621 #endif
5622
5623 ipf_nat_update(fin, nat);
5624 nflags &= ~IPN_TCPUDPICMP;
5625 fin->fin_flx |= FI_NATED;
5626 if (np != NULL && np->in_tag.ipt_num[0] != 0)
5627 fin->fin_nattag = &np->in_tag;
5628 return 1;
5629 /* NOTREACHED */
5630 }
5631 }
5632 if (nflags & IPN_TCPUDP)
5633 tcp = fin->fin_dp;
5634
5635 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
5636 u_short *csump;
5637
5638 if ((nat->nat_odport != 0) && (nflags & IPN_TCPUDP)) {
5639 switch (nat->nat_dir)
5640 {
5641 case NAT_INBOUND :
5642 tcp->th_sport = nat->nat_nsport;
5643 fin->fin_data[0] = ntohs(nat->nat_nsport);
5644 tcp->th_dport = nat->nat_ndport;
5645 fin->fin_data[1] = ntohs(nat->nat_ndport);
5646 break;
5647
5648 case NAT_OUTBOUND :
5649 tcp->th_sport = nat->nat_odport;
5650 fin->fin_data[0] = ntohs(nat->nat_odport);
5651 tcp->th_dport = nat->nat_osport;
5652 fin->fin_data[1] = ntohs(nat->nat_osport);
5653 break;
5654 }
5655 }
5656
5657
5658 if ((nat->nat_oicmpid != 0) && (nflags & IPN_ICMPQUERY)) {
5659 icmp = fin->fin_dp;
5660
5661 switch (nat->nat_dir)
5662 {
5663 case NAT_INBOUND :
5664 icmp->icmp_id = nat->nat_nicmpid;
5665 break;
5666 case NAT_OUTBOUND :
5667 icmp->icmp_id = nat->nat_oicmpid;
5668 break;
5669 }
5670 }
5671
5672 csump = ipf_nat_proto(fin, nat, nflags);
5673
5674 /*
5675 * The above comments do not hold for layer 4 (or higher)
5676 * checksums...
5677 */
5678 if (csump != NULL) {
5679 if (nat->nat_dir == NAT_OUTBOUND)
5680 ipf_fix_incksum(0, csump, nat->nat_sumd[0], 0);
5681 else
5682 ipf_fix_outcksum(0, csump, nat->nat_sumd[0], 0);
5683 }
5684 }
5685
5686 fin->fin_flx |= FI_NATED;
5687 if (np != NULL && np->in_tag.ipt_num[0] != 0)
5688 fin->fin_nattag = &np->in_tag;
5689 return 1;
5690 }
5691
5692
5693 /* ------------------------------------------------------------------------ */
5694 /* Function: ipf_nat_proto */
5695 /* Returns: u_short* - pointer to transport header checksum to update, */
5696 /* NULL if the transport protocol is not recognised */
5697 /* as needing a checksum update. */
5698 /* Parameters: fin(I) - pointer to packet information */
5699 /* nat(I) - pointer to NAT structure */
5700 /* nflags(I) - NAT flags set for this packet */
5701 /* */
5702 /* Return the pointer to the checksum field for each protocol so understood.*/
5703 /* If support for making other changes to a protocol header is required, */
5704 /* that is not strictly 'address' translation, such as clamping the MSS in */
5705 /* TCP down to a specific value, then do it from here. */
5706 /* ------------------------------------------------------------------------ */
5707 u_short *
5708 ipf_nat_proto(fr_info_t *fin, nat_t *nat, u_int nflags)
5709 {
5710 icmphdr_t *icmp;
5711 u_short *csump;
5712 tcphdr_t *tcp;
5713 udphdr_t *udp;
5714
5715 csump = NULL;
5716 if (fin->fin_out == 0) {
5717 fin->fin_rev = (nat->nat_dir & NAT_OUTBOUND);
5718 } else {
5719 fin->fin_rev = ((nat->nat_dir & NAT_OUTBOUND) == 0);
5720 }
5721
5722 switch (fin->fin_p)
5723 {
5724 case IPPROTO_TCP :
5725 tcp = fin->fin_dp;
5726
5727 if ((nflags & IPN_TCP) != 0)
5728 csump = &tcp->th_sum;
5729
5730 /*
5731 * Do a MSS CLAMPING on a SYN packet,
5732 * only deal IPv4 for now.
5733 */
5734 if ((nat->nat_mssclamp != 0) && (tcp->th_flags & TH_SYN) != 0)
5735 ipf_nat_mssclamp(tcp, nat->nat_mssclamp, fin, csump);
5736
5737 break;
5738
5739 case IPPROTO_UDP :
5740 udp = fin->fin_dp;
5741
5742 if ((nflags & IPN_UDP) != 0) {
5743 if (udp->uh_sum != 0)
5744 csump = &udp->uh_sum;
5745 }
5746 break;
5747
5748 case IPPROTO_ICMP :
5749 icmp = fin->fin_dp;
5750
5751 if ((nflags & IPN_ICMPQUERY) != 0) {
5752 if (icmp->icmp_cksum != 0)
5753 csump = &icmp->icmp_cksum;
5754 }
5755 break;
5756
5757 #ifdef USE_INET6
5758 case IPPROTO_ICMPV6 :
5759 {
5760 struct icmp6_hdr *icmp6 = (struct icmp6_hdr *)fin->fin_dp;
5761
5762 icmp6 = fin->fin_dp;
5763
5764 if ((nflags & IPN_ICMPQUERY) != 0) {
5765 if (icmp6->icmp6_cksum != 0)
5766 csump = &icmp6->icmp6_cksum;
5767 }
5768 break;
5769 }
5770 #endif
5771 }
5772 return csump;
5773 }
5774
5775
5776 /* ------------------------------------------------------------------------ */
5777 /* Function: ipf_nat_expire */
5778 /* Returns: Nil */
5779 /* Parameters: softc(I) - pointer to soft context main structure */
5780 /* */
5781 /* Check all of the timeout queues for entries at the top which need to be */
5782 /* expired. */
5783 /* ------------------------------------------------------------------------ */
5784 void
5785 ipf_nat_expire(ipf_main_softc_t *softc)
5786 {
5787 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5788 ipftq_t *ifq, *ifqnext;
5789 ipftqent_t *tqe, *tqn;
5790 int i;
5791 SPL_INT(s);
5792
5793 SPL_NET(s);
5794 WRITE_ENTER(&softc->ipf_nat);
5795 for (ifq = softn->ipf_nat_tcptq, i = 0; ifq != NULL;
5796 ifq = ifq->ifq_next) {
5797 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
5798 if (tqe->tqe_die > softc->ipf_ticks)
5799 break;
5800 tqn = tqe->tqe_next;
5801 ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
5802 }
5803 }
5804
5805 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifq->ifq_next) {
5806 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) {
5807 if (tqe->tqe_die > softc->ipf_ticks)
5808 break;
5809 tqn = tqe->tqe_next;
5810 ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE);
5811 }
5812 }
5813
5814 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) {
5815 ifqnext = ifq->ifq_next;
5816
5817 if (((ifq->ifq_flags & IFQF_DELETE) != 0) &&
5818 (ifq->ifq_ref == 0)) {
5819 ipf_freetimeoutqueue(softc, ifq);
5820 }
5821 }
5822
5823 if (softn->ipf_nat_doflush != 0) {
5824 ipf_nat_extraflush(softc, softn, 2);
5825 softn->ipf_nat_doflush = 0;
5826 }
5827
5828 RWLOCK_EXIT(&softc->ipf_nat);
5829 SPL_X(s);
5830 }
5831
5832
5833 /* ------------------------------------------------------------------------ */
5834 /* Function: ipf_nat_sync */
5835 /* Returns: Nil */
5836 /* Parameters: softc(I) - pointer to soft context main structure */
5837 /* ifp(I) - pointer to network interface */
5838 /* */
5839 /* Walk through all of the currently active NAT sessions, looking for those */
5840 /* which need to have their translated address updated. */
5841 /* ------------------------------------------------------------------------ */
5842 void
5843 ipf_nat_sync(ipf_main_softc_t *softc, void *ifp)
5844 {
5845 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
5846 u_32_t sum1, sum2, sumd;
5847 i6addr_t in;
5848 ipnat_t *n;
5849 nat_t *nat;
5850 void *ifp2;
5851 int idx;
5852 SPL_INT(s);
5853
5854 if (softc->ipf_running <= 0)
5855 return;
5856
5857 /*
5858 * Change IP addresses for NAT sessions for any protocol except TCP
5859 * since it will break the TCP connection anyway. The only rules
5860 * which will get changed are those which are "map ... -> 0/32",
5861 * where the rule specifies the address is taken from the interface.
5862 */
5863 SPL_NET(s);
5864 WRITE_ENTER(&softc->ipf_nat);
5865
5866 if (softc->ipf_running <= 0) {
5867 RWLOCK_EXIT(&softc->ipf_nat);
5868 return;
5869 }
5870
5871 for (nat = softn->ipf_nat_instances; nat; nat = nat->nat_next) {
5872 if ((nat->nat_flags & IPN_TCP) != 0)
5873 continue;
5874
5875 n = nat->nat_ptr;
5876 if (n != NULL) {
5877 if (n->in_v[1] == 4) {
5878 if (n->in_redir & NAT_MAP) {
5879 if ((n->in_nsrcaddr != 0) ||
5880 (n->in_nsrcmsk != 0xffffffff))
5881 continue;
5882 } else if (n->in_redir & NAT_REDIRECT) {
5883 if ((n->in_ndstaddr != 0) ||
5884 (n->in_ndstmsk != 0xffffffff))
5885 continue;
5886 }
5887 }
5888 #ifdef USE_INET6
5889 if (n->in_v[1] == 4) {
5890 if (n->in_redir & NAT_MAP) {
5891 if (!IP6_ISZERO(&n->in_nsrcaddr) ||
5892 !IP6_ISONES(&n->in_nsrcmsk))
5893 continue;
5894 } else if (n->in_redir & NAT_REDIRECT) {
5895 if (!IP6_ISZERO(&n->in_ndstaddr) ||
5896 !IP6_ISONES(&n->in_ndstmsk))
5897 continue;
5898 }
5899 }
5900 #endif
5901 }
5902
5903 if (((ifp == NULL) || (ifp == nat->nat_ifps[0]) ||
5904 (ifp == nat->nat_ifps[1]))) {
5905 nat->nat_ifps[0] = GETIFP(nat->nat_ifnames[0],
5906 nat->nat_v[0]);
5907 if ((nat->nat_ifps[0] != NULL) &&
5908 (nat->nat_ifps[0] != (void *)-1)) {
5909 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]);
5910 }
5911 if (nat->nat_ifnames[1][0] != '\0') {
5912 nat->nat_ifps[1] = GETIFP(nat->nat_ifnames[1],
5913 nat->nat_v[1]);
5914 } else {
5915 nat->nat_ifps[1] = nat->nat_ifps[0];
5916 }
5917 if ((nat->nat_ifps[1] != NULL) &&
5918 (nat->nat_ifps[1] != (void *)-1)) {
5919 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]);
5920 }
5921 ifp2 = nat->nat_ifps[0];
5922 if (ifp2 == NULL)
5923 continue;
5924
5925 /*
5926 * Change the map-to address to be the same as the
5927 * new one.
5928 */
5929 sum1 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
5930 if (ipf_ifpaddr(softc, nat->nat_v[0], FRI_NORMAL, ifp2,
5931 &in, NULL) != -1) {
5932 if (nat->nat_v[0] == 4)
5933 nat->nat_nsrcip = in.in4;
5934 }
5935 sum2 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6);
5936
5937 if (sum1 == sum2)
5938 continue;
5939 /*
5940 * Readjust the checksum adjustment to take into
5941 * account the new IP#.
5942 */
5943 CALC_SUMD(sum1, sum2, sumd);
5944 /* XXX - dont change for TCP when solaris does
5945 * hardware checksumming.
5946 */
5947 sumd += nat->nat_sumd[0];
5948 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
5949 nat->nat_sumd[1] = nat->nat_sumd[0];
5950 }
5951 }
5952
5953 for (n = softn->ipf_nat_list; (n != NULL); n = n->in_next) {
5954 char *base = n->in_names;
5955
5956 if ((ifp == NULL) || (n->in_ifps[0] == ifp))
5957 n->in_ifps[0] = ipf_resolvenic(softc,
5958 base + n->in_ifnames[0],
5959 n->in_v[0]);
5960 if ((ifp == NULL) || (n->in_ifps[1] == ifp))
5961 n->in_ifps[1] = ipf_resolvenic(softc,
5962 base + n->in_ifnames[1],
5963 n->in_v[1]);
5964
5965 if (n->in_redir & NAT_REDIRECT)
5966 idx = 1;
5967 else
5968 idx = 0;
5969
5970 if (((ifp == NULL) || (n->in_ifps[idx] == ifp)) &&
5971 (n->in_ifps[idx] != NULL &&
5972 n->in_ifps[idx] != (void *)-1)) {
5973
5974 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc,
5975 0, n->in_ifps[idx]);
5976 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst,
5977 0, n->in_ifps[idx]);
5978 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc,
5979 0, n->in_ifps[idx]);
5980 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst,
5981 0, n->in_ifps[idx]);
5982 }
5983 }
5984 RWLOCK_EXIT(&softc->ipf_nat);
5985 SPL_X(s);
5986 }
5987
5988
5989 /* ------------------------------------------------------------------------ */
5990 /* Function: ipf_nat_icmpquerytype */
5991 /* Returns: int - 1 == success, 0 == failure */
5992 /* Parameters: icmptype(I) - ICMP type number */
5993 /* */
5994 /* Tests to see if the ICMP type number passed is a query/response type or */
5995 /* not. */
5996 /* ------------------------------------------------------------------------ */
5997 static int
5998 ipf_nat_icmpquerytype(int icmptype)
5999 {
6000
6001 /*
6002 * For the ICMP query NAT code, it is essential that both the query
6003 * and the reply match on the NAT rule. Because the NAT structure
6004 * does not keep track of the icmptype, and a single NAT structure
6005 * is used for all icmp types with the same src, dest and id, we
6006 * simply define the replies as queries as well. The funny thing is,
6007 * although it seems silly to call a reply a query, this is exactly
6008 * as it is defined in the IPv4 specification
6009 */
6010 switch (icmptype)
6011 {
6012 case ICMP_ECHOREPLY:
6013 case ICMP_ECHO:
6014 /* route aedvertisement/solliciation is currently unsupported: */
6015 /* it would require rewriting the ICMP data section */
6016 case ICMP_TSTAMP:
6017 case ICMP_TSTAMPREPLY:
6018 case ICMP_IREQ:
6019 case ICMP_IREQREPLY:
6020 case ICMP_MASKREQ:
6021 case ICMP_MASKREPLY:
6022 return 1;
6023 default:
6024 return 0;
6025 }
6026 }
6027
6028
6029 /* ------------------------------------------------------------------------ */
6030 /* Function: nat_log */
6031 /* Returns: Nil */
6032 /* Parameters: softc(I) - pointer to soft context main structure */
6033 /* softn(I) - pointer to NAT context structure */
6034 /* nat(I) - pointer to NAT structure */
6035 /* action(I) - action related to NAT structure being performed */
6036 /* */
6037 /* Creates a NAT log entry. */
6038 /* ------------------------------------------------------------------------ */
6039 void
6040 ipf_nat_log(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, struct nat *nat,
6041 u_int action)
6042 {
6043 #ifdef IPFILTER_LOG
6044 # ifndef LARGE_NAT
6045 struct ipnat *np;
6046 int rulen;
6047 # endif
6048 struct natlog natl;
6049 void *items[1];
6050 size_t sizes[1];
6051 int types[1];
6052
6053 bcopy((char *)&nat->nat_osrc6, (char *)&natl.nl_osrcip,
6054 sizeof(natl.nl_osrcip));
6055 bcopy((char *)&nat->nat_nsrc6, (char *)&natl.nl_nsrcip,
6056 sizeof(natl.nl_nsrcip));
6057 bcopy((char *)&nat->nat_odst6, (char *)&natl.nl_odstip,
6058 sizeof(natl.nl_odstip));
6059 bcopy((char *)&nat->nat_ndst6, (char *)&natl.nl_ndstip,
6060 sizeof(natl.nl_ndstip));
6061
6062 natl.nl_bytes[0] = nat->nat_bytes[0];
6063 natl.nl_bytes[1] = nat->nat_bytes[1];
6064 natl.nl_pkts[0] = nat->nat_pkts[0];
6065 natl.nl_pkts[1] = nat->nat_pkts[1];
6066 natl.nl_odstport = nat->nat_odport;
6067 natl.nl_osrcport = nat->nat_osport;
6068 natl.nl_nsrcport = nat->nat_nsport;
6069 natl.nl_ndstport = nat->nat_ndport;
6070 natl.nl_p[0] = nat->nat_pr[0];
6071 natl.nl_p[1] = nat->nat_pr[1];
6072 natl.nl_v[0] = nat->nat_v[0];
6073 natl.nl_v[1] = nat->nat_v[1];
6074 natl.nl_type = nat->nat_redir;
6075 natl.nl_action = action;
6076 natl.nl_rule = -1;
6077
6078 bcopy(nat->nat_ifnames[0], natl.nl_ifnames[0],
6079 sizeof(nat->nat_ifnames[0]));
6080 bcopy(nat->nat_ifnames[1], natl.nl_ifnames[1],
6081 sizeof(nat->nat_ifnames[1]));
6082
6083 # ifndef LARGE_NAT
6084 if (nat->nat_ptr != NULL) {
6085 for (rulen = 0, np = softn->ipf_nat_list; np != NULL;
6086 np = np->in_next, rulen++)
6087 if (np == nat->nat_ptr) {
6088 natl.nl_rule = rulen;
6089 break;
6090 }
6091 }
6092 # endif
6093 items[0] = &natl;
6094 sizes[0] = sizeof(natl);
6095 types[0] = 0;
6096
6097 (void) ipf_log_items(softc, IPL_LOGNAT, NULL, items, sizes, types, 1);
6098 #endif
6099 }
6100
6101
6102 #if defined(__OpenBSD__)
6103 /* ------------------------------------------------------------------------ */
6104 /* Function: ipf_nat_ifdetach */
6105 /* Returns: Nil */
6106 /* Parameters: ifp(I) - pointer to network interface */
6107 /* */
6108 /* Compatibility interface for OpenBSD to trigger the correct updating of */
6109 /* interface references within IPFilter. */
6110 /* ------------------------------------------------------------------------ */
6111 void
6112 ipf_nat_ifdetach(ifp)
6113 void *ifp;
6114 {
6115 ipf_main_softc_t *softc;
6116
6117 softc = ipf_get_softc(0);
6118
6119 ipf_sync(ifp);
6120 return;
6121 }
6122 #endif
6123
6124
6125 /* ------------------------------------------------------------------------ */
6126 /* Function: ipf_nat_rule_deref */
6127 /* Returns: Nil */
6128 /* Parameters: softc(I) - pointer to soft context main structure */
6129 /* inp(I) - pointer to pointer to NAT rule */
6130 /* Write Locks: ipf_nat */
6131 /* */
6132 /* Dropping the refernce count for a rule means that whatever held the */
6133 /* pointer to this rule (*inp) is no longer interested in it and when the */
6134 /* reference count drops to zero, any resources allocated for the rule can */
6135 /* be released and the rule itself free'd. */
6136 /* ------------------------------------------------------------------------ */
6137 void
6138 ipf_nat_rule_deref(ipf_main_softc_t *softc, ipnat_t **inp)
6139 {
6140 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6141 ipnat_t *n;
6142
6143 n = *inp;
6144 *inp = NULL;
6145 n->in_use--;
6146 if (n->in_use > 0)
6147 return;
6148
6149 if (n->in_apr != NULL)
6150 ipf_proxy_deref(n->in_apr);
6151
6152 ipf_nat_rule_fini(softc, n);
6153
6154 if (n->in_redir & NAT_REDIRECT) {
6155 if ((n->in_flags & IPN_PROXYRULE) == 0) {
6156 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_rdr);
6157 }
6158 }
6159 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) {
6160 if ((n->in_flags & IPN_PROXYRULE) == 0) {
6161 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_map);
6162 }
6163 }
6164
6165 if (n->in_tqehead[0] != NULL) {
6166 if (ipf_deletetimeoutqueue(n->in_tqehead[0]) == 0) {
6167 ipf_freetimeoutqueue(softc, n->in_tqehead[1]);
6168 }
6169 }
6170
6171 if (n->in_tqehead[1] != NULL) {
6172 if (ipf_deletetimeoutqueue(n->in_tqehead[1]) == 0) {
6173 ipf_freetimeoutqueue(softc, n->in_tqehead[1]);
6174 }
6175 }
6176
6177 if ((n->in_flags & IPN_PROXYRULE) == 0) {
6178 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules);
6179 }
6180
6181 MUTEX_DESTROY(&n->in_lock);
6182
6183 KFREES(n, n->in_size);
6184
6185 #if SOLARIS && !defined(INSTANCES)
6186 if (softn->ipf_nat_stats.ns_rules == 0)
6187 pfil_delayed_copy = 1;
6188 #endif
6189 }
6190
6191
6192 /* ------------------------------------------------------------------------ */
6193 /* Function: ipf_nat_deref */
6194 /* Returns: Nil */
6195 /* Parameters: softc(I) - pointer to soft context main structure */
6196 /* natp(I) - pointer to pointer to NAT table entry */
6197 /* */
6198 /* Decrement the reference counter for this NAT table entry and free it if */
6199 /* there are no more things using it. */
6200 /* */
6201 /* IF nat_ref == 1 when this function is called, then we have an orphan nat */
6202 /* structure *because* it only gets called on paths _after_ nat_ref has been*/
6203 /* incremented. If nat_ref == 1 then we shouldn't decrement it here */
6204 /* because nat_delete() will do that and send nat_ref to -1. */
6205 /* */
6206 /* Holding the lock on nat_lock is required to serialise nat_delete() being */
6207 /* called from a NAT flush ioctl with a deref happening because of a packet.*/
6208 /* ------------------------------------------------------------------------ */
6209 void
6210 ipf_nat_deref(ipf_main_softc_t *softc, nat_t **natp)
6211 {
6212 nat_t *nat;
6213
6214 nat = *natp;
6215 *natp = NULL;
6216
6217 MUTEX_ENTER(&nat->nat_lock);
6218 if (nat->nat_ref > 1) {
6219 nat->nat_ref--;
6220 ASSERT(nat->nat_ref >= 0);
6221 MUTEX_EXIT(&nat->nat_lock);
6222 return;
6223 }
6224 MUTEX_EXIT(&nat->nat_lock);
6225
6226 WRITE_ENTER(&softc->ipf_nat);
6227 ipf_nat_delete(softc, nat, NL_EXPIRE);
6228 RWLOCK_EXIT(&softc->ipf_nat);
6229 }
6230
6231
6232 /* ------------------------------------------------------------------------ */
6233 /* Function: ipf_nat_clone */
6234 /* Returns: ipstate_t* - NULL == cloning failed, */
6235 /* else pointer to new state structure */
6236 /* Parameters: fin(I) - pointer to packet information */
6237 /* is(I) - pointer to master state structure */
6238 /* Write Lock: ipf_nat */
6239 /* */
6240 /* Create a "duplcate" state table entry from the master. */
6241 /* ------------------------------------------------------------------------ */
6242 nat_t *
6243 ipf_nat_clone(fr_info_t *fin, nat_t *nat)
6244 {
6245 ipf_main_softc_t *softc = fin->fin_main_soft;
6246 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6247 frentry_t *fr;
6248 nat_t *clone;
6249 ipnat_t *np;
6250
6251 KMALLOC(clone, nat_t *);
6252 if (clone == NULL) {
6253 NBUMPSIDED(fin->fin_out, ns_clone_nomem);
6254 return NULL;
6255 }
6256 bcopy((char *)nat, (char *)clone, sizeof(*clone));
6257
6258 MUTEX_NUKE(&clone->nat_lock);
6259
6260 clone->nat_rev = fin->fin_rev;
6261 clone->nat_aps = NULL;
6262 /*
6263 * Initialize all these so that ipf_nat_delete() doesn't cause a crash.
6264 */
6265 clone->nat_tqe.tqe_pnext = NULL;
6266 clone->nat_tqe.tqe_next = NULL;
6267 clone->nat_tqe.tqe_ifq = NULL;
6268 clone->nat_tqe.tqe_parent = clone;
6269
6270 clone->nat_flags &= ~SI_CLONE;
6271 clone->nat_flags |= SI_CLONED;
6272
6273 if (clone->nat_hm)
6274 clone->nat_hm->hm_ref++;
6275
6276 if (ipf_nat_insert(softc, softn, clone) == -1) {
6277 KFREE(clone);
6278 NBUMPSIDED(fin->fin_out, ns_insert_fail);
6279 return NULL;
6280 }
6281
6282 np = clone->nat_ptr;
6283 if (np != NULL) {
6284 if (softn->ipf_nat_logging)
6285 ipf_nat_log(softc, softn, clone, NL_CLONE);
6286 np->in_use++;
6287 }
6288 fr = clone->nat_fr;
6289 if (fr != NULL) {
6290 MUTEX_ENTER(&fr->fr_lock);
6291 fr->fr_ref++;
6292 MUTEX_EXIT(&fr->fr_lock);
6293 }
6294
6295
6296 /*
6297 * Because the clone is created outside the normal loop of things and
6298 * TCP has special needs in terms of state, initialise the timeout
6299 * state of the new NAT from here.
6300 */
6301 if (clone->nat_pr[0] == IPPROTO_TCP) {
6302 (void) ipf_tcp_age(&clone->nat_tqe, fin, softn->ipf_nat_tcptq,
6303 clone->nat_flags, 2);
6304 }
6305 clone->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, clone);
6306 if (softn->ipf_nat_logging)
6307 ipf_nat_log(softc, softn, clone, NL_CLONE);
6308 return clone;
6309 }
6310
6311
6312 /* ------------------------------------------------------------------------ */
6313 /* Function: ipf_nat_wildok */
6314 /* Returns: int - 1 == packet's ports match wildcards */
6315 /* 0 == packet's ports don't match wildcards */
6316 /* Parameters: nat(I) - NAT entry */
6317 /* sport(I) - source port */
6318 /* dport(I) - destination port */
6319 /* flags(I) - wildcard flags */
6320 /* dir(I) - packet direction */
6321 /* */
6322 /* Use NAT entry and packet direction to determine which combination of */
6323 /* wildcard flags should be used. */
6324 /* ------------------------------------------------------------------------ */
6325 int
6326 ipf_nat_wildok(nat_t *nat, int sport, int dport, int flags, int dir)
6327 {
6328 /*
6329 * When called by dir is set to
6330 * nat_inlookup NAT_INBOUND (0)
6331 * nat_outlookup NAT_OUTBOUND (1)
6332 *
6333 * We simply combine the packet's direction in dir with the original
6334 * "intended" direction of that NAT entry in nat->nat_dir to decide
6335 * which combination of wildcard flags to allow.
6336 */
6337 switch ((dir << 1) | (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND)))
6338 {
6339 case 3: /* outbound packet / outbound entry */
6340 if (((nat->nat_osport == sport) ||
6341 (flags & SI_W_SPORT)) &&
6342 ((nat->nat_odport == dport) ||
6343 (flags & SI_W_DPORT)))
6344 return 1;
6345 break;
6346 case 2: /* outbound packet / inbound entry */
6347 if (((nat->nat_osport == dport) ||
6348 (flags & SI_W_SPORT)) &&
6349 ((nat->nat_odport == sport) ||
6350 (flags & SI_W_DPORT)))
6351 return 1;
6352 break;
6353 case 1: /* inbound packet / outbound entry */
6354 if (((nat->nat_osport == dport) ||
6355 (flags & SI_W_SPORT)) &&
6356 ((nat->nat_odport == sport) ||
6357 (flags & SI_W_DPORT)))
6358 return 1;
6359 break;
6360 case 0: /* inbound packet / inbound entry */
6361 if (((nat->nat_osport == sport) ||
6362 (flags & SI_W_SPORT)) &&
6363 ((nat->nat_odport == dport) ||
6364 (flags & SI_W_DPORT)))
6365 return 1;
6366 break;
6367 default:
6368 break;
6369 }
6370
6371 return(0);
6372 }
6373
6374
6375 /* ------------------------------------------------------------------------ */
6376 /* Function: nat_mssclamp */
6377 /* Returns: Nil */
6378 /* Parameters: tcp(I) - pointer to TCP header */
6379 /* maxmss(I) - value to clamp the TCP MSS to */
6380 /* fin(I) - pointer to packet information */
6381 /* csump(I) - pointer to TCP checksum */
6382 /* */
6383 /* Check for MSS option and clamp it if necessary. If found and changed, */
6384 /* then the TCP header checksum will be updated to reflect the change in */
6385 /* the MSS. */
6386 /* ------------------------------------------------------------------------ */
6387 static void
6388 ipf_nat_mssclamp(tcphdr_t *tcp, u_32_t maxmss, fr_info_t *fin, u_short *csump)
6389 {
6390 u_char *cp, *ep, opt;
6391 int hlen, advance;
6392 u_32_t mss, sumd;
6393
6394 hlen = TCP_OFF(tcp) << 2;
6395 if (hlen > sizeof(*tcp)) {
6396 cp = (u_char *)tcp + sizeof(*tcp);
6397 ep = (u_char *)tcp + hlen;
6398
6399 while (cp < ep) {
6400 opt = cp[0];
6401 if (opt == TCPOPT_EOL)
6402 break;
6403 else if (opt == TCPOPT_NOP) {
6404 cp++;
6405 continue;
6406 }
6407
6408 if (cp + 1 >= ep)
6409 break;
6410 advance = cp[1];
6411 if ((cp + advance > ep) || (advance <= 0))
6412 break;
6413 switch (opt)
6414 {
6415 case TCPOPT_MAXSEG:
6416 if (advance != 4)
6417 break;
6418 mss = cp[2] * 256 + cp[3];
6419 if (mss > maxmss) {
6420 cp[2] = maxmss / 256;
6421 cp[3] = maxmss & 0xff;
6422 CALC_SUMD(mss, maxmss, sumd);
6423 ipf_fix_outcksum(0, csump, sumd, 0);
6424 }
6425 break;
6426 default:
6427 /* ignore unknown options */
6428 break;
6429 }
6430
6431 cp += advance;
6432 }
6433 }
6434 }
6435
6436
6437 /* ------------------------------------------------------------------------ */
6438 /* Function: ipf_nat_setqueue */
6439 /* Returns: Nil */
6440 /* Parameters: softc(I) - pointer to soft context main structure */
6441 /* softn(I) - pointer to NAT context structure */
6442 /* nat(I)- pointer to NAT structure */
6443 /* Locks: ipf_nat (read or write) */
6444 /* */
6445 /* Put the NAT entry on its default queue entry, using rev as a helped in */
6446 /* determining which queue it should be placed on. */
6447 /* ------------------------------------------------------------------------ */
6448 void
6449 ipf_nat_setqueue(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat)
6450 {
6451 ipftq_t *oifq, *nifq;
6452 int rev = nat->nat_rev;
6453
6454 if (nat->nat_ptr != NULL)
6455 nifq = nat->nat_ptr->in_tqehead[rev];
6456 else
6457 nifq = NULL;
6458
6459 if (nifq == NULL) {
6460 switch (nat->nat_pr[0])
6461 {
6462 case IPPROTO_UDP :
6463 nifq = &softn->ipf_nat_udptq;
6464 break;
6465 case IPPROTO_ICMP :
6466 nifq = &softn->ipf_nat_icmptq;
6467 break;
6468 case IPPROTO_TCP :
6469 nifq = softn->ipf_nat_tcptq +
6470 nat->nat_tqe.tqe_state[rev];
6471 break;
6472 default :
6473 nifq = &softn->ipf_nat_iptq;
6474 break;
6475 }
6476 }
6477
6478 oifq = nat->nat_tqe.tqe_ifq;
6479 /*
6480 * If it's currently on a timeout queue, move it from one queue to
6481 * another, else put it on the end of the newly determined queue.
6482 */
6483 if (oifq != NULL)
6484 ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq, nifq);
6485 else
6486 ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe, nifq, nat);
6487 return;
6488 }
6489
6490
6491 /* ------------------------------------------------------------------------ */
6492 /* Function: nat_getnext */
6493 /* Returns: int - 0 == ok, else error */
6494 /* Parameters: softc(I) - pointer to soft context main structure */
6495 /* t(I) - pointer to ipftoken structure */
6496 /* itp(I) - pointer to ipfgeniter_t structure */
6497 /* */
6498 /* Fetch the next nat/ipnat structure pointer from the linked list and */
6499 /* copy it out to the storage space pointed to by itp_data. The next item */
6500 /* in the list to look at is put back in the ipftoken struture. */
6501 /* ------------------------------------------------------------------------ */
6502 static int
6503 ipf_nat_getnext(ipf_main_softc_t *softc, ipftoken_t *t, ipfgeniter_t *itp,
6504 ipfobj_t *objp)
6505 {
6506 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6507 hostmap_t *hm, *nexthm = NULL, zerohm;
6508 ipnat_t *ipn, *nextipnat = NULL, zeroipn;
6509 nat_t *nat, *nextnat = NULL, zeronat;
6510 int error = 0;
6511 void *nnext;
6512
6513 if (itp->igi_nitems != 1) {
6514 IPFERROR(60075);
6515 return ENOSPC;
6516 }
6517
6518 READ_ENTER(&softc->ipf_nat);
6519
6520 switch (itp->igi_type)
6521 {
6522 case IPFGENITER_HOSTMAP :
6523 hm = t->ipt_data;
6524 if (hm == NULL) {
6525 nexthm = softn->ipf_hm_maplist;
6526 } else {
6527 nexthm = hm->hm_next;
6528 }
6529 if (nexthm != NULL) {
6530 ATOMIC_INC32(nexthm->hm_ref);
6531 t->ipt_data = nexthm;
6532 } else {
6533 bzero(&zerohm, sizeof(zerohm));
6534 nexthm = &zerohm;
6535 t->ipt_data = NULL;
6536 }
6537 nnext = nexthm->hm_next;
6538 break;
6539
6540 case IPFGENITER_IPNAT :
6541 ipn = t->ipt_data;
6542 if (ipn == NULL) {
6543 nextipnat = softn->ipf_nat_list;
6544 } else {
6545 nextipnat = ipn->in_next;
6546 }
6547 if (nextipnat != NULL) {
6548 ATOMIC_INC32(nextipnat->in_use);
6549 t->ipt_data = nextipnat;
6550 } else {
6551 bzero(&zeroipn, sizeof(zeroipn));
6552 nextipnat = &zeroipn;
6553 t->ipt_data = NULL;
6554 }
6555 nnext = nextipnat->in_next;
6556 break;
6557
6558 case IPFGENITER_NAT :
6559 nat = t->ipt_data;
6560 if (nat == NULL) {
6561 nextnat = softn->ipf_nat_instances;
6562 } else {
6563 nextnat = nat->nat_next;
6564 }
6565 if (nextnat != NULL) {
6566 MUTEX_ENTER(&nextnat->nat_lock);
6567 nextnat->nat_ref++;
6568 MUTEX_EXIT(&nextnat->nat_lock);
6569 t->ipt_data = nextnat;
6570 } else {
6571 bzero(&zeronat, sizeof(zeronat));
6572 nextnat = &zeronat;
6573 t->ipt_data = NULL;
6574 }
6575 nnext = nextnat->nat_next;
6576 break;
6577
6578 default :
6579 RWLOCK_EXIT(&softc->ipf_nat);
6580 IPFERROR(60055);
6581 return EINVAL;
6582 }
6583
6584 RWLOCK_EXIT(&softc->ipf_nat);
6585
6586 objp->ipfo_ptr = itp->igi_data;
6587
6588 switch (itp->igi_type)
6589 {
6590 case IPFGENITER_HOSTMAP :
6591 error = COPYOUT(nexthm, objp->ipfo_ptr, sizeof(*nexthm));
6592 if (error != 0) {
6593 IPFERROR(60049);
6594 error = EFAULT;
6595 }
6596 if (hm != NULL) {
6597 WRITE_ENTER(&softc->ipf_nat);
6598 ipf_nat_hostmapdel(softc, &hm);
6599 RWLOCK_EXIT(&softc->ipf_nat);
6600 }
6601 break;
6602
6603 case IPFGENITER_IPNAT :
6604 objp->ipfo_size = nextipnat->in_size;
6605 objp->ipfo_type = IPFOBJ_IPNAT;
6606 error = ipf_outobjk(softc, objp, nextipnat);
6607 if (ipn != NULL) {
6608 WRITE_ENTER(&softc->ipf_nat);
6609 ipf_nat_rule_deref(softc, &ipn);
6610 RWLOCK_EXIT(&softc->ipf_nat);
6611 }
6612 break;
6613
6614 case IPFGENITER_NAT :
6615 objp->ipfo_size = sizeof(nat_t);
6616 objp->ipfo_type = IPFOBJ_NAT;
6617 error = ipf_outobjk(softc, objp, nextnat);
6618 if (nat != NULL)
6619 ipf_nat_deref(softc, &nat);
6620
6621 break;
6622 }
6623
6624 if (nnext == NULL)
6625 ipf_token_mark_complete(t);
6626
6627 return error;
6628 }
6629
6630
6631 /* ------------------------------------------------------------------------ */
6632 /* Function: nat_extraflush */
6633 /* Returns: int - 0 == success, -1 == failure */
6634 /* Parameters: softc(I) - pointer to soft context main structure */
6635 /* softn(I) - pointer to NAT context structure */
6636 /* which(I) - how to flush the active NAT table */
6637 /* Write Locks: ipf_nat */
6638 /* */
6639 /* Flush nat tables. Three actions currently defined: */
6640 /* which == 0 : flush all nat table entries */
6641 /* which == 1 : flush TCP connections which have started to close but are */
6642 /* stuck for some reason. */
6643 /* which == 2 : flush TCP connections which have been idle for a long time, */
6644 /* starting at > 4 days idle and working back in successive half-*/
6645 /* days to at most 12 hours old. If this fails to free enough */
6646 /* slots then work backwards in half hour slots to 30 minutes. */
6647 /* If that too fails, then work backwards in 30 second intervals */
6648 /* for the last 30 minutes to at worst 30 seconds idle. */
6649 /* ------------------------------------------------------------------------ */
6650 static int
6651 ipf_nat_extraflush(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, int which)
6652 {
6653 nat_t *nat, **natp;
6654 ipftqent_t *tqn;
6655 ipftq_t *ifq;
6656 int removed;
6657 SPL_INT(s);
6658
6659 removed = 0;
6660
6661 SPL_NET(s);
6662 switch (which)
6663 {
6664 case 0 :
6665 softn->ipf_nat_stats.ns_flush_all++;
6666 /*
6667 * Style 0 flush removes everything...
6668 */
6669 for (natp = &softn->ipf_nat_instances;
6670 ((nat = *natp) != NULL); ) {
6671 ipf_nat_delete(softc, nat, NL_FLUSH);
6672 removed++;
6673 }
6674 break;
6675
6676 case 1 :
6677 softn->ipf_nat_stats.ns_flush_closing++;
6678 /*
6679 * Since we're only interested in things that are closing,
6680 * we can start with the appropriate timeout queue.
6681 */
6682 for (ifq = softn->ipf_nat_tcptq + IPF_TCPS_CLOSE_WAIT;
6683 ifq != NULL; ifq = ifq->ifq_next) {
6684
6685 for (tqn = ifq->ifq_head; tqn != NULL; ) {
6686 nat = tqn->tqe_parent;
6687 tqn = tqn->tqe_next;
6688 if (nat->nat_pr[0] != IPPROTO_TCP ||
6689 nat->nat_pr[1] != IPPROTO_TCP)
6690 break;
6691 ipf_nat_delete(softc, nat, NL_EXPIRE);
6692 removed++;
6693 }
6694 }
6695
6696 /*
6697 * Also need to look through the user defined queues.
6698 */
6699 for (ifq = softn->ipf_nat_utqe; ifq != NULL;
6700 ifq = ifq->ifq_next) {
6701 for (tqn = ifq->ifq_head; tqn != NULL; ) {
6702 nat = tqn->tqe_parent;
6703 tqn = tqn->tqe_next;
6704 if (nat->nat_pr[0] != IPPROTO_TCP ||
6705 nat->nat_pr[1] != IPPROTO_TCP)
6706 continue;
6707
6708 if ((nat->nat_tcpstate[0] >
6709 IPF_TCPS_ESTABLISHED) &&
6710 (nat->nat_tcpstate[1] >
6711 IPF_TCPS_ESTABLISHED)) {
6712 ipf_nat_delete(softc, nat, NL_EXPIRE);
6713 removed++;
6714 }
6715 }
6716 }
6717 break;
6718
6719 /*
6720 * Args 5-11 correspond to flushing those particular states
6721 * for TCP connections.
6722 */
6723 case IPF_TCPS_CLOSE_WAIT :
6724 case IPF_TCPS_FIN_WAIT_1 :
6725 case IPF_TCPS_CLOSING :
6726 case IPF_TCPS_LAST_ACK :
6727 case IPF_TCPS_FIN_WAIT_2 :
6728 case IPF_TCPS_TIME_WAIT :
6729 case IPF_TCPS_CLOSED :
6730 softn->ipf_nat_stats.ns_flush_state++;
6731 tqn = softn->ipf_nat_tcptq[which].ifq_head;
6732 while (tqn != NULL) {
6733 nat = tqn->tqe_parent;
6734 tqn = tqn->tqe_next;
6735 ipf_nat_delete(softc, nat, NL_FLUSH);
6736 removed++;
6737 }
6738 break;
6739
6740 default :
6741 if (which < 30)
6742 break;
6743
6744 softn->ipf_nat_stats.ns_flush_timeout++;
6745 /*
6746 * Take a large arbitrary number to mean the number of seconds
6747 * for which which consider to be the maximum value we'll allow
6748 * the expiration to be.
6749 */
6750 which = IPF_TTLVAL(which);
6751 for (natp = &softn->ipf_nat_instances;
6752 ((nat = *natp) != NULL); ) {
6753 if (softc->ipf_ticks - nat->nat_touched > which) {
6754 ipf_nat_delete(softc, nat, NL_FLUSH);
6755 removed++;
6756 } else
6757 natp = &nat->nat_next;
6758 }
6759 break;
6760 }
6761
6762 if (which != 2) {
6763 SPL_X(s);
6764 return removed;
6765 }
6766
6767 softn->ipf_nat_stats.ns_flush_queue++;
6768
6769 /*
6770 * Asked to remove inactive entries because the table is full, try
6771 * again, 3 times, if first attempt failed with a different criteria
6772 * each time. The order tried in must be in decreasing age.
6773 * Another alternative is to implement random drop and drop N entries
6774 * at random until N have been freed up.
6775 */
6776 if (softc->ipf_ticks - softn->ipf_nat_last_force_flush >
6777 IPF_TTLVAL(5)) {
6778 softn->ipf_nat_last_force_flush = softc->ipf_ticks;
6779
6780 removed = ipf_queueflush(softc, ipf_nat_flush_entry,
6781 softn->ipf_nat_tcptq,
6782 softn->ipf_nat_utqe,
6783 &softn->ipf_nat_stats.ns_active,
6784 softn->ipf_nat_table_sz,
6785 softn->ipf_nat_table_wm_low);
6786 }
6787
6788 SPL_X(s);
6789 return removed;
6790 }
6791
6792
6793 /* ------------------------------------------------------------------------ */
6794 /* Function: ipf_nat_flush_entry */
6795 /* Returns: 0 - always succeeds */
6796 /* Parameters: softc(I) - pointer to soft context main structure */
6797 /* entry(I) - pointer to NAT entry */
6798 /* Write Locks: ipf_nat */
6799 /* */
6800 /* This function is a stepping stone between ipf_queueflush() and */
6801 /* nat_dlete(). It is used so we can provide a uniform interface via the */
6802 /* ipf_queueflush() function. Since the nat_delete() function returns void */
6803 /* we translate that to mean it always succeeds in deleting something. */
6804 /* ------------------------------------------------------------------------ */
6805 static int
6806 ipf_nat_flush_entry(ipf_main_softc_t *softc, void *entry)
6807 {
6808 ipf_nat_delete(softc, entry, NL_FLUSH);
6809 return 0;
6810 }
6811
6812
6813 /* ------------------------------------------------------------------------ */
6814 /* Function: ipf_nat_iterator */
6815 /* Returns: int - 0 == ok, else error */
6816 /* Parameters: softc(I) - pointer to soft context main structure */
6817 /* token(I) - pointer to ipftoken structure */
6818 /* itp(I) - pointer to ipfgeniter_t structure */
6819 /* obj(I) - pointer to data description structure */
6820 /* */
6821 /* This function acts as a handler for the SIOCGENITER ioctls that use a */
6822 /* generic structure to iterate through a list. There are three different */
6823 /* linked lists of NAT related information to go through: NAT rules, active */
6824 /* NAT mappings and the NAT fragment cache. */
6825 /* ------------------------------------------------------------------------ */
6826 static int
6827 ipf_nat_iterator(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp,
6828 ipfobj_t *obj)
6829 {
6830 int error;
6831
6832 if (itp->igi_data == NULL) {
6833 IPFERROR(60052);
6834 return EFAULT;
6835 }
6836
6837 switch (itp->igi_type)
6838 {
6839 case IPFGENITER_HOSTMAP :
6840 case IPFGENITER_IPNAT :
6841 case IPFGENITER_NAT :
6842 error = ipf_nat_getnext(softc, token, itp, obj);
6843 break;
6844
6845 case IPFGENITER_NATFRAG :
6846 error = ipf_frag_nat_next(softc, token, itp);
6847 break;
6848 default :
6849 IPFERROR(60053);
6850 error = EINVAL;
6851 break;
6852 }
6853
6854 return error;
6855 }
6856
6857
6858 /* ------------------------------------------------------------------------ */
6859 /* Function: ipf_nat_setpending */
6860 /* Returns: Nil */
6861 /* Parameters: softc(I) - pointer to soft context main structure */
6862 /* nat(I) - pointer to NAT structure */
6863 /* Locks: ipf_nat (read or write) */
6864 /* */
6865 /* Put the NAT entry on to the pending queue - this queue has a very short */
6866 /* lifetime where items are put that can't be deleted straight away because */
6867 /* of locking issues but we want to delete them ASAP, anyway. In calling */
6868 /* this function, it is assumed that the owner (if there is one, as shown */
6869 /* by nat_me) is no longer interested in it. */
6870 /* ------------------------------------------------------------------------ */
6871 void
6872 ipf_nat_setpending(ipf_main_softc_t *softc, nat_t *nat)
6873 {
6874 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
6875 ipftq_t *oifq;
6876
6877 oifq = nat->nat_tqe.tqe_ifq;
6878 if (oifq != NULL)
6879 ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq,
6880 &softn->ipf_nat_pending);
6881 else
6882 ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe,
6883 &softn->ipf_nat_pending, nat);
6884
6885 if (nat->nat_me != NULL) {
6886 *nat->nat_me = NULL;
6887 nat->nat_me = NULL;
6888 nat->nat_ref--;
6889 ASSERT(nat->nat_ref >= 0);
6890 }
6891 }
6892
6893
6894 /* ------------------------------------------------------------------------ */
6895 /* Function: nat_newrewrite */
6896 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */
6897 /* allow rule to be moved if IPN_ROUNDR is set. */
6898 /* Parameters: fin(I) - pointer to packet information */
6899 /* nat(I) - pointer to NAT entry */
6900 /* ni(I) - pointer to structure with misc. information needed */
6901 /* to create new NAT entry. */
6902 /* Write Lock: ipf_nat */
6903 /* */
6904 /* This function is responsible for setting up an active NAT session where */
6905 /* we are changing both the source and destination parameters at the same */
6906 /* time. The loop in here works differently to elsewhere - each iteration */
6907 /* is responsible for changing a single parameter that can be incremented. */
6908 /* So one pass may increase the source IP#, next source port, next dest. IP#*/
6909 /* and the last destination port for a total of 4 iterations to try each. */
6910 /* This is done to try and exhaustively use the translation space available.*/
6911 /* ------------------------------------------------------------------------ */
6912 static int
6913 ipf_nat_newrewrite(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
6914 {
6915 int src_search = 1;
6916 int dst_search = 1;
6917 fr_info_t frnat;
6918 u_32_t flags;
6919 u_short swap;
6920 ipnat_t *np;
6921 nat_t *natl;
6922 int l = 0;
6923 int changed;
6924
6925 natl = NULL;
6926 changed = -1;
6927 np = nai->nai_np;
6928 flags = nat->nat_flags;
6929 bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
6930
6931 nat->nat_hm = NULL;
6932
6933 do {
6934 changed = -1;
6935 /* TRACE (l, src_search, dst_search, np) */
6936
6937 if ((src_search == 0) && (np->in_spnext == 0) &&
6938 (dst_search == 0) && (np->in_dpnext == 0)) {
6939 if (l > 0)
6940 return -1;
6941 }
6942
6943 /*
6944 * Find a new source address
6945 */
6946 if (ipf_nat_nextaddr(fin, &np->in_nsrc, &frnat.fin_saddr,
6947 &frnat.fin_saddr) == -1) {
6948 return -1;
6949 }
6950
6951 if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0xffffffff)) {
6952 src_search = 0;
6953 if (np->in_stepnext == 0)
6954 np->in_stepnext = 1;
6955
6956 } else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) {
6957 src_search = 0;
6958 if (np->in_stepnext == 0)
6959 np->in_stepnext = 1;
6960
6961 } else if (np->in_nsrcmsk == 0xffffffff) {
6962 src_search = 0;
6963 if (np->in_stepnext == 0)
6964 np->in_stepnext = 1;
6965
6966 } else if (np->in_nsrcmsk != 0xffffffff) {
6967 if (np->in_stepnext == 0 && changed == -1) {
6968 np->in_snip++;
6969 np->in_stepnext++;
6970 changed = 0;
6971 }
6972 }
6973
6974 if ((flags & IPN_TCPUDPICMP) != 0) {
6975 if (np->in_spnext != 0)
6976 frnat.fin_data[0] = np->in_spnext;
6977
6978 /*
6979 * Standard port translation. Select next port.
6980 */
6981 if ((flags & IPN_FIXEDSPORT) != 0) {
6982 np->in_stepnext = 2;
6983 } else if ((np->in_stepnext == 1) &&
6984 (changed == -1) && (natl != NULL)) {
6985 np->in_spnext++;
6986 np->in_stepnext++;
6987 changed = 1;
6988 if (np->in_spnext > np->in_spmax)
6989 np->in_spnext = np->in_spmin;
6990 }
6991 } else {
6992 np->in_stepnext = 2;
6993 }
6994 np->in_stepnext &= 0x3;
6995
6996 /*
6997 * Find a new destination address
6998 */
6999 /* TRACE (fin, np, l, frnat) */
7000
7001 if (ipf_nat_nextaddr(fin, &np->in_ndst, &frnat.fin_daddr,
7002 &frnat.fin_daddr) == -1)
7003 return -1;
7004 if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) {
7005 dst_search = 0;
7006 if (np->in_stepnext == 2)
7007 np->in_stepnext = 3;
7008
7009 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0)) {
7010 dst_search = 0;
7011 if (np->in_stepnext == 2)
7012 np->in_stepnext = 3;
7013
7014 } else if (np->in_ndstmsk == 0xffffffff) {
7015 dst_search = 0;
7016 if (np->in_stepnext == 2)
7017 np->in_stepnext = 3;
7018
7019 } else if (np->in_ndstmsk != 0xffffffff) {
7020 if ((np->in_stepnext == 2) && (changed == -1) &&
7021 (natl != NULL)) {
7022 changed = 2;
7023 np->in_stepnext++;
7024 np->in_dnip++;
7025 }
7026 }
7027
7028 if ((flags & IPN_TCPUDPICMP) != 0) {
7029 if (np->in_dpnext != 0)
7030 frnat.fin_data[1] = np->in_dpnext;
7031
7032 /*
7033 * Standard port translation. Select next port.
7034 */
7035 if ((flags & IPN_FIXEDDPORT) != 0) {
7036 np->in_stepnext = 0;
7037 } else if (np->in_stepnext == 3 && changed == -1) {
7038 np->in_dpnext++;
7039 np->in_stepnext++;
7040 changed = 3;
7041 if (np->in_dpnext > np->in_dpmax)
7042 np->in_dpnext = np->in_dpmin;
7043 }
7044 } else {
7045 if (np->in_stepnext == 3)
7046 np->in_stepnext = 0;
7047 }
7048
7049 /* TRACE (frnat) */
7050
7051 /*
7052 * Here we do a lookup of the connection as seen from
7053 * the outside. If an IP# pair already exists, try
7054 * again. So if you have A->B becomes C->B, you can
7055 * also have D->E become C->E but not D->B causing
7056 * another C->B. Also take protocol and ports into
7057 * account when determining whether a pre-existing
7058 * NAT setup will cause an external conflict where
7059 * this is appropriate.
7060 *
7061 * fin_data[] is swapped around because we are doing a
7062 * lookup of the packet is if it were moving in the opposite
7063 * direction of the one we are working with now.
7064 */
7065 if (flags & IPN_TCPUDP) {
7066 swap = frnat.fin_data[0];
7067 frnat.fin_data[0] = frnat.fin_data[1];
7068 frnat.fin_data[1] = swap;
7069 }
7070 if (fin->fin_out == 1) {
7071 natl = ipf_nat_inlookup(&frnat,
7072 flags & ~(SI_WILDP|NAT_SEARCH),
7073 (u_int)frnat.fin_p,
7074 frnat.fin_dst, frnat.fin_src);
7075
7076 } else {
7077 natl = ipf_nat_outlookup(&frnat,
7078 flags & ~(SI_WILDP|NAT_SEARCH),
7079 (u_int)frnat.fin_p,
7080 frnat.fin_dst, frnat.fin_src);
7081 }
7082 if (flags & IPN_TCPUDP) {
7083 swap = frnat.fin_data[0];
7084 frnat.fin_data[0] = frnat.fin_data[1];
7085 frnat.fin_data[1] = swap;
7086 }
7087
7088 /* TRACE natl, in_stepnext, l */
7089
7090 if ((natl != NULL) && (l > 8)) /* XXX 8 is arbitrary */
7091 return -1;
7092
7093 np->in_stepnext &= 0x3;
7094
7095 l++;
7096 changed = -1;
7097 } while (natl != NULL);
7098
7099 nat->nat_osrcip = fin->fin_src;
7100 nat->nat_odstip = fin->fin_dst;
7101 nat->nat_nsrcip = frnat.fin_src;
7102 nat->nat_ndstip = frnat.fin_dst;
7103
7104 if ((flags & IPN_TCPUDP) != 0) {
7105 nat->nat_osport = htons(fin->fin_data[0]);
7106 nat->nat_odport = htons(fin->fin_data[1]);
7107 nat->nat_nsport = htons(frnat.fin_data[0]);
7108 nat->nat_ndport = htons(frnat.fin_data[1]);
7109 } else if ((flags & IPN_ICMPQUERY) != 0) {
7110 nat->nat_oicmpid = fin->fin_data[1];
7111 nat->nat_nicmpid = frnat.fin_data[1];
7112 }
7113
7114 return 0;
7115 }
7116
7117
7118 /* ------------------------------------------------------------------------ */
7119 /* Function: nat_newdivert */
7120 /* Returns: int - -1 == error, 0 == success */
7121 /* Parameters: fin(I) - pointer to packet information */
7122 /* nat(I) - pointer to NAT entry */
7123 /* ni(I) - pointer to structure with misc. information needed */
7124 /* to create new NAT entry. */
7125 /* Write Lock: ipf_nat */
7126 /* */
7127 /* Create a new NAT divert session as defined by the NAT rule. This is */
7128 /* somewhat different to other NAT session creation routines because we */
7129 /* do not iterate through either port numbers or IP addresses, searching */
7130 /* for a unique mapping, however, a complimentary duplicate check is made. */
7131 /* ------------------------------------------------------------------------ */
7132 static int
7133 ipf_nat_newdivert(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
7134 {
7135 ipf_main_softc_t *softc = fin->fin_main_soft;
7136 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7137 fr_info_t frnat;
7138 ipnat_t *np;
7139 nat_t *natl;
7140 int p;
7141
7142 np = nai->nai_np;
7143 bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
7144
7145 nat->nat_pr[0] = 0;
7146 nat->nat_osrcaddr = fin->fin_saddr;
7147 nat->nat_odstaddr = fin->fin_daddr;
7148 frnat.fin_saddr = htonl(np->in_snip);
7149 frnat.fin_daddr = htonl(np->in_dnip);
7150 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
7151 nat->nat_osport = htons(fin->fin_data[0]);
7152 nat->nat_odport = htons(fin->fin_data[1]);
7153 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
7154 nat->nat_oicmpid = fin->fin_data[1];
7155 }
7156
7157 if (np->in_redir & NAT_DIVERTUDP) {
7158 frnat.fin_data[0] = np->in_spnext;
7159 frnat.fin_data[1] = np->in_dpnext;
7160 frnat.fin_flx |= FI_TCPUDP;
7161 p = IPPROTO_UDP;
7162 } else {
7163 frnat.fin_flx &= ~FI_TCPUDP;
7164 p = IPPROTO_IPIP;
7165 }
7166
7167 if (fin->fin_out == 1) {
7168 natl = ipf_nat_inlookup(&frnat, 0, p,
7169 frnat.fin_dst, frnat.fin_src);
7170
7171 } else {
7172 natl = ipf_nat_outlookup(&frnat, 0, p,
7173 frnat.fin_dst, frnat.fin_src);
7174 }
7175
7176 if (natl != NULL) {
7177 NBUMPSIDED(fin->fin_out, ns_divert_exist);
7178 return -1;
7179 }
7180
7181 nat->nat_nsrcaddr = frnat.fin_saddr;
7182 nat->nat_ndstaddr = frnat.fin_daddr;
7183 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
7184 nat->nat_nsport = htons(frnat.fin_data[0]);
7185 nat->nat_ndport = htons(frnat.fin_data[1]);
7186 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
7187 nat->nat_nicmpid = frnat.fin_data[1];
7188 }
7189
7190 nat->nat_pr[fin->fin_out] = fin->fin_p;
7191 nat->nat_pr[1 - fin->fin_out] = p;
7192
7193 if (np->in_redir & NAT_REDIRECT)
7194 nat->nat_dir = NAT_DIVERTIN;
7195 else
7196 nat->nat_dir = NAT_DIVERTOUT;
7197
7198 return 0;
7199 }
7200
7201
7202 /* ------------------------------------------------------------------------ */
7203 /* Function: nat_builddivertmp */
7204 /* Returns: int - -1 == error, 0 == success */
7205 /* Parameters: softn(I) - pointer to NAT context structure */
7206 /* np(I) - pointer to a NAT rule */
7207 /* */
7208 /* For divert rules, a skeleton packet representing what will be prepended */
7209 /* to the real packet is created. Even though we don't have the full */
7210 /* packet here, a checksum is calculated that we update later when we */
7211 /* fill in the final details. At present a 0 checksum for UDP is being set */
7212 /* here because it is expected that divert will be used for localhost. */
7213 /* ------------------------------------------------------------------------ */
7214 static int
7215 ipf_nat_builddivertmp(ipf_nat_softc_t *softn, ipnat_t *np)
7216 {
7217 udphdr_t *uh;
7218 size_t len;
7219 ip_t *ip;
7220
7221 if ((np->in_redir & NAT_DIVERTUDP) != 0)
7222 len = sizeof(ip_t) + sizeof(udphdr_t);
7223 else
7224 len = sizeof(ip_t);
7225
7226 ALLOC_MB_T(np->in_divmp, len);
7227 if (np->in_divmp == NULL) {
7228 NBUMPD(ipf_nat_stats, ns_divert_build);
7229 return -1;
7230 }
7231
7232 /*
7233 * First, the header to get the packet diverted to the new destination
7234 */
7235 ip = MTOD(np->in_divmp, ip_t *);
7236 IP_V_A(ip, 4);
7237 IP_HL_A(ip, 5);
7238 ip->ip_tos = 0;
7239 if ((np->in_redir & NAT_DIVERTUDP) != 0)
7240 ip->ip_p = IPPROTO_UDP;
7241 else
7242 ip->ip_p = IPPROTO_IPIP;
7243 ip->ip_ttl = 255;
7244 ip->ip_off = 0;
7245 ip->ip_sum = 0;
7246 ip->ip_len = htons(len);
7247 ip->ip_id = 0;
7248 ip->ip_src.s_addr = htonl(np->in_snip);
7249 ip->ip_dst.s_addr = htonl(np->in_dnip);
7250 ip->ip_sum = ipf_cksum((u_short *)ip, sizeof(*ip));
7251
7252 if (np->in_redir & NAT_DIVERTUDP) {
7253 uh = (udphdr_t *)(ip + 1);
7254 uh->uh_sum = 0;
7255 uh->uh_ulen = 8;
7256 uh->uh_sport = htons(np->in_spnext);
7257 uh->uh_dport = htons(np->in_dpnext);
7258 }
7259
7260 return 0;
7261 }
7262
7263
7264 #define MINDECAP (sizeof(ip_t) + sizeof(udphdr_t) + sizeof(ip_t))
7265
7266 /* ------------------------------------------------------------------------ */
7267 /* Function: nat_decap */
7268 /* Returns: int - -1 == error, 0 == success */
7269 /* Parameters: fin(I) - pointer to packet information */
7270 /* nat(I) - pointer to current NAT session */
7271 /* */
7272 /* This function is responsible for undoing a packet's encapsulation in the */
7273 /* reverse of an encap/divert rule. After removing the outer encapsulation */
7274 /* it is necessary to call ipf_makefrip() again so that the contents of 'fin'*/
7275 /* match the "new" packet as it may still be used by IPFilter elsewhere. */
7276 /* We use "dir" here as the basis for some of the expectations about the */
7277 /* outer header. If we return an error, the goal is to leave the original */
7278 /* packet information undisturbed - this falls short at the end where we'd */
7279 /* need to back a backup copy of "fin" - expensive. */
7280 /* ------------------------------------------------------------------------ */
7281 static int
7282 ipf_nat_decap(fr_info_t *fin, nat_t *nat)
7283 {
7284 ipf_main_softc_t *softc = fin->fin_main_soft;
7285 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7286 char *hdr;
7287 int hlen;
7288 int skip;
7289 mb_t *m;
7290
7291 if ((fin->fin_flx & FI_ICMPERR) != 0) {
7292 /*
7293 * ICMP packets don't get decapsulated, instead what we need
7294 * to do is change the ICMP reply from including (in the data
7295 * portion for errors) the encapsulated packet that we sent
7296 * out to something that resembles the original packet prior
7297 * to encapsulation. This isn't done here - all we're doing
7298 * here is changing the outer address to ensure that it gets
7299 * targetted back to the correct system.
7300 */
7301
7302 if (nat->nat_dir & NAT_OUTBOUND) {
7303 u_32_t sum1, sum2, sumd;
7304
7305 sum1 = ntohl(fin->fin_daddr);
7306 sum2 = ntohl(nat->nat_osrcaddr);
7307 CALC_SUMD(sum1, sum2, sumd);
7308 fin->fin_ip->ip_dst = nat->nat_osrcip;
7309 fin->fin_daddr = nat->nat_osrcaddr;
7310 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \
7311 defined(__osf__) || defined(linux)
7312 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, sumd, 0);
7313 #endif
7314 }
7315 return 0;
7316 }
7317
7318 m = fin->fin_m;
7319 skip = fin->fin_hlen;
7320
7321 switch (nat->nat_dir)
7322 {
7323 case NAT_DIVERTIN :
7324 case NAT_DIVERTOUT :
7325 if (fin->fin_plen < MINDECAP)
7326 return -1;
7327 skip += sizeof(udphdr_t);
7328 break;
7329
7330 case NAT_ENCAPIN :
7331 case NAT_ENCAPOUT :
7332 if (fin->fin_plen < (skip + sizeof(ip_t)))
7333 return -1;
7334 break;
7335 default :
7336 return -1;
7337 /* NOTREACHED */
7338 }
7339
7340 /*
7341 * The aim here is to keep the original packet details in "fin" for
7342 * as long as possible so that returning with an error is for the
7343 * original packet and there is little undoing work to do.
7344 */
7345 if (M_LEN(m) < skip + sizeof(ip_t)) {
7346 if (ipf_pr_pullup(fin, skip + sizeof(ip_t)) == -1)
7347 return -1;
7348 }
7349
7350 hdr = MTOD(fin->fin_m, char *);
7351 fin->fin_ip = (ip_t *)(hdr + skip);
7352 hlen = IP_HL(fin->fin_ip) << 2;
7353
7354 if (ipf_pr_pullup(fin, skip + hlen) == -1) {
7355 NBUMPSIDED(fin->fin_out, ns_decap_pullup);
7356 return -1;
7357 }
7358
7359 fin->fin_hlen = hlen;
7360 fin->fin_dlen -= skip;
7361 fin->fin_plen -= skip;
7362 fin->fin_ipoff += skip;
7363
7364 if (ipf_makefrip(hlen, (ip_t *)hdr, fin) == -1) {
7365 NBUMPSIDED(fin->fin_out, ns_decap_bad);
7366 return -1;
7367 }
7368
7369 return skip;
7370 }
7371
7372
7373 /* ------------------------------------------------------------------------ */
7374 /* Function: nat_nextaddr */
7375 /* Returns: int - -1 == bad input (no new address), */
7376 /* 0 == success and dst has new address */
7377 /* Parameters: fin(I) - pointer to packet information */
7378 /* na(I) - how to generate new address */
7379 /* old(I) - original address being replaced */
7380 /* dst(O) - where to put the new address */
7381 /* Write Lock: ipf_nat */
7382 /* */
7383 /* This function uses the contents of the "na" structure, in combination */
7384 /* with "old" to produce a new address to store in "dst". Not all of the */
7385 /* possible uses of "na" will result in a new address. */
7386 /* ------------------------------------------------------------------------ */
7387 static int
7388 ipf_nat_nextaddr(fr_info_t *fin, nat_addr_t *na, u_32_t *old, u_32_t *dst)
7389 {
7390 ipf_main_softc_t *softc = fin->fin_main_soft;
7391 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7392 u_32_t amin, amax, new;
7393 i6addr_t newip;
7394 int error;
7395
7396 new = 0;
7397 amin = na->na_addr[0].in4.s_addr;
7398
7399 switch (na->na_atype)
7400 {
7401 case FRI_RANGE :
7402 amax = na->na_addr[1].in4.s_addr;
7403 break;
7404
7405 case FRI_NETMASKED :
7406 case FRI_DYNAMIC :
7407 case FRI_NORMAL :
7408 /*
7409 * Compute the maximum address by adding the inverse of the
7410 * netmask to the minimum address.
7411 */
7412 amax = ~na->na_addr[1].in4.s_addr;
7413 amax |= amin;
7414 break;
7415
7416 case FRI_LOOKUP :
7417 break;
7418
7419 case FRI_BROADCAST :
7420 case FRI_PEERADDR :
7421 case FRI_NETWORK :
7422 default :
7423 return -1;
7424 }
7425
7426 error = -1;
7427
7428 if (na->na_atype == FRI_LOOKUP) {
7429 if (na->na_type == IPLT_DSTLIST) {
7430 error = ipf_dstlist_select_node(fin, na->na_ptr, dst,
7431 NULL);
7432 } else {
7433 NBUMPSIDE(fin->fin_out, ns_badnextaddr);
7434 }
7435
7436 } else if (na->na_atype == IPLT_NONE) {
7437 /*
7438 * 0/0 as the new address means leave it alone.
7439 */
7440 if (na->na_addr[0].in4.s_addr == 0 &&
7441 na->na_addr[1].in4.s_addr == 0) {
7442 new = *old;
7443
7444 /*
7445 * 0/32 means get the interface's address
7446 */
7447 } else if (na->na_addr[0].in4.s_addr == 0 &&
7448 na->na_addr[1].in4.s_addr == 0xffffffff) {
7449 if (ipf_ifpaddr(softc, 4, na->na_atype,
7450 fin->fin_ifp, &newip, NULL) == -1) {
7451 NBUMPSIDED(fin->fin_out, ns_ifpaddrfail);
7452 return -1;
7453 }
7454 new = newip.in4.s_addr;
7455 } else {
7456 new = htonl(na->na_nextip);
7457 }
7458 *dst = new;
7459 error = 0;
7460
7461 } else {
7462 NBUMPSIDE(fin->fin_out, ns_badnextaddr);
7463 }
7464
7465 return error;
7466 }
7467
7468
7469 /* ------------------------------------------------------------------------ */
7470 /* Function: nat_nextaddrinit */
7471 /* Returns: int - 0 == success, else error number */
7472 /* Parameters: softc(I) - pointer to soft context main structure */
7473 /* na(I) - NAT address information for generating new addr*/
7474 /* initial(I) - flag indicating if it is the first call for */
7475 /* this "na" structure. */
7476 /* ifp(I) - network interface to derive address */
7477 /* information from. */
7478 /* */
7479 /* This function is expected to be called in two scenarious: when a new NAT */
7480 /* rule is loaded into the kernel and when the list of NAT rules is sync'd */
7481 /* up with the valid network interfaces (possibly due to them changing.) */
7482 /* To distinguish between these, the "initial" parameter is used. If it is */
7483 /* 1 then this indicates the rule has just been reloaded and 0 for when we */
7484 /* are updating information. This difference is important because in */
7485 /* instances where we are not updating address information associated with */
7486 /* a network interface, we don't want to disturb what the "next" address to */
7487 /* come out of ipf_nat_nextaddr() will be. */
7488 /* ------------------------------------------------------------------------ */
7489 static int
7490 ipf_nat_nextaddrinit(ipf_main_softc_t *softc, char *base, nat_addr_t *na,
7491 int initial, void *ifp)
7492 {
7493
7494 switch (na->na_atype)
7495 {
7496 case FRI_LOOKUP :
7497 if (na->na_subtype == 0) {
7498 na->na_ptr = ipf_lookup_res_num(softc, IPL_LOGNAT,
7499 na->na_type,
7500 na->na_num,
7501 &na->na_func);
7502 } else if (na->na_subtype == 1) {
7503 na->na_ptr = ipf_lookup_res_name(softc, IPL_LOGNAT,
7504 na->na_type,
7505 base + na->na_num,
7506 &na->na_func);
7507 }
7508 if (na->na_func == NULL) {
7509 IPFERROR(60060);
7510 return ESRCH;
7511 }
7512 if (na->na_ptr == NULL) {
7513 IPFERROR(60056);
7514 return ESRCH;
7515 }
7516 break;
7517
7518 case FRI_DYNAMIC :
7519 case FRI_BROADCAST :
7520 case FRI_NETWORK :
7521 case FRI_NETMASKED :
7522 case FRI_PEERADDR :
7523 if (ifp != NULL)
7524 (void )ipf_ifpaddr(softc, 4, na->na_atype, ifp,
7525 &na->na_addr[0], &na->na_addr[1]);
7526 break;
7527
7528 case FRI_SPLIT :
7529 case FRI_RANGE :
7530 if (initial)
7531 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
7532 break;
7533
7534 case FRI_NONE :
7535 na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
7536 return 0;
7537
7538 case FRI_NORMAL :
7539 na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr;
7540 break;
7541
7542 default :
7543 IPFERROR(60054);
7544 return EINVAL;
7545 }
7546
7547 if (initial && (na->na_atype == FRI_NORMAL)) {
7548 if (na->na_addr[0].in4.s_addr == 0) {
7549 if ((na->na_addr[1].in4.s_addr == 0xffffffff) ||
7550 (na->na_addr[1].in4.s_addr == 0)) {
7551 return 0;
7552 }
7553 }
7554
7555 if (na->na_addr[1].in4.s_addr == 0xffffffff) {
7556 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr);
7557 } else {
7558 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr) + 1;
7559 }
7560 }
7561
7562 return 0;
7563 }
7564
7565
7566 /* ------------------------------------------------------------------------ */
7567 /* Function: ipf_nat_matchflush */
7568 /* Returns: int - -1 == error, 0 == success */
7569 /* Parameters: softc(I) - pointer to soft context main structure */
7570 /* softn(I) - pointer to NAT context structure */
7571 /* nat(I) - pointer to current NAT session */
7572 /* */
7573 /* ------------------------------------------------------------------------ */
7574 static int
7575 ipf_nat_matchflush(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, void *data)
7576 {
7577 int *array, flushed, error;
7578 nat_t *nat, *natnext;
7579 ipfobj_t obj;
7580
7581 error = ipf_matcharray_load(softc, data, &obj, &array);
7582 if (error != 0)
7583 return error;
7584
7585 flushed = 0;
7586
7587 for (nat = softn->ipf_nat_instances; nat != NULL; nat = natnext) {
7588 natnext = nat->nat_next;
7589 if (ipf_nat_matcharray(nat, array, softc->ipf_ticks) == 0) {
7590 ipf_nat_delete(softc, nat, NL_FLUSH);
7591 flushed++;
7592 }
7593 }
7594
7595 obj.ipfo_retval = flushed;
7596 error = BCOPYOUT(&obj, data, sizeof(obj));
7597
7598 KFREES(array, array[0] * sizeof(*array));
7599
7600 return error;
7601 }
7602
7603
7604 /* ------------------------------------------------------------------------ */
7605 /* Function: ipf_nat_matcharray */
7606 /* Returns: int - -1 == error, 0 == success */
7607 /* Parameters: fin(I) - pointer to packet information */
7608 /* nat(I) - pointer to current NAT session */
7609 /* */
7610 /* ------------------------------------------------------------------------ */
7611 static int
7612 ipf_nat_matcharray(nat_t *nat, int *array, u_long ticks)
7613 {
7614 int i, n, *x, e, p;
7615
7616 e = 0;
7617 n = array[0];
7618 x = array + 1;
7619
7620 for (; n > 0; x += 3 + x[2]) {
7621 if (x[0] == IPF_EXP_END)
7622 break;
7623 e = 0;
7624
7625 n -= x[2] + 3;
7626 if (n < 0)
7627 break;
7628
7629 p = x[0] >> 16;
7630 if (p != 0 && p != nat->nat_pr[1])
7631 break;
7632
7633 switch (x[0])
7634 {
7635 case IPF_EXP_IP_PR :
7636 for (i = 0; !e && i < x[2]; i++) {
7637 e |= (nat->nat_pr[1] == x[i + 3]);
7638 }
7639 break;
7640
7641 case IPF_EXP_IP_SRCADDR :
7642 if (nat->nat_v[0] == 4) {
7643 for (i = 0; !e && i < x[2]; i++) {
7644 e |= ((nat->nat_osrcaddr & x[i + 4]) ==
7645 x[i + 3]);
7646 }
7647 }
7648 if (nat->nat_v[1] == 4) {
7649 for (i = 0; !e && i < x[2]; i++) {
7650 e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
7651 x[i + 3]);
7652 }
7653 }
7654 break;
7655
7656 case IPF_EXP_IP_DSTADDR :
7657 if (nat->nat_v[0] == 4) {
7658 for (i = 0; !e && i < x[2]; i++) {
7659 e |= ((nat->nat_odstaddr & x[i + 4]) ==
7660 x[i + 3]);
7661 }
7662 }
7663 if (nat->nat_v[1] == 4) {
7664 for (i = 0; !e && i < x[2]; i++) {
7665 e |= ((nat->nat_ndstaddr & x[i + 4]) ==
7666 x[i + 3]);
7667 }
7668 }
7669 break;
7670
7671 case IPF_EXP_IP_ADDR :
7672 for (i = 0; !e && i < x[2]; i++) {
7673 if (nat->nat_v[0] == 4) {
7674 e |= ((nat->nat_osrcaddr & x[i + 4]) ==
7675 x[i + 3]);
7676 }
7677 if (nat->nat_v[1] == 4) {
7678 e |= ((nat->nat_nsrcaddr & x[i + 4]) ==
7679 x[i + 3]);
7680 }
7681 if (nat->nat_v[0] == 4) {
7682 e |= ((nat->nat_odstaddr & x[i + 4]) ==
7683 x[i + 3]);
7684 }
7685 if (nat->nat_v[1] == 4) {
7686 e |= ((nat->nat_ndstaddr & x[i + 4]) ==
7687 x[i + 3]);
7688 }
7689 }
7690 break;
7691
7692 #ifdef USE_INET6
7693 case IPF_EXP_IP6_SRCADDR :
7694 if (nat->nat_v[0] == 6) {
7695 for (i = 0; !e && i < x[3]; i++) {
7696 e |= IP6_MASKEQ(&nat->nat_osrc6,
7697 x + i + 7, x + i + 3);
7698 }
7699 }
7700 if (nat->nat_v[1] == 6) {
7701 for (i = 0; !e && i < x[3]; i++) {
7702 e |= IP6_MASKEQ(&nat->nat_nsrc6,
7703 x + i + 7, x + i + 3);
7704 }
7705 }
7706 break;
7707
7708 case IPF_EXP_IP6_DSTADDR :
7709 if (nat->nat_v[0] == 6) {
7710 for (i = 0; !e && i < x[3]; i++) {
7711 e |= IP6_MASKEQ(&nat->nat_odst6,
7712 x + i + 7,
7713 x + i + 3);
7714 }
7715 }
7716 if (nat->nat_v[1] == 6) {
7717 for (i = 0; !e && i < x[3]; i++) {
7718 e |= IP6_MASKEQ(&nat->nat_ndst6,
7719 x + i + 7,
7720 x + i + 3);
7721 }
7722 }
7723 break;
7724
7725 case IPF_EXP_IP6_ADDR :
7726 for (i = 0; !e && i < x[3]; i++) {
7727 if (nat->nat_v[0] == 6) {
7728 e |= IP6_MASKEQ(&nat->nat_osrc6,
7729 x + i + 7,
7730 x + i + 3);
7731 }
7732 if (nat->nat_v[0] == 6) {
7733 e |= IP6_MASKEQ(&nat->nat_odst6,
7734 x + i + 7,
7735 x + i + 3);
7736 }
7737 if (nat->nat_v[1] == 6) {
7738 e |= IP6_MASKEQ(&nat->nat_nsrc6,
7739 x + i + 7,
7740 x + i + 3);
7741 }
7742 if (nat->nat_v[1] == 6) {
7743 e |= IP6_MASKEQ(&nat->nat_ndst6,
7744 x + i + 7,
7745 x + i + 3);
7746 }
7747 }
7748 break;
7749 #endif
7750
7751 case IPF_EXP_UDP_PORT :
7752 case IPF_EXP_TCP_PORT :
7753 for (i = 0; !e && i < x[2]; i++) {
7754 e |= (nat->nat_nsport == x[i + 3]) ||
7755 (nat->nat_ndport == x[i + 3]);
7756 }
7757 break;
7758
7759 case IPF_EXP_UDP_SPORT :
7760 case IPF_EXP_TCP_SPORT :
7761 for (i = 0; !e && i < x[2]; i++) {
7762 e |= (nat->nat_nsport == x[i + 3]);
7763 }
7764 break;
7765
7766 case IPF_EXP_UDP_DPORT :
7767 case IPF_EXP_TCP_DPORT :
7768 for (i = 0; !e && i < x[2]; i++) {
7769 e |= (nat->nat_ndport == x[i + 3]);
7770 }
7771 break;
7772
7773 case IPF_EXP_TCP_STATE :
7774 for (i = 0; !e && i < x[2]; i++) {
7775 e |= (nat->nat_tcpstate[0] == x[i + 3]) ||
7776 (nat->nat_tcpstate[1] == x[i + 3]);
7777 }
7778 break;
7779
7780 case IPF_EXP_IDLE_GT :
7781 e |= (ticks - nat->nat_touched > x[3]);
7782 break;
7783 }
7784 e ^= x[1];
7785
7786 if (!e)
7787 break;
7788 }
7789
7790 return e;
7791 }
7792
7793
7794 /* ------------------------------------------------------------------------ */
7795 /* Function: ipf_nat_gettable */
7796 /* Returns: int - 0 = success, else error */
7797 /* Parameters: softc(I) - pointer to soft context main structure */
7798 /* softn(I) - pointer to NAT context structure */
7799 /* data(I) - pointer to ioctl data */
7800 /* */
7801 /* This function handles ioctl requests for tables of nat information. */
7802 /* At present the only table it deals with is the hash bucket statistics. */
7803 /* ------------------------------------------------------------------------ */
7804 static int
7805 ipf_nat_gettable(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, char *data)
7806 {
7807 ipftable_t table;
7808 int error;
7809
7810 error = ipf_inobj(softc, data, NULL, &table, IPFOBJ_GTABLE);
7811 if (error != 0)
7812 return error;
7813
7814 switch (table.ita_type)
7815 {
7816 case IPFTABLE_BUCKETS_NATIN :
7817 error = COPYOUT(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
7818 table.ita_table,
7819 softn->ipf_nat_table_sz * sizeof(u_int));
7820 break;
7821
7822 case IPFTABLE_BUCKETS_NATOUT :
7823 error = COPYOUT(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
7824 table.ita_table,
7825 softn->ipf_nat_table_sz * sizeof(u_int));
7826 break;
7827
7828 default :
7829 IPFERROR(60058);
7830 return EINVAL;
7831 }
7832
7833 if (error != 0) {
7834 IPFERROR(60059);
7835 error = EFAULT;
7836 }
7837 return error;
7838 }
7839
7840
7841 /* ------------------------------------------------------------------------ */
7842 /* Function: ipf_nat_settimeout */
7843 /* Returns: int - 0 = success, else failure */
7844 /* Parameters: softc(I) - pointer to soft context main structure */
7845 /* t(I) - pointer to tunable */
7846 /* p(I) - pointer to new tuning data */
7847 /* */
7848 /* Apply the timeout change to the NAT timeout queues. */
7849 /* ------------------------------------------------------------------------ */
7850 int
7851 ipf_nat_settimeout(struct ipf_main_softc_s *softc, ipftuneable_t *t,
7852 ipftuneval_t *p)
7853 {
7854 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7855
7856 if (!strncmp(t->ipft_name, "tcp_", 4))
7857 return ipf_settimeout_tcp(t, p, softn->ipf_nat_tcptq);
7858
7859 if (!strcmp(t->ipft_name, "udp_timeout")) {
7860 ipf_apply_timeout(&softn->ipf_nat_udptq, p->ipftu_int);
7861 } else if (!strcmp(t->ipft_name, "udp_ack_timeout")) {
7862 ipf_apply_timeout(&softn->ipf_nat_udpacktq, p->ipftu_int);
7863 } else if (!strcmp(t->ipft_name, "icmp_timeout")) {
7864 ipf_apply_timeout(&softn->ipf_nat_icmptq, p->ipftu_int);
7865 } else if (!strcmp(t->ipft_name, "icmp_ack_timeout")) {
7866 ipf_apply_timeout(&softn->ipf_nat_icmpacktq, p->ipftu_int);
7867 } else if (!strcmp(t->ipft_name, "ip_timeout")) {
7868 ipf_apply_timeout(&softn->ipf_nat_iptq, p->ipftu_int);
7869 } else {
7870 IPFERROR(60062);
7871 return ESRCH;
7872 }
7873 return 0;
7874 }
7875
7876
7877 /* ------------------------------------------------------------------------ */
7878 /* Function: ipf_nat_rehash */
7879 /* Returns: int - 0 = success, else failure */
7880 /* Parameters: softc(I) - pointer to soft context main structure */
7881 /* t(I) - pointer to tunable */
7882 /* p(I) - pointer to new tuning data */
7883 /* */
7884 /* To change the size of the basic NAT table, we need to first allocate the */
7885 /* new tables (lest it fails and we've got nowhere to store all of the NAT */
7886 /* sessions currently active) and then walk through the entire list and */
7887 /* insert them into the table. There are two tables here: an inbound one */
7888 /* and an outbound one. Each NAT entry goes into each table once. */
7889 /* ------------------------------------------------------------------------ */
7890 int
7891 ipf_nat_rehash(ipf_main_softc_t *softc, ipftuneable_t *t, ipftuneval_t *p)
7892 {
7893 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
7894 nat_t **newtab[2], *nat, **natp;
7895 u_int *bucketlens[2];
7896 u_int maxbucket;
7897 u_int newsize;
7898 int error;
7899 u_int hv;
7900 int i;
7901
7902 newsize = p->ipftu_int;
7903 /*
7904 * In case there is nothing to do...
7905 */
7906 if (newsize == softn->ipf_nat_table_sz)
7907 return 0;
7908
7909 newtab[0] = NULL;
7910 newtab[1] = NULL;
7911 bucketlens[0] = NULL;
7912 bucketlens[1] = NULL;
7913 /*
7914 * 4 tables depend on the NAT table size: the inbound looking table,
7915 * the outbound lookup table and the hash chain length for each.
7916 */
7917 KMALLOCS(newtab[0], nat_t **, newsize * sizeof(nat_t *));
7918 if (newtab[0] == NULL) {
7919 error = 60063;
7920 goto badrehash;
7921 }
7922
7923 KMALLOCS(newtab[1], nat_t **, newsize * sizeof(nat_t *));
7924 if (newtab[1] == NULL) {
7925 error = 60064;
7926 goto badrehash;
7927 }
7928
7929 KMALLOCS(bucketlens[0], u_int *, newsize * sizeof(u_int));
7930 if (bucketlens[0] == NULL) {
7931 error = 60065;
7932 goto badrehash;
7933 }
7934
7935 KMALLOCS(bucketlens[1], u_int *, newsize * sizeof(u_int));
7936 if (bucketlens[1] == NULL) {
7937 error = 60066;
7938 goto badrehash;
7939 }
7940
7941 /*
7942 * Recalculate the maximum length based on the new size.
7943 */
7944 for (maxbucket = 0, i = newsize; i > 0; i >>= 1)
7945 maxbucket++;
7946 maxbucket *= 2;
7947
7948 bzero((char *)newtab[0], newsize * sizeof(nat_t *));
7949 bzero((char *)newtab[1], newsize * sizeof(nat_t *));
7950 bzero((char *)bucketlens[0], newsize * sizeof(u_int));
7951 bzero((char *)bucketlens[1], newsize * sizeof(u_int));
7952
7953 WRITE_ENTER(&softc->ipf_nat);
7954
7955 if (softn->ipf_nat_table[0] != NULL) {
7956 KFREES(softn->ipf_nat_table[0],
7957 softn->ipf_nat_table_sz *
7958 sizeof(*softn->ipf_nat_table[0]));
7959 }
7960 softn->ipf_nat_table[0] = newtab[0];
7961
7962 if (softn->ipf_nat_table[1] != NULL) {
7963 KFREES(softn->ipf_nat_table[1],
7964 softn->ipf_nat_table_sz *
7965 sizeof(*softn->ipf_nat_table[1]));
7966 }
7967 softn->ipf_nat_table[1] = newtab[1];
7968
7969 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) {
7970 KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen,
7971 softn->ipf_nat_table_sz * sizeof(u_int));
7972 }
7973 softn->ipf_nat_stats.ns_side[0].ns_bucketlen = bucketlens[0];
7974
7975 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) {
7976 KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen,
7977 softn->ipf_nat_table_sz * sizeof(u_int));
7978 }
7979 softn->ipf_nat_stats.ns_side[1].ns_bucketlen = bucketlens[1];
7980
7981 softn->ipf_nat_maxbucket = maxbucket;
7982 softn->ipf_nat_table_sz = newsize;
7983 /*
7984 * Walk through the entire list of NAT table entries and put them
7985 * in the new NAT table, somewhere. Because we have a new table,
7986 * we need to restart the counter of how many chains are in use.
7987 */
7988 softn->ipf_nat_stats.ns_side[0].ns_inuse = 0;
7989 softn->ipf_nat_stats.ns_side[1].ns_inuse = 0;
7990
7991 for (nat = softn->ipf_nat_instances; nat != NULL; nat = nat->nat_next) {
7992 nat->nat_hnext[0] = NULL;
7993 nat->nat_phnext[0] = NULL;
7994 hv = nat->nat_hv[0] % softn->ipf_nat_table_sz;
7995
7996 natp = &softn->ipf_nat_table[0][hv];
7997 if (*natp) {
7998 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
7999 } else {
8000 NBUMPSIDE(0, ns_inuse);
8001 }
8002 nat->nat_phnext[0] = natp;
8003 nat->nat_hnext[0] = *natp;
8004 *natp = nat;
8005 NBUMPSIDE(0, ns_bucketlen[hv]);
8006
8007 nat->nat_hnext[1] = NULL;
8008 nat->nat_phnext[1] = NULL;
8009 hv = nat->nat_hv[1] % softn->ipf_nat_table_sz;
8010
8011 natp = &softn->ipf_nat_table[1][hv];
8012 if (*natp) {
8013 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
8014 } else {
8015 NBUMPSIDE(1, ns_inuse);
8016 }
8017 nat->nat_phnext[1] = natp;
8018 nat->nat_hnext[1] = *natp;
8019 *natp = nat;
8020 NBUMPSIDE(1, ns_bucketlen[hv]);
8021 }
8022 RWLOCK_EXIT(&softc->ipf_nat);
8023
8024 return 0;
8025
8026 badrehash:
8027 if (bucketlens[1] != NULL) {
8028 KFREES(bucketlens[0], newsize * sizeof(u_int));
8029 }
8030 if (bucketlens[0] != NULL) {
8031 KFREES(bucketlens[0], newsize * sizeof(u_int));
8032 }
8033 if (newtab[0] != NULL) {
8034 KFREES(newtab[0], newsize * sizeof(nat_t *));
8035 }
8036 if (newtab[1] != NULL) {
8037 KFREES(newtab[1], newsize * sizeof(nat_t *));
8038 }
8039 IPFERROR(error);
8040 return ENOMEM;
8041 }
8042
8043
8044 /* ------------------------------------------------------------------------ */
8045 /* Function: ipf_nat_rehash_rules */
8046 /* Returns: int - 0 = success, else failure */
8047 /* Parameters: softc(I) - pointer to soft context main structure */
8048 /* t(I) - pointer to tunable */
8049 /* p(I) - pointer to new tuning data */
8050 /* */
8051 /* All of the NAT rules hang off of a hash table that is searched with a */
8052 /* hash on address after the netmask is applied. There is a different table*/
8053 /* for both inbound rules (rdr) and outbound (map.) The resizing will only */
8054 /* affect one of these two tables. */
8055 /* ------------------------------------------------------------------------ */
8056 int
8057 ipf_nat_rehash_rules(ipf_main_softc_t *softc, ipftuneable_t *t, ipftuneval_t *p)
8058 {
8059 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8060 ipnat_t **newtab, *np, ***old, **npp;
8061 u_int newsize;
8062 u_int mask;
8063 u_int hv;
8064
8065 newsize = p->ipftu_int;
8066 /*
8067 * In case there is nothing to do...
8068 */
8069 if (newsize == *t->ipft_pint)
8070 return 0;
8071
8072 /*
8073 * All inbound rules have the NAT_REDIRECT bit set in in_redir and
8074 * all outbound rules have either NAT_MAP or MAT_MAPBLK set.
8075 * This if statement allows for some more generic code to be below,
8076 * rather than two huge gobs of code that almost do the same thing.
8077 */
8078 if (t->ipft_pint == &softn->ipf_nat_rdrrules_sz) {
8079 old = &softn->ipf_nat_rdr_rules;
8080 mask = NAT_REDIRECT;
8081 } else {
8082 old = &softn->ipf_nat_map_rules;
8083 mask = NAT_MAP|NAT_MAPBLK;
8084 }
8085
8086 KMALLOCS(newtab, ipnat_t **, newsize * sizeof(ipnat_t *));
8087 if (newtab == NULL) {
8088 IPFERROR(60067);
8089 return ENOMEM;
8090 }
8091
8092 bzero((char *)newtab, newsize * sizeof(ipnat_t *));
8093
8094 WRITE_ENTER(&softc->ipf_nat);
8095
8096 if (*old != NULL) {
8097 KFREES(*old, *t->ipft_pint * sizeof(ipnat_t **));
8098 }
8099 *old = newtab;
8100 *t->ipft_pint = newsize;
8101
8102 for (np = softn->ipf_nat_list; np != NULL; np = np->in_next) {
8103 if ((np->in_redir & mask) == 0)
8104 continue;
8105
8106 if (np->in_redir & NAT_REDIRECT) {
8107 np->in_rnext = NULL;
8108 hv = np->in_hv[0] % newsize;
8109 for (npp = newtab + hv; *npp != NULL; )
8110 npp = &(*npp)->in_rnext;
8111 np->in_prnext = npp;
8112 *npp = np;
8113 }
8114 if (np->in_redir & NAT_MAP) {
8115 np->in_mnext = NULL;
8116 hv = np->in_hv[1] % newsize;
8117 for (npp = newtab + hv; *npp != NULL; )
8118 npp = &(*npp)->in_mnext;
8119 np->in_pmnext = npp;
8120 *npp = np;
8121 }
8122
8123 }
8124 RWLOCK_EXIT(&softc->ipf_nat);
8125
8126 return 0;
8127 }
8128
8129
8130 /* ------------------------------------------------------------------------ */
8131 /* Function: ipf_nat_hostmap_rehash */
8132 /* Returns: int - 0 = success, else failure */
8133 /* Parameters: softc(I) - pointer to soft context main structure */
8134 /* t(I) - pointer to tunable */
8135 /* p(I) - pointer to new tuning data */
8136 /* */
8137 /* Allocate and populate a new hash table that will contain a reference to */
8138 /* all of the active IP# translations currently in place. */
8139 /* ------------------------------------------------------------------------ */
8140 int
8141 ipf_nat_hostmap_rehash(ipf_main_softc_t *softc, ipftuneable_t *t,
8142 ipftuneval_t *p)
8143 {
8144 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8145 hostmap_t *hm, **newtab;
8146 u_int newsize;
8147 u_int hv;
8148
8149 newsize = p->ipftu_int;
8150 /*
8151 * In case there is nothing to do...
8152 */
8153 if (newsize == *t->ipft_pint)
8154 return 0;
8155
8156 KMALLOCS(newtab, hostmap_t **, newsize * sizeof(hostmap_t *));
8157 if (newtab == NULL) {
8158 IPFERROR(60068);
8159 return ENOMEM;
8160 }
8161
8162 bzero((char *)newtab, newsize * sizeof(hostmap_t *));
8163
8164 WRITE_ENTER(&softc->ipf_nat);
8165 if (softn->ipf_hm_maptable != NULL) {
8166 KFREES(softn->ipf_hm_maptable,
8167 softn->ipf_nat_hostmap_sz * sizeof(hostmap_t *));
8168 }
8169 softn->ipf_hm_maptable = newtab;
8170 softn->ipf_nat_hostmap_sz = newsize;
8171
8172 for (hm = softn->ipf_hm_maplist; hm != NULL; hm = hm->hm_next) {
8173 hv = hm->hm_hv % softn->ipf_nat_hostmap_sz;
8174 hm->hm_hnext = softn->ipf_hm_maptable[hv];
8175 hm->hm_phnext = softn->ipf_hm_maptable + hv;
8176 if (softn->ipf_hm_maptable[hv] != NULL)
8177 softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
8178 softn->ipf_hm_maptable[hv] = hm;
8179 }
8180 RWLOCK_EXIT(&softc->ipf_nat);
8181
8182 return 0;
8183 }
8184
8185
8186 /* ------------------------------------------------------------------------ */
8187 /* Function: ipf_nat_add_tq */
8188 /* Parameters: softc(I) - pointer to soft context main structure */
8189 /* */
8190 /* ------------------------------------------------------------------------ */
8191 ipftq_t *
8192 ipf_nat_add_tq(ipf_main_softc_t *softc, int ttl)
8193 {
8194 ipf_nat_softc_t *softs = softc->ipf_nat_soft;
8195
8196 return ipf_addtimeoutqueue(softc, &softs->ipf_nat_utqe, ttl);
8197 }
8198
8199 /* ------------------------------------------------------------------------ */
8200 /* Function: ipf_nat_uncreate */
8201 /* Returns: Nil */
8202 /* Parameters: fin(I) - pointer to packet information */
8203 /* */
8204 /* This function is used to remove a NAT entry from the NAT table when we */
8205 /* decide that the create was actually in error. It is thus assumed that */
8206 /* fin_flx will have both FI_NATED and FI_NATNEW set. Because we're dealing */
8207 /* with the translated packet (not the original), we have to reverse the */
8208 /* lookup. Although doing the lookup is expensive (relatively speaking), it */
8209 /* is not anticipated that this will be a frequent occurance for normal */
8210 /* traffic patterns. */
8211 /* ------------------------------------------------------------------------ */
8212 void
8213 ipf_nat_uncreate(fr_info_t *fin)
8214 {
8215 ipf_main_softc_t *softc = fin->fin_main_soft;
8216 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
8217 int nflags;
8218 nat_t *nat;
8219
8220 switch (fin->fin_p)
8221 {
8222 case IPPROTO_TCP :
8223 nflags = IPN_TCP;
8224 break;
8225 case IPPROTO_UDP :
8226 nflags = IPN_UDP;
8227 break;
8228 default :
8229 nflags = 0;
8230 break;
8231 }
8232
8233 WRITE_ENTER(&softc->ipf_nat);
8234
8235 if (fin->fin_out == 0) {
8236 nat = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p,
8237 fin->fin_dst, fin->fin_src);
8238 } else {
8239 nat = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p,
8240 fin->fin_src, fin->fin_dst);
8241 }
8242
8243 if (nat != NULL) {
8244 NBUMPSIDE(fin->fin_out, ns_uncreate[0]);
8245 ipf_nat_delete(softc, nat, NL_DESTROY);
8246 } else {
8247 NBUMPSIDE(fin->fin_out, ns_uncreate[1]);
8248 }
8249
8250 RWLOCK_EXIT(&softc->ipf_nat);
8251 }
8252
8253
8254 /* ------------------------------------------------------------------------ */
8255 /* Function: ipf_nat_cmp_rules */
8256 /* Returns: int - 0 == success, else rules do not match. */
8257 /* Parameters: n1(I) - first rule to compare */
8258 /* n2(I) - first rule to compare */
8259 /* */
8260 /* Compare two rules using pointers to each rule. A straight bcmp will not */
8261 /* work as some fields (such as in_dst, in_pkts) actually do change once */
8262 /* the rule has been loaded into the kernel. Whilst this function returns */
8263 /* various non-zero returns, they're strictly to aid in debugging. Use of */
8264 /* this function should simply care if the result is zero or not. */
8265 /* ------------------------------------------------------------------------ */
8266 static int
8267 ipf_nat_cmp_rules(ipnat_t *n1, ipnat_t *n2)
8268 {
8269 if (n1->in_size != n2->in_size)
8270 return 1;
8271
8272 if (bcmp((char *)&n1->in_v, (char *)&n2->in_v,
8273 offsetof(ipnat_t, in_ndst) - offsetof(ipnat_t, in_v)) != 0)
8274 return 2;
8275
8276 if (bcmp((char *)&n1->in_tuc, (char *)&n2->in_tuc,
8277 n1->in_size - offsetof(ipnat_t, in_tuc)) != 0)
8278 return 3;
8279 if (n1->in_ndst.na_atype != n2->in_ndst.na_atype)
8280 return 5;
8281 if (n1->in_ndst.na_function != n2->in_ndst.na_function)
8282 return 6;
8283 if (bcmp((char *)&n1->in_ndst.na_addr, (char *)&n2->in_ndst.na_addr,
8284 sizeof(n1->in_ndst.na_addr)))
8285 return 7;
8286 if (n1->in_nsrc.na_atype != n2->in_nsrc.na_atype)
8287 return 8;
8288 if (n1->in_nsrc.na_function != n2->in_nsrc.na_function)
8289 return 9;
8290 if (bcmp((char *)&n1->in_nsrc.na_addr, (char *)&n2->in_nsrc.na_addr,
8291 sizeof(n1->in_nsrc.na_addr)))
8292 return 10;
8293 if (n1->in_odst.na_atype != n2->in_odst.na_atype)
8294 return 11;
8295 if (n1->in_odst.na_function != n2->in_odst.na_function)
8296 return 12;
8297 if (bcmp((char *)&n1->in_odst.na_addr, (char *)&n2->in_odst.na_addr,
8298 sizeof(n1->in_odst.na_addr)))
8299 return 13;
8300 if (n1->in_osrc.na_atype != n2->in_osrc.na_atype)
8301 return 14;
8302 if (n1->in_osrc.na_function != n2->in_osrc.na_function)
8303 return 15;
8304 if (bcmp((char *)&n1->in_osrc.na_addr, (char *)&n2->in_osrc.na_addr,
8305 sizeof(n1->in_osrc.na_addr)))
8306 return 16;
8307 return 0;
8308 }
8309
8310
8311 /* ------------------------------------------------------------------------ */
8312 /* Function: ipf_nat_rule_init */
8313 /* Returns: int - 0 == success, else rules do not match. */
8314 /* Parameters: softc(I) - pointer to soft context main structure */
8315 /* softn(I) - pointer to NAT context structure */
8316 /* n(I) - first rule to compare */
8317 /* */
8318 /* ------------------------------------------------------------------------ */
8319 static int
8320 ipf_nat_rule_init(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n)
8321 {
8322 int error = 0;
8323
8324 if ((n->in_flags & IPN_SIPRANGE) != 0)
8325 n->in_nsrcatype = FRI_RANGE;
8326
8327 if ((n->in_flags & IPN_DIPRANGE) != 0)
8328 n->in_ndstatype = FRI_RANGE;
8329
8330 if ((n->in_flags & IPN_SPLIT) != 0)
8331 n->in_ndstatype = FRI_SPLIT;
8332
8333 if ((n->in_redir & (NAT_MAP|NAT_REWRITE|NAT_DIVERTUDP)) != 0)
8334 n->in_spnext = n->in_spmin;
8335
8336 if ((n->in_redir & (NAT_REWRITE|NAT_DIVERTUDP)) != 0) {
8337 n->in_dpnext = n->in_dpmin;
8338 } else if (n->in_redir == NAT_REDIRECT) {
8339 n->in_dpnext = n->in_dpmin;
8340 }
8341
8342 n->in_stepnext = 0;
8343
8344 switch (n->in_v[0])
8345 {
8346 case 4 :
8347 error = ipf_nat_ruleaddrinit(softc, softn, n);
8348 if (error != 0)
8349 return error;
8350 break;
8351 #ifdef USE_INET6
8352 case 6 :
8353 error = ipf_nat6_ruleaddrinit(softc, softn, n);
8354 if (error != 0)
8355 return error;
8356 break;
8357 #endif
8358 default :
8359 break;
8360 }
8361
8362 if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) {
8363 /*
8364 * Prerecord whether or not the destination of the divert
8365 * is local or not to the interface the packet is going
8366 * to be sent out.
8367 */
8368 n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1],
8369 n->in_ifps[1], &n->in_ndstip6);
8370 }
8371
8372 return error;
8373 }
8374
8375
8376 /* ------------------------------------------------------------------------ */
8377 /* Function: ipf_nat_rule_fini */
8378 /* Returns: int - 0 == success, else rules do not match. */
8379 /* Parameters: softc(I) - pointer to soft context main structure */
8380 /* n(I) - rule to work on */
8381 /* */
8382 /* This function is used to release any objects that were referenced during */
8383 /* the rule initialisation. This is useful both when free'ing the rule and */
8384 /* when handling ioctls that need to initialise these fields but not */
8385 /* actually use them after the ioctl processing has finished. */
8386 /* ------------------------------------------------------------------------ */
8387 static void
8388 ipf_nat_rule_fini(ipf_main_softc_t *softc, ipnat_t *n)
8389 {
8390 if (n->in_odst.na_atype == FRI_LOOKUP && n->in_odst.na_ptr != NULL)
8391 ipf_lookup_deref(softc, n->in_odst.na_type, n->in_odst.na_ptr);
8392
8393 if (n->in_osrc.na_atype == FRI_LOOKUP && n->in_osrc.na_ptr != NULL)
8394 ipf_lookup_deref(softc, n->in_osrc.na_type, n->in_osrc.na_ptr);
8395
8396 if (n->in_ndst.na_atype == FRI_LOOKUP && n->in_ndst.na_ptr != NULL)
8397 ipf_lookup_deref(softc, n->in_ndst.na_type, n->in_ndst.na_ptr);
8398
8399 if (n->in_nsrc.na_atype == FRI_LOOKUP && n->in_nsrc.na_ptr != NULL)
8400 ipf_lookup_deref(softc, n->in_nsrc.na_type, n->in_nsrc.na_ptr);
8401
8402 if (n->in_divmp != NULL)
8403 FREE_MB_T(n->in_divmp);
8404 }
8405