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