2003-03-19 14:43:01 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* vconfig implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2003-03-19 14:43:01 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/* BB_AUDIT SUSv3 N/A */
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define vconfig_trivial_usage
|
|
|
|
//usage: "COMMAND [OPTIONS]"
|
|
|
|
//usage:#define vconfig_full_usage "\n\n"
|
|
|
|
//usage: "Create and remove virtual ethernet devices\n"
|
|
|
|
//usage: "\n add [interface-name] [vlan_id]"
|
|
|
|
//usage: "\n rem [vlan-name]"
|
|
|
|
//usage: "\n set_flag [interface-name] [flag-num] [0 | 1]"
|
|
|
|
//usage: "\n set_egress_map [vlan-name] [skb_priority] [vlan_qos]"
|
|
|
|
//usage: "\n set_ingress_map [vlan-name] [skb_priority] [vlan_qos]"
|
|
|
|
//usage: "\n set_name_type [name-type]"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-08-03 21:11:12 +05:30
|
|
|
#include <net/if.h>
|
2003-02-09 04:41:19 +05:30
|
|
|
|
2003-12-19 16:10:56 +05:30
|
|
|
/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
|
|
|
|
enum vlan_ioctl_cmds {
|
|
|
|
ADD_VLAN_CMD,
|
|
|
|
DEL_VLAN_CMD,
|
|
|
|
SET_VLAN_INGRESS_PRIORITY_CMD,
|
|
|
|
SET_VLAN_EGRESS_PRIORITY_CMD,
|
|
|
|
GET_VLAN_INGRESS_PRIORITY_CMD,
|
|
|
|
GET_VLAN_EGRESS_PRIORITY_CMD,
|
|
|
|
SET_VLAN_NAME_TYPE_CMD,
|
|
|
|
SET_VLAN_FLAG_CMD
|
|
|
|
};
|
|
|
|
enum vlan_name_types {
|
|
|
|
VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
|
|
|
|
VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
|
|
|
|
VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
|
|
|
|
VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
|
|
|
|
VLAN_NAME_TYPE_HIGHEST
|
|
|
|
};
|
|
|
|
|
|
|
|
struct vlan_ioctl_args {
|
|
|
|
int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
|
|
|
|
char device1[24];
|
|
|
|
|
2006-01-25 05:38:53 +05:30
|
|
|
union {
|
2003-12-19 16:10:56 +05:30
|
|
|
char device2[24];
|
|
|
|
int VID;
|
|
|
|
unsigned int skb_priority;
|
|
|
|
unsigned int name_type;
|
|
|
|
unsigned int bind_type;
|
|
|
|
unsigned int flag; /* Matches vlan_dev_info flags */
|
2006-01-25 05:38:53 +05:30
|
|
|
} u;
|
2003-12-19 16:10:56 +05:30
|
|
|
|
2004-03-15 13:59:22 +05:30
|
|
|
short vlan_qos;
|
2003-12-19 16:10:56 +05:30
|
|
|
};
|
|
|
|
|
2010-10-29 15:16:52 +05:30
|
|
|
#define VLAN_GROUP_ARRAY_LEN 4096
|
|
|
|
#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
|
2003-02-09 04:41:19 +05:30
|
|
|
|
2008-12-03 02:08:36 +05:30
|
|
|
/* On entry, table points to the length of the current string
|
|
|
|
* plus NUL terminator plus data length for the subsequent entry.
|
|
|
|
* The return value is the last data entry for the matching string. */
|
2003-02-09 04:41:19 +05:30
|
|
|
static const char *xfind_str(const char *table, const char *str)
|
|
|
|
{
|
|
|
|
while (strcasecmp(str, table+1) != 0) {
|
2008-12-03 02:08:36 +05:30
|
|
|
table += table[0];
|
|
|
|
if (!*table) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2003-02-09 04:41:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return table - 1;
|
|
|
|
}
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char cmds[] ALIGN1 = {
|
2003-02-09 04:41:19 +05:30
|
|
|
4, ADD_VLAN_CMD, 7,
|
|
|
|
'a', 'd', 'd', 0,
|
|
|
|
3, DEL_VLAN_CMD, 7,
|
|
|
|
'r', 'e', 'm', 0,
|
|
|
|
3, SET_VLAN_NAME_TYPE_CMD, 17,
|
|
|
|
's', 'e', 't', '_',
|
|
|
|
'n', 'a', 'm', 'e', '_',
|
|
|
|
't', 'y', 'p', 'e', 0,
|
2006-10-01 01:07:25 +05:30
|
|
|
5, SET_VLAN_FLAG_CMD, 12,
|
2003-02-09 04:41:19 +05:30
|
|
|
's', 'e', 't', '_',
|
|
|
|
'f', 'l', 'a', 'g', 0,
|
|
|
|
5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
|
|
|
|
's', 'e', 't', '_',
|
|
|
|
'e', 'g', 'r', 'e', 's', 's', '_',
|
|
|
|
'm', 'a', 'p', 0,
|
|
|
|
5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
|
|
|
|
's', 'e', 't', '_',
|
|
|
|
'i', 'n', 'g', 'r', 'e', 's', 's', '_',
|
|
|
|
'm', 'a', 'p', 0,
|
|
|
|
};
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char name_types[] ALIGN1 = {
|
2003-02-09 04:41:19 +05:30
|
|
|
VLAN_NAME_TYPE_PLUS_VID, 16,
|
|
|
|
'V', 'L', 'A', 'N',
|
|
|
|
'_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
|
|
|
|
0,
|
|
|
|
VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
|
2004-03-15 13:59:22 +05:30
|
|
|
'V', 'L', 'A', 'N',
|
2003-02-09 04:41:19 +05:30
|
|
|
'_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
|
|
|
|
'_', 'N', 'O', '_', 'P', 'A', 'D', 0,
|
|
|
|
VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
|
|
|
|
'D', 'E', 'V',
|
|
|
|
'_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
|
|
|
|
0,
|
|
|
|
VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
|
|
|
|
'D', 'E', 'V',
|
|
|
|
'_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
|
|
|
|
'_', 'N', 'O', '_', 'P', 'A', 'D', 0,
|
|
|
|
};
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char conf_file_name[] ALIGN1 = "/proc/net/vlan/config";
|
2003-02-09 04:41:19 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2003-02-09 04:41:19 +05:30
|
|
|
int vconfig_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct vlan_ioctl_args ifr;
|
|
|
|
const char *p;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (argc < 3) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2003-02-09 04:41:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't bother closing the filedes. It will be closed on cleanup. */
|
2006-04-13 18:15:04 +05:30
|
|
|
/* Will die if 802.1q is not present */
|
2006-09-11 21:31:40 +05:30
|
|
|
xopen(conf_file_name, O_RDONLY);
|
2003-02-09 04:41:19 +05:30
|
|
|
|
2008-12-03 02:08:36 +05:30
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2003-02-09 04:41:19 +05:30
|
|
|
|
|
|
|
++argv;
|
|
|
|
p = xfind_str(cmds+2, *argv);
|
|
|
|
ifr.cmd = *p;
|
|
|
|
if (argc != p[-1]) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2003-02-09 04:41:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
|
|
|
|
ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
|
|
|
|
} else {
|
2008-12-02 23:48:50 +05:30
|
|
|
strncpy_IFNAMSIZ(ifr.device1, argv[1]);
|
2003-03-19 14:43:01 +05:30
|
|
|
p = argv[2];
|
2003-02-09 04:41:19 +05:30
|
|
|
|
|
|
|
/* I suppose one could try to combine some of the function calls below,
|
|
|
|
* since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
|
|
|
|
* (unsigned) int members of a unions. But because of the range checking,
|
|
|
|
* doing so wouldn't save that much space and would also make maintainence
|
|
|
|
* more of a pain. */
|
|
|
|
if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
|
2006-10-08 18:19:22 +05:30
|
|
|
ifr.u.flag = xatoul_range(p, 0, 1);
|
2006-10-01 01:07:25 +05:30
|
|
|
/* DM: in order to set reorder header, qos must be set */
|
2006-10-08 18:19:22 +05:30
|
|
|
ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
|
2003-02-09 04:41:19 +05:30
|
|
|
} else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
|
2006-10-08 18:19:22 +05:30
|
|
|
ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
|
2003-02-09 04:41:19 +05:30
|
|
|
} else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
|
2006-10-08 18:19:22 +05:30
|
|
|
ifr.u.skb_priority = xatou(p);
|
|
|
|
ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
|
2003-02-09 04:41:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-03 21:11:12 +05:30
|
|
|
fd = xsocket(AF_INET, SOCK_STREAM, 0);
|
2007-07-15 03:37:14 +05:30
|
|
|
ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
|
|
|
|
"ioctl error for %s", *argv);
|
2003-02-09 04:41:19 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|