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>
35 #include "ip_common.h"
64 static void usage(void) __attribute__((noreturn
));
66 static void usage(void)
71 fprintf(stderr
, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
72 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
73 fprintf(stderr
, " ip addr del IFADDR dev STRING [mngtmpaddr]\n");
74 fprintf(stderr
, " ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
75 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [up]\n");
76 fprintf(stderr
, " ip addr {showdump|restore}\n");
77 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
78 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
79 fprintf(stderr
, " [ label STRING ] [ scope SCOPE-ID ]\n");
80 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
81 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
82 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
83 fprintf(stderr
, " tentative | deprecated | dadfailed | temporary |\n");
84 fprintf(stderr
, " CONFFLAG-LIST ]\n");
85 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
86 fprintf(stderr
, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute ]\n");
87 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
88 fprintf(stderr
, "LFT := forever | SECONDS\n");
93 static void print_link_flags(FILE *fp
, unsigned flags
, unsigned mdown
)
96 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
97 fprintf(fp
, "NO-CARRIER%s", flags
? "," : "");
98 flags
&= ~IFF_RUNNING
;
99 #define _PF(f) if (flags&IFF_##f) { \
100 flags &= ~IFF_##f ; \
101 fprintf(fp, #f "%s", flags ? "," : ""); }
122 fprintf(fp
, "%x", flags
);
124 fprintf(fp
, ",M-DOWN");
128 static const char *oper_states
[] = {
129 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
130 "TESTING", "DORMANT", "UP"
133 static void print_operstate(FILE *f
, __u8 state
)
135 if (state
>= sizeof(oper_states
)/sizeof(oper_states
[0]))
136 fprintf(f
, "state %#x ", state
);
138 fprintf(f
, "state %s ", oper_states
[state
]);
141 int get_operstate(const char *name
)
145 for (i
= 0; i
< sizeof(oper_states
)/sizeof(oper_states
[0]); i
++)
146 if (strcasecmp(name
, oper_states
[i
]) == 0)
151 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
156 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
159 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
164 memset(&ifr
, 0, sizeof(ifr
));
165 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
166 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
167 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
175 fprintf(f
, "qlen %d", qlen
);
178 static const char *link_modes
[] = {
182 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
184 unsigned int mode
= rta_getattr_u8(tb
);
186 if (mode
>= sizeof(link_modes
) / sizeof(link_modes
[0]))
187 fprintf(f
, "mode %d ", mode
);
189 fprintf(f
, "mode %s ", link_modes
[mode
]);
192 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
194 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
195 struct link_util
*lu
;
196 struct link_util
*slave_lu
;
200 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
202 if (linkinfo
[IFLA_INFO_KIND
]) {
203 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
205 fprintf(fp
, "%s", _SL_
);
206 fprintf(fp
, " %s ", kind
);
208 lu
= get_link_kind(kind
);
209 if (lu
&& lu
->print_opt
) {
210 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
212 if (linkinfo
[IFLA_INFO_DATA
]) {
213 parse_rtattr_nested(attr
, lu
->maxattr
,
214 linkinfo
[IFLA_INFO_DATA
]);
217 lu
->print_opt(lu
, fp
, data
);
219 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
221 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
225 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
226 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
228 fprintf(fp
, "%s", _SL_
);
229 fprintf(fp
, " %s_slave ", slave_kind
);
231 slave_lu
= get_link_slave_kind(slave_kind
);
232 if (slave_lu
&& slave_lu
->print_opt
) {
233 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
235 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
236 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
237 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
240 slave_lu
->print_opt(slave_lu
, fp
, data
);
245 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
247 struct ifla_vf_mac
*vf_mac
;
248 struct ifla_vf_vlan
*vf_vlan
;
249 struct ifla_vf_rate
*vf_rate
;
250 struct ifla_vf_tx_rate
*vf_tx_rate
;
251 struct ifla_vf_spoofchk
*vf_spoofchk
;
252 struct ifla_vf_link_state
*vf_linkstate
;
253 struct rtattr
*vf
[IFLA_VF_MAX
+1];
257 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
258 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
262 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
264 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
265 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
266 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
267 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
269 /* Check if the spoof checking vf info type is supported by
272 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
273 vf
[IFLA_VF_TX_RATE
]->rta_len
);
275 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
278 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
281 /* Check if the link state vf info type is supported by
284 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_SPOOFCHK
] +
285 vf
[IFLA_VF_SPOOFCHK
]->rta_len
);
287 if (tmp
->rta_type
!= IFLA_VF_LINK_STATE
)
290 vf_linkstate
= RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
294 fprintf(fp
, "\n vf %d MAC %s", vf_mac
->vf
,
295 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
296 ETH_ALEN
, 0, b1
, sizeof(b1
)));
298 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
300 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
301 if (vf_tx_rate
->rate
)
302 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
303 if (vf_rate
->max_tx_rate
)
304 fprintf(fp
, ", max_tx_rate %dMbps", vf_rate
->max_tx_rate
);
305 if (vf_rate
->min_tx_rate
)
306 fprintf(fp
, ", min_tx_rate %dMbps", vf_rate
->min_tx_rate
);
307 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
308 if (vf_spoofchk
->setting
)
309 fprintf(fp
, ", spoof checking on");
311 fprintf(fp
, ", spoof checking off");
314 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
315 fprintf(fp
, ", link-state auto");
316 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
317 fprintf(fp
, ", link-state enable");
319 fprintf(fp
, ", link-state disable");
323 static void print_num(FILE *fp
, unsigned width
, uint64_t count
)
325 const char *prefix
= "kMGTPE";
326 const unsigned int base
= use_iec
? 1024 : 1000;
329 uint8_t precision
= 2;
332 if (!human_readable
|| count
< base
) {
333 fprintf(fp
, "%-*"PRIu64
" ", width
, count
);
337 /* increase value by a factor of 1000/1024 and print
338 * if result is something a human can read */
341 if (count
/ base
< powi
)
349 /* try to guess a good number of digits for precision */
350 for (; precision
> 0; precision
--) {
352 if (count
/ powi
< powj
)
356 snprintf(buf
, sizeof(buf
), "%.*f%c%s", precision
,
357 (double) count
/ powi
, *prefix
, use_iec
? "i" : "");
359 fprintf(fp
, "%-*s ", width
, buf
);
362 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
,
363 const struct rtattr
*carrier_changes
)
366 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
367 s
->rx_compressed
? "compressed" : "", _SL_
);
370 print_num(fp
, 10, s
->rx_bytes
);
371 print_num(fp
, 8, s
->rx_packets
);
372 print_num(fp
, 7, s
->rx_errors
);
373 print_num(fp
, 7, s
->rx_dropped
);
374 print_num(fp
, 7, s
->rx_over_errors
);
375 print_num(fp
, 7, s
->multicast
);
376 if (s
->rx_compressed
)
377 print_num(fp
, 7, s
->rx_compressed
);
380 if (show_stats
> 1) {
381 fprintf(fp
, "%s", _SL_
);
382 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
385 print_num(fp
, 8, s
->rx_length_errors
);
386 print_num(fp
, 7, s
->rx_crc_errors
);
387 print_num(fp
, 7, s
->rx_frame_errors
);
388 print_num(fp
, 7, s
->rx_fifo_errors
);
389 print_num(fp
, 7, s
->rx_missed_errors
);
391 fprintf(fp
, "%s", _SL_
);
394 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
395 s
->tx_compressed
? "compressed" : "", _SL_
);
399 print_num(fp
, 10, s
->tx_bytes
);
400 print_num(fp
, 8, s
->tx_packets
);
401 print_num(fp
, 7, s
->tx_errors
);
402 print_num(fp
, 7, s
->tx_dropped
);
403 print_num(fp
, 7, s
->tx_carrier_errors
);
404 print_num(fp
, 7, s
->collisions
);
405 if (s
->tx_compressed
)
406 print_num(fp
, 7, s
->tx_compressed
);
409 if (show_stats
> 1) {
410 fprintf(fp
, "%s", _SL_
);
411 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
413 fprintf(fp
, " transns");
414 fprintf(fp
, "%s", _SL_
);
417 print_num(fp
, 8, s
->tx_aborted_errors
);
418 print_num(fp
, 7, s
->tx_fifo_errors
);
419 print_num(fp
, 7, s
->tx_window_errors
);
420 print_num(fp
, 7, s
->tx_heartbeat_errors
);
422 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
426 static void print_link_stats32(FILE *fp
, const struct rtnl_link_stats
*s
,
427 const struct rtattr
*carrier_changes
)
430 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
431 s
->rx_compressed
? "compressed" : "", _SL_
);
435 print_num(fp
, 10, s
->rx_bytes
);
436 print_num(fp
, 8, s
->rx_packets
);
437 print_num(fp
, 7, s
->rx_errors
);
438 print_num(fp
, 7, s
->rx_dropped
);
439 print_num(fp
, 7, s
->rx_over_errors
);
440 print_num(fp
, 7, s
->multicast
);
441 if (s
->rx_compressed
)
442 print_num(fp
, 7, s
->rx_compressed
);
445 if (show_stats
> 1) {
446 fprintf(fp
, "%s", _SL_
);
447 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
449 print_num(fp
, 8, s
->rx_length_errors
);
450 print_num(fp
, 7, s
->rx_crc_errors
);
451 print_num(fp
, 7, s
->rx_frame_errors
);
452 print_num(fp
, 7, s
->rx_fifo_errors
);
453 print_num(fp
, 7, s
->rx_missed_errors
);
455 fprintf(fp
, "%s", _SL_
);
458 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
459 s
->tx_compressed
? "compressed" : "", _SL_
);
462 print_num(fp
, 10, s
->tx_bytes
);
463 print_num(fp
, 8, s
->tx_packets
);
464 print_num(fp
, 7, s
->tx_errors
);
465 print_num(fp
, 7, s
->tx_dropped
);
466 print_num(fp
, 7, s
->tx_carrier_errors
);
467 print_num(fp
, 7, s
->collisions
);
468 if (s
->tx_compressed
)
469 print_num(fp
, 7, s
->tx_compressed
);
472 if (show_stats
> 1) {
473 fprintf(fp
, "%s", _SL_
);
474 fprintf(fp
, " TX errors: aborted fifo window heartbeat");
476 fprintf(fp
, " transns");
477 fprintf(fp
, "%s", _SL_
);
480 print_num(fp
, 8, s
->tx_aborted_errors
);
481 print_num(fp
, 7, s
->tx_fifo_errors
);
482 print_num(fp
, 7, s
->tx_window_errors
);
483 print_num(fp
, 7, s
->tx_heartbeat_errors
);
485 print_num(fp
, 7, *(uint32_t*)RTA_DATA(carrier_changes
));
489 static void __print_link_stats(FILE *fp
, struct rtattr
**tb
)
491 if (tb
[IFLA_STATS64
])
492 print_link_stats64(fp
, RTA_DATA(tb
[IFLA_STATS64
]),
493 tb
[IFLA_CARRIER_CHANGES
]);
494 else if (tb
[IFLA_STATS
])
495 print_link_stats32(fp
, RTA_DATA(tb
[IFLA_STATS
]),
496 tb
[IFLA_CARRIER_CHANGES
]);
499 static void print_link_stats(FILE *fp
, struct nlmsghdr
*n
)
501 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
502 struct rtattr
* tb
[IFLA_MAX
+1];
504 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
),
505 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
506 __print_link_stats(fp
, tb
);
507 fprintf(fp
, "%s", _SL_
);
510 int print_linkinfo(const struct sockaddr_nl
*who
,
511 struct nlmsghdr
*n
, void *arg
)
513 FILE *fp
= (FILE*)arg
;
514 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
515 struct rtattr
* tb
[IFLA_MAX
+1];
516 int len
= n
->nlmsg_len
;
519 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
522 len
-= NLMSG_LENGTH(sizeof(*ifi
));
526 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
528 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
531 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
532 if (tb
[IFLA_IFNAME
] == NULL
) {
533 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
536 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
537 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
540 if (tb
[IFLA_GROUP
]) {
541 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
542 if (filter
.group
!= -1 && group
!= filter
.group
)
546 if (tb
[IFLA_MASTER
]) {
547 int master
= *(int*)RTA_DATA(tb
[IFLA_MASTER
]);
548 if (filter
.master
> 0 && master
!= filter
.master
)
551 else if (filter
.master
> 0)
554 if (n
->nlmsg_type
== RTM_DELLINK
)
555 fprintf(fp
, "Deleted ");
557 fprintf(fp
, "%d: %s", ifi
->ifi_index
,
558 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
562 int iflink
= *(int*)RTA_DATA(tb
[IFLA_LINK
]);
564 fprintf(fp
, "@NONE: ");
566 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
567 m_flag
= ll_index_to_flags(iflink
);
568 m_flag
= !(m_flag
& IFF_UP
);
573 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
576 fprintf(fp
, "mtu %u ", *(int*)RTA_DATA(tb
[IFLA_MTU
]));
578 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
579 if (tb
[IFLA_MASTER
]) {
581 fprintf(fp
, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
584 if (tb
[IFLA_PHYS_PORT_ID
]) {
586 fprintf(fp
, "portid %s ",
587 hexstring_n2a(RTA_DATA(tb
[IFLA_PHYS_PORT_ID
]),
588 RTA_PAYLOAD(tb
[IFLA_PHYS_PORT_ID
]),
592 if (tb
[IFLA_OPERSTATE
])
593 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
595 if (do_link
&& tb
[IFLA_LINKMODE
])
596 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
598 if (tb
[IFLA_GROUP
]) {
600 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
601 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
604 if (filter
.showqueue
)
605 print_queuelen(fp
, tb
);
607 if (!filter
.family
|| filter
.family
== AF_PACKET
|| show_details
) {
609 fprintf(fp
, "%s", _SL_
);
610 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
612 if (tb
[IFLA_ADDRESS
]) {
613 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
614 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
618 if (tb
[IFLA_BROADCAST
]) {
619 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
620 fprintf(fp
, " peer ");
622 fprintf(fp
, " brd ");
623 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
624 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
630 if (tb
[IFLA_PROMISCUITY
] && show_details
)
631 fprintf(fp
, " promiscuity %u ",
632 *(int*)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
634 if (tb
[IFLA_LINKINFO
] && show_details
)
635 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
637 if ((do_link
|| show_details
) && tb
[IFLA_IFALIAS
]) {
638 fprintf(fp
, "%s alias %s", _SL_
,
639 rta_getattr_str(tb
[IFLA_IFALIAS
]));
642 if (do_link
&& show_stats
) {
643 fprintf(fp
, "%s", _SL_
);
644 __print_link_stats(fp
, tb
);
647 if ((do_link
|| show_details
) && tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
648 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
649 int rem
= RTA_PAYLOAD(vflist
);
650 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
659 static int flush_update(void)
661 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
662 perror("Failed to send flush request");
669 static int set_lifetime(unsigned int *lifetime
, char *argv
)
671 if (strcmp(argv
, "forever") == 0)
672 *lifetime
= INFINITY_LIFE_TIME
;
673 else if (get_u32(lifetime
, argv
, 0))
679 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
680 struct rtattr
*ifa_flags_attr
)
682 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
686 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
690 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
691 int len
= n
->nlmsg_len
;
693 /* Use local copy of ifa_flags to not interfere with filtering code */
694 unsigned int ifa_flags
;
695 struct rtattr
* rta_tb
[IFA_MAX
+1];
699 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
701 len
-= NLMSG_LENGTH(sizeof(*ifa
));
703 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
707 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
710 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
),
711 n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
713 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
715 if (!rta_tb
[IFA_LOCAL
])
716 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
717 if (!rta_tb
[IFA_ADDRESS
])
718 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
720 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
722 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
724 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
729 if (rta_tb
[IFA_LABEL
])
730 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
732 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
733 if (fnmatch(filter
.label
, label
, 0) != 0)
736 if (filter
.pfx
.family
) {
737 if (rta_tb
[IFA_LOCAL
]) {
739 memset(&dst
, 0, sizeof(dst
));
740 dst
.family
= ifa
->ifa_family
;
741 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
742 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
747 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
752 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
756 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
757 memcpy(fn
, n
, n
->nlmsg_len
);
758 fn
->nlmsg_type
= RTM_DELADDR
;
759 fn
->nlmsg_flags
= NLM_F_REQUEST
;
760 fn
->nlmsg_seq
= ++rth
.seq
;
761 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
767 if (n
->nlmsg_type
== RTM_DELADDR
)
768 fprintf(fp
, "Deleted ");
770 if (filter
.oneline
|| filter
.flushb
)
771 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
772 if (ifa
->ifa_family
== AF_INET
)
773 fprintf(fp
, " inet ");
774 else if (ifa
->ifa_family
== AF_INET6
)
775 fprintf(fp
, " inet6 ");
776 else if (ifa
->ifa_family
== AF_DECnet
)
777 fprintf(fp
, " dnet ");
778 else if (ifa
->ifa_family
== AF_IPX
)
779 fprintf(fp
, " ipx ");
781 fprintf(fp
, " family %d ", ifa
->ifa_family
);
783 if (rta_tb
[IFA_LOCAL
]) {
784 fprintf(fp
, "%s", format_host(ifa
->ifa_family
,
785 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
786 RTA_DATA(rta_tb
[IFA_LOCAL
]),
787 abuf
, sizeof(abuf
)));
789 if (rta_tb
[IFA_ADDRESS
] == NULL
||
790 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]), RTA_DATA(rta_tb
[IFA_LOCAL
]),
791 ifa
->ifa_family
== AF_INET
? 4 : 16) == 0) {
792 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
794 fprintf(fp
, " peer %s/%d ",
795 format_host(ifa
->ifa_family
,
796 RTA_PAYLOAD(rta_tb
[IFA_ADDRESS
]),
797 RTA_DATA(rta_tb
[IFA_ADDRESS
]),
803 if (rta_tb
[IFA_BROADCAST
]) {
804 fprintf(fp
, "brd %s ",
805 format_host(ifa
->ifa_family
,
806 RTA_PAYLOAD(rta_tb
[IFA_BROADCAST
]),
807 RTA_DATA(rta_tb
[IFA_BROADCAST
]),
808 abuf
, sizeof(abuf
)));
810 if (rta_tb
[IFA_ANYCAST
]) {
811 fprintf(fp
, "any %s ",
812 format_host(ifa
->ifa_family
,
813 RTA_PAYLOAD(rta_tb
[IFA_ANYCAST
]),
814 RTA_DATA(rta_tb
[IFA_ANYCAST
]),
815 abuf
, sizeof(abuf
)));
817 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
818 if (ifa_flags
& IFA_F_SECONDARY
) {
819 ifa_flags
&= ~IFA_F_SECONDARY
;
820 if (ifa
->ifa_family
== AF_INET6
)
821 fprintf(fp
, "temporary ");
823 fprintf(fp
, "secondary ");
825 if (ifa_flags
& IFA_F_TENTATIVE
) {
826 ifa_flags
&= ~IFA_F_TENTATIVE
;
827 fprintf(fp
, "tentative ");
829 if (ifa_flags
& IFA_F_DEPRECATED
) {
830 ifa_flags
&= ~IFA_F_DEPRECATED
;
832 fprintf(fp
, "deprecated ");
834 if (ifa_flags
& IFA_F_HOMEADDRESS
) {
835 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
836 fprintf(fp
, "home ");
838 if (ifa_flags
& IFA_F_NODAD
) {
839 ifa_flags
&= ~IFA_F_NODAD
;
840 fprintf(fp
, "nodad ");
842 if (ifa_flags
& IFA_F_MANAGETEMPADDR
) {
843 ifa_flags
&= ~IFA_F_MANAGETEMPADDR
;
844 fprintf(fp
, "mngtmpaddr ");
846 if (ifa_flags
& IFA_F_NOPREFIXROUTE
) {
847 ifa_flags
&= ~IFA_F_NOPREFIXROUTE
;
848 fprintf(fp
, "noprefixroute ");
850 if (!(ifa_flags
& IFA_F_PERMANENT
)) {
851 fprintf(fp
, "dynamic ");
853 ifa_flags
&= ~IFA_F_PERMANENT
;
854 if (ifa_flags
& IFA_F_DADFAILED
) {
855 ifa_flags
&= ~IFA_F_DADFAILED
;
856 fprintf(fp
, "dadfailed ");
859 fprintf(fp
, "flags %02x ", ifa_flags
);
860 if (rta_tb
[IFA_LABEL
])
861 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
862 if (rta_tb
[IFA_CACHEINFO
]) {
863 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
864 fprintf(fp
, "%s", _SL_
);
865 fprintf(fp
, " valid_lft ");
866 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
867 fprintf(fp
, "forever");
869 fprintf(fp
, "%usec", ci
->ifa_valid
);
870 fprintf(fp
, " preferred_lft ");
871 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
872 fprintf(fp
, "forever");
875 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
877 fprintf(fp
, "%usec", ci
->ifa_prefered
);
885 static int print_addrinfo_primary(const struct sockaddr_nl
*who
,
886 struct nlmsghdr
*n
, void *arg
)
888 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
890 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
893 return print_addrinfo(who
, n
, arg
);
896 static int print_addrinfo_secondary(const struct sockaddr_nl
*who
,
897 struct nlmsghdr
*n
, void *arg
)
899 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
901 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
904 return print_addrinfo(who
, n
, arg
);
909 struct nlmsg_list
*next
;
915 struct nlmsg_list
*head
;
916 struct nlmsg_list
*tail
;
919 static int print_selected_addrinfo(int ifindex
, struct nlmsg_list
*ainfo
, FILE *fp
)
921 for ( ;ainfo
; ainfo
= ainfo
->next
) {
922 struct nlmsghdr
*n
= &ainfo
->h
;
923 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
925 if (n
->nlmsg_type
!= RTM_NEWADDR
)
928 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
931 if (ifa
->ifa_index
!= ifindex
||
932 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
935 print_addrinfo(NULL
, n
, fp
);
941 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
944 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
945 struct nlmsg_list
*h
;
947 h
= malloc(n
->nlmsg_len
+sizeof(void*));
951 memcpy(&h
->h
, n
, n
->nlmsg_len
);
955 lchain
->tail
->next
= h
;
960 ll_remember_index(who
, n
, NULL
);
964 static __u32 ipadd_dump_magic
= 0x47361222;
966 static int ipadd_save_prep(void)
970 if (isatty(STDOUT_FILENO
)) {
971 fprintf(stderr
, "Not sending a binary stream to stdout\n");
975 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
976 if (ret
!= sizeof(ipadd_dump_magic
)) {
977 fprintf(stderr
, "Can't write magic to dump file\n");
984 static int ipadd_dump_check_magic(void)
989 if (isatty(STDIN_FILENO
)) {
990 fprintf(stderr
, "Can't restore addr dump from a terminal\n");
994 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
995 if (magic
!= ipadd_dump_magic
) {
996 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
1003 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
1008 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
1009 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
1010 fprintf(stderr
, "Short write while saving nlmsg\n");
1014 return ret
== n
->nlmsg_len
? 0 : ret
;
1017 static int show_handler(const struct sockaddr_nl
*nl
, struct nlmsghdr
*n
, void *arg
)
1019 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1021 printf("if%d:\n", ifa
->ifa_index
);
1022 print_addrinfo(NULL
, n
, stdout
);
1026 static int ipaddr_showdump(void)
1028 if (ipadd_dump_check_magic())
1031 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
1034 static int restore_handler(const struct sockaddr_nl
*nl
, struct nlmsghdr
*n
, void *arg
)
1038 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
1042 ret
= rtnl_talk(&rth
, n
, 0, 0, n
);
1043 if ((ret
< 0) && (errno
== EEXIST
))
1049 static int ipaddr_restore(void)
1051 if (ipadd_dump_check_magic())
1054 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
1057 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
1059 struct nlmsg_list
*l
, *n
;
1061 for (l
= info
->head
; l
; l
= n
) {
1067 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
1069 struct nlmsg_list
*l
, **lp
;
1072 while ( (l
= *lp
) != NULL
) {
1074 int missing_net_address
= 1;
1075 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1076 struct nlmsg_list
*a
;
1078 for (a
= ainfo
->head
; a
; a
= a
->next
) {
1079 struct nlmsghdr
*n
= &a
->h
;
1080 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
1081 struct rtattr
*tb
[IFA_MAX
+ 1];
1082 unsigned int ifa_flags
;
1084 if (ifa
->ifa_index
!= ifi
->ifi_index
)
1086 missing_net_address
= 0;
1087 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
1089 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
1092 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
1093 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
1095 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
1097 if (filter
.pfx
.family
|| filter
.label
) {
1099 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
1101 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
1103 memset(&dst
, 0, sizeof(dst
));
1104 dst
.family
= ifa
->ifa_family
;
1105 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
1106 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1113 label
= RTA_DATA(tb
[IFA_LABEL
]);
1115 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1116 if (fnmatch(filter
.label
, label
, 0) != 0)
1124 if (missing_net_address
&&
1125 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1135 static int ipaddr_flush(void)
1138 char flushb
[4096-512];
1140 filter
.flushb
= flushb
;
1142 filter
.flushe
= sizeof(flushb
);
1144 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1145 const struct rtnl_dump_filter_arg a
[3] = {
1147 .filter
= print_addrinfo_secondary
,
1151 .filter
= print_addrinfo_primary
,
1159 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1160 perror("Cannot send dump request");
1164 if (rtnl_dump_filter_l(&rth
, a
) < 0) {
1165 fprintf(stderr
, "Flush terminated\n");
1168 if (filter
.flushed
== 0) {
1172 printf("Nothing to flush.\n");
1174 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
1180 if (flush_update() < 0)
1184 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1188 /* If we are flushing, and specifying primary, then we
1189 * want to flush only a single round. Otherwise, we'll
1190 * start flushing secondaries that were promoted to
1193 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1196 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1201 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1203 struct nlmsg_chain linfo
= { NULL
, NULL
};
1204 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1205 struct nlmsg_list
*l
;
1206 char *filter_dev
= NULL
;
1209 ipaddr_reset_filter(oneline
, 0);
1210 filter
.showqueue
= 1;
1212 if (filter
.family
== AF_UNSPEC
)
1213 filter
.family
= preferred_family
;
1217 if (action
== IPADD_FLUSH
) {
1219 fprintf(stderr
, "Flush requires arguments.\n");
1223 if (filter
.family
== AF_PACKET
) {
1224 fprintf(stderr
, "Cannot flush link addresses.\n");
1230 if (strcmp(*argv
, "to") == 0) {
1232 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1233 if (filter
.family
== AF_UNSPEC
)
1234 filter
.family
= filter
.pfx
.family
;
1235 } else if (strcmp(*argv
, "scope") == 0) {
1238 filter
.scopemask
= -1;
1239 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1240 if (strcmp(*argv
, "all") != 0)
1241 invarg("invalid \"scope\"\n", *argv
);
1242 scope
= RT_SCOPE_NOWHERE
;
1243 filter
.scopemask
= 0;
1245 filter
.scope
= scope
;
1246 } else if (strcmp(*argv
, "up") == 0) {
1248 } else if (strcmp(*argv
, "dynamic") == 0) {
1249 filter
.flags
&= ~IFA_F_PERMANENT
;
1250 filter
.flagmask
|= IFA_F_PERMANENT
;
1251 } else if (strcmp(*argv
, "permanent") == 0) {
1252 filter
.flags
|= IFA_F_PERMANENT
;
1253 filter
.flagmask
|= IFA_F_PERMANENT
;
1254 } else if (strcmp(*argv
, "secondary") == 0 ||
1255 strcmp(*argv
, "temporary") == 0) {
1256 filter
.flags
|= IFA_F_SECONDARY
;
1257 filter
.flagmask
|= IFA_F_SECONDARY
;
1258 } else if (strcmp(*argv
, "primary") == 0) {
1259 filter
.flags
&= ~IFA_F_SECONDARY
;
1260 filter
.flagmask
|= IFA_F_SECONDARY
;
1261 } else if (strcmp(*argv
, "tentative") == 0) {
1262 filter
.flags
|= IFA_F_TENTATIVE
;
1263 filter
.flagmask
|= IFA_F_TENTATIVE
;
1264 } else if (strcmp(*argv
, "deprecated") == 0) {
1265 filter
.flags
|= IFA_F_DEPRECATED
;
1266 filter
.flagmask
|= IFA_F_DEPRECATED
;
1267 } else if (strcmp(*argv
, "home") == 0) {
1268 filter
.flags
|= IFA_F_HOMEADDRESS
;
1269 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1270 } else if (strcmp(*argv
, "nodad") == 0) {
1271 filter
.flags
|= IFA_F_NODAD
;
1272 filter
.flagmask
|= IFA_F_NODAD
;
1273 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1274 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1275 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1276 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1277 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1278 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1279 } else if (strcmp(*argv
, "dadfailed") == 0) {
1280 filter
.flags
|= IFA_F_DADFAILED
;
1281 filter
.flagmask
|= IFA_F_DADFAILED
;
1282 } else if (strcmp(*argv
, "label") == 0) {
1284 filter
.label
= *argv
;
1285 } else if (strcmp(*argv
, "group") == 0) {
1287 if (rtnl_group_a2n(&filter
.group
, *argv
))
1288 invarg("Invalid \"group\" value\n", *argv
);
1289 } else if (strcmp(*argv
, "master") == 0) {
1292 ifindex
= ll_name_to_index(*argv
);
1294 invarg("Device does not exist\n", *argv
);
1295 filter
.master
= ifindex
;
1297 if (strcmp(*argv
, "dev") == 0) {
1300 if (matches(*argv
, "help") == 0)
1303 duparg2("dev", *argv
);
1310 filter
.ifindex
= ll_name_to_index(filter_dev
);
1311 if (filter
.ifindex
<= 0) {
1312 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1317 if (action
== IPADD_FLUSH
)
1318 return ipaddr_flush();
1320 if (action
== IPADD_SAVE
) {
1321 if (ipadd_save_prep())
1324 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1325 perror("Cannot send dump request");
1329 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1330 fprintf(stderr
, "Save terminated\n");
1338 * If only filter_dev present and none of the other
1339 * link filters are present, use RTM_GETLINK to get
1342 if (filter_dev
&& filter
.group
== -1 && do_link
== 1) {
1343 if (iplink_get(0, filter_dev
, RTEXT_FILTER_VF
) < 0) {
1344 perror("Cannot send link get request");
1350 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
1351 perror("Cannot send dump request");
1355 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1356 fprintf(stderr
, "Dump terminated\n");
1360 if (filter
.family
!= AF_PACKET
) {
1364 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1365 perror("Cannot send dump request");
1369 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1370 fprintf(stderr
, "Dump terminated\n");
1374 ipaddr_filter(&linfo
, &ainfo
);
1377 for (l
= linfo
.head
; l
; l
= l
->next
) {
1380 if (no_link
|| (res
= print_linkinfo(NULL
, &l
->h
, stdout
)) >= 0) {
1381 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1382 if (filter
.family
!= AF_PACKET
)
1383 print_selected_addrinfo(ifi
->ifi_index
,
1384 ainfo
.head
, stdout
);
1385 if (res
> 0 && !do_link
&& show_stats
)
1386 print_link_stats(stdout
, &l
->h
);
1391 free_nlmsg_chain(&ainfo
);
1392 free_nlmsg_chain(&linfo
);
1398 ipaddr_loop_each_vf(struct rtattr
*tb
[], int vfnum
, int *min
, int *max
)
1400 struct rtattr
*vflist
= tb
[IFLA_VFINFO_LIST
];
1401 struct rtattr
*i
, *vf
[IFLA_VF_MAX
+1];
1402 struct ifla_vf_rate
*vf_rate
;
1405 rem
= RTA_PAYLOAD(vflist
);
1407 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
1408 parse_rtattr_nested(vf
, IFLA_VF_MAX
, i
);
1409 vf_rate
= RTA_DATA(vf
[IFLA_VF_RATE
]);
1410 if (vf_rate
->vf
== vfnum
) {
1411 *min
= vf_rate
->min_tx_rate
;
1412 *max
= vf_rate
->max_tx_rate
;
1416 fprintf(stderr
, "Cannot find VF %d\n", vfnum
);
1420 void ipaddr_get_vf_rate(int vfnum
, int *min
, int *max
, int idx
)
1422 struct nlmsg_chain linfo
= { NULL
, NULL
};
1423 struct rtattr
*tb
[IFLA_MAX
+1];
1424 struct ifinfomsg
*ifi
;
1425 struct nlmsg_list
*l
;
1429 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
1430 perror("Cannot send dump request");
1433 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1434 fprintf(stderr
, "Dump terminated\n");
1437 for (l
= linfo
.head
; l
; l
= l
->next
) {
1439 ifi
= NLMSG_DATA(n
);
1441 len
= n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1442 if (len
< 0 || (idx
&& idx
!= ifi
->ifi_index
))
1445 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
1447 if ((tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
])) {
1448 ipaddr_loop_each_vf(tb
, vfnum
, min
, max
);
1454 int ipaddr_list_link(int argc
, char **argv
)
1456 preferred_family
= AF_PACKET
;
1458 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1461 void ipaddr_reset_filter(int oneline
, int ifindex
)
1463 memset(&filter
, 0, sizeof(filter
));
1464 filter
.oneline
= oneline
;
1465 filter
.ifindex
= ifindex
;
1468 static int default_scope(inet_prefix
*lcl
)
1470 if (lcl
->family
== AF_INET
) {
1471 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1472 return RT_SCOPE_HOST
;
1477 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1481 struct ifaddrmsg ifa
;
1486 char *lcl_arg
= NULL
;
1487 char *valid_lftp
= NULL
;
1488 char *preferred_lftp
= NULL
;
1496 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1497 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1498 struct ifa_cacheinfo cinfo
;
1499 unsigned int ifa_flags
= 0;
1501 memset(&req
, 0, sizeof(req
));
1503 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1504 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1505 req
.n
.nlmsg_type
= cmd
;
1506 req
.ifa
.ifa_family
= preferred_family
;
1509 if (strcmp(*argv
, "peer") == 0 ||
1510 strcmp(*argv
, "remote") == 0) {
1514 duparg("peer", *argv
);
1515 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1516 peer_len
= peer
.bytelen
;
1517 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1518 req
.ifa
.ifa_family
= peer
.family
;
1519 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1520 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1521 } else if (matches(*argv
, "broadcast") == 0 ||
1522 strcmp(*argv
, "brd") == 0) {
1526 duparg("broadcast", *argv
);
1527 if (strcmp(*argv
, "+") == 0)
1529 else if (strcmp(*argv
, "-") == 0)
1532 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1533 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1534 req
.ifa
.ifa_family
= addr
.family
;
1535 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1536 brd_len
= addr
.bytelen
;
1538 } else if (strcmp(*argv
, "anycast") == 0) {
1542 duparg("anycast", *argv
);
1543 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1544 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1545 req
.ifa
.ifa_family
= addr
.family
;
1546 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1547 any_len
= addr
.bytelen
;
1548 } else if (strcmp(*argv
, "scope") == 0) {
1551 if (rtnl_rtscope_a2n(&scope
, *argv
))
1552 invarg("invalid scope value.", *argv
);
1553 req
.ifa
.ifa_scope
= scope
;
1555 } else if (strcmp(*argv
, "dev") == 0) {
1558 } else if (strcmp(*argv
, "label") == 0) {
1561 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1562 } else if (matches(*argv
, "valid_lft") == 0) {
1564 duparg("valid_lft", *argv
);
1567 if (set_lifetime(&valid_lft
, *argv
))
1568 invarg("valid_lft value", *argv
);
1569 } else if (matches(*argv
, "preferred_lft") == 0) {
1571 duparg("preferred_lft", *argv
);
1573 preferred_lftp
= *argv
;
1574 if (set_lifetime(&preferred_lft
, *argv
))
1575 invarg("preferred_lft value", *argv
);
1576 } else if (strcmp(*argv
, "home") == 0) {
1577 ifa_flags
|= IFA_F_HOMEADDRESS
;
1578 } else if (strcmp(*argv
, "nodad") == 0) {
1579 ifa_flags
|= IFA_F_NODAD
;
1580 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1581 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1582 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1583 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1585 if (strcmp(*argv
, "local") == 0) {
1588 if (matches(*argv
, "help") == 0)
1591 duparg2("local", *argv
);
1593 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1594 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1595 req
.ifa
.ifa_family
= lcl
.family
;
1596 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1597 local_len
= lcl
.bytelen
;
1601 if (ifa_flags
<= 0xff)
1602 req
.ifa
.ifa_flags
= ifa_flags
;
1604 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1607 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1610 if (l
&& matches(d
, l
) != 0) {
1611 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1615 if (peer_len
== 0 && local_len
) {
1616 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1618 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1619 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1620 " This special behaviour is likely to disappear in further releases,\n" \
1621 " fix your scripts!\n", lcl_arg
, local_len
*8);
1624 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1627 if (req
.ifa
.ifa_prefixlen
== 0)
1628 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1630 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1633 if (req
.ifa
.ifa_family
!= AF_INET
) {
1634 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1638 if (brd
.bitlen
<= 30) {
1639 for (i
= 31; i
>= brd
.bitlen
; i
--) {
1641 brd
.data
[0] |= htonl(1<<(31-i
));
1643 brd
.data
[0] &= ~htonl(1<<(31-i
));
1645 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1646 brd_len
= brd
.bytelen
;
1649 if (!scoped
&& cmd
!= RTM_DELADDR
)
1650 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1652 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1653 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1657 if (valid_lftp
|| preferred_lftp
) {
1659 fprintf(stderr
, "valid_lft is zero\n");
1662 if (valid_lft
< preferred_lft
) {
1663 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1667 memset(&cinfo
, 0, sizeof(cinfo
));
1668 cinfo
.ifa_prefered
= preferred_lft
;
1669 cinfo
.ifa_valid
= valid_lft
;
1670 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1674 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
1680 int do_ipaddr(int argc
, char **argv
)
1683 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
1684 if (matches(*argv
, "add") == 0)
1685 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1686 if (matches(*argv
, "change") == 0 ||
1687 strcmp(*argv
, "chg") == 0)
1688 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
1689 if (matches(*argv
, "replace") == 0)
1690 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
1691 if (matches(*argv
, "delete") == 0)
1692 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
1693 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1694 || matches(*argv
, "lst") == 0)
1695 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
1696 if (matches(*argv
, "flush") == 0)
1697 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
1698 if (matches(*argv
, "save") == 0)
1699 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
1700 if (matches(*argv
, "showdump") == 0)
1701 return ipaddr_showdump();
1702 if (matches(*argv
, "restore") == 0)
1703 return ipaddr_restore();
1704 if (matches(*argv
, "help") == 0)
1706 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv
);