]> The Tcpdump Group git mirrors - libpcap/blobdiff - gencode.c
Report an error for MPLS labels that don't fit in 20 bits.
[libpcap] / gencode.c
index 19a859951d2aaee76afa68533e7676ad9eb48479..09f72d437cb17b9aaa85d8b61f3a0474c57d9009 100644 (file)
--- a/gencode.c
+++ b/gencode.c
@@ -50,6 +50,8 @@
 
 #include "pcap-int.h"
 
+#include "extract.h"
+
 #include "ethertype.h"
 #include "nlpid.h"
 #include "llc.h"
@@ -276,6 +278,13 @@ struct _compiler_state {
         */
        struct addrinfo *ai;
 
+       /*
+        * Another thing that's allocated is the result of pcap_ether_aton();
+        * it must be freed with free().  This variable points to any
+        * address that would need to be freed.
+        */
+       u_char *e;
+
        /*
         * Various code constructs need to know the layout of the packet.
         * These values give the necessary offsets from the beginning
@@ -418,9 +427,28 @@ struct _compiler_state {
 };
 
 void PCAP_NORETURN
-bpf_syntax_error(compiler_state_t *cstate, const char *msg)
+bpf_parser_error(compiler_state_t *cstate, const char *msg)
 {
-       bpf_error(cstate, "syntax error in filter expression: %s", msg);
+       bpf_error(cstate, "can't parse filter expression: %s", msg);
+       /* NOTREACHED */
+}
+
+/*
+ * For use by the optimizer, which needs to do its *own* cleanup before
+ * delivering a longjmp-based exception.
+ */
+void
+bpf_vset_error(compiler_state_t *cstate, const char *fmt, va_list ap)
+{
+       if (cstate->bpf_pcap != NULL)
+               (void)pcap_vsnprintf(pcap_geterr(cstate->bpf_pcap),
+                   PCAP_ERRBUF_SIZE, fmt, ap);
+}
+
+void PCAP_NORETURN
+bpf_abort_compilation(compiler_state_t *cstate)
+{
+       longjmp(cstate->top_ctx, 1);
        /* NOTREACHED */
 }
 
@@ -431,11 +459,9 @@ bpf_error(compiler_state_t *cstate, const char *fmt, ...)
        va_list ap;
 
        va_start(ap, fmt);
-       if (cstate->bpf_pcap != NULL)
-               (void)pcap_vsnprintf(pcap_geterr(cstate->bpf_pcap),
-                   PCAP_ERRBUF_SIZE, fmt, ap);
+       bpf_vset_error(cstate, fmt, ap);
        va_end(ap);
-       longjmp(cstate->top_ctx, 1);
+       bpf_abort_compilation(cstate);
        /* NOTREACHED */
 }
 
@@ -610,7 +636,7 @@ sdup(compiler_state_t *cstate, const char *s)
        size_t n = strlen(s) + 1;
        char *cp = newchunk(cstate, n);
 
-       strlcpy(cp, s, n);
+       pcap_strlcpy(cp, s, n);
        return (cp);
 }
 
@@ -662,7 +688,7 @@ pcap_compile(pcap_t *p, struct bpf_program *program,
        compiler_state_t cstate;
        const char * volatile xbuf = buf;
        yyscan_t scanner = NULL;
-       YY_BUFFER_STATE in_buffer = NULL;
+       volatile YY_BUFFER_STATE in_buffer = NULL;
        u_int len;
        int  rc;
 
@@ -708,6 +734,7 @@ pcap_compile(pcap_t *p, struct bpf_program *program,
 #ifdef INET6
        cstate.ai = NULL;
 #endif
+       cstate.e = NULL;
        cstate.ic.root = NULL;
        cstate.ic.cur_mark = 0;
        cstate.bpf_pcap = p;
@@ -718,6 +745,8 @@ pcap_compile(pcap_t *p, struct bpf_program *program,
                if (cstate.ai != NULL)
                        freeaddrinfo(cstate.ai);
 #endif
+               if (cstate.e != NULL)
+                       free(cstate.e);
                rc = -1;
                goto quit;
        }
@@ -975,13 +1004,22 @@ gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
 {
        register struct block *b, *tmp;
 
+       /*
+        * XXX - the actual *instructions* do unsigned comparisons on
+        * most platforms, and the load instructions don't do sign
+        * extension, so gen_cmp() should really take an unsigned
+        * value argument.
+        *
+        * As the load instructons also don't do sign-extension, we
+        * fetch the values from the byte array as unsigned.  We don't
+        * want to use the signed versions of the extract calls.
+        */
        b = NULL;
        while (size >= 4) {
                register const u_char *p = &v[size - 4];
-               bpf_int32 w = ((bpf_int32)p[0] << 24) |
-                   ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
 
-               tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W, w);
+               tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W,
+                   (bpf_int32)EXTRACT_BE_U_4(p));
                if (b != NULL)
                        gen_and(b, tmp);
                b = tmp;
@@ -989,9 +1027,9 @@ gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
        }
        while (size >= 2) {
                register const u_char *p = &v[size - 2];
-               bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
 
-               tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H, w);
+               tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H,
+                   (bpf_int32)EXTRACT_BE_U_2(p));
                if (b != NULL)
                        gen_and(b, tmp);
                b = tmp;
