| 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 | */ |
|---|
| 59 | DLLEXPORT 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, ðertype, &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 | |
|---|
| 134 | DLLEXPORT 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 | void *link; |
|---|
| 150 | libtrace_linktype_t linktype; |
|---|
| 151 | link = trace_get_packet_buffer(packet, &linktype, remaining); |
|---|
| 152 | if (!link) |
|---|
| 153 | return NULL; |
|---|
| 154 | *proto = packet->transport_proto; |
|---|
| 155 | *remaining -= (packet->l4_header - link); |
|---|
| 156 | return packet->l4_header; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | transport = trace_get_layer3(packet,ðertype,remaining); |
|---|
| 160 | |
|---|
| 161 | if (!transport || *remaining == 0) |
|---|
| 162 | return NULL; |
|---|
| 163 | |
|---|
| 164 | switch (ethertype) { |
|---|
| 165 | case TRACE_ETHERTYPE_IP: /* IPv4 */ |
|---|
| 166 | transport=trace_get_payload_from_ip( |
|---|
| 167 | (libtrace_ip_t*)transport, proto, remaining); |
|---|
| 168 | /* IPv6 */ |
|---|
| 169 | if (transport && *proto == TRACE_IPPROTO_IPV6) { |
|---|
| 170 | transport=trace_get_payload_from_ip6( |
|---|
| 171 | (libtrace_ip6_t*)transport, proto,remaining); |
|---|
| 172 | } |
|---|
| 173 | break; |
|---|
| 174 | case TRACE_ETHERTYPE_IPV6: /* IPv6 */ |
|---|
| 175 | transport = trace_get_payload_from_ip6( |
|---|
| 176 | (libtrace_ip6_t*)transport, proto, remaining); |
|---|
| 177 | break; |
|---|
| 178 | default: |
|---|
| 179 | *proto = 0; |
|---|
| 180 | transport = NULL; |
|---|
| 181 | break; |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | ((libtrace_packet_t *)packet)->transport_proto = *proto; |
|---|
| 186 | ((libtrace_packet_t *)packet)->l4_header = transport; |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | return transport; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | DLLEXPORT libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet) { |
|---|
| 193 | uint8_t proto; |
|---|
| 194 | uint32_t rem = 0; |
|---|
| 195 | libtrace_tcp_t *tcp; |
|---|
| 196 | |
|---|
| 197 | tcp=(libtrace_tcp_t*)trace_get_transport(packet,&proto,&rem); |
|---|
| 198 | |
|---|
| 199 | if (!tcp || proto != TRACE_IPPROTO_TCP) |
|---|
| 200 | return NULL; |
|---|
| 201 | |
|---|
| 202 | /* We should return NULL if there isn't a full TCP header, because the |
|---|
| 203 | * caller has no way of telling how much of a TCP header we have |
|---|
| 204 | * returned - use trace_get_transport() if you want to deal with |
|---|
| 205 | * partial headers |
|---|
| 206 | * |
|---|
| 207 | * NOTE: We're not going to insist that all the TCP options are present |
|---|
| 208 | * as well, because lots of traces are snapped after 20 bytes of TCP |
|---|
| 209 | * header and I don't really want to break libtrace programs that |
|---|
| 210 | * use this function to process those traces */ |
|---|
| 211 | |
|---|
| 212 | if (rem < sizeof(libtrace_tcp_t)) |
|---|
| 213 | return NULL; |
|---|
| 214 | |
|---|
| 215 | return (libtrace_tcp_t*)tcp; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | DLLEXPORT libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining) |
|---|
| 219 | { |
|---|
| 220 | libtrace_tcp_t *tcpptr = 0; |
|---|
| 221 | |
|---|
| 222 | if (ip->ip_p == TRACE_IPPROTO_TCP) { |
|---|
| 223 | tcpptr = (libtrace_tcp_t *) |
|---|
| 224 | trace_get_payload_from_ip(ip, NULL, remaining); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | return tcpptr; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | DLLEXPORT libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet) { |
|---|
| 231 | uint8_t proto; |
|---|
| 232 | uint32_t rem = 0; |
|---|
| 233 | libtrace_udp_t *udp; |
|---|
| 234 | |
|---|
| 235 | udp=(libtrace_udp_t*)trace_get_transport(packet,&proto,&rem); |
|---|
| 236 | |
|---|
| 237 | if (!udp || proto != TRACE_IPPROTO_UDP) |
|---|
| 238 | return NULL; |
|---|
| 239 | |
|---|
| 240 | /* Make sure we return a full UDP header as the caller has no way of |
|---|
| 241 | * telling how much of the packet is remaining */ |
|---|
| 242 | if (rem < sizeof(libtrace_udp_t)) |
|---|
| 243 | return NULL; |
|---|
| 244 | |
|---|
| 245 | return udp; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | DLLEXPORT libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip, uint32_t *remaining) |
|---|
| 249 | { |
|---|
| 250 | libtrace_udp_t *udpptr = 0; |
|---|
| 251 | |
|---|
| 252 | if (ip->ip_p == TRACE_IPPROTO_UDP) { |
|---|
| 253 | udpptr = (libtrace_udp_t *) |
|---|
| 254 | trace_get_payload_from_ip(ip, NULL, remaining); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | return udpptr; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | DLLEXPORT libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet) { |
|---|
| 261 | uint8_t proto; |
|---|
| 262 | uint32_t rem = 0; |
|---|
| 263 | libtrace_icmp_t *icmp; |
|---|
| 264 | |
|---|
| 265 | icmp=(libtrace_icmp_t*)trace_get_transport(packet,&proto,&rem); |
|---|
| 266 | |
|---|
| 267 | if (!icmp || proto != TRACE_IPPROTO_ICMP) |
|---|
| 268 | return NULL; |
|---|
| 269 | |
|---|
| 270 | /* Make sure we return a full ICMP header as the caller has no way of |
|---|
| 271 | * telling how much of the packet is remaining */ |
|---|
| 272 | if (rem < sizeof(libtrace_icmp_t)) |
|---|
| 273 | return NULL; |
|---|
| 274 | |
|---|
| 275 | return icmp; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | DLLEXPORT libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip, uint32_t *remaining) |
|---|
| 279 | { |
|---|
| 280 | libtrace_icmp_t *icmpptr = 0; |
|---|
| 281 | |
|---|
| 282 | if (ip->ip_p == TRACE_IPPROTO_ICMP) { |
|---|
| 283 | icmpptr = (libtrace_icmp_t *)trace_get_payload_from_ip(ip, |
|---|
| 284 | NULL, remaining); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | return icmpptr; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining) |
|---|
| 291 | { |
|---|
| 292 | if (remaining) { |
|---|
| 293 | if (*remaining < sizeof(libtrace_udp_t)) { |
|---|
| 294 | *remaining = 0; |
|---|
| 295 | return NULL; |
|---|
| 296 | } |
|---|
| 297 | *remaining-=sizeof(libtrace_udp_t); |
|---|
| 298 | } |
|---|
| 299 | return (void*)((char*)udp+sizeof(libtrace_udp_t)); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining) |
|---|
| 303 | { |
|---|
| 304 | unsigned int dlen = tcp->doff*4; |
|---|
| 305 | if (remaining) { |
|---|
| 306 | if (*remaining < dlen) { |
|---|
| 307 | *remaining = 0; |
|---|
| 308 | return NULL; |
|---|
| 309 | } |
|---|
| 310 | *remaining-=dlen; |
|---|
| 311 | } |
|---|
| 312 | return (void *)((char *)tcp+dlen); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *remaining) |
|---|
| 316 | { |
|---|
| 317 | if (remaining) { |
|---|
| 318 | if (*remaining < sizeof(libtrace_icmp_t)) { |
|---|
| 319 | *remaining = 0; |
|---|
| 320 | return NULL; |
|---|
| 321 | } |
|---|
| 322 | *remaining-=sizeof(libtrace_icmp_t); |
|---|
| 323 | } |
|---|
| 324 | return (char*)icmp+sizeof(libtrace_icmp_t); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /* Return the source port |
|---|
| 328 | */ |
|---|
| 329 | DLLEXPORT uint16_t trace_get_source_port(const libtrace_packet_t *packet) |
|---|
| 330 | { |
|---|
| 331 | uint32_t remaining; |
|---|
| 332 | uint8_t proto; |
|---|
| 333 | const struct ports_t *port = |
|---|
| 334 | (const struct ports_t*)trace_get_transport((libtrace_packet_t*)packet, |
|---|
| 335 | &proto, &remaining); |
|---|
| 336 | |
|---|
| 337 | /* Snapped too early */ |
|---|
| 338 | if (remaining<2) |
|---|
| 339 | return 0; |
|---|
| 340 | |
|---|
| 341 | /* ICMP *technically* doesn't have ports */ |
|---|
| 342 | if (proto == TRACE_IPPROTO_ICMP) |
|---|
| 343 | return 0; |
|---|
| 344 | |
|---|
| 345 | if (port) |
|---|
| 346 | return ntohs(port->src); |
|---|
| 347 | else |
|---|
| 348 | return 0; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | /* Same as get_source_port except use the destination port */ |
|---|
| 352 | DLLEXPORT uint16_t trace_get_destination_port(const libtrace_packet_t *packet) |
|---|
| 353 | { |
|---|
| 354 | uint32_t remaining; |
|---|
| 355 | uint8_t proto; |
|---|
| 356 | struct ports_t *port = |
|---|
| 357 | (struct ports_t*)trace_get_transport((libtrace_packet_t*)packet, |
|---|
| 358 | &proto, &remaining); |
|---|
| 359 | /* Snapped too early */ |
|---|
| 360 | if (remaining<4) |
|---|
| 361 | return 0; |
|---|
| 362 | |
|---|
| 363 | /* ICMP *technically* doesn't have ports */ |
|---|
| 364 | if (proto == TRACE_IPPROTO_ICMP) |
|---|
| 365 | return 0; |
|---|
| 366 | |
|---|
| 367 | if (port) |
|---|
| 368 | return ntohs(port->dst); |
|---|
| 369 | else |
|---|
| 370 | return 0; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|