Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/wg/if_wg.c
| Show First 20 Lines • Show All 306 Lines • ▼ Show 20 Lines | |||||
| static void wg_timers_event_session_derived(struct wg_peer *); | static void wg_timers_event_session_derived(struct wg_peer *); | ||||
| static void wg_timers_event_want_initiation(struct wg_peer *); | static void wg_timers_event_want_initiation(struct wg_peer *); | ||||
| static void wg_timers_run_send_initiation(struct wg_peer *, bool); | static void wg_timers_run_send_initiation(struct wg_peer *, bool); | ||||
| static void wg_timers_run_retry_handshake(void *); | static void wg_timers_run_retry_handshake(void *); | ||||
| static void wg_timers_run_send_keepalive(void *); | static void wg_timers_run_send_keepalive(void *); | ||||
| static void wg_timers_run_new_handshake(void *); | static void wg_timers_run_new_handshake(void *); | ||||
| static void wg_timers_run_zero_key_material(void *); | static void wg_timers_run_zero_key_material(void *); | ||||
| static void wg_timers_run_persistent_keepalive(void *); | static void wg_timers_run_persistent_keepalive(void *); | ||||
| static int wg_aip_add(struct wg_softc *, struct wg_peer *, sa_family_t, const void *, uint8_t); | static int wg_aip_add(struct wg_softc *, struct wg_peer *, sa_family_t, | ||||
| const void *, uint8_t); | |||||
| static struct wg_peer *wg_aip_lookup(struct wg_softc *, sa_family_t, void *); | static struct wg_peer *wg_aip_lookup(struct wg_softc *, sa_family_t, void *); | ||||
| static void wg_aip_remove_all(struct wg_softc *, struct wg_peer *); | static void wg_aip_remove_all(struct wg_softc *, struct wg_peer *); | ||||
| static struct wg_peer *wg_peer_create(struct wg_softc *, | static struct wg_peer *wg_peer_create(struct wg_softc *, | ||||
| const uint8_t [WG_KEY_SIZE], int *); | const uint8_t [WG_KEY_SIZE], int *); | ||||
| static void wg_peer_free_deferred(struct noise_remote *); | static void wg_peer_free_deferred(struct noise_remote *); | ||||
| static void wg_peer_destroy(struct wg_peer *); | static void wg_peer_destroy(struct wg_peer *); | ||||
| static void wg_peer_destroy_all(struct wg_softc *); | static void wg_peer_destroy_all(struct wg_softc *); | ||||
| static void wg_peer_send_buf(struct wg_peer *, uint8_t *, size_t); | static void wg_peer_send_buf(struct wg_peer *, uint8_t *, size_t); | ||||
| ▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | |||||
| static void | static void | ||||
| wg_peer_get_endpoint(struct wg_peer *peer, struct wg_endpoint *e) | wg_peer_get_endpoint(struct wg_peer *peer, struct wg_endpoint *e) | ||||
| { | { | ||||
| rw_rlock(&peer->p_endpoint_lock); | rw_rlock(&peer->p_endpoint_lock); | ||||
| *e = peer->p_endpoint; | *e = peer->p_endpoint; | ||||
| rw_runlock(&peer->p_endpoint_lock); | rw_runlock(&peer->p_endpoint_lock); | ||||
| } | } | ||||
| /* Allowed IP */ | |||||
| static int | static int | ||||
| wg_aip_add(struct wg_softc *sc, struct wg_peer *peer, sa_family_t af, const void *addr, uint8_t cidr) | wg_aip_addrinfo(struct wg_aip *aip, const void *baddr, uint8_t cidr) | ||||
| { | { | ||||
| struct radix_node_head *root; | struct aip_addr *addr, *mask; | ||||
aly_aaronly.me: This function has quite a few parameters. Why not use something like:
```
static int… | |||||
Done Inline ActionsGenerally because of the next review, where we have to construct an addr/mask before we can find the existing entry. kevans: Generally because of the next review, where we have to construct an addr/mask before we can… | |||||
Done Inline ActionsI had a rough look at the next review as well, and I think it's okay to simplify this function by using an on-stack struct wg_aip aip instead of struct aip_addr a_addr, a_mask. aly_aaronly.me: I had a rough look at the next review as well, and I think it's okay to simplify this function… | |||||
| struct radix_node *node; | |||||
| struct wg_aip *aip; | |||||
| int ret = 0; | |||||
| aip = malloc(sizeof(*aip), M_WG, M_WAITOK | M_ZERO); | addr = &aip->a_addr; | ||||
| aip->a_peer = peer; | mask = &aip->a_mask; | ||||
| aip->a_af = af; | switch (aip->a_af) { | ||||
| switch (af) { | |||||
| #ifdef INET | #ifdef INET | ||||
| case AF_INET: | case AF_INET: | ||||
| if (cidr > 32) cidr = 32; | if (cidr > 32) cidr = 32; | ||||
| root = sc->sc_aip4; | addr->in = *(const struct in_addr *)baddr; | ||||
| aip->a_addr.in = *(const struct in_addr *)addr; | mask->ip = htonl(~((1LL << (32 - cidr)) - 1) & 0xffffffff); | ||||
| aip->a_mask.ip = htonl(~((1LL << (32 - cidr)) - 1) & 0xffffffff); | addr->ip &= mask->ip; | ||||
| aip->a_addr.ip &= aip->a_mask.ip; | addr->length = mask->length = offsetof(struct aip_addr, in) + sizeof(struct in_addr); | ||||
| aip->a_addr.length = aip->a_mask.length = offsetof(struct aip_addr, in) + sizeof(struct in_addr); | |||||
| break; | break; | ||||
| #endif | #endif | ||||
| #ifdef INET6 | #ifdef INET6 | ||||
| case AF_INET6: | case AF_INET6: | ||||
| if (cidr > 128) cidr = 128; | if (cidr > 128) cidr = 128; | ||||
| root = sc->sc_aip6; | addr->in6 = *(const struct in6_addr *)baddr; | ||||
| aip->a_addr.in6 = *(const struct in6_addr *)addr; | in6_prefixlen2mask(&mask->in6, cidr); | ||||
| in6_prefixlen2mask(&aip->a_mask.in6, cidr); | |||||
| for (int i = 0; i < 4; i++) | for (int i = 0; i < 4; i++) | ||||
| aip->a_addr.ip6[i] &= aip->a_mask.ip6[i]; | addr->ip6[i] &= mask->ip6[i]; | ||||
| aip->a_addr.length = aip->a_mask.length = offsetof(struct aip_addr, in6) + sizeof(struct in6_addr); | addr->length = mask->length = offsetof(struct aip_addr, in6) + sizeof(struct in6_addr); | ||||
| break; | break; | ||||
| #endif | #endif | ||||
| default: | default: | ||||
| free(aip, M_WG); | |||||
| return (EAFNOSUPPORT); | return (EAFNOSUPPORT); | ||||
| } | } | ||||
| return (0); | |||||
| } | |||||
| /* Allowed IP */ | |||||
| static int | |||||
| wg_aip_add(struct wg_softc *sc, struct wg_peer *peer, sa_family_t af, | |||||
| const void *baddr, uint8_t cidr) | |||||
| { | |||||
| struct radix_node_head *root = NULL; | |||||
| struct radix_node *node; | |||||
| struct wg_aip *aip; | |||||
| int ret = 0; | |||||
| aip = malloc(sizeof(*aip), M_WG, M_WAITOK | M_ZERO); | |||||
| aip->a_peer = peer; | |||||
| aip->a_af = af; | |||||
| ret = wg_aip_addrinfo(aip, baddr, cidr); | |||||
| if (ret != 0) { | |||||
| free(aip, M_WG); | |||||
| return (ret); | |||||
| } | |||||
| root = af == AF_INET ? sc->sc_aip4 : sc->sc_aip6; | |||||
| MPASS(root != NULL); | |||||
| RADIX_NODE_HEAD_LOCK(root); | RADIX_NODE_HEAD_LOCK(root); | ||||
| node = root->rnh_addaddr(&aip->a_addr, &aip->a_mask, &root->rh, aip->a_nodes); | node = root->rnh_addaddr(&aip->a_addr, &aip->a_mask, &root->rh, aip->a_nodes); | ||||
| if (node == aip->a_nodes) { | if (node == aip->a_nodes) { | ||||
| LIST_INSERT_HEAD(&peer->p_aips, aip, a_entry); | LIST_INSERT_HEAD(&peer->p_aips, aip, a_entry); | ||||
| peer->p_aips_num++; | peer->p_aips_num++; | ||||
| } else if (!node) | } else if (!node) | ||||
| node = root->rnh_lookup(&aip->a_addr, &aip->a_mask, &root->rh); | node = root->rnh_lookup(&aip->a_addr, &aip->a_mask, &root->rh); | ||||
| if (!node) { | if (!node) { | ||||
| ▲ Show 20 Lines • Show All 2,642 Lines • Show Last 20 Lines | |||||
This function has quite a few parameters. Why not use something like:
static int wg_aip_addrinfo(struct wg_aip *aip, const void *addr, uint8_t cidr) { switch (aip->af) { ... } } static int wg_aip_add(...) { ... ret = wg_aip_addrinfo(...); if (ret != 0) { ... }; MPASS(af == AF_INET4 || af == AF_INET6); root = af == AF_INET ? sc->sc_aip4 : sc->sc_aip6; ... }