2 * ipaddress.c "ip address".
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/sockios.h>
31 #include <linux/net_namespace.h>
36 #include "ip_common.h"
67 static void usage(void) __attribute__((noreturn
));
69 static void usage(void)
74 fprintf(stderr
, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
75 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
76 fprintf(stderr
, " ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
77 fprintf(stderr
, " ip address {show|save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
78 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
79 fprintf(stderr
, " ip address {showdump|restore}\n");
80 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
81 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
82 fprintf(stderr
, " [ label IFNAME ] [ scope SCOPE-ID ]\n");
83 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
84 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
85 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
86 fprintf(stderr
, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
87 fprintf(stderr
, " CONFFLAG-LIST ]\n");
88 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
89 fprintf(stderr
, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
90 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
91 fprintf(stderr
, "LFT := forever | SECONDS\n");
96 static void print_link_flags(FILE *fp
, unsigned flags
, unsigned mdown
)
99 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
100 fprintf(fp
, "NO-CARRIER%s", flags
? "," : "");
101 flags
&= ~IFF_RUNNING
;
102 #define _PF(f) if (flags&IFF_##f) { \
103 flags &= ~IFF_##f ; \
104 fprintf(fp, #f "%s", flags ? "," : ""); }
125 fprintf(fp
, "%x", flags
);
127 fprintf(fp
, ",M-DOWN");
131 static const char *oper_states
[] = {
132 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
133 "TESTING", "DORMANT", "UP"
136 static void print_operstate(FILE *f
, __u8 state
)
138 if (state
>= sizeof(oper_states
)/sizeof(oper_states
[0]))
139 fprintf(f
, "state %#x ", state
);
142 if (strcmp(oper_states
[state
], "UP") == 0)
143 color_fprintf(f
, COLOR_OPERSTATE_UP
, "%-14s ", oper_states
[state
]);
144 else if (strcmp(oper_states
[state
], "DOWN") == 0)
145 color_fprintf(f
, COLOR_OPERSTATE_DOWN
, "%-14s ", oper_states
[state
]);
147 fprintf(f
, "%-14s ", oper_states
[state
]);
149 fprintf(f
, "state ");
150 if (strcmp(oper_states
[state
], "UP") == 0)
151 color_fprintf(f
, COLOR_OPERSTATE_UP
, "%s ", oper_states
[state
]);
152 else if (strcmp(oper_states
[state
], "DOWN") == 0)
153 color_fprintf(f
, COLOR_OPERSTATE_DOWN
, "%s ", oper_states
[state
]);
155 fprintf(f
, "%s ", oper_states
[state
]);
160 int get_operstate(const char *name
)
164 for (i
= 0; i
< sizeof(oper_states
)/sizeof(oper_states
[0]); i
++)
165 if (strcasecmp(name
, oper_states
[i
]) == 0)
170 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
175 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
178 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
183 memset(&ifr
, 0, sizeof(ifr
));
184 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
185 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
186 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
194 fprintf(f
, "qlen %d", qlen
);
197 static const char *link_modes
[] = {
201 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
203 unsigned int mode
= rta_getattr_u8(tb
);
205 if (mode
>= sizeof(link_modes
) / sizeof(link_modes
[0]))
206 fprintf(f
, "mode %d ", mode
);
208 fprintf(f
, "mode %s ", link_modes
[mode
]);
211 static char *parse_link_kind(struct rtattr
*tb
)
213 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
215 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
217 if (linkinfo
[IFLA_INFO_KIND
])
218 return RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
223 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
225 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
226 struct link_util
*lu
;
227 struct link_util
*slave_lu
;
231 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
233 if (linkinfo
[IFLA_INFO_KIND
]) {
234 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
236 fprintf(fp
, "%s", _SL_
);
237 fprintf(fp
, " %s ", kind
);
239 lu
= get_link_kind(kind
);
240 if (lu
&& lu
->print_opt
) {
241 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
243 if (linkinfo
[IFLA_INFO_DATA
]) {
244 parse_rtattr_nested(attr
, lu
->maxattr
,
245 linkinfo
[IFLA_INFO_DATA
]);
248 lu
->print_opt(lu
, fp
, data
);
250 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
252 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
256 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
257 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
259 fprintf(fp
, "%s", _SL_
);
260 fprintf(fp
, " %s_slave ", slave_kind
);
262 slave_lu
= get_link_slave_kind(slave_kind
);
263 if (slave_lu
&& slave_lu
->print_opt
) {
264 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
266 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
267 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
268 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
271 slave_lu
->print_opt(slave_lu
, fp
, data
);
276 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
278 struct rtattr
*inet6_attr
;
279 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
281 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
285 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
287 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
288 __u8 mode
= rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
290 case IN6_ADDR_GEN_MODE_EUI64
:
291 fprintf(fp
, "addrgenmode eui64 ");
293 case IN6_ADDR_GEN_MODE_NONE
:
294 fprintf(fp
, "addrgenmode none ");
296 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
297 fprintf(fp
, "addrgenmode stable_secret ");
299 case IN6_ADDR_GEN_MODE_RANDOM
:
300 fprintf(fp
, "addrgenmode random ");
303 fprintf(fp
, "addrgenmode %#.2hhx ", mode
);
309 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
);
311 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
313 struct ifla_vf_mac
*vf_mac
;
314 struct ifla_vf_vlan
*vf_vlan
;
315 struct ifla_vf_tx_rate
*vf_tx_rate
;
316 struct ifla_vf_spoofchk
*vf_spoofchk
;
317 struct ifla_vf_link_state
*vf_linkstate
;
318 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
322 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
323 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
327 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
329 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
330 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
331 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
333 /* Check if the spoof checking vf info type is supported by
336 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
337 vf
[IFLA_VF_TX_RATE
]->rta_len
);
339 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
342 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
345 /* Check if the link state vf info type is supported by
348 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_SPOOFCHK
] +
349 vf
[IFLA_VF_SPOOFCHK
]->rta_len
);
351 if (tmp
->rta_type
!= IFLA_VF_LINK_STATE
)
354 vf_linkstate
= RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
358 fprintf(fp
, "%s vf %d MAC %s", _SL_
, vf_mac
->vf
,
359 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
360 ETH_ALEN
, 0, b1
, sizeof(b1
)));
362 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
364 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
365 if (vf_tx_rate
->rate
)
366 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
368 if (vf
[IFLA_VF_RATE
]) {
369 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
371 if (vf_rate
->max_tx_rate
)
372 fprintf(fp
, ", max_tx_rate %dMbps", vf_rate
->max_tx_rate
);
373 if (vf_rate
->min_tx_rate
)
374 fprintf(fp
, ", min_tx_rate %dMbps", vf_rate
->min_tx_rate
);
377 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
378 if (vf_spoofchk
->setting
)
379 fprintf(fp
, ", spoof checking on");
381 fprintf(fp
, ", spoof checking off");
384 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
385 fprintf(fp
, ", link-state auto");
386 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
387 fprintf(fp
, ", link-state enable");
389 fprintf(fp
, ", link-state disable");
391 if (vf
[IFLA_VF_STATS
] && show_stats
)
392 print_vf_stats64(fp
, vf
[IFLA_VF_STATS
]);
395 static void print_num(FILE *fp
, unsigned width
, uint64_t count
)
397 const char *prefix
= "kMGTPE";
398 const unsigned int base
= use_iec
? 1024 : 1000;
401 uint8_t precision
= 2;
404 if (!human_readable
|| count
< base
) {
405 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
409 /* increase value by a factor of 1000/1024 and print
410 * if result is something a human can read */
413 if (count
/ base
< powi
)
421 /* try to guess a good number of digits for precision */
422 for (; precision
> 0; precision
--) {
424 if (count
/ powi
< powj
)
428 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
429 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
431 fprintf(fp
, "%-*s ", width
, buf
);
434 static void print_vf_stats64(FILE *fp
, struct rtattr
*vfstats
)
436 struct rtattr
*vf
[IFLA_VF_STATS_MAX
+ 1] = {};
438 if (vfstats
->rta_type
!= IFLA_VF_STATS
) {
439 fprintf(stderr
, "BUG: rta type is %d\n", vfstats
->rta_type
);
443 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfstats
);
446 fprintf(fp
, "%s", _SL_
);
447 fprintf(fp
, " RX: bytes packets mcast bcast %s", _SL_
);
450 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_BYTES
]));
451 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_RX_PACKETS
]));
452 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_MULTICAST
]));
453 print_num(fp
, 7, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_BROADCAST
]));
456 fprintf(fp
, "%s", _SL_
);
457 fprintf(fp
, " TX: bytes packets %s", _SL_
);
460 print_num(fp
, 10, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_BYTES
]));
461 print_num(fp
, 8, *(__u64
*)RTA_DATA(vf
[IFLA_VF_STATS_TX_PACKETS
]));
464 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
465 const struct rtattr
*carrier_changes
)
468 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
469 s
->rx_compressed
? "compressed" : "", _SL_
);
472 print_num(fp
, 10, s
->rx_bytes
);
473 print_num(fp
, 8, s
->rx_packets
);
474 print_num(fp
, 7, s
->rx_errors
);
475 print_num(fp
, 7, s
->rx_dropped
);
476 print_num(fp
, 7, s
->rx_over_errors
);
477 print_num(fp
, 7, s
->multicast
);
478 if (s
->rx_compressed
)
479 print_num(fp
, 7, s
->rx_compressed
);
482 if (show_stats
> 1) {
483 fprintf(fp
, "%s", _SL_
);
484 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
485 s
->rx_nohandler
? " nohandler" : "", _SL_
);
488 print_num(fp
, 8, s
->rx_length_errors
);
489 print_num(fp
, 7, s
->rx_crc_errors
);
490 print_num(fp
, 7, s
->rx_frame_errors
);
491 print_num(fp
, 7, s
->rx_fifo_errors
);
492 print_num(fp
, 7, s
->rx_missed_errors
);
494 print_num(fp
, 7, s
->rx_nohandler
);
497 fprintf(fp
, "%s", _SL_
);
500 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
501 s
->tx_compressed
? "compressed" : "", _SL_
);
504 print_num(fp
, 10, s
->tx_bytes
);
505 print_num(fp
, 8, s
->tx_packets
);
506 print_num(fp
, 7, s
->tx_errors
);
507 print_num(fp
, 7, s
->tx_dropped
);
508 print_num(fp
, 7, s
->tx_carrier_errors
);
509 print_num(fp
, 7, s
->collisions
);
510 if (s
->tx_compressed
)
511 print_num(fp
, 7, s
->tx_compressed
);
514 if (show_stats
> 1) {
515 fprintf(fp
, "%s", _SL_
);
516 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
518 fprintf(fp
, " transns");
519 fprintf(fp
, "%s", _SL_
);
522 print_num(fp
, 8, s
->tx_aborted_errors
);
523 print_num(fp
, 7, s
->tx_fifo_errors
);
524 print_num(fp
, 7, s
->tx_window_errors
);
525 print_num(fp
, 7, s
->tx_heartbeat_errors
);
527 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
531 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
532 const struct rtattr
*carrier_changes
)
535 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
536 s
->rx_compressed
? "compressed" : "", _SL_
);
540 print_num(fp
, 10, s
->rx_bytes
);
541 print_num(fp
, 8, s
->rx_packets
);
542 print_num(fp
, 7, s
->rx_errors
);
543 print_num(fp
, 7, s
->rx_dropped
);
544 print_num(fp
, 7, s
->rx_over_errors
);
545 print_num(fp
, 7, s
->multicast
);
546 if (s
->rx_compressed
)
547 print_num(fp
, 7, s
->rx_compressed
);
550 if (show_stats
> 1) {
551 fprintf(fp
, "%s", _SL_
);
552 fprintf(fp
, " RX errors: length crc frame fifo missed%s%s",
553 s
->rx_nohandler
? " nohandler" : "", _SL_
);
555 print_num(fp
, 8, s
->rx_length_errors
);
556 print_num(fp
, 7, s
->rx_crc_errors
);
557 print_num(fp
, 7, s
->rx_frame_errors
);
558 print_num(fp
, 7, s
->rx_fifo_errors
);
559 print_num(fp
, 7, s
->rx_missed_errors
);
561 print_num(fp
, 7, s
->rx_nohandler
);
563 fprintf(fp
, "%s", _SL_
);
566 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
567 s
->tx_compressed
? "compressed" : "", _SL_
);
570 print_num(fp
, 10, s
->tx_bytes
);
571 print_num(fp
, 8, s
->tx_packets
);
572 print_num(fp
, 7, s
->tx_errors
);
573 print_num(fp
, 7, s
->tx_dropped
);
574 print_num(fp
, 7, s
->tx_carrier_errors
);
575 print_num(fp
, 7, s
->collisions
);
576 if (s
->tx_compressed
)
577 print_num(fp
, 7, s
->tx_compressed
);
580 if (show_stats
> 1) {
581 fprintf(fp
, "%s", _SL_
);
582 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
584 fprintf(fp
, " transns");
585 fprintf(fp
, "%s", _SL_
);
588 print_num(fp
, 8, s
->tx_aborted_errors
);
589 print_num(fp
, 7, s
->tx_fifo_errors
);
590 print_num(fp
, 7, s
->tx_window_errors
);
591 print_num(fp
, 7, s
->tx_heartbeat_errors
);
593 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
597 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
599 const struct rtattr
*carrier_changes
= tb
[IFLA_CARRIER_CHANGES
];
601 if (tb
[IFLA_STATS64
]) {
602 struct rtnl_link_stats64 stats
= { 0 };
604 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS64
]),
605 MIN(RTA_PAYLOAD(tb
[IFLA_STATS64
]), sizeof(stats
)));
607 print_link_stats64(fp
, &stats
, carrier_changes
);
608 } else if (tb
[IFLA_STATS
]) {
609 struct rtnl_link_stats stats
= { 0 };
611 memcpy(&stats
, RTA_DATA(tb
[IFLA_STATS
]),
612 MIN(RTA_PAYLOAD(tb
[IFLA_STATS
]), sizeof(stats
)));
614 print_link_stats32(fp
, &stats
, carrier_changes
);
618 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
620 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
621 struct rtattr
* tb
[IFLA_MAX
+1];
623 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
624 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
625 __print_link_stats(fp
, tb
);
626 fprintf(fp
, "%s", _SL_
);
629 int print_linkinfo_brief(const struct sockaddr_nl
*who
,
630 struct nlmsghdr
*n
, void *arg
)
632 FILE *fp
= (FILE*)arg
;
633 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
634 struct rtattr
* tb
[IFLA_MAX
+1];
635 int len
= n
->nlmsg_len
;
637 char buf
[32] = { 0, };
640 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
643 len
-= NLMSG_LENGTH(sizeof(*ifi
));
647 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
649 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
652 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
653 if (tb
[IFLA_IFNAME
] == NULL
) {
654 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
657 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
658 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
661 if (tb
[IFLA_GROUP
]) {
662 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
]);
669 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
]);
695 snprintf(buf
, sizeof(buf
), "%s@NONE", name
);
697 snprintf(buf
, sizeof(buf
),
698 "%s@%s", name
, ll_idx_n2a(iflink
, b1
));
699 m_flag
= ll_index_to_flags(iflink
);
700 m_flag
= !(m_flag
& IFF_UP
);
703 snprintf(buf
, sizeof(buf
), "%s", name
);
705 fprintf(fp
, "%-16s ", buf
);
707 if (tb
[IFLA_OPERSTATE
])
708 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
710 if (filter
.family
== AF_PACKET
) {
712 if (tb
[IFLA_ADDRESS
]) {
713 color_fprintf(fp
, COLOR_MAC
, "%s ",
714 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
715 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
721 if (filter
.family
== AF_PACKET
)
722 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
724 if (filter
.family
== AF_PACKET
)
730 int print_linkinfo(const struct sockaddr_nl
*who
,
731 struct nlmsghdr
*n
, void *arg
)
733 FILE *fp
= (FILE*)arg
;
734 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
735 struct rtattr
* tb
[IFLA_MAX
+1];
736 int len
= n
->nlmsg_len
;
739 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
742 len
-= NLMSG_LENGTH(sizeof(*ifi
));
746 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
748 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
751 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
752 if (tb
[IFLA_IFNAME
] == NULL
) {
753 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
756 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
757 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
760 if (tb
[IFLA_GROUP
]) {
761 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
762 if (filter
.group
!= -1 && group
!= filter
.group
)
766 if (tb
[IFLA_MASTER
]) {
767 int master
= *(int*)RTA_DATA(tb
[IFLA_MASTER
]);
768 if (filter
.master
> 0 && master
!= filter
.master
)
771 else if (filter
.master
> 0)
775 if (tb
[IFLA_LINKINFO
]) {
776 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
778 if (strcmp(kind
, filter
.kind
))
785 if (n
->nlmsg_type
== RTM_DELLINK
)
786 fprintf(fp
, "Deleted ");
788 fprintf(fp
, "%d: ", ifi
->ifi_index
);
789 color_fprintf(fp
, COLOR_IFNAME
, "%s",
790 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
794 int iflink
= *(int*)RTA_DATA(tb
[IFLA_LINK
]);
796 fprintf(fp
, "@NONE: ");
798 if (tb
[IFLA_LINK_NETNSID
])
799 fprintf(fp
, "@if%d: ", iflink
);
801 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
802 m_flag
= ll_index_to_flags(iflink
);
803 m_flag
= !(m_flag
& IFF_UP
);
809 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
812 fprintf(fp
, "mtu %u ", *(int*)RTA_DATA(tb
[IFLA_MTU
]));
814 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
815 if (tb
[IFLA_MASTER
]) {
817 fprintf(fp
, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
820 if (tb
[IFLA_PHYS_PORT_ID
]) {
822 fprintf(fp
, "portid %s ",
823 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
824 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
828 if (tb
[IFLA_PHYS_SWITCH_ID
]) {
830 fprintf(fp
, "switchid %s ",
831 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_SWITCH_ID
]),
832 RTA_PAYLOAD(tb
[IFLA_PHYS_SWITCH_ID
]),
836 if (tb
[IFLA_OPERSTATE
])
837 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
839 if (do_link
&& tb
[IFLA_LINKMODE
])
840 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
842 if (tb
[IFLA_GROUP
]) {
844 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
845 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
848 if (filter
.showqueue
)
849 print_queuelen(fp
, tb
);
851 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
853 fprintf(fp
, "%s", _SL_
);
854 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
856 if (tb
[IFLA_ADDRESS
]) {
857 color_fprintf(fp
, COLOR_MAC
, "%s",
858 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
859 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
863 if (tb
[IFLA_BROADCAST
]) {
864 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
865 fprintf(fp
, " peer ");
867 fprintf(fp
, " brd ");
868 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
869 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
875 if (tb
[IFLA_LINK_NETNSID
]) {
876 int id
= *(int*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
879 fprintf(fp
, " link-netnsid %d", id
);
881 fprintf(fp
, " link-netnsid unknown");
884 if (tb
[IFLA_PROTO_DOWN
]) {
885 if (rta_getattr_u8(tb
[IFLA_PROTO_DOWN
]))
886 fprintf(fp
, " protodown on ");
889 if (tb
[IFLA_PROMISCUITY
] && show_details
)
890 fprintf(fp
, " promiscuity %u ",
891 *(int*)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
893 if (tb
[IFLA_LINKINFO
] && show_details
)
894 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
896 if (do_link
&& tb
[IFLA_AF_SPEC
] && show_details
)
897 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
899 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
900 fprintf(fp
, "%s alias %s", _SL_
,
901 rta_getattr_str(tb
[IFLA_IFALIAS
]));
904 if (do_link
&& show_stats
) {
905 fprintf(fp
, "%s", _SL_
);
906 __print_link_stats(fp
, tb
);
909 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
910 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
911 int rem
= RTA_PAYLOAD(vflist
);
912 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
921 static int flush_update(void)
925 * Note that the kernel may delete multiple addresses for one
926 * delete request (e.g. if ipv4 address promotion is disabled).
927 * Since a flush operation is really a series of delete requests
928 * its possible that we may request an address delete that has
929 * already been done by the kernel. Therefore, ignore EADDRNOTAVAIL
930 * errors returned from a flush request
932 if ((rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) &&
933 (errno
!= EADDRNOTAVAIL
)) {
934 perror("Failed to send flush request");
941 static int set_lifetime(unsigned int *lifetime
, char *argv
)
943 if (strcmp(argv
, "forever") == 0)
944 *lifetime
= INFINITY_LIFE_TIME
;
945 else if (get_u32(lifetime
, argv
, 0))
951 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
952 struct rtattr
*ifa_flags_attr
)
954 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
958 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
962 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
963 int len
= n
->nlmsg_len
;
965 /* Use local copy of ifa_flags to not interfere with filtering code */
966 unsigned int ifa_flags
;
967 struct rtattr
* rta_tb
[IFA_MAX
+1];
971 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
973 len
-= NLMSG_LENGTH(sizeof(*ifa
));
975 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
979 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
982 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
983 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
985 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
987 if (!rta_tb
[IFA_LOCAL
])
988 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
989 if (!rta_tb
[IFA_ADDRESS
])
990 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
992 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
994 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
996 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1001 if (rta_tb
[IFA_LABEL
])
1002 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
1004 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1005 if (fnmatch(filter
.label
, label
, 0) != 0)
1008 if (filter
.pfx
.family
) {
1009 if (rta_tb
[IFA_LOCAL
]) {
1011 memset(&dst
, 0, sizeof(dst
));
1012 dst
.family
= ifa
->ifa_family
;
1013 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
1014 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1019 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1022 if (filter
.flushb
) {
1023 struct nlmsghdr
*fn
;
1024 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
1028 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
1029 memcpy(fn
, n
, n
->nlmsg_len
);
1030 fn
->nlmsg_type
= RTM_DELADDR
;
1031 fn
->nlmsg_flags
= NLM_F_REQUEST
;
1032 fn
->nlmsg_seq
= ++rth
.seq
;
1033 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
1039 if (n
->nlmsg_type
== RTM_DELADDR
)
1040 fprintf(fp
, "Deleted ");
1043 if (filter
.oneline
|| filter
.flushb
)
1044 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
1045 if (ifa
->ifa_family
== AF_INET
)
1046 fprintf(fp
, " inet ");
1047 else if (ifa
->ifa_family
== AF_INET6
)
1048 fprintf(fp
, " inet6 ");
1049 else if (ifa
->ifa_family
== AF_DECnet
)
1050 fprintf(fp
, " dnet ");
1051 else if (ifa
->ifa_family
== AF_IPX
)
1052 fprintf(fp
, " ipx ");
1054 fprintf(fp
, " family %d ", ifa
->ifa_family
);
1057 if (rta_tb
[IFA_LOCAL
]) {
1058 if (ifa
->ifa_family
== AF_INET
)
1059 color_fprintf(fp
, COLOR_INET
, "%s", format_host(ifa
->ifa_family
,
1060 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
1061 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1062 abuf
, sizeof(abuf
)));
1063 else if (ifa
->ifa_family
== AF_INET6
)
1064 color_fprintf(fp
, COLOR_INET6
, "%s", format_host(ifa
->ifa_family
,
1065 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
1066 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1067 abuf
, sizeof(abuf
)));
1069 fprintf(fp
, "%s", format_host(ifa
->ifa_family
,
1070 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
1071 RTA_DATA(rta_tb
[IFA_LOCAL
]),
1072 abuf
, sizeof(abuf
)));
1074 if (rta_tb
[IFA_ADDRESS
] == NULL
||
1075 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]), RTA_DATA(rta_tb
[IFA_LOCAL
]),
1076 ifa
->ifa_family
== AF_INET
? 4 : 16) == 0) {
1077 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
1079 fprintf(fp
, " peer %s/%d ",
1080 format_host(ifa
->ifa_family
,
1081 RTA_PAYLOAD(rta_tb
[IFA_ADDRESS
]),
1082 RTA_DATA(rta_tb
[IFA_ADDRESS
]),
1083 abuf
, sizeof(abuf
)),
1084 ifa
->ifa_prefixlen
);
1091 if (rta_tb
[IFA_BROADCAST
]) {
1092 fprintf(fp
, "brd %s ",
1093 format_host(ifa
->ifa_family
,
1094 RTA_PAYLOAD(rta_tb
[IFA_BROADCAST
]),
1095 RTA_DATA(rta_tb
[IFA_BROADCAST
]),
1096 abuf
, sizeof(abuf
)));
1098 if (rta_tb
[IFA_ANYCAST
]) {
1099 fprintf(fp
, "any %s ",
1100 format_host(ifa
->ifa_family
,
1101 RTA_PAYLOAD(rta_tb
[IFA_ANYCAST
]),
1102 RTA_DATA(rta_tb
[IFA_ANYCAST
]),
1103 abuf
, sizeof(abuf
)));
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
]);
1156 fprintf(fp
, "%s", _SL_
);
1157 fprintf(fp
, " valid_lft ");
1158 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
1159 fprintf(fp
, "forever");
1161 fprintf(fp
, "%usec", ci
->ifa_valid
);
1162 fprintf(fp
, " preferred_lft ");
1163 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
1164 fprintf(fp
, "forever");
1167 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
1169 fprintf(fp
, "%usec", ci
->ifa_prefered
);
1180 struct nlmsg_list
*next
;
1186 struct nlmsg_list
*head
;
1187 struct nlmsg_list
*tail
;
1190 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1191 struct nlmsg_list
*ainfo
, FILE *fp
)
1193 for ( ;ainfo
; ainfo
= ainfo
->next
) {
1194 struct nlmsghdr
*n
= &ainfo
->h
;
1195 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1197 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1200 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
1203 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1204 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1207 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1210 print_addrinfo(NULL
, n
, fp
);
1220 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1223 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1224 struct nlmsg_list
*h
;
1226 h
= malloc(n
->nlmsg_len
+sizeof(void*));
1230 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1234 lchain
->tail
->next
= h
;
1239 ll_remember_index(who
, n
, NULL
);
1243 static __u32 ipadd_dump_magic
= 0x47361222;
1245 static int ipadd_save_prep(void)
1249 if (isatty(STDOUT_FILENO
)) {
1250 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1254 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1255 if (ret
!= sizeof(ipadd_dump_magic
)) {
1256 fprintf(stderr
, "Can't write magic to dump file\n");
1263 static int ipadd_dump_check_magic(void)
1268 if (isatty(STDIN_FILENO
)) {
1269 fprintf(stderr
, "Can't restore address dump from a terminal\n");
1273 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1274 if (magic
!= ipadd_dump_magic
) {
1275 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1282 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1287 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1288 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1289 fprintf(stderr
, "Short write while saving nlmsg\n");
1293 return ret
== n
->nlmsg_len
? 0 : ret
;
1296 static int show_handler(const struct sockaddr_nl
*nl
,
1297 struct rtnl_ctrl_data
*ctrl
,
1298 struct nlmsghdr
*n
, void *arg
)
1300 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1302 printf("if%d:\n", ifa
->ifa_index
);
1303 print_addrinfo(NULL
, n
, stdout
);
1307 static int ipaddr_showdump(void)
1309 if (ipadd_dump_check_magic())
1312 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1315 static int restore_handler(const struct sockaddr_nl
*nl
,
1316 struct rtnl_ctrl_data
*ctrl
,
1317 struct nlmsghdr
*n
, void *arg
)
1321 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1325 ret
= rtnl_talk(&rth
, n
, n
, sizeof(*n
));
1326 if ((ret
< 0) && (errno
== EEXIST
))
1332 static int ipaddr_restore(void)
1334 if (ipadd_dump_check_magic())
1337 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1340 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1342 struct nlmsg_list
*l
, *n
;
1344 for (l
= info
->head
; l
; l
= n
) {
1350 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1352 struct nlmsg_list
*l
, **lp
;
1355 while ( (l
= *lp
) != NULL
) {
1357 int missing_net_address
= 1;
1358 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1359 struct nlmsg_list
*a
;
1361 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1362 struct nlmsghdr
*n
= &a
->h
;
1363 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1364 struct rtattr
*tb
[IFA_MAX
+ 1];
1365 unsigned int ifa_flags
;
1367 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1369 missing_net_address
= 0;
1370 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1372 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1375 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1376 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1378 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1380 if (filter
.pfx
.family
|| filter
.label
) {
1382 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1384 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
))
1396 label
= RTA_DATA(tb
[IFA_LABEL
]);
1398 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1399 if (fnmatch(filter
.label
, label
, 0) != 0)
1407 if (missing_net_address
&&
1408 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1418 static int ipaddr_flush(void)
1421 char flushb
[4096-512];
1423 filter
.flushb
= flushb
;
1425 filter
.flushe
= sizeof(flushb
);
1427 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1428 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1429 perror("Cannot send dump request");
1433 if (rtnl_dump_filter_nc(&rth
, print_addrinfo
,
1434 stdout
, NLM_F_DUMP_INTR
) < 0) {
1435 fprintf(stderr
, "Flush terminated\n");
1438 if (filter
.flushed
== 0) {
1442 printf("Nothing to flush.\n");
1444 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
1450 if (flush_update() < 0)
1454 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1458 /* If we are flushing, and specifying primary, then we
1459 * want to flush only a single round. Otherwise, we'll
1460 * start flushing secondaries that were promoted to
1463 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1466 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1471 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1473 struct nlmsg_chain linfo
= { NULL
, NULL
};
1474 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1475 struct nlmsg_list
*l
;
1476 char *filter_dev
= NULL
;
1479 ipaddr_reset_filter(oneline
, 0);
1480 filter
.showqueue
= 1;
1481 filter
.family
= preferred_family
;
1484 if (action
== IPADD_FLUSH
) {
1486 fprintf(stderr
, "Flush requires arguments.\n");
1490 if (filter
.family
== AF_PACKET
) {
1491 fprintf(stderr
, "Cannot flush link addresses.\n");
1497 if (strcmp(*argv
, "to") == 0) {
1499 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1500 if (filter
.family
== AF_UNSPEC
)
1501 filter
.family
= filter
.pfx
.family
;
1502 } else if (strcmp(*argv
, "scope") == 0) {
1505 filter
.scopemask
= -1;
1506 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1507 if (strcmp(*argv
, "all") != 0)
1508 invarg("invalid \"scope\"\n", *argv
);
1509 scope
= RT_SCOPE_NOWHERE
;
1510 filter
.scopemask
= 0;
1512 filter
.scope
= scope
;
1513 } else if (strcmp(*argv
, "up") == 0) {
1515 } else if (strcmp(*argv
, "dynamic") == 0) {
1516 filter
.flags
&= ~IFA_F_PERMANENT
;
1517 filter
.flagmask
|= IFA_F_PERMANENT
;
1518 } else if (strcmp(*argv
, "permanent") == 0) {
1519 filter
.flags
|= IFA_F_PERMANENT
;
1520 filter
.flagmask
|= IFA_F_PERMANENT
;
1521 } else if (strcmp(*argv
, "secondary") == 0 ||
1522 strcmp(*argv
, "temporary") == 0) {
1523 filter
.flags
|= IFA_F_SECONDARY
;
1524 filter
.flagmask
|= IFA_F_SECONDARY
;
1525 } else if (strcmp(*argv
, "primary") == 0) {
1526 filter
.flags
&= ~IFA_F_SECONDARY
;
1527 filter
.flagmask
|= IFA_F_SECONDARY
;
1528 } else if (strcmp(*argv
, "tentative") == 0) {
1529 filter
.flags
|= IFA_F_TENTATIVE
;
1530 filter
.flagmask
|= IFA_F_TENTATIVE
;
1531 } else if (strcmp(*argv
, "-tentative") == 0) {
1532 filter
.flags
&= ~IFA_F_TENTATIVE
;
1533 filter
.flagmask
|= IFA_F_TENTATIVE
;
1534 } else if (strcmp(*argv
, "deprecated") == 0) {
1535 filter
.flags
|= IFA_F_DEPRECATED
;
1536 filter
.flagmask
|= IFA_F_DEPRECATED
;
1537 } else if (strcmp(*argv
, "-deprecated") == 0) {
1538 filter
.flags
&= ~IFA_F_DEPRECATED
;
1539 filter
.flagmask
|= IFA_F_DEPRECATED
;
1540 } else if (strcmp(*argv
, "home") == 0) {
1541 filter
.flags
|= IFA_F_HOMEADDRESS
;
1542 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1543 } else if (strcmp(*argv
, "nodad") == 0) {
1544 filter
.flags
|= IFA_F_NODAD
;
1545 filter
.flagmask
|= IFA_F_NODAD
;
1546 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1547 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1548 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1549 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1550 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1551 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1552 } else if (strcmp(*argv
, "autojoin") == 0) {
1553 filter
.flags
|= IFA_F_MCAUTOJOIN
;
1554 filter
.flagmask
|= IFA_F_MCAUTOJOIN
;
1555 } else if (strcmp(*argv
, "dadfailed") == 0) {
1556 filter
.flags
|= IFA_F_DADFAILED
;
1557 filter
.flagmask
|= IFA_F_DADFAILED
;
1558 } else if (strcmp(*argv
, "-dadfailed") == 0) {
1559 filter
.flags
&= ~IFA_F_DADFAILED
;
1560 filter
.flagmask
|= IFA_F_DADFAILED
;
1561 } else if (strcmp(*argv
, "label") == 0) {
1563 filter
.label
= *argv
;
1564 } else if (strcmp(*argv
, "group") == 0) {
1566 if (rtnl_group_a2n(&filter
.group
, *argv
))
1567 invarg("Invalid \"group\" value\n", *argv
);
1568 } else if (strcmp(*argv
, "master") == 0) {
1571 ifindex
= ll_name_to_index(*argv
);
1573 invarg("Device does not exist\n", *argv
);
1574 filter
.master
= ifindex
;
1575 } else if (do_link
&& strcmp(*argv
, "type") == 0) {
1577 filter
.kind
= *argv
;
1579 if (strcmp(*argv
, "dev") == 0) {
1582 else if (matches(*argv
, "help") == 0)
1585 duparg2("dev", *argv
);
1592 filter
.ifindex
= ll_name_to_index(filter_dev
);
1593 if (filter
.ifindex
<= 0) {
1594 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1599 if (action
== IPADD_FLUSH
)
1600 return ipaddr_flush();
1602 if (action
== IPADD_SAVE
) {
1603 if (ipadd_save_prep())
1606 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1607 perror("Cannot send dump request");
1611 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1612 fprintf(stderr
, "Save terminated\n");
1620 * If only filter_dev present and none of the other
1621 * link filters are present, use RTM_GETLINK to get
1624 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1625 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1626 perror("Cannot send link get request");
1632 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
1633 perror("Cannot send dump request");
1637 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1638 fprintf(stderr
, "Dump terminated\n");
1642 if (filter
.family
!= AF_PACKET
) {
1646 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1647 perror("Cannot send dump request");
1651 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1652 fprintf(stderr
, "Dump terminated\n");
1656 ipaddr_filter(&linfo
, &ainfo
);
1659 for (l
= linfo
.head
; l
; l
= l
->next
) {
1661 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1664 if (print_linkinfo_brief(NULL
, &l
->h
, stdout
) == 0)
1665 if (filter
.family
!= AF_PACKET
)
1666 print_selected_addrinfo(ifi
,
1669 } else if (no_link
||
1670 (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1671 if (filter
.family
!= AF_PACKET
)
1672 print_selected_addrinfo(ifi
,
1673 ainfo
.head
, stdout
);
1674 if (res
> 0 && !do_link
&& show_stats
)
1675 print_link_stats(stdout
, &l
->h
);
1680 free_nlmsg_chain(&ainfo
);
1681 free_nlmsg_chain(&linfo
);
1687 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1689 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1690 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1691 struct ifla_vf_rate
*vf_rate
;
1694 rem
= RTA_PAYLOAD(vflist
);
1696 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1697 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1698 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1699 if (vf_rate
->vf
== vfnum
) {
1700 *min
= vf_rate
->min_tx_rate
;
1701 *max
= vf_rate
->max_tx_rate
;
1705 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1709 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1711 struct nlmsg_chain linfo
= { NULL
, NULL
};
1712 struct rtattr
*tb
[IFLA_MAX
+1];
1713 struct ifinfomsg
*ifi
;
1714 struct nlmsg_list
*l
;
1718 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1719 perror("Cannot send dump request");
1722 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1723 fprintf(stderr
, "Dump terminated\n");
1726 for (l
= linfo
.head
; l
; l
= l
->next
) {
1728 ifi
= NLMSG_DATA(n
);
1730 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1731 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1734 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1736 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1737 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1743 int ipaddr_list_link(int argc
, char **argv
)
1745 preferred_family
= AF_PACKET
;
1747 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1750 void ipaddr_reset_filter(int oneline
, int ifindex
)
1752 memset(&filter
, 0, sizeof(filter
));
1753 filter
.oneline
= oneline
;
1754 filter
.ifindex
= ifindex
;
1757 static int default_scope(inet_prefix
*lcl
)
1759 if (lcl
->family
== AF_INET
) {
1760 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1761 return RT_SCOPE_HOST
;
1766 static bool ipaddr_is_multicast(inet_prefix
*a
)
1768 if (a
->family
== AF_INET
)
1769 return IN_MULTICAST(ntohl(a
->data
[0]));
1770 else if (a
->family
== AF_INET6
)
1771 return IN6_IS_ADDR_MULTICAST(a
->data
);
1776 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1780 struct ifaddrmsg ifa
;
1785 char *lcl_arg
= NULL
;
1786 char *valid_lftp
= NULL
;
1787 char *preferred_lftp
= NULL
;
1795 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1796 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1797 struct ifa_cacheinfo cinfo
;
1798 unsigned int ifa_flags
= 0;
1800 memset(&req
, 0, sizeof(req
));
1802 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1803 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1804 req
.n
.nlmsg_type
= cmd
;
1805 req
.ifa
.ifa_family
= preferred_family
;
1808 if (strcmp(*argv
, "peer") == 0 ||
1809 strcmp(*argv
, "remote") == 0) {
1813 duparg("peer", *argv
);
1814 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1815 peer_len
= peer
.bytelen
;
1816 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1817 req
.ifa
.ifa_family
= peer
.family
;
1818 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1819 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1820 } else if (matches(*argv
, "broadcast") == 0 ||
1821 strcmp(*argv
, "brd") == 0) {
1825 duparg("broadcast", *argv
);
1826 if (strcmp(*argv
, "+") == 0)
1828 else if (strcmp(*argv
, "-") == 0)
1831 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1832 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1833 req
.ifa
.ifa_family
= addr
.family
;
1834 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1835 brd_len
= addr
.bytelen
;
1837 } else if (strcmp(*argv
, "anycast") == 0) {
1841 duparg("anycast", *argv
);
1842 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1843 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1844 req
.ifa
.ifa_family
= addr
.family
;
1845 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1846 any_len
= addr
.bytelen
;
1847 } else if (strcmp(*argv
, "scope") == 0) {
1850 if (rtnl_rtscope_a2n(&scope
, *argv
))
1851 invarg("invalid scope value.", *argv
);
1852 req
.ifa
.ifa_scope
= scope
;
1854 } else if (strcmp(*argv
, "dev") == 0) {
1857 } else if (strcmp(*argv
, "label") == 0) {
1860 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1861 } else if (matches(*argv
, "valid_lft") == 0) {
1863 duparg("valid_lft", *argv
);
1866 if (set_lifetime(&valid_lft
, *argv
))
1867 invarg("valid_lft value", *argv
);
1868 } else if (matches(*argv
, "preferred_lft") == 0) {
1870 duparg("preferred_lft", *argv
);
1872 preferred_lftp
= *argv
;
1873 if (set_lifetime(&preferred_lft
, *argv
))
1874 invarg("preferred_lft value", *argv
);
1875 } else if (strcmp(*argv
, "home") == 0) {
1876 ifa_flags
|= IFA_F_HOMEADDRESS
;
1877 } else if (strcmp(*argv
, "nodad") == 0) {
1878 ifa_flags
|= IFA_F_NODAD
;
1879 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1880 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1881 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1882 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1883 } else if (strcmp(*argv
, "autojoin") == 0) {
1884 ifa_flags
|= IFA_F_MCAUTOJOIN
;
1886 if (strcmp(*argv
, "local") == 0) {
1889 if (matches(*argv
, "help") == 0)
1892 duparg2("local", *argv
);
1894 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1895 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1896 req
.ifa
.ifa_family
= lcl
.family
;
1897 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1898 local_len
= lcl
.bytelen
;
1902 if (ifa_flags
<= 0xff)
1903 req
.ifa
.ifa_flags
= ifa_flags
;
1905 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1908 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1911 if (l
&& matches(d
, l
) != 0) {
1912 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1916 if (peer_len
== 0 && local_len
) {
1917 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1919 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1920 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1921 " This special behaviour is likely to disappear in further releases,\n" \
1922 " fix your scripts!\n", lcl_arg
, local_len
*8);
1925 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1928 if (req
.ifa
.ifa_prefixlen
== 0)
1929 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1931 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1934 if (req
.ifa
.ifa_family
!= AF_INET
) {
1935 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1939 if (brd
.bitlen
<= 30) {
1940 for (i
= 31; i
>= brd
.bitlen
; i
--) {
1942 brd
.data
[0] |= htonl(1<<(31-i
));
1944 brd
.data
[0] &= ~htonl(1<<(31-i
));
1946 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1947 brd_len
= brd
.bytelen
;
1950 if (!scoped
&& cmd
!= RTM_DELADDR
)
1951 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1953 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1954 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1958 if (valid_lftp
|| preferred_lftp
) {
1960 fprintf(stderr
, "valid_lft is zero\n");
1963 if (valid_lft
< preferred_lft
) {
1964 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1968 memset(&cinfo
, 0, sizeof(cinfo
));
1969 cinfo
.ifa_prefered
= preferred_lft
;
1970 cinfo
.ifa_valid
= valid_lft
;
1971 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1975 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
1976 fprintf(stderr
, "autojoin needs multicast address\n");
1980 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
1986 int do_ipaddr(int argc
, char **argv
)
1989 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
1990 if (matches(*argv
, "add") == 0)
1991 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1992 if (matches(*argv
, "change") == 0 ||
1993 strcmp(*argv
, "chg") == 0)
1994 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
1995 if (matches(*argv
, "replace") == 0)
1996 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
1997 if (matches(*argv
, "delete") == 0)
1998 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
1999 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
2000 || matches(*argv
, "lst") == 0)
2001 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
2002 if (matches(*argv
, "flush") == 0)
2003 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
2004 if (matches(*argv
, "save") == 0)
2005 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
2006 if (matches(*argv
, "showdump") == 0)
2007 return ipaddr_showdump();
2008 if (matches(*argv
, "restore") == 0)
2009 return ipaddr_restore();
2010 if (matches(*argv
, "help") == 0)
2012 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv
);