blob: 36458f784b81671c5bd36a2cac7f2ccbd3ea3aa4 [file] [log] [blame]
Manuel Novoa III cad53642003-03-19 09:13:01 +00001/* vi: set sw=4 ts=4: */
2/*
3 * vconfig implementation for busybox
4 *
5 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
6 *
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.
11 *
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.
16 *
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
20 *
21 */
22
23/* BB_AUDIT SUSv3 N/A */
24
Eric Andersen853c4942003-01-23 05:59:32 +000025#include <stdlib.h>
26#include <unistd.h>
27#include <fcntl.h>
Eric Andersen853c4942003-01-23 05:59:32 +000028#include <sys/ioctl.h>
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000029#include <net/if.h>
Eric Andersen853c4942003-01-23 05:59:32 +000030#include <string.h>
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000031#include <limits.h>
32#include "busybox.h"
Eric Andersen853c4942003-01-23 05:59:32 +000033
Eric Andersenb8d2cd42003-12-19 10:40:56 +000034/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
35enum vlan_ioctl_cmds {
36 ADD_VLAN_CMD,
37 DEL_VLAN_CMD,
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,
43 SET_VLAN_FLAG_CMD
44};
45enum 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
51};
52
53struct vlan_ioctl_args {
54 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
55 char device1[24];
56
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057 union {
Eric Andersenb8d2cd42003-12-19 10:40:56 +000058 char device2[24];
59 int VID;
60 unsigned int skb_priority;
61 unsigned int name_type;
62 unsigned int bind_type;
63 unsigned int flag; /* Matches vlan_dev_info flags */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000064 } u;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000065
Eric Andersenc7bda1c2004-03-15 08:29:22 +000066 short vlan_qos;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000067};
68
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000069#define VLAN_GROUP_ARRAY_LEN 4096
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000070#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
Eric Andersen853c4942003-01-23 05:59:32 +000071
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000072/* On entry, table points to the length of the current string plus
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 * nul terminator plus data length for the subsequent entry. The
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000074 * return value is the last data entry for the matching string. */
75static const char *xfind_str(const char *table, const char *str)
76{
77 while (strcasecmp(str, table+1) != 0) {
78 if (!*(table += table[0])) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000080 }
81 }
82 return table - 1;
83}
Eric Andersen853c4942003-01-23 05:59:32 +000084
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000085static const char cmds[] = {
86 4, ADD_VLAN_CMD, 7,
87 'a', 'd', 'd', 0,
88 3, DEL_VLAN_CMD, 7,
89 'r', 'e', 'm', 0,
90 3, SET_VLAN_NAME_TYPE_CMD, 17,
91 's', 'e', 't', '_',
92 'n', 'a', 'm', 'e', '_',
93 't', 'y', 'p', 'e', 0,
94 4, SET_VLAN_FLAG_CMD, 12,
95 's', 'e', 't', '_',
96 'f', 'l', 'a', 'g', 0,
97 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
98 's', 'e', 't', '_',
99 'e', 'g', 'r', 'e', 's', 's', '_',
100 'm', 'a', 'p', 0,
101 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
102 's', 'e', 't', '_',
103 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
104 'm', 'a', 'p', 0,
105};
Eric Andersen853c4942003-01-23 05:59:32 +0000106
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000107static const char name_types[] = {
108 VLAN_NAME_TYPE_PLUS_VID, 16,
109 'V', 'L', 'A', 'N',
110 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
111 0,
112 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113 'V', 'L', 'A', 'N',
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000114 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
115 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
116 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
117 'D', 'E', 'V',
118 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
119 0,
120 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
121 'D', 'E', 'V',
122 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
123 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
124};
Eric Andersen853c4942003-01-23 05:59:32 +0000125
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000126static const char conf_file_name[] = "/proc/net/vlan/config";
Eric Andersen853c4942003-01-23 05:59:32 +0000127
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000128int vconfig_main(int argc, char **argv)
129{
130 struct vlan_ioctl_args ifr;
131 const char *p;
132 int fd;
Eric Andersen853c4942003-01-23 05:59:32 +0000133
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000134 if (argc < 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000136 }
Eric Andersen853c4942003-01-23 05:59:32 +0000137
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000138 /* 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? */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 bb_perror_msg_and_die("open %s", conf_file_name);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000141 }
142
143 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
144
145 ++argv;
146 p = xfind_str(cmds+2, *argv);
147 ifr.cmd = *p;
148 if (argc != p[-1]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000150 }
151
152 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
153 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
154 } else {
155 if (strlen(argv[1]) >= IF_NAMESIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000157 }
158 strcpy(ifr.device1, argv[1]);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 p = argv[2];
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000160
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
165 * more of a pain. */
166 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000168 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000170 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX);
172 ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000173 }
174 }
175
176 if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
177 || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
178 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 bb_perror_msg_and_die("socket or ioctl error for %s", *argv);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000180 }
181
182 return 0;
183}
184