Upgrade to libpcap 1.7.4.

Bug: http://b/24902618
Change-Id: I1bb6c1163b573eb01b4bed7c494a6d447e8d45cf
diff --git a/gencode.c b/gencode.c
index 7bb07fb..ad938d4 100644
--- a/gencode.c
+++ b/gencode.c
@@ -19,10 +19,6 @@
  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
-#ifndef lint
-static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
-#endif
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -107,6 +103,10 @@
 
 #define ETHERMTU	1500
 
+#ifndef ETHERTYPE_TEB
+#define ETHERTYPE_TEB 0x6558
+#endif
+
 #ifndef IPPROTO_HOPOPTS
 #define IPPROTO_HOPOPTS 0
 #endif
@@ -123,6 +123,8 @@
 #define IPPROTO_SCTP 132
 #endif
 
+#define GENEVE_PORT 6081
+
 #ifdef HAVE_OS_PROTO_H
 #include "os-proto.h"
 #endif
@@ -133,11 +135,11 @@
 static jmp_buf top_ctx;
 static pcap_t *bpf_pcap;
 
-/* Hack for updating VLAN, MPLS, and PPPoE offsets. */
+/* Hack for handling VLAN and MPLS stacks. */
 #ifdef WIN32
-static u_int	orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
+static u_int	label_stack_depth = (u_int)-1, vlan_stack_depth = (u_int)-1;
 #else
-static u_int	orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
+static u_int	label_stack_depth = -1U, vlan_stack_depth = -1U;
 #endif
 
 /* XXX */
@@ -167,17 +169,43 @@
 static struct block *root;
 
 /*
+ * Absolute offsets, which are offsets from the beginning of the raw
+ * packet data, are, in the general case, the sum of a variable value
+ * and a constant value; the variable value may be absent, in which
+ * case the offset is only the constant value, and the constant value
+ * may be zero, in which case the offset is only the variable value.
+ *
+ * bpf_abs_offset is a structure containing all that information:
+ *
+ *   is_variable is 1 if there's a variable part.
+ *
+ *   constant_part is the constant part of the value, possibly zero;
+ *
+ *   if is_variable is 1, reg is the register number for a register
+ *   containing the variable value if the register has been assigned,
+ *   and -1 otherwise.
+ */
+typedef struct {
+	int	is_variable;
+	u_int	constant_part;
+	int	reg;
+} bpf_abs_offset;
+
+/*
  * Value passed to gen_load_a() to indicate what the offset argument
- * is relative to.
+ * is relative to the beginning of.
  */
 enum e_offrel {
-	OR_PACKET,	/* relative to the beginning of the packet */
-	OR_LINK,	/* relative to the beginning of the link-layer header */
-	OR_MACPL,	/* relative to the end of the MAC-layer header */
-	OR_NET,		/* relative to the network-layer header */
-	OR_NET_NOSNAP,	/* relative to the network-layer header, with no SNAP header at the link layer */
-	OR_TRAN_IPV4,	/* relative to the transport-layer header, with IPv4 network layer */
-	OR_TRAN_IPV6	/* relative to the transport-layer header, with IPv6 network layer */
+	OR_PACKET,		/* full packet data */
+	OR_LINKHDR,		/* link-layer header */
+	OR_PREVLINKHDR,		/* previous link-layer header */
+	OR_LLC,			/* 802.2 LLC header */
+	OR_PREVMPLSHDR,		/* previous MPLS header */
+	OR_LINKTYPE,		/* link-layer type */
+	OR_LINKPL,		/* link-layer payload */
+	OR_LINKPL_NOSNAP,	/* link-layer payload, with no SNAP header at the link layer */
+	OR_TRAN_IPV4,		/* transport-layer header, with IPv4 network layer */
+	OR_TRAN_IPV6		/* transport-layer header, with IPv6 network layer */
 };
 
 #ifdef INET6
@@ -228,8 +256,7 @@
 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
     bpf_u_int32, bpf_u_int32, int, bpf_int32);
-static struct slist *gen_load_llrel(u_int, u_int);
-static struct slist *gen_load_macplrel(u_int, u_int);
+static struct slist *gen_load_absoffsetrel(bpf_abs_offset *, u_int, u_int);
 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
 static struct slist *gen_loadx_iphdrlen(void);
 static struct block *gen_uncond(int);
@@ -243,8 +270,7 @@
 static struct slist *gen_load_radiotap_llprefixlen(void);
 static struct slist *gen_load_ppi_llprefixlen(void);
 static void insert_compute_vloffsets(struct block *);
-static struct slist *gen_llprefixlen(void);
-static struct slist *gen_off_macpl(void);
+static struct slist *gen_abs_offset_varpart(bpf_abs_offset *);
 static int ethertype_to_ppptype(int);
 static struct block *gen_linktype(int);
 static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
@@ -289,6 +315,7 @@
 static struct block *gen_mac_multicast(int);
 static struct block *gen_len(int, int);
 static struct block *gen_check_802_11_data_frame(void);
+static struct block *gen_geneve_ll_check(void);
 
 static struct block *gen_ppi_dlt_check(void);
 static struct block *gen_msg_abbrev(int type);
@@ -398,38 +425,30 @@
 static bpf_u_int32 netmask;
 static int snaplen;
 int no_optimize;
-#ifdef WIN32
-static int
-pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
-	     const char *buf, int optimize, bpf_u_int32 mask);
 
 int
 pcap_compile(pcap_t *p, struct bpf_program *program,
 	     const char *buf, int optimize, bpf_u_int32 mask)
 {
-	int result;
-
-	EnterCriticalSection(&g_PcapCompileCriticalSection);
-
-	result = pcap_compile_unsafe(p, program, buf, optimize, mask);
-
-	LeaveCriticalSection(&g_PcapCompileCriticalSection);
-	
-	return result;
-}
-
-static int
-pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
-	     const char *buf, int optimize, bpf_u_int32 mask)
-#else /* WIN32 */
-int
-pcap_compile(pcap_t *p, struct bpf_program *program,
-	     const char *buf, int optimize, bpf_u_int32 mask)
-#endif /* WIN32 */
-{
 	extern int n_errors;
 	const char * volatile xbuf = buf;
 	u_int len;
+	int  rc;
+
+	/*
+	 * XXX - single-thread this code path with pthread calls on
+	 * UN*X, if the platform supports pthreads?  If that requires
+	 * a separate -lpthread, we might not want to do that.
+	 */
+#ifdef WIN32
+	extern int wsockinit (void);
+	static int done = 0;
+
+	if (!done)
+		wsockinit();
+	done = 1;
+	EnterCriticalSection(&g_PcapCompileCriticalSection);
+#endif
 
 	/*
 	 * If this pcap_t hasn't been activated, it doesn't have a
@@ -438,13 +457,15 @@
 	if (!p->activated) {
 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
 		    "not-yet-activated pcap_t passed to pcap_compile");
-		return (-1);
+		rc = -1;
+		goto quit;
 	}
 	no_optimize = 0;
 	n_errors = 0;
 	root = NULL;
 	bpf_pcap = p;
 	init_regs();
+
 	if (setjmp(top_ctx)) {
 #ifdef INET6
 		if (ai != NULL) {
@@ -454,7 +475,8 @@
 #endif
 		lex_cleanup();
 		freechunks();
-		return (-1);
+		rc = -1;
+		goto quit;
 	}
 
 	netmask = mask;
@@ -463,7 +485,8 @@
 	if (snaplen == 0) {
 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
 			 "snaplen of 0 rejects all packets");
-		return -1;
+		rc = -1;
+		goto quit;
 	}
 
 	lex_init(xbuf ? xbuf : "");
@@ -487,7 +510,16 @@
 
 	lex_cleanup();
 	freechunks();
-	return (0);
+
+	rc = 0;  /* We're all okay */
+
+quit:
+
+#ifdef WIN32
+	LeaveCriticalSection(&g_PcapCompileCriticalSection);
+#endif
+
+	return (rc);
 }
 
 /*
@@ -592,7 +624,7 @@
 	 * worth the effort.
 	 */
 	insert_compute_vloffsets(p->head);
-	
+
 	/*
 	 * For DLT_PPI captures, generate a check of the per-packet
 	 * DLT value to make sure it's DLT_IEEE802_11.
@@ -764,74 +796,62 @@
 }
 
 /*
- * Various code constructs need to know the layout of the data link
- * layer.  These variables give the necessary offsets from the beginning
+ * Various code constructs need to know the layout of the packet.
+ * These variables give the necessary offsets from the beginning
  * of the packet data.
  */
 
 /*
- * This is the offset of the beginning of the link-layer header from
- * the beginning of the raw packet data.
- *
- * It's usually 0, except for 802.11 with a fixed-length radio header.
- * (For 802.11 with a variable-length radio header, we have to generate
- * code to compute that offset; off_ll is 0 in that case.)
+ * Absolute offset of the beginning of the link-layer header.
  */
-static u_int off_ll;
+static bpf_abs_offset off_linkhdr;
 
 /*
- * If there's a variable-length header preceding the link-layer header,
- * "reg_off_ll" is the register number for a register containing the
- * length of that header, and therefore the offset of the link-layer
- * header from the beginning of the raw packet data.  Otherwise,
- * "reg_off_ll" is -1.
+ * If we're checking a link-layer header for a packet encapsulated in
+ * another protocol layer, this is the equivalent information for the
+ * previous layers' link-layer header from the beginning of the raw
+ * packet data.
  */
-static int reg_off_ll;
+static bpf_abs_offset off_prevlinkhdr;
 
 /*
- * This is the offset of the beginning of the MAC-layer header from
- * the beginning of the link-layer header.
- * It's usually 0, except for ATM LANE, where it's the offset, relative
- * to the beginning of the raw packet data, of the Ethernet header, and
- * for Ethernet with various additional information.
+ * This is the equivalent information for the outermost layers' link-layer
+ * header.
  */
-static u_int off_mac;
+static bpf_abs_offset off_outermostlinkhdr;
 
 /*
- * This is the offset of the beginning of the MAC-layer payload,
- * from the beginning of the raw packet data.
- *
- * I.e., it's the sum of the length of the link-layer header (without,
- * for example, any 802.2 LLC header, so it's the MAC-layer
- * portion of that header), plus any prefix preceding the
- * link-layer header.
+ * "Push" the current value of the link-layer header type and link-layer
+ * header offset onto a "stack", and set a new value.  (It's not a
+ * full-blown stack; we keep only the top two items.)
  */
-static u_int off_macpl;
+#define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
+{ \
+	prevlinktype = new_linktype; \
+	off_prevlinkhdr = off_linkhdr; \
+	linktype = new_linktype; \
+	off_linkhdr.is_variable = new_is_variable; \
+	off_linkhdr.constant_part = new_constant_part; \
+	off_linkhdr.reg = new_reg; \
+	is_geneve = 0; \
+}
 
 /*
- * This is 1 if the offset of the beginning of the MAC-layer payload
- * from the beginning of the link-layer header is variable-length.
+ * Absolute offset of the beginning of the link-layer payload.
  */
-static int off_macpl_is_variable;
-
-/*
- * If the link layer has variable_length headers, "reg_off_macpl"
- * is the register number for a register containing the length of the
- * link-layer header plus the length of any variable-length header
- * preceding the link-layer header.  Otherwise, "reg_off_macpl"
- * is -1.
- */
-static int reg_off_macpl;
+static bpf_abs_offset off_linkpl;
 
 /*
  * "off_linktype" is the offset to information in the link-layer header
- * giving the packet type.  This offset is relative to the beginning
- * of the link-layer header (i.e., it doesn't include off_ll).
+ * giving the packet type. This is an absolute offset from the beginning
+ * of the packet.
  *
- * For Ethernet, it's the offset of the Ethernet type field.
+ * For Ethernet, it's the offset of the Ethernet type field; this
+ * means that it must have a value that skips VLAN tags.
  *
  * For link-layer types that always use 802.2 headers, it's the
- * offset of the LLC header.
+ * offset of the LLC header; this means that it must have a value
+ * that skips VLAN tags.
  *
  * For PPP, it's the offset of the PPP type field.
  *
@@ -841,16 +861,10 @@
  *
  * For Linux cooked sockets, it's the offset of the type field.
  *
- * It's set to -1 for no encapsulation, in which case, IP is assumed.
+ * off_linktype.constant_part is set to -1 for no encapsulation,
+ * in which case, IP is assumed.
  */
-static u_int off_linktype;
-
-/*
- * TRUE if "pppoes" appeared in the filter; it causes link-layer type
- * checks to check the PPP header, assumed to follow a LAN-style link-
- * layer header and a PPPoE session header.
- */
-static int is_pppoes = 0;
+static bpf_abs_offset off_linktype;
 
 /*
  * TRUE if the link layer includes an ATM pseudo-header.
@@ -858,10 +872,11 @@
 static int is_atm = 0;
 
 /*
- * TRUE if "lane" appeared in the filter; it causes us to generate
- * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
+ * TRUE if "geneve" appeared in the filter; it causes us to generate
+ * code that checks for a Geneve header and assume that later filters
+ * apply to the encapsulated payload.
  */
-static int is_lane = 0;
+static int is_geneve = 0;
 
 /*
  * These are offsets for the ATM pseudo-header.
@@ -892,8 +907,8 @@
 
 /*
  * These are offsets to the beginning of the network-layer header.
- * They are relative to the beginning of the MAC-layer payload (i.e.,
- * they don't include off_ll or off_macpl).
+ * They are relative to the beginning of the link-layer payload (i.e.,
+ * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
  *
  * If the link layer never uses 802.2 LLC:
  *
@@ -918,29 +933,57 @@
 static u_int off_nl_nosnap;
 
 static int linktype;
+static int prevlinktype;
+static int outermostlinktype;
 
 static void
 init_linktype(p)
 	pcap_t *p;
 {
-	linktype = pcap_datalink(p);
 	pcap_fddipad = p->fddipad;
 
 	/*
+	 * We start out with only one link-layer header.
+	 */
+	outermostlinktype = pcap_datalink(p);
+	off_outermostlinkhdr.constant_part = 0;
+	off_outermostlinkhdr.is_variable = 0;
+	off_outermostlinkhdr.reg = -1;
+
+	prevlinktype = outermostlinktype;
+	off_prevlinkhdr.constant_part = 0;
+	off_prevlinkhdr.is_variable = 0;
+	off_prevlinkhdr.reg = -1;
+
+	linktype = outermostlinktype;
+	off_linkhdr.constant_part = 0;
+	off_linkhdr.is_variable = 0;
+	off_linkhdr.reg = -1;
+
+	/*
+	 * XXX
+	 */
+	off_linkpl.constant_part = 0;
+	off_linkpl.is_variable = 0;
+	off_linkpl.reg = -1;
+
+	off_linktype.constant_part = 0;
+	off_linktype.is_variable = 0;
+	off_linktype.reg = -1;
+
+	/*
 	 * Assume it's not raw ATM with a pseudo-header, for now.
 	 */
-	off_mac = 0;
 	is_atm = 0;
-	is_lane = 0;
 	off_vpi = -1;
 	off_vci = -1;
 	off_proto = -1;
 	off_payload = -1;
 
 	/*
-	 * And that we're not doing PPPoE.
+	 * And not Geneve.
 	 */
-	is_pppoes = 0;
+	is_geneve = 0;
 
 	/*
 	 * And assume we're not doing SS7.
@@ -952,105 +995,94 @@
 	off_dpc = -1;
 	off_sls = -1;
 
-	/*
-	 * Also assume it's not 802.11.
-	 */
-	off_ll = 0;
-	off_macpl = 0;
-	off_macpl_is_variable = 0;
-
-	orig_linktype = -1;
-	orig_nl = -1;
         label_stack_depth = 0;
