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