1 /* vi: set sw=4 ts=4: */
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
20 #include <net/if_packet.h>
21 #include <netpacket/packet.h>
23 #include <net/ethernet.h>
27 #include "ip_common.h"
29 /* take from linux/sockios.h */
30 #define SIOCSIFNAME 0x8923 /* set interface name */
32 static int on_off(char *msg)
34 bb_error_msg("Error: argument of \"%s\" must be \"on\" or \"off\"", msg);
38 static int get_ctl_fd(void)
43 fd = socket(PF_INET, SOCK_DGRAM, 0);
47 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
50 fd = socket(PF_INET6, SOCK_DGRAM, 0);
54 perror("Cannot create control socket");
58 static int do_chflags(char *dev, __u32 flags, __u32 mask)
64 strcpy(ifr.ifr_name, dev);
68 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
70 perror("SIOCGIFFLAGS");
74 if ((ifr.ifr_flags^flags)&mask) {
75 ifr.ifr_flags &= ~mask;
76 ifr.ifr_flags |= mask&flags;
77 err = ioctl(fd, SIOCSIFFLAGS, &ifr);
79 perror("SIOCSIFFLAGS");
85 static int do_changename(char *dev, char *newdev)
91 strcpy(ifr.ifr_name, dev);
92 strcpy(ifr.ifr_newname, newdev);
96 err = ioctl(fd, SIOCSIFNAME, &ifr);
98 perror("SIOCSIFNAME");
106 static int set_qlen(char *dev, int qlen)
115 memset(&ifr, 0, sizeof(ifr));
116 strcpy(ifr.ifr_name, dev);
118 if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
119 perror("SIOCSIFXQLEN");
128 static int set_mtu(char *dev, int mtu)
137 memset(&ifr, 0, sizeof(ifr));
138 strcpy(ifr.ifr_name, dev);
140 if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
141 perror("SIOCSIFMTU");
150 static int get_address(char *dev, int *htype)
153 struct sockaddr_ll me;
157 s = socket(PF_PACKET, SOCK_DGRAM, 0);
159 perror("socket(PF_PACKET)");
163 memset(&ifr, 0, sizeof(ifr));
164 strcpy(ifr.ifr_name, dev);
165 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
166 perror("SIOCGIFINDEX");
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) {
182 if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
183 perror("getsockname");
188 *htype = me.sll_hatype;
192 static int parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
196 memset(ifr, 0, sizeof(*ifr));
197 strcpy(ifr->ifr_name, dev);
198 ifr->ifr_hwaddr.sa_family = hatype;
199 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
203 bb_error_msg("Wrong address (%s) length: expected %d bytes", lla, halen);
209 static int set_address(struct ifreq *ifr, int brd)
216 if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) {
217 perror(brd?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR");
226 static int do_set(int argc, char **argv)
233 char *newaddr = NULL;
235 struct ifreq ifr0, ifr1;
236 char *newname = NULL;
240 if (strcmp(*argv, "up") == 0) {
243 } else if (strcmp(*argv, "down") == 0) {
246 } else if (strcmp(*argv, "name") == 0) {
249 } else if (strcmp(*argv, "mtu") == 0) {
252 duparg("mtu", *argv);
253 if (get_integer(&mtu, *argv, 0))
254 invarg(*argv, "mtu");
255 } else if (strcmp(*argv, "multicast") == 0) {
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;
263 return on_off("multicast");
264 } else if (strcmp(*argv, "arp") == 0) {
267 if (strcmp(*argv, "on") == 0) {
269 } else if (strcmp(*argv, "off") == 0) {
272 return on_off("noarp");
273 } else if (strcmp(*argv, "addr") == 0) {
277 if (strcmp(*argv, "dev") == 0) {
281 duparg2("dev", *argv);
288 bb_error_msg(bb_msg_requires_arg, "\"dev\"");
292 if (newaddr || newbrd) {
293 halen = get_address(dev, &htype);
297 if (parse_address(dev, htype, halen, newaddr, &ifr0) < 0)
301 if (parse_address(dev, htype, halen, newbrd, &ifr1) < 0)
306 if (newname && strcmp(dev, newname)) {
307 if (do_changename(dev, newname) < 0)
312 if (set_qlen(dev, qlen) < 0)
316 if (set_mtu(dev, mtu) < 0)
319 if (newaddr || newbrd) {
321 if (set_address(&ifr1, 1) < 0)
325 if (set_address(&ifr0, 0) < 0)
330 return do_chflags(dev, flags, mask);
334 static int ipaddr_list_link(int argc, char **argv)
336 preferred_family = AF_PACKET;
337 return ipaddr_list_or_flush(argc, argv, 0);
340 int do_iplink(int argc, char **argv)
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);
350 return ipaddr_list_link(0, NULL);
352 bb_error_msg("Command \"%s\" is unknown.", *argv);