From: Guus Sliepen Date: Wed, 10 Oct 2012 12:46:22 +0000 (+0200) Subject: Try all known addresses of node during the PMTU discovery phase. X-Git-Tag: release-1.1pre3~13 X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=58f4b845b9a7d83739af77337f2ce263d8df7838;p=tinc Try all known addresses of node during the PMTU discovery phase. This helps in situations where some nodes have IPv6 and others have not. --- diff --git a/src/graph.c b/src/graph.c index 86b62a51..48310bcd 100644 --- a/src/graph.c +++ b/src/graph.c @@ -208,6 +208,7 @@ static void check_reachability(void) { } n->last_req_key = 0; + n->status.udp_confirmed = false; n->maxmtu = MTU; n->minmtu = 0; n->mtuprobes = 0; diff --git a/src/info.c b/src/info.c index 93be24e4..ed4dc827 100644 --- a/src/info.c +++ b/src/info.c @@ -111,6 +111,8 @@ static int info_node(int fd, const char *item) { printf(" indirect"); if(status.sptps) printf(" sptps"); + if(status.udp_confirmed) + printf(" udp_confirmed"); printf("\n"); printf("Options: "); diff --git a/src/net_packet.c b/src/net_packet.c index 88d791dc..6bb2fca0 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -97,6 +97,7 @@ static void send_mtu_probe_handler(int fd, short events, void *data) { } logger(DEBUG_TRAFFIC, LOG_INFO, "%s (%s) did not respond to UDP ping, restarting PMTU discovery", n->name, n->hostname); + n->status.udp_confirmed = false; n->mtuprobes = 1; n->minmtu = 0; n->maxmtu = MTU; @@ -166,6 +167,8 @@ static void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { packet->data[0] = 1; send_udppacket(n, packet); } else { + n->status.udp_confirmed = true; + if(n->mtuprobes > 30) { if(n->minmtu) n->mtuprobes = 30; @@ -528,42 +531,77 @@ static void send_udppacket(node_t *n, vpn_packet_t *origpkt) { inpkt->len += digest_length(&n->outdigest); } - /* Determine which socket we have to use */ - - if(n->address.sa.sa_family != listen_socket[n->sock].sa.sa.sa_family) { - for(int sock = 0; sock < listen_sockets; sock++) { - if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family) { - n->sock = sock; - break; - } - } - } - /* Send the packet */ - struct sockaddr *sa; - socklen_t sl; + sockaddr_t *sa; int sock; /* Overloaded use of priority field: -1 means local broadcast */ if(origpriority == -1 && n->prevedge) { - struct sockaddr_in in; - in.sin_family = AF_INET; - in.sin_addr.s_addr = -1; - in.sin_port = n->prevedge->address.in.sin_port; - sa = (struct sockaddr *)∈ - sl = sizeof in; + sockaddr_t broadcast; + broadcast.in.sin_family = AF_INET; + broadcast.in.sin_addr.s_addr = -1; + broadcast.in.sin_port = n->prevedge->address.in.sin_port; + sa = &broadcast; sock = 0; } else { if(origpriority == -1) origpriority = 0; - sa = &(n->address.sa); - sl = SALEN(n->address.sa); - sock = n->sock; + if(n->status.udp_confirmed) { + /* Address of this node is confirmed, so use it. */ + sa = &n->address; + sock = n->sock; + } else { + /* Otherwise, go through the list of known addresses of + this node. The first address we try is always the + one in n->address; that could be set to the node's + reflexive UDP address discovered during key + exchange. The other known addresses are those found + in edges to this node. */ + + static unsigned int i; + int j = 0; + edge_t *candidate = NULL; + + if(i) { + for splay_each(edge_t, e, edge_weight_tree) { + if(e->to != n) + continue; + j++; + if(!candidate || j == i) + candidate = e; + } + } + + if(!candidate) { + sa = &n->address; + sock = n->sock; + } else { + sa = &candidate->address; + sock = rand() % listen_sockets; + } + + if(i++) + if(i > j) + i = 0; + } } + /* Determine which socket we have to use */ + + if(sa->sa.sa_family != listen_socket[sock].sa.sa.sa_family) + for(sock = 0; sock < listen_sockets; sock++) + if(sa->sa.sa_family == listen_socket[sock].sa.sa.sa_family) + break; + + if(sock >= listen_sockets) + sock = 0; + + if(!n->status.udp_confirmed) + n->sock = sock; + #if defined(SOL_IP) && defined(IP_TOS) if(priorityinheritance && origpriority != priority && listen_socket[n->sock].sa.sa.sa_family == AF_INET) { @@ -574,7 +612,9 @@ static void send_udppacket(node_t *n, vpn_packet_t *origpkt) { } #endif - if(sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, sa, sl) < 0 && !sockwouldblock(sockerrno)) { + socklen_t sl = SALEN(n->address.sa); + + if(sendto(listen_socket[sock].udp, (char *) &inpkt->seqno, inpkt->len, 0, &sa->sa, sl) < 0 && !sockwouldblock(sockerrno)) { if(sockmsgsize(sockerrno)) { if(n->maxmtu >= origlen) n->maxmtu = origlen - 1; diff --git a/src/node.h b/src/node.h index d6d07d42..d0dccf3e 100644 --- a/src/node.h +++ b/src/node.h @@ -35,7 +35,8 @@ typedef struct node_status_t { unsigned int reachable:1; /* 1 if this node is reachable in the graph */ unsigned int indirect:1; /* 1 if this node is not directly reachable by us */ unsigned int sptps:1; /* 1 if this node supports SPTPS */ - unsigned int unused:25; + unsigned int udp_confirmed:1; /* 1 if the address is one that we received UDP traffic on */ + unsigned int unused:24; } node_status_t; typedef struct node_t {