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>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/sockios.h>
31 #include <linux/net_namespace.h>
36 #include "ip_common.h"
67 static void usage(void) __attribute__((noreturn
));
69 static void usage(void)
74 fprintf(stderr
, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
75 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
76 fprintf(stderr
, " ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
77 fprintf(stderr
, " ip address {show|save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
78 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
79 fprintf(stderr
, " ip address {showdump|restore}\n");
80 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
81 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
82 fprintf(stderr
, " [ label IFNAME ] [ scope SCOPE-ID ]\n");
83 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
84 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
85 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
86 fprintf(stderr
, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
87 fprintf(stderr
, " CONFFLAG-LIST ]\n");
88 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
89 fprintf(stderr
, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
90 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
91 fprintf(stderr
, "LFT := forever | SECONDS\n");
96 static void print_link_flags(FILE *fp
, unsigned flags
, unsigned mdown
)
99 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
100 fprintf(fp
, "NO-CARRIER%s", flags
? "," : "");
101 flags
&= ~IFF_RUNNING
;
102 #define _PF(f) if (flags&IFF_##f) { \
103 flags &= ~IFF_##f ; \
104 fprintf(fp, #f "%s", flags ? "," : ""); }
125 fprintf(fp
, "%x", flags
);
127 fprintf(fp
, ",M-DOWN");
131 static const char *oper_states
[] = {
132 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
133 "TESTING", "DORMANT", "UP"
136 static void print_operstate(FILE *f
, __u8 state
)
138 if (state
>= sizeof(oper_states
)/sizeof(oper_states
[0]))
139 fprintf(f
, "state %#x ", state
);
142 if (strcmp(oper_states
[state
], "UP") == 0)
143 color_fprintf(f
, COLOR_OPERSTATE_UP
, "%-14s ", oper_states
[state
]);
144 else if (strcmp(oper_states
[state
], "DOWN") == 0)
145 color_fprintf(f
, COLOR_OPERSTATE_DOWN
, "%-14s ", oper_states
[state
]);
147 fprintf(f
, "%-14s ", oper_states
[state
]);
149 fprintf(f
, "state ");
150 if (strcmp(oper_states
[state
], "UP") == 0)
151 color_fprintf(f
, COLOR_OPERSTATE_UP
, "%s ", oper_states
[state
]);
152 else if (strcmp(oper_states
[state
], "DOWN") == 0)
153 color_fprintf(f
, COLOR_OPERSTATE_DOWN
, "%s ", oper_states
[state
]);
155 fprintf(f
, "%s ", oper_states
[state
]);
160 int get_operstate(const char *name
)
164 for (i
= 0; i
< sizeof(oper_states
)/sizeof(oper_states
[0]); i
++)
165 if (strcasecmp(name
, oper_states
[i
]) == 0)
170 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
175 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
178 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
183 memset(&ifr
, 0, sizeof(ifr
));
184 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
185 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
186 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
194 fprintf(f
, "qlen %d", qlen
);
197 static const char *link_modes
[] = {
201 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
203 unsigned int mode
= rta_getattr_u8(tb
);
205 if (mode
>= sizeof(link_modes
) / sizeof(link_modes
[0]))
206 fprintf(f
, "mode %d ", mode
);
208 fprintf(f
, "mode %s ", link_modes
[mode
]);
211 static char *parse_link_kind(struct rtattr
*tb
)
213 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
215 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
217 if (linkinfo
[IFLA_INFO_KIND
])
218 return RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
223 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
225 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
226 struct link_util
*lu
;
227 struct link_util
*slave_lu
;
231 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
233 if (linkinfo
[IFLA_INFO_KIND
]) {
234 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
236 fprintf(fp
, "%s", _SL_
);
237 fprintf(fp
, " %s ", kind
);
239 lu
= get_link_kind(kind
);
240 if (lu
&& lu
->print_opt
) {
241 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
243 if (linkinfo
[IFLA_INFO_DATA
]) {
244 parse_rtattr_nested(attr
, lu
->maxattr
,
245 linkinfo
[IFLA_INFO_DATA
]);
248 lu
->print_opt(lu
, fp
, data
);
250 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
252 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
256 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
257 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
259 fprintf(fp
, "%s", _SL_
);
260 fprintf(fp
, " %s_slave ", slave_kind
);
262 slave_lu
= get_link_slave_kind(slave_kind
);
263 if (slave_lu
&& slave_lu
->print_opt
) {
264 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
266 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
267 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
268 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
271 slave_lu
->print_opt(slave_lu
, fp
, data
);
276 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
278 struct rtattr
*inet6_attr
;
279 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
281 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
285 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
287 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
288 switch (rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
])) {
289 case IN6_ADDR_GEN_MODE_EUI64
:
290 fprintf(fp
, "addrgenmode eui64 ");
292 case IN6_ADDR_GEN_MODE_NONE
:
293 fprintf(fp
, "addrgenmode none ");
299 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
301 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
303 struct ifla_vf_mac
*vf_mac
;
304 struct ifla_vf_vlan
*vf_vlan
;
305 struct ifla_vf_tx_rate
*vf_tx_rate
;
306 struct ifla_vf_spoofchk
*vf_spoofchk
;
307 struct ifla_vf_link_state
*vf_linkstate
;
308 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
312 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
313 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
317 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
319 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
320 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
321 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
323 /* Check if the spoof checking vf info type is supported by
326 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
327 vf
[IFLA_VF_TX_RATE
]->rta_len
);
329 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
332 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
335 /* Check if the link state vf info type is supported by
338 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_SPOOFCHK
] +
339 vf
[IFLA_VF_SPOOFCHK
]->rta_len
);
341 if (tmp
->rta_type
!= IFLA_VF_LINK_STATE
)
344 vf_linkstate
= RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
348 fprintf(fp
, "%s vf %d MAC %s", _SL_
, vf_mac
->vf
,
349 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
350 ETH_ALEN
, 0, b1
, sizeof(b1
)));
352 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
354 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
355 if (vf_tx_rate
->rate
)
356 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
358 if (vf
[IFLA_VF_RATE
]) {
359 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
361 if (vf_rate
->max_tx_rate
)
362 fprintf(fp
, ", max_tx_rate %dMbps", vf_rate
->max_tx_rate
);
363 if (vf_rate
->min_tx_rate
)
364 fprintf(fp
, ", min_tx_rate %dMbps", vf_rate
->min_tx_rate
);
367 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
368 if (vf_spoofchk
->setting
)
369 fprintf(fp
, ", spoof checking on");
371 fprintf(fp
, ", spoof checking off");
374 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
375 fprintf(fp
, ", link-state auto");
376 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
377 fprintf(fp
, ", link-state enable");
379 fprintf(fp
, ", link-state disable");
381 if (vf
[IFLA_VF_STATS
] && show_stats
)
382 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
385 static void print_num(FILE *fp
, unsigned width
, uint64_t count
)
387 const char *prefix
= "kMGTPE";
388 const unsigned int base
= use_iec
? 1024 : 1000;
391 uint8_t precision
= 2;
394 if (!human_readable
|| count
< base
) {
395 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
399 /* increase value by a factor of 1000/1024 and print
400 * if result is something a human can read */
403 if (count
/ base
< powi
)
411 /* try to guess a good number of digits for precision */
412 for (; precision
> 0; precision
--) {
414 if (count
/ powi
< powj
)
418 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
419 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
421 fprintf(fp
, "%-*s ", width
, buf
);
424 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
426 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1] = {};
428 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
429 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
433 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfstats
);
436 fprintf(fp
, "%s", _SL_
);
437 fprintf(fp
, " RX: bytes packets mcast bcast %s", _SL_
);
440 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_BYTES
]));
441 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_PACKETS
]));
442 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_MULTICAST
]));
443 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_BROADCAST
]));
446 fprintf(fp
, "%s", _SL_
);
447 fprintf(fp
, " TX: bytes packets %s", _SL_
);
450 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_BYTES
]));
451 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_PACKETS
]));
454 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
455 const struct rtattr
*carrier_changes
)
458 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
459 s
->rx_compressed
? "compressed" : "", _SL_
);
462 print_num(fp
, 10, s
->rx_bytes
);
463 print_num(fp
, 8, s
->rx_packets
);
464 print_num(fp
, 7, s
->rx_errors
);
465 print_num(fp
, 7, s
->rx_dropped
);
466 print_num(fp
, 7, s
->rx_over_errors
);
467 print_num(fp
, 7, s
->multicast
);
468 if (s
->rx_compressed
)
469 print_num(fp
, 7, s
->rx_compressed
);
472 if (show_stats
> 1) {
473 fprintf(fp
, "%s", _SL_
);
474 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
477 print_num(fp
, 8, s
->rx_length_errors
);
478 print_num(fp
, 7, s
->rx_crc_errors
);
479 print_num(fp
, 7, s
->rx_frame_errors
);
480 print_num(fp
, 7, s
->rx_fifo_errors
);
481 print_num(fp
, 7, s
->rx_missed_errors
);
483 fprintf(fp
, "%s", _SL_
);
486 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
487 s
->tx_compressed
? "compressed" : "", _SL_
);
491 print_num(fp
, 10, s
->tx_bytes
);
492 print_num(fp
, 8, s
->tx_packets
);
493 print_num(fp
, 7, s
->tx_errors
);
494 print_num(fp
, 7, s
->tx_dropped
);
495 print_num(fp
, 7, s
->tx_carrier_errors
);
496 print_num(fp
, 7, s
->collisions
);
497 if (s
->tx_compressed
)
498 print_num(fp
, 7, s
->tx_compressed
);
501 if (show_stats
> 1) {
502 fprintf(fp
, "%s", _SL_
);
503 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
505 fprintf(fp
, " transns");
506 fprintf(fp
, "%s", _SL_
);
509 print_num(fp
, 8, s
->tx_aborted_errors
);
510 print_num(fp
, 7, s
->tx_fifo_errors
);
511 print_num(fp
, 7, s
->tx_window_errors
);
512 print_num(fp
, 7, s
->tx_heartbeat_errors
);
514 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
518 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
519 const struct rtattr
*carrier_changes
)
522 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
523 s
->rx_compressed
? "compressed" : "", _SL_
);
527 print_num(fp
, 10, s
->rx_bytes
);
528 print_num(fp
, 8, s
->rx_packets
);
529 print_num(fp
, 7, s
->rx_errors
);
530 print_num(fp
, 7, s
->rx_dropped
);
531 print_num(fp
, 7, s
->rx_over_errors
);
532 print_num(fp
, 7, s
->multicast
);
533 if (s
->rx_compressed
)
534 print_num(fp
, 7, s
->rx_compressed
);
537 if (show_stats
> 1) {
538 fprintf(fp
, "%s", _SL_
);
539 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
541 print_num(fp
, 8, s
->rx_length_errors
);
542 print_num(fp
, 7, s
->rx_crc_errors
);
543 print_num(fp
, 7, s
->rx_frame_errors
);
544 print_num(fp
, 7, s
->rx_fifo_errors
);
545 print_num(fp
, 7, s
->rx_missed_errors
);
547 fprintf(fp
, "%s", _SL_
);
550 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
551 s
->tx_compressed
? "compressed" : "", _SL_
);
554 print_num(fp
, 10, s
->tx_bytes
);
555 print_num(fp
, 8, s
->tx_packets
);
556 print_num(fp
, 7, s
->tx_errors
);
557 print_num(fp
, 7, s
->tx_dropped
);
558 print_num(fp
, 7, s
->tx_carrier_errors
);
559 print_num(fp
, 7, s
->collisions
);
560 if (s
->tx_compressed
)
561 print_num(fp
, 7, s
->tx_compressed
);
564 if (show_stats
> 1) {
565 fprintf(fp
, "%s", _SL_
);
566 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
568 fprintf(fp
, " transns");
569 fprintf(fp
, "%s", _SL_
);
572 print_num(fp
, 8, s
->tx_aborted_errors
);
573 print_num(fp
, 7, s
->tx_fifo_errors
);
574 print_num(fp
, 7, s
->tx_window_errors
);
575 print_num(fp
, 7, s
->tx_heartbeat_errors
);
577 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
581 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
583 if (tb
[IFLA_STATS64
])
584 print_link_stats64(fp
, RTA_DATA(tb
[IFLA_STATS64
]),
585 tb
[IFLA_CARRIER_CHANGES
]);
586 else if (tb
[IFLA_STATS
])
587 print_link_stats32(fp
, RTA_DATA(tb
[IFLA_STATS
]),
588 tb
[IFLA_CARRIER_CHANGES
]);
591 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
593 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
594 struct rtattr
* tb
[IFLA_MAX
+1];
596 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
597 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
598 __print_link_stats(fp
, tb
);
599 fprintf(fp
, "%s", _SL_
);
602 int print_linkinfo_brief(const struct sockaddr_nl
*who
,
603 struct nlmsghdr
*n
, void *arg
)
605 FILE *fp
= (FILE*)arg
;
606 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
607 struct rtattr
* tb
[IFLA_MAX
+1];
608 int len
= n
->nlmsg_len
;
610 char buf
[32] = { 0, };
613 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
616 len
-= NLMSG_LENGTH(sizeof(*ifi
));
620 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
622 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
625 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
626 if (tb
[IFLA_IFNAME
] == NULL
) {
627 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
630 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
631 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
634 if (tb
[IFLA_GROUP
]) {
635 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
636 if (filter
.group
!= -1 && group
!= filter
.group
)
640 if (tb
[IFLA_MASTER
]) {
641 int master
= *(int*)RTA_DATA(tb
[IFLA_MASTER
]);
642 if (filter
.master
> 0 && master
!= filter
.master
)
645 else if (filter
.master
> 0)
649 if (tb
[IFLA_LINKINFO
]) {
650 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
652 if (strcmp(kind
, filter
.kind
))
659 if (n
->nlmsg_type
== RTM_DELLINK
)
660 fprintf(fp
, "Deleted ");
662 name
= (char *)(tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
666 int iflink
= *(int*)RTA_DATA(tb
[IFLA_LINK
]);
668 snprintf(buf
, sizeof(buf
), "%s@NONE", name
);
670 snprintf(buf
, sizeof(buf
),
671 "%s@%s", name
, ll_idx_n2a(iflink
, b1
));
672 m_flag
= ll_index_to_flags(iflink
);
673 m_flag
= !(m_flag
& IFF_UP
);
676 snprintf(buf
, sizeof(buf
), "%s", name
);
678 fprintf(fp
, "%-16s ", buf
);
680 if (tb
[IFLA_OPERSTATE
])
681 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
683 if (filter
.family
== AF_PACKET
) {
685 if (tb
[IFLA_ADDRESS
]) {
686 color_fprintf(fp
, COLOR_MAC
, "%s ",
687 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
688 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
694 if (filter
.family
== AF_PACKET
)
695 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
697 if (filter
.family
== AF_PACKET
)
703 int print_linkinfo(const struct sockaddr_nl
*who
,
704 struct nlmsghdr
*n
, void *arg
)
706 FILE *fp
= (FILE*)arg
;
707 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
708 struct rtattr
* tb
[IFLA_MAX
+1];
709 int len
= n
->nlmsg_len
;
712 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
715 len
-= NLMSG_LENGTH(sizeof(*ifi
));
719 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
721 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
724 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
725 if (tb
[IFLA_IFNAME
] == NULL
) {
726 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
729 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
730 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
733 if (tb
[IFLA_GROUP
]) {
734 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
735 if (filter
.group
!= -1 && group
!= filter
.group
)
739 if (tb
[IFLA_MASTER
]) {
740 int master
= *(int*)RTA_DATA(tb
[IFLA_MASTER
]);
741 if (filter
.master
> 0 && master
!= filter
.master
)
744 else if (filter
.master
> 0)
748 if (tb
[IFLA_LINKINFO
]) {
749 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
751 if (strcmp(kind
, filter
.kind
))
758 if (n
->nlmsg_type
== RTM_DELLINK
)
759 fprintf(fp
, "Deleted ");
761 fprintf(fp
, "%d: ", ifi
->ifi_index
);
762 color_fprintf(fp
, COLOR_IFNAME
, "%s",
763 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
767 int iflink
= *(int*)RTA_DATA(tb
[IFLA_LINK
]);
769 fprintf(fp
, "@NONE: ");
771 if (tb
[IFLA_LINK_NETNSID
])
772 fprintf(fp
, "@if%d: ", iflink
);
774 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
775 m_flag
= ll_index_to_flags(iflink
);
776 m_flag
= !(m_flag
& IFF_UP
);
782 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
785 fprintf(fp
, "mtu %u ", *(int*)RTA_DATA(tb
[IFLA_MTU
]));
787 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
788 if (tb
[IFLA_MASTER
]) {
790 fprintf(fp
, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
793 if (tb
[IFLA_PHYS_PORT_ID
]) {
795 fprintf(fp
, "portid %s ",
796 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
797 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
801 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
803 fprintf(fp
, "switchid %s ",
804 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
805 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
809 if (tb
[IFLA_OPERSTATE
])
810 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
812 if (do_link
&& tb
[IFLA_LINKMODE
])
813 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
815 if (tb
[IFLA_GROUP
]) {
817 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
818 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
821 if (filter
.showqueue
)
822 print_queuelen(fp
, tb
);
824 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
826 fprintf(fp
, "%s", _SL_
);
827 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
829 if (tb
[IFLA_ADDRESS
]) {
830 color_fprintf(fp
, COLOR_MAC
, "%s",
831 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
832 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
836 if (tb
[IFLA_BROADCAST
]) {
837 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
838 fprintf(fp
, " peer ");
840 fprintf(fp
, " brd ");
841 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
842 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
848 if (tb
[IFLA_LINK_NETNSID
]) {
849 int id
= *(int*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
852 fprintf(fp
, " link-netnsid %d", id
);
854 fprintf(fp
, " link-netnsid unknown");
857 if (tb
[IFLA_PROTO_DOWN
]) {
858 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
859 fprintf(fp
, " protodown on ");
862 if (tb
[IFLA_PROMISCUITY
] && show_details
)
863 fprintf(fp
, " promiscuity %u ",
864 *(int*)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
866 if (tb
[IFLA_LINKINFO
] && show_details
)
867 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
869 if (do_link
&& tb
[IFLA_AF_SPEC
] && show_details
)
870 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
872 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
873 fprintf(fp
, "%s alias %s", _SL_
,
874 rta_getattr_str(tb
[IFLA_IFALIAS
]));
877 if (do_link
&& show_stats
) {
878 fprintf(fp
, "%s", _SL_
);
879 __print_link_stats(fp
, tb
);
882 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
883 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
884 int rem
= RTA_PAYLOAD(vflist
);
885 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
894 static int flush_update(void)
896 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
897 perror("Failed to send flush request");
904 static int set_lifetime(unsigned int *lifetime
, char *argv
)
906 if (strcmp(argv
, "forever") == 0)
907 *lifetime
= INFINITY_LIFE_TIME
;
908 else if (get_u32(lifetime
, argv
, 0))
914 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
915 struct rtattr
*ifa_flags_attr
)
917 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
921 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
925 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
926 int len
= n
->nlmsg_len
;
928 /* Use local copy of ifa_flags to not interfere with filtering code */
929 unsigned int ifa_flags
;
930 struct rtattr
* rta_tb
[IFA_MAX
+1];
934 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
936 len
-= NLMSG_LENGTH(sizeof(*ifa
));
938 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
942 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
945 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
946 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
948 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
950 if (!rta_tb
[IFA_LOCAL
])
951 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
952 if (!rta_tb
[IFA_ADDRESS
])
953 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
955 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
957 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
959 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
964 if (rta_tb
[IFA_LABEL
])
965 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
967 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
968 if (fnmatch(filter
.label
, label
, 0) != 0)
971 if (filter
.pfx
.family
) {
972 if (rta_tb
[IFA_LOCAL
]) {
974 memset(&dst
, 0, sizeof(dst
));
975 dst
.family
= ifa
->ifa_family
;
976 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
977 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
982 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
987 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
991 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
992 memcpy(fn
, n
, n
->nlmsg_len
);
993 fn
->nlmsg_type
= RTM_DELADDR
;
994 fn
->nlmsg_flags
= NLM_F_REQUEST
;
995 fn
->nlmsg_seq
= ++rth
.seq
;
996 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1002 if (n
->nlmsg_type
== RTM_DELADDR
)
1003 fprintf(fp
, "Deleted ");
1006 if (filter
.oneline
|| filter
.flushb
)
1007 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
1008 if (ifa
->ifa_family
== AF_INET
)
1009 fprintf(fp
, " inet ");
1010 else if (ifa
->ifa_family
== AF_INET6
)
1011 fprintf(fp
, " inet6 ");
1012 else if (ifa
->ifa_family
== AF_DECnet
)
1013 fprintf(fp
, " dnet ");
1014 else if (ifa
->ifa_family
== AF_IPX
)
1015 fprintf(fp
, " ipx ");
1017 fprintf(fp
, " family %d ", ifa
->ifa_family
);
1020 if (rta_tb
[IFA_LOCAL
]) {
1021 if (ifa
->ifa_family
== AF_INET
)
1022 color_fprintf(fp
, COLOR_INET
, "%s", format_host(ifa
->ifa_family
,
1023 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
1024 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1025 abuf
, sizeof(abuf
)));
1026 else if (ifa
->ifa_family
== AF_INET6
)
1027 color_fprintf(fp
, COLOR_INET6
, "%s", format_host(ifa
->ifa_family
,
1028 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
1029 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1030 abuf
, sizeof(abuf
)));
1032 fprintf(fp
, "%s", format_host(ifa
->ifa_family
,
1033 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
1034 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1035 abuf
, sizeof(abuf
)));
1037 if (rta_tb
[IFA_ADDRESS
] == NULL
||
1038 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]), RTA_DATA(rta_tb
[IFA_LOCAL
]),
1039 ifa
->ifa_family
== AF_INET
? 4 : 16) == 0) {
1040 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
1042 fprintf(fp
, " peer %s/%d ",
1043 format_host(ifa
->ifa_family
,
1044 RTA_PAYLOAD(rta_tb
[IFA_ADDRESS
]),
1045 RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1046 abuf
, sizeof(abuf
)),
1047 ifa
->ifa_prefixlen
);
1054 if (rta_tb
[IFA_BROADCAST
]) {
1055 fprintf(fp
, "brd %s ",
1056 format_host(ifa
->ifa_family
,
1057 RTA_PAYLOAD(rta_tb
[IFA_BROADCAST
]),
1058 RTA_DATA(rta_tb
[IFA_BROADCAST
]),
1059 abuf
, sizeof(abuf
)));
1061 if (rta_tb
[IFA_ANYCAST
]) {
1062 fprintf(fp
, "any %s ",
1063 format_host(ifa
->ifa_family
,
1064 RTA_PAYLOAD(rta_tb
[IFA_ANYCAST
]),
1065 RTA_DATA(rta_tb
[IFA_ANYCAST
]),
1066 abuf
, sizeof(abuf
)));
1068 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1069 if (ifa_flags
& IFA_F_SECONDARY
) {
1070 ifa_flags
&= ~IFA_F_SECONDARY
;
1071 if (ifa
->ifa_family
== AF_INET6
)
1072 fprintf(fp
, "temporary ");
1074 fprintf(fp
, "secondary ");
1076 if (ifa_flags
& IFA_F_TENTATIVE
) {
1077 ifa_flags
&= ~IFA_F_TENTATIVE
;
1078 fprintf(fp
, "tentative ");
1080 if (ifa_flags
& IFA_F_DEPRECATED
) {
1081 ifa_flags
&= ~IFA_F_DEPRECATED
;
1083 fprintf(fp
, "deprecated ");
1085 if (ifa_flags
& IFA_F_HOMEADDRESS
) {
1086 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
1087 fprintf(fp
, "home ");
1089 if (ifa_flags
& IFA_F_NODAD
) {
1090 ifa_flags
&= ~IFA_F_NODAD
;
1091 fprintf(fp
, "nodad ");
1093 if (ifa_flags
& IFA_F_MANAGETEMPADDR
) {
1094 ifa_flags
&= ~IFA_F_MANAGETEMPADDR
;
1095 fprintf(fp
, "mngtmpaddr ");
1097 if (ifa_flags
& IFA_F_NOPREFIXROUTE
) {
1098 ifa_flags
&= ~IFA_F_NOPREFIXROUTE
;
1099 fprintf(fp
, "noprefixroute ");
1101 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
1102 ifa_flags
&= ~IFA_F_MCAUTOJOIN
;
1103 fprintf(fp
, "autojoin ");
1105 if (!(ifa_flags
& IFA_F_PERMANENT
)) {
1106 fprintf(fp
, "dynamic ");
1108 ifa_flags
&= ~IFA_F_PERMANENT
;
1109 if (ifa_flags
& IFA_F_DADFAILED
) {
1110 ifa_flags
&= ~IFA_F_DADFAILED
;
1111 fprintf(fp
, "dadfailed ");
1114 fprintf(fp
, "flags %02x ", ifa_flags
);
1115 if (rta_tb
[IFA_LABEL
])
1116 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
1117 if (rta_tb
[IFA_CACHEINFO
]) {
1118 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1119 fprintf(fp
, "%s", _SL_
);
1120 fprintf(fp
, " valid_lft ");
1121 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
1122 fprintf(fp
, "forever");
1124 fprintf(fp
, "%usec", ci
->ifa_valid
);
1125 fprintf(fp
, " preferred_lft ");
1126 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
1127 fprintf(fp
, "forever");
1130 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
1132 fprintf(fp
, "%usec", ci
->ifa_prefered
);
1141 static int print_addrinfo_primary(const struct sockaddr_nl
*who
,
1142 struct nlmsghdr
*n
, void *arg
)
1144 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1146 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
1149 return print_addrinfo(who
, n
, arg
);
1152 static int print_addrinfo_secondary(const struct sockaddr_nl
*who
,
1153 struct nlmsghdr
*n
, void *arg
)
1155 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1157 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
1160 return print_addrinfo(who
, n
, arg
);
1165 struct nlmsg_list
*next
;
1171 struct nlmsg_list
*head
;
1172 struct nlmsg_list
*tail
;
1175 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1176 struct nlmsg_list
*ainfo
, FILE *fp
)
1178 for ( ;ainfo
; ainfo
= ainfo
->next
) {
1179 struct nlmsghdr
*n
= &ainfo
->h
;
1180 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1182 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1185 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
1188 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1189 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1192 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1195 print_addrinfo(NULL
, n
, fp
);
1205 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1208 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1209 struct nlmsg_list
*h
;
1211 h
= malloc(n
->nlmsg_len
+sizeof(void*));
1215 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1219 lchain
->tail
->next
= h
;
1224 ll_remember_index(who
, n
, NULL
);
1228 static __u32 ipadd_dump_magic
= 0x47361222;
1230 static int ipadd_save_prep(void)
1234 if (isatty(STDOUT_FILENO
)) {
1235 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1239 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1240 if (ret
!= sizeof(ipadd_dump_magic
)) {
1241 fprintf(stderr
, "Can't write magic to dump file\n");
1248 static int ipadd_dump_check_magic(void)
1253 if (isatty(STDIN_FILENO
)) {
1254 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1258 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1259 if (magic
!= ipadd_dump_magic
) {
1260 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1267 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1272 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1273 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1274 fprintf(stderr
, "Short write while saving nlmsg\n");
1278 return ret
== n
->nlmsg_len
? 0 : ret
;
1281 static int show_handler(const struct sockaddr_nl
*nl
,
1282 struct rtnl_ctrl_data
*ctrl
,
1283 struct nlmsghdr
*n
, void *arg
)
1285 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1287 printf("if%d:\n", ifa
->ifa_index
);
1288 print_addrinfo(NULL
, n
, stdout
);
1292 static int ipaddr_showdump(void)
1294 if (ipadd_dump_check_magic())
1297 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1300 static int restore_handler(const struct sockaddr_nl
*nl
,
1301 struct rtnl_ctrl_data
*ctrl
,
1302 struct nlmsghdr
*n
, void *arg
)
1306 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1310 ret
= rtnl_talk(&rth
, n
, n
, sizeof(*n
));
1311 if ((ret
< 0) && (errno
== EEXIST
))
1317 static int ipaddr_restore(void)
1319 if (ipadd_dump_check_magic())
1322 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1325 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1327 struct nlmsg_list
*l
, *n
;
1329 for (l
= info
->head
; l
; l
= n
) {
1335 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1337 struct nlmsg_list
*l
, **lp
;
1340 while ( (l
= *lp
) != NULL
) {
1342 int missing_net_address
= 1;
1343 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1344 struct nlmsg_list
*a
;
1346 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1347 struct nlmsghdr
*n
= &a
->h
;
1348 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1349 struct rtattr
*tb
[IFA_MAX
+ 1];
1350 unsigned int ifa_flags
;
1352 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1354 missing_net_address
= 0;
1355 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1357 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1360 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1361 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1363 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1365 if (filter
.pfx
.family
|| filter
.label
) {
1367 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1369 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
1371 memset(&dst
, 0, sizeof(dst
));
1372 dst
.family
= ifa
->ifa_family
;
1373 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
1374 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1381 label
= RTA_DATA(tb
[IFA_LABEL
]);
1383 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1384 if (fnmatch(filter
.label
, label
, 0) != 0)
1392 if (missing_net_address
&&
1393 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1403 static int ipaddr_flush(void)
1406 char flushb
[4096-512];
1408 filter
.flushb
= flushb
;
1410 filter
.flushe
= sizeof(flushb
);
1412 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1413 const struct rtnl_dump_filter_arg a
[3] = {
1415 .filter
= print_addrinfo_secondary
,
1419 .filter
= print_addrinfo_primary
,
1427 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1428 perror("Cannot send dump request");
1432 if (rtnl_dump_filter_l(&rth
, a
) < 0) {
1433 fprintf(stderr
, "Flush terminated\n");
1436 if (filter
.flushed
== 0) {
1440 printf("Nothing to flush.\n");
1442 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
1448 if (flush_update() < 0)
1452 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1456 /* If we are flushing, and specifying primary, then we
1457 * want to flush only a single round. Otherwise, we'll
1458 * start flushing secondaries that were promoted to
1461 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1464 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1469 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1471 struct nlmsg_chain linfo
= { NULL
, NULL
};
1472 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1473 struct nlmsg_list
*l
;
1474 char *filter_dev
= NULL
;
1477 ipaddr_reset_filter(oneline
, 0);
1478 filter
.showqueue
= 1;
1480 if (filter
.family
== AF_UNSPEC
)
1481 filter
.family
= preferred_family
;
1485 if (action
== IPADD_FLUSH
) {
1487 fprintf(stderr
, "Flush requires arguments.\n");
1491 if (filter
.family
== AF_PACKET
) {
1492 fprintf(stderr
, "Cannot flush link addresses.\n");
1498 if (strcmp(*argv
, "to") == 0) {
1500 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1501 if (filter
.family
== AF_UNSPEC
)
1502 filter
.family
= filter
.pfx
.family
;
1503 } else if (strcmp(*argv
, "scope") == 0) {
1506 filter
.scopemask
= -1;
1507 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1508 if (strcmp(*argv
, "all") != 0)
1509 invarg("invalid \"scope\"\n", *argv
);
1510 scope
= RT_SCOPE_NOWHERE
;
1511 filter
.scopemask
= 0;
1513 filter
.scope
= scope
;
1514 } else if (strcmp(*argv
, "up") == 0) {
1516 } else if (strcmp(*argv
, "dynamic") == 0) {
1517 filter
.flags
&= ~IFA_F_PERMANENT
;
1518 filter
.flagmask
|= IFA_F_PERMANENT
;
1519 } else if (strcmp(*argv
, "permanent") == 0) {
1520 filter
.flags
|= IFA_F_PERMANENT
;
1521 filter
.flagmask
|= IFA_F_PERMANENT
;
1522 } else if (strcmp(*argv
, "secondary") == 0 ||
1523 strcmp(*argv
, "temporary") == 0) {
1524 filter
.flags
|= IFA_F_SECONDARY
;
1525 filter
.flagmask
|= IFA_F_SECONDARY
;
1526 } else if (strcmp(*argv
, "primary") == 0) {
1527 filter
.flags
&= ~IFA_F_SECONDARY
;
1528 filter
.flagmask
|= IFA_F_SECONDARY
;
1529 } else if (strcmp(*argv
, "tentative") == 0) {
1530 filter
.flags
|= IFA_F_TENTATIVE
;
1531 filter
.flagmask
|= IFA_F_TENTATIVE
;
1532 } else if (strcmp(*argv
, "-tentative") == 0) {
1533 filter
.flags
&= ~IFA_F_TENTATIVE
;
1534 filter
.flagmask
|= IFA_F_TENTATIVE
;
1535 } else if (strcmp(*argv
, "deprecated") == 0) {
1536 filter
.flags
|= IFA_F_DEPRECATED
;
1537 filter
.flagmask
|= IFA_F_DEPRECATED
;
1538 } else if (strcmp(*argv
, "-deprecated") == 0) {
1539 filter
.flags
&= ~IFA_F_DEPRECATED
;
1540 filter
.flagmask
|= IFA_F_DEPRECATED
;
1541 } else if (strcmp(*argv
, "home") == 0) {
1542 filter
.flags
|= IFA_F_HOMEADDRESS
;
1543 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1544 } else if (strcmp(*argv
, "nodad") == 0) {
1545 filter
.flags
|= IFA_F_NODAD
;
1546 filter
.flagmask
|= IFA_F_NODAD
;
1547 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1548 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1549 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1550 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1551 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1552 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1553 } else if (strcmp(*argv
, "autojoin") == 0) {
1554 filter
.flags
|= IFA_F_MCAUTOJOIN
;
1555 filter
.flagmask
|= IFA_F_MCAUTOJOIN
;
1556 } else if (strcmp(*argv
, "dadfailed") == 0) {
1557 filter
.flags
|= IFA_F_DADFAILED
;
1558 filter
.flagmask
|= IFA_F_DADFAILED
;
1559 } else if (strcmp(*argv
, "-dadfailed") == 0) {
1560 filter
.flags
&= ~IFA_F_DADFAILED
;
1561 filter
.flagmask
|= IFA_F_DADFAILED
;
1562 } else if (strcmp(*argv
, "label") == 0) {
1564 filter
.label
= *argv
;
1565 } else if (strcmp(*argv
, "group") == 0) {
1567 if (rtnl_group_a2n(&filter
.group
, *argv
))
1568 invarg("Invalid \"group\" value\n", *argv
);
1569 } else if (strcmp(*argv
, "master") == 0) {
1572 ifindex
= ll_name_to_index(*argv
);
1574 invarg("Device does not exist\n", *argv
);
1575 filter
.master
= ifindex
;
1576 } else if (do_link
&& strcmp(*argv
, "type") == 0) {
1578 filter
.kind
= *argv
;
1580 if (strcmp(*argv
, "dev") == 0) {
1583 else if (matches(*argv
, "help") == 0)
1586 duparg2("dev", *argv
);
1593 filter
.ifindex
= ll_name_to_index(filter_dev
);
1594 if (filter
.ifindex
<= 0) {
1595 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1600 if (action
== IPADD_FLUSH
)
1601 return ipaddr_flush();
1603 if (action
== IPADD_SAVE
) {
1604 if (ipadd_save_prep())
1607 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1608 perror("Cannot send dump request");
1612 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1613 fprintf(stderr
, "Save terminated\n");
1621 * If only filter_dev present and none of the other
1622 * link filters are present, use RTM_GETLINK to get
1625 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1626 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1627 perror("Cannot send link get request");
1633 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
1634 perror("Cannot send dump request");
1638 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1639 fprintf(stderr
, "Dump terminated\n");
1643 if (filter
.family
!= AF_PACKET
) {
1647 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1648 perror("Cannot send dump request");
1652 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1653 fprintf(stderr
, "Dump terminated\n");
1657 ipaddr_filter(&linfo
, &ainfo
);
1660 for (l
= linfo
.head
; l
; l
= l
->next
) {
1662 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1665 if (print_linkinfo_brief(NULL
, &l
->h
, stdout
) == 0)
1666 if (filter
.family
!= AF_PACKET
)
1667 print_selected_addrinfo(ifi
,
1670 } else if (no_link
||
1671 (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1672 if (filter
.family
!= AF_PACKET
)
1673 print_selected_addrinfo(ifi
,
1674 ainfo
.head
, stdout
);
1675 if (res
> 0 && !do_link
&& show_stats
)
1676 print_link_stats(stdout
, &l
->h
);
1681 free_nlmsg_chain(&ainfo
);
1682 free_nlmsg_chain(&linfo
);
1688 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1690 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1691 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1692 struct ifla_vf_rate
*vf_rate
;
1695 rem
= RTA_PAYLOAD(vflist
);
1697 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1698 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1699 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1700 if (vf_rate
->vf
== vfnum
) {
1701 *min
= vf_rate
->min_tx_rate
;
1702 *max
= vf_rate
->max_tx_rate
;
1706 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1710 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1712 struct nlmsg_chain linfo
= { NULL
, NULL
};
1713 struct rtattr
*tb
[IFLA_MAX
+1];
1714 struct ifinfomsg
*ifi
;
1715 struct nlmsg_list
*l
;
1719 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1720 perror("Cannot send dump request");
1723 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1724 fprintf(stderr
, "Dump terminated\n");
1727 for (l
= linfo
.head
; l
; l
= l
->next
) {
1729 ifi
= NLMSG_DATA(n
);
1731 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1732 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1735 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1737 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1738 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1744 int ipaddr_list_link(int argc
, char **argv
)
1746 preferred_family
= AF_PACKET
;
1748 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1751 void ipaddr_reset_filter(int oneline
, int ifindex
)
1753 memset(&filter
, 0, sizeof(filter
));
1754 filter
.oneline
= oneline
;
1755 filter
.ifindex
= ifindex
;
1758 static int default_scope(inet_prefix
*lcl
)
1760 if (lcl
->family
== AF_INET
) {
1761 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1762 return RT_SCOPE_HOST
;
1767 static bool ipaddr_is_multicast(inet_prefix
*a
)
1769 if (a
->family
== AF_INET
)
1770 return IN_MULTICAST(ntohl(a
->data
[0]));
1771 else if (a
->family
== AF_INET6
)
1772 return IN6_IS_ADDR_MULTICAST(a
->data
);
1777 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1781 struct ifaddrmsg ifa
;
1786 char *lcl_arg
= NULL
;
1787 char *valid_lftp
= NULL
;
1788 char *preferred_lftp
= NULL
;
1796 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1797 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1798 struct ifa_cacheinfo cinfo
;
1799 unsigned int ifa_flags
= 0;
1801 memset(&req
, 0, sizeof(req
));
1803 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1804 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1805 req
.n
.nlmsg_type
= cmd
;
1806 req
.ifa
.ifa_family
= preferred_family
;
1809 if (strcmp(*argv
, "peer") == 0 ||
1810 strcmp(*argv
, "remote") == 0) {
1814 duparg("peer", *argv
);
1815 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1816 peer_len
= peer
.bytelen
;
1817 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1818 req
.ifa
.ifa_family
= peer
.family
;
1819 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1820 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1821 } else if (matches(*argv
, "broadcast") == 0 ||
1822 strcmp(*argv
, "brd") == 0) {
1826 duparg("broadcast", *argv
);
1827 if (strcmp(*argv
, "+") == 0)
1829 else if (strcmp(*argv
, "-") == 0)
1832 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1833 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1834 req
.ifa
.ifa_family
= addr
.family
;
1835 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1836 brd_len
= addr
.bytelen
;
1838 } else if (strcmp(*argv
, "anycast") == 0) {
1842 duparg("anycast", *argv
);
1843 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1844 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1845 req
.ifa
.ifa_family
= addr
.family
;
1846 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1847 any_len
= addr
.bytelen
;
1848 } else if (strcmp(*argv
, "scope") == 0) {
1851 if (rtnl_rtscope_a2n(&scope
, *argv
))
1852 invarg("invalid scope value.", *argv
);
1853 req
.ifa
.ifa_scope
= scope
;
1855 } else if (strcmp(*argv
, "dev") == 0) {
1858 } else if (strcmp(*argv
, "label") == 0) {
1861 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1862 } else if (matches(*argv
, "valid_lft") == 0) {
1864 duparg("valid_lft", *argv
);
1867 if (set_lifetime(&valid_lft
, *argv
))
1868 invarg("valid_lft value", *argv
);
1869 } else if (matches(*argv
, "preferred_lft") == 0) {
1871 duparg("preferred_lft", *argv
);
1873 preferred_lftp
= *argv
;
1874 if (set_lifetime(&preferred_lft
, *argv
))
1875 invarg("preferred_lft value", *argv
);
1876 } else if (strcmp(*argv
, "home") == 0) {
1877 ifa_flags
|= IFA_F_HOMEADDRESS
;
1878 } else if (strcmp(*argv
, "nodad") == 0) {
1879 ifa_flags
|= IFA_F_NODAD
;
1880 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1881 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1882 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1883 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1884 } else if (strcmp(*argv
, "autojoin") == 0) {
1885 ifa_flags
|= IFA_F_MCAUTOJOIN
;
1887 if (strcmp(*argv
, "local") == 0) {
1890 if (matches(*argv
, "help") == 0)
1893 duparg2("local", *argv
);
1895 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1896 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1897 req
.ifa
.ifa_family
= lcl
.family
;
1898 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1899 local_len
= lcl
.bytelen
;
1903 if (ifa_flags
<= 0xff)
1904 req
.ifa
.ifa_flags
= ifa_flags
;
1906 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1909 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1912 if (l
&& matches(d
, l
) != 0) {
1913 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1917 if (peer_len
== 0 && local_len
) {
1918 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1920 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1921 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1922 " This special behaviour is likely to disappear in further releases,\n" \
1923 " fix your scripts!\n", lcl_arg
, local_len
*8);
1926 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1929 if (req
.ifa
.ifa_prefixlen
== 0)
1930 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1932 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1935 if (req
.ifa
.ifa_family
!= AF_INET
) {
1936 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1940 if (brd
.bitlen
<= 30) {
1941 for (i
= 31; i
>= brd
.bitlen
; i
--) {
1943 brd
.data
[0] |= htonl(1<<(31-i
));
1945 brd
.data
[0] &= ~htonl(1<<(31-i
));
1947 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1948 brd_len
= brd
.bytelen
;
1951 if (!scoped
&& cmd
!= RTM_DELADDR
)
1952 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1954 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1955 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1959 if (valid_lftp
|| preferred_lftp
) {
1961 fprintf(stderr
, "valid_lft is zero\n");
1964 if (valid_lft
< preferred_lft
) {
1965 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1969 memset(&cinfo
, 0, sizeof(cinfo
));
1970 cinfo
.ifa_prefered
= preferred_lft
;
1971 cinfo
.ifa_valid
= valid_lft
;
1972 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1976 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
1977 fprintf(stderr
, "autojoin needs multicast address\n");
1981 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
1987 int do_ipaddr(int argc
, char **argv
)
1990 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
1991 if (matches(*argv
, "add") == 0)
1992 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1993 if (matches(*argv
, "change") == 0 ||
1994 strcmp(*argv
, "chg") == 0)
1995 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
1996 if (matches(*argv
, "replace") == 0)
1997 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
1998 if (matches(*argv
, "delete") == 0)
1999 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2000 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2001 || matches(*argv
, "lst") == 0)
2002 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2003 if (matches(*argv
, "flush") == 0)
2004 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2005 if (matches(*argv
, "save") == 0)
2006 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2007 if (matches(*argv
, "showdump") == 0)
2008 return ipaddr_showdump();
2009 if (matches(*argv
, "restore") == 0)
2010 return ipaddr_restore();
2011 if (matches(*argv
, "help") == 0)
2013 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);