@@ -1330,13 +1368,20 @@ init_linktype(compiler_state_t *cstate, pcap_t *p)
                cstate->off_nl_nosnap = 0;      /* no 802.2 LLC */
                break;
 
-       case DLT_LINUX_SLL:     /* fake header for Linux cooked socket */
+       case DLT_LINUX_SLL:     /* fake header for Linux cooked socket v1 */
                cstate->off_linktype.constant_part = 14;
                cstate->off_linkpl.constant_part = 16;
                cstate->off_nl = 0;
                cstate->off_nl_nosnap = 0;      /* no 802.2 LLC */
                break;
 
+       case DLT_LINUX_SLL2:    /* fake header for Linux cooked socket v2 */
+               cstate->off_linktype.constant_part = 0;
+               cstate->off_linkpl.constant_part = 20;
+               cstate->off_nl = 0;
+               cstate->off_nl_nosnap = 0;      /* no 802.2 LLC */
+               break;
+
        case DLT_LTALK:
                /*
                 * LocalTalk does have a 1-byte type field in the LLAP header,
@@ -3860,8 +3905,8 @@ gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
                gen_and(b0, b1);
                return b1;
 
-       case Q_OR:
        case Q_DEFAULT:
+       case Q_OR:
                b0 = gen_hostop(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
                b1 = gen_hostop(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
                gen_or(b0, b1);
@@ -3925,8 +3970,8 @@ gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
                gen_and(b0, b1);
                return b1;
 
-       case Q_OR:
        case Q_DEFAULT:
+       case Q_OR:
                b0 = gen_hostop6(cstate, addr, mask, Q_SRC, proto, src_off, dst_off);
                b1 = gen_hostop6(cstate, addr, mask, Q_DST, proto, src_off, dst_off);
                gen_or(b0, b1);
@@ -4426,6 +4471,68 @@ gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
                gen_and(b1, b0);
                return b0;
 
+       case Q_AND:
+               b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
+               b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
+               gen_and(b0, b1);
+               return b1;
+
+       case Q_DEFAULT:
+       case Q_OR:
+               b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
+               b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
+               gen_or(b0, b1);
+               return b1;
+
+       /*
+        * XXX - add BSSID keyword?
+        */
+       case Q_ADDR1:
+               return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
+
+       case Q_ADDR2:
+               /*
+                * Not present in CTS or ACK control frames.
+                */
+               b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
+                       IEEE80211_FC0_TYPE_MASK);
+               gen_not(b0);
+               b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
+                       IEEE80211_FC0_SUBTYPE_MASK);
+               gen_not(b1);
+               b2 = gen_mcmp(cstate, 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(cstate, OR_LINKHDR, 10, 6, eaddr);
+               gen_and(b2, b1);
+               return b1;
+
+       case Q_ADDR3:
+               /*
+                * Not present in control frames.
+                */
+               b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
+                       IEEE80211_FC0_TYPE_MASK);
+               gen_not(b0);
+               b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
+               gen_and(b0, b1);
+               return b1;
+
+       case Q_ADDR4:
+               /*
+                * Present only if the direction mask has both "From DS"
+                * and "To DS" set.  Neither control frames nor management
+                * frames should have both of those set, so we don't
+                * check the frame type.
+                */
+               b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
+                       IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
+               b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
+               gen_and(b0, b1);
+               return b1;
+
        case Q_RA:
                /*
                 * Not present in management frames; addr1 in other
@@ -4496,68 +4603,6 @@ gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
                b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
                gen_and(b2, b1);
                return b1;
-
-       /*
-        * XXX - add BSSID keyword?
-        */
-       case Q_ADDR1:
-               return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
-
-       case Q_ADDR2:
-               /*
-                * Not present in CTS or ACK control frames.
-                */
-               b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
-                       IEEE80211_FC0_TYPE_MASK);
-               gen_not(b0);
-               b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
-                       IEEE80211_FC0_SUBTYPE_MASK);
-               gen_not(b1);
-               b2 = gen_mcmp(cstate, 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(cstate, OR_LINKHDR, 10, 6, eaddr);
-               gen_and(b2, b1);
-               return b1;
-
-       case Q_ADDR3:
-               /*
-                * Not present in control frames.
-                */
-               b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
-                       IEEE80211_FC0_TYPE_MASK);
-               gen_not(b0);
-               b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
-               gen_and(b0, b1);
-               return b1;
-
-       case Q_ADDR4:
-               /*
-                * Present only if the direction mask has both "From DS"
-                * and "To DS" set.  Neither control frames nor management
-                * frames should have both of those set, so we don't
-                * check the frame type.
-                */
-               b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
-                       IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
-               b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
-               gen_and(b0, b1);
-               return b1;
-
-       case Q_AND:
-               b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
-               b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
-               gen_and(b0, b1);
-               return b1;
-
-       case Q_DEFAULT:
-       case Q_OR:
-               b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
-               b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
-               gen_or(b0, b1);
-               return b1;
        }
        abort();
        /* NOTREACHED */
