vconfig: remove /proc/net/vlan/config check
[oweals/busybox.git] / networking / vconfig.c
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 source tree.
8  */
9
10 /* BB_AUDIT SUSv3 N/A */
11
12 //usage:#define vconfig_trivial_usage
13 //usage:       "COMMAND [OPTIONS]"
14 //usage:#define vconfig_full_usage "\n\n"
15 //usage:       "Create and remove virtual ethernet devices\n"
16 //usage:     "\n        add             [interface-name] [vlan_id]"
17 //usage:     "\n        rem             [vlan-name]"
18 //usage:     "\n        set_flag        [interface-name] [flag-num] [0 | 1]"
19 //usage:     "\n        set_egress_map  [vlan-name] [skb_priority] [vlan_qos]"
20 //usage:     "\n        set_ingress_map [vlan-name] [skb_priority] [vlan_qos]"
21 //usage:     "\n        set_name_type   [name-type]"
22
23 #include "libbb.h"
24 #include <net/if.h>
25
26 /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
27 enum vlan_ioctl_cmds {
28         ADD_VLAN_CMD,
29         DEL_VLAN_CMD,
30         SET_VLAN_INGRESS_PRIORITY_CMD,
31         SET_VLAN_EGRESS_PRIORITY_CMD,
32         GET_VLAN_INGRESS_PRIORITY_CMD,
33         GET_VLAN_EGRESS_PRIORITY_CMD,
34         SET_VLAN_NAME_TYPE_CMD,
35         SET_VLAN_FLAG_CMD
36 };
37 enum vlan_name_types {
38         VLAN_NAME_TYPE_PLUS_VID, /* Name will look like:  vlan0005 */
39         VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like:  eth1.0005 */
40         VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like:  vlan5 */
41         VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like:  eth0.5 */
42         VLAN_NAME_TYPE_HIGHEST
43 };
44
45 struct vlan_ioctl_args {
46         int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
47         char device1[24];
48
49         union {
50                 char device2[24];
51                 int VID;
52                 unsigned int skb_priority;
53                 unsigned int name_type;
54                 unsigned int bind_type;
55                 unsigned int flag; /* Matches vlan_dev_info flags */
56         } u;
57
58         short vlan_qos;
59 };
60
61 #define VLAN_GROUP_ARRAY_LEN  4096
62 #define SIOCSIFVLAN           0x8983  /* Set 802.1Q VLAN options */
63
64 /* On entry, table points to the length of the current string
65  * plus NUL terminator plus data length for the subsequent entry.
66  * The return value is the last data entry for the matching string. */
67 static const char *xfind_str(const char *table, const char *str)
68 {
69         while (strcasecmp(str, table+1) != 0) {
70                 table += table[0];
71                 if (!*table) {
72                         bb_show_usage();
73                 }
74         }
75         return table - 1;
76 }
77
78 static const char cmds[] ALIGN1 = {
79         4, ADD_VLAN_CMD, 7,
80         'a', 'd', 'd', 0,
81         3, DEL_VLAN_CMD, 7,
82         'r', 'e', 'm', 0,
83         3, SET_VLAN_NAME_TYPE_CMD, 17,
84         's', 'e', 't', '_',
85         'n', 'a', 'm', 'e', '_',
86         't', 'y', 'p', 'e', 0,
87         5, SET_VLAN_FLAG_CMD, 12,
88         's', 'e', 't', '_',
89         'f', 'l', 'a', 'g', 0,
90         5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
91         's', 'e', 't', '_',
92         'e', 'g', 'r', 'e', 's', 's', '_',
93         'm', 'a', 'p', 0,
94         5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
95         's', 'e', 't', '_',
96         'i', 'n', 'g', 'r', 'e', 's', 's', '_',
97         'm', 'a', 'p', 0,
98 };
99
100 static const char name_types[] ALIGN1 = {
101         VLAN_NAME_TYPE_PLUS_VID, 16,
102         'V', 'L', 'A', 'N',
103         '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
104         0,
105         VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
106         'V', 'L', 'A', 'N',
107         '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
108         '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
109         VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
110         'D', 'E', 'V',
111         '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
112         0,
113         VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
114         'D', 'E', 'V',
115         '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
116         '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
117 };
118
119 int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
120 int vconfig_main(int argc, char **argv)
121 {
122         struct vlan_ioctl_args ifr;
123         const char *p;
124         int fd;
125
126         if (argc < 3) {
127                 bb_show_usage();
128         }
129
130         memset(&ifr, 0, sizeof(ifr));
131
132         ++argv;
133         p = xfind_str(cmds+2, *argv);
134         ifr.cmd = *p;
135         if (argc != p[-1]) {
136                 bb_show_usage();
137         }
138
139         if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
140                 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
141         } else {
142                 strncpy_IFNAMSIZ(ifr.device1, argv[1]);
143                 p = argv[2];
144
145                 /* I suppose one could try to combine some of the function calls below,
146                  * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
147                  * (unsigned) int members of a unions.  But because of the range checking,
148                  * doing so wouldn't save that much space and would also make maintainence
149                  * more of a pain. */
150                 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
151                         ifr.u.flag = xatoul_range(p, 0, 1);
152                         /* DM: in order to set reorder header, qos must be set */
153                         ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
154                 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
155                         ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
156                 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
157                         ifr.u.skb_priority = xatou(p);
158                         ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
159                 }
160         }
161
162         fd = xsocket(AF_INET, SOCK_STREAM, 0);
163         ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
164                                                 "ioctl error for %s", *argv);
165
166         return 0;
167 }