1 | #include "libtrace.h" |
---|
2 | #include "config.h" |
---|
3 | #ifdef HAVE_PCAP |
---|
4 | #include <pcap.h> |
---|
5 | #endif |
---|
6 | #include "dagformat.h" |
---|
7 | #include "rt_protocol.h" |
---|
8 | #include <assert.h> |
---|
9 | #include "libtrace_int.h" |
---|
10 | #include <stdlib.h> |
---|
11 | #include <net/if_arp.h> |
---|
12 | #include <string.h> |
---|
13 | |
---|
14 | /* This file maps libtrace types to/from pcap DLT and erf types |
---|
15 | * |
---|
16 | * When adding a new linktype to libtrace, add the mapping(s) here, |
---|
17 | * and add the understanding of the type to get_ip(), and perhaps |
---|
18 | * get_{source,destination}_mac (if your linklayer has mac's) |
---|
19 | */ |
---|
20 | |
---|
21 | libtrace_linktype_t pcap_dlt_to_libtrace(libtrace_dlt_t dlt) |
---|
22 | { |
---|
23 | switch(dlt) { |
---|
24 | case TRACE_DLT_NULL: return TRACE_TYPE_NONE; |
---|
25 | case TRACE_DLT_EN10MB: return TRACE_TYPE_ETH; |
---|
26 | case TRACE_DLT_ATM_RFC1483: return TRACE_TYPE_ATM; |
---|
27 | case TRACE_DLT_IEEE802_11: return TRACE_TYPE_80211; |
---|
28 | case TRACE_DLT_LINUX_SLL: return TRACE_TYPE_LINUX_SLL; |
---|
29 | case TRACE_DLT_PFLOG: return TRACE_TYPE_PFLOG; |
---|
30 | } |
---|
31 | return ~0; |
---|
32 | } |
---|
33 | |
---|
34 | libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type) |
---|
35 | { |
---|
36 | switch(type) { |
---|
37 | case TRACE_TYPE_NONE: return TRACE_DLT_NULL; |
---|
38 | case TRACE_TYPE_ETH: return TRACE_DLT_EN10MB; |
---|
39 | case TRACE_TYPE_ATM: return TRACE_DLT_ATM_RFC1483; |
---|
40 | case TRACE_TYPE_80211: return TRACE_DLT_IEEE802_11; |
---|
41 | case TRACE_TYPE_LINUX_SLL: return TRACE_DLT_LINUX_SLL; |
---|
42 | case TRACE_TYPE_PFLOG: return TRACE_DLT_PFLOG; |
---|
43 | } |
---|
44 | return ~0; |
---|
45 | } |
---|
46 | |
---|
47 | enum rt_field_t pcap_dlt_to_rt(libtrace_dlt_t dlt) |
---|
48 | { |
---|
49 | /* For pcap the rt type is just the dlt + a fixed value */ |
---|
50 | return dlt + RT_DATA_PCAP; |
---|
51 | } |
---|
52 | |
---|
53 | libtrace_dlt_t rt_to_pcap_dlt(enum rt_field_t rt_type) |
---|
54 | { |
---|
55 | assert(rt_type >= RT_DATA_PCAP); |
---|
56 | return rt_type - RT_DATA_PCAP; |
---|
57 | } |
---|
58 | |
---|
59 | libtrace_linktype_t erf_type_to_libtrace(char erf) |
---|
60 | { |
---|
61 | switch (erf) { |
---|
62 | case TYPE_HDLC_POS: return TRACE_TYPE_HDLC_POS; |
---|
63 | case TYPE_ETH: return TRACE_TYPE_ETH; |
---|
64 | case TYPE_ATM: return TRACE_TYPE_ATM; |
---|
65 | case TYPE_AAL5: return TRACE_TYPE_AAL5; |
---|
66 | } |
---|
67 | return ~0; |
---|
68 | } |
---|
69 | |
---|
70 | char libtrace_to_erf_type(libtrace_linktype_t linktype) |
---|
71 | { |
---|
72 | switch(linktype) { |
---|
73 | case TRACE_TYPE_HDLC_POS: return TYPE_HDLC_POS; |
---|
74 | case TRACE_TYPE_ETH: return TYPE_ETH; |
---|
75 | case TRACE_TYPE_ATM: return TYPE_ATM; |
---|
76 | case TRACE_TYPE_AAL5: return TYPE_AAL5; |
---|
77 | } |
---|
78 | return -1; |
---|
79 | } |
---|
80 | |
---|
81 | /** Tinker with a packet |
---|
82 | * packets that don't support direction tagging are annoying, especially |
---|
83 | * when we have direction tagging information! So this converts the packet |
---|
84 | * to TRACE_TYPE_LINUX_SLL which does support direction tagging. This is a |
---|
85 | * pcap style packet for the reason that it means it works with bpf filters. |
---|
86 | * |
---|
87 | * @note this will copy the packet, so use sparingly if possible. |
---|
88 | */ |
---|
89 | void promote_packet(libtrace_packet_t *packet) |
---|
90 | { |
---|
91 | if (packet->trace->format->type == TRACE_FORMAT_PCAP) { |
---|
92 | char *tmpbuffer; |
---|
93 | libtrace_sll_header_t *hdr; |
---|
94 | |
---|
95 | switch(pcap_dlt_to_libtrace(rt_to_pcap_dlt(packet->type))) { |
---|
96 | case TRACE_TYPE_LINUX_SLL: |
---|
97 | return; /* Unnecessary */ |
---|
98 | |
---|
99 | case TRACE_TYPE_ETH: |
---|
100 | /* This should be easy, just prepend the header */ |
---|
101 | tmpbuffer= malloc(sizeof(libtrace_sll_header_t) |
---|
102 | +trace_get_capture_length(packet) |
---|
103 | +trace_get_framing_length(packet) |
---|
104 | ); |
---|
105 | |
---|
106 | hdr=(void*)((char*)tmpbuffer |
---|
107 | +trace_get_framing_length(packet)); |
---|
108 | |
---|
109 | hdr->pkttype=0; /* "outgoing" */ |
---|
110 | hdr->hatype = ARPHRD_ETHER; |
---|
111 | break; |
---|
112 | default: |
---|
113 | /* failed */ |
---|
114 | return; |
---|
115 | } |
---|
116 | memcpy(tmpbuffer,packet->header, |
---|
117 | trace_get_framing_length(packet)); |
---|
118 | memcpy(tmpbuffer |
---|
119 | +sizeof(libtrace_sll_header_t) |
---|
120 | +trace_get_framing_length(packet), |
---|
121 | packet->payload, |
---|
122 | trace_get_capture_length(packet)); |
---|
123 | if (packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
124 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
125 | } |
---|
126 | else { |
---|
127 | free(packet->buffer); |
---|
128 | } |
---|
129 | packet->buffer=tmpbuffer; |
---|
130 | packet->header=tmpbuffer; |
---|
131 | packet->payload=tmpbuffer+trace_get_framing_length(packet); |
---|
132 | packet->type=TRACE_TYPE_LINUX_SLL; |
---|
133 | return; |
---|
134 | } |
---|
135 | } |
---|