]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-sll.c
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[tcpdump] / print-sll.c
index 0f0a2e7f534e86e79087ded03583dcf72ccd85e2..87d824466508326df2d8cf9689aeb3a67632acce 100644 (file)
 
 /* \summary: Linux cooked sockets capture printer */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
+
+#ifdef __linux__
+#include <net/if.h>
 #endif
 
-#include <netdissect-stdinc.h>
+#include "netdissect-stdinc.h"
 
+#define ND_LONGJMP_FROM_TCHECK
 #include "netdissect.h"
 #include "addrtoname.h"
 #include "ethertype.h"
 #define SLL_ADDRLEN    8               /* length of address field */
 
 struct sll_header {
-       uint16_t        sll_pkttype;    /* packet type */
-       uint16_t        sll_hatype;     /* link-layer address type */
-       uint16_t        sll_halen;      /* link-layer address length */
-       uint8_t         sll_addr[SLL_ADDRLEN];  /* link-layer address */
-       uint16_t        sll_protocol;   /* protocol */
+       nd_uint16_t     sll_pkttype;    /* packet type */
+       nd_uint16_t     sll_hatype;     /* link-layer address type */
+       nd_uint16_t     sll_halen;      /* link-layer address length */
+       nd_byte         sll_addr[SLL_ADDRLEN];  /* link-layer address */
+       nd_uint16_t     sll_protocol;   /* protocol */
+};
+
+/*
+ * A DLT_LINUX_SLL2 fake link-layer header.
+ */
+#define SLL2_HDR_LEN   20              /* total header length */
+
+struct sll2_header {
+       nd_uint16_t     sll2_protocol;          /* protocol */
+       nd_uint16_t     sll2_reserved_mbz;      /* reserved - must be zero */
+       nd_uint32_t     sll2_if_index;          /* 1-based interface index */
+       nd_uint16_t     sll2_hatype;            /* link-layer address type */
+       nd_uint8_t      sll2_pkttype;           /* packet type */
+       nd_uint8_t      sll2_halen;             /* link-layer address length */
+       nd_byte         sll2_addr[SLL_ADDRLEN]; /* link-layer address */
 };
 
 /*
@@ -129,23 +147,25 @@ static const struct tok sll_pkttype_values[] = {
     { 0, NULL}
 };
 
-static inline void
+static void
 sll_print(netdissect_options *ndo, const struct sll_header *sllp, u_int length)
 {
        u_short ether_type;
 
-        ND_PRINT((ndo, "%3s ",tok2str(sll_pkttype_values,"?",EXTRACT_BE_U_2(&sllp->sll_pkttype))));
+       ndo->ndo_protocol = "sll";
+        ND_PRINT("%3s ",
+                tok2str(sll_pkttype_values,"?",GET_BE_U_2(sllp->sll_pkttype)));
 
        /*
         * XXX - check the link-layer address type value?
         * For now, we just assume 6 means Ethernet.
         * XXX - print others as strings of hex?
         */
-       if (EXTRACT_BE_U_2(&sllp->sll_halen) == 6)
-               ND_PRINT((ndo, "%s ", etheraddr_string(ndo, sllp->sll_addr)));
+       if (GET_BE_U_2(sllp->sll_halen) == MAC48_LEN)
+               ND_PRINT("%s ", GET_MAC48_STRING(sllp->sll_addr));
 
        if (!ndo->ndo_qflag) {
-               ether_type = EXTRACT_BE_U_2(&sllp->sll_protocol);
+               ether_type = GET_BE_U_2(sllp->sll_protocol);
 
                if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
                        /*
@@ -157,30 +177,30 @@ sll_print(netdissect_options *ndo, const struct sll_header *sllp, u_int length)
                                /*
                                 * Ethernet_802.3 IPX frame.
                                 */
-                               ND_PRINT((ndo, "802.3"));
+                               ND_PRINT("802.3");
                                break;
 
                        case LINUX_SLL_P_802_2:
                                /*
                                 * 802.2.
                                 */
-                               ND_PRINT((ndo, "802.2"));
+                               ND_PRINT("802.2");
                                break;
 
                        default:
                                /*
                                 * What is it?
                                 */
-                               ND_PRINT((ndo, "ethertype Unknown (0x%04x)",
-                                   ether_type));
+                               ND_PRINT("ethertype Unknown (0x%04x)",
+                                   ether_type);
                                break;
                        }
                } else {
-                       ND_PRINT((ndo, "ethertype %s (0x%04x)",
+                       ND_PRINT("ethertype %s (0x%04x)",
                            tok2str(ethertype_values, "Unknown", ether_type),
-                           ether_type));
+                           ether_type);
                }
-               ND_PRINT((ndo, ", length %u: ", length));
+               ND_PRINT(", length %u: ", length);
        }
 }
 
@@ -190,25 +210,19 @@ sll_print(netdissect_options *ndo, const struct sll_header *sllp, u_int length)
  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
  * is the number of bytes actually captured.
  */
