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 ] [ metric METRIC ]\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
]);
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
]);
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
)
419 print_uint(PRINT_ANY
,
421 ", tx rate %u (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_uint(PRINT_JSON
, "max_tx", NULL
, max_tx
);
432 print_uint(PRINT_ANY
, "min_tx", NULL
, min_tx
);
436 fprintf(fp
, ", max_tx_rate %uMbps", max_tx
);
438 fprintf(fp
, ", min_tx_rate %uMbps", 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_u64(PRINT_JSON
, "bytes", NULL
,
558 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
559 print_u64(PRINT_JSON
, "packets", NULL
,
560 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
561 print_u64(PRINT_JSON
, "multicast", NULL
,
562 rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
563 print_u64(PRINT_JSON
, "broadcast", NULL
,
564 rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
565 if (vf
[IFLA_VF_STATS_RX_DROPPED
])
566 print_u64(PRINT_JSON
, "dropped", NULL
,
567 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_DROPPED
]));
571 open_json_object("tx");
572 print_u64(PRINT_JSON
, "tx_bytes", NULL
,
573 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
574 print_u64(PRINT_JSON
, "tx_packets", NULL
,
575 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
576 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
577 print_u64(PRINT_JSON
, "dropped", NULL
,
578 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_DROPPED
]));
583 fprintf(fp
, "%s", _SL_
);
584 fprintf(fp
, " RX: bytes packets mcast bcast ");
585 if (vf
[IFLA_VF_STATS_RX_DROPPED
])
586 fprintf(fp
, " dropped ");
587 fprintf(fp
, "%s", _SL_
);
590 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
591 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
592 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
593 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
594 if (vf
[IFLA_VF_STATS_RX_DROPPED
])
595 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_DROPPED
]));
598 fprintf(fp
, "%s", _SL_
);
599 fprintf(fp
, " TX: bytes packets ");
600 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
601 fprintf(fp
, " dropped ");
602 fprintf(fp
, "%s", _SL_
);
605 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
606 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
607 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
608 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_DROPPED
]));
612 static void __print_link_stats(FILE *fp
, struct rtattr
*tb
[])
614 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
615 struct rtnl_link_stats64 _s
, *s
= &_s
;
618 ret
= get_rtnl_link_stats_rta(s
, tb
);
622 if (is_json_context()) {
623 open_json_object((ret
== sizeof(*s
)) ? "stats64" : "stats");
626 open_json_object("rx");
627 print_u64(PRINT_JSON
, "bytes", NULL
, s
->rx_bytes
);
628 print_u64(PRINT_JSON
, "packets", NULL
, s
->rx_packets
);
629 print_u64(PRINT_JSON
, "errors", NULL
, s
->rx_errors
);
630 print_u64(PRINT_JSON
, "dropped", NULL
, s
->rx_dropped
);
631 print_u64(PRINT_JSON
, "over_errors", NULL
, s
->rx_over_errors
);
632 print_u64(PRINT_JSON
, "multicast", NULL
, s
->multicast
);
633 if (s
->rx_compressed
)
634 print_u64(PRINT_JSON
,
635 "compressed", NULL
, s
->rx_compressed
);
638 if (show_stats
> 1) {
639 print_u64(PRINT_JSON
,
641 NULL
, s
->rx_length_errors
);
642 print_u64(PRINT_JSON
,
644 NULL
, s
->rx_crc_errors
);
645 print_u64(PRINT_JSON
,
647 NULL
, s
->rx_frame_errors
);
648 print_u64(PRINT_JSON
,
650 NULL
, s
->rx_fifo_errors
);
651 print_u64(PRINT_JSON
,
653 NULL
, s
->rx_missed_errors
);
655 print_u64(PRINT_JSON
,
656 "nohandler", NULL
, s
->rx_nohandler
);
661 open_json_object("tx");
662 print_u64(PRINT_JSON
, "bytes", NULL
, s
->tx_bytes
);
663 print_u64(PRINT_JSON
, "packets", NULL
, s
->tx_packets
);
664 print_u64(PRINT_JSON
, "errors", NULL
, s
->tx_errors
);
665 print_u64(PRINT_JSON
, "dropped", NULL
, s
->tx_dropped
);
666 print_u64(PRINT_JSON
,
668 NULL
, s
->tx_carrier_errors
);
669 print_u64(PRINT_JSON
, "collisions", NULL
, s
->collisions
);
670 if (s
->tx_compressed
)
671 print_u64(PRINT_JSON
,
672 "compressed", NULL
, s
->tx_compressed
);
675 if (show_stats
> 1) {
676 print_u64(PRINT_JSON
,
678 NULL
, s
->tx_aborted_errors
);
679 print_u64(PRINT_JSON
,
681 NULL
, s
->tx_fifo_errors
);
682 print_u64(PRINT_JSON
,
684 NULL
, s
->tx_window_errors
);
685 print_u64(PRINT_JSON
,
687 NULL
, s
->tx_heartbeat_errors
);
689 print_u64(PRINT_JSON
, "carrier_changes", NULL
,
690 rta_getattr_u32(carrier_changes
));
697 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
698 s
->rx_compressed
? "compressed" : "", _SL_
);
701 print_num(fp
, 10, s
->rx_bytes
);
702 print_num(fp
, 8, s
->rx_packets
);
703 print_num(fp
, 7, s
->rx_errors
);
704 print_num(fp
, 7, s
->rx_dropped
);
705 print_num(fp
, 7, s
->rx_over_errors
);
706 print_num(fp
, 7, s
->multicast
);
707 if (s
->rx_compressed
)
708 print_num(fp
, 7, s
->rx_compressed
);
711 if (show_stats
> 1) {
712 fprintf(fp
, "%s", _SL_
);
713 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
714 s
->rx_nohandler
? " nohandler" : "", _SL_
);
716 print_num(fp
, 8, s
->rx_length_errors
);
717 print_num(fp
, 7, s
->rx_crc_errors
);
718 print_num(fp
, 7, s
->rx_frame_errors
);
719 print_num(fp
, 7, s
->rx_fifo_errors
);
720 print_num(fp
, 7, s
->rx_missed_errors
);
722 print_num(fp
, 7, s
->rx_nohandler
);
724 fprintf(fp
, "%s", _SL_
);
727 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
728 s
->tx_compressed
? "compressed" : "", _SL_
);
731 print_num(fp
, 10, s
->tx_bytes
);
732 print_num(fp
, 8, s
->tx_packets
);
733 print_num(fp
, 7, s
->tx_errors
);
734 print_num(fp
, 7, s
->tx_dropped
);
735 print_num(fp
, 7, s
->tx_carrier_errors
);
736 print_num(fp
, 7, s
->collisions
);
737 if (s
->tx_compressed
)
738 print_num(fp
, 7, s
->tx_compressed
);
741 if (show_stats
> 1) {
742 fprintf(fp
, "%s", _SL_
);
743 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
745 fprintf(fp
, " transns");
746 fprintf(fp
, "%s", _SL_
);
749 print_num(fp
, 8, s
->tx_aborted_errors
);
750 print_num(fp
, 7, s
->tx_fifo_errors
);
751 print_num(fp
, 7, s
->tx_window_errors
);
752 print_num(fp
, 7, s
->tx_heartbeat_errors
);
755 rta_getattr_u32(carrier_changes
));
760 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
762 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
763 struct rtattr
*tb
[IFLA_MAX
+1];
765 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
766 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
767 __print_link_stats(fp
, tb
);
771 static int print_linkinfo_brief(FILE *fp
, const char *name
,
772 const struct ifinfomsg
*ifi
,
775 unsigned int m_flag
= 0;
777 m_flag
= print_name_and_link("%-16s ", name
, tb
);
779 if (tb
[IFLA_OPERSTATE
])
780 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
782 if (filter
.family
== AF_PACKET
) {
785 if (tb
[IFLA_ADDRESS
]) {
786 print_color_string(PRINT_ANY
, COLOR_MAC
,
789 RTA_DATA(tb
[IFLA_ADDRESS
]),
790 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
796 if (filter
.family
== AF_PACKET
) {
797 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
798 print_string(PRINT_FP
, NULL
, "%s", "\n");
805 static const char *link_events
[] = {
806 [IFLA_EVENT_NONE
] = "NONE",
807 [IFLA_EVENT_REBOOT
] = "REBOOT",
808 [IFLA_EVENT_FEATURES
] = "FEATURE CHANGE",
809 [IFLA_EVENT_BONDING_FAILOVER
] = "BONDING FAILOVER",
810 [IFLA_EVENT_NOTIFY_PEERS
] = "NOTIFY PEERS",
811 [IFLA_EVENT_IGMP_RESEND
] = "RESEND IGMP",
812 [IFLA_EVENT_BONDING_OPTIONS
] = "BONDING OPTION"
815 static void print_link_event(FILE *f
, __u32 event
)
817 if (event
>= ARRAY_SIZE(link_events
))
818 print_int(PRINT_ANY
, "event", "event %d ", event
);
821 print_string(PRINT_ANY
,
822 "event", "event %s ",
827 int print_linkinfo(struct nlmsghdr
*n
, void *arg
)
829 FILE *fp
= (FILE *)arg
;
830 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
831 struct rtattr
*tb
[IFLA_MAX
+1];
832 int len
= n
->nlmsg_len
;
834 unsigned int m_flag
= 0;
837 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
840 len
-= NLMSG_LENGTH(sizeof(*ifi
));
844 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
846 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
849 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
851 name
= get_ifname_rta(ifi
->ifi_index
, tb
[IFLA_IFNAME
]);
858 if (tb
[IFLA_GROUP
]) {
859 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
861 if (filter
.group
!= -1 && group
!= filter
.group
)
865 if (tb
[IFLA_MASTER
]) {
866 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
868 if (filter
.master
> 0 && master
!= filter
.master
)
870 } else if (filter
.master
> 0)
873 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
876 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
879 if (n
->nlmsg_type
== RTM_DELLINK
)
880 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
883 return print_linkinfo_brief(fp
, name
, ifi
, tb
);
885 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
887 m_flag
= print_name_and_link("%s: ", name
, tb
);
888 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
893 rta_getattr_u32(tb
[IFLA_MTU
]));
895 xdp_dump(fp
, tb
[IFLA_XDP
], do_link
, false);
897 print_string(PRINT_ANY
,
900 rta_getattr_str(tb
[IFLA_QDISC
]));
901 if (tb
[IFLA_MASTER
]) {
902 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
904 print_string(PRINT_ANY
,
905 "master", "master %s ",
906 ll_index_to_name(master
));
909 if (tb
[IFLA_OPERSTATE
])
910 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
912 if (do_link
&& tb
[IFLA_LINKMODE
])
913 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
915 if (tb
[IFLA_GROUP
]) {
916 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
918 print_string(PRINT_ANY
,
921 rtnl_group_n2a(group
, b1
, sizeof(b1
)));
924 if (filter
.showqueue
)
925 print_queuelen(fp
, tb
);
928 print_link_event(fp
, rta_getattr_u32(tb
[IFLA_EVENT
]));
930 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
932 print_string(PRINT_ANY
,
935 ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
936 if (tb
[IFLA_ADDRESS
]) {
937 print_color_string(PRINT_ANY
,
941 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
942 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
946 if (tb
[IFLA_BROADCAST
]) {
947 if (ifi
->ifi_flags
&IFF_POINTOPOINT
) {
948 print_string(PRINT_FP
, NULL
, " peer ", NULL
);
949 print_bool(PRINT_JSON
,
950 "link_pointtopoint", NULL
, true);
952 print_string(PRINT_FP
, NULL
, " brd ", NULL
);
954 print_color_string(PRINT_ANY
,
958 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
959 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
965 if (tb
[IFLA_LINK_NETNSID
]) {
966 int id
= rta_getattr_u32(tb
[IFLA_LINK_NETNSID
]);
968 if (is_json_context()) {
969 print_int(PRINT_JSON
, "link_netnsid", NULL
, id
);
972 char *name
= get_name_from_nsid(id
);
975 print_string(PRINT_FP
, NULL
,
976 " link-netns %s", name
);
978 print_int(PRINT_FP
, NULL
,
979 " link-netnsid %d", id
);
981 print_string(PRINT_FP
, NULL
,
982 " link-netnsid %s", "unknown");
986 if (tb
[IFLA_NEW_NETNSID
]) {
987 int id
= rta_getattr_u32(tb
[IFLA_NEW_NETNSID
]);
988 char *name
= get_name_from_nsid(id
);
991 print_string(PRINT_FP
, NULL
, " new-netns %s", name
);
993 print_int(PRINT_FP
, NULL
, " new-netnsid %d", id
);
995 if (tb
[IFLA_NEW_IFINDEX
]) {
996 int id
= rta_getattr_u32(tb
[IFLA_NEW_IFINDEX
]);
998 print_int(PRINT_FP
, NULL
, " new-ifindex %d", id
);
1001 if (tb
[IFLA_PROTO_DOWN
]) {
1002 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
1003 print_bool(PRINT_ANY
,
1004 "proto_down", " protodown on ", true);
1008 if (tb
[IFLA_PROMISCUITY
])
1009 print_uint(PRINT_ANY
,
1012 rta_getattr_u32(tb
[IFLA_PROMISCUITY
]));
1014 if (tb
[IFLA_MIN_MTU
])
1015 print_uint(PRINT_ANY
,
1016 "min_mtu", "minmtu %u ",
1017 rta_getattr_u32(tb
[IFLA_MIN_MTU
]));
1019 if (tb
[IFLA_MAX_MTU
])
1020 print_uint(PRINT_ANY
,
1021 "max_mtu", "maxmtu %u ",
1022 rta_getattr_u32(tb
[IFLA_MAX_MTU
]));
1024 if (tb
[IFLA_LINKINFO
])
1025 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
1027 if (do_link
&& tb
[IFLA_AF_SPEC
])
1028 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
1030 if (tb
[IFLA_NUM_TX_QUEUES
])
1031 print_uint(PRINT_ANY
,
1034 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
1036 if (tb
[IFLA_NUM_RX_QUEUES
])
1037 print_uint(PRINT_ANY
,
1040 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
1042 if (tb
[IFLA_GSO_MAX_SIZE
])
1043 print_uint(PRINT_ANY
,
1046 rta_getattr_u32(tb
[IFLA_GSO_MAX_SIZE
]));
1048 if (tb
[IFLA_GSO_MAX_SEGS
])
1049 print_uint(PRINT_ANY
,
1052 rta_getattr_u32(tb
[IFLA_GSO_MAX_SEGS
]));
1054 if (tb
[IFLA_PHYS_PORT_NAME
])
1055 print_string(PRINT_ANY
,
1058 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
1060 if (tb
[IFLA_PHYS_PORT_ID
]) {
1061 print_string(PRINT_ANY
,
1065 RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
1066 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
1070 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
1071 print_string(PRINT_ANY
,
1074 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
1075 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
1080 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
1081 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
1082 print_string(PRINT_ANY
,
1085 rta_getattr_str(tb
[IFLA_IFALIAS
]));
1088 if ((do_link
|| show_details
) && tb
[IFLA_XDP
])
1089 xdp_dump(fp
, tb
[IFLA_XDP
], true, true);
1091 if (do_link
&& show_stats
) {
1093 __print_link_stats(fp
, tb
);
1096 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
1097 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
1098 int rem
= RTA_PAYLOAD(vflist
);
1100 open_json_array(PRINT_JSON
, "vfinfo_list");
1101 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1102 open_json_object(NULL
);
1103 print_vfinfo(fp
, i
);
1104 close_json_object();
1106 close_json_array(PRINT_JSON
, NULL
);
1109 print_string(PRINT_FP
, NULL
, "%s", "\n");
1114 static int flush_update(void)
1118 * Note that the kernel may delete multiple addresses for one
1119 * delete request (e.g. if ipv4 address promotion is disabled).
1120 * Since a flush operation is really a series of delete requests
1121 * its possible that we may request an address delete that has
1122 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
1123 * errors returned from a flush request
1125 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
1126 (errno
!= EADDRNOTAVAIL
)) {
1127 perror("Failed to send flush request");
1134 static int set_lifetime(unsigned int *lifetime
, char *argv
)
1136 if (strcmp(argv
, "forever") == 0)
1137 *lifetime
= INFINITY_LIFE_TIME
;
1138 else if (get_u32(lifetime
, argv
, 0))
1144 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
1145 struct rtattr
*ifa_flags_attr
)
1147 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
1151 /* Mapping from argument to address flag mask */
1154 unsigned long value
;
1155 } ifa_flag_names
[] = {
1156 { "secondary", IFA_F_SECONDARY
},
1157 { "temporary", IFA_F_SECONDARY
},
1158 { "nodad", IFA_F_NODAD
},
1159 { "optimistic", IFA_F_OPTIMISTIC
},
1160 { "dadfailed", IFA_F_DADFAILED
},
1161 { "home", IFA_F_HOMEADDRESS
},
1162 { "deprecated", IFA_F_DEPRECATED
},
1163 { "tentative", IFA_F_TENTATIVE
},
1164 { "permanent", IFA_F_PERMANENT
},
1165 { "mngtmpaddr", IFA_F_MANAGETEMPADDR
},
1166 { "noprefixroute", IFA_F_NOPREFIXROUTE
},
1167 { "autojoin", IFA_F_MCAUTOJOIN
},
1168 { "stable-privacy", IFA_F_STABLE_PRIVACY
},
1171 static void print_ifa_flags(FILE *fp
, const struct ifaddrmsg
*ifa
,
1176 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1177 unsigned long mask
= ifa_flag_names
[i
].value
;
1179 if (mask
== IFA_F_PERMANENT
) {
1180 if (!(flags
& mask
))
1181 print_bool(PRINT_ANY
,
1182 "dynamic", "dynamic ", true);
1183 } else if (flags
& mask
) {
1184 if (mask
== IFA_F_SECONDARY
&&
1185 ifa
->ifa_family
== AF_INET6
) {
1186 print_bool(PRINT_ANY
,
1187 "temporary", "temporary ", true);
1189 print_string(PRINT_FP
, NULL
,
1190 "%s ", ifa_flag_names
[i
].name
);
1191 print_bool(PRINT_JSON
,
1192 ifa_flag_names
[i
].name
, NULL
, true);
1200 if (is_json_context()) {
1203 snprintf(b1
, sizeof(b1
), "%02x", flags
);
1204 print_string(PRINT_JSON
, "ifa_flags", NULL
, b1
);
1206 fprintf(fp
, "flags %02x ", flags
);
1212 static int get_filter(const char *arg
)
1217 if (strcmp(arg
, "dynamic") == 0) {
1218 filter
.flags
&= ~IFA_F_PERMANENT
;
1219 filter
.flagmask
|= IFA_F_PERMANENT
;
1220 } else if (strcmp(arg
, "primary") == 0) {
1221 filter
.flags
&= ~IFA_F_SECONDARY
;
1222 filter
.flagmask
|= IFA_F_SECONDARY
;
1223 } else if (*arg
== '-') {
1224 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1225 if (strcmp(arg
+ 1, ifa_flag_names
[i
].name
))
1228 filter
.flags
&= ifa_flag_names
[i
].value
;
1229 filter
.flagmask
|= ifa_flag_names
[i
].value
;
1235 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1236 if (strcmp(arg
, ifa_flag_names
[i
].name
))
1238 filter
.flags
|= ifa_flag_names
[i
].value
;
1239 filter
.flagmask
|= ifa_flag_names
[i
].value
;
1248 static int ifa_label_match_rta(int ifindex
, const struct rtattr
*rta
)
1256 label
= RTA_DATA(rta
);
1258 label
= ll_index_to_name(ifindex
);
1260 return fnmatch(filter
.label
, label
, 0);
1263 int print_addrinfo(struct nlmsghdr
*n
, void *arg
)
1266 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1267 int len
= n
->nlmsg_len
;
1268 unsigned int ifa_flags
;
1269 struct rtattr
*rta_tb
[IFA_MAX
+1];
1273 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
1275 len
-= NLMSG_LENGTH(sizeof(*ifa
));
1277 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1281 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
1284 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
1285 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1287 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1289 if (!rta_tb
[IFA_LOCAL
])
1290 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1291 if (!rta_tb
[IFA_ADDRESS
])
1292 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1294 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1296 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1298 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1301 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1304 if (ifa_label_match_rta(ifa
->ifa_index
, rta_tb
[IFA_LABEL
]))
1307 if (inet_addr_match_rta(&filter
.pfx
, rta_tb
[IFA_LOCAL
]))
1310 if (filter
.flushb
) {
1311 struct nlmsghdr
*fn
;
1313 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1317 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1318 memcpy(fn
, n
, n
->nlmsg_len
);
1319 fn
->nlmsg_type
= RTM_DELADDR
;
1320 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1321 fn
->nlmsg_seq
= ++rth
.seq
;
1322 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1328 if (n
->nlmsg_type
== RTM_DELADDR
)
1329 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
1334 if (filter
.oneline
|| filter
.flushb
) {
1335 const char *dev
= ll_index_to_name(ifa
->ifa_index
);
1337 if (is_json_context()) {
1338 print_int(PRINT_JSON
,
1339 "index", NULL
, ifa
->ifa_index
);
1340 print_string(PRINT_JSON
, "dev", NULL
, dev
);
1342 fprintf(fp
, "%u: %s", ifa
->ifa_index
, dev
);
1346 name
= family_name(ifa
->ifa_family
);
1348 print_string(PRINT_ANY
, "family", " %s ", name
);
1350 print_int(PRINT_ANY
, "family_index", " family %d ",
1355 if (rta_tb
[IFA_LOCAL
]) {
1356 print_color_string(PRINT_ANY
,
1357 ifa_family_color(ifa
->ifa_family
),
1359 format_host_rta(ifa
->ifa_family
,
1360 rta_tb
[IFA_LOCAL
]));
1361 if (rta_tb
[IFA_ADDRESS
] &&
1362 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1363 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1364 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1365 print_string(PRINT_FP
, NULL
, " %s ", "peer");
1366 print_color_string(PRINT_ANY
,
1367 ifa_family_color(ifa
->ifa_family
),
1370 format_host_rta(ifa
->ifa_family
,
1371 rta_tb
[IFA_ADDRESS
]));
1373 print_int(PRINT_ANY
, "prefixlen", "/%d ", ifa
->ifa_prefixlen
);
1375 if (rta_tb
[IFA_RT_PRIORITY
])
1376 print_uint(PRINT_ANY
, "metric", "metric %u ",
1377 rta_getattr_u32(rta_tb
[IFA_RT_PRIORITY
]));
1383 if (rta_tb
[IFA_BROADCAST
]) {
1384 print_string(PRINT_FP
, NULL
, "%s ", "brd");
1385 print_color_string(PRINT_ANY
,
1386 ifa_family_color(ifa
->ifa_family
),
1389 format_host_rta(ifa
->ifa_family
,
1390 rta_tb
[IFA_BROADCAST
]));
1393 if (rta_tb
[IFA_ANYCAST
]) {
1394 print_string(PRINT_FP
, NULL
, "%s ", "any");
1395 print_color_string(PRINT_ANY
,
1396 ifa_family_color(ifa
->ifa_family
),
1399 format_host_rta(ifa
->ifa_family
,
1400 rta_tb
[IFA_ANYCAST
]));
1403 print_string(PRINT_ANY
,
1406 rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1408 print_ifa_flags(fp
, ifa
, ifa_flags
);
1410 if (rta_tb
[IFA_LABEL
])
1411 print_string(PRINT_ANY
,
1414 rta_getattr_str(rta_tb
[IFA_LABEL
]));
1416 if (rta_tb
[IFA_CACHEINFO
]) {
1417 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1420 print_string(PRINT_FP
, NULL
, " valid_lft ", NULL
);
1422 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
) {
1423 print_uint(PRINT_JSON
,
1425 NULL
, INFINITY_LIFE_TIME
);
1426 print_string(PRINT_FP
, NULL
, "%s", "forever");
1428 print_uint(PRINT_ANY
,
1429 "valid_life_time", "%usec", ci
->ifa_valid
);
1432 print_string(PRINT_FP
, NULL
, " preferred_lft ", NULL
);
1433 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
) {
1434 print_uint(PRINT_JSON
,
1435 "preferred_life_time",
1436 NULL
, INFINITY_LIFE_TIME
);
1437 print_string(PRINT_FP
, NULL
, "%s", "forever");
1439 if (ifa_flags
& IFA_F_DEPRECATED
)
1440 print_int(PRINT_ANY
,
1441 "preferred_life_time",
1445 print_uint(PRINT_ANY
,
1446 "preferred_life_time",
1451 print_string(PRINT_FP
, NULL
, "%s", "\n");
1457 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1458 struct nlmsg_list
*ainfo
, FILE *fp
)
1460 open_json_array(PRINT_JSON
, "addr_info");
1461 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1462 struct nlmsghdr
*n
= &ainfo
->h
;
1463 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1465 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1468 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ifa
)))
1471 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1472 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1475 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1478 open_json_object(NULL
);
1479 print_addrinfo(n
, fp
);
1480 close_json_object();
1482 close_json_array(PRINT_JSON
, NULL
);
1485 print_string(PRINT_FP
, NULL
, "%s", "\n");
1492 static int store_nlmsg(struct nlmsghdr
*n
, void *arg
)
1494 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1495 struct nlmsg_list
*h
;
1497 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1501 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1505 lchain
->tail
->next
= h
;
1510 ll_remember_index(n
, NULL
);
1514 static __u32 ipadd_dump_magic
= 0x47361222;
1516 static int ipadd_save_prep(void)
1520 if (isatty(STDOUT_FILENO
)) {
1521 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1525 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1526 if (ret
!= sizeof(ipadd_dump_magic
)) {
1527 fprintf(stderr
, "Can't write magic to dump file\n");
1534 static int ipadd_dump_check_magic(void)
1539 if (isatty(STDIN_FILENO
)) {
1540 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1544 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1545 if (magic
!= ipadd_dump_magic
) {
1546 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1553 static int save_nlmsg(struct nlmsghdr
*n
, void *arg
)
1557 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1558 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1559 fprintf(stderr
, "Short write while saving nlmsg\n");
1563 return ret
== n
->nlmsg_len
? 0 : ret
;
1566 static int show_handler(struct rtnl_ctrl_data
*ctrl
,
1567 struct nlmsghdr
*n
, void *arg
)
1569 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1571 open_json_object(NULL
);
1572 print_int(PRINT_ANY
, "index", "if%d:\n", ifa
->ifa_index
);
1573 print_addrinfo(n
, stdout
);
1574 close_json_object();
1578 static int ipaddr_showdump(void)
1582 if (ipadd_dump_check_magic())
1586 open_json_object(NULL
);
1587 open_json_array(PRINT_JSON
, "addr_info");
1589 err
= rtnl_from_file(stdin
, &show_handler
, NULL
);
1591 close_json_array(PRINT_JSON
, NULL
);
1592 close_json_object();
1598 static int restore_handler(struct rtnl_ctrl_data
*ctrl
,
1599 struct nlmsghdr
*n
, void *arg
)
1603 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1607 ret
= rtnl_talk(&rth
, n
, NULL
);
1608 if ((ret
< 0) && (errno
== EEXIST
))
1614 static int ipaddr_restore(void)
1616 if (ipadd_dump_check_magic())
1619 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1622 void free_nlmsg_chain(struct nlmsg_chain
*info
)
1624 struct nlmsg_list
*l
, *n
;
1626 for (l
= info
->head
; l
; l
= n
) {
1632 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1634 struct nlmsg_list
*l
, **lp
;
1637 while ((l
= *lp
) != NULL
) {
1639 int missing_net_address
= 1;
1640 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1641 struct nlmsg_list
*a
;
1643 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1644 struct nlmsghdr
*n
= &a
->h
;
1645 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1646 struct rtattr
*tb
[IFA_MAX
+ 1];
1647 unsigned int ifa_flags
;
1649 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1651 missing_net_address
= 0;
1652 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1654 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1657 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1658 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1660 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1663 if (ifa_label_match_rta(ifa
->ifa_index
, tb
[IFA_LABEL
]))
1667 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1668 if (inet_addr_match_rta(&filter
.pfx
, tb
[IFA_LOCAL
]))
1674 if (missing_net_address
&&
1675 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1685 static int ipaddr_flush(void)
1688 char flushb
[4096-512];
1690 filter
.flushb
= flushb
;
1692 filter
.flushe
= sizeof(flushb
);
1694 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1695 if (rtnl_addrdump_req(&rth
, filter
.family
) < 0) {
1696 perror("Cannot send dump request");
1700 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1701 stdout
, NLM_F_DUMP_INTR
) < 0) {
1702 fprintf(stderr
, "Flush terminated\n");
1705 if (filter
.flushed
== 0) {
1709 printf("Nothing to flush.\n");
1711 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1717 if (flush_update() < 0)
1721 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1725 /* If we are flushing, and specifying primary, then we
1726 * want to flush only a single round. Otherwise, we'll
1727 * start flushing secondaries that were promoted to
1730 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1733 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1738 static int iplink_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
1742 err
= addattr32(nlh
, reqlen
, IFLA_EXT_MASK
, RTEXT_FILTER_VF
);
1746 if (filter
.master
) {
1747 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter
.master
);
1753 struct rtattr
*linkinfo
;
1755 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
1757 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, filter
.kind
,
1758 strlen(filter
.kind
));
1762 addattr_nest_end(nlh
, linkinfo
);
1768 /* fills in linfo with link data and optionally ainfo with address info
1769 * caller can walk lists as desired and must call free_nlmsg_chain for
1772 int ip_linkaddr_list(int family
, req_filter_fn_t filter_fn
,
1773 struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1775 if (rtnl_linkdump_req_filter_fn(&rth
, preferred_family
,
1777 perror("Cannot send dump request");
1781 if (rtnl_dump_filter(&rth
, store_nlmsg
, linfo
) < 0) {
1782 fprintf(stderr
, "Dump terminated\n");
1787 if (rtnl_addrdump_req(&rth
, family
) < 0) {
1788 perror("Cannot send dump request");
1792 if (rtnl_dump_filter(&rth
, store_nlmsg
, ainfo
) < 0) {
1793 fprintf(stderr
, "Dump terminated\n");
1801 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1803 struct nlmsg_chain linfo
= { NULL
, NULL
};
1804 struct nlmsg_chain _ainfo
= { NULL
, NULL
}, *ainfo
= NULL
;
1805 struct nlmsg_list
*l
;
1806 char *filter_dev
= NULL
;
1809 ipaddr_reset_filter(oneline
, 0);
1810 filter
.showqueue
= 1;
1811 filter
.family
= preferred_family
;
1814 if (action
== IPADD_FLUSH
) {
1816 fprintf(stderr
, "Flush requires arguments.\n");
1820 if (filter
.family
== AF_PACKET
) {
1821 fprintf(stderr
, "Cannot flush link addresses.\n");
1827 if (strcmp(*argv
, "to") == 0) {
1829 if (get_prefix(&filter
.pfx
, *argv
, filter
.family
))
1830 invarg("invalid \"to\"\n", *argv
);
1831 if (filter
.family
== AF_UNSPEC
)
1832 filter
.family
= filter
.pfx
.family
;
1833 } else if (strcmp(*argv
, "scope") == 0) {
1834 unsigned int scope
= 0;
1837 filter
.scopemask
= -1;
1838 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1839 if (strcmp(*argv
, "all") != 0)
1840 invarg("invalid \"scope\"\n", *argv
);
1841 scope
= RT_SCOPE_NOWHERE
;
1842 filter
.scopemask
= 0;
1844 filter
.scope
= scope
;
1845 } else if (strcmp(*argv
, "up") == 0) {
1847 } else if (get_filter(*argv
) == 0) {
1849 } else if (strcmp(*argv
, "label") == 0) {
1851 filter
.label
= *argv
;
1852 } else if (strcmp(*argv
, "group") == 0) {
1854 if (rtnl_group_a2n(&filter
.group
, *argv
))
1855 invarg("Invalid \"group\" value\n", *argv
);
1856 } else if (strcmp(*argv
, "master") == 0) {
1860 ifindex
= ll_name_to_index(*argv
);
1862 invarg("Device does not exist\n", *argv
);
1863 filter
.master
= ifindex
;
1864 } else if (strcmp(*argv
, "vrf") == 0) {
1868 ifindex
= ll_name_to_index(*argv
);
1870 invarg("Not a valid VRF name\n", *argv
);
1871 if (!name_is_vrf(*argv
))
1872 invarg("Not a valid VRF name\n", *argv
);
1873 filter
.master
= ifindex
;
1874 } else if (strcmp(*argv
, "type") == 0) {
1878 soff
= strlen(*argv
) - strlen("_slave");
1879 if (!strcmp(*argv
+ soff
, "_slave")) {
1880 (*argv
)[soff
] = '\0';
1881 filter
.slave_kind
= *argv
;
1883 filter
.kind
= *argv
;
1886 if (strcmp(*argv
, "dev") == 0)
1888 else if (matches(*argv
, "help") == 0)
1891 duparg2("dev", *argv
);
1898 filter
.ifindex
= ll_name_to_index(filter_dev
);
1899 if (filter
.ifindex
<= 0) {
1900 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1905 if (action
== IPADD_FLUSH
)
1906 return ipaddr_flush();
1908 if (action
== IPADD_SAVE
) {
1909 if (ipadd_save_prep())
1912 if (rtnl_addrdump_req(&rth
, preferred_family
) < 0) {
1913 perror("Cannot send dump request");
1917 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1918 fprintf(stderr
, "Save terminated\n");
1926 * Initialize a json_writer and open an array object
1927 * if -json was specified.
1932 * If only filter_dev present and none of the other
1933 * link filters are present, use RTM_GETLINK to get
1936 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1937 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1938 perror("Cannot send link get request");
1946 if (filter
.family
!= AF_PACKET
) {
1953 if (ip_linkaddr_list(filter
.family
, iplink_filter_req
,
1954 &linfo
, ainfo
) != 0)
1957 if (filter
.family
!= AF_PACKET
)
1958 ipaddr_filter(&linfo
, ainfo
);
1960 for (l
= linfo
.head
; l
; l
= l
->next
) {
1961 struct nlmsghdr
*n
= &l
->h
;
1962 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
1965 open_json_object(NULL
);
1966 if (brief
|| !no_link
)
1967 res
= print_linkinfo(n
, stdout
);
1968 if (res
>= 0 && filter
.family
!= AF_PACKET
)
1969 print_selected_addrinfo(ifi
, ainfo
->head
, stdout
);
1970 if (res
> 0 && !do_link
&& show_stats
)
1971 print_link_stats(stdout
, n
);
1972 close_json_object();
1978 free_nlmsg_chain(ainfo
);
1979 free_nlmsg_chain(&linfo
);
1985 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1987 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1988 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1989 struct ifla_vf_rate
*vf_rate
;
1992 rem
= RTA_PAYLOAD(vflist
);
1994 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1995 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1997 if (!vf
[IFLA_VF_RATE
]) {
1998 fprintf(stderr
, "VF min/max rate API not supported\n");
2002 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
2003 if (vf_rate
->vf
== vfnum
) {
2004 *min
= vf_rate
->min_tx_rate
;
2005 *max
= vf_rate
->max_tx_rate
;
2009 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
2013 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, const char *dev
)
2015 struct nlmsg_chain linfo
= { NULL
, NULL
};
2016 struct rtattr
*tb
[IFLA_MAX
+1];
2017 struct ifinfomsg
*ifi
;
2018 struct nlmsg_list
*l
;
2022 idx
= ll_name_to_index(dev
);
2024 fprintf(stderr
, "Device %s does not exist\n", dev
);
2028 if (rtnl_linkdump_req(&rth
, AF_UNSPEC
) < 0) {
2029 perror("Cannot send dump request");
2032 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
2033 fprintf(stderr
, "Dump terminated\n");
2036 for (l
= linfo
.head
; l
; l
= l
->next
) {
2038 ifi
= NLMSG_DATA(n
);
2040 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
2041 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
2044 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
2046 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
2047 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
2053 int ipaddr_list_link(int argc
, char **argv
)
2055 preferred_family
= AF_PACKET
;
2057 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
2060 void ipaddr_reset_filter(int oneline
, int ifindex
)
2062 memset(&filter
, 0, sizeof(filter
));
2063 filter
.oneline
= oneline
;
2064 filter
.ifindex
= ifindex
;
2067 static int default_scope(inet_prefix
*lcl
)
2069 if (lcl
->family
== AF_INET
) {
2070 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
2071 return RT_SCOPE_HOST
;
2076 static bool ipaddr_is_multicast(inet_prefix
*a
)
2078 if (a
->family
== AF_INET
)
2079 return IN_MULTICAST(ntohl(a
->data
[0]));
2080 else if (a
->family
== AF_INET6
)
2081 return IN6_IS_ADDR_MULTICAST(a
->data
);
2086 static bool is_valid_label(const char *dev
, const char *label
)
2088 size_t len
= strlen(dev
);
2090 if (strncmp(label
, dev
, len
) != 0)
2093 return label
[len
] == '\0' || label
[len
] == ':';
2096 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
2100 struct ifaddrmsg ifa
;
2103 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
2104 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
2105 .n
.nlmsg_type
= cmd
,
2106 .ifa
.ifa_family
= preferred_family
,
2110 char *lcl_arg
= NULL
;
2111 char *valid_lftp
= NULL
;
2112 char *preferred_lftp
= NULL
;
2113 inet_prefix lcl
= {};
2120 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
2121 __u32 valid_lft
= INFINITY_LIFE_TIME
;
2122 unsigned int ifa_flags
= 0;
2125 if (strcmp(*argv
, "peer") == 0 ||
2126 strcmp(*argv
, "remote") == 0) {
2130 duparg("peer", *argv
);
2131 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
2132 peer_len
= peer
.bytelen
;
2133 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2134 req
.ifa
.ifa_family
= peer
.family
;
2135 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
2136 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
2137 } else if (matches(*argv
, "broadcast") == 0 ||
2138 strcmp(*argv
, "brd") == 0) {
2143 duparg("broadcast", *argv
);
2144 if (strcmp(*argv
, "+") == 0)
2146 else if (strcmp(*argv
, "-") == 0)
2149 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2150 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2151 req
.ifa
.ifa_family
= addr
.family
;
2152 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
2153 brd_len
= addr
.bytelen
;
2155 } else if (strcmp(*argv
, "anycast") == 0) {
2160 duparg("anycast", *argv
);
2161 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2162 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2163 req
.ifa
.ifa_family
= addr
.family
;
2164 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
2165 any_len
= addr
.bytelen
;
2166 } else if (strcmp(*argv
, "scope") == 0) {
2167 unsigned int scope
= 0;
2170 if (rtnl_rtscope_a2n(&scope
, *argv
))
2171 invarg("invalid scope value.", *argv
);
2172 req
.ifa
.ifa_scope
= scope
;
2174 } else if (strcmp(*argv
, "dev") == 0) {
2177 } else if (strcmp(*argv
, "label") == 0) {
2180 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
2181 } else if (matches(*argv
, "metric") == 0 ||
2182 matches(*argv
, "priority") == 0 ||
2183 matches(*argv
, "preference") == 0) {
2187 if (get_u32(&metric
, *argv
, 0))
2188 invarg("\"metric\" value is invalid\n", *argv
);
2189 addattr32(&req
.n
, sizeof(req
), IFA_RT_PRIORITY
, metric
);
2190 } else if (matches(*argv
, "valid_lft") == 0) {
2192 duparg("valid_lft", *argv
);
2195 if (set_lifetime(&valid_lft
, *argv
))
2196 invarg("valid_lft value", *argv
);
2197 } else if (matches(*argv
, "preferred_lft") == 0) {
2199 duparg("preferred_lft", *argv
);
2201 preferred_lftp
= *argv
;
2202 if (set_lifetime(&preferred_lft
, *argv
))
2203 invarg("preferred_lft value", *argv
);
2204 } else if (strcmp(*argv
, "home") == 0) {
2205 ifa_flags
|= IFA_F_HOMEADDRESS
;
2206 } else if (strcmp(*argv
, "nodad") == 0) {
2207 ifa_flags
|= IFA_F_NODAD
;
2208 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
2209 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
2210 } else if (strcmp(*argv
, "noprefixroute") == 0) {
2211 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
2212 } else if (strcmp(*argv
, "autojoin") == 0) {
2213 ifa_flags
|= IFA_F_MCAUTOJOIN
;
2215 if (strcmp(*argv
, "local") == 0)
2217 if (matches(*argv
, "help") == 0)
2220 duparg2("local", *argv
);
2222 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
2223 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2224 req
.ifa
.ifa_family
= lcl
.family
;
2225 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
2226 local_len
= lcl
.bytelen
;
2230 if (ifa_flags
<= 0xff)
2231 req
.ifa
.ifa_flags
= ifa_flags
;
2233 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
2236 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
2239 if (l
&& !is_valid_label(d
, l
)) {
2241 "\"label\" (%s) must match \"dev\" (%s) or be prefixed by \"dev\" with a colon.\n",
2246 if (peer_len
== 0 && local_len
) {
2247 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
2249 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
2250 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
2251 " This special behaviour is likely to disappear in further releases,\n"
2252 " fix your scripts!\n", lcl_arg
, local_len
*8);
2255 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
2258 if (req
.ifa
.ifa_prefixlen
== 0)
2259 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
2261 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
2265 if (req
.ifa
.ifa_family
!= AF_INET
) {
2266 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
2270 if (brd
.bitlen
<= 30) {
2271 for (i
= 31; i
>= brd
.bitlen
; i
--) {
2273 brd
.data
[0] |= htonl(1<<(31-i
));
2275 brd
.data
[0] &= ~htonl(1<<(31-i
));
2277 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
2278 brd_len
= brd
.bytelen
;
2281 if (!scoped
&& cmd
!= RTM_DELADDR
)
2282 req
.ifa
.ifa_scope
= default_scope(&lcl
);
2284 req
.ifa
.ifa_index
= ll_name_to_index(d
);
2285 if (!req
.ifa
.ifa_index
)
2288 if (valid_lftp
|| preferred_lftp
) {
2289 struct ifa_cacheinfo cinfo
= {};
2292 fprintf(stderr
, "valid_lft is zero\n");
2295 if (valid_lft
< preferred_lft
) {
2296 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
2300 cinfo
.ifa_prefered
= preferred_lft
;
2301 cinfo
.ifa_valid
= valid_lft
;
2302 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
2306 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
2307 fprintf(stderr
, "autojoin needs multicast address\n");
2311 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
2317 int do_ipaddr(int argc
, char **argv
)
2320 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
2321 if (matches(*argv
, "add") == 0)
2322 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
2323 if (matches(*argv
, "change") == 0 ||
2324 strcmp(*argv
, "chg") == 0)
2325 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2326 if (matches(*argv
, "replace") == 0)
2327 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2328 if (matches(*argv
, "delete") == 0)
2329 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2330 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2331 || matches(*argv
, "lst") == 0)
2332 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2333 if (matches(*argv
, "flush") == 0)
2334 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2335 if (matches(*argv
, "save") == 0)
2336 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2337 if (matches(*argv
, "showdump") == 0)
2338 return ipaddr_showdump();
2339 if (matches(*argv
, "restore") == 0)
2340 return ipaddr_restore();
2341 if (matches(*argv
, "help") == 0)
2343 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);