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"
45 static struct link_filter filter
;
48 static void usage(void) __attribute__((noreturn
));
50 static void usage(void)
55 fprintf(stderr
, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
56 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
57 fprintf(stderr
, " ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
58 fprintf(stderr
, " ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
59 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
60 fprintf(stderr
, " ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n");
61 fprintf(stderr
, " [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n");
62 fprintf(stderr
, " [ label LABEL ] [up] [ vrf NAME ] ]\n");
63 fprintf(stderr
, " ip address {showdump|restore}\n");
64 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
65 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
66 fprintf(stderr
, " [ label IFNAME ] [ scope SCOPE-ID ]\n");
67 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
68 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
69 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
70 fprintf(stderr
, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
71 fprintf(stderr
, " CONFFLAG-LIST ]\n");
72 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
73 fprintf(stderr
, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
74 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
75 fprintf(stderr
, "LFT := forever | SECONDS\n");
76 fprintf(stderr
, "TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |\n");
77 fprintf(stderr
, " bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |\n");
78 fprintf(stderr
, " gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti |\n");
79 fprintf(stderr
, " nlmon | can | bond_slave | ipvlan | geneve | bridge_slave |\n");
80 fprintf(stderr
, " hsr | macsec | netdevsim\n");
85 static void print_link_flags(FILE *fp
, unsigned int flags
, unsigned int mdown
)
87 open_json_array(PRINT_ANY
, is_json_context() ? "flags" : "<");
88 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
89 print_string(PRINT_ANY
, NULL
,
90 flags
? "%s," : "%s", "NO-CARRIER");
91 flags
&= ~IFF_RUNNING
;
92 #define _PF(f) if (flags&IFF_##f) { \
94 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
115 print_hex(PRINT_ANY
, NULL
, "%x", flags
);
117 print_string(PRINT_ANY
, NULL
, ",%s", "M-DOWN");
118 close_json_array(PRINT_ANY
, "> ");
121 static const char *oper_states
[] = {
122 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
123 "TESTING", "DORMANT", "UP"
126 static void print_operstate(FILE *f
, __u8 state
)
128 if (state
>= ARRAY_SIZE(oper_states
)) {
129 if (is_json_context())
130 print_uint(PRINT_JSON
, "operstate_index", NULL
, state
);
132 print_0xhex(PRINT_FP
, NULL
, "state %#x", state
);
134 print_color_string(PRINT_ANY
,
135 oper_state_color(state
),
140 if (is_json_context())
141 print_string(PRINT_JSON
,
143 NULL
, oper_states
[state
]);
145 fprintf(f
, "state ");
146 color_fprintf(f
, oper_state_color(state
),
147 "%s ", oper_states
[state
]);
152 int get_operstate(const char *name
)
156 for (i
= 0; i
< ARRAY_SIZE(oper_states
); i
++)
157 if (strcasecmp(name
, oper_states
[i
]) == 0)
162 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
167 qlen
= rta_getattr_u32(tb
[IFLA_TXQLEN
]);
169 struct ifreq ifr
= {};
170 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
175 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
176 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
177 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
185 print_int(PRINT_ANY
, "txqlen", "qlen %d", qlen
);
188 static const char *link_modes
[] = {
192 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
194 unsigned int mode
= rta_getattr_u8(tb
);
196 if (mode
>= ARRAY_SIZE(link_modes
))
202 print_string(PRINT_ANY
,
208 static char *parse_link_kind(struct rtattr
*tb
, bool slave
)
210 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
211 int attr
= slave
? IFLA_INFO_SLAVE_KIND
: IFLA_INFO_KIND
;
213 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
216 return RTA_DATA(linkinfo
[attr
]);
221 static int match_link_kind(struct rtattr
**tb
, const char *kind
, bool slave
)
223 if (!tb
[IFLA_LINKINFO
])
226 return strcmp(parse_link_kind(tb
[IFLA_LINKINFO
], slave
), kind
);
229 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
231 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
232 struct link_util
*lu
;
233 struct link_util
*slave_lu
;
236 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
237 open_json_object("linkinfo");
239 if (linkinfo
[IFLA_INFO_KIND
]) {
241 = rta_getattr_str(linkinfo
[IFLA_INFO_KIND
]);
243 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
244 print_string(PRINT_ANY
, "info_kind", " %s ", kind
);
246 lu
= get_link_kind(kind
);
247 if (lu
&& lu
->print_opt
) {
248 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
250 if (linkinfo
[IFLA_INFO_DATA
]) {
251 parse_rtattr_nested(attr
, lu
->maxattr
,
252 linkinfo
[IFLA_INFO_DATA
]);
255 open_json_object("info_data");
256 lu
->print_opt(lu
, fp
, data
);
259 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
261 open_json_object("info_xstats");
262 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
268 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
269 const char *slave_kind
270 = rta_getattr_str(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
272 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
273 print_string(PRINT_ANY
,
278 snprintf(slave
, sizeof(slave
), "%s_slave", slave_kind
);
280 slave_lu
= get_link_kind(slave
);
281 if (slave_lu
&& slave_lu
->print_opt
) {
282 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
284 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
285 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
286 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
289 open_json_object("info_slave_data");
290 slave_lu
->print_opt(slave_lu
, fp
, data
);
297 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
299 struct rtattr
*inet6_attr
;
300 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
302 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
306 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
308 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
309 __u8 mode
= rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
313 case IN6_ADDR_GEN_MODE_EUI64
:
314 print_string(PRINT_ANY
,
315 "inet6_addr_gen_mode",
319 case IN6_ADDR_GEN_MODE_NONE
:
320 print_string(PRINT_ANY
,
321 "inet6_addr_gen_mode",
325 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
326 print_string(PRINT_ANY
,
327 "inet6_addr_gen_mode",
331 case IN6_ADDR_GEN_MODE_RANDOM
:
332 print_string(PRINT_ANY
,
333 "inet6_addr_gen_mode",
338 snprintf(b1
, sizeof(b1
), "%#.2hhx", mode
);
339 print_string(PRINT_ANY
,
340 "inet6_addr_gen_mode",
348 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
350 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
352 struct ifla_vf_mac
*vf_mac
;
353 struct ifla_vf_tx_rate
*vf_tx_rate
;
354 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
358 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
359 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
363 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
365 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
366 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
368 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
369 print_int(PRINT_ANY
, "vf", "vf %d ", vf_mac
->vf
);
370 print_string(PRINT_ANY
, "mac", "MAC %s",
371 ll_addr_n2a((unsigned char *) &vf_mac
->mac
,
372 ETH_ALEN
, 0, b1
, sizeof(b1
)));
374 if (vf
[IFLA_VF_VLAN_LIST
]) {
375 struct rtattr
*i
, *vfvlanlist
= vf
[IFLA_VF_VLAN_LIST
];
376 int rem
= RTA_PAYLOAD(vfvlanlist
);
378 open_json_array(PRINT_JSON
, "vlan_list");
379 for (i
= RTA_DATA(vfvlanlist
);
380 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
381 struct ifla_vf_vlan_info
*vf_vlan_info
= RTA_DATA(i
);
384 open_json_object(NULL
);
385 if (vf_vlan_info
->vlan
)
390 if (vf_vlan_info
->qos
)
395 if (vf_vlan_info
->vlan_proto
&&
396 vf_vlan_info
->vlan_proto
!= htons(ETH_P_8021Q
))
397 print_string(PRINT_ANY
,
399 ", vlan protocol %s",
401 vf_vlan_info
->vlan_proto
,
405 close_json_array(PRINT_JSON
, NULL
);
407 struct ifla_vf_vlan
*vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
415 print_int(PRINT_ANY
, "qos", ", qos %d", vf_vlan
->qos
);
418 if (vf_tx_rate
->rate
)
421 ", tx rate %d (Mbps)",
424 if (vf
[IFLA_VF_RATE
]) {
425 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
426 int max_tx
= vf_rate
->max_tx_rate
;
427 int min_tx
= vf_rate
->min_tx_rate
;
429 if (is_json_context()) {
430 open_json_object("rate");
431 print_int(PRINT_JSON
, "max_tx", NULL
, max_tx
);
432 print_int(PRINT_ANY
, "min_tx", NULL
, min_tx
);
436 fprintf(fp
, ", max_tx_rate %dMbps", max_tx
);
438 fprintf(fp
, ", min_tx_rate %dMbps", min_tx
);
442 if (vf
[IFLA_VF_SPOOFCHK
]) {
443 struct ifla_vf_spoofchk
*vf_spoofchk
=
444 RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
446 if (vf_spoofchk
->setting
!= -1)
447 print_bool(PRINT_ANY
,
449 vf_spoofchk
->setting
?
450 ", spoof checking on" : ", spoof checking off",
451 vf_spoofchk
->setting
);
454 if (vf
[IFLA_VF_LINK_STATE
]) {
455 struct ifla_vf_link_state
*vf_linkstate
=
456 RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
458 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
459 print_string(PRINT_ANY
,
463 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
464 print_string(PRINT_ANY
,
469 print_string(PRINT_ANY
,
475 if (vf
[IFLA_VF_TRUST
]) {
476 struct ifla_vf_trust
*vf_trust
= RTA_DATA(vf
[IFLA_VF_TRUST
]);
478 if (vf_trust
->setting
!= -1)
479 print_bool(PRINT_ANY
,
481 vf_trust
->setting
? ", trust on" : ", trust off",
485 if (vf
[IFLA_VF_RSS_QUERY_EN
]) {
486 struct ifla_vf_rss_query_en
*rss_query
=
487 RTA_DATA(vf
[IFLA_VF_RSS_QUERY_EN
]);
489 if (rss_query
->setting
!= -1)
490 print_bool(PRINT_ANY
,
492 rss_query
->setting
? ", query_rss on"
497 if (vf
[IFLA_VF_STATS
] && show_stats
)
498 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
501 void print_num(FILE *fp
, unsigned int width
, uint64_t count
)
503 const char *prefix
= "kMGTPE";
504 const unsigned int base
= use_iec
? 1024 : 1000;
507 uint8_t precision
= 2;
510 if (!human_readable
|| count
< base
) {
511 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
515 /* increase value by a factor of 1000/1024 and print
516 * if result is something a human can read
520 if (count
/ base
< powi
)
528 /* try to guess a good number of digits for precision */
529 for (; precision
> 0; precision
--) {
531 if (count
/ powi
< powj
)
535 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
536 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
538 fprintf(fp
, "%-*s ", width
, buf
);
541 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
543 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1];
545 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
546 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
550 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfstats
);
552 if (is_json_context()) {
553 open_json_object("stats");
556 open_json_object("rx");
557 print_uint(PRINT_JSON
, "bytes", NULL
,
558 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
559 print_uint(PRINT_JSON
, "packets", NULL
,
560 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
561 print_uint(PRINT_JSON
, "multicast", NULL
,
562 rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
563 print_uint(PRINT_JSON
, "broadcast", NULL
,
564 rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
568 open_json_object("tx");
569 print_uint(PRINT_JSON
, "tx_bytes", NULL
,
570 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
571 print_uint(PRINT_JSON
, "tx_packets", NULL
,
572 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
577 fprintf(fp
, "%s", _SL_
);
578 fprintf(fp
, " RX: bytes packets mcast bcast %s", _SL_
);
581 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
582 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
583 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
584 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
587 fprintf(fp
, "%s", _SL_
);
588 fprintf(fp
, " TX: bytes packets %s", _SL_
);
591 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
592 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
596 static void __print_link_stats(FILE *fp
, struct rtattr
*tb
[])
598 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
599 struct rtnl_link_stats64 _s
, *s
= &_s
;
602 ret
= get_rtnl_link_stats_rta(s
, tb
);
606 if (is_json_context()) {
607 open_json_object((ret
== sizeof(*s
)) ? "stats64" : "stats");
610 open_json_object("rx");
611 print_uint(PRINT_JSON
, "bytes", NULL
, s
->rx_bytes
);
612 print_uint(PRINT_JSON
, "packets", NULL
, s
->rx_packets
);
613 print_uint(PRINT_JSON
, "errors", NULL
, s
->rx_errors
);
614 print_uint(PRINT_JSON
, "dropped", NULL
, s
->rx_dropped
);
615 print_uint(PRINT_JSON
, "over_errors", NULL
, s
->rx_over_errors
);
616 print_uint(PRINT_JSON
, "multicast", NULL
, s
->multicast
);
617 if (s
->rx_compressed
)
618 print_uint(PRINT_JSON
,
619 "compressed", NULL
, s
->rx_compressed
);
622 if (show_stats
> 1) {
623 print_uint(PRINT_JSON
,
625 NULL
, s
->rx_length_errors
);
626 print_uint(PRINT_JSON
,
628 NULL
, s
->rx_crc_errors
);
629 print_uint(PRINT_JSON
,
631 NULL
, s
->rx_frame_errors
);
632 print_uint(PRINT_JSON
,
634 NULL
, s
->rx_fifo_errors
);
635 print_uint(PRINT_JSON
,
637 NULL
, s
->rx_missed_errors
);
639 print_uint(PRINT_JSON
,
640 "nohandler", NULL
, s
->rx_nohandler
);
645 open_json_object("tx");
646 print_uint(PRINT_JSON
, "bytes", NULL
, s
->tx_bytes
);
647 print_uint(PRINT_JSON
, "packets", NULL
, s
->tx_packets
);
648 print_uint(PRINT_JSON
, "errors", NULL
, s
->tx_errors
);
649 print_uint(PRINT_JSON
, "dropped", NULL
, s
->tx_dropped
);
650 print_uint(PRINT_JSON
,
652 NULL
, s
->tx_carrier_errors
);
653 print_uint(PRINT_JSON
, "collisions", NULL
, s
->collisions
);
654 if (s
->tx_compressed
)
655 print_uint(PRINT_JSON
,
656 "compressed", NULL
, s
->tx_compressed
);
659 if (show_stats
> 1) {
660 print_uint(PRINT_JSON
,
662 NULL
, s
->tx_aborted_errors
);
663 print_uint(PRINT_JSON
,
665 NULL
, s
->tx_fifo_errors
);
666 print_uint(PRINT_JSON
,
668 NULL
, s
->tx_window_errors
);
669 print_uint(PRINT_JSON
,
671 NULL
, s
->tx_heartbeat_errors
);
673 print_uint(PRINT_JSON
, "carrier_changes", NULL
,
674 rta_getattr_u32(carrier_changes
));
681 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
682 s
->rx_compressed
? "compressed" : "", _SL_
);
685 print_num(fp
, 10, s
->rx_bytes
);
686 print_num(fp
, 8, s
->rx_packets
);
687 print_num(fp
, 7, s
->rx_errors
);
688 print_num(fp
, 7, s
->rx_dropped
);
689 print_num(fp
, 7, s
->rx_over_errors
);
690 print_num(fp
, 7, s
->multicast
);
691 if (s
->rx_compressed
)
692 print_num(fp
, 7, s
->rx_compressed
);
695 if (show_stats
> 1) {
696 fprintf(fp
, "%s", _SL_
);
697 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
698 s
->rx_nohandler
? " nohandler" : "", _SL_
);
700 print_num(fp
, 8, s
->rx_length_errors
);
701 print_num(fp
, 7, s
->rx_crc_errors
);
702 print_num(fp
, 7, s
->rx_frame_errors
);
703 print_num(fp
, 7, s
->rx_fifo_errors
);
704 print_num(fp
, 7, s
->rx_missed_errors
);
706 print_num(fp
, 7, s
->rx_nohandler
);
708 fprintf(fp
, "%s", _SL_
);
711 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
712 s
->tx_compressed
? "compressed" : "", _SL_
);
715 print_num(fp
, 10, s
->tx_bytes
);
716 print_num(fp
, 8, s
->tx_packets
);
717 print_num(fp
, 7, s
->tx_errors
);
718 print_num(fp
, 7, s
->tx_dropped
);
719 print_num(fp
, 7, s
->tx_carrier_errors
);
720 print_num(fp
, 7, s
->collisions
);
721 if (s
->tx_compressed
)
722 print_num(fp
, 7, s
->tx_compressed
);
725 if (show_stats
> 1) {
726 fprintf(fp
, "%s", _SL_
);
727 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
729 fprintf(fp
, " transns");
730 fprintf(fp
, "%s", _SL_
);
733 print_num(fp
, 8, s
->tx_aborted_errors
);
734 print_num(fp
, 7, s
->tx_fifo_errors
);
735 print_num(fp
, 7, s
->tx_window_errors
);
736 print_num(fp
, 7, s
->tx_heartbeat_errors
);
739 rta_getattr_u32(carrier_changes
));
744 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
746 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
747 struct rtattr
*tb
[IFLA_MAX
+1];
749 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
750 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
751 __print_link_stats(fp
, tb
);
752 fprintf(fp
, "%s", _SL_
);
755 int print_linkinfo_brief(const struct sockaddr_nl
*who
,
756 struct nlmsghdr
*n
, void *arg
)
758 FILE *fp
= (FILE *)arg
;
759 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
760 struct rtattr
*tb
[IFLA_MAX
+1];
761 int len
= n
->nlmsg_len
;
763 char buf
[32] = { 0, };
764 unsigned int m_flag
= 0;
766 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
769 len
-= NLMSG_LENGTH(sizeof(*ifi
));
773 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
775 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
778 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
779 if (tb
[IFLA_IFNAME
] == NULL
) {
780 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
781 name
= ll_idx_n2a(ifi
->ifi_index
);
783 name
= rta_getattr_str(tb
[IFLA_IFNAME
]);
787 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
788 fnmatch(filter
.label
, name
, 0))
791 if (tb
[IFLA_GROUP
]) {
792 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
794 if (filter
.group
!= -1 && group
!= filter
.group
)
798 if (tb
[IFLA_MASTER
]) {
799 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
801 if (filter
.master
> 0 && master
!= filter
.master
)
803 } else if (filter
.master
> 0)
806 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
809 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
812 if (n
->nlmsg_type
== RTM_DELLINK
)
813 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
816 int iflink
= rta_getattr_u32(tb
[IFLA_LINK
]);
819 snprintf(buf
, sizeof(buf
), "%s@NONE", name
);
820 print_null(PRINT_JSON
, "link", NULL
, NULL
);
822 const char *link
= ll_index_to_name(iflink
);
824 print_string(PRINT_JSON
, "link", NULL
, link
);
825 snprintf(buf
, sizeof(buf
), "%s@%s", name
, link
);
826 m_flag
= ll_index_to_flags(iflink
);
827 m_flag
= !(m_flag
& IFF_UP
);
830 snprintf(buf
, sizeof(buf
), "%s", name
);
832 print_string(PRINT_FP
, NULL
, "%-16s ", buf
);
833 print_string(PRINT_JSON
, "ifname", NULL
, name
);
835 if (tb
[IFLA_OPERSTATE
])
836 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
838 if (filter
.family
== AF_PACKET
) {
841 if (tb
[IFLA_ADDRESS
]) {
842 print_color_string(PRINT_ANY
, COLOR_MAC
,
845 RTA_DATA(tb
[IFLA_ADDRESS
]),
846 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
852 if (filter
.family
== AF_PACKET
) {
853 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
854 print_string(PRINT_FP
, NULL
, "%s", "\n");
861 static const char *link_events
[] = {
862 [IFLA_EVENT_NONE
] = "NONE",
863 [IFLA_EVENT_REBOOT
] = "REBOOT",
864 [IFLA_EVENT_FEATURES
] = "FEATURE CHANGE",
865 [IFLA_EVENT_BONDING_FAILOVER
] = "BONDING FAILOVER",
866 [IFLA_EVENT_NOTIFY_PEERS
] = "NOTIFY PEERS",
867 [IFLA_EVENT_IGMP_RESEND
] = "RESEND IGMP",
868 [IFLA_EVENT_BONDING_OPTIONS
] = "BONDING OPTION"
871 static void print_link_event(FILE *f
, __u32 event
)
873 if (event
>= ARRAY_SIZE(link_events
))
874 print_int(PRINT_ANY
, "event", "event %d ", event
);
877 print_string(PRINT_ANY
,
878 "event", "event %s ",
883 int print_linkinfo(const struct sockaddr_nl
*who
,
884 struct nlmsghdr
*n
, void *arg
)
886 FILE *fp
= (FILE *)arg
;
887 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
888 struct rtattr
*tb
[IFLA_MAX
+1];
889 int len
= n
->nlmsg_len
;
891 unsigned int m_flag
= 0;
893 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
896 len
-= NLMSG_LENGTH(sizeof(*ifi
));
900 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
902 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
905 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
906 if (tb
[IFLA_IFNAME
] == NULL
) {
907 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
908 name
= ll_idx_n2a(ifi
->ifi_index
);
910 name
= rta_getattr_str(tb
[IFLA_IFNAME
]);
914 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
915 fnmatch(filter
.label
, name
, 0))
918 if (tb
[IFLA_GROUP
]) {
919 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
921 if (filter
.group
!= -1 && group
!= filter
.group
)
925 if (tb
[IFLA_MASTER
]) {
926 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
928 if (filter
.master
> 0 && master
!= filter
.master
)
930 } else if (filter
.master
> 0)
933 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
936 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
939 if (n
->nlmsg_type
== RTM_DELLINK
)
940 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
942 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
943 print_color_string(PRINT_ANY
, COLOR_IFNAME
, "ifname", "%s", name
);
946 int iflink
= rta_getattr_u32(tb
[IFLA_LINK
]);
949 print_null(PRINT_ANY
, "link", "@%s: ", "NONE");
951 if (tb
[IFLA_LINK_NETNSID
])
953 "link_index", "@if%d: ", iflink
);
955 print_string(PRINT_ANY
,
958 ll_index_to_name(iflink
));
959 m_flag
= ll_index_to_flags(iflink
);
960 m_flag
= !(m_flag
& IFF_UP
);
964 print_string(PRINT_FP
, NULL
, ": ", NULL
);
966 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
971 rta_getattr_u32(tb
[IFLA_MTU
]));
973 xdp_dump(fp
, tb
[IFLA_XDP
], do_link
, false);
975 print_string(PRINT_ANY
,
978 rta_getattr_str(tb
[IFLA_QDISC
]));
979 if (tb
[IFLA_MASTER
]) {
980 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
982 print_color_string(PRINT_ANY
,
986 ll_index_to_name(master
));
989 if (tb
[IFLA_OPERSTATE
])
990 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
992 if (do_link
&& tb
[IFLA_LINKMODE
])
993 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
995 if (tb
[IFLA_GROUP
]) {
997 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
999 print_string(PRINT_ANY
,
1002 rtnl_group_n2a(group
, b1
, sizeof(b1
)));
1005 if (filter
.showqueue
)
1006 print_queuelen(fp
, tb
);
1009 print_link_event(fp
, rta_getattr_u32(tb
[IFLA_EVENT
]));
1011 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
1014 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
1015 print_string(PRINT_ANY
,
1018 ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
1019 if (tb
[IFLA_ADDRESS
]) {
1020 print_color_string(PRINT_ANY
,
1024 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
1025 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
1029 if (tb
[IFLA_BROADCAST
]) {
1030 if (ifi
->ifi_flags
&IFF_POINTOPOINT
) {
1031 print_string(PRINT_FP
, NULL
, " peer ", NULL
);
1032 print_bool(PRINT_JSON
,
1033 "link_pointtopoint", NULL
, true);
1035 print_string(PRINT_FP
, NULL
, " brd ", NULL
);
1037 print_color_string(PRINT_ANY
,
1041 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
1042 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
1048 if (tb
[IFLA_LINK_NETNSID
]) {
1049 int id
= rta_getattr_u32(tb
[IFLA_LINK_NETNSID
]);
1051 if (is_json_context()) {
1052 print_int(PRINT_JSON
, "link_netnsid", NULL
, id
);
1055 print_int(PRINT_FP
, NULL
,
1056 " link-netnsid %d", id
);
1058 print_string(PRINT_FP
, NULL
,
1059 " link-netnsid %s", "unknown");
1063 if (tb
[IFLA_PROTO_DOWN
]) {
1064 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
1065 print_bool(PRINT_ANY
,
1066 "proto_down", " protodown on ", true);
1070 if (tb
[IFLA_PROMISCUITY
])
1071 print_uint(PRINT_ANY
,
1074 rta_getattr_u32(tb
[IFLA_PROMISCUITY
]));
1076 if (tb
[IFLA_LINKINFO
])
1077 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
1079 if (do_link
&& tb
[IFLA_AF_SPEC
])
1080 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
1082 if (tb
[IFLA_NUM_TX_QUEUES
])
1083 print_uint(PRINT_ANY
,
1086 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
1088 if (tb
[IFLA_NUM_RX_QUEUES
])
1089 print_uint(PRINT_ANY
,
1092 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
1094 if (tb
[IFLA_GSO_MAX_SIZE
])
1095 print_uint(PRINT_ANY
,
1098 rta_getattr_u32(tb
[IFLA_GSO_MAX_SIZE
]));
1100 if (tb
[IFLA_GSO_MAX_SEGS
])
1101 print_uint(PRINT_ANY
,
1104 rta_getattr_u32(tb
[IFLA_GSO_MAX_SEGS
]));
1106 if (tb
[IFLA_PHYS_PORT_NAME
])
1107 print_string(PRINT_ANY
,
1110 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
1112 if (tb
[IFLA_PHYS_PORT_ID
]) {
1114 print_string(PRINT_ANY
,
1118 RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
1119 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
1123 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
1125 print_string(PRINT_ANY
,
1128 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
1129 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
1134 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
1135 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
1136 print_string(PRINT_ANY
,
1139 rta_getattr_str(tb
[IFLA_IFALIAS
]));
1142 if ((do_link
|| show_details
) && tb
[IFLA_XDP
])
1143 xdp_dump(fp
, tb
[IFLA_XDP
], true, true);
1145 if (do_link
&& show_stats
) {
1146 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
1147 __print_link_stats(fp
, tb
);
1150 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
1151 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
1152 int rem
= RTA_PAYLOAD(vflist
);
1154 open_json_array(PRINT_JSON
, "vfinfo_list");
1155 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1156 open_json_object(NULL
);
1157 print_vfinfo(fp
, i
);
1158 close_json_object();
1160 close_json_array(PRINT_JSON
, NULL
);
1163 print_string(PRINT_FP
, NULL
, "\n", NULL
);
1168 static int flush_update(void)
1172 * Note that the kernel may delete multiple addresses for one
1173 * delete request (e.g. if ipv4 address promotion is disabled).
1174 * Since a flush operation is really a series of delete requests
1175 * its possible that we may request an address delete that has
1176 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
1177 * errors returned from a flush request
1179 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
1180 (errno
!= EADDRNOTAVAIL
)) {
1181 perror("Failed to send flush request");
1188 static int set_lifetime(unsigned int *lifetime
, char *argv
)
1190 if (strcmp(argv
, "forever") == 0)
1191 *lifetime
= INFINITY_LIFE_TIME
;
1192 else if (get_u32(lifetime
, argv
, 0))
1198 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
1199 struct rtattr
*ifa_flags_attr
)
1201 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
1205 /* Mapping from argument to address flag mask */
1208 unsigned long value
;
1209 } ifa_flag_names
[] = {
1210 { "secondary", IFA_F_SECONDARY
},
1211 { "temporary", IFA_F_SECONDARY
},
1212 { "nodad", IFA_F_NODAD
},
1213 { "optimistic", IFA_F_OPTIMISTIC
},
1214 { "dadfailed", IFA_F_DADFAILED
},
1215 { "home", IFA_F_HOMEADDRESS
},
1216 { "deprecated", IFA_F_DEPRECATED
},
1217 { "tentative", IFA_F_TENTATIVE
},
1218 { "permanent", IFA_F_PERMANENT
},
1219 { "mngtmpaddr", IFA_F_MANAGETEMPADDR
},
1220 { "noprefixroute", IFA_F_NOPREFIXROUTE
},
1221 { "autojoin", IFA_F_MCAUTOJOIN
},
1222 { "stable-privacy", IFA_F_STABLE_PRIVACY
},
1225 static void print_ifa_flags(FILE *fp
, const struct ifaddrmsg
*ifa
,
1230 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1231 unsigned long mask
= ifa_flag_names
[i
].value
;
1233 if (mask
== IFA_F_PERMANENT
) {
1234 if (!(flags
& mask
))
1235 print_bool(PRINT_ANY
,
1236 "dynamic", "dynamic ", true);
1237 } else if (flags
& mask
) {
1238 if (mask
== IFA_F_SECONDARY
&&
1239 ifa
->ifa_family
== AF_INET6
) {
1240 print_bool(PRINT_ANY
,
1241 "temporary", "temporary ", true);
1243 print_string(PRINT_FP
, NULL
,
1244 "%s ", ifa_flag_names
[i
].name
);
1245 print_bool(PRINT_JSON
,
1246 ifa_flag_names
[i
].name
, NULL
, true);
1254 if (is_json_context()) {
1257 snprintf(b1
, sizeof(b1
), "%02x", flags
);
1258 print_string(PRINT_JSON
, "ifa_flags", NULL
, b1
);
1260 fprintf(fp
, "flags %02x ", flags
);
1266 static int get_filter(const char *arg
)
1271 if (strcmp(arg
, "dynamic") == 0) {
1272 filter
.flags
&= ~IFA_F_PERMANENT
;
1273 filter
.flagmask
|= IFA_F_PERMANENT
;
1274 } else if (strcmp(arg
, "primary") == 0) {
1275 filter
.flags
&= ~IFA_F_SECONDARY
;
1276 filter
.flagmask
|= IFA_F_SECONDARY
;
1277 } else if (*arg
== '-') {
1278 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1279 if (strcmp(arg
+ 1, ifa_flag_names
[i
].name
))
1282 filter
.flags
&= ifa_flag_names
[i
].value
;
1283 filter
.flagmask
|= ifa_flag_names
[i
].value
;
1289 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1290 if (strcmp(arg
, ifa_flag_names
[i
].name
))
1292 filter
.flags
|= ifa_flag_names
[i
].value
;
1293 filter
.flagmask
|= ifa_flag_names
[i
].value
;
1302 static int ifa_label_match_rta(int ifindex
, const struct rtattr
*rta
)
1310 label
= RTA_DATA(rta
);
1312 label
= ll_index_to_name(ifindex
);
1314 return fnmatch(filter
.label
, label
, 0);
1317 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1321 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1322 int len
= n
->nlmsg_len
;
1323 unsigned int ifa_flags
;
1324 struct rtattr
*rta_tb
[IFA_MAX
+1];
1328 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
1330 len
-= NLMSG_LENGTH(sizeof(*ifa
));
1332 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1336 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
1339 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
1340 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1342 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1344 if (!rta_tb
[IFA_LOCAL
])
1345 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1346 if (!rta_tb
[IFA_ADDRESS
])
1347 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1349 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1351 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1353 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1356 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1359 if (ifa_label_match_rta(ifa
->ifa_index
, rta_tb
[IFA_LABEL
]))
1362 if (inet_addr_match_rta(&filter
.pfx
, rta_tb
[IFA_LOCAL
]))
1365 if (filter
.flushb
) {
1366 struct nlmsghdr
*fn
;
1368 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1372 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1373 memcpy(fn
, n
, n
->nlmsg_len
);
1374 fn
->nlmsg_type
= RTM_DELADDR
;
1375 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1376 fn
->nlmsg_seq
= ++rth
.seq
;
1377 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1383 if (n
->nlmsg_type
== RTM_DELADDR
)
1384 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
1389 if (filter
.oneline
|| filter
.flushb
) {
1390 const char *dev
= ll_index_to_name(ifa
->ifa_index
);
1392 if (is_json_context()) {
1393 print_int(PRINT_JSON
,
1394 "index", NULL
, ifa
->ifa_index
);
1395 print_string(PRINT_JSON
, "dev", NULL
, dev
);
1397 fprintf(fp
, "%u: %s", ifa
->ifa_index
, dev
);
1401 name
= family_name(ifa
->ifa_family
);
1403 print_string(PRINT_ANY
, "family", " %s ", name
);
1405 print_int(PRINT_ANY
, "family_index", " family %d ",
1410 if (rta_tb
[IFA_LOCAL
]) {
1411 print_color_string(PRINT_ANY
,
1412 ifa_family_color(ifa
->ifa_family
),
1414 format_host_rta(ifa
->ifa_family
,
1415 rta_tb
[IFA_LOCAL
]));
1416 if (rta_tb
[IFA_ADDRESS
] &&
1417 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1418 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1419 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1420 print_string(PRINT_FP
, NULL
, " %s ", "peer");
1421 print_color_string(PRINT_ANY
,
1422 ifa_family_color(ifa
->ifa_family
),
1425 format_host_rta(ifa
->ifa_family
,
1426 rta_tb
[IFA_ADDRESS
]));
1428 print_int(PRINT_ANY
, "prefixlen", "/%d ", ifa
->ifa_prefixlen
);
1434 if (rta_tb
[IFA_BROADCAST
]) {
1435 print_string(PRINT_FP
, NULL
, "%s ", "brd");
1436 print_color_string(PRINT_ANY
,
1437 ifa_family_color(ifa
->ifa_family
),
1440 format_host_rta(ifa
->ifa_family
,
1441 rta_tb
[IFA_BROADCAST
]));
1444 if (rta_tb
[IFA_ANYCAST
]) {
1445 print_string(PRINT_FP
, NULL
, "%s ", "any");
1446 print_color_string(PRINT_ANY
,
1447 ifa_family_color(ifa
->ifa_family
),
1450 format_host_rta(ifa
->ifa_family
,
1451 rta_tb
[IFA_ANYCAST
]));
1454 print_string(PRINT_ANY
,
1457 rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1459 print_ifa_flags(fp
, ifa
, ifa_flags
);
1461 if (rta_tb
[IFA_LABEL
])
1462 print_string(PRINT_ANY
,
1465 rta_getattr_str(rta_tb
[IFA_LABEL
]));
1467 if (rta_tb
[IFA_CACHEINFO
]) {
1468 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1470 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
1471 print_string(PRINT_FP
, NULL
, " valid_lft ", NULL
);
1473 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
) {
1474 print_uint(PRINT_JSON
,
1476 NULL
, INFINITY_LIFE_TIME
);
1477 print_string(PRINT_FP
, NULL
, "%s", "forever");
1479 print_uint(PRINT_ANY
,
1480 "valid_life_time", "%usec", ci
->ifa_valid
);
1483 print_string(PRINT_FP
, NULL
, " preferred_lft ", NULL
);
1484 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
) {
1485 print_uint(PRINT_JSON
,
1486 "preferred_life_time",
1487 NULL
, INFINITY_LIFE_TIME
);
1488 print_string(PRINT_FP
, NULL
, "%s", "forever");
1490 if (ifa_flags
& IFA_F_DEPRECATED
)
1491 print_int(PRINT_ANY
,
1492 "preferred_life_time",
1496 print_uint(PRINT_ANY
,
1497 "preferred_life_time",
1502 print_string(PRINT_FP
, NULL
, "%s", "\n");
1508 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1509 struct nlmsg_list
*ainfo
, FILE *fp
)
1511 open_json_array(PRINT_JSON
, "addr_info");
1512 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1513 struct nlmsghdr
*n
= &ainfo
->h
;
1514 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1516 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1519 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ifa
)))
1522 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1523 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1526 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1529 open_json_object(NULL
);
1530 print_addrinfo(NULL
, n
, fp
);
1531 close_json_object();
1533 close_json_array(PRINT_JSON
, NULL
);
1536 print_string(PRINT_FP
, NULL
, "%s", "\n");
1543 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1546 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1547 struct nlmsg_list
*h
;
1549 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1553 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1557 lchain
->tail
->next
= h
;
1562 ll_remember_index(who
, n
, NULL
);
1566 static __u32 ipadd_dump_magic
= 0x47361222;
1568 static int ipadd_save_prep(void)
1572 if (isatty(STDOUT_FILENO
)) {
1573 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1577 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1578 if (ret
!= sizeof(ipadd_dump_magic
)) {
1579 fprintf(stderr
, "Can't write magic to dump file\n");
1586 static int ipadd_dump_check_magic(void)
1591 if (isatty(STDIN_FILENO
)) {
1592 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1596 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1597 if (magic
!= ipadd_dump_magic
) {
1598 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1605 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1610 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1611 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1612 fprintf(stderr
, "Short write while saving nlmsg\n");
1616 return ret
== n
->nlmsg_len
? 0 : ret
;
1619 static int show_handler(const struct sockaddr_nl
*nl
,
1620 struct rtnl_ctrl_data
*ctrl
,
1621 struct nlmsghdr
*n
, void *arg
)
1623 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1625 open_json_object(NULL
);
1626 print_int(PRINT_ANY
, "index", "if%d:\n", ifa
->ifa_index
);
1627 print_addrinfo(NULL
, n
, stdout
);
1628 close_json_object();
1632 static int ipaddr_showdump(void)
1636 if (ipadd_dump_check_magic())
1640 open_json_object(NULL
);
1641 open_json_array(PRINT_JSON
, "addr_info");
1643 err
= rtnl_from_file(stdin
, &show_handler
, NULL
);
1645 close_json_array(PRINT_JSON
, NULL
);
1646 close_json_object();
1652 static int restore_handler(const struct sockaddr_nl
*nl
,
1653 struct rtnl_ctrl_data
*ctrl
,
1654 struct nlmsghdr
*n
, void *arg
)
1658 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1662 ret
= rtnl_talk(&rth
, n
, NULL
);
1663 if ((ret
< 0) && (errno
== EEXIST
))
1669 static int ipaddr_restore(void)
1671 if (ipadd_dump_check_magic())
1674 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1677 void free_nlmsg_chain(struct nlmsg_chain
*info
)
1679 struct nlmsg_list
*l
, *n
;
1681 for (l
= info
->head
; l
; l
= n
) {
1687 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1689 struct nlmsg_list
*l
, **lp
;
1692 while ((l
= *lp
) != NULL
) {
1694 int missing_net_address
= 1;
1695 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1696 struct nlmsg_list
*a
;
1698 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1699 struct nlmsghdr
*n
= &a
->h
;
1700 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1701 struct rtattr
*tb
[IFA_MAX
+ 1];
1702 unsigned int ifa_flags
;
1704 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1706 missing_net_address
= 0;
1707 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1709 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1712 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1713 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1715 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1718 if (ifa_label_match_rta(ifa
->ifa_index
, tb
[IFA_LABEL
]))
1722 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1723 if (inet_addr_match_rta(&filter
.pfx
, tb
[IFA_LOCAL
]))
1729 if (missing_net_address
&&
1730 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1740 static int ipaddr_flush(void)
1743 char flushb
[4096-512];
1745 filter
.flushb
= flushb
;
1747 filter
.flushe
= sizeof(flushb
);
1749 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1750 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1751 perror("Cannot send dump request");
1755 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1756 stdout
, NLM_F_DUMP_INTR
) < 0) {
1757 fprintf(stderr
, "Flush terminated\n");
1760 if (filter
.flushed
== 0) {
1764 printf("Nothing to flush.\n");
1766 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1772 if (flush_update() < 0)
1776 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1780 /* If we are flushing, and specifying primary, then we
1781 * want to flush only a single round. Otherwise, we'll
1782 * start flushing secondaries that were promoted to
1785 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1788 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1793 static int iplink_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
1797 err
= addattr32(nlh
, reqlen
, IFLA_EXT_MASK
, RTEXT_FILTER_VF
);
1801 if (filter
.master
) {
1802 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter
.master
);
1808 struct rtattr
*linkinfo
;
1810 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
1812 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, filter
.kind
,
1813 strlen(filter
.kind
));
1817 addattr_nest_end(nlh
, linkinfo
);
1823 /* fills in linfo with link data and optionally ainfo with address info
1824 * caller can walk lists as desired and must call free_nlmsg_chain for
1827 int ip_linkaddr_list(int family
, req_filter_fn_t filter_fn
,
1828 struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1830 if (rtnl_wilddump_req_filter_fn(&rth
, preferred_family
, RTM_GETLINK
,
1832 perror("Cannot send dump request");
1836 if (rtnl_dump_filter(&rth
, store_nlmsg
, linfo
) < 0) {
1837 fprintf(stderr
, "Dump terminated\n");
1842 if (rtnl_wilddump_request(&rth
, family
, RTM_GETADDR
) < 0) {
1843 perror("Cannot send dump request");
1847 if (rtnl_dump_filter(&rth
, store_nlmsg
, ainfo
) < 0) {
1848 fprintf(stderr
, "Dump terminated\n");
1856 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1858 struct nlmsg_chain linfo
= { NULL
, NULL
};
1859 struct nlmsg_chain _ainfo
= { NULL
, NULL
}, *ainfo
= NULL
;
1860 struct nlmsg_list
*l
;
1861 char *filter_dev
= NULL
;
1864 ipaddr_reset_filter(oneline
, 0);
1865 filter
.showqueue
= 1;
1866 filter
.family
= preferred_family
;
1869 if (action
== IPADD_FLUSH
) {
1871 fprintf(stderr
, "Flush requires arguments.\n");
1875 if (filter
.family
== AF_PACKET
) {
1876 fprintf(stderr
, "Cannot flush link addresses.\n");
1882 if (strcmp(*argv
, "to") == 0) {
1884 if (get_prefix(&filter
.pfx
, *argv
, filter
.family
))
1885 invarg("invalid \"to\"\n", *argv
);
1886 if (filter
.family
== AF_UNSPEC
)
1887 filter
.family
= filter
.pfx
.family
;
1888 } else if (strcmp(*argv
, "scope") == 0) {
1889 unsigned int scope
= 0;
1892 filter
.scopemask
= -1;
1893 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1894 if (strcmp(*argv
, "all") != 0)
1895 invarg("invalid \"scope\"\n", *argv
);
1896 scope
= RT_SCOPE_NOWHERE
;
1897 filter
.scopemask
= 0;
1899 filter
.scope
= scope
;
1900 } else if (strcmp(*argv
, "up") == 0) {
1902 } else if (get_filter(*argv
) == 0) {
1904 } else if (strcmp(*argv
, "label") == 0) {
1906 filter
.label
= *argv
;
1907 } else if (strcmp(*argv
, "group") == 0) {
1909 if (rtnl_group_a2n(&filter
.group
, *argv
))
1910 invarg("Invalid \"group\" value\n", *argv
);
1911 } else if (strcmp(*argv
, "master") == 0) {
1915 ifindex
= ll_name_to_index(*argv
);
1917 invarg("Device does not exist\n", *argv
);
1918 filter
.master
= ifindex
;
1919 } else if (strcmp(*argv
, "vrf") == 0) {
1923 ifindex
= ll_name_to_index(*argv
);
1925 invarg("Not a valid VRF name\n", *argv
);
1926 if (!name_is_vrf(*argv
))
1927 invarg("Not a valid VRF name\n", *argv
);
1928 filter
.master
= ifindex
;
1929 } else if (strcmp(*argv
, "type") == 0) {
1933 soff
= strlen(*argv
) - strlen("_slave");
1934 if (!strcmp(*argv
+ soff
, "_slave")) {
1935 (*argv
)[soff
] = '\0';
1936 filter
.slave_kind
= *argv
;
1938 filter
.kind
= *argv
;
1941 if (strcmp(*argv
, "dev") == 0)
1943 else if (matches(*argv
, "help") == 0)
1946 duparg2("dev", *argv
);
1953 filter
.ifindex
= ll_name_to_index(filter_dev
);
1954 if (filter
.ifindex
<= 0) {
1955 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1960 if (action
== IPADD_FLUSH
)
1961 return ipaddr_flush();
1963 if (action
== IPADD_SAVE
) {
1964 if (ipadd_save_prep())
1967 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1968 perror("Cannot send dump request");
1972 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1973 fprintf(stderr
, "Save terminated\n");
1981 * Initialize a json_writer and open an array object
1982 * if -json was specified.
1987 * If only filter_dev present and none of the other
1988 * link filters are present, use RTM_GETLINK to get
1991 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1992 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1993 perror("Cannot send link get request");
2001 if (filter
.family
!= AF_PACKET
) {
2008 if (ip_linkaddr_list(filter
.family
, iplink_filter_req
,
2009 &linfo
, ainfo
) != 0)
2012 if (filter
.family
!= AF_PACKET
)
2013 ipaddr_filter(&linfo
, ainfo
);
2015 for (l
= linfo
.head
; l
; l
= l
->next
) {
2017 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
2019 open_json_object(NULL
);
2021 if (print_linkinfo_brief(NULL
, &l
->h
, stdout
) == 0)
2022 if (filter
.family
!= AF_PACKET
)
2023 print_selected_addrinfo(ifi
,
2026 } else if (no_link
||
2027 (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
2028 if (filter
.family
!= AF_PACKET
)
2029 print_selected_addrinfo(ifi
,
2030 ainfo
->head
, stdout
);
2031 if (res
> 0 && !do_link
&& show_stats
)
2032 print_link_stats(stdout
, &l
->h
);
2034 close_json_object();
2040 free_nlmsg_chain(ainfo
);
2041 free_nlmsg_chain(&linfo
);
2047 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
2049 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
2050 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
2051 struct ifla_vf_rate
*vf_rate
;
2054 rem
= RTA_PAYLOAD(vflist
);
2056 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
2057 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
2059 if (!vf
[IFLA_VF_RATE
]) {
2060 fprintf(stderr
, "VF min/max rate API not supported\n");
2064 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
2065 if (vf_rate
->vf
== vfnum
) {
2066 *min
= vf_rate
->min_tx_rate
;
2067 *max
= vf_rate
->max_tx_rate
;
2071 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
2075 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
2077 struct nlmsg_chain linfo
= { NULL
, NULL
};
2078 struct rtattr
*tb
[IFLA_MAX
+1];
2079 struct ifinfomsg
*ifi
;
2080 struct nlmsg_list
*l
;
2084 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
2085 perror("Cannot send dump request");
2088 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
2089 fprintf(stderr
, "Dump terminated\n");
2092 for (l
= linfo
.head
; l
; l
= l
->next
) {
2094 ifi
= NLMSG_DATA(n
);
2096 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
2097 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
2100 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
2102 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
2103 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
2109 int ipaddr_list_link(int argc
, char **argv
)
2111 preferred_family
= AF_PACKET
;
2113 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
2116 void ipaddr_reset_filter(int oneline
, int ifindex
)
2118 memset(&filter
, 0, sizeof(filter
));
2119 filter
.oneline
= oneline
;
2120 filter
.ifindex
= ifindex
;
2123 static int default_scope(inet_prefix
*lcl
)
2125 if (lcl
->family
== AF_INET
) {
2126 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
2127 return RT_SCOPE_HOST
;
2132 static bool ipaddr_is_multicast(inet_prefix
*a
)
2134 if (a
->family
== AF_INET
)
2135 return IN_MULTICAST(ntohl(a
->data
[0]));
2136 else if (a
->family
== AF_INET6
)
2137 return IN6_IS_ADDR_MULTICAST(a
->data
);
2142 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
2146 struct ifaddrmsg ifa
;
2149 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
2150 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
2151 .n
.nlmsg_type
= cmd
,
2152 .ifa
.ifa_family
= preferred_family
,
2156 char *lcl_arg
= NULL
;
2157 char *valid_lftp
= NULL
;
2158 char *preferred_lftp
= NULL
;
2159 inet_prefix lcl
= {};
2166 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
2167 __u32 valid_lft
= INFINITY_LIFE_TIME
;
2168 unsigned int ifa_flags
= 0;
2171 if (strcmp(*argv
, "peer") == 0 ||
2172 strcmp(*argv
, "remote") == 0) {
2176 duparg("peer", *argv
);
2177 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
2178 peer_len
= peer
.bytelen
;
2179 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2180 req
.ifa
.ifa_family
= peer
.family
;
2181 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
2182 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
2183 } else if (matches(*argv
, "broadcast") == 0 ||
2184 strcmp(*argv
, "brd") == 0) {
2189 duparg("broadcast", *argv
);
2190 if (strcmp(*argv
, "+") == 0)
2192 else if (strcmp(*argv
, "-") == 0)
2195 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2196 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2197 req
.ifa
.ifa_family
= addr
.family
;
2198 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
2199 brd_len
= addr
.bytelen
;
2201 } else if (strcmp(*argv
, "anycast") == 0) {
2206 duparg("anycast", *argv
);
2207 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2208 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2209 req
.ifa
.ifa_family
= addr
.family
;
2210 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
2211 any_len
= addr
.bytelen
;
2212 } else if (strcmp(*argv
, "scope") == 0) {
2213 unsigned int scope
= 0;
2216 if (rtnl_rtscope_a2n(&scope
, *argv
))
2217 invarg("invalid scope value.", *argv
);
2218 req
.ifa
.ifa_scope
= scope
;
2220 } else if (strcmp(*argv
, "dev") == 0) {
2223 } else if (strcmp(*argv
, "label") == 0) {
2226 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
2227 } else if (matches(*argv
, "valid_lft") == 0) {
2229 duparg("valid_lft", *argv
);
2232 if (set_lifetime(&valid_lft
, *argv
))
2233 invarg("valid_lft value", *argv
);
2234 } else if (matches(*argv
, "preferred_lft") == 0) {
2236 duparg("preferred_lft", *argv
);
2238 preferred_lftp
= *argv
;
2239 if (set_lifetime(&preferred_lft
, *argv
))
2240 invarg("preferred_lft value", *argv
);
2241 } else if (strcmp(*argv
, "home") == 0) {
2242 ifa_flags
|= IFA_F_HOMEADDRESS
;
2243 } else if (strcmp(*argv
, "nodad") == 0) {
2244 ifa_flags
|= IFA_F_NODAD
;
2245 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
2246 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
2247 } else if (strcmp(*argv
, "noprefixroute") == 0) {
2248 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
2249 } else if (strcmp(*argv
, "autojoin") == 0) {
2250 ifa_flags
|= IFA_F_MCAUTOJOIN
;
2252 if (strcmp(*argv
, "local") == 0)
2254 if (matches(*argv
, "help") == 0)
2257 duparg2("local", *argv
);
2259 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
2260 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2261 req
.ifa
.ifa_family
= lcl
.family
;
2262 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
2263 local_len
= lcl
.bytelen
;
2267 if (ifa_flags
<= 0xff)
2268 req
.ifa
.ifa_flags
= ifa_flags
;
2270 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
2273 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
2276 if (l
&& matches(d
, l
) != 0) {
2277 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
2281 if (peer_len
== 0 && local_len
) {
2282 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
2284 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
2285 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
2286 " This special behaviour is likely to disappear in further releases,\n"
2287 " fix your scripts!\n", lcl_arg
, local_len
*8);
2290 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
2293 if (req
.ifa
.ifa_prefixlen
== 0)
2294 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
2296 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
2300 if (req
.ifa
.ifa_family
!= AF_INET
) {
2301 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
2305 if (brd
.bitlen
<= 30) {
2306 for (i
= 31; i
>= brd
.bitlen
; i
--) {
2308 brd
.data
[0] |= htonl(1<<(31-i
));
2310 brd
.data
[0] &= ~htonl(1<<(31-i
));
2312 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
2313 brd_len
= brd
.bytelen
;
2316 if (!scoped
&& cmd
!= RTM_DELADDR
)
2317 req
.ifa
.ifa_scope
= default_scope(&lcl
);
2319 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
2320 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
2324 if (valid_lftp
|| preferred_lftp
) {
2325 struct ifa_cacheinfo cinfo
= {};
2328 fprintf(stderr
, "valid_lft is zero\n");
2331 if (valid_lft
< preferred_lft
) {
2332 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
2336 cinfo
.ifa_prefered
= preferred_lft
;
2337 cinfo
.ifa_valid
= valid_lft
;
2338 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
2342 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
2343 fprintf(stderr
, "autojoin needs multicast address\n");
2347 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
2353 int do_ipaddr(int argc
, char **argv
)
2356 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
2357 if (matches(*argv
, "add") == 0)
2358 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
2359 if (matches(*argv
, "change") == 0 ||
2360 strcmp(*argv
, "chg") == 0)
2361 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2362 if (matches(*argv
, "replace") == 0)
2363 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2364 if (matches(*argv
, "delete") == 0)
2365 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2366 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2367 || matches(*argv
, "lst") == 0)
2368 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2369 if (matches(*argv
, "flush") == 0)
2370 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2371 if (matches(*argv
, "save") == 0)
2372 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2373 if (matches(*argv
, "showdump") == 0)
2374 return ipaddr_showdump();
2375 if (matches(*argv
, "restore") == 0)
2376 return ipaddr_restore();
2377 if (matches(*argv
, "help") == 0)
2379 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);