ip link: add support for "address ETHADDR". Closes 4862
[oweals/busybox.git] / networking / libiproute / ipaddress.c
index a603053e10c866f0966929ab870dd4c9707d9c99..aa4779ad1225d4f8daa0f1a3de0157b862adc8a5 100644 (file)
@@ -1,13 +1,11 @@
 /* vi: set sw=4 ts=4: */
 /*
- * ipaddress.c         "ip address".
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
- *
- * Authors:    Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  *
  * Changes:
- *     Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
+ * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
  */
 
 #include <fnmatch.h>
@@ -20,7 +18,7 @@
 
 #ifndef IFF_LOWER_UP
 /* from linux/if.h */
-#define IFF_LOWER_UP   0x10000         /* driver signals L1 up*/
+#define IFF_LOWER_UP  0x10000  /* driver signals L1 up */
 #endif
 
 struct filter_t {
@@ -164,6 +162,8 @@ static NOINLINE int print_linkinfo(const struct nlmsghdr *n)
                printf("master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
        }
 #endif
+/* IFLA_OPERSTATE was added to kernel with the same commit as IFF_DORMANT */
+#ifdef IFF_DORMANT
        if (tb[IFLA_OPERSTATE]) {
                static const char operstate_labels[] ALIGN1 =
                        "UNKNOWN\0""NOTPRESENT\0""DOWN\0""LOWERLAYERDOWN\0"
@@ -171,6 +171,7 @@ static NOINLINE int print_linkinfo(const struct nlmsghdr *n)
                printf("state %s ", nth_string(operstate_labels,
                                        *(uint8_t *)RTA_DATA(tb[IFLA_OPERSTATE])));
        }
+#endif
        if (G_filter.showqueue)
                print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
 
@@ -313,14 +314,16 @@ static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
        if (rta_tb[IFA_BROADCAST]) {
                printf("brd %s ",
                        rt_addr_n2a(ifa->ifa_family,
-                                   RTA_DATA(rta_tb[IFA_BROADCAST]),
-                                   abuf, sizeof(abuf)));
+                                       RTA_DATA(rta_tb[IFA_BROADCAST]),
+                                       abuf, sizeof(abuf))
+               );
        }
        if (rta_tb[IFA_ANYCAST]) {
                printf("any %s ",
                        rt_addr_n2a(ifa->ifa_family,
-                                   RTA_DATA(rta_tb[IFA_ANYCAST]),
-                                   abuf, sizeof(abuf)));
+                                       RTA_DATA(rta_tb[IFA_ANYCAST]),
+                                       abuf, sizeof(abuf))
+               );
        }
        printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1));
        if (ifa->ifa_flags & IFA_F_SECONDARY) {
@@ -365,7 +368,7 @@ static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
 
 struct nlmsg_list {
        struct nlmsg_list *next;
-       struct nlmsghdr   h;
+       struct nlmsghdr   h;
 };
 
 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
@@ -415,7 +418,7 @@ static void ipaddr_reset_filter(int _oneline)
 }
 
 /* Return value becomes exitcode. It's okay to not return at all */
