Ensure that NMRP packets are at least 64 bytes
[oweals/nmrpflash.git] / ethsock.c
index 024d77df6147402ba2f6b2c21303df0f3b2de590..5798d1cdf5f391ad1eb0ccb1e47bc2dd70421455 100644 (file)
--- a/ethsock.c
+++ b/ethsock.c
@@ -1,8 +1,28 @@
+/**
+ * nmrpflash - Netgear Unbrick Utility
+ * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
+ *
+ * nmrpflash is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * nmrpflash is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with nmrpflash.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 #include <sys/types.h>
 #include <stdbool.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <fcntl.h>
 #include "nmrpd.h"
 
 #if defined(NMRPFLASH_WINDOWS)
@@ -21,6 +41,7 @@
 #else
 #define NMRPFLASH_AF_PACKET AF_LINK
 #include <net/if_types.h>
+#include <net/if_media.h>
 #endif
 #endif
 
@@ -30,6 +51,9 @@ struct ethsock
        pcap_t *pcap;
 #ifndef NMRPFLASH_WINDOWS
        int fd;
+#ifdef NMRPFLASH_LINUX
+       bool stp;
+#endif
 #else
        HANDLE handle;
        DWORD index;
@@ -38,9 +62,15 @@ struct ethsock
        uint8_t hwaddr[6];
 };
 
+struct ethsock_arp_undo
+{
+       uint32_t ipaddr;
+       uint8_t hwaddr[6];
+};
+
 struct ethsock_ip_undo
 {
-#ifndef NRMPFLASH_WINDOWS
+#ifndef NMRPFLASH_WINDOWS
        uint32_t ip[2];
 #else
        ULONG context;
@@ -88,21 +118,71 @@ static inline bool sockaddr_get_hwaddr(struct sockaddr *sa, uint8_t *hwaddr)
        return true;
 }
 
-static bool get_intf_info(const char *intf, uint8_t *hwaddr, void *dummy)
+#ifdef NMRPFLASH_LINUX
+static int open_stp_state(const char *intf)
+{
+       char name[256];
+       snprintf(name, sizeof(name), "/sys/class/net/%s/bridge/stp_state", intf);
+       return open(name, O_RDWR, 0644);
+}
+
+static bool is_stp_enabled(const char *intf)
+{
+       char c;
+       int fd = open_stp_state(intf);
+       if (fd == -1) {
+               return false;
+       }
+
+       if (read(fd, &c, 1) != 1) {
+               c = '0';
+       }
+
+       close(fd);
+       return c == '1';
+}
+
+static bool set_stp_enabled(const char *intf, bool enabled)
+{
+       bool ret;
+       const char *s = enabled ? "1\n" : "0\n";
+       int fd = open_stp_state(intf);
+       if (fd == -1) {
+               return false;
+       }
+
+       ret = (write(fd, s, 2) == 2);
+       close(fd);
+
+       return ret;
+}
+#endif
+
+static bool get_intf_info(const char *intf, uint8_t *hwaddr, bool *bridge)
 {
        struct ifaddrs *ifas, *ifa;
        bool found;
 
        if (getifaddrs(&ifas) != 0) {
-               perror("getifaddrs");
+               xperror("getifaddrs");
                return false;
        }
 
        found = false;
+       *bridge = false;
+
+       if (bridge) {
+               *bridge = false;
+       }
 
        for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
                if (!strcmp(ifa->ifa_name, intf)) {
                        if (sockaddr_get_hwaddr(ifa->ifa_addr, hwaddr)) {
+#ifdef NMRPFLASH_BSD
+                               if (bridge) {
+                                       *bridge = ((struct if_data*) ifa->ifa_data)->ifi_type == IFT_BRIDGE;
+                               }
+#endif
                                found = true;
                                break;
                        }
@@ -146,13 +226,13 @@ static bool get_intf_info(const char *intf, uint8_t *hwaddr, DWORD *index)
 
        adapters = malloc(bufLen);
        if (!adapters) {
-               perror("malloc");
+               xperror("malloc");
                return false;
        }
 
        if ((ret = GetAdaptersInfo(adapters, &bufLen) == NO_ERROR)) {
                for (adapter = adapters; adapter; adapter = adapter->Next) {
-                       if (adapter->Type != MIB_IF_TYPE_ETHERNET) {
+                       if (adapter->Type != MIB_IF_TYPE_ETHERNET && adapter->Type != IF_TYPE_IEEE80211) {
                                continue;
                        }
 
@@ -268,14 +348,9 @@ struct ethsock *ethsock_create(const char *intf, uint16_t protocol)
        char buf[PCAP_ERRBUF_SIZE];
        struct bpf_program fp;
        struct ethsock *sock;
+       bool is_bridge;
        int err;
 
-       sock = malloc(sizeof(struct ethsock));
-       if (!sock) {
-               perror("malloc");
-               return NULL;
-       }
-
 #ifdef NMRPFLASH_WINDOWS
        intf = intf_alias_to_wpcap(intf);
        if (!intf) {
@@ -283,13 +358,19 @@ struct ethsock *ethsock_create(const char *intf, uint16_t protocol)
        }
 #endif
 
+       sock = malloc(sizeof(struct ethsock));
+       if (!sock) {
+               xperror("malloc");
+               return NULL;
+       }
+
        buf[0] = '\0';
 
        sock->intf = intf;
        sock->pcap = pcap_open_live(sock->intf, BUFSIZ, 1, 1, buf);
        if (!sock->pcap) {
                fprintf(stderr, "%s.\n", buf);
-               goto cleanup_malloc;
+               goto cleanup;
        }
 
        if (*buf) {
@@ -299,36 +380,36 @@ struct ethsock *ethsock_create(const char *intf, uint16_t protocol)
        if (pcap_datalink(sock->pcap) != DLT_EN10MB) {
                fprintf(stderr, "%s is not an ethernet interface.\n",
                                intf);
-               goto cleanup_pcap;
+               goto cleanup;
        }
 
 #ifndef NMRPFLASH_WINDOWS
-       err = !get_intf_info(intf, sock->hwaddr, NULL);
+       err = !get_intf_info(intf, sock->hwaddr, &is_bridge);
 #else
        err = !get_intf_info(intf, sock->hwaddr, &sock->index);
 #endif
        if (err) {
                fprintf(stderr, "Failed to get interface info.\n");
-               goto cleanup_malloc;
+               goto cleanup;
        }
 
 #ifndef NMRPFLASH_WINDOWS
        sock->fd = pcap_get_selectable_fd(sock->pcap);
        if (sock->fd == -1) {
                pcap_perror(sock->pcap, "pcap_get_selectable_fd");
-               goto cleanup_pcap;
+               goto cleanup;
        }
 #else
        sock->handle = pcap_getevent(sock->pcap);
        if (!sock->handle) {
                pcap_perror(sock->pcap, "pcap_getevent");
-               goto cleanup_pcap;
+               goto cleanup;
        }
 
        err = pcap_setmintocopy(sock->pcap, 1);
        if (err) {
                pcap_perror(sock->pcap, "pcap_setmintocopy");
-               goto cleanup_pcap;
+               goto cleanup;
        }
 #endif
 
@@ -338,7 +419,7 @@ struct ethsock *ethsock_create(const char *intf, uint16_t protocol)
        err = pcap_compile(sock->pcap, &fp, buf, 0, 0);
        if (err) {
                pcap_perror(sock->pcap, "pcap_compile");
-               goto cleanup_pcap;
+               goto cleanup;
        }
 
        err = pcap_setfilter(sock->pcap, &fp);
@@ -346,15 +427,27 @@ struct ethsock *ethsock_create(const char *intf, uint16_t protocol)
 
        if (err) {
                pcap_perror(sock->pcap, "pcap_setfilter");
-               goto cleanup_pcap;
+               goto cleanup;
+       }
+
+#ifdef NMRPFLASH_LINUX
+       // nmrpflash does not work on bridge interfaces with STP enabled
+       if ((sock->stp = is_stp_enabled(intf))) {
+               if (!set_stp_enabled(intf, false)) {
+                       fprintf(stderr, "Warning: failed to disable STP on %s.\n", intf);
+               }
+       }
+#else
+       if (is_bridge) {
+               fprintf(stderr, "Warning: bridge interfaces are not fully "
+                               "supported on this platform.\n");
        }
+#endif
 
        return sock;
 
-cleanup_pcap:
-       pcap_close(sock->pcap);
-cleanup_malloc:
-       free(sock);
+cleanup:
+       ethsock_close(sock);
        return NULL;
 }
 
@@ -443,7 +536,19 @@ int ethsock_send(struct ethsock *sock, void *buf, size_t len)
 
 int ethsock_close(struct ethsock *sock)
 {
-       pcap_close(sock->pcap);
+       if (!sock) {
+               return 0;
+       }
+
+#ifdef NMRPFLASH_LINUX
+       if (sock->stp) {
+               set_stp_enabled(sock->intf, true);
+       }
+#endif
+       if (sock->pcap) {
+               pcap_close(sock->pcap);
+       }
+
        free(sock);
        return 0;
 }
@@ -455,55 +560,72 @@ inline int ethsock_set_timeout(struct ethsock *sock, unsigned msec)
 }
 
 #ifndef NMRPFLASH_WINDOWS
-int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
+int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
 {
        return 0;
 }
 
-int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
+int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo)
 {
        return 0;
 }
 #else
-static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr, int add)
+static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
 {
        DWORD ret;
        MIB_IPNETROW arp = {
                .dwIndex = sock->index,
                .dwPhysAddrLen = 6,
-               .dwAddr = ipaddr->s_addr,
+               .dwAddr = ipaddr,
                .dwType = MIB_IPNET_TYPE_STATIC
        };
-       
+
        memcpy(arp.bPhysAddr, hwaddr, 6);
-       
-       if (add) {
+
+       if (undo) {
                ret = CreateIpNetEntry(&arp);
                if (ret != NO_ERROR) {
                        win_perror2("CreateIpNetEntry", ret);
                        return -1;
                }
+
+               *undo = malloc(sizeof(struct ethsock_arp_undo));
+               if (!*undo) {
+                       xperror("malloc");
+                       return -1;
+               }
+
+               (*undo)->ipaddr = ipaddr;
+               memcpy((*undo)->hwaddr, hwaddr, 6);
        } else {
                DeleteIpNetEntry(&arp);
        }
-       
+
        return 0;
 }
 
-int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
+int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, uint32_t ipaddr, struct ethsock_arp_undo **undo)
 {
-       ethsock_arp_del(sock, hwaddr, ipaddr);
-       return ethsock_arp(sock, hwaddr, ipaddr, 1);
+       ethsock_arp(sock, hwaddr, ipaddr, NULL);
+       return undo ? ethsock_arp(sock, hwaddr, ipaddr, undo) : -1;
 }
 
-int ethsock_arp_del(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
+int ethsock_arp_del(struct ethsock *sock, struct ethsock_arp_undo **undo)
 {
-       return ethsock_arp(sock, hwaddr, ipaddr, 0);
+       if (!*undo) {
+               return 0;
+       }
+
+       int ret = ethsock_arp(sock, (*undo)->hwaddr, (*undo)->ipaddr, NULL);
+       free(*undo);
+       *undo = NULL;
+       return ret;
 }
 #endif
 
 static bool get_hwaddr_from_pcap(const pcap_if_t *dev, uint8_t *hwaddr)
 {
+       bool is_bridge;
 #ifndef NMRPFLASH_WINDOWS
        pcap_addr_t *addr;
        int i;
@@ -524,7 +646,7 @@ static bool get_hwaddr_from_pcap(const pcap_if_t *dev, uint8_t *hwaddr)
        }
 #endif
 
-       return get_intf_info(dev->name, hwaddr, NULL);
+       return get_intf_info(dev->name, hwaddr, &is_bridge);
 }
 
 int ethsock_list_all(void)
@@ -644,21 +766,26 @@ int ethsock_for_each_ip(struct ethsock *sock, ethsock_ip_callback_t callback,
        return status <= 0 ? status : 0;
 }
 
-#ifndef NMRPFLASH_WINDOWS
 static inline void set_addr(void *p, uint32_t addr)
 {
        struct sockaddr_in* sin = p;
        sin->sin_family = AF_INET;
        sin->sin_addr.s_addr = addr;
+#ifdef NMRPFLASH_BSD
+       ((struct sockaddr*)p)->sa_len = sizeof(struct sockaddr_in);
+#endif
 }
 
+#ifndef NMRPFLASH_WINDOWS
 static bool set_interface_up(int fd, const char *intf, bool up)
 {
        struct ifreq ifr;
        strncpy(ifr.ifr_name, intf, IFNAMSIZ);
 
        if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
-               perror("ioctl(SIOCGIFFLAGS)");
+               if (up) {
+                       xperror("ioctl(SIOCGIFFLAGS)");
+               }
                return false;
        }
 
@@ -669,7 +796,9 @@ static bool set_interface_up(int fd, const char *intf, bool up)
        }
 
        if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
-               perror("ioctl(SIOCSIFFLAGS)");
+               if (up) {
+                       xperror("ioctl(SIOCSIFFLAGS)");
+               }
                return false;
        }
 
@@ -681,35 +810,36 @@ static bool set_interface_up(int fd, const char *intf, bool up)
 int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo)
 {
        if (undo && !(*undo = malloc(sizeof(struct ethsock_ip_undo)))) {
-               perror("malloc");
+               xperror("malloc");
                return -1;
        }
 
-#ifndef NMRPFLASH_WINDOWS
        int ret = -1;
        int fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (!fd) {
-               perror("socket");
-               return -1;
+               sock_perror("socket");
+               goto out;
        }
 
+#ifndef NMRPFLASH_WINDOWS
+       // XXX: undo is non-zero only if we're adding an IP
+       bool add = undo;
 #ifdef NMRPFLASH_LINUX
        struct ifreq ifr;
        strncpy(ifr.ifr_name, sock->intf, IFNAMSIZ);
        // FIXME: automatically determine the next free alias
        strcat(ifr.ifr_name, ":42");
 
-       // XXX: undo is non-zero only if we're actually adding an ip
-       if (undo) {
+       if (add) {
                set_addr(&ifr.ifr_addr, ipaddr);
                if (ioctl(fd, SIOCSIFADDR, &ifr) != 0) {
-                       perror("ioctl(SIOSIFADDR)");
+                       xperror("ioctl(SIOSIFADDR)");
                        goto out;
                }
 
                set_addr(&ifr.ifr_netmask, ipmask);
                if (ioctl(fd, SIOCSIFNETMASK, &ifr) != 0) {
-                       perror("ioctl(SIOCSIFNETMASK)");
+                       xperror("ioctl(SIOCSIFNETMASK)");
                        goto out;
                }
 
@@ -717,48 +847,72 @@ int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struc
                (*undo)->ip[1] = ipmask;
        }
 
-       if (!set_interface_up(fd, ifr.ifr_name, undo ? true : false)) {
+       if (!set_interface_up(fd, ifr.ifr_name, add)) {
                goto out;
        }
 #else // NMRPFLASH_OSX (or any other BSD)
        struct ifaliasreq ifra;
+       memset(&ifra, 0, sizeof(ifra));
        strncpy(ifra.ifra_name, sock->intf, IFNAMSIZ);
 
        set_addr(&ifra.ifra_addr, ipaddr);
        set_addr(&ifra.ifra_mask, ipmask);
        //set_addr(&ifra.ifra_broadaddr, (ipaddr & ipmask) | ~ipmask);
-       memset(&ifra.ifra_broadaddr, 0, sizeof(ifra.ifra_broadaddr));
-
-       // XXX: undo is non-zero only if we're actually adding an ip
 
-       if (ioctl(fd, undo ? SIOCAIFADDR : SIOCDIFADDR, &ifra) != 0) {
-               perror("ioctl(SIOCAIFADDR)");
+       if (ioctl(fd, add ? SIOCAIFADDR : SIOCDIFADDR, &ifra) != 0) {
+               if (add) {
+                       xperror("ioctl(SIOCAIFADDR");
+               }
                goto out;
        }
 
-       if (undo) {
+       if (add) {
                (*undo)->ip[0] = ipaddr;
                (*undo)->ip[1] = ipmask;
                set_interface_up(fd, ifra.ifra_name, true);
        }
 
 #endif
-       ret = 0;
-
-out:
-       close(fd);
-       return ret;
 #else // NMRPFLASH_WINDOWS
+       struct sockaddr_in sin;
        ULONG instance;
 
-       DWORD ret = AddIPAddress(ipaddr, ipmask, sock->index, &undo->context, &instance);
-       if (ret != NO_ERROR) {
-               win_perror2("AddIPAddress", ret);
-               return -1;
+       (*undo)->context = 0;
+
+       DWORD err = AddIPAddress(ipaddr, ipmask, sock->index, &(*undo)->context, &instance);
+       if (err != NO_ERROR && err != ERROR_DUP_DOMAINNAME && err != ERROR_OBJECT_ALREADY_EXISTS) {
+               win_perror2("AddIPAddress", err);
+               goto out;
        }
 
-       return 0;
+       set_addr(&sin, ipaddr);
+       time_t beg = time_monotonic();
+
+       /* Wait until the new IP has actually been added */
+
+       while (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
+               if ((time_monotonic() - beg) >= 5) {
+                       fprintf(stderr, "Failed to bind after 5 seconds: ");
+                       sock_perror("bind");
+                       DeleteIPAddress((*undo)->context);
+                       goto out;
+               }
+       }
+#endif
+       ret = 0;
+
+out:
+#ifndef NMRPFLASH_WINDOWS
+       close(fd);
+#else
+       closesocket(fd);
 #endif
+       if (ret != 0 && undo) {
+               free(*undo);
+               *undo = NULL;
+       }
+
+       return ret;
 }
 
 int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo)
@@ -776,13 +930,8 @@ int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo)
                ret = 0;
        }
 #else
-       DWORD err = DeleteIPAddress((*undo)->context);
-       if (err != NO_ERROR) {
-               win_perror2("DeleteIPAddress", ret);
-               ret = -1;
-       } else {
-               ret = 0;
-       }
+       DeleteIPAddress((*undo)->context);
+       ret = 0;
 #endif
 
        free(*undo);