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
, struct nlmsghdr
*n
, void *arg
)
1116 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1118 printf("if%d:\n", ifa
->ifa_index
);
1119 print_addrinfo(NULL
, n
, stdout
);
1123 static int ipaddr_showdump(void)
1125 if (ipadd_dump_check_magic())
1128 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1131 static int restore_handler(const struct sockaddr_nl
*nl
, struct nlmsghdr
*n
, void *arg
)
1135 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1139 ret
= rtnl_talk(&rth
, n
, n
, sizeof(*n
));
1140 if ((ret
< 0) && (errno
== EEXIST
))
1146 static int ipaddr_restore(void)
1148 if (ipadd_dump_check_magic())
1151 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1154 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1156 struct nlmsg_list
*l
, *n
;
1158 for (l
= info
->head
; l
; l
= n
) {
1164 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1166 struct nlmsg_list
*l
, **lp
;
1169 while ( (l
= *lp
) != NULL
) {
1171 int missing_net_address
= 1;
1172 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1173 struct nlmsg_list
*a
;
1175 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1176 struct nlmsghdr
*n
= &a
->h
;
1177 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1178 struct rtattr
*tb
[IFA_MAX
+ 1];
1179 unsigned int ifa_flags
;
1181 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1183 missing_net_address
= 0;
1184 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1186 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1189 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1190 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1192 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1194 if (filter
.pfx
.family
|| filter
.label
) {
1196 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1198 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
1200 memset(&dst
, 0, sizeof(dst
));
1201 dst
.family
= ifa
->ifa_family
;
1202 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
1203 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1210 label
= RTA_DATA(tb
[IFA_LABEL
]);
1212 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1213 if (fnmatch(filter
.label
, label
, 0) != 0)
1221 if (missing_net_address
&&
1222 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1232 static int ipaddr_flush(void)
1235 char flushb
[4096-512];
1237 filter
.flushb
= flushb
;
1239 filter
.flushe
= sizeof(flushb
);
1241 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1242 const struct rtnl_dump_filter_arg a
[3] = {
1244 .filter
= print_addrinfo_secondary
,
1248 .filter
= print_addrinfo_primary
,
1256 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1257 perror("Cannot send dump request");
1261 if (rtnl_dump_filter_l(&rth
, a
) < 0) {
1262 fprintf(stderr
, "Flush terminated\n");
1265 if (filter
.flushed
== 0) {
1269 printf("Nothing to flush.\n");
1271 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
1277 if (flush_update() < 0)
1281 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1285 /* If we are flushing, and specifying primary, then we
1286 * want to flush only a single round. Otherwise, we'll
1287 * start flushing secondaries that were promoted to
1290 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1293 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1298 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1300 struct nlmsg_chain linfo
= { NULL
, NULL
};
1301 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1302 struct nlmsg_list
*l
;
1303 char *filter_dev
= NULL
;
1306 ipaddr_reset_filter(oneline
, 0);
1307 filter
.showqueue
= 1;
1309 if (filter
.family
== AF_UNSPEC
)
1310 filter
.family
= preferred_family
;
1314 if (action
== IPADD_FLUSH
) {
1316 fprintf(stderr
, "Flush requires arguments.\n");
1320 if (filter
.family
== AF_PACKET
) {
1321 fprintf(stderr
, "Cannot flush link addresses.\n");
1327 if (strcmp(*argv
, "to") == 0) {
1329 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1330 if (filter
.family
== AF_UNSPEC
)
1331 filter
.family
= filter
.pfx
.family
;
1332 } else if (strcmp(*argv
, "scope") == 0) {
1335 filter
.scopemask
= -1;
1336 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1337 if (strcmp(*argv
, "all") != 0)
1338 invarg("invalid \"scope\"\n", *argv
);
1339 scope
= RT_SCOPE_NOWHERE
;
1340 filter
.scopemask
= 0;
1342 filter
.scope
= scope
;
1343 } else if (strcmp(*argv
, "up") == 0) {
1345 } else if (strcmp(*argv
, "dynamic") == 0) {
1346 filter
.flags
&= ~IFA_F_PERMANENT
;
1347 filter
.flagmask
|= IFA_F_PERMANENT
;
1348 } else if (strcmp(*argv
, "permanent") == 0) {
1349 filter
.flags
|= IFA_F_PERMANENT
;
1350 filter
.flagmask
|= IFA_F_PERMANENT
;
1351 } else if (strcmp(*argv
, "secondary") == 0 ||
1352 strcmp(*argv
, "temporary") == 0) {
1353 filter
.flags
|= IFA_F_SECONDARY
;
1354 filter
.flagmask
|= IFA_F_SECONDARY
;
1355 } else if (strcmp(*argv
, "primary") == 0) {
1356 filter
.flags
&= ~IFA_F_SECONDARY
;
1357 filter
.flagmask
|= IFA_F_SECONDARY
;
1358 } else if (strcmp(*argv
, "tentative") == 0) {
1359 filter
.flags
|= IFA_F_TENTATIVE
;
1360 filter
.flagmask
|= IFA_F_TENTATIVE
;
1361 } else if (strcmp(*argv
, "-tentative") == 0) {
1362 filter
.flags
&= ~IFA_F_TENTATIVE
;
1363 filter
.flagmask
|= IFA_F_TENTATIVE
;
1364 } else if (strcmp(*argv
, "deprecated") == 0) {
1365 filter
.flags
|= IFA_F_DEPRECATED
;
1366 filter
.flagmask
|= IFA_F_DEPRECATED
;
1367 } else if (strcmp(*argv
, "-deprecated") == 0) {
1368 filter
.flags
&= ~IFA_F_DEPRECATED
;
1369 filter
.flagmask
|= IFA_F_DEPRECATED
;
1370 } else if (strcmp(*argv
, "home") == 0) {
1371 filter
.flags
|= IFA_F_HOMEADDRESS
;
1372 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1373 } else if (strcmp(*argv
, "nodad") == 0) {
1374 filter
.flags
|= IFA_F_NODAD
;
1375 filter
.flagmask
|= IFA_F_NODAD
;
1376 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1377 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1378 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1379 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1380 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1381 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1382 } else if (strcmp(*argv
, "autojoin") == 0) {
1383 filter
.flags
|= IFA_F_MCAUTOJOIN
;
1384 filter
.flagmask
|= IFA_F_MCAUTOJOIN
;
1385 } else if (strcmp(*argv
, "dadfailed") == 0) {
1386 filter
.flags
|= IFA_F_DADFAILED
;
1387 filter
.flagmask
|= IFA_F_DADFAILED
;
1388 } else if (strcmp(*argv
, "-dadfailed") == 0) {
1389 filter
.flags
&= ~IFA_F_DADFAILED
;
1390 filter
.flagmask
|= IFA_F_DADFAILED
;
1391 } else if (strcmp(*argv
, "label") == 0) {
1393 filter
.label
= *argv
;
1394 } else if (strcmp(*argv
, "group") == 0) {
1396 if (rtnl_group_a2n(&filter
.group
, *argv
))
1397 invarg("Invalid \"group\" value\n", *argv
);
1398 } else if (strcmp(*argv
, "master") == 0) {
1401 ifindex
= ll_name_to_index(*argv
);
1403 invarg("Device does not exist\n", *argv
);
1404 filter
.master
= ifindex
;
1405 } else if (do_link
&& strcmp(*argv
, "type") == 0) {
1407 filter
.kind
= *argv
;
1409 if (strcmp(*argv
, "dev") == 0) {
1412 if (matches(*argv
, "help") == 0)
1415 duparg2("dev", *argv
);
1422 filter
.ifindex
= ll_name_to_index(filter_dev
);
1423 if (filter
.ifindex
<= 0) {
1424 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1429 if (action
== IPADD_FLUSH
)
1430 return ipaddr_flush();
1432 if (action
== IPADD_SAVE
) {
1433 if (ipadd_save_prep())
1436 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1437 perror("Cannot send dump request");
1441 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1442 fprintf(stderr
, "Save terminated\n");
1450 * If only filter_dev present and none of the other
1451 * link filters are present, use RTM_GETLINK to get
1454 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1455 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1456 perror("Cannot send link get request");
1462 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
1463 perror("Cannot send dump request");
1467 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1468 fprintf(stderr
, "Dump terminated\n");
1472 if (filter
.family
!= AF_PACKET
) {
1476 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1477 perror("Cannot send dump request");
1481 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1482 fprintf(stderr
, "Dump terminated\n");
1486 ipaddr_filter(&linfo
, &ainfo
);
1489 for (l
= linfo
.head
; l
; l
= l
->next
) {
1492 if (no_link
|| (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1493 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1494 if (filter
.family
!= AF_PACKET
)
1495 print_selected_addrinfo(ifi
,
1496 ainfo
.head
, stdout
);
1497 if (res
> 0 && !do_link
&& show_stats
)
1498 print_link_stats(stdout
, &l
->h
);
1503 free_nlmsg_chain(&ainfo
);
1504 free_nlmsg_chain(&linfo
);
1510 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1512 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1513 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1514 struct ifla_vf_rate
*vf_rate
;
1517 rem
= RTA_PAYLOAD(vflist
);
1519 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1520 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1521 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1522 if (vf_rate
->vf
== vfnum
) {
1523 *min
= vf_rate
->min_tx_rate
;
1524 *max
= vf_rate
->max_tx_rate
;
1528 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1532 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1534 struct nlmsg_chain linfo
= { NULL
, NULL
};
1535 struct rtattr
*tb
[IFLA_MAX
+1];
1536 struct ifinfomsg
*ifi
;
1537 struct nlmsg_list
*l
;
1541 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1542 perror("Cannot send dump request");
1545 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1546 fprintf(stderr
, "Dump terminated\n");
1549 for (l
= linfo
.head
; l
; l
= l
->next
) {
1551 ifi
= NLMSG_DATA(n
);
1553 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1554 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1557 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1559 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1560 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1566 int ipaddr_list_link(int argc
, char **argv
)
1568 preferred_family
= AF_PACKET
;
1570 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1573 void ipaddr_reset_filter(int oneline
, int ifindex
)
1575 memset(&filter
, 0, sizeof(filter
));
1576 filter
.oneline
= oneline
;
1577 filter
.ifindex
= ifindex
;
1580 static int default_scope(inet_prefix
*lcl
)
1582 if (lcl
->family
== AF_INET
) {
1583 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1584 return RT_SCOPE_HOST
;
1589 static bool ipaddr_is_multicast(inet_prefix
*a
)
1591 if (a
->family
== AF_INET
)
1592 return IN_MULTICAST(ntohl(a
->data
[0]));
1593 else if (a
->family
== AF_INET6
)
1594 return IN6_IS_ADDR_MULTICAST(a
->data
);
1599 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1603 struct ifaddrmsg ifa
;
1608 char *lcl_arg
= NULL
;
1609 char *valid_lftp
= NULL
;
1610 char *preferred_lftp
= NULL
;
1618 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1619 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1620 struct ifa_cacheinfo cinfo
;
1621 unsigned int ifa_flags
= 0;
1623 memset(&req
, 0, sizeof(req
));
1625 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1626 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1627 req
.n
.nlmsg_type
= cmd
;
1628 req
.ifa
.ifa_family
= preferred_family
;
1631 if (strcmp(*argv
, "peer") == 0 ||
1632 strcmp(*argv
, "remote") == 0) {
1636 duparg("peer", *argv
);
1637 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1638 peer_len
= peer
.bytelen
;
1639 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1640 req
.ifa
.ifa_family
= peer
.family
;
1641 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1642 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1643 } else if (matches(*argv
, "broadcast") == 0 ||
1644 strcmp(*argv
, "brd") == 0) {
1648 duparg("broadcast", *argv
);
1649 if (strcmp(*argv
, "+") == 0)
1651 else if (strcmp(*argv
, "-") == 0)
1654 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1655 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1656 req
.ifa
.ifa_family
= addr
.family
;
1657 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1658 brd_len
= addr
.bytelen
;
1660 } else if (strcmp(*argv
, "anycast") == 0) {
1664 duparg("anycast", *argv
);
1665 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1666 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1667 req
.ifa
.ifa_family
= addr
.family
;
1668 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1669 any_len
= addr
.bytelen
;
1670 } else if (strcmp(*argv
, "scope") == 0) {
1673 if (rtnl_rtscope_a2n(&scope
, *argv
))
1674 invarg("invalid scope value.", *argv
);
1675 req
.ifa
.ifa_scope
= scope
;
1677 } else if (strcmp(*argv
, "dev") == 0) {
1680 } else if (strcmp(*argv
, "label") == 0) {
1683 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1684 } else if (matches(*argv
, "valid_lft") == 0) {
1686 duparg("valid_lft", *argv
);
1689 if (set_lifetime(&valid_lft
, *argv
))
1690 invarg("valid_lft value", *argv
);
1691 } else if (matches(*argv
, "preferred_lft") == 0) {
1693 duparg("preferred_lft", *argv
);
1695 preferred_lftp
= *argv
;
1696 if (set_lifetime(&preferred_lft
, *argv
))
1697 invarg("preferred_lft value", *argv
);
1698 } else if (strcmp(*argv
, "home") == 0) {
1699 ifa_flags
|= IFA_F_HOMEADDRESS
;
1700 } else if (strcmp(*argv
, "nodad") == 0) {
1701 ifa_flags
|= IFA_F_NODAD
;
1702 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1703 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1704 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1705 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1706 } else if (strcmp(*argv
, "autojoin") == 0) {
1707 ifa_flags
|= IFA_F_MCAUTOJOIN
;
1709 if (strcmp(*argv
, "local") == 0) {
1712 if (matches(*argv
, "help") == 0)
1715 duparg2("local", *argv
);
1717 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1718 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1719 req
.ifa
.ifa_family
= lcl
.family
;
1720 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1721 local_len
= lcl
.bytelen
;
1725 if (ifa_flags
<= 0xff)
1726 req
.ifa
.ifa_flags
= ifa_flags
;
1728 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1731 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1734 if (l
&& matches(d
, l
) != 0) {
1735 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1739 if (peer_len
== 0 && local_len
) {
1740 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1742 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1743 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1744 " This special behaviour is likely to disappear in further releases,\n" \
1745 " fix your scripts!\n", lcl_arg
, local_len
*8);
1748 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1751 if (req
.ifa
.ifa_prefixlen
== 0)
1752 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1754 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1757 if (req
.ifa
.ifa_family
!= AF_INET
) {
1758 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1762 if (brd
.bitlen
<= 30) {
1763 for (i
= 31; i
>= brd
.bitlen
; i
--) {
1765 brd
.data
[0] |= htonl(1<<(31-i
));
1767 brd
.data
[0] &= ~htonl(1<<(31-i
));
1769 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1770 brd_len
= brd
.bytelen
;
1773 if (!scoped
&& cmd
!= RTM_DELADDR
)
1774 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1776 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1777 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1781 if (valid_lftp
|| preferred_lftp
) {
1783 fprintf(stderr
, "valid_lft is zero\n");
1786 if (valid_lft
< preferred_lft
) {
1787 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1791 memset(&cinfo
, 0, sizeof(cinfo
));
1792 cinfo
.ifa_prefered
= preferred_lft
;
1793 cinfo
.ifa_valid
= valid_lft
;
1794 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1798 if ((ifa_flags
& IFA_F_MCAUTOJOIN
) && !ipaddr_is_multicast(&lcl
)) {
1799 fprintf(stderr
, "autojoin needs multicast address\n");
1803 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
1809 int do_ipaddr(int argc
, char **argv
)
1812 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
1813 if (matches(*argv
, "add") == 0)
1814 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1815 if (matches(*argv
, "change") == 0 ||
1816 strcmp(*argv
, "chg") == 0)
1817 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
1818 if (matches(*argv
, "replace") == 0)
1819 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
1820 if (matches(*argv
, "delete") == 0)
1821 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
1822 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1823 || matches(*argv
, "lst") == 0)
1824 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
1825 if (matches(*argv
, "flush") == 0)
1826 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
1827 if (matches(*argv
, "save") == 0)
1828 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
1829 if (matches(*argv
, "showdump") == 0)
1830 return ipaddr_showdump();
1831 if (matches(*argv
, "restore") == 0)
1832 return ipaddr_restore();
1833 if (matches(*argv
, "help") == 0)
1835 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv
);