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