1 | /* 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 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
8 | */
|
---|
9 |
|
---|
10 | /* BB_AUDIT SUSv3 N/A */
|
---|
11 |
|
---|
12 | #include <stdlib.h>
|
---|
13 | #include <unistd.h>
|
---|
14 | #include <fcntl.h>
|
---|
15 | #include <sys/ioctl.h>
|
---|
16 | #include <net/if.h>
|
---|
17 | #include <string.h>
|
---|
18 | #include <limits.h>
|
---|
19 | #include "busybox.h"
|
---|
20 |
|
---|
21 | /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
|
---|
22 | enum 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 | };
|
---|
32 | enum 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 |
|
---|
40 | struct vlan_ioctl_args {
|
---|
41 | int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
|
---|
42 | char device1[24];
|
---|
43 |
|
---|
44 | union {
|
---|
45 | 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 */
|
---|
51 | } u;
|
---|
52 |
|
---|
53 | short vlan_qos;
|
---|
54 | };
|
---|
55 |
|
---|
56 | #define VLAN_GROUP_ARRAY_LEN 4096
|
---|
57 | #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
|
---|
58 |
|
---|
59 | /* On entry, table points to the length of the current string plus
|
---|
60 | * nul terminator plus data length for the subsequent entry. The
|
---|
61 | * return value is the last data entry for the matching string. */
|
---|
62 | static const char *xfind_str(const char *table, const char *str)
|
---|
63 | {
|
---|
64 | while (strcasecmp(str, table+1) != 0) {
|
---|
65 | if (!*(table += table[0])) {
|
---|
66 | bb_show_usage();
|
---|
67 | }
|
---|
68 | }
|
---|
69 | return table - 1;
|
---|
70 | }
|
---|
71 |
|
---|
72 | static 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 | };
|
---|
93 |
|
---|
94 | static 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,
|
---|
100 | 'V', 'L', 'A', 'N',
|
---|
101 | '_', '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 | };
|
---|
112 |
|
---|
113 | static const char conf_file_name[] = "/proc/net/vlan/config";
|
---|
114 |
|
---|
115 | int vconfig_main(int argc, char **argv)
|
---|
116 | {
|
---|
117 | struct vlan_ioctl_args ifr;
|
---|
118 | const char *p;
|
---|
119 | int fd;
|
---|
120 |
|
---|
121 | if (argc < 3) {
|
---|
122 | bb_show_usage();
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* Don't bother closing the filedes. It will be closed on cleanup. */
|
---|
126 | /* Will die if 802.1q is not present */
|
---|
127 | bb_xopen3(conf_file_name, O_RDONLY, 0);
|
---|
128 |
|
---|
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]) {
|
---|
135 | bb_show_usage();
|
---|
136 | }
|
---|
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) {
|
---|
142 | bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
|
---|
143 | }
|
---|
144 | strcpy(ifr.device1, argv[1]);
|
---|
145 | p = argv[2];
|
---|
146 |
|
---|
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 */
|
---|
153 | ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1);
|
---|
154 | } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
|
---|
155 | ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1);
|
---|
156 | } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
|
---|
157 | ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX);
|
---|
158 | ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | 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);
|
---|
165 | }
|
---|
166 |
|
---|
167 | return 0;
|
---|
168 | }
|
---|
169 |
|
---|