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