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