1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.53 2007/09/12 19:17:24 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/if_var.h>
80 #include <net/pf/pfvar.h>
81 #include <net/pf/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
88 #include <netdb.h> /* for "struct addrinfo" */
91 #include <pcap-namedb.h>
93 #include <netproto/802_11/ieee80211.h>
94 #include <netproto/802_11/ieee80211_radiotap.h>
99 #define IPPROTO_SCTP 132
102 #ifdef HAVE_OS_PROTO_H
103 #include "os-proto.h"
106 #define JMP(c) ((c)|BPF_JMP|BPF_K)
109 static jmp_buf top_ctx;
110 static pcap_t *bpf_pcap;
113 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
114 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
116 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
121 static int pcap_fddipad;
126 bpf_error(const char *fmt, ...)
131 if (bpf_pcap != NULL)
132 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
139 static void init_linktype(pcap_t *);
141 static int alloc_reg(void);
142 static void free_reg(int);
144 static struct block *root;
147 * Value passed to gen_load_a() to indicate what the offset argument
151 OR_PACKET, /* relative to the beginning of the packet */
152 OR_LINK, /* relative to the link-layer header */
153 OR_NET, /* relative to the network-layer header */
154 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
155 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
156 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
160 * We divy out chunks of memory rather than call malloc each time so
161 * we don't have to worry about leaking memory. It's probably
162 * not a big deal if all this memory was wasted but if this ever
163 * goes into a library that would probably not be a good idea.
165 * XXX - this *is* in a library....
168 #define CHUNK0SIZE 1024
174 static struct chunk chunks[NCHUNKS];
175 static int cur_chunk;
177 static void *newchunk(u_int);
178 static void freechunks(void);
179 static inline struct block *new_block(int);
180 static inline struct slist *new_stmt(int);
181 static struct block *gen_retblk(int);
182 static inline void syntax(void);
184 static void backpatch(struct block *, struct block *);
185 static void merge(struct block *, struct block *);
186 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
187 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
188 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
189 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
190 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
191 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
193 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
194 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
195 bpf_u_int32, bpf_u_int32, int, bpf_int32);
196 static struct slist *gen_load_llrel(u_int, u_int);
197 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
198 static struct slist *gen_loadx_iphdrlen(void);
199 static struct block *gen_uncond(int);
200 static inline struct block *gen_true(void);
201 static inline struct block *gen_false(void);
202 static struct block *gen_ether_linktype(int);
203 static struct block *gen_linux_sll_linktype(int);
204 static void insert_radiotap_load_llprefixlen(struct block *);
205 static void insert_ppi_load_llprefixlen(struct block *);
206 static void insert_load_llprefixlen(struct block *);
207 static struct slist *gen_llprefixlen(void);
208 static struct block *gen_linktype(int);
209 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
210 static struct block *gen_llc_linktype(int);
211 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
213 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
215 static struct block *gen_ahostop(const u_char *, int);
216 static struct block *gen_ehostop(const u_char *, int);
217 static struct block *gen_fhostop(const u_char *, int);
218 static struct block *gen_thostop(const u_char *, int);
219 static struct block *gen_wlanhostop(const u_char *, int);
220 static struct block *gen_ipfchostop(const u_char *, int);
221 static struct block *gen_dnhostop(bpf_u_int32, int);
222 static struct block *gen_mpls_linktype(int);
223 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
225 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
228 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
230 static struct block *gen_ipfrag(void);
231 static struct block *gen_portatom(int, bpf_int32);
232 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
234 static struct block *gen_portatom6(int, bpf_int32);
235 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
237 struct block *gen_portop(int, int, int);
238 static struct block *gen_port(int, int, int);
239 struct block *gen_portrangeop(int, int, int, int);
240 static struct block *gen_portrange(int, int, int, int);
242 struct block *gen_portop6(int, int, int);
243 static struct block *gen_port6(int, int, int);
244 struct block *gen_portrangeop6(int, int, int, int);
245 static struct block *gen_portrange6(int, int, int, int);
247 static int lookup_proto(const char *, int);
248 static struct block *gen_protochain(int, int, int);
249 static struct block *gen_proto(int, int, int);
250 static struct slist *xfer_to_x(struct arth *);
251 static struct slist *xfer_to_a(struct arth *);
252 static struct block *gen_mac_multicast(int);
253 static struct block *gen_len(int, int);
255 static struct block *gen_ppi_dlt_check(void);
256 static struct block *gen_msg_abbrev(int type);
267 /* XXX Round up to nearest long. */
268 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
270 /* XXX Round up to structure boundary. */
274 cp = &chunks[cur_chunk];
275 if (n > cp->n_left) {
276 ++cp, k = ++cur_chunk;
278 bpf_error("out of memory");
279 size = CHUNK0SIZE << k;
280 cp->m = (void *)malloc(size);
282 bpf_error("out of memory");
283 memset((char *)cp->m, 0, size);
286 bpf_error("out of memory");
289 return (void *)((char *)cp->m + cp->n_left);
298 for (i = 0; i < NCHUNKS; ++i)
299 if (chunks[i].m != NULL) {
306 * A strdup whose allocations are freed after code generation is over.
310 register const char *s;
312 int n = strlen(s) + 1;
313 char *cp = newchunk(n);
319 static inline struct block *
325 p = (struct block *)newchunk(sizeof(*p));
332 static inline struct slist *
338 p = (struct slist *)newchunk(sizeof(*p));
344 static struct block *
348 struct block *b = new_block(BPF_RET|BPF_K);
357 bpf_error("syntax error in filter expression");
360 static bpf_u_int32 netmask;
365 pcap_compile(pcap_t *p, struct bpf_program *program,
366 const char *buf, int optimize, bpf_u_int32 mask)
369 const char * volatile xbuf = buf;
376 if (setjmp(top_ctx)) {
384 snaplen = pcap_snapshot(p);
386 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
387 "snaplen of 0 rejects all packets");
391 lex_init(xbuf ? xbuf : "");
399 root = gen_retblk(snaplen);
401 if (optimize && !no_optimize) {
404 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
405 bpf_error("expression rejects all packets");
407 program->bf_insns = icode_to_fcode(root, &len);
408 program->bf_len = len;
416 * entry point for using the compiler with no pcap open
417 * pass in all the stuff that is needed explicitly instead.
420 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
421 struct bpf_program *program,
422 const char *buf, int optimize, bpf_u_int32 mask)
427 p = pcap_open_dead(linktype_arg, snaplen_arg);
430 ret = pcap_compile(p, program, buf, optimize, mask);
436 * Clean up a "struct bpf_program" by freeing all the memory allocated
440 pcap_freecode(struct bpf_program *program)
443 if (program->bf_insns != NULL) {
444 free((char *)program->bf_insns);
445 program->bf_insns = NULL;
450 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
451 * which of the jt and jf fields has been resolved and which is a pointer
452 * back to another unresolved block (or nil). At least one of the fields
453 * in each block is already resolved.
456 backpatch(list, target)
457 struct block *list, *target;
474 * Merge the lists in b0 and b1, using the 'sense' field to indicate
475 * which of jt and jf is the link.
479 struct block *b0, *b1;
481 register struct block **p = &b0;
483 /* Find end of list. */
485 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
487 /* Concatenate the lists. */
496 struct block *ppi_dlt_check;
498 ppi_dlt_check = gen_ppi_dlt_check();
500 if (ppi_dlt_check != NULL)
502 gen_and(ppi_dlt_check, p);
505 backpatch(p, gen_retblk(snaplen));
506 p->sense = !p->sense;
507 backpatch(p, gen_retblk(0));
511 * Insert before the statements of the first (root) block any
512 * statements needed to load the lengths of any variable-length
513 * headers into registers.
515 * XXX - a fancier strategy would be to insert those before the
516 * statements of all blocks that use those lengths and that
517 * have no predecessors that use them, so that we only compute
518 * the lengths if we need them. There might be even better
519 * approaches than that. However, as we're currently only
520 * handling variable-length radiotap headers, and as all
521 * filtering expressions other than raw link[M:N] tests
522 * require the length of that header, doing more for that
523 * header length isn't really worth the effort.
526 insert_load_llprefixlen(root);
531 struct block *b0, *b1;
533 backpatch(b0, b1->head);
534 b0->sense = !b0->sense;
535 b1->sense = !b1->sense;
537 b1->sense = !b1->sense;
543 struct block *b0, *b1;
545 b0->sense = !b0->sense;
546 backpatch(b0, b1->head);
547 b0->sense = !b0->sense;
556 b->sense = !b->sense;
559 static struct block *
560 gen_cmp(offrel, offset, size, v)
561 enum e_offrel offrel;
565 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
568 static struct block *
569 gen_cmp_gt(offrel, offset, size, v)
570 enum e_offrel offrel;
574 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
577 static struct block *
578 gen_cmp_ge(offrel, offset, size, v)
579 enum e_offrel offrel;
583 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
586 static struct block *
587 gen_cmp_lt(offrel, offset, size, v)
588 enum e_offrel offrel;
592 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
595 static struct block *
596 gen_cmp_le(offrel, offset, size, v)
597 enum e_offrel offrel;
601 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
604 static struct block *
605 gen_mcmp(offrel, offset, size, v, mask)
606 enum e_offrel offrel;
611 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
614 static struct block *
615 gen_bcmp(offrel, offset, size, v)
616 enum e_offrel offrel;
617 register u_int offset, size;
618 register const u_char *v;
620 register struct block *b, *tmp;
624 register const u_char *p = &v[size - 4];
625 bpf_int32 w = ((bpf_int32)p[0] << 24) |
626 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
628 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
635 register const u_char *p = &v[size - 2];
636 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
638 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
645 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
654 * AND the field of size "size" at offset "offset" relative to the header
655 * specified by "offrel" with "mask", and compare it with the value "v"
656 * with the test specified by "jtype"; if "reverse" is true, the test
657 * should test the opposite of "jtype".
659 static struct block *
660 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
661 enum e_offrel offrel;
663 bpf_u_int32 offset, size, mask, jtype;
666 struct slist *s, *s2;
669 s = gen_load_a(offrel, offset, size);
671 if (mask != 0xffffffff) {
672 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
677 b = new_block(JMP(jtype));
680 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
686 * Various code constructs need to know the layout of the data link
687 * layer. These variables give the necessary offsets from the beginning
688 * of the packet data.
690 * If the link layer has variable_length headers, the offsets are offsets
691 * from the end of the link-link-layer header, and "reg_ll_size" is
692 * the register number for a register containing the length of the
693 * link-layer header. Otherwise, "reg_ll_size" is -1.
695 static int reg_ll_size;
698 * This is the offset of the beginning of the link-layer header from
699 * the beginning of the raw packet data.
701 * It's usually 0, except for 802.11 with a fixed-length radio header.
702 * (For 802.11 with a variable-length radio header, we have to generate
703 * code to compute that offset; off_ll is 0 in that case.)
708 * This is the offset of the beginning of the MAC-layer header.
709 * It's usually 0, except for ATM LANE, where it's the offset, relative
710 * to the beginning of the raw packet data, of the Ethernet header.
712 static u_int off_mac;
715 * "off_linktype" is the offset to information in the link-layer header
716 * giving the packet type. This offset is relative to the beginning
717 * of the link-layer header (i.e., it doesn't include off_ll).
719 * For Ethernet, it's the offset of the Ethernet type field.
721 * For link-layer types that always use 802.2 headers, it's the
722 * offset of the LLC header.
724 * For PPP, it's the offset of the PPP type field.
726 * For Cisco HDLC, it's the offset of the CHDLC type field.
728 * For BSD loopback, it's the offset of the AF_ value.
730 * For Linux cooked sockets, it's the offset of the type field.
732 * It's set to -1 for no encapsulation, in which case, IP is assumed.
734 static u_int off_linktype;
737 * TRUE if the link layer includes an ATM pseudo-header.
739 static int is_atm = 0;
742 * TRUE if "lane" appeared in the filter; it causes us to generate
743 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
745 static int is_lane = 0;
748 * These are offsets for the ATM pseudo-header.
750 static u_int off_vpi;
751 static u_int off_vci;
752 static u_int off_proto;
755 * These are offsets for the MTP2 fields.
760 * These are offsets for the MTP3 fields.
762 static u_int off_sio;
763 static u_int off_opc;
764 static u_int off_dpc;
765 static u_int off_sls;
768 * This is the offset of the first byte after the ATM pseudo_header,
769 * or -1 if there is no ATM pseudo-header.
771 static u_int off_payload;
774 * These are offsets to the beginning of the network-layer header.
775 * They are relative to the beginning of the link-layer header (i.e.,
776 * they don't include off_ll).
778 * If the link layer never uses 802.2 LLC:
780 * "off_nl" and "off_nl_nosnap" are the same.
782 * If the link layer always uses 802.2 LLC:
784 * "off_nl" is the offset if there's a SNAP header following
787 * "off_nl_nosnap" is the offset if there's no SNAP header.
789 * If the link layer is Ethernet:
791 * "off_nl" is the offset if the packet is an Ethernet II packet
792 * (we assume no 802.3+802.2+SNAP);
794 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
795 * with an 802.2 header following it.
798 static u_int off_nl_nosnap;
806 linktype = pcap_datalink(p);
808 pcap_fddipad = p->fddipad;
812 * Assume it's not raw ATM with a pseudo-header, for now.
823 * And assume we're not doing SS7.
832 * Also assume it's not 802.11 with a fixed-length radio header.
838 label_stack_depth = 0;
846 off_nl = 6; /* XXX in reality, variable! */
847 off_nl_nosnap = 6; /* no 802.2 LLC */
850 case DLT_ARCNET_LINUX:
852 off_nl = 8; /* XXX in reality, variable! */
853 off_nl_nosnap = 8; /* no 802.2 LLC */
858 off_nl = 14; /* Ethernet II */
859 off_nl_nosnap = 17; /* 802.3+802.2 */
864 * SLIP doesn't have a link level type. The 16 byte
865 * header is hacked into our SLIP driver.
869 off_nl_nosnap = 16; /* no 802.2 LLC */
873 /* XXX this may be the same as the DLT_PPP_BSDOS case */
877 off_nl_nosnap = 24; /* no 802.2 LLC */
884 off_nl_nosnap = 4; /* no 802.2 LLC */
890 off_nl_nosnap = 12; /* no 802.2 LLC */
895 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
896 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
899 off_nl_nosnap = 4; /* no 802.2 LLC */
904 * This does no include the Ethernet header, and
905 * only covers session state.
909 off_nl_nosnap = 8; /* no 802.2 LLC */
915 off_nl_nosnap = 24; /* no 802.2 LLC */
920 * FDDI doesn't really have a link-level type field.
921 * We set "off_linktype" to the offset of the LLC header.
923 * To check for Ethernet types, we assume that SSAP = SNAP
924 * is being used and pick out the encapsulated Ethernet type.
925 * XXX - should we generate code to check for SNAP?
929 off_linktype += pcap_fddipad;
931 off_nl = 21; /* FDDI+802.2+SNAP */
932 off_nl_nosnap = 16; /* FDDI+802.2 */
934 off_nl += pcap_fddipad;
935 off_nl_nosnap += pcap_fddipad;
941 * Token Ring doesn't really have a link-level type field.
942 * We set "off_linktype" to the offset of the LLC header.
944 * To check for Ethernet types, we assume that SSAP = SNAP
945 * is being used and pick out the encapsulated Ethernet type.
946 * XXX - should we generate code to check for SNAP?
948 * XXX - the header is actually variable-length.
949 * Some various Linux patched versions gave 38
950 * as "off_linktype" and 40 as "off_nl"; however,
951 * if a token ring packet has *no* routing
952 * information, i.e. is not source-routed, the correct
953 * values are 20 and 22, as they are in the vanilla code.
955 * A packet is source-routed iff the uppermost bit
956 * of the first byte of the source address, at an
957 * offset of 8, has the uppermost bit set. If the
958 * packet is source-routed, the total number of bytes
959 * of routing information is 2 plus bits 0x1F00 of
960 * the 16-bit value at an offset of 14 (shifted right
961 * 8 - figure out which byte that is).
964 off_nl = 22; /* Token Ring+802.2+SNAP */
965 off_nl_nosnap = 17; /* Token Ring+802.2 */
970 * 802.11 doesn't really have a link-level type field.
971 * We set "off_linktype" to the offset of the LLC header.
973 * To check for Ethernet types, we assume that SSAP = SNAP
974 * is being used and pick out the encapsulated Ethernet type.
975 * XXX - should we generate code to check for SNAP?
977 * XXX - the header is actually variable-length. We
978 * assume a 24-byte link-layer header, as appears in
979 * data frames in networks with no bridges. If the
980 * fromds and tods 802.11 header bits are both set,
981 * it's actually supposed to be 30 bytes.
984 off_nl = 32; /* 802.11+802.2+SNAP */
985 off_nl_nosnap = 27; /* 802.11+802.2 */
988 case DLT_PRISM_HEADER:
990 * Same as 802.11, but with an additional header before
991 * the 802.11 header, containing a bunch of additional
992 * information including radio-level information.
994 * The header is 144 bytes long.
996 * XXX - same variable-length header problem; at least
997 * the Prism header is fixed-length.
1001 off_nl = 32; /* Prism+802.11+802.2+SNAP */
1002 off_nl_nosnap = 27; /* Prism+802.11+802.2 */
1005 case DLT_IEEE802_11_RADIO_AVS:
1007 * Same as 802.11, but with an additional header before
1008 * the 802.11 header, containing a bunch of additional
1009 * information including radio-level information.
1011 * The header is 64 bytes long, at least in its
1012 * current incarnation.
1014 * XXX - same variable-length header problem, only
1015 * more so; this header is also variable-length,
1016 * with the length being the 32-bit big-endian
1017 * number at an offset of 4 from the beginning
1018 * of the radio header. We should handle that the
1019 * same way we handle the length at the beginning
1020 * of the radiotap header.
1022 * XXX - in Linux, do any drivers that supply an AVS
1023 * header supply a link-layer type other than
1024 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1025 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1026 * any drivers that supply an AVS header but supply
1027 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1028 * have to check the header in the generated code to
1029 * determine whether it's Prism or AVS.
1033 off_nl = 32; /* Radio+802.11+802.2+SNAP */
1034 off_nl_nosnap = 27; /* Radio+802.11+802.2 */
1039 * At the moment we treat PPI as normal Radiotap encoded
1040 * packets. The difference is in the function that generates
1041 * the code at the beginning to compute the header length.
1042 * Since this code generator of PPI supports bare 802.11
1043 * encapsulation only (i.e. the encapsulated DLT should be
1044 * DLT_IEEE802_11) we generate code to check for this too.
1047 case DLT_IEEE802_11_RADIO:
1049 * Same as 802.11, but with an additional header before
1050 * the 802.11 header, containing a bunch of additional
1051 * information including radio-level information.
1053 * The radiotap header is variable length, and we
1054 * generate code to compute its length and store it
1055 * in a register. These offsets are relative to the
1056 * beginning of the 802.11 header.
1059 off_nl = 32; /* 802.11+802.2+SNAP */
1060 off_nl_nosnap = 27; /* 802.11+802.2 */
1063 case DLT_ATM_RFC1483:
1064 case DLT_ATM_CLIP: /* Linux ATM defines this */
1066 * assume routed, non-ISO PDUs
1067 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1069 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1070 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1071 * latter would presumably be treated the way PPPoE
1072 * should be, so you can do "pppoe and udp port 2049"
1073 * or "pppoa and tcp port 80" and have it check for
1074 * PPPo{A,E} and a PPP protocol of IP and....
1077 off_nl = 8; /* 802.2+SNAP */
1078 off_nl_nosnap = 3; /* 802.2 */
1083 * Full Frontal ATM; you get AALn PDUs with an ATM
1087 off_vpi = SUNATM_VPI_POS;
1088 off_vci = SUNATM_VCI_POS;
1089 off_proto = PROTO_POS;
1090 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1091 off_payload = SUNATM_PKT_BEGIN_POS;
1092 off_linktype = off_payload;
1093 off_nl = off_payload+8; /* 802.2+SNAP */
1094 off_nl_nosnap = off_payload+3; /* 802.2 */
1100 off_nl_nosnap = 0; /* no 802.2 LLC */
1103 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1106 off_nl_nosnap = 16; /* no 802.2 LLC */
1111 * LocalTalk does have a 1-byte type field in the LLAP header,
1112 * but really it just indicates whether there is a "short" or
1113 * "long" DDP packet following.
1117 off_nl_nosnap = 0; /* no 802.2 LLC */
1120 case DLT_IP_OVER_FC:
1122 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1123 * link-level type field. We set "off_linktype" to the
1124 * offset of the LLC header.
1126 * To check for Ethernet types, we assume that SSAP = SNAP
1127 * is being used and pick out the encapsulated Ethernet type.
1128 * XXX - should we generate code to check for SNAP? RFC
1129 * 2625 says SNAP should be used.
1132 off_nl = 24; /* IPFC+802.2+SNAP */
1133 off_nl_nosnap = 19; /* IPFC+802.2 */
1138 * XXX - we should set this to handle SNAP-encapsulated
1139 * frames (NLPID of 0x80).
1143 off_nl_nosnap = 0; /* no 802.2 LLC */
1147 * the only BPF-interesting FRF.16 frames are non-control frames;
1148 * Frame Relay has a variable length link-layer
1149 * so lets start with offset 4 for now and increments later on (FIXME);
1154 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1157 case DLT_APPLE_IP_OVER_IEEE1394:
1160 off_nl_nosnap = 18; /* no 802.2 LLC */
1163 case DLT_LINUX_IRDA:
1165 * Currently, only raw "link[N:M]" filtering is supported.
1174 * Currently, only raw "link[N:M]" filtering is supported.
1181 case DLT_SYMANTEC_FIREWALL:
1183 off_nl = 44; /* Ethernet II */
1184 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1187 #ifdef HAVE_NET_PFVAR_H
1190 off_nl = PFLOG_HDRLEN;
1191 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1195 case DLT_JUNIPER_MFR:
1196 case DLT_JUNIPER_MLFR:
1197 case DLT_JUNIPER_MLPPP:
1198 case DLT_JUNIPER_PPP:
1199 case DLT_JUNIPER_CHDLC:
1200 case DLT_JUNIPER_FRELAY:
1203 off_nl_nosnap = -1; /* no 802.2 LLC */
1206 case DLT_JUNIPER_ATM1:
1207 off_linktype = 4; /* in reality variable between 4-8 */
1212 case DLT_JUNIPER_ATM2:
1213 off_linktype = 8; /* in reality variable between 8-12 */
1218 /* frames captured on a Juniper PPPoE service PIC
1219 * contain raw ethernet frames */
1220 case DLT_JUNIPER_PPPOE:
1221 case DLT_JUNIPER_ETHER:
1223 off_nl = 18; /* Ethernet II */
1224 off_nl_nosnap = 21; /* 802.3+802.2 */
1227 case DLT_JUNIPER_PPPOE_ATM:
1230 off_nl_nosnap = -1; /* no 802.2 LLC */
1233 case DLT_JUNIPER_GGSN:
1236 off_nl_nosnap = -1; /* no 802.2 LLC */
1239 case DLT_JUNIPER_ES:
1241 off_nl = -1; /* not really a network layer but raw IP adresses */
1242 off_nl_nosnap = -1; /* no 802.2 LLC */
1245 case DLT_JUNIPER_MONITOR:
1247 off_nl = 12; /* raw IP/IP6 header */
1248 off_nl_nosnap = -1; /* no 802.2 LLC */
1251 case DLT_JUNIPER_SERVICES:
1253 off_nl = -1; /* L3 proto location dep. on cookie type */
1254 off_nl_nosnap = -1; /* no 802.2 LLC */
1257 case DLT_JUNIPER_VP:
1274 case DLT_MTP2_WITH_PHDR:
1293 case DLT_LINUX_LAPD:
1295 * Currently, only raw "link[N:M]" filtering is supported.
1304 * Currently, only raw "link[N:M]" filtering is supported.
1311 case DLT_BLUETOOTH_HCI_H4:
1313 * Currently, only raw "link[N:M]" filtering is supported.
1320 bpf_error("unknown data link type %d", linktype);
1325 * Load a value relative to the beginning of the link-layer header.
1326 * The link-layer header doesn't necessarily begin at the beginning
1327 * of the packet data; there might be a variable-length prefix containing
1328 * radio information.
1330 static struct slist *
1331 gen_load_llrel(offset, size)
1334 struct slist *s, *s2;
1336 s = gen_llprefixlen();
1339 * If "s" is non-null, it has code to arrange that the X register
1340 * contains the length of the prefix preceding the link-layer
1343 * Otherwise, the length of the prefix preceding the link-layer
1344 * header is "off_ll".
1348 * There's a variable-length prefix preceding the
1349 * link-layer header. "s" points to a list of statements
1350 * that put the length of that prefix into the X register.
1351 * do an indirect load, to use the X register as an offset.
1353 s2 = new_stmt(BPF_LD|BPF_IND|size);
1358 * There is no variable-length header preceding the
1359 * link-layer header; add in off_ll, which, if there's
1360 * a fixed-length header preceding the link-layer header,
1361 * is the length of that header.
1363 s = new_stmt(BPF_LD|BPF_ABS|size);
1364 s->s.k = offset + off_ll;
1371 * Load a value relative to the beginning of the specified header.
1373 static struct slist *
1374 gen_load_a(offrel, offset, size)
1375 enum e_offrel offrel;
1378 struct slist *s, *s2;
1383 s = new_stmt(BPF_LD|BPF_ABS|size);
1388 s = gen_load_llrel(offset, size);
1392 s = gen_load_llrel(off_nl + offset, size);
1396 s = gen_load_llrel(off_nl_nosnap + offset, size);
1401 * Load the X register with the length of the IPv4 header
1402 * (plus the offset of the link-layer header, if it's
1403 * preceded by a variable-length header such as a radio
1404 * header), in bytes.
1406 s = gen_loadx_iphdrlen();
1409 * Load the item at {offset of the link-layer header} +
1410 * {offset, relative to the start of the link-layer
1411 * header, of the IPv4 header} + {length of the IPv4 header} +
1412 * {specified offset}.
1414 * (If the link-layer is variable-length, it's included
1415 * in the value in the X register, and off_ll is 0.)
1417 s2 = new_stmt(BPF_LD|BPF_IND|size);
1418 s2->s.k = off_ll + off_nl + offset;
1423 s = gen_load_llrel(off_nl + 40 + offset, size);
1434 * Generate code to load into the X register the sum of the length of
1435 * the IPv4 header and any variable-length header preceding the link-layer
1438 static struct slist *
1439 gen_loadx_iphdrlen()
1441 struct slist *s, *s2;
1443 s = gen_llprefixlen();
1446 * There's a variable-length prefix preceding the
1447 * link-layer header. "s" points to a list of statements
1448 * that put the length of that prefix into the X register.
1449 * The 4*([k]&0xf) addressing mode can't be used, as we
1450 * don't have a constant offset, so we have to load the
1451 * value in question into the A register and add to it
1452 * the value from the X register.
1454 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1457 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1460 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1465 * The A register now contains the length of the
1466 * IP header. We need to add to it the length
1467 * of the prefix preceding the link-layer
1468 * header, which is still in the X register, and
1469 * move the result into the X register.
1471 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1472 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1475 * There is no variable-length header preceding the
1476 * link-layer header; add in off_ll, which, if there's
1477 * a fixed-length header preceding the link-layer header,
1478 * is the length of that header.
1480 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1481 s->s.k = off_ll + off_nl;
1486 static struct block *
1493 s = new_stmt(BPF_LD|BPF_IMM);
1495 b = new_block(JMP(BPF_JEQ));
1501 static inline struct block *
1504 return gen_uncond(1);
1507 static inline struct block *
1510 return gen_uncond(0);
1514 * Byte-swap a 32-bit number.
1515 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1516 * big-endian platforms.)
1518 #define SWAPLONG(y) \
1519 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1522 * Generate code to match a particular packet type.
1524 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1525 * value, if <= ETHERMTU. We use that to determine whether to
1526 * match the type/length field or to check the type/length field for
1527 * a value <= ETHERMTU to see whether it's a type field and then do
1528 * the appropriate test.
1530 static struct block *
1531 gen_ether_linktype(proto)
1534 struct block *b0, *b1;
1540 case LLCSAP_NETBEUI:
1542 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1543 * so we check the DSAP and SSAP.
1545 * LLCSAP_IP checks for IP-over-802.2, rather
1546 * than IP-over-Ethernet or IP-over-SNAP.
1548 * XXX - should we check both the DSAP and the
1549 * SSAP, like this, or should we check just the
1550 * DSAP, as we do for other types <= ETHERMTU
1551 * (i.e., other SAP values)?
1553 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1555 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1556 ((proto << 8) | proto));
1564 * Ethernet_II frames, which are Ethernet
1565 * frames with a frame type of ETHERTYPE_IPX;
1567 * Ethernet_802.3 frames, which are 802.3
1568 * frames (i.e., the type/length field is
1569 * a length field, <= ETHERMTU, rather than
1570 * a type field) with the first two bytes
1571 * after the Ethernet/802.3 header being
1574 * Ethernet_802.2 frames, which are 802.3
1575 * frames with an 802.2 LLC header and
1576 * with the IPX LSAP as the DSAP in the LLC
1579 * Ethernet_SNAP frames, which are 802.3
1580 * frames with an LLC header and a SNAP
1581 * header and with an OUI of 0x000000
1582 * (encapsulated Ethernet) and a protocol
1583 * ID of ETHERTYPE_IPX in the SNAP header.
1585 * XXX - should we generate the same code both
1586 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1590 * This generates code to check both for the
1591 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1593 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1594 (bpf_int32)LLCSAP_IPX);
1595 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1600 * Now we add code to check for SNAP frames with
1601 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1603 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1607 * Now we generate code to check for 802.3
1608 * frames in general.
1610 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1614 * Now add the check for 802.3 frames before the
1615 * check for Ethernet_802.2 and Ethernet_802.3,
1616 * as those checks should only be done on 802.3
1617 * frames, not on Ethernet frames.
1622 * Now add the check for Ethernet_II frames, and
1623 * do that before checking for the other frame
1626 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1627 (bpf_int32)ETHERTYPE_IPX);
1631 case ETHERTYPE_ATALK:
1632 case ETHERTYPE_AARP:
1634 * EtherTalk (AppleTalk protocols on Ethernet link
1635 * layer) may use 802.2 encapsulation.
1639 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1640 * we check for an Ethernet type field less than
1641 * 1500, which means it's an 802.3 length field.
1643 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1647 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1648 * SNAP packets with an organization code of
1649 * 0x080007 (Apple, for Appletalk) and a protocol
1650 * type of ETHERTYPE_ATALK (Appletalk).
1652 * 802.2-encapsulated ETHERTYPE_AARP packets are
1653 * SNAP packets with an organization code of
1654 * 0x000000 (encapsulated Ethernet) and a protocol
1655 * type of ETHERTYPE_AARP (Appletalk ARP).
1657 if (proto == ETHERTYPE_ATALK)
1658 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1659 else /* proto == ETHERTYPE_AARP */
1660 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1664 * Check for Ethernet encapsulation (Ethertalk
1665 * phase 1?); we just check for the Ethernet
1668 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1674 if (proto <= ETHERMTU) {
1676 * This is an LLC SAP value, so the frames
1677 * that match would be 802.2 frames.
1678 * Check that the frame is an 802.2 frame
1679 * (i.e., that the length/type field is
1680 * a length field, <= ETHERMTU) and
1681 * then check the DSAP.
1683 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1685 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1691 * This is an Ethernet type, so compare
1692 * the length/type field with it (if
1693 * the frame is an 802.2 frame, the length
1694 * field will be <= ETHERMTU, and, as
1695 * "proto" is > ETHERMTU, this test
1696 * will fail and the frame won't match,
1697 * which is what we want).
1699 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1706 * Generate code to match a particular packet type.
1708 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1709 * value, if <= ETHERMTU. We use that to determine whether to
1710 * match the type field or to check the type field for the special
1711 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1713 static struct block *
1714 gen_linux_sll_linktype(proto)
1717 struct block *b0, *b1;
1723 case LLCSAP_NETBEUI:
1725 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1726 * so we check the DSAP and SSAP.
1728 * LLCSAP_IP checks for IP-over-802.2, rather
1729 * than IP-over-Ethernet or IP-over-SNAP.
1731 * XXX - should we check both the DSAP and the
1732 * SSAP, like this, or should we check just the
1733 * DSAP, as we do for other types <= ETHERMTU
1734 * (i.e., other SAP values)?
1736 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1737 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1738 ((proto << 8) | proto));
1744 * Ethernet_II frames, which are Ethernet
1745 * frames with a frame type of ETHERTYPE_IPX;
1747 * Ethernet_802.3 frames, which have a frame
1748 * type of LINUX_SLL_P_802_3;
1750 * Ethernet_802.2 frames, which are 802.3
1751 * frames with an 802.2 LLC header (i.e, have
1752 * a frame type of LINUX_SLL_P_802_2) and
1753 * with the IPX LSAP as the DSAP in the LLC
1756 * Ethernet_SNAP frames, which are 802.3
1757 * frames with an LLC header and a SNAP
1758 * header and with an OUI of 0x000000
1759 * (encapsulated Ethernet) and a protocol
1760 * ID of ETHERTYPE_IPX in the SNAP header.
1762 * First, do the checks on LINUX_SLL_P_802_2
1763 * frames; generate the check for either
1764 * Ethernet_802.2 or Ethernet_SNAP frames, and
1765 * then put a check for LINUX_SLL_P_802_2 frames
1768 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1769 (bpf_int32)LLCSAP_IPX);
1770 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1773 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1777 * Now check for 802.3 frames and OR that with
1778 * the previous test.
1780 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1784 * Now add the check for Ethernet_II frames, and
1785 * do that before checking for the other frame
1788 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1789 (bpf_int32)ETHERTYPE_IPX);
1793 case ETHERTYPE_ATALK:
1794 case ETHERTYPE_AARP:
1796 * EtherTalk (AppleTalk protocols on Ethernet link
1797 * layer) may use 802.2 encapsulation.
1801 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1802 * we check for the 802.2 protocol type in the
1803 * "Ethernet type" field.
1805 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1808 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1809 * SNAP packets with an organization code of
1810 * 0x080007 (Apple, for Appletalk) and a protocol
1811 * type of ETHERTYPE_ATALK (Appletalk).
1813 * 802.2-encapsulated ETHERTYPE_AARP packets are
1814 * SNAP packets with an organization code of
1815 * 0x000000 (encapsulated Ethernet) and a protocol
1816 * type of ETHERTYPE_AARP (Appletalk ARP).
1818 if (proto == ETHERTYPE_ATALK)
1819 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1821 else /* proto == ETHERTYPE_AARP */
1822 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1827 * Check for Ethernet encapsulation (Ethertalk
1828 * phase 1?); we just check for the Ethernet
1831 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1837 if (proto <= ETHERMTU) {
1839 * This is an LLC SAP value, so the frames
1840 * that match would be 802.2 frames.
1841 * Check for the 802.2 protocol type
1842 * in the "Ethernet type" field, and
1843 * then check the DSAP.
1845 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1847 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1853 * This is an Ethernet type, so compare
1854 * the length/type field with it (if
1855 * the frame is an 802.2 frame, the length
1856 * field will be <= ETHERMTU, and, as
1857 * "proto" is > ETHERMTU, this test
1858 * will fail and the frame won't match,
1859 * which is what we want).
1861 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1868 insert_radiotap_load_llprefixlen(b)
1871 struct slist *s1, *s2;
1874 * Prepend to the statements in this block code to load the
1875 * length of the radiotap header into the register assigned
1876 * to hold that length, if one has been assigned.
1878 if (reg_ll_size != -1) {
1880 * The 2 bytes at offsets of 2 and 3 from the beginning
1881 * of the radiotap header are the length of the radiotap
1882 * header; unfortunately, it's little-endian, so we have
1883 * to load it a byte at a time and construct the value.
1887 * Load the high-order byte, at an offset of 3, shift it
1888 * left a byte, and put the result in the X register.
1890 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1892 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1895 s2 = new_stmt(BPF_MISC|BPF_TAX);
1899 * Load the next byte, at an offset of 2, and OR the
1900 * value from the X register into it.
1902 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1905 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1909 * Now allocate a register to hold that value and store
1912 s2 = new_stmt(BPF_ST);
1913 s2->s.k = reg_ll_size;
1917 * Now move it into the X register.
1919 s2 = new_stmt(BPF_MISC|BPF_TAX);
1923 * Now append all the existing statements in this
1924 * block to these statements.
1926 sappend(s1, b->stmts);
1932 * At the moment we treat PPI as normal Radiotap encoded
1933 * packets. The difference is in the function that generates
1934 * the code at the beginning to compute the header length.
1935 * Since this code generator of PPI supports bare 802.11
1936 * encapsulation only (i.e. the encapsulated DLT should be
1937 * DLT_IEEE802_11) we generate code to check for this too.
1940 insert_ppi_load_llprefixlen(b)
1943 struct slist *s1, *s2;
1946 * Prepend to the statements in this block code to load the
1947 * length of the radiotap header into the register assigned
1948 * to hold that length, if one has been assigned.
1950 if (reg_ll_size != -1) {
1952 * The 2 bytes at offsets of 2 and 3 from the beginning
1953 * of the radiotap header are the length of the radiotap
1954 * header; unfortunately, it's little-endian, so we have
1955 * to load it a byte at a time and construct the value.
1959 * Load the high-order byte, at an offset of 3, shift it
1960 * left a byte, and put the result in the X register.
1962 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1964 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1967 s2 = new_stmt(BPF_MISC|BPF_TAX);
1971 * Load the next byte, at an offset of 2, and OR the
1972 * value from the X register into it.
1974 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1977 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1981 * Now allocate a register to hold that value and store
1984 s2 = new_stmt(BPF_ST);
1985 s2->s.k = reg_ll_size;
1989 * Now move it into the X register.
1991 s2 = new_stmt(BPF_MISC|BPF_TAX);
1995 * Now append all the existing statements in this
1996 * block to these statements.
1998 sappend(s1, b->stmts);
2004 static struct block *
2005 gen_ppi_dlt_check(void)
2007 struct slist *s_load_dlt;
2010 if (linktype == DLT_PPI)
2012 /* Create the statements that check for the DLT
2014 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2015 s_load_dlt->s.k = 4;
2017 b = new_block(JMP(BPF_JEQ));
2019 b->stmts = s_load_dlt;
2020 b->s.k = SWAPLONG(DLT_IEEE802_11);
2031 insert_load_llprefixlen(b)
2037 * At the moment we treat PPI as normal Radiotap encoded
2038 * packets. The difference is in the function that generates
2039 * the code at the beginning to compute the header length.
2040 * Since this code generator of PPI supports bare 802.11
2041 * encapsulation only (i.e. the encapsulated DLT should be
2042 * DLT_IEEE802_11) we generate code to check for this too.
2045 insert_ppi_load_llprefixlen(b);
2048 case DLT_IEEE802_11_RADIO:
2049 insert_radiotap_load_llprefixlen(b);
2055 static struct slist *
2056 gen_radiotap_llprefixlen(void)
2060 if (reg_ll_size == -1) {
2062 * We haven't yet assigned a register for the length
2063 * of the radiotap header; allocate one.
2065 reg_ll_size = alloc_reg();
2069 * Load the register containing the radiotap length
2070 * into the X register.
2072 s = new_stmt(BPF_LDX|BPF_MEM);
2073 s->s.k = reg_ll_size;
2078 * At the moment we treat PPI as normal Radiotap encoded
2079 * packets. The difference is in the function that generates
2080 * the code at the beginning to compute the header length.
2081 * Since this code generator of PPI supports bare 802.11
2082 * encapsulation only (i.e. the encapsulated DLT should be
2083 * DLT_IEEE802_11) we generate code to check for this too.
2085 static struct slist *
2086 gen_ppi_llprefixlen(void)
2090 if (reg_ll_size == -1) {
2092 * We haven't yet assigned a register for the length
2093 * of the radiotap header; allocate one.
2095 reg_ll_size = alloc_reg();
2099 * Load the register containing the radiotap length
2100 * into the X register.
2102 s = new_stmt(BPF_LDX|BPF_MEM);
2103 s->s.k = reg_ll_size;
2110 * Generate code to compute the link-layer header length, if necessary,
2111 * putting it into the X register, and to return either a pointer to a
2112 * "struct slist" for the list of statements in that code, or NULL if
2113 * no code is necessary.
2115 static struct slist *
2116 gen_llprefixlen(void)
2121 return gen_ppi_llprefixlen();
2124 case DLT_IEEE802_11_RADIO:
2125 return gen_radiotap_llprefixlen();
2133 * Generate code to match a particular packet type by matching the
2134 * link-layer type field or fields in the 802.2 LLC header.
2136 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2137 * value, if <= ETHERMTU.
2139 static struct block *
2143 struct block *b0, *b1, *b2;
2145 /* are we checking MPLS-encapsulated packets? */
2146 if (label_stack_depth > 0) {
2150 /* FIXME add other L3 proto IDs */
2151 return gen_mpls_linktype(Q_IP);
2153 case ETHERTYPE_IPV6:
2155 /* FIXME add other L3 proto IDs */
2156 return gen_mpls_linktype(Q_IPV6);
2159 bpf_error("unsupported protocol over mpls");
2167 return gen_ether_linktype(proto);
2175 proto = (proto << 8 | LLCSAP_ISONS);
2179 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2189 case DLT_IEEE802_11:
2190 case DLT_IEEE802_11_RADIO_AVS:
2191 case DLT_IEEE802_11_RADIO:
2192 case DLT_PRISM_HEADER:
2193 case DLT_ATM_RFC1483:
2195 case DLT_IP_OVER_FC:
2196 return gen_llc_linktype(proto);
2202 * If "is_lane" is set, check for a LANE-encapsulated
2203 * version of this protocol, otherwise check for an
2204 * LLC-encapsulated version of this protocol.
2206 * We assume LANE means Ethernet, not Token Ring.
2210 * Check that the packet doesn't begin with an
2211 * LE Control marker. (We've already generated
2214 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
2219 * Now generate an Ethernet test.
2221 b1 = gen_ether_linktype(proto);
2226 * Check for LLC encapsulation and then check the
2229 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
2230 b1 = gen_llc_linktype(proto);
2238 return gen_linux_sll_linktype(proto);
2243 case DLT_SLIP_BSDOS:
2246 * These types don't provide any type field; packets
2247 * are always IPv4 or IPv6.
2249 * XXX - for IPv4, check for a version number of 4, and,
2250 * for IPv6, check for a version number of 6?
2255 /* Check for a version number of 4. */
2256 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
2258 case ETHERTYPE_IPV6:
2259 /* Check for a version number of 6. */
2260 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
2264 return gen_false(); /* always false */
2271 case DLT_PPP_SERIAL:
2274 * We use Ethernet protocol types inside libpcap;
2275 * map them to the corresponding PPP protocol types.
2284 case ETHERTYPE_IPV6:
2293 case ETHERTYPE_ATALK:
2307 * I'm assuming the "Bridging PDU"s that go
2308 * over PPP are Spanning Tree Protocol
2322 * We use Ethernet protocol types inside libpcap;
2323 * map them to the corresponding PPP protocol types.
2328 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2329 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2331 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2336 case ETHERTYPE_IPV6:
2346 case ETHERTYPE_ATALK:
2360 * I'm assuming the "Bridging PDU"s that go
2361 * over PPP are Spanning Tree Protocol
2377 * For DLT_NULL, the link-layer header is a 32-bit
2378 * word containing an AF_ value in *host* byte order,
2379 * and for DLT_ENC, the link-layer header begins
2380 * with a 32-bit work containing an AF_ value in
2383 * In addition, if we're reading a saved capture file,
2384 * the host byte order in the capture may not be the
2385 * same as the host byte order on this machine.
2387 * For DLT_LOOP, the link-layer header is a 32-bit
2388 * word containing an AF_ value in *network* byte order.
2390 * XXX - AF_ values may, unfortunately, be platform-
2391 * dependent; for example, FreeBSD's AF_INET6 is 24
2392 * whilst NetBSD's and OpenBSD's is 26.
2394 * This means that, when reading a capture file, just
2395 * checking for our AF_INET6 value won't work if the
2396 * capture file came from another OS.
2405 case ETHERTYPE_IPV6:
2412 * Not a type on which we support filtering.
2413 * XXX - support those that have AF_ values
2414 * #defined on this platform, at least?
2419 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2421 * The AF_ value is in host byte order, but
2422 * the BPF interpreter will convert it to
2423 * network byte order.
2425 * If this is a save file, and it's from a
2426 * machine with the opposite byte order to
2427 * ours, we byte-swap the AF_ value.
2429 * Then we run it through "htonl()", and
2430 * generate code to compare against the result.
2432 if (bpf_pcap->sf.rfile != NULL &&
2433 bpf_pcap->sf.swapped)
2434 proto = SWAPLONG(proto);
2435 proto = htonl(proto);
2437 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2439 #ifdef HAVE_NET_PFVAR_H
2442 * af field is host byte order in contrast to the rest of
2445 if (proto == ETHERTYPE_IP)
2446 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2447 BPF_B, (bpf_int32)AF_INET));
2449 else if (proto == ETHERTYPE_IPV6)
2450 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2451 BPF_B, (bpf_int32)AF_INET6));
2457 #endif /* HAVE_NET_PFVAR_H */
2460 case DLT_ARCNET_LINUX:
2462 * XXX should we check for first fragment if the protocol
2471 case ETHERTYPE_IPV6:
2472 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2473 (bpf_int32)ARCTYPE_INET6));
2477 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2478 (bpf_int32)ARCTYPE_IP);
2479 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2480 (bpf_int32)ARCTYPE_IP_OLD);
2485 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2486 (bpf_int32)ARCTYPE_ARP);
2487 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2488 (bpf_int32)ARCTYPE_ARP_OLD);
2492 case ETHERTYPE_REVARP:
2493 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2494 (bpf_int32)ARCTYPE_REVARP));
2496 case ETHERTYPE_ATALK:
2497 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2498 (bpf_int32)ARCTYPE_ATALK));
2505 case ETHERTYPE_ATALK:
2515 * XXX - assumes a 2-byte Frame Relay header with
2516 * DLCI and flags. What if the address is longer?
2522 * Check for the special NLPID for IP.
2524 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2527 case ETHERTYPE_IPV6:
2529 * Check for the special NLPID for IPv6.
2531 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2536 * Check for several OSI protocols.
2538 * Frame Relay packets typically have an OSI
2539 * NLPID at the beginning; we check for each
2542 * What we check for is the NLPID and a frame
2543 * control field of UI, i.e. 0x03 followed
2546 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2547 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2548 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2559 case DLT_JUNIPER_MFR:
2560 case DLT_JUNIPER_MLFR:
2561 case DLT_JUNIPER_MLPPP:
2562 case DLT_JUNIPER_ATM1:
2563 case DLT_JUNIPER_ATM2:
2564 case DLT_JUNIPER_PPPOE:
2565 case DLT_JUNIPER_PPPOE_ATM:
2566 case DLT_JUNIPER_GGSN:
2567 case DLT_JUNIPER_ES:
2568 case DLT_JUNIPER_MONITOR:
2569 case DLT_JUNIPER_SERVICES:
2570 case DLT_JUNIPER_ETHER:
2571 case DLT_JUNIPER_PPP:
2572 case DLT_JUNIPER_FRELAY:
2573 case DLT_JUNIPER_CHDLC:
2574 case DLT_JUNIPER_VP:
2575 /* just lets verify the magic number for now -
2576 * on ATM we may have up to 6 different encapsulations on the wire
2577 * and need a lot of heuristics to figure out that the payload
2580 * FIXME encapsulation specific BPF_ filters
2582 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2584 case DLT_LINUX_IRDA:
2585 bpf_error("IrDA link-layer type filtering not implemented");
2588 bpf_error("DOCSIS link-layer type filtering not implemented");
2590 case DLT_LINUX_LAPD:
2591 bpf_error("LAPD link-layer type filtering not implemented");
2595 * All the types that have no encapsulation should either be
2596 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2597 * all packets are IP packets, or should be handled in some
2598 * special case, if none of them are (if some are and some
2599 * aren't, the lack of encapsulation is a problem, as we'd
2600 * have to find some other way of determining the packet type).
2602 * Therefore, if "off_linktype" is -1, there's an error.
2604 if (off_linktype == (u_int)-1)
2608 * Any type not handled above should always have an Ethernet
2609 * type at an offset of "off_linktype". (PPP is partially
2610 * handled above - the protocol type is mapped from the
2611 * Ethernet and LLC types we use internally to the corresponding
2612 * PPP type - but the PPP type is always specified by a value
2613 * at "off_linktype", so we don't have to do the code generation
2616 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2620 * Check for an LLC SNAP packet with a given organization code and
2621 * protocol type; we check the entire contents of the 802.2 LLC and
2622 * snap headers, checking for DSAP and SSAP of SNAP and a control
2623 * field of 0x03 in the LLC header, and for the specified organization
2624 * code and protocol type in the SNAP header.
2626 static struct block *
2627 gen_snap(orgcode, ptype, offset)
2628 bpf_u_int32 orgcode;
2632 u_char snapblock[8];
2634 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2635 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2636 snapblock[2] = 0x03; /* control = UI */
2637 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2638 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2639 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2640 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2641 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2642 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2646 * Generate code to match a particular packet type, for link-layer types
2647 * using 802.2 LLC headers.
2649 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2650 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2652 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2653 * value, if <= ETHERMTU. We use that to determine whether to
2654 * match the DSAP or both DSAP and LSAP or to check the OUI and
2655 * protocol ID in a SNAP header.
2657 static struct block *
2658 gen_llc_linktype(proto)
2662 * XXX - handle token-ring variable-length header.
2668 case LLCSAP_NETBEUI:
2670 * XXX - should we check both the DSAP and the
2671 * SSAP, like this, or should we check just the
2672 * DSAP, as we do for other types <= ETHERMTU
2673 * (i.e., other SAP values)?
2675 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2676 ((proto << 8) | proto));
2680 * XXX - are there ever SNAP frames for IPX on
2681 * non-Ethernet 802.x networks?
2683 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2684 (bpf_int32)LLCSAP_IPX);
2686 case ETHERTYPE_ATALK:
2688 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2689 * SNAP packets with an organization code of
2690 * 0x080007 (Apple, for Appletalk) and a protocol
2691 * type of ETHERTYPE_ATALK (Appletalk).
2693 * XXX - check for an organization code of
2694 * encapsulated Ethernet as well?
2696 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2700 * XXX - we don't have to check for IPX 802.3
2701 * here, but should we check for the IPX Ethertype?
2703 if (proto <= ETHERMTU) {
2705 * This is an LLC SAP value, so check
2708 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2712 * This is an Ethernet type; we assume that it's
2713 * unlikely that it'll appear in the right place
2714 * at random, and therefore check only the
2715 * location that would hold the Ethernet type
2716 * in a SNAP frame with an organization code of
2717 * 0x000000 (encapsulated Ethernet).
2719 * XXX - if we were to check for the SNAP DSAP and
2720 * LSAP, as per XXX, and were also to check for an
2721 * organization code of 0x000000 (encapsulated
2722 * Ethernet), we'd do
2724 * return gen_snap(0x000000, proto,
2727 * here; for now, we don't, as per the above.
2728 * I don't know whether it's worth the extra CPU
2729 * time to do the right check or not.
2731 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2737 static struct block *
2738 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2742 u_int src_off, dst_off;
2744 struct block *b0, *b1;
2758 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2759 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2765 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2766 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2773 b0 = gen_linktype(proto);
2774 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2780 static struct block *
2781 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2782 struct in6_addr *addr;
2783 struct in6_addr *mask;
2785 u_int src_off, dst_off;
2787 struct block *b0, *b1;
2802 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2803 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2809 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2810 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2817 /* this order is important */
2818 a = (u_int32_t *)addr;
2819 m = (u_int32_t *)mask;
2820 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2821 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2823 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2825 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2827 b0 = gen_linktype(proto);
2833 static struct block *
2834 gen_ehostop(eaddr, dir)
2835 register const u_char *eaddr;
2838 register struct block *b0, *b1;
2842 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2845 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2848 b0 = gen_ehostop(eaddr, Q_SRC);
2849 b1 = gen_ehostop(eaddr, Q_DST);
2855 b0 = gen_ehostop(eaddr, Q_SRC);
2856 b1 = gen_ehostop(eaddr, Q_DST);
2865 * Like gen_ehostop, but for DLT_FDDI
2867 static struct block *
2868 gen_fhostop(eaddr, dir)
2869 register const u_char *eaddr;
2872 struct block *b0, *b1;
2877 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2879 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2884 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2886 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2890 b0 = gen_fhostop(eaddr, Q_SRC);
2891 b1 = gen_fhostop(eaddr, Q_DST);
2897 b0 = gen_fhostop(eaddr, Q_SRC);
2898 b1 = gen_fhostop(eaddr, Q_DST);
2907 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2909 static struct block *
2910 gen_thostop(eaddr, dir)
2911 register const u_char *eaddr;
2914 register struct block *b0, *b1;
2918 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2921 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2924 b0 = gen_thostop(eaddr, Q_SRC);
2925 b1 = gen_thostop(eaddr, Q_DST);
2931 b0 = gen_thostop(eaddr, Q_SRC);
2932 b1 = gen_thostop(eaddr, Q_DST);
2941 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2943 static struct block *
2944 gen_wlanhostop(eaddr, dir)
2945 register const u_char *eaddr;
2948 register struct block *b0, *b1, *b2;
2949 register struct slist *s;
2956 * For control frames, there is no SA.
2958 * For management frames, SA is at an
2959 * offset of 10 from the beginning of
2962 * For data frames, SA is at an offset
2963 * of 10 from the beginning of the packet
2964 * if From DS is clear, at an offset of
2965 * 16 from the beginning of the packet
2966 * if From DS is set and To DS is clear,
2967 * and an offset of 24 from the beginning
2968 * of the packet if From DS is set and To DS
2973 * Generate the tests to be done for data frames
2976 * First, check for To DS set, i.e. check "link[1] & 0x01".
2978 s = gen_load_a(OR_LINK, 1, BPF_B);
2979 b1 = new_block(JMP(BPF_JSET));
2980 b1->s.k = 0x01; /* To DS */
2984 * If To DS is set, the SA is at 24.
2986 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2990 * Now, check for To DS not set, i.e. check
2991 * "!(link[1] & 0x01)".
2993 s = gen_load_a(OR_LINK, 1, BPF_B);
2994 b2 = new_block(JMP(BPF_JSET));
2995 b2->s.k = 0x01; /* To DS */
3000 * If To DS is not set, the SA is at 16.
3002 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3006 * Now OR together the last two checks. That gives
3007 * the complete set of checks for data frames with
3013 * Now check for From DS being set, and AND that with
3014 * the ORed-together checks.
3016 s = gen_load_a(OR_LINK, 1, BPF_B);
3017 b1 = new_block(JMP(BPF_JSET));
3018 b1->s.k = 0x02; /* From DS */
3023 * Now check for data frames with From DS not set.
3025 s = gen_load_a(OR_LINK, 1, BPF_B);
3026 b2 = new_block(JMP(BPF_JSET));
3027 b2->s.k = 0x02; /* From DS */
3032 * If From DS isn't set, the SA is at 10.
3034 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3038 * Now OR together the checks for data frames with
3039 * From DS not set and for data frames with From DS
3040 * set; that gives the checks done for data frames.
3045 * Now check for a data frame.
3046 * I.e, check "link[0] & 0x08".
3048 gen_load_a(OR_LINK, 0, BPF_B);
3049 b1 = new_block(JMP(BPF_JSET));
3054 * AND that with the checks done for data frames.
3059 * If the high-order bit of the type value is 0, this
3060 * is a management frame.
3061 * I.e, check "!(link[0] & 0x08)".
3063 s = gen_load_a(OR_LINK, 0, BPF_B);
3064 b2 = new_block(JMP(BPF_JSET));
3070 * For management frames, the SA is at 10.
3072 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3076 * OR that with the checks done for data frames.
3077 * That gives the checks done for management and
3083 * If the low-order bit of the type value is 1,
3084 * this is either a control frame or a frame
3085 * with a reserved type, and thus not a
3088 * I.e., check "!(link[0] & 0x04)".
3090 s = gen_load_a(OR_LINK, 0, BPF_B);
3091 b1 = new_block(JMP(BPF_JSET));
3097 * AND that with the checks for data and management
3107 * For control frames, there is no DA.
3109 * For management frames, DA is at an
3110 * offset of 4 from the beginning of
3113 * For data frames, DA is at an offset
3114 * of 4 from the beginning of the packet
3115 * if To DS is clear and at an offset of
3116 * 16 from the beginning of the packet
3121 * Generate the tests to be done for data frames.
3123 * First, check for To DS set, i.e. "link[1] & 0x01".
3125 s = gen_load_a(OR_LINK, 1, BPF_B);
3126 b1 = new_block(JMP(BPF_JSET));
3127 b1->s.k = 0x01; /* To DS */
3131 * If To DS is set, the DA is at 16.
3133 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3137 * Now, check for To DS not set, i.e. check
3138 * "!(link[1] & 0x01)".
3140 s = gen_load_a(OR_LINK, 1, BPF_B);
3141 b2 = new_block(JMP(BPF_JSET));
3142 b2->s.k = 0x01; /* To DS */
3147 * If To DS is not set, the DA is at 4.
3149 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3153 * Now OR together the last two checks. That gives
3154 * the complete set of checks for data frames.
3159 * Now check for a data frame.
3160 * I.e, check "link[0] & 0x08".
3162 s = gen_load_a(OR_LINK, 0, BPF_B);
3163 b1 = new_block(JMP(BPF_JSET));
3168 * AND that with the checks done for data frames.
3173 * If the high-order bit of the type value is 0, this
3174 * is a management frame.
3175 * I.e, check "!(link[0] & 0x08)".
3177 s = gen_load_a(OR_LINK, 0, BPF_B);
3178 b2 = new_block(JMP(BPF_JSET));
3184 * For management frames, the DA is at 4.
3186 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3190 * OR that with the checks done for data frames.
3191 * That gives the checks done for management and
3197 * If the low-order bit of the type value is 1,
3198 * this is either a control frame or a frame
3199 * with a reserved type, and thus not a
3202 * I.e., check "!(link[0] & 0x04)".
3204 s = gen_load_a(OR_LINK, 0, BPF_B);
3205 b1 = new_block(JMP(BPF_JSET));
3211 * AND that with the checks for data and management
3218 b0 = gen_wlanhostop(eaddr, Q_SRC);
3219 b1 = gen_wlanhostop(eaddr, Q_DST);
3225 b0 = gen_wlanhostop(eaddr, Q_SRC);
3226 b1 = gen_wlanhostop(eaddr, Q_DST);
3235 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3236 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3237 * as the RFC states.)
3239 static struct block *
3240 gen_ipfchostop(eaddr, dir)
3241 register const u_char *eaddr;
3244 register struct block *b0, *b1;
3248 return gen_bcmp(OR_LINK, 10, 6, eaddr);
3251 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3254 b0 = gen_ipfchostop(eaddr, Q_SRC);
3255 b1 = gen_ipfchostop(eaddr, Q_DST);
3261 b0 = gen_ipfchostop(eaddr, Q_SRC);
3262 b1 = gen_ipfchostop(eaddr, Q_DST);
3271 * This is quite tricky because there may be pad bytes in front of the
3272 * DECNET header, and then there are two possible data packet formats that
3273 * carry both src and dst addresses, plus 5 packet types in a format that
3274 * carries only the src node, plus 2 types that use a different format and
3275 * also carry just the src node.
3279 * Instead of doing those all right, we just look for data packets with
3280 * 0 or 1 bytes of padding. If you want to look at other packets, that
3281 * will require a lot more hacking.
3283 * To add support for filtering on DECNET "areas" (network numbers)
3284 * one would want to add a "mask" argument to this routine. That would
3285 * make the filter even more inefficient, although one could be clever
3286 * and not generate masking instructions if the mask is 0xFFFF.
3288 static struct block *
3289 gen_dnhostop(addr, dir)
3293 struct block *b0, *b1, *b2, *tmp;
3294 u_int offset_lh; /* offset if long header is received */
3295 u_int offset_sh; /* offset if short header is received */
3300 offset_sh = 1; /* follows flags */
3301 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3305 offset_sh = 3; /* follows flags, dstnode */
3306 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3310 /* Inefficient because we do our Calvinball dance twice */
3311 b0 = gen_dnhostop(addr, Q_SRC);
3312 b1 = gen_dnhostop(addr, Q_DST);
3318 /* Inefficient because we do our Calvinball dance twice */
3319 b0 = gen_dnhostop(addr, Q_SRC);
3320 b1 = gen_dnhostop(addr, Q_DST);
3325 bpf_error("ISO host filtering not implemented");
3330 b0 = gen_linktype(ETHERTYPE_DN);
3331 /* Check for pad = 1, long header case */
3332 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3333 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3334 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3335 BPF_H, (bpf_int32)ntohs((u_short)addr));
3337 /* Check for pad = 0, long header case */
3338 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3339 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3342 /* Check for pad = 1, short header case */
3343 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3344 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3345 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3348 /* Check for pad = 0, short header case */
3349 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3350 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3354 /* Combine with test for linktype */
3360 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3361 * test the bottom-of-stack bit, and then check the version number
3362 * field in the IP header.
3364 static struct block *
3365 gen_mpls_linktype(proto)
3368 struct block *b0, *b1;
3373 /* match the bottom-of-stack bit */
3374 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3375 /* match the IPv4 version number */
3376 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3381 /* match the bottom-of-stack bit */
3382 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3383 /* match the IPv4 version number */
3384 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3393 static struct block *
3394 gen_host(addr, mask, proto, dir, type)
3401 struct block *b0, *b1;
3402 const char *typestr;
3412 b0 = gen_host(addr, mask, Q_IP, dir, type);
3414 * Only check for non-IPv4 addresses if we're not
3415 * checking MPLS-encapsulated packets.
3417 if (label_stack_depth == 0) {
3418 b1 = gen_host(addr, mask, Q_ARP, dir, type);
3420 b0 = gen_host(addr, mask, Q_RARP, dir, type);
3426 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3429 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3432 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3435 bpf_error("'tcp' modifier applied to %s", typestr);
3438 bpf_error("'sctp' modifier applied to %s", typestr);
3441 bpf_error("'udp' modifier applied to %s", typestr);
3444 bpf_error("'icmp' modifier applied to %s", typestr);
3447 bpf_error("'igmp' modifier applied to %s", typestr);
3450 bpf_error("'igrp' modifier applied to %s", typestr);
3453 bpf_error("'pim' modifier applied to %s", typestr);
3456 bpf_error("'vrrp' modifier applied to %s", typestr);
3459 bpf_error("ATALK host filtering not implemented");
3462 bpf_error("AARP host filtering not implemented");
3465 return gen_dnhostop(addr, dir);
3468 bpf_error("SCA host filtering not implemented");
3471 bpf_error("LAT host filtering not implemented");
3474 bpf_error("MOPDL host filtering not implemented");
3477 bpf_error("MOPRC host filtering not implemented");
3481 bpf_error("'ip6' modifier applied to ip host");
3484 bpf_error("'icmp6' modifier applied to %s", typestr);
3488 bpf_error("'ah' modifier applied to %s", typestr);
3491 bpf_error("'esp' modifier applied to %s", typestr);
3494 bpf_error("ISO host filtering not implemented");
3497 bpf_error("'esis' modifier applied to %s", typestr);
3500 bpf_error("'isis' modifier applied to %s", typestr);
3503 bpf_error("'clnp' modifier applied to %s", typestr);
3506 bpf_error("'stp' modifier applied to %s", typestr);
3509 bpf_error("IPX host filtering not implemented");
3512 bpf_error("'netbeui' modifier applied to %s", typestr);
3515 bpf_error("'radio' modifier applied to %s", typestr);
3524 static struct block *
3525 gen_host6(addr, mask, proto, dir, type)
3526 struct in6_addr *addr;
3527 struct in6_addr *mask;
3532 const char *typestr;
3542 return gen_host6(addr, mask, Q_IPV6, dir, type);
3545 bpf_error("'ip' modifier applied to ip6 %s", typestr);
3548 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
3551 bpf_error("'arp' modifier applied to ip6 %s", typestr);
3554 bpf_error("'sctp' modifier applied to %s", typestr);
3557 bpf_error("'tcp' modifier applied to %s", typestr);
3560 bpf_error("'udp' modifier applied to %s", typestr);
3563 bpf_error("'icmp' modifier applied to %s", typestr);
3566 bpf_error("'igmp' modifier applied to %s", typestr);
3569 bpf_error("'igrp' modifier applied to %s", typestr);
3572 bpf_error("'pim' modifier applied to %s", typestr);
3575 bpf_error("'vrrp' modifier applied to %s", typestr);
3578 bpf_error("ATALK host filtering not implemented");
3581 bpf_error("AARP host filtering not implemented");
3584 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
3587 bpf_error("SCA host filtering not implemented");
3590 bpf_error("LAT host filtering not implemented");
3593 bpf_error("MOPDL host filtering not implemented");
3596 bpf_error("MOPRC host filtering not implemented");
3599 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3602 bpf_error("'icmp6' modifier applied to %s", typestr);
3605 bpf_error("'ah' modifier applied to %s", typestr);
3608 bpf_error("'esp' modifier applied to %s", typestr);
3611 bpf_error("ISO host filtering not implemented");
3614 bpf_error("'esis' modifier applied to %s", typestr);
3617 bpf_error("'isis' modifier applied to %s", typestr);
3620 bpf_error("'clnp' modifier applied to %s", typestr);
3623 bpf_error("'stp' modifier applied to %s", typestr);
3626 bpf_error("IPX host filtering not implemented");
3629 bpf_error("'netbeui' modifier applied to %s", typestr);
3632 bpf_error("'radio' modifier applied to %s", typestr);
3642 static struct block *
3643 gen_gateway(eaddr, alist, proto, dir)
3644 const u_char *eaddr;
3645 bpf_u_int32 **alist;
3649 struct block *b0, *b1, *tmp;
3652 bpf_error("direction applied to 'gateway'");
3661 b0 = gen_ehostop(eaddr, Q_OR);
3664 b0 = gen_fhostop(eaddr, Q_OR);
3667 b0 = gen_thostop(eaddr, Q_OR);
3669 case DLT_IEEE802_11:
3670 case DLT_IEEE802_11_RADIO_AVS:
3672 case DLT_IEEE802_11_RADIO:
3673 case DLT_PRISM_HEADER:
3674 b0 = gen_wlanhostop(eaddr, Q_OR);
3679 * Check that the packet doesn't begin with an
3680 * LE Control marker. (We've already generated
3683 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3688 * Now check the MAC address.
3690 b0 = gen_ehostop(eaddr, Q_OR);
3694 case DLT_IP_OVER_FC:
3695 b0 = gen_ipfchostop(eaddr, Q_OR);
3699 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3701 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
3703 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
3712 bpf_error("illegal modifier of 'gateway'");
3718 gen_proto_abbrev(proto)
3727 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3729 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3735 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3737 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3743 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3745 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3751 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3754 #ifndef IPPROTO_IGMP
3755 #define IPPROTO_IGMP 2
3759 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3762 #ifndef IPPROTO_IGRP
3763 #define IPPROTO_IGRP 9
3766 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3770 #define IPPROTO_PIM 103
3774 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3776 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3781 #ifndef IPPROTO_VRRP
3782 #define IPPROTO_VRRP 112
3786 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3790 b1 = gen_linktype(ETHERTYPE_IP);
3794 b1 = gen_linktype(ETHERTYPE_ARP);
3798 b1 = gen_linktype(ETHERTYPE_REVARP);
3802 bpf_error("link layer applied in wrong context");
3805 b1 = gen_linktype(ETHERTYPE_ATALK);
3809 b1 = gen_linktype(ETHERTYPE_AARP);
3813 b1 = gen_linktype(ETHERTYPE_DN);
3817 b1 = gen_linktype(ETHERTYPE_SCA);
3821 b1 = gen_linktype(ETHERTYPE_LAT);
3825 b1 = gen_linktype(ETHERTYPE_MOPDL);
3829 b1 = gen_linktype(ETHERTYPE_MOPRC);
3834 b1 = gen_linktype(ETHERTYPE_IPV6);
3837 #ifndef IPPROTO_ICMPV6
3838 #define IPPROTO_ICMPV6 58
3841 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3846 #define IPPROTO_AH 51
3849 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3851 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3857 #define IPPROTO_ESP 50
3860 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3862 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3868 b1 = gen_linktype(LLCSAP_ISONS);
3872 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3876 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3879 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3880 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3881 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3883 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3885 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3887 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3891 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3892 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3893 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3895 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3897 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3899 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3903 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3904 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3905 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3907 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3912 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3913 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3918 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3919 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3921 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3923 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3928 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3929 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3934 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3935 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3940 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3944 b1 = gen_linktype(LLCSAP_8021D);
3948 b1 = gen_linktype(LLCSAP_IPX);
3952 b1 = gen_linktype(LLCSAP_NETBEUI);
3956 bpf_error("'radio' is not a valid protocol type");
3964 static struct block *
3971 s = gen_load_a(OR_NET, 6, BPF_H);
3972 b = new_block(JMP(BPF_JSET));
3981 * Generate a comparison to a port value in the transport-layer header
3982 * at the specified offset from the beginning of that header.
3984 * XXX - this handles a variable-length prefix preceding the link-layer
3985 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3986 * variable-length link-layer headers (such as Token Ring or 802.11
3989 static struct block *
3990 gen_portatom(off, v)
3994 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3998 static struct block *
3999 gen_portatom6(off, v)
4003 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
4008 gen_portop(port, proto, dir)
4009 int port, proto, dir;
4011 struct block *b0, *b1, *tmp;
4013 /* ip proto 'proto' */
4014 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4020 b1 = gen_portatom(0, (bpf_int32)port);
4024 b1 = gen_portatom(2, (bpf_int32)port);
4029 tmp = gen_portatom(0, (bpf_int32)port);
4030 b1 = gen_portatom(2, (bpf_int32)port);
4035 tmp = gen_portatom(0, (bpf_int32)port);
4036 b1 = gen_portatom(2, (bpf_int32)port);
4048 static struct block *
4049 gen_port(port, ip_proto, dir)
4054 struct block *b0, *b1, *tmp;
4059 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4060 * not LLC encapsulation with LLCSAP_IP.
4062 * For IEEE 802 networks - which includes 802.5 token ring
4063 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4064 * says that SNAP encapsulation is used, not LLC encapsulation
4067 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4068 * RFC 2225 say that SNAP encapsulation is used, not LLC
4069 * encapsulation with LLCSAP_IP.
4071 * So we always check for ETHERTYPE_IP.
4073 b0 = gen_linktype(ETHERTYPE_IP);
4079 b1 = gen_portop(port, ip_proto, dir);
4083 tmp = gen_portop(port, IPPROTO_TCP, dir);
4084 b1 = gen_portop(port, IPPROTO_UDP, dir);
4086 tmp = gen_portop(port, IPPROTO_SCTP, dir);
4099 gen_portop6(port, proto, dir)
4100 int port, proto, dir;
4102 struct block *b0, *b1, *tmp;
4104 /* ip6 proto 'proto' */
4105 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4109 b1 = gen_portatom6(0, (bpf_int32)port);
4113 b1 = gen_portatom6(2, (bpf_int32)port);
4118 tmp = gen_portatom6(0, (bpf_int32)port);
4119 b1 = gen_portatom6(2, (bpf_int32)port);
4124 tmp = gen_portatom6(0, (bpf_int32)port);
4125 b1 = gen_portatom6(2, (bpf_int32)port);
4137 static struct block *
4138 gen_port6(port, ip_proto, dir)
4143 struct block *b0, *b1, *tmp;
4145 /* link proto ip6 */
4146 b0 = gen_linktype(ETHERTYPE_IPV6);
4152 b1 = gen_portop6(port, ip_proto, dir);
4156 tmp = gen_portop6(port, IPPROTO_TCP, dir);
4157 b1 = gen_portop6(port, IPPROTO_UDP, dir);
4159 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
4171 /* gen_portrange code */
4172 static struct block *
4173 gen_portrangeatom(off, v1, v2)
4177 struct block *b1, *b2;
4181 * Reverse the order of the ports, so v1 is the lower one.
4190 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
4191 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
4199 gen_portrangeop(port1, port2, proto, dir)
4204 struct block *b0, *b1, *tmp;
4206 /* ip proto 'proto' */
4207 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4213 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4217 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4222 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4223 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4228 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4229 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4241 static struct block *
4242 gen_portrange(port1, port2, ip_proto, dir)
4247 struct block *b0, *b1, *tmp;
4250 b0 = gen_linktype(ETHERTYPE_IP);
4256 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
4260 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
4261 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
4263 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
4275 static struct block *
4276 gen_portrangeatom6(off, v1, v2)
4280 struct block *b1, *b2;
4284 * Reverse the order of the ports, so v1 is the lower one.
4293 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
4294 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
4302 gen_portrangeop6(port1, port2, proto, dir)
4307 struct block *b0, *b1, *tmp;
4309 /* ip6 proto 'proto' */
4310 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4314 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4318 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4323 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4324 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4329 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4330 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4342 static struct block *
4343 gen_portrange6(port1, port2, ip_proto, dir)
4348 struct block *b0, *b1, *tmp;
4350 /* link proto ip6 */
4351 b0 = gen_linktype(ETHERTYPE_IPV6);
4357 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4361 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4362 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4364 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4377 lookup_proto(name, proto)
4378 register const char *name;
4388 v = pcap_nametoproto(name);
4389 if (v == PROTO_UNDEF)
4390 bpf_error("unknown ip proto '%s'", name);
4394 /* XXX should look up h/w protocol type based on linktype */
4395 v = pcap_nametoeproto(name);
4396 if (v == PROTO_UNDEF) {
4397 v = pcap_nametollc(name);
4398 if (v == PROTO_UNDEF)
4399 bpf_error("unknown ether proto '%s'", name);
4404 if (strcmp(name, "esis") == 0)
4406 else if (strcmp(name, "isis") == 0)
4408 else if (strcmp(name, "clnp") == 0)
4411 bpf_error("unknown osi proto '%s'", name);
4431 static struct block *
4432 gen_protochain(v, proto, dir)
4437 #ifdef NO_PROTOCHAIN
4438 return gen_proto(v, proto, dir);
4440 struct block *b0, *b;
4441 struct slist *s[100];
4442 int fix2, fix3, fix4, fix5;
4443 int ahcheck, again, end;
4445 int reg2 = alloc_reg();
4447 memset(s, 0, sizeof(s));
4448 fix2 = fix3 = fix4 = fix5 = 0;
4455 b0 = gen_protochain(v, Q_IP, dir);
4456 b = gen_protochain(v, Q_IPV6, dir);
4460 bpf_error("bad protocol applied for 'protochain'");
4465 * We don't handle variable-length radiotap here headers yet.
4466 * We might want to add BPF instructions to do the protochain
4467 * work, to simplify that and, on platforms that have a BPF
4468 * interpreter with the new instructions, let the filtering
4469 * be done in the kernel. (We already require a modified BPF
4470 * engine to do the protochain stuff, to support backward
4471 * branches, and backward branch support is unlikely to appear
4472 * in kernel BPF engines.)
4474 if (linktype == DLT_IEEE802_11_RADIO)
4475 bpf_error("'protochain' not supported with radiotap headers");
4477 if (linktype == DLT_PPI)
4478 bpf_error("'protochain' not supported with PPI headers");
4480 no_optimize = 1; /*this code is not compatible with optimzer yet */
4483 * s[0] is a dummy entry to protect other BPF insn from damage
4484 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4485 * hard to find interdependency made by jump table fixup.
4488 s[i] = new_stmt(0); /*dummy*/
4493 b0 = gen_linktype(ETHERTYPE_IP);
4496 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4497 s[i]->s.k = off_ll + off_nl + 9;
4499 /* X = ip->ip_hl << 2 */
4500 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4501 s[i]->s.k = off_ll + off_nl;
4506 b0 = gen_linktype(ETHERTYPE_IPV6);
4508 /* A = ip6->ip_nxt */
4509 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4510 s[i]->s.k = off_ll + off_nl + 6;
4512 /* X = sizeof(struct ip6_hdr) */
4513 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4519 bpf_error("unsupported proto to gen_protochain");
4523 /* again: if (A == v) goto end; else fall through; */
4525 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4527 s[i]->s.jt = NULL; /*later*/
4528 s[i]->s.jf = NULL; /*update in next stmt*/
4532 #ifndef IPPROTO_NONE
4533 #define IPPROTO_NONE 59
4535 /* if (A == IPPROTO_NONE) goto end */
4536 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4537 s[i]->s.jt = NULL; /*later*/
4538 s[i]->s.jf = NULL; /*update in next stmt*/
4539 s[i]->s.k = IPPROTO_NONE;
4540 s[fix5]->s.jf = s[i];
4545 if (proto == Q_IPV6) {
4546 int v6start, v6end, v6advance, j;
4549 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4550 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4551 s[i]->s.jt = NULL; /*later*/
4552 s[i]->s.jf = NULL; /*update in next stmt*/
4553 s[i]->s.k = IPPROTO_HOPOPTS;
4554 s[fix2]->s.jf = s[i];
4556 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4557 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4558 s[i]->s.jt = NULL; /*later*/
4559 s[i]->s.jf = NULL; /*update in next stmt*/
4560 s[i]->s.k = IPPROTO_DSTOPTS;
4562 /* if (A == IPPROTO_ROUTING) goto v6advance */
4563 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4564 s[i]->s.jt = NULL; /*later*/
4565 s[i]->s.jf = NULL; /*update in next stmt*/
4566 s[i]->s.k = IPPROTO_ROUTING;
4568 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4569 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4570 s[i]->s.jt = NULL; /*later*/
4571 s[i]->s.jf = NULL; /*later*/
4572 s[i]->s.k = IPPROTO_FRAGMENT;
4583 * X = X + (P[X + 1] + 1) * 8;
4586 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4588 /* A = P[X + packet head] */
4589 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4590 s[i]->s.k = off_ll + off_nl;
4593 s[i] = new_stmt(BPF_ST);
4597 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4600 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4604 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4606 /* A = P[X + packet head]; */
4607 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4608 s[i]->s.k = off_ll + off_nl;
4611 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4615 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4619 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4622 s[i] = new_stmt(BPF_LD|BPF_MEM);
4626 /* goto again; (must use BPF_JA for backward jump) */
4627 s[i] = new_stmt(BPF_JMP|BPF_JA);
4628 s[i]->s.k = again - i - 1;
4629 s[i - 1]->s.jf = s[i];
4633 for (j = v6start; j <= v6end; j++)
4634 s[j]->s.jt = s[v6advance];
4639 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4641 s[fix2]->s.jf = s[i];
4647 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4648 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4649 s[i]->s.jt = NULL; /*later*/
4650 s[i]->s.jf = NULL; /*later*/
4651 s[i]->s.k = IPPROTO_AH;
4653 s[fix3]->s.jf = s[ahcheck];
4660 * X = X + (P[X + 1] + 2) * 4;
4663 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4665 /* A = P[X + packet head]; */
4666 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4667 s[i]->s.k = off_ll + off_nl;
4670 s[i] = new_stmt(BPF_ST);
4674 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4677 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4681 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4683 /* A = P[X + packet head] */
4684 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4685 s[i]->s.k = off_ll + off_nl;
4688 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4692 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4696 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4699 s[i] = new_stmt(BPF_LD|BPF_MEM);
4703 /* goto again; (must use BPF_JA for backward jump) */
4704 s[i] = new_stmt(BPF_JMP|BPF_JA);
4705 s[i]->s.k = again - i - 1;
4710 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4712 s[fix2]->s.jt = s[end];
4713 s[fix4]->s.jf = s[end];
4714 s[fix5]->s.jt = s[end];
4721 for (i = 0; i < max - 1; i++)
4722 s[i]->next = s[i + 1];
4723 s[max - 1]->next = NULL;
4728 b = new_block(JMP(BPF_JEQ));
4729 b->stmts = s[1]; /*remember, s[0] is dummy*/
4741 * Generate code that checks whether the packet is a packet for protocol
4742 * <proto> and whether the type field in that protocol's header has
4743 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4744 * IP packet and checks the protocol number in the IP header against <v>.
4746 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4747 * against Q_IP and Q_IPV6.
4749 static struct block *
4750 gen_proto(v, proto, dir)
4755 struct block *b0, *b1;
4757 if (dir != Q_DEFAULT)
4758 bpf_error("direction applied to 'proto'");
4763 b0 = gen_proto(v, Q_IP, dir);
4764 b1 = gen_proto(v, Q_IPV6, dir);
4772 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4773 * not LLC encapsulation with LLCSAP_IP.
4775 * For IEEE 802 networks - which includes 802.5 token ring
4776 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4777 * says that SNAP encapsulation is used, not LLC encapsulation
4780 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4781 * RFC 2225 say that SNAP encapsulation is used, not LLC
4782 * encapsulation with LLCSAP_IP.
4784 * So we always check for ETHERTYPE_IP.
4786 b0 = gen_linktype(ETHERTYPE_IP);
4788 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4790 b1 = gen_protochain(v, Q_IP);
4800 * Frame Relay packets typically have an OSI
4801 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4802 * generates code to check for all the OSI
4803 * NLPIDs, so calling it and then adding a check
4804 * for the particular NLPID for which we're
4805 * looking is bogus, as we can just check for
4808 * What we check for is the NLPID and a frame
4809 * control field value of UI, i.e. 0x03 followed
4812 * XXX - assumes a 2-byte Frame Relay header with
4813 * DLCI and flags. What if the address is longer?
4815 * XXX - what about SNAP-encapsulated frames?
4817 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4823 * Cisco uses an Ethertype lookalike - for OSI,
4826 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4827 /* OSI in C-HDLC is stuffed with a fudge byte */
4828 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4833 b0 = gen_linktype(LLCSAP_ISONS);
4834 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4840 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4842 * 4 is the offset of the PDU type relative to the IS-IS
4845 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4850 bpf_error("arp does not encapsulate another protocol");
4854 bpf_error("rarp does not encapsulate another protocol");
4858 bpf_error("atalk encapsulation is not specifiable");
4862 bpf_error("decnet encapsulation is not specifiable");
4866 bpf_error("sca does not encapsulate another protocol");
4870 bpf_error("lat does not encapsulate another protocol");
4874 bpf_error("moprc does not encapsulate another protocol");
4878 bpf_error("mopdl does not encapsulate another protocol");
4882 return gen_linktype(v);
4885 bpf_error("'udp proto' is bogus");
4889 bpf_error("'tcp proto' is bogus");
4893 bpf_error("'sctp proto' is bogus");
4897 bpf_error("'icmp proto' is bogus");
4901 bpf_error("'igmp proto' is bogus");
4905 bpf_error("'igrp proto' is bogus");
4909 bpf_error("'pim proto' is bogus");
4913 bpf_error("'vrrp proto' is bogus");
4918 b0 = gen_linktype(ETHERTYPE_IPV6);
4920 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4922 b1 = gen_protochain(v, Q_IPV6);
4928 bpf_error("'icmp6 proto' is bogus");
4932 bpf_error("'ah proto' is bogus");
4935 bpf_error("'ah proto' is bogus");
4938 bpf_error("'stp proto' is bogus");
4941 bpf_error("'ipx proto' is bogus");
4944 bpf_error("'netbeui proto' is bogus");
4947 bpf_error("'radio proto' is bogus");
4958 register const char *name;
4961 int proto = q.proto;
4965 bpf_u_int32 mask, addr;
4967 bpf_u_int32 **alist;
4970 struct sockaddr_in *sin4;
4971 struct sockaddr_in6 *sin6;
4972 struct addrinfo *res, *res0;
4973 struct in6_addr mask128;
4975 struct block *b, *tmp;
4976 int port, real_proto;
4982 addr = pcap_nametonetaddr(name);
4984 bpf_error("unknown network '%s'", name);
4985 /* Left justify network addr and calculate its network mask */
4987 while (addr && (addr & 0xff000000) == 0) {
4991 return gen_host(addr, mask, proto, dir, q.addr);
4995 if (proto == Q_LINK) {
4999 eaddr = pcap_ether_hostton(name);
5002 "unknown ether host '%s'", name);
5003 b = gen_ehostop(eaddr, dir);
5008 eaddr = pcap_ether_hostton(name);
5011 "unknown FDDI host '%s'", name);
5012 b = gen_fhostop(eaddr, dir);
5017 eaddr = pcap_ether_hostton(name);
5020 "unknown token ring host '%s'", name);
5021 b = gen_thostop(eaddr, dir);
5025 case DLT_IEEE802_11:
5026 case DLT_IEEE802_11_RADIO_AVS:
5027 case DLT_IEEE802_11_RADIO:
5028 case DLT_PRISM_HEADER:
5030 eaddr = pcap_ether_hostton(name);
5033 "unknown 802.11 host '%s'", name);
5034 b = gen_wlanhostop(eaddr, dir);
5038 case DLT_IP_OVER_FC:
5039 eaddr = pcap_ether_hostton(name);
5042 "unknown Fibre Channel host '%s'", name);
5043 b = gen_ipfchostop(eaddr, dir);
5052 * Check that the packet doesn't begin
5053 * with an LE Control marker. (We've
5054 * already generated a test for LANE.)
5056 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
5060 eaddr = pcap_ether_hostton(name);
5063 "unknown ether host '%s'", name);
5064 b = gen_ehostop(eaddr, dir);
5070 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5071 } else if (proto == Q_DECNET) {
5072 unsigned short dn_addr = __pcap_nametodnaddr(name);
5074 * I don't think DECNET hosts can be multihomed, so
5075 * there is no need to build up a list of addresses
5077 return (gen_host(dn_addr, 0, proto, dir, q.addr));
5080 alist = pcap_nametoaddr(name);
5081 if (alist == NULL || *alist == NULL)
5082 bpf_error("unknown host '%s'", name);
5084 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
5086 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
5088 tmp = gen_host(**alist++, 0xffffffff,
5089 tproto, dir, q.addr);
5095 memset(&mask128, 0xff, sizeof(mask128));
5096 res0 = res = pcap_nametoaddrinfo(name);
5098 bpf_error("unknown host '%s'", name);
5100 tproto = tproto6 = proto;
5101 if (off_linktype == -1 && tproto == Q_DEFAULT) {
5105 for (res = res0; res; res = res->ai_next) {
5106 switch (res->ai_family) {
5108 if (tproto == Q_IPV6)
5111 sin4 = (struct sockaddr_in *)
5113 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
5114 0xffffffff, tproto, dir, q.addr);
5117 if (tproto6 == Q_IP)
5120 sin6 = (struct sockaddr_in6 *)
5122 tmp = gen_host6(&sin6->sin6_addr,
5123 &mask128, tproto6, dir, q.addr);
5134 bpf_error("unknown host '%s'%s", name,
5135 (proto == Q_DEFAULT)
5137 : " for specified address family");
5144 if (proto != Q_DEFAULT &&
5145 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5146 bpf_error("illegal qualifier of 'port'");
5147 if (pcap_nametoport(name, &port, &real_proto) == 0)
5148 bpf_error("unknown port '%s'", name);
5149 if (proto == Q_UDP) {
5150 if (real_proto == IPPROTO_TCP)
5151 bpf_error("port '%s' is tcp", name);
5152 else if (real_proto == IPPROTO_SCTP)
5153 bpf_error("port '%s' is sctp", name);
5155 /* override PROTO_UNDEF */
5156 real_proto = IPPROTO_UDP;
5158 if (proto == Q_TCP) {
5159 if (real_proto == IPPROTO_UDP)
5160 bpf_error("port '%s' is udp", name);
5162 else if (real_proto == IPPROTO_SCTP)
5163 bpf_error("port '%s' is sctp", name);
5165 /* override PROTO_UNDEF */
5166 real_proto = IPPROTO_TCP;
5168 if (proto == Q_SCTP) {
5169 if (real_proto == IPPROTO_UDP)
5170 bpf_error("port '%s' is udp", name);
5172 else if (real_proto == IPPROTO_TCP)
5173 bpf_error("port '%s' is tcp", name);
5175 /* override PROTO_UNDEF */
5176 real_proto = IPPROTO_SCTP;
5179 return gen_port(port, real_proto, dir);
5181 b = gen_port(port, real_proto, dir);
5182 gen_or(gen_port6(port, real_proto, dir), b);
5187 if (proto != Q_DEFAULT &&
5188 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5189 bpf_error("illegal qualifier of 'portrange'");
5190 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
5191 bpf_error("unknown port in range '%s'", name);
5192 if (proto == Q_UDP) {
5193 if (real_proto == IPPROTO_TCP)
5194 bpf_error("port in range '%s' is tcp", name);
5195 else if (real_proto == IPPROTO_SCTP)
5196 bpf_error("port in range '%s' is sctp", name);
5198 /* override PROTO_UNDEF */
5199 real_proto = IPPROTO_UDP;
5201 if (proto == Q_TCP) {
5202 if (real_proto == IPPROTO_UDP)
5203 bpf_error("port in range '%s' is udp", name);
5204 else if (real_proto == IPPROTO_SCTP)
5205 bpf_error("port in range '%s' is sctp", name);
5207 /* override PROTO_UNDEF */
5208 real_proto = IPPROTO_TCP;
5210 if (proto == Q_SCTP) {
5211 if (real_proto == IPPROTO_UDP)
5212 bpf_error("port in range '%s' is udp", name);
5213 else if (real_proto == IPPROTO_TCP)
5214 bpf_error("port in range '%s' is tcp", name);
5216 /* override PROTO_UNDEF */
5217 real_proto = IPPROTO_SCTP;
5220 return gen_portrange(port1, port2, real_proto, dir);
5222 b = gen_portrange(port1, port2, real_proto, dir);
5223 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
5229 eaddr = pcap_ether_hostton(name);
5231 bpf_error("unknown ether host: %s", name);
5233 alist = pcap_nametoaddr(name);
5234 if (alist == NULL || *alist == NULL)
5235 bpf_error("unknown host '%s'", name);
5236 b = gen_gateway(eaddr, alist, proto, dir);
5240 bpf_error("'gateway' not supported in this configuration");
5244 real_proto = lookup_proto(name, proto);
5245 if (real_proto >= 0)
5246 return gen_proto(real_proto, proto, dir);
5248 bpf_error("unknown protocol: %s", name);
5251 real_proto = lookup_proto(name, proto);
5252 if (real_proto >= 0)
5253 return gen_protochain(real_proto, proto, dir);
5255 bpf_error("unknown protocol: %s", name);
5267 gen_mcode(s1, s2, masklen, q)
5268 register const char *s1, *s2;
5269 register int masklen;
5272 register int nlen, mlen;
5275 nlen = __pcap_atoin(s1, &n);
5276 /* Promote short ipaddr */
5280 mlen = __pcap_atoin(s2, &m);
5281 /* Promote short ipaddr */
5284 bpf_error("non-network bits set in \"%s mask %s\"",
5287 /* Convert mask len to mask */
5289 bpf_error("mask length must be <= 32");
5292 * X << 32 is not guaranteed by C to be 0; it's
5297 m = 0xffffffff << (32 - masklen);
5299 bpf_error("non-network bits set in \"%s/%d\"",
5306 return gen_host(n, m, q.proto, q.dir, q.addr);
5309 bpf_error("Mask syntax for networks only");
5318 register const char *s;
5323 int proto = q.proto;
5329 else if (q.proto == Q_DECNET)
5330 vlen = __pcap_atodn(s, &v);
5332 vlen = __pcap_atoin(s, &v);
5339 if (proto == Q_DECNET)
5340 return gen_host(v, 0, proto, dir, q.addr);
5341 else if (proto == Q_LINK) {
5342 bpf_error("illegal link layer address");
5345 if (s == NULL && q.addr == Q_NET) {
5346 /* Promote short net number */
5347 while (v && (v & 0xff000000) == 0) {
5352 /* Promote short ipaddr */
5356 return gen_host(v, mask, proto, dir, q.addr);
5361 proto = IPPROTO_UDP;
5362 else if (proto == Q_TCP)
5363 proto = IPPROTO_TCP;
5364 else if (proto == Q_SCTP)
5365 proto = IPPROTO_SCTP;
5366 else if (proto == Q_DEFAULT)
5367 proto = PROTO_UNDEF;
5369 bpf_error("illegal qualifier of 'port'");
5372 return gen_port((int)v, proto, dir);
5376 b = gen_port((int)v, proto, dir);
5377 gen_or(gen_port6((int)v, proto, dir), b);
5384 proto = IPPROTO_UDP;
5385 else if (proto == Q_TCP)
5386 proto = IPPROTO_TCP;
5387 else if (proto == Q_SCTP)
5388 proto = IPPROTO_SCTP;
5389 else if (proto == Q_DEFAULT)
5390 proto = PROTO_UNDEF;
5392 bpf_error("illegal qualifier of 'portrange'");
5395 return gen_portrange((int)v, (int)v, proto, dir);
5399 b = gen_portrange((int)v, (int)v, proto, dir);
5400 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5406 bpf_error("'gateway' requires a name");
5410 return gen_proto((int)v, proto, dir);
5413 return gen_protochain((int)v, proto, dir);
5428 gen_mcode6(s1, s2, masklen, q)
5429 register const char *s1, *s2;
5430 register int masklen;
5433 struct addrinfo *res;
5434 struct in6_addr *addr;
5435 struct in6_addr mask;
5440 bpf_error("no mask %s supported", s2);
5442 res = pcap_nametoaddrinfo(s1);
5444 bpf_error("invalid ip6 address %s", s1);
5446 bpf_error("%s resolved to multiple address", s1);
5447 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5449 if (sizeof(mask) * 8 < masklen)
5450 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5451 memset(&mask, 0, sizeof(mask));
5452 memset(&mask, 0xff, masklen / 8);
5454 mask.s6_addr[masklen / 8] =
5455 (0xff << (8 - masklen % 8)) & 0xff;
5458 a = (u_int32_t *)addr;
5459 m = (u_int32_t *)&mask;
5460 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5461 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5462 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5470 bpf_error("Mask syntax for networks only");
5474 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
5479 bpf_error("invalid qualifier against IPv6 address");
5488 register const u_char *eaddr;
5491 struct block *b, *tmp;
5493 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5496 return gen_ehostop(eaddr, (int)q.dir);
5498 return gen_fhostop(eaddr, (int)q.dir);
5500 return gen_thostop(eaddr, (int)q.dir);
5501 case DLT_IEEE802_11:
5502 case DLT_IEEE802_11_RADIO_AVS:
5503 case DLT_IEEE802_11_RADIO:
5504 case DLT_PRISM_HEADER:
5506 return gen_wlanhostop(eaddr, (int)q.dir);
5510 * Check that the packet doesn't begin with an
5511 * LE Control marker. (We've already generated
5514 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5519 * Now check the MAC address.
5521 b = gen_ehostop(eaddr, (int)q.dir);
5526 case DLT_IP_OVER_FC:
5527 return gen_ipfchostop(eaddr, (int)q.dir);
5529 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5533 bpf_error("ethernet address used in non-ether expression");
5540 struct slist *s0, *s1;
5543 * This is definitely not the best way to do this, but the
5544 * lists will rarely get long.
5551 static struct slist *
5557 s = new_stmt(BPF_LDX|BPF_MEM);
5562 static struct slist *
5568 s = new_stmt(BPF_LD|BPF_MEM);
5574 * Modify "index" to use the value stored into its register as an
5575 * offset relative to the beginning of the header for the protocol
5576 * "proto", and allocate a register and put an item "size" bytes long
5577 * (1, 2, or 4) at that offset into that register, making it the register
5581 gen_load(proto, inst, size)
5586 struct slist *s, *tmp;
5588 int regno = alloc_reg();
5590 free_reg(inst->regno);
5594 bpf_error("data size must be 1, 2, or 4");
5610 bpf_error("unsupported index operation");
5614 * The offset is relative to the beginning of the packet
5615 * data, if we have a radio header. (If we don't, this
5618 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5619 linktype != DLT_IEEE802_11_RADIO &&
5620 linktype != DLT_PRISM_HEADER)
5621 bpf_error("radio information not present in capture");
5624 * Load into the X register the offset computed into the
5625 * register specifed by "index".
5627 s = xfer_to_x(inst);
5630 * Load the item at that offset.
5632 tmp = new_stmt(BPF_LD|BPF_IND|size);
5634 sappend(inst->s, s);
5639 * The offset is relative to the beginning of
5640 * the link-layer header.
5642 * XXX - what about ATM LANE? Should the index be
5643 * relative to the beginning of the AAL5 frame, so
5644 * that 0 refers to the beginning of the LE Control
5645 * field, or relative to the beginning of the LAN
5646 * frame, so that 0 refers, for Ethernet LANE, to
5647 * the beginning of the destination address?
5649 s = gen_llprefixlen();
5652 * If "s" is non-null, it has code to arrange that the
5653 * X register contains the length of the prefix preceding
5654 * the link-layer header. Add to it the offset computed
5655 * into the register specified by "index", and move that
5656 * into the X register. Otherwise, just load into the X
5657 * register the offset computed into the register specifed
5661 sappend(s, xfer_to_a(inst));
5662 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5663 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5665 s = xfer_to_x(inst);
5668 * Load the item at the sum of the offset we've put in the
5669 * X register and the offset of the start of the link
5670 * layer header (which is 0 if the radio header is
5671 * variable-length; that header length is what we put
5672 * into the X register and then added to the index).
5674 tmp = new_stmt(BPF_LD|BPF_IND|size);
5677 sappend(inst->s, s);
5693 * The offset is relative to the beginning of
5694 * the network-layer header.
5695 * XXX - are there any cases where we want
5698 s = gen_llprefixlen();
5701 * If "s" is non-null, it has code to arrange that the
5702 * X register contains the length of the prefix preceding
5703 * the link-layer header. Add to it the offset computed
5704 * into the register specified by "index", and move that
5705 * into the X register. Otherwise, just load into the X
5706 * register the offset computed into the register specifed
5710 sappend(s, xfer_to_a(inst));
5711 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5712 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5714 s = xfer_to_x(inst);
5717 * Load the item at the sum of the offset we've put in the
5718 * X register, the offset of the start of the network
5719 * layer header, and the offset of the start of the link
5720 * layer header (which is 0 if the radio header is
5721 * variable-length; that header length is what we put
5722 * into the X register and then added to the index).
5724 tmp = new_stmt(BPF_LD|BPF_IND|size);
5725 tmp->s.k = off_ll + off_nl;
5727 sappend(inst->s, s);
5730 * Do the computation only if the packet contains
5731 * the protocol in question.
5733 b = gen_proto_abbrev(proto);
5735 gen_and(inst->b, b);
5748 * The offset is relative to the beginning of
5749 * the transport-layer header.
5751 * Load the X register with the length of the IPv4 header
5752 * (plus the offset of the link-layer header, if it's
5753 * a variable-length header), in bytes.
5755 * XXX - are there any cases where we want
5757 * XXX - we should, if we're built with
5758 * IPv6 support, generate code to load either
5759 * IPv4, IPv6, or both, as appropriate.
5761 s = gen_loadx_iphdrlen();
5764 * The X register now contains the sum of the length
5765 * of any variable-length header preceding the link-layer
5766 * header and the length of the network-layer header.
5767 * Load into the A register the offset relative to
5768 * the beginning of the transport layer header,
5769 * add the X register to that, move that to the
5770 * X register, and load with an offset from the
5771 * X register equal to the offset of the network
5772 * layer header relative to the beginning of
5773 * the link-layer header plus the length of any
5774 * fixed-length header preceding the link-layer
5777 sappend(s, xfer_to_a(inst));
5778 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5779 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5780 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5781 tmp->s.k = off_ll + off_nl;
5782 sappend(inst->s, s);
5785 * Do the computation only if the packet contains
5786 * the protocol in question - which is true only
5787 * if this is an IP datagram and is the first or
5788 * only fragment of that datagram.
5790 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5792 gen_and(inst->b, b);
5794 gen_and(gen_proto_abbrev(Q_IP), b);
5800 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5804 inst->regno = regno;
5805 s = new_stmt(BPF_ST);
5807 sappend(inst->s, s);
5813 gen_relation(code, a0, a1, reversed)
5815 struct arth *a0, *a1;
5818 struct slist *s0, *s1, *s2;
5819 struct block *b, *tmp;
5823 if (code == BPF_JEQ) {
5824 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5825 b = new_block(JMP(code));
5829 b = new_block(BPF_JMP|code|BPF_X);
5835 sappend(a0->s, a1->s);
5839 free_reg(a0->regno);
5840 free_reg(a1->regno);
5842 /* 'and' together protocol checks */
5845 gen_and(a0->b, tmp = a1->b);
5861 int regno = alloc_reg();
5862 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5865 s = new_stmt(BPF_LD|BPF_LEN);
5866 s->next = new_stmt(BPF_ST);
5867 s->next->s.k = regno;
5882 a = (struct arth *)newchunk(sizeof(*a));
5886 s = new_stmt(BPF_LD|BPF_IMM);
5888 s->next = new_stmt(BPF_ST);
5904 s = new_stmt(BPF_ALU|BPF_NEG);
5907 s = new_stmt(BPF_ST);
5915 gen_arth(code, a0, a1)
5917 struct arth *a0, *a1;
5919 struct slist *s0, *s1, *s2;
5923 s2 = new_stmt(BPF_ALU|BPF_X|code);
5928 sappend(a0->s, a1->s);
5930 free_reg(a0->regno);
5931 free_reg(a1->regno);
5933 s0 = new_stmt(BPF_ST);
5934 a0->regno = s0->s.k = alloc_reg();
5941 * Here we handle simple allocation of the scratch registers.
5942 * If too many registers are alloc'd, the allocator punts.
5944 static int regused[BPF_MEMWORDS];
5948 * Return the next free register.
5953 int n = BPF_MEMWORDS;
5956 if (regused[curreg])
5957 curreg = (curreg + 1) % BPF_MEMWORDS;
5959 regused[curreg] = 1;
5963 bpf_error("too many registers needed to evaluate expression");
5969 * Return a register to the table so it can
5979 static struct block *
5986 s = new_stmt(BPF_LD|BPF_LEN);
5987 b = new_block(JMP(jmp));
5998 return gen_len(BPF_JGE, n);
6002 * Actually, this is less than or equal.
6010 b = gen_len(BPF_JGT, n);
6017 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6018 * the beginning of the link-layer header.
6019 * XXX - that means you can't test values in the radiotap header, but
6020 * as that header is difficult if not impossible to parse generally
6021 * without a loop, that might not be a severe problem. A new keyword
6022 * "radio" could be added for that, although what you'd really want
6023 * would be a way of testing particular radio header values, which
6024 * would generate code appropriate to the radio header in question.
6027 gen_byteop(op, idx, val)
6038 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6041 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6045 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6049 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
6053 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
6057 b = new_block(JMP(BPF_JEQ));
6064 static u_char abroadcast[] = { 0x0 };
6067 gen_broadcast(proto)
6070 bpf_u_int32 hostmask;
6071 struct block *b0, *b1, *b2;
6072 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6080 case DLT_ARCNET_LINUX:
6081 return gen_ahostop(abroadcast, Q_DST);
6083 return gen_ehostop(ebroadcast, Q_DST);
6085 return gen_fhostop(ebroadcast, Q_DST);
6087 return gen_thostop(ebroadcast, Q_DST);
6088 case DLT_IEEE802_11:
6089 case DLT_IEEE802_11_RADIO_AVS:
6090 case DLT_IEEE802_11_RADIO:
6092 case DLT_PRISM_HEADER:
6093 return gen_wlanhostop(ebroadcast, Q_DST);
6094 case DLT_IP_OVER_FC:
6095 return gen_ipfchostop(ebroadcast, Q_DST);
6099 * Check that the packet doesn't begin with an
6100 * LE Control marker. (We've already generated
6103 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6108 * Now check the MAC address.
6110 b0 = gen_ehostop(ebroadcast, Q_DST);
6116 bpf_error("not a broadcast link");
6121 b0 = gen_linktype(ETHERTYPE_IP);
6122 hostmask = ~netmask;
6123 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
6124 b2 = gen_mcmp(OR_NET, 16, BPF_W,
6125 (bpf_int32)(~0 & hostmask), hostmask);
6130 bpf_error("only link-layer/IP broadcast filters supported");
6136 * Generate code to test the low-order bit of a MAC address (that's
6137 * the bottom bit of the *first* byte).
6139 static struct block *
6140 gen_mac_multicast(offset)
6143 register struct block *b0;
6144 register struct slist *s;
6146 /* link[offset] & 1 != 0 */
6147 s = gen_load_a(OR_LINK, offset, BPF_B);
6148 b0 = new_block(JMP(BPF_JSET));
6155 gen_multicast(proto)
6158 register struct block *b0, *b1, *b2;
6159 register struct slist *s;
6167 case DLT_ARCNET_LINUX:
6168 /* all ARCnet multicasts use the same address */
6169 return gen_ahostop(abroadcast, Q_DST);
6171 /* ether[0] & 1 != 0 */
6172 return gen_mac_multicast(0);
6175 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6177 * XXX - was that referring to bit-order issues?
6179 /* fddi[1] & 1 != 0 */
6180 return gen_mac_multicast(1);
6182 /* tr[2] & 1 != 0 */
6183 return gen_mac_multicast(2);
6184 case DLT_IEEE802_11:
6185 case DLT_IEEE802_11_RADIO_AVS:
6187 case DLT_IEEE802_11_RADIO:
6188 case DLT_PRISM_HEADER:
6192 * For control frames, there is no DA.
6194 * For management frames, DA is at an
6195 * offset of 4 from the beginning of
6198 * For data frames, DA is at an offset
6199 * of 4 from the beginning of the packet
6200 * if To DS is clear and at an offset of
6201 * 16 from the beginning of the packet
6206 * Generate the tests to be done for data frames.
6208 * First, check for To DS set, i.e. "link[1] & 0x01".
6210 s = gen_load_a(OR_LINK, 1, BPF_B);
6211 b1 = new_block(JMP(BPF_JSET));
6212 b1->s.k = 0x01; /* To DS */
6216 * If To DS is set, the DA is at 16.
6218 b0 = gen_mac_multicast(16);
6222 * Now, check for To DS not set, i.e. check
6223 * "!(link[1] & 0x01)".
6225 s = gen_load_a(OR_LINK, 1, BPF_B);
6226 b2 = new_block(JMP(BPF_JSET));
6227 b2->s.k = 0x01; /* To DS */
6232 * If To DS is not set, the DA is at 4.
6234 b1 = gen_mac_multicast(4);
6238 * Now OR together the last two checks. That gives
6239 * the complete set of checks for data frames.
6244 * Now check for a data frame.
6245 * I.e, check "link[0] & 0x08".
6247 s = gen_load_a(OR_LINK, 0, BPF_B);
6248 b1 = new_block(JMP(BPF_JSET));
6253 * AND that with the checks done for data frames.
6258 * If the high-order bit of the type value is 0, this
6259 * is a management frame.
6260 * I.e, check "!(link[0] & 0x08)".
6262 s = gen_load_a(OR_LINK, 0, BPF_B);
6263 b2 = new_block(JMP(BPF_JSET));
6269 * For management frames, the DA is at 4.
6271 b1 = gen_mac_multicast(4);
6275 * OR that with the checks done for data frames.
6276 * That gives the checks done for management and
6282 * If the low-order bit of the type value is 1,
6283 * this is either a control frame or a frame
6284 * with a reserved type, and thus not a
6287 * I.e., check "!(link[0] & 0x04)".
6289 s = gen_load_a(OR_LINK, 0, BPF_B);
6290 b1 = new_block(JMP(BPF_JSET));
6296 * AND that with the checks for data and management
6301 case DLT_IP_OVER_FC:
6302 b0 = gen_mac_multicast(2);
6307 * Check that the packet doesn't begin with an
6308 * LE Control marker. (We've already generated
6311 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6315 /* ether[off_mac] & 1 != 0 */
6316 b0 = gen_mac_multicast(off_mac);
6324 /* Link not known to support multicasts */
6328 b0 = gen_linktype(ETHERTYPE_IP);
6329 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
6335 b0 = gen_linktype(ETHERTYPE_IPV6);
6336 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
6341 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6347 * generate command for inbound/outbound. It's here so we can
6348 * make it link-type specific. 'dir' = 0 implies "inbound",
6349 * = 1 implies "outbound".
6355 register struct block *b0;
6358 * Only some data link types support inbound/outbound qualifiers.
6362 b0 = gen_relation(BPF_JEQ,
6363 gen_load(Q_LINK, gen_loadi(0), 1),
6371 * Match packets sent by this machine.
6373 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6376 * Match packets sent to this machine.
6377 * (No broadcast or multicast packets, or
6378 * packets sent to some other machine and
6379 * received promiscuously.)
6381 * XXX - packets sent to other machines probably
6382 * shouldn't be matched, but what about broadcast
6383 * or multicast packets we received?
6385 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6389 #ifdef HAVE_NET_PFVAR_H
6391 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6392 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6398 /* match outgoing packets */
6399 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6401 /* match incoming packets */
6402 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6406 case DLT_JUNIPER_MFR:
6407 case DLT_JUNIPER_MLFR:
6408 case DLT_JUNIPER_MLPPP:
6409 case DLT_JUNIPER_ATM1:
6410 case DLT_JUNIPER_ATM2:
6411 case DLT_JUNIPER_PPPOE:
6412 case DLT_JUNIPER_PPPOE_ATM:
6413 case DLT_JUNIPER_GGSN:
6414 case DLT_JUNIPER_ES:
6415 case DLT_JUNIPER_MONITOR:
6416 case DLT_JUNIPER_SERVICES:
6417 case DLT_JUNIPER_ETHER:
6418 case DLT_JUNIPER_PPP:
6419 case DLT_JUNIPER_FRELAY:
6420 case DLT_JUNIPER_CHDLC:
6421 case DLT_JUNIPER_VP:
6422 /* juniper flags (including direction) are stored
6423 * the byte after the 3-byte magic number */
6425 /* match outgoing packets */
6426 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6428 /* match incoming packets */
6429 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6434 bpf_error("inbound/outbound not supported on linktype %d",
6442 #ifdef HAVE_NET_PFVAR_H
6443 /* PF firewall log matched interface */
6445 gen_pf_ifname(const char *ifname)
6450 if (linktype == DLT_PFLOG) {
6451 len = sizeof(((struct pfloghdr *)0)->ifname);
6452 off = offsetof(struct pfloghdr, ifname);
6454 bpf_error("ifname not supported on linktype 0x%x", linktype);
6457 if (strlen(ifname) >= len) {
6458 bpf_error("ifname interface names can only be %d characters",
6462 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6466 /* PF firewall log ruleset name */
6468 gen_pf_ruleset(char *ruleset)
6472 if (linktype != DLT_PFLOG) {
6473 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6476 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6477 bpf_error("ruleset names can only be %ld characters",
6478 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6481 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6482 strlen(ruleset), (const u_char *)ruleset);
6486 /* PF firewall log rule number */
6492 if (linktype == DLT_PFLOG) {
6493 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6496 bpf_error("rnr not supported on linktype 0x%x", linktype);
6503 /* PF firewall log sub-rule number */
6505 gen_pf_srnr(int srnr)
6509 if (linktype != DLT_PFLOG) {
6510 bpf_error("srnr not supported on linktype 0x%x", linktype);
6514 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6519 /* PF firewall log reason code */
6521 gen_pf_reason(int reason)
6525 if (linktype == DLT_PFLOG) {
6526 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6529 bpf_error("reason not supported on linktype 0x%x", linktype);
6536 /* PF firewall log action */
6538 gen_pf_action(int action)
6542 if (linktype == DLT_PFLOG) {
6543 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6546 bpf_error("action not supported on linktype 0x%x", linktype);
6552 #else /* !HAVE_NET_PFVAR_H */
6554 gen_pf_ifname(const char *ifname)
6556 bpf_error("libpcap was compiled without pf support");
6562 gen_pf_ruleset(char *ruleset)
6564 bpf_error("libpcap was compiled on a machine without pf support");
6572 bpf_error("libpcap was compiled on a machine without pf support");
6578 gen_pf_srnr(int srnr)
6580 bpf_error("libpcap was compiled on a machine without pf support");
6586 gen_pf_reason(int reason)
6588 bpf_error("libpcap was compiled on a machine without pf support");
6594 gen_pf_action(int action)
6596 bpf_error("libpcap was compiled on a machine without pf support");
6600 #endif /* HAVE_NET_PFVAR_H */
6602 /* IEEE 802.11 wireless header */
6604 gen_p80211_type(int type, int mask)
6608 if (linktype != DLT_IEEE802_11 && linktype != DLT_IEEE802_11_RADIO) {
6609 bpf_error("action not supported on linktype 0x%x\n", linktype);
6612 b0 = gen_mcmp(OR_LINK, offsetof(struct ieee80211_frame, i_fc[0]),
6613 BPF_B, (bpf_int32)type, (bpf_int32)mask);
6619 register const u_char *eaddr;
6622 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6623 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6624 return gen_ahostop(eaddr, (int)q.dir);
6626 bpf_error("ARCnet address used in non-arc expression");
6631 static struct block *
6632 gen_ahostop(eaddr, dir)
6633 register const u_char *eaddr;
6636 register struct block *b0, *b1;
6639 /* src comes first, different from Ethernet */
6641 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6644 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6647 b0 = gen_ahostop(eaddr, Q_SRC);
6648 b1 = gen_ahostop(eaddr, Q_DST);
6654 b0 = gen_ahostop(eaddr, Q_SRC);
6655 b1 = gen_ahostop(eaddr, Q_DST);
6664 * support IEEE 802.1Q VLAN trunk over ethernet
6670 struct block *b0, *b1;
6672 /* can't check for VLAN-encapsulated packets inside MPLS */
6673 if (label_stack_depth > 0)
6674 bpf_error("no VLAN match after MPLS");
6677 * Change the offsets to point to the type and data fields within
6678 * the VLAN packet. Just increment the offsets, so that we
6679 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6680 * capture VLAN 200 encapsulated within VLAN 100.
6682 * XXX - this is a bit of a kludge. If we were to split the
6683 * compiler into a parser that parses an expression and
6684 * generates an expression tree, and a code generator that
6685 * takes an expression tree (which could come from our
6686 * parser or from some other parser) and generates BPF code,
6687 * we could perhaps make the offsets parameters of routines
6688 * and, in the handler for an "AND" node, pass to subnodes
6689 * other than the VLAN node the adjusted offsets.
6691 * This would mean that "vlan" would, instead of changing the
6692 * behavior of *all* tests after it, change only the behavior
6693 * of tests ANDed with it. That would change the documented
6694 * semantics of "vlan", which might break some expressions.
6695 * However, it would mean that "(vlan and ip) or ip" would check
6696 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6697 * checking only for VLAN-encapsulated IP, so that could still
6698 * be considered worth doing; it wouldn't break expressions
6699 * that are of the form "vlan and ..." or "vlan N and ...",
6700 * which I suspect are the most common expressions involving
6701 * "vlan". "vlan or ..." doesn't necessarily do what the user
6702 * would really want, now, as all the "or ..." tests would
6703 * be done assuming a VLAN, even though the "or" could be viewed
6704 * as meaning "or, if this isn't a VLAN packet...".
6706 orig_linktype = off_linktype; /* save original values */
6718 bpf_error("no VLAN support for data link type %d",
6723 /* check for VLAN */
6724 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6726 /* If a specific VLAN is requested, check VLAN id */
6727 if (vlan_num >= 0) {
6728 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6744 struct block *b0,*b1;
6747 * Change the offsets to point to the type and data fields within
6748 * the MPLS packet. Just increment the offsets, so that we
6749 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6750 * capture packets with an outer label of 100000 and an inner
6753 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6757 if (label_stack_depth > 0) {
6758 /* just match the bottom-of-stack bit clear */
6759 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6762 * Indicate that we're checking MPLS-encapsulated headers,
6763 * to make sure higher level code generators don't try to
6764 * match against IP-related protocols such as Q_ARP, Q_RARP
6769 case DLT_C_HDLC: /* fall through */
6771 b0 = gen_linktype(ETHERTYPE_MPLS);
6775 b0 = gen_linktype(PPP_MPLS_UCAST);
6778 /* FIXME add other DLT_s ...
6779 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6780 * leave it for now */
6783 bpf_error("no MPLS support for data link type %d",
6791 /* If a specific MPLS label is requested, check it */
6792 if (label_num >= 0) {
6793 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6794 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6795 0xfffff000); /* only compare the first 20 bits */
6802 label_stack_depth++;
6807 * Support PPPOE discovery and session.
6812 /* check for PPPoE discovery */
6813 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6822 * Test against the PPPoE session link-layer type.
6824 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6827 * Change the offsets to point to the type and data fields within
6830 * XXX - this is a bit of a kludge. If we were to split the
6831 * compiler into a parser that parses an expression and
6832 * generates an expression tree, and a code generator that
6833 * takes an expression tree (which could come from our
6834 * parser or from some other parser) and generates BPF code,
6835 * we could perhaps make the offsets parameters of routines
6836 * and, in the handler for an "AND" node, pass to subnodes
6837 * other than the PPPoE node the adjusted offsets.
6839 * This would mean that "pppoes" would, instead of changing the
6840 * behavior of *all* tests after it, change only the behavior
6841 * of tests ANDed with it. That would change the documented
6842 * semantics of "pppoes", which might break some expressions.
6843 * However, it would mean that "(pppoes and ip) or ip" would check
6844 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6845 * checking only for VLAN-encapsulated IP, so that could still
6846 * be considered worth doing; it wouldn't break expressions
6847 * that are of the form "pppoes and ..." which I suspect are the
6848 * most common expressions involving "pppoes". "pppoes or ..."
6849 * doesn't necessarily do what the user would really want, now,
6850 * as all the "or ..." tests would be done assuming PPPoE, even
6851 * though the "or" could be viewed as meaning "or, if this isn't
6852 * a PPPoE packet...".
6854 orig_linktype = off_linktype; /* save original values */
6858 * The "network-layer" protocol is PPPoE, which has a 6-byte
6859 * PPPoE header, followed by PPP payload, so we set the
6860 * offsets to the network layer offset plus 6 bytes for
6861 * the PPPoE header plus the values appropriate for PPP when
6862 * encapsulated in Ethernet (which means there's no HDLC
6865 off_linktype = orig_nl + 6;
6866 off_nl = orig_nl + 6 + 2;
6867 off_nl_nosnap = orig_nl + 6 + 2;
6870 * Set the link-layer type to PPP, as all subsequent tests will
6871 * be on the encapsulated PPP header.
6879 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6891 bpf_error("'vpi' supported only on raw ATM");
6892 if (off_vpi == (u_int)-1)
6894 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6900 bpf_error("'vci' supported only on raw ATM");
6901 if (off_vci == (u_int)-1)
6903 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6908 if (off_proto == (u_int)-1)
6909 abort(); /* XXX - this isn't on FreeBSD */
6910 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6915 if (off_payload == (u_int)-1)
6917 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6918 0xffffffff, jtype, reverse, jvalue);
6923 bpf_error("'callref' supported only on raw ATM");
6924 if (off_proto == (u_int)-1)
6926 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6927 jtype, reverse, jvalue);
6937 gen_atmtype_abbrev(type)
6940 struct block *b0, *b1;
6945 /* Get all packets in Meta signalling Circuit */
6947 bpf_error("'metac' supported only on raw ATM");
6948 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6949 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6954 /* Get all packets in Broadcast Circuit*/
6956 bpf_error("'bcc' supported only on raw ATM");
6957 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6958 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6963 /* Get all cells in Segment OAM F4 circuit*/
6965 bpf_error("'oam4sc' supported only on raw ATM");
6966 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6967 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6972 /* Get all cells in End-to-End OAM F4 Circuit*/
6974 bpf_error("'oam4ec' supported only on raw ATM");
6975 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6976 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6981 /* Get all packets in connection Signalling Circuit */
6983 bpf_error("'sc' supported only on raw ATM");
6984 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6985 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6990 /* Get all packets in ILMI Circuit */
6992 bpf_error("'ilmic' supported only on raw ATM");
6993 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6994 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6999 /* Get all LANE packets */
7001 bpf_error("'lane' supported only on raw ATM");
7002 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
7005 * Arrange that all subsequent tests assume LANE
7006 * rather than LLC-encapsulated packets, and set
7007 * the offsets appropriately for LANE-encapsulated
7010 * "off_mac" is the offset of the Ethernet header,
7011 * which is 2 bytes past the ATM pseudo-header
7012 * (skipping the pseudo-header and 2-byte LE Client
7013 * field). The other offsets are Ethernet offsets
7014 * relative to "off_mac".
7017 off_mac = off_payload + 2; /* MAC header */
7018 off_linktype = off_mac + 12;
7019 off_nl = off_mac + 14; /* Ethernet II */
7020 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
7024 /* Get all LLC-encapsulated packets */
7026 bpf_error("'llc' supported only on raw ATM");
7027 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
7038 * Filtering for MTP2 messages based on li value
7039 * FISU, length is null
7040 * LSSU, length is 1 or 2
7041 * MSU, length is 3 or more
7044 gen_mtp2type_abbrev(type)
7047 struct block *b0, *b1;
7052 if ( (linktype != DLT_MTP2) &&
7053 (linktype != DLT_MTP2_WITH_PHDR) )
7054 bpf_error("'fisu' supported only on MTP2");
7055 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7056 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
7060 if ( (linktype != DLT_MTP2) &&
7061 (linktype != DLT_MTP2_WITH_PHDR) )
7062 bpf_error("'lssu' supported only on MTP2");
7063 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
7064 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
7069 if ( (linktype != DLT_MTP2) &&
7070 (linktype != DLT_MTP2_WITH_PHDR) )
7071 bpf_error("'msu' supported only on MTP2");
7072 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
7082 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
7089 bpf_u_int32 val1 , val2 , val3;
7091 switch (mtp3field) {
7094 if (off_sio == (u_int)-1)
7095 bpf_error("'sio' supported only on SS7");
7096 /* sio coded on 1 byte so max value 255 */
7098 bpf_error("sio value %u too big; max value = 255",
7100 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
7101 (u_int)jtype, reverse, (u_int)jvalue);
7105 if (off_opc == (u_int)-1)
7106 bpf_error("'opc' supported only on SS7");
7107 /* opc coded on 14 bits so max value 16383 */
7109 bpf_error("opc value %u too big; max value = 16383",
7111 /* the following instructions are made to convert jvalue
7112 * to the form used to write opc in an ss7 message*/
7113 val1 = jvalue & 0x00003c00;
7115 val2 = jvalue & 0x000003fc;
7117 val3 = jvalue & 0x00000003;
7119 jvalue = val1 + val2 + val3;
7120 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
7121 (u_int)jtype, reverse, (u_int)jvalue);
7125 if (off_dpc == (u_int)-1)
7126 bpf_error("'dpc' supported only on SS7");
7127 /* dpc coded on 14 bits so max value 16383 */
7129 bpf_error("dpc value %u too big; max value = 16383",
7131 /* the following instructions are made to convert jvalue
7132 * to the forme used to write dpc in an ss7 message*/
7133 val1 = jvalue & 0x000000ff;
7135 val2 = jvalue & 0x00003f00;
7137 jvalue = val1 + val2;
7138 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
7139 (u_int)jtype, reverse, (u_int)jvalue);
7143 if (off_sls == (u_int)-1)
7144 bpf_error("'sls' supported only on SS7");
7145 /* sls coded on 4 bits so max value 15 */
7147 bpf_error("sls value %u too big; max value = 15",
7149 /* the following instruction is made to convert jvalue
7150 * to the forme used to write sls in an ss7 message*/
7151 jvalue = jvalue << 4;
7152 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
7153 (u_int)jtype,reverse, (u_int)jvalue);
7162 static struct block *
7163 gen_msg_abbrev(type)
7169 * Q.2931 signalling protocol messages for handling virtual circuits
7170 * establishment and teardown
7175 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
7179 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
7183 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
7187 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
7191 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
7194 case A_RELEASE_DONE:
7195 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
7205 gen_atmmulti_abbrev(type)
7208 struct block *b0, *b1;
7214 bpf_error("'oam' supported only on raw ATM");
7215 b1 = gen_atmmulti_abbrev(A_OAMF4);
7220 bpf_error("'oamf4' supported only on raw ATM");
7222 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
7223 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
7225 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7231 * Get Q.2931 signalling messages for switched
7232 * virtual connection
7235 bpf_error("'connectmsg' supported only on raw ATM");
7236 b0 = gen_msg_abbrev(A_SETUP);
7237 b1 = gen_msg_abbrev(A_CALLPROCEED);
7239 b0 = gen_msg_abbrev(A_CONNECT);
7241 b0 = gen_msg_abbrev(A_CONNECTACK);
7243 b0 = gen_msg_abbrev(A_RELEASE);
7245 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7247 b0 = gen_atmtype_abbrev(A_SC);
7253 bpf_error("'metaconnect' supported only on raw ATM");
7254 b0 = gen_msg_abbrev(A_SETUP);
7255 b1 = gen_msg_abbrev(A_CALLPROCEED);
7257 b0 = gen_msg_abbrev(A_CONNECT);
7259 b0 = gen_msg_abbrev(A_RELEASE);
7261 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7263 b0 = gen_atmtype_abbrev(A_METAC);