Blame SOURCES/0016-direction-for-any.patch

acb145
diff --git a/netdissect.h b/netdissect.h
acb145
index 4f3c666a..93fa8be6 100644
acb145
--- a/netdissect.h
acb145
+++ b/netdissect.h
acb145
@@ -465,6 +465,7 @@ extern u_int raw_if_print IF_PRINTER_ARGS;
acb145
 extern u_int sl_bsdos_if_print IF_PRINTER_ARGS;
acb145
 extern u_int sl_if_print IF_PRINTER_ARGS;
acb145
 extern u_int sll_if_print IF_PRINTER_ARGS;
acb145
+extern u_int sll2_if_print IF_PRINTER_ARGS;
acb145
 extern u_int sunatm_if_print IF_PRINTER_ARGS;
acb145
 extern u_int symantec_if_print IF_PRINTER_ARGS;
acb145
 extern u_int token_if_print IF_PRINTER_ARGS;
acb145
diff --git a/print-sll.c b/print-sll.c
acb145
index 571b7c5e..5a4e2f68 100644
acb145
--- a/print-sll.c
acb145
+++ b/print-sll.c
acb145
@@ -84,6 +84,21 @@ struct sll_header {
acb145
 	uint16_t	sll_protocol;	/* protocol */
acb145
 };
acb145
 
