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