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