osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ipaddress.c "ip address". |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
| 11 | * Changes: |
| 12 | * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <unistd.h> |
| 18 | #include <syslog.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/ioctl.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <string.h> |
| 26 | #include <fnmatch.h> |
| 27 | |
osdl.org!shemminger | e5779fb | 2004-06-09 22:56:28 +0000 | [diff] [blame^] | 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/if_arp.h> |
| 30 | #include <linux/sockios.h> |
| 31 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 32 | #include "rt_names.h" |
| 33 | #include "utils.h" |
| 34 | #include "ll_map.h" |
| 35 | #include "ip_common.h" |
| 36 | |
| 37 | static struct |
| 38 | { |
| 39 | int ifindex; |
| 40 | int family; |
| 41 | int oneline; |
| 42 | int showqueue; |
| 43 | inet_prefix pfx; |
| 44 | int scope, scopemask; |
| 45 | int flags, flagmask; |
| 46 | int up; |
| 47 | char *label; |
| 48 | int flushed; |
| 49 | char *flushb; |
| 50 | int flushp; |
| 51 | int flushe; |
| 52 | struct rtnl_handle *rth; |
| 53 | } filter; |
| 54 | |
| 55 | static int do_link; |
| 56 | |
| 57 | static void usage(void) __attribute__((noreturn)); |
| 58 | |
| 59 | static void usage(void) |
| 60 | { |
| 61 | if (do_link) { |
| 62 | iplink_usage(); |
| 63 | } |
| 64 | fprintf(stderr, "Usage: ip addr {add|del} IFADDR dev STRING\n"); |
| 65 | fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n"); |
| 66 | fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n"); |
| 67 | fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n"); |
| 68 | fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n"); |
| 69 | fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n"); |
| 70 | fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n"); |
| 71 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
| 72 | fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n"); |
| 73 | fprintf(stderr, " tentative | deprecated ]\n"); |
| 74 | exit(-1); |
| 75 | } |
| 76 | |
| 77 | void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) |
| 78 | { |
| 79 | fprintf(fp, "<"); |
| 80 | flags &= ~IFF_RUNNING; |
| 81 | #define _PF(f) if (flags&IFF_##f) { \ |
| 82 | flags &= ~IFF_##f ; \ |
| 83 | fprintf(fp, #f "%s", flags ? "," : ""); } |
| 84 | _PF(LOOPBACK); |
| 85 | _PF(BROADCAST); |
| 86 | _PF(POINTOPOINT); |
| 87 | _PF(MULTICAST); |
| 88 | _PF(NOARP); |
| 89 | _PF(ALLMULTI); |
| 90 | _PF(PROMISC); |
| 91 | _PF(MASTER); |
| 92 | _PF(SLAVE); |
| 93 | _PF(DEBUG); |
| 94 | _PF(DYNAMIC); |
| 95 | _PF(AUTOMEDIA); |
| 96 | _PF(PORTSEL); |
| 97 | _PF(NOTRAILERS); |
| 98 | _PF(UP); |
| 99 | #undef _PF |
| 100 | if (flags) |
| 101 | fprintf(fp, "%x", flags); |
| 102 | if (mdown) |
| 103 | fprintf(fp, ",M-DOWN"); |
| 104 | fprintf(fp, "> "); |
| 105 | } |
| 106 | |
| 107 | void print_queuelen(char *name) |
| 108 | { |
| 109 | struct ifreq ifr; |
| 110 | int s; |
| 111 | |
| 112 | s = socket(AF_INET, SOCK_STREAM, 0); |
| 113 | if (s < 0) |
| 114 | return; |
| 115 | |
| 116 | memset(&ifr, 0, sizeof(ifr)); |
| 117 | strcpy(ifr.ifr_name, name); |
| 118 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
| 119 | perror("SIOCGIFXQLEN"); |
| 120 | close(s); |
| 121 | return; |
| 122 | } |
| 123 | close(s); |
| 124 | |
| 125 | if (ifr.ifr_qlen) |
| 126 | printf("qlen %d", ifr.ifr_qlen); |
| 127 | } |
| 128 | |
| 129 | int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 130 | { |
| 131 | FILE *fp = (FILE*)arg; |
| 132 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 133 | struct rtattr * tb[IFLA_MAX+1]; |
| 134 | int len = n->nlmsg_len; |
| 135 | unsigned m_flag = 0; |
| 136 | |
| 137 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) |
| 138 | return 0; |
| 139 | |
| 140 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 141 | if (len < 0) |
| 142 | return -1; |
| 143 | |
| 144 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) |
| 145 | return 0; |
| 146 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 147 | return 0; |
| 148 | |
| 149 | memset(tb, 0, sizeof(tb)); |
| 150 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 151 | if (tb[IFLA_IFNAME] == NULL) { |
| 152 | fprintf(stderr, "BUG: nil ifname\n"); |
| 153 | return -1; |
| 154 | } |
| 155 | if (filter.label && |
| 156 | (!filter.family || filter.family == AF_PACKET) && |
| 157 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) |
| 158 | return 0; |
| 159 | |
| 160 | if (n->nlmsg_type == RTM_DELLINK) |
| 161 | fprintf(fp, "Deleted "); |
| 162 | |
| 163 | fprintf(fp, "%d: %s", ifi->ifi_index, |
| 164 | tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>"); |
| 165 | |
| 166 | if (tb[IFLA_LINK]) { |
| 167 | SPRINT_BUF(b1); |
| 168 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 169 | if (iflink == 0) |
| 170 | fprintf(fp, "@NONE: "); |
| 171 | else { |
| 172 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); |
| 173 | m_flag = ll_index_to_flags(iflink); |
| 174 | m_flag = !(m_flag & IFF_UP); |
| 175 | } |
| 176 | } else { |
| 177 | fprintf(fp, ": "); |
| 178 | } |
| 179 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
| 180 | |
| 181 | if (tb[IFLA_MTU]) |
| 182 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
| 183 | if (tb[IFLA_QDISC]) |
| 184 | fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); |
| 185 | #ifdef IFLA_MASTER |
| 186 | if (tb[IFLA_MASTER]) { |
| 187 | SPRINT_BUF(b1); |
| 188 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
| 189 | } |
| 190 | #endif |
| 191 | if (filter.showqueue) |
| 192 | print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME])); |
| 193 | |
| 194 | if (!filter.family || filter.family == AF_PACKET) { |
| 195 | SPRINT_BUF(b1); |
| 196 | fprintf(fp, "%s", _SL_); |
| 197 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); |
| 198 | |
| 199 | if (tb[IFLA_ADDRESS]) { |
| 200 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
| 201 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 202 | ifi->ifi_type, |
| 203 | b1, sizeof(b1))); |
| 204 | } |
| 205 | if (tb[IFLA_BROADCAST]) { |
| 206 | if (ifi->ifi_flags&IFF_POINTOPOINT) |
| 207 | fprintf(fp, " peer "); |
| 208 | else |
| 209 | fprintf(fp, " brd "); |
| 210 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
| 211 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 212 | ifi->ifi_type, |
| 213 | b1, sizeof(b1))); |
| 214 | } |
| 215 | } |
| 216 | if (do_link && tb[IFLA_STATS] && show_stats) { |
| 217 | struct net_device_stats slocal; |
| 218 | struct net_device_stats *s = RTA_DATA(tb[IFLA_STATS]); |
| 219 | if (((unsigned long)s) & (sizeof(unsigned long)-1)) { |
| 220 | memcpy(&slocal, s, sizeof(slocal)); |
| 221 | s = &slocal; |
| 222 | } |
| 223 | fprintf(fp, "%s", _SL_); |
| 224 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", |
| 225 | s->rx_compressed ? "compressed" : "", _SL_); |
| 226 | fprintf(fp, " %-10lu %-8lu %-7lu %-7lu %-7lu %-7lu", |
| 227 | s->rx_bytes, s->rx_packets, s->rx_errors, |
| 228 | s->rx_dropped, s->rx_over_errors, |
| 229 | s->multicast |
| 230 | ); |
| 231 | if (s->rx_compressed) |
| 232 | fprintf(fp, " %-7lu", s->rx_compressed); |
| 233 | if (show_stats > 1) { |
| 234 | fprintf(fp, "%s", _SL_); |
| 235 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); |
| 236 | fprintf(fp, " %-7lu %-7lu %-7lu %-7lu %-7lu", |
| 237 | s->rx_length_errors, |
| 238 | s->rx_crc_errors, |
| 239 | s->rx_frame_errors, |
| 240 | s->rx_fifo_errors, |
| 241 | s->rx_missed_errors |
| 242 | ); |
| 243 | } |
| 244 | fprintf(fp, "%s", _SL_); |
| 245 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", |
| 246 | s->tx_compressed ? "compressed" : "", _SL_); |
| 247 | fprintf(fp, " %-10lu %-8lu %-7lu %-7lu %-7lu %-7lu", |
| 248 | s->tx_bytes, s->tx_packets, s->tx_errors, |
| 249 | s->tx_dropped, s->tx_carrier_errors, s->collisions); |
| 250 | if (s->tx_compressed) |
| 251 | fprintf(fp, " %-7lu", s->tx_compressed); |
| 252 | if (show_stats > 1) { |
| 253 | fprintf(fp, "%s", _SL_); |
| 254 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); |
| 255 | fprintf(fp, " %-7lu %-7lu %-7lu %-7lu", |
| 256 | s->tx_aborted_errors, |
| 257 | s->tx_fifo_errors, |
| 258 | s->tx_window_errors, |
| 259 | s->tx_heartbeat_errors |
| 260 | ); |
| 261 | } |
| 262 | } |
| 263 | fprintf(fp, "\n"); |
| 264 | fflush(fp); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int flush_update(void) |
| 269 | { |
| 270 | if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { |
| 271 | perror("Failed to send flush request\n"); |
| 272 | return -1; |
| 273 | } |
| 274 | filter.flushp = 0; |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 279 | { |
| 280 | FILE *fp = (FILE*)arg; |
| 281 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 282 | int len = n->nlmsg_len; |
| 283 | struct rtattr * rta_tb[IFA_MAX+1]; |
| 284 | char abuf[256]; |
| 285 | SPRINT_BUF(b1); |
| 286 | |
| 287 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) |
| 288 | return 0; |
| 289 | len -= NLMSG_LENGTH(sizeof(*ifa)); |
| 290 | if (len < 0) { |
| 291 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) |
| 296 | return 0; |
| 297 | |
| 298 | memset(rta_tb, 0, sizeof(rta_tb)); |
| 299 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
| 300 | |
| 301 | if (!rta_tb[IFA_LOCAL]) |
| 302 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; |
| 303 | if (!rta_tb[IFA_ADDRESS]) |
| 304 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; |
| 305 | |
| 306 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) |
| 307 | return 0; |
| 308 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 309 | return 0; |
| 310 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 311 | return 0; |
| 312 | if (filter.label) { |
| 313 | SPRINT_BUF(b1); |
| 314 | const char *label; |
| 315 | if (rta_tb[IFA_LABEL]) |
| 316 | label = RTA_DATA(rta_tb[IFA_LABEL]); |
| 317 | else |
| 318 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 319 | if (fnmatch(filter.label, label, 0) != 0) |
| 320 | return 0; |
| 321 | } |
| 322 | if (filter.pfx.family) { |
| 323 | if (rta_tb[IFA_LOCAL]) { |
| 324 | inet_prefix dst; |
| 325 | memset(&dst, 0, sizeof(dst)); |
| 326 | dst.family = ifa->ifa_family; |
| 327 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); |
| 328 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 329 | return 0; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (filter.flushb) { |
| 334 | struct nlmsghdr *fn; |
| 335 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
| 336 | if (flush_update()) |
| 337 | return -1; |
| 338 | } |
| 339 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
| 340 | memcpy(fn, n, n->nlmsg_len); |
| 341 | fn->nlmsg_type = RTM_DELADDR; |
| 342 | fn->nlmsg_flags = NLM_F_REQUEST; |
| 343 | fn->nlmsg_seq = ++filter.rth->seq; |
| 344 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
| 345 | filter.flushed++; |
| 346 | if (show_stats < 2) |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | if (n->nlmsg_type == RTM_DELADDR) |
| 351 | fprintf(fp, "Deleted "); |
| 352 | |
| 353 | if (filter.oneline || filter.flushb) |
| 354 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
| 355 | if (ifa->ifa_family == AF_INET) |
| 356 | fprintf(fp, " inet "); |
| 357 | else if (ifa->ifa_family == AF_INET6) |
| 358 | fprintf(fp, " inet6 "); |
| 359 | else if (ifa->ifa_family == AF_DECnet) |
| 360 | fprintf(fp, " dnet "); |
| 361 | else if (ifa->ifa_family == AF_IPX) |
| 362 | fprintf(fp, " ipx "); |
| 363 | else |
| 364 | fprintf(fp, " family %d ", ifa->ifa_family); |
| 365 | |
| 366 | if (rta_tb[IFA_LOCAL]) { |
| 367 | fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family, |
| 368 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), |
| 369 | RTA_DATA(rta_tb[IFA_LOCAL]), |
| 370 | abuf, sizeof(abuf))); |
| 371 | |
| 372 | if (rta_tb[IFA_ADDRESS] == NULL || |
| 373 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { |
| 374 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); |
| 375 | } else { |
| 376 | fprintf(fp, " peer %s/%d ", |
| 377 | rt_addr_n2a(ifa->ifa_family, |
| 378 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), |
| 379 | RTA_DATA(rta_tb[IFA_ADDRESS]), |
| 380 | abuf, sizeof(abuf)), |
| 381 | ifa->ifa_prefixlen); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (rta_tb[IFA_BROADCAST]) { |
| 386 | fprintf(fp, "brd %s ", |
| 387 | rt_addr_n2a(ifa->ifa_family, |
| 388 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), |
| 389 | RTA_DATA(rta_tb[IFA_BROADCAST]), |
| 390 | abuf, sizeof(abuf))); |
| 391 | } |
| 392 | if (rta_tb[IFA_ANYCAST]) { |
| 393 | fprintf(fp, "any %s ", |
| 394 | rt_addr_n2a(ifa->ifa_family, |
| 395 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), |
| 396 | RTA_DATA(rta_tb[IFA_ANYCAST]), |
| 397 | abuf, sizeof(abuf))); |
| 398 | } |
| 399 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); |
| 400 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
| 401 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
| 402 | fprintf(fp, "secondary "); |
| 403 | } |
| 404 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { |
| 405 | ifa->ifa_flags &= ~IFA_F_TENTATIVE; |
| 406 | fprintf(fp, "tentative "); |
| 407 | } |
| 408 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { |
| 409 | ifa->ifa_flags &= ~IFA_F_DEPRECATED; |
| 410 | fprintf(fp, "deprecated "); |
| 411 | } |
| 412 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { |
| 413 | fprintf(fp, "dynamic "); |
| 414 | } else |
| 415 | ifa->ifa_flags &= ~IFA_F_PERMANENT; |
| 416 | if (ifa->ifa_flags) |
| 417 | fprintf(fp, "flags %02x ", ifa->ifa_flags); |
| 418 | if (rta_tb[IFA_LABEL]) |
| 419 | fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL])); |
| 420 | if (rta_tb[IFA_CACHEINFO]) { |
| 421 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
| 422 | char buf[128]; |
| 423 | fprintf(fp, "%s", _SL_); |
| 424 | if (ci->ifa_valid == 0xFFFFFFFFU) |
| 425 | sprintf(buf, "valid_lft forever"); |
| 426 | else |
| 427 | sprintf(buf, "valid_lft %dsec", ci->ifa_valid); |
| 428 | if (ci->ifa_prefered == 0xFFFFFFFFU) |
| 429 | sprintf(buf+strlen(buf), " preferred_lft forever"); |
| 430 | else |
| 431 | sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); |
| 432 | fprintf(fp, " %s", buf); |
| 433 | } |
| 434 | fprintf(fp, "\n"); |
| 435 | fflush(fp); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | struct nlmsg_list |
| 441 | { |
| 442 | struct nlmsg_list *next; |
| 443 | struct nlmsghdr h; |
| 444 | }; |
| 445 | |
| 446 | int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) |
| 447 | { |
| 448 | for ( ;ainfo ; ainfo = ainfo->next) { |
| 449 | struct nlmsghdr *n = &ainfo->h; |
| 450 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 451 | |
| 452 | if (n->nlmsg_type != RTM_NEWADDR) |
| 453 | continue; |
| 454 | |
| 455 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) |
| 456 | return -1; |
| 457 | |
| 458 | if (ifa->ifa_index != ifindex || |
| 459 | (filter.family && filter.family != ifa->ifa_family)) |
| 460 | continue; |
| 461 | |
| 462 | print_addrinfo(NULL, n, fp); |
| 463 | } |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 469 | { |
| 470 | struct nlmsg_list **linfo = (struct nlmsg_list**)arg; |
| 471 | struct nlmsg_list *h; |
| 472 | struct nlmsg_list **lp; |
| 473 | |
| 474 | h = malloc(n->nlmsg_len+sizeof(void*)); |
| 475 | if (h == NULL) |
| 476 | return -1; |
| 477 | |
| 478 | memcpy(&h->h, n, n->nlmsg_len); |
| 479 | h->next = NULL; |
| 480 | |
| 481 | for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */; |
| 482 | *lp = h; |
| 483 | |
| 484 | ll_remember_index(who, n, NULL); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | int ipaddr_list_or_flush(int argc, char **argv, int flush) |
| 489 | { |
| 490 | struct nlmsg_list *linfo = NULL; |
| 491 | struct nlmsg_list *ainfo = NULL; |
| 492 | struct nlmsg_list *l; |
| 493 | struct rtnl_handle rth; |
| 494 | char *filter_dev = NULL; |
| 495 | int no_link = 0; |
| 496 | |
| 497 | ipaddr_reset_filter(oneline); |
| 498 | filter.showqueue = 1; |
| 499 | |
| 500 | if (filter.family == AF_UNSPEC) |
| 501 | filter.family = preferred_family; |
| 502 | |
| 503 | if (flush) { |
| 504 | if (argc <= 0) { |
| 505 | fprintf(stderr, "Flush requires arguments.\n"); |
| 506 | return -1; |
| 507 | } |
| 508 | if (filter.family == AF_PACKET) { |
| 509 | fprintf(stderr, "Cannot flush link addresses.\n"); |
| 510 | return -1; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | while (argc > 0) { |
| 515 | if (strcmp(*argv, "to") == 0) { |
| 516 | NEXT_ARG(); |
| 517 | get_prefix(&filter.pfx, *argv, filter.family); |
| 518 | if (filter.family == AF_UNSPEC) |
| 519 | filter.family = filter.pfx.family; |
| 520 | } else if (strcmp(*argv, "scope") == 0) { |
| 521 | int scope = 0; |
| 522 | NEXT_ARG(); |
| 523 | filter.scopemask = -1; |
| 524 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 525 | if (strcmp(*argv, "all") != 0) |
| 526 | invarg("invalid \"scope\"\n", *argv); |
| 527 | scope = RT_SCOPE_NOWHERE; |
| 528 | filter.scopemask = 0; |
| 529 | } |
| 530 | filter.scope = scope; |
| 531 | } else if (strcmp(*argv, "up") == 0) { |
| 532 | filter.up = 1; |
| 533 | } else if (strcmp(*argv, "dynamic") == 0) { |
| 534 | filter.flags &= ~IFA_F_PERMANENT; |
| 535 | filter.flagmask |= IFA_F_PERMANENT; |
| 536 | } else if (strcmp(*argv, "permanent") == 0) { |
| 537 | filter.flags |= IFA_F_PERMANENT; |
| 538 | filter.flagmask |= IFA_F_PERMANENT; |
| 539 | } else if (strcmp(*argv, "secondary") == 0) { |
| 540 | filter.flags |= IFA_F_SECONDARY; |
| 541 | filter.flagmask |= IFA_F_SECONDARY; |
| 542 | } else if (strcmp(*argv, "primary") == 0) { |
| 543 | filter.flags &= ~IFA_F_SECONDARY; |
| 544 | filter.flagmask |= IFA_F_SECONDARY; |
| 545 | } else if (strcmp(*argv, "tentative") == 0) { |
| 546 | filter.flags |= IFA_F_TENTATIVE; |
| 547 | filter.flagmask |= IFA_F_TENTATIVE; |
| 548 | } else if (strcmp(*argv, "deprecated") == 0) { |
| 549 | filter.flags |= IFA_F_DEPRECATED; |
| 550 | filter.flagmask |= IFA_F_DEPRECATED; |
| 551 | } else if (strcmp(*argv, "label") == 0) { |
| 552 | NEXT_ARG(); |
| 553 | filter.label = *argv; |
| 554 | } else { |
| 555 | if (strcmp(*argv, "dev") == 0) { |
| 556 | NEXT_ARG(); |
| 557 | } |
| 558 | if (matches(*argv, "help") == 0) |
| 559 | usage(); |
| 560 | if (filter_dev) |
| 561 | duparg2("dev", *argv); |
| 562 | filter_dev = *argv; |
| 563 | } |
| 564 | argv++; argc--; |
| 565 | } |
| 566 | |
| 567 | if (rtnl_open(&rth, 0) < 0) |
| 568 | exit(1); |
| 569 | |
| 570 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { |
| 571 | perror("Cannot send dump request"); |
| 572 | exit(1); |
| 573 | } |
| 574 | |
| 575 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) { |
| 576 | fprintf(stderr, "Dump terminated\n"); |
| 577 | exit(1); |
| 578 | } |
| 579 | |
| 580 | if (filter_dev) { |
| 581 | filter.ifindex = ll_name_to_index(filter_dev); |
| 582 | if (filter.ifindex <= 0) { |
| 583 | fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev); |
| 584 | return -1; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (flush) { |
| 589 | int round = 0; |
| 590 | char flushb[4096-512]; |
| 591 | |
| 592 | filter.flushb = flushb; |
| 593 | filter.flushp = 0; |
| 594 | filter.flushe = sizeof(flushb); |
| 595 | filter.rth = &rth; |
| 596 | |
| 597 | for (;;) { |
| 598 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 599 | perror("Cannot send dump request"); |
| 600 | exit(1); |
| 601 | } |
| 602 | filter.flushed = 0; |
| 603 | if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) { |
| 604 | fprintf(stderr, "Flush terminated\n"); |
| 605 | exit(1); |
| 606 | } |
| 607 | if (filter.flushed == 0) { |
| 608 | if (round == 0) { |
| 609 | fprintf(stderr, "Nothing to flush.\n"); |
| 610 | } else if (show_stats) |
| 611 | printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); |
| 612 | fflush(stdout); |
| 613 | return 0; |
| 614 | } |
| 615 | round++; |
| 616 | if (flush_update() < 0) |
| 617 | exit(1); |
| 618 | if (show_stats) { |
| 619 | printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed); |
| 620 | fflush(stdout); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (filter.family != AF_PACKET) { |
| 626 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 627 | perror("Cannot send dump request"); |
| 628 | exit(1); |
| 629 | } |
| 630 | |
| 631 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) { |
| 632 | fprintf(stderr, "Dump terminated\n"); |
| 633 | exit(1); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | |
| 638 | if (filter.family && filter.family != AF_PACKET) { |
| 639 | struct nlmsg_list **lp; |
| 640 | lp=&linfo; |
| 641 | |
| 642 | if (filter.oneline) |
| 643 | no_link = 1; |
| 644 | |
| 645 | while ((l=*lp)!=NULL) { |
| 646 | int ok = 0; |
| 647 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 648 | struct nlmsg_list *a; |
| 649 | |
| 650 | for (a=ainfo; a; a=a->next) { |
| 651 | struct nlmsghdr *n = &a->h; |
| 652 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 653 | |
| 654 | if (ifa->ifa_index != ifi->ifi_index || |
| 655 | (filter.family && filter.family != ifa->ifa_family)) |
| 656 | continue; |
| 657 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 658 | continue; |
| 659 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 660 | continue; |
| 661 | if (filter.pfx.family || filter.label) { |
| 662 | struct rtattr *tb[IFA_MAX+1]; |
| 663 | memset(tb, 0, sizeof(tb)); |
| 664 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); |
| 665 | if (!tb[IFA_LOCAL]) |
| 666 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; |
| 667 | |
| 668 | if (filter.pfx.family && tb[IFA_LOCAL]) { |
| 669 | inet_prefix dst; |
| 670 | memset(&dst, 0, sizeof(dst)); |
| 671 | dst.family = ifa->ifa_family; |
| 672 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); |
| 673 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 674 | continue; |
| 675 | } |
| 676 | if (filter.label) { |
| 677 | SPRINT_BUF(b1); |
| 678 | const char *label; |
| 679 | if (tb[IFA_LABEL]) |
| 680 | label = RTA_DATA(tb[IFA_LABEL]); |
| 681 | else |
| 682 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 683 | if (fnmatch(filter.label, label, 0) != 0) |
| 684 | continue; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | ok = 1; |
| 689 | break; |
| 690 | } |
| 691 | if (!ok) |
| 692 | *lp = l->next; |
| 693 | else |
| 694 | lp = &l->next; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | for (l=linfo; l; l = l->next) { |
| 699 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { |
| 700 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 701 | if (filter.family != AF_PACKET) |
| 702 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); |
| 703 | } |
| 704 | fflush(stdout); |
| 705 | } |
| 706 | |
| 707 | exit(0); |
| 708 | } |
| 709 | |
| 710 | int ipaddr_list_link(int argc, char **argv) |
| 711 | { |
| 712 | preferred_family = AF_PACKET; |
| 713 | do_link = 1; |
| 714 | return ipaddr_list_or_flush(argc, argv, 0); |
| 715 | } |
| 716 | |
| 717 | void ipaddr_reset_filter(int oneline) |
| 718 | { |
| 719 | memset(&filter, 0, sizeof(filter)); |
| 720 | filter.oneline = oneline; |
| 721 | } |
| 722 | |
| 723 | int default_scope(inet_prefix *lcl) |
| 724 | { |
| 725 | if (lcl->family == AF_INET) { |
| 726 | if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127) |
| 727 | return RT_SCOPE_HOST; |
| 728 | } |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | int ipaddr_modify(int cmd, int argc, char **argv) |
| 733 | { |
| 734 | struct rtnl_handle rth; |
| 735 | struct { |
| 736 | struct nlmsghdr n; |
| 737 | struct ifaddrmsg ifa; |
| 738 | char buf[256]; |
| 739 | } req; |
| 740 | char *d = NULL; |
| 741 | char *l = NULL; |
| 742 | inet_prefix lcl; |
| 743 | inet_prefix peer; |
| 744 | int local_len = 0; |
| 745 | int peer_len = 0; |
| 746 | int brd_len = 0; |
| 747 | int any_len = 0; |
| 748 | int scoped = 0; |
| 749 | |
| 750 | memset(&req, 0, sizeof(req)); |
| 751 | |
| 752 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 753 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 754 | req.n.nlmsg_type = cmd; |
| 755 | req.ifa.ifa_family = preferred_family; |
| 756 | |
| 757 | while (argc > 0) { |
| 758 | if (strcmp(*argv, "peer") == 0 || |
| 759 | strcmp(*argv, "remote") == 0) { |
| 760 | NEXT_ARG(); |
| 761 | |
| 762 | if (peer_len) |
| 763 | duparg("peer", *argv); |
| 764 | get_prefix(&peer, *argv, req.ifa.ifa_family); |
| 765 | peer_len = peer.bytelen; |
| 766 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 767 | req.ifa.ifa_family = peer.family; |
| 768 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); |
| 769 | req.ifa.ifa_prefixlen = peer.bitlen; |
| 770 | } else if (matches(*argv, "broadcast") == 0 || |
| 771 | strcmp(*argv, "brd") == 0) { |
| 772 | inet_prefix addr; |
| 773 | NEXT_ARG(); |
| 774 | if (brd_len) |
| 775 | duparg("broadcast", *argv); |
| 776 | if (strcmp(*argv, "+") == 0) |
| 777 | brd_len = -1; |
| 778 | else if (strcmp(*argv, "-") == 0) |
| 779 | brd_len = -2; |
| 780 | else { |
| 781 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 782 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 783 | req.ifa.ifa_family = addr.family; |
| 784 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); |
| 785 | brd_len = addr.bytelen; |
| 786 | } |
| 787 | } else if (strcmp(*argv, "anycast") == 0) { |
| 788 | inet_prefix addr; |
| 789 | NEXT_ARG(); |
| 790 | if (any_len) |
| 791 | duparg("anycast", *argv); |
| 792 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 793 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 794 | req.ifa.ifa_family = addr.family; |
| 795 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); |
| 796 | any_len = addr.bytelen; |
| 797 | } else if (strcmp(*argv, "scope") == 0) { |
| 798 | int scope = 0; |
| 799 | NEXT_ARG(); |
| 800 | if (rtnl_rtscope_a2n(&scope, *argv)) |
| 801 | invarg(*argv, "invalid scope value."); |
| 802 | req.ifa.ifa_scope = scope; |
| 803 | scoped = 1; |
| 804 | } else if (strcmp(*argv, "dev") == 0) { |
| 805 | NEXT_ARG(); |
| 806 | d = *argv; |
| 807 | } else if (strcmp(*argv, "label") == 0) { |
| 808 | NEXT_ARG(); |
| 809 | l = *argv; |
| 810 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); |
| 811 | } else { |
| 812 | if (strcmp(*argv, "local") == 0) { |
| 813 | NEXT_ARG(); |
| 814 | } |
| 815 | if (matches(*argv, "help") == 0) |
| 816 | usage(); |
| 817 | if (local_len) |
| 818 | duparg2("local", *argv); |
| 819 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 820 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 821 | req.ifa.ifa_family = lcl.family; |
| 822 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 823 | local_len = lcl.bytelen; |
| 824 | } |
| 825 | argc--; argv++; |
| 826 | } |
| 827 | if (d == NULL) { |
| 828 | fprintf(stderr, "Not enough information: \"dev\" argument is required.\n"); |
| 829 | return -1; |
| 830 | } |
| 831 | if (l && matches(d, l) != 0) { |
| 832 | fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l); |
| 833 | exit(1); |
| 834 | } |
| 835 | |
| 836 | if (peer_len == 0 && local_len && cmd != RTM_DELADDR) { |
| 837 | peer = lcl; |
| 838 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 839 | } |
| 840 | if (req.ifa.ifa_prefixlen == 0) |
| 841 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 842 | |
| 843 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 844 | inet_prefix brd; |
| 845 | int i; |
| 846 | if (req.ifa.ifa_family != AF_INET) { |
| 847 | fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n"); |
| 848 | return -1; |
| 849 | } |
| 850 | brd = peer; |
| 851 | if (brd.bitlen <= 30) { |
| 852 | for (i=31; i>=brd.bitlen; i--) { |
| 853 | if (brd_len == -1) |
| 854 | brd.data[0] |= htonl(1<<(31-i)); |
| 855 | else |
| 856 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 857 | } |
| 858 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 859 | brd_len = brd.bytelen; |
| 860 | } |
| 861 | } |
| 862 | if (!scoped && cmd != RTM_DELADDR) |
| 863 | req.ifa.ifa_scope = default_scope(&lcl); |
| 864 | |
| 865 | if (rtnl_open(&rth, 0) < 0) |
| 866 | exit(1); |
| 867 | |
| 868 | ll_init_map(&rth); |
| 869 | |
| 870 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { |
| 871 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
| 872 | return -1; |
| 873 | } |
| 874 | |
| 875 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 876 | exit(2); |
| 877 | |
| 878 | exit(0); |
| 879 | } |
| 880 | |
| 881 | int do_ipaddr(int argc, char **argv) |
| 882 | { |
| 883 | if (argc < 1) |
| 884 | return ipaddr_list_or_flush(0, NULL, 0); |
| 885 | if (matches(*argv, "add") == 0) |
| 886 | return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1); |
| 887 | if (matches(*argv, "delete") == 0) |
| 888 | return ipaddr_modify(RTM_DELADDR, argc-1, argv+1); |
| 889 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 890 | || matches(*argv, "lst") == 0) |
| 891 | return ipaddr_list_or_flush(argc-1, argv+1, 0); |
| 892 | if (matches(*argv, "flush") == 0) |
| 893 | return ipaddr_list_or_flush(argc-1, argv+1, 1); |
| 894 | if (matches(*argv, "help") == 0) |
| 895 | usage(); |
| 896 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv); |
| 897 | exit(-1); |
| 898 | } |
| 899 | |