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 #include <sys/ioctl.h>
21 /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
22 enum vlan_ioctl_cmds {
25 SET_VLAN_INGRESS_PRIORITY_CMD,
26 SET_VLAN_EGRESS_PRIORITY_CMD,
27 GET_VLAN_INGRESS_PRIORITY_CMD,
28 GET_VLAN_EGRESS_PRIORITY_CMD,
29 SET_VLAN_NAME_TYPE_CMD,
32 enum vlan_name_types {
33 VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
34 VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
35 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
36 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
37 VLAN_NAME_TYPE_HIGHEST
40 struct vlan_ioctl_args {
41 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
47 unsigned int skb_priority;
48 unsigned int name_type;
49 unsigned int bind_type;
50 unsigned int flag; /* Matches vlan_dev_info flags */
56 #define VLAN_GROUP_ARRAY_LEN 4096
57 #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
59 /* On entry, table points to the length of the current string plus
60 * nul terminator plus data length for the subsequent entry. The
61 * return value is the last data entry for the matching string. */
62 static const char *xfind_str(const char *table, const char *str)
64 while (strcasecmp(str, table+1) != 0) {
65 if (!*(table += table[0])) {
72 static const char cmds[] = {
77 3, SET_VLAN_NAME_TYPE_CMD, 17,
79 'n', 'a', 'm', 'e', '_',
80 't', 'y', 'p', 'e', 0,
81 4, SET_VLAN_FLAG_CMD, 12,
83 'f', 'l', 'a', 'g', 0,
84 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
86 'e', 'g', 'r', 'e', 's', 's', '_',
88 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
90 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
94 static const char name_types[] = {
95 VLAN_NAME_TYPE_PLUS_VID, 16,
97 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
99 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
101 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
102 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
103 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
105 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
107 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
109 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
110 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
113 static const char conf_file_name[] = "/proc/net/vlan/config";
115 int vconfig_main(int argc, char **argv)
117 struct vlan_ioctl_args ifr;
125 /* Don't bother closing the filedes. It will be closed on cleanup. */
126 /* Will die if 802.1q is not present */
127 bb_xopen3(conf_file_name, O_RDONLY, 0);
129 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
132 p = xfind_str(cmds+2, *argv);
138 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
139 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
141 if (strlen(argv[1]) >= IF_NAMESIZE) {
142 bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
144 strcpy(ifr.device1, argv[1]);
147 /* I suppose one could try to combine some of the function calls below,
148 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
149 * (unsigned) int members of a unions. But because of the range checking,
150 * doing so wouldn't save that much space and would also make maintainence
152 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
153 ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1);
154 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
155 ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1);
156 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
157 ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX);
158 ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7);
162 fd = bb_xsocket(AF_INET, SOCK_STREAM, 0);
163 if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) {
164 bb_perror_msg_and_die("ioctl error for %s", *argv);