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
14 #include <sys/socket.h>
15 #include <sys/ioctl.h>
22 #include <net/if_arp.h>
26 #include "ip_common.h"
44 struct rtnl_handle *rth;
47 static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
50 flags &= ~IFF_RUNNING;
51 #define _PF(f) if (flags&IFF_##f) { \
53 fprintf(fp, #f "%s", flags ? "," : ""); }
73 fprintf(fp, "%x", flags);
75 fprintf(fp, ",M-DOWN");
79 static void print_queuelen(char *name)
84 s = socket(AF_INET, SOCK_STREAM, 0);
88 memset(&ifr, 0, sizeof(ifr));
89 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
90 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
91 perror("SIOCGIFXQLEN");
98 printf("qlen %d", ifr.ifr_qlen);
101 static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
102 const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
104 FILE *fp = (FILE*)arg;
105 struct ifinfomsg *ifi = NLMSG_DATA(n);
106 struct rtattr * tb[IFLA_MAX+1];
107 int len = n->nlmsg_len;
110 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
113 len -= NLMSG_LENGTH(sizeof(*ifi));
117 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
119 if (filter.up && !(ifi->ifi_flags&IFF_UP))
122 memset(tb, 0, sizeof(tb));
123 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
124 if (tb[IFLA_IFNAME] == NULL) {
125 bb_error_msg("nil ifname");
129 (!filter.family || filter.family == AF_PACKET) &&
130 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
133 if (n->nlmsg_type == RTM_DELLINK)
134 fprintf(fp, "Deleted ");
136 fprintf(fp, "%d: %s", ifi->ifi_index,
137 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
141 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
143 fprintf(fp, "@NONE: ");
145 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
146 m_flag = ll_index_to_flags(iflink);
147 m_flag = !(m_flag & IFF_UP);
152 print_link_flags(fp, ifi->ifi_flags, m_flag);
155 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
157 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
159 if (tb[IFLA_MASTER]) {
161 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
164 if (filter.showqueue)
165 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
167 if (!filter.family || filter.family == AF_PACKET) {
169 fprintf(fp, "%s", _SL_);
170 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
172 if (tb[IFLA_ADDRESS]) {
173 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
174 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
178 if (tb[IFLA_BROADCAST]) {
179 if (ifi->ifi_flags&IFF_POINTOPOINT)
180 fprintf(fp, " peer ");
182 fprintf(fp, " brd ");
183 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
184 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
194 static int flush_update(void)
196 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
197 perror("Failed to send flush request\n");
204 static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
205 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
207 FILE *fp = (FILE*)arg;
208 struct ifaddrmsg *ifa = NLMSG_DATA(n);
209 int len = n->nlmsg_len;
210 struct rtattr * rta_tb[IFA_MAX+1];
214 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
216 len -= NLMSG_LENGTH(sizeof(*ifa));
218 bb_error_msg("wrong nlmsg len %d", len);
222 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
225 memset(rta_tb, 0, sizeof(rta_tb));
226 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
228 if (!rta_tb[IFA_LOCAL])
229 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
230 if (!rta_tb[IFA_ADDRESS])
231 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
233 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
235 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
237 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
241 if (rta_tb[IFA_LABEL])
242 label = RTA_DATA(rta_tb[IFA_LABEL]);
244 label = ll_idx_n2a(ifa->ifa_index, b1);
245 if (fnmatch(filter.label, label, 0) != 0)
248 if (filter.pfx.family) {
249 if (rta_tb[IFA_LOCAL]) {
251 memset(&dst, 0, sizeof(dst));
252 dst.family = ifa->ifa_family;
253 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
254 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
261 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
265 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
266 memcpy(fn, n, n->nlmsg_len);
267 fn->nlmsg_type = RTM_DELADDR;
268 fn->nlmsg_flags = NLM_F_REQUEST;
269 fn->nlmsg_seq = ++filter.rth->seq;
270 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
275 if (n->nlmsg_type == RTM_DELADDR)
276 fprintf(fp, "Deleted ");
279 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
280 if (ifa->ifa_family == AF_INET)
281 fprintf(fp, " inet ");
282 else if (ifa->ifa_family == AF_INET6)
283 fprintf(fp, " inet6 ");
285 fprintf(fp, " family %d ", ifa->ifa_family);
287 if (rta_tb[IFA_LOCAL]) {
288 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
289 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
290 RTA_DATA(rta_tb[IFA_LOCAL]),
291 abuf, sizeof(abuf)));
293 if (rta_tb[IFA_ADDRESS] == NULL ||
294 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
295 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
297 fprintf(fp, " peer %s/%d ",
298 rt_addr_n2a(ifa->ifa_family,
299 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
300 RTA_DATA(rta_tb[IFA_ADDRESS]),
306 if (rta_tb[IFA_BROADCAST]) {
307 fprintf(fp, "brd %s ",
308 rt_addr_n2a(ifa->ifa_family,
309 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
310 RTA_DATA(rta_tb[IFA_BROADCAST]),
311 abuf, sizeof(abuf)));
313 if (rta_tb[IFA_ANYCAST]) {
314 fprintf(fp, "any %s ",
315 rt_addr_n2a(ifa->ifa_family,
316 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
317 RTA_DATA(rta_tb[IFA_ANYCAST]),
318 abuf, sizeof(abuf)));
320 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
321 if (ifa->ifa_flags&IFA_F_SECONDARY) {
322 ifa->ifa_flags &= ~IFA_F_SECONDARY;
323 fprintf(fp, "secondary ");
325 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
326 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
327 fprintf(fp, "tentative ");
329 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
330 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
331 fprintf(fp, "deprecated ");
333 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
334 fprintf(fp, "dynamic ");
336 ifa->ifa_flags &= ~IFA_F_PERMANENT;
338 fprintf(fp, "flags %02x ", ifa->ifa_flags);
339 if (rta_tb[IFA_LABEL])
340 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
341 if (rta_tb[IFA_CACHEINFO]) {
342 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
344 fprintf(fp, "%s", _SL_);
345 if (ci->ifa_valid == 0xFFFFFFFFU)
346 sprintf(buf, "valid_lft forever");
348 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
349 if (ci->ifa_prefered == 0xFFFFFFFFU)
350 sprintf(buf+strlen(buf), " preferred_lft forever");
352 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
353 fprintf(fp, " %s", buf);
363 struct nlmsg_list *next;
367 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
369 for ( ;ainfo ; ainfo = ainfo->next) {
370 struct nlmsghdr *n = &ainfo->h;
371 struct ifaddrmsg *ifa = NLMSG_DATA(n);
373 if (n->nlmsg_type != RTM_NEWADDR)
376 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
379 if (ifa->ifa_index != ifindex ||
380 (filter.family && filter.family != ifa->ifa_family))
383 print_addrinfo(NULL, n, fp);
389 static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
391 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
392 struct nlmsg_list *h;
393 struct nlmsg_list **lp;
395 h = malloc(n->nlmsg_len+sizeof(void*));
399 memcpy(&h->h, n, n->nlmsg_len);
402 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
405 ll_remember_index(who, n, NULL);
409 static void ipaddr_reset_filter(int _oneline)
411 memset(&filter, 0, sizeof(filter));
412 filter.oneline = _oneline;
415 int ipaddr_list_or_flush(int argc, char **argv, int flush)
417 static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 };
419 struct nlmsg_list *linfo = NULL;
420 struct nlmsg_list *ainfo = NULL;
421 struct nlmsg_list *l;
422 struct rtnl_handle rth;
423 char *filter_dev = NULL;
426 ipaddr_reset_filter(oneline);
427 filter.showqueue = 1;
429 if (filter.family == AF_UNSPEC)
430 filter.family = preferred_family;
434 bb_error_msg(bb_msg_requires_arg, "flush");
437 if (filter.family == AF_PACKET) {
438 bb_error_msg("cannot flush link addresses");
444 const int option_num = index_in_str_array(option, *argv);
445 switch (option_num) {
448 get_prefix(&filter.pfx, *argv, filter.family);
449 if (filter.family == AF_UNSPEC) {
450 filter.family = filter.pfx.family;
457 filter.scopemask = -1;
458 if (rtnl_rtscope_a2n(&scope, *argv)) {
459 if (strcmp(*argv, "all") != 0) {
460 invarg(*argv, "scope");
462 scope = RT_SCOPE_NOWHERE;
463 filter.scopemask = 0;
465 filter.scope = scope;
473 filter.label = *argv;
479 duparg2("dev", *argv);
487 if (rtnl_open(&rth, 0) < 0)
490 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
491 bb_perror_msg_and_die("cannot send dump request");
494 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
495 bb_error_msg_and_die("dump terminated");
499 filter.ifindex = ll_name_to_index(filter_dev);
500 if (filter.ifindex <= 0) {
501 bb_error_msg("device \"%s\" does not exist", filter_dev);
507 char flushb[4096-512];
509 filter.flushb = flushb;
511 filter.flushe = sizeof(flushb);
515 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
516 perror("Cannot send dump request");
520 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
521 fprintf(stderr, "Flush terminated\n");
524 if (filter.flushed == 0) {
528 if (flush_update() < 0)
533 if (filter.family != AF_PACKET) {
534 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
535 bb_perror_msg_and_die("cannot send dump request");
538 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
539 bb_error_msg_and_die("dump terminated");
544 if (filter.family && filter.family != AF_PACKET) {
545 struct nlmsg_list **lp;
551 while ((l=*lp)!=NULL) {
553 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
554 struct nlmsg_list *a;
556 for (a=ainfo; a; a=a->next) {
557 struct nlmsghdr *n = &a->h;
558 struct ifaddrmsg *ifa = NLMSG_DATA(n);
560 if (ifa->ifa_index != ifi->ifi_index ||
561 (filter.family && filter.family != ifa->ifa_family))
563 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
565 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
567 if (filter.pfx.family || filter.label) {
568 struct rtattr *tb[IFA_MAX+1];
569 memset(tb, 0, sizeof(tb));
570 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
572 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
574 if (filter.pfx.family && tb[IFA_LOCAL]) {
576 memset(&dst, 0, sizeof(dst));
577 dst.family = ifa->ifa_family;
578 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
579 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
586 label = RTA_DATA(tb[IFA_LABEL]);
588 label = ll_idx_n2a(ifa->ifa_index, b1);
589 if (fnmatch(filter.label, label, 0) != 0)
604 for (l=linfo; l; l = l->next) {
605 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
606 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
607 if (filter.family != AF_PACKET)
608 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
616 static int default_scope(inet_prefix *lcl)
618 if (lcl->family == AF_INET) {
619 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
620 return RT_SCOPE_HOST;
625 static int ipaddr_modify(int cmd, int argc, char **argv)
627 static const char *const option[] = {
628 "peer", "remote", "broadcast", "brd",
629 "anycast", "scope", "dev", "label", "local", 0
632 struct rtnl_handle rth;
635 struct ifaddrmsg ifa;
648 memset(&req, 0, sizeof(req));
650 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
651 req.n.nlmsg_flags = NLM_F_REQUEST;
652 req.n.nlmsg_type = cmd;
653 req.ifa.ifa_family = preferred_family;
656 const int option_num = index_in_str_array(option, *argv);
657 switch (option_num) {
663 duparg("peer", *argv);
665 get_prefix(&peer, *argv, req.ifa.ifa_family);
666 peer_len = peer.bytelen;
667 if (req.ifa.ifa_family == AF_UNSPEC) {
668 req.ifa.ifa_family = peer.family;
670 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
671 req.ifa.ifa_prefixlen = peer.bitlen;
673 case 2: /* broadcast */
679 duparg("broadcast", *argv);
681 if (LONE_CHAR(*argv, '+')) {
684 else if (LONE_DASH(*argv)) {
687 get_addr(&addr, *argv, req.ifa.ifa_family);
688 if (req.ifa.ifa_family == AF_UNSPEC)
689 req.ifa.ifa_family = addr.family;
690 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
691 brd_len = addr.bytelen;
695 case 4: /* anycast */
700 duparg("anycast", *argv);
702 get_addr(&addr, *argv, req.ifa.ifa_family);
703 if (req.ifa.ifa_family == AF_UNSPEC) {
704 req.ifa.ifa_family = addr.family;
706 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
707 any_len = addr.bytelen;
714 if (rtnl_rtscope_a2n(&scope, *argv)) {
715 invarg(*argv, "scope");
717 req.ifa.ifa_scope = scope;
728 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
734 duparg2("local", *argv);
736 get_prefix(&lcl, *argv, req.ifa.ifa_family);
737 if (req.ifa.ifa_family == AF_UNSPEC) {
738 req.ifa.ifa_family = lcl.family;
740 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
741 local_len = lcl.bytelen;
748 bb_error_msg(bb_msg_requires_arg,"\"dev\"");
751 if (l && matches(d, l) != 0) {
752 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
755 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
757 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
759 if (req.ifa.ifa_prefixlen == 0)
760 req.ifa.ifa_prefixlen = lcl.bitlen;
762 if (brd_len < 0 && cmd != RTM_DELADDR) {
765 if (req.ifa.ifa_family != AF_INET) {
766 bb_error_msg("broadcast can be set only for IPv4 addresses");
770 if (brd.bitlen <= 30) {
771 for (i=31; i>=brd.bitlen; i--) {
773 brd.data[0] |= htonl(1<<(31-i));
775 brd.data[0] &= ~htonl(1<<(31-i));
777 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
778 brd_len = brd.bytelen;
781 if (!scoped && cmd != RTM_DELADDR)
782 req.ifa.ifa_scope = default_scope(&lcl);
784 if (rtnl_open(&rth, 0) < 0)
789 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
790 bb_error_msg("cannot find device \"%s\"", d);
794 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
800 int do_ipaddr(int argc, char **argv)
802 static const char *const commands[] = {
803 "add", "delete", "list", "show", "lst", "flush", 0
809 command_num = index_in_substr_array(commands, *argv);
811 switch (command_num) {
813 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
815 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
819 return ipaddr_list_or_flush(argc-1, argv+1, 0);
821 return ipaddr_list_or_flush(argc-1, argv+1, 1);
823 bb_error_msg_and_die("unknown command %s", *argv);