]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipaddress.c
Merge branch 'master' into net-next
[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 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <inttypes.h>
18 #include <fcntl.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
22 #include <errno.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 #include <linux/net_namespace.h>
32
33 #include "rt_names.h"
34 #include "utils.h"
35 #include "ll_map.h"
36 #include "ip_common.h"
37 #include "color.h"
38
39 enum {
40 IPADD_LIST,
41 IPADD_FLUSH,
42 IPADD_SAVE,
43 };
44
45 static struct
46 {
47 int ifindex;
48 int family;
49 int oneline;
50 int showqueue;
51 inet_prefix pfx;
52 int scope, scopemask;
53 int flags, flagmask;
54 int up;
55 char *label;
56 int flushed;
57 char *flushb;
58 int flushp;
59 int flushe;
60 int group;
61 int master;
62 char *kind;
63 } filter;
64
65 static int do_link;
66
67 static void usage(void) __attribute__((noreturn));
68
69 static void usage(void)
70 {
71 if (do_link) {
72 iplink_usage();
73 }
74 fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n");
75 fprintf(stderr, " [ CONFFLAG-LIST ]\n");
76 fprintf(stderr, " ip addr del IFADDR dev STRING [mngtmpaddr]\n");
77 fprintf(stderr, " ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]\n");
78 fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [up]\n");
79 fprintf(stderr, " ip addr {showdump|restore}\n");
80 fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
81 fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n");
82 fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n");
83 fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
84 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
85 fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n");
86 fprintf(stderr, " [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
87 fprintf(stderr, " CONFFLAG-LIST ]\n");
88 fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
89 fprintf(stderr, "CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
90 fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
91 fprintf(stderr, "LFT := forever | SECONDS\n");
92
93 exit(-1);
94 }
95
96 static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
97 {
98 fprintf(fp, "<");
99 if (flags & IFF_UP && !(flags & IFF_RUNNING))
100 fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
101 flags &= ~IFF_RUNNING;
102 #define _PF(f) if (flags&IFF_##f) { \
103 flags &= ~IFF_##f ; \
104 fprintf(fp, #f "%s", flags ? "," : ""); }
105 _PF(LOOPBACK);
106 _PF(BROADCAST);
107 _PF(POINTOPOINT);
108 _PF(MULTICAST);
109 _PF(NOARP);
110 _PF(ALLMULTI);
111 _PF(PROMISC);
112 _PF(MASTER);
113 _PF(SLAVE);
114 _PF(DEBUG);
115 _PF(DYNAMIC);
116 _PF(AUTOMEDIA);
117 _PF(PORTSEL);
118 _PF(NOTRAILERS);
119 _PF(UP);
120 _PF(LOWER_UP);
121 _PF(DORMANT);
122 _PF(ECHO);
123 #undef _PF
124 if (flags)
125 fprintf(fp, "%x", flags);
126 if (mdown)
127 fprintf(fp, ",M-DOWN");
128 fprintf(fp, "> ");
129 }
130
131 static const char *oper_states[] = {
132 "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
133 "TESTING", "DORMANT", "UP"
134 };
135
136 static void print_operstate(FILE *f, __u8 state)
137 {
138 if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
139 fprintf(f, "state %#x ", state);
140 else {
141 fprintf(f, "state ");
142 if (strcmp(oper_states[state], "UP") == 0)
143 color_fprintf(f, COLOR_OPERSTATE_UP, "%s ", oper_states[state]);
144 else if (strcmp(oper_states[state], "DOWN") == 0)
145 color_fprintf(f, COLOR_OPERSTATE_DOWN, "%s ", oper_states[state]);
146 else
147 fprintf(f, "%s ", oper_states[state]);
148 }
149 }
150
151 int get_operstate(const char *name)
152 {
153 int i;
154
155 for (i = 0; i < sizeof(oper_states)/sizeof(oper_states[0]); i++)
156 if (strcasecmp(name, oper_states[i]) == 0)
157 return i;
158 return -1;
159 }
160
161 static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
162 {
163 int qlen;
164
165 if (tb[IFLA_TXQLEN])
166 qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
167 else {
168 struct ifreq ifr;
169 int s = socket(AF_INET, SOCK_STREAM, 0);
170
171 if (s < 0)
172 return;
173
174 memset(&ifr, 0, sizeof(ifr));
175 strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME]));
176 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
177 fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno));
178 close(s);
179 return;
180 }
181 close(s);
182 qlen = ifr.ifr_qlen;
183 }
184 if (qlen)
185 fprintf(f, "qlen %d", qlen);
186 }
187
188 static const char *link_modes[] = {
189 "DEFAULT", "DORMANT"
190 };
191
192 static void print_linkmode(FILE *f, struct rtattr *tb)
193 {
194 unsigned int mode = rta_getattr_u8(tb);
195
196 if (mode >= sizeof(link_modes) / sizeof(link_modes[0]))
197 fprintf(f, "mode %d ", mode);
198 else
199 fprintf(f, "mode %s ", link_modes[mode]);
200 }
201
202 static char *parse_link_kind(struct rtattr *tb)
203 {
204 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
205
206 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
207
208 if (linkinfo[IFLA_INFO_KIND])
209 return RTA_DATA(linkinfo[IFLA_INFO_KIND]);
210
211 return "";
212 }
213
214 static void print_linktype(FILE *fp, struct rtattr *tb)
215 {
216 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
217 struct link_util *lu;
218 struct link_util *slave_lu;
219 char *kind;
220 char *slave_kind;
221
222 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
223
224 if (linkinfo[IFLA_INFO_KIND]) {
225 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
226
227 fprintf(fp, "%s", _SL_);
228 fprintf(fp, " %s ", kind);
229
230 lu = get_link_kind(kind);
231 if (lu && lu->print_opt) {
232 struct rtattr *attr[lu->maxattr+1], **data = NULL;
233
234 if (linkinfo[IFLA_INFO_DATA]) {
235 parse_rtattr_nested(attr, lu->maxattr,
236 linkinfo[IFLA_INFO_DATA]);
237 data = attr;
238 }
239 lu->print_opt(lu, fp, data);
240
241 if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
242 lu->print_xstats)
243 lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
244 }
245 }
246
247 if (linkinfo[IFLA_INFO_SLAVE_KIND]) {
248 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
249
250 fprintf(fp, "%s", _SL_);
251 fprintf(fp, " %s_slave ", slave_kind);
252
253 slave_lu = get_link_slave_kind(slave_kind);
254 if (slave_lu && slave_lu->print_opt) {
255 struct rtattr *attr[slave_lu->maxattr+1], **data = NULL;
256
257 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
258 parse_rtattr_nested(attr, slave_lu->maxattr,
259 linkinfo[IFLA_INFO_SLAVE_DATA]);
260 data = attr;
261 }
262 slave_lu->print_opt(slave_lu, fp, data);
263 }
264 }
265 }
266
267 static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr)
268 {
269 struct rtattr *inet6_attr;
270 struct rtattr *tb[IFLA_INET6_MAX + 1];
271
272 inet6_attr = parse_rtattr_one_nested(AF_INET6, af_spec_attr);
273 if (!inet6_attr)
274 return;
275
276 parse_rtattr_nested(tb, IFLA_INET6_MAX, inet6_attr);
277
278 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
279 switch (rta_getattr_u8(tb[IFLA_INET6_ADDR_GEN_MODE])) {
280 case IN6_ADDR_GEN_MODE_EUI64:
281 fprintf(fp, "addrgenmode eui64 ");
282 break;
283 case IN6_ADDR_GEN_MODE_NONE:
284 fprintf(fp, "addrgenmode none ");
285 break;
286 }
287 }
288 }
289
290 static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
291 {
292 struct ifla_vf_mac *vf_mac;
293 struct ifla_vf_vlan *vf_vlan;
294 struct ifla_vf_tx_rate *vf_tx_rate;
295 struct ifla_vf_spoofchk *vf_spoofchk;
296 struct ifla_vf_link_state *vf_linkstate;
297 struct rtattr *vf[IFLA_VF_MAX + 1] = {};
298 struct rtattr *tmp;
299 SPRINT_BUF(b1);
300
301 if (vfinfo->rta_type != IFLA_VF_INFO) {
302 fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type);
303 return;
304 }
305
306 parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
307
308 vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
309 vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
310 vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
311
312 /* Check if the spoof checking vf info type is supported by
313 * this kernel.
314 */
315 tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
316 vf[IFLA_VF_TX_RATE]->rta_len);
317
318 if (tmp->rta_type != IFLA_VF_SPOOFCHK)
319 vf_spoofchk = NULL;
320 else
321 vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
322
323 if (vf_spoofchk) {
324 /* Check if the link state vf info type is supported by
325 * this kernel.
326 */
327 tmp = (struct rtattr *)((char *)vf[IFLA_VF_SPOOFCHK] +
328 vf[IFLA_VF_SPOOFCHK]->rta_len);
329
330 if (tmp->rta_type != IFLA_VF_LINK_STATE)
331 vf_linkstate = NULL;
332 else
333 vf_linkstate = RTA_DATA(vf[IFLA_VF_LINK_STATE]);
334 } else
335 vf_linkstate = NULL;
336
337 fprintf(fp, "\n vf %d MAC %s", vf_mac->vf,
338 ll_addr_n2a((unsigned char *)&vf_mac->mac,
339 ETH_ALEN, 0, b1, sizeof(b1)));
340 if (vf_vlan->vlan)
341 fprintf(fp, ", vlan %d", vf_vlan->vlan);
342 if (vf_vlan->qos)
343 fprintf(fp, ", qos %d", vf_vlan->qos);
344 if (vf_tx_rate->rate)
345 fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
346
347 if (vf[IFLA_VF_RATE]) {
348 struct ifla_vf_rate *vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
349
350 if (vf_rate->max_tx_rate)
351 fprintf(fp, ", max_tx_rate %dMbps", vf_rate->max_tx_rate);
352 if (vf_rate->min_tx_rate)
353 fprintf(fp, ", min_tx_rate %dMbps", vf_rate->min_tx_rate);
354 }
355
356 if (vf_spoofchk && vf_spoofchk->setting != -1) {
357 if (vf_spoofchk->setting)
358 fprintf(fp, ", spoof checking on");
359 else
360 fprintf(fp, ", spoof checking off");
361 }
362 if (vf_linkstate) {
363 if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_AUTO)
364 fprintf(fp, ", link-state auto");
365 else if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_ENABLE)
366 fprintf(fp, ", link-state enable");
367 else
368 fprintf(fp, ", link-state disable");
369 }
370 }
371
372 static void print_num(FILE *fp, unsigned width, uint64_t count)
373 {
374 const char *prefix = "kMGTPE";
375 const unsigned int base = use_iec ? 1024 : 1000;
376 uint64_t powi = 1;
377 uint16_t powj = 1;
378 uint8_t precision = 2;
379 char buf[64];
380
381 if (!human_readable || count < base) {
382 fprintf(fp, "%-*"PRIu64" ", width, count);
383 return;
384 }
385
386 /* increase value by a factor of 1000/1024 and print
387 * if result is something a human can read */
388 for(;;) {
389 powi *= base;
390 if (count / base < powi)
391 break;
392
393 if (!prefix[1])
394 break;
395 ++prefix;
396 }
397
398 /* try to guess a good number of digits for precision */
399 for (; precision > 0; precision--) {
400 powj *= 10;
401 if (count / powi < powj)
402 break;
403 }
404
405 snprintf(buf, sizeof(buf), "%.*f%c%s", precision,
406 (double) count / powi, *prefix, use_iec ? "i" : "");
407
408 fprintf(fp, "%-*s ", width, buf);
409 }
410
411 static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s,
412 const struct rtattr *carrier_changes)
413 {
414 /* RX stats */
415 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
416 s->rx_compressed ? "compressed" : "", _SL_);
417
418 fprintf(fp, " ");
419 print_num(fp, 10, s->rx_bytes);
420 print_num(fp, 8, s->rx_packets);
421 print_num(fp, 7, s->rx_errors);
422 print_num(fp, 7, s->rx_dropped);
423 print_num(fp, 7, s->rx_over_errors);
424 print_num(fp, 7, s->multicast);
425 if (s->rx_compressed)
426 print_num(fp, 7, s->rx_compressed);
427
428 /* RX error stats */
429 if (show_stats > 1) {
430 fprintf(fp, "%s", _SL_);
431 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
432
433 fprintf(fp, " ");
434 print_num(fp, 8, s->rx_length_errors);
435 print_num(fp, 7, s->rx_crc_errors);
436 print_num(fp, 7, s->rx_frame_errors);
437 print_num(fp, 7, s->rx_fifo_errors);
438 print_num(fp, 7, s->rx_missed_errors);
439 }
440 fprintf(fp, "%s", _SL_);
441
442 /* TX stats */
443 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
444 s->tx_compressed ? "compressed" : "", _SL_);
445
446
447 fprintf(fp, " ");
448 print_num(fp, 10, s->tx_bytes);
449 print_num(fp, 8, s->tx_packets);
450 print_num(fp, 7, s->tx_errors);
451 print_num(fp, 7, s->tx_dropped);
452 print_num(fp, 7, s->tx_carrier_errors);
453 print_num(fp, 7, s->collisions);
454 if (s->tx_compressed)
455 print_num(fp, 7, s->tx_compressed);
456
457 /* TX error stats */
458 if (show_stats > 1) {
459 fprintf(fp, "%s", _SL_);
460 fprintf(fp, " TX errors: aborted fifo window heartbeat");
461 if (carrier_changes)
462 fprintf(fp, " transns");
463 fprintf(fp, "%s", _SL_);
464
465 fprintf(fp, " ");
466 print_num(fp, 8, s->tx_aborted_errors);
467 print_num(fp, 7, s->tx_fifo_errors);
468 print_num(fp, 7, s->tx_window_errors);
469 print_num(fp, 7, s->tx_heartbeat_errors);
470 if (carrier_changes)
471 print_num(fp, 7, *(uint32_t*)RTA_DATA(carrier_changes));
472 }
473 }
474
475 static void print_link_stats32(FILE *fp, const struct rtnl_link_stats *s,
476 const struct rtattr *carrier_changes)
477 {
478 /* RX stats */
479 fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s",
480 s->rx_compressed ? "compressed" : "", _SL_);
481
482
483 fprintf(fp, " ");
484 print_num(fp, 10, s->rx_bytes);
485 print_num(fp, 8, s->rx_packets);
486 print_num(fp, 7, s->rx_errors);
487 print_num(fp, 7, s->rx_dropped);
488 print_num(fp, 7, s->rx_over_errors);
489 print_num(fp, 7, s->multicast);
490 if (s->rx_compressed)
491 print_num(fp, 7, s->rx_compressed);
492
493 /* RX error stats */
494 if (show_stats > 1) {
495 fprintf(fp, "%s", _SL_);
496 fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_);
497 fprintf(fp, " ");
498 print_num(fp, 8, s->rx_length_errors);
499 print_num(fp, 7, s->rx_crc_errors);
500 print_num(fp, 7, s->rx_frame_errors);
501 print_num(fp, 7, s->rx_fifo_errors);
502 print_num(fp, 7, s->rx_missed_errors);
503 }
504 fprintf(fp, "%s", _SL_);
505
506 /* TX stats */
507 fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s",
508 s->tx_compressed ? "compressed" : "", _SL_);
509
510 fprintf(fp, " ");
511 print_num(fp, 10, s->tx_bytes);
512 print_num(fp, 8, s->tx_packets);
513 print_num(fp, 7, s->tx_errors);
514 print_num(fp, 7, s->tx_dropped);
515 print_num(fp, 7, s->tx_carrier_errors);
516 print_num(fp, 7, s->collisions);
517 if (s->tx_compressed)
518 print_num(fp, 7, s->tx_compressed);
519
520 /* TX error stats */
521 if (show_stats > 1) {
522 fprintf(fp, "%s", _SL_);
523 fprintf(fp, " TX errors: aborted fifo window heartbeat");
524 if (carrier_changes)
525 fprintf(fp, " transns");
526 fprintf(fp, "%s", _SL_);
527
528 fprintf(fp, " ");
529 print_num(fp, 8, s->tx_aborted_errors);
530 print_num(fp, 7, s->tx_fifo_errors);
531 print_num(fp, 7, s->tx_window_errors);
532 print_num(fp, 7, s->tx_heartbeat_errors);
533 if (carrier_changes)
534 print_num(fp, 7, *(uint32_t*)RTA_DATA(carrier_changes));
535 }
536 }
537
538 static void __print_link_stats(FILE *fp, struct rtattr **tb)
539 {
540 if (tb[IFLA_STATS64])
541 print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64]),
542 tb[IFLA_CARRIER_CHANGES]);
543 else if (tb[IFLA_STATS])
544 print_link_stats32(fp, RTA_DATA(tb[IFLA_STATS]),
545 tb[IFLA_CARRIER_CHANGES]);
546 }
547
548 static void print_link_stats(FILE *fp, struct nlmsghdr *n)
549 {
550 struct ifinfomsg *ifi = NLMSG_DATA(n);
551 struct rtattr * tb[IFLA_MAX+1];
552
553 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
554 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
555 __print_link_stats(fp, tb);
556 fprintf(fp, "%s", _SL_);
557 }
558
559 int print_linkinfo(const struct sockaddr_nl *who,
560 struct nlmsghdr *n, void *arg)
561 {
562 FILE *fp = (FILE*)arg;
563 struct ifinfomsg *ifi = NLMSG_DATA(n);
564 struct rtattr * tb[IFLA_MAX+1];
565 int len = n->nlmsg_len;
566 unsigned m_flag = 0;
567
568 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
569 return 0;
570
571 len -= NLMSG_LENGTH(sizeof(*ifi));
572 if (len < 0)
573 return -1;
574
575 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
576 return 0;
577 if (filter.up && !(ifi->ifi_flags&IFF_UP))
578 return 0;
579
580 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
581 if (tb[IFLA_IFNAME] == NULL) {
582 fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
583 }
584 if (filter.label &&
585 (!filter.family || filter.family == AF_PACKET) &&
586 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
587 return 0;
588
589 if (tb[IFLA_GROUP]) {
590 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
591 if (filter.group != -1 && group != filter.group)
592 return -1;
593 }
594
595 if (tb[IFLA_MASTER]) {
596 int master = *(int*)RTA_DATA(tb[IFLA_MASTER]);
597 if (filter.master > 0 && master != filter.master)
598 return -1;
599 }
600 else if (filter.master > 0)
601 return -1;
602
603 if (filter.kind) {
604 if (tb[IFLA_LINKINFO]) {
605 char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
606
607 if (strcmp(kind, filter.kind))
608 return -1;
609 } else {
610 return -1;
611 }
612 }
613
614 if (n->nlmsg_type == RTM_DELLINK)
615 fprintf(fp, "Deleted ");
616
617 fprintf(fp, "%d: ", ifi->ifi_index);
618 color_fprintf(fp, COLOR_IFNAME, "%s",
619 tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
620
621 if (tb[IFLA_LINK]) {
622 SPRINT_BUF(b1);
623 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
624 if (iflink == 0)
625 fprintf(fp, "@NONE: ");
626 else {
627 if (tb[IFLA_LINK_NETNSID])
628 fprintf(fp, "@if%d: ", iflink);
629 else {
630 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
631 m_flag = ll_index_to_flags(iflink);
632 m_flag = !(m_flag & IFF_UP);
633 }
634 }
635 } else {
636 fprintf(fp, ": ");
637 }
638 print_link_flags(fp, ifi->ifi_flags, m_flag);
639
640 if (tb[IFLA_MTU])
641 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
642 if (tb[IFLA_QDISC])
643 fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC]));
644 if (tb[IFLA_MASTER]) {
645 SPRINT_BUF(b1);
646 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
647 }
648
649 if (tb[IFLA_PHYS_PORT_ID]) {
650 SPRINT_BUF(b1);
651 fprintf(fp, "portid %s ",
652 hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_PORT_ID]),
653 RTA_PAYLOAD(tb[IFLA_PHYS_PORT_ID]),
654 b1, sizeof(b1)));
655 }
656
657 if (tb[IFLA_OPERSTATE])
658 print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
659
660 if (do_link && tb[IFLA_LINKMODE])
661 print_linkmode(fp, tb[IFLA_LINKMODE]);
662
663 if (tb[IFLA_GROUP]) {
664 SPRINT_BUF(b1);
665 int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
666 fprintf(fp, "group %s ", rtnl_group_n2a(group, b1, sizeof(b1)));
667 }
668
669 if (filter.showqueue)
670 print_queuelen(fp, tb);
671
672 if (!filter.family || filter.family == AF_PACKET || show_details) {
673 SPRINT_BUF(b1);
674 fprintf(fp, "%s", _SL_);
675 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
676
677 if (tb[IFLA_ADDRESS]) {
678 color_fprintf(fp, COLOR_MAC, "%s",
679 ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
680 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
681 ifi->ifi_type,
682 b1, sizeof(b1)));
683 }
684 if (tb[IFLA_BROADCAST]) {
685 if (ifi->ifi_flags&IFF_POINTOPOINT)
686 fprintf(fp, " peer ");
687 else
688 fprintf(fp, " brd ");
689 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
690 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
691 ifi->ifi_type,
692 b1, sizeof(b1)));
693 }
694 }
695
696 if (tb[IFLA_LINK_NETNSID]) {
697 int id = *(int*)RTA_DATA(tb[IFLA_LINK_NETNSID]);
698
699 if (id >= 0)
700 fprintf(fp, " link-netnsid %d", id);
701 else
702 fprintf(fp, " link-netnsid unknown");
703 }
704
705 if (tb[IFLA_PROMISCUITY] && show_details)
706 fprintf(fp, " promiscuity %u ",
707 *(int*)RTA_DATA(tb[IFLA_PROMISCUITY]));
708
709 if (tb[IFLA_LINKINFO] && show_details)
710 print_linktype(fp, tb[IFLA_LINKINFO]);
711
712 if (do_link && tb[IFLA_AF_SPEC] && show_details)
713 print_af_spec(fp, tb[IFLA_AF_SPEC]);
714
715 if ((do_link || show_details) && tb[IFLA_IFALIAS]) {
716 fprintf(fp, "%s alias %s", _SL_,
717 rta_getattr_str(tb[IFLA_IFALIAS]));
718 }
719
720 if (do_link && show_stats) {
721 fprintf(fp, "%s", _SL_);
722 __print_link_stats(fp, tb);
723 }
724
725 if ((do_link || show_details) && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
726 struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
727 int rem = RTA_PAYLOAD(vflist);
728 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
729 print_vfinfo(fp, i);
730 }
731
732 fprintf(fp, "\n");
733 fflush(fp);
734 return 1;
735 }
736
737 static int flush_update(void)
738 {
739 if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
740 perror("Failed to send flush request");
741 return -1;
742 }
743 filter.flushp = 0;
744 return 0;
745 }
746
747 static int set_lifetime(unsigned int *lifetime, char *argv)
748 {
749 if (strcmp(argv, "forever") == 0)
750 *lifetime = INFINITY_LIFE_TIME;
751 else if (get_u32(lifetime, argv, 0))
752 return -1;
753
754 return 0;
755 }
756
757 static unsigned int get_ifa_flags(struct ifaddrmsg *ifa,
758 struct rtattr *ifa_flags_attr)
759 {
760 return ifa_flags_attr ? rta_getattr_u32(ifa_flags_attr) :
761 ifa->ifa_flags;
762 }
763
764 int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
765 void *arg)
766 {
767 FILE *fp = arg;
768 struct ifaddrmsg *ifa = NLMSG_DATA(n);
769 int len = n->nlmsg_len;
770 int deprecated = 0;
771 /* Use local copy of ifa_flags to not interfere with filtering code */
772 unsigned int ifa_flags;
773 struct rtattr * rta_tb[IFA_MAX+1];
774 char abuf[256];
775 SPRINT_BUF(b1);
776
777 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
778 return 0;
779 len -= NLMSG_LENGTH(sizeof(*ifa));
780 if (len < 0) {
781 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
782 return -1;
783 }
784
785 if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
786 return 0;
787
788 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa),
789 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
790
791 ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]);
792
793 if (!rta_tb[IFA_LOCAL])
794 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
795 if (!rta_tb[IFA_ADDRESS])
796 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
797
798 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
799 return 0;
800 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
801 return 0;
802 if ((filter.flags ^ ifa_flags) & filter.flagmask)
803 return 0;
804 if (filter.label) {
805 SPRINT_BUF(b1);
806 const char *label;
807 if (rta_tb[IFA_LABEL])
808 label = RTA_DATA(rta_tb[IFA_LABEL]);
809 else
810 label = ll_idx_n2a(ifa->ifa_index, b1);
811 if (fnmatch(filter.label, label, 0) != 0)
812 return 0;
813 }
814 if (filter.pfx.family) {
815 if (rta_tb[IFA_LOCAL]) {
816 inet_prefix dst;
817 memset(&dst, 0, sizeof(dst));
818 dst.family = ifa->ifa_family;
819 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
820 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
821 return 0;
822 }
823 }
824
825 if (filter.family && filter.family != ifa->ifa_family)
826 return 0;
827
828 if (filter.flushb) {
829 struct nlmsghdr *fn;
830 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
831 if (flush_update())
832 return -1;
833 }
834 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
835 memcpy(fn, n, n->nlmsg_len);
836 fn->nlmsg_type = RTM_DELADDR;
837 fn->nlmsg_flags = NLM_F_REQUEST;
838 fn->nlmsg_seq = ++rth.seq;
839 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
840 filter.flushed++;
841 if (show_stats < 2)
842 return 0;
843 }
844
845 if (n->nlmsg_type == RTM_DELADDR)
846 fprintf(fp, "Deleted ");
847
848 if (filter.oneline || filter.flushb)
849 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
850 if (ifa->ifa_family == AF_INET)
851 fprintf(fp, " inet ");
852 else if (ifa->ifa_family == AF_INET6)
853 fprintf(fp, " inet6 ");
854 else if (ifa->ifa_family == AF_DECnet)
855 fprintf(fp, " dnet ");
856 else if (ifa->ifa_family == AF_IPX)
857 fprintf(fp, " ipx ");
858 else
859 fprintf(fp, " family %d ", ifa->ifa_family);
860
861 if (rta_tb[IFA_LOCAL]) {
862 if (ifa->ifa_family == AF_INET)
863 color_fprintf(fp, COLOR_INET, "%s", format_host(ifa->ifa_family,
864 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
865 RTA_DATA(rta_tb[IFA_LOCAL]),
866 abuf, sizeof(abuf)));
867 else if (ifa->ifa_family == AF_INET6)
868 color_fprintf(fp, COLOR_INET6, "%s", format_host(ifa->ifa_family,
869 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
870 RTA_DATA(rta_tb[IFA_LOCAL]),
871 abuf, sizeof(abuf)));
872 else
873 fprintf(fp, "%s", format_host(ifa->ifa_family,
874 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
875 RTA_DATA(rta_tb[IFA_LOCAL]),
876 abuf, sizeof(abuf)));
877
878 if (rta_tb[IFA_ADDRESS] == NULL ||
879 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]),
880 ifa->ifa_family == AF_INET ? 4 : 16) == 0) {
881 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
882 } else {
883 fprintf(fp, " peer %s/%d ",
884 format_host(ifa->ifa_family,
885 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
886 RTA_DATA(rta_tb[IFA_ADDRESS]),
887 abuf, sizeof(abuf)),
888 ifa->ifa_prefixlen);
889 }
890 }
891
892 if (rta_tb[IFA_BROADCAST]) {
893 fprintf(fp, "brd %s ",
894 format_host(ifa->ifa_family,
895 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
896 RTA_DATA(rta_tb[IFA_BROADCAST]),
897 abuf, sizeof(abuf)));
898 }
899 if (rta_tb[IFA_ANYCAST]) {
900 fprintf(fp, "any %s ",
901 format_host(ifa->ifa_family,
902 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
903 RTA_DATA(rta_tb[IFA_ANYCAST]),
904 abuf, sizeof(abuf)));
905 }
906 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
907 if (ifa_flags & IFA_F_SECONDARY) {
908 ifa_flags &= ~IFA_F_SECONDARY;
909 if (ifa->ifa_family == AF_INET6)
910 fprintf(fp, "temporary ");
911 else
912 fprintf(fp, "secondary ");
913 }
914 if (ifa_flags & IFA_F_TENTATIVE) {
915 ifa_flags &= ~IFA_F_TENTATIVE;
916 fprintf(fp, "tentative ");
917 }
918 if (ifa_flags & IFA_F_DEPRECATED) {
919 ifa_flags &= ~IFA_F_DEPRECATED;
920 deprecated = 1;
921 fprintf(fp, "deprecated ");
922 }
923 if (ifa_flags & IFA_F_HOMEADDRESS) {
924 ifa_flags &= ~IFA_F_HOMEADDRESS;
925 fprintf(fp, "home ");
926 }
927 if (ifa_flags & IFA_F_NODAD) {
928 ifa_flags &= ~IFA_F_NODAD;
929 fprintf(fp, "nodad ");
930 }
931 if (ifa_flags & IFA_F_MANAGETEMPADDR) {
932 ifa_flags &= ~IFA_F_MANAGETEMPADDR;
933 fprintf(fp, "mngtmpaddr ");
934 }
935 if (ifa_flags & IFA_F_NOPREFIXROUTE) {
936 ifa_flags &= ~IFA_F_NOPREFIXROUTE;
937 fprintf(fp, "noprefixroute ");
938 }
939 if (ifa_flags & IFA_F_MCAUTOJOIN) {
940 ifa_flags &= ~IFA_F_MCAUTOJOIN;
941 fprintf(fp, "autojoin ");
942 }
943 if (!(ifa_flags & IFA_F_PERMANENT)) {
944 fprintf(fp, "dynamic ");
945 } else
946 ifa_flags &= ~IFA_F_PERMANENT;
947 if (ifa_flags & IFA_F_DADFAILED) {
948 ifa_flags &= ~IFA_F_DADFAILED;
949 fprintf(fp, "dadfailed ");
950 }
951 if (ifa_flags)
952 fprintf(fp, "flags %02x ", ifa_flags);
953 if (rta_tb[IFA_LABEL])
954 fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL]));
955 if (rta_tb[IFA_CACHEINFO]) {
956 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
957 fprintf(fp, "%s", _SL_);
958 fprintf(fp, " valid_lft ");
959 if (ci->ifa_valid == INFINITY_LIFE_TIME)
960 fprintf(fp, "forever");
961 else
962 fprintf(fp, "%usec", ci->ifa_valid);
963 fprintf(fp, " preferred_lft ");
964 if (ci->ifa_prefered == INFINITY_LIFE_TIME)
965 fprintf(fp, "forever");
966 else {
967 if (deprecated)
968 fprintf(fp, "%dsec", ci->ifa_prefered);
969 else
970 fprintf(fp, "%usec", ci->ifa_prefered);
971 }
972 }
973 fprintf(fp, "\n");
974 fflush(fp);
975 return 0;
976 }
977
978 static int print_addrinfo_primary(const struct sockaddr_nl *who,
979 struct nlmsghdr *n, void *arg)
980 {
981 struct ifaddrmsg *ifa = NLMSG_DATA(n);
982
983 if (ifa->ifa_flags & IFA_F_SECONDARY)
984 return 0;
985
986 return print_addrinfo(who, n, arg);
987 }
988
989 static int print_addrinfo_secondary(const struct sockaddr_nl *who,
990 struct nlmsghdr *n, void *arg)
991 {
992 struct ifaddrmsg *ifa = NLMSG_DATA(n);
993
994 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
995 return 0;
996
997 return print_addrinfo(who, n, arg);
998 }
999
1000 struct nlmsg_list
1001 {
1002 struct nlmsg_list *next;
1003 struct nlmsghdr h;
1004 };
1005
1006 struct nlmsg_chain
1007 {
1008 struct nlmsg_list *head;
1009 struct nlmsg_list *tail;
1010 };
1011
1012 static int print_selected_addrinfo(struct ifinfomsg *ifi,
1013 struct nlmsg_list *ainfo, FILE *fp)
1014 {
1015 for ( ;ainfo ; ainfo = ainfo->next) {
1016 struct nlmsghdr *n = &ainfo->h;
1017 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1018
1019 if (n->nlmsg_type != RTM_NEWADDR)
1020 continue;
1021
1022 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
1023 return -1;
1024
1025 if (ifa->ifa_index != ifi->ifi_index ||
1026 (filter.family && filter.family != ifa->ifa_family))
1027 continue;
1028
1029 if (filter.up && !(ifi->ifi_flags&IFF_UP))
1030 continue;
1031
1032 print_addrinfo(NULL, n, fp);
1033 }
1034 return 0;
1035 }
1036
1037
1038 static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
1039 void *arg)
1040 {
1041 struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg;
1042 struct nlmsg_list *h;
1043
1044 h = malloc(n->nlmsg_len+sizeof(void*));
1045 if (h == NULL)
1046 return -1;
1047
1048 memcpy(&h->h, n, n->nlmsg_len);
1049 h->next = NULL;
1050
1051 if (lchain->tail)
1052 lchain->tail->next = h;
1053 else
1054 lchain->head = h;
1055 lchain->tail = h;
1056
1057 ll_remember_index(who, n, NULL);
1058 return 0;
1059 }
1060
1061 static __u32 ipadd_dump_magic = 0x47361222;
1062
1063 static int ipadd_save_prep(void)
1064 {
1065 int ret;
1066
1067 if (isatty(STDOUT_FILENO)) {
1068 fprintf(stderr, "Not sending a binary stream to stdout\n");
1069 return -1;
1070 }
1071
1072 ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic));
1073 if (ret != sizeof(ipadd_dump_magic)) {
1074 fprintf(stderr, "Can't write magic to dump file\n");
1075 return -1;
1076 }
1077
1078 return 0;
1079 }
1080
1081 static int ipadd_dump_check_magic(void)
1082 {
1083 int ret;
1084 __u32 magic = 0;
1085
1086 if (isatty(STDIN_FILENO)) {
1087 fprintf(stderr, "Can't restore addr dump from a terminal\n");
1088 return -1;
1089 }
1090
1091 ret = fread(&magic, sizeof(magic), 1, stdin);
1092 if (magic != ipadd_dump_magic) {
1093 fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic);
1094 return -1;
1095 }
1096
1097 return 0;
1098 }
1099
1100 static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
1101 void *arg)
1102 {
1103 int ret;
1104
1105 ret = write(STDOUT_FILENO, n, n->nlmsg_len);
1106 if ((ret > 0) && (ret != n->nlmsg_len)) {
1107 fprintf(stderr, "Short write while saving nlmsg\n");
1108 ret = -EIO;
1109 }
1110
1111 return ret == n->nlmsg_len ? 0 : ret;
1112 }
1113
1114 static int show_handler(const struct sockaddr_nl *nl,
1115 struct rtnl_ctrl_data *ctrl,
1116 struct nlmsghdr *n, void *arg)
1117 {
1118 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1119
1120 printf("if%d:\n", ifa->ifa_index);
1121 print_addrinfo(NULL, n, stdout);
1122 return 0;
1123 }
1124
1125 static int ipaddr_showdump(void)
1126 {
1127 if (ipadd_dump_check_magic())
1128 exit(-1);
1129
1130 exit(rtnl_from_file(stdin, &show_handler, NULL));
1131 }
1132
1133 static int restore_handler(const struct sockaddr_nl *nl,
1134 struct rtnl_ctrl_data *ctrl,
1135 struct nlmsghdr *n, void *arg)
1136 {
1137 int ret;
1138
1139 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
1140
1141 ll_init_map(&rth);
1142
1143 ret = rtnl_talk(&rth, n, n, sizeof(*n));
1144 if ((ret < 0) && (errno == EEXIST))
1145 ret = 0;
1146
1147 return ret;
1148 }
1149
1150 static int ipaddr_restore(void)
1151 {
1152 if (ipadd_dump_check_magic())
1153 exit(-1);
1154
1155 exit(rtnl_from_file(stdin, &restore_handler, NULL));
1156 }
1157
1158 static void free_nlmsg_chain(struct nlmsg_chain *info)
1159 {
1160 struct nlmsg_list *l, *n;
1161
1162 for (l = info->head; l; l = n) {
1163 n = l->next;
1164 free(l);
1165 }
1166 }
1167
1168 static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
1169 {
1170 struct nlmsg_list *l, **lp;
1171
1172 lp = &linfo->head;
1173 while ( (l = *lp) != NULL) {
1174 int ok = 0;
1175 int missing_net_address = 1;
1176 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1177 struct nlmsg_list *a;
1178
1179 for (a = ainfo->head; a; a = a->next) {
1180 struct nlmsghdr *n = &a->h;
1181 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1182 struct rtattr *tb[IFA_MAX + 1];
1183 unsigned int ifa_flags;
1184
1185 if (ifa->ifa_index != ifi->ifi_index)
1186 continue;
1187 missing_net_address = 0;
1188 if (filter.family && filter.family != ifa->ifa_family)
1189 continue;
1190 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1191 continue;
1192
1193 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
1194 ifa_flags = get_ifa_flags(ifa, tb[IFA_FLAGS]);
1195
1196 if ((filter.flags ^ ifa_flags) & filter.flagmask)
1197 continue;
1198 if (filter.pfx.family || filter.label) {
1199 if (!tb[IFA_LOCAL])
1200 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1201
1202 if (filter.pfx.family && tb[IFA_LOCAL]) {
1203 inet_prefix dst;
1204 memset(&dst, 0, sizeof(dst));
1205 dst.family = ifa->ifa_family;
1206 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
1207 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1208 continue;
1209 }
1210 if (filter.label) {
1211 SPRINT_BUF(b1);
1212 const char *label;
1213 if (tb[IFA_LABEL])
1214 label = RTA_DATA(tb[IFA_LABEL]);
1215 else
1216 label = ll_idx_n2a(ifa->ifa_index, b1);
1217 if (fnmatch(filter.label, label, 0) != 0)
1218 continue;
1219 }
1220 }
1221
1222 ok = 1;
1223 break;
1224 }
1225 if (missing_net_address &&
1226 (filter.family == AF_UNSPEC || filter.family == AF_PACKET))
1227 ok = 1;
1228 if (!ok) {
1229 *lp = l->next;
1230 free(l);
1231 } else
1232 lp = &l->next;
1233 }
1234 }
1235
1236 static int ipaddr_flush(void)
1237 {
1238 int round = 0;
1239 char flushb[4096-512];
1240
1241 filter.flushb = flushb;
1242 filter.flushp = 0;
1243 filter.flushe = sizeof(flushb);
1244
1245 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
1246 const struct rtnl_dump_filter_arg a[3] = {
1247 {
1248 .filter = print_addrinfo_secondary,
1249 .arg1 = stdout,
1250 },
1251 {
1252 .filter = print_addrinfo_primary,
1253 .arg1 = stdout,
1254 },
1255 {
1256 .filter = NULL,
1257 .arg1 = NULL,
1258 },
1259 };
1260 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1261 perror("Cannot send dump request");
1262 exit(1);
1263 }
1264 filter.flushed = 0;
1265 if (rtnl_dump_filter_l(&rth, a) < 0) {
1266 fprintf(stderr, "Flush terminated\n");
1267 exit(1);
1268 }
1269 if (filter.flushed == 0) {
1270 flush_done:
1271 if (show_stats) {
1272 if (round == 0)
1273 printf("Nothing to flush.\n");
1274 else
1275 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
1276 }
1277 fflush(stdout);
1278 return 0;
1279 }
1280 round++;
1281 if (flush_update() < 0)
1282 return 1;
1283
1284 if (show_stats) {
1285 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
1286 fflush(stdout);
1287 }
1288
1289 /* If we are flushing, and specifying primary, then we
1290 * want to flush only a single round. Otherwise, we'll
1291 * start flushing secondaries that were promoted to
1292 * primaries.
1293 */
1294 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
1295 goto flush_done;
1296 }
1297 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
1298 fflush(stderr);
1299 return 1;
1300 }
1301
1302 static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
1303 {
1304 struct nlmsg_chain linfo = { NULL, NULL};
1305 struct nlmsg_chain ainfo = { NULL, NULL};
1306 struct nlmsg_list *l;
1307 char *filter_dev = NULL;
1308 int no_link = 0;
1309
1310 ipaddr_reset_filter(oneline, 0);
1311 filter.showqueue = 1;
1312
1313 if (filter.family == AF_UNSPEC)
1314 filter.family = preferred_family;
1315
1316 filter.group = -1;
1317
1318 if (action == IPADD_FLUSH) {
1319 if (argc <= 0) {
1320 fprintf(stderr, "Flush requires arguments.\n");
1321
1322 return -1;
1323 }
1324 if (filter.family == AF_PACKET) {
1325 fprintf(stderr, "Cannot flush link addresses.\n");
1326 return -1;
1327 }
1328 }
1329
1330 while (argc > 0) {
1331 if (strcmp(*argv, "to") == 0) {
1332 NEXT_ARG();
1333 get_prefix(&filter.pfx, *argv, filter.family);
1334 if (filter.family == AF_UNSPEC)
1335 filter.family = filter.pfx.family;
1336 } else if (strcmp(*argv, "scope") == 0) {
1337 unsigned scope = 0;
1338 NEXT_ARG();
1339 filter.scopemask = -1;
1340 if (rtnl_rtscope_a2n(&scope, *argv)) {
1341 if (strcmp(*argv, "all") != 0)
1342 invarg("invalid \"scope\"\n", *argv);
1343 scope = RT_SCOPE_NOWHERE;
1344 filter.scopemask = 0;
1345 }
1346 filter.scope = scope;
1347 } else if (strcmp(*argv, "up") == 0) {
1348 filter.up = 1;
1349 } else if (strcmp(*argv, "dynamic") == 0) {
1350 filter.flags &= ~IFA_F_PERMANENT;
1351 filter.flagmask |= IFA_F_PERMANENT;
1352 } else if (strcmp(*argv, "permanent") == 0) {
1353 filter.flags |= IFA_F_PERMANENT;
1354 filter.flagmask |= IFA_F_PERMANENT;
1355 } else if (strcmp(*argv, "secondary") == 0 ||
1356 strcmp(*argv, "temporary") == 0) {
1357 filter.flags |= IFA_F_SECONDARY;
1358 filter.flagmask |= IFA_F_SECONDARY;
1359 } else if (strcmp(*argv, "primary") == 0) {
1360 filter.flags &= ~IFA_F_SECONDARY;
1361 filter.flagmask |= IFA_F_SECONDARY;
1362 } else if (strcmp(*argv, "tentative") == 0) {
1363 filter.flags |= IFA_F_TENTATIVE;
1364 filter.flagmask |= IFA_F_TENTATIVE;
1365 } else if (strcmp(*argv, "-tentative") == 0) {
1366 filter.flags &= ~IFA_F_TENTATIVE;
1367 filter.flagmask |= IFA_F_TENTATIVE;
1368 } else if (strcmp(*argv, "deprecated") == 0) {
1369 filter.flags |= IFA_F_DEPRECATED;
1370 filter.flagmask |= IFA_F_DEPRECATED;
1371 } else if (strcmp(*argv, "-deprecated") == 0) {
1372 filter.flags &= ~IFA_F_DEPRECATED;
1373 filter.flagmask |= IFA_F_DEPRECATED;
1374 } else if (strcmp(*argv, "home") == 0) {
1375 filter.flags |= IFA_F_HOMEADDRESS;
1376 filter.flagmask |= IFA_F_HOMEADDRESS;
1377 } else if (strcmp(*argv, "nodad") == 0) {
1378 filter.flags |= IFA_F_NODAD;
1379 filter.flagmask |= IFA_F_NODAD;
1380 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1381 filter.flags |= IFA_F_MANAGETEMPADDR;
1382 filter.flagmask |= IFA_F_MANAGETEMPADDR;
1383 } else if (strcmp(*argv, "noprefixroute") == 0) {
1384 filter.flags |= IFA_F_NOPREFIXROUTE;
1385 filter.flagmask |= IFA_F_NOPREFIXROUTE;
1386 } else if (strcmp(*argv, "autojoin") == 0) {
1387 filter.flags |= IFA_F_MCAUTOJOIN;
1388 filter.flagmask |= IFA_F_MCAUTOJOIN;
1389 } else if (strcmp(*argv, "dadfailed") == 0) {
1390 filter.flags |= IFA_F_DADFAILED;
1391 filter.flagmask |= IFA_F_DADFAILED;
1392 } else if (strcmp(*argv, "-dadfailed") == 0) {
1393 filter.flags &= ~IFA_F_DADFAILED;
1394 filter.flagmask |= IFA_F_DADFAILED;
1395 } else if (strcmp(*argv, "label") == 0) {
1396 NEXT_ARG();
1397 filter.label = *argv;
1398 } else if (strcmp(*argv, "group") == 0) {
1399 NEXT_ARG();
1400 if (rtnl_group_a2n(&filter.group, *argv))
1401 invarg("Invalid \"group\" value\n", *argv);
1402 } else if (strcmp(*argv, "master") == 0) {
1403 int ifindex;
1404 NEXT_ARG();
1405 ifindex = ll_name_to_index(*argv);
1406 if (!ifindex)
1407 invarg("Device does not exist\n", *argv);
1408 filter.master = ifindex;
1409 } else if (do_link && strcmp(*argv, "type") == 0) {
1410 NEXT_ARG();
1411 filter.kind = *argv;
1412 } else {
1413 if (strcmp(*argv, "dev") == 0) {
1414 NEXT_ARG();
1415 }
1416 if (matches(*argv, "help") == 0)
1417 usage();
1418 if (filter_dev)
1419 duparg2("dev", *argv);
1420 filter_dev = *argv;
1421 }
1422 argv++; argc--;
1423 }
1424
1425 if (filter_dev) {
1426 filter.ifindex = ll_name_to_index(filter_dev);
1427 if (filter.ifindex <= 0) {
1428 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
1429 return -1;
1430 }
1431 }
1432
1433 if (action == IPADD_FLUSH)
1434 return ipaddr_flush();
1435
1436 if (action == IPADD_SAVE) {
1437 if (ipadd_save_prep())
1438 exit(1);
1439
1440 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
1441 perror("Cannot send dump request");
1442 exit(1);
1443 }
1444
1445 if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) {
1446 fprintf(stderr, "Save terminated\n");
1447 exit(1);
1448 }
1449
1450 exit(0);
1451 }
1452
1453 /*
1454 * If only filter_dev present and none of the other
1455 * link filters are present, use RTM_GETLINK to get
1456 * the link device
1457 */
1458 if (filter_dev && filter.group == -1 && do_link == 1) {
1459 if (iplink_get(0, filter_dev, RTEXT_FILTER_VF) < 0) {
1460 perror("Cannot send link get request");
1461 exit(1);
1462 }
1463 exit(0);
1464 }
1465
1466 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
1467 perror("Cannot send dump request");
1468 exit(1);
1469 }
1470
1471 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1472 fprintf(stderr, "Dump terminated\n");
1473 exit(1);
1474 }
1475
1476 if (filter.family != AF_PACKET) {
1477 if (filter.oneline)
1478 no_link = 1;
1479
1480 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1481 perror("Cannot send dump request");
1482 exit(1);
1483 }
1484
1485 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
1486 fprintf(stderr, "Dump terminated\n");
1487 exit(1);
1488 }
1489
1490 ipaddr_filter(&linfo, &ainfo);
1491 }
1492
1493 for (l = linfo.head; l; l = l->next) {
1494 int res = 0;
1495
1496 if (no_link || (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) {
1497 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1498 if (filter.family != AF_PACKET)
1499 print_selected_addrinfo(ifi,
1500 ainfo.head, stdout);
1501 if (res > 0 && !do_link && show_stats)
1502 print_link_stats(stdout, &l->h);
1503 }
1504 }
1505 fflush(stdout);
1506
1507 free_nlmsg_chain(&ainfo);
1508 free_nlmsg_chain(&linfo);
1509
1510 return 0;
1511 }
1512
1513 static void
1514 ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max)
1515 {
1516 struct rtattr *vflist = tb[IFLA_VFINFO_LIST];
1517 struct rtattr *i, *vf[IFLA_VF_MAX+1];
1518 struct ifla_vf_rate *vf_rate;
1519 int rem;
1520
1521 rem = RTA_PAYLOAD(vflist);
1522
1523 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
1524 parse_rtattr_nested(vf, IFLA_VF_MAX, i);
1525 vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
1526 if (vf_rate->vf == vfnum) {
1527 *min = vf_rate->min_tx_rate;
1528 *max = vf_rate->max_tx_rate;
1529 return;
1530 }
1531 }
1532 fprintf(stderr, "Cannot find VF %d\n", vfnum);
1533 exit(1);
1534 }
1535
1536 void ipaddr_get_vf_rate(int vfnum, int *min, int *max, int idx)
1537 {
1538 struct nlmsg_chain linfo = { NULL, NULL};
1539 struct rtattr *tb[IFLA_MAX+1];
1540 struct ifinfomsg *ifi;
1541 struct nlmsg_list *l;
1542 struct nlmsghdr *n;
1543 int len;
1544
1545 if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
1546 perror("Cannot send dump request");
1547 exit(1);
1548 }
1549 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1550 fprintf(stderr, "Dump terminated\n");
1551 exit(1);
1552 }
1553 for (l = linfo.head; l; l = l->next) {
1554 n = &l->h;
1555 ifi = NLMSG_DATA(n);
1556
1557 len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
1558 if (len < 0 || (idx && idx != ifi->ifi_index))
1559 continue;
1560
1561 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1562
1563 if ((tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF])) {
1564 ipaddr_loop_each_vf(tb, vfnum, min, max);
1565 return;
1566 }
1567 }
1568 }
1569
1570 int ipaddr_list_link(int argc, char **argv)
1571 {
1572 preferred_family = AF_PACKET;
1573 do_link = 1;
1574 return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST);
1575 }
1576
1577 void ipaddr_reset_filter(int oneline, int ifindex)
1578 {
1579 memset(&filter, 0, sizeof(filter));
1580 filter.oneline = oneline;
1581 filter.ifindex = ifindex;
1582 }
1583
1584 static int default_scope(inet_prefix *lcl)
1585 {
1586 if (lcl->family == AF_INET) {
1587 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
1588 return RT_SCOPE_HOST;
1589 }
1590 return 0;
1591 }
1592
1593 static bool ipaddr_is_multicast(inet_prefix *a)
1594 {
1595 if (a->family == AF_INET)
1596 return IN_MULTICAST(ntohl(a->data[0]));
1597 else if (a->family == AF_INET6)
1598 return IN6_IS_ADDR_MULTICAST(a->data);
1599 else
1600 return false;
1601 }
1602
1603 static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
1604 {
1605 struct {
1606 struct nlmsghdr n;
1607 struct ifaddrmsg ifa;
1608 char buf[256];
1609 } req;
1610 char *d = NULL;
1611 char *l = NULL;
1612 char *lcl_arg = NULL;
1613 char *valid_lftp = NULL;
1614 char *preferred_lftp = NULL;
1615 inet_prefix lcl;
1616 inet_prefix peer;
1617 int local_len = 0;
1618 int peer_len = 0;
1619 int brd_len = 0;
1620 int any_len = 0;
1621 int scoped = 0;
1622 __u32 preferred_lft = INFINITY_LIFE_TIME;
1623 __u32 valid_lft = INFINITY_LIFE_TIME;
1624 struct ifa_cacheinfo cinfo;
1625 unsigned int ifa_flags = 0;
1626
1627 memset(&req, 0, sizeof(req));
1628
1629 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1630 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
1631 req.n.nlmsg_type = cmd;
1632 req.ifa.ifa_family = preferred_family;
1633
1634 while (argc > 0) {
1635 if (strcmp(*argv, "peer") == 0 ||
1636 strcmp(*argv, "remote") == 0) {
1637 NEXT_ARG();
1638
1639 if (peer_len)
1640 duparg("peer", *argv);
1641 get_prefix(&peer, *argv, req.ifa.ifa_family);
1642 peer_len = peer.bytelen;
1643 if (req.ifa.ifa_family == AF_UNSPEC)
1644 req.ifa.ifa_family = peer.family;
1645 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1646 req.ifa.ifa_prefixlen = peer.bitlen;
1647 } else if (matches(*argv, "broadcast") == 0 ||
1648 strcmp(*argv, "brd") == 0) {
1649 inet_prefix addr;
1650 NEXT_ARG();
1651 if (brd_len)
1652 duparg("broadcast", *argv);
1653 if (strcmp(*argv, "+") == 0)
1654 brd_len = -1;
1655 else if (strcmp(*argv, "-") == 0)
1656 brd_len = -2;
1657 else {
1658 get_addr(&addr, *argv, req.ifa.ifa_family);
1659 if (req.ifa.ifa_family == AF_UNSPEC)
1660 req.ifa.ifa_family = addr.family;
1661 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1662 brd_len = addr.bytelen;
1663 }
1664 } else if (strcmp(*argv, "anycast") == 0) {
1665 inet_prefix addr;
1666 NEXT_ARG();
1667 if (any_len)
1668 duparg("anycast", *argv);
1669 get_addr(&addr, *argv, req.ifa.ifa_family);
1670 if (req.ifa.ifa_family == AF_UNSPEC)
1671 req.ifa.ifa_family = addr.family;
1672 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1673 any_len = addr.bytelen;
1674 } else if (strcmp(*argv, "scope") == 0) {
1675 unsigned scope = 0;
1676 NEXT_ARG();
1677 if (rtnl_rtscope_a2n(&scope, *argv))
1678 invarg("invalid scope value.", *argv);
1679 req.ifa.ifa_scope = scope;
1680 scoped = 1;
1681 } else if (strcmp(*argv, "dev") == 0) {
1682 NEXT_ARG();
1683 d = *argv;
1684 } else if (strcmp(*argv, "label") == 0) {
1685 NEXT_ARG();
1686 l = *argv;
1687 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
1688 } else if (matches(*argv, "valid_lft") == 0) {
1689 if (valid_lftp)
1690 duparg("valid_lft", *argv);
1691 NEXT_ARG();
1692 valid_lftp = *argv;
1693 if (set_lifetime(&valid_lft, *argv))
1694 invarg("valid_lft value", *argv);
1695 } else if (matches(*argv, "preferred_lft") == 0) {
1696 if (preferred_lftp)
1697 duparg("preferred_lft", *argv);
1698 NEXT_ARG();
1699 preferred_lftp = *argv;
1700 if (set_lifetime(&preferred_lft, *argv))
1701 invarg("preferred_lft value", *argv);
1702 } else if (strcmp(*argv, "home") == 0) {
1703 ifa_flags |= IFA_F_HOMEADDRESS;
1704 } else if (strcmp(*argv, "nodad") == 0) {
1705 ifa_flags |= IFA_F_NODAD;
1706 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1707 ifa_flags |= IFA_F_MANAGETEMPADDR;
1708 } else if (strcmp(*argv, "noprefixroute") == 0) {
1709 ifa_flags |= IFA_F_NOPREFIXROUTE;
1710 } else if (strcmp(*argv, "autojoin") == 0) {
1711 ifa_flags |= IFA_F_MCAUTOJOIN;
1712 } else {
1713 if (strcmp(*argv, "local") == 0) {
1714 NEXT_ARG();
1715 }
1716 if (matches(*argv, "help") == 0)
1717 usage();
1718 if (local_len)
1719 duparg2("local", *argv);
1720 lcl_arg = *argv;
1721 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1722 if (req.ifa.ifa_family == AF_UNSPEC)
1723 req.ifa.ifa_family = lcl.family;
1724 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1725 local_len = lcl.bytelen;
1726 }
1727 argc--; argv++;
1728 }
1729 if (ifa_flags <= 0xff)
1730 req.ifa.ifa_flags = ifa_flags;
1731 else
1732 addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
1733
1734 if (d == NULL) {
1735 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1736 return -1;
1737 }
1738 if (l && matches(d, l) != 0) {
1739 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
1740 return -1;
1741 }
1742
1743 if (peer_len == 0 && local_len) {
1744 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1745 fprintf(stderr,
1746 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1747 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1748 " This special behaviour is likely to disappear in further releases,\n" \
1749 " fix your scripts!\n", lcl_arg, local_len*8);
1750 } else {
1751 peer = lcl;
1752 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1753 }
1754 }
1755 if (req.ifa.ifa_prefixlen == 0)
1756 req.ifa.ifa_prefixlen = lcl.bitlen;
1757
1758 if (brd_len < 0 && cmd != RTM_DELADDR) {
1759 inet_prefix brd;
1760 int i;
1761 if (req.ifa.ifa_family != AF_INET) {
1762 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1763 return -1;
1764 }
1765 brd = peer;
1766 if (brd.bitlen <= 30) {
1767 for (i = 31; i >= brd.bitlen; i--) {
1768 if (brd_len == -1)
1769 brd.data[0] |= htonl(1<<(31-i));
1770 else
1771 brd.data[0] &= ~htonl(1<<(31-i));
1772 }
1773 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1774 brd_len = brd.bytelen;
1775 }
1776 }
1777 if (!scoped && cmd != RTM_DELADDR)
1778 req.ifa.ifa_scope = default_scope(&lcl);
1779
1780 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1781 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1782 return -1;
1783 }
1784
1785 if (valid_lftp || preferred_lftp) {
1786 if (!valid_lft) {
1787 fprintf(stderr, "valid_lft is zero\n");
1788 return -1;
1789 }
1790 if (valid_lft < preferred_lft) {
1791 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1792 return -1;
1793 }
1794
1795 memset(&cinfo, 0, sizeof(cinfo));
1796 cinfo.ifa_prefered = preferred_lft;
1797 cinfo.ifa_valid = valid_lft;
1798 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1799 sizeof(cinfo));
1800 }
1801
1802 if ((ifa_flags & IFA_F_MCAUTOJOIN) && !ipaddr_is_multicast(&lcl)) {
1803 fprintf(stderr, "autojoin needs multicast address\n");
1804 return -1;
1805 }
1806
1807 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
1808 return -2;
1809
1810 return 0;
1811 }
1812
1813 int do_ipaddr(int argc, char **argv)
1814 {
1815 if (argc < 1)
1816 return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST);
1817 if (matches(*argv, "add") == 0)
1818 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1819 if (matches(*argv, "change") == 0 ||
1820 strcmp(*argv, "chg") == 0)
1821 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1822 if (matches(*argv, "replace") == 0)
1823 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
1824 if (matches(*argv, "delete") == 0)
1825 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
1826 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1827 || matches(*argv, "lst") == 0)
1828 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST);
1829 if (matches(*argv, "flush") == 0)
1830 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH);
1831 if (matches(*argv, "save") == 0)
1832 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE);
1833 if (matches(*argv, "showdump") == 0)
1834 return ipaddr_showdump();
1835 if (matches(*argv, "restore") == 0)
1836 return ipaddr_restore();
1837 if (matches(*argv, "help") == 0)
1838 usage();
1839 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);
1840 exit(-1);
1841 }