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