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 "libtrace_int.h" |
---|
35 | #include "libtrace.h" |
---|
36 | #include "protocols.h" |
---|
37 | #include <assert.h> |
---|
38 | |
---|
39 | #ifndef WIN32 |
---|
40 | #include <net/if_arp.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | #ifndef ARPHRD_ETHER |
---|
44 | #define ARPHRD_ETHER 1 /* Ethernet 10/100Mbps. */ |
---|
45 | #endif |
---|
46 | |
---|
47 | #ifndef ARPHRD_PPP |
---|
48 | #define ARPHRD_PPP 512 |
---|
49 | #endif |
---|
50 | |
---|
51 | /* This file contains all the protocol decoding functions for the meta-data |
---|
52 | * headers that may be prepended to captured packets. |
---|
53 | * |
---|
54 | * Supported protocols include (but are not limited to): |
---|
55 | * Linux SLL |
---|
56 | * PFLOG |
---|
57 | * RadioTap |
---|
58 | * Prism |
---|
59 | */ |
---|
60 | |
---|
61 | /* NB: type is returned as an ARPHRD_ type for SLL*/ |
---|
62 | void *trace_get_payload_from_linux_sll(const void *link, |
---|
63 | uint16_t *type, uint32_t *remaining) |
---|
64 | { |
---|
65 | libtrace_sll_header_t *sll; |
---|
66 | |
---|
67 | sll = (libtrace_sll_header_t*) link; |
---|
68 | |
---|
69 | if (remaining) { |
---|
70 | if (*remaining < sizeof(*sll)) { |
---|
71 | *remaining = 0; |
---|
72 | return NULL; |
---|
73 | } |
---|
74 | *remaining-=sizeof(*sll); |
---|
75 | } |
---|
76 | |
---|
77 | if (type) *type = ntohs(sll->hatype); |
---|
78 | |
---|
79 | return (void*)((char*)sll+sizeof(*sll)); |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | /* NB: type is returned as an ethertype */ |
---|
84 | static void *trace_get_payload_from_pflog(const void *link, |
---|
85 | libtrace_linktype_t *type, uint32_t *remaining) |
---|
86 | { |
---|
87 | libtrace_pflog_header_t *pflog = (libtrace_pflog_header_t*)link; |
---|
88 | if (remaining) { |
---|
89 | if (*remaining<sizeof(*pflog)) { |
---|
90 | *remaining = 0; |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | *remaining-=sizeof(*pflog); |
---|
94 | } |
---|
95 | if (type) { |
---|
96 | *type = TRACE_TYPE_NONE; |
---|
97 | } |
---|
98 | return (void*)((char*)pflog+ sizeof(*pflog)); |
---|
99 | } |
---|
100 | |
---|
101 | /* Returns the 'payload' of the prism header, which is the 802.11 frame */ |
---|
102 | static void *trace_get_payload_from_prism (const void *link, |
---|
103 | libtrace_linktype_t *type, uint32_t *remaining) |
---|
104 | { |
---|
105 | if (remaining) { |
---|
106 | /* Prism header is 144 bytes long */ |
---|
107 | if (*remaining<144) { |
---|
108 | *remaining = 0; |
---|
109 | return NULL; |
---|
110 | } |
---|
111 | *remaining-=144; |
---|
112 | } |
---|
113 | |
---|
114 | if (type) *type = TRACE_TYPE_80211; |
---|
115 | |
---|
116 | return (void *) ((char*)link+144); |
---|
117 | } |
---|
118 | |
---|
119 | /* Returns the 'payload' of the radiotap header, which is the 802.11 frame */ |
---|
120 | static void *trace_get_payload_from_radiotap (const void *link, |
---|
121 | libtrace_linktype_t *type, uint32_t *remaining) |
---|
122 | { |
---|
123 | struct libtrace_radiotap_t *rtap = (struct libtrace_radiotap_t*)link; |
---|
124 | uint16_t rtaplen = bswap_le_to_host16(rtap->it_len); |
---|
125 | if (remaining) { |
---|
126 | if (*remaining < rtaplen) { |
---|
127 | *remaining = 0; |
---|
128 | return NULL; |
---|
129 | } |
---|
130 | *remaining -= rtaplen; |
---|
131 | } |
---|
132 | |
---|
133 | if (type) *type = TRACE_TYPE_80211; |
---|
134 | |
---|
135 | return (void*) ((char*)link + rtaplen); |
---|
136 | } |
---|
137 | |
---|
138 | DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet, |
---|
139 | libtrace_linktype_t *linktype, |
---|
140 | uint32_t *remaining) |
---|
141 | { |
---|
142 | uint32_t dummyrem; |
---|
143 | void *pktbuf = NULL; |
---|
144 | assert(packet != NULL); |
---|
145 | assert(linktype != NULL); |
---|
146 | |
---|
147 | if (remaining == NULL) |
---|
148 | remaining = &dummyrem; |
---|
149 | |
---|
150 | pktbuf = trace_get_packet_buffer(packet, linktype, remaining); |
---|
151 | switch (*linktype) { |
---|
152 | case TRACE_TYPE_LINUX_SLL: |
---|
153 | case TRACE_TYPE_80211_RADIO: |
---|
154 | case TRACE_TYPE_80211_PRISM: |
---|
155 | return pktbuf; |
---|
156 | /* Non metadata packets */ |
---|
157 | case TRACE_TYPE_HDLC_POS: |
---|
158 | case TRACE_TYPE_ETH: |
---|
159 | case TRACE_TYPE_ATM: |
---|
160 | case TRACE_TYPE_80211: |
---|
161 | case TRACE_TYPE_NONE: |
---|
162 | case TRACE_TYPE_PFLOG: |
---|
163 | case TRACE_TYPE_POS: |
---|
164 | case TRACE_TYPE_AAL5: |
---|
165 | case TRACE_TYPE_DUCK: |
---|
166 | case TRACE_TYPE_LLCSNAP: |
---|
167 | case TRACE_TYPE_PPP: |
---|
168 | case TRACE_TYPE_METADATA: |
---|
169 | case TRACE_TYPE_NONDATA: |
---|
170 | return NULL; |
---|
171 | } |
---|
172 | |
---|
173 | /* Shouldn't get here */ |
---|
174 | return NULL; |
---|
175 | } |
---|
176 | |
---|
177 | DLLEXPORT void *trace_get_payload_from_meta(const void *meta, |
---|
178 | libtrace_linktype_t *linktype, |
---|
179 | uint32_t *remaining) |
---|
180 | { |
---|
181 | void *nexthdr; |
---|
182 | uint16_t arphrd; |
---|
183 | |
---|
184 | assert(meta != NULL); |
---|
185 | assert(linktype != NULL); |
---|
186 | assert(remaining != NULL); |
---|
187 | |
---|
188 | switch(*linktype) { |
---|
189 | case TRACE_TYPE_LINUX_SLL: |
---|
190 | nexthdr = trace_get_payload_from_linux_sll(meta, |
---|
191 | &arphrd, remaining); |
---|
192 | *linktype = arphrd_type_to_libtrace(arphrd); |
---|
193 | return nexthdr; |
---|
194 | case TRACE_TYPE_80211_RADIO: |
---|
195 | nexthdr = trace_get_payload_from_radiotap(meta, |
---|
196 | linktype, remaining); |
---|
197 | return nexthdr; |
---|
198 | case TRACE_TYPE_80211_PRISM: |
---|
199 | nexthdr = trace_get_payload_from_prism(meta, |
---|
200 | linktype, remaining); |
---|
201 | return nexthdr; |
---|
202 | case TRACE_TYPE_PFLOG: |
---|
203 | nexthdr = trace_get_payload_from_pflog(meta, |
---|
204 | linktype, remaining); |
---|
205 | return nexthdr; |
---|
206 | case TRACE_TYPE_HDLC_POS: |
---|
207 | case TRACE_TYPE_ETH: |
---|
208 | case TRACE_TYPE_ATM: |
---|
209 | case TRACE_TYPE_80211: |
---|
210 | case TRACE_TYPE_NONE: |
---|
211 | case TRACE_TYPE_POS: |
---|
212 | case TRACE_TYPE_AAL5: |
---|
213 | case TRACE_TYPE_DUCK: |
---|
214 | case TRACE_TYPE_LLCSNAP: |
---|
215 | case TRACE_TYPE_PPP: |
---|
216 | case TRACE_TYPE_METADATA: |
---|
217 | case TRACE_TYPE_NONDATA: |
---|
218 | /* In this case, the pointer passed in does not point |
---|
219 | * to a metadata header and so we cannot get the |
---|
220 | * payload. |
---|
221 | */ |
---|
222 | return NULL; |
---|
223 | } |
---|
224 | /* Shouldn't get here */ |
---|
225 | return NULL; |
---|
226 | } |
---|
227 | |
---|