blob: 12b8fc8f8cdef44386c41f6e080c8fa762555182 [file] [log] [blame]
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00001/*
2 * ipaddress.c "ip address".
3 *
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +00004 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00005 *
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
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012#include <sys/socket.h>
13#include <sys/ioctl.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000014
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include <fnmatch.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000016#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <arpa/inet.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021
Eric Andersen8004bb72003-01-14 08:06:07 +000022#include <net/if.h>
23#include <net/if_arp.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024
25#include "rt_names.h"
26#include "utils.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000027#include "ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000028
Glenn L McGrath275be872002-12-16 07:37:21 +000029#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000030
31static struct
32{
33 int ifindex;
34 int family;
35 int oneline;
36 int showqueue;
37 inet_prefix pfx;
38 int scope, scopemask;
39 int flags, flagmask;
40 int up;
41 char *label;
Glenn L McGrathd66370c2003-01-13 21:40:38 +000042 int flushed;
43 char *flushb;
44 int flushp;
45 int flushe;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046 struct rtnl_handle *rth;
47} filter;
48
Eric Andersen14f5c8d2005-04-16 19:39:00 +000049static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000050{
51 fprintf(fp, "<");
52 flags &= ~IFF_RUNNING;
53#define _PF(f) if (flags&IFF_##f) { \
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000054 flags &= ~IFF_##f ; \
55 fprintf(fp, #f "%s", flags ? "," : ""); }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056 _PF(LOOPBACK);
57 _PF(BROADCAST);
58 _PF(POINTOPOINT);
59 _PF(MULTICAST);
60 _PF(NOARP);
61#if 0
62 _PF(ALLMULTI);
63 _PF(PROMISC);
64 _PF(MASTER);
65 _PF(SLAVE);
66 _PF(DEBUG);
67 _PF(DYNAMIC);
68 _PF(AUTOMEDIA);
69 _PF(PORTSEL);
70 _PF(NOTRAILERS);
71#endif
72 _PF(UP);
73#undef _PF
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000074 if (flags)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075 fprintf(fp, "%x", flags);
76 if (mdown)
77 fprintf(fp, ",M-DOWN");
78 fprintf(fp, "> ");
79}
80
Glenn L McGrath2626ef62002-12-02 01:40:05 +000081static void print_queuelen(char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082{
83 struct ifreq ifr;
84 int s;
85
86 s = socket(AF_INET, SOCK_STREAM, 0);
87 if (s < 0)
88 return;
89
90 memset(&ifr, 0, sizeof(ifr));
91 strcpy(ifr.ifr_name, name);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093 perror("SIOCGIFXQLEN");
94 close(s);
95 return;
96 }
97 close(s);
98
99 if (ifr.ifr_qlen)
100 printf("qlen %d", ifr.ifr_qlen);
101}
102
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000103static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
104 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105{
106 FILE *fp = (FILE*)arg;
107 struct ifinfomsg *ifi = NLMSG_DATA(n);
108 struct rtattr * tb[IFLA_MAX+1];
109 int len = n->nlmsg_len;
110 unsigned m_flag = 0;
111
112 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
113 return 0;
114
115 len -= NLMSG_LENGTH(sizeof(*ifi));
116 if (len < 0)
117 return -1;
118
119 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
120 return 0;
121 if (filter.up && !(ifi->ifi_flags&IFF_UP))
122 return 0;
123
124 memset(tb, 0, sizeof(tb));
125 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
126 if (tb[IFLA_IFNAME] == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 bb_error_msg("nil ifname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128 return -1;
129 }
130 if (filter.label &&
131 (!filter.family || filter.family == AF_PACKET) &&
132 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
133 return 0;
134
135 if (n->nlmsg_type == RTM_DELLINK)
136 fprintf(fp, "Deleted ");
137
138 fprintf(fp, "%d: %s", ifi->ifi_index,
139 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
140
141 if (tb[IFLA_LINK]) {
142 SPRINT_BUF(b1);
143 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
144 if (iflink == 0)
145 fprintf(fp, "@NONE: ");
146 else {
147 fprintf(fp, "@%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 fprintf(fp, ": ");
153 }
154 print_link_flags(fp, ifi->ifi_flags, m_flag);
155
156 if (tb[IFLA_MTU])
157 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
158 if (tb[IFLA_QDISC])
159 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
160#ifdef IFLA_MASTER
161 if (tb[IFLA_MASTER]) {
162 SPRINT_BUF(b1);
163 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
164 }
165#endif
166 if (filter.showqueue)
167 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000168
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169 if (!filter.family || filter.family == AF_PACKET) {
170 SPRINT_BUF(b1);
171 fprintf(fp, "%s", _SL_);
172 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
173
174 if (tb[IFLA_ADDRESS]) {
175 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
176 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
177 ifi->ifi_type,
178 b1, sizeof(b1)));
179 }
180 if (tb[IFLA_BROADCAST]) {
181 if (ifi->ifi_flags&IFF_POINTOPOINT)
182 fprintf(fp, " peer ");
183 else
184 fprintf(fp, " brd ");
185 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
186 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
187 ifi->ifi_type,
188 b1, sizeof(b1)));
189 }
190 }
191 fprintf(fp, "\n");
192 fflush(fp);
193 return 0;
194}
195
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000196static int flush_update(void)
197{
198 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
199 perror("Failed to send flush request\n");
200 return -1;
201 }
202 filter.flushp = 0;
203 return 0;
204}
205
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +0000206static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
207 struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208{
209 FILE *fp = (FILE*)arg;
210 struct ifaddrmsg *ifa = NLMSG_DATA(n);
211 int len = n->nlmsg_len;
212 struct rtattr * rta_tb[IFA_MAX+1];
213 char abuf[256];
214 SPRINT_BUF(b1);
215
216 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
217 return 0;
218 len -= NLMSG_LENGTH(sizeof(*ifa));
219 if (len < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 bb_error_msg("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221 return -1;
222 }
223
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000224 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
225 return 0;
226
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 memset(rta_tb, 0, sizeof(rta_tb));
228 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
229
230 if (!rta_tb[IFA_LOCAL])
231 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
232 if (!rta_tb[IFA_ADDRESS])
233 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
234
235 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
236 return 0;
237 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
238 return 0;
239 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
240 return 0;
241 if (filter.label) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000242 const char *label;
243 if (rta_tb[IFA_LABEL])
244 label = RTA_DATA(rta_tb[IFA_LABEL]);
245 else
246 label = ll_idx_n2a(ifa->ifa_index, b1);
247 if (fnmatch(filter.label, label, 0) != 0)
248 return 0;
249 }
250 if (filter.pfx.family) {
251 if (rta_tb[IFA_LOCAL]) {
252 inet_prefix dst;
253 memset(&dst, 0, sizeof(dst));
254 dst.family = ifa->ifa_family;
255 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
256 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
257 return 0;
258 }
259 }
260
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000261 if (filter.flushb) {
262 struct nlmsghdr *fn;
263 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
264 if (flush_update())
265 return -1;
266 }
267 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
268 memcpy(fn, n, n->nlmsg_len);
269 fn->nlmsg_type = RTM_DELADDR;
270 fn->nlmsg_flags = NLM_F_REQUEST;
271 fn->nlmsg_seq = ++filter.rth->seq;
272 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
273 filter.flushed++;
274 return 0;
275 }
276
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 if (n->nlmsg_type == RTM_DELADDR)
278 fprintf(fp, "Deleted ");
279
280 if (filter.oneline)
281 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
282 if (ifa->ifa_family == AF_INET)
283 fprintf(fp, " inet ");
284 else if (ifa->ifa_family == AF_INET6)
285 fprintf(fp, " inet6 ");
286 else
287 fprintf(fp, " family %d ", ifa->ifa_family);
288
289 if (rta_tb[IFA_LOCAL]) {
290 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
291 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
292 RTA_DATA(rta_tb[IFA_LOCAL]),
293 abuf, sizeof(abuf)));
294
295 if (rta_tb[IFA_ADDRESS] == NULL ||
296 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
297 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
298 } else {
299 fprintf(fp, " peer %s/%d ",
300 rt_addr_n2a(ifa->ifa_family,
301 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
302 RTA_DATA(rta_tb[IFA_ADDRESS]),
303 abuf, sizeof(abuf)),
304 ifa->ifa_prefixlen);
305 }
306 }
307
308 if (rta_tb[IFA_BROADCAST]) {
309 fprintf(fp, "brd %s ",
310 rt_addr_n2a(ifa->ifa_family,
311 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
312 RTA_DATA(rta_tb[IFA_BROADCAST]),
313 abuf, sizeof(abuf)));
314 }
315 if (rta_tb[IFA_ANYCAST]) {
316 fprintf(fp, "any %s ",
317 rt_addr_n2a(ifa->ifa_family,
318 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
319 RTA_DATA(rta_tb[IFA_ANYCAST]),
320 abuf, sizeof(abuf)));
321 }
322 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
323 if (ifa->ifa_flags&IFA_F_SECONDARY) {
324 ifa->ifa_flags &= ~IFA_F_SECONDARY;
325 fprintf(fp, "secondary ");
326 }
327 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
328 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
329 fprintf(fp, "tentative ");
330 }
331 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
332 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
333 fprintf(fp, "deprecated ");
334 }
335 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
336 fprintf(fp, "dynamic ");
337 } else
338 ifa->ifa_flags &= ~IFA_F_PERMANENT;
339 if (ifa->ifa_flags)
340 fprintf(fp, "flags %02x ", ifa->ifa_flags);
341 if (rta_tb[IFA_LABEL])
342 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
343 if (rta_tb[IFA_CACHEINFO]) {
344 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
345 char buf[128];
346 fprintf(fp, "%s", _SL_);
347 if (ci->ifa_valid == 0xFFFFFFFFU)
348 sprintf(buf, "valid_lft forever");
349 else
350 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
351 if (ci->ifa_prefered == 0xFFFFFFFFU)
352 sprintf(buf+strlen(buf), " preferred_lft forever");
353 else
354 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
355 fprintf(fp, " %s", buf);
356 }
357 fprintf(fp, "\n");
358 fflush(fp);
359 return 0;
360}
361
362
363struct nlmsg_list
364{
365 struct nlmsg_list *next;
366 struct nlmsghdr h;
367};
368
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000369static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000370{
371 for ( ;ainfo ; ainfo = ainfo->next) {
372 struct nlmsghdr *n = &ainfo->h;
373 struct ifaddrmsg *ifa = NLMSG_DATA(n);
374
375 if (n->nlmsg_type != RTM_NEWADDR)
376 continue;
377
378 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
379 return -1;
380
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000381 if (ifa->ifa_index != ifindex ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000382 (filter.family && filter.family != ifa->ifa_family))
383 continue;
384
385 print_addrinfo(NULL, n, fp);
386 }
387 return 0;
388}
389
390
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000391static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392{
393 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
394 struct nlmsg_list *h;
395 struct nlmsg_list **lp;
396
397 h = malloc(n->nlmsg_len+sizeof(void*));
398 if (h == NULL)
399 return -1;
400
401 memcpy(&h->h, n, n->nlmsg_len);
402 h->next = NULL;
403
404 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
405 *lp = h;
406
407 ll_remember_index(who, n, NULL);
408 return 0;
409}
410
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000411static void ipaddr_reset_filter(int _oneline)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000412{
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000413 memset(&filter, 0, sizeof(filter));
414 filter.oneline = _oneline;
415}
416
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000417extern int ipaddr_list_or_flush(int argc, char **argv, int flush)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000418{
419 const char *option[] = { "to", "scope", "up", "label", "dev", 0 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000420 struct nlmsg_list *linfo = NULL;
421 struct nlmsg_list *ainfo = NULL;
422 struct nlmsg_list *l;
423 struct rtnl_handle rth;
424 char *filter_dev = NULL;
425 int no_link = 0;
426
427 ipaddr_reset_filter(oneline);
428 filter.showqueue = 1;
429
430 if (filter.family == AF_UNSPEC)
431 filter.family = preferred_family;
432
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000433 if (flush) {
434 if (argc <= 0) {
435 fprintf(stderr, "Flush requires arguments.\n");
436 return -1;
437 }
438 if (filter.family == AF_PACKET) {
439 fprintf(stderr, "Cannot flush link addresses.\n");
440 return -1;
441 }
442 }
443
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000444 while (argc > 0) {
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000445 const int option_num = compare_string_array(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000446 switch (option_num) {
447 case 0: /* to */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000448 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000449 get_prefix(&filter.pfx, *argv, filter.family);
450 if (filter.family == AF_UNSPEC) {
451 filter.family = filter.pfx.family;
452 }
453 break;
454 case 1: /* scope */
455 {
456 int scope = 0;
457 NEXT_ARG();
458 filter.scopemask = -1;
459 if (rtnl_rtscope_a2n(&scope, *argv)) {
460 if (strcmp(*argv, "all") != 0) {
461 invarg("invalid \"scope\"\n", *argv);
462 }
463 scope = RT_SCOPE_NOWHERE;
464 filter.scopemask = 0;
465 }
466 filter.scope = scope;
467 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000468 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000469 case 2: /* up */
470 filter.up = 1;
471 break;
472 case 3: /* label */
473 NEXT_ARG();
474 filter.label = *argv;
475 break;
476 case 4: /* dev */
477 NEXT_ARG();
478 default:
479 if (filter_dev) {
480 duparg2("dev", *argv);
481 }
482 filter_dev = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000483 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000484 argv++;
485 argc--;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000486 }
487
488 if (rtnl_open(&rth, 0) < 0)
489 exit(1);
490
491 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000492 bb_perror_msg_and_die("Cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000493 }
494
495 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000496 bb_error_msg_and_die("Dump terminated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000497 }
498
499 if (filter_dev) {
500 filter.ifindex = ll_name_to_index(filter_dev);
501 if (filter.ifindex <= 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000502 bb_error_msg("Device \"%s\" does not exist.", filter_dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000503 return -1;
504 }
505 }
506
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000507 if (flush) {
508 int round = 0;
509 char flushb[4096-512];
510
511 filter.flushb = flushb;
512 filter.flushp = 0;
513 filter.flushe = sizeof(flushb);
514 filter.rth = &rth;
515
516 for (;;) {
517 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
518 perror("Cannot send dump request");
519 exit(1);
520 }
521 filter.flushed = 0;
522 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
523 fprintf(stderr, "Flush terminated\n");
524 exit(1);
525 }
526 if (filter.flushed == 0) {
Eric Andersen66a3af92003-01-27 17:41:19 +0000527#if 0
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000528 if (round == 0)
529 fprintf(stderr, "Nothing to flush.\n");
Eric Andersen66a3af92003-01-27 17:41:19 +0000530#endif
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000531 fflush(stdout);
532 return 0;
533 }
534 round++;
535 if (flush_update() < 0)
536 exit(1);
537 }
538 }
539
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000540 if (filter.family != AF_PACKET) {
541 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000542 bb_perror_msg_and_die("Cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000543 }
544
545 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000546 bb_error_msg_and_die("Dump terminated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000547 }
548 }
549
550
551 if (filter.family && filter.family != AF_PACKET) {
552 struct nlmsg_list **lp;
553 lp=&linfo;
554
555 if (filter.oneline)
556 no_link = 1;
557
558 while ((l=*lp)!=NULL) {
559 int ok = 0;
560 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
561 struct nlmsg_list *a;
562
563 for (a=ainfo; a; a=a->next) {
564 struct nlmsghdr *n = &a->h;
565 struct ifaddrmsg *ifa = NLMSG_DATA(n);
566
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000567 if (ifa->ifa_index != ifi->ifi_index ||
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000568 (filter.family && filter.family != ifa->ifa_family))
569 continue;
570 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
571 continue;
572 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
573 continue;
574 if (filter.pfx.family || filter.label) {
575 struct rtattr *tb[IFA_MAX+1];
576 memset(tb, 0, sizeof(tb));
577 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
578 if (!tb[IFA_LOCAL])
579 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
580
581 if (filter.pfx.family && tb[IFA_LOCAL]) {
582 inet_prefix dst;
583 memset(&dst, 0, sizeof(dst));
584 dst.family = ifa->ifa_family;
585 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
586 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
587 continue;
588 }
589 if (filter.label) {
590 SPRINT_BUF(b1);
591 const char *label;
592 if (tb[IFA_LABEL])
593 label = RTA_DATA(tb[IFA_LABEL]);
594 else
595 label = ll_idx_n2a(ifa->ifa_index, b1);
596 if (fnmatch(filter.label, label, 0) != 0)
597 continue;
598 }
599 }
600
601 ok = 1;
602 break;
603 }
604 if (!ok)
605 *lp = l->next;
606 else
607 lp = &l->next;
608 }
609 }
610
611 for (l=linfo; l; l = l->next) {
612 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
613 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
614 if (filter.family != AF_PACKET)
615 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
616 }
617 fflush(stdout);
618 }
619
620 exit(0);
621}
622
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000623static int default_scope(inet_prefix *lcl)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000624{
625 if (lcl->family == AF_INET) {
626 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
627 return RT_SCOPE_HOST;
628 }
629 return 0;
630}
631
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000632static int ipaddr_modify(int cmd, int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000633{
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000634 const char *option[] = { "peer", "remote", "broadcast", "brd",
635 "anycast", "scope", "dev", "label", "local", 0 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000636 struct rtnl_handle rth;
637 struct {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000638 struct nlmsghdr n;
639 struct ifaddrmsg ifa;
640 char buf[256];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000641 } req;
642 char *d = NULL;
643 char *l = NULL;
644 inet_prefix lcl;
645 inet_prefix peer;
646 int local_len = 0;
647 int peer_len = 0;
648 int brd_len = 0;
649 int any_len = 0;
650 int scoped = 0;
651
652 memset(&req, 0, sizeof(req));
653
654 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
655 req.n.nlmsg_flags = NLM_F_REQUEST;
656 req.n.nlmsg_type = cmd;
657 req.ifa.ifa_family = preferred_family;
658
659 while (argc > 0) {
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000660 const int option_num = compare_string_array(option, *argv);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000661 switch (option_num) {
662 case 0: /* peer */
663 case 1: /* remote */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000664 NEXT_ARG();
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000665
666 if (peer_len) {
667 duparg("peer", *argv);
668 }
669 get_prefix(&peer, *argv, req.ifa.ifa_family);
670 peer_len = peer.bytelen;
671 if (req.ifa.ifa_family == AF_UNSPEC) {
672 req.ifa.ifa_family = peer.family;
673 }
674 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
675 req.ifa.ifa_prefixlen = peer.bitlen;
676 break;
677 case 2: /* broadcast */
678 case 3: /* brd */
679 {
680 inet_prefix addr;
681 NEXT_ARG();
682 if (brd_len) {
683 duparg("broadcast", *argv);
684 }
685 if (strcmp(*argv, "+") == 0) {
686 brd_len = -1;
687 }
688 else if (strcmp(*argv, "-") == 0) {
689 brd_len = -2;
690 } else {
691 get_addr(&addr, *argv, req.ifa.ifa_family);
692 if (req.ifa.ifa_family == AF_UNSPEC)
693 req.ifa.ifa_family = addr.family;
694 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
695 brd_len = addr.bytelen;
696 }
697 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000698 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000699 case 4: /* anycast */
700 {
701 inet_prefix addr;
702 NEXT_ARG();
703 if (any_len) {
704 duparg("anycast", *argv);
705 }
706 get_addr(&addr, *argv, req.ifa.ifa_family);
707 if (req.ifa.ifa_family == AF_UNSPEC) {
708 req.ifa.ifa_family = addr.family;
709 }
710 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
711 any_len = addr.bytelen;
712 break;
713 }
714 case 5: /* scope */
715 {
716 int scope = 0;
717 NEXT_ARG();
718 if (rtnl_rtscope_a2n(&scope, *argv)) {
719 invarg(*argv, "invalid scope value.");
720 }
721 req.ifa.ifa_scope = scope;
722 scoped = 1;
723 break;
724 }
725 case 6: /* dev */
726 NEXT_ARG();
727 d = *argv;
728 break;
729 case 7: /* label */
730 NEXT_ARG();
731 l = *argv;
732 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
733 break;
734 case 8: /* local */
735 NEXT_ARG();
736 default:
737 if (local_len) {
738 duparg2("local", *argv);
739 }
740 get_prefix(&lcl, *argv, req.ifa.ifa_family);
741 if (req.ifa.ifa_family == AF_UNSPEC) {
742 req.ifa.ifa_family = lcl.family;
743 }
744 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
745 local_len = lcl.bytelen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000746 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000747 argc--;
748 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000749 }
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000750
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000751 if (d == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000752 bb_error_msg("Not enough information: \"dev\" argument is required.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000753 return -1;
754 }
755 if (l && matches(d, l) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000756 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000757 }
758
759 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
760 peer = lcl;
761 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
762 }
763 if (req.ifa.ifa_prefixlen == 0)
764 req.ifa.ifa_prefixlen = lcl.bitlen;
765
766 if (brd_len < 0 && cmd != RTM_DELADDR) {
767 inet_prefix brd;
768 int i;
769 if (req.ifa.ifa_family != AF_INET) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000770 bb_error_msg("Broadcast can be set only for IPv4 addresses");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000771 return -1;
772 }
773 brd = peer;
774 if (brd.bitlen <= 30) {
775 for (i=31; i>=brd.bitlen; i--) {
776 if (brd_len == -1)
777 brd.data[0] |= htonl(1<<(31-i));
778 else
779 brd.data[0] &= ~htonl(1<<(31-i));
780 }
781 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
782 brd_len = brd.bytelen;
783 }
784 }
785 if (!scoped && cmd != RTM_DELADDR)
786 req.ifa.ifa_scope = default_scope(&lcl);
787
788 if (rtnl_open(&rth, 0) < 0)
789 exit(1);
790
791 ll_init_map(&rth);
792
793 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000794 bb_error_msg("Cannot find device \"%s\"", d);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000795 return -1;
796 }
797
798 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
799 exit(2);
800
801 exit(0);
802}
803
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000804extern int do_ipaddr(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000805{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000806 const char *commands[] = { "add", "delete", "list", "show", "lst", "flush", 0 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000807 int command_num = 2;
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000808
809 if (*argv) {
810 command_num = compare_string_array(commands, *argv);
811 }
812 switch (command_num) {
813 case 0: /* add */
814 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
815 case 1: /* delete */
816 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
817 case 2: /* list */
818 case 3: /* show */
819 case 4: /* lst */
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000820 return ipaddr_list_or_flush(argc-1, argv+1, 0);
821 case 5: /* flush */
822 return ipaddr_list_or_flush(argc-1, argv+1, 1);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000823 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000824 bb_error_msg_and_die("Unknown command %s", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000825}