1 /* vi: set sw=4 ts=4: */
3 * vconfig implementation for busybox
5 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* BB_AUDIT SUSv3 N/A */
28 #include <sys/ioctl.h>
34 /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
35 enum vlan_ioctl_cmds {
38 SET_VLAN_INGRESS_PRIORITY_CMD,
39 SET_VLAN_EGRESS_PRIORITY_CMD,
40 GET_VLAN_INGRESS_PRIORITY_CMD,
41 GET_VLAN_EGRESS_PRIORITY_CMD,
42 SET_VLAN_NAME_TYPE_CMD,
45 enum vlan_name_types {
46 VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
47 VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
48 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
49 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
50 VLAN_NAME_TYPE_HIGHEST
53 struct vlan_ioctl_args {
54 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
60 unsigned int skb_priority;
61 unsigned int name_type;
62 unsigned int bind_type;
63 unsigned int flag; /* Matches vlan_dev_info flags */
69 #define VLAN_GROUP_ARRAY_LEN 4096
70 #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
72 /* On entry, table points to the length of the current string plus
73 * nul terminator plus data length for the subsequent entry. The
74 * return value is the last data entry for the matching string. */
75 static const char *xfind_str(const char *table, const char *str)
77 while (strcasecmp(str, table+1) != 0) {
78 if (!*(table += table[0])) {
85 static const char cmds[] = {
90 3, SET_VLAN_NAME_TYPE_CMD, 17,
92 'n', 'a', 'm', 'e', '_',
93 't', 'y', 'p', 'e', 0,
94 4, SET_VLAN_FLAG_CMD, 12,
96 'f', 'l', 'a', 'g', 0,
97 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
99 'e', 'g', 'r', 'e', 's', 's', '_',
101 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
103 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
107 static const char name_types[] = {
108 VLAN_NAME_TYPE_PLUS_VID, 16,
110 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
112 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
114 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
115 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
116 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
118 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
120 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
122 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
123 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
126 static const char conf_file_name[] = "/proc/net/vlan/config";
128 int vconfig_main(int argc, char **argv)
130 struct vlan_ioctl_args ifr;
138 /* Don't bother closing the filedes. It will be closed on cleanup. */
139 if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */
140 bb_perror_msg_and_die("open %s", conf_file_name);
143 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
146 p = xfind_str(cmds+2, *argv);
152 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
153 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
155 if (strlen(argv[1]) >= IF_NAMESIZE) {
156 bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
158 strcpy(ifr.device1, argv[1]);
161 /* I suppose one could try to combine some of the function calls below,
162 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
163 * (unsigned) int members of a unions. But because of the range checking,
164 * doing so wouldn't save that much space and would also make maintainence
166 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
167 ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1);
168 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
169 ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1);
170 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
171 ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX);
172 ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7);
176 if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
177 || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
179 bb_perror_msg_and_die("socket or ioctl error for %s", *argv);