udhcpc6: fix for ppp interface type
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 19 Jan 2018 17:44:19 +0000 (18:44 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 19 Jan 2018 17:44:19 +0000 (18:44 +0100)
function                                             old     new   delta
d6_read_interface                                    454     600    +146

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
networking/udhcp/d6_dhcpc.c
networking/udhcp/d6_socket.c

index f3a7b827ca2718225380eae7ced91fff2d90b80d..07a8b11c1575106e33ff720be5a1f1a9a8490a42 100644 (file)
@@ -1093,6 +1093,8 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
        int retval;
 
        setup_common_bufsiz();
+       /* We want random_xid to be random */
+       srand(monotonic_us());
 
        /* Default options */
        IF_FEATURE_UDHCP_PORT(SERVER_PORT6 = 547;)
@@ -1207,8 +1209,6 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
        bb_error_msg("started, v"BB_VER);
        /* Set up the signal pipe */
        udhcp_sp_setup();
-       /* We want random_xid to be random... */
-       srand(monotonic_us());
 
        state = INIT_SELECTING;
        d6_run_script(NULL, "deconfig");
index d00c217d68c0f3cdc903deea6c607e01f3b166e5..f9082ee9d3a83f432016ecdac2821871fd44c6a0 100644 (file)
@@ -16,7 +16,6 @@ int FAST_FUNC d6_read_interface(const char *interface, int *ifindex, struct in6_
        struct ifaddrs *ifap, *ifa;
 
        getifaddrs(&ifap);
-
        for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
                struct sockaddr_in6 *sip6;
 
@@ -29,9 +28,9 @@ int FAST_FUNC d6_read_interface(const char *interface, int *ifindex, struct in6_
                        struct sockaddr_ll *sll = (struct sockaddr_ll*)(ifa->ifa_addr);
                        memcpy(mac, sll->sll_addr, 6);
                        log2("MAC %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-                       log2("ifindex %d", sll->sll_ifindex);
                        *ifindex = sll->sll_ifindex;
-                       retval &= (0xf - (1<<0));
+                       log2("ifindex %d", *ifindex);
+                       retval &= (3 - (1<<0));
                }
 #if 0
                if (ifa->ifa_addr->sa_family == AF_INET) {
@@ -54,11 +53,29 @@ int FAST_FUNC d6_read_interface(const char *interface, int *ifindex, struct in6_
                                nip6->s6_addr[12], nip6->s6_addr[13],
                                nip6->s6_addr[14], nip6->s6_addr[15]
                        );
-                       retval &= (0xf - (1<<1));
+                       retval &= (3 - (1<<1));
                }
        }
-
        freeifaddrs(ifap);
+
+       if (retval & (1<<0)) {
+               /* This iface has no MAC (e.g. ppp), generate a random one */
+               struct ifreq ifr;
+               int fd;
+
+               memset(&ifr, 0, sizeof(ifr));
+               strncpy_IFNAMSIZ(ifr.ifr_name, interface);
+               fd = xsocket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
+               if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
+                       *ifindex = ifr.ifr_ifindex;
+                       log2("ifindex %d", *ifindex);
+                       ((uint32_t*)mac)[0] = rand();
+                       ((uint16_t*)mac)[2] = rand();
+                       retval &= (3 - (1<<0));
+               }
+               close(fd);
+       }
+
        if (retval == 0)
                return retval;