Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <unistd.h> |
| 3 | #include <fcntl.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 4 | #include <sys/ioctl.h> |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 5 | #include <net/if.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 6 | #include <linux/if_vlan.h> |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 7 | #include <string.h> |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 8 | #include <limits.h> |
| 9 | #include "busybox.h" |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 10 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 11 | #define VLAN_GROUP_ARRAY_LEN 4096 |
| 12 | #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 13 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 14 | /* This is rather specialized in that we're passing a 'char **' in |
| 15 | * order to avoid the pointer dereference multiple times in the |
| 16 | * actual calls below. */ |
| 17 | static unsigned long xstrtoul10(char **str, unsigned long max_val) |
| 18 | { |
| 19 | char *endptr; |
| 20 | unsigned long r; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 21 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 22 | r = strtoul(str[2], &endptr, 10); |
| 23 | if ((r > max_val) || (*endptr != 0)) { |
| 24 | show_usage(); |
| 25 | } |
| 26 | return r; |
| 27 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 28 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 29 | /* On entry, table points to the length of the current string plus |
| 30 | * nul terminator plus data lenght for the subsequent entry. The |
| 31 | * return value is the last data entry for the matching string. */ |
| 32 | static const char *xfind_str(const char *table, const char *str) |
| 33 | { |
| 34 | while (strcasecmp(str, table+1) != 0) { |
| 35 | if (!*(table += table[0])) { |
| 36 | show_usage(); |
| 37 | } |
| 38 | } |
| 39 | return table - 1; |
| 40 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 42 | static const char cmds[] = { |
| 43 | 4, ADD_VLAN_CMD, 7, |
| 44 | 'a', 'd', 'd', 0, |
| 45 | 3, DEL_VLAN_CMD, 7, |
| 46 | 'r', 'e', 'm', 0, |
| 47 | 3, SET_VLAN_NAME_TYPE_CMD, 17, |
| 48 | 's', 'e', 't', '_', |
| 49 | 'n', 'a', 'm', 'e', '_', |
| 50 | 't', 'y', 'p', 'e', 0, |
| 51 | 4, SET_VLAN_FLAG_CMD, 12, |
| 52 | 's', 'e', 't', '_', |
| 53 | 'f', 'l', 'a', 'g', 0, |
| 54 | 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18, |
| 55 | 's', 'e', 't', '_', |
| 56 | 'e', 'g', 'r', 'e', 's', 's', '_', |
| 57 | 'm', 'a', 'p', 0, |
| 58 | 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16, |
| 59 | 's', 'e', 't', '_', |
| 60 | 'i', 'n', 'g', 'r', 'e', 's', 's', '_', |
| 61 | 'm', 'a', 'p', 0, |
| 62 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 63 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 64 | static const char name_types[] = { |
| 65 | VLAN_NAME_TYPE_PLUS_VID, 16, |
| 66 | 'V', 'L', 'A', 'N', |
| 67 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 68 | 0, |
| 69 | VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22, |
| 70 | 'V', 'L', 'A', 'N', |
| 71 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 72 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 73 | VLAN_NAME_TYPE_RAW_PLUS_VID, 15, |
| 74 | 'D', 'E', 'V', |
| 75 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 76 | 0, |
| 77 | VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20, |
| 78 | 'D', 'E', 'V', |
| 79 | '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', |
| 80 | '_', 'N', 'O', '_', 'P', 'A', 'D', 0, |
| 81 | }; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 82 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 83 | static const char conf_file_name[] = "/proc/net/vlan/config"; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 84 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 85 | int vconfig_main(int argc, char **argv) |
| 86 | { |
| 87 | struct vlan_ioctl_args ifr; |
| 88 | const char *p; |
| 89 | int fd; |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 90 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 91 | if (argc < 3) { |
| 92 | show_usage(); |
| 93 | } |
Eric Andersen | 853c494 | 2003-01-23 05:59:32 +0000 | [diff] [blame] | 94 | |
Glenn L McGrath | b4f3d7f | 2003-02-08 23:11:19 +0000 | [diff] [blame] | 95 | /* Don't bother closing the filedes. It will be closed on cleanup. */ |
| 96 | if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */ |
| 97 | perror_msg_and_die("open %s", conf_file_name); |
| 98 | } |
| 99 | |
| 100 | memset(&ifr, 0, sizeof(struct vlan_ioctl_args)); |
| 101 | |
| 102 | ++argv; |
| 103 | p = xfind_str(cmds+2, *argv); |
| 104 | ifr.cmd = *p; |
| 105 | if (argc != p[-1]) { |
| 106 | show_usage(); |
| 107 | } |
| 108 | |
| 109 | if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */ |
| 110 | ifr.u.name_type = *xfind_str(name_types+1, argv[1]); |
| 111 | } else { |
| 112 | if (strlen(argv[1]) >= IF_NAMESIZE) { |
| 113 | error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE); |
| 114 | } |
| 115 | strcpy(ifr.device1, argv[1]); |
| 116 | |
| 117 | /* I suppose one could try to combine some of the function calls below, |
| 118 | * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized |
| 119 | * (unsigned) int members of a unions. But because of the range checking, |
| 120 | * doing so wouldn't save that much space and would also make maintainence |
| 121 | * more of a pain. */ |
| 122 | if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */ |
| 123 | ifr.u.flag = xstrtoul10(argv, 1); |
| 124 | } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */ |
| 125 | ifr.u.VID = xstrtoul10(argv, VLAN_GROUP_ARRAY_LEN-1); |
| 126 | } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */ |
| 127 | ifr.u.skb_priority = xstrtoul10(argv, ULONG_MAX); |
| 128 | ifr.vlan_qos = xstrtoul10(argv+1, 7); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
| 133 | || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) |
| 134 | ) { |
| 135 | perror_msg_and_die("socket or ioctl error for %s", *argv); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |