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