6 #include <linux/if_vlan.h>
11 #define VLAN_GROUP_ARRAY_LEN 4096
12 #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
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)
22 r = strtoul(str[2], &endptr, 10);
23 if ((r > max_val) || (*endptr != 0)) {
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)
34 while (strcasecmp(str, table+1) != 0) {
35 if (!*(table += table[0])) {
42 static const char cmds[] = {
47 3, SET_VLAN_NAME_TYPE_CMD, 17,
49 'n', 'a', 'm', 'e', '_',
50 't', 'y', 'p', 'e', 0,
51 4, SET_VLAN_FLAG_CMD, 12,
53 'f', 'l', 'a', 'g', 0,
54 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
56 'e', 'g', 'r', 'e', 's', 's', '_',
58 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
60 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
64 static const char name_types[] = {
65 VLAN_NAME_TYPE_PLUS_VID, 16,
67 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
69 VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
71 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
72 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
73 VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
75 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
77 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
79 '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
80 '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
83 static const char conf_file_name[] = "/proc/net/vlan/config";
85 int vconfig_main(int argc, char **argv)
87 struct vlan_ioctl_args ifr;
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);
100 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
103 p = xfind_str(cmds+2, *argv);
109 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
110 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
112 if (strlen(argv[1]) >= IF_NAMESIZE) {
113 error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
115 strcpy(ifr.device1, argv[1]);
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
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);
132 if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
133 || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
135 perror_msg_and_die("socket or ioctl error for %s", *argv);