-
-	reg_off_ll = -1;
-	reg_off_macpl = -1;
+        vlan_stack_depth = 0;
 
 	switch (linktype) {
 
 	case DLT_ARCNET:
-		off_linktype = 2;
-		off_macpl = 6;
+		off_linktype.constant_part = 2;
+		off_linkpl.constant_part = 6;
 		off_nl = 0;		/* XXX in reality, variable! */
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_ARCNET_LINUX:
-		off_linktype = 4;
-		off_macpl = 8;
+		off_linktype.constant_part = 4;
+		off_linkpl.constant_part = 8;
 		off_nl = 0;		/* XXX in reality, variable! */
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_EN10MB:
-		off_linktype = 12;
-		off_macpl = 14;		/* Ethernet header length */
+		off_linktype.constant_part = 12;
+		off_linkpl.constant_part = 14;	/* Ethernet header length */
 		off_nl = 0;		/* Ethernet II */
 		off_nl_nosnap = 3;	/* 802.3+802.2 */
-		return;
+		break;
 
 	case DLT_SLIP:
 		/*
 		 * SLIP doesn't have a link level type.  The 16 byte
 		 * header is hacked into our SLIP driver.
 		 */
-		off_linktype = -1;
-		off_macpl = 16;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = 16;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_SLIP_BSDOS:
 		/* XXX this may be the same as the DLT_PPP_BSDOS case */
-		off_linktype = -1;
+		off_linktype.constant_part = -1;
 		/* XXX end */
-		off_macpl = 24;
+		off_linkpl.constant_part = 24;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_NULL:
 	case DLT_LOOP:
-		off_linktype = 0;
-		off_macpl = 4;
+		off_linktype.constant_part = 0;
+		off_linkpl.constant_part = 4;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_ENC:
-		off_linktype = 0;
-		off_macpl = 12;
+		off_linktype.constant_part = 0;
+		off_linkpl.constant_part = 12;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_PPP:
 	case DLT_PPP_PPPD:
 	case DLT_C_HDLC:		/* BSD/OS Cisco HDLC */
 	case DLT_PPP_SERIAL:		/* NetBSD sync/async serial PPP */
-		off_linktype = 2;
-		off_macpl = 4;
+		off_linktype.constant_part = 2;	/* skip HDLC-like framing */
+		off_linkpl.constant_part = 4;	/* skip HDLC-like framing and protocol field */
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_PPP_ETHER:
 		/*
 		 * This does no include the Ethernet header, and
 		 * only covers session state.
 		 */
-		off_linktype = 6;
-		off_macpl = 8;
+		off_linktype.constant_part = 6;
+		off_linkpl.constant_part = 8;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_PPP_BSDOS:
-		off_linktype = 5;
-		off_macpl = 24;
+		off_linktype.constant_part = 5;
+		off_linkpl.constant_part = 24;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_FDDI:
 		/*
@@ -1061,13 +1093,13 @@
 		 * is being used and pick out the encapsulated Ethernet type.
 		 * XXX - should we generate code to check for SNAP?
 		 */
-		off_linktype = 13;
-		off_linktype += pcap_fddipad;
-		off_macpl = 13;		/* FDDI MAC header length */
-		off_macpl += pcap_fddipad;
+		off_linktype.constant_part = 13;
+		off_linktype.constant_part += pcap_fddipad;
+		off_linkpl.constant_part = 13;	/* FDDI MAC header length */
+		off_linkpl.constant_part += pcap_fddipad;
 		off_nl = 8;		/* 802.2+SNAP */
 		off_nl_nosnap = 3;	/* 802.2 */
-		return;
+		break;
 
 	case DLT_IEEE802:
 		/*
@@ -1093,19 +1125,24 @@
 		 * the 16-bit value at an offset of 14 (shifted right
 		 * 8 - figure out which byte that is).
 		 */
-		off_linktype = 14;
-		off_macpl = 14;		/* Token Ring MAC header length */
+		off_linktype.constant_part = 14;
+		off_linkpl.constant_part = 14;	/* Token Ring MAC header length */
 		off_nl = 8;		/* 802.2+SNAP */
 		off_nl_nosnap = 3;	/* 802.2 */
-		return;
+		break;
 
-	case DLT_IEEE802_11:
 	case DLT_PRISM_HEADER:
 	case DLT_IEEE802_11_RADIO_AVS:
 	case DLT_IEEE802_11_RADIO:
+		off_linkhdr.is_variable = 1;
+		/* Fall through, 802.11 doesn't have a variable link
+		 * prefix but is otherwise the same. */
+
+	case DLT_IEEE802_11:
 		/*
 		 * 802.11 doesn't really have a link-level type field.
-		 * We set "off_linktype" to the offset of the LLC header.
+		 * We set "off_linktype.constant_part" to the offset of
+		 * the LLC header.
 		 *
 		 * To check for Ethernet types, we assume that SSAP = SNAP
 		 * is being used and pick out the encapsulated Ethernet type.
@@ -1120,15 +1157,15 @@
 		 * header or an AVS header, so, in practice, it's
 		 * variable-length.
 		 */
-		off_linktype = 24;
-		off_macpl = 0;		/* link-layer header is variable-length */
-		off_macpl_is_variable = 1;
+		off_linktype.constant_part = 24;
+		off_linkpl.constant_part = 0;	/* link-layer header is variable-length */
+		off_linkpl.is_variable = 1;
 		off_nl = 8;		/* 802.2+SNAP */
 		off_nl_nosnap = 3;	/* 802.2 */
-		return;
+		break;
 
 	case DLT_PPI:
-		/* 
+		/*
 		 * At the moment we treat PPI the same way that we treat
 		 * normal Radiotap encoded packets. The difference is in
 		 * the function that generates the code at the beginning
@@ -1137,12 +1174,13 @@
 		 * the encapsulated DLT should be DLT_IEEE802_11) we
 		 * generate code to check for this too.
 		 */
-		off_linktype = 24;
-		off_macpl = 0;		/* link-layer header is variable-length */
-		off_macpl_is_variable = 1;
+		off_linktype.constant_part = 24;
+		off_linkpl.constant_part = 0;	/* link-layer header is variable-length */
+		off_linkpl.is_variable = 1;
+		off_linkhdr.is_variable = 1;
 		off_nl = 8;		/* 802.2+SNAP */
 		off_nl_nosnap = 3;	/* 802.2 */
-		return;
+		break;
 
 	case DLT_ATM_RFC1483:
 	case DLT_ATM_CLIP:	/* Linux ATM defines this */
@@ -1157,11 +1195,11 @@
 		 * or "pppoa and tcp port 80" and have it check for
 		 * PPPo{A,E} and a PPP protocol of IP and....
 		 */
-		off_linktype = 0;
-		off_macpl = 0;		/* packet begins with LLC header */
+		off_linktype.constant_part = 0;
+		off_linkpl.constant_part = 0;	/* packet begins with LLC header */
 		off_nl = 8;		/* 802.2+SNAP */
 		off_nl_nosnap = 3;	/* 802.2 */
-		return;
+		break;
 
 	case DLT_SUNATM:
 		/*
@@ -1172,29 +1210,28 @@
 		off_vpi = SUNATM_VPI_POS;
 		off_vci = SUNATM_VCI_POS;
 		off_proto = PROTO_POS;
-		off_mac = -1;	/* assume LLC-encapsulated, so no MAC-layer header */
 		off_payload = SUNATM_PKT_BEGIN_POS;
-		off_linktype = off_payload;
-		off_macpl = off_payload;	/* if LLC-encapsulated */
+		off_linktype.constant_part = off_payload;
+		off_linkpl.constant_part = off_payload;	/* if LLC-encapsulated */
 		off_nl = 8;		/* 802.2+SNAP */
 		off_nl_nosnap = 3;	/* 802.2 */
-		return;
+		break;
 
 	case DLT_RAW:
 	case DLT_IPV4:
 	case DLT_IPV6:
-		off_linktype = -1;
-		off_macpl = 0;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = 0;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_LINUX_SLL:	/* fake header for Linux cooked socket */
-		off_linktype = 14;
-		off_macpl = 16;
+		off_linktype.constant_part = 14;
+		off_linkpl.constant_part = 16;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_LTALK:
 		/*
@@ -1202,11 +1239,11 @@
 		 * but really it just indicates whether there is a "short" or
 		 * "long" DDP packet following.
 		 */
-		off_linktype = -1;
-		off_macpl = 0;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = 0;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_IP_OVER_FC:
 		/*
@@ -1219,22 +1256,22 @@
 		 * XXX - should we generate code to check for SNAP? RFC
 		 * 2625 says SNAP should be used.
 		 */
-		off_linktype = 16;
-		off_macpl = 16;
+		off_linktype.constant_part = 16;
+		off_linkpl.constant_part = 16;
 		off_nl = 8;		/* 802.2+SNAP */
 		off_nl_nosnap = 3;	/* 802.2 */
-		return;
+		break;
 
 	case DLT_FRELAY:
 		/*
 		 * XXX - we should set this to handle SNAP-encapsulated
 		 * frames (NLPID of 0x80).
 		 */
-		off_linktype = -1;
-		off_macpl = 0;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = 0;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
                 /*
                  * the only BPF-interesting FRF.16 frames are non-control frames;
@@ -1242,33 +1279,33 @@
                  * so lets start with offset 4 for now and increments later on (FIXME);
                  */
 	case DLT_MFR:
-		off_linktype = -1;
-		off_macpl = 0;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = 0;
 		off_nl = 4;
 		off_nl_nosnap = 0;	/* XXX - for now -> no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_APPLE_IP_OVER_IEEE1394:
-		off_linktype = 16;
-		off_macpl = 18;
+		off_linktype.constant_part = 16;
+		off_linkpl.constant_part = 18;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_SYMANTEC_FIREWALL:
-		off_linktype = 6;
-		off_macpl = 44;
+		off_linktype.constant_part = 6;
+		off_linkpl.constant_part = 44;
 		off_nl = 0;		/* Ethernet II */
 		off_nl_nosnap = 0;	/* XXX - what does it do with 802.3 packets? */
-		return;
+		break;
 
 #ifdef HAVE_NET_PFVAR_H
 	case DLT_PFLOG:
-		off_linktype = 0;
-		off_macpl = PFLOG_HDRLEN;
+		off_linktype.constant_part = 0;
+		off_linkpl.constant_part = PFLOG_HDRLEN;
 		off_nl = 0;
 		off_nl_nosnap = 0;	/* no 802.2 LLC */
-		return;
+		break;
 #endif
 
         case DLT_JUNIPER_MFR:
@@ -1277,108 +1314,108 @@
         case DLT_JUNIPER_PPP:
         case DLT_JUNIPER_CHDLC:
         case DLT_JUNIPER_FRELAY:
-                off_linktype = 4;
-		off_macpl = 4;
+                off_linktype.constant_part = 4;
+		off_linkpl.constant_part = 4;
 		off_nl = 0;
 		off_nl_nosnap = -1;	/* no 802.2 LLC */
-                return;
+                break;
 
 	case DLT_JUNIPER_ATM1:
-		off_linktype = 4;	/* in reality variable between 4-8 */
-		off_macpl = 4;	/* in reality variable between 4-8 */
+		off_linktype.constant_part = 4;		/* in reality variable between 4-8 */
+		off_linkpl.constant_part = 4;	/* in reality variable between 4-8 */
 		off_nl = 0;
 		off_nl_nosnap = 10;
-		return;
+		break;
 
 	case DLT_JUNIPER_ATM2:
-		off_linktype = 8;	/* in reality variable between 8-12 */
-		off_macpl = 8;	/* in reality variable between 8-12 */
+		off_linktype.constant_part = 8;		/* in reality variable between 8-12 */
+		off_linkpl.constant_part = 8;	/* in reality variable between 8-12 */
 		off_nl = 0;
 		off_nl_nosnap = 10;
-		return;
+		break;
 
 		/* frames captured on a Juniper PPPoE service PIC
 		 * contain raw ethernet frames */
 	case DLT_JUNIPER_PPPOE:
         case DLT_JUNIPER_ETHER:
-        	off_macpl = 14;
-		off_linktype = 16;
+        	off_linkpl.constant_part = 14;
+		off_linktype.constant_part = 16;
 		off_nl = 18;		/* Ethernet II */
 		off_nl_nosnap = 21;	/* 802.3+802.2 */
-		return;
+		break;
 
 	case DLT_JUNIPER_PPPOE_ATM:
-		off_linktype = 4;
-		off_macpl = 6;
+		off_linktype.constant_part = 4;
+		off_linkpl.constant_part = 6;
 		off_nl = 0;
 		off_nl_nosnap = -1;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_JUNIPER_GGSN:
-		off_linktype = 6;
-		off_macpl = 12;
+		off_linktype.constant_part = 6;
+		off_linkpl.constant_part = 12;
 		off_nl = 0;
 		off_nl_nosnap = -1;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_JUNIPER_ES:
-		off_linktype = 6;
-		off_macpl = -1;		/* not really a network layer but raw IP addresses */
+		off_linktype.constant_part = 6;
+		off_linkpl.constant_part = -1;	/* not really a network layer but raw IP addresses */
 		off_nl = -1;		/* not really a network layer but raw IP addresses */
 		off_nl_nosnap = -1;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_JUNIPER_MONITOR:
-		off_linktype = 12;
-		off_macpl = 12;
+		off_linktype.constant_part = 12;
+		off_linkpl.constant_part = 12;
 		off_nl = 0;		/* raw IP/IP6 header */
 		off_nl_nosnap = -1;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_BACNET_MS_TP:
-		off_linktype = -1;
-		off_macpl = -1;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_JUNIPER_SERVICES:
-		off_linktype = 12;
-		off_macpl = -1;		/* L3 proto location dep. on cookie type */
+		off_linktype.constant_part = 12;
+		off_linkpl.constant_part = -1;	/* L3 proto location dep. on cookie type */
 		off_nl = -1;		/* L3 proto location dep. on cookie type */
 		off_nl_nosnap = -1;	/* no 802.2 LLC */
-		return;
+		break;
 
 	case DLT_JUNIPER_VP:
-		off_linktype = 18;
-		off_macpl = -1;
+		off_linktype.constant_part = 18;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_JUNIPER_ST:
-		off_linktype = 18;
-		off_macpl = -1;
+		off_linktype.constant_part = 18;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_JUNIPER_ISM:
-		off_linktype = 8;
-		off_macpl = -1;
+		off_linktype.constant_part = 8;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_JUNIPER_VS:
 	case DLT_JUNIPER_SRX_E2E:
 	case DLT_JUNIPER_FIBRECHANNEL:
 	case DLT_JUNIPER_ATM_CEMIC:
-		off_linktype = 8;
-		off_macpl = -1;
+		off_linktype.constant_part = 8;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_MTP2:
 		off_li = 2;
@@ -1387,11 +1424,11 @@
 		off_opc = 4;
 		off_dpc = 4;
 		off_sls = 7;
-		off_linktype = -1;
-		off_macpl = -1;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_MTP2_WITH_PHDR:
 		off_li = 6;
@@ -1400,11 +1437,11 @@
 		off_opc = 8;
 		off_dpc = 8;
 		off_sls = 11;
-		off_linktype = -1;
-		off_macpl = -1;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_ERF:
 		off_li = 22;
@@ -1413,52 +1450,51 @@
 		off_opc = 24;
 		off_dpc = 24;
 		off_sls = 27;
-		off_linktype = -1;
-		off_macpl = -1;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = -1;
 		off_nl = -1;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_PFSYNC:
-		off_linktype = -1;
-		off_macpl = 4;
+		off_linktype.constant_part = -1;
+		off_linkpl.constant_part = 4;
 		off_nl = 0;
 		off_nl_nosnap = 0;
-		return;
+		break;
 
 	case DLT_AX25_KISS:
 		/*
 		 * Currently, only raw "link[N:M]" filtering is supported.
 		 */
-		off_linktype = -1;	/* variable, min 15, max 71 steps of 7 */
-		off_macpl = -1;
+		off_linktype.constant_part = -1;	/* variable, min 15, max 71 steps of 7 */
+		off_linkpl.constant_part = -1;
 		off_nl = -1;		/* variable, min 16, max 71 steps of 7 */
 		off_nl_nosnap = -1;	/* no 802.2 LLC */
-		off_mac = 1;		/* step over the kiss length byte */
-		return;
+		break;
 
 	case DLT_IPNET:
-		off_linktype = 1;
-		off_macpl = 24;		/* ipnet header length */
+		off_linktype.constant_part = 1;
+		off_linkpl.constant_part = 24;	/* ipnet header length */
 		off_nl = 0;
 		off_nl_nosnap = -1;
-		return;
+		break;
 
 	case DLT_NETANALYZER:
-		off_mac = 4;		/* MAC header is past 4-byte pseudo-header */
-		off_linktype = 16;	/* includes 4-byte pseudo-header */
-		off_macpl = 18;		/* pseudo-header+Ethernet header length */
+		off_linkhdr.constant_part = 4;	/* Ethernet header is past 4-byte pseudo-header */
+		off_linktype.constant_part = off_linkhdr.constant_part + 12;
+		off_linkpl.constant_part = off_linkhdr.constant_part + 14;	/* pseudo-header+Ethernet header length */
 		off_nl = 0;		/* Ethernet II */
 		off_nl_nosnap = 3;	/* 802.3+802.2 */
-		return;
+		break;
 
 	case DLT_NETANALYZER_TRANSPARENT:
-		off_mac = 12;		/* MAC header is past 4-byte pseudo-header, preamble, and SFD */
-		off_linktype = 24;	/* includes 4-byte pseudo-header+preamble+SFD */
-		off_macpl = 26;		/* pseudo-header+preamble+SFD+Ethernet header length */
+		off_linkhdr.constant_part = 12;	/* MAC header is past 4-byte pseudo-header, preamble, and SFD */
+		off_linktype.constant_part = off_linkhdr.constant_part + 12;
+		off_linkpl.constant_part = off_linkhdr.constant_part + 14;	/* pseudo-header+preamble+SFD+Ethernet header length */
 		off_nl = 0;		/* Ethernet II */
 		off_nl_nosnap = 3;	/* 802.3+802.2 */
-		return;
+		break;
 
 	default:
 		/*
@@ -1467,99 +1503,54 @@
 		 */
 		if (linktype >= DLT_MATCHING_MIN &&
 		    linktype <= DLT_MATCHING_MAX) {
-			off_linktype = -1;
-			off_macpl = -1;
+			off_linktype.constant_part = -1;
+			off_linkpl.constant_part = -1;
 			off_nl = -1;
 			off_nl_nosnap = -1;
-			return;
+		} else {
+			bpf_error("unknown data link type %d", linktype);
 		}
-
+		break;
 	}
-	bpf_error("unknown data link type %d", linktype);
-	/* NOTREACHED */
+
+	off_outermostlinkhdr = off_prevlinkhdr = off_linkhdr;
 }
 
 /*
- * Load a value relative to the beginning of the link-layer header.
- * The link-layer header doesn't necessarily begin at the beginning
- * of the packet data; there might be a variable-length prefix containing
- * radio information.
+ * Load a value relative to the specified absolute offset.
  */
 static struct slist *
