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>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
22 #include <sys/param.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/sockios.h>
32 #include <linux/net_namespace.h>
37 #include "ip_common.h"
69 static void usage(void) __attribute__((noreturn
));
71 static void usage(void)
76 fprintf(stderr
, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
77 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
78 fprintf(stderr
, " ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
79 fprintf(stderr
, " ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
80 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
81 fprintf(stderr
, " ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n");
82 fprintf(stderr
, " [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n");
83 fprintf(stderr
, " [ label LABEL ] [up] [ vrf NAME ] ]\n");
84 fprintf(stderr
, " ip address {showdump|restore}\n");
85 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
86 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
87 fprintf(stderr
, " [ label IFNAME ] [ scope SCOPE-ID ]\n");
88 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
89 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
90 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
91 fprintf(stderr
, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
92 fprintf(stderr
, " CONFFLAG-LIST ]\n");
93 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
94 fprintf(stderr
, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
95 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
96 fprintf(stderr
, "LFT := forever | SECONDS\n");
97 fprintf(stderr
, "TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | macvtap |\n");
98 fprintf(stderr
, " bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |\n");
99 fprintf(stderr
, " gre | gretap | ip6gre | ip6gretap | vti | nlmon | can |\n");
100 fprintf(stderr
, " bond_slave | ipvlan | geneve | bridge_slave | vrf | hsr | macsec }\n");
105 static void print_link_flags(FILE *fp
, unsigned int flags
, unsigned int mdown
)
108 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
109 fprintf(fp
, "NO-CARRIER%s", flags
? "," : "");
110 flags
&= ~IFF_RUNNING
;
111 #define _PF(f) if (flags&IFF_##f) { \
112 flags &= ~IFF_##f ; \
113 fprintf(fp, #f "%s", flags ? "," : ""); }
134 fprintf(fp
, "%x", flags
);
136 fprintf(fp
, ",M-DOWN");
140 static const char *oper_states
[] = {
141 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
142 "TESTING", "DORMANT", "UP"
145 static void print_operstate(FILE *f
, __u8 state
)
147 if (state
>= ARRAY_SIZE(oper_states
)) {
148 fprintf(f
, "state %#x ", state
);
150 color_fprintf(f
, oper_state_color(state
),
151 "%-14s ", oper_states
[state
]);
153 fprintf(f
, "state ");
154 color_fprintf(f
, oper_state_color(state
),
155 "%s ", oper_states
[state
]);
159 int get_operstate(const char *name
)
163 for (i
= 0; i
< ARRAY_SIZE(oper_states
); i
++)
164 if (strcasecmp(name
, oper_states
[i
]) == 0)
169 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
174 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
176 struct ifreq ifr
= {};
177 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
182 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
183 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
184 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
192 fprintf(f
, "qlen %d", qlen
);
195 static const char *link_modes
[] = {
199 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
201 unsigned int mode
= rta_getattr_u8(tb
);
203 if (mode
>= ARRAY_SIZE(link_modes
))
204 fprintf(f
, "mode %d ", mode
);
206 fprintf(f
, "mode %s ", link_modes
[mode
]);
209 static char *parse_link_kind(struct rtattr
*tb
, bool slave
)
211 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
212 int attr
= slave
? IFLA_INFO_SLAVE_KIND
: IFLA_INFO_KIND
;
214 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
217 return RTA_DATA(linkinfo
[attr
]);
222 static int match_link_kind(struct rtattr
**tb
, const char *kind
, bool slave
)
224 if (!tb
[IFLA_LINKINFO
])
227 return strcmp(parse_link_kind(tb
[IFLA_LINKINFO
], slave
), kind
);
230 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
232 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
233 struct link_util
*lu
;
234 struct link_util
*slave_lu
;
239 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
241 if (linkinfo
[IFLA_INFO_KIND
]) {
242 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
244 fprintf(fp
, "%s", _SL_
);
245 fprintf(fp
, " %s ", kind
);
247 lu
= get_link_kind(kind
);
248 if (lu
&& lu
->print_opt
) {
249 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
251 if (linkinfo
[IFLA_INFO_DATA
]) {
252 parse_rtattr_nested(attr
, lu
->maxattr
,
253 linkinfo
[IFLA_INFO_DATA
]);
256 lu
->print_opt(lu
, fp
, data
);
258 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
260 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
264 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
265 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
267 fprintf(fp
, "%s", _SL_
);
268 fprintf(fp
, " %s_slave ", slave_kind
);
269 snprintf(slave
, sizeof(slave
), "%s_slave", slave_kind
);
271 slave_lu
= get_link_kind(slave
);
272 if (slave_lu
&& slave_lu
->print_opt
) {
273 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
275 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
276 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
277 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
280 slave_lu
->print_opt(slave_lu
, fp
, data
);
285 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
287 struct rtattr
*inet6_attr
;
288 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
290 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
294 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
296 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
297 __u8 mode
= rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
300 case IN6_ADDR_GEN_MODE_EUI64
:
301 fprintf(fp
, "addrgenmode eui64 ");
303 case IN6_ADDR_GEN_MODE_NONE
:
304 fprintf(fp
, "addrgenmode none ");
306 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
307 fprintf(fp
, "addrgenmode stable_secret ");
309 case IN6_ADDR_GEN_MODE_RANDOM
:
310 fprintf(fp
, "addrgenmode random ");
313 fprintf(fp
, "addrgenmode %#.2hhx ", mode
);
319 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
321 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
323 struct ifla_vf_mac
*vf_mac
;
324 struct ifla_vf_vlan
*vf_vlan
;
325 struct ifla_vf_tx_rate
*vf_tx_rate
;
326 struct ifla_vf_spoofchk
*vf_spoofchk
;
327 struct ifla_vf_link_state
*vf_linkstate
;
328 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
333 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
334 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
338 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
340 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
341 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
342 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
344 /* Check if the spoof checking vf info type is supported by
347 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
348 vf
[IFLA_VF_TX_RATE
]->rta_len
);
350 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
353 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
356 /* Check if the link state vf info type is supported by
359 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_SPOOFCHK
] +
360 vf
[IFLA_VF_SPOOFCHK
]->rta_len
);
362 if (tmp
->rta_type
!= IFLA_VF_LINK_STATE
)
365 vf_linkstate
= RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
369 fprintf(fp
, "%s vf %d MAC %s", _SL_
, vf_mac
->vf
,
370 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
371 ETH_ALEN
, 0, b1
, sizeof(b1
)));
373 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
375 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
376 if (vf_tx_rate
->rate
)
377 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
379 if (vf
[IFLA_VF_RATE
]) {
380 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
382 if (vf_rate
->max_tx_rate
)
383 fprintf(fp
, ", max_tx_rate %dMbps", vf_rate
->max_tx_rate
);
384 if (vf_rate
->min_tx_rate
)
385 fprintf(fp
, ", min_tx_rate %dMbps", vf_rate
->min_tx_rate
);
388 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
389 if (vf_spoofchk
->setting
)
390 fprintf(fp
, ", spoof checking on");
392 fprintf(fp
, ", spoof checking off");
395 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
396 fprintf(fp
, ", link-state auto");
397 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
398 fprintf(fp
, ", link-state enable");
400 fprintf(fp
, ", link-state disable");
402 if (vf
[IFLA_VF_TRUST
]) {
403 struct ifla_vf_trust
*vf_trust
= RTA_DATA(vf
[IFLA_VF_TRUST
]);
405 if (vf_trust
->setting
!= -1)
406 fprintf(fp
, ", trust %s",
407 vf_trust
->setting
? "on" : "off");
409 if (vf
[IFLA_VF_STATS
] && show_stats
)
410 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
413 static void print_num(FILE *fp
, unsigned int width
, uint64_t count
)
415 const char *prefix
= "kMGTPE";
416 const unsigned int base
= use_iec
? 1024 : 1000;
419 uint8_t precision
= 2;
422 if (!human_readable
|| count
< base
) {
423 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
427 /* increase value by a factor of 1000/1024 and print
428 * if result is something a human can read */
431 if (count
/ base
< powi
)
439 /* try to guess a good number of digits for precision */
440 for (; precision
> 0; precision
--) {
442 if (count
/ powi
< powj
)
446 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
447 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
449 fprintf(fp
, "%-*s ", width
, buf
);
452 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
454 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1];
456 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
457 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
461 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfstats
);
464 fprintf(fp
, "%s", _SL_
);
465 fprintf(fp
, " RX: bytes packets mcast bcast %s", _SL_
);
468 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_BYTES
]));
469 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_PACKETS
]));
470 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_MULTICAST
]));
471 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_BROADCAST
]));
474 fprintf(fp
, "%s", _SL_
);
475 fprintf(fp
, " TX: bytes packets %s", _SL_
);
478 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_BYTES
]));
479 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_PACKETS
]));
482 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
483 const struct rtattr
*carrier_changes
)
486 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
487 s
->rx_compressed
? "compressed" : "", _SL_
);
490 print_num(fp
, 10, s
->rx_bytes
);
491 print_num(fp
, 8, s
->rx_packets
);
492 print_num(fp
, 7, s
->rx_errors
);
493 print_num(fp
, 7, s
->rx_dropped
);
494 print_num(fp
, 7, s
->rx_over_errors
);
495 print_num(fp
, 7, s
->multicast
);
496 if (s
->rx_compressed
)
497 print_num(fp
, 7, s
->rx_compressed
);
500 if (show_stats
> 1) {
501 fprintf(fp
, "%s", _SL_
);
502 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
503 s
->rx_nohandler
? " nohandler" : "", _SL_
);
506 print_num(fp
, 8, s
->rx_length_errors
);
507 print_num(fp
, 7, s
->rx_crc_errors
);
508 print_num(fp
, 7, s
->rx_frame_errors
);
509 print_num(fp
, 7, s
->rx_fifo_errors
);
510 print_num(fp
, 7, s
->rx_missed_errors
);
512 print_num(fp
, 7, s
->rx_nohandler
);
515 fprintf(fp
, "%s", _SL_
);
518 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
519 s
->tx_compressed
? "compressed" : "", _SL_
);
522 print_num(fp
, 10, s
->tx_bytes
);
523 print_num(fp
, 8, s
->tx_packets
);
524 print_num(fp
, 7, s
->tx_errors
);
525 print_num(fp
, 7, s
->tx_dropped
);
526 print_num(fp
, 7, s
->tx_carrier_errors
);
527 print_num(fp
, 7, s
->collisions
);
528 if (s
->tx_compressed
)
529 print_num(fp
, 7, s
->tx_compressed
);
532 if (show_stats
> 1) {
533 fprintf(fp
, "%s", _SL_
);
534 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
536 fprintf(fp
, " transns");
537 fprintf(fp
, "%s", _SL_
);
540 print_num(fp
, 8, s
->tx_aborted_errors
);
541 print_num(fp
, 7, s
->tx_fifo_errors
);
542 print_num(fp
, 7, s
->tx_window_errors
);
543 print_num(fp
, 7, s
->tx_heartbeat_errors
);
545 print_num(fp
, 7, *(uint32_t *)RTA_DATA(carrier_changes
));
549 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
550 const struct rtattr
*carrier_changes
)
553 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
554 s
->rx_compressed
? "compressed" : "", _SL_
);
558 print_num(fp
, 10, s
->rx_bytes
);
559 print_num(fp
, 8, s
->rx_packets
);
560 print_num(fp
, 7, s
->rx_errors
);
561 print_num(fp
, 7, s
->rx_dropped
);
562 print_num(fp
, 7, s
->rx_over_errors
);
563 print_num(fp
, 7, s
->multicast
);
564 if (s
->rx_compressed
)
565 print_num(fp
, 7, s
->rx_compressed
);
568 if (show_stats
> 1) {
569 fprintf(fp
, "%s", _SL_
);
570 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
571 s
->rx_nohandler
? " nohandler" : "", _SL_
);
573 print_num(fp
, 8, s
->rx_length_errors
);
574 print_num(fp
, 7, s
->rx_crc_errors
);
575 print_num(fp
, 7, s
->rx_frame_errors
);
576 print_num(fp
, 7, s
->rx_fifo_errors
);
577 print_num(fp
, 7, s
->rx_missed_errors
);
579 print_num(fp
, 7, s
->rx_nohandler
);
581 fprintf(fp
, "%s", _SL_
);
584 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
585 s
->tx_compressed
? "compressed" : "", _SL_
);
588 print_num(fp
, 10, s
->tx_bytes
);
589 print_num(fp
, 8, s
->tx_packets
);
590 print_num(fp
, 7, s
->tx_errors
);
591 print_num(fp
, 7, s
->tx_dropped
);
592 print_num(fp
, 7, s
->tx_carrier_errors
);
593 print_num(fp
, 7, s
->collisions
);
594 if (s
->tx_compressed
)
595 print_num(fp
, 7, s
->tx_compressed
);
598 if (show_stats
> 1) {
599 fprintf(fp
, "%s", _SL_
);
600 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
602 fprintf(fp
, " transns");
603 fprintf(fp
, "%s", _SL_
);
606 print_num(fp
, 8, s
->tx_aborted_errors
);
607 print_num(fp
, 7, s
->tx_fifo_errors
);
608 print_num(fp
, 7, s
->tx_window_errors
);
609 print_num(fp
, 7, s
->tx_heartbeat_errors
);
611 print_num(fp
, 7, *(uint32_t *)RTA_DATA(carrier_changes
));
615 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
617 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
619 if (tb
[IFLA_STATS64
]) {
620 struct rtnl_link_stats64 stats
= { 0 };
622 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS64
]),
623 MIN(RTA_PAYLOAD(tb
[IFLA_STATS64
]), sizeof(stats
)));
625 print_link_stats64(fp
, &stats
, carrier_changes
);
626 } else if (tb
[IFLA_STATS
]) {
627 struct rtnl_link_stats stats
= { 0 };
629 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS
]),
630 MIN(RTA_PAYLOAD(tb
[IFLA_STATS
]), sizeof(stats
)));
632 print_link_stats32(fp
, &stats
, carrier_changes
);
636 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
638 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
639 struct rtattr
*tb
[IFLA_MAX
+1];
641 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
642 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
643 __print_link_stats(fp
, tb
);
644 fprintf(fp
, "%s", _SL_
);
647 int print_linkinfo_brief(const struct sockaddr_nl
*who
,
648 struct nlmsghdr
*n
, void *arg
)
650 FILE *fp
= (FILE *)arg
;
651 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
652 struct rtattr
*tb
[IFLA_MAX
+1];
653 int len
= n
->nlmsg_len
;
655 char buf
[32] = { 0, };
656 unsigned int m_flag
= 0;
658 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
661 len
-= NLMSG_LENGTH(sizeof(*ifi
));
665 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
667 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
670 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
671 if (tb
[IFLA_IFNAME
] == NULL
) {
672 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
675 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
676 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
679 if (tb
[IFLA_GROUP
]) {
680 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
682 if (filter
.group
!= -1 && group
!= filter
.group
)
686 if (tb
[IFLA_MASTER
]) {
687 int master
= *(int *)RTA_DATA(tb
[IFLA_MASTER
]);
689 if (filter
.master
> 0 && master
!= filter
.master
)
691 } else if (filter
.master
> 0)
694 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
697 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
700 if (n
->nlmsg_type
== RTM_DELLINK
)
701 fprintf(fp
, "Deleted ");
703 name
= (char *)(tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
707 int iflink
= *(int *)RTA_DATA(tb
[IFLA_LINK
]);
710 snprintf(buf
, sizeof(buf
), "%s@NONE", name
);
712 snprintf(buf
, sizeof(buf
),
713 "%s@%s", name
, ll_idx_n2a(iflink
, b1
));
714 m_flag
= ll_index_to_flags(iflink
);
715 m_flag
= !(m_flag
& IFF_UP
);
718 snprintf(buf
, sizeof(buf
), "%s", name
);
720 fprintf(fp
, "%-16s ", buf
);
722 if (tb
[IFLA_OPERSTATE
])
723 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
725 if (filter
.family
== AF_PACKET
) {
727 if (tb
[IFLA_ADDRESS
]) {
728 color_fprintf(fp
, COLOR_MAC
, "%s ",
729 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
730 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
736 if (filter
.family
== AF_PACKET
)
737 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
739 if (filter
.family
== AF_PACKET
)
745 int print_linkinfo(const struct sockaddr_nl
*who
,
746 struct nlmsghdr
*n
, void *arg
)
748 FILE *fp
= (FILE *)arg
;
749 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
750 struct rtattr
*tb
[IFLA_MAX
+1];
751 int len
= n
->nlmsg_len
;
752 unsigned int m_flag
= 0;
754 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
757 len
-= NLMSG_LENGTH(sizeof(*ifi
));
761 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
763 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
766 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
767 if (tb
[IFLA_IFNAME
] == NULL
) {
768 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
771 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
772 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
775 if (tb
[IFLA_GROUP
]) {
776 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
778 if (filter
.group
!= -1 && group
!= filter
.group
)
782 if (tb
[IFLA_MASTER
]) {
783 int master
= *(int *)RTA_DATA(tb
[IFLA_MASTER
]);
785 if (filter
.master
> 0 && master
!= filter
.master
)
787 } else if (filter
.master
> 0)
790 if (filter
.kind
&& match_link_kind(tb
, filter
.kind
, 0))
793 if (filter
.slave_kind
&& match_link_kind(tb
, filter
.slave_kind
, 1))
796 if (n
->nlmsg_type
== RTM_DELLINK
)
797 fprintf(fp
, "Deleted ");
799 fprintf(fp
, "%d: ", ifi
->ifi_index
);
800 color_fprintf(fp
, COLOR_IFNAME
, "%s",
801 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
805 int iflink
= *(int *)RTA_DATA(tb
[IFLA_LINK
]);
808 fprintf(fp
, "@NONE: ");
810 if (tb
[IFLA_LINK_NETNSID
])
811 fprintf(fp
, "@if%d: ", iflink
);
813 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
814 m_flag
= ll_index_to_flags(iflink
);
815 m_flag
= !(m_flag
& IFF_UP
);
821 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
824 fprintf(fp
, "mtu %u ", *(int *)RTA_DATA(tb
[IFLA_MTU
]));
826 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
827 if (tb
[IFLA_MASTER
]) {
829 fprintf(fp
, "master %s ", ll_idx_n2a(*(int *)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
832 if (tb
[IFLA_OPERSTATE
])
833 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
835 if (do_link
&& tb
[IFLA_LINKMODE
])
836 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
838 if (tb
[IFLA_GROUP
]) {
840 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
842 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
845 if (filter
.showqueue
)
846 print_queuelen(fp
, tb
);
848 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
850 fprintf(fp
, "%s", _SL_
);
851 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
853 if (tb
[IFLA_ADDRESS
]) {
854 color_fprintf(fp
, COLOR_MAC
, "%s",
855 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
856 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
860 if (tb
[IFLA_BROADCAST
]) {
861 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
862 fprintf(fp
, " peer ");
864 fprintf(fp
, " brd ");
865 color_fprintf(fp
, COLOR_MAC
, "%s",
866 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
867 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
873 if (tb
[IFLA_LINK_NETNSID
]) {
874 int id
= *(int *)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
877 fprintf(fp
, " link-netnsid %d", id
);
879 fprintf(fp
, " link-netnsid unknown");
882 if (tb
[IFLA_PROTO_DOWN
]) {
883 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
884 fprintf(fp
, " protodown on ");
888 if (tb
[IFLA_PROMISCUITY
])
889 fprintf(fp
, " promiscuity %u ",
890 *(int *)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
892 if (tb
[IFLA_LINKINFO
])
893 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
895 if (do_link
&& tb
[IFLA_AF_SPEC
])
896 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
898 if (tb
[IFLA_NUM_TX_QUEUES
])
899 fprintf(fp
, "numtxqueues %u ",
900 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
902 if (tb
[IFLA_NUM_RX_QUEUES
])
903 fprintf(fp
, "numrxqueues %u ",
904 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
906 if (tb
[IFLA_GSO_MAX_SIZE
])
907 fprintf(fp
, "gso_max_size %u ",
908 rta_getattr_u32(tb
[IFLA_GSO_MAX_SIZE
]));
910 if (tb
[IFLA_GSO_MAX_SEGS
])
911 fprintf(fp
, "gso_max_segs %u ",
912 rta_getattr_u32(tb
[IFLA_GSO_MAX_SEGS
]));
914 if (tb
[IFLA_PHYS_PORT_NAME
])
915 fprintf(fp
, "portname %s ",
916 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
918 if (tb
[IFLA_PHYS_PORT_ID
]) {
920 fprintf(fp
, "portid %s ",
921 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
922 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
926 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
928 fprintf(fp
, "switchid %s ",
929 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
930 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
936 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
937 fprintf(fp
, "%s alias %s", _SL_
,
938 rta_getattr_str(tb
[IFLA_IFALIAS
]));
941 if (do_link
&& show_stats
) {
942 fprintf(fp
, "%s", _SL_
);
943 __print_link_stats(fp
, tb
);
946 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
947 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
948 int rem
= RTA_PAYLOAD(vflist
);
950 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
959 static int flush_update(void)
963 * Note that the kernel may delete multiple addresses for one
964 * delete request (e.g. if ipv4 address promotion is disabled).
965 * Since a flush operation is really a series of delete requests
966 * its possible that we may request an address delete that has
967 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
968 * errors returned from a flush request
970 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
971 (errno
!= EADDRNOTAVAIL
)) {
972 perror("Failed to send flush request");
979 static int set_lifetime(unsigned int *lifetime
, char *argv
)
981 if (strcmp(argv
, "forever") == 0)
982 *lifetime
= INFINITY_LIFE_TIME
;
983 else if (get_u32(lifetime
, argv
, 0))
989 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
990 struct rtattr
*ifa_flags_attr
)
992 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
996 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1000 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1001 int len
= n
->nlmsg_len
;
1003 /* Use local copy of ifa_flags to not interfere with filtering code */
1004 unsigned int ifa_flags
;
1005 struct rtattr
*rta_tb
[IFA_MAX
+1];
1009 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
1011 len
-= NLMSG_LENGTH(sizeof(*ifa
));
1013 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1017 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
1020 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
1021 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1023 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1025 if (!rta_tb
[IFA_LOCAL
])
1026 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1027 if (!rta_tb
[IFA_ADDRESS
])
1028 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1030 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1032 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1034 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1040 if (rta_tb
[IFA_LABEL
])
1041 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
1043 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1044 if (fnmatch(filter
.label
, label
, 0) != 0)
1047 if (filter
.pfx
.family
) {
1048 if (rta_tb
[IFA_LOCAL
]) {
1049 inet_prefix dst
= { .family
= ifa
->ifa_family
};
1051 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
1052 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1057 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1060 if (filter
.flushb
) {
1061 struct nlmsghdr
*fn
;
1063 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1067 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1068 memcpy(fn
, n
, n
->nlmsg_len
);
1069 fn
->nlmsg_type
= RTM_DELADDR
;
1070 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1071 fn
->nlmsg_seq
= ++rth
.seq
;
1072 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1078 if (n
->nlmsg_type
== RTM_DELADDR
)
1079 fprintf(fp
, "Deleted ");
1082 if (filter
.oneline
|| filter
.flushb
)
1083 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
1084 if (ifa
->ifa_family
== AF_INET
)
1085 fprintf(fp
, " inet ");
1086 else if (ifa
->ifa_family
== AF_INET6
)
1087 fprintf(fp
, " inet6 ");
1088 else if (ifa
->ifa_family
== AF_DECnet
)
1089 fprintf(fp
, " dnet ");
1090 else if (ifa
->ifa_family
== AF_IPX
)
1091 fprintf(fp
, " ipx ");
1093 fprintf(fp
, " family %d ", ifa
->ifa_family
);
1096 if (rta_tb
[IFA_LOCAL
]) {
1097 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s",
1098 format_host_rta(ifa
->ifa_family
,
1099 rta_tb
[IFA_LOCAL
]));
1100 if (rta_tb
[IFA_ADDRESS
] &&
1101 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1102 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1103 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1104 fprintf(fp
, " peer ");
1105 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
),
1106 "%s", format_host_rta(ifa
->ifa_family
,
1107 rta_tb
[IFA_ADDRESS
]));
1109 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
1115 if (rta_tb
[IFA_BROADCAST
]) {
1116 fprintf(fp
, "brd ");
1117 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s ",
1118 format_host_rta(ifa
->ifa_family
,
1119 rta_tb
[IFA_BROADCAST
]));
1121 if (rta_tb
[IFA_ANYCAST
]) {
1122 fprintf(fp
, "any ");
1123 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s ",
1124 format_host_rta(ifa
->ifa_family
,
1125 rta_tb
[IFA_ANYCAST
]));
1127 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1128 if (ifa_flags
& IFA_F_SECONDARY
) {
1129 ifa_flags
&= ~IFA_F_SECONDARY
;
1130 if (ifa
->ifa_family
== AF_INET6
)
1131 fprintf(fp
, "temporary ");
1133 fprintf(fp
, "secondary ");
1135 if (ifa_flags
& IFA_F_TENTATIVE
) {
1136 ifa_flags
&= ~IFA_F_TENTATIVE
;
1137 fprintf(fp
, "tentative ");
1139 if (ifa_flags
& IFA_F_DEPRECATED
) {
1140 ifa_flags
&= ~IFA_F_DEPRECATED
;
1142 fprintf(fp
, "deprecated ");
1144 if (ifa_flags
& IFA_F_HOMEADDRESS
) {
1145 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
1146 fprintf(fp
, "home ");
1148 if (ifa_flags
& IFA_F_NODAD
) {
1149 ifa_flags
&= ~IFA_F_NODAD
;
1150 fprintf(fp
, "nodad ");
1152 if (ifa_flags
& IFA_F_MANAGETEMPADDR
) {
1153 ifa_flags
&= ~IFA_F_MANAGETEMPADDR
;
1154 fprintf(fp
, "mngtmpaddr ");
1156 if (ifa_flags
& IFA_F_NOPREFIXROUTE
) {
1157 ifa_flags
&= ~IFA_F_NOPREFIXROUTE
;
1158 fprintf(fp
, "noprefixroute ");
1160 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
1161 ifa_flags
&= ~IFA_F_MCAUTOJOIN
;
1162 fprintf(fp
, "autojoin ");
1164 if (!(ifa_flags
& IFA_F_PERMANENT
)) {
1165 fprintf(fp
, "dynamic ");
1167 ifa_flags
&= ~IFA_F_PERMANENT
;
1168 if (ifa_flags
& IFA_F_DADFAILED
) {
1169 ifa_flags
&= ~IFA_F_DADFAILED
;
1170 fprintf(fp
, "dadfailed ");
1173 fprintf(fp
, "flags %02x ", ifa_flags
);
1174 if (rta_tb
[IFA_LABEL
])
1175 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
1176 if (rta_tb
[IFA_CACHEINFO
]) {
1177 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1179 fprintf(fp
, "%s", _SL_
);
1180 fprintf(fp
, " valid_lft ");
1181 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
1182 fprintf(fp
, "forever");
1184 fprintf(fp
, "%usec", ci
->ifa_valid
);
1185 fprintf(fp
, " preferred_lft ");
1186 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
1187 fprintf(fp
, "forever");
1190 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
1192 fprintf(fp
, "%usec", ci
->ifa_prefered
);
1202 struct nlmsg_list
*next
;
1206 struct nlmsg_chain
{
1207 struct nlmsg_list
*head
;
1208 struct nlmsg_list
*tail
;
1211 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1212 struct nlmsg_list
*ainfo
, FILE *fp
)
1214 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1215 struct nlmsghdr
*n
= &ainfo
->h
;
1216 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1218 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1221 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
1224 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1225 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1228 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1231 print_addrinfo(NULL
, n
, fp
);
1241 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1244 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1245 struct nlmsg_list
*h
;
1247 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1251 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1255 lchain
->tail
->next
= h
;
1260 ll_remember_index(who
, n
, NULL
);
1264 static __u32 ipadd_dump_magic
= 0x47361222;
1266 static int ipadd_save_prep(void)
1270 if (isatty(STDOUT_FILENO
)) {
1271 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1275 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1276 if (ret
!= sizeof(ipadd_dump_magic
)) {
1277 fprintf(stderr
, "Can't write magic to dump file\n");
1284 static int ipadd_dump_check_magic(void)
1289 if (isatty(STDIN_FILENO
)) {
1290 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1294 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1295 if (magic
!= ipadd_dump_magic
) {
1296 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1303 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1308 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1309 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1310 fprintf(stderr
, "Short write while saving nlmsg\n");
1314 return ret
== n
->nlmsg_len
? 0 : ret
;
1317 static int show_handler(const struct sockaddr_nl
*nl
,
1318 struct rtnl_ctrl_data
*ctrl
,
1319 struct nlmsghdr
*n
, void *arg
)
1321 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1323 printf("if%d:\n", ifa
->ifa_index
);
1324 print_addrinfo(NULL
, n
, stdout
);
1328 static int ipaddr_showdump(void)
1330 if (ipadd_dump_check_magic())
1333 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1336 static int restore_handler(const struct sockaddr_nl
*nl
,
1337 struct rtnl_ctrl_data
*ctrl
,
1338 struct nlmsghdr
*n
, void *arg
)
1342 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1346 ret
= rtnl_talk(&rth
, n
, n
, sizeof(*n
));
1347 if ((ret
< 0) && (errno
== EEXIST
))
1353 static int ipaddr_restore(void)
1355 if (ipadd_dump_check_magic())
1358 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1361 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1363 struct nlmsg_list
*l
, *n
;
1365 for (l
= info
->head
; l
; l
= n
) {
1371 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1373 struct nlmsg_list
*l
, **lp
;
1376 while ((l
= *lp
) != NULL
) {
1378 int missing_net_address
= 1;
1379 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1380 struct nlmsg_list
*a
;
1382 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1383 struct nlmsghdr
*n
= &a
->h
;
1384 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1385 struct rtattr
*tb
[IFA_MAX
+ 1];
1386 unsigned int ifa_flags
;
1388 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1390 missing_net_address
= 0;
1391 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1393 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1396 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1397 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1399 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1401 if (filter
.pfx
.family
|| filter
.label
) {
1403 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1405 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
1407 .family
= ifa
->ifa_family
1410 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
1411 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1419 label
= RTA_DATA(tb
[IFA_LABEL
]);
1421 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1422 if (fnmatch(filter
.label
, label
, 0) != 0)
1430 if (missing_net_address
&&
1431 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1441 static int ipaddr_flush(void)
1444 char flushb
[4096-512];
1446 filter
.flushb
= flushb
;
1448 filter
.flushe
= sizeof(flushb
);
1450 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1451 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1452 perror("Cannot send dump request");
1456 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1457 stdout
, NLM_F_DUMP_INTR
) < 0) {
1458 fprintf(stderr
, "Flush terminated\n");
1461 if (filter
.flushed
== 0) {
1465 printf("Nothing to flush.\n");
1467 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1473 if (flush_update() < 0)
1477 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1481 /* If we are flushing, and specifying primary, then we
1482 * want to flush only a single round. Otherwise, we'll
1483 * start flushing secondaries that were promoted to
1486 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1489 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1494 static int iplink_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
1498 err
= addattr32(nlh
, reqlen
, IFLA_EXT_MASK
, RTEXT_FILTER_VF
);
1502 if (filter
.master
) {
1503 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter
.master
);
1509 struct rtattr
*linkinfo
;
1511 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
1513 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, filter
.kind
,
1514 strlen(filter
.kind
));
1518 addattr_nest_end(nlh
, linkinfo
);
1524 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1526 struct nlmsg_chain linfo
= { NULL
, NULL
};
1527 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1528 struct nlmsg_list
*l
;
1529 char *filter_dev
= NULL
;
1532 ipaddr_reset_filter(oneline
, 0);
1533 filter
.showqueue
= 1;
1534 filter
.family
= preferred_family
;
1537 if (action
== IPADD_FLUSH
) {
1539 fprintf(stderr
, "Flush requires arguments.\n");
1543 if (filter
.family
== AF_PACKET
) {
1544 fprintf(stderr
, "Cannot flush link addresses.\n");
1550 if (strcmp(*argv
, "to") == 0) {
1552 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1553 if (filter
.family
== AF_UNSPEC
)
1554 filter
.family
= filter
.pfx
.family
;
1555 } else if (strcmp(*argv
, "scope") == 0) {
1556 unsigned int scope
= 0;
1559 filter
.scopemask
= -1;
1560 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1561 if (strcmp(*argv
, "all") != 0)
1562 invarg("invalid \"scope\"\n", *argv
);
1563 scope
= RT_SCOPE_NOWHERE
;
1564 filter
.scopemask
= 0;
1566 filter
.scope
= scope
;
1567 } else if (strcmp(*argv
, "up") == 0) {
1569 } else if (strcmp(*argv
, "dynamic") == 0) {
1570 filter
.flags
&= ~IFA_F_PERMANENT
;
1571 filter
.flagmask
|= IFA_F_PERMANENT
;
1572 } else if (strcmp(*argv
, "permanent") == 0) {
1573 filter
.flags
|= IFA_F_PERMANENT
;
1574 filter
.flagmask
|= IFA_F_PERMANENT
;
1575 } else if (strcmp(*argv
, "secondary") == 0 ||
1576 strcmp(*argv
, "temporary") == 0) {
1577 filter
.flags
|= IFA_F_SECONDARY
;
1578 filter
.flagmask
|= IFA_F_SECONDARY
;
1579 } else if (strcmp(*argv
, "primary") == 0) {
1580 filter
.flags
&= ~IFA_F_SECONDARY
;
1581 filter
.flagmask
|= IFA_F_SECONDARY
;
1582 } else if (strcmp(*argv
, "tentative") == 0) {
1583 filter
.flags
|= IFA_F_TENTATIVE
;
1584 filter
.flagmask
|= IFA_F_TENTATIVE
;
1585 } else if (strcmp(*argv
, "-tentative") == 0) {
1586 filter
.flags
&= ~IFA_F_TENTATIVE
;
1587 filter
.flagmask
|= IFA_F_TENTATIVE
;
1588 } else if (strcmp(*argv
, "deprecated") == 0) {
1589 filter
.flags
|= IFA_F_DEPRECATED
;
1590 filter
.flagmask
|= IFA_F_DEPRECATED
;
1591 } else if (strcmp(*argv
, "-deprecated") == 0) {
1592 filter
.flags
&= ~IFA_F_DEPRECATED
;
1593 filter
.flagmask
|= IFA_F_DEPRECATED
;
1594 } else if (strcmp(*argv
, "home") == 0) {
1595 filter
.flags
|= IFA_F_HOMEADDRESS
;
1596 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1597 } else if (strcmp(*argv
, "nodad") == 0) {
1598 filter
.flags
|= IFA_F_NODAD
;
1599 filter
.flagmask
|= IFA_F_NODAD
;
1600 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1601 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1602 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1603 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1604 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1605 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1606 } else if (strcmp(*argv
, "autojoin") == 0) {
1607 filter
.flags
|= IFA_F_MCAUTOJOIN
;
1608 filter
.flagmask
|= IFA_F_MCAUTOJOIN
;
1609 } else if (strcmp(*argv
, "dadfailed") == 0) {
1610 filter
.flags
|= IFA_F_DADFAILED
;
1611 filter
.flagmask
|= IFA_F_DADFAILED
;
1612 } else if (strcmp(*argv
, "-dadfailed") == 0) {
1613 filter
.flags
&= ~IFA_F_DADFAILED
;
1614 filter
.flagmask
|= IFA_F_DADFAILED
;
1615 } else if (strcmp(*argv
, "label") == 0) {
1617 filter
.label
= *argv
;
1618 } else if (strcmp(*argv
, "group") == 0) {
1620 if (rtnl_group_a2n(&filter
.group
, *argv
))
1621 invarg("Invalid \"group\" value\n", *argv
);
1622 } else if (strcmp(*argv
, "master") == 0) {
1626 ifindex
= ll_name_to_index(*argv
);
1628 invarg("Device does not exist\n", *argv
);
1629 filter
.master
= ifindex
;
1630 } else if (strcmp(*argv
, "vrf") == 0) {
1634 ifindex
= ll_name_to_index(*argv
);
1636 invarg("Not a valid VRF name\n", *argv
);
1637 if (!name_is_vrf(*argv
))
1638 invarg("Not a valid VRF name\n", *argv
);
1639 filter
.master
= ifindex
;
1640 } else if (strcmp(*argv
, "type") == 0) {
1644 soff
= strlen(*argv
) - strlen("_slave");
1645 if (!strcmp(*argv
+ soff
, "_slave")) {
1646 (*argv
)[soff
] = '\0';
1647 filter
.slave_kind
= *argv
;
1649 filter
.kind
= *argv
;
1652 if (strcmp(*argv
, "dev") == 0) {
1654 } else if (matches(*argv
, "help") == 0)
1657 duparg2("dev", *argv
);
1664 filter
.ifindex
= ll_name_to_index(filter_dev
);
1665 if (filter
.ifindex
<= 0) {
1666 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1671 if (action
== IPADD_FLUSH
)
1672 return ipaddr_flush();
1674 if (action
== IPADD_SAVE
) {
1675 if (ipadd_save_prep())
1678 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1679 perror("Cannot send dump request");
1683 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1684 fprintf(stderr
, "Save terminated\n");
1692 * If only filter_dev present and none of the other
1693 * link filters are present, use RTM_GETLINK to get
1696 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1697 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1698 perror("Cannot send link get request");
1704 if (rtnl_wilddump_req_filter_fn(&rth
, preferred_family
, RTM_GETLINK
,
1705 iplink_filter_req
) < 0) {
1706 perror("Cannot send dump request");
1710 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1711 fprintf(stderr
, "Dump terminated\n");
1715 if (filter
.family
!= AF_PACKET
) {
1719 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1720 perror("Cannot send dump request");
1724 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1725 fprintf(stderr
, "Dump terminated\n");
1729 ipaddr_filter(&linfo
, &ainfo
);
1732 for (l
= linfo
.head
; l
; l
= l
->next
) {
1734 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1737 if (print_linkinfo_brief(NULL
, &l
->h
, stdout
) == 0)
1738 if (filter
.family
!= AF_PACKET
)
1739 print_selected_addrinfo(ifi
,
1742 } else if (no_link
||
1743 (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1744 if (filter
.family
!= AF_PACKET
)
1745 print_selected_addrinfo(ifi
,
1746 ainfo
.head
, stdout
);
1747 if (res
> 0 && !do_link
&& show_stats
)
1748 print_link_stats(stdout
, &l
->h
);
1753 free_nlmsg_chain(&ainfo
);
1754 free_nlmsg_chain(&linfo
);
1760 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1762 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1763 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1764 struct ifla_vf_rate
*vf_rate
;
1767 rem
= RTA_PAYLOAD(vflist
);
1769 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1770 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1771 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1772 if (vf_rate
->vf
== vfnum
) {
1773 *min
= vf_rate
->min_tx_rate
;
1774 *max
= vf_rate
->max_tx_rate
;
1778 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1782 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1784 struct nlmsg_chain linfo
= { NULL
, NULL
};
1785 struct rtattr
*tb
[IFLA_MAX
+1];
1786 struct ifinfomsg
*ifi
;
1787 struct nlmsg_list
*l
;
1791 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1792 perror("Cannot send dump request");
1795 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1796 fprintf(stderr
, "Dump terminated\n");
1799 for (l
= linfo
.head
; l
; l
= l
->next
) {
1801 ifi
= NLMSG_DATA(n
);
1803 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1804 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1807 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1809 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1810 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1816 int ipaddr_list_link(int argc
, char **argv
)
1818 preferred_family
= AF_PACKET
;
1820 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1823 void ipaddr_reset_filter(int oneline
, int ifindex
)
1825 memset(&filter
, 0, sizeof(filter
));
1826 filter
.oneline
= oneline
;
1827 filter
.ifindex
= ifindex
;
1830 static int default_scope(inet_prefix
*lcl
)
1832 if (lcl
->family
== AF_INET
) {
1833 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1834 return RT_SCOPE_HOST
;
1839 static bool ipaddr_is_multicast(inet_prefix
*a
)
1841 if (a
->family
== AF_INET
)
1842 return IN_MULTICAST(ntohl(a
->data
[0]));
1843 else if (a
->family
== AF_INET6
)
1844 return IN6_IS_ADDR_MULTICAST(a
->data
);
1849 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1853 struct ifaddrmsg ifa
;
1856 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
1857 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
1858 .n
.nlmsg_type
= cmd
,
1859 .ifa
.ifa_family
= preferred_family
,
1863 char *lcl_arg
= NULL
;
1864 char *valid_lftp
= NULL
;
1865 char *preferred_lftp
= NULL
;
1873 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1874 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1875 unsigned int ifa_flags
= 0;
1878 if (strcmp(*argv
, "peer") == 0 ||
1879 strcmp(*argv
, "remote") == 0) {
1883 duparg("peer", *argv
);
1884 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1885 peer_len
= peer
.bytelen
;
1886 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1887 req
.ifa
.ifa_family
= peer
.family
;
1888 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1889 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1890 } else if (matches(*argv
, "broadcast") == 0 ||
1891 strcmp(*argv
, "brd") == 0) {
1896 duparg("broadcast", *argv
);
1897 if (strcmp(*argv
, "+") == 0)
1899 else if (strcmp(*argv
, "-") == 0)
1902 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1903 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1904 req
.ifa
.ifa_family
= addr
.family
;
1905 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1906 brd_len
= addr
.bytelen
;
1908 } else if (strcmp(*argv
, "anycast") == 0) {
1913 duparg("anycast", *argv
);
1914 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1915 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1916 req
.ifa
.ifa_family
= addr
.family
;
1917 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1918 any_len
= addr
.bytelen
;
1919 } else if (strcmp(*argv
, "scope") == 0) {
1920 unsigned int scope
= 0;
1923 if (rtnl_rtscope_a2n(&scope
, *argv
))
1924 invarg("invalid scope value.", *argv
);
1925 req
.ifa
.ifa_scope
= scope
;
1927 } else if (strcmp(*argv
, "dev") == 0) {
1930 } else if (strcmp(*argv
, "label") == 0) {
1933 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1934 } else if (matches(*argv
, "valid_lft") == 0) {
1936 duparg("valid_lft", *argv
);
1939 if (set_lifetime(&valid_lft
, *argv
))
1940 invarg("valid_lft value", *argv
);
1941 } else if (matches(*argv
, "preferred_lft") == 0) {
1943 duparg("preferred_lft", *argv
);
1945 preferred_lftp
= *argv
;
1946 if (set_lifetime(&preferred_lft
, *argv
))
1947 invarg("preferred_lft value", *argv
);
1948 } else if (strcmp(*argv
, "home") == 0) {
1949 ifa_flags
|= IFA_F_HOMEADDRESS
;
1950 } else if (strcmp(*argv
, "nodad") == 0) {
1951 ifa_flags
|= IFA_F_NODAD
;
1952 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1953 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1954 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1955 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1956 } else if (strcmp(*argv
, "autojoin") == 0) {
1957 ifa_flags
|= IFA_F_MCAUTOJOIN
;
1959 if (strcmp(*argv
, "local") == 0) {
1962 if (matches(*argv
, "help") == 0)
1965 duparg2("local", *argv
);
1967 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1968 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1969 req
.ifa
.ifa_family
= lcl
.family
;
1970 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1971 local_len
= lcl
.bytelen
;
1975 if (ifa_flags
<= 0xff)
1976 req
.ifa
.ifa_flags
= ifa_flags
;
1978 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1981 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1984 if (l
&& matches(d
, l
) != 0) {
1985 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1989 if (peer_len
== 0 && local_len
) {
1990 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1992 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1993 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1994 " This special behaviour is likely to disappear in further releases,\n" \
1995 " fix your scripts!\n", lcl_arg
, local_len
*8);
1998 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
2001 if (req
.ifa
.ifa_prefixlen
== 0)
2002 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
2004 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
2008 if (req
.ifa
.ifa_family
!= AF_INET
) {
2009 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
2013 if (brd
.bitlen
<= 30) {
2014 for (i
= 31; i
>= brd
.bitlen
; i
--) {
2016 brd
.data
[0] |= htonl(1<<(31-i
));
2018 brd
.data
[0] &= ~htonl(1<<(31-i
));
2020 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
2021 brd_len
= brd
.bytelen
;
2024 if (!scoped
&& cmd
!= RTM_DELADDR
)
2025 req
.ifa
.ifa_scope
= default_scope(&lcl
);
2027 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
2028 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
2032 if (valid_lftp
|| preferred_lftp
) {
2033 struct ifa_cacheinfo cinfo
= {};
2036 fprintf(stderr
, "valid_lft is zero\n");
2039 if (valid_lft
< preferred_lft
) {
2040 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
2044 cinfo
.ifa_prefered
= preferred_lft
;
2045 cinfo
.ifa_valid
= valid_lft
;
2046 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
2050 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
2051 fprintf(stderr
, "autojoin needs multicast address\n");
2055 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
2061 int do_ipaddr(int argc
, char **argv
)
2064 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
2065 if (matches(*argv
, "add") == 0)
2066 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
2067 if (matches(*argv
, "change") == 0 ||
2068 strcmp(*argv
, "chg") == 0)
2069 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2070 if (matches(*argv
, "replace") == 0)
2071 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2072 if (matches(*argv
, "delete") == 0)
2073 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2074 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2075 || matches(*argv
, "lst") == 0)
2076 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2077 if (matches(*argv
, "flush") == 0)
2078 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2079 if (matches(*argv
, "save") == 0)
2080 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2081 if (matches(*argv
, "showdump") == 0)
2082 return ipaddr_showdump();
2083 if (matches(*argv
, "restore") == 0)
2084 return ipaddr_restore();
2085 if (matches(*argv
, "help") == 0)
2087 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);