vconfig.c 4.4 KB

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