dlt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* $Id: dlt.c 1895 2007-08-22 04:24:33Z aturner $ */
  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-int.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. /*
  47. case DLT_USER:
  48. len = tcpedit->l2.len;
  49. break;
  50. */
  51. case DLT_NULL:
  52. len = 2;
  53. break;
  54. case DLT_RAW:
  55. len = 0;
  56. break;
  57. case DLT_EN10MB:
  58. len = 12;
  59. break;
  60. /*
  61. case DLT_VLAN:
  62. len = 14;
  63. break;
  64. */
  65. case DLT_LINUX_SLL:
  66. len = 16;
  67. break;
  68. case DLT_C_HDLC:
  69. len = 4;
  70. break;
  71. default:
  72. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  73. len = -1;
  74. }
  75. return len;
  76. }
  77. /**
  78. * each DLT type may require one or more user specified Layer 2 field
  79. * to be able to rewrite it as plain ethernet DLT_EN10MB
  80. * returns -1 on error or >= 0 on success
  81. */
  82. int
  83. dltrequires(tcpedit_t *tcpedit, int dlt)
  84. {
  85. assert(tcpedit);
  86. int req = TCPEDIT_DLT_OK; // no change required by default
  87. switch(dlt) {
  88. case DLT_EN10MB:
  89. /* case DLT_USER:
  90. case DLT_VLAN: */
  91. /* we have everthing we need in the original packet */
  92. break;
  93. case DLT_NULL:
  94. case DLT_RAW:
  95. case DLT_C_HDLC:
  96. req = TCPEDIT_DLT_SRC + TCPEDIT_DLT_DST;
  97. /* we just have the proto */
  98. break;
  99. case DLT_LINUX_SLL:
  100. /* we have proto & SRC address */
  101. req = TCPEDIT_DLT_DST;
  102. break;
  103. default:
  104. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  105. req = -1;
  106. }
  107. return req;
  108. }
  109. /**
  110. * returns the default MTU size for the given DLT type. Returns -1
  111. * for invalid DLT
  112. */
  113. int
  114. dlt2mtu(tcpedit_t *tcpedit, int dlt)
  115. {
  116. int mtu;
  117. assert(tcpedit);
  118. switch (dlt) {
  119. /* case DLT_VLAN:
  120. case DLT_USER: */
  121. case DLT_EN10MB:
  122. case DLT_RAW:
  123. case DLT_C_HDLC:
  124. mtu = 1500;
  125. break;
  126. case DLT_LINUX_SLL:
  127. mtu = 16436;
  128. break;
  129. case DLT_LOOP:
  130. mtu = 16384;
  131. break;
  132. default:
  133. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  134. mtu = -1;
  135. break;
  136. }
  137. return mtu;
  138. }
  139. /**
  140. * Returns the current layer 2 len based on the
  141. * DLT of the pcap or the --dlink value or -1 on error.
  142. * You need to call this function AFTER rewriting the layer 2 header
  143. * for it to be at all useful.
  144. */
  145. int
  146. layer2len(tcpedit_t *tcpedit)
  147. {
  148. assert(tcpedit);
  149. return tcpedit->dlt_ctx->l2len;
  150. }