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