blob: 12677d52263025dd4fb4226412ec4f7498e4a9fe [file] [log] [blame]
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00001/*
2 * iplink.c "ip link".
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
Glenn L McGrath275be872002-12-16 07:37:21 +000013#include <sys/ioctl.h>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include <sys/socket.h>
Eric Andersen0f08e532003-06-20 09:05:00 +000015#include <linux/version.h>
Glenn L McGrath275be872002-12-16 07:37:21 +000016
17#include <errno.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21
Eric Andersenab4e19a2003-01-14 08:54:08 +000022#include <net/if.h>
23#include <net/if_packet.h>
24#include <netpacket/packet.h>
25
26#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
27#include <net/ethernet.h>
28#else
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029#include <linux/if_ether.h>
Eric Andersenab4e19a2003-01-14 08:54:08 +000030#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000031
32#include "rt_names.h"
33#include "utils.h"
34#include "ip_common.h"
35
Glenn L McGrath275be872002-12-16 07:37:21 +000036#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000037
Eric Andersenab4e19a2003-01-14 08:54:08 +000038
39/* take from linux/sockios.h */
40#define SIOCSIFNAME 0x8923 /* set interface name */
41
Glenn L McGrath2626ef62002-12-02 01:40:05 +000042static int do_link;
43
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000044static int on_off(char *msg)
45{
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 bb_error_msg("Error: argument of \"%s\" must be \"on\" or \"off\"", msg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047 return -1;
48}
49
50static int get_ctl_fd(void)
51{
52 int s_errno;
53 int fd;
54
55 fd = socket(PF_INET, SOCK_DGRAM, 0);
56 if (fd >= 0)
57 return fd;
58 s_errno = errno;
59 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
60 if (fd >= 0)
61 return fd;
62 fd = socket(PF_INET6, SOCK_DGRAM, 0);
63 if (fd >= 0)
64 return fd;
65 errno = s_errno;
66 perror("Cannot create control socket");
67 return -1;
68}
69
70static int do_chflags(char *dev, __u32 flags, __u32 mask)
71{
72 struct ifreq ifr;
73 int fd;
74 int err;
75
76 strcpy(ifr.ifr_name, dev);
77 fd = get_ctl_fd();
78 if (fd < 0)
79 return -1;
80 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
81 if (err) {
82 perror("SIOCGIFFLAGS");
83 close(fd);
84 return -1;
85 }
86 if ((ifr.ifr_flags^flags)&mask) {
87 ifr.ifr_flags &= ~mask;
88 ifr.ifr_flags |= mask&flags;
89 err = ioctl(fd, SIOCSIFFLAGS, &ifr);
90 if (err)
91 perror("SIOCSIFFLAGS");
92 }
93 close(fd);
94 return err;
95}
96
97static int do_changename(char *dev, char *newdev)
98{
Eric Andersen0f08e532003-06-20 09:05:00 +000099#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100 struct ifreq ifr;
101 int fd;
102 int err;
103
104 strcpy(ifr.ifr_name, dev);
105 strcpy(ifr.ifr_newname, newdev);
106 fd = get_ctl_fd();
107 if (fd < 0)
108 return -1;
109 err = ioctl(fd, SIOCSIFNAME, &ifr);
110 if (err) {
111 perror("SIOCSIFNAME");
112 close(fd);
113 return -1;
114 }
115 close(fd);
116 return err;
Eric Andersen0f08e532003-06-20 09:05:00 +0000117#endif
118 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119}
120
121static int set_qlen(char *dev, int qlen)
122{
123 struct ifreq ifr;
124 int s;
125
126 s = get_ctl_fd();
127 if (s < 0)
128 return -1;
129
130 memset(&ifr, 0, sizeof(ifr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000131 strcpy(ifr.ifr_name, dev);
132 ifr.ifr_qlen = qlen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
134 perror("SIOCSIFXQLEN");
135 close(s);
136 return -1;
137 }
138 close(s);
139
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000140 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141}
142
143static int set_mtu(char *dev, int mtu)
144{
145 struct ifreq ifr;
146 int s;
147
148 s = get_ctl_fd();
149 if (s < 0)
150 return -1;
151
152 memset(&ifr, 0, sizeof(ifr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000153 strcpy(ifr.ifr_name, dev);
154 ifr.ifr_mtu = mtu;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
156 perror("SIOCSIFMTU");
157 close(s);
158 return -1;
159 }
160 close(s);
161
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000162 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163}
164
165static int get_address(char *dev, int *htype)
166{
167 struct ifreq ifr;
168 struct sockaddr_ll me;
Eric Andersend78aea82006-01-30 18:00:02 +0000169 socklen_t alen;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000170 int s;
171
172 s = socket(PF_PACKET, SOCK_DGRAM, 0);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000173 if (s < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174 perror("socket(PF_PACKET)");
175 return -1;
176 }
177
178 memset(&ifr, 0, sizeof(ifr));
179 strcpy(ifr.ifr_name, dev);
180 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
181 perror("SIOCGIFINDEX");
182 close(s);
183 return -1;
184 }
185
186 memset(&me, 0, sizeof(me));
187 me.sll_family = AF_PACKET;
188 me.sll_ifindex = ifr.ifr_ifindex;
189 me.sll_protocol = htons(ETH_P_LOOP);
190 if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
191 perror("bind");
192 close(s);
193 return -1;
194 }
195
196 alen = sizeof(me);
197 if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
198 perror("getsockname");
199 close(s);
200 return -1;
201 }
202 close(s);
203 *htype = me.sll_hatype;
204 return me.sll_halen;
205}
206
207static int parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
208{
209 int alen;
210
211 memset(ifr, 0, sizeof(*ifr));
212 strcpy(ifr->ifr_name, dev);
213 ifr->ifr_hwaddr.sa_family = hatype;
Eric Andersend78aea82006-01-30 18:00:02 +0000214 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 if (alen < 0)
216 return -1;
217 if (alen != halen) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_error_msg("Wrong address (%s) length: expected %d bytes", lla, halen);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000219 return -1;
220 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000221 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000222}
223
224static int set_address(struct ifreq *ifr, int brd)
225{
226 int s;
227
228 s = get_ctl_fd();
229 if (s < 0)
230 return -1;
231 if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) {
232 perror(brd?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR");
233 close(s);
234 return -1;
235 }
236 close(s);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000237 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000238}
239
240
241static int do_set(int argc, char **argv)
242{
243 char *dev = NULL;
244 __u32 mask = 0;
245 __u32 flags = 0;
246 int qlen = -1;
247 int mtu = -1;
248 char *newaddr = NULL;
249 char *newbrd = NULL;
250 struct ifreq ifr0, ifr1;
251 char *newname = NULL;
252 int htype, halen;
253
254 while (argc > 0) {
255 if (strcmp(*argv, "up") == 0) {
256 mask |= IFF_UP;
257 flags |= IFF_UP;
258 } else if (strcmp(*argv, "down") == 0) {
259 mask |= IFF_UP;
260 flags &= ~IFF_UP;
261 } else if (strcmp(*argv, "name") == 0) {
262 NEXT_ARG();
263 newname = *argv;
264 } else if (strcmp(*argv, "mtu") == 0) {
265 NEXT_ARG();
266 if (mtu != -1)
267 duparg("mtu", *argv);
268 if (get_integer(&mtu, *argv, 0))
269 invarg("Invalid \"mtu\" value\n", *argv);
270 } else if (strcmp(*argv, "multicast") == 0) {
271 NEXT_ARG();
272 mask |= IFF_MULTICAST;
273 if (strcmp(*argv, "on") == 0) {
274 flags |= IFF_MULTICAST;
275 } else if (strcmp(*argv, "off") == 0) {
276 flags &= ~IFF_MULTICAST;
277 } else
278 return on_off("multicast");
279 } else if (strcmp(*argv, "arp") == 0) {
280 NEXT_ARG();
281 mask |= IFF_NOARP;
282 if (strcmp(*argv, "on") == 0) {
283 flags &= ~IFF_NOARP;
284 } else if (strcmp(*argv, "off") == 0) {
285 flags |= IFF_NOARP;
286 } else
287 return on_off("noarp");
Rob Landley483027f2005-12-15 05:29:48 +0000288 } else if (strcmp(*argv, "addr") == 0) {
289 NEXT_ARG();
290 newaddr = *argv;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000291 } else {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000292 if (strcmp(*argv, "dev") == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000293 NEXT_ARG();
294 }
295 if (dev)
296 duparg2("dev", *argv);
297 dev = *argv;
298 }
299 argc--; argv++;
300 }
301
302 if (!dev) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000303 bb_error_msg("Not enough of information: \"dev\" argument is required.");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304 exit(-1);
305 }
306
307 if (newaddr || newbrd) {
308 halen = get_address(dev, &htype);
309 if (halen < 0)
310 return -1;
311 if (newaddr) {
312 if (parse_address(dev, htype, halen, newaddr, &ifr0) < 0)
313 return -1;
314 }
315 if (newbrd) {
316 if (parse_address(dev, htype, halen, newbrd, &ifr1) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000317 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000318 }
319 }
320
321 if (newname && strcmp(dev, newname)) {
322 if (do_changename(dev, newname) < 0)
323 return -1;
324 dev = newname;
325 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000326 if (qlen != -1) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000327 if (set_qlen(dev, qlen) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000328 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000329 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000330 if (mtu != -1) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000331 if (set_mtu(dev, mtu) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000332 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000333 }
334 if (newaddr || newbrd) {
335 if (newbrd) {
336 if (set_address(&ifr1, 1) < 0)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000337 return -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000338 }
339 if (newaddr) {
340 if (set_address(&ifr0, 0) < 0)
341 return -1;
342 }
343 }
344 if (mask)
345 return do_chflags(dev, flags, mask);
346 return 0;
347}
348
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000349static int ipaddr_list_link(int argc, char **argv)
350{
351 preferred_family = AF_PACKET;
352 do_link = 1;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000353 return ipaddr_list_or_flush(argc, argv, 0);
Glenn L McGrath2626ef62002-12-02 01:40:05 +0000354}
355
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000356int do_iplink(int argc, char **argv)
357{
358 if (argc > 0) {
359 if (matches(*argv, "set") == 0)
360 return do_set(argc-1, argv+1);
361 if (matches(*argv, "show") == 0 ||
362 matches(*argv, "lst") == 0 ||
363 matches(*argv, "list") == 0)
364 return ipaddr_list_link(argc-1, argv+1);
365 } else
366 return ipaddr_list_link(0, NULL);
367
Eric Andersenf71ad6c2004-04-26 19:32:49 +0000368 bb_error_msg("Command \"%s\" is unknown.", *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000369 exit(-1);
370}