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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /* BB_AUDIT SUSv3 N/A */ |
| 24 | |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include <fcntl.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 29 | #include <net/if.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 30 | #include <linux/if_vlan.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 31 | #include <string.h> |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 32 | #include <limits.h> |
| 33 | #include "busybox.h" |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 34 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 35 | #define VLAN_GROUP_ARRAY_LEN 4096 |
| 36 | #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 37 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 38 | /* On entry, table points to the length of the current string plus |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | * nul terminator plus data length for the subsequent entry. The |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 40 | * return value is the last data entry for the matching string. */ |
| 41 | static const char *xfind_str(const char *table, const char *str) |
| 42 | { |
| 43 | while (strcasecmp(str, table+1) != 0) { |
| 44 | if (!*(table += table[0])) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | return table - 1; |
| 49 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 51 | static const char cmds[] = { |
| 52 | 4, ADD_VLAN_CMD, 7, |
| 53 | 'a', 'd', 'd', 0, |
| 54 | 3, DEL_VLAN_CMD, 7, |
| 55 | 'r', 'e', 'm', 0, |
| 56 | 3, SET_VLAN_NAME_TYPE_CMD, 17, |
| 57 | 's', 'e', 't', '_', |
| 58 | 'n', 'a', 'm', 'e', '_', |
| 59 | 't', 'y', 'p', 'e', 0, |
| 60 | 4, SET_VLAN_FLAG_CMD, 12, |
| 61 | 's', 'e', 't', '_', |
| 62 | 'f', 'l', 'a', 'g', 0, |
| 63 | 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18, |
| 64 | 's', 'e', 't', '_', |
| 65 | 'e', 'g', 'r', 'e', 's', 's', '_', |
| 66 | 'm', 'a', 'p', 0, |
| 67 | 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16, |
| 68 | 's', 'e', 't', '_', |
| 69 | 'i', 'n', 'g', 'r', 'e', 's', 's', '_', |
| 70 | 'm', 'a', 'p', 0, |
| 71 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 72 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 73 | static const char name_types[] = { |
| 74 | VLAN_NAME_TYPE_PLUS_VID, 16, |
| 75 | 'V', 'L', 'A', 'N', |
| 76 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 77 | 0, |
| 78 | VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22, |
| 79 | 'V', 'L', 'A', 'N', |
| 80 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 81 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 82 | VLAN_NAME_TYPE_RAW_PLUS_VID, 15, |
| 83 | 'D', 'E', 'V', |
| 84 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 85 | 0, |
| 86 | VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20, |
| 87 | 'D', 'E', 'V', |
| 88 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 89 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 90 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 91 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 92 | static const char conf_file_name[] = "/proc/net/vlan/config"; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 93 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 94 | int vconfig_main(int argc, char **argv) |
| 95 | { |
| 96 | struct vlan_ioctl_args ifr; |
| 97 | const char *p; |
| 98 | int fd; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 99 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 100 | if (argc < 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 102 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 103 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 104 | /* Don't bother closing the filedes. It will be closed on cleanup. */ |
| 105 | if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | bb_perror_msg_and_die("open %s", conf_file_name); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | memset(&ifr, 0, sizeof(struct vlan_ioctl_args)); |
| 110 | |
| 111 | ++argv; |
| 112 | p = xfind_str(cmds+2, *argv); |
| 113 | ifr.cmd = *p; |
| 114 | if (argc != p[-1]) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 115 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */ |
| 119 | ifr.u.name_type = *xfind_str(name_types+1, argv[1]); |
| 120 | } else { |
| 121 | if (strlen(argv[1]) >= IF_NAMESIZE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 123 | } |
| 124 | strcpy(ifr.device1, argv[1]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | p = argv[2]; |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 126 | |
| 127 | /* I suppose one could try to combine some of the function calls below, |
| 128 | * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized |
| 129 | * (unsigned) int members of a unions. But because of the range checking, |
| 130 | * doing so wouldn't save that much space and would also make maintainence |
| 131 | * more of a pain. */ |
| 132 | if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 134 | } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 136 | } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 137 | ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX); |
| 138 | ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
| 143 | || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) |
| 144 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | bb_perror_msg_and_die("socket or ioctl error for %s", *argv); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |