source: lib/linktypes.c @ 0989e3e

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 0989e3e was 528eb6e, checked in by Shane Alcock <salcock@…>, 9 years ago
  • A very cut-down version of int: and ring: is now built on non-Linux systems, enabling them to be able to decode packets captured using those formats and exported via RT
  • Replaced arphrd.h with a new header file (libtrace_arphrd.h) that provides our own #defines for the various ARPHRD types. The inconsistency between what is available on different OS's was becoming problematic.
  • Increased RT buffer size so we can always fit a complete libtrace buffer into it - int: over RT tends to result in very large "packets".
  • Fixed copy-paste error in the failure case when creating a dummy linux trace in format_rt.c
  • Updated various bits of code to use the new LIBTRACE_ARPHRD types where sensible
  • Removed unneeded #include in protocols_pktmeta.c
  • Replaced u32 and u16 types in format_linux with more portable uint32_t and uint16_t types
  • Removed unneeded calls to linuxnative_init_input in the prepare_packet functions in format_linux.c
  • Don't call the fin_input function on a trace if the format does not provide one
  • Property mode set to 100644
File size: 12.2 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34#include "config.h"
35#include "libtrace.h"
36
37#include "rt_protocol.h"
38#include <assert.h>
39#include "libtrace_int.h"
40#include <stdlib.h>
41#include <string.h>
42
43#include "libtrace_arphrd.h"
44
45
46/* This file maps libtrace types to/from pcap DLT and erf types
47 *
48 * When adding a new linktype to libtrace, add the mapping(s) here,
49 * and add the understanding of the type to get_ip(), and perhaps
50 * get_{source,destination}_mac (if your linklayer has mac's)
51 */
52
53libtrace_linktype_t pcap_linktype_to_libtrace(libtrace_dlt_t linktype)
54{
55        switch(linktype) {
56                case TRACE_DLT_RAW:
57                case TRACE_DLT_LINKTYPE_RAW: return TRACE_TYPE_NONE;
58                case TRACE_DLT_EN10MB: return TRACE_TYPE_ETH;
59                case TRACE_DLT_IEEE802_11: return TRACE_TYPE_80211;
60                case TRACE_DLT_LINUX_SLL: return TRACE_TYPE_LINUX_SLL;
61                case TRACE_DLT_PFLOG: return TRACE_TYPE_PFLOG;
62                case TRACE_DLT_IEEE802_11_RADIO: return TRACE_TYPE_80211_RADIO;
63                case TRACE_DLT_ATM_RFC1483: return TRACE_TYPE_LLCSNAP;
64                case TRACE_DLT_PPP: return TRACE_TYPE_PPP;
65                case TRACE_DLT_PPP_SERIAL: return TRACE_TYPE_POS;
66                case TRACE_DLT_C_HDLC: return TRACE_TYPE_HDLC_POS;
67                /* Unhandled */
68                case TRACE_DLT_NULL:    /* Raw IP frame with a BSD specific
69                                         * header If you want raw L3 headers
70                                         * use TRACE_DLT_RAW
71                                         */
72                        break;
73        }
74        return ~0U;
75}
76
77libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type)
78{
79        /* If pcap doesn't have a DLT, you can either ask pcap to register
80         * you a DLT, (and perhaps write a tcpdump decoder for it), or you
81         * can add it to demote_packet
82         */
83        switch(type) {
84                case TRACE_TYPE_NONE: return TRACE_DLT_RAW; 
85                case TRACE_TYPE_ETH: return TRACE_DLT_EN10MB;
86                case TRACE_TYPE_80211: return TRACE_DLT_IEEE802_11;
87                case TRACE_TYPE_LINUX_SLL: return TRACE_DLT_LINUX_SLL;
88                case TRACE_TYPE_PFLOG: return TRACE_DLT_PFLOG;
89                case TRACE_TYPE_80211_RADIO: return TRACE_DLT_IEEE802_11_RADIO;
90                case TRACE_TYPE_LLCSNAP: return TRACE_DLT_ATM_RFC1483;
91                case TRACE_TYPE_PPP:    return TRACE_DLT_PPP;
92                case TRACE_TYPE_HDLC_POS: return TRACE_DLT_C_HDLC;
93                /* Theres more than one type of PPP.  Who knew? */
94                case TRACE_TYPE_POS:    return TRACE_DLT_PPP_SERIAL; 
95
96                /* Below here are unsupported conversions */
97                /* Despite hints to the contrary, there is no DLT
98                 * for 'raw atm packets that happen to be missing
99                 * the HEC' or even 'raw atm packets that have a hec'.
100                 *
101                 * The closest are DLT_ATM_RFC1483 but that doesn't
102                 * include the ATM header, only the LLCSNAP header.
103                 */
104                case TRACE_TYPE_ATM:
105                /* pcap has no DLT for DUCK */
106                case TRACE_TYPE_DUCK:
107                /* Used for test traces within WAND */
108                case TRACE_TYPE_80211_PRISM:   
109                /* Probably == PPP */
110                /* TODO: We haven't researched these yet */
111                case TRACE_TYPE_AAL5:
112                case TRACE_TYPE_METADATA:
113                case TRACE_TYPE_NONDATA:
114                        break;
115        }
116        return ~0U;
117}
118
119static libtrace_dlt_t pcap_dlt_to_pcap_linktype(libtrace_dlt_t linktype)
120{
121        switch (linktype) {
122                case TRACE_DLT_RAW: return TRACE_DLT_LINKTYPE_RAW;
123                default:
124                                    return linktype;
125        }
126}
127
128libtrace_dlt_t libtrace_to_pcap_linktype(libtrace_linktype_t type)
129{
130        return pcap_dlt_to_pcap_linktype(libtrace_to_pcap_dlt(type));
131}
132
133libtrace_rt_types_t pcap_linktype_to_rt(libtrace_dlt_t linktype) 
134{
135        /* For pcap the rt type is just the linktype + a fixed value */
136        return pcap_dlt_to_pcap_linktype(linktype) + TRACE_RT_DATA_DLT;
137}
138
139libtrace_rt_types_t bpf_linktype_to_rt(libtrace_dlt_t linktype) {
140        return pcap_dlt_to_pcap_linktype(linktype) + TRACE_RT_DATA_BPF;
141
142}
143
144libtrace_dlt_t rt_to_pcap_linktype(libtrace_rt_types_t rt_type)
145{
146       
147        if (rt_type >= TRACE_RT_DATA_DLT && rt_type < TRACE_RT_DATA_DLT_END) {
148                /* RT type is in the pcap range */
149                return rt_type - TRACE_RT_DATA_DLT;
150        } 
151        else if (rt_type >= TRACE_RT_DATA_BPF && rt_type < TRACE_RT_DATA_BPF_END) {
152                return rt_type - TRACE_RT_DATA_BPF;
153        }
154       
155        fprintf(stderr, "Error: RT type %u cannot be converted to a pcap DLT\n", rt_type);
156        assert(rt_type >= TRACE_RT_DATA_DLT && rt_type < TRACE_RT_DATA_BPF_END);
157        return 0;       /* satisfy warnings */
158}
159
160libtrace_linktype_t erf_type_to_libtrace(uint8_t erf)
161{
162        switch (erf) {
163                case TYPE_HDLC_POS:     return TRACE_TYPE_HDLC_POS;
164                case TYPE_ETH:          return TRACE_TYPE_ETH;
165                case TYPE_ATM:          return TRACE_TYPE_ATM;
166                case TYPE_AAL5:         return TRACE_TYPE_AAL5;
167                case TYPE_DSM_COLOR_ETH:return TRACE_TYPE_ETH;
168                case TYPE_IPV4:         return TRACE_TYPE_NONE;
169                case TYPE_IPV6:         return TRACE_TYPE_NONE;
170        }
171        return ~0U;
172}
173
174uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype)
175{
176        switch(linktype) {
177                case TRACE_TYPE_HDLC_POS: return TYPE_HDLC_POS;
178                case TRACE_TYPE_ETH:    return TYPE_ETH;
179                case TRACE_TYPE_ATM:    return TYPE_ATM;
180                case TRACE_TYPE_AAL5:   return TYPE_AAL5;
181               
182                /* Not technically correct! Could be IPv6 packet
183                 *
184                 * TODO: Maybe we want TRACE_TYPE_RAW_IPV4 and
185                 * TRACE_TYPE_RAW_IPV6 to replace TRACE_TYPE_NONE.
186                 * Still need a good way to figure out how to convert
187                 * TRACE_DLT_LINKTYPE_RAW into the correct type for the
188                 * IP version though :( */
189                case TRACE_TYPE_NONE:   return TYPE_IPV4;
190                /* Unsupported conversions */
191                case TRACE_TYPE_LLCSNAP:
192                case TRACE_TYPE_DUCK:
193                case TRACE_TYPE_80211_RADIO:
194                case TRACE_TYPE_80211_PRISM:
195                case TRACE_TYPE_80211:
196                case TRACE_TYPE_PFLOG:
197                case TRACE_TYPE_LINUX_SLL:
198                case TRACE_TYPE_PPP:
199                case TRACE_TYPE_POS:
200                case TRACE_TYPE_METADATA:
201                case TRACE_TYPE_NONDATA:
202                        break;
203        }
204        return 255;
205}
206
207libtrace_linktype_t arphrd_type_to_libtrace(unsigned int arphrd) {
208        switch(arphrd) {
209                case LIBTRACE_ARPHRD_ETHER: return TRACE_TYPE_ETH;     
210                case LIBTRACE_ARPHRD_EETHER: return TRACE_TYPE_ETH;     
211                case LIBTRACE_ARPHRD_IEEE80211: return TRACE_TYPE_80211;
212                case LIBTRACE_ARPHRD_IEEE80211_RADIOTAP: return TRACE_TYPE_80211_RADIO;
213                case LIBTRACE_ARPHRD_PPP: return TRACE_TYPE_NONE;
214                case LIBTRACE_ARPHRD_LOOPBACK: return TRACE_TYPE_ETH;
215                case LIBTRACE_ARPHRD_NONE: return TRACE_TYPE_NONE;
216        }
217        printf("Unknown ARPHRD %08x\n",arphrd);
218        return ~0U;
219}
220
221unsigned int libtrace_to_arphrd_type(libtrace_linktype_t linktype) {
222        switch(linktype) {
223                case TRACE_TYPE_ETH: return LIBTRACE_ARPHRD_ETHER;
224                case TRACE_TYPE_80211: return LIBTRACE_ARPHRD_IEEE80211;
225                case TRACE_TYPE_80211_RADIO: return LIBTRACE_ARPHRD_IEEE80211_RADIOTAP;
226                default: break;
227        }
228        return ~0U;
229}
230
231/** Prepends a Linux SLL header to the packet.
232 *
233 * Packets that don't support direction tagging are annoying, especially
234 * when we have direction tagging information!  So this converts the packet
235 * to TRACE_TYPE_LINUX_SLL which does support direction tagging.  This is a
236 * pcap style packet for the reason that it means it works with bpf filters.
237 *
238 * @note this will copy the packet, so use sparingly if possible.
239 */
240void promote_packet(libtrace_packet_t *packet)
241{
242        if (packet->trace->format->type == TRACE_FORMAT_PCAP) {
243                char *tmpbuffer;
244                libtrace_sll_header_t *hdr;
245
246                if (pcap_linktype_to_libtrace(rt_to_pcap_linktype(packet->type))
247                        == TRACE_TYPE_LINUX_SLL) {
248                        /* This is already been promoted, so ignore it */
249                        return;
250                }
251
252                /* This should be easy, just prepend the header */
253                tmpbuffer= (char*)malloc(
254                                sizeof(libtrace_sll_header_t)
255                                +trace_get_capture_length(packet)
256                                +trace_get_framing_length(packet)
257                                );
258
259                hdr=(libtrace_sll_header_t*)((char*)tmpbuffer
260                        +trace_get_framing_length(packet));
261
262                hdr->halen=htons(6);
263                hdr->pkttype=TRACE_SLL_OUTGOING;
264
265                switch(pcap_linktype_to_libtrace(rt_to_pcap_linktype(packet->type))) {
266                        case TRACE_TYPE_NONE:
267                                trace_get_layer3(packet, &hdr->protocol, NULL);
268                                hdr->hatype = htons(LIBTRACE_ARPHRD_PPP);
269                                hdr->protocol=htons(hdr->protocol);
270                                break;
271                        case TRACE_TYPE_ETH:
272                                hdr->hatype = htons(LIBTRACE_ARPHRD_ETHER);
273                                hdr->protocol=htons(0x0060); /* ETH_P_LOOP */
274                                break;
275                        default:
276                                /* failed */
277                                return;
278                }
279                memcpy(tmpbuffer,packet->header,
280                                trace_get_framing_length(packet));
281                memcpy(tmpbuffer
282                                +sizeof(libtrace_sll_header_t)
283                                +trace_get_framing_length(packet),
284                                packet->payload,
285                                trace_get_capture_length(packet));
286                if (packet->buf_control == TRACE_CTRL_EXTERNAL) {
287                        packet->buf_control=TRACE_CTRL_PACKET;
288                }
289                else {
290                        free(packet->buffer);
291                }
292                packet->buffer=tmpbuffer;
293                packet->header=tmpbuffer;
294                packet->payload=tmpbuffer+trace_get_framing_length(packet);
295                packet->type=pcap_linktype_to_rt(TRACE_DLT_LINUX_SLL);
296                ((struct libtrace_pcapfile_pkt_hdr_t*) packet->header)->caplen+=
297                        sizeof(libtrace_sll_header_t);
298                ((struct libtrace_pcapfile_pkt_hdr_t*) packet->header)->wirelen+=
299                        sizeof(libtrace_sll_header_t);
300                trace_clear_cache(packet);
301                return;
302        }
303}
304
305/* Try and remove any extraneous encapsulation that may have been added to
306 * a packet. Effectively the opposite to promote_packet.
307 *
308 * Returns true if demotion was possible, false if not.
309 */
310bool demote_packet(libtrace_packet_t *packet)
311{
312        uint8_t type;
313        uint16_t ha_type, next_proto;
314        libtrace_sll_header_t *sll = NULL;
315        uint32_t remaining = 0;
316        char *tmp;
317        struct timeval tv;
318        static libtrace_t *trace = NULL;
319        switch(trace_get_link_type(packet)) {
320                case TRACE_TYPE_ATM:
321                        remaining=trace_get_capture_length(packet);
322                        packet->payload=trace_get_payload_from_atm(
323                                packet->payload,&type,&remaining);
324                        if (!packet->payload)
325                                return false;
326                        tmp=(char*)malloc(
327                                trace_get_capture_length(packet)
328                                +sizeof(libtrace_pcapfile_pkt_hdr_t)
329                                );
330
331                        tv=trace_get_timeval(packet);
332                        ((libtrace_pcapfile_pkt_hdr_t*)tmp)->ts_sec=tv.tv_sec;
333                        ((libtrace_pcapfile_pkt_hdr_t*)tmp)->ts_usec=tv.tv_usec;
334                        ((libtrace_pcapfile_pkt_hdr_t*)tmp)->wirelen
335                                = trace_get_wire_length(packet)-(trace_get_capture_length(packet)-remaining);
336                        ((libtrace_pcapfile_pkt_hdr_t*)tmp)->caplen
337                                = remaining;
338
339                        memcpy(tmp+sizeof(libtrace_pcapfile_pkt_hdr_t),
340                                        packet->payload,
341                                        (size_t)remaining);
342                        if (packet->buf_control == TRACE_CTRL_EXTERNAL) {
343                                packet->buf_control=TRACE_CTRL_PACKET;
344                        }
345                        else {
346                                free(packet->buffer);
347                        }
348                        packet->buffer=tmp;
349                        packet->header=tmp;
350                        packet->payload=tmp+sizeof(libtrace_pcapfile_pkt_hdr_t);
351                        packet->type=pcap_linktype_to_rt(TRACE_DLT_ATM_RFC1483);
352                       
353                        if (trace == NULL) {
354                                trace = trace_create_dead("pcapfile:-");
355                        }
356
357                        packet->trace=trace;
358
359                        /* Invalidate caches */
360                        trace_clear_cache(packet);
361                        return true;
362
363                case TRACE_TYPE_LINUX_SLL:
364                        sll = (libtrace_sll_header_t *)(packet->payload);
365
366                        ha_type = ntohs(sll->hatype);
367                        next_proto = ntohs(sll->protocol);
368               
369                        /* Preserved from older libtrace behaviour */
370                        if (ha_type == LIBTRACE_ARPHRD_PPP)
371                                packet->type = pcap_linktype_to_rt(TRACE_DLT_RAW);
372                        /* Don't decide trace type based on ha_type,
373                         * decide based on the protocol header that is
374                         * coming up!
375                         */
376                        else if (next_proto == TRACE_ETHERTYPE_LOOPBACK)
377                                packet->type = pcap_linktype_to_rt(TRACE_DLT_EN10MB);
378                        else if (next_proto == TRACE_ETHERTYPE_IP) 
379                                packet->type = pcap_linktype_to_rt(TRACE_DLT_RAW);
380                        else if (next_proto == TRACE_ETHERTYPE_IPV6)
381                                packet->type = pcap_linktype_to_rt(TRACE_DLT_RAW);
382                        else
383                                return false;
384
385                        /* Skip the Linux SLL header */
386                        packet->payload=(void*)((char*)packet->payload
387                                        +sizeof(libtrace_sll_header_t));
388                        trace_set_capture_length(packet,
389                                trace_get_capture_length(packet)
390                                        -sizeof(libtrace_sll_header_t));
391
392                        /* Invalidate caches */
393                        trace_clear_cache(packet);
394                        break;
395                default:
396                        return false;
397        }
398
399        /* Invalidate caches */
400        trace_clear_cache(packet);
401        return true;
402}
Note: See TracBrowser for help on using the repository browser.