2 * ipaddress.c "ip address".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <sys/param.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/sockios.h>
30 #include <linux/net_namespace.h>
36 #include "ip_common.h"
46 static struct link_filter filter
;
49 static void usage(void) __attribute__((noreturn
));
51 static void usage(void)
56 fprintf(stderr
, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
57 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
58 fprintf(stderr
, " ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
59 fprintf(stderr
, " ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
60 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
61 fprintf(stderr
, " ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n");
62 fprintf(stderr
, " [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n");
63 fprintf(stderr
, " [ label LABEL ] [up] [ vrf NAME ] ]\n");
64 fprintf(stderr
, " ip address {showdump|restore}\n");
65 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
66 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
67 fprintf(stderr
, " [ label IFNAME ] [ scope SCOPE-ID ]\n");
68 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
69 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
70 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
71 fprintf(stderr
, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
72 fprintf(stderr
, " CONFFLAG-LIST ]\n");
73 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
74 fprintf(stderr
, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
75 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
76 fprintf(stderr
, "LFT := forever | SECONDS\n");
77 fprintf(stderr
, "TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |\n");
78 fprintf(stderr
, " bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |\n");
79 fprintf(stderr
, " gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti |\n");
80 fprintf(stderr
, " nlmon | can | bond_slave | ipvlan | geneve | bridge_slave |\n");
81 fprintf(stderr
, " hsr | macsec | netdevsim\n");
86 static void print_link_flags(FILE *fp
, unsigned int flags
, unsigned int mdown
)
88 open_json_array(PRINT_ANY
, is_json_context() ? "flags" : "<");
89 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
90 print_string(PRINT_ANY
, NULL
,
91 flags
? "%s," : "%s", "NO-CARRIER");
92 flags
&= ~IFF_RUNNING
;
93 #define _PF(f) if (flags&IFF_##f) { \
95 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
116 print_hex(PRINT_ANY
, NULL
, "%x", flags
);
118 print_string(PRINT_ANY
, NULL
, ",%s", "M-DOWN");
119 close_json_array(PRINT_ANY
, "> ");
122 static const char *oper_states
[] = {
123 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
124 "TESTING", "DORMANT", "UP"
127 static void print_operstate(FILE *f
, __u8 state
)
129 if (state
>= ARRAY_SIZE(oper_states
)) {
130 if (is_json_context())
131 print_uint(PRINT_JSON
, "operstate_index", NULL
, state
);
133 print_0xhex(PRINT_FP
, NULL
, "state %#x", state
);
135 print_color_string(PRINT_ANY
,
136 oper_state_color(state
),
141 if (is_json_context())
142 print_string(PRINT_JSON
,
144 NULL
, oper_states
[state
]);
146 fprintf(f
, "state ");
147 color_fprintf(f
, oper_state_color(state
),
148 "%s ", oper_states
[state
]);
153 int get_operstate(const char *name
)
157 for (i
= 0; i
< ARRAY_SIZE(oper_states
); i
++)
158 if (strcasecmp(name
, oper_states
[i
]) == 0)
163 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
168 qlen
= rta_getattr_u32(tb
[IFLA_TXQLEN
]);
170 struct ifreq ifr
= {};
171 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
176 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
177 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
178 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
186 print_int(PRINT_ANY
, "txqlen", "qlen %d", qlen
);
189 static const char *link_modes
[] = {
193 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
195 unsigned int mode
= rta_getattr_u8(tb
);
197 if (mode
>= ARRAY_SIZE(link_modes
))
203 print_string(PRINT_ANY
,
209 static char *parse_link_kind(struct rtattr
*tb
, bool slave
)
211 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
212 int attr
= slave
? IFLA_INFO_SLAVE_KIND
: IFLA_INFO_KIND
;
214 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
217 return RTA_DATA(linkinfo
[attr
]);
222 static int match_link_kind(struct rtattr
**tb
, const char *kind
, bool slave
)
224 if (!tb
[IFLA_LINKINFO
])
227 return strcmp(parse_link_kind(tb
[IFLA_LINKINFO
], slave
), kind
);
230 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
232 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
233 struct link_util
*lu
;
234 struct link_util
*slave_lu
;
237 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
238 open_json_object("linkinfo");
240 if (linkinfo
[IFLA_INFO_KIND
]) {
242 = rta_getattr_str(linkinfo
[IFLA_INFO_KIND
]);
244 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
245 print_string(PRINT_ANY
, "info_kind", " %s ", kind
);
247 lu
= get_link_kind(kind
);
248 if (lu
&& lu
->print_opt
) {
249 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
251 if (linkinfo
[IFLA_INFO_DATA
]) {
252 parse_rtattr_nested(attr
, lu
->maxattr
,
253 linkinfo
[IFLA_INFO_DATA
]);
256 open_json_object("info_data");
257 lu
->print_opt(lu
, fp
, data
);
260 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
262 open_json_object("info_xstats");
263 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
269 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
270 const char *slave_kind
271 = rta_getattr_str(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
273 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
274 print_string(PRINT_ANY
,
279 snprintf(slave
, sizeof(slave
), "%s_slave", slave_kind
);
281 slave_lu
= get_link_kind(slave
);
282 if (slave_lu
&& slave_lu
->print_opt
) {
283 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
285 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
286 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
287 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
290 open_json_object("info_slave_data");
291 slave_lu
->print_opt(slave_lu
, fp
, data
);
298 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
300 struct rtattr
*inet6_attr
;
301 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
303 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
307 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
309 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
310 __u8 mode
= rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
314 case IN6_ADDR_GEN_MODE_EUI64
:
315 print_string(PRINT_ANY
,
316 "inet6_addr_gen_mode",
320 case IN6_ADDR_GEN_MODE_NONE
:
321 print_string(PRINT_ANY
,
322 "inet6_addr_gen_mode",
326 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
327 print_string(PRINT_ANY
,
328 "inet6_addr_gen_mode",
332 case IN6_ADDR_GEN_MODE_RANDOM
:
333 print_string(PRINT_ANY
,
334 "inet6_addr_gen_mode",
339 snprintf(b1
, sizeof(b1
), "%#.2hhx", mode
);
340 print_string(PRINT_ANY
,
341 "inet6_addr_gen_mode",
349 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
351 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
353 struct ifla_vf_mac
*vf_mac
;
354 struct ifla_vf_tx_rate
*vf_tx_rate
;
355 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
359 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
360 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
364 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
366 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
367 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
369 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
370 print_int(PRINT_ANY
, "vf", "vf %d ", vf_mac
->vf
);
371 print_string(PRINT_ANY
, "mac", "MAC %s",
372 ll_addr_n2a((unsigned char *) &vf_mac
->mac
,
373 ETH_ALEN
, 0, b1
, sizeof(b1
)));
375 if (vf
[IFLA_VF_VLAN_LIST
]) {
376 struct rtattr
*i
, *vfvlanlist
= vf
[IFLA_VF_VLAN_LIST
];
377 int rem
= RTA_PAYLOAD(vfvlanlist
);
379 open_json_array(PRINT_JSON
, "vlan_list");
380 for (i
= RTA_DATA(vfvlanlist
);
381 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
382 struct ifla_vf_vlan_info
*vf_vlan_info
= RTA_DATA(i
);
385 open_json_object(NULL
);
386 if (vf_vlan_info
->vlan
)
391 if (vf_vlan_info
->qos
)
396 if (vf_vlan_info
->vlan_proto
&&
397 vf_vlan_info
->vlan_proto
!= htons(ETH_P_8021Q
))
398 print_string(PRINT_ANY
,
400 ", vlan protocol %s",
402 vf_vlan_info
->vlan_proto
,
406 close_json_array(PRINT_JSON
, NULL
);
408 struct ifla_vf_vlan
*vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
416 print_int(PRINT_ANY
, "qos", ", qos %d", vf_vlan
->qos
);
419 if (vf_tx_rate
->rate
)
422 ", tx rate %d (Mbps)",
425 if (vf
[IFLA_VF_RATE
]) {
426 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
427 int max_tx
= vf_rate
->max_tx_rate
;
428 int min_tx
= vf_rate
->min_tx_rate
;
430 if (is_json_context()) {
431 open_json_object("rate");
432 print_int(PRINT_JSON
, "max_tx", NULL
, max_tx
);
433 print_int(PRINT_ANY
, "min_tx", NULL
, min_tx
);
437 fprintf(fp
, ", max_tx_rate %dMbps", max_tx
);
439 fprintf(fp
, ", min_tx_rate %dMbps", min_tx
);
443 if (vf
[IFLA_VF_SPOOFCHK
]) {
444 struct ifla_vf_spoofchk
*vf_spoofchk
=
445 RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
447 if (vf_spoofchk
->setting
!= -1)
448 print_bool(PRINT_ANY
,
450 vf_spoofchk
->setting
?
451 ", spoof checking on" : ", spoof checking off",
452 vf_spoofchk
->setting
);
455 if (vf
[IFLA_VF_LINK_STATE
]) {
456 struct ifla_vf_link_state
*vf_linkstate
=
457 RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
459 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
460 print_string(PRINT_ANY
,
464 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
465 print_string(PRINT_ANY
,
470 print_string(PRINT_ANY
,
476 if (vf
[IFLA_VF_TRUST
]) {
477 struct ifla_vf_trust
*vf_trust
= RTA_DATA(vf
[IFLA_VF_TRUST
]);
479 if (vf_trust
->setting
!= -1)
480 print_bool(PRINT_ANY
,
482 vf_trust
->setting
? ", trust on" : ", trust off",
486 if (vf
[IFLA_VF_RSS_QUERY_EN
]) {
487 struct ifla_vf_rss_query_en
*rss_query
=
488 RTA_DATA(vf
[IFLA_VF_RSS_QUERY_EN
]);
490 if (rss_query
->setting
!= -1)
491 print_bool(PRINT_ANY
,
493 rss_query
->setting
? ", query_rss on"
498 if (vf
[IFLA_VF_STATS
] && show_stats
)
499 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
502 void print_num(FILE *fp
, unsigned int width
, uint64_t count
)
504 const char *prefix
= "kMGTPE";
505 const unsigned int base
= use_iec
? 1024 : 1000;
508 uint8_t precision
= 2;
511 if (!human_readable
|| count
< base
) {
512 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
516 /* increase value by a factor of 1000/1024 and print
517 * if result is something a human can read
521 if (count
/ base
< powi
)
529 /* try to guess a good number of digits for precision */
530 for (; precision
> 0; precision
--) {
532 if (count
/ powi
< powj
)
536 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
537 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
539 fprintf(fp
, "%-*s ", width
, buf
);
542 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
544 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1];
546 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
547 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
551 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfstats
);
553 if (is_json_context()) {
554 open_json_object("stats");
557 open_json_object("rx");
558 print_uint(PRINT_JSON
, "bytes", NULL
,
559 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
560 print_uint(PRINT_JSON
, "packets", NULL
,
561 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
562 print_uint(PRINT_JSON
, "multicast", NULL
,
563 rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
564 print_uint(PRINT_JSON
, "broadcast", NULL
,
565 rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
569 open_json_object("tx");
570 print_uint(PRINT_JSON
, "tx_bytes", NULL
,
571 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
572 print_uint(PRINT_JSON
, "tx_packets", NULL
,
573 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
578 fprintf(fp
, "%s", _SL_
);
579 fprintf(fp
, " RX: bytes packets mcast bcast %s", _SL_
);
582 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
583 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
584 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
585 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
588 fprintf(fp
, "%s", _SL_
);
589 fprintf(fp
, " TX: bytes packets %s", _SL_
);
592 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
593 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
597 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
598 const struct rtattr
*carrier_changes
)
600 if (is_json_context()) {
601 open_json_object("stats64");
604 open_json_object("rx");
605 print_uint(PRINT_JSON
, "bytes", NULL
, s
->rx_bytes
);
606 print_uint(PRINT_JSON
, "packets", NULL
, s
->rx_packets
);
607 print_uint(PRINT_JSON
, "errors", NULL
, s
->rx_errors
);
608 print_uint(PRINT_JSON
, "dropped", NULL
, s
->rx_dropped
);
609 print_uint(PRINT_JSON
, "over_errors", NULL
, s
->rx_over_errors
);
610 print_uint(PRINT_JSON
, "multicast", NULL
, s
->multicast
);
611 if (s
->rx_compressed
)
612 print_uint(PRINT_JSON
,
614 NULL
, s
->rx_compressed
);
617 if (show_stats
> 1) {
618 print_uint(PRINT_JSON
,
620 NULL
, s
->rx_length_errors
);
621 print_uint(PRINT_JSON
,
623 NULL
, s
->rx_crc_errors
);
624 print_uint(PRINT_JSON
,
626 NULL
, s
->rx_frame_errors
);
627 print_uint(PRINT_JSON
,
629 NULL
, s
->rx_fifo_errors
);
630 print_uint(PRINT_JSON
,
632 NULL
, s
->rx_missed_errors
);
634 print_uint(PRINT_JSON
,
635 "nohandler", NULL
, s
->rx_nohandler
);
640 open_json_object("tx");
641 print_uint(PRINT_JSON
, "bytes", NULL
, s
->tx_bytes
);
642 print_uint(PRINT_JSON
, "packets", NULL
, s
->tx_packets
);
643 print_uint(PRINT_JSON
, "errors", NULL
, s
->tx_errors
);
644 print_uint(PRINT_JSON
, "dropped", NULL
, s
->tx_dropped
);
645 print_uint(PRINT_JSON
,
647 NULL
, s
->tx_carrier_errors
);
648 print_uint(PRINT_JSON
, "collisions", NULL
, s
->collisions
);
649 if (s
->tx_compressed
)
650 print_uint(PRINT_JSON
,
652 NULL
, s
->tx_compressed
);
655 if (show_stats
> 1) {
656 print_uint(PRINT_JSON
,
658 NULL
, s
->tx_aborted_errors
);
659 print_uint(PRINT_JSON
,
661 NULL
, s
->tx_fifo_errors
);
662 print_uint(PRINT_JSON
,
664 NULL
, s
->tx_window_errors
);
665 print_uint(PRINT_JSON
,
667 NULL
, s
->tx_heartbeat_errors
);
669 print_uint(PRINT_JSON
, "carrier_changes", NULL
,
670 rta_getattr_u32(carrier_changes
));
677 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
678 s
->rx_compressed
? "compressed" : "", _SL_
);
681 print_num(fp
, 10, s
->rx_bytes
);
682 print_num(fp
, 8, s
->rx_packets
);
683 print_num(fp
, 7, s
->rx_errors
);
684 print_num(fp
, 7, s
->rx_dropped
);
685 print_num(fp
, 7, s
->rx_over_errors
);
686 print_num(fp
, 7, s
->multicast
);
687 if (s
->rx_compressed
)
688 print_num(fp
, 7, s
->rx_compressed
);
691 if (show_stats
> 1) {
692 fprintf(fp
, "%s", _SL_
);
693 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
694 s
->rx_nohandler
? " nohandler" : "", _SL_
);
697 print_num(fp
, 8, s
->rx_length_errors
);
698 print_num(fp
, 7, s
->rx_crc_errors
);
699 print_num(fp
, 7, s
->rx_frame_errors
);
700 print_num(fp
, 7, s
->rx_fifo_errors
);
701 print_num(fp
, 7, s
->rx_missed_errors
);
703 print_num(fp
, 7, s
->rx_nohandler
);
706 fprintf(fp
, "%s", _SL_
);
709 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
710 s
->tx_compressed
? "compressed" : "", _SL_
);
713 print_num(fp
, 10, s
->tx_bytes
);
714 print_num(fp
, 8, s
->tx_packets
);
715 print_num(fp
, 7, s
->tx_errors
);
716 print_num(fp
, 7, s
->tx_dropped
);
717 print_num(fp
, 7, s
->tx_carrier_errors
);
718 print_num(fp
, 7, s
->collisions
);
719 if (s
->tx_compressed
)
720 print_num(fp
, 7, s
->tx_compressed
);
723 if (show_stats
> 1) {
724 fprintf(fp
, "%s", _SL_
);
725 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
727 fprintf(fp
, " transns");
728 fprintf(fp
, "%s", _SL_
);
731 print_num(fp
, 8, s
->tx_aborted_errors
);
732 print_num(fp
, 7, s
->tx_fifo_errors
);
733 print_num(fp
, 7, s
->tx_window_errors
);
734 print_num(fp
, 7, s
->tx_heartbeat_errors
);
737 rta_getattr_u32(carrier_changes
));
742 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
743 const struct rtattr
*carrier_changes
)
745 if (is_json_context()) {
746 open_json_object("stats");
749 open_json_object("rx");
750 print_uint(PRINT_JSON
, "bytes", NULL
, s
->rx_bytes
);
751 print_uint(PRINT_JSON
, "packets", NULL
, s
->rx_packets
);
752 print_uint(PRINT_JSON
, "errors", NULL
, s
->rx_errors
);
753 print_uint(PRINT_JSON
, "dropped", NULL
, s
->rx_dropped
);
754 print_uint(PRINT_JSON
, "over_errors", NULL
, s
->rx_over_errors
);
755 print_uint(PRINT_JSON
, "multicast", NULL
, s
->multicast
);
756 if (s
->rx_compressed
)
757 print_int(PRINT_JSON
,
759 NULL
, s
->rx_compressed
);
762 if (show_stats
> 1) {
763 print_uint(PRINT_JSON
,
765 NULL
, s
->rx_length_errors
);
766 print_uint(PRINT_JSON
,
768 NULL
, s
->rx_crc_errors
);
769 print_uint(PRINT_JSON
,
771 NULL
, s
->rx_frame_errors
);
772 print_uint(PRINT_JSON
,
774 NULL
, s
->rx_fifo_errors
);
775 print_uint(PRINT_JSON
,
777 NULL
, s
->rx_missed_errors
);
779 print_int(PRINT_JSON
,
781 NULL
, s
->rx_nohandler
);
786 open_json_object("tx");
787 print_uint(PRINT_JSON
, "bytes", NULL
, s
->tx_bytes
);
788 print_uint(PRINT_JSON
, "packets", NULL
, s
->tx_packets
);
789 print_uint(PRINT_JSON
, "errors", NULL
, s
->tx_errors
);
790 print_uint(PRINT_JSON
, "dropped", NULL
, s
->tx_dropped
);
791 print_uint(PRINT_JSON
,
793 NULL
, s
->tx_carrier_errors
);
794 print_uint(PRINT_JSON
, "collisions", NULL
, s
->collisions
);
795 if (s
->tx_compressed
)
796 print_int(PRINT_JSON
,
798 NULL
, s
->tx_compressed
);
801 if (show_stats
> 1) {
802 print_uint(PRINT_JSON
,
804 NULL
, s
->tx_aborted_errors
);
805 print_uint(PRINT_JSON
,
807 NULL
, s
->tx_fifo_errors
);
808 print_uint(PRINT_JSON
,
810 NULL
, s
->tx_window_errors
);
811 print_uint(PRINT_JSON
,
813 NULL
, s
->tx_heartbeat_errors
);
815 print_uint(PRINT_JSON
,
818 rta_getattr_u32(carrier_changes
));
825 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
826 s
->rx_compressed
? "compressed" : "", _SL_
);
830 print_num(fp
, 10, s
->rx_bytes
);
831 print_num(fp
, 8, s
->rx_packets
);
832 print_num(fp
, 7, s
->rx_errors
);
833 print_num(fp
, 7, s
->rx_dropped
);
834 print_num(fp
, 7, s
->rx_over_errors
);
835 print_num(fp
, 7, s
->multicast
);
836 if (s
->rx_compressed
)
837 print_num(fp
, 7, s
->rx_compressed
);
840 if (show_stats
> 1) {
841 fprintf(fp
, "%s", _SL_
);
842 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
843 s
->rx_nohandler
? " nohandler" : "", _SL_
);
845 print_num(fp
, 8, s
->rx_length_errors
);
846 print_num(fp
, 7, s
->rx_crc_errors
);
847 print_num(fp
, 7, s
->rx_frame_errors
);
848 print_num(fp
, 7, s
->rx_fifo_errors
);
849 print_num(fp
, 7, s
->rx_missed_errors
);
851 print_num(fp
, 7, s
->rx_nohandler
);
853 fprintf(fp
, "%s", _SL_
);
856 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
857 s
->tx_compressed
? "compressed" : "", _SL_
);
860 print_num(fp
, 10, s
->tx_bytes
);
861 print_num(fp
, 8, s
->tx_packets
);
862 print_num(fp
, 7, s
->tx_errors
);
863 print_num(fp
, 7, s
->tx_dropped
);
864 print_num(fp
, 7, s
->tx_carrier_errors
);
865 print_num(fp
, 7, s
->collisions
);
866 if (s
->tx_compressed
)
867 print_num(fp
, 7, s
->tx_compressed
);
870 if (show_stats
> 1) {
871 fprintf(fp
, "%s", _SL_
);
872 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
874 fprintf(fp
, " transns");
875 fprintf(fp
, "%s", _SL_
);
878 print_num(fp
, 8, s
->tx_aborted_errors
);
879 print_num(fp
, 7, s
->tx_fifo_errors
);
880 print_num(fp
, 7, s
->tx_window_errors
);
881 print_num(fp
, 7, s
->tx_heartbeat_errors
);
884 rta_getattr_u32(carrier_changes
));
889 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
891 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
893 if (tb
[IFLA_STATS64
]) {
894 struct rtnl_link_stats64 stats
= { 0 };
896 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS64
]),
897 MIN(RTA_PAYLOAD(tb
[IFLA_STATS64
]), sizeof(stats
)));
899 print_link_stats64(fp
, &stats
, carrier_changes
);
900 } else if (tb
[IFLA_STATS
]) {
901 struct rtnl_link_stats stats
= { 0 };
903 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS
]),
904 MIN(RTA_PAYLOAD(tb
[IFLA_STATS
]), sizeof(stats
)));
906 print_link_stats32(fp
, &stats
, carrier_changes
);
910 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
912 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
913 struct rtattr
*tb
[IFLA_MAX
+1];
915 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
916 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
917 __print_link_stats(fp
, tb
);
918 fprintf(fp
, "%s", _SL_
);
921 int print_linkinfo_brief(const struct sockaddr_nl
*who
,
922 struct nlmsghdr
*n
, void *arg
)
924 FILE *fp
= (FILE *)arg
;
925 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
926 struct rtattr
*tb
[IFLA_MAX
+1];
927 int len
= n
->nlmsg_len
;
929 char buf
[32] = { 0, };
930 unsigned int m_flag
= 0;
932 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
935 len
-= NLMSG_LENGTH(sizeof(*ifi
));
939 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
941 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
944 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
945 if (tb
[IFLA_IFNAME
] == NULL
) {
946 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
949 name
= rta_getattr_str(tb
[IFLA_IFNAME
]);
953 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
954 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
957 if (tb
[IFLA_GROUP
]) {
958 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
960 if (filter
.group
!= -1 && group
!= filter
.group
)
964 if (tb
[IFLA_MASTER
]) {
965 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
967 if (filter
.master
> 0 && master
!= filter
.master
)
969 } else if (filter
.master
> 0)
972 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
975 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
978 if (n
->nlmsg_type
== RTM_DELLINK
)
979 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
983 int iflink
= rta_getattr_u32(tb
[IFLA_LINK
]);
986 snprintf(buf
, sizeof(buf
), "%s@NONE", name
);
987 print_null(PRINT_JSON
, "link", NULL
, NULL
);
989 const char *link
= ll_idx_n2a(iflink
, b1
);
991 print_string(PRINT_JSON
, "link", NULL
, link
);
992 snprintf(buf
, sizeof(buf
), "%s@%s", name
, link
);
993 m_flag
= ll_index_to_flags(iflink
);
994 m_flag
= !(m_flag
& IFF_UP
);
997 snprintf(buf
, sizeof(buf
), "%s", name
);
999 print_string(PRINT_FP
, NULL
, "%-16s ", buf
);
1000 print_string(PRINT_JSON
, "ifname", NULL
, name
);
1002 if (tb
[IFLA_OPERSTATE
])
1003 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
1005 if (filter
.family
== AF_PACKET
) {
1008 if (tb
[IFLA_ADDRESS
]) {
1009 print_color_string(PRINT_ANY
, COLOR_MAC
,
1012 RTA_DATA(tb
[IFLA_ADDRESS
]),
1013 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
1019 if (filter
.family
== AF_PACKET
) {
1020 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
1021 print_string(PRINT_FP
, NULL
, "%s", "\n");
1028 static const char *link_events
[] = {
1029 [IFLA_EVENT_NONE
] = "NONE",
1030 [IFLA_EVENT_REBOOT
] = "REBOOT",
1031 [IFLA_EVENT_FEATURES
] = "FEATURE CHANGE",
1032 [IFLA_EVENT_BONDING_FAILOVER
] = "BONDING FAILOVER",
1033 [IFLA_EVENT_NOTIFY_PEERS
] = "NOTIFY PEERS",
1034 [IFLA_EVENT_IGMP_RESEND
] = "RESEND IGMP",
1035 [IFLA_EVENT_BONDING_OPTIONS
] = "BONDING OPTION"
1038 static void print_link_event(FILE *f
, __u32 event
)
1040 if (event
>= ARRAY_SIZE(link_events
))
1041 print_int(PRINT_ANY
, "event", "event %d ", event
);
1044 print_string(PRINT_ANY
,
1045 "event", "event %s ",
1046 link_events
[event
]);
1050 int print_linkinfo(const struct sockaddr_nl
*who
,
1051 struct nlmsghdr
*n
, void *arg
)
1053 FILE *fp
= (FILE *)arg
;
1054 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
1055 struct rtattr
*tb
[IFLA_MAX
+1];
1056 int len
= n
->nlmsg_len
;
1057 unsigned int m_flag
= 0;
1059 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
1062 len
-= NLMSG_LENGTH(sizeof(*ifi
));
1066 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
1068 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1071 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1072 if (tb
[IFLA_IFNAME
] == NULL
)
1073 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
1076 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
1077 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
1080 if (tb
[IFLA_GROUP
]) {
1081 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
1083 if (filter
.group
!= -1 && group
!= filter
.group
)
1087 if (tb
[IFLA_MASTER
]) {
1088 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
1090 if (filter
.master
> 0 && master
!= filter
.master
)
1092 } else if (filter
.master
> 0)
1095 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
1098 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
1101 if (n
->nlmsg_type
== RTM_DELLINK
)
1102 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
1104 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
1105 if (tb
[IFLA_IFNAME
]) {
1106 print_color_string(PRINT_ANY
,
1109 rta_getattr_str(tb
[IFLA_IFNAME
]));
1111 print_null(PRINT_JSON
, "ifname", NULL
, NULL
);
1112 print_color_null(PRINT_FP
, COLOR_IFNAME
,
1113 "ifname", "%s", "<nil>");
1116 if (tb
[IFLA_LINK
]) {
1117 int iflink
= rta_getattr_u32(tb
[IFLA_LINK
]);
1120 print_null(PRINT_ANY
, "link", "@%s: ", "NONE");
1122 if (tb
[IFLA_LINK_NETNSID
])
1123 print_int(PRINT_ANY
,
1124 "link_index", "@if%d: ", iflink
);
1128 print_string(PRINT_ANY
,
1131 ll_idx_n2a(iflink
, b1
));
1132 m_flag
= ll_index_to_flags(iflink
);
1133 m_flag
= !(m_flag
& IFF_UP
);
1137 print_string(PRINT_FP
, NULL
, ": ", NULL
);
1139 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
1142 print_int(PRINT_ANY
,
1144 rta_getattr_u32(tb
[IFLA_MTU
]));
1146 xdp_dump(fp
, tb
[IFLA_XDP
], do_link
, false);
1148 print_string(PRINT_ANY
,
1151 rta_getattr_str(tb
[IFLA_QDISC
]));
1152 if (tb
[IFLA_MASTER
]) {
1155 print_string(PRINT_ANY
,
1158 ll_idx_n2a(rta_getattr_u32(tb
[IFLA_MASTER
]), b1
));
1161 if (tb
[IFLA_OPERSTATE
])
1162 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
1164 if (do_link
&& tb
[IFLA_LINKMODE
])
1165 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
1167 if (tb
[IFLA_GROUP
]) {
1169 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
1171 print_string(PRINT_ANY
,
1174 rtnl_group_n2a(group
, b1
, sizeof(b1
)));
1177 if (filter
.showqueue
)
1178 print_queuelen(fp
, tb
);
1181 print_link_event(fp
, rta_getattr_u32(tb
[IFLA_EVENT
]));
1183 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
1186 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
1187 print_string(PRINT_ANY
,
1190 ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
1191 if (tb
[IFLA_ADDRESS
]) {
1192 print_color_string(PRINT_ANY
,
1196 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
1197 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
1201 if (tb
[IFLA_BROADCAST
]) {
1202 if (ifi
->ifi_flags
&IFF_POINTOPOINT
) {
1203 print_string(PRINT_FP
, NULL
, " peer ", NULL
);
1204 print_bool(PRINT_JSON
,
1205 "link_pointtopoint", NULL
, true);
1207 print_string(PRINT_FP
, NULL
, " brd ", NULL
);
1209 print_color_string(PRINT_ANY
,
1213 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
1214 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
1220 if (tb
[IFLA_LINK_NETNSID
]) {
1221 int id
= rta_getattr_u32(tb
[IFLA_LINK_NETNSID
]);
1223 if (is_json_context()) {
1224 print_int(PRINT_JSON
, "link_netnsid", NULL
, id
);
1227 print_int(PRINT_FP
, NULL
,
1228 " link-netnsid %d", id
);
1230 print_string(PRINT_FP
, NULL
,
1231 " link-netnsid %s", "unknown");
1235 if (tb
[IFLA_PROTO_DOWN
]) {
1236 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
1237 print_bool(PRINT_ANY
,
1238 "proto_down", " protodown on ", true);
1242 if (tb
[IFLA_PROMISCUITY
])
1243 print_uint(PRINT_ANY
,
1246 rta_getattr_u32(tb
[IFLA_PROMISCUITY
]));
1248 if (tb
[IFLA_LINKINFO
])
1249 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
1251 if (do_link
&& tb
[IFLA_AF_SPEC
])
1252 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
1254 if (tb
[IFLA_NUM_TX_QUEUES
])
1255 print_uint(PRINT_ANY
,
1258 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
1260 if (tb
[IFLA_NUM_RX_QUEUES
])
1261 print_uint(PRINT_ANY
,
1264 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
1266 if (tb
[IFLA_GSO_MAX_SIZE
])
1267 print_uint(PRINT_ANY
,
1270 rta_getattr_u32(tb
[IFLA_GSO_MAX_SIZE
]));
1272 if (tb
[IFLA_GSO_MAX_SEGS
])
1273 print_uint(PRINT_ANY
,
1276 rta_getattr_u32(tb
[IFLA_GSO_MAX_SEGS
]));
1278 if (tb
[IFLA_PHYS_PORT_NAME
])
1279 print_string(PRINT_ANY
,
1282 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
1284 if (tb
[IFLA_PHYS_PORT_ID
]) {
1286 print_string(PRINT_ANY
,
1290 RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
1291 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
1295 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
1297 print_string(PRINT_ANY
,
1300 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
1301 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
1306 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
1307 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
1308 print_string(PRINT_ANY
,
1311 rta_getattr_str(tb
[IFLA_IFALIAS
]));
1314 if ((do_link
|| show_details
) && tb
[IFLA_XDP
])
1315 xdp_dump(fp
, tb
[IFLA_XDP
], true, true);
1317 if (do_link
&& show_stats
) {
1318 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
1319 __print_link_stats(fp
, tb
);
1322 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
1323 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
1324 int rem
= RTA_PAYLOAD(vflist
);
1326 open_json_array(PRINT_JSON
, "vfinfo_list");
1327 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1328 open_json_object(NULL
);
1329 print_vfinfo(fp
, i
);
1330 close_json_object();
1332 close_json_array(PRINT_JSON
, NULL
);
1335 print_string(PRINT_FP
, NULL
, "\n", NULL
);
1340 static int flush_update(void)
1344 * Note that the kernel may delete multiple addresses for one
1345 * delete request (e.g. if ipv4 address promotion is disabled).
1346 * Since a flush operation is really a series of delete requests
1347 * its possible that we may request an address delete that has
1348 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
1349 * errors returned from a flush request
1351 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
1352 (errno
!= EADDRNOTAVAIL
)) {
1353 perror("Failed to send flush request");
1360 static int set_lifetime(unsigned int *lifetime
, char *argv
)
1362 if (strcmp(argv
, "forever") == 0)
1363 *lifetime
= INFINITY_LIFE_TIME
;
1364 else if (get_u32(lifetime
, argv
, 0))
1370 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
1371 struct rtattr
*ifa_flags_attr
)
1373 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
1377 /* Mapping from argument to address flag mask */
1380 unsigned long value
;
1381 } ifa_flag_names
[] = {
1382 { "secondary", IFA_F_SECONDARY
},
1383 { "temporary", IFA_F_SECONDARY
},
1384 { "nodad", IFA_F_NODAD
},
1385 { "optimistic", IFA_F_OPTIMISTIC
},
1386 { "dadfailed", IFA_F_DADFAILED
},
1387 { "home", IFA_F_HOMEADDRESS
},
1388 { "deprecated", IFA_F_DEPRECATED
},
1389 { "tentative", IFA_F_TENTATIVE
},
1390 { "permanent", IFA_F_PERMANENT
},
1391 { "mngtmpaddr", IFA_F_MANAGETEMPADDR
},
1392 { "noprefixroute", IFA_F_NOPREFIXROUTE
},
1393 { "autojoin", IFA_F_MCAUTOJOIN
},
1394 { "stable-privacy", IFA_F_STABLE_PRIVACY
},
1397 static void print_ifa_flags(FILE *fp
, const struct ifaddrmsg
*ifa
,
1402 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1403 unsigned long mask
= ifa_flag_names
[i
].value
;
1405 if (mask
== IFA_F_PERMANENT
) {
1406 if (!(flags
& mask
))
1407 print_bool(PRINT_ANY
,
1408 "dynamic", "dynamic ", true);
1409 } else if (flags
& mask
) {
1410 if (mask
== IFA_F_SECONDARY
&&
1411 ifa
->ifa_family
== AF_INET6
) {
1412 print_bool(PRINT_ANY
,
1413 "temporary", "temporary ", true);
1415 print_string(PRINT_FP
, NULL
,
1416 "%s ", ifa_flag_names
[i
].name
);
1417 print_bool(PRINT_JSON
,
1418 ifa_flag_names
[i
].name
, NULL
, true);
1426 if (is_json_context()) {
1429 snprintf(b1
, sizeof(b1
), "%02x", flags
);
1430 print_string(PRINT_JSON
, "ifa_flags", NULL
, b1
);
1432 fprintf(fp
, "flags %02x ", flags
);
1438 static int get_filter(const char *arg
)
1443 if (strcmp(arg
, "dynamic") == 0) {
1444 filter
.flags
&= ~IFA_F_PERMANENT
;
1445 filter
.flagmask
|= IFA_F_PERMANENT
;
1446 } else if (strcmp(arg
, "primary") == 0) {
1447 filter
.flags
&= ~IFA_F_SECONDARY
;
1448 filter
.flagmask
|= IFA_F_SECONDARY
;
1449 } else if (*arg
== '-') {
1450 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1451 if (strcmp(arg
+ 1, ifa_flag_names
[i
].name
))
1454 filter
.flags
&= ifa_flag_names
[i
].value
;
1455 filter
.flagmask
|= ifa_flag_names
[i
].value
;
1461 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1462 if (strcmp(arg
, ifa_flag_names
[i
].name
))
1464 filter
.flags
|= ifa_flag_names
[i
].value
;
1465 filter
.flagmask
|= ifa_flag_names
[i
].value
;
1474 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1478 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1479 int len
= n
->nlmsg_len
;
1480 unsigned int ifa_flags
;
1481 struct rtattr
*rta_tb
[IFA_MAX
+1];
1485 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
1487 len
-= NLMSG_LENGTH(sizeof(*ifa
));
1489 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1493 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
1496 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
1497 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1499 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1501 if (!rta_tb
[IFA_LOCAL
])
1502 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1503 if (!rta_tb
[IFA_ADDRESS
])
1504 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1506 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1508 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1510 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1516 if (rta_tb
[IFA_LABEL
])
1517 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
1519 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1520 if (fnmatch(filter
.label
, label
, 0) != 0)
1524 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1527 if (inet_addr_match_rta(&filter
.pfx
, rta_tb
[IFA_LOCAL
]))
1530 if (filter
.flushb
) {
1531 struct nlmsghdr
*fn
;
1533 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1537 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1538 memcpy(fn
, n
, n
->nlmsg_len
);
1539 fn
->nlmsg_type
= RTM_DELADDR
;
1540 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1541 fn
->nlmsg_seq
= ++rth
.seq
;
1542 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1548 if (n
->nlmsg_type
== RTM_DELADDR
)
1549 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
1554 if (filter
.oneline
|| filter
.flushb
) {
1555 const char *dev
= ll_index_to_name(ifa
->ifa_index
);
1557 if (is_json_context()) {
1558 print_int(PRINT_JSON
,
1559 "index", NULL
, ifa
->ifa_index
);
1560 print_string(PRINT_JSON
, "dev", NULL
, dev
);
1562 fprintf(fp
, "%u: %s", ifa
->ifa_index
, dev
);
1566 name
= family_name(ifa
->ifa_family
);
1568 print_string(PRINT_ANY
, "family", " %s ", name
);
1570 print_int(PRINT_ANY
, "family_index", " family %d ",
1575 if (rta_tb
[IFA_LOCAL
]) {
1576 print_color_string(PRINT_ANY
,
1577 ifa_family_color(ifa
->ifa_family
),
1579 format_host_rta(ifa
->ifa_family
,
1580 rta_tb
[IFA_LOCAL
]));
1581 if (rta_tb
[IFA_ADDRESS
] &&
1582 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1583 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1584 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1585 print_string(PRINT_FP
, NULL
, " %s ", "peer");
1586 print_color_string(PRINT_ANY
,
1587 ifa_family_color(ifa
->ifa_family
),
1590 format_host_rta(ifa
->ifa_family
,
1591 rta_tb
[IFA_ADDRESS
]));
1593 print_int(PRINT_ANY
, "prefixlen", "/%d ", ifa
->ifa_prefixlen
);
1599 if (rta_tb
[IFA_BROADCAST
]) {
1600 print_string(PRINT_FP
, NULL
, "%s ", "brd");
1601 print_color_string(PRINT_ANY
,
1602 ifa_family_color(ifa
->ifa_family
),
1605 format_host_rta(ifa
->ifa_family
,
1606 rta_tb
[IFA_BROADCAST
]));
1609 if (rta_tb
[IFA_ANYCAST
]) {
1610 print_string(PRINT_FP
, NULL
, "%s ", "any");
1611 print_color_string(PRINT_ANY
,
1612 ifa_family_color(ifa
->ifa_family
),
1615 format_host_rta(ifa
->ifa_family
,
1616 rta_tb
[IFA_ANYCAST
]));
1619 print_string(PRINT_ANY
,
1622 rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1624 print_ifa_flags(fp
, ifa
, ifa_flags
);
1626 if (rta_tb
[IFA_LABEL
])
1627 print_string(PRINT_ANY
,
1630 rta_getattr_str(rta_tb
[IFA_LABEL
]));
1632 if (rta_tb
[IFA_CACHEINFO
]) {
1633 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1635 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
1636 print_string(PRINT_FP
, NULL
, " valid_lft ", NULL
);
1638 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
) {
1639 print_uint(PRINT_JSON
,
1641 NULL
, INFINITY_LIFE_TIME
);
1642 print_string(PRINT_FP
, NULL
, "%s", "forever");
1644 print_uint(PRINT_ANY
,
1645 "valid_life_time", "%usec", ci
->ifa_valid
);
1648 print_string(PRINT_FP
, NULL
, " preferred_lft ", NULL
);
1649 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
) {
1650 print_uint(PRINT_JSON
,
1651 "preferred_life_time",
1652 NULL
, INFINITY_LIFE_TIME
);
1653 print_string(PRINT_FP
, NULL
, "%s", "forever");
1655 if (ifa_flags
& IFA_F_DEPRECATED
)
1656 print_int(PRINT_ANY
,
1657 "preferred_life_time",
1661 print_uint(PRINT_ANY
,
1662 "preferred_life_time",
1667 print_string(PRINT_FP
, NULL
, "%s", "\n");
1673 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1674 struct nlmsg_list
*ainfo
, FILE *fp
)
1676 open_json_array(PRINT_JSON
, "addr_info");
1677 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1678 struct nlmsghdr
*n
= &ainfo
->h
;
1679 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1681 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1684 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ifa
)))
1687 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1688 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1691 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1694 open_json_object(NULL
);
1695 print_addrinfo(NULL
, n
, fp
);
1696 close_json_object();
1698 close_json_array(PRINT_JSON
, NULL
);
1701 print_string(PRINT_FP
, NULL
, "%s", "\n");
1708 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1711 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1712 struct nlmsg_list
*h
;
1714 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1718 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1722 lchain
->tail
->next
= h
;
1727 ll_remember_index(who
, n
, NULL
);
1731 static __u32 ipadd_dump_magic
= 0x47361222;
1733 static int ipadd_save_prep(void)
1737 if (isatty(STDOUT_FILENO
)) {
1738 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1742 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1743 if (ret
!= sizeof(ipadd_dump_magic
)) {
1744 fprintf(stderr
, "Can't write magic to dump file\n");
1751 static int ipadd_dump_check_magic(void)
1756 if (isatty(STDIN_FILENO
)) {
1757 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1761 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1762 if (magic
!= ipadd_dump_magic
) {
1763 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1770 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1775 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1776 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1777 fprintf(stderr
, "Short write while saving nlmsg\n");
1781 return ret
== n
->nlmsg_len
? 0 : ret
;
1784 static int show_handler(const struct sockaddr_nl
*nl
,
1785 struct rtnl_ctrl_data
*ctrl
,
1786 struct nlmsghdr
*n
, void *arg
)
1788 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1790 open_json_object(NULL
);
1791 print_int(PRINT_ANY
, "index", "if%d:\n", ifa
->ifa_index
);
1792 print_addrinfo(NULL
, n
, stdout
);
1793 close_json_object();
1797 static int ipaddr_showdump(void)
1801 if (ipadd_dump_check_magic())
1805 open_json_object(NULL
);
1806 open_json_array(PRINT_JSON
, "addr_info");
1808 err
= rtnl_from_file(stdin
, &show_handler
, NULL
);
1810 close_json_array(PRINT_JSON
, NULL
);
1811 close_json_object();
1817 static int restore_handler(const struct sockaddr_nl
*nl
,
1818 struct rtnl_ctrl_data
*ctrl
,
1819 struct nlmsghdr
*n
, void *arg
)
1823 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1827 ret
= rtnl_talk(&rth
, n
, NULL
);
1828 if ((ret
< 0) && (errno
== EEXIST
))
1834 static int ipaddr_restore(void)
1836 if (ipadd_dump_check_magic())
1839 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1842 void free_nlmsg_chain(struct nlmsg_chain
*info
)
1844 struct nlmsg_list
*l
, *n
;
1846 for (l
= info
->head
; l
; l
= n
) {
1852 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1854 struct nlmsg_list
*l
, **lp
;
1857 while ((l
= *lp
) != NULL
) {
1859 int missing_net_address
= 1;
1860 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1861 struct nlmsg_list
*a
;
1863 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1864 struct nlmsghdr
*n
= &a
->h
;
1865 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1866 struct rtattr
*tb
[IFA_MAX
+ 1];
1867 unsigned int ifa_flags
;
1869 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1871 missing_net_address
= 0;
1872 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1874 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1877 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1878 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1880 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1882 if (filter
.pfx
.family
|| filter
.label
) {
1883 struct rtattr
*rta
=
1884 tb
[IFA_LOCAL
] ? : tb
[IFA_ADDRESS
];
1886 if (inet_addr_match_rta(&filter
.pfx
, rta
))
1894 label
= RTA_DATA(tb
[IFA_LABEL
]);
1896 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1897 if (fnmatch(filter
.label
, label
, 0) != 0)
1905 if (missing_net_address
&&
1906 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1916 static int ipaddr_flush(void)
1919 char flushb
[4096-512];
1921 filter
.flushb
= flushb
;
1923 filter
.flushe
= sizeof(flushb
);
1925 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1926 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1927 perror("Cannot send dump request");
1931 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1932 stdout
, NLM_F_DUMP_INTR
) < 0) {
1933 fprintf(stderr
, "Flush terminated\n");
1936 if (filter
.flushed
== 0) {
1940 printf("Nothing to flush.\n");
1942 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1948 if (flush_update() < 0)
1952 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1956 /* If we are flushing, and specifying primary, then we
1957 * want to flush only a single round. Otherwise, we'll
1958 * start flushing secondaries that were promoted to
1961 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1964 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1969 static int iplink_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
1973 err
= addattr32(nlh
, reqlen
, IFLA_EXT_MASK
, RTEXT_FILTER_VF
);
1977 if (filter
.master
) {
1978 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter
.master
);
1984 struct rtattr
*linkinfo
;
1986 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
1988 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, filter
.kind
,
1989 strlen(filter
.kind
));
1993 addattr_nest_end(nlh
, linkinfo
);
1999 /* fills in linfo with link data and optionally ainfo with address info
2000 * caller can walk lists as desired and must call free_nlmsg_chain for
2003 int ip_linkaddr_list(int family
, req_filter_fn_t filter_fn
,
2004 struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
2006 if (rtnl_wilddump_req_filter_fn(&rth
, preferred_family
, RTM_GETLINK
,
2008 perror("Cannot send dump request");
2012 if (rtnl_dump_filter(&rth
, store_nlmsg
, linfo
) < 0) {
2013 fprintf(stderr
, "Dump terminated\n");
2018 if (rtnl_wilddump_request(&rth
, family
, RTM_GETADDR
) < 0) {
2019 perror("Cannot send dump request");
2023 if (rtnl_dump_filter(&rth
, store_nlmsg
, ainfo
) < 0) {
2024 fprintf(stderr
, "Dump terminated\n");
2032 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
2034 struct nlmsg_chain linfo
= { NULL
, NULL
};
2035 struct nlmsg_chain _ainfo
= { NULL
, NULL
}, *ainfo
= NULL
;
2036 struct nlmsg_list
*l
;
2037 char *filter_dev
= NULL
;
2040 ipaddr_reset_filter(oneline
, 0);
2041 filter
.showqueue
= 1;
2042 filter
.family
= preferred_family
;
2045 if (action
== IPADD_FLUSH
) {
2047 fprintf(stderr
, "Flush requires arguments.\n");
2051 if (filter
.family
== AF_PACKET
) {
2052 fprintf(stderr
, "Cannot flush link addresses.\n");
2058 if (strcmp(*argv
, "to") == 0) {
2060 if (get_prefix(&filter
.pfx
, *argv
, filter
.family
))
2061 invarg("invalid \"to\"\n", *argv
);
2062 if (filter
.family
== AF_UNSPEC
)
2063 filter
.family
= filter
.pfx
.family
;
2064 } else if (strcmp(*argv
, "scope") == 0) {
2065 unsigned int scope
= 0;
2068 filter
.scopemask
= -1;
2069 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
2070 if (strcmp(*argv
, "all") != 0)
2071 invarg("invalid \"scope\"\n", *argv
);
2072 scope
= RT_SCOPE_NOWHERE
;
2073 filter
.scopemask
= 0;
2075 filter
.scope
= scope
;
2076 } else if (strcmp(*argv
, "up") == 0) {
2078 } else if (get_filter(*argv
) == 0) {
2080 } else if (strcmp(*argv
, "label") == 0) {
2082 filter
.label
= *argv
;
2083 } else if (strcmp(*argv
, "group") == 0) {
2085 if (rtnl_group_a2n(&filter
.group
, *argv
))
2086 invarg("Invalid \"group\" value\n", *argv
);
2087 } else if (strcmp(*argv
, "master") == 0) {
2091 ifindex
= ll_name_to_index(*argv
);
2093 invarg("Device does not exist\n", *argv
);
2094 filter
.master
= ifindex
;
2095 } else if (strcmp(*argv
, "vrf") == 0) {
2099 ifindex
= ll_name_to_index(*argv
);
2101 invarg("Not a valid VRF name\n", *argv
);
2102 if (!name_is_vrf(*argv
))
2103 invarg("Not a valid VRF name\n", *argv
);
2104 filter
.master
= ifindex
;
2105 } else if (strcmp(*argv
, "type") == 0) {
2109 soff
= strlen(*argv
) - strlen("_slave");
2110 if (!strcmp(*argv
+ soff
, "_slave")) {
2111 (*argv
)[soff
] = '\0';
2112 filter
.slave_kind
= *argv
;
2114 filter
.kind
= *argv
;
2117 if (strcmp(*argv
, "dev") == 0)
2119 else if (matches(*argv
, "help") == 0)
2122 duparg2("dev", *argv
);
2129 filter
.ifindex
= ll_name_to_index(filter_dev
);
2130 if (filter
.ifindex
<= 0) {
2131 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
2136 if (action
== IPADD_FLUSH
)
2137 return ipaddr_flush();
2139 if (action
== IPADD_SAVE
) {
2140 if (ipadd_save_prep())
2143 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
2144 perror("Cannot send dump request");
2148 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
2149 fprintf(stderr
, "Save terminated\n");
2157 * Initialize a json_writer and open an array object
2158 * if -json was specified.
2163 * If only filter_dev present and none of the other
2164 * link filters are present, use RTM_GETLINK to get
2167 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
2168 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
2169 perror("Cannot send link get request");
2177 if (filter
.family
!= AF_PACKET
) {
2184 if (ip_linkaddr_list(filter
.family
, iplink_filter_req
,
2185 &linfo
, ainfo
) != 0)
2188 if (filter
.family
!= AF_PACKET
)
2189 ipaddr_filter(&linfo
, ainfo
);
2191 for (l
= linfo
.head
; l
; l
= l
->next
) {
2193 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
2195 open_json_object(NULL
);
2197 if (print_linkinfo_brief(NULL
, &l
->h
, stdout
) == 0)
2198 if (filter
.family
!= AF_PACKET
)
2199 print_selected_addrinfo(ifi
,
2202 } else if (no_link
||
2203 (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
2204 if (filter
.family
!= AF_PACKET
)
2205 print_selected_addrinfo(ifi
,
2206 ainfo
->head
, stdout
);
2207 if (res
> 0 && !do_link
&& show_stats
)
2208 print_link_stats(stdout
, &l
->h
);
2210 close_json_object();
2216 free_nlmsg_chain(ainfo
);
2217 free_nlmsg_chain(&linfo
);
2223 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
2225 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
2226 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
2227 struct ifla_vf_rate
*vf_rate
;
2230 rem
= RTA_PAYLOAD(vflist
);
2232 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
2233 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
2235 if (!vf
[IFLA_VF_RATE
]) {
2236 fprintf(stderr
, "VF min/max rate API not supported\n");
2240 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
2241 if (vf_rate
->vf
== vfnum
) {
2242 *min
= vf_rate
->min_tx_rate
;
2243 *max
= vf_rate
->max_tx_rate
;
2247 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
2251 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
2253 struct nlmsg_chain linfo
= { NULL
, NULL
};
2254 struct rtattr
*tb
[IFLA_MAX
+1];
2255 struct ifinfomsg
*ifi
;
2256 struct nlmsg_list
*l
;
2260 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
2261 perror("Cannot send dump request");
2264 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
2265 fprintf(stderr
, "Dump terminated\n");
2268 for (l
= linfo
.head
; l
; l
= l
->next
) {
2270 ifi
= NLMSG_DATA(n
);
2272 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
2273 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
2276 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
2278 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
2279 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
2285 int ipaddr_list_link(int argc
, char **argv
)
2287 preferred_family
= AF_PACKET
;
2289 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
2292 void ipaddr_reset_filter(int oneline
, int ifindex
)
2294 memset(&filter
, 0, sizeof(filter
));
2295 filter
.oneline
= oneline
;
2296 filter
.ifindex
= ifindex
;
2299 static int default_scope(inet_prefix
*lcl
)
2301 if (lcl
->family
== AF_INET
) {
2302 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
2303 return RT_SCOPE_HOST
;
2308 static bool ipaddr_is_multicast(inet_prefix
*a
)
2310 if (a
->family
== AF_INET
)
2311 return IN_MULTICAST(ntohl(a
->data
[0]));
2312 else if (a
->family
== AF_INET6
)
2313 return IN6_IS_ADDR_MULTICAST(a
->data
);
2318 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
2322 struct ifaddrmsg ifa
;
2325 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
2326 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
2327 .n
.nlmsg_type
= cmd
,
2328 .ifa
.ifa_family
= preferred_family
,
2332 char *lcl_arg
= NULL
;
2333 char *valid_lftp
= NULL
;
2334 char *preferred_lftp
= NULL
;
2335 inet_prefix lcl
= {};
2342 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
2343 __u32 valid_lft
= INFINITY_LIFE_TIME
;
2344 unsigned int ifa_flags
= 0;
2347 if (strcmp(*argv
, "peer") == 0 ||
2348 strcmp(*argv
, "remote") == 0) {
2352 duparg("peer", *argv
);
2353 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
2354 peer_len
= peer
.bytelen
;
2355 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2356 req
.ifa
.ifa_family
= peer
.family
;
2357 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
2358 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
2359 } else if (matches(*argv
, "broadcast") == 0 ||
2360 strcmp(*argv
, "brd") == 0) {
2365 duparg("broadcast", *argv
);
2366 if (strcmp(*argv
, "+") == 0)
2368 else if (strcmp(*argv
, "-") == 0)
2371 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2372 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2373 req
.ifa
.ifa_family
= addr
.family
;
2374 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
2375 brd_len
= addr
.bytelen
;
2377 } else if (strcmp(*argv
, "anycast") == 0) {
2382 duparg("anycast", *argv
);
2383 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2384 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2385 req
.ifa
.ifa_family
= addr
.family
;
2386 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
2387 any_len
= addr
.bytelen
;
2388 } else if (strcmp(*argv
, "scope") == 0) {
2389 unsigned int scope
= 0;
2392 if (rtnl_rtscope_a2n(&scope
, *argv
))
2393 invarg("invalid scope value.", *argv
);
2394 req
.ifa
.ifa_scope
= scope
;
2396 } else if (strcmp(*argv
, "dev") == 0) {
2399 } else if (strcmp(*argv
, "label") == 0) {
2402 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
2403 } else if (matches(*argv
, "valid_lft") == 0) {
2405 duparg("valid_lft", *argv
);
2408 if (set_lifetime(&valid_lft
, *argv
))
2409 invarg("valid_lft value", *argv
);
2410 } else if (matches(*argv
, "preferred_lft") == 0) {
2412 duparg("preferred_lft", *argv
);
2414 preferred_lftp
= *argv
;
2415 if (set_lifetime(&preferred_lft
, *argv
))
2416 invarg("preferred_lft value", *argv
);
2417 } else if (strcmp(*argv
, "home") == 0) {
2418 ifa_flags
|= IFA_F_HOMEADDRESS
;
2419 } else if (strcmp(*argv
, "nodad") == 0) {
2420 ifa_flags
|= IFA_F_NODAD
;
2421 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
2422 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
2423 } else if (strcmp(*argv
, "noprefixroute") == 0) {
2424 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
2425 } else if (strcmp(*argv
, "autojoin") == 0) {
2426 ifa_flags
|= IFA_F_MCAUTOJOIN
;
2428 if (strcmp(*argv
, "local") == 0)
2430 if (matches(*argv
, "help") == 0)
2433 duparg2("local", *argv
);
2435 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
2436 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2437 req
.ifa
.ifa_family
= lcl
.family
;
2438 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
2439 local_len
= lcl
.bytelen
;
2443 if (ifa_flags
<= 0xff)
2444 req
.ifa
.ifa_flags
= ifa_flags
;
2446 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
2449 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
2452 if (l
&& matches(d
, l
) != 0) {
2453 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
2457 if (peer_len
== 0 && local_len
) {
2458 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
2460 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
2461 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
2462 " This special behaviour is likely to disappear in further releases,\n"
2463 " fix your scripts!\n", lcl_arg
, local_len
*8);
2466 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
2469 if (req
.ifa
.ifa_prefixlen
== 0)
2470 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
2472 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
2476 if (req
.ifa
.ifa_family
!= AF_INET
) {
2477 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
2481 if (brd
.bitlen
<= 30) {
2482 for (i
= 31; i
>= brd
.bitlen
; i
--) {
2484 brd
.data
[0] |= htonl(1<<(31-i
));
2486 brd
.data
[0] &= ~htonl(1<<(31-i
));
2488 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
2489 brd_len
= brd
.bytelen
;
2492 if (!scoped
&& cmd
!= RTM_DELADDR
)
2493 req
.ifa
.ifa_scope
= default_scope(&lcl
);
2495 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
2496 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
2500 if (valid_lftp
|| preferred_lftp
) {
2501 struct ifa_cacheinfo cinfo
= {};
2504 fprintf(stderr
, "valid_lft is zero\n");
2507 if (valid_lft
< preferred_lft
) {
2508 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
2512 cinfo
.ifa_prefered
= preferred_lft
;
2513 cinfo
.ifa_valid
= valid_lft
;
2514 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
2518 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
2519 fprintf(stderr
, "autojoin needs multicast address\n");
2523 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
2529 int do_ipaddr(int argc
, char **argv
)
2532 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
2533 if (matches(*argv
, "add") == 0)
2534 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
2535 if (matches(*argv
, "change") == 0 ||
2536 strcmp(*argv
, "chg") == 0)
2537 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2538 if (matches(*argv
, "replace") == 0)
2539 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2540 if (matches(*argv
, "delete") == 0)
2541 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2542 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2543 || matches(*argv
, "lst") == 0)
2544 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2545 if (matches(*argv
, "flush") == 0)
2546 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2547 if (matches(*argv
, "save") == 0)
2548 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2549 if (matches(*argv
, "showdump") == 0)
2550 return ipaddr_showdump();
2551 if (matches(*argv
, "restore") == 0)
2552 return ipaddr_restore();
2553 if (matches(*argv
, "help") == 0)
2555 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);