null.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <stdlib.h>
  20. #include <string.h>
  21. #include "tcpedit.h"
  22. #include "common.h"
  23. #include "tcpr.h"
  24. #include "dlt_utils.h"
  25. #include "tcpedit_stub.h"
  26. #include "null.h"
  27. #include <sys/socket.h> // PF_* values
  28. static char dlt_name[] = "null";
  29. static char _U_ dlt_prefix[] = "null";
  30. static uint16_t dlt_value = DLT_NULL;
  31. /*
  32. * From the libpcap man page:
  33. * DLT_NULL aka BSD loopback encapsulation; the link layer header is a 4-byte
  34. * field, in host byte order, containing a PF_ value from
  35. * socket.h for the network-layer protocol of the packet.
  36. *
  37. * Note that ``host byte order'' is the byte order of the
  38. * machine on which the packets are captured, and the PF_ values
  39. * are for the OS of the machine on which the packets are captured;
  40. * if a live capture is being done, ``host byte order''
  41. * is the byte order of the machine capturing the packets, and
  42. * the PF_ values are those of the OS of the machine capturing
  43. * the packets, but if a ``savefile'' is being read, the byte
  44. * order and PF_ values are not necessarily those of the machine
  45. * reading the capture file.
  46. */
  47. /*
  48. * Function to register ourselves. This function is always called, regardless
  49. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  50. * or anything like that (use the dlt_null_init() function below for that).
  51. * Tasks:
  52. * - Create a new plugin struct
  53. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  54. * actually in the header.
  55. * - Add the plugin to the context's plugin chain
  56. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  57. */
  58. int
  59. dlt_null_register(tcpeditdlt_t *ctx)
  60. {
  61. tcpeditdlt_plugin_t *plugin;
  62. assert(ctx);
  63. /* create a new plugin structure */
  64. plugin = tcpedit_dlt_newplugin();
  65. /* set what we provide & require */
  66. plugin->provides += PLUGIN_MASK_PROTO;
  67. plugin->requires += 0;
  68. /* what is our DLT value? */
  69. plugin->dlt = dlt_value;
  70. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  71. plugin->name = safe_strdup(dlt_prefix);
  72. /*
  73. * Point to our functions, note, you need a function for EVERY method.
  74. * Even if it is only an empty stub returning success.
  75. */
  76. plugin->plugin_init = dlt_null_init;
  77. plugin->plugin_cleanup = dlt_null_cleanup;
  78. plugin->plugin_parse_opts = dlt_null_parse_opts;
  79. plugin->plugin_decode = dlt_null_decode;
  80. plugin->plugin_encode = dlt_null_encode;
  81. plugin->plugin_proto = dlt_null_proto;
  82. plugin->plugin_l2addr_type = dlt_null_l2addr_type;
  83. plugin->plugin_l2len = dlt_null_l2len;
  84. plugin->plugin_get_layer3 = dlt_null_get_layer3;
  85. plugin->plugin_merge_layer3 = dlt_null_merge_layer3;
  86. /* add it to the available plugin list */
  87. return tcpedit_dlt_addplugin(ctx, plugin);
  88. }
  89. /*
  90. * Initializer function. This function is called only once, if and only if
  91. * this plugin will be utilized. Remember, if you need to keep track of any state,
  92. * store it in your plugin->config, not a global!
  93. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  94. */
  95. int
  96. dlt_null_init(tcpeditdlt_t *ctx)
  97. {
  98. tcpeditdlt_plugin_t *plugin;
  99. assert(ctx);
  100. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  101. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  102. return TCPEDIT_ERROR;
  103. }
  104. return TCPEDIT_OK; /* success */
  105. }
  106. /*
  107. * Since this is used in a library, we should manually clean up after ourselves
  108. * Unless you allocated some memory in dlt_null_init(), this is just an stub.
  109. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  110. */
  111. int
  112. dlt_null_cleanup(tcpeditdlt_t *ctx)
  113. {
  114. tcpeditdlt_plugin_t *plugin;
  115. assert(ctx);
  116. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  117. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  118. return TCPEDIT_ERROR;
  119. }
  120. safe_free(plugin->name);
  121. plugin->name = NULL;
  122. safe_free(plugin->config);
  123. plugin->config = NULL;
  124. plugin->config_size = 0;
  125. return TCPEDIT_OK; /* success */
  126. }
  127. /*
  128. * This is where you should define all your AutoGen AutoOpts option parsing.
  129. * Any user specified option should have it's bit turned on in the 'provides'
  130. * bit mask.
  131. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  132. */
  133. int
  134. dlt_null_parse_opts(tcpeditdlt_t *ctx)
  135. {
  136. assert(ctx);
  137. /* nothing to parse here, move along */
  138. return TCPEDIT_OK; /* success */
  139. }
  140. /*
  141. * Function to decode the layer 2 header in the packet.
  142. * You need to fill out:
  143. * - ctx->l2len
  144. * - ctx->srcaddr
  145. * - ctx->dstaddr
  146. * - ctx->proto
  147. * - ctx->decoded_extra
  148. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  149. */
  150. int
  151. dlt_null_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  152. {
  153. int proto;
  154. assert(ctx);
  155. assert(packet);
  156. if ((proto = dlt_null_proto(ctx, packet, pktlen)) == TCPEDIT_ERROR)
  157. return TCPEDIT_ERROR;
  158. ctx->proto = (uint16_t)proto;
  159. ctx->l2len = 4;
  160. return TCPEDIT_OK; /* success */
  161. }
  162. /*
  163. * Function to encode the layer 2 header back into the packet.
  164. * Returns: total packet len or TCPEDIT_ERROR
  165. */
  166. int
  167. dlt_null_encode(tcpeditdlt_t *ctx, u_char *packet, _U_ int pktlen,
  168. _U_ tcpr_dir_t dir)
  169. {
  170. assert(ctx);
  171. assert(packet);
  172. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_NULL and DLT_LOOP plugins do not support packet encoding");
  173. return TCPEDIT_ERROR;
  174. }
  175. /*
  176. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  177. */
  178. int
  179. dlt_null_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  180. {
  181. assert(ctx);
  182. assert(packet);
  183. uint32_t *af_type;
  184. int protocol = 0;
  185. if (pktlen < 4)
  186. return TCPEDIT_ERROR;
  187. /* PF_INET is always 2 but PF_INET6 varies based on platform, i.e
  188. * Linux - 10
  189. * NetBSD,OpenBSD,BSD/OS - 24
  190. * NetBSD,OpenBSD,BSD/OS - 28
  191. * Darwin/macOS - 30
  192. * See https://gitlab.com/wireshark/wireshark/-/wikis/NullLoopback
  193. */
  194. af_type = (uint32_t *)packet;
  195. if (*af_type == PF_INET || SWAPLONG(*af_type) == PF_INET) {
  196. protocol = ETHERTYPE_IP;
  197. } else if (*af_type == PF_INET6 || SWAPLONG(*af_type) == PF_INET6 ||
  198. *af_type == 10 || SWAPLONG(*af_type) == 10 ||
  199. *af_type == 24 || SWAPLONG(*af_type) == 24 ||
  200. *af_type == 28 || SWAPLONG(*af_type) == 28 ||
  201. *af_type == 30 || SWAPLONG(*af_type) == 30) {
  202. protocol = ETHERTYPE_IP6;
  203. } else {
  204. tcpedit_seterr(ctx->tcpedit, "Unsupported DLT_NULL/DLT_LOOP PF_ type: 0x%04x", *af_type);
  205. return TCPEDIT_ERROR;
  206. }
  207. return htons(protocol);
  208. }
  209. /*
  210. * Function returns a pointer to the layer 3 protocol header or NULL on error
  211. */
  212. u_char *
  213. dlt_null_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  214. {
  215. int l2len;
  216. assert(ctx);
  217. assert(packet);
  218. l2len = dlt_null_l2len(ctx, packet, pktlen);
  219. if (pktlen < l2len)
  220. return NULL;
  221. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  222. }
  223. /*
  224. * function merges the packet (containing L2 and old L3) with the l3data buffer
  225. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  226. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  227. * like SPARC
  228. */
  229. u_char *
  230. dlt_null_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  231. {
  232. int l2len;
  233. assert(ctx);
  234. assert(packet);
  235. assert(l3data);
  236. l2len = dlt_null_l2len(ctx, packet, pktlen);
  237. if (pktlen < l2len)
  238. return NULL;
  239. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  240. }
  241. /*
  242. * return the length of the L2 header of the current packet
  243. */
  244. int
  245. dlt_null_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  246. {
  247. assert(ctx);
  248. assert(packet);
  249. if (pktlen < 4)
  250. return 0;
  251. /* always is 4 */
  252. return 4;
  253. }
  254. /*
  255. * return a static pointer to the source/destination MAC address
  256. * return NULL on error/address doesn't exist
  257. */
  258. u_char *
  259. dlt_null_get_mac(tcpeditdlt_t *ctx, _U_ tcpeditdlt_mac_type_t mac,
  260. const u_char *packet, _U_ const int pktlen)
  261. {
  262. assert(ctx);
  263. assert(packet);
  264. return(NULL);
  265. }
  266. tcpeditdlt_l2addr_type_t
  267. dlt_null_l2addr_type(void)
  268. {
  269. return NONE;
  270. }