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