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 static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
139 qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
142 int s = socket(AF_INET, SOCK_STREAM, 0);
147 memset(&ifr, 0, sizeof(ifr));
148 strcpy(ifr.ifr_name, (char *)RTA_DATA(tb[IFLA_IFNAME]));
149 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
150 fprintf(f, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno));
158 fprintf(f, "qlen %d", qlen);
161 static void print_linktype(FILE *fp, struct rtattr *tb)
163 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
164 struct link_util *lu;
167 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
169 if (!linkinfo[IFLA_INFO_KIND])
171 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
173 fprintf(fp, "%s", _SL_);
174 fprintf(fp, " %s ", kind);
176 lu = get_link_kind(kind);
177 if (!lu || !lu->print_opt)
181 struct rtattr *attr[lu->maxattr+1], **data = NULL;
183 if (linkinfo[IFLA_INFO_DATA]) {
184 parse_rtattr_nested(attr, lu->maxattr,
185 linkinfo[IFLA_INFO_DATA]);
188 lu->print_opt(lu, fp, data);
190 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
192 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
196 static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
198 struct ifla_vf_mac *vf_mac;
199 struct ifla_vf_vlan *vf_vlan;
200 struct ifla_vf_tx_rate *vf_tx_rate;
201 struct ifla_vf_spoofchk *vf_spoofchk;
202 struct rtattr *vf[IFLA_VF_MAX+1];
206 if (vfinfo->rta_type != IFLA_VF_INFO) {
207 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
211 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
213 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
214 vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
215 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
217 /* Check if the spoof checking vf info type is supported by
220 tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
221 vf[IFLA_VF_TX_RATE]->rta_len);
223 if (tmp->rta_type != IFLA_VF_SPOOFCHK)
226 vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
228 fprintf(fp, "\n vf %d MAC %s", vf_mac->vf,
229 ll_addr_n2a((unsigned char *)&vf_mac->mac,
230 ETH_ALEN, 0, b1, sizeof(b1)));
232 fprintf(fp, ", vlan %d", vf_vlan->vlan);
234 fprintf(fp, ", qos %d", vf_vlan->qos);
235 if (vf_tx_rate->rate)
236 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
237 if (vf_spoofchk && vf_spoofchk->setting != -1) {
238 if (vf_spoofchk->setting)
239 fprintf(fp, ", spoof checking on");
241 fprintf(fp, ", spoof checking off");
245 static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s) {
246 fprintf(fp, "%s", _SL_);
247 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
248 s->rx_compressed ? "compressed" : "", _SL_);
249 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
250 (uint64_t)s->rx_bytes,
251 (uint64_t)s->rx_packets,
252 (uint64_t)s->rx_errors,
253 (uint64_t)s->rx_dropped,
254 (uint64_t)s->rx_over_errors,
255 (uint64_t)s->multicast);
256 if (s->rx_compressed)
257 fprintf(fp, " %-7"PRIu64"",
258 (uint64_t)s->rx_compressed);
259 if (show_stats > 1) {
260 fprintf(fp, "%s", _SL_);
261 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
262 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
263 (uint64_t)s->rx_length_errors,
264 (uint64_t)s->rx_crc_errors,
265 (uint64_t)s->rx_frame_errors,
266 (uint64_t)s->rx_fifo_errors,
267 (uint64_t)s->rx_missed_errors);
269 fprintf(fp, "%s", _SL_);
270 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
271 (uint64_t)s->tx_compressed ? "compressed" : "", _SL_);
272 fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
273 (uint64_t)s->tx_bytes,
274 (uint64_t)s->tx_packets,
275 (uint64_t)s->tx_errors,
276 (uint64_t)s->tx_dropped,
277 (uint64_t)s->tx_carrier_errors,
278 (uint64_t)s->collisions);
279 if (s->tx_compressed)
280 fprintf(fp, " %-7"PRIu64"",
281 (uint64_t)s->tx_compressed);
282 if (show_stats > 1) {
283 fprintf(fp, "%s", _SL_);
284 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
285 fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"",
286 (uint64_t)s->tx_aborted_errors,
287 (uint64_t)s->tx_fifo_errors,
288 (uint64_t)s->tx_window_errors,
289 (uint64_t)s->tx_heartbeat_errors);
293 static void print_link_stats(FILE *fp, const struct rtnl_link_stats *s)
295 fprintf(fp, "%s", _SL_);
296 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
297 s->rx_compressed ? "compressed" : "", _SL_);
298 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
299 s->rx_bytes, s->rx_packets, s->rx_errors,
300 s->rx_dropped, s->rx_over_errors,
303 if (s->rx_compressed)
304 fprintf(fp, " %-7u", s->rx_compressed);
305 if (show_stats > 1) {
306 fprintf(fp, "%s", _SL_);
307 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
308 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
316 fprintf(fp, "%s", _SL_);
317 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
318 s->tx_compressed ? "compressed" : "", _SL_);
319 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
320 s->tx_bytes, s->tx_packets, s->tx_errors,
321 s->tx_dropped, s->tx_carrier_errors, s->collisions);
322 if (s->tx_compressed)
323 fprintf(fp, " %-7u", s->tx_compressed);
324 if (show_stats > 1) {
325 fprintf(fp, "%s", _SL_);
326 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
327 fprintf(fp, " %-7u %-7u %-7u %-7u",
328 s->tx_aborted_errors,
331 s->tx_heartbeat_errors
336 int print_linkinfo(const struct sockaddr_nl *who,
337 struct nlmsghdr *n, void *arg)
339 FILE *fp = (FILE*)arg;
340 struct ifinfomsg *ifi = NLMSG_DATA(n);
341 struct rtattr * tb[IFLA_MAX+1];
342 int len = n->nlmsg_len;
345 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
348 len -= NLMSG_LENGTH(sizeof(*ifi));
352 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
354 if (filter.up && !(ifi->ifi_flags&IFF_UP))
357 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
358 if (tb[IFLA_IFNAME] == NULL) {
359 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
362 (!filter.family || filter.family == AF_PACKET) &&
363 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
366 if (tb[IFLA_GROUP]) {
367 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
368 if (group != filter.group)
372 if (n->nlmsg_type == RTM_DELLINK)
373 fprintf(fp, "Deleted ");
375 fprintf(fp, "%d: %s", ifi->ifi_index,
376 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
380 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
382 fprintf(fp, "@NONE: ");
384 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
385 m_flag = ll_index_to_flags(iflink);
386 m_flag = !(m_flag & IFF_UP);
391 print_link_flags(fp, ifi->ifi_flags, m_flag);
394 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
396 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
397 if (tb[IFLA_MASTER]) {
399 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
401 if (tb[IFLA_OPERSTATE])
402 print_operstate(fp, *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE]));
404 if (filter.showqueue)
405 print_queuelen(fp, tb);
407 if (!filter.family || filter.family == AF_PACKET) {
409 fprintf(fp, "%s", _SL_);
410 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
412 if (tb[IFLA_ADDRESS]) {
413 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
414 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
418 if (tb[IFLA_BROADCAST]) {
419 if (ifi->ifi_flags&IFF_POINTOPOINT)
420 fprintf(fp, " peer ");
422 fprintf(fp, " brd ");
423 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
424 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
430 if (do_link && tb[IFLA_LINKINFO] && show_details)
431 print_linktype(fp, tb[IFLA_LINKINFO]);
433 if (do_link && tb[IFLA_IFALIAS])
434 fprintf(fp,"\n alias %s",
435 (const char *) RTA_DATA(tb[IFLA_IFALIAS]));
437 if (do_link && show_stats) {
438 if (tb[IFLA_STATS64])
439 print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64]));
440 else if (tb[IFLA_STATS])
441 print_link_stats(fp, RTA_DATA(tb[IFLA_STATS]));
444 if (do_link && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
445 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
446 int rem = RTA_PAYLOAD(vflist);
447 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
456 static int flush_update(void)
458 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
459 perror("Failed to send flush request");
466 static int set_lifetime(unsigned int *lifetime, char *argv)
468 if (strcmp(argv, "forever") == 0)
469 *lifetime = INFINITY_LIFE_TIME;
470 else if (get_u32(lifetime, argv, 0))
476 int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
479 FILE *fp = (FILE*)arg;
480 struct ifaddrmsg *ifa = NLMSG_DATA(n);
481 int len = n->nlmsg_len;
483 /* Use local copy of ifa_flags to not interfere with filtering code */
484 unsigned int ifa_flags;
485 struct rtattr * rta_tb[IFA_MAX+1];
489 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
491 len -= NLMSG_LENGTH(sizeof(*ifa));
493 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
497 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
500 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
502 if (!rta_tb[IFA_LOCAL])
503 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
504 if (!rta_tb[IFA_ADDRESS])
505 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
507 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
509 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
511 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
516 if (rta_tb[IFA_LABEL])
517 label = RTA_DATA(rta_tb[IFA_LABEL]);
519 label = ll_idx_n2a(ifa->ifa_index, b1);
520 if (fnmatch(filter.label, label, 0) != 0)
523 if (filter.pfx.family) {
524 if (rta_tb[IFA_LOCAL]) {
526 memset(&dst, 0, sizeof(dst));
527 dst.family = ifa->ifa_family;
528 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
529 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
534 if (filter.family && filter.family != ifa->ifa_family)
539 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
543 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
544 memcpy(fn, n, n->nlmsg_len);
545 fn->nlmsg_type = RTM_DELADDR;
546 fn->nlmsg_flags = NLM_F_REQUEST;
547 fn->nlmsg_seq = ++rth.seq;
548 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
554 if (n->nlmsg_type == RTM_DELADDR)
555 fprintf(fp, "Deleted ");
557 if (filter.oneline || filter.flushb)
558 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
559 if (ifa->ifa_family == AF_INET)
560 fprintf(fp, " inet ");
561 else if (ifa->ifa_family == AF_INET6)
562 fprintf(fp, " inet6 ");
563 else if (ifa->ifa_family == AF_DECnet)
564 fprintf(fp, " dnet ");
565 else if (ifa->ifa_family == AF_IPX)
566 fprintf(fp, " ipx ");
568 fprintf(fp, " family %d ", ifa->ifa_family);
570 if (rta_tb[IFA_LOCAL]) {
571 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
572 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
573 RTA_DATA(rta_tb[IFA_LOCAL]),
574 abuf, sizeof(abuf)));
576 if (rta_tb[IFA_ADDRESS] == NULL ||
577 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
578 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
580 fprintf(fp, " peer %s/%d ",
581 rt_addr_n2a(ifa->ifa_family,
582 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
583 RTA_DATA(rta_tb[IFA_ADDRESS]),
589 if (rta_tb[IFA_BROADCAST]) {
590 fprintf(fp, "brd %s ",
591 rt_addr_n2a(ifa->ifa_family,
592 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
593 RTA_DATA(rta_tb[IFA_BROADCAST]),
594 abuf, sizeof(abuf)));
596 if (rta_tb[IFA_ANYCAST]) {
597 fprintf(fp, "any %s ",
598 rt_addr_n2a(ifa->ifa_family,
599 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
600 RTA_DATA(rta_tb[IFA_ANYCAST]),
601 abuf, sizeof(abuf)));
603 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
604 ifa_flags = ifa->ifa_flags;
605 if (ifa->ifa_flags&IFA_F_SECONDARY) {
606 ifa_flags &= ~IFA_F_SECONDARY;
607 if (ifa->ifa_family == AF_INET6)
608 fprintf(fp, "temporary ");
610 fprintf(fp, "secondary ");
612 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
613 ifa_flags &= ~IFA_F_TENTATIVE;
614 fprintf(fp, "tentative ");
616 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
617 ifa_flags &= ~IFA_F_DEPRECATED;
619 fprintf(fp, "deprecated ");
621 if (ifa->ifa_flags&IFA_F_HOMEADDRESS) {
622 ifa_flags &= ~IFA_F_HOMEADDRESS;
623 fprintf(fp, "home ");
625 if (ifa->ifa_flags&IFA_F_NODAD) {
626 ifa_flags &= ~IFA_F_NODAD;
627 fprintf(fp, "nodad ");
629 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
630 fprintf(fp, "dynamic ");
632 ifa_flags &= ~IFA_F_PERMANENT;
633 if (ifa->ifa_flags&IFA_F_DADFAILED) {
634 ifa_flags &= ~IFA_F_DADFAILED;
635 fprintf(fp, "dadfailed ");
638 fprintf(fp, "flags %02x ", ifa_flags);
639 if (rta_tb[IFA_LABEL])
640 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
641 if (rta_tb[IFA_CACHEINFO]) {
642 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
643 fprintf(fp, "%s", _SL_);
644 fprintf(fp, " valid_lft ");
645 if (ci->ifa_valid == INFINITY_LIFE_TIME)
646 fprintf(fp, "forever");
648 fprintf(fp, "%usec", ci->ifa_valid);
649 fprintf(fp, " preferred_lft ");
650 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
651 fprintf(fp, "forever");
654 fprintf(fp, "%dsec", ci->ifa_prefered);
656 fprintf(fp, "%usec", ci->ifa_prefered);
664 int print_addrinfo_primary(const struct sockaddr_nl *who, struct nlmsghdr *n,
667 struct ifaddrmsg *ifa = NLMSG_DATA(n);
669 if (ifa->ifa_flags & IFA_F_SECONDARY)
672 return print_addrinfo(who, n, arg);
675 int print_addrinfo_secondary(const struct sockaddr_nl *who, struct nlmsghdr *n,
678 struct ifaddrmsg *ifa = NLMSG_DATA(n);
680 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
683 return print_addrinfo(who, n, arg);
688 struct nlmsg_list *next;
692 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
694 for ( ;ainfo ; ainfo = ainfo->next) {
695 struct nlmsghdr *n = &ainfo->h;
696 struct ifaddrmsg *ifa = NLMSG_DATA(n);
698 if (n->nlmsg_type != RTM_NEWADDR)
701 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
704 if (ifa->ifa_index != ifindex ||
705 (filter.family && filter.family != ifa->ifa_family))
708 print_addrinfo(NULL, n, fp);
714 static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
717 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
718 struct nlmsg_list *h;
719 struct nlmsg_list **lp;
721 h = malloc(n->nlmsg_len+sizeof(void*));
725 memcpy(&h->h, n, n->nlmsg_len);
728 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
731 ll_remember_index(who, n, NULL);
735 static int ipaddr_list_or_flush(int argc, char **argv, int flush)
737 struct nlmsg_list *linfo = NULL;
738 struct nlmsg_list *ainfo = NULL;
739 struct nlmsg_list *l, *n;
740 char *filter_dev = NULL;
743 ipaddr_reset_filter(oneline);
744 filter.showqueue = 1;
746 if (filter.family == AF_UNSPEC)
747 filter.family = preferred_family;
749 filter.group = INIT_NETDEV_GROUP;
753 fprintf(stderr, "Flush requires arguments.\n");
757 if (filter.family == AF_PACKET) {
758 fprintf(stderr, "Cannot flush link addresses.\n");
764 if (strcmp(*argv, "to") == 0) {
766 get_prefix(&filter.pfx, *argv, filter.family);
767 if (filter.family == AF_UNSPEC)
768 filter.family = filter.pfx.family;
769 } else if (strcmp(*argv, "scope") == 0) {
772 filter.scopemask = -1;
773 if (rtnl_rtscope_a2n(&scope, *argv)) {
774 if (strcmp(*argv, "all") != 0)
775 invarg("invalid \"scope\"\n", *argv);
776 scope = RT_SCOPE_NOWHERE;
777 filter.scopemask = 0;
779 filter.scope = scope;
780 } else if (strcmp(*argv, "up") == 0) {
782 } else if (strcmp(*argv, "dynamic") == 0) {
783 filter.flags &= ~IFA_F_PERMANENT;
784 filter.flagmask |= IFA_F_PERMANENT;
785 } else if (strcmp(*argv, "permanent") == 0) {
786 filter.flags |= IFA_F_PERMANENT;
787 filter.flagmask |= IFA_F_PERMANENT;
788 } else if (strcmp(*argv, "secondary") == 0 ||
789 strcmp(*argv, "temporary") == 0) {
790 filter.flags |= IFA_F_SECONDARY;
791 filter.flagmask |= IFA_F_SECONDARY;
792 } else if (strcmp(*argv, "primary") == 0) {
793 filter.flags &= ~IFA_F_SECONDARY;
794 filter.flagmask |= IFA_F_SECONDARY;
795 } else if (strcmp(*argv, "tentative") == 0) {
796 filter.flags |= IFA_F_TENTATIVE;
797 filter.flagmask |= IFA_F_TENTATIVE;
798 } else if (strcmp(*argv, "deprecated") == 0) {
799 filter.flags |= IFA_F_DEPRECATED;
800 filter.flagmask |= IFA_F_DEPRECATED;
801 } else if (strcmp(*argv, "home") == 0) {
802 filter.flags |= IFA_F_HOMEADDRESS;
803 filter.flagmask |= IFA_F_HOMEADDRESS;
804 } else if (strcmp(*argv, "nodad") == 0) {
805 filter.flags |= IFA_F_NODAD;
806 filter.flagmask |= IFA_F_NODAD;
807 } else if (strcmp(*argv, "dadfailed") == 0) {
808 filter.flags |= IFA_F_DADFAILED;
809 filter.flagmask |= IFA_F_DADFAILED;
810 } else if (strcmp(*argv, "label") == 0) {
812 filter.label = *argv;
813 } else if (strcmp(*argv, "group") == 0) {
815 if (rtnl_group_a2n(&filter.group, *argv))
816 invarg("Invalid \"group\" value\n", *argv);
818 if (strcmp(*argv, "dev") == 0) {
821 if (matches(*argv, "help") == 0)
824 duparg2("dev", *argv);
830 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
831 perror("Cannot send dump request");
835 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
836 fprintf(stderr, "Dump terminated\n");
841 filter.ifindex = ll_name_to_index(filter_dev);
842 if (filter.ifindex <= 0) {
843 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
850 char flushb[4096-512];
852 filter.flushb = flushb;
854 filter.flushe = sizeof(flushb);
856 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
857 const struct rtnl_dump_filter_arg a[3] = {
859 .filter = print_addrinfo_secondary,
863 .filter = print_addrinfo_primary,
871 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
872 perror("Cannot send dump request");
876 if (rtnl_dump_filter_l(&rth, a) < 0) {
877 fprintf(stderr, "Flush terminated\n");
880 if (filter.flushed == 0) {
884 printf("Nothing to flush.\n");
886 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
892 if (flush_update() < 0)
896 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
900 /* If we are flushing, and specifying primary, then we
901 * want to flush only a single round. Otherwise, we'll
902 * start flushing secondaries that were promoted to
905 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
908 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
913 if (filter.family != AF_PACKET) {
914 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
915 perror("Cannot send dump request");
919 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
920 fprintf(stderr, "Dump terminated\n");
926 if (filter.family && filter.family != AF_PACKET) {
927 struct nlmsg_list **lp;
933 while ((l=*lp)!=NULL) {
935 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
936 struct nlmsg_list *a;
938 for (a=ainfo; a; a=a->next) {
939 struct nlmsghdr *n = &a->h;
940 struct ifaddrmsg *ifa = NLMSG_DATA(n);
942 if (ifa->ifa_index != ifi->ifi_index ||
943 (filter.family && filter.family != ifa->ifa_family))
945 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
947 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
949 if (filter.pfx.family || filter.label) {
950 struct rtattr *tb[IFA_MAX+1];
951 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
953 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
955 if (filter.pfx.family && tb[IFA_LOCAL]) {
957 memset(&dst, 0, sizeof(dst));
958 dst.family = ifa->ifa_family;
959 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
960 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
967 label = RTA_DATA(tb[IFA_LABEL]);
969 label = ll_idx_n2a(ifa->ifa_index, b1);
970 if (fnmatch(filter.label, label, 0) != 0)
985 for (l=linfo; l; l = n) {
987 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
988 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
989 if (filter.family != AF_PACKET)
990 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
999 int ipaddr_list_link(int argc, char **argv)
1001 preferred_family = AF_PACKET;
1003 return ipaddr_list_or_flush(argc, argv, 0);
1006 void ipaddr_reset_filter(int oneline)
1008 memset(&filter, 0, sizeof(filter));
1009 filter.oneline = oneline;
1012 static int default_scope(inet_prefix *lcl)
1014 if (lcl->family == AF_INET) {
1015 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
1016 return RT_SCOPE_HOST;
1021 static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
1025 struct ifaddrmsg ifa;
1030 char *lcl_arg = NULL;
1031 char *valid_lftp = NULL;
1032 char *preferred_lftp = NULL;
1040 __u32 preferred_lft = INFINITY_LIFE_TIME;
1041 __u32 valid_lft = INFINITY_LIFE_TIME;
1042 struct ifa_cacheinfo cinfo;
1044 memset(&req, 0, sizeof(req));
1046 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1047 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
1048 req.n.nlmsg_type = cmd;
1049 req.ifa.ifa_family = preferred_family;
1052 if (strcmp(*argv, "peer") == 0 ||
1053 strcmp(*argv, "remote") == 0) {
1057 duparg("peer", *argv);
1058 get_prefix(&peer, *argv, req.ifa.ifa_family);
1059 peer_len = peer.bytelen;
1060 if (req.ifa.ifa_family == AF_UNSPEC)
1061 req.ifa.ifa_family = peer.family;
1062 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1063 req.ifa.ifa_prefixlen = peer.bitlen;
1064 } else if (matches(*argv, "broadcast") == 0 ||
1065 strcmp(*argv, "brd") == 0) {
1069 duparg("broadcast", *argv);
1070 if (strcmp(*argv, "+") == 0)
1072 else if (strcmp(*argv, "-") == 0)
1075 get_addr(&addr, *argv, req.ifa.ifa_family);
1076 if (req.ifa.ifa_family == AF_UNSPEC)
1077 req.ifa.ifa_family = addr.family;
1078 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1079 brd_len = addr.bytelen;
1081 } else if (strcmp(*argv, "anycast") == 0) {
1085 duparg("anycast", *argv);
1086 get_addr(&addr, *argv, req.ifa.ifa_family);
1087 if (req.ifa.ifa_family == AF_UNSPEC)
1088 req.ifa.ifa_family = addr.family;
1089 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1090 any_len = addr.bytelen;
1091 } else if (strcmp(*argv, "scope") == 0) {
1094 if (rtnl_rtscope_a2n(&scope, *argv))
1095 invarg(*argv, "invalid scope value.");
1096 req.ifa.ifa_scope = scope;
1098 } else if (strcmp(*argv, "dev") == 0) {
1101 } else if (strcmp(*argv, "label") == 0) {
1104 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
1105 } else if (matches(*argv, "valid_lft") == 0) {
1107 duparg("valid_lft", *argv);
1110 if (set_lifetime(&valid_lft, *argv))
1111 invarg("valid_lft value", *argv);
1112 } else if (matches(*argv, "preferred_lft") == 0) {
1114 duparg("preferred_lft", *argv);
1116 preferred_lftp = *argv;
1117 if (set_lifetime(&preferred_lft, *argv))
1118 invarg("preferred_lft value", *argv);
1119 } else if (strcmp(*argv, "home") == 0) {
1120 req.ifa.ifa_flags |= IFA_F_HOMEADDRESS;
1121 } else if (strcmp(*argv, "nodad") == 0) {
1122 req.ifa.ifa_flags |= IFA_F_NODAD;
1124 if (strcmp(*argv, "local") == 0) {
1127 if (matches(*argv, "help") == 0)
1130 duparg2("local", *argv);
1132 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1133 if (req.ifa.ifa_family == AF_UNSPEC)
1134 req.ifa.ifa_family = lcl.family;
1135 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1136 local_len = lcl.bytelen;
1141 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1144 if (l && matches(d, l) != 0) {
1145 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
1149 if (peer_len == 0 && local_len) {
1150 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1152 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1153 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1154 " This special behaviour is likely to disappear in further releases,\n" \
1155 " fix your scripts!\n", lcl_arg, local_len*8);
1158 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1161 if (req.ifa.ifa_prefixlen == 0)
1162 req.ifa.ifa_prefixlen = lcl.bitlen;
1164 if (brd_len < 0 && cmd != RTM_DELADDR) {
1167 if (req.ifa.ifa_family != AF_INET) {
1168 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1172 if (brd.bitlen <= 30) {
1173 for (i=31; i>=brd.bitlen; i--) {
1175 brd.data[0] |= htonl(1<<(31-i));
1177 brd.data[0] &= ~htonl(1<<(31-i));
1179 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1180 brd_len = brd.bytelen;
1183 if (!scoped && cmd != RTM_DELADDR)
1184 req.ifa.ifa_scope = default_scope(&lcl);
1188 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1189 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1193 if (valid_lftp || preferred_lftp) {
1195 fprintf(stderr, "valid_lft is zero\n");
1198 if (valid_lft < preferred_lft) {
1199 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1203 memset(&cinfo, 0, sizeof(cinfo));
1204 cinfo.ifa_prefered = preferred_lft;
1205 cinfo.ifa_valid = valid_lft;
1206 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1210 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1216 int do_ipaddr(int argc, char **argv)
1219 return ipaddr_list_or_flush(0, NULL, 0);
1220 if (matches(*argv, "add") == 0)
1221 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1222 if (matches(*argv, "change") == 0 ||
1223 strcmp(*argv, "chg") == 0)
1224 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1225 if (matches(*argv, "replace") == 0)
1226 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
1227 if (matches(*argv, "delete") == 0)
1228 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
1229 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1230 || matches(*argv, "lst") == 0)
1231 return ipaddr_list_or_flush(argc-1, argv+1, 0);
1232 if (matches(*argv, "flush") == 0)
1233 return ipaddr_list_or_flush(argc-1, argv+1, 1);
1234 if (matches(*argv, "help") == 0)
1236 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);