-u_int
+void
 sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
 {
        u_int caplen = h->caplen;
        u_int length = h->len;
        const struct sll_header *sllp;
+       u_short hatype;
        u_short ether_type;
        int llc_hdrlen;
        u_int hdrlen;
 
-       if (caplen < SLL_HDR_LEN) {
-               /*
-                * XXX - this "can't happen" because "pcap-linux.c" always
-                * adds this many bytes of header to every packet in a
-                * cooked socket capture.
-                */
-               ND_PRINT((ndo, "[|sll]"));
-               return (caplen);
-       }
+       ndo->ndo_protocol = "sll";
+       ND_TCHECK_LEN(p, SLL_HDR_LEN);
 
        sllp = (const struct sll_header *)p;
 
@@ -223,7 +237,19 @@ sll_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char
        p += SLL_HDR_LEN;
        hdrlen = SLL_HDR_LEN;
 
-       ether_type = EXTRACT_BE_U_2(&sllp->sll_protocol);
+       hatype = GET_BE_U_2(sllp->sll_hatype);
+       switch (hatype) {
+
+       case 803:
+               /*
+                * This is an packet with a radiotap header;
+                * just dissect the payload as such.
+                */
+               ndo->ndo_ll_hdr_len += SLL_HDR_LEN;
+               ndo->ndo_ll_hdr_len += ieee802_11_radio_print(ndo, p, length, caplen);
+               return;
+       }
+       ether_type = GET_BE_U_2(sllp->sll_protocol);
 
 recurse:
        /*
@@ -269,25 +295,23 @@ recurse:
                 * the enclosed type field.
                 */
                if (caplen < 4) {
-                       ND_PRINT((ndo, "[|vlan]"));
-                       return (hdrlen + caplen);
-               }
-               if (length < 4) {
-                       ND_PRINT((ndo, "[|vlan]"));
-                       return (hdrlen + length);
+                       ndo->ndo_protocol = "vlan";
+                       nd_print_trunc(ndo);
+                       ndo->ndo_ll_hdr_len += hdrlen + caplen;
+                       return;
                }
                if (ndo->ndo_eflag) {
-                       uint16_t tag = EXTRACT_BE_U_2(p);
+                       uint16_t tag = GET_BE_U_2(p);
 
-                       ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
+                       ND_PRINT("%s, ", ieee8021q_tci_string(tag));
                }
 
-               ether_type = EXTRACT_BE_U_2(p + 2);
+               ether_type = GET_BE_U_2(p + 2);
                if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
                        ether_type = LINUX_SLL_P_802_2;
                if (!ndo->ndo_qflag) {
-                       ND_PRINT((ndo, "ethertype %s, ",
-                           tok2str(ethertype_values, "Unknown", ether_type)));
+                       ND_PRINT("ethertype %s, ",
+                           tok2str(ethertype_values, "Unknown", ether_type));
                }
                p += 4;
                length -= 4;
@@ -304,5 +328,214 @@ recurse:
                }
        }
 
