1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "libbb.h"
32#include <net/if.h>
33
34
35
36
37enum vlan_ioctl_cmds {
38 ADD_VLAN_CMD,
39 DEL_VLAN_CMD,
40 SET_VLAN_INGRESS_PRIORITY_CMD,
41 SET_VLAN_EGRESS_PRIORITY_CMD,
42 GET_VLAN_INGRESS_PRIORITY_CMD,
43 GET_VLAN_EGRESS_PRIORITY_CMD,
44 SET_VLAN_NAME_TYPE_CMD,
45 SET_VLAN_FLAG_CMD
46};
47enum vlan_name_types {
48 VLAN_NAME_TYPE_PLUS_VID,
49 VLAN_NAME_TYPE_RAW_PLUS_VID,
50 VLAN_NAME_TYPE_PLUS_VID_NO_PAD,
51 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
52 VLAN_NAME_TYPE_HIGHEST
53};
54
55struct vlan_ioctl_args {
56 int cmd;
57 char device1[24];
58
59 union {
60 char device2[24];
61 int VID;
62 unsigned int skb_priority;
63 unsigned int name_type;
64 unsigned int bind_type;
65 unsigned int flag;
66 } u;
67
68 short vlan_qos;
69};
70
71#define VLAN_GROUP_ARRAY_LEN 4096
72#define SIOCSIFVLAN 0x8983
73
74
75
76
77static const char *xfind_str(const char *table, const char *str)
78{
79 while (strcasecmp(str, table + 1) != 0) {
80 if (!table[0])
81 bb_show_usage();
82 table += table[0];
83 }
84 return table - 1;
85}
86
87static const char cmds[] ALIGN1 = {
88 4, ADD_VLAN_CMD, 7,
89 'a','d','d',0,
90 3, DEL_VLAN_CMD, 7,
91 'r','e','m',0,
92 3, SET_VLAN_NAME_TYPE_CMD, 17,
93 's','e','t','_','n','a','m','e','_','t','y','p','e',0,
94 5, SET_VLAN_FLAG_CMD, 12,
95 's','e','t','_','f','l','a','g',0,
96 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
97 's','e','t','_','e','g','r','e','s','s','_','m','a','p',0,
98 5, SET_VLAN_INGRESS_PRIORITY_CMD, 0,
99 's','e','t','_','i','n','g','r','e','s','s','_','m','a','p',0,
100};
101
102static const char name_types[] ALIGN1 = {
103 VLAN_NAME_TYPE_PLUS_VID, 16,
104 'V','L','A','N','_','P','L','U','S','_','V','I','D',0,
105 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
106 'V','L','A','N','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0,
107 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
108 'D','E','V','_','P','L','U','S','_','V','I','D',0,
109 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 0,
110 'D','E','V','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0,
111};
112
113int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
114int vconfig_main(int argc, char **argv)
115{
116 struct vlan_ioctl_args ifr;
117 const char *p;
118 int fd;
119
120 memset(&ifr, 0, sizeof(ifr));
121
122 ++argv;
123 if (!argv[0])
124 bb_show_usage();
125 p = xfind_str(cmds + 2, argv[0]);
126 ifr.cmd = *p;
127 if (argc != p[-1])
128 bb_show_usage();
129
130 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) {
131
132 ifr.u.name_type = *xfind_str(name_types + 1, argv[1]);
133 } else {
134 strncpy_IFNAMSIZ(ifr.device1, argv[1]);
135 p = argv[2];
136
137
138
139
140
141
142
143 if (ifr.cmd == SET_VLAN_FLAG_CMD) {
144
145 ifr.u.flag = xatou_range(p, 0, 1);
146
147 ifr.vlan_qos = xatou_range(argv[3], 0, 7);
148 } else if (ifr.cmd == ADD_VLAN_CMD) {
149
150 ifr.u.VID = xatou_range(p, 0, VLAN_GROUP_ARRAY_LEN - 1);
151 } else if (ifr.cmd != DEL_VLAN_CMD) {
152
153 ifr.u.skb_priority = xatou(p);
154 ifr.vlan_qos = xatou_range(argv[3], 0, 7);
155 }
156 }
157
158 fd = xsocket(AF_INET, SOCK_STREAM, 0);
159 ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
160 "ioctl error for %s", argv[0]);
161
162 return 0;
163}
164