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