blob: 1ea11f60b1db5be3d9498093aa3910a1c4d3095f [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
Rob Landleyecae66a2006-06-02 20:53:38 +000010#include "libbb.h"
11
Glenn L McGrath275be872002-12-16 07:37:21 +000012#include <sys/ioctl.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include <sys/socket.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000014
15#include <errno.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000016#include <string.h>
17#include <unistd.h>
18
Eric Andersenab4e19a2003-01-14 08:54:08 +000019#include <net/if.h>
20#include <net/if_packet.h>
21#include <netpacket/packet.h>
22
Eric Andersenab4e19a2003-01-14 08:54:08 +000023#include <net/ethernet.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024
25#include "rt_names.h"
26#include "utils.h"
27#include "ip_common.h"
28
Eric Andersenab4e19a2003-01-14 08:54:08 +000029/* take from linux/sockios.h */
30#define SIOCSIFNAME 0x8923 /* set interface name */
31
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032static int on_off(char *msg)
33{
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000034 bb_error_msg("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000035 return -1;
36}
37
38static int get_ctl_fd(void)
39{
40 int s_errno;
41 int fd;
42
43 fd = socket(PF_INET, SOCK_DGRAM, 0);
44 if (fd >= 0)
45 return fd;
46 s_errno = errno;
47 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
48 if (fd >= 0)
49 return fd;
50 fd = socket(PF_INET6, SOCK_DGRAM, 0);
51 if (fd >= 0)
52 return fd;
53 errno = s_errno;
54 perror("Cannot create control socket");
55 return -1;
56}
57
58static int do_chflags(char *dev, __u32 flags, __u32 mask)
59{
60 struct ifreq ifr;
61 int fd;
62 int err;
63
Denis Vlasenko229b3d22006-11-27 23:44:57 +000064 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065 fd = get_ctl_fd();
66 if (fd < 0)
67 return -1;
68 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
69 if (err) {
70 perror("SIOCGIFFLAGS");
71 close(fd);
72 return -1;
73 }
74 if ((ifr.ifr_flags^flags)&mask) {
75 ifr.ifr_flags &= ~mask;
76 ifr.ifr_flags |= mask&flags;
77 err = ioctl(fd, SIOCSIFFLAGS, &ifr);
78 if (err)
79 perror("SIOCSIFFLAGS");
80 }
81 close(fd);
82 return err;
83}
84
85static int do_changename(char *dev, char *newdev)
86{
87 struct ifreq ifr;
88 int fd;
89 int err;
90
Denis Vlasenko229b3d22006-11-27 23:44:57 +000091 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
92 strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093 fd = get_ctl_fd();
94 if (fd < 0)
95 return -1;
96 err = ioctl(fd, SIOCSIFNAME, &ifr);
97 if (err) {
98 perror("SIOCSIFNAME");
99 close(fd);
100 return -1;
101 }
102 close(fd);
103 return err;
104}
105
106static int set_qlen(char *dev, int qlen)
107{
108 struct ifreq ifr;
109 int s;
110
111 s = get_ctl_fd();
112 if (s < 0)
113 return -1;
114
115 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000116 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000117 ifr.ifr_qlen = qlen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
119 perror("SIOCSIFXQLEN");
120 close(s);
121 return -1;
122 }
123 close(s);
124
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000125 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126}
127
128static int set_mtu(char *dev, int mtu)
129{
130 struct ifreq ifr;
131 int s;
132
133 s = get_ctl_fd();
134 if (s < 0)
135 return -1;
136
137 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000138 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000139 ifr.ifr_mtu = mtu;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140 if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
141 perror("SIOCSIFMTU");
142 close(s);
143 return -1;
144 }
145 close(s);
146
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000147 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000148}
149
150static int get_address(char *dev, int *htype)
151{
152 struct ifreq ifr;
153 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000154 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 int s;
156
157 s = socket(PF_PACKET, SOCK_DGRAM, 0);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000158 if (s < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000159 perror("socket(PF_PACKET)");
160 return -1;
161 }
162
163 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000164 strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
166 perror("SIOCGIFINDEX");
167 close(s);
168 return -1;
169 }
170
171 memset(&me, 0, sizeof(me));
172 me.sll_family = AF_PACKET;
173 me.sll_ifindex = ifr.ifr_ifindex;
174 me.sll_protocol = htons(ETH_P_LOOP);
175 if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
176 perror("bind");
177 close(s);
178 return -1;
179 }
180
181 alen = sizeof(me);
182 if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
183 perror("getsockname");
184 close(s);
185 return -1;
186 }
187 close(s);
188 *htype = me.sll_hatype;
189 return me.sll_halen;
190}
191
192static int parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
193{
194 int alen;
195
196 memset(ifr, 0, sizeof(*ifr));
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000197 strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198 ifr->ifr_hwaddr.sa_family = hatype;
Eric Andersend78aea82006-01-30 18:00:02 +0000199 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200 if (alen < 0)
201 return -1;
202 if (alen != halen) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000203 bb_error_msg("wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000204 return -1;
205 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000206 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000207}
208
209static int set_address(struct ifreq *ifr, int brd)
210{
211 int s;
212
213 s = get_ctl_fd();
214 if (s < 0)
215 return -1;
216 if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) {
217 perror(brd?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR");
218 close(s);
219 return -1;
220 }
221 close(s);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000222 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000223}
224
225
226static int do_set(int argc, char **argv)
227{
228 char *dev = NULL;
229 __u32 mask = 0;
230 __u32 flags = 0;
231 int qlen = -1;
232 int mtu = -1;
233 char *newaddr = NULL;
234 char *newbrd = NULL;
235 struct ifreq ifr0, ifr1;
236 char *newname = NULL;
237 int htype, halen;
238
239 while (argc > 0) {
240 if (strcmp(*argv, "up") == 0) {
241 mask |= IFF_UP;
242 flags |= IFF_UP;
243 } else if (strcmp(*argv, "down") == 0) {
244 mask |= IFF_UP;
245 flags &= ~IFF_UP;
246 } else if (strcmp(*argv, "name") == 0) {
247 NEXT_ARG();
248 newname = *argv;
249 } else if (strcmp(*argv, "mtu") == 0) {
250 NEXT_ARG();
251 if (mtu != -1)
252 duparg("mtu", *argv);
253 if (get_integer(&mtu, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000254 invarg(*argv, "mtu");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 } else if (strcmp(*argv, "multicast") == 0) {
256 NEXT_ARG();
257 mask |= IFF_MULTICAST;
258 if (strcmp(*argv, "on") == 0) {
259 flags |= IFF_MULTICAST;
260 } else if (strcmp(*argv, "off") == 0) {
261 flags &= ~IFF_MULTICAST;
262 } else
263 return on_off("multicast");
264 } else if (strcmp(*argv, "arp") == 0) {
265 NEXT_ARG();
266 mask |= IFF_NOARP;
267 if (strcmp(*argv, "on") == 0) {
268 flags &= ~IFF_NOARP;
269 } else if (strcmp(*argv, "off") == 0) {
270 flags |= IFF_NOARP;
271 } else
272 return on_off("noarp");
Rob Landley483027f2005-12-15 05:29:48 +0000273 } else if (strcmp(*argv, "addr") == 0) {
274 NEXT_ARG();
275 newaddr = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 } else {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000277 if (strcmp(*argv, "dev") == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 NEXT_ARG();
279 }
280 if (dev)
281 duparg2("dev", *argv);
282 dev = *argv;
283 }
284 argc--; argv++;
285 }
286
287 if (!dev) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000288 bb_error_msg(bb_msg_requires_arg, "\"dev\"");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000289 exit(-1);
290 }
291
292 if (newaddr || newbrd) {
293 halen = get_address(dev, &htype);
294 if (halen < 0)
295 return -1;
296 if (newaddr) {
297 if (parse_address(dev, htype, halen, newaddr, &ifr0) < 0)
298 return -1;
299 }
300 if (newbrd) {
301 if (parse_address(dev, htype, halen, newbrd, &ifr1) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000302 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303 }
304 }
305
306 if (newname && strcmp(dev, newname)) {
307 if (do_changename(dev, newname) < 0)
308 return -1;
309 dev = newname;
310 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000311 if (qlen != -1) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000312 if (set_qlen(dev, qlen) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000313 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000314 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000315 if (mtu != -1) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000316 if (set_mtu(dev, mtu) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000317 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000318 }
319 if (newaddr || newbrd) {
320 if (newbrd) {
321 if (set_address(&ifr1, 1) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000322 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000323 }
324 if (newaddr) {
325 if (set_address(&ifr0, 0) < 0)
326 return -1;
327 }
328 }
329 if (mask)
330 return do_chflags(dev, flags, mask);
331 return 0;
332}
333
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000334static int ipaddr_list_link(int argc, char **argv)
335{
336 preferred_family = AF_PACKET;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000337 return ipaddr_list_or_flush(argc, argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000338}
339
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000340int do_iplink(int argc, char **argv)
341{
342 if (argc > 0) {
343 if (matches(*argv, "set") == 0)
344 return do_set(argc-1, argv+1);
345 if (matches(*argv, "show") == 0 ||
346 matches(*argv, "lst") == 0 ||
347 matches(*argv, "list") == 0)
348 return ipaddr_list_link(argc-1, argv+1);
349 } else
350 return ipaddr_list_link(0, NULL);
351
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000352 bb_error_msg("command \"%s\" is unknown", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000353 exit(-1);
354}