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