vconfig.c 2.8 KB

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