vconfig.c 4.4 KB

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