Changeset c909fad for lib


Ignore:
Timestamp:
03/06/13 13:28:51 (8 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
68b7f29
Parents:
af27241
Message:
  • Added checksumming functions for both the IP and transport layer. The functions will calculate the correct checksum for that packet and also return a pointer to the checksum field in the appropriate header so that the caller can either evaluate whether the checksum is correct, replace the existing checksum, or do whatever they want.
  • Also managed to fix a bunch of broken Revision svn tags
Location:
lib
Files:
2 added
13 edited

Legend:

Unmodified
Added
Removed
  • lib/Makefile.am

    ra464de7 rc909fad  
    3434                libtrace_int.h lt_inttypes.h \
    3535                linktypes.c link_wireless.c \
     36                checksum.c checksum.h \
    3637                protocols_pktmeta.c protocols_l2.c protocols_l3.c \
    3738                protocols_transport.c protocols.h protocols_ospf.c \
  • lib/format_bpf.c

    ra464de7 rc909fad  
    516516/* Prints some slightly useful help text for the BPF capture format */
    517517static void bpf_help() {
    518         printf("bpf format module: $Revision$\n");
     518        printf("bpf format module: $Revision: 1782 $\n");
    519519        printf("Supported input URIs:\n");
    520520        printf("\tbpf:\n");
     
    567567/* Prints some slightly useful help text for the BPF capture format */
    568568static void bpf_help() {
    569         printf("bpf format module: $Revision$\n");
     569        printf("bpf format module: $Revision: 1782 $\n");
    570570        printf("Not supported on this host\n");
    571571        return;
  • lib/format_dag24.c

    r44028d4 rc909fad  
    506506/* Prints some semi-useful help text about the DAG format module */
    507507static void dag_help(void) {
    508         printf("dag format module: $Revision$\n");
     508        printf("dag format module: $Revision: 1715 $\n");
    509509        printf("Supported input URIs:\n");
    510510        printf("\tdag:/dev/dagn\n");
  • lib/format_dag25.c

    rc5a4210 rc909fad  
    11591159/* Prints some semi-useful help text about the DAG format module */
    11601160static void dag_help(void) {
    1161         printf("dag format module: $Revision$\n");
     1161        printf("dag format module: $Revision: 1755 $\n");
    11621162        printf("Supported input URIs:\n");
    11631163        printf("\tdag:/dev/dagn\n");
  • lib/format_erf.c

    re90d5a8 rc909fad  
    748748
    749749static void erf_help(void) {
    750         printf("erf format module: $Revision$\n");
     750        printf("erf format module: $Revision: 1752 $\n");
    751751        printf("Supported input URIs:\n");
    752752        printf("\terf:/path/to/file\t(uncompressed)\n");
  • lib/format_legacy.c

    rfefd34b rc909fad  
    467467
    468468static void legacypos_help(void) {
    469         printf("legacypos format module: $Revision$\n");
     469        printf("legacypos format module: $Revision: 1753 $\n");
    470470        printf("Supported input URIs:\n");
    471471        printf("\tlegacypos:/path/to/file\t(uncompressed)\n");
     
    478478
    479479static void legacyatm_help(void) {
    480         printf("legacyatm format module: $Revision$\n");
     480        printf("legacyatm format module: $Revision: 1753 $\n");
    481481        printf("Supported input URIs:\n");
    482482        printf("\tlegacyatm:/path/to/file\t(uncompressed)\n");
     
    489489
    490490static void legacyeth_help(void) {
    491         printf("legacyeth format module: $Revision$\n");
     491        printf("legacyeth format module: $Revision: 1753 $\n");
    492492        printf("Supported input URIs:\n");
    493493        printf("\tlegacyeth:/path/to/file\t(uncompressed)\n");
     
    500500
    501501static void legacynzix_help(void) {
    502         printf("legacynzix format module: $Revision$\n");
     502        printf("legacynzix format module: $Revision: 1753 $\n");
    503503        printf("Supported input URIs:\n");
    504504        printf("\tlegacynzix:/path/to/file\t(uncompressed)\n");
  • lib/format_linux.c

    r0801187 rc909fad  
    14821482#ifdef HAVE_NETPACKET_PACKET_H
    14831483static void linuxnative_help(void) {
    1484         printf("linuxnative format module: $Revision$\n");
     1484        printf("linuxnative format module: $Revision: 1793 $\n");
    14851485        printf("Supported input URIs:\n");
    14861486        printf("\tint:eth0\n");
     
    14931493
    14941494static void linuxring_help(void) {
    1495         printf("linuxring format module: $Revision$\n");
     1495        printf("linuxring format module: $Revision: 1793 $\n");
    14961496        printf("Supported input URIs:\n");
    14971497        printf("\tring:eth0\n");
     
    15901590#else
    15911591static void linuxnative_help(void) {
    1592         printf("linuxnative format module: $Revision$\n");
     1592        printf("linuxnative format module: $Revision: 1793 $\n");
    15931593        printf("Not supported on this host\n");
    15941594}
    15951595static void linuxring_help(void) {
    1596         printf("linuxring format module: $Revision$\n");
     1596        printf("linuxring format module: $Revision: 1793 $\n");
    15971597        printf("Not supported on this host\n");
    15981598}
  • lib/format_pcap.c

    rc0506ea rc909fad  
    737737
    738738static void pcap_help(void) {
    739         printf("pcap format module: $Revision$\n");
     739        printf("pcap format module: $Revision: 1729 $\n");
    740740        printf("Supported input URIs:\n");
    741741        printf("\tpcap:/path/to/file\n");
     
    749749
    750750static void pcapint_help(void) {
    751         printf("pcapint format module: $Revision$\n");
     751        printf("pcapint format module: $Revision: 1729 $\n");
    752752        printf("Supported input URIs:\n");
    753753        printf("\tpcapint:interface\n");
  • lib/format_pcapfile.c

    r64d4398 rc909fad  
    683683
    684684static void pcapfile_help(void) {
    685         printf("pcapfile format module: $Revision$\n");
     685        printf("pcapfile format module: $Revision: 1768 $\n");
    686686        printf("Supported input URIs:\n");
    687687        printf("\tpcapfile:/path/to/file\n");
  • lib/format_tsh.c

    r191f2de rc909fad  
    218218
    219219static void tsh_help(void) {
    220         printf("tsh format module: $Revision$\n");
     220        printf("tsh format module: $Revision: 1611 $\n");
    221221        printf("Supported input URIs:\n");
    222222        printf("\ttsh:/path/to/file\t(uncompressed)\n");
  • lib/libtrace.h.in

    r5699389 rc909fad  
    16731673                uint16_t *ethertype, uint32_t *remaining);
    16741674
     1675/** Calculates the expected IP checksum for a packet.
     1676 * @param packet        The libtrace packet to calculate the checksum for
     1677 * @param[out] csum     The checksum that is calculated by this function. This
     1678 *                      may not be NULL.
     1679 *
     1680 * @return A pointer to the original checksum field within the IP
     1681 * header. If the checksum field is not present in the packet, NULL is returned.
     1682 *
     1683 * @note The return value points to the checksum that exists within the current
     1684 * packet. The value in csum is the value that the checksum should be, given
     1685 * the current packet contents. 
     1686 *
     1687 * @note This function involves the use of a memcpy, so be careful about
     1688 * calling it excessively if performance is a concern for you.
     1689 *
     1690 * New in libtrace 3.0.17
     1691 */
     1692DLLEXPORT uint16_t *trace_checksum_layer3(libtrace_packet_t *packet,
     1693                uint16_t *csum);
     1694
     1695/** Calculates the expected checksum for the transport header in a packet.
     1696 * @param packet        The libtrace packet to calculate the checksum for
     1697 * @param[out] csum     The checksum that is calculated by this function. This
     1698 *                      may not be NULL.
     1699 *
     1700 * @return A pointer to the original checksum field within the transport
     1701 * header. If the checksum field is not present in the packet, NULL is returned.
     1702 *
     1703 * @note The return value points to the checksum that exists within the current
     1704 * packet. The value in csum is the value that the checksum should be, given
     1705 * the current packet contents. 
     1706 *
     1707 * @note This function involves the use of a memcpy, so be careful about
     1708 * calling it excessively if performance is a concern for you.
     1709 *
     1710 * @note Because transport checksums are calculated across the entire payload,
     1711 * truncated packets will result in NULL being returned.
     1712 *
     1713 * This function will determine the appropriate checksum for whatever transport
     1714 * layer header is present in the provided packet. At this stage, this only
     1715 * currently works for TCP, UDP and ICMP packets.
     1716 *
     1717 * Be wary of TCP checksum offloading if you are examining the checksum of
     1718 * packets captured on the same host that generated them!
     1719 *
     1720 * New in libtrace 3.0.17
     1721 */
     1722DLLEXPORT uint16_t *trace_checksum_transport(libtrace_packet_t *packet,
     1723                uint16_t *csum);
     1724
    16751725/** Gets a pointer to the transport layer header (if any)
    16761726 * @param packet   The libtrace packet to find the transport header for
  • lib/protocols_l3.c

    r5699389 rc909fad  
    3636#include "libtrace.h"
    3737#include "protocols.h"
     38#include "checksum.h"
    3839#include <assert.h>
    3940#include <stdlib.h>
     
    676677}
    677678
     679DLLEXPORT uint16_t *trace_checksum_layer3(libtrace_packet_t *packet,
     680                uint16_t *csum) {
     681
     682        void *l3;
     683        uint16_t ethertype;
     684        uint32_t remaining;
     685        uint16_t *csum_ptr;
     686
     687        uint8_t safety[65536];
     688
     689        if (csum == NULL)
     690                return NULL;
     691       
     692        l3 = trace_get_layer3(packet, &ethertype, &remaining);
     693               
     694        if (l3 == NULL)
     695                return NULL;
     696       
     697        if (ethertype == TRACE_ETHERTYPE_IP) {
     698                libtrace_ip_t *ip = (libtrace_ip_t *)l3;
     699                if (remaining < sizeof(libtrace_ip_t))
     700                        return NULL;
     701
     702                csum_ptr = &ip->ip_sum;
     703
     704                /* I hate memcpys, but this is the only truly safe way to
     705                 * do this without modifying the packet. I'm trying to be
     706                 * careful about not creating any more thread-safety issues
     707                 * than there already are :) */
     708                memcpy(safety, ip, ip->ip_hl * sizeof(uint32_t));
     709               
     710                /* Set the checksum to zero, so we can do the calculation */
     711                ip = (libtrace_ip_t *)safety;
     712                ip->ip_sum = 0;
     713
     714                *csum = checksum_buffer(safety, ip->ip_hl * sizeof(uint32_t));
     715               
     716                /* Remember to byteswap appropriately */
     717                *csum = ntohs(*csum);
     718               
     719                return csum_ptr;
     720        }
     721
     722        return NULL;
     723}
     724
  • lib/protocols_transport.c

    r05f2718 rc909fad  
    3535#include "libtrace.h"
    3636#include "protocols.h"
     37#include "checksum.h"
    3738#include <assert.h>
    3839#include <stdlib.h>
    3940#include <stdio.h> // fprintf
     41#include <string.h>
    4042
    4143/* This file contains all the protocol decoding functions for transport layer
     
    419421}
    420422
    421 
     423DLLEXPORT uint16_t *trace_checksum_transport(libtrace_packet_t *packet,
     424                uint16_t *csum) {
     425
     426        void *header = NULL;
     427        uint16_t ethertype;
     428        uint32_t remaining;
     429        uint32_t sum = 0;
     430        uint8_t proto = 0;
     431        uint16_t *csum_ptr = NULL;
     432        int plen = 0;
     433
     434        uint8_t safety[65536];
     435        uint8_t *ptr = safety;
     436
     437        header = trace_get_layer3(packet, &ethertype, &remaining);
     438
     439        if (header == NULL)
     440                return NULL;
     441       
     442        if (ethertype == TRACE_ETHERTYPE_IP) {
     443                libtrace_ip_t *ip = (libtrace_ip_t *)header;
     444
     445                if (remaining < sizeof(libtrace_ip_t))
     446                        return NULL;
     447
     448                sum = ipv4_pseudo_checksum(ip);
     449
     450        } else if (ethertype == TRACE_ETHERTYPE_IPV6) {
     451                libtrace_ip6_t *ip = (libtrace_ip6_t *)header;
     452               
     453                if (remaining < sizeof(libtrace_ip6_t))
     454                        return 0;
     455
     456                sum = ipv6_pseudo_checksum(ip);
     457       
     458        }
     459
     460        header = trace_get_transport(packet, &proto, &remaining);
     461
     462        if (proto == TRACE_IPPROTO_TCP) {
     463                libtrace_tcp_t *tcp = (libtrace_tcp_t *)header;
     464                header = trace_get_payload_from_tcp(tcp, &remaining);
     465               
     466                csum_ptr = &tcp->check;
     467
     468                memcpy(ptr, tcp, tcp->doff * 4);
     469
     470                tcp = (libtrace_tcp_t *)ptr;
     471                tcp->check = 0;
     472
     473                ptr += (tcp->doff * 4);
     474        }
     475       
     476        else if (proto == TRACE_IPPROTO_UDP) {
     477
     478                libtrace_udp_t *udp = (libtrace_udp_t *)header;
     479                header = trace_get_payload_from_udp(udp, &remaining);
     480               
     481                csum_ptr = &udp->check;
     482                memcpy(ptr, udp, sizeof(libtrace_udp_t));
     483
     484                udp = (libtrace_udp_t *)ptr;
     485                udp->check = 0;
     486
     487                ptr += sizeof(libtrace_udp_t);
     488        }
     489       
     490        else if (proto == TRACE_IPPROTO_ICMP) {
     491                /* ICMP doesn't use the pseudo header */
     492                sum = 0;
     493
     494                libtrace_icmp_t *icmp = (libtrace_icmp_t *)header;
     495                header = trace_get_payload_from_icmp(icmp, &remaining);
     496               
     497                csum_ptr = &icmp->checksum;
     498                memcpy(ptr, icmp, sizeof(libtrace_icmp_t));
     499
     500                icmp = (libtrace_icmp_t *)ptr;
     501                icmp->checksum = 0;
     502               
     503                ptr += sizeof(libtrace_icmp_t);
     504
     505        }
     506        else {
     507                return NULL;
     508        }
     509
     510        sum += add_checksum(safety, (uint16_t)(ptr - safety));
     511
     512        plen = trace_get_payload_length(packet);
     513        if (plen < 0)
     514                return NULL;
     515
     516        if (remaining < (uint32_t)plen)
     517                return NULL;
     518
     519        if (header == NULL)
     520                return NULL;
     521
     522        sum += add_checksum(header, (uint16_t)plen);
     523        *csum = ntohs(finish_checksum(sum));
     524        //assert(0);
     525       
     526        return csum_ptr;
     527}
Note: See TracChangeset for help on using the changeset viewer.