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 | * |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <syslog.h> |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 17 | #include <inttypes.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <sys/ioctl.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/ioctl.h> |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 22 | #include <sys/errno.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <string.h> |
| 26 | #include <fnmatch.h> |
| 27 | |
osdl.org!shemminger | e5779fb | 2004-06-09 22:56:28 +0000 | [diff] [blame] | 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/if_arp.h> |
| 30 | #include <linux/sockios.h> |
| 31 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 32 | #include "rt_names.h" |
| 33 | #include "utils.h" |
| 34 | #include "ll_map.h" |
| 35 | #include "ip_common.h" |
| 36 | |
Daniel Silverstone | 7b3d366 | 2007-10-19 13:32:24 +0200 | [diff] [blame] | 37 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 38 | static struct |
| 39 | { |
| 40 | int ifindex; |
| 41 | int family; |
| 42 | int oneline; |
| 43 | int showqueue; |
| 44 | inet_prefix pfx; |
| 45 | int scope, scopemask; |
| 46 | int flags, flagmask; |
| 47 | int up; |
| 48 | char *label; |
| 49 | int flushed; |
| 50 | char *flushb; |
| 51 | int flushp; |
| 52 | int flushe; |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 53 | int group; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 54 | } filter; |
| 55 | |
| 56 | static int do_link; |
| 57 | |
| 58 | static void usage(void) __attribute__((noreturn)); |
| 59 | |
| 60 | static void usage(void) |
| 61 | { |
| 62 | if (do_link) { |
| 63 | iplink_usage(); |
| 64 | } |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 65 | fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n"); |
Brian Haley | a1f2779 | 2009-12-03 10:39:36 +0000 | [diff] [blame] | 66 | fprintf(stderr, " [ CONFFLAG-LIST ]\n"); |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 67 | fprintf(stderr, " ip addr del IFADDR dev STRING\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 68 | fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n"); |
| 69 | fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n"); |
| 70 | fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n"); |
| 71 | fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n"); |
| 72 | fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n"); |
| 73 | fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n"); |
| 74 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
| 75 | fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n"); |
Brian Haley | a1b9ffc | 2009-09-14 17:01:43 -0400 | [diff] [blame] | 76 | fprintf(stderr, " tentative | deprecated | dadfailed | temporary |\n"); |
Brian Haley | a1f2779 | 2009-12-03 10:39:36 +0000 | [diff] [blame] | 77 | fprintf(stderr, " CONFFLAG-LIST ]\n"); |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 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); |
Oliver Hartkopp | 98f9a1d | 2009-03-27 11:21:29 -0700 | [diff] [blame] | 112 | _PF(ECHO); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 113 | #undef _PF |
| 114 | if (flags) |
| 115 | fprintf(fp, "%x", flags); |
| 116 | if (mdown) |
| 117 | fprintf(fp, ",M-DOWN"); |
| 118 | fprintf(fp, "> "); |
| 119 | } |
| 120 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 121 | static const char *oper_states[] = { |
| 122 | "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN", |
| 123 | "TESTING", "DORMANT", "UP" |
| 124 | }; |
| 125 | |
| 126 | static void print_operstate(FILE *f, __u8 state) |
| 127 | { |
| 128 | if (state >= sizeof(oper_states)/sizeof(oper_states[0])) |
| 129 | fprintf(f, "state %#x ", state); |
| 130 | else |
| 131 | fprintf(f, "state %s ", oper_states[state]); |
| 132 | } |
| 133 | |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 134 | static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1]) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 135 | { |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 136 | int qlen; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 137 | |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 138 | if (tb[IFLA_TXQLEN]) |
| 139 | qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]); |
| 140 | else { |
| 141 | struct ifreq ifr; |
| 142 | int s = socket(AF_INET, SOCK_STREAM, 0); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 143 | |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 144 | if (s < 0) |
| 145 | return; |
| 146 | |
| 147 | memset(&ifr, 0, sizeof(ifr)); |
| 148 | strcpy(ifr.ifr_name, (char *)RTA_DATA(tb[IFLA_IFNAME])); |
| 149 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
| 150 | fprintf(f, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno)); |
| 151 | close(s); |
| 152 | return; |
| 153 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 154 | close(s); |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 155 | qlen = ifr.ifr_qlen; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 156 | } |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 157 | if (qlen) |
| 158 | fprintf(f, "qlen %d", qlen); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 161 | static void print_linktype(FILE *fp, struct rtattr *tb) |
| 162 | { |
| 163 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 164 | struct link_util *lu; |
| 165 | char *kind; |
| 166 | |
| 167 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); |
| 168 | |
| 169 | if (!linkinfo[IFLA_INFO_KIND]) |
| 170 | return; |
| 171 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); |
| 172 | |
| 173 | fprintf(fp, "%s", _SL_); |
| 174 | fprintf(fp, " %s ", kind); |
| 175 | |
| 176 | lu = get_link_kind(kind); |
| 177 | if (!lu || !lu->print_opt) |
| 178 | return; |
| 179 | |
| 180 | if (1) { |
| 181 | struct rtattr *attr[lu->maxattr+1], **data = NULL; |
| 182 | |
| 183 | if (linkinfo[IFLA_INFO_DATA]) { |
| 184 | parse_rtattr_nested(attr, lu->maxattr, |
| 185 | linkinfo[IFLA_INFO_DATA]); |
| 186 | data = attr; |
| 187 | } |
| 188 | lu->print_opt(lu, fp, data); |
| 189 | |
| 190 | if (linkinfo[IFLA_INFO_XSTATS] && show_stats && |
| 191 | lu->print_xstats) |
| 192 | lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]); |
| 193 | } |
| 194 | } |
| 195 | |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 196 | static void print_vfinfo(FILE *fp, struct rtattr *vfinfo) |
| 197 | { |
| 198 | struct ifla_vf_mac *vf_mac; |
| 199 | struct ifla_vf_vlan *vf_vlan; |
| 200 | struct ifla_vf_tx_rate *vf_tx_rate; |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 201 | struct ifla_vf_spoofchk *vf_spoofchk; |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 202 | struct rtattr *vf[IFLA_VF_MAX+1]; |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 203 | struct rtattr *tmp; |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 204 | SPRINT_BUF(b1); |
| 205 | |
| 206 | if (vfinfo->rta_type != IFLA_VF_INFO) { |
| 207 | fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo); |
| 212 | |
| 213 | vf_mac = RTA_DATA(vf[IFLA_VF_MAC]); |
| 214 | vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]); |
| 215 | vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]); |
| 216 | |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 217 | /* Check if the spoof checking vf info type is supported by |
| 218 | * this kernel. |
| 219 | */ |
| 220 | tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] + |
| 221 | vf[IFLA_VF_TX_RATE]->rta_len); |
| 222 | |
| 223 | if (tmp->rta_type != IFLA_VF_SPOOFCHK) |
| 224 | vf_spoofchk = NULL; |
| 225 | else |
| 226 | vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]); |
| 227 | |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 228 | fprintf(fp, "\n vf %d MAC %s", vf_mac->vf, |
| 229 | ll_addr_n2a((unsigned char *)&vf_mac->mac, |
| 230 | ETH_ALEN, 0, b1, sizeof(b1))); |
| 231 | if (vf_vlan->vlan) |
| 232 | fprintf(fp, ", vlan %d", vf_vlan->vlan); |
| 233 | if (vf_vlan->qos) |
| 234 | fprintf(fp, ", qos %d", vf_vlan->qos); |
| 235 | if (vf_tx_rate->rate) |
| 236 | fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate); |
Greg Rose | 7b8179c | 2011-10-13 20:31:32 +0000 | [diff] [blame] | 237 | if (vf_spoofchk && vf_spoofchk->setting != -1) { |
| 238 | if (vf_spoofchk->setting) |
| 239 | fprintf(fp, ", spoof checking on"); |
| 240 | else |
| 241 | fprintf(fp, ", spoof checking off"); |
| 242 | } |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 245 | static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s) { |
| 246 | fprintf(fp, "%s", _SL_); |
| 247 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", |
| 248 | s->rx_compressed ? "compressed" : "", _SL_); |
| 249 | fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", |
| 250 | (uint64_t)s->rx_bytes, |
| 251 | (uint64_t)s->rx_packets, |
| 252 | (uint64_t)s->rx_errors, |
| 253 | (uint64_t)s->rx_dropped, |
| 254 | (uint64_t)s->rx_over_errors, |
| 255 | (uint64_t)s->multicast); |
| 256 | if (s->rx_compressed) |
| 257 | fprintf(fp, " %-7"PRIu64"", |
| 258 | (uint64_t)s->rx_compressed); |
| 259 | if (show_stats > 1) { |
| 260 | fprintf(fp, "%s", _SL_); |
| 261 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); |
| 262 | fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", |
| 263 | (uint64_t)s->rx_length_errors, |
| 264 | (uint64_t)s->rx_crc_errors, |
| 265 | (uint64_t)s->rx_frame_errors, |
| 266 | (uint64_t)s->rx_fifo_errors, |
| 267 | (uint64_t)s->rx_missed_errors); |
| 268 | } |
| 269 | fprintf(fp, "%s", _SL_); |
| 270 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", |
| 271 | (uint64_t)s->tx_compressed ? "compressed" : "", _SL_); |
| 272 | fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", |
| 273 | (uint64_t)s->tx_bytes, |
| 274 | (uint64_t)s->tx_packets, |
| 275 | (uint64_t)s->tx_errors, |
| 276 | (uint64_t)s->tx_dropped, |
| 277 | (uint64_t)s->tx_carrier_errors, |
| 278 | (uint64_t)s->collisions); |
| 279 | if (s->tx_compressed) |
| 280 | fprintf(fp, " %-7"PRIu64"", |
| 281 | (uint64_t)s->tx_compressed); |
| 282 | if (show_stats > 1) { |
| 283 | fprintf(fp, "%s", _SL_); |
| 284 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); |
| 285 | fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", |
| 286 | (uint64_t)s->tx_aborted_errors, |
| 287 | (uint64_t)s->tx_fifo_errors, |
| 288 | (uint64_t)s->tx_window_errors, |
| 289 | (uint64_t)s->tx_heartbeat_errors); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static void print_link_stats(FILE *fp, const struct rtnl_link_stats *s) |
| 294 | { |
| 295 | fprintf(fp, "%s", _SL_); |
| 296 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", |
| 297 | s->rx_compressed ? "compressed" : "", _SL_); |
| 298 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", |
| 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) |
| 304 | fprintf(fp, " %-7u", s->rx_compressed); |
| 305 | if (show_stats > 1) { |
| 306 | fprintf(fp, "%s", _SL_); |
| 307 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); |
| 308 | fprintf(fp, " %-7u %-7u %-7u %-7u %-7u", |
| 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_); |
| 319 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", |
| 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) |
| 323 | fprintf(fp, " %-7u", s->tx_compressed); |
| 324 | if (show_stats > 1) { |
| 325 | fprintf(fp, "%s", _SL_); |
| 326 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); |
| 327 | fprintf(fp, " %-7u %-7u %-7u %-7u", |
| 328 | s->tx_aborted_errors, |
| 329 | s->tx_fifo_errors, |
| 330 | s->tx_window_errors, |
| 331 | s->tx_heartbeat_errors |
| 332 | ); |
| 333 | } |
| 334 | } |
| 335 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 336 | int print_linkinfo(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 337 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 338 | { |
| 339 | FILE *fp = (FILE*)arg; |
| 340 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 341 | struct rtattr * tb[IFLA_MAX+1]; |
| 342 | int len = n->nlmsg_len; |
| 343 | unsigned m_flag = 0; |
| 344 | |
| 345 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) |
| 346 | return 0; |
| 347 | |
| 348 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 349 | if (len < 0) |
| 350 | return -1; |
| 351 | |
| 352 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) |
| 353 | return 0; |
| 354 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 355 | return 0; |
| 356 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 357 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 358 | if (tb[IFLA_IFNAME] == NULL) { |
jamal | 4cd23bd | 2008-08-08 10:06:17 -0400 | [diff] [blame] | 359 | 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] | 360 | } |
| 361 | if (filter.label && |
| 362 | (!filter.family || filter.family == AF_PACKET) && |
| 363 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) |
| 364 | return 0; |
| 365 | |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 366 | if (tb[IFLA_GROUP]) { |
| 367 | int group = *(int*)RTA_DATA(tb[IFLA_GROUP]); |
| 368 | if (group != filter.group) |
| 369 | return -1; |
| 370 | } |
| 371 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 372 | if (n->nlmsg_type == RTM_DELLINK) |
| 373 | fprintf(fp, "Deleted "); |
| 374 | |
| 375 | fprintf(fp, "%d: %s", ifi->ifi_index, |
| 376 | tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>"); |
| 377 | |
| 378 | if (tb[IFLA_LINK]) { |
| 379 | SPRINT_BUF(b1); |
| 380 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 381 | if (iflink == 0) |
| 382 | fprintf(fp, "@NONE: "); |
| 383 | else { |
| 384 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); |
| 385 | m_flag = ll_index_to_flags(iflink); |
| 386 | m_flag = !(m_flag & IFF_UP); |
| 387 | } |
| 388 | } else { |
| 389 | fprintf(fp, ": "); |
| 390 | } |
| 391 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
| 392 | |
| 393 | if (tb[IFLA_MTU]) |
| 394 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
| 395 | if (tb[IFLA_QDISC]) |
| 396 | fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 397 | if (tb[IFLA_MASTER]) { |
| 398 | SPRINT_BUF(b1); |
| 399 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
| 400 | } |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 401 | if (tb[IFLA_OPERSTATE]) |
| 402 | print_operstate(fp, *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE])); |
| 403 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 404 | if (filter.showqueue) |
Eric Dumazet | f78e316 | 2009-10-22 18:13:21 +0000 | [diff] [blame] | 405 | print_queuelen(fp, tb); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 406 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 407 | if (!filter.family || filter.family == AF_PACKET) { |
| 408 | SPRINT_BUF(b1); |
| 409 | fprintf(fp, "%s", _SL_); |
| 410 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); |
| 411 | |
| 412 | if (tb[IFLA_ADDRESS]) { |
| 413 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
| 414 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 415 | ifi->ifi_type, |
| 416 | b1, sizeof(b1))); |
| 417 | } |
| 418 | if (tb[IFLA_BROADCAST]) { |
| 419 | if (ifi->ifi_flags&IFF_POINTOPOINT) |
| 420 | fprintf(fp, " peer "); |
| 421 | else |
| 422 | fprintf(fp, " brd "); |
| 423 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
| 424 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 425 | ifi->ifi_type, |
| 426 | b1, sizeof(b1))); |
| 427 | } |
| 428 | } |
Patrick McHardy | 1d93483 | 2007-08-22 10:49:01 -0700 | [diff] [blame] | 429 | |
| 430 | if (do_link && tb[IFLA_LINKINFO] && show_details) |
| 431 | print_linktype(fp, tb[IFLA_LINKINFO]); |
| 432 | |
Stephen Hemminger | ace9c96 | 2009-03-23 10:46:47 -0700 | [diff] [blame] | 433 | if (do_link && tb[IFLA_IFALIAS]) |
| 434 | fprintf(fp,"\n alias %s", |
| 435 | (const char *) RTA_DATA(tb[IFLA_IFALIAS])); |
| 436 | |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 437 | if (do_link && show_stats) { |
| 438 | if (tb[IFLA_STATS64]) |
| 439 | print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64])); |
| 440 | else if (tb[IFLA_STATS]) |
| 441 | print_link_stats(fp, RTA_DATA(tb[IFLA_STATS])); |
Jan Engelhardt | 8864ac9 | 2010-03-11 10:00:34 +0000 | [diff] [blame] | 442 | } |
Stephen Hemminger | e6e6fb5 | 2012-02-21 17:18:59 -0800 | [diff] [blame] | 443 | |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 444 | if (do_link && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) { |
| 445 | struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST]; |
| 446 | int rem = RTA_PAYLOAD(vflist); |
| 447 | for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) |
| 448 | print_vfinfo(fp, i); |
Williams, Mitch A | ae7229d | 2010-02-10 01:47:08 +0000 | [diff] [blame] | 449 | } |
Chris Wright | 3fd8663 | 2010-05-18 00:57:00 -0700 | [diff] [blame] | 450 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 451 | fprintf(fp, "\n"); |
| 452 | fflush(fp); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int flush_update(void) |
| 457 | { |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 458 | if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) { |
Stephen Hemminger | 1fb0a99 | 2008-01-26 11:08:31 -0800 | [diff] [blame] | 459 | perror("Failed to send flush request"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 460 | return -1; |
| 461 | } |
| 462 | filter.flushp = 0; |
| 463 | return 0; |
| 464 | } |
| 465 | |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 466 | static int set_lifetime(unsigned int *lifetime, char *argv) |
| 467 | { |
| 468 | if (strcmp(argv, "forever") == 0) |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 469 | *lifetime = INFINITY_LIFE_TIME; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 470 | else if (get_u32(lifetime, argv, 0)) |
| 471 | return -1; |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 476 | int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 477 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 478 | { |
| 479 | FILE *fp = (FILE*)arg; |
| 480 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 481 | int len = n->nlmsg_len; |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 482 | int deprecated = 0; |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 483 | /* Use local copy of ifa_flags to not interfere with filtering code */ |
| 484 | unsigned int ifa_flags; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 485 | struct rtattr * rta_tb[IFA_MAX+1]; |
| 486 | char abuf[256]; |
| 487 | SPRINT_BUF(b1); |
| 488 | |
| 489 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) |
| 490 | return 0; |
| 491 | len -= NLMSG_LENGTH(sizeof(*ifa)); |
| 492 | if (len < 0) { |
| 493 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) |
| 498 | return 0; |
| 499 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 500 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
| 501 | |
| 502 | if (!rta_tb[IFA_LOCAL]) |
| 503 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; |
| 504 | if (!rta_tb[IFA_ADDRESS]) |
| 505 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; |
| 506 | |
| 507 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) |
| 508 | return 0; |
| 509 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 510 | return 0; |
| 511 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 512 | return 0; |
| 513 | if (filter.label) { |
| 514 | SPRINT_BUF(b1); |
| 515 | const char *label; |
| 516 | if (rta_tb[IFA_LABEL]) |
| 517 | label = RTA_DATA(rta_tb[IFA_LABEL]); |
| 518 | else |
| 519 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 520 | if (fnmatch(filter.label, label, 0) != 0) |
| 521 | return 0; |
| 522 | } |
| 523 | if (filter.pfx.family) { |
| 524 | if (rta_tb[IFA_LOCAL]) { |
| 525 | inet_prefix dst; |
| 526 | memset(&dst, 0, sizeof(dst)); |
| 527 | dst.family = ifa->ifa_family; |
| 528 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); |
| 529 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 530 | return 0; |
| 531 | } |
| 532 | } |
| 533 | |
net[shemminger]!shemminger | 3eb1731 | 2005-02-07 18:28:31 +0000 | [diff] [blame] | 534 | if (filter.family && filter.family != ifa->ifa_family) |
| 535 | return 0; |
| 536 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 537 | if (filter.flushb) { |
| 538 | struct nlmsghdr *fn; |
| 539 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
| 540 | if (flush_update()) |
| 541 | return -1; |
| 542 | } |
| 543 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
| 544 | memcpy(fn, n, n->nlmsg_len); |
| 545 | fn->nlmsg_type = RTM_DELADDR; |
| 546 | fn->nlmsg_flags = NLM_F_REQUEST; |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 547 | fn->nlmsg_seq = ++rth.seq; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 548 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
| 549 | filter.flushed++; |
| 550 | if (show_stats < 2) |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | if (n->nlmsg_type == RTM_DELADDR) |
| 555 | fprintf(fp, "Deleted "); |
| 556 | |
| 557 | if (filter.oneline || filter.flushb) |
| 558 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
| 559 | if (ifa->ifa_family == AF_INET) |
| 560 | fprintf(fp, " inet "); |
| 561 | else if (ifa->ifa_family == AF_INET6) |
| 562 | fprintf(fp, " inet6 "); |
| 563 | else if (ifa->ifa_family == AF_DECnet) |
| 564 | fprintf(fp, " dnet "); |
| 565 | else if (ifa->ifa_family == AF_IPX) |
| 566 | fprintf(fp, " ipx "); |
| 567 | else |
| 568 | fprintf(fp, " family %d ", ifa->ifa_family); |
| 569 | |
| 570 | if (rta_tb[IFA_LOCAL]) { |
| 571 | fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family, |
| 572 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), |
| 573 | RTA_DATA(rta_tb[IFA_LOCAL]), |
| 574 | abuf, sizeof(abuf))); |
| 575 | |
| 576 | if (rta_tb[IFA_ADDRESS] == NULL || |
| 577 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { |
| 578 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); |
| 579 | } else { |
| 580 | fprintf(fp, " peer %s/%d ", |
| 581 | rt_addr_n2a(ifa->ifa_family, |
| 582 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), |
| 583 | RTA_DATA(rta_tb[IFA_ADDRESS]), |
| 584 | abuf, sizeof(abuf)), |
| 585 | ifa->ifa_prefixlen); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (rta_tb[IFA_BROADCAST]) { |
| 590 | fprintf(fp, "brd %s ", |
| 591 | rt_addr_n2a(ifa->ifa_family, |
| 592 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), |
| 593 | RTA_DATA(rta_tb[IFA_BROADCAST]), |
| 594 | abuf, sizeof(abuf))); |
| 595 | } |
| 596 | if (rta_tb[IFA_ANYCAST]) { |
| 597 | fprintf(fp, "any %s ", |
| 598 | rt_addr_n2a(ifa->ifa_family, |
| 599 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), |
| 600 | RTA_DATA(rta_tb[IFA_ANYCAST]), |
| 601 | abuf, sizeof(abuf))); |
| 602 | } |
| 603 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 604 | ifa_flags = ifa->ifa_flags; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 605 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 606 | ifa_flags &= ~IFA_F_SECONDARY; |
Brian Haley | a1b9ffc | 2009-09-14 17:01:43 -0400 | [diff] [blame] | 607 | if (ifa->ifa_family == AF_INET6) |
| 608 | fprintf(fp, "temporary "); |
| 609 | else |
| 610 | fprintf(fp, "secondary "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 611 | } |
| 612 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 613 | ifa_flags &= ~IFA_F_TENTATIVE; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 614 | fprintf(fp, "tentative "); |
| 615 | } |
| 616 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 617 | ifa_flags &= ~IFA_F_DEPRECATED; |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 618 | deprecated = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 619 | fprintf(fp, "deprecated "); |
| 620 | } |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 621 | if (ifa->ifa_flags&IFA_F_HOMEADDRESS) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 622 | ifa_flags &= ~IFA_F_HOMEADDRESS; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 623 | fprintf(fp, "home "); |
| 624 | } |
| 625 | if (ifa->ifa_flags&IFA_F_NODAD) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 626 | ifa_flags &= ~IFA_F_NODAD; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 627 | fprintf(fp, "nodad "); |
| 628 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 629 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { |
| 630 | fprintf(fp, "dynamic "); |
| 631 | } else |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 632 | ifa_flags &= ~IFA_F_PERMANENT; |
Brian Haley | f4af851 | 2009-12-01 15:58:44 -0800 | [diff] [blame] | 633 | if (ifa->ifa_flags&IFA_F_DADFAILED) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 634 | ifa_flags &= ~IFA_F_DADFAILED; |
Brian Haley | f4af851 | 2009-12-01 15:58:44 -0800 | [diff] [blame] | 635 | fprintf(fp, "dadfailed "); |
| 636 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 637 | if (ifa_flags) |
| 638 | fprintf(fp, "flags %02x ", ifa_flags); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 639 | if (rta_tb[IFA_LABEL]) |
| 640 | fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL])); |
| 641 | if (rta_tb[IFA_CACHEINFO]) { |
| 642 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 643 | fprintf(fp, "%s", _SL_); |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 644 | fprintf(fp, " valid_lft "); |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 645 | if (ci->ifa_valid == INFINITY_LIFE_TIME) |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 646 | fprintf(fp, "forever"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 647 | else |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 648 | fprintf(fp, "%usec", ci->ifa_valid); |
| 649 | fprintf(fp, " preferred_lft "); |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 650 | if (ci->ifa_prefered == INFINITY_LIFE_TIME) |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 651 | fprintf(fp, "forever"); |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 652 | else { |
| 653 | if (deprecated) |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 654 | fprintf(fp, "%dsec", ci->ifa_prefered); |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 655 | else |
Andreas Schwab | f66efad | 2010-11-05 23:26:29 +0000 | [diff] [blame] | 656 | fprintf(fp, "%usec", ci->ifa_prefered); |
Benedikt Gollatz | 037d950 | 2009-01-06 19:36:56 -0800 | [diff] [blame] | 657 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 658 | } |
| 659 | fprintf(fp, "\n"); |
| 660 | fflush(fp); |
| 661 | return 0; |
| 662 | } |
| 663 | |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 664 | int print_addrinfo_primary(const struct sockaddr_nl *who, struct nlmsghdr *n, |
| 665 | void *arg) |
| 666 | { |
| 667 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 668 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 669 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 670 | return 0; |
| 671 | |
| 672 | return print_addrinfo(who, n, arg); |
| 673 | } |
| 674 | |
| 675 | int print_addrinfo_secondary(const struct sockaddr_nl *who, struct nlmsghdr *n, |
| 676 | void *arg) |
| 677 | { |
| 678 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 679 | |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 680 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 681 | return 0; |
| 682 | |
| 683 | return print_addrinfo(who, n, arg); |
| 684 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 685 | |
| 686 | struct nlmsg_list |
| 687 | { |
| 688 | struct nlmsg_list *next; |
| 689 | struct nlmsghdr h; |
| 690 | }; |
| 691 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 692 | 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] | 693 | { |
| 694 | for ( ;ainfo ; ainfo = ainfo->next) { |
| 695 | struct nlmsghdr *n = &ainfo->h; |
| 696 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 697 | |
| 698 | if (n->nlmsg_type != RTM_NEWADDR) |
| 699 | continue; |
| 700 | |
| 701 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) |
| 702 | return -1; |
| 703 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 704 | if (ifa->ifa_index != ifindex || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 705 | (filter.family && filter.family != ifa->ifa_family)) |
| 706 | continue; |
| 707 | |
| 708 | print_addrinfo(NULL, n, fp); |
| 709 | } |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 714 | 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] | 715 | void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 716 | { |
| 717 | struct nlmsg_list **linfo = (struct nlmsg_list**)arg; |
| 718 | struct nlmsg_list *h; |
| 719 | struct nlmsg_list **lp; |
| 720 | |
| 721 | h = malloc(n->nlmsg_len+sizeof(void*)); |
| 722 | if (h == NULL) |
| 723 | return -1; |
| 724 | |
| 725 | memcpy(&h->h, n, n->nlmsg_len); |
| 726 | h->next = NULL; |
| 727 | |
| 728 | for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */; |
| 729 | *lp = h; |
| 730 | |
| 731 | ll_remember_index(who, n, NULL); |
| 732 | return 0; |
| 733 | } |
| 734 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 735 | 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] | 736 | { |
| 737 | struct nlmsg_list *linfo = NULL; |
| 738 | struct nlmsg_list *ainfo = NULL; |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 739 | struct nlmsg_list *l, *n; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 740 | char *filter_dev = NULL; |
| 741 | int no_link = 0; |
| 742 | |
| 743 | ipaddr_reset_filter(oneline); |
| 744 | filter.showqueue = 1; |
| 745 | |
| 746 | if (filter.family == AF_UNSPEC) |
| 747 | filter.family = preferred_family; |
| 748 | |
Stephen Hemminger | 242b8da | 2011-04-12 14:40:14 -0700 | [diff] [blame] | 749 | filter.group = INIT_NETDEV_GROUP; |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 750 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 751 | if (flush) { |
| 752 | if (argc <= 0) { |
| 753 | fprintf(stderr, "Flush requires arguments.\n"); |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 754 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 755 | return -1; |
| 756 | } |
| 757 | if (filter.family == AF_PACKET) { |
| 758 | fprintf(stderr, "Cannot flush link addresses.\n"); |
| 759 | return -1; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | while (argc > 0) { |
| 764 | if (strcmp(*argv, "to") == 0) { |
| 765 | NEXT_ARG(); |
| 766 | get_prefix(&filter.pfx, *argv, filter.family); |
| 767 | if (filter.family == AF_UNSPEC) |
| 768 | filter.family = filter.pfx.family; |
| 769 | } else if (strcmp(*argv, "scope") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 770 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 771 | NEXT_ARG(); |
| 772 | filter.scopemask = -1; |
| 773 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 774 | if (strcmp(*argv, "all") != 0) |
| 775 | invarg("invalid \"scope\"\n", *argv); |
| 776 | scope = RT_SCOPE_NOWHERE; |
| 777 | filter.scopemask = 0; |
| 778 | } |
| 779 | filter.scope = scope; |
| 780 | } else if (strcmp(*argv, "up") == 0) { |
| 781 | filter.up = 1; |
| 782 | } else if (strcmp(*argv, "dynamic") == 0) { |
| 783 | filter.flags &= ~IFA_F_PERMANENT; |
| 784 | filter.flagmask |= IFA_F_PERMANENT; |
| 785 | } else if (strcmp(*argv, "permanent") == 0) { |
| 786 | filter.flags |= IFA_F_PERMANENT; |
| 787 | filter.flagmask |= IFA_F_PERMANENT; |
Brian Haley | a1b9ffc | 2009-09-14 17:01:43 -0400 | [diff] [blame] | 788 | } else if (strcmp(*argv, "secondary") == 0 || |
| 789 | strcmp(*argv, "temporary") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 790 | filter.flags |= IFA_F_SECONDARY; |
| 791 | filter.flagmask |= IFA_F_SECONDARY; |
| 792 | } else if (strcmp(*argv, "primary") == 0) { |
| 793 | filter.flags &= ~IFA_F_SECONDARY; |
| 794 | filter.flagmask |= IFA_F_SECONDARY; |
| 795 | } else if (strcmp(*argv, "tentative") == 0) { |
| 796 | filter.flags |= IFA_F_TENTATIVE; |
| 797 | filter.flagmask |= IFA_F_TENTATIVE; |
| 798 | } else if (strcmp(*argv, "deprecated") == 0) { |
| 799 | filter.flags |= IFA_F_DEPRECATED; |
| 800 | filter.flagmask |= IFA_F_DEPRECATED; |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 801 | } else if (strcmp(*argv, "home") == 0) { |
| 802 | filter.flags |= IFA_F_HOMEADDRESS; |
| 803 | filter.flagmask |= IFA_F_HOMEADDRESS; |
| 804 | } else if (strcmp(*argv, "nodad") == 0) { |
| 805 | filter.flags |= IFA_F_NODAD; |
| 806 | filter.flagmask |= IFA_F_NODAD; |
Brian Haley | a1f2779 | 2009-12-03 10:39:36 +0000 | [diff] [blame] | 807 | } else if (strcmp(*argv, "dadfailed") == 0) { |
| 808 | filter.flags |= IFA_F_DADFAILED; |
| 809 | filter.flagmask |= IFA_F_DADFAILED; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 810 | } else if (strcmp(*argv, "label") == 0) { |
| 811 | NEXT_ARG(); |
| 812 | filter.label = *argv; |
Vlad Dogaru | f960c92 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 813 | } else if (strcmp(*argv, "group") == 0) { |
| 814 | NEXT_ARG(); |
| 815 | if (rtnl_group_a2n(&filter.group, *argv)) |
| 816 | invarg("Invalid \"group\" value\n", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 817 | } else { |
| 818 | if (strcmp(*argv, "dev") == 0) { |
| 819 | NEXT_ARG(); |
| 820 | } |
| 821 | if (matches(*argv, "help") == 0) |
| 822 | usage(); |
| 823 | if (filter_dev) |
| 824 | duparg2("dev", *argv); |
| 825 | filter_dev = *argv; |
| 826 | } |
| 827 | argv++; argc--; |
| 828 | } |
| 829 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 830 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { |
| 831 | perror("Cannot send dump request"); |
| 832 | exit(1); |
| 833 | } |
| 834 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 835 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 836 | fprintf(stderr, "Dump terminated\n"); |
| 837 | exit(1); |
| 838 | } |
| 839 | |
| 840 | if (filter_dev) { |
| 841 | filter.ifindex = ll_name_to_index(filter_dev); |
| 842 | if (filter.ifindex <= 0) { |
| 843 | fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev); |
| 844 | return -1; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | if (flush) { |
| 849 | int round = 0; |
| 850 | char flushb[4096-512]; |
| 851 | |
| 852 | filter.flushb = flushb; |
| 853 | filter.flushp = 0; |
| 854 | filter.flushe = sizeof(flushb); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 855 | |
Ben Greear | 64c7956 | 2010-12-01 11:13:51 -0800 | [diff] [blame] | 856 | while ((max_flush_loops == 0) || (round < max_flush_loops)) { |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 857 | const struct rtnl_dump_filter_arg a[3] = { |
| 858 | { |
| 859 | .filter = print_addrinfo_secondary, |
| 860 | .arg1 = stdout, |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 861 | }, |
| 862 | { |
| 863 | .filter = print_addrinfo_primary, |
| 864 | .arg1 = stdout, |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 865 | }, |
| 866 | { |
| 867 | .filter = NULL, |
| 868 | .arg1 = NULL, |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 869 | }, |
| 870 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 871 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 872 | perror("Cannot send dump request"); |
| 873 | exit(1); |
| 874 | } |
| 875 | filter.flushed = 0; |
Simon Horman | b49240e | 2009-12-03 12:08:27 +1100 | [diff] [blame] | 876 | if (rtnl_dump_filter_l(&rth, a) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 877 | fprintf(stderr, "Flush terminated\n"); |
| 878 | exit(1); |
| 879 | } |
| 880 | if (filter.flushed == 0) { |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 881 | flush_done: |
Andreas Henriksson | f0b34d2 | 2008-08-29 19:52:48 +0200 | [diff] [blame] | 882 | if (show_stats) { |
| 883 | if (round == 0) |
| 884 | printf("Nothing to flush.\n"); |
| 885 | else |
| 886 | printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); |
| 887 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 888 | fflush(stdout); |
| 889 | return 0; |
| 890 | } |
| 891 | round++; |
| 892 | if (flush_update() < 0) |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 893 | return 1; |
| 894 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 895 | if (show_stats) { |
| 896 | printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed); |
| 897 | fflush(stdout); |
| 898 | } |
Ben Greear | 3bc1c4f | 2010-08-16 10:00:08 -0700 | [diff] [blame] | 899 | |
| 900 | /* If we are flushing, and specifying primary, then we |
| 901 | * want to flush only a single round. Otherwise, we'll |
| 902 | * start flushing secondaries that were promoted to |
| 903 | * primaries. |
| 904 | */ |
| 905 | if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY)) |
| 906 | goto flush_done; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 907 | } |
Ben Greear | 64c7956 | 2010-12-01 11:13:51 -0800 | [diff] [blame] | 908 | fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops); |
| 909 | fflush(stderr); |
Daniel Silverstone | 7b3d366 | 2007-10-19 13:32:24 +0200 | [diff] [blame] | 910 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | if (filter.family != AF_PACKET) { |
| 914 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 915 | perror("Cannot send dump request"); |
| 916 | exit(1); |
| 917 | } |
| 918 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 919 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 920 | fprintf(stderr, "Dump terminated\n"); |
| 921 | exit(1); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | |
| 926 | if (filter.family && filter.family != AF_PACKET) { |
| 927 | struct nlmsg_list **lp; |
| 928 | lp=&linfo; |
| 929 | |
| 930 | if (filter.oneline) |
| 931 | no_link = 1; |
| 932 | |
| 933 | while ((l=*lp)!=NULL) { |
| 934 | int ok = 0; |
| 935 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 936 | struct nlmsg_list *a; |
| 937 | |
| 938 | for (a=ainfo; a; a=a->next) { |
| 939 | struct nlmsghdr *n = &a->h; |
| 940 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 941 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 942 | if (ifa->ifa_index != ifi->ifi_index || |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 943 | (filter.family && filter.family != ifa->ifa_family)) |
| 944 | continue; |
| 945 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 946 | continue; |
| 947 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 948 | continue; |
| 949 | if (filter.pfx.family || filter.label) { |
| 950 | struct rtattr *tb[IFA_MAX+1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 951 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); |
| 952 | if (!tb[IFA_LOCAL]) |
| 953 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; |
| 954 | |
| 955 | if (filter.pfx.family && tb[IFA_LOCAL]) { |
| 956 | inet_prefix dst; |
| 957 | memset(&dst, 0, sizeof(dst)); |
| 958 | dst.family = ifa->ifa_family; |
| 959 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); |
| 960 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 961 | continue; |
| 962 | } |
| 963 | if (filter.label) { |
| 964 | SPRINT_BUF(b1); |
| 965 | const char *label; |
| 966 | if (tb[IFA_LABEL]) |
| 967 | label = RTA_DATA(tb[IFA_LABEL]); |
| 968 | else |
| 969 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 970 | if (fnmatch(filter.label, label, 0) != 0) |
| 971 | continue; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | ok = 1; |
| 976 | break; |
| 977 | } |
| 978 | if (!ok) |
| 979 | *lp = l->next; |
| 980 | else |
| 981 | lp = &l->next; |
| 982 | } |
| 983 | } |
| 984 | |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 985 | for (l=linfo; l; l = n) { |
| 986 | n = l->next; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 987 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { |
| 988 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 989 | if (filter.family != AF_PACKET) |
| 990 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); |
| 991 | } |
| 992 | fflush(stdout); |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 993 | free(l); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 994 | } |
| 995 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 996 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | int ipaddr_list_link(int argc, char **argv) |
| 1000 | { |
| 1001 | preferred_family = AF_PACKET; |
| 1002 | do_link = 1; |
| 1003 | return ipaddr_list_or_flush(argc, argv, 0); |
| 1004 | } |
| 1005 | |
| 1006 | void ipaddr_reset_filter(int oneline) |
| 1007 | { |
| 1008 | memset(&filter, 0, sizeof(filter)); |
| 1009 | filter.oneline = oneline; |
| 1010 | } |
| 1011 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 1012 | static int default_scope(inet_prefix *lcl) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1013 | { |
| 1014 | if (lcl->family == AF_INET) { |
| 1015 | if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127) |
| 1016 | return RT_SCOPE_HOST; |
| 1017 | } |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
Stephen Hemminger | 3d866ba | 2008-03-14 15:30:03 -0700 | [diff] [blame] | 1021 | 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] | 1022 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1023 | struct { |
| 1024 | struct nlmsghdr n; |
| 1025 | struct ifaddrmsg ifa; |
| 1026 | char buf[256]; |
| 1027 | } req; |
| 1028 | char *d = NULL; |
| 1029 | char *l = NULL; |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 1030 | char *lcl_arg = NULL; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1031 | char *valid_lftp = NULL; |
| 1032 | char *preferred_lftp = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1033 | inet_prefix lcl; |
| 1034 | inet_prefix peer; |
| 1035 | int local_len = 0; |
| 1036 | int peer_len = 0; |
| 1037 | int brd_len = 0; |
| 1038 | int any_len = 0; |
| 1039 | int scoped = 0; |
Masahide NAKAMURA | 141bb60 | 2006-11-24 12:27:01 +0900 | [diff] [blame] | 1040 | __u32 preferred_lft = INFINITY_LIFE_TIME; |
| 1041 | __u32 valid_lft = INFINITY_LIFE_TIME; |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1042 | struct ifa_cacheinfo cinfo; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1043 | |
| 1044 | memset(&req, 0, sizeof(req)); |
| 1045 | |
| 1046 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1047 | req.n.nlmsg_flags = NLM_F_REQUEST | flags; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1048 | req.n.nlmsg_type = cmd; |
| 1049 | req.ifa.ifa_family = preferred_family; |
| 1050 | |
| 1051 | while (argc > 0) { |
| 1052 | if (strcmp(*argv, "peer") == 0 || |
| 1053 | strcmp(*argv, "remote") == 0) { |
| 1054 | NEXT_ARG(); |
| 1055 | |
| 1056 | if (peer_len) |
| 1057 | duparg("peer", *argv); |
| 1058 | get_prefix(&peer, *argv, req.ifa.ifa_family); |
| 1059 | peer_len = peer.bytelen; |
| 1060 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1061 | req.ifa.ifa_family = peer.family; |
| 1062 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); |
| 1063 | req.ifa.ifa_prefixlen = peer.bitlen; |
| 1064 | } else if (matches(*argv, "broadcast") == 0 || |
| 1065 | strcmp(*argv, "brd") == 0) { |
| 1066 | inet_prefix addr; |
| 1067 | NEXT_ARG(); |
| 1068 | if (brd_len) |
| 1069 | duparg("broadcast", *argv); |
| 1070 | if (strcmp(*argv, "+") == 0) |
| 1071 | brd_len = -1; |
| 1072 | else if (strcmp(*argv, "-") == 0) |
| 1073 | brd_len = -2; |
| 1074 | else { |
| 1075 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 1076 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1077 | req.ifa.ifa_family = addr.family; |
| 1078 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); |
| 1079 | brd_len = addr.bytelen; |
| 1080 | } |
| 1081 | } else if (strcmp(*argv, "anycast") == 0) { |
| 1082 | inet_prefix addr; |
| 1083 | NEXT_ARG(); |
| 1084 | if (any_len) |
| 1085 | duparg("anycast", *argv); |
| 1086 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 1087 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1088 | req.ifa.ifa_family = addr.family; |
| 1089 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); |
| 1090 | any_len = addr.bytelen; |
| 1091 | } else if (strcmp(*argv, "scope") == 0) { |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 1092 | unsigned scope = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1093 | NEXT_ARG(); |
| 1094 | if (rtnl_rtscope_a2n(&scope, *argv)) |
| 1095 | invarg(*argv, "invalid scope value."); |
| 1096 | req.ifa.ifa_scope = scope; |
| 1097 | scoped = 1; |
| 1098 | } else if (strcmp(*argv, "dev") == 0) { |
| 1099 | NEXT_ARG(); |
| 1100 | d = *argv; |
| 1101 | } else if (strcmp(*argv, "label") == 0) { |
| 1102 | NEXT_ARG(); |
| 1103 | l = *argv; |
| 1104 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1105 | } else if (matches(*argv, "valid_lft") == 0) { |
| 1106 | if (valid_lftp) |
| 1107 | duparg("valid_lft", *argv); |
| 1108 | NEXT_ARG(); |
| 1109 | valid_lftp = *argv; |
| 1110 | if (set_lifetime(&valid_lft, *argv)) |
| 1111 | invarg("valid_lft value", *argv); |
| 1112 | } else if (matches(*argv, "preferred_lft") == 0) { |
| 1113 | if (preferred_lftp) |
| 1114 | duparg("preferred_lft", *argv); |
| 1115 | NEXT_ARG(); |
| 1116 | preferred_lftp = *argv; |
| 1117 | if (set_lifetime(&preferred_lft, *argv)) |
| 1118 | invarg("preferred_lft value", *argv); |
Noriaki TAKAMIYA | bac735c | 2007-03-08 03:15:43 +0900 | [diff] [blame] | 1119 | } else if (strcmp(*argv, "home") == 0) { |
| 1120 | req.ifa.ifa_flags |= IFA_F_HOMEADDRESS; |
| 1121 | } else if (strcmp(*argv, "nodad") == 0) { |
| 1122 | req.ifa.ifa_flags |= IFA_F_NODAD; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1123 | } else { |
| 1124 | if (strcmp(*argv, "local") == 0) { |
| 1125 | NEXT_ARG(); |
| 1126 | } |
| 1127 | if (matches(*argv, "help") == 0) |
| 1128 | usage(); |
| 1129 | if (local_len) |
| 1130 | duparg2("local", *argv); |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 1131 | lcl_arg = *argv; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1132 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 1133 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 1134 | req.ifa.ifa_family = lcl.family; |
| 1135 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 1136 | local_len = lcl.bytelen; |
| 1137 | } |
| 1138 | argc--; argv++; |
| 1139 | } |
| 1140 | if (d == NULL) { |
| 1141 | fprintf(stderr, "Not enough information: \"dev\" argument is required.\n"); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | if (l && matches(d, l) != 0) { |
| 1145 | fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l); |
Michele Petrazzo - Unipex | 1db61e0 | 2010-03-06 08:56:53 +0000 | [diff] [blame] | 1146 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
net[shemminger]!shemminger | f082b64 | 2005-03-30 18:16:10 +0000 | [diff] [blame] | 1149 | if (peer_len == 0 && local_len) { |
| 1150 | if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) { |
| 1151 | fprintf(stderr, |
| 1152 | "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \ |
| 1153 | " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \ |
| 1154 | " This special behaviour is likely to disappear in further releases,\n" \ |
| 1155 | " fix your scripts!\n", lcl_arg, local_len*8); |
| 1156 | } else { |
| 1157 | peer = lcl; |
| 1158 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 1159 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1160 | } |
| 1161 | if (req.ifa.ifa_prefixlen == 0) |
| 1162 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 1163 | |
| 1164 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 1165 | inet_prefix brd; |
| 1166 | int i; |
| 1167 | if (req.ifa.ifa_family != AF_INET) { |
| 1168 | fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n"); |
| 1169 | return -1; |
| 1170 | } |
| 1171 | brd = peer; |
| 1172 | if (brd.bitlen <= 30) { |
| 1173 | for (i=31; i>=brd.bitlen; i--) { |
| 1174 | if (brd_len == -1) |
| 1175 | brd.data[0] |= htonl(1<<(31-i)); |
| 1176 | else |
| 1177 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 1178 | } |
| 1179 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 1180 | brd_len = brd.bytelen; |
| 1181 | } |
| 1182 | } |
| 1183 | if (!scoped && cmd != RTM_DELADDR) |
| 1184 | req.ifa.ifa_scope = default_scope(&lcl); |
| 1185 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1186 | ll_init_map(&rth); |
| 1187 | |
| 1188 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { |
| 1189 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | |
Masahide NAKAMURA | 35546df | 2006-11-24 12:26:55 +0900 | [diff] [blame] | 1193 | if (valid_lftp || preferred_lftp) { |
| 1194 | if (!valid_lft) { |
| 1195 | fprintf(stderr, "valid_lft is zero\n"); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | if (valid_lft < preferred_lft) { |
| 1199 | fprintf(stderr, "preferred_lft is greater than valid_lft\n"); |
| 1200 | return -1; |
| 1201 | } |
| 1202 | |
| 1203 | memset(&cinfo, 0, sizeof(cinfo)); |
| 1204 | cinfo.ifa_prefered = preferred_lft; |
| 1205 | cinfo.ifa_valid = valid_lft; |
| 1206 | addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo, |
| 1207 | sizeof(cinfo)); |
| 1208 | } |
| 1209 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 1210 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) |
Michele Petrazzo - Unipex | 1db61e0 | 2010-03-06 08:56:53 +0000 | [diff] [blame] | 1211 | return -2; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1212 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 1213 | return 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | int do_ipaddr(int argc, char **argv) |
| 1217 | { |
| 1218 | if (argc < 1) |
| 1219 | return ipaddr_list_or_flush(0, NULL, 0); |
| 1220 | if (matches(*argv, "add") == 0) |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1221 | return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); |
| 1222 | if (matches(*argv, "change") == 0 || |
| 1223 | strcmp(*argv, "chg") == 0) |
| 1224 | return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1); |
| 1225 | if (matches(*argv, "replace") == 0) |
| 1226 | 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] | 1227 | if (matches(*argv, "delete") == 0) |
Noriaki TAKAMIYA | 0aef366 | 2006-11-24 12:26:58 +0900 | [diff] [blame] | 1228 | return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1229 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 1230 | || matches(*argv, "lst") == 0) |
| 1231 | return ipaddr_list_or_flush(argc-1, argv+1, 0); |
| 1232 | if (matches(*argv, "flush") == 0) |
| 1233 | return ipaddr_list_or_flush(argc-1, argv+1, 1); |
| 1234 | if (matches(*argv, "help") == 0) |
| 1235 | usage(); |
Alexander Wirt | b096fa5 | 2007-10-12 10:56:36 +0200 | [diff] [blame] | 1236 | 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] | 1237 | exit(-1); |
| 1238 | } |
| 1239 | |