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