]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-ether.c
Fix the pointer tests in the non-ndoified TTEST2() macro as well.
[tcpdump] / print-ether.c
index 581d688207e8e47162ea550ad05af6067805553f..e086d1660c090c8b2dd160051e0d996067ec3903 100644 (file)
@@ -113,20 +113,32 @@ ether_hdr_print(register const u_char *bp, u_int length)
        (void)printf(", length %u: ", length);
 }
 
+/*
+ * Print an Ethernet frame.
+ * This might be encapsulated within another frame; we might be passed
+ * a pointer to a function that can print header information for that
+ * frame's protocol, and an argument to pass to that function.
+ */
 void
-ether_print(const u_char *p, u_int length, u_int caplen)
+ether_print(const u_char *p, u_int length, u_int caplen,
+    void (*print_encap_header)(const u_char *), const u_char *encap_header_arg)
 {
        struct ether_header *ep;
+       u_int orig_length;
        u_short ether_type;
        u_short extracted_ether_type;
 
-       if (caplen < ETHER_HDRLEN) {
+       if (caplen < ETHER_HDRLEN || length < ETHER_HDRLEN) {
                printf("[|ether]");
                return;
        }
 
-       if (eflag)
+       if (eflag) {
+               if (print_encap_header != NULL)
+                       (*print_encap_header)(encap_header_arg);
                ether_hdr_print(p, length);
+       }
+       orig_length = length;
 
        length -= ETHER_HDRLEN;
        caplen -= ETHER_HDRLEN;
@@ -135,6 +147,7 @@ ether_print(const u_char *p, u_int length, u_int caplen)
 
        ether_type = EXTRACT_16BITS(&ep->ether_type);
 
+recurse:
        /*
         * Is it (gag) an 802.3 encapsulation?
         */
@@ -143,21 +156,76 @@ ether_print(const u_char *p, u_int length, u_int caplen)
                if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
                    &extracted_ether_type) == 0) {
                        /* ether_type not known, print raw packet */
-                       if (!eflag)
-                               ether_hdr_print((u_char *)ep, length + ETHER_HDRLEN);
+                       if (!eflag) {
+                               if (print_encap_header != NULL)
+                                       (*print_encap_header)(encap_header_arg);
+                               ether_hdr_print((u_char *)ep, orig_length);
+                       }
+
+                       if (!suppress_default_print)
+                               default_print(p, caplen);
+               }
+       } else if (ether_type == ETHERTYPE_8021Q) {
+               /*
+                * Print VLAN information, and then go back and process
+                * the enclosed type field.
+                */
+               if (caplen < 4 || length < 4) {
+                       printf("[|vlan]");
+                       return;
+               }
+               if (eflag) {
+                       u_int16_t tag = EXTRACT_16BITS(p);
+
+                       printf("vlan %u, p %u%s, ",
+                           tag & 0xfff,
+                           tag >> 13,
+                           (tag & 0x1000) ? ", CFI" : "");
+               }
+
+               ether_type = EXTRACT_16BITS(p + 2);
+               if (eflag && ether_type > ETHERMTU)
+                       printf("ethertype %s, ", tok2str(ethertype_values,"0x%04x", ether_type));
+               p += 4;
+               length -= 4;
+               caplen -= 4;
+               goto recurse;
+       } else if (ether_type == ETHERTYPE_JUMBO) {
+               /*
+                * Alteon jumbo frames.
+                * See
+                *
+                *      http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
+                *
+                * which indicates that, following the type field,
+                * there's an LLC header and payload.
+                */
+               /* Try to print the LLC-layer header & higher layers */
+               if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
+                   &extracted_ether_type) == 0) {
+                       /* ether_type not known, print raw packet */
+                       if (!eflag) {
+                               if (print_encap_header != NULL)
+                                       (*print_encap_header)(encap_header_arg);
+                               ether_hdr_print((u_char *)ep, orig_length);
+                       }
+
+                       if (!suppress_default_print)
+                               default_print(p, caplen);
+               }
+       } else {
+               if (ethertype_print(ether_type, p, length, caplen) == 0) {
+                       /* ether_type not known, print raw packet */
+                       if (!eflag) {
+                               if (print_encap_header != NULL)
+                                       (*print_encap_header)(encap_header_arg);
+                               ether_hdr_print((u_char *)ep, orig_length);
+                       }
 
                        if (!suppress_default_print)
                                default_print(p, caplen);
                }
-       } else if (ether_encap_print(ether_type, p, length, caplen,
-           &extracted_ether_type) == 0) {
-               /* ether_type not known, print raw packet */
-               if (!eflag)
-                       ether_hdr_print((u_char *)ep, length + ETHER_HDRLEN);
-
-               if (!suppress_default_print)
-                       default_print(p, caplen);
-       } 
+       }
 }
 
 /*
@@ -169,30 +237,21 @@ ether_print(const u_char *p, u_int length, u_int caplen)
 u_int
 ether_if_print(const struct pcap_pkthdr *h, const u_char *p)
 {
-       ether_print(p, h->len, h->caplen);
+       ether_print(p, h->len, h->caplen, NULL, NULL);
 
        return (ETHER_HDRLEN);
 }
 
 /*
- * Prints the packet encapsulated in an Ethernet data segment
- * (or an equivalent encapsulation), given the Ethernet type code.
+ * Prints the packet payload, given an Ethernet type code for the payload's
+ * protocol.
  *
  * Returns non-zero if it can do so, zero if the ethertype is unknown.
- *
- * The Ethernet type code is passed through a pointer; if it was
- * ETHERTYPE_8021Q, it gets updated to be the Ethernet type of
- * the 802.1Q payload, for the benefit of lower layers that might
- * want to know what it is.
  */
 
 int
