]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-l2tp.c
When checking for pcap_if_t, add $V_INCLS to CFLAGS, so we look at the
[tcpdump] / print-l2tp.c
index 9be877c57d3d2e03f17476978be56fc8fa793bfc..7546a860621f00552419544bc3617085cff1b64e 100644 (file)
  */
 
 #ifndef lint
-static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-l2tp.c,v 1.9 2001-11-05 10:03:27 guy Exp $";
+static const char rcsid[] _U_ =
+    "@(#) $Header: /tcpdump/master/tcpdump/print-l2tp.c,v 1.17 2003-12-26 23:20:58 guy Exp $";
 #endif
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#include <tcpdump-stdinc.h>
+
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/param.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 
 #include "l2tp.h"
 #include "interface.h"
+#include "extract.h"
 
 static char tstr[] = " [|l2tp]";
 
@@ -145,27 +144,27 @@ static struct tok l2tp_avp2str[] = {
        { L2TP_AVP_MAXIMUM_BPS,         "MAXIMUM_BPS" },
        { L2TP_AVP_BEARER_TYPE,         "BEARER_TYPE" },
        { L2TP_AVP_FRAMING_TYPE,        "FRAMING_TYPE" },
-       { L2TP_AVP_PACKET_PROC_DELAY,   "PACKET_PROC_DELAY" },  
+       { L2TP_AVP_PACKET_PROC_DELAY,   "PACKET_PROC_DELAY" },
        { L2TP_AVP_CALLED_NUMBER,       "CALLED_NUMBER" },
        { L2TP_AVP_CALLING_NUMBER,      "CALLING_NUMBER" },
        { L2TP_AVP_SUB_ADDRESS,         "SUB_ADDRESS" },
        { L2TP_AVP_TX_CONN_SPEED,       "TX_CONN_SPEED" },
        { L2TP_AVP_PHY_CHANNEL_ID,      "PHY_CHANNEL_ID" },
        { L2TP_AVP_INI_RECV_LCP,        "INI_RECV_LCP" },
-       { L2TP_AVP_LAST_SENT_LCP,       "LAST_SENT_LCP" },      
-       { L2TP_AVP_LAST_RECV_LCP,       "LAST_RECV_LCP" },      
-       { L2TP_AVP_PROXY_AUTH_TYPE,     "PROXY_AUTH_TYPE" },    
+       { L2TP_AVP_LAST_SENT_LCP,       "LAST_SENT_LCP" },
+       { L2TP_AVP_LAST_RECV_LCP,       "LAST_RECV_LCP" },
+       { L2TP_AVP_PROXY_AUTH_TYPE,     "PROXY_AUTH_TYPE" },
        { L2TP_AVP_PROXY_AUTH_NAME,     "PROXY_AUTH_NAME" },
-       { L2TP_AVP_PROXY_AUTH_CHAL,     "PROXY_AUTH_CHAL" },    
+       { L2TP_AVP_PROXY_AUTH_CHAL,     "PROXY_AUTH_CHAL" },
        { L2TP_AVP_PROXY_AUTH_ID,       "PROXY_AUTH_ID" },
        { L2TP_AVP_PROXY_AUTH_RESP,     "PROXY_AUTH_RESP" },
        { L2TP_AVP_CALL_ERRORS,         "CALL_ERRORS" },
-       { L2TP_AVP_ACCM,                "ACCM" },               
-       { L2TP_AVP_RANDOM_VECTOR,       "RANDOM_VECTOR" },      
+       { L2TP_AVP_ACCM,                "ACCM" },
+       { L2TP_AVP_RANDOM_VECTOR,       "RANDOM_VECTOR" },
        { L2TP_AVP_PRIVATE_GRP_ID,      "PRIVATE_GRP_ID" },
-       { L2TP_AVP_RX_CONN_SPEED,       "RX_CONN_SPEED" },      
-       { L2TP_AVP_SEQ_REQUIRED,        "SEQ_REQUIRED" },       
-       { L2TP_AVP_PPP_DISCON_CC,       "PPP_DISCON_CC" },      
+       { L2TP_AVP_RX_CONN_SPEED,       "RX_CONN_SPEED" },
+       { L2TP_AVP_SEQ_REQUIRED,        "SEQ_REQUIRED" },
+       { L2TP_AVP_PPP_DISCON_CC,       "PPP_DISCON_CC" },
        { 0,                            NULL }
 };
 
