1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2001 Daniel Hartmeier
5 * Copyright (c) 2002 - 2008 Henning Brauer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Effort sponsored in part by the Defense Advanced Research Projects
33 * Agency (DARPA) and Air Force Research Laboratory, Air Force
34 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
35 *
36 * $OpenBSD: pf_lb.c,v 1.2 2009/02/12 02:13:15 sthen Exp $
37 */
38
39 #include <sys/cdefs.h>
40 #include "opt_pf.h"
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43
44 #include <sys/param.h>
45 #include <sys/lock.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49
50 #include <net/if.h>
51 #include <net/vnet.h>
52 #include <net/pfvar.h>
53 #include <net/if_pflog.h>
54
55 #define DPFPRINTF(n, x) if (V_pf_status.debug >= (n)) printf x
56
57 static void pf_hash(struct pf_addr *, struct pf_addr *,
58 struct pf_poolhashkey *, sa_family_t);
59 static struct pf_krule *pf_match_translation(struct pf_pdesc *, struct mbuf *,
60 int, int, struct pfi_kkif *,
61 struct pf_addr *, u_int16_t, struct pf_addr *,
62 uint16_t, int, struct pf_kanchor_stackframe *);
63 static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *,
64 struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *,
65 uint16_t *, uint16_t, uint16_t, struct pf_ksrc_node **);
66
67 #define mix(a,b,c) \
68 do { \
69 a -= b; a -= c; a ^= (c >> 13); \
70 b -= c; b -= a; b ^= (a << 8); \
71 c -= a; c -= b; c ^= (b >> 13); \
72 a -= b; a -= c; a ^= (c >> 12); \
73 b -= c; b -= a; b ^= (a << 16); \
74 c -= a; c -= b; c ^= (b >> 5); \
75 a -= b; a -= c; a ^= (c >> 3); \
76 b -= c; b -= a; b ^= (a << 10); \
77 c -= a; c -= b; c ^= (b >> 15); \
78 } while (0)
79
80 /*
81 * hash function based on bridge_hash in if_bridge.c
82 */
83 static void
pf_hash(struct pf_addr * inaddr,struct pf_addr * hash,struct pf_poolhashkey * key,sa_family_t af)84 pf_hash(struct pf_addr *inaddr, struct pf_addr *hash,
85 struct pf_poolhashkey *key, sa_family_t af)
86 {
87 u_int32_t a = 0x9e3779b9, b = 0x9e3779b9, c = key->key32[0];
88
89 switch (af) {
90 #ifdef INET
91 case AF_INET:
92 a += inaddr->addr32[0];
93 b += key->key32[1];
94 mix(a, b, c);
95 hash->addr32[0] = c + key->key32[2];
96 break;
97 #endif /* INET */
98 #ifdef INET6
99 case AF_INET6:
100 a += inaddr->addr32[0];
101 b += inaddr->addr32[2];
102 mix(a, b, c);
103 hash->addr32[0] = c;
104 a += inaddr->addr32[1];
105 b += inaddr->addr32[3];
106 c += key->key32[1];
107 mix(a, b, c);
108 hash->addr32[1] = c;
109 a += inaddr->addr32[2];
110 b += inaddr->addr32[1];
111 c += key->key32[2];
112 mix(a, b, c);
113 hash->addr32[2] = c;
114 a += inaddr->addr32[3];
115 b += inaddr->addr32[0];
116 c += key->key32[3];
117 mix(a, b, c);
118 hash->addr32[3] = c;
119 break;
120 #endif /* INET6 */
121 }
122 }
123
124 static struct pf_krule *
pf_match_translation(struct pf_pdesc * pd,struct mbuf * m,int off,int direction,struct pfi_kkif * kif,struct pf_addr * saddr,u_int16_t sport,struct pf_addr * daddr,uint16_t dport,int rs_num,struct pf_kanchor_stackframe * anchor_stack)125 pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off,
126 int direction, struct pfi_kkif *kif, struct pf_addr *saddr, u_int16_t sport,
127 struct pf_addr *daddr, uint16_t dport, int rs_num,
128 struct pf_kanchor_stackframe *anchor_stack)
129 {
130 struct pf_krule *r, *rm = NULL;
131 struct pf_kruleset *ruleset = NULL;
132 int tag = -1;
133 int rtableid = -1;
134 int asd = 0;
135
136 r = TAILQ_FIRST(pf_main_ruleset.rules[rs_num].active.ptr);
137 while (r && rm == NULL) {
138 struct pf_rule_addr *src = NULL, *dst = NULL;
139 struct pf_addr_wrap *xdst = NULL;
140
141 if (r->action == PF_BINAT && direction == PF_IN) {
142 src = &r->dst;
143 if (r->rpool.cur != NULL)
144 xdst = &r->rpool.cur->addr;
145 } else {
146 src = &r->src;
147 dst = &r->dst;
148 }
149
150 pf_counter_u64_add(&r->evaluations, 1);
151 if (pfi_kkif_match(r->kif, kif) == r->ifnot)
152 r = r->skip[PF_SKIP_IFP].ptr;
153 else if (r->direction && r->direction != direction)
154 r = r->skip[PF_SKIP_DIR].ptr;
155 else if (r->af && r->af != pd->af)
156 r = r->skip[PF_SKIP_AF].ptr;
157 else if (r->proto && r->proto != pd->proto)
158 r = r->skip[PF_SKIP_PROTO].ptr;
159 else if (PF_MISMATCHAW(&src->addr, saddr, pd->af,
160 src->neg, kif, M_GETFIB(m)))
161 r = r->skip[src == &r->src ? PF_SKIP_SRC_ADDR :
162 PF_SKIP_DST_ADDR].ptr;
163 else if (src->port_op && !pf_match_port(src->port_op,
164 src->port[0], src->port[1], sport))
165 r = r->skip[src == &r->src ? PF_SKIP_SRC_PORT :
166 PF_SKIP_DST_PORT].ptr;
167 else if (dst != NULL &&
168 PF_MISMATCHAW(&dst->addr, daddr, pd->af, dst->neg, NULL,
169 M_GETFIB(m)))
170 r = r->skip[PF_SKIP_DST_ADDR].ptr;
171 else if (xdst != NULL && PF_MISMATCHAW(xdst, daddr, pd->af,
172 0, NULL, M_GETFIB(m)))
173 r = TAILQ_NEXT(r, entries);
174 else if (dst != NULL && dst->port_op &&
175 !pf_match_port(dst->port_op, dst->port[0],
176 dst->port[1], dport))
177 r = r->skip[PF_SKIP_DST_PORT].ptr;
178 else if (r->match_tag && !pf_match_tag(m, r, &tag,
179 pd->pf_mtag ? pd->pf_mtag->tag : 0))
180 r = TAILQ_NEXT(r, entries);
181 else if (r->os_fingerprint != PF_OSFP_ANY && (pd->proto !=
182 IPPROTO_TCP || !pf_osfp_match(pf_osfp_fingerprint(pd, m,
183 off, &pd->hdr.tcp), r->os_fingerprint)))
184 r = TAILQ_NEXT(r, entries);
185 else {
186 if (r->tag)
187 tag = r->tag;
188 if (r->rtableid >= 0)
189 rtableid = r->rtableid;
190 if (r->anchor == NULL) {
191 rm = r;
192 } else
193 pf_step_into_anchor(anchor_stack, &asd,
194 &ruleset, rs_num, &r, NULL, NULL);
195 }
196 if (r == NULL)
197 pf_step_out_of_anchor(anchor_stack, &asd, &ruleset,
198 rs_num, &r, NULL, NULL);
199 }
200
201 if (tag > 0 && pf_tag_packet(m, pd, tag))
202 return (NULL);
203 if (rtableid >= 0)
204 M_SETFIB(m, rtableid);
205
206 if (rm != NULL && (rm->action == PF_NONAT ||
207 rm->action == PF_NORDR || rm->action == PF_NOBINAT))
208 return (NULL);
209 return (rm);
210 }
211
212 static int
pf_get_sport(sa_family_t af,u_int8_t proto,struct pf_krule * r,struct pf_addr * saddr,uint16_t sport,struct pf_addr * daddr,uint16_t dport,struct pf_addr * naddr,uint16_t * nport,uint16_t low,uint16_t high,struct pf_ksrc_node ** sn)213 pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
214 struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
215 uint16_t dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low,
216 uint16_t high, struct pf_ksrc_node **sn)
217 {
218 struct pf_state_key_cmp key;
219 struct pf_addr init_addr;
220
221 bzero(&init_addr, sizeof(init_addr));
222 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
223 return (1);
224
225 if (proto == IPPROTO_ICMP) {
226 if (*nport == htons(ICMP_ECHO)) {
227 low = 1;
228 high = 65535;
229 } else
230 return (0); /* Don't try to modify non-echo ICMP */
231 }
232 #ifdef INET6
233 if (proto == IPPROTO_ICMPV6) {
234 if (*nport == htons(ICMP6_ECHO_REQUEST)) {
235 low = 1;
236 high = 65535;
237 } else
238 return (0); /* Don't try to modify non-echo ICMP */
239 }
240 #endif /* INET6 */
241
242 bzero(&key, sizeof(key));
243 key.af = af;
244 key.proto = proto;
245 key.port[0] = dport;
246 PF_ACPY(&key.addr[0], daddr, key.af);
247
248 do {
249 PF_ACPY(&key.addr[1], naddr, key.af);
250
251 /*
252 * port search; start random, step;
253 * similar 2 portloop in in_pcbbind
254 */
255 if (proto == IPPROTO_SCTP) {
256 key.port[1] = sport;
257 if (!pf_find_state_all_exists(&key, PF_IN)) {
258 *nport = sport;
259 return (0);
260 } else {
261 return (1); /* Fail mapping. */
262 }
263 } else if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP ||
264 proto == IPPROTO_ICMP) || (low == 0 && high == 0)) {
265 /*
266 * XXX bug: icmp states don't use the id on both sides.
267 * (traceroute -I through nat)
268 */
269 key.port[1] = sport;
270 if (!pf_find_state_all_exists(&key, PF_IN)) {
271 *nport = sport;
272 return (0);
273 }
274 } else if (low == high) {
275 key.port[1] = htons(low);
276 if (!pf_find_state_all_exists(&key, PF_IN)) {
277 *nport = htons(low);
278 return (0);
279 }
280 } else {
281 uint32_t tmp;
282 uint16_t cut;
283
284 if (low > high) {
285 tmp = low;
286 low = high;
287 high = tmp;
288 }
289 /* low < high */
290 cut = arc4random() % (1 + high - low) + low;
291 /* low <= cut <= high */
292 for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) {
293 key.port[1] = htons(tmp);
294 if (!pf_find_state_all_exists(&key, PF_IN)) {
295 *nport = htons(tmp);
296 return (0);
297 }
298 }
299 tmp = cut;
300 for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) {
301 key.port[1] = htons(tmp);
302 if (!pf_find_state_all_exists(&key, PF_IN)) {
303 *nport = htons(tmp);
304 return (0);
305 }
306 }
307 }
308
309 switch (r->rpool.opts & PF_POOL_TYPEMASK) {
310 case PF_POOL_RANDOM:
311 case PF_POOL_ROUNDROBIN:
312 /*
313 * pick a different source address since we're out
314 * of free port choices for the current one.
315 */
316 if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn))
317 return (1);
318 break;
319 case PF_POOL_NONE:
320 case PF_POOL_SRCHASH:
321 case PF_POOL_BITMASK:
322 default:
323 return (1);
324 }
325 } while (! PF_AEQ(&init_addr, naddr, af) );
326 return (1); /* none available */
327 }
328
329 static int
pf_get_mape_sport(sa_family_t af,u_int8_t proto,struct pf_krule * r,struct pf_addr * saddr,uint16_t sport,struct pf_addr * daddr,uint16_t dport,struct pf_addr * naddr,uint16_t * nport,struct pf_ksrc_node ** sn)330 pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r,
331 struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr,
332 uint16_t dport, struct pf_addr *naddr, uint16_t *nport,
333 struct pf_ksrc_node **sn)
334 {
335 uint16_t psmask, low, highmask;
336 uint16_t i, ahigh, cut;
337 int ashift, psidshift;
338
339 ashift = 16 - r->rpool.mape.offset;
340 psidshift = ashift - r->rpool.mape.psidlen;
341 psmask = r->rpool.mape.psid & ((1U << r->rpool.mape.psidlen) - 1);
342 psmask = psmask << psidshift;
343 highmask = (1U << psidshift) - 1;
344
345 ahigh = (1U << r->rpool.mape.offset) - 1;
346 cut = arc4random() & ahigh;
347 if (cut == 0)
348 cut = 1;
349
350 for (i = cut; i <= ahigh; i++) {
351 low = (i << ashift) | psmask;
352 if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
353 naddr, nport, low, low | highmask, sn))
354 return (0);
355 }
356 for (i = cut - 1; i > 0; i--) {
357 low = (i << ashift) | psmask;
358 if (!pf_get_sport(af, proto, r, saddr, sport, daddr, dport,
359 naddr, nport, low, low | highmask, sn))
360 return (0);
361 }
362 return (1);
363 }
364
365 int
pf_map_addr(sa_family_t af,struct pf_krule * r,struct pf_addr * saddr,struct pf_addr * naddr,struct pf_addr * init_addr,struct pf_ksrc_node ** sn)366 pf_map_addr(sa_family_t af, struct pf_krule *r, struct pf_addr *saddr,
367 struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_ksrc_node **sn)
368 {
369 struct pf_kpool *rpool = &r->rpool;
370 struct pf_addr *raddr = NULL, *rmask = NULL;
371
372 /* Try to find a src_node if none was given and this
373 is a sticky-address rule. */
374 if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR &&
375 (r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE)
376 *sn = pf_find_src_node(saddr, r, af, 0);
377
378 /* If a src_node was found or explicitly given and it has a non-zero
379 route address, use this address. A zeroed address is found if the
380 src node was created just a moment ago in pf_create_state and it
381 needs to be filled in with routing decision calculated here. */
382 if (*sn != NULL && !PF_AZERO(&(*sn)->raddr, af)) {
383 /* If the supplied address is the same as the current one we've
384 * been asked before, so tell the caller that there's no other
385 * address to be had. */
386 if (PF_AEQ(naddr, &(*sn)->raddr, af))
387 return (1);
388
389 PF_ACPY(naddr, &(*sn)->raddr, af);
390 if (V_pf_status.debug >= PF_DEBUG_MISC) {
391 printf("pf_map_addr: src tracking maps ");
392 pf_print_host(saddr, 0, af);
393 printf(" to ");
394 pf_print_host(naddr, 0, af);
395 printf("\n");
396 }
397 return (0);
398 }
399
400 mtx_lock(&rpool->mtx);
401 /* Find the route using chosen algorithm. Store the found route
402 in src_node if it was given or found. */
403 if (rpool->cur->addr.type == PF_ADDR_NOROUTE) {
404 mtx_unlock(&rpool->mtx);
405 return (1);
406 }
407 if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
408 switch (af) {
409 #ifdef INET
410 case AF_INET:
411 if (rpool->cur->addr.p.dyn->pfid_acnt4 < 1 &&
412 (rpool->opts & PF_POOL_TYPEMASK) !=
413 PF_POOL_ROUNDROBIN) {
414 mtx_unlock(&rpool->mtx);
415 return (1);
416 }
417 raddr = &rpool->cur->addr.p.dyn->pfid_addr4;
418 rmask = &rpool->cur->addr.p.dyn->pfid_mask4;
419 break;
420 #endif /* INET */
421 #ifdef INET6
422 case AF_INET6:
423 if (rpool->cur->addr.p.dyn->pfid_acnt6 < 1 &&
424 (rpool->opts & PF_POOL_TYPEMASK) !=
425 PF_POOL_ROUNDROBIN) {
426 mtx_unlock(&rpool->mtx);
427 return (1);
428 }
429 raddr = &rpool->cur->addr.p.dyn->pfid_addr6;
430 rmask = &rpool->cur->addr.p.dyn->pfid_mask6;
431 break;
432 #endif /* INET6 */
433 }
434 } else if (rpool->cur->addr.type == PF_ADDR_TABLE) {
435 if ((rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_ROUNDROBIN) {
436 mtx_unlock(&rpool->mtx);
437 return (1); /* unsupported */
438 }
439 } else {
440 raddr = &rpool->cur->addr.v.a.addr;
441 rmask = &rpool->cur->addr.v.a.mask;
442 }
443
444 switch (rpool->opts & PF_POOL_TYPEMASK) {
445 case PF_POOL_NONE:
446 PF_ACPY(naddr, raddr, af);
447 break;
448 case PF_POOL_BITMASK:
449 PF_POOLMASK(naddr, raddr, rmask, saddr, af);
450 break;
451 case PF_POOL_RANDOM:
452 if (init_addr != NULL && PF_AZERO(init_addr, af)) {
453 switch (af) {
454 #ifdef INET
455 case AF_INET:
456 rpool->counter.addr32[0] = htonl(arc4random());
457 break;
458 #endif /* INET */
459 #ifdef INET6
460 case AF_INET6:
461 if (rmask->addr32[3] != 0xffffffff)
462 rpool->counter.addr32[3] =
463 htonl(arc4random());
464 else
465 break;
466 if (rmask->addr32[2] != 0xffffffff)
467 rpool->counter.addr32[2] =
468 htonl(arc4random());
469 else
470 break;
471 if (rmask->addr32[1] != 0xffffffff)
472 rpool->counter.addr32[1] =
473 htonl(arc4random());
474 else
475 break;
476 if (rmask->addr32[0] != 0xffffffff)
477 rpool->counter.addr32[0] =
478 htonl(arc4random());
479 break;
480 #endif /* INET6 */
481 }
482 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
483 PF_ACPY(init_addr, naddr, af);
484
485 } else {
486 PF_AINC(&rpool->counter, af);
487 PF_POOLMASK(naddr, raddr, rmask, &rpool->counter, af);
488 }
489 break;
490 case PF_POOL_SRCHASH:
491 {
492 unsigned char hash[16];
493
494 pf_hash(saddr, (struct pf_addr *)&hash, &rpool->key, af);
495 PF_POOLMASK(naddr, raddr, rmask, (struct pf_addr *)&hash, af);
496 break;
497 }
498 case PF_POOL_ROUNDROBIN:
499 {
500 struct pf_kpooladdr *acur = rpool->cur;
501
502 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
503 if (!pfr_pool_get(rpool->cur->addr.p.tbl,
504 &rpool->tblidx, &rpool->counter, af))
505 goto get_addr;
506 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
507 if (!pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
508 &rpool->tblidx, &rpool->counter, af))
509 goto get_addr;
510 } else if (pf_match_addr(0, raddr, rmask, &rpool->counter, af))
511 goto get_addr;
512
513 try_next:
514 if (TAILQ_NEXT(rpool->cur, entries) == NULL)
515 rpool->cur = TAILQ_FIRST(&rpool->list);
516 else
517 rpool->cur = TAILQ_NEXT(rpool->cur, entries);
518 if (rpool->cur->addr.type == PF_ADDR_TABLE) {
519 rpool->tblidx = -1;
520 if (pfr_pool_get(rpool->cur->addr.p.tbl,
521 &rpool->tblidx, &rpool->counter, af)) {
522 /* table contains no address of type 'af' */
523 if (rpool->cur != acur)
524 goto try_next;
525 mtx_unlock(&rpool->mtx);
526 return (1);
527 }
528 } else if (rpool->cur->addr.type == PF_ADDR_DYNIFTL) {
529 rpool->tblidx = -1;
530 if (pfr_pool_get(rpool->cur->addr.p.dyn->pfid_kt,
531 &rpool->tblidx, &rpool->counter, af)) {
532 /* table contains no address of type 'af' */
533 if (rpool->cur != acur)
534 goto try_next;
535 mtx_unlock(&rpool->mtx);
536 return (1);
537 }
538 } else {
539 raddr = &rpool->cur->addr.v.a.addr;
540 rmask = &rpool->cur->addr.v.a.mask;
541 PF_ACPY(&rpool->counter, raddr, af);
542 }
543
544 get_addr:
545 PF_ACPY(naddr, &rpool->counter, af);
546 if (init_addr != NULL && PF_AZERO(init_addr, af))
547 PF_ACPY(init_addr, naddr, af);
548 PF_AINC(&rpool->counter, af);
549 break;
550 }
551 }
552 if (*sn != NULL)
553 PF_ACPY(&(*sn)->raddr, naddr, af);
554
555 mtx_unlock(&rpool->mtx);
556
557 if (V_pf_status.debug >= PF_DEBUG_MISC &&
558 (rpool->opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) {
559 printf("pf_map_addr: selected address ");
560 pf_print_host(naddr, 0, af);
561 printf("\n");
562 }
563
564 return (0);
565 }
566
567 struct pf_krule *
pf_get_translation(struct pf_pdesc * pd,struct mbuf * m,int off,int direction,struct pfi_kkif * kif,struct pf_ksrc_node ** sn,struct pf_state_key ** skp,struct pf_state_key ** nkp,struct pf_addr * saddr,struct pf_addr * daddr,uint16_t sport,uint16_t dport,struct pf_kanchor_stackframe * anchor_stack)568 pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction,
569 struct pfi_kkif *kif, struct pf_ksrc_node **sn,
570 struct pf_state_key **skp, struct pf_state_key **nkp,
571 struct pf_addr *saddr, struct pf_addr *daddr,
572 uint16_t sport, uint16_t dport, struct pf_kanchor_stackframe *anchor_stack)
573 {
574 struct pf_krule *r = NULL;
575 struct pf_addr *naddr;
576 uint16_t *nport;
577 uint16_t low, high;
578
579 PF_RULES_RASSERT();
580 KASSERT(*skp == NULL, ("*skp not NULL"));
581 KASSERT(*nkp == NULL, ("*nkp not NULL"));
582
583 if (direction == PF_OUT) {
584 r = pf_match_translation(pd, m, off, direction, kif, saddr,
585 sport, daddr, dport, PF_RULESET_BINAT, anchor_stack);
586 if (r == NULL)
587 r = pf_match_translation(pd, m, off, direction, kif,
588 saddr, sport, daddr, dport, PF_RULESET_NAT,
589 anchor_stack);
590 } else {
591 r = pf_match_translation(pd, m, off, direction, kif, saddr,
592 sport, daddr, dport, PF_RULESET_RDR, anchor_stack);
593 if (r == NULL)
594 r = pf_match_translation(pd, m, off, direction, kif,
595 saddr, sport, daddr, dport, PF_RULESET_BINAT,
596 anchor_stack);
597 }
598
599 if (r == NULL)
600 return (NULL);
601
602 switch (r->action) {
603 case PF_NONAT:
604 case PF_NOBINAT:
605 case PF_NORDR:
606 return (NULL);
607 }
608
609 *skp = pf_state_key_setup(pd, m, off, saddr, daddr, sport, dport);
610 if (*skp == NULL)
611 return (NULL);
612 *nkp = pf_state_key_clone(*skp);
613 if (*nkp == NULL) {
614 uma_zfree(V_pf_state_key_z, *skp);
615 *skp = NULL;
616 return (NULL);
617 }
618
619 /* XXX We only modify one side for now. */
620 naddr = &(*nkp)->addr[1];
621 nport = &(*nkp)->port[1];
622
623 switch (r->action) {
624 case PF_NAT:
625 if (pd->proto == IPPROTO_ICMP) {
626 low = 1;
627 high = 65535;
628 } else {
629 low = r->rpool.proxy_port[0];
630 high = r->rpool.proxy_port[1];
631 }
632 if (r->rpool.mape.offset > 0) {
633 if (pf_get_mape_sport(pd->af, pd->proto, r, saddr,
634 sport, daddr, dport, naddr, nport, sn)) {
635 DPFPRINTF(PF_DEBUG_MISC,
636 ("pf: MAP-E port allocation (%u/%u/%u)"
637 " failed\n",
638 r->rpool.mape.offset,
639 r->rpool.mape.psidlen,
640 r->rpool.mape.psid));
641 goto notrans;
642 }
643 } else if (pf_get_sport(pd->af, pd->proto, r, saddr, sport,
644 daddr, dport, naddr, nport, low, high, sn)) {
645 DPFPRINTF(PF_DEBUG_MISC,
646 ("pf: NAT proxy port allocation (%u-%u) failed\n",
647 r->rpool.proxy_port[0], r->rpool.proxy_port[1]));
648 goto notrans;
649 }
650 break;
651 case PF_BINAT:
652 switch (direction) {
653 case PF_OUT:
654 if (r->rpool.cur->addr.type == PF_ADDR_DYNIFTL){
655 switch (pd->af) {
656 #ifdef INET
657 case AF_INET:
658 if (r->rpool.cur->addr.p.dyn->
659 pfid_acnt4 < 1)
660 goto notrans;
661 PF_POOLMASK(naddr,
662 &r->rpool.cur->addr.p.dyn->
663 pfid_addr4,
664 &r->rpool.cur->addr.p.dyn->
665 pfid_mask4, saddr, AF_INET);
666 break;
667 #endif /* INET */
668 #ifdef INET6
669 case AF_INET6:
670 if (r->rpool.cur->addr.p.dyn->
671 pfid_acnt6 < 1)
672 goto notrans;
673 PF_POOLMASK(naddr,
674 &r->rpool.cur->addr.p.dyn->
675 pfid_addr6,
676 &r->rpool.cur->addr.p.dyn->
677 pfid_mask6, saddr, AF_INET6);
678 break;
679 #endif /* INET6 */
680 }
681 } else
682 PF_POOLMASK(naddr,
683 &r->rpool.cur->addr.v.a.addr,
684 &r->rpool.cur->addr.v.a.mask, saddr,
685 pd->af);
686 break;
687 case PF_IN:
688 if (r->src.addr.type == PF_ADDR_DYNIFTL) {
689 switch (pd->af) {
690 #ifdef INET
691 case AF_INET:
692 if (r->src.addr.p.dyn-> pfid_acnt4 < 1)
693 goto notrans;
694 PF_POOLMASK(naddr,
695 &r->src.addr.p.dyn->pfid_addr4,
696 &r->src.addr.p.dyn->pfid_mask4,
697 daddr, AF_INET);
698 break;
699 #endif /* INET */
700 #ifdef INET6
701 case AF_INET6:
702 if (r->src.addr.p.dyn->pfid_acnt6 < 1)
703 goto notrans;
704 PF_POOLMASK(naddr,
705 &r->src.addr.p.dyn->pfid_addr6,
706 &r->src.addr.p.dyn->pfid_mask6,
707 daddr, AF_INET6);
708 break;
709 #endif /* INET6 */
710 }
711 } else
712 PF_POOLMASK(naddr, &r->src.addr.v.a.addr,
713 &r->src.addr.v.a.mask, daddr, pd->af);
714 break;
715 }
716 break;
717 case PF_RDR: {
718 if (pf_map_addr(pd->af, r, saddr, naddr, NULL, sn))
719 goto notrans;
720 if ((r->rpool.opts & PF_POOL_TYPEMASK) == PF_POOL_BITMASK)
721 PF_POOLMASK(naddr, naddr, &r->rpool.cur->addr.v.a.mask,
722 daddr, pd->af);
723
724 /* Do not change SCTP ports. */
725 if (pd->proto == IPPROTO_SCTP)
726 break;
727
728 if (r->rpool.proxy_port[1]) {
729 uint32_t tmp_nport;
730
731 tmp_nport = ((ntohs(dport) - ntohs(r->dst.port[0])) %
732 (r->rpool.proxy_port[1] - r->rpool.proxy_port[0] +
733 1)) + r->rpool.proxy_port[0];
734
735 /* Wrap around if necessary. */
736 if (tmp_nport > 65535)
737 tmp_nport -= 65535;
738 *nport = htons((uint16_t)tmp_nport);
739 } else if (r->rpool.proxy_port[0])
740 *nport = htons(r->rpool.proxy_port[0]);
741 break;
742 }
743 default:
744 panic("%s: unknown action %u", __func__, r->action);
745 }
746
747 /* Return success only if translation really happened. */
748 if (bcmp(*skp, *nkp, sizeof(struct pf_state_key_cmp)))
749 return (r);
750
751 notrans:
752 uma_zfree(V_pf_state_key_z, *nkp);
753 uma_zfree(V_pf_state_key_z, *skp);
754 *skp = *nkp = NULL;
755 *sn = NULL;
756
757 return (NULL);
758 }
759