blob: 3bd9c3040e6f12c7ccbc2e19f6fd9c71b3f18aa3 [file] [log] [blame]
Eric Andersen853c4942003-01-23 05:59:32 +00001#include <stdlib.h>
2#include <unistd.h>
3#include <fcntl.h>
Eric Andersen853c4942003-01-23 05:59:32 +00004#include <sys/ioctl.h>
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +00005#include <net/if.h>
Eric Andersen853c4942003-01-23 05:59:32 +00006#include <linux/if_vlan.h>
Eric Andersen853c4942003-01-23 05:59:32 +00007#include <string.h>
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +00008#include <limits.h>
9#include "busybox.h"
Eric Andersen853c4942003-01-23 05:59:32 +000010
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000011#define VLAN_GROUP_ARRAY_LEN 4096
12#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
Eric Andersen853c4942003-01-23 05:59:32 +000013
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000014/* This is rather specialized in that we're passing a 'char **' in
15 * order to avoid the pointer dereference multiple times in the
16 * actual calls below. */
17static unsigned long xstrtoul10(char **str, unsigned long max_val)
18{
19 char *endptr;
20 unsigned long r;
Eric Andersen853c4942003-01-23 05:59:32 +000021
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000022 r = strtoul(str[2], &endptr, 10);
23 if ((r > max_val) || (*endptr != 0)) {
24 show_usage();
25 }
26 return r;
27}
Eric Andersen853c4942003-01-23 05:59:32 +000028
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000029/* On entry, table points to the length of the current string plus
30 * nul terminator plus data lenght for the subsequent entry. The
31 * return value is the last data entry for the matching string. */
32static const char *xfind_str(const char *table, const char *str)
33{
34 while (strcasecmp(str, table+1) != 0) {
35 if (!*(table += table[0])) {
36 show_usage();
37 }
38 }
39 return table - 1;
40}
Eric Andersen853c4942003-01-23 05:59:32 +000041
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000042static const char cmds[] = {
43 4, ADD_VLAN_CMD, 7,
44 'a', 'd', 'd', 0,
45 3, DEL_VLAN_CMD, 7,
46 'r', 'e', 'm', 0,
47 3, SET_VLAN_NAME_TYPE_CMD, 17,
48 's', 'e', 't', '_',
49 'n', 'a', 'm', 'e', '_',
50 't', 'y', 'p', 'e', 0,
51 4, SET_VLAN_FLAG_CMD, 12,
52 's', 'e', 't', '_',
53 'f', 'l', 'a', 'g', 0,
54 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
55 's', 'e', 't', '_',
56 'e', 'g', 'r', 'e', 's', 's', '_',
57 'm', 'a', 'p', 0,
58 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
59 's', 'e', 't', '_',
60 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
61 'm', 'a', 'p', 0,
62};
Eric Andersen853c4942003-01-23 05:59:32 +000063
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000064static const char name_types[] = {
65 VLAN_NAME_TYPE_PLUS_VID, 16,
66 'V', 'L', 'A', 'N',
67 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
68 0,
69 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
70 'V', 'L', 'A', 'N',
71 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
72 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
73 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
74 'D', 'E', 'V',
75 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
76 0,
77 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
78 'D', 'E', 'V',
79 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
80 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
81};
Eric Andersen853c4942003-01-23 05:59:32 +000082
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000083static const char conf_file_name[] = "/proc/net/vlan/config";
Eric Andersen853c4942003-01-23 05:59:32 +000084
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000085int vconfig_main(int argc, char **argv)
86{
87 struct vlan_ioctl_args ifr;
88 const char *p;
89 int fd;
Eric Andersen853c4942003-01-23 05:59:32 +000090
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000091 if (argc < 3) {
92 show_usage();
93 }
Eric Andersen853c4942003-01-23 05:59:32 +000094
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000095 /* Don't bother closing the filedes. It will be closed on cleanup. */
96 if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */
97 perror_msg_and_die("open %s", conf_file_name);
98 }
99
100 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
101
102 ++argv;
103 p = xfind_str(cmds+2, *argv);
104 ifr.cmd = *p;
105 if (argc != p[-1]) {
106 show_usage();
107 }
108
109 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
110 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
111 } else {
112 if (strlen(argv[1]) >= IF_NAMESIZE) {
113 error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
114 }
115 strcpy(ifr.device1, argv[1]);
116
117 /* I suppose one could try to combine some of the function calls below,
118 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
119 * (unsigned) int members of a unions. But because of the range checking,
120 * doing so wouldn't save that much space and would also make maintainence
121 * more of a pain. */
122 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
123 ifr.u.flag = xstrtoul10(argv, 1);
124 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
125 ifr.u.VID = xstrtoul10(argv, VLAN_GROUP_ARRAY_LEN-1);
126 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
127 ifr.u.skb_priority = xstrtoul10(argv, ULONG_MAX);
128 ifr.vlan_qos = xstrtoul10(argv+1, 7);
129 }
130 }
131
132 if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
133 || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
134 ) {
135 perror_msg_and_die("socket or ioctl error for %s", *argv);
136 }
137
138 return 0;
139}
140