summaryrefslogtreecommitdiff
path: root/src/tcpedit/plugins/dlt_plugins.c
blob: 7aaad34ab620dfeb0481b2442745167abb31d7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* $Id: dlt_plugins.c 2423 2010-03-13 07:09:49Z aturner $ */

/*
 * Copyright (c) 2006-2010 Aaron Turner.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright owners nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include <stdlib.h>

#include "dlt_plugins-int.h"
#include "dlt_utils.h"
#include "common.h"

/**
 * Include plugin header files here...
 */
#include "dlt_en10mb/en10mb.h"
#include "dlt_user/user.h"
#include "dlt_hdlc/hdlc.h"
#include "dlt_raw/raw.h"
#include "dlt_null/null.h"
#include "dlt_loop/loop.h"
#include "dlt_linuxsll/linuxsll.h"
#include "dlt_ieee80211/ieee80211.h"
#include "dlt_radiotap/radiotap.h"


/**
 * Everyone writing a DLT plugin, must add their registration function
 * here.
 */
int 
tcpedit_dlt_register(tcpeditdlt_t *ctx)
{
    int retcode = 0;
    assert(ctx);
    
    retcode += dlt_en10mb_register(ctx);
    retcode += dlt_hdlc_register(ctx);
    retcode += dlt_user_register(ctx);
    retcode += dlt_raw_register(ctx);
    retcode += dlt_null_register(ctx);
    retcode += dlt_loop_register(ctx);
    retcode += dlt_linuxsll_register(ctx);
    retcode += dlt_ieee80211_register(ctx);
    retcode += dlt_radiotap_register(ctx);
    
    if (retcode < 0)
        return TCPEDIT_ERROR;
    
    return TCPEDIT_OK;
}



/********************************************************************
 * People writing DLT plugins should stop editing here!
 *
 * Well actually, that's true most of the time, but feel free to take
 * a look!
 ********************************************************************/

/* 
 * mapping for bit_mask to bit_info.  If you're making changes here
 * then you almost certainly need to modify tcpeditdlt_t in dlt_plugins-int.h
 */
const u_int32_t tcpeditdlt_bit_map[] = {
    PLUGIN_MASK_PROTO,
    PLUGIN_MASK_SRCADDR,
    PLUGIN_MASK_DSTADDR
};

/* Meanings of the above map */
const char *tcpeditdlt_bit_info[] = {
    "Missing required Layer 3 protocol.",
    "Missing required Layer 2 source address.",
    "Missing required Layer 2 destination address."
};
 
/*********************************************************************
 * Internal functions
 ********************************************************************/

/*********************************************************************
 * Public functions
 ********************************************************************/
 
/**
 * initialize our plugin library.  Pass the DLT of the source pcap handle.
 * Actions:
 * - Create new tcpeditdlt_t context
 * - Link tcpedit to new context
 * - Register plugins
 * - Select decoder plugin using srcdlt
 * - Select encoder plugin using destination name
 * - Initialize decoder/encoder plugins
 * - Parse options for encoder plugin
 * - Validate provides/reqiures + user options
 */