-int ipaddr_list_or_flush(char **argv, int flush)
+int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
 {
        static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
 
@@ -444,28 +447,28 @@ int ipaddr_list_or_flush(char **argv, int flush)
        while (*argv) {
                const smalluint key = index_in_strings(option, *argv);
                if (key == 0) { /* to */
-                               NEXT_ARG();
-                               get_prefix(&G_filter.pfx, *argv, G_filter.family);
-                               if (G_filter.family == AF_UNSPEC) {
-                                       G_filter.family = G_filter.pfx.family;
-                               }
+                       NEXT_ARG();
+                       get_prefix(&G_filter.pfx, *argv, G_filter.family);
+                       if (G_filter.family == AF_UNSPEC) {
+                               G_filter.family = G_filter.pfx.family;
+                       }
                } else if (key == 1) { /* scope */
-                               uint32_t scope = 0;
-                               NEXT_ARG();
-                               G_filter.scopemask = -1;
-                               if (rtnl_rtscope_a2n(&scope, *argv)) {
-                                       if (strcmp(*argv, "all") != 0) {
-                                               invarg(*argv, "scope");
-                                       }
-                                       scope = RT_SCOPE_NOWHERE;
-                                       G_filter.scopemask = 0;
+                       uint32_t scope = 0;
+                       NEXT_ARG();
+                       G_filter.scopemask = -1;
+                       if (rtnl_rtscope_a2n(&scope, *argv)) {
+                               if (strcmp(*argv, "all") != 0) {
+                                       invarg(*argv, "scope");
                                }
-                               G_filter.scope = scope;
+                               scope = RT_SCOPE_NOWHERE;
+                               G_filter.scopemask = 0;
+                       }
+                       G_filter.scope = scope;
                } else if (key == 2) { /* up */
-                               G_filter.up = 1;
+                       G_filter.up = 1;
                } else if (key == 3) { /* label */
-                               NEXT_ARG();
-                               G_filter.label = *argv;
+                       NEXT_ARG();
+                       G_filter.label = *argv;
                } else {
                        if (key == 4) /* dev */
                                NEXT_ARG();
@@ -624,10 +627,12 @@ static int ipaddr_modify(int cmd, char **argv)
        req.ifa.ifa_family = preferred_family;
 
        while (*argv) {
-               const smalluint arg = index_in_strings(option, *argv);
-               if (arg <= 1) { /* peer, remote */
+               unsigned arg = index_in_strings(option, *argv);
+               /* if search fails, "local" is assumed */
+               if ((int)arg >= 0)
                        NEXT_ARG();
 
+               if (arg <= 1) { /* peer, remote */
                        if (peer_len) {
                                duparg("peer", *argv);
                        }
@@ -640,7 +645,6 @@ static int ipaddr_modify(int cmd, char **argv)
                        req.ifa.ifa_prefixlen = peer.bitlen;
                } else if (arg <= 3) { /* broadcast, brd */
                        inet_prefix addr;
-                       NEXT_ARG();
                        if (brd_len) {
                                duparg("broadcast", *argv);
                        }
@@ -657,7 +661,6 @@ static int ipaddr_modify(int cmd, char **argv)
                        }
                } else if (arg == 4) { /* anycast */
                        inet_prefix addr;
-                       NEXT_ARG();
                        if (any_len) {
                                duparg("anycast", *argv);
                        }
@@ -669,22 +672,18 @@ static int ipaddr_modify(int cmd, char **argv)
                        any_len = addr.bytelen;
                } else if (arg == 5) { /* scope */
                        uint32_t scope = 0;
-                       NEXT_ARG();
                        if (rtnl_rtscope_a2n(&scope, *argv)) {
                                invarg(*argv, "scope");
                        }
                        req.ifa.ifa_scope = scope;
                        scoped = 1;
                } else if (arg == 6) { /* dev */
-                       NEXT_ARG();
                        d = *argv;
                } else if (arg == 7) { /* label */
-                       NEXT_ARG();
                        l = *argv;
-                       addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
+                       addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
                } else {
-                       if (arg == 8) /* local */
-                               NEXT_ARG();
+                       /* local (specified or assumed) */
                        if (local_len) {
                                duparg2("local", *argv);
                        }
@@ -698,11 +697,10 @@ static int ipaddr_modify(int cmd, char **argv)
                argv++;
        }
 
-       // d cannot be null here, NEXT_ARG() of "dev" ensures that
-       //if (d == NULL) {
-       //      bb_error_msg(bb_msg_requires_arg, "\"dev\"");
-       //      return -1;
-       //}
+       if (!d) {
+               /* There was no "dev IFACE", but we need that */
+               bb_error_msg_and_die("need \"dev IFACE\"");
+       }
        if (l && strncmp(d, l, strlen(d)) != 0) {
                bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
        }
@@ -722,7 +720,7 @@ static int ipaddr_modify(int cmd, char **argv)
                }
                brd = peer;
                if (brd.bitlen <= 30) {
-                       for (i=31; i>=brd.bitlen; i--) {
+                       for (i = 31; i >= brd.bitlen; i--) {
                                if (brd_len == -1)
                                        brd.data[0] |= htonl(1<<(31-i));
                                else
@@ -748,15 +746,15 @@ static int ipaddr_modify(int cmd, char **argv)
 }
 
 /* Return value becomes exitcode. It's okay to not return at all */
-int do_ipaddr(char **argv)
+int FAST_FUNC do_ipaddr(char **argv)
 {
        static const char commands[] ALIGN1 =
                "add\0""delete\0""list\0""show\0""lst\0""flush\0";
-       smalluint cmd = 2;
+       int cmd = 2;
        if (*argv) {
                cmd = index_in_substrings(commands, *argv);
-               if (cmd > 5)
-                       bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
+               if (cmd < 0)
+                       invarg(*argv, applet_name);
                argv++;
                if (cmd <= 1)
                        return ipaddr_modify((cmd == 0) ? RTM_NEWADDR : RTM_DELADDR, argv);