"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 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 13 | //#include <sys/socket.h> |
| 14 | //#include <sys/ioctl.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | #include <fnmatch.h> |
Eric Andersen | 8004bb7 | 2003-01-14 08:06:07 +0000 | [diff] [blame] | 16 | #include <net/if.h> |
| 17 | #include <net/if_arp.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 19 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | #include "rt_names.h" |
| 21 | #include "utils.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 22 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 24 | typedef struct filter_t { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 25 | int ifindex; |
| 26 | int family; |
| 27 | int oneline; |
| 28 | int showqueue; |
| 29 | inet_prefix pfx; |
| 30 | int scope, scopemask; |
| 31 | int flags, flagmask; |
| 32 | int up; |
| 33 | char *label; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 34 | int flushed; |
| 35 | char *flushb; |
| 36 | int flushp; |
| 37 | int flushe; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | struct rtnl_handle *rth; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 39 | } filter_t; |
| 40 | |
| 41 | #define filter (*(filter_t*)&bb_common_bufsiz1) |
| 42 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 43 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 44 | static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 45 | { |
| 46 | fprintf(fp, "<"); |
| 47 | flags &= ~IFF_RUNNING; |
| 48 | #define _PF(f) if (flags&IFF_##f) { \ |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 49 | flags &= ~IFF_##f; \ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 50 | fprintf(fp, #f "%s", flags ? "," : ""); } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 51 | _PF(LOOPBACK); |
| 52 | _PF(BROADCAST); |
| 53 | _PF(POINTOPOINT); |
| 54 | _PF(MULTICAST); |
| 55 | _PF(NOARP); |
| 56 | #if 0 |
| 57 | _PF(ALLMULTI); |
| 58 | _PF(PROMISC); |
| 59 | _PF(MASTER); |
| 60 | _PF(SLAVE); |
| 61 | _PF(DEBUG); |
| 62 | _PF(DYNAMIC); |
| 63 | _PF(AUTOMEDIA); |
| 64 | _PF(PORTSEL); |
| 65 | _PF(NOTRAILERS); |
| 66 | #endif |
| 67 | _PF(UP); |
| 68 | #undef _PF |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 69 | if (flags) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 70 | fprintf(fp, "%x", flags); |
| 71 | if (mdown) |
| 72 | fprintf(fp, ",M-DOWN"); |
| 73 | fprintf(fp, "> "); |
| 74 | } |
| 75 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 76 | static void print_queuelen(char *name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 77 | { |
| 78 | struct ifreq ifr; |
| 79 | int s; |
| 80 | |
| 81 | s = socket(AF_INET, SOCK_STREAM, 0); |
| 82 | if (s < 0) |
| 83 | return; |
| 84 | |
| 85 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 86 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 87 | if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 88 | close(s); |
| 89 | return; |
| 90 | } |
| 91 | close(s); |
| 92 | |
| 93 | if (ifr.ifr_qlen) |
| 94 | printf("qlen %d", ifr.ifr_qlen); |
| 95 | } |
| 96 | |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 97 | static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 98 | const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | { |
| 100 | FILE *fp = (FILE*)arg; |
| 101 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 102 | struct rtattr * tb[IFLA_MAX+1]; |
| 103 | int len = n->nlmsg_len; |
| 104 | unsigned m_flag = 0; |
| 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 | |
| 113 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) |
| 114 | return 0; |
| 115 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 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 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 124 | if (filter.label |
| 125 | && (!filter.family || filter.family == AF_PACKET) |
| 126 | && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0) |
| 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) |
| 132 | fprintf(fp, "Deleted "); |
| 133 | |
| 134 | fprintf(fp, "%d: %s", ifi->ifi_index, |
| 135 | tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>"); |
| 136 | |
| 137 | if (tb[IFLA_LINK]) { |
| 138 | SPRINT_BUF(b1); |
| 139 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 140 | if (iflink == 0) |
| 141 | fprintf(fp, "@NONE: "); |
| 142 | else { |
| 143 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); |
| 144 | m_flag = ll_index_to_flags(iflink); |
| 145 | m_flag = !(m_flag & IFF_UP); |
| 146 | } |
| 147 | } else { |
| 148 | fprintf(fp, ": "); |
| 149 | } |
| 150 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
| 151 | |
| 152 | if (tb[IFLA_MTU]) |
| 153 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
| 154 | if (tb[IFLA_QDISC]) |
| 155 | fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); |
| 156 | #ifdef IFLA_MASTER |
| 157 | if (tb[IFLA_MASTER]) { |
| 158 | SPRINT_BUF(b1); |
| 159 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
| 160 | } |
| 161 | #endif |
| 162 | if (filter.showqueue) |
| 163 | print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME])); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 164 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 165 | if (!filter.family || filter.family == AF_PACKET) { |
| 166 | SPRINT_BUF(b1); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 167 | fprintf(fp, "%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] | 168 | |
| 169 | if (tb[IFLA_ADDRESS]) { |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 170 | fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 171 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 172 | ifi->ifi_type, |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 173 | b1, sizeof(b1)), fp); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 174 | } |
| 175 | if (tb[IFLA_BROADCAST]) { |
Denis Vlasenko | dfc0740 | 2007-10-29 19:33:26 +0000 | [diff] [blame] | 176 | if (ifi->ifi_flags & IFF_POINTOPOINT) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 177 | fprintf(fp, " peer "); |
| 178 | else |
| 179 | fprintf(fp, " brd "); |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 180 | fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 181 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 182 | ifi->ifi_type, |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 183 | b1, sizeof(b1)), fp); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 186 | fputc('\n', fp); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 187 | fflush(fp); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 191 | static int flush_update(void) |
| 192 | { |
| 193 | if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 194 | bb_perror_msg("failed to send flush request"); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 195 | return -1; |
| 196 | } |
| 197 | filter.flushp = 0; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 201 | static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who, |
| 202 | struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 203 | { |
| 204 | FILE *fp = (FILE*)arg; |
| 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; |
| 232 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 233 | return 0; |
| 234 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 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; |
| 268 | filter.flushed++; |
| 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) |
| 273 | fprintf(fp, "Deleted "); |
| 274 | |
| 275 | if (filter.oneline) |
| 276 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
| 277 | if (ifa->ifa_family == AF_INET) |
| 278 | fprintf(fp, " inet "); |
| 279 | else if (ifa->ifa_family == AF_INET6) |
| 280 | fprintf(fp, " inet6 "); |
| 281 | else |
| 282 | fprintf(fp, " family %d ", ifa->ifa_family); |
| 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 | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 288 | abuf, sizeof(abuf)), fp); |
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) { |
| 292 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); |
| 293 | } else { |
| 294 | fprintf(fp, " peer %s/%d ", |
| 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]) { |
| 304 | fprintf(fp, "brd %s ", |
| 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]) { |
| 311 | fprintf(fp, "any %s ", |
| 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 | } |
| 317 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); |
| 318 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
| 319 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
| 320 | fprintf(fp, "secondary "); |
| 321 | } |
| 322 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { |
| 323 | ifa->ifa_flags &= ~IFA_F_TENTATIVE; |
| 324 | fprintf(fp, "tentative "); |
| 325 | } |
| 326 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { |
| 327 | ifa->ifa_flags &= ~IFA_F_DEPRECATED; |
| 328 | fprintf(fp, "deprecated "); |
| 329 | } |
| 330 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { |
| 331 | fprintf(fp, "dynamic "); |
| 332 | } else |
| 333 | ifa->ifa_flags &= ~IFA_F_PERMANENT; |
| 334 | if (ifa->ifa_flags) |
| 335 | fprintf(fp, "flags %02x ", ifa->ifa_flags); |
| 336 | if (rta_tb[IFA_LABEL]) |
Denis Vlasenko | 605b20e | 2007-09-30 16:22:36 +0000 | [diff] [blame] | 337 | fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), fp); |
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 | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 341 | fputc(_SL_, fp); |
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); |
| 350 | fprintf(fp, " %s", buf); |
| 351 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 352 | fputc('\n', fp); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 353 | fflush(fp); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | struct nlmsg_list |
| 359 | { |
| 360 | struct nlmsg_list *next; |
| 361 | struct nlmsghdr h; |
| 362 | }; |
| 363 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 364 | static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) |
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 | |
| 380 | print_addrinfo(NULL, n, fp); |
| 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 | |
| 399 | for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */; |
| 400 | *lp = h; |
| 401 | |
| 402 | ll_remember_index(who, n, NULL); |
| 403 | return 0; |
| 404 | } |
| 405 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 406 | static void ipaddr_reset_filter(int _oneline) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 407 | { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 408 | memset(&filter, 0, sizeof(filter)); |
| 409 | filter.oneline = _oneline; |
| 410 | } |
| 411 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 412 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 413 | int ipaddr_list_or_flush(char **argv, int flush) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 414 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 415 | 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] | 416 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 417 | struct nlmsg_list *linfo = NULL; |
| 418 | struct nlmsg_list *ainfo = NULL; |
| 419 | struct nlmsg_list *l; |
| 420 | struct rtnl_handle rth; |
| 421 | char *filter_dev = NULL; |
| 422 | int no_link = 0; |
| 423 | |
| 424 | ipaddr_reset_filter(oneline); |
| 425 | filter.showqueue = 1; |
| 426 | |
| 427 | if (filter.family == AF_UNSPEC) |
| 428 | filter.family = preferred_family; |
| 429 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 430 | if (flush) { |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 431 | if (!*argv) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 432 | bb_error_msg_and_die(bb_msg_requires_arg, "flush"); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 433 | } |
| 434 | if (filter.family == AF_PACKET) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 435 | bb_error_msg_and_die("cannot flush link addresses"); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 439 | while (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 440 | const int option_num = index_in_strings(option, *argv); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 441 | switch (option_num) { |
| 442 | case 0: /* to */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 443 | NEXT_ARG(); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 444 | get_prefix(&filter.pfx, *argv, filter.family); |
| 445 | if (filter.family == AF_UNSPEC) { |
| 446 | filter.family = filter.pfx.family; |
| 447 | } |
| 448 | break; |
| 449 | case 1: /* scope */ |
| 450 | { |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 451 | uint32_t scope = 0; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 452 | NEXT_ARG(); |
| 453 | filter.scopemask = -1; |
| 454 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 455 | if (strcmp(*argv, "all") != 0) { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 456 | invarg(*argv, "scope"); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 457 | } |
| 458 | scope = RT_SCOPE_NOWHERE; |
| 459 | filter.scopemask = 0; |
| 460 | } |
| 461 | filter.scope = scope; |
| 462 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 463 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 464 | case 2: /* up */ |
| 465 | filter.up = 1; |
| 466 | break; |
| 467 | case 3: /* label */ |
| 468 | NEXT_ARG(); |
| 469 | filter.label = *argv; |
| 470 | break; |
| 471 | case 4: /* dev */ |
| 472 | NEXT_ARG(); |
| 473 | default: |
| 474 | if (filter_dev) { |
| 475 | duparg2("dev", *argv); |
| 476 | } |
| 477 | filter_dev = *argv; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 478 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 479 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 482 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 483 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 484 | xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK); |
| 485 | xrtnl_dump_filter(&rth, store_nlmsg, &linfo); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 486 | |
| 487 | if (filter_dev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 488 | filter.ifindex = xll_name_to_index(filter_dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 491 | if (flush) { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 492 | char flushb[4096-512]; |
| 493 | |
| 494 | filter.flushb = flushb; |
| 495 | filter.flushp = 0; |
| 496 | filter.flushe = sizeof(flushb); |
| 497 | filter.rth = &rth; |
| 498 | |
| 499 | for (;;) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 500 | xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 501 | filter.flushed = 0; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 502 | xrtnl_dump_filter(&rth, print_addrinfo, stdout); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 503 | if (filter.flushed == 0) { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 504 | return 0; |
| 505 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 506 | if (flush_update() < 0) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 507 | return 1; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 511 | if (filter.family != AF_PACKET) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 512 | xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR); |
| 513 | xrtnl_dump_filter(&rth, store_nlmsg, &ainfo); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
| 517 | if (filter.family && filter.family != AF_PACKET) { |
| 518 | struct nlmsg_list **lp; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 519 | lp = &linfo; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 520 | |
| 521 | if (filter.oneline) |
| 522 | no_link = 1; |
| 523 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 524 | while ((l = *lp) != NULL) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 525 | int ok = 0; |
| 526 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 527 | struct nlmsg_list *a; |
| 528 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 529 | for (a = ainfo; a; a = a->next) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 530 | struct nlmsghdr *n = &a->h; |
| 531 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 532 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 533 | if (ifa->ifa_index != ifi->ifi_index || |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 534 | (filter.family && filter.family != ifa->ifa_family)) |
| 535 | continue; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 536 | if ((filter.scope ^ ifa->ifa_scope) & filter.scopemask) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 537 | continue; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 538 | if ((filter.flags ^ ifa->ifa_flags) & filter.flagmask) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 539 | continue; |
| 540 | if (filter.pfx.family || filter.label) { |
| 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 | |
| 547 | if (filter.pfx.family && tb[IFA_LOCAL]) { |
| 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])); |
| 552 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 553 | continue; |
| 554 | } |
| 555 | if (filter.label) { |
| 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); |
| 562 | if (fnmatch(filter.label, label, 0) != 0) |
| 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) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 578 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { |
| 579 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 580 | if (filter.family != AF_PACKET) |
| 581 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); |
| 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) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 627 | const int option_num = index_in_strings(option, *argv); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 628 | switch (option_num) { |
| 629 | case 0: /* peer */ |
| 630 | case 1: /* remote */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 631 | NEXT_ARG(); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 632 | |
| 633 | if (peer_len) { |
| 634 | duparg("peer", *argv); |
| 635 | } |
| 636 | get_prefix(&peer, *argv, req.ifa.ifa_family); |
| 637 | peer_len = peer.bytelen; |
| 638 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 639 | req.ifa.ifa_family = peer.family; |
| 640 | } |
| 641 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); |
| 642 | req.ifa.ifa_prefixlen = peer.bitlen; |
| 643 | break; |
| 644 | case 2: /* broadcast */ |
| 645 | case 3: /* brd */ |
| 646 | { |
| 647 | inet_prefix addr; |
| 648 | NEXT_ARG(); |
| 649 | if (brd_len) { |
| 650 | duparg("broadcast", *argv); |
| 651 | } |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 652 | if (LONE_CHAR(*argv, '+')) { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 653 | brd_len = -1; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 654 | } else if (LONE_DASH(*argv)) { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 655 | brd_len = -2; |
| 656 | } else { |
| 657 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 658 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 659 | req.ifa.ifa_family = addr.family; |
| 660 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); |
| 661 | brd_len = addr.bytelen; |
| 662 | } |
| 663 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 664 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 665 | case 4: /* anycast */ |
| 666 | { |
| 667 | inet_prefix addr; |
| 668 | NEXT_ARG(); |
| 669 | if (any_len) { |
| 670 | duparg("anycast", *argv); |
| 671 | } |
| 672 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 673 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 674 | req.ifa.ifa_family = addr.family; |
| 675 | } |
| 676 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); |
| 677 | any_len = addr.bytelen; |
| 678 | break; |
| 679 | } |
| 680 | case 5: /* scope */ |
| 681 | { |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 682 | uint32_t scope = 0; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 683 | NEXT_ARG(); |
| 684 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 685 | invarg(*argv, "scope"); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 686 | } |
| 687 | req.ifa.ifa_scope = scope; |
| 688 | scoped = 1; |
| 689 | break; |
| 690 | } |
| 691 | case 6: /* dev */ |
| 692 | NEXT_ARG(); |
| 693 | d = *argv; |
| 694 | break; |
| 695 | case 7: /* label */ |
| 696 | NEXT_ARG(); |
| 697 | l = *argv; |
| 698 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); |
| 699 | break; |
| 700 | case 8: /* local */ |
| 701 | NEXT_ARG(); |
| 702 | default: |
| 703 | if (local_len) { |
| 704 | duparg2("local", *argv); |
| 705 | } |
| 706 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 707 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 708 | req.ifa.ifa_family = lcl.family; |
| 709 | } |
| 710 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 711 | local_len = lcl.bytelen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 712 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 713 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 714 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 715 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 716 | if (d == NULL) { |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 717 | bb_error_msg(bb_msg_requires_arg, "\"dev\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 718 | return -1; |
| 719 | } |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 720 | if (l && strncmp(d, l, strlen(d)) != 0) { |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 721 | 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] | 722 | } |
| 723 | |
| 724 | if (peer_len == 0 && local_len && cmd != RTM_DELADDR) { |
| 725 | peer = lcl; |
| 726 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 727 | } |
| 728 | if (req.ifa.ifa_prefixlen == 0) |
| 729 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 730 | |
| 731 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 732 | inet_prefix brd; |
| 733 | int i; |
| 734 | if (req.ifa.ifa_family != AF_INET) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 735 | 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] | 736 | } |
| 737 | brd = peer; |
| 738 | if (brd.bitlen <= 30) { |
| 739 | for (i=31; i>=brd.bitlen; i--) { |
| 740 | if (brd_len == -1) |
| 741 | brd.data[0] |= htonl(1<<(31-i)); |
| 742 | else |
| 743 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 744 | } |
| 745 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 746 | brd_len = brd.bytelen; |
| 747 | } |
| 748 | } |
| 749 | if (!scoped && cmd != RTM_DELADDR) |
| 750 | req.ifa.ifa_scope = default_scope(&lcl); |
| 751 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 752 | xrtnl_open(&rth); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 753 | |
| 754 | ll_init_map(&rth); |
| 755 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 756 | req.ifa.ifa_index = xll_name_to_index(d); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 757 | |
| 758 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 759 | return 2; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 760 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 761 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 764 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 765 | int do_ipaddr(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 766 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 767 | static const char commands[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 768 | "add\0""delete\0""list\0""show\0""lst\0""flush\0"; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 769 | |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 770 | int command_num = 2; /* default command is list */ |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 771 | |
| 772 | if (*argv) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 773 | command_num = index_in_substrings(commands, *argv); |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 774 | if (command_num < 0 || command_num > 5) |
| 775 | bb_error_msg_and_die("unknown command %s", *argv); |
| 776 | argv++; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 777 | } |
Bernhard Reutner-Fischer | cd0e80c | 2007-06-20 14:53:49 +0000 | [diff] [blame] | 778 | if (command_num == 0) /* add */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 779 | return ipaddr_modify(RTM_NEWADDR, argv); |
| 780 | if (command_num == 1) /* delete */ |
| 781 | return ipaddr_modify(RTM_DELADDR, argv); |
| 782 | if (command_num == 5) /* flush */ |
| 783 | return ipaddr_list_or_flush(argv, 1); |
| 784 | /* 2 == list, 3 == show, 4 == lst */ |
| 785 | return ipaddr_list_or_flush(argv, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 786 | } |