tcpeditdlt_t *
tcpedit_dlt_init(tcpedit_t *tcpedit, const int srcdlt) 
{
    tcpeditdlt_t *ctx;
    int rcode;
    const char *dst_dlt_name = NULL;

    assert(tcpedit);
    assert(srcdlt >= 0);

    ctx = (tcpeditdlt_t *)safe_malloc(sizeof(tcpeditdlt_t));

    /* do we need a side buffer for L3 data? */
#ifdef FORCE_ALIGN
    ctx->l3buff = (u_char *)safe_malloc(MAXPACKET);
#endif

    /* copy our tcpedit context */
    ctx->tcpedit = tcpedit;

    /* register all our plugins */
    if (tcpedit_dlt_register(ctx) != TCPEDIT_OK) {
        goto INIT_ERROR;
    }

    /* Choose decode plugin */
    if ((ctx->decoder = tcpedit_dlt_getplugin(ctx, srcdlt)) == NULL) {
        tcpedit_seterr(tcpedit, "No DLT plugin available for source DLT: 0x%x", srcdlt);
        goto INIT_ERROR;
    }

    /* set our dlt type */
    ctx->dlt = srcdlt;

    /* set our address type */
    ctx->addr_type = ctx->decoder->plugin_l2addr_type();

    /* initalize decoder plugin */
    rcode = ctx->decoder->plugin_init(ctx);
    if (tcpedit_checkerror(ctx->tcpedit, rcode, NULL) != TCPEDIT_OK) {
        goto INIT_ERROR;
    }

    /* Select the encoder plugin */
    dst_dlt_name = OPT_ARG(DLT) ? OPT_ARG(DLT) : ctx->decoder->name;
    if ((ctx->encoder = tcpedit_dlt_getplugin_byname(ctx, dst_dlt_name)) == NULL) {
        tcpedit_seterr(tcpedit, "No output DLT plugin available for: %s", dst_dlt_name);
        goto INIT_ERROR;
    }
    
    /* Figure out if we're skipping braodcast & multicast */
    if (HAVE_OPT(SKIPL2BROADCAST))
        ctx->skip_broadcast = 1;

    /* init encoder plugin if it's not the decoder plugin */
    if (ctx->encoder->dlt != ctx->decoder->dlt) {
        rcode = ctx->encoder->plugin_init(ctx);
        if (tcpedit_checkerror(ctx->tcpedit, rcode, NULL) != TCPEDIT_OK) {
            goto INIT_ERROR;
        }
    }

    /* parse the DLT specific options */
    rcode = tcpedit_dlt_parse_opts(ctx);
    if (tcpedit_checkerror(ctx->tcpedit, rcode, "parsing options") != TCPEDIT_OK) {
        goto INIT_ERROR;
    }


    /* validate that the SRC/DST DLT + options give us enough info */
    rcode = tcpedit_dlt_validate(ctx);
    if (tcpedit_checkerror(ctx->tcpedit, rcode, "validating options") != TCPEDIT_OK) {
        goto INIT_ERROR;
    }

    /* we're OK */
    return ctx;

INIT_ERROR:
    tcpedit_dlt_cleanup(ctx);
    return NULL;
}
 

/**
 * This is the recommended method to edit a packet.  Returns (new) total packet length
 * FIXME: This is *broken*.  taking packet as a u_char*, but using it as a u_char **!
 */
int
tcpedit_dlt_process(tcpeditdlt_t *ctx, u_char **packet, int pktlen, tcpr_dir_t direction)
{
    int rcode;
    
    assert(ctx);
    assert(packet);
    assert(pktlen);
    assert(direction == TCPR_DIR_C2S || direction == TCPR_DIR_S2C || direction == TCPR_DIR_NOSEND);
    
    /* nothing to do here */
    if (direction == TCPR_DIR_NOSEND)
        return pktlen;
    
    /* decode packet */    
    if ((rcode = tcpedit_dlt_decode(ctx, *packet, pktlen)) == TCPEDIT_ERROR) {
        return TCPEDIT_ERROR;
    } else if (rcode == TCPEDIT_WARN) {
        warnx("Warning decoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
    } else if (rcode == TCPEDIT_SOFT_ERROR) {
        return rcode; /* can't edit the packet */
    }
    
    /* encode packet */
    if ((rcode = tcpedit_dlt_encode(ctx, *packet, pktlen, direction)) == TCPEDIT_ERROR) {
        return TCPEDIT_ERROR;
    } else if (rcode == TCPEDIT_WARN) {
        warnx("Warning encoding packet: %s", tcpedit_getwarn(ctx->tcpedit));
    }
       
    return rcode;
}


/**
 * What is the output DLT type???
 */
int 
tcpedit_dlt_output_dlt(tcpeditdlt_t *ctx)
{
    u_int16_t dlt;
    assert(ctx);
        
    /* 
     * usually we just return the DLT value of the decoder, but for DLT_USER0
     * we return a user-specified value via --user-dlt
     */
    if (ctx->encoder->dlt == DLT_USER0) {
        dlt = dlt_user_get_output_dlt(ctx);
    } else {
        dlt = ctx->encoder->dlt;
    }
    return dlt;
}

/**
 * Get the layer 2 length of the packet using the DLT plugin currently in
 * place
 */
int
tcpedit_dlt_l2len(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
{
    tcpeditdlt_plugin_t *plugin;
    assert(ctx);
    assert(dlt >= 0);
    assert(packet);
    assert(pktlen);
    
    if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
        tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
        return -1;        
    }
    return plugin->plugin_l2len(ctx, packet, pktlen);
}

/**
 * Get the L3 type.  Returns -1 on error.  Get error via tcpedit->geterr()
 */
int
tcpedit_dlt_proto(tcpeditdlt_t *ctx, int dlt, const u_char *packet, const int pktlen)
{
    tcpeditdlt_plugin_t *plugin;

    assert(ctx);
    assert(dlt >= 0);
    assert(packet);
    assert(pktlen);

    if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
        tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
        return -1;
    }
    
    return plugin->plugin_proto(ctx, packet, pktlen);
}