@@ -242,19 +241,19 @@ static char *l2tp_error_code_general[] = {
 /******************************/
 /* generic print out routines */
 /******************************/
-static void 
+static void
 print_string(const u_char *dat, u_int length)
 {
-       int i;
+       u_int i;
        for (i=0; i<length; i++) {
                printf("%c", *dat++);
        }
 }
 
-static void 
+static void
 print_octets(const u_char *dat, u_int length)
 {
-       int i;
+       u_int i;
        for (i=0; i<length; i++) {
                printf("%02x", *dat++);
        }
@@ -263,13 +262,13 @@ print_octets(const u_char *dat, u_int length)
 static void
 print_16bits_val(const u_int16_t *dat)
 {
-       printf("%u", ntohs(*dat));
+       printf("%u", EXTRACT_16BITS(dat));
 }
 
 static void
 print_32bits_val(const u_int32_t *dat)
 {
-       printf("%lu", (u_long)ntohl(*dat));
+       printf("%lu", (u_long)EXTRACT_32BITS(dat));
 }
 
 /***********************************/
@@ -280,19 +279,20 @@ l2tp_msgtype_print(const u_char *dat)
 {
        u_int16_t *ptr = (u_int16_t*)dat;
 
-       printf("%s", tok2str(l2tp_msgtype2str, "MSGTYPE-#%u", ntohs(*ptr)));
+       printf("%s", tok2str(l2tp_msgtype2str, "MSGTYPE-#%u",
+           EXTRACT_16BITS(ptr)));
 }
 
 static void
 l2tp_result_code_print(const u_char *dat, u_int length)
 {
        u_int16_t *ptr = (u_int16_t *)dat;
-       
-       printf("%u", ntohs(*ptr++));            /* Result Code */
-       if (length > 2) {                       /* Error Code (opt) */
-               printf("/%u", ntohs(*ptr++));   
+
+       printf("%u", EXTRACT_16BITS(ptr)); ptr++;       /* Result Code */
+       if (length > 2) {                               /* Error Code (opt) */
+               printf("/%u", EXTRACT_16BITS(ptr)); ptr++;
        }
-       if (length > 4) {                       /* Error Message (opt) */
+       if (length > 4) {                               /* Error Message (opt) */
                printf(" ");
                print_string((u_char *)ptr, length - 4);
        }
@@ -301,7 +301,8 @@ l2tp_result_code_print(const u_char *dat, u_int length)
 static void
 l2tp_proto_ver_print(const u_int16_t *dat)
 {
-       printf("%u.%u", (ntohs(*dat) >> 8), (ntohs(*dat) & 0xff));
+       printf("%u.%u", (EXTRACT_16BITS(dat) >> 8),
+           (EXTRACT_16BITS(dat) & 0xff));
 }
 
 static void
@@ -309,10 +310,10 @@ l2tp_framing_cap_print(const u_char *dat)
 {
        u_int32_t *ptr = (u_int32_t *)dat;
 
-       if (ntohl(*ptr) &  L2TP_FRAMING_CAP_ASYNC_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_CAP_ASYNC_MASK) {
                printf("A");
        }
-       if (ntohl(*ptr) &  L2TP_FRAMING_CAP_SYNC_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_CAP_SYNC_MASK) {
                printf("S");
        }
 }
@@ -322,10 +323,10 @@ l2tp_bearer_cap_print(const u_char *dat)
 {
        u_int32_t *ptr = (u_int32_t *)dat;
 
-       if (ntohl(*ptr) &  L2TP_BEARER_CAP_ANALOG_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_CAP_ANALOG_MASK) {
                printf("A");
        }
-       if (ntohl(*ptr) &  L2TP_BEARER_CAP_DIGITAL_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_CAP_DIGITAL_MASK) {
                printf("D");
        }
 }
@@ -338,7 +339,7 @@ l2tp_q931_cc_print(const u_char *dat, u_int length)
        if (length > 3) {
                printf(" ");
                print_string(dat+3, length-3);
-       } 
+       }
 }
 
 static void
@@ -346,10 +347,10 @@ l2tp_bearer_type_print(const u_char *dat)
 {
        u_int32_t *ptr = (u_int32_t *)dat;
 
-       if (ntohl(*ptr) &  L2TP_BEARER_TYPE_ANALOG_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_TYPE_ANALOG_MASK) {
                printf("A");
        }
-       if (ntohl(*ptr) &  L2TP_BEARER_TYPE_DIGITAL_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_BEARER_TYPE_DIGITAL_MASK) {
                printf("D");
        }
 }
@@ -359,16 +360,16 @@ l2tp_framing_type_print(const u_char *dat)
 {
        u_int32_t *ptr = (u_int32_t *)dat;
 
-       if (ntohl(*ptr) &  L2TP_FRAMING_TYPE_ASYNC_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_TYPE_ASYNC_MASK) {
                printf("A");
        }
-       if (ntohl(*ptr) &  L2TP_FRAMING_TYPE_SYNC_MASK) {
+       if (EXTRACT_32BITS(ptr) &  L2TP_FRAMING_TYPE_SYNC_MASK) {
                printf("S");
        }
 }
 
 static void
-l2tp_packet_proc_delay_print()
+l2tp_packet_proc_delay_print(void)
 {
        printf("obsolete");
 }
@@ -378,8 +379,8 @@ l2tp_proxy_auth_type_print(const u_char *dat)
 {
        u_int16_t *ptr = (u_int16_t *)dat;
 
-       printf("%s", tok2str(l2tp_authentype2str, 
-                            "AuthType-#%u", ntohs(*ptr)));
+       printf("%s", tok2str(l2tp_authentype2str,
+                            "AuthType-#%u", EXTRACT_16BITS(ptr)));
 }
 
 static void
@@ -387,7 +388,7 @@ l2tp_proxy_auth_id_print(const u_char *dat)
 {
        u_int16_t *ptr = (u_int16_t *)dat;
 
-       printf("%u", ntohs(*ptr) & L2TP_PROXY_AUTH_ID_MASK);
+       printf("%u", EXTRACT_16BITS(ptr) & L2TP_PROXY_AUTH_ID_MASK);
 }
 
 static void
@@ -395,31 +396,31 @@ l2tp_call_errors_print(const u_char *dat)
 {
        u_int16_t *ptr = (u_int16_t *)dat;
        u_int16_t val_h, val_l;
-       
+
        ptr++;          /* skip "Reserved" */
 
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("CRCErr=%u ", (val_h<<16) + val_l);
 
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("FrameErr=%u ", (val_h<<16) + val_l);
 
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("HardOver=%u ", (val_h<<16) + val_l);
 
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("BufOver=%u ", (val_h<<16) + val_l);
 
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("Timeout=%u ", (val_h<<16) + val_l);
 
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("AlignErr=%u ", (val_h<<16) + val_l);
 }
 
@@ -431,12 +432,12 @@ l2tp_accm_print(const u_char *dat)
 
        ptr++;          /* skip "Reserved" */
 
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("send=%08x ", (val_h<<16) + val_l);
-       
-       val_h = ntohs(*ptr++);
-       val_l = ntohs(*ptr++);
+
+       val_h = EXTRACT_16BITS(ptr); ptr++;
+       val_l = EXTRACT_16BITS(ptr); ptr++;
        printf("recv=%08x ", (val_h<<16) + val_l);
 }
 
@@ -444,10 +445,10 @@ static void
 l2tp_ppp_discon_cc_print(const u_char *dat, u_int length)
 {
        u_int16_t *ptr = (u_int16_t *)dat;
-       
-       printf("%04x, ", ntohs(*ptr++));        /* Disconnect Code */
-       printf("%04x ",  ntohs(*ptr++));        /* Control Protocol Number */
-       printf("%s", tok2str(l2tp_cc_direction2str, 
+
+       printf("%04x, ", EXTRACT_16BITS(ptr)); ptr++;   /* Disconnect Code */
+       printf("%04x ",  EXTRACT_16BITS(ptr)); ptr++;   /* Control Protocol Number */
+       printf("%s", tok2str(l2tp_cc_direction2str,
                             "Direction-#%u", *((u_char *)ptr++)));
 
        if (length > 5) {
@@ -471,33 +472,42 @@ l2tp_avp_print(const u_char *dat, int length)
        printf(" ");
 
        TCHECK(*ptr);   /* Flags & Length */
-       len = ntohs(*ptr) & L2TP_AVP_HDR_LEN_MASK;
+       len = EXTRACT_16BITS(ptr) & L2TP_AVP_HDR_LEN_MASK;
+
+       /* If it is not long enough to contain the header, we'll give up. */
+       if (len < 6)
+               goto trunc;
 
-       /* If it is not long enough to decode the entire AVP, we'll 
-          abandon. */
+       /* If it goes past the end of the remaining length of the packet,
+          we'll give up. */
+       if (len > (u_int)length)
+               goto trunc;
+
+       /* If it goes past the end of the remaining length of the captured
+          data, we'll give up. */
        TCHECK2(*ptr, len);
        /* After this point, no need to worry about truncation */
 
-       if (ntohs(*ptr) & L2TP_AVP_HDR_FLAG_MANDATORY) {
+       if (EXTRACT_16BITS(ptr) & L2TP_AVP_HDR_FLAG_MANDATORY) {
                printf("*");
        }
-       if (ntohs(*ptr) & L2TP_AVP_HDR_FLAG_HIDDEN) {
+       if (EXTRACT_16BITS(ptr) & L2TP_AVP_HDR_FLAG_HIDDEN) {
                hidden = TRUE;
                printf("?");
        }
        ptr++;
 
-       if (ntohs(*ptr)) {
+       if (EXTRACT_16BITS(ptr)) {
                /* Vendor Specific Attribute */
-               printf("VENDOR%04x:", ntohs(*ptr++));
-               printf("ATTR%04x", ntohs(*ptr++));
+               printf("VENDOR%04x:", EXTRACT_16BITS(ptr)); ptr++;
+               printf("ATTR%04x", EXTRACT_16BITS(ptr)); ptr++;
                printf("(");
                print_octets((u_char *)ptr, len-6);
                printf(")");
        } else {
-               /* IETF-defined Attributes */ 
+               /* IETF-defined Attributes */
                ptr++;
-               attr_type = ntohs(*ptr++);
+               attr_type = EXTRACT_16BITS(ptr); ptr++;
                printf("%s", tok2str(l2tp_avp2str, "AVP-#%u", attr_type));
                printf("(");
                if (hidden) {
@@ -534,7 +544,7 @@ l2tp_avp_print(const u_char *dat, int length)
                        case L2TP_AVP_CALLED_NUMBER:
                        case L2TP_AVP_SUB_ADDRESS:
                        case L2TP_AVP_PROXY_AUTH_NAME:
-                       case L2TP_AVP_PRIVATE_GRP_ID:   
+                       case L2TP_AVP_PRIVATE_GRP_ID:
                                print_string((u_char *)ptr, len-6);
                                break;
                        case L2TP_AVP_CHALLENGE:
@@ -613,9 +623,9 @@ l2tp_print(const u_char *dat, u_int length)
        flag_t = flag_l = flag_s = flag_o = flag_p = FALSE;
 
        TCHECK(*ptr);   /* Flags & Version */
-       if ((ntohs(*ptr) & L2TP_VERSION_MASK) == L2TP_VERSION_L2TP) {
+       if ((EXTRACT_16BITS(ptr) & L2TP_VERSION_MASK) == L2TP_VERSION_L2TP) {
                printf(" l2tp:");
-       } else if ((ntohs(*ptr) & L2TP_VERSION_MASK) == L2TP_VERSION_L2F) {
+       } else if ((EXTRACT_16BITS(ptr) & L2TP_VERSION_MASK) == L2TP_VERSION_L2F) {
                printf(" l2f:");
                return;         /* nothing to do */
        } else {
@@ -624,23 +634,23 @@ l2tp_print(const u_char *dat, u_int length)
        }
 
        printf("[");
-       if (ntohs(*ptr) & L2TP_FLAG_TYPE) {
+       if (EXTRACT_16BITS(ptr) & L2TP_FLAG_TYPE) {
                flag_t = TRUE;
                printf("T");
        }
-       if (ntohs(*ptr) & L2TP_FLAG_LENGTH) {
+       if (EXTRACT_16BITS(ptr) & L2TP_FLAG_LENGTH) {
                flag_l = TRUE;
                printf("L");
        }
-       if (ntohs(*ptr) & L2TP_FLAG_SEQUENCE) {
+       if (EXTRACT_16BITS(ptr) & L2TP_FLAG_SEQUENCE) {
                flag_s = TRUE;
                printf("S");
        }
-       if (ntohs(*ptr) & L2TP_FLAG_OFFSET) {
+       if (EXTRACT_16BITS(ptr) & L2TP_FLAG_OFFSET) {
                flag_o = TRUE;
                printf("O");
        }
-       if (ntohs(*ptr) & L2TP_FLAG_PRIORITY) {
+       if (EXTRACT_16BITS(ptr) & L2TP_FLAG_PRIORITY) {
                flag_p = TRUE;
                printf("P");
        }
@@ -648,34 +658,34 @@ l2tp_print(const u_char *dat, u_int length)
 
        ptr++;
        cnt += 2;
-       
+
        if (flag_l) {
                TCHECK(*ptr);   /* Length */
-               l2tp_len = ntohs(*ptr++);
+               l2tp_len = EXTRACT_16BITS(ptr); ptr++;
                cnt += 2;
        } else {
                l2tp_len = 0;
        }
 
        TCHECK(*ptr);           /* Tunnel ID */
-       printf("(%u/", ntohs(*ptr++));
+       printf("(%u/", EXTRACT_16BITS(ptr)); ptr++;
        cnt += 2;
        TCHECK(*ptr);           /* Session ID */
-       printf("%u)",  ntohs(*ptr++));
+       printf("%u)",  EXTRACT_16BITS(ptr)); ptr++;
        cnt += 2;
 
        if (flag_s) {
                TCHECK(*ptr);   /* Ns */
-               printf("Ns=%u,", ntohs(*ptr++));
+               printf("Ns=%u,", EXTRACT_16BITS(ptr)); ptr++;
                cnt += 2;
                TCHECK(*ptr);   /* Nr */
-               printf("Nr=%u",  ntohs(*ptr++));
+               printf("Nr=%u",  EXTRACT_16BITS(ptr)); ptr++;
                cnt += 2;
        }
 
        if (flag_o) {
                TCHECK(*ptr);   /* Offset Size */
-               pad =  ntohs(*ptr++);
+               pad =  EXTRACT_16BITS(ptr); ptr++;
                ptr += pad / sizeof(*ptr);
                cnt += (2 + pad);
        }
@@ -696,4 +706,4 @@ l2tp_print(const u_char *dat, u_int length)
 
  trunc:
        printf("%s", tstr);
-}      
+}