[BusyBox] vlanconfig - alternative to vconfig applet

Nick Fedchik fnm at fusion.ukrsat.com
Sun Jan 26 08:49:04 UTC 2003


On Sat, 25 Jan 2003, Nick Fedchik wrote:

...
> so much more changes and have made a decision to make new
> applet - vlanconfig.
> The patch attached.
Sorry, that previous patch was very buggy! I was very tired, so
made a lot of mistakes. I hope it was'nt critical to anyone.
Here the new patch instead of previous.
Also this version has heavy redesigned, so I don't made a patch
over previous patch, but post completelly new patch.

Sorry again!

-- 
Nick Fedchik, FNM3-RIPE(UANIC)
Internet Dept./ISP UkrSat, Kiev, Ukraine
-------------- next part --------------
diff -urN busybox/networking/Config.in busybox-dirty/networking/Config.in
--- busybox/networking/Config.in	2003-01-23 07:59:32 +0200
+++ busybox-dirty/networking/Config.in	2003-01-25 17:16:35 +0200
@@ -382,6 +382,13 @@
 	help
 	  Creates, removes, and configures VLAN interfaces
 
+config CONFIG_VLANCONFIG
+	bool "vlanconfig"
+	default n
+	help
+	  Utility to add, delete or configure VLAN-aware interfaces.
+	  Alternative to vconfig.
+
 config CONFIG_WGET
 	bool "wget"
 	default n
diff -urN busybox/networking/Makefile.in busybox-dirty/networking/Makefile.in
--- busybox/networking/Makefile.in	2003-01-23 07:59:32 +0200
+++ busybox-dirty/networking/Makefile.in	2003-01-25 17:16:49 +0200
@@ -48,6 +48,7 @@
 NETWORKING-$(CONFIG_TFTP)		+= tftp.o
 NETWORKING-$(CONFIG_TRACEROUTE)	+= traceroute.o
 NETWORKING-$(CONFIG_VCONFIG)		+= vconfig.o
+NETWORKING-$(CONFIG_VLANCONFIG)		+= vlanconfig.o
 NETWORKING-$(CONFIG_WGET)		+= wget.o
 
 libraries-y+=$(NETWORKING_DIR)$(NETWORKING_AR)
