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