1 /* vi: set sw=4 ts=4: */
3 * ipaddress.c "ip address".
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
13 //#include <sys/socket.h>
14 //#include <sys/ioctl.h>
17 #include <net/if_arp.h>
19 #include "ip_common.h" /* #include "libbb.h" is inside */
24 typedef struct filter_t {
38 struct rtnl_handle *rth;
41 #define filter (*(filter_t*)&bb_common_bufsiz1)
44 static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
47 flags &= ~IFF_RUNNING;
48 #define _PF(f) if (flags&IFF_##f) { \
50 fprintf(fp, #f "%s", flags ? "," : ""); }
70 fprintf(fp, "%x", flags);
72 fprintf(fp, ",M-DOWN");
76 static void print_queuelen(char *name)
81 s = socket(AF_INET, SOCK_STREAM, 0);
85 memset(&ifr, 0, sizeof(ifr));
86 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
87 if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
94 printf("qlen %d", ifr.ifr_qlen);
97 static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
98 const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
100 FILE *fp = (FILE*)arg;
101 struct ifinfomsg *ifi = NLMSG_DATA(n);
102 struct rtattr * tb[IFLA_MAX+1];
103 int len = n->nlmsg_len;
106 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
109 len -= NLMSG_LENGTH(sizeof(*ifi));
113 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
115 if (filter.up && !(ifi->ifi_flags&IFF_UP))
118 memset(tb, 0, sizeof(tb));
119 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
120 if (tb[IFLA_IFNAME] == NULL) {
121 bb_error_msg("nil ifname");
125 && (!filter.family || filter.family == AF_PACKET)
126 && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
131 if (n->nlmsg_type == RTM_DELLINK)
132 fprintf(fp, "Deleted ");
134 fprintf(fp, "%d: %s", ifi->ifi_index,
135 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
139 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
141 fprintf(fp, "@NONE: ");
143 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
144 m_flag = ll_index_to_flags(iflink);
145 m_flag = !(m_flag & IFF_UP);
150 print_link_flags(fp, ifi->ifi_flags, m_flag);
153 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
155 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
157 if (tb[IFLA_MASTER]) {
159 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
162 if (filter.showqueue)
163 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
165 if (!filter.family || filter.family == AF_PACKET) {
167 fprintf(fp, "%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
169 if (tb[IFLA_ADDRESS]) {
170 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
171 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
175 if (tb[IFLA_BROADCAST]) {
176 if (ifi->ifi_flags&IFF_POINTOPOINT)
177 fprintf(fp, " peer ");
179 fprintf(fp, " brd ");
180 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
181 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
191 static int flush_update(void)
193 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
194 bb_perror_msg("failed to send flush request");
201 static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
202 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
204 FILE *fp = (FILE*)arg;
205 struct ifaddrmsg *ifa = NLMSG_DATA(n);
206 int len = n->nlmsg_len;
207 struct rtattr * rta_tb[IFA_MAX+1];
211 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
213 len -= NLMSG_LENGTH(sizeof(*ifa));
215 bb_error_msg("wrong nlmsg len %d", len);
219 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
222 memset(rta_tb, 0, sizeof(rta_tb));
223 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
225 if (!rta_tb[IFA_LOCAL])
226 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
227 if (!rta_tb[IFA_ADDRESS])
228 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
230 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
232 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
234 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
238 if (rta_tb[IFA_LABEL])
239 label = RTA_DATA(rta_tb[IFA_LABEL]);
241 label = ll_idx_n2a(ifa->ifa_index, b1);
242 if (fnmatch(filter.label, label, 0) != 0)
245 if (filter.pfx.family) {
246 if (rta_tb[IFA_LOCAL]) {
248 memset(&dst, 0, sizeof(dst));
249 dst.family = ifa->ifa_family;
250 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
251 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
258 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
262 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
263 memcpy(fn, n, n->nlmsg_len);
264 fn->nlmsg_type = RTM_DELADDR;
265 fn->nlmsg_flags = NLM_F_REQUEST;
266 fn->nlmsg_seq = ++filter.rth->seq;
267 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
272 if (n->nlmsg_type == RTM_DELADDR)
273 fprintf(fp, "Deleted ");
276 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
277 if (ifa->ifa_family == AF_INET)
278 fprintf(fp, " inet ");
279 else if (ifa->ifa_family == AF_INET6)
280 fprintf(fp, " inet6 ");
282 fprintf(fp, " family %d ", ifa->ifa_family);
284 if (rta_tb[IFA_LOCAL]) {
285 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
286 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
287 RTA_DATA(rta_tb[IFA_LOCAL]),
288 abuf, sizeof(abuf)));
290 if (rta_tb[IFA_ADDRESS] == NULL ||
291 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
292 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
294 fprintf(fp, " peer %s/%d ",
295 rt_addr_n2a(ifa->ifa_family,
296 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
297 RTA_DATA(rta_tb[IFA_ADDRESS]),
303 if (rta_tb[IFA_BROADCAST]) {
304 fprintf(fp, "brd %s ",
305 rt_addr_n2a(ifa->ifa_family,
306 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
307 RTA_DATA(rta_tb[IFA_BROADCAST]),
308 abuf, sizeof(abuf)));
310 if (rta_tb[IFA_ANYCAST]) {
311 fprintf(fp, "any %s ",
312 rt_addr_n2a(ifa->ifa_family,
313 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
314 RTA_DATA(rta_tb[IFA_ANYCAST]),
315 abuf, sizeof(abuf)));
317 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
318 if (ifa->ifa_flags&IFA_F_SECONDARY) {
319 ifa->ifa_flags &= ~IFA_F_SECONDARY;
320 fprintf(fp, "secondary ");
322 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
323 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
324 fprintf(fp, "tentative ");
326 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
327 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
328 fprintf(fp, "deprecated ");
330 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
331 fprintf(fp, "dynamic ");
333 ifa->ifa_flags &= ~IFA_F_PERMANENT;
335 fprintf(fp, "flags %02x ", ifa->ifa_flags);
336 if (rta_tb[IFA_LABEL])
337 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
338 if (rta_tb[IFA_CACHEINFO]) {
339 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
342 if (ci->ifa_valid == 0xFFFFFFFFU)
343 sprintf(buf, "valid_lft forever");
345 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
346 if (ci->ifa_prefered == 0xFFFFFFFFU)
347 sprintf(buf+strlen(buf), " preferred_lft forever");
349 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
350 fprintf(fp, " %s", buf);
360 struct nlmsg_list *next;
364 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
366 for (; ainfo; ainfo = ainfo->next) {
367 struct nlmsghdr *n = &ainfo->h;
368 struct ifaddrmsg *ifa = NLMSG_DATA(n);
370 if (n->nlmsg_type != RTM_NEWADDR)
373 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
376 if (ifa->ifa_index != ifindex ||
377 (filter.family && filter.family != ifa->ifa_family))
380 print_addrinfo(NULL, n, fp);
386 static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
388 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
389 struct nlmsg_list *h;
390 struct nlmsg_list **lp;
392 h = malloc(n->nlmsg_len+sizeof(void*));
396 memcpy(&h->h, n, n->nlmsg_len);
399 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
402 ll_remember_index(who, n, NULL);
406 static void ipaddr_reset_filter(int _oneline)
408 memset(&filter, 0, sizeof(filter));
409 filter.oneline = _oneline;
412 /* Return value becomes exitcode. It's okay to not return at all */
413 int ipaddr_list_or_flush(int argc, char **argv, int flush)
415 static const char option[] = "to\0""scope\0""up\0""label\0""dev\0";
417 struct nlmsg_list *linfo = NULL;
418 struct nlmsg_list *ainfo = NULL;
419 struct nlmsg_list *l;
420 struct rtnl_handle rth;
421 char *filter_dev = NULL;
424 ipaddr_reset_filter(oneline);
425 filter.showqueue = 1;
427 if (filter.family == AF_UNSPEC)
428 filter.family = preferred_family;
432 bb_error_msg_and_die(bb_msg_requires_arg, "flush");
434 if (filter.family == AF_PACKET) {
435 bb_error_msg_and_die("cannot flush link addresses");
440 const int option_num = index_in_strings(option, *argv);
441 switch (option_num) {
444 get_prefix(&filter.pfx, *argv, filter.family);
445 if (filter.family == AF_UNSPEC) {
446 filter.family = filter.pfx.family;
453 filter.scopemask = -1;
454 if (rtnl_rtscope_a2n(&scope, *argv)) {
455 if (strcmp(*argv, "all") != 0) {
456 invarg(*argv, "scope");
458 scope = RT_SCOPE_NOWHERE;
459 filter.scopemask = 0;
461 filter.scope = scope;
469 filter.label = *argv;
475 duparg2("dev", *argv);
485 xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
486 xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
489 filter.ifindex = xll_name_to_index(filter_dev);
493 char flushb[4096-512];
495 filter.flushb = flushb;
497 filter.flushe = sizeof(flushb);
501 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
503 xrtnl_dump_filter(&rth, print_addrinfo, stdout);
504 if (filter.flushed == 0) {
507 if (flush_update() < 0)
512 if (filter.family != AF_PACKET) {
513 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
514 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
518 if (filter.family && filter.family != AF_PACKET) {
519 struct nlmsg_list **lp;
525 while ((l=*lp)!=NULL) {
527 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
528 struct nlmsg_list *a;
530 for (a=ainfo; a; a=a->next) {
531 struct nlmsghdr *n = &a->h;
532 struct ifaddrmsg *ifa = NLMSG_DATA(n);
534 if (ifa->ifa_index != ifi->ifi_index ||
535 (filter.family && filter.family != ifa->ifa_family))
537 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
539 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
541 if (filter.pfx.family || filter.label) {
542 struct rtattr *tb[IFA_MAX+1];
543 memset(tb, 0, sizeof(tb));
544 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
546 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
548 if (filter.pfx.family && tb[IFA_LOCAL]) {
550 memset(&dst, 0, sizeof(dst));
551 dst.family = ifa->ifa_family;
552 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
553 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
560 label = RTA_DATA(tb[IFA_LABEL]);
562 label = ll_idx_n2a(ifa->ifa_index, b1);
563 if (fnmatch(filter.label, label, 0) != 0)
578 for (l = linfo; l; l = l->next) {
579 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
580 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
581 if (filter.family != AF_PACKET)
582 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
584 fflush(stdout); /* why? */
590 static int default_scope(inet_prefix *lcl)
592 if (lcl->family == AF_INET) {
593 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
594 return RT_SCOPE_HOST;
599 /* Return value becomes exitcode. It's okay to not return at all */
600 static int ipaddr_modify(int cmd, int argc, char **argv)
602 static const char option[] =
603 "peer\0""remote\0""broadcast\0""brd\0"
604 "anycast\0""scope\0""dev\0""label\0""local\0";
605 struct rtnl_handle rth;
608 struct ifaddrmsg ifa;
621 memset(&req, 0, sizeof(req));
623 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
624 req.n.nlmsg_flags = NLM_F_REQUEST;
625 req.n.nlmsg_type = cmd;
626 req.ifa.ifa_family = preferred_family;
629 const int option_num = index_in_strings(option, *argv);
630 switch (option_num) {
636 duparg("peer", *argv);
638 get_prefix(&peer, *argv, req.ifa.ifa_family);
639 peer_len = peer.bytelen;
640 if (req.ifa.ifa_family == AF_UNSPEC) {
641 req.ifa.ifa_family = peer.family;
643 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
644 req.ifa.ifa_prefixlen = peer.bitlen;
646 case 2: /* broadcast */
652 duparg("broadcast", *argv);
654 if (LONE_CHAR(*argv, '+')) {
657 else if (LONE_DASH(*argv)) {
660 get_addr(&addr, *argv, req.ifa.ifa_family);
661 if (req.ifa.ifa_family == AF_UNSPEC)
662 req.ifa.ifa_family = addr.family;
663 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
664 brd_len = addr.bytelen;
668 case 4: /* anycast */
673 duparg("anycast", *argv);
675 get_addr(&addr, *argv, req.ifa.ifa_family);
676 if (req.ifa.ifa_family == AF_UNSPEC) {
677 req.ifa.ifa_family = addr.family;
679 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
680 any_len = addr.bytelen;
687 if (rtnl_rtscope_a2n(&scope, *argv)) {
688 invarg(*argv, "scope");
690 req.ifa.ifa_scope = scope;
701 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
707 duparg2("local", *argv);
709 get_prefix(&lcl, *argv, req.ifa.ifa_family);
710 if (req.ifa.ifa_family == AF_UNSPEC) {
711 req.ifa.ifa_family = lcl.family;
713 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
714 local_len = lcl.bytelen;
721 bb_error_msg(bb_msg_requires_arg,"\"dev\"");
724 if (l && strncmp(d, l, strlen(d)) != 0) {
725 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
728 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
730 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
732 if (req.ifa.ifa_prefixlen == 0)
733 req.ifa.ifa_prefixlen = lcl.bitlen;
735 if (brd_len < 0 && cmd != RTM_DELADDR) {
738 if (req.ifa.ifa_family != AF_INET) {
739 bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
742 if (brd.bitlen <= 30) {
743 for (i=31; i>=brd.bitlen; i--) {
745 brd.data[0] |= htonl(1<<(31-i));
747 brd.data[0] &= ~htonl(1<<(31-i));
749 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
750 brd_len = brd.bytelen;
753 if (!scoped && cmd != RTM_DELADDR)
754 req.ifa.ifa_scope = default_scope(&lcl);
760 req.ifa.ifa_index = xll_name_to_index(d);
762 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
768 /* Return value becomes exitcode. It's okay to not return at all */
769 int do_ipaddr(int argc, char **argv)
771 static const char commands[] =
772 "add\0""delete\0""list\0""show\0""lst\0""flush\0";
774 int command_num = 2; /* default command is list */
777 command_num = index_in_substrings(commands, *argv);
779 if (command_num < 0 || command_num > 5)
780 bb_error_msg_and_die("unknown command %s", *argv);
783 if (command_num == 0) /* add */
784 return ipaddr_modify(RTM_NEWADDR, argc, argv);
785 else if (command_num == 1) /* delete */
786 return ipaddr_modify(RTM_DELADDR, argc, argv);
787 else if (command_num == 5) /* flush */
788 return ipaddr_list_or_flush(argc, argv, 1);
789 else /* 2 == list, 3 == show, 4 == lst */
790 return ipaddr_list_or_flush(argc, argv, 0);