@@ -4665,16 +4710,37 @@ gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
                gen_and(b0, b1);
                return b1;
 
-       case Q_OR:
        case Q_DEFAULT:
+       case Q_OR:
                /* Inefficient because we do our Calvinball dance twice */
                b0 = gen_dnhostop(cstate, addr, Q_SRC);
                b1 = gen_dnhostop(cstate, addr, Q_DST);
                gen_or(b0, b1);
                return b1;
 
-       case Q_ISO:
-               bpf_error(cstate, "ISO host filtering not implemented");
+       case Q_ADDR1:
+               bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
+               break;
+
+       case Q_ADDR2:
+               bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
+               break;
+
+       case Q_ADDR3:
+               bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
+               break;
+
+       case Q_ADDR4:
+               bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
+               break;
+
+       case Q_RA:
+               bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
+               break;
+
+       case Q_TA:
+               bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
+               break;
 
        default:
                abort();
@@ -4769,6 +4835,9 @@ gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
                }
                return b0;
 
+       case Q_LINK:
+               bpf_error(cstate, "link-layer modifier applied to %s", typestr);
+
        case Q_IP:
                return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
 
@@ -4778,12 +4847,12 @@ gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
        case Q_ARP:
                return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
 
-       case Q_TCP:
-               bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
-
        case Q_SCTP:
                bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
 
+       case Q_TCP:
+               bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
+
        case Q_UDP:
                bpf_error(cstate, "'udp' modifier applied to %s", typestr);
 
@@ -4796,36 +4865,24 @@ gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
        case Q_IGRP:
                bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
 
-       case Q_PIM:
-               bpf_error(cstate, "'pim' modifier applied to %s", typestr);
-
-       case Q_VRRP:
-               bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
-
-       case Q_CARP:
-               bpf_error(cstate, "'carp' modifier applied to %s", typestr);
-
        case Q_ATALK:
-               bpf_error(cstate, "ATALK host filtering not implemented");
-
-       case Q_AARP:
-               bpf_error(cstate, "AARP host filtering not implemented");
+               bpf_error(cstate, "AppleTalk host filtering not implemented");
 
        case Q_DECNET:
                return gen_dnhostop(cstate, addr, dir);
 
-       case Q_SCA:
-               bpf_error(cstate, "SCA host filtering not implemented");
-
        case Q_LAT:
                bpf_error(cstate, "LAT host filtering not implemented");
 
-       case Q_MOPDL:
-               bpf_error(cstate, "MOPDL host filtering not implemented");
+       case Q_SCA:
+               bpf_error(cstate, "SCA host filtering not implemented");
 
        case Q_MOPRC:
                bpf_error(cstate, "MOPRC host filtering not implemented");
 
