Rewrite by Manuel Novoa III, very compact implimentation.

This commit is contained in:
Glenn L McGrath 2003-02-08 23:11:19 +00:00
parent cb665093b8
commit b4f3d7f594

View File

@ -1,166 +1,140 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <strings.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_vlan.h> #include <linux/if_vlan.h>
#include <linux/sockios.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <limits.h>
#include <sys/types.h>
#include "busybox.h" #include "busybox.h"
#define VLAN_GROUP_ARRAY_LEN 4096
#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
/* This is rather specialized in that we're passing a 'char **' in
* order to avoid the pointer dereference multiple times in the
* actual calls below. */
static unsigned long xstrtoul10(char **str, unsigned long max_val)
{
char *endptr;
unsigned long r;
int vconfig_main(int argc, char** argv) { r = strtoul(str[2], &endptr, 10);
int fd; if ((r > max_val) || (*endptr != 0)) {
struct vlan_ioctl_args if_request; show_usage();
}
return r;
}
char* cmd = NULL; /* On entry, table points to the length of the current string plus
char* if_name = NULL; * nul terminator plus data lenght for the subsequent entry. The
unsigned int vid = 0; * return value is the last data entry for the matching string. */
unsigned int skb_priority; static const char *xfind_str(const char *table, const char *str)
unsigned short vlan_qos; {
unsigned int nm_type = VLAN_NAME_TYPE_PLUS_VID; while (strcasecmp(str, table+1) != 0) {
if (!*(table += table[0])) {
show_usage();
}
}
return table - 1;
}
char* conf_file_name = "/proc/net/vlan/config"; static const char cmds[] = {
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,
4, SET_VLAN_FLAG_CMD, 12,
'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,
};
memset(&if_request, 0, sizeof(struct vlan_ioctl_args)); static const char name_types[] = {
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,
'V', 'L', 'A', 'N',
'_', '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,
};
if ((argc < 3) || (argc > 5)) { static const char conf_file_name[] = "/proc/net/vlan/config";
error_msg_and_die("Expecting argc to be 3-5, inclusive. Was: %d\n",argc);
}
else {
cmd = argv[1];
if (strcasecmp(cmd, "set_name_type") == 0) { int vconfig_main(int argc, char **argv)
if (strcasecmp(argv[2], "VLAN_PLUS_VID") == 0) { {
nm_type = VLAN_NAME_TYPE_PLUS_VID; struct vlan_ioctl_args ifr;
} const char *p;
else if (strcasecmp(argv[2], "VLAN_PLUS_VID_NO_PAD") == 0) { int fd;
nm_type = VLAN_NAME_TYPE_PLUS_VID_NO_PAD;
}
else if (strcasecmp(argv[2], "DEV_PLUS_VID") == 0) {
nm_type = VLAN_NAME_TYPE_RAW_PLUS_VID;
}
else if (strcasecmp(argv[2], "DEV_PLUS_VID_NO_PAD") == 0) {
nm_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
}
else {
error_msg_and_die("Invalid name type.\n");
}
if_request.u.name_type = nm_type;
}
else {
if_name = argv[2];
if (strlen(if_name) > 15) {
error_msg_and_die("ERROR: if_name must be 15 characters or less.\n");
}
strcpy(if_request.device1, if_name);
}
if (argc == 4) { if (argc < 3) {
vid = atoi(argv[3]); show_usage();
if_request.u.VID = vid; }
}
if (argc == 5) { /* Don't bother closing the filedes. It will be closed on cleanup. */
skb_priority = atoi(argv[3]); if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */
vlan_qos = atoi(argv[4]); perror_msg_and_die("open %s", conf_file_name);
if_request.u.skb_priority = skb_priority; }
if_request.vlan_qos = vlan_qos;
}
}
// Open up the /proc/vlan/config memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
if ((fd = open(conf_file_name, O_RDONLY)) < 0) {
error_msg("WARNING: Could not open /proc/net/vlan/config. Maybe you need to load the 8021q module, or maybe you are not using PROCFS??\n");
}
else {
close(fd);
}
/* We use sockets now, instead of the file descriptor */ ++argv;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { p = xfind_str(cmds+2, *argv);
error_msg_and_die("FATAL: Couldn't open a socket..go figure!\n"); ifr.cmd = *p;
} if (argc != p[-1]) {
show_usage();
}
/* add */ if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
if (strcasecmp(cmd, "add") == 0) { ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
if_request.cmd = ADD_VLAN_CMD; } else {
if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { if (strlen(argv[1]) >= IF_NAMESIZE) {
error_msg("ERROR: trying to add VLAN #%u to IF -:%s:- error: %s\n", error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
vid, if_name, strerror(errno)); }
} strcpy(ifr.device1, argv[1]);
else {
printf("Added VLAN with VID == %u to IF -:%s:-\n", vid, if_name); /* I suppose one could try to combine some of the function calls below,
if (vid == 1) { * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
error_msg("WARNING: VLAN 1 does not work with many switches,\nconsider another number if you have problems.\n"); * (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 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
else if (strcasecmp(cmd, "rem") == 0) { ifr.u.flag = xstrtoul10(argv, 1);
if_request.cmd = DEL_VLAN_CMD; } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { ifr.u.VID = xstrtoul10(argv, VLAN_GROUP_ARRAY_LEN-1);
error_msg("ERROR: trying to remove VLAN -:%s:- error: %s\n", } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
if_name, strerror(errno)); ifr.u.skb_priority = xstrtoul10(argv, ULONG_MAX);
} ifr.vlan_qos = xstrtoul10(argv+1, 7);
else { }
printf("Removed VLAN -:%s:-\n", if_name); }
}
}//if if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
else if (strcasecmp(cmd, "set_egress_map") == 0) { || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
if_request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD; ) {
if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { perror_msg_and_die("socket or ioctl error for %s", *argv);
error_msg("ERROR: trying to set egress map on device -:%s:- error: %s\n", }
if_name, strerror(errno));
} return 0;
else { }
printf("Set egress mapping on device -:%s:- "
"Should be visible in /proc/net/vlan/%s\n",
if_name, if_name);
}
}
else if (strcasecmp(cmd, "set_ingress_map") == 0) {
if_request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
error_msg("ERROR: trying to set ingress map on device -:%s:- error: %s\n",
if_name, strerror(errno));
}
else {
printf("Set ingress mapping on device -:%s:- "
"Should be visible in /proc/net/vlan/%s\n",
if_name, if_name);
}
}
else if (strcasecmp(cmd, "set_flag") == 0) {
if_request.cmd = SET_VLAN_FLAG_CMD;
if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
error_msg("ERROR: trying to set flag on device -:%s:- error: %s\n",
if_name, strerror(errno));
}
else {
printf("Set flag on device -:%s:- "
"Should be visible in /proc/net/vlan/%s\n",
if_name, if_name);
}
}
else if (strcasecmp(cmd, "set_name_type") == 0) {
if_request.cmd = SET_VLAN_NAME_TYPE_CMD;
if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
error_msg("ERROR: trying to set name type for VLAN subsystem, error: %s\n",
strerror(errno));
}
else {
printf("Set name-type for VLAN subsystem."
" Should be visible in /proc/net/vlan/config\n");
}
}
else {
error_msg_and_die("Unknown command -:%s:-\n", cmd);
}
return 0;
}/* main */