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.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.290.2.16 2008-09-22 20:16:01 guy Exp $ (LBL)";
34 #include <pcap-stdinc.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
41 * XXX - why was this included even on UNIX?
50 #include <sys/param.h>
53 #include <netinet/in.h>
69 #include "ethertype.h"
73 #include "ieee80211.h"
75 #include "sunatmpos.h"
79 #ifdef HAVE_NET_PFVAR_H
80 #include <sys/socket.h>
82 #include <net/pfvar.h>
83 #include <net/if_pflog.h>
86 #define offsetof(s, e) ((size_t)&((s *)0)->e)
90 #include <netdb.h> /* for "struct addrinfo" */
93 #include <pcap/namedb.h>
98 #define IPPROTO_SCTP 132
101 #ifdef HAVE_OS_PROTO_H
102 #include "os-proto.h"
105 #define JMP(c) ((c)|BPF_JMP|BPF_K)
108 static jmp_buf top_ctx;
109 static pcap_t *bpf_pcap;
111 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
113 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
115 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
120 static int pcap_fddipad;
125 bpf_error(const char *fmt, ...)
130 if (bpf_pcap != NULL)
131 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
138 static void init_linktype(pcap_t *);
140 static void init_regs(void);
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 beginning of the link-layer header */
153 OR_MACPL, /* relative to the end of the MAC-layer header */
154 OR_NET, /* relative to the network-layer header */
155 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
156 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
157 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
161 * We divy out chunks of memory rather than call malloc each time so
162 * we don't have to worry about leaking memory. It's probably
163 * not a big deal if all this memory was wasted but if this ever
164 * goes into a library that would probably not be a good idea.
166 * XXX - this *is* in a library....
169 #define CHUNK0SIZE 1024
175 static struct chunk chunks[NCHUNKS];
176 static int cur_chunk;
178 static void *newchunk(u_int);
179 static void freechunks(void);
180 static inline struct block *new_block(int);
181 static inline struct slist *new_stmt(int);
182 static struct block *gen_retblk(int);
183 static inline void syntax(void);
185 static void backpatch(struct block *, struct block *);
186 static void merge(struct block *, struct block *);
187 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
188 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
189 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
190 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
191 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
192 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
194 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
195 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
196 bpf_u_int32, bpf_u_int32, int, bpf_int32);
197 static struct slist *gen_load_llrel(u_int, u_int);
198 static struct slist *gen_load_macplrel(u_int, u_int);
199 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
200 static struct slist *gen_loadx_iphdrlen(void);
201 static struct block *gen_uncond(int);
202 static inline struct block *gen_true(void);
203 static inline struct block *gen_false(void);
204 static struct block *gen_ether_linktype(int);
205 static struct block *gen_linux_sll_linktype(int);
206 static struct slist *gen_load_prism_llprefixlen(void);
207 static struct slist *gen_load_avs_llprefixlen(void);
208 static struct slist *gen_load_radiotap_llprefixlen(void);
209 static struct slist *gen_load_ppi_llprefixlen(void);
210 static void insert_compute_vloffsets(struct block *);
211 static struct slist *gen_llprefixlen(void);
212 static struct slist *gen_off_macpl(void);
213 static int ethertype_to_ppptype(int);
214 static struct block *gen_linktype(int);
215 static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
216 static struct block *gen_llc_linktype(int);
217 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
219 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
221 static struct block *gen_ahostop(const u_char *, int);
222 static struct block *gen_ehostop(const u_char *, int);
223 static struct block *gen_fhostop(const u_char *, int);
224 static struct block *gen_thostop(const u_char *, int);
225 static struct block *gen_wlanhostop(const u_char *, int);
226 static struct block *gen_ipfchostop(const u_char *, int);
227 static struct block *gen_dnhostop(bpf_u_int32, int);
228 static struct block *gen_mpls_linktype(int);
229 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
231 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
234 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
236 static struct block *gen_ipfrag(void);
237 static struct block *gen_portatom(int, bpf_int32);
238 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
240 static struct block *gen_portatom6(int, bpf_int32);
241 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
243 struct block *gen_portop(int, int, int);
244 static struct block *gen_port(int, int, int);
245 struct block *gen_portrangeop(int, int, int, int);
246 static struct block *gen_portrange(int, int, int, int);
248 struct block *gen_portop6(int, int, int);
249 static struct block *gen_port6(int, int, int);
250 struct block *gen_portrangeop6(int, int, int, int);
251 static struct block *gen_portrange6(int, int, int, int);
253 static int lookup_proto(const char *, int);
254 static struct block *gen_protochain(int, int, int);
255 static struct block *gen_proto(int, int, int);
256 static struct slist *xfer_to_x(struct arth *);
257 static struct slist *xfer_to_a(struct arth *);
258 static struct block *gen_mac_multicast(int);
259 static struct block *gen_len(int, int);
260 static struct block *gen_check_802_11_data_frame(void);
262 static struct block *gen_ppi_dlt_check(void);
263 static struct block *gen_msg_abbrev(int type);
274 /* XXX Round up to nearest long. */
275 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
277 /* XXX Round up to structure boundary. */
281 cp = &chunks[cur_chunk];
282 if (n > cp->n_left) {
283 ++cp, k = ++cur_chunk;
285 bpf_error("out of memory");
286 size = CHUNK0SIZE << k;
287 cp->m = (void *)malloc(size);
289 bpf_error("out of memory");
290 memset((char *)cp->m, 0, size);
293 bpf_error("out of memory");
296 return (void *)((char *)cp->m + cp->n_left);
305 for (i = 0; i < NCHUNKS; ++i)
306 if (chunks[i].m != NULL) {
313 * A strdup whose allocations are freed after code generation is over.
317 register const char *s;
319 int n = strlen(s) + 1;
320 char *cp = newchunk(n);
326 static inline struct block *
332 p = (struct block *)newchunk(sizeof(*p));
339 static inline struct slist *
345 p = (struct slist *)newchunk(sizeof(*p));
351 static struct block *
355 struct block *b = new_block(BPF_RET|BPF_K);
364 bpf_error("syntax error in filter expression");
367 static bpf_u_int32 netmask;
372 pcap_compile(pcap_t *p, struct bpf_program *program,
373 const char *buf, int optimize, bpf_u_int32 mask)
376 const char * volatile xbuf = buf;
384 if (setjmp(top_ctx)) {
392 snaplen = pcap_snapshot(p);
394 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
395 "snaplen of 0 rejects all packets");
399 lex_init(xbuf ? xbuf : "");
407 root = gen_retblk(snaplen);
409 if (optimize && !no_optimize) {
412 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
413 bpf_error("expression rejects all packets");
415 program->bf_insns = icode_to_fcode(root, &len);
416 program->bf_len = len;
424 * entry point for using the compiler with no pcap open
425 * pass in all the stuff that is needed explicitly instead.
428 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
429 struct bpf_program *program,
430 const char *buf, int optimize, bpf_u_int32 mask)
435 p = pcap_open_dead(linktype_arg, snaplen_arg);
438 ret = pcap_compile(p, program, buf, optimize, mask);
444 * Clean up a "struct bpf_program" by freeing all the memory allocated
448 pcap_freecode(struct bpf_program *program)
451 if (program->bf_insns != NULL) {
452 free((char *)program->bf_insns);
453 program->bf_insns = NULL;
458 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
459 * which of the jt and jf fields has been resolved and which is a pointer
460 * back to another unresolved block (or nil). At least one of the fields
461 * in each block is already resolved.
464 backpatch(list, target)
465 struct block *list, *target;
482 * Merge the lists in b0 and b1, using the 'sense' field to indicate
483 * which of jt and jf is the link.
487 struct block *b0, *b1;
489 register struct block **p = &b0;
491 /* Find end of list. */
493 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
495 /* Concatenate the lists. */
503 struct block *ppi_dlt_check;
506 * Insert before the statements of the first (root) block any
507 * statements needed to load the lengths of any variable-length
508 * headers into registers.
510 * XXX - a fancier strategy would be to insert those before the
511 * statements of all blocks that use those lengths and that
512 * have no predecessors that use them, so that we only compute
513 * the lengths if we need them. There might be even better
514 * approaches than that.
516 * However, those strategies would be more complicated, and
517 * as we don't generate code to compute a length if the
518 * program has no tests that use the length, and as most
519 * tests will probably use those lengths, we would just
520 * postpone computing the lengths so that it's not done
521 * for tests that fail early, and it's not clear that's
524 insert_compute_vloffsets(p->head);
527 * For DLT_PPI captures, generate a check of the per-packet
528 * DLT value to make sure it's DLT_IEEE802_11.
530 ppi_dlt_check = gen_ppi_dlt_check();
531 if (ppi_dlt_check != NULL)
532 gen_and(ppi_dlt_check, p);
534 backpatch(p, gen_retblk(snaplen));
535 p->sense = !p->sense;
536 backpatch(p, gen_retblk(0));
542 struct block *b0, *b1;
544 backpatch(b0, b1->head);
545 b0->sense = !b0->sense;
546 b1->sense = !b1->sense;
548 b1->sense = !b1->sense;
554 struct block *b0, *b1;
556 b0->sense = !b0->sense;
557 backpatch(b0, b1->head);
558 b0->sense = !b0->sense;
567 b->sense = !b->sense;
570 static struct block *
571 gen_cmp(offrel, offset, size, v)
572 enum e_offrel offrel;
576 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
579 static struct block *
580 gen_cmp_gt(offrel, offset, size, v)
581 enum e_offrel offrel;
585 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
588 static struct block *
589 gen_cmp_ge(offrel, offset, size, v)
590 enum e_offrel offrel;
594 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
597 static struct block *
598 gen_cmp_lt(offrel, offset, size, v)
599 enum e_offrel offrel;
603 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
606 static struct block *
607 gen_cmp_le(offrel, offset, size, v)
608 enum e_offrel offrel;
612 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
615 static struct block *
616 gen_mcmp(offrel, offset, size, v, mask)
617 enum e_offrel offrel;
622 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
625 static struct block *
626 gen_bcmp(offrel, offset, size, v)
627 enum e_offrel offrel;
628 register u_int offset, size;
629 register const u_char *v;
631 register struct block *b, *tmp;
635 register const u_char *p = &v[size - 4];
636 bpf_int32 w = ((bpf_int32)p[0] << 24) |
637 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
639 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
646 register const u_char *p = &v[size - 2];
647 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
649 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
656 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
665 * AND the field of size "size" at offset "offset" relative to the header
666 * specified by "offrel" with "mask", and compare it with the value "v"
667 * with the test specified by "jtype"; if "reverse" is true, the test
668 * should test the opposite of "jtype".
670 static struct block *
671 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
672 enum e_offrel offrel;
674 bpf_u_int32 offset, size, mask, jtype;
677 struct slist *s, *s2;
680 s = gen_load_a(offrel, offset, size);
682 if (mask != 0xffffffff) {
683 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
688 b = new_block(JMP(jtype));
691 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
697 * Various code constructs need to know the layout of the data link
698 * layer. These variables give the necessary offsets from the beginning
699 * of the packet data.
703 * This is the offset of the beginning of the link-layer header from
704 * the beginning of the raw packet data.
706 * It's usually 0, except for 802.11 with a fixed-length radio header.
707 * (For 802.11 with a variable-length radio header, we have to generate
708 * code to compute that offset; off_ll is 0 in that case.)
713 * If there's a variable-length header preceding the link-layer header,
714 * "reg_off_ll" is the register number for a register containing the
715 * length of that header, and therefore the offset of the link-layer
716 * header from the beginning of the raw packet data. Otherwise,
717 * "reg_off_ll" is -1.
719 static int reg_off_ll;
722 * This is the offset of the beginning of the MAC-layer header from
723 * the beginning of the link-layer header.
724 * It's usually 0, except for ATM LANE, where it's the offset, relative
725 * to the beginning of the raw packet data, of the Ethernet header.
727 static u_int off_mac;
730 * This is the offset of the beginning of the MAC-layer payload,
731 * from the beginning of the raw packet data.
733 * I.e., it's the sum of the length of the link-layer header (without,
734 * for example, any 802.2 LLC header, so it's the MAC-layer
735 * portion of that header), plus any prefix preceding the
738 static u_int off_macpl;
741 * This is 1 if the offset of the beginning of the MAC-layer payload
742 * from the beginning of the link-layer header is variable-length.
744 static int off_macpl_is_variable;
747 * If the link layer has variable_length headers, "reg_off_macpl"
748 * is the register number for a register containing the length of the
749 * link-layer header plus the length of any variable-length header
750 * preceding the link-layer header. Otherwise, "reg_off_macpl"
753 static int reg_off_macpl;
756 * "off_linktype" is the offset to information in the link-layer header
757 * giving the packet type. This offset is relative to the beginning
758 * of the link-layer header (i.e., it doesn't include off_ll).
760 * For Ethernet, it's the offset of the Ethernet type field.
762 * For link-layer types that always use 802.2 headers, it's the
763 * offset of the LLC header.
765 * For PPP, it's the offset of the PPP type field.
767 * For Cisco HDLC, it's the offset of the CHDLC type field.
769 * For BSD loopback, it's the offset of the AF_ value.
771 * For Linux cooked sockets, it's the offset of the type field.
773 * It's set to -1 for no encapsulation, in which case, IP is assumed.
775 static u_int off_linktype;
778 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
779 * checks to check the PPP header, assumed to follow a LAN-style link-
780 * layer header and a PPPoE session header.
782 static int is_pppoes = 0;
785 * TRUE if the link layer includes an ATM pseudo-header.
787 static int is_atm = 0;
790 * TRUE if "lane" appeared in the filter; it causes us to generate
791 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
793 static int is_lane = 0;
796 * These are offsets for the ATM pseudo-header.
798 static u_int off_vpi;
799 static u_int off_vci;
800 static u_int off_proto;
803 * These are offsets for the MTP2 fields.
808 * These are offsets for the MTP3 fields.
810 static u_int off_sio;
811 static u_int off_opc;
812 static u_int off_dpc;
813 static u_int off_sls;
816 * This is the offset of the first byte after the ATM pseudo_header,
817 * or -1 if there is no ATM pseudo-header.
819 static u_int off_payload;
822 * These are offsets to the beginning of the network-layer header.
823 * They are relative to the beginning of the MAC-layer payload (i.e.,
824 * they don't include off_ll or off_macpl).
826 * If the link layer never uses 802.2 LLC:
828 * "off_nl" and "off_nl_nosnap" are the same.
830 * If the link layer always uses 802.2 LLC:
832 * "off_nl" is the offset if there's a SNAP header following
835 * "off_nl_nosnap" is the offset if there's no SNAP header.
837 * If the link layer is Ethernet:
839 * "off_nl" is the offset if the packet is an Ethernet II packet
840 * (we assume no 802.3+802.2+SNAP);
842 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
843 * with an 802.2 header following it.
846 static u_int off_nl_nosnap;
854 linktype = pcap_datalink(p);
856 pcap_fddipad = p->fddipad;
860 * Assume it's not raw ATM with a pseudo-header, for now.
871 * And that we're not doing PPPoE.
876 * And assume we're not doing SS7.
885 * Also assume it's not 802.11.
889 off_macpl_is_variable = 0;
893 label_stack_depth = 0;
903 off_nl = 0; /* XXX in reality, variable! */
904 off_nl_nosnap = 0; /* no 802.2 LLC */
907 case DLT_ARCNET_LINUX:
910 off_nl = 0; /* XXX in reality, variable! */
911 off_nl_nosnap = 0; /* no 802.2 LLC */
916 off_macpl = 14; /* Ethernet header length */
917 off_nl = 0; /* Ethernet II */
918 off_nl_nosnap = 3; /* 802.3+802.2 */
923 * SLIP doesn't have a link level type. The 16 byte
924 * header is hacked into our SLIP driver.
929 off_nl_nosnap = 0; /* no 802.2 LLC */
933 /* XXX this may be the same as the DLT_PPP_BSDOS case */
938 off_nl_nosnap = 0; /* no 802.2 LLC */
946 off_nl_nosnap = 0; /* no 802.2 LLC */
953 off_nl_nosnap = 0; /* no 802.2 LLC */
958 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
959 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
963 off_nl_nosnap = 0; /* no 802.2 LLC */
968 * This does no include the Ethernet header, and
969 * only covers session state.
974 off_nl_nosnap = 0; /* no 802.2 LLC */
981 off_nl_nosnap = 0; /* no 802.2 LLC */
986 * FDDI doesn't really have a link-level type field.
987 * We set "off_linktype" to the offset of the LLC header.
989 * To check for Ethernet types, we assume that SSAP = SNAP
990 * is being used and pick out the encapsulated Ethernet type.
991 * XXX - should we generate code to check for SNAP?
995 off_linktype += pcap_fddipad;
997 off_macpl = 13; /* FDDI MAC header length */
999 off_macpl += pcap_fddipad;
1001 off_nl = 8; /* 802.2+SNAP */
1002 off_nl_nosnap = 3; /* 802.2 */
1007 * Token Ring doesn't really have a link-level type field.
1008 * We set "off_linktype" to the offset of the LLC header.
1010 * To check for Ethernet types, we assume that SSAP = SNAP
1011 * is being used and pick out the encapsulated Ethernet type.
1012 * XXX - should we generate code to check for SNAP?
1014 * XXX - the header is actually variable-length.
1015 * Some various Linux patched versions gave 38
1016 * as "off_linktype" and 40 as "off_nl"; however,
1017 * if a token ring packet has *no* routing
1018 * information, i.e. is not source-routed, the correct
1019 * values are 20 and 22, as they are in the vanilla code.
1021 * A packet is source-routed iff the uppermost bit
1022 * of the first byte of the source address, at an
1023 * offset of 8, has the uppermost bit set. If the
1024 * packet is source-routed, the total number of bytes
1025 * of routing information is 2 plus bits 0x1F00 of
1026 * the 16-bit value at an offset of 14 (shifted right
1027 * 8 - figure out which byte that is).
1030 off_macpl = 14; /* Token Ring MAC header length */
1031 off_nl = 8; /* 802.2+SNAP */
1032 off_nl_nosnap = 3; /* 802.2 */
1035 case DLT_IEEE802_11:
1036 case DLT_PRISM_HEADER:
1037 case DLT_IEEE802_11_RADIO_AVS:
1038 case DLT_IEEE802_11_RADIO:
1040 * 802.11 doesn't really have a link-level type field.
1041 * We set "off_linktype" to the offset of the LLC header.
1043 * To check for Ethernet types, we assume that SSAP = SNAP
1044 * is being used and pick out the encapsulated Ethernet type.
1045 * XXX - should we generate code to check for SNAP?
1047 * We also handle variable-length radio headers here.
1048 * The Prism header is in theory variable-length, but in
1049 * practice it's always 144 bytes long. However, some
1050 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1051 * sometimes or always supply an AVS header, so we
1052 * have to check whether the radio header is a Prism
1053 * header or an AVS header, so, in practice, it's
1057 off_macpl = 0; /* link-layer header is variable-length */
1058 off_macpl_is_variable = 1;
1059 off_nl = 8; /* 802.2+SNAP */
1060 off_nl_nosnap = 3; /* 802.2 */
1065 * At the moment we treat PPI the same way that we treat
1066 * normal Radiotap encoded packets. The difference is in
1067 * the function that generates the code at the beginning
1068 * to compute the header length. Since this code generator
1069 * of PPI supports bare 802.11 encapsulation only (i.e.
1070 * the encapsulated DLT should be DLT_IEEE802_11) we
1071 * generate code to check for this too.
1074 off_macpl = 0; /* link-layer header is variable-length */
1075 off_macpl_is_variable = 1;
1076 off_nl = 8; /* 802.2+SNAP */
1077 off_nl_nosnap = 3; /* 802.2 */
1080 case DLT_ATM_RFC1483:
1081 case DLT_ATM_CLIP: /* Linux ATM defines this */
1083 * assume routed, non-ISO PDUs
1084 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1086 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1087 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1088 * latter would presumably be treated the way PPPoE
1089 * should be, so you can do "pppoe and udp port 2049"
1090 * or "pppoa and tcp port 80" and have it check for
1091 * PPPo{A,E} and a PPP protocol of IP and....
1094 off_macpl = 0; /* packet begins with LLC header */
1095 off_nl = 8; /* 802.2+SNAP */
1096 off_nl_nosnap = 3; /* 802.2 */
1101 * Full Frontal ATM; you get AALn PDUs with an ATM
1105 off_vpi = SUNATM_VPI_POS;
1106 off_vci = SUNATM_VCI_POS;
1107 off_proto = PROTO_POS;
1108 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1109 off_payload = SUNATM_PKT_BEGIN_POS;
1110 off_linktype = off_payload;
1111 off_macpl = off_payload; /* if LLC-encapsulated */
1112 off_nl = 8; /* 802.2+SNAP */
1113 off_nl_nosnap = 3; /* 802.2 */
1120 off_nl_nosnap = 0; /* no 802.2 LLC */
1123 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1127 off_nl_nosnap = 0; /* no 802.2 LLC */
1132 * LocalTalk does have a 1-byte type field in the LLAP header,
1133 * but really it just indicates whether there is a "short" or
1134 * "long" DDP packet following.
1139 off_nl_nosnap = 0; /* no 802.2 LLC */
1142 case DLT_IP_OVER_FC:
1144 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1145 * link-level type field. We set "off_linktype" to the
1146 * offset of the LLC header.
1148 * To check for Ethernet types, we assume that SSAP = SNAP
1149 * is being used and pick out the encapsulated Ethernet type.
1150 * XXX - should we generate code to check for SNAP? RFC
1151 * 2625 says SNAP should be used.
1155 off_nl = 8; /* 802.2+SNAP */
1156 off_nl_nosnap = 3; /* 802.2 */
1161 * XXX - we should set this to handle SNAP-encapsulated
1162 * frames (NLPID of 0x80).
1167 off_nl_nosnap = 0; /* no 802.2 LLC */
1171 * the only BPF-interesting FRF.16 frames are non-control frames;
1172 * Frame Relay has a variable length link-layer
1173 * so lets start with offset 4 for now and increments later on (FIXME);
1179 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1182 case DLT_APPLE_IP_OVER_IEEE1394:
1186 off_nl_nosnap = 0; /* no 802.2 LLC */
1189 case DLT_LINUX_IRDA:
1191 * Currently, only raw "link[N:M]" filtering is supported.
1201 * Currently, only raw "link[N:M]" filtering is supported.
1209 case DLT_SYMANTEC_FIREWALL:
1212 off_nl = 0; /* Ethernet II */
1213 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1216 #ifdef HAVE_NET_PFVAR_H
1219 off_macpl = PFLOG_HDRLEN;
1221 off_nl_nosnap = 0; /* no 802.2 LLC */
1225 case DLT_JUNIPER_MFR:
1226 case DLT_JUNIPER_MLFR:
1227 case DLT_JUNIPER_MLPPP:
1228 case DLT_JUNIPER_PPP:
1229 case DLT_JUNIPER_CHDLC:
1230 case DLT_JUNIPER_FRELAY:
1234 off_nl_nosnap = -1; /* no 802.2 LLC */
1237 case DLT_JUNIPER_ATM1:
1238 off_linktype = 4; /* in reality variable between 4-8 */
1239 off_macpl = 4; /* in reality variable between 4-8 */
1244 case DLT_JUNIPER_ATM2:
1245 off_linktype = 8; /* in reality variable between 8-12 */
1246 off_macpl = 8; /* in reality variable between 8-12 */
1251 /* frames captured on a Juniper PPPoE service PIC
1252 * contain raw ethernet frames */
1253 case DLT_JUNIPER_PPPOE:
1254 case DLT_JUNIPER_ETHER:
1257 off_nl = 18; /* Ethernet II */
1258 off_nl_nosnap = 21; /* 802.3+802.2 */
1261 case DLT_JUNIPER_PPPOE_ATM:
1265 off_nl_nosnap = -1; /* no 802.2 LLC */
1268 case DLT_JUNIPER_GGSN:
1272 off_nl_nosnap = -1; /* no 802.2 LLC */
1275 case DLT_JUNIPER_ES:
1277 off_macpl = -1; /* not really a network layer but raw IP addresses */
1278 off_nl = -1; /* not really a network layer but raw IP addresses */
1279 off_nl_nosnap = -1; /* no 802.2 LLC */
1282 case DLT_JUNIPER_MONITOR:
1285 off_nl = 0; /* raw IP/IP6 header */
1286 off_nl_nosnap = -1; /* no 802.2 LLC */
1289 case DLT_JUNIPER_SERVICES:
1291 off_macpl = -1; /* L3 proto location dep. on cookie type */
1292 off_nl = -1; /* L3 proto location dep. on cookie type */
1293 off_nl_nosnap = -1; /* no 802.2 LLC */
1296 case DLT_JUNIPER_VP:
1303 case DLT_JUNIPER_ST:
1310 case DLT_JUNIPER_ISM:
1329 case DLT_MTP2_WITH_PHDR:
1362 case DLT_LINUX_LAPD:
1364 * Currently, only raw "link[N:M]" filtering is supported.
1374 * Currently, only raw "link[N:M]" filtering is supported.
1382 case DLT_BLUETOOTH_HCI_H4:
1384 * Currently, only raw "link[N:M]" filtering is supported.
1394 * Currently, only raw "link[N:M]" filtering is supported.
1404 * Currently, only raw "link[N:M]" filtering is supported.
1412 case DLT_IEEE802_15_4_LINUX:
1414 * Currently, only raw "link[N:M]" filtering is supported.
1422 case DLT_IEEE802_16_MAC_CPS_RADIO:
1424 * Currently, only raw "link[N:M]" filtering is supported.
1432 case DLT_IEEE802_15_4:
1434 * Currently, only raw "link[N:M]" filtering is supported.
1444 * Currently, only raw "link[N:M]" filtering is supported.
1454 * Currently, only raw "link[N:M]" filtering is supported.
1464 * Currently, only raw "link[N:M]" filtering is supported.
1472 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
1474 * Currently, only raw "link[N:M]" filtering is supported.
1484 * Currently, only raw "link[N:M]" filtering is supported.
1486 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1488 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1489 off_nl_nosnap = -1; /* no 802.2 LLC */
1490 off_mac = 1; /* step over the kiss length byte */
1493 case DLT_IEEE802_15_4_NONASK_PHY:
1495 * Currently, only raw "link[N:M]" filtering is supported.
1503 bpf_error("unknown data link type %d", linktype);
1508 * Load a value relative to the beginning of the link-layer header.
1509 * The link-layer header doesn't necessarily begin at the beginning
1510 * of the packet data; there might be a variable-length prefix containing
1511 * radio information.
1513 static struct slist *
1514 gen_load_llrel(offset, size)
1517 struct slist *s, *s2;
1519 s = gen_llprefixlen();
1522 * If "s" is non-null, it has code to arrange that the X register
1523 * contains the length of the prefix preceding the link-layer
1526 * Otherwise, the length of the prefix preceding the link-layer
1527 * header is "off_ll".
1531 * There's a variable-length prefix preceding the
1532 * link-layer header. "s" points to a list of statements
1533 * that put the length of that prefix into the X register.
1534 * do an indirect load, to use the X register as an offset.
1536 s2 = new_stmt(BPF_LD|BPF_IND|size);
1541 * There is no variable-length header preceding the
1542 * link-layer header; add in off_ll, which, if there's
1543 * a fixed-length header preceding the link-layer header,
1544 * is the length of that header.
1546 s = new_stmt(BPF_LD|BPF_ABS|size);
1547 s->s.k = offset + off_ll;
1553 * Load a value relative to the beginning of the MAC-layer payload.
1555 static struct slist *
1556 gen_load_macplrel(offset, size)
1559 struct slist *s, *s2;
1561 s = gen_off_macpl();
1564 * If s is non-null, the offset of the MAC-layer payload is
1565 * variable, and s points to a list of instructions that
1566 * arrange that the X register contains that offset.
1568 * Otherwise, the offset of the MAC-layer payload is constant,
1569 * and is in off_macpl.
1573 * The offset of the MAC-layer payload is in the X
1574 * register. Do an indirect load, to use the X register
1577 s2 = new_stmt(BPF_LD|BPF_IND|size);
1582 * The offset of the MAC-layer payload is constant,
1583 * and is in off_macpl; load the value at that offset
1584 * plus the specified offset.
1586 s = new_stmt(BPF_LD|BPF_ABS|size);
1587 s->s.k = off_macpl + offset;
1593 * Load a value relative to the beginning of the specified header.
1595 static struct slist *
1596 gen_load_a(offrel, offset, size)
1597 enum e_offrel offrel;
1600 struct slist *s, *s2;
1605 s = new_stmt(BPF_LD|BPF_ABS|size);
1610 s = gen_load_llrel(offset, size);
1614 s = gen_load_macplrel(offset, size);
1618 s = gen_load_macplrel(off_nl + offset, size);
1622 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1627 * Load the X register with the length of the IPv4 header
1628 * (plus the offset of the link-layer header, if it's
1629 * preceded by a variable-length header such as a radio
1630 * header), in bytes.
1632 s = gen_loadx_iphdrlen();
1635 * Load the item at {offset of the MAC-layer payload} +
1636 * {offset, relative to the start of the MAC-layer
1637 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1638 * {specified offset}.
1640 * (If the offset of the MAC-layer payload is variable,
1641 * it's included in the value in the X register, and
1644 s2 = new_stmt(BPF_LD|BPF_IND|size);
1645 s2->s.k = off_macpl + off_nl + offset;
1650 s = gen_load_macplrel(off_nl + 40 + offset, size);
1661 * Generate code to load into the X register the sum of the length of
1662 * the IPv4 header and any variable-length header preceding the link-layer
1665 static struct slist *
1666 gen_loadx_iphdrlen()
1668 struct slist *s, *s2;
1670 s = gen_off_macpl();
1673 * There's a variable-length prefix preceding the
1674 * link-layer header, or the link-layer header is itself
1675 * variable-length. "s" points to a list of statements
1676 * that put the offset of the MAC-layer payload into
1679 * The 4*([k]&0xf) addressing mode can't be used, as we
1680 * don't have a constant offset, so we have to load the
1681 * value in question into the A register and add to it
1682 * the value from the X register.
1684 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1687 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1690 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1695 * The A register now contains the length of the
1696 * IP header. We need to add to it the offset of
1697 * the MAC-layer payload, which is still in the X
1698 * register, and move the result into the X register.
1700 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1701 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1704 * There is no variable-length header preceding the
1705 * link-layer header, and the link-layer header is
1706 * fixed-length; load the length of the IPv4 header,
1707 * which is at an offset of off_nl from the beginning
1708 * of the MAC-layer payload, and thus at an offset
1709 * of off_mac_pl + off_nl from the beginning of the
1712 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1713 s->s.k = off_macpl + off_nl;
1718 static struct block *
1725 s = new_stmt(BPF_LD|BPF_IMM);
1727 b = new_block(JMP(BPF_JEQ));
1733 static inline struct block *
1736 return gen_uncond(1);
1739 static inline struct block *
1742 return gen_uncond(0);
1746 * Byte-swap a 32-bit number.
1747 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1748 * big-endian platforms.)
1750 #define SWAPLONG(y) \
1751 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1754 * Generate code to match a particular packet type.
1756 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1757 * value, if <= ETHERMTU. We use that to determine whether to
1758 * match the type/length field or to check the type/length field for
1759 * a value <= ETHERMTU to see whether it's a type field and then do
1760 * the appropriate test.
1762 static struct block *
1763 gen_ether_linktype(proto)
1766 struct block *b0, *b1;
1772 case LLCSAP_NETBEUI:
1774 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1775 * so we check the DSAP and SSAP.
1777 * LLCSAP_IP checks for IP-over-802.2, rather
1778 * than IP-over-Ethernet or IP-over-SNAP.
1780 * XXX - should we check both the DSAP and the
1781 * SSAP, like this, or should we check just the
1782 * DSAP, as we do for other types <= ETHERMTU
1783 * (i.e., other SAP values)?
1785 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1787 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1788 ((proto << 8) | proto));
1796 * Ethernet_II frames, which are Ethernet
1797 * frames with a frame type of ETHERTYPE_IPX;
1799 * Ethernet_802.3 frames, which are 802.3
1800 * frames (i.e., the type/length field is
1801 * a length field, <= ETHERMTU, rather than
1802 * a type field) with the first two bytes
1803 * after the Ethernet/802.3 header being
1806 * Ethernet_802.2 frames, which are 802.3
1807 * frames with an 802.2 LLC header and
1808 * with the IPX LSAP as the DSAP in the LLC
1811 * Ethernet_SNAP frames, which are 802.3
1812 * frames with an LLC header and a SNAP
1813 * header and with an OUI of 0x000000
1814 * (encapsulated Ethernet) and a protocol
1815 * ID of ETHERTYPE_IPX in the SNAP header.
1817 * XXX - should we generate the same code both
1818 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1822 * This generates code to check both for the
1823 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1825 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1826 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1830 * Now we add code to check for SNAP frames with
1831 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1833 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1837 * Now we generate code to check for 802.3
1838 * frames in general.
1840 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1844 * Now add the check for 802.3 frames before the
1845 * check for Ethernet_802.2 and Ethernet_802.3,
1846 * as those checks should only be done on 802.3
1847 * frames, not on Ethernet frames.
1852 * Now add the check for Ethernet_II frames, and
1853 * do that before checking for the other frame
1856 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1857 (bpf_int32)ETHERTYPE_IPX);
1861 case ETHERTYPE_ATALK:
1862 case ETHERTYPE_AARP:
1864 * EtherTalk (AppleTalk protocols on Ethernet link
1865 * layer) may use 802.2 encapsulation.
1869 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1870 * we check for an Ethernet type field less than
1871 * 1500, which means it's an 802.3 length field.
1873 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1877 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1878 * SNAP packets with an organization code of
1879 * 0x080007 (Apple, for Appletalk) and a protocol
1880 * type of ETHERTYPE_ATALK (Appletalk).
1882 * 802.2-encapsulated ETHERTYPE_AARP packets are
1883 * SNAP packets with an organization code of
1884 * 0x000000 (encapsulated Ethernet) and a protocol
1885 * type of ETHERTYPE_AARP (Appletalk ARP).
1887 if (proto == ETHERTYPE_ATALK)
1888 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1889 else /* proto == ETHERTYPE_AARP */
1890 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1894 * Check for Ethernet encapsulation (Ethertalk
1895 * phase 1?); we just check for the Ethernet
1898 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1904 if (proto <= ETHERMTU) {
1906 * This is an LLC SAP value, so the frames
1907 * that match would be 802.2 frames.
1908 * Check that the frame is an 802.2 frame
1909 * (i.e., that the length/type field is
1910 * a length field, <= ETHERMTU) and
1911 * then check the DSAP.
1913 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1915 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1921 * This is an Ethernet type, so compare
1922 * the length/type field with it (if
1923 * the frame is an 802.2 frame, the length
1924 * field will be <= ETHERMTU, and, as
1925 * "proto" is > ETHERMTU, this test
1926 * will fail and the frame won't match,
1927 * which is what we want).
1929 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1936 * Generate code to match a particular packet type.
1938 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1939 * value, if <= ETHERMTU. We use that to determine whether to
1940 * match the type field or to check the type field for the special
1941 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1943 static struct block *
1944 gen_linux_sll_linktype(proto)
1947 struct block *b0, *b1;
1953 case LLCSAP_NETBEUI:
1955 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1956 * so we check the DSAP and SSAP.
1958 * LLCSAP_IP checks for IP-over-802.2, rather
1959 * than IP-over-Ethernet or IP-over-SNAP.
1961 * XXX - should we check both the DSAP and the
1962 * SSAP, like this, or should we check just the
1963 * DSAP, as we do for other types <= ETHERMTU
1964 * (i.e., other SAP values)?
1966 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1967 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1968 ((proto << 8) | proto));
1974 * Ethernet_II frames, which are Ethernet
1975 * frames with a frame type of ETHERTYPE_IPX;
1977 * Ethernet_802.3 frames, which have a frame
1978 * type of LINUX_SLL_P_802_3;
1980 * Ethernet_802.2 frames, which are 802.3
1981 * frames with an 802.2 LLC header (i.e, have
1982 * a frame type of LINUX_SLL_P_802_2) and
1983 * with the IPX LSAP as the DSAP in the LLC
1986 * Ethernet_SNAP frames, which are 802.3
1987 * frames with an LLC header and a SNAP
1988 * header and with an OUI of 0x000000
1989 * (encapsulated Ethernet) and a protocol
1990 * ID of ETHERTYPE_IPX in the SNAP header.
1992 * First, do the checks on LINUX_SLL_P_802_2
1993 * frames; generate the check for either
1994 * Ethernet_802.2 or Ethernet_SNAP frames, and
1995 * then put a check for LINUX_SLL_P_802_2 frames
1998 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1999 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
2001 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2005 * Now check for 802.3 frames and OR that with
2006 * the previous test.
2008 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
2012 * Now add the check for Ethernet_II frames, and
2013 * do that before checking for the other frame
2016 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2017 (bpf_int32)ETHERTYPE_IPX);
2021 case ETHERTYPE_ATALK:
2022 case ETHERTYPE_AARP:
2024 * EtherTalk (AppleTalk protocols on Ethernet link
2025 * layer) may use 802.2 encapsulation.
2029 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2030 * we check for the 802.2 protocol type in the
2031 * "Ethernet type" field.
2033 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2036 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2037 * SNAP packets with an organization code of
2038 * 0x080007 (Apple, for Appletalk) and a protocol
2039 * type of ETHERTYPE_ATALK (Appletalk).
2041 * 802.2-encapsulated ETHERTYPE_AARP packets are
2042 * SNAP packets with an organization code of
2043 * 0x000000 (encapsulated Ethernet) and a protocol
2044 * type of ETHERTYPE_AARP (Appletalk ARP).
2046 if (proto == ETHERTYPE_ATALK)
2047 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2048 else /* proto == ETHERTYPE_AARP */
2049 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2053 * Check for Ethernet encapsulation (Ethertalk
2054 * phase 1?); we just check for the Ethernet
2057 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2063 if (proto <= ETHERMTU) {
2065 * This is an LLC SAP value, so the frames
2066 * that match would be 802.2 frames.
2067 * Check for the 802.2 protocol type
2068 * in the "Ethernet type" field, and
2069 * then check the DSAP.
2071 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2073 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2079 * This is an Ethernet type, so compare
2080 * the length/type field with it (if
2081 * the frame is an 802.2 frame, the length
2082 * field will be <= ETHERMTU, and, as
2083 * "proto" is > ETHERMTU, this test
2084 * will fail and the frame won't match,
2085 * which is what we want).
2087 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2093 static struct slist *
2094 gen_load_prism_llprefixlen()
2096 struct slist *s1, *s2;
2097 struct slist *sjeq_avs_cookie;
2098 struct slist *sjcommon;
2101 * This code is not compatible with the optimizer, as
2102 * we are generating jmp instructions within a normal
2103 * slist of instructions
2108 * Generate code to load the length of the radio header into
2109 * the register assigned to hold that length, if one has been
2110 * assigned. (If one hasn't been assigned, no code we've
2111 * generated uses that prefix, so we don't need to generate any
2114 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2115 * or always use the AVS header rather than the Prism header.
2116 * We load a 4-byte big-endian value at the beginning of the
2117 * raw packet data, and see whether, when masked with 0xFFFFF000,
2118 * it's equal to 0x80211000. If so, that indicates that it's
2119 * an AVS header (the masked-out bits are the version number).
2120 * Otherwise, it's a Prism header.
2122 * XXX - the Prism header is also, in theory, variable-length,
2123 * but no known software generates headers that aren't 144
2126 if (reg_off_ll != -1) {
2130 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2134 * AND it with 0xFFFFF000.
2136 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2137 s2->s.k = 0xFFFFF000;
2141 * Compare with 0x80211000.
2143 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2144 sjeq_avs_cookie->s.k = 0x80211000;
2145 sappend(s1, sjeq_avs_cookie);
2150 * The 4 bytes at an offset of 4 from the beginning of
2151 * the AVS header are the length of the AVS header.
2152 * That field is big-endian.
2154 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2157 sjeq_avs_cookie->s.jt = s2;
2160 * Now jump to the code to allocate a register
2161 * into which to save the header length and
2162 * store the length there. (The "jump always"
2163 * instruction needs to have the k field set;
2164 * it's added to the PC, so, as we're jumping
2165 * over a single instruction, it should be 1.)
2167 sjcommon = new_stmt(JMP(BPF_JA));
2169 sappend(s1, sjcommon);
2172 * Now for the code that handles the Prism header.
2173 * Just load the length of the Prism header (144)
2174 * into the A register. Have the test for an AVS
2175 * header branch here if we don't have an AVS header.
2177 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2180 sjeq_avs_cookie->s.jf = s2;
2183 * Now allocate a register to hold that value and store
2184 * it. The code for the AVS header will jump here after
2185 * loading the length of the AVS header.
2187 s2 = new_stmt(BPF_ST);
2188 s2->s.k = reg_off_ll;
2190 sjcommon->s.jf = s2;
2193 * Now move it into the X register.
2195 s2 = new_stmt(BPF_MISC|BPF_TAX);
2203 static struct slist *
2204 gen_load_avs_llprefixlen()
2206 struct slist *s1, *s2;
2209 * Generate code to load the length of the AVS header into
2210 * the register assigned to hold that length, if one has been
2211 * assigned. (If one hasn't been assigned, no code we've
2212 * generated uses that prefix, so we don't need to generate any
2215 if (reg_off_ll != -1) {
2217 * The 4 bytes at an offset of 4 from the beginning of
2218 * the AVS header are the length of the AVS header.
2219 * That field is big-endian.
2221 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2225 * Now allocate a register to hold that value and store
2228 s2 = new_stmt(BPF_ST);
2229 s2->s.k = reg_off_ll;
2233 * Now move it into the X register.
2235 s2 = new_stmt(BPF_MISC|BPF_TAX);
2243 static struct slist *
2244 gen_load_radiotap_llprefixlen()
2246 struct slist *s1, *s2;
2249 * Generate code to load the length of the radiotap header into
2250 * the register assigned to hold that length, if one has been
2251 * assigned. (If one hasn't been assigned, no code we've
2252 * generated uses that prefix, so we don't need to generate any
2255 if (reg_off_ll != -1) {
2257 * The 2 bytes at offsets of 2 and 3 from the beginning
2258 * of the radiotap header are the length of the radiotap
2259 * header; unfortunately, it's little-endian, so we have
2260 * to load it a byte at a time and construct the value.
2264 * Load the high-order byte, at an offset of 3, shift it
2265 * left a byte, and put the result in the X register.
2267 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2269 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2272 s2 = new_stmt(BPF_MISC|BPF_TAX);
2276 * Load the next byte, at an offset of 2, and OR the
2277 * value from the X register into it.
2279 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2282 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2286 * Now allocate a register to hold that value and store
2289 s2 = new_stmt(BPF_ST);
2290 s2->s.k = reg_off_ll;
2294 * Now move it into the X register.
2296 s2 = new_stmt(BPF_MISC|BPF_TAX);
2305 * At the moment we treat PPI as normal Radiotap encoded
2306 * packets. The difference is in the function that generates
2307 * the code at the beginning to compute the header length.
2308 * Since this code generator of PPI supports bare 802.11
2309 * encapsulation only (i.e. the encapsulated DLT should be
2310 * DLT_IEEE802_11) we generate code to check for this too;
2311 * that's done in finish_parse().
2313 static struct slist *
2314 gen_load_ppi_llprefixlen()
2316 struct slist *s1, *s2;
2319 * Generate code to load the length of the radiotap header
2320 * into the register assigned to hold that length, if one has
2323 if (reg_off_ll != -1) {
2325 * The 2 bytes at offsets of 2 and 3 from the beginning
2326 * of the radiotap header are the length of the radiotap
2327 * header; unfortunately, it's little-endian, so we have
2328 * to load it a byte at a time and construct the value.
2332 * Load the high-order byte, at an offset of 3, shift it
2333 * left a byte, and put the result in the X register.
2335 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2337 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2340 s2 = new_stmt(BPF_MISC|BPF_TAX);
2344 * Load the next byte, at an offset of 2, and OR the
2345 * value from the X register into it.
2347 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2350 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2354 * Now allocate a register to hold that value and store
2357 s2 = new_stmt(BPF_ST);
2358 s2->s.k = reg_off_ll;
2362 * Now move it into the X register.
2364 s2 = new_stmt(BPF_MISC|BPF_TAX);
2373 * Load a value relative to the beginning of the link-layer header after the 802.11
2374 * header, i.e. LLC_SNAP.
2375 * The link-layer header doesn't necessarily begin at the beginning
2376 * of the packet data; there might be a variable-length prefix containing
2377 * radio information.
2379 static struct slist *
2380 gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2383 struct slist *sjset_data_frame_1;
2384 struct slist *sjset_data_frame_2;
2385 struct slist *sjset_qos;
2386 struct slist *sjset_radiotap_flags;
2387 struct slist *sjset_radiotap_tsft;
2388 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2389 struct slist *s_roundup;
2391 if (reg_off_macpl == -1) {
2393 * No register has been assigned to the offset of
2394 * the MAC-layer payload, which means nobody needs
2395 * it; don't bother computing it - just return
2396 * what we already have.
2402 * This code is not compatible with the optimizer, as
2403 * we are generating jmp instructions within a normal
2404 * slist of instructions
2409 * If "s" is non-null, it has code to arrange that the X register
2410 * contains the length of the prefix preceding the link-layer
2413 * Otherwise, the length of the prefix preceding the link-layer
2414 * header is "off_ll".
2418 * There is no variable-length header preceding the
2419 * link-layer header.
2421 * Load the length of the fixed-length prefix preceding
2422 * the link-layer header (if any) into the X register,
2423 * and store it in the reg_off_macpl register.
2424 * That length is off_ll.
2426 s = new_stmt(BPF_LDX|BPF_IMM);
2431 * The X register contains the offset of the beginning of the
2432 * link-layer header; add 24, which is the minimum length
2433 * of the MAC header for a data frame, to that, and store it
2434 * in reg_off_macpl, and then load the Frame Control field,
2435 * which is at the offset in the X register, with an indexed load.
2437 s2 = new_stmt(BPF_MISC|BPF_TXA);
2439 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2442 s2 = new_stmt(BPF_ST);
2443 s2->s.k = reg_off_macpl;
2446 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2451 * Check the Frame Control field to see if this is a data frame;
2452 * a data frame has the 0x08 bit (b3) in that field set and the
2453 * 0x04 bit (b2) clear.
2455 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2456 sjset_data_frame_1->s.k = 0x08;
2457 sappend(s, sjset_data_frame_1);
2460 * If b3 is set, test b2, otherwise go to the first statement of
2461 * the rest of the program.
2463 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2464 sjset_data_frame_2->s.k = 0x04;
2465 sappend(s, sjset_data_frame_2);
2466 sjset_data_frame_1->s.jf = snext;
2469 * If b2 is not set, this is a data frame; test the QoS bit.
2470 * Otherwise, go to the first statement of the rest of the
2473 sjset_data_frame_2->s.jt = snext;
2474 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2475 sjset_qos->s.k = 0x80; /* QoS bit */
2476 sappend(s, sjset_qos);
2479 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2481 * Otherwise, go to the first statement of the rest of the
2484 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2485 s2->s.k = reg_off_macpl;
2487 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2490 s2 = new_stmt(BPF_ST);
2491 s2->s.k = reg_off_macpl;
2495 * If we have a radiotap header, look at it to see whether
2496 * there's Atheros padding between the MAC-layer header
2499 * Note: all of the fields in the radiotap header are
2500 * little-endian, so we byte-swap all of the values
2501 * we test against, as they will be loaded as big-endian
2504 if (linktype == DLT_IEEE802_11_RADIO) {
2506 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2507 * in the presence flag?
2509 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2513 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2514 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2515 sappend(s, sjset_radiotap_flags);
2518 * If not, skip all of this.
2520 sjset_radiotap_flags->s.jf = snext;
2523 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2525 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2526 new_stmt(JMP(BPF_JSET));
2527 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2528 sappend(s, sjset_radiotap_tsft);
2531 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2532 * at an offset of 16 from the beginning of the raw packet
2533 * data (8 bytes for the radiotap header and 8 bytes for
2536 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2539 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2543 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2544 sjset_tsft_datapad->s.k = 0x20;
2545 sappend(s, sjset_tsft_datapad);
2548 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2549 * at an offset of 8 from the beginning of the raw packet
2550 * data (8 bytes for the radiotap header).
2552 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2555 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2559 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2560 sjset_notsft_datapad->s.k = 0x20;
2561 sappend(s, sjset_notsft_datapad);
2564 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2565 * set, round the length of the 802.11 header to
2566 * a multiple of 4. Do that by adding 3 and then
2567 * dividing by and multiplying by 4, which we do by
2570 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2571 s_roundup->s.k = reg_off_macpl;
2572 sappend(s, s_roundup);
2573 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2576 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2579 s2 = new_stmt(BPF_ST);
2580 s2->s.k = reg_off_macpl;
2583 sjset_tsft_datapad->s.jt = s_roundup;
2584 sjset_tsft_datapad->s.jf = snext;
2585 sjset_notsft_datapad->s.jt = s_roundup;
2586 sjset_notsft_datapad->s.jf = snext;
2588 sjset_qos->s.jf = snext;
2594 insert_compute_vloffsets(b)
2600 * For link-layer types that have a variable-length header
2601 * preceding the link-layer header, generate code to load
2602 * the offset of the link-layer header into the register
2603 * assigned to that offset, if any.
2607 case DLT_PRISM_HEADER:
2608 s = gen_load_prism_llprefixlen();
2611 case DLT_IEEE802_11_RADIO_AVS:
2612 s = gen_load_avs_llprefixlen();
2615 case DLT_IEEE802_11_RADIO:
2616 s = gen_load_radiotap_llprefixlen();
2620 s = gen_load_ppi_llprefixlen();
2629 * For link-layer types that have a variable-length link-layer
2630 * header, generate code to load the offset of the MAC-layer
2631 * payload into the register assigned to that offset, if any.
2635 case DLT_IEEE802_11:
2636 case DLT_PRISM_HEADER:
2637 case DLT_IEEE802_11_RADIO_AVS:
2638 case DLT_IEEE802_11_RADIO:
2640 s = gen_load_802_11_header_len(s, b->stmts);
2645 * If we have any offset-loading code, append all the
2646 * existing statements in the block to those statements,
2647 * and make the resulting list the list of statements
2651 sappend(s, b->stmts);
2656 static struct block *
2657 gen_ppi_dlt_check(void)
2659 struct slist *s_load_dlt;
2662 if (linktype == DLT_PPI)
2664 /* Create the statements that check for the DLT
2666 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2667 s_load_dlt->s.k = 4;
2669 b = new_block(JMP(BPF_JEQ));
2671 b->stmts = s_load_dlt;
2672 b->s.k = SWAPLONG(DLT_IEEE802_11);
2682 static struct slist *
2683 gen_prism_llprefixlen(void)
2687 if (reg_off_ll == -1) {
2689 * We haven't yet assigned a register for the length
2690 * of the radio header; allocate one.
2692 reg_off_ll = alloc_reg();
2696 * Load the register containing the radio length
2697 * into the X register.
2699 s = new_stmt(BPF_LDX|BPF_MEM);
2700 s->s.k = reg_off_ll;
2704 static struct slist *
2705 gen_avs_llprefixlen(void)
2709 if (reg_off_ll == -1) {
2711 * We haven't yet assigned a register for the length
2712 * of the AVS header; allocate one.
2714 reg_off_ll = alloc_reg();
2718 * Load the register containing the AVS length
2719 * into the X register.
2721 s = new_stmt(BPF_LDX|BPF_MEM);
2722 s->s.k = reg_off_ll;
2726 static struct slist *
2727 gen_radiotap_llprefixlen(void)
2731 if (reg_off_ll == -1) {
2733 * We haven't yet assigned a register for the length
2734 * of the radiotap header; allocate one.
2736 reg_off_ll = alloc_reg();
2740 * Load the register containing the radiotap length
2741 * into the X register.
2743 s = new_stmt(BPF_LDX|BPF_MEM);
2744 s->s.k = reg_off_ll;
2749 * At the moment we treat PPI as normal Radiotap encoded
2750 * packets. The difference is in the function that generates
2751 * the code at the beginning to compute the header length.
2752 * Since this code generator of PPI supports bare 802.11
2753 * encapsulation only (i.e. the encapsulated DLT should be
2754 * DLT_IEEE802_11) we generate code to check for this too.
2756 static struct slist *
2757 gen_ppi_llprefixlen(void)
2761 if (reg_off_ll == -1) {
2763 * We haven't yet assigned a register for the length
2764 * of the radiotap header; allocate one.
2766 reg_off_ll = alloc_reg();
2770 * Load the register containing the PPI length
2771 * into the X register.
2773 s = new_stmt(BPF_LDX|BPF_MEM);
2774 s->s.k = reg_off_ll;
2779 * Generate code to compute the link-layer header length, if necessary,
2780 * putting it into the X register, and to return either a pointer to a
2781 * "struct slist" for the list of statements in that code, or NULL if
2782 * no code is necessary.
2784 static struct slist *
2785 gen_llprefixlen(void)
2789 case DLT_PRISM_HEADER:
2790 return gen_prism_llprefixlen();
2792 case DLT_IEEE802_11_RADIO_AVS:
2793 return gen_avs_llprefixlen();
2795 case DLT_IEEE802_11_RADIO:
2796 return gen_radiotap_llprefixlen();
2799 return gen_ppi_llprefixlen();
2807 * Generate code to load the register containing the offset of the
2808 * MAC-layer payload into the X register; if no register for that offset
2809 * has been allocated, allocate it first.
2811 static struct slist *
2816 if (off_macpl_is_variable) {
2817 if (reg_off_macpl == -1) {
2819 * We haven't yet assigned a register for the offset
2820 * of the MAC-layer payload; allocate one.
2822 reg_off_macpl = alloc_reg();
2826 * Load the register containing the offset of the MAC-layer
2827 * payload into the X register.
2829 s = new_stmt(BPF_LDX|BPF_MEM);
2830 s->s.k = reg_off_macpl;
2834 * That offset isn't variable, so we don't need to
2835 * generate any code.
2842 * Map an Ethernet type to the equivalent PPP type.
2845 ethertype_to_ppptype(proto)
2855 case ETHERTYPE_IPV6:
2864 case ETHERTYPE_ATALK:
2878 * I'm assuming the "Bridging PDU"s that go
2879 * over PPP are Spanning Tree Protocol
2893 * Generate code to match a particular packet type by matching the
2894 * link-layer type field or fields in the 802.2 LLC header.
2896 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2897 * value, if <= ETHERMTU.
2899 static struct block *
2903 struct block *b0, *b1, *b2;
2905 /* are we checking MPLS-encapsulated packets? */
2906 if (label_stack_depth > 0) {
2910 /* FIXME add other L3 proto IDs */
2911 return gen_mpls_linktype(Q_IP);
2913 case ETHERTYPE_IPV6:
2915 /* FIXME add other L3 proto IDs */
2916 return gen_mpls_linktype(Q_IPV6);
2919 bpf_error("unsupported protocol over mpls");
2925 * Are we testing PPPoE packets?
2929 * The PPPoE session header is part of the
2930 * MAC-layer payload, so all references
2931 * should be relative to the beginning of
2936 * We use Ethernet protocol types inside libpcap;
2937 * map them to the corresponding PPP protocol types.
2939 proto = ethertype_to_ppptype(proto);
2940 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2946 return gen_ether_linktype(proto);
2954 proto = (proto << 8 | LLCSAP_ISONS);
2958 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2965 case DLT_IEEE802_11:
2966 case DLT_PRISM_HEADER:
2967 case DLT_IEEE802_11_RADIO_AVS:
2968 case DLT_IEEE802_11_RADIO:
2971 * Check that we have a data frame.
2973 b0 = gen_check_802_11_data_frame();
2976 * Now check for the specified link-layer type.
2978 b1 = gen_llc_linktype(proto);
2986 * XXX - check for asynchronous frames, as per RFC 1103.
2988 return gen_llc_linktype(proto);
2994 * XXX - check for LLC PDUs, as per IEEE 802.5.
2996 return gen_llc_linktype(proto);
3000 case DLT_ATM_RFC1483:
3002 case DLT_IP_OVER_FC:
3003 return gen_llc_linktype(proto);
3009 * If "is_lane" is set, check for a LANE-encapsulated
3010 * version of this protocol, otherwise check for an
3011 * LLC-encapsulated version of this protocol.
3013 * We assume LANE means Ethernet, not Token Ring.
3017 * Check that the packet doesn't begin with an
3018 * LE Control marker. (We've already generated
3021 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3026 * Now generate an Ethernet test.
3028 b1 = gen_ether_linktype(proto);
3033 * Check for LLC encapsulation and then check the
3036 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3037 b1 = gen_llc_linktype(proto);
3045 return gen_linux_sll_linktype(proto);
3050 case DLT_SLIP_BSDOS:
3053 * These types don't provide any type field; packets
3054 * are always IPv4 or IPv6.
3056 * XXX - for IPv4, check for a version number of 4, and,
3057 * for IPv6, check for a version number of 6?
3062 /* Check for a version number of 4. */
3063 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3065 case ETHERTYPE_IPV6:
3066 /* Check for a version number of 6. */
3067 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3071 return gen_false(); /* always false */
3078 case DLT_PPP_SERIAL:
3081 * We use Ethernet protocol types inside libpcap;
3082 * map them to the corresponding PPP protocol types.
3084 proto = ethertype_to_ppptype(proto);
3085 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3091 * We use Ethernet protocol types inside libpcap;
3092 * map them to the corresponding PPP protocol types.
3098 * Also check for Van Jacobson-compressed IP.
3099 * XXX - do this for other forms of PPP?
3101 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3102 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3104 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3109 proto = ethertype_to_ppptype(proto);
3110 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3120 * For DLT_NULL, the link-layer header is a 32-bit
3121 * word containing an AF_ value in *host* byte order,
3122 * and for DLT_ENC, the link-layer header begins
3123 * with a 32-bit work containing an AF_ value in
3126 * In addition, if we're reading a saved capture file,
3127 * the host byte order in the capture may not be the
3128 * same as the host byte order on this machine.
3130 * For DLT_LOOP, the link-layer header is a 32-bit
3131 * word containing an AF_ value in *network* byte order.
3133 * XXX - AF_ values may, unfortunately, be platform-
3134 * dependent; for example, FreeBSD's AF_INET6 is 24
3135 * whilst NetBSD's and OpenBSD's is 26.
3137 * This means that, when reading a capture file, just
3138 * checking for our AF_INET6 value won't work if the
3139 * capture file came from another OS.
3148 case ETHERTYPE_IPV6:
3155 * Not a type on which we support filtering.
3156 * XXX - support those that have AF_ values
3157 * #defined on this platform, at least?
3162 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3164 * The AF_ value is in host byte order, but
3165 * the BPF interpreter will convert it to
3166 * network byte order.
3168 * If this is a save file, and it's from a
3169 * machine with the opposite byte order to
3170 * ours, we byte-swap the AF_ value.
3172 * Then we run it through "htonl()", and
3173 * generate code to compare against the result.
3175 if (bpf_pcap->sf.rfile != NULL &&
3176 bpf_pcap->sf.swapped)
3177 proto = SWAPLONG(proto);
3178 proto = htonl(proto);
3180 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3182 #ifdef HAVE_NET_PFVAR_H
3185 * af field is host byte order in contrast to the rest of
3188 if (proto == ETHERTYPE_IP)
3189 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3190 BPF_B, (bpf_int32)AF_INET));
3192 else if (proto == ETHERTYPE_IPV6)
3193 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3194 BPF_B, (bpf_int32)AF_INET6));
3200 #endif /* HAVE_NET_PFVAR_H */
3203 case DLT_ARCNET_LINUX:
3205 * XXX should we check for first fragment if the protocol
3214 case ETHERTYPE_IPV6:
3215 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3216 (bpf_int32)ARCTYPE_INET6));
3220 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3221 (bpf_int32)ARCTYPE_IP);
3222 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3223 (bpf_int32)ARCTYPE_IP_OLD);
3228 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3229 (bpf_int32)ARCTYPE_ARP);
3230 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3231 (bpf_int32)ARCTYPE_ARP_OLD);
3235 case ETHERTYPE_REVARP:
3236 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3237 (bpf_int32)ARCTYPE_REVARP));
3239 case ETHERTYPE_ATALK:
3240 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3241 (bpf_int32)ARCTYPE_ATALK));
3248 case ETHERTYPE_ATALK:
3258 * XXX - assumes a 2-byte Frame Relay header with
3259 * DLCI and flags. What if the address is longer?
3265 * Check for the special NLPID for IP.
3267 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3270 case ETHERTYPE_IPV6:
3272 * Check for the special NLPID for IPv6.
3274 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3279 * Check for several OSI protocols.
3281 * Frame Relay packets typically have an OSI
3282 * NLPID at the beginning; we check for each
3285 * What we check for is the NLPID and a frame
3286 * control field of UI, i.e. 0x03 followed
3289 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3290 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3291 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3303 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3305 case DLT_JUNIPER_MFR:
3306 case DLT_JUNIPER_MLFR:
3307 case DLT_JUNIPER_MLPPP:
3308 case DLT_JUNIPER_ATM1:
3309 case DLT_JUNIPER_ATM2:
3310 case DLT_JUNIPER_PPPOE:
3311 case DLT_JUNIPER_PPPOE_ATM:
3312 case DLT_JUNIPER_GGSN:
3313 case DLT_JUNIPER_ES:
3314 case DLT_JUNIPER_MONITOR:
3315 case DLT_JUNIPER_SERVICES:
3316 case DLT_JUNIPER_ETHER:
3317 case DLT_JUNIPER_PPP:
3318 case DLT_JUNIPER_FRELAY:
3319 case DLT_JUNIPER_CHDLC:
3320 case DLT_JUNIPER_VP:
3321 case DLT_JUNIPER_ST:
3322 case DLT_JUNIPER_ISM:
3323 /* just lets verify the magic number for now -
3324 * on ATM we may have up to 6 different encapsulations on the wire
3325 * and need a lot of heuristics to figure out that the payload
3328 * FIXME encapsulation specific BPF_ filters
3330 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3332 case DLT_LINUX_IRDA:
3333 bpf_error("IrDA link-layer type filtering not implemented");
3336 bpf_error("DOCSIS link-layer type filtering not implemented");
3339 case DLT_MTP2_WITH_PHDR:
3340 bpf_error("MTP2 link-layer type filtering not implemented");
3343 bpf_error("ERF link-layer type filtering not implemented");
3347 bpf_error("PFSYNC link-layer type filtering not implemented");
3350 case DLT_LINUX_LAPD:
3351 bpf_error("LAPD link-layer type filtering not implemented");
3355 bpf_error("USB link-layer type filtering not implemented");
3357 case DLT_BLUETOOTH_HCI_H4:
3358 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3359 bpf_error("Bluetooth link-layer type filtering not implemented");
3362 bpf_error("CAN20B link-layer type filtering not implemented");
3364 case DLT_IEEE802_15_4:
3365 case DLT_IEEE802_15_4_LINUX:
3366 case DLT_IEEE802_15_4_NONASK_PHY:
3367 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3369 case DLT_IEEE802_16_MAC_CPS_RADIO:
3370 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3373 bpf_error("SITA link-layer type filtering not implemented");
3376 bpf_error("RAIF1 link-layer type filtering not implemented");
3379 bpf_error("IPMB link-layer type filtering not implemented");
3382 bpf_error("AX.25 link-layer type filtering not implemented");
3386 * All the types that have no encapsulation should either be
3387 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3388 * all packets are IP packets, or should be handled in some
3389 * special case, if none of them are (if some are and some
3390 * aren't, the lack of encapsulation is a problem, as we'd
3391 * have to find some other way of determining the packet type).
3393 * Therefore, if "off_linktype" is -1, there's an error.
3395 if (off_linktype == (u_int)-1)
3399 * Any type not handled above should always have an Ethernet
3400 * type at an offset of "off_linktype".
3402 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3406 * Check for an LLC SNAP packet with a given organization code and
3407 * protocol type; we check the entire contents of the 802.2 LLC and
3408 * snap headers, checking for DSAP and SSAP of SNAP and a control
3409 * field of 0x03 in the LLC header, and for the specified organization
3410 * code and protocol type in the SNAP header.
3412 static struct block *
3413 gen_snap(orgcode, ptype)
3414 bpf_u_int32 orgcode;
3417 u_char snapblock[8];
3419 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3420 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3421 snapblock[2] = 0x03; /* control = UI */
3422 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3423 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3424 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3425 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3426 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3427 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3431 * Generate code to match a particular packet type, for link-layer types
3432 * using 802.2 LLC headers.
3434 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3435 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3437 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3438 * value, if <= ETHERMTU. We use that to determine whether to
3439 * match the DSAP or both DSAP and LSAP or to check the OUI and
3440 * protocol ID in a SNAP header.
3442 static struct block *
3443 gen_llc_linktype(proto)
3447 * XXX - handle token-ring variable-length header.
3453 case LLCSAP_NETBEUI:
3455 * XXX - should we check both the DSAP and the
3456 * SSAP, like this, or should we check just the
3457 * DSAP, as we do for other types <= ETHERMTU
3458 * (i.e., other SAP values)?
3460 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3461 ((proto << 8) | proto));
3465 * XXX - are there ever SNAP frames for IPX on
3466 * non-Ethernet 802.x networks?
3468 return gen_cmp(OR_MACPL, 0, BPF_B,
3469 (bpf_int32)LLCSAP_IPX);
3471 case ETHERTYPE_ATALK:
3473 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3474 * SNAP packets with an organization code of
3475 * 0x080007 (Apple, for Appletalk) and a protocol
3476 * type of ETHERTYPE_ATALK (Appletalk).
3478 * XXX - check for an organization code of
3479 * encapsulated Ethernet as well?
3481 return gen_snap(0x080007, ETHERTYPE_ATALK);
3485 * XXX - we don't have to check for IPX 802.3
3486 * here, but should we check for the IPX Ethertype?
3488 if (proto <= ETHERMTU) {
3490 * This is an LLC SAP value, so check
3493 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3496 * This is an Ethernet type; we assume that it's
3497 * unlikely that it'll appear in the right place
3498 * at random, and therefore check only the
3499 * location that would hold the Ethernet type
3500 * in a SNAP frame with an organization code of
3501 * 0x000000 (encapsulated Ethernet).
3503 * XXX - if we were to check for the SNAP DSAP and
3504 * LSAP, as per XXX, and were also to check for an
3505 * organization code of 0x000000 (encapsulated
3506 * Ethernet), we'd do
3508 * return gen_snap(0x000000, proto);
3510 * here; for now, we don't, as per the above.
3511 * I don't know whether it's worth the extra CPU
3512 * time to do the right check or not.
3514 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3519 static struct block *
3520 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3524 u_int src_off, dst_off;
3526 struct block *b0, *b1;
3540 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3541 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3547 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3548 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3555 b0 = gen_linktype(proto);
3556 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3562 static struct block *
3563 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3564 struct in6_addr *addr;
3565 struct in6_addr *mask;
3567 u_int src_off, dst_off;
3569 struct block *b0, *b1;
3584 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3585 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3591 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3592 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3599 /* this order is important */
3600 a = (u_int32_t *)addr;
3601 m = (u_int32_t *)mask;
3602 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3603 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3605 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3607 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3609 b0 = gen_linktype(proto);
3615 static struct block *
3616 gen_ehostop(eaddr, dir)
3617 register const u_char *eaddr;
3620 register struct block *b0, *b1;
3624 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3627 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3630 b0 = gen_ehostop(eaddr, Q_SRC);
3631 b1 = gen_ehostop(eaddr, Q_DST);
3637 b0 = gen_ehostop(eaddr, Q_SRC);
3638 b1 = gen_ehostop(eaddr, Q_DST);
3647 * Like gen_ehostop, but for DLT_FDDI
3649 static struct block *
3650 gen_fhostop(eaddr, dir)
3651 register const u_char *eaddr;
3654 struct block *b0, *b1;
3659 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3661 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3666 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3668 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3672 b0 = gen_fhostop(eaddr, Q_SRC);
3673 b1 = gen_fhostop(eaddr, Q_DST);
3679 b0 = gen_fhostop(eaddr, Q_SRC);
3680 b1 = gen_fhostop(eaddr, Q_DST);
3689 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3691 static struct block *
3692 gen_thostop(eaddr, dir)
3693 register const u_char *eaddr;
3696 register struct block *b0, *b1;
3700 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3703 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3706 b0 = gen_thostop(eaddr, Q_SRC);
3707 b1 = gen_thostop(eaddr, Q_DST);
3713 b0 = gen_thostop(eaddr, Q_SRC);
3714 b1 = gen_thostop(eaddr, Q_DST);
3723 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3724 * various 802.11 + radio headers.
3726 static struct block *
3727 gen_wlanhostop(eaddr, dir)
3728 register const u_char *eaddr;
3731 register struct block *b0, *b1, *b2;
3732 register struct slist *s;
3734 #ifdef ENABLE_WLAN_FILTERING_PATCH
3737 * We need to disable the optimizer because the optimizer is buggy
3738 * and wipes out some LD instructions generated by the below
3739 * code to validate the Frame Control bits
3742 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3749 * For control frames, there is no SA.
3751 * For management frames, SA is at an
3752 * offset of 10 from the beginning of
3755 * For data frames, SA is at an offset
3756 * of 10 from the beginning of the packet
3757 * if From DS is clear, at an offset of
3758 * 16 from the beginning of the packet
3759 * if From DS is set and To DS is clear,
3760 * and an offset of 24 from the beginning
3761 * of the packet if From DS is set and To DS
3766 * Generate the tests to be done for data frames
3769 * First, check for To DS set, i.e. check "link[1] & 0x01".
3771 s = gen_load_a(OR_LINK, 1, BPF_B);
3772 b1 = new_block(JMP(BPF_JSET));
3773 b1->s.k = 0x01; /* To DS */
3777 * If To DS is set, the SA is at 24.
3779 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3783 * Now, check for To DS not set, i.e. check
3784 * "!(link[1] & 0x01)".
3786 s = gen_load_a(OR_LINK, 1, BPF_B);
3787 b2 = new_block(JMP(BPF_JSET));
3788 b2->s.k = 0x01; /* To DS */
3793 * If To DS is not set, the SA is at 16.
3795 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3799 * Now OR together the last two checks. That gives
3800 * the complete set of checks for data frames with
3806 * Now check for From DS being set, and AND that with
3807 * the ORed-together checks.
3809 s = gen_load_a(OR_LINK, 1, BPF_B);
3810 b1 = new_block(JMP(BPF_JSET));
3811 b1->s.k = 0x02; /* From DS */
3816 * Now check for data frames with From DS not set.
3818 s = gen_load_a(OR_LINK, 1, BPF_B);
3819 b2 = new_block(JMP(BPF_JSET));
3820 b2->s.k = 0x02; /* From DS */
3825 * If From DS isn't set, the SA is at 10.
3827 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3831 * Now OR together the checks for data frames with
3832 * From DS not set and for data frames with From DS
3833 * set; that gives the checks done for data frames.
3838 * Now check for a data frame.
3839 * I.e, check "link[0] & 0x08".
3841 s = gen_load_a(OR_LINK, 0, BPF_B);
3842 b1 = new_block(JMP(BPF_JSET));
3847 * AND that with the checks done for data frames.
3852 * If the high-order bit of the type value is 0, this
3853 * is a management frame.
3854 * I.e, check "!(link[0] & 0x08)".
3856 s = gen_load_a(OR_LINK, 0, BPF_B);
3857 b2 = new_block(JMP(BPF_JSET));
3863 * For management frames, the SA is at 10.
3865 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3869 * OR that with the checks done for data frames.
3870 * That gives the checks done for management and
3876 * If the low-order bit of the type value is 1,
3877 * this is either a control frame or a frame
3878 * with a reserved type, and thus not a
3881 * I.e., check "!(link[0] & 0x04)".
3883 s = gen_load_a(OR_LINK, 0, BPF_B);
3884 b1 = new_block(JMP(BPF_JSET));
3890 * AND that with the checks for data and management
3900 * For control frames, there is no DA.
3902 * For management frames, DA is at an
3903 * offset of 4 from the beginning of
3906 * For data frames, DA is at an offset
3907 * of 4 from the beginning of the packet
3908 * if To DS is clear and at an offset of
3909 * 16 from the beginning of the packet
3914 * Generate the tests to be done for data frames.
3916 * First, check for To DS set, i.e. "link[1] & 0x01".
3918 s = gen_load_a(OR_LINK, 1, BPF_B);
3919 b1 = new_block(JMP(BPF_JSET));
3920 b1->s.k = 0x01; /* To DS */
3924 * If To DS is set, the DA is at 16.
3926 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3930 * Now, check for To DS not set, i.e. check
3931 * "!(link[1] & 0x01)".
3933 s = gen_load_a(OR_LINK, 1, BPF_B);
3934 b2 = new_block(JMP(BPF_JSET));
3935 b2->s.k = 0x01; /* To DS */
3940 * If To DS is not set, the DA is at 4.
3942 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3946 * Now OR together the last two checks. That gives
3947 * the complete set of checks for data frames.
3952 * Now check for a data frame.
3953 * I.e, check "link[0] & 0x08".
3955 s = gen_load_a(OR_LINK, 0, BPF_B);
3956 b1 = new_block(JMP(BPF_JSET));
3961 * AND that with the checks done for data frames.
3966 * If the high-order bit of the type value is 0, this
3967 * is a management frame.
3968 * I.e, check "!(link[0] & 0x08)".
3970 s = gen_load_a(OR_LINK, 0, BPF_B);
3971 b2 = new_block(JMP(BPF_JSET));
3977 * For management frames, the DA is at 4.
3979 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3983 * OR that with the checks done for data frames.
3984 * That gives the checks done for management and
3990 * If the low-order bit of the type value is 1,
3991 * this is either a control frame or a frame
3992 * with a reserved type, and thus not a
3995 * I.e., check "!(link[0] & 0x04)".
3997 s = gen_load_a(OR_LINK, 0, BPF_B);
3998 b1 = new_block(JMP(BPF_JSET));
4004 * AND that with the checks for data and management
4011 * XXX - add RA, TA, and BSSID keywords?
4014 return (gen_bcmp(OR_LINK, 4, 6, eaddr));
4018 * Not present in CTS or ACK control frames.
4020 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4021 IEEE80211_FC0_TYPE_MASK);
4023 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4024 IEEE80211_FC0_SUBTYPE_MASK);
4026 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4027 IEEE80211_FC0_SUBTYPE_MASK);
4031 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4037 * Not present in control frames.
4039 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4040 IEEE80211_FC0_TYPE_MASK);
4042 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4048 * Present only if the direction mask has both "From DS"
4049 * and "To DS" set. Neither control frames nor management
4050 * frames should have both of those set, so we don't
4051 * check the frame type.
4053 b0 = gen_mcmp(OR_LINK, 1, BPF_B,
4054 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4055 b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
4060 b0 = gen_wlanhostop(eaddr, Q_SRC);
4061 b1 = gen_wlanhostop(eaddr, Q_DST);
4067 b0 = gen_wlanhostop(eaddr, Q_SRC);
4068 b1 = gen_wlanhostop(eaddr, Q_DST);
4077 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4078 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4079 * as the RFC states.)
4081 static struct block *
4082 gen_ipfchostop(eaddr, dir)
4083 register const u_char *eaddr;
4086 register struct block *b0, *b1;
4090 return gen_bcmp(OR_LINK, 10, 6, eaddr);
4093 return gen_bcmp(OR_LINK, 2, 6, eaddr);
4096 b0 = gen_ipfchostop(eaddr, Q_SRC);
4097 b1 = gen_ipfchostop(eaddr, Q_DST);
4103 b0 = gen_ipfchostop(eaddr, Q_SRC);
4104 b1 = gen_ipfchostop(eaddr, Q_DST);
4113 * This is quite tricky because there may be pad bytes in front of the
4114 * DECNET header, and then there are two possible data packet formats that
4115 * carry both src and dst addresses, plus 5 packet types in a format that
4116 * carries only the src node, plus 2 types that use a different format and
4117 * also carry just the src node.
4121 * Instead of doing those all right, we just look for data packets with
4122 * 0 or 1 bytes of padding. If you want to look at other packets, that
4123 * will require a lot more hacking.
4125 * To add support for filtering on DECNET "areas" (network numbers)
4126 * one would want to add a "mask" argument to this routine. That would
4127 * make the filter even more inefficient, although one could be clever
4128 * and not generate masking instructions if the mask is 0xFFFF.
4130 static struct block *
4131 gen_dnhostop(addr, dir)
4135 struct block *b0, *b1, *b2, *tmp;
4136 u_int offset_lh; /* offset if long header is received */
4137 u_int offset_sh; /* offset if short header is received */
4142 offset_sh = 1; /* follows flags */
4143 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4147 offset_sh = 3; /* follows flags, dstnode */
4148 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4152 /* Inefficient because we do our Calvinball dance twice */
4153 b0 = gen_dnhostop(addr, Q_SRC);
4154 b1 = gen_dnhostop(addr, Q_DST);
4160 /* Inefficient because we do our Calvinball dance twice */
4161 b0 = gen_dnhostop(addr, Q_SRC);
4162 b1 = gen_dnhostop(addr, Q_DST);
4167 bpf_error("ISO host filtering not implemented");
4172 b0 = gen_linktype(ETHERTYPE_DN);
4173 /* Check for pad = 1, long header case */
4174 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4175 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
4176 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
4177 BPF_H, (bpf_int32)ntohs((u_short)addr));
4179 /* Check for pad = 0, long header case */
4180 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4181 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4184 /* Check for pad = 1, short header case */
4185 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4186 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
4187 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4190 /* Check for pad = 0, short header case */
4191 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4192 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4196 /* Combine with test for linktype */
4202 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4203 * test the bottom-of-stack bit, and then check the version number
4204 * field in the IP header.
4206 static struct block *
4207 gen_mpls_linktype(proto)
4210 struct block *b0, *b1;
4215 /* match the bottom-of-stack bit */
4216 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4217 /* match the IPv4 version number */
4218 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
4223 /* match the bottom-of-stack bit */
4224 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4225 /* match the IPv4 version number */
4226 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
4235 static struct block *
4236 gen_host(addr, mask, proto, dir, type)
4243 struct block *b0, *b1;
4244 const char *typestr;
4254 b0 = gen_host(addr, mask, Q_IP, dir, type);
4256 * Only check for non-IPv4 addresses if we're not
4257 * checking MPLS-encapsulated packets.
4259 if (label_stack_depth == 0) {
4260 b1 = gen_host(addr, mask, Q_ARP, dir, type);
4262 b0 = gen_host(addr, mask, Q_RARP, dir, type);
4268 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
4271 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4274 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4277 bpf_error("'tcp' modifier applied to %s", typestr);
4280 bpf_error("'sctp' modifier applied to %s", typestr);
4283 bpf_error("'udp' modifier applied to %s", typestr);
4286 bpf_error("'icmp' modifier applied to %s", typestr);
4289 bpf_error("'igmp' modifier applied to %s", typestr);
4292 bpf_error("'igrp' modifier applied to %s", typestr);
4295 bpf_error("'pim' modifier applied to %s", typestr);
4298 bpf_error("'vrrp' modifier applied to %s", typestr);
4301 bpf_error("ATALK host filtering not implemented");
4304 bpf_error("AARP host filtering not implemented");
4307 return gen_dnhostop(addr, dir);
4310 bpf_error("SCA host filtering not implemented");
4313 bpf_error("LAT host filtering not implemented");
4316 bpf_error("MOPDL host filtering not implemented");
4319 bpf_error("MOPRC host filtering not implemented");
4323 bpf_error("'ip6' modifier applied to ip host");
4326 bpf_error("'icmp6' modifier applied to %s", typestr);
4330 bpf_error("'ah' modifier applied to %s", typestr);
4333 bpf_error("'esp' modifier applied to %s", typestr);
4336 bpf_error("ISO host filtering not implemented");
4339 bpf_error("'esis' modifier applied to %s", typestr);
4342 bpf_error("'isis' modifier applied to %s", typestr);
4345 bpf_error("'clnp' modifier applied to %s", typestr);
4348 bpf_error("'stp' modifier applied to %s", typestr);
4351 bpf_error("IPX host filtering not implemented");
4354 bpf_error("'netbeui' modifier applied to %s", typestr);
4357 bpf_error("'radio' modifier applied to %s", typestr);
4366 static struct block *
4367 gen_host6(addr, mask, proto, dir, type)
4368 struct in6_addr *addr;
4369 struct in6_addr *mask;
4374 const char *typestr;
4384 return gen_host6(addr, mask, Q_IPV6, dir, type);
4387 bpf_error("'ip' modifier applied to ip6 %s", typestr);
4390 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
4393 bpf_error("'arp' modifier applied to ip6 %s", typestr);
4396 bpf_error("'sctp' modifier applied to %s", typestr);
4399 bpf_error("'tcp' modifier applied to %s", typestr);
4402 bpf_error("'udp' modifier applied to %s", typestr);
4405 bpf_error("'icmp' modifier applied to %s", typestr);
4408 bpf_error("'igmp' modifier applied to %s", typestr);
4411 bpf_error("'igrp' modifier applied to %s", typestr);
4414 bpf_error("'pim' modifier applied to %s", typestr);
4417 bpf_error("'vrrp' modifier applied to %s", typestr);
4420 bpf_error("ATALK host filtering not implemented");
4423 bpf_error("AARP host filtering not implemented");
4426 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
4429 bpf_error("SCA host filtering not implemented");
4432 bpf_error("LAT host filtering not implemented");
4435 bpf_error("MOPDL host filtering not implemented");
4438 bpf_error("MOPRC host filtering not implemented");
4441 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4444 bpf_error("'icmp6' modifier applied to %s", typestr);
4447 bpf_error("'ah' modifier applied to %s", typestr);
4450 bpf_error("'esp' modifier applied to %s", typestr);
4453 bpf_error("ISO host filtering not implemented");
4456 bpf_error("'esis' modifier applied to %s", typestr);
4459 bpf_error("'isis' modifier applied to %s", typestr);
4462 bpf_error("'clnp' modifier applied to %s", typestr);
4465 bpf_error("'stp' modifier applied to %s", typestr);
4468 bpf_error("IPX host filtering not implemented");
4471 bpf_error("'netbeui' modifier applied to %s", typestr);
4474 bpf_error("'radio' modifier applied to %s", typestr);
4484 static struct block *
4485 gen_gateway(eaddr, alist, proto, dir)
4486 const u_char *eaddr;
4487 bpf_u_int32 **alist;
4491 struct block *b0, *b1, *tmp;
4494 bpf_error("direction applied to 'gateway'");
4503 b0 = gen_ehostop(eaddr, Q_OR);
4506 b0 = gen_fhostop(eaddr, Q_OR);
4509 b0 = gen_thostop(eaddr, Q_OR);
4511 case DLT_IEEE802_11:
4512 case DLT_PRISM_HEADER:
4513 case DLT_IEEE802_11_RADIO_AVS:
4514 case DLT_IEEE802_11_RADIO:
4516 b0 = gen_wlanhostop(eaddr, Q_OR);
4521 * Check that the packet doesn't begin with an
4522 * LE Control marker. (We've already generated
4525 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4530 * Now check the MAC address.
4532 b0 = gen_ehostop(eaddr, Q_OR);
4536 case DLT_IP_OVER_FC:
4537 b0 = gen_ipfchostop(eaddr, Q_OR);
4541 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4543 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
4545 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
4554 bpf_error("illegal modifier of 'gateway'");
4560 gen_proto_abbrev(proto)
4569 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
4571 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
4577 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
4579 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
4585 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
4587 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
4593 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
4596 #ifndef IPPROTO_IGMP
4597 #define IPPROTO_IGMP 2
4601 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
4604 #ifndef IPPROTO_IGRP
4605 #define IPPROTO_IGRP 9
4608 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
4612 #define IPPROTO_PIM 103
4616 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
4618 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
4623 #ifndef IPPROTO_VRRP
4624 #define IPPROTO_VRRP 112
4628 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
4632 b1 = gen_linktype(ETHERTYPE_IP);
4636 b1 = gen_linktype(ETHERTYPE_ARP);
4640 b1 = gen_linktype(ETHERTYPE_REVARP);
4644 bpf_error("link layer applied in wrong context");
4647 b1 = gen_linktype(ETHERTYPE_ATALK);
4651 b1 = gen_linktype(ETHERTYPE_AARP);
4655 b1 = gen_linktype(ETHERTYPE_DN);
4659 b1 = gen_linktype(ETHERTYPE_SCA);
4663 b1 = gen_linktype(ETHERTYPE_LAT);
4667 b1 = gen_linktype(ETHERTYPE_MOPDL);
4671 b1 = gen_linktype(ETHERTYPE_MOPRC);
4676 b1 = gen_linktype(ETHERTYPE_IPV6);
4679 #ifndef IPPROTO_ICMPV6
4680 #define IPPROTO_ICMPV6 58
4683 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
4688 #define IPPROTO_AH 51
4691 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
4693 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
4699 #define IPPROTO_ESP 50
4702 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
4704 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
4710 b1 = gen_linktype(LLCSAP_ISONS);
4714 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
4718 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4721 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
4722 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4723 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4725 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4727 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4729 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4733 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
4734 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4735 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4737 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4739 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4741 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4745 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
4746 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4747 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4749 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
4754 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4755 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4760 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4761 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4763 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4765 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4770 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4771 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4776 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4777 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4782 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
4786 b1 = gen_linktype(LLCSAP_8021D);
4790 b1 = gen_linktype(LLCSAP_IPX);
4794 b1 = gen_linktype(LLCSAP_NETBEUI);
4798 bpf_error("'radio' is not a valid protocol type");
4806 static struct block *
4813 s = gen_load_a(OR_NET, 6, BPF_H);
4814 b = new_block(JMP(BPF_JSET));
4823 * Generate a comparison to a port value in the transport-layer header
4824 * at the specified offset from the beginning of that header.
4826 * XXX - this handles a variable-length prefix preceding the link-layer
4827 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4828 * variable-length link-layer headers (such as Token Ring or 802.11
4831 static struct block *
4832 gen_portatom(off, v)
4836 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
4840 static struct block *
4841 gen_portatom6(off, v)
4845 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
4850 gen_portop(port, proto, dir)
4851 int port, proto, dir;
4853 struct block *b0, *b1, *tmp;
4855 /* ip proto 'proto' */
4856 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4862 b1 = gen_portatom(0, (bpf_int32)port);
4866 b1 = gen_portatom(2, (bpf_int32)port);
4871 tmp = gen_portatom(0, (bpf_int32)port);
4872 b1 = gen_portatom(2, (bpf_int32)port);
4877 tmp = gen_portatom(0, (bpf_int32)port);
4878 b1 = gen_portatom(2, (bpf_int32)port);
4890 static struct block *
4891 gen_port(port, ip_proto, dir)
4896 struct block *b0, *b1, *tmp;
4901 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4902 * not LLC encapsulation with LLCSAP_IP.
4904 * For IEEE 802 networks - which includes 802.5 token ring
4905 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4906 * says that SNAP encapsulation is used, not LLC encapsulation
4909 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4910 * RFC 2225 say that SNAP encapsulation is used, not LLC
4911 * encapsulation with LLCSAP_IP.
4913 * So we always check for ETHERTYPE_IP.
4915 b0 = gen_linktype(ETHERTYPE_IP);
4921 b1 = gen_portop(port, ip_proto, dir);
4925 tmp = gen_portop(port, IPPROTO_TCP, dir);
4926 b1 = gen_portop(port, IPPROTO_UDP, dir);
4928 tmp = gen_portop(port, IPPROTO_SCTP, dir);
4941 gen_portop6(port, proto, dir)
4942 int port, proto, dir;
4944 struct block *b0, *b1, *tmp;
4946 /* ip6 proto 'proto' */
4947 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4951 b1 = gen_portatom6(0, (bpf_int32)port);
4955 b1 = gen_portatom6(2, (bpf_int32)port);
4960 tmp = gen_portatom6(0, (bpf_int32)port);
4961 b1 = gen_portatom6(2, (bpf_int32)port);
4966 tmp = gen_portatom6(0, (bpf_int32)port);
4967 b1 = gen_portatom6(2, (bpf_int32)port);
4979 static struct block *
4980 gen_port6(port, ip_proto, dir)
4985 struct block *b0, *b1, *tmp;
4987 /* link proto ip6 */
4988 b0 = gen_linktype(ETHERTYPE_IPV6);
4994 b1 = gen_portop6(port, ip_proto, dir);
4998 tmp = gen_portop6(port, IPPROTO_TCP, dir);
4999 b1 = gen_portop6(port, IPPROTO_UDP, dir);
5001 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
5013 /* gen_portrange code */
5014 static struct block *
5015 gen_portrangeatom(off, v1, v2)
5019 struct block *b1, *b2;
5023 * Reverse the order of the ports, so v1 is the lower one.
5032 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
5033 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
5041 gen_portrangeop(port1, port2, proto, dir)
5046 struct block *b0, *b1, *tmp;
5048 /* ip proto 'proto' */
5049 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5055 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5059 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5064 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5065 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5070 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5071 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5083 static struct block *
5084 gen_portrange(port1, port2, ip_proto, dir)
5089 struct block *b0, *b1, *tmp;
5092 b0 = gen_linktype(ETHERTYPE_IP);
5098 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
5102 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
5103 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
5105 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
5117 static struct block *
5118 gen_portrangeatom6(off, v1, v2)
5122 struct block *b1, *b2;
5126 * Reverse the order of the ports, so v1 is the lower one.
5135 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
5136 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
5144 gen_portrangeop6(port1, port2, proto, dir)
5149 struct block *b0, *b1, *tmp;
5151 /* ip6 proto 'proto' */
5152 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5156 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5160 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5165 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5166 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5171 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5172 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5184 static struct block *
5185 gen_portrange6(port1, port2, ip_proto, dir)
5190 struct block *b0, *b1, *tmp;
5192 /* link proto ip6 */
5193 b0 = gen_linktype(ETHERTYPE_IPV6);
5199 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
5203 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
5204 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
5206 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
5219 lookup_proto(name, proto)
5220 register const char *name;
5230 v = pcap_nametoproto(name);
5231 if (v == PROTO_UNDEF)
5232 bpf_error("unknown ip proto '%s'", name);
5236 /* XXX should look up h/w protocol type based on linktype */
5237 v = pcap_nametoeproto(name);
5238 if (v == PROTO_UNDEF) {
5239 v = pcap_nametollc(name);
5240 if (v == PROTO_UNDEF)
5241 bpf_error("unknown ether proto '%s'", name);
5246 if (strcmp(name, "esis") == 0)
5248 else if (strcmp(name, "isis") == 0)
5250 else if (strcmp(name, "clnp") == 0)
5253 bpf_error("unknown osi proto '%s'", name);
5273 static struct block *
5274 gen_protochain(v, proto, dir)
5279 #ifdef NO_PROTOCHAIN
5280 return gen_proto(v, proto, dir);
5282 struct block *b0, *b;
5283 struct slist *s[100];
5284 int fix2, fix3, fix4, fix5;
5285 int ahcheck, again, end;
5287 int reg2 = alloc_reg();
5289 memset(s, 0, sizeof(s));
5290 fix2 = fix3 = fix4 = fix5 = 0;
5297 b0 = gen_protochain(v, Q_IP, dir);
5298 b = gen_protochain(v, Q_IPV6, dir);
5302 bpf_error("bad protocol applied for 'protochain'");
5307 * We don't handle variable-length prefixes before the link-layer
5308 * header, or variable-length link-layer headers, here yet.
5309 * We might want to add BPF instructions to do the protochain
5310 * work, to simplify that and, on platforms that have a BPF
5311 * interpreter with the new instructions, let the filtering
5312 * be done in the kernel. (We already require a modified BPF
5313 * engine to do the protochain stuff, to support backward
5314 * branches, and backward branch support is unlikely to appear
5315 * in kernel BPF engines.)
5319 case DLT_IEEE802_11:
5320 case DLT_PRISM_HEADER:
5321 case DLT_IEEE802_11_RADIO_AVS:
5322 case DLT_IEEE802_11_RADIO:
5324 bpf_error("'protochain' not supported with 802.11");
5327 no_optimize = 1; /*this code is not compatible with optimzer yet */
5330 * s[0] is a dummy entry to protect other BPF insn from damage
5331 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5332 * hard to find interdependency made by jump table fixup.
5335 s[i] = new_stmt(0); /*dummy*/
5340 b0 = gen_linktype(ETHERTYPE_IP);
5343 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5344 s[i]->s.k = off_macpl + off_nl + 9;
5346 /* X = ip->ip_hl << 2 */
5347 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
5348 s[i]->s.k = off_macpl + off_nl;
5353 b0 = gen_linktype(ETHERTYPE_IPV6);
5355 /* A = ip6->ip_nxt */
5356 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5357 s[i]->s.k = off_macpl + off_nl + 6;
5359 /* X = sizeof(struct ip6_hdr) */
5360 s[i] = new_stmt(BPF_LDX|BPF_IMM);
5366 bpf_error("unsupported proto to gen_protochain");
5370 /* again: if (A == v) goto end; else fall through; */
5372 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5374 s[i]->s.jt = NULL; /*later*/
5375 s[i]->s.jf = NULL; /*update in next stmt*/
5379 #ifndef IPPROTO_NONE
5380 #define IPPROTO_NONE 59
5382 /* if (A == IPPROTO_NONE) goto end */
5383 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5384 s[i]->s.jt = NULL; /*later*/
5385 s[i]->s.jf = NULL; /*update in next stmt*/
5386 s[i]->s.k = IPPROTO_NONE;
5387 s[fix5]->s.jf = s[i];
5392 if (proto == Q_IPV6) {
5393 int v6start, v6end, v6advance, j;
5396 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5397 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5398 s[i]->s.jt = NULL; /*later*/
5399 s[i]->s.jf = NULL; /*update in next stmt*/
5400 s[i]->s.k = IPPROTO_HOPOPTS;
5401 s[fix2]->s.jf = s[i];
5403 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5404 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5405 s[i]->s.jt = NULL; /*later*/
5406 s[i]->s.jf = NULL; /*update in next stmt*/
5407 s[i]->s.k = IPPROTO_DSTOPTS;
5409 /* if (A == IPPROTO_ROUTING) goto v6advance */
5410 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5411 s[i]->s.jt = NULL; /*later*/
5412 s[i]->s.jf = NULL; /*update in next stmt*/
5413 s[i]->s.k = IPPROTO_ROUTING;
5415 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5416 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5417 s[i]->s.jt = NULL; /*later*/
5418 s[i]->s.jf = NULL; /*later*/
5419 s[i]->s.k = IPPROTO_FRAGMENT;
5430 * X = X + (P[X + 1] + 1) * 8;
5433 s[i] = new_stmt(BPF_MISC|BPF_TXA);
5435 /* A = P[X + packet head] */
5436 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5437 s[i]->s.k = off_macpl + off_nl;
5440 s[i] = new_stmt(BPF_ST);
5444 s[i] = new_stmt(BPF_MISC|BPF_TXA);
5447 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5451 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5453 /* A = P[X + packet head]; */
5454 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5455 s[i]->s.k = off_macpl + off_nl;
5458 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5462 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5466 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5469 s[i] = new_stmt(BPF_LD|BPF_MEM);
5473 /* goto again; (must use BPF_JA for backward jump) */
5474 s[i] = new_stmt(BPF_JMP|BPF_JA);
5475 s[i]->s.k = again - i - 1;
5476 s[i - 1]->s.jf = s[i];
5480 for (j = v6start; j <= v6end; j++)
5481 s[j]->s.jt = s[v6advance];
5486 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5488 s[fix2]->s.jf = s[i];
5494 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5495 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5496 s[i]->s.jt = NULL; /*later*/
5497 s[i]->s.jf = NULL; /*later*/
5498 s[i]->s.k = IPPROTO_AH;
5500 s[fix3]->s.jf = s[ahcheck];
5507 * X = X + (P[X + 1] + 2) * 4;
5510 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5512 /* A = P[X + packet head]; */
5513 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5514 s[i]->s.k = off_macpl + off_nl;
5517 s[i] = new_stmt(BPF_ST);
5521 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5524 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5528 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5530 /* A = P[X + packet head] */
5531 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5532 s[i]->s.k = off_macpl + off_nl;
5535 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5539 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5543 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5546 s[i] = new_stmt(BPF_LD|BPF_MEM);
5550 /* goto again; (must use BPF_JA for backward jump) */
5551 s[i] = new_stmt(BPF_JMP|BPF_JA);
5552 s[i]->s.k = again - i - 1;
5557 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5559 s[fix2]->s.jt = s[end];
5560 s[fix4]->s.jf = s[end];
5561 s[fix5]->s.jt = s[end];
5568 for (i = 0; i < max - 1; i++)
5569 s[i]->next = s[i + 1];
5570 s[max - 1]->next = NULL;
5575 b = new_block(JMP(BPF_JEQ));
5576 b->stmts = s[1]; /*remember, s[0] is dummy*/
5586 static struct block *
5587 gen_check_802_11_data_frame()
5590 struct block *b0, *b1;
5593 * A data frame has the 0x08 bit (b3) in the frame control field set
5594 * and the 0x04 bit (b2) clear.
5596 s = gen_load_a(OR_LINK, 0, BPF_B);
5597 b0 = new_block(JMP(BPF_JSET));
5601 s = gen_load_a(OR_LINK, 0, BPF_B);
5602 b1 = new_block(JMP(BPF_JSET));
5613 * Generate code that checks whether the packet is a packet for protocol
5614 * <proto> and whether the type field in that protocol's header has
5615 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5616 * IP packet and checks the protocol number in the IP header against <v>.
5618 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5619 * against Q_IP and Q_IPV6.
5621 static struct block *
5622 gen_proto(v, proto, dir)
5627 struct block *b0, *b1;
5629 if (dir != Q_DEFAULT)
5630 bpf_error("direction applied to 'proto'");
5635 b0 = gen_proto(v, Q_IP, dir);
5636 b1 = gen_proto(v, Q_IPV6, dir);
5644 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5645 * not LLC encapsulation with LLCSAP_IP.
5647 * For IEEE 802 networks - which includes 802.5 token ring
5648 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5649 * says that SNAP encapsulation is used, not LLC encapsulation
5652 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5653 * RFC 2225 say that SNAP encapsulation is used, not LLC
5654 * encapsulation with LLCSAP_IP.
5656 * So we always check for ETHERTYPE_IP.
5658 b0 = gen_linktype(ETHERTYPE_IP);
5660 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
5662 b1 = gen_protochain(v, Q_IP);
5672 * Frame Relay packets typically have an OSI
5673 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5674 * generates code to check for all the OSI
5675 * NLPIDs, so calling it and then adding a check
5676 * for the particular NLPID for which we're
5677 * looking is bogus, as we can just check for
5680 * What we check for is the NLPID and a frame
5681 * control field value of UI, i.e. 0x03 followed
5684 * XXX - assumes a 2-byte Frame Relay header with
5685 * DLCI and flags. What if the address is longer?
5687 * XXX - what about SNAP-encapsulated frames?
5689 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
5695 * Cisco uses an Ethertype lookalike - for OSI,
5698 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
5699 /* OSI in C-HDLC is stuffed with a fudge byte */
5700 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
5705 b0 = gen_linktype(LLCSAP_ISONS);
5706 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
5712 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5714 * 4 is the offset of the PDU type relative to the IS-IS
5717 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
5722 bpf_error("arp does not encapsulate another protocol");
5726 bpf_error("rarp does not encapsulate another protocol");
5730 bpf_error("atalk encapsulation is not specifiable");
5734 bpf_error("decnet encapsulation is not specifiable");
5738 bpf_error("sca does not encapsulate another protocol");
5742 bpf_error("lat does not encapsulate another protocol");
5746 bpf_error("moprc does not encapsulate another protocol");
5750 bpf_error("mopdl does not encapsulate another protocol");
5754 return gen_linktype(v);
5757 bpf_error("'udp proto' is bogus");
5761 bpf_error("'tcp proto' is bogus");
5765 bpf_error("'sctp proto' is bogus");
5769 bpf_error("'icmp proto' is bogus");
5773 bpf_error("'igmp proto' is bogus");
5777 bpf_error("'igrp proto' is bogus");
5781 bpf_error("'pim proto' is bogus");
5785 bpf_error("'vrrp proto' is bogus");
5790 b0 = gen_linktype(ETHERTYPE_IPV6);
5792 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
5794 b1 = gen_protochain(v, Q_IPV6);
5800 bpf_error("'icmp6 proto' is bogus");
5804 bpf_error("'ah proto' is bogus");
5807 bpf_error("'ah proto' is bogus");
5810 bpf_error("'stp proto' is bogus");
5813 bpf_error("'ipx proto' is bogus");
5816 bpf_error("'netbeui proto' is bogus");
5819 bpf_error("'radio proto' is bogus");
5830 register const char *name;
5833 int proto = q.proto;
5837 bpf_u_int32 mask, addr;
5839 bpf_u_int32 **alist;
5842 struct sockaddr_in *sin4;
5843 struct sockaddr_in6 *sin6;
5844 struct addrinfo *res, *res0;
5845 struct in6_addr mask128;
5847 struct block *b, *tmp;
5848 int port, real_proto;
5854 addr = pcap_nametonetaddr(name);
5856 bpf_error("unknown network '%s'", name);
5857 /* Left justify network addr and calculate its network mask */
5859 while (addr && (addr & 0xff000000) == 0) {
5863 return gen_host(addr, mask, proto, dir, q.addr);
5867 if (proto == Q_LINK) {
5871 eaddr = pcap_ether_hostton(name);
5874 "unknown ether host '%s'", name);
5875 b = gen_ehostop(eaddr, dir);
5880 eaddr = pcap_ether_hostton(name);
5883 "unknown FDDI host '%s'", name);
5884 b = gen_fhostop(eaddr, dir);
5889 eaddr = pcap_ether_hostton(name);
5892 "unknown token ring host '%s'", name);
5893 b = gen_thostop(eaddr, dir);
5897 case DLT_IEEE802_11:
5898 case DLT_PRISM_HEADER:
5899 case DLT_IEEE802_11_RADIO_AVS:
5900 case DLT_IEEE802_11_RADIO:
5902 eaddr = pcap_ether_hostton(name);
5905 "unknown 802.11 host '%s'", name);
5906 b = gen_wlanhostop(eaddr, dir);
5910 case DLT_IP_OVER_FC:
5911 eaddr = pcap_ether_hostton(name);
5914 "unknown Fibre Channel host '%s'", name);
5915 b = gen_ipfchostop(eaddr, dir);
5924 * Check that the packet doesn't begin
5925 * with an LE Control marker. (We've
5926 * already generated a test for LANE.)
5928 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
5932 eaddr = pcap_ether_hostton(name);
5935 "unknown ether host '%s'", name);
5936 b = gen_ehostop(eaddr, dir);
5942 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5943 } else if (proto == Q_DECNET) {
5944 unsigned short dn_addr = __pcap_nametodnaddr(name);
5946 * I don't think DECNET hosts can be multihomed, so
5947 * there is no need to build up a list of addresses
5949 return (gen_host(dn_addr, 0, proto, dir, q.addr));
5952 alist = pcap_nametoaddr(name);
5953 if (alist == NULL || *alist == NULL)
5954 bpf_error("unknown host '%s'", name);
5956 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
5958 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
5960 tmp = gen_host(**alist++, 0xffffffff,
5961 tproto, dir, q.addr);
5967 memset(&mask128, 0xff, sizeof(mask128));
5968 res0 = res = pcap_nametoaddrinfo(name);
5970 bpf_error("unknown host '%s'", name);
5972 tproto = tproto6 = proto;
5973 if (off_linktype == -1 && tproto == Q_DEFAULT) {
5977 for (res = res0; res; res = res->ai_next) {
5978 switch (res->ai_family) {
5980 if (tproto == Q_IPV6)
5983 sin4 = (struct sockaddr_in *)
5985 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
5986 0xffffffff, tproto, dir, q.addr);
5989 if (tproto6 == Q_IP)
5992 sin6 = (struct sockaddr_in6 *)
5994 tmp = gen_host6(&sin6->sin6_addr,
5995 &mask128, tproto6, dir, q.addr);
6006 bpf_error("unknown host '%s'%s", name,
6007 (proto == Q_DEFAULT)
6009 : " for specified address family");
6016 if (proto != Q_DEFAULT &&
6017 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6018 bpf_error("illegal qualifier of 'port'");
6019 if (pcap_nametoport(name, &port, &real_proto) == 0)
6020 bpf_error("unknown port '%s'", name);
6021 if (proto == Q_UDP) {
6022 if (real_proto == IPPROTO_TCP)
6023 bpf_error("port '%s' is tcp", name);
6024 else if (real_proto == IPPROTO_SCTP)
6025 bpf_error("port '%s' is sctp", name);
6027 /* override PROTO_UNDEF */
6028 real_proto = IPPROTO_UDP;
6030 if (proto == Q_TCP) {
6031 if (real_proto == IPPROTO_UDP)
6032 bpf_error("port '%s' is udp", name);
6034 else if (real_proto == IPPROTO_SCTP)
6035 bpf_error("port '%s' is sctp", name);
6037 /* override PROTO_UNDEF */
6038 real_proto = IPPROTO_TCP;
6040 if (proto == Q_SCTP) {
6041 if (real_proto == IPPROTO_UDP)
6042 bpf_error("port '%s' is udp", name);
6044 else if (real_proto == IPPROTO_TCP)
6045 bpf_error("port '%s' is tcp", name);
6047 /* override PROTO_UNDEF */
6048 real_proto = IPPROTO_SCTP;
6051 return gen_port(port, real_proto, dir);
6053 b = gen_port(port, real_proto, dir);
6054 gen_or(gen_port6(port, real_proto, dir), b);
6059 if (proto != Q_DEFAULT &&
6060 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6061 bpf_error("illegal qualifier of 'portrange'");
6062 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6063 bpf_error("unknown port in range '%s'", name);
6064 if (proto == Q_UDP) {
6065 if (real_proto == IPPROTO_TCP)
6066 bpf_error("port in range '%s' is tcp", name);
6067 else if (real_proto == IPPROTO_SCTP)
6068 bpf_error("port in range '%s' is sctp", name);
6070 /* override PROTO_UNDEF */
6071 real_proto = IPPROTO_UDP;
6073 if (proto == Q_TCP) {
6074 if (real_proto == IPPROTO_UDP)
6075 bpf_error("port in range '%s' is udp", name);
6076 else if (real_proto == IPPROTO_SCTP)
6077 bpf_error("port in range '%s' is sctp", name);
6079 /* override PROTO_UNDEF */
6080 real_proto = IPPROTO_TCP;
6082 if (proto == Q_SCTP) {
6083 if (real_proto == IPPROTO_UDP)
6084 bpf_error("port in range '%s' is udp", name);
6085 else if (real_proto == IPPROTO_TCP)
6086 bpf_error("port in range '%s' is tcp", name);
6088 /* override PROTO_UNDEF */
6089 real_proto = IPPROTO_SCTP;
6092 return gen_portrange(port1, port2, real_proto, dir);
6094 b = gen_portrange(port1, port2, real_proto, dir);
6095 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
6101 eaddr = pcap_ether_hostton(name);
6103 bpf_error("unknown ether host: %s", name);
6105 alist = pcap_nametoaddr(name);
6106 if (alist == NULL || *alist == NULL)
6107 bpf_error("unknown host '%s'", name);
6108 b = gen_gateway(eaddr, alist, proto, dir);
6112 bpf_error("'gateway' not supported in this configuration");
6116 real_proto = lookup_proto(name, proto);
6117 if (real_proto >= 0)
6118 return gen_proto(real_proto, proto, dir);
6120 bpf_error("unknown protocol: %s", name);
6123 real_proto = lookup_proto(name, proto);
6124 if (real_proto >= 0)
6125 return gen_protochain(real_proto, proto, dir);
6127 bpf_error("unknown protocol: %s", name);
6138 gen_mcode(s1, s2, masklen, q)
6139 register const char *s1, *s2;
6140 register int masklen;
6143 register int nlen, mlen;
6146 nlen = __pcap_atoin(s1, &n);
6147 /* Promote short ipaddr */
6151 mlen = __pcap_atoin(s2, &m);
6152 /* Promote short ipaddr */
6155 bpf_error("non-network bits set in \"%s mask %s\"",
6158 /* Convert mask len to mask */
6160 bpf_error("mask length must be <= 32");
6163 * X << 32 is not guaranteed by C to be 0; it's
6168 m = 0xffffffff << (32 - masklen);
6170 bpf_error("non-network bits set in \"%s/%d\"",
6177 return gen_host(n, m, q.proto, q.dir, q.addr);
6180 bpf_error("Mask syntax for networks only");
6189 register const char *s;
6194 int proto = q.proto;
6200 else if (q.proto == Q_DECNET)
6201 vlen = __pcap_atodn(s, &v);
6203 vlen = __pcap_atoin(s, &v);
6210 if (proto == Q_DECNET)
6211 return gen_host(v, 0, proto, dir, q.addr);
6212 else if (proto == Q_LINK) {
6213 bpf_error("illegal link layer address");
6216 if (s == NULL && q.addr == Q_NET) {
6217 /* Promote short net number */
6218 while (v && (v & 0xff000000) == 0) {
6223 /* Promote short ipaddr */
6227 return gen_host(v, mask, proto, dir, q.addr);
6232 proto = IPPROTO_UDP;
6233 else if (proto == Q_TCP)
6234 proto = IPPROTO_TCP;
6235 else if (proto == Q_SCTP)
6236 proto = IPPROTO_SCTP;
6237 else if (proto == Q_DEFAULT)
6238 proto = PROTO_UNDEF;
6240 bpf_error("illegal qualifier of 'port'");
6243 return gen_port((int)v, proto, dir);
6247 b = gen_port((int)v, proto, dir);
6248 gen_or(gen_port6((int)v, proto, dir), b);
6255 proto = IPPROTO_UDP;
6256 else if (proto == Q_TCP)
6257 proto = IPPROTO_TCP;
6258 else if (proto == Q_SCTP)
6259 proto = IPPROTO_SCTP;
6260 else if (proto == Q_DEFAULT)
6261 proto = PROTO_UNDEF;
6263 bpf_error("illegal qualifier of 'portrange'");
6266 return gen_portrange((int)v, (int)v, proto, dir);
6270 b = gen_portrange((int)v, (int)v, proto, dir);
6271 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
6277 bpf_error("'gateway' requires a name");
6281 return gen_proto((int)v, proto, dir);
6284 return gen_protochain((int)v, proto, dir);
6299 gen_mcode6(s1, s2, masklen, q)
6300 register const char *s1, *s2;
6301 register int masklen;
6304 struct addrinfo *res;
6305 struct in6_addr *addr;
6306 struct in6_addr mask;
6311 bpf_error("no mask %s supported", s2);
6313 res = pcap_nametoaddrinfo(s1);
6315 bpf_error("invalid ip6 address %s", s1);
6317 bpf_error("%s resolved to multiple address", s1);
6318 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
6320 if (sizeof(mask) * 8 < masklen)
6321 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6322 memset(&mask, 0, sizeof(mask));
6323 memset(&mask, 0xff, masklen / 8);
6325 mask.s6_addr[masklen / 8] =
6326 (0xff << (8 - masklen % 8)) & 0xff;
6329 a = (u_int32_t *)addr;
6330 m = (u_int32_t *)&mask;
6331 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
6332 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
6333 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
6341 bpf_error("Mask syntax for networks only");
6345 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
6350 bpf_error("invalid qualifier against IPv6 address");
6359 register const u_char *eaddr;
6362 struct block *b, *tmp;
6364 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6367 return gen_ehostop(eaddr, (int)q.dir);
6369 return gen_fhostop(eaddr, (int)q.dir);
6371 return gen_thostop(eaddr, (int)q.dir);
6372 case DLT_IEEE802_11:
6373 case DLT_PRISM_HEADER:
6374 case DLT_IEEE802_11_RADIO_AVS:
6375 case DLT_IEEE802_11_RADIO:
6377 return gen_wlanhostop(eaddr, (int)q.dir);
6381 * Check that the packet doesn't begin with an
6382 * LE Control marker. (We've already generated
6385 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6390 * Now check the MAC address.
6392 b = gen_ehostop(eaddr, (int)q.dir);
6397 case DLT_IP_OVER_FC:
6398 return gen_ipfchostop(eaddr, (int)q.dir);
6400 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6404 bpf_error("ethernet address used in non-ether expression");
6411 struct slist *s0, *s1;
6414 * This is definitely not the best way to do this, but the
6415 * lists will rarely get long.
6422 static struct slist *
6428 s = new_stmt(BPF_LDX|BPF_MEM);
6433 static struct slist *
6439 s = new_stmt(BPF_LD|BPF_MEM);
6445 * Modify "index" to use the value stored into its register as an
6446 * offset relative to the beginning of the header for the protocol
6447 * "proto", and allocate a register and put an item "size" bytes long
6448 * (1, 2, or 4) at that offset into that register, making it the register
6452 gen_load(proto, inst, size)
6457 struct slist *s, *tmp;
6459 int regno = alloc_reg();
6461 free_reg(inst->regno);
6465 bpf_error("data size must be 1, 2, or 4");
6481 bpf_error("unsupported index operation");
6485 * The offset is relative to the beginning of the packet
6486 * data, if we have a radio header. (If we don't, this
6489 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
6490 linktype != DLT_IEEE802_11_RADIO &&
6491 linktype != DLT_PRISM_HEADER)
6492 bpf_error("radio information not present in capture");
6495 * Load into the X register the offset computed into the
6496 * register specifed by "index".
6498 s = xfer_to_x(inst);
6501 * Load the item at that offset.
6503 tmp = new_stmt(BPF_LD|BPF_IND|size);
6505 sappend(inst->s, s);
6510 * The offset is relative to the beginning of
6511 * the link-layer header.
6513 * XXX - what about ATM LANE? Should the index be
6514 * relative to the beginning of the AAL5 frame, so
6515 * that 0 refers to the beginning of the LE Control
6516 * field, or relative to the beginning of the LAN
6517 * frame, so that 0 refers, for Ethernet LANE, to
6518 * the beginning of the destination address?
6520 s = gen_llprefixlen();
6523 * If "s" is non-null, it has code to arrange that the
6524 * X register contains the length of the prefix preceding
6525 * the link-layer header. Add to it the offset computed
6526 * into the register specified by "index", and move that
6527 * into the X register. Otherwise, just load into the X
6528 * register the offset computed into the register specifed
6532 sappend(s, xfer_to_a(inst));
6533 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6534 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6536 s = xfer_to_x(inst);
6539 * Load the item at the sum of the offset we've put in the
6540 * X register and the offset of the start of the link
6541 * layer header (which is 0 if the radio header is
6542 * variable-length; that header length is what we put
6543 * into the X register and then added to the index).
6545 tmp = new_stmt(BPF_LD|BPF_IND|size);
6548 sappend(inst->s, s);
6564 * The offset is relative to the beginning of
6565 * the network-layer header.
6566 * XXX - are there any cases where we want
6569 s = gen_off_macpl();
6572 * If "s" is non-null, it has code to arrange that the
6573 * X register contains the offset of the MAC-layer
6574 * payload. Add to it the offset computed into the
6575 * register specified by "index", and move that into
6576 * the X register. Otherwise, just load into the X
6577 * register the offset computed into the register specifed
6581 sappend(s, xfer_to_a(inst));
6582 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6583 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6585 s = xfer_to_x(inst);
6588 * Load the item at the sum of the offset we've put in the
6589 * X register, the offset of the start of the network
6590 * layer header from the beginning of the MAC-layer
6591 * payload, and the purported offset of the start of the
6592 * MAC-layer payload (which might be 0 if there's a
6593 * variable-length prefix before the link-layer header
6594 * or the link-layer header itself is variable-length;
6595 * the variable-length offset of the start of the
6596 * MAC-layer payload is what we put into the X register
6597 * and then added to the index).
6599 tmp = new_stmt(BPF_LD|BPF_IND|size);
6600 tmp->s.k = off_macpl + off_nl;
6602 sappend(inst->s, s);
6605 * Do the computation only if the packet contains
6606 * the protocol in question.
6608 b = gen_proto_abbrev(proto);
6610 gen_and(inst->b, b);
6623 * The offset is relative to the beginning of
6624 * the transport-layer header.
6626 * Load the X register with the length of the IPv4 header
6627 * (plus the offset of the link-layer header, if it's
6628 * a variable-length header), in bytes.
6630 * XXX - are there any cases where we want
6632 * XXX - we should, if we're built with
6633 * IPv6 support, generate code to load either
6634 * IPv4, IPv6, or both, as appropriate.
6636 s = gen_loadx_iphdrlen();
6639 * The X register now contains the sum of the length
6640 * of any variable-length header preceding the link-layer
6641 * header, any variable-length link-layer header, and the
6642 * length of the network-layer header.
6644 * Load into the A register the offset relative to
6645 * the beginning of the transport layer header,
6646 * add the X register to that, move that to the
6647 * X register, and load with an offset from the
6648 * X register equal to the offset of the network
6649 * layer header relative to the beginning of
6650 * the MAC-layer payload plus the fixed-length
6651 * portion of the offset of the MAC-layer payload
6652 * from the beginning of the raw packet data.
6654 sappend(s, xfer_to_a(inst));
6655 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6656 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6657 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
6658 tmp->s.k = off_macpl + off_nl;
6659 sappend(inst->s, s);
6662 * Do the computation only if the packet contains
6663 * the protocol in question - which is true only
6664 * if this is an IP datagram and is the first or
6665 * only fragment of that datagram.
6667 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
6669 gen_and(inst->b, b);
6671 gen_and(gen_proto_abbrev(Q_IP), b);
6677 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6681 inst->regno = regno;
6682 s = new_stmt(BPF_ST);
6684 sappend(inst->s, s);
6690 gen_relation(code, a0, a1, reversed)
6692 struct arth *a0, *a1;
6695 struct slist *s0, *s1, *s2;
6696 struct block *b, *tmp;
6700 if (code == BPF_JEQ) {
6701 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
6702 b = new_block(JMP(code));
6706 b = new_block(BPF_JMP|code|BPF_X);
6712 sappend(a0->s, a1->s);
6716 free_reg(a0->regno);
6717 free_reg(a1->regno);
6719 /* 'and' together protocol checks */
6722 gen_and(a0->b, tmp = a1->b);
6738 int regno = alloc_reg();
6739 struct arth *a = (struct arth *)newchunk(sizeof(*a));
6742 s = new_stmt(BPF_LD|BPF_LEN);
6743 s->next = new_stmt(BPF_ST);
6744 s->next->s.k = regno;
6759 a = (struct arth *)newchunk(sizeof(*a));
6763 s = new_stmt(BPF_LD|BPF_IMM);
6765 s->next = new_stmt(BPF_ST);
6781 s = new_stmt(BPF_ALU|BPF_NEG);
6784 s = new_stmt(BPF_ST);
6792 gen_arth(code, a0, a1)
6794 struct arth *a0, *a1;
6796 struct slist *s0, *s1, *s2;
6800 s2 = new_stmt(BPF_ALU|BPF_X|code);
6805 sappend(a0->s, a1->s);
6807 free_reg(a0->regno);
6808 free_reg(a1->regno);
6810 s0 = new_stmt(BPF_ST);
6811 a0->regno = s0->s.k = alloc_reg();
6818 * Here we handle simple allocation of the scratch registers.
6819 * If too many registers are alloc'd, the allocator punts.
6821 static int regused[BPF_MEMWORDS];
6825 * Initialize the table of used registers and the current register.
6831 memset(regused, 0, sizeof regused);
6835 * Return the next free register.
6840 int n = BPF_MEMWORDS;
6843 if (regused[curreg])
6844 curreg = (curreg + 1) % BPF_MEMWORDS;
6846 regused[curreg] = 1;
6850 bpf_error("too many registers needed to evaluate expression");
6856 * Return a register to the table so it can
6866 static struct block *
6873 s = new_stmt(BPF_LD|BPF_LEN);
6874 b = new_block(JMP(jmp));
6885 return gen_len(BPF_JGE, n);
6889 * Actually, this is less than or equal.
6897 b = gen_len(BPF_JGT, n);
6904 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6905 * the beginning of the link-layer header.
6906 * XXX - that means you can't test values in the radiotap header, but
6907 * as that header is difficult if not impossible to parse generally
6908 * without a loop, that might not be a severe problem. A new keyword
6909 * "radio" could be added for that, although what you'd really want
6910 * would be a way of testing particular radio header values, which
6911 * would generate code appropriate to the radio header in question.
6914 gen_byteop(op, idx, val)
6925 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6928 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6932 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6936 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
6940 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
6944 b = new_block(JMP(BPF_JEQ));
6951 static u_char abroadcast[] = { 0x0 };
6954 gen_broadcast(proto)
6957 bpf_u_int32 hostmask;
6958 struct block *b0, *b1, *b2;
6959 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6967 case DLT_ARCNET_LINUX:
6968 return gen_ahostop(abroadcast, Q_DST);
6970 return gen_ehostop(ebroadcast, Q_DST);
6972 return gen_fhostop(ebroadcast, Q_DST);
6974 return gen_thostop(ebroadcast, Q_DST);
6975 case DLT_IEEE802_11:
6976 case DLT_PRISM_HEADER:
6977 case DLT_IEEE802_11_RADIO_AVS:
6978 case DLT_IEEE802_11_RADIO:
6980 return gen_wlanhostop(ebroadcast, Q_DST);
6981 case DLT_IP_OVER_FC:
6982 return gen_ipfchostop(ebroadcast, Q_DST);
6986 * Check that the packet doesn't begin with an
6987 * LE Control marker. (We've already generated
6990 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
6995 * Now check the MAC address.
6997 b0 = gen_ehostop(ebroadcast, Q_DST);
7003 bpf_error("not a broadcast link");
7008 b0 = gen_linktype(ETHERTYPE_IP);
7009 hostmask = ~netmask;
7010 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
7011 b2 = gen_mcmp(OR_NET, 16, BPF_W,
7012 (bpf_int32)(~0 & hostmask), hostmask);
7017 bpf_error("only link-layer/IP broadcast filters supported");
7023 * Generate code to test the low-order bit of a MAC address (that's
7024 * the bottom bit of the *first* byte).
7026 static struct block *
7027 gen_mac_multicast(offset)
7030 register struct block *b0;
7031 register struct slist *s;
7033 /* link[offset] & 1 != 0 */
7034 s = gen_load_a(OR_LINK, offset, BPF_B);
7035 b0 = new_block(JMP(BPF_JSET));
7042 gen_multicast(proto)
7045 register struct block *b0, *b1, *b2;
7046 register struct slist *s;
7054 case DLT_ARCNET_LINUX:
7055 /* all ARCnet multicasts use the same address */
7056 return gen_ahostop(abroadcast, Q_DST);
7058 /* ether[0] & 1 != 0 */
7059 return gen_mac_multicast(0);
7062 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7064 * XXX - was that referring to bit-order issues?
7066 /* fddi[1] & 1 != 0 */
7067 return gen_mac_multicast(1);
7069 /* tr[2] & 1 != 0 */
7070 return gen_mac_multicast(2);
7071 case DLT_IEEE802_11:
7072 case DLT_PRISM_HEADER:
7073 case DLT_IEEE802_11_RADIO_AVS:
7074 case DLT_IEEE802_11_RADIO:
7079 * For control frames, there is no DA.
7081 * For management frames, DA is at an
7082 * offset of 4 from the beginning of
7085 * For data frames, DA is at an offset
7086 * of 4 from the beginning of the packet
7087 * if To DS is clear and at an offset of
7088 * 16 from the beginning of the packet
7093 * Generate the tests to be done for data frames.
7095 * First, check for To DS set, i.e. "link[1] & 0x01".
7097 s = gen_load_a(OR_LINK, 1, BPF_B);
7098 b1 = new_block(JMP(BPF_JSET));
7099 b1->s.k = 0x01; /* To DS */
7103 * If To DS is set, the DA is at 16.
7105 b0 = gen_mac_multicast(16);
7109 * Now, check for To DS not set, i.e. check
7110 * "!(link[1] & 0x01)".
7112 s = gen_load_a(OR_LINK, 1, BPF_B);
7113 b2 = new_block(JMP(BPF_JSET));
7114 b2->s.k = 0x01; /* To DS */
7119 * If To DS is not set, the DA is at 4.
7121 b1 = gen_mac_multicast(4);
7125 * Now OR together the last two checks. That gives
7126 * the complete set of checks for data frames.
7131 * Now check for a data frame.
7132 * I.e, check "link[0] & 0x08".
7134 s = gen_load_a(OR_LINK, 0, BPF_B);
7135 b1 = new_block(JMP(BPF_JSET));
7140 * AND that with the checks done for data frames.
7145 * If the high-order bit of the type value is 0, this
7146 * is a management frame.
7147 * I.e, check "!(link[0] & 0x08)".
7149 s = gen_load_a(OR_LINK, 0, BPF_B);
7150 b2 = new_block(JMP(BPF_JSET));
7156 * For management frames, the DA is at 4.
7158 b1 = gen_mac_multicast(4);
7162 * OR that with the checks done for data frames.
7163 * That gives the checks done for management and
7169 * If the low-order bit of the type value is 1,
7170 * this is either a control frame or a frame
7171 * with a reserved type, and thus not a
7174 * I.e., check "!(link[0] & 0x04)".
7176 s = gen_load_a(OR_LINK, 0, BPF_B);
7177 b1 = new_block(JMP(BPF_JSET));
7183 * AND that with the checks for data and management
7188 case DLT_IP_OVER_FC:
7189 b0 = gen_mac_multicast(2);
7194 * Check that the packet doesn't begin with an
7195 * LE Control marker. (We've already generated
7198 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7202 /* ether[off_mac] & 1 != 0 */
7203 b0 = gen_mac_multicast(off_mac);
7211 /* Link not known to support multicasts */
7215 b0 = gen_linktype(ETHERTYPE_IP);
7216 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
7222 b0 = gen_linktype(ETHERTYPE_IPV6);
7223 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
7228 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7234 * generate command for inbound/outbound. It's here so we can
7235 * make it link-type specific. 'dir' = 0 implies "inbound",
7236 * = 1 implies "outbound".
7242 register struct block *b0;
7245 * Only some data link types support inbound/outbound qualifiers.
7249 b0 = gen_relation(BPF_JEQ,
7250 gen_load(Q_LINK, gen_loadi(0), 1),
7258 * Match packets sent by this machine.
7260 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
7263 * Match packets sent to this machine.
7264 * (No broadcast or multicast packets, or
7265 * packets sent to some other machine and
7266 * received promiscuously.)
7268 * XXX - packets sent to other machines probably
7269 * shouldn't be matched, but what about broadcast
7270 * or multicast packets we received?
7272 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
7276 #ifdef HAVE_NET_PFVAR_H
7278 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
7279 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
7285 /* match outgoing packets */
7286 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
7288 /* match incoming packets */
7289 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
7293 case DLT_JUNIPER_MFR:
7294 case DLT_JUNIPER_MLFR:
7295 case DLT_JUNIPER_MLPPP:
7296 case DLT_JUNIPER_ATM1:
7297 case DLT_JUNIPER_ATM2:
7298 case DLT_JUNIPER_PPPOE:
7299 case DLT_JUNIPER_PPPOE_ATM:
7300 case DLT_JUNIPER_GGSN:
7301 case DLT_JUNIPER_ES:
7302 case DLT_JUNIPER_MONITOR:
7303 case DLT_JUNIPER_SERVICES:
7304 case DLT_JUNIPER_ETHER:
7305 case DLT_JUNIPER_PPP:
7306 case DLT_JUNIPER_FRELAY:
7307 case DLT_JUNIPER_CHDLC:
7308 case DLT_JUNIPER_VP:
7309 case DLT_JUNIPER_ST:
7310 case DLT_JUNIPER_ISM:
7311 /* juniper flags (including direction) are stored
7312 * the byte after the 3-byte magic number */
7314 /* match outgoing packets */
7315 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
7317 /* match incoming packets */
7318 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
7323 bpf_error("inbound/outbound not supported on linktype %d",
7331 #ifdef HAVE_NET_PFVAR_H
7332 /* PF firewall log matched interface */
7334 gen_pf_ifname(const char *ifname)
7339 if (linktype != DLT_PFLOG) {
7340 bpf_error("ifname supported only on PF linktype");
7343 len = sizeof(((struct pfloghdr *)0)->ifname);
7344 off = offsetof(struct pfloghdr, ifname);
7345 if (strlen(ifname) >= len) {
7346 bpf_error("ifname interface names can only be %d characters",
7350 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
7354 /* PF firewall log ruleset name */
7356 gen_pf_ruleset(char *ruleset)
7360 if (linktype != DLT_PFLOG) {
7361 bpf_error("ruleset supported only on PF linktype");
7365 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
7366 bpf_error("ruleset names can only be %ld characters",
7367 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
7371 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
7372 strlen(ruleset), (const u_char *)ruleset);
7376 /* PF firewall log rule number */
7382 if (linktype != DLT_PFLOG) {
7383 bpf_error("rnr supported only on PF linktype");
7387 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
7392 /* PF firewall log sub-rule number */
7394 gen_pf_srnr(int srnr)
7398 if (linktype != DLT_PFLOG) {
7399 bpf_error("srnr supported only on PF linktype");
7403 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
7408 /* PF firewall log reason code */
7410 gen_pf_reason(int reason)
7414 if (linktype != DLT_PFLOG) {
7415 bpf_error("reason supported only on PF linktype");
7419 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
7424 /* PF firewall log action */
7426 gen_pf_action(int action)
7430 if (linktype != DLT_PFLOG) {
7431 bpf_error("action supported only on PF linktype");
7435 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
7439 #else /* !HAVE_NET_PFVAR_H */
7441 gen_pf_ifname(const char *ifname)
7443 bpf_error("libpcap was compiled without pf support");
7449 gen_pf_ruleset(char *ruleset)
7451 bpf_error("libpcap was compiled on a machine without pf support");
7459 bpf_error("libpcap was compiled on a machine without pf support");
7465 gen_pf_srnr(int srnr)
7467 bpf_error("libpcap was compiled on a machine without pf support");
7473 gen_pf_reason(int reason)
7475 bpf_error("libpcap was compiled on a machine without pf support");
7481 gen_pf_action(int action)
7483 bpf_error("libpcap was compiled on a machine without pf support");
7487 #endif /* HAVE_NET_PFVAR_H */
7489 /* IEEE 802.11 wireless header */
7491 gen_p80211_type(int type, int mask)
7497 case DLT_IEEE802_11:
7498 case DLT_PRISM_HEADER:
7499 case DLT_IEEE802_11_RADIO_AVS:
7500 case DLT_IEEE802_11_RADIO:
7501 b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
7506 bpf_error("802.11 link-layer types supported only on 802.11");
7514 gen_p80211_fcdir(int fcdir)
7520 case DLT_IEEE802_11:
7521 case DLT_PRISM_HEADER:
7522 case DLT_IEEE802_11_RADIO_AVS:
7523 case DLT_IEEE802_11_RADIO:
7527 bpf_error("frame direction supported only with 802.11 headers");
7531 b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
7532 (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
7539 register const u_char *eaddr;
7545 case DLT_ARCNET_LINUX:
7546 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
7548 return (gen_ahostop(eaddr, (int)q.dir));
7550 bpf_error("ARCnet address used in non-arc expression");
7556 bpf_error("aid supported only on ARCnet");
7559 bpf_error("ARCnet address used in non-arc expression");
7564 static struct block *
7565 gen_ahostop(eaddr, dir)
7566 register const u_char *eaddr;
7569 register struct block *b0, *b1;
7572 /* src comes first, different from Ethernet */
7574 return gen_bcmp(OR_LINK, 0, 1, eaddr);
7577 return gen_bcmp(OR_LINK, 1, 1, eaddr);
7580 b0 = gen_ahostop(eaddr, Q_SRC);
7581 b1 = gen_ahostop(eaddr, Q_DST);
7587 b0 = gen_ahostop(eaddr, Q_SRC);
7588 b1 = gen_ahostop(eaddr, Q_DST);
7597 * support IEEE 802.1Q VLAN trunk over ethernet
7603 struct block *b0, *b1;
7605 /* can't check for VLAN-encapsulated packets inside MPLS */
7606 if (label_stack_depth > 0)
7607 bpf_error("no VLAN match after MPLS");
7610 * Check for a VLAN packet, and then change the offsets to point
7611 * to the type and data fields within the VLAN packet. Just
7612 * increment the offsets, so that we can support a hierarchy, e.g.
7613 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7616 * XXX - this is a bit of a kludge. If we were to split the
7617 * compiler into a parser that parses an expression and
7618 * generates an expression tree, and a code generator that
7619 * takes an expression tree (which could come from our
7620 * parser or from some other parser) and generates BPF code,
7621 * we could perhaps make the offsets parameters of routines
7622 * and, in the handler for an "AND" node, pass to subnodes
7623 * other than the VLAN node the adjusted offsets.
7625 * This would mean that "vlan" would, instead of changing the
7626 * behavior of *all* tests after it, change only the behavior
7627 * of tests ANDed with it. That would change the documented
7628 * semantics of "vlan", which might break some expressions.
7629 * However, it would mean that "(vlan and ip) or ip" would check
7630 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7631 * checking only for VLAN-encapsulated IP, so that could still
7632 * be considered worth doing; it wouldn't break expressions
7633 * that are of the form "vlan and ..." or "vlan N and ...",
7634 * which I suspect are the most common expressions involving
7635 * "vlan". "vlan or ..." doesn't necessarily do what the user
7636 * would really want, now, as all the "or ..." tests would
7637 * be done assuming a VLAN, even though the "or" could be viewed
7638 * as meaning "or, if this isn't a VLAN packet...".
7645 /* check for VLAN */
7646 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7647 (bpf_int32)ETHERTYPE_8021Q);
7649 /* If a specific VLAN is requested, check VLAN id */
7650 if (vlan_num >= 0) {
7651 b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
7652 (bpf_int32)vlan_num, 0x0fff);
7666 bpf_error("no VLAN support for data link type %d",
7681 struct block *b0,*b1;
7684 * Change the offsets to point to the type and data fields within
7685 * the MPLS packet. Just increment the offsets, so that we
7686 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7687 * capture packets with an outer label of 100000 and an inner
7690 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7694 if (label_stack_depth > 0) {
7695 /* just match the bottom-of-stack bit clear */
7696 b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
7699 * Indicate that we're checking MPLS-encapsulated headers,
7700 * to make sure higher level code generators don't try to
7701 * match against IP-related protocols such as Q_ARP, Q_RARP
7706 case DLT_C_HDLC: /* fall through */
7708 b0 = gen_linktype(ETHERTYPE_MPLS);
7712 b0 = gen_linktype(PPP_MPLS_UCAST);
7715 /* FIXME add other DLT_s ...
7716 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7717 * leave it for now */
7720 bpf_error("no MPLS support for data link type %d",
7728 /* If a specific MPLS label is requested, check it */
7729 if (label_num >= 0) {
7730 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
7731 b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
7732 0xfffff000); /* only compare the first 20 bits */
7739 label_stack_depth++;
7744 * Support PPPOE discovery and session.
7749 /* check for PPPoE discovery */
7750 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
7759 * Test against the PPPoE session link-layer type.
7761 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
7764 * Change the offsets to point to the type and data fields within
7765 * the PPP packet, and note that this is PPPoE rather than
7768 * XXX - this is a bit of a kludge. If we were to split the
7769 * compiler into a parser that parses an expression and
7770 * generates an expression tree, and a code generator that
7771 * takes an expression tree (which could come from our
7772 * parser or from some other parser) and generates BPF code,
7773 * we could perhaps make the offsets parameters of routines
7774 * and, in the handler for an "AND" node, pass to subnodes
7775 * other than the PPPoE node the adjusted offsets.
7777 * This would mean that "pppoes" would, instead of changing the
7778 * behavior of *all* tests after it, change only the behavior
7779 * of tests ANDed with it. That would change the documented
7780 * semantics of "pppoes", which might break some expressions.
7781 * However, it would mean that "(pppoes and ip) or ip" would check
7782 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7783 * checking only for VLAN-encapsulated IP, so that could still
7784 * be considered worth doing; it wouldn't break expressions
7785 * that are of the form "pppoes and ..." which I suspect are the
7786 * most common expressions involving "pppoes". "pppoes or ..."
7787 * doesn't necessarily do what the user would really want, now,
7788 * as all the "or ..." tests would be done assuming PPPoE, even
7789 * though the "or" could be viewed as meaning "or, if this isn't
7790 * a PPPoE packet...".
7792 orig_linktype = off_linktype; /* save original values */
7797 * The "network-layer" protocol is PPPoE, which has a 6-byte
7798 * PPPoE header, followed by a PPP packet.
7800 * There is no HDLC encapsulation for the PPP packet (it's
7801 * encapsulated in PPPoES instead), so the link-layer type
7802 * starts at the first byte of the PPP packet. For PPPoE,
7803 * that offset is relative to the beginning of the total
7804 * link-layer payload, including any 802.2 LLC header, so
7805 * it's 6 bytes past off_nl.
7807 off_linktype = off_nl + 6;
7810 * The network-layer offsets are relative to the beginning
7811 * of the MAC-layer payload; that's past the 6-byte
7812 * PPPoE header and the 2-byte PPP header.
7815 off_nl_nosnap = 6+2;
7821 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
7833 bpf_error("'vpi' supported only on raw ATM");
7834 if (off_vpi == (u_int)-1)
7836 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
7842 bpf_error("'vci' supported only on raw ATM");
7843 if (off_vci == (u_int)-1)
7845 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
7850 if (off_proto == (u_int)-1)
7851 abort(); /* XXX - this isn't on FreeBSD */
7852 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
7857 if (off_payload == (u_int)-1)
7859 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
7860 0xffffffff, jtype, reverse, jvalue);
7865 bpf_error("'callref' supported only on raw ATM");
7866 if (off_proto == (u_int)-1)
7868 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
7869 jtype, reverse, jvalue);
7879 gen_atmtype_abbrev(type)
7882 struct block *b0, *b1;
7887 /* Get all packets in Meta signalling Circuit */
7889 bpf_error("'metac' supported only on raw ATM");
7890 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7891 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
7896 /* Get all packets in Broadcast Circuit*/
7898 bpf_error("'bcc' supported only on raw ATM");
7899 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7900 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
7905 /* Get all cells in Segment OAM F4 circuit*/
7907 bpf_error("'oam4sc' supported only on raw ATM");
7908 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7909 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
7914 /* Get all cells in End-to-End OAM F4 Circuit*/
7916 bpf_error("'oam4ec' supported only on raw ATM");
7917 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7918 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
7923 /* Get all packets in connection Signalling Circuit */
7925 bpf_error("'sc' supported only on raw ATM");
7926 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7927 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
7932 /* Get all packets in ILMI Circuit */
7934 bpf_error("'ilmic' supported only on raw ATM");
7935 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7936 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
7941 /* Get all LANE packets */
7943 bpf_error("'lane' supported only on raw ATM");
7944 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
7947 * Arrange that all subsequent tests assume LANE
7948 * rather than LLC-encapsulated packets, and set
7949 * the offsets appropriately for LANE-encapsulated
7952 * "off_mac" is the offset of the Ethernet header,
7953 * which is 2 bytes past the ATM pseudo-header
7954 * (skipping the pseudo-header and 2-byte LE Client
7955 * field). The other offsets are Ethernet offsets
7956 * relative to "off_mac".
7959 off_mac = off_payload + 2; /* MAC header */
7960 off_linktype = off_mac + 12;
7961 off_macpl = off_mac + 14; /* Ethernet */
7962 off_nl = 0; /* Ethernet II */
7963 off_nl_nosnap = 3; /* 802.3+802.2 */
7967 /* Get all LLC-encapsulated packets */
7969 bpf_error("'llc' supported only on raw ATM");
7970 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
7981 * Filtering for MTP2 messages based on li value
7982 * FISU, length is null
7983 * LSSU, length is 1 or 2
7984 * MSU, length is 3 or more
7987 gen_mtp2type_abbrev(type)
7990 struct block *b0, *b1;
7995 if ( (linktype != DLT_MTP2) &&
7996 (linktype != DLT_ERF) &&
7997 (linktype != DLT_MTP2_WITH_PHDR) )
7998 bpf_error("'fisu' supported only on MTP2");
7999 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8000 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8004 if ( (linktype != DLT_MTP2) &&
8005 (linktype != DLT_ERF) &&
8006 (linktype != DLT_MTP2_WITH_PHDR) )
8007 bpf_error("'lssu' supported only on MTP2");
8008 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8009 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
8014 if ( (linktype != DLT_MTP2) &&
8015 (linktype != DLT_ERF) &&
8016 (linktype != DLT_MTP2_WITH_PHDR) )
8017 bpf_error("'msu' supported only on MTP2");
8018 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
8028 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
8035 bpf_u_int32 val1 , val2 , val3;
8037 switch (mtp3field) {
8040 if (off_sio == (u_int)-1)
8041 bpf_error("'sio' supported only on SS7");
8042 /* sio coded on 1 byte so max value 255 */
8044 bpf_error("sio value %u too big; max value = 255",
8046 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
8047 (u_int)jtype, reverse, (u_int)jvalue);
8051 if (off_opc == (u_int)-1)
8052 bpf_error("'opc' supported only on SS7");
8053 /* opc coded on 14 bits so max value 16383 */
8055 bpf_error("opc value %u too big; max value = 16383",
8057 /* the following instructions are made to convert jvalue
8058 * to the form used to write opc in an ss7 message*/
8059 val1 = jvalue & 0x00003c00;
8061 val2 = jvalue & 0x000003fc;
8063 val3 = jvalue & 0x00000003;
8065 jvalue = val1 + val2 + val3;
8066 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
8067 (u_int)jtype, reverse, (u_int)jvalue);
8071 if (off_dpc == (u_int)-1)
8072 bpf_error("'dpc' supported only on SS7");
8073 /* dpc coded on 14 bits so max value 16383 */
8075 bpf_error("dpc value %u too big; max value = 16383",
8077 /* the following instructions are made to convert jvalue
8078 * to the forme used to write dpc in an ss7 message*/
8079 val1 = jvalue & 0x000000ff;
8081 val2 = jvalue & 0x00003f00;
8083 jvalue = val1 + val2;
8084 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
8085 (u_int)jtype, reverse, (u_int)jvalue);
8089 if (off_sls == (u_int)-1)
8090 bpf_error("'sls' supported only on SS7");
8091 /* sls coded on 4 bits so max value 15 */
8093 bpf_error("sls value %u too big; max value = 15",
8095 /* the following instruction is made to convert jvalue
8096 * to the forme used to write sls in an ss7 message*/
8097 jvalue = jvalue << 4;
8098 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
8099 (u_int)jtype,reverse, (u_int)jvalue);
8108 static struct block *
8109 gen_msg_abbrev(type)
8115 * Q.2931 signalling protocol messages for handling virtual circuits
8116 * establishment and teardown
8121 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
8125 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
8129 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
8133 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
8137 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
8140 case A_RELEASE_DONE:
8141 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
8151 gen_atmmulti_abbrev(type)
8154 struct block *b0, *b1;
8160 bpf_error("'oam' supported only on raw ATM");
8161 b1 = gen_atmmulti_abbrev(A_OAMF4);
8166 bpf_error("'oamf4' supported only on raw ATM");
8168 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8169 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8171 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8177 * Get Q.2931 signalling messages for switched
8178 * virtual connection
8181 bpf_error("'connectmsg' supported only on raw ATM");
8182 b0 = gen_msg_abbrev(A_SETUP);
8183 b1 = gen_msg_abbrev(A_CALLPROCEED);
8185 b0 = gen_msg_abbrev(A_CONNECT);
8187 b0 = gen_msg_abbrev(A_CONNECTACK);
8189 b0 = gen_msg_abbrev(A_RELEASE);
8191 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8193 b0 = gen_atmtype_abbrev(A_SC);
8199 bpf_error("'metaconnect' supported only on raw ATM");
8200 b0 = gen_msg_abbrev(A_SETUP);
8201 b1 = gen_msg_abbrev(A_CALLPROCEED);
8203 b0 = gen_msg_abbrev(A_CONNECT);
8205 b0 = gen_msg_abbrev(A_RELEASE);
8207 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8209 b0 = gen_atmtype_abbrev(A_METAC);