-       return (hdrlen);
+       ndo->ndo_ll_hdr_len += hdrlen;
+}
+
+static void
+sll2_print(netdissect_options *ndo, const struct sll2_header *sllp, u_int length)
+{
+       u_short ether_type;
+
+       ndo->ndo_protocol = "sll2";
+       ND_PRINT("ifindex %u ", GET_BE_U_4(sllp->sll2_if_index));
+
+       /*
+        * XXX - check the link-layer address type value?
+        * For now, we just assume 6 means Ethernet.
+        * XXX - print others as strings of hex?
+        */
+       if (GET_U_1(sllp->sll2_halen) == MAC48_LEN)
+               ND_PRINT("%s ", GET_MAC48_STRING(sllp->sll2_addr));
+
+       if (!ndo->ndo_qflag) {
+               ether_type = GET_BE_U_2(sllp->sll2_protocol);
+
+               if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
+                       /*
+                        * Not an Ethernet type; what type is it?
+                        */
+                       switch (ether_type) {
+
+                       case LINUX_SLL_P_802_3:
+                               /*
+                                * Ethernet_802.3 IPX frame.
+                                */
+                               ND_PRINT("802.3");
+                               break;
+
+                       case LINUX_SLL_P_802_2:
+                               /*
+                                * 802.2.
+                                */
+                               ND_PRINT("802.2");
+                               break;
+
+                       default:
+                               /*
+                                * What is it?
+                                */
+                               ND_PRINT("ethertype Unknown (0x%04x)",
+                                   ether_type);
+                               break;
+                       }
+               } else {
+                       ND_PRINT("ethertype %s (0x%04x)",
+                           tok2str(ethertype_values, "Unknown", ether_type),
+                           ether_type);
+               }
+               ND_PRINT(", length %u: ", length);
+       }
+}
+
+/*
+ * This is the top level routine of the printer.  'p' points to the
+ * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
+ * 'h->len' is the length of the packet off the wire, and 'h->caplen'
+ * is the number of bytes actually captured.
+ *
+ * On Linux, we attempt to look up the interface index and print the
+ * name of the interface on which the packet arrived or was sent.
+ *
+ * This look up is only likely to work well if done on the same machine
+ * as the one on which the capture was done, as the interface with a
+ * given index on the latter machine is unlikely to have the same
+ * name as the interface with that index on the former machine.
+ *
+ * As DLT_LINUX_SLL2 live captures are supported only on Linux, this
+ * means that if the machine on which we're reading the file isn't
+ * running Linux, it's probably not the machine that captured the file,
+ * so we don't bother trying to do the lookup on non-Linux machines.
+ */
+void
+sll2_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
+{
+       u_int caplen = h->caplen;
+       u_int length = h->len;
+       const struct sll2_header *sllp;
+       u_short hatype;
+       u_short ether_type;
+       int llc_hdrlen;
+       u_int hdrlen;
+#ifdef __linux__
+       uint32_t if_index;
+       char ifname[IF_NAMESIZE];
+#endif
+
+       ndo->ndo_protocol = "sll2";
+       ND_TCHECK_LEN(p, SLL2_HDR_LEN);
+
+       sllp = (const struct sll2_header *)p;
+#ifdef __linux__
+       if_index = GET_BE_U_4(sllp->sll2_if_index);
+       if (!if_indextoname(if_index, ifname))
+               strncpy(ifname, "?", 2);
+       ND_PRINT("%-5s ", ifname);
+#endif
+
+       ND_PRINT("%-3s ",
+                tok2str(sll_pkttype_values, "?", GET_U_1(sllp->sll2_pkttype)));
+
+       if (ndo->ndo_eflag)
+               sll2_print(ndo, sllp, length);
+
+       /*
+        * Go past the cooked-mode header.
+        */
+       length -= SLL2_HDR_LEN;
+       caplen -= SLL2_HDR_LEN;
+       p += SLL2_HDR_LEN;
+       hdrlen = SLL2_HDR_LEN;
+
+       hatype = GET_BE_U_2(sllp->sll2_hatype);
+       switch (hatype) {
+
+       case 803:
+               /*
+                * This is an packet with a radiotap header;
+                * just dissect the payload as such.
+                */
+               ndo->ndo_ll_hdr_len += SLL2_HDR_LEN;
+               ndo->ndo_ll_hdr_len += ieee802_11_radio_print(ndo, p, length, caplen);
+               return;
+       }
+       ether_type = GET_BE_U_2(sllp->sll2_protocol);
+
+recurse:
+       /*
+        * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
+        * packet type?
+        */
+       if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
+               /*
+                * Yes - what type is it?
+                */
+               switch (ether_type) {
+
+               case LINUX_SLL_P_802_3:
+                       /*
+                        * Ethernet_802.3 IPX frame.
+                        */
+                       ipx_print(ndo, p, length);
+                       break;
+
+               case LINUX_SLL_P_802_2:
+                       /*
+                        * 802.2.
+                        * Try to print the LLC-layer header & higher layers.
+                        */
+                       llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
+                       if (llc_hdrlen < 0)
+                               goto unknown;   /* unknown LLC type */
+                       hdrlen += llc_hdrlen;
+                       break;
+
+               default:
+                       /*FALLTHROUGH*/
+
+               unknown:
+                       /* packet type not known, print raw packet */
+                       if (!ndo->ndo_suppress_default_print)
+                               ND_DEFAULTPRINT(p, caplen);
+                       break;
+               }
+       } else if (ether_type == ETHERTYPE_8021Q) {
+               /*
+                * Print VLAN information, and then go back and process
+                * the enclosed type field.
+                */
+               if (caplen < 4) {
+                       ndo->ndo_protocol = "vlan";
+                       nd_print_trunc(ndo);
+                       ndo->ndo_ll_hdr_len += hdrlen + caplen;
+                       return;
+               }
+               if (ndo->ndo_eflag) {
+                       uint16_t tag = GET_BE_U_2(p);
+
+                       ND_PRINT("%s, ", ieee8021q_tci_string(tag));
+               }
+
+               ether_type = GET_BE_U_2(p + 2);
+               if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
+                       ether_type = LINUX_SLL_P_802_2;
+               if (!ndo->ndo_qflag) {
+                       ND_PRINT("ethertype %s, ",
+                           tok2str(ethertype_values, "Unknown", ether_type));
+               }
+               p += 4;
+               length -= 4;
+               caplen -= 4;
+               hdrlen += 4;
+               goto recurse;
+       } else {
+               if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
+                       /* ether_type not known, print raw packet */
+                       if (!ndo->ndo_eflag)
+                               sll2_print(ndo, sllp, length + SLL2_HDR_LEN);
+                       if (!ndo->ndo_suppress_default_print)
+                               ND_DEFAULTPRINT(p, caplen);
+               }
+       }
+
+       ndo->ndo_ll_hdr_len += hdrlen;
 }