/* 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>
#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 {
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"
"TESTING\0""DORMANT\0""UP\0";
printf("state %s ", nth_string(operstate_labels,
- *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE])));
+ *(uint8_t *)RTA_DATA(tb[IFLA_OPERSTATE])));
}
+#endif
if (G_filter.showqueue)
print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
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) {
struct nlmsg_list {
struct nlmsg_list *next;
- struct nlmsghdr h;
+ struct nlmsghdr h;
};
static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
}
/* 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";
}
while (*argv) {
- const int option_num = index_in_strings(option, *argv);
- switch (option_num) {
- case 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;
- }
- break;
- case 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;
+ 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;
+ }
+ } 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");
}
- G_filter.scope = scope;
- break;
+ scope = RT_SCOPE_NOWHERE;
+ G_filter.scopemask = 0;
}
- case 2: /* up */
- G_filter.up = 1;
- break;
- case 3: /* label */
- NEXT_ARG();
- G_filter.label = *argv;
- break;
- case 4: /* dev */
+ G_filter.scope = scope;
+ } else if (key == 2) { /* up */
+ G_filter.up = 1;
+ } else if (key == 3) { /* label */
+ NEXT_ARG();
+ G_filter.label = *argv;
+ } else {
+ if (key == 4) /* dev */
NEXT_ARG();
- default:
- if (filter_dev) {
- duparg2("dev", *argv);
- }
- filter_dev = *argv;
+ if (filter_dev)
+ duparg2("dev", *argv);
+ filter_dev = *argv;
}
argv++;
}
req.ifa.ifa_family = preferred_family;
while (*argv) {
- const int option_num = index_in_strings(option, *argv);
- switch (option_num) {
- case 0: /* peer */
- case 1: /* remote */
- NEXT_ARG();
-
- if (peer_len) {
- duparg("peer", *argv);
- }
- get_prefix(&peer, *argv, req.ifa.ifa_family);
- peer_len = peer.bytelen;
- if (req.ifa.ifa_family == AF_UNSPEC) {
- req.ifa.ifa_family = peer.family;
- }
- addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
- req.ifa.ifa_prefixlen = peer.bitlen;
- break;
- case 2: /* broadcast */
- case 3: /* brd */
- {
- inet_prefix addr;
- NEXT_ARG();
- if (brd_len) {
- duparg("broadcast", *argv);
- }
- if (LONE_CHAR(*argv, '+')) {
- brd_len = -1;
- } else if (LONE_DASH(*argv)) {
- brd_len = -2;
- } else {
- get_addr(&addr, *argv, req.ifa.ifa_family);
- if (req.ifa.ifa_family == AF_UNSPEC)
- req.ifa.ifa_family = addr.family;
- addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
- brd_len = addr.bytelen;
- }
- break;
+ 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);
}
- case 4: /* anycast */
- {
- inet_prefix addr;
- NEXT_ARG();
- if (any_len) {
- duparg("anycast", *argv);
- }
+ get_prefix(&peer, *argv, req.ifa.ifa_family);
+ peer_len = peer.bytelen;
+ if (req.ifa.ifa_family == AF_UNSPEC) {
+ req.ifa.ifa_family = peer.family;
+ }
+ addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
+ req.ifa.ifa_prefixlen = peer.bitlen;
+ } else if (arg <= 3) { /* broadcast, brd */
+ inet_prefix addr;
+ if (brd_len) {
+ duparg("broadcast", *argv);
+ }
+ if (LONE_CHAR(*argv, '+')) {
+ brd_len = -1;
+ } else if (LONE_DASH(*argv)) {
+ brd_len = -2;
+ } else {
get_addr(&addr, *argv, req.ifa.ifa_family);
- if (req.ifa.ifa_family == AF_UNSPEC) {
+ if (req.ifa.ifa_family == AF_UNSPEC)
req.ifa.ifa_family = addr.family;
- }
- addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
- any_len = addr.bytelen;
- break;
+ addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
+ brd_len = addr.bytelen;
}
- case 5: /* scope */
- {
- uint32_t scope = 0;
- NEXT_ARG();
- if (rtnl_rtscope_a2n(&scope, *argv)) {
- invarg(*argv, "scope");
- }
- req.ifa.ifa_scope = scope;
- scoped = 1;
- break;
+ } else if (arg == 4) { /* anycast */
+ inet_prefix addr;
+ if (any_len) {
+ duparg("anycast", *argv);
}
- case 6: /* dev */
- NEXT_ARG();
- d = *argv;
- break;
- case 7: /* label */
- NEXT_ARG();
- l = *argv;
- addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
- break;
- case 8: /* local */
- NEXT_ARG();
- default:
- if (local_len) {
- duparg2("local", *argv);
- }
- get_prefix(&lcl, *argv, req.ifa.ifa_family);
- if (req.ifa.ifa_family == AF_UNSPEC) {
- req.ifa.ifa_family = lcl.family;
- }
- addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
- local_len = lcl.bytelen;
+ get_addr(&addr, *argv, req.ifa.ifa_family);
+ if (req.ifa.ifa_family == AF_UNSPEC) {
+ req.ifa.ifa_family = addr.family;
+ }
+ addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
+ any_len = addr.bytelen;
+ } else if (arg == 5) { /* scope */
+ uint32_t scope = 0;
+ if (rtnl_rtscope_a2n(&scope, *argv)) {
+ invarg(*argv, "scope");
+ }
+ req.ifa.ifa_scope = scope;
+ scoped = 1;
+ } else if (arg == 6) { /* dev */
+ d = *argv;
+ } else if (arg == 7) { /* label */
+ l = *argv;
+ addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
+ } else {
+ /* local (specified or assumed) */
+ if (local_len) {
+ duparg2("local", *argv);
+ }
+ get_prefix(&lcl, *argv, req.ifa.ifa_family);
+ if (req.ifa.ifa_family == AF_UNSPEC) {
+ req.ifa.ifa_family = lcl.family;
+ }
+ addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
+ local_len = lcl.bytelen;
}
argv++;
}
- 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);
}
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
}
/* 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);