"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * ipaddress.c "ip address". |
| 4 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
| 7 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 8 | * |
| 9 | * Changes: |
| 10 | * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated |
| 11 | */ |
| 12 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 13 | #include <fnmatch.h> |
Eric Andersen | 8004bb7 | 2003-01-14 08:06:07 +0000 | [diff] [blame] | 14 | #include <net/if.h> |
| 15 | #include <net/if_arp.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 17 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 18 | #include "rt_names.h" |
| 19 | #include "utils.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 9b6f4aa | 2008-06-05 14:01:04 +0000 | [diff] [blame] | 21 | #ifndef IFF_LOWER_UP |
| 22 | /* from linux/if.h */ |
| 23 | #define IFF_LOWER_UP 0x10000 /* driver signals L1 up*/ |
| 24 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 25 | |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 26 | struct filter_t { |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 27 | char *label; |
| 28 | char *flushb; |
| 29 | struct rtnl_handle *rth; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 30 | int scope, scopemask; |
| 31 | int flags, flagmask; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 32 | int flushp; |
| 33 | int flushe; |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 34 | int ifindex; |
| 35 | family_t family; |
| 36 | smallint showqueue; |
| 37 | smallint oneline; |
| 38 | smallint up; |
| 39 | smallint flushed; |
| 40 | inet_prefix pfx; |
Denys Vlasenko | 36659fd | 2010-02-05 14:40:23 +0100 | [diff] [blame] | 41 | } FIX_ALIASING; |
| 42 | typedef struct filter_t filter_t; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 43 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 44 | #define G_filter (*(filter_t*)&bb_common_bufsiz1) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 45 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 47 | static void print_link_flags(unsigned flags, unsigned mdown) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 48 | { |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 49 | static const int flag_masks[] = { |
| 50 | IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT, |
| 51 | IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP }; |
| 52 | static const char flag_labels[] ALIGN1 = |
| 53 | "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0" |
| 54 | "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0"; |
| 55 | |
Denis Vlasenko | 7d60fc1 | 2008-06-05 06:51:06 +0000 | [diff] [blame] | 56 | bb_putchar('<'); |
Bernhard Reutner-Fischer | 49ee839 | 2010-05-25 09:55:42 +0200 | [diff] [blame] | 57 | if (flags & IFF_UP && !(flags & IFF_RUNNING)) |
| 58 | printf("NO-CARRIER,"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 59 | flags &= ~IFF_RUNNING; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 60 | #if 0 |
| 61 | _PF(ALLMULTI); |
| 62 | _PF(PROMISC); |
| 63 | _PF(MASTER); |
| 64 | _PF(SLAVE); |
| 65 | _PF(DEBUG); |
| 66 | _PF(DYNAMIC); |
| 67 | _PF(AUTOMEDIA); |
| 68 | _PF(PORTSEL); |
| 69 | _PF(NOTRAILERS); |
| 70 | #endif |
Denis Vlasenko | 53354ac | 2008-06-07 15:10:29 +0000 | [diff] [blame] | 71 | flags = print_flags_separated(flag_masks, flag_labels, flags, ","); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 72 | if (flags) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 73 | printf("%x", flags); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 74 | if (mdown) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 75 | printf(",M-DOWN"); |
| 76 | printf("> "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 79 | static void print_queuelen(char *name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 80 | { |
| 81 | struct ifreq ifr; |
| 82 | int s; |
| 83 | |
| 84 | s = socket(AF_INET, SOCK_STREAM, 0); |
| 85 | if (s < 0) |
| 86 | return; |
| 87 | |
| 88 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 89 | strncpy_IFNAMSIZ(ifr.ifr_name, name); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 90 | if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | close(s); |
| 92 | return; |
| 93 | } |
| 94 | close(s); |
| 95 | |
| 96 | if (ifr.ifr_qlen) |
| 97 | printf("qlen %d", ifr.ifr_qlen); |
| 98 | } |
| 99 | |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 100 | static NOINLINE int print_linkinfo(const struct nlmsghdr *n) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 101 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 102 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 103 | struct rtattr *tb[IFLA_MAX+1]; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 104 | int len = n->nlmsg_len; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 105 | |
| 106 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) |
| 107 | return 0; |
| 108 | |
| 109 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 110 | if (len < 0) |
| 111 | return -1; |
| 112 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 113 | if (G_filter.ifindex && ifi->ifi_index != G_filter.ifindex) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 114 | return 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 115 | if (G_filter.up && !(ifi->ifi_flags & IFF_UP)) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | |
| 118 | memset(tb, 0, sizeof(tb)); |
| 119 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 120 | if (tb[IFLA_IFNAME] == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 121 | bb_error_msg("nil ifname"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 122 | return -1; |
| 123 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 124 | if (G_filter.label |
| 125 | && (!G_filter.family || G_filter.family == AF_PACKET) |
| 126 | && fnmatch(G_filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 127 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 128 | return 0; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 129 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 130 | |
| 131 | if (n->nlmsg_type == RTM_DELLINK) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 132 | printf("Deleted "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 134 | printf("%d: %s", ifi->ifi_index, |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 135 | /*tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>" - we checked tb[IFLA_IFNAME] above*/ |
| 136 | (char*)RTA_DATA(tb[IFLA_IFNAME]) |
| 137 | ); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 138 | |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 139 | { |
| 140 | unsigned m_flag = 0; |
| 141 | if (tb[IFLA_LINK]) { |
| 142 | SPRINT_BUF(b1); |
| 143 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 144 | if (iflink == 0) |
| 145 | printf("@NONE: "); |
| 146 | else { |
| 147 | printf("@%s: ", ll_idx_n2a(iflink, b1)); |
| 148 | m_flag = ll_index_to_flags(iflink); |
| 149 | m_flag = !(m_flag & IFF_UP); |
| 150 | } |
| 151 | } else { |
| 152 | printf(": "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 153 | } |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 154 | print_link_flags(ifi->ifi_flags, m_flag); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 155 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 156 | |
| 157 | if (tb[IFLA_MTU]) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 158 | printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 159 | if (tb[IFLA_QDISC]) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 160 | printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 161 | #ifdef IFLA_MASTER |
| 162 | if (tb[IFLA_MASTER]) { |
| 163 | SPRINT_BUF(b1); |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 164 | printf("master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 165 | } |
| 166 | #endif |
Bernhard Reutner-Fischer | 49ee839 | 2010-05-25 09:55:42 +0200 | [diff] [blame] | 167 | if (tb[IFLA_OPERSTATE]) { |
| 168 | static const char operstate_labels[] ALIGN1 = |
| 169 | "UNKNOWN\0""NOTPRESENT\0""DOWN\0""LOWERLAYERDOWN\0" |
| 170 | "TESTING\0""DORMANT\0""UP\0"; |
| 171 | printf("state %s ", nth_string(operstate_labels, |
Denys Vlasenko | eb29e91 | 2010-05-27 13:35:04 +0200 | [diff] [blame^] | 172 | *(uint8_t *)RTA_DATA(tb[IFLA_OPERSTATE]))); |
Bernhard Reutner-Fischer | 49ee839 | 2010-05-25 09:55:42 +0200 | [diff] [blame] | 173 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 174 | if (G_filter.showqueue) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 175 | print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME])); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 176 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 177 | if (!G_filter.family || G_filter.family == AF_PACKET) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 178 | SPRINT_BUF(b1); |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 179 | printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 180 | |
| 181 | if (tb[IFLA_ADDRESS]) { |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 182 | fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 183 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 184 | ifi->ifi_type, |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 185 | b1, sizeof(b1)), stdout); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 186 | } |
| 187 | if (tb[IFLA_BROADCAST]) { |
Denis Vlasenko | dfc0740 | 2007-10-29 19:33:26 +0000 | [diff] [blame] | 188 | if (ifi->ifi_flags & IFF_POINTOPOINT) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 189 | printf(" peer "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 190 | else |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 191 | printf(" brd "); |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 192 | fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 193 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 194 | ifi->ifi_type, |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 195 | b1, sizeof(b1)), stdout); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 198 | bb_putchar('\n'); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 199 | /*fflush_all();*/ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 203 | static int flush_update(void) |
| 204 | { |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 205 | if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) { |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 206 | bb_perror_msg("can't send flush request"); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 207 | return -1; |
| 208 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 209 | G_filter.flushp = 0; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 213 | static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 214 | struct nlmsghdr *n, void *arg UNUSED_PARAM) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 215 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 216 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 217 | int len = n->nlmsg_len; |
| 218 | struct rtattr * rta_tb[IFA_MAX+1]; |
| 219 | char abuf[256]; |
| 220 | SPRINT_BUF(b1); |
| 221 | |
| 222 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) |
| 223 | return 0; |
| 224 | len -= NLMSG_LENGTH(sizeof(*ifa)); |
| 225 | if (len < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | bb_error_msg("wrong nlmsg len %d", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 227 | return -1; |
| 228 | } |
| 229 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 230 | if (G_filter.flushb && n->nlmsg_type != RTM_NEWADDR) |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 231 | return 0; |
| 232 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 233 | memset(rta_tb, 0, sizeof(rta_tb)); |
| 234 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
| 235 | |
| 236 | if (!rta_tb[IFA_LOCAL]) |
| 237 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; |
| 238 | if (!rta_tb[IFA_ADDRESS]) |
| 239 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; |
| 240 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 241 | if (G_filter.ifindex && G_filter.ifindex != ifa->ifa_index) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 242 | return 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 243 | if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 244 | return 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 245 | if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 246 | return 0; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 247 | if (G_filter.label) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 248 | const char *label; |
| 249 | if (rta_tb[IFA_LABEL]) |
| 250 | label = RTA_DATA(rta_tb[IFA_LABEL]); |
| 251 | else |
| 252 | label = ll_idx_n2a(ifa->ifa_index, b1); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 253 | if (fnmatch(G_filter.label, label, 0) != 0) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 256 | if (G_filter.pfx.family) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 257 | if (rta_tb[IFA_LOCAL]) { |
| 258 | inet_prefix dst; |
| 259 | memset(&dst, 0, sizeof(dst)); |
| 260 | dst.family = ifa->ifa_family; |
| 261 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 262 | if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen)) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | } |
| 266 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 267 | if (G_filter.flushb) { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 268 | struct nlmsghdr *fn; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 269 | if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 270 | if (flush_update()) |
| 271 | return -1; |
| 272 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 273 | fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 274 | memcpy(fn, n, n->nlmsg_len); |
| 275 | fn->nlmsg_type = RTM_DELADDR; |
| 276 | fn->nlmsg_flags = NLM_F_REQUEST; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 277 | fn->nlmsg_seq = ++G_filter.rth->seq; |
| 278 | G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb; |
| 279 | G_filter.flushed = 1; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 283 | if (n->nlmsg_type == RTM_DELADDR) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 284 | printf("Deleted "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 285 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 286 | if (G_filter.oneline) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 287 | printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 288 | if (ifa->ifa_family == AF_INET) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 289 | printf(" inet "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 290 | else if (ifa->ifa_family == AF_INET6) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 291 | printf(" inet6 "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 292 | else |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 293 | printf(" family %d ", ifa->ifa_family); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 294 | |
| 295 | if (rta_tb[IFA_LOCAL]) { |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 296 | fputs(rt_addr_n2a(ifa->ifa_family, |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 297 | RTA_DATA(rta_tb[IFA_LOCAL]), |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 298 | abuf, sizeof(abuf)), stdout); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 299 | |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 300 | if (rta_tb[IFA_ADDRESS] == NULL |
| 301 | || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0 |
| 302 | ) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 303 | printf("/%d ", ifa->ifa_prefixlen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 304 | } else { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 305 | printf(" peer %s/%d ", |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 306 | rt_addr_n2a(ifa->ifa_family, |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 307 | RTA_DATA(rta_tb[IFA_ADDRESS]), |
| 308 | abuf, sizeof(abuf)), |
| 309 | ifa->ifa_prefixlen); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if (rta_tb[IFA_BROADCAST]) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 314 | printf("brd %s ", |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 315 | rt_addr_n2a(ifa->ifa_family, |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 316 | RTA_DATA(rta_tb[IFA_BROADCAST]), |
| 317 | abuf, sizeof(abuf))); |
| 318 | } |
| 319 | if (rta_tb[IFA_ANYCAST]) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 320 | printf("any %s ", |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 321 | rt_addr_n2a(ifa->ifa_family, |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 322 | RTA_DATA(rta_tb[IFA_ANYCAST]), |
| 323 | abuf, sizeof(abuf))); |
| 324 | } |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 325 | printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1)); |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 326 | if (ifa->ifa_flags & IFA_F_SECONDARY) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 327 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 328 | printf("secondary "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 329 | } |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 330 | if (ifa->ifa_flags & IFA_F_TENTATIVE) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 331 | ifa->ifa_flags &= ~IFA_F_TENTATIVE; |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 332 | printf("tentative "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 333 | } |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 334 | if (ifa->ifa_flags & IFA_F_DEPRECATED) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 335 | ifa->ifa_flags &= ~IFA_F_DEPRECATED; |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 336 | printf("deprecated "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 337 | } |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 338 | if (!(ifa->ifa_flags & IFA_F_PERMANENT)) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 339 | printf("dynamic "); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 340 | } else |
| 341 | ifa->ifa_flags &= ~IFA_F_PERMANENT; |
| 342 | if (ifa->ifa_flags) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 343 | printf("flags %02x ", ifa->ifa_flags); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 344 | if (rta_tb[IFA_LABEL]) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 345 | fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 346 | if (rta_tb[IFA_CACHEINFO]) { |
| 347 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
| 348 | char buf[128]; |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 349 | bb_putchar(_SL_); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 350 | if (ci->ifa_valid == 0xFFFFFFFFU) |
| 351 | sprintf(buf, "valid_lft forever"); |
| 352 | else |
| 353 | sprintf(buf, "valid_lft %dsec", ci->ifa_valid); |
| 354 | if (ci->ifa_prefered == 0xFFFFFFFFU) |
| 355 | sprintf(buf+strlen(buf), " preferred_lft forever"); |
| 356 | else |
| 357 | sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 358 | printf(" %s", buf); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 359 | } |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 360 | bb_putchar('\n'); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 361 | /*fflush_all();*/ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 366 | struct nlmsg_list { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 367 | struct nlmsg_list *next; |
| 368 | struct nlmsghdr h; |
| 369 | }; |
| 370 | |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 371 | static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 372 | { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 373 | for (; ainfo; ainfo = ainfo->next) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 374 | struct nlmsghdr *n = &ainfo->h; |
| 375 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 376 | |
| 377 | if (n->nlmsg_type != RTM_NEWADDR) |
| 378 | continue; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 379 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) |
| 380 | return -1; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 381 | if (ifa->ifa_index != ifindex |
| 382 | || (G_filter.family && G_filter.family != ifa->ifa_family) |
| 383 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 384 | continue; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 385 | } |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 386 | print_addrinfo(NULL, n, NULL); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 392 | static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 393 | { |
| 394 | struct nlmsg_list **linfo = (struct nlmsg_list**)arg; |
| 395 | struct nlmsg_list *h; |
| 396 | struct nlmsg_list **lp; |
| 397 | |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 398 | h = xzalloc(n->nlmsg_len + sizeof(void*)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 399 | |
| 400 | memcpy(&h->h, n, n->nlmsg_len); |
Denys Vlasenko | 9037787 | 2010-01-08 09:07:50 +0100 | [diff] [blame] | 401 | /*h->next = NULL; - xzalloc did it */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 402 | |
Denis Vlasenko | 3e57adb | 2008-05-31 07:33:18 +0000 | [diff] [blame] | 403 | for (lp = linfo; *lp; lp = &(*lp)->next) |
| 404 | continue; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 405 | *lp = h; |
| 406 | |
| 407 | ll_remember_index(who, n, NULL); |
| 408 | return 0; |
| 409 | } |
| 410 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 411 | static void ipaddr_reset_filter(int _oneline) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 412 | { |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 413 | memset(&G_filter, 0, sizeof(G_filter)); |
| 414 | G_filter.oneline = _oneline; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 417 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 418 | int ipaddr_list_or_flush(char **argv, int flush) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 419 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 420 | static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0"; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 421 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 422 | struct nlmsg_list *linfo = NULL; |
| 423 | struct nlmsg_list *ainfo = NULL; |
| 424 | struct nlmsg_list *l; |
| 425 | struct rtnl_handle rth; |
| 426 | char *filter_dev = NULL; |
| 427 | int no_link = 0; |
| 428 | |
| 429 | ipaddr_reset_filter(oneline); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 430 | G_filter.showqueue = 1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 431 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 432 | if (G_filter.family == AF_UNSPEC) |
| 433 | G_filter.family = preferred_family; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 434 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 435 | if (flush) { |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 436 | if (!*argv) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 437 | bb_error_msg_and_die(bb_msg_requires_arg, "flush"); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 438 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 439 | if (G_filter.family == AF_PACKET) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 440 | bb_error_msg_and_die("can't flush link addresses"); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 444 | while (*argv) { |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 445 | const smalluint key = index_in_strings(option, *argv); |
| 446 | if (key == 0) { /* to */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 447 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 448 | get_prefix(&G_filter.pfx, *argv, G_filter.family); |
| 449 | if (G_filter.family == AF_UNSPEC) { |
| 450 | G_filter.family = G_filter.pfx.family; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 451 | } |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 452 | } else if (key == 1) { /* scope */ |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 453 | uint32_t scope = 0; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 454 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 455 | G_filter.scopemask = -1; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 456 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 457 | if (strcmp(*argv, "all") != 0) { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 458 | invarg(*argv, "scope"); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 459 | } |
| 460 | scope = RT_SCOPE_NOWHERE; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 461 | G_filter.scopemask = 0; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 462 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 463 | G_filter.scope = scope; |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 464 | } else if (key == 2) { /* up */ |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 465 | G_filter.up = 1; |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 466 | } else if (key == 3) { /* label */ |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 467 | NEXT_ARG(); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 468 | G_filter.label = *argv; |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 469 | } else { |
| 470 | if (key == 4) /* dev */ |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 471 | NEXT_ARG(); |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 472 | if (filter_dev) |
| 473 | duparg2("dev", *argv); |
| 474 | filter_dev = *argv; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 475 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 476 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 479 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 480 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 481 | xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK); |
| 482 | xrtnl_dump_filter(&rth, store_nlmsg, &linfo); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 483 | |
| 484 | if (filter_dev) { |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 485 | G_filter.ifindex = xll_name_to_index(filter_dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 488 | if (flush) { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 489 | char flushb[4096-512]; |
| 490 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 491 | G_filter.flushb = flushb; |
| 492 | G_filter.flushp = 0; |
| 493 | G_filter.flushe = sizeof(flushb); |
| 494 | G_filter.rth = &rth; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 495 | |
| 496 | for (;;) { |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 497 | xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR); |
| 498 | G_filter.flushed = 0; |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 499 | xrtnl_dump_filter(&rth, print_addrinfo, NULL); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 500 | if (G_filter.flushed == 0) { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 501 | return 0; |
| 502 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 503 | if (flush_update() < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 504 | return 1; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 505 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 509 | if (G_filter.family != AF_PACKET) { |
| 510 | xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 511 | xrtnl_dump_filter(&rth, store_nlmsg, &ainfo); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 515 | if (G_filter.family && G_filter.family != AF_PACKET) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 516 | struct nlmsg_list **lp; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 517 | lp = &linfo; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 518 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 519 | if (G_filter.oneline) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 520 | no_link = 1; |
| 521 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 522 | while ((l = *lp) != NULL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 523 | int ok = 0; |
| 524 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 525 | struct nlmsg_list *a; |
| 526 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 527 | for (a = ainfo; a; a = a->next) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 528 | struct nlmsghdr *n = &a->h; |
| 529 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 530 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 531 | if (ifa->ifa_index != ifi->ifi_index |
| 532 | || (G_filter.family && G_filter.family != ifa->ifa_family) |
| 533 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 534 | continue; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 535 | } |
| 536 | if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 537 | continue; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 538 | if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 539 | continue; |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 540 | if (G_filter.pfx.family || G_filter.label) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 541 | struct rtattr *tb[IFA_MAX+1]; |
| 542 | memset(tb, 0, sizeof(tb)); |
| 543 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); |
| 544 | if (!tb[IFA_LOCAL]) |
| 545 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; |
| 546 | |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 547 | if (G_filter.pfx.family && tb[IFA_LOCAL]) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 548 | inet_prefix dst; |
| 549 | memset(&dst, 0, sizeof(dst)); |
| 550 | dst.family = ifa->ifa_family; |
| 551 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 552 | if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen)) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 553 | continue; |
| 554 | } |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 555 | if (G_filter.label) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 556 | SPRINT_BUF(b1); |
| 557 | const char *label; |
| 558 | if (tb[IFA_LABEL]) |
| 559 | label = RTA_DATA(tb[IFA_LABEL]); |
| 560 | else |
| 561 | label = ll_idx_n2a(ifa->ifa_index, b1); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 562 | if (fnmatch(G_filter.label, label, 0) != 0) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 563 | continue; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | ok = 1; |
| 568 | break; |
| 569 | } |
| 570 | if (!ok) |
| 571 | *lp = l->next; |
| 572 | else |
| 573 | lp = &l->next; |
| 574 | } |
| 575 | } |
| 576 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 577 | for (l = linfo; l; l = l->next) { |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 578 | if (no_link || print_linkinfo(&l->h) == 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 579 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
Denys Vlasenko | ffc4bce | 2010-01-26 11:03:16 +0100 | [diff] [blame] | 580 | if (G_filter.family != AF_PACKET) |
Denis Vlasenko | bedfabd | 2008-06-05 05:00:24 +0000 | [diff] [blame] | 581 | print_selected_addrinfo(ifi->ifi_index, ainfo); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 582 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 585 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 588 | static int default_scope(inet_prefix *lcl) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 589 | { |
| 590 | if (lcl->family == AF_INET) { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 591 | if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 592 | return RT_SCOPE_HOST; |
| 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 597 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 598 | static int ipaddr_modify(int cmd, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 599 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 600 | static const char option[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 601 | "peer\0""remote\0""broadcast\0""brd\0" |
| 602 | "anycast\0""scope\0""dev\0""label\0""local\0"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 603 | struct rtnl_handle rth; |
| 604 | struct { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 605 | struct nlmsghdr n; |
| 606 | struct ifaddrmsg ifa; |
| 607 | char buf[256]; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 608 | } req; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 609 | char *d = NULL; |
| 610 | char *l = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 611 | inet_prefix lcl; |
| 612 | inet_prefix peer; |
| 613 | int local_len = 0; |
| 614 | int peer_len = 0; |
| 615 | int brd_len = 0; |
| 616 | int any_len = 0; |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 617 | bool scoped = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 618 | |
| 619 | memset(&req, 0, sizeof(req)); |
| 620 | |
| 621 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 622 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 623 | req.n.nlmsg_type = cmd; |
| 624 | req.ifa.ifa_family = preferred_family; |
| 625 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 626 | while (*argv) { |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 627 | const smalluint arg = index_in_strings(option, *argv); |
| 628 | if (arg <= 1) { /* peer, remote */ |
| 629 | NEXT_ARG(); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 630 | |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 631 | if (peer_len) { |
| 632 | duparg("peer", *argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 633 | } |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 634 | get_prefix(&peer, *argv, req.ifa.ifa_family); |
| 635 | peer_len = peer.bytelen; |
| 636 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 637 | req.ifa.ifa_family = peer.family; |
| 638 | } |
| 639 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); |
| 640 | req.ifa.ifa_prefixlen = peer.bitlen; |
| 641 | } else if (arg <= 3) { /* broadcast, brd */ |
| 642 | inet_prefix addr; |
| 643 | NEXT_ARG(); |
| 644 | if (brd_len) { |
| 645 | duparg("broadcast", *argv); |
| 646 | } |
| 647 | if (LONE_CHAR(*argv, '+')) { |
| 648 | brd_len = -1; |
| 649 | } else if (LONE_DASH(*argv)) { |
| 650 | brd_len = -2; |
| 651 | } else { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 652 | get_addr(&addr, *argv, req.ifa.ifa_family); |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 653 | if (req.ifa.ifa_family == AF_UNSPEC) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 654 | req.ifa.ifa_family = addr.family; |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 655 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); |
| 656 | brd_len = addr.bytelen; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 657 | } |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 658 | } else if (arg == 4) { /* anycast */ |
| 659 | inet_prefix addr; |
| 660 | NEXT_ARG(); |
| 661 | if (any_len) { |
| 662 | duparg("anycast", *argv); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 663 | } |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 664 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 665 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 666 | req.ifa.ifa_family = addr.family; |
| 667 | } |
| 668 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); |
| 669 | any_len = addr.bytelen; |
| 670 | } else if (arg == 5) { /* scope */ |
| 671 | uint32_t scope = 0; |
| 672 | NEXT_ARG(); |
| 673 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 674 | invarg(*argv, "scope"); |
| 675 | } |
| 676 | req.ifa.ifa_scope = scope; |
| 677 | scoped = 1; |
| 678 | } else if (arg == 6) { /* dev */ |
| 679 | NEXT_ARG(); |
| 680 | d = *argv; |
| 681 | } else if (arg == 7) { /* label */ |
| 682 | NEXT_ARG(); |
| 683 | l = *argv; |
| 684 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); |
| 685 | } else { |
| 686 | if (arg == 8) /* local */ |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 687 | NEXT_ARG(); |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 688 | if (local_len) { |
| 689 | duparg2("local", *argv); |
| 690 | } |
| 691 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 692 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 693 | req.ifa.ifa_family = lcl.family; |
| 694 | } |
| 695 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 696 | local_len = lcl.bytelen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 697 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 698 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 699 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 700 | |
Bernhard Reutner-Fischer | c5f30c0 | 2010-05-25 18:46:09 +0200 | [diff] [blame] | 701 | // d cannot be null here, NEXT_ARG() of "dev" ensures that |
| 702 | //if (d == NULL) { |
| 703 | // bb_error_msg(bb_msg_requires_arg, "\"dev\""); |
| 704 | // return -1; |
| 705 | //} |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 706 | if (l && strncmp(d, l, strlen(d)) != 0) { |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 707 | bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | if (peer_len == 0 && local_len && cmd != RTM_DELADDR) { |
| 711 | peer = lcl; |
| 712 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 713 | } |
| 714 | if (req.ifa.ifa_prefixlen == 0) |
| 715 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 716 | |
| 717 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 718 | inet_prefix brd; |
| 719 | int i; |
| 720 | if (req.ifa.ifa_family != AF_INET) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 721 | bb_error_msg_and_die("broadcast can be set only for IPv4 addresses"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 722 | } |
| 723 | brd = peer; |
| 724 | if (brd.bitlen <= 30) { |
| 725 | for (i=31; i>=brd.bitlen; i--) { |
| 726 | if (brd_len == -1) |
| 727 | brd.data[0] |= htonl(1<<(31-i)); |
| 728 | else |
| 729 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 730 | } |
| 731 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 732 | brd_len = brd.bytelen; |
| 733 | } |
| 734 | } |
| 735 | if (!scoped && cmd != RTM_DELADDR) |
| 736 | req.ifa.ifa_scope = default_scope(&lcl); |
| 737 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 738 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 739 | |
| 740 | ll_init_map(&rth); |
| 741 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 742 | req.ifa.ifa_index = xll_name_to_index(d); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 743 | |
| 744 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 745 | return 2; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 746 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 747 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 750 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 751 | int do_ipaddr(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 752 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 753 | static const char commands[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 754 | "add\0""delete\0""list\0""show\0""lst\0""flush\0"; |
Bernhard Reutner-Fischer | cc4493a | 2010-05-25 17:42:01 +0200 | [diff] [blame] | 755 | smalluint cmd = 2; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 756 | if (*argv) { |
Bernhard Reutner-Fischer | cc4493a | 2010-05-25 17:42:01 +0200 | [diff] [blame] | 757 | cmd = index_in_substrings(commands, *argv); |
| 758 | if (cmd > 5) |
| 759 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 760 | argv++; |
Bernhard Reutner-Fischer | cc4493a | 2010-05-25 17:42:01 +0200 | [diff] [blame] | 761 | if (cmd <= 1) |
| 762 | return ipaddr_modify((cmd == 0) ? RTM_NEWADDR : RTM_DELADDR, argv); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 763 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 764 | /* 2 == list, 3 == show, 4 == lst */ |
Bernhard Reutner-Fischer | cc4493a | 2010-05-25 17:42:01 +0200 | [diff] [blame] | 765 | return ipaddr_list_or_flush(argv, cmd == 5); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 766 | } |