Changeset View
Changeset View
Standalone View
Standalone View
sys/netpfil/pf/pf_lb.c
Show First 20 Lines • Show All 56 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_krule *pf_match_translation(struct pf_pdesc *, struct mbuf *, | static struct pf_krule *pf_match_translation(struct pf_pdesc *, struct mbuf *, | ||||
int, struct pfi_kkif *, | int, struct pfi_kkif *, | ||||
struct pf_addr *, u_int16_t, struct pf_addr *, | struct pf_addr *, u_int16_t, struct pf_addr *, | ||||
uint16_t, int, struct pf_kanchor_stackframe *); | uint16_t, int, struct pf_kanchor_stackframe *); | ||||
static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *, | static int pf_get_sport(sa_family_t, uint8_t, struct pf_krule *, | ||||
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_ksrc_node **); | uint16_t *, uint16_t, uint16_t, struct pf_ksrc_node **); | ||||
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 137 Lines • ▼ Show 20 Lines | pf_match_translation(struct pf_pdesc *pd, struct mbuf *m, int off, | ||||
return (rm); | return (rm); | ||||
} | } | ||||
static int | static int | ||||
pf_get_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r, | 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, | 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_ksrc_node **sn) | uint16_t high, struct pf_ksrc_node **sn) | ||||
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; | ||||
struct pf_srchash *sh = NULL; | |||||
bzero(&init_addr, sizeof(init_addr)); | bzero(&init_addr, sizeof(init_addr)); | ||||
Not Done Inline ActionsEmpty line below. glebius: Empty line below. | |||||
Done Inline ActionsI'd MPASS(*udp_mapping == NULL) here. kp: I'd MPASS(*udp_mapping == NULL) here. | |||||
/* | |||||
* If we are UDP and have an existing mapping we can get source port | |||||
* from the mapping. In this case we have to look up the src_node as | |||||
* pf_map_addr would. | |||||
*/ | |||||
if (proto == IPPROTO_UDP && (r->rpool.opts & PF_POOL_ENDPI)) { | |||||
struct pf_udp_endpoint_cmp udp_source; | |||||
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); | |||||
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… | |||||
if (*udp_mapping) { | |||||
PF_ACPY(naddr, &(*udp_mapping)->endpoints[1].addr, af); | |||||
*nport = (*udp_mapping)->endpoints[1].port; | |||||
/* Try to find a src_node 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, &sh, 0); | |||||
return (0); | |||||
} else { | |||||
*udp_mapping = pf_udp_mapping_create(af, saddr, sport, &init_addr, 0); | |||||
if (*udp_mapping == NULL) | |||||
Done Inline ActionsAccidentally indented with spaces, not tabs. kp: Accidentally indented with spaces, not tabs. | |||||
return (1); | |||||
} | |||||
} | |||||
if (pf_map_addr(af, r, saddr, naddr, NULL, &init_addr, sn)) | if (pf_map_addr(af, r, saddr, naddr, NULL, &init_addr, sn)) | ||||
return (1); | return (1); | ||||
if (proto == IPPROTO_ICMP) { | if (proto == IPPROTO_ICMP) { | ||||
if (*nport == htons(ICMP_ECHO)) { | if (*nport == htons(ICMP_ECHO)) { | ||||
low = 1; | low = 1; | ||||
high = 65535; | high = 65535; | ||||
} else | } else | ||||
Show All 39 Lines | if (proto == IPPROTO_SCTP) { | ||||
key.port[1] = sport; | key.port[1] = sport; | ||||
if (!pf_find_state_all_exists(&key, PF_IN)) { | if (!pf_find_state_all_exists(&key, PF_IN)) { | ||||
*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_exists(&key, PF_IN)) { | if (!pf_find_state_all_exists(&key, PF_IN)) { | ||||
if (proto == IPPROTO_UDP && | |||||
(r->rpool.opts & PF_POOL_ENDPI)) { | |||||
(*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 { | |||||
uint32_t tmp; | uint32_t tmp; | ||||
uint16_t cut; | uint16_t 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 <= 0xffff; ++tmp) { | for (tmp = cut; tmp <= high && tmp <= 0xffff; ++tmp) { | ||||
if (proto == IPPROTO_UDP && | |||||
(r->rpool.opts & PF_POOL_ENDPI)) { | |||||
(*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_exists(&key, PF_IN)) { | if (!pf_find_state_all_exists(&key, PF_IN)) { | ||||
*nport = htons(tmp); | *nport = htons(tmp); | ||||
return (0); | return (0); | ||||
} | } | ||||
} | } | ||||
} | |||||
tmp = cut; | tmp = cut; | ||||
for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) { | for (tmp -= 1; tmp >= low && tmp <= 0xffff; --tmp) { | ||||
if (proto == IPPROTO_UDP && | |||||
(r->rpool.opts & PF_POOL_ENDPI)) { | |||||
(*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_exists(&key, PF_IN)) { | if (!pf_find_state_all_exists(&key, PF_IN)) { | ||||
*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: | ||||
/* | /* | ||||
* pick a different source address since we're out | * pick a different source address since we're out | ||||
* of free port choices for the current one. | * of free port choices for the current one. | ||||
*/ | */ | ||||
if (pf_map_addr(af, r, saddr, naddr, NULL, &init_addr, sn)) | if (pf_map_addr(af, r, saddr, naddr, NULL, &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 */ | return (1); /* none available */ | ||||
} | } | ||||
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. | |||||
static int | static int | ||||
pf_get_mape_sport(sa_family_t af, u_int8_t proto, struct pf_krule *r, | 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, | struct pf_addr *saddr, uint16_t sport, struct pf_addr *daddr, | ||||
uint16_t dport, struct pf_addr *naddr, uint16_t *nport, | uint16_t dport, struct pf_addr *naddr, uint16_t *nport, | ||||
struct pf_ksrc_node **sn) | struct pf_ksrc_node **sn) | ||||
{ | { | ||||
uint16_t psmask, low, highmask; | uint16_t psmask, low, highmask; | ||||
uint16_t i, ahigh, cut; | uint16_t i, ahigh, cut; | ||||
▲ Show 20 Lines • Show All 446 Lines • Show Last 20 Lines |
Line length.