blob: e6e2872bfef40c524003210c7fa39ecc70ae3129 [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
Denys Vlasenko47367e12016-11-23 09:05:14 +010012//config:config VCONFIG
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "vconfig (2.5 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010014//config: default y
15//config: select PLATFORM_LINUX
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: Creates, removes, and configures VLAN interfaces
Denys Vlasenko47367e12016-11-23 09:05:14 +010018
19//applet:IF_VCONFIG(APPLET(vconfig, BB_DIR_SBIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_VCONFIG) += vconfig.o
22
Pere Orga5bc8c002011-04-11 03:29:49 +020023//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"
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010027//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"
Pere Orga5bc8c002011-04-11 03:29:49 +020033
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000035#include <net/if.h>
Eric Andersen853c4942003-01-23 05:59:32 +000036
Eric Andersenb8d2cd42003-12-19 10:40:56 +000037/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
38enum 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};
48enum 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
56struct vlan_ioctl_args {
57 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
58 char device1[24];
59
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060 union {
Eric Andersenb8d2cd42003-12-19 10:40:56 +000061 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 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000067 } u;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000068
Eric Andersenc7bda1c2004-03-15 08:29:22 +000069 short vlan_qos;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000070};
71
Denys Vlasenkofb132e42010-10-29 11:46:52 +020072#define VLAN_GROUP_ARRAY_LEN 4096
73#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
Eric Andersen853c4942003-01-23 05:59:32 +000074
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +000075/* 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. */
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000078static const char *xfind_str(const char *table, const char *str)
79{
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010080 while (strcasecmp(str, table + 1) != 0) {
81 if (!table[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 bb_show_usage();
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010083 table += table[0];
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000084 }
85 return table - 1;
86}
Eric Andersen853c4942003-01-23 05:59:32 +000087
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000088static const char cmds[] ALIGN1 = {
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000089 4, ADD_VLAN_CMD, 7,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010090 'a','d','d',0,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000091 3, DEL_VLAN_CMD, 7,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010092 'r','e','m',0,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000093 3, SET_VLAN_NAME_TYPE_CMD, 17,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010094 's','e','t','_','n','a','m','e','_','t','y','p','e',0,
Denis Vlasenko3038ac92006-09-30 19:37:25 +000095 5, SET_VLAN_FLAG_CMD, 12,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010096 's','e','t','_','f','l','a','g',0,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000097 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +010098 '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,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000101};
Eric Andersen853c4942003-01-23 05:59:32 +0000102
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000103static const char name_types[] ALIGN1 = {
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000104 VLAN_NAME_TYPE_PLUS_VID, 16,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100105 'V','L','A','N','_','P','L','U','S','_','V','I','D',0,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000106 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100107 'V','L','A','N','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000108 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100109 '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,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000112};
Eric Andersen853c4942003-01-23 05:59:32 +0000113
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000114int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000115int vconfig_main(int argc, char **argv)
116{
117 struct vlan_ioctl_args ifr;
118 const char *p;
119 int fd;
Eric Andersen853c4942003-01-23 05:59:32 +0000120
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +0000121 memset(&ifr, 0, sizeof(ifr));
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000122
123 ++argv;
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100124 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 bb_show_usage();
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100126 p = xfind_str(cmds + 2, argv[0]);
127 ifr.cmd = *p;
128 if (argc != p[-1])
129 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000130
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100131 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) {
132 /* set_name_type */
133 ifr.u.name_type = *xfind_str(name_types + 1, argv[1]);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000134 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000135 strncpy_IFNAMSIZ(ifr.device1, argv[1]);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 p = argv[2];
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000137
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,
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200141 * doing so wouldn't save that much space and would also make maintenance
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100142 * 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);
Denis Vlasenko3038ac92006-09-30 19:37:25 +0000147 /* DM: in order to set reorder header, qos must be set */
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100148 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 */
Denis Vlasenko13858992006-10-08 12:49:22 +0000154 ifr.u.skb_priority = xatou(p);
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100155 ifr.vlan_qos = xatou_range(argv[3], 0, 7);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000156 }
157 }
158
Rob Landleyd921b2e2006-08-03 15:41:12 +0000159 fd = xsocket(AF_INET, SOCK_STREAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000160 ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
Denys Vlasenkofdd0b3b2012-01-16 04:00:37 +0100161 "ioctl error for %s", argv[0]);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000162
163 return 0;
164}