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