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