blob: d3d0338b0af54a19d6cc4b12162efe023ca9bcf4 [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 * iplink.c "ip link".
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
Rob Landleyecae66a2006-06-02 20:53:38 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00008 */
9
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000010//#include <sys/ioctl.h>
11//#include <sys/socket.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000012#include <net/if.h>
13#include <net/if_packet.h>
14#include <netpacket/packet.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000015#include <net/ethernet.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000017#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018#include "rt_names.h"
19#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020
Denis Vlasenko540a2a12007-04-07 01:14:45 +000021/* taken from linux/sockios.h */
Eric Andersenab4e19a2003-01-14 08:54:08 +000022#define SIOCSIFNAME 0x8923 /* set interface name */
23
Denis Vlasenko540a2a12007-04-07 01:14:45 +000024/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000025static int get_ctl_fd(void)
26{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027 int fd;
28
29 fd = socket(PF_INET, SOCK_DGRAM, 0);
30 if (fd >= 0)
31 return fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
33 if (fd >= 0)
34 return fd;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000035 return xsocket(PF_INET6, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000036}
37
Denis Vlasenko540a2a12007-04-07 01:14:45 +000038/* Exits on error */
39static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000040{
41 struct ifreq ifr;
42 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043
Denis Vlasenko360d9662008-12-02 18:18:50 +000044 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000046 xioctl(fd, SIOCGIFFLAGS, &ifr);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000047 if ((ifr.ifr_flags ^ flags) & mask) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000048 ifr.ifr_flags &= ~mask;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000049 ifr.ifr_flags |= mask & flags;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000050 xioctl(fd, SIOCSIFFLAGS, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051 }
52 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053}
54
Denis Vlasenko540a2a12007-04-07 01:14:45 +000055/* Exits on error */
56static void do_changename(char *dev, char *newdev)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000057{
58 struct ifreq ifr;
59 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060
Denis Vlasenko360d9662008-12-02 18:18:50 +000061 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
62 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000064 xioctl(fd, SIOCSIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066}
67
Denis Vlasenko540a2a12007-04-07 01:14:45 +000068/* Exits on error */
69static void set_qlen(char *dev, int qlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070{
71 struct ifreq ifr;
72 int s;
73
74 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +000076 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077 ifr.ifr_qlen = qlen;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000078 xioctl(s, SIOCSIFTXQLEN, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080}
81
Denis Vlasenko540a2a12007-04-07 01:14:45 +000082/* Exits on error */
83static void set_mtu(char *dev, int mtu)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084{
85 struct ifreq ifr;
86 int s;
87
88 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000089 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +000090 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091 ifr.ifr_mtu = mtu;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000092 xioctl(s, SIOCSIFMTU, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094}
95
Denis Vlasenko540a2a12007-04-07 01:14:45 +000096/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097static int get_address(char *dev, int *htype)
98{
99 struct ifreq ifr;
100 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000101 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 int s;
103
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000104 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105
106 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000107 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000108 xioctl(s, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109
110 memset(&me, 0, sizeof(me));
111 me.sll_family = AF_PACKET;
112 me.sll_ifindex = ifr.ifr_ifindex;
113 me.sll_protocol = htons(ETH_P_LOOP);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000114 xbind(s, (struct sockaddr*)&me, sizeof(me));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115
116 alen = sizeof(me);
117 if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000118 bb_perror_msg_and_die("getsockname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119 }
120 close(s);
121 *htype = me.sll_hatype;
122 return me.sll_halen;
123}
124
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000125/* Exits on error */
126static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127{
128 int alen;
129
130 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000131 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000132 ifr->ifr_hwaddr.sa_family = hatype;
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000133
134 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
135 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 if (alen < 0)
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000137 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138 if (alen != halen) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000139 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141}
142
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000143/* Exits on error */
144static void set_address(struct ifreq *ifr, int brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145{
146 int s;
147
148 s = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000149 if (brd)
150 xioctl(s, SIOCSIFHWBROADCAST, ifr);
151 else
152 xioctl(s, SIOCSIFHWADDR, ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000154}
155
156
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000157static void die_must_be_on_off(const char *msg) NORETURN;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000158static void die_must_be_on_off(const char *msg)
159{
160 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
161}
162
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000163/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000164static int do_set(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165{
166 char *dev = NULL;
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000167 uint32_t mask = 0;
168 uint32_t flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000169 int qlen = -1;
170 int mtu = -1;
171 char *newaddr = NULL;
172 char *newbrd = NULL;
173 struct ifreq ifr0, ifr1;
174 char *newname = NULL;
175 int htype, halen;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000176 static const char keywords[] ALIGN1 =
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000177 "up\0""down\0""name\0""mtu\0""multicast\0"
178 "arp\0""address\0""dev\0";
179 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_multicast,
180 ARG_arp, ARG_addr, ARG_dev };
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000181 static const char str_on_off[] ALIGN1 = "on\0""off\0";
182 enum { PARM_on = 0, PARM_off };
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000183 smalluint key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000185 while (*argv) {
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000186 /* substring search ensures that e.g. "addr" and "address"
187 * are both accepted */
188 key = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000189 if (key == ARG_up) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190 mask |= IFF_UP;
191 flags |= IFF_UP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000192 }
193 if (key == ARG_down) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000194 mask |= IFF_UP;
195 flags &= ~IFF_UP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000196 }
197 if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198 NEXT_ARG();
199 newname = *argv;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000200 }
201 if (key == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000202 NEXT_ARG();
203 if (mtu != -1)
204 duparg("mtu", *argv);
205 if (get_integer(&mtu, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000206 invarg(*argv, "mtu");
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000207 }
208 if (key == ARG_multicast) {
209 int param;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000210 NEXT_ARG();
211 mask |= IFF_MULTICAST;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000212 param = index_in_strings(str_on_off, *argv);
213 if (param < 0)
214 die_must_be_on_off("multicast");
215 if (param == PARM_on)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000216 flags |= IFF_MULTICAST;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000217 else
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000218 flags &= ~IFF_MULTICAST;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000219 }
220 if (key == ARG_arp) {
221 int param;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222 NEXT_ARG();
223 mask |= IFF_NOARP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000224 param = index_in_strings(str_on_off, *argv);
225 if (param < 0)
226 die_must_be_on_off("arp");
227 if (param == PARM_on)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 flags &= ~IFF_NOARP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000229 else
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000230 flags |= IFF_NOARP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000231 }
232 if (key == ARG_addr) {
Rob Landley483027f2005-12-15 05:29:48 +0000233 NEXT_ARG();
234 newaddr = *argv;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000235 }
236 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;
243 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000244 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245 }
246
247 if (!dev) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000248 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 }
250
251 if (newaddr || newbrd) {
252 halen = get_address(dev, &htype);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000254 parse_address(dev, htype, halen, newaddr, &ifr0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 }
256 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000257 parse_address(dev, htype, halen, newbrd, &ifr1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000258 }
259 }
260
261 if (newname && strcmp(dev, newname)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000262 do_changename(dev, newname);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000263 dev = newname;
264 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000265 if (qlen != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000266 set_qlen(dev, qlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000267 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000268 if (mtu != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000269 set_mtu(dev, mtu);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 }
271 if (newaddr || newbrd) {
272 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000273 set_address(&ifr1, 1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 }
275 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000276 set_address(&ifr0, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 }
278 }
279 if (mask)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000280 do_chflags(dev, flags, mask);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 return 0;
282}
283
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000284static int ipaddr_list_link(char **argv)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000285{
286 preferred_family = AF_PACKET;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000287 return ipaddr_list_or_flush(argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000288}
289
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000290/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000291int do_iplink(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000293 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000294 "set\0""show\0""lst\0""list\0";
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000295 int key;
296 if (!*argv)
297 return ipaddr_list_link(argv);
298 key = index_in_substrings(keywords, *argv);
299 if (key < 0)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000300 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000301 argv++;
302 if (key == 0) /* set */
303 return do_set(argv);
304 /* show, lst, list */
305 return ipaddr_list_link(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000306}