Changeset View
Changeset View
Standalone View
Standalone View
sys/netpfil/pf/pf_lb.c
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
static void pf_hash(struct pf_addr *, struct pf_addr *, | static void pf_hash(struct pf_addr *, struct pf_addr *, | ||||
struct pf_poolhashkey *, sa_family_t); | struct pf_poolhashkey *, sa_family_t); | ||||
static struct pf_rule *pf_match_translation(struct pf_pdesc *, struct mbuf *, | static struct pf_rule *pf_match_translation(struct pf_pdesc *, struct mbuf *, | ||||
int, int, struct pfi_kif *, | int, int, struct pfi_kif *, | ||||
struct pf_addr *, u_int16_t, struct pf_addr *, | struct pf_addr *, u_int16_t, struct pf_addr *, | ||||
uint16_t, int, struct pf_anchor_stackframe *); | uint16_t, int, struct pf_anchor_stackframe *); | ||||
static int pf_get_sport(sa_family_t, uint8_t, struct pf_rule *, | static int pf_get_sport(sa_family_t, uint8_t, struct pf_rule *, | ||||
struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *, | struct pf_addr *, uint16_t, struct pf_addr *, uint16_t, struct pf_addr *, | ||||
uint16_t *, uint16_t, uint16_t, struct pf_src_node **); | uint16_t *, uint16_t, uint16_t, struct pf_src_node **, | ||||
struct pf_udp_mapping**); | |||||
kp: Line length. | |||||
#define mix(a,b,c) \ | #define mix(a,b,c) \ | ||||
do { \ | do { \ | ||||
a -= b; a -= c; a ^= (c >> 13); \ | a -= b; a -= c; a ^= (c >> 13); \ | ||||
b -= c; b -= a; b ^= (a << 8); \ | b -= c; b -= a; b ^= (a << 8); \ | ||||
c -= a; c -= b; c ^= (b >> 13); \ | c -= a; c -= b; c ^= (b >> 13); \ | ||||
a -= b; a -= c; a ^= (c >> 12); \ | a -= b; a -= c; a ^= (c >> 12); \ | ||||
b -= c; b -= a; b ^= (a << 16); \ | b -= c; b -= a; b ^= (a << 16); \ | ||||
▲ Show 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | if (rm != NULL && (rm->action == PF_NONAT || | ||||
return (NULL); | return (NULL); | ||||
return (rm); | return (rm); | ||||
} | } | ||||
static int | static int | ||||
pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r, | pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_rule *r, | ||||
struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr, | 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 dport, struct pf_addr *naddr, uint16_t *nport, uint16_t low, | ||||
uint16_t high, struct pf_src_node **sn) | uint16_t high, struct pf_src_node **sn, struct pf_udp_mapping **udp_mapping) | ||||
Done Inline ActionsLine length. kp: Line length. | |||||
{ | { | ||||
struct pf_state_key_cmp key; | struct pf_state_key_cmp key; | ||||
struct pf_addr init_addr; | struct pf_addr init_addr; | ||||
if (proto == IPPROTO_UDP) { | |||||
struct pf_udp_endpoint_cmp udp_source; | |||||
Not Done Inline ActionsEmpty line below. glebius: Empty line below. | |||||
bzero(&udp_source, sizeof(udp_source)); | |||||
udp_source.af = af; | |||||
PF_ACPY(&udp_source.addr, saddr, af); | |||||
udp_source.port = sport; | |||||
*udp_mapping = pf_udp_mapping_find(&udp_source); | |||||
if (*udp_mapping) { | |||||
PF_ACPY(naddr, &(*udp_mapping)->endpoints[1].addr, af); | |||||
*nport = (*udp_mapping)->endpoints[1].port; | |||||
/* as per pf_map_addr(): */ | |||||
if (*sn == NULL && r->rpool.opts & PF_POOL_STICKYADDR && | |||||
(r->rpool.opts & PF_POOL_TYPEMASK) != PF_POOL_NONE) | |||||
*sn = pf_find_src_node(saddr, r, af, 0); | |||||
return (0); | |||||
} else { | |||||
*udp_mapping = pf_udp_mapping_create(af, saddr, sport, &init_addr, 0); | |||||
if (*udp_mapping == NULL) | |||||
return (1); | |||||
} | |||||
} | |||||
bzero(&init_addr, sizeof(init_addr)); | bzero(&init_addr, sizeof(init_addr)); | ||||
if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn)) | if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn)) | ||||
Done Inline ActionsI'd MPASS(*udp_mapping == NULL) here. kp: I'd MPASS(*udp_mapping == NULL) here. | |||||
return (1); | goto failed; | ||||
if (proto == IPPROTO_ICMP) { | if (proto == IPPROTO_ICMP) { | ||||
low = 1; | low = 1; | ||||
high = 65535; | high = 65535; | ||||
} | } | ||||
bzero(&key, sizeof(key)); | bzero(&key, sizeof(key)); | ||||
key.af = af; | key.af = af; | ||||
key.proto = proto; | key.proto = proto; | ||||
key.port[0] = dport; | key.port[0] = dport; | ||||
PF_ACPY(&key.addr[0], daddr, key.af); | PF_ACPY(&key.addr[0], daddr, key.af); | ||||
Not Done Inline ActionsDo we not need to release the mapping when we're done with it? kp: Do we not need to release the mapping when we're done with it? | |||||
Not Done Inline ActionsThe mapping is cleaned up in pf_unlink_state, or do you mean something else I'm missing? thj: The mapping is cleaned up in pf_unlink_state, or do you mean something else I'm missing? | |||||
Not Done Inline ActionsI meant that pf_udp_mapping_find() increments the reference counter for the mapping, and I didn't see where we decrement it again. I suspect I missed that we return it to the caller, and rely on it to release the reference for us. Have you tried unloading pf after a test run? If we forget to clean things up uma ought to warn. kp: I meant that pf_udp_mapping_find() increments the reference counter for the mapping, and I… | |||||
do { | do { | ||||
PF_ACPY(&key.addr[1], naddr, key.af); | PF_ACPY(&key.addr[1], naddr, key.af); | ||||
if ((*udp_mapping)) | |||||
PF_ACPY(&(*udp_mapping)->endpoints[1].addr, naddr, af); | |||||
/* | /* | ||||
* port search; start random, step; | * port search; start random, step; | ||||
* similar 2 portloop in in_pcbbind | * similar 2 portloop in in_pcbbind | ||||
*/ | */ | ||||
if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP || | if (!(proto == IPPROTO_TCP || proto == IPPROTO_UDP || | ||||
proto == IPPROTO_ICMP) || (low == 0 && high == 0)) { | proto == IPPROTO_ICMP) || (low == 0 && high == 0)) { | ||||
Done Inline ActionsAccidentally indented with spaces, not tabs. kp: Accidentally indented with spaces, not tabs. | |||||
/* | /* | ||||
* XXX bug: icmp states don't use the id on both sides. | * XXX bug: icmp states don't use the id on both sides. | ||||
* (traceroute -I through nat) | * (traceroute -I through nat) | ||||
*/ | */ | ||||
key.port[1] = sport; | key.port[1] = sport; | ||||
if (pf_find_state_all(&key, PF_IN, NULL) == NULL) { | if (pf_find_state_all(&key, PF_IN, NULL) == NULL) { | ||||
*nport = sport; | *nport = sport; | ||||
return (0); | return (0); | ||||
} | } | ||||
} else if (low == high) { | } else if (low == high) { | ||||
key.port[1] = htons(low); | key.port[1] = htons(low); | ||||
if (pf_find_state_all(&key, PF_IN, NULL) == NULL) { | if (pf_find_state_all(&key, PF_IN, NULL) == NULL) { | ||||
if (proto == IPPROTO_UDP) { | |||||
(*udp_mapping)->endpoints[1].port = htons(low); | |||||
if (pf_udp_mapping_insert(*udp_mapping) == 0) { | |||||
*nport = htons(low); | *nport = htons(low); | ||||
return (0); | return (0); | ||||
} | } | ||||
} else { | } else { | ||||
*nport = htons(low); | |||||
return (0); | |||||
} | |||||
Done Inline ActionsCan we not just if (*udp_mapping != NULL) here? kp: Can we not just `if (*udp_mapping != NULL)` here? | |||||
} | |||||
} else { | |||||
uint16_t tmp, cut; | uint16_t tmp, cut; | ||||
if (low > high) { | if (low > high) { | ||||
tmp = low; | tmp = low; | ||||
low = high; | low = high; | ||||
high = tmp; | high = tmp; | ||||
} | } | ||||
/* low < high */ | /* low < high */ | ||||
cut = arc4random() % (1 + high - low) + low; | cut = arc4random() % (1 + high - low) + low; | ||||
/* low <= cut <= high */ | /* low <= cut <= high */ | ||||
for (tmp = cut; tmp <= high; ++(tmp)) { | for (tmp = cut; tmp <= high; ++(tmp)) { | ||||
if (proto == IPPROTO_UDP) { | |||||
(*udp_mapping)->endpoints[1].port = htons(tmp); | |||||
if (pf_udp_mapping_insert(*udp_mapping) == 0) { | |||||
*nport = htons(tmp); | |||||
return (0); | |||||
} | |||||
} else { | |||||
key.port[1] = htons(tmp); | key.port[1] = htons(tmp); | ||||
if (pf_find_state_all(&key, PF_IN, NULL) == | if (pf_find_state_all(&key, PF_IN, NULL) == | ||||
NULL) { | NULL) { | ||||
*nport = htons(tmp); | *nport = htons(tmp); | ||||
return (0); | return (0); | ||||
} | } | ||||
} | } | ||||
} | |||||
for (tmp = cut - 1; tmp >= low; --(tmp)) { | for (tmp = cut - 1; tmp >= low; --(tmp)) { | ||||
if (proto == IPPROTO_UDP) { | |||||
(*udp_mapping)->endpoints[1].port = htons(tmp); | |||||
if (pf_udp_mapping_insert(*udp_mapping) == 0) { | |||||
*nport = htons(tmp); | |||||
return (0); | |||||
} | |||||
} else { | |||||
key.port[1] = htons(tmp); | key.port[1] = htons(tmp); | ||||
if (pf_find_state_all(&key, PF_IN, NULL) == | if (pf_find_state_all(&key, PF_IN, NULL) == | ||||
NULL) { | NULL) { | ||||
*nport = htons(tmp); | *nport = htons(tmp); | ||||
return (0); | return (0); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | |||||
switch (r->rpool.opts & PF_POOL_TYPEMASK) { | switch (r->rpool.opts & PF_POOL_TYPEMASK) { | ||||
case PF_POOL_RANDOM: | case PF_POOL_RANDOM: | ||||
case PF_POOL_ROUNDROBIN: | case PF_POOL_ROUNDROBIN: | ||||
if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn)) | if (pf_map_addr(af, r, saddr, naddr, &init_addr, sn)) | ||||
return (1); | return (1); | ||||
break; | break; | ||||
case PF_POOL_NONE: | case PF_POOL_NONE: | ||||
case PF_POOL_SRCHASH: | case PF_POOL_SRCHASH: | ||||
case PF_POOL_BITMASK: | case PF_POOL_BITMASK: | ||||
default: | default: | ||||
return (1); | return (1); | ||||
} | } | ||||
} while (! PF_AEQ(&init_addr, naddr, af) ); | } while (! PF_AEQ(&init_addr, naddr, af) ); | ||||
return (1); /* none available */ | /* none available */ | ||||
failed: | |||||
if (*udp_mapping) { | |||||
Not Done Inline ActionsThat doesn't need the NULL check. ima_zfree_pcpu(..., NULL) is safe. kp: That doesn't need the NULL check. ima_zfree_pcpu(..., NULL) is safe. | |||||
uma_zfree(V_pf_udp_mapping_z, *udp_mapping); | |||||
*udp_mapping = NULL; | |||||
} | } | ||||
return (1); | |||||
} | |||||
int | int | ||||
pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr, | pf_map_addr(sa_family_t af, struct pf_rule *r, struct pf_addr *saddr, | ||||
struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn) | struct pf_addr *naddr, struct pf_addr *init_addr, struct pf_src_node **sn) | ||||
{ | { | ||||
struct pf_pool *rpool = &r->rpool; | struct pf_pool *rpool = &r->rpool; | ||||
struct pf_addr *raddr = NULL, *rmask = NULL; | struct pf_addr *raddr = NULL, *rmask = NULL; | ||||
▲ Show 20 Lines • Show All 194 Lines • ▼ Show 20 Lines | #endif /* INET6 */ | ||||
return (0); | return (0); | ||||
} | } | ||||
struct pf_rule * | struct pf_rule * | ||||
pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction, | pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction, | ||||
struct pfi_kif *kif, struct pf_src_node **sn, | struct pfi_kif *kif, struct pf_src_node **sn, | ||||
struct pf_state_key **skp, struct pf_state_key **nkp, | struct pf_state_key **skp, struct pf_state_key **nkp, | ||||
struct pf_addr *saddr, struct pf_addr *daddr, | struct pf_addr *saddr, struct pf_addr *daddr, | ||||
uint16_t sport, uint16_t dport, struct pf_anchor_stackframe *anchor_stack) | uint16_t sport, uint16_t dport, struct pf_anchor_stackframe *anchor_stack, | ||||
struct pf_udp_mapping **udp_mapping) | |||||
{ | { | ||||
struct pf_rule *r = NULL; | struct pf_rule *r = NULL; | ||||
struct pf_addr *naddr; | struct pf_addr *naddr; | ||||
uint16_t *nport; | uint16_t *nport; | ||||
PF_RULES_RASSERT(); | PF_RULES_RASSERT(); | ||||
KASSERT(*skp == NULL, ("*skp not NULL")); | KASSERT(*skp == NULL, ("*skp not NULL")); | ||||
KASSERT(*nkp == NULL, ("*nkp not NULL")); | KASSERT(*nkp == NULL, ("*nkp not NULL")); | ||||
Show All 37 Lines | pf_get_translation(struct pf_pdesc *pd, struct mbuf *m, int off, int direction, | ||||
/* XXX We only modify one side for now. */ | /* XXX We only modify one side for now. */ | ||||
naddr = &(*nkp)->addr[1]; | naddr = &(*nkp)->addr[1]; | ||||
nport = &(*nkp)->port[1]; | nport = &(*nkp)->port[1]; | ||||
switch (r->action) { | switch (r->action) { | ||||
case PF_NAT: | case PF_NAT: | ||||
if (pf_get_sport(pd->af, pd->proto, r, saddr, sport, daddr, | if (pf_get_sport(pd->af, pd->proto, r, saddr, sport, daddr, | ||||
dport, naddr, nport, r->rpool.proxy_port[0], | dport, naddr, nport, r->rpool.proxy_port[0], | ||||
r->rpool.proxy_port[1], sn)) { | r->rpool.proxy_port[1], sn, udp_mapping)) { | ||||
DPFPRINTF(PF_DEBUG_MISC, | DPFPRINTF(PF_DEBUG_MISC, | ||||
("pf: NAT proxy port allocation (%u-%u) failed\n", | ("pf: NAT proxy port allocation (%u-%u) failed\n", | ||||
r->rpool.proxy_port[0], r->rpool.proxy_port[1])); | r->rpool.proxy_port[0], r->rpool.proxy_port[1])); | ||||
goto notrans; | goto notrans; | ||||
} | } | ||||
break; | break; | ||||
case PF_BINAT: | case PF_BINAT: | ||||
switch (direction) { | switch (direction) { | ||||
▲ Show 20 Lines • Show All 102 Lines • Show Last 20 Lines |
Line length.