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; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 52 | } filter; |
| 53 | |
| 54 | static int do_link; |
| 55 | |
| 56 | static void usage(void) __attribute__((noreturn)); |
| 57 | |
| 58 | static void usage(void) |
| 59 | { |
| 60 | if (do_link) { |
| 61 | iplink_usage(); |
| 62 | } |
| 63 | fprintf(stderr, "Usage: ip addr {add|del} IFADDR dev STRING\n"); |
| 64 | fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n"); |
| 65 | fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n"); |
| 66 | fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n"); |
| 67 | fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n"); |
| 68 | fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n"); |
| 69 | fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n"); |
| 70 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
| 71 | fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n"); |
| 72 | fprintf(stderr, " tentative | deprecated ]\n"); |
| 73 | exit(-1); |
| 74 | } |
| 75 | |
| 76 | void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) |
| 77 | { |
| 78 | fprintf(fp, "<"); |
net[shemminger]!shemminger | 73b49e9 | 2005-03-14 18:47:38 +0000 | [diff] [blame] | 79 | if (flags & IFF_UP && !(flags & IFF_RUNNING)) |
| 80 | fprintf(fp, "NO-CARRIER%s", flags ? "," : ""); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 81 | flags &= ~IFF_RUNNING; |
| 82 | #define _PF(f) if (flags&IFF_##f) { \ |
| 83 | flags &= ~IFF_##f ; \ |
| 84 | fprintf(fp, #f "%s", flags ? "," : ""); } |
| 85 | _PF(LOOPBACK); |
| 86 | _PF(BROADCAST); |
| 87 | _PF(POINTOPOINT); |
| 88 | _PF(MULTICAST); |
| 89 | _PF(NOARP); |
| 90 | _PF(ALLMULTI); |
| 91 | _PF(PROMISC); |
| 92 | _PF(MASTER); |
| 93 | _PF(SLAVE); |
| 94 | _PF(DEBUG); |
| 95 | _PF(DYNAMIC); |
| 96 | _PF(AUTOMEDIA); |
| 97 | _PF(PORTSEL); |
| 98 | _PF(NOTRAILERS); |
| 99 | _PF(UP); |
| 100 | #undef _PF |
| 101 | if (flags) |
| 102 | fprintf(fp, "%x", flags); |
| 103 | if (mdown) |
| 104 | fprintf(fp, ",M-DOWN"); |
| 105 | fprintf(fp, "> "); |
| 106 | } |
| 107 | |
| 108 | void print_queuelen(char *name) |
| 109 | { |
| 110 | struct ifreq ifr; |
| 111 | int s; |
| 112 | |
| 113 | s = socket(AF_INET, SOCK_STREAM, 0); |
| 114 | if (s < 0) |
| 115 | return; |
| 116 | |
| 117 | memset(&ifr, 0, sizeof(ifr)); |
| 118 | strcpy(ifr.ifr_name, name); |
| 119 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
| 120 | perror("SIOCGIFXQLEN"); |
| 121 | close(s); |
| 122 | return; |
| 123 | } |
| 124 | close(s); |
| 125 | |
| 126 | if (ifr.ifr_qlen) |
| 127 | printf("qlen %d", ifr.ifr_qlen); |
| 128 | } |
| 129 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 130 | int print_linkinfo(const struct sockaddr_nl *who, |
| 131 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | { |
| 133 | FILE *fp = (FILE*)arg; |
| 134 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 135 | struct rtattr * tb[IFLA_MAX+1]; |
| 136 | int len = n->nlmsg_len; |
| 137 | unsigned m_flag = 0; |
| 138 | |
| 139 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) |
| 140 | return 0; |
| 141 | |
| 142 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 143 | if (len < 0) |
| 144 | return -1; |
| 145 | |
| 146 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) |
| 147 | return 0; |
| 148 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 149 | return 0; |
| 150 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 151 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 152 | if (tb[IFLA_IFNAME] == NULL) { |
| 153 | fprintf(stderr, "BUG: nil ifname\n"); |
| 154 | return -1; |
| 155 | } |
| 156 | if (filter.label && |
| 157 | (!filter.family || filter.family == AF_PACKET) && |
| 158 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) |
| 159 | return 0; |
| 160 | |
| 161 | if (n->nlmsg_type == RTM_DELLINK) |
| 162 | fprintf(fp, "Deleted "); |
| 163 | |
| 164 | fprintf(fp, "%d: %s", ifi->ifi_index, |
| 165 | tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>"); |
| 166 | |
| 167 | if (tb[IFLA_LINK]) { |
| 168 | SPRINT_BUF(b1); |
| 169 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 170 | if (iflink == 0) |
| 171 | fprintf(fp, "@NONE: "); |
| 172 | else { |
| 173 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); |
| 174 | m_flag = ll_index_to_flags(iflink); |
| 175 | m_flag = !(m_flag & IFF_UP); |
| 176 | } |
| 177 | } else { |
| 178 | fprintf(fp, ": "); |
| 179 | } |
| 180 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
| 181 | |
| 182 | if (tb[IFLA_MTU]) |
| 183 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
| 184 | if (tb[IFLA_QDISC]) |
| 185 | fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); |
| 186 | #ifdef IFLA_MASTER |
| 187 | if (tb[IFLA_MASTER]) { |
| 188 | SPRINT_BUF(b1); |
| 189 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
| 190 | } |
| 191 | #endif |
| 192 | if (filter.showqueue) |
| 193 | print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME])); |
| 194 | |
| 195 | if (!filter.family || filter.family == AF_PACKET) { |
| 196 | SPRINT_BUF(b1); |
| 197 | fprintf(fp, "%s", _SL_); |
| 198 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); |
| 199 | |
| 200 | if (tb[IFLA_ADDRESS]) { |
| 201 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
| 202 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 203 | ifi->ifi_type, |
| 204 | b1, sizeof(b1))); |
| 205 | } |
| 206 | if (tb[IFLA_BROADCAST]) { |
| 207 | if (ifi->ifi_flags&IFF_POINTOPOINT) |
| 208 | fprintf(fp, " peer "); |
| 209 | else |
| 210 | fprintf(fp, " brd "); |
| 211 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
| 212 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 213 | ifi->ifi_type, |
| 214 | b1, sizeof(b1))); |
| 215 | } |
| 216 | } |
| 217 | if (do_link && tb[IFLA_STATS] && show_stats) { |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 218 | struct rtnl_link_stats slocal; |
| 219 | struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 220 | if (((unsigned long)s) & (sizeof(unsigned long)-1)) { |
| 221 | memcpy(&slocal, s, sizeof(slocal)); |
| 222 | s = &slocal; |
| 223 | } |
| 224 | fprintf(fp, "%s", _SL_); |
| 225 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", |
| 226 | s->rx_compressed ? "compressed" : "", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 227 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 228 | s->rx_bytes, s->rx_packets, s->rx_errors, |
| 229 | s->rx_dropped, s->rx_over_errors, |
| 230 | s->multicast |
| 231 | ); |
| 232 | if (s->rx_compressed) |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 233 | fprintf(fp, " %-7u", s->rx_compressed); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 234 | if (show_stats > 1) { |
| 235 | fprintf(fp, "%s", _SL_); |
| 236 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 237 | fprintf(fp, " %-7u %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 238 | s->rx_length_errors, |
| 239 | s->rx_crc_errors, |
| 240 | s->rx_frame_errors, |
| 241 | s->rx_fifo_errors, |
| 242 | s->rx_missed_errors |
| 243 | ); |
| 244 | } |
| 245 | fprintf(fp, "%s", _SL_); |
| 246 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", |
| 247 | s->tx_compressed ? "compressed" : "", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 248 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 249 | s->tx_bytes, s->tx_packets, s->tx_errors, |
| 250 | s->tx_dropped, s->tx_carrier_errors, s->collisions); |
| 251 | if (s->tx_compressed) |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 252 | fprintf(fp, " %-7u", s->tx_compressed); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 253 | if (show_stats > 1) { |
| 254 | fprintf(fp, "%s", _SL_); |
| 255 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 256 | fprintf(fp, " %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 257 | s->tx_aborted_errors, |
| 258 | s->tx_fifo_errors, |
| 259 | s->tx_window_errors, |
| 260 | s->tx_heartbeat_errors |
| 261 | ); |
| 262 | } |
| 263 | } |
| 264 | fprintf(fp, "\n"); |
| 265 | fflush(fp); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int flush_update(void) |
| 270 | { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame^] | 271 | if (rtnl_send(&rth, filter.flushb, filter.flushp) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 272 | perror("Failed to send flush request\n"); |
| 273 | return -1; |
| 274 | } |
| 275 | filter.flushp = 0; |
| 276 | return 0; |
| 277 | } |
| 278 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 279 | int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 280 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 281 | { |
| 282 | FILE *fp = (FILE*)arg; |
| 283 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 284 | int len = n->nlmsg_len; |
| 285 | struct rtattr * rta_tb[IFA_MAX+1]; |
| 286 | char abuf[256]; |
| 287 | SPRINT_BUF(b1); |
| 288 | |
| 289 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) |
| 290 | return 0; |
| 291 | len -= NLMSG_LENGTH(sizeof(*ifa)); |
| 292 | if (len < 0) { |
| 293 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) |
| 298 | return 0; |
| 299 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 300 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
| 301 | |
| 302 | if (!rta_tb[IFA_LOCAL]) |
| 303 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; |
| 304 | if (!rta_tb[IFA_ADDRESS]) |
| 305 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; |
| 306 | |
| 307 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) |
| 308 | return 0; |
| 309 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 310 | return 0; |
| 311 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 312 | return 0; |
| 313 | if (filter.label) { |
| 314 | SPRINT_BUF(b1); |
| 315 | const char *label; |
| 316 | if (rta_tb[IFA_LABEL]) |
| 317 | label = RTA_DATA(rta_tb[IFA_LABEL]); |
| 318 | else |
| 319 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 320 | if (fnmatch(filter.label, label, 0) != 0) |
| 321 | return 0; |
| 322 | } |
| 323 | if (filter.pfx.family) { |
| 324 | if (rta_tb[IFA_LOCAL]) { |
| 325 | inet_prefix dst; |
| 326 | memset(&dst, 0, sizeof(dst)); |
| 327 | dst.family = ifa->ifa_family; |
| 328 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); |
| 329 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 330 | return 0; |
| 331 | } |
| 332 | } |
| 333 | |
net[shemminger]!shemminger | 3eb1731 | 2005-02-07 18:28:31 +0000 | [diff] [blame] | 334 | if (filter.family && filter.family != ifa->ifa_family) |
| 335 | return 0; |
| 336 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 337 | if (filter.flushb) { |
| 338 | struct nlmsghdr *fn; |
| 339 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
| 340 | if (flush_update()) |
| 341 | return -1; |
| 342 | } |
| 343 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
| 344 | memcpy(fn, n, n->nlmsg_len); |
| 345 | fn->nlmsg_type = RTM_DELADDR; |
| 346 | fn->nlmsg_flags = NLM_F_REQUEST; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame^] | 347 | fn->nlmsg_seq = ++rth.seq; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 348 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
| 349 | filter.flushed++; |
| 350 | if (show_stats < 2) |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | if (n->nlmsg_type == RTM_DELADDR) |
| 355 | fprintf(fp, "Deleted "); |
| 356 | |
| 357 | if (filter.oneline || filter.flushb) |
| 358 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
| 359 | if (ifa->ifa_family == AF_INET) |
| 360 | fprintf(fp, " inet "); |
| 361 | else if (ifa->ifa_family == AF_INET6) |
| 362 | fprintf(fp, " inet6 "); |
| 363 | else if (ifa->ifa_family == AF_DECnet) |
| 364 | fprintf(fp, " dnet "); |
| 365 | else if (ifa->ifa_family == AF_IPX) |
| 366 | fprintf(fp, " ipx "); |
| 367 | else |
| 368 | fprintf(fp, " family %d ", ifa->ifa_family); |
| 369 | |
| 370 | if (rta_tb[IFA_LOCAL]) { |
| 371 | fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family, |
| 372 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), |
| 373 | RTA_DATA(rta_tb[IFA_LOCAL]), |
| 374 | abuf, sizeof(abuf))); |
| 375 | |
| 376 | if (rta_tb[IFA_ADDRESS] == NULL || |
| 377 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { |
| 378 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); |
| 379 | } else { |
| 380 | fprintf(fp, " peer %s/%d ", |
| 381 | rt_addr_n2a(ifa->ifa_family, |
| 382 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), |
| 383 | RTA_DATA(rta_tb[IFA_ADDRESS]), |
| 384 | abuf, sizeof(abuf)), |
| 385 | ifa->ifa_prefixlen); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (rta_tb[IFA_BROADCAST]) { |
| 390 | fprintf(fp, "brd %s ", |
| 391 | rt_addr_n2a(ifa->ifa_family, |
| 392 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), |
| 393 | RTA_DATA(rta_tb[IFA_BROADCAST]), |
| 394 | abuf, sizeof(abuf))); |
| 395 | } |
| 396 | if (rta_tb[IFA_ANYCAST]) { |
| 397 | fprintf(fp, "any %s ", |
| 398 | rt_addr_n2a(ifa->ifa_family, |
| 399 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), |
| 400 | RTA_DATA(rta_tb[IFA_ANYCAST]), |
| 401 | abuf, sizeof(abuf))); |
| 402 | } |
| 403 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); |
| 404 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
| 405 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
| 406 | fprintf(fp, "secondary "); |
| 407 | } |
| 408 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { |
| 409 | ifa->ifa_flags &= ~IFA_F_TENTATIVE; |
| 410 | fprintf(fp, "tentative "); |
| 411 | } |
| 412 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { |
| 413 | ifa->ifa_flags &= ~IFA_F_DEPRECATED; |
| 414 | fprintf(fp, "deprecated "); |
| 415 | } |
| 416 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { |
| 417 | fprintf(fp, "dynamic "); |
| 418 | } else |
| 419 | ifa->ifa_flags &= ~IFA_F_PERMANENT; |
| 420 | if (ifa->ifa_flags) |
| 421 | fprintf(fp, "flags %02x ", ifa->ifa_flags); |
| 422 | if (rta_tb[IFA_LABEL]) |
| 423 | fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL])); |
| 424 | if (rta_tb[IFA_CACHEINFO]) { |
| 425 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
| 426 | char buf[128]; |
| 427 | fprintf(fp, "%s", _SL_); |
| 428 | if (ci->ifa_valid == 0xFFFFFFFFU) |
| 429 | sprintf(buf, "valid_lft forever"); |
| 430 | else |
| 431 | sprintf(buf, "valid_lft %dsec", ci->ifa_valid); |
| 432 | if (ci->ifa_prefered == 0xFFFFFFFFU) |
| 433 | sprintf(buf+strlen(buf), " preferred_lft forever"); |
| 434 | else |
| 435 | sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); |
| 436 | fprintf(fp, " %s", buf); |
| 437 | } |
| 438 | fprintf(fp, "\n"); |
| 439 | fflush(fp); |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | struct nlmsg_list |
| 445 | { |
| 446 | struct nlmsg_list *next; |
| 447 | struct nlmsghdr h; |
| 448 | }; |
| 449 | |
| 450 | int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) |
| 451 | { |
| 452 | for ( ;ainfo ; ainfo = ainfo->next) { |
| 453 | struct nlmsghdr *n = &ainfo->h; |
| 454 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 455 | |
| 456 | if (n->nlmsg_type != RTM_NEWADDR) |
| 457 | continue; |
| 458 | |
| 459 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) |
| 460 | return -1; |
| 461 | |
| 462 | if (ifa->ifa_index != ifindex || |
| 463 | (filter.family && filter.family != ifa->ifa_family)) |
| 464 | continue; |
| 465 | |
| 466 | print_addrinfo(NULL, n, fp); |
| 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 472 | static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 473 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 474 | { |
| 475 | struct nlmsg_list **linfo = (struct nlmsg_list**)arg; |
| 476 | struct nlmsg_list *h; |
| 477 | struct nlmsg_list **lp; |
| 478 | |
| 479 | h = malloc(n->nlmsg_len+sizeof(void*)); |
| 480 | if (h == NULL) |
| 481 | return -1; |
| 482 | |
| 483 | memcpy(&h->h, n, n->nlmsg_len); |
| 484 | h->next = NULL; |
| 485 | |
| 486 | for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */; |
| 487 | *lp = h; |
| 488 | |
| 489 | ll_remember_index(who, n, NULL); |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | int ipaddr_list_or_flush(int argc, char **argv, int flush) |
| 494 | { |
| 495 | struct nlmsg_list *linfo = NULL; |
| 496 | struct nlmsg_list *ainfo = NULL; |
| 497 | struct nlmsg_list *l; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 498 | char *filter_dev = NULL; |
| 499 | int no_link = 0; |
| 500 | |
| 501 | ipaddr_reset_filter(oneline); |
| 502 | filter.showqueue = 1; |
| 503 | |
| 504 | if (filter.family == AF_UNSPEC) |
| 505 | filter.family = preferred_family; |
| 506 | |
| 507 | if (flush) { |
| 508 | if (argc <= 0) { |
| 509 | fprintf(stderr, "Flush requires arguments.\n"); |
| 510 | return -1; |
| 511 | } |
| 512 | if (filter.family == AF_PACKET) { |
| 513 | fprintf(stderr, "Cannot flush link addresses.\n"); |
| 514 | return -1; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | while (argc > 0) { |
| 519 | if (strcmp(*argv, "to") == 0) { |
| 520 | NEXT_ARG(); |
| 521 | get_prefix(&filter.pfx, *argv, filter.family); |
| 522 | if (filter.family == AF_UNSPEC) |
| 523 | filter.family = filter.pfx.family; |
| 524 | } else if (strcmp(*argv, "scope") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 525 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 526 | NEXT_ARG(); |
| 527 | filter.scopemask = -1; |
| 528 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 529 | if (strcmp(*argv, "all") != 0) |
| 530 | invarg("invalid \"scope\"\n", *argv); |
| 531 | scope = RT_SCOPE_NOWHERE; |
| 532 | filter.scopemask = 0; |
| 533 | } |
| 534 | filter.scope = scope; |
| 535 | } else if (strcmp(*argv, "up") == 0) { |
| 536 | filter.up = 1; |
| 537 | } else if (strcmp(*argv, "dynamic") == 0) { |
| 538 | filter.flags &= ~IFA_F_PERMANENT; |
| 539 | filter.flagmask |= IFA_F_PERMANENT; |
| 540 | } else if (strcmp(*argv, "permanent") == 0) { |
| 541 | filter.flags |= IFA_F_PERMANENT; |
| 542 | filter.flagmask |= IFA_F_PERMANENT; |
| 543 | } else if (strcmp(*argv, "secondary") == 0) { |
| 544 | filter.flags |= IFA_F_SECONDARY; |
| 545 | filter.flagmask |= IFA_F_SECONDARY; |
| 546 | } else if (strcmp(*argv, "primary") == 0) { |
| 547 | filter.flags &= ~IFA_F_SECONDARY; |
| 548 | filter.flagmask |= IFA_F_SECONDARY; |
| 549 | } else if (strcmp(*argv, "tentative") == 0) { |
| 550 | filter.flags |= IFA_F_TENTATIVE; |
| 551 | filter.flagmask |= IFA_F_TENTATIVE; |
| 552 | } else if (strcmp(*argv, "deprecated") == 0) { |
| 553 | filter.flags |= IFA_F_DEPRECATED; |
| 554 | filter.flagmask |= IFA_F_DEPRECATED; |
| 555 | } else if (strcmp(*argv, "label") == 0) { |
| 556 | NEXT_ARG(); |
| 557 | filter.label = *argv; |
| 558 | } else { |
| 559 | if (strcmp(*argv, "dev") == 0) { |
| 560 | NEXT_ARG(); |
| 561 | } |
| 562 | if (matches(*argv, "help") == 0) |
| 563 | usage(); |
| 564 | if (filter_dev) |
| 565 | duparg2("dev", *argv); |
| 566 | filter_dev = *argv; |
| 567 | } |
| 568 | argv++; argc--; |
| 569 | } |
| 570 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 571 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { |
| 572 | perror("Cannot send dump request"); |
| 573 | exit(1); |
| 574 | } |
| 575 | |
| 576 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) { |
| 577 | fprintf(stderr, "Dump terminated\n"); |
| 578 | exit(1); |
| 579 | } |
| 580 | |
| 581 | if (filter_dev) { |
| 582 | filter.ifindex = ll_name_to_index(filter_dev); |
| 583 | if (filter.ifindex <= 0) { |
| 584 | fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev); |
| 585 | return -1; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (flush) { |
| 590 | int round = 0; |
| 591 | char flushb[4096-512]; |
| 592 | |
| 593 | filter.flushb = flushb; |
| 594 | filter.flushp = 0; |
| 595 | filter.flushe = sizeof(flushb); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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) |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame^] | 617 | return 1; |
| 618 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 619 | if (show_stats) { |
| 620 | printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed); |
| 621 | fflush(stdout); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (filter.family != AF_PACKET) { |
| 627 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 628 | perror("Cannot send dump request"); |
| 629 | exit(1); |
| 630 | } |
| 631 | |
| 632 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) { |
| 633 | fprintf(stderr, "Dump terminated\n"); |
| 634 | exit(1); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | |
| 639 | if (filter.family && filter.family != AF_PACKET) { |
| 640 | struct nlmsg_list **lp; |
| 641 | lp=&linfo; |
| 642 | |
| 643 | if (filter.oneline) |
| 644 | no_link = 1; |
| 645 | |
| 646 | while ((l=*lp)!=NULL) { |
| 647 | int ok = 0; |
| 648 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 649 | struct nlmsg_list *a; |
| 650 | |
| 651 | for (a=ainfo; a; a=a->next) { |
| 652 | struct nlmsghdr *n = &a->h; |
| 653 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 654 | |
| 655 | if (ifa->ifa_index != ifi->ifi_index || |
| 656 | (filter.family && filter.family != ifa->ifa_family)) |
| 657 | continue; |
| 658 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 659 | continue; |
| 660 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 661 | continue; |
| 662 | if (filter.pfx.family || filter.label) { |
| 663 | struct rtattr *tb[IFA_MAX+1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame^] | 707 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 734 | struct { |
| 735 | struct nlmsghdr n; |
| 736 | struct ifaddrmsg ifa; |
| 737 | char buf[256]; |
| 738 | } req; |
| 739 | char *d = NULL; |
| 740 | char *l = NULL; |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 741 | char *lcl_arg = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 798 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 819 | lcl_arg = *argv; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 820 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 821 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 822 | req.ifa.ifa_family = lcl.family; |
| 823 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 824 | local_len = lcl.bytelen; |
| 825 | } |
| 826 | argc--; argv++; |
| 827 | } |
| 828 | if (d == NULL) { |
| 829 | fprintf(stderr, "Not enough information: \"dev\" argument is required.\n"); |
| 830 | return -1; |
| 831 | } |
| 832 | if (l && matches(d, l) != 0) { |
| 833 | fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l); |
| 834 | exit(1); |
| 835 | } |
| 836 | |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 837 | if (peer_len == 0 && local_len) { |
| 838 | if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) { |
| 839 | fprintf(stderr, |
| 840 | "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \ |
| 841 | " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \ |
| 842 | " This special behaviour is likely to disappear in further releases,\n" \ |
| 843 | " fix your scripts!\n", lcl_arg, local_len*8); |
| 844 | } else { |
| 845 | peer = lcl; |
| 846 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 847 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 848 | } |
| 849 | if (req.ifa.ifa_prefixlen == 0) |
| 850 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 851 | |
| 852 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 853 | inet_prefix brd; |
| 854 | int i; |
| 855 | if (req.ifa.ifa_family != AF_INET) { |
| 856 | fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n"); |
| 857 | return -1; |
| 858 | } |
| 859 | brd = peer; |
| 860 | if (brd.bitlen <= 30) { |
| 861 | for (i=31; i>=brd.bitlen; i--) { |
| 862 | if (brd_len == -1) |
| 863 | brd.data[0] |= htonl(1<<(31-i)); |
| 864 | else |
| 865 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 866 | } |
| 867 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 868 | brd_len = brd.bytelen; |
| 869 | } |
| 870 | } |
| 871 | if (!scoped && cmd != RTM_DELADDR) |
| 872 | req.ifa.ifa_scope = default_scope(&lcl); |
| 873 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 874 | ll_init_map(&rth); |
| 875 | |
| 876 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { |
| 877 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
| 878 | return -1; |
| 879 | } |
| 880 | |
| 881 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 882 | exit(2); |
| 883 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame^] | 884 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | int do_ipaddr(int argc, char **argv) |
| 888 | { |
| 889 | if (argc < 1) |
| 890 | return ipaddr_list_or_flush(0, NULL, 0); |
| 891 | if (matches(*argv, "add") == 0) |
| 892 | return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1); |
| 893 | if (matches(*argv, "delete") == 0) |
| 894 | return ipaddr_modify(RTM_DELADDR, argc-1, argv+1); |
| 895 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 896 | || matches(*argv, "lst") == 0) |
| 897 | return ipaddr_list_or_flush(argc-1, argv+1, 0); |
| 898 | if (matches(*argv, "flush") == 0) |
| 899 | return ipaddr_list_or_flush(argc-1, argv+1, 1); |
| 900 | if (matches(*argv, "help") == 0) |
| 901 | usage(); |
| 902 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv); |
| 903 | exit(-1); |
| 904 | } |
| 905 | |