blob: 1c89f4a7dbbfdfba015d417dd23a4e854a5829b6 [file] [log] [blame]
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00001/*
2 * ipaddress.c "ip address".
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
12 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
13 */
14
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include <sys/socket.h>
16#include <sys/ioctl.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000017
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018#include <fnmatch.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000019#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22
23#include <arpa/inet.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024
Eric Andersen8004bb72003-01-14 08:06:07 +000025#include <net/if.h>
26#include <net/if_arp.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027
28#include "rt_names.h"
29#include "utils.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000030#include "ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000031
Glenn L McGrath275be872002-12-16 07:37:21 +000032#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000033
34static struct
35{
36 int ifindex;
37 int family;
38 int oneline;
39 int showqueue;
40 inet_prefix pfx;
41 int scope, scopemask;
42 int flags, flagmask;
43 int up;
44 char *label;
Glenn L McGrathd66370c2003-01-13 21:40:38 +000045 int flushed;
46 char *flushb;
47 int flushp;
48 int flushe;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049 struct rtnl_handle *rth;
50} filter;
51
Eric Andersen14f5c8d2005-04-16 19:39:00 +000052static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053{
54 fprintf(fp, "<");
55 flags &= ~IFF_RUNNING;
56#define _PF(f) if (flags&IFF_##f) { \
57 flags &= ~IFF_##f ; \
58 fprintf(fp, #f "%s", flags ? "," : ""); }
59 _PF(LOOPBACK);
60 _PF(BROADCAST);
61 _PF(POINTOPOINT);
62 _PF(MULTICAST);
63 _PF(NOARP);
64#if 0
65 _PF(ALLMULTI);
66 _PF(PROMISC);
67 _PF(MASTER);
68 _PF(SLAVE);
69 _PF(DEBUG);
70 _PF(DYNAMIC);
71 _PF(AUTOMEDIA);
72 _PF(PORTSEL);
73 _PF(NOTRAILERS);
74#endif
75 _PF(UP);
76#undef _PF
77 if (flags)
78 fprintf(fp, "%x", flags);
79 if (mdown)
80 fprintf(fp, ",M-DOWN");
81 fprintf(fp, "> ");
82}
83
Glenn L McGrath2626ef62002-12-02 01:40:05 +000084static void print_queuelen(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085{
86 struct ifreq ifr;
87 int s;
88
89 s = socket(AF_INET, SOCK_STREAM, 0);
90 if (s < 0)
91 return;
92
93 memset(&ifr, 0, sizeof(ifr));
94 strcpy(ifr.ifr_name, name);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000095 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000096 perror("SIOCGIFXQLEN");
97 close(s);
98 return;
99 }
100 close(s);
101
102 if (ifr.ifr_qlen)
103 printf("qlen %d", ifr.ifr_qlen);
104}
105
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000106static int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107{
108 FILE *fp = (FILE*)arg;
109 struct ifinfomsg *ifi = NLMSG_DATA(n);
110 struct rtattr * tb[IFLA_MAX+1];
111 int len = n->nlmsg_len;
112 unsigned m_flag = 0;
113
114 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
115 return 0;
116
117 len -= NLMSG_LENGTH(sizeof(*ifi));
118 if (len < 0)
119 return -1;
120
121 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
122 return 0;
123 if (filter.up && !(ifi->ifi_flags&IFF_UP))
124 return 0;
125
126 memset(tb, 0, sizeof(tb));
127 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
128 if (tb[IFLA_IFNAME] == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_error_msg("nil ifname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130 return -1;
131 }
132 if (filter.label &&
133 (!filter.family || filter.family == AF_PACKET) &&
134 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
135 return 0;
136
137 if (n->nlmsg_type == RTM_DELLINK)
138 fprintf(fp, "Deleted ");
139
140 fprintf(fp, "%d: %s", ifi->ifi_index,
141 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
142
143 if (tb[IFLA_LINK]) {
144 SPRINT_BUF(b1);
145 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
146 if (iflink == 0)
147 fprintf(fp, "@NONE: ");
148 else {
149 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
150 m_flag = ll_index_to_flags(iflink);
151 m_flag = !(m_flag & IFF_UP);
152 }
153 } else {
154 fprintf(fp, ": ");
155 }
156 print_link_flags(fp, ifi->ifi_flags, m_flag);
157
158 if (tb[IFLA_MTU])
159 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
160 if (tb[IFLA_QDISC])
161 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
162#ifdef IFLA_MASTER
163 if (tb[IFLA_MASTER]) {
164 SPRINT_BUF(b1);
165 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
166 }
167#endif
168 if (filter.showqueue)
169 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000170
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000171 if (!filter.family || filter.family == AF_PACKET) {
172 SPRINT_BUF(b1);
173 fprintf(fp, "%s", _SL_);
174 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
175
176 if (tb[IFLA_ADDRESS]) {
177 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
178 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
179 ifi->ifi_type,
180 b1, sizeof(b1)));
181 }
182 if (tb[IFLA_BROADCAST]) {
183 if (ifi->ifi_flags&IFF_POINTOPOINT)
184 fprintf(fp, " peer ");
185 else
186 fprintf(fp, " brd ");
187 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
188 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
189 ifi->ifi_type,
190 b1, sizeof(b1)));
191 }
192 }
193 fprintf(fp, "\n");
194 fflush(fp);
195 return 0;
196}
197
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000198static int flush_update(void)
199{
200 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
201 perror("Failed to send flush request\n");
202 return -1;
203 }
204 filter.flushp = 0;
205 return 0;
206}
207
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000208static int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209{
210 FILE *fp = (FILE*)arg;
211 struct ifaddrmsg *ifa = NLMSG_DATA(n);
212 int len = n->nlmsg_len;
213 struct rtattr * rta_tb[IFA_MAX+1];
214 char abuf[256];
215 SPRINT_BUF(b1);
216
217 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
218 return 0;
219 len -= NLMSG_LENGTH(sizeof(*ifa));
220 if (len < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 bb_error_msg("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222 return -1;
223 }
224
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000225 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
226 return 0;
227
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 memset(rta_tb, 0, sizeof(rta_tb));
229 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
230
231 if (!rta_tb[IFA_LOCAL])
232 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
233 if (!rta_tb[IFA_ADDRESS])
234 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
235
236 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
237 return 0;
238 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
239 return 0;
240 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
241 return 0;
242 if (filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 const char *label;
244 if (rta_tb[IFA_LABEL])
245 label = RTA_DATA(rta_tb[IFA_LABEL]);
246 else
247 label = ll_idx_n2a(ifa->ifa_index, b1);
248 if (fnmatch(filter.label, label, 0) != 0)
249 return 0;
250 }
251 if (filter.pfx.family) {
252 if (rta_tb[IFA_LOCAL]) {
253 inet_prefix dst;
254 memset(&dst, 0, sizeof(dst));
255 dst.family = ifa->ifa_family;
256 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
257 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
258 return 0;
259 }
260 }
261
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000262 if (filter.flushb) {
263 struct nlmsghdr *fn;
264 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
265 if (flush_update())
266 return -1;
267 }
268 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
269 memcpy(fn, n, n->nlmsg_len);
270 fn->nlmsg_type = RTM_DELADDR;
271 fn->nlmsg_flags = NLM_F_REQUEST;
272 fn->nlmsg_seq = ++filter.rth->seq;
273 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
274 filter.flushed++;
275 return 0;
276 }
277
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 if (n->nlmsg_type == RTM_DELADDR)
279 fprintf(fp, "Deleted ");
280
281 if (filter.oneline)
282 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
283 if (ifa->ifa_family == AF_INET)
284 fprintf(fp, " inet ");
285 else if (ifa->ifa_family == AF_INET6)
286 fprintf(fp, " inet6 ");
287 else
288 fprintf(fp, " family %d ", ifa->ifa_family);
289
290 if (rta_tb[IFA_LOCAL]) {
291 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
292 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
293 RTA_DATA(rta_tb[IFA_LOCAL]),
294 abuf, sizeof(abuf)));
295
296 if (rta_tb[IFA_ADDRESS] == NULL ||
297 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
298 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
299 } else {
300 fprintf(fp, " peer %s/%d ",
301 rt_addr_n2a(ifa->ifa_family,
302 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
303 RTA_DATA(rta_tb[IFA_ADDRESS]),
304 abuf, sizeof(abuf)),
305 ifa->ifa_prefixlen);
306 }
307 }
308
309 if (rta_tb[IFA_BROADCAST]) {
310 fprintf(fp, "brd %s ",
311 rt_addr_n2a(ifa->ifa_family,
312 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
313 RTA_DATA(rta_tb[IFA_BROADCAST]),
314 abuf, sizeof(abuf)));
315 }
316 if (rta_tb[IFA_ANYCAST]) {
317 fprintf(fp, "any %s ",
318 rt_addr_n2a(ifa->ifa_family,
319 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
320 RTA_DATA(rta_tb[IFA_ANYCAST]),
321 abuf, sizeof(abuf)));
322 }
323 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
324 if (ifa->ifa_flags&IFA_F_SECONDARY) {
325 ifa->ifa_flags &= ~IFA_F_SECONDARY;
326 fprintf(fp, "secondary ");
327 }
328 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
329 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
330 fprintf(fp, "tentative ");
331 }
332 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
333 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
334 fprintf(fp, "deprecated ");
335 }
336 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
337 fprintf(fp, "dynamic ");
338 } else
339 ifa->ifa_flags &= ~IFA_F_PERMANENT;
340 if (ifa->ifa_flags)
341 fprintf(fp, "flags %02x ", ifa->ifa_flags);
342 if (rta_tb[IFA_LABEL])
343 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
344 if (rta_tb[IFA_CACHEINFO]) {
345 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
346 char buf[128];
347 fprintf(fp, "%s", _SL_);
348 if (ci->ifa_valid == 0xFFFFFFFFU)
349 sprintf(buf, "valid_lft forever");
350 else
351 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
352 if (ci->ifa_prefered == 0xFFFFFFFFU)
353 sprintf(buf+strlen(buf), " preferred_lft forever");
354 else
355 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
356 fprintf(fp, " %s", buf);
357 }
358 fprintf(fp, "\n");
359 fflush(fp);
360 return 0;
361}
362
363
364struct nlmsg_list
365{
366 struct nlmsg_list *next;
367 struct nlmsghdr h;
368};
369
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000370static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371{
372 for ( ;ainfo ; ainfo = ainfo->next) {
373 struct nlmsghdr *n = &ainfo->h;
374 struct ifaddrmsg *ifa = NLMSG_DATA(n);
375
376 if (n->nlmsg_type != RTM_NEWADDR)
377 continue;
378
379 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
380 return -1;
381
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000382 if (ifa->ifa_index != ifindex ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383 (filter.family && filter.family != ifa->ifa_family))
384 continue;
385
386 print_addrinfo(NULL, n, fp);
387 }
388 return 0;
389}
390
391
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000392static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000393{
394 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
395 struct nlmsg_list *h;
396 struct nlmsg_list **lp;
397
398 h = malloc(n->nlmsg_len+sizeof(void*));
399 if (h == NULL)
400 return -1;
401
402 memcpy(&h->h, n, n->nlmsg_len);
403 h->next = NULL;
404
405 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
406 *lp = h;
407
408 ll_remember_index(who, n, NULL);
409 return 0;
410}
411
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000412static void ipaddr_reset_filter(int _oneline)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413{
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000414 memset(&filter, 0, sizeof(filter));
415 filter.oneline = _oneline;
416}
417
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000418extern int ipaddr_list_or_flush(int argc, char **argv, int flush)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000419{
420 const char *option[] = { "to", "scope", "up", "label", "dev", 0 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000421 struct nlmsg_list *linfo = NULL;
422 struct nlmsg_list *ainfo = NULL;
423 struct nlmsg_list *l;
424 struct rtnl_handle rth;
425 char *filter_dev = NULL;
426 int no_link = 0;
427
428 ipaddr_reset_filter(oneline);
429 filter.showqueue = 1;
430
431 if (filter.family == AF_UNSPEC)
432 filter.family = preferred_family;
433
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000434 if (flush) {
435 if (argc <= 0) {
436 fprintf(stderr, "Flush requires arguments.\n");
437 return -1;
438 }
439 if (filter.family == AF_PACKET) {
440 fprintf(stderr, "Cannot flush link addresses.\n");
441 return -1;
442 }
443 }
444
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000445 while (argc > 0) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000446 const unsigned short option_num = compare_string_array(option, *argv);
447 switch (option_num) {
448 case 0: /* to */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000449 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000450 get_prefix(&filter.pfx, *argv, filter.family);
451 if (filter.family == AF_UNSPEC) {
452 filter.family = filter.pfx.family;
453 }
454 break;
455 case 1: /* scope */
456 {
457 int scope = 0;
458 NEXT_ARG();
459 filter.scopemask = -1;
460 if (rtnl_rtscope_a2n(&scope, *argv)) {
461 if (strcmp(*argv, "all") != 0) {
462 invarg("invalid \"scope\"\n", *argv);
463 }
464 scope = RT_SCOPE_NOWHERE;
465 filter.scopemask = 0;
466 }
467 filter.scope = scope;
468 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000469 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000470 case 2: /* up */
471 filter.up = 1;
472 break;
473 case 3: /* label */
474 NEXT_ARG();
475 filter.label = *argv;
476 break;
477 case 4: /* dev */
478 NEXT_ARG();
479 default:
480 if (filter_dev) {
481 duparg2("dev", *argv);
482 }
483 filter_dev = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000484 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000485 argv++;
486 argc--;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000487 }
488
489 if (rtnl_open(&rth, 0) < 0)
490 exit(1);
491
492 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000493 bb_perror_msg_and_die("Cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000494 }
495
496 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000497 bb_error_msg_and_die("Dump terminated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000498 }
499
500 if (filter_dev) {
501 filter.ifindex = ll_name_to_index(filter_dev);
502 if (filter.ifindex <= 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000503 bb_error_msg("Device \"%s\" does not exist.", filter_dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000504 return -1;
505 }
506 }
507
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000508 if (flush) {
509 int round = 0;
510 char flushb[4096-512];
511
512 filter.flushb = flushb;
513 filter.flushp = 0;
514 filter.flushe = sizeof(flushb);
515 filter.rth = &rth;
516
517 for (;;) {
518 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
519 perror("Cannot send dump request");
520 exit(1);
521 }
522 filter.flushed = 0;
523 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
524 fprintf(stderr, "Flush terminated\n");
525 exit(1);
526 }
527 if (filter.flushed == 0) {
Eric Andersen66a3af92003-01-27 17:41:19 +0000528#if 0
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000529 if (round == 0)
530 fprintf(stderr, "Nothing to flush.\n");
Eric Andersen66a3af92003-01-27 17:41:19 +0000531#endif
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000532 fflush(stdout);
533 return 0;
534 }
535 round++;
536 if (flush_update() < 0)
537 exit(1);
538 }
539 }
540
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000541 if (filter.family != AF_PACKET) {
542 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000543 bb_perror_msg_and_die("Cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000544 }
545
546 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000547 bb_error_msg_and_die("Dump terminated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000548 }
549 }
550
551
552 if (filter.family && filter.family != AF_PACKET) {
553 struct nlmsg_list **lp;
554 lp=&linfo;
555
556 if (filter.oneline)
557 no_link = 1;
558
559 while ((l=*lp)!=NULL) {
560 int ok = 0;
561 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
562 struct nlmsg_list *a;
563
564 for (a=ainfo; a; a=a->next) {
565 struct nlmsghdr *n = &a->h;
566 struct ifaddrmsg *ifa = NLMSG_DATA(n);
567
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000568 if (ifa->ifa_index != ifi->ifi_index ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000569 (filter.family && filter.family != ifa->ifa_family))
570 continue;
571 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
572 continue;
573 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
574 continue;
575 if (filter.pfx.family || filter.label) {
576 struct rtattr *tb[IFA_MAX+1];
577 memset(tb, 0, sizeof(tb));
578 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
579 if (!tb[IFA_LOCAL])
580 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
581
582 if (filter.pfx.family && tb[IFA_LOCAL]) {
583 inet_prefix dst;
584 memset(&dst, 0, sizeof(dst));
585 dst.family = ifa->ifa_family;
586 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
587 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
588 continue;
589 }
590 if (filter.label) {
591 SPRINT_BUF(b1);
592 const char *label;
593 if (tb[IFA_LABEL])
594 label = RTA_DATA(tb[IFA_LABEL]);
595 else
596 label = ll_idx_n2a(ifa->ifa_index, b1);
597 if (fnmatch(filter.label, label, 0) != 0)
598 continue;
599 }
600 }
601
602 ok = 1;
603 break;
604 }
605 if (!ok)
606 *lp = l->next;
607 else
608 lp = &l->next;
609 }
610 }
611
612 for (l=linfo; l; l = l->next) {
613 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
614 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
615 if (filter.family != AF_PACKET)
616 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
617 }
618 fflush(stdout);
619 }
620
621 exit(0);
622}
623
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000624static int default_scope(inet_prefix *lcl)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000625{
626 if (lcl->family == AF_INET) {
627 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
628 return RT_SCOPE_HOST;
629 }
630 return 0;
631}
632
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000633static int ipaddr_modify(int cmd, int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000634{
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000635 const char *option[] = { "peer", "remote", "broadcast", "brd",
636 "anycast", "scope", "dev", "label", "local", 0 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000637 struct rtnl_handle rth;
638 struct {
639 struct nlmsghdr n;
640 struct ifaddrmsg ifa;
641 char buf[256];
642 } req;
643 char *d = NULL;
644 char *l = NULL;
645 inet_prefix lcl;
646 inet_prefix peer;
647 int local_len = 0;
648 int peer_len = 0;
649 int brd_len = 0;
650 int any_len = 0;
651 int scoped = 0;
652
653 memset(&req, 0, sizeof(req));
654
655 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
656 req.n.nlmsg_flags = NLM_F_REQUEST;
657 req.n.nlmsg_type = cmd;
658 req.ifa.ifa_family = preferred_family;
659
660 while (argc > 0) {
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000661 const unsigned short option_num = compare_string_array(option, *argv);
662 switch (option_num) {
663 case 0: /* peer */
664 case 1: /* remote */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000665 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000666
667 if (peer_len) {
668 duparg("peer", *argv);
669 }
670 get_prefix(&peer, *argv, req.ifa.ifa_family);
671 peer_len = peer.bytelen;
672 if (req.ifa.ifa_family == AF_UNSPEC) {
673 req.ifa.ifa_family = peer.family;
674 }
675 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
676 req.ifa.ifa_prefixlen = peer.bitlen;
677 break;
678 case 2: /* broadcast */
679 case 3: /* brd */
680 {
681 inet_prefix addr;
682 NEXT_ARG();
683 if (brd_len) {
684 duparg("broadcast", *argv);
685 }
686 if (strcmp(*argv, "+") == 0) {
687 brd_len = -1;
688 }
689 else if (strcmp(*argv, "-") == 0) {
690 brd_len = -2;
691 } else {
692 get_addr(&addr, *argv, req.ifa.ifa_family);
693 if (req.ifa.ifa_family == AF_UNSPEC)
694 req.ifa.ifa_family = addr.family;
695 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
696 brd_len = addr.bytelen;
697 }
698 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000699 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000700 case 4: /* anycast */
701 {
702 inet_prefix addr;
703 NEXT_ARG();
704 if (any_len) {
705 duparg("anycast", *argv);
706 }
707 get_addr(&addr, *argv, req.ifa.ifa_family);
708 if (req.ifa.ifa_family == AF_UNSPEC) {
709 req.ifa.ifa_family = addr.family;
710 }
711 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
712 any_len = addr.bytelen;
713 break;
714 }
715 case 5: /* scope */
716 {
717 int scope = 0;
718 NEXT_ARG();
719 if (rtnl_rtscope_a2n(&scope, *argv)) {
720 invarg(*argv, "invalid scope value.");
721 }
722 req.ifa.ifa_scope = scope;
723 scoped = 1;
724 break;
725 }
726 case 6: /* dev */
727 NEXT_ARG();
728 d = *argv;
729 break;
730 case 7: /* label */
731 NEXT_ARG();
732 l = *argv;
733 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
734 break;
735 case 8: /* local */
736 NEXT_ARG();
737 default:
738 if (local_len) {
739 duparg2("local", *argv);
740 }
741 get_prefix(&lcl, *argv, req.ifa.ifa_family);
742 if (req.ifa.ifa_family == AF_UNSPEC) {
743 req.ifa.ifa_family = lcl.family;
744 }
745 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
746 local_len = lcl.bytelen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000747 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000748 argc--;
749 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000750 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000751
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000752 if (d == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000753 bb_error_msg("Not enough information: \"dev\" argument is required.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000754 return -1;
755 }
756 if (l && matches(d, l) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000757 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000758 }
759
760 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
761 peer = lcl;
762 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
763 }
764 if (req.ifa.ifa_prefixlen == 0)
765 req.ifa.ifa_prefixlen = lcl.bitlen;
766
767 if (brd_len < 0 && cmd != RTM_DELADDR) {
768 inet_prefix brd;
769 int i;
770 if (req.ifa.ifa_family != AF_INET) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000771 bb_error_msg("Broadcast can be set only for IPv4 addresses");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000772 return -1;
773 }
774 brd = peer;
775 if (brd.bitlen <= 30) {
776 for (i=31; i>=brd.bitlen; i--) {
777 if (brd_len == -1)
778 brd.data[0] |= htonl(1<<(31-i));
779 else
780 brd.data[0] &= ~htonl(1<<(31-i));
781 }
782 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
783 brd_len = brd.bytelen;
784 }
785 }
786 if (!scoped && cmd != RTM_DELADDR)
787 req.ifa.ifa_scope = default_scope(&lcl);
788
789 if (rtnl_open(&rth, 0) < 0)
790 exit(1);
791
792 ll_init_map(&rth);
793
794 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000795 bb_error_msg("Cannot find device \"%s\"", d);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000796 return -1;
797 }
798
799 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
800 exit(2);
801
802 exit(0);
803}
804
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000805extern int do_ipaddr(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000806{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000807 const char *commands[] = { "add", "delete", "list", "show", "lst", "flush", 0 };
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000808 unsigned short command_num = 2;
809
810 if (*argv) {
811 command_num = compare_string_array(commands, *argv);
812 }
813 switch (command_num) {
814 case 0: /* add */
815 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
816 case 1: /* delete */
817 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
818 case 2: /* list */
819 case 3: /* show */
820 case 4: /* lst */
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000821 return ipaddr_list_or_flush(argc-1, argv+1, 0);
822 case 5: /* flush */
823 return ipaddr_list_or_flush(argc-1, argv+1, 1);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000824 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000825 bb_error_msg_and_die("Unknown command %s", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000826}