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