From db5adc203807af87ae936d14965442109aec8b42 Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Sun, 3 Oct 2021 18:19:29 +0545 Subject: [PATCH] fine-tuned REGISTER cookie handling (#837) --- include/n2n_define.h | 9 ++++++--- include/n2n_typedefs.h | 1 - src/edge_utils.c | 33 +++++++++++++++++---------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/n2n_define.h b/include/n2n_define.h index 9ddf4a8..ddba86a 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -165,9 +165,12 @@ enum skip_add{SN_ADD = 0, SN_ADD_SKIP = 1, SN_ADD_ADDED = 2}; #define N2N_PRIVATE_PUBLIC_KEY_SIZE 32 #define N2N_USER_KEY_LINE_STARTER '*' #define N2N_MAC_SIZE 6 -#define N2N_LOCAL_REG_COOKIE 0x00100000 -#define N2N_REGULAR_REG_COOKIE 0x01000000 -#define N2N_FORWARDED_REG_COOKIE 0x10000000 +#define N2N_NO_REG_COOKIE 0x00000000 +#define N2N_FORWARDED_REG_COOKIE 0x00001000 +#define N2N_PORT_REG_COOKIE 0x00004000 +#define N2N_REGULAR_REG_COOKIE 0x00010000 +#define N2N_MCAST_REG_COOKIE 0x00400000 +#define N2N_LOCAL_REG_COOKIE 0x01000000 #define N2N_DESC_SIZE 16 #define N2N_PKT_BUF_SIZE 2048 #define N2N_SOCKBUF_SIZE 64 /* string representation of INET or INET6 sockets */ diff --git a/include/n2n_typedefs.h b/include/n2n_typedefs.h index 7ba234e..fc9c8cb 100644 --- a/include/n2n_typedefs.h +++ b/include/n2n_typedefs.h @@ -437,7 +437,6 @@ struct peer_info { n2n_sock_t sock; SOCKET socket_fd; n2n_sock_t preferred_sock; - time_t last_local_reg; n2n_cookie_t last_cookie; n2n_auth_t auth; int timeout; diff --git a/src/edge_utils.c b/src/edge_utils.c index b5a159e..afe54ce 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -591,7 +591,7 @@ static void register_with_local_peers (n2n_edge_t * eee) { /* send registration to the local multicast group */ traceEvent(TRACE_DEBUG, "registering with multicast group %s:%u", N2N_MULTICAST_GROUP, N2N_MULTICAST_PORT); - send_register(eee, &(eee->multicast_peer), NULL, N2N_LOCAL_REG_COOKIE); + send_register(eee, &(eee->multicast_peer), NULL, N2N_MCAST_REG_COOKIE); } #else traceEvent(TRACE_DEBUG, "multicast peers discovery is disabled, skipping"); @@ -688,7 +688,7 @@ static void register_with_new_peer (n2n_edge_t *eee, (void *) (char *) &eee->conf.register_ttl, sizeof(eee->conf.register_ttl)); for(; alter > 0; alter--, sock.port++) { - send_register(eee, &sock, mac, N2N_REGULAR_REG_COOKIE); + send_register(eee, &sock, mac, N2N_PORT_REG_COOKIE); } setsockopt(eee->sock, IPPROTO_IP, IP_TTL, (void *) (char *) &curTTL, sizeof(curTTL)); #endif @@ -739,7 +739,7 @@ static void check_peer_registration_needed (n2n_edge_t *eee, memcpy(scan->mac_addr, mac, sizeof(n2n_mac_t)); HASH_ADD_PEER(eee->known_peers, scan); // reset last_local_reg to allow re-registration - scan->last_local_reg = 0; + scan->last_cookie = N2N_NO_REG_COOKIE; } } @@ -756,9 +756,8 @@ static void check_peer_registration_needed (n2n_edge_t *eee, if(via_multicast) scan->local = 1; - if(((now - scan->last_seen) > 0 /* >= 1 sec */) - ||(cookie & N2N_LOCAL_REG_COOKIE)) { + ||(cookie > scan->last_cookie)) { /* Don't register too often */ check_known_peer_sock_change(eee, from_supernode, via_multicast, mac, dev_addr, dev_desc, peer, now); } @@ -787,7 +786,7 @@ static void peer_set_p2p_confirmed (n2n_edge_t * eee, scan = find_peer_by_sock(peer, eee->pending_peers); // in case of MAC change, reset last_local_reg to allow re-registration if(scan) - scan->last_local_reg = 0; + scan->last_cookie = N2N_NO_REG_COOKIE; } if(scan) { @@ -799,20 +798,20 @@ static void peer_set_p2p_confirmed (n2n_edge_t * eee, free(scan); scan = scan_tmp; memcpy(scan->mac_addr, mac, sizeof(n2n_mac_t)); - // in case of MAC change, reset last_local_reg to allow re-registration - scan->last_local_reg = 0; + // in case of MAC change, reset cookie to allow immediate re-registration + scan->last_cookie = N2N_NO_REG_COOKIE; } else { - // ignore regular ACKs's socket update for a while if we have recently received a local (!) ACK - if(((now - scan->last_local_reg) > REGISTRATION_TIMEOUT) - ||(cookie & N2N_LOCAL_REG_COOKIE)) { + // update sock but ... + // ... ignore ACKs's (and their socks) from lower ranked inbound ways for a while + if(((now - scan->last_seen) > REGISTRATION_TIMEOUT / 4) + ||(cookie > scan->last_cookie)) { scan->sock = *peer; + scan->last_cookie = cookie; } } HASH_ADD_PEER(eee->known_peers, scan); scan->last_p2p = now; - if(cookie & N2N_LOCAL_REG_COOKIE) - scan->last_local_reg = now; traceEvent(TRACE_DEBUG, "p2p connection established: %s [%s]", macaddr_str(mac_buf, mac), @@ -1594,7 +1593,7 @@ void update_supernode_reg (n2n_edge_t * eee, time_t now) { if(eee->sn_wait == 1) HASH_ITER(hh, eee->known_peers, peer, tmp_peer) if((now - peer->last_seen) > REGISTER_SUPER_INTERVAL_DFL) - send_register(eee, &(peer->sock), peer->mac_addr, N2N_REGULAR_REG_COOKIE); + send_register(eee, &(peer->sock), peer->mac_addr, peer->last_cookie); eee->sn_wait = 1; @@ -2517,7 +2516,10 @@ void process_udp (n2n_edge_t *eee, const struct sockaddr_in *sender_sock, const /* Update the sender in peer table entry */ check_peer_registration_needed(eee, from_supernode, via_multicast, - pkt.srcMac, N2N_REGULAR_REG_COOKIE, NULL, NULL, orig_sender); + pkt.srcMac, + // REVISIT: also consider PORT_REG_COOKIEs when implemented + from_supernode ? N2N_FORWARDED_REG_COOKIE : N2N_REGULAR_REG_COOKIE, + NULL, NULL, orig_sender); handle_PACKET(eee, from_supernode, &pkt, orig_sender, udp_buf + idx, udp_size - idx); break; @@ -2600,7 +2602,6 @@ void process_udp (n2n_edge_t *eee, const struct sockaddr_in *sender_sock, const sock_to_cstr(sockbuf1, &sender), (ra.cookie & N2N_LOCAL_REG_COOKIE) ? " (local)" : ""); - peer_set_p2p_confirmed(eee, ra.srcMac, ra.cookie, &sender, now);