linuxsll.c 9.2 KB

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