as suggested by Renaud Cerrato and Souf, switch over to recursive_action() for some...
[oweals/busybox.git] / networking / libiproute / ipaddress.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ipaddress.c          "ip address".
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6  *
7  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8  *
9  * Changes:
10  *      Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
11  */
12
13 //#include <sys/socket.h>
14 //#include <sys/ioctl.h>
15 #include <fnmatch.h>
16 #include <net/if.h>
17 #include <net/if_arp.h>
18
19 #include "ip_common.h"  /* #include "libbb.h" is inside */
20 #include "rt_names.h"
21 #include "utils.h"
22
23
24 typedef struct filter_t {
25         int ifindex;
26         int family;
27         int oneline;
28         int showqueue;
29         inet_prefix pfx;
30         int scope, scopemask;
31         int flags, flagmask;
32         int up;
33         char *label;
34         int flushed;
35         char *flushb;
36         int flushp;
37         int flushe;
38         struct rtnl_handle *rth;
39 } filter_t;
40
41 #define filter (*(filter_t*)&bb_common_bufsiz1)
42
43
44 static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
45 {
46         fprintf(fp, "<");
47         flags &= ~IFF_RUNNING;
48 #define _PF(f) if (flags&IFF_##f) { \
49                   flags &= ~IFF_##f ; \
50                   fprintf(fp, #f "%s", flags ? "," : ""); }
51         _PF(LOOPBACK);
52         _PF(BROADCAST);
53         _PF(POINTOPOINT);
54         _PF(MULTICAST);
55         _PF(NOARP);
56 #if 0
57         _PF(ALLMULTI);
58         _PF(PROMISC);
59         _PF(MASTER);
60         _PF(SLAVE);
61         _PF(DEBUG);
62         _PF(DYNAMIC);
63         _PF(AUTOMEDIA);
64         _PF(PORTSEL);
65         _PF(NOTRAILERS);
66 #endif
67         _PF(UP);
68 #undef _PF
69         if (flags)
70                 fprintf(fp, "%x", flags);
71         if (mdown)
72                 fprintf(fp, ",M-DOWN");
73         fprintf(fp, "> ");
74 }
75
76 static void print_queuelen(char *name)
77 {
78         struct ifreq ifr;
79         int s;
80
81         s = socket(AF_INET, SOCK_STREAM, 0);
82         if (s < 0)
83                 return;
84
85         memset(&ifr, 0, sizeof(ifr));
86         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
87         if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
88                 bb_perror_msg("SIOCGIFXQLEN");
89                 close(s);
90                 return;
91         }
92         close(s);
93
94         if (ifr.ifr_qlen)
95                 printf("qlen %d", ifr.ifr_qlen);
96 }
97
98 static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
99                 const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
100 {
101         FILE *fp = (FILE*)arg;
102         struct ifinfomsg *ifi = NLMSG_DATA(n);
103         struct rtattr * tb[IFLA_MAX+1];
104         int len = n->nlmsg_len;
105         unsigned m_flag = 0;
106
107         if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
108                 return 0;
109
110         len -= NLMSG_LENGTH(sizeof(*ifi));
111         if (len < 0)
112                 return -1;
113
114         if (filter.ifindex && ifi->ifi_index != filter.ifindex)
115                 return 0;
116         if (filter.up && !(ifi->ifi_flags&IFF_UP))
117                 return 0;
118
119         memset(tb, 0, sizeof(tb));
120         parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
121         if (tb[IFLA_IFNAME] == NULL) {
122                 bb_error_msg("nil ifname");
123                 return -1;
124         }
125         if (filter.label
126          && (!filter.family || filter.family == AF_PACKET)
127          && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
128         ) {
129                 return 0;
130         }
131
132         if (n->nlmsg_type == RTM_DELLINK)
133                 fprintf(fp, "Deleted ");
134
135         fprintf(fp, "%d: %s", ifi->ifi_index,
136                 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
137
138         if (tb[IFLA_LINK]) {
139                 SPRINT_BUF(b1);
140                 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
141                 if (iflink == 0)
142                         fprintf(fp, "@NONE: ");
143                 else {
144                         fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
145                         m_flag = ll_index_to_flags(iflink);
146                         m_flag = !(m_flag & IFF_UP);
147                 }
148         } else {
149                 fprintf(fp, ": ");
150         }
151         print_link_flags(fp, ifi->ifi_flags, m_flag);
152
153         if (tb[IFLA_MTU])
154                 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
155         if (tb[IFLA_QDISC])
156                 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
157 #ifdef IFLA_MASTER
158         if (tb[IFLA_MASTER]) {
159                 SPRINT_BUF(b1);
160                 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
161         }
162 #endif
163         if (filter.showqueue)
164                 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
165
166         if (!filter.family || filter.family == AF_PACKET) {
167                 SPRINT_BUF(b1);
168                 fprintf(fp, "%c    link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
169
170                 if (tb[IFLA_ADDRESS]) {
171                         fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
172                                                       RTA_PAYLOAD(tb[IFLA_ADDRESS]),
173                                                       ifi->ifi_type,
174                                                       b1, sizeof(b1)));
175                 }
176                 if (tb[IFLA_BROADCAST]) {
177                         if (ifi->ifi_flags&IFF_POINTOPOINT)
178                                 fprintf(fp, " peer ");
179                         else
180                                 fprintf(fp, " brd ");
181                         fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
182                                                       RTA_PAYLOAD(tb[IFLA_BROADCAST]),
183                                                       ifi->ifi_type,
184                                                       b1, sizeof(b1)));
185                 }
186         }
187         fputc('\n', fp);
188         fflush(fp);
189         return 0;
190 }
191
192 static int flush_update(void)
193 {
194         if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
195                 bb_perror_msg("failed to send flush request");
196                 return -1;
197         }
198         filter.flushp = 0;
199         return 0;
200 }
201
202 static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
203                 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
204 {
205         FILE *fp = (FILE*)arg;
206         struct ifaddrmsg *ifa = NLMSG_DATA(n);
207         int len = n->nlmsg_len;
208         struct rtattr * rta_tb[IFA_MAX+1];
209         char abuf[256];
210         SPRINT_BUF(b1);
211
212         if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
213                 return 0;
214         len -= NLMSG_LENGTH(sizeof(*ifa));
215         if (len < 0) {
216                 bb_error_msg("wrong nlmsg len %d", len);
217                 return -1;
218         }
219
220         if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
221                 return 0;
222
223         memset(rta_tb, 0, sizeof(rta_tb));
224         parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
225
226         if (!rta_tb[IFA_LOCAL])
227                 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
228         if (!rta_tb[IFA_ADDRESS])
229                 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
230
231         if (filter.ifindex && filter.ifindex != ifa->ifa_index)
232                 return 0;
233         if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
234                 return 0;
235         if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
236                 return 0;
237         if (filter.label) {
238                 const char *label;
239                 if (rta_tb[IFA_LABEL])
240                         label = RTA_DATA(rta_tb[IFA_LABEL]);
241                 else
242                         label = ll_idx_n2a(ifa->ifa_index, b1);
243                 if (fnmatch(filter.label, label, 0) != 0)
244                         return 0;
245         }
246         if (filter.pfx.family) {
247                 if (rta_tb[IFA_LOCAL]) {
248                         inet_prefix dst;
249                         memset(&dst, 0, sizeof(dst));
250                         dst.family = ifa->ifa_family;
251                         memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
252                         if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
253                                 return 0;
254                 }
255         }
256
257         if (filter.flushb) {
258                 struct nlmsghdr *fn;
259                 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
260                         if (flush_update())
261                                 return -1;
262                 }
263                 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
264                 memcpy(fn, n, n->nlmsg_len);
265                 fn->nlmsg_type = RTM_DELADDR;
266                 fn->nlmsg_flags = NLM_F_REQUEST;
267                 fn->nlmsg_seq = ++filter.rth->seq;
268                 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
269                 filter.flushed++;
270                 return 0;
271         }
272
273         if (n->nlmsg_type == RTM_DELADDR)
274                 fprintf(fp, "Deleted ");
275
276         if (filter.oneline)
277                 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
278         if (ifa->ifa_family == AF_INET)
279                 fprintf(fp, "    inet ");
280         else if (ifa->ifa_family == AF_INET6)
281                 fprintf(fp, "    inet6 ");
282         else
283                 fprintf(fp, "    family %d ", ifa->ifa_family);
284
285         if (rta_tb[IFA_LOCAL]) {
286                 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
287                                               RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
288                                               RTA_DATA(rta_tb[IFA_LOCAL]),
289                                               abuf, sizeof(abuf)));
290
291                 if (rta_tb[IFA_ADDRESS] == NULL ||
292                     memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
293                         fprintf(fp, "/%d ", ifa->ifa_prefixlen);
294                 } else {
295                         fprintf(fp, " peer %s/%d ",
296                                 rt_addr_n2a(ifa->ifa_family,
297                                             RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
298                                             RTA_DATA(rta_tb[IFA_ADDRESS]),
299                                             abuf, sizeof(abuf)),
300                                 ifa->ifa_prefixlen);
301                 }
302         }
303
304         if (rta_tb[IFA_BROADCAST]) {
305                 fprintf(fp, "brd %s ",
306                         rt_addr_n2a(ifa->ifa_family,
307                                     RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
308                                     RTA_DATA(rta_tb[IFA_BROADCAST]),
309                                     abuf, sizeof(abuf)));
310         }
311         if (rta_tb[IFA_ANYCAST]) {
312                 fprintf(fp, "any %s ",
313                         rt_addr_n2a(ifa->ifa_family,
314                                     RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
315                                     RTA_DATA(rta_tb[IFA_ANYCAST]),
316                                     abuf, sizeof(abuf)));
317         }
318         fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
319         if (ifa->ifa_flags&IFA_F_SECONDARY) {
320                 ifa->ifa_flags &= ~IFA_F_SECONDARY;
321                 fprintf(fp, "secondary ");
322         }
323         if (ifa->ifa_flags&IFA_F_TENTATIVE) {
324                 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
325                 fprintf(fp, "tentative ");
326         }
327         if (ifa->ifa_flags&IFA_F_DEPRECATED) {
328                 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
329                 fprintf(fp, "deprecated ");
330         }
331         if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
332                 fprintf(fp, "dynamic ");
333         } else
334                 ifa->ifa_flags &= ~IFA_F_PERMANENT;
335         if (ifa->ifa_flags)
336                 fprintf(fp, "flags %02x ", ifa->ifa_flags);
337         if (rta_tb[IFA_LABEL])
338                 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
339         if (rta_tb[IFA_CACHEINFO]) {
340                 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
341                 char buf[128];
342                 fputc(_SL_, fp);
343                 if (ci->ifa_valid == 0xFFFFFFFFU)
344                         sprintf(buf, "valid_lft forever");
345                 else
346                         sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
347                 if (ci->ifa_prefered == 0xFFFFFFFFU)
348                         sprintf(buf+strlen(buf), " preferred_lft forever");
349                 else
350                         sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
351                 fprintf(fp, "       %s", buf);
352         }
353         fputc('\n', fp);
354         fflush(fp);
355         return 0;
356 }
357
358
359 struct nlmsg_list
360 {
361         struct nlmsg_list *next;
362         struct nlmsghdr   h;
363 };
364
365 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
366 {
367         for (; ainfo; ainfo = ainfo->next) {
368                 struct nlmsghdr *n = &ainfo->h;
369                 struct ifaddrmsg *ifa = NLMSG_DATA(n);
370
371                 if (n->nlmsg_type != RTM_NEWADDR)
372                         continue;
373
374                 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
375                         return -1;
376
377                 if (ifa->ifa_index != ifindex ||
378                     (filter.family && filter.family != ifa->ifa_family))
379                         continue;
380
381                 print_addrinfo(NULL, n, fp);
382         }
383         return 0;
384 }
385
386
387 static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
388 {
389         struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
390         struct nlmsg_list *h;
391         struct nlmsg_list **lp;
392
393         h = malloc(n->nlmsg_len+sizeof(void*));
394         if (h == NULL)
395                 return -1;
396
397         memcpy(&h->h, n, n->nlmsg_len);
398         h->next = NULL;
399
400         for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
401         *lp = h;
402
403         ll_remember_index(who, n, NULL);
404         return 0;
405 }
406
407 static void ipaddr_reset_filter(int _oneline)
408 {
409         memset(&filter, 0, sizeof(filter));
410         filter.oneline = _oneline;
411 }
412
413 /* Return value becomes exitcode. It's okay to not return at all */
414 int ipaddr_list_or_flush(int argc, char **argv, int flush)
415 {
416         static const char *const option[] = { "to", "scope", "up", "label", "dev", 0 };
417
418         struct nlmsg_list *linfo = NULL;
419         struct nlmsg_list *ainfo = NULL;
420         struct nlmsg_list *l;
421         struct rtnl_handle rth;
422         char *filter_dev = NULL;
423         int no_link = 0;
424
425         ipaddr_reset_filter(oneline);
426         filter.showqueue = 1;
427
428         if (filter.family == AF_UNSPEC)
429                 filter.family = preferred_family;
430
431         if (flush) {
432                 if (argc <= 0) {
433                         bb_error_msg_and_die(bb_msg_requires_arg, "flush");
434                 }
435                 if (filter.family == AF_PACKET) {
436                         bb_error_msg_and_die("cannot flush link addresses");
437                 }
438         }
439
440         while (argc > 0) {
441                 const int option_num = index_in_str_array(option, *argv);
442                 switch (option_num) {
443                         case 0: /* to */
444                                 NEXT_ARG();
445                                 get_prefix(&filter.pfx, *argv, filter.family);
446                                 if (filter.family == AF_UNSPEC) {
447                                         filter.family = filter.pfx.family;
448                                 }
449                                 break;
450                         case 1: /* scope */
451                         {
452                                 uint32_t scope = 0;
453                                 NEXT_ARG();
454                                 filter.scopemask = -1;
455                                 if (rtnl_rtscope_a2n(&scope, *argv)) {
456                                         if (strcmp(*argv, "all") != 0) {
457                                                 invarg(*argv, "scope");
458                                         }
459                                         scope = RT_SCOPE_NOWHERE;
460                                         filter.scopemask = 0;
461                                 }
462                                 filter.scope = scope;
463                                 break;
464                         }
465                         case 2: /* up */
466                                 filter.up = 1;
467                                 break;
468                         case 3: /* label */
469                                 NEXT_ARG();
470                                 filter.label = *argv;
471                                 break;
472                         case 4: /* dev */
473                                 NEXT_ARG();
474                         default:
475                                 if (filter_dev) {
476                                         duparg2("dev", *argv);
477                                 }
478                                 filter_dev = *argv;
479                 }
480                 argv++;
481                 argc--;
482         }
483
484         xrtnl_open(&rth);
485
486         xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
487         xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
488
489         if (filter_dev) {
490                 filter.ifindex = xll_name_to_index(filter_dev);
491         }
492
493         if (flush) {
494                 char flushb[4096-512];
495
496                 filter.flushb = flushb;
497                 filter.flushp = 0;
498                 filter.flushe = sizeof(flushb);
499                 filter.rth = &rth;
500
501                 for (;;) {
502                         xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
503                         filter.flushed = 0;
504                         xrtnl_dump_filter(&rth, print_addrinfo, stdout);
505                         if (filter.flushed == 0) {
506                                 return 0;
507                         }
508                         if (flush_update() < 0)
509                                 return 1;
510                 }
511         }
512
513         if (filter.family != AF_PACKET) {
514                 xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
515                 xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
516         }
517
518
519         if (filter.family && filter.family != AF_PACKET) {
520                 struct nlmsg_list **lp;
521                 lp=&linfo;
522
523                 if (filter.oneline)
524                         no_link = 1;
525
526                 while ((l=*lp)!=NULL) {
527                         int ok = 0;
528                         struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
529                         struct nlmsg_list *a;
530
531                         for (a=ainfo; a; a=a->next) {
532                                 struct nlmsghdr *n = &a->h;
533                                 struct ifaddrmsg *ifa = NLMSG_DATA(n);
534
535                                 if (ifa->ifa_index != ifi->ifi_index ||
536                                     (filter.family && filter.family != ifa->ifa_family))
537                                         continue;
538                                 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
539                                         continue;
540                                 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
541                                         continue;
542                                 if (filter.pfx.family || filter.label) {
543                                         struct rtattr *tb[IFA_MAX+1];
544                                         memset(tb, 0, sizeof(tb));
545                                         parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
546                                         if (!tb[IFA_LOCAL])
547                                                 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
548
549                                         if (filter.pfx.family && tb[IFA_LOCAL]) {
550                                                 inet_prefix dst;
551                                                 memset(&dst, 0, sizeof(dst));
552                                                 dst.family = ifa->ifa_family;
553                                                 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
554                                                 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
555                                                         continue;
556                                         }
557                                         if (filter.label) {
558                                                 SPRINT_BUF(b1);
559                                                 const char *label;
560                                                 if (tb[IFA_LABEL])
561                                                         label = RTA_DATA(tb[IFA_LABEL]);
562                                                 else
563                                                         label = ll_idx_n2a(ifa->ifa_index, b1);
564                                                 if (fnmatch(filter.label, label, 0) != 0)
565                                                         continue;
566                                         }
567                                 }
568
569                                 ok = 1;
570                                 break;
571                         }
572                         if (!ok)
573                                 *lp = l->next;
574                         else
575                                 lp = &l->next;
576                 }
577         }
578
579         for (l = linfo; l; l = l->next) {
580                 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
581                         struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
582                         if (filter.family != AF_PACKET)
583                                 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
584                 }
585                 fflush(stdout); /* why? */
586         }
587
588         return 0;
589 }
590
591 static int default_scope(inet_prefix *lcl)
592 {
593         if (lcl->family == AF_INET) {
594                 if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
595                         return RT_SCOPE_HOST;
596         }
597         return 0;
598 }
599
600 /* Return value becomes exitcode. It's okay to not return at all */
601 static int ipaddr_modify(int cmd, int argc, char **argv)
602 {
603         static const char *const option[] = {
604                 "peer", "remote", "broadcast", "brd",
605                 "anycast", "scope", "dev", "label", "local", 0
606         };
607
608         struct rtnl_handle rth;
609         struct {
610                 struct nlmsghdr         n;
611                 struct ifaddrmsg        ifa;
612                 char                    buf[256];
613         } req;
614         char  *d = NULL;
615         char  *l = NULL;
616         inet_prefix lcl;
617         inet_prefix peer;
618         int local_len = 0;
619         int peer_len = 0;
620         int brd_len = 0;
621         int any_len = 0;
622         int scoped = 0;
623
624         memset(&req, 0, sizeof(req));
625
626         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
627         req.n.nlmsg_flags = NLM_F_REQUEST;
628         req.n.nlmsg_type = cmd;
629         req.ifa.ifa_family = preferred_family;
630
631         while (argc > 0) {
632                 const int option_num = index_in_str_array(option, *argv);
633                 switch (option_num) {
634                         case 0: /* peer */
635                         case 1: /* remote */
636                                 NEXT_ARG();
637
638                                 if (peer_len) {
639                                         duparg("peer", *argv);
640                                 }
641                                 get_prefix(&peer, *argv, req.ifa.ifa_family);
642                                 peer_len = peer.bytelen;
643                                 if (req.ifa.ifa_family == AF_UNSPEC) {
644                                         req.ifa.ifa_family = peer.family;
645                                 }
646                                 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
647                                 req.ifa.ifa_prefixlen = peer.bitlen;
648                                 break;
649                         case 2: /* broadcast */
650                         case 3: /* brd */
651                         {
652                                 inet_prefix addr;
653                                 NEXT_ARG();
654                                 if (brd_len) {
655                                         duparg("broadcast", *argv);
656                                 }
657                                 if (LONE_CHAR(*argv, '+')) {
658                                         brd_len = -1;
659                                 }
660                                 else if (LONE_DASH(*argv)) {
661                                         brd_len = -2;
662                                 } else {
663                                         get_addr(&addr, *argv, req.ifa.ifa_family);
664                                         if (req.ifa.ifa_family == AF_UNSPEC)
665                                                 req.ifa.ifa_family = addr.family;
666                                         addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
667                                         brd_len = addr.bytelen;
668                                 }
669                                 break;
670                         }
671                         case 4: /* anycast */
672                         {
673                                 inet_prefix addr;
674                                 NEXT_ARG();
675                                 if (any_len) {
676                                         duparg("anycast", *argv);
677                                 }
678                                 get_addr(&addr, *argv, req.ifa.ifa_family);
679                                 if (req.ifa.ifa_family == AF_UNSPEC) {
680                                         req.ifa.ifa_family = addr.family;
681                                 }
682                                 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
683                                 any_len = addr.bytelen;
684                                 break;
685                         }
686                         case 5: /* scope */
687                         {
688                                 uint32_t scope = 0;
689                                 NEXT_ARG();
690                                 if (rtnl_rtscope_a2n(&scope, *argv)) {
691                                         invarg(*argv, "scope");
692                                 }
693                                 req.ifa.ifa_scope = scope;
694                                 scoped = 1;
695                                 break;
696                         }
697                         case 6: /* dev */
698                                 NEXT_ARG();
699                                 d = *argv;
700                                 break;
701                         case 7: /* label */
702                                 NEXT_ARG();
703                                 l = *argv;
704                                 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
705                                 break;
706                         case 8: /* local */
707                                 NEXT_ARG();
708                         default:
709                                 if (local_len) {
710                                         duparg2("local", *argv);
711                                 }
712                                 get_prefix(&lcl, *argv, req.ifa.ifa_family);
713                                 if (req.ifa.ifa_family == AF_UNSPEC) {
714                                         req.ifa.ifa_family = lcl.family;
715                                 }
716                                 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
717                                 local_len = lcl.bytelen;
718                 }
719                 argc--;
720                 argv++;
721         }
722
723         if (d == NULL) {
724                 bb_error_msg(bb_msg_requires_arg,"\"dev\"");
725                 return -1;
726         }
727         if (l && matches(d, l) != 0) {
728                 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
729         }
730
731         if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
732                 peer = lcl;
733                 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
734         }
735         if (req.ifa.ifa_prefixlen == 0)
736                 req.ifa.ifa_prefixlen = lcl.bitlen;
737
738         if (brd_len < 0 && cmd != RTM_DELADDR) {
739                 inet_prefix brd;
740                 int i;
741                 if (req.ifa.ifa_family != AF_INET) {
742                         bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
743                 }
744                 brd = peer;
745                 if (brd.bitlen <= 30) {
746                         for (i=31; i>=brd.bitlen; i--) {
747                                 if (brd_len == -1)
748                                         brd.data[0] |= htonl(1<<(31-i));
749                                 else
750                                         brd.data[0] &= ~htonl(1<<(31-i));
751                         }
752                         addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
753                         brd_len = brd.bytelen;
754                 }
755         }
756         if (!scoped && cmd != RTM_DELADDR)
757                 req.ifa.ifa_scope = default_scope(&lcl);
758
759         xrtnl_open(&rth);
760
761         ll_init_map(&rth);
762
763         req.ifa.ifa_index = xll_name_to_index(d);
764
765         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
766                 return 2;
767
768         return 0;
769 }
770
771 /* Return value becomes exitcode. It's okay to not return at all */
772 int do_ipaddr(int argc, char **argv)
773 {
774         static const char *const commands[] = {
775                 "add", "delete", "list", "show", "lst", "flush", 0
776         };
777
778         int command_num = 2;
779
780         if (*argv) {
781                 command_num = index_in_substr_array(commands, *argv);
782         }
783         switch (command_num) {
784                 case 0: /* add */
785                         return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
786                 case 1: /* delete */
787                         return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
788                 case 2: /* list */
789                 case 3: /* show */
790                 case 4: /* lst */
791                         return ipaddr_list_or_flush(argc-1, argv+1, 0);
792                 case 5: /* flush */
793                         return ipaddr_list_or_flush(argc-1, argv+1, 1);
794         }
795         bb_error_msg_and_die("unknown command %s", *argv);
796 }