blob: f00c402159d9ed6be2ff5ba6269af81ad947961d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00003 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Patrick McHardya7ecbed2013-07-15 05:16:13 +02004 * Patrick McHardy <kaber@trash.net>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 */
Eric Andersenab4e19a2003-01-14 08:54:08 +00008#include <net/if.h>
Denys Vlasenkoec2bef12013-08-16 12:20:25 +02009/*#include <net/if_packet.h> - not needed? */
Eric Andersenab4e19a2003-01-14 08:54:08 +000010#include <netpacket/packet.h>
Denys Vlasenkob307eab2011-06-12 17:15:16 +020011#include <netinet/if_ether.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012
Patrick McHardya7ecbed2013-07-15 05:16:13 +020013#include <linux/if_vlan.h>
Denys Vlasenko2ead5312013-07-28 23:24:55 +020014#include "ip_common.h" /* #include "libbb.h" is inside */
15#include "rt_names.h"
16#include "utils.h"
17
Patrick McHardya7ecbed2013-07-15 05:16:13 +020018#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
Denys Vlasenkob276e412010-08-22 10:02:55 +020031#ifndef IFLA_LINKINFO
32# define IFLA_LINKINFO 18
Denys Vlasenkoc71ec702010-08-30 19:20:09 +020033# define IFLA_INFO_KIND 1
Eugene Rudoy731f5962014-01-22 11:49:46 +010034# 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
40struct ifla_vlan_flags {
41 uint32_t flags;
42 uint32_t mask;
43};
Denys Vlasenkob276e412010-08-22 10:02:55 +020044#endif
Denys Vlasenkoc71ec702010-08-30 19:20:09 +020045
Denis Vlasenko540a2a12007-04-07 01:14:45 +000046/* taken from linux/sockios.h */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020047#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000048
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +010049#if 0
50# define dbg(...) bb_error_msg(__VA_ARGS__)
51#else
52# define dbg(...) ((void)0)
53#endif
54
Denys Vlasenkoa7386bb2017-04-06 15:55:29 +020055
56#define str_on_off "on\0""off\0"
57
Denis Vlasenko540a2a12007-04-07 01:14:45 +000058/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000059static int get_ctl_fd(void)
60{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061 int fd;
62
63 fd = socket(PF_INET, SOCK_DGRAM, 0);
64 if (fd >= 0)
65 return fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
67 if (fd >= 0)
68 return fd;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000069 return xsocket(PF_INET6, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070}
71
Denis Vlasenko540a2a12007-04-07 01:14:45 +000072/* Exits on error */
73static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000074{
75 struct ifreq ifr;
76 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077
Denis Vlasenko360d9662008-12-02 18:18:50 +000078 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000080 xioctl(fd, SIOCGIFFLAGS, &ifr);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000081 if ((ifr.ifr_flags ^ flags) & mask) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082 ifr.ifr_flags &= ~mask;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000083 ifr.ifr_flags |= mask & flags;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000084 xioctl(fd, SIOCSIFFLAGS, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085 }
86 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087}
88
Denis Vlasenko540a2a12007-04-07 01:14:45 +000089/* Exits on error */
90static void do_changename(char *dev, char *newdev)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091{
92 struct ifreq ifr;
93 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094
Denis Vlasenko360d9662008-12-02 18:18:50 +000095 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
96 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000098 xioctl(fd, SIOCSIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100}
101
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000102/* Exits on error */
103static void set_qlen(char *dev, int qlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104{
105 struct ifreq ifr;
106 int s;
107
108 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000110 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000111 ifr.ifr_qlen = qlen;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000112 xioctl(s, SIOCSIFTXQLEN, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114}
115
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000116/* Exits on error */
117static void set_mtu(char *dev, int mtu)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118{
119 struct ifreq ifr;
120 int s;
121
122 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000124 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000125 ifr.ifr_mtu = mtu;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000126 xioctl(s, SIOCSIFMTU, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128}
129
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000130/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000131static int get_address(char *dev, int *htype)
132{
133 struct ifreq ifr;
134 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000135 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 int s;
137
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000138 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000139
140 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000141 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000142 xioctl(s, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143
144 memset(&me, 0, sizeof(me));
145 me.sll_family = AF_PACKET;
146 me.sll_ifindex = ifr.ifr_ifindex;
147 me.sll_protocol = htons(ETH_P_LOOP);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000148 xbind(s, (struct sockaddr*)&me, sizeof(me));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000150 getsockname(s, (struct sockaddr*)&me, &alen);
151 //never happens:
152 //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
153 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000154 close(s);
155 *htype = me.sll_hatype;
156 return me.sll_halen;
157}
158
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000159/* Exits on error */
160static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161{
162 int alen;
163
164 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000165 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166 ifr->ifr_hwaddr.sa_family = hatype;
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000167
168 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
169 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 if (alen < 0)
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000171 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000172 if (alen != halen) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000173 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175}
176
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000177/* Exits on error */
178static void set_address(struct ifreq *ifr, int brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000179{
180 int s;
181
182 s = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000183 if (brd)
184 xioctl(s, SIOCSIFHWBROADCAST, ifr);
185 else
186 xioctl(s, SIOCSIFHWADDR, ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000188}
189
190
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000191static void die_must_be_on_off(const char *msg) NORETURN;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000192static void die_must_be_on_off(const char *msg)
193{
194 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
195}
196
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000197/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000198static int do_set(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199{
200 char *dev = NULL;
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000201 uint32_t mask = 0;
202 uint32_t flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203 int qlen = -1;
204 int mtu = -1;
205 char *newaddr = NULL;
206 char *newbrd = NULL;
207 struct ifreq ifr0, ifr1;
208 char *newname = NULL;
209 int htype, halen;
Denys Vlasenkoa7386bb2017-04-06 15:55:29 +0200210 /* If you add stuff here, update iplink_full_usage */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000211 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200212 "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
Denys Vlasenkoa7386bb2017-04-06 15:55:29 +0200213 "arp\0""address\0"
214 "dev\0" /* must be last */;
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200215 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
Denys Vlasenkoa7386bb2017-04-06 15:55:29 +0200216 ARG_arp, ARG_addr,
217 ARG_dev };
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000218 enum { PARM_on = 0, PARM_off };
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000219 smalluint key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000220
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000221 while (*argv) {
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000222 /* substring search ensures that e.g. "addr" and "address"
223 * are both accepted */
224 key = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000225 if (key == ARG_up) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226 mask |= IFF_UP;
227 flags |= IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200228 } else if (key == ARG_down) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000229 mask |= IFF_UP;
230 flags &= ~IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200231 } else if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000232 NEXT_ARG();
233 newname = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200234 } else if (key == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000235 NEXT_ARG();
236 if (mtu != -1)
237 duparg("mtu", *argv);
Denis Vlasenko76140a72009-03-05 09:21:57 +0000238 mtu = get_unsigned(*argv, "mtu");
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200239 } else if (key == ARG_qlen) {
240 NEXT_ARG();
241 if (qlen != -1)
242 duparg("qlen", *argv);
243 qlen = get_unsigned(*argv, "qlen");
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200244 } else if (key == ARG_addr) {
Rob Landley483027f2005-12-15 05:29:48 +0000245 NEXT_ARG();
246 newaddr = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200247 } else if (key >= ARG_dev) {
Denys Vlasenkoa7386bb2017-04-06 15:55:29 +0200248 /* ^^^^^^ ">=" here results in "dev IFACE" treated as default */
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000249 if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000250 NEXT_ARG();
251 }
252 if (dev)
253 duparg2("dev", *argv);
254 dev = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200255 } else {
Denys Vlasenkoa7386bb2017-04-06 15:55:29 +0200256 /* "on|off" options */
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200257 int param;
258 NEXT_ARG();
259 param = index_in_strings(str_on_off, *argv);
260 if (key == ARG_multicast) {
261 if (param < 0)
262 die_must_be_on_off("multicast");
263 mask |= IFF_MULTICAST;
264 if (param == PARM_on)
265 flags |= IFF_MULTICAST;
266 else
267 flags &= ~IFF_MULTICAST;
268 } else if (key == ARG_arp) {
269 if (param < 0)
270 die_must_be_on_off("arp");
271 mask |= IFF_NOARP;
272 if (param == PARM_on)
273 flags &= ~IFF_NOARP;
274 else
275 flags |= IFF_NOARP;
276 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 }
Denys Vlasenkoa7386bb2017-04-06 15:55:29 +0200278
279/* Other keywords recognized by iproute2-3.12.0: */
280#if 0
281 } else if (matches(*argv, "broadcast") == 0 ||
282 strcmp(*argv, "brd") == 0) {
283 NEXT_ARG();
284 len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
285 if (len < 0)
286 return -1;
287 addattr_l(&req->n, sizeof(*req), IFLA_BROADCAST, abuf, len);
288 } else if (matches(*argv, "txqueuelen") == 0 ||
289 strcmp(*argv, "qlen") == 0 ||
290 matches(*argv, "txqlen") == 0) {
291 NEXT_ARG();
292 if (qlen != -1)
293 duparg("txqueuelen", *argv);
294 if (get_integer(&qlen, *argv, 0))
295 invarg_1_to_2(*argv, "txqueuelen");
296 addattr_l(&req->n, sizeof(*req), IFLA_TXQLEN, &qlen, 4);
297 } else if (strcmp(*argv, "netns") == 0) {
298 NEXT_ARG();
299 if (netns != -1)
300 duparg("netns", *argv);
301 if ((netns = get_netns_fd(*argv)) >= 0)
302 addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, &netns, 4);
303 else if (get_integer(&netns, *argv, 0) == 0)
304 addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_PID, &netns, 4);
305 else
306 invarg_1_to_2(*argv, "netns");
307 } else if (strcmp(*argv, "allmulticast") == 0) {
308 NEXT_ARG();
309 req->i.ifi_change |= IFF_ALLMULTI;
310 if (strcmp(*argv, "on") == 0) {
311 req->i.ifi_flags |= IFF_ALLMULTI;
312 } else if (strcmp(*argv, "off") == 0) {
313 req->i.ifi_flags &= ~IFF_ALLMULTI;
314 } else
315 return on_off("allmulticast", *argv);
316 } else if (strcmp(*argv, "promisc") == 0) {
317 NEXT_ARG();
318 req->i.ifi_change |= IFF_PROMISC;
319 if (strcmp(*argv, "on") == 0) {
320 req->i.ifi_flags |= IFF_PROMISC;
321 } else if (strcmp(*argv, "off") == 0) {
322 req->i.ifi_flags &= ~IFF_PROMISC;
323 } else
324 return on_off("promisc", *argv);
325 } else if (strcmp(*argv, "trailers") == 0) {
326 NEXT_ARG();
327 req->i.ifi_change |= IFF_NOTRAILERS;
328 if (strcmp(*argv, "off") == 0) {
329 req->i.ifi_flags |= IFF_NOTRAILERS;
330 } else if (strcmp(*argv, "on") == 0) {
331 req->i.ifi_flags &= ~IFF_NOTRAILERS;
332 } else
333 return on_off("trailers", *argv);
334 } else if (strcmp(*argv, "vf") == 0) {
335 struct rtattr *vflist;
336 NEXT_ARG();
337 if (get_integer(&vf, *argv, 0)) {
338 invarg_1_to_2(*argv, "vf");
339 }
340 vflist = addattr_nest(&req->n, sizeof(*req),
341 IFLA_VFINFO_LIST);
342 len = iplink_parse_vf(vf, &argc, &argv, req);
343 if (len < 0)
344 return -1;
345 addattr_nest_end(&req->n, vflist);
346 } else if (matches(*argv, "master") == 0) {
347 int ifindex;
348 NEXT_ARG();
349 ifindex = ll_name_to_index(*argv);
350 if (!ifindex)
351 invarg_1_to_2(*argv, "master");
352 addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
353 &ifindex, 4);
354 } else if (matches(*argv, "nomaster") == 0) {
355 int ifindex = 0;
356 addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
357 &ifindex, 4);
358 } else if (matches(*argv, "dynamic") == 0) {
359 NEXT_ARG();
360 req->i.ifi_change |= IFF_DYNAMIC;
361 if (strcmp(*argv, "on") == 0) {
362 req->i.ifi_flags |= IFF_DYNAMIC;
363 } else if (strcmp(*argv, "off") == 0) {
364 req->i.ifi_flags &= ~IFF_DYNAMIC;
365 } else
366 return on_off("dynamic", *argv);
367 } else if (matches(*argv, "alias") == 0) {
368 NEXT_ARG();
369 addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS,
370 *argv, strlen(*argv));
371 argc--; argv++;
372 break;
373 } else if (strcmp(*argv, "group") == 0) {
374 NEXT_ARG();
375 if (*group != -1)
376 duparg("group", *argv);
377 if (rtnl_group_a2n(group, *argv))
378 invarg_1_to_2(*argv, "group");
379 } else if (strcmp(*argv, "mode") == 0) {
380 int mode;
381 NEXT_ARG();
382 mode = get_link_mode(*argv);
383 if (mode < 0)
384 invarg_1_to_2(*argv, "mode");
385 addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode);
386 } else if (strcmp(*argv, "state") == 0) {
387 int state;
388 NEXT_ARG();
389 state = get_operstate(*argv);
390 if (state < 0)
391 invarg_1_to_2(*argv, "state");
392 addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state);
393 } else if (matches(*argv, "numtxqueues") == 0) {
394 NEXT_ARG();
395 if (numtxqueues != -1)
396 duparg("numtxqueues", *argv);
397 if (get_integer(&numtxqueues, *argv, 0))
398 invarg_1_to_2(*argv, "numtxqueues");
399 addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES,
400 &numtxqueues, 4);
401 } else if (matches(*argv, "numrxqueues") == 0) {
402 NEXT_ARG();
403 if (numrxqueues != -1)
404 duparg("numrxqueues", *argv);
405 if (get_integer(&numrxqueues, *argv, 0))
406 invarg_1_to_2(*argv, "numrxqueues");
407 addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES,
408 &numrxqueues, 4);
409 }
410#endif
411
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000412 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413 }
414
415 if (!dev) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000416 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000417 }
418
419 if (newaddr || newbrd) {
420 halen = get_address(dev, &htype);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000421 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000422 parse_address(dev, htype, halen, newaddr, &ifr0);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200423 set_address(&ifr0, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000424 }
425 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000426 parse_address(dev, htype, halen, newbrd, &ifr1);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200427 set_address(&ifr1, 1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000428 }
429 }
430
431 if (newname && strcmp(dev, newname)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000432 do_changename(dev, newname);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000433 dev = newname;
434 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000435 if (qlen != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000436 set_qlen(dev, qlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000437 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000438 if (mtu != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000439 set_mtu(dev, mtu);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000440 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000441 if (mask)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000442 do_chflags(dev, flags, mask);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000443 return 0;
444}
445
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000446static int ipaddr_list_link(char **argv)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000447{
448 preferred_family = AF_PACKET;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000449 return ipaddr_list_or_flush(argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000450}
451
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200452static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
453{
454 static const char keywords[] ALIGN1 =
455 "id\0"
456 "protocol\0"
457 "reorder_hdr\0"
458 "gvrp\0"
459 "mvrp\0"
460 "loose_binding\0"
461 ;
462 static const char protocols[] ALIGN1 =
463 "802.1q\0"
464 "802.1ad\0"
465 ;
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200466 enum {
467 ARG_id = 0,
468 ARG_reorder_hdr,
469 ARG_gvrp,
470 ARG_mvrp,
471 ARG_loose_binding,
472 ARG_protocol,
473 };
474 enum {
475 PROTO_8021Q = 0,
476 PROTO_8021AD,
477 };
478 enum {
479 PARM_on = 0,
480 PARM_off
481 };
482 int arg;
483 uint16_t id, proto;
484 struct ifla_vlan_flags flags = {};
485
486 while (*argv) {
487 arg = index_in_substrings(keywords, *argv);
488 if (arg < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200489 invarg_1_to_2(*argv, "type vlan");
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200490
491 NEXT_ARG();
492 if (arg == ARG_id) {
493 id = get_u16(*argv, "id");
494 addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
495 } else if (arg == ARG_protocol) {
496 arg = index_in_substrings(protocols, *argv);
497 if (arg == PROTO_8021Q)
498 proto = ETH_P_8021Q;
499 else if (arg == PROTO_8021AD)
500 proto = ETH_P_8021AD;
501 else
502 bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
503 *argv);
504 addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
505 } else {
506 int param = index_in_strings(str_on_off, *argv);
507 if (param < 0)
508 die_must_be_on_off(nth_string(keywords, arg));
509
510 if (arg == ARG_reorder_hdr) {
511 flags.mask |= VLAN_FLAG_REORDER_HDR;
512 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
513 if (param == PARM_on)
514 flags.flags |= VLAN_FLAG_REORDER_HDR;
515 } else if (arg == ARG_gvrp) {
516 flags.mask |= VLAN_FLAG_GVRP;
517 flags.flags &= ~VLAN_FLAG_GVRP;
518 if (param == PARM_on)
519 flags.flags |= VLAN_FLAG_GVRP;
520 } else if (arg == ARG_mvrp) {
521 flags.mask |= VLAN_FLAG_MVRP;
522 flags.flags &= ~VLAN_FLAG_MVRP;
523 if (param == PARM_on)
524 flags.flags |= VLAN_FLAG_MVRP;
525 } else { /*if (arg == ARG_loose_binding) */
526 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
527 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
528 if (param == PARM_on)
529 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
530 }
531 }
532 argv++;
533 }
534
535 if (flags.mask)
536 addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
537}
538
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200539#ifndef NLMSG_TAIL
540#define NLMSG_TAIL(nmsg) \
541 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
542#endif
543/* Return value becomes exitcode. It's okay to not return at all */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200544static int do_add_or_delete(char **argv, const unsigned rtm)
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200545{
546 static const char keywords[] ALIGN1 =
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100547 "link\0""name\0""type\0""dev\0""address\0";
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200548 enum {
549 ARG_link,
550 ARG_name,
551 ARG_type,
552 ARG_dev,
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100553 ARG_address,
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200554 };
555 struct rtnl_handle rth;
556 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200557 struct nlmsghdr n;
558 struct ifinfomsg i;
559 char buf[1024];
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200560 } req;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200561 smalluint arg;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100562 char *name_str = NULL;
563 char *link_str = NULL;
564 char *type_str = NULL;
565 char *dev_str = NULL;
566 char *address_str = NULL;
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200567
568 memset(&req, 0, sizeof(req));
569
570 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
571 req.n.nlmsg_flags = NLM_F_REQUEST;
572 req.n.nlmsg_type = rtm;
573 req.i.ifi_family = preferred_family;
574 if (rtm == RTM_NEWLINK)
575 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
576
577 while (*argv) {
578 arg = index_in_substrings(keywords, *argv);
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200579 if (arg == ARG_type) {
580 NEXT_ARG();
581 type_str = *argv++;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100582 dbg("type_str:'%s'", type_str);
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200583 break;
584 }
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200585 if (arg == ARG_link) {
586 NEXT_ARG();
587 link_str = *argv;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100588 dbg("link_str:'%s'", link_str);
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200589 } else if (arg == ARG_name) {
590 NEXT_ARG();
591 name_str = *argv;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100592 dbg("name_str:'%s'", name_str);
593 } else if (arg == ARG_address) {
594 NEXT_ARG();
595 address_str = *argv;
596 dbg("address_str:'%s'", name_str);
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200597 } else {
598 if (arg == ARG_dev) {
599 if (dev_str)
600 duparg(*argv, "dev");
601 NEXT_ARG();
602 }
603 dev_str = *argv;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100604 dbg("dev_str:'%s'", dev_str);
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200605 }
606 argv++;
607 }
608 xrtnl_open(&rth);
609 ll_init_map(&rth);
610 if (type_str) {
611 struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
612
613 addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
614 addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
615 strlen(type_str));
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200616
617 if (*argv) {
618 struct rtattr *data = NLMSG_TAIL(&req.n);
619 addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
620
621 if (strcmp(type_str, "vlan") == 0)
622 vlan_parse_opt(argv, &req.n, sizeof(req));
623
624 data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
625 }
626
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200627 linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
628 }
629 if (rtm != RTM_NEWLINK) {
630 if (!dev_str)
631 return 1; /* Need a device to delete */
632 req.i.ifi_index = xll_name_to_index(dev_str);
633 } else {
634 if (!name_str)
635 name_str = dev_str;
636 if (link_str) {
637 int idx = xll_name_to_index(link_str);
638 addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
639 }
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100640 if (address_str) {
641 unsigned char abuf[32];
642 int len = ll_addr_a2n(abuf, sizeof(abuf), address_str);
643 dbg("address len:%d", len);
644 if (len < 0)
645 return -1;
646 addattr_l(&req.n, sizeof(req), IFLA_ADDRESS, abuf, len);
647 }
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200648 }
649 if (name_str) {
650 const size_t name_len = strlen(name_str) + 1;
651 if (name_len < 2 || name_len > IFNAMSIZ)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200652 invarg_1_to_2(name_str, "name");
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200653 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
654 }
655 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
656 return 2;
657 return 0;
658}
659
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000660/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200661int FAST_FUNC do_iplink(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000662{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000663 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200664 "add\0""delete\0""set\0""show\0""lst\0""list\0";
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200665 if (*argv) {
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200666 int key = index_in_substrings(keywords, *argv);
667 if (key < 0) /* invalid argument */
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200668 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200669 argv++;
670 if (key <= 1) /* add/delete */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200671 return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
672 if (key == 2) /* set */
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200673 return do_set(argv);
674 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000675 /* show, lst, list */
676 return ipaddr_list_link(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000677}