blob: 6cbbb54ca13267071cad9f935699ad80ba7a5fc8 [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 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Manuel Novoa III cad53642003-03-19 09:13:01 +00008 */
9
10/* BB_AUDIT SUSv3 N/A */
11
Eric Andersen853c4942003-01-23 05:59:32 +000012#include <stdlib.h>
13#include <unistd.h>
14#include <fcntl.h>
Eric Andersen853c4942003-01-23 05:59:32 +000015#include <sys/ioctl.h>
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000016#include <net/if.h>
Eric Andersen853c4942003-01-23 05:59:32 +000017#include <string.h>
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000018#include <limits.h>
19#include "busybox.h"
Eric Andersen853c4942003-01-23 05:59:32 +000020
Eric Andersenb8d2cd42003-12-19 10:40:56 +000021/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
22enum vlan_ioctl_cmds {
23 ADD_VLAN_CMD,
24 DEL_VLAN_CMD,
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,
30 SET_VLAN_FLAG_CMD
31};
32enum 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
38};
39
40struct vlan_ioctl_args {
41 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
42 char device1[24];
43
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000044 union {
Eric Andersenb8d2cd42003-12-19 10:40:56 +000045 char device2[24];
46 int VID;
47 unsigned int skb_priority;
48 unsigned int name_type;
49 unsigned int bind_type;
50 unsigned int flag; /* Matches vlan_dev_info flags */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051 } u;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000052
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 short vlan_qos;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000054};
55
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000056#define VLAN_GROUP_ARRAY_LEN 4096
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
Eric Andersen853c4942003-01-23 05:59:32 +000058
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000059/* On entry, table points to the length of the current string plus
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 * nul terminator plus data length for the subsequent entry. The
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000061 * return value is the last data entry for the matching string. */
62static const char *xfind_str(const char *table, const char *str)
63{
64 while (strcasecmp(str, table+1) != 0) {
65 if (!*(table += table[0])) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000067 }
68 }
69 return table - 1;
70}
Eric Andersen853c4942003-01-23 05:59:32 +000071
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000072static const char cmds[] = {
73 4, ADD_VLAN_CMD, 7,
74 'a', 'd', 'd', 0,
75 3, DEL_VLAN_CMD, 7,
76 'r', 'e', 'm', 0,
77 3, SET_VLAN_NAME_TYPE_CMD, 17,
78 's', 'e', 't', '_',
79 'n', 'a', 'm', 'e', '_',
80 't', 'y', 'p', 'e', 0,
81 4, SET_VLAN_FLAG_CMD, 12,
82 's', 'e', 't', '_',
83 'f', 'l', 'a', 'g', 0,
84 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
85 's', 'e', 't', '_',
86 'e', 'g', 'r', 'e', 's', 's', '_',
87 'm', 'a', 'p', 0,
88 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
89 's', 'e', 't', '_',
90 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
91 'm', 'a', 'p', 0,
92};
Eric Andersen853c4942003-01-23 05:59:32 +000093
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000094static const char name_types[] = {
95 VLAN_NAME_TYPE_PLUS_VID, 16,
96 'V', 'L', 'A', 'N',
97 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
98 0,
99 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000100 'V', 'L', 'A', 'N',
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000101 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
102 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
103 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
104 'D', 'E', 'V',
105 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
106 0,
107 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
108 'D', 'E', 'V',
109 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
110 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
111};
Eric Andersen853c4942003-01-23 05:59:32 +0000112
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000113static const char conf_file_name[] = "/proc/net/vlan/config";
Eric Andersen853c4942003-01-23 05:59:32 +0000114
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000115int vconfig_main(int argc, char **argv)
116{
117 struct vlan_ioctl_args ifr;
118 const char *p;
119 int fd;
Eric Andersen853c4942003-01-23 05:59:32 +0000120
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000121 if (argc < 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000123 }
Eric Andersen853c4942003-01-23 05:59:32 +0000124
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000125 /* Don't bother closing the filedes. It will be closed on cleanup. */
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000126 /* Will die if 802.1q is not present */
127 bb_xopen3(conf_file_name, O_RDONLY, 0);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000128
129 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
130
131 ++argv;
132 p = xfind_str(cmds+2, *argv);
133 ifr.cmd = *p;
134 if (argc != p[-1]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000136 }
137
138 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
139 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
140 } else {
141 if (strlen(argv[1]) >= IF_NAMESIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000143 }
144 strcpy(ifr.device1, argv[1]);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 p = argv[2];
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000146
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
151 * more of a pain. */
152 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000153 ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000154 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000156 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157 ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX);
158 ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000159 }
160 }
161
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000162 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);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000165 }
166
167 return 0;
168}
169