Changeset 387d299


Ignore:
Timestamp:
07/04/14 11:37:25 (7 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, 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:
d14e23d
Parents:
9ad7a35
Message:

Remove duplicate macros in libpacketdump

Ensure OSPF libpacketdump module won't segfault or print garbage
if a truncated OSPF header is passed into it.

Renamed 'len' field in OSPF header to 'ospf_len' to avoid
potential conflict with variables named len in libpacketdump
code.

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • lib/libtrace.h.in

    r5a15b2f r387d299  
    867867        uint8_t ospf_v;         /**< OSPF Version, should be 2 */
    868868        uint8_t type;           /**< OSPF Packet Type */
    869         uint16_t len;           /**< Packet length, including OSPF header */
     869        uint16_t ospf_len;      /**< Packet length, including OSPF header */
    870870        struct in_addr router;  /**< Router ID of the packet source */
    871871        struct in_addr area;    /**< Area the packet belongs to */
  • libpacketdump/eth_2048.c

    r66ad025 r387d299  
    1010#include <netdb.h>
    1111
    12 #define DISPLAY_EXP(x,fmt,exp) \
    13         if ((unsigned int)len>=((char*)&ip->x-(char*)ip+sizeof(ip->x))) \
    14                 printf(fmt,exp); \
    15         else \
    16                 return;
    17 
    18 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,ip->x)
    19 
    20 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(ip->x))
    21 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&ip->x))
    22 
    2312DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
    2413{
     
    2918        }
    3019        //DISPLAY(ip_tos," TOS %02x")
    31         DISPLAY_EXP(ip_tos," DSCP %02x",ip->ip_tos >> 2)
    32         DISPLAY_EXP(ip_tos," ECN %x",ip->ip_tos & 0x2)
    33         DISPLAYS(ip_len," Total Length %i")
     20        DISPLAY_EXP(ip, ip_tos," DSCP %02x",ip->ip_tos >> 2);
     21        DISPLAY_EXP(ip, ip_tos," ECN %x",ip->ip_tos & 0x2);
     22        DISPLAYS(ip, ip_len," Total Length %i");
    3423        printf("\n IP:");
    35         DISPLAYS(ip_id," Id %u");
     24        DISPLAYS(ip, ip_id," Id %u");
    3625       
    3726        if ((unsigned int)len >= ((char *)&ip->ip_ttl - (char *)ip - 2)) {
     
    4231        }
    4332        //printf("\n IP:");
    44         DISPLAY(ip_ttl,"\n IP: TTL %i");
     33        DISPLAY(ip, ip_ttl,"\n IP: TTL %i");
    4534        if ((unsigned int)len>=((char*)&ip->ip_p-(char*)ip+sizeof(ip->ip_p))) {
    4635                struct protoent *ent=getprotobynumber(ip->ip_p);
     
    5544                return;
    5645        }
    57         DISPLAYS(ip_sum," Checksum %i\n");
    58         DISPLAYIP(ip_src," IP: Source %s ");
    59         DISPLAYIP(ip_dst,"Destination %s\n");
     46        DISPLAYS(ip, ip_sum," Checksum %i\n");
     47        DISPLAYIP(ip, ip_src," IP: Source %s ");
     48        DISPLAYIP(ip, ip_dst,"Destination %s\n");
    6049        decode_next(packet+ip->ip_hl*4,len-ip->ip_hl*4,"ip",ip->ip_p);
    6150        return;
  • libpacketdump/ip_1.c

    rec0b927 r387d299  
    33#include <dlfcn.h>
    44#include "libpacketdump.h"
    5 
    6 #define STRUCT icmp
    7 
    8 #define SAFE(x) \
    9         ((unsigned int)len>=((char*)&STRUCT->x-(char*)STRUCT+sizeof(STRUCT->x)))
    10 #define DISPLAY_EXP(x,fmt,exp) \
    11         if (SAFE(x)) \
    12                 printf(fmt,exp); \
    13         else \
    14                 return;
    15 
    16 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,STRUCT->x)
    17 
    18 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(STRUCT->x))
    19 #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(STRUCT->x))
    20 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&STRUCT->x))
    215
    226static char *unreach_types[]={
  • libpacketdump/ip_17.c

    rc7062df r387d299  
    88#include <netdb.h>
    99
    10 #define STRUCT udp
    11 
    12 #define SAFE(x) \
    13         ((unsigned int)len>=((char*)&STRUCT->x-(char*)STRUCT+sizeof(STRUCT->x)))
    14 #define DISPLAY_EXP(x,fmt,exp) \
    15         if (SAFE(x)) \
    16                 printf(fmt,exp); \
    17         else \
    18                 return;
    19 
    20 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,STRUCT->x)
    21 
    22 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(STRUCT->x))
    23 #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(STRUCT->x))
    24 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&STRUCT->x))
    25 
    2610
    2711DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
     
    2913        struct libtrace_udp *udp = (struct libtrace_udp*)packet;
    3014        printf(" UDP:");
    31         if (SAFE(source)) {
     15        if (SAFE(udp, source)) {
    3216                struct servent *ent=getservbyport(udp->source,"udp");
    3317                if(ent) {
     
    4125                return;
    4226        }
    43         if (SAFE(dest)) {
     27        if (SAFE(udp, dest)) {
    4428                struct servent *ent=getservbyport(udp->dest,"udp");
    4529                if(ent) {
     
    5438        }
    5539        printf("\n UDP:");
    56         DISPLAYS(len," Len %u");
    57         DISPLAYS(check," Checksum %u");
     40        DISPLAYS(udp, len," Len %u");
     41        DISPLAYS(udp, check," Checksum %u");
    5842        printf("\n");
    5943        if (htons(udp->source) < htons(udp->dest))
  • libpacketdump/ip_33.c

    rc7062df r387d299  
    66#include <netinet/tcp.h>
    77#include <netinet/in.h>
    8 
    9 #define STRUCT dccp
    10 
    11 #define SAFE(x) \
    12         ((unsigned int)len>=((char*)&STRUCT->x-(char*)STRUCT+sizeof(STRUCT->x)))
    13 #define DISPLAY_EXP(x,fmt,exp) \
    14         if (SAFE(x)) \
    15                 printf(fmt,exp); \
    16         else \
    17                 return;
    18 
    19 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,STRUCT->x)
    20 
    21 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(STRUCT->x))
    22 #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(STRUCT->x))
    23 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&STRUCT->x))
    248
    259struct dccphdr {
     
    5034{
    5135        struct dccphdr *dccp = (struct dccphdr*)packet;
    52         DISPLAYS(source," DCCP: Source %i");
    53         DISPLAYS(dest," Dest %i");
     36        DISPLAYS(dccp, source," DCCP: Source %i");
     37        DISPLAYS(dccp, dest," Dest %i");
    5438        if (len>4) {
    5539                printf("\n DCCP: Type %i",dccp->type);
     
    6953        else
    7054                return;
    71         DISPLAY(doff," DCCP: Dataoff: %i\n");
     55        DISPLAY(dccp, doff," DCCP: Dataoff: %i\n");
    7256        if (len>9)
    7357                printf(" DCCP: NDP %i CsLen: %i\n",dccp->ndp,dccp->cslen);
     
    7559                return;
    7660        }
    77         DISPLAY(check," DCCP: Checksum: %i\n");
     61        /* Should this be byteswapped??? */
     62        DISPLAY(dccp, check," DCCP: Checksum: %i\n");
    7863        if (htons(dccp->source) < htons(dccp->dest))
    7964                decode_next(packet+dccp->doff*4,len-dccp->doff*4,"dccp",htons(dccp->source));
  • libpacketdump/ip_6.c

    r9ca0b29 r387d299  
    55#include <assert.h>
    66#include <netdb.h>
    7 
    8 #define SAFE(x) \
    9         ((unsigned int)len>=((char*)&tcp->x-(char*)tcp+sizeof(tcp->x)))
    10 #define DISPLAY_EXP(x,fmt,exp) \
    11         if (SAFE(x)) \
    12                 printf(fmt,exp); \
    13         else \
    14                 return;
    15 
    16 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,tcp->x)
    17 
    18 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(tcp->x))
    19 #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(tcp->x))
    20 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&tcp->x))
    217
    228DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
     
    2713        libtrace_tcp_t *tcp = (libtrace_tcp_t *)packet;
    2814        printf(" TCP:");
    29         if (SAFE(source)) {
     15        if (SAFE(tcp, source)) {
    3016                struct servent *ent=getservbyport(tcp->source,"tcp");
    3117                if(ent) {
     
    3925                return;
    4026        }
    41         if (SAFE(dest)) {
     27        if (SAFE(tcp, dest)) {
    4228                struct servent *ent=getservbyport(tcp->dest,"tcp");
    4329                if(ent) {
     
    5238        }
    5339        printf("\n TCP:");
    54         DISPLAYL(seq," Seq %u");
     40        DISPLAYL(tcp, seq," Seq %u");
    5541        printf("\n TCP:");
    56         DISPLAYL(ack_seq," Ack %u");
     42        DISPLAYL(tcp, ack_seq," Ack %u");
    5743        if ((char*)&tcp->window-(char *)tcp>len) {
    5844                printf("\n");
     
    7157        if (tcp->ack) printf(" ACK");
    7258        if (tcp->urg) printf(" URG");
    73         DISPLAYS(window," Window %i");
     59        DISPLAYS(tcp, window," Window %i");
    7460        printf("\n TCP:");
    75         DISPLAYS(check," Checksum %i");
    76         DISPLAYS(urg_ptr," Urgent %i");
     61        DISPLAYS(tcp, check," Checksum %i");
     62        DISPLAYS(tcp, urg_ptr," Urgent %i");
    7763        pkt = (unsigned char*)packet+sizeof(*tcp);
    7864        plen = (len-sizeof *tcp) < (tcp->doff*4-sizeof(*tcp))?(len-sizeof(*tcp)):(tcp->doff*4-sizeof *tcp);
  • libpacketdump/ip_89.c

    re224862 r387d299  
    1010static void dump_ospf_v2_header(libtrace_ospf_v2_t *hdr, unsigned len) {
    1111
    12         printf(" OSPF Header: Version %u Type %u ",
    13                         hdr->ospf_v, hdr->type);
     12        DISPLAY(hdr, ospf_v, " OSPF Header: Version %u");
     13        DISPLAY(hdr, type, " Type %u ");
    1414        switch(hdr->type) {
    1515                case TRACE_OSPF_HELLO:
     
    2929                        break;
    3030        }
     31        printf("\n");
    3132
    32         printf("\n OSPF Header: Length %u \n", ntohs(hdr->len));
    33         printf(" OSPF Header: Router Id %s ", inet_ntoa(hdr->router));
    34         printf("Area Id %s\n", inet_ntoa(hdr->area));
    35         printf(" OSPF Header: Checksum %u Auth Type %u\n", ntohs(hdr->sum),
    36                         ntohs(hdr->au_type));
    37 
    38         printf(" OSPF Header: Auth Key ID %u Auth Data Len %u\n",
    39                         hdr->au_key_id, hdr->au_data_len);
    40         printf(" OSPF Header: Auth Crypto Seq %u\n", ntohl(hdr->au_seq_num));
    41 
     33        DISPLAYS(hdr, ospf_len, "OSPF Header: Length %u \n");
     34        DISPLAYIP(hdr, router, " OSPF Header: Router Id %s ");
     35        DISPLAYIP(hdr, area, "Area Id %s\n");
     36        DISPLAYS(hdr, sum, " OSPF Header: Checksum %u ");
     37        DISPLAYS(hdr, au_type, "Auth Type %u\n");
     38        DISPLAY(hdr, au_key_id, " OSPF Header: Auth Key ID %u ");
     39        DISPLAY(hdr, au_data_len, "Auth Data Len %u\n");
     40        DISPLAYL(hdr, au_seq_num, " OSPF Header: Auth Crypto Seq %u\n");
    4241
    4342}
  • libpacketdump/libpacketdump.h

    r66ad025 r387d299  
    66extern "C" {
    77#endif
     8
     9#define SAFE(hdr,x) \
     10        ((unsigned int)len>=((char*)&hdr->x-(char*)hdr+sizeof(hdr->x)))
     11
     12#define DISPLAY_EXP(hdr,x,fmt,exp) \
     13        if (SAFE(hdr, x)) \
     14                printf(fmt,exp); \
     15        else {\
     16                printf("(Truncated)\n"); \
     17                return; \
     18        }
     19
     20#define DISPLAY(hdr,x,fmt) DISPLAY_EXP(hdr,x,fmt,hdr->x)
     21
     22#define DISPLAYS(hdr,x,fmt) DISPLAY_EXP(hdr,x,fmt,htons(hdr->x))
     23#define DISPLAYL(hdr,x,fmt) DISPLAY_EXP(hdr,x,fmt,htonl(hdr->x))
     24#define DISPLAYIP(hdr,x,fmt) DISPLAY_EXP(hdr,x,fmt,inet_ntoa(*(struct in_addr*)&hdr->x))
     25
    826
    927void trace_hexdump_packet(libtrace_packet_t *packet);
Note: See TracChangeset for help on using the changeset viewer.