source: MondoRescue/branches/stable/mindi-busybox/networking/libiproute/ipaddress.c@ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

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