+       case Q_MOPDL:
+               bpf_error(cstate, "MOPDL host filtering not implemented");
+
        case Q_IPV6:
                bpf_error(cstate, "'ip6' modifier applied to ip host");
 
@@ -4838,6 +4895,15 @@ gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
        case Q_ESP:
                bpf_error(cstate, "'esp' modifier applied to %s", typestr);
 
+       case Q_PIM:
+               bpf_error(cstate, "'pim' modifier applied to %s", typestr);
+
+       case Q_VRRP:
+               bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
+
+       case Q_AARP:
+               bpf_error(cstate, "AARP host filtering not implemented");
+
        case Q_ISO:
                bpf_error(cstate, "ISO host filtering not implemented");
 
@@ -4859,9 +4925,33 @@ gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
        case Q_NETBEUI:
                bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
 
+       case Q_ISIS_L1:
+               bpf_error(cstate, "'l1' modifier applied to %s", typestr);
+
+       case Q_ISIS_L2:
+               bpf_error(cstate, "'l2' modifier applied to %s", typestr);
+
+       case Q_ISIS_IIH:
+               bpf_error(cstate, "'iih' modifier applied to %s", typestr);
+
+       case Q_ISIS_SNP:
+               bpf_error(cstate, "'snp' modifier applied to %s", typestr);
+
+       case Q_ISIS_CSNP:
+               bpf_error(cstate, "'csnp' modifier applied to %s", typestr);
+
+       case Q_ISIS_PSNP:
+               bpf_error(cstate, "'psnp' modifier applied to %s", typestr);
+
+       case Q_ISIS_LSP:
+               bpf_error(cstate, "'lsp' modifier applied to %s", typestr);
+
        case Q_RADIO:
                bpf_error(cstate, "'radio' modifier applied to %s", typestr);
 
+       case Q_CARP:
+               bpf_error(cstate, "'carp' modifier applied to %s", typestr);
+
        default:
                abort();
        }
@@ -4898,88 +4988,109 @@ gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
                bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
 
        case Q_SCTP:
-               bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr);
 
        case Q_TCP:
-               bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr);
 
        case Q_UDP:
-               bpf_error(cstate, "'udp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr);
 
        case Q_ICMP:
-               bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr);
 
        case Q_IGMP:
-               bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr);
 
        case Q_IGRP:
-               bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
-
-       case Q_PIM:
-               bpf_error(cstate, "'pim' modifier applied to %s", typestr);
-
-       case Q_VRRP:
-               bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
-
-       case Q_CARP:
-               bpf_error(cstate, "'carp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr);
 
        case Q_ATALK:
-               bpf_error(cstate, "ATALK host filtering not implemented");
-
-       case Q_AARP:
-               bpf_error(cstate, "AARP host filtering not implemented");
+               bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr);
 
        case Q_DECNET:
                bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
 
-       case Q_SCA:
-               bpf_error(cstate, "SCA host filtering not implemented");
-
        case Q_LAT:
-               bpf_error(cstate, "LAT host filtering not implemented");
+               bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr);
 
-       case Q_MOPDL:
-               bpf_error(cstate, "MOPDL host filtering not implemented");
+       case Q_SCA:
+               bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr);
 
        case Q_MOPRC:
-               bpf_error(cstate, "MOPRC host filtering not implemented");
+               bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr);
+
+       case Q_MOPDL:
+               bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);
 
        case Q_IPV6:
                return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
 
        case Q_ICMPV6:
-               bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
+               bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);
 
        case Q_AH:
-               bpf_error(cstate, "'ah' modifier applied to %s", typestr);
+               bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr);
 
        case Q_ESP:
-               bpf_error(cstate, "'esp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr);
+
+       case Q_PIM:
+               bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr);
+
+       case Q_VRRP:
+               bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr);
+
+       case Q_AARP:
+               bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr);
 
        case Q_ISO:
-               bpf_error(cstate, "ISO host filtering not implemented");
+               bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr);
 
        case Q_ESIS:
-               bpf_error(cstate, "'esis' modifier applied to %s", typestr);
+               bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr);
 
        case Q_ISIS:
-               bpf_error(cstate, "'isis' modifier applied to %s", typestr);
+               bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr);
 
        case Q_CLNP:
-               bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr);
 
        case Q_STP:
