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 | * |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* BB_AUDIT SUSv3 N/A */ |
| 11 | |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
| 14 | #include <fcntl.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 15 | #include <sys/ioctl.h> |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 16 | #include <net/if.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 17 | #include <string.h> |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 18 | #include <limits.h> |
| 19 | #include "busybox.h" |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 20 | |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 21 | /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */ |
| 22 | enum vlan_ioctl_cmds { |
| 23 | ADD_VLAN_CMD, |
| 24 | DEL_VLAN_CMD, |
| 25 | SET_VLAN_INGRESS_PRIORITY_CMD, |
| 26 | SET_VLAN_EGRESS_PRIORITY_CMD, |
| 27 | GET_VLAN_INGRESS_PRIORITY_CMD, |
| 28 | GET_VLAN_EGRESS_PRIORITY_CMD, |
| 29 | SET_VLAN_NAME_TYPE_CMD, |
| 30 | SET_VLAN_FLAG_CMD |
| 31 | }; |
| 32 | enum vlan_name_types { |
| 33 | VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */ |
| 34 | VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */ |
| 35 | VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */ |
| 36 | VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */ |
| 37 | VLAN_NAME_TYPE_HIGHEST |
| 38 | }; |
| 39 | |
| 40 | struct vlan_ioctl_args { |
| 41 | int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */ |
| 42 | char device1[24]; |
| 43 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 44 | union { |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 45 | char device2[24]; |
| 46 | int VID; |
| 47 | unsigned int skb_priority; |
| 48 | unsigned int name_type; |
| 49 | unsigned int bind_type; |
| 50 | unsigned int flag; /* Matches vlan_dev_info flags */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 51 | } u; |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 52 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 53 | short vlan_qos; |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 56 | #define VLAN_GROUP_ARRAY_LEN 4096 |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 57 | #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 58 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 59 | /* 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] | 60 | * nul terminator plus data length for the subsequent entry. The |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 61 | * return value is the last data entry for the matching string. */ |
| 62 | static const char *xfind_str(const char *table, const char *str) |
| 63 | { |
| 64 | while (strcasecmp(str, table+1) != 0) { |
| 65 | if (!*(table += table[0])) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | return table - 1; |
| 70 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 71 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 72 | static const char cmds[] = { |
| 73 | 4, ADD_VLAN_CMD, 7, |
| 74 | 'a', 'd', 'd', 0, |
| 75 | 3, DEL_VLAN_CMD, 7, |
| 76 | 'r', 'e', 'm', 0, |
| 77 | 3, SET_VLAN_NAME_TYPE_CMD, 17, |
| 78 | 's', 'e', 't', '_', |
| 79 | 'n', 'a', 'm', 'e', '_', |
| 80 | 't', 'y', 'p', 'e', 0, |
| 81 | 4, SET_VLAN_FLAG_CMD, 12, |
| 82 | 's', 'e', 't', '_', |
| 83 | 'f', 'l', 'a', 'g', 0, |
| 84 | 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18, |
| 85 | 's', 'e', 't', '_', |
| 86 | 'e', 'g', 'r', 'e', 's', 's', '_', |
| 87 | 'm', 'a', 'p', 0, |
| 88 | 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16, |
| 89 | 's', 'e', 't', '_', |
| 90 | 'i', 'n', 'g', 'r', 'e', 's', 's', '_', |
| 91 | 'm', 'a', 'p', 0, |
| 92 | }; |
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 | static const char name_types[] = { |
| 95 | VLAN_NAME_TYPE_PLUS_VID, 16, |
| 96 | 'V', 'L', 'A', 'N', |
| 97 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 98 | 0, |
| 99 | VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22, |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 100 | 'V', 'L', 'A', 'N', |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 101 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 102 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 103 | VLAN_NAME_TYPE_RAW_PLUS_VID, 15, |
| 104 | 'D', 'E', 'V', |
| 105 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 106 | 0, |
| 107 | VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20, |
| 108 | 'D', 'E', 'V', |
| 109 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 110 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 111 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 112 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 113 | static const char conf_file_name[] = "/proc/net/vlan/config"; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 114 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 115 | int vconfig_main(int argc, char **argv) |
| 116 | { |
| 117 | struct vlan_ioctl_args ifr; |
| 118 | const char *p; |
| 119 | int fd; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 120 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 121 | if (argc < 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 123 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 124 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 125 | /* Don't bother closing the filedes. It will be closed on cleanup. */ |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 126 | /* Will die if 802.1q is not present */ |
| 127 | bb_xopen3(conf_file_name, O_RDONLY, 0); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 128 | |
| 129 | memset(&ifr, 0, sizeof(struct vlan_ioctl_args)); |
| 130 | |
| 131 | ++argv; |
| 132 | p = xfind_str(cmds+2, *argv); |
| 133 | ifr.cmd = *p; |
| 134 | if (argc != p[-1]) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */ |
| 139 | ifr.u.name_type = *xfind_str(name_types+1, argv[1]); |
| 140 | } else { |
| 141 | if (strlen(argv[1]) >= IF_NAMESIZE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 142 | 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] | 143 | } |
| 144 | strcpy(ifr.device1, argv[1]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | p = argv[2]; |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 146 | |
| 147 | /* I suppose one could try to combine some of the function calls below, |
| 148 | * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized |
| 149 | * (unsigned) int members of a unions. But because of the range checking, |
| 150 | * doing so wouldn't save that much space and would also make maintainence |
| 151 | * more of a pain. */ |
| 152 | if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 153 | ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 154 | } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 155 | 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] | 156 | } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 157 | ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX); |
| 158 | ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 162 | fd = bb_xsocket(AF_INET, SOCK_STREAM, 0); |
| 163 | if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) { |
| 164 | bb_perror_msg_and_die("ioctl error for %s", *argv); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |