Make sure PriorityInheritance also works in switch mode.
[tinc] / src / net_packet.c
index b11949a..2780f51 100644 (file)
@@ -500,17 +500,27 @@ static void send_udppacket(node_t *n, vpn_packet_t *origpkt) {
        struct sockaddr *sa;
        socklen_t sl;
        int sock;
+       sockaddr_t broadcast;
 
        /* 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;
-               sock = 0;
+               sock = rand() % listen_sockets;
+               memset(&broadcast, 0, sizeof broadcast);
+               if(listen_socket[sock].sa.sa.sa_family == AF_INET6) {
+                       broadcast.in6.sin6_family = AF_INET6;
+                       broadcast.in6.sin6_addr.s6_addr[0x0] = 0xff;
+                       broadcast.in6.sin6_addr.s6_addr[0x1] = 0x02;
+                       broadcast.in6.sin6_addr.s6_addr[0xf] = 0x01;
+                       broadcast.in6.sin6_port = n->prevedge->address.in.sin_port;
+                       broadcast.in6.sin6_scope_id = listen_socket[sock].sa.in6.sin6_scope_id;
+               } else {
+                       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.sa;
+               sl = SALEN(broadcast.sa);
        } else {
                if(origpriority == -1)
                        origpriority = 0;
@@ -584,24 +594,50 @@ void send_packet(const node_t *n, vpn_packet_t *packet) {
 void broadcast_packet(const node_t *from, vpn_packet_t *packet) {
        avl_node_t *node;
        connection_t *c;
+       node_t *n;
+
+       // Always give ourself a copy of the packet.
+       if(from != myself)
+               send_packet(myself, packet);
+
+       // In TunnelServer mode, do not forward broadcast packets.
+        // The MST might not be valid and create loops.
+       if(tunnelserver || broadcast_mode == BMODE_NONE)
+               return;
 
        ifdebug(TRAFFIC) logger(LOG_INFO, "Broadcasting packet of %d bytes from %s (%s)",
                           packet->len, from->name, from->hostname);
 
-       if(from != myself) {
-               send_packet(myself, packet);
+       switch(broadcast_mode) {
+               // In MST mode, broadcast packets travel via the Minimum Spanning Tree.
+               // This guarantees all nodes receive the broadcast packet, and
+               // usually distributes the sending of broadcast packets over all nodes.
+               case BMODE_MST:
+                       for(node = connection_tree->head; node; node = node->next) {
+                               c = node->data;
 
-               // In TunnelServer mode, do not forward broadcast packets.
-                // The MST might not be valid and create loops.
-               if(tunnelserver)
-                       return;
-       }
+                               if(c->status.active && c->status.mst && c != from->nexthop->connection)
+                                       send_packet(c->node, packet);
+                       }
+                       break;
 
-       for(node = connection_tree->head; node; node = node->next) {
-               c = node->data;
+               // In direct mode, we send copies to each node we know of.
+               // However, this only reaches nodes that can be reached in a single hop.
+               // We don't have enough information to forward broadcast packets in this case.
+               case BMODE_DIRECT:
+                       if(from != myself)
+                               break;
+
+                       for(node = node_udp_tree->head; node; node = node->next) {
+                               n = node->data;
+
+                               if(n->status.reachable && ((n->via == myself && n->nexthop == n) || n->via == n))
+                                       send_packet(n, packet);
+                       }
+                       break;
 
-               if(c->status.active && c->status.mst && c != from->nexthop->connection)
-                       send_packet(c->node, packet);
+               default:
+                       break;
        }
 }