1 /* vconfig.c - Creates virtual ethernet devices.
2  *
3  * Copyright 2012 Sandeep Sharma <sandeep.jack2756@gmail.com>
4  * Copyright 2012 Kyungwan Han <asura321@gmail.com>
5  *
6  * No standard
7 
8 USE_VCONFIG(NEWTOY(vconfig, "<2>4", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
9 
10 config VCONFIG
11   bool "vconfig"
12   default y
13   help
14     usage: vconfig COMMAND [OPTIONS]
15 
16     Create and remove virtual ethernet devices
17 
18     add             [interface-name] [vlan_id]
19     rem             [vlan-name]
20     set_flag        [interface-name] [flag-num]       [0 | 1]
21     set_egress_map  [vlan-name]      [skb_priority]   [vlan_qos]
22     set_ingress_map [vlan-name]      [skb_priority]   [vlan_qos]
23     set_name_type   [name-type]
24 */
25 
26 #include "toys.h"
27 #include <linux/if_vlan.h>
28 #include <linux/sockios.h>
29 
vconfig_main(void)30 void vconfig_main(void)
31 {
32   struct vlan_ioctl_args request;
33   char *cmd;
34   int fd;
35 
36   fd = xsocket(AF_INET, SOCK_STREAM, 0);
37   memset(&request, 0, sizeof(struct vlan_ioctl_args));
38   cmd = toys.optargs[0];
39 
40   if (!strcmp(cmd, "set_name_type")) {
41     char *types[] = {"VLAN_PLUS_VID", "DEV_PLUS_VID", "VLAN_PLUS_VID_NO_PAD",
42                      "DEV_PLUS_VID_NO_PAD"};
43     int i, j = sizeof(types)/sizeof(*types);
44 
45     for (i=0; i<j; i++) if (!strcmp(toys.optargs[1], types[i])) break;
46     if (i == j) {
47       for (i=0; i<j; i++) puts(types[i]);
48       error_exit("%s: unknown '%s'", cmd, toys.optargs[1]);
49     }
50 
51     request.u.name_type = i;
52     request.cmd = SET_VLAN_NAME_TYPE_CMD;
53     xioctl(fd, SIOCSIFVLAN, &request);
54     return;
55   }
56 
57   // Store interface name
58   xstrncpy(request.device1, toys.optargs[1], 16);
59 
60   if (!strcmp(cmd, "add")) {
61     request.cmd = ADD_VLAN_CMD;
62     if (toys.optargs[2]) request.u.VID = atolx_range(toys.optargs[2], 0, 4094);
63     if (request.u.VID == 1)
64       xprintf("WARNING: VLAN 1 does not work with many switches.\n");
65   } else if (!strcmp(cmd, "rem")) request.cmd = DEL_VLAN_CMD;
66   else if (!strcmp(cmd, "set_flag")) {
67     request.cmd = SET_VLAN_FLAG_CMD;
68     if (toys.optargs[2]) request.u.flag = atolx_range(toys.optargs[2], 0, 1);
69     if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
70   } else if(strcmp(cmd, "set_egress_map") == 0) {
71     request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
72     if (toys.optargs[2])
73       request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
74     if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
75   } else if(strcmp(cmd, "set_ingress_map") == 0) {
76     request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
77     if (toys.optargs[2])
78       request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
79     //To set flag we must have to set vlan_qos
80     if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
81   } else {
82     xclose(fd);
83     perror_exit("Unknown command %s", cmd);
84   }
85 
86   xioctl(fd, SIOCSIFVLAN, &request);
87   xprintf("Successful %s on device %s\n", cmd, toys.optargs[1]);
88 }
89