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