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