-               bpf_error(cstate, "'stp' modifier applied to %s", typestr);
+               bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr);
 
        case Q_IPX:
-               bpf_error(cstate, "IPX host filtering not implemented");
+               bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr);
 
        case Q_NETBEUI:
-               bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
+               bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr);
+
+       case Q_ISIS_L1:
+               bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr);
+
+       case Q_ISIS_L2:
+               bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr);
+
+       case Q_ISIS_IIH:
+               bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr);
+
+       case Q_ISIS_SNP:
+               bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr);
+
+       case Q_ISIS_CSNP:
+               bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr);
+
+       case Q_ISIS_PSNP:
+               bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr);
+
+       case Q_ISIS_LSP:
+               bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr);
 
        case Q_RADIO:
-               bpf_error(cstate, "'radio' modifier applied to %s", typestr);
+               bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr);
+
+       case Q_CARP:
+               bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr);
 
        default:
                abort();
@@ -5391,17 +5502,41 @@ gen_portop(compiler_state_t *cstate, int port, int proto, int dir)
                b1 = gen_portatom(cstate, 2, (bpf_int32)port);
                break;
 
-       case Q_OR:
-       case Q_DEFAULT:
+       case Q_AND:
                tmp = gen_portatom(cstate, 0, (bpf_int32)port);
                b1 = gen_portatom(cstate, 2, (bpf_int32)port);
-               gen_or(tmp, b1);
+               gen_and(tmp, b1);
                break;
 
-       case Q_AND:
+       case Q_DEFAULT:
+       case Q_OR:
                tmp = gen_portatom(cstate, 0, (bpf_int32)port);
                b1 = gen_portatom(cstate, 2, (bpf_int32)port);
-               gen_and(tmp, b1);
+               gen_or(tmp, b1);
+               break;
+
+       case Q_ADDR1:
+               bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");
+               break;
+
+       case Q_ADDR2:
+               bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");
+               break;
+
+       case Q_ADDR3:
+               bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");
+               break;
+
+       case Q_ADDR4:
+               bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");
+               break;
+
+       case Q_RA:
+               bpf_error(cstate, "'ra' is not a valid qualifier for ports");
+               break;
+
+       case Q_TA:
+               bpf_error(cstate, "'ta' is not a valid qualifier for ports");
                break;
 
        default:
@@ -5476,17 +5611,17 @@ gen_portop6(compiler_state_t *cstate, int port, int proto, int dir)
                b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
                break;
 
-       case Q_OR:
-       case Q_DEFAULT:
+       case Q_AND:
                tmp = gen_portatom6(cstate, 0, (bpf_int32)port);
                b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
-               gen_or(tmp, b1);
+               gen_and(tmp, b1);
                break;
 
-       case Q_AND:
+       case Q_DEFAULT:
+       case Q_OR:
                tmp = gen_portatom6(cstate, 0, (bpf_int32)port);
                b1 = gen_portatom6(cstate, 2, (bpf_int32)port);
-               gen_and(tmp, b1);
+               gen_or(tmp, b1);
                break;
 
        default:
@@ -5573,17 +5708,41 @@ gen_portrangeop(compiler_state_t *cstate, int port1, int port2, int proto,
                b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
                break;
 
-       case Q_OR:
-       case Q_DEFAULT:
+       case Q_AND:
                tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
                b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
-               gen_or(tmp, b1);
+               gen_and(tmp, b1);
                break;
 
-       case Q_AND:
+       case Q_DEFAULT:
+       case Q_OR:
                tmp = gen_portrangeatom(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
                b1 = gen_portrangeatom(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
-               gen_and(tmp, b1);
+               gen_or(tmp, b1);
+               break;
+
+       case Q_ADDR1:
+               bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");
+               break;
+
+       case Q_ADDR2:
+               bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");
+               break;
+
+       case Q_ADDR3:
+               bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");
+               break;
+
+       case Q_ADDR4:
+               bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");
+               break;
+
+       case Q_RA:
+               bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");
+               break;
+
+       case Q_TA:
+               bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");
                break;
 
        default:
@@ -5669,17 +5828,17 @@ gen_portrangeop6(compiler_state_t *cstate, int port1, int port2, int proto,
                b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
                break;
 
-       case Q_OR:
-       case Q_DEFAULT:
+       case Q_AND:
                tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
                b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
-               gen_or(tmp, b1);
+               gen_and(tmp, b1);
                break;
 
-       case Q_AND:
+       case Q_DEFAULT:
+       case Q_OR:
                tmp = gen_portrangeatom6(cstate, 0, (bpf_int32)port1, (bpf_int32)port2);
                b1 = gen_portrangeatom6(cstate, 2, (bpf_int32)port1, (bpf_int32)port2);
-               gen_and(tmp, b1);
+               gen_or(tmp, b1);
                break;
 
        default:
@@ -6174,13 +6333,13 @@ gen_proto(compiler_state_t *cstate, int v, int proto, int dir)
                         */
                        b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
                        /* OSI in C-HDLC is stuffed with a fudge byte */
-                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, (long)v);
+                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, (bpf_int32)v);
                        gen_and(b0, b1);
                        return b1;
 
                default:
                        b0 = gen_linktype(cstate, LLCSAP_ISONS);
-                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, (long)v);
+                       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, (bpf_int32)v);
                        gen_and(b0, b1);
                        return b1;
                }
