dlt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 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 "dlt.h"
  20. #include "config.h"
  21. #include "tcpedit.h"
  22. #include <lib/sll.h>
  23. #include <assert.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. case DLT_LINUX_SLL:
  49. len = SLL_HDR_LEN;
  50. break;
  51. case DLT_LINUX_SLL2:
  52. len = SLL2_HDR_LEN;
  53. break;
  54. case DLT_PPP_SERIAL:
  55. case DLT_C_HDLC:
  56. len = 4;
  57. break;
  58. case DLT_JUNIPER_ETHER:
  59. len = 36;
  60. break;
  61. default:
  62. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  63. len = -1;
  64. }
  65. return len;
  66. }
  67. /**
  68. * each DLT type may require one or more user specified Layer 2 field
  69. * to be able to rewrite it as plain ethernet DLT_EN10MB
  70. * returns -1 on error or >= 0 on success
  71. */
  72. int
  73. dltrequires(tcpedit_t *tcpedit, int dlt)
  74. {
  75. assert(tcpedit);
  76. int req = TCPEDIT_DLT_OK; // no change required by default
  77. switch (dlt) {
  78. case DLT_JUNIPER_ETHER:
  79. case DLT_EN10MB:
  80. /* case DLT_USER:
  81. case DLT_VLAN: */
  82. /* we have everything we need in the original packet */
  83. break;
  84. case DLT_NULL:
  85. case DLT_RAW:
  86. case DLT_C_HDLC:
  87. case DLT_PPP_SERIAL:
  88. req = TCPEDIT_DLT_SRC + TCPEDIT_DLT_DST;
  89. /* we just have the proto */
  90. break;
  91. case DLT_LINUX_SLL:
  92. case DLT_LINUX_SLL2:
  93. /* we have proto & SRC address */
  94. req = TCPEDIT_DLT_DST;
  95. break;
  96. default:
  97. tcpedit_seterr(tcpedit, "Invalid DLT Type: %d", dlt);
  98. req = -1;
  99. }
  100. return req;
  101. }
  102. /**
  103. * returns the default MTU size for the given DLT type. Returns -1
  104. * for invalid DLT
  105. */
  106. int
  107. dlt2mtu(tcpedit_t *tcpedit, int dlt)
  108. {
  109. int mtu;
  110. assert(tcpedit);
  111. switch (dlt) {
  112. /* case DLT_VLAN:
  113. case DLT_USER: */
  114. case DLT_PPP_SERIAL:
  115. case DLT_EN10MB:
  116. case DLT_RAW:
  117. case DLT_C_HDLC:
  118. case DLT_JUNIPER_ETHER:
  119. mtu = 1500;
  120. break;
  121. case DLT_LINUX_SLL:
  122. case DLT_LINUX_SLL2:
  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, u_char *packet, uint32_t caplen)
  143. {
  144. assert(tcpedit);
  145. assert(tcpedit->dlt_ctx);
  146. assert(tcpedit->dlt_ctx->encoder);
  147. return tcpedit->dlt_ctx->encoder->plugin_l2len(tcpedit->dlt_ctx, packet, caplen);
  148. }