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