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