source: lib/protocols_pktmeta.c @ 89e2ff7

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 89e2ff7 was ee6e802, checked in by Shane Alcock <salcock@…>, 5 years ago

Updated copyright blurb on all source files

In some cases, this meant adding copyright blurbs to files that
had never had them before.

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