1 | /* Protocol decodes for layer 2 */ |
---|
2 | #include "libtrace.h" |
---|
3 | #include "protocols.h" |
---|
4 | #include "libtrace_int.h" |
---|
5 | #include <assert.h> |
---|
6 | |
---|
7 | /* Returns the payload from 802.3 ethernet. Type optionally returned in |
---|
8 | * "type" in host byte order. This will return a vlan header. |
---|
9 | */ |
---|
10 | void *trace_get_payload_from_ethernet(void *ethernet, |
---|
11 | uint16_t *type, |
---|
12 | uint32_t *remaining) |
---|
13 | { |
---|
14 | libtrace_ether_t *eth = (libtrace_ether_t*)ethernet; |
---|
15 | |
---|
16 | if (remaining) { |
---|
17 | if (*remaining < sizeof(*eth)) |
---|
18 | return NULL; |
---|
19 | *remaining-=sizeof(*eth); |
---|
20 | } |
---|
21 | |
---|
22 | if (type) |
---|
23 | *type = ntohs(eth->ether_type); |
---|
24 | |
---|
25 | return (void*)((char *)eth + sizeof(*eth)); |
---|
26 | } |
---|
27 | |
---|
28 | /* skip any 802.1q headers if necessary |
---|
29 | * type is input/output |
---|
30 | */ |
---|
31 | void *trace_get_vlan_payload_from_ethernet_payload(void *ethernet, uint16_t *type, |
---|
32 | uint32_t *remaining) |
---|
33 | { |
---|
34 | if (*type == 0x8100) { |
---|
35 | libtrace_8021q_t *vlanhdr = (libtrace_8021q_t *)ethernet; |
---|
36 | |
---|
37 | if (remaining) { |
---|
38 | if (*remaining < sizeof(libtrace_8021q_t)) |
---|
39 | return NULL; |
---|
40 | |
---|
41 | *remaining=*remaining-sizeof(libtrace_8021q_t); |
---|
42 | } |
---|
43 | |
---|
44 | *type = ntohs(vlanhdr->vlan_ether_type); |
---|
45 | |
---|
46 | return (void*)((char *)ethernet + sizeof(*vlanhdr)); |
---|
47 | } |
---|
48 | |
---|
49 | return ethernet; |
---|
50 | } |
---|
51 | |
---|
52 | /* skip any MPLS headers if necessary, guessing what the next type is |
---|
53 | * type is input/output. If the next type is "ethernet" this will |
---|
54 | * return a type of 0x0000. |
---|
55 | */ |
---|
56 | void *trace_get_mpls_payload_from_ethernet_payload(void *ethernet, |
---|
57 | uint16_t *type, uint32_t *remaining) |
---|
58 | { |
---|
59 | if (*type == 0x8847) { |
---|
60 | if ((((char*)ethernet)[2]&0x01)==0) { |
---|
61 | *type = 0x8847; |
---|
62 | } |
---|
63 | else { |
---|
64 | if (!remaining || *remaining>=5) { |
---|
65 | switch (((char*)ethernet)[4]&0xF0) { |
---|
66 | case 0x40: |
---|
67 | *type = 0x0800; |
---|
68 | break; |
---|
69 | case 0x60: |
---|
70 | *type = 0x86DD; |
---|
71 | break; |
---|
72 | default: |
---|
73 | /* Ethernet */ |
---|
74 | *type = 0; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | ethernet=(char*)ethernet+4; |
---|
79 | if (remaining) { |
---|
80 | if (*remaining<4) |
---|
81 | return NULL; |
---|
82 | else |
---|
83 | *remaining-=4; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | return ethernet; |
---|
88 | } |
---|
89 | else |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | |
---|
93 | static void *trace_get_payload_from_llcsnap(void *link, |
---|
94 | uint16_t *type, uint32_t *remaining) |
---|
95 | { |
---|
96 | /* 64 byte capture. */ |
---|
97 | libtrace_llcsnap_t *llc = (libtrace_llcsnap_t*)link; |
---|
98 | |
---|
99 | if (remaining) { |
---|
100 | if (*remaining < sizeof(libtrace_llcsnap_t)) |
---|
101 | return NULL; |
---|
102 | *remaining-=(sizeof(libtrace_llcsnap_t)); |
---|
103 | } |
---|
104 | |
---|
105 | llc = (libtrace_llcsnap_t*)((char *)llc); |
---|
106 | |
---|
107 | if (type) *type = ntohs(llc->type); |
---|
108 | |
---|
109 | return (void*)((char*)llc+sizeof(*llc)); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | static void *trace_get_payload_from_80211(void *link, uint16_t *type, uint32_t *remaining) |
---|
114 | { |
---|
115 | libtrace_80211_t *wifi; |
---|
116 | uint16_t *eth; /* ethertype */ |
---|
117 | int8_t extra = 0; /* how many QoS bytes to skip */ |
---|
118 | |
---|
119 | if (remaining && *remaining < sizeof(libtrace_80211_t)) |
---|
120 | return NULL; |
---|
121 | |
---|
122 | wifi=(libtrace_80211_t*)link; |
---|
123 | |
---|
124 | /* Data packet? */ |
---|
125 | if (wifi->type != 2) { |
---|
126 | return NULL; |
---|
127 | } |
---|
128 | |
---|
129 | /* If FromDS and ToDS are both set then we have a four-address |
---|
130 | * frame. Otherwise we have a three-address frame */ |
---|
131 | if (!(wifi->to_ds && wifi->from_ds)) |
---|
132 | extra -= 6; |
---|
133 | |
---|
134 | /* Indicates QoS field present, see IEEE802.11e-2005 pg 21 */ |
---|
135 | if (wifi->subtype & 0x8) |
---|
136 | extra += 2; |
---|
137 | |
---|
138 | if (remaining && *remaining < sizeof(*eth)) |
---|
139 | return NULL; |
---|
140 | |
---|
141 | eth=(uint16_t *)((char*)wifi+sizeof(*wifi)+extra); |
---|
142 | |
---|
143 | if (*eth == 0xaaaa) |
---|
144 | /* Payload contains an 802.2 LLC/SNAP frame */ |
---|
145 | return trace_get_payload_from_llcsnap((void *)eth, type, remaining); |
---|
146 | |
---|
147 | /* Otherwise we assume an Ethernet II frame */ |
---|
148 | if (type) *type=ntohs(*eth); |
---|
149 | if (remaining) *remaining = *remaining - sizeof(libtrace_80211_t) - extra - sizeof(*eth); |
---|
150 | |
---|
151 | return (void*)((char*)eth+sizeof(*eth)); |
---|
152 | } |
---|
153 | |
---|
154 | static void *trace_get_payload_from_ppp(void *link, |
---|
155 | uint16_t *type, uint32_t *remaining) |
---|
156 | { |
---|
157 | /* 64 byte capture. */ |
---|
158 | libtrace_ppp_t *ppp = (libtrace_ppp_t*)link; |
---|
159 | |
---|
160 | if (remaining) { |
---|
161 | if (*remaining < sizeof(libtrace_ppp_t)) |
---|
162 | return NULL; |
---|
163 | *remaining-=sizeof(libtrace_ppp_t); |
---|
164 | } |
---|
165 | |
---|
166 | if (type) { |
---|
167 | switch(ntohs(ppp->protocol)) { |
---|
168 | case 0x0021: *type = 0x0800; break; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | return (void*)((char *)ppp+sizeof(*ppp)); |
---|
174 | } |
---|
175 | |
---|
176 | typedef struct libtrace_chdlc_t { |
---|
177 | uint8_t address; /** 0xF0 for unicast, 0xF8 for multicast */ |
---|
178 | uint8_t control; |
---|
179 | uint16_t ethertype; |
---|
180 | } libtrace_chdlc_t; |
---|
181 | |
---|
182 | static void *trace_get_payload_from_chdlc(void *link, |
---|
183 | uint16_t *type, uint32_t *remaining) |
---|
184 | { |
---|
185 | libtrace_chdlc_t *chdlc = (libtrace_chdlc_t*)link; |
---|
186 | |
---|
187 | if (remaining) { |
---|
188 | if (*remaining < sizeof(libtrace_chdlc_t)) |
---|
189 | return NULL; |
---|
190 | *remaining-=sizeof(libtrace_chdlc_t); |
---|
191 | } |
---|
192 | |
---|
193 | if (type) { |
---|
194 | *type=ntohs(chdlc->ethertype); |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | return (void*)((char *)chdlc+sizeof(*chdlc)); |
---|
199 | } |
---|
200 | |
---|
201 | void *trace_get_payload_from_link(void *link, libtrace_linktype_t linktype, |
---|
202 | uint16_t *ethertype, uint32_t *remaining) |
---|
203 | { |
---|
204 | void *l = NULL; |
---|
205 | |
---|
206 | do { |
---|
207 | l = trace_get_payload_from_meta(link, &linktype, remaining); |
---|
208 | if (l != NULL) { |
---|
209 | link=l; |
---|
210 | continue; |
---|
211 | } |
---|
212 | } while (0); |
---|
213 | |
---|
214 | return trace_get_payload_from_layer2(link,linktype,ethertype,remaining); |
---|
215 | |
---|
216 | } |
---|
217 | |
---|
218 | DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet, |
---|
219 | libtrace_linktype_t *linktype, |
---|
220 | uint32_t *remaining) |
---|
221 | { |
---|
222 | uint32_t dummyrem; |
---|
223 | |
---|
224 | assert(packet != NULL); |
---|
225 | assert(linktype != NULL); |
---|
226 | |
---|
227 | if (remaining == NULL) |
---|
228 | remaining = &dummyrem; |
---|
229 | |
---|
230 | void *meta = trace_get_packet_meta(packet, linktype, remaining); |
---|
231 | |
---|
232 | /* If there are no meta-data headers, we just return the start of the |
---|
233 | * packet buffer, along with the linktype, etc. |
---|
234 | */ |
---|
235 | if (meta == NULL) |
---|
236 | return trace_get_packet_buffer(packet, linktype, remaining); |
---|
237 | |
---|
238 | /* If there are meta-data headers, we need to skip over them until we |
---|
239 | * find a non-meta data header and return that. |
---|
240 | */ |
---|
241 | for(;;) { |
---|
242 | void *nexthdr = trace_get_payload_from_meta(meta, |
---|
243 | linktype, remaining); |
---|
244 | if (nexthdr == NULL) |
---|
245 | return meta; |
---|
246 | meta = nexthdr; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | DLLEXPORT |
---|
251 | void *trace_get_payload_from_atm(void *link, |
---|
252 | uint8_t *type, uint32_t *remaining) |
---|
253 | { |
---|
254 | libtrace_atm_capture_cell_t *cell; |
---|
255 | if (remaining && *remaining<sizeof(libtrace_atm_capture_cell_t)) |
---|
256 | return NULL; |
---|
257 | cell=(libtrace_atm_capture_cell_t*)link; |
---|
258 | |
---|
259 | if (type) |
---|
260 | *type=cell->pt; |
---|
261 | |
---|
262 | if (remaining) |
---|
263 | *remaining-=sizeof(libtrace_atm_capture_cell_t); |
---|
264 | |
---|
265 | return ((char*)link)+sizeof(libtrace_atm_capture_cell_t); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | DLLEXPORT void *trace_get_payload_from_layer2(void *link, |
---|
271 | libtrace_linktype_t linktype, |
---|
272 | uint16_t *ethertype, |
---|
273 | uint32_t *remaining) |
---|
274 | { |
---|
275 | void *l; |
---|
276 | switch(linktype) { |
---|
277 | /* Packet Metadata headers, not layer2 headers */ |
---|
278 | case TRACE_TYPE_80211_PRISM: |
---|
279 | case TRACE_TYPE_80211_RADIO: |
---|
280 | case TRACE_TYPE_PFLOG: |
---|
281 | case TRACE_TYPE_LINUX_SLL: |
---|
282 | return NULL; |
---|
283 | |
---|
284 | /* duck packets have no payload! */ |
---|
285 | case TRACE_TYPE_DUCK: |
---|
286 | return NULL; |
---|
287 | |
---|
288 | /* The payload is in these packets does |
---|
289 | not correspond to a genuine link-layer |
---|
290 | */ |
---|
291 | case TRACE_TYPE_METADATA: |
---|
292 | return NULL; |
---|
293 | |
---|
294 | case TRACE_TYPE_80211: |
---|
295 | return trace_get_payload_from_80211(link,ethertype,remaining); |
---|
296 | case TRACE_TYPE_ETH: |
---|
297 | return trace_get_payload_from_ethernet(link,ethertype,remaining); |
---|
298 | case TRACE_TYPE_NONE: |
---|
299 | if ((*(char*)link&0xF0) == 0x40) |
---|
300 | *ethertype=0x0800; |
---|
301 | else if ((*(char*)link&0xF0) == 0x60) |
---|
302 | *ethertype=0x86DD; |
---|
303 | return link; /* I love the simplicity */ |
---|
304 | case TRACE_TYPE_PPP: |
---|
305 | return trace_get_payload_from_ppp(link,ethertype,remaining); |
---|
306 | case TRACE_TYPE_ATM: |
---|
307 | l=trace_get_payload_from_atm(link,NULL,remaining); |
---|
308 | /* FIXME: We shouldn't skip llcsnap here, we should return |
---|
309 | * an ethertype for it (somehow) |
---|
310 | */ |
---|
311 | return (l ? trace_get_payload_from_llcsnap(l, |
---|
312 | ethertype, remaining):NULL); |
---|
313 | case TRACE_TYPE_LLCSNAP: |
---|
314 | return trace_get_payload_from_llcsnap(link,ethertype,remaining); |
---|
315 | |
---|
316 | case TRACE_TYPE_HDLC_POS: |
---|
317 | return trace_get_payload_from_chdlc(link,ethertype, |
---|
318 | remaining); |
---|
319 | /* TODO: Unsupported */ |
---|
320 | case TRACE_TYPE_POS: |
---|
321 | case TRACE_TYPE_AAL5: |
---|
322 | return NULL; |
---|
323 | } |
---|
324 | return NULL; |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | |
---|