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