-gen_load_llrel(offset, size)
-	u_int offset, size;
+gen_load_absoffsetrel(bpf_abs_offset *abs_offset, u_int offset, u_int size)
 {
 	struct slist *s, *s2;
 
-	s = gen_llprefixlen();
+	s = gen_abs_offset_varpart(abs_offset);
 
 	/*
 	 * If "s" is non-null, it has code to arrange that the X register
-	 * contains the length of the prefix preceding the link-layer
-	 * header.
+	 * contains the variable part of the absolute offset, so we
+	 * generate a load relative to that, with an offset of
+	 * abs_offset->constant_part + offset.
 	 *
-	 * Otherwise, the length of the prefix preceding the link-layer
-	 * header is "off_ll".
+	 * Otherwise, we can do an absolute load with an offset of
+	 * abs_offset->constant_part + offset.
 	 */
 	if (s != NULL) {
 		/*
-		 * There's a variable-length prefix preceding the
-		 * link-layer header.  "s" points to a list of statements
-		 * that put the length of that prefix into the X register.
-		 * do an indirect load, to use the X register as an offset.
+		 * "s" points to a list of statements that puts the
+		 * variable part of the absolute offset into the X register.
+		 * Do an indirect load, to use the X register as an offset.
 		 */
 		s2 = new_stmt(BPF_LD|BPF_IND|size);
-		s2->s.k = offset;
+		s2->s.k = abs_offset->constant_part + offset;
 		sappend(s, s2);
 	} else {
 		/*
-		 * There is no variable-length header preceding the
-		 * link-layer header; add in off_ll, which, if there's
-		 * a fixed-length header preceding the link-layer header,
-		 * is the length of that header.
+		 * There is no variable part of the absolute offset, so
+		 * just do an absolute load.
 		 */
 		s = new_stmt(BPF_LD|BPF_ABS|size);
-		s->s.k = offset + off_ll;
-	}
-	return s;
-}
-
-/*
- * Load a value relative to the beginning of the MAC-layer payload.
- */
-static struct slist *
-gen_load_macplrel(offset, size)
-	u_int offset, size;
-{
-	struct slist *s, *s2;
-
-	s = gen_off_macpl();
-
-	/*
-	 * If s is non-null, the offset of the MAC-layer payload is
-	 * variable, and s points to a list of instructions that
-	 * arrange that the X register contains that offset.
-	 *
-	 * Otherwise, the offset of the MAC-layer payload is constant,
-	 * and is in off_macpl.
-	 */
-	if (s != NULL) {
-		/*
-		 * The offset of the MAC-layer payload is in the X
-		 * register.  Do an indirect load, to use the X register
-		 * as an offset.
-		 */
-		s2 = new_stmt(BPF_LD|BPF_IND|size);
-		s2->s.k = offset;
-		sappend(s, s2);
-	} else {
-		/*
-		 * The offset of the MAC-layer payload is constant,
-		 * and is in off_macpl; load the value at that offset
-		 * plus the specified offset.
-		 */
-		s = new_stmt(BPF_LD|BPF_ABS|size);
-		s->s.k = off_macpl + offset;
+		s->s.k = abs_offset->constant_part + offset;
 	}
 	return s;
 }
@@ -1581,20 +1572,32 @@
                 s->s.k = offset;
 		break;
 
-	case OR_LINK:
-		s = gen_load_llrel(offset, size);
+	case OR_LINKHDR:
+		s = gen_load_absoffsetrel(&off_linkhdr, offset, size);
 		break;
 
-	case OR_MACPL:
-		s = gen_load_macplrel(offset, size);
+	case OR_PREVLINKHDR:
+		s = gen_load_absoffsetrel(&off_prevlinkhdr, offset, size);
 		break;
 
-	case OR_NET:
-		s = gen_load_macplrel(off_nl + offset, size);
+	case OR_LLC:
+		s = gen_load_absoffsetrel(&off_linkpl, offset, size);
 		break;
 
-	case OR_NET_NOSNAP:
-		s = gen_load_macplrel(off_nl_nosnap + offset, size);
+	case OR_PREVMPLSHDR:
+		s = gen_load_absoffsetrel(&off_linkpl, off_nl - 4 + offset, size);
+		break;
+
+	case OR_LINKPL:
+		s = gen_load_absoffsetrel(&off_linkpl, off_nl + offset, size);
+		break;
+
+	case OR_LINKPL_NOSNAP:
+		s = gen_load_absoffsetrel(&off_linkpl, off_nl_nosnap + offset, size);
+		break;
+
+	case OR_LINKTYPE:
+		s = gen_load_absoffsetrel(&off_linktype, offset, size);
 		break;
 
 	case OR_TRAN_IPV4:
@@ -1607,22 +1610,23 @@
 		s = gen_loadx_iphdrlen();
 
 		/*
-		 * Load the item at {offset of the MAC-layer payload} +
-		 * {offset, relative to the start of the MAC-layer
+		 * Load the item at {offset of the link-layer payload} +
+		 * {offset, relative to the start of the link-layer
 		 * paylod, of the IPv4 header} + {length of the IPv4 header} +
 		 * {specified offset}.
 		 *
-		 * (If the offset of the MAC-layer payload is variable,
-		 * it's included in the value in the X register, and
-		 * off_macpl is 0.)
+		 * If the offset of the link-layer payload is variable,
+		 * the variable part of that offset is included in the
+		 * value in the X register, and we include the constant
+		 * part in the offset of the load.
 		 */
 		s2 = new_stmt(BPF_LD|BPF_IND|size);
-		s2->s.k = off_macpl + off_nl + offset;
+		s2->s.k = off_linkpl.constant_part + off_nl + offset;
 		sappend(s, s2);
 		break;
 
 	case OR_TRAN_IPV6:
-		s = gen_load_macplrel(off_nl + 40 + offset, size);
+		s = gen_load_absoffsetrel(&off_linkpl, off_nl + 40 + offset, size);
 		break;
 
 	default:
@@ -1634,22 +1638,20 @@
 
 /*
  * Generate code to load into the X register the sum of the length of
- * the IPv4 header and any variable-length header preceding the link-layer
- * header.
+ * the IPv4 header and the variable part of the offset of the link-layer
+ * payload.
  */
 static struct slist *
 gen_loadx_iphdrlen()
 {
 	struct slist *s, *s2;
 
-	s = gen_off_macpl();
+	s = gen_abs_offset_varpart(&off_linkpl);
 	if (s != NULL) {
 		/*
-		 * There's a variable-length prefix preceding the
-		 * link-layer header, or the link-layer header is itself
-		 * variable-length.  "s" points to a list of statements
-		 * that put the offset of the MAC-layer payload into
-		 * the X register.
+		 * The offset of the link-layer payload has a variable
+		 * part.  "s" points to a list of statements that put
+		 * the variable part of that offset into the X register.
 		 *
 		 * The 4*([k]&0xf) addressing mode can't be used, as we
 		 * don't have a constant offset, so we have to load the
@@ -1657,7 +1659,7 @@
 		 * the value from the X register.
 		 */
 		s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
-		s2->s.k = off_nl;
+		s2->s.k = off_linkpl.constant_part + off_nl;
 		sappend(s, s2);
 		s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
 		s2->s.k = 0xf;
@@ -1667,25 +1669,28 @@
 		sappend(s, s2);
 
 		/*
-		 * The A register now contains the length of the
-		 * IP header.  We need to add to it the offset of
-		 * the MAC-layer payload, which is still in the X
+		 * The A register now contains the length of the IP header.
+		 * We need to add to it the variable part of the offset of
+		 * the link-layer payload, which is still in the X
 		 * register, and move the result into the X register.
 		 */
 		sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
 		sappend(s, new_stmt(BPF_MISC|BPF_TAX));
 	} else {
 		/*
-		 * There is no variable-length header preceding the
-		 * link-layer header, and the link-layer header is
-		 * fixed-length; load the length of the IPv4 header,
-		 * which is at an offset of off_nl from the beginning
-		 * of the MAC-layer payload, and thus at an offset
-		 * of off_mac_pl + off_nl from the beginning of the
-		 * raw packet data.
+		 * The offset of the link-layer payload is a constant,
+		 * so no code was generated to load the (non-existent)
+		 * variable part of that offset.
+		 *
+		 * This means we can use the 4*([k]&0xf) addressing
+		 * mode.  Load the length of the IPv4 header, which
+		 * is at an offset of off_nl from the beginning of
+		 * the link-layer payload, and thus at an offset of
+		 * off_linkpl.constant_part + off_nl from the beginning
+		 * of the raw packet data, using that addressing mode.
 		 */
 		s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
-		s->s.k = off_macpl + off_nl;
+		s->s.k = off_linkpl.constant_part + off_nl;
 	}
 	return s;
 }
@@ -1757,9 +1762,9 @@
 		 * DSAP, as we do for other types <= ETHERMTU
 		 * (i.e., other SAP values)?
 		 */
-		b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
+		b0 = gen_cmp_gt(OR_LINKTYPE, 0, BPF_H, ETHERMTU);
 		gen_not(b0);
-		b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
+		b1 = gen_cmp(OR_LLC, 0, BPF_H, (bpf_int32)
 			     ((proto << 8) | proto));
 		gen_and(b0, b1);
 		return b1;
@@ -1797,8 +1802,8 @@
 		 * This generates code to check both for the
 		 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
 		 */
-		b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
-		b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
+		b0 = gen_cmp(OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
+		b1 = gen_cmp(OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF);
 		gen_or(b0, b1);
 
 		/*
@@ -1812,7 +1817,7 @@
 		 * Now we generate code to check for 802.3
 		 * frames in general.
 		 */
-		b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
+		b0 = gen_cmp_gt(OR_LINKTYPE, 0, BPF_H, ETHERMTU);
 		gen_not(b0);
 
 		/*
@@ -1828,8 +1833,7 @@
 		 * do that before checking for the other frame
 		 * types.
 		 */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
-		    (bpf_int32)ETHERTYPE_IPX);
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX);
 		gen_or(b0, b1);
 		return b1;
 
@@ -1845,7 +1849,7 @@
 		 * we check for an Ethernet type field less than
 		 * 1500, which means it's an 802.3 length field.
 		 */
-		b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
+		b0 = gen_cmp_gt(OR_LINKTYPE, 0, BPF_H, ETHERMTU);
 		gen_not(b0);
 
 		/*
@@ -1870,7 +1874,7 @@
 		 * phase 1?); we just check for the Ethernet
 		 * protocol type.
 		 */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
 
 		gen_or(b0, b1);
 		return b1;
@@ -1885,10 +1889,9 @@
 			 * a length field, <= ETHERMTU) and
 			 * then check the DSAP.
 			 */
-			b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
+			b0 = gen_cmp_gt(OR_LINKTYPE, 0, BPF_H, ETHERMTU);
 			gen_not(b0);
-			b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
-			    (bpf_int32)proto);
+			b1 = gen_cmp(OR_LINKTYPE, 2, BPF_B, (bpf_int32)proto);
 			gen_and(b0, b1);
 			return b1;
 		} else {
@@ -1901,7 +1904,7 @@
 			 * will fail and the frame won't match,
 			 * which is what we want).
 			 */
-			return gen_cmp(OR_LINK, off_linktype, BPF_H,
+			return gen_cmp(OR_LINKTYPE, 0, BPF_H,
 			    (bpf_int32)proto);
 		}
 	}
@@ -1918,12 +1921,11 @@
 	switch (proto) {
 
 	case ETHERTYPE_IP:
-		return gen_cmp(OR_LINK, off_linktype, BPF_B,
-		    (bpf_int32)IPH_AF_INET);
+		return gen_cmp(OR_LINKTYPE, 0, BPF_B, (bpf_int32)IPH_AF_INET);
 		/* NOTREACHED */
 
 	case ETHERTYPE_IPV6:
-		return gen_cmp(OR_LINK, off_linktype, BPF_B,
+		return gen_cmp(OR_LINKTYPE, 0, BPF_B,
 		    (bpf_int32)IPH_AF_INET6);
 		/* NOTREACHED */
 
@@ -1965,8 +1967,8 @@
 		 * DSAP, as we do for other types <= ETHERMTU
 		 * (i.e., other SAP values)?
 		 */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
-		b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
+		b1 = gen_cmp(OR_LLC, 0, BPF_H, (bpf_int32)
 			     ((proto << 8) | proto));
 		gen_and(b0, b1);
 		return b1;
@@ -1997,17 +1999,17 @@
 		 * then put a check for LINUX_SLL_P_802_2 frames
 		 * before it.
 		 */
-		b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
+		b0 = gen_cmp(OR_LLC, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
 		b1 = gen_snap(0x000000, ETHERTYPE_IPX);
 		gen_or(b0, b1);
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
 		gen_and(b0, b1);
 
 		/*
 		 * Now check for 802.3 frames and OR that with
 		 * the previous test.
 		 */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);
 		gen_or(b0, b1);
 
 		/*
@@ -2015,8 +2017,7 @@
 		 * do that before checking for the other frame
 		 * types.
 		 */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
-		    (bpf_int32)ETHERTYPE_IPX);
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)ETHERTYPE_IPX);
 		gen_or(b0, b1);
 		return b1;
 
@@ -2032,7 +2033,7 @@
 		 * we check for the 802.2 protocol type in the
 		 * "Ethernet type" field.
 		 */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
 
 		/*
 		 * 802.2-encapsulated ETHERTYPE_ATALK packets are
@@ -2056,7 +2057,7 @@
 		 * phase 1?); we just check for the Ethernet
 		 * protocol type.
 		 */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
+		b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
 
 		gen_or(b0, b1);
 		return b1;
@@ -2070,9 +2071,8 @@
 			 * in the "Ethernet type" field, and
 			 * then check the DSAP.
 			 */
-			b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
-			    LINUX_SLL_P_802_2);
-			b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
+			b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
+			b1 = gen_cmp(OR_LINKHDR, off_linkpl.constant_part, BPF_B,
 			     (bpf_int32)proto);
 			gen_and(b0, b1);
 			return b1;
@@ -2086,8 +2086,7 @@
 			 * will fail and the frame won't match,
 			 * which is what we want).
 			 */
-			return gen_cmp(OR_LINK, off_linktype, BPF_H,
-			    (bpf_int32)proto);
+			return gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
 		}
 	}
 }
@@ -2125,7 +2124,7 @@
 	 * but no known software generates headers that aren't 144
 	 * bytes long.
 	 */
