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