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