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