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 "busybox.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[] = {
  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[] = {
  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[] = "/proc/net/vlan/config";
  97. int vconfig_main(int argc, char **argv)
  98. {
  99. struct vlan_ioctl_args ifr;
  100. const char *p;
  101. int fd;
  102. if (argc < 3) {
  103. bb_show_usage();
  104. }
  105. /* Don't bother closing the filedes. It will be closed on cleanup. */
  106. /* Will die if 802.1q is not present */
  107. xopen(conf_file_name, O_RDONLY);
  108. memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
  109. ++argv;
  110. p = xfind_str(cmds+2, *argv);
  111. ifr.cmd = *p;
  112. if (argc != p[-1]) {
  113. bb_show_usage();
  114. }
  115. if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
  116. ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
  117. } else {
  118. if (strlen(argv[1]) >= IF_NAMESIZE) {
  119. bb_error_msg_and_die("if_name >= %d chars", IF_NAMESIZE);
  120. }
  121. strcpy(ifr.device1, argv[1]);
  122. p = argv[2];
  123. /* I suppose one could try to combine some of the function calls below,
  124. * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
  125. * (unsigned) int members of a unions. But because of the range checking,
  126. * doing so wouldn't save that much space and would also make maintainence
  127. * more of a pain. */
  128. if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
  129. ifr.u.flag = xatoul_range(p, 0, 1);
  130. /* DM: in order to set reorder header, qos must be set */
  131. ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
  132. } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
  133. ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1);
  134. } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
  135. ifr.u.skb_priority = xatou(p);
  136. ifr.vlan_qos = xatoul_range(argv[3], 0, 7);
  137. }
  138. }
  139. fd = xsocket(AF_INET, SOCK_STREAM, 0);
  140. if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) {
  141. bb_perror_msg_and_die("ioctl error for %s", *argv);
  142. }
  143. return 0;
  144. }