]> git.meshlink.io Git - meshlink/blobdiff - src/net_packet.c
Fix potential NULL pointer dereference.
[meshlink] / src / net_packet.c
index 8bc363d229a8f45350a09e3d18a53ad5ca264681..b32c78f7d467456166e9551ca211242a651361f7 100644 (file)
@@ -102,7 +102,7 @@ static void send_mtu_probe_handler(event_loop_t *loop, void *data) {
        }
 
        if(n->mtuprobes == 31) {
-               if(!n->minmtu && n->status.want_udp) {
+               if(!n->minmtu && n->status.want_udp && n->nexthop && n->nexthop->connection) {
                        /* Send a dummy ANS_KEY to try to update the reflexive UDP address */
                        send_request(mesh, n->nexthop->connection, NULL, "%d %s %s . -1 -1 -1 0", ANS_KEY, mesh->self->name, n->name);
                        n->status.want_udp = false;
@@ -188,7 +188,13 @@ static void mtu_probe_h(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *packet
                if(!n->status.udp_confirmed) {
                        char *address, *port;
                        sockaddr2str(&n->address, &address, &port);
-                       send_request(mesh, n->nexthop->connection, NULL, "%d %s %s . -1 -1 -1 0 %s %s", ANS_KEY, n->name, n->name, address, port);
+
+                       if(n->nexthop && n->nexthop->connection) {
+                               send_request(mesh, n->nexthop->connection, NULL, "%d %s %s . -1 -1 -1 0 %s %s", ANS_KEY, n->name, n->name, address, port);
+                       } else {
+                               logger(mesh, MESHLINK_WARNING, "Cannot send reflexive address to %s via %s", n->name, n->nexthop ? n->nexthop->name : n->name);
+                       }
+
                        free(address);
                        free(port);
                        n->status.udp_confirmed = true;
@@ -245,6 +251,11 @@ static bool try_mac(meshlink_handle_t *mesh, node_t *n, const vpn_packet_t *inpk
 }
 
 static void receive_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *inpkt) {
+       if(!n->status.reachable) {
+               logger(mesh, MESHLINK_ERROR, "Got SPTPS data from unreachable node %s", n->name);
+               return;
+       }
+
        if(!n->sptps.state) {
                if(!n->status.waitingforkey) {
                        logger(mesh, MESHLINK_DEBUG, "Got packet from %s but we haven't exchanged keys yet", n->name);
@@ -262,6 +273,11 @@ static void receive_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *
 }
 
 static void send_sptps_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *origpkt) {
+       if(!n->status.reachable) {
+               logger(mesh, MESHLINK_ERROR, "Trying to send SPTPS data to unreachable node %s", n->name);
+               return;
+       }
+
        if(!n->status.validkey) {
                logger(mesh, MESHLINK_INFO, "No valid key known yet for %s", n->name);
 
@@ -317,14 +333,17 @@ static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const s
        /* Otherwise, address are found in edges to this node.
           So we pick a random edge and a random socket. */
 
-       int i = 0;
-       int j = prng(mesh, n->edge_tree->count);
        edge_t *candidate = NULL;
 
-       for splay_each(edge_t, e, n->edge_tree) {
-               if(i++ == j) {
-                       candidate = e->reverse;
-                       break;
+       {
+               int i = 0;
+               int j = prng(mesh, n->edge_tree->count);
+
+               for splay_each(edge_t, e, n->edge_tree) {
+                       if(i++ == j) {
+                               candidate = e->reverse;
+                               break;
+                       }
                }
        }
 
@@ -334,6 +353,7 @@ static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const s
        }
 
 check_socket:
+
        /* Make sure we have a suitable socket for the chosen address */
        if(mesh->listen_socket[*sock].sa.sa.sa_family != (*sa)->sa.sa_family) {
                for(int i = 0; i < mesh->listen_sockets; i++) {
@@ -375,12 +395,22 @@ bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
        node_t *to = handle;
        meshlink_handle_t *mesh = to->mesh;
 
+       if(!to->status.reachable) {
+               logger(mesh, MESHLINK_ERROR, "Trying to send SPTPS data to unreachable node %s", to->name);
+               return false;
+       }
+
        /* Send it via TCP if it is a handshake packet, TCPOnly is in use, or this packet is larger than the MTU. */
 
        if(type >= SPTPS_HANDSHAKE || (type != PKT_PROBE && (len - 21) > to->minmtu)) {
                char buf[len * 4 / 3 + 5];
                b64encode(data, buf, len);
 
+               if(!to->nexthop || !to->nexthop->connection) {
+                       logger(mesh, MESHLINK_WARNING, "Unable to forward SPTPS packet to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
+                       return false;
+               }
+
                /* If no valid key is known yet, send the packets using ANS_KEY requests,
                   to ensure we get to learn the reflexive UDP address. */
                if(!to->status.validkey) {