blob: df8a354a1274ecbba56baffccecfb472f25159a4 [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 */
Eric Andersenab4e19a2003-01-14 08:54:08 +00009#include <net/if.h>
10#include <net/if_packet.h>
11#include <netpacket/packet.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000012#include <net/ethernet.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000014#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "rt_names.h"
16#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017
Denis Vlasenko540a2a12007-04-07 01:14:45 +000018/* taken from linux/sockios.h */
Eric Andersenab4e19a2003-01-14 08:54:08 +000019#define SIOCSIFNAME 0x8923 /* set interface name */
20
Denis Vlasenko540a2a12007-04-07 01:14:45 +000021/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022static int get_ctl_fd(void)
23{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024 int fd;
25
26 fd = socket(PF_INET, SOCK_DGRAM, 0);
27 if (fd >= 0)
28 return fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
30 if (fd >= 0)
31 return fd;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000032 return xsocket(PF_INET6, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000033}
34
Denis Vlasenko540a2a12007-04-07 01:14:45 +000035/* Exits on error */
36static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000037{
38 struct ifreq ifr;
39 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000040
Denis Vlasenko360d9662008-12-02 18:18:50 +000041 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000043 xioctl(fd, SIOCGIFFLAGS, &ifr);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000044 if ((ifr.ifr_flags ^ flags) & mask) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045 ifr.ifr_flags &= ~mask;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000046 ifr.ifr_flags |= mask & flags;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000047 xioctl(fd, SIOCSIFFLAGS, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000048 }
49 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000050}
51
Denis Vlasenko540a2a12007-04-07 01:14:45 +000052/* Exits on error */
53static void do_changename(char *dev, char *newdev)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000054{
55 struct ifreq ifr;
56 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000057
Denis Vlasenko360d9662008-12-02 18:18:50 +000058 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
59 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000061 xioctl(fd, SIOCSIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063}
64
Denis Vlasenko540a2a12007-04-07 01:14:45 +000065/* Exits on error */
66static void set_qlen(char *dev, int qlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000067{
68 struct ifreq ifr;
69 int s;
70
71 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +000073 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000074 ifr.ifr_qlen = qlen;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000075 xioctl(s, SIOCSIFTXQLEN, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077}
78
Denis Vlasenko540a2a12007-04-07 01:14:45 +000079/* Exits on error */
80static void set_mtu(char *dev, int mtu)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081{
82 struct ifreq ifr;
83 int s;
84
85 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000086 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +000087 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000088 ifr.ifr_mtu = mtu;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000089 xioctl(s, SIOCSIFMTU, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091}
92
Denis Vlasenko540a2a12007-04-07 01:14:45 +000093/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094static int get_address(char *dev, int *htype)
95{
96 struct ifreq ifr;
97 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +000098 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 int s;
100
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000101 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102
103 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000104 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000105 xioctl(s, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106
107 memset(&me, 0, sizeof(me));
108 me.sll_family = AF_PACKET;
109 me.sll_ifindex = ifr.ifr_ifindex;
110 me.sll_protocol = htons(ETH_P_LOOP);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000111 xbind(s, (struct sockaddr*)&me, sizeof(me));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000112 alen = sizeof(me);
Denis Vlasenkoa771e7c2009-04-21 23:48:38 +0000113 getsockname(s, (struct sockaddr*)&me, &alen);
114 //never happens:
115 //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
116 // bb_perror_msg_and_die("getsockname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117 close(s);
118 *htype = me.sll_hatype;
119 return me.sll_halen;
120}
121
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000122/* Exits on error */
123static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124{
125 int alen;
126
127 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko360d9662008-12-02 18:18:50 +0000128 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000129 ifr->ifr_hwaddr.sa_family = hatype;
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000130
131 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
132 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 if (alen < 0)
Bernhard Reutner-Fischer51a06c02008-05-16 17:19:03 +0000134 exit(EXIT_FAILURE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 if (alen != halen) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000136 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138}
139
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000140/* Exits on error */
141static void set_address(struct ifreq *ifr, int brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142{
143 int s;
144
145 s = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000146 if (brd)
147 xioctl(s, SIOCSIFHWBROADCAST, ifr);
148 else
149 xioctl(s, SIOCSIFHWADDR, ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151}
152
153
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000154static void die_must_be_on_off(const char *msg) NORETURN;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000155static void die_must_be_on_off(const char *msg)
156{
157 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
158}
159
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000160/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000161static int do_set(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162{
163 char *dev = NULL;
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000164 uint32_t mask = 0;
165 uint32_t flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000166 int qlen = -1;
167 int mtu = -1;
168 char *newaddr = NULL;
169 char *newbrd = NULL;
170 struct ifreq ifr0, ifr1;
171 char *newname = NULL;
172 int htype, halen;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000173 static const char keywords[] ALIGN1 =
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000174 "up\0""down\0""name\0""mtu\0""multicast\0"
175 "arp\0""address\0""dev\0";
176 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_multicast,
177 ARG_arp, ARG_addr, ARG_dev };
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000178 static const char str_on_off[] ALIGN1 = "on\0""off\0";
179 enum { PARM_on = 0, PARM_off };
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000180 smalluint key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000181
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000182 while (*argv) {
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000183 /* substring search ensures that e.g. "addr" and "address"
184 * are both accepted */
185 key = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000186 if (key == ARG_up) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187 mask |= IFF_UP;
188 flags |= IFF_UP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000189 }
190 if (key == ARG_down) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191 mask |= IFF_UP;
192 flags &= ~IFF_UP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000193 }
194 if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000195 NEXT_ARG();
196 newname = *argv;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000197 }
198 if (key == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199 NEXT_ARG();
200 if (mtu != -1)
201 duparg("mtu", *argv);
Denis Vlasenko76140a72009-03-05 09:21:57 +0000202 mtu = get_unsigned(*argv, "mtu");
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000203 }
204 if (key == ARG_multicast) {
205 int param;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000206 NEXT_ARG();
207 mask |= IFF_MULTICAST;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000208 param = index_in_strings(str_on_off, *argv);
209 if (param < 0)
210 die_must_be_on_off("multicast");
211 if (param == PARM_on)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000212 flags |= IFF_MULTICAST;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000213 else
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000214 flags &= ~IFF_MULTICAST;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000215 }
216 if (key == ARG_arp) {
217 int param;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000218 NEXT_ARG();
219 mask |= IFF_NOARP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000220 param = index_in_strings(str_on_off, *argv);
221 if (param < 0)
222 die_must_be_on_off("arp");
223 if (param == PARM_on)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 flags &= ~IFF_NOARP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000225 else
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226 flags |= IFF_NOARP;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000227 }
228 if (key == ARG_addr) {
Rob Landley483027f2005-12-15 05:29:48 +0000229 NEXT_ARG();
230 newaddr = *argv;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000231 }
232 if (key >= ARG_dev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000233 if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 NEXT_ARG();
235 }
236 if (dev)
237 duparg2("dev", *argv);
238 dev = *argv;
239 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000240 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 }
242
243 if (!dev) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000244 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000245 }
246
247 if (newaddr || newbrd) {
248 halen = get_address(dev, &htype);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000250 parse_address(dev, htype, halen, newaddr, &ifr0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000251 }
252 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000253 parse_address(dev, htype, halen, newbrd, &ifr1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000254 }
255 }
256
257 if (newname && strcmp(dev, newname)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000258 do_changename(dev, newname);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 dev = newname;
260 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000261 if (qlen != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000262 set_qlen(dev, qlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000263 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000264 if (mtu != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000265 set_mtu(dev, mtu);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 }
267 if (newaddr || newbrd) {
268 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000269 set_address(&ifr1, 1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 }
271 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000272 set_address(&ifr0, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000273 }
274 }
275 if (mask)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000276 do_chflags(dev, flags, mask);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 return 0;
278}
279
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000280static int ipaddr_list_link(char **argv)
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000281{
282 preferred_family = AF_PACKET;
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000283 return ipaddr_list_or_flush(argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000284}
285
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000286/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000287int do_iplink(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000289 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000290 "set\0""show\0""lst\0""list\0";
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000291 int key;
292 if (!*argv)
293 return ipaddr_list_link(argv);
294 key = index_in_substrings(keywords, *argv);
295 if (key < 0)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000296 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000297 argv++;
298 if (key == 0) /* set */
299 return do_set(argv);
300 /* show, lst, list */
301 return ipaddr_list_link(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000302}