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