Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 9 | //config:config VCONFIG |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "vconfig (2.3 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 11 | //config: default y |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: Creates, removes, and configures VLAN interfaces |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 14 | |
Denys Vlasenko | a4d4ab0 | 2017-08-09 18:52:19 +0200 | [diff] [blame] | 15 | //applet:IF_VCONFIG(APPLET_NOEXEC(vconfig, vconfig, BB_DIR_SBIN, BB_SUID_DROP, vconfig)) |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 16 | |
| 17 | //kbuild:lib-$(CONFIG_VCONFIG) += vconfig.o |
| 18 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 19 | //usage:#define vconfig_trivial_usage |
| 20 | //usage: "COMMAND [OPTIONS]" |
| 21 | //usage:#define vconfig_full_usage "\n\n" |
| 22 | //usage: "Create and remove virtual ethernet devices\n" |
Denys Vlasenko | 6b6826f | 2021-06-13 01:08:48 +0200 | [diff] [blame] | 23 | //usage: "\n add IFACE VLAN_ID" |
| 24 | //usage: "\n rem VLAN_NAME" |
| 25 | //usage: "\n set_flag IFACE 0|1 VLAN_QOS" |
| 26 | //usage: "\n set_egress_map VLAN_NAME SKB_PRIO VLAN_QOS" |
| 27 | //usage: "\n set_ingress_map VLAN_NAME SKB_PRIO VLAN_QOS" |
| 28 | //usage: "\n set_name_type NAME_TYPE" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 31 | #include <net/if.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 32 | |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 33 | /* BB_AUDIT SUSv3 N/A */ |
| 34 | |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 35 | /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */ |
| 36 | enum vlan_ioctl_cmds { |
| 37 | ADD_VLAN_CMD, |
| 38 | DEL_VLAN_CMD, |
| 39 | SET_VLAN_INGRESS_PRIORITY_CMD, |
| 40 | SET_VLAN_EGRESS_PRIORITY_CMD, |
| 41 | GET_VLAN_INGRESS_PRIORITY_CMD, |
| 42 | GET_VLAN_EGRESS_PRIORITY_CMD, |
| 43 | SET_VLAN_NAME_TYPE_CMD, |
| 44 | SET_VLAN_FLAG_CMD |
| 45 | }; |
| 46 | enum vlan_name_types { |
| 47 | VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */ |
| 48 | VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */ |
| 49 | VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */ |
| 50 | VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */ |
| 51 | VLAN_NAME_TYPE_HIGHEST |
| 52 | }; |
| 53 | |
| 54 | struct vlan_ioctl_args { |
| 55 | int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */ |
| 56 | char device1[24]; |
| 57 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 58 | union { |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 59 | char device2[24]; |
| 60 | int VID; |
| 61 | unsigned int skb_priority; |
| 62 | unsigned int name_type; |
| 63 | unsigned int bind_type; |
| 64 | unsigned int flag; /* Matches vlan_dev_info flags */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 65 | } u; |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 66 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 67 | short vlan_qos; |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 70 | #define VLAN_GROUP_ARRAY_LEN 4096 |
| 71 | #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | c5045fd | 2008-12-02 20:38:36 +0000 | [diff] [blame] | 73 | /* On entry, table points to the length of the current string |
| 74 | * plus NUL terminator plus data length for the subsequent entry. |
| 75 | * The return value is the last data entry for the matching string. */ |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 76 | static const char *xfind_str(const char *table, const char *str) |
| 77 | { |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 78 | while (strcasecmp(str, table + 1) != 0) { |
| 79 | if (!table[0]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | bb_show_usage(); |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 81 | table += table[0]; |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 82 | } |
| 83 | return table - 1; |
| 84 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 86 | static const char cmds[] ALIGN1 = { |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 87 | 4, ADD_VLAN_CMD, 7, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 88 | 'a','d','d',0, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 89 | 3, DEL_VLAN_CMD, 7, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 90 | 'r','e','m',0, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 91 | 3, SET_VLAN_NAME_TYPE_CMD, 17, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 92 | 's','e','t','_','n','a','m','e','_','t','y','p','e',0, |
Denis Vlasenko | 3038ac9 | 2006-09-30 19:37:25 +0000 | [diff] [blame] | 93 | 5, SET_VLAN_FLAG_CMD, 12, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 94 | 's','e','t','_','f','l','a','g',0, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 95 | 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 96 | 's','e','t','_','e','g','r','e','s','s','_','m','a','p',0, |
| 97 | 5, SET_VLAN_INGRESS_PRIORITY_CMD, 0, |
| 98 | 's','e','t','_','i','n','g','r','e','s','s','_','m','a','p',0, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 99 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 101 | static const char name_types[] ALIGN1 = { |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 102 | VLAN_NAME_TYPE_PLUS_VID, 16, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 103 | 'V','L','A','N','_','P','L','U','S','_','V','I','D',0, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 104 | VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 105 | 'V','L','A','N','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 106 | VLAN_NAME_TYPE_RAW_PLUS_VID, 15, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 107 | 'D','E','V','_','P','L','U','S','_','V','I','D',0, |
| 108 | VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 0, |
| 109 | 'D','E','V','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 110 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 112 | int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 113 | int vconfig_main(int argc, char **argv) |
| 114 | { |
| 115 | struct vlan_ioctl_args ifr; |
| 116 | const char *p; |
| 117 | int fd; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | c5045fd | 2008-12-02 20:38:36 +0000 | [diff] [blame] | 119 | memset(&ifr, 0, sizeof(ifr)); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 120 | |
| 121 | ++argv; |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 122 | if (!argv[0]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | bb_show_usage(); |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 124 | p = xfind_str(cmds + 2, argv[0]); |
| 125 | ifr.cmd = *p; |
| 126 | if (argc != p[-1]) |
| 127 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 128 | |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 129 | if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { |
| 130 | /* set_name_type */ |
| 131 | ifr.u.name_type = *xfind_str(name_types + 1, argv[1]); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 132 | } else { |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 133 | strncpy_IFNAMSIZ(ifr.device1, argv[1]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 134 | p = argv[2]; |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 135 | |
| 136 | /* I suppose one could try to combine some of the function calls below, |
| 137 | * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized |
| 138 | * (unsigned) int members of a unions. But because of the range checking, |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 139 | * doing so wouldn't save that much space and would also make maintenance |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 140 | * more of a pain. |
| 141 | */ |
| 142 | if (ifr.cmd == SET_VLAN_FLAG_CMD) { |
| 143 | /* set_flag */ |
| 144 | ifr.u.flag = xatou_range(p, 0, 1); |
Denis Vlasenko | 3038ac9 | 2006-09-30 19:37:25 +0000 | [diff] [blame] | 145 | /* DM: in order to set reorder header, qos must be set */ |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 146 | ifr.vlan_qos = xatou_range(argv[3], 0, 7); |
| 147 | } else if (ifr.cmd == ADD_VLAN_CMD) { |
| 148 | /* add */ |
| 149 | ifr.u.VID = xatou_range(p, 0, VLAN_GROUP_ARRAY_LEN - 1); |
| 150 | } else if (ifr.cmd != DEL_VLAN_CMD) { |
| 151 | /* set_{egress|ingress}_map */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 152 | ifr.u.skb_priority = xatou(p); |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 153 | ifr.vlan_qos = xatou_range(argv[3], 0, 7); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 157 | fd = xsocket(AF_INET, SOCK_STREAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 158 | ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr, |
Denys Vlasenko | fdd0b3b | 2012-01-16 04:00:37 +0100 | [diff] [blame] | 159 | "ioctl error for %s", argv[0]); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 160 | |
| 161 | return 0; |
| 162 | } |