@@ -6191,7 +6350,7 @@ gen_proto(compiler_state_t *cstate, int v, int proto, int dir)
                 * 4 is the offset of the PDU type relative to the IS-IS
                 * header.
                 */
-               b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, (long)v);
+               b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, (bpf_int32)v);
                gen_and(b0, b1);
                return b1;
 
@@ -6829,36 +6988,49 @@ gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
 #endif /*INET6*/
 
 struct block *
-gen_ecode(compiler_state_t *cstate, const u_char *eaddr, struct qual q)
+gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)
 {
        struct block *b, *tmp;
 
        if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
+               cstate->e = pcap_ether_aton(s);
+               if (cstate->e == NULL)
+                       bpf_error(cstate, "malloc");
                switch (cstate->linktype) {
                case DLT_EN10MB:
                case DLT_NETANALYZER:
                case DLT_NETANALYZER_TRANSPARENT:
                        tmp = gen_prevlinkhdr_check(cstate);
-                       b = gen_ehostop(cstate, eaddr, (int)q.dir);
+                       b = gen_ehostop(cstate, cstate->e, (int)q.dir);
                        if (tmp != NULL)
                                gen_and(tmp, b);
-                       return b;
+                       break;
                case DLT_FDDI:
-                       return gen_fhostop(cstate, eaddr, (int)q.dir);
+                       b = gen_fhostop(cstate, cstate->e, (int)q.dir);
+                       break;
                case DLT_IEEE802:
-                       return gen_thostop(cstate, eaddr, (int)q.dir);
+                       b = gen_thostop(cstate, cstate->e, (int)q.dir);
+                       break;
                case DLT_IEEE802_11:
                case DLT_PRISM_HEADER:
                case DLT_IEEE802_11_RADIO_AVS:
                case DLT_IEEE802_11_RADIO:
                case DLT_PPI:
-                       return gen_wlanhostop(cstate, eaddr, (int)q.dir);
+                       b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);
+                       break;
                case DLT_IP_OVER_FC:
-                       return gen_ipfchostop(cstate, eaddr, (int)q.dir);
+                       b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);
+                       break;
                default:
+                       free(cstate->e);
+                       cstate->e = NULL;
                        bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
+                       /* NOTREACHED */
                        break;
                }
+               free(cstate->e);
+               cstate->e = NULL;
+               return (b);
        }
        bpf_error(cstate, "ethernet address used in non-ether expression");
        /* NOTREACHED */
@@ -7283,6 +7455,9 @@ gen_arth(compiler_state_t *cstate, int code, struct arth *a0,
        /*
         * Disallow division by, or modulus by, zero; we do this here
         * so that it gets done even if the optimizer is disabled.
+        *
+        * Also disallow shifts by a value greater than 31; we do this
+        * here, for the same reason.
         */
        if (code == BPF_DIV) {
                if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
@@ -7290,6 +7465,15 @@ gen_arth(compiler_state_t *cstate, int code, struct arth *a0,
        } else if (code == BPF_MOD) {
                if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
                        bpf_error(cstate, "modulus by zero");
+       } else if (code == BPF_LSH || code == BPF_RSH) {
+               /*
+                * XXX - we need to make up our minds as to what integers
+                * are signed and what integers are unsigned in BPF programs
+                * and in our IR.
+                */
+               if (a1->s->s.code == (BPF_LD|BPF_IMM) &&
+                   (a1->s->s.k < 0 || a1->s->s.k > 31))
+                       bpf_error(cstate, "shift by more than 31 bits");
        }
        s0 = xfer_to_x(cstate, a1);
        s1 = xfer_to_a(cstate, a0);
@@ -7733,6 +7917,15 @@ gen_inbound(compiler_state_t *cstate, int dir)
                }
                break;
 
+       case DLT_LINUX_SLL2:
+               /* match outgoing packets */
+               b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);
+               if (!dir) {
+                       /* to filter on inbound traffic, invert the match */
+                       gen_not(b0);
+               }
+               break;
+
 #ifdef HAVE_NET_PFVAR_H
        case DLT_PFLOG:
                b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
@@ -8031,16 +8224,24 @@ gen_p80211_fcdir(compiler_state_t *cstate, int fcdir)
 }
 
 struct block *
-gen_acode(compiler_state_t *cstate, const u_char *eaddr, struct qual q)
+gen_acode(compiler_state_t *cstate, const char *s, struct qual q)
 {
+       struct block *b;
+
        switch (cstate->linktype) {
 
        case DLT_ARCNET:
        case DLT_ARCNET_LINUX:
                if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
-                   q.proto == Q_LINK)
-                       return (gen_ahostop(cstate, eaddr, (int)q.dir));
-               else {
+                   q.proto == Q_LINK) {
+                       cstate->e = pcap_ether_aton(s);
+                       if (cstate->e == NULL)
+                               bpf_error(cstate, "malloc");
+                       b = gen_ahostop(cstate, cstate->e, (int)q.dir);
+                       free(cstate->e);
+                       cstate->e = NULL;
+                       return (b);
+               } else {
                        bpf_error(cstate, "ARCnet address used in non-arc expression");
                        /* NOTREACHED */
                }
@@ -8151,7 +8352,7 @@ gen_vlan_no_bpf_extensions(compiler_state_t *cstate, int vlan_num)
        return b0;
 }
 
-#if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
+#if defined(SKF_AD_VLAN_TAG_PRESENT)
 /* add v to variable part of off */
 static void
 gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off, int v, struct slist *s)
@@ -8218,12 +8419,15 @@ gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
        sappend(s, s2);
        sjeq->s.jt = s2;
 
-       /* jump to the test in b_vid (bypass loading VID from packet data) */
+       /* Jump to the test in b_vid. We need to jump one instruction before
+        * the end of the b_vid block so that we only skip loading the TCI
+        * from packet data and not the 'and' instruction extractging VID.
+        */
        cnt = 0;
        for (s2 = b_vid->stmts; s2; s2 = s2->next)
                cnt++;
        s2 = new_stmt(cstate, JMP(BPF_JA));
-       s2->s.k = cnt;
+       s2->s.k = cnt - 1;
        sappend(s, s2);
 
        /* insert our statements at the beginning of b_vid */
@@ -8330,7 +8534,7 @@ gen_vlan(compiler_state_t *cstate, int vlan_num)
        case DLT_EN10MB:
        case DLT_NETANALYZER:
        case DLT_NETANALYZER_TRANSPARENT:
-#if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
+#if defined(SKF_AD_VLAN_TAG_PRESENT)
                /* Verify that this is the outer part of the packet and
                 * not encapsulated somehow. */
                if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
@@ -8370,7 +8574,7 @@ gen_vlan(compiler_state_t *cstate, int vlan_num)
  * support for MPLS
  */
 struct block *
-gen_mpls(compiler_state_t *cstate, int label_num)
+gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num, int has_label_num)
 {
        struct  block   *b0, *b1;
 
@@ -8408,7 +8612,11 @@ gen_mpls(compiler_state_t *cstate, int label_num)
         }
 
        /* If a specific MPLS label is requested, check it */
-       if (label_num >= 0) {
+       if (has_label_num) {
+               if (label_num > 0xFFFFF) {
+                       bpf_error(cstate, "MPLS label %u greater than maximum %u",
+                           label_num, 0xFFFFF);
+               }
                label_num = label_num << 12; /* label is shifted 12 bits on the wire */
                b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, (bpf_int32)label_num,
                    0xfffff000); /* only compare the first 20 bits */