/* vi: set sw=4 ts=4: */
/*
- * iplink.c "ip link".
- *
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ * Patrick McHardy <kaber@trash.net>
*
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
#include <net/if.h>
-#include <net/if_packet.h>
+/*#include <net/if_packet.h> - not needed? */
#include <netpacket/packet.h>
-#include <net/ethernet.h>
+#include <netinet/if_ether.h>
+#include <linux/if_vlan.h>
#include "ip_common.h" /* #include "libbb.h" is inside */
#include "rt_names.h"
#include "utils.h"
+#undef ETH_P_8021AD
+#define ETH_P_8021AD 0x88A8
+#undef VLAN_FLAG_REORDER_HDR
+#define VLAN_FLAG_REORDER_HDR 0x1
+#undef VLAN_FLAG_GVRP
+#define VLAN_FLAG_GVRP 0x2
+#undef VLAN_FLAG_LOOSE_BINDING
+#define VLAN_FLAG_LOOSE_BINDING 0x4
+#undef VLAN_FLAG_MVRP
+#define VLAN_FLAG_MVRP 0x8
+#undef IFLA_VLAN_PROTOCOL
+#define IFLA_VLAN_PROTOCOL 5
+
+#ifndef IFLA_LINKINFO
+# define IFLA_LINKINFO 18
+# define IFLA_INFO_KIND 1
+# define IFLA_INFO_DATA 2
+#endif
+
+#ifndef IFLA_VLAN_MAX
+# define IFLA_VLAN_ID 1
+# define IFLA_VLAN_FLAGS 2
+struct ifla_vlan_flags {
+ uint32_t flags;
+ uint32_t mask;
+};
+#endif
+
/* taken from linux/sockios.h */
-#define SIOCSIFNAME 0x8923 /* set interface name */
+#define SIOCSIFNAME 0x8923 /* set interface name */
+
+#if 0
+# define dbg(...) bb_error_msg(__VA_ARGS__)
+#else
+# define dbg(...) ((void)0)
+#endif
/* Exits on error */
static int get_ctl_fd(void)
char *newname = NULL;
int htype, halen;
static const char keywords[] ALIGN1 =
- "up\0""down\0""name\0""mtu\0""multicast\0"
+ "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
"arp\0""address\0""dev\0";
- enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_multicast,
+ enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
ARG_arp, ARG_addr, ARG_dev };
static const char str_on_off[] ALIGN1 = "on\0""off\0";
enum { PARM_on = 0, PARM_off };
if (mtu != -1)
duparg("mtu", *argv);
mtu = get_unsigned(*argv, "mtu");
+ } else if (key == ARG_qlen) {
+ NEXT_ARG();
+ if (qlen != -1)
+ duparg("qlen", *argv);
+ qlen = get_unsigned(*argv, "qlen");
} else if (key == ARG_addr) {
NEXT_ARG();
newaddr = *argv;
return ipaddr_list_or_flush(argv, 0);
}
+static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
+{
+ static const char keywords[] ALIGN1 =
+ "id\0"
+ "protocol\0"
+ "reorder_hdr\0"
+ "gvrp\0"
+ "mvrp\0"
+ "loose_binding\0"
+ ;
+ static const char protocols[] ALIGN1 =
+ "802.1q\0"
+ "802.1ad\0"
+ ;
+ static const char str_on_off[] ALIGN1 =
+ "on\0"
+ "off\0"
+ ;
+ enum {
+ ARG_id = 0,
+ ARG_reorder_hdr,
+ ARG_gvrp,
+ ARG_mvrp,
+ ARG_loose_binding,
+ ARG_protocol,
+ };
+ enum {
+ PROTO_8021Q = 0,
+ PROTO_8021AD,
+ };
+ enum {
+ PARM_on = 0,
+ PARM_off
+ };
+ int arg;
+ uint16_t id, proto;
+ struct ifla_vlan_flags flags = {};
+
+ while (*argv) {
+ arg = index_in_substrings(keywords, *argv);
+ if (arg < 0)
+ invarg(*argv, "type vlan");
+
+ NEXT_ARG();
+ if (arg == ARG_id) {
+ id = get_u16(*argv, "id");
+ addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
+ } else if (arg == ARG_protocol) {
+ arg = index_in_substrings(protocols, *argv);
+ if (arg == PROTO_8021Q)
+ proto = ETH_P_8021Q;
+ else if (arg == PROTO_8021AD)
+ proto = ETH_P_8021AD;
+ else
+ bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
+ *argv);
+ addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
+ } else {
+ int param = index_in_strings(str_on_off, *argv);
+ if (param < 0)
+ die_must_be_on_off(nth_string(keywords, arg));
+
+ if (arg == ARG_reorder_hdr) {
+ flags.mask |= VLAN_FLAG_REORDER_HDR;
+ flags.flags &= ~VLAN_FLAG_REORDER_HDR;
+ if (param == PARM_on)
+ flags.flags |= VLAN_FLAG_REORDER_HDR;
+ } else if (arg == ARG_gvrp) {
+ flags.mask |= VLAN_FLAG_GVRP;
+ flags.flags &= ~VLAN_FLAG_GVRP;
+ if (param == PARM_on)
+ flags.flags |= VLAN_FLAG_GVRP;
+ } else if (arg == ARG_mvrp) {
+ flags.mask |= VLAN_FLAG_MVRP;
+ flags.flags &= ~VLAN_FLAG_MVRP;
+ if (param == PARM_on)
+ flags.flags |= VLAN_FLAG_MVRP;
+ } else { /*if (arg == ARG_loose_binding) */
+ flags.mask |= VLAN_FLAG_LOOSE_BINDING;
+ flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
+ if (param == PARM_on)
+ flags.flags |= VLAN_FLAG_LOOSE_BINDING;
+ }
+ }
+ argv++;
+ }
+
+ if (flags.mask)
+ addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
+}
+
#ifndef NLMSG_TAIL
#define NLMSG_TAIL(nmsg) \
((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
#endif
/* Return value becomes exitcode. It's okay to not return at all */
-static int do_change(char **argv, const unsigned rtm)
+static int do_add_or_delete(char **argv, const unsigned rtm)
{
static const char keywords[] ALIGN1 =
- "link\0""name\0""type\0""dev\0";
+ "link\0""name\0""type\0""dev\0""address\0";
enum {
ARG_link,
ARG_name,
ARG_type,
ARG_dev,
+ ARG_address,
};
struct rtnl_handle rth;
struct {
- struct nlmsghdr n;
- struct ifinfomsg i;
- char buf[1024];
+ struct nlmsghdr n;
+ struct ifinfomsg i;
+ char buf[1024];
} req;
smalluint arg;
- char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL;
+ char *name_str = NULL;
+ char *link_str = NULL;
+ char *type_str = NULL;
+ char *dev_str = NULL;
+ char *address_str = NULL;
memset(&req, 0, sizeof(req));
while (*argv) {
arg = index_in_substrings(keywords, *argv);
+ if (arg == ARG_type) {
+ NEXT_ARG();
+ type_str = *argv++;
+ dbg("type_str:'%s'", type_str);
+ break;
+ }
if (arg == ARG_link) {
NEXT_ARG();
link_str = *argv;
+ dbg("link_str:'%s'", link_str);
} else if (arg == ARG_name) {
NEXT_ARG();
name_str = *argv;
- } else if (arg == ARG_type) {
+ dbg("name_str:'%s'", name_str);
+ } else if (arg == ARG_address) {
NEXT_ARG();
- type_str = *argv;
+ address_str = *argv;
+ dbg("address_str:'%s'", name_str);
} else {
if (arg == ARG_dev) {
if (dev_str)
NEXT_ARG();
}
dev_str = *argv;
+ dbg("dev_str:'%s'", dev_str);
}
argv++;
}
addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
strlen(type_str));
+
+ if (*argv) {
+ struct rtattr *data = NLMSG_TAIL(&req.n);
+ addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
+
+ if (strcmp(type_str, "vlan") == 0)
+ vlan_parse_opt(argv, &req.n, sizeof(req));
+
+ data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
+ }
+
linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
}
if (rtm != RTM_NEWLINK) {
int idx = xll_name_to_index(link_str);
addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
}
+ if (address_str) {
+ unsigned char abuf[32];
+ int len = ll_addr_a2n(abuf, sizeof(abuf), address_str);
+ dbg("address len:%d", len);
+ if (len < 0)
+ return -1;
+ addattr_l(&req.n, sizeof(req), IFLA_ADDRESS, abuf, len);
+ }
}
if (name_str) {
const size_t name_len = strlen(name_str) + 1;
return 0;
}
+/* Other keywords recognized by iproute2-3.12.0: */
+#if 0
+ } else if (matches(*argv, "broadcast") == 0 ||
+ strcmp(*argv, "brd") == 0) {
+ NEXT_ARG();
+ len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
+ if (len < 0)
+ return -1;
+ addattr_l(&req->n, sizeof(*req), IFLA_BROADCAST, abuf, len);
+ } else if (matches(*argv, "txqueuelen") == 0 ||
+ strcmp(*argv, "qlen") == 0 ||
+ matches(*argv, "txqlen") == 0) {
+ NEXT_ARG();
+ if (qlen != -1)
+ duparg("txqueuelen", *argv);
+ if (get_integer(&qlen, *argv, 0))
+ invarg("Invalid \"txqueuelen\" value\n", *argv);
+ addattr_l(&req->n, sizeof(*req), IFLA_TXQLEN, &qlen, 4);
+ } else if (strcmp(*argv, "mtu") == 0) {
+ NEXT_ARG();
+ if (mtu != -1)
+ duparg("mtu", *argv);
+ if (get_integer(&mtu, *argv, 0))
+ invarg("Invalid \"mtu\" value\n", *argv);
+ addattr_l(&req->n, sizeof(*req), IFLA_MTU, &mtu, 4);
+ } else if (strcmp(*argv, "netns") == 0) {
+ NEXT_ARG();
+ if (netns != -1)
+ duparg("netns", *argv);
+ if ((netns = get_netns_fd(*argv)) >= 0)
+ addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, &netns, 4);
+ else if (get_integer(&netns, *argv, 0) == 0)
+ addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_PID, &netns, 4);
+ else
+ invarg("Invalid \"netns\" value\n", *argv);
+ } else if (strcmp(*argv, "multicast") == 0) {
+ NEXT_ARG();
+ req->i.ifi_change |= IFF_MULTICAST;
+ if (strcmp(*argv, "on") == 0) {
+ req->i.ifi_flags |= IFF_MULTICAST;
+ } else if (strcmp(*argv, "off") == 0) {
+ req->i.ifi_flags &= ~IFF_MULTICAST;
+ } else
+ return on_off("multicast", *argv);
+ } else if (strcmp(*argv, "allmulticast") == 0) {
+ NEXT_ARG();
+ req->i.ifi_change |= IFF_ALLMULTI;
+ if (strcmp(*argv, "on") == 0) {
+ req->i.ifi_flags |= IFF_ALLMULTI;
+ } else if (strcmp(*argv, "off") == 0) {
+ req->i.ifi_flags &= ~IFF_ALLMULTI;
+ } else
+ return on_off("allmulticast", *argv);
+ } else if (strcmp(*argv, "promisc") == 0) {
+ NEXT_ARG();
+ req->i.ifi_change |= IFF_PROMISC;
+ if (strcmp(*argv, "on") == 0) {
+ req->i.ifi_flags |= IFF_PROMISC;
+ } else if (strcmp(*argv, "off") == 0) {
+ req->i.ifi_flags &= ~IFF_PROMISC;
+ } else
+ return on_off("promisc", *argv);
+ } else if (strcmp(*argv, "trailers") == 0) {
+ NEXT_ARG();
+ req->i.ifi_change |= IFF_NOTRAILERS;
+ if (strcmp(*argv, "off") == 0) {
+ req->i.ifi_flags |= IFF_NOTRAILERS;
+ } else if (strcmp(*argv, "on") == 0) {
+ req->i.ifi_flags &= ~IFF_NOTRAILERS;
+ } else
+ return on_off("trailers", *argv);
+ } else if (strcmp(*argv, "arp") == 0) {
+ NEXT_ARG();
+ req->i.ifi_change |= IFF_NOARP;
+ if (strcmp(*argv, "on") == 0) {
+ req->i.ifi_flags &= ~IFF_NOARP;
+ } else if (strcmp(*argv, "off") == 0) {
+ req->i.ifi_flags |= IFF_NOARP;
+ } else
+ return on_off("noarp", *argv);
+ } else if (strcmp(*argv, "vf") == 0) {
+ struct rtattr *vflist;
+ NEXT_ARG();
+ if (get_integer(&vf, *argv, 0)) {
+ invarg("Invalid \"vf\" value\n", *argv);
+ }
+ vflist = addattr_nest(&req->n, sizeof(*req),
+ IFLA_VFINFO_LIST);
+ len = iplink_parse_vf(vf, &argc, &argv, req);
+ if (len < 0)
+ return -1;
+ addattr_nest_end(&req->n, vflist);
+ } else if (matches(*argv, "master") == 0) {
+ int ifindex;
+ NEXT_ARG();
+ ifindex = ll_name_to_index(*argv);
+ if (!ifindex)
+ invarg("Device does not exist\n", *argv);
+ addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
+ &ifindex, 4);
+ } else if (matches(*argv, "nomaster") == 0) {
+ int ifindex = 0;
+ addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
+ &ifindex, 4);
+ } else if (matches(*argv, "dynamic") == 0) {
+ NEXT_ARG();
+ req->i.ifi_change |= IFF_DYNAMIC;
+ if (strcmp(*argv, "on") == 0) {
+ req->i.ifi_flags |= IFF_DYNAMIC;
+ } else if (strcmp(*argv, "off") == 0) {
+ req->i.ifi_flags &= ~IFF_DYNAMIC;
+ } else
+ return on_off("dynamic", *argv);
+ } else if (matches(*argv, "alias") == 0) {
+ NEXT_ARG();
+ addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS,
+ *argv, strlen(*argv));
+ argc--; argv++;
+ break;
+ } else if (strcmp(*argv, "group") == 0) {
+ NEXT_ARG();
+ if (*group != -1)
+ duparg("group", *argv);
+ if (rtnl_group_a2n(group, *argv))
+ invarg("Invalid \"group\" value\n", *argv);
+ } else if (strcmp(*argv, "mode") == 0) {
+ int mode;
+ NEXT_ARG();
+ mode = get_link_mode(*argv);
+ if (mode < 0)
+ invarg("Invalid link mode\n", *argv);
+ addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode);
+ } else if (strcmp(*argv, "state") == 0) {
+ int state;
+ NEXT_ARG();
+ state = get_operstate(*argv);
+ if (state < 0)
+ invarg("Invalid operstate\n", *argv);
+
+ addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state);
+ } else if (matches(*argv, "numtxqueues") == 0) {
+ NEXT_ARG();
+ if (numtxqueues != -1)
+ duparg("numtxqueues", *argv);
+ if (get_integer(&numtxqueues, *argv, 0))
+ invarg("Invalid \"numtxqueues\" value\n", *argv);
+ addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES,
+ &numtxqueues, 4);
+ } else if (matches(*argv, "numrxqueues") == 0) {
+ NEXT_ARG();
+ if (numrxqueues != -1)
+ duparg("numrxqueues", *argv);
+ if (get_integer(&numrxqueues, *argv, 0))
+ invarg("Invalid \"numrxqueues\" value\n", *argv);
+ addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES,
+ &numrxqueues, 4);
+ }
+#endif
+
/* Return value becomes exitcode. It's okay to not return at all */
-int do_iplink(char **argv)
+int FAST_FUNC do_iplink(char **argv)
{
static const char keywords[] ALIGN1 =
"add\0""delete\0""set\0""show\0""lst\0""list\0";
if (*argv) {
- smalluint key = index_in_substrings(keywords, *argv);
- if (key > 5) /* invalid argument */
- bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
+ int key = index_in_substrings(keywords, *argv);
+ if (key < 0) /* invalid argument */
+ invarg(*argv, applet_name);
argv++;
if (key <= 1) /* add/delete */
- return do_change(argv, key ? RTM_DELLINK : RTM_NEWLINK);
- else if (key == 2) /* set */
+ return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
+ if (key == 2) /* set */
return do_set(argv);
}
/* show, lst, list */