]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipaddress.c
libnetlink: add size argument to rtnl_talk
[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, struct nlmsghdr *n, void *arg)
1115 {
1116 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1117
1118 printf("if%d:\n", ifa->ifa_index);
1119 print_addrinfo(NULL, n, stdout);
1120 return 0;
1121 }
1122
1123 static int ipaddr_showdump(void)
1124 {
1125 if (ipadd_dump_check_magic())
1126 exit(-1);
1127
1128 exit(rtnl_from_file(stdin, &show_handler, NULL));
1129 }
1130
1131 static int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg)
1132 {
1133 int ret;
1134
1135 n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
1136
1137 ll_init_map(&rth);
1138
1139 ret = rtnl_talk(&rth, n, n, sizeof(*n));
1140 if ((ret < 0) && (errno == EEXIST))
1141 ret = 0;
1142
1143 return ret;
1144 }
1145
1146 static int ipaddr_restore(void)
1147 {
1148 if (ipadd_dump_check_magic())
1149 exit(-1);
1150
1151 exit(rtnl_from_file(stdin, &restore_handler, NULL));
1152 }
1153
1154 static void free_nlmsg_chain(struct nlmsg_chain *info)
1155 {
1156 struct nlmsg_list *l, *n;
1157
1158 for (l = info->head; l; l = n) {
1159 n = l->next;
1160 free(l);
1161 }
1162 }
1163
1164 static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
1165 {
1166 struct nlmsg_list *l, **lp;
1167
1168 lp = &linfo->head;
1169 while ( (l = *lp) != NULL) {
1170 int ok = 0;
1171 int missing_net_address = 1;
1172 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1173 struct nlmsg_list *a;
1174
1175 for (a = ainfo->head; a; a = a->next) {
1176 struct nlmsghdr *n = &a->h;
1177 struct ifaddrmsg *ifa = NLMSG_DATA(n);
1178 struct rtattr *tb[IFA_MAX + 1];
1179 unsigned int ifa_flags;
1180
1181 if (ifa->ifa_index != ifi->ifi_index)
1182 continue;
1183 missing_net_address = 0;
1184 if (filter.family && filter.family != ifa->ifa_family)
1185 continue;
1186 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
1187 continue;
1188
1189 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
1190 ifa_flags = get_ifa_flags(ifa, tb[IFA_FLAGS]);
1191
1192 if ((filter.flags ^ ifa_flags) & filter.flagmask)
1193 continue;
1194 if (filter.pfx.family || filter.label) {
1195 if (!tb[IFA_LOCAL])
1196 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1197
1198 if (filter.pfx.family && tb[IFA_LOCAL]) {
1199 inet_prefix dst;
1200 memset(&dst, 0, sizeof(dst));
1201 dst.family = ifa->ifa_family;
1202 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
1203 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
1204 continue;
1205 }
1206 if (filter.label) {
1207 SPRINT_BUF(b1);
1208 const char *label;
1209 if (tb[IFA_LABEL])
1210 label = RTA_DATA(tb[IFA_LABEL]);
1211 else
1212 label = ll_idx_n2a(ifa->ifa_index, b1);
1213 if (fnmatch(filter.label, label, 0) != 0)
1214 continue;
1215 }
1216 }
1217
1218 ok = 1;
1219 break;
1220 }
1221 if (missing_net_address &&
1222 (filter.family == AF_UNSPEC || filter.family == AF_PACKET))
1223 ok = 1;
1224 if (!ok) {
1225 *lp = l->next;
1226 free(l);
1227 } else
1228 lp = &l->next;
1229 }
1230 }
1231
1232 static int ipaddr_flush(void)
1233 {
1234 int round = 0;
1235 char flushb[4096-512];
1236
1237 filter.flushb = flushb;
1238 filter.flushp = 0;
1239 filter.flushe = sizeof(flushb);
1240
1241 while ((max_flush_loops == 0) || (round < max_flush_loops)) {
1242 const struct rtnl_dump_filter_arg a[3] = {
1243 {
1244 .filter = print_addrinfo_secondary,
1245 .arg1 = stdout,
1246 },
1247 {
1248 .filter = print_addrinfo_primary,
1249 .arg1 = stdout,
1250 },
1251 {
1252 .filter = NULL,
1253 .arg1 = NULL,
1254 },
1255 };
1256 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1257 perror("Cannot send dump request");
1258 exit(1);
1259 }
1260 filter.flushed = 0;
1261 if (rtnl_dump_filter_l(&rth, a) < 0) {
1262 fprintf(stderr, "Flush terminated\n");
1263 exit(1);
1264 }
1265 if (filter.flushed == 0) {
1266 flush_done:
1267 if (show_stats) {
1268 if (round == 0)
1269 printf("Nothing to flush.\n");
1270 else
1271 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
1272 }
1273 fflush(stdout);
1274 return 0;
1275 }
1276 round++;
1277 if (flush_update() < 0)
1278 return 1;
1279
1280 if (show_stats) {
1281 printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed);
1282 fflush(stdout);
1283 }
1284
1285 /* If we are flushing, and specifying primary, then we
1286 * want to flush only a single round. Otherwise, we'll
1287 * start flushing secondaries that were promoted to
1288 * primaries.
1289 */
1290 if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY))
1291 goto flush_done;
1292 }
1293 fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops);
1294 fflush(stderr);
1295 return 1;
1296 }
1297
1298 static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
1299 {
1300 struct nlmsg_chain linfo = { NULL, NULL};
1301 struct nlmsg_chain ainfo = { NULL, NULL};
1302 struct nlmsg_list *l;
1303 char *filter_dev = NULL;
1304 int no_link = 0;
1305
1306 ipaddr_reset_filter(oneline, 0);
1307 filter.showqueue = 1;
1308
1309 if (filter.family == AF_UNSPEC)
1310 filter.family = preferred_family;
1311
1312 filter.group = -1;
1313
1314 if (action == IPADD_FLUSH) {
1315 if (argc <= 0) {
1316 fprintf(stderr, "Flush requires arguments.\n");
1317
1318 return -1;
1319 }
1320 if (filter.family == AF_PACKET) {
1321 fprintf(stderr, "Cannot flush link addresses.\n");
1322 return -1;
1323 }
1324 }
1325
1326 while (argc > 0) {
1327 if (strcmp(*argv, "to") == 0) {
1328 NEXT_ARG();
1329 get_prefix(&filter.pfx, *argv, filter.family);
1330 if (filter.family == AF_UNSPEC)
1331 filter.family = filter.pfx.family;
1332 } else if (strcmp(*argv, "scope") == 0) {
1333 unsigned scope = 0;
1334 NEXT_ARG();
1335 filter.scopemask = -1;
1336 if (rtnl_rtscope_a2n(&scope, *argv)) {
1337 if (strcmp(*argv, "all") != 0)
1338 invarg("invalid \"scope\"\n", *argv);
1339 scope = RT_SCOPE_NOWHERE;
1340 filter.scopemask = 0;
1341 }
1342 filter.scope = scope;
1343 } else if (strcmp(*argv, "up") == 0) {
1344 filter.up = 1;
1345 } else if (strcmp(*argv, "dynamic") == 0) {
1346 filter.flags &= ~IFA_F_PERMANENT;
1347 filter.flagmask |= IFA_F_PERMANENT;
1348 } else if (strcmp(*argv, "permanent") == 0) {
1349 filter.flags |= IFA_F_PERMANENT;
1350 filter.flagmask |= IFA_F_PERMANENT;
1351 } else if (strcmp(*argv, "secondary") == 0 ||
1352 strcmp(*argv, "temporary") == 0) {
1353 filter.flags |= IFA_F_SECONDARY;
1354 filter.flagmask |= IFA_F_SECONDARY;
1355 } else if (strcmp(*argv, "primary") == 0) {
1356 filter.flags &= ~IFA_F_SECONDARY;
1357 filter.flagmask |= IFA_F_SECONDARY;
1358 } else if (strcmp(*argv, "tentative") == 0) {
1359 filter.flags |= IFA_F_TENTATIVE;
1360 filter.flagmask |= IFA_F_TENTATIVE;
1361 } else if (strcmp(*argv, "-tentative") == 0) {
1362 filter.flags &= ~IFA_F_TENTATIVE;
1363 filter.flagmask |= IFA_F_TENTATIVE;
1364 } else if (strcmp(*argv, "deprecated") == 0) {
1365 filter.flags |= IFA_F_DEPRECATED;
1366 filter.flagmask |= IFA_F_DEPRECATED;
1367 } else if (strcmp(*argv, "-deprecated") == 0) {
1368 filter.flags &= ~IFA_F_DEPRECATED;
1369 filter.flagmask |= IFA_F_DEPRECATED;
1370 } else if (strcmp(*argv, "home") == 0) {
1371 filter.flags |= IFA_F_HOMEADDRESS;
1372 filter.flagmask |= IFA_F_HOMEADDRESS;
1373 } else if (strcmp(*argv, "nodad") == 0) {
1374 filter.flags |= IFA_F_NODAD;
1375 filter.flagmask |= IFA_F_NODAD;
1376 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1377 filter.flags |= IFA_F_MANAGETEMPADDR;
1378 filter.flagmask |= IFA_F_MANAGETEMPADDR;
1379 } else if (strcmp(*argv, "noprefixroute") == 0) {
1380 filter.flags |= IFA_F_NOPREFIXROUTE;
1381 filter.flagmask |= IFA_F_NOPREFIXROUTE;
1382 } else if (strcmp(*argv, "autojoin") == 0) {
1383 filter.flags |= IFA_F_MCAUTOJOIN;
1384 filter.flagmask |= IFA_F_MCAUTOJOIN;
1385 } else if (strcmp(*argv, "dadfailed") == 0) {
1386 filter.flags |= IFA_F_DADFAILED;
1387 filter.flagmask |= IFA_F_DADFAILED;
1388 } else if (strcmp(*argv, "-dadfailed") == 0) {
1389 filter.flags &= ~IFA_F_DADFAILED;
1390 filter.flagmask |= IFA_F_DADFAILED;
1391 } else if (strcmp(*argv, "label") == 0) {
1392 NEXT_ARG();
1393 filter.label = *argv;
1394 } else if (strcmp(*argv, "group") == 0) {
1395 NEXT_ARG();
1396 if (rtnl_group_a2n(&filter.group, *argv))
1397 invarg("Invalid \"group\" value\n", *argv);
1398 } else if (strcmp(*argv, "master") == 0) {
1399 int ifindex;
1400 NEXT_ARG();
1401 ifindex = ll_name_to_index(*argv);
1402 if (!ifindex)
1403 invarg("Device does not exist\n", *argv);
1404 filter.master = ifindex;
1405 } else if (do_link && strcmp(*argv, "type") == 0) {
1406 NEXT_ARG();
1407 filter.kind = *argv;
1408 } else {
1409 if (strcmp(*argv, "dev") == 0) {
1410 NEXT_ARG();
1411 }
1412 if (matches(*argv, "help") == 0)
1413 usage();
1414 if (filter_dev)
1415 duparg2("dev", *argv);
1416 filter_dev = *argv;
1417 }
1418 argv++; argc--;
1419 }
1420
1421 if (filter_dev) {
1422 filter.ifindex = ll_name_to_index(filter_dev);
1423 if (filter.ifindex <= 0) {
1424 fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev);
1425 return -1;
1426 }
1427 }
1428
1429 if (action == IPADD_FLUSH)
1430 return ipaddr_flush();
1431
1432 if (action == IPADD_SAVE) {
1433 if (ipadd_save_prep())
1434 exit(1);
1435
1436 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
1437 perror("Cannot send dump request");
1438 exit(1);
1439 }
1440
1441 if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) {
1442 fprintf(stderr, "Save terminated\n");
1443 exit(1);
1444 }
1445
1446 exit(0);
1447 }
1448
1449 /*
1450 * If only filter_dev present and none of the other
1451 * link filters are present, use RTM_GETLINK to get
1452 * the link device
1453 */
1454 if (filter_dev && filter.group == -1 && do_link == 1) {
1455 if (iplink_get(0, filter_dev, RTEXT_FILTER_VF) < 0) {
1456 perror("Cannot send link get request");
1457 exit(1);
1458 }
1459 exit(0);
1460 }
1461
1462 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
1463 perror("Cannot send dump request");
1464 exit(1);
1465 }
1466
1467 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1468 fprintf(stderr, "Dump terminated\n");
1469 exit(1);
1470 }
1471
1472 if (filter.family != AF_PACKET) {
1473 if (filter.oneline)
1474 no_link = 1;
1475
1476 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
1477 perror("Cannot send dump request");
1478 exit(1);
1479 }
1480
1481 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
1482 fprintf(stderr, "Dump terminated\n");
1483 exit(1);
1484 }
1485
1486 ipaddr_filter(&linfo, &ainfo);
1487 }
1488
1489 for (l = linfo.head; l; l = l->next) {
1490 int res = 0;
1491
1492 if (no_link || (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) {
1493 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
1494 if (filter.family != AF_PACKET)
1495 print_selected_addrinfo(ifi,
1496 ainfo.head, stdout);
1497 if (res > 0 && !do_link && show_stats)
1498 print_link_stats(stdout, &l->h);
1499 }
1500 }
1501 fflush(stdout);
1502
1503 free_nlmsg_chain(&ainfo);
1504 free_nlmsg_chain(&linfo);
1505
1506 return 0;
1507 }
1508
1509 static void
1510 ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max)
1511 {
1512 struct rtattr *vflist = tb[IFLA_VFINFO_LIST];
1513 struct rtattr *i, *vf[IFLA_VF_MAX+1];
1514 struct ifla_vf_rate *vf_rate;
1515 int rem;
1516
1517 rem = RTA_PAYLOAD(vflist);
1518
1519 for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
1520 parse_rtattr_nested(vf, IFLA_VF_MAX, i);
1521 vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
1522 if (vf_rate->vf == vfnum) {
1523 *min = vf_rate->min_tx_rate;
1524 *max = vf_rate->max_tx_rate;
1525 return;
1526 }
1527 }
1528 fprintf(stderr, "Cannot find VF %d\n", vfnum);
1529 exit(1);
1530 }
1531
1532 void ipaddr_get_vf_rate(int vfnum, int *min, int *max, int idx)
1533 {
1534 struct nlmsg_chain linfo = { NULL, NULL};
1535 struct rtattr *tb[IFLA_MAX+1];
1536 struct ifinfomsg *ifi;
1537 struct nlmsg_list *l;
1538 struct nlmsghdr *n;
1539 int len;
1540
1541 if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
1542 perror("Cannot send dump request");
1543 exit(1);
1544 }
1545 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
1546 fprintf(stderr, "Dump terminated\n");
1547 exit(1);
1548 }
1549 for (l = linfo.head; l; l = l->next) {
1550 n = &l->h;
1551 ifi = NLMSG_DATA(n);
1552
1553 len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
1554 if (len < 0 || (idx && idx != ifi->ifi_index))
1555 continue;
1556
1557 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1558
1559 if ((tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF])) {
1560 ipaddr_loop_each_vf(tb, vfnum, min, max);
1561 return;
1562 }
1563 }
1564 }
1565
1566 int ipaddr_list_link(int argc, char **argv)
1567 {
1568 preferred_family = AF_PACKET;
1569 do_link = 1;
1570 return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST);
1571 }
1572
1573 void ipaddr_reset_filter(int oneline, int ifindex)
1574 {
1575 memset(&filter, 0, sizeof(filter));
1576 filter.oneline = oneline;
1577 filter.ifindex = ifindex;
1578 }
1579
1580 static int default_scope(inet_prefix *lcl)
1581 {
1582 if (lcl->family == AF_INET) {
1583 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
1584 return RT_SCOPE_HOST;
1585 }
1586 return 0;
1587 }
1588
1589 static bool ipaddr_is_multicast(inet_prefix *a)
1590 {
1591 if (a->family == AF_INET)
1592 return IN_MULTICAST(ntohl(a->data[0]));
1593 else if (a->family == AF_INET6)
1594 return IN6_IS_ADDR_MULTICAST(a->data);
1595 else
1596 return false;
1597 }
1598
1599 static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
1600 {
1601 struct {
1602 struct nlmsghdr n;
1603 struct ifaddrmsg ifa;
1604 char buf[256];
1605 } req;
1606 char *d = NULL;
1607 char *l = NULL;
1608 char *lcl_arg = NULL;
1609 char *valid_lftp = NULL;
1610 char *preferred_lftp = NULL;
1611 inet_prefix lcl;
1612 inet_prefix peer;
1613 int local_len = 0;
1614 int peer_len = 0;
1615 int brd_len = 0;
1616 int any_len = 0;
1617 int scoped = 0;
1618 __u32 preferred_lft = INFINITY_LIFE_TIME;
1619 __u32 valid_lft = INFINITY_LIFE_TIME;
1620 struct ifa_cacheinfo cinfo;
1621 unsigned int ifa_flags = 0;
1622
1623 memset(&req, 0, sizeof(req));
1624
1625 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1626 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
1627 req.n.nlmsg_type = cmd;
1628 req.ifa.ifa_family = preferred_family;
1629
1630 while (argc > 0) {
1631 if (strcmp(*argv, "peer") == 0 ||
1632 strcmp(*argv, "remote") == 0) {
1633 NEXT_ARG();
1634
1635 if (peer_len)
1636 duparg("peer", *argv);
1637 get_prefix(&peer, *argv, req.ifa.ifa_family);
1638 peer_len = peer.bytelen;
1639 if (req.ifa.ifa_family == AF_UNSPEC)
1640 req.ifa.ifa_family = peer.family;
1641 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
1642 req.ifa.ifa_prefixlen = peer.bitlen;
1643 } else if (matches(*argv, "broadcast") == 0 ||
1644 strcmp(*argv, "brd") == 0) {
1645 inet_prefix addr;
1646 NEXT_ARG();
1647 if (brd_len)
1648 duparg("broadcast", *argv);
1649 if (strcmp(*argv, "+") == 0)
1650 brd_len = -1;
1651 else if (strcmp(*argv, "-") == 0)
1652 brd_len = -2;
1653 else {
1654 get_addr(&addr, *argv, req.ifa.ifa_family);
1655 if (req.ifa.ifa_family == AF_UNSPEC)
1656 req.ifa.ifa_family = addr.family;
1657 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
1658 brd_len = addr.bytelen;
1659 }
1660 } else if (strcmp(*argv, "anycast") == 0) {
1661 inet_prefix addr;
1662 NEXT_ARG();
1663 if (any_len)
1664 duparg("anycast", *argv);
1665 get_addr(&addr, *argv, req.ifa.ifa_family);
1666 if (req.ifa.ifa_family == AF_UNSPEC)
1667 req.ifa.ifa_family = addr.family;
1668 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
1669 any_len = addr.bytelen;
1670 } else if (strcmp(*argv, "scope") == 0) {
1671 unsigned scope = 0;
1672 NEXT_ARG();
1673 if (rtnl_rtscope_a2n(&scope, *argv))
1674 invarg("invalid scope value.", *argv);
1675 req.ifa.ifa_scope = scope;
1676 scoped = 1;
1677 } else if (strcmp(*argv, "dev") == 0) {
1678 NEXT_ARG();
1679 d = *argv;
1680 } else if (strcmp(*argv, "label") == 0) {
1681 NEXT_ARG();
1682 l = *argv;
1683 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
1684 } else if (matches(*argv, "valid_lft") == 0) {
1685 if (valid_lftp)
1686 duparg("valid_lft", *argv);
1687 NEXT_ARG();
1688 valid_lftp = *argv;
1689 if (set_lifetime(&valid_lft, *argv))
1690 invarg("valid_lft value", *argv);
1691 } else if (matches(*argv, "preferred_lft") == 0) {
1692 if (preferred_lftp)
1693 duparg("preferred_lft", *argv);
1694 NEXT_ARG();
1695 preferred_lftp = *argv;
1696 if (set_lifetime(&preferred_lft, *argv))
1697 invarg("preferred_lft value", *argv);
1698 } else if (strcmp(*argv, "home") == 0) {
1699 ifa_flags |= IFA_F_HOMEADDRESS;
1700 } else if (strcmp(*argv, "nodad") == 0) {
1701 ifa_flags |= IFA_F_NODAD;
1702 } else if (strcmp(*argv, "mngtmpaddr") == 0) {
1703 ifa_flags |= IFA_F_MANAGETEMPADDR;
1704 } else if (strcmp(*argv, "noprefixroute") == 0) {
1705 ifa_flags |= IFA_F_NOPREFIXROUTE;
1706 } else if (strcmp(*argv, "autojoin") == 0) {
1707 ifa_flags |= IFA_F_MCAUTOJOIN;
1708 } else {
1709 if (strcmp(*argv, "local") == 0) {
1710 NEXT_ARG();
1711 }
1712 if (matches(*argv, "help") == 0)
1713 usage();
1714 if (local_len)
1715 duparg2("local", *argv);
1716 lcl_arg = *argv;
1717 get_prefix(&lcl, *argv, req.ifa.ifa_family);
1718 if (req.ifa.ifa_family == AF_UNSPEC)
1719 req.ifa.ifa_family = lcl.family;
1720 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
1721 local_len = lcl.bytelen;
1722 }
1723 argc--; argv++;
1724 }
1725 if (ifa_flags <= 0xff)
1726 req.ifa.ifa_flags = ifa_flags;
1727 else
1728 addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
1729
1730 if (d == NULL) {
1731 fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
1732 return -1;
1733 }
1734 if (l && matches(d, l) != 0) {
1735 fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
1736 return -1;
1737 }
1738
1739 if (peer_len == 0 && local_len) {
1740 if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
1741 fprintf(stderr,
1742 "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
1743 " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
1744 " This special behaviour is likely to disappear in further releases,\n" \
1745 " fix your scripts!\n", lcl_arg, local_len*8);
1746 } else {
1747 peer = lcl;
1748 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
1749 }
1750 }
1751 if (req.ifa.ifa_prefixlen == 0)
1752 req.ifa.ifa_prefixlen = lcl.bitlen;
1753
1754 if (brd_len < 0 && cmd != RTM_DELADDR) {
1755 inet_prefix brd;
1756 int i;
1757 if (req.ifa.ifa_family != AF_INET) {
1758 fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
1759 return -1;
1760 }
1761 brd = peer;
1762 if (brd.bitlen <= 30) {
1763 for (i = 31; i >= brd.bitlen; i--) {
1764 if (brd_len == -1)
1765 brd.data[0] |= htonl(1<<(31-i));
1766 else
1767 brd.data[0] &= ~htonl(1<<(31-i));
1768 }
1769 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
1770 brd_len = brd.bytelen;
1771 }
1772 }
1773 if (!scoped && cmd != RTM_DELADDR)
1774 req.ifa.ifa_scope = default_scope(&lcl);
1775
1776 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
1777 fprintf(stderr, "Cannot find device \"%s\"\n", d);
1778 return -1;
1779 }
1780
1781 if (valid_lftp || preferred_lftp) {
1782 if (!valid_lft) {
1783 fprintf(stderr, "valid_lft is zero\n");
1784 return -1;
1785 }
1786 if (valid_lft < preferred_lft) {
1787 fprintf(stderr, "preferred_lft is greater than valid_lft\n");
1788 return -1;
1789 }
1790
1791 memset(&cinfo, 0, sizeof(cinfo));
1792 cinfo.ifa_prefered = preferred_lft;
1793 cinfo.ifa_valid = valid_lft;
1794 addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
1795 sizeof(cinfo));
1796 }
1797
1798 if ((ifa_flags & IFA_F_MCAUTOJOIN) && !ipaddr_is_multicast(&lcl)) {
1799 fprintf(stderr, "autojoin needs multicast address\n");
1800 return -1;
1801 }
1802
1803 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
1804 return -2;
1805
1806 return 0;
1807 }
1808
1809 int do_ipaddr(int argc, char **argv)
1810 {
1811 if (argc < 1)
1812 return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST);
1813 if (matches(*argv, "add") == 0)
1814 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
1815 if (matches(*argv, "change") == 0 ||
1816 strcmp(*argv, "chg") == 0)
1817 return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
1818 if (matches(*argv, "replace") == 0)
1819 return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
1820 if (matches(*argv, "delete") == 0)
1821 return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1);
1822 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1823 || matches(*argv, "lst") == 0)
1824 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST);
1825 if (matches(*argv, "flush") == 0)
1826 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH);
1827 if (matches(*argv, "save") == 0)
1828 return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE);
1829 if (matches(*argv, "showdump") == 0)
1830 return ipaddr_showdump();
1831 if (matches(*argv, "restore") == 0)
1832 return ipaddr_restore();
1833 if (matches(*argv, "help") == 0)
1834 usage();
1835 fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv);
1836 exit(-1);
1837 }