vconfig.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * vconfig implementation for busybox
  4. *
  5. * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config VCONFIG
  10. //config: bool "vconfig (2.5 kb)"
  11. //config: default y
  12. //config: select PLATFORM_LINUX
  13. //config: help
  14. //config: Creates, removes, and configures VLAN interfaces
  15. //applet:IF_VCONFIG(APPLET_NOEXEC(vconfig, vconfig, BB_DIR_SBIN, BB_SUID_DROP, vconfig))
  16. //kbuild:lib-$(CONFIG_VCONFIG) += vconfig.o
  17. //usage:#define vconfig_trivial_usage
  18. //usage: "COMMAND [OPTIONS]"
  19. //usage:#define vconfig_full_usage "\n\n"
  20. //usage: "Create and remove virtual ethernet devices\n"
  21. //usage: "\n add IFACE VLAN_ID"
  22. //usage: "\n rem VLAN_NAME"
  23. //usage: "\n set_flag IFACE 0|1 VLAN_QOS"
  24. //usage: "\n set_egress_map VLAN_NAME SKB_PRIO VLAN_QOS"
  25. //usage: "\n set_ingress_map VLAN_NAME SKB_PRIO VLAN_QOS"
  26. //usage: "\n set_name_type NAME_TYPE"
  27. #include "libbb.h"
  28. #include <net/if.h>
  29. /* BB_AUDIT SUSv3 N/A */
  30. /* Stuff from linux/if_vlan.h, kernel version 2.4.23 */
  31. enum vlan_ioctl_cmds {
  32. ADD_VLAN_CMD,
  33. DEL_VLAN_CMD,
  34. SET_VLAN_INGRESS_PRIORITY_CMD,
  35. SET_VLAN_EGRESS_PRIORITY_CMD,
  36. GET_VLAN_INGRESS_PRIORITY_CMD,
  37. GET_VLAN_EGRESS_PRIORITY_CMD,
  38. SET_VLAN_NAME_TYPE_CMD,
  39. SET_VLAN_FLAG_CMD
  40. };
  41. enum vlan_name_types {
  42. VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */
  43. VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */
  44. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */
  45. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */
  46. VLAN_NAME_TYPE_HIGHEST
  47. };
  48. struct vlan_ioctl_args {
  49. int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */
  50. char device1[24];
  51. union {
  52. char device2[24];
  53. int VID;
  54. unsigned int skb_priority;
  55. unsigned int name_type;
  56. unsigned int bind_type;
  57. unsigned int flag; /* Matches vlan_dev_info flags */
  58. } u;
  59. short vlan_qos;
  60. };
  61. #define VLAN_GROUP_ARRAY_LEN 4096
  62. #define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
  63. /* On entry, table points to the length of the current string
  64. * plus NUL terminator plus data length for the subsequent entry.
  65. * The return value is the last data entry for the matching string. */
  66. static const char *xfind_str(const char *table, const char *str)
  67. {
  68. while (strcasecmp(str, table + 1) != 0) {
  69. if (!table[0])
  70. bb_show_usage();
  71. table += table[0];
  72. }
  73. return table - 1;
  74. }
  75. static const char cmds[] ALIGN1 = {
  76. 4, ADD_VLAN_CMD, 7,
  77. 'a','d','d',0,
  78. 3, DEL_VLAN_CMD, 7,
  79. 'r','e','m',0,
  80. 3, SET_VLAN_NAME_TYPE_CMD, 17,
  81. 's','e','t','_','n','a','m','e','_','t','y','p','e',0,
  82. 5, SET_VLAN_FLAG_CMD, 12,
  83. 's','e','t','_','f','l','a','g',0,
  84. 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
  85. 's','e','t','_','e','g','r','e','s','s','_','m','a','p',0,
  86. 5, SET_VLAN_INGRESS_PRIORITY_CMD, 0,
  87. 's','e','t','_','i','n','g','r','e','s','s','_','m','a','p',0,
  88. };
  89. static const char name_types[] ALIGN1 = {
  90. VLAN_NAME_TYPE_PLUS_VID, 16,
  91. 'V','L','A','N','_','P','L','U','S','_','V','I','D',0,
  92. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
  93. 'V','L','A','N','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0,
  94. VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
  95. 'D','E','V','_','P','L','U','S','_','V','I','D',0,
  96. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 0,
  97. 'D','E','V','_','P','L','U','S','_','V','I','D','_','N','O','_','P','A','D',0,
  98. };
  99. int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  100. int vconfig_main(int argc, char **argv)
  101. {
  102. struct vlan_ioctl_args ifr;
  103. const char *p;
  104. int fd;
  105. memset(&ifr, 0, sizeof(ifr));
  106. ++argv;
  107. if (!argv[0])
  108. bb_show_usage();
  109. p = xfind_str(cmds + 2, argv[0]);
  110. ifr.cmd = *p;
  111. if (argc != p[-1])
  112. bb_show_usage();
  113. if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) {
  114. /* set_name_type */
  115. ifr.u.name_type = *xfind_str(name_types + 1, argv[1]);
  116. } else {
  117. strncpy_IFNAMSIZ(ifr.device1, argv[1]);
  118. p = argv[2];
  119. /* I suppose one could try to combine some of the function calls below,
  120. * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
  121. * (unsigned) int members of a unions. But because of the range checking,
  122. * doing so wouldn't save that much space and would also make maintenance
  123. * more of a pain.
  124. */
  125. if (ifr.cmd == SET_VLAN_FLAG_CMD) {
  126. /* set_flag */
  127. ifr.u.flag = xatou_range(p, 0, 1);
  128. /* DM: in order to set reorder header, qos must be set */
  129. ifr.vlan_qos = xatou_range(argv[3], 0, 7);
  130. } else if (ifr.cmd == ADD_VLAN_CMD) {
  131. /* add */
  132. ifr.u.VID = xatou_range(p, 0, VLAN_GROUP_ARRAY_LEN - 1);
  133. } else if (ifr.cmd != DEL_VLAN_CMD) {
  134. /* set_{egress|ingress}_map */
  135. ifr.u.skb_priority = xatou(p);
  136. ifr.vlan_qos = xatou_range(argv[3], 0, 7);
  137. }
  138. }
  139. fd = xsocket(AF_INET, SOCK_STREAM, 0);
  140. ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
  141. "ioctl error for %s", argv[0]);
  142. return 0;
  143. }