blob: ae3ef0ceb2cc7f630da1093c385975027086f9c4 [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
Denis Vlasenko540a2a12007-04-07 01:14:45 +000055/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056static int get_ctl_fd(void)
57{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058 int fd;
59
60 fd = socket(PF_INET, SOCK_DGRAM, 0);
61 if (fd >= 0)
62 return fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
64 if (fd >= 0)
65 return fd;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000066 return xsocket(PF_INET6, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000067}
68
Denis Vlasenko540a2a12007-04-07 01:14:45 +000069/* Exits on error */
70static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071{
72 struct ifreq ifr;
73 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000074
Denis Vlasenko360d9662008-12-02 18:18:50 +000075 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000077 xioctl(fd, SIOCGIFFLAGS, &ifr);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000078 if ((ifr.ifr_flags ^ flags) & mask) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 ifr.ifr_flags &= ~mask;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000080 ifr.ifr_flags |= mask & flags;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000081 xioctl(fd, SIOCSIFFLAGS, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082 }
83 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084}
85
Denis Vlasenko540a2a12007-04-07 01:14:45 +000086/* Exits on error */
87static void do_changename(char *dev, char *newdev)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000088{
89 struct ifreq ifr;
90 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091
Denis Vlasenko360d9662008-12-02 18:18:50 +000092 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
93 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000095 xioctl(fd, SIOCSIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000096 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097}
98
Denis Vlasenko540a2a12007-04-07 01:14:45 +000099/* Exits on error */
100static void set_qlen(char *dev, int qlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101{
102 struct ifreq ifr;
103 int s;
104
105 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000107 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000108 ifr.ifr_qlen = qlen;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000109 xioctl(s, SIOCSIFTXQLEN, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111}
112
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000113/* Exits on error */
114static void set_mtu(char *dev, int mtu)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115{
116 struct ifreq ifr;
117 int s;
118
119 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000121 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000122 ifr.ifr_mtu = mtu;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000123 xioctl(s, SIOCSIFMTU, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125}
126
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000127/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128static int get_address(char *dev, int *htype)
129{
130 struct ifreq ifr;
131 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000132 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 int s;
134
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000135 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136
137 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000138 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000139 xioctl(s, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140
141 memset(&me, 0, sizeof(me));
142 me.sll_family = AF_PACKET;
143 me.sll_ifindex = ifr.ifr_ifindex;
144 me.sll_protocol = htons(ETH_P_LOOP);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000145 xbind(s, (struct sockaddr*)&me, sizeof(me));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146 alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000147 getsockname(s, (struct sockaddr*)&me, &alen);
148 //never happens:
149 //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
150 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151 close(s);
152 *htype = me.sll_hatype;
153 return me.sll_halen;
154}
155
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000156/* Exits on error */
157static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158{
159 int alen;
160
161 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000162 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 ifr->ifr_hwaddr.sa_family = hatype;
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000164
165 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
166 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167 if (alen < 0)
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000168 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169 if (alen != halen) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000170 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000171 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000172}
173
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000174/* Exits on error */
175static void set_address(struct ifreq *ifr, int brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000176{
177 int s;
178
179 s = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000180 if (brd)
181 xioctl(s, SIOCSIFHWBROADCAST, ifr);
182 else
183 xioctl(s, SIOCSIFHWADDR, ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000185}
186
187
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000188static void die_must_be_on_off(const char *msg) NORETURN;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000189static void die_must_be_on_off(const char *msg)
190{
191 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
192}
193
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000194/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000195static int do_set(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000196{
197 char *dev = NULL;
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000198 uint32_t mask = 0;
199 uint32_t flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200 int qlen = -1;
201 int mtu = -1;
202 char *newaddr = NULL;
203 char *newbrd = NULL;
204 struct ifreq ifr0, ifr1;
205 char *newname = NULL;
206 int htype, halen;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000207 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200208 "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000209 "arp\0""address\0""dev\0";
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200210 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000211 ARG_arp, ARG_addr, ARG_dev };
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000212 static const char str_on_off[] ALIGN1 = "on\0""off\0";
213 enum { PARM_on = 0, PARM_off };
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000214 smalluint key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000216 while (*argv) {
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000217 /* substring search ensures that e.g. "addr" and "address"
218 * are both accepted */
219 key = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000220 if (key == ARG_up) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221 mask |= IFF_UP;
222 flags |= IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200223 } else if (key == ARG_down) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 mask |= IFF_UP;
225 flags &= ~IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200226 } else if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000227 NEXT_ARG();
228 newname = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200229 } else if (key == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000230 NEXT_ARG();
231 if (mtu != -1)
232 duparg("mtu", *argv);
Denis Vlasenko76140a72009-03-05 09:21:57 +0000233 mtu = get_unsigned(*argv, "mtu");
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200234 } else if (key == ARG_qlen) {
235 NEXT_ARG();
236 if (qlen != -1)
237 duparg("qlen", *argv);
238 qlen = get_unsigned(*argv, "qlen");
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200239 } else if (key == ARG_addr) {
Rob Landley483027f2005-12-15 05:29:48 +0000240 NEXT_ARG();
241 newaddr = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200242 } else if (key >= ARG_dev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000243 if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000244 NEXT_ARG();
245 }
246 if (dev)
247 duparg2("dev", *argv);
248 dev = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200249 } else {
250 int param;
251 NEXT_ARG();
252 param = index_in_strings(str_on_off, *argv);
253 if (key == ARG_multicast) {
254 if (param < 0)
255 die_must_be_on_off("multicast");
256 mask |= IFF_MULTICAST;
257 if (param == PARM_on)
258 flags |= IFF_MULTICAST;
259 else
260 flags &= ~IFF_MULTICAST;
261 } else if (key == ARG_arp) {
262 if (param < 0)
263 die_must_be_on_off("arp");
264 mask |= IFF_NOARP;
265 if (param == PARM_on)
266 flags &= ~IFF_NOARP;
267 else
268 flags |= IFF_NOARP;
269 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000271 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 }
273
274 if (!dev) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000275 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 }
277
278 if (newaddr || newbrd) {
279 halen = get_address(dev, &htype);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000281 parse_address(dev, htype, halen, newaddr, &ifr0);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200282 set_address(&ifr0, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 }
284 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000285 parse_address(dev, htype, halen, newbrd, &ifr1);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200286 set_address(&ifr1, 1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000287 }
288 }
289
290 if (newname && strcmp(dev, newname)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000291 do_changename(dev, newname);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 dev = newname;
293 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000294 if (qlen != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000295 set_qlen(dev, qlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000297 if (mtu != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000298 set_mtu(dev, mtu);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000299 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000300 if (mask)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000301 do_chflags(dev, flags, mask);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000302 return 0;
303}
304
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000305static int ipaddr_list_link(char **argv)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000306{
307 preferred_family = AF_PACKET;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000308 return ipaddr_list_or_flush(argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000309}
310
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200311static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
312{
313 static const char keywords[] ALIGN1 =
314 "id\0"
315 "protocol\0"
316 "reorder_hdr\0"
317 "gvrp\0"
318 "mvrp\0"
319 "loose_binding\0"
320 ;
321 static const char protocols[] ALIGN1 =
322 "802.1q\0"
323 "802.1ad\0"
324 ;
325 static const char str_on_off[] ALIGN1 =
326 "on\0"
327 "off\0"
328 ;
329 enum {
330 ARG_id = 0,
331 ARG_reorder_hdr,
332 ARG_gvrp,
333 ARG_mvrp,
334 ARG_loose_binding,
335 ARG_protocol,
336 };
337 enum {
338 PROTO_8021Q = 0,
339 PROTO_8021AD,
340 };
341 enum {
342 PARM_on = 0,
343 PARM_off
344 };
345 int arg;
346 uint16_t id, proto;
347 struct ifla_vlan_flags flags = {};
348
349 while (*argv) {
350 arg = index_in_substrings(keywords, *argv);
351 if (arg < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200352 invarg_1_to_2(*argv, "type vlan");
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200353
354 NEXT_ARG();
355 if (arg == ARG_id) {
356 id = get_u16(*argv, "id");
357 addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
358 } else if (arg == ARG_protocol) {
359 arg = index_in_substrings(protocols, *argv);
360 if (arg == PROTO_8021Q)
361 proto = ETH_P_8021Q;
362 else if (arg == PROTO_8021AD)
363 proto = ETH_P_8021AD;
364 else
365 bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
366 *argv);
367 addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
368 } else {
369 int param = index_in_strings(str_on_off, *argv);
370 if (param < 0)
371 die_must_be_on_off(nth_string(keywords, arg));
372
373 if (arg == ARG_reorder_hdr) {
374 flags.mask |= VLAN_FLAG_REORDER_HDR;
375 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
376 if (param == PARM_on)
377 flags.flags |= VLAN_FLAG_REORDER_HDR;
378 } else if (arg == ARG_gvrp) {
379 flags.mask |= VLAN_FLAG_GVRP;
380 flags.flags &= ~VLAN_FLAG_GVRP;
381 if (param == PARM_on)
382 flags.flags |= VLAN_FLAG_GVRP;
383 } else if (arg == ARG_mvrp) {
384 flags.mask |= VLAN_FLAG_MVRP;
385 flags.flags &= ~VLAN_FLAG_MVRP;
386 if (param == PARM_on)
387 flags.flags |= VLAN_FLAG_MVRP;
388 } else { /*if (arg == ARG_loose_binding) */
389 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
390 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
391 if (param == PARM_on)
392 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
393 }
394 }
395 argv++;
396 }
397
398 if (flags.mask)
399 addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
400}
401
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200402#ifndef NLMSG_TAIL
403#define NLMSG_TAIL(nmsg) \
404 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
405#endif
406/* Return value becomes exitcode. It's okay to not return at all */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200407static int do_add_or_delete(char **argv, const unsigned rtm)
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200408{
409 static const char keywords[] ALIGN1 =
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100410 "link\0""name\0""type\0""dev\0""address\0";
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200411 enum {
412 ARG_link,
413 ARG_name,
414 ARG_type,
415 ARG_dev,
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100416 ARG_address,
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200417 };
418 struct rtnl_handle rth;
419 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200420 struct nlmsghdr n;
421 struct ifinfomsg i;
422 char buf[1024];
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200423 } req;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200424 smalluint arg;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100425 char *name_str = NULL;
426 char *link_str = NULL;
427 char *type_str = NULL;
428 char *dev_str = NULL;
429 char *address_str = NULL;
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200430
431 memset(&req, 0, sizeof(req));
432
433 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
434 req.n.nlmsg_flags = NLM_F_REQUEST;
435 req.n.nlmsg_type = rtm;
436 req.i.ifi_family = preferred_family;
437 if (rtm == RTM_NEWLINK)
438 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
439
440 while (*argv) {
441 arg = index_in_substrings(keywords, *argv);
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200442 if (arg == ARG_type) {
443 NEXT_ARG();
444 type_str = *argv++;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100445 dbg("type_str:'%s'", type_str);
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200446 break;
447 }
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200448 if (arg == ARG_link) {
449 NEXT_ARG();
450 link_str = *argv;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100451 dbg("link_str:'%s'", link_str);
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200452 } else if (arg == ARG_name) {
453 NEXT_ARG();
454 name_str = *argv;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100455 dbg("name_str:'%s'", name_str);
456 } else if (arg == ARG_address) {
457 NEXT_ARG();
458 address_str = *argv;
459 dbg("address_str:'%s'", name_str);
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200460 } else {
461 if (arg == ARG_dev) {
462 if (dev_str)
463 duparg(*argv, "dev");
464 NEXT_ARG();
465 }
466 dev_str = *argv;
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100467 dbg("dev_str:'%s'", dev_str);
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200468 }
469 argv++;
470 }
471 xrtnl_open(&rth);
472 ll_init_map(&rth);
473 if (type_str) {
474 struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
475
476 addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
477 addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
478 strlen(type_str));
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200479
480 if (*argv) {
481 struct rtattr *data = NLMSG_TAIL(&req.n);
482 addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
483
484 if (strcmp(type_str, "vlan") == 0)
485 vlan_parse_opt(argv, &req.n, sizeof(req));
486
487 data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
488 }
489
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200490 linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
491 }
492 if (rtm != RTM_NEWLINK) {
493 if (!dev_str)
494 return 1; /* Need a device to delete */
495 req.i.ifi_index = xll_name_to_index(dev_str);
496 } else {
497 if (!name_str)
498 name_str = dev_str;
499 if (link_str) {
500 int idx = xll_name_to_index(link_str);
501 addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
502 }
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100503 if (address_str) {
504 unsigned char abuf[32];
505 int len = ll_addr_a2n(abuf, sizeof(abuf), address_str);
506 dbg("address len:%d", len);
507 if (len < 0)
508 return -1;
509 addattr_l(&req.n, sizeof(req), IFLA_ADDRESS, abuf, len);
510 }
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200511 }
512 if (name_str) {
513 const size_t name_len = strlen(name_str) + 1;
514 if (name_len < 2 || name_len > IFNAMSIZ)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200515 invarg_1_to_2(name_str, "name");
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200516 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
517 }
518 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
519 return 2;
520 return 0;
521}
522
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100523/* Other keywords recognized by iproute2-3.12.0: */
524#if 0
525 } else if (matches(*argv, "broadcast") == 0 ||
526 strcmp(*argv, "brd") == 0) {
527 NEXT_ARG();
528 len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
529 if (len < 0)
530 return -1;
531 addattr_l(&req->n, sizeof(*req), IFLA_BROADCAST, abuf, len);
532 } else if (matches(*argv, "txqueuelen") == 0 ||
533 strcmp(*argv, "qlen") == 0 ||
534 matches(*argv, "txqlen") == 0) {
535 NEXT_ARG();
536 if (qlen != -1)
537 duparg("txqueuelen", *argv);
538 if (get_integer(&qlen, *argv, 0))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200539 invarg_1_to_2(*argv, "txqueuelen");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100540 addattr_l(&req->n, sizeof(*req), IFLA_TXQLEN, &qlen, 4);
541 } else if (strcmp(*argv, "mtu") == 0) {
542 NEXT_ARG();
543 if (mtu != -1)
544 duparg("mtu", *argv);
545 if (get_integer(&mtu, *argv, 0))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200546 invarg_1_to_2(*argv, "mtu");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100547 addattr_l(&req->n, sizeof(*req), IFLA_MTU, &mtu, 4);
548 } else if (strcmp(*argv, "netns") == 0) {
549 NEXT_ARG();
550 if (netns != -1)
551 duparg("netns", *argv);
552 if ((netns = get_netns_fd(*argv)) >= 0)
553 addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, &netns, 4);
554 else if (get_integer(&netns, *argv, 0) == 0)
555 addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_PID, &netns, 4);
556 else
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200557 invarg_1_to_2(*argv, "netns");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100558 } else if (strcmp(*argv, "multicast") == 0) {
559 NEXT_ARG();
560 req->i.ifi_change |= IFF_MULTICAST;
561 if (strcmp(*argv, "on") == 0) {
562 req->i.ifi_flags |= IFF_MULTICAST;
563 } else if (strcmp(*argv, "off") == 0) {
564 req->i.ifi_flags &= ~IFF_MULTICAST;
565 } else
566 return on_off("multicast", *argv);
567 } else if (strcmp(*argv, "allmulticast") == 0) {
568 NEXT_ARG();
569 req->i.ifi_change |= IFF_ALLMULTI;
570 if (strcmp(*argv, "on") == 0) {
571 req->i.ifi_flags |= IFF_ALLMULTI;
572 } else if (strcmp(*argv, "off") == 0) {
573 req->i.ifi_flags &= ~IFF_ALLMULTI;
574 } else
575 return on_off("allmulticast", *argv);
576 } else if (strcmp(*argv, "promisc") == 0) {
577 NEXT_ARG();
578 req->i.ifi_change |= IFF_PROMISC;
579 if (strcmp(*argv, "on") == 0) {
580 req->i.ifi_flags |= IFF_PROMISC;
581 } else if (strcmp(*argv, "off") == 0) {
582 req->i.ifi_flags &= ~IFF_PROMISC;
583 } else
584 return on_off("promisc", *argv);
585 } else if (strcmp(*argv, "trailers") == 0) {
586 NEXT_ARG();
587 req->i.ifi_change |= IFF_NOTRAILERS;
588 if (strcmp(*argv, "off") == 0) {
589 req->i.ifi_flags |= IFF_NOTRAILERS;
590 } else if (strcmp(*argv, "on") == 0) {
591 req->i.ifi_flags &= ~IFF_NOTRAILERS;
592 } else
593 return on_off("trailers", *argv);
594 } else if (strcmp(*argv, "arp") == 0) {
595 NEXT_ARG();
596 req->i.ifi_change |= IFF_NOARP;
597 if (strcmp(*argv, "on") == 0) {
598 req->i.ifi_flags &= ~IFF_NOARP;
599 } else if (strcmp(*argv, "off") == 0) {
600 req->i.ifi_flags |= IFF_NOARP;
601 } else
602 return on_off("noarp", *argv);
603 } else if (strcmp(*argv, "vf") == 0) {
604 struct rtattr *vflist;
605 NEXT_ARG();
606 if (get_integer(&vf, *argv, 0)) {
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200607 invarg_1_to_2(*argv, "vf");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100608 }
609 vflist = addattr_nest(&req->n, sizeof(*req),
610 IFLA_VFINFO_LIST);
611 len = iplink_parse_vf(vf, &argc, &argv, req);
612 if (len < 0)
613 return -1;
614 addattr_nest_end(&req->n, vflist);
615 } else if (matches(*argv, "master") == 0) {
616 int ifindex;
617 NEXT_ARG();
618 ifindex = ll_name_to_index(*argv);
619 if (!ifindex)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200620 invarg_1_to_2(*argv, "master");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100621 addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
622 &ifindex, 4);
623 } else if (matches(*argv, "nomaster") == 0) {
624 int ifindex = 0;
625 addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
626 &ifindex, 4);
627 } else if (matches(*argv, "dynamic") == 0) {
628 NEXT_ARG();
629 req->i.ifi_change |= IFF_DYNAMIC;
630 if (strcmp(*argv, "on") == 0) {
631 req->i.ifi_flags |= IFF_DYNAMIC;
632 } else if (strcmp(*argv, "off") == 0) {
633 req->i.ifi_flags &= ~IFF_DYNAMIC;
634 } else
635 return on_off("dynamic", *argv);
636 } else if (matches(*argv, "alias") == 0) {
637 NEXT_ARG();
638 addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS,
639 *argv, strlen(*argv));
640 argc--; argv++;
641 break;
642 } else if (strcmp(*argv, "group") == 0) {
643 NEXT_ARG();
644 if (*group != -1)
645 duparg("group", *argv);
646 if (rtnl_group_a2n(group, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200647 invarg_1_to_2(*argv, "group");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100648 } else if (strcmp(*argv, "mode") == 0) {
649 int mode;
650 NEXT_ARG();
651 mode = get_link_mode(*argv);
652 if (mode < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200653 invarg_1_to_2(*argv, "mode");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100654 addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode);
655 } else if (strcmp(*argv, "state") == 0) {
656 int state;
657 NEXT_ARG();
658 state = get_operstate(*argv);
659 if (state < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200660 invarg_1_to_2(*argv, "state");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100661 addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state);
662 } else if (matches(*argv, "numtxqueues") == 0) {
663 NEXT_ARG();
664 if (numtxqueues != -1)
665 duparg("numtxqueues", *argv);
666 if (get_integer(&numtxqueues, *argv, 0))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200667 invarg_1_to_2(*argv, "numtxqueues");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100668 addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES,
669 &numtxqueues, 4);
670 } else if (matches(*argv, "numrxqueues") == 0) {
671 NEXT_ARG();
672 if (numrxqueues != -1)
673 duparg("numrxqueues", *argv);
674 if (get_integer(&numrxqueues, *argv, 0))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200675 invarg_1_to_2(*argv, "numrxqueues");
Denys Vlasenkoecd8c3a2014-03-01 17:38:41 +0100676 addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES,
677 &numrxqueues, 4);
678 }
679#endif
680
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000681/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200682int FAST_FUNC do_iplink(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000683{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000684 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200685 "add\0""delete\0""set\0""show\0""lst\0""list\0";
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200686 if (*argv) {
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200687 int key = index_in_substrings(keywords, *argv);
688 if (key < 0) /* invalid argument */
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200689 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200690 argv++;
691 if (key <= 1) /* add/delete */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200692 return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
693 if (key == 2) /* set */
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200694 return do_set(argv);
695 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000696 /* show, lst, list */
697 return ipaddr_list_link(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000698}