improve the reliability of gratuitous arp by sending both request and reply, also...
[project/relayd.git] / main.c
diff --git a/main.c b/main.c
index 2f3bae8..e2886de 100644 (file)
--- a/main.c
+++ b/main.c
@@ -206,15 +206,15 @@ void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8
        }
 }
 
-static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4],
-                           uint8_t tha[ETH_ALEN], uint8_t tpa[4])
+static void send_arp_reply(struct relayd_interface *rif, const uint8_t spa[4],
+                           const uint8_t tha[ETH_ALEN], const uint8_t tpa[4])
 {
        struct arp_packet pkt;
 
        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, 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,6 +233,20 @@ static void send_arp_reply(struct relayd_interface *rif, 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)
@@ -291,6 +306,19 @@ static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t
        return host;
 }
 
+static void send_gratuitous_arp(struct relayd_interface *rif, const uint8_t *spa)
+{
+       struct relayd_interface *to_rif;
+
+       list_for_each_entry(to_rif, &interfaces, list) {
+               if (rif == to_rif)
+                       continue;
+
+               send_arp_reply(to_rif, spa, NULL, spa);
+       }
+}
+
+
 struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
 {
        struct relayd_host *host;
@@ -315,6 +343,7 @@ struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint
        } else {
                host->cleanup_pending = false;
                uloop_timeout_set(&host->timeout, host_timeout * 1000);
+               send_gratuitous_arp(rif, ipaddr);
        }
 
        return host;
@@ -331,7 +360,9 @@ static void relay_arp_request(struct relayd_interface *from_rif, struct arp_pack
                        continue;
 
                memcpy(reqpkt.eth.ether_shost, rif->sll.sll_addr, ETH_ALEN);
+               memset(reqpkt.eth.ether_dhost, 0xff, ETH_ALEN);
                memcpy(reqpkt.arp.arp_sha, rif->sll.sll_addr, ETH_ALEN);
+               memset(reqpkt.arp.arp_tha, 0, ETH_ALEN);
 
                DPRINTF(2, "%s: sending ARP who-has "IP_FMT", tell "IP_FMT" ("MAC_FMT")\n",
                        rif->ifname, IP_BUF(reqpkt.arp.arp_tpa),
@@ -378,10 +409,8 @@ static void recv_arp_request(struct relayd_interface *rif, struct arp_packet *pk
        relay_arp_request(rif, pkt);
 }
 
-
 static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
 {
-       struct relayd_interface *to_rif;
        struct relayd_host *host;
 
        DPRINTF(2, "%s: received ARP reply for "IP_FMT" from "MAC_FMT", deliver to "IP_FMT"\n",
@@ -393,20 +422,6 @@ static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
        if (memcmp(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN) != 0)
                relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
 
-       if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4)) {
-               /*
-                * locally initiated lookup, relay as gratuitous ARP
-                * to all other interfaces
-                */
-               list_for_each_entry(to_rif, &interfaces, list) {
-                       if (rif == to_rif)
-                               continue;
-
-                       send_arp_reply(to_rif, pkt->arp.arp_spa, NULL, pkt->arp.arp_spa);
-               }
-               return;
-       }
-
        host = find_host_by_ipaddr(NULL, pkt->arp.arp_tpa);
        if (!host)
                return;