Add TFTP upload spinner
[oweals/nmrpflash.git] / ethsock.c
index be1031d8e87f27a1643d64a876d3f7b9e04a3bfd..b346ce9c2fc371a8c23770e923336a640ed10b3b 100644 (file)
--- a/ethsock.c
+++ b/ethsock.c
@@ -1,8 +1,29 @@
+/**
+ * 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 <stdarg.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <fcntl.h>
 #include "nmrpd.h"
 
 #if defined(NMRPFLASH_WINDOWS)
 #if defined(NMRPFLASH_LINUX)
 #define NMRPFLASH_AF_PACKET AF_PACKET
 #include <linux/if_packet.h>
+#include <netlink/route/addr.h>
+#include <netlink/route/neighbour.h>
 #else
 #define NMRPFLASH_AF_PACKET AF_LINK
 #include <net/if_types.h>
+#include <net/if_media.h>
 #endif
 #endif
 
@@ -30,6 +54,9 @@ struct ethsock
        pcap_t *pcap;
 #ifndef NMRPFLASH_WINDOWS
        int fd;
+#ifdef NMRPFLASH_LINUX
+       bool stp;
+#endif
 #else
        HANDLE handle;
        DWORD index;
@@ -38,6 +65,12 @@ struct ethsock
        uint8_t hwaddr[6];
 };
 
+struct ethsock_arp_undo
+{
+       uint32_t ipaddr;
+       uint8_t hwaddr[6];
+};
+
 struct ethsock_ip_undo
 {
 #ifndef NMRPFLASH_WINDOWS
@@ -66,6 +99,26 @@ static int x_pcap_findalldevs(pcap_if_t **devs)
        return 0;
 }
 
+#ifndef NMRPFLASH_LINUX
+static int systemf(const char *fmt, ...)
+{
+       char cmd[1024];
+       int ret;
+       va_list va;
+       va_start(va, fmt);
+
+       ret = vsnprintf(cmd, sizeof(cmd) - 1, fmt, va);
+       if (ret >= sizeof(cmd) - 1) {
+               return -1;
+       }
+
+       ret = system(cmd);
+       va_end(va);
+
+       return ret;
+}
+#endif
+
 #ifndef NMRPFLASH_WINDOWS
 static inline bool sockaddr_get_hwaddr(struct sockaddr *sa, uint8_t *hwaddr)
 {
@@ -88,21 +141,228 @@ 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 bridge_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 bridge_stp_enabled(const char *intf)
+{
+       char c;
+       int fd = bridge_stp_state(intf);
+       if (fd == -1) {
+               return false;
+       }
+
+       if (read(fd, &c, 1) != 1) {
+               c = '0';
+       }
+
+       close(fd);
+       return c == '1';
+}
+
+static bool bridge_stp(const char *intf, bool enabled)
+{
+       bool ret;
+       const char *s = enabled ? "1\n" : "0\n";
+       int fd = bridge_stp_state(intf);
+       if (fd == -1) {
+               return false;
+       }
+
+       ret = (write(fd, s, 2) == 2);
+       close(fd);
+
+       return ret;
+}
+
+static struct nl_addr *build_ip(uint32_t ip)
+{
+       struct nl_addr *na = nl_addr_build(AF_INET, &ip, 4);
+       if (!na) {
+               xperror("nl_addr_build");
+       }
+
+       return na;
+}
+
+static struct nl_sock *xnl_socket_route()
+{
+       int err;
+       struct nl_sock *sk = nl_socket_alloc();
+       if (sk) {
+               if (!(err = nl_connect(sk, NETLINK_ROUTE))) {
+                       return sk;
+               }
+               nl_socket_free(sk);
+               nl_perror(err, "nl_connect");
+       } else {
+               xperror("nl_socket_alloc");
+       }
+
+       return NULL;
+}
+
+static bool intf_add_del_ip(const char *intf, uint32_t ipaddr, uint32_t ipmask, bool add)
+{
+       struct rtnl_addr *ra = NULL;
+       struct nl_sock *sk = NULL;
+       struct nl_addr *na = NULL;
+       int err = 1;
+
+       if (!(sk = xnl_socket_route())) {
+               return false;
+       }
+
+       if (!(ra = rtnl_addr_alloc())) {
+               xperror("rtnl_addr_alloc");
+               goto out;
+       }
+
+       rtnl_addr_set_ifindex(ra, if_nametoindex(intf));
+
+       if (!(na = build_ip(ipaddr))) {
+               goto out;
+       }
+
+       nl_addr_set_prefixlen(na, bitcount(ipmask));
+       rtnl_addr_set_local(ra, na);
+       nl_addr_put(na);
+
+       if (!(na = build_ip((ipaddr & ipmask) | ~ipmask))) {
+               goto out;
+       }
+
+       rtnl_addr_set_broadcast(ra, na);
+       nl_addr_put(na);
+
+       if ((err = add ? rtnl_addr_add(sk, ra, 0) : rtnl_addr_delete(sk, ra, 0)) < 0) {
+               if (add && err == -NLE_EXIST) {
+                       err = 0;
+               } else if (add || verbosity > 1) {
+                       nl_perror(err, add ? "rtnl_addr_add" : "rtnl_addr_delete");
+               }
+       }
+
+out:
+       rtnl_addr_put(ra);
+       nl_socket_free(sk);
+
+       return !err;
+}
+
+static bool intf_add_del_arp(const char *intf, uint32_t ipaddr, uint8_t *hwaddr, bool add)
+{
+#if 0
+       struct arpreq arp;
+       memset(&arp, 0, sizeof(arp));
+       arp.arp_ha.sa_family = ARPHRD_ETHER;
+       memcpy(&arp.arp_ha.sa_data, hwaddr, 6);
+       arp.arp_flags = ATF_PERM | ATF_COM;
+
+       struct sockaddr_in *in = (struct sockaddr_in*)&req.arp_pa;
+       in->sin_addr.s_addr = htonl(ipaddr);
+       in->sin_family = AF_INET;
+
+       int fd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (fd < 0) {
+               perror("socket");
+               return false;
+       }
+
+       bool ret = true;
+
+       if (ioctl(fd, add ? SIOCSARP : SIOCDARP, &req) < 0) {
+               perror(add ? "ioctl(SIOCSARP)" : "ioctl(SIOCDARP");
+               ret = false;
+       }
+
+       close(fd);
+       return ret;
+#else
+       struct nl_sock *sk;
+       struct rtnl_neigh *neigh;
+       struct nl_addr *mac, *ip;
+       int err = 1;
+
+       sk = NULL;
+       neigh = NULL;
+       mac = ip = NULL;
+
+       if (!(sk = xnl_socket_route())) {
+               goto out;
+       }
+
+       if (!(neigh = rtnl_neigh_alloc())) {
+               xperror("rtnl_neigh_alloc");
+               goto out;
+       }
+
+       if (!(mac = nl_addr_build(AF_PACKET, hwaddr, 6))) {
+               xperror("nl_addr_build");
+               goto out;
+       }
+
+       if (!(ip = nl_addr_build(AF_INET, &ipaddr, 4))) {
+               xperror("nl_addr_build");
+               goto out;
+       }
+
+       rtnl_neigh_set_ifindex(neigh, if_nametoindex(intf));
+       rtnl_neigh_set_dst(neigh, ip);
+
+       err = rtnl_neigh_delete(sk, neigh, 0);
+
+       if (add) {
+               rtnl_neigh_set_lladdr(neigh, mac);
+               rtnl_neigh_set_state(neigh, NUD_PERMANENT);
+               err = rtnl_neigh_add(sk, neigh, NLM_F_CREATE);
+       }
+
+       if (err && (add || verbosity > 1)) {
+               nl_perror(err, add ? "rtnl_neigh_add" : "rtnl_neigh_delete");
+       }
+
+out:
+       nl_addr_put(ip);
+       nl_addr_put(mac);
+       rtnl_neigh_put(neigh);
+       nl_socket_free(sk);
+
+       return !err;
+#endif
+}
+
+#endif
+
+static bool intf_get_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;
 
+       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;
                        }
@@ -132,7 +392,7 @@ void win_perror2(const char *msg, DWORD err)
        }
 }
 
-static bool get_intf_info(const char *intf, uint8_t *hwaddr, DWORD *index)
+static bool intf_get_info(const char *intf, uint8_t *hwaddr, DWORD *index)
 {
        PIP_ADAPTER_INFO adapters, adapter;
        DWORD ret;
@@ -146,7 +406,7 @@ static bool get_intf_info(const char *intf, uint8_t *hwaddr, DWORD *index)
 
        adapters = malloc(bufLen);
        if (!adapters) {
-               perror("malloc");
+               xperror("malloc");
                return false;
        }
 
@@ -268,14 +528,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 +538,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 +560,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 = !intf_get_info(intf, sock->hwaddr, &is_bridge);
 #else
-       err = !get_intf_info(intf, sock->hwaddr, &sock->index);
+       err = !intf_get_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 +599,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 +607,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 = bridge_stp_enabled(intf))) {
+               if (!bridge_stp(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;
 }
 
@@ -367,8 +640,8 @@ int select_fd(int fd, unsigned timeout)
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
 
-       tv.tv_sec = timeout / 1000;
-       tv.tv_usec = 1000 * (timeout % 1000);
+       tv.tv_sec = timeout / 1000000;
+       tv.tv_usec = timeout % 1000000;
 
        status = select(fd + 1, &fds, NULL, NULL, &tv);
        if (status < 0) {
@@ -443,7 +716,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) {
+               bridge_stp(sock->intf, true);
+       }
+#endif
+       if (sock->pcap) {
+               pcap_close(sock->pcap);
+       }
+
        free(sock);
        return 0;
 }
@@ -454,53 +739,79 @@ inline int ethsock_set_timeout(struct ethsock *sock, unsigned msec)
        return 0;
 }
 
-#ifndef NMRPFLASH_WINDOWS
-int ethsock_arp_add(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr)
+static int ethsock_arp(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)
-{
-       return 0;
-}
-#else
-static int ethsock_arp(struct ethsock *sock, uint8_t *hwaddr, struct in_addr *ipaddr, int add)
-{
-       DWORD ret;
+#if defined(NMRPFLASH_UNIX) && !defined(NMRPFLASH_LINUX)
+       struct in_addr addr = { .s_addr = ipaddr };
+#elif defined(NMRPFLASH_WINDOWS)
+       DWORD err;
        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) {
-               ret = CreateIpNetEntry(&arp);
-               if (ret != NO_ERROR) {
-                       win_perror2("CreateIpNetEntry", ret);
+#endif
+
+       if (undo) {
+#if defined(NMRPFLASH_LINUX)
+               if (!intf_add_del_arp(sock->intf, ipaddr, hwaddr, true)) {
                        return -1;
                }
+#elif defined(NMRPFLASH_WINDOWS)
+               err = CreateIpNetEntry(&arp);
+               if (err != NO_ERROR) {
+                       win_perror2("CreateIpNetEntry", err);
+                       return -1;
+               }
+#else
+               if (systemf("arp -s %s %s", inet_ntoa(addr), mac_to_str(hwaddr)) != 0) {
+                       return -1;
+               }
+#endif
+
+               *undo = malloc(sizeof(struct ethsock_arp_undo));
+               if (!*undo) {
+                       xperror("malloc");
+                       return -1;
+               }
+
+               (*undo)->ipaddr = ipaddr;
+               memcpy((*undo)->hwaddr, hwaddr, 6);
        } else {
-               DeleteIpNetEntry(&arp);
+#if defined(NMRPFLASH_LINUX)
+               if (!intf_add_del_arp(sock->intf, ipaddr, hwaddr, false)) {
+                       return -1;
+               }
+#elif defined(NMRPFLASH_WINDOWS)
+               return DeleteIpNetEntry(&arp) ? 0 : -1;
+#else
+               return systemf("arp -d %s", inet_ntoa(addr));
+#endif
        }
-       
+
        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)
 {
@@ -524,7 +835,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 intf_get_info(dev->name, hwaddr, NULL);
 }
 
 int ethsock_list_all(void)
@@ -566,7 +877,7 @@ int ethsock_list_all(void)
                pretty = intf_get_pretty_name(dev->name);
 
                if (!verbosity) {
-                       printf("%s%u", NMRPFLASH_NETALIAS_PREFIX, dev_num);
+                       printf("%s%-2u", NMRPFLASH_NETALIAS_PREFIX, dev_num);
                } else {
                        printf("%s", dev->name);
                }
@@ -649,16 +960,21 @@ 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)
+#if !defined(NMRPFLASH_WINDOWS) && !defined(NMRPFLASH_LINUX)
+static bool intf_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,76 +985,66 @@ 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;
        }
 
        return true;
 }
-
 #endif
 
-int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo)
+static int ethsock_ip_add_del(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo, bool add)
 {
-       if (undo && !(*undo = malloc(sizeof(struct ethsock_ip_undo)))) {
-               perror("malloc");
-               return -1;
+       int ret, fd;
+
+       if (add && undo) {
+               if (!(*undo = malloc(sizeof(struct ethsock_ip_undo)))) {
+                       xperror("malloc");
+                       return -1;
+               }
+
+               memset(*undo, 0, sizeof(**undo));
        }
 
-       int fd = socket(AF_INET, SOCK_DGRAM, 0);
-       if (!fd) {
+       ret = -1;
+       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (fd < 0) {
                sock_perror("socket");
-               return -1;
+               goto out;
        }
 
-       int ret = -1;
 #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");
-
        if (add) {
-               set_addr(&ifr.ifr_addr, ipaddr);
-               if (ioctl(fd, SIOCSIFADDR, &ifr) != 0) {
-                       perror("ioctl(SIOSIFADDR)");
-                       goto out;
-               }
-
-               set_addr(&ifr.ifr_netmask, ipmask);
-               if (ioctl(fd, SIOCSIFNETMASK, &ifr) != 0) {
-                       perror("ioctl(SIOCSIFNETMASK)");
-                       goto out;
-               }
-
                (*undo)->ip[0] = ipaddr;
                (*undo)->ip[1] = ipmask;
        }
 
-       if (!set_interface_up(fd, ifr.ifr_name, add ? true : false)) {
+       if (!intf_add_del_ip(sock->intf, (*undo)->ip[0], (*undo)->ip[1], 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));
 
        if (ioctl(fd, add ? SIOCAIFADDR : SIOCDIFADDR, &ifra) != 0) {
-               perror(add ? "ioctl(SIOCAIFADDR)" : "ioctl(SIOCDIFADDR)");
+               if (add) {
+                       xperror("ioctl(SIOCAIFADDR");
+               }
                goto out;
        }
 
        if (add) {
                (*undo)->ip[0] = ipaddr;
                (*undo)->ip[1] = ipmask;
-               set_interface_up(fd, ifra.ifra_name, true);
+               intf_up(fd, ifra.ifra_name, true);
        }
 
 #endif
@@ -755,19 +1061,18 @@ int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struc
        }
 
        set_addr(&sin, ipaddr);
-       clock_t now = clock();
+       time_t beg = time_monotonic();
 
        /* Wait until the new IP has actually been added */
 
        while (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
-               if (((clock() - now) / CLOCKS_PER_SEC) >= 5) {
+               if ((time_monotonic() - beg) >= 5) {
                        fprintf(stderr, "Failed to bind after 5 seconds: ");
                        sock_perror("bind");
                        DeleteIPAddress((*undo)->context);
                        goto out;
                }
        }
-       return 0;
 #endif
        ret = 0;
 
@@ -777,9 +1082,19 @@ out:
 #else
        closesocket(fd);
 #endif
+       if (ret != 0 && undo) {
+               free(*undo);
+               *undo = NULL;
+       }
+
        return ret;
 }
 
+int ethsock_ip_add(struct ethsock *sock, uint32_t ipaddr, uint32_t ipmask, struct ethsock_ip_undo **undo)
+{
+       return ethsock_ip_add_del(sock, ipaddr, ipmask, undo, true);
+}
+
 int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo)
 {
        if (!*undo) {
@@ -790,13 +1105,12 @@ int ethsock_ip_del(struct ethsock *sock, struct ethsock_ip_undo **undo)
 
 #ifndef NMRPFLASH_WINDOWS
        if ((*undo)->ip[0] != INADDR_NONE) {
-               ret = ethsock_ip_add(sock, (*undo)->ip[0], (*undo)->ip[1], NULL);
+               ret = ethsock_ip_add_del(sock, (*undo)->ip[0], (*undo)->ip[1], undo, false);
        } else {
                ret = 0;
        }
 #else
-       DeleteIPAddress((*undo)->context);
-       ret = 0;
+       ret = DeleteIPAddress((*undo)->context) ? 0 : -1;
 #endif
 
        free(*undo);