blob: a686eb201cda5f8cb19850101a984a5dde4c7769 [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>
9#include <net/if_packet.h>
10#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
Denys Vlasenkob276e412010-08-22 10:02:55 +020034#endif
Denys Vlasenkoc71ec702010-08-30 19:20:09 +020035
Denis Vlasenko540a2a12007-04-07 01:14:45 +000036/* taken from linux/sockios.h */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020037#define SIOCSIFNAME 0x8923 /* set interface name */
Eric Andersenab4e19a2003-01-14 08:54:08 +000038
Denis Vlasenko540a2a12007-04-07 01:14:45 +000039/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000040static int get_ctl_fd(void)
41{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042 int fd;
43
44 fd = socket(PF_INET, SOCK_DGRAM, 0);
45 if (fd >= 0)
46 return fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
48 if (fd >= 0)
49 return fd;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000050 return xsocket(PF_INET6, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051}
52
Denis Vlasenko540a2a12007-04-07 01:14:45 +000053/* Exits on error */
54static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000055{
56 struct ifreq ifr;
57 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058
Denis Vlasenko360d9662008-12-02 18:18:50 +000059 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000061 xioctl(fd, SIOCGIFFLAGS, &ifr);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000062 if ((ifr.ifr_flags ^ flags) & mask) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 ifr.ifr_flags &= ~mask;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000064 ifr.ifr_flags |= mask & flags;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000065 xioctl(fd, SIOCSIFFLAGS, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 }
67 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068}
69
Denis Vlasenko540a2a12007-04-07 01:14:45 +000070/* Exits on error */
71static void do_changename(char *dev, char *newdev)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072{
73 struct ifreq ifr;
74 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075
Denis Vlasenko360d9662008-12-02 18:18:50 +000076 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
77 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000079 xioctl(fd, SIOCSIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081}
82
Denis Vlasenko540a2a12007-04-07 01:14:45 +000083/* Exits on error */
84static void set_qlen(char *dev, int qlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085{
86 struct ifreq ifr;
87 int s;
88
89 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +000091 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092 ifr.ifr_qlen = qlen;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000093 xioctl(s, SIOCSIFTXQLEN, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095}
96
Denis Vlasenko540a2a12007-04-07 01:14:45 +000097/* Exits on error */
98static void set_mtu(char *dev, int mtu)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099{
100 struct ifreq ifr;
101 int s;
102
103 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000105 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000106 ifr.ifr_mtu = mtu;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000107 xioctl(s, SIOCSIFMTU, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109}
110
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000111/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000112static int get_address(char *dev, int *htype)
113{
114 struct ifreq ifr;
115 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000116 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117 int s;
118
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000119 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120
121 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000122 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000123 xioctl(s, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124
125 memset(&me, 0, sizeof(me));
126 me.sll_family = AF_PACKET;
127 me.sll_ifindex = ifr.ifr_ifindex;
128 me.sll_protocol = htons(ETH_P_LOOP);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000129 xbind(s, (struct sockaddr*)&me, sizeof(me));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130 alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000131 getsockname(s, (struct sockaddr*)&me, &alen);
132 //never happens:
133 //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
134 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 close(s);
136 *htype = me.sll_hatype;
137 return me.sll_halen;
138}
139
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000140/* Exits on error */
141static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142{
143 int alen;
144
145 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000146 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000147 ifr->ifr_hwaddr.sa_family = hatype;
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000148
149 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
150 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151 if (alen < 0)
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000152 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 if (alen != halen) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000154 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000156}
157
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000158/* Exits on error */
159static void set_address(struct ifreq *ifr, int brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000160{
161 int s;
162
163 s = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000164 if (brd)
165 xioctl(s, SIOCSIFHWBROADCAST, ifr);
166 else
167 xioctl(s, SIOCSIFHWADDR, ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169}
170
171
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000172static void die_must_be_on_off(const char *msg) NORETURN;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000173static void die_must_be_on_off(const char *msg)
174{
175 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
176}
177
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000178/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000179static int do_set(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180{
181 char *dev = NULL;
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000182 uint32_t mask = 0;
183 uint32_t flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 int qlen = -1;
185 int mtu = -1;
186 char *newaddr = NULL;
187 char *newbrd = NULL;
188 struct ifreq ifr0, ifr1;
189 char *newname = NULL;
190 int htype, halen;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000191 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200192 "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000193 "arp\0""address\0""dev\0";
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200194 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000195 ARG_arp, ARG_addr, ARG_dev };
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000196 static const char str_on_off[] ALIGN1 = "on\0""off\0";
197 enum { PARM_on = 0, PARM_off };
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000198 smalluint key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000200 while (*argv) {
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000201 /* substring search ensures that e.g. "addr" and "address"
202 * are both accepted */
203 key = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000204 if (key == ARG_up) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000205 mask |= IFF_UP;
206 flags |= IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200207 } else if (key == ARG_down) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000208 mask |= IFF_UP;
209 flags &= ~IFF_UP;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200210 } else if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000211 NEXT_ARG();
212 newname = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200213 } else if (key == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000214 NEXT_ARG();
215 if (mtu != -1)
216 duparg("mtu", *argv);
Denis Vlasenko76140a72009-03-05 09:21:57 +0000217 mtu = get_unsigned(*argv, "mtu");
Bernhard Reutner-Fischerab0e4122010-05-25 16:57:08 +0200218 } else if (key == ARG_qlen) {
219 NEXT_ARG();
220 if (qlen != -1)
221 duparg("qlen", *argv);
222 qlen = get_unsigned(*argv, "qlen");
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200223 } else if (key == ARG_addr) {
Rob Landley483027f2005-12-15 05:29:48 +0000224 NEXT_ARG();
225 newaddr = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200226 } else if (key >= ARG_dev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000227 if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 NEXT_ARG();
229 }
230 if (dev)
231 duparg2("dev", *argv);
232 dev = *argv;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200233 } else {
234 int param;
235 NEXT_ARG();
236 param = index_in_strings(str_on_off, *argv);
237 if (key == ARG_multicast) {
238 if (param < 0)
239 die_must_be_on_off("multicast");
240 mask |= IFF_MULTICAST;
241 if (param == PARM_on)
242 flags |= IFF_MULTICAST;
243 else
244 flags &= ~IFF_MULTICAST;
245 } else if (key == ARG_arp) {
246 if (param < 0)
247 die_must_be_on_off("arp");
248 mask |= IFF_NOARP;
249 if (param == PARM_on)
250 flags &= ~IFF_NOARP;
251 else
252 flags |= IFF_NOARP;
253 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000254 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000255 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000256 }
257
258 if (!dev) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000259 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000260 }
261
262 if (newaddr || newbrd) {
263 halen = get_address(dev, &htype);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000265 parse_address(dev, htype, halen, newaddr, &ifr0);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200266 set_address(&ifr0, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 }
268 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000269 parse_address(dev, htype, halen, newbrd, &ifr1);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200270 set_address(&ifr1, 1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000271 }
272 }
273
274 if (newname && strcmp(dev, newname)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000275 do_changename(dev, newname);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 dev = newname;
277 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000278 if (qlen != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000279 set_qlen(dev, qlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000280 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000281 if (mtu != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000282 set_mtu(dev, mtu);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000284 if (mask)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000285 do_chflags(dev, flags, mask);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000286 return 0;
287}
288
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000289static int ipaddr_list_link(char **argv)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000290{
291 preferred_family = AF_PACKET;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000292 return ipaddr_list_or_flush(argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000293}
294
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200295static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
296{
297 static const char keywords[] ALIGN1 =
298 "id\0"
299 "protocol\0"
300 "reorder_hdr\0"
301 "gvrp\0"
302 "mvrp\0"
303 "loose_binding\0"
304 ;
305 static const char protocols[] ALIGN1 =
306 "802.1q\0"
307 "802.1ad\0"
308 ;
309 static const char str_on_off[] ALIGN1 =
310 "on\0"
311 "off\0"
312 ;
313 enum {
314 ARG_id = 0,
315 ARG_reorder_hdr,
316 ARG_gvrp,
317 ARG_mvrp,
318 ARG_loose_binding,
319 ARG_protocol,
320 };
321 enum {
322 PROTO_8021Q = 0,
323 PROTO_8021AD,
324 };
325 enum {
326 PARM_on = 0,
327 PARM_off
328 };
329 int arg;
330 uint16_t id, proto;
331 struct ifla_vlan_flags flags = {};
332
333 while (*argv) {
334 arg = index_in_substrings(keywords, *argv);
335 if (arg < 0)
336 invarg(*argv, "type vlan");
337
338 NEXT_ARG();
339 if (arg == ARG_id) {
340 id = get_u16(*argv, "id");
341 addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
342 } else if (arg == ARG_protocol) {
343 arg = index_in_substrings(protocols, *argv);
344 if (arg == PROTO_8021Q)
345 proto = ETH_P_8021Q;
346 else if (arg == PROTO_8021AD)
347 proto = ETH_P_8021AD;
348 else
349 bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
350 *argv);
351 addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
352 } else {
353 int param = index_in_strings(str_on_off, *argv);
354 if (param < 0)
355 die_must_be_on_off(nth_string(keywords, arg));
356
357 if (arg == ARG_reorder_hdr) {
358 flags.mask |= VLAN_FLAG_REORDER_HDR;
359 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
360 if (param == PARM_on)
361 flags.flags |= VLAN_FLAG_REORDER_HDR;
362 } else if (arg == ARG_gvrp) {
363 flags.mask |= VLAN_FLAG_GVRP;
364 flags.flags &= ~VLAN_FLAG_GVRP;
365 if (param == PARM_on)
366 flags.flags |= VLAN_FLAG_GVRP;
367 } else if (arg == ARG_mvrp) {
368 flags.mask |= VLAN_FLAG_MVRP;
369 flags.flags &= ~VLAN_FLAG_MVRP;
370 if (param == PARM_on)
371 flags.flags |= VLAN_FLAG_MVRP;
372 } else { /*if (arg == ARG_loose_binding) */
373 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
374 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
375 if (param == PARM_on)
376 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
377 }
378 }
379 argv++;
380 }
381
382 if (flags.mask)
383 addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
384}
385
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200386#ifndef NLMSG_TAIL
387#define NLMSG_TAIL(nmsg) \
388 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
389#endif
390/* Return value becomes exitcode. It's okay to not return at all */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200391static int do_add_or_delete(char **argv, const unsigned rtm)
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200392{
393 static const char keywords[] ALIGN1 =
394 "link\0""name\0""type\0""dev\0";
395 enum {
396 ARG_link,
397 ARG_name,
398 ARG_type,
399 ARG_dev,
400 };
401 struct rtnl_handle rth;
402 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200403 struct nlmsghdr n;
404 struct ifinfomsg i;
405 char buf[1024];
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200406 } req;
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200407 smalluint arg;
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200408 char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL;
409
410 memset(&req, 0, sizeof(req));
411
412 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
413 req.n.nlmsg_flags = NLM_F_REQUEST;
414 req.n.nlmsg_type = rtm;
415 req.i.ifi_family = preferred_family;
416 if (rtm == RTM_NEWLINK)
417 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
418
419 while (*argv) {
420 arg = index_in_substrings(keywords, *argv);
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200421 if (arg == ARG_type) {
422 NEXT_ARG();
423 type_str = *argv++;
424 break;
425 }
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200426 if (arg == ARG_link) {
427 NEXT_ARG();
428 link_str = *argv;
429 } else if (arg == ARG_name) {
430 NEXT_ARG();
431 name_str = *argv;
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200432 } else {
433 if (arg == ARG_dev) {
434 if (dev_str)
435 duparg(*argv, "dev");
436 NEXT_ARG();
437 }
438 dev_str = *argv;
439 }
440 argv++;
441 }
442 xrtnl_open(&rth);
443 ll_init_map(&rth);
444 if (type_str) {
445 struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
446
447 addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
448 addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
449 strlen(type_str));
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200450
451 if (*argv) {
452 struct rtattr *data = NLMSG_TAIL(&req.n);
453 addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
454
455 if (strcmp(type_str, "vlan") == 0)
456 vlan_parse_opt(argv, &req.n, sizeof(req));
457
458 data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
459 }
460
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200461 linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
462 }
463 if (rtm != RTM_NEWLINK) {
464 if (!dev_str)
465 return 1; /* Need a device to delete */
466 req.i.ifi_index = xll_name_to_index(dev_str);
467 } else {
468 if (!name_str)
469 name_str = dev_str;
470 if (link_str) {
471 int idx = xll_name_to_index(link_str);
472 addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
473 }
474 }
475 if (name_str) {
476 const size_t name_len = strlen(name_str) + 1;
477 if (name_len < 2 || name_len > IFNAMSIZ)
478 invarg(name_str, "name");
479 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
480 }
481 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
482 return 2;
483 return 0;
484}
485
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000486/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200487int FAST_FUNC do_iplink(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000488{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000489 static const char keywords[] ALIGN1 =
Bernhard Reutner-Fischer6faebfa2010-05-25 10:34:27 +0200490 "add\0""delete\0""set\0""show\0""lst\0""list\0";
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200491 if (*argv) {
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200492 int key = index_in_substrings(keywords, *argv);
493 if (key < 0) /* invalid argument */
494 invarg(*argv, applet_name);
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200495 argv++;
496 if (key <= 1) /* add/delete */
Patrick McHardya7ecbed2013-07-15 05:16:13 +0200497 return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
498 if (key == 2) /* set */
Bernhard Reutner-Fischerd148e482010-05-25 16:16:28 +0200499 return do_set(argv);
500 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000501 /* show, lst, list */
502 return ipaddr_list_link(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000503}