blob: 00379fc2f982756dd12d7bac37597fedb83f2b89 [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 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Manuel Novoa III cad53642003-03-19 09:13:01 +00008 */
9
10/* BB_AUDIT SUSv3 N/A */
11
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000013#include <net/if.h>
Eric Andersen853c4942003-01-23 05:59:32 +000014
Eric Andersenb8d2cd42003-12-19 10:40:56 +000015/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
16enum vlan_ioctl_cmds {
17 ADD_VLAN_CMD,
18 DEL_VLAN_CMD,
19 SET_VLAN_INGRESS_PRIORITY_CMD,
20 SET_VLAN_EGRESS_PRIORITY_CMD,
21 GET_VLAN_INGRESS_PRIORITY_CMD,
22 GET_VLAN_EGRESS_PRIORITY_CMD,
23 SET_VLAN_NAME_TYPE_CMD,
24 SET_VLAN_FLAG_CMD
25};
26enum vlan_name_types {
27 VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
28 VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
29 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
30 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
31 VLAN_NAME_TYPE_HIGHEST
32};
33
34struct vlan_ioctl_args {
35 int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
36 char device1[24];
37
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000038 union {
Eric Andersenb8d2cd42003-12-19 10:40:56 +000039 char device2[24];
40 int VID;
41 unsigned int skb_priority;
42 unsigned int name_type;
43 unsigned int bind_type;
44 unsigned int flag; /* Matches vlan_dev_info flags */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000045 } u;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000046
Eric Andersenc7bda1c2004-03-15 08:29:22 +000047 short vlan_qos;
Eric Andersenb8d2cd42003-12-19 10:40:56 +000048};
49
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000050#define VLAN_GROUP_ARRAY_LEN 4096
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
Eric Andersen853c4942003-01-23 05:59:32 +000052
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +000053/* On entry, table points to the length of the current string
54 * plus NUL terminator plus data length for the subsequent entry.
55 * The return value is the last data entry for the matching string. */
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000056static const char *xfind_str(const char *table, const char *str)
57{
58 while (strcasecmp(str, table+1) != 0) {
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +000059 table += table[0];
60 if (!*table) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000062 }
63 }
64 return table - 1;
65}
Eric Andersen853c4942003-01-23 05:59:32 +000066
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000067static const char cmds[] ALIGN1 = {
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000068 4, ADD_VLAN_CMD, 7,
69 'a', 'd', 'd', 0,
70 3, DEL_VLAN_CMD, 7,
71 'r', 'e', 'm', 0,
72 3, SET_VLAN_NAME_TYPE_CMD, 17,
73 's', 'e', 't', '_',
74 'n', 'a', 'm', 'e', '_',
75 't', 'y', 'p', 'e', 0,
Denis Vlasenko3038ac92006-09-30 19:37:25 +000076 5, SET_VLAN_FLAG_CMD, 12,
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000077 's', 'e', 't', '_',
78 'f', 'l', 'a', 'g', 0,
79 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
80 's', 'e', 't', '_',
81 'e', 'g', 'r', 'e', 's', 's', '_',
82 'm', 'a', 'p', 0,
83 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
84 's', 'e', 't', '_',
85 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
86 'm', 'a', 'p', 0,
87};
Eric Andersen853c4942003-01-23 05:59:32 +000088
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000089static const char name_types[] ALIGN1 = {
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000090 VLAN_NAME_TYPE_PLUS_VID, 16,
91 'V', 'L', 'A', 'N',
92 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
93 0,
94 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
Eric Andersenc7bda1c2004-03-15 08:29:22 +000095 'V', 'L', 'A', 'N',
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +000096 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
97 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
98 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
99 'D', 'E', 'V',
100 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
101 0,
102 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
103 'D', 'E', 'V',
104 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
105 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
106};
Eric Andersen853c4942003-01-23 05:59:32 +0000107
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000108static const char conf_file_name[] ALIGN1 = "/proc/net/vlan/config";
Eric Andersen853c4942003-01-23 05:59:32 +0000109
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000110int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000111int vconfig_main(int argc, char **argv)
112{
113 struct vlan_ioctl_args ifr;
114 const char *p;
115 int fd;
Eric Andersen853c4942003-01-23 05:59:32 +0000116
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000117 if (argc < 3) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000119 }
Eric Andersen853c4942003-01-23 05:59:32 +0000120
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000121 /* Don't bother closing the filedes. It will be closed on cleanup. */
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000122 /* Will die if 802.1q is not present */
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000123 xopen(conf_file_name, O_RDONLY);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000124
Denis Vlasenkoc5045fd2008-12-02 20:38:36 +0000125 memset(&ifr, 0, sizeof(ifr));
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000126
127 ++argv;
128 p = xfind_str(cmds+2, *argv);
129 ifr.cmd = *p;
130 if (argc != p[-1]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 bb_show_usage();
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000132 }
133
134 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
135 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
136 } else {
Denis Vlasenko360d9662008-12-02 18:18:50 +0000137 strncpy_IFNAMSIZ(ifr.device1, argv[1]);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 p = argv[2];
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000139
140 /* I suppose one could try to combine some of the function calls below,
141 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
142 * (unsigned) int members of a unions. But because of the range checking,
143 * doing so wouldn't save that much space and would also make maintainence
144 * more of a pain. */
145 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
Denis Vlasenko13858992006-10-08 12:49:22 +0000146 ifr.u.flag = xatoul_range(p, 0, 1);
Denis Vlasenko3038ac92006-09-30 19:37:25 +0000147 /* DM: in order to set reorder header, qos must be set */
Denis Vlasenko13858992006-10-08 12:49:22 +0000148 ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000149 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
Denis Vlasenko13858992006-10-08 12:49:22 +0000150 ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000151 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
Denis Vlasenko13858992006-10-08 12:49:22 +0000152 ifr.u.skb_priority = xatou(p);
153 ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000154 }
155 }
156
Rob Landleyd921b2e2006-08-03 15:41:12 +0000157 fd = xsocket(AF_INET, SOCK_STREAM, 0);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000158 ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
159 "ioctl error for %s", *argv);
Glenn L McGrathb4f3d7f2003-02-08 23:11:19 +0000160
161 return 0;
162}