diff -urN busybox/networking/vlanconfig.c busybox-dirty/networking/vlanconfig.c
--- busybox/networking/vlanconfig.c	1970-01-01 03:00:00 +0300
+++ busybox-dirty/networking/vlanconfig.c	2003-01-26 16:13:09 +0200
@@ -0,0 +1,162 @@
+/*
+ * vlanconfig.c - Configure 802.1Q vlan-aware Interfaces for busybox.
+ *
+ * Busybox 2003 by Nick Fedchik <nick at fedchik.org.ua>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ * 02111-1307 USA
+ *
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <linux/if_vlan.h>
+#include <string.h>
+#include "busybox.h"
+
+#define VLAN_GROUP_ARRAY_LEN 4096
+#define SIOCSIFVLAN	0x8983	/* Set 802.1Q VLAN options  */
+
+int vlanconfig_main(int argc, char **argv)
+{
+	int fd;
+	struct vlan_ioctl_args ifr;
+	int cmd, set_opt;
+	char *endptr = NULL;
+	char *conf_file_name = "/proc/net/vlan/config";
+	char *int_len_msg = "Interface name must be less than";
+
+/*
+Usage: add      <iface-name> <vid>
+       del      <vlan-name>
+       set	nametype  {1|2|3|4}
+       set	flag	<vlan-name>
+       set	egress  <vlan-name>	<skb_priority>   <vlan_qos> 0-7
+       set	ingress <vlan-name>	<skb_priority>   <vlan_qos>
+
+*/
+
+	enum vlan_cmd_num { cmd_add, cmd_del, cmd_set };
+
+#define VLAN_CMD_HIGHEST 3
+	char *vlan_cmd[VLAN_CMD_HIGHEST] = {
+		"add",
+		"del",
+		"set"
+	};
+
+	enum vlan_cmd_set_opt_num { set_nametype, set_flag, set_egress,
+		set_ingress
+	};
+
+#define VLAN_CMD_SET_OPT_HIGHEST 4
+	char *vlan_cmd_set_opt[VLAN_CMD_SET_OPT_HIGHEST] = {
+		"nametype",
+		"flag",
+		"egress",
+		"ingress"
+	};
+
+	if ((argc < 3) || (argc > 6))
+		show_usage();
+
+	if ((fd = open(conf_file_name, O_RDONLY)) < 0)	// Open up the /proc/vlan/config
+		error_msg_and_die("open %s: %m\n", conf_file_name);
+	close(fd);
+
+	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)	/* We use sockets now, instead of the file descriptor */
+		error_msg_and_die("socket: %m\n");
+
+	memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
+
+	for (cmd = 0; cmd < VLAN_CMD_HIGHEST; cmd++)
+		if (!strcasecmp(vlan_cmd[cmd], argv[1]))
+			break;
+
+	if (cmd == VLAN_CMD_HIGHEST)
+		show_usage();
+
+	if (cmd == cmd_set) {
+
+		if (argc < 4)
+			show_usage();
+		for (set_opt = 0; set_opt < VLAN_CMD_SET_OPT_HIGHEST; set_opt++)
+			if (!strcasecmp(vlan_cmd_set_opt[set_opt], argv[2]))
+				break;
+
+		if (set_opt == VLAN_CMD_SET_OPT_HIGHEST)
+			show_usage();
+		if (set_opt == set_nametype) {
+			ifr.u.name_type = strtoul(argv[3], &endptr, 10);
+			if ((ifr.u.name_type > VLAN_NAME_TYPE_HIGHEST)
+				|| (*endptr != '\0'))
+				show_usage();
+			ifr.cmd = SET_VLAN_NAME_TYPE_CMD;
+		} else {
+			if (strlen(argv[3]) > IF_NAMESIZE)	// For those cases argv[3] is interface (physical or vlan-aware)
+				error_msg_and_die("%s %d\n", int_len_msg, IF_NAMESIZE);
+			strcpy(ifr.device1, argv[3]);
+
+			if (set_opt == set_flag) {
+/*
+				if (argc < 5)
+					show_usage();
+				ifr.u.flag = strtoul(argv[4], &endptr, 2);
+				if ((ifr.u.flag > 1) || (*endptr != '\0'))
+					show_usage();
+*/
+				ifr.u.flag = 1;
+				ifr.cmd = SET_VLAN_FLAG_CMD;
+			} else {
+				if (argc < 6)
+					show_usage();
+				ifr.u.skb_priority = strtoul(argv[4], &endptr, 10);
+				if ((ifr.u.skb_priority > 0xFFFF) || (*endptr != '\0'))
+					show_usage();
+				ifr.vlan_qos = strtoul(argv[5], &endptr, 8);
+				if ((ifr.vlan_qos > 7) || (*endptr != '\0'))
+					show_usage();
+				if (set_opt == set_egress)
+					ifr.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
+				if (set_opt == set_ingress)
+					ifr.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
+			}
+		}
+	} else {
+		// general check here, argv[2] is ifname    
+		if (strlen(argv[2]) > IF_NAMESIZE)
+			error_msg_and_die("%s %d\n", int_len_msg, IF_NAMESIZE);
+		strcpy(ifr.device1, argv[2]);
+		if (cmd == cmd_add) {
+			ifr.cmd = ADD_VLAN_CMD;
+			if (argc < 4)
+				show_usage();
+			ifr.u.VID = strtoul(argv[3], &endptr, 10);	// argv[3] is VID (0-4096)
+			if ((ifr.u.VID >= VLAN_GROUP_ARRAY_LEN) || (ifr.u.VID < 0)
+				|| (*endptr != '\0'))
+				show_usage();
+		}
+		if (cmd == cmd_del)
+			ifr.cmd = DEL_VLAN_CMD;
+	};
+	if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
+		error_msg_and_die("ioctl error when %s %s: %m\n", vlan_cmd[cmd],
+						  argv[2]);
+
+	return 0;
+}
diff -urN busybox/include/applets.h busybox-dirty/include/applets.h
--- busybox/include/applets.h	2003-01-23 07:59:31 +0200
+++ busybox-dirty/include/applets.h	2003-01-25 17:17:50 +0200
@@ -604,6 +604,9 @@
 #ifdef CONFIG_VI
 	APPLET(vi, vi_main, _BB_DIR_BIN, _BB_SUID_NEVER)
 #endif
+#ifdef CONFIG_VLANCONFIG
+	APPLET(vlanconfig, vlanconfig_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
+#endif
 #ifdef CONFIG_VLOCK
 	APPLET(vlock, vlock_main, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS)
 #endif
diff -urN busybox/include/usage.h busybox-dirty/include/usage.h
--- busybox/include/usage.h	2003-01-23 07:59:31 +0200
+++ busybox-dirty/include/usage.h	2003-01-26 16:53:09 +0200
@@ -2387,6 +2387,27 @@
 	"Options:\n" \
 	"\t-R\tRead-only- do not write to the file." 
 
+#define vlanconfig_trivial_usage \
+	"COMMAND [OPTIONS]"
+#define vlanconfig_full_usage \
+	"Add, delete or configure vlan-aware interface\n" \
+	"Options:\n" \
+	"\tadd\t<if-name>\t<0-4095>\n" \
+	"\t\tAdd new vlan-aware interface with specified vlan ID\n" \
+	"\tdel\t<if_vlan-name>\n" \
+	"\t\tDelete vlan-aware interface\n" \
+	"\tset\tnametype\t<1-4> Set vlan-aware interface name type\n" \
+	"\t\t\t0 - vlan plus vlan-id (vlan0005)\n" \
+	"\t\t\t1 - vlan plus vlan-id no pad (vlan5)\n" \
+	"\t\t\t2 - device plus vlan-id (eth0.0005)\n" \
+	"\t\t\t3 - device plus vlan-id no pad (eth0.5)\n" \
+	"\tset\tflag\t<if_vlan-name>\n" \
+	"\t\tSet flag to make vlan-aware interface look like real ethernet device\n" \
+	"\tset\tegress\t<if_vlan-name>\t<skb_priority>\t<0-7>\n" \
+	"\t\tSet egress mapping in vlan-aware interface\n" \
+	"\tset\tingress\t<if_vlan-name>\t<skb_priority>\t<0-7>\n" \
+	"\t\tSet ingress mapping in vlan-aware interface\n"
+
 #define vlock_trivial_usage \
 	"[OPTIONS]"
 #define vlock_full_usage \


More information about the busybox mailing list