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> |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 23 | #include <sys/errno.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 24 | #include <netinet/in.h> |
| 25 | #include <arpa/inet.h> |
| 26 | #include <string.h> |
| 27 | #include <fnmatch.h> |
| 28 | |
osdl.org!shemminger | e5779fb | 2004-06-09 22:56:28 +0000 | [diff] [blame] | 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/if_arp.h> |
| 31 | #include <linux/sockios.h> |
| 32 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 33 | #include "rt_names.h" |
| 34 | #include "utils.h" |
| 35 | #include "ll_map.h" |
| 36 | #include "ip_common.h" |
| 37 | |
Daniel Silverstone | 7b3d366 | 2007-10-19 13:32:24 +0200 | [diff] [blame^] | 38 | #define MAX_ROUNDS 10 |
| 39 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 40 | static struct |
| 41 | { |
| 42 | int ifindex; |
| 43 | int family; |
| 44 | int oneline; |
| 45 | int showqueue; |
| 46 | inet_prefix pfx; |
| 47 | int scope, scopemask; |
| 48 | int flags, flagmask; |
| 49 | int up; |
| 50 | char *label; |
| 51 | int flushed; |
| 52 | char *flushb; |
| 53 | int flushp; |
| 54 | int flushe; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 55 | } filter; |
| 56 | |
| 57 | static int do_link; |
| 58 | |
| 59 | static void usage(void) __attribute__((noreturn)); |
| 60 | |
| 61 | static void usage(void) |
| 62 | { |
| 63 | if (do_link) { |
| 64 | iplink_usage(); |
| 65 | } |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 66 | fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n"); |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 67 | fprintf(stderr, " [ CONFFLAG-LIST]\n"); |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 68 | fprintf(stderr, " ip addr del IFADDR dev STRING\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 69 | fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n"); |
| 70 | fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n"); |
| 71 | fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n"); |
| 72 | fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n"); |
| 73 | fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n"); |
| 74 | fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n"); |
| 75 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
| 76 | fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n"); |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 77 | fprintf(stderr, " tentative | deprecated | CONFFLAG-LIST ]\n"); |
| 78 | fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n"); |
| 79 | fprintf(stderr, "CONFFLAG := [ home | nodad ]\n"); |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 80 | fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n"); |
| 81 | fprintf(stderr, "LFT := forever | SECONDS\n"); |
| 82 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 83 | exit(-1); |
| 84 | } |
| 85 | |
| 86 | void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) |
| 87 | { |
| 88 | fprintf(fp, "<"); |
net[shemminger]!shemminger | 73b49e9 | 2005-03-14 18:47:38 +0000 | [diff] [blame] | 89 | if (flags & IFF_UP && !(flags & IFF_RUNNING)) |
| 90 | fprintf(fp, "NO-CARRIER%s", flags ? "," : ""); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 91 | flags &= ~IFF_RUNNING; |
| 92 | #define _PF(f) if (flags&IFF_##f) { \ |
| 93 | flags &= ~IFF_##f ; \ |
| 94 | fprintf(fp, #f "%s", flags ? "," : ""); } |
| 95 | _PF(LOOPBACK); |
| 96 | _PF(BROADCAST); |
| 97 | _PF(POINTOPOINT); |
| 98 | _PF(MULTICAST); |
| 99 | _PF(NOARP); |
| 100 | _PF(ALLMULTI); |
| 101 | _PF(PROMISC); |
| 102 | _PF(MASTER); |
| 103 | _PF(SLAVE); |
| 104 | _PF(DEBUG); |
| 105 | _PF(DYNAMIC); |
| 106 | _PF(AUTOMEDIA); |
| 107 | _PF(PORTSEL); |
| 108 | _PF(NOTRAILERS); |
| 109 | _PF(UP); |
Thomas Graf | dcb283c | 2007-06-19 16:40:40 -0700 | [diff] [blame] | 110 | _PF(LOWER_UP); |
| 111 | _PF(DORMANT); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 112 | #undef _PF |
| 113 | if (flags) |
| 114 | fprintf(fp, "%x", flags); |
| 115 | if (mdown) |
| 116 | fprintf(fp, ",M-DOWN"); |
| 117 | fprintf(fp, "> "); |
| 118 | } |
| 119 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 120 | static const char *oper_states[] = { |
| 121 | "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN", |
| 122 | "TESTING", "DORMANT", "UP" |
| 123 | }; |
| 124 | |
| 125 | static void print_operstate(FILE *f, __u8 state) |
| 126 | { |
| 127 | if (state >= sizeof(oper_states)/sizeof(oper_states[0])) |
| 128 | fprintf(f, "state %#x ", state); |
| 129 | else |
| 130 | fprintf(f, "state %s ", oper_states[state]); |
| 131 | } |
| 132 | |
| 133 | static void print_queuelen(FILE *f, const char *name) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 134 | { |
| 135 | struct ifreq ifr; |
| 136 | int s; |
| 137 | |
| 138 | s = socket(AF_INET, SOCK_STREAM, 0); |
| 139 | if (s < 0) |
| 140 | return; |
| 141 | |
| 142 | memset(&ifr, 0, sizeof(ifr)); |
| 143 | strcpy(ifr.ifr_name, name); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 144 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 145 | fprintf(f, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 146 | close(s); |
| 147 | return; |
| 148 | } |
| 149 | close(s); |
| 150 | |
| 151 | if (ifr.ifr_qlen) |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 152 | fprintf(f, "qlen %d", ifr.ifr_qlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 155 | static void print_linktype(FILE *fp, struct rtattr *tb) |
| 156 | { |
| 157 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 158 | struct link_util *lu; |
| 159 | char *kind; |
| 160 | |
| 161 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); |
| 162 | |
| 163 | if (!linkinfo[IFLA_INFO_KIND]) |
| 164 | return; |
| 165 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); |
| 166 | |
| 167 | fprintf(fp, "%s", _SL_); |
| 168 | fprintf(fp, " %s ", kind); |
| 169 | |
| 170 | lu = get_link_kind(kind); |
| 171 | if (!lu || !lu->print_opt) |
| 172 | return; |
| 173 | |
| 174 | if (1) { |
| 175 | struct rtattr *attr[lu->maxattr+1], **data = NULL; |
| 176 | |
| 177 | if (linkinfo[IFLA_INFO_DATA]) { |
| 178 | parse_rtattr_nested(attr, lu->maxattr, |
| 179 | linkinfo[IFLA_INFO_DATA]); |
| 180 | data = attr; |
| 181 | } |
| 182 | lu->print_opt(lu, fp, data); |
| 183 | |
| 184 | if (linkinfo[IFLA_INFO_XSTATS] && show_stats && |
| 185 | lu->print_xstats) |
| 186 | lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]); |
| 187 | } |
| 188 | } |
| 189 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 190 | int print_linkinfo(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 191 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 192 | { |
| 193 | FILE *fp = (FILE*)arg; |
| 194 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 195 | struct rtattr * tb[IFLA_MAX+1]; |
| 196 | int len = n->nlmsg_len; |
| 197 | unsigned m_flag = 0; |
| 198 | |
| 199 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) |
| 200 | return 0; |
| 201 | |
| 202 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 203 | if (len < 0) |
| 204 | return -1; |
| 205 | |
| 206 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) |
| 207 | return 0; |
| 208 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 209 | return 0; |
| 210 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 211 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 212 | if (tb[IFLA_IFNAME] == NULL) { |
| 213 | fprintf(stderr, "BUG: nil ifname\n"); |
| 214 | return -1; |
| 215 | } |
| 216 | if (filter.label && |
| 217 | (!filter.family || filter.family == AF_PACKET) && |
| 218 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) |
| 219 | return 0; |
| 220 | |
| 221 | if (n->nlmsg_type == RTM_DELLINK) |
| 222 | fprintf(fp, "Deleted "); |
| 223 | |
| 224 | fprintf(fp, "%d: %s", ifi->ifi_index, |
| 225 | tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>"); |
| 226 | |
| 227 | if (tb[IFLA_LINK]) { |
| 228 | SPRINT_BUF(b1); |
| 229 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 230 | if (iflink == 0) |
| 231 | fprintf(fp, "@NONE: "); |
| 232 | else { |
| 233 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); |
| 234 | m_flag = ll_index_to_flags(iflink); |
| 235 | m_flag = !(m_flag & IFF_UP); |
| 236 | } |
| 237 | } else { |
| 238 | fprintf(fp, ": "); |
| 239 | } |
| 240 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
| 241 | |
| 242 | if (tb[IFLA_MTU]) |
| 243 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
| 244 | if (tb[IFLA_QDISC]) |
| 245 | fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); |
| 246 | #ifdef IFLA_MASTER |
| 247 | if (tb[IFLA_MASTER]) { |
| 248 | SPRINT_BUF(b1); |
| 249 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
| 250 | } |
| 251 | #endif |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 252 | if (tb[IFLA_OPERSTATE]) |
| 253 | print_operstate(fp, *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE])); |
| 254 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 255 | if (filter.showqueue) |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 256 | print_queuelen(fp, (char*)RTA_DATA(tb[IFLA_IFNAME])); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 257 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 258 | if (!filter.family || filter.family == AF_PACKET) { |
| 259 | SPRINT_BUF(b1); |
| 260 | fprintf(fp, "%s", _SL_); |
| 261 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); |
| 262 | |
| 263 | if (tb[IFLA_ADDRESS]) { |
| 264 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
| 265 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 266 | ifi->ifi_type, |
| 267 | b1, sizeof(b1))); |
| 268 | } |
| 269 | if (tb[IFLA_BROADCAST]) { |
| 270 | if (ifi->ifi_flags&IFF_POINTOPOINT) |
| 271 | fprintf(fp, " peer "); |
| 272 | else |
| 273 | fprintf(fp, " brd "); |
| 274 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
| 275 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 276 | ifi->ifi_type, |
| 277 | b1, sizeof(b1))); |
| 278 | } |
| 279 | } |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 280 | |
| 281 | if (do_link && tb[IFLA_LINKINFO] && show_details) |
| 282 | print_linktype(fp, tb[IFLA_LINKINFO]); |
| 283 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 284 | if (do_link && tb[IFLA_STATS] && show_stats) { |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 285 | struct rtnl_link_stats slocal; |
| 286 | struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 287 | if (((unsigned long)s) & (sizeof(unsigned long)-1)) { |
| 288 | memcpy(&slocal, s, sizeof(slocal)); |
| 289 | s = &slocal; |
| 290 | } |
| 291 | fprintf(fp, "%s", _SL_); |
| 292 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", |
| 293 | s->rx_compressed ? "compressed" : "", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 294 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 295 | s->rx_bytes, s->rx_packets, s->rx_errors, |
| 296 | s->rx_dropped, s->rx_over_errors, |
| 297 | s->multicast |
| 298 | ); |
| 299 | if (s->rx_compressed) |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 300 | fprintf(fp, " %-7u", s->rx_compressed); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 301 | if (show_stats > 1) { |
| 302 | fprintf(fp, "%s", _SL_); |
| 303 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 304 | fprintf(fp, " %-7u %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 305 | s->rx_length_errors, |
| 306 | s->rx_crc_errors, |
| 307 | s->rx_frame_errors, |
| 308 | s->rx_fifo_errors, |
| 309 | s->rx_missed_errors |
| 310 | ); |
| 311 | } |
| 312 | fprintf(fp, "%s", _SL_); |
| 313 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", |
| 314 | s->tx_compressed ? "compressed" : "", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 315 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 316 | s->tx_bytes, s->tx_packets, s->tx_errors, |
| 317 | s->tx_dropped, s->tx_carrier_errors, s->collisions); |
| 318 | if (s->tx_compressed) |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 319 | fprintf(fp, " %-7u", s->tx_compressed); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 320 | if (show_stats > 1) { |
| 321 | fprintf(fp, "%s", _SL_); |
| 322 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); |
net[shemminger]!shemminger | ae0f144 | 2004-10-19 19:57:38 +0000 | [diff] [blame] | 323 | fprintf(fp, " %-7u %-7u %-7u %-7u", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 324 | s->tx_aborted_errors, |
| 325 | s->tx_fifo_errors, |
| 326 | s->tx_window_errors, |
| 327 | s->tx_heartbeat_errors |
| 328 | ); |
| 329 | } |
| 330 | } |
| 331 | fprintf(fp, "\n"); |
| 332 | fflush(fp); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int flush_update(void) |
| 337 | { |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 338 | if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) { |
Stephen Hemminger | 1fb0a99 | 2008-01-26 11:08:31 -0800 | [diff] [blame] | 339 | perror("Failed to send flush request"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 340 | return -1; |
| 341 | } |
| 342 | filter.flushp = 0; |
| 343 | return 0; |
| 344 | } |
| 345 | |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 346 | static int set_lifetime(unsigned int *lifetime, char *argv) |
| 347 | { |
| 348 | if (strcmp(argv, "forever") == 0) |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 349 | *lifetime = INFINITY_LIFE_TIME; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 350 | else if (get_u32(lifetime, argv, 0)) |
| 351 | return -1; |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 356 | int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 357 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 358 | { |
| 359 | FILE *fp = (FILE*)arg; |
| 360 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 361 | int len = n->nlmsg_len; |
| 362 | struct rtattr * rta_tb[IFA_MAX+1]; |
| 363 | char abuf[256]; |
| 364 | SPRINT_BUF(b1); |
| 365 | |
| 366 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) |
| 367 | return 0; |
| 368 | len -= NLMSG_LENGTH(sizeof(*ifa)); |
| 369 | if (len < 0) { |
| 370 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 371 | return -1; |
| 372 | } |
| 373 | |
| 374 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) |
| 375 | return 0; |
| 376 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 377 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
| 378 | |
| 379 | if (!rta_tb[IFA_LOCAL]) |
| 380 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; |
| 381 | if (!rta_tb[IFA_ADDRESS]) |
| 382 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; |
| 383 | |
| 384 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) |
| 385 | return 0; |
| 386 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 387 | return 0; |
| 388 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 389 | return 0; |
| 390 | if (filter.label) { |
| 391 | SPRINT_BUF(b1); |
| 392 | const char *label; |
| 393 | if (rta_tb[IFA_LABEL]) |
| 394 | label = RTA_DATA(rta_tb[IFA_LABEL]); |
| 395 | else |
| 396 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 397 | if (fnmatch(filter.label, label, 0) != 0) |
| 398 | return 0; |
| 399 | } |
| 400 | if (filter.pfx.family) { |
| 401 | if (rta_tb[IFA_LOCAL]) { |
| 402 | inet_prefix dst; |
| 403 | memset(&dst, 0, sizeof(dst)); |
| 404 | dst.family = ifa->ifa_family; |
| 405 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); |
| 406 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 407 | return 0; |
| 408 | } |
| 409 | } |
| 410 | |
net[shemminger]!shemminger | 3eb1731 | 2005-02-07 18:28:31 +0000 | [diff] [blame] | 411 | if (filter.family && filter.family != ifa->ifa_family) |
| 412 | return 0; |
| 413 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 414 | if (filter.flushb) { |
| 415 | struct nlmsghdr *fn; |
| 416 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
| 417 | if (flush_update()) |
| 418 | return -1; |
| 419 | } |
| 420 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
| 421 | memcpy(fn, n, n->nlmsg_len); |
| 422 | fn->nlmsg_type = RTM_DELADDR; |
| 423 | fn->nlmsg_flags = NLM_F_REQUEST; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 424 | fn->nlmsg_seq = ++rth.seq; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 425 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
| 426 | filter.flushed++; |
| 427 | if (show_stats < 2) |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | if (n->nlmsg_type == RTM_DELADDR) |
| 432 | fprintf(fp, "Deleted "); |
| 433 | |
| 434 | if (filter.oneline || filter.flushb) |
| 435 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
| 436 | if (ifa->ifa_family == AF_INET) |
| 437 | fprintf(fp, " inet "); |
| 438 | else if (ifa->ifa_family == AF_INET6) |
| 439 | fprintf(fp, " inet6 "); |
| 440 | else if (ifa->ifa_family == AF_DECnet) |
| 441 | fprintf(fp, " dnet "); |
| 442 | else if (ifa->ifa_family == AF_IPX) |
| 443 | fprintf(fp, " ipx "); |
| 444 | else |
| 445 | fprintf(fp, " family %d ", ifa->ifa_family); |
| 446 | |
| 447 | if (rta_tb[IFA_LOCAL]) { |
| 448 | fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family, |
| 449 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), |
| 450 | RTA_DATA(rta_tb[IFA_LOCAL]), |
| 451 | abuf, sizeof(abuf))); |
| 452 | |
| 453 | if (rta_tb[IFA_ADDRESS] == NULL || |
| 454 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { |
| 455 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); |
| 456 | } else { |
| 457 | fprintf(fp, " peer %s/%d ", |
| 458 | rt_addr_n2a(ifa->ifa_family, |
| 459 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), |
| 460 | RTA_DATA(rta_tb[IFA_ADDRESS]), |
| 461 | abuf, sizeof(abuf)), |
| 462 | ifa->ifa_prefixlen); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (rta_tb[IFA_BROADCAST]) { |
| 467 | fprintf(fp, "brd %s ", |
| 468 | rt_addr_n2a(ifa->ifa_family, |
| 469 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), |
| 470 | RTA_DATA(rta_tb[IFA_BROADCAST]), |
| 471 | abuf, sizeof(abuf))); |
| 472 | } |
| 473 | if (rta_tb[IFA_ANYCAST]) { |
| 474 | fprintf(fp, "any %s ", |
| 475 | rt_addr_n2a(ifa->ifa_family, |
| 476 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), |
| 477 | RTA_DATA(rta_tb[IFA_ANYCAST]), |
| 478 | abuf, sizeof(abuf))); |
| 479 | } |
| 480 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); |
| 481 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
| 482 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
| 483 | fprintf(fp, "secondary "); |
| 484 | } |
| 485 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { |
| 486 | ifa->ifa_flags &= ~IFA_F_TENTATIVE; |
| 487 | fprintf(fp, "tentative "); |
| 488 | } |
| 489 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { |
| 490 | ifa->ifa_flags &= ~IFA_F_DEPRECATED; |
| 491 | fprintf(fp, "deprecated "); |
| 492 | } |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 493 | if (ifa->ifa_flags&IFA_F_HOMEADDRESS) { |
| 494 | ifa->ifa_flags &= ~IFA_F_HOMEADDRESS; |
| 495 | fprintf(fp, "home "); |
| 496 | } |
| 497 | if (ifa->ifa_flags&IFA_F_NODAD) { |
| 498 | ifa->ifa_flags &= ~IFA_F_NODAD; |
| 499 | fprintf(fp, "nodad "); |
| 500 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 501 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { |
| 502 | fprintf(fp, "dynamic "); |
| 503 | } else |
| 504 | ifa->ifa_flags &= ~IFA_F_PERMANENT; |
| 505 | if (ifa->ifa_flags) |
| 506 | fprintf(fp, "flags %02x ", ifa->ifa_flags); |
| 507 | if (rta_tb[IFA_LABEL]) |
| 508 | fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL])); |
| 509 | if (rta_tb[IFA_CACHEINFO]) { |
| 510 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
| 511 | char buf[128]; |
| 512 | fprintf(fp, "%s", _SL_); |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 513 | if (ci->ifa_valid == INFINITY_LIFE_TIME) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 514 | sprintf(buf, "valid_lft forever"); |
| 515 | else |
Masahide NAKAMURA | 0cc5ebf | 2006-11-24 12:26:53 +0900 | [diff] [blame] | 516 | sprintf(buf, "valid_lft %usec", ci->ifa_valid); |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 517 | if (ci->ifa_prefered == INFINITY_LIFE_TIME) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 518 | sprintf(buf+strlen(buf), " preferred_lft forever"); |
| 519 | else |
Masahide NAKAMURA | 0cc5ebf | 2006-11-24 12:26:53 +0900 | [diff] [blame] | 520 | sprintf(buf+strlen(buf), " preferred_lft %usec", |
| 521 | ci->ifa_prefered); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 522 | fprintf(fp, " %s", buf); |
| 523 | } |
| 524 | fprintf(fp, "\n"); |
| 525 | fflush(fp); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | struct nlmsg_list |
| 531 | { |
| 532 | struct nlmsg_list *next; |
| 533 | struct nlmsghdr h; |
| 534 | }; |
| 535 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 536 | static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 537 | { |
| 538 | for ( ;ainfo ; ainfo = ainfo->next) { |
| 539 | struct nlmsghdr *n = &ainfo->h; |
| 540 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 541 | |
| 542 | if (n->nlmsg_type != RTM_NEWADDR) |
| 543 | continue; |
| 544 | |
| 545 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) |
| 546 | return -1; |
| 547 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 548 | if (ifa->ifa_index != ifindex || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 549 | (filter.family && filter.family != ifa->ifa_family)) |
| 550 | continue; |
| 551 | |
| 552 | print_addrinfo(NULL, n, fp); |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 558 | 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] | 559 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 560 | { |
| 561 | struct nlmsg_list **linfo = (struct nlmsg_list**)arg; |
| 562 | struct nlmsg_list *h; |
| 563 | struct nlmsg_list **lp; |
| 564 | |
| 565 | h = malloc(n->nlmsg_len+sizeof(void*)); |
| 566 | if (h == NULL) |
| 567 | return -1; |
| 568 | |
| 569 | memcpy(&h->h, n, n->nlmsg_len); |
| 570 | h->next = NULL; |
| 571 | |
| 572 | for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */; |
| 573 | *lp = h; |
| 574 | |
| 575 | ll_remember_index(who, n, NULL); |
| 576 | return 0; |
| 577 | } |
| 578 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 579 | static int ipaddr_list_or_flush(int argc, char **argv, int flush) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 580 | { |
| 581 | struct nlmsg_list *linfo = NULL; |
| 582 | struct nlmsg_list *ainfo = NULL; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 583 | struct nlmsg_list *l, *n; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 584 | char *filter_dev = NULL; |
| 585 | int no_link = 0; |
| 586 | |
| 587 | ipaddr_reset_filter(oneline); |
| 588 | filter.showqueue = 1; |
| 589 | |
| 590 | if (filter.family == AF_UNSPEC) |
| 591 | filter.family = preferred_family; |
| 592 | |
| 593 | if (flush) { |
| 594 | if (argc <= 0) { |
| 595 | fprintf(stderr, "Flush requires arguments.\n"); |
| 596 | return -1; |
| 597 | } |
| 598 | if (filter.family == AF_PACKET) { |
| 599 | fprintf(stderr, "Cannot flush link addresses.\n"); |
| 600 | return -1; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | while (argc > 0) { |
| 605 | if (strcmp(*argv, "to") == 0) { |
| 606 | NEXT_ARG(); |
| 607 | get_prefix(&filter.pfx, *argv, filter.family); |
| 608 | if (filter.family == AF_UNSPEC) |
| 609 | filter.family = filter.pfx.family; |
| 610 | } else if (strcmp(*argv, "scope") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 611 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 612 | NEXT_ARG(); |
| 613 | filter.scopemask = -1; |
| 614 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 615 | if (strcmp(*argv, "all") != 0) |
| 616 | invarg("invalid \"scope\"\n", *argv); |
| 617 | scope = RT_SCOPE_NOWHERE; |
| 618 | filter.scopemask = 0; |
| 619 | } |
| 620 | filter.scope = scope; |
| 621 | } else if (strcmp(*argv, "up") == 0) { |
| 622 | filter.up = 1; |
| 623 | } else if (strcmp(*argv, "dynamic") == 0) { |
| 624 | filter.flags &= ~IFA_F_PERMANENT; |
| 625 | filter.flagmask |= IFA_F_PERMANENT; |
| 626 | } else if (strcmp(*argv, "permanent") == 0) { |
| 627 | filter.flags |= IFA_F_PERMANENT; |
| 628 | filter.flagmask |= IFA_F_PERMANENT; |
| 629 | } else if (strcmp(*argv, "secondary") == 0) { |
| 630 | filter.flags |= IFA_F_SECONDARY; |
| 631 | filter.flagmask |= IFA_F_SECONDARY; |
| 632 | } else if (strcmp(*argv, "primary") == 0) { |
| 633 | filter.flags &= ~IFA_F_SECONDARY; |
| 634 | filter.flagmask |= IFA_F_SECONDARY; |
| 635 | } else if (strcmp(*argv, "tentative") == 0) { |
| 636 | filter.flags |= IFA_F_TENTATIVE; |
| 637 | filter.flagmask |= IFA_F_TENTATIVE; |
| 638 | } else if (strcmp(*argv, "deprecated") == 0) { |
| 639 | filter.flags |= IFA_F_DEPRECATED; |
| 640 | filter.flagmask |= IFA_F_DEPRECATED; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 641 | } else if (strcmp(*argv, "home") == 0) { |
| 642 | filter.flags |= IFA_F_HOMEADDRESS; |
| 643 | filter.flagmask |= IFA_F_HOMEADDRESS; |
| 644 | } else if (strcmp(*argv, "nodad") == 0) { |
| 645 | filter.flags |= IFA_F_NODAD; |
| 646 | filter.flagmask |= IFA_F_NODAD; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 647 | } else if (strcmp(*argv, "label") == 0) { |
| 648 | NEXT_ARG(); |
| 649 | filter.label = *argv; |
| 650 | } else { |
| 651 | if (strcmp(*argv, "dev") == 0) { |
| 652 | NEXT_ARG(); |
| 653 | } |
| 654 | if (matches(*argv, "help") == 0) |
| 655 | usage(); |
| 656 | if (filter_dev) |
| 657 | duparg2("dev", *argv); |
| 658 | filter_dev = *argv; |
| 659 | } |
| 660 | argv++; argc--; |
| 661 | } |
| 662 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 663 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { |
| 664 | perror("Cannot send dump request"); |
| 665 | exit(1); |
| 666 | } |
| 667 | |
| 668 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) { |
| 669 | fprintf(stderr, "Dump terminated\n"); |
| 670 | exit(1); |
| 671 | } |
| 672 | |
| 673 | if (filter_dev) { |
| 674 | filter.ifindex = ll_name_to_index(filter_dev); |
| 675 | if (filter.ifindex <= 0) { |
| 676 | fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev); |
| 677 | return -1; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if (flush) { |
| 682 | int round = 0; |
| 683 | char flushb[4096-512]; |
| 684 | |
| 685 | filter.flushb = flushb; |
| 686 | filter.flushp = 0; |
| 687 | filter.flushe = sizeof(flushb); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 688 | |
Daniel Silverstone | 7b3d366 | 2007-10-19 13:32:24 +0200 | [diff] [blame^] | 689 | while (round < MAX_ROUNDS) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 690 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 691 | perror("Cannot send dump request"); |
| 692 | exit(1); |
| 693 | } |
| 694 | filter.flushed = 0; |
| 695 | if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) { |
| 696 | fprintf(stderr, "Flush terminated\n"); |
| 697 | exit(1); |
| 698 | } |
| 699 | if (filter.flushed == 0) { |
| 700 | if (round == 0) { |
| 701 | fprintf(stderr, "Nothing to flush.\n"); |
| 702 | } else if (show_stats) |
| 703 | printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); |
| 704 | fflush(stdout); |
| 705 | return 0; |
| 706 | } |
| 707 | round++; |
| 708 | if (flush_update() < 0) |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 709 | return 1; |
| 710 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 711 | if (show_stats) { |
| 712 | printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed); |
| 713 | fflush(stdout); |
| 714 | } |
| 715 | } |
Daniel Silverstone | 7b3d366 | 2007-10-19 13:32:24 +0200 | [diff] [blame^] | 716 | fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", MAX_ROUNDS); fflush(stderr); |
| 717 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | if (filter.family != AF_PACKET) { |
| 721 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 722 | perror("Cannot send dump request"); |
| 723 | exit(1); |
| 724 | } |
| 725 | |
| 726 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) { |
| 727 | fprintf(stderr, "Dump terminated\n"); |
| 728 | exit(1); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | |
| 733 | if (filter.family && filter.family != AF_PACKET) { |
| 734 | struct nlmsg_list **lp; |
| 735 | lp=&linfo; |
| 736 | |
| 737 | if (filter.oneline) |
| 738 | no_link = 1; |
| 739 | |
| 740 | while ((l=*lp)!=NULL) { |
| 741 | int ok = 0; |
| 742 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 743 | struct nlmsg_list *a; |
| 744 | |
| 745 | for (a=ainfo; a; a=a->next) { |
| 746 | struct nlmsghdr *n = &a->h; |
| 747 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 748 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 749 | if (ifa->ifa_index != ifi->ifi_index || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 750 | (filter.family && filter.family != ifa->ifa_family)) |
| 751 | continue; |
| 752 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 753 | continue; |
| 754 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 755 | continue; |
| 756 | if (filter.pfx.family || filter.label) { |
| 757 | struct rtattr *tb[IFA_MAX+1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 758 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); |
| 759 | if (!tb[IFA_LOCAL]) |
| 760 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; |
| 761 | |
| 762 | if (filter.pfx.family && tb[IFA_LOCAL]) { |
| 763 | inet_prefix dst; |
| 764 | memset(&dst, 0, sizeof(dst)); |
| 765 | dst.family = ifa->ifa_family; |
| 766 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); |
| 767 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 768 | continue; |
| 769 | } |
| 770 | if (filter.label) { |
| 771 | SPRINT_BUF(b1); |
| 772 | const char *label; |
| 773 | if (tb[IFA_LABEL]) |
| 774 | label = RTA_DATA(tb[IFA_LABEL]); |
| 775 | else |
| 776 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 777 | if (fnmatch(filter.label, label, 0) != 0) |
| 778 | continue; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | ok = 1; |
| 783 | break; |
| 784 | } |
| 785 | if (!ok) |
| 786 | *lp = l->next; |
| 787 | else |
| 788 | lp = &l->next; |
| 789 | } |
| 790 | } |
| 791 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 792 | for (l=linfo; l; l = n) { |
| 793 | n = l->next; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 794 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { |
| 795 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 796 | if (filter.family != AF_PACKET) |
| 797 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); |
| 798 | } |
| 799 | fflush(stdout); |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 800 | free(l); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 801 | } |
| 802 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 803 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | int ipaddr_list_link(int argc, char **argv) |
| 807 | { |
| 808 | preferred_family = AF_PACKET; |
| 809 | do_link = 1; |
| 810 | return ipaddr_list_or_flush(argc, argv, 0); |
| 811 | } |
| 812 | |
| 813 | void ipaddr_reset_filter(int oneline) |
| 814 | { |
| 815 | memset(&filter, 0, sizeof(filter)); |
| 816 | filter.oneline = oneline; |
| 817 | } |
| 818 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 819 | static int default_scope(inet_prefix *lcl) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 820 | { |
| 821 | if (lcl->family == AF_INET) { |
| 822 | if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127) |
| 823 | return RT_SCOPE_HOST; |
| 824 | } |
| 825 | return 0; |
| 826 | } |
| 827 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 828 | static int ipaddr_modify(int cmd, int flags, int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 829 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 830 | struct { |
| 831 | struct nlmsghdr n; |
| 832 | struct ifaddrmsg ifa; |
| 833 | char buf[256]; |
| 834 | } req; |
| 835 | char *d = NULL; |
| 836 | char *l = NULL; |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 837 | char *lcl_arg = NULL; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 838 | char *valid_lftp = NULL; |
| 839 | char *preferred_lftp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 840 | inet_prefix lcl; |
| 841 | inet_prefix peer; |
| 842 | int local_len = 0; |
| 843 | int peer_len = 0; |
| 844 | int brd_len = 0; |
| 845 | int any_len = 0; |
| 846 | int scoped = 0; |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 847 | __u32 preferred_lft = INFINITY_LIFE_TIME; |
| 848 | __u32 valid_lft = INFINITY_LIFE_TIME; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 849 | struct ifa_cacheinfo cinfo; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 850 | |
| 851 | memset(&req, 0, sizeof(req)); |
| 852 | |
| 853 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 854 | req.n.nlmsg_flags = NLM_F_REQUEST | flags; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 855 | req.n.nlmsg_type = cmd; |
| 856 | req.ifa.ifa_family = preferred_family; |
| 857 | |
| 858 | while (argc > 0) { |
| 859 | if (strcmp(*argv, "peer") == 0 || |
| 860 | strcmp(*argv, "remote") == 0) { |
| 861 | NEXT_ARG(); |
| 862 | |
| 863 | if (peer_len) |
| 864 | duparg("peer", *argv); |
| 865 | get_prefix(&peer, *argv, req.ifa.ifa_family); |
| 866 | peer_len = peer.bytelen; |
| 867 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 868 | req.ifa.ifa_family = peer.family; |
| 869 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); |
| 870 | req.ifa.ifa_prefixlen = peer.bitlen; |
| 871 | } else if (matches(*argv, "broadcast") == 0 || |
| 872 | strcmp(*argv, "brd") == 0) { |
| 873 | inet_prefix addr; |
| 874 | NEXT_ARG(); |
| 875 | if (brd_len) |
| 876 | duparg("broadcast", *argv); |
| 877 | if (strcmp(*argv, "+") == 0) |
| 878 | brd_len = -1; |
| 879 | else if (strcmp(*argv, "-") == 0) |
| 880 | brd_len = -2; |
| 881 | else { |
| 882 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 883 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 884 | req.ifa.ifa_family = addr.family; |
| 885 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); |
| 886 | brd_len = addr.bytelen; |
| 887 | } |
| 888 | } else if (strcmp(*argv, "anycast") == 0) { |
| 889 | inet_prefix addr; |
| 890 | NEXT_ARG(); |
| 891 | if (any_len) |
| 892 | duparg("anycast", *argv); |
| 893 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 894 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 895 | req.ifa.ifa_family = addr.family; |
| 896 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); |
| 897 | any_len = addr.bytelen; |
| 898 | } else if (strcmp(*argv, "scope") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 899 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 900 | NEXT_ARG(); |
| 901 | if (rtnl_rtscope_a2n(&scope, *argv)) |
| 902 | invarg(*argv, "invalid scope value."); |
| 903 | req.ifa.ifa_scope = scope; |
| 904 | scoped = 1; |
| 905 | } else if (strcmp(*argv, "dev") == 0) { |
| 906 | NEXT_ARG(); |
| 907 | d = *argv; |
| 908 | } else if (strcmp(*argv, "label") == 0) { |
| 909 | NEXT_ARG(); |
| 910 | l = *argv; |
| 911 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 912 | } else if (matches(*argv, "valid_lft") == 0) { |
| 913 | if (valid_lftp) |
| 914 | duparg("valid_lft", *argv); |
| 915 | NEXT_ARG(); |
| 916 | valid_lftp = *argv; |
| 917 | if (set_lifetime(&valid_lft, *argv)) |
| 918 | invarg("valid_lft value", *argv); |
| 919 | } else if (matches(*argv, "preferred_lft") == 0) { |
| 920 | if (preferred_lftp) |
| 921 | duparg("preferred_lft", *argv); |
| 922 | NEXT_ARG(); |
| 923 | preferred_lftp = *argv; |
| 924 | if (set_lifetime(&preferred_lft, *argv)) |
| 925 | invarg("preferred_lft value", *argv); |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 926 | } else if (strcmp(*argv, "home") == 0) { |
| 927 | req.ifa.ifa_flags |= IFA_F_HOMEADDRESS; |
| 928 | } else if (strcmp(*argv, "nodad") == 0) { |
| 929 | req.ifa.ifa_flags |= IFA_F_NODAD; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 930 | } else { |
| 931 | if (strcmp(*argv, "local") == 0) { |
| 932 | NEXT_ARG(); |
| 933 | } |
| 934 | if (matches(*argv, "help") == 0) |
| 935 | usage(); |
| 936 | if (local_len) |
| 937 | duparg2("local", *argv); |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 938 | lcl_arg = *argv; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 939 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 940 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 941 | req.ifa.ifa_family = lcl.family; |
| 942 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 943 | local_len = lcl.bytelen; |
| 944 | } |
| 945 | argc--; argv++; |
| 946 | } |
| 947 | if (d == NULL) { |
| 948 | fprintf(stderr, "Not enough information: \"dev\" argument is required.\n"); |
| 949 | return -1; |
| 950 | } |
| 951 | if (l && matches(d, l) != 0) { |
| 952 | fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l); |
| 953 | exit(1); |
| 954 | } |
| 955 | |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 956 | if (peer_len == 0 && local_len) { |
| 957 | if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) { |
| 958 | fprintf(stderr, |
| 959 | "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \ |
| 960 | " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \ |
| 961 | " This special behaviour is likely to disappear in further releases,\n" \ |
| 962 | " fix your scripts!\n", lcl_arg, local_len*8); |
| 963 | } else { |
| 964 | peer = lcl; |
| 965 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 966 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 967 | } |
| 968 | if (req.ifa.ifa_prefixlen == 0) |
| 969 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 970 | |
| 971 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 972 | inet_prefix brd; |
| 973 | int i; |
| 974 | if (req.ifa.ifa_family != AF_INET) { |
| 975 | fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n"); |
| 976 | return -1; |
| 977 | } |
| 978 | brd = peer; |
| 979 | if (brd.bitlen <= 30) { |
| 980 | for (i=31; i>=brd.bitlen; i--) { |
| 981 | if (brd_len == -1) |
| 982 | brd.data[0] |= htonl(1<<(31-i)); |
| 983 | else |
| 984 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 985 | } |
| 986 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 987 | brd_len = brd.bytelen; |
| 988 | } |
| 989 | } |
| 990 | if (!scoped && cmd != RTM_DELADDR) |
| 991 | req.ifa.ifa_scope = default_scope(&lcl); |
| 992 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 993 | ll_init_map(&rth); |
| 994 | |
| 995 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { |
| 996 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
| 997 | return -1; |
| 998 | } |
| 999 | |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1000 | if (valid_lftp || preferred_lftp) { |
| 1001 | if (!valid_lft) { |
| 1002 | fprintf(stderr, "valid_lft is zero\n"); |
| 1003 | return -1; |
| 1004 | } |
| 1005 | if (valid_lft < preferred_lft) { |
| 1006 | fprintf(stderr, "preferred_lft is greater than valid_lft\n"); |
| 1007 | return -1; |
| 1008 | } |
| 1009 | |
| 1010 | memset(&cinfo, 0, sizeof(cinfo)); |
| 1011 | cinfo.ifa_prefered = preferred_lft; |
| 1012 | cinfo.ifa_valid = valid_lft; |
| 1013 | addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo, |
| 1014 | sizeof(cinfo)); |
| 1015 | } |
| 1016 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1017 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 1018 | exit(2); |
| 1019 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1020 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | int do_ipaddr(int argc, char **argv) |
| 1024 | { |
| 1025 | if (argc < 1) |
| 1026 | return ipaddr_list_or_flush(0, NULL, 0); |
| 1027 | if (matches(*argv, "add") == 0) |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1028 | return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); |
| 1029 | if (matches(*argv, "change") == 0 || |
| 1030 | strcmp(*argv, "chg") == 0) |
| 1031 | return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1); |
| 1032 | if (matches(*argv, "replace") == 0) |
| 1033 | return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1034 | if (matches(*argv, "delete") == 0) |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1035 | return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1036 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 1037 | || matches(*argv, "lst") == 0) |
| 1038 | return ipaddr_list_or_flush(argc-1, argv+1, 0); |
| 1039 | if (matches(*argv, "flush") == 0) |
| 1040 | return ipaddr_list_or_flush(argc-1, argv+1, 1); |
| 1041 | if (matches(*argv, "help") == 0) |
| 1042 | usage(); |
Alexander Wirt | b096fa5 | 2007-10-12 10:56:36 +0200 | [diff] [blame] | 1043 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1044 | exit(-1); |
| 1045 | } |
| 1046 | |