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