aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-02-08 23:11:19 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-02-08 23:11:19 +0000
commitb4f3d7f5948f94203687031a3cb0b123104a6516 (patch)
tree2ed94f9fbcb8c47ec156d3501c7211671d1b4bca
parentcb665093b81a5547df3fdd6cd94cce2e05e5235e (diff)
downloadbusybox-w32-b4f3d7f5948f94203687031a3cb0b123104a6516.tar.gz
busybox-w32-b4f3d7f5948f94203687031a3cb0b123104a6516.tar.bz2
busybox-w32-b4f3d7f5948f94203687031a3cb0b123104a6516.zip
Rewrite by Manuel Novoa III, very compact implimentation.
-rw-r--r--networking/vconfig.c294
1 files changed, 134 insertions, 160 deletions
diff --git a/networking/vconfig.c b/networking/vconfig.c
index 0ad759e69..3bd9c3040 100644
--- a/networking/vconfig.c
+++ b/networking/vconfig.c
@@ -1,166 +1,140 @@
1#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h> 1#include <stdlib.h>
4#include <unistd.h> 2#include <unistd.h>
5#include <fcntl.h> 3#include <fcntl.h>
6#include <strings.h>
7#include <sys/ioctl.h> 4#include <sys/ioctl.h>
5#include <net/if.h>
8#include <linux/if_vlan.h> 6#include <linux/if_vlan.h>
9#include <linux/sockios.h>
10#include <string.h> 7#include <string.h>
11#include <sys/socket.h> 8#include <limits.h>
12#include <sys/types.h> 9#include "busybox.h"
13#include "busybox.h" 10
14 11#define VLAN_GROUP_ARRAY_LEN 4096
15 12#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
16 13
17int vconfig_main(int argc, char** argv) { 14/* This is rather specialized in that we're passing a 'char **' in
18 int fd; 15 * order to avoid the pointer dereference multiple times in the
19 struct vlan_ioctl_args if_request; 16 * actual calls below. */
20 17static unsigned long xstrtoul10(char **str, unsigned long max_val)
21 char* cmd = NULL; 18{
22 char* if_name = NULL; 19 char *endptr;
23 unsigned int vid = 0; 20 unsigned long r;
24 unsigned int skb_priority; 21
25 unsigned short vlan_qos; 22 r = strtoul(str[2], &endptr, 10);
26 unsigned int nm_type = VLAN_NAME_TYPE_PLUS_VID; 23 if ((r > max_val) || (*endptr != 0)) {
27 24 show_usage();
28 char* conf_file_name = "/proc/net/vlan/config"; 25 }
29 26 return r;
30 memset(&if_request, 0, sizeof(struct vlan_ioctl_args)); 27}
31 28
32 if ((argc < 3) || (argc > 5)) { 29/* On entry, table points to the length of the current string plus
33 error_msg_and_die("Expecting argc to be 3-5, inclusive. Was: %d\n",argc); 30 * nul terminator plus data lenght for the subsequent entry. The
34 } 31 * return value is the last data entry for the matching string. */
35 else { 32static const char *xfind_str(const char *table, const char *str)
36 cmd = argv[1]; 33{
37 34 while (strcasecmp(str, table+1) != 0) {
38 if (strcasecmp(cmd, "set_name_type") == 0) { 35 if (!*(table += table[0])) {
39 if (strcasecmp(argv[2], "VLAN_PLUS_VID") == 0) { 36 show_usage();
40 nm_type = VLAN_NAME_TYPE_PLUS_VID; 37 }
41 } 38 }
42 else if (strcasecmp(argv[2], "VLAN_PLUS_VID_NO_PAD") == 0) { 39 return table - 1;
43 nm_type = VLAN_NAME_TYPE_PLUS_VID_NO_PAD; 40}
44 } 41
45 else if (strcasecmp(argv[2], "DEV_PLUS_VID") == 0) { 42static const char cmds[] = {
46 nm_type = VLAN_NAME_TYPE_RAW_PLUS_VID; 43 4, ADD_VLAN_CMD, 7,
47 } 44 'a', 'd', 'd', 0,
48 else if (strcasecmp(argv[2], "DEV_PLUS_VID_NO_PAD") == 0) { 45 3, DEL_VLAN_CMD, 7,
49 nm_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD; 46 'r', 'e', 'm', 0,
50 } 47 3, SET_VLAN_NAME_TYPE_CMD, 17,
51 else { 48 's', 'e', 't', '_',
52 error_msg_and_die("Invalid name type.\n"); 49 'n', 'a', 'm', 'e', '_',
53 } 50 't', 'y', 'p', 'e', 0,
54 if_request.u.name_type = nm_type; 51 4, SET_VLAN_FLAG_CMD, 12,
55 } 52 's', 'e', 't', '_',
56 else { 53 'f', 'l', 'a', 'g', 0,
57 if_name = argv[2]; 54 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
58 if (strlen(if_name) > 15) { 55 's', 'e', 't', '_',
59 error_msg_and_die("ERROR: if_name must be 15 characters or less.\n"); 56 'e', 'g', 'r', 'e', 's', 's', '_',
60 } 57 'm', 'a', 'p', 0,
61 strcpy(if_request.device1, if_name); 58 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
62 } 59 's', 'e', 't', '_',
63 60 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
64 if (argc == 4) { 61 'm', 'a', 'p', 0,
65 vid = atoi(argv[3]); 62};
66 if_request.u.VID = vid; 63
67 } 64static const char name_types[] = {
68 65 VLAN_NAME_TYPE_PLUS_VID, 16,
69 if (argc == 5) { 66 'V', 'L', 'A', 'N',
70 skb_priority = atoi(argv[3]); 67 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
71 vlan_qos = atoi(argv[4]); 68 0,
72 if_request.u.skb_priority = skb_priority; 69 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
73 if_request.vlan_qos = vlan_qos; 70 'V', 'L', 'A', 'N',
74 } 71 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
75 } 72 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
76 73 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
77 // Open up the /proc/vlan/config 74 'D', 'E', 'V',
78 if ((fd = open(conf_file_name, O_RDONLY)) < 0) { 75 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
79 error_msg("WARNING: Could not open /proc/net/vlan/config. Maybe you need to load the 8021q module, or maybe you are not using PROCFS??\n"); 76 0,
80 } 77 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
81 else { 78 'D', 'E', 'V',
82 close(fd); 79 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
83 } 80 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
84 81};
85 /* We use sockets now, instead of the file descriptor */ 82
86 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 83static const char conf_file_name[] = "/proc/net/vlan/config";
87 error_msg_and_die("FATAL: Couldn't open a socket..go figure!\n"); 84
88 } 85int vconfig_main(int argc, char **argv)
89 86{
90 /* add */ 87 struct vlan_ioctl_args ifr;
91 if (strcasecmp(cmd, "add") == 0) { 88 const char *p;
92 if_request.cmd = ADD_VLAN_CMD; 89 int fd;
93 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { 90
94 error_msg("ERROR: trying to add VLAN #%u to IF -:%s:- error: %s\n", 91 if (argc < 3) {
95 vid, if_name, strerror(errno)); 92 show_usage();
96 } 93 }
97 else { 94
98 printf("Added VLAN with VID == %u to IF -:%s:-\n", vid, if_name); 95 /* Don't bother closing the filedes. It will be closed on cleanup. */
99 if (vid == 1) { 96 if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */
100 error_msg("WARNING: VLAN 1 does not work with many switches,\nconsider another number if you have problems.\n"); 97 perror_msg_and_die("open %s", conf_file_name);
101 } 98 }
102 } 99
103 }//if 100 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
104 else if (strcasecmp(cmd, "rem") == 0) { 101
105 if_request.cmd = DEL_VLAN_CMD; 102 ++argv;
106 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { 103 p = xfind_str(cmds+2, *argv);
107 error_msg("ERROR: trying to remove VLAN -:%s:- error: %s\n", 104 ifr.cmd = *p;
108 if_name, strerror(errno)); 105 if (argc != p[-1]) {
109 } 106 show_usage();
110 else { 107 }
111 printf("Removed VLAN -:%s:-\n", if_name); 108
112 } 109 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
113 }//if 110 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
114 else if (strcasecmp(cmd, "set_egress_map") == 0) { 111 } else {
115 if_request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD; 112 if (strlen(argv[1]) >= IF_NAMESIZE) {
116 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { 113 error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
117 error_msg("ERROR: trying to set egress map on device -:%s:- error: %s\n", 114 }
118 if_name, strerror(errno)); 115 strcpy(ifr.device1, argv[1]);
119 } 116
120 else { 117 /* I suppose one could try to combine some of the function calls below,
121 printf("Set egress mapping on device -:%s:- " 118 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
122 "Should be visible in /proc/net/vlan/%s\n", 119 * (unsigned) int members of a unions. But because of the range checking,
123 if_name, if_name); 120 * doing so wouldn't save that much space and would also make maintainence
124 } 121 * more of a pain. */
125 } 122 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
126 else if (strcasecmp(cmd, "set_ingress_map") == 0) { 123 ifr.u.flag = xstrtoul10(argv, 1);
127 if_request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD; 124 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
128 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { 125 ifr.u.VID = xstrtoul10(argv, VLAN_GROUP_ARRAY_LEN-1);
129 error_msg("ERROR: trying to set ingress map on device -:%s:- error: %s\n", 126 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
130 if_name, strerror(errno)); 127 ifr.u.skb_priority = xstrtoul10(argv, ULONG_MAX);
131 } 128 ifr.vlan_qos = xstrtoul10(argv+1, 7);
132 else { 129 }
133 printf("Set ingress mapping on device -:%s:- " 130 }
134 "Should be visible in /proc/net/vlan/%s\n", 131
135 if_name, if_name); 132 if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
136 } 133 || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
137 } 134 ) {
138 else if (strcasecmp(cmd, "set_flag") == 0) { 135 perror_msg_and_die("socket or ioctl error for %s", *argv);
139 if_request.cmd = SET_VLAN_FLAG_CMD; 136 }
140 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { 137
141 error_msg("ERROR: trying to set flag on device -:%s:- error: %s\n", 138 return 0;
142 if_name, strerror(errno)); 139}
143 } 140
144 else {
145 printf("Set flag on device -:%s:- "
146 "Should be visible in /proc/net/vlan/%s\n",
147 if_name, if_name);
148 }
149 }
150 else if (strcasecmp(cmd, "set_name_type") == 0) {
151 if_request.cmd = SET_VLAN_NAME_TYPE_CMD;
152 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
153 error_msg("ERROR: trying to set name type for VLAN subsystem, error: %s\n",
154 strerror(errno));
155 }
156 else {
157 printf("Set name-type for VLAN subsystem."
158 " Should be visible in /proc/net/vlan/config\n");
159 }
160 }
161 else {
162 error_msg_and_die("Unknown command -:%s:-\n", cmd);
163 }
164
165 return 0;
166}/* main */