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#include "toys.h"
27#include <linux/if_vlan.h>
28#include <linux/sockios.h>
29
30void vconfig_main(void)
31{
32 struct vlan_ioctl_args request;
33 char *cmd = *toys.optargs;
34 int fd = xsocket(AF_INET, SOCK_STREAM, 0);
35
36 memset(&request, 0, sizeof(struct vlan_ioctl_args));
37
38 if (!strcmp(cmd, "set_name_type")) {
39 char *types[] = {"VLAN_PLUS_VID", "DEV_PLUS_VID", "VLAN_PLUS_VID_NO_PAD",
40 "DEV_PLUS_VID_NO_PAD"};
41 int i, j = ARRAY_LEN(types);
42
43 for (i=0; i<j; i++) if (!strcmp(toys.optargs[1], types[i])) break;
44 if (i == j) {
45 for (i=0; i<j; i++) puts(types[i]);
46 error_exit("%s: unknown '%s'", cmd, toys.optargs[1]);
47 }
48
49 request.u.name_type = i;
50 request.cmd = SET_VLAN_NAME_TYPE_CMD;
51 xioctl(fd, SIOCSIFVLAN, &request);
52
53 return;
54 }
55
56
57 xstrncpy(request.device1, toys.optargs[1], sizeof(request.device1));
58
59 if (!strcmp(cmd, "add")) {
60 request.cmd = ADD_VLAN_CMD;
61 if (toys.optargs[2]) request.u.VID = atolx_range(toys.optargs[2], 0, 4094);
62 if (request.u.VID == 1)
63 xprintf("WARNING: VLAN 1 does not work with many switches.\n");
64 } else if (!strcmp(cmd, "rem")) request.cmd = DEL_VLAN_CMD;
65 else if (!strcmp(cmd, "set_flag")) {
66 request.cmd = SET_VLAN_FLAG_CMD;
67 if (toys.optargs[2]) request.u.flag = atolx_range(toys.optargs[2], 0, 1);
68 if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
69 } else if(strcmp(cmd, "set_egress_map") == 0) {
70 request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
71 if (toys.optargs[2])
72 request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
73 if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
74 } else if(strcmp(cmd, "set_ingress_map") == 0) {
75 request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
76 if (toys.optargs[2])
77 request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
78
79 if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
80 } else {
81 xclose(fd);
82 perror_exit("Unknown command %s", cmd);
83 }
84
85 xioctl(fd, SIOCSIFVLAN, &request);
86 xprintf("Successful %s on device %s\n", cmd, toys.optargs[1]);
87}
88