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.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
34 #include <pcap-stdinc.h>
41 #ifdef HAVE_SYS_BITYPES_H
42 #include <sys/bitypes.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
49 * XXX - why was this included even on UNIX?
58 #include <sys/param.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
78 #include "ethertype.h"
82 #include "ieee80211.h"
84 #include "sunatmpos.h"
87 #include "pcap/ipnet.h"
89 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
90 #include <linux/types.h>
91 #include <linux/if_packet.h>
92 #include <linux/filter.h>
94 #ifdef HAVE_NET_PFVAR_H
95 #include <sys/socket.h>
97 #include <net/pfvar.h>
98 #include <net/if_pflog.h>
101 #define offsetof(s, e) ((size_t)&((s *)0)->e)
105 #include <netdb.h> /* for "struct addrinfo" */
108 #include <pcap/namedb.h>
110 #define ETHERMTU 1500
113 #define IPPROTO_SCTP 132
116 #ifdef HAVE_OS_PROTO_H
117 #include "os-proto.h"
120 #define JMP(c) ((c)|BPF_JMP|BPF_K)
123 static jmp_buf top_ctx;
124 static pcap_t *bpf_pcap;
126 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
128 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
130 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
135 static int pcap_fddipad;
140 bpf_error(const char *fmt, ...)
145 if (bpf_pcap != NULL)
146 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
153 static void init_linktype(pcap_t *);
155 static void init_regs(void);
156 static int alloc_reg(void);
157 static void free_reg(int);
159 static struct block *root;
162 * Value passed to gen_load_a() to indicate what the offset argument
166 OR_PACKET, /* relative to the beginning of the packet */
167 OR_LINK, /* relative to the beginning of the link-layer header */
168 OR_MACPL, /* relative to the end of the MAC-layer header */
169 OR_NET, /* relative to the network-layer header */
170 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
171 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
172 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
177 * As errors are handled by a longjmp, anything allocated must be freed
178 * in the longjmp handler, so it must be reachable from that handler.
179 * One thing that's allocated is the result of pcap_nametoaddrinfo();
180 * it must be freed with freeaddrinfo(). This variable points to any
181 * addrinfo structure that would need to be freed.
183 static struct addrinfo *ai;
187 * We divy out chunks of memory rather than call malloc each time so
188 * we don't have to worry about leaking memory. It's probably
189 * not a big deal if all this memory was wasted but if this ever
190 * goes into a library that would probably not be a good idea.
192 * XXX - this *is* in a library....
195 #define CHUNK0SIZE 1024
201 static struct chunk chunks[NCHUNKS];
202 static int cur_chunk;
204 static void *newchunk(u_int);
205 static void freechunks(void);
206 static inline struct block *new_block(int);
207 static inline struct slist *new_stmt(int);
208 static struct block *gen_retblk(int);
209 static inline void syntax(void);
211 static void backpatch(struct block *, struct block *);
212 static void merge(struct block *, struct block *);
213 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
214 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
215 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
216 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
217 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
218 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
220 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
221 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
222 bpf_u_int32, bpf_u_int32, int, bpf_int32);
223 static struct slist *gen_load_llrel(u_int, u_int);
224 static struct slist *gen_load_macplrel(u_int, u_int);
225 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
226 static struct slist *gen_loadx_iphdrlen(void);
227 static struct block *gen_uncond(int);
228 static inline struct block *gen_true(void);
229 static inline struct block *gen_false(void);
230 static struct block *gen_ether_linktype(int);
231 static struct block *gen_ipnet_linktype(int);
232 static struct block *gen_linux_sll_linktype(int);
233 static struct slist *gen_load_prism_llprefixlen(void);
234 static struct slist *gen_load_avs_llprefixlen(void);
235 static struct slist *gen_load_radiotap_llprefixlen(void);
236 static struct slist *gen_load_ppi_llprefixlen(void);
237 static void insert_compute_vloffsets(struct block *);
238 static struct slist *gen_llprefixlen(void);
239 static struct slist *gen_off_macpl(void);
240 static int ethertype_to_ppptype(int);
241 static struct block *gen_linktype(int);
242 static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
243 static struct block *gen_llc_linktype(int);
244 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
246 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
248 static struct block *gen_ahostop(const u_char *, int);
249 static struct block *gen_ehostop(const u_char *, int);
250 static struct block *gen_fhostop(const u_char *, int);
251 static struct block *gen_thostop(const u_char *, int);
252 static struct block *gen_wlanhostop(const u_char *, int);
253 static struct block *gen_ipfchostop(const u_char *, int);
254 static struct block *gen_dnhostop(bpf_u_int32, int);
255 static struct block *gen_mpls_linktype(int);
256 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
258 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
261 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
263 static struct block *gen_ipfrag(void);
264 static struct block *gen_portatom(int, bpf_int32);
265 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
267 static struct block *gen_portatom6(int, bpf_int32);
268 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
270 struct block *gen_portop(int, int, int);
271 static struct block *gen_port(int, int, int);
272 struct block *gen_portrangeop(int, int, int, int);
273 static struct block *gen_portrange(int, int, int, int);
275 struct block *gen_portop6(int, int, int);
276 static struct block *gen_port6(int, int, int);
277 struct block *gen_portrangeop6(int, int, int, int);
278 static struct block *gen_portrange6(int, int, int, int);
280 static int lookup_proto(const char *, int);
281 static struct block *gen_protochain(int, int, int);
282 static struct block *gen_proto(int, int, int);
283 static struct slist *xfer_to_x(struct arth *);
284 static struct slist *xfer_to_a(struct arth *);
285 static struct block *gen_mac_multicast(int);
286 static struct block *gen_len(int, int);
287 static struct block *gen_check_802_11_data_frame(void);
289 static struct block *gen_ppi_dlt_check(void);
290 static struct block *gen_msg_abbrev(int type);
301 /* XXX Round up to nearest long. */
302 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
304 /* XXX Round up to structure boundary. */
308 cp = &chunks[cur_chunk];
309 if (n > cp->n_left) {
310 ++cp, k = ++cur_chunk;
312 bpf_error("out of memory");
313 size = CHUNK0SIZE << k;
314 cp->m = (void *)malloc(size);
316 bpf_error("out of memory");
317 memset((char *)cp->m, 0, size);
320 bpf_error("out of memory");
323 return (void *)((char *)cp->m + cp->n_left);
332 for (i = 0; i < NCHUNKS; ++i)
333 if (chunks[i].m != NULL) {
340 * A strdup whose allocations are freed after code generation is over.
344 register const char *s;
346 int n = strlen(s) + 1;
347 char *cp = newchunk(n);
353 static inline struct block *
359 p = (struct block *)newchunk(sizeof(*p));
366 static inline struct slist *
372 p = (struct slist *)newchunk(sizeof(*p));
378 static struct block *
382 struct block *b = new_block(BPF_RET|BPF_K);
391 bpf_error("syntax error in filter expression");
394 static bpf_u_int32 netmask;
399 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
400 const char *buf, int optimize, bpf_u_int32 mask);
403 pcap_compile(pcap_t *p, struct bpf_program *program,
404 const char *buf, int optimize, bpf_u_int32 mask)
408 EnterCriticalSection(&g_PcapCompileCriticalSection);
410 result = pcap_compile_unsafe(p, program, buf, optimize, mask);
412 LeaveCriticalSection(&g_PcapCompileCriticalSection);
418 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
419 const char *buf, int optimize, bpf_u_int32 mask)
422 pcap_compile(pcap_t *p, struct bpf_program *program,
423 const char *buf, int optimize, bpf_u_int32 mask)
427 const char * volatile xbuf = buf;
435 if (setjmp(top_ctx)) {
449 snaplen = pcap_snapshot(p);
451 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
452 "snaplen of 0 rejects all packets");
456 lex_init(xbuf ? xbuf : "");
464 root = gen_retblk(snaplen);
466 if (optimize && !no_optimize) {
469 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
470 bpf_error("expression rejects all packets");
472 program->bf_insns = icode_to_fcode(root, &len);
473 program->bf_len = len;
481 * entry point for using the compiler with no pcap open
482 * pass in all the stuff that is needed explicitly instead.
485 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
486 struct bpf_program *program,
487 const char *buf, int optimize, bpf_u_int32 mask)
492 p = pcap_open_dead(linktype_arg, snaplen_arg);
495 ret = pcap_compile(p, program, buf, optimize, mask);
501 * Clean up a "struct bpf_program" by freeing all the memory allocated
505 pcap_freecode(struct bpf_program *program)
508 if (program->bf_insns != NULL) {
509 free((char *)program->bf_insns);
510 program->bf_insns = NULL;
515 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
516 * which of the jt and jf fields has been resolved and which is a pointer
517 * back to another unresolved block (or nil). At least one of the fields
518 * in each block is already resolved.
521 backpatch(list, target)
522 struct block *list, *target;
539 * Merge the lists in b0 and b1, using the 'sense' field to indicate
540 * which of jt and jf is the link.
544 struct block *b0, *b1;
546 register struct block **p = &b0;
548 /* Find end of list. */
550 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
552 /* Concatenate the lists. */
560 struct block *ppi_dlt_check;
563 * Insert before the statements of the first (root) block any
564 * statements needed to load the lengths of any variable-length
565 * headers into registers.
567 * XXX - a fancier strategy would be to insert those before the
568 * statements of all blocks that use those lengths and that
569 * have no predecessors that use them, so that we only compute
570 * the lengths if we need them. There might be even better
571 * approaches than that.
573 * However, those strategies would be more complicated, and
574 * as we don't generate code to compute a length if the
575 * program has no tests that use the length, and as most
576 * tests will probably use those lengths, we would just
577 * postpone computing the lengths so that it's not done
578 * for tests that fail early, and it's not clear that's
581 insert_compute_vloffsets(p->head);
584 * For DLT_PPI captures, generate a check of the per-packet
585 * DLT value to make sure it's DLT_IEEE802_11.
587 ppi_dlt_check = gen_ppi_dlt_check();
588 if (ppi_dlt_check != NULL)
589 gen_and(ppi_dlt_check, p);
591 backpatch(p, gen_retblk(snaplen));
592 p->sense = !p->sense;
593 backpatch(p, gen_retblk(0));
599 struct block *b0, *b1;
601 backpatch(b0, b1->head);
602 b0->sense = !b0->sense;
603 b1->sense = !b1->sense;
605 b1->sense = !b1->sense;
611 struct block *b0, *b1;
613 b0->sense = !b0->sense;
614 backpatch(b0, b1->head);
615 b0->sense = !b0->sense;
624 b->sense = !b->sense;
627 static struct block *
628 gen_cmp(offrel, offset, size, v)
629 enum e_offrel offrel;
633 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
636 static struct block *
637 gen_cmp_gt(offrel, offset, size, v)
638 enum e_offrel offrel;
642 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
645 static struct block *
646 gen_cmp_ge(offrel, offset, size, v)
647 enum e_offrel offrel;
651 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
654 static struct block *
655 gen_cmp_lt(offrel, offset, size, v)
656 enum e_offrel offrel;
660 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
663 static struct block *
664 gen_cmp_le(offrel, offset, size, v)
665 enum e_offrel offrel;
669 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
672 static struct block *
673 gen_mcmp(offrel, offset, size, v, mask)
674 enum e_offrel offrel;
679 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
682 static struct block *
683 gen_bcmp(offrel, offset, size, v)
684 enum e_offrel offrel;
685 register u_int offset, size;
686 register const u_char *v;
688 register struct block *b, *tmp;
692 register const u_char *p = &v[size - 4];
693 bpf_int32 w = ((bpf_int32)p[0] << 24) |
694 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
696 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
703 register const u_char *p = &v[size - 2];
704 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
706 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
713 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
722 * AND the field of size "size" at offset "offset" relative to the header
723 * specified by "offrel" with "mask", and compare it with the value "v"
724 * with the test specified by "jtype"; if "reverse" is true, the test
725 * should test the opposite of "jtype".
727 static struct block *
728 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
729 enum e_offrel offrel;
731 bpf_u_int32 offset, size, mask, jtype;
734 struct slist *s, *s2;
737 s = gen_load_a(offrel, offset, size);
739 if (mask != 0xffffffff) {
740 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
745 b = new_block(JMP(jtype));
748 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
754 * Various code constructs need to know the layout of the data link
755 * layer. These variables give the necessary offsets from the beginning
756 * of the packet data.
760 * This is the offset of the beginning of the link-layer header from
761 * the beginning of the raw packet data.
763 * It's usually 0, except for 802.11 with a fixed-length radio header.
764 * (For 802.11 with a variable-length radio header, we have to generate
765 * code to compute that offset; off_ll is 0 in that case.)
770 * If there's a variable-length header preceding the link-layer header,
771 * "reg_off_ll" is the register number for a register containing the
772 * length of that header, and therefore the offset of the link-layer
773 * header from the beginning of the raw packet data. Otherwise,
774 * "reg_off_ll" is -1.
776 static int reg_off_ll;
779 * This is the offset of the beginning of the MAC-layer header from
780 * the beginning of the link-layer header.
781 * It's usually 0, except for ATM LANE, where it's the offset, relative
782 * to the beginning of the raw packet data, of the Ethernet header, and
783 * for Ethernet with various additional information.
785 static u_int off_mac;
788 * This is the offset of the beginning of the MAC-layer payload,
789 * from the beginning of the raw packet data.
791 * I.e., it's the sum of the length of the link-layer header (without,
792 * for example, any 802.2 LLC header, so it's the MAC-layer
793 * portion of that header), plus any prefix preceding the
796 static u_int off_macpl;
799 * This is 1 if the offset of the beginning of the MAC-layer payload
800 * from the beginning of the link-layer header is variable-length.
802 static int off_macpl_is_variable;
805 * If the link layer has variable_length headers, "reg_off_macpl"
806 * is the register number for a register containing the length of the
807 * link-layer header plus the length of any variable-length header
808 * preceding the link-layer header. Otherwise, "reg_off_macpl"
811 static int reg_off_macpl;
814 * "off_linktype" is the offset to information in the link-layer header
815 * giving the packet type. This offset is relative to the beginning
816 * of the link-layer header (i.e., it doesn't include off_ll).
818 * For Ethernet, it's the offset of the Ethernet type field.
820 * For link-layer types that always use 802.2 headers, it's the
821 * offset of the LLC header.
823 * For PPP, it's the offset of the PPP type field.
825 * For Cisco HDLC, it's the offset of the CHDLC type field.
827 * For BSD loopback, it's the offset of the AF_ value.
829 * For Linux cooked sockets, it's the offset of the type field.
831 * It's set to -1 for no encapsulation, in which case, IP is assumed.
833 static u_int off_linktype;
836 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
837 * checks to check the PPP header, assumed to follow a LAN-style link-
838 * layer header and a PPPoE session header.
840 static int is_pppoes = 0;
843 * TRUE if the link layer includes an ATM pseudo-header.
845 static int is_atm = 0;
848 * TRUE if "lane" appeared in the filter; it causes us to generate
849 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
851 static int is_lane = 0;
854 * These are offsets for the ATM pseudo-header.
856 static u_int off_vpi;
857 static u_int off_vci;
858 static u_int off_proto;
861 * These are offsets for the MTP2 fields.
866 * These are offsets for the MTP3 fields.
868 static u_int off_sio;
869 static u_int off_opc;
870 static u_int off_dpc;
871 static u_int off_sls;
874 * This is the offset of the first byte after the ATM pseudo_header,
875 * or -1 if there is no ATM pseudo-header.
877 static u_int off_payload;
880 * These are offsets to the beginning of the network-layer header.
881 * They are relative to the beginning of the MAC-layer payload (i.e.,
882 * they don't include off_ll or off_macpl).
884 * If the link layer never uses 802.2 LLC:
886 * "off_nl" and "off_nl_nosnap" are the same.
888 * If the link layer always uses 802.2 LLC:
890 * "off_nl" is the offset if there's a SNAP header following
893 * "off_nl_nosnap" is the offset if there's no SNAP header.
895 * If the link layer is Ethernet:
897 * "off_nl" is the offset if the packet is an Ethernet II packet
898 * (we assume no 802.3+802.2+SNAP);
900 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
901 * with an 802.2 header following it.
904 static u_int off_nl_nosnap;
912 linktype = pcap_datalink(p);
914 pcap_fddipad = p->fddipad;
918 * Assume it's not raw ATM with a pseudo-header, for now.
929 * And that we're not doing PPPoE.
934 * And assume we're not doing SS7.
943 * Also assume it's not 802.11.
947 off_macpl_is_variable = 0;
951 label_stack_depth = 0;
961 off_nl = 0; /* XXX in reality, variable! */
962 off_nl_nosnap = 0; /* no 802.2 LLC */
965 case DLT_ARCNET_LINUX:
968 off_nl = 0; /* XXX in reality, variable! */
969 off_nl_nosnap = 0; /* no 802.2 LLC */
974 off_macpl = 14; /* Ethernet header length */
975 off_nl = 0; /* Ethernet II */
976 off_nl_nosnap = 3; /* 802.3+802.2 */
981 * SLIP doesn't have a link level type. The 16 byte
982 * header is hacked into our SLIP driver.
987 off_nl_nosnap = 0; /* no 802.2 LLC */
991 /* XXX this may be the same as the DLT_PPP_BSDOS case */
996 off_nl_nosnap = 0; /* no 802.2 LLC */
1004 off_nl_nosnap = 0; /* no 802.2 LLC */
1011 off_nl_nosnap = 0; /* no 802.2 LLC */
1016 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1017 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1021 off_nl_nosnap = 0; /* no 802.2 LLC */
1026 * This does no include the Ethernet header, and
1027 * only covers session state.
1032 off_nl_nosnap = 0; /* no 802.2 LLC */
1039 off_nl_nosnap = 0; /* no 802.2 LLC */
1044 * FDDI doesn't really have a link-level type field.
1045 * We set "off_linktype" to the offset of the LLC header.
1047 * To check for Ethernet types, we assume that SSAP = SNAP
1048 * is being used and pick out the encapsulated Ethernet type.
1049 * XXX - should we generate code to check for SNAP?
1053 off_linktype += pcap_fddipad;
1055 off_macpl = 13; /* FDDI MAC header length */
1057 off_macpl += pcap_fddipad;
1059 off_nl = 8; /* 802.2+SNAP */
1060 off_nl_nosnap = 3; /* 802.2 */
1065 * Token Ring doesn't really have a link-level type field.
1066 * We set "off_linktype" to the offset of the LLC header.
1068 * To check for Ethernet types, we assume that SSAP = SNAP
1069 * is being used and pick out the encapsulated Ethernet type.
1070 * XXX - should we generate code to check for SNAP?
1072 * XXX - the header is actually variable-length.
1073 * Some various Linux patched versions gave 38
1074 * as "off_linktype" and 40 as "off_nl"; however,
1075 * if a token ring packet has *no* routing
1076 * information, i.e. is not source-routed, the correct
1077 * values are 20 and 22, as they are in the vanilla code.
1079 * A packet is source-routed iff the uppermost bit
1080 * of the first byte of the source address, at an
1081 * offset of 8, has the uppermost bit set. If the
1082 * packet is source-routed, the total number of bytes
1083 * of routing information is 2 plus bits 0x1F00 of
1084 * the 16-bit value at an offset of 14 (shifted right
1085 * 8 - figure out which byte that is).
1088 off_macpl = 14; /* Token Ring MAC header length */
1089 off_nl = 8; /* 802.2+SNAP */
1090 off_nl_nosnap = 3; /* 802.2 */
1093 case DLT_IEEE802_11:
1094 case DLT_PRISM_HEADER:
1095 case DLT_IEEE802_11_RADIO_AVS:
1096 case DLT_IEEE802_11_RADIO:
1098 * 802.11 doesn't really have a link-level type field.
1099 * We set "off_linktype" to the offset of the LLC header.
1101 * To check for Ethernet types, we assume that SSAP = SNAP
1102 * is being used and pick out the encapsulated Ethernet type.
1103 * XXX - should we generate code to check for SNAP?
1105 * We also handle variable-length radio headers here.
1106 * The Prism header is in theory variable-length, but in
1107 * practice it's always 144 bytes long. However, some
1108 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1109 * sometimes or always supply an AVS header, so we
1110 * have to check whether the radio header is a Prism
1111 * header or an AVS header, so, in practice, it's
1115 off_macpl = 0; /* link-layer header is variable-length */
1116 off_macpl_is_variable = 1;
1117 off_nl = 8; /* 802.2+SNAP */
1118 off_nl_nosnap = 3; /* 802.2 */
1123 * At the moment we treat PPI the same way that we treat
1124 * normal Radiotap encoded packets. The difference is in
1125 * the function that generates the code at the beginning
1126 * to compute the header length. Since this code generator
1127 * of PPI supports bare 802.11 encapsulation only (i.e.
1128 * the encapsulated DLT should be DLT_IEEE802_11) we
1129 * generate code to check for this too.
1132 off_macpl = 0; /* link-layer header is variable-length */
1133 off_macpl_is_variable = 1;
1134 off_nl = 8; /* 802.2+SNAP */
1135 off_nl_nosnap = 3; /* 802.2 */
1138 case DLT_ATM_RFC1483:
1139 case DLT_ATM_CLIP: /* Linux ATM defines this */
1141 * assume routed, non-ISO PDUs
1142 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1144 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1145 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1146 * latter would presumably be treated the way PPPoE
1147 * should be, so you can do "pppoe and udp port 2049"
1148 * or "pppoa and tcp port 80" and have it check for
1149 * PPPo{A,E} and a PPP protocol of IP and....
1152 off_macpl = 0; /* packet begins with LLC header */
1153 off_nl = 8; /* 802.2+SNAP */
1154 off_nl_nosnap = 3; /* 802.2 */
1159 * Full Frontal ATM; you get AALn PDUs with an ATM
1163 off_vpi = SUNATM_VPI_POS;
1164 off_vci = SUNATM_VCI_POS;
1165 off_proto = PROTO_POS;
1166 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1167 off_payload = SUNATM_PKT_BEGIN_POS;
1168 off_linktype = off_payload;
1169 off_macpl = off_payload; /* if LLC-encapsulated */
1170 off_nl = 8; /* 802.2+SNAP */
1171 off_nl_nosnap = 3; /* 802.2 */
1180 off_nl_nosnap = 0; /* no 802.2 LLC */
1183 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1187 off_nl_nosnap = 0; /* no 802.2 LLC */
1192 * LocalTalk does have a 1-byte type field in the LLAP header,
1193 * but really it just indicates whether there is a "short" or
1194 * "long" DDP packet following.
1199 off_nl_nosnap = 0; /* no 802.2 LLC */
1202 case DLT_IP_OVER_FC:
1204 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1205 * link-level type field. We set "off_linktype" to the
1206 * offset of the LLC header.
1208 * To check for Ethernet types, we assume that SSAP = SNAP
1209 * is being used and pick out the encapsulated Ethernet type.
1210 * XXX - should we generate code to check for SNAP? RFC
1211 * 2625 says SNAP should be used.
1215 off_nl = 8; /* 802.2+SNAP */
1216 off_nl_nosnap = 3; /* 802.2 */
1221 * XXX - we should set this to handle SNAP-encapsulated
1222 * frames (NLPID of 0x80).
1227 off_nl_nosnap = 0; /* no 802.2 LLC */
1231 * the only BPF-interesting FRF.16 frames are non-control frames;
1232 * Frame Relay has a variable length link-layer
1233 * so lets start with offset 4 for now and increments later on (FIXME);
1239 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1242 case DLT_APPLE_IP_OVER_IEEE1394:
1246 off_nl_nosnap = 0; /* no 802.2 LLC */
1249 case DLT_SYMANTEC_FIREWALL:
1252 off_nl = 0; /* Ethernet II */
1253 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1256 #ifdef HAVE_NET_PFVAR_H
1259 off_macpl = PFLOG_HDRLEN;
1261 off_nl_nosnap = 0; /* no 802.2 LLC */
1265 case DLT_JUNIPER_MFR:
1266 case DLT_JUNIPER_MLFR:
1267 case DLT_JUNIPER_MLPPP:
1268 case DLT_JUNIPER_PPP:
1269 case DLT_JUNIPER_CHDLC:
1270 case DLT_JUNIPER_FRELAY:
1274 off_nl_nosnap = -1; /* no 802.2 LLC */
1277 case DLT_JUNIPER_ATM1:
1278 off_linktype = 4; /* in reality variable between 4-8 */
1279 off_macpl = 4; /* in reality variable between 4-8 */
1284 case DLT_JUNIPER_ATM2:
1285 off_linktype = 8; /* in reality variable between 8-12 */
1286 off_macpl = 8; /* in reality variable between 8-12 */
1291 /* frames captured on a Juniper PPPoE service PIC
1292 * contain raw ethernet frames */
1293 case DLT_JUNIPER_PPPOE:
1294 case DLT_JUNIPER_ETHER:
1297 off_nl = 18; /* Ethernet II */
1298 off_nl_nosnap = 21; /* 802.3+802.2 */
1301 case DLT_JUNIPER_PPPOE_ATM:
1305 off_nl_nosnap = -1; /* no 802.2 LLC */
1308 case DLT_JUNIPER_GGSN:
1312 off_nl_nosnap = -1; /* no 802.2 LLC */
1315 case DLT_JUNIPER_ES:
1317 off_macpl = -1; /* not really a network layer but raw IP addresses */
1318 off_nl = -1; /* not really a network layer but raw IP addresses */
1319 off_nl_nosnap = -1; /* no 802.2 LLC */
1322 case DLT_JUNIPER_MONITOR:
1325 off_nl = 0; /* raw IP/IP6 header */
1326 off_nl_nosnap = -1; /* no 802.2 LLC */
1329 case DLT_JUNIPER_SERVICES:
1331 off_macpl = -1; /* L3 proto location dep. on cookie type */
1332 off_nl = -1; /* L3 proto location dep. on cookie type */
1333 off_nl_nosnap = -1; /* no 802.2 LLC */
1336 case DLT_JUNIPER_VP:
1343 case DLT_JUNIPER_ST:
1350 case DLT_JUNIPER_ISM:
1357 case DLT_JUNIPER_VS:
1358 case DLT_JUNIPER_SRX_E2E:
1359 case DLT_JUNIPER_FIBRECHANNEL:
1360 case DLT_JUNIPER_ATM_CEMIC:
1379 case DLT_MTP2_WITH_PHDR:
1412 * Currently, only raw "link[N:M]" filtering is supported.
1414 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1416 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1417 off_nl_nosnap = -1; /* no 802.2 LLC */
1418 off_mac = 1; /* step over the kiss length byte */
1423 off_macpl = 24; /* ipnet header length */
1428 case DLT_NETANALYZER:
1429 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1430 off_linktype = 16; /* includes 4-byte pseudo-header */
1431 off_macpl = 18; /* pseudo-header+Ethernet header length */
1432 off_nl = 0; /* Ethernet II */
1433 off_nl_nosnap = 3; /* 802.3+802.2 */
1436 case DLT_NETANALYZER_TRANSPARENT:
1437 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1438 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1439 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1440 off_nl = 0; /* Ethernet II */
1441 off_nl_nosnap = 3; /* 802.3+802.2 */
1446 * For values in the range in which we've assigned new
1447 * DLT_ values, only raw "link[N:M]" filtering is supported.
1449 if (linktype >= DLT_MATCHING_MIN &&
1450 linktype <= DLT_MATCHING_MAX) {
1459 bpf_error("unknown data link type %d", linktype);
1464 * Load a value relative to the beginning of the link-layer header.
1465 * The link-layer header doesn't necessarily begin at the beginning
1466 * of the packet data; there might be a variable-length prefix containing
1467 * radio information.
1469 static struct slist *
1470 gen_load_llrel(offset, size)
1473 struct slist *s, *s2;
1475 s = gen_llprefixlen();
1478 * If "s" is non-null, it has code to arrange that the X register
1479 * contains the length of the prefix preceding the link-layer
1482 * Otherwise, the length of the prefix preceding the link-layer
1483 * header is "off_ll".
1487 * There's a variable-length prefix preceding the
1488 * link-layer header. "s" points to a list of statements
1489 * that put the length of that prefix into the X register.
1490 * do an indirect load, to use the X register as an offset.
1492 s2 = new_stmt(BPF_LD|BPF_IND|size);
1497 * There is no variable-length header preceding the
1498 * link-layer header; add in off_ll, which, if there's
1499 * a fixed-length header preceding the link-layer header,
1500 * is the length of that header.
1502 s = new_stmt(BPF_LD|BPF_ABS|size);
1503 s->s.k = offset + off_ll;
1509 * Load a value relative to the beginning of the MAC-layer payload.
1511 static struct slist *
1512 gen_load_macplrel(offset, size)
1515 struct slist *s, *s2;
1517 s = gen_off_macpl();
1520 * If s is non-null, the offset of the MAC-layer payload is
1521 * variable, and s points to a list of instructions that
1522 * arrange that the X register contains that offset.
1524 * Otherwise, the offset of the MAC-layer payload is constant,
1525 * and is in off_macpl.
1529 * The offset of the MAC-layer payload is in the X
1530 * register. Do an indirect load, to use the X register
1533 s2 = new_stmt(BPF_LD|BPF_IND|size);
1538 * The offset of the MAC-layer payload is constant,
1539 * and is in off_macpl; load the value at that offset
1540 * plus the specified offset.
1542 s = new_stmt(BPF_LD|BPF_ABS|size);
1543 s->s.k = off_macpl + offset;
1549 * Load a value relative to the beginning of the specified header.
1551 static struct slist *
1552 gen_load_a(offrel, offset, size)
1553 enum e_offrel offrel;
1556 struct slist *s, *s2;
1561 s = new_stmt(BPF_LD|BPF_ABS|size);
1566 s = gen_load_llrel(offset, size);
1570 s = gen_load_macplrel(offset, size);
1574 s = gen_load_macplrel(off_nl + offset, size);
1578 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1583 * Load the X register with the length of the IPv4 header
1584 * (plus the offset of the link-layer header, if it's
1585 * preceded by a variable-length header such as a radio
1586 * header), in bytes.
1588 s = gen_loadx_iphdrlen();
1591 * Load the item at {offset of the MAC-layer payload} +
1592 * {offset, relative to the start of the MAC-layer
1593 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1594 * {specified offset}.
1596 * (If the offset of the MAC-layer payload is variable,
1597 * it's included in the value in the X register, and
1600 s2 = new_stmt(BPF_LD|BPF_IND|size);
1601 s2->s.k = off_macpl + off_nl + offset;
1606 s = gen_load_macplrel(off_nl + 40 + offset, size);
1617 * Generate code to load into the X register the sum of the length of
1618 * the IPv4 header and any variable-length header preceding the link-layer
1621 static struct slist *
1622 gen_loadx_iphdrlen()
1624 struct slist *s, *s2;
1626 s = gen_off_macpl();
1629 * There's a variable-length prefix preceding the
1630 * link-layer header, or the link-layer header is itself
1631 * variable-length. "s" points to a list of statements
1632 * that put the offset of the MAC-layer payload into
1635 * The 4*([k]&0xf) addressing mode can't be used, as we
1636 * don't have a constant offset, so we have to load the
1637 * value in question into the A register and add to it
1638 * the value from the X register.
1640 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1643 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1646 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1651 * The A register now contains the length of the
1652 * IP header. We need to add to it the offset of
1653 * the MAC-layer payload, which is still in the X
1654 * register, and move the result into the X register.
1656 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1657 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1660 * There is no variable-length header preceding the
1661 * link-layer header, and the link-layer header is
1662 * fixed-length; load the length of the IPv4 header,
1663 * which is at an offset of off_nl from the beginning
1664 * of the MAC-layer payload, and thus at an offset
1665 * of off_mac_pl + off_nl from the beginning of the
1668 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1669 s->s.k = off_macpl + off_nl;
1674 static struct block *
1681 s = new_stmt(BPF_LD|BPF_IMM);
1683 b = new_block(JMP(BPF_JEQ));
1689 static inline struct block *
1692 return gen_uncond(1);
1695 static inline struct block *
1698 return gen_uncond(0);
1702 * Byte-swap a 32-bit number.
1703 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1704 * big-endian platforms.)
1706 #define SWAPLONG(y) \
1707 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1710 * Generate code to match a particular packet type.
1712 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1713 * value, if <= ETHERMTU. We use that to determine whether to
1714 * match the type/length field or to check the type/length field for
1715 * a value <= ETHERMTU to see whether it's a type field and then do
1716 * the appropriate test.
1718 static struct block *
1719 gen_ether_linktype(proto)
1722 struct block *b0, *b1;
1728 case LLCSAP_NETBEUI:
1730 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1731 * so we check the DSAP and SSAP.
1733 * LLCSAP_IP checks for IP-over-802.2, rather
1734 * than IP-over-Ethernet or IP-over-SNAP.
1736 * XXX - should we check both the DSAP and the
1737 * SSAP, like this, or should we check just the
1738 * DSAP, as we do for other types <= ETHERMTU
1739 * (i.e., other SAP values)?
1741 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1743 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1744 ((proto << 8) | proto));
1752 * Ethernet_II frames, which are Ethernet
1753 * frames with a frame type of ETHERTYPE_IPX;
1755 * Ethernet_802.3 frames, which are 802.3
1756 * frames (i.e., the type/length field is
1757 * a length field, <= ETHERMTU, rather than
1758 * a type field) with the first two bytes
1759 * after the Ethernet/802.3 header being
1762 * Ethernet_802.2 frames, which are 802.3
1763 * frames with an 802.2 LLC header and
1764 * with the IPX LSAP as the DSAP in the LLC
1767 * Ethernet_SNAP frames, which are 802.3
1768 * frames with an LLC header and a SNAP
1769 * header and with an OUI of 0x000000
1770 * (encapsulated Ethernet) and a protocol
1771 * ID of ETHERTYPE_IPX in the SNAP header.
1773 * XXX - should we generate the same code both
1774 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1778 * This generates code to check both for the
1779 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1781 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1782 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1786 * Now we add code to check for SNAP frames with
1787 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1789 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1793 * Now we generate code to check for 802.3
1794 * frames in general.
1796 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1800 * Now add the check for 802.3 frames before the
1801 * check for Ethernet_802.2 and Ethernet_802.3,
1802 * as those checks should only be done on 802.3
1803 * frames, not on Ethernet frames.
1808 * Now add the check for Ethernet_II frames, and
1809 * do that before checking for the other frame
1812 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1813 (bpf_int32)ETHERTYPE_IPX);
1817 case ETHERTYPE_ATALK:
1818 case ETHERTYPE_AARP:
1820 * EtherTalk (AppleTalk protocols on Ethernet link
1821 * layer) may use 802.2 encapsulation.
1825 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1826 * we check for an Ethernet type field less than
1827 * 1500, which means it's an 802.3 length field.
1829 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1833 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1834 * SNAP packets with an organization code of
1835 * 0x080007 (Apple, for Appletalk) and a protocol
1836 * type of ETHERTYPE_ATALK (Appletalk).
1838 * 802.2-encapsulated ETHERTYPE_AARP packets are
1839 * SNAP packets with an organization code of
1840 * 0x000000 (encapsulated Ethernet) and a protocol
1841 * type of ETHERTYPE_AARP (Appletalk ARP).
1843 if (proto == ETHERTYPE_ATALK)
1844 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1845 else /* proto == ETHERTYPE_AARP */
1846 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1850 * Check for Ethernet encapsulation (Ethertalk
1851 * phase 1?); we just check for the Ethernet
1854 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1860 if (proto <= ETHERMTU) {
1862 * This is an LLC SAP value, so the frames
1863 * that match would be 802.2 frames.
1864 * Check that the frame is an 802.2 frame
1865 * (i.e., that the length/type field is
1866 * a length field, <= ETHERMTU) and
1867 * then check the DSAP.
1869 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1871 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1877 * This is an Ethernet type, so compare
1878 * the length/type field with it (if
1879 * the frame is an 802.2 frame, the length
1880 * field will be <= ETHERMTU, and, as
1881 * "proto" is > ETHERMTU, this test
1882 * will fail and the frame won't match,
1883 * which is what we want).
1885 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1892 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1893 * or IPv6 then we have an error.
1895 static struct block *
1896 gen_ipnet_linktype(proto)
1902 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1903 (bpf_int32)IPH_AF_INET);
1906 case ETHERTYPE_IPV6:
1907 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1908 (bpf_int32)IPH_AF_INET6);
1919 * Generate code to match a particular packet type.
1921 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1922 * value, if <= ETHERMTU. We use that to determine whether to
1923 * match the type field or to check the type field for the special
1924 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1926 static struct block *
1927 gen_linux_sll_linktype(proto)
1930 struct block *b0, *b1;
1936 case LLCSAP_NETBEUI:
1938 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1939 * so we check the DSAP and SSAP.
1941 * LLCSAP_IP checks for IP-over-802.2, rather
1942 * than IP-over-Ethernet or IP-over-SNAP.
1944 * XXX - should we check both the DSAP and the
1945 * SSAP, like this, or should we check just the
1946 * DSAP, as we do for other types <= ETHERMTU
1947 * (i.e., other SAP values)?
1949 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1950 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1951 ((proto << 8) | proto));
1957 * Ethernet_II frames, which are Ethernet
1958 * frames with a frame type of ETHERTYPE_IPX;
1960 * Ethernet_802.3 frames, which have a frame
1961 * type of LINUX_SLL_P_802_3;
1963 * Ethernet_802.2 frames, which are 802.3
1964 * frames with an 802.2 LLC header (i.e, have
1965 * a frame type of LINUX_SLL_P_802_2) and
1966 * with the IPX LSAP as the DSAP in the LLC
1969 * Ethernet_SNAP frames, which are 802.3
1970 * frames with an LLC header and a SNAP
1971 * header and with an OUI of 0x000000
1972 * (encapsulated Ethernet) and a protocol
1973 * ID of ETHERTYPE_IPX in the SNAP header.
1975 * First, do the checks on LINUX_SLL_P_802_2
1976 * frames; generate the check for either
1977 * Ethernet_802.2 or Ethernet_SNAP frames, and
1978 * then put a check for LINUX_SLL_P_802_2 frames
1981 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1982 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
1984 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1988 * Now check for 802.3 frames and OR that with
1989 * the previous test.
1991 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1995 * Now add the check for Ethernet_II frames, and
1996 * do that before checking for the other frame
1999 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2000 (bpf_int32)ETHERTYPE_IPX);
2004 case ETHERTYPE_ATALK:
2005 case ETHERTYPE_AARP:
2007 * EtherTalk (AppleTalk protocols on Ethernet link
2008 * layer) may use 802.2 encapsulation.
2012 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2013 * we check for the 802.2 protocol type in the
2014 * "Ethernet type" field.
2016 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2019 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2020 * SNAP packets with an organization code of
2021 * 0x080007 (Apple, for Appletalk) and a protocol
2022 * type of ETHERTYPE_ATALK (Appletalk).
2024 * 802.2-encapsulated ETHERTYPE_AARP packets are
2025 * SNAP packets with an organization code of
2026 * 0x000000 (encapsulated Ethernet) and a protocol
2027 * type of ETHERTYPE_AARP (Appletalk ARP).
2029 if (proto == ETHERTYPE_ATALK)
2030 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2031 else /* proto == ETHERTYPE_AARP */
2032 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2036 * Check for Ethernet encapsulation (Ethertalk
2037 * phase 1?); we just check for the Ethernet
2040 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2046 if (proto <= ETHERMTU) {
2048 * This is an LLC SAP value, so the frames
2049 * that match would be 802.2 frames.
2050 * Check for the 802.2 protocol type
2051 * in the "Ethernet type" field, and
2052 * then check the DSAP.
2054 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2056 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2062 * This is an Ethernet type, so compare
2063 * the length/type field with it (if
2064 * the frame is an 802.2 frame, the length
2065 * field will be <= ETHERMTU, and, as
2066 * "proto" is > ETHERMTU, this test
2067 * will fail and the frame won't match,
2068 * which is what we want).
2070 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2076 static struct slist *
2077 gen_load_prism_llprefixlen()
2079 struct slist *s1, *s2;
2080 struct slist *sjeq_avs_cookie;
2081 struct slist *sjcommon;
2084 * This code is not compatible with the optimizer, as
2085 * we are generating jmp instructions within a normal
2086 * slist of instructions
2091 * Generate code to load the length of the radio header into
2092 * the register assigned to hold that length, if one has been
2093 * assigned. (If one hasn't been assigned, no code we've
2094 * generated uses that prefix, so we don't need to generate any
2097 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2098 * or always use the AVS header rather than the Prism header.
2099 * We load a 4-byte big-endian value at the beginning of the
2100 * raw packet data, and see whether, when masked with 0xFFFFF000,
2101 * it's equal to 0x80211000. If so, that indicates that it's
2102 * an AVS header (the masked-out bits are the version number).
2103 * Otherwise, it's a Prism header.
2105 * XXX - the Prism header is also, in theory, variable-length,
2106 * but no known software generates headers that aren't 144
2109 if (reg_off_ll != -1) {
2113 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2117 * AND it with 0xFFFFF000.
2119 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2120 s2->s.k = 0xFFFFF000;
2124 * Compare with 0x80211000.
2126 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2127 sjeq_avs_cookie->s.k = 0x80211000;
2128 sappend(s1, sjeq_avs_cookie);
2133 * The 4 bytes at an offset of 4 from the beginning of
2134 * the AVS header are the length of the AVS header.
2135 * That field is big-endian.
2137 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2140 sjeq_avs_cookie->s.jt = s2;
2143 * Now jump to the code to allocate a register
2144 * into which to save the header length and
2145 * store the length there. (The "jump always"
2146 * instruction needs to have the k field set;
2147 * it's added to the PC, so, as we're jumping
2148 * over a single instruction, it should be 1.)
2150 sjcommon = new_stmt(JMP(BPF_JA));
2152 sappend(s1, sjcommon);
2155 * Now for the code that handles the Prism header.
2156 * Just load the length of the Prism header (144)
2157 * into the A register. Have the test for an AVS
2158 * header branch here if we don't have an AVS header.
2160 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2163 sjeq_avs_cookie->s.jf = s2;
2166 * Now allocate a register to hold that value and store
2167 * it. The code for the AVS header will jump here after
2168 * loading the length of the AVS header.
2170 s2 = new_stmt(BPF_ST);
2171 s2->s.k = reg_off_ll;
2173 sjcommon->s.jf = s2;
2176 * Now move it into the X register.
2178 s2 = new_stmt(BPF_MISC|BPF_TAX);
2186 static struct slist *
2187 gen_load_avs_llprefixlen()
2189 struct slist *s1, *s2;
2192 * Generate code to load the length of the AVS header into
2193 * the register assigned to hold that length, if one has been
2194 * assigned. (If one hasn't been assigned, no code we've
2195 * generated uses that prefix, so we don't need to generate any
2198 if (reg_off_ll != -1) {
2200 * The 4 bytes at an offset of 4 from the beginning of
2201 * the AVS header are the length of the AVS header.
2202 * That field is big-endian.
2204 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2208 * Now allocate a register to hold that value and store
2211 s2 = new_stmt(BPF_ST);
2212 s2->s.k = reg_off_ll;
2216 * Now move it into the X register.
2218 s2 = new_stmt(BPF_MISC|BPF_TAX);
2226 static struct slist *
2227 gen_load_radiotap_llprefixlen()
2229 struct slist *s1, *s2;
2232 * Generate code to load the length of the radiotap header into
2233 * the register assigned to hold that length, if one has been
2234 * assigned. (If one hasn't been assigned, no code we've
2235 * generated uses that prefix, so we don't need to generate any
2238 if (reg_off_ll != -1) {
2240 * The 2 bytes at offsets of 2 and 3 from the beginning
2241 * of the radiotap header are the length of the radiotap
2242 * header; unfortunately, it's little-endian, so we have
2243 * to load it a byte at a time and construct the value.
2247 * Load the high-order byte, at an offset of 3, shift it
2248 * left a byte, and put the result in the X register.
2250 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2252 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2255 s2 = new_stmt(BPF_MISC|BPF_TAX);
2259 * Load the next byte, at an offset of 2, and OR the
2260 * value from the X register into it.
2262 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2265 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2269 * Now allocate a register to hold that value and store
2272 s2 = new_stmt(BPF_ST);
2273 s2->s.k = reg_off_ll;
2277 * Now move it into the X register.
2279 s2 = new_stmt(BPF_MISC|BPF_TAX);
2288 * At the moment we treat PPI as normal Radiotap encoded
2289 * packets. The difference is in the function that generates
2290 * the code at the beginning to compute the header length.
2291 * Since this code generator of PPI supports bare 802.11
2292 * encapsulation only (i.e. the encapsulated DLT should be
2293 * DLT_IEEE802_11) we generate code to check for this too;
2294 * that's done in finish_parse().
2296 static struct slist *
2297 gen_load_ppi_llprefixlen()
2299 struct slist *s1, *s2;
2302 * Generate code to load the length of the radiotap header
2303 * into the register assigned to hold that length, if one has
2306 if (reg_off_ll != -1) {
2308 * The 2 bytes at offsets of 2 and 3 from the beginning
2309 * of the radiotap header are the length of the radiotap
2310 * header; unfortunately, it's little-endian, so we have
2311 * to load it a byte at a time and construct the value.
2315 * Load the high-order byte, at an offset of 3, shift it
2316 * left a byte, and put the result in the X register.
2318 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2320 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2323 s2 = new_stmt(BPF_MISC|BPF_TAX);
2327 * Load the next byte, at an offset of 2, and OR the
2328 * value from the X register into it.
2330 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2333 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2337 * Now allocate a register to hold that value and store
2340 s2 = new_stmt(BPF_ST);
2341 s2->s.k = reg_off_ll;
2345 * Now move it into the X register.
2347 s2 = new_stmt(BPF_MISC|BPF_TAX);
2356 * Load a value relative to the beginning of the link-layer header after the 802.11
2357 * header, i.e. LLC_SNAP.
2358 * The link-layer header doesn't necessarily begin at the beginning
2359 * of the packet data; there might be a variable-length prefix containing
2360 * radio information.
2362 static struct slist *
2363 gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2366 struct slist *sjset_data_frame_1;
2367 struct slist *sjset_data_frame_2;
2368 struct slist *sjset_qos;
2369 struct slist *sjset_radiotap_flags;
2370 struct slist *sjset_radiotap_tsft;
2371 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2372 struct slist *s_roundup;
2374 if (reg_off_macpl == -1) {
2376 * No register has been assigned to the offset of
2377 * the MAC-layer payload, which means nobody needs
2378 * it; don't bother computing it - just return
2379 * what we already have.
2385 * This code is not compatible with the optimizer, as
2386 * we are generating jmp instructions within a normal
2387 * slist of instructions
2392 * If "s" is non-null, it has code to arrange that the X register
2393 * contains the length of the prefix preceding the link-layer
2396 * Otherwise, the length of the prefix preceding the link-layer
2397 * header is "off_ll".
2401 * There is no variable-length header preceding the
2402 * link-layer header.
2404 * Load the length of the fixed-length prefix preceding
2405 * the link-layer header (if any) into the X register,
2406 * and store it in the reg_off_macpl register.
2407 * That length is off_ll.
2409 s = new_stmt(BPF_LDX|BPF_IMM);
2414 * The X register contains the offset of the beginning of the
2415 * link-layer header; add 24, which is the minimum length
2416 * of the MAC header for a data frame, to that, and store it
2417 * in reg_off_macpl, and then load the Frame Control field,
2418 * which is at the offset in the X register, with an indexed load.
2420 s2 = new_stmt(BPF_MISC|BPF_TXA);
2422 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2425 s2 = new_stmt(BPF_ST);
2426 s2->s.k = reg_off_macpl;
2429 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2434 * Check the Frame Control field to see if this is a data frame;
2435 * a data frame has the 0x08 bit (b3) in that field set and the
2436 * 0x04 bit (b2) clear.
2438 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2439 sjset_data_frame_1->s.k = 0x08;
2440 sappend(s, sjset_data_frame_1);
2443 * If b3 is set, test b2, otherwise go to the first statement of
2444 * the rest of the program.
2446 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2447 sjset_data_frame_2->s.k = 0x04;
2448 sappend(s, sjset_data_frame_2);
2449 sjset_data_frame_1->s.jf = snext;
2452 * If b2 is not set, this is a data frame; test the QoS bit.
2453 * Otherwise, go to the first statement of the rest of the
2456 sjset_data_frame_2->s.jt = snext;
2457 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2458 sjset_qos->s.k = 0x80; /* QoS bit */
2459 sappend(s, sjset_qos);
2462 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2464 * Otherwise, go to the first statement of the rest of the
2467 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2468 s2->s.k = reg_off_macpl;
2470 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2473 s2 = new_stmt(BPF_ST);
2474 s2->s.k = reg_off_macpl;
2478 * If we have a radiotap header, look at it to see whether
2479 * there's Atheros padding between the MAC-layer header
2482 * Note: all of the fields in the radiotap header are
2483 * little-endian, so we byte-swap all of the values
2484 * we test against, as they will be loaded as big-endian
2487 if (linktype == DLT_IEEE802_11_RADIO) {
2489 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2490 * in the presence flag?
2492 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2496 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2497 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2498 sappend(s, sjset_radiotap_flags);
2501 * If not, skip all of this.
2503 sjset_radiotap_flags->s.jf = snext;
2506 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2508 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2509 new_stmt(JMP(BPF_JSET));
2510 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2511 sappend(s, sjset_radiotap_tsft);
2514 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2515 * at an offset of 16 from the beginning of the raw packet
2516 * data (8 bytes for the radiotap header and 8 bytes for
2519 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2522 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2526 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2527 sjset_tsft_datapad->s.k = 0x20;
2528 sappend(s, sjset_tsft_datapad);
2531 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2532 * at an offset of 8 from the beginning of the raw packet
2533 * data (8 bytes for the radiotap header).
2535 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2538 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2542 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2543 sjset_notsft_datapad->s.k = 0x20;
2544 sappend(s, sjset_notsft_datapad);
2547 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2548 * set, round the length of the 802.11 header to
2549 * a multiple of 4. Do that by adding 3 and then
2550 * dividing by and multiplying by 4, which we do by
2553 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2554 s_roundup->s.k = reg_off_macpl;
2555 sappend(s, s_roundup);
2556 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2559 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2562 s2 = new_stmt(BPF_ST);
2563 s2->s.k = reg_off_macpl;
2566 sjset_tsft_datapad->s.jt = s_roundup;
2567 sjset_tsft_datapad->s.jf = snext;
2568 sjset_notsft_datapad->s.jt = s_roundup;
2569 sjset_notsft_datapad->s.jf = snext;
2571 sjset_qos->s.jf = snext;
2577 insert_compute_vloffsets(b)
2583 * For link-layer types that have a variable-length header
2584 * preceding the link-layer header, generate code to load
2585 * the offset of the link-layer header into the register
2586 * assigned to that offset, if any.
2590 case DLT_PRISM_HEADER:
2591 s = gen_load_prism_llprefixlen();
2594 case DLT_IEEE802_11_RADIO_AVS:
2595 s = gen_load_avs_llprefixlen();
2598 case DLT_IEEE802_11_RADIO:
2599 s = gen_load_radiotap_llprefixlen();
2603 s = gen_load_ppi_llprefixlen();
2612 * For link-layer types that have a variable-length link-layer
2613 * header, generate code to load the offset of the MAC-layer
2614 * payload into the register assigned to that offset, if any.
2618 case DLT_IEEE802_11:
2619 case DLT_PRISM_HEADER:
2620 case DLT_IEEE802_11_RADIO_AVS:
2621 case DLT_IEEE802_11_RADIO:
2623 s = gen_load_802_11_header_len(s, b->stmts);
2628 * If we have any offset-loading code, append all the
2629 * existing statements in the block to those statements,
2630 * and make the resulting list the list of statements
2634 sappend(s, b->stmts);
2639 static struct block *
2640 gen_ppi_dlt_check(void)
2642 struct slist *s_load_dlt;
2645 if (linktype == DLT_PPI)
2647 /* Create the statements that check for the DLT
2649 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2650 s_load_dlt->s.k = 4;
2652 b = new_block(JMP(BPF_JEQ));
2654 b->stmts = s_load_dlt;
2655 b->s.k = SWAPLONG(DLT_IEEE802_11);
2665 static struct slist *
2666 gen_prism_llprefixlen(void)
2670 if (reg_off_ll == -1) {
2672 * We haven't yet assigned a register for the length
2673 * of the radio header; allocate one.
2675 reg_off_ll = alloc_reg();
2679 * Load the register containing the radio length
2680 * into the X register.
2682 s = new_stmt(BPF_LDX|BPF_MEM);
2683 s->s.k = reg_off_ll;
2687 static struct slist *
2688 gen_avs_llprefixlen(void)
2692 if (reg_off_ll == -1) {
2694 * We haven't yet assigned a register for the length
2695 * of the AVS header; allocate one.
2697 reg_off_ll = alloc_reg();
2701 * Load the register containing the AVS length
2702 * into the X register.
2704 s = new_stmt(BPF_LDX|BPF_MEM);
2705 s->s.k = reg_off_ll;
2709 static struct slist *
2710 gen_radiotap_llprefixlen(void)
2714 if (reg_off_ll == -1) {
2716 * We haven't yet assigned a register for the length
2717 * of the radiotap header; allocate one.
2719 reg_off_ll = alloc_reg();
2723 * Load the register containing the radiotap length
2724 * into the X register.
2726 s = new_stmt(BPF_LDX|BPF_MEM);
2727 s->s.k = reg_off_ll;
2732 * At the moment we treat PPI as normal Radiotap encoded
2733 * packets. The difference is in the function that generates
2734 * the code at the beginning to compute the header length.
2735 * Since this code generator of PPI supports bare 802.11
2736 * encapsulation only (i.e. the encapsulated DLT should be
2737 * DLT_IEEE802_11) we generate code to check for this too.
2739 static struct slist *
2740 gen_ppi_llprefixlen(void)
2744 if (reg_off_ll == -1) {
2746 * We haven't yet assigned a register for the length
2747 * of the radiotap header; allocate one.
2749 reg_off_ll = alloc_reg();
2753 * Load the register containing the PPI length
2754 * into the X register.
2756 s = new_stmt(BPF_LDX|BPF_MEM);
2757 s->s.k = reg_off_ll;
2762 * Generate code to compute the link-layer header length, if necessary,
2763 * putting it into the X register, and to return either a pointer to a
2764 * "struct slist" for the list of statements in that code, or NULL if
2765 * no code is necessary.
2767 static struct slist *
2768 gen_llprefixlen(void)
2772 case DLT_PRISM_HEADER:
2773 return gen_prism_llprefixlen();
2775 case DLT_IEEE802_11_RADIO_AVS:
2776 return gen_avs_llprefixlen();
2778 case DLT_IEEE802_11_RADIO:
2779 return gen_radiotap_llprefixlen();
2782 return gen_ppi_llprefixlen();
2790 * Generate code to load the register containing the offset of the
2791 * MAC-layer payload into the X register; if no register for that offset
2792 * has been allocated, allocate it first.
2794 static struct slist *
2799 if (off_macpl_is_variable) {
2800 if (reg_off_macpl == -1) {
2802 * We haven't yet assigned a register for the offset
2803 * of the MAC-layer payload; allocate one.
2805 reg_off_macpl = alloc_reg();
2809 * Load the register containing the offset of the MAC-layer
2810 * payload into the X register.
2812 s = new_stmt(BPF_LDX|BPF_MEM);
2813 s->s.k = reg_off_macpl;
2817 * That offset isn't variable, so we don't need to
2818 * generate any code.
2825 * Map an Ethernet type to the equivalent PPP type.
2828 ethertype_to_ppptype(proto)
2838 case ETHERTYPE_IPV6:
2847 case ETHERTYPE_ATALK:
2861 * I'm assuming the "Bridging PDU"s that go
2862 * over PPP are Spanning Tree Protocol
2876 * Generate code to match a particular packet type by matching the
2877 * link-layer type field or fields in the 802.2 LLC header.
2879 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2880 * value, if <= ETHERMTU.
2882 static struct block *
2886 struct block *b0, *b1, *b2;
2888 /* are we checking MPLS-encapsulated packets? */
2889 if (label_stack_depth > 0) {
2893 /* FIXME add other L3 proto IDs */
2894 return gen_mpls_linktype(Q_IP);
2896 case ETHERTYPE_IPV6:
2898 /* FIXME add other L3 proto IDs */
2899 return gen_mpls_linktype(Q_IPV6);
2902 bpf_error("unsupported protocol over mpls");
2908 * Are we testing PPPoE packets?
2912 * The PPPoE session header is part of the
2913 * MAC-layer payload, so all references
2914 * should be relative to the beginning of
2919 * We use Ethernet protocol types inside libpcap;
2920 * map them to the corresponding PPP protocol types.
2922 proto = ethertype_to_ppptype(proto);
2923 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2929 case DLT_NETANALYZER:
2930 case DLT_NETANALYZER_TRANSPARENT:
2931 return gen_ether_linktype(proto);
2939 proto = (proto << 8 | LLCSAP_ISONS);
2943 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2950 case DLT_IEEE802_11:
2951 case DLT_PRISM_HEADER:
2952 case DLT_IEEE802_11_RADIO_AVS:
2953 case DLT_IEEE802_11_RADIO:
2956 * Check that we have a data frame.
2958 b0 = gen_check_802_11_data_frame();
2961 * Now check for the specified link-layer type.
2963 b1 = gen_llc_linktype(proto);
2971 * XXX - check for asynchronous frames, as per RFC 1103.
2973 return gen_llc_linktype(proto);
2979 * XXX - check for LLC PDUs, as per IEEE 802.5.
2981 return gen_llc_linktype(proto);
2985 case DLT_ATM_RFC1483:
2987 case DLT_IP_OVER_FC:
2988 return gen_llc_linktype(proto);
2994 * If "is_lane" is set, check for a LANE-encapsulated
2995 * version of this protocol, otherwise check for an
2996 * LLC-encapsulated version of this protocol.
2998 * We assume LANE means Ethernet, not Token Ring.
3002 * Check that the packet doesn't begin with an
3003 * LE Control marker. (We've already generated
3006 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3011 * Now generate an Ethernet test.
3013 b1 = gen_ether_linktype(proto);
3018 * Check for LLC encapsulation and then check the
3021 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3022 b1 = gen_llc_linktype(proto);
3030 return gen_linux_sll_linktype(proto);
3035 case DLT_SLIP_BSDOS:
3038 * These types don't provide any type field; packets
3039 * are always IPv4 or IPv6.
3041 * XXX - for IPv4, check for a version number of 4, and,
3042 * for IPv6, check for a version number of 6?
3047 /* Check for a version number of 4. */
3048 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3050 case ETHERTYPE_IPV6:
3051 /* Check for a version number of 6. */
3052 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3056 return gen_false(); /* always false */
3063 * Raw IPv4, so no type field.
3065 if (proto == ETHERTYPE_IP)
3066 return gen_true(); /* always true */
3068 /* Checking for something other than IPv4; always false */
3075 * Raw IPv6, so no type field.
3078 if (proto == ETHERTYPE_IPV6)
3079 return gen_true(); /* always true */
3082 /* Checking for something other than IPv6; always false */
3089 case DLT_PPP_SERIAL:
3092 * We use Ethernet protocol types inside libpcap;
3093 * map them to the corresponding PPP protocol types.
3095 proto = ethertype_to_ppptype(proto);
3096 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3102 * We use Ethernet protocol types inside libpcap;
3103 * map them to the corresponding PPP protocol types.
3109 * Also check for Van Jacobson-compressed IP.
3110 * XXX - do this for other forms of PPP?
3112 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3113 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3115 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3120 proto = ethertype_to_ppptype(proto);
3121 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3131 * For DLT_NULL, the link-layer header is a 32-bit
3132 * word containing an AF_ value in *host* byte order,
3133 * and for DLT_ENC, the link-layer header begins
3134 * with a 32-bit work containing an AF_ value in
3137 * In addition, if we're reading a saved capture file,
3138 * the host byte order in the capture may not be the
3139 * same as the host byte order on this machine.
3141 * For DLT_LOOP, the link-layer header is a 32-bit
3142 * word containing an AF_ value in *network* byte order.
3144 * XXX - AF_ values may, unfortunately, be platform-
3145 * dependent; for example, FreeBSD's AF_INET6 is 24
3146 * whilst NetBSD's and OpenBSD's is 26.
3148 * This means that, when reading a capture file, just
3149 * checking for our AF_INET6 value won't work if the
3150 * capture file came from another OS.
3159 case ETHERTYPE_IPV6:
3166 * Not a type on which we support filtering.
3167 * XXX - support those that have AF_ values
3168 * #defined on this platform, at least?
3173 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3175 * The AF_ value is in host byte order, but
3176 * the BPF interpreter will convert it to
3177 * network byte order.
3179 * If this is a save file, and it's from a
3180 * machine with the opposite byte order to
3181 * ours, we byte-swap the AF_ value.
3183 * Then we run it through "htonl()", and
3184 * generate code to compare against the result.
3186 if (bpf_pcap->sf.rfile != NULL &&
3187 bpf_pcap->sf.swapped)
3188 proto = SWAPLONG(proto);
3189 proto = htonl(proto);
3191 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3193 #ifdef HAVE_NET_PFVAR_H
3196 * af field is host byte order in contrast to the rest of
3199 if (proto == ETHERTYPE_IP)
3200 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3201 BPF_B, (bpf_int32)AF_INET));
3203 else if (proto == ETHERTYPE_IPV6)
3204 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3205 BPF_B, (bpf_int32)AF_INET6));
3211 #endif /* HAVE_NET_PFVAR_H */
3214 case DLT_ARCNET_LINUX:
3216 * XXX should we check for first fragment if the protocol
3225 case ETHERTYPE_IPV6:
3226 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3227 (bpf_int32)ARCTYPE_INET6));
3231 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3232 (bpf_int32)ARCTYPE_IP);
3233 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3234 (bpf_int32)ARCTYPE_IP_OLD);
3239 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3240 (bpf_int32)ARCTYPE_ARP);
3241 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3242 (bpf_int32)ARCTYPE_ARP_OLD);
3246 case ETHERTYPE_REVARP:
3247 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3248 (bpf_int32)ARCTYPE_REVARP));
3250 case ETHERTYPE_ATALK:
3251 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3252 (bpf_int32)ARCTYPE_ATALK));
3259 case ETHERTYPE_ATALK:
3269 * XXX - assumes a 2-byte Frame Relay header with
3270 * DLCI and flags. What if the address is longer?
3276 * Check for the special NLPID for IP.
3278 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3281 case ETHERTYPE_IPV6:
3283 * Check for the special NLPID for IPv6.
3285 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3290 * Check for several OSI protocols.
3292 * Frame Relay packets typically have an OSI
3293 * NLPID at the beginning; we check for each
3296 * What we check for is the NLPID and a frame
3297 * control field of UI, i.e. 0x03 followed
3300 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3301 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3302 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3314 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3316 case DLT_JUNIPER_MFR:
3317 case DLT_JUNIPER_MLFR:
3318 case DLT_JUNIPER_MLPPP:
3319 case DLT_JUNIPER_ATM1:
3320 case DLT_JUNIPER_ATM2:
3321 case DLT_JUNIPER_PPPOE:
3322 case DLT_JUNIPER_PPPOE_ATM:
3323 case DLT_JUNIPER_GGSN:
3324 case DLT_JUNIPER_ES:
3325 case DLT_JUNIPER_MONITOR:
3326 case DLT_JUNIPER_SERVICES:
3327 case DLT_JUNIPER_ETHER:
3328 case DLT_JUNIPER_PPP:
3329 case DLT_JUNIPER_FRELAY:
3330 case DLT_JUNIPER_CHDLC:
3331 case DLT_JUNIPER_VP:
3332 case DLT_JUNIPER_ST:
3333 case DLT_JUNIPER_ISM:
3334 case DLT_JUNIPER_VS:
3335 case DLT_JUNIPER_SRX_E2E:
3336 case DLT_JUNIPER_FIBRECHANNEL:
3337 case DLT_JUNIPER_ATM_CEMIC:
3339 /* just lets verify the magic number for now -
3340 * on ATM we may have up to 6 different encapsulations on the wire
3341 * and need a lot of heuristics to figure out that the payload
3344 * FIXME encapsulation specific BPF_ filters
3346 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3349 return gen_ipnet_linktype(proto);
3351 case DLT_LINUX_IRDA:
3352 bpf_error("IrDA link-layer type filtering not implemented");
3355 bpf_error("DOCSIS link-layer type filtering not implemented");
3358 case DLT_MTP2_WITH_PHDR:
3359 bpf_error("MTP2 link-layer type filtering not implemented");
3362 bpf_error("ERF link-layer type filtering not implemented");
3365 bpf_error("PFSYNC link-layer type filtering not implemented");
3367 case DLT_LINUX_LAPD:
3368 bpf_error("LAPD link-layer type filtering not implemented");
3372 case DLT_USB_LINUX_MMAPPED:
3373 bpf_error("USB link-layer type filtering not implemented");
3375 case DLT_BLUETOOTH_HCI_H4:
3376 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3377 bpf_error("Bluetooth link-layer type filtering not implemented");
3380 case DLT_CAN_SOCKETCAN:
3381 bpf_error("CAN link-layer type filtering not implemented");
3383 case DLT_IEEE802_15_4:
3384 case DLT_IEEE802_15_4_LINUX:
3385 case DLT_IEEE802_15_4_NONASK_PHY:
3386 case DLT_IEEE802_15_4_NOFCS:
3387 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3389 case DLT_IEEE802_16_MAC_CPS_RADIO:
3390 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3393 bpf_error("SITA link-layer type filtering not implemented");
3396 bpf_error("RAIF1 link-layer type filtering not implemented");
3399 bpf_error("IPMB link-layer type filtering not implemented");
3402 bpf_error("AX.25 link-layer type filtering not implemented");
3406 * All the types that have no encapsulation should either be
3407 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3408 * all packets are IP packets, or should be handled in some
3409 * special case, if none of them are (if some are and some
3410 * aren't, the lack of encapsulation is a problem, as we'd
3411 * have to find some other way of determining the packet type).
3413 * Therefore, if "off_linktype" is -1, there's an error.
3415 if (off_linktype == (u_int)-1)
3419 * Any type not handled above should always have an Ethernet
3420 * type at an offset of "off_linktype".
3422 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3426 * Check for an LLC SNAP packet with a given organization code and
3427 * protocol type; we check the entire contents of the 802.2 LLC and
3428 * snap headers, checking for DSAP and SSAP of SNAP and a control
3429 * field of 0x03 in the LLC header, and for the specified organization
3430 * code and protocol type in the SNAP header.
3432 static struct block *
3433 gen_snap(orgcode, ptype)
3434 bpf_u_int32 orgcode;
3437 u_char snapblock[8];
3439 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3440 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3441 snapblock[2] = 0x03; /* control = UI */
3442 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3443 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3444 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3445 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3446 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3447 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3451 * Generate code to match a particular packet type, for link-layer types
3452 * using 802.2 LLC headers.
3454 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3455 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3457 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3458 * value, if <= ETHERMTU. We use that to determine whether to
3459 * match the DSAP or both DSAP and LSAP or to check the OUI and
3460 * protocol ID in a SNAP header.
3462 static struct block *
3463 gen_llc_linktype(proto)
3467 * XXX - handle token-ring variable-length header.
3473 case LLCSAP_NETBEUI:
3475 * XXX - should we check both the DSAP and the
3476 * SSAP, like this, or should we check just the
3477 * DSAP, as we do for other types <= ETHERMTU
3478 * (i.e., other SAP values)?
3480 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3481 ((proto << 8) | proto));
3485 * XXX - are there ever SNAP frames for IPX on
3486 * non-Ethernet 802.x networks?
3488 return gen_cmp(OR_MACPL, 0, BPF_B,
3489 (bpf_int32)LLCSAP_IPX);
3491 case ETHERTYPE_ATALK:
3493 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3494 * SNAP packets with an organization code of
3495 * 0x080007 (Apple, for Appletalk) and a protocol
3496 * type of ETHERTYPE_ATALK (Appletalk).
3498 * XXX - check for an organization code of
3499 * encapsulated Ethernet as well?
3501 return gen_snap(0x080007, ETHERTYPE_ATALK);
3505 * XXX - we don't have to check for IPX 802.3
3506 * here, but should we check for the IPX Ethertype?
3508 if (proto <= ETHERMTU) {
3510 * This is an LLC SAP value, so check
3513 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3516 * This is an Ethernet type; we assume that it's
3517 * unlikely that it'll appear in the right place
3518 * at random, and therefore check only the
3519 * location that would hold the Ethernet type
3520 * in a SNAP frame with an organization code of
3521 * 0x000000 (encapsulated Ethernet).
3523 * XXX - if we were to check for the SNAP DSAP and
3524 * LSAP, as per XXX, and were also to check for an
3525 * organization code of 0x000000 (encapsulated
3526 * Ethernet), we'd do
3528 * return gen_snap(0x000000, proto);
3530 * here; for now, we don't, as per the above.
3531 * I don't know whether it's worth the extra CPU
3532 * time to do the right check or not.
3534 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3539 static struct block *
3540 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3544 u_int src_off, dst_off;
3546 struct block *b0, *b1;
3560 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3561 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3567 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3568 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3575 b0 = gen_linktype(proto);
3576 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3582 static struct block *
3583 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3584 struct in6_addr *addr;
3585 struct in6_addr *mask;
3587 u_int src_off, dst_off;
3589 struct block *b0, *b1;
3604 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3605 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3611 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3612 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3619 /* this order is important */
3620 a = (u_int32_t *)addr;
3621 m = (u_int32_t *)mask;
3622 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3623 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3625 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3627 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3629 b0 = gen_linktype(proto);
3635 static struct block *
3636 gen_ehostop(eaddr, dir)
3637 register const u_char *eaddr;
3640 register struct block *b0, *b1;
3644 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3647 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3650 b0 = gen_ehostop(eaddr, Q_SRC);
3651 b1 = gen_ehostop(eaddr, Q_DST);
3657 b0 = gen_ehostop(eaddr, Q_SRC);
3658 b1 = gen_ehostop(eaddr, Q_DST);
3663 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3667 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3671 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3675 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3679 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3683 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3691 * Like gen_ehostop, but for DLT_FDDI
3693 static struct block *
3694 gen_fhostop(eaddr, dir)
3695 register const u_char *eaddr;
3698 struct block *b0, *b1;
3703 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3705 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3710 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3712 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3716 b0 = gen_fhostop(eaddr, Q_SRC);
3717 b1 = gen_fhostop(eaddr, Q_DST);
3723 b0 = gen_fhostop(eaddr, Q_SRC);
3724 b1 = gen_fhostop(eaddr, Q_DST);
3729 bpf_error("'addr1' is only supported on 802.11");
3733 bpf_error("'addr2' is only supported on 802.11");
3737 bpf_error("'addr3' is only supported on 802.11");
3741 bpf_error("'addr4' is only supported on 802.11");
3745 bpf_error("'ra' is only supported on 802.11");
3749 bpf_error("'ta' is only supported on 802.11");
3757 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3759 static struct block *
3760 gen_thostop(eaddr, dir)
3761 register const u_char *eaddr;
3764 register struct block *b0, *b1;
3768 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3771 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3774 b0 = gen_thostop(eaddr, Q_SRC);
3775 b1 = gen_thostop(eaddr, Q_DST);
3781 b0 = gen_thostop(eaddr, Q_SRC);
3782 b1 = gen_thostop(eaddr, Q_DST);
3787 bpf_error("'addr1' is only supported on 802.11");
3791 bpf_error("'addr2' is only supported on 802.11");
3795 bpf_error("'addr3' is only supported on 802.11");
3799 bpf_error("'addr4' is only supported on 802.11");
3803 bpf_error("'ra' is only supported on 802.11");
3807 bpf_error("'ta' is only supported on 802.11");
3815 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3816 * various 802.11 + radio headers.
3818 static struct block *
3819 gen_wlanhostop(eaddr, dir)
3820 register const u_char *eaddr;
3823 register struct block *b0, *b1, *b2;
3824 register struct slist *s;
3826 #ifdef ENABLE_WLAN_FILTERING_PATCH
3829 * We need to disable the optimizer because the optimizer is buggy
3830 * and wipes out some LD instructions generated by the below
3831 * code to validate the Frame Control bits
3834 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3841 * For control frames, there is no SA.
3843 * For management frames, SA is at an
3844 * offset of 10 from the beginning of
3847 * For data frames, SA is at an offset
3848 * of 10 from the beginning of the packet
3849 * if From DS is clear, at an offset of
3850 * 16 from the beginning of the packet
3851 * if From DS is set and To DS is clear,
3852 * and an offset of 24 from the beginning
3853 * of the packet if From DS is set and To DS
3858 * Generate the tests to be done for data frames
3861 * First, check for To DS set, i.e. check "link[1] & 0x01".
3863 s = gen_load_a(OR_LINK, 1, BPF_B);
3864 b1 = new_block(JMP(BPF_JSET));
3865 b1->s.k = 0x01; /* To DS */
3869 * If To DS is set, the SA is at 24.
3871 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3875 * Now, check for To DS not set, i.e. check
3876 * "!(link[1] & 0x01)".
3878 s = gen_load_a(OR_LINK, 1, BPF_B);
3879 b2 = new_block(JMP(BPF_JSET));
3880 b2->s.k = 0x01; /* To DS */
3885 * If To DS is not set, the SA is at 16.
3887 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3891 * Now OR together the last two checks. That gives
3892 * the complete set of checks for data frames with
3898 * Now check for From DS being set, and AND that with
3899 * the ORed-together checks.
3901 s = gen_load_a(OR_LINK, 1, BPF_B);
3902 b1 = new_block(JMP(BPF_JSET));
3903 b1->s.k = 0x02; /* From DS */
3908 * Now check for data frames with From DS not set.
3910 s = gen_load_a(OR_LINK, 1, BPF_B);
3911 b2 = new_block(JMP(BPF_JSET));
3912 b2->s.k = 0x02; /* From DS */
3917 * If From DS isn't set, the SA is at 10.
3919 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3923 * Now OR together the checks for data frames with
3924 * From DS not set and for data frames with From DS
3925 * set; that gives the checks done for data frames.
3930 * Now check for a data frame.
3931 * I.e, check "link[0] & 0x08".
3933 s = gen_load_a(OR_LINK, 0, BPF_B);
3934 b1 = new_block(JMP(BPF_JSET));
3939 * AND that with the checks done for data frames.
3944 * If the high-order bit of the type value is 0, this
3945 * is a management frame.
3946 * I.e, check "!(link[0] & 0x08)".
3948 s = gen_load_a(OR_LINK, 0, BPF_B);
3949 b2 = new_block(JMP(BPF_JSET));
3955 * For management frames, the SA is at 10.
3957 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3961 * OR that with the checks done for data frames.
3962 * That gives the checks done for management and
3968 * If the low-order bit of the type value is 1,
3969 * this is either a control frame or a frame
3970 * with a reserved type, and thus not a
3973 * I.e., check "!(link[0] & 0x04)".
3975 s = gen_load_a(OR_LINK, 0, BPF_B);
3976 b1 = new_block(JMP(BPF_JSET));
3982 * AND that with the checks for data and management
3992 * For control frames, there is no DA.
3994 * For management frames, DA is at an
3995 * offset of 4 from the beginning of
3998 * For data frames, DA is at an offset
3999 * of 4 from the beginning of the packet
4000 * if To DS is clear and at an offset of
4001 * 16 from the beginning of the packet
4006 * Generate the tests to be done for data frames.
4008 * First, check for To DS set, i.e. "link[1] & 0x01".
4010 s = gen_load_a(OR_LINK, 1, BPF_B);
4011 b1 = new_block(JMP(BPF_JSET));
4012 b1->s.k = 0x01; /* To DS */
4016 * If To DS is set, the DA is at 16.
4018 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4022 * Now, check for To DS not set, i.e. check
4023 * "!(link[1] & 0x01)".
4025 s = gen_load_a(OR_LINK, 1, BPF_B);
4026 b2 = new_block(JMP(BPF_JSET));
4027 b2->s.k = 0x01; /* To DS */
4032 * If To DS is not set, the DA is at 4.
4034 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4038 * Now OR together the last two checks. That gives
4039 * the complete set of checks for data frames.
4044 * Now check for a data frame.
4045 * I.e, check "link[0] & 0x08".
4047 s = gen_load_a(OR_LINK, 0, BPF_B);
4048 b1 = new_block(JMP(BPF_JSET));
4053 * AND that with the checks done for data frames.
4058 * If the high-order bit of the type value is 0, this
4059 * is a management frame.
4060 * I.e, check "!(link[0] & 0x08)".
4062 s = gen_load_a(OR_LINK, 0, BPF_B);
4063 b2 = new_block(JMP(BPF_JSET));
4069 * For management frames, the DA is at 4.
4071 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4075 * OR that with the checks done for data frames.
4076 * That gives the checks done for management and
4082 * If the low-order bit of the type value is 1,
4083 * this is either a control frame or a frame
4084 * with a reserved type, and thus not a
4087 * I.e., check "!(link[0] & 0x04)".
4089 s = gen_load_a(OR_LINK, 0, BPF_B);
4090 b1 = new_block(JMP(BPF_JSET));
4096 * AND that with the checks for data and management
4104 * Not present in management frames; addr1 in other
4109 * If the high-order bit of the type value is 0, this
4110 * is a management frame.
4111 * I.e, check "(link[0] & 0x08)".
4113 s = gen_load_a(OR_LINK, 0, BPF_B);
4114 b1 = new_block(JMP(BPF_JSET));
4121 b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4124 * AND that with the check of addr1.
4131 * Not present in management frames; addr2, if present,
4136 * Not present in CTS or ACK control frames.
4138 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4139 IEEE80211_FC0_TYPE_MASK);
4141 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4142 IEEE80211_FC0_SUBTYPE_MASK);
4144 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4145 IEEE80211_FC0_SUBTYPE_MASK);
4151 * If the high-order bit of the type value is 0, this
4152 * is a management frame.
4153 * I.e, check "(link[0] & 0x08)".
4155 s = gen_load_a(OR_LINK, 0, BPF_B);
4156 b1 = new_block(JMP(BPF_JSET));
4161 * AND that with the check for frames other than
4162 * CTS and ACK frames.
4169 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4174 * XXX - add BSSID keyword?
4177 return (gen_bcmp(OR_LINK, 4, 6, eaddr));
4181 * Not present in CTS or ACK control frames.
4183 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4184 IEEE80211_FC0_TYPE_MASK);
4186 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4187 IEEE80211_FC0_SUBTYPE_MASK);
4189 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4190 IEEE80211_FC0_SUBTYPE_MASK);
4194 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4200 * Not present in control frames.
4202 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4203 IEEE80211_FC0_TYPE_MASK);
4205 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4211 * Present only if the direction mask has both "From DS"
4212 * and "To DS" set. Neither control frames nor management
4213 * frames should have both of those set, so we don't
4214 * check the frame type.
4216 b0 = gen_mcmp(OR_LINK, 1, BPF_B,
4217 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4218 b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
4223 b0 = gen_wlanhostop(eaddr, Q_SRC);
4224 b1 = gen_wlanhostop(eaddr, Q_DST);
4230 b0 = gen_wlanhostop(eaddr, Q_SRC);
4231 b1 = gen_wlanhostop(eaddr, Q_DST);
4240 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4241 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4242 * as the RFC states.)
4244 static struct block *
4245 gen_ipfchostop(eaddr, dir)
4246 register const u_char *eaddr;
4249 register struct block *b0, *b1;
4253 return gen_bcmp(OR_LINK, 10, 6, eaddr);
4256 return gen_bcmp(OR_LINK, 2, 6, eaddr);
4259 b0 = gen_ipfchostop(eaddr, Q_SRC);
4260 b1 = gen_ipfchostop(eaddr, Q_DST);
4266 b0 = gen_ipfchostop(eaddr, Q_SRC);
4267 b1 = gen_ipfchostop(eaddr, Q_DST);
4272 bpf_error("'addr1' is only supported on 802.11");
4276 bpf_error("'addr2' is only supported on 802.11");
4280 bpf_error("'addr3' is only supported on 802.11");
4284 bpf_error("'addr4' is only supported on 802.11");
4288 bpf_error("'ra' is only supported on 802.11");
4292 bpf_error("'ta' is only supported on 802.11");
4300 * This is quite tricky because there may be pad bytes in front of the
4301 * DECNET header, and then there are two possible data packet formats that
4302 * carry both src and dst addresses, plus 5 packet types in a format that
4303 * carries only the src node, plus 2 types that use a different format and
4304 * also carry just the src node.
4308 * Instead of doing those all right, we just look for data packets with
4309 * 0 or 1 bytes of padding. If you want to look at other packets, that
4310 * will require a lot more hacking.
4312 * To add support for filtering on DECNET "areas" (network numbers)
4313 * one would want to add a "mask" argument to this routine. That would
4314 * make the filter even more inefficient, although one could be clever
4315 * and not generate masking instructions if the mask is 0xFFFF.
4317 static struct block *
4318 gen_dnhostop(addr, dir)
4322 struct block *b0, *b1, *b2, *tmp;
4323 u_int offset_lh; /* offset if long header is received */
4324 u_int offset_sh; /* offset if short header is received */
4329 offset_sh = 1; /* follows flags */
4330 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4334 offset_sh = 3; /* follows flags, dstnode */
4335 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4339 /* Inefficient because we do our Calvinball dance twice */
4340 b0 = gen_dnhostop(addr, Q_SRC);
4341 b1 = gen_dnhostop(addr, Q_DST);
4347 /* Inefficient because we do our Calvinball dance twice */
4348 b0 = gen_dnhostop(addr, Q_SRC);
4349 b1 = gen_dnhostop(addr, Q_DST);
4354 bpf_error("ISO host filtering not implemented");
4359 b0 = gen_linktype(ETHERTYPE_DN);
4360 /* Check for pad = 1, long header case */
4361 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4362 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
4363 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
4364 BPF_H, (bpf_int32)ntohs((u_short)addr));
4366 /* Check for pad = 0, long header case */
4367 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4368 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4371 /* Check for pad = 1, short header case */
4372 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4373 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
4374 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4377 /* Check for pad = 0, short header case */
4378 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4379 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4383 /* Combine with test for linktype */
4389 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4390 * test the bottom-of-stack bit, and then check the version number
4391 * field in the IP header.
4393 static struct block *
4394 gen_mpls_linktype(proto)
4397 struct block *b0, *b1;
4402 /* match the bottom-of-stack bit */
4403 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4404 /* match the IPv4 version number */
4405 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
4410 /* match the bottom-of-stack bit */
4411 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4412 /* match the IPv4 version number */
4413 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
4422 static struct block *
4423 gen_host(addr, mask, proto, dir, type)
4430 struct block *b0, *b1;
4431 const char *typestr;
4441 b0 = gen_host(addr, mask, Q_IP, dir, type);
4443 * Only check for non-IPv4 addresses if we're not
4444 * checking MPLS-encapsulated packets.
4446 if (label_stack_depth == 0) {
4447 b1 = gen_host(addr, mask, Q_ARP, dir, type);
4449 b0 = gen_host(addr, mask, Q_RARP, dir, type);
4455 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
4458 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4461 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4464 bpf_error("'tcp' modifier applied to %s", typestr);
4467 bpf_error("'sctp' modifier applied to %s", typestr);
4470 bpf_error("'udp' modifier applied to %s", typestr);
4473 bpf_error("'icmp' modifier applied to %s", typestr);
4476 bpf_error("'igmp' modifier applied to %s", typestr);
4479 bpf_error("'igrp' modifier applied to %s", typestr);
4482 bpf_error("'pim' modifier applied to %s", typestr);
4485 bpf_error("'vrrp' modifier applied to %s", typestr);
4488 bpf_error("'carp' modifier applied to %s", typestr);
4491 bpf_error("ATALK host filtering not implemented");
4494 bpf_error("AARP host filtering not implemented");
4497 return gen_dnhostop(addr, dir);
4500 bpf_error("SCA host filtering not implemented");
4503 bpf_error("LAT host filtering not implemented");
4506 bpf_error("MOPDL host filtering not implemented");
4509 bpf_error("MOPRC host filtering not implemented");
4513 bpf_error("'ip6' modifier applied to ip host");
4516 bpf_error("'icmp6' modifier applied to %s", typestr);
4520 bpf_error("'ah' modifier applied to %s", typestr);
4523 bpf_error("'esp' modifier applied to %s", typestr);
4526 bpf_error("ISO host filtering not implemented");
4529 bpf_error("'esis' modifier applied to %s", typestr);
4532 bpf_error("'isis' modifier applied to %s", typestr);
4535 bpf_error("'clnp' modifier applied to %s", typestr);
4538 bpf_error("'stp' modifier applied to %s", typestr);
4541 bpf_error("IPX host filtering not implemented");
4544 bpf_error("'netbeui' modifier applied to %s", typestr);
4547 bpf_error("'radio' modifier applied to %s", typestr);
4556 static struct block *
4557 gen_host6(addr, mask, proto, dir, type)
4558 struct in6_addr *addr;
4559 struct in6_addr *mask;
4564 const char *typestr;
4574 return gen_host6(addr, mask, Q_IPV6, dir, type);
4577 bpf_error("'ip' modifier applied to ip6 %s", typestr);
4580 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
4583 bpf_error("'arp' modifier applied to ip6 %s", typestr);
4586 bpf_error("'sctp' modifier applied to %s", typestr);
4589 bpf_error("'tcp' modifier applied to %s", typestr);
4592 bpf_error("'udp' modifier applied to %s", typestr);
4595 bpf_error("'icmp' modifier applied to %s", typestr);
4598 bpf_error("'igmp' modifier applied to %s", typestr);
4601 bpf_error("'igrp' modifier applied to %s", typestr);
4604 bpf_error("'pim' modifier applied to %s", typestr);
4607 bpf_error("'vrrp' modifier applied to %s", typestr);
4610 bpf_error("'carp' modifier applied to %s", typestr);
4613 bpf_error("ATALK host filtering not implemented");
4616 bpf_error("AARP host filtering not implemented");
4619 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
4622 bpf_error("SCA host filtering not implemented");
4625 bpf_error("LAT host filtering not implemented");
4628 bpf_error("MOPDL host filtering not implemented");
4631 bpf_error("MOPRC host filtering not implemented");
4634 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4637 bpf_error("'icmp6' modifier applied to %s", typestr);
4640 bpf_error("'ah' modifier applied to %s", typestr);
4643 bpf_error("'esp' modifier applied to %s", typestr);
4646 bpf_error("ISO host filtering not implemented");
4649 bpf_error("'esis' modifier applied to %s", typestr);
4652 bpf_error("'isis' modifier applied to %s", typestr);
4655 bpf_error("'clnp' modifier applied to %s", typestr);
4658 bpf_error("'stp' modifier applied to %s", typestr);
4661 bpf_error("IPX host filtering not implemented");
4664 bpf_error("'netbeui' modifier applied to %s", typestr);
4667 bpf_error("'radio' modifier applied to %s", typestr);
4677 static struct block *
4678 gen_gateway(eaddr, alist, proto, dir)
4679 const u_char *eaddr;
4680 bpf_u_int32 **alist;
4684 struct block *b0, *b1, *tmp;
4687 bpf_error("direction applied to 'gateway'");
4696 case DLT_NETANALYZER:
4697 case DLT_NETANALYZER_TRANSPARENT:
4698 b0 = gen_ehostop(eaddr, Q_OR);
4701 b0 = gen_fhostop(eaddr, Q_OR);
4704 b0 = gen_thostop(eaddr, Q_OR);
4706 case DLT_IEEE802_11:
4707 case DLT_PRISM_HEADER:
4708 case DLT_IEEE802_11_RADIO_AVS:
4709 case DLT_IEEE802_11_RADIO:
4711 b0 = gen_wlanhostop(eaddr, Q_OR);
4716 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4718 * Check that the packet doesn't begin with an
4719 * LE Control marker. (We've already generated
4722 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4727 * Now check the MAC address.
4729 b0 = gen_ehostop(eaddr, Q_OR);
4732 case DLT_IP_OVER_FC:
4733 b0 = gen_ipfchostop(eaddr, Q_OR);
4737 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4739 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
4741 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
4750 bpf_error("illegal modifier of 'gateway'");
4756 gen_proto_abbrev(proto)
4765 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
4767 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
4773 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
4775 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
4781 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
4783 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
4789 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
4792 #ifndef IPPROTO_IGMP
4793 #define IPPROTO_IGMP 2
4797 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
4800 #ifndef IPPROTO_IGRP
4801 #define IPPROTO_IGRP 9
4804 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
4808 #define IPPROTO_PIM 103
4812 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
4814 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
4819 #ifndef IPPROTO_VRRP
4820 #define IPPROTO_VRRP 112
4824 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
4827 #ifndef IPPROTO_CARP
4828 #define IPPROTO_CARP 112
4832 b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT);
4836 b1 = gen_linktype(ETHERTYPE_IP);
4840 b1 = gen_linktype(ETHERTYPE_ARP);
4844 b1 = gen_linktype(ETHERTYPE_REVARP);
4848 bpf_error("link layer applied in wrong context");
4851 b1 = gen_linktype(ETHERTYPE_ATALK);
4855 b1 = gen_linktype(ETHERTYPE_AARP);
4859 b1 = gen_linktype(ETHERTYPE_DN);
4863 b1 = gen_linktype(ETHERTYPE_SCA);
4867 b1 = gen_linktype(ETHERTYPE_LAT);
4871 b1 = gen_linktype(ETHERTYPE_MOPDL);
4875 b1 = gen_linktype(ETHERTYPE_MOPRC);
4880 b1 = gen_linktype(ETHERTYPE_IPV6);
4883 #ifndef IPPROTO_ICMPV6
4884 #define IPPROTO_ICMPV6 58
4887 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
4892 #define IPPROTO_AH 51
4895 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
4897 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
4903 #define IPPROTO_ESP 50
4906 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
4908 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
4914 b1 = gen_linktype(LLCSAP_ISONS);
4918 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
4922 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4925 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
4926 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4927 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4929 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4931 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4933 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4937 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
4938 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4939 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4941 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4943 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4945 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4949 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
4950 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4951 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4953 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
4958 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4959 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4964 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4965 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4967 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4969 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4974 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4975 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4980 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4981 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4986 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
4990 b1 = gen_linktype(LLCSAP_8021D);
4994 b1 = gen_linktype(LLCSAP_IPX);
4998 b1 = gen_linktype(LLCSAP_NETBEUI);
5002 bpf_error("'radio' is not a valid protocol type");
5010 static struct block *
5016 /* not IPv4 frag other than the first frag */
5017 s = gen_load_a(OR_NET, 6, BPF_H);
5018 b = new_block(JMP(BPF_JSET));
5027 * Generate a comparison to a port value in the transport-layer header
5028 * at the specified offset from the beginning of that header.
5030 * XXX - this handles a variable-length prefix preceding the link-layer
5031 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5032 * variable-length link-layer headers (such as Token Ring or 802.11
5035 static struct block *
5036 gen_portatom(off, v)
5040 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
5044 static struct block *
5045 gen_portatom6(off, v)
5049 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
5054 gen_portop(port, proto, dir)
5055 int port, proto, dir;
5057 struct block *b0, *b1, *tmp;
5059 /* ip proto 'proto' and not a fragment other than the first fragment */
5060 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5066 b1 = gen_portatom(0, (bpf_int32)port);
5070 b1 = gen_portatom(2, (bpf_int32)port);
5075 tmp = gen_portatom(0, (bpf_int32)port);
5076 b1 = gen_portatom(2, (bpf_int32)port);
5081 tmp = gen_portatom(0, (bpf_int32)port);
5082 b1 = gen_portatom(2, (bpf_int32)port);
5094 static struct block *
5095 gen_port(port, ip_proto, dir)
5100 struct block *b0, *b1, *tmp;
5105 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5106 * not LLC encapsulation with LLCSAP_IP.
5108 * For IEEE 802 networks - which includes 802.5 token ring
5109 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5110 * says that SNAP encapsulation is used, not LLC encapsulation
5113 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5114 * RFC 2225 say that SNAP encapsulation is used, not LLC
5115 * encapsulation with LLCSAP_IP.
5117 * So we always check for ETHERTYPE_IP.
5119 b0 = gen_linktype(ETHERTYPE_IP);
5125 b1 = gen_portop(port, ip_proto, dir);
5129 tmp = gen_portop(port, IPPROTO_TCP, dir);
5130 b1 = gen_portop(port, IPPROTO_UDP, dir);
5132 tmp = gen_portop(port, IPPROTO_SCTP, dir);
5145 gen_portop6(port, proto, dir)
5146 int port, proto, dir;
5148 struct block *b0, *b1, *tmp;
5150 /* ip6 proto 'proto' */
5151 /* XXX - catch the first fragment of a fragmented packet? */
5152 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5156 b1 = gen_portatom6(0, (bpf_int32)port);
5160 b1 = gen_portatom6(2, (bpf_int32)port);
5165 tmp = gen_portatom6(0, (bpf_int32)port);
5166 b1 = gen_portatom6(2, (bpf_int32)port);
5171 tmp = gen_portatom6(0, (bpf_int32)port);
5172 b1 = gen_portatom6(2, (bpf_int32)port);
5184 static struct block *
5185 gen_port6(port, ip_proto, dir)
5190 struct block *b0, *b1, *tmp;
5192 /* link proto ip6 */
5193 b0 = gen_linktype(ETHERTYPE_IPV6);
5199 b1 = gen_portop6(port, ip_proto, dir);
5203 tmp = gen_portop6(port, IPPROTO_TCP, dir);
5204 b1 = gen_portop6(port, IPPROTO_UDP, dir);
5206 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
5218 /* gen_portrange code */
5219 static struct block *
5220 gen_portrangeatom(off, v1, v2)
5224 struct block *b1, *b2;
5228 * Reverse the order of the ports, so v1 is the lower one.
5237 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
5238 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
5246 gen_portrangeop(port1, port2, proto, dir)
5251 struct block *b0, *b1, *tmp;
5253 /* ip proto 'proto' and not a fragment other than the first fragment */
5254 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5260 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5264 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5269 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5270 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5275 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5276 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5288 static struct block *
5289 gen_portrange(port1, port2, ip_proto, dir)
5294 struct block *b0, *b1, *tmp;
5297 b0 = gen_linktype(ETHERTYPE_IP);
5303 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
5307 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
5308 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
5310 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
5322 static struct block *
5323 gen_portrangeatom6(off, v1, v2)
5327 struct block *b1, *b2;
5331 * Reverse the order of the ports, so v1 is the lower one.
5340 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
5341 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
5349 gen_portrangeop6(port1, port2, proto, dir)
5354 struct block *b0, *b1, *tmp;
5356 /* ip6 proto 'proto' */
5357 /* XXX - catch the first fragment of a fragmented packet? */
5358 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5362 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5366 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5371 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5372 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5377 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5378 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5390 static struct block *
5391 gen_portrange6(port1, port2, ip_proto, dir)
5396 struct block *b0, *b1, *tmp;
5398 /* link proto ip6 */
5399 b0 = gen_linktype(ETHERTYPE_IPV6);
5405 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
5409 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
5410 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
5412 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
5425 lookup_proto(name, proto)
5426 register const char *name;
5436 v = pcap_nametoproto(name);
5437 if (v == PROTO_UNDEF)
5438 bpf_error("unknown ip proto '%s'", name);
5442 /* XXX should look up h/w protocol type based on linktype */
5443 v = pcap_nametoeproto(name);
5444 if (v == PROTO_UNDEF) {
5445 v = pcap_nametollc(name);
5446 if (v == PROTO_UNDEF)
5447 bpf_error("unknown ether proto '%s'", name);
5452 if (strcmp(name, "esis") == 0)
5454 else if (strcmp(name, "isis") == 0)
5456 else if (strcmp(name, "clnp") == 0)
5459 bpf_error("unknown osi proto '%s'", name);
5479 static struct block *
5480 gen_protochain(v, proto, dir)
5485 #ifdef NO_PROTOCHAIN
5486 return gen_proto(v, proto, dir);
5488 struct block *b0, *b;
5489 struct slist *s[100];
5490 int fix2, fix3, fix4, fix5;
5491 int ahcheck, again, end;
5493 int reg2 = alloc_reg();
5495 memset(s, 0, sizeof(s));
5496 fix2 = fix3 = fix4 = fix5 = 0;
5503 b0 = gen_protochain(v, Q_IP, dir);
5504 b = gen_protochain(v, Q_IPV6, dir);
5508 bpf_error("bad protocol applied for 'protochain'");
5513 * We don't handle variable-length prefixes before the link-layer
5514 * header, or variable-length link-layer headers, here yet.
5515 * We might want to add BPF instructions to do the protochain
5516 * work, to simplify that and, on platforms that have a BPF
5517 * interpreter with the new instructions, let the filtering
5518 * be done in the kernel. (We already require a modified BPF
5519 * engine to do the protochain stuff, to support backward
5520 * branches, and backward branch support is unlikely to appear
5521 * in kernel BPF engines.)
5525 case DLT_IEEE802_11:
5526 case DLT_PRISM_HEADER:
5527 case DLT_IEEE802_11_RADIO_AVS:
5528 case DLT_IEEE802_11_RADIO:
5530 bpf_error("'protochain' not supported with 802.11");
5533 no_optimize = 1; /*this code is not compatible with optimzer yet */
5536 * s[0] is a dummy entry to protect other BPF insn from damage
5537 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5538 * hard to find interdependency made by jump table fixup.
5541 s[i] = new_stmt(0); /*dummy*/
5546 b0 = gen_linktype(ETHERTYPE_IP);
5549 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5550 s[i]->s.k = off_macpl + off_nl + 9;
5552 /* X = ip->ip_hl << 2 */
5553 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
5554 s[i]->s.k = off_macpl + off_nl;
5559 b0 = gen_linktype(ETHERTYPE_IPV6);
5561 /* A = ip6->ip_nxt */
5562 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5563 s[i]->s.k = off_macpl + off_nl + 6;
5565 /* X = sizeof(struct ip6_hdr) */
5566 s[i] = new_stmt(BPF_LDX|BPF_IMM);
5572 bpf_error("unsupported proto to gen_protochain");
5576 /* again: if (A == v) goto end; else fall through; */
5578 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5580 s[i]->s.jt = NULL; /*later*/
5581 s[i]->s.jf = NULL; /*update in next stmt*/
5585 #ifndef IPPROTO_NONE
5586 #define IPPROTO_NONE 59
5588 /* if (A == IPPROTO_NONE) goto end */
5589 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5590 s[i]->s.jt = NULL; /*later*/
5591 s[i]->s.jf = NULL; /*update in next stmt*/
5592 s[i]->s.k = IPPROTO_NONE;
5593 s[fix5]->s.jf = s[i];
5598 if (proto == Q_IPV6) {
5599 int v6start, v6end, v6advance, j;
5602 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5603 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5604 s[i]->s.jt = NULL; /*later*/
5605 s[i]->s.jf = NULL; /*update in next stmt*/
5606 s[i]->s.k = IPPROTO_HOPOPTS;
5607 s[fix2]->s.jf = s[i];
5609 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5610 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5611 s[i]->s.jt = NULL; /*later*/
5612 s[i]->s.jf = NULL; /*update in next stmt*/
5613 s[i]->s.k = IPPROTO_DSTOPTS;
5615 /* if (A == IPPROTO_ROUTING) goto v6advance */
5616 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5617 s[i]->s.jt = NULL; /*later*/
5618 s[i]->s.jf = NULL; /*update in next stmt*/
5619 s[i]->s.k = IPPROTO_ROUTING;
5621 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5622 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5623 s[i]->s.jt = NULL; /*later*/
5624 s[i]->s.jf = NULL; /*later*/
5625 s[i]->s.k = IPPROTO_FRAGMENT;
5635 * A = P[X + packet head];
5636 * X = X + (P[X + packet head + 1] + 1) * 8;
5638 /* A = P[X + packet head] */
5639 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5640 s[i]->s.k = off_macpl + off_nl;
5643 s[i] = new_stmt(BPF_ST);
5646 /* A = P[X + packet head + 1]; */
5647 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5648 s[i]->s.k = off_macpl + off_nl + 1;
5651 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5655 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5659 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
5663 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5666 s[i] = new_stmt(BPF_LD|BPF_MEM);
5670 /* goto again; (must use BPF_JA for backward jump) */
5671 s[i] = new_stmt(BPF_JMP|BPF_JA);
5672 s[i]->s.k = again - i - 1;
5673 s[i - 1]->s.jf = s[i];
5677 for (j = v6start; j <= v6end; j++)
5678 s[j]->s.jt = s[v6advance];
5683 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5685 s[fix2]->s.jf = s[i];
5691 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5692 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5693 s[i]->s.jt = NULL; /*later*/
5694 s[i]->s.jf = NULL; /*later*/
5695 s[i]->s.k = IPPROTO_AH;
5697 s[fix3]->s.jf = s[ahcheck];
5704 * X = X + (P[X + 1] + 2) * 4;
5707 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5709 /* A = P[X + packet head]; */
5710 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5711 s[i]->s.k = off_macpl + off_nl;
5714 s[i] = new_stmt(BPF_ST);
5718 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5721 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5725 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5727 /* A = P[X + packet head] */
5728 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5729 s[i]->s.k = off_macpl + off_nl;
5732 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5736 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5740 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5743 s[i] = new_stmt(BPF_LD|BPF_MEM);
5747 /* goto again; (must use BPF_JA for backward jump) */
5748 s[i] = new_stmt(BPF_JMP|BPF_JA);
5749 s[i]->s.k = again - i - 1;
5754 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5756 s[fix2]->s.jt = s[end];
5757 s[fix4]->s.jf = s[end];
5758 s[fix5]->s.jt = s[end];
5765 for (i = 0; i < max - 1; i++)
5766 s[i]->next = s[i + 1];
5767 s[max - 1]->next = NULL;
5772 b = new_block(JMP(BPF_JEQ));
5773 b->stmts = s[1]; /*remember, s[0] is dummy*/
5783 static struct block *
5784 gen_check_802_11_data_frame()
5787 struct block *b0, *b1;
5790 * A data frame has the 0x08 bit (b3) in the frame control field set
5791 * and the 0x04 bit (b2) clear.
5793 s = gen_load_a(OR_LINK, 0, BPF_B);
5794 b0 = new_block(JMP(BPF_JSET));
5798 s = gen_load_a(OR_LINK, 0, BPF_B);
5799 b1 = new_block(JMP(BPF_JSET));
5810 * Generate code that checks whether the packet is a packet for protocol
5811 * <proto> and whether the type field in that protocol's header has
5812 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5813 * IP packet and checks the protocol number in the IP header against <v>.
5815 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5816 * against Q_IP and Q_IPV6.
5818 static struct block *
5819 gen_proto(v, proto, dir)
5824 struct block *b0, *b1;
5831 if (dir != Q_DEFAULT)
5832 bpf_error("direction applied to 'proto'");
5837 b0 = gen_proto(v, Q_IP, dir);
5838 b1 = gen_proto(v, Q_IPV6, dir);
5846 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5847 * not LLC encapsulation with LLCSAP_IP.
5849 * For IEEE 802 networks - which includes 802.5 token ring
5850 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5851 * says that SNAP encapsulation is used, not LLC encapsulation
5854 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5855 * RFC 2225 say that SNAP encapsulation is used, not LLC
5856 * encapsulation with LLCSAP_IP.
5858 * So we always check for ETHERTYPE_IP.
5860 b0 = gen_linktype(ETHERTYPE_IP);
5862 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
5864 b1 = gen_protochain(v, Q_IP);
5874 * Frame Relay packets typically have an OSI
5875 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5876 * generates code to check for all the OSI
5877 * NLPIDs, so calling it and then adding a check
5878 * for the particular NLPID for which we're
5879 * looking is bogus, as we can just check for
5882 * What we check for is the NLPID and a frame
5883 * control field value of UI, i.e. 0x03 followed
5886 * XXX - assumes a 2-byte Frame Relay header with
5887 * DLCI and flags. What if the address is longer?
5889 * XXX - what about SNAP-encapsulated frames?
5891 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
5897 * Cisco uses an Ethertype lookalike - for OSI,
5900 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
5901 /* OSI in C-HDLC is stuffed with a fudge byte */
5902 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
5907 b0 = gen_linktype(LLCSAP_ISONS);
5908 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
5914 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5916 * 4 is the offset of the PDU type relative to the IS-IS
5919 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
5924 bpf_error("arp does not encapsulate another protocol");
5928 bpf_error("rarp does not encapsulate another protocol");
5932 bpf_error("atalk encapsulation is not specifiable");
5936 bpf_error("decnet encapsulation is not specifiable");
5940 bpf_error("sca does not encapsulate another protocol");
5944 bpf_error("lat does not encapsulate another protocol");
5948 bpf_error("moprc does not encapsulate another protocol");
5952 bpf_error("mopdl does not encapsulate another protocol");
5956 return gen_linktype(v);
5959 bpf_error("'udp proto' is bogus");
5963 bpf_error("'tcp proto' is bogus");
5967 bpf_error("'sctp proto' is bogus");
5971 bpf_error("'icmp proto' is bogus");
5975 bpf_error("'igmp proto' is bogus");
5979 bpf_error("'igrp proto' is bogus");
5983 bpf_error("'pim proto' is bogus");
5987 bpf_error("'vrrp proto' is bogus");
5991 bpf_error("'carp proto' is bogus");
5996 b0 = gen_linktype(ETHERTYPE_IPV6);
5999 * Also check for a fragment header before the final
6002 b2 = gen_cmp(OR_NET, 6, BPF_B, IPPROTO_FRAGMENT);
6003 b1 = gen_cmp(OR_NET, 40, BPF_B, (bpf_int32)v);
6005 b2 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
6008 b1 = gen_protochain(v, Q_IPV6);
6014 bpf_error("'icmp6 proto' is bogus");
6018 bpf_error("'ah proto' is bogus");
6021 bpf_error("'ah proto' is bogus");
6024 bpf_error("'stp proto' is bogus");
6027 bpf_error("'ipx proto' is bogus");
6030 bpf_error("'netbeui proto' is bogus");
6033 bpf_error("'radio proto' is bogus");
6044 register const char *name;
6047 int proto = q.proto;
6051 bpf_u_int32 mask, addr;
6053 bpf_u_int32 **alist;
6056 struct sockaddr_in *sin4;
6057 struct sockaddr_in6 *sin6;
6058 struct addrinfo *res, *res0;
6059 struct in6_addr mask128;
6061 struct block *b, *tmp;
6062 int port, real_proto;
6068 addr = pcap_nametonetaddr(name);
6070 bpf_error("unknown network '%s'", name);
6071 /* Left justify network addr and calculate its network mask */
6073 while (addr && (addr & 0xff000000) == 0) {
6077 return gen_host(addr, mask, proto, dir, q.addr);
6081 if (proto == Q_LINK) {
6085 case DLT_NETANALYZER:
6086 case DLT_NETANALYZER_TRANSPARENT:
6087 eaddr = pcap_ether_hostton(name);
6090 "unknown ether host '%s'", name);
6091 b = gen_ehostop(eaddr, dir);
6096 eaddr = pcap_ether_hostton(name);
6099 "unknown FDDI host '%s'", name);
6100 b = gen_fhostop(eaddr, dir);
6105 eaddr = pcap_ether_hostton(name);
6108 "unknown token ring host '%s'", name);
6109 b = gen_thostop(eaddr, dir);
6113 case DLT_IEEE802_11:
6114 case DLT_PRISM_HEADER:
6115 case DLT_IEEE802_11_RADIO_AVS:
6116 case DLT_IEEE802_11_RADIO:
6118 eaddr = pcap_ether_hostton(name);
6121 "unknown 802.11 host '%s'", name);
6122 b = gen_wlanhostop(eaddr, dir);
6126 case DLT_IP_OVER_FC:
6127 eaddr = pcap_ether_hostton(name);
6130 "unknown Fibre Channel host '%s'", name);
6131 b = gen_ipfchostop(eaddr, dir);
6140 * Check that the packet doesn't begin
6141 * with an LE Control marker. (We've
6142 * already generated a test for LANE.)
6144 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
6148 eaddr = pcap_ether_hostton(name);
6151 "unknown ether host '%s'", name);
6152 b = gen_ehostop(eaddr, dir);
6158 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6159 } else if (proto == Q_DECNET) {
6160 unsigned short dn_addr = __pcap_nametodnaddr(name);
6162 * I don't think DECNET hosts can be multihomed, so
6163 * there is no need to build up a list of addresses
6165 return (gen_host(dn_addr, 0, proto, dir, q.addr));
6168 alist = pcap_nametoaddr(name);
6169 if (alist == NULL || *alist == NULL)
6170 bpf_error("unknown host '%s'", name);
6172 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
6174 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
6176 tmp = gen_host(**alist++, 0xffffffff,
6177 tproto, dir, q.addr);
6183 memset(&mask128, 0xff, sizeof(mask128));
6184 res0 = res = pcap_nametoaddrinfo(name);
6186 bpf_error("unknown host '%s'", name);
6189 tproto = tproto6 = proto;
6190 if (off_linktype == -1 && tproto == Q_DEFAULT) {
6194 for (res = res0; res; res = res->ai_next) {
6195 switch (res->ai_family) {
6197 if (tproto == Q_IPV6)
6200 sin4 = (struct sockaddr_in *)
6202 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
6203 0xffffffff, tproto, dir, q.addr);
6206 if (tproto6 == Q_IP)
6209 sin6 = (struct sockaddr_in6 *)
6211 tmp = gen_host6(&sin6->sin6_addr,
6212 &mask128, tproto6, dir, q.addr);
6224 bpf_error("unknown host '%s'%s", name,
6225 (proto == Q_DEFAULT)
6227 : " for specified address family");
6234 if (proto != Q_DEFAULT &&
6235 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6236 bpf_error("illegal qualifier of 'port'");
6237 if (pcap_nametoport(name, &port, &real_proto) == 0)
6238 bpf_error("unknown port '%s'", name);
6239 if (proto == Q_UDP) {
6240 if (real_proto == IPPROTO_TCP)
6241 bpf_error("port '%s' is tcp", name);
6242 else if (real_proto == IPPROTO_SCTP)
6243 bpf_error("port '%s' is sctp", name);
6245 /* override PROTO_UNDEF */
6246 real_proto = IPPROTO_UDP;
6248 if (proto == Q_TCP) {
6249 if (real_proto == IPPROTO_UDP)
6250 bpf_error("port '%s' is udp", name);
6252 else if (real_proto == IPPROTO_SCTP)
6253 bpf_error("port '%s' is sctp", name);
6255 /* override PROTO_UNDEF */
6256 real_proto = IPPROTO_TCP;
6258 if (proto == Q_SCTP) {
6259 if (real_proto == IPPROTO_UDP)
6260 bpf_error("port '%s' is udp", name);
6262 else if (real_proto == IPPROTO_TCP)
6263 bpf_error("port '%s' is tcp", name);
6265 /* override PROTO_UNDEF */
6266 real_proto = IPPROTO_SCTP;
6269 bpf_error("illegal port number %d < 0", port);
6271 bpf_error("illegal port number %d > 65535", port);
6273 return gen_port(port, real_proto, dir);
6275 b = gen_port(port, real_proto, dir);
6276 gen_or(gen_port6(port, real_proto, dir), b);
6281 if (proto != Q_DEFAULT &&
6282 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6283 bpf_error("illegal qualifier of 'portrange'");
6284 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6285 bpf_error("unknown port in range '%s'", name);
6286 if (proto == Q_UDP) {
6287 if (real_proto == IPPROTO_TCP)
6288 bpf_error("port in range '%s' is tcp", name);
6289 else if (real_proto == IPPROTO_SCTP)
6290 bpf_error("port in range '%s' is sctp", name);
6292 /* override PROTO_UNDEF */
6293 real_proto = IPPROTO_UDP;
6295 if (proto == Q_TCP) {
6296 if (real_proto == IPPROTO_UDP)
6297 bpf_error("port in range '%s' is udp", name);
6298 else if (real_proto == IPPROTO_SCTP)
6299 bpf_error("port in range '%s' is sctp", name);
6301 /* override PROTO_UNDEF */
6302 real_proto = IPPROTO_TCP;
6304 if (proto == Q_SCTP) {
6305 if (real_proto == IPPROTO_UDP)
6306 bpf_error("port in range '%s' is udp", name);
6307 else if (real_proto == IPPROTO_TCP)
6308 bpf_error("port in range '%s' is tcp", name);
6310 /* override PROTO_UNDEF */
6311 real_proto = IPPROTO_SCTP;
6314 bpf_error("illegal port number %d < 0", port1);
6316 bpf_error("illegal port number %d > 65535", port1);
6318 bpf_error("illegal port number %d < 0", port2);
6320 bpf_error("illegal port number %d > 65535", port2);
6323 return gen_portrange(port1, port2, real_proto, dir);
6325 b = gen_portrange(port1, port2, real_proto, dir);
6326 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
6332 eaddr = pcap_ether_hostton(name);
6334 bpf_error("unknown ether host: %s", name);
6336 alist = pcap_nametoaddr(name);
6337 if (alist == NULL || *alist == NULL)
6338 bpf_error("unknown host '%s'", name);
6339 b = gen_gateway(eaddr, alist, proto, dir);
6343 bpf_error("'gateway' not supported in this configuration");
6347 real_proto = lookup_proto(name, proto);
6348 if (real_proto >= 0)
6349 return gen_proto(real_proto, proto, dir);
6351 bpf_error("unknown protocol: %s", name);
6354 real_proto = lookup_proto(name, proto);
6355 if (real_proto >= 0)
6356 return gen_protochain(real_proto, proto, dir);
6358 bpf_error("unknown protocol: %s", name);
6369 gen_mcode(s1, s2, masklen, q)
6370 register const char *s1, *s2;
6371 register int masklen;
6374 register int nlen, mlen;
6377 nlen = __pcap_atoin(s1, &n);
6378 /* Promote short ipaddr */
6382 mlen = __pcap_atoin(s2, &m);
6383 /* Promote short ipaddr */
6386 bpf_error("non-network bits set in \"%s mask %s\"",
6389 /* Convert mask len to mask */
6391 bpf_error("mask length must be <= 32");
6394 * X << 32 is not guaranteed by C to be 0; it's
6399 m = 0xffffffff << (32 - masklen);
6401 bpf_error("non-network bits set in \"%s/%d\"",
6408 return gen_host(n, m, q.proto, q.dir, q.addr);
6411 bpf_error("Mask syntax for networks only");
6420 register const char *s;
6425 int proto = q.proto;
6431 else if (q.proto == Q_DECNET)
6432 vlen = __pcap_atodn(s, &v);
6434 vlen = __pcap_atoin(s, &v);
6441 if (proto == Q_DECNET)
6442 return gen_host(v, 0, proto, dir, q.addr);
6443 else if (proto == Q_LINK) {
6444 bpf_error("illegal link layer address");
6447 if (s == NULL && q.addr == Q_NET) {
6448 /* Promote short net number */
6449 while (v && (v & 0xff000000) == 0) {
6454 /* Promote short ipaddr */
6458 return gen_host(v, mask, proto, dir, q.addr);
6463 proto = IPPROTO_UDP;
6464 else if (proto == Q_TCP)
6465 proto = IPPROTO_TCP;
6466 else if (proto == Q_SCTP)
6467 proto = IPPROTO_SCTP;
6468 else if (proto == Q_DEFAULT)
6469 proto = PROTO_UNDEF;
6471 bpf_error("illegal qualifier of 'port'");
6474 bpf_error("illegal port number %u > 65535", v);
6477 return gen_port((int)v, proto, dir);
6481 b = gen_port((int)v, proto, dir);
6482 gen_or(gen_port6((int)v, proto, dir), b);
6489 proto = IPPROTO_UDP;
6490 else if (proto == Q_TCP)
6491 proto = IPPROTO_TCP;
6492 else if (proto == Q_SCTP)
6493 proto = IPPROTO_SCTP;
6494 else if (proto == Q_DEFAULT)
6495 proto = PROTO_UNDEF;
6497 bpf_error("illegal qualifier of 'portrange'");
6500 bpf_error("illegal port number %u > 65535", v);
6503 return gen_portrange((int)v, (int)v, proto, dir);
6507 b = gen_portrange((int)v, (int)v, proto, dir);
6508 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
6514 bpf_error("'gateway' requires a name");
6518 return gen_proto((int)v, proto, dir);
6521 return gen_protochain((int)v, proto, dir);
6536 gen_mcode6(s1, s2, masklen, q)
6537 register const char *s1, *s2;
6538 register int masklen;
6541 struct addrinfo *res;
6542 struct in6_addr *addr;
6543 struct in6_addr mask;
6548 bpf_error("no mask %s supported", s2);
6550 res = pcap_nametoaddrinfo(s1);
6552 bpf_error("invalid ip6 address %s", s1);
6555 bpf_error("%s resolved to multiple address", s1);
6556 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
6558 if (sizeof(mask) * 8 < masklen)
6559 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6560 memset(&mask, 0, sizeof(mask));
6561 memset(&mask, 0xff, masklen / 8);
6563 mask.s6_addr[masklen / 8] =
6564 (0xff << (8 - masklen % 8)) & 0xff;
6567 a = (u_int32_t *)addr;
6568 m = (u_int32_t *)&mask;
6569 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
6570 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
6571 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
6579 bpf_error("Mask syntax for networks only");
6583 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
6589 bpf_error("invalid qualifier against IPv6 address");
6598 register const u_char *eaddr;
6601 struct block *b, *tmp;
6603 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6606 case DLT_NETANALYZER:
6607 case DLT_NETANALYZER_TRANSPARENT:
6608 return gen_ehostop(eaddr, (int)q.dir);
6610 return gen_fhostop(eaddr, (int)q.dir);
6612 return gen_thostop(eaddr, (int)q.dir);
6613 case DLT_IEEE802_11:
6614 case DLT_PRISM_HEADER:
6615 case DLT_IEEE802_11_RADIO_AVS:
6616 case DLT_IEEE802_11_RADIO:
6618 return gen_wlanhostop(eaddr, (int)q.dir);
6622 * Check that the packet doesn't begin with an
6623 * LE Control marker. (We've already generated
6626 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6631 * Now check the MAC address.
6633 b = gen_ehostop(eaddr, (int)q.dir);
6638 case DLT_IP_OVER_FC:
6639 return gen_ipfchostop(eaddr, (int)q.dir);
6641 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6645 bpf_error("ethernet address used in non-ether expression");
6652 struct slist *s0, *s1;
6655 * This is definitely not the best way to do this, but the
6656 * lists will rarely get long.
6663 static struct slist *
6669 s = new_stmt(BPF_LDX|BPF_MEM);
6674 static struct slist *
6680 s = new_stmt(BPF_LD|BPF_MEM);
6686 * Modify "index" to use the value stored into its register as an
6687 * offset relative to the beginning of the header for the protocol
6688 * "proto", and allocate a register and put an item "size" bytes long
6689 * (1, 2, or 4) at that offset into that register, making it the register
6693 gen_load(proto, inst, size)
6698 struct slist *s, *tmp;
6700 int regno = alloc_reg();
6702 free_reg(inst->regno);
6706 bpf_error("data size must be 1, 2, or 4");
6722 bpf_error("unsupported index operation");
6726 * The offset is relative to the beginning of the packet
6727 * data, if we have a radio header. (If we don't, this
6730 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
6731 linktype != DLT_IEEE802_11_RADIO &&
6732 linktype != DLT_PRISM_HEADER)
6733 bpf_error("radio information not present in capture");
6736 * Load into the X register the offset computed into the
6737 * register specified by "index".
6739 s = xfer_to_x(inst);
6742 * Load the item at that offset.
6744 tmp = new_stmt(BPF_LD|BPF_IND|size);
6746 sappend(inst->s, s);
6751 * The offset is relative to the beginning of
6752 * the link-layer header.
6754 * XXX - what about ATM LANE? Should the index be
6755 * relative to the beginning of the AAL5 frame, so
6756 * that 0 refers to the beginning of the LE Control
6757 * field, or relative to the beginning of the LAN
6758 * frame, so that 0 refers, for Ethernet LANE, to
6759 * the beginning of the destination address?
6761 s = gen_llprefixlen();
6764 * If "s" is non-null, it has code to arrange that the
6765 * X register contains the length of the prefix preceding
6766 * the link-layer header. Add to it the offset computed
6767 * into the register specified by "index", and move that
6768 * into the X register. Otherwise, just load into the X
6769 * register the offset computed into the register specified
6773 sappend(s, xfer_to_a(inst));
6774 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6775 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6777 s = xfer_to_x(inst);
6780 * Load the item at the sum of the offset we've put in the
6781 * X register and the offset of the start of the link
6782 * layer header (which is 0 if the radio header is
6783 * variable-length; that header length is what we put
6784 * into the X register and then added to the index).
6786 tmp = new_stmt(BPF_LD|BPF_IND|size);
6789 sappend(inst->s, s);
6805 * The offset is relative to the beginning of
6806 * the network-layer header.
6807 * XXX - are there any cases where we want
6810 s = gen_off_macpl();
6813 * If "s" is non-null, it has code to arrange that the
6814 * X register contains the offset of the MAC-layer
6815 * payload. Add to it the offset computed into the
6816 * register specified by "index", and move that into
6817 * the X register. Otherwise, just load into the X
6818 * register the offset computed into the register specified
6822 sappend(s, xfer_to_a(inst));
6823 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6824 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6826 s = xfer_to_x(inst);
6829 * Load the item at the sum of the offset we've put in the
6830 * X register, the offset of the start of the network
6831 * layer header from the beginning of the MAC-layer
6832 * payload, and the purported offset of the start of the
6833 * MAC-layer payload (which might be 0 if there's a
6834 * variable-length prefix before the link-layer header
6835 * or the link-layer header itself is variable-length;
6836 * the variable-length offset of the start of the
6837 * MAC-layer payload is what we put into the X register
6838 * and then added to the index).
6840 tmp = new_stmt(BPF_LD|BPF_IND|size);
6841 tmp->s.k = off_macpl + off_nl;
6843 sappend(inst->s, s);
6846 * Do the computation only if the packet contains
6847 * the protocol in question.
6849 b = gen_proto_abbrev(proto);
6851 gen_and(inst->b, b);
6865 * The offset is relative to the beginning of
6866 * the transport-layer header.
6868 * Load the X register with the length of the IPv4 header
6869 * (plus the offset of the link-layer header, if it's
6870 * a variable-length header), in bytes.
6872 * XXX - are there any cases where we want
6874 * XXX - we should, if we're built with
6875 * IPv6 support, generate code to load either
6876 * IPv4, IPv6, or both, as appropriate.
6878 s = gen_loadx_iphdrlen();
6881 * The X register now contains the sum of the length
6882 * of any variable-length header preceding the link-layer
6883 * header, any variable-length link-layer header, and the
6884 * length of the network-layer header.
6886 * Load into the A register the offset relative to
6887 * the beginning of the transport layer header,
6888 * add the X register to that, move that to the
6889 * X register, and load with an offset from the
6890 * X register equal to the offset of the network
6891 * layer header relative to the beginning of
6892 * the MAC-layer payload plus the fixed-length
6893 * portion of the offset of the MAC-layer payload
6894 * from the beginning of the raw packet data.
6896 sappend(s, xfer_to_a(inst));
6897 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6898 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6899 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
6900 tmp->s.k = off_macpl + off_nl;
6901 sappend(inst->s, s);
6904 * Do the computation only if the packet contains
6905 * the protocol in question - which is true only
6906 * if this is an IP datagram and is the first or
6907 * only fragment of that datagram.
6909 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
6911 gen_and(inst->b, b);
6913 gen_and(gen_proto_abbrev(Q_IP), b);
6919 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6923 inst->regno = regno;
6924 s = new_stmt(BPF_ST);
6926 sappend(inst->s, s);
6932 gen_relation(code, a0, a1, reversed)
6934 struct arth *a0, *a1;
6937 struct slist *s0, *s1, *s2;
6938 struct block *b, *tmp;
6942 if (code == BPF_JEQ) {
6943 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
6944 b = new_block(JMP(code));
6948 b = new_block(BPF_JMP|code|BPF_X);
6954 sappend(a0->s, a1->s);
6958 free_reg(a0->regno);
6959 free_reg(a1->regno);
6961 /* 'and' together protocol checks */
6964 gen_and(a0->b, tmp = a1->b);
6980 int regno = alloc_reg();
6981 struct arth *a = (struct arth *)newchunk(sizeof(*a));
6984 s = new_stmt(BPF_LD|BPF_LEN);
6985 s->next = new_stmt(BPF_ST);
6986 s->next->s.k = regno;
7001 a = (struct arth *)newchunk(sizeof(*a));
7005 s = new_stmt(BPF_LD|BPF_IMM);
7007 s->next = new_stmt(BPF_ST);
7023 s = new_stmt(BPF_ALU|BPF_NEG);
7026 s = new_stmt(BPF_ST);
7034 gen_arth(code, a0, a1)
7036 struct arth *a0, *a1;
7038 struct slist *s0, *s1, *s2;
7042 s2 = new_stmt(BPF_ALU|BPF_X|code);
7047 sappend(a0->s, a1->s);
7049 free_reg(a0->regno);
7050 free_reg(a1->regno);
7052 s0 = new_stmt(BPF_ST);
7053 a0->regno = s0->s.k = alloc_reg();
7060 * Here we handle simple allocation of the scratch registers.
7061 * If too many registers are alloc'd, the allocator punts.
7063 static int regused[BPF_MEMWORDS];
7067 * Initialize the table of used registers and the current register.
7073 memset(regused, 0, sizeof regused);
7077 * Return the next free register.
7082 int n = BPF_MEMWORDS;
7085 if (regused[curreg])
7086 curreg = (curreg + 1) % BPF_MEMWORDS;
7088 regused[curreg] = 1;
7092 bpf_error("too many registers needed to evaluate expression");
7098 * Return a register to the table so it can
7108 static struct block *
7115 s = new_stmt(BPF_LD|BPF_LEN);
7116 b = new_block(JMP(jmp));
7127 return gen_len(BPF_JGE, n);
7131 * Actually, this is less than or equal.
7139 b = gen_len(BPF_JGT, n);
7146 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7147 * the beginning of the link-layer header.
7148 * XXX - that means you can't test values in the radiotap header, but
7149 * as that header is difficult if not impossible to parse generally
7150 * without a loop, that might not be a severe problem. A new keyword
7151 * "radio" could be added for that, although what you'd really want
7152 * would be a way of testing particular radio header values, which
7153 * would generate code appropriate to the radio header in question.
7156 gen_byteop(op, idx, val)
7167 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7170 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7174 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7178 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
7182 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
7186 b = new_block(JMP(BPF_JEQ));
7193 static u_char abroadcast[] = { 0x0 };
7196 gen_broadcast(proto)
7199 bpf_u_int32 hostmask;
7200 struct block *b0, *b1, *b2;
7201 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7209 case DLT_ARCNET_LINUX:
7210 return gen_ahostop(abroadcast, Q_DST);
7212 case DLT_NETANALYZER:
7213 case DLT_NETANALYZER_TRANSPARENT:
7214 return gen_ehostop(ebroadcast, Q_DST);
7216 return gen_fhostop(ebroadcast, Q_DST);
7218 return gen_thostop(ebroadcast, Q_DST);
7219 case DLT_IEEE802_11:
7220 case DLT_PRISM_HEADER:
7221 case DLT_IEEE802_11_RADIO_AVS:
7222 case DLT_IEEE802_11_RADIO:
7224 return gen_wlanhostop(ebroadcast, Q_DST);
7225 case DLT_IP_OVER_FC:
7226 return gen_ipfchostop(ebroadcast, Q_DST);
7230 * Check that the packet doesn't begin with an
7231 * LE Control marker. (We've already generated
7234 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7239 * Now check the MAC address.
7241 b0 = gen_ehostop(ebroadcast, Q_DST);
7247 bpf_error("not a broadcast link");
7253 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7254 * as an indication that we don't know the netmask, and fail
7257 if (netmask == PCAP_NETMASK_UNKNOWN)
7258 bpf_error("netmask not known, so 'ip broadcast' not supported");
7259 b0 = gen_linktype(ETHERTYPE_IP);
7260 hostmask = ~netmask;
7261 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
7262 b2 = gen_mcmp(OR_NET, 16, BPF_W,
7263 (bpf_int32)(~0 & hostmask), hostmask);
7268 bpf_error("only link-layer/IP broadcast filters supported");
7274 * Generate code to test the low-order bit of a MAC address (that's
7275 * the bottom bit of the *first* byte).
7277 static struct block *
7278 gen_mac_multicast(offset)
7281 register struct block *b0;
7282 register struct slist *s;
7284 /* link[offset] & 1 != 0 */
7285 s = gen_load_a(OR_LINK, offset, BPF_B);
7286 b0 = new_block(JMP(BPF_JSET));
7293 gen_multicast(proto)
7296 register struct block *b0, *b1, *b2;
7297 register struct slist *s;
7305 case DLT_ARCNET_LINUX:
7306 /* all ARCnet multicasts use the same address */
7307 return gen_ahostop(abroadcast, Q_DST);
7309 case DLT_NETANALYZER:
7310 case DLT_NETANALYZER_TRANSPARENT:
7311 /* ether[0] & 1 != 0 */
7312 return gen_mac_multicast(0);
7315 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7317 * XXX - was that referring to bit-order issues?
7319 /* fddi[1] & 1 != 0 */
7320 return gen_mac_multicast(1);
7322 /* tr[2] & 1 != 0 */
7323 return gen_mac_multicast(2);
7324 case DLT_IEEE802_11:
7325 case DLT_PRISM_HEADER:
7326 case DLT_IEEE802_11_RADIO_AVS:
7327 case DLT_IEEE802_11_RADIO:
7332 * For control frames, there is no DA.
7334 * For management frames, DA is at an
7335 * offset of 4 from the beginning of
7338 * For data frames, DA is at an offset
7339 * of 4 from the beginning of the packet
7340 * if To DS is clear and at an offset of
7341 * 16 from the beginning of the packet
7346 * Generate the tests to be done for data frames.
7348 * First, check for To DS set, i.e. "link[1] & 0x01".
7350 s = gen_load_a(OR_LINK, 1, BPF_B);
7351 b1 = new_block(JMP(BPF_JSET));
7352 b1->s.k = 0x01; /* To DS */
7356 * If To DS is set, the DA is at 16.
7358 b0 = gen_mac_multicast(16);
7362 * Now, check for To DS not set, i.e. check
7363 * "!(link[1] & 0x01)".
7365 s = gen_load_a(OR_LINK, 1, BPF_B);
7366 b2 = new_block(JMP(BPF_JSET));
7367 b2->s.k = 0x01; /* To DS */
7372 * If To DS is not set, the DA is at 4.
7374 b1 = gen_mac_multicast(4);
7378 * Now OR together the last two checks. That gives
7379 * the complete set of checks for data frames.
7384 * Now check for a data frame.
7385 * I.e, check "link[0] & 0x08".
7387 s = gen_load_a(OR_LINK, 0, BPF_B);
7388 b1 = new_block(JMP(BPF_JSET));
7393 * AND that with the checks done for data frames.
7398 * If the high-order bit of the type value is 0, this
7399 * is a management frame.
7400 * I.e, check "!(link[0] & 0x08)".
7402 s = gen_load_a(OR_LINK, 0, BPF_B);
7403 b2 = new_block(JMP(BPF_JSET));
7409 * For management frames, the DA is at 4.
7411 b1 = gen_mac_multicast(4);
7415 * OR that with the checks done for data frames.
7416 * That gives the checks done for management and
7422 * If the low-order bit of the type value is 1,
7423 * this is either a control frame or a frame
7424 * with a reserved type, and thus not a
7427 * I.e., check "!(link[0] & 0x04)".
7429 s = gen_load_a(OR_LINK, 0, BPF_B);
7430 b1 = new_block(JMP(BPF_JSET));
7436 * AND that with the checks for data and management
7441 case DLT_IP_OVER_FC:
7442 b0 = gen_mac_multicast(2);
7447 * Check that the packet doesn't begin with an
7448 * LE Control marker. (We've already generated
7451 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7455 /* ether[off_mac] & 1 != 0 */
7456 b0 = gen_mac_multicast(off_mac);
7464 /* Link not known to support multicasts */
7468 b0 = gen_linktype(ETHERTYPE_IP);
7469 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
7475 b0 = gen_linktype(ETHERTYPE_IPV6);
7476 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
7481 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7487 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7488 * Outbound traffic is sent by this machine, while inbound traffic is
7489 * sent by a remote machine (and may include packets destined for a
7490 * unicast or multicast link-layer address we are not subscribing to).
7491 * These are the same definitions implemented by pcap_setdirection().
7492 * Capturing only unicast traffic destined for this host is probably
7493 * better accomplished using a higher-layer filter.
7499 register struct block *b0;
7502 * Only some data link types support inbound/outbound qualifiers.
7506 b0 = gen_relation(BPF_JEQ,
7507 gen_load(Q_LINK, gen_loadi(0), 1),
7514 /* match outgoing packets */
7515 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
7517 /* match incoming packets */
7518 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
7523 /* match outgoing packets */
7524 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
7526 /* to filter on inbound traffic, invert the match */
7531 #ifdef HAVE_NET_PFVAR_H
7533 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
7534 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
7540 /* match outgoing packets */
7541 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
7543 /* match incoming packets */
7544 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
7548 case DLT_JUNIPER_MFR:
7549 case DLT_JUNIPER_MLFR:
7550 case DLT_JUNIPER_MLPPP:
7551 case DLT_JUNIPER_ATM1:
7552 case DLT_JUNIPER_ATM2:
7553 case DLT_JUNIPER_PPPOE:
7554 case DLT_JUNIPER_PPPOE_ATM:
7555 case DLT_JUNIPER_GGSN:
7556 case DLT_JUNIPER_ES:
7557 case DLT_JUNIPER_MONITOR:
7558 case DLT_JUNIPER_SERVICES:
7559 case DLT_JUNIPER_ETHER:
7560 case DLT_JUNIPER_PPP:
7561 case DLT_JUNIPER_FRELAY:
7562 case DLT_JUNIPER_CHDLC:
7563 case DLT_JUNIPER_VP:
7564 case DLT_JUNIPER_ST:
7565 case DLT_JUNIPER_ISM:
7566 case DLT_JUNIPER_VS:
7567 case DLT_JUNIPER_SRX_E2E:
7568 case DLT_JUNIPER_FIBRECHANNEL:
7569 case DLT_JUNIPER_ATM_CEMIC:
7571 /* juniper flags (including direction) are stored
7572 * the byte after the 3-byte magic number */
7574 /* match outgoing packets */
7575 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
7577 /* match incoming packets */
7578 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
7584 * If we have packet meta-data indicating a direction,
7585 * check it, otherwise give up as this link-layer type
7586 * has nothing in the packet data.
7588 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7590 * We infer that this is Linux with PF_PACKET support.
7591 * If this is a *live* capture, we can look at
7592 * special meta-data in the filter expression;
7593 * if it's a savefile, we can't.
7595 if (bpf_pcap->sf.rfile != NULL) {
7596 /* We have a FILE *, so this is a savefile */
7597 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7602 /* match outgoing packets */
7603 b0 = gen_cmp(OR_LINK, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
7606 /* to filter on inbound traffic, invert the match */
7609 #else /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7610 bpf_error("inbound/outbound not supported on linktype %d",
7614 #endif /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7619 #ifdef HAVE_NET_PFVAR_H
7620 /* PF firewall log matched interface */
7622 gen_pf_ifname(const char *ifname)
7627 if (linktype != DLT_PFLOG) {
7628 bpf_error("ifname supported only on PF linktype");
7631 len = sizeof(((struct pfloghdr *)0)->ifname);
7632 off = offsetof(struct pfloghdr, ifname);
7633 if (strlen(ifname) >= len) {
7634 bpf_error("ifname interface names can only be %d characters",
7638 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
7642 /* PF firewall log ruleset name */
7644 gen_pf_ruleset(char *ruleset)
7648 if (linktype != DLT_PFLOG) {
7649 bpf_error("ruleset supported only on PF linktype");
7653 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
7654 bpf_error("ruleset names can only be %ld characters",
7655 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
7659 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
7660 strlen(ruleset), (const u_char *)ruleset);
7664 /* PF firewall log rule number */
7670 if (linktype != DLT_PFLOG) {
7671 bpf_error("rnr supported only on PF linktype");
7675 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
7680 /* PF firewall log sub-rule number */
7682 gen_pf_srnr(int srnr)
7686 if (linktype != DLT_PFLOG) {
7687 bpf_error("srnr supported only on PF linktype");
7691 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
7696 /* PF firewall log reason code */
7698 gen_pf_reason(int reason)
7702 if (linktype != DLT_PFLOG) {
7703 bpf_error("reason supported only on PF linktype");
7707 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
7712 /* PF firewall log action */
7714 gen_pf_action(int action)
7718 if (linktype != DLT_PFLOG) {
7719 bpf_error("action supported only on PF linktype");
7723 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
7727 #else /* !HAVE_NET_PFVAR_H */
7729 gen_pf_ifname(const char *ifname)
7731 bpf_error("libpcap was compiled without pf support");
7737 gen_pf_ruleset(char *ruleset)
7739 bpf_error("libpcap was compiled on a machine without pf support");
7747 bpf_error("libpcap was compiled on a machine without pf support");
7753 gen_pf_srnr(int srnr)
7755 bpf_error("libpcap was compiled on a machine without pf support");
7761 gen_pf_reason(int reason)
7763 bpf_error("libpcap was compiled on a machine without pf support");
7769 gen_pf_action(int action)
7771 bpf_error("libpcap was compiled on a machine without pf support");
7775 #endif /* HAVE_NET_PFVAR_H */
7777 /* IEEE 802.11 wireless header */
7779 gen_p80211_type(int type, int mask)
7785 case DLT_IEEE802_11:
7786 case DLT_PRISM_HEADER:
7787 case DLT_IEEE802_11_RADIO_AVS:
7788 case DLT_IEEE802_11_RADIO:
7789 b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
7794 bpf_error("802.11 link-layer types supported only on 802.11");
7802 gen_p80211_fcdir(int fcdir)
7808 case DLT_IEEE802_11:
7809 case DLT_PRISM_HEADER:
7810 case DLT_IEEE802_11_RADIO_AVS:
7811 case DLT_IEEE802_11_RADIO:
7815 bpf_error("frame direction supported only with 802.11 headers");
7819 b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
7820 (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
7827 register const u_char *eaddr;
7833 case DLT_ARCNET_LINUX:
7834 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
7836 return (gen_ahostop(eaddr, (int)q.dir));
7838 bpf_error("ARCnet address used in non-arc expression");
7844 bpf_error("aid supported only on ARCnet");
7847 bpf_error("ARCnet address used in non-arc expression");
7852 static struct block *
7853 gen_ahostop(eaddr, dir)
7854 register const u_char *eaddr;
7857 register struct block *b0, *b1;
7860 /* src comes first, different from Ethernet */
7862 return gen_bcmp(OR_LINK, 0, 1, eaddr);
7865 return gen_bcmp(OR_LINK, 1, 1, eaddr);
7868 b0 = gen_ahostop(eaddr, Q_SRC);
7869 b1 = gen_ahostop(eaddr, Q_DST);
7875 b0 = gen_ahostop(eaddr, Q_SRC);
7876 b1 = gen_ahostop(eaddr, Q_DST);
7881 bpf_error("'addr1' is only supported on 802.11");
7885 bpf_error("'addr2' is only supported on 802.11");
7889 bpf_error("'addr3' is only supported on 802.11");
7893 bpf_error("'addr4' is only supported on 802.11");
7897 bpf_error("'ra' is only supported on 802.11");
7901 bpf_error("'ta' is only supported on 802.11");
7909 * support IEEE 802.1Q VLAN trunk over ethernet
7915 struct block *b0, *b1;
7917 /* can't check for VLAN-encapsulated packets inside MPLS */
7918 if (label_stack_depth > 0)
7919 bpf_error("no VLAN match after MPLS");
7922 * Check for a VLAN packet, and then change the offsets to point
7923 * to the type and data fields within the VLAN packet. Just
7924 * increment the offsets, so that we can support a hierarchy, e.g.
7925 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7928 * XXX - this is a bit of a kludge. If we were to split the
7929 * compiler into a parser that parses an expression and
7930 * generates an expression tree, and a code generator that
7931 * takes an expression tree (which could come from our
7932 * parser or from some other parser) and generates BPF code,
7933 * we could perhaps make the offsets parameters of routines
7934 * and, in the handler for an "AND" node, pass to subnodes
7935 * other than the VLAN node the adjusted offsets.
7937 * This would mean that "vlan" would, instead of changing the
7938 * behavior of *all* tests after it, change only the behavior
7939 * of tests ANDed with it. That would change the documented
7940 * semantics of "vlan", which might break some expressions.
7941 * However, it would mean that "(vlan and ip) or ip" would check
7942 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7943 * checking only for VLAN-encapsulated IP, so that could still
7944 * be considered worth doing; it wouldn't break expressions
7945 * that are of the form "vlan and ..." or "vlan N and ...",
7946 * which I suspect are the most common expressions involving
7947 * "vlan". "vlan or ..." doesn't necessarily do what the user
7948 * would really want, now, as all the "or ..." tests would
7949 * be done assuming a VLAN, even though the "or" could be viewed
7950 * as meaning "or, if this isn't a VLAN packet...".
7957 case DLT_NETANALYZER:
7958 case DLT_NETANALYZER_TRANSPARENT:
7959 /* check for VLAN, including QinQ */
7960 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7961 (bpf_int32)ETHERTYPE_8021Q);
7962 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7963 (bpf_int32)ETHERTYPE_8021QINQ);
7967 /* If a specific VLAN is requested, check VLAN id */
7968 if (vlan_num >= 0) {
7969 b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
7970 (bpf_int32)vlan_num, 0x0fff);
7984 bpf_error("no VLAN support for data link type %d",
7999 struct block *b0,*b1;
8002 * Change the offsets to point to the type and data fields within
8003 * the MPLS packet. Just increment the offsets, so that we
8004 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8005 * capture packets with an outer label of 100000 and an inner
8008 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8012 if (label_stack_depth > 0) {
8013 /* just match the bottom-of-stack bit clear */
8014 b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
8017 * Indicate that we're checking MPLS-encapsulated headers,
8018 * to make sure higher level code generators don't try to
8019 * match against IP-related protocols such as Q_ARP, Q_RARP
8024 case DLT_C_HDLC: /* fall through */
8026 case DLT_NETANALYZER:
8027 case DLT_NETANALYZER_TRANSPARENT:
8028 b0 = gen_linktype(ETHERTYPE_MPLS);
8032 b0 = gen_linktype(PPP_MPLS_UCAST);
8035 /* FIXME add other DLT_s ...
8036 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8037 * leave it for now */
8040 bpf_error("no MPLS support for data link type %d",
8048 /* If a specific MPLS label is requested, check it */
8049 if (label_num >= 0) {
8050 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
8051 b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
8052 0xfffff000); /* only compare the first 20 bits */
8059 label_stack_depth++;
8064 * Support PPPOE discovery and session.
8069 /* check for PPPoE discovery */
8070 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
8079 * Test against the PPPoE session link-layer type.
8081 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
8084 * Change the offsets to point to the type and data fields within
8085 * the PPP packet, and note that this is PPPoE rather than
8088 * XXX - this is a bit of a kludge. If we were to split the
8089 * compiler into a parser that parses an expression and
8090 * generates an expression tree, and a code generator that
8091 * takes an expression tree (which could come from our
8092 * parser or from some other parser) and generates BPF code,
8093 * we could perhaps make the offsets parameters of routines
8094 * and, in the handler for an "AND" node, pass to subnodes
8095 * other than the PPPoE node the adjusted offsets.
8097 * This would mean that "pppoes" would, instead of changing the
8098 * behavior of *all* tests after it, change only the behavior
8099 * of tests ANDed with it. That would change the documented
8100 * semantics of "pppoes", which might break some expressions.
8101 * However, it would mean that "(pppoes and ip) or ip" would check
8102 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8103 * checking only for VLAN-encapsulated IP, so that could still
8104 * be considered worth doing; it wouldn't break expressions
8105 * that are of the form "pppoes and ..." which I suspect are the
8106 * most common expressions involving "pppoes". "pppoes or ..."
8107 * doesn't necessarily do what the user would really want, now,
8108 * as all the "or ..." tests would be done assuming PPPoE, even
8109 * though the "or" could be viewed as meaning "or, if this isn't
8110 * a PPPoE packet...".
8112 orig_linktype = off_linktype; /* save original values */
8117 * The "network-layer" protocol is PPPoE, which has a 6-byte
8118 * PPPoE header, followed by a PPP packet.
8120 * There is no HDLC encapsulation for the PPP packet (it's
8121 * encapsulated in PPPoES instead), so the link-layer type
8122 * starts at the first byte of the PPP packet. For PPPoE,
8123 * that offset is relative to the beginning of the total
8124 * link-layer payload, including any 802.2 LLC header, so
8125 * it's 6 bytes past off_nl.
8127 off_linktype = off_nl + 6;
8130 * The network-layer offsets are relative to the beginning
8131 * of the MAC-layer payload; that's past the 6-byte
8132 * PPPoE header and the 2-byte PPP header.
8135 off_nl_nosnap = 6+2;
8141 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
8153 bpf_error("'vpi' supported only on raw ATM");
8154 if (off_vpi == (u_int)-1)
8156 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
8162 bpf_error("'vci' supported only on raw ATM");
8163 if (off_vci == (u_int)-1)
8165 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
8170 if (off_proto == (u_int)-1)
8171 abort(); /* XXX - this isn't on FreeBSD */
8172 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
8177 if (off_payload == (u_int)-1)
8179 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
8180 0xffffffff, jtype, reverse, jvalue);
8185 bpf_error("'callref' supported only on raw ATM");
8186 if (off_proto == (u_int)-1)
8188 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
8189 jtype, reverse, jvalue);
8199 gen_atmtype_abbrev(type)
8202 struct block *b0, *b1;
8207 /* Get all packets in Meta signalling Circuit */
8209 bpf_error("'metac' supported only on raw ATM");
8210 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8211 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
8216 /* Get all packets in Broadcast Circuit*/
8218 bpf_error("'bcc' supported only on raw ATM");
8219 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8220 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
8225 /* Get all cells in Segment OAM F4 circuit*/
8227 bpf_error("'oam4sc' supported only on raw ATM");
8228 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8229 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8234 /* Get all cells in End-to-End OAM F4 Circuit*/
8236 bpf_error("'oam4ec' supported only on raw ATM");
8237 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8238 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8243 /* Get all packets in connection Signalling Circuit */
8245 bpf_error("'sc' supported only on raw ATM");
8246 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8247 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
8252 /* Get all packets in ILMI Circuit */
8254 bpf_error("'ilmic' supported only on raw ATM");
8255 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8256 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
8261 /* Get all LANE packets */
8263 bpf_error("'lane' supported only on raw ATM");
8264 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
8267 * Arrange that all subsequent tests assume LANE
8268 * rather than LLC-encapsulated packets, and set
8269 * the offsets appropriately for LANE-encapsulated
8272 * "off_mac" is the offset of the Ethernet header,
8273 * which is 2 bytes past the ATM pseudo-header
8274 * (skipping the pseudo-header and 2-byte LE Client
8275 * field). The other offsets are Ethernet offsets
8276 * relative to "off_mac".
8279 off_mac = off_payload + 2; /* MAC header */
8280 off_linktype = off_mac + 12;
8281 off_macpl = off_mac + 14; /* Ethernet */
8282 off_nl = 0; /* Ethernet II */
8283 off_nl_nosnap = 3; /* 802.3+802.2 */
8287 /* Get all LLC-encapsulated packets */
8289 bpf_error("'llc' supported only on raw ATM");
8290 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
8301 * Filtering for MTP2 messages based on li value
8302 * FISU, length is null
8303 * LSSU, length is 1 or 2
8304 * MSU, length is 3 or more
8307 gen_mtp2type_abbrev(type)
8310 struct block *b0, *b1;
8315 if ( (linktype != DLT_MTP2) &&
8316 (linktype != DLT_ERF) &&
8317 (linktype != DLT_MTP2_WITH_PHDR) )
8318 bpf_error("'fisu' supported only on MTP2");
8319 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8320 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8324 if ( (linktype != DLT_MTP2) &&
8325 (linktype != DLT_ERF) &&
8326 (linktype != DLT_MTP2_WITH_PHDR) )
8327 bpf_error("'lssu' supported only on MTP2");
8328 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8329 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
8334 if ( (linktype != DLT_MTP2) &&
8335 (linktype != DLT_ERF) &&
8336 (linktype != DLT_MTP2_WITH_PHDR) )
8337 bpf_error("'msu' supported only on MTP2");
8338 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
8348 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
8355 bpf_u_int32 val1 , val2 , val3;
8357 switch (mtp3field) {
8360 if (off_sio == (u_int)-1)
8361 bpf_error("'sio' supported only on SS7");
8362 /* sio coded on 1 byte so max value 255 */
8364 bpf_error("sio value %u too big; max value = 255",
8366 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
8367 (u_int)jtype, reverse, (u_int)jvalue);
8371 if (off_opc == (u_int)-1)
8372 bpf_error("'opc' supported only on SS7");
8373 /* opc coded on 14 bits so max value 16383 */
8375 bpf_error("opc value %u too big; max value = 16383",
8377 /* the following instructions are made to convert jvalue
8378 * to the form used to write opc in an ss7 message*/
8379 val1 = jvalue & 0x00003c00;
8381 val2 = jvalue & 0x000003fc;
8383 val3 = jvalue & 0x00000003;
8385 jvalue = val1 + val2 + val3;
8386 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
8387 (u_int)jtype, reverse, (u_int)jvalue);
8391 if (off_dpc == (u_int)-1)
8392 bpf_error("'dpc' supported only on SS7");
8393 /* dpc coded on 14 bits so max value 16383 */
8395 bpf_error("dpc value %u too big; max value = 16383",
8397 /* the following instructions are made to convert jvalue
8398 * to the forme used to write dpc in an ss7 message*/
8399 val1 = jvalue & 0x000000ff;
8401 val2 = jvalue & 0x00003f00;
8403 jvalue = val1 + val2;
8404 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
8405 (u_int)jtype, reverse, (u_int)jvalue);
8409 if (off_sls == (u_int)-1)
8410 bpf_error("'sls' supported only on SS7");
8411 /* sls coded on 4 bits so max value 15 */
8413 bpf_error("sls value %u too big; max value = 15",
8415 /* the following instruction is made to convert jvalue
8416 * to the forme used to write sls in an ss7 message*/
8417 jvalue = jvalue << 4;
8418 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
8419 (u_int)jtype,reverse, (u_int)jvalue);
8428 static struct block *
8429 gen_msg_abbrev(type)
8435 * Q.2931 signalling protocol messages for handling virtual circuits
8436 * establishment and teardown
8441 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
8445 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
8449 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
8453 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
8457 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
8460 case A_RELEASE_DONE:
8461 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
8471 gen_atmmulti_abbrev(type)
8474 struct block *b0, *b1;
8480 bpf_error("'oam' supported only on raw ATM");
8481 b1 = gen_atmmulti_abbrev(A_OAMF4);
8486 bpf_error("'oamf4' supported only on raw ATM");
8488 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8489 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8491 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8497 * Get Q.2931 signalling messages for switched
8498 * virtual connection
8501 bpf_error("'connectmsg' supported only on raw ATM");
8502 b0 = gen_msg_abbrev(A_SETUP);
8503 b1 = gen_msg_abbrev(A_CALLPROCEED);
8505 b0 = gen_msg_abbrev(A_CONNECT);
8507 b0 = gen_msg_abbrev(A_CONNECTACK);
8509 b0 = gen_msg_abbrev(A_RELEASE);
8511 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8513 b0 = gen_atmtype_abbrev(A_SC);
8519 bpf_error("'metaconnect' supported only on raw ATM");
8520 b0 = gen_msg_abbrev(A_SETUP);
8521 b1 = gen_msg_abbrev(A_CALLPROCEED);
8523 b0 = gen_msg_abbrev(A_CONNECT);
8525 b0 = gen_msg_abbrev(A_RELEASE);
8527 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8529 b0 = gen_atmtype_abbrev(A_METAC);