dlt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <assert.h>
  20. #include "config.h"
  21. #include "defines.h"
  22. #include "tcpedit.h"
  23. #include "dlt.h"
  24. /**
  25. * takes in a libpcap DLT_ type and returns the length of the layer2 header
  26. * returns -1 for unsupported DLT
  27. */
  28. int
  29. dlt2layer2len(tcpedit_t *tcpedit, int dlt)
  30. {
  31. assert(tcpedit);
  32. int len;
  33. switch(dlt) {
  34. /*
  35. case DLT_USER:
  36. len = tcpedit->l2.len;
  37. break;
  38. */
  39. case DLT_NULL:
  40. len = 2;
  41. break;
  42. case DLT_RAW:
  43. len = 0;
  44. break;
  45. case DLT_EN10MB:
  46. len = 12;
  47. break;
  48. /*
  49. case DLT_VLAN:
  50. len = 14;
  51. break;
  52. */
  53. case DLT_LINUX_SLL:
  54. len = 16;
  55. break;
  56. case DLT_PPP_SERIAL:
  57. case DLT_C_HDLC:
  58. len = 4;
  59. break;
  60. case DLT_JUNIPER_ETHER:
  61. len = 36;
  62. break;
  63. default:
  64. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  65. len = -1;
  66. }
  67. return len;
  68. }
  69. /**
  70. * each DLT type may require one or more user specified Layer 2 field
  71. * to be able to rewrite it as plain ethernet DLT_EN10MB
  72. * returns -1 on error or >= 0 on success
  73. */
  74. int
  75. dltrequires(tcpedit_t *tcpedit, int dlt)
  76. {
  77. assert(tcpedit);
  78. int req = TCPEDIT_DLT_OK; // no change required by default
  79. switch(dlt) {
  80. case DLT_JUNIPER_ETHER:
  81. case DLT_EN10MB:
  82. /* case DLT_USER:
  83. case DLT_VLAN: */
  84. /* we have everything we need in the original packet */
  85. break;
  86. case DLT_NULL:
  87. case DLT_RAW:
  88. case DLT_C_HDLC:
  89. case DLT_PPP_SERIAL:
  90. req = TCPEDIT_DLT_SRC + TCPEDIT_DLT_DST;
  91. /* we just have the proto */
  92. break;
  93. case DLT_LINUX_SLL:
  94. /* we have proto & SRC address */
  95. req = TCPEDIT_DLT_DST;
  96. break;
  97. default:
  98. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  99. req = -1;
  100. }
  101. return req;
  102. }
  103. /**
  104. * returns the default MTU size for the given DLT type. Returns -1
  105. * for invalid DLT
  106. */
  107. int
  108. dlt2mtu(tcpedit_t *tcpedit, int dlt)
  109. {
  110. int mtu;
  111. assert(tcpedit);
  112. switch (dlt) {
  113. /* case DLT_VLAN:
  114. case DLT_USER: */
  115. case DLT_PPP_SERIAL:
  116. case DLT_EN10MB:
  117. case DLT_RAW:
  118. case DLT_C_HDLC:
  119. case DLT_JUNIPER_ETHER:
  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. return tcpedit->dlt_ctx->l2len;
  146. }