1 /* vi: set sw=4 ts=4: */
2 /*
3  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
4  * 			Patrick McHardy <kaber@trash.net>
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7  */
8 #include <net/if.h>
9 /*#include <net/if_packet.h> - not needed? */
10 #include <netpacket/packet.h>
11 #include <netinet/if_ether.h>
12 
13 #include <linux/if_vlan.h>
14 #include "ip_common.h"  /* #include "libbb.h" is inside */
15 #include "rt_names.h"
16 #include "utils.h"
17 
18 #undef  ETH_P_8021AD
19 #define ETH_P_8021AD            0x88A8
20 #undef  VLAN_FLAG_REORDER_HDR
21 #define VLAN_FLAG_REORDER_HDR   0x1
22 #undef  VLAN_FLAG_GVRP
23 #define VLAN_FLAG_GVRP          0x2
24 #undef  VLAN_FLAG_LOOSE_BINDING
25 #define VLAN_FLAG_LOOSE_BINDING 0x4
26 #undef  VLAN_FLAG_MVRP
27 #define VLAN_FLAG_MVRP          0x8
28 #undef  IFLA_VLAN_PROTOCOL
29 #define IFLA_VLAN_PROTOCOL      5
30 
31 #ifndef IFLA_LINKINFO
32 # define IFLA_LINKINFO 18
33 # define IFLA_INFO_KIND 1
34 # define IFLA_INFO_DATA 2
35 #endif
36 
37 #ifndef IFLA_VLAN_MAX
38 # define IFLA_VLAN_ID 1
39 # define IFLA_VLAN_FLAGS 2
40 struct ifla_vlan_flags {
41 	uint32_t	flags;
42 	uint32_t	mask;
43 };
44 #endif
45 
46 /* taken from linux/sockios.h */
47 #define SIOCSIFNAME  0x8923  /* set interface name */
48 
49 #if 0
50 # define dbg(...) bb_error_msg(__VA_ARGS__)
51 #else
52 # define dbg(...) ((void)0)
53 #endif
54 
55 
56 #define str_on_off "on\0""off\0"
57 
58 /* Exits on error */
get_ctl_fd(void)59 static int get_ctl_fd(void)
60 {
61 	int fd;
62 
63 	fd = socket(PF_INET, SOCK_DGRAM, 0);
64 	if (fd >= 0)
65 		return fd;
66 	fd = socket(PF_PACKET, SOCK_DGRAM, 0);
67 	if (fd >= 0)
68 		return fd;
69 	return xsocket(PF_INET6, SOCK_DGRAM, 0);
70 }
71 
72 /* Exits on error */
do_chflags(char * dev,uint32_t flags,uint32_t mask)73 static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
74 {
75 	struct ifreq ifr;
76 	int fd;
77 
78 	strncpy_IFNAMSIZ(ifr.ifr_name, dev);
79 	fd = get_ctl_fd();
80 	xioctl(fd, SIOCGIFFLAGS, &ifr);
81 	if ((ifr.ifr_flags ^ flags) & mask) {
82 		ifr.ifr_flags &= ~mask;
83 		ifr.ifr_flags |= mask & flags;
84 		xioctl(fd, SIOCSIFFLAGS, &ifr);
85 	}
86 	close(fd);
87 }
88 
89 /* Exits on error */
do_changename(char * dev,char * newdev)90 static void do_changename(char *dev, char *newdev)
91 {
92 	struct ifreq ifr;
93 	int fd;
94 
95 	strncpy_IFNAMSIZ(ifr.ifr_name, dev);
96 	strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
97 	fd = get_ctl_fd();
98 	xioctl(fd, SIOCSIFNAME, &ifr);
99 	close(fd);
100 }
101 
102 /* Exits on error */
set_qlen(char * dev,int qlen)103 static void set_qlen(char *dev, int qlen)
104 {
105 	struct ifreq ifr;
106 	int s;
107 
108 	s = get_ctl_fd();
109 	memset(&ifr, 0, sizeof(ifr));
110 	strncpy_IFNAMSIZ(ifr.ifr_name, dev);
111 	ifr.ifr_qlen = qlen;
112 	xioctl(s, SIOCSIFTXQLEN, &ifr);
113 	close(s);
114 }
115 
116 /* Exits on error */
set_mtu(char * dev,int mtu)117 static void set_mtu(char *dev, int mtu)
118 {
119 	struct ifreq ifr;
120 	int s;
121 
122 	s = get_ctl_fd();
123 	memset(&ifr, 0, sizeof(ifr));
124 	strncpy_IFNAMSIZ(ifr.ifr_name, dev);
125 	ifr.ifr_mtu = mtu;
126 	xioctl(s, SIOCSIFMTU, &ifr);
127 	close(s);
128 }
129 
130 /* Exits on error */
set_master(char * dev,int master)131 static void set_master(char *dev, int master)
132 {
133 	struct rtnl_handle rth;
134 	struct {
135 		struct nlmsghdr  n;
136 		struct ifinfomsg i;
137 		char             buf[1024];
138 	} req;
139 
140 	memset(&req, 0, sizeof(req));
141 
142 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
143 	req.n.nlmsg_flags = NLM_F_REQUEST;
144 	req.n.nlmsg_type = RTM_NEWLINK;
145 	req.i.ifi_family = preferred_family;
146 
147 	xrtnl_open(&rth);
148 	req.i.ifi_index = xll_name_to_index(dev);
149 	//printf("master %i for %i\n", master, req.i.ifi_index);
150 	addattr_l(&req.n, sizeof(req), IFLA_MASTER, &master, 4);
151 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
152 		xfunc_die();
153 }
154 
155 /* Exits on error */
set_netns(char * dev,int netns)156 static void set_netns(char *dev, int netns)
157 {
158 	struct rtnl_handle rth;
159 	struct {
160 		struct nlmsghdr  n;
161 		struct ifinfomsg i;
162 		char             buf[1024];
163 	} req;
164 
165 	memset(&req, 0, sizeof(req));
166 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
167 	req.n.nlmsg_flags = NLM_F_REQUEST;
168 	req.n.nlmsg_type = RTM_NEWLINK;
169 	req.i.ifi_family = preferred_family;
170 
171 	xrtnl_open(&rth);
172 	req.i.ifi_index = xll_name_to_index(dev);
173 	//printf("netns %i for %i\n", netns, req.i.ifi_index);
174 	addattr_l(&req.n, sizeof(req), IFLA_NET_NS_PID, &netns, 4);
175 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
176 		xfunc_die();
177 }
178 
179 /* Exits on error */
get_address(char * dev,int * htype)180 static int get_address(char *dev, int *htype)
181 {
182 	struct ifreq ifr;
183 	struct sockaddr_ll me;
184 	int s;
185 
186 	s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
187 
188 	/*memset(&ifr, 0, sizeof(ifr)); - SIOCGIFINDEX does not need to clear all */
189 	strncpy_IFNAMSIZ(ifr.ifr_name, dev);
190 	xioctl(s, SIOCGIFINDEX, &ifr);
191 
192 	memset(&me, 0, sizeof(me));
193 	me.sll_family = AF_PACKET;
194 	me.sll_ifindex = ifr.ifr_ifindex;
195 	me.sll_protocol = htons(ETH_P_LOOP);
196 	xbind(s, (struct sockaddr*)&me, sizeof(me));
197 	bb_getsockname(s, (struct sockaddr*)&me, sizeof(me));
198 	//never happens:
199 	//if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
200 	//	bb_perror_msg_and_die("getsockname");
201 	close(s);
202 	*htype = me.sll_hatype;
203 	return me.sll_halen;
204 }
205 
206 /* Exits on error */
parse_address(char * dev,int hatype,int halen,char * lla,struct ifreq * ifr)207 static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
208 {
209 	int alen;
210 
211 	memset(ifr, 0, sizeof(*ifr));
212 	strncpy_IFNAMSIZ(ifr->ifr_name, dev);
213 	ifr->ifr_hwaddr.sa_family = hatype;
214 
215 	alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
216 	alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
217 	if (alen < 0)
218 		exit(EXIT_FAILURE);
219 	if (alen != halen) {
220 		bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
221 	}
222 }
223 
224 /* Exits on error */
set_address(struct ifreq * ifr,int brd)225 static void set_address(struct ifreq *ifr, int brd)
226 {
227 	int s;
228 
229 	s = get_ctl_fd();
230 	if (brd)
231 		xioctl(s, SIOCSIFHWBROADCAST, ifr);
232 	else
233 		xioctl(s, SIOCSIFHWADDR, ifr);
234 	close(s);
235 }
236 
237 
238 static void die_must_be_on_off(const char *msg) NORETURN;
die_must_be_on_off(const char * msg)239 static void die_must_be_on_off(const char *msg)
240 {
241 	bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
242 }
243 
244 /* Return value becomes exitcode. It's okay to not return at all */
do_set(char ** argv)245 static int do_set(char **argv)
246 {
247 	char *dev = NULL;
248 	uint32_t mask = 0;
249 	uint32_t flags = 0;
250 	int qlen = -1;
251 	int mtu = -1;
252 	int master = -1;
253 	int netns = -1;
254 	char *newaddr = NULL;
255 	char *newbrd = NULL;
256 	struct ifreq ifr0, ifr1;
257 	char *newname = NULL;
258 	int htype, halen;
259 	/* If you add stuff here, update iplink_full_usage */
260 	static const char keywords[] ALIGN1 =
261 		"up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
262 		"arp\0""promisc\0""address\0""netns\0"
263 		"master\0""nomaster\0"
264 		"dev\0" /* must be last */;
265 	enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
266 		ARG_arp, ARG_promisc, ARG_addr, ARG_netns,
267 		ARG_master, ARG_nomaster,
268 		ARG_dev };
269 	enum { PARM_on = 0, PARM_off };
270 	smalluint key;
271 
272 	while (*argv) {
273 		/* substring search ensures that e.g. "addr" and "address"
274 		 * are both accepted */
275 		key = index_in_substrings(keywords, *argv);
276 		if (key == ARG_up) {
277 			mask |= IFF_UP;
278 			flags |= IFF_UP;
279 		} else if (key == ARG_down) {
280 			mask |= IFF_UP;
281 			flags &= ~IFF_UP;
282 		} else if (key == ARG_name) {
283 			NEXT_ARG();
284 			newname = *argv;
285 		} else if (key == ARG_mtu) {
286 			NEXT_ARG();
287 			if (mtu != -1)
288 				duparg("mtu", *argv);
289 			mtu = get_unsigned(*argv, "mtu");
290 		} else if (key == ARG_qlen) {
291 //TODO: txqueuelen, txqlen are synonyms to qlen
292 			NEXT_ARG();
293 			if (qlen != -1)
294 				duparg("qlen", *argv);
295 			qlen = get_unsigned(*argv, "qlen");
296 		} else if (key == ARG_addr) {
297 			NEXT_ARG();
298 			newaddr = *argv;
299 		} else if (key == ARG_master) {
300 			NEXT_ARG();
301 			master = xll_name_to_index(*argv);
302 		} else if (key == ARG_nomaster) {
303 			master = 0;
304 		} else if (key == ARG_netns) {
305 			NEXT_ARG();
306 			netns = get_unsigned(*argv, "netns");
307 		} else if (key >= ARG_dev) {
308 			/* ^^^^^^ ">=" here results in "dev IFACE" treated as default */
309 			if (key == ARG_dev) {
310 				NEXT_ARG();
311 			}
312 			if (dev)
313 				duparg2("dev", *argv);
314 			dev = *argv;
315 		} else {
316 			/* "on|off" options */
317 			int param;
318 			NEXT_ARG();
319 			param = index_in_strings(str_on_off, *argv);
320 			if (key == ARG_multicast) {
321 				if (param < 0)
322 					die_must_be_on_off("multicast");
323 				mask |= IFF_MULTICAST;
324 				if (param == PARM_on)
325 					flags |= IFF_MULTICAST;
326 				else
327 					flags &= ~IFF_MULTICAST;
328 			} else if (key == ARG_arp) {
329 				if (param < 0)
330 					die_must_be_on_off("arp");
331 				mask |= IFF_NOARP;
332 				if (param == PARM_on)
333 					flags &= ~IFF_NOARP;
334 				else
335 					flags |= IFF_NOARP;
336 			} else if (key == ARG_promisc) {
337 				if (param < 0)
338 					die_must_be_on_off("promisc");
339 				mask |= IFF_PROMISC;
340 				if (param == PARM_on)
341 					flags |= IFF_PROMISC;
342 				else
343 					flags &= ~IFF_PROMISC;
344 			}
345 		}
346 
347 /* Other keywords recognized by iproute2-3.12.0: */
348 #if 0
349 		} else if (matches(*argv, "broadcast") == 0 ||
350 				strcmp(*argv, "brd") == 0) {
351 			NEXT_ARG();
352 			len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
353 			if (len < 0)
354 				return -1;
355 			addattr_l(&req->n, sizeof(*req), IFLA_BROADCAST, abuf, len);
356                 } else if (strcmp(*argv, "netns") == 0) {
357                         NEXT_ARG();
358                         if (netns != -1)
359                                 duparg("netns", *argv);
360 			if ((netns = get_netns_fd(*argv)) >= 0)
361 				addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, &netns, 4);
362 			else if (get_integer(&netns, *argv, 0) == 0)
363 				addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_PID, &netns, 4);
364 			else
365                                 invarg_1_to_2(*argv, "netns");
366 		} else if (strcmp(*argv, "allmulticast") == 0) {
367 			NEXT_ARG();
368 			req->i.ifi_change |= IFF_ALLMULTI;
369 			if (strcmp(*argv, "on") == 0) {
370 				req->i.ifi_flags |= IFF_ALLMULTI;
371 			} else if (strcmp(*argv, "off") == 0) {
372 				req->i.ifi_flags &= ~IFF_ALLMULTI;
373 			} else
374 				return on_off("allmulticast", *argv);
375 		} else if (strcmp(*argv, "trailers") == 0) {
376 			NEXT_ARG();
377 			req->i.ifi_change |= IFF_NOTRAILERS;
378 			if (strcmp(*argv, "off") == 0) {
379 				req->i.ifi_flags |= IFF_NOTRAILERS;
380 			} else if (strcmp(*argv, "on") == 0) {
381 				req->i.ifi_flags &= ~IFF_NOTRAILERS;
382 			} else
383 				return on_off("trailers", *argv);
384 		} else if (strcmp(*argv, "vf") == 0) {
385 			struct rtattr *vflist;
386 			NEXT_ARG();
387 			if (get_integer(&vf,  *argv, 0)) {
388 				invarg_1_to_2(*argv, "vf");
389 			}
390 			vflist = addattr_nest(&req->n, sizeof(*req),
391 					      IFLA_VFINFO_LIST);
392 			len = iplink_parse_vf(vf, &argc, &argv, req);
393 			if (len < 0)
394 				return -1;
395 			addattr_nest_end(&req->n, vflist);
396 		} else if (matches(*argv, "master") == 0) {
397 			int ifindex;
398 			NEXT_ARG();
399 			ifindex = ll_name_to_index(*argv);
400 			if (!ifindex)
401 				invarg_1_to_2(*argv, "master");
402 			addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
403 				  &ifindex, 4);
404 		} else if (matches(*argv, "nomaster") == 0) {
405 			int ifindex = 0;
406 			addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
407 				  &ifindex, 4);
408 		} else if (matches(*argv, "dynamic") == 0) {
409 			NEXT_ARG();
410 			req->i.ifi_change |= IFF_DYNAMIC;
411 			if (strcmp(*argv, "on") == 0) {
412 				req->i.ifi_flags |= IFF_DYNAMIC;
413 			} else if (strcmp(*argv, "off") == 0) {
414 				req->i.ifi_flags &= ~IFF_DYNAMIC;
415 			} else
416 				return on_off("dynamic", *argv);
417 		} else if (matches(*argv, "alias") == 0) {
418 			NEXT_ARG();
419 			addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS,
420 				  *argv, strlen(*argv));
421 			argc--; argv++;
422 			break;
423 		} else if (strcmp(*argv, "group") == 0) {
424 			NEXT_ARG();
425 			if (*group != -1)
426 				duparg("group", *argv);
427 			if (rtnl_group_a2n(group, *argv))
428 				invarg_1_to_2(*argv, "group");
429 		} else if (strcmp(*argv, "mode") == 0) {
430 			int mode;
431 			NEXT_ARG();
432 			mode = get_link_mode(*argv);
433 			if (mode < 0)
434 				invarg_1_to_2(*argv, "mode");
435 			addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode);
436 		} else if (strcmp(*argv, "state") == 0) {
437 			int state;
438 			NEXT_ARG();
439 			state = get_operstate(*argv);
440 			if (state < 0)
441 				invarg_1_to_2(*argv, "state");
442 			addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state);
443 		} else if (matches(*argv, "numtxqueues") == 0) {
444 			NEXT_ARG();
445 			if (numtxqueues != -1)
446 				duparg("numtxqueues", *argv);
447 			if (get_integer(&numtxqueues, *argv, 0))
448 				invarg_1_to_2(*argv, "numtxqueues");
449 			addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES,
450 				  &numtxqueues, 4);
451 		} else if (matches(*argv, "numrxqueues") == 0) {
452 			NEXT_ARG();
453 			if (numrxqueues != -1)
454 				duparg("numrxqueues", *argv);
455 			if (get_integer(&numrxqueues, *argv, 0))
456 				invarg_1_to_2(*argv, "numrxqueues");
457 			addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES,
458 				  &numrxqueues, 4);
459 		}
460 #endif
461 
462 		argv++;
463 	}
464 
465 	if (!dev) {
466 		bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
467 	}
468 
469 	if (newaddr || newbrd) {
470 		halen = get_address(dev, &htype);
471 		if (newaddr) {
472 			parse_address(dev, htype, halen, newaddr, &ifr0);
473 			set_address(&ifr0, 0);
474 		}
475 		if (newbrd) {
476 			parse_address(dev, htype, halen, newbrd, &ifr1);
477 			set_address(&ifr1, 1);
478 		}
479 	}
480 
481 	if (newname && strcmp(dev, newname)) {
482 		do_changename(dev, newname);
483 		dev = newname;
484 	}
485 	if (qlen != -1) {
486 		set_qlen(dev, qlen);
487 	}
488 	if (mtu != -1) {
489 		set_mtu(dev, mtu);
490 	}
491 	if (master != -1) {
492 		set_master(dev, master);
493 	}
494 	if (netns != -1) {
495 		set_netns(dev, netns);
496 	}
497 	if (mask)
498 		do_chflags(dev, flags, mask);
499 	return 0;
500 }
501 
ipaddr_list_link(char ** argv)502 static int ipaddr_list_link(char **argv)
503 {
504 	preferred_family = AF_PACKET;
505 	return ipaddr_list_or_flush(argv, 0);
506 }
507 
vlan_parse_opt(char ** argv,struct nlmsghdr * n,unsigned int size)508 static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
509 {
510 	static const char keywords[] ALIGN1 =
511 		"id\0"
512 		"protocol\0"
513 		"reorder_hdr\0"
514 		"gvrp\0"
515 		"mvrp\0"
516 		"loose_binding\0"
517 	;
518 	static const char protocols[] ALIGN1 =
519 		"802.1q\0"
520 		"802.1ad\0"
521 	;
522 	enum {
523 		ARG_id = 0,
524 		ARG_protocol,
525 		ARG_reorder_hdr,
526 		ARG_gvrp,
527 		ARG_mvrp,
528 		ARG_loose_binding,
529 	};
530 	enum {
531 		PROTO_8021Q = 0,
532 		PROTO_8021AD,
533 	};
534 	enum {
535 		PARM_on = 0,
536 		PARM_off
537 	};
538 	int arg;
539 	uint16_t id, proto;
540 	struct ifla_vlan_flags flags = {};
541 
542 	while (*argv) {
543 		arg = index_in_substrings(keywords, *argv);
544 		if (arg < 0)
545 			invarg_1_to_2(*argv, "type vlan");
546 
547 		NEXT_ARG();
548 		if (arg == ARG_id) {
549 			id = get_u16(*argv, "id");
550 			addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
551 		} else if (arg == ARG_protocol) {
552 			arg = index_in_substrings(protocols, str_tolower(*argv));
553 			if (arg == PROTO_8021Q)
554 				proto = htons(ETH_P_8021Q);
555 			else if (arg == PROTO_8021AD)
556 				proto = htons(ETH_P_8021AD);
557 			else
558 				bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
559 								     *argv);
560 			addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
561 		} else {
562 			int param = index_in_strings(str_on_off, *argv);
563 			if (param < 0)
564 				die_must_be_on_off(nth_string(keywords, arg));
565 
566 			if (arg == ARG_reorder_hdr) {
567 				flags.mask |= VLAN_FLAG_REORDER_HDR;
568 				flags.flags &= ~VLAN_FLAG_REORDER_HDR;
569 				if (param == PARM_on)
570 					flags.flags |= VLAN_FLAG_REORDER_HDR;
571 			} else if (arg == ARG_gvrp) {
572 				flags.mask |= VLAN_FLAG_GVRP;
573 				flags.flags &= ~VLAN_FLAG_GVRP;
574 				if (param == PARM_on)
575 					flags.flags |= VLAN_FLAG_GVRP;
576 			} else if (arg == ARG_mvrp) {
577 				flags.mask |= VLAN_FLAG_MVRP;
578 				flags.flags &= ~VLAN_FLAG_MVRP;
579 				if (param == PARM_on)
580 					flags.flags |= VLAN_FLAG_MVRP;
581 			} else { /*if (arg == ARG_loose_binding) */
582 				flags.mask |= VLAN_FLAG_LOOSE_BINDING;
583 				flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
584 				if (param == PARM_on)
585 					flags.flags |= VLAN_FLAG_LOOSE_BINDING;
586 			}
587 		}
588 		argv++;
589 	}
590 
591 	if (flags.mask)
592 		addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
593 }
594 
vrf_parse_opt(char ** argv,struct nlmsghdr * n,unsigned int size)595 static void vrf_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
596 {
597 /* IFLA_VRF_TABLE is an enum, not a define -
598  * can't test "defined(IFLA_VRF_TABLE)".
599  */
600 #if !defined(IFLA_VRF_MAX)
601 # define IFLA_VRF_TABLE 1
602 #endif
603 	uint32_t table;
604 
605 	if (strcmp(*argv, "table") != 0)
606 		invarg_1_to_2(*argv, "type vrf");
607 
608 	NEXT_ARG();
609 	table = get_u32(*argv, "table");
610 	addattr_l(n, size, IFLA_VRF_TABLE, &table, sizeof(table));
611 }
612 
613 #ifndef NLMSG_TAIL
614 #define NLMSG_TAIL(nmsg) \
615 	((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
616 #endif
617 /* Return value becomes exitcode. It's okay to not return at all */
do_add_or_delete(char ** argv,const unsigned rtm)618 static int do_add_or_delete(char **argv, const unsigned rtm)
619 {
620 	static const char keywords[] ALIGN1 =
621 		"link\0""name\0""type\0""dev\0""address\0";
622 	enum {
623 		ARG_link,
624 		ARG_name,
625 		ARG_type,
626 		ARG_dev,
627 		ARG_address,
628 	};
629 	struct rtnl_handle rth;
630 	struct {
631 		struct nlmsghdr  n;
632 		struct ifinfomsg i;
633 		char             buf[1024];
634 	} req;
635 	smalluint arg;
636 	char *name_str = NULL;
637 	char *link_str = NULL;
638 	char *type_str = NULL;
639 	char *dev_str = NULL;
640 	char *address_str = NULL;
641 
642 	memset(&req, 0, sizeof(req));
643 
644 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
645 	req.n.nlmsg_flags = NLM_F_REQUEST;
646 	req.n.nlmsg_type = rtm;
647 	req.i.ifi_family = preferred_family;
648 	if (rtm == RTM_NEWLINK)
649 		req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
650 
651 	/* NB: update iplink_full_usage if you extend this code */
652 
653 	while (*argv) {
654 		arg = index_in_substrings(keywords, *argv);
655 		if (arg == ARG_type) {
656 			NEXT_ARG();
657 			type_str = *argv++;
658 			dbg("type_str:'%s'", type_str);
659 			break;
660 		}
661 		if (arg == ARG_link) {
662 			NEXT_ARG();
663 			link_str = *argv;
664 			dbg("link_str:'%s'", link_str);
665 		} else if (arg == ARG_name) {
666 			NEXT_ARG();
667 			name_str = *argv;
668 			dbg("name_str:'%s'", name_str);
669 		} else if (arg == ARG_address) {
670 			NEXT_ARG();
671 			address_str = *argv;
672 			dbg("address_str:'%s'", address_str);
673 		} else {
674 			if (arg == ARG_dev) {
675 				if (dev_str)
676 					duparg(*argv, "dev");
677 				NEXT_ARG();
678 			}
679 			dev_str = *argv;
680 			dbg("dev_str:'%s'", dev_str);
681 		}
682 		argv++;
683 	}
684 	xrtnl_open(&rth);
685 	ll_init_map(&rth);
686 	if (type_str) {
687 		struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
688 
689 		addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
690 		addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
691 				strlen(type_str));
692 
693 		if (*argv) {
694 			struct rtattr *data = NLMSG_TAIL(&req.n);
695 			addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
696 
697 			if (strcmp(type_str, "vlan") == 0)
698 				vlan_parse_opt(argv, &req.n, sizeof(req));
699 			else if (strcmp(type_str, "vrf") == 0)
700 				vrf_parse_opt(argv, &req.n, sizeof(req));
701 
702 			data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
703 		}
704 
705 		linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
706 	}
707 	/* Allow "ip link add dev" and "ip link add name" */
708 	if (!name_str)
709 		name_str = dev_str;
710 	else if (!dev_str)
711 		dev_str = name_str;
712 	/* else if (!strcmp(name_str, dev_str))
713 		name_str = dev_str; */
714 
715 	if (rtm != RTM_NEWLINK) {
716 		if (!dev_str)
717 			return 1; /* Need a device to delete */
718 		req.i.ifi_index = xll_name_to_index(dev_str);
719 	} else {
720 		if (link_str) {
721 			int idx = xll_name_to_index(link_str);
722 			addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
723 		}
724 		if (address_str) {
725 			unsigned char abuf[32];
726 			int len = ll_addr_a2n(abuf, sizeof(abuf), address_str);
727 			dbg("address len:%d", len);
728 			if (len < 0)
729 				return -1;
730 			addattr_l(&req.n, sizeof(req), IFLA_ADDRESS, abuf, len);
731 		}
732 	}
733 	if (name_str) {
734 		const size_t name_len = strlen(name_str) + 1;
735 		if (name_len < 2 || name_len > IFNAMSIZ)
736 			invarg_1_to_2(name_str, "name");
737 		addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
738 	}
739 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
740 		return 2;
741 	return 0;
742 }
743 
744 /* Return value becomes exitcode. It's okay to not return at all */
do_iplink(char ** argv)745 int FAST_FUNC do_iplink(char **argv)
746 {
747 	static const char keywords[] ALIGN1 =
748 		"add\0""delete\0""set\0""show\0""lst\0""list\0";
749 
750 	xfunc_error_retval = 2; //TODO: move up to "ip"? Is it the common rule for all "ip" tools?
751 	if (*argv) {
752 		int key = index_in_substrings(keywords, *argv);
753 		if (key < 0) /* invalid argument */
754 			invarg_1_to_2(*argv, applet_name);
755 		argv++;
756 		if (key <= 1) /* add/delete */
757 			return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
758 		if (key == 2) /* set */
759 			return do_set(argv);
760 	}
761 	/* show, lst, list */
762 	return ipaddr_list_link(argv);
763 }
764