]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipaddress.c
tc: updated man page to reflect GET command to retrieve a single filter.
[mirror_iproute2.git] / ip / ipaddress.c
CommitLineData
aba5acdf
SH
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 *
aba5acdf
SH
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
e6e6fb5c 17#include <inttypes.h>
aba5acdf
SH
18#include <fcntl.h>
19#include <sys/ioctl.h>
20#include <sys/socket.h>
21#include <sys/ioctl.h>
110e84a0 22#include <sys/param.h>
5bd9dd49 23#include <errno.h>
aba5acdf
SH
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <string.h>
27#include <fnmatch.h>
28
e5779fb2
SH
29#include <linux/netdevice.h>
30#include <linux/if_arp.h>
31#include <linux/sockios.h>
ccdcbf35 32#include <linux/net_namespace.h>
e5779fb2 33
aba5acdf
SH
34#include "rt_names.h"
35#include "utils.h"
36#include "ll_map.h"
37#include "ip_common.h"
d7bd2db5 38#include "color.h"
aba5acdf 39
81824ac2
PE
40enum {
41 IPADD_LIST,
42 IPADD_FLUSH,
43 IPADD_SAVE,
44};
7b3d366e 45
aba5acdf
SH
46static struct
47{
48 int ifindex;
49 int family;
50 int oneline;
51 int showqueue;
52 inet_prefix pfx;
53 int scope, scopemask;
54 int flags, flagmask;
55 int up;
56 char *label;
57 int flushed;
58 char *flushb;
59 int flushp;
60 int flushe;
f960c92a 61 int group;
1b944148 62 int master;
712249d8 63 char *kind;
e0513807 64 char *slave_kind;
aba5acdf
SH
65} filter;
66
67static int do_link;
68
69static void usage(void) __attribute__((noreturn));
70
71static void usage(void)
72{
73 if (do_link) {
74 iplink_usage();
75 }
4e972d5e 76 fprintf(stderr, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
a1f27794 77 fprintf(stderr, " [ CONFFLAG-LIST ]\n");
4e972d5e 78 fprintf(stderr, " ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
24604eb2 79 fprintf(stderr, " ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
4e972d5e 80 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
24604eb2
PS
81 fprintf(stderr, " ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n");
82 fprintf(stderr, " [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n");
104444c2 83 fprintf(stderr, " [ label LABEL ] [up] [ vrf NAME ] ]\n");
4e972d5e 84 fprintf(stderr, " ip address {showdump|restore}\n");
aba5acdf
SH
85 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
86 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
4e972d5e 87 fprintf(stderr, " [ label IFNAME ] [ scope SCOPE-ID ]\n");
aba5acdf
SH
88 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
89 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
90 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
b5f39b25 91 fprintf(stderr, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
a1f27794 92 fprintf(stderr, " CONFFLAG-LIST ]\n");
bac735c5 93 fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
e31867ac 94 fprintf(stderr, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
35546df7
MN
95 fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
96 fprintf(stderr, "LFT := forever | SECONDS\n");
24604eb2 97 fprintf(stderr, "TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | macvtap |\n");
577cfe0b
PS
98 fprintf(stderr, " bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |\n");
99 fprintf(stderr, " gre | gretap | ip6gre | ip6gretap | vti | nlmon | can |\n");
fd4df5b2 100 fprintf(stderr, " bond_slave | ipvlan | geneve | bridge_slave | vrf | hsr | macsec }\n");
35546df7 101
aba5acdf
SH
102 exit(-1);
103}
104
56f5daac 105static void print_link_flags(FILE *fp, unsigned int flags, unsigned int mdown)
aba5acdf
SH
106{
107 fprintf(fp, "<");
73b49e9f 108 if (flags & IFF_UP && !(flags & IFF_RUNNING))
109 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
aba5acdf
SH
110 flags &= ~IFF_RUNNING;
111#define _PF(f) if (flags&IFF_##f) { \
1124ffb7
SH
112 flags &= ~IFF_##f ; \
113 fprintf(fp, #f "%s", flags ? "," : ""); }
aba5acdf
SH
114 _PF(LOOPBACK);
115 _PF(BROADCAST);
116 _PF(POINTOPOINT);
117 _PF(MULTICAST);
118 _PF(NOARP);
119 _PF(ALLMULTI);
120 _PF(PROMISC);
121 _PF(MASTER);
122 _PF(SLAVE);
123 _PF(DEBUG);
124 _PF(DYNAMIC);
125 _PF(AUTOMEDIA);
126 _PF(PORTSEL);
127 _PF(NOTRAILERS);
128 _PF(UP);
dcb283c3
TG
129 _PF(LOWER_UP);
130 _PF(DORMANT);
98f9a1d2 131 _PF(ECHO);
aba5acdf 132#undef _PF
1124ffb7 133 if (flags)
aba5acdf
SH
134 fprintf(fp, "%x", flags);
135 if (mdown)
136 fprintf(fp, ",M-DOWN");
137 fprintf(fp, "> ");
138}
139
3d866ba2 140static const char *oper_states[] = {
0612519e 141 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
3d866ba2
SH
142 "TESTING", "DORMANT", "UP"
143};
144
145static void print_operstate(FILE *f, __u8 state)
146{
a1121aa1 147 if (state >= ARRAY_SIZE(oper_states)) {
3d866ba2 148 fprintf(f, "state %#x ", state);
a1121aa1
PS
149 } else if (brief) {
150 color_fprintf(f, oper_state_color(state),
468fa020 151 "%-14s ", oper_states[state]);
a1121aa1
PS
152 } else {
153 fprintf(f, "state ");
154 color_fprintf(f, oper_state_color(state),
468fa020 155 "%s ", oper_states[state]);
d7bd2db5 156 }
3d866ba2
SH
157}
158
4f2fdd44
SH
159int get_operstate(const char *name)
160{
161 int i;
162
56f5daac 163 for (i = 0; i < ARRAY_SIZE(oper_states); i++)
4f2fdd44
SH
164 if (strcasecmp(name, oper_states[i]) == 0)
165 return i;
166 return -1;
167}
168
f78e316f 169static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
aba5acdf 170{
f78e316f
ED
171 int qlen;
172
173 if (tb[IFLA_TXQLEN])
174 qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
175 else {
d17b136f 176 struct ifreq ifr = {};
f78e316f
ED
177 int s = socket(AF_INET, SOCK_STREAM, 0);
178
179 if (s < 0)
180 return;
181
ff24746c 182 strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME]));
f78e316f 183 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
d3603518 184 fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno));
f78e316f
ED
185 close(s);
186 return;
187 }
aba5acdf 188 close(s);
f78e316f 189 qlen = ifr.ifr_qlen;
aba5acdf 190 }
f78e316f
ED
191 if (qlen)
192 fprintf(f, "qlen %d", qlen);
aba5acdf
SH
193}
194
82499282
SH
195static const char *link_modes[] = {
196 "DEFAULT", "DORMANT"
197};
198
199static void print_linkmode(FILE *f, struct rtattr *tb)
200{
201 unsigned int mode = rta_getattr_u8(tb);
202
56f5daac 203 if (mode >= ARRAY_SIZE(link_modes))
82499282
SH
204 fprintf(f, "mode %d ", mode);
205 else
206 fprintf(f, "mode %s ", link_modes[mode]);
207}
208
e0513807 209static char *parse_link_kind(struct rtattr *tb, bool slave)
712249d8
VK
210{
211 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
e0513807 212 int attr = slave ? IFLA_INFO_SLAVE_KIND : IFLA_INFO_KIND;
712249d8
VK
213
214 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
215
e0513807
PS
216 if (linkinfo[attr])
217 return RTA_DATA(linkinfo[attr]);
712249d8
VK
218
219 return "";
220}
221
03ac85b7 222static int match_link_kind(struct rtattr **tb, const char *kind, bool slave)
e0513807
PS
223{
224 if (!tb[IFLA_LINKINFO])
225 return -1;
226
227 return strcmp(parse_link_kind(tb[IFLA_LINKINFO], slave), kind);
228}
229
1d934839
PM
230static void print_linktype(FILE *fp, struct rtattr *tb)
231{
232 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
233 struct link_util *lu;
fbea6115 234 struct link_util *slave_lu;
22a84711 235 char slave[32];
1d934839 236 char *kind;
fbea6115 237 char *slave_kind;
1d934839
PM
238
239 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
240
fbea6115
JP
241 if (linkinfo[IFLA_INFO_KIND]) {
242 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1d934839 243
fbea6115
JP
244 fprintf(fp, "%s", _SL_);
245 fprintf(fp, " %s ", kind);
1d934839 246
fbea6115
JP
247 lu = get_link_kind(kind);
248 if (lu && lu->print_opt) {
249 struct rtattr *attr[lu->maxattr+1], **data = NULL;
1d934839 250
fbea6115
JP
251 if (linkinfo[IFLA_INFO_DATA]) {
252 parse_rtattr_nested(attr, lu->maxattr,
253 linkinfo[IFLA_INFO_DATA]);
254 data = attr;
255 }
256 lu->print_opt(lu, fp, data);
1d934839 257
fbea6115
JP
258 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
259 lu->print_xstats)
260 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
1d934839 261 }
fbea6115
JP
262 }
263
264 if (linkinfo[IFLA_INFO_SLAVE_KIND]) {
265 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1d934839 266
fbea6115
JP
267 fprintf(fp, "%s", _SL_);
268 fprintf(fp, " %s_slave ", slave_kind);
22a84711 269 snprintf(slave, sizeof(slave), "%s_slave", slave_kind);
fbea6115 270
22a84711 271 slave_lu = get_link_kind(slave);
fbea6115
JP
272 if (slave_lu && slave_lu->print_opt) {
273 struct rtattr *attr[slave_lu->maxattr+1], **data = NULL;
274
275 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
276 parse_rtattr_nested(attr, slave_lu->maxattr,
277 linkinfo[IFLA_INFO_SLAVE_DATA]);
278 data = attr;
279 }
280 slave_lu->print_opt(slave_lu, fp, data);
281 }
1d934839
PM
282 }
283}
284
ee0067a9
JP
285static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr)
286{
287 struct rtattr *inet6_attr;
288 struct rtattr *tb[IFLA_INET6_MAX + 1];
289
290 inet6_attr = parse_rtattr_one_nested(AF_INET6, af_spec_attr);
291 if (!inet6_attr)
292 return;
293
294 parse_rtattr_nested(tb, IFLA_INET6_MAX, inet6_attr);
295
296 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5c5176ce 297 __u8 mode = rta_getattr_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
56f5daac 298
5c5176ce 299 switch (mode) {
ee0067a9
JP
300 case IN6_ADDR_GEN_MODE_EUI64:
301 fprintf(fp, "addrgenmode eui64 ");
302 break;
303 case IN6_ADDR_GEN_MODE_NONE:
304 fprintf(fp, "addrgenmode none ");
305 break;
5c5176ce
HFS
306 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
307 fprintf(fp, "addrgenmode stable_secret ");
308 break;
8e12bc0a
BM
309 case IN6_ADDR_GEN_MODE_RANDOM:
310 fprintf(fp, "addrgenmode random ");
311 break;
5c5176ce
HFS
312 default:
313 fprintf(fp, "addrgenmode %#.2hhx ", mode);
314 break;
ee0067a9
JP
315 }
316 }
317}
318
a1b99717
EBE
319static void print_vf_stats64(FILE *fp, struct rtattr *vfstats);
320
3fd86630
CW
321static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
322{
323 struct ifla_vf_mac *vf_mac;
3fd86630 324 struct ifla_vf_tx_rate *vf_tx_rate;
8c29ae7c 325 struct rtattr *vf[IFLA_VF_MAX + 1] = {};
56f5daac 326
3fd86630
CW
327 SPRINT_BUF(b1);
328
329 if (vfinfo->rta_type != IFLA_VF_INFO) {
330 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
331 return;
332 }
333
334 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
335
336 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
3fd86630
CW
337 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
338
6f07f3dc 339 fprintf(fp, "%s vf %d MAC %s", _SL_, vf_mac->vf,
3fd86630 340 ll_addr_n2a((unsigned char *)&vf_mac->mac,
bbac6c63 341 ETH_ALEN, 0, b1, sizeof(b1)));
56e9f0ab
MS
342 if (vf[IFLA_VF_VLAN_LIST]) {
343 struct rtattr *i, *vfvlanlist = vf[IFLA_VF_VLAN_LIST];
344 int rem = RTA_PAYLOAD(vfvlanlist);
345
346 for (i = RTA_DATA(vfvlanlist);
347 RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
348 struct ifla_vf_vlan_info *vf_vlan_info =
349 RTA_DATA(i);
350 SPRINT_BUF(b2);
351
352 if (vf_vlan_info->vlan)
353 fprintf(fp, ", vlan %d", vf_vlan_info->vlan);
354 if (vf_vlan_info->qos)
355 fprintf(fp, ", qos %d", vf_vlan_info->qos);
356 if (vf_vlan_info->vlan_proto &&
357 vf_vlan_info->vlan_proto != htons(ETH_P_8021Q))
358 fprintf(fp, ", vlan protocol %s",
359 ll_proto_n2a(vf_vlan_info->vlan_proto,
360 b2, sizeof(b2)));
361
362 }
363 } else {
364 struct ifla_vf_vlan *vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
365
366 if (vf_vlan->vlan)
367 fprintf(fp, ", vlan %d", vf_vlan->vlan);
368 if (vf_vlan->qos)
369 fprintf(fp, ", qos %d", vf_vlan->qos);
370 }
3fd86630
CW
371 if (vf_tx_rate->rate)
372 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
8c29ae7c
VK
373
374 if (vf[IFLA_VF_RATE]) {
375 struct ifla_vf_rate *vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
376
377 if (vf_rate->max_tx_rate)
378 fprintf(fp, ", max_tx_rate %dMbps", vf_rate->max_tx_rate);
379 if (vf_rate->min_tx_rate)
380 fprintf(fp, ", min_tx_rate %dMbps", vf_rate->min_tx_rate);
381 }
ff9463e0
PS
382 if (vf[IFLA_VF_SPOOFCHK]) {
383 struct ifla_vf_spoofchk *vf_spoofchk =
384 RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
8c29ae7c 385
ff9463e0
PS
386 if (vf_spoofchk->setting != -1)
387 fprintf(fp, ", spoof checking %s",
468fa020 388 vf_spoofchk->setting ? "on" : "off");
7b8179c7 389 }
ff9463e0
PS
390 if (vf[IFLA_VF_LINK_STATE]) {
391 struct ifla_vf_link_state *vf_linkstate =
392 RTA_DATA(vf[IFLA_VF_LINK_STATE]);
393
07fa9c15
RE
394 if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_AUTO)
395 fprintf(fp, ", link-state auto");
396 else if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_ENABLE)
397 fprintf(fp, ", link-state enable");
398 else
399 fprintf(fp, ", link-state disable");
400 }
fe932278
PS
401 if (vf[IFLA_VF_TRUST]) {
402 struct ifla_vf_trust *vf_trust = RTA_DATA(vf[IFLA_VF_TRUST]);
403
404 if (vf_trust->setting != -1)
405 fprintf(fp, ", trust %s",
468fa020 406 vf_trust->setting ? "on" : "off");
fe932278 407 }
a1b99717
EBE
408 if (vf[IFLA_VF_STATS] && show_stats)
409 print_vf_stats64(fp, vf[IFLA_VF_STATS]);
3fd86630
CW
410}
411
56f5daac 412static void print_num(FILE *fp, unsigned int width, uint64_t count)
b68d9837 413{
1e264abc
SH
414 const char *prefix = "kMGTPE";
415 const unsigned int base = use_iec ? 1024 : 1000;
b68d9837 416 uint64_t powi = 1;
50ec6650
CH
417 uint16_t powj = 1;
418 uint8_t precision = 2;
1e264abc 419 char buf[64];
b68d9837 420
1e264abc 421 if (!human_readable || count < base) {
f4fe81d2 422 fprintf(fp, "%-*"PRIu64" ", width, count);
1e264abc 423 return;
b68d9837
CH
424 }
425
1e264abc 426 /* increase value by a factor of 1000/1024 and print
468fa020
SH
427 * if result is something a human can read
428 */
56f5daac 429 for (;;) {
1e264abc
SH
430 powi *= base;
431 if (count / base < powi)
432 break;
b68d9837 433
1e264abc
SH
434 if (!prefix[1])
435 break;
436 ++prefix;
b68d9837
CH
437 }
438
50ec6650
CH
439 /* try to guess a good number of digits for precision */
440 for (; precision > 0; precision--) {
441 powj *= 10;
442 if (count / powi < powj)
443 break;
444 }
445
446 snprintf(buf, sizeof(buf), "%.*f%c%s", precision,
447 (double) count / powi, *prefix, use_iec ? "i" : "");
1e264abc 448
f4fe81d2 449 fprintf(fp, "%-*s ", width, buf);
b68d9837
CH
450}
451
a1b99717
EBE
452static void print_vf_stats64(FILE *fp, struct rtattr *vfstats)
453{
30a8842c 454 struct rtattr *vf[IFLA_VF_STATS_MAX + 1];
a1b99717
EBE
455
456 if (vfstats->rta_type != IFLA_VF_STATS) {
457 fprintf(stderr, "BUG: rta type is %d\n", vfstats->rta_type);
458 return;
459 }
460
461 parse_rtattr_nested(vf, IFLA_VF_MAX, vfstats);
462
463 /* RX stats */
464 fprintf(fp, "%s", _SL_);
465 fprintf(fp, " RX: bytes packets mcast bcast %s", _SL_);
466 fprintf(fp, " ");
467
468 print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_BYTES]));
469 print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_PACKETS]));
470 print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_MULTICAST]));
471 print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_BROADCAST]));
472
473 /* TX stats */
474 fprintf(fp, "%s", _SL_);
475 fprintf(fp, " TX: bytes packets %s", _SL_);
476 fprintf(fp, " ");
477
478 print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_BYTES]));
479 print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_PACKETS]));
480}
481
30b55792 482static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s,
56f5daac 483 const struct rtattr *carrier_changes)
4b726cb1 484{
b68d9837 485 /* RX stats */
e6e6fb5c
SH
486 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
487 s->rx_compressed ? "compressed" : "", _SL_);
1e264abc
SH
488
489 fprintf(fp, " ");
490 print_num(fp, 10, s->rx_bytes);
491 print_num(fp, 8, s->rx_packets);
492 print_num(fp, 7, s->rx_errors);
493 print_num(fp, 7, s->rx_dropped);
494 print_num(fp, 7, s->rx_over_errors);
495 print_num(fp, 7, s->multicast);
496 if (s->rx_compressed)
497 print_num(fp, 7, s->rx_compressed);
b68d9837
CH
498
499 /* RX error stats */
e6e6fb5c
SH
500 if (show_stats > 1) {
501 fprintf(fp, "%s", _SL_);
07ec1834
SH
502 fprintf(fp, " RX errors: length crc frame fifo missed%s%s",
503 s->rx_nohandler ? " nohandler" : "", _SL_);
1e264abc
SH
504
505 fprintf(fp, " ");
506 print_num(fp, 8, s->rx_length_errors);
507 print_num(fp, 7, s->rx_crc_errors);
508 print_num(fp, 7, s->rx_frame_errors);
509 print_num(fp, 7, s->rx_fifo_errors);
510 print_num(fp, 7, s->rx_missed_errors);
07ec1834
SH
511 if (s->rx_nohandler)
512 print_num(fp, 7, s->rx_nohandler);
513
e6e6fb5c
SH
514 }
515 fprintf(fp, "%s", _SL_);
b68d9837
CH
516
517 /* TX stats */
e6e6fb5c 518 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
1e264abc
SH
519 s->tx_compressed ? "compressed" : "", _SL_);
520
1e264abc
SH
521 fprintf(fp, " ");
522 print_num(fp, 10, s->tx_bytes);
523 print_num(fp, 8, s->tx_packets);
524 print_num(fp, 7, s->tx_errors);
525 print_num(fp, 7, s->tx_dropped);
526 print_num(fp, 7, s->tx_carrier_errors);
527 print_num(fp, 7, s->collisions);
528 if (s->tx_compressed)
529 print_num(fp, 7, s->tx_compressed);
b68d9837
CH
530
531 /* TX error stats */
e6e6fb5c
SH
532 if (show_stats > 1) {
533 fprintf(fp, "%s", _SL_);
b68d9837 534 fprintf(fp, " TX errors: aborted fifo window heartbeat");
56f5daac 535 if (carrier_changes)
30b55792 536 fprintf(fp, " transns");
4ec0ffde 537 fprintf(fp, "%s", _SL_);
1e264abc
SH
538
539 fprintf(fp, " ");
540 print_num(fp, 8, s->tx_aborted_errors);
541 print_num(fp, 7, s->tx_fifo_errors);
542 print_num(fp, 7, s->tx_window_errors);
543 print_num(fp, 7, s->tx_heartbeat_errors);
544 if (carrier_changes)
56f5daac 545 print_num(fp, 7, *(uint32_t *)RTA_DATA(carrier_changes));
e6e6fb5c
SH
546 }
547}
548
5d5cf1b4
JB
549static void print_link_stats32(FILE *fp, const struct rtnl_link_stats *s,
550 const struct rtattr *carrier_changes)
e6e6fb5c 551{
b68d9837 552 /* RX stats */
e6e6fb5c
SH
553 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
554 s->rx_compressed ? "compressed" : "", _SL_);
1e264abc
SH
555
556
557 fprintf(fp, " ");
558 print_num(fp, 10, s->rx_bytes);
559 print_num(fp, 8, s->rx_packets);
560 print_num(fp, 7, s->rx_errors);
561 print_num(fp, 7, s->rx_dropped);
562 print_num(fp, 7, s->rx_over_errors);
563 print_num(fp, 7, s->multicast);
564 if (s->rx_compressed)
565 print_num(fp, 7, s->rx_compressed);
b68d9837
CH
566
567 /* RX error stats */
e6e6fb5c
SH
568 if (show_stats > 1) {
569 fprintf(fp, "%s", _SL_);
07ec1834
SH
570 fprintf(fp, " RX errors: length crc frame fifo missed%s%s",
571 s->rx_nohandler ? " nohandler" : "", _SL_);
1e264abc
SH
572 fprintf(fp, " ");
573 print_num(fp, 8, s->rx_length_errors);
574 print_num(fp, 7, s->rx_crc_errors);
575 print_num(fp, 7, s->rx_frame_errors);
576 print_num(fp, 7, s->rx_fifo_errors);
577 print_num(fp, 7, s->rx_missed_errors);
07ec1834
SH
578 if (s->rx_nohandler)
579 print_num(fp, 7, s->rx_nohandler);
e6e6fb5c
SH
580 }
581 fprintf(fp, "%s", _SL_);
b68d9837
CH
582
583 /* TX stats */
e6e6fb5c
SH
584 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
585 s->tx_compressed ? "compressed" : "", _SL_);
1e264abc
SH
586
587 fprintf(fp, " ");
588 print_num(fp, 10, s->tx_bytes);
589 print_num(fp, 8, s->tx_packets);
590 print_num(fp, 7, s->tx_errors);
591 print_num(fp, 7, s->tx_dropped);
592 print_num(fp, 7, s->tx_carrier_errors);
593 print_num(fp, 7, s->collisions);
594 if (s->tx_compressed)
595 print_num(fp, 7, s->tx_compressed);
b68d9837
CH
596
597 /* TX error stats */
e6e6fb5c
SH
598 if (show_stats > 1) {
599 fprintf(fp, "%s", _SL_);
b68d9837 600 fprintf(fp, " TX errors: aborted fifo window heartbeat");
56f5daac 601 if (carrier_changes)
30b55792 602 fprintf(fp, " transns");
4ec0ffde 603 fprintf(fp, "%s", _SL_);
1e264abc
SH
604
605 fprintf(fp, " ");
606 print_num(fp, 8, s->tx_aborted_errors);
607 print_num(fp, 7, s->tx_fifo_errors);
608 print_num(fp, 7, s->tx_window_errors);
609 print_num(fp, 7, s->tx_heartbeat_errors);
610 if (carrier_changes)
56f5daac 611 print_num(fp, 7, *(uint32_t *)RTA_DATA(carrier_changes));
e6e6fb5c
SH
612 }
613}
614
5d5cf1b4
JB
615static void __print_link_stats(FILE *fp, struct rtattr **tb)
616{
07ec1834
SH
617 const struct rtattr *carrier_changes = tb[IFLA_CARRIER_CHANGES];
618
619 if (tb[IFLA_STATS64]) {
620 struct rtnl_link_stats64 stats = { 0 };
621
622 memcpy(&stats, RTA_DATA(tb[IFLA_STATS64]),
623 MIN(RTA_PAYLOAD(tb[IFLA_STATS64]), sizeof(stats)));
624
625 print_link_stats64(fp, &stats, carrier_changes);
626 } else if (tb[IFLA_STATS]) {
627 struct rtnl_link_stats stats = { 0 };
628
629 memcpy(&stats, RTA_DATA(tb[IFLA_STATS]),
630 MIN(RTA_PAYLOAD(tb[IFLA_STATS]), sizeof(stats)));
631
632 print_link_stats32(fp, &stats, carrier_changes);
633 }
5d5cf1b4
JB
634}
635
636static void print_link_stats(FILE *fp, struct nlmsghdr *n)
637{
638 struct ifinfomsg *ifi = NLMSG_DATA(n);
56f5daac 639 struct rtattr *tb[IFLA_MAX+1];
5d5cf1b4
JB
640
641 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
642 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
643 __print_link_stats(fp, tb);
644 fprintf(fp, "%s", _SL_);
645}
646
5d295bb8
AG
647int print_linkinfo_brief(const struct sockaddr_nl *who,
648 struct nlmsghdr *n, void *arg)
649{
56f5daac 650 FILE *fp = (FILE *)arg;
5d295bb8 651 struct ifinfomsg *ifi = NLMSG_DATA(n);
56f5daac 652 struct rtattr *tb[IFLA_MAX+1];
5d295bb8
AG
653 int len = n->nlmsg_len;
654 char *name;
655 char buf[32] = { 0, };
56f5daac 656 unsigned int m_flag = 0;
5d295bb8
AG
657
658 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
659 return -1;
660
661 len -= NLMSG_LENGTH(sizeof(*ifi));
662 if (len < 0)
663 return -1;
664
665 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
666 return -1;
667 if (filter.up && !(ifi->ifi_flags&IFF_UP))
668 return -1;
669
670 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
468fa020 671 if (tb[IFLA_IFNAME] == NULL)
5d295bb8 672 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
468fa020 673
5d295bb8
AG
674 if (filter.label &&
675 (!filter.family || filter.family == AF_PACKET) &&
676 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
677 return -1;
678
679 if (tb[IFLA_GROUP]) {
56f5daac
SH
680 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
681
5d295bb8
AG
682 if (filter.group != -1 && group != filter.group)
683 return -1;
684 }
685
84d30afd 686 if (tb[IFLA_MASTER]) {
56f5daac
SH
687 int master = *(int *)RTA_DATA(tb[IFLA_MASTER]);
688
84d30afd
DA
689 if (filter.master > 0 && master != filter.master)
690 return -1;
56f5daac 691 } else if (filter.master > 0)
84d30afd
DA
692 return -1;
693
e0513807
PS
694 if (filter.kind && match_link_kind(tb, filter.kind, 0))
695 return -1;
84d30afd 696
e0513807
PS
697 if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
698 return -1;
84d30afd 699
5d295bb8
AG
700 if (n->nlmsg_type == RTM_DELLINK)
701 fprintf(fp, "Deleted ");
702
703 name = (char *)(tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
704
705 if (tb[IFLA_LINK]) {
706 SPRINT_BUF(b1);
56f5daac
SH
707 int iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);
708
5d295bb8
AG
709 if (iflink == 0)
710 snprintf(buf, sizeof(buf), "%s@NONE", name);
711 else {
712 snprintf(buf, sizeof(buf),
713 "%s@%s", name, ll_idx_n2a(iflink, b1));
714 m_flag = ll_index_to_flags(iflink);
715 m_flag = !(m_flag & IFF_UP);
716 }
717 } else
718 snprintf(buf, sizeof(buf), "%s", name);
719
720 fprintf(fp, "%-16s ", buf);
721
722 if (tb[IFLA_OPERSTATE])
723 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
724
725 if (filter.family == AF_PACKET) {
726 SPRINT_BUF(b1);
727 if (tb[IFLA_ADDRESS]) {
728 color_fprintf(fp, COLOR_MAC, "%s ",
729 ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
730 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
731 ifi->ifi_type,
732 b1, sizeof(b1)));
733 }
734 }
735
736 if (filter.family == AF_PACKET)
737 print_link_flags(fp, ifi->ifi_flags, m_flag);
738
739 if (filter.family == AF_PACKET)
740 fprintf(fp, "\n");
741 fflush(fp);
742 return 0;
743}
744
ae665a52 745int print_linkinfo(const struct sockaddr_nl *who,
50772dc5 746 struct nlmsghdr *n, void *arg)
aba5acdf 747{
56f5daac 748 FILE *fp = (FILE *)arg;
aba5acdf 749 struct ifinfomsg *ifi = NLMSG_DATA(n);
56f5daac 750 struct rtattr *tb[IFLA_MAX+1];
aba5acdf 751 int len = n->nlmsg_len;
56f5daac 752 unsigned int m_flag = 0;
aba5acdf
SH
753
754 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
755 return 0;
756
757 len -= NLMSG_LENGTH(sizeof(*ifi));
758 if (len < 0)
759 return -1;
760
761 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
762 return 0;
763 if (filter.up && !(ifi->ifi_flags&IFF_UP))
764 return 0;
765
aba5acdf 766 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
468fa020 767 if (tb[IFLA_IFNAME] == NULL)
4cd23bdd 768 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
468fa020 769
aba5acdf
SH
770 if (filter.label &&
771 (!filter.family || filter.family == AF_PACKET) &&
772 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
773 return 0;
774
f960c92a 775 if (tb[IFLA_GROUP]) {
56f5daac
SH
776 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
777
c4fdf75d 778 if (filter.group != -1 && group != filter.group)
f960c92a
VD
779 return -1;
780 }
781
1b944148 782 if (tb[IFLA_MASTER]) {
56f5daac
SH
783 int master = *(int *)RTA_DATA(tb[IFLA_MASTER]);
784
1b944148
VK
785 if (filter.master > 0 && master != filter.master)
786 return -1;
56f5daac 787 } else if (filter.master > 0)
1b944148
VK
788 return -1;
789
e0513807
PS
790 if (filter.kind && match_link_kind(tb, filter.kind, 0))
791 return -1;
712249d8 792
e0513807
PS
793 if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
794 return -1;
712249d8 795
aba5acdf
SH
796 if (n->nlmsg_type == RTM_DELLINK)
797 fprintf(fp, "Deleted ");
798
d7bd2db5
MN
799 fprintf(fp, "%d: ", ifi->ifi_index);
800 color_fprintf(fp, COLOR_IFNAME, "%s",
11522e7d 801 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
aba5acdf
SH
802
803 if (tb[IFLA_LINK]) {
804 SPRINT_BUF(b1);
56f5daac
SH
805 int iflink = *(int *)RTA_DATA(tb[IFLA_LINK]);
806
aba5acdf
SH
807 if (iflink == 0)
808 fprintf(fp, "@NONE: ");
809 else {
ccdcbf35
ND
810 if (tb[IFLA_LINK_NETNSID])
811 fprintf(fp, "@if%d: ", iflink);
812 else {
813 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
814 m_flag = ll_index_to_flags(iflink);
815 m_flag = !(m_flag & IFF_UP);
816 }
aba5acdf
SH
817 }
818 } else {
819 fprintf(fp, ": ");
820 }
821 print_link_flags(fp, ifi->ifi_flags, m_flag);
822
823 if (tb[IFLA_MTU])
56f5daac 824 fprintf(fp, "mtu %u ", *(int *)RTA_DATA(tb[IFLA_MTU]));
aba5acdf 825 if (tb[IFLA_QDISC])
ff24746c 826 fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC]));
aba5acdf
SH
827 if (tb[IFLA_MASTER]) {
828 SPRINT_BUF(b1);
56f5daac 829 fprintf(fp, "master %s ", ll_idx_n2a(*(int *)RTA_DATA(tb[IFLA_MASTER]), b1));
aba5acdf 830 }
82499282 831
3d866ba2 832 if (tb[IFLA_OPERSTATE])
ff24746c 833 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
82499282
SH
834
835 if (do_link && tb[IFLA_LINKMODE])
836 print_linkmode(fp, tb[IFLA_LINKMODE]);
837
c4fdf75d
ST
838 if (tb[IFLA_GROUP]) {
839 SPRINT_BUF(b1);
56f5daac
SH
840 int group = *(int *)RTA_DATA(tb[IFLA_GROUP]);
841
c4fdf75d
ST
842 fprintf(fp, "group %s ", rtnl_group_n2a(group, b1, sizeof(b1)));
843 }
844
aba5acdf 845 if (filter.showqueue)
f78e316f 846 print_queuelen(fp, tb);
ae665a52 847
2ec28933 848 if (!filter.family || filter.family == AF_PACKET || show_details) {
aba5acdf
SH
849 SPRINT_BUF(b1);
850 fprintf(fp, "%s", _SL_);
851 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
852
853 if (tb[IFLA_ADDRESS]) {
d7bd2db5 854 color_fprintf(fp, COLOR_MAC, "%s",
11522e7d
SH
855 ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
856 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
857 ifi->ifi_type,
858 b1, sizeof(b1)));
aba5acdf
SH
859 }
860 if (tb[IFLA_BROADCAST]) {
861 if (ifi->ifi_flags&IFF_POINTOPOINT)
862 fprintf(fp, " peer ");
863 else
864 fprintf(fp, " brd ");
ff9d8f37 865 color_fprintf(fp, COLOR_MAC, "%s",
11522e7d
SH
866 ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
867 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
868 ifi->ifi_type,
869 b1, sizeof(b1)));
aba5acdf
SH
870 }
871 }
1d934839 872
ccdcbf35 873 if (tb[IFLA_LINK_NETNSID]) {
56f5daac 874 int id = *(int *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
ccdcbf35
ND
875
876 if (id >= 0)
877 fprintf(fp, " link-netnsid %d", id);
878 else
879 fprintf(fp, " link-netnsid unknown");
880 }
881
18864827
AK
882 if (tb[IFLA_PROTO_DOWN]) {
883 if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
884 fprintf(fp, " protodown on ");
885 }
886
11522e7d
SH
887 if (show_details) {
888 if (tb[IFLA_PROMISCUITY])
889 fprintf(fp, " promiscuity %u ",
890 *(int *)RTA_DATA(tb[IFLA_PROMISCUITY]));
891
892 if (tb[IFLA_LINKINFO])
893 print_linktype(fp, tb[IFLA_LINKINFO]);
894
895 if (do_link && tb[IFLA_AF_SPEC])
896 print_af_spec(fp, tb[IFLA_AF_SPEC]);
897
898 if (tb[IFLA_NUM_TX_QUEUES])
899 fprintf(fp, "numtxqueues %u ",
900 rta_getattr_u32(tb[IFLA_NUM_TX_QUEUES]));
ede6a3ea 901
11522e7d
SH
902 if (tb[IFLA_NUM_RX_QUEUES])
903 fprintf(fp, "numrxqueues %u ",
904 rta_getattr_u32(tb[IFLA_NUM_RX_QUEUES]));
1cb6a110 905
1acd208c
ED
906 if (tb[IFLA_GSO_MAX_SIZE])
907 fprintf(fp, "gso_max_size %u ",
908 rta_getattr_u32(tb[IFLA_GSO_MAX_SIZE]));
909
910 if (tb[IFLA_GSO_MAX_SEGS])
911 fprintf(fp, "gso_max_segs %u ",
912 rta_getattr_u32(tb[IFLA_GSO_MAX_SEGS]));
913
11522e7d
SH
914 if (tb[IFLA_PHYS_PORT_NAME])
915 fprintf(fp, "portname %s ",
916 rta_getattr_str(tb[IFLA_PHYS_PORT_NAME]));
ee0067a9 917
11522e7d
SH
918 if (tb[IFLA_PHYS_PORT_ID]) {
919 SPRINT_BUF(b1);
920 fprintf(fp, "portid %s ",
921 hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_PORT_ID]),
922 RTA_PAYLOAD(tb[IFLA_PHYS_PORT_ID]),
923 b1, sizeof(b1)));
924 }
925
926 if (tb[IFLA_PHYS_SWITCH_ID]) {
927 SPRINT_BUF(b1);
928 fprintf(fp, "switchid %s ",
929 hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_SWITCH_ID]),
930 RTA_PAYLOAD(tb[IFLA_PHYS_SWITCH_ID]),
931 b1, sizeof(b1)));
932 }
933 }
f1c656e5 934
f1c656e5 935
2ec28933 936 if ((do_link || show_details) && tb[IFLA_IFALIAS]) {
1124ffb7 937 fprintf(fp, "%s alias %s", _SL_,
ff24746c 938 rta_getattr_str(tb[IFLA_IFALIAS]));
263c894f 939 }
ace9c961 940
e6e6fb5c 941 if (do_link && show_stats) {
5d5cf1b4
JB
942 fprintf(fp, "%s", _SL_);
943 __print_link_stats(fp, tb);
ae7229d5 944 }
e6e6fb5c 945
2ec28933 946 if ((do_link || show_details) && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
3fd86630
CW
947 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
948 int rem = RTA_PAYLOAD(vflist);
56f5daac 949
3fd86630
CW
950 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
951 print_vfinfo(fp, i);
aba5acdf 952 }
3fd86630 953
aba5acdf
SH
954 fprintf(fp, "\n");
955 fflush(fp);
5d5cf1b4 956 return 1;
aba5acdf
SH
957}
958
959static int flush_update(void)
960{
e149d4e8
NH
961
962 /*
963 * Note that the kernel may delete multiple addresses for one
964 * delete request (e.g. if ipv4 address promotion is disabled).
965 * Since a flush operation is really a series of delete requests
966 * its possible that we may request an address delete that has
967 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
968 * errors returned from a flush request
969 */
970 if ((rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) &&
971 (errno != EADDRNOTAVAIL)) {
1fb0a998 972 perror("Failed to send flush request");
aba5acdf
SH
973 return -1;
974 }
975 filter.flushp = 0;
976 return 0;
977}
978
35546df7
MN
979static int set_lifetime(unsigned int *lifetime, char *argv)
980{
981 if (strcmp(argv, "forever") == 0)
141bb606 982 *lifetime = INFINITY_LIFE_TIME;
35546df7
MN
983 else if (get_u32(lifetime, argv, 0))
984 return -1;
985
986 return 0;
987}
988
37c9b94e
JP
989static unsigned int get_ifa_flags(struct ifaddrmsg *ifa,
990 struct rtattr *ifa_flags_attr)
991{
992 return ifa_flags_attr ? rta_getattr_u32(ifa_flags_attr) :
993 ifa->ifa_flags;
994}
995
ae665a52 996int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
6dc9f016 997 void *arg)
aba5acdf 998{
4b726cb1 999 FILE *fp = arg;
aba5acdf
SH
1000 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1001 int len = n->nlmsg_len;
037d950b 1002 int deprecated = 0;
3bc1c4f2
BG
1003 /* Use local copy of ifa_flags to not interfere with filtering code */
1004 unsigned int ifa_flags;
56f5daac 1005 struct rtattr *rta_tb[IFA_MAX+1];
56f5daac 1006
aba5acdf
SH
1007 SPRINT_BUF(b1);
1008
1009 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
1010 return 0;
1011 len -= NLMSG_LENGTH(sizeof(*ifa));
1012 if (len < 0) {
1013 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
1014 return -1;
1015 }
1016
1017 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
1018 return 0;
1019
4b726cb1
SH
1020 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa),
1021 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
aba5acdf 1022
37c9b94e
JP
1023 ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]);
1024
aba5acdf
SH
1025 if (!rta_tb[IFA_LOCAL])
1026 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
1027 if (!rta_tb[IFA_ADDRESS])
1028 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
1029
1030 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
1031 return 0;
1032 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1033 return 0;
37c9b94e 1034 if ((filter.flags ^ ifa_flags) & filter.flagmask)
aba5acdf
SH
1035 return 0;
1036 if (filter.label) {
1037 SPRINT_BUF(b1);
1038 const char *label;
56f5daac 1039
aba5acdf
SH
1040 if (rta_tb[IFA_LABEL])
1041 label = RTA_DATA(rta_tb[IFA_LABEL]);
1042 else
1043 label = ll_idx_n2a(ifa->ifa_index, b1);
1044 if (fnmatch(filter.label, label, 0) != 0)
1045 return 0;
1046 }
1047 if (filter.pfx.family) {
1048 if (rta_tb[IFA_LOCAL]) {
d17b136f 1049 inet_prefix dst = { .family = ifa->ifa_family };
56f5daac 1050
aba5acdf
SH
1051 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
1052 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1053 return 0;
1054 }
1055 }
1056
3eb1731b 1057 if (filter.family && filter.family != ifa->ifa_family)
1058 return 0;
1059
aba5acdf
SH
1060 if (filter.flushb) {
1061 struct nlmsghdr *fn;
56f5daac 1062
aba5acdf
SH
1063 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
1064 if (flush_update())
1065 return -1;
1066 }
56f5daac 1067 fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
aba5acdf
SH
1068 memcpy(fn, n, n->nlmsg_len);
1069 fn->nlmsg_type = RTM_DELADDR;
1070 fn->nlmsg_flags = NLM_F_REQUEST;
351efcde 1071 fn->nlmsg_seq = ++rth.seq;
56f5daac 1072 filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb;
aba5acdf
SH
1073 filter.flushed++;
1074 if (show_stats < 2)
1075 return 0;
1076 }
1077
1078 if (n->nlmsg_type == RTM_DELADDR)
1079 fprintf(fp, "Deleted ");
1080
5d295bb8
AG
1081 if (!brief) {
1082 if (filter.oneline || filter.flushb)
1083 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
1084 if (ifa->ifa_family == AF_INET)
1085 fprintf(fp, " inet ");
1086 else if (ifa->ifa_family == AF_INET6)
1087 fprintf(fp, " inet6 ");
1088 else if (ifa->ifa_family == AF_DECnet)
1089 fprintf(fp, " dnet ");
1090 else if (ifa->ifa_family == AF_IPX)
1091 fprintf(fp, " ipx ");
1092 else
1093 fprintf(fp, " family %d ", ifa->ifa_family);
1094 }
aba5acdf
SH
1095
1096 if (rta_tb[IFA_LOCAL]) {
a1121aa1 1097 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
468fa020
SH
1098 format_host_rta(ifa->ifa_family,
1099 rta_tb[IFA_LOCAL]));
ff9d8f37
PS
1100 if (rta_tb[IFA_ADDRESS] &&
1101 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
468fa020
SH
1102 RTA_DATA(rta_tb[IFA_LOCAL]),
1103 ifa->ifa_family == AF_INET ? 4 : 16)) {
ff9d8f37
PS
1104 fprintf(fp, " peer ");
1105 color_fprintf(fp, ifa_family_color(ifa->ifa_family),
468fa020
SH
1106 "%s", format_host_rta(ifa->ifa_family,
1107 rta_tb[IFA_ADDRESS]));
aba5acdf 1108 }
ff9d8f37 1109 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
aba5acdf
SH
1110 }
1111
5d295bb8
AG
1112 if (brief)
1113 goto brief_exit;
1114
aba5acdf 1115 if (rta_tb[IFA_BROADCAST]) {
ff9d8f37
PS
1116 fprintf(fp, "brd ");
1117 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
468fa020
SH
1118 format_host_rta(ifa->ifa_family,
1119 rta_tb[IFA_BROADCAST]));
aba5acdf
SH
1120 }
1121 if (rta_tb[IFA_ANYCAST]) {
ff9d8f37
PS
1122 fprintf(fp, "any ");
1123 color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s ",
468fa020
SH
1124 format_host_rta(ifa->ifa_family,
1125 rta_tb[IFA_ANYCAST]));
aba5acdf
SH
1126 }
1127 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
37c9b94e 1128 if (ifa_flags & IFA_F_SECONDARY) {
3bc1c4f2 1129 ifa_flags &= ~IFA_F_SECONDARY;
a1b9ffcc
BH
1130 if (ifa->ifa_family == AF_INET6)
1131 fprintf(fp, "temporary ");
1132 else
1133 fprintf(fp, "secondary ");
aba5acdf 1134 }
37c9b94e 1135 if (ifa_flags & IFA_F_TENTATIVE) {
3bc1c4f2 1136 ifa_flags &= ~IFA_F_TENTATIVE;
aba5acdf
SH
1137 fprintf(fp, "tentative ");
1138 }
37c9b94e 1139 if (ifa_flags & IFA_F_DEPRECATED) {
3bc1c4f2 1140 ifa_flags &= ~IFA_F_DEPRECATED;
037d950b 1141 deprecated = 1;
aba5acdf
SH
1142 fprintf(fp, "deprecated ");
1143 }
37c9b94e 1144 if (ifa_flags & IFA_F_HOMEADDRESS) {
3bc1c4f2 1145 ifa_flags &= ~IFA_F_HOMEADDRESS;
bac735c5
NT
1146 fprintf(fp, "home ");
1147 }
37c9b94e 1148 if (ifa_flags & IFA_F_NODAD) {
3bc1c4f2 1149 ifa_flags &= ~IFA_F_NODAD;
bac735c5
NT
1150 fprintf(fp, "nodad ");
1151 }
5b7e21c4
JP
1152 if (ifa_flags & IFA_F_MANAGETEMPADDR) {
1153 ifa_flags &= ~IFA_F_MANAGETEMPADDR;
1154 fprintf(fp, "mngtmpaddr ");
1155 }
58c69b22
TH
1156 if (ifa_flags & IFA_F_NOPREFIXROUTE) {
1157 ifa_flags &= ~IFA_F_NOPREFIXROUTE;
1158 fprintf(fp, "noprefixroute ");
1159 }
e31867ac
MC
1160 if (ifa_flags & IFA_F_MCAUTOJOIN) {
1161 ifa_flags &= ~IFA_F_MCAUTOJOIN;
1162 fprintf(fp, "autojoin ");
1163 }
468fa020 1164 if (!(ifa_flags & IFA_F_PERMANENT))
aba5acdf 1165 fprintf(fp, "dynamic ");
468fa020 1166 else
3bc1c4f2 1167 ifa_flags &= ~IFA_F_PERMANENT;
37c9b94e 1168 if (ifa_flags & IFA_F_DADFAILED) {
3bc1c4f2 1169 ifa_flags &= ~IFA_F_DADFAILED;
f4af851b
BH
1170 fprintf(fp, "dadfailed ");
1171 }
3bc1c4f2
BG
1172 if (ifa_flags)
1173 fprintf(fp, "flags %02x ", ifa_flags);
aba5acdf 1174 if (rta_tb[IFA_LABEL])
ff24746c 1175 fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL]));
aba5acdf
SH
1176 if (rta_tb[IFA_CACHEINFO]) {
1177 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
56f5daac 1178
aba5acdf 1179 fprintf(fp, "%s", _SL_);
f66efadd 1180 fprintf(fp, " valid_lft ");
141bb606 1181 if (ci->ifa_valid == INFINITY_LIFE_TIME)
f66efadd 1182 fprintf(fp, "forever");
aba5acdf 1183 else
f66efadd
AS
1184 fprintf(fp, "%usec", ci->ifa_valid);
1185 fprintf(fp, " preferred_lft ");
141bb606 1186 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
f66efadd 1187 fprintf(fp, "forever");
037d950b
BG
1188 else {
1189 if (deprecated)
f66efadd 1190 fprintf(fp, "%dsec", ci->ifa_prefered);
037d950b 1191 else
f66efadd 1192 fprintf(fp, "%usec", ci->ifa_prefered);
037d950b 1193 }
aba5acdf
SH
1194 }
1195 fprintf(fp, "\n");
5d295bb8 1196brief_exit:
aba5acdf
SH
1197 fflush(fp);
1198 return 0;
1199}
1200
56f5daac 1201struct nlmsg_list {
aba5acdf
SH
1202 struct nlmsg_list *next;
1203 struct nlmsghdr h;
1204};
1205
56f5daac 1206struct nlmsg_chain {
62e2e540
ED
1207 struct nlmsg_list *head;
1208 struct nlmsg_list *tail;
1209};
1210
5e5055bc
AH
1211static int print_selected_addrinfo(struct ifinfomsg *ifi,
1212 struct nlmsg_list *ainfo, FILE *fp)
aba5acdf 1213{
56f5daac 1214 for ( ; ainfo ; ainfo = ainfo->next) {
aba5acdf
SH
1215 struct nlmsghdr *n = &ainfo->h;
1216 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1217
1218 if (n->nlmsg_type != RTM_NEWADDR)
1219 continue;
1220
6cf2609d 1221 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifa)))
aba5acdf
SH
1222 return -1;
1223
5e5055bc 1224 if (ifa->ifa_index != ifi->ifi_index ||
aba5acdf
SH
1225 (filter.family && filter.family != ifa->ifa_family))
1226 continue;
1227
5e5055bc
AH
1228 if (filter.up && !(ifi->ifi_flags&IFF_UP))
1229 continue;
1230
aba5acdf
SH
1231 print_addrinfo(NULL, n, fp);
1232 }
5d295bb8
AG
1233 if (brief) {
1234 fprintf(fp, "\n");
1235 fflush(fp);
1236 }
aba5acdf
SH
1237 return 0;
1238}
1239
1240
ae665a52 1241static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
6dc9f016 1242 void *arg)
aba5acdf 1243{
62e2e540 1244 struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg;
aba5acdf 1245 struct nlmsg_list *h;
aba5acdf 1246
56f5daac 1247 h = malloc(n->nlmsg_len+sizeof(void *));
aba5acdf
SH
1248 if (h == NULL)
1249 return -1;
1250
1251 memcpy(&h->h, n, n->nlmsg_len);
1252 h->next = NULL;
1253
62e2e540
ED
1254 if (lchain->tail)
1255 lchain->tail->next = h;
1256 else
1257 lchain->head = h;
1258 lchain->tail = h;
aba5acdf
SH
1259
1260 ll_remember_index(who, n, NULL);
1261 return 0;
1262}
1263
81824ac2
PE
1264static __u32 ipadd_dump_magic = 0x47361222;
1265
1266static int ipadd_save_prep(void)
1267{
1268 int ret;
1269
1270 if (isatty(STDOUT_FILENO)) {
14645ec2 1271 fprintf(stderr, "Not sending a binary stream to stdout\n");
81824ac2
PE
1272 return -1;
1273 }
1274
1275 ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic));
1276 if (ret != sizeof(ipadd_dump_magic)) {
1277 fprintf(stderr, "Can't write magic to dump file\n");
1278 return -1;
1279 }
1280
1281 return 0;
1282}
1283
1284static int ipadd_dump_check_magic(void)
1285{
1286 int ret;
1287 __u32 magic = 0;
1288
1289 if (isatty(STDIN_FILENO)) {
4e972d5e 1290 fprintf(stderr, "Can't restore address dump from a terminal\n");
81824ac2
PE
1291 return -1;
1292 }
1293
1294 ret = fread(&magic, sizeof(magic), 1, stdin);
1295 if (magic != ipadd_dump_magic) {
1296 fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic);
1297 return -1;
1298 }
1299
1300 return 0;
1301}
1302
1303static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
1304 void *arg)
1305{
1306 int ret;
1307
1308 ret = write(STDOUT_FILENO, n, n->nlmsg_len);
1309 if ((ret > 0) && (ret != n->nlmsg_len)) {
1310 fprintf(stderr, "Short write while saving nlmsg\n");
1311 ret = -EIO;
1312 }
1313
1314 return ret == n->nlmsg_len ? 0 : ret;
1315}
1316
0628cddd
ND
1317static int show_handler(const struct sockaddr_nl *nl,
1318 struct rtnl_ctrl_data *ctrl,
1319 struct nlmsghdr *n, void *arg)
81824ac2
PE
1320{
1321 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1322
1323 printf("if%d:\n", ifa->ifa_index);
1324 print_addrinfo(NULL, n, stdout);
1325 return 0;
1326}
1327
1328static int ipaddr_showdump(void)
1329{
1330 if (ipadd_dump_check_magic())
1331 exit(-1);
1332
1333 exit(rtnl_from_file(stdin, &show_handler, NULL));
1334}
1335
0628cddd
ND
1336static int restore_handler(const struct sockaddr_nl *nl,
1337 struct rtnl_ctrl_data *ctrl,
1338 struct nlmsghdr *n, void *arg)
81824ac2
PE
1339{
1340 int ret;
1341
1342 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
1343
1344 ll_init_map(&rth);
1345
c079e121 1346 ret = rtnl_talk(&rth, n, n, sizeof(*n));
81824ac2
PE
1347 if ((ret < 0) && (errno == EEXIST))
1348 ret = 0;
1349
1350 return ret;
1351}
1352
1353static int ipaddr_restore(void)
1354{
1355 if (ipadd_dump_check_magic())
1356 exit(-1);
1357
1358 exit(rtnl_from_file(stdin, &restore_handler, NULL));
1359}
1360
8d07e5f7
SH
1361static void free_nlmsg_chain(struct nlmsg_chain *info)
1362{
1363 struct nlmsg_list *l, *n;
1364
1365 for (l = info->head; l; l = n) {
1366 n = l->next;
1367 free(l);
1368 }
1369}
1370
1371static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
1372{
1373 struct nlmsg_list *l, **lp;
1374
1375 lp = &linfo->head;
56f5daac 1376 while ((l = *lp) != NULL) {
8d07e5f7 1377 int ok = 0;
7f747fd9 1378 int missing_net_address = 1;
8d07e5f7
SH
1379 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1380 struct nlmsg_list *a;
1381
1382 for (a = ainfo->head; a; a = a->next) {
1383 struct nlmsghdr *n = &a->h;
1384 struct ifaddrmsg *ifa = NLMSG_DATA(n);
37c9b94e
JP
1385 struct rtattr *tb[IFA_MAX + 1];
1386 unsigned int ifa_flags;
8d07e5f7 1387
7f747fd9
PP
1388 if (ifa->ifa_index != ifi->ifi_index)
1389 continue;
1390 missing_net_address = 0;
1391 if (filter.family && filter.family != ifa->ifa_family)
8d07e5f7
SH
1392 continue;
1393 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1394 continue;
37c9b94e
JP
1395
1396 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
1397 ifa_flags = get_ifa_flags(ifa, tb[IFA_FLAGS]);
1398
1399 if ((filter.flags ^ ifa_flags) & filter.flagmask)
8d07e5f7
SH
1400 continue;
1401 if (filter.pfx.family || filter.label) {
8d07e5f7
SH
1402 if (!tb[IFA_LOCAL])
1403 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1404
1405 if (filter.pfx.family && tb[IFA_LOCAL]) {
d17b136f
PS
1406 inet_prefix dst = {
1407 .family = ifa->ifa_family
1408 };
56f5daac 1409
8d07e5f7
SH
1410 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
1411 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1412 continue;
1413 }
1414 if (filter.label) {
1415 SPRINT_BUF(b1);
1416 const char *label;
56f5daac 1417
8d07e5f7
SH
1418 if (tb[IFA_LABEL])
1419 label = RTA_DATA(tb[IFA_LABEL]);
1420 else
1421 label = ll_idx_n2a(ifa->ifa_index, b1);
1422 if (fnmatch(filter.label, label, 0) != 0)
1423 continue;
1424 }
1425 }
1426
1427 ok = 1;
1428 break;
1429 }
7f747fd9
PP
1430 if (missing_net_address &&
1431 (filter.family == AF_UNSPEC || filter.family == AF_PACKET))
1432 ok = 1;
8d07e5f7
SH
1433 if (!ok) {
1434 *lp = l->next;
1435 free(l);
1436 } else
1437 lp = &l->next;
1438 }
1439}
1440
1441static int ipaddr_flush(void)
1442{
1443 int round = 0;
1444 char flushb[4096-512];
1445
1446 filter.flushb = flushb;
1447 filter.flushp = 0;
1448 filter.flushe = sizeof(flushb);
1449
1450 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
8d07e5f7
SH
1451 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1452 perror("Cannot send dump request");
1453 exit(1);
1454 }
1455 filter.flushed = 0;
d25ec03e
PS
1456 if (rtnl_dump_filter_nc(&rth, print_addrinfo,
1457 stdout, NLM_F_DUMP_INTR) < 0) {
8d07e5f7
SH
1458 fprintf(stderr, "Flush terminated\n");
1459 exit(1);
1460 }
1461 if (filter.flushed == 0) {
1462 flush_done:
1463 if (show_stats) {
1464 if (round == 0)
1465 printf("Nothing to flush.\n");
1466 else
56f5daac 1467 printf("*** Flush is complete after %d round%s ***\n", round, round > 1?"s":"");
8d07e5f7
SH
1468 }
1469 fflush(stdout);
1470 return 0;
1471 }
1472 round++;
1473 if (flush_update() < 0)
1474 return 1;
1475
1476 if (show_stats) {
1477 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
1478 fflush(stdout);
1479 }
1480
1481 /* If we are flushing, and specifying primary, then we
1482 * want to flush only a single round. Otherwise, we'll
1483 * start flushing secondaries that were promoted to
1484 * primaries.
1485 */
1486 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
1487 goto flush_done;
1488 }
1489 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
1490 fflush(stderr);
1491 return 1;
1492}
1493
b0a4ce62
DA
1494static int iplink_filter_req(struct nlmsghdr *nlh, int reqlen)
1495{
1496 int err;
1497
1498 err = addattr32(nlh, reqlen, IFLA_EXT_MASK, RTEXT_FILTER_VF);
1499 if (err)
1500 return err;
1501
1502 if (filter.master) {
1503 err = addattr32(nlh, reqlen, IFLA_MASTER, filter.master);
1504 if (err)
1505 return err;
1506 }
1507
1508 if (filter.kind) {
1509 struct rtattr *linkinfo;
1510
1511 linkinfo = addattr_nest(nlh, reqlen, IFLA_LINKINFO);
1512
1513 err = addattr_l(nlh, reqlen, IFLA_INFO_KIND, filter.kind,
1514 strlen(filter.kind));
1515 if (err)
1516 return err;
1517
1518 addattr_nest_end(nlh, linkinfo);
1519 }
1520
1521 return 0;
1522}
1523
81824ac2 1524static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
aba5acdf 1525{
62e2e540
ED
1526 struct nlmsg_chain linfo = { NULL, NULL};
1527 struct nlmsg_chain ainfo = { NULL, NULL};
8d07e5f7 1528 struct nlmsg_list *l;
aba5acdf
SH
1529 char *filter_dev = NULL;
1530 int no_link = 0;
1531
093b7646 1532 ipaddr_reset_filter(oneline, 0);
aba5acdf 1533 filter.showqueue = 1;
906dfe48 1534 filter.family = preferred_family;
c4fdf75d 1535 filter.group = -1;
f960c92a 1536
81824ac2 1537 if (action == IPADD_FLUSH) {
aba5acdf
SH
1538 if (argc <= 0) {
1539 fprintf(stderr, "Flush requires arguments.\n");
f960c92a 1540
aba5acdf
SH
1541 return -1;
1542 }
1543 if (filter.family == AF_PACKET) {
1544 fprintf(stderr, "Cannot flush link addresses.\n");
1545 return -1;
1546 }
1547 }
1548
1549 while (argc > 0) {
1550 if (strcmp(*argv, "to") == 0) {
1551 NEXT_ARG();
1552 get_prefix(&filter.pfx, *argv, filter.family);
1553 if (filter.family == AF_UNSPEC)
1554 filter.family = filter.pfx.family;
1555 } else if (strcmp(*argv, "scope") == 0) {
56f5daac
SH
1556 unsigned int scope = 0;
1557
aba5acdf
SH
1558 NEXT_ARG();
1559 filter.scopemask = -1;
1560 if (rtnl_rtscope_a2n(&scope, *argv)) {
1561 if (strcmp(*argv, "all") != 0)
1562 invarg("invalid \"scope\"\n", *argv);
1563 scope = RT_SCOPE_NOWHERE;
1564 filter.scopemask = 0;
1565 }
1566 filter.scope = scope;
1567 } else if (strcmp(*argv, "up") == 0) {
1568 filter.up = 1;
1569 } else if (strcmp(*argv, "dynamic") == 0) {
1570 filter.flags &= ~IFA_F_PERMANENT;
1571 filter.flagmask |= IFA_F_PERMANENT;
1572 } else if (strcmp(*argv, "permanent") == 0) {
1573 filter.flags |= IFA_F_PERMANENT;
1574 filter.flagmask |= IFA_F_PERMANENT;
a1b9ffcc
BH
1575 } else if (strcmp(*argv, "secondary") == 0 ||
1576 strcmp(*argv, "temporary") == 0) {
aba5acdf
SH
1577 filter.flags |= IFA_F_SECONDARY;
1578 filter.flagmask |= IFA_F_SECONDARY;
1579 } else if (strcmp(*argv, "primary") == 0) {
1580 filter.flags &= ~IFA_F_SECONDARY;
1581 filter.flagmask |= IFA_F_SECONDARY;
1582 } else if (strcmp(*argv, "tentative") == 0) {
1583 filter.flags |= IFA_F_TENTATIVE;
1584 filter.flagmask |= IFA_F_TENTATIVE;
b5f39b25
HK
1585 } else if (strcmp(*argv, "-tentative") == 0) {
1586 filter.flags &= ~IFA_F_TENTATIVE;
1587 filter.flagmask |= IFA_F_TENTATIVE;
aba5acdf
SH
1588 } else if (strcmp(*argv, "deprecated") == 0) {
1589 filter.flags |= IFA_F_DEPRECATED;
1590 filter.flagmask |= IFA_F_DEPRECATED;
b5f39b25
HK
1591 } else if (strcmp(*argv, "-deprecated") == 0) {
1592 filter.flags &= ~IFA_F_DEPRECATED;
1593 filter.flagmask |= IFA_F_DEPRECATED;
bac735c5
NT
1594 } else if (strcmp(*argv, "home") == 0) {
1595 filter.flags |= IFA_F_HOMEADDRESS;
1596 filter.flagmask |= IFA_F_HOMEADDRESS;
1597 } else if (strcmp(*argv, "nodad") == 0) {
1598 filter.flags |= IFA_F_NODAD;
1599 filter.flagmask |= IFA_F_NODAD;
5b7e21c4
JP
1600 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1601 filter.flags |= IFA_F_MANAGETEMPADDR;
1602 filter.flagmask |= IFA_F_MANAGETEMPADDR;
58c69b22
TH
1603 } else if (strcmp(*argv, "noprefixroute") == 0) {
1604 filter.flags |= IFA_F_NOPREFIXROUTE;
1605 filter.flagmask |= IFA_F_NOPREFIXROUTE;
e31867ac
MC
1606 } else if (strcmp(*argv, "autojoin") == 0) {
1607 filter.flags |= IFA_F_MCAUTOJOIN;
1608 filter.flagmask |= IFA_F_MCAUTOJOIN;
a1f27794
BH
1609 } else if (strcmp(*argv, "dadfailed") == 0) {
1610 filter.flags |= IFA_F_DADFAILED;
1611 filter.flagmask |= IFA_F_DADFAILED;
b5f39b25
HK
1612 } else if (strcmp(*argv, "-dadfailed") == 0) {
1613 filter.flags &= ~IFA_F_DADFAILED;
1614 filter.flagmask |= IFA_F_DADFAILED;
aba5acdf
SH
1615 } else if (strcmp(*argv, "label") == 0) {
1616 NEXT_ARG();
1617 filter.label = *argv;
f960c92a
VD
1618 } else if (strcmp(*argv, "group") == 0) {
1619 NEXT_ARG();
1620 if (rtnl_group_a2n(&filter.group, *argv))
1621 invarg("Invalid \"group\" value\n", *argv);
1b944148
VK
1622 } else if (strcmp(*argv, "master") == 0) {
1623 int ifindex;
56f5daac 1624
1b944148
VK
1625 NEXT_ARG();
1626 ifindex = ll_name_to_index(*argv);
1627 if (!ifindex)
1628 invarg("Device does not exist\n", *argv);
1629 filter.master = ifindex;
104444c2
DA
1630 } else if (strcmp(*argv, "vrf") == 0) {
1631 int ifindex;
1632
1633 NEXT_ARG();
1634 ifindex = ll_name_to_index(*argv);
1635 if (!ifindex)
1636 invarg("Not a valid VRF name\n", *argv);
1637 if (!name_is_vrf(*argv))
1638 invarg("Not a valid VRF name\n", *argv);
1639 filter.master = ifindex;
24604eb2 1640 } else if (strcmp(*argv, "type") == 0) {
e0513807
PS
1641 int soff;
1642
712249d8 1643 NEXT_ARG();
e0513807
PS
1644 soff = strlen(*argv) - strlen("_slave");
1645 if (!strcmp(*argv + soff, "_slave")) {
1646 (*argv)[soff] = '\0';
1647 filter.slave_kind = *argv;
1648 } else {
1649 filter.kind = *argv;
1650 }
aba5acdf 1651 } else {
468fa020 1652 if (strcmp(*argv, "dev") == 0)
aba5acdf 1653 NEXT_ARG();
468fa020 1654 else if (matches(*argv, "help") == 0)
aba5acdf
SH
1655 usage();
1656 if (filter_dev)
1657 duparg2("dev", *argv);
1658 filter_dev = *argv;
1659 }
1660 argv++; argc--;
1661 }
1662
aba5acdf
SH
1663 if (filter_dev) {
1664 filter.ifindex = ll_name_to_index(filter_dev);
1665 if (filter.ifindex <= 0) {
1666 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
1667 return -1;
1668 }
1669 }
1670
81824ac2 1671 if (action == IPADD_FLUSH)
8d07e5f7 1672 return ipaddr_flush();
351efcde 1673
81824ac2
PE
1674 if (action == IPADD_SAVE) {
1675 if (ipadd_save_prep())
1676 exit(1);
1677
1678 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
1679 perror("Cannot send dump request");
1680 exit(1);
1681 }
1682
1683 if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) {
1684 fprintf(stderr, "Save terminated\n");
1685 exit(1);
1686 }
1687
1688 exit(0);
1689 }
1690
50b9950d
RP
1691 /*
1692 * If only filter_dev present and none of the other
1693 * link filters are present, use RTM_GETLINK to get
1694 * the link device
1695 */
1696 if (filter_dev && filter.group == -1 && do_link == 1) {
1697 if (iplink_get(0, filter_dev, RTEXT_FILTER_VF) < 0) {
1698 perror("Cannot send link get request");
1699 exit(1);
1700 }
1701 exit(0);
1702 }
1703
b0a4ce62
DA
1704 if (rtnl_wilddump_req_filter_fn(&rth, preferred_family, RTM_GETLINK,
1705 iplink_filter_req) < 0) {
8d07e5f7
SH
1706 perror("Cannot send dump request");
1707 exit(1);
1708 }
3bc1c4f2 1709
8d07e5f7
SH
1710 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1711 fprintf(stderr, "Dump terminated\n");
1712 exit(1);
aba5acdf
SH
1713 }
1714
af9d406f 1715 if (filter.family != AF_PACKET) {
8d07e5f7
SH
1716 if (filter.oneline)
1717 no_link = 1;
1718
aba5acdf
SH
1719 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1720 perror("Cannot send dump request");
1721 exit(1);
1722 }
1723
cd70f3f5 1724 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
aba5acdf
SH
1725 fprintf(stderr, "Dump terminated\n");
1726 exit(1);
1727 }
aba5acdf 1728
8d07e5f7 1729 ipaddr_filter(&linfo, &ainfo);
aba5acdf
SH
1730 }
1731
8d07e5f7 1732 for (l = linfo.head; l; l = l->next) {
5d5cf1b4 1733 int res = 0;
5d295bb8 1734 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
5d5cf1b4 1735
5d295bb8
AG
1736 if (brief) {
1737 if (print_linkinfo_brief(NULL, &l->h, stdout) == 0)
1738 if (filter.family != AF_PACKET)
1739 print_selected_addrinfo(ifi,
1740 ainfo.head,
1741 stdout);
1742 } else if (no_link ||
1743 (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) {
aba5acdf 1744 if (filter.family != AF_PACKET)
5e5055bc 1745 print_selected_addrinfo(ifi,
8d07e5f7 1746 ainfo.head, stdout);
5d5cf1b4
JB
1747 if (res > 0 && !do_link && show_stats)
1748 print_link_stats(stdout, &l->h);
aba5acdf 1749 }
aba5acdf 1750 }
8d07e5f7
SH
1751 fflush(stdout);
1752
1753 free_nlmsg_chain(&ainfo);
1754 free_nlmsg_chain(&linfo);
aba5acdf 1755
351efcde 1756 return 0;
aba5acdf
SH
1757}
1758
f89a2a05
SC
1759static void
1760ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max)
1761{
1762 struct rtattr *vflist = tb[IFLA_VFINFO_LIST];
1763 struct rtattr *i, *vf[IFLA_VF_MAX+1];
1764 struct ifla_vf_rate *vf_rate;
1765 int rem;
1766
1767 rem = RTA_PAYLOAD(vflist);
1768
1769 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
1770 parse_rtattr_nested(vf, IFLA_VF_MAX, i);
1771 vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
1772 if (vf_rate->vf == vfnum) {
1773 *min = vf_rate->min_tx_rate;
1774 *max = vf_rate->max_tx_rate;
1775 return;
1776 }
1777 }
1778 fprintf(stderr, "Cannot find VF %d\n", vfnum);
1779 exit(1);
1780}
1781
1782void ipaddr_get_vf_rate(int vfnum, int *min, int *max, int idx)
1783{
1784 struct nlmsg_chain linfo = { NULL, NULL};
1785 struct rtattr *tb[IFLA_MAX+1];
1786 struct ifinfomsg *ifi;
1787 struct nlmsg_list *l;
1788 struct nlmsghdr *n;
1789 int len;
1790
1791 if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
1792 perror("Cannot send dump request");
1793 exit(1);
1794 }
1795 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1796 fprintf(stderr, "Dump terminated\n");
1797 exit(1);
1798 }
1799 for (l = linfo.head; l; l = l->next) {
1800 n = &l->h;
1801 ifi = NLMSG_DATA(n);
1802
1803 len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
1199c4f5 1804 if (len < 0 || (idx && idx != ifi->ifi_index))
f89a2a05
SC
1805 continue;
1806
1807 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1808
1809 if ((tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF])) {
1810 ipaddr_loop_each_vf(tb, vfnum, min, max);
1811 return;
1812 }
1813 }
1814}
1815
aba5acdf
SH
1816int ipaddr_list_link(int argc, char **argv)
1817{
1818 preferred_family = AF_PACKET;
1819 do_link = 1;
81824ac2 1820 return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST);
aba5acdf
SH
1821}
1822
093b7646 1823void ipaddr_reset_filter(int oneline, int ifindex)
aba5acdf
SH
1824{
1825 memset(&filter, 0, sizeof(filter));
1826 filter.oneline = oneline;
093b7646 1827 filter.ifindex = ifindex;
aba5acdf
SH
1828}
1829
3d866ba2 1830static int default_scope(inet_prefix *lcl)
aba5acdf
SH
1831{
1832 if (lcl->family == AF_INET) {
56f5daac 1833 if (lcl->bytelen >= 1 && *(__u8 *)&lcl->data == 127)
aba5acdf
SH
1834 return RT_SCOPE_HOST;
1835 }
1836 return 0;
1837}
1838
e31867ac
MC
1839static bool ipaddr_is_multicast(inet_prefix *a)
1840{
1841 if (a->family == AF_INET)
1842 return IN_MULTICAST(ntohl(a->data[0]));
1843 else if (a->family == AF_INET6)
1844 return IN6_IS_ADDR_MULTICAST(a->data);
1845 else
1846 return false;
1847}
1848
3d866ba2 1849static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
aba5acdf 1850{
aba5acdf 1851 struct {
4806867a
SH
1852 struct nlmsghdr n;
1853 struct ifaddrmsg ifa;
4b726cb1 1854 char buf[256];
d17b136f
PS
1855 } req = {
1856 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
1857 .n.nlmsg_flags = NLM_F_REQUEST | flags,
1858 .n.nlmsg_type = cmd,
1859 .ifa.ifa_family = preferred_family,
1860 };
aba5acdf
SH
1861 char *d = NULL;
1862 char *l = NULL;
f082b64f 1863 char *lcl_arg = NULL;
35546df7
MN
1864 char *valid_lftp = NULL;
1865 char *preferred_lftp = NULL;
aba5acdf
SH
1866 inet_prefix lcl;
1867 inet_prefix peer;
1868 int local_len = 0;
1869 int peer_len = 0;
1870 int brd_len = 0;
1871 int any_len = 0;
1872 int scoped = 0;
141bb606
MN
1873 __u32 preferred_lft = INFINITY_LIFE_TIME;
1874 __u32 valid_lft = INFINITY_LIFE_TIME;
37c9b94e 1875 unsigned int ifa_flags = 0;
aba5acdf 1876
aba5acdf
SH
1877 while (argc > 0) {
1878 if (strcmp(*argv, "peer") == 0 ||
1879 strcmp(*argv, "remote") == 0) {
1880 NEXT_ARG();
1881
1882 if (peer_len)
1883 duparg("peer", *argv);
1884 get_prefix(&peer, *argv, req.ifa.ifa_family);
1885 peer_len = peer.bytelen;
1886 if (req.ifa.ifa_family == AF_UNSPEC)
1887 req.ifa.ifa_family = peer.family;
1888 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1889 req.ifa.ifa_prefixlen = peer.bitlen;
1890 } else if (matches(*argv, "broadcast") == 0 ||
1891 strcmp(*argv, "brd") == 0) {
1892 inet_prefix addr;
56f5daac 1893
aba5acdf
SH
1894 NEXT_ARG();
1895 if (brd_len)
1896 duparg("broadcast", *argv);
1897 if (strcmp(*argv, "+") == 0)
1898 brd_len = -1;
1899 else if (strcmp(*argv, "-") == 0)
1900 brd_len = -2;
1901 else {
1902 get_addr(&addr, *argv, req.ifa.ifa_family);
1903 if (req.ifa.ifa_family == AF_UNSPEC)
1904 req.ifa.ifa_family = addr.family;
1905 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1906 brd_len = addr.bytelen;
1907 }
1908 } else if (strcmp(*argv, "anycast") == 0) {
1909 inet_prefix addr;
56f5daac 1910
aba5acdf
SH
1911 NEXT_ARG();
1912 if (any_len)
1913 duparg("anycast", *argv);
1914 get_addr(&addr, *argv, req.ifa.ifa_family);
1915 if (req.ifa.ifa_family == AF_UNSPEC)
1916 req.ifa.ifa_family = addr.family;
1917 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1918 any_len = addr.bytelen;
1919 } else if (strcmp(*argv, "scope") == 0) {
56f5daac
SH
1920 unsigned int scope = 0;
1921
aba5acdf
SH
1922 NEXT_ARG();
1923 if (rtnl_rtscope_a2n(&scope, *argv))
f1675d61 1924 invarg("invalid scope value.", *argv);
aba5acdf
SH
1925 req.ifa.ifa_scope = scope;
1926 scoped = 1;
1927 } else if (strcmp(*argv, "dev") == 0) {
1928 NEXT_ARG();
1929 d = *argv;
1930 } else if (strcmp(*argv, "label") == 0) {
1931 NEXT_ARG();
1932 l = *argv;
1933 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
35546df7
MN
1934 } else if (matches(*argv, "valid_lft") == 0) {
1935 if (valid_lftp)
1936 duparg("valid_lft", *argv);
1937 NEXT_ARG();
1938 valid_lftp = *argv;
1939 if (set_lifetime(&valid_lft, *argv))
1940 invarg("valid_lft value", *argv);
1941 } else if (matches(*argv, "preferred_lft") == 0) {
1942 if (preferred_lftp)
1943 duparg("preferred_lft", *argv);
1944 NEXT_ARG();
1945 preferred_lftp = *argv;
1946 if (set_lifetime(&preferred_lft, *argv))
1947 invarg("preferred_lft value", *argv);
bac735c5 1948 } else if (strcmp(*argv, "home") == 0) {
37c9b94e 1949 ifa_flags |= IFA_F_HOMEADDRESS;
bac735c5 1950 } else if (strcmp(*argv, "nodad") == 0) {
37c9b94e 1951 ifa_flags |= IFA_F_NODAD;
5b7e21c4
JP
1952 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1953 ifa_flags |= IFA_F_MANAGETEMPADDR;
58c69b22
TH
1954 } else if (strcmp(*argv, "noprefixroute") == 0) {
1955 ifa_flags |= IFA_F_NOPREFIXROUTE;
e31867ac
MC
1956 } else if (strcmp(*argv, "autojoin") == 0) {
1957 ifa_flags |= IFA_F_MCAUTOJOIN;
aba5acdf 1958 } else {
468fa020 1959 if (strcmp(*argv, "local") == 0)
aba5acdf 1960 NEXT_ARG();
aba5acdf
SH
1961 if (matches(*argv, "help") == 0)
1962 usage();
1963 if (local_len)
1964 duparg2("local", *argv);
f082b64f 1965 lcl_arg = *argv;
aba5acdf
SH
1966 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1967 if (req.ifa.ifa_family == AF_UNSPEC)
1968 req.ifa.ifa_family = lcl.family;
1969 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1970 local_len = lcl.bytelen;
1971 }
1972 argc--; argv++;
1973 }
8b21f88d
WC
1974 if (ifa_flags <= 0xff)
1975 req.ifa.ifa_flags = ifa_flags;
1976 else
1977 addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
37c9b94e 1978
aba5acdf
SH
1979 if (d == NULL) {
1980 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1981 return -1;
1982 }
1983 if (l && matches(d, l) != 0) {
1984 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
1db61e02 1985 return -1;
aba5acdf
SH
1986 }
1987
f082b64f 1988 if (peer_len == 0 && local_len) {
1989 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1990 fprintf(stderr,
468fa020
SH
1991 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
1992 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
1993 " This special behaviour is likely to disappear in further releases,\n"
f082b64f 1994 " fix your scripts!\n", lcl_arg, local_len*8);
1995 } else {
1996 peer = lcl;
1997 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1998 }
aba5acdf
SH
1999 }
2000 if (req.ifa.ifa_prefixlen == 0)
2001 req.ifa.ifa_prefixlen = lcl.bitlen;
2002
2003 if (brd_len < 0 && cmd != RTM_DELADDR) {
2004 inet_prefix brd;
2005 int i;
56f5daac 2006
aba5acdf
SH
2007 if (req.ifa.ifa_family != AF_INET) {
2008 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
2009 return -1;
2010 }
2011 brd = peer;
2012 if (brd.bitlen <= 30) {
4b726cb1 2013 for (i = 31; i >= brd.bitlen; i--) {
aba5acdf
SH
2014 if (brd_len == -1)
2015 brd.data[0] |= htonl(1<<(31-i));
2016 else
2017 brd.data[0] &= ~htonl(1<<(31-i));
2018 }
2019 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
2020 brd_len = brd.bytelen;
2021 }
2022 }
2023 if (!scoped && cmd != RTM_DELADDR)
2024 req.ifa.ifa_scope = default_scope(&lcl);
2025
aba5acdf
SH
2026 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
2027 fprintf(stderr, "Cannot find device \"%s\"\n", d);
2028 return -1;
2029 }
2030
35546df7 2031 if (valid_lftp || preferred_lftp) {
d17b136f
PS
2032 struct ifa_cacheinfo cinfo = {};
2033
35546df7
MN
2034 if (!valid_lft) {
2035 fprintf(stderr, "valid_lft is zero\n");
2036 return -1;
2037 }
2038 if (valid_lft < preferred_lft) {
2039 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
2040 return -1;
2041 }
2042
35546df7
MN
2043 cinfo.ifa_prefered = preferred_lft;
2044 cinfo.ifa_valid = valid_lft;
2045 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
2046 sizeof(cinfo));
2047 }
2048
e31867ac
MC
2049 if ((ifa_flags & IFA_F_MCAUTOJOIN) && !ipaddr_is_multicast(&lcl)) {
2050 fprintf(stderr, "autojoin needs multicast address\n");
2051 return -1;
2052 }
2053
c079e121 2054 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
1db61e02 2055 return -2;
aba5acdf 2056
351efcde 2057 return 0;
aba5acdf
SH
2058}
2059
2060int do_ipaddr(int argc, char **argv)
2061{
2062 if (argc < 1)
81824ac2 2063 return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST);
aba5acdf 2064 if (matches(*argv, "add") == 0)
0aef366b
NT
2065 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
2066 if (matches(*argv, "change") == 0 ||
2067 strcmp(*argv, "chg") == 0)
2068 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
2069 if (matches(*argv, "replace") == 0)
2070 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
aba5acdf 2071 if (matches(*argv, "delete") == 0)
0aef366b 2072 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
aba5acdf
SH
2073 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
2074 || matches(*argv, "lst") == 0)
81824ac2 2075 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST);
aba5acdf 2076 if (matches(*argv, "flush") == 0)
81824ac2
PE
2077 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH);
2078 if (matches(*argv, "save") == 0)
2079 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE);
2080 if (matches(*argv, "showdump") == 0)
2081 return ipaddr_showdump();
2082 if (matches(*argv, "restore") == 0)
2083 return ipaddr_restore();
aba5acdf
SH
2084 if (matches(*argv, "help") == 0)
2085 usage();
4e972d5e 2086 fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv);
aba5acdf
SH
2087 exit(-1);
2088}