1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | #include "libtrace_int.h" |
---|
27 | #include "libtrace.h" |
---|
28 | #include "protocols.h" |
---|
29 | |
---|
30 | #ifdef HAVE_WANDDER |
---|
31 | #include <libwandder_etsili.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | /* This file contains all the protocol decoding functions for the meta-data |
---|
35 | * headers that may be prepended to captured packets. |
---|
36 | * |
---|
37 | * Supported protocols include (but are not limited to): |
---|
38 | * Linux SLL |
---|
39 | * PFLOG |
---|
40 | * RadioTap |
---|
41 | * Prism |
---|
42 | */ |
---|
43 | |
---|
44 | /* NB: type is returned as an ARPHRD_ type for SLL*/ |
---|
45 | void *trace_get_payload_from_linux_sll(const void *link, |
---|
46 | uint16_t *arphrd_type, uint16_t *next, |
---|
47 | uint32_t *remaining) |
---|
48 | { |
---|
49 | libtrace_sll_header_t *sll; |
---|
50 | |
---|
51 | sll = (libtrace_sll_header_t*) link; |
---|
52 | |
---|
53 | if (remaining) { |
---|
54 | if (*remaining < sizeof(*sll)) { |
---|
55 | *remaining = 0; |
---|
56 | return NULL; |
---|
57 | } |
---|
58 | *remaining-=sizeof(*sll); |
---|
59 | } |
---|
60 | |
---|
61 | /* The SLL header is actually in place of a link layer header, so |
---|
62 | * we want to use the protocol field to tell our caller what the |
---|
63 | * next header is going to be */ |
---|
64 | if (next) *next = (libtrace_linktype_t)(ntohs(sll->protocol)); |
---|
65 | if (arphrd_type) *arphrd_type = ntohs(sll->hatype); |
---|
66 | |
---|
67 | return (void*)((char*)sll+sizeof(*sll)); |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | /* NB: type is returned as an ethertype */ |
---|
72 | static void *trace_get_payload_from_pflog(const void *link, |
---|
73 | libtrace_linktype_t *type, uint32_t *remaining) |
---|
74 | { |
---|
75 | libtrace_pflog_header_t *pflog = (libtrace_pflog_header_t*)link; |
---|
76 | if (remaining) { |
---|
77 | if (*remaining<sizeof(*pflog)) { |
---|
78 | *remaining = 0; |
---|
79 | return NULL; |
---|
80 | } |
---|
81 | *remaining-=sizeof(*pflog); |
---|
82 | } |
---|
83 | if (type) { |
---|
84 | *type = TRACE_TYPE_NONE; |
---|
85 | } |
---|
86 | return (void*)((char*)pflog+ sizeof(*pflog)); |
---|
87 | } |
---|
88 | |
---|
89 | /* Returns the 'payload' of the prism header, which is the 802.11 frame */ |
---|
90 | static void *trace_get_payload_from_prism (const void *link, |
---|
91 | libtrace_linktype_t *type, uint32_t *remaining) |
---|
92 | { |
---|
93 | if (remaining) { |
---|
94 | /* Prism header is 144 bytes long */ |
---|
95 | if (*remaining<144) { |
---|
96 | *remaining = 0; |
---|
97 | return NULL; |
---|
98 | } |
---|
99 | *remaining-=144; |
---|
100 | } |
---|
101 | |
---|
102 | if (type) *type = TRACE_TYPE_80211; |
---|
103 | |
---|
104 | return (void *) ((char*)link+144); |
---|
105 | } |
---|
106 | |
---|
107 | /* Returns the 'payload' of the radiotap header, which is the 802.11 frame */ |
---|
108 | static void *trace_get_payload_from_radiotap (const void *link, |
---|
109 | libtrace_linktype_t *type, uint32_t *remaining) |
---|
110 | { |
---|
111 | struct libtrace_radiotap_t *rtap = (struct libtrace_radiotap_t*)link; |
---|
112 | uint16_t rtaplen = bswap_le_to_host16(rtap->it_len); |
---|
113 | if (remaining) { |
---|
114 | if (*remaining < rtaplen) { |
---|
115 | *remaining = 0; |
---|
116 | return NULL; |
---|
117 | } |
---|
118 | *remaining -= rtaplen; |
---|
119 | } |
---|
120 | |
---|
121 | if (type) *type = TRACE_TYPE_80211; |
---|
122 | |
---|
123 | return (void*) ((char*)link + rtaplen); |
---|
124 | } |
---|
125 | |
---|
126 | static void *trace_get_payload_from_etsili(const void *link, |
---|
127 | libtrace_linktype_t *type, uint32_t *remaining) { |
---|
128 | |
---|
129 | #ifdef HAVE_WANDDER |
---|
130 | wandder_etsispec_t *dec; |
---|
131 | uint8_t *ccptr; |
---|
132 | |
---|
133 | /* XXX Bit annoying to be creating and freeing this every time */ |
---|
134 | dec = wandder_create_etsili_decoder(); |
---|
135 | wandder_attach_etsili_buffer(dec, (uint8_t *)link, *remaining, false); |
---|
136 | ccptr = wandder_etsili_get_cc_contents(dec, remaining, NULL, 0); |
---|
137 | /* Assuming all CCs are IP for now */ |
---|
138 | *type = TRACE_TYPE_NONE; |
---|
139 | wandder_free_etsili_decoder(dec); |
---|
140 | return ccptr; |
---|
141 | |
---|
142 | #else |
---|
143 | (void)link; |
---|
144 | (void)type; |
---|
145 | *remaining = 0; |
---|
146 | return NULL; |
---|
147 | #endif |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet, |
---|
152 | libtrace_linktype_t *linktype, |
---|
153 | uint32_t *remaining) |
---|
154 | { |
---|
155 | uint32_t dummyrem; |
---|
156 | void *pktbuf = NULL; |
---|
157 | if (!packet) { |
---|
158 | fprintf(stderr, "NULL packet passed into trace_get_packet_meta()"); |
---|
159 | return NULL; |
---|
160 | } |
---|
161 | if (!linktype) { |
---|
162 | fprintf(stderr, "NULL linkype passed into trace_get_packet_meta()"); |
---|
163 | return NULL; |
---|
164 | } |
---|
165 | |
---|
166 | if (remaining == NULL) |
---|
167 | remaining = &dummyrem; |
---|
168 | |
---|
169 | pktbuf = trace_get_packet_buffer(packet, linktype, remaining); |
---|
170 | switch (*linktype) { |
---|
171 | case TRACE_TYPE_LINUX_SLL: |
---|
172 | case TRACE_TYPE_80211_RADIO: |
---|
173 | case TRACE_TYPE_80211_PRISM: |
---|
174 | case TRACE_TYPE_ERF_META: |
---|
175 | case TRACE_TYPE_ETSILI: |
---|
176 | return pktbuf; |
---|
177 | /* Non metadata packets */ |
---|
178 | case TRACE_TYPE_HDLC_POS: |
---|
179 | case TRACE_TYPE_ETH: |
---|
180 | case TRACE_TYPE_ATM: |
---|
181 | case TRACE_TYPE_80211: |
---|
182 | case TRACE_TYPE_NONE: |
---|
183 | case TRACE_TYPE_PFLOG: |
---|
184 | case TRACE_TYPE_POS: |
---|
185 | case TRACE_TYPE_AAL5: |
---|
186 | case TRACE_TYPE_DUCK: |
---|
187 | case TRACE_TYPE_LLCSNAP: |
---|
188 | case TRACE_TYPE_PPP: |
---|
189 | case TRACE_TYPE_METADATA: |
---|
190 | case TRACE_TYPE_NONDATA: |
---|
191 | case TRACE_TYPE_OPENBSD_LOOP: |
---|
192 | case TRACE_TYPE_UNKNOWN: |
---|
193 | case TRACE_TYPE_PCAPNG_META: |
---|
194 | case TRACE_TYPE_CONTENT_INVALID: |
---|
195 | return NULL; |
---|
196 | } |
---|
197 | |
---|
198 | /* Shouldn't get here */ |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | |
---|
202 | DLLEXPORT void *trace_get_payload_from_meta(const void *meta, |
---|
203 | libtrace_linktype_t *linktype, |
---|
204 | uint32_t *remaining) |
---|
205 | { |
---|
206 | void *nexthdr; |
---|
207 | uint16_t arphrd = 0; |
---|
208 | uint16_t next = 0; |
---|
209 | |
---|
210 | if (!meta) { |
---|
211 | fprintf(stderr, "NULL meta passed into trace_get_payload_from_meta()"); |
---|
212 | return NULL; |
---|
213 | } |
---|
214 | if (!linktype) { |
---|
215 | fprintf(stderr, "NULL linktype passed into trace_get_payload_from_meta()"); |
---|
216 | return NULL; |
---|
217 | } |
---|
218 | if (!remaining) { |
---|
219 | fprintf(stderr, "NULL remaining passed into trace_get_payload_from_meta()"); |
---|
220 | return NULL; |
---|
221 | } |
---|
222 | |
---|
223 | switch(*linktype) { |
---|
224 | case TRACE_TYPE_LINUX_SLL: |
---|
225 | nexthdr = trace_get_payload_from_linux_sll(meta, |
---|
226 | &arphrd, &next, remaining); |
---|
227 | |
---|
228 | /* Ethernet header is usually absent in SLL captures, |
---|
229 | * so we don't want to skip it just yet */ |
---|
230 | if (arphrd_type_to_libtrace(arphrd) == TRACE_TYPE_ETH && next != 0x0060) |
---|
231 | *linktype = TRACE_TYPE_NONE; |
---|
232 | else |
---|
233 | *linktype = arphrd_type_to_libtrace(arphrd); |
---|
234 | return nexthdr; |
---|
235 | case TRACE_TYPE_80211_RADIO: |
---|
236 | nexthdr = trace_get_payload_from_radiotap(meta, |
---|
237 | linktype, remaining); |
---|
238 | return nexthdr; |
---|
239 | case TRACE_TYPE_80211_PRISM: |
---|
240 | nexthdr = trace_get_payload_from_prism(meta, |
---|
241 | linktype, remaining); |
---|
242 | return nexthdr; |
---|
243 | case TRACE_TYPE_PFLOG: |
---|
244 | nexthdr = trace_get_payload_from_pflog(meta, |
---|
245 | linktype, remaining); |
---|
246 | return nexthdr; |
---|
247 | case TRACE_TYPE_ETSILI: |
---|
248 | nexthdr = trace_get_payload_from_etsili(meta, |
---|
249 | linktype, remaining); |
---|
250 | return nexthdr; |
---|
251 | |
---|
252 | case TRACE_TYPE_PCAPNG_META: |
---|
253 | case TRACE_TYPE_HDLC_POS: |
---|
254 | case TRACE_TYPE_ETH: |
---|
255 | case TRACE_TYPE_ATM: |
---|
256 | case TRACE_TYPE_80211: |
---|
257 | case TRACE_TYPE_NONE: |
---|
258 | case TRACE_TYPE_POS: |
---|
259 | case TRACE_TYPE_AAL5: |
---|
260 | case TRACE_TYPE_DUCK: |
---|
261 | case TRACE_TYPE_LLCSNAP: |
---|
262 | case TRACE_TYPE_PPP: |
---|
263 | case TRACE_TYPE_METADATA: |
---|
264 | case TRACE_TYPE_NONDATA: |
---|
265 | case TRACE_TYPE_OPENBSD_LOOP: |
---|
266 | case TRACE_TYPE_ERF_META: |
---|
267 | case TRACE_TYPE_UNKNOWN: |
---|
268 | case TRACE_TYPE_CONTENT_INVALID: |
---|
269 | /* In this case, the pointer passed in does not point |
---|
270 | * to a metadata header and so we cannot get the |
---|
271 | * payload. |
---|
272 | */ |
---|
273 | return NULL; |
---|
274 | } |
---|
275 | /* Shouldn't get here */ |
---|
276 | return NULL; |
---|
277 | } |
---|
278 | |
---|