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 source tree.
10 /* BB_AUDIT SUSv3 N/A */
12 //usage:#define vconfig_trivial_usage
13 //usage: "COMMAND [OPTIONS]"
14 //usage:#define vconfig_full_usage "\n\n"
15 //usage: "Create and remove virtual ethernet devices\n"
16 //usage: "\n add IFACE VLAN_ID"
17 //usage: "\n rem VLAN_NAME"
18 //usage: "\n set_flag IFACE 0|1 VLAN_QOS"
19 //usage: "\n set_egress_map VLAN_NAME SKB_PRIO VLAN_QOS"
20 //usage: "\n set_ingress_map VLAN_NAME SKB_PRIO VLAN_QOS"
21 //usage: "\n set_name_type NAME_TYPE"
26 /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
27 enum vlan_ioctl_cmds {
30 SET_VLAN_INGRESS_PRIORITY_CMD,
31 SET_VLAN_EGRESS_PRIORITY_CMD,
32 GET_VLAN_INGRESS_PRIORITY_CMD,
33 GET_VLAN_EGRESS_PRIORITY_CMD,
34 SET_VLAN_NAME_TYPE_CMD,
37 enum vlan_name_types {
38 VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
39 VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
40 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
41 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
42 VLAN_NAME_TYPE_HIGHEST
45 struct vlan_ioctl_args {
46 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
52 unsigned int skb_priority;
53 unsigned int name_type;
54 unsigned int bind_type;
55 unsigned int flag; /* Matches vlan_dev_info flags */
61 #define VLAN_GROUP_ARRAY_LEN 4096
62 #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
64 /* On entry, table points to the length of the current string
65 * plus NUL terminator plus data length for the subsequent entry.
66 * The return value is the last data entry for the matching string. */
67 static const char *xfind_str(const char *table, const char *str)
69 while (strcasecmp(str, table + 1) != 0) {
77 static const char cmds[] ALIGN1 = {
82 3, SET_VLAN_NAME_TYPE_CMD, 17,
83 's','e','t','_','n','a','m','e','_','t','y','p','e',0,
84 5, SET_VLAN_FLAG_CMD, 12,
85 's','e','t','_','f','l','a','g',0,
86 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
87 's','e','t','_','e','g','r','e','s','s','_','m','a','p',0,
88 5, SET_VLAN_INGRESS_PRIORITY_CMD, 0,
89 's','e','t','_','i','n','g','r','e','s','s','_','m','a','p',0,
92 static const char name_types[] ALIGN1 = {
93 VLAN_NAME_TYPE_PLUS_VID, 16,
94 'V','L','A','N','_','P','L','U','S','_','V','I','D',0,
95 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
96 'V','L','A','N','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0,
97 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
98 'D','E','V','_','P','L','U','S','_','V','I','D',0,
99 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 0,
100 'D','E','V','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0,
103 int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
104 int vconfig_main(int argc, char **argv)
106 struct vlan_ioctl_args ifr;
110 memset(&ifr, 0, sizeof(ifr));
115 p = xfind_str(cmds + 2, argv[0]);
120 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) {
122 ifr.u.name_type = *xfind_str(name_types + 1, argv[1]);
124 strncpy_IFNAMSIZ(ifr.device1, argv[1]);
127 /* I suppose one could try to combine some of the function calls below,
128 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
129 * (unsigned) int members of a unions. But because of the range checking,
130 * doing so wouldn't save that much space and would also make maintainence
133 if (ifr.cmd == SET_VLAN_FLAG_CMD) {
135 ifr.u.flag = xatou_range(p, 0, 1);
136 /* DM: in order to set reorder header, qos must be set */
137 ifr.vlan_qos = xatou_range(argv[3], 0, 7);
138 } else if (ifr.cmd == ADD_VLAN_CMD) {
140 ifr.u.VID = xatou_range(p, 0, VLAN_GROUP_ARRAY_LEN - 1);
141 } else if (ifr.cmd != DEL_VLAN_CMD) {
142 /* set_{egress|ingress}_map */
143 ifr.u.skb_priority = xatou(p);
144 ifr.vlan_qos = xatou_range(argv[3], 0, 7);
148 fd = xsocket(AF_INET, SOCK_STREAM, 0);
149 ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
150 "ioctl error for %s", argv[0]);