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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 13 | #include <net/if.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 14 | |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 15 | /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */ |
| 16 | enum 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 | }; |
| 26 | enum 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 | |
| 34 | struct vlan_ioctl_args { |
| 35 | int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */ |
| 36 | char device1[24]; |
| 37 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 38 | union { |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 39 | 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 Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 45 | } u; |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 46 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 47 | short vlan_qos; |
Eric Andersen | b8d2cd4 | 2003-12-19 10:40:56 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 50 | #define VLAN_GROUP_ARRAY_LEN 4096 |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 51 | #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 52 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 53 | /* 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] | 54 | * nul terminator plus data length for the subsequent entry. The |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 55 | * return value is the last data entry for the matching string. */ |
| 56 | static const char *xfind_str(const char *table, const char *str) |
| 57 | { |
| 58 | while (strcasecmp(str, table+1) != 0) { |
| 59 | if (!*(table += table[0])) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | return table - 1; |
| 64 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 65 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 66 | static const char cmds[] = { |
| 67 | 4, ADD_VLAN_CMD, 7, |
| 68 | 'a', 'd', 'd', 0, |
| 69 | 3, DEL_VLAN_CMD, 7, |
| 70 | 'r', 'e', 'm', 0, |
| 71 | 3, SET_VLAN_NAME_TYPE_CMD, 17, |
| 72 | 's', 'e', 't', '_', |
| 73 | 'n', 'a', 'm', 'e', '_', |
| 74 | 't', 'y', 'p', 'e', 0, |
Denis Vlasenko | 3038ac9 | 2006-09-30 19:37:25 +0000 | [diff] [blame] | 75 | 5, SET_VLAN_FLAG_CMD, 12, |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 76 | 's', 'e', 't', '_', |
| 77 | 'f', 'l', 'a', 'g', 0, |
| 78 | 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18, |
| 79 | 's', 'e', 't', '_', |
| 80 | 'e', 'g', 'r', 'e', 's', 's', '_', |
| 81 | 'm', 'a', 'p', 0, |
| 82 | 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16, |
| 83 | 's', 'e', 't', '_', |
| 84 | 'i', 'n', 'g', 'r', 'e', 's', 's', '_', |
| 85 | 'm', 'a', 'p', 0, |
| 86 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 87 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 88 | static const char name_types[] = { |
| 89 | VLAN_NAME_TYPE_PLUS_VID, 16, |
| 90 | 'V', 'L', 'A', 'N', |
| 91 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 92 | 0, |
| 93 | VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22, |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 94 | 'V', 'L', 'A', 'N', |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 95 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 96 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 97 | VLAN_NAME_TYPE_RAW_PLUS_VID, 15, |
| 98 | 'D', 'E', 'V', |
| 99 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 100 | 0, |
| 101 | VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20, |
| 102 | 'D', 'E', 'V', |
| 103 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 104 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 105 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 106 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 107 | static const char conf_file_name[] = "/proc/net/vlan/config"; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 109 | int vconfig_main(int argc, char **argv); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 110 | int vconfig_main(int argc, char **argv) |
| 111 | { |
| 112 | struct vlan_ioctl_args ifr; |
| 113 | const char *p; |
| 114 | int fd; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 115 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 116 | if (argc < 3) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 117 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 118 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 119 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 120 | /* 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] | 121 | /* Will die if 802.1q is not present */ |
Bernhard Reutner-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 122 | xopen(conf_file_name, O_RDONLY); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 123 | |
| 124 | memset(&ifr, 0, sizeof(struct vlan_ioctl_args)); |
| 125 | |
| 126 | ++argv; |
| 127 | p = xfind_str(cmds+2, *argv); |
| 128 | ifr.cmd = *p; |
| 129 | if (argc != p[-1]) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 130 | bb_show_usage(); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */ |
| 134 | ifr.u.name_type = *xfind_str(name_types+1, argv[1]); |
| 135 | } else { |
| 136 | if (strlen(argv[1]) >= IF_NAMESIZE) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 137 | bb_error_msg_and_die("if_name >= %d chars", IF_NAMESIZE); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 138 | } |
| 139 | strcpy(ifr.device1, argv[1]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | p = argv[2]; |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 141 | |
| 142 | /* I suppose one could try to combine some of the function calls below, |
| 143 | * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized |
| 144 | * (unsigned) int members of a unions. But because of the range checking, |
| 145 | * doing so wouldn't save that much space and would also make maintainence |
| 146 | * more of a pain. */ |
| 147 | if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 148 | ifr.u.flag = xatoul_range(p, 0, 1); |
Denis Vlasenko | 3038ac9 | 2006-09-30 19:37:25 +0000 | [diff] [blame] | 149 | /* DM: in order to set reorder header, qos must be set */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 150 | ifr.vlan_qos = xatoul_range(argv[3], 0, 7); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 151 | } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 152 | ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 153 | } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 154 | ifr.u.skb_priority = xatou(p); |
| 155 | ifr.vlan_qos = xatoul_range(argv[3], 0, 7); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 159 | fd = xsocket(AF_INET, SOCK_STREAM, 0); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame^] | 160 | ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr, |
| 161 | "ioctl error for %s", *argv); |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |