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