1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/*
* This file is part of libtrace
*
* Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
* New Zealand.
*
* Authors: Daniel Lawson
* Perry Lorier
* Shane Alcock
*
* All rights reserved.
*
* This code has been developed by the University of Waikato WAND
* research group. For further information please see http://www.wand.net.nz/
*
* libtrace is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libtrace is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libtrace; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: protocols_l3.c 1645 2010-08-02 03:48:16Z salcock $
*
*/
#include "libtrace_int.h"
#include "libtrace.h"
#include "protocols.h"
#include <assert.h>
#include <stdlib.h>
#ifdef HAVE_NETPACKET_PACKET_H
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <string.h>
#else
#include <net/if_dl.h>
#include <string.h>
#endif
/* This file contains all the protocol decoding functions for layer 3
* (the IP layer) protocols. This includes functions for accessing IP
* addresses.
*
* Supported protocols include:
* IPv4
* IPv6
*/
/* Gets an IPv4 header */
libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet)
{
uint16_t ethertype;
void *ret;
uint32_t remaining = trace_get_capture_length(packet);
ret = trace_get_layer3(packet,ðertype,&remaining);
if (!ret || ethertype!=TRACE_ETHERTYPE_IP)
return NULL;
/* Make sure we have at least a base IPv4 header */
if (remaining < sizeof(libtrace_ip_t))
return NULL;
/* Not an IPv4 packet */
if (((libtrace_ip_t*)ret)->ip_v != 4)
return NULL;
return (libtrace_ip_t*)ret;
}
libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet)
{
uint16_t ethertype;
void *ret;
uint32_t remaining = trace_get_capture_length(packet);
ret = trace_get_layer3(packet,ðertype,&remaining);
if (!ret || ethertype!=TRACE_ETHERTYPE_IPV6)
return NULL;
/* Make sure we have at least the base IPv6 header */
if (remaining < sizeof(libtrace_ip6_t))
return NULL;
return (libtrace_ip6_t*)ret;
}
#define SW_IP_OFFMASK 0x1fff
DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ipptr, uint8_t *prot,
uint32_t *remaining)
{
void *trans_ptr = 0;
assert(ipptr != NULL);
/* Er? IPv5? */
if (ipptr->ip_v != 4)
return NULL;
if ((ntohs(ipptr->ip_off) & SW_IP_OFFMASK) != 0) {
if (remaining)
*remaining = 0;
return NULL;
}
if (remaining) {
if (*remaining<(ipptr->ip_hl*4U)) {
*remaining = 0;
return NULL;
}
*remaining-=(ipptr->ip_hl * 4);
}
trans_ptr = (void *)((char *)ipptr + (ipptr->ip_hl * 4));
if (prot) *prot = ipptr->ip_p;
return trans_ptr;
}
void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr, uint8_t *prot,
uint32_t *remaining)
{
void *payload = (char*)ipptr+sizeof(libtrace_ip6_t);
uint8_t nxt;
assert (ipptr != NULL);
nxt = ipptr->nxt;
if (remaining) {
if (*remaining<sizeof(libtrace_ip6_t)) {
*remaining = 0;
return NULL;
}
*remaining-=sizeof(libtrace_ip6_t);
}
while(1) {
switch (nxt) {
case 0: /* hop by hop options */
case TRACE_IPPROTO_ROUTING:
case TRACE_IPPROTO_FRAGMENT:
case TRACE_IPPROTO_ESP:
case TRACE_IPPROTO_AH:
case TRACE_IPPROTO_DSTOPTS:
{
uint16_t len=((libtrace_ip6_ext_t*)payload)->len
+sizeof(libtrace_ip6_ext_t);
if (remaining) {
if (*remaining < len) {
/* Snap too short */
*remaining = 0;
return NULL;
}
*remaining-=len;
}
payload=(char*)payload+len;
nxt=((libtrace_ip6_ext_t*)payload)->nxt;
continue;
}
default:
if (prot) *prot=nxt;
return payload;
}
}
}
DLLEXPORT void *trace_get_layer3(const libtrace_packet_t *packet,
uint16_t *ethertype,
uint32_t *remaining)
{
void *iphdr;
uint16_t dummy_ethertype;
void *link;
uint32_t dummy_remaining;
libtrace_linktype_t linktype;
if (!ethertype) ethertype=&dummy_ethertype;
if (!remaining) remaining=&dummy_remaining;
/* use l3 cache */
if (packet->l3_header)
{
link = trace_get_packet_buffer(packet,&linktype,remaining);
if (!link)
return NULL;
*ethertype = packet->l3_ethertype;
*remaining -= (packet->l3_header - link);
return packet->l3_header;
}
link = trace_get_layer2(packet,&linktype,remaining);
iphdr = trace_get_payload_from_layer2(
link,
linktype,
ethertype,
remaining);
for(;;) {
if (!iphdr || *remaining == 0)
break;
switch(*ethertype) {
case TRACE_ETHERTYPE_8021Q: /* VLAN */
iphdr=trace_get_payload_from_vlan(
iphdr,ethertype,remaining);
continue;
case TRACE_ETHERTYPE_MPLS: /* MPLS */
iphdr=trace_get_payload_from_mpls(
iphdr,ethertype,remaining);
if (iphdr && ethertype == 0x0) {
iphdr=trace_get_payload_from_ethernet(
iphdr,ethertype,remaining);
}
continue;
case TRACE_ETHERTYPE_PPP_SES: /* PPPoE */
iphdr = trace_get_payload_from_pppoe(iphdr, ethertype,
remaining);
continue;
default:
break;
}
break;
}
if (!iphdr || *remaining == 0)
return NULL;
/* Store values in the cache for later */
/* Cast away constness, nasty, but this is just a cache */
((libtrace_packet_t*)packet)->l3_ethertype = *ethertype;
((libtrace_packet_t*)packet)->l3_header = iphdr;
return iphdr;
}
/* Parse an ip or tcp option
* @param[in,out] ptr the pointer to the current option
* @param[in,out] len the length of the remaining buffer
* @param[out] type the type of the option
* @param[out] optlen the length of the option
* @param[out] data the data of the option
*
* @returns bool true if there is another option (and the fields are filled in)
* or false if this was the last option.
*
* This updates ptr to point to the next option after this one, and updates
* len to be the number of bytes remaining in the options area. Type is updated
* to be the code of this option, and data points to the data of this option,
* with optlen saying how many bytes there are.
*
* @note Beware of fragmented packets.
* @author Perry Lorier
*/
DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
unsigned char *type,
unsigned char *optlen,
unsigned char **data)
{
if (*len<=0)
return 0;
*type=**ptr;
switch(*type) {
case 0: /* End of options */
return 0;
case 1: /* Pad */
(*ptr)++;
(*len)--;
return 1;
default:
*optlen = *(*ptr+1);
if (*optlen<2)
return 0; /* I have no idea wtf is going on
* with these packets
*/
/* Ensure that optlen is not greater than the
* amount of buffer remaining */
if (*optlen > *len)
return 0;
(*len)-=*optlen;
(*data)=(*ptr+2);
(*ptr)+=*optlen;
if (*len<0)
return 0;
return 1;
}
assert(0);
}
/* Extract the source mac address from a frame and bundle it up into a sockaddr */
static struct sockaddr *get_source_ethernet_address(
libtrace_ether_t *ethernet, struct sockaddr *addr)
{
static struct sockaddr_storage dummy;
#ifdef HAVE_NETPACKET_PACKET_H
/* Use linux's sockaddr_ll structure */
struct sockaddr_ll *l2addr;
if (addr)
l2addr = (struct sockaddr_ll*)addr;
else
l2addr = (struct sockaddr_ll*)&dummy;
l2addr->sll_family = AF_PACKET;
l2addr->sll_protocol = ethernet->ether_type;
l2addr->sll_ifindex = 0; /* Irrelevant */
l2addr->sll_hatype = ARPHRD_ETHER;
l2addr->sll_pkttype = PACKET_OTHERHOST;
l2addr->sll_halen = 6;
memcpy(l2addr->sll_addr,ethernet->ether_shost, 6);
return (struct sockaddr*)l2addr;
#else
/* Use BSD's sockaddr_dl structure */
struct sockaddr_dl *l2addr;
if (addr)
l2addr = (struct sockaddr_dl *)addr;
else
l2addr = (struct sockaddr_dl *)&dummy;
l2addr->sdl_family = AF_LINK;
#if HAVE_SDL_LEN == 1
l2addr->sdl_len = sizeof(struct sockaddr_dl);
#endif
l2addr->sdl_index = 0; /* Unused */
l2addr->sdl_alen = 6; /* Address length */
l2addr->sdl_nlen = 0; /* No name in here - this *should* work, right? */
l2addr->sdl_slen = 0;
l2addr->sdl_type = 0; /* Hopefully zero is OK for this value too */
memcpy(l2addr->sdl_data, ethernet->ether_shost, 6);
return (struct sockaddr *)l2addr;
#endif
}
static struct sockaddr *get_source_l2_address(
const libtrace_packet_t *packet, struct sockaddr *addr)
{
static struct sockaddr_storage dummy;
void *l2;
libtrace_linktype_t linktype;
uint32_t remaining;
if (!addr)
addr =(struct sockaddr*)&dummy;
l2=trace_get_layer2(packet, &linktype, &remaining);
if (!l2) {
return NULL;
}
switch (linktype) {
case TRACE_TYPE_ETH:
return get_source_ethernet_address((libtrace_ether_t*)l2, addr);
default:
return NULL;
}
}
DLLEXPORT struct sockaddr *trace_get_source_address(
const libtrace_packet_t *packet, struct sockaddr *addr)
{
uint16_t ethertype;
uint32_t remaining;
void *l3;
struct ports_t *ports;
static struct sockaddr_storage dummy;
if (!addr)
addr=(struct sockaddr*)&dummy;
l3 = trace_get_layer3(packet,ðertype,&remaining);
if (!l3)
return get_source_l2_address(packet,addr);
switch (ethertype) {
case TRACE_ETHERTYPE_IP: /* IPv4 */
{
struct sockaddr_in *addr4=(struct sockaddr_in*)addr;
libtrace_ip_t *ip = (libtrace_ip_t*)l3;
ports = (struct ports_t*)
trace_get_payload_from_ip(ip,NULL,&remaining);
addr4->sin_family=AF_INET;
if (ports && remaining>=sizeof(*ports))
addr4->sin_port=ports->src;
else
addr4->sin_port=0;
addr4->sin_addr=ip->ip_src;
return addr;
}
case TRACE_ETHERTYPE_IPV6: /* IPv6 */
{
struct sockaddr_in6 *addr6=(struct sockaddr_in6*)addr;
libtrace_ip6_t *ip6 = (libtrace_ip6_t*)l3;
ports = (struct ports_t*)
trace_get_payload_from_ip6(ip6,NULL,&remaining);
addr6->sin6_family=AF_INET6;
if (ports && remaining>=sizeof(*ports))
addr6->sin6_port=ports->src;
else
addr6->sin6_port=0;
addr6->sin6_flowinfo=0;
addr6->sin6_addr=ip6->ip_src;
return addr;
}
default:
return get_source_l2_address(packet, addr);
}
}
static struct sockaddr *get_destination_ethernet_address(
libtrace_ether_t *ethernet, struct sockaddr *addr)
{
static struct sockaddr_storage dummy;
#ifdef HAVE_NETPACKET_PACKET_H
/* Use linux's sockaddr_ll structure */
struct sockaddr_ll *l2addr;
if (addr)
l2addr = (struct sockaddr_ll*)addr;
else
l2addr = (struct sockaddr_ll*)&dummy;
l2addr->sll_family = AF_PACKET;
l2addr->sll_protocol = ethernet->ether_type;
l2addr->sll_ifindex = 0; /* Irrelevant */
l2addr->sll_hatype = ARPHRD_ETHER;
l2addr->sll_pkttype = PACKET_OTHERHOST;
l2addr->sll_halen = 6;
memcpy(l2addr->sll_addr,ethernet->ether_dhost, 6);
return (struct sockaddr*)l2addr;
#else
/* Use BSD's sockaddr_dl structure */
struct sockaddr_dl *l2addr;
if (addr)
l2addr = (struct sockaddr_dl *)addr;
else
l2addr = (struct sockaddr_dl *)&dummy;
l2addr->sdl_family = AF_LINK;
#if HAVE_SDL_LEN == 1
l2addr->sdl_len = sizeof(struct sockaddr_dl);
#endif
l2addr->sdl_index = 0; /* Unused */
l2addr->sdl_alen = 6; /* Address length */
l2addr->sdl_nlen = 0; /* No name in here - this *should* work, right? */
l2addr->sdl_slen = 0;
l2addr->sdl_type = 0; /* Hopefully zero is OK for this value too */
memcpy(l2addr->sdl_data, ethernet->ether_dhost, 6);
return (struct sockaddr *)l2addr;
#endif
}
static struct sockaddr *get_destination_l2_address(
const libtrace_packet_t *packet, struct sockaddr *addr)
{
static struct sockaddr_storage dummy;
void *l2;
libtrace_linktype_t linktype;
uint32_t remaining;
if (!addr)
addr =(struct sockaddr*)&dummy;
l2=trace_get_layer2(packet, &linktype, &remaining);
if (!l2)
return NULL;
switch (linktype) {
case TRACE_TYPE_ETH:
return get_destination_ethernet_address((libtrace_ether_t*)l2, addr);
default:
return NULL;
}
}
DLLEXPORT struct sockaddr *trace_get_destination_address(
const libtrace_packet_t *packet, struct sockaddr *addr)
{
uint16_t ethertype;
uint32_t remaining;
void *l3;
struct ports_t *ports;
static struct sockaddr_storage dummy;
if (!addr)
addr=(struct sockaddr*)&dummy;
l3 = trace_get_layer3(packet,ðertype,&remaining);
if (!l3)
return get_destination_l2_address(packet,addr);
switch (ethertype) {
case TRACE_ETHERTYPE_IP: /* IPv4 */
{
struct sockaddr_in *addr4=(struct sockaddr_in*)addr;
libtrace_ip_t *ip = (libtrace_ip_t*)l3;
ports = (struct ports_t*)
trace_get_payload_from_ip(ip,NULL,&remaining);
addr4->sin_family=AF_INET;
if (ports && remaining>=sizeof(*ports))
addr4->sin_port=ports->dst;
else
addr4->sin_port=0;
addr4->sin_addr=ip->ip_dst;
return addr;
}
case TRACE_ETHERTYPE_IPV6: /* IPv6 */
{
struct sockaddr_in6 *addr6=(struct sockaddr_in6*)addr;
libtrace_ip6_t *ip6 = (libtrace_ip6_t*)l3;
ports = (struct ports_t*)
trace_get_payload_from_ip6(ip6,NULL,&remaining);
addr6->sin6_family=AF_INET6;
if (ports && remaining>=sizeof(*ports))
addr6->sin6_port=ports->dst;
else
addr6->sin6_port=0;
addr6->sin6_flowinfo=0;
addr6->sin6_addr=ip6->ip_dst;
return addr;
}
default:
return get_destination_l2_address(packet, addr);
}
}
|