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"
63 static void usage(void) __attribute__((noreturn
));
65 static void usage(void)
70 fprintf(stderr
, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
71 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
72 fprintf(stderr
, " ip addr del IFADDR dev STRING\n");
73 fprintf(stderr
, " ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
74 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [up]\n");
75 fprintf(stderr
, " ip addr {showdump|restore}\n");
76 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
77 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
78 fprintf(stderr
, " [ label STRING ] [ scope SCOPE-ID ]\n");
79 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
80 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
81 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
82 fprintf(stderr
, " tentative | deprecated | dadfailed | temporary |\n");
83 fprintf(stderr
, " CONFFLAG-LIST ]\n");
84 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
85 fprintf(stderr
, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute ]\n");
86 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
87 fprintf(stderr
, "LFT := forever | SECONDS\n");
92 static void print_link_flags(FILE *fp
, unsigned flags
, unsigned mdown
)
95 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
96 fprintf(fp
, "NO-CARRIER%s", flags
? "," : "");
97 flags
&= ~IFF_RUNNING
;
98 #define _PF(f) if (flags&IFF_##f) { \
100 fprintf(fp, #f "%s", flags ? "," : ""); }
121 fprintf(fp
, "%x", flags
);
123 fprintf(fp
, ",M-DOWN");
127 static const char *oper_states
[] = {
128 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
129 "TESTING", "DORMANT", "UP"
132 static void print_operstate(FILE *f
, __u8 state
)
134 if (state
>= sizeof(oper_states
)/sizeof(oper_states
[0]))
135 fprintf(f
, "state %#x ", state
);
137 fprintf(f
, "state %s ", oper_states
[state
]);
140 int get_operstate(const char *name
)
144 for (i
= 0; i
< sizeof(oper_states
)/sizeof(oper_states
[0]); i
++)
145 if (strcasecmp(name
, oper_states
[i
]) == 0)
150 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
155 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
158 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
163 memset(&ifr
, 0, sizeof(ifr
));
164 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
165 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
166 fprintf(f
, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno
));
174 fprintf(f
, "qlen %d", qlen
);
177 static const char *link_modes
[] = {
181 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
183 unsigned int mode
= rta_getattr_u8(tb
);
185 if (mode
>= sizeof(link_modes
) / sizeof(link_modes
[0]))
186 fprintf(f
, "mode %d ", mode
);
188 fprintf(f
, "mode %s ", link_modes
[mode
]);
191 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
193 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
194 struct link_util
*lu
;
195 struct link_util
*slave_lu
;
199 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
201 if (linkinfo
[IFLA_INFO_KIND
]) {
202 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
204 fprintf(fp
, "%s", _SL_
);
205 fprintf(fp
, " %s ", kind
);
207 lu
= get_link_kind(kind
);
208 if (lu
&& lu
->print_opt
) {
209 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
211 if (linkinfo
[IFLA_INFO_DATA
]) {
212 parse_rtattr_nested(attr
, lu
->maxattr
,
213 linkinfo
[IFLA_INFO_DATA
]);
216 lu
->print_opt(lu
, fp
, data
);
218 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
220 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
224 if (linkinfo
[IFLA_INFO_SLAVE_KIND
]) {
225 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
227 fprintf(fp
, "%s", _SL_
);
228 fprintf(fp
, " %s_slave ", slave_kind
);
230 slave_lu
= get_link_slave_kind(slave_kind
);
231 if (slave_lu
&& slave_lu
->print_opt
) {
232 struct rtattr
*attr
[slave_lu
->maxattr
+1], **data
= NULL
;
234 if (linkinfo
[IFLA_INFO_SLAVE_DATA
]) {
235 parse_rtattr_nested(attr
, slave_lu
->maxattr
,
236 linkinfo
[IFLA_INFO_SLAVE_DATA
]);
239 slave_lu
->print_opt(slave_lu
, fp
, data
);
244 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
246 struct ifla_vf_mac
*vf_mac
;
247 struct ifla_vf_vlan
*vf_vlan
;
248 struct ifla_vf_tx_rate
*vf_tx_rate
;
249 struct ifla_vf_spoofchk
*vf_spoofchk
;
250 struct ifla_vf_link_state
*vf_linkstate
;
251 struct rtattr
*vf
[IFLA_VF_MAX
+1];
255 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
256 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
260 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
262 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
263 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
264 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
266 /* Check if the spoof checking vf info type is supported by
269 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
270 vf
[IFLA_VF_TX_RATE
]->rta_len
);
272 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
275 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
278 /* Check if the link state vf info type is supported by
281 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_SPOOFCHK
] +
282 vf
[IFLA_VF_SPOOFCHK
]->rta_len
);
284 if (tmp
->rta_type
!= IFLA_VF_LINK_STATE
)
287 vf_linkstate
= RTA_DATA(vf
[IFLA_VF_LINK_STATE
]);
291 fprintf(fp
, "\n vf %d MAC %s", vf_mac
->vf
,
292 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
293 ETH_ALEN
, 0, b1
, sizeof(b1
)));
295 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
297 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
298 if (vf_tx_rate
->rate
)
299 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
300 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
301 if (vf_spoofchk
->setting
)
302 fprintf(fp
, ", spoof checking on");
304 fprintf(fp
, ", spoof checking off");
307 if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_AUTO
)
308 fprintf(fp
, ", link-state auto");
309 else if (vf_linkstate
->link_state
== IFLA_VF_LINK_STATE_ENABLE
)
310 fprintf(fp
, ", link-state enable");
312 fprintf(fp
, ", link-state disable");
316 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
) {
317 fprintf(fp
, "%s", _SL_
);
318 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
319 s
->rx_compressed
? "compressed" : "", _SL_
);
320 fprintf(fp
, " %-10"PRIu64
" %-8"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
321 (uint64_t)s
->rx_bytes
,
322 (uint64_t)s
->rx_packets
,
323 (uint64_t)s
->rx_errors
,
324 (uint64_t)s
->rx_dropped
,
325 (uint64_t)s
->rx_over_errors
,
326 (uint64_t)s
->multicast
);
327 if (s
->rx_compressed
)
328 fprintf(fp
, " %-7"PRIu64
"",
329 (uint64_t)s
->rx_compressed
);
330 if (show_stats
> 1) {
331 fprintf(fp
, "%s", _SL_
);
332 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
333 fprintf(fp
, " %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
334 (uint64_t)s
->rx_length_errors
,
335 (uint64_t)s
->rx_crc_errors
,
336 (uint64_t)s
->rx_frame_errors
,
337 (uint64_t)s
->rx_fifo_errors
,
338 (uint64_t)s
->rx_missed_errors
);
340 fprintf(fp
, "%s", _SL_
);
341 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
342 (uint64_t)s
->tx_compressed
? "compressed" : "", _SL_
);
343 fprintf(fp
, " %-10"PRIu64
" %-8"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
344 (uint64_t)s
->tx_bytes
,
345 (uint64_t)s
->tx_packets
,
346 (uint64_t)s
->tx_errors
,
347 (uint64_t)s
->tx_dropped
,
348 (uint64_t)s
->tx_carrier_errors
,
349 (uint64_t)s
->collisions
);
350 if (s
->tx_compressed
)
351 fprintf(fp
, " %-7"PRIu64
"",
352 (uint64_t)s
->tx_compressed
);
353 if (show_stats
> 1) {
354 fprintf(fp
, "%s", _SL_
);
355 fprintf(fp
, " TX errors: aborted fifo window heartbeat%s", _SL_
);
356 fprintf(fp
, " %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
357 (uint64_t)s
->tx_aborted_errors
,
358 (uint64_t)s
->tx_fifo_errors
,
359 (uint64_t)s
->tx_window_errors
,
360 (uint64_t)s
->tx_heartbeat_errors
);
364 static void print_link_stats(FILE *fp
, const struct rtnl_link_stats
*s
)
366 fprintf(fp
, "%s", _SL_
);
367 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
368 s
->rx_compressed
? "compressed" : "", _SL_
);
369 fprintf(fp
, " %-10u %-8u %-7u %-7u %-7u %-7u",
370 s
->rx_bytes
, s
->rx_packets
, s
->rx_errors
,
371 s
->rx_dropped
, s
->rx_over_errors
,
374 if (s
->rx_compressed
)
375 fprintf(fp
, " %-7u", s
->rx_compressed
);
376 if (show_stats
> 1) {
377 fprintf(fp
, "%s", _SL_
);
378 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
379 fprintf(fp
, " %-7u %-7u %-7u %-7u %-7u",
387 fprintf(fp
, "%s", _SL_
);
388 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
389 s
->tx_compressed
? "compressed" : "", _SL_
);
390 fprintf(fp
, " %-10u %-8u %-7u %-7u %-7u %-7u",
391 s
->tx_bytes
, s
->tx_packets
, s
->tx_errors
,
392 s
->tx_dropped
, s
->tx_carrier_errors
, s
->collisions
);
393 if (s
->tx_compressed
)
394 fprintf(fp
, " %-7u", s
->tx_compressed
);
395 if (show_stats
> 1) {
396 fprintf(fp
, "%s", _SL_
);
397 fprintf(fp
, " TX errors: aborted fifo window heartbeat%s", _SL_
);
398 fprintf(fp
, " %-7u %-7u %-7u %-7u",
399 s
->tx_aborted_errors
,
402 s
->tx_heartbeat_errors
407 int print_linkinfo(const struct sockaddr_nl
*who
,
408 struct nlmsghdr
*n
, void *arg
)
410 FILE *fp
= (FILE*)arg
;
411 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
412 struct rtattr
* tb
[IFLA_MAX
+1];
413 int len
= n
->nlmsg_len
;
416 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
419 len
-= NLMSG_LENGTH(sizeof(*ifi
));
423 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
425 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
428 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
429 if (tb
[IFLA_IFNAME
] == NULL
) {
430 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
433 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
434 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
437 if (tb
[IFLA_GROUP
]) {
438 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
439 if (filter
.group
!= -1 && group
!= filter
.group
)
443 if (n
->nlmsg_type
== RTM_DELLINK
)
444 fprintf(fp
, "Deleted ");
446 fprintf(fp
, "%d: %s", ifi
->ifi_index
,
447 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
451 int iflink
= *(int*)RTA_DATA(tb
[IFLA_LINK
]);
453 fprintf(fp
, "@NONE: ");
455 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
456 m_flag
= ll_index_to_flags(iflink
);
457 m_flag
= !(m_flag
& IFF_UP
);
462 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
465 fprintf(fp
, "mtu %u ", *(int*)RTA_DATA(tb
[IFLA_MTU
]));
467 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
468 if (tb
[IFLA_MASTER
]) {
470 fprintf(fp
, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
473 if (tb
[IFLA_OPERSTATE
])
474 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
476 if (do_link
&& tb
[IFLA_LINKMODE
])
477 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
479 if (tb
[IFLA_GROUP
]) {
481 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
482 fprintf(fp
, "group %s ", rtnl_group_n2a(group
, b1
, sizeof(b1
)));
485 if (filter
.showqueue
)
486 print_queuelen(fp
, tb
);
488 if (!filter
.family
|| filter
.family
== AF_PACKET
) {
490 fprintf(fp
, "%s", _SL_
);
491 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
493 if (tb
[IFLA_ADDRESS
]) {
494 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
495 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
499 if (tb
[IFLA_BROADCAST
]) {
500 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
501 fprintf(fp
, " peer ");
503 fprintf(fp
, " brd ");
504 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
505 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
511 if (do_link
&& tb
[IFLA_PROMISCUITY
] && show_details
)
512 fprintf(fp
, " promiscuity %u ",
513 *(int*)RTA_DATA(tb
[IFLA_PROMISCUITY
]));
515 if (do_link
&& tb
[IFLA_LINKINFO
] && show_details
)
516 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
518 if (do_link
&& tb
[IFLA_IFALIAS
]) {
519 fprintf(fp
, "%s alias %s", _SL_
,
520 rta_getattr_str(tb
[IFLA_IFALIAS
]));
523 if (do_link
&& show_stats
) {
524 if (tb
[IFLA_STATS64
])
525 print_link_stats64(fp
, RTA_DATA(tb
[IFLA_STATS64
]));
526 else if (tb
[IFLA_STATS
])
527 print_link_stats(fp
, RTA_DATA(tb
[IFLA_STATS
]));
530 if (do_link
&& tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
531 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
532 int rem
= RTA_PAYLOAD(vflist
);
533 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
542 static int flush_update(void)
544 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
545 perror("Failed to send flush request");
552 static int set_lifetime(unsigned int *lifetime
, char *argv
)
554 if (strcmp(argv
, "forever") == 0)
555 *lifetime
= INFINITY_LIFE_TIME
;
556 else if (get_u32(lifetime
, argv
, 0))
562 static unsigned int get_ifa_flags(struct ifaddrmsg
*ifa
,
563 struct rtattr
*ifa_flags_attr
)
565 return ifa_flags_attr
? rta_getattr_u32(ifa_flags_attr
) :
569 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
572 FILE *fp
= (FILE*)arg
;
573 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
574 int len
= n
->nlmsg_len
;
576 /* Use local copy of ifa_flags to not interfere with filtering code */
577 unsigned int ifa_flags
;
578 struct rtattr
* rta_tb
[IFA_MAX
+1];
582 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
584 len
-= NLMSG_LENGTH(sizeof(*ifa
));
586 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
590 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
593 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
595 ifa_flags
= get_ifa_flags(ifa
, rta_tb
[IFA_FLAGS
]);
597 if (!rta_tb
[IFA_LOCAL
])
598 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
599 if (!rta_tb
[IFA_ADDRESS
])
600 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
602 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
604 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
606 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
611 if (rta_tb
[IFA_LABEL
])
612 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
614 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
615 if (fnmatch(filter
.label
, label
, 0) != 0)
618 if (filter
.pfx
.family
) {
619 if (rta_tb
[IFA_LOCAL
]) {
621 memset(&dst
, 0, sizeof(dst
));
622 dst
.family
= ifa
->ifa_family
;
623 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
624 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
629 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
634 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
638 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
639 memcpy(fn
, n
, n
->nlmsg_len
);
640 fn
->nlmsg_type
= RTM_DELADDR
;
641 fn
->nlmsg_flags
= NLM_F_REQUEST
;
642 fn
->nlmsg_seq
= ++rth
.seq
;
643 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
649 if (n
->nlmsg_type
== RTM_DELADDR
)
650 fprintf(fp
, "Deleted ");
652 if (filter
.oneline
|| filter
.flushb
)
653 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
654 if (ifa
->ifa_family
== AF_INET
)
655 fprintf(fp
, " inet ");
656 else if (ifa
->ifa_family
== AF_INET6
)
657 fprintf(fp
, " inet6 ");
658 else if (ifa
->ifa_family
== AF_DECnet
)
659 fprintf(fp
, " dnet ");
660 else if (ifa
->ifa_family
== AF_IPX
)
661 fprintf(fp
, " ipx ");
663 fprintf(fp
, " family %d ", ifa
->ifa_family
);
665 if (rta_tb
[IFA_LOCAL
]) {
666 fprintf(fp
, "%s", format_host(ifa
->ifa_family
,
667 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
668 RTA_DATA(rta_tb
[IFA_LOCAL
]),
669 abuf
, sizeof(abuf
)));
671 if (rta_tb
[IFA_ADDRESS
] == NULL
||
672 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]), RTA_DATA(rta_tb
[IFA_LOCAL
]),
673 ifa
->ifa_family
== AF_INET
? 4 : 16) == 0) {
674 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
676 fprintf(fp
, " peer %s/%d ",
677 format_host(ifa
->ifa_family
,
678 RTA_PAYLOAD(rta_tb
[IFA_ADDRESS
]),
679 RTA_DATA(rta_tb
[IFA_ADDRESS
]),
685 if (rta_tb
[IFA_BROADCAST
]) {
686 fprintf(fp
, "brd %s ",
687 format_host(ifa
->ifa_family
,
688 RTA_PAYLOAD(rta_tb
[IFA_BROADCAST
]),
689 RTA_DATA(rta_tb
[IFA_BROADCAST
]),
690 abuf
, sizeof(abuf
)));
692 if (rta_tb
[IFA_ANYCAST
]) {
693 fprintf(fp
, "any %s ",
694 format_host(ifa
->ifa_family
,
695 RTA_PAYLOAD(rta_tb
[IFA_ANYCAST
]),
696 RTA_DATA(rta_tb
[IFA_ANYCAST
]),
697 abuf
, sizeof(abuf
)));
699 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
700 if (ifa_flags
& IFA_F_SECONDARY
) {
701 ifa_flags
&= ~IFA_F_SECONDARY
;
702 if (ifa
->ifa_family
== AF_INET6
)
703 fprintf(fp
, "temporary ");
705 fprintf(fp
, "secondary ");
707 if (ifa_flags
& IFA_F_TENTATIVE
) {
708 ifa_flags
&= ~IFA_F_TENTATIVE
;
709 fprintf(fp
, "tentative ");
711 if (ifa_flags
& IFA_F_DEPRECATED
) {
712 ifa_flags
&= ~IFA_F_DEPRECATED
;
714 fprintf(fp
, "deprecated ");
716 if (ifa_flags
& IFA_F_HOMEADDRESS
) {
717 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
718 fprintf(fp
, "home ");
720 if (ifa_flags
& IFA_F_NODAD
) {
721 ifa_flags
&= ~IFA_F_NODAD
;
722 fprintf(fp
, "nodad ");
724 if (ifa_flags
& IFA_F_MANAGETEMPADDR
) {
725 ifa_flags
&= ~IFA_F_MANAGETEMPADDR
;
726 fprintf(fp
, "mngtmpaddr ");
728 if (ifa_flags
& IFA_F_NOPREFIXROUTE
) {
729 ifa_flags
&= ~IFA_F_NOPREFIXROUTE
;
730 fprintf(fp
, "noprefixroute ");
732 if (!(ifa_flags
& IFA_F_PERMANENT
)) {
733 fprintf(fp
, "dynamic ");
735 ifa_flags
&= ~IFA_F_PERMANENT
;
736 if (ifa_flags
& IFA_F_DADFAILED
) {
737 ifa_flags
&= ~IFA_F_DADFAILED
;
738 fprintf(fp
, "dadfailed ");
741 fprintf(fp
, "flags %02x ", ifa_flags
);
742 if (rta_tb
[IFA_LABEL
])
743 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
744 if (rta_tb
[IFA_CACHEINFO
]) {
745 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
746 fprintf(fp
, "%s", _SL_
);
747 fprintf(fp
, " valid_lft ");
748 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
749 fprintf(fp
, "forever");
751 fprintf(fp
, "%usec", ci
->ifa_valid
);
752 fprintf(fp
, " preferred_lft ");
753 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
754 fprintf(fp
, "forever");
757 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
759 fprintf(fp
, "%usec", ci
->ifa_prefered
);
767 static int print_addrinfo_primary(const struct sockaddr_nl
*who
,
768 struct nlmsghdr
*n
, void *arg
)
770 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
772 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
775 return print_addrinfo(who
, n
, arg
);
778 static int print_addrinfo_secondary(const struct sockaddr_nl
*who
,
779 struct nlmsghdr
*n
, void *arg
)
781 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
783 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
786 return print_addrinfo(who
, n
, arg
);
791 struct nlmsg_list
*next
;
797 struct nlmsg_list
*head
;
798 struct nlmsg_list
*tail
;
801 static int print_selected_addrinfo(int ifindex
, struct nlmsg_list
*ainfo
, FILE *fp
)
803 for ( ;ainfo
; ainfo
= ainfo
->next
) {
804 struct nlmsghdr
*n
= &ainfo
->h
;
805 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
807 if (n
->nlmsg_type
!= RTM_NEWADDR
)
810 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
813 if (ifa
->ifa_index
!= ifindex
||
814 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
817 print_addrinfo(NULL
, n
, fp
);
823 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
826 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
827 struct nlmsg_list
*h
;
829 h
= malloc(n
->nlmsg_len
+sizeof(void*));
833 memcpy(&h
->h
, n
, n
->nlmsg_len
);
837 lchain
->tail
->next
= h
;
842 ll_remember_index(who
, n
, NULL
);
846 static __u32 ipadd_dump_magic
= 0x47361222;
848 static int ipadd_save_prep(void)
852 if (isatty(STDOUT_FILENO
)) {
853 fprintf(stderr
, "Not sending a binary stream to stdout\n");
857 ret
= write(STDOUT_FILENO
, &ipadd_dump_magic
, sizeof(ipadd_dump_magic
));
858 if (ret
!= sizeof(ipadd_dump_magic
)) {
859 fprintf(stderr
, "Can't write magic to dump file\n");
866 static int ipadd_dump_check_magic(void)
871 if (isatty(STDIN_FILENO
)) {
872 fprintf(stderr
, "Can't restore addr dump from a terminal\n");
876 ret
= fread(&magic
, sizeof(magic
), 1, stdin
);
877 if (magic
!= ipadd_dump_magic
) {
878 fprintf(stderr
, "Magic mismatch (%d elems, %x magic)\n", ret
, magic
);
885 static int save_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
890 ret
= write(STDOUT_FILENO
, n
, n
->nlmsg_len
);
891 if ((ret
> 0) && (ret
!= n
->nlmsg_len
)) {
892 fprintf(stderr
, "Short write while saving nlmsg\n");
896 return ret
== n
->nlmsg_len
? 0 : ret
;
899 static int show_handler(const struct sockaddr_nl
*nl
, struct nlmsghdr
*n
, void *arg
)
901 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
903 printf("if%d:\n", ifa
->ifa_index
);
904 print_addrinfo(NULL
, n
, stdout
);
908 static int ipaddr_showdump(void)
910 if (ipadd_dump_check_magic())
913 exit(rtnl_from_file(stdin
, &show_handler
, NULL
));
916 static int restore_handler(const struct sockaddr_nl
*nl
, struct nlmsghdr
*n
, void *arg
)
920 n
->nlmsg_flags
|= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_ACK
;
924 ret
= rtnl_talk(&rth
, n
, 0, 0, n
);
925 if ((ret
< 0) && (errno
== EEXIST
))
931 static int ipaddr_restore(void)
933 if (ipadd_dump_check_magic())
936 exit(rtnl_from_file(stdin
, &restore_handler
, NULL
));
939 static void free_nlmsg_chain(struct nlmsg_chain
*info
)
941 struct nlmsg_list
*l
, *n
;
943 for (l
= info
->head
; l
; l
= n
) {
949 static void ipaddr_filter(struct nlmsg_chain
*linfo
, struct nlmsg_chain
*ainfo
)
951 struct nlmsg_list
*l
, **lp
;
954 while ( (l
= *lp
) != NULL
) {
956 int missing_net_address
= 1;
957 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
958 struct nlmsg_list
*a
;
960 for (a
= ainfo
->head
; a
; a
= a
->next
) {
961 struct nlmsghdr
*n
= &a
->h
;
962 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
963 struct rtattr
*tb
[IFA_MAX
+ 1];
964 unsigned int ifa_flags
;
966 if (ifa
->ifa_index
!= ifi
->ifi_index
)
968 missing_net_address
= 0;
969 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
971 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
974 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
975 ifa_flags
= get_ifa_flags(ifa
, tb
[IFA_FLAGS
]);
977 if ((filter
.flags
^ ifa_flags
) & filter
.flagmask
)
979 if (filter
.pfx
.family
|| filter
.label
) {
981 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
983 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
985 memset(&dst
, 0, sizeof(dst
));
986 dst
.family
= ifa
->ifa_family
;
987 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
988 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
995 label
= RTA_DATA(tb
[IFA_LABEL
]);
997 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
998 if (fnmatch(filter
.label
, label
, 0) != 0)
1006 if (missing_net_address
&&
1007 (filter
.family
== AF_UNSPEC
|| filter
.family
== AF_PACKET
))
1017 static int ipaddr_flush(void)
1020 char flushb
[4096-512];
1022 filter
.flushb
= flushb
;
1024 filter
.flushe
= sizeof(flushb
);
1026 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
1027 const struct rtnl_dump_filter_arg a
[3] = {
1029 .filter
= print_addrinfo_secondary
,
1033 .filter
= print_addrinfo_primary
,
1041 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1042 perror("Cannot send dump request");
1046 if (rtnl_dump_filter_l(&rth
, a
) < 0) {
1047 fprintf(stderr
, "Flush terminated\n");
1050 if (filter
.flushed
== 0) {
1054 printf("Nothing to flush.\n");
1056 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
1062 if (flush_update() < 0)
1066 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
1070 /* If we are flushing, and specifying primary, then we
1071 * want to flush only a single round. Otherwise, we'll
1072 * start flushing secondaries that were promoted to
1075 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
1078 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
1083 static int ipaddr_list_flush_or_save(int argc
, char **argv
, int action
)
1085 struct nlmsg_chain linfo
= { NULL
, NULL
};
1086 struct nlmsg_chain ainfo
= { NULL
, NULL
};
1087 struct nlmsg_list
*l
;
1088 char *filter_dev
= NULL
;
1091 ipaddr_reset_filter(oneline
);
1092 filter
.showqueue
= 1;
1094 if (filter
.family
== AF_UNSPEC
)
1095 filter
.family
= preferred_family
;
1099 if (action
== IPADD_FLUSH
) {
1101 fprintf(stderr
, "Flush requires arguments.\n");
1105 if (filter
.family
== AF_PACKET
) {
1106 fprintf(stderr
, "Cannot flush link addresses.\n");
1112 if (strcmp(*argv
, "to") == 0) {
1114 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
1115 if (filter
.family
== AF_UNSPEC
)
1116 filter
.family
= filter
.pfx
.family
;
1117 } else if (strcmp(*argv
, "scope") == 0) {
1120 filter
.scopemask
= -1;
1121 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
1122 if (strcmp(*argv
, "all") != 0)
1123 invarg("invalid \"scope\"\n", *argv
);
1124 scope
= RT_SCOPE_NOWHERE
;
1125 filter
.scopemask
= 0;
1127 filter
.scope
= scope
;
1128 } else if (strcmp(*argv
, "up") == 0) {
1130 } else if (strcmp(*argv
, "dynamic") == 0) {
1131 filter
.flags
&= ~IFA_F_PERMANENT
;
1132 filter
.flagmask
|= IFA_F_PERMANENT
;
1133 } else if (strcmp(*argv
, "permanent") == 0) {
1134 filter
.flags
|= IFA_F_PERMANENT
;
1135 filter
.flagmask
|= IFA_F_PERMANENT
;
1136 } else if (strcmp(*argv
, "secondary") == 0 ||
1137 strcmp(*argv
, "temporary") == 0) {
1138 filter
.flags
|= IFA_F_SECONDARY
;
1139 filter
.flagmask
|= IFA_F_SECONDARY
;
1140 } else if (strcmp(*argv
, "primary") == 0) {
1141 filter
.flags
&= ~IFA_F_SECONDARY
;
1142 filter
.flagmask
|= IFA_F_SECONDARY
;
1143 } else if (strcmp(*argv
, "tentative") == 0) {
1144 filter
.flags
|= IFA_F_TENTATIVE
;
1145 filter
.flagmask
|= IFA_F_TENTATIVE
;
1146 } else if (strcmp(*argv
, "deprecated") == 0) {
1147 filter
.flags
|= IFA_F_DEPRECATED
;
1148 filter
.flagmask
|= IFA_F_DEPRECATED
;
1149 } else if (strcmp(*argv
, "home") == 0) {
1150 filter
.flags
|= IFA_F_HOMEADDRESS
;
1151 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
1152 } else if (strcmp(*argv
, "nodad") == 0) {
1153 filter
.flags
|= IFA_F_NODAD
;
1154 filter
.flagmask
|= IFA_F_NODAD
;
1155 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1156 filter
.flags
|= IFA_F_MANAGETEMPADDR
;
1157 filter
.flagmask
|= IFA_F_MANAGETEMPADDR
;
1158 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1159 filter
.flags
|= IFA_F_NOPREFIXROUTE
;
1160 filter
.flagmask
|= IFA_F_NOPREFIXROUTE
;
1161 } else if (strcmp(*argv
, "dadfailed") == 0) {
1162 filter
.flags
|= IFA_F_DADFAILED
;
1163 filter
.flagmask
|= IFA_F_DADFAILED
;
1164 } else if (strcmp(*argv
, "label") == 0) {
1166 filter
.label
= *argv
;
1167 } else if (strcmp(*argv
, "group") == 0) {
1169 if (rtnl_group_a2n(&filter
.group
, *argv
))
1170 invarg("Invalid \"group\" value\n", *argv
);
1172 if (strcmp(*argv
, "dev") == 0) {
1175 if (matches(*argv
, "help") == 0)
1178 duparg2("dev", *argv
);
1185 filter
.ifindex
= ll_name_to_index(filter_dev
);
1186 if (filter
.ifindex
<= 0) {
1187 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
1192 if (action
== IPADD_FLUSH
)
1193 return ipaddr_flush();
1195 if (action
== IPADD_SAVE
) {
1196 if (ipadd_save_prep())
1199 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETADDR
) < 0) {
1200 perror("Cannot send dump request");
1204 if (rtnl_dump_filter(&rth
, save_nlmsg
, stdout
) < 0) {
1205 fprintf(stderr
, "Save terminated\n");
1212 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
1213 perror("Cannot send dump request");
1217 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
1218 fprintf(stderr
, "Dump terminated\n");
1222 if (filter
.family
!= AF_PACKET
) {
1226 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
1227 perror("Cannot send dump request");
1231 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
1232 fprintf(stderr
, "Dump terminated\n");
1236 ipaddr_filter(&linfo
, &ainfo
);
1239 for (l
= linfo
.head
; l
; l
= l
->next
) {
1240 if (no_link
|| print_linkinfo(NULL
, &l
->h
, stdout
) == 0) {
1241 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1242 if (filter
.family
!= AF_PACKET
)
1243 print_selected_addrinfo(ifi
->ifi_index
,
1244 ainfo
.head
, stdout
);
1249 free_nlmsg_chain(&ainfo
);
1250 free_nlmsg_chain(&linfo
);
1255 int ipaddr_list_link(int argc
, char **argv
)
1257 preferred_family
= AF_PACKET
;
1259 return ipaddr_list_flush_or_save(argc
, argv
, IPADD_LIST
);
1262 void ipaddr_reset_filter(int oneline
)
1264 memset(&filter
, 0, sizeof(filter
));
1265 filter
.oneline
= oneline
;
1268 static int default_scope(inet_prefix
*lcl
)
1270 if (lcl
->family
== AF_INET
) {
1271 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1272 return RT_SCOPE_HOST
;
1277 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1281 struct ifaddrmsg ifa
;
1286 char *lcl_arg
= NULL
;
1287 char *valid_lftp
= NULL
;
1288 char *preferred_lftp
= NULL
;
1296 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1297 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1298 struct ifa_cacheinfo cinfo
;
1299 unsigned int ifa_flags
= 0;
1301 memset(&req
, 0, sizeof(req
));
1303 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1304 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1305 req
.n
.nlmsg_type
= cmd
;
1306 req
.ifa
.ifa_family
= preferred_family
;
1309 if (strcmp(*argv
, "peer") == 0 ||
1310 strcmp(*argv
, "remote") == 0) {
1314 duparg("peer", *argv
);
1315 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1316 peer_len
= peer
.bytelen
;
1317 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1318 req
.ifa
.ifa_family
= peer
.family
;
1319 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1320 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1321 } else if (matches(*argv
, "broadcast") == 0 ||
1322 strcmp(*argv
, "brd") == 0) {
1326 duparg("broadcast", *argv
);
1327 if (strcmp(*argv
, "+") == 0)
1329 else if (strcmp(*argv
, "-") == 0)
1332 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1333 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1334 req
.ifa
.ifa_family
= addr
.family
;
1335 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1336 brd_len
= addr
.bytelen
;
1338 } else if (strcmp(*argv
, "anycast") == 0) {
1342 duparg("anycast", *argv
);
1343 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1344 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1345 req
.ifa
.ifa_family
= addr
.family
;
1346 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1347 any_len
= addr
.bytelen
;
1348 } else if (strcmp(*argv
, "scope") == 0) {
1351 if (rtnl_rtscope_a2n(&scope
, *argv
))
1352 invarg("invalid scope value.", *argv
);
1353 req
.ifa
.ifa_scope
= scope
;
1355 } else if (strcmp(*argv
, "dev") == 0) {
1358 } else if (strcmp(*argv
, "label") == 0) {
1361 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1362 } else if (matches(*argv
, "valid_lft") == 0) {
1364 duparg("valid_lft", *argv
);
1367 if (set_lifetime(&valid_lft
, *argv
))
1368 invarg("valid_lft value", *argv
);
1369 } else if (matches(*argv
, "preferred_lft") == 0) {
1371 duparg("preferred_lft", *argv
);
1373 preferred_lftp
= *argv
;
1374 if (set_lifetime(&preferred_lft
, *argv
))
1375 invarg("preferred_lft value", *argv
);
1376 } else if (strcmp(*argv
, "home") == 0) {
1377 ifa_flags
|= IFA_F_HOMEADDRESS
;
1378 } else if (strcmp(*argv
, "nodad") == 0) {
1379 ifa_flags
|= IFA_F_NODAD
;
1380 } else if (strcmp(*argv
, "mngtmpaddr") == 0) {
1381 ifa_flags
|= IFA_F_MANAGETEMPADDR
;
1382 } else if (strcmp(*argv
, "noprefixroute") == 0) {
1383 ifa_flags
|= IFA_F_NOPREFIXROUTE
;
1385 if (strcmp(*argv
, "local") == 0) {
1388 if (matches(*argv
, "help") == 0)
1391 duparg2("local", *argv
);
1393 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1394 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1395 req
.ifa
.ifa_family
= lcl
.family
;
1396 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1397 local_len
= lcl
.bytelen
;
1401 req
.ifa
.ifa_flags
= ifa_flags
;
1402 addattr32(&req
.n
, sizeof(req
), IFA_FLAGS
, ifa_flags
);
1405 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1408 if (l
&& matches(d
, l
) != 0) {
1409 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1413 if (peer_len
== 0 && local_len
) {
1414 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1416 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1417 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1418 " This special behaviour is likely to disappear in further releases,\n" \
1419 " fix your scripts!\n", lcl_arg
, local_len
*8);
1422 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1425 if (req
.ifa
.ifa_prefixlen
== 0)
1426 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1428 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1431 if (req
.ifa
.ifa_family
!= AF_INET
) {
1432 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1436 if (brd
.bitlen
<= 30) {
1437 for (i
=31; i
>=brd
.bitlen
; i
--) {
1439 brd
.data
[0] |= htonl(1<<(31-i
));
1441 brd
.data
[0] &= ~htonl(1<<(31-i
));
1443 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1444 brd_len
= brd
.bytelen
;
1447 if (!scoped
&& cmd
!= RTM_DELADDR
)
1448 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1450 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1451 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1455 if (valid_lftp
|| preferred_lftp
) {
1457 fprintf(stderr
, "valid_lft is zero\n");
1460 if (valid_lft
< preferred_lft
) {
1461 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1465 memset(&cinfo
, 0, sizeof(cinfo
));
1466 cinfo
.ifa_prefered
= preferred_lft
;
1467 cinfo
.ifa_valid
= valid_lft
;
1468 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1472 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
1478 int do_ipaddr(int argc
, char **argv
)
1481 return ipaddr_list_flush_or_save(0, NULL
, IPADD_LIST
);
1482 if (matches(*argv
, "add") == 0)
1483 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1484 if (matches(*argv
, "change") == 0 ||
1485 strcmp(*argv
, "chg") == 0)
1486 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
1487 if (matches(*argv
, "replace") == 0)
1488 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
1489 if (matches(*argv
, "delete") == 0)
1490 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
1491 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1492 || matches(*argv
, "lst") == 0)
1493 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_LIST
);
1494 if (matches(*argv
, "flush") == 0)
1495 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_FLUSH
);
1496 if (matches(*argv
, "save") == 0)
1497 return ipaddr_list_flush_or_save(argc
-1, argv
+1, IPADD_SAVE
);
1498 if (matches(*argv
, "showdump") == 0)
1499 return ipaddr_showdump();
1500 if (matches(*argv
, "restore") == 0)
1501 return ipaddr_restore();
1502 if (matches(*argv
, "help") == 0)
1504 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv
);