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_STATS
] && show_stats
)
384 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
387 static void print_num(FILE *fp
, unsigned int width
, uint64_t count
)
389 const char *prefix
= "kMGTPE";
390 const unsigned int base
= use_iec
? 1024 : 1000;
393 uint8_t precision
= 2;
396 if (!human_readable
|| count
< base
) {
397 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
401 /* increase value by a factor of 1000/1024 and print
402 * if result is something a human can read */
405 if (count
/ base
< powi
)
413 /* try to guess a good number of digits for precision */
414 for (; precision
> 0; precision
--) {
416 if (count
/ powi
< powj
)
420 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
421 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
423 fprintf(fp
, "%-*s ", width
, buf
);
426 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
428 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1] = {};
430 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
431 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
435 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfstats
);
438 fprintf(fp
, "%s", _SL_
);
439 fprintf(fp
, " RX: bytes packets mcast bcast %s", _SL_
);
442 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_BYTES
]));
443 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_PACKETS
]));
444 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_MULTICAST
]));
445 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_BROADCAST
]));
448 fprintf(fp
, "%s", _SL_
);
449 fprintf(fp
, " TX: bytes packets %s", _SL_
);
452 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_BYTES
]));
453 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_PACKETS
]));
456 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
457 const struct rtattr
*carrier_changes
)
460 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
461 s
->rx_compressed
? "compressed" : "", _SL_
);
464 print_num(fp
, 10, s
->rx_bytes
);
465 print_num(fp
, 8, s
->rx_packets
);
466 print_num(fp
, 7, s
->rx_errors
);
467 print_num(fp
, 7, s
->rx_dropped
);
468 print_num(fp
, 7, s
->rx_over_errors
);
469 print_num(fp
, 7, s
->multicast
);
470 if (s
->rx_compressed
)
471 print_num(fp
, 7, s
->rx_compressed
);
474 if (show_stats
> 1) {
475 fprintf(fp
, "%s", _SL_
);
476 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
477 s
->rx_nohandler
? " nohandler" : "", _SL_
);
480 print_num(fp
, 8, s
->rx_length_errors
);
481 print_num(fp
, 7, s
->rx_crc_errors
);
482 print_num(fp
, 7, s
->rx_frame_errors
);
483 print_num(fp
, 7, s
->rx_fifo_errors
);
484 print_num(fp
, 7, s
->rx_missed_errors
);
486 print_num(fp
, 7, s
->rx_nohandler
);
489 fprintf(fp
, "%s", _SL_
);
492 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
493 s
->tx_compressed
? "compressed" : "", _SL_
);
496 print_num(fp
, 10, s
->tx_bytes
);
497 print_num(fp
, 8, s
->tx_packets
);
498 print_num(fp
, 7, s
->tx_errors
);
499 print_num(fp
, 7, s
->tx_dropped
);
500 print_num(fp
, 7, s
->tx_carrier_errors
);
501 print_num(fp
, 7, s
->collisions
);
502 if (s
->tx_compressed
)
503 print_num(fp
, 7, s
->tx_compressed
);
506 if (show_stats
> 1) {
507 fprintf(fp
, "%s", _SL_
);
508 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
510 fprintf(fp
, " transns");
511 fprintf(fp
, "%s", _SL_
);
514 print_num(fp
, 8, s
->tx_aborted_errors
);
515 print_num(fp
, 7, s
->tx_fifo_errors
);
516 print_num(fp
, 7, s
->tx_window_errors
);
517 print_num(fp
, 7, s
->tx_heartbeat_errors
);
519 print_num(fp
, 7, *(uint32_t *)RTA_DATA(carrier_changes
));
523 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
524 const struct rtattr
*carrier_changes
)
527 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
528 s
->rx_compressed
? "compressed" : "", _SL_
);
532 print_num(fp
, 10, s
->rx_bytes
);
533 print_num(fp
, 8, s
->rx_packets
);
534 print_num(fp
, 7, s
->rx_errors
);
535 print_num(fp
, 7, s
->rx_dropped
);
536 print_num(fp
, 7, s
->rx_over_errors
);
537 print_num(fp
, 7, s
->multicast
);
538 if (s
->rx_compressed
)
539 print_num(fp
, 7, s
->rx_compressed
);
542 if (show_stats
> 1) {
543 fprintf(fp
, "%s", _SL_
);
544 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
545 s
->rx_nohandler
? " nohandler" : "", _SL_
);
547 print_num(fp
, 8, s
->rx_length_errors
);
548 print_num(fp
, 7, s
->rx_crc_errors
);
549 print_num(fp
, 7, s
->rx_frame_errors
);
550 print_num(fp
, 7, s
->rx_fifo_errors
);
551 print_num(fp
, 7, s
->rx_missed_errors
);
553 print_num(fp
, 7, s
->rx_nohandler
);
555 fprintf(fp
, "%s", _SL_
);
558 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
559 s
->tx_compressed
? "compressed" : "", _SL_
);
562 print_num(fp
, 10, s
->tx_bytes
);
563 print_num(fp
, 8, s
->tx_packets
);
564 print_num(fp
, 7, s
->tx_errors
);
565 print_num(fp
, 7, s
->tx_dropped
);
566 print_num(fp
, 7, s
->tx_carrier_errors
);
567 print_num(fp
, 7, s
->collisions
);
568 if (s
->tx_compressed
)
569 print_num(fp
, 7, s
->tx_compressed
);
572 if (show_stats
> 1) {
573 fprintf(fp
, "%s", _SL_
);
574 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
576 fprintf(fp
, " transns");
577 fprintf(fp
, "%s", _SL_
);
580 print_num(fp
, 8, s
->tx_aborted_errors
);
581 print_num(fp
, 7, s
->tx_fifo_errors
);
582 print_num(fp
, 7, s
->tx_window_errors
);
583 print_num(fp
, 7, s
->tx_heartbeat_errors
);
585 print_num(fp
, 7, *(uint32_t *)RTA_DATA(carrier_changes
));
589 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
591 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
593 if (tb
[IFLA_STATS64
]) {
594 struct rtnl_link_stats64 stats
= { 0 };
596 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS64
]),
597 MIN(RTA_PAYLOAD(tb
[IFLA_STATS64
]), sizeof(stats
)));
599 print_link_stats64(fp
, &stats
, carrier_changes
);
600 } else if (tb
[IFLA_STATS
]) {
601 struct rtnl_link_stats stats
= { 0 };
603 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS
]),
604 MIN(RTA_PAYLOAD(tb
[IFLA_STATS
]), sizeof(stats
)));
606 print_link_stats32(fp
, &stats
, carrier_changes
);
610 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
612 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
613 struct rtattr
*tb
[IFLA_MAX
+1];
615 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
616 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
617 __print_link_stats(fp
, tb
);
618 fprintf(fp
, "%s", _SL_
);
621 int print_linkinfo_brief(const struct sockaddr_nl
*who
,
622 struct nlmsghdr
*n
, void *arg
)
624 FILE *fp
= (FILE *)arg
;
625 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
626 struct rtattr
*tb
[IFLA_MAX
+1];
627 int len
= n
->nlmsg_len
;
629 char buf
[32] = { 0, };
630 unsigned int m_flag
= 0;
632 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
635 len
-= NLMSG_LENGTH(sizeof(*ifi
));
639 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
641 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
644 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
645 if (tb
[IFLA_IFNAME
] == NULL
) {
646 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
649 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
650 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
653 if (tb
[IFLA_GROUP
]) {
654 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
656 if (filter
.group
!= -1 && group
!= filter
.group
)
660 if (tb
[IFLA_MASTER
]) {
661 int master
= *(int *)RTA_DATA(tb
[IFLA_MASTER
]);
663 if (filter
.master
> 0 && master
!= filter
.master
)
665 } else if (filter
.master
> 0)
669 if (tb
[IFLA_LINKINFO
]) {
670 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
672 if (strcmp(kind
, filter
.kind
))
679 if (n
->nlmsg_type
== RTM_DELLINK
)
680 fprintf(fp
, "Deleted ");
682 name
= (char *)(tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
686 int iflink
= *(int *)RTA_DATA(tb
[IFLA_LINK
]);
689 snprintf(buf
, sizeof(buf
), "%s@NONE", name
);
691 snprintf(buf
, sizeof(buf
),
692 "%s@%s", name
, ll_idx_n2a(iflink
, b1
));
693 m_flag
= ll_index_to_flags(iflink
);
694 m_flag
= !(m_flag
& IFF_UP
);
697 snprintf(buf
, sizeof(buf
), "%s", name
);
699 fprintf(fp
, "%-16s ", buf
);
701 if (tb
[IFLA_OPERSTATE
])
702 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
704 if (filter
.family
== AF_PACKET
) {
706 if (tb
[IFLA_ADDRESS
]) {
707 color_fprintf(fp
, COLOR_MAC
, "%s ",
708 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
709 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
715 if (filter
.family
== AF_PACKET
)
716 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
718 if (filter
.family
== AF_PACKET
)
724 int print_linkinfo(const struct sockaddr_nl
*who
,
725 struct nlmsghdr
*n
, void *arg
)
727 FILE *fp
= (FILE *)arg
;
728 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
729 struct rtattr
*tb
[IFLA_MAX
+1];
730 int len
= n
->nlmsg_len
;
731 unsigned int m_flag
= 0;
733 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
736 len
-= NLMSG_LENGTH(sizeof(*ifi
));
740 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
742 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
745 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
746 if (tb
[IFLA_IFNAME
] == NULL
) {
747 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
750 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
751 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
754 if (tb
[IFLA_GROUP
]) {
755 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
757 if (filter
.group
!= -1 && group
!= filter
.group
)
761 if (tb
[IFLA_MASTER
]) {
762 int master
= *(int *)RTA_DATA(tb
[IFLA_MASTER
]);
764 if (filter
.master
> 0 && master
!= filter
.master
)
766 } else if (filter
.master
> 0)
770 if (tb
[IFLA_LINKINFO
]) {
771 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
773 if (strcmp(kind
, filter
.kind
))
780 if (n
->nlmsg_type
== RTM_DELLINK
)
781 fprintf(fp
, "Deleted ");
783 fprintf(fp
, "%d: ", ifi
->ifi_index
);
784 color_fprintf(fp
, COLOR_IFNAME
, "%s",
785 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
789 int iflink
= *(int *)RTA_DATA(tb
[IFLA_LINK
]);
792 fprintf(fp
, "@NONE: ");
794 if (tb
[IFLA_LINK_NETNSID
])
795 fprintf(fp
, "@if%d: ", iflink
);
797 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
798 m_flag
= ll_index_to_flags(iflink
);
799 m_flag
= !(m_flag
& IFF_UP
);
805 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
808 fprintf(fp
, "mtu %u ", *(int *)RTA_DATA(tb
[IFLA_MTU
]));
810 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
811 if (tb
[IFLA_MASTER
]) {
813 fprintf(fp
, "master %s ", ll_idx_n2a(*(int *)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
816 if (tb
[IFLA_OPERSTATE
])
817 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
819 if (do_link
&& tb
[IFLA_LINKMODE
])
820 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
822 if (tb
[IFLA_GROUP
]) {
824 int group
= *(int *)RTA_DATA(tb
[IFLA_GROUP
]);
826 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
829 if (filter
.showqueue
)
830 print_queuelen(fp
, tb
);
832 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
834 fprintf(fp
, "%s", _SL_
);
835 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
837 if (tb
[IFLA_ADDRESS
]) {
838 color_fprintf(fp
, COLOR_MAC
, "%s",
839 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
840 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
844 if (tb
[IFLA_BROADCAST
]) {
845 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
846 fprintf(fp
, " peer ");
848 fprintf(fp
, " brd ");
849 color_fprintf(fp
, COLOR_MAC
, "%s",
850 ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
851 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
857 if (tb
[IFLA_LINK_NETNSID
]) {
858 int id
= *(int *)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
861 fprintf(fp
, " link-netnsid %d", id
);
863 fprintf(fp
, " link-netnsid unknown");
866 if (tb
[IFLA_PROTO_DOWN
]) {
867 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
868 fprintf(fp
, " protodown on ");
872 if (tb
[IFLA_PROMISCUITY
])
873 fprintf(fp
, " promiscuity %u ",
874 *(int *)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
876 if (tb
[IFLA_LINKINFO
])
877 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
879 if (do_link
&& tb
[IFLA_AF_SPEC
])
880 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
882 if (tb
[IFLA_NUM_TX_QUEUES
])
883 fprintf(fp
, "numtxqueues %u ",
884 rta_getattr_u32(tb
[IFLA_NUM_TX_QUEUES
]));
886 if (tb
[IFLA_NUM_RX_QUEUES
])
887 fprintf(fp
, "numrxqueues %u ",
888 rta_getattr_u32(tb
[IFLA_NUM_RX_QUEUES
]));
890 if (tb
[IFLA_PHYS_PORT_NAME
])
891 fprintf(fp
, "portname %s ",
892 rta_getattr_str(tb
[IFLA_PHYS_PORT_NAME
]));
894 if (tb
[IFLA_PHYS_PORT_ID
]) {
896 fprintf(fp
, "portid %s ",
897 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
898 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
902 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
904 fprintf(fp
, "switchid %s ",
905 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
906 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
912 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
913 fprintf(fp
, "%s alias %s", _SL_
,
914 rta_getattr_str(tb
[IFLA_IFALIAS
]));
917 if (do_link
&& show_stats
) {
918 fprintf(fp
, "%s", _SL_
);
919 __print_link_stats(fp
, tb
);
922 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
923 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
924 int rem
= RTA_PAYLOAD(vflist
);
926 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
935 static int flush_update(void)
939 * Note that the kernel may delete multiple addresses for one
940 * delete request (e.g. if ipv4 address promotion is disabled).
941 * Since a flush operation is really a series of delete requests
942 * its possible that we may request an address delete that has
943 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
944 * errors returned from a flush request
946 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
947 (errno
!= EADDRNOTAVAIL
)) {
948 perror("Failed to send flush request");
955 static int set_lifetime(unsigned int *lifetime
, char *argv
)
957 if (strcmp(argv
, "forever") == 0)
958 *lifetime
= INFINITY_LIFE_TIME
;
959 else if (get_u32(lifetime
, argv
, 0))
965 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
966 struct rtattr
*ifa_flags_attr
)
968 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
972 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
976 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
977 int len
= n
->nlmsg_len
;
979 /* Use local copy of ifa_flags to not interfere with filtering code */
980 unsigned int ifa_flags
;
981 struct rtattr
*rta_tb
[IFA_MAX
+1];
985 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
987 len
-= NLMSG_LENGTH(sizeof(*ifa
));
989 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
993 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
996 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
997 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
999 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
1001 if (!rta_tb
[IFA_LOCAL
])
1002 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
1003 if (!rta_tb
[IFA_ADDRESS
])
1004 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
1006 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
1008 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1010 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1016 if (rta_tb
[IFA_LABEL
])
1017 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
1019 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1020 if (fnmatch(filter
.label
, label
, 0) != 0)
1023 if (filter
.pfx
.family
) {
1024 if (rta_tb
[IFA_LOCAL
]) {
1027 memset(&dst
, 0, sizeof(dst
));
1028 dst
.family
= ifa
->ifa_family
;
1029 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
1030 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1035 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1038 if (filter
.flushb
) {
1039 struct nlmsghdr
*fn
;
1041 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1045 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1046 memcpy(fn
, n
, n
->nlmsg_len
);
1047 fn
->nlmsg_type
= RTM_DELADDR
;
1048 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1049 fn
->nlmsg_seq
= ++rth
.seq
;
1050 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1056 if (n
->nlmsg_type
== RTM_DELADDR
)
1057 fprintf(fp
, "Deleted ");
1060 if (filter
.oneline
|| filter
.flushb
)
1061 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
1062 if (ifa
->ifa_family
== AF_INET
)
1063 fprintf(fp
, " inet ");
1064 else if (ifa
->ifa_family
== AF_INET6
)
1065 fprintf(fp
, " inet6 ");
1066 else if (ifa
->ifa_family
== AF_DECnet
)
1067 fprintf(fp
, " dnet ");
1068 else if (ifa
->ifa_family
== AF_IPX
)
1069 fprintf(fp
, " ipx ");
1071 fprintf(fp
, " family %d ", ifa
->ifa_family
);
1074 if (rta_tb
[IFA_LOCAL
]) {
1075 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s",
1076 format_host_rta(ifa
->ifa_family
,
1077 rta_tb
[IFA_LOCAL
]));
1078 if (rta_tb
[IFA_ADDRESS
] &&
1079 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1080 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1081 ifa
->ifa_family
== AF_INET
? 4 : 16)) {
1082 fprintf(fp
, " peer ");
1083 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
),
1084 "%s", format_host_rta(ifa
->ifa_family
,
1085 rta_tb
[IFA_ADDRESS
]));
1087 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
1093 if (rta_tb
[IFA_BROADCAST
]) {
1094 fprintf(fp
, "brd ");
1095 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s ",
1096 format_host_rta(ifa
->ifa_family
,
1097 rta_tb
[IFA_BROADCAST
]));
1099 if (rta_tb
[IFA_ANYCAST
]) {
1100 fprintf(fp
, "any ");
1101 color_fprintf(fp
, ifa_family_color(ifa
->ifa_family
), "%s ",
1102 format_host_rta(ifa
->ifa_family
,
1103 rta_tb
[IFA_ANYCAST
]));
1105 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
1106 if (ifa_flags
& IFA_F_SECONDARY
) {
1107 ifa_flags
&= ~IFA_F_SECONDARY
;
1108 if (ifa
->ifa_family
== AF_INET6
)
1109 fprintf(fp
, "temporary ");
1111 fprintf(fp
, "secondary ");
1113 if (ifa_flags
& IFA_F_TENTATIVE
) {
1114 ifa_flags
&= ~IFA_F_TENTATIVE
;
1115 fprintf(fp
, "tentative ");
1117 if (ifa_flags
& IFA_F_DEPRECATED
) {
1118 ifa_flags
&= ~IFA_F_DEPRECATED
;
1120 fprintf(fp
, "deprecated ");
1122 if (ifa_flags
& IFA_F_HOMEADDRESS
) {
1123 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
1124 fprintf(fp
, "home ");
1126 if (ifa_flags
& IFA_F_NODAD
) {
1127 ifa_flags
&= ~IFA_F_NODAD
;
1128 fprintf(fp
, "nodad ");
1130 if (ifa_flags
& IFA_F_MANAGETEMPADDR
) {
1131 ifa_flags
&= ~IFA_F_MANAGETEMPADDR
;
1132 fprintf(fp
, "mngtmpaddr ");
1134 if (ifa_flags
& IFA_F_NOPREFIXROUTE
) {
1135 ifa_flags
&= ~IFA_F_NOPREFIXROUTE
;
1136 fprintf(fp
, "noprefixroute ");
1138 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
1139 ifa_flags
&= ~IFA_F_MCAUTOJOIN
;
1140 fprintf(fp
, "autojoin ");
1142 if (!(ifa_flags
& IFA_F_PERMANENT
)) {
1143 fprintf(fp
, "dynamic ");
1145 ifa_flags
&= ~IFA_F_PERMANENT
;
1146 if (ifa_flags
& IFA_F_DADFAILED
) {
1147 ifa_flags
&= ~IFA_F_DADFAILED
;
1148 fprintf(fp
, "dadfailed ");
1151 fprintf(fp
, "flags %02x ", ifa_flags
);
1152 if (rta_tb
[IFA_LABEL
])
1153 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
1154 if (rta_tb
[IFA_CACHEINFO
]) {
1155 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
1157 fprintf(fp
, "%s", _SL_
);
1158 fprintf(fp
, " valid_lft ");
1159 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
1160 fprintf(fp
, "forever");
1162 fprintf(fp
, "%usec", ci
->ifa_valid
);
1163 fprintf(fp
, " preferred_lft ");
1164 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
1165 fprintf(fp
, "forever");
1168 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
1170 fprintf(fp
, "%usec", ci
->ifa_prefered
);
1180 struct nlmsg_list
*next
;
1184 struct nlmsg_chain
{
1185 struct nlmsg_list
*head
;
1186 struct nlmsg_list
*tail
;
1189 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1190 struct nlmsg_list
*ainfo
, FILE *fp
)
1192 for ( ; ainfo
; ainfo
= ainfo
->next
) {
1193 struct nlmsghdr
*n
= &ainfo
->h
;
1194 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1196 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1199 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
1202 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1203 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1206 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1209 print_addrinfo(NULL
, n
, fp
);
1219 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1222 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1223 struct nlmsg_list
*h
;
1225 h
= malloc(n
->nlmsg_len
+sizeof(void *));
1229 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1233 lchain
->tail
->next
= h
;
1238 ll_remember_index(who
, n
, NULL
);
1242 static __u32 ipadd_dump_magic
= 0x47361222;
1244 static int ipadd_save_prep(void)
1248 if (isatty(STDOUT_FILENO
)) {
1249 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1253 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1254 if (ret
!= sizeof(ipadd_dump_magic
)) {
1255 fprintf(stderr
, "Can't write magic to dump file\n");
1262 static int ipadd_dump_check_magic(void)
1267 if (isatty(STDIN_FILENO
)) {
1268 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1272 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1273 if (magic
!= ipadd_dump_magic
) {
1274 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1281 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1286 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1287 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1288 fprintf(stderr
, "Short write while saving nlmsg\n");
1292 return ret
== n
->nlmsg_len
? 0 : ret
;
1295 static int show_handler(const struct sockaddr_nl
*nl
,
1296 struct rtnl_ctrl_data
*ctrl
,
1297 struct nlmsghdr
*n
, void *arg
)
1299 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1301 printf("if%d:\n", ifa
->ifa_index
);
1302 print_addrinfo(NULL
, n
, stdout
);
1306 static int ipaddr_showdump(void)
1308 if (ipadd_dump_check_magic())
1311 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1314 static int restore_handler(const struct sockaddr_nl
*nl
,
1315 struct rtnl_ctrl_data
*ctrl
,
1316 struct nlmsghdr
*n
, void *arg
)
1320 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1324 ret
= rtnl_talk(&rth
, n
, n
, sizeof(*n
));
1325 if ((ret
< 0) && (errno
== EEXIST
))
1331 static int ipaddr_restore(void)
1333 if (ipadd_dump_check_magic())
1336 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1339 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1341 struct nlmsg_list
*l
, *n
;
1343 for (l
= info
->head
; l
; l
= n
) {
1349 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1351 struct nlmsg_list
*l
, **lp
;
1354 while ((l
= *lp
) != NULL
) {
1356 int missing_net_address
= 1;
1357 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1358 struct nlmsg_list
*a
;
1360 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1361 struct nlmsghdr
*n
= &a
->h
;
1362 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1363 struct rtattr
*tb
[IFA_MAX
+ 1];
1364 unsigned int ifa_flags
;
1366 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1368 missing_net_address
= 0;
1369 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1371 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1374 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1375 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1377 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1379 if (filter
.pfx
.family
|| filter
.label
) {
1381 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1383 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
1386 memset(&dst
, 0, sizeof(dst
));
1387 dst
.family
= ifa
->ifa_family
;
1388 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
1389 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1397 label
= RTA_DATA(tb
[IFA_LABEL
]);
1399 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1400 if (fnmatch(filter
.label
, label
, 0) != 0)
1408 if (missing_net_address
&&
1409 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1419 static int ipaddr_flush(void)
1422 char flushb
[4096-512];
1424 filter
.flushb
= flushb
;
1426 filter
.flushe
= sizeof(flushb
);
1428 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1429 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1430 perror("Cannot send dump request");
1434 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1435 stdout
, NLM_F_DUMP_INTR
) < 0) {
1436 fprintf(stderr
, "Flush terminated\n");
1439 if (filter
.flushed
== 0) {
1443 printf("Nothing to flush.\n");
1445 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
1451 if (flush_update() < 0)
1455 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1459 /* If we are flushing, and specifying primary, then we
1460 * want to flush only a single round. Otherwise, we'll
1461 * start flushing secondaries that were promoted to
1464 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1467 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1472 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1474 struct nlmsg_chain linfo
= { NULL
, NULL
};
1475 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1476 struct nlmsg_list
*l
;
1477 char *filter_dev
= NULL
;
1480 ipaddr_reset_filter(oneline
, 0);
1481 filter
.showqueue
= 1;
1482 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) {
1504 unsigned int scope
= 0;
1507 filter
.scopemask
= -1;
1508 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1509 if (strcmp(*argv
, "all") != 0)
1510 invarg("invalid \"scope\"\n", *argv
);
1511 scope
= RT_SCOPE_NOWHERE
;
1512 filter
.scopemask
= 0;
1514 filter
.scope
= scope
;
1515 } else if (strcmp(*argv
, "up") == 0) {
1517 } else if (strcmp(*argv
, "dynamic") == 0) {
1518 filter
.flags
&= ~IFA_F_PERMANENT
;
1519 filter
.flagmask
|= IFA_F_PERMANENT
;
1520 } else if (strcmp(*argv
, "permanent") == 0) {
1521 filter
.flags
|= IFA_F_PERMANENT
;
1522 filter
.flagmask
|= IFA_F_PERMANENT
;
1523 } else if (strcmp(*argv
, "secondary") == 0 ||
1524 strcmp(*argv
, "temporary") == 0) {
1525 filter
.flags
|= IFA_F_SECONDARY
;
1526 filter
.flagmask
|= IFA_F_SECONDARY
;
1527 } else if (strcmp(*argv
, "primary") == 0) {
1528 filter
.flags
&= ~IFA_F_SECONDARY
;
1529 filter
.flagmask
|= IFA_F_SECONDARY
;
1530 } else if (strcmp(*argv
, "tentative") == 0) {
1531 filter
.flags
|= IFA_F_TENTATIVE
;
1532 filter
.flagmask
|= IFA_F_TENTATIVE
;
1533 } else if (strcmp(*argv
, "-tentative") == 0) {
1534 filter
.flags
&= ~IFA_F_TENTATIVE
;
1535 filter
.flagmask
|= IFA_F_TENTATIVE
;
1536 } else if (strcmp(*argv
, "deprecated") == 0) {
1537 filter
.flags
|= IFA_F_DEPRECATED
;
1538 filter
.flagmask
|= IFA_F_DEPRECATED
;
1539 } else if (strcmp(*argv
, "-deprecated") == 0) {
1540 filter
.flags
&= ~IFA_F_DEPRECATED
;
1541 filter
.flagmask
|= IFA_F_DEPRECATED
;
1542 } else if (strcmp(*argv
, "home") == 0) {
1543 filter
.flags
|= IFA_F_HOMEADDRESS
;
1544 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1545 } else if (strcmp(*argv
, "nodad") == 0) {
1546 filter
.flags
|= IFA_F_NODAD
;
1547 filter
.flagmask
|= IFA_F_NODAD
;
1548 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1549 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1550 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1551 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1552 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1553 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1554 } else if (strcmp(*argv
, "autojoin") == 0) {
1555 filter
.flags
|= IFA_F_MCAUTOJOIN
;
1556 filter
.flagmask
|= IFA_F_MCAUTOJOIN
;
1557 } else if (strcmp(*argv
, "dadfailed") == 0) {
1558 filter
.flags
|= IFA_F_DADFAILED
;
1559 filter
.flagmask
|= IFA_F_DADFAILED
;
1560 } else if (strcmp(*argv
, "-dadfailed") == 0) {
1561 filter
.flags
&= ~IFA_F_DADFAILED
;
1562 filter
.flagmask
|= IFA_F_DADFAILED
;
1563 } else if (strcmp(*argv
, "label") == 0) {
1565 filter
.label
= *argv
;
1566 } else if (strcmp(*argv
, "group") == 0) {
1568 if (rtnl_group_a2n(&filter
.group
, *argv
))
1569 invarg("Invalid \"group\" value\n", *argv
);
1570 } else if (strcmp(*argv
, "master") == 0) {
1574 ifindex
= ll_name_to_index(*argv
);
1576 invarg("Device does not exist\n", *argv
);
1577 filter
.master
= ifindex
;
1578 } else if (do_link
&& strcmp(*argv
, "type") == 0) {
1580 filter
.kind
= *argv
;
1582 if (strcmp(*argv
, "dev") == 0) {
1584 } else if (matches(*argv
, "help") == 0)
1587 duparg2("dev", *argv
);
1594 filter
.ifindex
= ll_name_to_index(filter_dev
);
1595 if (filter
.ifindex
<= 0) {
1596 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1601 if (action
== IPADD_FLUSH
)
1602 return ipaddr_flush();
1604 if (action
== IPADD_SAVE
) {
1605 if (ipadd_save_prep())
1608 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1609 perror("Cannot send dump request");
1613 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1614 fprintf(stderr
, "Save terminated\n");
1622 * If only filter_dev present and none of the other
1623 * link filters are present, use RTM_GETLINK to get
1626 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1627 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1628 perror("Cannot send link get request");
1634 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
1635 perror("Cannot send dump request");
1639 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1640 fprintf(stderr
, "Dump terminated\n");
1644 if (filter
.family
!= AF_PACKET
) {
1648 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1649 perror("Cannot send dump request");
1653 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1654 fprintf(stderr
, "Dump terminated\n");
1658 ipaddr_filter(&linfo
, &ainfo
);
1661 for (l
= linfo
.head
; l
; l
= l
->next
) {
1663 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1666 if (print_linkinfo_brief(NULL
, &l
->h
, stdout
) == 0)
1667 if (filter
.family
!= AF_PACKET
)
1668 print_selected_addrinfo(ifi
,
1671 } else if (no_link
||
1672 (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1673 if (filter
.family
!= AF_PACKET
)
1674 print_selected_addrinfo(ifi
,
1675 ainfo
.head
, stdout
);
1676 if (res
> 0 && !do_link
&& show_stats
)
1677 print_link_stats(stdout
, &l
->h
);
1682 free_nlmsg_chain(&ainfo
);
1683 free_nlmsg_chain(&linfo
);
1689 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1691 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1692 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1693 struct ifla_vf_rate
*vf_rate
;
1696 rem
= RTA_PAYLOAD(vflist
);
1698 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1699 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1700 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1701 if (vf_rate
->vf
== vfnum
) {
1702 *min
= vf_rate
->min_tx_rate
;
1703 *max
= vf_rate
->max_tx_rate
;
1707 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1711 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1713 struct nlmsg_chain linfo
= { NULL
, NULL
};
1714 struct rtattr
*tb
[IFLA_MAX
+1];
1715 struct ifinfomsg
*ifi
;
1716 struct nlmsg_list
*l
;
1720 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1721 perror("Cannot send dump request");
1724 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1725 fprintf(stderr
, "Dump terminated\n");
1728 for (l
= linfo
.head
; l
; l
= l
->next
) {
1730 ifi
= NLMSG_DATA(n
);
1732 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1733 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1736 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1738 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1739 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1745 int ipaddr_list_link(int argc
, char **argv
)
1747 preferred_family
= AF_PACKET
;
1749 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1752 void ipaddr_reset_filter(int oneline
, int ifindex
)
1754 memset(&filter
, 0, sizeof(filter
));
1755 filter
.oneline
= oneline
;
1756 filter
.ifindex
= ifindex
;
1759 static int default_scope(inet_prefix
*lcl
)
1761 if (lcl
->family
== AF_INET
) {
1762 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1763 return RT_SCOPE_HOST
;
1768 static bool ipaddr_is_multicast(inet_prefix
*a
)
1770 if (a
->family
== AF_INET
)
1771 return IN_MULTICAST(ntohl(a
->data
[0]));
1772 else if (a
->family
== AF_INET6
)
1773 return IN6_IS_ADDR_MULTICAST(a
->data
);
1778 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1782 struct ifaddrmsg ifa
;
1787 char *lcl_arg
= NULL
;
1788 char *valid_lftp
= NULL
;
1789 char *preferred_lftp
= NULL
;
1797 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1798 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1799 struct ifa_cacheinfo cinfo
;
1800 unsigned int ifa_flags
= 0;
1802 memset(&req
, 0, sizeof(req
));
1804 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1805 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1806 req
.n
.nlmsg_type
= cmd
;
1807 req
.ifa
.ifa_family
= preferred_family
;
1810 if (strcmp(*argv
, "peer") == 0 ||
1811 strcmp(*argv
, "remote") == 0) {
1815 duparg("peer", *argv
);
1816 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1817 peer_len
= peer
.bytelen
;
1818 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1819 req
.ifa
.ifa_family
= peer
.family
;
1820 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1821 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1822 } else if (matches(*argv
, "broadcast") == 0 ||
1823 strcmp(*argv
, "brd") == 0) {
1828 duparg("broadcast", *argv
);
1829 if (strcmp(*argv
, "+") == 0)
1831 else if (strcmp(*argv
, "-") == 0)
1834 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1835 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1836 req
.ifa
.ifa_family
= addr
.family
;
1837 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1838 brd_len
= addr
.bytelen
;
1840 } else if (strcmp(*argv
, "anycast") == 0) {
1845 duparg("anycast", *argv
);
1846 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1847 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1848 req
.ifa
.ifa_family
= addr
.family
;
1849 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1850 any_len
= addr
.bytelen
;
1851 } else if (strcmp(*argv
, "scope") == 0) {
1852 unsigned int scope
= 0;
1855 if (rtnl_rtscope_a2n(&scope
, *argv
))
1856 invarg("invalid scope value.", *argv
);
1857 req
.ifa
.ifa_scope
= scope
;
1859 } else if (strcmp(*argv
, "dev") == 0) {
1862 } else if (strcmp(*argv
, "label") == 0) {
1865 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1866 } else if (matches(*argv
, "valid_lft") == 0) {
1868 duparg("valid_lft", *argv
);
1871 if (set_lifetime(&valid_lft
, *argv
))
1872 invarg("valid_lft value", *argv
);
1873 } else if (matches(*argv
, "preferred_lft") == 0) {
1875 duparg("preferred_lft", *argv
);
1877 preferred_lftp
= *argv
;
1878 if (set_lifetime(&preferred_lft
, *argv
))
1879 invarg("preferred_lft value", *argv
);
1880 } else if (strcmp(*argv
, "home") == 0) {
1881 ifa_flags
|= IFA_F_HOMEADDRESS
;
1882 } else if (strcmp(*argv
, "nodad") == 0) {
1883 ifa_flags
|= IFA_F_NODAD
;
1884 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1885 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1886 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1887 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1888 } else if (strcmp(*argv
, "autojoin") == 0) {
1889 ifa_flags
|= IFA_F_MCAUTOJOIN
;
1891 if (strcmp(*argv
, "local") == 0) {
1894 if (matches(*argv
, "help") == 0)
1897 duparg2("local", *argv
);
1899 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1900 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1901 req
.ifa
.ifa_family
= lcl
.family
;
1902 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1903 local_len
= lcl
.bytelen
;
1907 if (ifa_flags
<= 0xff)
1908 req
.ifa
.ifa_flags
= ifa_flags
;
1910 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1913 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1916 if (l
&& matches(d
, l
) != 0) {
1917 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1921 if (peer_len
== 0 && local_len
) {
1922 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1924 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1925 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1926 " This special behaviour is likely to disappear in further releases,\n" \
1927 " fix your scripts!\n", lcl_arg
, local_len
*8);
1930 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1933 if (req
.ifa
.ifa_prefixlen
== 0)
1934 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1936 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1940 if (req
.ifa
.ifa_family
!= AF_INET
) {
1941 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1945 if (brd
.bitlen
<= 30) {
1946 for (i
= 31; i
>= brd
.bitlen
; i
--) {
1948 brd
.data
[0] |= htonl(1<<(31-i
));
1950 brd
.data
[0] &= ~htonl(1<<(31-i
));
1952 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1953 brd_len
= brd
.bytelen
;
1956 if (!scoped
&& cmd
!= RTM_DELADDR
)
1957 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1959 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1960 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1964 if (valid_lftp
|| preferred_lftp
) {
1966 fprintf(stderr
, "valid_lft is zero\n");
1969 if (valid_lft
< preferred_lft
) {
1970 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1974 memset(&cinfo
, 0, sizeof(cinfo
));
1975 cinfo
.ifa_prefered
= preferred_lft
;
1976 cinfo
.ifa_valid
= valid_lft
;
1977 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1981 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
1982 fprintf(stderr
, "autojoin needs multicast address\n");
1986 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
1992 int do_ipaddr(int argc
, char **argv
)
1995 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
1996 if (matches(*argv
, "add") == 0)
1997 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1998 if (matches(*argv
, "change") == 0 ||
1999 strcmp(*argv
, "chg") == 0)
2000 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
2001 if (matches(*argv
, "replace") == 0)
2002 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
2003 if (matches(*argv
, "delete") == 0)
2004 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
2005 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2006 || matches(*argv
, "lst") == 0)
2007 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2008 if (matches(*argv
, "flush") == 0)
2009 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2010 if (matches(*argv
, "save") == 0)
2011 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2012 if (matches(*argv
, "showdump") == 0)
2013 return ipaddr_showdump();
2014 if (matches(*argv
, "restore") == 0)
2015 return ipaddr_restore();
2016 if (matches(*argv
, "help") == 0)
2018 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);