2 * ipaddress.c "ip address".
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <sys/param.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_infiniband.h>
30 #include <linux/sockios.h>
31 #include <linux/net_namespace.h>
37 #include "ip_common.h"
46 static struct link_filter filter
;
49 static void usage(void) __attribute__((noreturn
));
51 static void usage(void)
57 "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n"
58 " [ CONFFLAG-LIST ]\n"
59 " ip address del IFADDR dev IFNAME [mngtmpaddr]\n"
60 " ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n"
61 " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n"
62 " ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n"
63 " [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n"
64 " [ label LABEL ] [up] [ vrf NAME ] ]\n"
65 " ip address {showdump|restore}\n"
66 "IFADDR := PREFIX | ADDR peer PREFIX\n"
67 " [ broadcast ADDR ] [ anycast ADDR ]\n"
68 " [ label IFNAME ] [ scope SCOPE-ID ] [ metric METRIC ]\n"
69 "SCOPE-ID := [ host | link | global | NUMBER ]\n"
70 "FLAG-LIST := [ FLAG-LIST ] FLAG\n"
71 "FLAG := [ permanent | dynamic | secondary | primary |\n"
72 " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n"
74 "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n"
75 "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n"
76 "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n"
77 "LFT := forever | SECONDS\n"
78 "TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |\n"
79 " bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |\n"
80 " gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti |\n"
81 " nlmon | can | bond_slave | ipvlan | geneve | bridge_slave |\n"
82 " hsr | macsec | netdevsim }\n");
87 static void print_link_flags(FILE *fp
, unsigned int flags
, unsigned int mdown
)
89 open_json_array(PRINT_ANY
, is_json_context() ? "flags" : "<");
90 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
91 print_string(PRINT_ANY
, NULL
,
92 flags
? "%s," : "%s", "NO-CARRIER");
93 flags
&= ~IFF_RUNNING
;
94 #define _PF(f) if (flags&IFF_##f) { \
96 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
117 print_hex(PRINT_ANY
, NULL
, "%x", flags
);
119 print_string(PRINT_ANY
, NULL
, ",%s", "M-DOWN");
120 close_json_array(PRINT_ANY
, "> ");
123 static const char *oper_states
[] = {
124 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
125 "TESTING", "DORMANT", "UP"
128 static void print_operstate(FILE *f
, __u8 state
)
130 if (state
>= ARRAY_SIZE(oper_states
)) {
131 if (is_json_context())
132 print_uint(PRINT_JSON
, "operstate_index", NULL
, state
);
134 print_0xhex(PRINT_FP
, NULL
, "state %#llx", state
);
136 print_color_string(PRINT_ANY
,
137 oper_state_color(state
),
142 if (is_json_context())
143 print_string(PRINT_JSON
,
145 NULL
, oper_states
[state
]);
147 fprintf(f
, "state ");
148 color_fprintf(f
, oper_state_color(state
),
149 "%s ", oper_states
[state
]);
154 int get_operstate(const char *name
)
158 for (i
= 0; i
< ARRAY_SIZE(oper_states
); i
++)
159 if (strcasecmp(name
, oper_states
[i
]) == 0)
164 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
169 qlen
= rta_getattr_u32(tb
[IFLA_TXQLEN
]);
171 struct ifreq ifr
= {};
172 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
177 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
178 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
180 "ioctl(SIOCGIFTXQLEN) failed: %s\n",
189 print_int(PRINT_ANY
, "txqlen", "qlen %d", qlen
);
192 static const char *link_modes
[] = {
196 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
198 unsigned int mode
= rta_getattr_u8(tb
);
200 if (mode
>= ARRAY_SIZE(link_modes
))
206 print_string(PRINT_ANY
,
212 static char *parse_link_kind(struct rtattr
*tb
, bool slave
)
214 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
215 int attr
= slave
? IFLA_INFO_SLAVE_KIND
: IFLA_INFO_KIND
;
217 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
220 return RTA_DATA(linkinfo
[attr
]);
225 static int match_link_kind(struct rtattr
**tb
, const char *kind
, bool slave
)
227 if (!tb
[IFLA_LINKINFO
])
230 return strcmp(parse_link_kind(tb
[IFLA_LINKINFO
], slave
), kind
);
233 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
235 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
236 struct link_util
*lu
;
237 struct link_util
*slave_lu
;
240 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
241 open_json_object("linkinfo");
243 if (linkinfo
[IFLA_INFO_KIND
]) {
245 = rta_getattr_str(linkinfo
[IFLA_INFO_KIND
]);
248 print_string(PRINT_ANY
, "info_kind", " %s ", kind
);
250 lu
= get_link_kind(kind
);
251 if (lu
&& lu
->print_opt
) {
252 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
254 if (linkinfo
[IFLA_INFO_DATA
]) {
255 parse_rtattr_nested(attr
, lu
->maxattr
,
256 linkinfo
[IFLA_INFO_DATA
]);
259 open_json_object("info_data");
260 lu
->print_opt(lu
, fp
, data
);
263 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
265 open_json_object("info_xstats");
266 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
272 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
273 const char *slave_kind
274 = rta_getattr_str(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
277 print_string(PRINT_ANY
,
282 snprintf(slave
, sizeof(slave
), "%s_slave", slave_kind
);
284 slave_lu
= get_link_kind(slave
);
285 if (slave_lu
&& slave_lu
->print_opt
) {
286 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
288 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
289 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
290 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
293 open_json_object("info_slave_data");
294 slave_lu
->print_opt(slave_lu
, fp
, data
);
301 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
303 struct rtattr
*inet6_attr
;
304 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
306 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
310 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
312 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
313 __u8 mode
= rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
317 case IN6_ADDR_GEN_MODE_EUI64
:
318 print_string(PRINT_ANY
,
319 "inet6_addr_gen_mode",
323 case IN6_ADDR_GEN_MODE_NONE
:
324 print_string(PRINT_ANY
,
325 "inet6_addr_gen_mode",
329 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
330 print_string(PRINT_ANY
,
331 "inet6_addr_gen_mode",
335 case IN6_ADDR_GEN_MODE_RANDOM
:
336 print_string(PRINT_ANY
,
337 "inet6_addr_gen_mode",
342 snprintf(b1
, sizeof(b1
), "%#.2hhx", mode
);
343 print_string(PRINT_ANY
,
344 "inet6_addr_gen_mode",
352 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
354 static void print_vfinfo(FILE *fp
, struct ifinfomsg
*ifi
, struct rtattr
*vfinfo
)
356 struct ifla_vf_mac
*vf_mac
;
357 struct ifla_vf_broadcast
*vf_broadcast
;
358 struct ifla_vf_tx_rate
*vf_tx_rate
;
359 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
363 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
364 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
368 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
370 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
371 vf_broadcast
= RTA_DATA(vf
[IFLA_VF_BROADCAST
]);
372 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
374 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
375 print_int(PRINT_ANY
, "vf", "vf %d ", vf_mac
->vf
);
377 print_string(PRINT_ANY
,
380 ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
382 print_color_string(PRINT_ANY
, COLOR_MAC
,
384 ll_addr_n2a((unsigned char *) &vf_mac
->mac
,
385 ifi
->ifi_type
== ARPHRD_ETHER
?
386 ETH_ALEN
: INFINIBAND_ALEN
,
390 if (vf
[IFLA_VF_BROADCAST
]) {
391 if (ifi
->ifi_flags
&IFF_POINTOPOINT
) {
392 print_string(PRINT_FP
, NULL
, " peer ", NULL
);
393 print_bool(PRINT_JSON
,
394 "link_pointtopoint", NULL
, true);
396 print_string(PRINT_FP
, NULL
, " brd ", NULL
);
398 print_color_string(PRINT_ANY
, COLOR_MAC
,
400 ll_addr_n2a((unsigned char *) &vf_broadcast
->broadcast
,
401 ifi
->ifi_type
== ARPHRD_ETHER
?
402 ETH_ALEN
: INFINIBAND_ALEN
,
407 if (vf
[IFLA_VF_VLAN_LIST
]) {
408 struct rtattr
*i
, *vfvlanlist
= vf
[IFLA_VF_VLAN_LIST
];
409 int rem
= RTA_PAYLOAD(vfvlanlist
);
411 open_json_array(PRINT_JSON
, "vlan_list");
412 for (i
= RTA_DATA(vfvlanlist
);
413 RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
414 struct ifla_vf_vlan_info
*vf_vlan_info
= RTA_DATA(i
);
417 open_json_object(NULL
);
418 if (vf_vlan_info
->vlan
)
423 if (vf_vlan_info
->qos
)
428 if (vf_vlan_info
->vlan_proto
&&
429 vf_vlan_info
->vlan_proto
!= htons(ETH_P_8021Q
))
430 print_string(PRINT_ANY
,
432 ", vlan protocol %s",
434 vf_vlan_info
->vlan_proto
,
438 close_json_array(PRINT_JSON
, NULL
);
440 struct ifla_vf_vlan
*vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
448 print_int(PRINT_ANY
, "qos", ", qos %d", vf_vlan
->qos
);
451 if (vf_tx_rate
->rate
)
452 print_uint(PRINT_ANY
,
454 ", tx rate %u (Mbps)",
457 if (vf
[IFLA_VF_RATE
]) {
458 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
459 int max_tx
= vf_rate
->max_tx_rate
;
460 int min_tx
= vf_rate
->min_tx_rate
;
462 if (is_json_context()) {
463 open_json_object("rate");
464 print_uint(PRINT_JSON
, "max_tx", NULL
, max_tx
);
465 print_uint(PRINT_ANY
, "min_tx", NULL
, min_tx
);
469 fprintf(fp
, ", max_tx_rate %uMbps", max_tx
);
471 fprintf(fp
, ", min_tx_rate %uMbps", min_tx
);
475 if (vf
[IFLA_VF_SPOOFCHK
]) {
476 struct ifla_vf_spoofchk
*vf_spoofchk
=
477 RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
479 if (vf_spoofchk
->setting
!= -1)
480 print_bool(PRINT_ANY
,
482 vf_spoofchk
->setting
?
483 ", spoof checking on" : ", spoof checking off",
484 vf_spoofchk
->setting
);
487 if (vf
[IFLA_VF_LINK_STATE
]) {
488 struct ifla_vf_link_state
*vf_linkstate
=
489 RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
491 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
492 print_string(PRINT_ANY
,
496 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
497 print_string(PRINT_ANY
,
502 print_string(PRINT_ANY
,
508 if (vf
[IFLA_VF_TRUST
]) {
509 struct ifla_vf_trust
*vf_trust
= RTA_DATA(vf
[IFLA_VF_TRUST
]);
511 if (vf_trust
->setting
!= -1)
512 print_bool(PRINT_ANY
,
514 vf_trust
->setting
? ", trust on" : ", trust off",
518 if (vf
[IFLA_VF_RSS_QUERY_EN
]) {
519 struct ifla_vf_rss_query_en
*rss_query
=
520 RTA_DATA(vf
[IFLA_VF_RSS_QUERY_EN
]);
522 if (rss_query
->setting
!= -1)
523 print_bool(PRINT_ANY
,
525 rss_query
->setting
? ", query_rss on"
530 if (vf
[IFLA_VF_STATS
] && show_stats
)
531 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
534 void print_num(FILE *fp
, unsigned int width
, uint64_t count
)
536 const char *prefix
= "kMGTPE";
537 const unsigned int base
= use_iec
? 1024 : 1000;
540 uint8_t precision
= 2;
543 if (!human_readable
|| count
< base
) {
544 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
548 /* increase value by a factor of 1000/1024 and print
549 * if result is something a human can read
553 if (count
/ base
< powi
)
561 /* try to guess a good number of digits for precision */
562 for (; precision
> 0; precision
--) {
564 if (count
/ powi
< powj
)
568 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
569 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
571 fprintf(fp
, "%-*s ", width
, buf
);
574 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
576 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1];
578 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
579 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
583 parse_rtattr_nested(vf
, IFLA_VF_STATS_MAX
, vfstats
);
585 if (is_json_context()) {
586 open_json_object("stats");
589 open_json_object("rx");
590 print_u64(PRINT_JSON
, "bytes", NULL
,
591 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
592 print_u64(PRINT_JSON
, "packets", NULL
,
593 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
594 print_u64(PRINT_JSON
, "multicast", NULL
,
595 rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
596 print_u64(PRINT_JSON
, "broadcast", NULL
,
597 rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
598 if (vf
[IFLA_VF_STATS_RX_DROPPED
])
599 print_u64(PRINT_JSON
, "dropped", NULL
,
600 rta_getattr_u64(vf
[IFLA_VF_STATS_RX_DROPPED
]));
604 open_json_object("tx");
605 print_u64(PRINT_JSON
, "tx_bytes", NULL
,
606 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
607 print_u64(PRINT_JSON
, "tx_packets", NULL
,
608 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
609 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
610 print_u64(PRINT_JSON
, "dropped", NULL
,
611 rta_getattr_u64(vf
[IFLA_VF_STATS_TX_DROPPED
]));
616 fprintf(fp
, "%s", _SL_
);
617 fprintf(fp
, " RX: bytes packets mcast bcast ");
618 if (vf
[IFLA_VF_STATS_RX_DROPPED
])
619 fprintf(fp
, " dropped ");
620 fprintf(fp
, "%s", _SL_
);
623 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_BYTES
]));
624 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_PACKETS
]));
625 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_MULTICAST
]));
626 print_num(fp
, 7, rta_getattr_u64(vf
[IFLA_VF_STATS_BROADCAST
]));
627 if (vf
[IFLA_VF_STATS_RX_DROPPED
])
628 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_RX_DROPPED
]));
631 fprintf(fp
, "%s", _SL_
);
632 fprintf(fp
, " TX: bytes packets ");
633 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
634 fprintf(fp
, " dropped ");
635 fprintf(fp
, "%s", _SL_
);
638 print_num(fp
, 10, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_BYTES
]));
639 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_PACKETS
]));
640 if (vf
[IFLA_VF_STATS_TX_DROPPED
])
641 print_num(fp
, 8, rta_getattr_u64(vf
[IFLA_VF_STATS_TX_DROPPED
]));
645 static void __print_link_stats(FILE *fp
, struct rtattr
*tb
[])
647 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
648 struct rtnl_link_stats64 _s
, *s
= &_s
;
651 ret
= get_rtnl_link_stats_rta(s
, tb
);
655 if (is_json_context()) {
656 open_json_object((ret
== sizeof(*s
)) ? "stats64" : "stats");
659 open_json_object("rx");
660 print_u64(PRINT_JSON
, "bytes", NULL
, s
->rx_bytes
);
661 print_u64(PRINT_JSON
, "packets", NULL
, s
->rx_packets
);
662 print_u64(PRINT_JSON
, "errors", NULL
, s
->rx_errors
);
663 print_u64(PRINT_JSON
, "dropped", NULL
, s
->rx_dropped
);
664 print_u64(PRINT_JSON
, "over_errors", NULL
, s
->rx_over_errors
);
665 print_u64(PRINT_JSON
, "multicast", NULL
, s
->multicast
);
666 if (s
->rx_compressed
)
667 print_u64(PRINT_JSON
,
668 "compressed", NULL
, s
->rx_compressed
);
671 if (show_stats
> 1) {
672 print_u64(PRINT_JSON
,
674 NULL
, s
->rx_length_errors
);
675 print_u64(PRINT_JSON
,
677 NULL
, s
->rx_crc_errors
);
678 print_u64(PRINT_JSON
,
680 NULL
, s
->rx_frame_errors
);
681 print_u64(PRINT_JSON
,
683 NULL
, s
->rx_fifo_errors
);
684 print_u64(PRINT_JSON
,
686 NULL
, s
->rx_missed_errors
);
688 print_u64(PRINT_JSON
,
689 "nohandler", NULL
, s
->rx_nohandler
);
694 open_json_object("tx");
695 print_u64(PRINT_JSON
, "bytes", NULL
, s
->tx_bytes
);
696 print_u64(PRINT_JSON
, "packets", NULL
, s
->tx_packets
);
697 print_u64(PRINT_JSON
, "errors", NULL
, s
->tx_errors
);
698 print_u64(PRINT_JSON
, "dropped", NULL
, s
->tx_dropped
);
699 print_u64(PRINT_JSON
,
701 NULL
, s
->tx_carrier_errors
);
702 print_u64(PRINT_JSON
, "collisions", NULL
, s
->collisions
);
703 if (s
->tx_compressed
)
704 print_u64(PRINT_JSON
,
705 "compressed", NULL
, s
->tx_compressed
);
708 if (show_stats
> 1) {
709 print_u64(PRINT_JSON
,
711 NULL
, s
->tx_aborted_errors
);
712 print_u64(PRINT_JSON
,
714 NULL
, s
->tx_fifo_errors
);
715 print_u64(PRINT_JSON
,
717 NULL
, s
->tx_window_errors
);
718 print_u64(PRINT_JSON
,
720 NULL
, s
->tx_heartbeat_errors
);
722 print_u64(PRINT_JSON
, "carrier_changes", NULL
,
723 rta_getattr_u32(carrier_changes
));
730 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
731 s
->rx_compressed
? "compressed" : "", _SL_
);
734 print_num(fp
, 10, s
->rx_bytes
);
735 print_num(fp
, 8, s
->rx_packets
);
736 print_num(fp
, 7, s
->rx_errors
);
737 print_num(fp
, 7, s
->rx_dropped
);
738 print_num(fp
, 7, s
->rx_over_errors
);
739 print_num(fp
, 7, s
->multicast
);
740 if (s
->rx_compressed
)
741 print_num(fp
, 7, s
->rx_compressed
);
744 if (show_stats
> 1) {
745 fprintf(fp
, "%s", _SL_
);
746 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
747 s
->rx_nohandler
? " nohandler" : "", _SL_
);
749 print_num(fp
, 8, s
->rx_length_errors
);
750 print_num(fp
, 7, s
->rx_crc_errors
);
751 print_num(fp
, 7, s
->rx_frame_errors
);
752 print_num(fp
, 7, s
->rx_fifo_errors
);
753 print_num(fp
, 7, s
->rx_missed_errors
);
755 print_num(fp
, 7, s
->rx_nohandler
);
757 fprintf(fp
, "%s", _SL_
);
760 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
761 s
->tx_compressed
? "compressed" : "", _SL_
);
764 print_num(fp
, 10, s
->tx_bytes
);
765 print_num(fp
, 8, s
->tx_packets
);
766 print_num(fp
, 7, s
->tx_errors
);
767 print_num(fp
, 7, s
->tx_dropped
);
768 print_num(fp
, 7, s
->tx_carrier_errors
);
769 print_num(fp
, 7, s
->collisions
);
770 if (s
->tx_compressed
)
771 print_num(fp
, 7, s
->tx_compressed
);
774 if (show_stats
> 1) {
775 fprintf(fp
, "%s", _SL_
);
776 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
778 fprintf(fp
, " transns");
779 fprintf(fp
, "%s", _SL_
);
782 print_num(fp
, 8, s
->tx_aborted_errors
);
783 print_num(fp
, 7, s
->tx_fifo_errors
);
784 print_num(fp
, 7, s
->tx_window_errors
);
785 print_num(fp
, 7, s
->tx_heartbeat_errors
);
788 rta_getattr_u32(carrier_changes
));
793 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
795 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
796 struct rtattr
*tb
[IFLA_MAX
+1];
798 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
799 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
800 __print_link_stats(fp
, tb
);
804 static int print_linkinfo_brief(FILE *fp
, const char *name
,
805 const struct ifinfomsg
*ifi
,
808 unsigned int m_flag
= 0;
810 m_flag
= print_name_and_link("%-16s ", name
, tb
);
812 if (tb
[IFLA_OPERSTATE
])
813 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
815 if (filter
.family
== AF_PACKET
) {
818 if (tb
[IFLA_ADDRESS
]) {
819 print_color_string(PRINT_ANY
, COLOR_MAC
,
822 RTA_DATA(tb
[IFLA_ADDRESS
]),
823 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
829 if (filter
.family
== AF_PACKET
) {
830 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
831 print_string(PRINT_FP
, NULL
, "%s", "\n");
838 static const char *link_events
[] = {
839 [IFLA_EVENT_NONE
] = "NONE",
840 [IFLA_EVENT_REBOOT
] = "REBOOT",
841 [IFLA_EVENT_FEATURES
] = "FEATURE CHANGE",
842 [IFLA_EVENT_BONDING_FAILOVER
] = "BONDING FAILOVER",
843 [IFLA_EVENT_NOTIFY_PEERS
] = "NOTIFY PEERS",
844 [IFLA_EVENT_IGMP_RESEND
] = "RESEND IGMP",
845 [IFLA_EVENT_BONDING_OPTIONS
] = "BONDING OPTION"
848 static void print_link_event(FILE *f
, __u32 event
)
850 if (event
>= ARRAY_SIZE(link_events
))
851 print_int(PRINT_ANY
, "event", "event %d ", event
);
854 print_string(PRINT_ANY
,
855 "event", "event %s ",
860 int print_linkinfo(struct nlmsghdr
*n
, void *arg
)
862 FILE *fp
= (FILE *)arg
;
863 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
864 struct rtattr
*tb
[IFLA_MAX
+1];
865 int len
= n
->nlmsg_len
;
867 unsigned int m_flag
= 0;
870 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
873 len
-= NLMSG_LENGTH(sizeof(*ifi
));
877 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
879 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
882 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
884 name
= get_ifname_rta(ifi
->ifi_index
, tb
[IFLA_IFNAME
]);
891 if (tb
[IFLA_GROUP
]) {
892 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
894 if (filter
.group
!= -1 && group
!= filter
.group
)
898 if (tb
[IFLA_MASTER
]) {
899 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
901 if (filter
.master
> 0 && master
!= filter
.master
)
903 } else if (filter
.master
> 0)
906 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
909 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
912 if (n
->nlmsg_type
== RTM_DELLINK
)
913 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
916 return print_linkinfo_brief(fp
, name
, ifi
, tb
);
918 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
920 m_flag
= print_name_and_link("%s: ", name
, tb
);
921 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
926 rta_getattr_u32(tb
[IFLA_MTU
]));
928 xdp_dump(fp
, tb
[IFLA_XDP
], do_link
, false);
930 print_string(PRINT_ANY
,
933 rta_getattr_str(tb
[IFLA_QDISC
]));
934 if (tb
[IFLA_MASTER
]) {
935 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
937 print_string(PRINT_ANY
,
938 "master", "master %s ",
939 ll_index_to_name(master
));
942 if (tb
[IFLA_OPERSTATE
])
943 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
945 if (do_link
&& tb
[IFLA_LINKMODE
])
946 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
948 if (tb
[IFLA_GROUP
]) {
949 int group
= rta_getattr_u32(tb
[IFLA_GROUP
]);
951 print_string(PRINT_ANY
,
954 rtnl_group_n2a(group
, b1
, sizeof(b1
)));
957 if (filter
.showqueue
)
958 print_queuelen(fp
, tb
);
961 print_link_event(fp
, rta_getattr_u32(tb
[IFLA_EVENT
]));
963 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
965 print_string(PRINT_ANY
,
968 ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
969 if (tb
[IFLA_ADDRESS
]) {
970 print_color_string(PRINT_ANY
,
974 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
975 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
979 if (tb
[IFLA_BROADCAST
]) {
980 if (ifi
->ifi_flags
&IFF_POINTOPOINT
) {
981 print_string(PRINT_FP
, NULL
, " peer ", NULL
);
982 print_bool(PRINT_JSON
,
983 "link_pointtopoint", NULL
, true);
985 print_string(PRINT_FP
, NULL
, " brd ", NULL
);
987 print_color_string(PRINT_ANY
,
991 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
992 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
998 if (tb
[IFLA_LINK_NETNSID
]) {
999 int id
= rta_getattr_u32(tb
[IFLA_LINK_NETNSID
]);
1001 if (is_json_context()) {
1002 print_int(PRINT_JSON
, "link_netnsid", NULL
, id
);
1005 char *name
= get_name_from_nsid(id
);
1008 print_string(PRINT_FP
, NULL
,
1009 " link-netns %s", name
);
1011 print_int(PRINT_FP
, NULL
,
1012 " link-netnsid %d", id
);
1014 print_string(PRINT_FP
, NULL
,
1015 " link-netnsid %s", "unknown");
1019 if (tb
[IFLA_NEW_NETNSID
]) {
1020 int id
= rta_getattr_u32(tb
[IFLA_NEW_NETNSID
]);
1021 char *name
= get_name_from_nsid(id
);
1024 print_string(PRINT_FP
, NULL
, " new-netns %s", name
);
1026 print_int(PRINT_FP
, NULL
, " new-netnsid %d", id
);
1028 if (tb
[IFLA_NEW_IFINDEX
]) {
1029 int id
= rta_getattr_u32(tb
[IFLA_NEW_IFINDEX
]);
1031 print_int(PRINT_FP
, NULL
, " new-ifindex %d", id
);
1034 if (tb
[IFLA_PROTO_DOWN
]) {
1035 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
1036 print_bool(PRINT_ANY
,
1037 "proto_down", " protodown on ", true);
1041 if (tb
[IFLA_PROMISCUITY
])
1042 print_uint(PRINT_ANY
,
1045 rta_getattr_u32(tb
[IFLA_PROMISCUITY
]));
1047 if (tb
[IFLA_MIN_MTU
])
1048 print_uint(PRINT_ANY
,
1049 "min_mtu", "minmtu %u ",
1050 rta_getattr_u32(tb
[IFLA_MIN_MTU
]));
1052 if (tb
[IFLA_MAX_MTU
])
1053 print_uint(PRINT_ANY
,
1054 "max_mtu", "maxmtu %u ",
1055 rta_getattr_u32(tb
[IFLA_MAX_MTU
]));
1057 if (tb
[IFLA_LINKINFO
])
1058 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
1060 if (do_link
&& tb
[IFLA_AF_SPEC
])
1061 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
1063 if (tb
[IFLA_NUM_TX_QUEUES
])
1064 print_uint(PRINT_ANY
,
1067 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
1069 if (tb
[IFLA_NUM_RX_QUEUES
])
1070 print_uint(PRINT_ANY
,
1073 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
1075 if (tb
[IFLA_GSO_MAX_SIZE
])
1076 print_uint(PRINT_ANY
,
1079 rta_getattr_u32(tb
[IFLA_GSO_MAX_SIZE
]));
1081 if (tb
[IFLA_GSO_MAX_SEGS
])
1082 print_uint(PRINT_ANY
,
1085 rta_getattr_u32(tb
[IFLA_GSO_MAX_SEGS
]));
1087 if (tb
[IFLA_PHYS_PORT_NAME
])
1088 print_string(PRINT_ANY
,
1091 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
1093 if (tb
[IFLA_PHYS_PORT_ID
]) {
1094 print_string(PRINT_ANY
,
1098 RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
1099 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
1103 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
1104 print_string(PRINT_ANY
,
1107 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
1108 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
1113 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
1114 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
1115 print_string(PRINT_ANY
,
1118 rta_getattr_str(tb
[IFLA_IFALIAS
]));
1121 if ((do_link
|| show_details
) && tb
[IFLA_XDP
])
1122 xdp_dump(fp
, tb
[IFLA_XDP
], true, true);
1124 if (do_link
&& show_stats
) {
1126 __print_link_stats(fp
, tb
);
1129 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
1130 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
1131 int rem
= RTA_PAYLOAD(vflist
);
1133 open_json_array(PRINT_JSON
, "vfinfo_list");
1134 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1135 open_json_object(NULL
);
1136 print_vfinfo(fp
, ifi
, i
);
1137 close_json_object();
1139 close_json_array(PRINT_JSON
, NULL
);
1142 print_string(PRINT_FP
, NULL
, "%s", "\n");
1147 static int flush_update(void)
1151 * Note that the kernel may delete multiple addresses for one
1152 * delete request (e.g. if ipv4 address promotion is disabled).
1153 * Since a flush operation is really a series of delete requests
1154 * its possible that we may request an address delete that has
1155 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
1156 * errors returned from a flush request
1158 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
1159 (errno
!= EADDRNOTAVAIL
)) {
1160 perror("Failed to send flush request");
1167 static int set_lifetime(unsigned int *lifetime
, char *argv
)
1169 if (strcmp(argv
, "forever") == 0)
1170 *lifetime
= INFINITY_LIFE_TIME
;
1171 else if (get_u32(lifetime
, argv
, 0))
1177 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
1178 struct rtattr
*ifa_flags_attr
)
1180 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
1184 /* Mapping from argument to address flag mask */
1185 static const struct {
1187 unsigned long value
;
1188 } ifa_flag_names
[] = {
1189 { "secondary", IFA_F_SECONDARY
},
1190 { "temporary", IFA_F_SECONDARY
},
1191 { "nodad", IFA_F_NODAD
},
1192 { "optimistic", IFA_F_OPTIMISTIC
},
1193 { "dadfailed", IFA_F_DADFAILED
},
1194 { "home", IFA_F_HOMEADDRESS
},
1195 { "deprecated", IFA_F_DEPRECATED
},
1196 { "tentative", IFA_F_TENTATIVE
},
1197 { "permanent", IFA_F_PERMANENT
},
1198 { "mngtmpaddr", IFA_F_MANAGETEMPADDR
},
1199 { "noprefixroute", IFA_F_NOPREFIXROUTE
},
1200 { "autojoin", IFA_F_MCAUTOJOIN
},
1201 { "stable-privacy", IFA_F_STABLE_PRIVACY
},
1204 static void print_ifa_flags(FILE *fp
, const struct ifaddrmsg
*ifa
,
1209 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1210 unsigned long mask
= ifa_flag_names
[i
].value
;
1212 if (mask
== IFA_F_PERMANENT
) {
1213 if (!(flags
& mask
))
1214 print_bool(PRINT_ANY
,
1215 "dynamic", "dynamic ", true);
1216 } else if (flags
& mask
) {
1217 if (mask
== IFA_F_SECONDARY
&&
1218 ifa
->ifa_family
== AF_INET6
) {
1219 print_bool(PRINT_ANY
,
1220 "temporary", "temporary ", true);
1222 print_string(PRINT_FP
, NULL
,
1223 "%s ", ifa_flag_names
[i
].name
);
1224 print_bool(PRINT_JSON
,
1225 ifa_flag_names
[i
].name
, NULL
, true);
1233 if (is_json_context()) {
1236 snprintf(b1
, sizeof(b1
), "%02x", flags
);
1237 print_string(PRINT_JSON
, "ifa_flags", NULL
, b1
);
1239 fprintf(fp
, "flags %02x ", flags
);
1245 static int get_filter(const char *arg
)
1250 if (arg
[0] == '-') {
1256 if (strcmp(arg
, "dynamic") == 0) {
1259 } else if (strcmp(arg
, "primary") == 0) {
1264 for (i
= 0; i
< ARRAY_SIZE(ifa_flag_names
); i
++) {
1265 if (strcmp(arg
, ifa_flag_names
[i
].name
))
1269 filter
.flags
&= ~ifa_flag_names
[i
].value
;
1271 filter
.flags
|= ifa_flag_names
[i
].value
;
1272 filter
.flagmask
|= ifa_flag_names
[i
].value
;
1278 static int ifa_label_match_rta(int ifindex
, const struct rtattr
*rta
)
1286 label
= RTA_DATA(rta
);
1288 label
= ll_index_to_name(ifindex
);
1290 return fnmatch(filter
.label
, label
, 0);
1293 int print_addrinfo(struct nlmsghdr
*n
, void *arg
)
1296 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1297 int len
= n
->nlmsg_len
;
1298 unsigned int ifa_flags
;
1299 struct rtattr
*rta_tb
[IFA_MAX
+1];
1303 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
1305 len
-= NLMSG_LENGTH(sizeof(*ifa
));
1307 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1311 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
1314 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
1315 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1317 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1319 if (!rta_tb
[IFA_LOCAL
])
1320 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1321 if (!rta_tb
[IFA_ADDRESS
])
1322 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1324 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1326 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1328 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1331 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1334 if (ifa_label_match_rta(ifa
->ifa_index
, rta_tb
[IFA_LABEL
]))
1337 if (inet_addr_match_rta(&filter
.pfx
, rta_tb
[IFA_LOCAL
]))
1340 if (filter
.flushb
) {
1341 struct nlmsghdr
*fn
;
1343 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1347 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1348 memcpy(fn
, n
, n
->nlmsg_len
);
1349 fn
->nlmsg_type
= RTM_DELADDR
;
1350 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1351 fn
->nlmsg_seq
= ++rth
.seq
;
1352 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1358 if (n
->nlmsg_type
== RTM_DELADDR
)
1359 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
1364 if (filter
.oneline
|| filter
.flushb
) {
1365 const char *dev
= ll_index_to_name(ifa
->ifa_index
);
1367 if (is_json_context()) {
1368 print_int(PRINT_JSON
,
1369 "index", NULL
, ifa
->ifa_index
);
1370 print_string(PRINT_JSON
, "dev", NULL
, dev
);
1372 fprintf(fp
, "%u: %s", ifa
->ifa_index
, dev
);
1376 name
= family_name(ifa
->ifa_family
);
1378 print_string(PRINT_ANY
, "family", " %s ", name
);
1380 print_int(PRINT_ANY
, "family_index", " family %d ",
1385 if (rta_tb
[IFA_LOCAL
]) {
1386 print_color_string(PRINT_ANY
,
1387 ifa_family_color(ifa
->ifa_family
),
1389 format_host_rta(ifa
->ifa_family
,
1390 rta_tb
[IFA_LOCAL
]));
1391 if (rta_tb
[IFA_ADDRESS
] &&
1392 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1393 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1394 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1395 print_string(PRINT_FP
, NULL
, " %s ", "peer");
1396 print_color_string(PRINT_ANY
,
1397 ifa_family_color(ifa
->ifa_family
),
1400 format_host_rta(ifa
->ifa_family
,
1401 rta_tb
[IFA_ADDRESS
]));
1403 print_int(PRINT_ANY
, "prefixlen", "/%d ", ifa
->ifa_prefixlen
);
1405 if (rta_tb
[IFA_RT_PRIORITY
])
1406 print_uint(PRINT_ANY
, "metric", "metric %u ",
1407 rta_getattr_u32(rta_tb
[IFA_RT_PRIORITY
]));
1413 if (rta_tb
[IFA_BROADCAST
]) {
1414 print_string(PRINT_FP
, NULL
, "%s ", "brd");
1415 print_color_string(PRINT_ANY
,
1416 ifa_family_color(ifa
->ifa_family
),
1419 format_host_rta(ifa
->ifa_family
,
1420 rta_tb
[IFA_BROADCAST
]));
1423 if (rta_tb
[IFA_ANYCAST
]) {
1424 print_string(PRINT_FP
, NULL
, "%s ", "any");
1425 print_color_string(PRINT_ANY
,
1426 ifa_family_color(ifa
->ifa_family
),
1429 format_host_rta(ifa
->ifa_family
,
1430 rta_tb
[IFA_ANYCAST
]));
1433 print_string(PRINT_ANY
,
1436 rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1438 print_ifa_flags(fp
, ifa
, ifa_flags
);
1440 if (rta_tb
[IFA_LABEL
])
1441 print_string(PRINT_ANY
,
1444 rta_getattr_str(rta_tb
[IFA_LABEL
]));
1446 if (rta_tb
[IFA_CACHEINFO
]) {
1447 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1450 print_string(PRINT_FP
, NULL
, " valid_lft ", NULL
);
1452 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
) {
1453 print_uint(PRINT_JSON
,
1455 NULL
, INFINITY_LIFE_TIME
);
1456 print_string(PRINT_FP
, NULL
, "%s", "forever");
1458 print_uint(PRINT_ANY
,
1459 "valid_life_time", "%usec", ci
->ifa_valid
);
1462 print_string(PRINT_FP
, NULL
, " preferred_lft ", NULL
);
1463 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
) {
1464 print_uint(PRINT_JSON
,
1465 "preferred_life_time",
1466 NULL
, INFINITY_LIFE_TIME
);
1467 print_string(PRINT_FP
, NULL
, "%s", "forever");
1469 if (ifa_flags
& IFA_F_DEPRECATED
)
1470 print_int(PRINT_ANY
,
1471 "preferred_life_time",
1475 print_uint(PRINT_ANY
,
1476 "preferred_life_time",
1481 print_string(PRINT_FP
, NULL
, "%s", "\n");
1487 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1488 struct nlmsg_list
*ainfo
, FILE *fp
)
1490 open_json_array(PRINT_JSON
, "addr_info");
1491 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1492 struct nlmsghdr
*n
= &ainfo
->h
;
1493 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1495 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1498 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(*ifa
)))
1501 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1502 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1505 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1508 open_json_object(NULL
);
1509 print_addrinfo(n
, fp
);
1510 close_json_object();
1512 close_json_array(PRINT_JSON
, NULL
);
1515 print_string(PRINT_FP
, NULL
, "%s", "\n");
1522 static int store_nlmsg(struct nlmsghdr
*n
, void *arg
)
1524 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1525 struct nlmsg_list
*h
;
1527 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1531 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1535 lchain
->tail
->next
= h
;
1540 ll_remember_index(n
, NULL
);
1544 static __u32 ipadd_dump_magic
= 0x47361222;
1546 static int ipadd_save_prep(void)
1550 if (isatty(STDOUT_FILENO
)) {
1551 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1555 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1556 if (ret
!= sizeof(ipadd_dump_magic
)) {
1557 fprintf(stderr
, "Can't write magic to dump file\n");
1564 static int ipadd_dump_check_magic(void)
1569 if (isatty(STDIN_FILENO
)) {
1570 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1574 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1575 if (magic
!= ipadd_dump_magic
) {
1576 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1583 static int save_nlmsg(struct nlmsghdr
*n
, void *arg
)
1587 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1588 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1589 fprintf(stderr
, "Short write while saving nlmsg\n");
1593 return ret
== n
->nlmsg_len
? 0 : ret
;
1596 static int show_handler(struct rtnl_ctrl_data
*ctrl
,
1597 struct nlmsghdr
*n
, void *arg
)
1599 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1601 open_json_object(NULL
);
1602 print_int(PRINT_ANY
, "index", "if%d:\n", ifa
->ifa_index
);
1603 print_addrinfo(n
, stdout
);
1604 close_json_object();
1608 static int ipaddr_showdump(void)
1612 if (ipadd_dump_check_magic())
1616 open_json_object(NULL
);
1617 open_json_array(PRINT_JSON
, "addr_info");
1619 err
= rtnl_from_file(stdin
, &show_handler
, NULL
);
1621 close_json_array(PRINT_JSON
, NULL
);
1622 close_json_object();
1628 static int restore_handler(struct rtnl_ctrl_data
*ctrl
,
1629 struct nlmsghdr
*n
, void *arg
)
1633 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1637 ret
= rtnl_talk(&rth
, n
, NULL
);
1638 if ((ret
< 0) && (errno
== EEXIST
))
1644 static int ipaddr_restore(void)
1646 if (ipadd_dump_check_magic())
1649 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1652 void free_nlmsg_chain(struct nlmsg_chain
*info
)
1654 struct nlmsg_list
*l
, *n
;
1656 for (l
= info
->head
; l
; l
= n
) {
1662 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1664 struct nlmsg_list
*l
, **lp
;
1667 while ((l
= *lp
) != NULL
) {
1669 int missing_net_address
= 1;
1670 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1671 struct nlmsg_list
*a
;
1673 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1674 struct nlmsghdr
*n
= &a
->h
;
1675 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1676 struct rtattr
*tb
[IFA_MAX
+ 1];
1677 unsigned int ifa_flags
;
1679 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1681 missing_net_address
= 0;
1682 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1684 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1687 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1688 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1690 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1693 if (ifa_label_match_rta(ifa
->ifa_index
, tb
[IFA_LABEL
]))
1697 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1698 if (inet_addr_match_rta(&filter
.pfx
, tb
[IFA_LOCAL
]))
1704 if (missing_net_address
&&
1705 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1715 static int ipaddr_dump_filter(struct nlmsghdr
*nlh
, int reqlen
)
1717 struct ifaddrmsg
*ifa
= NLMSG_DATA(nlh
);
1719 ifa
->ifa_index
= filter
.ifindex
;
1724 static int ipaddr_flush(void)
1727 char flushb
[4096-512];
1729 filter
.flushb
= flushb
;
1731 filter
.flushe
= sizeof(flushb
);
1733 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1734 if (rtnl_addrdump_req(&rth
, filter
.family
,
1735 ipaddr_dump_filter
) < 0) {
1736 perror("Cannot send dump request");
1740 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1741 stdout
, NLM_F_DUMP_INTR
) < 0) {
1742 fprintf(stderr
, "Flush terminated\n");
1745 if (filter
.flushed
== 0) {
1749 printf("Nothing to flush.\n");
1751 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1757 if (flush_update() < 0)
1761 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1765 /* If we are flushing, and specifying primary, then we
1766 * want to flush only a single round. Otherwise, we'll
1767 * start flushing secondaries that were promoted to
1770 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1773 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1778 static int iplink_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
1782 err
= addattr32(nlh
, reqlen
, IFLA_EXT_MASK
, RTEXT_FILTER_VF
);
1786 if (filter
.master
) {
1787 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter
.master
);
1793 struct rtattr
*linkinfo
;
1795 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
1797 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, filter
.kind
,
1798 strlen(filter
.kind
));
1802 addattr_nest_end(nlh
, linkinfo
);
1808 static int ipaddr_link_get(int index
, struct nlmsg_chain
*linfo
)
1810 struct iplink_req req
= {
1811 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
1812 .n
.nlmsg_flags
= NLM_F_REQUEST
,
1813 .n
.nlmsg_type
= RTM_GETLINK
,
1814 .i
.ifi_family
= filter
.family
,
1815 .i
.ifi_index
= index
,
1817 __u32 filt_mask
= RTEXT_FILTER_VF
;
1818 struct nlmsghdr
*answer
;
1821 filt_mask
|= RTEXT_FILTER_SKIP_STATS
;
1823 addattr32(&req
.n
, sizeof(req
), IFLA_EXT_MASK
, filt_mask
);
1825 if (rtnl_talk(&rth
, &req
.n
, &answer
) < 0) {
1826 perror("Cannot send link request");
1830 if (store_nlmsg(answer
, linfo
) < 0) {
1831 fprintf(stderr
, "Failed to process link information\n");
1838 /* fills in linfo with link data and optionally ainfo with address info
1839 * caller can walk lists as desired and must call free_nlmsg_chain for
1842 int ip_link_list(req_filter_fn_t filter_fn
, struct nlmsg_chain
*linfo
)
1844 if (rtnl_linkdump_req_filter_fn(&rth
, preferred_family
,
1846 perror("Cannot send dump request");
1850 if (rtnl_dump_filter(&rth
, store_nlmsg
, linfo
) < 0) {
1851 fprintf(stderr
, "Dump terminated\n");
1858 static int ip_addr_list(struct nlmsg_chain
*ainfo
)
1860 if (rtnl_addrdump_req(&rth
, filter
.family
, ipaddr_dump_filter
) < 0) {
1861 perror("Cannot send dump request");
1865 if (rtnl_dump_filter(&rth
, store_nlmsg
, ainfo
) < 0) {
1866 fprintf(stderr
, "Dump terminated\n");
1873 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1875 struct nlmsg_chain linfo
= { NULL
, NULL
};
1876 struct nlmsg_chain _ainfo
= { NULL
, NULL
}, *ainfo
= &_ainfo
;
1877 struct nlmsg_list
*l
;
1878 char *filter_dev
= NULL
;
1881 ipaddr_reset_filter(oneline
, 0);
1882 filter
.showqueue
= 1;
1883 filter
.family
= preferred_family
;
1885 if (action
== IPADD_FLUSH
) {
1887 fprintf(stderr
, "Flush requires arguments.\n");
1891 if (filter
.family
== AF_PACKET
) {
1892 fprintf(stderr
, "Cannot flush link addresses.\n");
1898 if (strcmp(*argv
, "to") == 0) {
1900 if (get_prefix(&filter
.pfx
, *argv
, filter
.family
))
1901 invarg("invalid \"to\"\n", *argv
);
1902 if (filter
.family
== AF_UNSPEC
)
1903 filter
.family
= filter
.pfx
.family
;
1904 } else if (strcmp(*argv
, "scope") == 0) {
1905 unsigned int scope
= 0;
1908 filter
.scopemask
= -1;
1909 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1910 if (strcmp(*argv
, "all") != 0)
1911 invarg("invalid \"scope\"\n", *argv
);
1912 scope
= RT_SCOPE_NOWHERE
;
1913 filter
.scopemask
= 0;
1915 filter
.scope
= scope
;
1916 } else if (strcmp(*argv
, "up") == 0) {
1918 } else if (get_filter(*argv
) == 0) {
1920 } else if (strcmp(*argv
, "label") == 0) {
1922 filter
.label
= *argv
;
1923 } else if (strcmp(*argv
, "group") == 0) {
1925 if (rtnl_group_a2n(&filter
.group
, *argv
))
1926 invarg("Invalid \"group\" value\n", *argv
);
1927 } else if (strcmp(*argv
, "master") == 0) {
1931 ifindex
= ll_name_to_index(*argv
);
1933 invarg("Device does not exist\n", *argv
);
1934 filter
.master
= ifindex
;
1935 } else if (strcmp(*argv
, "vrf") == 0) {
1939 ifindex
= ll_name_to_index(*argv
);
1941 invarg("Not a valid VRF name\n", *argv
);
1942 if (!name_is_vrf(*argv
))
1943 invarg("Not a valid VRF name\n", *argv
);
1944 filter
.master
= ifindex
;
1945 } else if (strcmp(*argv
, "type") == 0) {
1949 soff
= strlen(*argv
) - strlen("_slave");
1950 if (!strcmp(*argv
+ soff
, "_slave")) {
1951 (*argv
)[soff
] = '\0';
1952 filter
.slave_kind
= *argv
;
1954 filter
.kind
= *argv
;
1957 if (strcmp(*argv
, "dev") == 0)
1959 else if (matches(*argv
, "help") == 0)
1962 duparg2("dev", *argv
);
1969 filter
.ifindex
= ll_name_to_index(filter_dev
);
1970 if (filter
.ifindex
<= 0) {
1971 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1976 if (action
== IPADD_FLUSH
)
1977 return ipaddr_flush();
1979 if (action
== IPADD_SAVE
) {
1980 if (ipadd_save_prep())
1983 if (rtnl_addrdump_req(&rth
, preferred_family
,
1984 ipaddr_dump_filter
) < 0) {
1985 perror("Cannot send dump request");
1989 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1990 fprintf(stderr
, "Save terminated\n");
1998 * Initialize a json_writer and open an array object
1999 * if -json was specified.
2004 * If only filter_dev present and none of the other
2005 * link filters are present, use RTM_GETLINK to get
2008 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
2009 if (iplink_get(filter_dev
, RTEXT_FILTER_VF
) < 0) {
2010 perror("Cannot send link get request");
2018 if (filter
.ifindex
) {
2019 if (ipaddr_link_get(filter
.ifindex
, &linfo
) != 0)
2022 if (ip_link_list(iplink_filter_req
, &linfo
) != 0)
2026 if (filter
.family
!= AF_PACKET
) {
2030 if (ip_addr_list(ainfo
) != 0)
2033 ipaddr_filter(&linfo
, ainfo
);
2036 for (l
= linfo
.head
; l
; l
= l
->next
) {
2037 struct nlmsghdr
*n
= &l
->h
;
2038 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
2041 open_json_object(NULL
);
2042 if (brief
|| !no_link
)
2043 res
= print_linkinfo(n
, stdout
);
2044 if (res
>= 0 && filter
.family
!= AF_PACKET
)
2045 print_selected_addrinfo(ifi
, ainfo
->head
, stdout
);
2046 if (res
> 0 && !do_link
&& show_stats
)
2047 print_link_stats(stdout
, n
);
2048 close_json_object();
2053 free_nlmsg_chain(ainfo
);
2054 free_nlmsg_chain(&linfo
);
2060 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
2062 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
2063 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
2064 struct ifla_vf_rate
*vf_rate
;
2067 rem
= RTA_PAYLOAD(vflist
);
2069 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
2070 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
2072 if (!vf
[IFLA_VF_RATE
]) {
2073 fprintf(stderr
, "VF min/max rate API not supported\n");
2077 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
2078 if (vf_rate
->vf
== vfnum
) {
2079 *min
= vf_rate
->min_tx_rate
;
2080 *max
= vf_rate
->max_tx_rate
;
2084 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
2088 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, const char *dev
)
2090 struct nlmsg_chain linfo
= { NULL
, NULL
};
2091 struct rtattr
*tb
[IFLA_MAX
+1];
2092 struct ifinfomsg
*ifi
;
2093 struct nlmsg_list
*l
;
2097 idx
= ll_name_to_index(dev
);
2099 fprintf(stderr
, "Device %s does not exist\n", dev
);
2103 if (rtnl_linkdump_req(&rth
, AF_UNSPEC
) < 0) {
2104 perror("Cannot send dump request");
2107 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
2108 fprintf(stderr
, "Dump terminated\n");
2111 for (l
= linfo
.head
; l
; l
= l
->next
) {
2113 ifi
= NLMSG_DATA(n
);
2115 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
2116 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
2119 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
2121 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
2122 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
2128 int ipaddr_list_link(int argc
, char **argv
)
2130 preferred_family
= AF_PACKET
;
2132 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
2135 void ipaddr_reset_filter(int oneline
, int ifindex
)
2137 memset(&filter
, 0, sizeof(filter
));
2138 filter
.oneline
= oneline
;
2139 filter
.ifindex
= ifindex
;
2143 static int default_scope(inet_prefix
*lcl
)
2145 if (lcl
->family
== AF_INET
) {
2146 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
2147 return RT_SCOPE_HOST
;
2152 static bool ipaddr_is_multicast(inet_prefix
*a
)
2154 if (a
->family
== AF_INET
)
2155 return IN_MULTICAST(ntohl(a
->data
[0]));
2156 else if (a
->family
== AF_INET6
)
2157 return IN6_IS_ADDR_MULTICAST(a
->data
);
2162 static bool is_valid_label(const char *dev
, const char *label
)
2164 size_t len
= strlen(dev
);
2166 if (strncmp(label
, dev
, len
) != 0)
2169 return label
[len
] == '\0' || label
[len
] == ':';
2172 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
2176 struct ifaddrmsg ifa
;
2179 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
2180 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
2181 .n
.nlmsg_type
= cmd
,
2182 .ifa
.ifa_family
= preferred_family
,
2186 char *lcl_arg
= NULL
;
2187 char *valid_lftp
= NULL
;
2188 char *preferred_lftp
= NULL
;
2189 inet_prefix lcl
= {};
2196 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
2197 __u32 valid_lft
= INFINITY_LIFE_TIME
;
2198 unsigned int ifa_flags
= 0;
2201 if (strcmp(*argv
, "peer") == 0 ||
2202 strcmp(*argv
, "remote") == 0) {
2206 duparg("peer", *argv
);
2207 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
2208 peer_len
= peer
.bytelen
;
2209 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2210 req
.ifa
.ifa_family
= peer
.family
;
2211 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
2212 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
2213 } else if (matches(*argv
, "broadcast") == 0 ||
2214 strcmp(*argv
, "brd") == 0) {
2219 duparg("broadcast", *argv
);
2220 if (strcmp(*argv
, "+") == 0)
2222 else if (strcmp(*argv
, "-") == 0)
2225 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2226 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2227 req
.ifa
.ifa_family
= addr
.family
;
2228 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
2229 brd_len
= addr
.bytelen
;
2231 } else if (strcmp(*argv
, "anycast") == 0) {
2236 duparg("anycast", *argv
);
2237 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
2238 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2239 req
.ifa
.ifa_family
= addr
.family
;
2240 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
2241 any_len
= addr
.bytelen
;
2242 } else if (strcmp(*argv
, "scope") == 0) {
2243 unsigned int scope
= 0;
2246 if (rtnl_rtscope_a2n(&scope
, *argv
))
2247 invarg("invalid scope value.", *argv
);
2248 req
.ifa
.ifa_scope
= scope
;
2250 } else if (strcmp(*argv
, "dev") == 0) {
2253 } else if (strcmp(*argv
, "label") == 0) {
2256 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
2257 } else if (matches(*argv
, "metric") == 0 ||
2258 matches(*argv
, "priority") == 0 ||
2259 matches(*argv
, "preference") == 0) {
2263 if (get_u32(&metric
, *argv
, 0))
2264 invarg("\"metric\" value is invalid\n", *argv
);
2265 addattr32(&req
.n
, sizeof(req
), IFA_RT_PRIORITY
, metric
);
2266 } else if (matches(*argv
, "valid_lft") == 0) {
2268 duparg("valid_lft", *argv
);
2271 if (set_lifetime(&valid_lft
, *argv
))
2272 invarg("valid_lft value", *argv
);
2273 } else if (matches(*argv
, "preferred_lft") == 0) {
2275 duparg("preferred_lft", *argv
);
2277 preferred_lftp
= *argv
;
2278 if (set_lifetime(&preferred_lft
, *argv
))
2279 invarg("preferred_lft value", *argv
);
2280 } else if (strcmp(*argv
, "home") == 0) {
2281 if (req
.ifa
.ifa_family
== AF_INET6
)
2282 ifa_flags
|= IFA_F_HOMEADDRESS
;
2284 fprintf(stderr
, "Warning: home option can be set only for IPv6 addresses\n");
2285 } else if (strcmp(*argv
, "nodad") == 0) {
2286 if (req
.ifa
.ifa_family
== AF_INET6
)
2287 ifa_flags
|= IFA_F_NODAD
;
2289 fprintf(stderr
, "Warning: nodad option can be set only for IPv6 addresses\n");
2290 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
2291 if (req
.ifa
.ifa_family
== AF_INET6
)
2292 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
2294 fprintf(stderr
, "Warning: mngtmpaddr option can be set only for IPv6 addresses\n");
2295 } else if (strcmp(*argv
, "noprefixroute") == 0) {
2296 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
2297 } else if (strcmp(*argv
, "autojoin") == 0) {
2298 ifa_flags
|= IFA_F_MCAUTOJOIN
;
2300 if (strcmp(*argv
, "local") == 0)
2302 if (matches(*argv
, "help") == 0)
2305 duparg2("local", *argv
);
2307 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
2308 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
2309 req
.ifa
.ifa_family
= lcl
.family
;
2310 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
2311 local_len
= lcl
.bytelen
;
2315 if (ifa_flags
<= 0xff)
2316 req
.ifa
.ifa_flags
= ifa_flags
;
2318 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
2321 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
2324 if (l
&& !is_valid_label(d
, l
)) {
2326 "\"label\" (%s) must match \"dev\" (%s) or be prefixed by \"dev\" with a colon.\n",
2331 if (peer_len
== 0 && local_len
) {
2332 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
2334 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
2335 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
2336 " This special behaviour is likely to disappear in further releases,\n"
2337 " fix your scripts!\n", lcl_arg
, local_len
*8);
2340 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
2343 if (req
.ifa
.ifa_prefixlen
== 0)
2344 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
2346 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
2350 if (req
.ifa
.ifa_family
!= AF_INET
) {
2351 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
2355 if (brd
.bitlen
<= 30) {
2356 for (i
= 31; i
>= brd
.bitlen
; i
--) {
2358 brd
.data
[0] |= htonl(1<<(31-i
));
2360 brd
.data
[0] &= ~htonl(1<<(31-i
));
2362 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
2363 brd_len
= brd
.bytelen
;
2366 if (!scoped
&& cmd
!= RTM_DELADDR
)
2367 req
.ifa
.ifa_scope
= default_scope(&lcl
);
2369 req
.ifa
.ifa_index
= ll_name_to_index(d
);
2370 if (!req
.ifa
.ifa_index
)
2373 if (valid_lftp
|| preferred_lftp
) {
2374 struct ifa_cacheinfo cinfo
= {};
2377 fprintf(stderr
, "valid_lft is zero\n");
2380 if (valid_lft
< preferred_lft
) {
2381 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
2385 cinfo
.ifa_prefered
= preferred_lft
;
2386 cinfo
.ifa_valid
= valid_lft
;
2387 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
2391 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
2392 fprintf(stderr
, "autojoin needs multicast address\n");
2396 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
2402 int do_ipaddr(int argc
, char **argv
)
2405 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
2406 if (matches(*argv
, "add") == 0)
2407 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
2408 if (matches(*argv
, "change") == 0 ||
2409 strcmp(*argv
, "chg") == 0)
2410 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2411 if (matches(*argv
, "replace") == 0)
2412 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2413 if (matches(*argv
, "delete") == 0)
2414 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2415 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2416 || matches(*argv
, "lst") == 0)
2417 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2418 if (matches(*argv
, "flush") == 0)
2419 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2420 if (matches(*argv
, "save") == 0)
2421 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2422 if (matches(*argv
, "showdump") == 0)
2423 return ipaddr_showdump();
2424 if (matches(*argv
, "restore") == 0)
2425 return ipaddr_restore();
2426 if (matches(*argv
, "help") == 0)
2428 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);