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 addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
75 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
76 fprintf(stderr
, " ip addr del IFADDR dev STRING [mngtmpaddr]\n");
77 fprintf(stderr
, " ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
78 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [up]\n");
79 fprintf(stderr
, " ip addr {showdump|restore}\n");
80 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
81 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
82 fprintf(stderr
, " [ label STRING ] [ 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
);
141 fprintf(f
, "state ");
142 if (strcmp(oper_states
[state
], "UP") == 0)
143 color_fprintf(f
, COLOR_OPERSTATE_UP
, "%s ", oper_states
[state
]);
144 else if (strcmp(oper_states
[state
], "DOWN") == 0)
145 color_fprintf(f
, COLOR_OPERSTATE_DOWN
, "%s ", oper_states
[state
]);
147 fprintf(f
, "%s ", oper_states
[state
]);
151 int get_operstate(const char *name
)
155 for (i
= 0; i
< sizeof(oper_states
)/sizeof(oper_states
[0]); i
++)
156 if (strcasecmp(name
, oper_states
[i
]) == 0)
161 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
166 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
169 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
174 memset(&ifr
, 0, sizeof(ifr
));
175 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
176 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
177 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
185 fprintf(f
, "qlen %d", qlen
);
188 static const char *link_modes
[] = {
192 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
194 unsigned int mode
= rta_getattr_u8(tb
);
196 if (mode
>= sizeof(link_modes
) / sizeof(link_modes
[0]))
197 fprintf(f
, "mode %d ", mode
);
199 fprintf(f
, "mode %s ", link_modes
[mode
]);
202 static char *parse_link_kind(struct rtattr
*tb
)
204 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
206 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
208 if (linkinfo
[IFLA_INFO_KIND
])
209 return RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
214 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
216 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
217 struct link_util
*lu
;
218 struct link_util
*slave_lu
;
222 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
224 if (linkinfo
[IFLA_INFO_KIND
]) {
225 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
227 fprintf(fp
, "%s", _SL_
);
228 fprintf(fp
, " %s ", kind
);
230 lu
= get_link_kind(kind
);
231 if (lu
&& lu
->print_opt
) {
232 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
234 if (linkinfo
[IFLA_INFO_DATA
]) {
235 parse_rtattr_nested(attr
, lu
->maxattr
,
236 linkinfo
[IFLA_INFO_DATA
]);
239 lu
->print_opt(lu
, fp
, data
);
241 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
243 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
247 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
248 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
250 fprintf(fp
, "%s", _SL_
);
251 fprintf(fp
, " %s_slave ", slave_kind
);
253 slave_lu
= get_link_slave_kind(slave_kind
);
254 if (slave_lu
&& slave_lu
->print_opt
) {
255 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
257 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
258 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
259 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
262 slave_lu
->print_opt(slave_lu
, fp
, data
);
267 static void print_af_spec(FILE *fp
, struct rtattr
*af_spec_attr
)
269 struct rtattr
*inet6_attr
;
270 struct rtattr
*tb
[IFLA_INET6_MAX
+ 1];
272 inet6_attr
= parse_rtattr_one_nested(AF_INET6
, af_spec_attr
);
276 parse_rtattr_nested(tb
, IFLA_INET6_MAX
, inet6_attr
);
278 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
279 switch (rta_getattr_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
])) {
280 case IN6_ADDR_GEN_MODE_EUI64
:
281 fprintf(fp
, "addrgenmode eui64 ");
283 case IN6_ADDR_GEN_MODE_NONE
:
284 fprintf(fp
, "addrgenmode none ");
290 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
292 struct ifla_vf_mac
*vf_mac
;
293 struct ifla_vf_vlan
*vf_vlan
;
294 struct ifla_vf_tx_rate
*vf_tx_rate
;
295 struct ifla_vf_spoofchk
*vf_spoofchk
;
296 struct ifla_vf_link_state
*vf_linkstate
;
297 struct rtattr
*vf
[IFLA_VF_MAX
+ 1] = {};
301 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
302 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
306 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
308 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
309 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
310 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
312 /* Check if the spoof checking vf info type is supported by
315 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
316 vf
[IFLA_VF_TX_RATE
]->rta_len
);
318 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
321 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
324 /* Check if the link state vf info type is supported by
327 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_SPOOFCHK
] +
328 vf
[IFLA_VF_SPOOFCHK
]->rta_len
);
330 if (tmp
->rta_type
!= IFLA_VF_LINK_STATE
)
333 vf_linkstate
= RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
337 fprintf(fp
, "\n vf %d MAC %s", vf_mac
->vf
,
338 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
339 ETH_ALEN
, 0, b1
, sizeof(b1
)));
341 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
343 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
344 if (vf_tx_rate
->rate
)
345 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
347 if (vf
[IFLA_VF_RATE
]) {
348 struct ifla_vf_rate
*vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
350 if (vf_rate
->max_tx_rate
)
351 fprintf(fp
, ", max_tx_rate %dMbps", vf_rate
->max_tx_rate
);
352 if (vf_rate
->min_tx_rate
)
353 fprintf(fp
, ", min_tx_rate %dMbps", vf_rate
->min_tx_rate
);
356 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
357 if (vf_spoofchk
->setting
)
358 fprintf(fp
, ", spoof checking on");
360 fprintf(fp
, ", spoof checking off");
363 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
364 fprintf(fp
, ", link-state auto");
365 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
366 fprintf(fp
, ", link-state enable");
368 fprintf(fp
, ", link-state disable");
372 static void print_num(FILE *fp
, unsigned width
, uint64_t count
)
374 const char *prefix
= "kMGTPE";
375 const unsigned int base
= use_iec
? 1024 : 1000;
378 uint8_t precision
= 2;
381 if (!human_readable
|| count
< base
) {
382 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
386 /* increase value by a factor of 1000/1024 and print
387 * if result is something a human can read */
390 if (count
/ base
< powi
)
398 /* try to guess a good number of digits for precision */
399 for (; precision
> 0; precision
--) {
401 if (count
/ powi
< powj
)
405 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
406 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
408 fprintf(fp
, "%-*s ", width
, buf
);
411 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
412 const struct rtattr
*carrier_changes
)
415 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
416 s
->rx_compressed
? "compressed" : "", _SL_
);
419 print_num(fp
, 10, s
->rx_bytes
);
420 print_num(fp
, 8, s
->rx_packets
);
421 print_num(fp
, 7, s
->rx_errors
);
422 print_num(fp
, 7, s
->rx_dropped
);
423 print_num(fp
, 7, s
->rx_over_errors
);
424 print_num(fp
, 7, s
->multicast
);
425 if (s
->rx_compressed
)
426 print_num(fp
, 7, s
->rx_compressed
);
429 if (show_stats
> 1) {
430 fprintf(fp
, "%s", _SL_
);
431 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
434 print_num(fp
, 8, s
->rx_length_errors
);
435 print_num(fp
, 7, s
->rx_crc_errors
);
436 print_num(fp
, 7, s
->rx_frame_errors
);
437 print_num(fp
, 7, s
->rx_fifo_errors
);
438 print_num(fp
, 7, s
->rx_missed_errors
);
440 fprintf(fp
, "%s", _SL_
);
443 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
444 s
->tx_compressed
? "compressed" : "", _SL_
);
448 print_num(fp
, 10, s
->tx_bytes
);
449 print_num(fp
, 8, s
->tx_packets
);
450 print_num(fp
, 7, s
->tx_errors
);
451 print_num(fp
, 7, s
->tx_dropped
);
452 print_num(fp
, 7, s
->tx_carrier_errors
);
453 print_num(fp
, 7, s
->collisions
);
454 if (s
->tx_compressed
)
455 print_num(fp
, 7, s
->tx_compressed
);
458 if (show_stats
> 1) {
459 fprintf(fp
, "%s", _SL_
);
460 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
462 fprintf(fp
, " transns");
463 fprintf(fp
, "%s", _SL_
);
466 print_num(fp
, 8, s
->tx_aborted_errors
);
467 print_num(fp
, 7, s
->tx_fifo_errors
);
468 print_num(fp
, 7, s
->tx_window_errors
);
469 print_num(fp
, 7, s
->tx_heartbeat_errors
);
471 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
475 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
476 const struct rtattr
*carrier_changes
)
479 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
480 s
->rx_compressed
? "compressed" : "", _SL_
);
484 print_num(fp
, 10, s
->rx_bytes
);
485 print_num(fp
, 8, s
->rx_packets
);
486 print_num(fp
, 7, s
->rx_errors
);
487 print_num(fp
, 7, s
->rx_dropped
);
488 print_num(fp
, 7, s
->rx_over_errors
);
489 print_num(fp
, 7, s
->multicast
);
490 if (s
->rx_compressed
)
491 print_num(fp
, 7, s
->rx_compressed
);
494 if (show_stats
> 1) {
495 fprintf(fp
, "%s", _SL_
);
496 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
498 print_num(fp
, 8, s
->rx_length_errors
);
499 print_num(fp
, 7, s
->rx_crc_errors
);
500 print_num(fp
, 7, s
->rx_frame_errors
);
501 print_num(fp
, 7, s
->rx_fifo_errors
);
502 print_num(fp
, 7, s
->rx_missed_errors
);
504 fprintf(fp
, "%s", _SL_
);
507 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
508 s
->tx_compressed
? "compressed" : "", _SL_
);
511 print_num(fp
, 10, s
->tx_bytes
);
512 print_num(fp
, 8, s
->tx_packets
);
513 print_num(fp
, 7, s
->tx_errors
);
514 print_num(fp
, 7, s
->tx_dropped
);
515 print_num(fp
, 7, s
->tx_carrier_errors
);
516 print_num(fp
, 7, s
->collisions
);
517 if (s
->tx_compressed
)
518 print_num(fp
, 7, s
->tx_compressed
);
521 if (show_stats
> 1) {
522 fprintf(fp
, "%s", _SL_
);
523 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
525 fprintf(fp
, " transns");
526 fprintf(fp
, "%s", _SL_
);
529 print_num(fp
, 8, s
->tx_aborted_errors
);
530 print_num(fp
, 7, s
->tx_fifo_errors
);
531 print_num(fp
, 7, s
->tx_window_errors
);
532 print_num(fp
, 7, s
->tx_heartbeat_errors
);
534 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
538 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
540 if (tb
[IFLA_STATS64
])
541 print_link_stats64(fp
, RTA_DATA(tb
[IFLA_STATS64
]),
542 tb
[IFLA_CARRIER_CHANGES
]);
543 else if (tb
[IFLA_STATS
])
544 print_link_stats32(fp
, RTA_DATA(tb
[IFLA_STATS
]),
545 tb
[IFLA_CARRIER_CHANGES
]);
548 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
550 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
551 struct rtattr
* tb
[IFLA_MAX
+1];
553 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
554 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
555 __print_link_stats(fp
, tb
);
556 fprintf(fp
, "%s", _SL_
);
559 int print_linkinfo(const struct sockaddr_nl
*who
,
560 struct nlmsghdr
*n
, void *arg
)
562 FILE *fp
= (FILE*)arg
;
563 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
564 struct rtattr
* tb
[IFLA_MAX
+1];
565 int len
= n
->nlmsg_len
;
568 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
571 len
-= NLMSG_LENGTH(sizeof(*ifi
));
575 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
577 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
580 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
581 if (tb
[IFLA_IFNAME
] == NULL
) {
582 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
585 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
586 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
589 if (tb
[IFLA_GROUP
]) {
590 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
591 if (filter
.group
!= -1 && group
!= filter
.group
)
595 if (tb
[IFLA_MASTER
]) {
596 int master
= *(int*)RTA_DATA(tb
[IFLA_MASTER
]);
597 if (filter
.master
> 0 && master
!= filter
.master
)
600 else if (filter
.master
> 0)
604 if (tb
[IFLA_LINKINFO
]) {
605 char *kind
= parse_link_kind(tb
[IFLA_LINKINFO
]);
607 if (strcmp(kind
, filter
.kind
))
614 if (n
->nlmsg_type
== RTM_DELLINK
)
615 fprintf(fp
, "Deleted ");
617 fprintf(fp
, "%d: ", ifi
->ifi_index
);
618 color_fprintf(fp
, COLOR_IFNAME
, "%s",
619 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
623 int iflink
= *(int*)RTA_DATA(tb
[IFLA_LINK
]);
625 fprintf(fp
, "@NONE: ");
627 if (tb
[IFLA_LINK_NETNSID
])
628 fprintf(fp
, "@if%d: ", iflink
);
630 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
631 m_flag
= ll_index_to_flags(iflink
);
632 m_flag
= !(m_flag
& IFF_UP
);
638 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
641 fprintf(fp
, "mtu %u ", *(int*)RTA_DATA(tb
[IFLA_MTU
]));
643 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
644 if (tb
[IFLA_MASTER
]) {
646 fprintf(fp
, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
649 if (tb
[IFLA_PHYS_PORT_ID
]) {
651 fprintf(fp
, "portid %s ",
652 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
653 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
657 if (tb
[IFLA_OPERSTATE
])
658 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
660 if (do_link
&& tb
[IFLA_LINKMODE
])
661 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
663 if (tb
[IFLA_GROUP
]) {
665 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
666 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
669 if (filter
.showqueue
)
670 print_queuelen(fp
, tb
);
672 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
674 fprintf(fp
, "%s", _SL_
);
675 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
677 if (tb
[IFLA_ADDRESS
]) {
678 color_fprintf(fp
, COLOR_MAC
, "%s",
679 ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
680 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
684 if (tb
[IFLA_BROADCAST
]) {
685 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
686 fprintf(fp
, " peer ");
688 fprintf(fp
, " brd ");
689 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
690 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
696 if (tb
[IFLA_LINK_NETNSID
]) {
697 int id
= *(int*)RTA_DATA(tb
[IFLA_LINK_NETNSID
]);
700 fprintf(fp
, " link-netnsid %d", id
);
702 fprintf(fp
, " link-netnsid unknown");
705 if (tb
[IFLA_PROMISCUITY
] && show_details
)
706 fprintf(fp
, " promiscuity %u ",
707 *(int*)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
709 if (tb
[IFLA_LINKINFO
] && show_details
)
710 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
712 if (do_link
&& tb
[IFLA_AF_SPEC
] && show_details
)
713 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
715 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
716 fprintf(fp
, "%s alias %s", _SL_
,
717 rta_getattr_str(tb
[IFLA_IFALIAS
]));
720 if (do_link
&& show_stats
) {
721 fprintf(fp
, "%s", _SL_
);
722 __print_link_stats(fp
, tb
);
725 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
726 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
727 int rem
= RTA_PAYLOAD(vflist
);
728 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
737 static int flush_update(void)
739 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
740 perror("Failed to send flush request");
747 static int set_lifetime(unsigned int *lifetime
, char *argv
)
749 if (strcmp(argv
, "forever") == 0)
750 *lifetime
= INFINITY_LIFE_TIME
;
751 else if (get_u32(lifetime
, argv
, 0))
757 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
758 struct rtattr
*ifa_flags_attr
)
760 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
764 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
768 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
769 int len
= n
->nlmsg_len
;
771 /* Use local copy of ifa_flags to not interfere with filtering code */
772 unsigned int ifa_flags
;
773 struct rtattr
* rta_tb
[IFA_MAX
+1];
777 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
779 len
-= NLMSG_LENGTH(sizeof(*ifa
));
781 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
785 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
788 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
789 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
791 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
793 if (!rta_tb
[IFA_LOCAL
])
794 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
795 if (!rta_tb
[IFA_ADDRESS
])
796 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
798 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
800 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
802 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
807 if (rta_tb
[IFA_LABEL
])
808 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
810 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
811 if (fnmatch(filter
.label
, label
, 0) != 0)
814 if (filter
.pfx
.family
) {
815 if (rta_tb
[IFA_LOCAL
]) {
817 memset(&dst
, 0, sizeof(dst
));
818 dst
.family
= ifa
->ifa_family
;
819 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
820 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
825 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
830 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
834 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
835 memcpy(fn
, n
, n
->nlmsg_len
);
836 fn
->nlmsg_type
= RTM_DELADDR
;
837 fn
->nlmsg_flags
= NLM_F_REQUEST
;
838 fn
->nlmsg_seq
= ++rth
.seq
;
839 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
845 if (n
->nlmsg_type
== RTM_DELADDR
)
846 fprintf(fp
, "Deleted ");
848 if (filter
.oneline
|| filter
.flushb
)
849 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
850 if (ifa
->ifa_family
== AF_INET
)
851 fprintf(fp
, " inet ");
852 else if (ifa
->ifa_family
== AF_INET6
)
853 fprintf(fp
, " inet6 ");
854 else if (ifa
->ifa_family
== AF_DECnet
)
855 fprintf(fp
, " dnet ");
856 else if (ifa
->ifa_family
== AF_IPX
)
857 fprintf(fp
, " ipx ");
859 fprintf(fp
, " family %d ", ifa
->ifa_family
);
861 if (rta_tb
[IFA_LOCAL
]) {
862 if (ifa
->ifa_family
== AF_INET
)
863 color_fprintf(fp
, COLOR_INET
, "%s", format_host(ifa
->ifa_family
,
864 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
865 RTA_DATA(rta_tb
[IFA_LOCAL
]),
866 abuf
, sizeof(abuf
)));
867 else if (ifa
->ifa_family
== AF_INET6
)
868 color_fprintf(fp
, COLOR_INET6
, "%s", format_host(ifa
->ifa_family
,
869 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
870 RTA_DATA(rta_tb
[IFA_LOCAL
]),
871 abuf
, sizeof(abuf
)));
873 fprintf(fp
, "%s", format_host(ifa
->ifa_family
,
874 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
875 RTA_DATA(rta_tb
[IFA_LOCAL
]),
876 abuf
, sizeof(abuf
)));
878 if (rta_tb
[IFA_ADDRESS
] == NULL
||
879 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]), RTA_DATA(rta_tb
[IFA_LOCAL
]),
880 ifa
->ifa_family
== AF_INET
? 4 : 16) == 0) {
881 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
883 fprintf(fp
, " peer %s/%d ",
884 format_host(ifa
->ifa_family
,
885 RTA_PAYLOAD(rta_tb
[IFA_ADDRESS
]),
886 RTA_DATA(rta_tb
[IFA_ADDRESS
]),
892 if (rta_tb
[IFA_BROADCAST
]) {
893 fprintf(fp
, "brd %s ",
894 format_host(ifa
->ifa_family
,
895 RTA_PAYLOAD(rta_tb
[IFA_BROADCAST
]),
896 RTA_DATA(rta_tb
[IFA_BROADCAST
]),
897 abuf
, sizeof(abuf
)));
899 if (rta_tb
[IFA_ANYCAST
]) {
900 fprintf(fp
, "any %s ",
901 format_host(ifa
->ifa_family
,
902 RTA_PAYLOAD(rta_tb
[IFA_ANYCAST
]),
903 RTA_DATA(rta_tb
[IFA_ANYCAST
]),
904 abuf
, sizeof(abuf
)));
906 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
907 if (ifa_flags
& IFA_F_SECONDARY
) {
908 ifa_flags
&= ~IFA_F_SECONDARY
;
909 if (ifa
->ifa_family
== AF_INET6
)
910 fprintf(fp
, "temporary ");
912 fprintf(fp
, "secondary ");
914 if (ifa_flags
& IFA_F_TENTATIVE
) {
915 ifa_flags
&= ~IFA_F_TENTATIVE
;
916 fprintf(fp
, "tentative ");
918 if (ifa_flags
& IFA_F_DEPRECATED
) {
919 ifa_flags
&= ~IFA_F_DEPRECATED
;
921 fprintf(fp
, "deprecated ");
923 if (ifa_flags
& IFA_F_HOMEADDRESS
) {
924 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
925 fprintf(fp
, "home ");
927 if (ifa_flags
& IFA_F_NODAD
) {
928 ifa_flags
&= ~IFA_F_NODAD
;
929 fprintf(fp
, "nodad ");
931 if (ifa_flags
& IFA_F_MANAGETEMPADDR
) {
932 ifa_flags
&= ~IFA_F_MANAGETEMPADDR
;
933 fprintf(fp
, "mngtmpaddr ");
935 if (ifa_flags
& IFA_F_NOPREFIXROUTE
) {
936 ifa_flags
&= ~IFA_F_NOPREFIXROUTE
;
937 fprintf(fp
, "noprefixroute ");
939 if (ifa_flags
& IFA_F_MCAUTOJOIN
) {
940 ifa_flags
&= ~IFA_F_MCAUTOJOIN
;
941 fprintf(fp
, "autojoin ");
943 if (!(ifa_flags
& IFA_F_PERMANENT
)) {
944 fprintf(fp
, "dynamic ");
946 ifa_flags
&= ~IFA_F_PERMANENT
;
947 if (ifa_flags
& IFA_F_DADFAILED
) {
948 ifa_flags
&= ~IFA_F_DADFAILED
;
949 fprintf(fp
, "dadfailed ");
952 fprintf(fp
, "flags %02x ", ifa_flags
);
953 if (rta_tb
[IFA_LABEL
])
954 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
955 if (rta_tb
[IFA_CACHEINFO
]) {
956 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
957 fprintf(fp
, "%s", _SL_
);
958 fprintf(fp
, " valid_lft ");
959 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
960 fprintf(fp
, "forever");
962 fprintf(fp
, "%usec", ci
->ifa_valid
);
963 fprintf(fp
, " preferred_lft ");
964 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
965 fprintf(fp
, "forever");
968 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
970 fprintf(fp
, "%usec", ci
->ifa_prefered
);
978 static int print_addrinfo_primary(const struct sockaddr_nl
*who
,
979 struct nlmsghdr
*n
, void *arg
)
981 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
983 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
986 return print_addrinfo(who
, n
, arg
);
989 static int print_addrinfo_secondary(const struct sockaddr_nl
*who
,
990 struct nlmsghdr
*n
, void *arg
)
992 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
994 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
997 return print_addrinfo(who
, n
, arg
);
1002 struct nlmsg_list
*next
;
1008 struct nlmsg_list
*head
;
1009 struct nlmsg_list
*tail
;
1012 static int print_selected_addrinfo(struct ifinfomsg
*ifi
,
1013 struct nlmsg_list
*ainfo
, FILE *fp
)
1015 for ( ;ainfo
; ainfo
= ainfo
->next
) {
1016 struct nlmsghdr
*n
= &ainfo
->h
;
1017 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1019 if (n
->nlmsg_type
!= RTM_NEWADDR
)
1022 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
1025 if (ifa
->ifa_index
!= ifi
->ifi_index
||
1026 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
1029 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
1032 print_addrinfo(NULL
, n
, fp
);
1038 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1041 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
1042 struct nlmsg_list
*h
;
1044 h
= malloc(n
->nlmsg_len
+sizeof(void*));
1048 memcpy(&h
->h
, n
, n
->nlmsg_len
);
1052 lchain
->tail
->next
= h
;
1057 ll_remember_index(who
, n
, NULL
);
1061 static __u32 ipadd_dump_magic
= 0x47361222;
1063 static int ipadd_save_prep(void)
1067 if (isatty(STDOUT_FILENO
)) {
1068 fprintf(stderr
, "Not sending a binary stream to stdout\n");
1072 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
1073 if (ret
!= sizeof(ipadd_dump_magic
)) {
1074 fprintf(stderr
, "Can't write magic to dump file\n");
1081 static int ipadd_dump_check_magic(void)
1086 if (isatty(STDIN_FILENO
)) {
1087 fprintf(stderr
, "Can't restore addr dump from a terminal\n");
1091 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
1092 if (magic
!= ipadd_dump_magic
) {
1093 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1100 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1105 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1106 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1107 fprintf(stderr
, "Short write while saving nlmsg\n");
1111 return ret
== n
->nlmsg_len
? 0 : ret
;
1114 static int show_handler(const struct sockaddr_nl
*nl
,
1115 struct rtnl_ctrl_data
*ctrl
,
1116 struct nlmsghdr
*n
, void *arg
)
1118 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1120 printf("if%d:\n", ifa
->ifa_index
);
1121 print_addrinfo(NULL
, n
, stdout
);
1125 static int ipaddr_showdump(void)
1127 if (ipadd_dump_check_magic())
1130 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1133 static int restore_handler(const struct sockaddr_nl
*nl
,
1134 struct rtnl_ctrl_data
*ctrl
,
1135 struct nlmsghdr
*n
, void *arg
)
1139 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1143 ret
= rtnl_talk(&rth
, n
, n
, sizeof(*n
));
1144 if ((ret
< 0) && (errno
== EEXIST
))
1150 static int ipaddr_restore(void)
1152 if (ipadd_dump_check_magic())
1155 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1158 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1160 struct nlmsg_list
*l
, *n
;
1162 for (l
= info
->head
; l
; l
= n
) {
1168 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1170 struct nlmsg_list
*l
, **lp
;
1173 while ( (l
= *lp
) != NULL
) {
1175 int missing_net_address
= 1;
1176 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1177 struct nlmsg_list
*a
;
1179 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1180 struct nlmsghdr
*n
= &a
->h
;
1181 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1182 struct rtattr
*tb
[IFA_MAX
+ 1];
1183 unsigned int ifa_flags
;
1185 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1187 missing_net_address
= 0;
1188 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1190 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1193 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1194 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1196 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1198 if (filter
.pfx
.family
|| filter
.label
) {
1200 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1202 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
1204 memset(&dst
, 0, sizeof(dst
));
1205 dst
.family
= ifa
->ifa_family
;
1206 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
1207 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1214 label
= RTA_DATA(tb
[IFA_LABEL
]);
1216 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1217 if (fnmatch(filter
.label
, label
, 0) != 0)
1225 if (missing_net_address
&&
1226 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1236 static int ipaddr_flush(void)
1239 char flushb
[4096-512];
1241 filter
.flushb
= flushb
;
1243 filter
.flushe
= sizeof(flushb
);
1245 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1246 const struct rtnl_dump_filter_arg a
[3] = {
1248 .filter
= print_addrinfo_secondary
,
1252 .filter
= print_addrinfo_primary
,
1260 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1261 perror("Cannot send dump request");
1265 if (rtnl_dump_filter_l(&rth
, a
) < 0) {
1266 fprintf(stderr
, "Flush terminated\n");
1269 if (filter
.flushed
== 0) {
1273 printf("Nothing to flush.\n");
1275 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
1281 if (flush_update() < 0)
1285 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1289 /* If we are flushing, and specifying primary, then we
1290 * want to flush only a single round. Otherwise, we'll
1291 * start flushing secondaries that were promoted to
1294 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1297 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1302 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1304 struct nlmsg_chain linfo
= { NULL
, NULL
};
1305 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1306 struct nlmsg_list
*l
;
1307 char *filter_dev
= NULL
;
1310 ipaddr_reset_filter(oneline
, 0);
1311 filter
.showqueue
= 1;
1313 if (filter
.family
== AF_UNSPEC
)
1314 filter
.family
= preferred_family
;
1318 if (action
== IPADD_FLUSH
) {
1320 fprintf(stderr
, "Flush requires arguments.\n");
1324 if (filter
.family
== AF_PACKET
) {
1325 fprintf(stderr
, "Cannot flush link addresses.\n");
1331 if (strcmp(*argv
, "to") == 0) {
1333 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1334 if (filter
.family
== AF_UNSPEC
)
1335 filter
.family
= filter
.pfx
.family
;
1336 } else if (strcmp(*argv
, "scope") == 0) {
1339 filter
.scopemask
= -1;
1340 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1341 if (strcmp(*argv
, "all") != 0)
1342 invarg("invalid \"scope\"\n", *argv
);
1343 scope
= RT_SCOPE_NOWHERE
;
1344 filter
.scopemask
= 0;
1346 filter
.scope
= scope
;
1347 } else if (strcmp(*argv
, "up") == 0) {
1349 } else if (strcmp(*argv
, "dynamic") == 0) {
1350 filter
.flags
&= ~IFA_F_PERMANENT
;
1351 filter
.flagmask
|= IFA_F_PERMANENT
;
1352 } else if (strcmp(*argv
, "permanent") == 0) {
1353 filter
.flags
|= IFA_F_PERMANENT
;
1354 filter
.flagmask
|= IFA_F_PERMANENT
;
1355 } else if (strcmp(*argv
, "secondary") == 0 ||
1356 strcmp(*argv
, "temporary") == 0) {
1357 filter
.flags
|= IFA_F_SECONDARY
;
1358 filter
.flagmask
|= IFA_F_SECONDARY
;
1359 } else if (strcmp(*argv
, "primary") == 0) {
1360 filter
.flags
&= ~IFA_F_SECONDARY
;
1361 filter
.flagmask
|= IFA_F_SECONDARY
;
1362 } else if (strcmp(*argv
, "tentative") == 0) {
1363 filter
.flags
|= IFA_F_TENTATIVE
;
1364 filter
.flagmask
|= IFA_F_TENTATIVE
;
1365 } else if (strcmp(*argv
, "-tentative") == 0) {
1366 filter
.flags
&= ~IFA_F_TENTATIVE
;
1367 filter
.flagmask
|= IFA_F_TENTATIVE
;
1368 } else if (strcmp(*argv
, "deprecated") == 0) {
1369 filter
.flags
|= IFA_F_DEPRECATED
;
1370 filter
.flagmask
|= IFA_F_DEPRECATED
;
1371 } else if (strcmp(*argv
, "-deprecated") == 0) {
1372 filter
.flags
&= ~IFA_F_DEPRECATED
;
1373 filter
.flagmask
|= IFA_F_DEPRECATED
;
1374 } else if (strcmp(*argv
, "home") == 0) {
1375 filter
.flags
|= IFA_F_HOMEADDRESS
;
1376 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1377 } else if (strcmp(*argv
, "nodad") == 0) {
1378 filter
.flags
|= IFA_F_NODAD
;
1379 filter
.flagmask
|= IFA_F_NODAD
;
1380 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1381 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1382 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1383 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1384 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1385 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1386 } else if (strcmp(*argv
, "autojoin") == 0) {
1387 filter
.flags
|= IFA_F_MCAUTOJOIN
;
1388 filter
.flagmask
|= IFA_F_MCAUTOJOIN
;
1389 } else if (strcmp(*argv
, "dadfailed") == 0) {
1390 filter
.flags
|= IFA_F_DADFAILED
;
1391 filter
.flagmask
|= IFA_F_DADFAILED
;
1392 } else if (strcmp(*argv
, "-dadfailed") == 0) {
1393 filter
.flags
&= ~IFA_F_DADFAILED
;
1394 filter
.flagmask
|= IFA_F_DADFAILED
;
1395 } else if (strcmp(*argv
, "label") == 0) {
1397 filter
.label
= *argv
;
1398 } else if (strcmp(*argv
, "group") == 0) {
1400 if (rtnl_group_a2n(&filter
.group
, *argv
))
1401 invarg("Invalid \"group\" value\n", *argv
);
1402 } else if (strcmp(*argv
, "master") == 0) {
1405 ifindex
= ll_name_to_index(*argv
);
1407 invarg("Device does not exist\n", *argv
);
1408 filter
.master
= ifindex
;
1409 } else if (do_link
&& strcmp(*argv
, "type") == 0) {
1411 filter
.kind
= *argv
;
1413 if (strcmp(*argv
, "dev") == 0) {
1416 if (matches(*argv
, "help") == 0)
1419 duparg2("dev", *argv
);
1426 filter
.ifindex
= ll_name_to_index(filter_dev
);
1427 if (filter
.ifindex
<= 0) {
1428 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1433 if (action
== IPADD_FLUSH
)
1434 return ipaddr_flush();
1436 if (action
== IPADD_SAVE
) {
1437 if (ipadd_save_prep())
1440 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1441 perror("Cannot send dump request");
1445 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1446 fprintf(stderr
, "Save terminated\n");
1454 * If only filter_dev present and none of the other
1455 * link filters are present, use RTM_GETLINK to get
1458 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1459 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1460 perror("Cannot send link get request");
1466 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
1467 perror("Cannot send dump request");
1471 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1472 fprintf(stderr
, "Dump terminated\n");
1476 if (filter
.family
!= AF_PACKET
) {
1480 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1481 perror("Cannot send dump request");
1485 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1486 fprintf(stderr
, "Dump terminated\n");
1490 ipaddr_filter(&linfo
, &ainfo
);
1493 for (l
= linfo
.head
; l
; l
= l
->next
) {
1496 if (no_link
|| (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1497 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1498 if (filter
.family
!= AF_PACKET
)
1499 print_selected_addrinfo(ifi
,
1500 ainfo
.head
, stdout
);
1501 if (res
> 0 && !do_link
&& show_stats
)
1502 print_link_stats(stdout
, &l
->h
);
1507 free_nlmsg_chain(&ainfo
);
1508 free_nlmsg_chain(&linfo
);
1514 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1516 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1517 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1518 struct ifla_vf_rate
*vf_rate
;
1521 rem
= RTA_PAYLOAD(vflist
);
1523 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1524 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1525 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1526 if (vf_rate
->vf
== vfnum
) {
1527 *min
= vf_rate
->min_tx_rate
;
1528 *max
= vf_rate
->max_tx_rate
;
1532 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1536 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1538 struct nlmsg_chain linfo
= { NULL
, NULL
};
1539 struct rtattr
*tb
[IFLA_MAX
+1];
1540 struct ifinfomsg
*ifi
;
1541 struct nlmsg_list
*l
;
1545 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1546 perror("Cannot send dump request");
1549 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1550 fprintf(stderr
, "Dump terminated\n");
1553 for (l
= linfo
.head
; l
; l
= l
->next
) {
1555 ifi
= NLMSG_DATA(n
);
1557 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1558 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1561 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1563 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1564 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1570 int ipaddr_list_link(int argc
, char **argv
)
1572 preferred_family
= AF_PACKET
;
1574 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1577 void ipaddr_reset_filter(int oneline
, int ifindex
)
1579 memset(&filter
, 0, sizeof(filter
));
1580 filter
.oneline
= oneline
;
1581 filter
.ifindex
= ifindex
;
1584 static int default_scope(inet_prefix
*lcl
)
1586 if (lcl
->family
== AF_INET
) {
1587 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1588 return RT_SCOPE_HOST
;
1593 static bool ipaddr_is_multicast(inet_prefix
*a
)
1595 if (a
->family
== AF_INET
)
1596 return IN_MULTICAST(ntohl(a
->data
[0]));
1597 else if (a
->family
== AF_INET6
)
1598 return IN6_IS_ADDR_MULTICAST(a
->data
);
1603 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1607 struct ifaddrmsg ifa
;
1612 char *lcl_arg
= NULL
;
1613 char *valid_lftp
= NULL
;
1614 char *preferred_lftp
= NULL
;
1622 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1623 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1624 struct ifa_cacheinfo cinfo
;
1625 unsigned int ifa_flags
= 0;
1627 memset(&req
, 0, sizeof(req
));
1629 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1630 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1631 req
.n
.nlmsg_type
= cmd
;
1632 req
.ifa
.ifa_family
= preferred_family
;
1635 if (strcmp(*argv
, "peer") == 0 ||
1636 strcmp(*argv
, "remote") == 0) {
1640 duparg("peer", *argv
);
1641 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1642 peer_len
= peer
.bytelen
;
1643 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1644 req
.ifa
.ifa_family
= peer
.family
;
1645 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1646 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1647 } else if (matches(*argv
, "broadcast") == 0 ||
1648 strcmp(*argv
, "brd") == 0) {
1652 duparg("broadcast", *argv
);
1653 if (strcmp(*argv
, "+") == 0)
1655 else if (strcmp(*argv
, "-") == 0)
1658 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1659 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1660 req
.ifa
.ifa_family
= addr
.family
;
1661 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1662 brd_len
= addr
.bytelen
;
1664 } else if (strcmp(*argv
, "anycast") == 0) {
1668 duparg("anycast", *argv
);
1669 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1670 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1671 req
.ifa
.ifa_family
= addr
.family
;
1672 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1673 any_len
= addr
.bytelen
;
1674 } else if (strcmp(*argv
, "scope") == 0) {
1677 if (rtnl_rtscope_a2n(&scope
, *argv
))
1678 invarg("invalid scope value.", *argv
);
1679 req
.ifa
.ifa_scope
= scope
;
1681 } else if (strcmp(*argv
, "dev") == 0) {
1684 } else if (strcmp(*argv
, "label") == 0) {
1687 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1688 } else if (matches(*argv
, "valid_lft") == 0) {
1690 duparg("valid_lft", *argv
);
1693 if (set_lifetime(&valid_lft
, *argv
))
1694 invarg("valid_lft value", *argv
);
1695 } else if (matches(*argv
, "preferred_lft") == 0) {
1697 duparg("preferred_lft", *argv
);
1699 preferred_lftp
= *argv
;
1700 if (set_lifetime(&preferred_lft
, *argv
))
1701 invarg("preferred_lft value", *argv
);
1702 } else if (strcmp(*argv
, "home") == 0) {
1703 ifa_flags
|= IFA_F_HOMEADDRESS
;
1704 } else if (strcmp(*argv
, "nodad") == 0) {
1705 ifa_flags
|= IFA_F_NODAD
;
1706 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1707 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1708 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1709 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1710 } else if (strcmp(*argv
, "autojoin") == 0) {
1711 ifa_flags
|= IFA_F_MCAUTOJOIN
;
1713 if (strcmp(*argv
, "local") == 0) {
1716 if (matches(*argv
, "help") == 0)
1719 duparg2("local", *argv
);
1721 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1722 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1723 req
.ifa
.ifa_family
= lcl
.family
;
1724 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1725 local_len
= lcl
.bytelen
;
1729 if (ifa_flags
<= 0xff)
1730 req
.ifa
.ifa_flags
= ifa_flags
;
1732 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1735 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1738 if (l
&& matches(d
, l
) != 0) {
1739 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1743 if (peer_len
== 0 && local_len
) {
1744 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1746 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1747 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1748 " This special behaviour is likely to disappear in further releases,\n" \
1749 " fix your scripts!\n", lcl_arg
, local_len
*8);
1752 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1755 if (req
.ifa
.ifa_prefixlen
== 0)
1756 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1758 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1761 if (req
.ifa
.ifa_family
!= AF_INET
) {
1762 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1766 if (brd
.bitlen
<= 30) {
1767 for (i
= 31; i
>= brd
.bitlen
; i
--) {
1769 brd
.data
[0] |= htonl(1<<(31-i
));
1771 brd
.data
[0] &= ~htonl(1<<(31-i
));
1773 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1774 brd_len
= brd
.bytelen
;
1777 if (!scoped
&& cmd
!= RTM_DELADDR
)
1778 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1780 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1781 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1785 if (valid_lftp
|| preferred_lftp
) {
1787 fprintf(stderr
, "valid_lft is zero\n");
1790 if (valid_lft
< preferred_lft
) {
1791 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1795 memset(&cinfo
, 0, sizeof(cinfo
));
1796 cinfo
.ifa_prefered
= preferred_lft
;
1797 cinfo
.ifa_valid
= valid_lft
;
1798 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1802 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
1803 fprintf(stderr
, "autojoin needs multicast address\n");
1807 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
1813 int do_ipaddr(int argc
, char **argv
)
1816 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
1817 if (matches(*argv
, "add") == 0)
1818 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1819 if (matches(*argv
, "change") == 0 ||
1820 strcmp(*argv
, "chg") == 0)
1821 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
1822 if (matches(*argv
, "replace") == 0)
1823 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
1824 if (matches(*argv
, "delete") == 0)
1825 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
1826 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1827 || matches(*argv
, "lst") == 0)
1828 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
1829 if (matches(*argv
, "flush") == 0)
1830 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
1831 if (matches(*argv
, "save") == 0)
1832 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
1833 if (matches(*argv
, "showdump") == 0)
1834 return ipaddr_showdump();
1835 if (matches(*argv
, "restore") == 0)
1836 return ipaddr_restore();
1837 if (matches(*argv
, "help") == 0)
1839 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv
);