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