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