blob: e1c9c60433622697da9e2807f958d92773e42532 [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 +000024static void on_off(const char *msg) ATTRIBUTE_NORETURN;
25static void on_off(const char *msg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000026{
Denis Vlasenko540a2a12007-04-07 01:14:45 +000027 bb_error_msg_and_die("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000028}
29
Denis Vlasenko540a2a12007-04-07 01:14:45 +000030/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000031static int get_ctl_fd(void)
32{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000033 int fd;
34
35 fd = socket(PF_INET, SOCK_DGRAM, 0);
36 if (fd >= 0)
37 return fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
39 if (fd >= 0)
40 return fd;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000041 return xsocket(PF_INET6, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042}
43
Denis Vlasenko540a2a12007-04-07 01:14:45 +000044/* Exits on error */
45static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046{
47 struct ifreq ifr;
48 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049
Denis Vlasenko229b3d22006-11-27 23:44:57 +000050 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000052 xioctl(fd, SIOCGIFFLAGS, &ifr);
Denis Vlasenko540a2a12007-04-07 01:14:45 +000053 if ((ifr.ifr_flags ^ flags) & mask) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000054 ifr.ifr_flags &= ~mask;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000055 ifr.ifr_flags |= mask & flags;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000056 xioctl(fd, SIOCSIFFLAGS, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000057 }
58 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000059}
60
Denis Vlasenko540a2a12007-04-07 01:14:45 +000061/* Exits on error */
62static void do_changename(char *dev, char *newdev)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063{
64 struct ifreq ifr;
65 int fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066
Denis Vlasenko229b3d22006-11-27 23:44:57 +000067 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
68 strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000069 fd = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000070 xioctl(fd, SIOCSIFNAME, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071 close(fd);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072}
73
Denis Vlasenko540a2a12007-04-07 01:14:45 +000074/* Exits on error */
75static void set_qlen(char *dev, int qlen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076{
77 struct ifreq ifr;
78 int s;
79
80 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +000082 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083 ifr.ifr_qlen = qlen;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000084 xioctl(s, SIOCSIFTXQLEN, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000086}
87
Denis Vlasenko540a2a12007-04-07 01:14:45 +000088/* Exits on error */
89static void set_mtu(char *dev, int mtu)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090{
91 struct ifreq ifr;
92 int s;
93
94 s = get_ctl_fd();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +000096 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Eric Andersenc7bda1c2004-03-15 08:29:22 +000097 ifr.ifr_mtu = mtu;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000098 xioctl(s, SIOCSIFMTU, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100}
101
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000102/* Exits on error */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000103static int get_address(char *dev, int *htype)
104{
105 struct ifreq ifr;
106 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000107 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108 int s;
109
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000110 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111
112 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000113 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000114 xioctl(s, SIOCGIFINDEX, &ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115
116 memset(&me, 0, sizeof(me));
117 me.sll_family = AF_PACKET;
118 me.sll_ifindex = ifr.ifr_ifindex;
119 me.sll_protocol = htons(ETH_P_LOOP);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000120 xbind(s, (struct sockaddr*)&me, sizeof(me));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121
122 alen = sizeof(me);
123 if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000124 bb_perror_msg_and_die("getsockname");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125 }
126 close(s);
127 *htype = me.sll_hatype;
128 return me.sll_halen;
129}
130
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000131/* Exits on error */
132static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133{
134 int alen;
135
136 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000137 strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138 ifr->ifr_hwaddr.sa_family = hatype;
Eric Andersend78aea82006-01-30 18:00:02 +0000139 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140 if (alen < 0)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000141 exit(1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142 if (alen != halen) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000143 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145}
146
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000147/* Exits on error */
148static void set_address(struct ifreq *ifr, int brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149{
150 int s;
151
152 s = get_ctl_fd();
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000153 if (brd)
154 xioctl(s, SIOCSIFHWBROADCAST, ifr);
155 else
156 xioctl(s, SIOCSIFHWADDR, ifr);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157 close(s);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158}
159
160
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000161/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162static int do_set(int argc, char **argv)
163{
164 char *dev = NULL;
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000165 uint32_t mask = 0;
166 uint32_t flags = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167 int qlen = -1;
168 int mtu = -1;
169 char *newaddr = NULL;
170 char *newbrd = NULL;
171 struct ifreq ifr0, ifr1;
172 char *newname = NULL;
173 int htype, halen;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000174 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000175 "up\0""down\0""name\0""mtu\0""multicast\0""arp\0""addr\0""dev\0"
176 "on\0""off\0";
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000177 enum { ARG_up = 1, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp,
178 ARG_addr, ARG_dev, PARM_on, PARM_off };
179 smalluint key;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000180
181 while (argc > 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000182 key = index_in_strings(keywords, *argv) + 1;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000183 if (key == ARG_up) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000184 mask |= IFF_UP;
185 flags |= IFF_UP;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000186 } else if (key == ARG_down) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187 mask |= IFF_UP;
188 flags &= ~IFF_UP;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000189 } else if (key == ARG_name) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190 NEXT_ARG();
191 newname = *argv;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000192 } else if (key == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193 NEXT_ARG();
194 if (mtu != -1)
195 duparg("mtu", *argv);
196 if (get_integer(&mtu, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000197 invarg(*argv, "mtu");
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000198 } else if (key == ARG_multicast) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199 NEXT_ARG();
200 mask |= IFF_MULTICAST;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000201 key = index_in_strings(keywords, *argv) + 1;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000202 if (key == PARM_on) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203 flags |= IFF_MULTICAST;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000204 } else if (key == PARM_off) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000205 flags &= ~IFF_MULTICAST;
206 } else
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000207 on_off("multicast");
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000208 } else if (key == ARG_arp) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209 NEXT_ARG();
210 mask |= IFF_NOARP;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000211 key = index_in_strings(keywords, *argv) + 1;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000212 if (key == PARM_on) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000213 flags &= ~IFF_NOARP;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000214 } else if (key == PARM_off) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 flags |= IFF_NOARP;
216 } else
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000217 on_off("arp");
218 } else if (key == ARG_addr) {
Rob Landley483027f2005-12-15 05:29:48 +0000219 NEXT_ARG();
220 newaddr = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000221 } else {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000222 if (key == ARG_dev) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000223 NEXT_ARG();
224 }
225 if (dev)
226 duparg2("dev", *argv);
227 dev = *argv;
228 }
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000229 argc--; argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000230 }
231
232 if (!dev) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000233 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 }
235
236 if (newaddr || newbrd) {
237 halen = get_address(dev, &htype);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000239 parse_address(dev, htype, halen, newaddr, &ifr0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000240 }
241 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000242 parse_address(dev, htype, halen, newbrd, &ifr1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 }
244 }
245
246 if (newname && strcmp(dev, newname)) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000247 do_changename(dev, newname);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000248 dev = newname;
249 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000250 if (qlen != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000251 set_qlen(dev, qlen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000253 if (mtu != -1) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000254 set_mtu(dev, mtu);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 }
256 if (newaddr || newbrd) {
257 if (newbrd) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000258 set_address(&ifr1, 1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 }
260 if (newaddr) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000261 set_address(&ifr0, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000262 }
263 }
264 if (mask)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000265 do_chflags(dev, flags, mask);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 return 0;
267}
268
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000269static int ipaddr_list_link(int argc, char **argv)
270{
271 preferred_family = AF_PACKET;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000272 return ipaddr_list_or_flush(argc, argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000273}
274
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000275/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276int do_iplink(int argc, char **argv)
277{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000278 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000279 "set\0""show\0""lst\0""list\0";
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000280 smalluint key;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000281 if (argc <= 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000282 return ipaddr_list_link(0, NULL);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000283 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000284 if (key == 0)
285 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
286 argc--; argv++;
287 if (key == 1) /* set */
288 return do_set(argc, argv);
289 else /* show, lst, list */
290 return ipaddr_list_link(argc, argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000291}