vconfig.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 [interface-name] [vlan_id]"
  15. //usage: "\n rem [vlan-name]"
  16. //usage: "\n set_flag [interface-name] [flag-num] [0 | 1]"
  17. //usage: "\n set_egress_map [vlan-name] [skb_priority] [vlan_qos]"
  18. //usage: "\n set_ingress_map [vlan-name] [skb_priority] [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. table += table[0];
  62. if (!*table) {
  63. bb_show_usage();
  64. }
  65. }
  66. return table - 1;
  67. }
  68. static const char cmds[] ALIGN1 = {
  69. 4, ADD_VLAN_CMD, 7,
  70. 'a', 'd', 'd', 0,
  71. 3, DEL_VLAN_CMD, 7,
  72. 'r', 'e', 'm', 0,
  73. 3, SET_VLAN_NAME_TYPE_CMD, 17,
  74. 's', 'e', 't', '_',
  75. 'n', 'a', 'm', 'e', '_',
  76. 't', 'y', 'p', 'e', 0,
  77. 5, SET_VLAN_FLAG_CMD, 12,
  78. 's', 'e', 't', '_',
  79. 'f', 'l', 'a', 'g', 0,
  80. 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18,
  81. 's', 'e', 't', '_',
  82. 'e', 'g', 'r', 'e', 's', 's', '_',
  83. 'm', 'a', 'p', 0,
  84. 5, SET_VLAN_INGRESS_PRIORITY_CMD, 16,
  85. 's', 'e', 't', '_',
  86. 'i', 'n', 'g', 'r', 'e', 's', 's', '_',
  87. 'm', 'a', 'p', 0,
  88. };
  89. static const char name_types[] ALIGN1 = {
  90. VLAN_NAME_TYPE_PLUS_VID, 16,
  91. 'V', 'L', 'A', 'N',
  92. '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
  93. 0,
  94. VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22,
  95. 'V', 'L', 'A', 'N',
  96. '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
  97. '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
  98. VLAN_NAME_TYPE_RAW_PLUS_VID, 15,
  99. 'D', 'E', 'V',
  100. '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
  101. 0,
  102. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, 20,
  103. 'D', 'E', 'V',
  104. '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D',
  105. '_', 'N', 'O', '_', 'P', 'A', 'D', 0,
  106. };
  107. static const char conf_file_name[] ALIGN1 = "/proc/net/vlan/config";
  108. int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  109. int vconfig_main(int argc, char **argv)
  110. {
  111. struct vlan_ioctl_args ifr;
  112. const char *p;
  113. int fd;
  114. if (argc < 3) {
  115. bb_show_usage();
  116. }
  117. /* Don't bother closing the filedes. It will be closed on cleanup. */
  118. /* Will die if 802.1q is not present */
  119. xopen(conf_file_name, O_RDONLY);
  120. memset(&ifr, 0, sizeof(ifr));
  121. ++argv;
  122. p = xfind_str(cmds+2, *argv);
  123. ifr.cmd = *p;
  124. if (argc != p[-1]) {
  125. bb_show_usage();
  126. }
  127. if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
  128. ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
  129. } else {
  130. strncpy_IFNAMSIZ(ifr.device1, argv[1]);
  131. p = argv[2];
  132. /* I suppose one could try to combine some of the function calls below,
  133. * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
  134. * (unsigned) int members of a unions. But because of the range checking,
  135. * doing so wouldn't save that much space and would also make maintainence
  136. * more of a pain. */
  137. if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
  138. ifr.u.flag = xatoul_range(p, 0, 1);
  139. /* DM: in order to set reorder header, qos must be set */
  140. ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
  141. } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
  142. ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
  143. } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
  144. ifr.u.skb_priority = xatou(p);
  145. ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
  146. }
  147. }
  148. fd = xsocket(AF_INET, SOCK_STREAM, 0);
  149. ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr,
  150. "ioctl error for %s", *argv);
  151. return 0;
  152. }