-	if (reg_off_ll != -1) {
+	if (off_linkhdr.reg != -1) {
 		/*
 		 * Load the cookie.
 		 */
@@ -2187,7 +2186,7 @@
 		 * loading the length of the AVS header.
 		 */
 		s2 = new_stmt(BPF_ST);
-		s2->s.k = reg_off_ll;
+		s2->s.k = off_linkhdr.reg;
 		sappend(s1, s2);
 		sjcommon->s.jf = s2;
 
@@ -2214,7 +2213,7 @@
 	 * generated uses that prefix, so we don't need to generate any
 	 * code to load it.)
 	 */
-	if (reg_off_ll != -1) {
+	if (off_linkhdr.reg != -1) {
 		/*
 		 * The 4 bytes at an offset of 4 from the beginning of
 		 * the AVS header are the length of the AVS header.
@@ -2228,7 +2227,7 @@
 		 * it.
 		 */
 		s2 = new_stmt(BPF_ST);
-		s2->s.k = reg_off_ll;
+		s2->s.k = off_linkhdr.reg;
 		sappend(s1, s2);
 
 		/*
@@ -2254,7 +2253,7 @@
 	 * generated uses that prefix, so we don't need to generate any
 	 * code to load it.)
 	 */
-	if (reg_off_ll != -1) {
+	if (off_linkhdr.reg != -1) {
 		/*
 		 * The 2 bytes at offsets of 2 and 3 from the beginning
 		 * of the radiotap header are the length of the radiotap
@@ -2289,7 +2288,7 @@
 		 * it.
 		 */
 		s2 = new_stmt(BPF_ST);
-		s2->s.k = reg_off_ll;
+		s2->s.k = off_linkhdr.reg;
 		sappend(s1, s2);
 
 		/*
@@ -2303,7 +2302,7 @@
 		return (NULL);
 }
 
-/* 
+/*
  * At the moment we treat PPI as normal Radiotap encoded
  * packets. The difference is in the function that generates
  * the code at the beginning to compute the header length.
@@ -2316,13 +2315,13 @@
 gen_load_ppi_llprefixlen()
 {
 	struct slist *s1, *s2;
-	
+
 	/*
 	 * Generate code to load the length of the radiotap header
 	 * into the register assigned to hold that length, if one has
 	 * been assigned.
 	 */
-	if (reg_off_ll != -1) {
+	if (off_linkhdr.reg != -1) {
 		/*
 		 * The 2 bytes at offsets of 2 and 3 from the beginning
 		 * of the radiotap header are the length of the radiotap
@@ -2357,7 +2356,7 @@
 		 * it.
 		 */
 		s2 = new_stmt(BPF_ST);
-		s2->s.k = reg_off_ll;
+		s2->s.k = off_linkhdr.reg;
 		sappend(s1, s2);
 
 		/*
@@ -2390,10 +2389,10 @@
 	struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
 	struct slist *s_roundup;
 
-	if (reg_off_macpl == -1) {
+	if (off_linkpl.reg == -1) {
 		/*
 		 * No register has been assigned to the offset of
-		 * the MAC-layer payload, which means nobody needs
+		 * the link-layer payload, which means nobody needs
 		 * it; don't bother computing it - just return
 		 * what we already have.
 		 */
@@ -2406,14 +2405,14 @@
 	 * slist of instructions
 	 */
 	no_optimize = 1;
-	
+
 	/*
 	 * If "s" is non-null, it has code to arrange that the X register
 	 * contains the length of the prefix preceding the link-layer
 	 * header.
 	 *
 	 * Otherwise, the length of the prefix preceding the link-layer
-	 * header is "off_ll".
+	 * header is "off_outermostlinkhdr.constant_part".
 	 */
 	if (s == NULL) {
 		/*
@@ -2422,18 +2421,18 @@
 		 *
 		 * Load the length of the fixed-length prefix preceding
 		 * the link-layer header (if any) into the X register,
-		 * and store it in the reg_off_macpl register.
-		 * That length is off_ll.
+		 * and store it in the off_linkpl.reg register.
+		 * That length is off_outermostlinkhdr.constant_part.
 		 */
 		s = new_stmt(BPF_LDX|BPF_IMM);
-		s->s.k = off_ll;
+		s->s.k = off_outermostlinkhdr.constant_part;
 	}
 
 	/*
 	 * The X register contains the offset of the beginning of the
 	 * link-layer header; add 24, which is the minimum length
 	 * of the MAC header for a data frame, to that, and store it
-	 * in reg_off_macpl, and then load the Frame Control field,
+	 * in off_linkpl.reg, and then load the Frame Control field,
 	 * which is at the offset in the X register, with an indexed load.
 	 */
 	s2 = new_stmt(BPF_MISC|BPF_TXA);
@@ -2442,7 +2441,7 @@
 	s2->s.k = 24;
 	sappend(s, s2);
 	s2 = new_stmt(BPF_ST);
-	s2->s.k = reg_off_macpl;
+	s2->s.k = off_linkpl.reg;
 	sappend(s, s2);
 
 	s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
@@ -2457,7 +2456,7 @@
 	sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
 	sjset_data_frame_1->s.k = 0x08;
 	sappend(s, sjset_data_frame_1);
-		
+
 	/*
 	 * If b3 is set, test b2, otherwise go to the first statement of
 	 * the rest of the program.
@@ -2476,21 +2475,21 @@
 	sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
 	sjset_qos->s.k = 0x80;	/* QoS bit */
 	sappend(s, sjset_qos);
-		
+
 	/*
-	 * If it's set, add 2 to reg_off_macpl, to skip the QoS
+	 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
 	 * field.
 	 * Otherwise, go to the first statement of the rest of the
 	 * program.
 	 */
 	sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
-	s2->s.k = reg_off_macpl;
+	s2->s.k = off_linkpl.reg;
 	sappend(s, s2);
 	s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
 	s2->s.k = 2;
 	sappend(s, s2);
 	s2 = new_stmt(BPF_ST);
-	s2->s.k = reg_off_macpl;
+	s2->s.k = off_linkpl.reg;
 	sappend(s, s2);
 
 	/*
@@ -2570,7 +2569,7 @@
 		 * ANDing with ~3.
 		 */
 		s_roundup = new_stmt(BPF_LD|BPF_MEM);
-		s_roundup->s.k = reg_off_macpl;
+		s_roundup->s.k = off_linkpl.reg;
 		sappend(s, s_roundup);
 		s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
 		s2->s.k = 3;
@@ -2579,7 +2578,7 @@
 		s2->s.k = ~3;
 		sappend(s, s2);
 		s2 = new_stmt(BPF_ST);
-		s2->s.k = reg_off_macpl;
+		s2->s.k = off_linkpl.reg;
 		sappend(s, s2);
 
 		sjset_tsft_datapad->s.jt = s_roundup;
@@ -2598,13 +2597,27 @@
 {
 	struct slist *s;
 
+	/* There is an implicit dependency between the link
+	 * payload and link header since the payload computation
+	 * includes the variable part of the header. Therefore,
+	 * if nobody else has allocated a register for the link
+	 * header and we need it, do it now. */
+	if (off_linkpl.reg != -1 && off_linkhdr.is_variable &&
+	    off_linkhdr.reg == -1)
+		off_linkhdr.reg = alloc_reg();
+
 	/*
 	 * For link-layer types that have a variable-length header
 	 * preceding the link-layer header, generate code to load
 	 * the offset of the link-layer header into the register
 	 * assigned to that offset, if any.
+	 *
+	 * XXX - this, and the next switch statement, won't handle
+	 * encapsulation of 802.11 or 802.11+radio information in
+	 * some other protocol stack.  That's significantly more
+	 * complicated.
 	 */
-	switch (linktype) {
+	switch (outermostlinktype) {
 
 	case DLT_PRISM_HEADER:
 		s = gen_load_prism_llprefixlen();
@@ -2629,10 +2642,10 @@
 
 	/*
 	 * For link-layer types that have a variable-length link-layer
-	 * header, generate code to load the offset of the MAC-layer
+	 * header, generate code to load the offset of the link-layer
 	 * payload into the register assigned to that offset, if any.
 	 */
-	switch (linktype) {
+	switch (outermostlinktype) {
 
 	case DLT_IEEE802_11:
 	case DLT_PRISM_HEADER:
@@ -2681,160 +2694,45 @@
 	return b;
 }
 
-static struct slist *
-gen_prism_llprefixlen(void)
-{
-	struct slist *s;
-
-	if (reg_off_ll == -1) {
-		/*
-		 * We haven't yet assigned a register for the length
-		 * of the radio header; allocate one.
-		 */
-		reg_off_ll = alloc_reg();
-	}
-
-	/*
-	 * Load the register containing the radio length
-	 * into the X register.
-	 */
-	s = new_stmt(BPF_LDX|BPF_MEM);
-	s->s.k = reg_off_ll;
-	return s;
-}
-
-static struct slist *
-gen_avs_llprefixlen(void)
-{
-	struct slist *s;
-
-	if (reg_off_ll == -1) {
-		/*
-		 * We haven't yet assigned a register for the length
-		 * of the AVS header; allocate one.
-		 */
-		reg_off_ll = alloc_reg();
-	}
-
-	/*
-	 * Load the register containing the AVS length
-	 * into the X register.
-	 */
-	s = new_stmt(BPF_LDX|BPF_MEM);
-	s->s.k = reg_off_ll;
-	return s;
-}
-
-static struct slist *
-gen_radiotap_llprefixlen(void)
-{
-	struct slist *s;
-
-	if (reg_off_ll == -1) {
-		/*
-		 * We haven't yet assigned a register for the length
-		 * of the radiotap header; allocate one.
-		 */
-		reg_off_ll = alloc_reg();
-	}
-
-	/*
-	 * Load the register containing the radiotap length
-	 * into the X register.
-	 */
-	s = new_stmt(BPF_LDX|BPF_MEM);
-	s->s.k = reg_off_ll;
-	return s;
-}
-
-/* 
- * At the moment we treat PPI as normal Radiotap encoded
- * packets. The difference is in the function that generates
- * the code at the beginning to compute the header length.
- * Since this code generator of PPI supports bare 802.11
- * encapsulation only (i.e. the encapsulated DLT should be
- * DLT_IEEE802_11) we generate code to check for this too.
- */
-static struct slist *
-gen_ppi_llprefixlen(void)
-{
-	struct slist *s;
-
-	if (reg_off_ll == -1) {
-		/*
-		 * We haven't yet assigned a register for the length
-		 * of the radiotap header; allocate one.
-		 */
-		reg_off_ll = alloc_reg();
-	}
-
-	/*
-	 * Load the register containing the PPI length
-	 * into the X register.
-	 */
-	s = new_stmt(BPF_LDX|BPF_MEM);
-	s->s.k = reg_off_ll;
-	return s;
-}
-
 /*
- * Generate code to compute the link-layer header length, if necessary,
- * putting it into the X register, and to return either a pointer to a
- * "struct slist" for the list of statements in that code, or NULL if
- * no code is necessary.
+ * Take an absolute offset, and:
+ *
+ *    if it has no variable part, return NULL;
+ *
+ *    if it has a variable part, generate code to load the register
+ *    containing that variable part into the X register, returning
+ *    a pointer to that code - if no register for that offset has
+ *    been allocated, allocate it first.
+ *
+ * (The code to set that register will be generated later, but will
+ * be placed earlier in the code sequence.)
  */
 static struct slist *
-gen_llprefixlen(void)
-{
-	switch (linktype) {
-
-	case DLT_PRISM_HEADER:
-		return gen_prism_llprefixlen();
-
-	case DLT_IEEE802_11_RADIO_AVS:
-		return gen_avs_llprefixlen();
-
-	case DLT_IEEE802_11_RADIO:
-		return gen_radiotap_llprefixlen();
-
-	case DLT_PPI:
-		return gen_ppi_llprefixlen();
-
-	default:
-		return NULL;
-	}
-}
-
-/*
- * Generate code to load the register containing the offset of the
- * MAC-layer payload into the X register; if no register for that offset
- * has been allocated, allocate it first.
- */
-static struct slist *
-gen_off_macpl(void)
+gen_abs_offset_varpart(bpf_abs_offset *off)
 {
 	struct slist *s;
 
-	if (off_macpl_is_variable) {
-		if (reg_off_macpl == -1) {
+	if (off->is_variable) {
+		if (off->reg == -1) {
 			/*
-			 * We haven't yet assigned a register for the offset
-			 * of the MAC-layer payload; allocate one.
+			 * We haven't yet assigned a register for the
+			 * variable part of the offset of the link-layer
+			 * header; allocate one.
 			 */
-			reg_off_macpl = alloc_reg();
+			off->reg = alloc_reg();
 		}
 
 		/*
-		 * Load the register containing the offset of the MAC-layer
-		 * payload into the X register.
+		 * Load the register containing the variable part of the
+		 * offset of the link-layer header into the X register.
 		 */
 		s = new_stmt(BPF_LDX|BPF_MEM);
-		s->s.k = reg_off_macpl;
+		s->s.k = off->reg;
 		return s;
 	} else {
 		/*
-		 * That offset isn't variable, so we don't need to
-		 * generate any code.
+		 * That offset isn't variable, there's no variable part,
+		 * so we don't need to generate any code.
 		 */
 		return NULL;
 	}
@@ -2890,6 +2788,43 @@
 }
 
 /*
+ * Generate any tests that, for encapsulation of a link-layer packet
+ * inside another protocol stack, need to be done to check for those
+ * link-layer packets (and that haven't already been done by a check
+ * for that encapsulation).
+ */
+static struct block *
+gen_prevlinkhdr_check(void)
+{
+	struct block *b0;
+
+	if (is_geneve)
+		return gen_geneve_ll_check();
+
+	switch (prevlinktype) {
+
+	case DLT_SUNATM:
+		/*
+		 * This is LANE-encapsulated Ethernet; check that the LANE
+		 * packet doesn't begin with an LE Control marker, i.e.
+		 * that it's data, not a control message.
+		 *
+		 * (We've already generated a test for LANE.)
+		 */
+		b0 = gen_cmp(OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
+		gen_not(b0);
+		return b0;
+
+	default:
+		/*
+		 * No such tests are necessary.
+		 */
+		return NULL;
+	}
+	/*NOTREACHED*/
+}
+
+/*
  * Generate code to match a particular packet type by matching the
  * link-layer type field or fields in the 802.2 LLC header.
  *
@@ -2901,6 +2836,7 @@
 	register int proto;
 {
 	struct block *b0, *b1, *b2;
+	const char *description;
 
 	/* are we checking MPLS-encapsulated packets? */
 	if (label_stack_depth > 0) {
@@ -2908,12 +2844,12 @@
 		case ETHERTYPE_IP:
 		case PPP_IP:
 			/* FIXME add other L3 proto IDs */
-			return gen_mpls_linktype(Q_IP); 
+			return gen_mpls_linktype(Q_IP);
 
 		case ETHERTYPE_IPV6:
 		case PPP_IPV6:
 			/* FIXME add other L3 proto IDs */
-			return gen_mpls_linktype(Q_IPV6); 
+			return gen_mpls_linktype(Q_IPV6);
 
 		default:
 			bpf_error("unsupported protocol over mpls");
@@ -2921,31 +2857,22 @@
 		}
 	}
 
-	/*
-	 * Are we testing PPPoE packets?
-	 */
-	if (is_pppoes) {
-		/*
-		 * The PPPoE session header is part of the
-		 * MAC-layer payload, so all references
-		 * should be relative to the beginning of
-		 * that payload.
-		 */
-
-		/*
-		 * We use Ethernet protocol types inside libpcap;
-		 * map them to the corresponding PPP protocol types.
-		 */
-		proto = ethertype_to_ppptype(proto);
-		return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
-	}
-
 	switch (linktype) {
 
 	case DLT_EN10MB:
 	case DLT_NETANALYZER:
 	case DLT_NETANALYZER_TRANSPARENT:
-		return gen_ether_linktype(proto);
+		/* Geneve has an EtherType regardless of whether there is an
+		 * L2 header. */
+		if (!is_geneve)
+			b0 = gen_prevlinkhdr_check();
+		else
+			b0 = NULL;
+
+		b1 = gen_ether_linktype(proto);
+		if (b0 != NULL)
+			gen_and(b0, b1);
+		return b1;
 		/*NOTREACHED*/
 		break;
 
@@ -2957,8 +2884,7 @@
 			/* fall through */
 
 		default:
-			return gen_cmp(OR_LINK, off_linktype, BPF_H,
-			    (bpf_int32)proto);
+			return gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
 			/*NOTREACHED*/
 			break;
 		}
@@ -2985,7 +2911,7 @@
 
 	case DLT_FDDI:
 		/*
-		 * XXX - check for asynchronous frames, as per RFC 1103.
+		 * XXX - check for LLC frames.
 		 */
 		return gen_llc_linktype(proto);
 		/*NOTREACHED*/
@@ -3008,38 +2934,16 @@
 
 	case DLT_SUNATM:
 		/*
-		 * If "is_lane" is set, check for a LANE-encapsulated
-		 * version of this protocol, otherwise check for an
-		 * LLC-encapsulated version of this protocol.
+		 * Check for an LLC-encapsulated version of this protocol;
+		 * if we were checking for LANE, linktype would no longer
+		 * be DLT_SUNATM.
 		 *
-		 * We assume LANE means Ethernet, not Token Ring.
+		 * Check for LLC encapsulation and then check the protocol.
 		 */
-		if (is_lane) {
-			/*
-			 * Check that the packet doesn't begin with an
-			 * LE Control marker.  (We've already generated
-			 * a test for LANE.)
-			 */
-			b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
-			    0xFF00);
-			gen_not(b0);
-
-			/*
-			 * Now generate an Ethernet test.
-			 */
-			b1 = gen_ether_linktype(proto);
-			gen_and(b0, b1);
-			return b1;
-		} else {
-			/*
-			 * Check for LLC encapsulation and then check the
-			 * protocol.
-			 */
-			b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
-			b1 = gen_llc_linktype(proto);
-			gen_and(b0, b1);
-			return b1;
-		}
+		b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
+		b1 = gen_llc_linktype(proto);
+		gen_and(b0, b1);
+		return b1;
 		/*NOTREACHED*/
 		break;
 
@@ -3062,11 +2966,11 @@
 
 		case ETHERTYPE_IP:
 			/* Check for a version number of 4. */
-			return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
+			return gen_mcmp(OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);
 
 		case ETHERTYPE_IPV6:
 			/* Check for a version number of 6. */
-			return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
+			return gen_mcmp(OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);
 
 		default:
 			return gen_false();		/* always false */
@@ -3107,7 +3011,7 @@
 		 * map them to the corresponding PPP protocol types.
 		 */
 		proto = ethertype_to_ppptype(proto);
-		return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
+		return gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
 		/*NOTREACHED*/
 		break;
 
@@ -3123,16 +3027,16 @@
 			 * Also check for Van Jacobson-compressed IP.
 			 * XXX - do this for other forms of PPP?
 			 */
-			b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
-			b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
+			b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, PPP_IP);
+			b1 = gen_cmp(OR_LINKTYPE, 0, BPF_H, PPP_VJC);
 			gen_or(b0, b1);
-			b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
+			b0 = gen_cmp(OR_LINKTYPE, 0, BPF_H, PPP_VJNC);
 			gen_or(b1, b0);
 			return b0;
 
 		default:
 			proto = ethertype_to_ppptype(proto);
-			return gen_cmp(OR_LINK, off_linktype, BPF_H,
+			return gen_cmp(OR_LINKTYPE, 0, BPF_H,
 				(bpf_int32)proto);
 		}
 		/*NOTREACHED*/
@@ -3201,7 +3105,7 @@
 				proto = SWAPLONG(proto);
 			proto = htonl(proto);
 		}
-		return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
+		return (gen_cmp(OR_LINKHDR, 0, BPF_W, (bpf_int32)proto));
 
 #ifdef HAVE_NET_PFVAR_H
 	case DLT_PFLOG:
@@ -3210,10 +3114,10 @@
 		 * the packet.
 		 */
 		if (proto == ETHERTYPE_IP)
-			return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
+			return (gen_cmp(OR_LINKHDR, offsetof(struct pfloghdr, af),
 			    BPF_B, (bpf_int32)AF_INET));
 		else if (proto == ETHERTYPE_IPV6)
-			return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
+			return (gen_cmp(OR_LINKHDR, offsetof(struct pfloghdr, af),
 			    BPF_B, (bpf_int32)AF_INET6));
 		else
 			return gen_false();
@@ -3233,31 +3137,31 @@
 			return gen_false();
 
 		case ETHERTYPE_IPV6:
-			return (gen_cmp(OR_LINK, off_linktype, BPF_B,
+			return (gen_cmp(OR_LINKTYPE, 0, BPF_B,
 				(bpf_int32)ARCTYPE_INET6));
 
 		case ETHERTYPE_IP:
-			b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
+			b0 = gen_cmp(OR_LINKTYPE, 0, BPF_B,
 				     (bpf_int32)ARCTYPE_IP);
-			b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
+			b1 = gen_cmp(OR_LINKTYPE, 0, BPF_B,
 				     (bpf_int32)ARCTYPE_IP_OLD);
 			gen_or(b0, b1);
 			return (b1);
 
 		case ETHERTYPE_ARP:
-			b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
+			b0 = gen_cmp(OR_LINKTYPE, 0, BPF_B,
 				     (bpf_int32)ARCTYPE_ARP);
-			b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
+			b1 = gen_cmp(OR_LINKTYPE, 0, BPF_B,
 				     (bpf_int32)ARCTYPE_ARP_OLD);
 			gen_or(b0, b1);
 			return (b1);
 
 		case ETHERTYPE_REVARP:
-			return (gen_cmp(OR_LINK, off_linktype, BPF_B,
+			return (gen_cmp(OR_LINKTYPE, 0, BPF_B,
 					(bpf_int32)ARCTYPE_REVARP));
 
 		case ETHERTYPE_ATALK:
-			return (gen_cmp(OR_LINK, off_linktype, BPF_B,
+			return (gen_cmp(OR_LINKTYPE, 0, BPF_B,
 					(bpf_int32)ARCTYPE_ATALK));
 		}
 		/*NOTREACHED*/
@@ -3284,13 +3188,13 @@
 			/*
 			 * Check for the special NLPID for IP.
 			 */
-			return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
+			return gen_cmp(OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);
 
 		case ETHERTYPE_IPV6:
 			/*
 			 * Check for the special NLPID for IPv6.
 			 */
-			return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
+			return gen_cmp(OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);
 
 		case LLCSAP_ISONS:
 			/*
@@ -3304,9 +3208,9 @@
 			 * control field of UI, i.e. 0x03 followed
 			 * by the NLPID.
 			 */
-			b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
-			b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
-			b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
+			b0 = gen_cmp(OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
+			b1 = gen_cmp(OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
+			b2 = gen_cmp(OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
 			gen_or(b1, b2);
 			gen_or(b0, b2);
 			return b2;
@@ -3350,10 +3254,10 @@
 		 *
 		 * FIXME encapsulation specific BPF_ filters
 		 */
-		return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
+		return gen_mcmp(OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
 
 	case DLT_BACNET_MS_TP:
-		return gen_mcmp(OR_LINK, 0, BPF_W, 0x55FF0000, 0xffff0000);
+		return gen_mcmp(OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);
 
 	case DLT_IPNET:
 		return gen_ipnet_linktype(proto);
@@ -3410,26 +3314,43 @@
 
 	case DLT_AX25_KISS:
 		bpf_error("AX.25 link-layer type filtering not implemented");
+
+	case DLT_NFLOG:
+		/* Using the fixed-size NFLOG header it is possible to tell only
+		 * the address family of the packet, other meaningful data is
+		 * either missing or behind TLVs.
+		 */
+		bpf_error("NFLOG link-layer type filtering not implemented");
+
+	default:
+		/*
+		 * Does this link-layer header type have a field
+		 * indicating the type of the next protocol?  If
+		 * so, off_linktype.constant_part will be the offset of that
+		 * field in the packet; if not, it will be -1.
+		 */
+		if (off_linktype.constant_part != (u_int)-1) {
+			/*
+			 * Yes; assume it's an Ethernet type.  (If
+			 * it's not, it needs to be handled specially
+			 * above.)
+			 */
+			return gen_cmp(OR_LINKTYPE, 0, BPF_H, (bpf_int32)proto);
+		} else {
+			/*
+			 * No; report an error.
+			 */
+			description = pcap_datalink_val_to_description(linktype);
+			if (description != NULL) {
+				bpf_error("%s link-layer type filtering not implemented",
+				    description);
+			} else {
+				bpf_error("DLT %u link-layer type filtering not implemented",
+				    linktype);
+			}
+		}
+		break;
 	}
-
-	/*
-	 * All the types that have no encapsulation should either be
-	 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
-	 * all packets are IP packets, or should be handled in some
-	 * special case, if none of them are (if some are and some
-	 * aren't, the lack of encapsulation is a problem, as we'd
-	 * have to find some other way of determining the packet type).
-	 *
-	 * Therefore, if "off_linktype" is -1, there's an error.
-	 */
-	if (off_linktype == (u_int)-1)
-		abort();
-
-	/*
-	 * Any type not handled above should always have an Ethernet
-	 * type at an offset of "off_linktype".
-	 */
-	return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
 }
 
 /*
@@ -3454,7 +3375,179 @@
 	snapblock[5] = (orgcode >> 0);	/* lower 8 bits of organization code */
 	snapblock[6] = (ptype >> 8);	/* upper 8 bits of protocol type */
 	snapblock[7] = (ptype >> 0);	/* lower 8 bits of protocol type */
-	return gen_bcmp(OR_MACPL, 0, 8, snapblock);
+	return gen_bcmp(OR_LLC, 0, 8, snapblock);
+}
+
+/*
+ * Generate code to match frames with an LLC header.
+ */
+struct block *
+gen_llc(void)
+{
+	struct block *b0, *b1;
+
+	switch (linktype) {
+
+	case DLT_EN10MB:
+		/*
+		 * We check for an Ethernet type field less than
+		 * 1500, which means it's an 802.3 length field.
+		 */
+		b0 = gen_cmp_gt(OR_LINKTYPE, 0, BPF_H, ETHERMTU);
+		gen_not(b0);
+
+		/*
+		 * Now check for the purported DSAP and SSAP not being
+		 * 0xFF, to rule out NetWare-over-802.3.
+		 */
+		b1 = gen_cmp(OR_LLC, 0, BPF_H, (bpf_int32)0xFFFF);
+		gen_not(b1);
+		gen_and(b0, b1);
+		return b1;
+
+	case DLT_SUNATM:
+		/*
+		 * We check for LLC traffic.
+		 */
+		b0 = gen_atmtype_abbrev(A_LLC);
+		return b0;
+
+	case DLT_IEEE802:	/* Token Ring */
+		/*
+		 * XXX - check for LLC frames.
+		 */
+		return gen_true();
+
+	case DLT_FDDI:
+		/*
+		 * XXX - check for LLC frames.
+		 */
+		return gen_true();
+
+	case DLT_ATM_RFC1483:
+		/*
+		 * For LLC encapsulation, these are defined to have an
+		 * 802.2 LLC header.
+		 *
+		 * For VC encapsulation, they don't, but there's no
+		 * way to check for that; the protocol used on the VC
+		 * is negotiated out of band.
+		 */
+		return gen_true();
+
+	case DLT_IEEE802_11:
+	case DLT_PRISM_HEADER:
+	case DLT_IEEE802_11_RADIO:
+	case DLT_IEEE802_11_RADIO_AVS:
+	case DLT_PPI:
+		/*
+		 * Check that we have a data frame.
+		 */
+		b0 = gen_check_802_11_data_frame();
+		return b0;
+
+	default:
+		bpf_error("'llc' not supported for linktype %d", linktype);
+		/* NOTREACHED */
+	}
+}
+
+struct block *
+gen_llc_i(void)
+{
+	struct block *b0, *b1;
+	struct slist *s;
+
+	/*
+	 * Check whether this is an LLC frame.
+	 */
+	b0 = gen_llc();
+
+	/*
+	 * Load the control byte and test the low-order bit; it must
+	 * be clear for I frames.
+	 */
+	s = gen_load_a(OR_LLC, 2, BPF_B);
+	b1 = new_block(JMP(BPF_JSET));
+	b1->s.k = 0x01;
+	b1->stmts = s;
+	gen_not(b1);
+	gen_and(b0, b1);
+	return b1;
+}
+
+struct block *
+gen_llc_s(void)
+{
+	struct block *b0, *b1;
+
+	/*
+	 * Check whether this is an LLC frame.
+	 */
+	b0 = gen_llc();
+
+	/*
+	 * Now compare the low-order 2 bit of the control byte against
+	 * the appropriate value for S frames.
+	 */
+	b1 = gen_mcmp(OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
+	gen_and(b0, b1);
+	return b1;
+}
+
+struct block *
+gen_llc_u(void)
+{
+	struct block *b0, *b1;
+
+	/*
+	 * Check whether this is an LLC frame.
+	 */
+	b0 = gen_llc();
+
+	/*
+	 * Now compare the low-order 2 bit of the control byte against
+	 * the appropriate value for U frames.
+	 */
+	b1 = gen_mcmp(OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
+	gen_and(b0, b1);
+	return b1;
+}
+
+struct block *
+gen_llc_s_subtype(bpf_u_int32 subtype)
+{
+	struct block *b0, *b1;
+
+	/*
+	 * Check whether this is an LLC frame.
+	 */
+	b0 = gen_llc();
+
+	/*
+	 * Now check for an S frame with the appropriate type.
+	 */
+	b1 = gen_mcmp(OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
+	gen_and(b0, b1);
+	return b1;
+}
+
+struct block *
+gen_llc_u_subtype(bpf_u_int32 subtype)
+{
+	struct block *b0, *b1;
+
+	/*
+	 * Check whether this is an LLC frame.
+	 */
+	b0 = gen_llc();
+
+	/*
+	 * Now check for a U frame with the appropriate type.
+	 */
+	b1 = gen_mcmp(OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
+	gen_and(b0, b1);
+	return b1;
 }
 
 /*
@@ -3484,10 +3577,9 @@
 		/*
 		 * XXX - should we check both the DSAP and the
 		 * SSAP, like this, or should we check just the
-		 * DSAP, as we do for other types <= ETHERMTU
-		 * (i.e., other SAP values)?
+		 * DSAP, as we do for other SAP values?
 		 */
-		return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
+		return gen_cmp(OR_LLC, 0, BPF_H, (bpf_u_int32)
 			     ((proto << 8) | proto));
 
 	case LLCSAP_IPX:
@@ -3495,7 +3587,7 @@
 		 * XXX - are there ever SNAP frames for IPX on
 		 * non-Ethernet 802.x networks?
 		 */
-		return gen_cmp(OR_MACPL, 0, BPF_B,
+		return gen_cmp(OR_LLC, 0, BPF_B,
 		    (bpf_int32)LLCSAP_IPX);
 
 	case ETHERTYPE_ATALK:
@@ -3520,7 +3612,7 @@
 			 * This is an LLC SAP value, so check
 			 * the DSAP.
 			 */
-			return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
+			return gen_cmp(OR_LLC, 0, BPF_B, (bpf_int32)proto);
 		} else {
 			/*
 			 * This is an Ethernet type; we assume that it's
@@ -3541,7 +3633,7 @@
 			 * I don't know whether it's worth the extra CPU
 			 * time to do the right check or not.
 			 */
-			return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
+			return gen_cmp(OR_LLC, 6, BPF_H, (bpf_int32)proto);
 		}
 	}
 }
@@ -3583,7 +3675,7 @@
 		abort();
 	}
 	b0 = gen_linktype(proto);
-	b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
+	b1 = gen_mcmp(OR_LINKPL, offset, BPF_W, (bpf_int32)addr, mask);
 	gen_and(b0, b1);
 	return b1;
 }
@@ -3629,12 +3721,12 @@
 	/* this order is important */
 	a = (u_int32_t *)addr;
 	m = (u_int32_t *)mask;
-	b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
-	b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
+	b1 = gen_mcmp(OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
+	b0 = gen_mcmp(OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
 	gen_and(b0, b1);
-	b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
+	b0 = gen_mcmp(OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
 	gen_and(b0, b1);
-	b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
+	b0 = gen_mcmp(OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
 	gen_and(b0, b1);
 	b0 = gen_linktype(proto);
 	gen_and(b0, b1);
@@ -3651,10 +3743,10 @@
 
 	switch (dir) {
 	case Q_SRC:
-		return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 6, 6, eaddr);
 
 	case Q_DST:
-		return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 0, 6, eaddr);
 
 	case Q_AND:
 		b0 = gen_ehostop(eaddr, Q_SRC);
@@ -3709,10 +3801,10 @@
 
 	switch (dir) {
 	case Q_SRC:
-		return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 6 + 1 + pcap_fddipad, 6, eaddr);
 
 	case Q_DST:
-		return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 0 + 1 + pcap_fddipad, 6, eaddr);
 
 	case Q_AND:
 		b0 = gen_fhostop(eaddr, Q_SRC);
@@ -3767,10 +3859,10 @@
 
 	switch (dir) {
 	case Q_SRC:
-		return gen_bcmp(OR_LINK, 8, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 8, 6, eaddr);
 
 	case Q_DST:
-		return gen_bcmp(OR_LINK, 2, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 2, 6, eaddr);
 
 	case Q_AND:
 		b0 = gen_thostop(eaddr, Q_SRC);
@@ -3862,7 +3954,7 @@
 		 *
 		 * First, check for To DS set, i.e. check "link[1] & 0x01".
 		 */
-		s = gen_load_a(OR_LINK, 1, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x01;	/* To DS */
 		b1->stmts = s;
@@ -3870,14 +3962,14 @@
 		/*
 		 * If To DS is set, the SA is at 24.
 		 */
-		b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
+		b0 = gen_bcmp(OR_LINKHDR, 24, 6, eaddr);
 		gen_and(b1, b0);
 
 		/*
 		 * Now, check for To DS not set, i.e. check
 		 * "!(link[1] & 0x01)".
 		 */
-		s = gen_load_a(OR_LINK, 1, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 		b2 = new_block(JMP(BPF_JSET));
 		b2->s.k = 0x01;	/* To DS */
 		b2->stmts = s;
@@ -3886,7 +3978,7 @@
 		/*
 		 * If To DS is not set, the SA is at 16.
 		 */
-		b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 16, 6, eaddr);
 		gen_and(b2, b1);
 
 		/*
@@ -3900,7 +3992,7 @@
 		 * Now check for From DS being set, and AND that with
 		 * the ORed-together checks.
 		 */
-		s = gen_load_a(OR_LINK, 1, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x02;	/* From DS */
 		b1->stmts = s;
@@ -3909,7 +4001,7 @@
 		/*
 		 * Now check for data frames with From DS not set.
 		 */
-		s = gen_load_a(OR_LINK, 1, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 		b2 = new_block(JMP(BPF_JSET));
 		b2->s.k = 0x02;	/* From DS */
 		b2->stmts = s;
@@ -3918,7 +4010,7 @@
 		/*
 		 * If From DS isn't set, the SA is at 10.
 		 */
-		b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 10, 6, eaddr);
 		gen_and(b2, b1);
 
 		/*
@@ -3932,7 +4024,7 @@
 		 * Now check for a data frame.
 		 * I.e, check "link[0] & 0x08".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x08;
 		b1->stmts = s;
@@ -3947,7 +4039,7 @@
 		 * is a management frame.
 		 * I.e, check "!(link[0] & 0x08)".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b2 = new_block(JMP(BPF_JSET));
 		b2->s.k = 0x08;
 		b2->stmts = s;
@@ -3956,7 +4048,7 @@
 		/*
 		 * For management frames, the SA is at 10.
 		 */
-		b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 10, 6, eaddr);
 		gen_and(b2, b1);
 
 		/*
@@ -3974,7 +4066,7 @@
 		 *
 		 * I.e., check "!(link[0] & 0x04)".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x04;
 		b1->stmts = s;
@@ -4009,7 +4101,7 @@
 		 *
 		 * First, check for To DS set, i.e. "link[1] & 0x01".
 		 */
-		s = gen_load_a(OR_LINK, 1, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x01;	/* To DS */
 		b1->stmts = s;
@@ -4017,14 +4109,14 @@
 		/*
 		 * If To DS is set, the DA is at 16.
 		 */
-		b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
+		b0 = gen_bcmp(OR_LINKHDR, 16, 6, eaddr);
 		gen_and(b1, b0);
 
 		/*
 		 * Now, check for To DS not set, i.e. check
 		 * "!(link[1] & 0x01)".
 		 */
-		s = gen_load_a(OR_LINK, 1, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 		b2 = new_block(JMP(BPF_JSET));
 		b2->s.k = 0x01;	/* To DS */
 		b2->stmts = s;
@@ -4033,7 +4125,7 @@
 		/*
 		 * If To DS is not set, the DA is at 4.
 		 */
-		b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 4, 6, eaddr);
 		gen_and(b2, b1);
 
 		/*
@@ -4046,7 +4138,7 @@
 		 * Now check for a data frame.
 		 * I.e, check "link[0] & 0x08".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x08;
 		b1->stmts = s;
@@ -4061,7 +4153,7 @@
 		 * is a management frame.
 		 * I.e, check "!(link[0] & 0x08)".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b2 = new_block(JMP(BPF_JSET));
 		b2->s.k = 0x08;
 		b2->stmts = s;
@@ -4070,7 +4162,7 @@
 		/*
 		 * For management frames, the DA is at 4.
 		 */
-		b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 4, 6, eaddr);
 		gen_and(b2, b1);
 
 		/*
@@ -4088,7 +4180,7 @@
 		 *
 		 * I.e., check "!(link[0] & 0x04)".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x04;
 		b1->stmts = s;
@@ -4112,7 +4204,7 @@
 		 * is a management frame.
 		 * I.e, check "(link[0] & 0x08)".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x08;
 		b1->stmts = s;
@@ -4120,7 +4212,7 @@
 		/*
 		 * Check addr1.
 		 */
-		b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
+		b0 = gen_bcmp(OR_LINKHDR, 4, 6, eaddr);
 
 		/*
 		 * AND that with the check of addr1.
@@ -4137,13 +4229,13 @@
 		/*
 		 * Not present in CTS or ACK control frames.
 		 */
-		b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
+		b0 = gen_mcmp(OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
 			IEEE80211_FC0_TYPE_MASK);
 		gen_not(b0);
-		b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
+		b1 = gen_mcmp(OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
 			IEEE80211_FC0_SUBTYPE_MASK);
 		gen_not(b1);
-		b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
+		b2 = gen_mcmp(OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
 			IEEE80211_FC0_SUBTYPE_MASK);
 		gen_not(b2);
 		gen_and(b1, b2);
@@ -4154,7 +4246,7 @@
 		 * is a management frame.
 		 * I.e, check "(link[0] & 0x08)".
 		 */
-		s = gen_load_a(OR_LINK, 0, BPF_B);
+		s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 		b1 = new_block(JMP(BPF_JSET));
 		b1->s.k = 0x08;
 		b1->stmts = s;
@@ -4168,7 +4260,7 @@
 		/*
 		 * Check addr2.
 		 */
-		b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 10, 6, eaddr);
 		gen_and(b2, b1);
 		return b1;
 
@@ -4176,24 +4268,24 @@
 	 * XXX - add BSSID keyword?
 	 */
 	case Q_ADDR1:
-		return (gen_bcmp(OR_LINK, 4, 6, eaddr));
+		return (gen_bcmp(OR_LINKHDR, 4, 6, eaddr));
 
 	case Q_ADDR2:
 		/*
 		 * Not present in CTS or ACK control frames.
 		 */
-		b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
+		b0 = gen_mcmp(OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
 			IEEE80211_FC0_TYPE_MASK);
 		gen_not(b0);
-		b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
+		b1 = gen_mcmp(OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
 			IEEE80211_FC0_SUBTYPE_MASK);
 		gen_not(b1);
-		b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
+		b2 = gen_mcmp(OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
 			IEEE80211_FC0_SUBTYPE_MASK);
 		gen_not(b2);
 		gen_and(b1, b2);
 		gen_or(b0, b2);
-		b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 10, 6, eaddr);
 		gen_and(b2, b1);
 		return b1;
 
@@ -4201,10 +4293,10 @@
 		/*
 		 * Not present in control frames.
 		 */
-		b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
+		b0 = gen_mcmp(OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
 			IEEE80211_FC0_TYPE_MASK);
 		gen_not(b0);
-		b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 16, 6, eaddr);
 		gen_and(b0, b1);
 		return b1;
 
@@ -4215,9 +4307,9 @@
 		 * frames should have both of those set, so we don't
 		 * check the frame type.
 		 */
-		b0 = gen_mcmp(OR_LINK, 1, BPF_B,
+		b0 = gen_mcmp(OR_LINKHDR, 1, BPF_B,
 			IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
-		b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
+		b1 = gen_bcmp(OR_LINKHDR, 24, 6, eaddr);
 		gen_and(b0, b1);
 		return b1;
 
@@ -4252,10 +4344,10 @@
 
 	switch (dir) {
 	case Q_SRC:
-		return gen_bcmp(OR_LINK, 10, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 10, 6, eaddr);
 
 	case Q_DST:
-		return gen_bcmp(OR_LINK, 2, 6, eaddr);
+		return gen_bcmp(OR_LINKHDR, 2, 6, eaddr);
 
 	case Q_AND:
 		b0 = gen_ipfchostop(eaddr, Q_SRC);
@@ -4360,25 +4452,25 @@
 	}
 	b0 = gen_linktype(ETHERTYPE_DN);
 	/* Check for pad = 1, long header case */
-	tmp = gen_mcmp(OR_NET, 2, BPF_H,
+	tmp = gen_mcmp(OR_LINKPL, 2, BPF_H,
 	    (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
-	b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
+	b1 = gen_cmp(OR_LINKPL, 2 + 1 + offset_lh,
 	    BPF_H, (bpf_int32)ntohs((u_short)addr));
 	gen_and(tmp, b1);
 	/* Check for pad = 0, long header case */
-	tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
-	b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
+	tmp = gen_mcmp(OR_LINKPL, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
+	b2 = gen_cmp(OR_LINKPL, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
 	gen_and(tmp, b2);
 	gen_or(b2, b1);
 	/* Check for pad = 1, short header case */
-	tmp = gen_mcmp(OR_NET, 2, BPF_H,
+	tmp = gen_mcmp(OR_LINKPL, 2, BPF_H,
 	    (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
-	b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
+	b2 = gen_cmp(OR_LINKPL, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
 	gen_and(tmp, b2);
 	gen_or(b2, b1);
 	/* Check for pad = 0, short header case */
-	tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
-	b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
+	tmp = gen_mcmp(OR_LINKPL, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
+	b2 = gen_cmp(OR_LINKPL, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
 	gen_and(tmp, b2);
 	gen_or(b2, b1);
 
@@ -4402,20 +4494,20 @@
 
         case Q_IP:
                 /* match the bottom-of-stack bit */
-                b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
+                b0 = gen_mcmp(OR_LINKPL, -2, BPF_B, 0x01, 0x01);
                 /* match the IPv4 version number */
-                b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
+                b1 = gen_mcmp(OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
                 gen_and(b0, b1);
                 return b1;
- 
+
        case Q_IPV6:
                 /* match the bottom-of-stack bit */
-                b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
+                b0 = gen_mcmp(OR_LINKPL, -2, BPF_B, 0x01, 0x01);
                 /* match the IPv4 version number */
-                b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
+                b1 = gen_mcmp(OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
                 gen_and(b0, b1);
                 return b1;
- 
+
        default:
                 abort();
         }
@@ -4698,7 +4790,10 @@
 		case DLT_EN10MB:
 		case DLT_NETANALYZER:
 		case DLT_NETANALYZER_TRANSPARENT:
+			b1 = gen_prevlinkhdr_check();
 			b0 = gen_ehostop(eaddr, Q_OR);
+			if (b1 != NULL)
+				gen_and(b1, b0);
 			break;
 		case DLT_FDDI:
 			b0 = gen_fhostop(eaddr, Q_OR);
@@ -4714,23 +4809,13 @@
 			b0 = gen_wlanhostop(eaddr, Q_OR);
 			break;
 		case DLT_SUNATM:
-			if (!is_lane)
-				bpf_error(
-				    "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
 			/*
-			 * Check that the packet doesn't begin with an
-			 * LE Control marker.  (We've already generated
-			 * a test for LANE.)
+			 * This is LLC-multiplexed traffic; if it were
+			 * LANE, linktype would have been set to
+			 * DLT_EN10MB.
 			 */
-			b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
-			    BPF_H, 0xFF00);
-			gen_not(b1);
-
-			/*
-			 * Now check the MAC address.
-			 */
-			b0 = gen_ehostop(eaddr, Q_OR);
-			gen_and(b1, b0);
+			bpf_error(
+			    "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
 			break;
 		case DLT_IP_OVER_FC:
 			b0 = gen_ipfchostop(eaddr, Q_OR);
@@ -5003,7 +5088,7 @@
 	struct block *b;
 
 	/* not IPv4 frag other than the first frag */
-	s = gen_load_a(OR_NET, 6, BPF_H);
+	s = gen_load_a(OR_LINKPL, 6, BPF_H);
 	b = new_block(JMP(BPF_JSET));
 	b->s.k = 0x1fff;
 	b->stmts = s;
@@ -5044,7 +5129,7 @@
 	struct block *b0, *b1, *tmp;
 
 	/* ip proto 'proto' and not a fragment other than the first fragment */
-	tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
+	tmp = gen_cmp(OR_LINKPL, 9, BPF_B, (bpf_int32)proto);
 	b0 = gen_ipfrag();
 	gen_and(tmp, b0);
 
@@ -5135,7 +5220,7 @@
 
 	/* ip6 proto 'proto' */
 	/* XXX - catch the first fragment of a fragmented packet? */
-	b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
+	b0 = gen_cmp(OR_LINKPL, 6, BPF_B, (bpf_int32)proto);
 
 	switch (dir) {
 	case Q_SRC:
@@ -5222,7 +5307,7 @@
 	b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
 	b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
 
-	gen_and(b1, b2); 
+	gen_and(b1, b2);
 
 	return b2;
 }
@@ -5236,7 +5321,7 @@
 	struct block *b0, *b1, *tmp;
 
 	/* ip proto 'proto' and not a fragment other than the first fragment */
-	tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
+	tmp = gen_cmp(OR_LINKPL, 9, BPF_B, (bpf_int32)proto);
 	b0 = gen_ipfrag();
 	gen_and(tmp, b0);
 
@@ -5324,7 +5409,7 @@
 	b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
 	b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
 
-	gen_and(b1, b2); 
+	gen_and(b1, b2);
 
 	return b2;
 }
@@ -5339,7 +5424,7 @@
 
 	/* ip6 proto 'proto' */
 	/* XXX - catch the first fragment of a fragmented packet? */
-	b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
+	b0 = gen_cmp(OR_LINKPL, 6, BPF_B, (bpf_int32)proto);
 
 	switch (dir) {
 	case Q_SRC:
@@ -5503,15 +5588,8 @@
 	 * branches, and backward branch support is unlikely to appear
 	 * in kernel BPF engines.)
 	 */
-	switch (linktype) {
-
-	case DLT_IEEE802_11:
-	case DLT_PRISM_HEADER:
-	case DLT_IEEE802_11_RADIO_AVS:
-	case DLT_IEEE802_11_RADIO:
-	case DLT_PPI:
-		bpf_error("'protochain' not supported with 802.11");
-	}
+	if (off_linkpl.is_variable)
+		bpf_error("'protochain' not supported with variable length headers");
 
 	no_optimize = 1; /*this code is not compatible with optimzer yet */
 
@@ -5530,11 +5608,11 @@
 
 		/* A = ip->ip_p */
 		s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
-		s[i]->s.k = off_macpl + off_nl + 9;
+		s[i]->s.k = off_linkpl.constant_part + off_nl + 9;
 		i++;
 		/* X = ip->ip_hl << 2 */
 		s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
-		s[i]->s.k = off_macpl + off_nl;
+		s[i]->s.k = off_linkpl.constant_part + off_nl;
 		i++;
 		break;
 
@@ -5543,7 +5621,7 @@
 
 		/* A = ip6->ip_nxt */
 		s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
-		s[i]->s.k = off_macpl + off_nl + 6;
+		s[i]->s.k = off_linkpl.constant_part + off_nl + 6;
 		i++;
 		/* X = sizeof(struct ip6_hdr) */
 		s[i] = new_stmt(BPF_LDX|BPF_IMM);
@@ -5619,7 +5697,7 @@
 		 */
 		/* A = P[X + packet head] */
 		s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
-		s[i]->s.k = off_macpl + off_nl;
+		s[i]->s.k = off_linkpl.constant_part + off_nl;
 		i++;
 		/* MEM[reg2] = A */
 		s[i] = new_stmt(BPF_ST);
@@ -5627,7 +5705,7 @@
 		i++;
 		/* A = P[X + packet head + 1]; */
 		s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
-		s[i]->s.k = off_macpl + off_nl + 1;
+		s[i]->s.k = off_linkpl.constant_part + off_nl + 1;
 		i++;
 		/* A += 1 */
 		s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
@@ -5688,7 +5766,7 @@
 	i++;
 	/* A = P[X + packet head]; */
 	s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
-	s[i]->s.k = off_macpl + off_nl;
+	s[i]->s.k = off_linkpl.constant_part + off_nl;
 	i++;
 	/* MEM[reg2] = A */
 	s[i] = new_stmt(BPF_ST);
@@ -5706,7 +5784,7 @@
 	i++;
 	/* A = P[X + packet head] */
 	s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
-	s[i]->s.k = off_macpl + off_nl;
+	s[i]->s.k = off_linkpl.constant_part + off_nl;
 	i++;
 	/* A += 2 */
 	s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
@@ -5770,12 +5848,12 @@
 	 * A data frame has the 0x08 bit (b3) in the frame control field set
 	 * and the 0x04 bit (b2) clear.
 	 */
-	s = gen_load_a(OR_LINK, 0, BPF_B);
+	s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 	b0 = new_block(JMP(BPF_JSET));
 	b0->s.k = 0x08;
 	b0->stmts = s;
-	
-	s = gen_load_a(OR_LINK, 0, BPF_B);
+
+	s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 	b1 = new_block(JMP(BPF_JSET));
 	b1->s.k = 0x04;
 	b1->stmts = s;
@@ -5834,7 +5912,7 @@
 		 */
 		b0 = gen_linktype(ETHERTYPE_IP);
 #ifndef CHASE_CHAIN
-		b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
+		b1 = gen_cmp(OR_LINKPL, 9, BPF_B, (bpf_int32)v);
 #else
 		b1 = gen_protochain(v, Q_IP);
 #endif
@@ -5863,7 +5941,7 @@
 			 *
 			 * XXX - what about SNAP-encapsulated frames?
 			 */
-			return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
+			return gen_cmp(OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
 			/*NOTREACHED*/
 			break;
 
@@ -5874,13 +5952,13 @@
 			 */
 			b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
 			/* OSI in C-HDLC is stuffed with a fudge byte */
-			b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
+			b1 = gen_cmp(OR_LINKPL_NOSNAP, 1, BPF_B, (long)v);
 			gen_and(b0, b1);
 			return b1;
 
 		default:
 			b0 = gen_linktype(LLCSAP_ISONS);
-			b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
+			b1 = gen_cmp(OR_LINKPL_NOSNAP, 0, BPF_B, (long)v);
 			gen_and(b0, b1);
 			return b1;
 		}
@@ -5891,7 +5969,7 @@
 		 * 4 is the offset of the PDU type relative to the IS-IS
 		 * header.
 		 */
-		b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
+		b1 = gen_cmp(OR_LINKPL_NOSNAP, 4, BPF_B, (long)v);
 		gen_and(b0, b1);
 		return b1;
 
@@ -5973,10 +6051,10 @@
 		 * Also check for a fragment header before the final
 		 * header.
 		 */
-		b2 = gen_cmp(OR_NET, 6, BPF_B, IPPROTO_FRAGMENT);
-		b1 = gen_cmp(OR_NET, 40, BPF_B, (bpf_int32)v);
+		b2 = gen_cmp(OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
+		b1 = gen_cmp(OR_LINKPL, 40, BPF_B, (bpf_int32)v);
 		gen_and(b2, b1);
-		b2 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
+		b2 = gen_cmp(OR_LINKPL, 6, BPF_B, (bpf_int32)v);
 		gen_or(b2, b1);
 #else
 		b1 = gen_protochain(v, Q_IPV6);
@@ -6061,7 +6139,10 @@
 				if (eaddr == NULL)
 					bpf_error(
 					    "unknown ether host '%s'", name);
+				tmp = gen_prevlinkhdr_check();
 				b = gen_ehostop(eaddr, dir);
+				if (tmp != NULL)
+					gen_and(tmp, b);
 				free(eaddr);
 				return b;
 
@@ -6104,28 +6185,6 @@
 				b = gen_ipfchostop(eaddr, dir);
 				free(eaddr);
 				return b;
-
-			case DLT_SUNATM:
-				if (!is_lane)
-					break;
-
-				/*
-				 * Check that the packet doesn't begin
-				 * with an LE Control marker.  (We've
-				 * already generated a test for LANE.)
-				 */
-				tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
-				    BPF_H, 0xFF00);
-				gen_not(tmp);
-
-				eaddr = pcap_ether_hostton(name);
-				if (eaddr == NULL)
-					bpf_error(
-					    "unknown ether host '%s'", name);
-				b = gen_ehostop(eaddr, dir);
-				gen_and(tmp, b);
-				free(eaddr);
-				return b;
 			}
 
 			bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
@@ -6142,7 +6201,8 @@
 			if (alist == NULL || *alist == NULL)
 				bpf_error("unknown host '%s'", name);
 			tproto = proto;
-			if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
+			if (off_linktype.constant_part == (u_int)-1 &&
+			    tproto == Q_DEFAULT)
 				tproto = Q_IP;
 			b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
 			while (*alist) {
@@ -6160,7 +6220,8 @@
 			ai = res;
 			b = tmp = NULL;
 			tproto = tproto6 = proto;
-			if (off_linktype == -1 && tproto == Q_DEFAULT) {
+			if (off_linktype.constant_part == -1 &&
+			    tproto == Q_DEFAULT) {
 				tproto = Q_IP;
 				tproto6 = Q_IPV6;
 			}
@@ -6250,7 +6311,7 @@
 		if (proto != Q_DEFAULT &&
 		    proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
 			bpf_error("illegal qualifier of 'portrange'");
-		if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0) 
+		if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
 			bpf_error("unknown port in range '%s'", name);
 		if (proto == Q_UDP) {
 			if (real_proto == IPPROTO_TCP)
@@ -6277,7 +6338,7 @@
 				bpf_error("port in range '%s' is tcp", name);
 			else
 				/* override PROTO_UNDEF */
-				real_proto = IPPROTO_SCTP;	
+				real_proto = IPPROTO_SCTP;
 		}
 		if (port1 < 0)
 			bpf_error("illegal port number %d < 0", port1);
@@ -6333,7 +6394,7 @@
 struct block *
 gen_mcode(s1, s2, masklen, q)
 	register const char *s1, *s2;
-	register int masklen;
+	register unsigned int masklen;
 	struct qual q;
 {
 	register int nlen, mlen;
@@ -6492,7 +6553,7 @@
 struct block *
 gen_mcode6(s1, s2, masklen, q)
 	register const char *s1, *s2;
-	register int masklen;
+	register unsigned int masklen;
 	struct qual q;
 {
 	struct addrinfo *res;
@@ -6562,7 +6623,11 @@
 		case DLT_EN10MB:
 		case DLT_NETANALYZER:
 		case DLT_NETANALYZER_TRANSPARENT:
-			return gen_ehostop(eaddr, (int)q.dir);
+			tmp = gen_prevlinkhdr_check();
+			b = gen_ehostop(eaddr, (int)q.dir);
+			if (tmp != NULL)
+				gen_and(tmp, b);
+			return b;
 		case DLT_FDDI:
 			return gen_fhostop(eaddr, (int)q.dir);
 		case DLT_IEEE802:
@@ -6573,25 +6638,6 @@
 		case DLT_IEEE802_11_RADIO:
 		case DLT_PPI:
 			return gen_wlanhostop(eaddr, (int)q.dir);
-		case DLT_SUNATM:
-			if (is_lane) {
-				/*
-				 * Check that the packet doesn't begin with an
-				 * LE Control marker.  (We've already generated
-				 * a test for LANE.)
-				 */
-				tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
-					0xFF00);
-				gen_not(tmp);
-
-				/*
-				 * Now check the MAC address.
-				 */
-				b = gen_ehostop(eaddr, (int)q.dir);
-				gen_and(tmp, b);
-				return b;
-			}
-			break;
 		case DLT_IP_OVER_FC:
 			return gen_ipfchostop(eaddr, (int)q.dir);
 		default:
@@ -6715,7 +6761,7 @@
 		 * frame, so that 0 refers, for Ethernet LANE, to
 		 * the beginning of the destination address?
 		 */
-		s = gen_llprefixlen();
+		s = gen_abs_offset_varpart(&off_linkhdr);
 
 		/*
 		 * If "s" is non-null, it has code to arrange that the
@@ -6741,7 +6787,7 @@
 		 * into the X register and then added to the index).
 		 */
 		tmp = new_stmt(BPF_LD|BPF_IND|size);
-		tmp->s.k = off_ll;
+		tmp->s.k = off_linkhdr.constant_part;
 		sappend(s, tmp);
 		sappend(inst->s, s);
 		break;
@@ -6762,16 +6808,16 @@
 		 * XXX - are there any cases where we want
 		 * off_nl_nosnap?
 		 */
-		s = gen_off_macpl();
+		s = gen_abs_offset_varpart(&off_linkpl);
 
 		/*
 		 * If "s" is non-null, it has code to arrange that the
-		 * X register contains the offset of the MAC-layer
-		 * payload.  Add to it the offset computed into the
-		 * register specified by "index", and move that into
-		 * the X register.  Otherwise, just load into the X
-		 * register the offset computed into the register specified
-		 * by "index".
+		 * X register contains the variable part of the offset
+		 * of the link-layer payload.  Add to it the offset
+		 * computed into the register specified by "index",
+		 * and move that into the X register.  Otherwise, just
+		 * load into the X register the offset computed into
+		 * the register specified by "index".
 		 */
 		if (s != NULL) {
 			sappend(s, xfer_to_a(inst));
@@ -6783,17 +6829,12 @@
 		/*
 		 * Load the item at the sum of the offset we've put in the
 		 * X register, the offset of the start of the network
-		 * layer header from the beginning of the MAC-layer
-		 * payload, and the purported offset of the start of the
-		 * MAC-layer payload (which might be 0 if there's a
-		 * variable-length prefix before the link-layer header
-		 * or the link-layer header itself is variable-length;
-		 * the variable-length offset of the start of the
-		 * MAC-layer payload is what we put into the X register
-		 * and then added to the index).
+		 * layer header from the beginning of the link-layer
+		 * payload, and the constant part of the offset of the
+		 * start of the link-layer payload.
 		 */
 		tmp = new_stmt(BPF_LD|BPF_IND|size);
-		tmp->s.k = off_macpl + off_nl;
+		tmp->s.k = off_linkpl.constant_part + off_nl;
 		sappend(s, tmp);
 		sappend(inst->s, s);
 
@@ -6833,26 +6874,24 @@
 		s = gen_loadx_iphdrlen();
 
 		/*
-		 * The X register now contains the sum of the length
-		 * of any variable-length header preceding the link-layer
-		 * header, any variable-length link-layer header, and the
+		 * The X register now contains the sum of the variable
+		 * part of the offset of the link-layer payload and the
 		 * length of the network-layer header.
 		 *
 		 * Load into the A register the offset relative to
 		 * the beginning of the transport layer header,
 		 * add the X register to that, move that to the
 		 * X register, and load with an offset from the
-		 * X register equal to the offset of the network
-		 * layer header relative to the beginning of
-		 * the MAC-layer payload plus the fixed-length
-		 * portion of the offset of the MAC-layer payload
-		 * from the beginning of the raw packet data.
+		 * X register equal to the sum of the constant part of
+		 * the offset of the link-layer payload and the offset,
+		 * relative to the beginning of the link-layer payload,
+		 * of the network-layer header.
 		 */
 		sappend(s, xfer_to_a(inst));
 		sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
 		sappend(s, new_stmt(BPF_MISC|BPF_TAX));
 		sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
-		tmp->s.k = off_macpl + off_nl;
+		tmp->s.k = off_linkpl.constant_part + off_nl;
 		sappend(inst->s, s);
 
 		/*
@@ -7115,14 +7154,14 @@
 		abort();
 
 	case '=':
-		return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
+		return gen_cmp(OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
 
 	case '<':
-		b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
+		b = gen_cmp_lt(OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
 		return b;
 
 	case '>':
-		b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
+		b = gen_cmp_gt(OR_LINKHDR, (u_int)idx, BPF_B, (bpf_int32)val);
 		return b;
 
 	case '|':
@@ -7162,7 +7201,11 @@
 		case DLT_EN10MB:
 		case DLT_NETANALYZER:
 		case DLT_NETANALYZER_TRANSPARENT:
-			return gen_ehostop(ebroadcast, Q_DST);
+			b1 = gen_prevlinkhdr_check();
+			b0 = gen_ehostop(ebroadcast, Q_DST);
+			if (b1 != NULL)
+				gen_and(b1, b0);
+			return b0;
 		case DLT_FDDI:
 			return gen_fhostop(ebroadcast, Q_DST);
 		case DLT_IEEE802:
@@ -7175,25 +7218,6 @@
 			return gen_wlanhostop(ebroadcast, Q_DST);
 		case DLT_IP_OVER_FC:
 			return gen_ipfchostop(ebroadcast, Q_DST);
-		case DLT_SUNATM:
-			if (is_lane) {
-				/*
-				 * Check that the packet doesn't begin with an
-				 * LE Control marker.  (We've already generated
-				 * a test for LANE.)
-				 */
-				b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
-				    BPF_H, 0xFF00);
-				gen_not(b1);
-
-				/*
-				 * Now check the MAC address.
-				 */
-				b0 = gen_ehostop(ebroadcast, Q_DST);
-				gen_and(b1, b0);
-				return b0;
-			}
-			break;
 		default:
 			bpf_error("not a broadcast link");
 		}
@@ -7209,8 +7233,8 @@
 			bpf_error("netmask not known, so 'ip broadcast' not supported");
 		b0 = gen_linktype(ETHERTYPE_IP);
 		hostmask = ~netmask;
-		b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
-		b2 = gen_mcmp(OR_NET, 16, BPF_W,
+		b1 = gen_mcmp(OR_LINKPL, 16, BPF_W, (bpf_int32)0, hostmask);
+		b2 = gen_mcmp(OR_LINKPL, 16, BPF_W,
 			      (bpf_int32)(~0 & hostmask), hostmask);
 		gen_or(b1, b2);
 		gen_and(b0, b2);
@@ -7233,7 +7257,7 @@
 	register struct slist *s;
 
 	/* link[offset] & 1 != 0 */
-	s = gen_load_a(OR_LINK, offset, BPF_B);
+	s = gen_load_a(OR_LINKHDR, offset, BPF_B);
 	b0 = new_block(JMP(BPF_JSET));
 	b0->s.k = 1;
 	b0->stmts = s;
@@ -7259,8 +7283,12 @@
 		case DLT_EN10MB:
 		case DLT_NETANALYZER:
 		case DLT_NETANALYZER_TRANSPARENT:
+			b1 = gen_prevlinkhdr_check();
 			/* ether[0] & 1 != 0 */
-			return gen_mac_multicast(0);
+			b0 = gen_mac_multicast(0);
+			if (b1 != NULL)
+				gen_and(b1, b0);
+			return b0;
 		case DLT_FDDI:
 			/*
 			 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
@@ -7298,7 +7326,7 @@
 			 *
 			 * First, check for To DS set, i.e. "link[1] & 0x01".
 			 */
-			s = gen_load_a(OR_LINK, 1, BPF_B);
+			s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 			b1 = new_block(JMP(BPF_JSET));
 			b1->s.k = 0x01;	/* To DS */
 			b1->stmts = s;
@@ -7313,7 +7341,7 @@
 			 * Now, check for To DS not set, i.e. check
 			 * "!(link[1] & 0x01)".
 			 */
-			s = gen_load_a(OR_LINK, 1, BPF_B);
+			s = gen_load_a(OR_LINKHDR, 1, BPF_B);
 			b2 = new_block(JMP(BPF_JSET));
 			b2->s.k = 0x01;	/* To DS */
 			b2->stmts = s;
@@ -7335,7 +7363,7 @@
 			 * Now check for a data frame.
 			 * I.e, check "link[0] & 0x08".
 			 */
-			s = gen_load_a(OR_LINK, 0, BPF_B);
+			s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 			b1 = new_block(JMP(BPF_JSET));
 			b1->s.k = 0x08;
 			b1->stmts = s;
@@ -7350,7 +7378,7 @@
 			 * is a management frame.
 			 * I.e, check "!(link[0] & 0x08)".
 			 */
-			s = gen_load_a(OR_LINK, 0, BPF_B);
+			s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 			b2 = new_block(JMP(BPF_JSET));
 			b2->s.k = 0x08;
 			b2->stmts = s;
@@ -7377,7 +7405,7 @@
 			 *
 			 * I.e., check "!(link[0] & 0x04)".
 			 */
-			s = gen_load_a(OR_LINK, 0, BPF_B);
+			s = gen_load_a(OR_LINKHDR, 0, BPF_B);
 			b1 = new_block(JMP(BPF_JSET));
 			b1->s.k = 0x04;
 			b1->stmts = s;
@@ -7392,23 +7420,6 @@
 		case DLT_IP_OVER_FC:
 			b0 = gen_mac_multicast(2);
 			return b0;
-		case DLT_SUNATM:
-			if (is_lane) {
-				/*
-				 * Check that the packet doesn't begin with an
-				 * LE Control marker.  (We've already generated
-				 * a test for LANE.)
-				 */
-				b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
-				    BPF_H, 0xFF00);
-				gen_not(b1);
-
-				/* ether[off_mac] & 1 != 0 */
-				b0 = gen_mac_multicast(off_mac);
-				gen_and(b1, b0);
-				return b0;
-			}
-			break;
 		default:
 			break;
 		}
@@ -7417,13 +7428,13 @@
 
 	case Q_IP:
 		b0 = gen_linktype(ETHERTYPE_IP);
-		b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
+		b1 = gen_cmp_ge(OR_LINKPL, 16, BPF_B, (bpf_int32)224);
 		gen_and(b0, b1);
 		return b1;
 
 	case Q_IPV6:
 		b0 = gen_linktype(ETHERTYPE_IPV6);
-		b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
+		b1 = gen_cmp(OR_LINKPL, 24, BPF_B, (bpf_int32)255);
 		gen_and(b0, b1);
 		return b1;
 	}
@@ -7461,16 +7472,16 @@
 	case DLT_IPNET:
 		if (dir) {
 			/* match outgoing packets */
-			b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
+			b0 = gen_cmp(OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
 		} else {
 			/* match incoming packets */
-			b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
+			b0 = gen_cmp(OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
 		}
 		break;
 
 	case DLT_LINUX_SLL:
 		/* match outgoing packets */
-		b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
+		b0 = gen_cmp(OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
 		if (!dir) {
 			/* to filter on inbound traffic, invert the match */
 			gen_not(b0);
@@ -7479,7 +7490,7 @@
 
 #ifdef HAVE_NET_PFVAR_H
 	case DLT_PFLOG:
-		b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
+		b0 = gen_cmp(OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
 		    (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
 		break;
 #endif
@@ -7487,10 +7498,10 @@
 	case DLT_PPP_PPPD:
 		if (dir) {
 			/* match outgoing packets */
-			b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
+			b0 = gen_cmp(OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
 		} else {
 			/* match incoming packets */
-			b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
+			b0 = gen_cmp(OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
 		}
 		break;
 
@@ -7521,10 +7532,10 @@
 		 * the byte after the 3-byte magic number */
 		if (dir) {
 			/* match outgoing packets */
-			b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
+			b0 = gen_mcmp(OR_LINKHDR, 3, BPF_B, 0, 0x01);
 		} else {
 			/* match incoming packets */
-			b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
+			b0 = gen_mcmp(OR_LINKHDR, 3, BPF_B, 1, 0x01);
 		}
 		break;
 
@@ -7549,7 +7560,7 @@
 			/* NOTREACHED */
 		}
 		/* match outgoing packets */
-		b0 = gen_cmp(OR_LINK, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
+		b0 = gen_cmp(OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
 		             PACKET_OUTGOING);
 		if (!dir) {
 			/* to filter on inbound traffic, invert the match */
@@ -7584,7 +7595,7 @@
 		    len-1);
 		/* NOTREACHED */
 	}
-	b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
+	b0 = gen_bcmp(OR_LINKHDR, off, strlen(ifname), (const u_char *)ifname);
 	return (b0);
 }
 
@@ -7605,7 +7616,7 @@
 		/* NOTREACHED */
 	}
 
-	b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
+	b0 = gen_bcmp(OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
 	    strlen(ruleset), (const u_char *)ruleset);
 	return (b0);
 }
@@ -7621,7 +7632,7 @@
 		/* NOTREACHED */
 	}
 
-	b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
+	b0 = gen_cmp(OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
 		 (bpf_int32)rnr);
 	return (b0);
 }
@@ -7637,7 +7648,7 @@
 		/* NOTREACHED */
 	}
 
-	b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
+	b0 = gen_cmp(OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
 	    (bpf_int32)srnr);
 	return (b0);
 }
@@ -7653,7 +7664,7 @@
 		/* NOTREACHED */
 	}
 
-	b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
+	b0 = gen_cmp(OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
 	    (bpf_int32)reason);
 	return (b0);
 }
@@ -7669,7 +7680,7 @@
 		/* NOTREACHED */
 	}
 
-	b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
+	b0 = gen_cmp(OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
 	    (bpf_int32)action);
 	return (b0);
 }
@@ -7735,7 +7746,7 @@
 	case DLT_PRISM_HEADER:
 	case DLT_IEEE802_11_RADIO_AVS:
 	case DLT_IEEE802_11_RADIO:
-		b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
+		b0 = gen_mcmp(OR_LINKHDR, 0, BPF_B, (bpf_int32)type,
 		    (bpf_int32)mask);
 		break;
 
@@ -7765,7 +7776,7 @@
 		/* NOTREACHED */
 	}
 
-	b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
+	b0 = gen_mcmp(OR_LINKHDR, 1, BPF_B, (bpf_int32)fcdir,
 		(bpf_u_int32)IEEE80211_FC1_DIR_MASK);
 
 	return (b0);
@@ -7808,10 +7819,10 @@
 	switch (dir) {
 	/* src comes first, different from Ethernet */
 	case Q_SRC:
-		return gen_bcmp(OR_LINK, 0, 1, eaddr);
+		return gen_bcmp(OR_LINKHDR, 0, 1, eaddr);
 
 	case Q_DST:
-		return gen_bcmp(OR_LINK, 1, 1, eaddr);
+		return gen_bcmp(OR_LINKHDR, 1, 1, eaddr);
 
 	case Q_AND:
 		b0 = gen_ahostop(eaddr, Q_SRC);
@@ -7854,6 +7865,72 @@
 	/* NOTREACHED */
 }
 
+#if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
+static struct block *
+gen_vlan_bpf_extensions(int vlan_num)
+{
+        struct block *b0, *b1;
+        struct slist *s;
+
+        /* generate new filter code based on extracting packet
+         * metadata */
+        s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
+        s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
+
+        b0 = new_block(JMP(BPF_JEQ));
+        b0->stmts = s;
+        b0->s.k = 1;
+
+        if (vlan_num >= 0) {
+                s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
+                s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;
+
+                b1 = new_block(JMP(BPF_JEQ));
+                b1->stmts = s;
+                b1->s.k = (bpf_int32) vlan_num;
+
+                gen_and(b0,b1);
+                b0 = b1;
+        }
+
+        return b0;
+}
+#endif
+
+static struct block *
+gen_vlan_no_bpf_extensions(int vlan_num)
+{
+        struct block *b0, *b1;
+
+        /* check for VLAN, including QinQ */
+        b0 = gen_linktype(ETHERTYPE_8021Q);
+        b1 = gen_linktype(ETHERTYPE_8021QINQ);
+        gen_or(b0,b1);
+        b0 = b1;
+
+        /* If a specific VLAN is requested, check VLAN id */
+        if (vlan_num >= 0) {
+                b1 = gen_mcmp(OR_LINKPL, 0, BPF_H,
+                              (bpf_int32)vlan_num, 0x0fff);
+                gen_and(b0, b1);
+                b0 = b1;
+        }
+
+	/*
+	 * The payload follows the full header, including the
+	 * VLAN tags, so skip past this VLAN tag.
+	 */
+        off_linkpl.constant_part += 4;
+
+	/*
+	 * The link-layer type information follows the VLAN tags, so
+	 * skip past this VLAN tag.
+	 */
+        off_linktype.constant_part += 4;
+
+        return b0;
+}
+
 /*
  * support IEEE 802.1Q VLAN trunk over ethernet
  */
@@ -7861,7 +7938,7 @@
 gen_vlan(vlan_num)
 	int vlan_num;
 {
-	struct	block	*b0, *b1;
+	struct	block	*b0;
 
 	/* can't check for VLAN-encapsulated packets inside MPLS */
 	if (label_stack_depth > 0)
@@ -7898,35 +7975,34 @@
 	 * be done assuming a VLAN, even though the "or" could be viewed
 	 * as meaning "or, if this isn't a VLAN packet...".
 	 */
-	orig_nl = off_nl;
-
 	switch (linktype) {
 
 	case DLT_EN10MB:
 	case DLT_NETANALYZER:
 	case DLT_NETANALYZER_TRANSPARENT:
-		/* check for VLAN, including QinQ */
-		b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
-		    (bpf_int32)ETHERTYPE_8021Q);
-		b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
-		    (bpf_int32)ETHERTYPE_8021QINQ);
-		gen_or(b0,b1);
-		b0 = b1;
-
-		/* If a specific VLAN is requested, check VLAN id */
-		if (vlan_num >= 0) {
-			b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
-			    (bpf_int32)vlan_num, 0x0fff);
-			gen_and(b0, b1);
-			b0 = b1;
-		}
-
-		off_macpl += 4;
-		off_linktype += 4;
-#if 0
-		off_nl_nosnap += 4;
-		off_nl += 4;
+#if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
+		/* Verify that this is the outer part of the packet and
+		 * not encapsulated somehow. */
+		if (vlan_stack_depth == 0 && !off_linkhdr.is_variable &&
+		    off_linkhdr.constant_part ==
+		    off_outermostlinkhdr.constant_part) {
+			/*
+			 * Do we need special VLAN handling?
+			 */
+			if (bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
+				b0 = gen_vlan_bpf_extensions(vlan_num);
+			else
+				b0 = gen_vlan_no_bpf_extensions(vlan_num);
+		} else
 #endif
+			b0 = gen_vlan_no_bpf_extensions(vlan_num);
+                break;
+
+	case DLT_IEEE802_11:
+	case DLT_PRISM_HEADER:
+	case DLT_IEEE802_11_RADIO_AVS:
+	case DLT_IEEE802_11_RADIO:
+		b0 = gen_vlan_no_bpf_extensions(vlan_num);
 		break;
 
 	default:
@@ -7935,6 +8011,8 @@
 		/*NOTREACHED*/
 	}
 
+        vlan_stack_depth++;
+
 	return (b0);
 }
 
@@ -7945,46 +8023,33 @@
 gen_mpls(label_num)
 	int label_num;
 {
-	struct	block	*b0,*b1;
-
-	/*
-	 * Change the offsets to point to the type and data fields within
-	 * the MPLS packet.  Just increment the offsets, so that we
-	 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
-	 * capture packets with an outer label of 100000 and an inner
-	 * label of 1024.
-	 *
-	 * XXX - this is a bit of a kludge.  See comments in gen_vlan().
-	 */
-        orig_nl = off_nl;
+	struct	block	*b0, *b1;
 
         if (label_stack_depth > 0) {
             /* just match the bottom-of-stack bit clear */
-            b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
+            b0 = gen_mcmp(OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
         } else {
             /*
-             * Indicate that we're checking MPLS-encapsulated headers,
-             * to make sure higher level code generators don't try to
-             * match against IP-related protocols such as Q_ARP, Q_RARP
-             * etc.
+             * We're not in an MPLS stack yet, so check the link-layer
+             * type against MPLS.
              */
             switch (linktype) {
-                
+
             case DLT_C_HDLC: /* fall through */
             case DLT_EN10MB:
             case DLT_NETANALYZER:
             case DLT_NETANALYZER_TRANSPARENT:
                     b0 = gen_linktype(ETHERTYPE_MPLS);
                     break;
-                
+
             case DLT_PPP:
                     b0 = gen_linktype(PPP_MPLS_UCAST);
                     break;
-                
+
                     /* FIXME add other DLT_s ...
                      * for Frame-Relay/and ATM this may get messy due to SNAP headers
                      * leave it for now */
-                
+
             default:
                     bpf_error("no MPLS support for data link type %d",
                           linktype);
@@ -7997,12 +8062,26 @@
 	/* If a specific MPLS label is requested, check it */
 	if (label_num >= 0) {
 		label_num = label_num << 12; /* label is shifted 12 bits on the wire */
-		b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
+		b1 = gen_mcmp(OR_LINKPL, 0, BPF_W, (bpf_int32)label_num,
 		    0xfffff000); /* only compare the first 20 bits */
 		gen_and(b0, b1);
 		b0 = b1;
 	}
 
+        /*
+         * Change the offsets to point to the type and data fields within
+         * the MPLS packet.  Just increment the offsets, so that we
+         * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
+         * capture packets with an outer label of 100000 and an inner
+         * label of 1024.
+         *
+         * Increment the MPLS stack depth as well; this indicates that
+         * we're checking MPLS-encapsulated headers, to make sure higher
+         * level code generators don't try to match against IP-related
+         * protocols such as Q_ARP, Q_RARP etc.
+         *
+         * XXX - this is a bit of a kludge.  See comments in gen_vlan().
+         */
         off_nl_nosnap += 4;
         off_nl += 4;
         label_stack_depth++;
@@ -8030,6 +8109,14 @@
 	 */
 	b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
 
+	/* If a specific session is requested, check PPPoE session id */
+	if (sess_num >= 0) {
+		b1 = gen_mcmp(OR_LINKPL, 0, BPF_W,
+		    (bpf_int32)sess_num, 0x0000ffff);
+		gen_and(b0, b1);
+		b0 = b1;
+	}
+
 	/*
 	 * Change the offsets to point to the type and data fields within
 	 * the PPP packet, and note that this is PPPoE rather than
@@ -8058,20 +8145,7 @@
 	 * as all the "or ..." tests would be done assuming PPPoE, even
 	 * though the "or" could be viewed as meaning "or, if this isn't
 	 * a PPPoE packet...".
-	 */
-	orig_linktype = off_linktype;	/* save original values */
-	orig_nl = off_nl;
-	is_pppoes = 1;
-
-	/* If a specific session is requested, check PPPoE session id */
-	if (sess_num >= 0) {
-		b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W,
-		    (bpf_int32)sess_num, 0x0000ffff);
-		gen_and(b0, b1);
-		b0 = b1;
-	}
-
-	/*
+	 *
 	 * The "network-layer" protocol is PPPoE, which has a 6-byte
 	 * PPPoE header, followed by a PPP packet.
 	 *
@@ -8082,15 +8156,309 @@
 	 * link-layer payload, including any 802.2 LLC header, so
 	 * it's 6 bytes past off_nl.
 	 */
-	off_linktype = off_nl + 6;
+	PUSH_LINKHDR(DLT_PPP, off_linkpl.is_variable,
+	    off_linkpl.constant_part + off_nl + 6, /* 6 bytes past the PPPoE header */
+	    off_linkpl.reg);
 
-	/*
-	 * The network-layer offsets are relative to the beginning
-	 * of the MAC-layer payload; that's past the 6-byte
-	 * PPPoE header and the 2-byte PPP header.
-	 */
-	off_nl = 6+2;
-	off_nl_nosnap = 6+2;
+	off_linktype = off_linkhdr;
+	off_linkpl.constant_part = off_linkhdr.constant_part + 2;
+
+	off_nl = 0;
+	off_nl_nosnap = 0;	/* no 802.2 LLC */
+
+	return b0;
+}
+
+/* Check that this is Geneve and the VNI is correct if
+ * specified. Parameterized to handle both IPv4 and IPv6. */
+static struct block *
+gen_geneve_check(struct block *(*gen_portfn)(int, int, int),
+		 enum e_offrel offrel, int vni)
+{
+	struct block *b0, *b1;
+
+	b0 = gen_portfn(GENEVE_PORT, IPPROTO_UDP, Q_DST);
+
+	/* Check that we are operating on version 0. Otherwise, we
+	 * can't decode the rest of the fields. The version is 2 bits
+	 * in the first byte of the Geneve header. */
+	b1 = gen_mcmp(offrel, 8, BPF_B, (bpf_int32)0, 0xc0);
+	gen_and(b0, b1);
+	b0 = b1;
+
+	if (vni >= 0) {
+		vni <<= 8; /* VNI is in the upper 3 bytes */
+		b1 = gen_mcmp(offrel, 12, BPF_W, (bpf_int32)vni,
+			      0xffffff00);
+		gen_and(b0, b1);
+		b0 = b1;
+	}
+
+	return b0;
+}
+
+/* The IPv4 and IPv6 Geneve checks need to do two things:
+ * - Verify that this actually is Geneve with the right VNI.
+ * - Place the IP header length (plus variable link prefix if
+ *   needed) into register A to be used later to compute
+ *   the inner packet offsets. */
+static struct block *
+gen_geneve4(int vni)
+{
+	struct block *b0, *b1;
+	struct slist *s, *s1;
+
+	b0 = gen_geneve_check(gen_port, OR_TRAN_IPV4, vni);
+
+	/* Load the IP header length into A. */
+	s = gen_loadx_iphdrlen();
+
+	s1 = new_stmt(BPF_MISC|BPF_TXA);
+	sappend(s, s1);
+
+	/* Forcibly append these statements to the true condition
+	 * of the protocol check by creating a new block that is
+	 * always true and ANDing them. */
+	b1 = new_block(BPF_JMP|BPF_JEQ|BPF_X);
+	b1->stmts = s;
+	b1->s.k = 0;
+
+	gen_and(b0, b1);
+
+	return b1;
+}
+
+static struct block *
+gen_geneve6(int vni)
+{
+	struct block *b0, *b1;
+	struct slist *s, *s1;
+
+	b0 = gen_geneve_check(gen_port6, OR_TRAN_IPV6, vni);
+
+	/* Load the IP header length. We need to account for a
+	 * variable length link prefix if there is one. */
+	s = gen_abs_offset_varpart(&off_linkpl);
+	if (s) {
+		s1 = new_stmt(BPF_LD|BPF_IMM);
+		s1->s.k = 40;
+		sappend(s, s1);
+
+		s1 = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
+		s1->s.k = 0;
+		sappend(s, s1);
+	} else {
+		s = new_stmt(BPF_LD|BPF_IMM);
+		s->s.k = 40;;
+	}
+
+	/* Forcibly append these statements to the true condition
+	 * of the protocol check by creating a new block that is
+	 * always true and ANDing them. */
+	s1 = new_stmt(BPF_MISC|BPF_TAX);
+	sappend(s, s1);
+
+	b1 = new_block(BPF_JMP|BPF_JEQ|BPF_X);
+	b1->stmts = s;
+	b1->s.k = 0;
+
+	gen_and(b0, b1);
+
+	return b1;
+}
+
+/* We need to store three values based on the Geneve header::
+ * - The offset of the linktype.
+ * - The offset of the end of the Geneve header.
+ * - The offset of the end of the encapsulated MAC header. */
+static struct slist *
+gen_geneve_offsets(void)
+{
+	struct slist *s, *s1, *s_proto;
+
+	/* First we need to calculate the offset of the Geneve header
+	 * itself. This is composed of the IP header previously calculated
+	 * (include any variable link prefix) and stored in A plus the
+	 * fixed sized headers (fixed link prefix, MAC length, and UDP
+	 * header). */
+	s = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
+	s->s.k = off_linkpl.constant_part + off_nl + 8;
+
+	/* Stash this in X since we'll need it later. */
+	s1 = new_stmt(BPF_MISC|BPF_TAX);
+	sappend(s, s1);
+
+	/* The EtherType in Geneve is 2 bytes in. Calculate this and
+	 * store it. */
+	s1 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
+	s1->s.k = 2;
+	sappend(s, s1);
+
+	off_linktype.reg = alloc_reg();
+	off_linktype.is_variable = 1;
+	off_linktype.constant_part = 0;
+
+	s1 = new_stmt(BPF_ST);
+	s1->s.k = off_linktype.reg;
+	sappend(s, s1);
+
+	/* Load the Geneve option length and mask and shift to get the
+	 * number of bytes. It is stored in the first byte of the Geneve
+	 * header. */
+	s1 = new_stmt(BPF_LD|BPF_IND|BPF_B);
+	s1->s.k = 0;
+	sappend(s, s1);
+
+	s1 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
+	s1->s.k = 0x3f;
+	sappend(s, s1);
+
+	s1 = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
+	s1->s.k = 4;
+	sappend(s, s1);
+
+	/* Add in the rest of the Geneve base header. */
+	s1 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
+	s1->s.k = 8;
+	sappend(s, s1);
+
+	/* Add the Geneve header length to its offset and store. */
+	s1 = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
+	s1->s.k = 0;
+	sappend(s, s1);
+
+	/* Set the encapsulated type as Ethernet. Even though we may
+	 * not actually have Ethernet inside there are two reasons this
+	 * is useful:
+	 * - The linktype field is always in EtherType format regardless
+	 *   of whether it is in Geneve or an inner Ethernet frame.
+	 * - The only link layer that we have specific support for is
+	 *   Ethernet. We will confirm that the packet actually is
+	 *   Ethernet at runtime before executing these checks. */
+	PUSH_LINKHDR(DLT_EN10MB, 1, 0, alloc_reg());
+
+	s1 = new_stmt(BPF_ST);
+	s1->s.k = off_linkhdr.reg;
+	sappend(s, s1);
+
+	/* Calculate whether we have an Ethernet header or just raw IP/
+	 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
+	 * and linktype by 14 bytes so that the network header can be found
+	 * seamlessly. Otherwise, keep what we've calculated already. */
+
+	/* We have a bare jmp so we can't use the optimizer. */
+	no_optimize = 1;
+
+	/* Load the EtherType in the Geneve header, 2 bytes in. */
+	s1 = new_stmt(BPF_LD|BPF_IND|BPF_H);
+	s1->s.k = 2;
+	sappend(s, s1);
+
+	/* Load X with the end of the Geneve header. */
+	s1 = new_stmt(BPF_LDX|BPF_MEM);
+	s1->s.k = off_linkhdr.reg;
+	sappend(s, s1);
+
+	/* Check if the EtherType is Transparent Ethernet Bridging. At the
+	 * end of this check, we should have the total length in X. In
+	 * the non-Ethernet case, it's already there. */
+	s_proto = new_stmt(JMP(BPF_JEQ));
+	s_proto->s.k = ETHERTYPE_TEB;
+	sappend(s, s_proto);
+
+	s1 = new_stmt(BPF_MISC|BPF_TXA);
+	sappend(s, s1);
+	s_proto->s.jt = s1;
+
+	/* Since this is Ethernet, use the EtherType of the payload
+	 * directly as the linktype. Overwrite what we already have. */
+	s1 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
+	s1->s.k = 12;
+	sappend(s, s1);
+
+	s1 = new_stmt(BPF_ST);
+	s1->s.k = off_linktype.reg;
+	sappend(s, s1);
+
+	/* Advance two bytes further to get the end of the Ethernet
+	 * header. */
+	s1 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
+	s1->s.k = 2;
+	sappend(s, s1);
+
+	/* Move the result to X. */
+	s1 = new_stmt(BPF_MISC|BPF_TAX);
+	sappend(s, s1);
+
+	/* Store the final result of our linkpl calculation. */
+	off_linkpl.reg = alloc_reg();
+	off_linkpl.is_variable = 1;
+	off_linkpl.constant_part = 0;
+
+	s1 = new_stmt(BPF_STX);
+	s1->s.k = off_linkpl.reg;
+	sappend(s, s1);
+	s_proto->s.jf = s1;
+
+	off_nl = 0;
+
+	return s;
+}
+
+/* Check to see if this is a Geneve packet. */
+struct block *
+gen_geneve(int vni)
+{
+	struct block *b0, *b1;
+	struct slist *s;
+
+	b0 = gen_geneve4(vni);
+	b1 = gen_geneve6(vni);
+
+	gen_or(b0, b1);
+	b0 = b1;
+
+	/* Later filters should act on the payload of the Geneve frame,
+	 * update all of the header pointers. Attach this code so that
+	 * it gets executed in the event that the Geneve filter matches. */
+	s = gen_geneve_offsets();
+
+	b1 = gen_true();
+	sappend(s, b1->stmts);
+	b1->stmts = s;
+
+	gen_and(b0, b1);
+
+	is_geneve = 1;
+
+	return b1;
+}
+
+/* Check that the encapsulated frame has a link layer header
+ * for Ethernet filters. */
+static struct block *
+gen_geneve_ll_check()
+{
+	struct block *b0;
+	struct slist *s, *s1;
+
+	/* The easiest way to see if there is a link layer present
+	 * is to check if the link layer header and payload are not
+	 * the same. */
+
+	/* Geneve always generates pure variable offsets so we can
+	 * compare only the registers. */
+	s = new_stmt(BPF_LD|BPF_MEM);
+	s->s.k = off_linkhdr.reg;
+
+	s1 = new_stmt(BPF_LDX|BPF_MEM);
+	s1->s.k = off_linkpl.reg;
+	sappend(s, s1);
+
+	b0 = new_block(BPF_JMP|BPF_JEQ|BPF_X);
+	b0->stmts = s;
+	b0->s.k = 0;
+	gen_not(b0);
 
 	return b0;
 }
@@ -8111,7 +8479,7 @@
 			bpf_error("'vpi' supported only on raw ATM");
 		if (off_vpi == (u_int)-1)
 			abort();
-		b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
+		b0 = gen_ncmp(OR_LINKHDR, off_vpi, BPF_B, 0xffffffff, jtype,
 		    reverse, jvalue);
 		break;
 
@@ -8120,21 +8488,21 @@
 			bpf_error("'vci' supported only on raw ATM");
 		if (off_vci == (u_int)-1)
 			abort();
-		b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
+		b0 = gen_ncmp(OR_LINKHDR, off_vci, BPF_H, 0xffffffff, jtype,
 		    reverse, jvalue);
 		break;
 
 	case A_PROTOTYPE:
 		if (off_proto == (u_int)-1)
 			abort();	/* XXX - this isn't on FreeBSD */
-		b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
+		b0 = gen_ncmp(OR_LINKHDR, off_proto, BPF_B, 0x0f, jtype,
 		    reverse, jvalue);
 		break;
 
 	case A_MSGTYPE:
 		if (off_payload == (u_int)-1)
 			abort();
-		b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
+		b0 = gen_ncmp(OR_LINKHDR, off_payload + MSG_TYPE_POS, BPF_B,
 		    0xffffffff, jtype, reverse, jvalue);
 		break;
 
@@ -8143,7 +8511,7 @@
 			bpf_error("'callref' supported only on raw ATM");
 		if (off_proto == (u_int)-1)
 			abort();
-		b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
+		b0 = gen_ncmp(OR_LINKHDR, off_proto, BPF_B, 0xffffffff,
 		    jtype, reverse, jvalue);
 		break;
 
@@ -8227,16 +8595,13 @@
 		 * the offsets appropriately for LANE-encapsulated
 		 * Ethernet.
 		 *
-		 * "off_mac" is the offset of the Ethernet header,
-		 * which is 2 bytes past the ATM pseudo-header
-		 * (skipping the pseudo-header and 2-byte LE Client
-		 * field).  The other offsets are Ethernet offsets
-		 * relative to "off_mac".
+		 * We assume LANE means Ethernet, not Token Ring.
 		 */
-		is_lane = 1;
-		off_mac = off_payload + 2;	/* MAC header */
-		off_linktype = off_mac + 12;
-		off_macpl = off_mac + 14;	/* Ethernet */
+		PUSH_LINKHDR(DLT_EN10MB, 0,
+		    off_payload + 2,	/* Ethernet header */
+		    -1);
+		off_linktype.constant_part = off_linkhdr.constant_part + 12;
+		off_linkpl.constant_part = off_linkhdr.constant_part + 14;	/* Ethernet */
 		off_nl = 0;			/* Ethernet II */
 		off_nl_nosnap = 3;		/* 802.3+802.2 */
 		break;
@@ -8246,7 +8611,7 @@
 		if (!is_atm)
 			bpf_error("'llc' supported only on raw ATM");
 		b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
-		is_lane = 0;
+		linktype = prevlinktype;
 		break;
 
 	default:
@@ -8255,7 +8620,7 @@
 	return b1;
 }
 
-/* 
+/*
  * Filtering for MTP2 messages based on li value
  * FISU, length is null
  * LSSU, length is 1 or 2