toybox/toys/other/vconfig.c
<<
>>
Prefs
   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
   8USE_VCONFIG(NEWTOY(vconfig, "<2>4", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
   9
  10config 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
  30void vconfig_main(void)
  31{
  32  struct vlan_ioctl_args request;
  33  char *cmd = *toys.optargs;
  34  int fd = xsocket(AF_INET, SOCK_STREAM, 0);
  35
  36  memset(&request, 0, sizeof(struct vlan_ioctl_args));
  37
  38  if (!strcmp(cmd, "set_name_type")) {
  39    char *types[] = {"VLAN_PLUS_VID", "DEV_PLUS_VID", "VLAN_PLUS_VID_NO_PAD",
  40                     "DEV_PLUS_VID_NO_PAD"};
  41    int i, j = ARRAY_LEN(types);
  42
  43    for (i=0; i<j; i++) if (!strcmp(toys.optargs[1], types[i])) break;
  44    if (i == j) {
  45      for (i=0; i<j; i++) puts(types[i]);
  46      error_exit("%s: unknown '%s'", cmd, toys.optargs[1]);
  47    }
  48
  49    request.u.name_type = i;
  50    request.cmd = SET_VLAN_NAME_TYPE_CMD;
  51    xioctl(fd, SIOCSIFVLAN, &request);
  52
  53    return;
  54  }
  55
  56  // Store interface name
  57  xstrncpy(request.device1, toys.optargs[1], sizeof(request.device1));
  58
  59  if (!strcmp(cmd, "add")) {
  60    request.cmd = ADD_VLAN_CMD;
  61    if (toys.optargs[2]) request.u.VID = atolx_range(toys.optargs[2], 0, 4094);
  62    if (request.u.VID == 1)
  63      xprintf("WARNING: VLAN 1 does not work with many switches.\n");
  64  } else if (!strcmp(cmd, "rem")) request.cmd = DEL_VLAN_CMD;
  65  else if (!strcmp(cmd, "set_flag")) {
  66    request.cmd = SET_VLAN_FLAG_CMD;
  67    if (toys.optargs[2]) request.u.flag = atolx_range(toys.optargs[2], 0, 1);
  68    if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
  69  } else if(strcmp(cmd, "set_egress_map") == 0) {
  70    request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
  71    if (toys.optargs[2])
  72      request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
  73    if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
  74  } else if(strcmp(cmd, "set_ingress_map") == 0) {
  75    request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
  76    if (toys.optargs[2])
  77      request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
  78    //To set flag we must have to set vlan_qos
  79    if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
  80  } else {
  81    xclose(fd);
  82    perror_exit("Unknown command %s", cmd);
  83  }
  84
  85  xioctl(fd, SIOCSIFVLAN, &request);
  86  xprintf("Successful %s on device %s\n", cmd, toys.optargs[1]);
  87}
  88