/**
 * Get the L3 data.  Returns NULL on error.  Get error via tcpedit->geterr()
 */
u_char *
tcpedit_dlt_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen)
{
    tcpeditdlt_plugin_t *plugin;

    assert(ctx);
    assert(dlt >= 0);
    assert(packet);
    assert(pktlen);
        
    if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
        tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
        return NULL;
    }

    return plugin->plugin_get_layer3(ctx, packet, pktlen);
}

/**
 * \brief Merge the Layer 3 data back onto the mainbuffer so it's immediately
 *   after the layer 2 header
 * 
 * Since some L2 headers aren't strictly aligned, we need to "merge" the packet w/ L2 data
 * and the L3 buffer.  This is basically a NO-OP for things like vlan tagged ethernet (16 byte) header
 * or Cisco HDLC (4 byte header) but is critical for std ethernet (12 byte header)
 */
u_char *
tcpedit_dlt_merge_l3data(tcpeditdlt_t *ctx, int dlt, u_char *packet, const int pktlen, u_char *l3data)
{
    tcpeditdlt_plugin_t *plugin;
    assert(ctx);
    assert(dlt >= 0);
    assert(pktlen >= 0);
    assert(packet);

    if (l3data == NULL)
        return packet;
        
    if ((plugin = tcpedit_dlt_getplugin(ctx, dlt)) == NULL) {
        tcpedit_seterr(ctx->tcpedit, "Unable to find plugin for DLT 0x%04x", dlt);
        return NULL;
    }

    return plugin->plugin_merge_layer3(ctx, packet, pktlen, l3data);
}



/**
 * Call the specific plugin decode() method
 */
int 
tcpedit_dlt_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
{
    return ctx->decoder->plugin_decode(ctx, packet, pktlen);
}

/**
 * Call the specific plugin encode() method
 */
int 
tcpedit_dlt_encode(tcpeditdlt_t* ctx, u_char *packet, int pktlen, tcpr_dir_t direction)
{
    return ctx->encoder->plugin_encode(ctx, packet, pktlen, direction);
}

/**
 * what is the source (decoder) DLT type?
 */
int 
tcpedit_dlt_src(tcpeditdlt_t *ctx)
{
    assert(ctx);
    return ctx->decoder->dlt;
}

/**
 * What is the destination (encoder) DLT type
 */
int 
tcpedit_dlt_dst(tcpeditdlt_t *ctx)
{
   assert(ctx);
   return ctx->encoder->dlt; 
}


/**
 * cleanup after ourselves: destroys our context and all plugin data
 */
void
tcpedit_dlt_cleanup(tcpeditdlt_t *ctx)
{
    assert(ctx);
    
    if (ctx->encoder != NULL)
        ctx->encoder->plugin_cleanup(ctx);
    
    if (ctx->decoder != NULL)
        ctx->decoder->plugin_cleanup(ctx);

#ifdef FORCE_ALIGN
    safe_free(ctx->l3buff);
#endif

    if (ctx->decoded_extra != NULL)
        safe_free(ctx->decoded_extra);
        
    safe_free(ctx);
}