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>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <sys/ioctl.h>
21 #include <sys/errno.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/sockios.h>
34 #include "ip_common.h"
57 static void usage(void) __attribute__((noreturn));
59 static void usage(void)
64 fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
65 fprintf(stderr, " [ CONFFLAG-LIST ]\n");
66 fprintf(stderr, " ip addr del IFADDR dev STRING\n");
67 fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
68 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n");
69 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
70 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
71 fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n");
72 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
73 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
74 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
75 fprintf(stderr, " tentative | deprecated | dadfailed | temporary |\n");
76 fprintf(stderr, " CONFFLAG-LIST ]\n");
77 fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
78 fprintf(stderr, "CONFFLAG := [ home | nodad ]\n");
79 fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
80 fprintf(stderr, "LFT := forever | SECONDS\n");
85 void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
88 if (flags & IFF_UP && !(flags & IFF_RUNNING))
89 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
90 flags &= ~IFF_RUNNING;
91 #define _PF(f) if (flags&IFF_##f) { \
93 fprintf(fp, #f "%s", flags ? "," : ""); }
114 fprintf(fp, "%x", flags);
116 fprintf(fp, ",M-DOWN");
120 static const char *oper_states[] = {
121 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
122 "TESTING", "DORMANT", "UP"
125 static void print_operstate(FILE *f, __u8 state)
127 if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
128 fprintf(f, "state %#x ", state);
130 fprintf(f, "state %s ", oper_states[state]);
133 static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
138 qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
141 int s = socket(AF_INET, SOCK_STREAM, 0);
146 memset(&ifr, 0, sizeof(ifr));
147 strcpy(ifr.ifr_name, (char *)RTA_DATA(tb[IFLA_IFNAME]));
148 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
149 fprintf(f, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno));
157 fprintf(f, "qlen %d", qlen);
160 static void print_linktype(FILE *fp, struct rtattr *tb)
162 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
163 struct link_util *lu;
166 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
168 if (!linkinfo[IFLA_INFO_KIND])
170 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
172 fprintf(fp, "%s", _SL_);
173 fprintf(fp, " %s ", kind);
175 lu = get_link_kind(kind);
176 if (!lu || !lu->print_opt)
180 struct rtattr *attr[lu->maxattr+1], **data = NULL;
182 if (linkinfo[IFLA_INFO_DATA]) {
183 parse_rtattr_nested(attr, lu->maxattr,
184 linkinfo[IFLA_INFO_DATA]);
187 lu->print_opt(lu, fp, data);
189 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
191 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
195 static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
197 struct ifla_vf_mac *vf_mac;
198 struct ifla_vf_vlan *vf_vlan;
199 struct ifla_vf_tx_rate *vf_tx_rate;
200 struct ifla_vf_spoofchk *vf_spoofchk;
201 struct rtattr *vf[IFLA_VF_MAX+1];
205 if (vfinfo->rta_type != IFLA_VF_INFO) {
206 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
210 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
212 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
213 vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
214 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
216 /* Check if the spoof checking vf info type is supported by
219 tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
220 vf[IFLA_VF_TX_RATE]->rta_len);
222 if (tmp->rta_type != IFLA_VF_SPOOFCHK)
225 vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
227 fprintf(fp, "\n vf %d MAC %s", vf_mac->vf,
228 ll_addr_n2a((unsigned char *)&vf_mac->mac,
229 ETH_ALEN, 0, b1, sizeof(b1)));
231 fprintf(fp, ", vlan %d", vf_vlan->vlan);
233 fprintf(fp, ", qos %d", vf_vlan->qos);
234 if (vf_tx_rate->rate)
235 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
236 if (vf_spoofchk && vf_spoofchk->setting != -1) {
237 if (vf_spoofchk->setting)
238 fprintf(fp, ", spoof checking on");
240 fprintf(fp, ", spoof checking off");
244 int print_linkinfo(const struct sockaddr_nl *who,
245 struct nlmsghdr *n, void *arg)
247 FILE *fp = (FILE*)arg;
248 struct ifinfomsg *ifi = NLMSG_DATA(n);
249 struct rtattr * tb[IFLA_MAX+1];
250 int len = n->nlmsg_len;
253 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
256 len -= NLMSG_LENGTH(sizeof(*ifi));
260 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
262 if (filter.up && !(ifi->ifi_flags&IFF_UP))
265 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
266 if (tb[IFLA_IFNAME] == NULL) {
267 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
270 (!filter.family || filter.family == AF_PACKET) &&
271 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
274 if (tb[IFLA_GROUP]) {
275 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
276 if (group != filter.group)
280 if (n->nlmsg_type == RTM_DELLINK)
281 fprintf(fp, "Deleted ");
283 fprintf(fp, "%d: %s", ifi->ifi_index,
284 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
288 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
290 fprintf(fp, "@NONE: ");
292 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
293 m_flag = ll_index_to_flags(iflink);
294 m_flag = !(m_flag & IFF_UP);
299 print_link_flags(fp, ifi->ifi_flags, m_flag);
302 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
304 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
305 if (tb[IFLA_MASTER]) {
307 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
309 if (tb[IFLA_OPERSTATE])
310 print_operstate(fp, *(__u8 *)RTA_DATA(tb[IFLA_OPERSTATE]));
312 if (filter.showqueue)
313 print_queuelen(fp, tb);
315 if (!filter.family || filter.family == AF_PACKET) {
317 fprintf(fp, "%s", _SL_);
318 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
320 if (tb[IFLA_ADDRESS]) {
321 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
322 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
326 if (tb[IFLA_BROADCAST]) {
327 if (ifi->ifi_flags&IFF_POINTOPOINT)
328 fprintf(fp, " peer ");
330 fprintf(fp, " brd ");
331 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
332 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
338 if (do_link && tb[IFLA_LINKINFO] && show_details)
339 print_linktype(fp, tb[IFLA_LINKINFO]);
341 if (do_link && tb[IFLA_IFALIAS])
342 fprintf(fp,"\n alias %s",
343 (const char *) RTA_DATA(tb[IFLA_IFALIAS]));
345 if (do_link && tb[IFLA_STATS64] && show_stats) {
346 struct rtnl_link_stats64 slocal;
347 struct rtnl_link_stats64 *s = RTA_DATA(tb[IFLA_STATS64]);
348 if (((unsigned long)s) & (sizeof(unsigned long)-1)) {
349 memcpy(&slocal, s, sizeof(slocal));
352 fprintf(fp, "%s", _SL_);
353 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
354 s->rx_compressed ? "compressed" : "", _SL_);
355 fprintf(fp, " %-10llu %-8llu %-7llu %-7llu %-7llu %-7llu",
356 (unsigned long long)s->rx_bytes,
357 (unsigned long long)s->rx_packets,
358 (unsigned long long)s->rx_errors,
359 (unsigned long long)s->rx_dropped,
360 (unsigned long long)s->rx_over_errors,
361 (unsigned long long)s->multicast);
362 if (s->rx_compressed)
363 fprintf(fp, " %-7llu",
364 (unsigned long long)s->rx_compressed);
365 if (show_stats > 1) {
366 fprintf(fp, "%s", _SL_);
367 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
368 fprintf(fp, " %-7llu %-7llu %-7llu %-7llu %-7llu",
369 (unsigned long long)s->rx_length_errors,
370 (unsigned long long)s->rx_crc_errors,
371 (unsigned long long)s->rx_frame_errors,
372 (unsigned long long)s->rx_fifo_errors,
373 (unsigned long long)s->rx_missed_errors);
375 fprintf(fp, "%s", _SL_);
376 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
377 s->tx_compressed ? "compressed" : "", _SL_);
378 fprintf(fp, " %-10llu %-8llu %-7llu %-7llu %-7llu %-7llu",
379 (unsigned long long)s->tx_bytes,
380 (unsigned long long)s->tx_packets,
381 (unsigned long long)s->tx_errors,
382 (unsigned long long)s->tx_dropped,
383 (unsigned long long)s->tx_carrier_errors,
384 (unsigned long long)s->collisions);
385 if (s->tx_compressed)
386 fprintf(fp, " %-7llu",
387 (unsigned long long)s->tx_compressed);
388 if (show_stats > 1) {
389 fprintf(fp, "%s", _SL_);
390 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
391 fprintf(fp, " %-7llu %-7llu %-7llu %-7llu",
392 (unsigned long long)s->tx_aborted_errors,
393 (unsigned long long)s->tx_fifo_errors,
394 (unsigned long long)s->tx_window_errors,
395 (unsigned long long)s->tx_heartbeat_errors);
398 if (do_link && !tb[IFLA_STATS64] && tb[IFLA_STATS] && show_stats) {
399 struct rtnl_link_stats slocal;
400 struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]);
401 if (((unsigned long)s) & (sizeof(unsigned long)-1)) {
402 memcpy(&slocal, s, sizeof(slocal));
405 fprintf(fp, "%s", _SL_);
406 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
407 s->rx_compressed ? "compressed" : "", _SL_);
408 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
409 s->rx_bytes, s->rx_packets, s->rx_errors,
410 s->rx_dropped, s->rx_over_errors,
413 if (s->rx_compressed)
414 fprintf(fp, " %-7u", s->rx_compressed);
415 if (show_stats > 1) {
416 fprintf(fp, "%s", _SL_);
417 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
418 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
426 fprintf(fp, "%s", _SL_);
427 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
428 s->tx_compressed ? "compressed" : "", _SL_);
429 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
430 s->tx_bytes, s->tx_packets, s->tx_errors,
431 s->tx_dropped, s->tx_carrier_errors, s->collisions);
432 if (s->tx_compressed)
433 fprintf(fp, " %-7u", s->tx_compressed);
434 if (show_stats > 1) {
435 fprintf(fp, "%s", _SL_);
436 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
437 fprintf(fp, " %-7u %-7u %-7u %-7u",
438 s->tx_aborted_errors,
441 s->tx_heartbeat_errors
445 if (do_link && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
446 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
447 int rem = RTA_PAYLOAD(vflist);
448 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
457 static int flush_update(void)
459 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
460 perror("Failed to send flush request");
467 static int set_lifetime(unsigned int *lifetime, char *argv)
469 if (strcmp(argv, "forever") == 0)
470 *lifetime = INFINITY_LIFE_TIME;
471 else if (get_u32(lifetime, argv, 0))
477 int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
480 FILE *fp = (FILE*)arg;
481 struct ifaddrmsg *ifa = NLMSG_DATA(n);
482 int len = n->nlmsg_len;
484 /* Use local copy of ifa_flags to not interfere with filtering code */
485 unsigned int ifa_flags;
486 struct rtattr * rta_tb[IFA_MAX+1];
490 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
492 len -= NLMSG_LENGTH(sizeof(*ifa));
494 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
498 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
501 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
503 if (!rta_tb[IFA_LOCAL])
504 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
505 if (!rta_tb[IFA_ADDRESS])
506 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
508 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
510 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
512 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
517 if (rta_tb[IFA_LABEL])
518 label = RTA_DATA(rta_tb[IFA_LABEL]);
520 label = ll_idx_n2a(ifa->ifa_index, b1);
521 if (fnmatch(filter.label, label, 0) != 0)
524 if (filter.pfx.family) {
525 if (rta_tb[IFA_LOCAL]) {
527 memset(&dst, 0, sizeof(dst));
528 dst.family = ifa->ifa_family;
529 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
530 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
535 if (filter.family && filter.family != ifa->ifa_family)
540 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
544 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
545 memcpy(fn, n, n->nlmsg_len);
546 fn->nlmsg_type = RTM_DELADDR;
547 fn->nlmsg_flags = NLM_F_REQUEST;
548 fn->nlmsg_seq = ++rth.seq;
549 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
555 if (n->nlmsg_type == RTM_DELADDR)
556 fprintf(fp, "Deleted ");
558 if (filter.oneline || filter.flushb)
559 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
560 if (ifa->ifa_family == AF_INET)
561 fprintf(fp, " inet ");
562 else if (ifa->ifa_family == AF_INET6)
563 fprintf(fp, " inet6 ");
564 else if (ifa->ifa_family == AF_DECnet)
565 fprintf(fp, " dnet ");
566 else if (ifa->ifa_family == AF_IPX)
567 fprintf(fp, " ipx ");
569 fprintf(fp, " family %d ", ifa->ifa_family);
571 if (rta_tb[IFA_LOCAL]) {
572 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
573 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
574 RTA_DATA(rta_tb[IFA_LOCAL]),
575 abuf, sizeof(abuf)));
577 if (rta_tb[IFA_ADDRESS] == NULL ||
578 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
579 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
581 fprintf(fp, " peer %s/%d ",
582 rt_addr_n2a(ifa->ifa_family,
583 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
584 RTA_DATA(rta_tb[IFA_ADDRESS]),
590 if (rta_tb[IFA_BROADCAST]) {
591 fprintf(fp, "brd %s ",
592 rt_addr_n2a(ifa->ifa_family,
593 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
594 RTA_DATA(rta_tb[IFA_BROADCAST]),
595 abuf, sizeof(abuf)));
597 if (rta_tb[IFA_ANYCAST]) {
598 fprintf(fp, "any %s ",
599 rt_addr_n2a(ifa->ifa_family,
600 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
601 RTA_DATA(rta_tb[IFA_ANYCAST]),
602 abuf, sizeof(abuf)));
604 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
605 ifa_flags = ifa->ifa_flags;
606 if (ifa->ifa_flags&IFA_F_SECONDARY) {
607 ifa_flags &= ~IFA_F_SECONDARY;
608 if (ifa->ifa_family == AF_INET6)
609 fprintf(fp, "temporary ");
611 fprintf(fp, "secondary ");
613 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
614 ifa_flags &= ~IFA_F_TENTATIVE;
615 fprintf(fp, "tentative ");
617 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
618 ifa_flags &= ~IFA_F_DEPRECATED;
620 fprintf(fp, "deprecated ");
622 if (ifa->ifa_flags&IFA_F_HOMEADDRESS) {
623 ifa_flags &= ~IFA_F_HOMEADDRESS;
624 fprintf(fp, "home ");
626 if (ifa->ifa_flags&IFA_F_NODAD) {
627 ifa_flags &= ~IFA_F_NODAD;
628 fprintf(fp, "nodad ");
630 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
631 fprintf(fp, "dynamic ");
633 ifa_flags &= ~IFA_F_PERMANENT;
634 if (ifa->ifa_flags&IFA_F_DADFAILED) {
635 ifa_flags &= ~IFA_F_DADFAILED;
636 fprintf(fp, "dadfailed ");
639 fprintf(fp, "flags %02x ", ifa_flags);
640 if (rta_tb[IFA_LABEL])
641 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
642 if (rta_tb[IFA_CACHEINFO]) {
643 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
644 fprintf(fp, "%s", _SL_);
645 fprintf(fp, " valid_lft ");
646 if (ci->ifa_valid == INFINITY_LIFE_TIME)
647 fprintf(fp, "forever");
649 fprintf(fp, "%usec", ci->ifa_valid);
650 fprintf(fp, " preferred_lft ");
651 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
652 fprintf(fp, "forever");
655 fprintf(fp, "%dsec", ci->ifa_prefered);
657 fprintf(fp, "%usec", ci->ifa_prefered);
665 int print_addrinfo_primary(const struct sockaddr_nl *who, struct nlmsghdr *n,
668 struct ifaddrmsg *ifa = NLMSG_DATA(n);
670 if (ifa->ifa_flags & IFA_F_SECONDARY)
673 return print_addrinfo(who, n, arg);
676 int print_addrinfo_secondary(const struct sockaddr_nl *who, struct nlmsghdr *n,
679 struct ifaddrmsg *ifa = NLMSG_DATA(n);
681 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
684 return print_addrinfo(who, n, arg);
689 struct nlmsg_list *next;
693 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
695 for ( ;ainfo ; ainfo = ainfo->next) {
696 struct nlmsghdr *n = &ainfo->h;
697 struct ifaddrmsg *ifa = NLMSG_DATA(n);
699 if (n->nlmsg_type != RTM_NEWADDR)
702 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
705 if (ifa->ifa_index != ifindex ||
706 (filter.family && filter.family != ifa->ifa_family))
709 print_addrinfo(NULL, n, fp);
715 static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
718 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
719 struct nlmsg_list *h;
720 struct nlmsg_list **lp;
722 h = malloc(n->nlmsg_len+sizeof(void*));
726 memcpy(&h->h, n, n->nlmsg_len);
729 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
732 ll_remember_index(who, n, NULL);
736 static int ipaddr_list_or_flush(int argc, char **argv, int flush)
738 struct nlmsg_list *linfo = NULL;
739 struct nlmsg_list *ainfo = NULL;
740 struct nlmsg_list *l, *n;
741 char *filter_dev = NULL;
744 ipaddr_reset_filter(oneline);
745 filter.showqueue = 1;
747 if (filter.family == AF_UNSPEC)
748 filter.family = preferred_family;
750 filter.group = INIT_NETDEV_GROUP;
754 fprintf(stderr, "Flush requires arguments.\n");
758 if (filter.family == AF_PACKET) {
759 fprintf(stderr, "Cannot flush link addresses.\n");
765 if (strcmp(*argv, "to") == 0) {
767 get_prefix(&filter.pfx, *argv, filter.family);
768 if (filter.family == AF_UNSPEC)
769 filter.family = filter.pfx.family;
770 } else if (strcmp(*argv, "scope") == 0) {
773 filter.scopemask = -1;
774 if (rtnl_rtscope_a2n(&scope, *argv)) {
775 if (strcmp(*argv, "all") != 0)
776 invarg("invalid \"scope\"\n", *argv);
777 scope = RT_SCOPE_NOWHERE;
778 filter.scopemask = 0;
780 filter.scope = scope;
781 } else if (strcmp(*argv, "up") == 0) {
783 } else if (strcmp(*argv, "dynamic") == 0) {
784 filter.flags &= ~IFA_F_PERMANENT;
785 filter.flagmask |= IFA_F_PERMANENT;
786 } else if (strcmp(*argv, "permanent") == 0) {
787 filter.flags |= IFA_F_PERMANENT;
788 filter.flagmask |= IFA_F_PERMANENT;
789 } else if (strcmp(*argv, "secondary") == 0 ||
790 strcmp(*argv, "temporary") == 0) {
791 filter.flags |= IFA_F_SECONDARY;
792 filter.flagmask |= IFA_F_SECONDARY;
793 } else if (strcmp(*argv, "primary") == 0) {
794 filter.flags &= ~IFA_F_SECONDARY;
795 filter.flagmask |= IFA_F_SECONDARY;
796 } else if (strcmp(*argv, "tentative") == 0) {
797 filter.flags |= IFA_F_TENTATIVE;
798 filter.flagmask |= IFA_F_TENTATIVE;
799 } else if (strcmp(*argv, "deprecated") == 0) {
800 filter.flags |= IFA_F_DEPRECATED;
801 filter.flagmask |= IFA_F_DEPRECATED;
802 } else if (strcmp(*argv, "home") == 0) {
803 filter.flags |= IFA_F_HOMEADDRESS;
804 filter.flagmask |= IFA_F_HOMEADDRESS;
805 } else if (strcmp(*argv, "nodad") == 0) {
806 filter.flags |= IFA_F_NODAD;
807 filter.flagmask |= IFA_F_NODAD;
808 } else if (strcmp(*argv, "dadfailed") == 0) {
809 filter.flags |= IFA_F_DADFAILED;
810 filter.flagmask |= IFA_F_DADFAILED;
811 } else if (strcmp(*argv, "label") == 0) {
813 filter.label = *argv;
814 } else if (strcmp(*argv, "group") == 0) {
816 if (rtnl_group_a2n(&filter.group, *argv))
817 invarg("Invalid \"group\" value\n", *argv);
819 if (strcmp(*argv, "dev") == 0) {
822 if (matches(*argv, "help") == 0)
825 duparg2("dev", *argv);
831 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
832 perror("Cannot send dump request");
836 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
837 fprintf(stderr, "Dump terminated\n");
842 filter.ifindex = ll_name_to_index(filter_dev);
843 if (filter.ifindex <= 0) {
844 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
851 char flushb[4096-512];
853 filter.flushb = flushb;
855 filter.flushe = sizeof(flushb);
857 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
858 const struct rtnl_dump_filter_arg a[3] = {
860 .filter = print_addrinfo_secondary,
864 .filter = print_addrinfo_primary,
872 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
873 perror("Cannot send dump request");
877 if (rtnl_dump_filter_l(&rth, a) < 0) {
878 fprintf(stderr, "Flush terminated\n");
881 if (filter.flushed == 0) {
885 printf("Nothing to flush.\n");
887 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
893 if (flush_update() < 0)
897 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
901 /* If we are flushing, and specifying primary, then we
902 * want to flush only a single round. Otherwise, we'll
903 * start flushing secondaries that were promoted to
906 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
909 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
914 if (filter.family != AF_PACKET) {
915 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
916 perror("Cannot send dump request");
920 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
921 fprintf(stderr, "Dump terminated\n");
927 if (filter.family && filter.family != AF_PACKET) {
928 struct nlmsg_list **lp;
934 while ((l=*lp)!=NULL) {
936 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
937 struct nlmsg_list *a;
939 for (a=ainfo; a; a=a->next) {
940 struct nlmsghdr *n = &a->h;
941 struct ifaddrmsg *ifa = NLMSG_DATA(n);
943 if (ifa->ifa_index != ifi->ifi_index ||
944 (filter.family && filter.family != ifa->ifa_family))
946 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
948 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
950 if (filter.pfx.family || filter.label) {
951 struct rtattr *tb[IFA_MAX+1];
952 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
954 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
956 if (filter.pfx.family && tb[IFA_LOCAL]) {
958 memset(&dst, 0, sizeof(dst));
959 dst.family = ifa->ifa_family;
960 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
961 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
968 label = RTA_DATA(tb[IFA_LABEL]);
970 label = ll_idx_n2a(ifa->ifa_index, b1);
971 if (fnmatch(filter.label, label, 0) != 0)
986 for (l=linfo; l; l = n) {
988 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
989 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
990 if (filter.family != AF_PACKET)
991 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
1000 int ipaddr_list_link(int argc, char **argv)
1002 preferred_family = AF_PACKET;
1004 return ipaddr_list_or_flush(argc, argv, 0);
1007 void ipaddr_reset_filter(int oneline)
1009 memset(&filter, 0, sizeof(filter));
1010 filter.oneline = oneline;
1013 static int default_scope(inet_prefix *lcl)
1015 if (lcl->family == AF_INET) {
1016 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
1017 return RT_SCOPE_HOST;
1022 static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
1026 struct ifaddrmsg ifa;
1031 char *lcl_arg = NULL;
1032 char *valid_lftp = NULL;
1033 char *preferred_lftp = NULL;
1041 __u32 preferred_lft = INFINITY_LIFE_TIME;
1042 __u32 valid_lft = INFINITY_LIFE_TIME;
1043 struct ifa_cacheinfo cinfo;
1045 memset(&req, 0, sizeof(req));
1047 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1048 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
1049 req.n.nlmsg_type = cmd;
1050 req.ifa.ifa_family = preferred_family;
1053 if (strcmp(*argv, "peer") == 0 ||
1054 strcmp(*argv, "remote") == 0) {
1058 duparg("peer", *argv);
1059 get_prefix(&peer, *argv, req.ifa.ifa_family);
1060 peer_len = peer.bytelen;
1061 if (req.ifa.ifa_family == AF_UNSPEC)
1062 req.ifa.ifa_family = peer.family;
1063 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1064 req.ifa.ifa_prefixlen = peer.bitlen;
1065 } else if (matches(*argv, "broadcast") == 0 ||
1066 strcmp(*argv, "brd") == 0) {
1070 duparg("broadcast", *argv);
1071 if (strcmp(*argv, "+") == 0)
1073 else if (strcmp(*argv, "-") == 0)
1076 get_addr(&addr, *argv, req.ifa.ifa_family);
1077 if (req.ifa.ifa_family == AF_UNSPEC)
1078 req.ifa.ifa_family = addr.family;
1079 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1080 brd_len = addr.bytelen;
1082 } else if (strcmp(*argv, "anycast") == 0) {
1086 duparg("anycast", *argv);
1087 get_addr(&addr, *argv, req.ifa.ifa_family);
1088 if (req.ifa.ifa_family == AF_UNSPEC)
1089 req.ifa.ifa_family = addr.family;
1090 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1091 any_len = addr.bytelen;
1092 } else if (strcmp(*argv, "scope") == 0) {
1095 if (rtnl_rtscope_a2n(&scope, *argv))
1096 invarg(*argv, "invalid scope value.");
1097 req.ifa.ifa_scope = scope;
1099 } else if (strcmp(*argv, "dev") == 0) {
1102 } else if (strcmp(*argv, "label") == 0) {
1105 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
1106 } else if (matches(*argv, "valid_lft") == 0) {
1108 duparg("valid_lft", *argv);
1111 if (set_lifetime(&valid_lft, *argv))
1112 invarg("valid_lft value", *argv);
1113 } else if (matches(*argv, "preferred_lft") == 0) {
1115 duparg("preferred_lft", *argv);
1117 preferred_lftp = *argv;
1118 if (set_lifetime(&preferred_lft, *argv))
1119 invarg("preferred_lft value", *argv);
1120 } else if (strcmp(*argv, "home") == 0) {
1121 req.ifa.ifa_flags |= IFA_F_HOMEADDRESS;
1122 } else if (strcmp(*argv, "nodad") == 0) {
1123 req.ifa.ifa_flags |= IFA_F_NODAD;
1125 if (strcmp(*argv, "local") == 0) {
1128 if (matches(*argv, "help") == 0)
1131 duparg2("local", *argv);
1133 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1134 if (req.ifa.ifa_family == AF_UNSPEC)
1135 req.ifa.ifa_family = lcl.family;
1136 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1137 local_len = lcl.bytelen;
1142 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1145 if (l && matches(d, l) != 0) {
1146 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
1150 if (peer_len == 0 && local_len) {
1151 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1153 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1154 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1155 " This special behaviour is likely to disappear in further releases,\n" \
1156 " fix your scripts!\n", lcl_arg, local_len*8);
1159 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1162 if (req.ifa.ifa_prefixlen == 0)
1163 req.ifa.ifa_prefixlen = lcl.bitlen;
1165 if (brd_len < 0 && cmd != RTM_DELADDR) {
1168 if (req.ifa.ifa_family != AF_INET) {
1169 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1173 if (brd.bitlen <= 30) {
1174 for (i=31; i>=brd.bitlen; i--) {
1176 brd.data[0] |= htonl(1<<(31-i));
1178 brd.data[0] &= ~htonl(1<<(31-i));
1180 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1181 brd_len = brd.bytelen;
1184 if (!scoped && cmd != RTM_DELADDR)
1185 req.ifa.ifa_scope = default_scope(&lcl);
1189 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1190 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1194 if (valid_lftp || preferred_lftp) {
1196 fprintf(stderr, "valid_lft is zero\n");
1199 if (valid_lft < preferred_lft) {
1200 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1204 memset(&cinfo, 0, sizeof(cinfo));
1205 cinfo.ifa_prefered = preferred_lft;
1206 cinfo.ifa_valid = valid_lft;
1207 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1211 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1217 int do_ipaddr(int argc, char **argv)
1220 return ipaddr_list_or_flush(0, NULL, 0);
1221 if (matches(*argv, "add") == 0)
1222 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1223 if (matches(*argv, "change") == 0 ||
1224 strcmp(*argv, "chg") == 0)
1225 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1226 if (matches(*argv, "replace") == 0)
1227 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
1228 if (matches(*argv, "delete") == 0)
1229 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
1230 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1231 || matches(*argv, "lst") == 0)
1232 return ipaddr_list_or_flush(argc-1, argv+1, 0);
1233 if (matches(*argv, "flush") == 0)
1234 return ipaddr_list_or_flush(argc-1, argv+1, 1);
1235 if (matches(*argv, "help") == 0)
1237 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);