]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipaddress.c
gcc 2.95 compile fix.
[mirror_iproute2.git] / ip / ipaddress.c
CommitLineData
aba5acdf
SH
1/*
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
15#include <stdio.h>
16#include <stdlib.h>
17#include <unistd.h>
18#include <syslog.h>
19#include <fcntl.h>
20#include <sys/ioctl.h>
21#include <sys/socket.h>
22#include <sys/ioctl.h>
aba5acdf
SH
23#include <netinet/in.h>
24#include <arpa/inet.h>
25#include <string.h>
26#include <fnmatch.h>
27
e5779fb2
SH
28#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/sockios.h>
31
aba5acdf
SH
32#include "rt_names.h"
33#include "utils.h"
34#include "ll_map.h"
35#include "ip_common.h"
36
37static struct
38{
39 int ifindex;
40 int family;
41 int oneline;
42 int showqueue;
43 inet_prefix pfx;
44 int scope, scopemask;
45 int flags, flagmask;
46 int up;
47 char *label;
48 int flushed;
49 char *flushb;
50 int flushp;
51 int flushe;
52 struct rtnl_handle *rth;
53} filter;
54
55static int do_link;
56
57static void usage(void) __attribute__((noreturn));
58
59static void usage(void)
60{
61 if (do_link) {
62 iplink_usage();
63 }
64 fprintf(stderr, "Usage: ip addr {add|del} IFADDR dev STRING\n");
65 fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
66 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n");
67 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
68 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
69 fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n");
70 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
71 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
72 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
73 fprintf(stderr, " tentative | deprecated ]\n");
74 exit(-1);
75}
76
77void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
78{
79 fprintf(fp, "<");
80 flags &= ~IFF_RUNNING;
81#define _PF(f) if (flags&IFF_##f) { \
82 flags &= ~IFF_##f ; \
83 fprintf(fp, #f "%s", flags ? "," : ""); }
84 _PF(LOOPBACK);
85 _PF(BROADCAST);
86 _PF(POINTOPOINT);
87 _PF(MULTICAST);
88 _PF(NOARP);
89 _PF(ALLMULTI);
90 _PF(PROMISC);
91 _PF(MASTER);
92 _PF(SLAVE);
93 _PF(DEBUG);
94 _PF(DYNAMIC);
95 _PF(AUTOMEDIA);
96 _PF(PORTSEL);
97 _PF(NOTRAILERS);
98 _PF(UP);
99#undef _PF
100 if (flags)
101 fprintf(fp, "%x", flags);
102 if (mdown)
103 fprintf(fp, ",M-DOWN");
104 fprintf(fp, "> ");
105}
106
107void print_queuelen(char *name)
108{
109 struct ifreq ifr;
110 int s;
111
112 s = socket(AF_INET, SOCK_STREAM, 0);
113 if (s < 0)
114 return;
115
116 memset(&ifr, 0, sizeof(ifr));
117 strcpy(ifr.ifr_name, name);
118 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
119 perror("SIOCGIFXQLEN");
120 close(s);
121 return;
122 }
123 close(s);
124
125 if (ifr.ifr_qlen)
126 printf("qlen %d", ifr.ifr_qlen);
127}
128
50772dc5
SH
129int print_linkinfo(const struct sockaddr_nl *who,
130 struct nlmsghdr *n, void *arg)
aba5acdf
SH
131{
132 FILE *fp = (FILE*)arg;
133 struct ifinfomsg *ifi = NLMSG_DATA(n);
134 struct rtattr * tb[IFLA_MAX+1];
135 int len = n->nlmsg_len;
136 unsigned m_flag = 0;
137
138 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
139 return 0;
140
141 len -= NLMSG_LENGTH(sizeof(*ifi));
142 if (len < 0)
143 return -1;
144
145 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
146 return 0;
147 if (filter.up && !(ifi->ifi_flags&IFF_UP))
148 return 0;
149
aba5acdf
SH
150 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
151 if (tb[IFLA_IFNAME] == NULL) {
152 fprintf(stderr, "BUG: nil ifname\n");
153 return -1;
154 }
155 if (filter.label &&
156 (!filter.family || filter.family == AF_PACKET) &&
157 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
158 return 0;
159
160 if (n->nlmsg_type == RTM_DELLINK)
161 fprintf(fp, "Deleted ");
162
163 fprintf(fp, "%d: %s", ifi->ifi_index,
164 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
165
166 if (tb[IFLA_LINK]) {
167 SPRINT_BUF(b1);
168 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
169 if (iflink == 0)
170 fprintf(fp, "@NONE: ");
171 else {
172 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
173 m_flag = ll_index_to_flags(iflink);
174 m_flag = !(m_flag & IFF_UP);
175 }
176 } else {
177 fprintf(fp, ": ");
178 }
179 print_link_flags(fp, ifi->ifi_flags, m_flag);
180
181 if (tb[IFLA_MTU])
182 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
183 if (tb[IFLA_QDISC])
184 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
185#ifdef IFLA_MASTER
186 if (tb[IFLA_MASTER]) {
187 SPRINT_BUF(b1);
188 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
189 }
190#endif
191 if (filter.showqueue)
192 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
193
194 if (!filter.family || filter.family == AF_PACKET) {
195 SPRINT_BUF(b1);
196 fprintf(fp, "%s", _SL_);
197 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
198
199 if (tb[IFLA_ADDRESS]) {
200 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
201 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
202 ifi->ifi_type,
203 b1, sizeof(b1)));
204 }
205 if (tb[IFLA_BROADCAST]) {
206 if (ifi->ifi_flags&IFF_POINTOPOINT)
207 fprintf(fp, " peer ");
208 else
209 fprintf(fp, " brd ");
210 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
211 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
212 ifi->ifi_type,
213 b1, sizeof(b1)));
214 }
215 }
216 if (do_link && tb[IFLA_STATS] && show_stats) {
ae0f1443 217 struct rtnl_link_stats slocal;
218 struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]);
aba5acdf
SH
219 if (((unsigned long)s) & (sizeof(unsigned long)-1)) {
220 memcpy(&slocal, s, sizeof(slocal));
221 s = &slocal;
222 }
223 fprintf(fp, "%s", _SL_);
224 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
225 s->rx_compressed ? "compressed" : "", _SL_);
ae0f1443 226 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
aba5acdf
SH
227 s->rx_bytes, s->rx_packets, s->rx_errors,
228 s->rx_dropped, s->rx_over_errors,
229 s->multicast
230 );
231 if (s->rx_compressed)
ae0f1443 232 fprintf(fp, " %-7u", s->rx_compressed);
aba5acdf
SH
233 if (show_stats > 1) {
234 fprintf(fp, "%s", _SL_);
235 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
ae0f1443 236 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
aba5acdf
SH
237 s->rx_length_errors,
238 s->rx_crc_errors,
239 s->rx_frame_errors,
240 s->rx_fifo_errors,
241 s->rx_missed_errors
242 );
243 }
244 fprintf(fp, "%s", _SL_);
245 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
246 s->tx_compressed ? "compressed" : "", _SL_);
ae0f1443 247 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
aba5acdf
SH
248 s->tx_bytes, s->tx_packets, s->tx_errors,
249 s->tx_dropped, s->tx_carrier_errors, s->collisions);
250 if (s->tx_compressed)
ae0f1443 251 fprintf(fp, " %-7u", s->tx_compressed);
aba5acdf
SH
252 if (show_stats > 1) {
253 fprintf(fp, "%s", _SL_);
254 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
ae0f1443 255 fprintf(fp, " %-7u %-7u %-7u %-7u",
aba5acdf
SH
256 s->tx_aborted_errors,
257 s->tx_fifo_errors,
258 s->tx_window_errors,
259 s->tx_heartbeat_errors
260 );
261 }
262 }
263 fprintf(fp, "\n");
264 fflush(fp);
265 return 0;
266}
267
268static int flush_update(void)
269{
270 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
271 perror("Failed to send flush request\n");
272 return -1;
273 }
274 filter.flushp = 0;
275 return 0;
276}
277
50772dc5 278int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
6dc9f016 279 void *arg)
aba5acdf
SH
280{
281 FILE *fp = (FILE*)arg;
282 struct ifaddrmsg *ifa = NLMSG_DATA(n);
283 int len = n->nlmsg_len;
284 struct rtattr * rta_tb[IFA_MAX+1];
285 char abuf[256];
286 SPRINT_BUF(b1);
287
288 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
289 return 0;
290 len -= NLMSG_LENGTH(sizeof(*ifa));
291 if (len < 0) {
292 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
293 return -1;
294 }
295
296 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
297 return 0;
298
aba5acdf
SH
299 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
300
301 if (!rta_tb[IFA_LOCAL])
302 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
303 if (!rta_tb[IFA_ADDRESS])
304 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
305
306 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
307 return 0;
308 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
309 return 0;
310 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
311 return 0;
312 if (filter.label) {
313 SPRINT_BUF(b1);
314 const char *label;
315 if (rta_tb[IFA_LABEL])
316 label = RTA_DATA(rta_tb[IFA_LABEL]);
317 else
318 label = ll_idx_n2a(ifa->ifa_index, b1);
319 if (fnmatch(filter.label, label, 0) != 0)
320 return 0;
321 }
322 if (filter.pfx.family) {
323 if (rta_tb[IFA_LOCAL]) {
324 inet_prefix dst;
325 memset(&dst, 0, sizeof(dst));
326 dst.family = ifa->ifa_family;
327 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
328 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
329 return 0;
330 }
331 }
332
333 if (filter.flushb) {
334 struct nlmsghdr *fn;
335 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
336 if (flush_update())
337 return -1;
338 }
339 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
340 memcpy(fn, n, n->nlmsg_len);
341 fn->nlmsg_type = RTM_DELADDR;
342 fn->nlmsg_flags = NLM_F_REQUEST;
343 fn->nlmsg_seq = ++filter.rth->seq;
344 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
345 filter.flushed++;
346 if (show_stats < 2)
347 return 0;
348 }
349
350 if (n->nlmsg_type == RTM_DELADDR)
351 fprintf(fp, "Deleted ");
352
353 if (filter.oneline || filter.flushb)
354 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
355 if (ifa->ifa_family == AF_INET)
356 fprintf(fp, " inet ");
357 else if (ifa->ifa_family == AF_INET6)
358 fprintf(fp, " inet6 ");
359 else if (ifa->ifa_family == AF_DECnet)
360 fprintf(fp, " dnet ");
361 else if (ifa->ifa_family == AF_IPX)
362 fprintf(fp, " ipx ");
363 else
364 fprintf(fp, " family %d ", ifa->ifa_family);
365
366 if (rta_tb[IFA_LOCAL]) {
367 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
368 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
369 RTA_DATA(rta_tb[IFA_LOCAL]),
370 abuf, sizeof(abuf)));
371
372 if (rta_tb[IFA_ADDRESS] == NULL ||
373 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
374 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
375 } else {
376 fprintf(fp, " peer %s/%d ",
377 rt_addr_n2a(ifa->ifa_family,
378 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
379 RTA_DATA(rta_tb[IFA_ADDRESS]),
380 abuf, sizeof(abuf)),
381 ifa->ifa_prefixlen);
382 }
383 }
384
385 if (rta_tb[IFA_BROADCAST]) {
386 fprintf(fp, "brd %s ",
387 rt_addr_n2a(ifa->ifa_family,
388 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
389 RTA_DATA(rta_tb[IFA_BROADCAST]),
390 abuf, sizeof(abuf)));
391 }
392 if (rta_tb[IFA_ANYCAST]) {
393 fprintf(fp, "any %s ",
394 rt_addr_n2a(ifa->ifa_family,
395 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
396 RTA_DATA(rta_tb[IFA_ANYCAST]),
397 abuf, sizeof(abuf)));
398 }
399 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
400 if (ifa->ifa_flags&IFA_F_SECONDARY) {
401 ifa->ifa_flags &= ~IFA_F_SECONDARY;
402 fprintf(fp, "secondary ");
403 }
404 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
405 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
406 fprintf(fp, "tentative ");
407 }
408 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
409 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
410 fprintf(fp, "deprecated ");
411 }
412 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
413 fprintf(fp, "dynamic ");
414 } else
415 ifa->ifa_flags &= ~IFA_F_PERMANENT;
416 if (ifa->ifa_flags)
417 fprintf(fp, "flags %02x ", ifa->ifa_flags);
418 if (rta_tb[IFA_LABEL])
419 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
420 if (rta_tb[IFA_CACHEINFO]) {
421 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
422 char buf[128];
423 fprintf(fp, "%s", _SL_);
424 if (ci->ifa_valid == 0xFFFFFFFFU)
425 sprintf(buf, "valid_lft forever");
426 else
427 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
428 if (ci->ifa_prefered == 0xFFFFFFFFU)
429 sprintf(buf+strlen(buf), " preferred_lft forever");
430 else
431 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
432 fprintf(fp, " %s", buf);
433 }
434 fprintf(fp, "\n");
435 fflush(fp);
436 return 0;
437}
438
439
440struct nlmsg_list
441{
442 struct nlmsg_list *next;
443 struct nlmsghdr h;
444};
445
446int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
447{
448 for ( ;ainfo ; ainfo = ainfo->next) {
449 struct nlmsghdr *n = &ainfo->h;
450 struct ifaddrmsg *ifa = NLMSG_DATA(n);
451
452 if (n->nlmsg_type != RTM_NEWADDR)
453 continue;
454
455 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
456 return -1;
457
458 if (ifa->ifa_index != ifindex ||
459 (filter.family && filter.family != ifa->ifa_family))
460 continue;
461
462 print_addrinfo(NULL, n, fp);
463 }
464 return 0;
465}
466
467
50772dc5 468static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
6dc9f016 469 void *arg)
aba5acdf
SH
470{
471 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
472 struct nlmsg_list *h;
473 struct nlmsg_list **lp;
474
475 h = malloc(n->nlmsg_len+sizeof(void*));
476 if (h == NULL)
477 return -1;
478
479 memcpy(&h->h, n, n->nlmsg_len);
480 h->next = NULL;
481
482 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
483 *lp = h;
484
485 ll_remember_index(who, n, NULL);
486 return 0;
487}
488
489int ipaddr_list_or_flush(int argc, char **argv, int flush)
490{
491 struct nlmsg_list *linfo = NULL;
492 struct nlmsg_list *ainfo = NULL;
493 struct nlmsg_list *l;
494 struct rtnl_handle rth;
495 char *filter_dev = NULL;
496 int no_link = 0;
497
498 ipaddr_reset_filter(oneline);
499 filter.showqueue = 1;
500
501 if (filter.family == AF_UNSPEC)
502 filter.family = preferred_family;
503
504 if (flush) {
505 if (argc <= 0) {
506 fprintf(stderr, "Flush requires arguments.\n");
507 return -1;
508 }
509 if (filter.family == AF_PACKET) {
510 fprintf(stderr, "Cannot flush link addresses.\n");
511 return -1;
512 }
513 }
514
515 while (argc > 0) {
516 if (strcmp(*argv, "to") == 0) {
517 NEXT_ARG();
518 get_prefix(&filter.pfx, *argv, filter.family);
519 if (filter.family == AF_UNSPEC)
520 filter.family = filter.pfx.family;
521 } else if (strcmp(*argv, "scope") == 0) {
522 int scope = 0;
523 NEXT_ARG();
524 filter.scopemask = -1;
525 if (rtnl_rtscope_a2n(&scope, *argv)) {
526 if (strcmp(*argv, "all") != 0)
527 invarg("invalid \"scope\"\n", *argv);
528 scope = RT_SCOPE_NOWHERE;
529 filter.scopemask = 0;
530 }
531 filter.scope = scope;
532 } else if (strcmp(*argv, "up") == 0) {
533 filter.up = 1;
534 } else if (strcmp(*argv, "dynamic") == 0) {
535 filter.flags &= ~IFA_F_PERMANENT;
536 filter.flagmask |= IFA_F_PERMANENT;
537 } else if (strcmp(*argv, "permanent") == 0) {
538 filter.flags |= IFA_F_PERMANENT;
539 filter.flagmask |= IFA_F_PERMANENT;
540 } else if (strcmp(*argv, "secondary") == 0) {
541 filter.flags |= IFA_F_SECONDARY;
542 filter.flagmask |= IFA_F_SECONDARY;
543 } else if (strcmp(*argv, "primary") == 0) {
544 filter.flags &= ~IFA_F_SECONDARY;
545 filter.flagmask |= IFA_F_SECONDARY;
546 } else if (strcmp(*argv, "tentative") == 0) {
547 filter.flags |= IFA_F_TENTATIVE;
548 filter.flagmask |= IFA_F_TENTATIVE;
549 } else if (strcmp(*argv, "deprecated") == 0) {
550 filter.flags |= IFA_F_DEPRECATED;
551 filter.flagmask |= IFA_F_DEPRECATED;
552 } else if (strcmp(*argv, "label") == 0) {
553 NEXT_ARG();
554 filter.label = *argv;
555 } else {
556 if (strcmp(*argv, "dev") == 0) {
557 NEXT_ARG();
558 }
559 if (matches(*argv, "help") == 0)
560 usage();
561 if (filter_dev)
562 duparg2("dev", *argv);
563 filter_dev = *argv;
564 }
565 argv++; argc--;
566 }
567
568 if (rtnl_open(&rth, 0) < 0)
569 exit(1);
570
571 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
572 perror("Cannot send dump request");
573 exit(1);
574 }
575
576 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
577 fprintf(stderr, "Dump terminated\n");
578 exit(1);
579 }
580
581 if (filter_dev) {
582 filter.ifindex = ll_name_to_index(filter_dev);
583 if (filter.ifindex <= 0) {
584 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
585 return -1;
586 }
587 }
588
589 if (flush) {
590 int round = 0;
591 char flushb[4096-512];
592
593 filter.flushb = flushb;
594 filter.flushp = 0;
595 filter.flushe = sizeof(flushb);
596 filter.rth = &rth;
597
598 for (;;) {
599 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
600 perror("Cannot send dump request");
601 exit(1);
602 }
603 filter.flushed = 0;
604 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
605 fprintf(stderr, "Flush terminated\n");
606 exit(1);
607 }
608 if (filter.flushed == 0) {
609 if (round == 0) {
610 fprintf(stderr, "Nothing to flush.\n");
611 } else if (show_stats)
612 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
613 fflush(stdout);
614 return 0;
615 }
616 round++;
617 if (flush_update() < 0)
618 exit(1);
619 if (show_stats) {
620 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
621 fflush(stdout);
622 }
623 }
624 }
625
626 if (filter.family != AF_PACKET) {
627 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
628 perror("Cannot send dump request");
629 exit(1);
630 }
631
632 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
633 fprintf(stderr, "Dump terminated\n");
634 exit(1);
635 }
636 }
637
638
639 if (filter.family && filter.family != AF_PACKET) {
640 struct nlmsg_list **lp;
641 lp=&linfo;
642
643 if (filter.oneline)
644 no_link = 1;
645
646 while ((l=*lp)!=NULL) {
647 int ok = 0;
648 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
649 struct nlmsg_list *a;
650
651 for (a=ainfo; a; a=a->next) {
652 struct nlmsghdr *n = &a->h;
653 struct ifaddrmsg *ifa = NLMSG_DATA(n);
654
655 if (ifa->ifa_index != ifi->ifi_index ||
656 (filter.family && filter.family != ifa->ifa_family))
657 continue;
658 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
659 continue;
660 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
661 continue;
662 if (filter.pfx.family || filter.label) {
663 struct rtattr *tb[IFA_MAX+1];
aba5acdf
SH
664 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
665 if (!tb[IFA_LOCAL])
666 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
667
668 if (filter.pfx.family && tb[IFA_LOCAL]) {
669 inet_prefix dst;
670 memset(&dst, 0, sizeof(dst));
671 dst.family = ifa->ifa_family;
672 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
673 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
674 continue;
675 }
676 if (filter.label) {
677 SPRINT_BUF(b1);
678 const char *label;
679 if (tb[IFA_LABEL])
680 label = RTA_DATA(tb[IFA_LABEL]);
681 else
682 label = ll_idx_n2a(ifa->ifa_index, b1);
683 if (fnmatch(filter.label, label, 0) != 0)
684 continue;
685 }
686 }
687
688 ok = 1;
689 break;
690 }
691 if (!ok)
692 *lp = l->next;
693 else
694 lp = &l->next;
695 }
696 }
697
698 for (l=linfo; l; l = l->next) {
699 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
700 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
701 if (filter.family != AF_PACKET)
702 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
703 }
704 fflush(stdout);
705 }
706
707 exit(0);
708}
709
710int ipaddr_list_link(int argc, char **argv)
711{
712 preferred_family = AF_PACKET;
713 do_link = 1;
714 return ipaddr_list_or_flush(argc, argv, 0);
715}
716
717void ipaddr_reset_filter(int oneline)
718{
719 memset(&filter, 0, sizeof(filter));
720 filter.oneline = oneline;
721}
722
723int default_scope(inet_prefix *lcl)
724{
725 if (lcl->family == AF_INET) {
726 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
727 return RT_SCOPE_HOST;
728 }
729 return 0;
730}
731
732int ipaddr_modify(int cmd, int argc, char **argv)
733{
734 struct rtnl_handle rth;
735 struct {
736 struct nlmsghdr n;
737 struct ifaddrmsg ifa;
738 char buf[256];
739 } req;
740 char *d = NULL;
741 char *l = NULL;
742 inet_prefix lcl;
743 inet_prefix peer;
744 int local_len = 0;
745 int peer_len = 0;
746 int brd_len = 0;
747 int any_len = 0;
748 int scoped = 0;
749
750 memset(&req, 0, sizeof(req));
751
752 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
753 req.n.nlmsg_flags = NLM_F_REQUEST;
754 req.n.nlmsg_type = cmd;
755 req.ifa.ifa_family = preferred_family;
756
757 while (argc > 0) {
758 if (strcmp(*argv, "peer") == 0 ||
759 strcmp(*argv, "remote") == 0) {
760 NEXT_ARG();
761
762 if (peer_len)
763 duparg("peer", *argv);
764 get_prefix(&peer, *argv, req.ifa.ifa_family);
765 peer_len = peer.bytelen;
766 if (req.ifa.ifa_family == AF_UNSPEC)
767 req.ifa.ifa_family = peer.family;
768 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
769 req.ifa.ifa_prefixlen = peer.bitlen;
770 } else if (matches(*argv, "broadcast") == 0 ||
771 strcmp(*argv, "brd") == 0) {
772 inet_prefix addr;
773 NEXT_ARG();
774 if (brd_len)
775 duparg("broadcast", *argv);
776 if (strcmp(*argv, "+") == 0)
777 brd_len = -1;
778 else if (strcmp(*argv, "-") == 0)
779 brd_len = -2;
780 else {
781 get_addr(&addr, *argv, req.ifa.ifa_family);
782 if (req.ifa.ifa_family == AF_UNSPEC)
783 req.ifa.ifa_family = addr.family;
784 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
785 brd_len = addr.bytelen;
786 }
787 } else if (strcmp(*argv, "anycast") == 0) {
788 inet_prefix addr;
789 NEXT_ARG();
790 if (any_len)
791 duparg("anycast", *argv);
792 get_addr(&addr, *argv, req.ifa.ifa_family);
793 if (req.ifa.ifa_family == AF_UNSPEC)
794 req.ifa.ifa_family = addr.family;
795 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
796 any_len = addr.bytelen;
797 } else if (strcmp(*argv, "scope") == 0) {
798 int scope = 0;
799 NEXT_ARG();
800 if (rtnl_rtscope_a2n(&scope, *argv))
801 invarg(*argv, "invalid scope value.");
802 req.ifa.ifa_scope = scope;
803 scoped = 1;
804 } else if (strcmp(*argv, "dev") == 0) {
805 NEXT_ARG();
806 d = *argv;
807 } else if (strcmp(*argv, "label") == 0) {
808 NEXT_ARG();
809 l = *argv;
810 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
811 } else {
812 if (strcmp(*argv, "local") == 0) {
813 NEXT_ARG();
814 }
815 if (matches(*argv, "help") == 0)
816 usage();
817 if (local_len)
818 duparg2("local", *argv);
819 get_prefix(&lcl, *argv, req.ifa.ifa_family);
820 if (req.ifa.ifa_family == AF_UNSPEC)
821 req.ifa.ifa_family = lcl.family;
822 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
823 local_len = lcl.bytelen;
824 }
825 argc--; argv++;
826 }
827 if (d == NULL) {
828 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
829 return -1;
830 }
831 if (l && matches(d, l) != 0) {
832 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
833 exit(1);
834 }
835
836 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
837 peer = lcl;
838 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
839 }
840 if (req.ifa.ifa_prefixlen == 0)
841 req.ifa.ifa_prefixlen = lcl.bitlen;
842
843 if (brd_len < 0 && cmd != RTM_DELADDR) {
844 inet_prefix brd;
845 int i;
846 if (req.ifa.ifa_family != AF_INET) {
847 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
848 return -1;
849 }
850 brd = peer;
851 if (brd.bitlen <= 30) {
852 for (i=31; i>=brd.bitlen; i--) {
853 if (brd_len == -1)
854 brd.data[0] |= htonl(1<<(31-i));
855 else
856 brd.data[0] &= ~htonl(1<<(31-i));
857 }
858 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
859 brd_len = brd.bytelen;
860 }
861 }
862 if (!scoped && cmd != RTM_DELADDR)
863 req.ifa.ifa_scope = default_scope(&lcl);
864
865 if (rtnl_open(&rth, 0) < 0)
866 exit(1);
867
868 ll_init_map(&rth);
869
870 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
871 fprintf(stderr, "Cannot find device \"%s\"\n", d);
872 return -1;
873 }
874
875 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
876 exit(2);
877
878 exit(0);
879}
880
881int do_ipaddr(int argc, char **argv)
882{
883 if (argc < 1)
884 return ipaddr_list_or_flush(0, NULL, 0);
885 if (matches(*argv, "add") == 0)
886 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
887 if (matches(*argv, "delete") == 0)
888 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
889 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
890 || matches(*argv, "lst") == 0)
891 return ipaddr_list_or_flush(argc-1, argv+1, 0);
892 if (matches(*argv, "flush") == 0)
893 return ipaddr_list_or_flush(argc-1, argv+1, 1);
894 if (matches(*argv, "help") == 0)
895 usage();
896 fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv);
897 exit(-1);
898}
899