linuxsll.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "../ethernet.h"
  27. #include "linuxsll.h"
  28. static char dlt_name[] = "linuxsll";
  29. static char _U_ dlt_prefix[] = "linuxsll";
  30. static uint16_t dlt_value = DLT_LINUX_SLL;
  31. /*
  32. * Function to register ourselves. This function is always called, regardless
  33. * of what DLT types are being used, so it shouldn't be allocating extra buffers
  34. * or anything like that (use the dlt_linuxsll_init() function below for that).
  35. * Tasks:
  36. * - Create a new plugin struct
  37. * - Fill out the provides/requires bit masks. Note: Only specify which fields are
  38. * actually in the header.
  39. * - Add the plugin to the context's plugin chain
  40. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  41. */
  42. int
  43. dlt_linuxsll_register(tcpeditdlt_t *ctx)
  44. {
  45. tcpeditdlt_plugin_t *plugin;
  46. assert(ctx);
  47. /* create a new plugin structure */
  48. plugin = tcpedit_dlt_newplugin();
  49. /* FIXME: set what we provide & require */
  50. plugin->provides += PLUGIN_MASK_PROTO + PLUGIN_MASK_SRCADDR;
  51. plugin->requires += 0;
  52. /* what is our DLT value? */
  53. plugin->dlt = dlt_value;
  54. /* set the prefix name of our plugin. This is also used as the prefix for our options */
  55. plugin->name = safe_strdup(dlt_prefix);
  56. /*
  57. * Point to our functions, note, you need a function for EVERY method.
  58. * Even if it is only an empty stub returning success.
  59. */
  60. plugin->plugin_init = dlt_linuxsll_init;
  61. plugin->plugin_cleanup = dlt_linuxsll_cleanup;
  62. plugin->plugin_parse_opts = dlt_linuxsll_parse_opts;
  63. plugin->plugin_decode = dlt_linuxsll_decode;
  64. plugin->plugin_encode = dlt_linuxsll_encode;
  65. plugin->plugin_proto = dlt_linuxsll_proto;
  66. plugin->plugin_l2addr_type = dlt_linuxsll_l2addr_type;
  67. plugin->plugin_l2len = dlt_linuxsll_l2len;
  68. plugin->plugin_get_layer3 = dlt_linuxsll_get_layer3;
  69. plugin->plugin_merge_layer3 = dlt_linuxsll_merge_layer3;
  70. plugin->plugin_get_mac = dlt_linuxsll_get_mac;
  71. /* add it to the available plugin list */
  72. return tcpedit_dlt_addplugin(ctx, plugin);
  73. }
  74. /*
  75. * Initializer function. This function is called only once, if and only if
  76. * this plugin will be utilized. Remember, if you need to keep track of any state,
  77. * store it in your plugin->config, not a global!
  78. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  79. */
  80. int
  81. dlt_linuxsll_init(tcpeditdlt_t *ctx)
  82. {
  83. tcpeditdlt_plugin_t *plugin;
  84. assert(ctx);
  85. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  86. tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
  87. return TCPEDIT_ERROR;
  88. }
  89. /* allocate memory for our decode extra data */
  90. ctx->decoded_extra_size = sizeof(linuxsll_extra_t);
  91. ctx->decoded_extra = safe_malloc(ctx->decoded_extra_size);
  92. /* allocate memory for our config data */
  93. plugin->config_size = sizeof(linuxsll_config_t);
  94. plugin->config = safe_malloc(plugin->config_size);
  95. return TCPEDIT_OK; /* success */
  96. }
  97. /*
  98. * Since this is used in a library, we should manually clean up after ourselves
  99. * Unless you allocated some memory in dlt_linuxsll_init(), this is just an stub.
  100. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  101. */
  102. int
  103. dlt_linuxsll_cleanup(tcpeditdlt_t *ctx)
  104. {
  105. tcpeditdlt_plugin_t *plugin;
  106. assert(ctx);
  107. if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
  108. tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
  109. return TCPEDIT_ERROR;
  110. }
  111. /* FIXME: make this function do something if necessary */
  112. if (ctx->decoded_extra != NULL) {
  113. safe_free(ctx->decoded_extra);
  114. ctx->decoded_extra = NULL;
  115. ctx->decoded_extra_size = 0;
  116. }
  117. if (plugin->config != NULL) {
  118. safe_free(plugin->config);
  119. plugin->config = NULL;
  120. plugin->config_size = 0;
  121. }
  122. return TCPEDIT_OK; /* success */
  123. }
  124. /*
  125. * This is where you should define all your AutoGen AutoOpts option parsing.
  126. * Any user specified option should have it's bit turned on in the 'provides'
  127. * bit mask.
  128. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  129. */
  130. int
  131. dlt_linuxsll_parse_opts(tcpeditdlt_t *ctx)
  132. {
  133. assert(ctx);
  134. /* nothing to parse */
  135. return TCPEDIT_OK; /* success */
  136. }
  137. /*
  138. * Function to decode the layer 2 header in the packet.
  139. * You need to fill out:
  140. * - ctx->l2len
  141. * - ctx->srcaddr
  142. * - ctx->dstaddr
  143. * - ctx->proto
  144. * - ctx->decoded_extra
  145. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  146. */
  147. int
  148. dlt_linuxsll_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  149. {
  150. int type;
  151. linux_sll_header_t *linux_sll;
  152. assert(ctx);
  153. assert(packet);
  154. if (pktlen < (int)sizeof(linux_sll_header_t))
  155. return TCPEDIT_ERROR;
  156. linux_sll = (linux_sll_header_t *)packet;
  157. ctx->proto = linux_sll->proto;
  158. ctx->l2len = sizeof(linux_sll_header_t);
  159. type = ntohs(linux_sll->type);
  160. if (type == ARPHRD_ETHER || type == ARPHRD_LOOPBACK) { /* ethernet or loopback */
  161. memcpy(&(ctx->srcaddr), linux_sll->address, ETHER_ADDR_LEN);
  162. } else {
  163. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL pcap's must contain only ethernet or loopback packets");
  164. return TCPEDIT_ERROR;
  165. }
  166. return TCPEDIT_OK; /* success */
  167. }
  168. /*
  169. * Function to encode the layer 2 header back into the packet.
  170. * Returns: total packet len or TCPEDIT_ERROR
  171. */
  172. int
  173. dlt_linuxsll_encode(tcpeditdlt_t *ctx, u_char *packet, _U_ int pktlen,
  174. _U_ tcpr_dir_t dir)
  175. {
  176. assert(ctx);
  177. assert(packet);
  178. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL plugin does not support packet encoding");
  179. return TCPEDIT_ERROR;
  180. }
  181. /*
  182. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  183. */
  184. int
  185. dlt_linuxsll_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  186. {
  187. linux_sll_header_t *linux_sll;
  188. assert(ctx);
  189. assert(packet);
  190. if (pktlen < (int)sizeof(linux_sll_header_t))
  191. return TCPEDIT_ERROR;
  192. linux_sll = (linux_sll_header_t *)packet;
  193. return linux_sll->proto;
  194. }
  195. /*
  196. * Function returns a pointer to the layer 3 protocol header or NULL on error
  197. */
  198. u_char *
  199. dlt_linuxsll_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  200. {
  201. int l2len;
  202. assert(ctx);
  203. assert(packet);
  204. l2len = dlt_linuxsll_l2len(ctx, packet, pktlen);
  205. if (l2len == -1 || pktlen < l2len)
  206. return NULL;
  207. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  208. }
  209. /*
  210. * function merges the packet (containing L2 and old L3) with the l3data buffer
  211. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  212. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  213. * like SPARC
  214. */
  215. u_char *
  216. dlt_linuxsll_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  217. {
  218. int l2len;
  219. assert(ctx);
  220. assert(packet);
  221. assert(l3data);
  222. l2len = dlt_linuxsll_l2len(ctx, packet, pktlen);
  223. if (l2len == -1 || pktlen < l2len)
  224. return NULL;
  225. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  226. }
  227. /*
  228. * return the length of the L2 header of the current packet
  229. */
  230. int
  231. dlt_linuxsll_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  232. {
  233. assert(ctx);
  234. assert(packet);
  235. if (pktlen < (int)sizeof(linux_sll_header_t))
  236. return -1;
  237. return sizeof(linux_sll_header_t);
  238. }
  239. /*
  240. * return a static pointer to the source/destination MAC address
  241. * return NULL on error/address doesn't exist
  242. */
  243. u_char *
  244. dlt_linuxsll_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  245. {
  246. assert(ctx);
  247. assert(packet);
  248. if (pktlen < 14)
  249. return NULL;
  250. /* FIXME: return a ptr to the source or dest mac address. */
  251. switch(mac) {
  252. case SRC_MAC:
  253. memcpy(ctx->srcmac, &packet[6], 8); /* linuxssl defines the src mac field to be 8 bytes, not 6 */
  254. return(ctx->srcmac);
  255. break;
  256. case DST_MAC:
  257. return(NULL);
  258. break;
  259. default:
  260. errx(-1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
  261. }
  262. return(NULL);
  263. }
  264. tcpeditdlt_l2addr_type_t
  265. dlt_linuxsll_l2addr_type(void)
  266. {
  267. /* we only support ethernet packets */
  268. return ETHERNET;
  269. }