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