A patch from John F. Kelly to add in a utility for configuring
[oweals/busybox.git] / networking / vconfig.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <strings.h>
7 #include <sys/ioctl.h>
8 #include <linux/if_vlan.h>
9 #include <linux/sockios.h>
10 #include <string.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include "busybox.h" 
14
15
16
17 int vconfig_main(int argc, char** argv) {
18    int fd;
19    struct vlan_ioctl_args if_request;
20    
21    char* cmd = NULL;
22    char* if_name = NULL;
23    unsigned int vid = 0;
24    unsigned int skb_priority;
25    unsigned short vlan_qos;
26    unsigned int nm_type = VLAN_NAME_TYPE_PLUS_VID;
27
28    char* conf_file_name = "/proc/net/vlan/config";
29
30    memset(&if_request, 0, sizeof(struct vlan_ioctl_args));
31    
32    if ((argc < 3) || (argc > 5)) {
33       error_msg_and_die("Expecting argc to be 3-5, inclusive.  Was: %d\n",argc);
34    }
35    else {
36       cmd = argv[1];
37           
38       if (strcasecmp(cmd, "set_name_type") == 0) {
39          if (strcasecmp(argv[2], "VLAN_PLUS_VID") == 0) {
40             nm_type = VLAN_NAME_TYPE_PLUS_VID;
41          }
42          else if (strcasecmp(argv[2], "VLAN_PLUS_VID_NO_PAD") == 0) {
43             nm_type = VLAN_NAME_TYPE_PLUS_VID_NO_PAD;
44          }
45          else if (strcasecmp(argv[2], "DEV_PLUS_VID") == 0) {
46             nm_type = VLAN_NAME_TYPE_RAW_PLUS_VID;
47          }
48          else if (strcasecmp(argv[2], "DEV_PLUS_VID_NO_PAD") == 0) {
49             nm_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
50          }
51          else {
52             error_msg_and_die("Invalid name type.\n");
53          }
54          if_request.u.name_type = nm_type;
55       }
56       else {
57          if_name = argv[2];
58          if (strlen(if_name) > 15) {
59             error_msg_and_die("ERROR:  if_name must be 15 characters or less.\n");
60          }
61          strcpy(if_request.device1, if_name);
62       }
63
64       if (argc == 4) {
65          vid = atoi(argv[3]);
66          if_request.u.VID = vid;
67       }
68
69       if (argc == 5) {
70          skb_priority = atoi(argv[3]);
71          vlan_qos = atoi(argv[4]);
72          if_request.u.skb_priority = skb_priority;
73          if_request.vlan_qos = vlan_qos;
74       }
75    }
76    
77    // Open up the /proc/vlan/config
78    if ((fd = open(conf_file_name, O_RDONLY)) < 0) {
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");
80    }
81    else {
82       close(fd);
83    }
84
85    /* We use sockets now, instead of the file descriptor */
86    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
87       error_msg_and_die("FATAL:  Couldn't open a socket..go figure!\n");
88    }   
89
90    /* add */
91    if (strcasecmp(cmd, "add") == 0) {
92       if_request.cmd = ADD_VLAN_CMD;
93       if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
94          error_msg("ERROR: trying to add VLAN #%u to IF -:%s:-  error: %s\n",
95                     vid, if_name, strerror(errno));                 
96       }
97       else {
98          printf("Added VLAN with VID == %u to IF -:%s:-\n", vid, if_name);
99          if (vid == 1) {
100             error_msg("WARNING:  VLAN 1 does not work with many switches,\nconsider another number if you have problems.\n");
101          }
102       }
103    }//if
104    else if (strcasecmp(cmd, "rem") == 0) {
105       if_request.cmd = DEL_VLAN_CMD;
106       if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
107          error_msg("ERROR: trying to remove VLAN -:%s:- error: %s\n",
108                  if_name, strerror(errno));         
109       }
110       else {
111          printf("Removed VLAN -:%s:-\n", if_name);
112       }
113    }//if
114    else if (strcasecmp(cmd, "set_egress_map") == 0) {
115       if_request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
116       if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
117          error_msg("ERROR: trying to set egress map on device -:%s:- error: %s\n",
118                  if_name, strerror(errno));         
119       }
120       else {
121          printf("Set egress mapping on device -:%s:- "
122                  "Should be visible in /proc/net/vlan/%s\n",
123                  if_name, if_name);
124       }
125    }
126    else if (strcasecmp(cmd, "set_ingress_map") == 0) {
127       if_request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
128       if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
129          error_msg("ERROR: trying to set ingress map on device -:%s:- error: %s\n",
130                  if_name, strerror(errno));
131       }
132       else {
133          printf("Set ingress mapping on device -:%s:- "
134                  "Should be visible in /proc/net/vlan/%s\n",
135                  if_name, if_name);                
136       }
137    }   
138    else if (strcasecmp(cmd, "set_flag") == 0) {
139       if_request.cmd = SET_VLAN_FLAG_CMD;
140       if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
141          error_msg("ERROR: trying to set flag on device -:%s:- error: %s\n",
142                  if_name, strerror(errno));
143       }
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 */