source: lib/protocols_transport.c @ ee58d0d

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since ee58d0d was ee58d0d, checked in by Shane Alcock <salcock@…>, 10 years ago
  • Improved performance by caching more stuff, especially L2 headers and various "remaining" values - not much point caching something if you need to re-read the entire packet to calculate "remaining" still :)
  • Also improved performance of trace_get_layer2 by avoiding an effective double call of trace_get_packet_buffer
  • Fixed bug with payload length calculation if the packet has extra padding beyond what the IP len states
  • Improved (hopefully) performance when reading ERF traces by avoiding updating the drops counter if the loss counter was zero (should save an ntohs at least)
  • Property mode set to 100644
File size: 9.0 KB
Line 
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
35#include "libtrace.h"
36#include "protocols.h"
37#include <assert.h>
38#include <stdlib.h>
39#include <stdio.h> // fprintf
40
41/* This file contains all the protocol decoding functions for transport layer
42 * protocols. This includes functions for access port numbers.
43 *
44 * Supported protocols include (but are not limited to):
45 *      TCP
46 *      UDP
47 *      ICMP
48 */
49
50/* Get the size of the payload as it was in the original packet, i.e. prior
51 * to any truncation.
52 *
53 * Basically, wire length minus the packet headers.
54 *
55 * Currently only supports IP (v4 and v6) and TCP, UDP and ICMP. Will return
56 * 0 if an unsupported protocol header is encountered, or if one of the
57 * headers is truncated.
58 */
59DLLEXPORT size_t trace_get_payload_length(const libtrace_packet_t *packet) {
60
61        void *layer;
62        uint16_t ethertype;
63        uint8_t proto;
64        uint32_t rem;
65        libtrace_ip_t *ip;
66        libtrace_ip6_t *ip6;
67        libtrace_tcp_t *tcp;
68        size_t len = 0;
69
70        /* Just use the cached length if we can */
71        if (packet->payload_length != -1)
72                return packet->payload_length; 
73
74        /* Set to zero so that we can return early without having to
75         * worry about forgetting to update the cached value */
76        ((libtrace_packet_t *)packet)->payload_length = 0;
77        layer = trace_get_layer3(packet, &ethertype, &rem);
78        if (!layer)
79                return 0;
80        switch (ethertype) {
81                case TRACE_ETHERTYPE_IP:
82                        ip = (libtrace_ip_t *)layer;
83                        if (rem < sizeof(libtrace_ip_t))
84                                return 0;
85                        len = ntohs(ip->ip_len) - (4 * ip->ip_hl);
86               
87                        /* Deal with v6 within v4 */
88                        if (ip->ip_p == TRACE_IPPROTO_IPV6)
89                                len -= sizeof(libtrace_ip6_t);
90                       
91                        break;
92                case TRACE_ETHERTYPE_IPV6:
93                        ip6 = (libtrace_ip6_t *)layer;
94                        if (rem < sizeof(libtrace_ip6_t))
95                                return 0;
96                        len = ntohs(ip6->plen);
97                        break;
98                default:
99                        return 0;
100        }
101
102        layer = trace_get_transport(packet, &proto, &rem);
103        if (!layer)
104                return 0;
105       
106        switch(proto) {
107                case TRACE_IPPROTO_TCP:
108                        if (rem < sizeof(libtrace_tcp_t))
109                                return 0;
110                        tcp = (libtrace_tcp_t *)layer;
111                       
112                        if (len < (size_t)(4 * tcp->doff))
113                                return 0;
114                       
115                        len -= (4 * tcp->doff);
116                        break;
117                case TRACE_IPPROTO_UDP:
118                        len -= sizeof(libtrace_udp_t);
119                        break;
120                case TRACE_IPPROTO_ICMP:
121                        len -= sizeof(libtrace_icmp_t);
122                        break;
123               
124                default:
125                        return 0;
126        }
127
128
129        ((libtrace_packet_t *)packet)->payload_length = len;
130        return len;
131
132}
133
134DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet, 
135                uint8_t *proto,
136                uint32_t *remaining
137                ) 
138{
139        uint8_t dummy_proto;
140        uint16_t ethertype;
141        uint32_t dummy_remaining;
142        void *transport;
143
144        if (!proto) proto=&dummy_proto;
145
146        if (!remaining) remaining=&dummy_remaining;
147
148        if (packet->l4_header) {
149                /*
150                void *link;
151                libtrace_linktype_t linktype;
152                link = trace_get_packet_buffer(packet, &linktype, remaining);
153                if (!link)
154                        return NULL;
155                */
156                *proto = packet->transport_proto;
157                /* *remaining -= (packet->l4_header - link); */
158                *remaining = packet->l4_remaining;
159                return packet->l4_header;
160        }
161
162        transport = trace_get_layer3(packet,&ethertype,remaining);
163
164        if (!transport || *remaining == 0)
165                return NULL;
166
167        switch (ethertype) {
168                case TRACE_ETHERTYPE_IP: /* IPv4 */
169                        transport=trace_get_payload_from_ip(
170                                (libtrace_ip_t*)transport, proto, remaining);
171                        /* IPv6 */
172                        if (transport && *proto == TRACE_IPPROTO_IPV6) {
173                                transport=trace_get_payload_from_ip6(
174                                 (libtrace_ip6_t*)transport, proto,remaining);
175                        }
176                        break;
177                case TRACE_ETHERTYPE_IPV6: /* IPv6 */
178                        transport = trace_get_payload_from_ip6(
179                                (libtrace_ip6_t*)transport, proto, remaining);
180                        break;
181                default:
182                        *proto = 0;
183                        transport = NULL;
184                        break;
185                       
186        }
187
188        ((libtrace_packet_t *)packet)->transport_proto = *proto;
189        ((libtrace_packet_t *)packet)->l4_header = transport;
190        ((libtrace_packet_t *)packet)->l4_remaining = *remaining;
191
192
193        return transport;
194}
195
196DLLEXPORT libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet) {
197        uint8_t proto;
198        uint32_t rem = 0;
199        libtrace_tcp_t *tcp;
200
201        tcp=(libtrace_tcp_t*)trace_get_transport(packet,&proto,&rem);
202
203        if (!tcp || proto != TRACE_IPPROTO_TCP)
204                return NULL;
205
206        /* We should return NULL if there isn't a full TCP header, because the
207         * caller has no way of telling how much of a TCP header we have
208         * returned - use trace_get_transport() if you want to deal with
209         * partial headers
210         *
211         * NOTE: We're not going to insist that all the TCP options are present
212         * as well, because lots of traces are snapped after 20 bytes of TCP
213         * header and I don't really want to break libtrace programs that
214         * use this function to process those traces */
215
216        if (rem < sizeof(libtrace_tcp_t))
217                return NULL;
218
219        return (libtrace_tcp_t*)tcp;
220}
221
222DLLEXPORT libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining)
223{
224        libtrace_tcp_t *tcpptr = 0;
225
226        if (ip->ip_p == TRACE_IPPROTO_TCP)  {
227                tcpptr = (libtrace_tcp_t *)
228                        trace_get_payload_from_ip(ip, NULL, remaining);
229        }
230
231        return tcpptr;
232}
233
234DLLEXPORT libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet) {
235        uint8_t proto;
236        uint32_t rem = 0;
237        libtrace_udp_t *udp;
238
239        udp=(libtrace_udp_t*)trace_get_transport(packet,&proto,&rem);
240
241        if (!udp || proto != TRACE_IPPROTO_UDP)
242                return NULL;
243
244        /* Make sure we return a full UDP header as the caller has no way of
245         * telling how much of the packet is remaining */
246        if (rem < sizeof(libtrace_udp_t))
247                return NULL;
248
249        return udp;
250}
251
252DLLEXPORT libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip, uint32_t *remaining)
253{
254        libtrace_udp_t *udpptr = 0;
255
256        if (ip->ip_p == TRACE_IPPROTO_UDP) {
257                udpptr = (libtrace_udp_t *)
258                        trace_get_payload_from_ip(ip, NULL, remaining);
259        }
260
261        return udpptr;
262}
263
264DLLEXPORT libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet) {
265        uint8_t proto;
266        uint32_t rem = 0;
267        libtrace_icmp_t *icmp;
268
269        icmp=(libtrace_icmp_t*)trace_get_transport(packet,&proto,&rem);
270
271        if (!icmp || proto != TRACE_IPPROTO_ICMP)
272                return NULL;
273
274        /* Make sure we return a full ICMP header as the caller has no way of
275         * telling how much of the packet is remaining */
276        if (rem < sizeof(libtrace_icmp_t))
277                return NULL;
278
279        return icmp;
280}
281
282DLLEXPORT libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip, uint32_t *remaining)
283{
284        libtrace_icmp_t *icmpptr = 0;
285
286        if (ip->ip_p == TRACE_IPPROTO_ICMP)  {
287                icmpptr = (libtrace_icmp_t *)trace_get_payload_from_ip(ip, 
288                                NULL, remaining);
289        }
290
291        return icmpptr;
292}
293
294DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining)
295{
296        if (remaining) {
297                if (*remaining < sizeof(libtrace_udp_t)) {
298                        *remaining = 0;
299                        return NULL;
300                }
301                *remaining-=sizeof(libtrace_udp_t);
302        }
303        return (void*)((char*)udp+sizeof(libtrace_udp_t));
304}
305
306DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining)
307{
308        unsigned int dlen = tcp->doff*4;
309        if (remaining) {
310                if (*remaining < dlen) {
311                        *remaining = 0;
312                        return NULL;
313                }
314                *remaining-=dlen;
315        }
316        return (void *)((char *)tcp+dlen);
317}
318
319DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *remaining)
320{
321        if (remaining) {
322                if (*remaining < sizeof(libtrace_icmp_t)) {
323                        *remaining = 0;
324                        return NULL;
325                }
326                *remaining-=sizeof(libtrace_icmp_t);
327        }
328        return (char*)icmp+sizeof(libtrace_icmp_t);
329}
330
331/* Return the source port
332 */
333DLLEXPORT uint16_t trace_get_source_port(const libtrace_packet_t *packet)
334{
335        uint32_t remaining;
336        uint8_t proto;
337        const struct ports_t *port = 
338                (const struct ports_t*)trace_get_transport((libtrace_packet_t*)packet,
339                        &proto, &remaining);
340
341        /* Snapped too early */
342        if (remaining<2)
343                return 0;
344
345        /* ICMP *technically* doesn't have ports */
346        if (proto == TRACE_IPPROTO_ICMP)
347                return 0;
348
349        if (port)
350                return ntohs(port->src);
351        else
352                return 0;
353}
354
355/* Same as get_source_port except use the destination port */
356DLLEXPORT uint16_t trace_get_destination_port(const libtrace_packet_t *packet)
357{
358        uint32_t remaining;
359        uint8_t proto;
360        struct ports_t *port = 
361                (struct ports_t*)trace_get_transport((libtrace_packet_t*)packet,
362                        &proto, &remaining);
363        /* Snapped too early */
364        if (remaining<4)
365                return 0;
366       
367        /* ICMP *technically* doesn't have ports */
368        if (proto == TRACE_IPPROTO_ICMP)
369                return 0;
370
371        if (port)
372                return ntohs(port->dst);
373        else
374                return 0;
375}
376
377
Note: See TracBrowser for help on using the repository browser.