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