improve host detection on interface change
[project/relayd.git] / main.c
diff --git a/main.c b/main.c
index 3196a1e..318ba1c 100644 (file)
--- a/main.c
+++ b/main.c
@@ -213,8 +213,8 @@ static void send_arp_reply(struct relayd_interface *rif, const uint8_t spa[4],
 
        fill_arp_packet(&pkt, rif, spa, tpa);
 
-       pkt.arp.arp_op = htons(ARPOP_REPLY);
        if (tha) {
+               pkt.arp.arp_op = htons(ARPOP_REPLY);
                memcpy(pkt.eth.ether_dhost, tha, ETH_ALEN);
                memcpy(pkt.arp.arp_tha, tha, ETH_ALEN);
 
@@ -222,8 +222,9 @@ static void send_arp_reply(struct relayd_interface *rif, const uint8_t spa[4],
                        rif->ifname, IP_BUF(pkt.arp.arp_tpa),
                        IP_BUF(pkt.arp.arp_spa), MAC_BUF(pkt.eth.ether_shost));
        } else {
+               pkt.arp.arp_op = htons(ARPOP_REQUEST);
                memset(pkt.eth.ether_dhost, 0xff, ETH_ALEN);
-               memset(pkt.arp.arp_tha, 0, ETH_ALEN);
+               memset(pkt.arp.arp_tha, 0xff, ETH_ALEN);
 
                DPRINTF(2, "%s: sending gratuitous ARP: "IP_FMT" is at ("MAC_FMT")\n",
                        rif->ifname, IP_BUF(pkt.arp.arp_tpa),
@@ -232,11 +233,26 @@ static void send_arp_reply(struct relayd_interface *rif, const uint8_t spa[4],
 
        sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
                (struct sockaddr *) &rif->sll, sizeof(rif->sll));
+
+       if (tha)
+               return;
+
+       /*
+        * Gratuitous ARP comes in two flavours, request and reply.
+        * Some operating systems only accept request, some only reply.
+        * Let's just send both...
+        */
+       pkt.arp.arp_op = htons(ARPOP_REPLY);
+
+       sendto(rif->fd.fd, &pkt, sizeof(pkt), 0,
+               (struct sockaddr *) &rif->sll, sizeof(rif->sll));
+
 }
 
 static void host_entry_timeout(struct uloop_timeout *timeout)
 {
        struct relayd_host *host = container_of(timeout, struct relayd_host, timeout);
+       struct relayd_interface *rif;
 
        /*
         * When a host is behind a managed interface, we must not expire its host
@@ -246,7 +262,9 @@ static void host_entry_timeout(struct uloop_timeout *timeout)
         * giving up on it.
         */
        if (host->rif->managed && host->cleanup_pending < host_ping_tries) {
-               send_arp_request(host->rif, host->ipaddr);
+               list_for_each_entry(rif, &interfaces, list) {
+                       send_arp_request(rif, host->ipaddr);
+               }
                host->cleanup_pending++;
                uloop_timeout_set(&host->timeout, 1000);
                return;
@@ -371,15 +389,15 @@ static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pk
        if (!memcmp(pkt->arp.arp_spa, "\x00\x00\x00\x00", 4))
                return;
 
+       host = find_host_by_ipaddr(NULL, pkt->arp.arp_spa);
+       if (!host || host->rif != rif)
+               relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
+
        if (local_route_table && !memcmp(pkt->arp.arp_tpa, local_addr, sizeof(local_addr))) {
                send_arp_reply(rif, local_addr, pkt->arp.arp_sha, pkt->arp.arp_spa);
                return;
        }
 
-       host = find_host_by_ipaddr(NULL, pkt->arp.arp_spa);
-       if (!host || host->rif != rif)
-               relayd_refresh_host(rif, pkt->eth.ether_shost, pkt->arp.arp_spa);
-
        host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
 
        /*
@@ -571,7 +589,7 @@ static int init_interface(struct relayd_interface *rif)
        }
 
 #ifdef PACKET_RECV_TYPE
-       pkt_type = (1 << PACKET_BROADCAST);
+       pkt_type = (1 << PACKET_BROADCAST) | (1 << PACKET_MULTICAST);
        setsockopt(fd, SOL_PACKET, PACKET_RECV_TYPE, &pkt_type, sizeof(pkt_type));
 #endif
 
@@ -704,7 +722,7 @@ int main(int argc, char **argv)
        local_route_table = 0;
        uloop_init();
 
-       while ((ch = getopt(argc, argv, "I:i:t:BDdT:G:R:L:")) != -1) {
+       while ((ch = getopt(argc, argv, "I:i:t:p:BDdT:G:R:L:")) != -1) {
                switch(ch) {
                case 'I':
                        managed = true;