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