3
0

vconfig.c 4.7 KB

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