blob: 506fb3c7ef1791c293526a95f1a113aa73255ca7 [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
Denis Vlasenko540a2a12007-04-07 01:14:45 +000049/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000050static int get_ctl_fd(void)
51{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000052 int fd;
53
54 fd = socket(PF_INET, SOCK_DGRAM, 0);
55 if (fd >= 0)
56 return fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000057 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
58 if (fd >= 0)
59 return fd;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000060 return xsocket(PF_INET6, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061}
62
Denis Vlasenko540a2a12007-04-07 01:14:45 +000063/* Exits on error */
64static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065{
66 struct ifreq ifr;
67 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068
Denis Vlasenko360d9662008-12-02 18:18:50 +000069 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000071 xioctl(fd, SIOCGIFFLAGS, &ifr);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000072 if ((ifr.ifr_flags ^ flags) & mask) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000073 ifr.ifr_flags &= ~mask;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000074 ifr.ifr_flags |= mask & flags;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000075 xioctl(fd, SIOCSIFFLAGS, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076 }
77 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078}
79
Denis Vlasenko540a2a12007-04-07 01:14:45 +000080/* Exits on error */
81static void do_changename(char *dev, char *newdev)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082{
83 struct ifreq ifr;
84 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085
Denis Vlasenko360d9662008-12-02 18:18:50 +000086 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
87 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000088 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000089 xioctl(fd, SIOCSIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091}
92
Denis Vlasenko540a2a12007-04-07 01:14:45 +000093/* Exits on error */
94static void set_qlen(char *dev, int qlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095{
96 struct ifreq ifr;
97 int s;
98
99 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000101 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000102 ifr.ifr_qlen = qlen;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000103 xioctl(s, SIOCSIFTXQLEN, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105}
106
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000107/* Exits on error */
108static void set_mtu(char *dev, int mtu)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109{
110 struct ifreq ifr;
111 int s;
112
113 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000115 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000116 ifr.ifr_mtu = mtu;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000117 xioctl(s, SIOCSIFMTU, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119}
120
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000121/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122static int get_address(char *dev, int *htype)
123{
124 struct ifreq ifr;
125 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000126 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127 int s;
128
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000129 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130
131 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000132 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000133 xioctl(s, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000134
135 memset(&me, 0, sizeof(me));
136 me.sll_family = AF_PACKET;
137 me.sll_ifindex = ifr.ifr_ifindex;
138 me.sll_protocol = htons(ETH_P_LOOP);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000139 xbind(s, (struct sockaddr*)&me, sizeof(me));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140 alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000141 getsockname(s, (struct sockaddr*)&me, &alen);
142 //never happens:
143 //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
144 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 close(s);
146 *htype = me.sll_hatype;
147 return me.sll_halen;
148}
149
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000150/* Exits on error */
151static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000152{
153 int alen;
154
155 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000156 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 ifr->ifr_hwaddr.sa_family = hatype;
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000158
159 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
160 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 if (alen < 0)
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000162 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 if (alen != halen) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000164 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166}
167
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000168/* Exits on error */
169static void set_address(struct ifreq *ifr, int brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170{
171 int s;
172
173 s = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000174 if (brd)
175 xioctl(s, SIOCSIFHWBROADCAST, ifr);
176 else
177 xioctl(s, SIOCSIFHWADDR, ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000179}
180
181
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000182static void die_must_be_on_off(const char *msg) NORETURN;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000183static void die_must_be_on_off(const char *msg)
184{
185 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
186}
187
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000188/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000189static int do_set(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190{
191 char *dev = NULL;
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000192 uint32_t mask = 0;
193 uint32_t flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000194 int qlen = -1;
195 int mtu = -1;
196 char *newaddr = NULL;
197 char *newbrd = NULL;
198 struct ifreq ifr0, ifr1;
199 char *newname = NULL;
200 int htype, halen;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000201 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200202 "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000203 "arp\0""address\0""dev\0";
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200204 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000205 ARG_arp, ARG_addr, ARG_dev };
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000206 static const char str_on_off[] ALIGN1 = "on\0""off\0";
207 enum { PARM_on = 0, PARM_off };
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000208 smalluint key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000210 while (*argv) {
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000211 /* substring search ensures that e.g. "addr" and "address"
212 * are both accepted */
213 key = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000214 if (key == ARG_up) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 mask |= IFF_UP;
216 flags |= IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200217 } else if (key == ARG_down) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000218 mask |= IFF_UP;
219 flags &= ~IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200220 } else if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221 NEXT_ARG();
222 newname = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200223 } else if (key == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 NEXT_ARG();
225 if (mtu != -1)
226 duparg("mtu", *argv);
Denis Vlasenko76140a72009-03-05 09:21:57 +0000227 mtu = get_unsigned(*argv, "mtu");
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200228 } else if (key == ARG_qlen) {
229 NEXT_ARG();
230 if (qlen != -1)
231 duparg("qlen", *argv);
232 qlen = get_unsigned(*argv, "qlen");
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200233 } else if (key == ARG_addr) {
Rob Landley483027f2005-12-15 05:29:48 +0000234 NEXT_ARG();
235 newaddr = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200236 } else if (key >= ARG_dev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000237 if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238 NEXT_ARG();
239 }
240 if (dev)
241 duparg2("dev", *argv);
242 dev = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200243 } else {
244 int param;
245 NEXT_ARG();
246 param = index_in_strings(str_on_off, *argv);
247 if (key == ARG_multicast) {
248 if (param < 0)
249 die_must_be_on_off("multicast");
250 mask |= IFF_MULTICAST;
251 if (param == PARM_on)
252 flags |= IFF_MULTICAST;
253 else
254 flags &= ~IFF_MULTICAST;
255 } else if (key == ARG_arp) {
256 if (param < 0)
257 die_must_be_on_off("arp");
258 mask |= IFF_NOARP;
259 if (param == PARM_on)
260 flags &= ~IFF_NOARP;
261 else
262 flags |= IFF_NOARP;
263 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000265 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 }
267
268 if (!dev) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000269 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 }
271
272 if (newaddr || newbrd) {
273 halen = get_address(dev, &htype);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000275 parse_address(dev, htype, halen, newaddr, &ifr0);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200276 set_address(&ifr0, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 }
278 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000279 parse_address(dev, htype, halen, newbrd, &ifr1);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200280 set_address(&ifr1, 1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 }
282 }
283
284 if (newname && strcmp(dev, newname)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000285 do_changename(dev, newname);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000286 dev = newname;
287 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000288 if (qlen != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000289 set_qlen(dev, qlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000291 if (mtu != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000292 set_mtu(dev, mtu);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000293 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000294 if (mask)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000295 do_chflags(dev, flags, mask);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296 return 0;
297}
298
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000299static int ipaddr_list_link(char **argv)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000300{
301 preferred_family = AF_PACKET;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000302 return ipaddr_list_or_flush(argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000303}
304
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200305static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
306{
307 static const char keywords[] ALIGN1 =
308 "id\0"
309 "protocol\0"
310 "reorder_hdr\0"
311 "gvrp\0"
312 "mvrp\0"
313 "loose_binding\0"
314 ;
315 static const char protocols[] ALIGN1 =
316 "802.1q\0"
317 "802.1ad\0"
318 ;
319 static const char str_on_off[] ALIGN1 =
320 "on\0"
321 "off\0"
322 ;
323 enum {
324 ARG_id = 0,
325 ARG_reorder_hdr,
326 ARG_gvrp,
327 ARG_mvrp,
328 ARG_loose_binding,
329 ARG_protocol,
330 };
331 enum {
332 PROTO_8021Q = 0,
333 PROTO_8021AD,
334 };
335 enum {
336 PARM_on = 0,
337 PARM_off
338 };
339 int arg;
340 uint16_t id, proto;
341 struct ifla_vlan_flags flags = {};
342
343 while (*argv) {
344 arg = index_in_substrings(keywords, *argv);
345 if (arg < 0)
346 invarg(*argv, "type vlan");
347
348 NEXT_ARG();
349 if (arg == ARG_id) {
350 id = get_u16(*argv, "id");
351 addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
352 } else if (arg == ARG_protocol) {
353 arg = index_in_substrings(protocols, *argv);
354 if (arg == PROTO_8021Q)
355 proto = ETH_P_8021Q;
356 else if (arg == PROTO_8021AD)
357 proto = ETH_P_8021AD;
358 else
359 bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
360 *argv);
361 addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
362 } else {
363 int param = index_in_strings(str_on_off, *argv);
364 if (param < 0)
365 die_must_be_on_off(nth_string(keywords, arg));
366
367 if (arg == ARG_reorder_hdr) {
368 flags.mask |= VLAN_FLAG_REORDER_HDR;
369 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
370 if (param == PARM_on)
371 flags.flags |= VLAN_FLAG_REORDER_HDR;
372 } else if (arg == ARG_gvrp) {
373 flags.mask |= VLAN_FLAG_GVRP;
374 flags.flags &= ~VLAN_FLAG_GVRP;
375 if (param == PARM_on)
376 flags.flags |= VLAN_FLAG_GVRP;
377 } else if (arg == ARG_mvrp) {
378 flags.mask |= VLAN_FLAG_MVRP;
379 flags.flags &= ~VLAN_FLAG_MVRP;
380 if (param == PARM_on)
381 flags.flags |= VLAN_FLAG_MVRP;
382 } else { /*if (arg == ARG_loose_binding) */
383 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
384 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
385 if (param == PARM_on)
386 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
387 }
388 }
389 argv++;
390 }
391
392 if (flags.mask)
393 addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
394}
395
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200396#ifndef NLMSG_TAIL
397#define NLMSG_TAIL(nmsg) \
398 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
399#endif
400/* Return value becomes exitcode. It's okay to not return at all */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200401static int do_add_or_delete(char **argv, const unsigned rtm)
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200402{
403 static const char keywords[] ALIGN1 =
404 "link\0""name\0""type\0""dev\0";
405 enum {
406 ARG_link,
407 ARG_name,
408 ARG_type,
409 ARG_dev,
410 };
411 struct rtnl_handle rth;
412 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200413 struct nlmsghdr n;
414 struct ifinfomsg i;
415 char buf[1024];
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200416 } req;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200417 smalluint arg;
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200418 char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL;
419
420 memset(&req, 0, sizeof(req));
421
422 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
423 req.n.nlmsg_flags = NLM_F_REQUEST;
424 req.n.nlmsg_type = rtm;
425 req.i.ifi_family = preferred_family;
426 if (rtm == RTM_NEWLINK)
427 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
428
429 while (*argv) {
430 arg = index_in_substrings(keywords, *argv);
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200431 if (arg == ARG_type) {
432 NEXT_ARG();
433 type_str = *argv++;
434 break;
435 }
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200436 if (arg == ARG_link) {
437 NEXT_ARG();
438 link_str = *argv;
439 } else if (arg == ARG_name) {
440 NEXT_ARG();
441 name_str = *argv;
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200442 } else {
443 if (arg == ARG_dev) {
444 if (dev_str)
445 duparg(*argv, "dev");
446 NEXT_ARG();
447 }
448 dev_str = *argv;
449 }
450 argv++;
451 }
452 xrtnl_open(&rth);
453 ll_init_map(&rth);
454 if (type_str) {
455 struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
456
457 addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
458 addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
459 strlen(type_str));
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200460
461 if (*argv) {
462 struct rtattr *data = NLMSG_TAIL(&req.n);
463 addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
464
465 if (strcmp(type_str, "vlan") == 0)
466 vlan_parse_opt(argv, &req.n, sizeof(req));
467
468 data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
469 }
470
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200471 linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
472 }
473 if (rtm != RTM_NEWLINK) {
474 if (!dev_str)
475 return 1; /* Need a device to delete */
476 req.i.ifi_index = xll_name_to_index(dev_str);
477 } else {
478 if (!name_str)
479 name_str = dev_str;
480 if (link_str) {
481 int idx = xll_name_to_index(link_str);
482 addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
483 }
484 }
485 if (name_str) {
486 const size_t name_len = strlen(name_str) + 1;
487 if (name_len < 2 || name_len > IFNAMSIZ)
488 invarg(name_str, "name");
489 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
490 }
491 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
492 return 2;
493 return 0;
494}
495
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000496/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200497int FAST_FUNC do_iplink(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000498{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000499 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200500 "add\0""delete\0""set\0""show\0""lst\0""list\0";
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200501 if (*argv) {
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200502 int key = index_in_substrings(keywords, *argv);
503 if (key < 0) /* invalid argument */
504 invarg(*argv, applet_name);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200505 argv++;
506 if (key <= 1) /* add/delete */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200507 return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
508 if (key == 2) /* set */
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200509 return do_set(argv);
510 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000511 /* show, lst, list */
512 return ipaddr_list_link(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000513}