]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipaddress.c
(Logical change 1.160)
[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
3eb1731b 333 if (filter.family && filter.family != ifa->ifa_family)
334 return 0;
335
aba5acdf
SH
336 if (filter.flushb) {
337 struct nlmsghdr *fn;
338 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
339 if (flush_update())
340 return -1;
341 }
342 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
343 memcpy(fn, n, n->nlmsg_len);
344 fn->nlmsg_type = RTM_DELADDR;
345 fn->nlmsg_flags = NLM_F_REQUEST;
346 fn->nlmsg_seq = ++filter.rth->seq;
347 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
348 filter.flushed++;
349 if (show_stats < 2)
350 return 0;
351 }
352
353 if (n->nlmsg_type == RTM_DELADDR)
354 fprintf(fp, "Deleted ");
355
356 if (filter.oneline || filter.flushb)
357 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
358 if (ifa->ifa_family == AF_INET)
359 fprintf(fp, " inet ");
360 else if (ifa->ifa_family == AF_INET6)
361 fprintf(fp, " inet6 ");
362 else if (ifa->ifa_family == AF_DECnet)
363 fprintf(fp, " dnet ");
364 else if (ifa->ifa_family == AF_IPX)
365 fprintf(fp, " ipx ");
366 else
367 fprintf(fp, " family %d ", ifa->ifa_family);
368
369 if (rta_tb[IFA_LOCAL]) {
370 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
371 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
372 RTA_DATA(rta_tb[IFA_LOCAL]),
373 abuf, sizeof(abuf)));
374
375 if (rta_tb[IFA_ADDRESS] == NULL ||
376 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
377 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
378 } else {
379 fprintf(fp, " peer %s/%d ",
380 rt_addr_n2a(ifa->ifa_family,
381 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
382 RTA_DATA(rta_tb[IFA_ADDRESS]),
383 abuf, sizeof(abuf)),
384 ifa->ifa_prefixlen);
385 }
386 }
387
388 if (rta_tb[IFA_BROADCAST]) {
389 fprintf(fp, "brd %s ",
390 rt_addr_n2a(ifa->ifa_family,
391 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
392 RTA_DATA(rta_tb[IFA_BROADCAST]),
393 abuf, sizeof(abuf)));
394 }
395 if (rta_tb[IFA_ANYCAST]) {
396 fprintf(fp, "any %s ",
397 rt_addr_n2a(ifa->ifa_family,
398 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
399 RTA_DATA(rta_tb[IFA_ANYCAST]),
400 abuf, sizeof(abuf)));
401 }
402 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
403 if (ifa->ifa_flags&IFA_F_SECONDARY) {
404 ifa->ifa_flags &= ~IFA_F_SECONDARY;
405 fprintf(fp, "secondary ");
406 }
407 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
408 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
409 fprintf(fp, "tentative ");
410 }
411 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
412 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
413 fprintf(fp, "deprecated ");
414 }
415 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
416 fprintf(fp, "dynamic ");
417 } else
418 ifa->ifa_flags &= ~IFA_F_PERMANENT;
419 if (ifa->ifa_flags)
420 fprintf(fp, "flags %02x ", ifa->ifa_flags);
421 if (rta_tb[IFA_LABEL])
422 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
423 if (rta_tb[IFA_CACHEINFO]) {
424 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
425 char buf[128];
426 fprintf(fp, "%s", _SL_);
427 if (ci->ifa_valid == 0xFFFFFFFFU)
428 sprintf(buf, "valid_lft forever");
429 else
430 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
431 if (ci->ifa_prefered == 0xFFFFFFFFU)
432 sprintf(buf+strlen(buf), " preferred_lft forever");
433 else
434 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
435 fprintf(fp, " %s", buf);
436 }
437 fprintf(fp, "\n");
438 fflush(fp);
439 return 0;
440}
441
442
443struct nlmsg_list
444{
445 struct nlmsg_list *next;
446 struct nlmsghdr h;
447};
448
449int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
450{
451 for ( ;ainfo ; ainfo = ainfo->next) {
452 struct nlmsghdr *n = &ainfo->h;
453 struct ifaddrmsg *ifa = NLMSG_DATA(n);
454
455 if (n->nlmsg_type != RTM_NEWADDR)
456 continue;
457
458 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
459 return -1;
460
461 if (ifa->ifa_index != ifindex ||
462 (filter.family && filter.family != ifa->ifa_family))
463 continue;
464
465 print_addrinfo(NULL, n, fp);
466 }
467 return 0;
468}
469
470
50772dc5 471static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
6dc9f016 472 void *arg)
aba5acdf
SH
473{
474 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
475 struct nlmsg_list *h;
476 struct nlmsg_list **lp;
477
478 h = malloc(n->nlmsg_len+sizeof(void*));
479 if (h == NULL)
480 return -1;
481
482 memcpy(&h->h, n, n->nlmsg_len);
483 h->next = NULL;
484
485 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
486 *lp = h;
487
488 ll_remember_index(who, n, NULL);
489 return 0;
490}
491
492int ipaddr_list_or_flush(int argc, char **argv, int flush)
493{
494 struct nlmsg_list *linfo = NULL;
495 struct nlmsg_list *ainfo = NULL;
496 struct nlmsg_list *l;
497 struct rtnl_handle rth;
498 char *filter_dev = NULL;
499 int no_link = 0;
500
501 ipaddr_reset_filter(oneline);
502 filter.showqueue = 1;
503
504 if (filter.family == AF_UNSPEC)
505 filter.family = preferred_family;
506
507 if (flush) {
508 if (argc <= 0) {
509 fprintf(stderr, "Flush requires arguments.\n");
510 return -1;
511 }
512 if (filter.family == AF_PACKET) {
513 fprintf(stderr, "Cannot flush link addresses.\n");
514 return -1;
515 }
516 }
517
518 while (argc > 0) {
519 if (strcmp(*argv, "to") == 0) {
520 NEXT_ARG();
521 get_prefix(&filter.pfx, *argv, filter.family);
522 if (filter.family == AF_UNSPEC)
523 filter.family = filter.pfx.family;
524 } else if (strcmp(*argv, "scope") == 0) {
525 int scope = 0;
526 NEXT_ARG();
527 filter.scopemask = -1;
528 if (rtnl_rtscope_a2n(&scope, *argv)) {
529 if (strcmp(*argv, "all") != 0)
530 invarg("invalid \"scope\"\n", *argv);
531 scope = RT_SCOPE_NOWHERE;
532 filter.scopemask = 0;
533 }
534 filter.scope = scope;
535 } else if (strcmp(*argv, "up") == 0) {
536 filter.up = 1;
537 } else if (strcmp(*argv, "dynamic") == 0) {
538 filter.flags &= ~IFA_F_PERMANENT;
539 filter.flagmask |= IFA_F_PERMANENT;
540 } else if (strcmp(*argv, "permanent") == 0) {
541 filter.flags |= IFA_F_PERMANENT;
542 filter.flagmask |= IFA_F_PERMANENT;
543 } else if (strcmp(*argv, "secondary") == 0) {
544 filter.flags |= IFA_F_SECONDARY;
545 filter.flagmask |= IFA_F_SECONDARY;
546 } else if (strcmp(*argv, "primary") == 0) {
547 filter.flags &= ~IFA_F_SECONDARY;
548 filter.flagmask |= IFA_F_SECONDARY;
549 } else if (strcmp(*argv, "tentative") == 0) {
550 filter.flags |= IFA_F_TENTATIVE;
551 filter.flagmask |= IFA_F_TENTATIVE;
552 } else if (strcmp(*argv, "deprecated") == 0) {
553 filter.flags |= IFA_F_DEPRECATED;
554 filter.flagmask |= IFA_F_DEPRECATED;
555 } else if (strcmp(*argv, "label") == 0) {
556 NEXT_ARG();
557 filter.label = *argv;
558 } else {
559 if (strcmp(*argv, "dev") == 0) {
560 NEXT_ARG();
561 }
562 if (matches(*argv, "help") == 0)
563 usage();
564 if (filter_dev)
565 duparg2("dev", *argv);
566 filter_dev = *argv;
567 }
568 argv++; argc--;
569 }
570
571 if (rtnl_open(&rth, 0) < 0)
572 exit(1);
573
574 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
575 perror("Cannot send dump request");
576 exit(1);
577 }
578
579 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
580 fprintf(stderr, "Dump terminated\n");
581 exit(1);
582 }
583
584 if (filter_dev) {
585 filter.ifindex = ll_name_to_index(filter_dev);
586 if (filter.ifindex <= 0) {
587 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
588 return -1;
589 }
590 }
591
592 if (flush) {
593 int round = 0;
594 char flushb[4096-512];
595
596 filter.flushb = flushb;
597 filter.flushp = 0;
598 filter.flushe = sizeof(flushb);
599 filter.rth = &rth;
600
601 for (;;) {
602 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
603 perror("Cannot send dump request");
604 exit(1);
605 }
606 filter.flushed = 0;
607 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
608 fprintf(stderr, "Flush terminated\n");
609 exit(1);
610 }
611 if (filter.flushed == 0) {
612 if (round == 0) {
613 fprintf(stderr, "Nothing to flush.\n");
614 } else if (show_stats)
615 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
616 fflush(stdout);
617 return 0;
618 }
619 round++;
620 if (flush_update() < 0)
621 exit(1);
622 if (show_stats) {
623 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
624 fflush(stdout);
625 }
626 }
627 }
628
629 if (filter.family != AF_PACKET) {
630 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
631 perror("Cannot send dump request");
632 exit(1);
633 }
634
635 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
636 fprintf(stderr, "Dump terminated\n");
637 exit(1);
638 }
639 }
640
641
642 if (filter.family && filter.family != AF_PACKET) {
643 struct nlmsg_list **lp;
644 lp=&linfo;
645
646 if (filter.oneline)
647 no_link = 1;
648
649 while ((l=*lp)!=NULL) {
650 int ok = 0;
651 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
652 struct nlmsg_list *a;
653
654 for (a=ainfo; a; a=a->next) {
655 struct nlmsghdr *n = &a->h;
656 struct ifaddrmsg *ifa = NLMSG_DATA(n);
657
658 if (ifa->ifa_index != ifi->ifi_index ||
659 (filter.family && filter.family != ifa->ifa_family))
660 continue;
661 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
662 continue;
663 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
664 continue;
665 if (filter.pfx.family || filter.label) {
666 struct rtattr *tb[IFA_MAX+1];
aba5acdf
SH
667 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
668 if (!tb[IFA_LOCAL])
669 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
670
671 if (filter.pfx.family && tb[IFA_LOCAL]) {
672 inet_prefix dst;
673 memset(&dst, 0, sizeof(dst));
674 dst.family = ifa->ifa_family;
675 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
676 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
677 continue;
678 }
679 if (filter.label) {
680 SPRINT_BUF(b1);
681 const char *label;
682 if (tb[IFA_LABEL])
683 label = RTA_DATA(tb[IFA_LABEL]);
684 else
685 label = ll_idx_n2a(ifa->ifa_index, b1);
686 if (fnmatch(filter.label, label, 0) != 0)
687 continue;
688 }
689 }
690
691 ok = 1;
692 break;
693 }
694 if (!ok)
695 *lp = l->next;
696 else
697 lp = &l->next;
698 }
699 }
700
701 for (l=linfo; l; l = l->next) {
702 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
703 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
704 if (filter.family != AF_PACKET)
705 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
706 }
707 fflush(stdout);
708 }
709
710 exit(0);
711}
712
713int ipaddr_list_link(int argc, char **argv)
714{
715 preferred_family = AF_PACKET;
716 do_link = 1;
717 return ipaddr_list_or_flush(argc, argv, 0);
718}
719
720void ipaddr_reset_filter(int oneline)
721{
722 memset(&filter, 0, sizeof(filter));
723 filter.oneline = oneline;
724}
725
726int default_scope(inet_prefix *lcl)
727{
728 if (lcl->family == AF_INET) {
729 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
730 return RT_SCOPE_HOST;
731 }
732 return 0;
733}
734
735int ipaddr_modify(int cmd, int argc, char **argv)
736{
737 struct rtnl_handle rth;
738 struct {
739 struct nlmsghdr n;
740 struct ifaddrmsg ifa;
741 char buf[256];
742 } req;
743 char *d = NULL;
744 char *l = NULL;
745 inet_prefix lcl;
746 inet_prefix peer;
747 int local_len = 0;
748 int peer_len = 0;
749 int brd_len = 0;
750 int any_len = 0;
751 int scoped = 0;
752
753 memset(&req, 0, sizeof(req));
754
755 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
756 req.n.nlmsg_flags = NLM_F_REQUEST;
757 req.n.nlmsg_type = cmd;
758 req.ifa.ifa_family = preferred_family;
759
760 while (argc > 0) {
761 if (strcmp(*argv, "peer") == 0 ||
762 strcmp(*argv, "remote") == 0) {
763 NEXT_ARG();
764
765 if (peer_len)
766 duparg("peer", *argv);
767 get_prefix(&peer, *argv, req.ifa.ifa_family);
768 peer_len = peer.bytelen;
769 if (req.ifa.ifa_family == AF_UNSPEC)
770 req.ifa.ifa_family = peer.family;
771 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
772 req.ifa.ifa_prefixlen = peer.bitlen;
773 } else if (matches(*argv, "broadcast") == 0 ||
774 strcmp(*argv, "brd") == 0) {
775 inet_prefix addr;
776 NEXT_ARG();
777 if (brd_len)
778 duparg("broadcast", *argv);
779 if (strcmp(*argv, "+") == 0)
780 brd_len = -1;
781 else if (strcmp(*argv, "-") == 0)
782 brd_len = -2;
783 else {
784 get_addr(&addr, *argv, req.ifa.ifa_family);
785 if (req.ifa.ifa_family == AF_UNSPEC)
786 req.ifa.ifa_family = addr.family;
787 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
788 brd_len = addr.bytelen;
789 }
790 } else if (strcmp(*argv, "anycast") == 0) {
791 inet_prefix addr;
792 NEXT_ARG();
793 if (any_len)
794 duparg("anycast", *argv);
795 get_addr(&addr, *argv, req.ifa.ifa_family);
796 if (req.ifa.ifa_family == AF_UNSPEC)
797 req.ifa.ifa_family = addr.family;
798 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
799 any_len = addr.bytelen;
800 } else if (strcmp(*argv, "scope") == 0) {
801 int scope = 0;
802 NEXT_ARG();
803 if (rtnl_rtscope_a2n(&scope, *argv))
804 invarg(*argv, "invalid scope value.");
805 req.ifa.ifa_scope = scope;
806 scoped = 1;
807 } else if (strcmp(*argv, "dev") == 0) {
808 NEXT_ARG();
809 d = *argv;
810 } else if (strcmp(*argv, "label") == 0) {
811 NEXT_ARG();
812 l = *argv;
813 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
814 } else {
815 if (strcmp(*argv, "local") == 0) {
816 NEXT_ARG();
817 }
818 if (matches(*argv, "help") == 0)
819 usage();
820 if (local_len)
821 duparg2("local", *argv);
822 get_prefix(&lcl, *argv, req.ifa.ifa_family);
823 if (req.ifa.ifa_family == AF_UNSPEC)
824 req.ifa.ifa_family = lcl.family;
825 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
826 local_len = lcl.bytelen;
827 }
828 argc--; argv++;
829 }
830 if (d == NULL) {
831 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
832 return -1;
833 }
834 if (l && matches(d, l) != 0) {
835 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
836 exit(1);
837 }
838
839 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
840 peer = lcl;
841 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
842 }
843 if (req.ifa.ifa_prefixlen == 0)
844 req.ifa.ifa_prefixlen = lcl.bitlen;
845
846 if (brd_len < 0 && cmd != RTM_DELADDR) {
847 inet_prefix brd;
848 int i;
849 if (req.ifa.ifa_family != AF_INET) {
850 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
851 return -1;
852 }
853 brd = peer;
854 if (brd.bitlen <= 30) {
855 for (i=31; i>=brd.bitlen; i--) {
856 if (brd_len == -1)
857 brd.data[0] |= htonl(1<<(31-i));
858 else
859 brd.data[0] &= ~htonl(1<<(31-i));
860 }
861 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
862 brd_len = brd.bytelen;
863 }
864 }
865 if (!scoped && cmd != RTM_DELADDR)
866 req.ifa.ifa_scope = default_scope(&lcl);
867
868 if (rtnl_open(&rth, 0) < 0)
869 exit(1);
870
871 ll_init_map(&rth);
872
873 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
874 fprintf(stderr, "Cannot find device \"%s\"\n", d);
875 return -1;
876 }
877
878 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
879 exit(2);
880
881 exit(0);
882}
883
884int do_ipaddr(int argc, char **argv)
885{
886 if (argc < 1)
887 return ipaddr_list_or_flush(0, NULL, 0);
888 if (matches(*argv, "add") == 0)
889 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
890 if (matches(*argv, "delete") == 0)
891 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
892 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
893 || matches(*argv, "lst") == 0)
894 return ipaddr_list_or_flush(argc-1, argv+1, 0);
895 if (matches(*argv, "flush") == 0)
896 return ipaddr_list_or_flush(argc-1, argv+1, 1);
897 if (matches(*argv, "help") == 0)
898 usage();
899 fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv);
900 exit(-1);
901}
902