Changeset 1376

Show
Ignore:
Timestamp:
31/10/08 13:51:59 (2 months ago)
Author:
perry
Message:

If we have a complete header, but truncated at the end of it, we shouldn't return NULL from
get_payload_from_* functions so we can distinguish it from a truncated header.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/libtrace.h.in

    r1372 r1376  
    10771077 * wireless monitoring header. 
    10781078 * 
     1079 * If the header is truncated, this function will return NULL, and remaining will be 0. 
     1080 * If there are 0 bytes of payload, the function will return a pointer, and remaining will be 0. 
     1081 * 
    10791082 * @param[in] meta a pointer to a header 
    10801083 * @param[in,out] linktype the linktype of meta (updated to indicate the 
     
    10851088 * unchanged. 
    10861089 * 
    1087  * All parameters are mandatory. NULL will be returned if any are NULL. 
     1090 * All parameters are mandatory.  
    10881091 */ 
    10891092DLLEXPORT void *trace_get_payload_from_meta(const void *meta, 
     
    11161119 * @param l2                    The pointer to the current layer2 header 
    11171120 * @param linktype              The type of the layer2 header 
    1118  * @param[out] ethertype        An optional output variable of the ethernet type 
    1119  * @param[in,out] remaining     Optionally updated with the length remaining 
     1121 * @param[out] ethertype        An optional output variable of the ethernet type of the new header 
     1122 * @param[in,out] remaining     Updated with the length remaining 
    11201123 * 
    11211124 * @return a pointer to the transport layer header, or NULL if header isn't 
    11221125 * present. 
    11231126 * 
    1124  * type may be NULL if not needed. 
    1125  * 
    1126  * Remaining may be NULL.  If Remaining is not NULL it must point to the number 
    1127  * of bytes captured of the layer2 header and beyond.  It will be decremented 
    1128  * by the number of bytes skipped to find the payload. 
     1127 * Remaining must point to the number of bytes captured of the layer2 header 
     1128 * and beyond.  It will be decremented by the number of bytes skipped to find 
     1129 * the payload. 
     1130 * 
     1131 * If the layer2 header is complete but there are zero bytes of payload after the end of the header, 
     1132 * a pointer to where the payload would be is returned and remaining will be set to 0.  If the  
     1133 * layer2 header is incomplete (truncated), then NULL is returned and remaining will be set to 0. 
    11291134 * 
    11301135 */ 
     
    11691174 * present. 
    11701175 * 
    1171  * Remaining may be NULL.  If Remaining is not NULL it must point to the number 
    1172  * of bytes captured of the IP header and beyond.  It will be decremented by 
    1173  * the length of the IPv4 header (including any options). 
    1174  * 
    1175  * proto may be NULL if not needed. 
     1176 * Remaining will be decremented by the size of the IPv4 header (including any options).  
     1177 * If the IPv4 header is complete but there are zero bytes of payload after the IPv4 header, a pointer 
     1178 * to where the payload would be is returned and remaining will be set to 0.  If the IPv4 header is 
     1179 * incomplete, NULL will be returned and remaining will be set to 0. 
     1180 * 
     1181 * proto may be NULL, in which case it won't be updated 
    11761182 * 
    11771183 * @note This is similar to trace_get_transport_from_ip in libtrace2 
     
    11881194 * isn't complete. 
    11891195 * 
    1190  * Remaining may be NULL.  If Remaining is not NULL it must point to the number 
    1191  * of bytes captured of the IP header and beyond.  It will be decremented by 
    1192  * this function by the length of the IPV6 header. 
    1193  * 
    1194  * proto may be NULL if not needed. 
     1196 * Remaining will be decremented by the size of the IPv6 header (including any options).  
     1197 * If the IPv6 header is complete but there are zero bytes of payload after the IPv6 header, a pointer 
     1198 * to where the payload would be is returned and remaining will be set to 0.  If the IPv6 header is 
     1199 * incomplete, NULL will be returned and remaining will be set to 0. 
     1200 * 
     1201 * proto may be NULL, in which case it won't be updated. 
    11951202 * 
    11961203 */ 
     
    12181225                uint16_t *type, uint32_t *remaining); 
    12191226 
    1220 /** Skips over any 802.1q headers, if present. 
     1227/** Skips over any 802.1q header, if present. 
    12211228 * @param vlan      A pointer to the vlan header 
    1222  * @param[in,out] type  The ethernet type, replaced with the vlan ether type 
     1229 * @param[out] type  The ethernet type, replaced with the vlan ether type 
    12231230 * @param[in,out] remaining Updated with the number of bytes remaining 
    12241231 * 
    12251232 * @return a pointer to the header beyond the vlan header, if present. 
    1226  * Otherwise, returns NULL 
    1227  * 
    1228  * Remaining may be NULL. If Remaining is not NULL it must point to the number 
    1229  * of bytes captured past (but not including) the link layer. This function 
    1230  * will decrement it by the length of the 802.1q headers if present. 
    1231  * 
    1232  * Type must point to the value of the ethernet type. Libtrace will assert 
    1233  * fail if type is NULL. 
     1233 * Otherwise, returns NULL.   
     1234 * 
     1235 * Remaining will be decremented by the size of the vlan header.  If the vlan 
     1236 * header is complete but there are zero bytes of payload after the vlan 
     1237 * header, a pointer to where the payload would be is returned and remaining 
     1238 * will be set to 0.  If the vlan header is incomplete, NULL will be returned 
     1239 * and remaining will be set to 0. 
     1240 * 
     1241 * type will be set to the ethertype contained within the vlan payload. 
    12341242 * 
    12351243 */ 
     
    12371245                void *vlan, uint16_t *type, uint32_t *remaining); 
    12381246 
    1239 /** Skips over any MPLS headers, if present. 
     1247/** Skips over a MPLS header 
    12401248 * @param mpls      A pointer to the mpls header 
    1241  * @param[in,out] type  The ethernet type, replaced by the ether type of the 
     1249 * @param[out] type  The ethernet type, replaced by the ether type of the 
    12421250 * following header - 0x0000 if an Ethernet header is deemed to be next 
    12431251 * @param[in,out] remaining     Updated with the number of bytes remaining 
     
    12451253 * @return a pointer to the header beyond the MPLS label, if present. Will 
    12461254 * return NULL if there is not enough bytes remaining to skip past the MPLS 
    1247  * label or if the ether type is not MPLS. 
    1248  * 
    1249  * Remaining may be NULL. If remaining is not NULL it must point to the number 
    1250  * of bytes captured past (but not including) the link layer. This function 
    1251  * will decrement it by the length of the topmost MPLS label if present. 
     1255 * label. 
     1256 * 
     1257 * Remaining will be decremented by the size of the MPLS header.  If the MPLS 
     1258 * header is complete but there are zero bytes of payload after the MPLS 
     1259 * header, a pointer to where the payload would be is returned and remaining 
     1260 * will be set to 0.  If the MPLS header is incomplete, NULL will be returned 
     1261 * and remaining will be set to 0. 
    12521262 * 
    12531263 * Type must point to the value of the ethernet type. Libtrace will assert 
     
    12661276 * @param[in,out] remaining Updated with the number of bytes remaining 
    12671277 * 
    1268  * @return a pointer to the tcp payload, or NULL if the payload isn't present. 
    1269  * 
    1270  * Remaining may be NULL.  If remaining is not NULL it must point to the number 
    1271  * of bytes captured of the TCP header and beyond.  It will be decremented by 
    1272  * this function by the length of the TCP header (including any options). 
     1278 * @return a pointer to the tcp payload, or NULL if the tcp header is truncated. 
     1279 * 
     1280 * Remaining will be decremented by the size of the TCP header.  If the TCP 
     1281 * header is complete but there are zero bytes of payload after the TCP 
     1282 * header, a pointer to where the payload would be is returned and remaining 
     1283 * will be set to 0.  If the TCP header is incomplete, NULL will be returned 
     1284 * and remaining will be set to 0. 
    12731285 * 
    12741286 */ 
     
    12801292 * @param[in,out] remaining Updated with the number of bytes remaining 
    12811293 * 
    1282  * @return a pointer to the udp payload, or NULL if the payload isn't present. 
    1283  * 
    1284  * Remaining may be NULL.  If Remaining is not NULL it must point to the number 
    1285  * of bytes captured of the UDP header and beyond.  It will be decremented by 
    1286  * this function to the number of bytes remaining after the UDP header. 
     1294 * @return a pointer to the udp payload, or NULL if the udp header is truncated. 
     1295 * 
     1296 * Remaining will be decremented by the size of the TCP header.  If the TCP 
     1297 * header is complete but there are zero bytes of payload after the TCP 
     1298 * header, a pointer to where the payload would be is returned and remaining 
     1299 * will be set to 0.  If the TCP header is incomplete, NULL will be returned 
     1300 * and remaining will be set to 0. 
    12871301 * 
    12881302 */ 
     
    12931307 * @param[in,out] remaining Updated with the number of bytes remaining 
    12941308 * 
    1295  * @return a pointer to the icmp payload, or NULL if the payload isn't present. 
    1296  * 
    1297  * Remaining may be NULL.  If remaining is not NULL it must point to the number 
    1298  * of bytes captured of the ICMP header and beyond.  It will be decremented 
    1299  * by the number of bytes in the ICMP header. 
     1309 * @return a pointer to the icmp payload, or NULL if the icmp header is truncated. 
     1310 * 
     1311 * Remaining will be decremented by the size of the TCP header.  If the TCP 
     1312 * header is complete but there are zero bytes of payload after the TCP 
     1313 * header, a pointer to where the payload would be is returned and remaining 
     1314 * will be set to 0.  If the TCP header is incomplete, NULL will be returned 
     1315 * and remaining will be set to 0. 
    13001316 *  
    13011317 */ 
  • trunk/lib/protocols_l2.c

    r1372 r1376  
    1515 
    1616        if (remaining) { 
    17                 if (*remaining <= sizeof(*eth)) { 
     17                if (*remaining < sizeof(*eth)) { 
    1818                        *remaining = 0; 
    1919                        return NULL; 
     
    2929 
    3030/* skip any 802.1q headers if necessary  
    31  * type is input/output 
     31 * type is now output only (why check it if we don't need to?) 
    3232 */ 
    3333void *trace_get_payload_from_vlan(void *ethernet, uint16_t *type, 
    3434                uint32_t *remaining) 
    3535{ 
    36         assert(type != NULL); 
    37         if (*type == 0x8100) { 
    38                 libtrace_8021q_t *vlanhdr = (libtrace_8021q_t *)ethernet; 
    39  
    40                 if (remaining) { 
    41                         if (*remaining <= sizeof(libtrace_8021q_t)) { 
    42                                 *remaining = 0; 
    43                                 return NULL; 
    44                         } 
    45  
    46                         *remaining=*remaining-sizeof(libtrace_8021q_t); 
    47                 } 
    48  
     36        libtrace_8021q_t *vlanhdr = (libtrace_8021q_t *)ethernet; 
     37 
     38        if (remaining) { 
     39                if (*remaining < sizeof(libtrace_8021q_t)) { 
     40                        *remaining = 0; 
     41                        return NULL; 
     42                } 
     43 
     44                *remaining=*remaining-sizeof(libtrace_8021q_t); 
     45        } 
     46 
     47        if (type) 
    4948                *type = ntohs(vlanhdr->vlan_ether_type); 
    5049 
    51                 return (void*)((char *)ethernet + sizeof(*vlanhdr)); 
    52         } else 
    53                 return NULL; 
     50        return (void*)((char *)ethernet + sizeof(*vlanhdr)); 
    5451 
    5552} 
     
    6259                uint32_t *remaining) 
    6360{ 
    64         assert(type != NULL); 
    65          
    66         if (*type == 0x8847) { 
    67                 if ((((char*)ethernet)[2]&0x01)==0) { 
    68                         *type = 0x8847; 
    69                 } 
    70                 else { 
    71                         if (!remaining || *remaining>=5) { 
    72                                 switch (((char*)ethernet)[4]&0xF0) { 
    73                                         case 0x40: 
    74                                                 *type = 0x0800; 
    75                                                 break; 
    76                                         case 0x60: 
    77                                                 *type = 0x86DD; 
    78                                                 break; 
    79                                         default: 
    80                                                 /* Ethernet */ 
    81                                                 *type = 0; 
    82                                 } 
     61         
     62        assert(type); 
     63        if ((((char*)ethernet)[2]&0x01)==0) { 
     64                /* The MPLS Stack bit is set */ 
     65                *type = 0x8847; 
     66        } 
     67        else { 
     68                if (!remaining || *remaining>=5) { 
     69                        switch (((char*)ethernet)[4]&0xF0) { 
     70                                case 0x40:      /* IPv4 */ 
     71                                        *type = 0x0800; 
     72                                        break; 
     73                                case 0x60:      /* IPv6 */ 
     74                                        *type = 0x86DD; 
     75                                        break; 
     76                                default:        /* VPLS */ 
     77                                        /* Ethernet */ 
     78                                        *type = 0; 
    8379                        } 
    8480                } 
    85                 ethernet=(char*)ethernet+4; 
    86                 if (remaining) { 
    87                         if (*remaining<=4) 
    88                                 return NULL; 
    89                         else 
    90                                 *remaining-=4; 
    91                 } 
    92  
    93  
    94                 return ethernet; 
    95         } 
    96         else 
    97                 return NULL; 
     81        } 
     82        ethernet=(char*)ethernet+4; 
     83        if (remaining) { 
     84                if (*remaining<4) 
     85                        return NULL; 
     86                else 
     87                        *remaining-=4; 
     88        } 
     89 
     90 
     91        return ethernet; 
    9892} 
    9993 
     
    10599 
    106100        if (remaining) { 
    107                 if (*remaining <= sizeof(libtrace_llcsnap_t)) { 
     101                if (*remaining < sizeof(libtrace_llcsnap_t)) { 
    108102                        *remaining = 0; 
    109103                        return NULL; 
     
    126120        int8_t extra = 0; /* how many QoS bytes to skip */ 
    127121         
    128         if (remaining && *remaining <= sizeof(libtrace_80211_t)) { 
     122        if (remaining && *remaining < sizeof(libtrace_80211_t)) { 
    129123                *remaining = 0; 
    130124                return NULL; 
     
    172166 
    173167        if (remaining) { 
    174                 if (*remaining <= sizeof(libtrace_ppp_t)) { 
     168                if (*remaining < sizeof(libtrace_ppp_t)) { 
    175169                        *remaining = 0; 
    176170                        return NULL; 
     
    196190                uint32_t *remaining) { 
    197191        if (remaining) { 
    198                 if (*remaining <= sizeof(libtrace_pppoe_t)) { 
     192                if (*remaining < sizeof(libtrace_pppoe_t)) { 
    199193                        *remaining = 0; 
    200194                        return NULL; 
     
    221215 
    222216        if (remaining) { 
    223                 if (*remaining <= sizeof(libtrace_chdlc_t)) { 
     217                if (*remaining < sizeof(libtrace_chdlc_t)) { 
    224218                        *remaining = 0; 
    225219                        return NULL; 
     
    245239                if (l != NULL) { 
    246240                        link=l; 
    247                         continue; 
    248                 } 
    249         } while (0); 
     241                } 
     242        } while (l != NULL); 
    250243 
    251244        return trace_get_payload_from_layer2(link,linktype,ethertype,remaining); 
     
    291284{ 
    292285        libtrace_atm_capture_cell_t *cell; 
    293         if (remaining && *remaining<=sizeof(libtrace_atm_capture_cell_t)) { 
     286        if (remaining && *remaining<sizeof(libtrace_atm_capture_cell_t)) { 
    294287                *remaining = 0; 
    295288                return NULL; 
     
    338331                case TRACE_TYPE_NONE: 
    339332                        if ((*(char*)link&0xF0) == 0x40) 
    340                                 *ethertype=0x0800; 
     333                                *ethertype=0x0800;     /* IPv4 */ 
    341334                        else if ((*(char*)link&0xF0) == 0x60) 
    342                                 *ethertype=0x86DD; 
     335                                *ethertype=0x86DD;     /* IPv6 */ 
    343336                        return link; /* I love the simplicity */ 
    344337                case TRACE_TYPE_PPP: 
  • trunk/lib/protocols_l3.c

    r1372 r1376  
    4949        void *trans_ptr = 0; 
    5050 
    51         if (ipptr == NULL) 
    52                return NULL
     51        assert(ipptr != NULL); 
     52        assert(ipptr->ip_v == 4)
    5353 
    5454        if ((ntohs(ipptr->ip_off) & SW_IP_OFFMASK) != 0) { 
     
    5959 
    6060        if (remaining) { 
    61                 if (*remaining<=(ipptr->ip_hl*4U)) { 
     61                if (*remaining<(ipptr->ip_hl*4U)) { 
    6262                        *remaining = 0; 
    6363                        return NULL; 
     
    7777{ 
    7878        void *payload = (char*)ipptr+sizeof(libtrace_ip6_t); 
    79         uint8_t nxt = ipptr->nxt; 
    80  
    81         if (ipptr == NULL) 
    82                 return NULL; 
    83          
     79        uint8_t nxt; 
     80 
     81        assert (ipptr != NULL); 
     82        nxt = ipptr->nxt;        
    8483        if (remaining) { 
    8584                if (*remaining<sizeof(libtrace_ip6_t)) { 
  • trunk/lib/protocols_pktmeta.c

    r1362 r1376  
    2626 
    2727        if (remaining) { 
    28                 if (*remaining <= sizeof(*sll)) { 
     28                if (*remaining < sizeof(*sll)) { 
    2929                        *remaining = 0; 
    3030                        return NULL; 
     
    4545        libtrace_pflog_header_t *pflog = (libtrace_pflog_header_t*)link; 
    4646        if (remaining) { 
    47                 if (*remaining<=sizeof(*pflog)) { 
     47                if (*remaining<sizeof(*pflog)) { 
    4848                        *remaining = 0; 
    4949                        return NULL; 
     
    6262{ 
    6363        if (remaining) { 
    64                 if (*remaining<=144) { 
     64                /* Prism header is 144 bytes long */ 
     65                if (*remaining<144) { 
    6566                        *remaining = 0; 
    6667                        return NULL; 
     
    8182        uint16_t rtaplen = bswap_le_to_host16(rtap->it_len); 
    8283        if (remaining) { 
    83                 if (*remaining <= rtaplen) { 
     84                if (*remaining < rtaplen) { 
    8485                        *remaining = 0; 
    8586                        return NULL; 
  • trunk/lib/protocols_transport.c

    r1354 r1376  
    3737                         
    3838                default: 
     39                        printf("unknown ethertype=%04x\n",ethertype); 
    3940                        *proto=0; 
    4041                        return NULL; 
     
    118119{ 
    119120        if (remaining) { 
    120                 if (*remaining <= sizeof(libtrace_udp_t)) { 
     121                if (*remaining < sizeof(libtrace_udp_t)) { 
    121122                        *remaining = 0; 
    122123                        return NULL; 
     
    131132        unsigned int dlen = tcp->doff*4; 
    132133        if (remaining) { 
    133                 if (*remaining <= dlen) { 
     134                if (*remaining < dlen) { 
    134135                        *remaining = 0; 
    135136                        return NULL; 
     
    143144{ 
    144145        if (remaining) { 
    145                 if (*remaining <= sizeof(libtrace_icmp_t)) { 
     146                if (*remaining < sizeof(libtrace_icmp_t)) { 
    146147                        *remaining = 0; 
    147148                        return NULL;