fb79a2e2cf
function old new delta do_iptunnel 203 977 +774 process_dev 5328 5494 +166 ioctl_or_perror - 54 +54 ioctl_or_perror_and_die - 51 +51 ioctl_alt_func - 49 +49 bb_ioctl_or_warn - 47 +47 do_add_ioctl 102 145 +43 bb_xioctl - 39 +39 print_value_on_off - 31 +31 get_lcm 105 123 +18 arp_main 2155 2167 +12 .................. zcip_main 1576 1566 -10 setlogcons_main 92 82 -10 dumpkmap_main 263 253 -10 do_get_ioctl 85 75 -10 setkeycodes_main 165 154 -11 write_table 244 232 -12 vconfig_main 318 306 -12 do_del_ioctl 93 81 -12 set_address 75 62 -13 maybe_set_utc 30 16 -14 loadfont_main 495 479 -16 slattach_main 712 695 -17 do_loadfont 191 174 -17 do_iplink 1155 1136 -19 getty_main 2583 2562 -21 fbset_main 2058 2035 -23 do_time 588 565 -23 xioctl 25 - -25 read_rtc 186 160 -26 parse_conf 1299 1270 -29 udhcp_read_interface 269 239 -30 bb_ioctl 45 - -45 bb_ioctl_alt 70 - -70 bb_ioctl_on_off 78 - -78 .rodata 129370 129018 -352 do_show 799 - -799 ------------------------------------------------------------------------------ (add/remove: 6/5 grow/shrink: 13/49 up/down: 1316/-1864) Total: -548 bytes text data bss dec hex filename 675352 2740 13968 692060 a8f5c busybox_old 674804 2740 13968 691512 a8d38 busybox_unstripped
294 lines
6.3 KiB
C
294 lines
6.3 KiB
C
/* vi: set sw=4 ts=4: */
|
|
/*
|
|
* iplink.c "ip link".
|
|
*
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
*
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
*/
|
|
|
|
//#include <sys/ioctl.h>
|
|
//#include <sys/socket.h>
|
|
#include <net/if.h>
|
|
#include <net/if_packet.h>
|
|
#include <netpacket/packet.h>
|
|
#include <net/ethernet.h>
|
|
|
|
#include "ip_common.h" /* #include "libbb.h" is inside */
|
|
#include "rt_names.h"
|
|
#include "utils.h"
|
|
|
|
/* taken from linux/sockios.h */
|
|
#define SIOCSIFNAME 0x8923 /* set interface name */
|
|
|
|
static void on_off(const char *msg) ATTRIBUTE_NORETURN;
|
|
static void on_off(const char *msg)
|
|
{
|
|
bb_error_msg_and_die("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
|
|
}
|
|
|
|
/* Exits on error */
|
|
static int get_ctl_fd(void)
|
|
{
|
|
int fd;
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
if (fd >= 0)
|
|
return fd;
|
|
fd = socket(PF_PACKET, SOCK_DGRAM, 0);
|
|
if (fd >= 0)
|
|
return fd;
|
|
return xsocket(PF_INET6, SOCK_DGRAM, 0);
|
|
}
|
|
|
|
/* Exits on error */
|
|
static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
|
|
{
|
|
struct ifreq ifr;
|
|
int fd;
|
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
|
fd = get_ctl_fd();
|
|
xioctl(fd, SIOCGIFFLAGS, &ifr);
|
|
if ((ifr.ifr_flags ^ flags) & mask) {
|
|
ifr.ifr_flags &= ~mask;
|
|
ifr.ifr_flags |= mask & flags;
|
|
xioctl(fd, SIOCSIFFLAGS, &ifr);
|
|
}
|
|
close(fd);
|
|
}
|
|
|
|
/* Exits on error */
|
|
static void do_changename(char *dev, char *newdev)
|
|
{
|
|
struct ifreq ifr;
|
|
int fd;
|
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
|
strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
|
|
fd = get_ctl_fd();
|
|
xioctl(fd, SIOCSIFNAME, &ifr);
|
|
close(fd);
|
|
}
|
|
|
|
/* Exits on error */
|
|
static void set_qlen(char *dev, int qlen)
|
|
{
|
|
struct ifreq ifr;
|
|
int s;
|
|
|
|
s = get_ctl_fd();
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
|
ifr.ifr_qlen = qlen;
|
|
xioctl(s, SIOCSIFTXQLEN, &ifr);
|
|
close(s);
|
|
}
|
|
|
|
/* Exits on error */
|
|
static void set_mtu(char *dev, int mtu)
|
|
{
|
|
struct ifreq ifr;
|
|
int s;
|
|
|
|
s = get_ctl_fd();
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
|
ifr.ifr_mtu = mtu;
|
|
xioctl(s, SIOCSIFMTU, &ifr);
|
|
close(s);
|
|
}
|
|
|
|
/* Exits on error */
|
|
static int get_address(char *dev, int *htype)
|
|
{
|
|
struct ifreq ifr;
|
|
struct sockaddr_ll me;
|
|
socklen_t alen;
|
|
int s;
|
|
|
|
s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
|
xioctl(s, SIOCGIFINDEX, &ifr);
|
|
|
|
memset(&me, 0, sizeof(me));
|
|
me.sll_family = AF_PACKET;
|
|
me.sll_ifindex = ifr.ifr_ifindex;
|
|
me.sll_protocol = htons(ETH_P_LOOP);
|
|
xbind(s, (struct sockaddr*)&me, sizeof(me));
|
|
|
|
alen = sizeof(me);
|
|
if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
|
|
bb_perror_msg_and_die("getsockname");
|
|
}
|
|
close(s);
|
|
*htype = me.sll_hatype;
|
|
return me.sll_halen;
|
|
}
|
|
|
|
/* Exits on error */
|
|
static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
|
|
{
|
|
int alen;
|
|
|
|
memset(ifr, 0, sizeof(*ifr));
|
|
strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
|
|
ifr->ifr_hwaddr.sa_family = hatype;
|
|
alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
|
|
if (alen < 0)
|
|
exit(1);
|
|
if (alen != halen) {
|
|
bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
|
|
}
|
|
}
|
|
|
|
/* Exits on error */
|
|
static void set_address(struct ifreq *ifr, int brd)
|
|
{
|
|
int s;
|
|
|
|
s = get_ctl_fd();
|
|
if (brd)
|
|
xioctl(s, SIOCSIFHWBROADCAST, ifr);
|
|
else
|
|
xioctl(s, SIOCSIFHWADDR, ifr);
|
|
close(s);
|
|
}
|
|
|
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
|
static int do_set(int argc, char **argv)
|
|
{
|
|
char *dev = NULL;
|
|
uint32_t mask = 0;
|
|
uint32_t flags = 0;
|
|
int qlen = -1;
|
|
int mtu = -1;
|
|
char *newaddr = NULL;
|
|
char *newbrd = NULL;
|
|
struct ifreq ifr0, ifr1;
|
|
char *newname = NULL;
|
|
int htype, halen;
|
|
static const char * const keywords[] = {
|
|
"up", "down", "name", "mtu", "multicast", "arp", "addr", "dev",
|
|
"on", "off", NULL
|
|
};
|
|
enum { ARG_up = 1, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp,
|
|
ARG_addr, ARG_dev, PARM_on, PARM_off };
|
|
smalluint key;
|
|
|
|
while (argc > 0) {
|
|
key = index_in_str_array(keywords, *argv) + 1;
|
|
if (key == ARG_up) {
|
|
mask |= IFF_UP;
|
|
flags |= IFF_UP;
|
|
} else if (key == ARG_down) {
|
|
mask |= IFF_UP;
|
|
flags &= ~IFF_UP;
|
|
} else if (key == ARG_name) {
|
|
NEXT_ARG();
|
|
newname = *argv;
|
|
} else if (key == ARG_mtu) {
|
|
NEXT_ARG();
|
|
if (mtu != -1)
|
|
duparg("mtu", *argv);
|
|
if (get_integer(&mtu, *argv, 0))
|
|
invarg(*argv, "mtu");
|
|
} else if (key == ARG_multicast) {
|
|
NEXT_ARG();
|
|
mask |= IFF_MULTICAST;
|
|
key = index_in_str_array(keywords, *argv) + 1;
|
|
if (key == PARM_on) {
|
|
flags |= IFF_MULTICAST;
|
|
} else if (key == PARM_off) {
|
|
flags &= ~IFF_MULTICAST;
|
|
} else
|
|
on_off("multicast");
|
|
} else if (key == ARG_arp) {
|
|
NEXT_ARG();
|
|
mask |= IFF_NOARP;
|
|
key = index_in_str_array(keywords, *argv) + 1;
|
|
if (key == PARM_on) {
|
|
flags &= ~IFF_NOARP;
|
|
} else if (key == PARM_off) {
|
|
flags |= IFF_NOARP;
|
|
} else
|
|
on_off("arp");
|
|
} else if (key == ARG_addr) {
|
|
NEXT_ARG();
|
|
newaddr = *argv;
|
|
} else {
|
|
if (key == ARG_dev) {
|
|
NEXT_ARG();
|
|
}
|
|
if (dev)
|
|
duparg2("dev", *argv);
|
|
dev = *argv;
|
|
}
|
|
argc--; argv++;
|
|
}
|
|
|
|
if (!dev) {
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
|
|
}
|
|
|
|
if (newaddr || newbrd) {
|
|
halen = get_address(dev, &htype);
|
|
if (newaddr) {
|
|
parse_address(dev, htype, halen, newaddr, &ifr0);
|
|
}
|
|
if (newbrd) {
|
|
parse_address(dev, htype, halen, newbrd, &ifr1);
|
|
}
|
|
}
|
|
|
|
if (newname && strcmp(dev, newname)) {
|
|
do_changename(dev, newname);
|
|
dev = newname;
|
|
}
|
|
if (qlen != -1) {
|
|
set_qlen(dev, qlen);
|
|
}
|
|
if (mtu != -1) {
|
|
set_mtu(dev, mtu);
|
|
}
|
|
if (newaddr || newbrd) {
|
|
if (newbrd) {
|
|
set_address(&ifr1, 1);
|
|
}
|
|
if (newaddr) {
|
|
set_address(&ifr0, 0);
|
|
}
|
|
}
|
|
if (mask)
|
|
do_chflags(dev, flags, mask);
|
|
return 0;
|
|
}
|
|
|
|
static int ipaddr_list_link(int argc, char **argv)
|
|
{
|
|
preferred_family = AF_PACKET;
|
|
return ipaddr_list_or_flush(argc, argv, 0);
|
|
}
|
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
|
int do_iplink(int argc, char **argv)
|
|
{
|
|
static const char * const keywords[] = {
|
|
"set", "show", "lst", "list", NULL
|
|
};
|
|
smalluint key;
|
|
if (argc <= 0)
|
|
return ipaddr_list_link(0, NULL);
|
|
key = index_in_substr_array(keywords, *argv) + 1;
|
|
if (key == 0)
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
|
|
argc--; argv++;
|
|
if (key == 1) /* set */
|
|
return do_set(argc, argv);
|
|
else /* show, lst, list */
|
|
return ipaddr_list_link(argc, argv);
|
|
}
|