blob: 48b45d9af19fd084ddbcb997cc9a64811cae9d98 [file] [log] [blame]
Manuel Novoa III cad53642003-03-19 09:13:01 +00001/* vi: set sw=4 ts=4: */
2/*
3 * vconfig implementation for busybox
4 *
5 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Manuel Novoa III cad53642003-03-19 09:13:01 +00008 */
9
10/* BB_AUDIT SUSv3 N/A */
11
Pere Orga5bc8c002011-04-11 03:29:49 +020012//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"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000024#include <net/if.h>
Eric Andersen853c4942003-01-23 05:59:32 +000025
Eric Andersenb8d2cd42003-12-19 10:40:56 +000026/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
27enum 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};
37enum 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
45struct vlan_ioctl_args {
46 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
47 char device1[24];
48
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049 union {
Eric Andersenb8d2cd42003-12-19 10:40:56 +000050 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 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000056 } u;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000057
Eric Andersenc7bda1c2004-03-15 08:29:22 +000058 short vlan_qos;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000059};
60
Denys Vlasenkofb132e42010-10-29 11:46:52 +020061#define VLAN_GROUP_ARRAY_LEN 4096
62#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
Eric Andersen853c4942003-01-23 05:59:32 +000063
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +000064/* 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. */
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000067static const char *xfind_str(const char *table, const char *str)
68{
69 while (strcasecmp(str, table+1) != 0) {
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +000070 table += table[0];
71 if (!*table) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000073 }
74 }
75 return table - 1;
76}
Eric Andersen853c4942003-01-23 05:59:32 +000077
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000078static const char cmds[] ALIGN1 = {
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000079 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,
Denis Vlasenko3038ac92006-09-30 19:37:25 +000087 5, SET_VLAN_FLAG_CMD, 12,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000088 '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};
Eric Andersen853c4942003-01-23 05:59:32 +000099
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000100static const char name_types[] ALIGN1 = {
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000101 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,
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000106 'V', 'L', 'A', 'N',
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000107 '_', '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};
Eric Andersen853c4942003-01-23 05:59:32 +0000118
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000119static const char conf_file_name[] ALIGN1 = "/proc/net/vlan/config";
Eric Andersen853c4942003-01-23 05:59:32 +0000120
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000121int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000122int vconfig_main(int argc, char **argv)
123{
124 struct vlan_ioctl_args ifr;
125 const char *p;
126 int fd;
Eric Andersen853c4942003-01-23 05:59:32 +0000127
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000128 if (argc < 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000130 }
Eric Andersen853c4942003-01-23 05:59:32 +0000131
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000132 /* Don't bother closing the filedes. It will be closed on cleanup. */
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000133 /* Will die if 802.1q is not present */
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000134 xopen(conf_file_name, O_RDONLY);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000135
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +0000136 memset(&ifr, 0, sizeof(ifr));
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000137
138 ++argv;
139 p = xfind_str(cmds+2, *argv);
140 ifr.cmd = *p;
141 if (argc != p[-1]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000143 }
144
145 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
146 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
147 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000148 strncpy_IFNAMSIZ(ifr.device1, argv[1]);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 p = argv[2];
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000150
151 /* I suppose one could try to combine some of the function calls below,
152 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
153 * (unsigned) int members of a unions. But because of the range checking,
154 * doing so wouldn't save that much space and would also make maintainence
155 * more of a pain. */
156 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
Denis Vlasenko13858992006-10-08 12:49:22 +0000157 ifr.u.flag = xatoul_range(p, 0, 1);
Denis Vlasenko3038ac92006-09-30 19:37:25 +0000158 /* DM: in order to set reorder header, qos must be set */
Denis Vlasenko13858992006-10-08 12:49:22 +0000159 ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000160 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
Denis Vlasenko13858992006-10-08 12:49:22 +0000161 ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000162 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
Denis Vlasenko13858992006-10-08 12:49:22 +0000163 ifr.u.skb_priority = xatou(p);
164 ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000165 }
166 }
167
Rob Landleyd921b2e2006-08-03 15:41:12 +0000168 fd = xsocket(AF_INET, SOCK_STREAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000169 ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
170 "ioctl error for %s", *argv);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000171
172 return 0;
173}