1 /* vi: set sw=4 ts=4: */
3 * vconfig implementation for busybox
5 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* BB_AUDIT SUSv3 N/A */
15 /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
16 enum vlan_ioctl_cmds {
19 SET_VLAN_INGRESS_PRIORITY_CMD,
20 SET_VLAN_EGRESS_PRIORITY_CMD,
21 GET_VLAN_INGRESS_PRIORITY_CMD,
22 GET_VLAN_EGRESS_PRIORITY_CMD,
23 SET_VLAN_NAME_TYPE_CMD,
26 enum vlan_name_types {
27 VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
28 VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
29 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
30 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
31 VLAN_NAME_TYPE_HIGHEST
34 struct vlan_ioctl_args {
35 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
41 unsigned int skb_priority;
42 unsigned int name_type;
43 unsigned int bind_type;
44 unsigned int flag; /* Matches vlan_dev_info flags */
50 #define VLAN_GROUP_ARRAY_LEN 4096
51 #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
53 /* On entry, table points to the length of the current string
54 * plus NUL terminator plus data length for the subsequent entry.
55 * The return value is the last data entry for the matching string. */
56 static const char *xfind_str(const char *table, const char *str)
58 while (strcasecmp(str, table+1) != 0) {
67 static const char cmds[] ALIGN1 = {
72 3, SET_VLAN_NAME_TYPE_CMD, 17,
74 'n', 'a', 'm', 'e', '_',
75 't', 'y', 'p', 'e', 0,
76 5, SET_VLAN_FLAG_CMD, 12,
78 'f', 'l', 'a', 'g', 0,
79 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
81 'e', 'g', 'r', 'e', 's', 's', '_',
83 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
85 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
89 static const char name_types[] ALIGN1 = {
90 VLAN_NAME_TYPE_PLUS_VID, 16,
92 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
94 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
96 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
97 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
98 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
100 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
102 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
104 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
105 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
108 static const char conf_file_name[] ALIGN1 = "/proc/net/vlan/config";
110 int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
111 int vconfig_main(int argc, char **argv)
113 struct vlan_ioctl_args ifr;
121 /* Don't bother closing the filedes. It will be closed on cleanup. */
122 /* Will die if 802.1q is not present */
123 xopen(conf_file_name, O_RDONLY);
125 memset(&ifr, 0, sizeof(ifr));
128 p = xfind_str(cmds+2, *argv);
134 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
135 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
137 strncpy_IFNAMSIZ(ifr.device1, argv[1]);
140 /* I suppose one could try to combine some of the function calls below,
141 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
142 * (unsigned) int members of a unions. But because of the range checking,
143 * doing so wouldn't save that much space and would also make maintainence
145 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
146 ifr.u.flag = xatoul_range(p, 0, 1);
147 /* DM: in order to set reorder header, qos must be set */
148 ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
149 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
150 ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
151 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
152 ifr.u.skb_priority = xatou(p);
153 ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
157 fd = xsocket(AF_INET, SOCK_STREAM, 0);
158 ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
159 "ioctl error for %s", *argv);