2 * ipaddress.c "ip address".
4 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
12 #include <sys/socket.h>
13 #include <sys/ioctl.h>
20 #include <arpa/inet.h>
23 #include <net/if_arp.h>
27 #include "ip_common.h"
46 struct rtnl_handle *rth;
49 static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
52 flags &= ~IFF_RUNNING;
53 #define _PF(f) if (flags&IFF_##f) { \
55 fprintf(fp, #f "%s", flags ? "," : ""); }
75 fprintf(fp, "%x", flags);
77 fprintf(fp, ",M-DOWN");
81 static void print_queuelen(char *name)
86 s = socket(AF_INET, SOCK_STREAM, 0);
90 memset(&ifr, 0, sizeof(ifr));
91 strcpy(ifr.ifr_name, name);
92 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
93 perror("SIOCGIFXQLEN");
100 printf("qlen %d", ifr.ifr_qlen);
103 static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
104 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
106 FILE *fp = (FILE*)arg;
107 struct ifinfomsg *ifi = NLMSG_DATA(n);
108 struct rtattr * tb[IFLA_MAX+1];
109 int len = n->nlmsg_len;
112 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
115 len -= NLMSG_LENGTH(sizeof(*ifi));
119 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
121 if (filter.up && !(ifi->ifi_flags&IFF_UP))
124 memset(tb, 0, sizeof(tb));
125 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
126 if (tb[IFLA_IFNAME] == NULL) {
127 bb_error_msg("nil ifname");
131 (!filter.family || filter.family == AF_PACKET) &&
132 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
135 if (n->nlmsg_type == RTM_DELLINK)
136 fprintf(fp, "Deleted ");
138 fprintf(fp, "%d: %s", ifi->ifi_index,
139 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
143 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
145 fprintf(fp, "@NONE: ");
147 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
148 m_flag = ll_index_to_flags(iflink);
149 m_flag = !(m_flag & IFF_UP);
154 print_link_flags(fp, ifi->ifi_flags, m_flag);
157 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
159 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
161 if (tb[IFLA_MASTER]) {
163 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
166 if (filter.showqueue)
167 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
169 if (!filter.family || filter.family == AF_PACKET) {
171 fprintf(fp, "%s", _SL_);
172 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
174 if (tb[IFLA_ADDRESS]) {
175 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
176 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
180 if (tb[IFLA_BROADCAST]) {
181 if (ifi->ifi_flags&IFF_POINTOPOINT)
182 fprintf(fp, " peer ");
184 fprintf(fp, " brd ");
185 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
186 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
196 static int flush_update(void)
198 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
199 perror("Failed to send flush request\n");
206 static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
207 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
209 FILE *fp = (FILE*)arg;
210 struct ifaddrmsg *ifa = NLMSG_DATA(n);
211 int len = n->nlmsg_len;
212 struct rtattr * rta_tb[IFA_MAX+1];
216 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
218 len -= NLMSG_LENGTH(sizeof(*ifa));
220 bb_error_msg("wrong nlmsg len %d", len);
224 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
227 memset(rta_tb, 0, sizeof(rta_tb));
228 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
230 if (!rta_tb[IFA_LOCAL])
231 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
232 if (!rta_tb[IFA_ADDRESS])
233 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
235 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
237 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
239 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
243 if (rta_tb[IFA_LABEL])
244 label = RTA_DATA(rta_tb[IFA_LABEL]);
246 label = ll_idx_n2a(ifa->ifa_index, b1);
247 if (fnmatch(filter.label, label, 0) != 0)
250 if (filter.pfx.family) {
251 if (rta_tb[IFA_LOCAL]) {
253 memset(&dst, 0, sizeof(dst));
254 dst.family = ifa->ifa_family;
255 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
256 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
263 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
267 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
268 memcpy(fn, n, n->nlmsg_len);
269 fn->nlmsg_type = RTM_DELADDR;
270 fn->nlmsg_flags = NLM_F_REQUEST;
271 fn->nlmsg_seq = ++filter.rth->seq;
272 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
277 if (n->nlmsg_type == RTM_DELADDR)
278 fprintf(fp, "Deleted ");
281 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
282 if (ifa->ifa_family == AF_INET)
283 fprintf(fp, " inet ");
284 else if (ifa->ifa_family == AF_INET6)
285 fprintf(fp, " inet6 ");
287 fprintf(fp, " family %d ", ifa->ifa_family);
289 if (rta_tb[IFA_LOCAL]) {
290 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
291 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
292 RTA_DATA(rta_tb[IFA_LOCAL]),
293 abuf, sizeof(abuf)));
295 if (rta_tb[IFA_ADDRESS] == NULL ||
296 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
297 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
299 fprintf(fp, " peer %s/%d ",
300 rt_addr_n2a(ifa->ifa_family,
301 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
302 RTA_DATA(rta_tb[IFA_ADDRESS]),
308 if (rta_tb[IFA_BROADCAST]) {
309 fprintf(fp, "brd %s ",
310 rt_addr_n2a(ifa->ifa_family,
311 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
312 RTA_DATA(rta_tb[IFA_BROADCAST]),
313 abuf, sizeof(abuf)));
315 if (rta_tb[IFA_ANYCAST]) {
316 fprintf(fp, "any %s ",
317 rt_addr_n2a(ifa->ifa_family,
318 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
319 RTA_DATA(rta_tb[IFA_ANYCAST]),
320 abuf, sizeof(abuf)));
322 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
323 if (ifa->ifa_flags&IFA_F_SECONDARY) {
324 ifa->ifa_flags &= ~IFA_F_SECONDARY;
325 fprintf(fp, "secondary ");
327 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
328 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
329 fprintf(fp, "tentative ");
331 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
332 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
333 fprintf(fp, "deprecated ");
335 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
336 fprintf(fp, "dynamic ");
338 ifa->ifa_flags &= ~IFA_F_PERMANENT;
340 fprintf(fp, "flags %02x ", ifa->ifa_flags);
341 if (rta_tb[IFA_LABEL])
342 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
343 if (rta_tb[IFA_CACHEINFO]) {
344 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
346 fprintf(fp, "%s", _SL_);
347 if (ci->ifa_valid == 0xFFFFFFFFU)
348 sprintf(buf, "valid_lft forever");
350 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
351 if (ci->ifa_prefered == 0xFFFFFFFFU)
352 sprintf(buf+strlen(buf), " preferred_lft forever");
354 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
355 fprintf(fp, " %s", buf);
365 struct nlmsg_list *next;
369 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
371 for ( ;ainfo ; ainfo = ainfo->next) {
372 struct nlmsghdr *n = &ainfo->h;
373 struct ifaddrmsg *ifa = NLMSG_DATA(n);
375 if (n->nlmsg_type != RTM_NEWADDR)
378 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
381 if (ifa->ifa_index != ifindex ||
382 (filter.family && filter.family != ifa->ifa_family))
385 print_addrinfo(NULL, n, fp);
391 static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
393 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
394 struct nlmsg_list *h;
395 struct nlmsg_list **lp;
397 h = malloc(n->nlmsg_len+sizeof(void*));
401 memcpy(&h->h, n, n->nlmsg_len);
404 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
407 ll_remember_index(who, n, NULL);
411 static void ipaddr_reset_filter(int _oneline)
413 memset(&filter, 0, sizeof(filter));
414 filter.oneline = _oneline;
417 int ipaddr_list_or_flush(int argc, char **argv, int flush)
419 static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 };
421 struct nlmsg_list *linfo = NULL;
422 struct nlmsg_list *ainfo = NULL;
423 struct nlmsg_list *l;
424 struct rtnl_handle rth;
425 char *filter_dev = NULL;
428 ipaddr_reset_filter(oneline);
429 filter.showqueue = 1;
431 if (filter.family == AF_UNSPEC)
432 filter.family = preferred_family;
436 fprintf(stderr, "Flush requires arguments.\n");
439 if (filter.family == AF_PACKET) {
440 fprintf(stderr, "Cannot flush link addresses.\n");
446 const int option_num = compare_string_array(option, *argv);
447 switch (option_num) {
450 get_prefix(&filter.pfx, *argv, filter.family);
451 if (filter.family == AF_UNSPEC) {
452 filter.family = filter.pfx.family;
459 filter.scopemask = -1;
460 if (rtnl_rtscope_a2n(&scope, *argv)) {
461 if (strcmp(*argv, "all") != 0) {
462 invarg("invalid \"scope\"\n", *argv);
464 scope = RT_SCOPE_NOWHERE;
465 filter.scopemask = 0;
467 filter.scope = scope;
475 filter.label = *argv;
481 duparg2("dev", *argv);
489 if (rtnl_open(&rth, 0) < 0)
492 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
493 bb_perror_msg_and_die("Cannot send dump request");
496 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
497 bb_error_msg_and_die("Dump terminated");
501 filter.ifindex = ll_name_to_index(filter_dev);
502 if (filter.ifindex <= 0) {
503 bb_error_msg("Device \"%s\" does not exist", filter_dev);
509 char flushb[4096-512];
511 filter.flushb = flushb;
513 filter.flushe = sizeof(flushb);
517 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
518 perror("Cannot send dump request");
522 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
523 fprintf(stderr, "Flush terminated\n");
526 if (filter.flushed == 0) {
530 if (flush_update() < 0)
535 if (filter.family != AF_PACKET) {
536 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
537 bb_perror_msg_and_die("Cannot send dump request");
540 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
541 bb_error_msg_and_die("Dump terminated");
546 if (filter.family && filter.family != AF_PACKET) {
547 struct nlmsg_list **lp;
553 while ((l=*lp)!=NULL) {
555 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
556 struct nlmsg_list *a;
558 for (a=ainfo; a; a=a->next) {
559 struct nlmsghdr *n = &a->h;
560 struct ifaddrmsg *ifa = NLMSG_DATA(n);
562 if (ifa->ifa_index != ifi->ifi_index ||
563 (filter.family && filter.family != ifa->ifa_family))
565 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
567 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
569 if (filter.pfx.family || filter.label) {
570 struct rtattr *tb[IFA_MAX+1];
571 memset(tb, 0, sizeof(tb));
572 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
574 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
576 if (filter.pfx.family && tb[IFA_LOCAL]) {
578 memset(&dst, 0, sizeof(dst));
579 dst.family = ifa->ifa_family;
580 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
581 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
588 label = RTA_DATA(tb[IFA_LABEL]);
590 label = ll_idx_n2a(ifa->ifa_index, b1);
591 if (fnmatch(filter.label, label, 0) != 0)
606 for (l=linfo; l; l = l->next) {
607 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
608 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
609 if (filter.family != AF_PACKET)
610 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
618 static int default_scope(inet_prefix *lcl)
620 if (lcl->family == AF_INET) {
621 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
622 return RT_SCOPE_HOST;
627 static int ipaddr_modify(int cmd, int argc, char **argv)
629 static const char *const option[] = {
630 "peer", "remote", "broadcast", "brd",
631 "anycast", "scope", "dev", "label", "local", 0
634 struct rtnl_handle rth;
637 struct ifaddrmsg ifa;
650 memset(&req, 0, sizeof(req));
652 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
653 req.n.nlmsg_flags = NLM_F_REQUEST;
654 req.n.nlmsg_type = cmd;
655 req.ifa.ifa_family = preferred_family;
658 const int option_num = compare_string_array(option, *argv);
659 switch (option_num) {
665 duparg("peer", *argv);
667 get_prefix(&peer, *argv, req.ifa.ifa_family);
668 peer_len = peer.bytelen;
669 if (req.ifa.ifa_family == AF_UNSPEC) {
670 req.ifa.ifa_family = peer.family;
672 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
673 req.ifa.ifa_prefixlen = peer.bitlen;
675 case 2: /* broadcast */
681 duparg("broadcast", *argv);
683 if (strcmp(*argv, "+") == 0) {
686 else if (strcmp(*argv, "-") == 0) {
689 get_addr(&addr, *argv, req.ifa.ifa_family);
690 if (req.ifa.ifa_family == AF_UNSPEC)
691 req.ifa.ifa_family = addr.family;
692 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
693 brd_len = addr.bytelen;
697 case 4: /* anycast */
702 duparg("anycast", *argv);
704 get_addr(&addr, *argv, req.ifa.ifa_family);
705 if (req.ifa.ifa_family == AF_UNSPEC) {
706 req.ifa.ifa_family = addr.family;
708 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
709 any_len = addr.bytelen;
716 if (rtnl_rtscope_a2n(&scope, *argv)) {
717 invarg(*argv, "invalid scope value");
719 req.ifa.ifa_scope = scope;
730 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
736 duparg2("local", *argv);
738 get_prefix(&lcl, *argv, req.ifa.ifa_family);
739 if (req.ifa.ifa_family == AF_UNSPEC) {
740 req.ifa.ifa_family = lcl.family;
742 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
743 local_len = lcl.bytelen;
750 bb_error_msg("Not enough information: \"dev\" argument is required");
753 if (l && matches(d, l) != 0) {
754 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
757 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
759 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
761 if (req.ifa.ifa_prefixlen == 0)
762 req.ifa.ifa_prefixlen = lcl.bitlen;
764 if (brd_len < 0 && cmd != RTM_DELADDR) {
767 if (req.ifa.ifa_family != AF_INET) {
768 bb_error_msg("Broadcast can be set only for IPv4 addresses");
772 if (brd.bitlen <= 30) {
773 for (i=31; i>=brd.bitlen; i--) {
775 brd.data[0] |= htonl(1<<(31-i));
777 brd.data[0] &= ~htonl(1<<(31-i));
779 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
780 brd_len = brd.bytelen;
783 if (!scoped && cmd != RTM_DELADDR)
784 req.ifa.ifa_scope = default_scope(&lcl);
786 if (rtnl_open(&rth, 0) < 0)
791 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
792 bb_error_msg("Cannot find device \"%s\"", d);
796 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
802 int do_ipaddr(int argc, char **argv)
804 static const char *const commands[] = {
805 "add", "delete", "list", "show", "lst", "flush", 0
811 command_num = compare_string_array(commands, *argv);
813 switch (command_num) {
815 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
817 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
821 return ipaddr_list_or_flush(argc-1, argv+1, 0);
823 return ipaddr_list_or_flush(argc-1, argv+1, 1);
825 bb_error_msg_and_die("Unknown command %s", *argv);