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