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/if_infiniband.h>
30 #include <linux/sockios.h>
31 #include <linux/net_namespace.h>
37 #include "ip_common.h"
46 static struct link_filter filter
;
49 static void usage(void) __attribute__((noreturn
));
51 static void usage(void)
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"
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");
87 static void print_link_flags(FILE *fp
, unsigned int flags
, unsigned int mdown
)
89 open_json_array(PRINT_ANY
, is_json_context() ? "flags" : "<");
90 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
91 print_string(PRINT_ANY
, NULL
,
92 flags
? "%s," : "%s", "NO-CARRIER");
93 flags
&= ~IFF_RUNNING
;
94 #define _PF(f) if (flags&IFF_##f) { \
96 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
117 print_hex(PRINT_ANY
, NULL
, "%x", flags
);
119 print_string(PRINT_ANY
, NULL
, ",%s", "M-DOWN");
120 close_json_array(PRINT_ANY
, "> ");
123 static const char *oper_states
[] = {
124 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
125 "TESTING", "DORMANT", "UP"
128 static void print_operstate(FILE *f
, __u8 state
)
130 if (state
>= ARRAY_SIZE(oper_states
)) {
131 if (is_json_context())
132 print_uint(PRINT_JSON
, "operstate_index", NULL
, state
);
134 print_0xhex(PRINT_FP
, NULL
, "state %#llx", state
);
136 print_color_string(PRINT_ANY
,
137 oper_state_color(state
),
142 if (is_json_context())
143 print_string(PRINT_JSON
,
145 NULL
, oper_states
[state
]);
147 fprintf(f
, "state ");
148 color_fprintf(f
, oper_state_color(state
),
149 "%s ", oper_states
[state
]);
154 int get_operstate(const char *name
)
158 for (i
= 0; i
< ARRAY_SIZE(oper_states
); i
++)
159 if (strcasecmp(name
, oper_states
[i
]) == 0)
164 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
169 qlen
= rta_getattr_u32(tb
[IFLA_TXQLEN
]);
171 struct ifreq ifr
= {};
172 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
177 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
178 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
180 "ioctl(SIOCGIFTXQLEN) failed: %s\n",
189 print_int(PRINT_ANY
, "txqlen", "qlen %d", qlen
);
192 static const char *link_modes
[] = {
196 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
198 unsigned int mode
= rta_getattr_u8(tb
);
200 if (mode
>= ARRAY_SIZE(link_modes
))
206 print_string(PRINT_ANY
,
212 static char *parse_link_kind(struct rtattr
*tb
, bool slave
)
214 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
215 int attr
= slave
? IFLA_INFO_SLAVE_KIND
: IFLA_INFO_KIND
;
217 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
220 return RTA_DATA(linkinfo
[attr
]);
225 static int match_link_kind(struct rtattr
**tb
, const char *kind
, bool slave
)
227 if (!tb
[IFLA_LINKINFO
])
230 return strcmp(parse_link_kind(tb
[IFLA_LINKINFO
], slave
), kind
);
233 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
235 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
236 struct link_util
*lu
;
237 struct link_util
*slave_lu
;
240 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
241 open_json_object("linkinfo");
243 if (linkinfo
[IFLA_INFO_KIND
]) {
245 = rta_getattr_str(linkinfo
[IFLA_INFO_KIND
]);
248 print_string(PRINT_ANY
, "info_kind", " %s ", kind
);
250 lu
= get_link_kind(kind
);
251 if (lu
&& lu
->print_opt
) {
252 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
254 if (linkinfo
[IFLA_INFO_DATA
]) {
255 parse_rtattr_nested(attr
, lu
->maxattr
,
256 linkinfo
[IFLA_INFO_DATA
]);
259 open_json_object("info_data");
260 lu
->print_opt(lu
, fp
, data
);
263 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
265 open_json_object("info_xstats");
266 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
272 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
273 const char *slave_kind
274 = rta_getattr_str(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
277 print_string(PRINT_ANY
,
282 snprintf(slave
, sizeof(slave
), "%s_slave", slave_kind
);
284 slave_lu
= get_link_kind(slave
);
285 if (slave_lu
&& slave_lu
->print_opt
) {
286 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
288 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
289 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
290 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
293 open_json_object("info_slave_data");
294 slave_lu
->print_opt(slave_lu
, fp
, data
);
301 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
303 struct rtattr
*inet6_attr
;
304 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
306 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
310 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
312 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
313 __u8 mode
= rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
317 case IN6_ADDR_GEN_MODE_EUI64
:
318 print_string(PRINT_ANY
,
319 "inet6_addr_gen_mode",
323 case IN6_ADDR_GEN_MODE_NONE
:
324 print_string(PRINT_ANY
,
325 "inet6_addr_gen_mode",
329 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
330 print_string(PRINT_ANY
,
331 "inet6_addr_gen_mode",
335 case IN6_ADDR_GEN_MODE_RANDOM
:
336 print_string(PRINT_ANY
,
337 "inet6_addr_gen_mode",
342 snprintf(b1
, sizeof(b1
), "%#.2hhx", mode
);
343 print_string(PRINT_ANY
,
344 "inet6_addr_gen_mode",
352 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
354 static void print_vfinfo(FILE *fp
, struct ifinfomsg
*ifi
, struct rtattr
*vfinfo
)
356 struct ifla_vf_mac
*vf_mac
;
357 struct ifla_vf_broadcast
*vf_broadcast
;
358 struct ifla_vf_tx_rate
*vf_tx_rate
;
359 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
363 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
364 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
368 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
370 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
371 vf_broadcast
= RTA_DATA(vf
[IFLA_VF_BROADCAST
]);
372 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
374 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
375 print_int(PRINT_ANY
, "vf", "vf %d ", vf_mac
->vf
);
377 print_string(PRINT_ANY
,
380 ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
382 print_color_string(PRINT_ANY
, COLOR_MAC
,
384 ll_addr_n2a((unsigned char *) &vf_mac
->mac
,
385 ifi
->ifi_type
== ARPHRD_ETHER
?
386 ETH_ALEN
: INFINIBAND_ALEN
,
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);
396 print_string(PRINT_FP
, NULL
, " brd ", NULL
);
398 print_color_string(PRINT_ANY
, COLOR_MAC
,
400 ll_addr_n2a((unsigned char *) &vf_broadcast
->broadcast
,
401 ifi
->ifi_type
== ARPHRD_ETHER
?
402 ETH_ALEN
: INFINIBAND_ALEN
,
407 if (vf
[IFLA_VF_VLAN_LIST
]) {
408 struct rtattr
*i
, *vfvlanlist
= vf
[IFLA_VF_VLAN_LIST
];
409 int rem
= RTA_PAYLOAD(vfvlanlist
);
411 open_json_array(PRINT_JSON
, "vlan_list");
412 for (i
= RTA_DATA(vfvlanlist
);
413 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
414 struct ifla_vf_vlan_info
*vf_vlan_info
= RTA_DATA(i
);
417 open_json_object(NULL
);
418 if (vf_vlan_info
->vlan
)
423 if (vf_vlan_info
->qos
)
428 if (vf_vlan_info
->vlan_proto
&&
429 vf_vlan_info
->vlan_proto
!= htons(ETH_P_8021Q
))
430 print_string(PRINT_ANY
,
432 ", vlan protocol %s",
434 vf_vlan_info
->vlan_proto
,
438 close_json_array(PRINT_JSON
, NULL
);
440 struct ifla_vf_vlan
*vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
448 print_int(PRINT_ANY
, "qos", ", qos %d", vf_vlan
->qos
);
451 if (vf_tx_rate
->rate
)
452 print_uint(PRINT_ANY
,
454 ", tx rate %u (Mbps)",
457 if (vf
[IFLA_VF_RATE
]) {
458 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
459 int max_tx
= vf_rate
->max_tx_rate
;
460 int min_tx
= vf_rate
->min_tx_rate
;
462 if (is_json_context()) {
463 open_json_object("rate");
464 print_uint(PRINT_JSON
, "max_tx", NULL
, max_tx
);
465 print_uint(PRINT_ANY
, "min_tx", NULL
, min_tx
);
469 fprintf(fp
, ", max_tx_rate %uMbps", max_tx
);
471 fprintf(fp
, ", min_tx_rate %uMbps", min_tx
);
475 if (vf
[IFLA_VF_SPOOFCHK
]) {
476 struct ifla_vf_spoofchk
*vf_spoofchk
=
477 RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
479 if (vf_spoofchk
->setting
!= -1)
480 print_bool(PRINT_ANY
,
482 vf_spoofchk
->setting
?
483 ", spoof checking on" : ", spoof checking off",
484 vf_spoofchk
->setting
);
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
);
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
,
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
);
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
,
505 if (vf
[IFLA_VF_LINK_STATE
]) {
506 struct ifla_vf_link_state
*vf_linkstate
=
507 RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
509 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
510 print_string(PRINT_ANY
,
514 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
515 print_string(PRINT_ANY
,
520 print_string(PRINT_ANY
,
526 if (vf
[IFLA_VF_TRUST
]) {
527 struct ifla_vf_trust
*vf_trust
= RTA_DATA(vf
[IFLA_VF_TRUST
]);
529 if (vf_trust
->setting
!= -1)
530 print_bool(PRINT_ANY
,
532 vf_trust
->setting
? ", trust on" : ", trust off",
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
]);
540 if (rss_query
->setting
!= -1)
541 print_bool(PRINT_ANY
,
543 rss_query
->setting
? ", query_rss on"
548 if (vf
[IFLA_VF_STATS
] && show_stats
)
549 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
552 void print_num(FILE *fp
, unsigned int width
, uint64_t count
)
554 const char *prefix
= "kMGTPE";
555 const unsigned int base
= use_iec
? 1024 : 1000;
558 uint8_t precision
= 2;
561 if (!human_readable
|| count
< base
) {
562 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
566 /* increase value by a factor of 1000/1024 and print
567 * if result is something a human can read
571 if (count
/ base
< powi
)
579 /* try to guess a good number of digits for precision */
580 for (; precision
> 0; precision
--) {
582 if (count
/ powi
< powj
)
586 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
587 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
589 fprintf(fp
, "%-*s ", width
, buf
);
592 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
594 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1];
596 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
597 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
601 parse_rtattr_nested(vf
, IFLA_VF_STATS_MAX
, vfstats
);
603 if (is_json_context()) {
604 open_json_object("stats");
607 open_json_object("rx");
608 print_u64(PRINT_JSON
, "bytes", NULL
,
609 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
610 print_u64(PRINT_JSON
, "packets", NULL
,
611 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
612 print_u64(PRINT_JSON
, "multicast", NULL
,
613 rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
614 print_u64(PRINT_JSON
, "broadcast", NULL
,
615 rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
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
]));
622 open_json_object("tx");
623 print_u64(PRINT_JSON
, "tx_bytes", NULL
,
624 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
625 print_u64(PRINT_JSON
, "tx_packets", NULL
,
626 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
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
]));
634 fprintf(fp
, "%s", _SL_
);
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_
);
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
]));
645 if (vf
[IFLA_VF_STATS_RX_DROPPED
])
646 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_DROPPED
]));
649 fprintf(fp
, "%s", _SL_
);
650 fprintf(fp
, " TX: bytes packets ");
651 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
652 fprintf(fp
, " dropped ");
653 fprintf(fp
, "%s", _SL_
);
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
]));
658 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
659 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_DROPPED
]));
663 static void __print_link_stats(FILE *fp
, struct rtattr
*tb
[])
665 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
666 struct rtnl_link_stats64 _s
, *s
= &_s
;
669 ret
= get_rtnl_link_stats_rta(s
, tb
);
673 if (is_json_context()) {
674 open_json_object((ret
== sizeof(*s
)) ? "stats64" : "stats");
677 open_json_object("rx");
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
);
684 if (s
->rx_compressed
)
685 print_u64(PRINT_JSON
,
686 "compressed", NULL
, s
->rx_compressed
);
689 if (show_stats
> 1) {
690 print_u64(PRINT_JSON
,
692 NULL
, s
->rx_length_errors
);
693 print_u64(PRINT_JSON
,
695 NULL
, s
->rx_crc_errors
);
696 print_u64(PRINT_JSON
,
698 NULL
, s
->rx_frame_errors
);
699 print_u64(PRINT_JSON
,
701 NULL
, s
->rx_fifo_errors
);
702 print_u64(PRINT_JSON
,
704 NULL
, s
->rx_missed_errors
);
706 print_u64(PRINT_JSON
,
707 "nohandler", NULL
, s
->rx_nohandler
);
712 open_json_object("tx");
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
,
719 NULL
, s
->tx_carrier_errors
);
720 print_u64(PRINT_JSON
, "collisions", NULL
, s
->collisions
);
721 if (s
->tx_compressed
)
722 print_u64(PRINT_JSON
,
723 "compressed", NULL
, s
->tx_compressed
);
726 if (show_stats
> 1) {
727 print_u64(PRINT_JSON
,
729 NULL
, s
->tx_aborted_errors
);
730 print_u64(PRINT_JSON
,
732 NULL
, s
->tx_fifo_errors
);
733 print_u64(PRINT_JSON
,
735 NULL
, s
->tx_window_errors
);
736 print_u64(PRINT_JSON
,
738 NULL
, s
->tx_heartbeat_errors
);
740 print_u64(PRINT_JSON
, "carrier_changes", NULL
,
741 rta_getattr_u32(carrier_changes
));
748 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
749 s
->rx_compressed
? "compressed" : "", _SL_
);
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
);
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_
);
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
);
773 print_num(fp
, 7, s
->rx_nohandler
);
775 fprintf(fp
, "%s", _SL_
);
778 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
779 s
->tx_compressed
? "compressed" : "", _SL_
);
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
);
792 if (show_stats
> 1) {
793 fprintf(fp
, "%s", _SL_
);
794 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
796 fprintf(fp
, " transns");
797 fprintf(fp
, "%s", _SL_
);
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
);
806 rta_getattr_u32(carrier_changes
));
811 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
813 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
814 struct rtattr
*tb
[IFLA_MAX
+1];
816 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
817 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
818 __print_link_stats(fp
, tb
);
822 static int print_linkinfo_brief(FILE *fp
, const char *name
,
823 const struct ifinfomsg
*ifi
,
826 unsigned int m_flag
= 0;
828 m_flag
= print_name_and_link("%-16s ", name
, tb
);
830 if (tb
[IFLA_OPERSTATE
])
831 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
833 if (filter
.family
== AF_PACKET
) {
836 if (tb
[IFLA_ADDRESS
]) {
837 print_color_string(PRINT_ANY
, COLOR_MAC
,
840 RTA_DATA(tb
[IFLA_ADDRESS
]),
841 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
847 if (filter
.family
== AF_PACKET
) {
848 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
849 print_string(PRINT_FP
, NULL
, "%s", "\n");
856 static 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"
866 static void print_link_event(FILE *f
, __u32 event
)
868 if (event
>= ARRAY_SIZE(link_events
))
869 print_int(PRINT_ANY
, "event", "event %d ", event
);
872 print_string(PRINT_ANY
,
873 "event", "event %s ",
878 int print_linkinfo(struct nlmsghdr
*n
, void *arg
)
880 FILE *fp
= (FILE *)arg
;
881 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
882 struct rtattr
*tb
[IFLA_MAX
+1];
883 int len
= n
->nlmsg_len
;
885 unsigned int m_flag
= 0;
888 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
891 len
-= NLMSG_LENGTH(sizeof(*ifi
));
895 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
897 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
900 parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
, NLA_F_NESTED
);
902 name
= get_ifname_rta(ifi
->ifi_index
, tb
[IFLA_IFNAME
]);
909 if (tb
[IFLA_GROUP
]) {
910 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
912 if (filter
.group
!= -1 && group
!= filter
.group
)
916 if (tb
[IFLA_MASTER
]) {
917 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
919 if (filter
.master
> 0 && master
!= filter
.master
)
921 } else if (filter
.master
> 0)
924 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
927 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
930 if (n
->nlmsg_type
== RTM_DELLINK
)
931 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
934 return print_linkinfo_brief(fp
, name
, ifi
, tb
);
936 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
938 m_flag
= print_name_and_link("%s: ", name
, tb
);
939 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
944 rta_getattr_u32(tb
[IFLA_MTU
]));
946 xdp_dump(fp
, tb
[IFLA_XDP
], do_link
, false);
948 print_string(PRINT_ANY
,
951 rta_getattr_str(tb
[IFLA_QDISC
]));
952 if (tb
[IFLA_MASTER
]) {
953 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
955 print_string(PRINT_ANY
,
956 "master", "master %s ",
957 ll_index_to_name(master
));
960 if (tb
[IFLA_OPERSTATE
])
961 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
963 if (do_link
&& tb
[IFLA_LINKMODE
])
964 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
966 if (tb
[IFLA_GROUP
]) {
967 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
969 print_string(PRINT_ANY
,
972 rtnl_group_n2a(group
, b1
, sizeof(b1
)));
975 if (filter
.showqueue
)
976 print_queuelen(fp
, tb
);
979 print_link_event(fp
, rta_getattr_u32(tb
[IFLA_EVENT
]));
981 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
983 print_string(PRINT_ANY
,
986 ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
987 if (tb
[IFLA_ADDRESS
]) {
988 print_color_string(PRINT_ANY
,
992 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
993 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
997 if (tb
[IFLA_BROADCAST
]) {
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);
1003 print_string(PRINT_FP
, NULL
, " brd ", NULL
);
1005 print_color_string(PRINT_ANY
,
1009 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
1010 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
1014 if (tb
[IFLA_PERM_ADDRESS
]) {
1015 unsigned int len
= RTA_PAYLOAD(tb
[IFLA_PERM_ADDRESS
]);
1017 if (!tb
[IFLA_ADDRESS
] ||
1018 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]) != len
||
1019 memcmp(RTA_DATA(tb
[IFLA_PERM_ADDRESS
]),
1020 RTA_DATA(tb
[IFLA_ADDRESS
]), len
)) {
1021 print_string(PRINT_FP
, NULL
, " permaddr ", NULL
);
1022 print_color_string(PRINT_ANY
,
1026 ll_addr_n2a(RTA_DATA(tb
[IFLA_PERM_ADDRESS
]),
1027 RTA_PAYLOAD(tb
[IFLA_PERM_ADDRESS
]),
1034 if (tb
[IFLA_LINK_NETNSID
]) {
1035 int id
= rta_getattr_u32(tb
[IFLA_LINK_NETNSID
]);
1037 if (is_json_context()) {
1038 print_int(PRINT_JSON
, "link_netnsid", NULL
, id
);
1041 char *name
= get_name_from_nsid(id
);
1044 print_string(PRINT_FP
, NULL
,
1045 " link-netns %s", name
);
1047 print_int(PRINT_FP
, NULL
,
1048 " link-netnsid %d", id
);
1050 print_string(PRINT_FP
, NULL
,
1051 " link-netnsid %s", "unknown");
1055 if (tb
[IFLA_NEW_NETNSID
]) {
1056 int id
= rta_getattr_u32(tb
[IFLA_NEW_NETNSID
]);
1057 char *name
= get_name_from_nsid(id
);
1060 print_string(PRINT_FP
, NULL
, " new-netns %s", name
);
1062 print_int(PRINT_FP
, NULL
, " new-netnsid %d", id
);
1064 if (tb
[IFLA_NEW_IFINDEX
]) {
1065 int id
= rta_getattr_u32(tb
[IFLA_NEW_IFINDEX
]);
1067 print_int(PRINT_FP
, NULL
, " new-ifindex %d", id
);
1070 if (tb
[IFLA_PROTO_DOWN
]) {
1071 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
1072 print_bool(PRINT_ANY
,
1073 "proto_down", " protodown on ", true);
1077 if (tb
[IFLA_PROMISCUITY
])
1078 print_uint(PRINT_ANY
,
1081 rta_getattr_u32(tb
[IFLA_PROMISCUITY
]));
1083 if (tb
[IFLA_MIN_MTU
])
1084 print_uint(PRINT_ANY
,
1085 "min_mtu", "minmtu %u ",
1086 rta_getattr_u32(tb
[IFLA_MIN_MTU
]));
1088 if (tb
[IFLA_MAX_MTU
])
1089 print_uint(PRINT_ANY
,
1090 "max_mtu", "maxmtu %u ",
1091 rta_getattr_u32(tb
[IFLA_MAX_MTU
]));
1093 if (tb
[IFLA_LINKINFO
])
1094 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
1096 if (do_link
&& tb
[IFLA_AF_SPEC
])
1097 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
1099 if (tb
[IFLA_NUM_TX_QUEUES
])
1100 print_uint(PRINT_ANY
,
1103 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
1105 if (tb
[IFLA_NUM_RX_QUEUES
])
1106 print_uint(PRINT_ANY
,
1109 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
1111 if (tb
[IFLA_GSO_MAX_SIZE
])
1112 print_uint(PRINT_ANY
,
1115 rta_getattr_u32(tb
[IFLA_GSO_MAX_SIZE
]));
1117 if (tb
[IFLA_GSO_MAX_SEGS
])
1118 print_uint(PRINT_ANY
,
1121 rta_getattr_u32(tb
[IFLA_GSO_MAX_SEGS
]));
1123 if (tb
[IFLA_PHYS_PORT_NAME
])
1124 print_string(PRINT_ANY
,
1127 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
1129 if (tb
[IFLA_PHYS_PORT_ID
]) {
1130 print_string(PRINT_ANY
,
1134 RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
1135 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
1139 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
1140 print_string(PRINT_ANY
,
1143 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
1144 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
1149 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
1150 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
1151 print_string(PRINT_ANY
,
1154 rta_getattr_str(tb
[IFLA_IFALIAS
]));
1157 if ((do_link
|| show_details
) && tb
[IFLA_XDP
])
1158 xdp_dump(fp
, tb
[IFLA_XDP
], true, true);
1160 if (do_link
&& show_stats
) {
1162 __print_link_stats(fp
, tb
);
1165 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
1166 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
1167 int rem
= RTA_PAYLOAD(vflist
);
1169 open_json_array(PRINT_JSON
, "vfinfo_list");
1170 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1171 open_json_object(NULL
);
1172 print_vfinfo(fp
, ifi
, i
);
1173 close_json_object();
1175 close_json_array(PRINT_JSON
, NULL
);
1178 if (tb
[IFLA_PROP_LIST
]) {
1179 struct rtattr
*i
, *proplist
= tb
[IFLA_PROP_LIST
];
1180 int rem
= RTA_PAYLOAD(proplist
);
1182 open_json_array(PRINT_JSON
, "altnames");
1183 for (i
= RTA_DATA(proplist
); RTA_OK(i
, rem
);
1184 i
= RTA_NEXT(i
, rem
)) {
1185 if (i
->rta_type
!= IFLA_ALT_IFNAME
)
1187 print_string(PRINT_FP
, NULL
, "%s altname ", _SL_
);
1188 print_string(PRINT_ANY
, NULL
,
1189 "%s", rta_getattr_str(i
));
1191 close_json_array(PRINT_JSON
, NULL
);
1194 print_string(PRINT_FP
, NULL
, "%s", "\n");
1199 static int flush_update(void)
1203 * Note that the kernel may delete multiple addresses for one
1204 * delete request (e.g. if ipv4 address promotion is disabled).
1205 * Since a flush operation is really a series of delete requests
1206 * its possible that we may request an address delete that has
1207 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
1208 * errors returned from a flush request
1210 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
1211 (errno
!= EADDRNOTAVAIL
)) {
1212 perror("Failed to send flush request");
1219 static int set_lifetime(unsigned int *lifetime
, char *argv
)
1221 if (strcmp(argv
, "forever") == 0)
1222 *lifetime
= INFINITY_LIFE_TIME
;
1223 else if (get_u32(lifetime
, argv
, 0))
1229 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
1230 struct rtattr
*ifa_flags_attr
)
1232 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
1236 /* Mapping from argument to address flag mask and attributes */
1237 static const struct ifa_flag_data_t
{
1242 } ifa_flag_data
[] = {
1243 { .name
= "secondary", .mask
= IFA_F_SECONDARY
, .readonly
= true, .v6only
= false},
1244 { .name
= "temporary", .mask
= IFA_F_SECONDARY
, .readonly
= true, .v6only
= false},
1245 { .name
= "nodad", .mask
= IFA_F_NODAD
, .readonly
= false, .v6only
= true},
1246 { .name
= "optimistic", .mask
= IFA_F_OPTIMISTIC
, .readonly
= false, .v6only
= true},
1247 { .name
= "dadfailed", .mask
= IFA_F_DADFAILED
, .readonly
= true, .v6only
= true},
1248 { .name
= "home", .mask
= IFA_F_HOMEADDRESS
, .readonly
= false, .v6only
= true},
1249 { .name
= "deprecated", .mask
= IFA_F_DEPRECATED
, .readonly
= true, .v6only
= true},
1250 { .name
= "tentative", .mask
= IFA_F_TENTATIVE
, .readonly
= true, .v6only
= true},
1251 { .name
= "permanent", .mask
= IFA_F_PERMANENT
, .readonly
= true, .v6only
= true},
1252 { .name
= "mngtmpaddr", .mask
= IFA_F_MANAGETEMPADDR
, .readonly
= false, .v6only
= true},
1253 { .name
= "noprefixroute", .mask
= IFA_F_NOPREFIXROUTE
, .readonly
= false, .v6only
= true},
1254 { .name
= "autojoin", .mask
= IFA_F_MCAUTOJOIN
, .readonly
= false, .v6only
= true},
1255 { .name
= "stable-privacy", .mask
= IFA_F_STABLE_PRIVACY
, .readonly
= true, .v6only
= true},
1258 /* Returns a pointer to the data structure for a particular interface flag, or null if no flag could be found */
1259 static const struct ifa_flag_data_t
* lookup_flag_data_by_name(const char* flag_name
) {
1260 for (int i
= 0; i
< ARRAY_SIZE(ifa_flag_data
); ++i
) {
1261 if (strcmp(flag_name
, ifa_flag_data
[i
].name
) == 0)
1262 return &ifa_flag_data
[i
];
1267 static void print_ifa_flags(FILE *fp
, const struct ifaddrmsg
*ifa
,
1272 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_data
); i
++) {
1273 const struct ifa_flag_data_t
* flag_data
= &ifa_flag_data
[i
];
1275 if (flag_data
->mask
== IFA_F_PERMANENT
) {
1276 if (!(flags
& flag_data
->mask
))
1277 print_bool(PRINT_ANY
,
1278 "dynamic", "dynamic ", true);
1279 } else if (flags
& flag_data
->mask
) {
1280 if (flag_data
->mask
== IFA_F_SECONDARY
&&
1281 ifa
->ifa_family
== AF_INET6
) {
1282 print_bool(PRINT_ANY
,
1283 "temporary", "temporary ", true);
1285 print_string(PRINT_FP
, NULL
,
1286 "%s ", flag_data
->name
);
1287 print_bool(PRINT_JSON
,
1288 flag_data
->name
, NULL
, true);
1292 flags
&= ~flag_data
->mask
;
1296 if (is_json_context()) {
1299 snprintf(b1
, sizeof(b1
), "%02x", flags
);
1300 print_string(PRINT_JSON
, "ifa_flags", NULL
, b1
);
1302 fprintf(fp
, "flags %02x ", flags
);
1308 static int get_filter(const char *arg
)
1312 if (arg
[0] == '-') {
1318 if (strcmp(arg
, "dynamic") == 0) {
1321 } else if (strcmp(arg
, "primary") == 0) {
1326 const struct ifa_flag_data_t
* flag_data
= lookup_flag_data_by_name(arg
);
1327 if (flag_data
== NULL
)
1331 filter
.flags
&= ~flag_data
->mask
;
1333 filter
.flags
|= flag_data
->mask
;
1334 filter
.flagmask
|= flag_data
->mask
;
1338 static int ifa_label_match_rta(int ifindex
, const struct rtattr
*rta
)
1346 label
= RTA_DATA(rta
);
1348 label
= ll_index_to_name(ifindex
);
1350 return fnmatch(filter
.label
, label
, 0);
1353 int print_addrinfo(struct nlmsghdr
*n
, void *arg
)
1356 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1357 int len
= n
->nlmsg_len
;
1358 unsigned int ifa_flags
;
1359 struct rtattr
*rta_tb
[IFA_MAX
+1];
1363 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
1365 len
-= NLMSG_LENGTH(sizeof(*ifa
));
1367 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1371 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
1374 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
1375 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1377 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1379 if (!rta_tb
[IFA_LOCAL
])
1380 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1381 if (!rta_tb
[IFA_ADDRESS
])
1382 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1384 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1386 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1388 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1391 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1394 if (ifa_label_match_rta(ifa
->ifa_index
, rta_tb
[IFA_LABEL
]))
1397 if (inet_addr_match_rta(&filter
.pfx
, rta_tb
[IFA_LOCAL
]))
1400 if (filter
.flushb
) {
1401 struct nlmsghdr
*fn
;
1403 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1407 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1408 memcpy(fn
, n
, n
->nlmsg_len
);
1409 fn
->nlmsg_type
= RTM_DELADDR
;
1410 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1411 fn
->nlmsg_seq
= ++rth
.seq
;
1412 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1418 if (n
->nlmsg_type
== RTM_DELADDR
)
1419 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
1424 if (filter
.oneline
|| filter
.flushb
) {
1425 const char *dev
= ll_index_to_name(ifa
->ifa_index
);
1427 if (is_json_context()) {
1428 print_int(PRINT_JSON
,
1429 "index", NULL
, ifa
->ifa_index
);
1430 print_string(PRINT_JSON
, "dev", NULL
, dev
);
1432 fprintf(fp
, "%u: %s", ifa
->ifa_index
, dev
);
1436 name
= family_name(ifa
->ifa_family
);
1438 print_string(PRINT_ANY
, "family", " %s ", name
);
1440 print_int(PRINT_ANY
, "family_index", " family %d ",
1445 if (rta_tb
[IFA_LOCAL
]) {
1446 print_color_string(PRINT_ANY
,
1447 ifa_family_color(ifa
->ifa_family
),
1449 format_host_rta(ifa
->ifa_family
,
1450 rta_tb
[IFA_LOCAL
]));
1451 if (rta_tb
[IFA_ADDRESS
] &&
1452 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1453 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1454 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1455 print_string(PRINT_FP
, NULL
, " %s ", "peer");
1456 print_color_string(PRINT_ANY
,
1457 ifa_family_color(ifa
->ifa_family
),
1460 format_host_rta(ifa
->ifa_family
,
1461 rta_tb
[IFA_ADDRESS
]));
1463 print_int(PRINT_ANY
, "prefixlen", "/%d ", ifa
->ifa_prefixlen
);
1465 if (rta_tb
[IFA_RT_PRIORITY
])
1466 print_uint(PRINT_ANY
, "metric", "metric %u ",
1467 rta_getattr_u32(rta_tb
[IFA_RT_PRIORITY
]));
1473 if (rta_tb
[IFA_BROADCAST
]) {
1474 print_string(PRINT_FP
, NULL
, "%s ", "brd");
1475 print_color_string(PRINT_ANY
,
1476 ifa_family_color(ifa
->ifa_family
),
1479 format_host_rta(ifa
->ifa_family
,
1480 rta_tb
[IFA_BROADCAST
]));
1483 if (rta_tb
[IFA_ANYCAST
]) {
1484 print_string(PRINT_FP
, NULL
, "%s ", "any");
1485 print_color_string(PRINT_ANY
,
1486 ifa_family_color(ifa
->ifa_family
),
1489 format_host_rta(ifa
->ifa_family
,
1490 rta_tb
[IFA_ANYCAST
]));
1493 print_string(PRINT_ANY
,
1496 rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1498 print_ifa_flags(fp
, ifa
, ifa_flags
);
1500 if (rta_tb
[IFA_LABEL
])
1501 print_string(PRINT_ANY
,
1504 rta_getattr_str(rta_tb
[IFA_LABEL
]));
1506 if (rta_tb
[IFA_CACHEINFO
]) {
1507 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1510 print_string(PRINT_FP
, NULL
, " valid_lft ", NULL
);
1512 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
) {
1513 print_uint(PRINT_JSON
,
1515 NULL
, INFINITY_LIFE_TIME
);
1516 print_string(PRINT_FP
, NULL
, "%s", "forever");
1518 print_uint(PRINT_ANY
,
1519 "valid_life_time", "%usec", ci
->ifa_valid
);
1522 print_string(PRINT_FP
, NULL
, " preferred_lft ", NULL
);
1523 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
) {
1524 print_uint(PRINT_JSON
,
1525 "preferred_life_time",
1526 NULL
, INFINITY_LIFE_TIME
);
1527 print_string(PRINT_FP
, NULL
, "%s", "forever");
1529 if (ifa_flags
& IFA_F_DEPRECATED
)
1530 print_int(PRINT_ANY
,
1531 "preferred_life_time",
1535 print_uint(PRINT_ANY
,
1536 "preferred_life_time",
1541 print_string(PRINT_FP
, NULL
, "%s", "\n");
1547 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1548 struct nlmsg_list
*ainfo
, FILE *fp
)
1550 open_json_array(PRINT_JSON
, "addr_info");
1551 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1552 struct nlmsghdr
*n
= &ainfo
->h
;
1553 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1555 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1558 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ifa
)))
1561 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1562 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1565 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1568 open_json_object(NULL
);
1569 print_addrinfo(n
, fp
);
1570 close_json_object();
1572 close_json_array(PRINT_JSON
, NULL
);
1575 print_string(PRINT_FP
, NULL
, "%s", "\n");
1582 static int store_nlmsg(struct nlmsghdr
*n
, void *arg
)
1584 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1585 struct nlmsg_list
*h
;
1587 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1591 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1595 lchain
->tail
->next
= h
;
1600 ll_remember_index(n
, NULL
);
1604 static __u32 ipadd_dump_magic
= 0x47361222;
1606 static int ipadd_save_prep(void)
1610 if (isatty(STDOUT_FILENO
)) {
1611 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1615 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1616 if (ret
!= sizeof(ipadd_dump_magic
)) {
1617 fprintf(stderr
, "Can't write magic to dump file\n");
1624 static int ipadd_dump_check_magic(void)
1629 if (isatty(STDIN_FILENO
)) {
1630 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1634 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1635 if (magic
!= ipadd_dump_magic
) {
1636 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1643 static int save_nlmsg(struct nlmsghdr
*n
, void *arg
)
1647 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1648 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1649 fprintf(stderr
, "Short write while saving nlmsg\n");
1653 return ret
== n
->nlmsg_len
? 0 : ret
;
1656 static int show_handler(struct rtnl_ctrl_data
*ctrl
,
1657 struct nlmsghdr
*n
, void *arg
)
1659 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1661 open_json_object(NULL
);
1662 print_int(PRINT_ANY
, "index", "if%d:", ifa
->ifa_index
);
1664 print_addrinfo(n
, stdout
);
1665 close_json_object();
1669 static int ipaddr_showdump(void)
1673 if (ipadd_dump_check_magic())
1677 open_json_object(NULL
);
1678 open_json_array(PRINT_JSON
, "addr_info");
1680 err
= rtnl_from_file(stdin
, &show_handler
, NULL
);
1682 close_json_array(PRINT_JSON
, NULL
);
1683 close_json_object();
1689 static int restore_handler(struct rtnl_ctrl_data
*ctrl
,
1690 struct nlmsghdr
*n
, void *arg
)
1694 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1698 ret
= rtnl_talk(&rth
, n
, NULL
);
1699 if ((ret
< 0) && (errno
== EEXIST
))
1705 static int ipaddr_restore(void)
1707 if (ipadd_dump_check_magic())
1710 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1713 void free_nlmsg_chain(struct nlmsg_chain
*info
)
1715 struct nlmsg_list
*l
, *n
;
1717 for (l
= info
->head
; l
; l
= n
) {
1723 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1725 struct nlmsg_list
*l
, **lp
;
1728 while ((l
= *lp
) != NULL
) {
1730 int missing_net_address
= 1;
1731 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1732 struct nlmsg_list
*a
;
1734 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1735 struct nlmsghdr
*n
= &a
->h
;
1736 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1737 struct rtattr
*tb
[IFA_MAX
+ 1];
1738 unsigned int ifa_flags
;
1740 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1742 missing_net_address
= 0;
1743 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1745 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1748 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1749 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1751 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1754 if (ifa_label_match_rta(ifa
->ifa_index
, tb
[IFA_LABEL
]))
1758 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1759 if (inet_addr_match_rta(&filter
.pfx
, tb
[IFA_LOCAL
]))
1765 if (missing_net_address
&&
1766 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1776 static int ipaddr_dump_filter(struct nlmsghdr
*nlh
, int reqlen
)
1778 struct ifaddrmsg
*ifa
= NLMSG_DATA(nlh
);
1780 ifa
->ifa_index
= filter
.ifindex
;
1785 static int ipaddr_flush(void)
1788 char flushb
[4096-512];
1790 filter
.flushb
= flushb
;
1792 filter
.flushe
= sizeof(flushb
);
1794 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1795 if (rtnl_addrdump_req(&rth
, filter
.family
,
1796 ipaddr_dump_filter
) < 0) {
1797 perror("Cannot send dump request");
1801 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1802 stdout
, NLM_F_DUMP_INTR
) < 0) {
1803 fprintf(stderr
, "Flush terminated\n");
1806 if (filter
.flushed
== 0) {
1810 printf("Nothing to flush.\n");
1812 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1818 if (flush_update() < 0)
1822 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1826 /* If we are flushing, and specifying primary, then we
1827 * want to flush only a single round. Otherwise, we'll
1828 * start flushing secondaries that were promoted to
1831 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1834 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1839 static int iplink_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
1843 err
= addattr32(nlh
, reqlen
, IFLA_EXT_MASK
, RTEXT_FILTER_VF
);
1847 if (filter
.master
) {
1848 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter
.master
);
1854 struct rtattr
*linkinfo
;
1856 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
1858 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, filter
.kind
,
1859 strlen(filter
.kind
));
1863 addattr_nest_end(nlh
, linkinfo
);
1869 static int ipaddr_link_get(int index
, struct nlmsg_chain
*linfo
)
1871 struct iplink_req req
= {
1872 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
1873 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1874 .n
.nlmsg_type
= RTM_GETLINK
,
1875 .i
.ifi_family
= filter
.family
,
1876 .i
.ifi_index
= index
,
1878 __u32 filt_mask
= RTEXT_FILTER_VF
;
1879 struct nlmsghdr
*answer
;
1882 filt_mask
|= RTEXT_FILTER_SKIP_STATS
;
1884 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filt_mask
);
1886 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
1887 perror("Cannot send link request");
1891 if (store_nlmsg(answer
, linfo
) < 0) {
1892 fprintf(stderr
, "Failed to process link information\n");
1899 /* fills in linfo with link data and optionally ainfo with address info
1900 * caller can walk lists as desired and must call free_nlmsg_chain for
1903 int ip_link_list(req_filter_fn_t filter_fn
, struct nlmsg_chain
*linfo
)
1905 if (rtnl_linkdump_req_filter_fn(&rth
, preferred_family
,
1907 perror("Cannot send dump request");
1911 if (rtnl_dump_filter(&rth
, store_nlmsg
, linfo
) < 0) {
1912 fprintf(stderr
, "Dump terminated\n");
1919 static int ip_addr_list(struct nlmsg_chain
*ainfo
)
1921 if (rtnl_addrdump_req(&rth
, filter
.family
, ipaddr_dump_filter
) < 0) {
1922 perror("Cannot send dump request");
1926 if (rtnl_dump_filter(&rth
, store_nlmsg
, ainfo
) < 0) {
1927 fprintf(stderr
, "Dump terminated\n");
1934 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1936 struct nlmsg_chain linfo
= { NULL
, NULL
};
1937 struct nlmsg_chain _ainfo
= { NULL
, NULL
}, *ainfo
= &_ainfo
;
1938 struct nlmsg_list
*l
;
1939 char *filter_dev
= NULL
;
1942 ipaddr_reset_filter(oneline
, 0);
1943 filter
.showqueue
= 1;
1944 filter
.family
= preferred_family
;
1946 if (action
== IPADD_FLUSH
) {
1948 fprintf(stderr
, "Flush requires arguments.\n");
1952 if (filter
.family
== AF_PACKET
) {
1953 fprintf(stderr
, "Cannot flush link addresses.\n");
1959 if (strcmp(*argv
, "to") == 0) {
1961 if (get_prefix(&filter
.pfx
, *argv
, filter
.family
))
1962 invarg("invalid \"to\"\n", *argv
);
1963 if (filter
.family
== AF_UNSPEC
)
1964 filter
.family
= filter
.pfx
.family
;
1965 } else if (strcmp(*argv
, "scope") == 0) {
1966 unsigned int scope
= 0;
1969 filter
.scopemask
= -1;
1970 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1971 if (strcmp(*argv
, "all") != 0)
1972 invarg("invalid \"scope\"\n", *argv
);
1973 scope
= RT_SCOPE_NOWHERE
;
1974 filter
.scopemask
= 0;
1976 filter
.scope
= scope
;
1977 } else if (strcmp(*argv
, "up") == 0) {
1979 } else if (get_filter(*argv
) == 0) {
1981 } else if (strcmp(*argv
, "label") == 0) {
1983 filter
.label
= *argv
;
1984 } else if (strcmp(*argv
, "group") == 0) {
1986 if (rtnl_group_a2n(&filter
.group
, *argv
))
1987 invarg("Invalid \"group\" value\n", *argv
);
1988 } else if (strcmp(*argv
, "master") == 0) {
1992 ifindex
= ll_name_to_index(*argv
);
1994 invarg("Device does not exist\n", *argv
);
1995 filter
.master
= ifindex
;
1996 } else if (strcmp(*argv
, "vrf") == 0) {
2000 ifindex
= ll_name_to_index(*argv
);
2002 invarg("Not a valid VRF name\n", *argv
);
2003 if (!name_is_vrf(*argv
))
2004 invarg("Not a valid VRF name\n", *argv
);
2005 filter
.master
= ifindex
;
2006 } else if (strcmp(*argv
, "type") == 0) {
2010 soff
= strlen(*argv
) - strlen("_slave");
2011 if (!strcmp(*argv
+ soff
, "_slave")) {
2012 (*argv
)[soff
] = '\0';
2013 filter
.slave_kind
= *argv
;
2015 filter
.kind
= *argv
;
2018 if (strcmp(*argv
, "dev") == 0)
2020 else if (matches(*argv
, "help") == 0)
2023 duparg2("dev", *argv
);
2030 filter
.ifindex
= ll_name_to_index(filter_dev
);
2031 if (filter
.ifindex
<= 0) {
2032 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
2037 if (action
== IPADD_FLUSH
)
2038 return ipaddr_flush();
2040 if (action
== IPADD_SAVE
) {
2041 if (ipadd_save_prep())
2044 if (rtnl_addrdump_req(&rth
, preferred_family
,
2045 ipaddr_dump_filter
) < 0) {
2046 perror("Cannot send dump request");
2050 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
2051 fprintf(stderr
, "Save terminated\n");
2059 * Initialize a json_writer and open an array object
2060 * if -json was specified.
2065 * If only filter_dev present and none of the other
2066 * link filters are present, use RTM_GETLINK to get
2069 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
2070 if (iplink_get(filter_dev
, RTEXT_FILTER_VF
) < 0) {
2071 perror("Cannot send link get request");
2079 if (filter
.ifindex
) {
2080 if (ipaddr_link_get(filter
.ifindex
, &linfo
) != 0)
2083 if (ip_link_list(iplink_filter_req
, &linfo
) != 0)
2087 if (filter
.family
!= AF_PACKET
) {
2091 if (ip_addr_list(ainfo
) != 0)
2094 ipaddr_filter(&linfo
, ainfo
);
2097 for (l
= linfo
.head
; l
; l
= l
->next
) {
2098 struct nlmsghdr
*n
= &l
->h
;
2099 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
2102 open_json_object(NULL
);
2103 if (brief
|| !no_link
)
2104 res
= print_linkinfo(n
, stdout
);
2105 if (res
>= 0 && filter
.family
!= AF_PACKET
)
2106 print_selected_addrinfo(ifi
, ainfo
->head
, stdout
);
2107 if (res
> 0 && !do_link
&& show_stats
)
2108 print_link_stats(stdout
, n
);
2109 close_json_object();
2114 free_nlmsg_chain(ainfo
);
2115 free_nlmsg_chain(&linfo
);
2121 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
2123 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
2124 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
2125 struct ifla_vf_rate
*vf_rate
;
2128 rem
= RTA_PAYLOAD(vflist
);
2130 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
2131 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
2133 if (!vf
[IFLA_VF_RATE
]) {
2134 fprintf(stderr
, "VF min/max rate API not supported\n");
2138 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
2139 if (vf_rate
->vf
== vfnum
) {
2140 *min
= vf_rate
->min_tx_rate
;
2141 *max
= vf_rate
->max_tx_rate
;
2145 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
2149 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, const char *dev
)
2151 struct nlmsg_chain linfo
= { NULL
, NULL
};
2152 struct rtattr
*tb
[IFLA_MAX
+1];
2153 struct ifinfomsg
*ifi
;
2154 struct nlmsg_list
*l
;
2158 idx
= ll_name_to_index(dev
);
2160 fprintf(stderr
, "Device %s does not exist\n", dev
);
2164 if (rtnl_linkdump_req(&rth
, AF_UNSPEC
) < 0) {
2165 perror("Cannot send dump request");
2168 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
2169 fprintf(stderr
, "Dump terminated\n");
2172 for (l
= linfo
.head
; l
; l
= l
->next
) {
2174 ifi
= NLMSG_DATA(n
);
2176 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
2177 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
2180 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
2182 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
2183 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
2189 int ipaddr_list_link(int argc
, char **argv
)
2191 preferred_family
= AF_PACKET
;
2193 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
2196 void ipaddr_reset_filter(int oneline
, int ifindex
)
2198 memset(&filter
, 0, sizeof(filter
));
2199 filter
.oneline
= oneline
;
2200 filter
.ifindex
= ifindex
;
2204 static int default_scope(inet_prefix
*lcl
)
2206 if (lcl
->family
== AF_INET
) {
2207 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
2208 return RT_SCOPE_HOST
;
2213 static bool ipaddr_is_multicast(inet_prefix
*a
)
2215 if (a
->family
== AF_INET
)
2216 return IN_MULTICAST(ntohl(a
->data
[0]));
2217 else if (a
->family
== AF_INET6
)
2218 return IN6_IS_ADDR_MULTICAST(a
->data
);
2223 static bool is_valid_label(const char *dev
, const char *label
)
2225 size_t len
= strlen(dev
);
2227 if (strncmp(label
, dev
, len
) != 0)
2230 return label
[len
] == '\0' || label
[len
] == ':';
2233 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
2237 struct ifaddrmsg ifa
;
2240 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
2241 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
2242 .n
.nlmsg_type
= cmd
,
2243 .ifa
.ifa_family
= preferred_family
,
2247 char *lcl_arg
= NULL
;
2248 char *valid_lftp
= NULL
;
2249 char *preferred_lftp
= NULL
;
2250 inet_prefix lcl
= {};
2257 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
2258 __u32 valid_lft
= INFINITY_LIFE_TIME
;
2259 unsigned int ifa_flags
= 0;
2262 if (strcmp(*argv
, "peer") == 0 ||
2263 strcmp(*argv
, "remote") == 0) {
2267 duparg("peer", *argv
);
2268 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
2269 peer_len
= peer
.bytelen
;
2270 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2271 req
.ifa
.ifa_family
= peer
.family
;
2272 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
2273 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
2274 } else if (matches(*argv
, "broadcast") == 0 ||
2275 strcmp(*argv
, "brd") == 0) {
2280 duparg("broadcast", *argv
);
2281 if (strcmp(*argv
, "+") == 0)
2283 else if (strcmp(*argv
, "-") == 0)
2286 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2287 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2288 req
.ifa
.ifa_family
= addr
.family
;
2289 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
2290 brd_len
= addr
.bytelen
;
2292 } else if (strcmp(*argv
, "anycast") == 0) {
2297 duparg("anycast", *argv
);
2298 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2299 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2300 req
.ifa
.ifa_family
= addr
.family
;
2301 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
2302 any_len
= addr
.bytelen
;
2303 } else if (strcmp(*argv
, "scope") == 0) {
2304 unsigned int scope
= 0;
2307 if (rtnl_rtscope_a2n(&scope
, *argv
))
2308 invarg("invalid scope value.", *argv
);
2309 req
.ifa
.ifa_scope
= scope
;
2311 } else if (strcmp(*argv
, "dev") == 0) {
2314 } else if (strcmp(*argv
, "label") == 0) {
2317 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
2318 } else if (matches(*argv
, "metric") == 0 ||
2319 matches(*argv
, "priority") == 0 ||
2320 matches(*argv
, "preference") == 0) {
2324 if (get_u32(&metric
, *argv
, 0))
2325 invarg("\"metric\" value is invalid\n", *argv
);
2326 addattr32(&req
.n
, sizeof(req
), IFA_RT_PRIORITY
, metric
);
2327 } else if (matches(*argv
, "valid_lft") == 0) {
2329 duparg("valid_lft", *argv
);
2332 if (set_lifetime(&valid_lft
, *argv
))
2333 invarg("valid_lft value", *argv
);
2334 } else if (matches(*argv
, "preferred_lft") == 0) {
2336 duparg("preferred_lft", *argv
);
2338 preferred_lftp
= *argv
;
2339 if (set_lifetime(&preferred_lft
, *argv
))
2340 invarg("preferred_lft value", *argv
);
2341 } else if (lookup_flag_data_by_name(*argv
)) {
2342 const struct ifa_flag_data_t
* flag_data
= lookup_flag_data_by_name(*argv
);
2343 if (flag_data
->readonly
) {
2344 fprintf(stderr
, "Warning: %s option is not mutable from userspace\n", flag_data
->name
);
2345 } else if (flag_data
->v6only
&& req
.ifa
.ifa_family
!= AF_INET6
) {
2346 fprintf(stderr
, "Warning: %s option can be set only for IPv6 addresses\n", flag_data
->name
);
2348 ifa_flags
|= flag_data
->mask
;
2351 if (strcmp(*argv
, "local") == 0)
2353 if (matches(*argv
, "help") == 0)
2356 duparg2("local", *argv
);
2358 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
2359 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2360 req
.ifa
.ifa_family
= lcl
.family
;
2361 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
2362 local_len
= lcl
.bytelen
;
2366 if (ifa_flags
<= 0xff)
2367 req
.ifa
.ifa_flags
= ifa_flags
;
2369 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
2372 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
2375 if (l
&& !is_valid_label(d
, l
)) {
2377 "\"label\" (%s) must match \"dev\" (%s) or be prefixed by \"dev\" with a colon.\n",
2382 if (peer_len
== 0 && local_len
) {
2383 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
2385 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
2386 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
2387 " This special behaviour is likely to disappear in further releases,\n"
2388 " fix your scripts!\n", lcl_arg
, local_len
*8);
2391 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
2394 if (req
.ifa
.ifa_prefixlen
== 0)
2395 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
2397 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
2401 if (req
.ifa
.ifa_family
!= AF_INET
) {
2402 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
2406 if (brd
.bitlen
<= 30) {
2407 for (i
= 31; i
>= brd
.bitlen
; i
--) {
2409 brd
.data
[0] |= htonl(1<<(31-i
));
2411 brd
.data
[0] &= ~htonl(1<<(31-i
));
2413 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
2414 brd_len
= brd
.bytelen
;
2417 if (!scoped
&& cmd
!= RTM_DELADDR
)
2418 req
.ifa
.ifa_scope
= default_scope(&lcl
);
2420 req
.ifa
.ifa_index
= ll_name_to_index(d
);
2421 if (!req
.ifa
.ifa_index
)
2424 if (valid_lftp
|| preferred_lftp
) {
2425 struct ifa_cacheinfo cinfo
= {};
2428 fprintf(stderr
, "valid_lft is zero\n");
2431 if (valid_lft
< preferred_lft
) {
2432 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
2436 cinfo
.ifa_prefered
= preferred_lft
;
2437 cinfo
.ifa_valid
= valid_lft
;
2438 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
2442 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
2443 fprintf(stderr
, "autojoin needs multicast address\n");
2447 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
2453 int do_ipaddr(int argc
, char **argv
)
2456 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
2457 if (matches(*argv
, "add") == 0)
2458 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
2459 if (matches(*argv
, "change") == 0 ||
2460 strcmp(*argv
, "chg") == 0)
2461 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2462 if (matches(*argv
, "replace") == 0)
2463 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2464 if (matches(*argv
, "delete") == 0)
2465 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2466 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2467 || matches(*argv
, "lst") == 0)
2468 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2469 if (matches(*argv
, "flush") == 0)
2470 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2471 if (matches(*argv
, "save") == 0)
2472 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2473 if (matches(*argv
, "showdump") == 0)
2474 return ipaddr_showdump();
2475 if (matches(*argv
, "restore") == 0)
2476 return ipaddr_restore();
2477 if (matches(*argv
, "help") == 0)
2479 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);