blob: ae6f8c1bfc0bdf58c0931284d4a0bce62ac3d935 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * ipaddress.c "ip address".
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
12 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
13 */
14
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>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000023#include <netinet/in.h>
24#include <arpa/inet.h>
25#include <string.h>
26#include <fnmatch.h>
27
osdl.org!shemmingere5779fb2004-06-09 22:56:28 +000028#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/sockios.h>
31
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000032#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
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000129int print_linkinfo(const struct sockaddr_nl *who,
130 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131{
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
150 memset(tb, 0, sizeof(tb));
151 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
152 if (tb[IFLA_IFNAME] == NULL) {
153 fprintf(stderr, "BUG: nil ifname\n");
154 return -1;
155 }
156 if (filter.label &&
157 (!filter.family || filter.family == AF_PACKET) &&
158 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
159 return 0;
160
161 if (n->nlmsg_type == RTM_DELLINK)
162 fprintf(fp, "Deleted ");
163
164 fprintf(fp, "%d: %s", ifi->ifi_index,
165 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
166
167 if (tb[IFLA_LINK]) {
168 SPRINT_BUF(b1);
169 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
170 if (iflink == 0)
171 fprintf(fp, "@NONE: ");
172 else {
173 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
174 m_flag = ll_index_to_flags(iflink);
175 m_flag = !(m_flag & IFF_UP);
176 }
177 } else {
178 fprintf(fp, ": ");
179 }
180 print_link_flags(fp, ifi->ifi_flags, m_flag);
181
182 if (tb[IFLA_MTU])
183 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
184 if (tb[IFLA_QDISC])
185 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
186#ifdef IFLA_MASTER
187 if (tb[IFLA_MASTER]) {
188 SPRINT_BUF(b1);
189 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
190 }
191#endif
192 if (filter.showqueue)
193 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
194
195 if (!filter.family || filter.family == AF_PACKET) {
196 SPRINT_BUF(b1);
197 fprintf(fp, "%s", _SL_);
198 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
199
200 if (tb[IFLA_ADDRESS]) {
201 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
202 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
203 ifi->ifi_type,
204 b1, sizeof(b1)));
205 }
206 if (tb[IFLA_BROADCAST]) {
207 if (ifi->ifi_flags&IFF_POINTOPOINT)
208 fprintf(fp, " peer ");
209 else
210 fprintf(fp, " brd ");
211 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
212 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
213 ifi->ifi_type,
214 b1, sizeof(b1)));
215 }
216 }
217 if (do_link && tb[IFLA_STATS] && show_stats) {
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000218 struct rtnl_link_stats slocal;
219 struct rtnl_link_stats *s = RTA_DATA(tb[IFLA_STATS]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000220 if (((unsigned long)s) & (sizeof(unsigned long)-1)) {
221 memcpy(&slocal, s, sizeof(slocal));
222 s = &slocal;
223 }
224 fprintf(fp, "%s", _SL_);
225 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
226 s->rx_compressed ? "compressed" : "", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000227 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000228 s->rx_bytes, s->rx_packets, s->rx_errors,
229 s->rx_dropped, s->rx_over_errors,
230 s->multicast
231 );
232 if (s->rx_compressed)
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000233 fprintf(fp, " %-7u", s->rx_compressed);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000234 if (show_stats > 1) {
235 fprintf(fp, "%s", _SL_);
236 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000237 fprintf(fp, " %-7u %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000238 s->rx_length_errors,
239 s->rx_crc_errors,
240 s->rx_frame_errors,
241 s->rx_fifo_errors,
242 s->rx_missed_errors
243 );
244 }
245 fprintf(fp, "%s", _SL_);
246 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
247 s->tx_compressed ? "compressed" : "", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000248 fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000249 s->tx_bytes, s->tx_packets, s->tx_errors,
250 s->tx_dropped, s->tx_carrier_errors, s->collisions);
251 if (s->tx_compressed)
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000252 fprintf(fp, " %-7u", s->tx_compressed);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000253 if (show_stats > 1) {
254 fprintf(fp, "%s", _SL_);
255 fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_);
net[shemminger]!shemmingerae0f1442004-10-19 19:57:38 +0000256 fprintf(fp, " %-7u %-7u %-7u %-7u",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000257 s->tx_aborted_errors,
258 s->tx_fifo_errors,
259 s->tx_window_errors,
260 s->tx_heartbeat_errors
261 );
262 }
263 }
264 fprintf(fp, "\n");
265 fflush(fp);
266 return 0;
267}
268
269static int flush_update(void)
270{
271 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
272 perror("Failed to send flush request\n");
273 return -1;
274 }
275 filter.flushp = 0;
276 return 0;
277}
278
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000279int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000280 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000281{
282 FILE *fp = (FILE*)arg;
283 struct ifaddrmsg *ifa = NLMSG_DATA(n);
284 int len = n->nlmsg_len;
285 struct rtattr * rta_tb[IFA_MAX+1];
286 char abuf[256];
287 SPRINT_BUF(b1);
288
289 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
290 return 0;
291 len -= NLMSG_LENGTH(sizeof(*ifa));
292 if (len < 0) {
293 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
294 return -1;
295 }
296
297 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
298 return 0;
299
300 memset(rta_tb, 0, sizeof(rta_tb));
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.flushb) {
336 struct nlmsghdr *fn;
337 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
338 if (flush_update())
339 return -1;
340 }
341 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
342 memcpy(fn, n, n->nlmsg_len);
343 fn->nlmsg_type = RTM_DELADDR;
344 fn->nlmsg_flags = NLM_F_REQUEST;
345 fn->nlmsg_seq = ++filter.rth->seq;
346 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
347 filter.flushed++;
348 if (show_stats < 2)
349 return 0;
350 }
351
352 if (n->nlmsg_type == RTM_DELADDR)
353 fprintf(fp, "Deleted ");
354
355 if (filter.oneline || filter.flushb)
356 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
357 if (ifa->ifa_family == AF_INET)
358 fprintf(fp, " inet ");
359 else if (ifa->ifa_family == AF_INET6)
360 fprintf(fp, " inet6 ");
361 else if (ifa->ifa_family == AF_DECnet)
362 fprintf(fp, " dnet ");
363 else if (ifa->ifa_family == AF_IPX)
364 fprintf(fp, " ipx ");
365 else
366 fprintf(fp, " family %d ", ifa->ifa_family);
367
368 if (rta_tb[IFA_LOCAL]) {
369 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
370 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
371 RTA_DATA(rta_tb[IFA_LOCAL]),
372 abuf, sizeof(abuf)));
373
374 if (rta_tb[IFA_ADDRESS] == NULL ||
375 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
376 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
377 } else {
378 fprintf(fp, " peer %s/%d ",
379 rt_addr_n2a(ifa->ifa_family,
380 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
381 RTA_DATA(rta_tb[IFA_ADDRESS]),
382 abuf, sizeof(abuf)),
383 ifa->ifa_prefixlen);
384 }
385 }
386
387 if (rta_tb[IFA_BROADCAST]) {
388 fprintf(fp, "brd %s ",
389 rt_addr_n2a(ifa->ifa_family,
390 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
391 RTA_DATA(rta_tb[IFA_BROADCAST]),
392 abuf, sizeof(abuf)));
393 }
394 if (rta_tb[IFA_ANYCAST]) {
395 fprintf(fp, "any %s ",
396 rt_addr_n2a(ifa->ifa_family,
397 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
398 RTA_DATA(rta_tb[IFA_ANYCAST]),
399 abuf, sizeof(abuf)));
400 }
401 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
402 if (ifa->ifa_flags&IFA_F_SECONDARY) {
403 ifa->ifa_flags &= ~IFA_F_SECONDARY;
404 fprintf(fp, "secondary ");
405 }
406 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
407 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
408 fprintf(fp, "tentative ");
409 }
410 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
411 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
412 fprintf(fp, "deprecated ");
413 }
414 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
415 fprintf(fp, "dynamic ");
416 } else
417 ifa->ifa_flags &= ~IFA_F_PERMANENT;
418 if (ifa->ifa_flags)
419 fprintf(fp, "flags %02x ", ifa->ifa_flags);
420 if (rta_tb[IFA_LABEL])
421 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
422 if (rta_tb[IFA_CACHEINFO]) {
423 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
424 char buf[128];
425 fprintf(fp, "%s", _SL_);
426 if (ci->ifa_valid == 0xFFFFFFFFU)
427 sprintf(buf, "valid_lft forever");
428 else
429 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
430 if (ci->ifa_prefered == 0xFFFFFFFFU)
431 sprintf(buf+strlen(buf), " preferred_lft forever");
432 else
433 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
434 fprintf(fp, " %s", buf);
435 }
436 fprintf(fp, "\n");
437 fflush(fp);
438 return 0;
439}
440
441
442struct nlmsg_list
443{
444 struct nlmsg_list *next;
445 struct nlmsghdr h;
446};
447
448int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
449{
450 for ( ;ainfo ; ainfo = ainfo->next) {
451 struct nlmsghdr *n = &ainfo->h;
452 struct ifaddrmsg *ifa = NLMSG_DATA(n);
453
454 if (n->nlmsg_type != RTM_NEWADDR)
455 continue;
456
457 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
458 return -1;
459
460 if (ifa->ifa_index != ifindex ||
461 (filter.family && filter.family != ifa->ifa_family))
462 continue;
463
464 print_addrinfo(NULL, n, fp);
465 }
466 return 0;
467}
468
469
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000470static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000471 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000472{
473 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
474 struct nlmsg_list *h;
475 struct nlmsg_list **lp;
476
477 h = malloc(n->nlmsg_len+sizeof(void*));
478 if (h == NULL)
479 return -1;
480
481 memcpy(&h->h, n, n->nlmsg_len);
482 h->next = NULL;
483
484 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
485 *lp = h;
486
487 ll_remember_index(who, n, NULL);
488 return 0;
489}
490
491int ipaddr_list_or_flush(int argc, char **argv, int flush)
492{
493 struct nlmsg_list *linfo = NULL;
494 struct nlmsg_list *ainfo = NULL;
495 struct nlmsg_list *l;
496 struct rtnl_handle rth;
497 char *filter_dev = NULL;
498 int no_link = 0;
499
500 ipaddr_reset_filter(oneline);
501 filter.showqueue = 1;
502
503 if (filter.family == AF_UNSPEC)
504 filter.family = preferred_family;
505
506 if (flush) {
507 if (argc <= 0) {
508 fprintf(stderr, "Flush requires arguments.\n");
509 return -1;
510 }
511 if (filter.family == AF_PACKET) {
512 fprintf(stderr, "Cannot flush link addresses.\n");
513 return -1;
514 }
515 }
516
517 while (argc > 0) {
518 if (strcmp(*argv, "to") == 0) {
519 NEXT_ARG();
520 get_prefix(&filter.pfx, *argv, filter.family);
521 if (filter.family == AF_UNSPEC)
522 filter.family = filter.pfx.family;
523 } else if (strcmp(*argv, "scope") == 0) {
524 int scope = 0;
525 NEXT_ARG();
526 filter.scopemask = -1;
527 if (rtnl_rtscope_a2n(&scope, *argv)) {
528 if (strcmp(*argv, "all") != 0)
529 invarg("invalid \"scope\"\n", *argv);
530 scope = RT_SCOPE_NOWHERE;
531 filter.scopemask = 0;
532 }
533 filter.scope = scope;
534 } else if (strcmp(*argv, "up") == 0) {
535 filter.up = 1;
536 } else if (strcmp(*argv, "dynamic") == 0) {
537 filter.flags &= ~IFA_F_PERMANENT;
538 filter.flagmask |= IFA_F_PERMANENT;
539 } else if (strcmp(*argv, "permanent") == 0) {
540 filter.flags |= IFA_F_PERMANENT;
541 filter.flagmask |= IFA_F_PERMANENT;
542 } else if (strcmp(*argv, "secondary") == 0) {
543 filter.flags |= IFA_F_SECONDARY;
544 filter.flagmask |= IFA_F_SECONDARY;
545 } else if (strcmp(*argv, "primary") == 0) {
546 filter.flags &= ~IFA_F_SECONDARY;
547 filter.flagmask |= IFA_F_SECONDARY;
548 } else if (strcmp(*argv, "tentative") == 0) {
549 filter.flags |= IFA_F_TENTATIVE;
550 filter.flagmask |= IFA_F_TENTATIVE;
551 } else if (strcmp(*argv, "deprecated") == 0) {
552 filter.flags |= IFA_F_DEPRECATED;
553 filter.flagmask |= IFA_F_DEPRECATED;
554 } else if (strcmp(*argv, "label") == 0) {
555 NEXT_ARG();
556 filter.label = *argv;
557 } else {
558 if (strcmp(*argv, "dev") == 0) {
559 NEXT_ARG();
560 }
561 if (matches(*argv, "help") == 0)
562 usage();
563 if (filter_dev)
564 duparg2("dev", *argv);
565 filter_dev = *argv;
566 }
567 argv++; argc--;
568 }
569
570 if (rtnl_open(&rth, 0) < 0)
571 exit(1);
572
573 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
574 perror("Cannot send dump request");
575 exit(1);
576 }
577
578 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
579 fprintf(stderr, "Dump terminated\n");
580 exit(1);
581 }
582
583 if (filter_dev) {
584 filter.ifindex = ll_name_to_index(filter_dev);
585 if (filter.ifindex <= 0) {
586 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
587 return -1;
588 }
589 }
590
591 if (flush) {
592 int round = 0;
593 char flushb[4096-512];
594
595 filter.flushb = flushb;
596 filter.flushp = 0;
597 filter.flushe = sizeof(flushb);
598 filter.rth = &rth;
599
600 for (;;) {
601 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
602 perror("Cannot send dump request");
603 exit(1);
604 }
605 filter.flushed = 0;
606 if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) {
607 fprintf(stderr, "Flush terminated\n");
608 exit(1);
609 }
610 if (filter.flushed == 0) {
611 if (round == 0) {
612 fprintf(stderr, "Nothing to flush.\n");
613 } else if (show_stats)
614 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
615 fflush(stdout);
616 return 0;
617 }
618 round++;
619 if (flush_update() < 0)
620 exit(1);
621 if (show_stats) {
622 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
623 fflush(stdout);
624 }
625 }
626 }
627
628 if (filter.family != AF_PACKET) {
629 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
630 perror("Cannot send dump request");
631 exit(1);
632 }
633
634 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
635 fprintf(stderr, "Dump terminated\n");
636 exit(1);
637 }
638 }
639
640
641 if (filter.family && filter.family != AF_PACKET) {
642 struct nlmsg_list **lp;
643 lp=&linfo;
644
645 if (filter.oneline)
646 no_link = 1;
647
648 while ((l=*lp)!=NULL) {
649 int ok = 0;
650 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
651 struct nlmsg_list *a;
652
653 for (a=ainfo; a; a=a->next) {
654 struct nlmsghdr *n = &a->h;
655 struct ifaddrmsg *ifa = NLMSG_DATA(n);
656
657 if (ifa->ifa_index != ifi->ifi_index ||
658 (filter.family && filter.family != ifa->ifa_family))
659 continue;
660 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
661 continue;
662 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
663 continue;
664 if (filter.pfx.family || filter.label) {
665 struct rtattr *tb[IFA_MAX+1];
666 memset(tb, 0, sizeof(tb));
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