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 int flags
, unsigned int 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
>= ARRAY_SIZE(oper_states
)) {
139 fprintf(f
, "state %#x ", state
);
141 color_fprintf(f
, oper_state_color(state
),
142 "%-14s ", oper_states
[state
]);
144 fprintf(f
, "state ");
145 color_fprintf(f
, oper_state_color(state
),
146 "%s ", oper_states
[state
]);
150 int get_operstate(const char *name
)
154 for (i
= 0; i
< ARRAY_SIZE(oper_states
); i
++)
155 if (strcasecmp(name
, oper_states
[i
]) == 0)
160 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
165 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
168 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
173 memset(&ifr
, 0, sizeof(ifr
));
174 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
175 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
176 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
184 fprintf(f
, "qlen %d", qlen
);
187 static const char *link_modes
[] = {
191 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
193 unsigned int mode
= rta_getattr_u8(tb
);
195 if (mode
>= ARRAY_SIZE(link_modes
))
196 fprintf(f
, "mode %d ", mode
);
198 fprintf(f
, "mode %s ", link_modes
[mode
]);
201 static char *parse_link_kind(struct rtattr
*tb
)
203 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
205 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
207 if (linkinfo
[IFLA_INFO_KIND
])
208 return RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
213 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
215 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
216 struct link_util
*lu
;
217 struct link_util
*slave_lu
;
221 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
223 if (linkinfo
[IFLA_INFO_KIND
]) {
224 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
226 fprintf(fp
, "%s", _SL_
);
227 fprintf(fp
, " %s ", kind
);
229 lu
= get_link_kind(kind
);
230 if (lu
&& lu
->print_opt
) {
231 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
233 if (linkinfo
[IFLA_INFO_DATA
]) {
234 parse_rtattr_nested(attr
, lu
->maxattr
,
235 linkinfo
[IFLA_INFO_DATA
]);
238 lu
->print_opt(lu
, fp
, data
);
240 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
242 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
246 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
247 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
249 fprintf(fp
, "%s", _SL_
);
250 fprintf(fp
, " %s_slave ", slave_kind
);
252 slave_lu
= get_link_slave_kind(slave_kind
);
253 if (slave_lu
&& slave_lu
->print_opt
) {
254 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
256 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
257 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
258 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
261 slave_lu
->print_opt(slave_lu
, fp
, data
);
266 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
268 struct rtattr
*inet6_attr
;
269 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
271 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
275 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
277 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
278 __u8 mode
= rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
281 case IN6_ADDR_GEN_MODE_EUI64
:
282 fprintf(fp
, "addrgenmode eui64 ");
284 case IN6_ADDR_GEN_MODE_NONE
:
285 fprintf(fp
, "addrgenmode none ");
287 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
288 fprintf(fp
, "addrgenmode stable_secret ");
290 case IN6_ADDR_GEN_MODE_RANDOM
:
291 fprintf(fp
, "addrgenmode random ");
294 fprintf(fp
, "addrgenmode %#.2hhx ", mode
);
300 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
302 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
304 struct ifla_vf_mac
*vf_mac
;
305 struct ifla_vf_vlan
*vf_vlan
;
306 struct ifla_vf_tx_rate
*vf_tx_rate
;
307 struct ifla_vf_spoofchk
*vf_spoofchk
;
308 struct ifla_vf_link_state
*vf_linkstate
;
309 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
314 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
315 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
319 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
321 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
322 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
323 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
325 /* Check if the spoof checking vf info type is supported by
328 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
329 vf
[IFLA_VF_TX_RATE
]->rta_len
);
331 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
334 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
337 /* Check if the link state vf info type is supported by
340 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_SPOOFCHK
] +
341 vf
[IFLA_VF_SPOOFCHK
]->rta_len
);
343 if (tmp
->rta_type
!= IFLA_VF_LINK_STATE
)
346 vf_linkstate
= RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
350 fprintf(fp
, "%s vf %d MAC %s", _SL_
, vf_mac
->vf
,
351 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
352 ETH_ALEN
, 0, b1
, sizeof(b1
)));
354 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
356 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
357 if (vf_tx_rate
->rate
)
358 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
360 if (vf
[IFLA_VF_RATE
]) {
361 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
363 if (vf_rate
->max_tx_rate
)
364 fprintf(fp
, ", max_tx_rate %dMbps", vf_rate
->max_tx_rate
);
365 if (vf_rate
->min_tx_rate
)
366 fprintf(fp
, ", min_tx_rate %dMbps", vf_rate
->min_tx_rate
);
369 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
370 if (vf_spoofchk
->setting
)
371 fprintf(fp
, ", spoof checking on");
373 fprintf(fp
, ", spoof checking off");
376 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
377 fprintf(fp
, ", link-state auto");
378 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
379 fprintf(fp
, ", link-state enable");
381 fprintf(fp
, ", link-state disable");
383 if (vf
[IFLA_VF_TRUST
]) {
384 struct ifla_vf_trust
*vf_trust
= RTA_DATA(vf
[IFLA_VF_TRUST
]);
386 if (vf_trust
->setting
!= -1)
387 fprintf(fp
, ", trust %s",
388 vf_trust
->setting
? "on" : "off");
390 if (vf
[IFLA_VF_STATS
] && show_stats
)
391 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
394 static void print_num(FILE *fp
, unsigned int width
, uint64_t count
)
396 const char *prefix
= "kMGTPE";
397 const unsigned int base
= use_iec
? 1024 : 1000;
400 uint8_t precision
= 2;
403 if (!human_readable
|| count
< base
) {
404 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
408 /* increase value by a factor of 1000/1024 and print
409 * if result is something a human can read */
412 if (count
/ base
< powi
)
420 /* try to guess a good number of digits for precision */
421 for (; precision
> 0; precision
--) {
423 if (count
/ powi
< powj
)
427 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
428 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
430 fprintf(fp
, "%-*s ", width
, buf
);
433 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
435 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1] = {};
437 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
438 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
442 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfstats
);
445 fprintf(fp
, "%s", _SL_
);
446 fprintf(fp
, " RX: bytes packets mcast bcast %s", _SL_
);
449 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_BYTES
]));
450 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_PACKETS
]));
451 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_MULTICAST
]));
452 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_BROADCAST
]));
455 fprintf(fp
, "%s", _SL_
);
456 fprintf(fp
, " TX: bytes packets %s", _SL_
);
459 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_BYTES
]));
460 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_PACKETS
]));
463 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
464 const struct rtattr
*carrier_changes
)
467 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
468 s
->rx_compressed
? "compressed" : "", _SL_
);
471 print_num(fp
, 10, s
->rx_bytes
);
472 print_num(fp
, 8, s
->rx_packets
);
473 print_num(fp
, 7, s
->rx_errors
);
474 print_num(fp
, 7, s
->rx_dropped
);
475 print_num(fp
, 7, s
->rx_over_errors
);
476 print_num(fp
, 7, s
->multicast
);
477 if (s
->rx_compressed
)
478 print_num(fp
, 7, s
->rx_compressed
);
481 if (show_stats
> 1) {
482 fprintf(fp
, "%s", _SL_
);
483 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
484 s
->rx_nohandler
? " nohandler" : "", _SL_
);
487 print_num(fp
, 8, s
->rx_length_errors
);
488 print_num(fp
, 7, s
->rx_crc_errors
);
489 print_num(fp
, 7, s
->rx_frame_errors
);
490 print_num(fp
, 7, s
->rx_fifo_errors
);
491 print_num(fp
, 7, s
->rx_missed_errors
);
493 print_num(fp
, 7, s
->rx_nohandler
);
496 fprintf(fp
, "%s", _SL_
);
499 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
500 s
->tx_compressed
? "compressed" : "", _SL_
);
503 print_num(fp
, 10, s
->tx_bytes
);
504 print_num(fp
, 8, s
->tx_packets
);
505 print_num(fp
, 7, s
->tx_errors
);
506 print_num(fp
, 7, s
->tx_dropped
);
507 print_num(fp
, 7, s
->tx_carrier_errors
);
508 print_num(fp
, 7, s
->collisions
);
509 if (s
->tx_compressed
)
510 print_num(fp
, 7, s
->tx_compressed
);
513 if (show_stats
> 1) {
514 fprintf(fp
, "%s", _SL_
);
515 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
517 fprintf(fp
, " transns");
518 fprintf(fp
, "%s", _SL_
);
521 print_num(fp
, 8, s
->tx_aborted_errors
);
522 print_num(fp
, 7, s
->tx_fifo_errors
);
523 print_num(fp
, 7, s
->tx_window_errors
);
524 print_num(fp
, 7, s
->tx_heartbeat_errors
);
526 print_num(fp
, 7, *(uint32_t *)RTA_DATA(carrier_changes
));
530 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
531 const struct rtattr
*carrier_changes
)
534 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
535 s
->rx_compressed
? "compressed" : "", _SL_
);
539 print_num(fp
, 10, s
->rx_bytes
);
540 print_num(fp
, 8, s
->rx_packets
);
541 print_num(fp
, 7, s
->rx_errors
);
542 print_num(fp
, 7, s
->rx_dropped
);
543 print_num(fp
, 7, s
->rx_over_errors
);
544 print_num(fp
, 7, s
->multicast
);
545 if (s
->rx_compressed
)
546 print_num(fp
, 7, s
->rx_compressed
);
549 if (show_stats
> 1) {
550 fprintf(fp
, "%s", _SL_
);
551 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
552 s
->rx_nohandler
? " nohandler" : "", _SL_
);
554 print_num(fp
, 8, s
->rx_length_errors
);
555 print_num(fp
, 7, s
->rx_crc_errors
);
556 print_num(fp
, 7, s
->rx_frame_errors
);
557 print_num(fp
, 7, s
->rx_fifo_errors
);
558 print_num(fp
, 7, s
->rx_missed_errors
);
560 print_num(fp
, 7, s
->rx_nohandler
);
562 fprintf(fp
, "%s", _SL_
);
565 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
566 s
->tx_compressed
? "compressed" : "", _SL_
);
569 print_num(fp
, 10, s
->tx_bytes
);
570 print_num(fp
, 8, s
->tx_packets
);
571 print_num(fp
, 7, s
->tx_errors
);
572 print_num(fp
, 7, s
->tx_dropped
);
573 print_num(fp
, 7, s
->tx_carrier_errors
);
574 print_num(fp
, 7, s
->collisions
);
575 if (s
->tx_compressed
)
576 print_num(fp
, 7, s
->tx_compressed
);
579 if (show_stats
> 1) {
580 fprintf(fp
, "%s", _SL_
);
581 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
583 fprintf(fp
, " transns");
584 fprintf(fp
, "%s", _SL_
);
587 print_num(fp
, 8, s
->tx_aborted_errors
);
588 print_num(fp
, 7, s
->tx_fifo_errors
);
589 print_num(fp
, 7, s
->tx_window_errors
);
590 print_num(fp
, 7, s
->tx_heartbeat_errors
);
592 print_num(fp
, 7, *(uint32_t *)RTA_DATA(carrier_changes
));
596 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
598 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
600 if (tb
[IFLA_STATS64
]) {
601 struct rtnl_link_stats64 stats
= { 0 };
603 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS64
]),
604 MIN(RTA_PAYLOAD(tb
[IFLA_STATS64
]), sizeof(stats
)));
606 print_link_stats64(fp
, &stats
, carrier_changes
);
607 } else if (tb
[IFLA_STATS
]) {
608 struct rtnl_link_stats stats
= { 0 };
610 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS
]),
611 MIN(RTA_PAYLOAD(tb
[IFLA_STATS
]), sizeof(stats
)));
613 print_link_stats32(fp
, &stats
, carrier_changes
);
617 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
619 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
620 struct rtattr
*tb
[IFLA_MAX
+1];
622 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
623 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
624 __print_link_stats(fp
, tb
);
625 fprintf(fp
, "%s", _SL_
);
628 int print_linkinfo_brief(const struct sockaddr_nl
*who
,
629 struct nlmsghdr
*n
, void *arg
)
631 FILE *fp
= (FILE *)arg
;
632 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
633 struct rtattr
*tb
[IFLA_MAX
+1];
634 int len
= n
->nlmsg_len
;
636 char buf
[32] = { 0, };
637 unsigned int m_flag
= 0;
639 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
642 len
-= NLMSG_LENGTH(sizeof(*ifi
));
646 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
648 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
651 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
652 if (tb
[IFLA_IFNAME
] == NULL
) {
653 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
656 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
657 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
660 if (tb
[IFLA_GROUP
]) {
661 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
663 if (filter
.group
!= -1 && group
!= filter
.group
)
667 if (tb
[IFLA_MASTER
]) {
668 int master
= *(int *)RTA_DATA(tb
[IFLA_MASTER
]);
670 if (filter
.master
> 0 && master
!= filter
.master
)
672 } else if (filter
.master
> 0)
676 if (tb
[IFLA_LINKINFO
]) {
677 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
679 if (strcmp(kind
, filter
.kind
))
686 if (n
->nlmsg_type
== RTM_DELLINK
)
687 fprintf(fp
, "Deleted ");
689 name
= (char *)(tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
693 int iflink
= *(int *)RTA_DATA(tb
[IFLA_LINK
]);
696 snprintf(buf
, sizeof(buf
), "%s@NONE", name
);
698 snprintf(buf
, sizeof(buf
),
699 "%s@%s", name
, ll_idx_n2a(iflink
, b1
));
700 m_flag
= ll_index_to_flags(iflink
);
701 m_flag
= !(m_flag
& IFF_UP
);
704 snprintf(buf
, sizeof(buf
), "%s", name
);
706 fprintf(fp
, "%-16s ", buf
);
708 if (tb
[IFLA_OPERSTATE
])
709 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
711 if (filter
.family
== AF_PACKET
) {
713 if (tb
[IFLA_ADDRESS
]) {
714 color_fprintf(fp
, COLOR_MAC
, "%s ",
715 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
716 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
722 if (filter
.family
== AF_PACKET
)
723 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
725 if (filter
.family
== AF_PACKET
)
731 int print_linkinfo(const struct sockaddr_nl
*who
,
732 struct nlmsghdr
*n
, void *arg
)
734 FILE *fp
= (FILE *)arg
;
735 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
736 struct rtattr
*tb
[IFLA_MAX
+1];
737 int len
= n
->nlmsg_len
;
738 unsigned int m_flag
= 0;
740 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
743 len
-= NLMSG_LENGTH(sizeof(*ifi
));
747 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
749 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
752 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
753 if (tb
[IFLA_IFNAME
] == NULL
) {
754 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
757 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
758 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
761 if (tb
[IFLA_GROUP
]) {
762 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
764 if (filter
.group
!= -1 && group
!= filter
.group
)
768 if (tb
[IFLA_MASTER
]) {
769 int master
= *(int *)RTA_DATA(tb
[IFLA_MASTER
]);
771 if (filter
.master
> 0 && master
!= filter
.master
)
773 } else if (filter
.master
> 0)
777 if (tb
[IFLA_LINKINFO
]) {
778 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
780 if (strcmp(kind
, filter
.kind
))
787 if (n
->nlmsg_type
== RTM_DELLINK
)
788 fprintf(fp
, "Deleted ");
790 fprintf(fp
, "%d: ", ifi
->ifi_index
);
791 color_fprintf(fp
, COLOR_IFNAME
, "%s",
792 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
796 int iflink
= *(int *)RTA_DATA(tb
[IFLA_LINK
]);
799 fprintf(fp
, "@NONE: ");
801 if (tb
[IFLA_LINK_NETNSID
])
802 fprintf(fp
, "@if%d: ", iflink
);
804 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
805 m_flag
= ll_index_to_flags(iflink
);
806 m_flag
= !(m_flag
& IFF_UP
);
812 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
815 fprintf(fp
, "mtu %u ", *(int *)RTA_DATA(tb
[IFLA_MTU
]));
817 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
818 if (tb
[IFLA_MASTER
]) {
820 fprintf(fp
, "master %s ", ll_idx_n2a(*(int *)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
823 if (tb
[IFLA_OPERSTATE
])
824 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
826 if (do_link
&& tb
[IFLA_LINKMODE
])
827 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
829 if (tb
[IFLA_GROUP
]) {
831 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
833 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
836 if (filter
.showqueue
)
837 print_queuelen(fp
, tb
);
839 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
841 fprintf(fp
, "%s", _SL_
);
842 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
844 if (tb
[IFLA_ADDRESS
]) {
845 color_fprintf(fp
, COLOR_MAC
, "%s",
846 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
847 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
851 if (tb
[IFLA_BROADCAST
]) {
852 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
853 fprintf(fp
, " peer ");
855 fprintf(fp
, " brd ");
856 color_fprintf(fp
, COLOR_MAC
, "%s",
857 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
858 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
864 if (tb
[IFLA_LINK_NETNSID
]) {
865 int id
= *(int *)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
868 fprintf(fp
, " link-netnsid %d", id
);
870 fprintf(fp
, " link-netnsid unknown");
873 if (tb
[IFLA_PROTO_DOWN
]) {
874 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
875 fprintf(fp
, " protodown on ");
879 if (tb
[IFLA_PROMISCUITY
])
880 fprintf(fp
, " promiscuity %u ",
881 *(int *)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
883 if (tb
[IFLA_LINKINFO
])
884 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
886 if (do_link
&& tb
[IFLA_AF_SPEC
])
887 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
889 if (tb
[IFLA_NUM_TX_QUEUES
])
890 fprintf(fp
, "numtxqueues %u ",
891 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
893 if (tb
[IFLA_NUM_RX_QUEUES
])
894 fprintf(fp
, "numrxqueues %u ",
895 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
897 if (tb
[IFLA_PHYS_PORT_NAME
])
898 fprintf(fp
, "portname %s ",
899 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
901 if (tb
[IFLA_PHYS_PORT_ID
]) {
903 fprintf(fp
, "portid %s ",
904 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
905 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
909 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
911 fprintf(fp
, "switchid %s ",
912 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
913 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
919 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
920 fprintf(fp
, "%s alias %s", _SL_
,
921 rta_getattr_str(tb
[IFLA_IFALIAS
]));
924 if (do_link
&& show_stats
) {
925 fprintf(fp
, "%s", _SL_
);
926 __print_link_stats(fp
, tb
);
929 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
930 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
931 int rem
= RTA_PAYLOAD(vflist
);
933 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
942 static int flush_update(void)
946 * Note that the kernel may delete multiple addresses for one
947 * delete request (e.g. if ipv4 address promotion is disabled).
948 * Since a flush operation is really a series of delete requests
949 * its possible that we may request an address delete that has
950 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
951 * errors returned from a flush request
953 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
954 (errno
!= EADDRNOTAVAIL
)) {
955 perror("Failed to send flush request");
962 static int set_lifetime(unsigned int *lifetime
, char *argv
)
964 if (strcmp(argv
, "forever") == 0)
965 *lifetime
= INFINITY_LIFE_TIME
;
966 else if (get_u32(lifetime
, argv
, 0))
972 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
973 struct rtattr
*ifa_flags_attr
)
975 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
979 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
983 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
984 int len
= n
->nlmsg_len
;
986 /* Use local copy of ifa_flags to not interfere with filtering code */
987 unsigned int ifa_flags
;
988 struct rtattr
*rta_tb
[IFA_MAX
+1];
992 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
994 len
-= NLMSG_LENGTH(sizeof(*ifa
));
996 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
1000 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
1003 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
1004 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
1006 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1008 if (!rta_tb
[IFA_LOCAL
])
1009 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1010 if (!rta_tb
[IFA_ADDRESS
])
1011 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1013 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1015 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1017 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1023 if (rta_tb
[IFA_LABEL
])
1024 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
1026 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1027 if (fnmatch(filter
.label
, label
, 0) != 0)
1030 if (filter
.pfx
.family
) {
1031 if (rta_tb
[IFA_LOCAL
]) {
1034 memset(&dst
, 0, sizeof(dst
));
1035 dst
.family
= ifa
->ifa_family
;
1036 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
1037 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1042 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1045 if (filter
.flushb
) {
1046 struct nlmsghdr
*fn
;
1048 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1052 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1053 memcpy(fn
, n
, n
->nlmsg_len
);
1054 fn
->nlmsg_type
= RTM_DELADDR
;
1055 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1056 fn
->nlmsg_seq
= ++rth
.seq
;
1057 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1063 if (n
->nlmsg_type
== RTM_DELADDR
)
1064 fprintf(fp
, "Deleted ");
1067 if (filter
.oneline
|| filter
.flushb
)
1068 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
1069 if (ifa
->ifa_family
== AF_INET
)
1070 fprintf(fp
, " inet ");
1071 else if (ifa
->ifa_family
== AF_INET6
)
1072 fprintf(fp
, " inet6 ");
1073 else if (ifa
->ifa_family
== AF_DECnet
)
1074 fprintf(fp
, " dnet ");
1075 else if (ifa
->ifa_family
== AF_IPX
)
1076 fprintf(fp
, " ipx ");
1078 fprintf(fp
, " family %d ", ifa
->ifa_family
);
1081 if (rta_tb
[IFA_LOCAL
]) {
1082 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s",
1083 format_host_rta(ifa
->ifa_family
,
1084 rta_tb
[IFA_LOCAL
]));
1085 if (rta_tb
[IFA_ADDRESS
] &&
1086 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1087 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1088 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1089 fprintf(fp
, " peer ");
1090 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
),
1091 "%s", format_host_rta(ifa
->ifa_family
,
1092 rta_tb
[IFA_ADDRESS
]));
1094 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
1100 if (rta_tb
[IFA_BROADCAST
]) {
1101 fprintf(fp
, "brd ");
1102 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s ",
1103 format_host_rta(ifa
->ifa_family
,
1104 rta_tb
[IFA_BROADCAST
]));
1106 if (rta_tb
[IFA_ANYCAST
]) {
1107 fprintf(fp
, "any ");
1108 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s ",
1109 format_host_rta(ifa
->ifa_family
,
1110 rta_tb
[IFA_ANYCAST
]));
1112 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1113 if (ifa_flags
& IFA_F_SECONDARY
) {
1114 ifa_flags
&= ~IFA_F_SECONDARY
;
1115 if (ifa
->ifa_family
== AF_INET6
)
1116 fprintf(fp
, "temporary ");
1118 fprintf(fp
, "secondary ");
1120 if (ifa_flags
& IFA_F_TENTATIVE
) {
1121 ifa_flags
&= ~IFA_F_TENTATIVE
;
1122 fprintf(fp
, "tentative ");
1124 if (ifa_flags
& IFA_F_DEPRECATED
) {
1125 ifa_flags
&= ~IFA_F_DEPRECATED
;
1127 fprintf(fp
, "deprecated ");
1129 if (ifa_flags
& IFA_F_HOMEADDRESS
) {
1130 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
1131 fprintf(fp
, "home ");
1133 if (ifa_flags
& IFA_F_NODAD
) {
1134 ifa_flags
&= ~IFA_F_NODAD
;
1135 fprintf(fp
, "nodad ");
1137 if (ifa_flags
& IFA_F_MANAGETEMPADDR
) {
1138 ifa_flags
&= ~IFA_F_MANAGETEMPADDR
;
1139 fprintf(fp
, "mngtmpaddr ");
1141 if (ifa_flags
& IFA_F_NOPREFIXROUTE
) {
1142 ifa_flags
&= ~IFA_F_NOPREFIXROUTE
;
1143 fprintf(fp
, "noprefixroute ");
1145 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
1146 ifa_flags
&= ~IFA_F_MCAUTOJOIN
;
1147 fprintf(fp
, "autojoin ");
1149 if (!(ifa_flags
& IFA_F_PERMANENT
)) {
1150 fprintf(fp
, "dynamic ");
1152 ifa_flags
&= ~IFA_F_PERMANENT
;
1153 if (ifa_flags
& IFA_F_DADFAILED
) {
1154 ifa_flags
&= ~IFA_F_DADFAILED
;
1155 fprintf(fp
, "dadfailed ");
1158 fprintf(fp
, "flags %02x ", ifa_flags
);
1159 if (rta_tb
[IFA_LABEL
])
1160 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
1161 if (rta_tb
[IFA_CACHEINFO
]) {
1162 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1164 fprintf(fp
, "%s", _SL_
);
1165 fprintf(fp
, " valid_lft ");
1166 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
1167 fprintf(fp
, "forever");
1169 fprintf(fp
, "%usec", ci
->ifa_valid
);
1170 fprintf(fp
, " preferred_lft ");
1171 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
1172 fprintf(fp
, "forever");
1175 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
1177 fprintf(fp
, "%usec", ci
->ifa_prefered
);
1187 struct nlmsg_list
*next
;
1191 struct nlmsg_chain
{
1192 struct nlmsg_list
*head
;
1193 struct nlmsg_list
*tail
;
1196 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1197 struct nlmsg_list
*ainfo
, FILE *fp
)
1199 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1200 struct nlmsghdr
*n
= &ainfo
->h
;
1201 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1203 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1206 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
1209 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1210 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1213 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1216 print_addrinfo(NULL
, n
, fp
);
1226 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1229 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1230 struct nlmsg_list
*h
;
1232 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1236 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1240 lchain
->tail
->next
= h
;
1245 ll_remember_index(who
, n
, NULL
);
1249 static __u32 ipadd_dump_magic
= 0x47361222;
1251 static int ipadd_save_prep(void)
1255 if (isatty(STDOUT_FILENO
)) {
1256 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1260 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1261 if (ret
!= sizeof(ipadd_dump_magic
)) {
1262 fprintf(stderr
, "Can't write magic to dump file\n");
1269 static int ipadd_dump_check_magic(void)
1274 if (isatty(STDIN_FILENO
)) {
1275 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1279 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1280 if (magic
!= ipadd_dump_magic
) {
1281 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1288 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1293 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1294 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1295 fprintf(stderr
, "Short write while saving nlmsg\n");
1299 return ret
== n
->nlmsg_len
? 0 : ret
;
1302 static int show_handler(const struct sockaddr_nl
*nl
,
1303 struct rtnl_ctrl_data
*ctrl
,
1304 struct nlmsghdr
*n
, void *arg
)
1306 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1308 printf("if%d:\n", ifa
->ifa_index
);
1309 print_addrinfo(NULL
, n
, stdout
);
1313 static int ipaddr_showdump(void)
1315 if (ipadd_dump_check_magic())
1318 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1321 static int restore_handler(const struct sockaddr_nl
*nl
,
1322 struct rtnl_ctrl_data
*ctrl
,
1323 struct nlmsghdr
*n
, void *arg
)
1327 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1331 ret
= rtnl_talk(&rth
, n
, n
, sizeof(*n
));
1332 if ((ret
< 0) && (errno
== EEXIST
))
1338 static int ipaddr_restore(void)
1340 if (ipadd_dump_check_magic())
1343 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1346 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1348 struct nlmsg_list
*l
, *n
;
1350 for (l
= info
->head
; l
; l
= n
) {
1356 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1358 struct nlmsg_list
*l
, **lp
;
1361 while ((l
= *lp
) != NULL
) {
1363 int missing_net_address
= 1;
1364 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1365 struct nlmsg_list
*a
;
1367 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1368 struct nlmsghdr
*n
= &a
->h
;
1369 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1370 struct rtattr
*tb
[IFA_MAX
+ 1];
1371 unsigned int ifa_flags
;
1373 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1375 missing_net_address
= 0;
1376 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1378 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1381 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1382 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1384 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1386 if (filter
.pfx
.family
|| filter
.label
) {
1388 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1390 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
1393 memset(&dst
, 0, sizeof(dst
));
1394 dst
.family
= ifa
->ifa_family
;
1395 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
1396 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1404 label
= RTA_DATA(tb
[IFA_LABEL
]);
1406 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1407 if (fnmatch(filter
.label
, label
, 0) != 0)
1415 if (missing_net_address
&&
1416 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1426 static int ipaddr_flush(void)
1429 char flushb
[4096-512];
1431 filter
.flushb
= flushb
;
1433 filter
.flushe
= sizeof(flushb
);
1435 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1436 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1437 perror("Cannot send dump request");
1441 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1442 stdout
, NLM_F_DUMP_INTR
) < 0) {
1443 fprintf(stderr
, "Flush terminated\n");
1446 if (filter
.flushed
== 0) {
1450 printf("Nothing to flush.\n");
1452 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1458 if (flush_update() < 0)
1462 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1466 /* If we are flushing, and specifying primary, then we
1467 * want to flush only a single round. Otherwise, we'll
1468 * start flushing secondaries that were promoted to
1471 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1474 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1479 static int iplink_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
1483 err
= addattr32(nlh
, reqlen
, IFLA_EXT_MASK
, RTEXT_FILTER_VF
);
1487 if (filter
.master
) {
1488 err
= addattr32(nlh
, reqlen
, IFLA_MASTER
, filter
.master
);
1494 struct rtattr
*linkinfo
;
1496 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
1498 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, filter
.kind
,
1499 strlen(filter
.kind
));
1503 addattr_nest_end(nlh
, linkinfo
);
1509 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1511 struct nlmsg_chain linfo
= { NULL
, NULL
};
1512 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1513 struct nlmsg_list
*l
;
1514 char *filter_dev
= NULL
;
1517 ipaddr_reset_filter(oneline
, 0);
1518 filter
.showqueue
= 1;
1519 filter
.family
= preferred_family
;
1522 if (action
== IPADD_FLUSH
) {
1524 fprintf(stderr
, "Flush requires arguments.\n");
1528 if (filter
.family
== AF_PACKET
) {
1529 fprintf(stderr
, "Cannot flush link addresses.\n");
1535 if (strcmp(*argv
, "to") == 0) {
1537 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1538 if (filter
.family
== AF_UNSPEC
)
1539 filter
.family
= filter
.pfx
.family
;
1540 } else if (strcmp(*argv
, "scope") == 0) {
1541 unsigned int scope
= 0;
1544 filter
.scopemask
= -1;
1545 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1546 if (strcmp(*argv
, "all") != 0)
1547 invarg("invalid \"scope\"\n", *argv
);
1548 scope
= RT_SCOPE_NOWHERE
;
1549 filter
.scopemask
= 0;
1551 filter
.scope
= scope
;
1552 } else if (strcmp(*argv
, "up") == 0) {
1554 } else if (strcmp(*argv
, "dynamic") == 0) {
1555 filter
.flags
&= ~IFA_F_PERMANENT
;
1556 filter
.flagmask
|= IFA_F_PERMANENT
;
1557 } else if (strcmp(*argv
, "permanent") == 0) {
1558 filter
.flags
|= IFA_F_PERMANENT
;
1559 filter
.flagmask
|= IFA_F_PERMANENT
;
1560 } else if (strcmp(*argv
, "secondary") == 0 ||
1561 strcmp(*argv
, "temporary") == 0) {
1562 filter
.flags
|= IFA_F_SECONDARY
;
1563 filter
.flagmask
|= IFA_F_SECONDARY
;
1564 } else if (strcmp(*argv
, "primary") == 0) {
1565 filter
.flags
&= ~IFA_F_SECONDARY
;
1566 filter
.flagmask
|= IFA_F_SECONDARY
;
1567 } else if (strcmp(*argv
, "tentative") == 0) {
1568 filter
.flags
|= IFA_F_TENTATIVE
;
1569 filter
.flagmask
|= IFA_F_TENTATIVE
;
1570 } else if (strcmp(*argv
, "-tentative") == 0) {
1571 filter
.flags
&= ~IFA_F_TENTATIVE
;
1572 filter
.flagmask
|= IFA_F_TENTATIVE
;
1573 } else if (strcmp(*argv
, "deprecated") == 0) {
1574 filter
.flags
|= IFA_F_DEPRECATED
;
1575 filter
.flagmask
|= IFA_F_DEPRECATED
;
1576 } else if (strcmp(*argv
, "-deprecated") == 0) {
1577 filter
.flags
&= ~IFA_F_DEPRECATED
;
1578 filter
.flagmask
|= IFA_F_DEPRECATED
;
1579 } else if (strcmp(*argv
, "home") == 0) {
1580 filter
.flags
|= IFA_F_HOMEADDRESS
;
1581 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1582 } else if (strcmp(*argv
, "nodad") == 0) {
1583 filter
.flags
|= IFA_F_NODAD
;
1584 filter
.flagmask
|= IFA_F_NODAD
;
1585 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1586 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1587 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1588 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1589 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1590 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1591 } else if (strcmp(*argv
, "autojoin") == 0) {
1592 filter
.flags
|= IFA_F_MCAUTOJOIN
;
1593 filter
.flagmask
|= IFA_F_MCAUTOJOIN
;
1594 } else if (strcmp(*argv
, "dadfailed") == 0) {
1595 filter
.flags
|= IFA_F_DADFAILED
;
1596 filter
.flagmask
|= IFA_F_DADFAILED
;
1597 } else if (strcmp(*argv
, "-dadfailed") == 0) {
1598 filter
.flags
&= ~IFA_F_DADFAILED
;
1599 filter
.flagmask
|= IFA_F_DADFAILED
;
1600 } else if (strcmp(*argv
, "label") == 0) {
1602 filter
.label
= *argv
;
1603 } else if (strcmp(*argv
, "group") == 0) {
1605 if (rtnl_group_a2n(&filter
.group
, *argv
))
1606 invarg("Invalid \"group\" value\n", *argv
);
1607 } else if (strcmp(*argv
, "master") == 0) {
1611 ifindex
= ll_name_to_index(*argv
);
1613 invarg("Device does not exist\n", *argv
);
1614 filter
.master
= ifindex
;
1615 } else if (do_link
&& strcmp(*argv
, "type") == 0) {
1617 filter
.kind
= *argv
;
1619 if (strcmp(*argv
, "dev") == 0) {
1621 } else if (matches(*argv
, "help") == 0)
1624 duparg2("dev", *argv
);
1631 filter
.ifindex
= ll_name_to_index(filter_dev
);
1632 if (filter
.ifindex
<= 0) {
1633 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1638 if (action
== IPADD_FLUSH
)
1639 return ipaddr_flush();
1641 if (action
== IPADD_SAVE
) {
1642 if (ipadd_save_prep())
1645 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1646 perror("Cannot send dump request");
1650 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1651 fprintf(stderr
, "Save terminated\n");
1659 * If only filter_dev present and none of the other
1660 * link filters are present, use RTM_GETLINK to get
1663 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1664 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1665 perror("Cannot send link get request");
1671 if (rtnl_wilddump_req_filter_fn(&rth
, preferred_family
, RTM_GETLINK
,
1672 iplink_filter_req
) < 0) {
1673 perror("Cannot send dump request");
1677 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1678 fprintf(stderr
, "Dump terminated\n");
1682 if (filter
.family
!= AF_PACKET
) {
1686 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1687 perror("Cannot send dump request");
1691 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1692 fprintf(stderr
, "Dump terminated\n");
1696 ipaddr_filter(&linfo
, &ainfo
);
1699 for (l
= linfo
.head
; l
; l
= l
->next
) {
1701 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1704 if (print_linkinfo_brief(NULL
, &l
->h
, stdout
) == 0)
1705 if (filter
.family
!= AF_PACKET
)
1706 print_selected_addrinfo(ifi
,
1709 } else if (no_link
||
1710 (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1711 if (filter
.family
!= AF_PACKET
)
1712 print_selected_addrinfo(ifi
,
1713 ainfo
.head
, stdout
);
1714 if (res
> 0 && !do_link
&& show_stats
)
1715 print_link_stats(stdout
, &l
->h
);
1720 free_nlmsg_chain(&ainfo
);
1721 free_nlmsg_chain(&linfo
);
1727 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1729 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1730 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1731 struct ifla_vf_rate
*vf_rate
;
1734 rem
= RTA_PAYLOAD(vflist
);
1736 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1737 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1738 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1739 if (vf_rate
->vf
== vfnum
) {
1740 *min
= vf_rate
->min_tx_rate
;
1741 *max
= vf_rate
->max_tx_rate
;
1745 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1749 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1751 struct nlmsg_chain linfo
= { NULL
, NULL
};
1752 struct rtattr
*tb
[IFLA_MAX
+1];
1753 struct ifinfomsg
*ifi
;
1754 struct nlmsg_list
*l
;
1758 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1759 perror("Cannot send dump request");
1762 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1763 fprintf(stderr
, "Dump terminated\n");
1766 for (l
= linfo
.head
; l
; l
= l
->next
) {
1768 ifi
= NLMSG_DATA(n
);
1770 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1771 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1774 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1776 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1777 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1783 int ipaddr_list_link(int argc
, char **argv
)
1785 preferred_family
= AF_PACKET
;
1787 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1790 void ipaddr_reset_filter(int oneline
, int ifindex
)
1792 memset(&filter
, 0, sizeof(filter
));
1793 filter
.oneline
= oneline
;
1794 filter
.ifindex
= ifindex
;
1797 static int default_scope(inet_prefix
*lcl
)
1799 if (lcl
->family
== AF_INET
) {
1800 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1801 return RT_SCOPE_HOST
;
1806 static bool ipaddr_is_multicast(inet_prefix
*a
)
1808 if (a
->family
== AF_INET
)
1809 return IN_MULTICAST(ntohl(a
->data
[0]));
1810 else if (a
->family
== AF_INET6
)
1811 return IN6_IS_ADDR_MULTICAST(a
->data
);
1816 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1820 struct ifaddrmsg ifa
;
1825 char *lcl_arg
= NULL
;
1826 char *valid_lftp
= NULL
;
1827 char *preferred_lftp
= NULL
;
1835 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1836 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1837 struct ifa_cacheinfo cinfo
;
1838 unsigned int ifa_flags
= 0;
1840 memset(&req
, 0, sizeof(req
));
1842 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1843 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1844 req
.n
.nlmsg_type
= cmd
;
1845 req
.ifa
.ifa_family
= preferred_family
;
1848 if (strcmp(*argv
, "peer") == 0 ||
1849 strcmp(*argv
, "remote") == 0) {
1853 duparg("peer", *argv
);
1854 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1855 peer_len
= peer
.bytelen
;
1856 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1857 req
.ifa
.ifa_family
= peer
.family
;
1858 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1859 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1860 } else if (matches(*argv
, "broadcast") == 0 ||
1861 strcmp(*argv
, "brd") == 0) {
1866 duparg("broadcast", *argv
);
1867 if (strcmp(*argv
, "+") == 0)
1869 else if (strcmp(*argv
, "-") == 0)
1872 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1873 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1874 req
.ifa
.ifa_family
= addr
.family
;
1875 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1876 brd_len
= addr
.bytelen
;
1878 } else if (strcmp(*argv
, "anycast") == 0) {
1883 duparg("anycast", *argv
);
1884 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1885 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1886 req
.ifa
.ifa_family
= addr
.family
;
1887 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1888 any_len
= addr
.bytelen
;
1889 } else if (strcmp(*argv
, "scope") == 0) {
1890 unsigned int scope
= 0;
1893 if (rtnl_rtscope_a2n(&scope
, *argv
))
1894 invarg("invalid scope value.", *argv
);
1895 req
.ifa
.ifa_scope
= scope
;
1897 } else if (strcmp(*argv
, "dev") == 0) {
1900 } else if (strcmp(*argv
, "label") == 0) {
1903 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1904 } else if (matches(*argv
, "valid_lft") == 0) {
1906 duparg("valid_lft", *argv
);
1909 if (set_lifetime(&valid_lft
, *argv
))
1910 invarg("valid_lft value", *argv
);
1911 } else if (matches(*argv
, "preferred_lft") == 0) {
1913 duparg("preferred_lft", *argv
);
1915 preferred_lftp
= *argv
;
1916 if (set_lifetime(&preferred_lft
, *argv
))
1917 invarg("preferred_lft value", *argv
);
1918 } else if (strcmp(*argv
, "home") == 0) {
1919 ifa_flags
|= IFA_F_HOMEADDRESS
;
1920 } else if (strcmp(*argv
, "nodad") == 0) {
1921 ifa_flags
|= IFA_F_NODAD
;
1922 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1923 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1924 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1925 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1926 } else if (strcmp(*argv
, "autojoin") == 0) {
1927 ifa_flags
|= IFA_F_MCAUTOJOIN
;
1929 if (strcmp(*argv
, "local") == 0) {
1932 if (matches(*argv
, "help") == 0)
1935 duparg2("local", *argv
);
1937 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1938 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1939 req
.ifa
.ifa_family
= lcl
.family
;
1940 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1941 local_len
= lcl
.bytelen
;
1945 if (ifa_flags
<= 0xff)
1946 req
.ifa
.ifa_flags
= ifa_flags
;
1948 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1951 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1954 if (l
&& matches(d
, l
) != 0) {
1955 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1959 if (peer_len
== 0 && local_len
) {
1960 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1962 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1963 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1964 " This special behaviour is likely to disappear in further releases,\n" \
1965 " fix your scripts!\n", lcl_arg
, local_len
*8);
1968 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1971 if (req
.ifa
.ifa_prefixlen
== 0)
1972 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1974 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1978 if (req
.ifa
.ifa_family
!= AF_INET
) {
1979 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1983 if (brd
.bitlen
<= 30) {
1984 for (i
= 31; i
>= brd
.bitlen
; i
--) {
1986 brd
.data
[0] |= htonl(1<<(31-i
));
1988 brd
.data
[0] &= ~htonl(1<<(31-i
));
1990 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1991 brd_len
= brd
.bytelen
;
1994 if (!scoped
&& cmd
!= RTM_DELADDR
)
1995 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1997 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1998 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
2002 if (valid_lftp
|| preferred_lftp
) {
2004 fprintf(stderr
, "valid_lft is zero\n");
2007 if (valid_lft
< preferred_lft
) {
2008 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
2012 memset(&cinfo
, 0, sizeof(cinfo
));
2013 cinfo
.ifa_prefered
= preferred_lft
;
2014 cinfo
.ifa_valid
= valid_lft
;
2015 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
2019 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
2020 fprintf(stderr
, "autojoin needs multicast address\n");
2024 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
2030 int do_ipaddr(int argc
, char **argv
)
2033 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
2034 if (matches(*argv
, "add") == 0)
2035 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
2036 if (matches(*argv
, "change") == 0 ||
2037 strcmp(*argv
, "chg") == 0)
2038 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2039 if (matches(*argv
, "replace") == 0)
2040 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2041 if (matches(*argv
, "delete") == 0)
2042 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2043 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2044 || matches(*argv
, "lst") == 0)
2045 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2046 if (matches(*argv
, "flush") == 0)
2047 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2048 if (matches(*argv
, "save") == 0)
2049 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2050 if (matches(*argv
, "showdump") == 0)
2051 return ipaddr_showdump();
2052 if (matches(*argv
, "restore") == 0)
2053 return ipaddr_restore();
2054 if (matches(*argv
, "help") == 0)
2056 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);