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