linuxsll.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 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 iif
  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 deocde extra data */
  90. if (sizeof(linuxsll_extra_t) > 0)
  91. ctx->decoded_extra = safe_malloc(sizeof(linuxsll_extra_t));
  92. /* allocate memory for our config data */
  93. if (sizeof(linuxsll_config_t) > 0)
  94. plugin->config = safe_malloc(sizeof(linuxsll_config_t));
  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. }
  116. if (plugin->config != NULL) {
  117. safe_free(plugin->config);
  118. plugin->config = NULL;
  119. }
  120. return TCPEDIT_OK; /* success */
  121. }
  122. /*
  123. * This is where you should define all your AutoGen AutoOpts option parsing.
  124. * Any user specified option should have it's bit turned on in the 'provides'
  125. * bit mask.
  126. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  127. */
  128. int
  129. dlt_linuxsll_parse_opts(tcpeditdlt_t *ctx)
  130. {
  131. assert(ctx);
  132. /* nothing to parse */
  133. return TCPEDIT_OK; /* success */
  134. }
  135. /*
  136. * Function to decode the layer 2 header in the packet.
  137. * You need to fill out:
  138. * - ctx->l2len
  139. * - ctx->srcaddr
  140. * - ctx->dstaddr
  141. * - ctx->proto
  142. * - ctx->decoded_extra
  143. * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
  144. */
  145. int
  146. dlt_linuxsll_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  147. {
  148. int type;
  149. linux_sll_header_t *linux_sll;
  150. assert(ctx);
  151. assert(packet);
  152. if (pktlen < (int)sizeof(linux_sll_header_t))
  153. return TCPEDIT_ERROR;
  154. linux_sll = (linux_sll_header_t *)packet;
  155. ctx->proto = linux_sll->proto;
  156. ctx->l2len = sizeof(linux_sll_header_t);
  157. type = ntohs(linux_sll->type);
  158. if (type == ARPHRD_ETHER || type == ARPHRD_LOOPBACK) { /* ethernet or loopback */
  159. memcpy(&(ctx->srcaddr), linux_sll->address, ETHER_ADDR_LEN);
  160. } else {
  161. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL pcap's must contain only ethernet or loopback packets");
  162. return TCPEDIT_ERROR;
  163. }
  164. return TCPEDIT_OK; /* success */
  165. }
  166. /*
  167. * Function to encode the layer 2 header back into the packet.
  168. * Returns: total packet len or TCPEDIT_ERROR
  169. */
  170. int
  171. dlt_linuxsll_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
  172. {
  173. assert(ctx);
  174. assert(packet);
  175. tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL plugin does not support packet encoding");
  176. return TCPEDIT_ERROR;
  177. }
  178. /*
  179. * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
  180. */
  181. int
  182. dlt_linuxsll_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  183. {
  184. linux_sll_header_t *linux_sll;
  185. assert(ctx);
  186. assert(packet);
  187. if (pktlen < (int)sizeof(linux_sll_header_t))
  188. return TCPEDIT_ERROR;
  189. linux_sll = (linux_sll_header_t *)packet;
  190. return linux_sll->proto;
  191. }
  192. /*
  193. * Function returns a pointer to the layer 3 protocol header or NULL on error
  194. */
  195. u_char *
  196. dlt_linuxsll_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
  197. {
  198. int l2len;
  199. assert(ctx);
  200. assert(packet);
  201. l2len = dlt_linuxsll_l2len(ctx, packet, pktlen);
  202. if (pktlen < l2len)
  203. return NULL;
  204. return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
  205. }
  206. /*
  207. * function merges the packet (containing L2 and old L3) with the l3data buffer
  208. * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
  209. * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
  210. * like SPARC
  211. */
  212. u_char *
  213. dlt_linuxsll_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
  214. {
  215. int l2len;
  216. assert(ctx);
  217. assert(packet);
  218. assert(l3data);
  219. l2len = dlt_linuxsll_l2len(ctx, packet, pktlen);
  220. if (pktlen < l2len)
  221. return NULL;
  222. return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
  223. }
  224. /*
  225. * return the length of the L2 header of the current packet
  226. */
  227. int
  228. dlt_linuxsll_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
  229. {
  230. assert(ctx);
  231. assert(packet);
  232. if (pktlen < (int)sizeof(linux_sll_header_t))
  233. return 0;
  234. return sizeof(linux_sll_header_t);
  235. }
  236. /*
  237. * return a static pointer to the source/destination MAC address
  238. * return NULL on error/address doesn't exist
  239. */
  240. u_char *
  241. dlt_linuxsll_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
  242. {
  243. assert(ctx);
  244. assert(packet);
  245. if (pktlen < 14)
  246. return NULL;
  247. /* FIXME: return a ptr to the source or dest mac address. */
  248. switch(mac) {
  249. case SRC_MAC:
  250. memcpy(ctx->srcmac, &packet[6], 8); /* linuxssl defines the src mac field to be 8 bytes, not 6 */
  251. return(ctx->srcmac);
  252. break;
  253. case DST_MAC:
  254. return(NULL);
  255. break;
  256. default:
  257. errx(-1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
  258. }
  259. return(NULL);
  260. }
  261. tcpeditdlt_l2addr_type_t
  262. dlt_linuxsll_l2addr_type(void)
  263. {
  264. /* we only support ethernet packets */
  265. return ETHERNET;
  266. }