2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
2006-10-20 18:58:22 +05:30
|
|
|
* iplink.c "ip link".
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
2006-10-20 18:58:22 +05:30
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
2006-06-03 02:23:38 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2002-11-10 07:03:55 +05:30
|
|
|
*/
|
|
|
|
|
2006-06-03 02:23:38 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2002-12-16 13:07:21 +05:30
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2003-01-14 14:24:08 +05:30
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_packet.h>
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
|
|
|
|
#include <net/ethernet.h>
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
#include "rt_names.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "ip_common.h"
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* taken from linux/sockios.h */
|
2003-01-14 14:24:08 +05:30
|
|
|
#define SIOCSIFNAME 0x8923 /* set interface name */
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
static void on_off(const char *msg) ATTRIBUTE_NORETURN;
|
|
|
|
static void on_off(const char *msg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
2002-11-10 07:03:55 +05:30
|
|
|
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;
|
2007-04-12 17:04:39 +05:30
|
|
|
return xsocket(PF_INET6, SOCK_DGRAM, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
|
|
|
static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
2002-11-10 07:03:55 +05:30
|
|
|
fd = get_ctl_fd();
|
2007-04-07 06:44:45 +05:30
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
|
|
|
|
bb_perror_msg_and_die("SIOCGIFFLAGS");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-04-07 06:44:45 +05:30
|
|
|
if ((ifr.ifr_flags ^ flags) & mask) {
|
2002-11-10 07:03:55 +05:30
|
|
|
ifr.ifr_flags &= ~mask;
|
2007-04-07 06:44:45 +05:30
|
|
|
ifr.ifr_flags |= mask & flags;
|
|
|
|
if (ioctl(fd, SIOCSIFFLAGS, &ifr))
|
|
|
|
bb_perror_msg_and_die("SIOCSIFFLAGS");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
|
|
|
static void do_changename(char *dev, char *newdev)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int fd;
|
|
|
|
int err;
|
|
|
|
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
|
|
|
strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
|
2002-11-10 07:03:55 +05:30
|
|
|
fd = get_ctl_fd();
|
|
|
|
err = ioctl(fd, SIOCSIFNAME, &ifr);
|
|
|
|
if (err) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_perror_msg_and_die("SIOCSIFNAME");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
|
|
|
static void set_qlen(char *dev, int qlen)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = get_ctl_fd();
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
2004-03-15 13:59:22 +05:30
|
|
|
ifr.ifr_qlen = qlen;
|
2002-11-10 07:03:55 +05:30
|
|
|
if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_perror_msg_and_die("SIOCSIFXQLEN");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
|
|
|
static void set_mtu(char *dev, int mtu)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = get_ctl_fd();
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
2004-03-15 13:59:22 +05:30
|
|
|
ifr.ifr_mtu = mtu;
|
2002-11-10 07:03:55 +05:30
|
|
|
if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_perror_msg_and_die("SIOCSIFMTU");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
2002-11-10 07:03:55 +05:30
|
|
|
static int get_address(char *dev, int *htype)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
struct sockaddr_ll me;
|
2006-01-30 23:30:02 +05:30
|
|
|
socklen_t alen;
|
2002-11-10 07:03:55 +05:30
|
|
|
int s;
|
|
|
|
|
2007-04-12 17:04:39 +05:30
|
|
|
s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
|
2002-11-10 07:03:55 +05:30
|
|
|
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_perror_msg_and_die("SIOCGIFINDEX");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
memset(&me, 0, sizeof(me));
|
|
|
|
me.sll_family = AF_PACKET;
|
|
|
|
me.sll_ifindex = ifr.ifr_ifindex;
|
|
|
|
me.sll_protocol = htons(ETH_P_LOOP);
|
2007-04-12 17:04:39 +05:30
|
|
|
xbind(s, (struct sockaddr*)&me, sizeof(me));
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
alen = sizeof(me);
|
|
|
|
if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_perror_msg_and_die("getsockname");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
*htype = me.sll_hatype;
|
|
|
|
return me.sll_halen;
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
|
|
|
static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
int alen;
|
|
|
|
|
|
|
|
memset(ifr, 0, sizeof(*ifr));
|
2006-11-28 05:14:57 +05:30
|
|
|
strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
|
2002-11-10 07:03:55 +05:30
|
|
|
ifr->ifr_hwaddr.sa_family = hatype;
|
2006-01-30 23:30:02 +05:30
|
|
|
alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
|
2002-11-10 07:03:55 +05:30
|
|
|
if (alen < 0)
|
2007-04-07 06:44:45 +05:30
|
|
|
exit(1);
|
2002-11-10 07:03:55 +05:30
|
|
|
if (alen != halen) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Exits on error */
|
|
|
|
static void set_address(struct ifreq *ifr, int brd)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = get_ctl_fd();
|
2007-04-07 06:44:45 +05:30
|
|
|
if (ioctl(s, brd ? SIOCSIFHWBROADCAST :SIOCSIFHWADDR, ifr) < 0) {
|
|
|
|
bb_perror_msg_and_die(brd ? "SIOCSIFHWBROADCAST" : "SIOCSIFHWADDR");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2002-11-10 07:03:55 +05:30
|
|
|
static int do_set(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *dev = NULL;
|
2007-01-01 00:27:37 +05:30
|
|
|
uint32_t mask = 0;
|
|
|
|
uint32_t flags = 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
int qlen = -1;
|
|
|
|
int mtu = -1;
|
|
|
|
char *newaddr = NULL;
|
|
|
|
char *newbrd = NULL;
|
|
|
|
struct ifreq ifr0, ifr1;
|
|
|
|
char *newname = NULL;
|
|
|
|
int htype, halen;
|
2007-04-12 17:04:39 +05:30
|
|
|
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;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
while (argc > 0) {
|
2007-04-12 17:04:39 +05:30
|
|
|
key = index_in_str_array(keywords, *argv) + 1;
|
|
|
|
if (key == ARG_up) {
|
2002-11-10 07:03:55 +05:30
|
|
|
mask |= IFF_UP;
|
|
|
|
flags |= IFF_UP;
|
2007-04-12 17:04:39 +05:30
|
|
|
} else if (key == ARG_down) {
|
2002-11-10 07:03:55 +05:30
|
|
|
mask |= IFF_UP;
|
|
|
|
flags &= ~IFF_UP;
|
2007-04-12 17:04:39 +05:30
|
|
|
} else if (key == ARG_name) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
newname = *argv;
|
2007-04-12 17:04:39 +05:30
|
|
|
} else if (key == ARG_mtu) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
if (mtu != -1)
|
|
|
|
duparg("mtu", *argv);
|
|
|
|
if (get_integer(&mtu, *argv, 0))
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "mtu");
|
2007-04-12 17:04:39 +05:30
|
|
|
} else if (key == ARG_multicast) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
mask |= IFF_MULTICAST;
|
2007-04-12 17:04:39 +05:30
|
|
|
key = index_in_str_array(keywords, *argv) + 1;
|
|
|
|
if (key == PARM_on) {
|
2002-11-10 07:03:55 +05:30
|
|
|
flags |= IFF_MULTICAST;
|
2007-04-12 17:04:39 +05:30
|
|
|
} else if (key == PARM_off) {
|
2002-11-10 07:03:55 +05:30
|
|
|
flags &= ~IFF_MULTICAST;
|
|
|
|
} else
|
2007-04-07 06:44:45 +05:30
|
|
|
on_off("multicast");
|
2007-04-12 17:04:39 +05:30
|
|
|
} else if (key == ARG_arp) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
mask |= IFF_NOARP;
|
2007-04-12 17:04:39 +05:30
|
|
|
key = index_in_str_array(keywords, *argv) + 1;
|
|
|
|
if (key == PARM_on) {
|
2002-11-10 07:03:55 +05:30
|
|
|
flags &= ~IFF_NOARP;
|
2007-04-12 17:04:39 +05:30
|
|
|
} else if (key == PARM_off) {
|
2002-11-10 07:03:55 +05:30
|
|
|
flags |= IFF_NOARP;
|
|
|
|
} else
|
2007-04-12 17:04:39 +05:30
|
|
|
on_off("arp");
|
|
|
|
} else if (key == ARG_addr) {
|
2005-12-15 10:59:48 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
newaddr = *argv;
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
2007-04-12 17:04:39 +05:30
|
|
|
if (key == ARG_dev) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (dev)
|
|
|
|
duparg2("dev", *argv);
|
|
|
|
dev = *argv;
|
|
|
|
}
|
2007-04-12 17:04:39 +05:30
|
|
|
argc--; argv++;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (newaddr || newbrd) {
|
|
|
|
halen = get_address(dev, &htype);
|
|
|
|
if (newaddr) {
|
2007-04-07 06:44:45 +05:30
|
|
|
parse_address(dev, htype, halen, newaddr, &ifr0);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
if (newbrd) {
|
2007-04-07 06:44:45 +05:30
|
|
|
parse_address(dev, htype, halen, newbrd, &ifr1);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newname && strcmp(dev, newname)) {
|
2007-04-07 06:44:45 +05:30
|
|
|
do_changename(dev, newname);
|
2002-11-10 07:03:55 +05:30
|
|
|
dev = newname;
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
if (qlen != -1) {
|
2007-04-07 06:44:45 +05:30
|
|
|
set_qlen(dev, qlen);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
if (mtu != -1) {
|
2007-04-07 06:44:45 +05:30
|
|
|
set_mtu(dev, mtu);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
if (newaddr || newbrd) {
|
|
|
|
if (newbrd) {
|
2007-04-07 06:44:45 +05:30
|
|
|
set_address(&ifr1, 1);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
if (newaddr) {
|
2007-04-07 06:44:45 +05:30
|
|
|
set_address(&ifr0, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mask)
|
2007-04-07 06:44:45 +05:30
|
|
|
do_chflags(dev, flags, mask);
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-02 07:10:05 +05:30
|
|
|
static int ipaddr_list_link(int argc, char **argv)
|
|
|
|
{
|
|
|
|
preferred_family = AF_PACKET;
|
2003-01-14 03:10:38 +05:30
|
|
|
return ipaddr_list_or_flush(argc, argv, 0);
|
2002-12-02 07:10:05 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2002-11-10 07:03:55 +05:30
|
|
|
int do_iplink(int argc, char **argv)
|
|
|
|
{
|
2007-04-12 17:04:39 +05:30
|
|
|
static const char * const keywords[] = {
|
|
|
|
"set", "show", "lst", "list", NULL
|
|
|
|
};
|
|
|
|
smalluint key;
|
2007-04-07 06:44:45 +05:30
|
|
|
if (argc <= 0)
|
2002-11-10 07:03:55 +05:30
|
|
|
return ipaddr_list_link(0, NULL);
|
2007-04-12 17:04:39 +05:30
|
|
|
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);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|