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