]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipaddress.c
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>
22 #include <sys/errno.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"
58 static void usage(void) __attribute__((noreturn
));
60 static void usage(void)
65 fprintf(stderr
, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
66 fprintf(stderr
, " [ CONFFLAG-LIST ]\n");
67 fprintf(stderr
, " ip addr del IFADDR dev STRING\n");
68 fprintf(stderr
, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
69 fprintf(stderr
, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n");
70 fprintf(stderr
, "IFADDR := PREFIX | ADDR peer PREFIX\n");
71 fprintf(stderr
, " [ broadcast ADDR ] [ anycast ADDR ]\n");
72 fprintf(stderr
, " [ label STRING ] [ scope SCOPE-ID ]\n");
73 fprintf(stderr
, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
74 fprintf(stderr
, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
75 fprintf(stderr
, "FLAG := [ permanent | dynamic | secondary | primary |\n");
76 fprintf(stderr
, " tentative | deprecated | dadfailed | temporary |\n");
77 fprintf(stderr
, " CONFFLAG-LIST ]\n");
78 fprintf(stderr
, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
79 fprintf(stderr
, "CONFFLAG := [ home | nodad ]\n");
80 fprintf(stderr
, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
81 fprintf(stderr
, "LFT := forever | SECONDS\n");
86 void print_link_flags(FILE *fp
, unsigned flags
, unsigned mdown
)
89 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
90 fprintf(fp
, "NO-CARRIER%s", flags
? "," : "");
91 flags
&= ~IFF_RUNNING
;
92 #define _PF(f) if (flags&IFF_##f) { \
94 fprintf(fp, #f "%s", flags ? "," : ""); }
115 fprintf(fp
, "%x", flags
);
117 fprintf(fp
, ",M-DOWN");
121 static const char *oper_states
[] = {
122 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
123 "TESTING", "DORMANT", "UP"
126 static void print_operstate(FILE *f
, __u8 state
)
128 if (state
>= sizeof(oper_states
)/sizeof(oper_states
[0]))
129 fprintf(f
, "state %#x ", state
);
131 fprintf(f
, "state %s ", oper_states
[state
]);
134 int get_operstate(const char *name
)
138 for (i
= 0; i
< sizeof(oper_states
)/sizeof(oper_states
[0]); i
++)
139 if (strcasecmp(name
, oper_states
[i
]) == 0)
144 static void print_queuelen(FILE *f
, struct rtattr
*tb
[IFLA_MAX
+ 1])
149 qlen
= *(int *)RTA_DATA(tb
[IFLA_TXQLEN
]);
152 int s
= socket(AF_INET
, SOCK_STREAM
, 0);
157 memset(&ifr
, 0, sizeof(ifr
));
158 strcpy(ifr
.ifr_name
, rta_getattr_str(tb
[IFLA_IFNAME
]));
159 if (ioctl(s
, SIOCGIFTXQLEN
, &ifr
) < 0) {
160 fprintf(f
, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno
));
168 fprintf(f
, "qlen %d", qlen
);
171 static const char *link_modes
[] = {
175 static void print_linkmode(FILE *f
, struct rtattr
*tb
)
177 unsigned int mode
= rta_getattr_u8(tb
);
179 if (mode
>= sizeof(link_modes
) / sizeof(link_modes
[0]))
180 fprintf(f
, "mode %d ", mode
);
182 fprintf(f
, "mode %s ", link_modes
[mode
]);
185 static void print_linktype(FILE *fp
, struct rtattr
*tb
)
187 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
188 struct link_util
*lu
;
191 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
193 if (!linkinfo
[IFLA_INFO_KIND
])
195 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
197 fprintf(fp
, "%s", _SL_
);
198 fprintf(fp
, " %s ", kind
);
200 lu
= get_link_kind(kind
);
201 if (!lu
|| !lu
->print_opt
)
205 struct rtattr
*attr
[lu
->maxattr
+1], **data
= NULL
;
207 if (linkinfo
[IFLA_INFO_DATA
]) {
208 parse_rtattr_nested(attr
, lu
->maxattr
,
209 linkinfo
[IFLA_INFO_DATA
]);
212 lu
->print_opt(lu
, fp
, data
);
214 if (linkinfo
[IFLA_INFO_XSTATS
] && show_stats
&&
216 lu
->print_xstats(lu
, fp
, linkinfo
[IFLA_INFO_XSTATS
]);
220 static void print_vfinfo(FILE *fp
, struct rtattr
*vfinfo
)
222 struct ifla_vf_mac
*vf_mac
;
223 struct ifla_vf_vlan
*vf_vlan
;
224 struct ifla_vf_tx_rate
*vf_tx_rate
;
225 struct ifla_vf_spoofchk
*vf_spoofchk
;
226 struct rtattr
*vf
[IFLA_VF_MAX
+1];
230 if (vfinfo
->rta_type
!= IFLA_VF_INFO
) {
231 fprintf(stderr
, "BUG: rta type is %d\n", vfinfo
->rta_type
);
235 parse_rtattr_nested(vf
, IFLA_VF_MAX
, vfinfo
);
237 vf_mac
= RTA_DATA(vf
[IFLA_VF_MAC
]);
238 vf_vlan
= RTA_DATA(vf
[IFLA_VF_VLAN
]);
239 vf_tx_rate
= RTA_DATA(vf
[IFLA_VF_TX_RATE
]);
241 /* Check if the spoof checking vf info type is supported by
244 tmp
= (struct rtattr
*)((char *)vf
[IFLA_VF_TX_RATE
] +
245 vf
[IFLA_VF_TX_RATE
]->rta_len
);
247 if (tmp
->rta_type
!= IFLA_VF_SPOOFCHK
)
250 vf_spoofchk
= RTA_DATA(vf
[IFLA_VF_SPOOFCHK
]);
252 fprintf(fp
, "\n vf %d MAC %s", vf_mac
->vf
,
253 ll_addr_n2a((unsigned char *)&vf_mac
->mac
,
254 ETH_ALEN
, 0, b1
, sizeof(b1
)));
256 fprintf(fp
, ", vlan %d", vf_vlan
->vlan
);
258 fprintf(fp
, ", qos %d", vf_vlan
->qos
);
259 if (vf_tx_rate
->rate
)
260 fprintf(fp
, ", tx rate %d (Mbps)", vf_tx_rate
->rate
);
261 if (vf_spoofchk
&& vf_spoofchk
->setting
!= -1) {
262 if (vf_spoofchk
->setting
)
263 fprintf(fp
, ", spoof checking on");
265 fprintf(fp
, ", spoof checking off");
269 static void print_link_stats64(FILE *fp
, const struct rtnl_link_stats64
*s
) {
270 fprintf(fp
, "%s", _SL_
);
271 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
272 s
->rx_compressed
? "compressed" : "", _SL_
);
273 fprintf(fp
, " %-10"PRIu64
" %-8"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
274 (uint64_t)s
->rx_bytes
,
275 (uint64_t)s
->rx_packets
,
276 (uint64_t)s
->rx_errors
,
277 (uint64_t)s
->rx_dropped
,
278 (uint64_t)s
->rx_over_errors
,
279 (uint64_t)s
->multicast
);
280 if (s
->rx_compressed
)
281 fprintf(fp
, " %-7"PRIu64
"",
282 (uint64_t)s
->rx_compressed
);
283 if (show_stats
> 1) {
284 fprintf(fp
, "%s", _SL_
);
285 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
286 fprintf(fp
, " %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
287 (uint64_t)s
->rx_length_errors
,
288 (uint64_t)s
->rx_crc_errors
,
289 (uint64_t)s
->rx_frame_errors
,
290 (uint64_t)s
->rx_fifo_errors
,
291 (uint64_t)s
->rx_missed_errors
);
293 fprintf(fp
, "%s", _SL_
);
294 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
295 (uint64_t)s
->tx_compressed
? "compressed" : "", _SL_
);
296 fprintf(fp
, " %-10"PRIu64
" %-8"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
297 (uint64_t)s
->tx_bytes
,
298 (uint64_t)s
->tx_packets
,
299 (uint64_t)s
->tx_errors
,
300 (uint64_t)s
->tx_dropped
,
301 (uint64_t)s
->tx_carrier_errors
,
302 (uint64_t)s
->collisions
);
303 if (s
->tx_compressed
)
304 fprintf(fp
, " %-7"PRIu64
"",
305 (uint64_t)s
->tx_compressed
);
306 if (show_stats
> 1) {
307 fprintf(fp
, "%s", _SL_
);
308 fprintf(fp
, " TX errors: aborted fifo window heartbeat%s", _SL_
);
309 fprintf(fp
, " %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
" %-7"PRIu64
"",
310 (uint64_t)s
->tx_aborted_errors
,
311 (uint64_t)s
->tx_fifo_errors
,
312 (uint64_t)s
->tx_window_errors
,
313 (uint64_t)s
->tx_heartbeat_errors
);
317 static void print_link_stats(FILE *fp
, const struct rtnl_link_stats
*s
)
319 fprintf(fp
, "%s", _SL_
);
320 fprintf(fp
, " RX: bytes packets errors dropped overrun mcast %s%s",
321 s
->rx_compressed
? "compressed" : "", _SL_
);
322 fprintf(fp
, " %-10u %-8u %-7u %-7u %-7u %-7u",
323 s
->rx_bytes
, s
->rx_packets
, s
->rx_errors
,
324 s
->rx_dropped
, s
->rx_over_errors
,
327 if (s
->rx_compressed
)
328 fprintf(fp
, " %-7u", s
->rx_compressed
);
329 if (show_stats
> 1) {
330 fprintf(fp
, "%s", _SL_
);
331 fprintf(fp
, " RX errors: length crc frame fifo missed%s", _SL_
);
332 fprintf(fp
, " %-7u %-7u %-7u %-7u %-7u",
340 fprintf(fp
, "%s", _SL_
);
341 fprintf(fp
, " TX: bytes packets errors dropped carrier collsns %s%s",
342 s
->tx_compressed
? "compressed" : "", _SL_
);
343 fprintf(fp
, " %-10u %-8u %-7u %-7u %-7u %-7u",
344 s
->tx_bytes
, s
->tx_packets
, s
->tx_errors
,
345 s
->tx_dropped
, s
->tx_carrier_errors
, s
->collisions
);
346 if (s
->tx_compressed
)
347 fprintf(fp
, " %-7u", s
->tx_compressed
);
348 if (show_stats
> 1) {
349 fprintf(fp
, "%s", _SL_
);
350 fprintf(fp
, " TX errors: aborted fifo window heartbeat%s", _SL_
);
351 fprintf(fp
, " %-7u %-7u %-7u %-7u",
352 s
->tx_aborted_errors
,
355 s
->tx_heartbeat_errors
360 int print_linkinfo(const struct sockaddr_nl
*who
,
361 struct nlmsghdr
*n
, void *arg
)
363 FILE *fp
= (FILE*)arg
;
364 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
365 struct rtattr
* tb
[IFLA_MAX
+1];
366 int len
= n
->nlmsg_len
;
369 if (n
->nlmsg_type
!= RTM_NEWLINK
&& n
->nlmsg_type
!= RTM_DELLINK
)
372 len
-= NLMSG_LENGTH(sizeof(*ifi
));
376 if (filter
.ifindex
&& ifi
->ifi_index
!= filter
.ifindex
)
378 if (filter
.up
&& !(ifi
->ifi_flags
&IFF_UP
))
381 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
382 if (tb
[IFLA_IFNAME
] == NULL
) {
383 fprintf(stderr
, "BUG: device with ifindex %d has nil ifname\n", ifi
->ifi_index
);
386 (!filter
.family
|| filter
.family
== AF_PACKET
) &&
387 fnmatch(filter
.label
, RTA_DATA(tb
[IFLA_IFNAME
]), 0))
390 if (tb
[IFLA_GROUP
]) {
391 int group
= *(int*)RTA_DATA(tb
[IFLA_GROUP
]);
392 if (group
!= filter
.group
)
396 if (n
->nlmsg_type
== RTM_DELLINK
)
397 fprintf(fp
, "Deleted ");
399 fprintf(fp
, "%d: %s", ifi
->ifi_index
,
400 tb
[IFLA_IFNAME
] ? rta_getattr_str(tb
[IFLA_IFNAME
]) : "<nil>");
404 int iflink
= *(int*)RTA_DATA(tb
[IFLA_LINK
]);
406 fprintf(fp
, "@NONE: ");
408 fprintf(fp
, "@%s: ", ll_idx_n2a(iflink
, b1
));
409 m_flag
= ll_index_to_flags(iflink
);
410 m_flag
= !(m_flag
& IFF_UP
);
415 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
418 fprintf(fp
, "mtu %u ", *(int*)RTA_DATA(tb
[IFLA_MTU
]));
420 fprintf(fp
, "qdisc %s ", rta_getattr_str(tb
[IFLA_QDISC
]));
421 if (tb
[IFLA_MASTER
]) {
423 fprintf(fp
, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb
[IFLA_MASTER
]), b1
));
426 if (tb
[IFLA_OPERSTATE
])
427 print_operstate(fp
, rta_getattr_u8(tb
[IFLA_OPERSTATE
]));
429 if (do_link
&& tb
[IFLA_LINKMODE
])
430 print_linkmode(fp
, tb
[IFLA_LINKMODE
]);
432 if (filter
.showqueue
)
433 print_queuelen(fp
, tb
);
435 if (!filter
.family
|| filter
.family
== AF_PACKET
) {
437 fprintf(fp
, "%s", _SL_
);
438 fprintf(fp
, " link/%s ", ll_type_n2a(ifi
->ifi_type
, b1
, sizeof(b1
)));
440 if (tb
[IFLA_ADDRESS
]) {
441 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_ADDRESS
]),
442 RTA_PAYLOAD(tb
[IFLA_ADDRESS
]),
446 if (tb
[IFLA_BROADCAST
]) {
447 if (ifi
->ifi_flags
&IFF_POINTOPOINT
)
448 fprintf(fp
, " peer ");
450 fprintf(fp
, " brd ");
451 fprintf(fp
, "%s", ll_addr_n2a(RTA_DATA(tb
[IFLA_BROADCAST
]),
452 RTA_PAYLOAD(tb
[IFLA_BROADCAST
]),
458 if (do_link
&& tb
[IFLA_LINKINFO
] && show_details
)
459 print_linktype(fp
, tb
[IFLA_LINKINFO
]);
461 if (do_link
&& tb
[IFLA_IFALIAS
])
462 fprintf(fp
,"\n alias %s",
463 rta_getattr_str(tb
[IFLA_IFALIAS
]));
465 if (do_link
&& show_stats
) {
466 if (tb
[IFLA_STATS64
])
467 print_link_stats64(fp
, RTA_DATA(tb
[IFLA_STATS64
]));
468 else if (tb
[IFLA_STATS
])
469 print_link_stats(fp
, RTA_DATA(tb
[IFLA_STATS
]));
472 if (do_link
&& tb
[IFLA_VFINFO_LIST
] && tb
[IFLA_NUM_VF
]) {
473 struct rtattr
*i
, *vflist
= tb
[IFLA_VFINFO_LIST
];
474 int rem
= RTA_PAYLOAD(vflist
);
475 for (i
= RTA_DATA(vflist
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
484 static int flush_update(void)
486 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
487 perror("Failed to send flush request");
494 static int set_lifetime(unsigned int *lifetime
, char *argv
)
496 if (strcmp(argv
, "forever") == 0)
497 *lifetime
= INFINITY_LIFE_TIME
;
498 else if (get_u32(lifetime
, argv
, 0))
504 int print_addrinfo(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
507 FILE *fp
= (FILE*)arg
;
508 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
509 int len
= n
->nlmsg_len
;
511 /* Use local copy of ifa_flags to not interfere with filtering code */
512 unsigned int ifa_flags
;
513 struct rtattr
* rta_tb
[IFA_MAX
+1];
517 if (n
->nlmsg_type
!= RTM_NEWADDR
&& n
->nlmsg_type
!= RTM_DELADDR
)
519 len
-= NLMSG_LENGTH(sizeof(*ifa
));
521 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
525 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWADDR
)
528 parse_rtattr(rta_tb
, IFA_MAX
, IFA_RTA(ifa
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
530 if (!rta_tb
[IFA_LOCAL
])
531 rta_tb
[IFA_LOCAL
] = rta_tb
[IFA_ADDRESS
];
532 if (!rta_tb
[IFA_ADDRESS
])
533 rta_tb
[IFA_ADDRESS
] = rta_tb
[IFA_LOCAL
];
535 if (filter
.ifindex
&& filter
.ifindex
!= ifa
->ifa_index
)
537 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
539 if ((filter
.flags
^ifa
->ifa_flags
)&filter
.flagmask
)
544 if (rta_tb
[IFA_LABEL
])
545 label
= RTA_DATA(rta_tb
[IFA_LABEL
]);
547 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
548 if (fnmatch(filter
.label
, label
, 0) != 0)
551 if (filter
.pfx
.family
) {
552 if (rta_tb
[IFA_LOCAL
]) {
554 memset(&dst
, 0, sizeof(dst
));
555 dst
.family
= ifa
->ifa_family
;
556 memcpy(&dst
.data
, RTA_DATA(rta_tb
[IFA_LOCAL
]), RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]));
557 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
562 if (filter
.family
&& filter
.family
!= ifa
->ifa_family
)
567 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
571 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
572 memcpy(fn
, n
, n
->nlmsg_len
);
573 fn
->nlmsg_type
= RTM_DELADDR
;
574 fn
->nlmsg_flags
= NLM_F_REQUEST
;
575 fn
->nlmsg_seq
= ++rth
.seq
;
576 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
582 if (n
->nlmsg_type
== RTM_DELADDR
)
583 fprintf(fp
, "Deleted ");
585 if (filter
.oneline
|| filter
.flushb
)
586 fprintf(fp
, "%u: %s", ifa
->ifa_index
, ll_index_to_name(ifa
->ifa_index
));
587 if (ifa
->ifa_family
== AF_INET
)
588 fprintf(fp
, " inet ");
589 else if (ifa
->ifa_family
== AF_INET6
)
590 fprintf(fp
, " inet6 ");
591 else if (ifa
->ifa_family
== AF_DECnet
)
592 fprintf(fp
, " dnet ");
593 else if (ifa
->ifa_family
== AF_IPX
)
594 fprintf(fp
, " ipx ");
596 fprintf(fp
, " family %d ", ifa
->ifa_family
);
598 if (rta_tb
[IFA_LOCAL
]) {
599 fprintf(fp
, "%s", rt_addr_n2a(ifa
->ifa_family
,
600 RTA_PAYLOAD(rta_tb
[IFA_LOCAL
]),
601 RTA_DATA(rta_tb
[IFA_LOCAL
]),
602 abuf
, sizeof(abuf
)));
604 if (rta_tb
[IFA_ADDRESS
] == NULL
||
605 memcmp(RTA_DATA(rta_tb
[IFA_ADDRESS
]), RTA_DATA(rta_tb
[IFA_LOCAL
]), 4) == 0) {
606 fprintf(fp
, "/%d ", ifa
->ifa_prefixlen
);
608 fprintf(fp
, " peer %s/%d ",
609 rt_addr_n2a(ifa
->ifa_family
,
610 RTA_PAYLOAD(rta_tb
[IFA_ADDRESS
]),
611 RTA_DATA(rta_tb
[IFA_ADDRESS
]),
617 if (rta_tb
[IFA_BROADCAST
]) {
618 fprintf(fp
, "brd %s ",
619 rt_addr_n2a(ifa
->ifa_family
,
620 RTA_PAYLOAD(rta_tb
[IFA_BROADCAST
]),
621 RTA_DATA(rta_tb
[IFA_BROADCAST
]),
622 abuf
, sizeof(abuf
)));
624 if (rta_tb
[IFA_ANYCAST
]) {
625 fprintf(fp
, "any %s ",
626 rt_addr_n2a(ifa
->ifa_family
,
627 RTA_PAYLOAD(rta_tb
[IFA_ANYCAST
]),
628 RTA_DATA(rta_tb
[IFA_ANYCAST
]),
629 abuf
, sizeof(abuf
)));
631 fprintf(fp
, "scope %s ", rtnl_rtscope_n2a(ifa
->ifa_scope
, b1
, sizeof(b1
)));
632 ifa_flags
= ifa
->ifa_flags
;
633 if (ifa
->ifa_flags
&IFA_F_SECONDARY
) {
634 ifa_flags
&= ~IFA_F_SECONDARY
;
635 if (ifa
->ifa_family
== AF_INET6
)
636 fprintf(fp
, "temporary ");
638 fprintf(fp
, "secondary ");
640 if (ifa
->ifa_flags
&IFA_F_TENTATIVE
) {
641 ifa_flags
&= ~IFA_F_TENTATIVE
;
642 fprintf(fp
, "tentative ");
644 if (ifa
->ifa_flags
&IFA_F_DEPRECATED
) {
645 ifa_flags
&= ~IFA_F_DEPRECATED
;
647 fprintf(fp
, "deprecated ");
649 if (ifa
->ifa_flags
&IFA_F_HOMEADDRESS
) {
650 ifa_flags
&= ~IFA_F_HOMEADDRESS
;
651 fprintf(fp
, "home ");
653 if (ifa
->ifa_flags
&IFA_F_NODAD
) {
654 ifa_flags
&= ~IFA_F_NODAD
;
655 fprintf(fp
, "nodad ");
657 if (!(ifa
->ifa_flags
&IFA_F_PERMANENT
)) {
658 fprintf(fp
, "dynamic ");
660 ifa_flags
&= ~IFA_F_PERMANENT
;
661 if (ifa
->ifa_flags
&IFA_F_DADFAILED
) {
662 ifa_flags
&= ~IFA_F_DADFAILED
;
663 fprintf(fp
, "dadfailed ");
666 fprintf(fp
, "flags %02x ", ifa_flags
);
667 if (rta_tb
[IFA_LABEL
])
668 fprintf(fp
, "%s", rta_getattr_str(rta_tb
[IFA_LABEL
]));
669 if (rta_tb
[IFA_CACHEINFO
]) {
670 struct ifa_cacheinfo
*ci
= RTA_DATA(rta_tb
[IFA_CACHEINFO
]);
671 fprintf(fp
, "%s", _SL_
);
672 fprintf(fp
, " valid_lft ");
673 if (ci
->ifa_valid
== INFINITY_LIFE_TIME
)
674 fprintf(fp
, "forever");
676 fprintf(fp
, "%usec", ci
->ifa_valid
);
677 fprintf(fp
, " preferred_lft ");
678 if (ci
->ifa_prefered
== INFINITY_LIFE_TIME
)
679 fprintf(fp
, "forever");
682 fprintf(fp
, "%dsec", ci
->ifa_prefered
);
684 fprintf(fp
, "%usec", ci
->ifa_prefered
);
692 int print_addrinfo_primary(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
695 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
697 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
700 return print_addrinfo(who
, n
, arg
);
703 int print_addrinfo_secondary(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
706 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
708 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
711 return print_addrinfo(who
, n
, arg
);
716 struct nlmsg_list
*next
;
722 struct nlmsg_list
*head
;
723 struct nlmsg_list
*tail
;
726 static int print_selected_addrinfo(int ifindex
, struct nlmsg_list
*ainfo
, FILE *fp
)
728 for ( ;ainfo
; ainfo
= ainfo
->next
) {
729 struct nlmsghdr
*n
= &ainfo
->h
;
730 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
732 if (n
->nlmsg_type
!= RTM_NEWADDR
)
735 if (n
->nlmsg_len
< NLMSG_LENGTH(sizeof(ifa
)))
738 if (ifa
->ifa_index
!= ifindex
||
739 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
742 print_addrinfo(NULL
, n
, fp
);
748 static int store_nlmsg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
751 struct nlmsg_chain
*lchain
= (struct nlmsg_chain
*)arg
;
752 struct nlmsg_list
*h
;
754 h
= malloc(n
->nlmsg_len
+sizeof(void*));
758 memcpy(&h
->h
, n
, n
->nlmsg_len
);
762 lchain
->tail
->next
= h
;
767 ll_remember_index(who
, n
, NULL
);
771 static int ipaddr_list_or_flush(int argc
, char **argv
, int flush
)
773 struct nlmsg_chain linfo
= { NULL
, NULL
};
774 struct nlmsg_chain ainfo
= { NULL
, NULL
};
775 struct nlmsg_list
*l
, *n
;
776 char *filter_dev
= NULL
;
779 ipaddr_reset_filter(oneline
);
780 filter
.showqueue
= 1;
782 if (filter
.family
== AF_UNSPEC
)
783 filter
.family
= preferred_family
;
785 filter
.group
= INIT_NETDEV_GROUP
;
789 fprintf(stderr
, "Flush requires arguments.\n");
793 if (filter
.family
== AF_PACKET
) {
794 fprintf(stderr
, "Cannot flush link addresses.\n");
800 if (strcmp(*argv
, "to") == 0) {
802 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
803 if (filter
.family
== AF_UNSPEC
)
804 filter
.family
= filter
.pfx
.family
;
805 } else if (strcmp(*argv
, "scope") == 0) {
808 filter
.scopemask
= -1;
809 if (rtnl_rtscope_a2n(&scope
, *argv
)) {
810 if (strcmp(*argv
, "all") != 0)
811 invarg("invalid \"scope\"\n", *argv
);
812 scope
= RT_SCOPE_NOWHERE
;
813 filter
.scopemask
= 0;
815 filter
.scope
= scope
;
816 } else if (strcmp(*argv
, "up") == 0) {
818 } else if (strcmp(*argv
, "dynamic") == 0) {
819 filter
.flags
&= ~IFA_F_PERMANENT
;
820 filter
.flagmask
|= IFA_F_PERMANENT
;
821 } else if (strcmp(*argv
, "permanent") == 0) {
822 filter
.flags
|= IFA_F_PERMANENT
;
823 filter
.flagmask
|= IFA_F_PERMANENT
;
824 } else if (strcmp(*argv
, "secondary") == 0 ||
825 strcmp(*argv
, "temporary") == 0) {
826 filter
.flags
|= IFA_F_SECONDARY
;
827 filter
.flagmask
|= IFA_F_SECONDARY
;
828 } else if (strcmp(*argv
, "primary") == 0) {
829 filter
.flags
&= ~IFA_F_SECONDARY
;
830 filter
.flagmask
|= IFA_F_SECONDARY
;
831 } else if (strcmp(*argv
, "tentative") == 0) {
832 filter
.flags
|= IFA_F_TENTATIVE
;
833 filter
.flagmask
|= IFA_F_TENTATIVE
;
834 } else if (strcmp(*argv
, "deprecated") == 0) {
835 filter
.flags
|= IFA_F_DEPRECATED
;
836 filter
.flagmask
|= IFA_F_DEPRECATED
;
837 } else if (strcmp(*argv
, "home") == 0) {
838 filter
.flags
|= IFA_F_HOMEADDRESS
;
839 filter
.flagmask
|= IFA_F_HOMEADDRESS
;
840 } else if (strcmp(*argv
, "nodad") == 0) {
841 filter
.flags
|= IFA_F_NODAD
;
842 filter
.flagmask
|= IFA_F_NODAD
;
843 } else if (strcmp(*argv
, "dadfailed") == 0) {
844 filter
.flags
|= IFA_F_DADFAILED
;
845 filter
.flagmask
|= IFA_F_DADFAILED
;
846 } else if (strcmp(*argv
, "label") == 0) {
848 filter
.label
= *argv
;
849 } else if (strcmp(*argv
, "group") == 0) {
851 if (rtnl_group_a2n(&filter
.group
, *argv
))
852 invarg("Invalid \"group\" value\n", *argv
);
854 if (strcmp(*argv
, "dev") == 0) {
857 if (matches(*argv
, "help") == 0)
860 duparg2("dev", *argv
);
866 if (rtnl_wilddump_request(&rth
, preferred_family
, RTM_GETLINK
) < 0) {
867 perror("Cannot send dump request");
871 if (rtnl_dump_filter(&rth
, store_nlmsg
, &linfo
) < 0) {
872 fprintf(stderr
, "Dump terminated\n");
877 filter
.ifindex
= ll_name_to_index(filter_dev
);
878 if (filter
.ifindex
<= 0) {
879 fprintf(stderr
, "Device \"%s\" does not exist.\n", filter_dev
);
886 char flushb
[4096-512];
888 filter
.flushb
= flushb
;
890 filter
.flushe
= sizeof(flushb
);
892 while ((max_flush_loops
== 0) || (round
< max_flush_loops
)) {
893 const struct rtnl_dump_filter_arg a
[3] = {
895 .filter
= print_addrinfo_secondary
,
899 .filter
= print_addrinfo_primary
,
907 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
908 perror("Cannot send dump request");
912 if (rtnl_dump_filter_l(&rth
, a
) < 0) {
913 fprintf(stderr
, "Flush terminated\n");
916 if (filter
.flushed
== 0) {
920 printf("Nothing to flush.\n");
922 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
928 if (flush_update() < 0)
932 printf("\n*** Round %d, deleting %d addresses ***\n", round
, filter
.flushed
);
936 /* If we are flushing, and specifying primary, then we
937 * want to flush only a single round. Otherwise, we'll
938 * start flushing secondaries that were promoted to
941 if (!(filter
.flags
& IFA_F_SECONDARY
) && (filter
.flagmask
& IFA_F_SECONDARY
))
944 fprintf(stderr
, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops
);
949 if (filter
.family
!= AF_PACKET
) {
950 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETADDR
) < 0) {
951 perror("Cannot send dump request");
955 if (rtnl_dump_filter(&rth
, store_nlmsg
, &ainfo
) < 0) {
956 fprintf(stderr
, "Dump terminated\n");
962 if (filter
.family
&& filter
.family
!= AF_PACKET
) {
963 struct nlmsg_list
**lp
;
969 while ((l
=*lp
)!=NULL
) {
971 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
972 struct nlmsg_list
*a
;
974 for (a
= ainfo
.head
; a
; a
= a
->next
) {
975 struct nlmsghdr
*n
= &a
->h
;
976 struct ifaddrmsg
*ifa
= NLMSG_DATA(n
);
978 if (ifa
->ifa_index
!= ifi
->ifi_index
||
979 (filter
.family
&& filter
.family
!= ifa
->ifa_family
))
981 if ((filter
.scope
^ifa
->ifa_scope
)&filter
.scopemask
)
983 if ((filter
.flags
^ifa
->ifa_flags
)&filter
.flagmask
)
985 if (filter
.pfx
.family
|| filter
.label
) {
986 struct rtattr
*tb
[IFA_MAX
+1];
987 parse_rtattr(tb
, IFA_MAX
, IFA_RTA(ifa
), IFA_PAYLOAD(n
));
989 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
991 if (filter
.pfx
.family
&& tb
[IFA_LOCAL
]) {
993 memset(&dst
, 0, sizeof(dst
));
994 dst
.family
= ifa
->ifa_family
;
995 memcpy(&dst
.data
, RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_LOCAL
]));
996 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
1003 label
= RTA_DATA(tb
[IFA_LABEL
]);
1005 label
= ll_idx_n2a(ifa
->ifa_index
, b1
);
1006 if (fnmatch(filter
.label
, label
, 0) != 0)
1021 for (l
= linfo
.head
; l
; l
= n
) {
1023 if (no_link
|| print_linkinfo(NULL
, &l
->h
, stdout
) == 0) {
1024 struct ifinfomsg
*ifi
= NLMSG_DATA(&l
->h
);
1025 if (filter
.family
!= AF_PACKET
)
1026 print_selected_addrinfo(ifi
->ifi_index
, ainfo
.head
, stdout
);
1035 int ipaddr_list_link(int argc
, char **argv
)
1037 preferred_family
= AF_PACKET
;
1039 return ipaddr_list_or_flush(argc
, argv
, 0);
1042 void ipaddr_reset_filter(int oneline
)
1044 memset(&filter
, 0, sizeof(filter
));
1045 filter
.oneline
= oneline
;
1048 static int default_scope(inet_prefix
*lcl
)
1050 if (lcl
->family
== AF_INET
) {
1051 if (lcl
->bytelen
>= 1 && *(__u8
*)&lcl
->data
== 127)
1052 return RT_SCOPE_HOST
;
1057 static int ipaddr_modify(int cmd
, int flags
, int argc
, char **argv
)
1061 struct ifaddrmsg ifa
;
1066 char *lcl_arg
= NULL
;
1067 char *valid_lftp
= NULL
;
1068 char *preferred_lftp
= NULL
;
1076 __u32 preferred_lft
= INFINITY_LIFE_TIME
;
1077 __u32 valid_lft
= INFINITY_LIFE_TIME
;
1078 struct ifa_cacheinfo cinfo
;
1080 memset(&req
, 0, sizeof(req
));
1082 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifaddrmsg
));
1083 req
.n
.nlmsg_flags
= NLM_F_REQUEST
| flags
;
1084 req
.n
.nlmsg_type
= cmd
;
1085 req
.ifa
.ifa_family
= preferred_family
;
1088 if (strcmp(*argv
, "peer") == 0 ||
1089 strcmp(*argv
, "remote") == 0) {
1093 duparg("peer", *argv
);
1094 get_prefix(&peer
, *argv
, req
.ifa
.ifa_family
);
1095 peer_len
= peer
.bytelen
;
1096 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1097 req
.ifa
.ifa_family
= peer
.family
;
1098 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &peer
.data
, peer
.bytelen
);
1099 req
.ifa
.ifa_prefixlen
= peer
.bitlen
;
1100 } else if (matches(*argv
, "broadcast") == 0 ||
1101 strcmp(*argv
, "brd") == 0) {
1105 duparg("broadcast", *argv
);
1106 if (strcmp(*argv
, "+") == 0)
1108 else if (strcmp(*argv
, "-") == 0)
1111 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1112 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1113 req
.ifa
.ifa_family
= addr
.family
;
1114 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &addr
.data
, addr
.bytelen
);
1115 brd_len
= addr
.bytelen
;
1117 } else if (strcmp(*argv
, "anycast") == 0) {
1121 duparg("anycast", *argv
);
1122 get_addr(&addr
, *argv
, req
.ifa
.ifa_family
);
1123 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1124 req
.ifa
.ifa_family
= addr
.family
;
1125 addattr_l(&req
.n
, sizeof(req
), IFA_ANYCAST
, &addr
.data
, addr
.bytelen
);
1126 any_len
= addr
.bytelen
;
1127 } else if (strcmp(*argv
, "scope") == 0) {
1130 if (rtnl_rtscope_a2n(&scope
, *argv
))
1131 invarg(*argv
, "invalid scope value.");
1132 req
.ifa
.ifa_scope
= scope
;
1134 } else if (strcmp(*argv
, "dev") == 0) {
1137 } else if (strcmp(*argv
, "label") == 0) {
1140 addattr_l(&req
.n
, sizeof(req
), IFA_LABEL
, l
, strlen(l
)+1);
1141 } else if (matches(*argv
, "valid_lft") == 0) {
1143 duparg("valid_lft", *argv
);
1146 if (set_lifetime(&valid_lft
, *argv
))
1147 invarg("valid_lft value", *argv
);
1148 } else if (matches(*argv
, "preferred_lft") == 0) {
1150 duparg("preferred_lft", *argv
);
1152 preferred_lftp
= *argv
;
1153 if (set_lifetime(&preferred_lft
, *argv
))
1154 invarg("preferred_lft value", *argv
);
1155 } else if (strcmp(*argv
, "home") == 0) {
1156 req
.ifa
.ifa_flags
|= IFA_F_HOMEADDRESS
;
1157 } else if (strcmp(*argv
, "nodad") == 0) {
1158 req
.ifa
.ifa_flags
|= IFA_F_NODAD
;
1160 if (strcmp(*argv
, "local") == 0) {
1163 if (matches(*argv
, "help") == 0)
1166 duparg2("local", *argv
);
1168 get_prefix(&lcl
, *argv
, req
.ifa
.ifa_family
);
1169 if (req
.ifa
.ifa_family
== AF_UNSPEC
)
1170 req
.ifa
.ifa_family
= lcl
.family
;
1171 addattr_l(&req
.n
, sizeof(req
), IFA_LOCAL
, &lcl
.data
, lcl
.bytelen
);
1172 local_len
= lcl
.bytelen
;
1177 fprintf(stderr
, "Not enough information: \"dev\" argument is required.\n");
1180 if (l
&& matches(d
, l
) != 0) {
1181 fprintf(stderr
, "\"dev\" (%s) must match \"label\" (%s).\n", d
, l
);
1185 if (peer_len
== 0 && local_len
) {
1186 if (cmd
== RTM_DELADDR
&& lcl
.family
== AF_INET
&& !(lcl
.flags
& PREFIXLEN_SPECIFIED
)) {
1188 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1189 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1190 " This special behaviour is likely to disappear in further releases,\n" \
1191 " fix your scripts!\n", lcl_arg
, local_len
*8);
1194 addattr_l(&req
.n
, sizeof(req
), IFA_ADDRESS
, &lcl
.data
, lcl
.bytelen
);
1197 if (req
.ifa
.ifa_prefixlen
== 0)
1198 req
.ifa
.ifa_prefixlen
= lcl
.bitlen
;
1200 if (brd_len
< 0 && cmd
!= RTM_DELADDR
) {
1203 if (req
.ifa
.ifa_family
!= AF_INET
) {
1204 fprintf(stderr
, "Broadcast can be set only for IPv4 addresses\n");
1208 if (brd
.bitlen
<= 30) {
1209 for (i
=31; i
>=brd
.bitlen
; i
--) {
1211 brd
.data
[0] |= htonl(1<<(31-i
));
1213 brd
.data
[0] &= ~htonl(1<<(31-i
));
1215 addattr_l(&req
.n
, sizeof(req
), IFA_BROADCAST
, &brd
.data
, brd
.bytelen
);
1216 brd_len
= brd
.bytelen
;
1219 if (!scoped
&& cmd
!= RTM_DELADDR
)
1220 req
.ifa
.ifa_scope
= default_scope(&lcl
);
1224 if ((req
.ifa
.ifa_index
= ll_name_to_index(d
)) == 0) {
1225 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
1229 if (valid_lftp
|| preferred_lftp
) {
1231 fprintf(stderr
, "valid_lft is zero\n");
1234 if (valid_lft
< preferred_lft
) {
1235 fprintf(stderr
, "preferred_lft is greater than valid_lft\n");
1239 memset(&cinfo
, 0, sizeof(cinfo
));
1240 cinfo
.ifa_prefered
= preferred_lft
;
1241 cinfo
.ifa_valid
= valid_lft
;
1242 addattr_l(&req
.n
, sizeof(req
), IFA_CACHEINFO
, &cinfo
,
1246 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
) < 0)
1252 int do_ipaddr(int argc
, char **argv
)
1255 return ipaddr_list_or_flush(0, NULL
, 0);
1256 if (matches(*argv
, "add") == 0)
1257 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
1258 if (matches(*argv
, "change") == 0 ||
1259 strcmp(*argv
, "chg") == 0)
1260 return ipaddr_modify(RTM_NEWADDR
, NLM_F_REPLACE
, argc
-1, argv
+1);
1261 if (matches(*argv
, "replace") == 0)
1262 return ipaddr_modify(RTM_NEWADDR
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
1263 if (matches(*argv
, "delete") == 0)
1264 return ipaddr_modify(RTM_DELADDR
, 0, argc
-1, argv
+1);
1265 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
1266 || matches(*argv
, "lst") == 0)
1267 return ipaddr_list_or_flush(argc
-1, argv
+1, 0);
1268 if (matches(*argv
, "flush") == 0)
1269 return ipaddr_list_or_flush(argc
-1, argv
+1, 1);
1270 if (matches(*argv
, "help") == 0)
1272 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv
);