1 /* $Id$ */
2
3 /*
4 * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
5 * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
6 *
7 * The Tcpreplay Suite of tools is free software: you can redistribute it
8 * and/or modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or with the authors permission any later version.
11 *
12 * The Tcpreplay Suite is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "tcpedit.h"
25 #include "common.h"
26 #include "tcpr.h"
27 #include "dlt_utils.h"
28 #include "tcpedit_stub.h"
29 #include "hdlc.h"
30
31 static char dlt_name[] = "hdlc";
32 static char _U_ dlt_prefix[] = "hdlc";
33 static uint16_t dlt_value = DLT_C_HDLC;
34
35 /*
36 * Function to register ourselves. This function is always called, regardless
37 * of what DLT types are being used, so it shouldn't be allocating extra buffers
38 * or anything like that (use the dlt_hdlc_init() function below for that).
39 * Tasks:
40 * - Create a new plugin struct
41 * - Fill out the provides/requires bit masks. Note: Only specify which fields are
42 * actually in the header.
43 * - Add the plugin to the context's plugin chain
44 * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
45 */
46 int
dlt_hdlc_register(tcpeditdlt_t * ctx)47 dlt_hdlc_register(tcpeditdlt_t *ctx)
48 {
49 tcpeditdlt_plugin_t *plugin;
50 assert(ctx);
51
52 /* create a new plugin structure */
53 plugin = tcpedit_dlt_newplugin();
54
55 /* FIXME: set what we provide & require */
56 plugin->provides += PLUGIN_MASK_PROTO;
57 plugin->requires += PLUGIN_MASK_PROTO;
58
59 /* what is our DLT value? */
60 plugin->dlt = dlt_value;
61
62 /* set the prefix name of our plugin. This is also used as the prefix for our options */
63 plugin->name = safe_strdup(dlt_prefix);
64
65 /*
66 * Point to our functions, note, you need a function for EVERY method.
67 * Even if it is only an empty stub returning success.
68 */
69 plugin->plugin_init = dlt_hdlc_init;
70 plugin->plugin_cleanup = dlt_hdlc_cleanup;
71 plugin->plugin_parse_opts = dlt_hdlc_parse_opts;
72 plugin->plugin_decode = dlt_hdlc_decode;
73 plugin->plugin_encode = dlt_hdlc_encode;
74 plugin->plugin_proto = dlt_hdlc_proto;
75 plugin->plugin_l2addr_type = dlt_hdlc_l2addr_type;
76 plugin->plugin_l2len = dlt_hdlc_l2len;
77 plugin->plugin_get_layer3 = dlt_hdlc_get_layer3;
78 plugin->plugin_merge_layer3 = dlt_hdlc_merge_layer3;
79 plugin->plugin_get_mac = dlt_hdlc_get_mac;
80
81 /* add it to the available plugin list */
82 return tcpedit_dlt_addplugin(ctx, plugin);
83 }
84
85
86 /*
87 * Initializer function. This function is called only once, if and only if
88 * this plugin will be utilized. Remember, if you need to keep track of any state,
89 * store it in your plugin->config, not a global!
90 * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
91 */
92 int
dlt_hdlc_init(tcpeditdlt_t * ctx)93 dlt_hdlc_init(tcpeditdlt_t *ctx)
94 {
95 tcpeditdlt_plugin_t *plugin;
96 hdlc_config_t *config;
97 assert(ctx);
98
99 if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
100 tcpedit_seterr(ctx->tcpedit, "Unable to initialize unregistered plugin %s", dlt_name);
101 return TCPEDIT_ERROR;
102 }
103
104 /* allocate memory for our deocde extra data */
105 if (ctx->decoded_extra_size > 0) {
106 if (ctx->decoded_extra_size < sizeof(hdlc_extra_t)) {
107 ctx->decoded_extra_size = sizeof(hdlc_extra_t);
108 ctx->decoded_extra = safe_realloc(ctx->decoded_extra,
109 ctx->decoded_extra_size);
110 }
111 } else {
112 ctx->decoded_extra_size = sizeof(hdlc_extra_t);
113 ctx->decoded_extra = safe_malloc(ctx->decoded_extra_size);
114 }
115
116 /* allocate memory for our config data */
117 plugin->config_size = sizeof(hdlc_config_t);
118 plugin->config = safe_malloc(plugin->config_size);
119 config = (hdlc_config_t *)plugin->config;
120
121 /* default to unset */
122 config->address = 65535;
123 config->control = 65535;
124 return TCPEDIT_OK; /* success */
125 }
126
127 /*
128 * Since this is used in a library, we should manually clean up after ourselves
129 * Unless you allocated some memory in dlt_hdlc_init(), this is just an stub.
130 * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
131 */
132 int
dlt_hdlc_cleanup(tcpeditdlt_t * ctx)133 dlt_hdlc_cleanup(tcpeditdlt_t *ctx)
134 {
135 tcpeditdlt_plugin_t *plugin;
136 assert(ctx);
137
138 if ((plugin = tcpedit_dlt_getplugin(ctx, dlt_value)) == NULL) {
139 tcpedit_seterr(ctx->tcpedit, "Unable to cleanup unregistered plugin %s", dlt_name);
140 return TCPEDIT_ERROR;
141 }
142
143 safe_free(plugin->name);
144 plugin->name = NULL;
145 safe_free(plugin->config);
146 plugin->config = NULL;
147 plugin->config_size = 0;
148
149 return TCPEDIT_OK; /* success */
150 }
151
152 /*
153 * This is where you should define all your AutoGen AutoOpts option parsing.
154 * Any user specified option should have it's bit turned on in the 'provides'
155 * bit mask.
156 * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
157 */
158 int
dlt_hdlc_parse_opts(tcpeditdlt_t * ctx)159 dlt_hdlc_parse_opts(tcpeditdlt_t *ctx)
160 {
161 tcpeditdlt_plugin_t *plugin;
162 hdlc_config_t *config;
163 assert(ctx);
164
165
166 plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
167 if (!plugin)
168 return TCPEDIT_ERROR;
169
170 config = plugin->config;
171 if (plugin->config_size < sizeof(*config))
172 return TCPEDIT_ERROR;
173
174 if (HAVE_OPT(HDLC_CONTROL)) {
175 config->control = (uint16_t)OPT_VALUE_HDLC_CONTROL;
176 }
177
178 if (HAVE_OPT(HDLC_ADDRESS)) {
179 config->address = (uint16_t)OPT_VALUE_HDLC_ADDRESS;
180 }
181
182 return TCPEDIT_OK; /* success */
183 }
184
185 /*
186 * Function to decode the layer 2 header in the packet.
187 * You need to fill out:
188 * - ctx->l2len
189 * - ctx->srcaddr
190 * - ctx->dstaddr
191 * - ctx->proto
192 * - ctx->decoded_extra
193 * Returns: TCPEDIT_ERROR | TCPEDIT_OK | TCPEDIT_WARN
194 */
195 int
dlt_hdlc_decode(tcpeditdlt_t * ctx,const u_char * packet,const int pktlen)196 dlt_hdlc_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
197 {
198 cisco_hdlc_t *hdlc;
199 hdlc_extra_t *extra;
200 assert(ctx);
201 assert(packet);
202
203 if ((size_t)pktlen < sizeof(*hdlc))
204 return TCPEDIT_ERROR;
205
206 if (ctx->decoded_extra_size < sizeof(*extra))
207 return TCPEDIT_ERROR;
208
209 extra = (hdlc_extra_t *)ctx->decoded_extra;
210
211 hdlc = (cisco_hdlc_t *)packet;
212
213 ctx->proto = hdlc->protocol;
214 ctx->l2len = 4;
215
216 extra->address = hdlc->address;
217 extra->control = hdlc->control;
218
219 return TCPEDIT_OK; /* success */
220 }
221
222 /*
223 * Function to encode the layer 2 header back into the packet.
224 * Returns: total packet len or TCPEDIT_ERROR
225 */
226 int
dlt_hdlc_encode(tcpeditdlt_t * ctx,u_char * packet,int pktlen,_U_ tcpr_dir_t dir)227 dlt_hdlc_encode(tcpeditdlt_t *ctx, u_char *packet, int pktlen, _U_ tcpr_dir_t dir)
228 {
229 cisco_hdlc_t *hdlc;
230 hdlc_config_t *config = NULL;
231 hdlc_extra_t *extra = NULL;
232 tcpeditdlt_plugin_t *plugin = NULL;
233 int newpktlen;
234
235 assert(ctx);
236 assert(packet);
237
238 if ((size_t)pktlen < sizeof(*hdlc))
239 return TCPEDIT_ERROR;
240
241 if (ctx->decoded_extra_size < sizeof(*extra))
242 return TCPEDIT_ERROR;
243
244 /* Make room for our new l2 header if old l2len != 4 */
245 if (ctx->l2len > 4) {
246 memmove(packet + 4, packet + ctx->l2len, pktlen - ctx->l2len);
247 } else if (ctx->l2len < 4) {
248 u_char *tmpbuff = safe_malloc(pktlen);
249 memcpy(tmpbuff, packet, pktlen);
250 memcpy(packet + 4, (tmpbuff + ctx->l2len), pktlen - ctx->l2len);
251 safe_free(tmpbuff);
252 }
253
254 /* update the total packet length */
255 newpktlen = pktlen + 4 - ctx->l2len;
256
257 /*
258 * HDLC doesn't support direction, since we have no real src/dst addresses
259 * to deal with, so we just use the original packet data or option data
260 */
261 hdlc = (cisco_hdlc_t *)packet;
262 plugin = tcpedit_dlt_getplugin(ctx, dlt_value);
263 if (!plugin)
264 return TCPEDIT_ERROR;
265
266 config = plugin->config;
267 if (plugin->config_size < sizeof(*config))
268 return TCPEDIT_ERROR;
269
270 extra = (hdlc_extra_t *)ctx->decoded_extra;
271 if (ctx->decoded_extra_size < sizeof(*extra))
272 return TCPEDIT_ERROR;
273
274 /* set the address field */
275 if (config->address < 65535) {
276 hdlc->address = (uint8_t)config->address;
277 } else if (extra->hdlc) {
278 hdlc->address = extra->hdlc;
279 } else {
280 tcpedit_seterr(ctx->tcpedit, "%s", "Non-HDLC packet requires --hdlc-address");
281 return TCPEDIT_ERROR;
282 }
283
284 /* set the control field */
285 if (config->control < 65535) {
286 hdlc->control = (uint8_t)config->control;
287 } else if (extra->hdlc) {
288 hdlc->control = extra->hdlc;
289 } else {
290 tcpedit_seterr(ctx->tcpedit, "%s", "Non-HDLC packet requires --hdlc-control");
291 return TCPEDIT_ERROR;
292 }
293
294 /* copy over our protocol */
295 hdlc->protocol = ctx->proto;
296
297 return newpktlen; /* success */
298 }
299
300 /*
301 * Function returns the Layer 3 protocol type of the given packet, or TCPEDIT_ERROR on error
302 */
303 int
dlt_hdlc_proto(tcpeditdlt_t * ctx,const u_char * packet,const int pktlen)304 dlt_hdlc_proto(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
305 {
306 cisco_hdlc_t *hdlc;
307 assert(ctx);
308 assert(packet);
309
310 if (pktlen < 4)
311 return TCPEDIT_ERROR;
312
313 hdlc = (cisco_hdlc_t *)packet;
314
315 return hdlc->protocol;
316 }
317
318 /*
319 * Function returns a pointer to the layer 3 protocol header or NULL on error
320 */
321 u_char *
dlt_hdlc_get_layer3(tcpeditdlt_t * ctx,u_char * packet,const int pktlen)322 dlt_hdlc_get_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen)
323 {
324 int l2len;
325 assert(ctx);
326 assert(packet);
327
328 l2len = dlt_hdlc_l2len(ctx, packet, pktlen);
329 if (l2len == -1 || pktlen < l2len)
330 return NULL;
331
332 return tcpedit_dlt_l3data_copy(ctx, packet, pktlen, l2len);
333 }
334
335 /*
336 * function merges the packet (containing L2 and old L3) with the l3data buffer
337 * containing the new l3 data. Note, if L2 % 4 == 0, then they're pointing to the
338 * same buffer, otherwise there was a memcpy involved on strictly aligned architectures
339 * like SPARC
340 */
341 u_char *
dlt_hdlc_merge_layer3(tcpeditdlt_t * ctx,u_char * packet,const int pktlen,u_char * l3data)342 dlt_hdlc_merge_layer3(tcpeditdlt_t *ctx, u_char *packet, const int pktlen, u_char *l3data)
343 {
344 int l2len;
345 assert(ctx);
346 assert(packet);
347 assert(l3data);
348
349 l2len = dlt_hdlc_l2len(ctx, packet, pktlen);
350 if (l2len == -1 || pktlen < l2len)
351 return NULL;
352
353 return tcpedit_dlt_l3data_merge(ctx, packet, pktlen, l3data, l2len);
354 }
355
356 /*
357 * return the length of the L2 header of the current packet
358 */
359 int
dlt_hdlc_l2len(tcpeditdlt_t * ctx,const u_char * packet,const int pktlen)360 dlt_hdlc_l2len(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
361 {
362 assert(ctx);
363 assert(packet);
364
365 if (pktlen < 4)
366 return -1;
367
368 /* HDLC is a static 4 bytes */
369 return 4;
370 }
371
372 /*
373 * return a static pointer to the source/destination MAC address
374 * return NULL on error/address doesn't exist
375 */
376 u_char *
dlt_hdlc_get_mac(tcpeditdlt_t * ctx,tcpeditdlt_mac_type_t mac,const u_char * packet,const int pktlen)377 dlt_hdlc_get_mac(tcpeditdlt_t *ctx, tcpeditdlt_mac_type_t mac, const u_char *packet, const int pktlen)
378 {
379 assert(ctx);
380 assert(packet);
381
382 if (pktlen < 14)
383 return NULL;
384
385 /* FIXME: return a ptr to the source or dest mac address. */
386 switch(mac) {
387 case SRC_MAC:
388 return(NULL);
389 break;
390
391 case DST_MAC:
392 memcpy(ctx->dstmac, packet, 2);
393 return(ctx->dstmac);
394 break;
395
396 default:
397 errx(-1, "Invalid tcpeditdlt_mac_type_t: %d", mac);
398 }
399 return(NULL);
400 }
401
402 tcpeditdlt_l2addr_type_t
dlt_hdlc_l2addr_type(void)403 dlt_hdlc_l2addr_type(void)
404 {
405 return C_HDLC;
406 }
407
408