1 /*
2 * Copyright (C) 2012 by Darren Reed.
3 *
4 * See the IPFILTER.LICENCE file for details on licencing.
5 */
6 #if defined(KERNEL) || defined(_KERNEL)
7 # undef KERNEL
8 # undef _KERNEL
9 # define KERNEL 1
10 # define _KERNEL 1
11 #endif
12 #include <sys/errno.h>
13 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <sys/time.h>
16 #include <sys/file.h>
17 #if defined(_KERNEL) && defined(__NetBSD_Version__) && \
18 (__NetBSD_Version__ >= 399002000)
19 # include <sys/kauth.h>
20 #endif
21 #if !defined(_KERNEL)
22 # include <stdio.h>
23 # include <string.h>
24 # include <stdlib.h>
25 # define _KERNEL
26 # ifdef ipf_nat6__OpenBSD__
27 struct file;
28 # endif
29 # include <sys/uio.h>
30 # undef _KERNEL
31 #endif
32 #if defined(_KERNEL) && defined(__FreeBSD__)
33 # include <sys/filio.h>
34 # include <sys/fcntl.h>
35 #else
36 # include <sys/ioctl.h>
37 #endif
38 # include <sys/fcntl.h>
39 # include <sys/protosw.h>
40 #include <sys/socket.h>
41 #if defined(_KERNEL)
42 # include <sys/systm.h>
43 # if !defined(__SVR4)
44 # include <sys/mbuf.h>
45 # endif
46 #endif
47 #if defined(__SVR4)
48 # include <sys/filio.h>
49 # include <sys/byteorder.h>
50 # ifdef _KERNEL
51 # include <sys/dditypes.h>
52 # endif
53 # include <sys/stream.h>
54 # include <sys/kmem.h>
55 #endif
56 #if defined(__FreeBSD__)
57 # include <sys/queue.h>
58 #endif
59 #include <net/if.h>
60 #if defined(__FreeBSD__)
61 # include <net/if_var.h>
62 #endif
63 #ifdef sun
64 # include <net/af.h>
65 #endif
66 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70
71 #ifdef RFC1825
72 # include <vpn/md5.h>
73 # include <vpn/ipsec.h>
74 extern struct ifnet vpnif;
75 #endif
76
77 # include <netinet/ip_var.h>
78 #include <netinet/tcp.h>
79 #include <netinet/udp.h>
80 #include <netinet/ip_icmp.h>
81 #include "netinet/ip_compat.h"
82 #include <netinet/tcpip.h>
83 #include "netinet/ip_fil.h"
84 #include "netinet/ip_nat.h"
85 #include "netinet/ip_frag.h"
86 #include "netinet/ip_state.h"
87 #include "netinet/ip_proxy.h"
88 #include "netinet/ip_lookup.h"
89 #include "netinet/ip_dstlist.h"
90 #include "netinet/ip_sync.h"
91 #if defined(__FreeBSD__)
92 # include <sys/malloc.h>
93 #endif
94 #ifdef HAS_SYS_MD5_H
95 # include <sys/md5.h>
96 #else
97 # include "md5.h"
98 #endif
99 /* END OF INCLUDES */
100
101 #undef SOCKADDR_IN
102 #define SOCKADDR_IN struct sockaddr_in
103
104
105 #ifdef USE_INET6
106 static struct hostmap *ipf_nat6_hostmap(ipf_nat_softc_t *, ipnat_t *,
107 i6addr_t *, i6addr_t *,
108 i6addr_t *, u_32_t);
109 static int ipf_nat6_match(fr_info_t *, ipnat_t *);
110 static void ipf_nat6_tabmove(ipf_nat_softc_t *, nat_t *);
111 static int ipf_nat6_decap(fr_info_t *, nat_t *);
112 static int ipf_nat6_nextaddr(fr_info_t *, nat_addr_t *, i6addr_t *,
113 i6addr_t *);
114 static int ipf_nat6_icmpquerytype(int);
115 static int ipf_nat6_out(fr_info_t *, nat_t *, int, u_32_t);
116 static int ipf_nat6_in(fr_info_t *, nat_t *, int, u_32_t);
117 static int ipf_nat6_builddivertmp(ipf_nat_softc_t *, ipnat_t *);
118 static int ipf_nat6_nextaddrinit(ipf_main_softc_t *, char *,
119 nat_addr_t *, int, void *);
120 static int ipf_nat6_insert(ipf_main_softc_t *, ipf_nat_softc_t *,
121 nat_t *);
122
123
124 #define NINCLSIDE6(y,x) ATOMIC_INCL(softn->ipf_nat_stats.ns_side6[y].x)
125 #define NBUMPSIDE(y,x) softn->ipf_nat_stats.ns_side[y].x++
126 #define NBUMPSIDE6(y,x) softn->ipf_nat_stats.ns_side6[y].x++
127 #define NBUMPSIDE6D(y,x) \
128 do { \
129 softn->ipf_nat_stats.ns_side6[y].x++; \
130 DT(x); \
131 } while (0)
132 #define NBUMPSIDE6DX(y,x,z) \
133 do { \
134 softn->ipf_nat_stats.ns_side6[y].x++; \
135 DT(z); \
136 } while (0)
137
138
139 /* ------------------------------------------------------------------------ */
140 /* Function: ipf_nat6_ruleaddrinit */
141 /* Returns: int - 0 == success, else failure */
142 /* Parameters: in(I) - NAT rule that requires address fields to be init'd */
143 /* */
144 /* For each of the source/destination address fields in a NAT rule, call */
145 /* ipf_nat6_nextaddrinit() to prepare the structure for active duty. Other */
146 /* IPv6 specific actions can also be taken care of here. */
147 /* ------------------------------------------------------------------------ */
148 int
ipf_nat6_ruleaddrinit(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,ipnat_t * n)149 ipf_nat6_ruleaddrinit(ipf_main_softc_t *softc, ipf_nat_softc_t *softn,
150 ipnat_t *n)
151 {
152 int idx, error;
153
154 if (n->in_redir == NAT_BIMAP) {
155 n->in_ndstip6 = n->in_osrcip6;
156 n->in_ndstmsk6 = n->in_osrcmsk6;
157 n->in_odstip6 = n->in_nsrcip6;
158 n->in_odstmsk6 = n->in_nsrcmsk6;
159
160 }
161
162 if (n->in_redir & NAT_REDIRECT)
163 idx = 1;
164 else
165 idx = 0;
166 /*
167 * Initialise all of the address fields.
168 */
169 error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_osrc, 1,
170 n->in_ifps[idx]);
171 if (error != 0)
172 return (error);
173
174 error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_odst, 1,
175 n->in_ifps[idx]);
176 if (error != 0)
177 return (error);
178
179 error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_nsrc, 1,
180 n->in_ifps[idx]);
181 if (error != 0)
182 return (error);
183
184 error = ipf_nat6_nextaddrinit(softc, n->in_names, &n->in_ndst, 1,
185 n->in_ifps[idx]);
186 if (error != 0)
187 return (error);
188
189 if (n->in_redir & NAT_DIVERTUDP)
190 ipf_nat6_builddivertmp(softn, n);
191 return (0);
192 }
193
194
195 /* ------------------------------------------------------------------------ */
196 /* Function: ipf_nat6_addrdr */
197 /* Returns: Nil */
198 /* Parameters: n(I) - pointer to NAT rule to add */
199 /* */
200 /* Adds a redirect rule to the hash table of redirect rules and the list of */
201 /* loaded NAT rules. Updates the bitmask indicating which netmasks are in */
202 /* use by redirect rules. */
203 /* ------------------------------------------------------------------------ */
204 void
ipf_nat6_addrdr(ipf_nat_softc_t * softn,ipnat_t * n)205 ipf_nat6_addrdr(ipf_nat_softc_t *softn, ipnat_t *n)
206 {
207 i6addr_t *mask;
208 ipnat_t **np;
209 i6addr_t j;
210 u_int hv;
211 int k;
212
213 if ((n->in_redir & NAT_BIMAP) == NAT_BIMAP) {
214 k = count6bits(n->in_nsrcmsk6.i6);
215 mask = &n->in_nsrcmsk6;
216 IP6_AND(&n->in_odstip6, &n->in_odstmsk6, &j);
217 hv = NAT_HASH_FN6(&j, 0, softn->ipf_nat_rdrrules_sz);
218
219 } else if (n->in_odstatype == FRI_NORMAL) {
220 k = count6bits(n->in_odstmsk6.i6);
221 mask = &n->in_odstmsk6;
222 IP6_AND(&n->in_odstip6, &n->in_odstmsk6, &j);
223 hv = NAT_HASH_FN6(&j, 0, softn->ipf_nat_rdrrules_sz);
224 } else {
225 k = 0;
226 hv = 0;
227 mask = NULL;
228 }
229 ipf_inet6_mask_add(k, mask, &softn->ipf_nat6_rdr_mask);
230
231 np = softn->ipf_nat_rdr_rules + hv;
232 while (*np != NULL)
233 np = &(*np)->in_rnext;
234 n->in_rnext = NULL;
235 n->in_prnext = np;
236 n->in_hv[0] = hv;
237 n->in_use++;
238 *np = n;
239 }
240
241
242 /* ------------------------------------------------------------------------ */
243 /* Function: ipf_nat6_addmap */
244 /* Returns: Nil */
245 /* Parameters: n(I) - pointer to NAT rule to add */
246 /* */
247 /* Adds a NAT map rule to the hash table of rules and the list of loaded */
248 /* NAT rules. Updates the bitmask indicating which netmasks are in use by */
249 /* redirect rules. */
250 /* ------------------------------------------------------------------------ */
251 void
ipf_nat6_addmap(ipf_nat_softc_t * softn,ipnat_t * n)252 ipf_nat6_addmap(ipf_nat_softc_t *softn, ipnat_t *n)
253 {
254 i6addr_t *mask;
255 ipnat_t **np;
256 i6addr_t j;
257 u_int hv;
258 int k;
259
260 if (n->in_osrcatype == FRI_NORMAL) {
261 k = count6bits(n->in_osrcmsk6.i6);
262 mask = &n->in_osrcmsk6;
263 IP6_AND(&n->in_osrcip6, &n->in_osrcmsk6, &j);
264 hv = NAT_HASH_FN6(&j, 0, softn->ipf_nat_maprules_sz);
265 } else {
266 k = 0;
267 hv = 0;
268 mask = NULL;
269 }
270 ipf_inet6_mask_add(k, mask, &softn->ipf_nat6_map_mask);
271
272 np = softn->ipf_nat_map_rules + hv;
273 while (*np != NULL)
274 np = &(*np)->in_mnext;
275 n->in_mnext = NULL;
276 n->in_pmnext = np;
277 n->in_hv[1] = hv;
278 n->in_use++;
279 *np = n;
280 }
281
282
283 /* ------------------------------------------------------------------------ */
284 /* Function: ipf_nat6_del_rdr */
285 /* Returns: Nil */
286 /* Parameters: n(I) - pointer to NAT rule to delete */
287 /* */
288 /* Removes a NAT rdr rule from the hash table of NAT rdr rules. */
289 /* ------------------------------------------------------------------------ */
290 void
ipf_nat6_delrdr(ipf_nat_softc_t * softn,ipnat_t * n)291 ipf_nat6_delrdr(ipf_nat_softc_t *softn, ipnat_t *n)
292 {
293 i6addr_t *mask;
294 int k;
295
296 if ((n->in_redir & NAT_BIMAP) == NAT_BIMAP) {
297 k = count6bits(n->in_nsrcmsk6.i6);
298 mask = &n->in_nsrcmsk6;
299 } else if (n->in_odstatype == FRI_NORMAL) {
300 k = count6bits(n->in_odstmsk6.i6);
301 mask = &n->in_odstmsk6;
302 } else {
303 k = 0;
304 mask = NULL;
305 }
306 ipf_inet6_mask_del(k, mask, &softn->ipf_nat6_rdr_mask);
307
308 if (n->in_rnext != NULL)
309 n->in_rnext->in_prnext = n->in_prnext;
310 *n->in_prnext = n->in_rnext;
311 n->in_use--;
312 }
313
314
315 /* ------------------------------------------------------------------------ */
316 /* Function: ipf_nat6_delmap */
317 /* Returns: Nil */
318 /* Parameters: n(I) - pointer to NAT rule to delete */
319 /* */
320 /* Removes a NAT map rule from the hash table of NAT map rules. */
321 /* ------------------------------------------------------------------------ */
322 void
ipf_nat6_delmap(ipf_nat_softc_t * softn,ipnat_t * n)323 ipf_nat6_delmap(ipf_nat_softc_t *softn, ipnat_t *n)
324 {
325 i6addr_t *mask;
326 int k;
327
328 if (n->in_osrcatype == FRI_NORMAL) {
329 k = count6bits(n->in_osrcmsk6.i6);
330 mask = &n->in_osrcmsk6;
331 } else {
332 k = 0;
333 mask = NULL;
334 }
335 ipf_inet6_mask_del(k, mask, &softn->ipf_nat6_map_mask);
336
337 if (n->in_mnext != NULL)
338 n->in_mnext->in_pmnext = n->in_pmnext;
339 *n->in_pmnext = n->in_mnext;
340 n->in_use--;
341 }
342
343
344 /* ------------------------------------------------------------------------ */
345 /* Function: ipf_nat6_hostmap */
346 /* Returns: struct hostmap* - NULL if no hostmap could be created, */
347 /* else a pointer to the hostmapping to use */
348 /* Parameters: np(I) - pointer to NAT rule */
349 /* real(I) - real IP address */
350 /* map(I) - mapped IP address */
351 /* port(I) - destination port number */
352 /* Write Locks: ipf_nat */
353 /* */
354 /* Check if an ip address has already been allocated for a given mapping */
355 /* that is not doing port based translation. If is not yet allocated, then */
356 /* create a new entry if a non-NULL NAT rule pointer has been supplied. */
357 /* ------------------------------------------------------------------------ */
358 static struct hostmap *
ipf_nat6_hostmap(ipf_nat_softc_t * softn,ipnat_t * np,i6addr_t * src,i6addr_t * dst,i6addr_t * map,u_32_t port)359 ipf_nat6_hostmap(ipf_nat_softc_t *softn, ipnat_t *np,
360 i6addr_t *src, i6addr_t *dst, i6addr_t *map, u_32_t port)
361 {
362 hostmap_t *hm;
363 u_int hv;
364
365 hv = (src->i6[3] ^ dst->i6[3]);
366 hv += (src->i6[2] ^ dst->i6[2]);
367 hv += (src->i6[1] ^ dst->i6[1]);
368 hv += (src->i6[0] ^ dst->i6[0]);
369 hv += src->i6[3];
370 hv += src->i6[2];
371 hv += src->i6[1];
372 hv += src->i6[0];
373 hv += dst->i6[3];
374 hv += dst->i6[2];
375 hv += dst->i6[1];
376 hv += dst->i6[0];
377 hv %= softn->ipf_nat_hostmap_sz;
378 for (hm = softn->ipf_hm_maptable[hv]; hm; hm = hm->hm_next)
379 if (IP6_EQ(&hm->hm_osrc6, src) &&
380 IP6_EQ(&hm->hm_odst6, dst) &&
381 ((np == NULL) || (np == hm->hm_ipnat)) &&
382 ((port == 0) || (port == hm->hm_port))) {
383 softn->ipf_nat_stats.ns_hm_addref++;
384 hm->hm_ref++;
385 return (hm);
386 }
387
388 if (np == NULL) {
389 softn->ipf_nat_stats.ns_hm_nullnp++;
390 return (NULL);
391 }
392
393 KMALLOC(hm, hostmap_t *);
394 if (hm) {
395 hm->hm_next = softn->ipf_hm_maplist;
396 hm->hm_pnext = &softn->ipf_hm_maplist;
397 if (softn->ipf_hm_maplist != NULL)
398 softn->ipf_hm_maplist->hm_pnext = &hm->hm_next;
399 softn->ipf_hm_maplist = hm;
400 hm->hm_hnext = softn->ipf_hm_maptable[hv];
401 hm->hm_phnext = softn->ipf_hm_maptable + hv;
402 if (softn->ipf_hm_maptable[hv] != NULL)
403 softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext;
404 softn->ipf_hm_maptable[hv] = hm;
405 hm->hm_ipnat = np;
406 np->in_use++;
407 hm->hm_osrcip6 = *src;
408 hm->hm_odstip6 = *dst;
409 hm->hm_nsrcip6 = *map;
410 hm->hm_ndstip6.i6[0] = 0;
411 hm->hm_ndstip6.i6[1] = 0;
412 hm->hm_ndstip6.i6[2] = 0;
413 hm->hm_ndstip6.i6[3] = 0;
414 hm->hm_ref = 1;
415 hm->hm_port = port;
416 hm->hm_hv = hv;
417 hm->hm_v = 6;
418 softn->ipf_nat_stats.ns_hm_new++;
419 } else {
420 softn->ipf_nat_stats.ns_hm_newfail++;
421 }
422 return (hm);
423 }
424
425
426 /* ------------------------------------------------------------------------ */
427 /* Function: ipf_nat6_newmap */
428 /* Returns: int - -1 == error, 0 == success */
429 /* Parameters: fin(I) - pointer to packet information */
430 /* nat(I) - pointer to NAT entry */
431 /* ni(I) - pointer to structure with misc. information needed */
432 /* to create new NAT entry. */
433 /* */
434 /* Given an empty NAT structure, populate it with new information about a */
435 /* new NAT session, as defined by the matching NAT rule. */
436 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
437 /* to the new IP address for the translation. */
438 /* ------------------------------------------------------------------------ */
439 int
ipf_nat6_newmap(fr_info_t * fin,nat_t * nat,natinfo_t * ni)440 ipf_nat6_newmap(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
441 {
442 ipf_main_softc_t *softc = fin->fin_main_soft;
443 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
444 u_short st_port, dport, sport, port, sp, dp;
445 i6addr_t in, st_ip;
446 hostmap_t *hm;
447 u_32_t flags;
448 ipnat_t *np;
449 nat_t *natl;
450 int l;
451
452 /*
453 * If it's an outbound packet which doesn't match any existing
454 * record, then create a new port
455 */
456 l = 0;
457 hm = NULL;
458 np = ni->nai_np;
459 st_ip = np->in_snip6;
460 st_port = np->in_spnext;
461 flags = nat->nat_flags;
462
463 if (flags & IPN_ICMPQUERY) {
464 sport = fin->fin_data[1];
465 dport = 0;
466 } else {
467 sport = htons(fin->fin_data[0]);
468 dport = htons(fin->fin_data[1]);
469 }
470
471 /*
472 * Do a loop until we either run out of entries to try or we find
473 * a NAT mapping that isn't currently being used. This is done
474 * because the change to the source is not (usually) being fixed.
475 */
476 do {
477 port = 0;
478 in = np->in_nsrc.na_nextaddr;
479 if (l == 0) {
480 /*
481 * Check to see if there is an existing NAT
482 * setup for this IP address pair.
483 */
484 hm = ipf_nat6_hostmap(softn, np, &fin->fin_src6,
485 &fin->fin_dst6, &in, 0);
486 if (hm != NULL)
487 in = hm->hm_nsrcip6;
488 } else if ((l == 1) && (hm != NULL)) {
489 ipf_nat_hostmapdel(softc, &hm);
490 }
491
492 nat->nat_hm = hm;
493
494 if (IP6_ISONES(&np->in_nsrcmsk6) && (np->in_spnext == 0)) {
495 if (l > 0) {
496 NBUMPSIDE6DX(1, ns_exhausted, ns_exhausted_1);
497 return (-1);
498 }
499 }
500
501 if ((np->in_redir == NAT_BIMAP) &&
502 IP6_EQ(&np->in_osrcmsk6, &np->in_nsrcmsk6)) {
503 i6addr_t temp;
504 /*
505 * map the address block in a 1:1 fashion
506 */
507 temp.i6[0] = fin->fin_src6.i6[0] &
508 ~np->in_osrcmsk6.i6[0];
509 temp.i6[1] = fin->fin_src6.i6[1] &
510 ~np->in_osrcmsk6.i6[1];
511 temp.i6[2] = fin->fin_src6.i6[2] &
512 ~np->in_osrcmsk6.i6[0];
513 temp.i6[3] = fin->fin_src6.i6[3] &
514 ~np->in_osrcmsk6.i6[3];
515 in = np->in_nsrcip6;
516 IP6_MERGE(&in, &temp, &np->in_osrc);
517
518 #ifdef NEED_128BIT_MATH
519 } else if (np->in_redir & NAT_MAPBLK) {
520 if ((l >= np->in_ppip) || ((l > 0) &&
521 !(flags & IPN_TCPUDP))) {
522 NBUMPSIDE6DX(1, ns_exhausted, ns_exhausted_2);
523 return (-1);
524 }
525 /*
526 * map-block - Calculate destination address.
527 */
528 IP6_MASK(&in, &fin->fin_src6, &np->in_osrcmsk6);
529 in = ntohl(in);
530 inb = in;
531 in.s_addr /= np->in_ippip;
532 in.s_addr &= ntohl(~np->in_nsrcmsk6);
533 in.s_addr += ntohl(np->in_nsrcaddr6);
534 /*
535 * Calculate destination port.
536 */
537 if ((flags & IPN_TCPUDP) &&
538 (np->in_ppip != 0)) {
539 port = ntohs(sport) + l;
540 port %= np->in_ppip;
541 port += np->in_ppip *
542 (inb.s_addr % np->in_ippip);
543 port += MAPBLK_MINPORT;
544 port = htons(port);
545 }
546 #endif
547
548 } else if (IP6_ISZERO(&np->in_nsrcaddr) &&
549 IP6_ISONES(&np->in_nsrcmsk)) {
550 /*
551 * 0/32 - use the interface's IP address.
552 */
553 if ((l > 0) ||
554 ipf_ifpaddr(softc, 6, FRI_NORMAL, fin->fin_ifp,
555 &in, NULL) == -1) {
556 NBUMPSIDE6DX(1, ns_new_ifpaddr,
557 ns_new_ifpaddr_1);
558 return (-1);
559 }
560
561 } else if (IP6_ISZERO(&np->in_nsrcip6) &&
562 IP6_ISZERO(&np->in_nsrcmsk6)) {
563 /*
564 * 0/0 - use the original source address/port.
565 */
566 if (l > 0) {
567 NBUMPSIDE6DX(1, ns_exhausted, ns_exhausted_3);
568 return (-1);
569 }
570 in = fin->fin_src6;
571
572 } else if (!IP6_ISONES(&np->in_nsrcmsk6) &&
573 (np->in_spnext == 0) && ((l > 0) || (hm == NULL))) {
574 IP6_INC(&np->in_snip6);
575 }
576
577 natl = NULL;
578
579 if ((flags & IPN_TCPUDP) &&
580 ((np->in_redir & NAT_MAPBLK) == 0) &&
581 (np->in_flags & IPN_AUTOPORTMAP)) {
582 #ifdef NEED_128BIT_MATH
583 /*
584 * "ports auto" (without map-block)
585 */
586 if ((l > 0) && (l % np->in_ppip == 0)) {
587 if ((l > np->in_ppip) &&
588 !IP6_ISONES(&np->in_nsrcmsk)) {
589 IP6_INC(&np->in_snip6)
590 }
591 }
592 if (np->in_ppip != 0) {
593 port = ntohs(sport);
594 port += (l % np->in_ppip);
595 port %= np->in_ppip;
596 port += np->in_ppip *
597 (ntohl(fin->fin_src6) %
598 np->in_ippip);
599 port += MAPBLK_MINPORT;
600 port = htons(port);
601 }
602 #endif
603
604 } else if (((np->in_redir & NAT_MAPBLK) == 0) &&
605 (flags & IPN_TCPUDPICMP) && (np->in_spnext != 0)) {
606 /*
607 * Standard port translation. Select next port.
608 */
609 if (np->in_flags & IPN_SEQUENTIAL) {
610 port = np->in_spnext;
611 } else {
612 port = ipf_random() % (np->in_spmax -
613 np->in_spmin + 1);
614 port += np->in_spmin;
615 }
616 port = htons(port);
617 np->in_spnext++;
618
619 if (np->in_spnext > np->in_spmax) {
620 np->in_spnext = np->in_spmin;
621 if (!IP6_ISONES(&np->in_nsrcmsk6)) {
622 IP6_INC(&np->in_snip6);
623 }
624 }
625 }
626
627 if (np->in_flags & IPN_SIPRANGE) {
628 if (IP6_GT(&np->in_snip, &np->in_nsrcmsk))
629 np->in_snip6 = np->in_nsrcip6;
630 } else {
631 i6addr_t a1, a2;
632
633 a1 = np->in_snip6;
634 IP6_INC(&a1);
635 IP6_AND(&a1, &np->in_nsrcmsk6, &a2);
636
637 if (!IP6_ISONES(&np->in_nsrcmsk6) &&
638 IP6_GT(&a2, &np->in_nsrcip6)) {
639 IP6_ADD(&np->in_nsrcip6, 1, &np->in_snip6);
640 }
641 }
642
643 if ((port == 0) && (flags & (IPN_TCPUDPICMP|IPN_ICMPQUERY)))
644 port = sport;
645
646 /*
647 * Here we do a lookup of the connection as seen from
648 * the outside. If an IP# pair already exists, try
649 * again. So if you have A->B becomes C->B, you can
650 * also have D->E become C->E but not D->B causing
651 * another C->B. Also take protocol and ports into
652 * account when determining whether a pre-existing
653 * NAT setup will cause an external conflict where
654 * this is appropriate.
655 */
656 sp = fin->fin_data[0];
657 dp = fin->fin_data[1];
658 fin->fin_data[0] = fin->fin_data[1];
659 fin->fin_data[1] = ntohs(port);
660 natl = ipf_nat6_inlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
661 (u_int)fin->fin_p, &fin->fin_dst6.in6,
662 &in.in6);
663 fin->fin_data[0] = sp;
664 fin->fin_data[1] = dp;
665
666 /*
667 * Has the search wrapped around and come back to the
668 * start ?
669 */
670 if ((natl != NULL) &&
671 (np->in_spnext != 0) && (st_port == np->in_spnext) &&
672 (!IP6_ISZERO(&np->in_snip6) &&
673 IP6_EQ(&st_ip, &np->in_snip6))) {
674 NBUMPSIDE6D(1, ns_wrap);
675 return (-1);
676 }
677 l++;
678 } while (natl != NULL);
679
680 /* Setup the NAT table */
681 nat->nat_osrc6 = fin->fin_src6;
682 nat->nat_nsrc6 = in;
683 nat->nat_odst6 = fin->fin_dst6;
684 nat->nat_ndst6 = fin->fin_dst6;
685 if (nat->nat_hm == NULL)
686 nat->nat_hm = ipf_nat6_hostmap(softn, np, &fin->fin_src6,
687 &fin->fin_dst6,
688 &nat->nat_nsrc6, 0);
689
690 if (flags & IPN_TCPUDP) {
691 nat->nat_osport = sport;
692 nat->nat_nsport = port; /* sport */
693 nat->nat_odport = dport;
694 nat->nat_ndport = dport;
695 ((tcphdr_t *)fin->fin_dp)->th_sport = port;
696 } else if (flags & IPN_ICMPQUERY) {
697 nat->nat_oicmpid = fin->fin_data[1];
698 ((struct icmp6_hdr *)fin->fin_dp)->icmp6_id = port;
699 nat->nat_nicmpid = port;
700 }
701 return (0);
702 }
703
704
705 /* ------------------------------------------------------------------------ */
706 /* Function: ipf_nat6_newrdr */
707 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */
708 /* allow rule to be moved if IPN_ROUNDR is set. */
709 /* Parameters: fin(I) - pointer to packet information */
710 /* nat(I) - pointer to NAT entry */
711 /* ni(I) - pointer to structure with misc. information needed */
712 /* to create new NAT entry. */
713 /* */
714 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/
715 /* to the new IP address for the translation. */
716 /* ------------------------------------------------------------------------ */
717 int
ipf_nat6_newrdr(fr_info_t * fin,nat_t * nat,natinfo_t * ni)718 ipf_nat6_newrdr(fr_info_t *fin, nat_t *nat, natinfo_t *ni)
719 {
720 ipf_main_softc_t *softc = fin->fin_main_soft;
721 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
722 u_short nport, dport, sport;
723 u_short sp, dp;
724 hostmap_t *hm;
725 u_32_t flags;
726 i6addr_t in;
727 ipnat_t *np;
728 nat_t *natl;
729 int move;
730
731 move = 1;
732 hm = NULL;
733 in.i6[0] = 0;
734 in.i6[1] = 0;
735 in.i6[2] = 0;
736 in.i6[3] = 0;
737 np = ni->nai_np;
738 flags = nat->nat_flags;
739
740 if (flags & IPN_ICMPQUERY) {
741 dport = fin->fin_data[1];
742 sport = 0;
743 } else {
744 sport = htons(fin->fin_data[0]);
745 dport = htons(fin->fin_data[1]);
746 }
747
748 /* TRACE sport, dport */
749
750
751 /*
752 * If the matching rule has IPN_STICKY set, then we want to have the
753 * same rule kick in as before. Why would this happen? If you have
754 * a collection of rdr rules with "round-robin sticky", the current
755 * packet might match a different one to the previous connection but
756 * we want the same destination to be used.
757 */
758 if (((np->in_flags & (IPN_ROUNDR|IPN_SPLIT)) != 0) &&
759 ((np->in_flags & IPN_STICKY) != 0)) {
760 hm = ipf_nat6_hostmap(softn, NULL, &fin->fin_src6,
761 &fin->fin_dst6, &in, (u_32_t)dport);
762 if (hm != NULL) {
763 in = hm->hm_ndstip6;
764 np = hm->hm_ipnat;
765 ni->nai_np = np;
766 move = 0;
767 }
768 }
769
770 /*
771 * Otherwise, it's an inbound packet. Most likely, we don't
772 * want to rewrite source ports and source addresses. Instead,
773 * we want to rewrite to a fixed internal address and fixed
774 * internal port.
775 */
776 if (np->in_flags & IPN_SPLIT) {
777 in = np->in_dnip6;
778
779 if ((np->in_flags & (IPN_ROUNDR|IPN_STICKY)) == IPN_STICKY) {
780 hm = ipf_nat6_hostmap(softn, NULL, &fin->fin_src6,
781 &fin->fin_dst6, &in,
782 (u_32_t)dport);
783 if (hm != NULL) {
784 in = hm->hm_ndstip6;
785 move = 0;
786 }
787 }
788
789 if (hm == NULL || hm->hm_ref == 1) {
790 if (IP6_EQ(&np->in_ndstip6, &in)) {
791 np->in_dnip6 = np->in_ndstmsk6;
792 move = 0;
793 } else {
794 np->in_dnip6 = np->in_ndstip6;
795 }
796 }
797
798 } else if (IP6_ISZERO(&np->in_ndstaddr) &&
799 IP6_ISONES(&np->in_ndstmsk)) {
800 /*
801 * 0/32 - use the interface's IP address.
802 */
803 if (ipf_ifpaddr(softc, 6, FRI_NORMAL, fin->fin_ifp,
804 &in, NULL) == -1) {
805 NBUMPSIDE6DX(0, ns_new_ifpaddr, ns_new_ifpaddr_2);
806 return (-1);
807 }
808
809 } else if (IP6_ISZERO(&np->in_ndstip6) &&
810 IP6_ISZERO(&np->in_ndstmsk6)) {
811 /*
812 * 0/0 - use the original destination address/port.
813 */
814 in = fin->fin_dst6;
815
816 } else if (np->in_redir == NAT_BIMAP &&
817 IP6_EQ(&np->in_ndstmsk6, &np->in_odstmsk6)) {
818 i6addr_t temp;
819 /*
820 * map the address block in a 1:1 fashion
821 */
822 temp.i6[0] = fin->fin_dst6.i6[0] & ~np->in_osrcmsk6.i6[0];
823 temp.i6[1] = fin->fin_dst6.i6[1] & ~np->in_osrcmsk6.i6[1];
824 temp.i6[2] = fin->fin_dst6.i6[2] & ~np->in_osrcmsk6.i6[0];
825 temp.i6[3] = fin->fin_dst6.i6[3] & ~np->in_osrcmsk6.i6[3];
826 in = np->in_ndstip6;
827 IP6_MERGE(&in, &temp, &np->in_ndstmsk6);
828 } else {
829 in = np->in_ndstip6;
830 }
831
832 if ((np->in_dpnext == 0) || ((flags & NAT_NOTRULEPORT) != 0))
833 nport = dport;
834 else {
835 /*
836 * Whilst not optimized for the case where
837 * pmin == pmax, the gain is not significant.
838 */
839 if (((np->in_flags & IPN_FIXEDDPORT) == 0) &&
840 (np->in_odport != np->in_dtop)) {
841 nport = ntohs(dport) - np->in_odport + np->in_dpmax;
842 nport = htons(nport);
843 } else {
844 nport = htons(np->in_dpnext);
845 np->in_dpnext++;
846 if (np->in_dpnext > np->in_dpmax)
847 np->in_dpnext = np->in_dpmin;
848 }
849 }
850
851 /*
852 * When the redirect-to address is set to 0.0.0.0, just
853 * assume a blank `forwarding' of the packet. We don't
854 * setup any translation for this either.
855 */
856 if (IP6_ISZERO(&in)) {
857 if (nport == dport) {
858 NBUMPSIDE6D(0, ns_xlate_null);
859 return (-1);
860 }
861 in = fin->fin_dst6;
862 }
863
864 /*
865 * Check to see if this redirect mapping already exists and if
866 * it does, return "failure" (allowing it to be created will just
867 * cause one or both of these "connections" to stop working.)
868 */
869 sp = fin->fin_data[0];
870 dp = fin->fin_data[1];
871 fin->fin_data[1] = fin->fin_data[0];
872 fin->fin_data[0] = ntohs(nport);
873 natl = ipf_nat6_outlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH),
874 (u_int)fin->fin_p, &in.in6,
875 &fin->fin_src6.in6);
876 fin->fin_data[0] = sp;
877 fin->fin_data[1] = dp;
878 if (natl != NULL) {
879 NBUMPSIDE6D(0, ns_xlate_exists);
880 return (-1);
881 }
882
883 nat->nat_ndst6 = in;
884 nat->nat_odst6 = fin->fin_dst6;
885 nat->nat_nsrc6 = fin->fin_src6;
886 nat->nat_osrc6 = fin->fin_src6;
887 if ((nat->nat_hm == NULL) && ((np->in_flags & IPN_STICKY) != 0))
888 nat->nat_hm = ipf_nat6_hostmap(softn, np, &fin->fin_src6,
889 &fin->fin_dst6, &in,
890 (u_32_t)dport);
891
892 if (flags & IPN_TCPUDP) {
893 nat->nat_odport = dport;
894 nat->nat_ndport = nport;
895 nat->nat_osport = sport;
896 nat->nat_nsport = sport;
897 ((tcphdr_t *)fin->fin_dp)->th_dport = nport;
898 } else if (flags & IPN_ICMPQUERY) {
899 nat->nat_oicmpid = fin->fin_data[1];
900 ((struct icmp6_hdr *)fin->fin_dp)->icmp6_id = nport;
901 nat->nat_nicmpid = nport;
902 }
903
904 return (move);
905 }
906
907 /* ------------------------------------------------------------------------ */
908 /* Function: ipf_nat6_add */
909 /* Returns: nat6_t* - NULL == failure to create new NAT structure, */
910 /* else pointer to new NAT structure */
911 /* Parameters: fin(I) - pointer to packet information */
912 /* np(I) - pointer to NAT rule */
913 /* natsave(I) - pointer to where to store NAT struct pointer */
914 /* flags(I) - flags describing the current packet */
915 /* direction(I) - direction of packet (in/out) */
916 /* Write Lock: ipf_nat */
917 /* */
918 /* Attempts to create a new NAT entry. Does not actually change the packet */
919 /* in any way. */
920 /* */
921 /* This fucntion is in three main parts: (1) deal with creating a new NAT */
922 /* structure for a "MAP" rule (outgoing NAT translation); (2) deal with */
923 /* creating a new NAT structure for a "RDR" rule (incoming NAT translation) */
924 /* and (3) building that structure and putting it into the NAT table(s). */
925 /* */
926 /* NOTE: natsave should NOT be used top point back to an ipstate_t struct */
927 /* as it can result in memory being corrupted. */
928 /* ------------------------------------------------------------------------ */
929 nat_t *
ipf_nat6_add(fr_info_t * fin,ipnat_t * np,nat_t ** natsave,u_int flags,int direction)930 ipf_nat6_add(fr_info_t *fin, ipnat_t *np, nat_t **natsave, u_int flags,
931 int direction)
932 {
933 ipf_main_softc_t *softc = fin->fin_main_soft;
934 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
935 hostmap_t *hm = NULL;
936 nat_t *nat, *natl;
937 natstat_t *nsp;
938 u_int nflags;
939 natinfo_t ni;
940 int move;
941 #if SOLARIS && defined(_KERNEL) && defined(ICK_M_CTL_MAGIC)
942 qpktinfo_t *qpi = fin->fin_qpi;
943 #endif
944
945 nsp = &softn->ipf_nat_stats;
946
947 if ((nsp->ns_active * 100 / softn->ipf_nat_table_max) >
948 softn->ipf_nat_table_wm_high) {
949 softn->ipf_nat_doflush = 1;
950 }
951
952 if (nsp->ns_active >= softn->ipf_nat_table_max) {
953 NBUMPSIDE6(fin->fin_out, ns_table_max);
954 return (NULL);
955 }
956
957 move = 1;
958 nflags = np->in_flags & flags;
959 nflags &= NAT_FROMRULE;
960
961 ni.nai_np = np;
962 ni.nai_dport = 0;
963 ni.nai_sport = 0;
964
965 /* Give me a new nat */
966 KMALLOC(nat, nat_t *);
967 if (nat == NULL) {
968 NBUMPSIDE6(fin->fin_out, ns_memfail);
969 /*
970 * Try to automatically tune the max # of entries in the
971 * table allowed to be less than what will cause kmem_alloc()
972 * to fail and try to eliminate panics due to out of memory
973 * conditions arising.
974 */
975 if ((softn->ipf_nat_table_max > softn->ipf_nat_table_sz) &&
976 (nsp->ns_active > 100)) {
977 softn->ipf_nat_table_max = nsp->ns_active - 100;
978 printf("table_max reduced to %d\n",
979 softn->ipf_nat_table_max);
980 }
981 return (NULL);
982 }
983
984 if (flags & IPN_ICMPQUERY) {
985 /*
986 * In the ICMP query NAT code, we translate the ICMP id fields
987 * to make them unique. This is indepedent of the ICMP type
988 * (e.g. in the unlikely event that a host sends an echo and
989 * an tstamp request with the same id, both packets will have
990 * their ip address/id field changed in the same way).
991 */
992 /* The icmp6_id field is used by the sender to identify the
993 * process making the icmp request. (the receiver justs
994 * copies it back in its response). So, it closely matches
995 * the concept of source port. We overlay sport, so we can
996 * maximally reuse the existing code.
997 */
998 ni.nai_sport = fin->fin_data[1];
999 ni.nai_dport = 0;
1000 }
1001
1002 bzero((char *)nat, sizeof(*nat));
1003 nat->nat_flags = flags;
1004 nat->nat_redir = np->in_redir;
1005 nat->nat_dir = direction;
1006 nat->nat_pr[0] = fin->fin_p;
1007 nat->nat_pr[1] = fin->fin_p;
1008
1009 /*
1010 * Search the current table for a match and create a new mapping
1011 * if there is none found.
1012 */
1013 if (np->in_redir & NAT_DIVERTUDP) {
1014 move = ipf_nat6_newdivert(fin, nat, &ni);
1015
1016 } else if (np->in_redir & NAT_REWRITE) {
1017 move = ipf_nat6_newrewrite(fin, nat, &ni);
1018
1019 } else if (direction == NAT_OUTBOUND) {
1020 /*
1021 * We can now arrange to call this for the same connection
1022 * because ipf_nat6_new doesn't protect the code path into
1023 * this function.
1024 */
1025 natl = ipf_nat6_outlookup(fin, nflags, (u_int)fin->fin_p,
1026 &fin->fin_src6.in6,
1027 &fin->fin_dst6.in6);
1028 if (natl != NULL) {
1029 KFREE(nat);
1030 nat = natl;
1031 goto done;
1032 }
1033
1034 move = ipf_nat6_newmap(fin, nat, &ni);
1035 } else {
1036 /*
1037 * NAT_INBOUND is used for redirects rules
1038 */
1039 natl = ipf_nat6_inlookup(fin, nflags, (u_int)fin->fin_p,
1040 &fin->fin_src6.in6,
1041 &fin->fin_dst6.in6);
1042 if (natl != NULL) {
1043 KFREE(nat);
1044 nat = natl;
1045 goto done;
1046 }
1047
1048 move = ipf_nat6_newrdr(fin, nat, &ni);
1049 }
1050 if (move == -1)
1051 goto badnat;
1052
1053 np = ni.nai_np;
1054
1055 nat->nat_mssclamp = np->in_mssclamp;
1056 nat->nat_me = natsave;
1057 nat->nat_fr = fin->fin_fr;
1058 nat->nat_rev = fin->fin_rev;
1059 nat->nat_ptr = np;
1060 nat->nat_dlocal = np->in_dlocal;
1061
1062 if ((np->in_apr != NULL) && ((nat->nat_flags & NAT_SLAVE) == 0)) {
1063 if (ipf_proxy_new(fin, nat) == -1) {
1064 NBUMPSIDE6D(fin->fin_out, ns_appr_fail);
1065 goto badnat;
1066 }
1067 }
1068
1069 nat->nat_ifps[0] = np->in_ifps[0];
1070 if (np->in_ifps[0] != NULL) {
1071 COPYIFNAME(np->in_v[0], np->in_ifps[0], nat->nat_ifnames[0]);
1072 }
1073
1074 nat->nat_ifps[1] = np->in_ifps[1];
1075 if (np->in_ifps[1] != NULL) {
1076 COPYIFNAME(np->in_v[1], np->in_ifps[1], nat->nat_ifnames[1]);
1077 }
1078
1079 if (ipf_nat6_finalise(fin, nat) == -1) {
1080 goto badnat;
1081 }
1082
1083 np->in_use++;
1084
1085 if ((move == 1) && (np->in_flags & IPN_ROUNDR)) {
1086 if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_REDIRECT) {
1087 ipf_nat6_delrdr(softn, np);
1088 ipf_nat6_addrdr(softn, np);
1089 } else if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_MAP) {
1090 ipf_nat6_delmap(softn, np);
1091 ipf_nat6_addmap(softn, np);
1092 }
1093 }
1094
1095 if (flags & SI_WILDP)
1096 nsp->ns_wilds++;
1097 softn->ipf_nat_stats.ns_proto[nat->nat_pr[0]]++;
1098
1099 goto done;
1100 badnat:
1101 NBUMPSIDE6(fin->fin_out, ns_badnatnew);
1102 if ((hm = nat->nat_hm) != NULL)
1103 ipf_nat_hostmapdel(softc, &hm);
1104 KFREE(nat);
1105 nat = NULL;
1106 done:
1107 if (nat != NULL && np != NULL)
1108 np->in_hits++;
1109 if (natsave != NULL)
1110 *natsave = nat;
1111 return (nat);
1112 }
1113
1114
1115 /* ------------------------------------------------------------------------ */
1116 /* Function: ipf_nat6_finalise */
1117 /* Returns: int - 0 == sucess, -1 == failure */
1118 /* Parameters: fin(I) - pointer to packet information */
1119 /* nat(I) - pointer to NAT entry */
1120 /* Write Lock: ipf_nat */
1121 /* */
1122 /* This is the tail end of constructing a new NAT entry and is the same */
1123 /* for both IPv4 and IPv6. */
1124 /* ------------------------------------------------------------------------ */
1125 /*ARGSUSED*/
1126 int
ipf_nat6_finalise(fr_info_t * fin,nat_t * nat)1127 ipf_nat6_finalise(fr_info_t *fin, nat_t *nat)
1128 {
1129 ipf_main_softc_t *softc = fin->fin_main_soft;
1130 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1131 u_32_t sum1, sum2, sumd;
1132 frentry_t *fr;
1133
1134 switch (fin->fin_p)
1135 {
1136 case IPPROTO_ICMPV6 :
1137 sum1 = LONG_SUM6(&nat->nat_osrc6);
1138 sum1 += ntohs(nat->nat_oicmpid);
1139 sum2 = LONG_SUM6(&nat->nat_nsrc6);
1140 sum2 += ntohs(nat->nat_nicmpid);
1141 CALC_SUMD(sum1, sum2, sumd);
1142 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1143
1144 sum1 = LONG_SUM6(&nat->nat_odst6);
1145 sum2 = LONG_SUM6(&nat->nat_ndst6);
1146 CALC_SUMD(sum1, sum2, sumd);
1147 nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
1148 break;
1149
1150 case IPPROTO_TCP :
1151 case IPPROTO_UDP :
1152 sum1 = LONG_SUM6(&nat->nat_osrc6);
1153 sum1 += ntohs(nat->nat_osport);
1154 sum2 = LONG_SUM6(&nat->nat_nsrc6);
1155 sum2 += ntohs(nat->nat_nsport);
1156 CALC_SUMD(sum1, sum2, sumd);
1157 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1158
1159 sum1 = LONG_SUM6(&nat->nat_odst6);
1160 sum1 += ntohs(nat->nat_odport);
1161 sum2 = LONG_SUM6(&nat->nat_ndst6);
1162 sum2 += ntohs(nat->nat_ndport);
1163 CALC_SUMD(sum1, sum2, sumd);
1164 nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
1165 break;
1166
1167 default :
1168 sum1 = LONG_SUM6(&nat->nat_osrc6);
1169 sum2 = LONG_SUM6(&nat->nat_nsrc6);
1170 CALC_SUMD(sum1, sum2, sumd);
1171 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
1172
1173 sum1 = LONG_SUM6(&nat->nat_odst6);
1174 sum2 = LONG_SUM6(&nat->nat_ndst6);
1175 CALC_SUMD(sum1, sum2, sumd);
1176 nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16);
1177 break;
1178 }
1179
1180 /*
1181 * Compute the partial checksum, just in case.
1182 * This is only ever placed into outbound packets so care needs
1183 * to be taken over which pair of addresses are used.
1184 */
1185 if (nat->nat_dir == NAT_OUTBOUND) {
1186 sum1 = LONG_SUM6(&nat->nat_nsrc6);
1187 sum1 += LONG_SUM6(&nat->nat_ndst6);
1188 } else {
1189 sum1 = LONG_SUM6(&nat->nat_osrc6);
1190 sum1 += LONG_SUM6(&nat->nat_odst6);
1191 }
1192 sum1 += nat->nat_pr[1];
1193 nat->nat_sumd[1] = (sum1 & 0xffff) + (sum1 >> 16);
1194
1195 if ((nat->nat_flags & SI_CLONE) == 0)
1196 nat->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, nat);
1197
1198 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
1199 nat->nat_mtu[0] = GETIFMTU_6(nat->nat_ifps[0]);
1200 }
1201
1202 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
1203 nat->nat_mtu[1] = GETIFMTU_6(nat->nat_ifps[1]);
1204 }
1205
1206 nat->nat_v[0] = 6;
1207 nat->nat_v[1] = 6;
1208
1209 if (ipf_nat6_insert(softc, softn, nat) == 0) {
1210 if (softn->ipf_nat_logging)
1211 ipf_nat_log(softc, softn, nat, NL_NEW);
1212 fr = nat->nat_fr;
1213 if (fr != NULL) {
1214 MUTEX_ENTER(&fr->fr_lock);
1215 fr->fr_ref++;
1216 MUTEX_EXIT(&fr->fr_lock);
1217 }
1218 return (0);
1219 }
1220
1221 NBUMPSIDE6D(fin->fin_out, ns_unfinalised);
1222 /*
1223 * nat6_insert failed, so cleanup time...
1224 */
1225 if (nat->nat_sync != NULL)
1226 ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync);
1227 return (-1);
1228 }
1229
1230
1231 /* ------------------------------------------------------------------------ */
1232 /* Function: ipf_nat6_insert */
1233 /* Returns: int - 0 == sucess, -1 == failure */
1234 /* Parameters: softc(I) - pointer to soft context main structure */
1235 /* softn(I) - pointer to NAT context structure */
1236 /* nat(I) - pointer to NAT structure */
1237 /* Write Lock: ipf_nat */
1238 /* */
1239 /* Insert a NAT entry into the hash tables for searching and add it to the */
1240 /* list of active NAT entries. Adjust global counters when complete. */
1241 /* ------------------------------------------------------------------------ */
1242 static int
ipf_nat6_insert(ipf_main_softc_t * softc,ipf_nat_softc_t * softn,nat_t * nat)1243 ipf_nat6_insert(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat)
1244 {
1245 u_int hv1, hv2;
1246 u_32_t sp, dp;
1247 ipnat_t *in;
1248
1249 /*
1250 * Try and return an error as early as possible, so calculate the hash
1251 * entry numbers first and then proceed.
1252 */
1253 if ((nat->nat_flags & (SI_W_SPORT|SI_W_DPORT)) == 0) {
1254 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
1255 sp = nat->nat_osport;
1256 dp = nat->nat_odport;
1257 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
1258 sp = 0;
1259 dp = nat->nat_oicmpid;
1260 } else {
1261 sp = 0;
1262 dp = 0;
1263 }
1264 hv1 = NAT_HASH_FN6(&nat->nat_osrc6, sp, 0xffffffff);
1265 hv1 = NAT_HASH_FN6(&nat->nat_odst6, hv1 + dp,
1266 softn->ipf_nat_table_sz);
1267
1268 /*
1269 * TRACE nat6_osrc6, nat6_osport, nat6_odst6,
1270 * nat6_odport, hv1
1271 */
1272
1273 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
1274 sp = nat->nat_nsport;
1275 dp = nat->nat_ndport;
1276 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) {
1277 sp = 0;
1278 dp = nat->nat_nicmpid;
1279 } else {
1280 sp = 0;
1281 dp = 0;
1282 }
1283 hv2 = NAT_HASH_FN6(&nat->nat_nsrc6, sp, 0xffffffff);
1284 hv2 = NAT_HASH_FN6(&nat->nat_ndst6, hv2 + dp,
1285 softn->ipf_nat_table_sz);
1286 /*
1287 * TRACE nat6_nsrcaddr, nat6_nsport, nat6_ndstaddr,
1288 * nat6_ndport, hv1
1289 */
1290 } else {
1291 hv1 = NAT_HASH_FN6(&nat->nat_osrc6, 0, 0xffffffff);
1292 hv1 = NAT_HASH_FN6(&nat->nat_odst6, hv1,
1293 softn->ipf_nat_table_sz);
1294 /* TRACE nat6_osrcip6, nat6_odstip6, hv1 */
1295
1296 hv2 = NAT_HASH_FN6(&nat->nat_nsrc6, 0, 0xffffffff);
1297 hv2 = NAT_HASH_FN6(&nat->nat_ndst6, hv2,
1298 softn->ipf_nat_table_sz);
1299 /* TRACE nat6_nsrcip6, nat6_ndstip6, hv2 */
1300 }
1301
1302 nat->nat_hv[0] = hv1;
1303 nat->nat_hv[1] = hv2;
1304
1305 MUTEX_INIT(&nat->nat_lock, "nat entry lock");
1306
1307 in = nat->nat_ptr;
1308 nat->nat_ref = nat->nat_me ? 2 : 1;
1309
1310 nat->nat_ifnames[0][LIFNAMSIZ - 1] = '\0';
1311 nat->nat_ifps[0] = ipf_resolvenic(softc, nat->nat_ifnames[0],
1312 nat->nat_v[0]);
1313
1314 if (nat->nat_ifnames[1][0] != '\0') {
1315 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
1316 nat->nat_ifps[1] = ipf_resolvenic(softc, nat->nat_ifnames[1],
1317 nat->nat_v[1]);
1318 } else if (in->in_ifnames[1] != -1) {
1319 char *name;
1320
1321 name = in->in_names + in->in_ifnames[1];
1322 if (name[1] != '\0' && name[0] != '-' && name[0] != '*') {
1323 (void) strncpy(nat->nat_ifnames[1],
1324 nat->nat_ifnames[0], LIFNAMSIZ);
1325 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0';
1326 nat->nat_ifps[1] = nat->nat_ifps[0];
1327 }
1328 }
1329 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) {
1330 nat->nat_mtu[0] = GETIFMTU_6(nat->nat_ifps[0]);
1331 }
1332 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) {
1333 nat->nat_mtu[1] = GETIFMTU_6(nat->nat_ifps[1]);
1334 }
1335
1336 return (ipf_nat_hashtab_add(softc, softn, nat));
1337 }
1338
1339
1340 /* ------------------------------------------------------------------------ */
1341 /* Function: ipf_nat6_icmperrorlookup */
1342 /* Returns: nat6_t* - point to matching NAT structure */
1343 /* Parameters: fin(I) - pointer to packet information */
1344 /* dir(I) - direction of packet (in/out) */
1345 /* */
1346 /* Check if the ICMP error message is related to an existing TCP, UDP or */
1347 /* ICMP query nat entry. It is assumed that the packet is already of the */
1348 /* the required length. */
1349 /* ------------------------------------------------------------------------ */
1350 nat_t *
ipf_nat6_icmperrorlookup(fr_info_t * fin,int dir)1351 ipf_nat6_icmperrorlookup(fr_info_t *fin, int dir)
1352 {
1353 ipf_main_softc_t *softc = fin->fin_main_soft;
1354 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1355 struct icmp6_hdr *orgicmp;
1356 int flags = 0, minlen;
1357 nat_stat_side_t *nside;
1358 tcphdr_t *tcp = NULL;
1359 u_short data[2];
1360 ip6_t *oip6;
1361 nat_t *nat;
1362 u_int p;
1363
1364 minlen = 40;
1365 nside = &softn->ipf_nat_stats.ns_side6[fin->fin_out];
1366 /*
1367 * Does it at least have the return (basic) IP header ?
1368 * Only a basic IP header (no options) should be with an ICMP error
1369 * header. Also, if it's not an error type, then return.
1370 */
1371 if (!(fin->fin_flx & FI_ICMPERR)) {
1372 ATOMIC_INCL(nside->ns_icmp_basic);
1373 return (NULL);
1374 }
1375
1376 /*
1377 * Check packet size
1378 */
1379 if (fin->fin_plen < ICMP6ERR_IPICMPHLEN) {
1380 ATOMIC_INCL(nside->ns_icmp_size);
1381 return (NULL);
1382 }
1383 oip6 = (ip6_t *)((char *)fin->fin_dp + 8);
1384
1385 /*
1386 * Is the buffer big enough for all of it ? It's the size of the IP
1387 * header claimed in the encapsulated part which is of concern. It
1388 * may be too big to be in this buffer but not so big that it's
1389 * outside the ICMP packet, leading to TCP deref's causing problems.
1390 * This is possible because we don't know how big oip_hl is when we
1391 * do the pullup early in ipf_check() and thus can't gaurantee it is
1392 * all here now.
1393 */
1394 #ifdef _KERNEL
1395 {
1396 mb_t *m;
1397
1398 m = fin->fin_m;
1399 # if SOLARIS
1400 if ((char *)oip6 + fin->fin_dlen - ICMPERR_ICMPHLEN >
1401 (char *)m->b_wptr) {
1402 ATOMIC_INCL(nside->ns_icmp_mbuf);
1403 return (NULL);
1404 }
1405 # else
1406 if ((char *)oip6 + fin->fin_dlen - ICMPERR_ICMPHLEN >
1407 (char *)fin->fin_ip + M_LEN(m)) {
1408 ATOMIC_INCL(nside->ns_icmp_mbuf);
1409 return (NULL);
1410 }
1411 # endif
1412 }
1413 #endif
1414
1415 if (IP6_NEQ(&fin->fin_dst6, &oip6->ip6_src)) {
1416 ATOMIC_INCL(nside->ns_icmp_address);
1417 return (NULL);
1418 }
1419
1420 p = oip6->ip6_nxt;
1421 if (p == IPPROTO_TCP)
1422 flags = IPN_TCP;
1423 else if (p == IPPROTO_UDP)
1424 flags = IPN_UDP;
1425 else if (p == IPPROTO_ICMPV6) {
1426 orgicmp = (struct icmp6_hdr *)(oip6 + 1);
1427
1428 /* see if this is related to an ICMP query */
1429 if (ipf_nat6_icmpquerytype(orgicmp->icmp6_type)) {
1430 data[0] = fin->fin_data[0];
1431 data[1] = fin->fin_data[1];
1432 fin->fin_data[0] = 0;
1433 fin->fin_data[1] = orgicmp->icmp6_id;
1434
1435 flags = IPN_ICMPERR|IPN_ICMPQUERY;
1436 /*
1437 * NOTE : dir refers to the direction of the original
1438 * ip packet. By definition the icmp error
1439 * message flows in the opposite direction.
1440 */
1441 if (dir == NAT_INBOUND)
1442 nat = ipf_nat6_inlookup(fin, flags, p,
1443 &oip6->ip6_dst,
1444 &oip6->ip6_src);
1445 else
1446 nat = ipf_nat6_outlookup(fin, flags, p,
1447 &oip6->ip6_dst,
1448 &oip6->ip6_src);
1449 fin->fin_data[0] = data[0];
1450 fin->fin_data[1] = data[1];
1451 return (nat);
1452 }
1453 }
1454
1455 if (flags & IPN_TCPUDP) {
1456 minlen += 8; /* + 64bits of data to get ports */
1457 /* TRACE (fin,minlen) */
1458 if (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen) {
1459 ATOMIC_INCL(nside->ns_icmp_short);
1460 return (NULL);
1461 }
1462
1463 data[0] = fin->fin_data[0];
1464 data[1] = fin->fin_data[1];
1465 tcp = (tcphdr_t *)(oip6 + 1);
1466 fin->fin_data[0] = ntohs(tcp->th_dport);
1467 fin->fin_data[1] = ntohs(tcp->th_sport);
1468
1469 if (dir == NAT_INBOUND) {
1470 nat = ipf_nat6_inlookup(fin, flags, p, &oip6->ip6_dst,
1471 &oip6->ip6_src);
1472 } else {
1473 nat = ipf_nat6_outlookup(fin, flags, p, &oip6->ip6_dst,
1474 &oip6->ip6_src);
1475 }
1476 fin->fin_data[0] = data[0];
1477 fin->fin_data[1] = data[1];
1478 return (nat);
1479 }
1480 if (dir == NAT_INBOUND)
1481 nat = ipf_nat6_inlookup(fin, 0, p, &oip6->ip6_dst,
1482 &oip6->ip6_src);
1483 else
1484 nat = ipf_nat6_outlookup(fin, 0, p, &oip6->ip6_dst,
1485 &oip6->ip6_src);
1486
1487 return (nat);
1488 }
1489
1490
1491 /* result = ip1 - ip2 */
1492 u_32_t
ipf_nat6_ip6subtract(i6addr_t * ip1,i6addr_t * ip2)1493 ipf_nat6_ip6subtract(i6addr_t *ip1, i6addr_t *ip2)
1494 {
1495 i6addr_t l1, l2, d;
1496 u_short *s1, *s2, *ds;
1497 u_32_t r;
1498 int i;
1499
1500 l1 = *ip1;
1501 l2 = *ip2;
1502 s1 = (u_short *)&l1;
1503 s2 = (u_short *)&l2;
1504 ds = (u_short *)&d;
1505
1506 for (i = 7; i > 0; i--) {
1507 if (s1[i] > s2[i]) {
1508 ds[i] = s2[i] + 0x10000 - s1[i];
1509 s2[i - 1] += 0x10000;
1510 } else {
1511 ds[i] = s2[i] - s1[i];
1512 }
1513 }
1514 if (s2[0] > s1[0]) {
1515 ds[0] = s2[0] + 0x10000 - s1[0];
1516 } else {
1517 ds[0] = s2[0] - s1[0];
1518 }
1519
1520 for (i = 0, r = 0; i < 8; i++) {
1521 r += ds[i];
1522 }
1523
1524 return (r);
1525 }
1526
1527
1528 /* ------------------------------------------------------------------------ */
1529 /* Function: ipf_nat6_icmperror */
1530 /* Returns: nat6_t* - point to matching NAT structure */
1531 /* Parameters: fin(I) - pointer to packet information */
1532 /* nflags(I) - NAT flags for this packet */
1533 /* dir(I) - direction of packet (in/out) */
1534 /* */
1535 /* Fix up an ICMP packet which is an error message for an existing NAT */
1536 /* session. This will correct both packet header data and checksums. */
1537 /* */
1538 /* This should *ONLY* be used for incoming ICMP error packets to make sure */
1539 /* a NAT'd ICMP packet gets correctly recognised. */
1540 /* ------------------------------------------------------------------------ */
1541 nat_t *
ipf_nat6_icmperror(fr_info_t * fin,u_int * nflags,int dir)1542 ipf_nat6_icmperror(fr_info_t *fin, u_int *nflags, int dir)
1543 {
1544 ipf_main_softc_t *softc = fin->fin_main_soft;
1545 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1546 u_32_t sum1, sum2, sumd, sumd2;
1547 i6addr_t a1, a2, a3, a4;
1548 struct icmp6_hdr *icmp6;
1549 int flags, dlen, odst;
1550 u_short *csump;
1551 tcphdr_t *tcp;
1552 ip6_t *oip6;
1553 nat_t *nat;
1554 void *dp;
1555
1556 if ((fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
1557 NBUMPSIDE6D(fin->fin_out, ns_icmp_short);
1558 return (NULL);
1559 }
1560
1561 /*
1562 * ipf_nat6_icmperrorlookup() will return NULL for `defective' packets.
1563 */
1564 if ((fin->fin_v != 6) || !(nat = ipf_nat6_icmperrorlookup(fin, dir))) {
1565 NBUMPSIDE6D(fin->fin_out, ns_icmp_notfound);
1566 return (NULL);
1567 }
1568
1569 tcp = NULL;
1570 csump = NULL;
1571 flags = 0;
1572 sumd2 = 0;
1573 *nflags = IPN_ICMPERR;
1574 icmp6 = fin->fin_dp;
1575 oip6 = (ip6_t *)((u_char *)icmp6 + sizeof(*icmp6));
1576 dp = (u_char *)oip6 + sizeof(*oip6);
1577 if (oip6->ip6_nxt == IPPROTO_TCP) {
1578 tcp = (tcphdr_t *)dp;
1579 csump = (u_short *)&tcp->th_sum;
1580 flags = IPN_TCP;
1581 } else if (oip6->ip6_nxt == IPPROTO_UDP) {
1582 udphdr_t *udp;
1583
1584 udp = (udphdr_t *)dp;
1585 tcp = (tcphdr_t *)dp;
1586 csump = (u_short *)&udp->uh_sum;
1587 flags = IPN_UDP;
1588 } else if (oip6->ip6_nxt == IPPROTO_ICMPV6)
1589 flags = IPN_ICMPQUERY;
1590 dlen = fin->fin_plen - ((char *)dp - (char *)fin->fin_ip);
1591
1592 /*
1593 * Need to adjust ICMP header to include the real IP#'s and
1594 * port #'s. Only apply a checksum change relative to the
1595 * IP address change as it will be modified again in ipf_nat6_checkout
1596 * for both address and port. Two checksum changes are
1597 * necessary for the two header address changes. Be careful
1598 * to only modify the checksum once for the port # and twice
1599 * for the IP#.
1600 */
1601
1602 /*
1603 * Step 1
1604 * Fix the IP addresses in the offending IP packet. You also need
1605 * to adjust the IP header checksum of that offending IP packet.
1606 *
1607 * Normally, you would expect that the ICMP checksum of the
1608 * ICMP error message needs to be adjusted as well for the
1609 * IP address change in oip.
1610 * However, this is a NOP, because the ICMP checksum is
1611 * calculated over the complete ICMP packet, which includes the
1612 * changed oip IP addresses and oip6->ip6_sum. However, these
1613 * two changes cancel each other out (if the delta for
1614 * the IP address is x, then the delta for ip_sum is minus x),
1615 * so no change in the icmp_cksum is necessary.
1616 *
1617 * Inbound ICMP
1618 * ------------
1619 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
1620 * - response to outgoing packet (a,b)=>(c,b) (OIP_SRC=c,OIP_DST=b)
1621 * - OIP_SRC(c)=nat6_newsrcip, OIP_DST(b)=nat6_newdstip
1622 *=> OIP_SRC(c)=nat6_oldsrcip, OIP_DST(b)=nat6_olddstip
1623 *
1624 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
1625 * - response to outgoing packet (c,a)=>(b,a) (OIP_SRC=b,OIP_DST=a)
1626 * - OIP_SRC(b)=nat6_olddstip, OIP_DST(a)=nat6_oldsrcip
1627 *=> OIP_SRC(b)=nat6_newdstip, OIP_DST(a)=nat6_newsrcip
1628 *
1629 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
1630 * - response to outgoing packet (a,b)=>(c,d) (OIP_SRC=c,OIP_DST=d)
1631 * - OIP_SRC(c)=nat6_newsrcip, OIP_DST(d)=nat6_newdstip
1632 *=> OIP_SRC(c)=nat6_oldsrcip, OIP_DST(d)=nat6_olddstip
1633 *
1634 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
1635 * - response to outgoing packet (d,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
1636 * - OIP_SRC(b)=nat6_olddstip, OIP_DST(a)=nat6_oldsrcip
1637 *=> OIP_SRC(b)=nat6_newdstip, OIP_DST(a)=nat6_newsrcip
1638 *
1639 * Outbound ICMP
1640 * -------------
1641 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b
1642 * - response to incoming packet (b,c)=>(b,a) (OIP_SRC=b,OIP_DST=a)
1643 * - OIP_SRC(b)=nat6_olddstip, OIP_DST(a)=nat6_oldsrcip
1644 *=> OIP_SRC(b)=nat6_newdstip, OIP_DST(a)=nat6_newsrcip
1645 *
1646 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c
1647 * - response to incoming packet (a,b)=>(a,c) (OIP_SRC=a,OIP_DST=c)
1648 * - OIP_SRC(a)=nat6_newsrcip, OIP_DST(c)=nat6_newdstip
1649 *=> OIP_SRC(a)=nat6_oldsrcip, OIP_DST(c)=nat6_olddstip
1650 *
1651 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d
1652 * - response to incoming packet (d,c)=>(b,a) (OIP_SRC=c,OIP_DST=d)
1653 * - OIP_SRC(c)=nat6_olddstip, OIP_DST(d)=nat6_oldsrcip
1654 *=> OIP_SRC(b)=nat6_newdstip, OIP_DST(a)=nat6_newsrcip
1655 *
1656 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d
1657 * - response to incoming packet (a,b)=>(c,d) (OIP_SRC=b,OIP_DST=a)
1658 * - OIP_SRC(b)=nat6_newsrcip, OIP_DST(a)=nat6_newdstip
1659 *=> OIP_SRC(a)=nat6_oldsrcip, OIP_DST(c)=nat6_olddstip
1660 */
1661
1662 if (((fin->fin_out == 0) && ((nat->nat_redir & NAT_MAP) != 0)) ||
1663 ((fin->fin_out == 1) && ((nat->nat_redir & NAT_REDIRECT) != 0))) {
1664 a1 = nat->nat_osrc6;
1665 a4.in6 = oip6->ip6_src;
1666 a3 = nat->nat_odst6;
1667 a2.in6 = oip6->ip6_dst;
1668 oip6->ip6_src = a1.in6;
1669 oip6->ip6_dst = a3.in6;
1670 odst = 1;
1671 } else {
1672 a1 = nat->nat_ndst6;
1673 a2.in6 = oip6->ip6_dst;
1674 a3 = nat->nat_nsrc6;
1675 a4.in6 = oip6->ip6_src;
1676 oip6->ip6_dst = a3.in6;
1677 oip6->ip6_src = a1.in6;
1678 odst = 0;
1679 }
1680
1681 sumd = 0;
1682 if (IP6_NEQ(&a3, &a2) || IP6_NEQ(&a1, &a4)) {
1683 if (IP6_GT(&a3, &a2)) {
1684 sumd = ipf_nat6_ip6subtract(&a2, &a3);
1685 sumd--;
1686 } else {
1687 sumd = ipf_nat6_ip6subtract(&a2, &a3);
1688 }
1689 if (IP6_GT(&a1, &a4)) {
1690 sumd += ipf_nat6_ip6subtract(&a4, &a1);
1691 sumd--;
1692 } else {
1693 sumd += ipf_nat6_ip6subtract(&a4, &a1);
1694 }
1695 sumd = ~sumd;
1696 }
1697
1698 sumd2 = sumd;
1699 sum1 = 0;
1700 sum2 = 0;
1701
1702 /*
1703 * Fix UDP pseudo header checksum to compensate for the
1704 * IP address change.
1705 */
1706 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) {
1707 u_32_t sum3, sum4;
1708 /*
1709 * Step 2 :
1710 * For offending TCP/UDP IP packets, translate the ports as
1711 * well, based on the NAT specification. Of course such
1712 * a change may be reflected in the ICMP checksum as well.
1713 *
1714 * Since the port fields are part of the TCP/UDP checksum
1715 * of the offending IP packet, you need to adjust that checksum
1716 * as well... except that the change in the port numbers should
1717 * be offset by the checksum change. However, the TCP/UDP
1718 * checksum will also need to change if there has been an
1719 * IP address change.
1720 */
1721 if (odst == 1) {
1722 sum1 = ntohs(nat->nat_osport);
1723 sum4 = ntohs(tcp->th_sport);
1724 sum3 = ntohs(nat->nat_odport);
1725 sum2 = ntohs(tcp->th_dport);
1726
1727 tcp->th_sport = htons(sum1);
1728 tcp->th_dport = htons(sum3);
1729 } else {
1730 sum1 = ntohs(nat->nat_ndport);
1731 sum2 = ntohs(tcp->th_dport);
1732 sum3 = ntohs(nat->nat_nsport);
1733 sum4 = ntohs(tcp->th_sport);
1734
1735 tcp->th_dport = htons(sum3);
1736 tcp->th_sport = htons(sum1);
1737 }
1738 sumd += sum1 - sum4;
1739 sumd += sum3 - sum2;
1740
1741 if (sumd != 0 || sumd2 != 0) {
1742 /*
1743 * At this point, sumd is the delta to apply to the
1744 * TCP/UDP header, given the changes in both the IP
1745 * address and the ports and sumd2 is the delta to
1746 * apply to the ICMP header, given the IP address
1747 * change delta that may need to be applied to the
1748 * TCP/UDP checksum instead.
1749 *
1750 * If we will both the IP and TCP/UDP checksums
1751 * then the ICMP checksum changes by the address
1752 * delta applied to the TCP/UDP checksum. If we
1753 * do not change the TCP/UDP checksum them we
1754 * apply the delta in ports to the ICMP checksum.
1755 */
1756 if (oip6->ip6_nxt == IPPROTO_UDP) {
1757 if ((dlen >= 8) && (*csump != 0)) {
1758 ipf_fix_datacksum(csump, sumd);
1759 } else {
1760 sumd2 = sum4 - sum1;
1761 if (sum1 > sum4)
1762 sumd2--;
1763 sumd2 += sum2 - sum3;
1764 if (sum3 > sum2)
1765 sumd2--;
1766 }
1767 } else if (oip6->ip6_nxt == IPPROTO_TCP) {
1768 if (dlen >= 18) {
1769 ipf_fix_datacksum(csump, sumd);
1770 } else {
1771 sumd2 = sum4 - sum1;
1772 if (sum1 > sum4)
1773 sumd2--;
1774 sumd2 += sum2 - sum3;
1775 if (sum3 > sum2)
1776 sumd2--;
1777 }
1778 }
1779 if (sumd2 != 0) {
1780 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1781 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1782 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16);
1783 ipf_fix_incksum(0, &icmp6->icmp6_cksum,
1784 sumd2, 0);
1785 }
1786 }
1787 } else if (((flags & IPN_ICMPQUERY) != 0) && (dlen >= 8)) {
1788 struct icmp6_hdr *orgicmp;
1789
1790 /*
1791 * XXX - what if this is bogus hl and we go off the end ?
1792 * In this case, ipf_nat6_icmperrorlookup() will have
1793 * returned NULL.
1794 */
1795 orgicmp = (struct icmp6_hdr *)dp;
1796
1797 if (odst == 1) {
1798 if (orgicmp->icmp6_id != nat->nat_osport) {
1799
1800 /*
1801 * Fix ICMP checksum (of the offening ICMP
1802 * query packet) to compensate the change
1803 * in the ICMP id of the offending ICMP
1804 * packet.
1805 *
1806 * Since you modify orgicmp->icmp6_id with
1807 * a delta (say x) and you compensate that
1808 * in origicmp->icmp6_cksum with a delta
1809 * minus x, you don't have to adjust the
1810 * overall icmp->icmp6_cksum
1811 */
1812 sum1 = ntohs(orgicmp->icmp6_id);
1813 sum2 = ntohs(nat->nat_osport);
1814 CALC_SUMD(sum1, sum2, sumd);
1815 orgicmp->icmp6_id = nat->nat_oicmpid;
1816 ipf_fix_datacksum(&orgicmp->icmp6_cksum, sumd);
1817 }
1818 } /* nat6_dir == NAT_INBOUND is impossible for icmp queries */
1819 }
1820 return (nat);
1821 }
1822
1823
1824 /*
1825 * MAP-IN MAP-OUT RDR-IN RDR-OUT
1826 * osrc X == src == src X
1827 * odst X == dst == dst X
1828 * nsrc == dst X X == dst
1829 * ndst == src X X == src
1830 * MAP = NAT_OUTBOUND, RDR = NAT_INBOUND
1831 */
1832 /*
1833 * NB: these lookups don't lock access to the list, it assumed that it has
1834 * already been done!
1835 */
1836 /* ------------------------------------------------------------------------ */
1837 /* Function: ipf_nat6_inlookup */
1838 /* Returns: nat6_t* - NULL == no match, */
1839 /* else pointer to matching NAT entry */
1840 /* Parameters: fin(I) - pointer to packet information */
1841 /* flags(I) - NAT flags for this packet */
1842 /* p(I) - protocol for this packet */
1843 /* src(I) - source IP address */
1844 /* mapdst(I) - destination IP address */
1845 /* */
1846 /* Lookup a nat entry based on the mapped destination ip address/port and */
1847 /* real source address/port. We use this lookup when receiving a packet, */
1848 /* we're looking for a table entry, based on the destination address. */
1849 /* */
1850 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */
1851 /* */
1852 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */
1853 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */
1854 /* */
1855 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */
1856 /* the packet is of said protocol */
1857 /* ------------------------------------------------------------------------ */
1858 nat_t *
ipf_nat6_inlookup(fr_info_t * fin,u_int flags,u_int p,struct in6_addr * src,struct in6_addr * mapdst)1859 ipf_nat6_inlookup(fr_info_t *fin, u_int flags, u_int p,
1860 struct in6_addr *src , struct in6_addr *mapdst)
1861 {
1862 ipf_main_softc_t *softc = fin->fin_main_soft;
1863 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
1864 u_short sport, dport;
1865 #ifdef IPF_V6_PROXIES
1866 ipnat_t *ipn;
1867 #endif
1868 nat_t *nat;
1869 int nflags;
1870 i6addr_t dst;
1871 void *ifp;
1872 u_int hv;
1873
1874 ifp = fin->fin_ifp;
1875 sport = 0;
1876 dport = 0;
1877 dst.in6 = *mapdst;
1878 switch (p)
1879 {
1880 case IPPROTO_TCP :
1881 case IPPROTO_UDP :
1882 sport = htons(fin->fin_data[0]);
1883 dport = htons(fin->fin_data[1]);
1884 break;
1885 case IPPROTO_ICMPV6 :
1886 if (flags & IPN_ICMPERR)
1887 sport = fin->fin_data[1];
1888 else
1889 dport = fin->fin_data[1];
1890 break;
1891 default :
1892 break;
1893 }
1894
1895
1896 if ((flags & SI_WILDP) != 0)
1897 goto find_in_wild_ports;
1898
1899 hv = NAT_HASH_FN6(&dst, dport, 0xffffffff);
1900 hv = NAT_HASH_FN6(src, hv + sport, softn->ipf_nat_table_sz);
1901 nat = softn->ipf_nat_table[1][hv];
1902 /* TRACE dst, dport, src, sport, hv, nat */
1903
1904 for (; nat; nat = nat->nat_hnext[1]) {
1905 if (nat->nat_ifps[0] != NULL) {
1906 if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
1907 continue;
1908 }
1909
1910 if (nat->nat_pr[0] != p)
1911 continue;
1912
1913 switch (nat->nat_dir)
1914 {
1915 case NAT_INBOUND :
1916 if (nat->nat_v[0] != 6)
1917 continue;
1918 if (IP6_NEQ(&nat->nat_osrc6, src) ||
1919 IP6_NEQ(&nat->nat_odst6, &dst))
1920 continue;
1921 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
1922 if (nat->nat_osport != sport)
1923 continue;
1924 if (nat->nat_odport != dport)
1925 continue;
1926
1927 } else if (p == IPPROTO_ICMPV6) {
1928 if (nat->nat_osport != dport) {
1929 continue;
1930 }
1931 }
1932 break;
1933 case NAT_OUTBOUND :
1934 if (nat->nat_v[1] != 6)
1935 continue;
1936 if (IP6_NEQ(&nat->nat_ndst6, src) ||
1937 IP6_NEQ(&nat->nat_nsrc6, &dst))
1938 continue;
1939 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
1940 if (nat->nat_ndport != sport)
1941 continue;
1942 if (nat->nat_nsport != dport)
1943 continue;
1944
1945 } else if (p == IPPROTO_ICMPV6) {
1946 if (nat->nat_osport != dport) {
1947 continue;
1948 }
1949 }
1950 break;
1951 }
1952
1953
1954 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
1955 #ifdef IPF_V6_PROXIES
1956 ipn = nat->nat_ptr;
1957 if ((ipn != NULL) && (nat->nat_aps != NULL))
1958 if (appr_match(fin, nat) != 0)
1959 continue;
1960 #endif
1961 }
1962 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
1963 nat->nat_ifps[0] = ifp;
1964 nat->nat_mtu[0] = GETIFMTU_6(ifp);
1965 }
1966 return (nat);
1967 }
1968
1969 /*
1970 * So if we didn't find it but there are wildcard members in the hash
1971 * table, go back and look for them. We do this search and update here
1972 * because it is modifying the NAT table and we want to do this only
1973 * for the first packet that matches. The exception, of course, is
1974 * for "dummy" (FI_IGNORE) lookups.
1975 */
1976 find_in_wild_ports:
1977 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
1978 NBUMPSIDE6DX(0, ns_lookup_miss, ns_lookup_miss_1);
1979 return (NULL);
1980 }
1981 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
1982 NBUMPSIDE6D(0, ns_lookup_nowild);
1983 return (NULL);
1984 }
1985
1986 RWLOCK_EXIT(&softc->ipf_nat);
1987
1988 hv = NAT_HASH_FN6(&dst, 0, 0xffffffff);
1989 hv = NAT_HASH_FN6(src, hv, softn->ipf_nat_table_sz);
1990 WRITE_ENTER(&softc->ipf_nat);
1991
1992 nat = softn->ipf_nat_table[1][hv];
1993 /* TRACE dst, src, hv, nat */
1994 for (; nat; nat = nat->nat_hnext[1]) {
1995 if (nat->nat_ifps[0] != NULL) {
1996 if ((ifp != NULL) && (ifp != nat->nat_ifps[0]))
1997 continue;
1998 }
1999
2000 if (nat->nat_pr[0] != fin->fin_p)
2001 continue;
2002
2003 switch (nat->nat_dir)
2004 {
2005 case NAT_INBOUND :
2006 if (nat->nat_v[0] != 6)
2007 continue;
2008 if (IP6_NEQ(&nat->nat_osrc6, src) ||
2009 IP6_NEQ(&nat->nat_odst6, &dst))
2010 continue;
2011 break;
2012 case NAT_OUTBOUND :
2013 if (nat->nat_v[1] != 6)
2014 continue;
2015 if (IP6_NEQ(&nat->nat_ndst6, src) ||
2016 IP6_NEQ(&nat->nat_nsrc6, &dst))
2017 continue;
2018 break;
2019 }
2020
2021 nflags = nat->nat_flags;
2022 if (!(nflags & (NAT_TCPUDP|SI_WILDP)))
2023 continue;
2024
2025 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nflags,
2026 NAT_INBOUND) == 1) {
2027 if ((fin->fin_flx & FI_IGNORE) != 0)
2028 break;
2029 if ((nflags & SI_CLONE) != 0) {
2030 nat = ipf_nat_clone(fin, nat);
2031 if (nat == NULL)
2032 break;
2033 } else {
2034 MUTEX_ENTER(&softn->ipf_nat_new);
2035 softn->ipf_nat_stats.ns_wilds--;
2036 MUTEX_EXIT(&softn->ipf_nat_new);
2037 }
2038
2039 if (nat->nat_dir == NAT_INBOUND) {
2040 if (nat->nat_osport == 0) {
2041 nat->nat_osport = sport;
2042 nat->nat_nsport = sport;
2043 }
2044 if (nat->nat_odport == 0) {
2045 nat->nat_odport = dport;
2046 nat->nat_ndport = dport;
2047 }
2048 } else {
2049 if (nat->nat_osport == 0) {
2050 nat->nat_osport = dport;
2051 nat->nat_nsport = dport;
2052 }
2053 if (nat->nat_odport == 0) {
2054 nat->nat_odport = sport;
2055 nat->nat_ndport = sport;
2056 }
2057 }
2058 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) {
2059 nat->nat_ifps[0] = ifp;
2060 nat->nat_mtu[0] = GETIFMTU_6(ifp);
2061 }
2062 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
2063 ipf_nat6_tabmove(softn, nat);
2064 break;
2065 }
2066 }
2067
2068 MUTEX_DOWNGRADE(&softc->ipf_nat);
2069
2070 if (nat == NULL) {
2071 NBUMPSIDE6DX(0, ns_lookup_miss, ns_lookup_miss_2);
2072 }
2073 return (nat);
2074 }
2075
2076
2077 /* ------------------------------------------------------------------------ */
2078 /* Function: ipf_nat6_tabmove */
2079 /* Returns: Nil */
2080 /* Parameters: nat(I) - pointer to NAT structure */
2081 /* Write Lock: ipf_nat */
2082 /* */
2083 /* This function is only called for TCP/UDP NAT table entries where the */
2084 /* original was placed in the table without hashing on the ports and we now */
2085 /* want to include hashing on port numbers. */
2086 /* ------------------------------------------------------------------------ */
2087 static void
ipf_nat6_tabmove(ipf_nat_softc_t * softn,nat_t * nat)2088 ipf_nat6_tabmove(ipf_nat_softc_t *softn, nat_t *nat)
2089 {
2090 nat_t **natp;
2091 u_int hv0, hv1;
2092
2093 if (nat->nat_flags & SI_CLONE)
2094 return;
2095
2096 /*
2097 * Remove the NAT entry from the old location
2098 */
2099 if (nat->nat_hnext[0])
2100 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0];
2101 *nat->nat_phnext[0] = nat->nat_hnext[0];
2102 softn->ipf_nat_stats.ns_side[0].ns_bucketlen[nat->nat_hv[0]]--;
2103
2104 if (nat->nat_hnext[1])
2105 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1];
2106 *nat->nat_phnext[1] = nat->nat_hnext[1];
2107 softn->ipf_nat_stats.ns_side[1].ns_bucketlen[nat->nat_hv[1]]--;
2108
2109 /*
2110 * Add into the NAT table in the new position
2111 */
2112 if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_DIVERTIN) {
2113 hv1 = NAT_HASH_FN6(&nat->nat_osrc6,
2114 nat->nat_osport, 0xffffffff);
2115 hv1 = NAT_HASH_FN6(&nat->nat_odst6, hv1 + nat->nat_odport,
2116 softn->ipf_nat_table_sz);
2117 hv0 = NAT_HASH_FN6(&nat->nat_nsrc6,
2118 nat->nat_nsport, 0xffffffff);
2119 hv0 = NAT_HASH_FN6(&nat->nat_ndst6, hv0 + nat->nat_ndport,
2120 softn->ipf_nat_table_sz);
2121 } else {
2122 hv0 = NAT_HASH_FN6(&nat->nat_osrc6,
2123 nat->nat_osport, 0xffffffff);
2124 hv0 = NAT_HASH_FN6(&nat->nat_odst6, hv0 + nat->nat_odport,
2125 softn->ipf_nat_table_sz);
2126 hv1 = NAT_HASH_FN6(&nat->nat_nsrc6,
2127 nat->nat_nsport, 0xffffffff);
2128 hv1 = NAT_HASH_FN6(&nat->nat_ndst6, hv1 + nat->nat_ndport,
2129 softn->ipf_nat_table_sz);
2130 }
2131
2132 /* TRACE nat_osrc6, nat_osport, nat_odst6, nat_odport, hv0 */
2133 /* TRACE nat_nsrc6, nat_nsport, nat_ndst6, nat_ndport, hv1 */
2134
2135 nat->nat_hv[0] = hv0;
2136 natp = &softn->ipf_nat_table[0][hv0];
2137 if (*natp)
2138 (*natp)->nat_phnext[0] = &nat->nat_hnext[0];
2139 nat->nat_phnext[0] = natp;
2140 nat->nat_hnext[0] = *natp;
2141 *natp = nat;
2142 softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0]++;
2143
2144 nat->nat_hv[1] = hv1;
2145 natp = &softn->ipf_nat_table[1][hv1];
2146 if (*natp)
2147 (*natp)->nat_phnext[1] = &nat->nat_hnext[1];
2148 nat->nat_phnext[1] = natp;
2149 nat->nat_hnext[1] = *natp;
2150 *natp = nat;
2151 softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1]++;
2152 }
2153
2154
2155 /* ------------------------------------------------------------------------ */
2156 /* Function: ipf_nat6_outlookup */
2157 /* Returns: nat6_t* - NULL == no match, */
2158 /* else pointer to matching NAT entry */
2159 /* Parameters: fin(I) - pointer to packet information */
2160 /* flags(I) - NAT flags for this packet */
2161 /* p(I) - protocol for this packet */
2162 /* src(I) - source IP address */
2163 /* dst(I) - destination IP address */
2164 /* rw(I) - 1 == write lock on held, 0 == read lock. */
2165 /* */
2166 /* Lookup a nat entry based on the source 'real' ip address/port and */
2167 /* destination address/port. We use this lookup when sending a packet out, */
2168 /* we're looking for a table entry, based on the source address. */
2169 /* */
2170 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */
2171 /* */
2172 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */
2173 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */
2174 /* */
2175 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */
2176 /* the packet is of said protocol */
2177 /* ------------------------------------------------------------------------ */
2178 nat_t *
ipf_nat6_outlookup(fr_info_t * fin,u_int flags,u_int p,struct in6_addr * src,struct in6_addr * dst)2179 ipf_nat6_outlookup(fr_info_t *fin, u_int flags, u_int p,
2180 struct in6_addr *src, struct in6_addr *dst)
2181 {
2182 ipf_main_softc_t *softc = fin->fin_main_soft;
2183 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2184 u_short sport, dport;
2185 #ifdef IPF_V6_PROXIES
2186 ipnat_t *ipn;
2187 #endif
2188 nat_t *nat;
2189 void *ifp;
2190 u_int hv;
2191
2192 ifp = fin->fin_ifp;
2193 sport = 0;
2194 dport = 0;
2195
2196 switch (p)
2197 {
2198 case IPPROTO_TCP :
2199 case IPPROTO_UDP :
2200 sport = htons(fin->fin_data[0]);
2201 dport = htons(fin->fin_data[1]);
2202 break;
2203 case IPPROTO_ICMPV6 :
2204 if (flags & IPN_ICMPERR)
2205 sport = fin->fin_data[1];
2206 else
2207 dport = fin->fin_data[1];
2208 break;
2209 default :
2210 break;
2211 }
2212
2213 if ((flags & SI_WILDP) != 0)
2214 goto find_out_wild_ports;
2215
2216 hv = NAT_HASH_FN6(src, sport, 0xffffffff);
2217 hv = NAT_HASH_FN6(dst, hv + dport, softn->ipf_nat_table_sz);
2218 nat = softn->ipf_nat_table[0][hv];
2219
2220 /* TRACE src, sport, dst, dport, hv, nat */
2221
2222 for (; nat; nat = nat->nat_hnext[0]) {
2223 if (nat->nat_ifps[1] != NULL) {
2224 if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
2225 continue;
2226 }
2227
2228 if (nat->nat_pr[1] != p)
2229 continue;
2230
2231 switch (nat->nat_dir)
2232 {
2233 case NAT_INBOUND :
2234 if (nat->nat_v[1] != 6)
2235 continue;
2236 if (IP6_NEQ(&nat->nat_ndst6, src) ||
2237 IP6_NEQ(&nat->nat_nsrc6, dst))
2238 continue;
2239
2240 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
2241 if (nat->nat_ndport != sport)
2242 continue;
2243 if (nat->nat_nsport != dport)
2244 continue;
2245
2246 } else if (p == IPPROTO_ICMPV6) {
2247 if (nat->nat_osport != dport) {
2248 continue;
2249 }
2250 }
2251 break;
2252 case NAT_OUTBOUND :
2253 if (nat->nat_v[0] != 6)
2254 continue;
2255 if (IP6_NEQ(&nat->nat_osrc6, src) ||
2256 IP6_NEQ(&nat->nat_odst6, dst))
2257 continue;
2258
2259 if ((nat->nat_flags & IPN_TCPUDP) != 0) {
2260 if (nat->nat_odport != dport)
2261 continue;
2262 if (nat->nat_osport != sport)
2263 continue;
2264
2265 } else if (p == IPPROTO_ICMPV6) {
2266 if (nat->nat_osport != dport) {
2267 continue;
2268 }
2269 }
2270 break;
2271 }
2272
2273 #ifdef IPF_V6_PROXIES
2274 ipn = nat->nat_ptr;
2275 if ((ipn != NULL) && (nat->nat_aps != NULL))
2276 if (appr_match(fin, nat) != 0)
2277 continue;
2278 #endif
2279
2280 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
2281 nat->nat_ifps[1] = ifp;
2282 nat->nat_mtu[1] = GETIFMTU_6(ifp);
2283 }
2284 return (nat);
2285 }
2286
2287 /*
2288 * So if we didn't find it but there are wildcard members in the hash
2289 * table, go back and look for them. We do this search and update here
2290 * because it is modifying the NAT table and we want to do this only
2291 * for the first packet that matches. The exception, of course, is
2292 * for "dummy" (FI_IGNORE) lookups.
2293 */
2294 find_out_wild_ports:
2295 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) {
2296 NBUMPSIDE6DX(1, ns_lookup_miss, ns_lookup_miss_3);
2297 return (NULL);
2298 }
2299 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) {
2300 NBUMPSIDE6D(1, ns_lookup_nowild);
2301 return (NULL);
2302 }
2303
2304 RWLOCK_EXIT(&softc->ipf_nat);
2305
2306 hv = NAT_HASH_FN6(src, 0, 0xffffffff);
2307 hv = NAT_HASH_FN6(dst, hv, softn->ipf_nat_table_sz);
2308
2309 WRITE_ENTER(&softc->ipf_nat);
2310
2311 nat = softn->ipf_nat_table[0][hv];
2312 for (; nat; nat = nat->nat_hnext[0]) {
2313 if (nat->nat_ifps[1] != NULL) {
2314 if ((ifp != NULL) && (ifp != nat->nat_ifps[1]))
2315 continue;
2316 }
2317
2318 if (nat->nat_pr[1] != fin->fin_p)
2319 continue;
2320
2321 switch (nat->nat_dir)
2322 {
2323 case NAT_INBOUND :
2324 if (nat->nat_v[1] != 6)
2325 continue;
2326 if (IP6_NEQ(&nat->nat_ndst6, src) ||
2327 IP6_NEQ(&nat->nat_nsrc6, dst))
2328 continue;
2329 break;
2330 case NAT_OUTBOUND :
2331 if (nat->nat_v[0] != 6)
2332 continue;
2333 if (IP6_NEQ(&nat->nat_osrc6, src) ||
2334 IP6_NEQ(&nat->nat_odst6, dst))
2335 continue;
2336 break;
2337 }
2338
2339 if (!(nat->nat_flags & (NAT_TCPUDP|SI_WILDP)))
2340 continue;
2341
2342 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nat->nat_flags,
2343 NAT_OUTBOUND) == 1) {
2344 if ((fin->fin_flx & FI_IGNORE) != 0)
2345 break;
2346 if ((nat->nat_flags & SI_CLONE) != 0) {
2347 nat = ipf_nat_clone(fin, nat);
2348 if (nat == NULL)
2349 break;
2350 } else {
2351 MUTEX_ENTER(&softn->ipf_nat_new);
2352 softn->ipf_nat_stats.ns_wilds--;
2353 MUTEX_EXIT(&softn->ipf_nat_new);
2354 }
2355
2356 if (nat->nat_dir == NAT_OUTBOUND) {
2357 if (nat->nat_osport == 0) {
2358 nat->nat_osport = sport;
2359 nat->nat_nsport = sport;
2360 }
2361 if (nat->nat_odport == 0) {
2362 nat->nat_odport = dport;
2363 nat->nat_ndport = dport;
2364 }
2365 } else {
2366 if (nat->nat_osport == 0) {
2367 nat->nat_osport = dport;
2368 nat->nat_nsport = dport;
2369 }
2370 if (nat->nat_odport == 0) {
2371 nat->nat_odport = sport;
2372 nat->nat_ndport = sport;
2373 }
2374 }
2375 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) {
2376 nat->nat_ifps[1] = ifp;
2377 nat->nat_mtu[1] = GETIFMTU_6(ifp);
2378 }
2379 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT);
2380 ipf_nat6_tabmove(softn, nat);
2381 break;
2382 }
2383 }
2384
2385 MUTEX_DOWNGRADE(&softc->ipf_nat);
2386
2387 if (nat == NULL) {
2388 NBUMPSIDE6DX(1, ns_lookup_miss, ns_lookup_miss_4);
2389 }
2390 return (nat);
2391 }
2392
2393
2394 /* ------------------------------------------------------------------------ */
2395 /* Function: ipf_nat6_lookupredir */
2396 /* Returns: nat6_t* - NULL == no match, */
2397 /* else pointer to matching NAT entry */
2398 /* Parameters: np(I) - pointer to description of packet to find NAT table */
2399 /* entry for. */
2400 /* */
2401 /* Lookup the NAT tables to search for a matching redirect */
2402 /* The contents of natlookup_t should imitate those found in a packet that */
2403 /* would be translated - ie a packet coming in for RDR or going out for MAP.*/
2404 /* We can do the lookup in one of two ways, imitating an inbound or */
2405 /* outbound packet. By default we assume outbound, unless IPN_IN is set. */
2406 /* For IN, the fields are set as follows: */
2407 /* nl_real* = source information */
2408 /* nl_out* = destination information (translated) */
2409 /* For an out packet, the fields are set like this: */
2410 /* nl_in* = source information (untranslated) */
2411 /* nl_out* = destination information (translated) */
2412 /* ------------------------------------------------------------------------ */
2413 nat_t *
ipf_nat6_lookupredir(natlookup_t * np)2414 ipf_nat6_lookupredir(natlookup_t *np)
2415 {
2416 fr_info_t fi;
2417 nat_t *nat;
2418
2419 bzero((char *)&fi, sizeof(fi));
2420 if (np->nl_flags & IPN_IN) {
2421 fi.fin_data[0] = ntohs(np->nl_realport);
2422 fi.fin_data[1] = ntohs(np->nl_outport);
2423 } else {
2424 fi.fin_data[0] = ntohs(np->nl_inport);
2425 fi.fin_data[1] = ntohs(np->nl_outport);
2426 }
2427 if (np->nl_flags & IPN_TCP)
2428 fi.fin_p = IPPROTO_TCP;
2429 else if (np->nl_flags & IPN_UDP)
2430 fi.fin_p = IPPROTO_UDP;
2431 else if (np->nl_flags & (IPN_ICMPERR|IPN_ICMPQUERY))
2432 fi.fin_p = IPPROTO_ICMPV6;
2433
2434 /*
2435 * We can do two sorts of lookups:
2436 * - IPN_IN: we have the `real' and `out' address, look for `in'.
2437 * - default: we have the `in' and `out' address, look for `real'.
2438 */
2439 if (np->nl_flags & IPN_IN) {
2440 if ((nat = ipf_nat6_inlookup(&fi, np->nl_flags, fi.fin_p,
2441 &np->nl_realip6,
2442 &np->nl_outip6))) {
2443 np->nl_inip6 = nat->nat_odst6.in6;
2444 np->nl_inport = nat->nat_odport;
2445 }
2446 } else {
2447 /*
2448 * If nl_inip is non null, this is a lookup based on the real
2449 * ip address. Else, we use the fake.
2450 */
2451 if ((nat = ipf_nat6_outlookup(&fi, np->nl_flags, fi.fin_p,
2452 &np->nl_inip6, &np->nl_outip6))) {
2453
2454 if ((np->nl_flags & IPN_FINDFORWARD) != 0) {
2455 fr_info_t fin;
2456 bzero((char *)&fin, sizeof(fin));
2457 fin.fin_p = nat->nat_pr[0];
2458 fin.fin_data[0] = ntohs(nat->nat_ndport);
2459 fin.fin_data[1] = ntohs(nat->nat_nsport);
2460 if (ipf_nat6_inlookup(&fin, np->nl_flags,
2461 fin.fin_p,
2462 &nat->nat_ndst6.in6,
2463 &nat->nat_nsrc6.in6) !=
2464 NULL) {
2465 np->nl_flags &= ~IPN_FINDFORWARD;
2466 }
2467 }
2468
2469 np->nl_realip6 = nat->nat_odst6.in6;
2470 np->nl_realport = nat->nat_odport;
2471 }
2472 }
2473
2474 return (nat);
2475 }
2476
2477
2478 /* ------------------------------------------------------------------------ */
2479 /* Function: ipf_nat6_match */
2480 /* Returns: int - 0 == no match, 1 == match */
2481 /* Parameters: fin(I) - pointer to packet information */
2482 /* np(I) - pointer to NAT rule */
2483 /* */
2484 /* Pull the matching of a packet against a NAT rule out of that complex */
2485 /* loop inside ipf_nat6_checkin() and lay it out properly in its own */
2486 /* function. */
2487 /* ------------------------------------------------------------------------ */
2488 static int
ipf_nat6_match(fr_info_t * fin,ipnat_t * np)2489 ipf_nat6_match(fr_info_t *fin, ipnat_t *np)
2490 {
2491 frtuc_t *ft;
2492 int match;
2493
2494 match = 0;
2495 switch (np->in_osrcatype)
2496 {
2497 case FRI_NORMAL :
2498 match = IP6_MASKNEQ(&fin->fin_src6, &np->in_osrcmsk6,
2499 &np->in_osrcip6);
2500 break;
2501 case FRI_LOOKUP :
2502 match = (*np->in_osrcfunc)(fin->fin_main_soft, np->in_osrcptr,
2503 6, &fin->fin_src6, fin->fin_plen);
2504 break;
2505 }
2506 match ^= ((np->in_flags & IPN_NOTSRC) != 0);
2507 if (match)
2508 return (0);
2509
2510 match = 0;
2511 switch (np->in_odstatype)
2512 {
2513 case FRI_NORMAL :
2514 match = IP6_MASKNEQ(&fin->fin_dst6, &np->in_odstmsk6,
2515 &np->in_odstip6);
2516 break;
2517 case FRI_LOOKUP :
2518 match = (*np->in_odstfunc)(fin->fin_main_soft, np->in_odstptr,
2519 6, &fin->fin_dst6, fin->fin_plen);
2520 break;
2521 }
2522
2523 match ^= ((np->in_flags & IPN_NOTDST) != 0);
2524 if (match)
2525 return (0);
2526
2527 ft = &np->in_tuc;
2528 if (!(fin->fin_flx & FI_TCPUDP) ||
2529 (fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) {
2530 if (ft->ftu_scmp || ft->ftu_dcmp)
2531 return (0);
2532 return (1);
2533 }
2534
2535 return (ipf_tcpudpchk(&fin->fin_fi, ft));
2536 }
2537
2538
2539 /* ------------------------------------------------------------------------ */
2540 /* Function: ipf_nat6_checkout */
2541 /* Returns: int - -1 == packet failed NAT checks so block it, */
2542 /* 0 == no packet translation occurred, */
2543 /* 1 == packet was successfully translated. */
2544 /* Parameters: fin(I) - pointer to packet information */
2545 /* passp(I) - pointer to filtering result flags */
2546 /* */
2547 /* Check to see if an outcoming packet should be changed. ICMP packets are */
2548 /* first checked to see if they match an existing entry (if an error), */
2549 /* otherwise a search of the current NAT table is made. If neither results */
2550 /* in a match then a search for a matching NAT rule is made. Create a new */
2551 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */
2552 /* packet header(s) as required. */
2553 /* ------------------------------------------------------------------------ */
2554 int
ipf_nat6_checkout(fr_info_t * fin,u_32_t * passp)2555 ipf_nat6_checkout(fr_info_t *fin, u_32_t *passp)
2556 {
2557 ipf_main_softc_t *softc = fin->fin_main_soft;
2558 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2559 struct icmp6_hdr *icmp6 = NULL;
2560 struct ifnet *ifp, *sifp;
2561 int rval, natfailed;
2562 ipnat_t *np = NULL;
2563 u_int nflags = 0;
2564 i6addr_t ipa, iph;
2565 int natadd = 1;
2566 frentry_t *fr;
2567 nat_t *nat;
2568
2569 if (softn->ipf_nat_stats.ns_rules == 0 || softn->ipf_nat_lock != 0)
2570 return (0);
2571
2572 icmp6 = NULL;
2573 natfailed = 0;
2574 fr = fin->fin_fr;
2575 sifp = fin->fin_ifp;
2576 if (fr != NULL) {
2577 ifp = fr->fr_tifs[fin->fin_rev].fd_ptr;
2578 if ((ifp != NULL) && (ifp != (void *)-1))
2579 fin->fin_ifp = ifp;
2580 }
2581 ifp = fin->fin_ifp;
2582
2583 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
2584 switch (fin->fin_p)
2585 {
2586 case IPPROTO_TCP :
2587 nflags = IPN_TCP;
2588 break;
2589 case IPPROTO_UDP :
2590 nflags = IPN_UDP;
2591 break;
2592 case IPPROTO_ICMPV6 :
2593 icmp6 = fin->fin_dp;
2594
2595 /*
2596 * Apart from ECHO request and reply, all other
2597 * informational messages should not be translated
2598 * so as to keep IPv6 working.
2599 */
2600 if (icmp6->icmp6_type > ICMP6_ECHO_REPLY)
2601 return (0);
2602
2603 /*
2604 * This is an incoming packet, so the destination is
2605 * the icmp6_id and the source port equals 0
2606 */
2607 if ((fin->fin_flx & FI_ICMPQUERY) != 0)
2608 nflags = IPN_ICMPQUERY;
2609 break;
2610 default :
2611 break;
2612 }
2613 }
2614
2615 ipa = fin->fin_src6;
2616
2617 READ_ENTER(&softc->ipf_nat);
2618
2619 if ((fin->fin_p == IPPROTO_ICMPV6) && !(nflags & IPN_ICMPQUERY) &&
2620 (nat = ipf_nat6_icmperror(fin, &nflags, NAT_OUTBOUND)))
2621 /*EMPTY*/;
2622 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
2623 natadd = 0;
2624 else if ((nat = ipf_nat6_outlookup(fin, nflags|NAT_SEARCH,
2625 (u_int)fin->fin_p,
2626 &fin->fin_src6.in6,
2627 &fin->fin_dst6.in6))) {
2628 nflags = nat->nat_flags;
2629 } else if (fin->fin_off == 0) {
2630 u_32_t hv, nmsk = 0;
2631 i6addr_t *msk;
2632
2633 /*
2634 * If there is no current entry in the nat table for this IP#,
2635 * create one for it (if there is a matching rule).
2636 */
2637 maskloop:
2638 msk = &softn->ipf_nat6_map_active_masks[nmsk];
2639 IP6_AND(&ipa, msk, &iph);
2640 hv = NAT_HASH_FN6(&iph, 0, softn->ipf_nat_maprules_sz);
2641 for (np = softn->ipf_nat_map_rules[hv]; np; np = np->in_mnext) {
2642 if ((np->in_ifps[1] && (np->in_ifps[1] != ifp)))
2643 continue;
2644 if (np->in_v[0] != 6)
2645 continue;
2646 if (np->in_pr[1] && (np->in_pr[1] != fin->fin_p))
2647 continue;
2648 if ((np->in_flags & IPN_RF) &&
2649 !(np->in_flags & nflags))
2650 continue;
2651 if (np->in_flags & IPN_FILTER) {
2652 switch (ipf_nat6_match(fin, np))
2653 {
2654 case 0 :
2655 continue;
2656 case -1 :
2657 rval = -1;
2658 goto outmatchfail;
2659 case 1 :
2660 default :
2661 break;
2662 }
2663 } else if (!IP6_MASKEQ(&ipa, &np->in_osrcmsk,
2664 &np->in_osrcip6))
2665 continue;
2666
2667 if ((fr != NULL) &&
2668 !ipf_matchtag(&np->in_tag, &fr->fr_nattag))
2669 continue;
2670
2671 #ifdef IPF_V6_PROXIES
2672 if (np->in_plabel != -1) {
2673 if (((np->in_flags & IPN_FILTER) == 0) &&
2674 (np->in_odport != fin->fin_data[1]))
2675 continue;
2676 if (appr_ok(fin, tcp, np) == 0)
2677 continue;
2678 }
2679 #endif
2680
2681 if (np->in_flags & IPN_NO) {
2682 np->in_hits++;
2683 break;
2684 }
2685
2686 MUTEX_ENTER(&softn->ipf_nat_new);
2687 nat = ipf_nat6_add(fin, np, NULL, nflags, NAT_OUTBOUND);
2688 MUTEX_EXIT(&softn->ipf_nat_new);
2689 if (nat != NULL) {
2690 np->in_hits++;
2691 break;
2692 }
2693 natfailed = -1;
2694 }
2695 if ((np == NULL) && (nmsk < softn->ipf_nat6_map_max)) {
2696 nmsk++;
2697 goto maskloop;
2698 }
2699 }
2700
2701 if (nat != NULL) {
2702 rval = ipf_nat6_out(fin, nat, natadd, nflags);
2703 if (rval == 1) {
2704 MUTEX_ENTER(&nat->nat_lock);
2705 ipf_nat_update(fin, nat);
2706 nat->nat_bytes[1] += fin->fin_plen;
2707 nat->nat_pkts[1]++;
2708 MUTEX_EXIT(&nat->nat_lock);
2709 }
2710 } else
2711 rval = natfailed;
2712 outmatchfail:
2713 RWLOCK_EXIT(&softc->ipf_nat);
2714
2715 switch (rval)
2716 {
2717 case -1 :
2718 if (passp != NULL) {
2719 NBUMPSIDE6D(1, ns_drop);
2720 *passp = FR_BLOCK;
2721 fin->fin_reason = FRB_NATV6;
2722 }
2723 fin->fin_flx |= FI_BADNAT;
2724 NBUMPSIDE6D(1, ns_badnat);
2725 break;
2726 case 0 :
2727 NBUMPSIDE6D(1, ns_ignored);
2728 break;
2729 case 1 :
2730 NBUMPSIDE6D(1, ns_translated);
2731 break;
2732 }
2733 fin->fin_ifp = sifp;
2734 return (rval);
2735 }
2736
2737 /* ------------------------------------------------------------------------ */
2738 /* Function: ipf_nat6_out */
2739 /* Returns: int - -1 == packet failed NAT checks so block it, */
2740 /* 1 == packet was successfully translated. */
2741 /* Parameters: fin(I) - pointer to packet information */
2742 /* nat(I) - pointer to NAT structure */
2743 /* natadd(I) - flag indicating if it is safe to add frag cache */
2744 /* nflags(I) - NAT flags set for this packet */
2745 /* */
2746 /* Translate a packet coming "out" on an interface. */
2747 /* ------------------------------------------------------------------------ */
2748 static int
ipf_nat6_out(fr_info_t * fin,nat_t * nat,int natadd,u_32_t nflags)2749 ipf_nat6_out(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
2750 {
2751 ipf_main_softc_t *softc = fin->fin_main_soft;
2752 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2753 struct icmp6_hdr *icmp6;
2754 tcphdr_t *tcp;
2755 ipnat_t *np;
2756 int skip;
2757 int i;
2758
2759 tcp = NULL;
2760 icmp6 = NULL;
2761 np = nat->nat_ptr;
2762
2763 if ((natadd != 0) && (fin->fin_flx & FI_FRAG) && (np != NULL))
2764 (void) ipf_frag_natnew(softc, fin, 0, nat);
2765
2766 /*
2767 * Address assignment is after the checksum modification because
2768 * we are using the address in the packet for determining the
2769 * correct checksum offset (the ICMP error could be coming from
2770 * anyone...)
2771 */
2772 switch (nat->nat_dir)
2773 {
2774 case NAT_OUTBOUND :
2775 fin->fin_ip6->ip6_src = nat->nat_nsrc6.in6;
2776 fin->fin_src6 = nat->nat_nsrc6;
2777 fin->fin_ip6->ip6_dst = nat->nat_ndst6.in6;
2778 fin->fin_dst6 = nat->nat_ndst6;
2779 break;
2780
2781 case NAT_INBOUND :
2782 fin->fin_ip6->ip6_src = nat->nat_odst6.in6;
2783 fin->fin_src6 = nat->nat_ndst6;
2784 fin->fin_ip6->ip6_dst = nat->nat_osrc6.in6;
2785 fin->fin_dst6 = nat->nat_nsrc6;
2786 break;
2787
2788 case NAT_DIVERTIN :
2789 {
2790 mb_t *m;
2791
2792 skip = ipf_nat6_decap(fin, nat);
2793 if (skip <= 0) {
2794 NBUMPSIDE6D(1, ns_decap_fail);
2795 return (-1);
2796 }
2797
2798 m = fin->fin_m;
2799
2800 #if SOLARIS && defined(_KERNEL)
2801 m->b_rptr += skip;
2802 #else
2803 m->m_data += skip;
2804 m->m_len -= skip;
2805
2806 # ifdef M_PKTHDR
2807 if (m->m_flags & M_PKTHDR)
2808 m->m_pkthdr.len -= skip;
2809 # endif
2810 #endif
2811
2812 MUTEX_ENTER(&nat->nat_lock);
2813 ipf_nat_update(fin, nat);
2814 MUTEX_EXIT(&nat->nat_lock);
2815 fin->fin_flx |= FI_NATED;
2816 if (np != NULL && np->in_tag.ipt_num[0] != 0)
2817 fin->fin_nattag = &np->in_tag;
2818 return (1);
2819 /* NOTREACHED */
2820 }
2821
2822 case NAT_DIVERTOUT :
2823 {
2824 udphdr_t *uh;
2825 ip6_t *ip6;
2826 mb_t *m;
2827
2828 m = M_DUP(np->in_divmp);
2829 if (m == NULL) {
2830 NBUMPSIDE6D(1, ns_divert_dup);
2831 return (-1);
2832 }
2833
2834 ip6 = MTOD(m, ip6_t *);
2835
2836 ip6->ip6_plen = htons(fin->fin_plen + 8);
2837
2838 uh = (udphdr_t *)(ip6 + 1);
2839 uh->uh_ulen = htons(fin->fin_plen);
2840
2841 PREP_MB_T(fin, m);
2842
2843 fin->fin_ip6 = ip6;
2844 fin->fin_plen += sizeof(ip6_t) + 8; /* UDP + new IPv4 hdr */
2845 fin->fin_dlen += sizeof(ip6_t) + 8; /* UDP + old IPv4 hdr */
2846
2847 nflags &= ~IPN_TCPUDPICMP;
2848
2849 break;
2850 }
2851
2852 default :
2853 break;
2854 }
2855
2856 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
2857 u_short *csump;
2858
2859 if ((nat->nat_nsport != 0) && (nflags & IPN_TCPUDP)) {
2860 tcp = fin->fin_dp;
2861
2862 switch (nat->nat_dir)
2863 {
2864 case NAT_OUTBOUND :
2865 tcp->th_sport = nat->nat_nsport;
2866 fin->fin_data[0] = ntohs(nat->nat_nsport);
2867 tcp->th_dport = nat->nat_ndport;
2868 fin->fin_data[1] = ntohs(nat->nat_ndport);
2869 break;
2870
2871 case NAT_INBOUND :
2872 tcp->th_sport = nat->nat_odport;
2873 fin->fin_data[0] = ntohs(nat->nat_odport);
2874 tcp->th_dport = nat->nat_osport;
2875 fin->fin_data[1] = ntohs(nat->nat_osport);
2876 break;
2877 }
2878 }
2879
2880 if ((nat->nat_nsport != 0) && (nflags & IPN_ICMPQUERY)) {
2881 icmp6 = fin->fin_dp;
2882 icmp6->icmp6_id = nat->nat_nicmpid;
2883 }
2884
2885 csump = ipf_nat_proto(fin, nat, nflags);
2886
2887 /*
2888 * The above comments do not hold for layer 4 (or higher)
2889 * checksums...
2890 */
2891 if (csump != NULL) {
2892 if (nat->nat_dir == NAT_OUTBOUND)
2893 ipf_fix_outcksum(fin->fin_cksum, csump,
2894 nat->nat_sumd[0],
2895 nat->nat_sumd[1] +
2896 fin->fin_dlen);
2897 else
2898 ipf_fix_incksum(fin->fin_cksum, csump,
2899 nat->nat_sumd[0],
2900 nat->nat_sumd[1] +
2901 fin->fin_dlen);
2902 }
2903 }
2904
2905 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
2906 /* ------------------------------------------------------------- */
2907 /* A few quick notes: */
2908 /* Following are test conditions prior to calling the */
2909 /* ipf_proxy_check routine. */
2910 /* */
2911 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */
2912 /* with a redirect rule, we attempt to match the packet's */
2913 /* source port against in_dport, otherwise we'd compare the */
2914 /* packet's destination. */
2915 /* ------------------------------------------------------------- */
2916 if ((np != NULL) && (np->in_apr != NULL)) {
2917 i = ipf_proxy_check(fin, nat);
2918 if (i == -1) {
2919 NBUMPSIDE6D(1, ns_ipf_proxy_fail);
2920 }
2921 } else {
2922 i = 1;
2923 }
2924 fin->fin_flx |= FI_NATED;
2925 return (i);
2926 }
2927
2928
2929 /* ------------------------------------------------------------------------ */
2930 /* Function: ipf_nat6_checkin */
2931 /* Returns: int - -1 == packet failed NAT checks so block it, */
2932 /* 0 == no packet translation occurred, */
2933 /* 1 == packet was successfully translated. */
2934 /* Parameters: fin(I) - pointer to packet information */
2935 /* passp(I) - pointer to filtering result flags */
2936 /* */
2937 /* Check to see if an incoming packet should be changed. ICMP packets are */
2938 /* first checked to see if they match an existing entry (if an error), */
2939 /* otherwise a search of the current NAT table is made. If neither results */
2940 /* in a match then a search for a matching NAT rule is made. Create a new */
2941 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */
2942 /* packet header(s) as required. */
2943 /* ------------------------------------------------------------------------ */
2944 int
ipf_nat6_checkin(fr_info_t * fin,u_32_t * passp)2945 ipf_nat6_checkin(fr_info_t *fin, u_32_t *passp)
2946 {
2947 ipf_main_softc_t *softc = fin->fin_main_soft;
2948 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
2949 struct icmp6_hdr *icmp6;
2950 u_int nflags, natadd;
2951 int rval, natfailed;
2952 struct ifnet *ifp;
2953 i6addr_t ipa, iph;
2954 #ifdef IPF_V6_PROXIES
2955 tcphdr_t *tcp = NULL;
2956 #endif
2957 u_short dport;
2958 ipnat_t *np;
2959 nat_t *nat;
2960
2961 if (softn->ipf_nat_stats.ns_rules == 0 || softn->ipf_nat_lock != 0)
2962 return (0);
2963
2964 icmp6 = NULL;
2965 dport = 0;
2966 natadd = 1;
2967 nflags = 0;
2968 natfailed = 0;
2969 ifp = fin->fin_ifp;
2970
2971 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
2972 switch (fin->fin_p)
2973 {
2974 case IPPROTO_TCP :
2975 nflags = IPN_TCP;
2976 break;
2977 case IPPROTO_UDP :
2978 nflags = IPN_UDP;
2979 break;
2980 case IPPROTO_ICMPV6 :
2981 icmp6 = fin->fin_dp;
2982
2983 /*
2984 * Apart from ECHO request and reply, all other
2985 * informational messages should not be translated
2986 * so as to keep IPv6 working.
2987 */
2988 if (icmp6->icmp6_type > ICMP6_ECHO_REPLY)
2989 return (0);
2990
2991 /*
2992 * This is an incoming packet, so the destination is
2993 * the icmp6_id and the source port equals 0
2994 */
2995 if ((fin->fin_flx & FI_ICMPQUERY) != 0) {
2996 nflags = IPN_ICMPQUERY;
2997 dport = icmp6->icmp6_id;
2998 } break;
2999 default :
3000 break;
3001 }
3002
3003 if ((nflags & IPN_TCPUDP)) {
3004 #ifdef IPF_V6_PROXIES
3005 tcp = fin->fin_dp;
3006 #endif
3007 dport = fin->fin_data[1];
3008 }
3009 }
3010
3011 ipa = fin->fin_dst6;
3012
3013 READ_ENTER(&softc->ipf_nat);
3014
3015 if ((fin->fin_p == IPPROTO_ICMPV6) && !(nflags & IPN_ICMPQUERY) &&
3016 (nat = ipf_nat6_icmperror(fin, &nflags, NAT_INBOUND)))
3017 /*EMPTY*/;
3018 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin)))
3019 natadd = 0;
3020 else if ((nat = ipf_nat6_inlookup(fin, nflags|NAT_SEARCH,
3021 (u_int)fin->fin_p,
3022 &fin->fin_src6.in6, &ipa.in6))) {
3023 nflags = nat->nat_flags;
3024 } else if (fin->fin_off == 0) {
3025 u_32_t hv, rmsk = 0;
3026 i6addr_t *msk;
3027
3028 /*
3029 * If there is no current entry in the nat table for this IP#,
3030 * create one for it (if there is a matching rule).
3031 */
3032 maskloop:
3033 msk = &softn->ipf_nat6_rdr_active_masks[rmsk];
3034 IP6_AND(&ipa, msk, &iph);
3035 hv = NAT_HASH_FN6(&iph, 0, softn->ipf_nat_rdrrules_sz);
3036 for (np = softn->ipf_nat_rdr_rules[hv]; np; np = np->in_rnext) {
3037 if (np->in_ifps[0] && (np->in_ifps[0] != ifp))
3038 continue;
3039 if (np->in_v[0] != 6)
3040 continue;
3041 if (np->in_pr[0] && (np->in_pr[0] != fin->fin_p))
3042 continue;
3043 if ((np->in_flags & IPN_RF) && !(np->in_flags & nflags))
3044 continue;
3045 if (np->in_flags & IPN_FILTER) {
3046 switch (ipf_nat6_match(fin, np))
3047 {
3048 case 0 :
3049 continue;
3050 case -1 :
3051 rval = -1;
3052 goto inmatchfail;
3053 case 1 :
3054 default :
3055 break;
3056 }
3057 } else {
3058 if (!IP6_MASKEQ(&ipa, &np->in_odstmsk6,
3059 &np->in_odstip6)) {
3060 continue;
3061 }
3062 if (np->in_odport &&
3063 ((np->in_dtop < dport) ||
3064 (dport < np->in_odport)))
3065 continue;
3066 }
3067
3068 #ifdef IPF_V6_PROXIES
3069 if (np->in_plabel != -1) {
3070 if (!appr_ok(fin, tcp, np)) {
3071 continue;
3072 }
3073 }
3074 #endif
3075
3076 if (np->in_flags & IPN_NO) {
3077 np->in_hits++;
3078 break;
3079 }
3080
3081 MUTEX_ENTER(&softn->ipf_nat_new);
3082 nat = ipf_nat6_add(fin, np, NULL, nflags, NAT_INBOUND);
3083 MUTEX_EXIT(&softn->ipf_nat_new);
3084 if (nat != NULL) {
3085 np->in_hits++;
3086 break;
3087 }
3088 natfailed = -1;
3089 }
3090
3091 if ((np == NULL) && (rmsk < softn->ipf_nat6_rdr_max)) {
3092 rmsk++;
3093 goto maskloop;
3094 }
3095 }
3096 if (nat != NULL) {
3097 rval = ipf_nat6_in(fin, nat, natadd, nflags);
3098 if (rval == 1) {
3099 MUTEX_ENTER(&nat->nat_lock);
3100 ipf_nat_update(fin, nat);
3101 nat->nat_bytes[0] += fin->fin_plen;
3102 nat->nat_pkts[0]++;
3103 MUTEX_EXIT(&nat->nat_lock);
3104 }
3105 } else
3106 rval = natfailed;
3107 inmatchfail:
3108 RWLOCK_EXIT(&softc->ipf_nat);
3109
3110 DT2(frb_natv6in, fr_info_t *, fin, int, rval);
3111 switch (rval)
3112 {
3113 case -1 :
3114 if (passp != NULL) {
3115 NBUMPSIDE6D(0, ns_drop);
3116 *passp = FR_BLOCK;
3117 fin->fin_reason = FRB_NATV6;
3118 }
3119 fin->fin_flx |= FI_BADNAT;
3120 NBUMPSIDE6D(0, ns_badnat);
3121 break;
3122 case 0 :
3123 NBUMPSIDE6D(0, ns_ignored);
3124 break;
3125 case 1 :
3126 NBUMPSIDE6D(0, ns_translated);
3127 break;
3128 }
3129 return (rval);
3130 }
3131
3132
3133 /* ------------------------------------------------------------------------ */
3134 /* Function: ipf_nat6_in */
3135 /* Returns: int - -1 == packet failed NAT checks so block it, */
3136 /* 1 == packet was successfully translated. */
3137 /* Parameters: fin(I) - pointer to packet information */
3138 /* nat(I) - pointer to NAT structure */
3139 /* natadd(I) - flag indicating if it is safe to add frag cache */
3140 /* nflags(I) - NAT flags set for this packet */
3141 /* Locks Held: (READ) */
3142 /* */
3143 /* Translate a packet coming "in" on an interface. */
3144 /* ------------------------------------------------------------------------ */
3145 static int
ipf_nat6_in(fr_info_t * fin,nat_t * nat,int natadd,u_32_t nflags)3146 ipf_nat6_in(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
3147 {
3148 ipf_main_softc_t *softc = fin->fin_main_soft;
3149 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3150 struct icmp6_hdr *icmp6;
3151 u_short *csump;
3152 tcphdr_t *tcp;
3153 ipnat_t *np;
3154 int skip;
3155 int i;
3156
3157 tcp = NULL;
3158 csump = NULL;
3159 np = nat->nat_ptr;
3160 fin->fin_fr = nat->nat_fr;
3161
3162 if (np != NULL) {
3163 if ((natadd != 0) && (fin->fin_flx & FI_FRAG))
3164 (void) ipf_frag_natnew(softc, fin, 0, nat);
3165
3166 /* ------------------------------------------------------------- */
3167 /* A few quick notes: */
3168 /* Following are test conditions prior to calling the */
3169 /* ipf_proxy_check routine. */
3170 /* */
3171 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */
3172 /* with a map rule, we attempt to match the packet's */
3173 /* source port against in_dport, otherwise we'd compare the */
3174 /* packet's destination. */
3175 /* ------------------------------------------------------------- */
3176 if (np->in_apr != NULL) {
3177 i = ipf_proxy_check(fin, nat);
3178 if (i == -1) {
3179 NBUMPSIDE6D(0, ns_ipf_proxy_fail);
3180 return (-1);
3181 }
3182 }
3183 }
3184
3185 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync);
3186
3187 /*
3188 * Fix up checksums, not by recalculating them, but
3189 * simply computing adjustments.
3190 * Why only do this for some platforms on inbound packets ?
3191 * Because for those that it is done, IP processing is yet to happen
3192 * and so the IPv4 header checksum has not yet been evaluated.
3193 * Perhaps it should always be done for the benefit of things like
3194 * fast forwarding (so that it doesn't need to be recomputed) but with
3195 * header checksum offloading, perhaps it is a moot point.
3196 */
3197
3198 switch (nat->nat_dir)
3199 {
3200 case NAT_INBOUND :
3201 if ((fin->fin_flx & FI_ICMPERR) == 0) {
3202 fin->fin_ip6->ip6_src = nat->nat_nsrc6.in6;
3203 fin->fin_src6 = nat->nat_nsrc6;
3204 }
3205 fin->fin_ip6->ip6_dst = nat->nat_ndst6.in6;
3206 fin->fin_dst6 = nat->nat_ndst6;
3207 break;
3208
3209 case NAT_OUTBOUND :
3210 if ((fin->fin_flx & FI_ICMPERR) == 0) {
3211 fin->fin_ip6->ip6_src = nat->nat_odst6.in6;
3212 fin->fin_src6 = nat->nat_odst6;
3213 }
3214 fin->fin_ip6->ip6_dst = nat->nat_osrc6.in6;
3215 fin->fin_dst6 = nat->nat_osrc6;
3216 break;
3217
3218 case NAT_DIVERTIN :
3219 {
3220 udphdr_t *uh;
3221 ip6_t *ip6;
3222 mb_t *m;
3223
3224 m = M_DUP(np->in_divmp);
3225 if (m == NULL) {
3226 NBUMPSIDE6D(0, ns_divert_dup);
3227 return (-1);
3228 }
3229
3230 ip6 = MTOD(m, ip6_t *);
3231 ip6->ip6_plen = htons(fin->fin_plen + sizeof(udphdr_t));
3232
3233 uh = (udphdr_t *)(ip6 + 1);
3234 uh->uh_ulen = ntohs(fin->fin_plen);
3235
3236 PREP_MB_T(fin, m);
3237
3238 fin->fin_ip6 = ip6;
3239 fin->fin_plen += sizeof(ip6_t) + 8; /* UDP + new IPv6 hdr */
3240 fin->fin_dlen += sizeof(ip6_t) + 8; /* UDP + old IPv6 hdr */
3241
3242 nflags &= ~IPN_TCPUDPICMP;
3243
3244 break;
3245 }
3246
3247 case NAT_DIVERTOUT :
3248 {
3249 mb_t *m;
3250
3251 skip = ipf_nat6_decap(fin, nat);
3252 if (skip <= 0) {
3253 NBUMPSIDE6D(0, ns_decap_fail);
3254 return (-1);
3255 }
3256
3257 m = fin->fin_m;
3258
3259 #if SOLARIS && defined(_KERNEL)
3260 m->b_rptr += skip;
3261 #else
3262 m->m_data += skip;
3263 m->m_len -= skip;
3264
3265 # ifdef M_PKTHDR
3266 if (m->m_flags & M_PKTHDR)
3267 m->m_pkthdr.len -= skip;
3268 # endif
3269 #endif
3270
3271 ipf_nat_update(fin, nat);
3272 fin->fin_flx |= FI_NATED;
3273 if (np != NULL && np->in_tag.ipt_num[0] != 0)
3274 fin->fin_nattag = &np->in_tag;
3275 return (1);
3276 /* NOTREACHED */
3277 }
3278 }
3279 if (nflags & IPN_TCPUDP)
3280 tcp = fin->fin_dp;
3281
3282 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) {
3283 if ((nat->nat_odport != 0) && (nflags & IPN_TCPUDP)) {
3284 switch (nat->nat_dir)
3285 {
3286 case NAT_INBOUND :
3287 tcp->th_sport = nat->nat_nsport;
3288 fin->fin_data[0] = ntohs(nat->nat_nsport);
3289 tcp->th_dport = nat->nat_ndport;
3290 fin->fin_data[1] = ntohs(nat->nat_ndport);
3291 break;
3292
3293 case NAT_OUTBOUND :
3294 tcp->th_sport = nat->nat_odport;
3295 fin->fin_data[0] = ntohs(nat->nat_odport);
3296 tcp->th_dport = nat->nat_osport;
3297 fin->fin_data[1] = ntohs(nat->nat_osport);
3298 break;
3299 }
3300 }
3301
3302
3303 if ((nat->nat_odport != 0) && (nflags & IPN_ICMPQUERY)) {
3304 icmp6 = fin->fin_dp;
3305
3306 icmp6->icmp6_id = nat->nat_nicmpid;
3307 }
3308
3309 csump = ipf_nat_proto(fin, nat, nflags);
3310 }
3311
3312 /*
3313 * The above comments do not hold for layer 4 (or higher) checksums...
3314 */
3315 if (csump != NULL) {
3316 if (nat->nat_dir == NAT_OUTBOUND)
3317 ipf_fix_incksum(0, csump, nat->nat_sumd[0], 0);
3318 else
3319 ipf_fix_outcksum(0, csump, nat->nat_sumd[0], 0);
3320 }
3321 fin->fin_flx |= FI_NATED;
3322 if (np != NULL && np->in_tag.ipt_num[0] != 0)
3323 fin->fin_nattag = &np->in_tag;
3324 return (1);
3325 }
3326
3327
3328 /* ------------------------------------------------------------------------ */
3329 /* Function: ipf_nat6_newrewrite */
3330 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */
3331 /* allow rule to be moved if IPN_ROUNDR is set. */
3332 /* Parameters: fin(I) - pointer to packet information */
3333 /* nat(I) - pointer to NAT entry */
3334 /* ni(I) - pointer to structure with misc. information needed */
3335 /* to create new NAT entry. */
3336 /* Write Lock: ipf_nat */
3337 /* */
3338 /* This function is responsible for setting up an active NAT session where */
3339 /* we are changing both the source and destination parameters at the same */
3340 /* time. The loop in here works differently to elsewhere - each iteration */
3341 /* is responsible for changing a single parameter that can be incremented. */
3342 /* So one pass may increase the source IP#, next source port, next dest. IP#*/
3343 /* and the last destination port for a total of 4 iterations to try each. */
3344 /* This is done to try and exhaustively use the translation space available.*/
3345 /* ------------------------------------------------------------------------ */
3346 int
ipf_nat6_newrewrite(fr_info_t * fin,nat_t * nat,natinfo_t * nai)3347 ipf_nat6_newrewrite(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
3348 {
3349 int src_search = 1;
3350 int dst_search = 1;
3351 fr_info_t frnat;
3352 u_32_t flags;
3353 u_short swap;
3354 ipnat_t *np;
3355 nat_t *natl;
3356 int l = 0;
3357 int changed;
3358
3359 natl = NULL;
3360 changed = -1;
3361 np = nai->nai_np;
3362 flags = nat->nat_flags;
3363 bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
3364
3365 nat->nat_hm = NULL;
3366
3367 do {
3368 changed = -1;
3369 /* TRACE (l, src_search, dst_search, np) */
3370
3371 if ((src_search == 0) && (np->in_spnext == 0) &&
3372 (dst_search == 0) && (np->in_dpnext == 0)) {
3373 if (l > 0)
3374 return (-1);
3375 }
3376
3377 /*
3378 * Find a new source address
3379 */
3380 if (ipf_nat6_nextaddr(fin, &np->in_nsrc, &frnat.fin_src6,
3381 &frnat.fin_src6) == -1) {
3382 return (-1);
3383 }
3384
3385 if (IP6_ISZERO(&np->in_nsrcip6) &&
3386 IP6_ISONES(&np->in_nsrcmsk6)) {
3387 src_search = 0;
3388 if (np->in_stepnext == 0)
3389 np->in_stepnext = 1;
3390
3391 } else if (IP6_ISZERO(&np->in_nsrcip6) &&
3392 IP6_ISZERO(&np->in_nsrcmsk6)) {
3393 src_search = 0;
3394 if (np->in_stepnext == 0)
3395 np->in_stepnext = 1;
3396
3397 } else if (IP6_ISONES(&np->in_nsrcmsk)) {
3398 src_search = 0;
3399 if (np->in_stepnext == 0)
3400 np->in_stepnext = 1;
3401
3402 } else if (!IP6_ISONES(&np->in_nsrcmsk6)) {
3403 if (np->in_stepnext == 0 && changed == -1) {
3404 IP6_INC(&np->in_snip);
3405 np->in_stepnext++;
3406 changed = 0;
3407 }
3408 }
3409
3410 if ((flags & IPN_TCPUDPICMP) != 0) {
3411 if (np->in_spnext != 0)
3412 frnat.fin_data[0] = np->in_spnext;
3413
3414 /*
3415 * Standard port translation. Select next port.
3416 */
3417 if ((flags & IPN_FIXEDSPORT) != 0) {
3418 np->in_stepnext = 2;
3419 } else if ((np->in_stepnext == 1) &&
3420 (changed == -1) && (natl != NULL)) {
3421 np->in_spnext++;
3422 np->in_stepnext++;
3423 changed = 1;
3424 if (np->in_spnext > np->in_spmax)
3425 np->in_spnext = np->in_spmin;
3426 }
3427 } else {
3428 np->in_stepnext = 2;
3429 }
3430 np->in_stepnext &= 0x3;
3431
3432 /*
3433 * Find a new destination address
3434 */
3435 /* TRACE (fin, np, l, frnat) */
3436
3437 if (ipf_nat6_nextaddr(fin, &np->in_ndst, &frnat.fin_dst6,
3438 &frnat.fin_dst6) == -1)
3439 return (-1);
3440
3441 if (IP6_ISZERO(&np->in_ndstip6) &&
3442 IP6_ISONES(&np->in_ndstmsk6)) {
3443 dst_search = 0;
3444 if (np->in_stepnext == 2)
3445 np->in_stepnext = 3;
3446
3447 } else if (IP6_ISZERO(&np->in_ndstip6) &&
3448 IP6_ISZERO(&np->in_ndstmsk6)) {
3449 dst_search = 0;
3450 if (np->in_stepnext == 2)
3451 np->in_stepnext = 3;
3452
3453 } else if (IP6_ISONES(&np->in_ndstmsk6)) {
3454 dst_search = 0;
3455 if (np->in_stepnext == 2)
3456 np->in_stepnext = 3;
3457
3458 } else if (!IP6_ISONES(&np->in_ndstmsk6)) {
3459 if ((np->in_stepnext == 2) && (changed == -1) &&
3460 (natl != NULL)) {
3461 changed = 2;
3462 np->in_stepnext++;
3463 IP6_INC(&np->in_dnip6);
3464 }
3465 }
3466
3467 if ((flags & IPN_TCPUDPICMP) != 0) {
3468 if (np->in_dpnext != 0)
3469 frnat.fin_data[1] = np->in_dpnext;
3470
3471 /*
3472 * Standard port translation. Select next port.
3473 */
3474 if ((flags & IPN_FIXEDDPORT) != 0) {
3475 np->in_stepnext = 0;
3476 } else if (np->in_stepnext == 3 && changed == -1) {
3477 np->in_dpnext++;
3478 np->in_stepnext++;
3479 changed = 3;
3480 if (np->in_dpnext > np->in_dpmax)
3481 np->in_dpnext = np->in_dpmin;
3482 }
3483 } else {
3484 if (np->in_stepnext == 3)
3485 np->in_stepnext = 0;
3486 }
3487
3488 /* TRACE (frnat) */
3489
3490 /*
3491 * Here we do a lookup of the connection as seen from
3492 * the outside. If an IP# pair already exists, try
3493 * again. So if you have A->B becomes C->B, you can
3494 * also have D->E become C->E but not D->B causing
3495 * another C->B. Also take protocol and ports into
3496 * account when determining whether a pre-existing
3497 * NAT setup will cause an external conflict where
3498 * this is appropriate.
3499 *
3500 * fin_data[] is swapped around because we are doing a
3501 * lookup of the packet is if it were moving in the opposite
3502 * direction of the one we are working with now.
3503 */
3504 if (flags & IPN_TCPUDP) {
3505 swap = frnat.fin_data[0];
3506 frnat.fin_data[0] = frnat.fin_data[1];
3507 frnat.fin_data[1] = swap;
3508 }
3509 if (fin->fin_out == 1) {
3510 natl = ipf_nat6_inlookup(&frnat,
3511 flags & ~(SI_WILDP|NAT_SEARCH),
3512 (u_int)frnat.fin_p,
3513 &frnat.fin_dst6.in6,
3514 &frnat.fin_src6.in6);
3515
3516 } else {
3517 natl = ipf_nat6_outlookup(&frnat,
3518 flags & ~(SI_WILDP|NAT_SEARCH),
3519 (u_int)frnat.fin_p,
3520 &frnat.fin_dst6.in6,
3521 &frnat.fin_src6.in6);
3522 }
3523 if (flags & IPN_TCPUDP) {
3524 swap = frnat.fin_data[0];
3525 frnat.fin_data[0] = frnat.fin_data[1];
3526 frnat.fin_data[1] = swap;
3527 }
3528
3529 /* TRACE natl, in_stepnext, l */
3530
3531 if ((natl != NULL) && (l > 8)) /* XXX 8 is arbitrary */
3532 return (-1);
3533
3534 np->in_stepnext &= 0x3;
3535
3536 l++;
3537 changed = -1;
3538 } while (natl != NULL);
3539 nat->nat_osrc6 = fin->fin_src6;
3540 nat->nat_odst6 = fin->fin_dst6;
3541 nat->nat_nsrc6 = frnat.fin_src6;
3542 nat->nat_ndst6 = frnat.fin_dst6;
3543
3544 if ((flags & IPN_TCPUDP) != 0) {
3545 nat->nat_osport = htons(fin->fin_data[0]);
3546 nat->nat_odport = htons(fin->fin_data[1]);
3547 nat->nat_nsport = htons(frnat.fin_data[0]);
3548 nat->nat_ndport = htons(frnat.fin_data[1]);
3549 } else if ((flags & IPN_ICMPQUERY) != 0) {
3550 nat->nat_oicmpid = fin->fin_data[1];
3551 nat->nat_nicmpid = frnat.fin_data[1];
3552 }
3553
3554 return (0);
3555 }
3556
3557
3558 /* ------------------------------------------------------------------------ */
3559 /* Function: ipf_nat6_newdivert */
3560 /* Returns: int - -1 == error, 0 == success */
3561 /* Parameters: fin(I) - pointer to packet information */
3562 /* nat(I) - pointer to NAT entry */
3563 /* ni(I) - pointer to structure with misc. information needed */
3564 /* to create new NAT entry. */
3565 /* Write Lock: ipf_nat */
3566 /* */
3567 /* Create a new NAT divert session as defined by the NAT rule. This is */
3568 /* somewhat different to other NAT session creation routines because we */
3569 /* do not iterate through either port numbers or IP addresses, searching */
3570 /* for a unique mapping, however, a complimentary duplicate check is made. */
3571 /* ------------------------------------------------------------------------ */
3572 int
ipf_nat6_newdivert(fr_info_t * fin,nat_t * nat,natinfo_t * nai)3573 ipf_nat6_newdivert(fr_info_t *fin, nat_t *nat, natinfo_t *nai)
3574 {
3575 ipf_main_softc_t *softc = fin->fin_main_soft;
3576 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3577 fr_info_t frnat;
3578 ipnat_t *np;
3579 nat_t *natl;
3580 int p;
3581
3582 np = nai->nai_np;
3583 bcopy((char *)fin, (char *)&frnat, sizeof(*fin));
3584
3585 nat->nat_pr[0] = 0;
3586 nat->nat_osrc6 = fin->fin_src6;
3587 nat->nat_odst6 = fin->fin_dst6;
3588 nat->nat_osport = htons(fin->fin_data[0]);
3589 nat->nat_odport = htons(fin->fin_data[1]);
3590 frnat.fin_src6 = np->in_snip6;
3591 frnat.fin_dst6 = np->in_dnip6;
3592
3593 if (np->in_redir & NAT_DIVERTUDP) {
3594 frnat.fin_data[0] = np->in_spnext;
3595 frnat.fin_data[1] = np->in_dpnext;
3596 frnat.fin_flx |= FI_TCPUDP;
3597 p = IPPROTO_UDP;
3598 } else {
3599 frnat.fin_flx &= ~FI_TCPUDP;
3600 p = IPPROTO_IPIP;
3601 }
3602
3603 if (fin->fin_out == 1) {
3604 natl = ipf_nat6_inlookup(&frnat, 0, p, &frnat.fin_dst6.in6,
3605 &frnat.fin_src6.in6);
3606
3607 } else {
3608 natl = ipf_nat6_outlookup(&frnat, 0, p, &frnat.fin_dst6.in6,
3609 &frnat.fin_src6.in6);
3610 }
3611
3612 if (natl != NULL) {
3613 NBUMPSIDE6D(fin->fin_out, ns_divert_exist);
3614 return (-1);
3615 }
3616
3617 nat->nat_nsrc6 = frnat.fin_src6;
3618 nat->nat_ndst6 = frnat.fin_dst6;
3619 if (np->in_redir & NAT_DIVERTUDP) {
3620 nat->nat_nsport = htons(frnat.fin_data[0]);
3621 nat->nat_ndport = htons(frnat.fin_data[1]);
3622 }
3623 nat->nat_pr[fin->fin_out] = fin->fin_p;
3624 nat->nat_pr[1 - fin->fin_out] = p;
3625
3626 if (np->in_redir & NAT_REDIRECT)
3627 nat->nat_dir = NAT_DIVERTIN;
3628 else
3629 nat->nat_dir = NAT_DIVERTOUT;
3630
3631 return (0);
3632 }
3633
3634
3635 /* ------------------------------------------------------------------------ */
3636 /* Function: nat6_builddivertmp */
3637 /* Returns: int - -1 == error, 0 == success */
3638 /* Parameters: np(I) - pointer to a NAT rule */
3639 /* */
3640 /* For divert rules, a skeleton packet representing what will be prepended */
3641 /* to the real packet is created. Even though we don't have the full */
3642 /* packet here, a checksum is calculated that we update later when we */
3643 /* fill in the final details. At present a 0 checksum for UDP is being set */
3644 /* here because it is expected that divert will be used for localhost. */
3645 /* ------------------------------------------------------------------------ */
3646 static int
ipf_nat6_builddivertmp(ipf_nat_softc_t * softn,ipnat_t * np)3647 ipf_nat6_builddivertmp(ipf_nat_softc_t *softn, ipnat_t *np)
3648 {
3649 udphdr_t *uh;
3650 size_t len;
3651 ip6_t *ip6;
3652
3653 if ((np->in_redir & NAT_DIVERTUDP) != 0)
3654 len = sizeof(ip6_t) + sizeof(udphdr_t);
3655 else
3656 len = sizeof(ip6_t);
3657
3658 ALLOC_MB_T(np->in_divmp, len);
3659 if (np->in_divmp == NULL) {
3660 ATOMIC_INCL(softn->ipf_nat_stats.ns_divert_build);
3661 return (-1);
3662 }
3663
3664 /*
3665 * First, the header to get the packet diverted to the new destination
3666 */
3667 ip6 = MTOD(np->in_divmp, ip6_t *);
3668 ip6->ip6_vfc = 0x60;
3669 if ((np->in_redir & NAT_DIVERTUDP) != 0)
3670 ip6->ip6_nxt = IPPROTO_UDP;
3671 else
3672 ip6->ip6_nxt = IPPROTO_IPIP;
3673 ip6->ip6_hlim = 255;
3674 ip6->ip6_plen = 0;
3675 ip6->ip6_src = np->in_snip6.in6;
3676 ip6->ip6_dst = np->in_dnip6.in6;
3677
3678 if (np->in_redir & NAT_DIVERTUDP) {
3679 uh = (udphdr_t *)((u_char *)ip6 + sizeof(*ip6));
3680 uh->uh_sum = 0;
3681 uh->uh_ulen = 8;
3682 uh->uh_sport = htons(np->in_spnext);
3683 uh->uh_dport = htons(np->in_dpnext);
3684 }
3685
3686 return (0);
3687 }
3688
3689
3690 #define MINDECAP (sizeof(ip6_t) + sizeof(udphdr_t) + sizeof(ip6_t))
3691
3692 /* ------------------------------------------------------------------------ */
3693 /* Function: nat6_decap */
3694 /* Returns: int - -1 == error, 0 == success */
3695 /* Parameters: fin(I) - pointer to packet information */
3696 /* nat(I) - pointer to current NAT session */
3697 /* */
3698 /* This function is responsible for undoing a packet's encapsulation in the */
3699 /* reverse of an encap/divert rule. After removing the outer encapsulation */
3700 /* it is necessary to call ipf_makefrip() again so that the contents of 'fin'*/
3701 /* match the "new" packet as it may still be used by IPFilter elsewhere. */
3702 /* We use "dir" here as the basis for some of the expectations about the */
3703 /* outer header. If we return an error, the goal is to leave the original */
3704 /* packet information undisturbed - this falls short at the end where we'd */
3705 /* need to back a backup copy of "fin" - expensive. */
3706 /* ------------------------------------------------------------------------ */
3707 static int
ipf_nat6_decap(fr_info_t * fin,nat_t * nat)3708 ipf_nat6_decap(fr_info_t *fin, nat_t *nat)
3709 {
3710 ipf_main_softc_t *softc = fin->fin_main_soft;
3711 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3712 char *hdr;
3713 int skip;
3714 mb_t *m;
3715
3716 if ((fin->fin_flx & FI_ICMPERR) != 0) {
3717 return (0);
3718 }
3719
3720 m = fin->fin_m;
3721 skip = fin->fin_hlen;
3722
3723 switch (nat->nat_dir)
3724 {
3725 case NAT_DIVERTIN :
3726 case NAT_DIVERTOUT :
3727 if (fin->fin_plen < MINDECAP)
3728 return (-1);
3729 skip += sizeof(udphdr_t);
3730 break;
3731
3732 case NAT_ENCAPIN :
3733 case NAT_ENCAPOUT :
3734 if (fin->fin_plen < (skip + sizeof(ip6_t)))
3735 return (-1);
3736 break;
3737 default :
3738 return (-1);
3739 /* NOTREACHED */
3740 }
3741
3742 /*
3743 * The aim here is to keep the original packet details in "fin" for
3744 * as long as possible so that returning with an error is for the
3745 * original packet and there is little undoing work to do.
3746 */
3747 if (M_LEN(m) < skip + sizeof(ip6_t)) {
3748 if (ipf_pr_pullup(fin, skip + sizeof(ip6_t)) == -1)
3749 return (-1);
3750 }
3751
3752 hdr = MTOD(fin->fin_m, char *);
3753 fin->fin_ip6 = (ip6_t *)(hdr + skip);
3754
3755 if (ipf_pr_pullup(fin, skip + sizeof(ip6_t)) == -1) {
3756 NBUMPSIDE6D(fin->fin_out, ns_decap_pullup);
3757 return (-1);
3758 }
3759
3760 fin->fin_hlen = sizeof(ip6_t);
3761 fin->fin_dlen -= skip;
3762 fin->fin_plen -= skip;
3763 fin->fin_ipoff += skip;
3764
3765 if (ipf_makefrip(sizeof(ip6_t), (ip_t *)hdr, fin) == -1) {
3766 NBUMPSIDE6D(fin->fin_out, ns_decap_bad);
3767 return (-1);
3768 }
3769
3770 return (skip);
3771 }
3772
3773
3774 /* ------------------------------------------------------------------------ */
3775 /* Function: nat6_nextaddr */
3776 /* Returns: int - -1 == bad input (no new address), */
3777 /* 0 == success and dst has new address */
3778 /* Parameters: fin(I) - pointer to packet information */
3779 /* na(I) - how to generate new address */
3780 /* old(I) - original address being replaced */
3781 /* dst(O) - where to put the new address */
3782 /* Write Lock: ipf_nat */
3783 /* */
3784 /* This function uses the contents of the "na" structure, in combination */
3785 /* with "old" to produce a new address to store in "dst". Not all of the */
3786 /* possible uses of "na" will result in a new address. */
3787 /* ------------------------------------------------------------------------ */
3788 static int
ipf_nat6_nextaddr(fr_info_t * fin,nat_addr_t * na,i6addr_t * old,i6addr_t * dst)3789 ipf_nat6_nextaddr(fr_info_t *fin, nat_addr_t *na, i6addr_t *old, i6addr_t *dst)
3790 {
3791 ipf_main_softc_t *softc = fin->fin_main_soft;
3792 ipf_nat_softc_t *softn = softc->ipf_nat_soft;
3793 i6addr_t newip, new;
3794 int error;
3795
3796 new.i6[0] = 0;
3797 new.i6[1] = 0;
3798 new.i6[2] = 0;
3799 new.i6[3] = 0;
3800
3801 switch (na->na_atype)
3802 {
3803 case FRI_RANGE :
3804 case FRI_NETMASKED :
3805 case FRI_DYNAMIC :
3806 case FRI_NORMAL :
3807 case FRI_LOOKUP :
3808 break;
3809
3810 case FRI_BROADCAST :
3811 case FRI_PEERADDR :
3812 case FRI_NETWORK :
3813 default :
3814 return (-1);
3815 }
3816
3817 error = -1;
3818 switch (na->na_function)
3819 {
3820 case IPLT_DSTLIST :
3821 error = ipf_dstlist_select_node(fin, na->na_ptr, dst->i6,
3822 NULL);
3823 break;
3824
3825 case IPLT_NONE :
3826 /*
3827 * 0/0 as the new address means leave it alone.
3828 */
3829 if (na->na_addr[0].in4.s_addr == 0 &&
3830 na->na_addr[1].in4.s_addr == 0) {
3831 new = *old;
3832
3833 /*
3834 * 0/32 means get the interface's address
3835 */
3836 } else if (IP6_ISZERO(&na->na_addr[0].in6) &&
3837 IP6_ISONES(&na->na_addr[1].in6)) {
3838 if (ipf_ifpaddr(softc, 6, na->na_atype,
3839 fin->fin_ifp, &newip, NULL) == -1) {
3840 NBUMPSIDE6(fin->fin_out, ns_ifpaddrfail);
3841 return (-1);
3842 }
3843 new = newip;
3844 } else {
3845 new.in6 = na->na_nextip6;
3846 }
3847 *dst = new;
3848 error = 0;
3849 break;
3850
3851 default :
3852 NBUMPSIDE6(fin->fin_out, ns_badnextaddr);
3853 break;
3854 }
3855
3856 return (error);
3857 }
3858
3859
3860 /* ------------------------------------------------------------------------ */
3861 /* Function: ipf_nat6_nextaddrinit */
3862 /* Returns: int - 0 == success, else error number */
3863 /* Parameters: na(I) - NAT address information for generating new addr*/
3864 /* base(I) - start of where to find strings */
3865 /* initial(I) - flag indicating if it is the first call for */
3866 /* this "na" structure. */
3867 /* ifp(I) - network interface to derive address */
3868 /* information from. */
3869 /* */
3870 /* This function is expected to be called in two scenarious: when a new NAT */
3871 /* rule is loaded into the kernel and when the list of NAT rules is sync'd */
3872 /* up with the valid network interfaces (possibly due to them changing.) */
3873 /* To distinguish between these, the "initial" parameter is used. If it is */
3874 /* 1 then this indicates the rule has just been reloaded and 0 for when we */
3875 /* are updating information. This difference is important because in */
3876 /* instances where we are not updating address information associated with */
3877 /* a network interface, we don't want to disturb what the "next" address to */
3878 /* come out of ipf_nat6_nextaddr() will be. */
3879 /* ------------------------------------------------------------------------ */
3880 static int
ipf_nat6_nextaddrinit(ipf_main_softc_t * softc,char * base,nat_addr_t * na,int initial,void * ifp)3881 ipf_nat6_nextaddrinit(ipf_main_softc_t *softc, char *base, nat_addr_t *na,
3882 int initial, void *ifp)
3883 {
3884 switch (na->na_atype)
3885 {
3886 case FRI_LOOKUP :
3887 if (na->na_subtype == 0) {
3888 na->na_ptr = ipf_lookup_res_num(softc, IPL_LOGNAT,
3889 na->na_type,
3890 na->na_num,
3891 &na->na_func);
3892 } else if (na->na_subtype == 1) {
3893 na->na_ptr = ipf_lookup_res_name(softc, IPL_LOGNAT,
3894 na->na_type,
3895 base + na->na_num,
3896 &na->na_func);
3897 }
3898 if (na->na_func == NULL) {
3899 IPFERROR(60072);
3900 return (ESRCH);
3901 }
3902 if (na->na_ptr == NULL) {
3903 IPFERROR(60073);
3904 return (ESRCH);
3905 }
3906 break;
3907 case FRI_DYNAMIC :
3908 case FRI_BROADCAST :
3909 case FRI_NETWORK :
3910 case FRI_NETMASKED :
3911 case FRI_PEERADDR :
3912 if (ifp != NULL)
3913 (void )ipf_ifpaddr(softc, 6, na->na_atype, ifp,
3914 &na->na_addr[0],
3915 &na->na_addr[1]);
3916 break;
3917
3918 case FRI_SPLIT :
3919 case FRI_RANGE :
3920 if (initial)
3921 na->na_nextip6 = na->na_addr[0].in6;
3922 break;
3923
3924 case FRI_NONE :
3925 IP6_ANDASSIGN(&na->na_addr[0].in6, &na->na_addr[1].in6);
3926 return (0);
3927
3928 case FRI_NORMAL :
3929 IP6_ANDASSIGN(&na->na_addr[0].in6, &na->na_addr[1].in6);
3930 break;
3931
3932 default :
3933 IPFERROR(60074);
3934 return (EINVAL);
3935 }
3936
3937 if (initial && (na->na_atype == FRI_NORMAL)) {
3938 if (IP6_ISZERO(&na->na_addr[0].in6)) {
3939 if (IP6_ISONES(&na->na_addr[1].in6) ||
3940 IP6_ISZERO(&na->na_addr[1].in6)) {
3941 return (0);
3942 }
3943 }
3944
3945 na->na_nextip6 = na->na_addr[0].in6;
3946 if (!IP6_ISONES(&na->na_addr[1].in6)) {
3947 IP6_INC(&na->na_nextip6);
3948 }
3949 }
3950
3951 return (0);
3952 }
3953
3954
3955 /* ------------------------------------------------------------------------ */
3956 /* Function: ipf_nat6_icmpquerytype */
3957 /* Returns: int - 1 == success, 0 == failure */
3958 /* Parameters: icmptype(I) - ICMP type number */
3959 /* */
3960 /* Tests to see if the ICMP type number passed is a query/response type or */
3961 /* not. */
3962 /* ------------------------------------------------------------------------ */
3963 static int
ipf_nat6_icmpquerytype(int icmptype)3964 ipf_nat6_icmpquerytype(int icmptype)
3965 {
3966
3967 /*
3968 * For the ICMP query NAT code, it is essential that both the query
3969 * and the reply match on the NAT rule. Because the NAT structure
3970 * does not keep track of the icmptype, and a single NAT structure
3971 * is used for all icmp types with the same src, dest and id, we
3972 * simply define the replies as queries as well. The funny thing is,
3973 * altough it seems silly to call a reply a query, this is exactly
3974 * as it is defined in the IPv4 specification
3975 */
3976
3977 switch (icmptype)
3978 {
3979
3980 case ICMP6_ECHO_REPLY:
3981 case ICMP6_ECHO_REQUEST:
3982 /* route aedvertisement/solliciation is currently unsupported: */
3983 /* it would require rewriting the ICMP data section */
3984 case ICMP6_MEMBERSHIP_QUERY:
3985 case ICMP6_MEMBERSHIP_REPORT:
3986 case ICMP6_MEMBERSHIP_REDUCTION:
3987 case ICMP6_WRUREQUEST:
3988 case ICMP6_WRUREPLY:
3989 case MLD6_MTRACE_RESP:
3990 case MLD6_MTRACE:
3991 return (1);
3992 default:
3993 return (0);
3994 }
3995 }
3996 #endif /* USE_INET6 */
3997