acb145
+/*
acb145
+ * A DLT_LINUX_SLL2 fake link-layer header.
acb145
+ */
acb145
+#define SLL2_HDR_LEN	20		/* total header length */
acb145
+
acb145
+struct sll2_header {
acb145
+	uint16_t	sll2_protocol;		/* protocol */
acb145
+	uint16_t	sll2_reserved_mbz;	/* reserved - must be zero */
acb145
+	uint32_t	sll2_if_index;		/* 1-based interface index */
acb145
+	uint16_t	sll2_hatype;		/* link-layer address type */
acb145
+	uint8_t		sll2_pkttype;		/* packet type */
acb145
+	uint8_t		sll2_halen;		/* link-layer address length */
acb145
+	u_char		sll2_addr[SLL_ADDRLEN];	/* link-layer address */
acb145
+};
acb145
+
acb145
 /*
acb145
  * The LINUX_SLL_ values for "sll_pkttype"; these correspond to the
acb145
  * PACKET_ values on Linux, but are defined here so that they're
acb145
@@ -308,3 +323,192 @@ recurse:
acb145
 
acb145
 	return (hdrlen);
acb145
 }
acb145
+
acb145
+static void
acb145
+sll2_print(netdissect_options *ndo, const struct sll2_header *sllp, u_int length)
acb145
+{
acb145
+	u_short ether_type;
acb145
+
acb145
+	ndo->ndo_protocol = "sll2";
acb145
+       ND_PRINT((ndo,"ifindex %u ", EXTRACT_32BITS(&sllp->sll2_if_index)));
acb145
+
acb145
+	/*
acb145
+	 * XXX - check the link-layer address type value?
acb145
+	 * For now, we just assume 6 means Ethernet.
acb145
+	 * XXX - print others as strings of hex?
acb145
+	 */
acb145
+	if (EXTRACT_8BITS(&sllp->sll2_halen) == 6)
acb145
+		ND_PRINT((ndo, "%s ", etheraddr_string(ndo, sllp->sll2_addr)));
acb145
+
acb145
+	if (!ndo->ndo_qflag) {
acb145
+		ether_type = EXTRACT_16BITS(&sllp->sll2_protocol);
acb145
+
acb145
+		if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
acb145
+			/*
acb145
+			 * Not an Ethernet type; what type is it?
acb145
+			 */
acb145
+			switch (ether_type) {
acb145
+
acb145
+			case LINUX_SLL_P_802_3:
acb145
+				/*
acb145
+				 * Ethernet_802.3 IPX frame.
acb145
+				 */
acb145
+				ND_PRINT((ndo, "802.3"));
acb145
+				break;
acb145
+
acb145
+			case LINUX_SLL_P_802_2:
acb145
+				/*
acb145
+				 * 802.2.
acb145
+				 */
acb145
+				ND_PRINT((ndo, "802.2"));
acb145
+				break;
acb145
+
acb145
+			default:
acb145
+				/*
acb145
+				 * What is it?
acb145
+				 */
acb145
+				ND_PRINT((ndo, "ethertype Unknown (0x%04x)",
acb145
+				    ether_type));
acb145
+				break;
acb145
+			}
acb145
+		} else {
acb145
+			ND_PRINT((ndo, "ethertype %s (0x%04x)",
acb145
+			    tok2str(ethertype_values, "Unknown", ether_type),
acb145
+			    ether_type));
acb145
+		}
acb145
+		ND_PRINT((ndo, ", length %u: ", length));
acb145
+	}
acb145
+}
acb145
+
acb145
+/*
acb145
+ * This is the top level routine of the printer.  'p' points to the
acb145
+ * Linux "cooked capture" header of the packet, 'h->ts' is the timestamp,
acb145
+ * 'h->len' is the length of the packet off the wire, and 'h->caplen'
acb145
+ * is the number of bytes actually captured.
acb145
+ */
acb145
+u_int
acb145
+sll2_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
acb145
+{
acb145
+	u_int caplen = h->caplen;
acb145
+	u_int length = h->len;
acb145
+	const struct sll2_header *sllp;
acb145
+	u_short ether_type;
acb145
+	int llc_hdrlen;
acb145
+	u_int hdrlen;
acb145
+
acb145
+	ndo->ndo_protocol = "sll2";
acb145
+	if (caplen < SLL2_HDR_LEN) {
acb145
+		/*
acb145
+		 * XXX - this "can't happen" because "pcap-linux.c" always
acb145
+		 * adds this many bytes of header to every packet in a
acb145
+		 * cooked socket capture.
acb145
+		 */
acb145
+		ND_PRINT((ndo, " [|%s]", ndo->ndo_protocol));
acb145
+		return (caplen);
acb145
+	}
acb145
+
acb145
+	sllp = (const struct sll2_header *)p;
acb145
+#ifdef HAVE_NET_IF_H
acb145
+	uint32_t if_index = EXTRACT_32BITS(&sllp->sll2_if_index);
acb145
+	if (!if_indextoname(if_index, ifname))
acb145
+		strncpy(ifname, "?", 2);
acb145
+	ND_PRINT((ndo, "%-5s ", ifname));
acb145
+#endif
acb145
+
acb145
+	ND_PRINT((ndo, "%-3s ",
acb145
+		tok2str(sll_pkttype_values, "?", EXTRACT_8BITS(&sllp->sll2_pkttype))));
acb145
+
acb145
+	if (ndo->ndo_eflag)
acb145
+		sll2_print(ndo, sllp, length);
acb145
+
acb145
+	/*
acb145
+	 * Go past the cooked-mode header.
acb145
+	 */
acb145
+	length -= SLL2_HDR_LEN;
acb145
+	caplen -= SLL2_HDR_LEN;
acb145
+	p += SLL2_HDR_LEN;
acb145
+	hdrlen = SLL2_HDR_LEN;
acb145
+
acb145
+	ether_type = EXTRACT_16BITS(&sllp->sll2_protocol);
acb145
+
acb145
+recurse:
acb145
+	/*
acb145
+	 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet
acb145
+	 * packet type?
acb145
+	 */
acb145
+	if (ether_type <= MAX_ETHERNET_LENGTH_VAL) {
acb145
+		/*
acb145
+		 * Yes - what type is it?
acb145
+		 */
acb145
+		switch (ether_type) {
acb145
+
acb145
+		case LINUX_SLL_P_802_3:
acb145
+			/*
acb145
+			 * Ethernet_802.3 IPX frame.
acb145
+			 */
acb145
+			ipx_print(ndo, p, length);
acb145
+			break;
acb145
+
acb145
+		case LINUX_SLL_P_802_2:
acb145
+			/*
acb145
+			 * 802.2.
acb145
+			 * Try to print the LLC-layer header & higher layers.
acb145
+			 */
acb145
+			llc_hdrlen = llc_print(ndo, p, length, caplen, NULL, NULL);
acb145
+			if (llc_hdrlen < 0)
acb145
+				goto unknown;	/* unknown LLC type */
acb145
+			hdrlen += llc_hdrlen;
acb145
+			break;
acb145
+
acb145
+		default:
acb145
+			/*FALLTHROUGH*/
acb145
+
acb145
+		unknown:
acb145
+			/* packet type not known, print raw packet */
acb145
+			if (!ndo->ndo_suppress_default_print)
acb145
+				ND_DEFAULTPRINT(p, caplen);
acb145
+			break;
acb145
+		}
acb145
+	} else if (ether_type == ETHERTYPE_8021Q) {
acb145
+		/*
acb145
+		 * Print VLAN information, and then go back and process
acb145
+		 * the enclosed type field.
acb145
+		 */
acb145
+		if (caplen < 4) {
acb145
+			ND_PRINT((ndo, "[|vlan]"));
acb145
+			return (hdrlen + caplen);
acb145
+		}
acb145
+		if (length < 4) {
acb145
+			ND_PRINT((ndo, "[|vlan]"));
acb145
+			return (hdrlen + length);
acb145
+		}
acb145
+	        if (ndo->ndo_eflag) {
acb145
+			uint16_t tag = EXTRACT_16BITS(p);
acb145
+
acb145
+			ND_PRINT((ndo, "%s, ", ieee8021q_tci_string(tag)));
acb145
+		}
acb145
+
acb145
+		ether_type = EXTRACT_16BITS(p + 2);
acb145
+		if (ether_type <= MAX_ETHERNET_LENGTH_VAL)
acb145
+			ether_type = LINUX_SLL_P_802_2;
acb145
+		if (!ndo->ndo_qflag) {
acb145
+			ND_PRINT((ndo, "ethertype %s, ",
acb145
+			    tok2str(ethertype_values, "Unknown", ether_type)));
acb145
+		}
acb145
+		p += 4;
acb145
+		length -= 4;
acb145
+		caplen -= 4;
acb145
+		hdrlen += 4;
acb145
+		goto recurse;
acb145
+	} else {
acb145
+		if (ethertype_print(ndo, ether_type, p, length, caplen, NULL, NULL) == 0) {
acb145
+			/* ether_type not known, print raw packet */
acb145
+			if (!ndo->ndo_eflag)
acb145
+				sll2_print(ndo, sllp, length + SLL2_HDR_LEN);
acb145
+			if (!ndo->ndo_suppress_default_print)
acb145
+				ND_DEFAULTPRINT(p, caplen);
acb145
+		}
acb145
+	}
acb145
+
acb145
+	return (hdrlen);
acb145
+}
acb145
diff --git a/print.c b/print.c
acb145
index b9c92adc..4cc35bab 100644
acb145
--- a/print.c
acb145
+++ b/print.c
acb145
@@ -126,6 +126,9 @@ static const struct printer printers[] = {
acb145
 #ifdef DLT_LINUX_SLL
acb145
 	{ sll_if_print,		DLT_LINUX_SLL },
acb145
 #endif
acb145
+#ifdef DLT_LINUX_SLL2
acb145
+	{ sll2_if_print,	DLT_LINUX_SLL2 },
acb145
+#endif
acb145
 #ifdef DLT_FR
acb145
 	{ fr_if_print,		DLT_FR },
acb145
 #endif
acb145
diff --git a/ethertype.h b/ethertype.h
acb145
index f38ec8e4..7719a6f0 100644
acb145
--- a/ethertype.h
acb145
+++ b/ethertype.h
acb145
@@ -19,6 +19,13 @@
acb145
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
acb145
  */
acb145
 
acb145
+/*
acb145
+ * Maximum length of the length field in an Ethernet header; any value
acb145
+ * greater than this is not a length value, so it's either an Ethernet
acb145
+ * type or an invalid value.
acb145
+ */
acb145
+#define        MAX_ETHERNET_LENGTH_VAL 1500
acb145
+
acb145
 /*
acb145
  * Ethernet types.
acb145
  *
acb145
diff --git a/config.h.in b/config.h.in
acb145
index 4fcbba77..8ae16730 100644
acb145
--- a/config.h.in
acb145
+++ b/config.h.in
acb145
@@ -78,6 +78,9 @@
acb145
 /* Define to 1 if you have the <netinet/if_ether.h> header file. */
acb145
 #undef HAVE_NETINET_IF_ETHER_H
acb145
 
acb145
+/* Define to 1 if you have the <net/if.h> header file. */
acb145
+#undef HAVE_NET_IF_H
acb145
+
acb145
 /* Define to 1 if you have the <net/if_pflog.h> header file. */
acb145
 #undef HAVE_NET_IF_PFLOG_H
acb145
 
acb145
diff --git a/configure b/configure
acb145
index eb33db18..f64c4eea 100755
acb145
--- a/configure
acb145
+++ b/configure
acb145
@@ -4037,7 +4037,7 @@ fi
acb145
 done
acb145
 
acb145
 
acb145
-for ac_header in fcntl.h rpc/rpc.h rpc/rpcent.h
acb145
+for ac_header in fcntl.h rpc/rpc.h rpc/rpcent.h net/if.h
acb145
 do :
acb145
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
acb145
 ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
acb145
diff --git a/configure.ac b/configure.ac
acb145
index 32f48b60..46f841c0 100644
acb145
--- a/configure.ac
acb145
+++ b/configure.ac
acb145
@@ -24,7 +24,7 @@ AC_PROG_CC_C99
acb145
 	fi
acb145
 fi
acb145
 
acb145
-AC_CHECK_HEADERS(fcntl.h rpc/rpc.h rpc/rpcent.h)
acb145
+AC_CHECK_HEADERS(fcntl.h rpc/rpc.h rpc/rpcent.h net/if.h)
acb145
 AC_CHECK_HEADERS(net/pfvar.h, , , [#include <sys/types.h>
acb145
 #include <sys/socket.h>
acb145
 #include <net/if.h>])
acb145
diff --git a/print-sll.c b/print-sll.c
acb145
index 96031442..e6c7bd4a 100644
acb145
--- a/print-sll.c
acb145
+++ b/print-sll.c
acb145
@@ -25,6 +25,10 @@
acb145
 #include "config.h"
acb145
 #endif
acb145
 
acb145
+#ifdef HAVE_NET_IF_H
acb145
+#include <net/if.h>
acb145
+#endif
acb145
+
acb145
 #include <netdissect-stdinc.h>
acb145
 
acb145
 #include "netdissect.h"
acb145
@@ -395,6 +399,9 @@ sll2_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char
acb145
 	u_short ether_type;
acb145
 	int llc_hdrlen;
acb145
 	u_int hdrlen;
acb145
+#ifdef HAVE_NET_IF_H
acb145
+	char ifname[IF_NAMESIZE];
acb145
+#endif
acb145
 
acb145
 	ndo->ndo_protocol = "sll2";
acb145
 	if (caplen < SLL2_HDR_LEN) {
acb145
diff --git a/tcpdump.c b/tcpdump.c
acb145
index 2bef72c8..0ef21117 100644
acb145
--- a/tcpdump.c
acb145
+++ b/tcpdump.c
acb145
@@ -1978,6 +1978,10 @@ main(int argc, char **argv)
acb145
 			    RFileName, dlt_name,
acb145
 			    pcap_datalink_val_to_description(dlt));
acb145
 		}
acb145
+#ifdef DLT_LINUX_SLL2
acb145
+		if (dlt == DLT_LINUX_SLL2)
acb145
+			fprintf(stderr, "Warning: interface names might be incorrect\n");
acb145
+#endif
acb145
 	} else {
acb145
 		/*
acb145
 		 * We're doing a live capture.
acb145
diff --git a/tcpdump.c b/tcpdump.c
acb145
index 376d9a20..06d3d9b9 100644
acb145
--- a/tcpdump.c
acb145
+++ b/tcpdump.c
acb145
@@ -155,6 +155,7 @@ static int Iflag;			/* rfmon (monitor) mode */
acb145
 static int Jflag;			/* list available time stamp types */
acb145
 #endif
acb145
 static int jflag = -1;			/* packet time stamp source */
acb145
+static int oflag = 0;
acb145
 static int pflag;			/* don't go promiscuous */
acb145
 #ifdef HAVE_PCAP_SETDIRECTION
acb145
 static int Qflag = -1;			/* restrict captured packet by send/receive direction */
acb145
@@ -515,7 +515,7 @@ show_devices_and_exit (void)
acb145
 #define Q_FLAG
acb145
 #endif
acb145
 
acb145
-#define SHORTOPTS "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:s:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:#"
acb145
+#define SHORTOPTS "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNoOpq" Q_FLAG "r:s:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:#"
acb145
 
acb145
 /*
acb145
  * Long options.
acb145
@@ -1340,6 +1341,10 @@ main(int argc, char **argv)
acb145
 			++ndo->ndo_Nflag;
acb145
 			break;
acb145
 
acb145
+		case 'o':
acb145
+			oflag++;
acb145
+			break;
acb145
+
acb145
 		case 'O':
acb145
 			Oflag = 0;
acb145
 			break;
acb145
@@ -1932,6 +1932,14 @@ main(int argc, char **argv)
acb145
 		show_devices_and_exit();
acb145
 #endif
acb145
 
acb145
+#if defined(DLT_LINUX_SLL2) && defined(HAVE_PCAP_SET_DATALINK)
acb145
+/* Set default linktype DLT_LINUX_SLL2 when capturing on the "any" device */
acb145
+		if (device != NULL &&
acb145
+		    strncmp (device, "any", strlen("any")) == 0
acb145
+		    && yflag_dlt == -1 && oflag > 0)
acb145
+			yflag_dlt = DLT_LINUX_SLL2;
acb145
+#endif
acb145
+
acb145
 	switch (ndo->ndo_tflag) {
acb145
 
acb145
 	case 0: /* Default */
acb145
@@ -2180,7 +2188,8 @@ main(int argc, char **argv)
acb145
 			}
acb145
 #endif
acb145
 			(void)fprintf(stderr, "%s: data link type %s\n",
acb145
-				      program_name, yflag_dlt_name);
acb145
+				      program_name,
acb145
+				      pcap_datalink_val_to_name(yflag_dlt));
acb145
 			(void)fflush(stderr);
acb145
 		}
acb145
 		i = pcap_snapshot(pd);