]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * iplink.c "ip link". | |
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 | * | |
11 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
aba5acdf | 16 | #include <fcntl.h> |
1d934839 | 17 | #include <dlfcn.h> |
aba5acdf SH |
18 | #include <errno.h> |
19 | #include <sys/socket.h> | |
20 | #include <linux/if.h> | |
21 | #include <linux/if_packet.h> | |
22 | #include <linux/if_ether.h> | |
23 | #include <linux/sockios.h> | |
24 | #include <netinet/in.h> | |
25 | #include <arpa/inet.h> | |
26 | #include <string.h> | |
27 | #include <sys/ioctl.h> | |
fbea6115 | 28 | #include <stdbool.h> |
837552b4 | 29 | #include <linux/mpls.h> |
aba5acdf SH |
30 | |
31 | #include "rt_names.h" | |
32 | #include "utils.h" | |
33 | #include "ip_common.h" | |
c7272ca7 | 34 | #include "xdp.h" |
c3087c10 | 35 | #include "namespace.h" |
aba5acdf | 36 | |
1d934839 | 37 | #define IPLINK_IOCTL_COMPAT 1 |
5e3bb534 | 38 | #ifndef LIBDIR |
5c434a9e | 39 | #define LIBDIR "/usr/lib" |
b514b358 | 40 | #endif |
aba5acdf | 41 | |
08f9d166 SH |
42 | #ifndef GSO_MAX_SIZE |
43 | #define GSO_MAX_SIZE 65536 | |
44 | #endif | |
45 | #ifndef GSO_MAX_SEGS | |
46 | #define GSO_MAX_SEGS 65535 | |
47 | #endif | |
48 | ||
49 | ||
aba5acdf | 50 | static void usage(void) __attribute__((noreturn)); |
750a405a | 51 | static int iplink_have_newlink(void); |
aba5acdf SH |
52 | |
53 | void iplink_usage(void) | |
54 | { | |
750a405a | 55 | if (iplink_have_newlink()) { |
6773bcc2 SH |
56 | fprintf(stderr, |
57 | "Usage: ip link add [link DEV] [ name ] NAME\n" | |
58 | " [ txqueuelen PACKETS ]\n" | |
59 | " [ address LLADDR ]\n" | |
60 | " [ broadcast LLADDR ]\n" | |
61 | " [ mtu MTU ] [index IDX ]\n" | |
62 | " [ numtxqueues QUEUE_COUNT ]\n" | |
63 | " [ numrxqueues QUEUE_COUNT ]\n" | |
64 | " type TYPE [ ARGS ]\n" | |
c7272ca7 | 65 | "\n" |
6773bcc2 SH |
66 | " ip link delete { DEVICE | dev DEVICE | group DEVGROUP } type TYPE [ ARGS ]\n" |
67 | "\n" | |
68 | " ip link set { DEVICE | dev DEVICE | group DEVGROUP }\n" | |
69 | " [ { up | down } ]\n" | |
70 | " [ type TYPE ARGS ]\n"); | |
750a405a | 71 | } else |
bb6ab47b SH |
72 | fprintf(stderr, |
73 | "Usage: ip link set DEVICE [ { up | down } ]\n"); | |
750a405a | 74 | |
6773bcc2 SH |
75 | fprintf(stderr, |
76 | " [ arp { on | off } ]\n" | |
77 | " [ dynamic { on | off } ]\n" | |
78 | " [ multicast { on | off } ]\n" | |
79 | " [ allmulticast { on | off } ]\n" | |
80 | " [ promisc { on | off } ]\n" | |
81 | " [ trailers { on | off } ]\n" | |
432b92a7 | 82 | " [ carrier { on | off } ]\n" |
6773bcc2 SH |
83 | " [ txqueuelen PACKETS ]\n" |
84 | " [ name NEWNAME ]\n" | |
85 | " [ address LLADDR ]\n" | |
86 | " [ broadcast LLADDR ]\n" | |
87 | " [ mtu MTU ]\n" | |
88 | " [ netns { PID | NAME } ]\n" | |
89 | " [ link-netnsid ID ]\n" | |
90 | " [ alias NAME ]\n" | |
91 | " [ vf NUM [ mac LLADDR ]\n" | |
92 | " [ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ]\n" | |
6773bcc2 SH |
93 | " [ rate TXRATE ]\n" |
94 | " [ max_tx_rate TXRATE ]\n" | |
95 | " [ min_tx_rate TXRATE ]\n" | |
6773bcc2 SH |
96 | " [ spoofchk { on | off} ]\n" |
97 | " [ query_rss { on | off} ]\n" | |
98 | " [ state { auto | enable | disable} ] ]\n" | |
99 | " [ trust { on | off} ] ]\n" | |
5a3ec4ba EC |
100 | " [ node_guid { eui64 } ]\n" |
101 | " [ port_guid { eui64 } ]\n" | |
c7272ca7 DB |
102 | " [ xdp { off |\n" |
103 | " object FILE [ section NAME ] [ verbose ] |\n" | |
104 | " pinned FILE } ]\n" | |
6773bcc2 SH |
105 | " [ master DEVICE ][ vrf NAME ]\n" |
106 | " [ nomaster ]\n" | |
107 | " [ addrgenmode { eui64 | none | stable_secret | random } ]\n" | |
c2db423f SH |
108 | " [ protodown { on | off } ]\n" |
109 | " [ gso_max_size BYTES ] | [ gso_max_segs PACKETS ]\n" | |
c7272ca7 | 110 | "\n" |
6773bcc2 | 111 | " ip link show [ DEVICE | group GROUP ] [up] [master DEV] [vrf NAME] [type TYPE]\n"); |
750a405a | 112 | |
94f1a22a | 113 | fprintf(stderr, "\n ip link xstats type TYPE [ ARGS ]\n"); |
837552b4 | 114 | fprintf(stderr, "\n ip link afstats [ dev DEVICE ]\n"); |
94f1a22a | 115 | |
750a405a | 116 | if (iplink_have_newlink()) { |
6773bcc2 | 117 | fprintf(stderr, |
c7272ca7 DB |
118 | "\n" |
119 | " ip link help [ TYPE ]\n" | |
120 | "\n" | |
7827b376 | 121 | "TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |\n" |
6bd1ea28 | 122 | " bridge | bond | team | ipoib | ip6tnl | ipip | sit | vxlan |\n" |
8595cc40 WT |
123 | " gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan |\n" |
124 | " vti | nlmon | team_slave | bond_slave | ipvlan | geneve |\n" | |
7d424c71 | 125 | " bridge_slave | vrf | macsec | netdevsim }\n"); |
750a405a | 126 | } |
aba5acdf SH |
127 | exit(-1); |
128 | } | |
129 | ||
130 | static void usage(void) | |
131 | { | |
132 | iplink_usage(); | |
133 | } | |
134 | ||
14645ec2 | 135 | static int on_off(const char *msg, const char *realval) |
aba5acdf | 136 | { |
6c5ffb9a SH |
137 | fprintf(stderr, |
138 | "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", | |
139 | msg, realval); | |
aba5acdf SH |
140 | return -1; |
141 | } | |
142 | ||
1d934839 PM |
143 | static void *BODY; /* cached dlopen(NULL) handle */ |
144 | static struct link_util *linkutil_list; | |
145 | ||
22a84711 | 146 | struct link_util *get_link_kind(const char *id) |
1d934839 PM |
147 | { |
148 | void *dlh; | |
149 | char buf[256]; | |
150 | struct link_util *l; | |
151 | ||
152 | for (l = linkutil_list; l; l = l->next) | |
22a84711 | 153 | if (strcmp(l->id, id) == 0) |
1d934839 PM |
154 | return l; |
155 | ||
5e3bb534 | 156 | snprintf(buf, sizeof(buf), LIBDIR "/ip/link_%s.so", id); |
1d934839 PM |
157 | dlh = dlopen(buf, RTLD_LAZY); |
158 | if (dlh == NULL) { | |
159 | /* look in current binary, only open once */ | |
160 | dlh = BODY; | |
161 | if (dlh == NULL) { | |
162 | dlh = BODY = dlopen(NULL, RTLD_LAZY); | |
163 | if (dlh == NULL) | |
164 | return NULL; | |
165 | } | |
166 | } | |
167 | ||
22a84711 | 168 | snprintf(buf, sizeof(buf), "%s_link_util", id); |
1d934839 PM |
169 | l = dlsym(dlh, buf); |
170 | if (l == NULL) | |
171 | return NULL; | |
172 | ||
173 | l->next = linkutil_list; | |
174 | linkutil_list = l; | |
175 | return l; | |
176 | } | |
177 | ||
d1f28cf1 | 178 | static int get_link_mode(const char *mode) |
82499282 | 179 | { |
4ccfb44d | 180 | if (strcasecmp(mode, "default") == 0) |
82499282 | 181 | return IF_LINK_MODE_DEFAULT; |
4ccfb44d | 182 | if (strcasecmp(mode, "dormant") == 0) |
82499282 SH |
183 | return IF_LINK_MODE_DORMANT; |
184 | return -1; | |
185 | } | |
186 | ||
ff7c2084 JP |
187 | static int get_addr_gen_mode(const char *mode) |
188 | { | |
189 | if (strcasecmp(mode, "eui64") == 0) | |
190 | return IN6_ADDR_GEN_MODE_EUI64; | |
191 | if (strcasecmp(mode, "none") == 0) | |
192 | return IN6_ADDR_GEN_MODE_NONE; | |
8e098dd8 BM |
193 | if (strcasecmp(mode, "stable_secret") == 0) |
194 | return IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
8e12bc0a BM |
195 | if (strcasecmp(mode, "random") == 0) |
196 | return IN6_ADDR_GEN_MODE_RANDOM; | |
ff7c2084 JP |
197 | return -1; |
198 | } | |
199 | ||
1d934839 PM |
200 | #if IPLINK_IOCTL_COMPAT |
201 | static int have_rtnl_newlink = -1; | |
202 | ||
203 | static int accept_msg(const struct sockaddr_nl *who, | |
0628cddd | 204 | struct rtnl_ctrl_data *ctrl, |
1d934839 PM |
205 | struct nlmsghdr *n, void *arg) |
206 | { | |
207 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n); | |
208 | ||
66e529f5 PM |
209 | if (n->nlmsg_type == NLMSG_ERROR && |
210 | (err->error == -EOPNOTSUPP || err->error == -EINVAL)) | |
1d934839 PM |
211 | have_rtnl_newlink = 0; |
212 | else | |
213 | have_rtnl_newlink = 1; | |
214 | return -1; | |
215 | } | |
216 | ||
217 | static int iplink_have_newlink(void) | |
218 | { | |
219 | struct { | |
220 | struct nlmsghdr n; | |
221 | struct ifinfomsg i; | |
222 | char buf[1024]; | |
d17b136f PS |
223 | } req = { |
224 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
225 | .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, | |
226 | .n.nlmsg_type = RTM_NEWLINK, | |
227 | .i.ifi_family = AF_UNSPEC, | |
228 | }; | |
1d934839 PM |
229 | |
230 | if (have_rtnl_newlink < 0) { | |
d2468da0 SH |
231 | if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) { |
232 | perror("request send failed"); | |
233 | exit(1); | |
234 | } | |
1d934839 PM |
235 | rtnl_listen(&rth, accept_msg, NULL); |
236 | } | |
237 | return have_rtnl_newlink; | |
238 | } | |
239 | #else /* IPLINK_IOCTL_COMPAT */ | |
240 | static int iplink_have_newlink(void) | |
241 | { | |
242 | return 1; | |
243 | } | |
244 | #endif /* ! IPLINK_IOCTL_COMPAT */ | |
245 | ||
8fe58d58 PS |
246 | static int nl_get_ll_addr_len(unsigned int dev_index) |
247 | { | |
248 | int len; | |
249 | struct iplink_req req = { | |
250 | .n = { | |
251 | .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
252 | .nlmsg_type = RTM_GETLINK, | |
253 | .nlmsg_flags = NLM_F_REQUEST | |
254 | }, | |
255 | .i = { | |
256 | .ifi_family = preferred_family, | |
257 | .ifi_index = dev_index, | |
258 | } | |
259 | }; | |
86bf43c7 | 260 | struct nlmsghdr *answer; |
8fe58d58 PS |
261 | struct rtattr *tb[IFLA_MAX+1]; |
262 | ||
86bf43c7 | 263 | if (rtnl_talk(&rth, &req.n, &answer) < 0) |
8fe58d58 PS |
264 | return -1; |
265 | ||
86bf43c7 HL |
266 | len = answer->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
267 | if (len < 0) { | |
268 | free(answer); | |
8fe58d58 | 269 | return -1; |
86bf43c7 | 270 | } |
8fe58d58 | 271 | |
86bf43c7 HL |
272 | parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(NLMSG_DATA(answer)), |
273 | len, NLA_F_NESTED); | |
274 | if (!tb[IFLA_ADDRESS]) { | |
275 | free(answer); | |
8fe58d58 | 276 | return -1; |
86bf43c7 | 277 | } |
8fe58d58 | 278 | |
86bf43c7 | 279 | free(answer); |
8fe58d58 PS |
280 | return RTA_PAYLOAD(tb[IFLA_ADDRESS]); |
281 | } | |
282 | ||
56e9f0ab MS |
283 | static void iplink_parse_vf_vlan_info(int vf, int *argcp, char ***argvp, |
284 | struct ifla_vf_vlan_info *ivvip) | |
285 | { | |
286 | int argc = *argcp; | |
287 | char **argv = *argvp; | |
288 | ||
289 | NEXT_ARG(); | |
290 | if (get_unsigned(&ivvip->vlan, *argv, 0)) | |
291 | invarg("Invalid \"vlan\" value\n", *argv); | |
292 | ||
293 | ivvip->vf = vf; | |
294 | ivvip->qos = 0; | |
295 | ivvip->vlan_proto = htons(ETH_P_8021Q); | |
296 | if (NEXT_ARG_OK()) { | |
297 | NEXT_ARG(); | |
298 | if (matches(*argv, "qos") == 0) { | |
299 | NEXT_ARG(); | |
300 | if (get_unsigned(&ivvip->qos, *argv, 0)) | |
301 | invarg("Invalid \"qos\" value\n", *argv); | |
302 | } else { | |
303 | /* rewind arg */ | |
304 | PREV_ARG(); | |
305 | } | |
306 | } | |
307 | if (NEXT_ARG_OK()) { | |
308 | NEXT_ARG(); | |
309 | if (matches(*argv, "proto") == 0) { | |
310 | NEXT_ARG(); | |
311 | if (ll_proto_a2n(&ivvip->vlan_proto, *argv)) | |
312 | invarg("protocol is invalid\n", *argv); | |
313 | if (ivvip->vlan_proto != htons(ETH_P_8021AD) && | |
314 | ivvip->vlan_proto != htons(ETH_P_8021Q)) { | |
315 | SPRINT_BUF(b1); | |
316 | SPRINT_BUF(b2); | |
317 | char msg[64 + sizeof(b1) + sizeof(b2)]; | |
318 | ||
bb6ab47b SH |
319 | sprintf(msg, |
320 | "Invalid \"vlan protocol\" value - supported %s, %s\n", | |
56e9f0ab MS |
321 | ll_proto_n2a(htons(ETH_P_8021Q), |
322 | b1, sizeof(b1)), | |
323 | ll_proto_n2a(htons(ETH_P_8021AD), | |
324 | b2, sizeof(b2))); | |
325 | invarg(msg, *argv); | |
326 | } | |
327 | } else { | |
328 | /* rewind arg */ | |
329 | PREV_ARG(); | |
330 | } | |
331 | } | |
332 | ||
333 | *argcp = argc; | |
334 | *argvp = argv; | |
335 | } | |
336 | ||
d1f28cf1 | 337 | static int iplink_parse_vf(int vf, int *argcp, char ***argvp, |
f89a2a05 | 338 | struct iplink_req *req, int dev_index) |
3fd86630 | 339 | { |
f89a2a05 SC |
340 | char new_rate_api = 0, count = 0, override_legacy_rate = 0; |
341 | struct ifla_vf_rate tivt; | |
3fd86630 CW |
342 | int len, argc = *argcp; |
343 | char **argv = *argvp; | |
344 | struct rtattr *vfinfo; | |
1598b9ef | 345 | |
f89a2a05 SC |
346 | tivt.min_tx_rate = -1; |
347 | tivt.max_tx_rate = -1; | |
348 | ||
1598b9ef | 349 | vfinfo = addattr_nest(&req->n, sizeof(*req), IFLA_VF_INFO); |
3fd86630 | 350 | |
f89a2a05 SC |
351 | while (NEXT_ARG_OK()) { |
352 | NEXT_ARG(); | |
353 | count++; | |
354 | if (!matches(*argv, "max_tx_rate")) { | |
355 | /* new API in use */ | |
356 | new_rate_api = 1; | |
357 | /* override legacy rate */ | |
358 | override_legacy_rate = 1; | |
359 | } else if (!matches(*argv, "min_tx_rate")) { | |
360 | /* new API in use */ | |
361 | new_rate_api = 1; | |
362 | } | |
363 | } | |
364 | ||
365 | while (count--) { | |
366 | /* rewind arg */ | |
367 | PREV_ARG(); | |
368 | } | |
369 | ||
3fd86630 CW |
370 | while (NEXT_ARG_OK()) { |
371 | NEXT_ARG(); | |
1598b9ef | 372 | if (matches(*argv, "mac") == 0) { |
a89193a7 | 373 | struct ifla_vf_mac ivm = { 0 }; |
8fe58d58 | 374 | int halen = nl_get_ll_addr_len(dev_index); |
6c5ffb9a | 375 | |
3fd86630 | 376 | NEXT_ARG(); |
1598b9ef SH |
377 | ivm.vf = vf; |
378 | len = ll_addr_a2n((char *)ivm.mac, 32, *argv); | |
379 | if (len < 0) | |
380 | return -1; | |
8fe58d58 PS |
381 | if (halen > 0 && len != halen) { |
382 | fprintf(stderr, | |
383 | "Invalid address length %d - must be %d bytes\n", | |
384 | len, halen); | |
385 | return -1; | |
386 | } | |
ef0a738c SH |
387 | addattr_l(&req->n, sizeof(*req), IFLA_VF_MAC, |
388 | &ivm, sizeof(ivm)); | |
3fd86630 | 389 | } else if (matches(*argv, "vlan") == 0) { |
56e9f0ab | 390 | struct ifla_vf_vlan_info ivvi; |
6c5ffb9a | 391 | |
56e9f0ab MS |
392 | iplink_parse_vf_vlan_info(vf, &argc, &argv, &ivvi); |
393 | /* support the old interface in case of older kernel*/ | |
394 | if (ivvi.vlan_proto == htons(ETH_P_8021Q)) { | |
395 | struct ifla_vf_vlan ivv; | |
396 | ||
397 | ivv.vf = ivvi.vf; | |
398 | ivv.vlan = ivvi.vlan; | |
399 | ivv.qos = ivvi.qos; | |
400 | addattr_l(&req->n, sizeof(*req), | |
401 | IFLA_VF_VLAN, &ivv, sizeof(ivv)); | |
402 | } else { | |
403 | struct rtattr *vfvlanlist; | |
404 | ||
405 | vfvlanlist = addattr_nest(&req->n, sizeof(*req), | |
406 | IFLA_VF_VLAN_LIST); | |
407 | addattr_l(&req->n, sizeof(*req), | |
408 | IFLA_VF_VLAN_INFO, &ivvi, | |
409 | sizeof(ivvi)); | |
410 | ||
411 | while (NEXT_ARG_OK()) { | |
3fd86630 | 412 | NEXT_ARG(); |
56e9f0ab MS |
413 | if (matches(*argv, "vlan") != 0) { |
414 | PREV_ARG(); | |
415 | break; | |
416 | } | |
417 | iplink_parse_vf_vlan_info(vf, &argc, | |
418 | &argv, &ivvi); | |
419 | addattr_l(&req->n, sizeof(*req), | |
420 | IFLA_VF_VLAN_INFO, &ivvi, | |
421 | sizeof(ivvi)); | |
3fd86630 | 422 | } |
56e9f0ab | 423 | addattr_nest_end(&req->n, vfvlanlist); |
3fd86630 | 424 | } |
3fd86630 | 425 | } else if (matches(*argv, "rate") == 0) { |
1598b9ef | 426 | struct ifla_vf_tx_rate ivt; |
6c5ffb9a | 427 | |
3fd86630 | 428 | NEXT_ARG(); |
6c5ffb9a | 429 | if (get_unsigned(&ivt.rate, *argv, 0)) |
1598b9ef | 430 | invarg("Invalid \"rate\" value\n", *argv); |
6c5ffb9a | 431 | |
1598b9ef | 432 | ivt.vf = vf; |
f89a2a05 SC |
433 | if (!new_rate_api) |
434 | addattr_l(&req->n, sizeof(*req), | |
435 | IFLA_VF_TX_RATE, &ivt, sizeof(ivt)); | |
436 | else if (!override_legacy_rate) | |
437 | tivt.max_tx_rate = ivt.rate; | |
438 | ||
439 | } else if (matches(*argv, "max_tx_rate") == 0) { | |
440 | NEXT_ARG(); | |
441 | if (get_unsigned(&tivt.max_tx_rate, *argv, 0)) | |
442 | invarg("Invalid \"max tx rate\" value\n", | |
443 | *argv); | |
444 | tivt.vf = vf; | |
445 | ||
446 | } else if (matches(*argv, "min_tx_rate") == 0) { | |
447 | NEXT_ARG(); | |
448 | if (get_unsigned(&tivt.min_tx_rate, *argv, 0)) | |
449 | invarg("Invalid \"min tx rate\" value\n", | |
450 | *argv); | |
451 | tivt.vf = vf; | |
af89576d | 452 | |
7b8179c7 GR |
453 | } else if (matches(*argv, "spoofchk") == 0) { |
454 | struct ifla_vf_spoofchk ivs; | |
6c5ffb9a | 455 | |
7b8179c7 GR |
456 | NEXT_ARG(); |
457 | if (matches(*argv, "on") == 0) | |
458 | ivs.setting = 1; | |
459 | else if (matches(*argv, "off") == 0) | |
460 | ivs.setting = 0; | |
461 | else | |
ff1e35ed | 462 | return on_off("spoofchk", *argv); |
7b8179c7 | 463 | ivs.vf = vf; |
ef0a738c SH |
464 | addattr_l(&req->n, sizeof(*req), IFLA_VF_SPOOFCHK, |
465 | &ivs, sizeof(ivs)); | |
7b8179c7 | 466 | |
6c55c8c4 VZ |
467 | } else if (matches(*argv, "query_rss") == 0) { |
468 | struct ifla_vf_rss_query_en ivs; | |
6c5ffb9a | 469 | |
6c55c8c4 VZ |
470 | NEXT_ARG(); |
471 | if (matches(*argv, "on") == 0) | |
472 | ivs.setting = 1; | |
473 | else if (matches(*argv, "off") == 0) | |
474 | ivs.setting = 0; | |
475 | else | |
ff1e35ed | 476 | return on_off("query_rss", *argv); |
6c55c8c4 | 477 | ivs.vf = vf; |
ef0a738c SH |
478 | addattr_l(&req->n, sizeof(*req), IFLA_VF_RSS_QUERY_EN, |
479 | &ivs, sizeof(ivs)); | |
6c55c8c4 | 480 | |
b6d77d9e HS |
481 | } else if (matches(*argv, "trust") == 0) { |
482 | struct ifla_vf_trust ivt; | |
56f5daac | 483 | |
b6d77d9e HS |
484 | NEXT_ARG(); |
485 | if (matches(*argv, "on") == 0) | |
486 | ivt.setting = 1; | |
487 | else if (matches(*argv, "off") == 0) | |
488 | ivt.setting = 0; | |
489 | else | |
490 | invarg("Invalid \"trust\" value\n", *argv); | |
491 | ivt.vf = vf; | |
ef0a738c SH |
492 | addattr_l(&req->n, sizeof(*req), IFLA_VF_TRUST, |
493 | &ivt, sizeof(ivt)); | |
b6d77d9e | 494 | |
07fa9c15 RE |
495 | } else if (matches(*argv, "state") == 0) { |
496 | struct ifla_vf_link_state ivl; | |
6c5ffb9a | 497 | |
07fa9c15 RE |
498 | NEXT_ARG(); |
499 | if (matches(*argv, "auto") == 0) | |
500 | ivl.link_state = IFLA_VF_LINK_STATE_AUTO; | |
501 | else if (matches(*argv, "enable") == 0) | |
502 | ivl.link_state = IFLA_VF_LINK_STATE_ENABLE; | |
503 | else if (matches(*argv, "disable") == 0) | |
504 | ivl.link_state = IFLA_VF_LINK_STATE_DISABLE; | |
505 | else | |
506 | invarg("Invalid \"state\" value\n", *argv); | |
507 | ivl.vf = vf; | |
ef0a738c SH |
508 | addattr_l(&req->n, sizeof(*req), IFLA_VF_LINK_STATE, |
509 | &ivl, sizeof(ivl)); | |
d91fb3f4 EC |
510 | } else if (matches(*argv, "node_guid") == 0) { |
511 | struct ifla_vf_guid ivg; | |
512 | ||
513 | NEXT_ARG(); | |
514 | ivg.vf = vf; | |
515 | if (get_guid(&ivg.guid, *argv)) { | |
516 | invarg("Invalid GUID format\n", *argv); | |
517 | return -1; | |
518 | } | |
ef0a738c SH |
519 | addattr_l(&req->n, sizeof(*req), IFLA_VF_IB_NODE_GUID, |
520 | &ivg, sizeof(ivg)); | |
d91fb3f4 EC |
521 | } else if (matches(*argv, "port_guid") == 0) { |
522 | struct ifla_vf_guid ivg; | |
523 | ||
524 | NEXT_ARG(); | |
525 | ivg.vf = vf; | |
526 | if (get_guid(&ivg.guid, *argv)) { | |
527 | invarg("Invalid GUID format\n", *argv); | |
528 | return -1; | |
529 | } | |
ef0a738c SH |
530 | addattr_l(&req->n, sizeof(*req), IFLA_VF_IB_PORT_GUID, |
531 | &ivg, sizeof(ivg)); | |
3fd86630 CW |
532 | } else { |
533 | /* rewind arg */ | |
534 | PREV_ARG(); | |
535 | break; | |
536 | } | |
537 | } | |
538 | ||
f89a2a05 SC |
539 | if (new_rate_api) { |
540 | int tmin, tmax; | |
9a02651a | 541 | |
f89a2a05 SC |
542 | if (tivt.min_tx_rate == -1 || tivt.max_tx_rate == -1) { |
543 | ipaddr_get_vf_rate(tivt.vf, &tmin, &tmax, dev_index); | |
544 | if (tivt.min_tx_rate == -1) | |
545 | tivt.min_tx_rate = tmin; | |
546 | if (tivt.max_tx_rate == -1) | |
547 | tivt.max_tx_rate = tmax; | |
548 | } | |
549 | addattr_l(&req->n, sizeof(*req), IFLA_VF_RATE, &tivt, | |
550 | sizeof(tivt)); | |
551 | } | |
552 | ||
3fd86630 CW |
553 | if (argc == *argcp) |
554 | incomplete_command(); | |
555 | ||
1598b9ef | 556 | addattr_nest_end(&req->n, vfinfo); |
3fd86630 CW |
557 | |
558 | *argcp = argc; | |
559 | *argvp = argv; | |
1598b9ef | 560 | return 0; |
3fd86630 CW |
561 | } |
562 | ||
909dfe2c | 563 | int iplink_parse(int argc, char **argv, struct iplink_req *req, |
6c5ffb9a SH |
564 | char **name, char **type, char **link, char **dev, |
565 | int *group, int *index) | |
1d934839 | 566 | { |
909dfe2c PE |
567 | int ret, len; |
568 | char abuf[32]; | |
1d934839 PM |
569 | int qlen = -1; |
570 | int mtu = -1; | |
e2613dc8 | 571 | int netns = -1; |
ae7229d5 | 572 | int vf = -1; |
d992f3e6 JP |
573 | int numtxqueues = -1; |
574 | int numrxqueues = -1; | |
9a02651a | 575 | int dev_index = 0; |
ccdcbf35 | 576 | int link_netnsid = -1; |
8fe58d58 | 577 | int addr_len = 0; |
1d934839 | 578 | |
db02608b | 579 | *group = -1; |
909dfe2c | 580 | ret = argc; |
1d934839 PM |
581 | |
582 | while (argc > 0) { | |
583 | if (strcmp(*argv, "up") == 0) { | |
909dfe2c PE |
584 | req->i.ifi_change |= IFF_UP; |
585 | req->i.ifi_flags |= IFF_UP; | |
1d934839 | 586 | } else if (strcmp(*argv, "down") == 0) { |
909dfe2c PE |
587 | req->i.ifi_change |= IFF_UP; |
588 | req->i.ifi_flags &= ~IFF_UP; | |
1d934839 PM |
589 | } else if (strcmp(*argv, "name") == 0) { |
590 | NEXT_ARG(); | |
625df645 PS |
591 | if (check_ifname(*argv)) |
592 | invarg("\"name\" not a valid ifname", *argv); | |
909dfe2c | 593 | *name = *argv; |
5e25cf77 PE |
594 | } else if (strcmp(*argv, "index") == 0) { |
595 | NEXT_ARG(); | |
596 | *index = atoi(*argv); | |
3c682146 WC |
597 | if (*index < 0) |
598 | invarg("Invalid \"index\" value", *argv); | |
1d934839 PM |
599 | } else if (matches(*argv, "link") == 0) { |
600 | NEXT_ARG(); | |
909dfe2c | 601 | *link = *argv; |
1d934839 PM |
602 | } else if (matches(*argv, "address") == 0) { |
603 | NEXT_ARG(); | |
8fe58d58 | 604 | addr_len = ll_addr_a2n(abuf, sizeof(abuf), *argv); |
0aae2346 | 605 | if (addr_len < 0) |
cb2eb999 | 606 | return -1; |
bb6ab47b SH |
607 | addattr_l(&req->n, sizeof(*req), |
608 | IFLA_ADDRESS, abuf, addr_len); | |
1d934839 | 609 | } else if (matches(*argv, "broadcast") == 0 || |
6c5ffb9a | 610 | strcmp(*argv, "brd") == 0) { |
1d934839 PM |
611 | NEXT_ARG(); |
612 | len = ll_addr_a2n(abuf, sizeof(abuf), *argv); | |
cb2eb999 AH |
613 | if (len < 0) |
614 | return -1; | |
bb6ab47b SH |
615 | addattr_l(&req->n, sizeof(*req), |
616 | IFLA_BROADCAST, abuf, len); | |
1d934839 | 617 | } else if (matches(*argv, "txqueuelen") == 0 || |
6c5ffb9a SH |
618 | strcmp(*argv, "qlen") == 0 || |
619 | matches(*argv, "txqlen") == 0) { | |
1d934839 PM |
620 | NEXT_ARG(); |
621 | if (qlen != -1) | |
622 | duparg("txqueuelen", *argv); | |
623 | if (get_integer(&qlen, *argv, 0)) | |
624 | invarg("Invalid \"txqueuelen\" value\n", *argv); | |
bb6ab47b SH |
625 | addattr_l(&req->n, sizeof(*req), |
626 | IFLA_TXQLEN, &qlen, 4); | |
1d934839 PM |
627 | } else if (strcmp(*argv, "mtu") == 0) { |
628 | NEXT_ARG(); | |
629 | if (mtu != -1) | |
630 | duparg("mtu", *argv); | |
631 | if (get_integer(&mtu, *argv, 0)) | |
632 | invarg("Invalid \"mtu\" value\n", *argv); | |
909dfe2c | 633 | addattr_l(&req->n, sizeof(*req), IFLA_MTU, &mtu, 4); |
a872b870 | 634 | } else if (strcmp(*argv, "xdpgeneric") == 0 || |
14683814 | 635 | strcmp(*argv, "xdpdrv") == 0 || |
1b5e8094 | 636 | strcmp(*argv, "xdpoffload") == 0 || |
a872b870 DB |
637 | strcmp(*argv, "xdp") == 0) { |
638 | bool generic = strcmp(*argv, "xdpgeneric") == 0; | |
14683814 | 639 | bool drv = strcmp(*argv, "xdpdrv") == 0; |
1b5e8094 | 640 | bool offload = strcmp(*argv, "xdpoffload") == 0; |
a872b870 | 641 | |
c7272ca7 | 642 | NEXT_ARG(); |
4f2eb14f JK |
643 | if (xdp_parse(&argc, &argv, req, dev_index, |
644 | generic, drv, offload)) | |
c7272ca7 | 645 | exit(-1); |
4b726cb1 SH |
646 | } else if (strcmp(*argv, "netns") == 0) { |
647 | NEXT_ARG(); | |
648 | if (netns != -1) | |
649 | duparg("netns", *argv); | |
6c5ffb9a SH |
650 | netns = netns_get_fd(*argv); |
651 | if (netns >= 0) | |
ef0a738c SH |
652 | addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, |
653 | &netns, 4); | |
0dc34c77 | 654 | else if (get_integer(&netns, *argv, 0) == 0) |
bb6ab47b SH |
655 | addattr_l(&req->n, sizeof(*req), |
656 | IFLA_NET_NS_PID, &netns, 4); | |
0dc34c77 | 657 | else |
4b726cb1 | 658 | invarg("Invalid \"netns\" value\n", *argv); |
1d934839 PM |
659 | } else if (strcmp(*argv, "multicast") == 0) { |
660 | NEXT_ARG(); | |
909dfe2c | 661 | req->i.ifi_change |= IFF_MULTICAST; |
6c5ffb9a SH |
662 | |
663 | if (strcmp(*argv, "on") == 0) | |
909dfe2c | 664 | req->i.ifi_flags |= IFF_MULTICAST; |
6c5ffb9a | 665 | else if (strcmp(*argv, "off") == 0) |
909dfe2c | 666 | req->i.ifi_flags &= ~IFF_MULTICAST; |
6c5ffb9a | 667 | else |
14645ec2 | 668 | return on_off("multicast", *argv); |
1d934839 PM |
669 | } else if (strcmp(*argv, "allmulticast") == 0) { |
670 | NEXT_ARG(); | |
909dfe2c | 671 | req->i.ifi_change |= IFF_ALLMULTI; |
6c5ffb9a SH |
672 | |
673 | if (strcmp(*argv, "on") == 0) | |
909dfe2c | 674 | req->i.ifi_flags |= IFF_ALLMULTI; |
6c5ffb9a | 675 | else if (strcmp(*argv, "off") == 0) |
909dfe2c | 676 | req->i.ifi_flags &= ~IFF_ALLMULTI; |
6c5ffb9a | 677 | else |
14645ec2 | 678 | return on_off("allmulticast", *argv); |
1d934839 PM |
679 | } else if (strcmp(*argv, "promisc") == 0) { |
680 | NEXT_ARG(); | |
909dfe2c | 681 | req->i.ifi_change |= IFF_PROMISC; |
6c5ffb9a SH |
682 | |
683 | if (strcmp(*argv, "on") == 0) | |
909dfe2c | 684 | req->i.ifi_flags |= IFF_PROMISC; |
6c5ffb9a | 685 | else if (strcmp(*argv, "off") == 0) |
909dfe2c | 686 | req->i.ifi_flags &= ~IFF_PROMISC; |
6c5ffb9a | 687 | else |
14645ec2 | 688 | return on_off("promisc", *argv); |
1d934839 PM |
689 | } else if (strcmp(*argv, "trailers") == 0) { |
690 | NEXT_ARG(); | |
909dfe2c | 691 | req->i.ifi_change |= IFF_NOTRAILERS; |
6c5ffb9a SH |
692 | |
693 | if (strcmp(*argv, "off") == 0) | |
909dfe2c | 694 | req->i.ifi_flags |= IFF_NOTRAILERS; |
6c5ffb9a | 695 | else if (strcmp(*argv, "on") == 0) |
909dfe2c | 696 | req->i.ifi_flags &= ~IFF_NOTRAILERS; |
6c5ffb9a | 697 | else |
14645ec2 | 698 | return on_off("trailers", *argv); |
1d934839 PM |
699 | } else if (strcmp(*argv, "arp") == 0) { |
700 | NEXT_ARG(); | |
909dfe2c | 701 | req->i.ifi_change |= IFF_NOARP; |
6c5ffb9a SH |
702 | |
703 | if (strcmp(*argv, "on") == 0) | |
909dfe2c | 704 | req->i.ifi_flags &= ~IFF_NOARP; |
6c5ffb9a | 705 | else if (strcmp(*argv, "off") == 0) |
909dfe2c | 706 | req->i.ifi_flags |= IFF_NOARP; |
6c5ffb9a | 707 | else |
e543a6a8 | 708 | return on_off("arp", *argv); |
432b92a7 ZS |
709 | } else if (strcmp(*argv, "carrier") == 0) { |
710 | int carrier; | |
bb6ab47b | 711 | |
432b92a7 ZS |
712 | NEXT_ARG(); |
713 | if (strcmp(*argv, "on") == 0) | |
714 | carrier = 1; | |
715 | else if (strcmp(*argv, "off") == 0) | |
716 | carrier = 0; | |
717 | else | |
718 | return on_off("carrier", *argv); | |
719 | ||
720 | addattr8(&req->n, sizeof(*req), IFLA_CARRIER, carrier); | |
ae7229d5 | 721 | } else if (strcmp(*argv, "vf") == 0) { |
1598b9ef | 722 | struct rtattr *vflist; |
6c5ffb9a | 723 | |
ae7229d5 | 724 | NEXT_ARG(); |
6c5ffb9a | 725 | if (get_integer(&vf, *argv, 0)) |
ae7229d5 | 726 | invarg("Invalid \"vf\" value\n", *argv); |
6c5ffb9a | 727 | |
1598b9ef SH |
728 | vflist = addattr_nest(&req->n, sizeof(*req), |
729 | IFLA_VFINFO_LIST); | |
9a02651a SH |
730 | if (dev_index == 0) |
731 | missarg("dev"); | |
732 | ||
f89a2a05 | 733 | len = iplink_parse_vf(vf, &argc, &argv, req, dev_index); |
1598b9ef SH |
734 | if (len < 0) |
735 | return -1; | |
736 | addattr_nest_end(&req->n, vflist); | |
a1e191b9 JP |
737 | } else if (matches(*argv, "master") == 0) { |
738 | int ifindex; | |
6c5ffb9a | 739 | |
a1e191b9 JP |
740 | NEXT_ARG(); |
741 | ifindex = ll_name_to_index(*argv); | |
742 | if (!ifindex) | |
743 | invarg("Device does not exist\n", *argv); | |
744 | addattr_l(&req->n, sizeof(*req), IFLA_MASTER, | |
745 | &ifindex, 4); | |
104444c2 DA |
746 | } else if (strcmp(*argv, "vrf") == 0) { |
747 | int ifindex; | |
748 | ||
749 | NEXT_ARG(); | |
750 | ifindex = ll_name_to_index(*argv); | |
751 | if (!ifindex) | |
752 | invarg("Not a valid VRF name\n", *argv); | |
753 | if (!name_is_vrf(*argv)) | |
754 | invarg("Not a valid VRF name\n", *argv); | |
755 | addattr_l(&req->n, sizeof(*req), IFLA_MASTER, | |
756 | &ifindex, sizeof(ifindex)); | |
a1e191b9 JP |
757 | } else if (matches(*argv, "nomaster") == 0) { |
758 | int ifindex = 0; | |
6c5ffb9a | 759 | |
a1e191b9 JP |
760 | addattr_l(&req->n, sizeof(*req), IFLA_MASTER, |
761 | &ifindex, 4); | |
1d934839 PM |
762 | } else if (matches(*argv, "dynamic") == 0) { |
763 | NEXT_ARG(); | |
909dfe2c | 764 | req->i.ifi_change |= IFF_DYNAMIC; |
6c5ffb9a SH |
765 | |
766 | if (strcmp(*argv, "on") == 0) | |
909dfe2c | 767 | req->i.ifi_flags |= IFF_DYNAMIC; |
6c5ffb9a | 768 | else if (strcmp(*argv, "off") == 0) |
909dfe2c | 769 | req->i.ifi_flags &= ~IFF_DYNAMIC; |
6c5ffb9a | 770 | else |
14645ec2 | 771 | return on_off("dynamic", *argv); |
1d934839 PM |
772 | } else if (matches(*argv, "type") == 0) { |
773 | NEXT_ARG(); | |
909dfe2c | 774 | *type = *argv; |
1d934839 PM |
775 | argc--; argv++; |
776 | break; | |
ace9c961 SH |
777 | } else if (matches(*argv, "alias") == 0) { |
778 | NEXT_ARG(); | |
779 | addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS, | |
780 | *argv, strlen(*argv)); | |
781 | argc--; argv++; | |
782 | break; | |
db02608b VD |
783 | } else if (strcmp(*argv, "group") == 0) { |
784 | NEXT_ARG(); | |
785 | if (*group != -1) | |
786 | duparg("group", *argv); | |
787 | if (rtnl_group_a2n(group, *argv)) | |
788 | invarg("Invalid \"group\" value\n", *argv); | |
82499282 SH |
789 | } else if (strcmp(*argv, "mode") == 0) { |
790 | int mode; | |
6c5ffb9a | 791 | |
82499282 | 792 | NEXT_ARG(); |
4ccfb44d | 793 | mode = get_link_mode(*argv); |
82499282 SH |
794 | if (mode < 0) |
795 | invarg("Invalid link mode\n", *argv); | |
796 | addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode); | |
4f2fdd44 SH |
797 | } else if (strcmp(*argv, "state") == 0) { |
798 | int state; | |
6c5ffb9a | 799 | |
4f2fdd44 SH |
800 | NEXT_ARG(); |
801 | state = get_operstate(*argv); | |
802 | if (state < 0) | |
803 | invarg("Invalid operstate\n", *argv); | |
804 | ||
805 | addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state); | |
d992f3e6 JP |
806 | } else if (matches(*argv, "numtxqueues") == 0) { |
807 | NEXT_ARG(); | |
808 | if (numtxqueues != -1) | |
809 | duparg("numtxqueues", *argv); | |
810 | if (get_integer(&numtxqueues, *argv, 0)) | |
bb6ab47b SH |
811 | invarg("Invalid \"numtxqueues\" value\n", |
812 | *argv); | |
d992f3e6 JP |
813 | addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES, |
814 | &numtxqueues, 4); | |
815 | } else if (matches(*argv, "numrxqueues") == 0) { | |
816 | NEXT_ARG(); | |
817 | if (numrxqueues != -1) | |
818 | duparg("numrxqueues", *argv); | |
819 | if (get_integer(&numrxqueues, *argv, 0)) | |
bb6ab47b SH |
820 | invarg("Invalid \"numrxqueues\" value\n", |
821 | *argv); | |
d992f3e6 JP |
822 | addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES, |
823 | &numrxqueues, 4); | |
ff7c2084 JP |
824 | } else if (matches(*argv, "addrgenmode") == 0) { |
825 | struct rtattr *afs, *afs6; | |
826 | int mode; | |
6c5ffb9a | 827 | |
ff7c2084 JP |
828 | NEXT_ARG(); |
829 | mode = get_addr_gen_mode(*argv); | |
830 | if (mode < 0) | |
bb6ab47b SH |
831 | invarg("Invalid address generation mode\n", |
832 | *argv); | |
ff7c2084 JP |
833 | afs = addattr_nest(&req->n, sizeof(*req), IFLA_AF_SPEC); |
834 | afs6 = addattr_nest(&req->n, sizeof(*req), AF_INET6); | |
ef0a738c SH |
835 | addattr8(&req->n, sizeof(*req), |
836 | IFLA_INET6_ADDR_GEN_MODE, mode); | |
ff7c2084 JP |
837 | addattr_nest_end(&req->n, afs6); |
838 | addattr_nest_end(&req->n, afs); | |
ccdcbf35 ND |
839 | } else if (matches(*argv, "link-netnsid") == 0) { |
840 | NEXT_ARG(); | |
841 | if (link_netnsid != -1) | |
842 | duparg("link-netnsid", *argv); | |
843 | if (get_integer(&link_netnsid, *argv, 0)) | |
bb6ab47b SH |
844 | invarg("Invalid \"link-netnsid\" value\n", |
845 | *argv); | |
ccdcbf35 ND |
846 | addattr32(&req->n, sizeof(*req), IFLA_LINK_NETNSID, |
847 | link_netnsid); | |
18864827 AK |
848 | } else if (strcmp(*argv, "protodown") == 0) { |
849 | unsigned int proto_down; | |
850 | ||
851 | NEXT_ARG(); | |
852 | if (strcmp(*argv, "on") == 0) | |
853 | proto_down = 1; | |
854 | else if (strcmp(*argv, "off") == 0) | |
855 | proto_down = 0; | |
856 | else | |
857 | return on_off("protodown", *argv); | |
858 | addattr8(&req->n, sizeof(*req), IFLA_PROTO_DOWN, | |
859 | proto_down); | |
c2db423f SH |
860 | } else if (strcmp(*argv, "gso_max_size") == 0) { |
861 | unsigned int max_size; | |
862 | ||
863 | NEXT_ARG(); | |
051274b4 | 864 | if (get_unsigned(&max_size, *argv, 0) || |
08f9d166 | 865 | max_size > GSO_MAX_SIZE) |
c2db423f SH |
866 | invarg("Invalid \"gso_max_size\" value\n", |
867 | *argv); | |
08f9d166 SH |
868 | addattr32(&req->n, sizeof(*req), |
869 | IFLA_GSO_MAX_SIZE, max_size); | |
c2db423f SH |
870 | } else if (strcmp(*argv, "gso_max_segs") == 0) { |
871 | unsigned int max_segs; | |
872 | ||
873 | NEXT_ARG(); | |
08f9d166 SH |
874 | if (get_unsigned(&max_segs, *argv, 0) || |
875 | max_segs > GSO_MAX_SEGS) | |
c2db423f SH |
876 | invarg("Invalid \"gso_max_segs\" value\n", |
877 | *argv); | |
08f9d166 SH |
878 | addattr32(&req->n, sizeof(*req), |
879 | IFLA_GSO_MAX_SEGS, max_segs); | |
1d934839 | 880 | } else { |
05325552 PM |
881 | if (matches(*argv, "help") == 0) |
882 | usage(); | |
940a96e6 PS |
883 | |
884 | if (strcmp(*argv, "dev") == 0) | |
885 | NEXT_ARG(); | |
909dfe2c | 886 | if (*dev) |
1d934839 | 887 | duparg2("dev", *argv); |
625df645 PS |
888 | if (check_ifname(*argv)) |
889 | invarg("\"dev\" not a valid ifname", *argv); | |
909dfe2c | 890 | *dev = *argv; |
f89a2a05 | 891 | dev_index = ll_name_to_index(*dev); |
1d934839 PM |
892 | } |
893 | argc--; argv++; | |
894 | } | |
895 | ||
8fe58d58 PS |
896 | if (dev_index && addr_len) { |
897 | int halen = nl_get_ll_addr_len(dev_index); | |
ef0a738c | 898 | |
8fe58d58 PS |
899 | if (halen >= 0 && halen != addr_len) { |
900 | fprintf(stderr, | |
ef0a738c SH |
901 | "Invalid address length %d - must be %d bytes\n", |
902 | addr_len, halen); | |
8fe58d58 PS |
903 | return -1; |
904 | } | |
905 | } | |
906 | ||
909dfe2c PE |
907 | return ret - argc; |
908 | } | |
909 | ||
910 | static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv) | |
911 | { | |
909dfe2c PE |
912 | char *dev = NULL; |
913 | char *name = NULL; | |
914 | char *link = NULL; | |
915 | char *type = NULL; | |
3c682146 | 916 | int index = -1; |
db02608b | 917 | int group; |
909dfe2c | 918 | struct link_util *lu = NULL; |
d17b136f PS |
919 | struct iplink_req req = { |
920 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
921 | .n.nlmsg_flags = NLM_F_REQUEST | flags, | |
922 | .n.nlmsg_type = cmd, | |
923 | .i.ifi_family = preferred_family, | |
924 | }; | |
909dfe2c PE |
925 | int ret; |
926 | ||
ef0a738c SH |
927 | ret = iplink_parse(argc, argv, |
928 | &req, &name, &type, &link, &dev, &group, &index); | |
909dfe2c PE |
929 | if (ret < 0) |
930 | return ret; | |
931 | ||
932 | argc -= ret; | |
933 | argv += ret; | |
db02608b VD |
934 | |
935 | if (group != -1) { | |
936 | if (dev) | |
937 | addattr_l(&req.n, sizeof(req), IFLA_GROUP, | |
938 | &group, sizeof(group)); | |
939 | else { | |
940 | if (argc) { | |
6773bcc2 SH |
941 | fprintf(stderr, |
942 | "Garbage instead of arguments \"%s ...\". Try \"ip link help\".\n", | |
943 | *argv); | |
db02608b VD |
944 | return -1; |
945 | } | |
946 | if (flags & NLM_F_CREATE) { | |
bb6ab47b SH |
947 | fprintf(stderr, |
948 | "group cannot be used when creating devices.\n"); | |
db02608b VD |
949 | return -1; |
950 | } | |
951 | ||
952 | req.i.ifi_index = 0; | |
953 | addattr32(&req.n, sizeof(req), IFLA_GROUP, group); | |
86bf43c7 | 954 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
f921f567 | 955 | return -2; |
db02608b VD |
956 | return 0; |
957 | } | |
958 | } | |
959 | ||
1d934839 PM |
960 | if (!(flags & NLM_F_CREATE)) { |
961 | if (!dev) { | |
bb6ab47b SH |
962 | fprintf(stderr, |
963 | "Not enough information: \"dev\" argument is required.\n"); | |
1d934839 PM |
964 | exit(-1); |
965 | } | |
3c682146 | 966 | if (cmd == RTM_NEWLINK && index != -1) { |
bb6ab47b SH |
967 | fprintf(stderr, |
968 | "index can be used only when creating devices.\n"); | |
3c682146 WC |
969 | exit(-1); |
970 | } | |
1d934839 PM |
971 | |
972 | req.i.ifi_index = ll_name_to_index(dev); | |
973 | if (req.i.ifi_index == 0) { | |
974 | fprintf(stderr, "Cannot find device \"%s\"\n", dev); | |
975 | return -1; | |
976 | } | |
977 | } else { | |
978 | /* Allow "ip link add dev" and "ip link add name" */ | |
979 | if (!name) | |
980 | name = dev; | |
981 | ||
982 | if (link) { | |
983 | int ifindex; | |
984 | ||
985 | ifindex = ll_name_to_index(link); | |
986 | if (ifindex == 0) { | |
987 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
988 | link); | |
989 | return -1; | |
990 | } | |
991 | addattr_l(&req.n, sizeof(req), IFLA_LINK, &ifindex, 4); | |
992 | } | |
5e25cf77 | 993 | |
68ac9ab3 AW |
994 | if (index == -1) |
995 | req.i.ifi_index = 0; | |
996 | else | |
997 | req.i.ifi_index = index; | |
1d934839 PM |
998 | } |
999 | ||
1000 | if (name) { | |
625df645 PS |
1001 | addattr_l(&req.n, sizeof(req), |
1002 | IFLA_IFNAME, name, strlen(name) + 1); | |
1d934839 PM |
1003 | } |
1004 | ||
09fa3279 | 1005 | if (type) { |
a1e2e5fc | 1006 | struct rtattr *linkinfo; |
22a84711 | 1007 | char *ulinep = strchr(type, '_'); |
620ddeda NA |
1008 | int iflatype; |
1009 | ||
a1e2e5fc | 1010 | linkinfo = addattr_nest(&req.n, sizeof(req), IFLA_LINKINFO); |
09fa3279 SH |
1011 | addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type, |
1012 | strlen(type)); | |
1013 | ||
22a84711 HL |
1014 | lu = get_link_kind(type); |
1015 | if (ulinep && !strcmp(ulinep, "_slave")) | |
620ddeda | 1016 | iflatype = IFLA_INFO_SLAVE_DATA; |
22a84711 | 1017 | else |
620ddeda | 1018 | iflatype = IFLA_INFO_DATA; |
09fa3279 | 1019 | if (lu && argc) { |
bb6ab47b SH |
1020 | struct rtattr *data |
1021 | = addattr_nest(&req.n, | |
1022 | sizeof(req), iflatype); | |
09fa3279 SH |
1023 | |
1024 | if (lu->parse_opt && | |
1025 | lu->parse_opt(lu, argc, argv, &req.n)) | |
1026 | return -1; | |
1027 | ||
a1e2e5fc | 1028 | addattr_nest_end(&req.n, data); |
09fa3279 SH |
1029 | } else if (argc) { |
1030 | if (matches(*argv, "help") == 0) | |
1031 | usage(); | |
6773bcc2 SH |
1032 | fprintf(stderr, |
1033 | "Garbage instead of arguments \"%s ...\". Try \"ip link help\".\n", | |
1034 | *argv); | |
09fa3279 SH |
1035 | return -1; |
1036 | } | |
a1e2e5fc | 1037 | addattr_nest_end(&req.n, linkinfo); |
09fa3279 | 1038 | } else if (flags & NLM_F_CREATE) { |
bb6ab47b SH |
1039 | fprintf(stderr, |
1040 | "Not enough information: \"type\" argument is required\n"); | |
09fa3279 SH |
1041 | return -1; |
1042 | } | |
1043 | ||
86bf43c7 | 1044 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
f921f567 | 1045 | return -2; |
1d934839 PM |
1046 | |
1047 | return 0; | |
1048 | } | |
1049 | ||
50b9950d RP |
1050 | int iplink_get(unsigned int flags, char *name, __u32 filt_mask) |
1051 | { | |
d17b136f PS |
1052 | struct iplink_req req = { |
1053 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
1054 | .n.nlmsg_flags = NLM_F_REQUEST | flags, | |
1055 | .n.nlmsg_type = RTM_GETLINK, | |
1056 | .i.ifi_family = preferred_family, | |
1057 | }; | |
86bf43c7 | 1058 | struct nlmsghdr *answer; |
50b9950d | 1059 | |
50b9950d | 1060 | if (name) { |
625df645 PS |
1061 | addattr_l(&req.n, sizeof(req), |
1062 | IFLA_IFNAME, name, strlen(name) + 1); | |
50b9950d RP |
1063 | } |
1064 | addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filt_mask); | |
1065 | ||
86bf43c7 | 1066 | if (rtnl_talk(&rth, &req.n, &answer) < 0) |
50b9950d RP |
1067 | return -2; |
1068 | ||
e4a1216a | 1069 | open_json_object(NULL); |
5d295bb8 | 1070 | if (brief) |
86bf43c7 | 1071 | print_linkinfo_brief(NULL, answer, stdout, NULL); |
5d295bb8 | 1072 | else |
86bf43c7 | 1073 | print_linkinfo(NULL, answer, stdout); |
e4a1216a | 1074 | close_json_object(); |
50b9950d | 1075 | |
86bf43c7 | 1076 | free(answer); |
50b9950d RP |
1077 | return 0; |
1078 | } | |
1079 | ||
1d934839 | 1080 | #if IPLINK_IOCTL_COMPAT |
aba5acdf SH |
1081 | static int get_ctl_fd(void) |
1082 | { | |
1083 | int s_errno; | |
1084 | int fd; | |
1085 | ||
1086 | fd = socket(PF_INET, SOCK_DGRAM, 0); | |
1087 | if (fd >= 0) | |
1088 | return fd; | |
1089 | s_errno = errno; | |
1090 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); | |
1091 | if (fd >= 0) | |
1092 | return fd; | |
1093 | fd = socket(PF_INET6, SOCK_DGRAM, 0); | |
1094 | if (fd >= 0) | |
1095 | return fd; | |
1096 | errno = s_errno; | |
1097 | perror("Cannot create control socket"); | |
1098 | return -1; | |
1099 | } | |
1100 | ||
71058eb8 | 1101 | static int do_chflags(const char *dev, __u32 flags, __u32 mask) |
aba5acdf SH |
1102 | { |
1103 | struct ifreq ifr; | |
1104 | int fd; | |
1105 | int err; | |
1106 | ||
71058eb8 | 1107 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
aba5acdf SH |
1108 | fd = get_ctl_fd(); |
1109 | if (fd < 0) | |
1110 | return -1; | |
1111 | err = ioctl(fd, SIOCGIFFLAGS, &ifr); | |
1112 | if (err) { | |
1113 | perror("SIOCGIFFLAGS"); | |
1114 | close(fd); | |
1115 | return -1; | |
1116 | } | |
1117 | if ((ifr.ifr_flags^flags)&mask) { | |
1118 | ifr.ifr_flags &= ~mask; | |
1119 | ifr.ifr_flags |= mask&flags; | |
1120 | err = ioctl(fd, SIOCSIFFLAGS, &ifr); | |
1121 | if (err) | |
1122 | perror("SIOCSIFFLAGS"); | |
1123 | } | |
1124 | close(fd); | |
1125 | return err; | |
1126 | } | |
1127 | ||
71058eb8 | 1128 | static int do_changename(const char *dev, const char *newdev) |
aba5acdf SH |
1129 | { |
1130 | struct ifreq ifr; | |
1131 | int fd; | |
1132 | int err; | |
1133 | ||
71058eb8 SH |
1134 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
1135 | strncpy(ifr.ifr_newname, newdev, IFNAMSIZ); | |
aba5acdf SH |
1136 | fd = get_ctl_fd(); |
1137 | if (fd < 0) | |
1138 | return -1; | |
1139 | err = ioctl(fd, SIOCSIFNAME, &ifr); | |
1140 | if (err) { | |
1141 | perror("SIOCSIFNAME"); | |
1142 | close(fd); | |
1143 | return -1; | |
1144 | } | |
1145 | close(fd); | |
1146 | return err; | |
1147 | } | |
1148 | ||
71058eb8 | 1149 | static int set_qlen(const char *dev, int qlen) |
aba5acdf | 1150 | { |
d17b136f | 1151 | struct ifreq ifr = { .ifr_qlen = qlen }; |
aba5acdf SH |
1152 | int s; |
1153 | ||
1154 | s = get_ctl_fd(); | |
1155 | if (s < 0) | |
1156 | return -1; | |
1157 | ||
ae665a52 | 1158 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
aba5acdf SH |
1159 | if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) { |
1160 | perror("SIOCSIFXQLEN"); | |
1161 | close(s); | |
1162 | return -1; | |
1163 | } | |
1164 | close(s); | |
1165 | ||
ae665a52 | 1166 | return 0; |
aba5acdf SH |
1167 | } |
1168 | ||
71058eb8 | 1169 | static int set_mtu(const char *dev, int mtu) |
aba5acdf | 1170 | { |
d17b136f | 1171 | struct ifreq ifr = { .ifr_mtu = mtu }; |
aba5acdf SH |
1172 | int s; |
1173 | ||
1174 | s = get_ctl_fd(); | |
1175 | if (s < 0) | |
1176 | return -1; | |
1177 | ||
ae665a52 | 1178 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
aba5acdf SH |
1179 | if (ioctl(s, SIOCSIFMTU, &ifr) < 0) { |
1180 | perror("SIOCSIFMTU"); | |
1181 | close(s); | |
1182 | return -1; | |
1183 | } | |
1184 | close(s); | |
1185 | ||
ae665a52 | 1186 | return 0; |
aba5acdf SH |
1187 | } |
1188 | ||
71058eb8 | 1189 | static int get_address(const char *dev, int *htype) |
aba5acdf | 1190 | { |
d17b136f PS |
1191 | struct ifreq ifr = {}; |
1192 | struct sockaddr_ll me = { | |
1193 | .sll_family = AF_PACKET, | |
1194 | .sll_protocol = htons(ETH_P_LOOP), | |
1195 | }; | |
f332d169 | 1196 | socklen_t alen; |
aba5acdf SH |
1197 | int s; |
1198 | ||
1199 | s = socket(PF_PACKET, SOCK_DGRAM, 0); | |
ae665a52 | 1200 | if (s < 0) { |
aba5acdf SH |
1201 | perror("socket(PF_PACKET)"); |
1202 | return -1; | |
1203 | } | |
1204 | ||
71058eb8 | 1205 | strncpy(ifr.ifr_name, dev, IFNAMSIZ); |
aba5acdf SH |
1206 | if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { |
1207 | perror("SIOCGIFINDEX"); | |
1208 | close(s); | |
1209 | return -1; | |
1210 | } | |
1211 | ||
aba5acdf | 1212 | me.sll_ifindex = ifr.ifr_ifindex; |
6c5ffb9a | 1213 | if (bind(s, (struct sockaddr *)&me, sizeof(me)) == -1) { |
aba5acdf SH |
1214 | perror("bind"); |
1215 | close(s); | |
1216 | return -1; | |
1217 | } | |
1218 | ||
1219 | alen = sizeof(me); | |
6c5ffb9a | 1220 | if (getsockname(s, (struct sockaddr *)&me, &alen) == -1) { |
aba5acdf SH |
1221 | perror("getsockname"); |
1222 | close(s); | |
1223 | return -1; | |
1224 | } | |
1225 | close(s); | |
1226 | *htype = me.sll_hatype; | |
1227 | return me.sll_halen; | |
1228 | } | |
1229 | ||
ae665a52 | 1230 | static int parse_address(const char *dev, int hatype, int halen, |
7b565754 | 1231 | char *lla, struct ifreq *ifr) |
aba5acdf SH |
1232 | { |
1233 | int alen; | |
1234 | ||
1235 | memset(ifr, 0, sizeof(*ifr)); | |
71058eb8 | 1236 | strncpy(ifr->ifr_name, dev, IFNAMSIZ); |
aba5acdf SH |
1237 | ifr->ifr_hwaddr.sa_family = hatype; |
1238 | alen = ll_addr_a2n(ifr->ifr_hwaddr.sa_data, 14, lla); | |
1239 | if (alen < 0) | |
1240 | return -1; | |
1241 | if (alen != halen) { | |
bb6ab47b SH |
1242 | fprintf(stderr, |
1243 | "Wrong address (%s) length: expected %d bytes\n", | |
ef0a738c | 1244 | lla, halen); |
aba5acdf SH |
1245 | return -1; |
1246 | } | |
ae665a52 | 1247 | return 0; |
aba5acdf SH |
1248 | } |
1249 | ||
1250 | static int set_address(struct ifreq *ifr, int brd) | |
1251 | { | |
1252 | int s; | |
1253 | ||
1254 | s = get_ctl_fd(); | |
1255 | if (s < 0) | |
1256 | return -1; | |
1257 | if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) { | |
1258 | perror(brd?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR"); | |
1259 | close(s); | |
1260 | return -1; | |
1261 | } | |
1262 | close(s); | |
ae665a52 | 1263 | return 0; |
aba5acdf SH |
1264 | } |
1265 | ||
aba5acdf SH |
1266 | static int do_set(int argc, char **argv) |
1267 | { | |
1268 | char *dev = NULL; | |
1269 | __u32 mask = 0; | |
1270 | __u32 flags = 0; | |
1271 | int qlen = -1; | |
1272 | int mtu = -1; | |
1273 | char *newaddr = NULL; | |
1274 | char *newbrd = NULL; | |
1275 | struct ifreq ifr0, ifr1; | |
1276 | char *newname = NULL; | |
1277 | int htype, halen; | |
1278 | ||
1279 | while (argc > 0) { | |
1280 | if (strcmp(*argv, "up") == 0) { | |
1281 | mask |= IFF_UP; | |
1282 | flags |= IFF_UP; | |
1283 | } else if (strcmp(*argv, "down") == 0) { | |
1284 | mask |= IFF_UP; | |
1285 | flags &= ~IFF_UP; | |
1286 | } else if (strcmp(*argv, "name") == 0) { | |
1287 | NEXT_ARG(); | |
625df645 PS |
1288 | if (check_ifname(*argv)) |
1289 | invarg("\"name\" not a valid ifname", *argv); | |
aba5acdf SH |
1290 | newname = *argv; |
1291 | } else if (matches(*argv, "address") == 0) { | |
1292 | NEXT_ARG(); | |
1293 | newaddr = *argv; | |
1294 | } else if (matches(*argv, "broadcast") == 0 || | |
1295 | strcmp(*argv, "brd") == 0) { | |
1296 | NEXT_ARG(); | |
1297 | newbrd = *argv; | |
1298 | } else if (matches(*argv, "txqueuelen") == 0 || | |
1299 | strcmp(*argv, "qlen") == 0 || | |
1300 | matches(*argv, "txqlen") == 0) { | |
1301 | NEXT_ARG(); | |
1302 | if (qlen != -1) | |
1303 | duparg("txqueuelen", *argv); | |
1304 | if (get_integer(&qlen, *argv, 0)) | |
1305 | invarg("Invalid \"txqueuelen\" value\n", *argv); | |
1306 | } else if (strcmp(*argv, "mtu") == 0) { | |
1307 | NEXT_ARG(); | |
1308 | if (mtu != -1) | |
1309 | duparg("mtu", *argv); | |
1310 | if (get_integer(&mtu, *argv, 0)) | |
1311 | invarg("Invalid \"mtu\" value\n", *argv); | |
1312 | } else if (strcmp(*argv, "multicast") == 0) { | |
1313 | NEXT_ARG(); | |
1314 | mask |= IFF_MULTICAST; | |
6c5ffb9a SH |
1315 | |
1316 | if (strcmp(*argv, "on") == 0) | |
aba5acdf | 1317 | flags |= IFF_MULTICAST; |
6c5ffb9a | 1318 | else if (strcmp(*argv, "off") == 0) |
aba5acdf | 1319 | flags &= ~IFF_MULTICAST; |
6c5ffb9a | 1320 | else |
14645ec2 | 1321 | return on_off("multicast", *argv); |
d27b1b5b | 1322 | } else if (strcmp(*argv, "allmulticast") == 0) { |
1323 | NEXT_ARG(); | |
1324 | mask |= IFF_ALLMULTI; | |
6c5ffb9a SH |
1325 | |
1326 | if (strcmp(*argv, "on") == 0) | |
d27b1b5b | 1327 | flags |= IFF_ALLMULTI; |
6c5ffb9a | 1328 | else if (strcmp(*argv, "off") == 0) |
d27b1b5b | 1329 | flags &= ~IFF_ALLMULTI; |
6c5ffb9a | 1330 | else |
14645ec2 | 1331 | return on_off("allmulticast", *argv); |
d27b1b5b | 1332 | } else if (strcmp(*argv, "promisc") == 0) { |
1333 | NEXT_ARG(); | |
1334 | mask |= IFF_PROMISC; | |
6c5ffb9a SH |
1335 | |
1336 | if (strcmp(*argv, "on") == 0) | |
d27b1b5b | 1337 | flags |= IFF_PROMISC; |
6c5ffb9a | 1338 | else if (strcmp(*argv, "off") == 0) |
d27b1b5b | 1339 | flags &= ~IFF_PROMISC; |
6c5ffb9a | 1340 | else |
14645ec2 | 1341 | return on_off("promisc", *argv); |
d27b1b5b | 1342 | } else if (strcmp(*argv, "trailers") == 0) { |
1343 | NEXT_ARG(); | |
1344 | mask |= IFF_NOTRAILERS; | |
6c5ffb9a SH |
1345 | |
1346 | if (strcmp(*argv, "off") == 0) | |
d27b1b5b | 1347 | flags |= IFF_NOTRAILERS; |
6c5ffb9a | 1348 | else if (strcmp(*argv, "on") == 0) |
d27b1b5b | 1349 | flags &= ~IFF_NOTRAILERS; |
6c5ffb9a | 1350 | else |
14645ec2 | 1351 | return on_off("trailers", *argv); |
aba5acdf SH |
1352 | } else if (strcmp(*argv, "arp") == 0) { |
1353 | NEXT_ARG(); | |
1354 | mask |= IFF_NOARP; | |
6c5ffb9a SH |
1355 | |
1356 | if (strcmp(*argv, "on") == 0) | |
aba5acdf | 1357 | flags &= ~IFF_NOARP; |
6c5ffb9a | 1358 | else if (strcmp(*argv, "off") == 0) |
aba5acdf | 1359 | flags |= IFF_NOARP; |
6c5ffb9a | 1360 | else |
ff1e35ed | 1361 | return on_off("arp", *argv); |
aba5acdf SH |
1362 | } else if (matches(*argv, "dynamic") == 0) { |
1363 | NEXT_ARG(); | |
1364 | mask |= IFF_DYNAMIC; | |
6c5ffb9a SH |
1365 | |
1366 | if (strcmp(*argv, "on") == 0) | |
aba5acdf | 1367 | flags |= IFF_DYNAMIC; |
6c5ffb9a | 1368 | else if (strcmp(*argv, "off") == 0) |
aba5acdf | 1369 | flags &= ~IFF_DYNAMIC; |
6c5ffb9a | 1370 | else |
14645ec2 | 1371 | return on_off("dynamic", *argv); |
aba5acdf | 1372 | } else { |
6c5ffb9a | 1373 | if (strcmp(*argv, "dev") == 0) |
aba5acdf | 1374 | NEXT_ARG(); |
8aacb9bb | 1375 | else if (matches(*argv, "help") == 0) |
aba5acdf | 1376 | usage(); |
6c5ffb9a | 1377 | |
aba5acdf SH |
1378 | if (dev) |
1379 | duparg2("dev", *argv); | |
625df645 PS |
1380 | if (check_ifname(*argv)) |
1381 | invarg("\"dev\" not a valid ifname", *argv); | |
aba5acdf SH |
1382 | dev = *argv; |
1383 | } | |
1384 | argc--; argv++; | |
1385 | } | |
1386 | ||
1387 | if (!dev) { | |
ef0a738c SH |
1388 | fprintf(stderr, |
1389 | "Not enough of information: \"dev\" argument is required.\n"); | |
aba5acdf SH |
1390 | exit(-1); |
1391 | } | |
1392 | ||
1393 | if (newaddr || newbrd) { | |
1394 | halen = get_address(dev, &htype); | |
1395 | if (halen < 0) | |
1396 | return -1; | |
1397 | if (newaddr) { | |
bb6ab47b SH |
1398 | if (parse_address(dev, htype, halen, |
1399 | newaddr, &ifr0) < 0) | |
aba5acdf SH |
1400 | return -1; |
1401 | } | |
1402 | if (newbrd) { | |
bb6ab47b SH |
1403 | if (parse_address(dev, htype, halen, |
1404 | newbrd, &ifr1) < 0) | |
ae665a52 | 1405 | return -1; |
aba5acdf SH |
1406 | } |
1407 | } | |
1408 | ||
1409 | if (newname && strcmp(dev, newname)) { | |
1410 | if (do_changename(dev, newname) < 0) | |
1411 | return -1; | |
1412 | dev = newname; | |
1413 | } | |
ae665a52 | 1414 | if (qlen != -1) { |
aba5acdf | 1415 | if (set_qlen(dev, qlen) < 0) |
ae665a52 | 1416 | return -1; |
aba5acdf | 1417 | } |
ae665a52 | 1418 | if (mtu != -1) { |
aba5acdf | 1419 | if (set_mtu(dev, mtu) < 0) |
ae665a52 | 1420 | return -1; |
aba5acdf SH |
1421 | } |
1422 | if (newaddr || newbrd) { | |
1423 | if (newbrd) { | |
1424 | if (set_address(&ifr1, 1) < 0) | |
ae665a52 | 1425 | return -1; |
aba5acdf SH |
1426 | } |
1427 | if (newaddr) { | |
1428 | if (set_address(&ifr0, 0) < 0) | |
1429 | return -1; | |
1430 | } | |
1431 | } | |
1432 | if (mask) | |
1433 | return do_chflags(dev, flags, mask); | |
1434 | return 0; | |
1435 | } | |
1d934839 | 1436 | #endif /* IPLINK_IOCTL_COMPAT */ |
aba5acdf | 1437 | |
837552b4 RS |
1438 | static void print_mpls_stats(FILE *fp, struct rtattr *attr) |
1439 | { | |
1440 | struct rtattr *mrtb[MPLS_STATS_MAX+1]; | |
1441 | struct mpls_link_stats *stats; | |
1442 | ||
1443 | parse_rtattr(mrtb, MPLS_STATS_MAX, RTA_DATA(attr), | |
1444 | RTA_PAYLOAD(attr)); | |
1445 | if (!mrtb[MPLS_STATS_LINK]) | |
1446 | return; | |
1447 | ||
1448 | stats = RTA_DATA(mrtb[MPLS_STATS_LINK]); | |
1449 | ||
1450 | fprintf(fp, " mpls:\n"); | |
1451 | fprintf(fp, " RX: bytes packets errors dropped noroute\n"); | |
1452 | fprintf(fp, " "); | |
1453 | print_num(fp, 10, stats->rx_bytes); | |
1454 | print_num(fp, 8, stats->rx_packets); | |
1455 | print_num(fp, 7, stats->rx_errors); | |
1456 | print_num(fp, 8, stats->rx_dropped); | |
1457 | print_num(fp, 7, stats->rx_noroute); | |
1458 | fprintf(fp, "\n"); | |
1459 | fprintf(fp, " TX: bytes packets errors dropped\n"); | |
1460 | fprintf(fp, " "); | |
1461 | print_num(fp, 10, stats->tx_bytes); | |
1462 | print_num(fp, 8, stats->tx_packets); | |
1463 | print_num(fp, 7, stats->tx_errors); | |
1464 | print_num(fp, 7, stats->tx_dropped); | |
1465 | fprintf(fp, "\n"); | |
1466 | } | |
1467 | ||
1468 | static void print_af_stats_attr(FILE *fp, int ifindex, struct rtattr *attr) | |
1469 | { | |
1470 | bool if_printed = false; | |
1471 | struct rtattr *i; | |
1472 | int rem; | |
1473 | ||
1474 | rem = RTA_PAYLOAD(attr); | |
1475 | for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { | |
1476 | if (preferred_family != AF_UNSPEC && | |
1477 | i->rta_type != preferred_family) | |
1478 | continue; | |
1479 | ||
1480 | if (!if_printed) { | |
1481 | fprintf(fp, "%u: %s\n", ifindex, | |
1482 | ll_index_to_name(ifindex)); | |
1483 | if_printed = true; | |
1484 | } | |
1485 | ||
1486 | switch (i->rta_type) { | |
1487 | case AF_MPLS: | |
1488 | print_mpls_stats(fp, i); | |
1489 | break; | |
1490 | default: | |
1491 | fprintf(fp, " unknown af(%d)\n", i->rta_type); | |
1492 | break; | |
1493 | } | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | struct af_stats_ctx { | |
1498 | FILE *fp; | |
1499 | int ifindex; | |
1500 | }; | |
1501 | ||
1502 | static int print_af_stats(const struct sockaddr_nl *who, | |
1503 | struct nlmsghdr *n, | |
1504 | void *arg) | |
1505 | { | |
1506 | struct if_stats_msg *ifsm = NLMSG_DATA(n); | |
1507 | struct rtattr *tb[IFLA_STATS_MAX+1]; | |
1508 | int len = n->nlmsg_len; | |
1509 | struct af_stats_ctx *ctx = arg; | |
1510 | FILE *fp = ctx->fp; | |
1511 | ||
1512 | len -= NLMSG_LENGTH(sizeof(*ifsm)); | |
1513 | if (len < 0) { | |
1514 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
1515 | return -1; | |
1516 | } | |
1517 | ||
1518 | if (ctx->ifindex && ifsm->ifindex != ctx->ifindex) | |
1519 | return 0; | |
1520 | ||
1521 | parse_rtattr(tb, IFLA_STATS_MAX, IFLA_STATS_RTA(ifsm), len); | |
1522 | ||
1523 | if (tb[IFLA_STATS_AF_SPEC]) | |
1524 | print_af_stats_attr(fp, ifsm->ifindex, tb[IFLA_STATS_AF_SPEC]); | |
1525 | ||
1526 | fflush(fp); | |
1527 | return 0; | |
1528 | } | |
1529 | ||
1530 | static int iplink_afstats(int argc, char **argv) | |
1531 | { | |
1532 | __u32 filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_AF_SPEC); | |
1533 | const char *filter_dev = NULL; | |
1534 | struct af_stats_ctx ctx = { | |
1535 | .fp = stdout, | |
1536 | .ifindex = 0, | |
1537 | }; | |
1538 | ||
1539 | while (argc > 0) { | |
1540 | if (strcmp(*argv, "dev") == 0) { | |
1541 | NEXT_ARG(); | |
1542 | if (filter_dev) | |
1543 | duparg2("dev", *argv); | |
1544 | filter_dev = *argv; | |
1545 | } else if (matches(*argv, "help") == 0) { | |
1546 | usage(); | |
1547 | } else { | |
1548 | fprintf(stderr, | |
1549 | "Command \"%s\" is unknown, try \"ip link help\".\n", | |
1550 | *argv); | |
1551 | exit(-1); | |
1552 | } | |
1553 | ||
1554 | argv++; argc--; | |
1555 | } | |
1556 | ||
1557 | if (filter_dev) { | |
1558 | ctx.ifindex = ll_name_to_index(filter_dev); | |
1559 | if (ctx.ifindex <= 0) { | |
1560 | fprintf(stderr, | |
1561 | "Device \"%s\" does not exist.\n", | |
1562 | filter_dev); | |
1563 | return -1; | |
1564 | } | |
1565 | } | |
1566 | ||
1567 | if (rtnl_wilddump_stats_req_filter(&rth, AF_UNSPEC, | |
1568 | RTM_GETSTATS, | |
1569 | filt_mask) < 0) { | |
1570 | perror("Cannont send dump request"); | |
1571 | return 1; | |
1572 | } | |
1573 | ||
1574 | if (rtnl_dump_filter(&rth, print_af_stats, &ctx) < 0) { | |
1575 | fprintf(stderr, "Dump terminated\n"); | |
1576 | return 1; | |
1577 | } | |
1578 | ||
1579 | return 0; | |
1580 | } | |
1581 | ||
561e650e | 1582 | static void do_help(int argc, char **argv) |
1583 | { | |
1584 | struct link_util *lu = NULL; | |
1585 | ||
1586 | if (argc <= 0) { | |
1587 | usage(); | |
6c5ffb9a | 1588 | return; |
561e650e | 1589 | } |
1590 | ||
1591 | lu = get_link_kind(*argv); | |
561e650e | 1592 | if (lu && lu->print_help) |
1593 | lu->print_help(lu, argc-1, argv+1, stdout); | |
1594 | else | |
1595 | usage(); | |
1596 | } | |
1597 | ||
aba5acdf SH |
1598 | int do_iplink(int argc, char **argv) |
1599 | { | |
6843d36e ZS |
1600 | if (argc < 1) |
1601 | return ipaddr_list_link(0, NULL); | |
1602 | ||
1603 | if (iplink_have_newlink()) { | |
1604 | if (matches(*argv, "add") == 0) | |
1605 | return iplink_modify(RTM_NEWLINK, | |
6c5ffb9a SH |
1606 | NLM_F_CREATE|NLM_F_EXCL, |
1607 | argc-1, argv+1); | |
6843d36e | 1608 | if (matches(*argv, "set") == 0 || |
6c5ffb9a | 1609 | matches(*argv, "change") == 0) |
6843d36e | 1610 | return iplink_modify(RTM_NEWLINK, 0, |
6c5ffb9a | 1611 | argc-1, argv+1); |
6843d36e ZS |
1612 | if (matches(*argv, "replace") == 0) |
1613 | return iplink_modify(RTM_NEWLINK, | |
6c5ffb9a SH |
1614 | NLM_F_CREATE|NLM_F_REPLACE, |
1615 | argc-1, argv+1); | |
6843d36e ZS |
1616 | if (matches(*argv, "delete") == 0) |
1617 | return iplink_modify(RTM_DELLINK, 0, | |
6c5ffb9a | 1618 | argc-1, argv+1); |
6843d36e | 1619 | } else { |
1d934839 | 1620 | #if IPLINK_IOCTL_COMPAT |
6843d36e ZS |
1621 | if (matches(*argv, "set") == 0) |
1622 | return do_set(argc-1, argv+1); | |
1d934839 | 1623 | #endif |
6843d36e | 1624 | } |
6c5ffb9a | 1625 | |
6843d36e | 1626 | if (matches(*argv, "show") == 0 || |
6c5ffb9a SH |
1627 | matches(*argv, "lst") == 0 || |
1628 | matches(*argv, "list") == 0) | |
6843d36e | 1629 | return ipaddr_list_link(argc-1, argv+1); |
6c5ffb9a | 1630 | |
94f1a22a NA |
1631 | if (matches(*argv, "xstats") == 0) |
1632 | return iplink_ifla_xstats(argc-1, argv+1); | |
1633 | ||
837552b4 RS |
1634 | if (matches(*argv, "afstats") == 0) { |
1635 | iplink_afstats(argc-1, argv+1); | |
1636 | return 0; | |
1637 | } | |
1638 | ||
6843d36e ZS |
1639 | if (matches(*argv, "help") == 0) { |
1640 | do_help(argc-1, argv+1); | |
1641 | return 0; | |
1642 | } | |
aba5acdf | 1643 | |
6843d36e | 1644 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip link help\".\n", |
6c5ffb9a | 1645 | *argv); |
aba5acdf SH |
1646 | exit(-1); |
1647 | } |