dlt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2006 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <assert.h>
  32. #include "config.h"
  33. #include "defines.h"
  34. #include "tcpedit.h"
  35. #include "dlt.h"
  36. /*
  37. * takes in a libpcap DLT_ type and returns the length of the layer2 header
  38. * returns -1 for unsupported DLT
  39. */
  40. int
  41. dlt2layer2len(tcpedit_t *tcpedit, int dlt)
  42. {
  43. assert(tcpedit);
  44. int len;
  45. switch(dlt) {
  46. case DLT_USER:
  47. len = tcpedit->l2.len;
  48. break;
  49. case DLT_NULL:
  50. len = 2;
  51. break;
  52. case DLT_RAW:
  53. len = 0;
  54. break;
  55. case DLT_EN10MB:
  56. len = 12;
  57. break;
  58. case DLT_VLAN:
  59. len = 14;
  60. break;
  61. case DLT_LINUX_SLL:
  62. len = 16;
  63. break;
  64. case DLT_C_HDLC:
  65. len = 4;
  66. break;
  67. default:
  68. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  69. len = -1;
  70. }
  71. return len;
  72. }
  73. /*
  74. * each DLT type may require one or more user specified Layer 2 field
  75. * to be able to rewrite it as plain ethernet DLT_EN10MB
  76. * returns -1 on error or >= 0 on success
  77. */
  78. int
  79. dltrequires(tcpedit_t *tcpedit, int dlt)
  80. {
  81. assert(tcpedit);
  82. int req = TCPEDIT_DLT_OK; // no change required by default
  83. switch(dlt) {
  84. case DLT_USER:
  85. case DLT_EN10MB:
  86. case DLT_VLAN:
  87. /* we have everthing we need in the original packet */
  88. break;
  89. case DLT_NULL:
  90. case DLT_RAW:
  91. case DLT_C_HDLC:
  92. req = TCPEDIT_DLT_SRC + TCPEDIT_DLT_DST;
  93. /* we just have the proto */
  94. break;
  95. case DLT_LINUX_SLL:
  96. /* we have proto & SRC address */
  97. req = TCPEDIT_DLT_DST;
  98. break;
  99. default:
  100. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  101. req = -1;
  102. }
  103. return req;
  104. }
  105. /*
  106. * returns the default MTU size for the given DLT type. Returns -1
  107. * for invalid DLT
  108. */
  109. int
  110. dlt2mtu(tcpedit_t *tcpedit, int dlt)
  111. {
  112. int mtu;
  113. assert(tcpedit);
  114. switch (dlt) {
  115. case DLT_EN10MB:
  116. case DLT_VLAN:
  117. case DLT_USER:
  118. case DLT_RAW:
  119. case DLT_C_HDLC:
  120. mtu = 1500;
  121. break;
  122. case DLT_LINUX_SLL:
  123. mtu = 16436;
  124. break;
  125. case DLT_LOOP:
  126. mtu = 16384;
  127. break;
  128. default:
  129. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  130. mtu = -1;
  131. break;
  132. }
  133. return mtu;
  134. }
  135. /*
  136. * Returns the current layer 2 len based on the
  137. * DLT of the pcap or the --dlink value or -1 on error.
  138. * You need to call this function AFTER rewriting the layer 2 header
  139. * for it to be at all useful.
  140. */
  141. int
  142. layer2len(tcpedit_t *tcpedit)
  143. {
  144. assert(tcpedit);
  145. if (tcpedit->l2.enabled)
  146. return tcpedit->l2.len;
  147. switch (tcpedit->l2.dlt) {
  148. case DLT_EN10MB:
  149. return 14;
  150. break;
  151. case DLT_VLAN:
  152. return 18;
  153. break;
  154. case DLT_RAW:
  155. case DLT_LOOP:
  156. return 0;
  157. break;
  158. case DLT_LINUX_SLL:
  159. return 16; // is this right?
  160. case DLT_USER:
  161. return tcpedit->l2.len;
  162. break;
  163. case DLT_C_HDLC:
  164. return 4;
  165. break;
  166. default:
  167. /* this shouldn't happen... fall back to the default */
  168. break;
  169. }
  170. tcpedit_seterr(tcpedit, "Unable to determine layer2len");
  171. return -1;
  172. }