-ether_encap_print(u_short ether_type, const u_char *p,
-    u_int length, u_int caplen, u_short *extracted_ether_type)
+ethertype_print(u_short ether_type, const u_char *p, u_int length, u_int caplen)
 {
- recurse:
-       *extracted_ether_type = ether_type;
-
        switch (ether_type) {
 
        case ETHERTYPE_IP:
@@ -229,68 +288,6 @@ ether_encap_print(u_short ether_type, const u_char *p,
                ipx_print(p, length);
                return (1);
 
-       case ETHERTYPE_8021Q:
-               if (eflag) {
-                       u_int16_t tag = EXTRACT_16BITS(p);
-
-                       printf("vlan %u, p %u%s, ",
-                           tag & 0xfff,
-                           tag >> 13,
-                           (tag & 0x1000) ? ", CFI" : "");
-               }
-
-               ether_type = EXTRACT_16BITS(p + 2);
-               p += 4;
-               length -= 4;
-               caplen -= 4;
-
-               if (ether_type > ETHERMTU) {
-                       if (eflag)
-                               printf("ethertype %s, ",
-                                      tok2str(ethertype_values,"0x%04x", ether_type));
-                       goto recurse;
-               }
-
-               *extracted_ether_type = 0;
-
-               if (llc_print(p, length, caplen, p - 18, p - 12,
-                   extracted_ether_type) == 0) {
-                        ether_hdr_print(p - 18, length + 4);
-
-                        if (!suppress_default_print) {
-                                default_print(p - 18, caplen + 4);
-                        }
-               }
-
-
-               return (1);
-
-        case ETHERTYPE_JUMBO:
-                ether_type = EXTRACT_16BITS(p);
-                p += 2;
-                length -= 2;      
-                caplen -= 2;
-
-                if (ether_type > ETHERMTU) {
-                    if (eflag)
-                        printf("ethertype %s, ",
-                               tok2str(ethertype_values,"0x%04x", ether_type));
-                    goto recurse;
-                }
-
-                *extracted_ether_type = 0;
-
-                if (llc_print(p, length, caplen, p - 16, p - 10,
-                              extracted_ether_type) == 0) {
-                    ether_hdr_print(p - 16, length + 2);
-
-                    if (!suppress_default_print) {
-                            default_print(p - 16, caplen + 2);
-                    }
-                }
-
-                return (1);
-
         case ETHERTYPE_ISO:
                 isoclns_print(p+1, length-1, length-1);
                 return(1);