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 #ifdef HAVE_NET_PFVAR_H
90 #include <sys/socket.h>
92 #include <net/pfvar.h>
93 #include <net/if_pflog.h>
96 #define offsetof(s, e) ((size_t)&((s *)0)->e)
100 #include <netdb.h> /* for "struct addrinfo" */
103 #include <pcap/namedb.h>
105 #define ETHERMTU 1500
108 #define IPPROTO_SCTP 132
111 #ifdef HAVE_OS_PROTO_H
112 #include "os-proto.h"
115 #define JMP(c) ((c)|BPF_JMP|BPF_K)
118 static jmp_buf top_ctx;
119 static pcap_t *bpf_pcap;
121 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
123 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
125 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
130 static int pcap_fddipad;
135 bpf_error(const char *fmt, ...)
140 if (bpf_pcap != NULL)
141 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
148 static void init_linktype(pcap_t *);
150 static void init_regs(void);
151 static int alloc_reg(void);
152 static void free_reg(int);
154 static struct block *root;
157 * Value passed to gen_load_a() to indicate what the offset argument
161 OR_PACKET, /* relative to the beginning of the packet */
162 OR_LINK, /* relative to the beginning of the link-layer header */
163 OR_MACPL, /* relative to the end of the MAC-layer header */
164 OR_NET, /* relative to the network-layer header */
165 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
166 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
167 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
172 * As errors are handled by a longjmp, anything allocated must be freed
173 * in the longjmp handler, so it must be reachable from that handler.
174 * One thing that's allocated is the result of pcap_nametoaddrinfo();
175 * it must be freed with freeaddrinfo(). This variable points to any
176 * addrinfo structure that would need to be freed.
178 static struct addrinfo *ai;
182 * We divy out chunks of memory rather than call malloc each time so
183 * we don't have to worry about leaking memory. It's probably
184 * not a big deal if all this memory was wasted but if this ever
185 * goes into a library that would probably not be a good idea.
187 * XXX - this *is* in a library....
190 #define CHUNK0SIZE 1024
196 static struct chunk chunks[NCHUNKS];
197 static int cur_chunk;
199 static void *newchunk(u_int);
200 static void freechunks(void);
201 static inline struct block *new_block(int);
202 static inline struct slist *new_stmt(int);
203 static struct block *gen_retblk(int);
204 static inline void syntax(void);
206 static void backpatch(struct block *, struct block *);
207 static void merge(struct block *, struct block *);
208 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
209 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
210 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
211 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
212 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
213 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
215 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
216 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
217 bpf_u_int32, bpf_u_int32, int, bpf_int32);
218 static struct slist *gen_load_llrel(u_int, u_int);
219 static struct slist *gen_load_macplrel(u_int, u_int);
220 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
221 static struct slist *gen_loadx_iphdrlen(void);
222 static struct block *gen_uncond(int);
223 static inline struct block *gen_true(void);
224 static inline struct block *gen_false(void);
225 static struct block *gen_ether_linktype(int);
226 static struct block *gen_ipnet_linktype(int);
227 static struct block *gen_linux_sll_linktype(int);
228 static struct slist *gen_load_prism_llprefixlen(void);
229 static struct slist *gen_load_avs_llprefixlen(void);
230 static struct slist *gen_load_radiotap_llprefixlen(void);
231 static struct slist *gen_load_ppi_llprefixlen(void);
232 static void insert_compute_vloffsets(struct block *);
233 static struct slist *gen_llprefixlen(void);
234 static struct slist *gen_off_macpl(void);
235 static int ethertype_to_ppptype(int);
236 static struct block *gen_linktype(int);
237 static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
238 static struct block *gen_llc_linktype(int);
239 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
241 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
243 static struct block *gen_ahostop(const u_char *, int);
244 static struct block *gen_ehostop(const u_char *, int);
245 static struct block *gen_fhostop(const u_char *, int);
246 static struct block *gen_thostop(const u_char *, int);
247 static struct block *gen_wlanhostop(const u_char *, int);
248 static struct block *gen_ipfchostop(const u_char *, int);
249 static struct block *gen_dnhostop(bpf_u_int32, int);
250 static struct block *gen_mpls_linktype(int);
251 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
253 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
256 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
258 static struct block *gen_ipfrag(void);
259 static struct block *gen_portatom(int, bpf_int32);
260 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
262 static struct block *gen_portatom6(int, bpf_int32);
263 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
265 struct block *gen_portop(int, int, int);
266 static struct block *gen_port(int, int, int);
267 struct block *gen_portrangeop(int, int, int, int);
268 static struct block *gen_portrange(int, int, int, int);
270 struct block *gen_portop6(int, int, int);
271 static struct block *gen_port6(int, int, int);
272 struct block *gen_portrangeop6(int, int, int, int);
273 static struct block *gen_portrange6(int, int, int, int);
275 static int lookup_proto(const char *, int);
276 static struct block *gen_protochain(int, int, int);
277 static struct block *gen_proto(int, int, int);
278 static struct slist *xfer_to_x(struct arth *);
279 static struct slist *xfer_to_a(struct arth *);
280 static struct block *gen_mac_multicast(int);
281 static struct block *gen_len(int, int);
282 static struct block *gen_check_802_11_data_frame(void);
284 static struct block *gen_ppi_dlt_check(void);
285 static struct block *gen_msg_abbrev(int type);
296 /* XXX Round up to nearest long. */
297 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
299 /* XXX Round up to structure boundary. */
303 cp = &chunks[cur_chunk];
304 if (n > cp->n_left) {
305 ++cp, k = ++cur_chunk;
307 bpf_error("out of memory");
308 size = CHUNK0SIZE << k;
309 cp->m = (void *)malloc(size);
311 bpf_error("out of memory");
312 memset((char *)cp->m, 0, size);
315 bpf_error("out of memory");
318 return (void *)((char *)cp->m + cp->n_left);
327 for (i = 0; i < NCHUNKS; ++i)
328 if (chunks[i].m != NULL) {
335 * A strdup whose allocations are freed after code generation is over.
339 register const char *s;
341 int n = strlen(s) + 1;
342 char *cp = newchunk(n);
348 static inline struct block *
354 p = (struct block *)newchunk(sizeof(*p));
361 static inline struct slist *
367 p = (struct slist *)newchunk(sizeof(*p));
373 static struct block *
377 struct block *b = new_block(BPF_RET|BPF_K);
386 bpf_error("syntax error in filter expression");
389 static bpf_u_int32 netmask;
394 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
395 const char *buf, int optimize, bpf_u_int32 mask);
398 pcap_compile(pcap_t *p, struct bpf_program *program,
399 const char *buf, int optimize, bpf_u_int32 mask)
403 EnterCriticalSection(&g_PcapCompileCriticalSection);
405 result = pcap_compile_unsafe(p, program, buf, optimize, mask);
407 LeaveCriticalSection(&g_PcapCompileCriticalSection);
413 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
414 const char *buf, int optimize, bpf_u_int32 mask)
417 pcap_compile(pcap_t *p, struct bpf_program *program,
418 const char *buf, int optimize, bpf_u_int32 mask)
422 const char * volatile xbuf = buf;
430 if (setjmp(top_ctx)) {
444 snaplen = pcap_snapshot(p);
446 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
447 "snaplen of 0 rejects all packets");
451 lex_init(xbuf ? xbuf : "");
459 root = gen_retblk(snaplen);
461 if (optimize && !no_optimize) {
464 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
465 bpf_error("expression rejects all packets");
467 program->bf_insns = icode_to_fcode(root, &len);
468 program->bf_len = len;
476 * entry point for using the compiler with no pcap open
477 * pass in all the stuff that is needed explicitly instead.
480 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
481 struct bpf_program *program,
482 const char *buf, int optimize, bpf_u_int32 mask)
487 p = pcap_open_dead(linktype_arg, snaplen_arg);
490 ret = pcap_compile(p, program, buf, optimize, mask);
496 * Clean up a "struct bpf_program" by freeing all the memory allocated
500 pcap_freecode(struct bpf_program *program)
503 if (program->bf_insns != NULL) {
504 free((char *)program->bf_insns);
505 program->bf_insns = NULL;
510 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
511 * which of the jt and jf fields has been resolved and which is a pointer
512 * back to another unresolved block (or nil). At least one of the fields
513 * in each block is already resolved.
516 backpatch(list, target)
517 struct block *list, *target;
534 * Merge the lists in b0 and b1, using the 'sense' field to indicate
535 * which of jt and jf is the link.
539 struct block *b0, *b1;
541 register struct block **p = &b0;
543 /* Find end of list. */
545 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
547 /* Concatenate the lists. */
555 struct block *ppi_dlt_check;
558 * Insert before the statements of the first (root) block any
559 * statements needed to load the lengths of any variable-length
560 * headers into registers.
562 * XXX - a fancier strategy would be to insert those before the
563 * statements of all blocks that use those lengths and that
564 * have no predecessors that use them, so that we only compute
565 * the lengths if we need them. There might be even better
566 * approaches than that.
568 * However, those strategies would be more complicated, and
569 * as we don't generate code to compute a length if the
570 * program has no tests that use the length, and as most
571 * tests will probably use those lengths, we would just
572 * postpone computing the lengths so that it's not done
573 * for tests that fail early, and it's not clear that's
576 insert_compute_vloffsets(p->head);
579 * For DLT_PPI captures, generate a check of the per-packet
580 * DLT value to make sure it's DLT_IEEE802_11.
582 ppi_dlt_check = gen_ppi_dlt_check();
583 if (ppi_dlt_check != NULL)
584 gen_and(ppi_dlt_check, p);
586 backpatch(p, gen_retblk(snaplen));
587 p->sense = !p->sense;
588 backpatch(p, gen_retblk(0));
594 struct block *b0, *b1;
596 backpatch(b0, b1->head);
597 b0->sense = !b0->sense;
598 b1->sense = !b1->sense;
600 b1->sense = !b1->sense;
606 struct block *b0, *b1;
608 b0->sense = !b0->sense;
609 backpatch(b0, b1->head);
610 b0->sense = !b0->sense;
619 b->sense = !b->sense;
622 static struct block *
623 gen_cmp(offrel, offset, size, v)
624 enum e_offrel offrel;
628 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
631 static struct block *
632 gen_cmp_gt(offrel, offset, size, v)
633 enum e_offrel offrel;
637 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
640 static struct block *
641 gen_cmp_ge(offrel, offset, size, v)
642 enum e_offrel offrel;
646 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
649 static struct block *
650 gen_cmp_lt(offrel, offset, size, v)
651 enum e_offrel offrel;
655 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
658 static struct block *
659 gen_cmp_le(offrel, offset, size, v)
660 enum e_offrel offrel;
664 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
667 static struct block *
668 gen_mcmp(offrel, offset, size, v, mask)
669 enum e_offrel offrel;
674 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
677 static struct block *
678 gen_bcmp(offrel, offset, size, v)
679 enum e_offrel offrel;
680 register u_int offset, size;
681 register const u_char *v;
683 register struct block *b, *tmp;
687 register const u_char *p = &v[size - 4];
688 bpf_int32 w = ((bpf_int32)p[0] << 24) |
689 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
691 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
698 register const u_char *p = &v[size - 2];
699 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
701 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
708 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
717 * AND the field of size "size" at offset "offset" relative to the header
718 * specified by "offrel" with "mask", and compare it with the value "v"
719 * with the test specified by "jtype"; if "reverse" is true, the test
720 * should test the opposite of "jtype".
722 static struct block *
723 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
724 enum e_offrel offrel;
726 bpf_u_int32 offset, size, mask, jtype;
729 struct slist *s, *s2;
732 s = gen_load_a(offrel, offset, size);
734 if (mask != 0xffffffff) {
735 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
740 b = new_block(JMP(jtype));
743 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
749 * Various code constructs need to know the layout of the data link
750 * layer. These variables give the necessary offsets from the beginning
751 * of the packet data.
755 * This is the offset of the beginning of the link-layer header from
756 * the beginning of the raw packet data.
758 * It's usually 0, except for 802.11 with a fixed-length radio header.
759 * (For 802.11 with a variable-length radio header, we have to generate
760 * code to compute that offset; off_ll is 0 in that case.)
765 * If there's a variable-length header preceding the link-layer header,
766 * "reg_off_ll" is the register number for a register containing the
767 * length of that header, and therefore the offset of the link-layer
768 * header from the beginning of the raw packet data. Otherwise,
769 * "reg_off_ll" is -1.
771 static int reg_off_ll;
774 * This is the offset of the beginning of the MAC-layer header from
775 * the beginning of the link-layer header.
776 * It's usually 0, except for ATM LANE, where it's the offset, relative
777 * to the beginning of the raw packet data, of the Ethernet header, and
778 * for Ethernet with various additional information.
780 static u_int off_mac;
783 * This is the offset of the beginning of the MAC-layer payload,
784 * from the beginning of the raw packet data.
786 * I.e., it's the sum of the length of the link-layer header (without,
787 * for example, any 802.2 LLC header, so it's the MAC-layer
788 * portion of that header), plus any prefix preceding the
791 static u_int off_macpl;
794 * This is 1 if the offset of the beginning of the MAC-layer payload
795 * from the beginning of the link-layer header is variable-length.
797 static int off_macpl_is_variable;
800 * If the link layer has variable_length headers, "reg_off_macpl"
801 * is the register number for a register containing the length of the
802 * link-layer header plus the length of any variable-length header
803 * preceding the link-layer header. Otherwise, "reg_off_macpl"
806 static int reg_off_macpl;
809 * "off_linktype" is the offset to information in the link-layer header
810 * giving the packet type. This offset is relative to the beginning
811 * of the link-layer header (i.e., it doesn't include off_ll).
813 * For Ethernet, it's the offset of the Ethernet type field.
815 * For link-layer types that always use 802.2 headers, it's the
816 * offset of the LLC header.
818 * For PPP, it's the offset of the PPP type field.
820 * For Cisco HDLC, it's the offset of the CHDLC type field.
822 * For BSD loopback, it's the offset of the AF_ value.
824 * For Linux cooked sockets, it's the offset of the type field.
826 * It's set to -1 for no encapsulation, in which case, IP is assumed.
828 static u_int off_linktype;
831 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
832 * checks to check the PPP header, assumed to follow a LAN-style link-
833 * layer header and a PPPoE session header.
835 static int is_pppoes = 0;
838 * TRUE if the link layer includes an ATM pseudo-header.
840 static int is_atm = 0;
843 * TRUE if "lane" appeared in the filter; it causes us to generate
844 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
846 static int is_lane = 0;
849 * These are offsets for the ATM pseudo-header.
851 static u_int off_vpi;
852 static u_int off_vci;
853 static u_int off_proto;
856 * These are offsets for the MTP2 fields.
861 * These are offsets for the MTP3 fields.
863 static u_int off_sio;
864 static u_int off_opc;
865 static u_int off_dpc;
866 static u_int off_sls;
869 * This is the offset of the first byte after the ATM pseudo_header,
870 * or -1 if there is no ATM pseudo-header.
872 static u_int off_payload;
875 * These are offsets to the beginning of the network-layer header.
876 * They are relative to the beginning of the MAC-layer payload (i.e.,
877 * they don't include off_ll or off_macpl).
879 * If the link layer never uses 802.2 LLC:
881 * "off_nl" and "off_nl_nosnap" are the same.
883 * If the link layer always uses 802.2 LLC:
885 * "off_nl" is the offset if there's a SNAP header following
888 * "off_nl_nosnap" is the offset if there's no SNAP header.
890 * If the link layer is Ethernet:
892 * "off_nl" is the offset if the packet is an Ethernet II packet
893 * (we assume no 802.3+802.2+SNAP);
895 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
896 * with an 802.2 header following it.
899 static u_int off_nl_nosnap;
907 linktype = pcap_datalink(p);
909 pcap_fddipad = p->fddipad;
913 * Assume it's not raw ATM with a pseudo-header, for now.
924 * And that we're not doing PPPoE.
929 * And assume we're not doing SS7.
938 * Also assume it's not 802.11.
942 off_macpl_is_variable = 0;
946 label_stack_depth = 0;
956 off_nl = 0; /* XXX in reality, variable! */
957 off_nl_nosnap = 0; /* no 802.2 LLC */
960 case DLT_ARCNET_LINUX:
963 off_nl = 0; /* XXX in reality, variable! */
964 off_nl_nosnap = 0; /* no 802.2 LLC */
969 off_macpl = 14; /* Ethernet header length */
970 off_nl = 0; /* Ethernet II */
971 off_nl_nosnap = 3; /* 802.3+802.2 */
976 * SLIP doesn't have a link level type. The 16 byte
977 * header is hacked into our SLIP driver.
982 off_nl_nosnap = 0; /* no 802.2 LLC */
986 /* XXX this may be the same as the DLT_PPP_BSDOS case */
991 off_nl_nosnap = 0; /* no 802.2 LLC */
999 off_nl_nosnap = 0; /* no 802.2 LLC */
1006 off_nl_nosnap = 0; /* no 802.2 LLC */
1011 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1012 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1016 off_nl_nosnap = 0; /* no 802.2 LLC */
1021 * This does no include the Ethernet header, and
1022 * only covers session state.
1027 off_nl_nosnap = 0; /* no 802.2 LLC */
1034 off_nl_nosnap = 0; /* no 802.2 LLC */
1039 * FDDI doesn't really have a link-level type field.
1040 * We set "off_linktype" to the offset of the LLC header.
1042 * To check for Ethernet types, we assume that SSAP = SNAP
1043 * is being used and pick out the encapsulated Ethernet type.
1044 * XXX - should we generate code to check for SNAP?
1048 off_linktype += pcap_fddipad;
1050 off_macpl = 13; /* FDDI MAC header length */
1052 off_macpl += pcap_fddipad;
1054 off_nl = 8; /* 802.2+SNAP */
1055 off_nl_nosnap = 3; /* 802.2 */
1060 * Token Ring doesn't really have a link-level type field.
1061 * We set "off_linktype" to the offset of the LLC header.
1063 * To check for Ethernet types, we assume that SSAP = SNAP
1064 * is being used and pick out the encapsulated Ethernet type.
1065 * XXX - should we generate code to check for SNAP?
1067 * XXX - the header is actually variable-length.
1068 * Some various Linux patched versions gave 38
1069 * as "off_linktype" and 40 as "off_nl"; however,
1070 * if a token ring packet has *no* routing
1071 * information, i.e. is not source-routed, the correct
1072 * values are 20 and 22, as they are in the vanilla code.
1074 * A packet is source-routed iff the uppermost bit
1075 * of the first byte of the source address, at an
1076 * offset of 8, has the uppermost bit set. If the
1077 * packet is source-routed, the total number of bytes
1078 * of routing information is 2 plus bits 0x1F00 of
1079 * the 16-bit value at an offset of 14 (shifted right
1080 * 8 - figure out which byte that is).
1083 off_macpl = 14; /* Token Ring MAC header length */
1084 off_nl = 8; /* 802.2+SNAP */
1085 off_nl_nosnap = 3; /* 802.2 */
1088 case DLT_IEEE802_11:
1089 case DLT_PRISM_HEADER:
1090 case DLT_IEEE802_11_RADIO_AVS:
1091 case DLT_IEEE802_11_RADIO:
1093 * 802.11 doesn't really have a link-level type field.
1094 * We set "off_linktype" to the offset of the LLC header.
1096 * To check for Ethernet types, we assume that SSAP = SNAP
1097 * is being used and pick out the encapsulated Ethernet type.
1098 * XXX - should we generate code to check for SNAP?
1100 * We also handle variable-length radio headers here.
1101 * The Prism header is in theory variable-length, but in
1102 * practice it's always 144 bytes long. However, some
1103 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1104 * sometimes or always supply an AVS header, so we
1105 * have to check whether the radio header is a Prism
1106 * header or an AVS header, so, in practice, it's
1110 off_macpl = 0; /* link-layer header is variable-length */
1111 off_macpl_is_variable = 1;
1112 off_nl = 8; /* 802.2+SNAP */
1113 off_nl_nosnap = 3; /* 802.2 */
1118 * At the moment we treat PPI the same way that we treat
1119 * normal Radiotap encoded packets. The difference is in
1120 * the function that generates the code at the beginning
1121 * to compute the header length. Since this code generator
1122 * of PPI supports bare 802.11 encapsulation only (i.e.
1123 * the encapsulated DLT should be DLT_IEEE802_11) we
1124 * generate code to check for this too.
1127 off_macpl = 0; /* link-layer header is variable-length */
1128 off_macpl_is_variable = 1;
1129 off_nl = 8; /* 802.2+SNAP */
1130 off_nl_nosnap = 3; /* 802.2 */
1133 case DLT_ATM_RFC1483:
1134 case DLT_ATM_CLIP: /* Linux ATM defines this */
1136 * assume routed, non-ISO PDUs
1137 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1139 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1140 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1141 * latter would presumably be treated the way PPPoE
1142 * should be, so you can do "pppoe and udp port 2049"
1143 * or "pppoa and tcp port 80" and have it check for
1144 * PPPo{A,E} and a PPP protocol of IP and....
1147 off_macpl = 0; /* packet begins with LLC header */
1148 off_nl = 8; /* 802.2+SNAP */
1149 off_nl_nosnap = 3; /* 802.2 */
1154 * Full Frontal ATM; you get AALn PDUs with an ATM
1158 off_vpi = SUNATM_VPI_POS;
1159 off_vci = SUNATM_VCI_POS;
1160 off_proto = PROTO_POS;
1161 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1162 off_payload = SUNATM_PKT_BEGIN_POS;
1163 off_linktype = off_payload;
1164 off_macpl = off_payload; /* if LLC-encapsulated */
1165 off_nl = 8; /* 802.2+SNAP */
1166 off_nl_nosnap = 3; /* 802.2 */
1175 off_nl_nosnap = 0; /* no 802.2 LLC */
1178 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1182 off_nl_nosnap = 0; /* no 802.2 LLC */
1187 * LocalTalk does have a 1-byte type field in the LLAP header,
1188 * but really it just indicates whether there is a "short" or
1189 * "long" DDP packet following.
1194 off_nl_nosnap = 0; /* no 802.2 LLC */
1197 case DLT_IP_OVER_FC:
1199 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1200 * link-level type field. We set "off_linktype" to the
1201 * offset of the LLC header.
1203 * To check for Ethernet types, we assume that SSAP = SNAP
1204 * is being used and pick out the encapsulated Ethernet type.
1205 * XXX - should we generate code to check for SNAP? RFC
1206 * 2625 says SNAP should be used.
1210 off_nl = 8; /* 802.2+SNAP */
1211 off_nl_nosnap = 3; /* 802.2 */
1216 * XXX - we should set this to handle SNAP-encapsulated
1217 * frames (NLPID of 0x80).
1222 off_nl_nosnap = 0; /* no 802.2 LLC */
1226 * the only BPF-interesting FRF.16 frames are non-control frames;
1227 * Frame Relay has a variable length link-layer
1228 * so lets start with offset 4 for now and increments later on (FIXME);
1234 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1237 case DLT_APPLE_IP_OVER_IEEE1394:
1241 off_nl_nosnap = 0; /* no 802.2 LLC */
1244 case DLT_SYMANTEC_FIREWALL:
1247 off_nl = 0; /* Ethernet II */
1248 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1251 #ifdef HAVE_NET_PFVAR_H
1254 off_macpl = PFLOG_HDRLEN;
1256 off_nl_nosnap = 0; /* no 802.2 LLC */
1260 case DLT_JUNIPER_MFR:
1261 case DLT_JUNIPER_MLFR:
1262 case DLT_JUNIPER_MLPPP:
1263 case DLT_JUNIPER_PPP:
1264 case DLT_JUNIPER_CHDLC:
1265 case DLT_JUNIPER_FRELAY:
1269 off_nl_nosnap = -1; /* no 802.2 LLC */
1272 case DLT_JUNIPER_ATM1:
1273 off_linktype = 4; /* in reality variable between 4-8 */
1274 off_macpl = 4; /* in reality variable between 4-8 */
1279 case DLT_JUNIPER_ATM2:
1280 off_linktype = 8; /* in reality variable between 8-12 */
1281 off_macpl = 8; /* in reality variable between 8-12 */
1286 /* frames captured on a Juniper PPPoE service PIC
1287 * contain raw ethernet frames */
1288 case DLT_JUNIPER_PPPOE:
1289 case DLT_JUNIPER_ETHER:
1292 off_nl = 18; /* Ethernet II */
1293 off_nl_nosnap = 21; /* 802.3+802.2 */
1296 case DLT_JUNIPER_PPPOE_ATM:
1300 off_nl_nosnap = -1; /* no 802.2 LLC */
1303 case DLT_JUNIPER_GGSN:
1307 off_nl_nosnap = -1; /* no 802.2 LLC */
1310 case DLT_JUNIPER_ES:
1312 off_macpl = -1; /* not really a network layer but raw IP addresses */
1313 off_nl = -1; /* not really a network layer but raw IP addresses */
1314 off_nl_nosnap = -1; /* no 802.2 LLC */
1317 case DLT_JUNIPER_MONITOR:
1320 off_nl = 0; /* raw IP/IP6 header */
1321 off_nl_nosnap = -1; /* no 802.2 LLC */
1324 case DLT_JUNIPER_SERVICES:
1326 off_macpl = -1; /* L3 proto location dep. on cookie type */
1327 off_nl = -1; /* L3 proto location dep. on cookie type */
1328 off_nl_nosnap = -1; /* no 802.2 LLC */
1331 case DLT_JUNIPER_VP:
1338 case DLT_JUNIPER_ST:
1345 case DLT_JUNIPER_ISM:
1352 case DLT_JUNIPER_VS:
1353 case DLT_JUNIPER_SRX_E2E:
1354 case DLT_JUNIPER_FIBRECHANNEL:
1355 case DLT_JUNIPER_ATM_CEMIC:
1374 case DLT_MTP2_WITH_PHDR:
1409 * Currently, only raw "link[N:M]" filtering is supported.
1411 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1413 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1414 off_nl_nosnap = -1; /* no 802.2 LLC */
1415 off_mac = 1; /* step over the kiss length byte */
1420 off_macpl = 24; /* ipnet header length */
1425 case DLT_NETANALYZER:
1426 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1427 off_linktype = 16; /* includes 4-byte pseudo-header */
1428 off_macpl = 18; /* pseudo-header+Ethernet header length */
1429 off_nl = 0; /* Ethernet II */
1430 off_nl_nosnap = 3; /* 802.3+802.2 */
1433 case DLT_NETANALYZER_TRANSPARENT:
1434 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1435 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1436 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1437 off_nl = 0; /* Ethernet II */
1438 off_nl_nosnap = 3; /* 802.3+802.2 */
1443 * For values in the range in which we've assigned new
1444 * DLT_ values, only raw "link[N:M]" filtering is supported.
1446 if (linktype >= DLT_MATCHING_MIN &&
1447 linktype <= DLT_MATCHING_MAX) {
1456 bpf_error("unknown data link type %d", linktype);
1461 * Load a value relative to the beginning of the link-layer header.
1462 * The link-layer header doesn't necessarily begin at the beginning
1463 * of the packet data; there might be a variable-length prefix containing
1464 * radio information.
1466 static struct slist *
1467 gen_load_llrel(offset, size)
1470 struct slist *s, *s2;
1472 s = gen_llprefixlen();
1475 * If "s" is non-null, it has code to arrange that the X register
1476 * contains the length of the prefix preceding the link-layer
1479 * Otherwise, the length of the prefix preceding the link-layer
1480 * header is "off_ll".
1484 * There's a variable-length prefix preceding the
1485 * link-layer header. "s" points to a list of statements
1486 * that put the length of that prefix into the X register.
1487 * do an indirect load, to use the X register as an offset.
1489 s2 = new_stmt(BPF_LD|BPF_IND|size);
1494 * There is no variable-length header preceding the
1495 * link-layer header; add in off_ll, which, if there's
1496 * a fixed-length header preceding the link-layer header,
1497 * is the length of that header.
1499 s = new_stmt(BPF_LD|BPF_ABS|size);
1500 s->s.k = offset + off_ll;
1506 * Load a value relative to the beginning of the MAC-layer payload.
1508 static struct slist *
1509 gen_load_macplrel(offset, size)
1512 struct slist *s, *s2;
1514 s = gen_off_macpl();
1517 * If s is non-null, the offset of the MAC-layer payload is
1518 * variable, and s points to a list of instructions that
1519 * arrange that the X register contains that offset.
1521 * Otherwise, the offset of the MAC-layer payload is constant,
1522 * and is in off_macpl.
1526 * The offset of the MAC-layer payload is in the X
1527 * register. Do an indirect load, to use the X register
1530 s2 = new_stmt(BPF_LD|BPF_IND|size);
1535 * The offset of the MAC-layer payload is constant,
1536 * and is in off_macpl; load the value at that offset
1537 * plus the specified offset.
1539 s = new_stmt(BPF_LD|BPF_ABS|size);
1540 s->s.k = off_macpl + offset;
1546 * Load a value relative to the beginning of the specified header.
1548 static struct slist *
1549 gen_load_a(offrel, offset, size)
1550 enum e_offrel offrel;
1553 struct slist *s, *s2;
1558 s = new_stmt(BPF_LD|BPF_ABS|size);
1563 s = gen_load_llrel(offset, size);
1567 s = gen_load_macplrel(offset, size);
1571 s = gen_load_macplrel(off_nl + offset, size);
1575 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1580 * Load the X register with the length of the IPv4 header
1581 * (plus the offset of the link-layer header, if it's
1582 * preceded by a variable-length header such as a radio
1583 * header), in bytes.
1585 s = gen_loadx_iphdrlen();
1588 * Load the item at {offset of the MAC-layer payload} +
1589 * {offset, relative to the start of the MAC-layer
1590 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1591 * {specified offset}.
1593 * (If the offset of the MAC-layer payload is variable,
1594 * it's included in the value in the X register, and
1597 s2 = new_stmt(BPF_LD|BPF_IND|size);
1598 s2->s.k = off_macpl + off_nl + offset;
1603 s = gen_load_macplrel(off_nl + 40 + offset, size);
1614 * Generate code to load into the X register the sum of the length of
1615 * the IPv4 header and any variable-length header preceding the link-layer
1618 static struct slist *
1619 gen_loadx_iphdrlen()
1621 struct slist *s, *s2;
1623 s = gen_off_macpl();
1626 * There's a variable-length prefix preceding the
1627 * link-layer header, or the link-layer header is itself
1628 * variable-length. "s" points to a list of statements
1629 * that put the offset of the MAC-layer payload into
1632 * The 4*([k]&0xf) addressing mode can't be used, as we
1633 * don't have a constant offset, so we have to load the
1634 * value in question into the A register and add to it
1635 * the value from the X register.
1637 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1640 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1643 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1648 * The A register now contains the length of the
1649 * IP header. We need to add to it the offset of
1650 * the MAC-layer payload, which is still in the X
1651 * register, and move the result into the X register.
1653 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1654 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1657 * There is no variable-length header preceding the
1658 * link-layer header, and the link-layer header is
1659 * fixed-length; load the length of the IPv4 header,
1660 * which is at an offset of off_nl from the beginning
1661 * of the MAC-layer payload, and thus at an offset
1662 * of off_mac_pl + off_nl from the beginning of the
1665 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1666 s->s.k = off_macpl + off_nl;
1671 static struct block *
1678 s = new_stmt(BPF_LD|BPF_IMM);
1680 b = new_block(JMP(BPF_JEQ));
1686 static inline struct block *
1689 return gen_uncond(1);
1692 static inline struct block *
1695 return gen_uncond(0);
1699 * Byte-swap a 32-bit number.
1700 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1701 * big-endian platforms.)
1703 #define SWAPLONG(y) \
1704 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1707 * Generate code to match a particular packet type.
1709 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1710 * value, if <= ETHERMTU. We use that to determine whether to
1711 * match the type/length field or to check the type/length field for
1712 * a value <= ETHERMTU to see whether it's a type field and then do
1713 * the appropriate test.
1715 static struct block *
1716 gen_ether_linktype(proto)
1719 struct block *b0, *b1;
1725 case LLCSAP_NETBEUI:
1727 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1728 * so we check the DSAP and SSAP.
1730 * LLCSAP_IP checks for IP-over-802.2, rather
1731 * than IP-over-Ethernet or IP-over-SNAP.
1733 * XXX - should we check both the DSAP and the
1734 * SSAP, like this, or should we check just the
1735 * DSAP, as we do for other types <= ETHERMTU
1736 * (i.e., other SAP values)?
1738 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1740 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1741 ((proto << 8) | proto));
1749 * Ethernet_II frames, which are Ethernet
1750 * frames with a frame type of ETHERTYPE_IPX;
1752 * Ethernet_802.3 frames, which are 802.3
1753 * frames (i.e., the type/length field is
1754 * a length field, <= ETHERMTU, rather than
1755 * a type field) with the first two bytes
1756 * after the Ethernet/802.3 header being
1759 * Ethernet_802.2 frames, which are 802.3
1760 * frames with an 802.2 LLC header and
1761 * with the IPX LSAP as the DSAP in the LLC
1764 * Ethernet_SNAP frames, which are 802.3
1765 * frames with an LLC header and a SNAP
1766 * header and with an OUI of 0x000000
1767 * (encapsulated Ethernet) and a protocol
1768 * ID of ETHERTYPE_IPX in the SNAP header.
1770 * XXX - should we generate the same code both
1771 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1775 * This generates code to check both for the
1776 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1778 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1779 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1783 * Now we add code to check for SNAP frames with
1784 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1786 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1790 * Now we generate code to check for 802.3
1791 * frames in general.
1793 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1797 * Now add the check for 802.3 frames before the
1798 * check for Ethernet_802.2 and Ethernet_802.3,
1799 * as those checks should only be done on 802.3
1800 * frames, not on Ethernet frames.
1805 * Now add the check for Ethernet_II frames, and
1806 * do that before checking for the other frame
1809 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1810 (bpf_int32)ETHERTYPE_IPX);
1814 case ETHERTYPE_ATALK:
1815 case ETHERTYPE_AARP:
1817 * EtherTalk (AppleTalk protocols on Ethernet link
1818 * layer) may use 802.2 encapsulation.
1822 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1823 * we check for an Ethernet type field less than
1824 * 1500, which means it's an 802.3 length field.
1826 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1830 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1831 * SNAP packets with an organization code of
1832 * 0x080007 (Apple, for Appletalk) and a protocol
1833 * type of ETHERTYPE_ATALK (Appletalk).
1835 * 802.2-encapsulated ETHERTYPE_AARP packets are
1836 * SNAP packets with an organization code of
1837 * 0x000000 (encapsulated Ethernet) and a protocol
1838 * type of ETHERTYPE_AARP (Appletalk ARP).
1840 if (proto == ETHERTYPE_ATALK)
1841 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1842 else /* proto == ETHERTYPE_AARP */
1843 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1847 * Check for Ethernet encapsulation (Ethertalk
1848 * phase 1?); we just check for the Ethernet
1851 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1857 if (proto <= ETHERMTU) {
1859 * This is an LLC SAP value, so the frames
1860 * that match would be 802.2 frames.
1861 * Check that the frame is an 802.2 frame
1862 * (i.e., that the length/type field is
1863 * a length field, <= ETHERMTU) and
1864 * then check the DSAP.
1866 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1868 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1874 * This is an Ethernet type, so compare
1875 * the length/type field with it (if
1876 * the frame is an 802.2 frame, the length
1877 * field will be <= ETHERMTU, and, as
1878 * "proto" is > ETHERMTU, this test
1879 * will fail and the frame won't match,
1880 * which is what we want).
1882 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1889 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1890 * or IPv6 then we have an error.
1892 static struct block *
1893 gen_ipnet_linktype(proto)
1899 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1900 (bpf_int32)IPH_AF_INET);
1903 case ETHERTYPE_IPV6:
1904 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1905 (bpf_int32)IPH_AF_INET6);
1916 * Generate code to match a particular packet type.
1918 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1919 * value, if <= ETHERMTU. We use that to determine whether to
1920 * match the type field or to check the type field for the special
1921 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1923 static struct block *
1924 gen_linux_sll_linktype(proto)
1927 struct block *b0, *b1;
1933 case LLCSAP_NETBEUI:
1935 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1936 * so we check the DSAP and SSAP.
1938 * LLCSAP_IP checks for IP-over-802.2, rather
1939 * than IP-over-Ethernet or IP-over-SNAP.
1941 * XXX - should we check both the DSAP and the
1942 * SSAP, like this, or should we check just the
1943 * DSAP, as we do for other types <= ETHERMTU
1944 * (i.e., other SAP values)?
1946 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1947 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1948 ((proto << 8) | proto));
1954 * Ethernet_II frames, which are Ethernet
1955 * frames with a frame type of ETHERTYPE_IPX;
1957 * Ethernet_802.3 frames, which have a frame
1958 * type of LINUX_SLL_P_802_3;
1960 * Ethernet_802.2 frames, which are 802.3
1961 * frames with an 802.2 LLC header (i.e, have
1962 * a frame type of LINUX_SLL_P_802_2) and
1963 * with the IPX LSAP as the DSAP in the LLC
1966 * Ethernet_SNAP frames, which are 802.3
1967 * frames with an LLC header and a SNAP
1968 * header and with an OUI of 0x000000
1969 * (encapsulated Ethernet) and a protocol
1970 * ID of ETHERTYPE_IPX in the SNAP header.
1972 * First, do the checks on LINUX_SLL_P_802_2
1973 * frames; generate the check for either
1974 * Ethernet_802.2 or Ethernet_SNAP frames, and
1975 * then put a check for LINUX_SLL_P_802_2 frames
1978 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1979 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
1981 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1985 * Now check for 802.3 frames and OR that with
1986 * the previous test.
1988 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1992 * Now add the check for Ethernet_II frames, and
1993 * do that before checking for the other frame
1996 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1997 (bpf_int32)ETHERTYPE_IPX);
2001 case ETHERTYPE_ATALK:
2002 case ETHERTYPE_AARP:
2004 * EtherTalk (AppleTalk protocols on Ethernet link
2005 * layer) may use 802.2 encapsulation.
2009 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2010 * we check for the 802.2 protocol type in the
2011 * "Ethernet type" field.
2013 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2016 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2017 * SNAP packets with an organization code of
2018 * 0x080007 (Apple, for Appletalk) and a protocol
2019 * type of ETHERTYPE_ATALK (Appletalk).
2021 * 802.2-encapsulated ETHERTYPE_AARP packets are
2022 * SNAP packets with an organization code of
2023 * 0x000000 (encapsulated Ethernet) and a protocol
2024 * type of ETHERTYPE_AARP (Appletalk ARP).
2026 if (proto == ETHERTYPE_ATALK)
2027 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2028 else /* proto == ETHERTYPE_AARP */
2029 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2033 * Check for Ethernet encapsulation (Ethertalk
2034 * phase 1?); we just check for the Ethernet
2037 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2043 if (proto <= ETHERMTU) {
2045 * This is an LLC SAP value, so the frames
2046 * that match would be 802.2 frames.
2047 * Check for the 802.2 protocol type
2048 * in the "Ethernet type" field, and
2049 * then check the DSAP.
2051 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2053 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2059 * This is an Ethernet type, so compare
2060 * the length/type field with it (if
2061 * the frame is an 802.2 frame, the length
2062 * field will be <= ETHERMTU, and, as
2063 * "proto" is > ETHERMTU, this test
2064 * will fail and the frame won't match,
2065 * which is what we want).
2067 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2073 static struct slist *
2074 gen_load_prism_llprefixlen()
2076 struct slist *s1, *s2;
2077 struct slist *sjeq_avs_cookie;
2078 struct slist *sjcommon;
2081 * This code is not compatible with the optimizer, as
2082 * we are generating jmp instructions within a normal
2083 * slist of instructions
2088 * Generate code to load the length of the radio header into
2089 * the register assigned to hold that length, if one has been
2090 * assigned. (If one hasn't been assigned, no code we've
2091 * generated uses that prefix, so we don't need to generate any
2094 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2095 * or always use the AVS header rather than the Prism header.
2096 * We load a 4-byte big-endian value at the beginning of the
2097 * raw packet data, and see whether, when masked with 0xFFFFF000,
2098 * it's equal to 0x80211000. If so, that indicates that it's
2099 * an AVS header (the masked-out bits are the version number).
2100 * Otherwise, it's a Prism header.
2102 * XXX - the Prism header is also, in theory, variable-length,
2103 * but no known software generates headers that aren't 144
2106 if (reg_off_ll != -1) {
2110 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2114 * AND it with 0xFFFFF000.
2116 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2117 s2->s.k = 0xFFFFF000;
2121 * Compare with 0x80211000.
2123 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2124 sjeq_avs_cookie->s.k = 0x80211000;
2125 sappend(s1, sjeq_avs_cookie);
2130 * The 4 bytes at an offset of 4 from the beginning of
2131 * the AVS header are the length of the AVS header.
2132 * That field is big-endian.
2134 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2137 sjeq_avs_cookie->s.jt = s2;
2140 * Now jump to the code to allocate a register
2141 * into which to save the header length and
2142 * store the length there. (The "jump always"
2143 * instruction needs to have the k field set;
2144 * it's added to the PC, so, as we're jumping
2145 * over a single instruction, it should be 1.)
2147 sjcommon = new_stmt(JMP(BPF_JA));
2149 sappend(s1, sjcommon);
2152 * Now for the code that handles the Prism header.
2153 * Just load the length of the Prism header (144)
2154 * into the A register. Have the test for an AVS
2155 * header branch here if we don't have an AVS header.
2157 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2160 sjeq_avs_cookie->s.jf = s2;
2163 * Now allocate a register to hold that value and store
2164 * it. The code for the AVS header will jump here after
2165 * loading the length of the AVS header.
2167 s2 = new_stmt(BPF_ST);
2168 s2->s.k = reg_off_ll;
2170 sjcommon->s.jf = s2;
2173 * Now move it into the X register.
2175 s2 = new_stmt(BPF_MISC|BPF_TAX);
2183 static struct slist *
2184 gen_load_avs_llprefixlen()
2186 struct slist *s1, *s2;
2189 * Generate code to load the length of the AVS header into
2190 * the register assigned to hold that length, if one has been
2191 * assigned. (If one hasn't been assigned, no code we've
2192 * generated uses that prefix, so we don't need to generate any
2195 if (reg_off_ll != -1) {
2197 * The 4 bytes at an offset of 4 from the beginning of
2198 * the AVS header are the length of the AVS header.
2199 * That field is big-endian.
2201 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2205 * Now allocate a register to hold that value and store
2208 s2 = new_stmt(BPF_ST);
2209 s2->s.k = reg_off_ll;
2213 * Now move it into the X register.
2215 s2 = new_stmt(BPF_MISC|BPF_TAX);
2223 static struct slist *
2224 gen_load_radiotap_llprefixlen()
2226 struct slist *s1, *s2;
2229 * Generate code to load the length of the radiotap header into
2230 * the register assigned to hold that length, if one has been
2231 * assigned. (If one hasn't been assigned, no code we've
2232 * generated uses that prefix, so we don't need to generate any
2235 if (reg_off_ll != -1) {
2237 * The 2 bytes at offsets of 2 and 3 from the beginning
2238 * of the radiotap header are the length of the radiotap
2239 * header; unfortunately, it's little-endian, so we have
2240 * to load it a byte at a time and construct the value.
2244 * Load the high-order byte, at an offset of 3, shift it
2245 * left a byte, and put the result in the X register.
2247 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2249 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2252 s2 = new_stmt(BPF_MISC|BPF_TAX);
2256 * Load the next byte, at an offset of 2, and OR the
2257 * value from the X register into it.
2259 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2262 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2266 * Now allocate a register to hold that value and store
2269 s2 = new_stmt(BPF_ST);
2270 s2->s.k = reg_off_ll;
2274 * Now move it into the X register.
2276 s2 = new_stmt(BPF_MISC|BPF_TAX);
2285 * At the moment we treat PPI as normal Radiotap encoded
2286 * packets. The difference is in the function that generates
2287 * the code at the beginning to compute the header length.
2288 * Since this code generator of PPI supports bare 802.11
2289 * encapsulation only (i.e. the encapsulated DLT should be
2290 * DLT_IEEE802_11) we generate code to check for this too;
2291 * that's done in finish_parse().
2293 static struct slist *
2294 gen_load_ppi_llprefixlen()
2296 struct slist *s1, *s2;
2299 * Generate code to load the length of the radiotap header
2300 * into the register assigned to hold that length, if one has
2303 if (reg_off_ll != -1) {
2305 * The 2 bytes at offsets of 2 and 3 from the beginning
2306 * of the radiotap header are the length of the radiotap
2307 * header; unfortunately, it's little-endian, so we have
2308 * to load it a byte at a time and construct the value.
2312 * Load the high-order byte, at an offset of 3, shift it
2313 * left a byte, and put the result in the X register.
2315 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2317 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2320 s2 = new_stmt(BPF_MISC|BPF_TAX);
2324 * Load the next byte, at an offset of 2, and OR the
2325 * value from the X register into it.
2327 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2330 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2334 * Now allocate a register to hold that value and store
2337 s2 = new_stmt(BPF_ST);
2338 s2->s.k = reg_off_ll;
2342 * Now move it into the X register.
2344 s2 = new_stmt(BPF_MISC|BPF_TAX);
2353 * Load a value relative to the beginning of the link-layer header after the 802.11
2354 * header, i.e. LLC_SNAP.
2355 * The link-layer header doesn't necessarily begin at the beginning
2356 * of the packet data; there might be a variable-length prefix containing
2357 * radio information.
2359 static struct slist *
2360 gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2363 struct slist *sjset_data_frame_1;
2364 struct slist *sjset_data_frame_2;
2365 struct slist *sjset_qos;
2366 struct slist *sjset_radiotap_flags;
2367 struct slist *sjset_radiotap_tsft;
2368 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2369 struct slist *s_roundup;
2371 if (reg_off_macpl == -1) {
2373 * No register has been assigned to the offset of
2374 * the MAC-layer payload, which means nobody needs
2375 * it; don't bother computing it - just return
2376 * what we already have.
2382 * This code is not compatible with the optimizer, as
2383 * we are generating jmp instructions within a normal
2384 * slist of instructions
2389 * If "s" is non-null, it has code to arrange that the X register
2390 * contains the length of the prefix preceding the link-layer
2393 * Otherwise, the length of the prefix preceding the link-layer
2394 * header is "off_ll".
2398 * There is no variable-length header preceding the
2399 * link-layer header.
2401 * Load the length of the fixed-length prefix preceding
2402 * the link-layer header (if any) into the X register,
2403 * and store it in the reg_off_macpl register.
2404 * That length is off_ll.
2406 s = new_stmt(BPF_LDX|BPF_IMM);
2411 * The X register contains the offset of the beginning of the
2412 * link-layer header; add 24, which is the minimum length
2413 * of the MAC header for a data frame, to that, and store it
2414 * in reg_off_macpl, and then load the Frame Control field,
2415 * which is at the offset in the X register, with an indexed load.
2417 s2 = new_stmt(BPF_MISC|BPF_TXA);
2419 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2422 s2 = new_stmt(BPF_ST);
2423 s2->s.k = reg_off_macpl;
2426 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2431 * Check the Frame Control field to see if this is a data frame;
2432 * a data frame has the 0x08 bit (b3) in that field set and the
2433 * 0x04 bit (b2) clear.
2435 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2436 sjset_data_frame_1->s.k = 0x08;
2437 sappend(s, sjset_data_frame_1);
2440 * If b3 is set, test b2, otherwise go to the first statement of
2441 * the rest of the program.
2443 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2444 sjset_data_frame_2->s.k = 0x04;
2445 sappend(s, sjset_data_frame_2);
2446 sjset_data_frame_1->s.jf = snext;
2449 * If b2 is not set, this is a data frame; test the QoS bit.
2450 * Otherwise, go to the first statement of the rest of the
2453 sjset_data_frame_2->s.jt = snext;
2454 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2455 sjset_qos->s.k = 0x80; /* QoS bit */
2456 sappend(s, sjset_qos);
2459 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2461 * Otherwise, go to the first statement of the rest of the
2464 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2465 s2->s.k = reg_off_macpl;
2467 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2470 s2 = new_stmt(BPF_ST);
2471 s2->s.k = reg_off_macpl;
2475 * If we have a radiotap header, look at it to see whether
2476 * there's Atheros padding between the MAC-layer header
2479 * Note: all of the fields in the radiotap header are
2480 * little-endian, so we byte-swap all of the values
2481 * we test against, as they will be loaded as big-endian
2484 if (linktype == DLT_IEEE802_11_RADIO) {
2486 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2487 * in the presence flag?
2489 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2493 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2494 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2495 sappend(s, sjset_radiotap_flags);
2498 * If not, skip all of this.
2500 sjset_radiotap_flags->s.jf = snext;
2503 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2505 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2506 new_stmt(JMP(BPF_JSET));
2507 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2508 sappend(s, sjset_radiotap_tsft);
2511 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2512 * at an offset of 16 from the beginning of the raw packet
2513 * data (8 bytes for the radiotap header and 8 bytes for
2516 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2519 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2523 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2524 sjset_tsft_datapad->s.k = 0x20;
2525 sappend(s, sjset_tsft_datapad);
2528 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2529 * at an offset of 8 from the beginning of the raw packet
2530 * data (8 bytes for the radiotap header).
2532 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2535 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2539 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2540 sjset_notsft_datapad->s.k = 0x20;
2541 sappend(s, sjset_notsft_datapad);
2544 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2545 * set, round the length of the 802.11 header to
2546 * a multiple of 4. Do that by adding 3 and then
2547 * dividing by and multiplying by 4, which we do by
2550 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2551 s_roundup->s.k = reg_off_macpl;
2552 sappend(s, s_roundup);
2553 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2556 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2559 s2 = new_stmt(BPF_ST);
2560 s2->s.k = reg_off_macpl;
2563 sjset_tsft_datapad->s.jt = s_roundup;
2564 sjset_tsft_datapad->s.jf = snext;
2565 sjset_notsft_datapad->s.jt = s_roundup;
2566 sjset_notsft_datapad->s.jf = snext;
2568 sjset_qos->s.jf = snext;
2574 insert_compute_vloffsets(b)
2580 * For link-layer types that have a variable-length header
2581 * preceding the link-layer header, generate code to load
2582 * the offset of the link-layer header into the register
2583 * assigned to that offset, if any.
2587 case DLT_PRISM_HEADER:
2588 s = gen_load_prism_llprefixlen();
2591 case DLT_IEEE802_11_RADIO_AVS:
2592 s = gen_load_avs_llprefixlen();
2595 case DLT_IEEE802_11_RADIO:
2596 s = gen_load_radiotap_llprefixlen();
2600 s = gen_load_ppi_llprefixlen();
2609 * For link-layer types that have a variable-length link-layer
2610 * header, generate code to load the offset of the MAC-layer
2611 * payload into the register assigned to that offset, if any.
2615 case DLT_IEEE802_11:
2616 case DLT_PRISM_HEADER:
2617 case DLT_IEEE802_11_RADIO_AVS:
2618 case DLT_IEEE802_11_RADIO:
2620 s = gen_load_802_11_header_len(s, b->stmts);
2625 * If we have any offset-loading code, append all the
2626 * existing statements in the block to those statements,
2627 * and make the resulting list the list of statements
2631 sappend(s, b->stmts);
2636 static struct block *
2637 gen_ppi_dlt_check(void)
2639 struct slist *s_load_dlt;
2642 if (linktype == DLT_PPI)
2644 /* Create the statements that check for the DLT
2646 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2647 s_load_dlt->s.k = 4;
2649 b = new_block(JMP(BPF_JEQ));
2651 b->stmts = s_load_dlt;
2652 b->s.k = SWAPLONG(DLT_IEEE802_11);
2662 static struct slist *
2663 gen_prism_llprefixlen(void)
2667 if (reg_off_ll == -1) {
2669 * We haven't yet assigned a register for the length
2670 * of the radio header; allocate one.
2672 reg_off_ll = alloc_reg();
2676 * Load the register containing the radio length
2677 * into the X register.
2679 s = new_stmt(BPF_LDX|BPF_MEM);
2680 s->s.k = reg_off_ll;
2684 static struct slist *
2685 gen_avs_llprefixlen(void)
2689 if (reg_off_ll == -1) {
2691 * We haven't yet assigned a register for the length
2692 * of the AVS header; allocate one.
2694 reg_off_ll = alloc_reg();
2698 * Load the register containing the AVS length
2699 * into the X register.
2701 s = new_stmt(BPF_LDX|BPF_MEM);
2702 s->s.k = reg_off_ll;
2706 static struct slist *
2707 gen_radiotap_llprefixlen(void)
2711 if (reg_off_ll == -1) {
2713 * We haven't yet assigned a register for the length
2714 * of the radiotap header; allocate one.
2716 reg_off_ll = alloc_reg();
2720 * Load the register containing the radiotap length
2721 * into the X register.
2723 s = new_stmt(BPF_LDX|BPF_MEM);
2724 s->s.k = reg_off_ll;
2729 * At the moment we treat PPI as normal Radiotap encoded
2730 * packets. The difference is in the function that generates
2731 * the code at the beginning to compute the header length.
2732 * Since this code generator of PPI supports bare 802.11
2733 * encapsulation only (i.e. the encapsulated DLT should be
2734 * DLT_IEEE802_11) we generate code to check for this too.
2736 static struct slist *
2737 gen_ppi_llprefixlen(void)
2741 if (reg_off_ll == -1) {
2743 * We haven't yet assigned a register for the length
2744 * of the radiotap header; allocate one.
2746 reg_off_ll = alloc_reg();
2750 * Load the register containing the PPI length
2751 * into the X register.
2753 s = new_stmt(BPF_LDX|BPF_MEM);
2754 s->s.k = reg_off_ll;
2759 * Generate code to compute the link-layer header length, if necessary,
2760 * putting it into the X register, and to return either a pointer to a
2761 * "struct slist" for the list of statements in that code, or NULL if
2762 * no code is necessary.
2764 static struct slist *
2765 gen_llprefixlen(void)
2769 case DLT_PRISM_HEADER:
2770 return gen_prism_llprefixlen();
2772 case DLT_IEEE802_11_RADIO_AVS:
2773 return gen_avs_llprefixlen();
2775 case DLT_IEEE802_11_RADIO:
2776 return gen_radiotap_llprefixlen();
2779 return gen_ppi_llprefixlen();
2787 * Generate code to load the register containing the offset of the
2788 * MAC-layer payload into the X register; if no register for that offset
2789 * has been allocated, allocate it first.
2791 static struct slist *
2796 if (off_macpl_is_variable) {
2797 if (reg_off_macpl == -1) {
2799 * We haven't yet assigned a register for the offset
2800 * of the MAC-layer payload; allocate one.
2802 reg_off_macpl = alloc_reg();
2806 * Load the register containing the offset of the MAC-layer
2807 * payload into the X register.
2809 s = new_stmt(BPF_LDX|BPF_MEM);
2810 s->s.k = reg_off_macpl;
2814 * That offset isn't variable, so we don't need to
2815 * generate any code.
2822 * Map an Ethernet type to the equivalent PPP type.
2825 ethertype_to_ppptype(proto)
2835 case ETHERTYPE_IPV6:
2844 case ETHERTYPE_ATALK:
2858 * I'm assuming the "Bridging PDU"s that go
2859 * over PPP are Spanning Tree Protocol
2873 * Generate code to match a particular packet type by matching the
2874 * link-layer type field or fields in the 802.2 LLC header.
2876 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2877 * value, if <= ETHERMTU.
2879 static struct block *
2883 struct block *b0, *b1, *b2;
2885 /* are we checking MPLS-encapsulated packets? */
2886 if (label_stack_depth > 0) {
2890 /* FIXME add other L3 proto IDs */
2891 return gen_mpls_linktype(Q_IP);
2893 case ETHERTYPE_IPV6:
2895 /* FIXME add other L3 proto IDs */
2896 return gen_mpls_linktype(Q_IPV6);
2899 bpf_error("unsupported protocol over mpls");
2905 * Are we testing PPPoE packets?
2909 * The PPPoE session header is part of the
2910 * MAC-layer payload, so all references
2911 * should be relative to the beginning of
2916 * We use Ethernet protocol types inside libpcap;
2917 * map them to the corresponding PPP protocol types.
2919 proto = ethertype_to_ppptype(proto);
2920 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2926 case DLT_NETANALYZER:
2927 case DLT_NETANALYZER_TRANSPARENT:
2928 return gen_ether_linktype(proto);
2936 proto = (proto << 8 | LLCSAP_ISONS);
2940 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2947 case DLT_IEEE802_11:
2948 case DLT_PRISM_HEADER:
2949 case DLT_IEEE802_11_RADIO_AVS:
2950 case DLT_IEEE802_11_RADIO:
2953 * Check that we have a data frame.
2955 b0 = gen_check_802_11_data_frame();
2958 * Now check for the specified link-layer type.
2960 b1 = gen_llc_linktype(proto);
2968 * XXX - check for asynchronous frames, as per RFC 1103.
2970 return gen_llc_linktype(proto);
2976 * XXX - check for LLC PDUs, as per IEEE 802.5.
2978 return gen_llc_linktype(proto);
2982 case DLT_ATM_RFC1483:
2984 case DLT_IP_OVER_FC:
2985 return gen_llc_linktype(proto);
2991 * If "is_lane" is set, check for a LANE-encapsulated
2992 * version of this protocol, otherwise check for an
2993 * LLC-encapsulated version of this protocol.
2995 * We assume LANE means Ethernet, not Token Ring.
2999 * Check that the packet doesn't begin with an
3000 * LE Control marker. (We've already generated
3003 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3008 * Now generate an Ethernet test.
3010 b1 = gen_ether_linktype(proto);
3015 * Check for LLC encapsulation and then check the
3018 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3019 b1 = gen_llc_linktype(proto);
3027 return gen_linux_sll_linktype(proto);
3032 case DLT_SLIP_BSDOS:
3035 * These types don't provide any type field; packets
3036 * are always IPv4 or IPv6.
3038 * XXX - for IPv4, check for a version number of 4, and,
3039 * for IPv6, check for a version number of 6?
3044 /* Check for a version number of 4. */
3045 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3047 case ETHERTYPE_IPV6:
3048 /* Check for a version number of 6. */
3049 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3053 return gen_false(); /* always false */
3060 * Raw IPv4, so no type field.
3062 if (proto == ETHERTYPE_IP)
3063 return gen_true(); /* always true */
3065 /* Checking for something other than IPv4; always false */
3072 * Raw IPv6, so no type field.
3075 if (proto == ETHERTYPE_IPV6)
3076 return gen_true(); /* always true */
3079 /* Checking for something other than IPv6; always false */
3086 case DLT_PPP_SERIAL:
3089 * We use Ethernet protocol types inside libpcap;
3090 * map them to the corresponding PPP protocol types.
3092 proto = ethertype_to_ppptype(proto);
3093 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3099 * We use Ethernet protocol types inside libpcap;
3100 * map them to the corresponding PPP protocol types.
3106 * Also check for Van Jacobson-compressed IP.
3107 * XXX - do this for other forms of PPP?
3109 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3110 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3112 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3117 proto = ethertype_to_ppptype(proto);
3118 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3128 * For DLT_NULL, the link-layer header is a 32-bit
3129 * word containing an AF_ value in *host* byte order,
3130 * and for DLT_ENC, the link-layer header begins
3131 * with a 32-bit work containing an AF_ value in
3134 * In addition, if we're reading a saved capture file,
3135 * the host byte order in the capture may not be the
3136 * same as the host byte order on this machine.
3138 * For DLT_LOOP, the link-layer header is a 32-bit
3139 * word containing an AF_ value in *network* byte order.
3141 * XXX - AF_ values may, unfortunately, be platform-
3142 * dependent; for example, FreeBSD's AF_INET6 is 24
3143 * whilst NetBSD's and OpenBSD's is 26.
3145 * This means that, when reading a capture file, just
3146 * checking for our AF_INET6 value won't work if the
3147 * capture file came from another OS.
3156 case ETHERTYPE_IPV6:
3163 * Not a type on which we support filtering.
3164 * XXX - support those that have AF_ values
3165 * #defined on this platform, at least?
3170 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3172 * The AF_ value is in host byte order, but
3173 * the BPF interpreter will convert it to
3174 * network byte order.
3176 * If this is a save file, and it's from a
3177 * machine with the opposite byte order to
3178 * ours, we byte-swap the AF_ value.
3180 * Then we run it through "htonl()", and
3181 * generate code to compare against the result.
3183 if (bpf_pcap->sf.rfile != NULL &&
3184 bpf_pcap->sf.swapped)
3185 proto = SWAPLONG(proto);
3186 proto = htonl(proto);
3188 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3190 #ifdef HAVE_NET_PFVAR_H
3193 * af field is host byte order in contrast to the rest of
3196 if (proto == ETHERTYPE_IP)
3197 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3198 BPF_B, (bpf_int32)AF_INET));
3200 else if (proto == ETHERTYPE_IPV6)
3201 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3202 BPF_B, (bpf_int32)AF_INET6));
3208 #endif /* HAVE_NET_PFVAR_H */
3211 case DLT_ARCNET_LINUX:
3213 * XXX should we check for first fragment if the protocol
3222 case ETHERTYPE_IPV6:
3223 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3224 (bpf_int32)ARCTYPE_INET6));
3228 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3229 (bpf_int32)ARCTYPE_IP);
3230 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3231 (bpf_int32)ARCTYPE_IP_OLD);
3236 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3237 (bpf_int32)ARCTYPE_ARP);
3238 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3239 (bpf_int32)ARCTYPE_ARP_OLD);
3243 case ETHERTYPE_REVARP:
3244 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3245 (bpf_int32)ARCTYPE_REVARP));
3247 case ETHERTYPE_ATALK:
3248 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3249 (bpf_int32)ARCTYPE_ATALK));
3256 case ETHERTYPE_ATALK:
3266 * XXX - assumes a 2-byte Frame Relay header with
3267 * DLCI and flags. What if the address is longer?
3273 * Check for the special NLPID for IP.
3275 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3278 case ETHERTYPE_IPV6:
3280 * Check for the special NLPID for IPv6.
3282 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3287 * Check for several OSI protocols.
3289 * Frame Relay packets typically have an OSI
3290 * NLPID at the beginning; we check for each
3293 * What we check for is the NLPID and a frame
3294 * control field of UI, i.e. 0x03 followed
3297 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3298 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3299 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3311 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3313 case DLT_JUNIPER_MFR:
3314 case DLT_JUNIPER_MLFR:
3315 case DLT_JUNIPER_MLPPP:
3316 case DLT_JUNIPER_ATM1:
3317 case DLT_JUNIPER_ATM2:
3318 case DLT_JUNIPER_PPPOE:
3319 case DLT_JUNIPER_PPPOE_ATM:
3320 case DLT_JUNIPER_GGSN:
3321 case DLT_JUNIPER_ES:
3322 case DLT_JUNIPER_MONITOR:
3323 case DLT_JUNIPER_SERVICES:
3324 case DLT_JUNIPER_ETHER:
3325 case DLT_JUNIPER_PPP:
3326 case DLT_JUNIPER_FRELAY:
3327 case DLT_JUNIPER_CHDLC:
3328 case DLT_JUNIPER_VP:
3329 case DLT_JUNIPER_ST:
3330 case DLT_JUNIPER_ISM:
3331 case DLT_JUNIPER_VS:
3332 case DLT_JUNIPER_SRX_E2E:
3333 case DLT_JUNIPER_FIBRECHANNEL:
3334 case DLT_JUNIPER_ATM_CEMIC:
3336 /* just lets verify the magic number for now -
3337 * on ATM we may have up to 6 different encapsulations on the wire
3338 * and need a lot of heuristics to figure out that the payload
3341 * FIXME encapsulation specific BPF_ filters
3343 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3346 return gen_ipnet_linktype(proto);
3348 case DLT_LINUX_IRDA:
3349 bpf_error("IrDA link-layer type filtering not implemented");
3352 bpf_error("DOCSIS link-layer type filtering not implemented");
3355 case DLT_MTP2_WITH_PHDR:
3356 bpf_error("MTP2 link-layer type filtering not implemented");
3359 bpf_error("ERF link-layer type filtering not implemented");
3363 bpf_error("PFSYNC link-layer type filtering not implemented");
3366 case DLT_LINUX_LAPD:
3367 bpf_error("LAPD link-layer type filtering not implemented");
3371 case DLT_USB_LINUX_MMAPPED:
3372 bpf_error("USB link-layer type filtering not implemented");
3374 case DLT_BLUETOOTH_HCI_H4:
3375 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3376 bpf_error("Bluetooth link-layer type filtering not implemented");
3379 case DLT_CAN_SOCKETCAN:
3380 bpf_error("CAN link-layer type filtering not implemented");
3382 case DLT_IEEE802_15_4:
3383 case DLT_IEEE802_15_4_LINUX:
3384 case DLT_IEEE802_15_4_NONASK_PHY:
3385 case DLT_IEEE802_15_4_NOFCS:
3386 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3388 case DLT_IEEE802_16_MAC_CPS_RADIO:
3389 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3392 bpf_error("SITA link-layer type filtering not implemented");
3395 bpf_error("RAIF1 link-layer type filtering not implemented");
3398 bpf_error("IPMB link-layer type filtering not implemented");
3401 bpf_error("AX.25 link-layer type filtering not implemented");
3405 * All the types that have no encapsulation should either be
3406 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3407 * all packets are IP packets, or should be handled in some
3408 * special case, if none of them are (if some are and some
3409 * aren't, the lack of encapsulation is a problem, as we'd
3410 * have to find some other way of determining the packet type).
3412 * Therefore, if "off_linktype" is -1, there's an error.
3414 if (off_linktype == (u_int)-1)
3418 * Any type not handled above should always have an Ethernet
3419 * type at an offset of "off_linktype".
3421 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3425 * Check for an LLC SNAP packet with a given organization code and
3426 * protocol type; we check the entire contents of the 802.2 LLC and
3427 * snap headers, checking for DSAP and SSAP of SNAP and a control
3428 * field of 0x03 in the LLC header, and for the specified organization
3429 * code and protocol type in the SNAP header.
3431 static struct block *
3432 gen_snap(orgcode, ptype)
3433 bpf_u_int32 orgcode;
3436 u_char snapblock[8];
3438 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3439 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3440 snapblock[2] = 0x03; /* control = UI */
3441 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3442 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3443 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3444 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3445 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3446 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3450 * Generate code to match a particular packet type, for link-layer types
3451 * using 802.2 LLC headers.
3453 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3454 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3456 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3457 * value, if <= ETHERMTU. We use that to determine whether to
3458 * match the DSAP or both DSAP and LSAP or to check the OUI and
3459 * protocol ID in a SNAP header.
3461 static struct block *
3462 gen_llc_linktype(proto)
3466 * XXX - handle token-ring variable-length header.
3472 case LLCSAP_NETBEUI:
3474 * XXX - should we check both the DSAP and the
3475 * SSAP, like this, or should we check just the
3476 * DSAP, as we do for other types <= ETHERMTU
3477 * (i.e., other SAP values)?
3479 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3480 ((proto << 8) | proto));
3484 * XXX - are there ever SNAP frames for IPX on
3485 * non-Ethernet 802.x networks?
3487 return gen_cmp(OR_MACPL, 0, BPF_B,
3488 (bpf_int32)LLCSAP_IPX);
3490 case ETHERTYPE_ATALK:
3492 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3493 * SNAP packets with an organization code of
3494 * 0x080007 (Apple, for Appletalk) and a protocol
3495 * type of ETHERTYPE_ATALK (Appletalk).
3497 * XXX - check for an organization code of
3498 * encapsulated Ethernet as well?
3500 return gen_snap(0x080007, ETHERTYPE_ATALK);
3504 * XXX - we don't have to check for IPX 802.3
3505 * here, but should we check for the IPX Ethertype?
3507 if (proto <= ETHERMTU) {
3509 * This is an LLC SAP value, so check
3512 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3515 * This is an Ethernet type; we assume that it's
3516 * unlikely that it'll appear in the right place
3517 * at random, and therefore check only the
3518 * location that would hold the Ethernet type
3519 * in a SNAP frame with an organization code of
3520 * 0x000000 (encapsulated Ethernet).
3522 * XXX - if we were to check for the SNAP DSAP and
3523 * LSAP, as per XXX, and were also to check for an
3524 * organization code of 0x000000 (encapsulated
3525 * Ethernet), we'd do
3527 * return gen_snap(0x000000, proto);
3529 * here; for now, we don't, as per the above.
3530 * I don't know whether it's worth the extra CPU
3531 * time to do the right check or not.
3533 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3538 static struct block *
3539 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3543 u_int src_off, dst_off;
3545 struct block *b0, *b1;
3559 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3560 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3566 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3567 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3574 b0 = gen_linktype(proto);
3575 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3581 static struct block *
3582 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3583 struct in6_addr *addr;
3584 struct in6_addr *mask;
3586 u_int src_off, dst_off;
3588 struct block *b0, *b1;
3603 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3604 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3610 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3611 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3618 /* this order is important */
3619 a = (u_int32_t *)addr;
3620 m = (u_int32_t *)mask;
3621 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3622 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3624 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3626 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3628 b0 = gen_linktype(proto);
3634 static struct block *
3635 gen_ehostop(eaddr, dir)
3636 register const u_char *eaddr;
3639 register struct block *b0, *b1;
3643 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3646 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3649 b0 = gen_ehostop(eaddr, Q_SRC);
3650 b1 = gen_ehostop(eaddr, Q_DST);
3656 b0 = gen_ehostop(eaddr, Q_SRC);
3657 b1 = gen_ehostop(eaddr, Q_DST);
3662 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3666 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3670 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3674 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3678 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3682 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3690 * Like gen_ehostop, but for DLT_FDDI
3692 static struct block *
3693 gen_fhostop(eaddr, dir)
3694 register const u_char *eaddr;
3697 struct block *b0, *b1;
3702 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3704 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3709 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3711 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3715 b0 = gen_fhostop(eaddr, Q_SRC);
3716 b1 = gen_fhostop(eaddr, Q_DST);
3722 b0 = gen_fhostop(eaddr, Q_SRC);
3723 b1 = gen_fhostop(eaddr, Q_DST);
3728 bpf_error("'addr1' is only supported on 802.11");
3732 bpf_error("'addr2' is only supported on 802.11");
3736 bpf_error("'addr3' is only supported on 802.11");
3740 bpf_error("'addr4' is only supported on 802.11");
3744 bpf_error("'ra' is only supported on 802.11");
3748 bpf_error("'ta' is only supported on 802.11");
3756 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3758 static struct block *
3759 gen_thostop(eaddr, dir)
3760 register const u_char *eaddr;
3763 register struct block *b0, *b1;
3767 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3770 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3773 b0 = gen_thostop(eaddr, Q_SRC);
3774 b1 = gen_thostop(eaddr, Q_DST);
3780 b0 = gen_thostop(eaddr, Q_SRC);
3781 b1 = gen_thostop(eaddr, Q_DST);
3786 bpf_error("'addr1' is only supported on 802.11");
3790 bpf_error("'addr2' is only supported on 802.11");
3794 bpf_error("'addr3' is only supported on 802.11");
3798 bpf_error("'addr4' is only supported on 802.11");
3802 bpf_error("'ra' is only supported on 802.11");
3806 bpf_error("'ta' is only supported on 802.11");
3814 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3815 * various 802.11 + radio headers.
3817 static struct block *
3818 gen_wlanhostop(eaddr, dir)
3819 register const u_char *eaddr;
3822 register struct block *b0, *b1, *b2;
3823 register struct slist *s;
3825 #ifdef ENABLE_WLAN_FILTERING_PATCH
3828 * We need to disable the optimizer because the optimizer is buggy
3829 * and wipes out some LD instructions generated by the below
3830 * code to validate the Frame Control bits
3833 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3840 * For control frames, there is no SA.
3842 * For management frames, SA is at an
3843 * offset of 10 from the beginning of
3846 * For data frames, SA is at an offset
3847 * of 10 from the beginning of the packet
3848 * if From DS is clear, at an offset of
3849 * 16 from the beginning of the packet
3850 * if From DS is set and To DS is clear,
3851 * and an offset of 24 from the beginning
3852 * of the packet if From DS is set and To DS
3857 * Generate the tests to be done for data frames
3860 * First, check for To DS set, i.e. check "link[1] & 0x01".
3862 s = gen_load_a(OR_LINK, 1, BPF_B);
3863 b1 = new_block(JMP(BPF_JSET));
3864 b1->s.k = 0x01; /* To DS */
3868 * If To DS is set, the SA is at 24.
3870 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3874 * Now, check for To DS not set, i.e. check
3875 * "!(link[1] & 0x01)".
3877 s = gen_load_a(OR_LINK, 1, BPF_B);
3878 b2 = new_block(JMP(BPF_JSET));
3879 b2->s.k = 0x01; /* To DS */
3884 * If To DS is not set, the SA is at 16.
3886 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3890 * Now OR together the last two checks. That gives
3891 * the complete set of checks for data frames with
3897 * Now check for From DS being set, and AND that with
3898 * the ORed-together checks.
3900 s = gen_load_a(OR_LINK, 1, BPF_B);
3901 b1 = new_block(JMP(BPF_JSET));
3902 b1->s.k = 0x02; /* From DS */
3907 * Now check for data frames with From DS not set.
3909 s = gen_load_a(OR_LINK, 1, BPF_B);
3910 b2 = new_block(JMP(BPF_JSET));
3911 b2->s.k = 0x02; /* From DS */
3916 * If From DS isn't set, the SA is at 10.
3918 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3922 * Now OR together the checks for data frames with
3923 * From DS not set and for data frames with From DS
3924 * set; that gives the checks done for data frames.
3929 * Now check for a data frame.
3930 * I.e, check "link[0] & 0x08".
3932 s = gen_load_a(OR_LINK, 0, BPF_B);
3933 b1 = new_block(JMP(BPF_JSET));
3938 * AND that with the checks done for data frames.
3943 * If the high-order bit of the type value is 0, this
3944 * is a management frame.
3945 * I.e, check "!(link[0] & 0x08)".
3947 s = gen_load_a(OR_LINK, 0, BPF_B);
3948 b2 = new_block(JMP(BPF_JSET));
3954 * For management frames, the SA is at 10.
3956 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3960 * OR that with the checks done for data frames.
3961 * That gives the checks done for management and
3967 * If the low-order bit of the type value is 1,
3968 * this is either a control frame or a frame
3969 * with a reserved type, and thus not a
3972 * I.e., check "!(link[0] & 0x04)".
3974 s = gen_load_a(OR_LINK, 0, BPF_B);
3975 b1 = new_block(JMP(BPF_JSET));
3981 * AND that with the checks for data and management
3991 * For control frames, there is no DA.
3993 * For management frames, DA is at an
3994 * offset of 4 from the beginning of
3997 * For data frames, DA is at an offset
3998 * of 4 from the beginning of the packet
3999 * if To DS is clear and at an offset of
4000 * 16 from the beginning of the packet
4005 * Generate the tests to be done for data frames.
4007 * First, check for To DS set, i.e. "link[1] & 0x01".
4009 s = gen_load_a(OR_LINK, 1, BPF_B);
4010 b1 = new_block(JMP(BPF_JSET));
4011 b1->s.k = 0x01; /* To DS */
4015 * If To DS is set, the DA is at 16.
4017 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4021 * Now, check for To DS not set, i.e. check
4022 * "!(link[1] & 0x01)".
4024 s = gen_load_a(OR_LINK, 1, BPF_B);
4025 b2 = new_block(JMP(BPF_JSET));
4026 b2->s.k = 0x01; /* To DS */
4031 * If To DS is not set, the DA is at 4.
4033 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4037 * Now OR together the last two checks. That gives
4038 * the complete set of checks for data frames.
4043 * Now check for a data frame.
4044 * I.e, check "link[0] & 0x08".
4046 s = gen_load_a(OR_LINK, 0, BPF_B);
4047 b1 = new_block(JMP(BPF_JSET));
4052 * AND that with the checks done for data frames.
4057 * If the high-order bit of the type value is 0, this
4058 * is a management frame.
4059 * I.e, check "!(link[0] & 0x08)".
4061 s = gen_load_a(OR_LINK, 0, BPF_B);
4062 b2 = new_block(JMP(BPF_JSET));
4068 * For management frames, the DA is at 4.
4070 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4074 * OR that with the checks done for data frames.
4075 * That gives the checks done for management and
4081 * If the low-order bit of the type value is 1,
4082 * this is either a control frame or a frame
4083 * with a reserved type, and thus not a
4086 * I.e., check "!(link[0] & 0x04)".
4088 s = gen_load_a(OR_LINK, 0, BPF_B);
4089 b1 = new_block(JMP(BPF_JSET));
4095 * AND that with the checks for data and management
4103 * Not present in management frames; addr1 in other
4108 * If the high-order bit of the type value is 0, this
4109 * is a management frame.
4110 * I.e, check "(link[0] & 0x08)".
4112 s = gen_load_a(OR_LINK, 0, BPF_B);
4113 b1 = new_block(JMP(BPF_JSET));
4120 b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4123 * AND that with the check of addr1.
4130 * Not present in management frames; addr2, if present,
4135 * Not present in CTS or ACK control frames.
4137 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4138 IEEE80211_FC0_TYPE_MASK);
4140 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4141 IEEE80211_FC0_SUBTYPE_MASK);
4143 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4144 IEEE80211_FC0_SUBTYPE_MASK);
4150 * If the high-order bit of the type value is 0, this
4151 * is a management frame.
4152 * I.e, check "(link[0] & 0x08)".
4154 s = gen_load_a(OR_LINK, 0, BPF_B);
4155 b1 = new_block(JMP(BPF_JSET));
4160 * AND that with the check for frames other than
4161 * CTS and ACK frames.
4168 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4173 * XXX - add BSSID keyword?
4176 return (gen_bcmp(OR_LINK, 4, 6, eaddr));
4180 * Not present in CTS or ACK control frames.
4182 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4183 IEEE80211_FC0_TYPE_MASK);
4185 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4186 IEEE80211_FC0_SUBTYPE_MASK);
4188 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4189 IEEE80211_FC0_SUBTYPE_MASK);
4193 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4199 * Not present in control frames.
4201 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4202 IEEE80211_FC0_TYPE_MASK);
4204 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4210 * Present only if the direction mask has both "From DS"
4211 * and "To DS" set. Neither control frames nor management
4212 * frames should have both of those set, so we don't
4213 * check the frame type.
4215 b0 = gen_mcmp(OR_LINK, 1, BPF_B,
4216 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4217 b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
4222 b0 = gen_wlanhostop(eaddr, Q_SRC);
4223 b1 = gen_wlanhostop(eaddr, Q_DST);
4229 b0 = gen_wlanhostop(eaddr, Q_SRC);
4230 b1 = gen_wlanhostop(eaddr, Q_DST);
4239 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4240 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4241 * as the RFC states.)
4243 static struct block *
4244 gen_ipfchostop(eaddr, dir)
4245 register const u_char *eaddr;
4248 register struct block *b0, *b1;
4252 return gen_bcmp(OR_LINK, 10, 6, eaddr);
4255 return gen_bcmp(OR_LINK, 2, 6, eaddr);
4258 b0 = gen_ipfchostop(eaddr, Q_SRC);
4259 b1 = gen_ipfchostop(eaddr, Q_DST);
4265 b0 = gen_ipfchostop(eaddr, Q_SRC);
4266 b1 = gen_ipfchostop(eaddr, Q_DST);
4271 bpf_error("'addr1' is only supported on 802.11");
4275 bpf_error("'addr2' is only supported on 802.11");
4279 bpf_error("'addr3' is only supported on 802.11");
4283 bpf_error("'addr4' is only supported on 802.11");
4287 bpf_error("'ra' is only supported on 802.11");
4291 bpf_error("'ta' is only supported on 802.11");
4299 * This is quite tricky because there may be pad bytes in front of the
4300 * DECNET header, and then there are two possible data packet formats that
4301 * carry both src and dst addresses, plus 5 packet types in a format that
4302 * carries only the src node, plus 2 types that use a different format and
4303 * also carry just the src node.
4307 * Instead of doing those all right, we just look for data packets with
4308 * 0 or 1 bytes of padding. If you want to look at other packets, that
4309 * will require a lot more hacking.
4311 * To add support for filtering on DECNET "areas" (network numbers)
4312 * one would want to add a "mask" argument to this routine. That would
4313 * make the filter even more inefficient, although one could be clever
4314 * and not generate masking instructions if the mask is 0xFFFF.
4316 static struct block *
4317 gen_dnhostop(addr, dir)
4321 struct block *b0, *b1, *b2, *tmp;
4322 u_int offset_lh; /* offset if long header is received */
4323 u_int offset_sh; /* offset if short header is received */
4328 offset_sh = 1; /* follows flags */
4329 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4333 offset_sh = 3; /* follows flags, dstnode */
4334 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4338 /* Inefficient because we do our Calvinball dance twice */
4339 b0 = gen_dnhostop(addr, Q_SRC);
4340 b1 = gen_dnhostop(addr, Q_DST);
4346 /* Inefficient because we do our Calvinball dance twice */
4347 b0 = gen_dnhostop(addr, Q_SRC);
4348 b1 = gen_dnhostop(addr, Q_DST);
4353 bpf_error("ISO host filtering not implemented");
4358 b0 = gen_linktype(ETHERTYPE_DN);
4359 /* Check for pad = 1, long header case */
4360 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4361 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
4362 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
4363 BPF_H, (bpf_int32)ntohs((u_short)addr));
4365 /* Check for pad = 0, long header case */
4366 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4367 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4370 /* Check for pad = 1, short header case */
4371 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4372 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
4373 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4376 /* Check for pad = 0, short header case */
4377 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4378 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4382 /* Combine with test for linktype */
4388 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4389 * test the bottom-of-stack bit, and then check the version number
4390 * field in the IP header.
4392 static struct block *
4393 gen_mpls_linktype(proto)
4396 struct block *b0, *b1;
4401 /* match the bottom-of-stack bit */
4402 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4403 /* match the IPv4 version number */
4404 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
4409 /* match the bottom-of-stack bit */
4410 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4411 /* match the IPv4 version number */
4412 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
4421 static struct block *
4422 gen_host(addr, mask, proto, dir, type)
4429 struct block *b0, *b1;
4430 const char *typestr;
4440 b0 = gen_host(addr, mask, Q_IP, dir, type);
4442 * Only check for non-IPv4 addresses if we're not
4443 * checking MPLS-encapsulated packets.
4445 if (label_stack_depth == 0) {
4446 b1 = gen_host(addr, mask, Q_ARP, dir, type);
4448 b0 = gen_host(addr, mask, Q_RARP, dir, type);
4454 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
4457 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4460 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4463 bpf_error("'tcp' modifier applied to %s", typestr);
4466 bpf_error("'sctp' modifier applied to %s", typestr);
4469 bpf_error("'udp' modifier applied to %s", typestr);
4472 bpf_error("'icmp' modifier applied to %s", typestr);
4475 bpf_error("'igmp' modifier applied to %s", typestr);
4478 bpf_error("'igrp' modifier applied to %s", typestr);
4481 bpf_error("'pim' modifier applied to %s", typestr);
4484 bpf_error("'vrrp' modifier applied to %s", typestr);
4487 bpf_error("'carp' modifier applied to %s", typestr);
4490 bpf_error("ATALK host filtering not implemented");
4493 bpf_error("AARP host filtering not implemented");
4496 return gen_dnhostop(addr, dir);
4499 bpf_error("SCA host filtering not implemented");
4502 bpf_error("LAT host filtering not implemented");
4505 bpf_error("MOPDL host filtering not implemented");
4508 bpf_error("MOPRC host filtering not implemented");
4512 bpf_error("'ip6' modifier applied to ip host");
4515 bpf_error("'icmp6' modifier applied to %s", typestr);
4519 bpf_error("'ah' modifier applied to %s", typestr);
4522 bpf_error("'esp' modifier applied to %s", typestr);
4525 bpf_error("ISO host filtering not implemented");
4528 bpf_error("'esis' modifier applied to %s", typestr);
4531 bpf_error("'isis' modifier applied to %s", typestr);
4534 bpf_error("'clnp' modifier applied to %s", typestr);
4537 bpf_error("'stp' modifier applied to %s", typestr);
4540 bpf_error("IPX host filtering not implemented");
4543 bpf_error("'netbeui' modifier applied to %s", typestr);
4546 bpf_error("'radio' modifier applied to %s", typestr);
4555 static struct block *
4556 gen_host6(addr, mask, proto, dir, type)
4557 struct in6_addr *addr;
4558 struct in6_addr *mask;
4563 const char *typestr;
4573 return gen_host6(addr, mask, Q_IPV6, dir, type);
4576 bpf_error("'ip' modifier applied to ip6 %s", typestr);
4579 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
4582 bpf_error("'arp' modifier applied to ip6 %s", typestr);
4585 bpf_error("'sctp' modifier applied to %s", typestr);
4588 bpf_error("'tcp' modifier applied to %s", typestr);
4591 bpf_error("'udp' modifier applied to %s", typestr);
4594 bpf_error("'icmp' modifier applied to %s", typestr);
4597 bpf_error("'igmp' modifier applied to %s", typestr);
4600 bpf_error("'igrp' modifier applied to %s", typestr);
4603 bpf_error("'pim' modifier applied to %s", typestr);
4606 bpf_error("'vrrp' modifier applied to %s", typestr);
4609 bpf_error("'carp' modifier applied to %s", typestr);
4612 bpf_error("ATALK host filtering not implemented");
4615 bpf_error("AARP host filtering not implemented");
4618 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
4621 bpf_error("SCA host filtering not implemented");
4624 bpf_error("LAT host filtering not implemented");
4627 bpf_error("MOPDL host filtering not implemented");
4630 bpf_error("MOPRC host filtering not implemented");
4633 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4636 bpf_error("'icmp6' modifier applied to %s", typestr);
4639 bpf_error("'ah' modifier applied to %s", typestr);
4642 bpf_error("'esp' modifier applied to %s", typestr);
4645 bpf_error("ISO host filtering not implemented");
4648 bpf_error("'esis' modifier applied to %s", typestr);
4651 bpf_error("'isis' modifier applied to %s", typestr);
4654 bpf_error("'clnp' modifier applied to %s", typestr);
4657 bpf_error("'stp' modifier applied to %s", typestr);
4660 bpf_error("IPX host filtering not implemented");
4663 bpf_error("'netbeui' modifier applied to %s", typestr);
4666 bpf_error("'radio' modifier applied to %s", typestr);
4676 static struct block *
4677 gen_gateway(eaddr, alist, proto, dir)
4678 const u_char *eaddr;
4679 bpf_u_int32 **alist;
4683 struct block *b0, *b1, *tmp;
4686 bpf_error("direction applied to 'gateway'");
4695 case DLT_NETANALYZER:
4696 case DLT_NETANALYZER_TRANSPARENT:
4697 b0 = gen_ehostop(eaddr, Q_OR);
4700 b0 = gen_fhostop(eaddr, Q_OR);
4703 b0 = gen_thostop(eaddr, Q_OR);
4705 case DLT_IEEE802_11:
4706 case DLT_PRISM_HEADER:
4707 case DLT_IEEE802_11_RADIO_AVS:
4708 case DLT_IEEE802_11_RADIO:
4710 b0 = gen_wlanhostop(eaddr, Q_OR);
4715 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4717 * Check that the packet doesn't begin with an
4718 * LE Control marker. (We've already generated
4721 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4726 * Now check the MAC address.
4728 b0 = gen_ehostop(eaddr, Q_OR);
4731 case DLT_IP_OVER_FC:
4732 b0 = gen_ipfchostop(eaddr, Q_OR);
4736 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4738 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
4740 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
4749 bpf_error("illegal modifier of 'gateway'");
4755 gen_proto_abbrev(proto)
4764 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
4766 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
4772 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
4774 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
4780 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
4782 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
4788 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
4791 #ifndef IPPROTO_IGMP
4792 #define IPPROTO_IGMP 2
4796 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
4799 #ifndef IPPROTO_IGRP
4800 #define IPPROTO_IGRP 9
4803 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
4807 #define IPPROTO_PIM 103
4811 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
4813 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
4818 #ifndef IPPROTO_VRRP
4819 #define IPPROTO_VRRP 112
4823 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
4826 #ifndef IPPROTO_CARP
4827 #define IPPROTO_CARP 112
4831 b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT);
4835 b1 = gen_linktype(ETHERTYPE_IP);
4839 b1 = gen_linktype(ETHERTYPE_ARP);
4843 b1 = gen_linktype(ETHERTYPE_REVARP);
4847 bpf_error("link layer applied in wrong context");
4850 b1 = gen_linktype(ETHERTYPE_ATALK);
4854 b1 = gen_linktype(ETHERTYPE_AARP);
4858 b1 = gen_linktype(ETHERTYPE_DN);
4862 b1 = gen_linktype(ETHERTYPE_SCA);
4866 b1 = gen_linktype(ETHERTYPE_LAT);
4870 b1 = gen_linktype(ETHERTYPE_MOPDL);
4874 b1 = gen_linktype(ETHERTYPE_MOPRC);
4879 b1 = gen_linktype(ETHERTYPE_IPV6);
4882 #ifndef IPPROTO_ICMPV6
4883 #define IPPROTO_ICMPV6 58
4886 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
4891 #define IPPROTO_AH 51
4894 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
4896 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
4902 #define IPPROTO_ESP 50
4905 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
4907 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
4913 b1 = gen_linktype(LLCSAP_ISONS);
4917 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
4921 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4924 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
4925 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4926 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4928 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4930 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4932 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4936 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
4937 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4938 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4940 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4942 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4944 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4948 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
4949 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4950 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4952 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
4957 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4958 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4963 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4964 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4966 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4968 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4973 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4974 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4979 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4980 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4985 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
4989 b1 = gen_linktype(LLCSAP_8021D);
4993 b1 = gen_linktype(LLCSAP_IPX);
4997 b1 = gen_linktype(LLCSAP_NETBEUI);
5001 bpf_error("'radio' is not a valid protocol type");
5009 static struct block *
5015 /* not IPv4 frag other than the first frag */
5016 s = gen_load_a(OR_NET, 6, BPF_H);
5017 b = new_block(JMP(BPF_JSET));
5026 * Generate a comparison to a port value in the transport-layer header
5027 * at the specified offset from the beginning of that header.
5029 * XXX - this handles a variable-length prefix preceding the link-layer
5030 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5031 * variable-length link-layer headers (such as Token Ring or 802.11
5034 static struct block *
5035 gen_portatom(off, v)
5039 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
5043 static struct block *
5044 gen_portatom6(off, v)
5048 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
5053 gen_portop(port, proto, dir)
5054 int port, proto, dir;
5056 struct block *b0, *b1, *tmp;
5058 /* ip proto 'proto' and not a fragment other than the first fragment */
5059 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5065 b1 = gen_portatom(0, (bpf_int32)port);
5069 b1 = gen_portatom(2, (bpf_int32)port);
5074 tmp = gen_portatom(0, (bpf_int32)port);
5075 b1 = gen_portatom(2, (bpf_int32)port);
5080 tmp = gen_portatom(0, (bpf_int32)port);
5081 b1 = gen_portatom(2, (bpf_int32)port);
5093 static struct block *
5094 gen_port(port, ip_proto, dir)
5099 struct block *b0, *b1, *tmp;
5104 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5105 * not LLC encapsulation with LLCSAP_IP.
5107 * For IEEE 802 networks - which includes 802.5 token ring
5108 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5109 * says that SNAP encapsulation is used, not LLC encapsulation
5112 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5113 * RFC 2225 say that SNAP encapsulation is used, not LLC
5114 * encapsulation with LLCSAP_IP.
5116 * So we always check for ETHERTYPE_IP.
5118 b0 = gen_linktype(ETHERTYPE_IP);
5124 b1 = gen_portop(port, ip_proto, dir);
5128 tmp = gen_portop(port, IPPROTO_TCP, dir);
5129 b1 = gen_portop(port, IPPROTO_UDP, dir);
5131 tmp = gen_portop(port, IPPROTO_SCTP, dir);
5144 gen_portop6(port, proto, dir)
5145 int port, proto, dir;
5147 struct block *b0, *b1, *tmp;
5149 /* ip6 proto 'proto' */
5150 /* XXX - catch the first fragment of a fragmented packet? */
5151 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5155 b1 = gen_portatom6(0, (bpf_int32)port);
5159 b1 = gen_portatom6(2, (bpf_int32)port);
5164 tmp = gen_portatom6(0, (bpf_int32)port);
5165 b1 = gen_portatom6(2, (bpf_int32)port);
5170 tmp = gen_portatom6(0, (bpf_int32)port);
5171 b1 = gen_portatom6(2, (bpf_int32)port);
5183 static struct block *
5184 gen_port6(port, ip_proto, dir)
5189 struct block *b0, *b1, *tmp;
5191 /* link proto ip6 */
5192 b0 = gen_linktype(ETHERTYPE_IPV6);
5198 b1 = gen_portop6(port, ip_proto, dir);
5202 tmp = gen_portop6(port, IPPROTO_TCP, dir);
5203 b1 = gen_portop6(port, IPPROTO_UDP, dir);
5205 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
5217 /* gen_portrange code */
5218 static struct block *
5219 gen_portrangeatom(off, v1, v2)
5223 struct block *b1, *b2;
5227 * Reverse the order of the ports, so v1 is the lower one.
5236 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
5237 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
5245 gen_portrangeop(port1, port2, proto, dir)
5250 struct block *b0, *b1, *tmp;
5252 /* ip proto 'proto' and not a fragment other than the first fragment */
5253 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5259 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5263 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5268 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5269 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5274 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5275 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5287 static struct block *
5288 gen_portrange(port1, port2, ip_proto, dir)
5293 struct block *b0, *b1, *tmp;
5296 b0 = gen_linktype(ETHERTYPE_IP);
5302 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
5306 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
5307 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
5309 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
5321 static struct block *
5322 gen_portrangeatom6(off, v1, v2)
5326 struct block *b1, *b2;
5330 * Reverse the order of the ports, so v1 is the lower one.
5339 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
5340 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
5348 gen_portrangeop6(port1, port2, proto, dir)
5353 struct block *b0, *b1, *tmp;
5355 /* ip6 proto 'proto' */
5356 /* XXX - catch the first fragment of a fragmented packet? */
5357 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5361 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5365 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5370 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5371 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5376 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5377 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5389 static struct block *
5390 gen_portrange6(port1, port2, ip_proto, dir)
5395 struct block *b0, *b1, *tmp;
5397 /* link proto ip6 */
5398 b0 = gen_linktype(ETHERTYPE_IPV6);
5404 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
5408 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
5409 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
5411 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
5424 lookup_proto(name, proto)
5425 register const char *name;
5435 v = pcap_nametoproto(name);
5436 if (v == PROTO_UNDEF)
5437 bpf_error("unknown ip proto '%s'", name);
5441 /* XXX should look up h/w protocol type based on linktype */
5442 v = pcap_nametoeproto(name);
5443 if (v == PROTO_UNDEF) {
5444 v = pcap_nametollc(name);
5445 if (v == PROTO_UNDEF)
5446 bpf_error("unknown ether proto '%s'", name);
5451 if (strcmp(name, "esis") == 0)
5453 else if (strcmp(name, "isis") == 0)
5455 else if (strcmp(name, "clnp") == 0)
5458 bpf_error("unknown osi proto '%s'", name);
5478 static struct block *
5479 gen_protochain(v, proto, dir)
5484 #ifdef NO_PROTOCHAIN
5485 return gen_proto(v, proto, dir);
5487 struct block *b0, *b;
5488 struct slist *s[100];
5489 int fix2, fix3, fix4, fix5;
5490 int ahcheck, again, end;
5492 int reg2 = alloc_reg();
5494 memset(s, 0, sizeof(s));
5495 fix2 = fix3 = fix4 = fix5 = 0;
5502 b0 = gen_protochain(v, Q_IP, dir);
5503 b = gen_protochain(v, Q_IPV6, dir);
5507 bpf_error("bad protocol applied for 'protochain'");
5512 * We don't handle variable-length prefixes before the link-layer
5513 * header, or variable-length link-layer headers, here yet.
5514 * We might want to add BPF instructions to do the protochain
5515 * work, to simplify that and, on platforms that have a BPF
5516 * interpreter with the new instructions, let the filtering
5517 * be done in the kernel. (We already require a modified BPF
5518 * engine to do the protochain stuff, to support backward
5519 * branches, and backward branch support is unlikely to appear
5520 * in kernel BPF engines.)
5524 case DLT_IEEE802_11:
5525 case DLT_PRISM_HEADER:
5526 case DLT_IEEE802_11_RADIO_AVS:
5527 case DLT_IEEE802_11_RADIO:
5529 bpf_error("'protochain' not supported with 802.11");
5532 no_optimize = 1; /*this code is not compatible with optimzer yet */
5535 * s[0] is a dummy entry to protect other BPF insn from damage
5536 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5537 * hard to find interdependency made by jump table fixup.
5540 s[i] = new_stmt(0); /*dummy*/
5545 b0 = gen_linktype(ETHERTYPE_IP);
5548 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5549 s[i]->s.k = off_macpl + off_nl + 9;
5551 /* X = ip->ip_hl << 2 */
5552 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
5553 s[i]->s.k = off_macpl + off_nl;
5558 b0 = gen_linktype(ETHERTYPE_IPV6);
5560 /* A = ip6->ip_nxt */
5561 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5562 s[i]->s.k = off_macpl + off_nl + 6;
5564 /* X = sizeof(struct ip6_hdr) */
5565 s[i] = new_stmt(BPF_LDX|BPF_IMM);
5571 bpf_error("unsupported proto to gen_protochain");
5575 /* again: if (A == v) goto end; else fall through; */
5577 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5579 s[i]->s.jt = NULL; /*later*/
5580 s[i]->s.jf = NULL; /*update in next stmt*/
5584 #ifndef IPPROTO_NONE
5585 #define IPPROTO_NONE 59
5587 /* if (A == IPPROTO_NONE) goto end */
5588 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5589 s[i]->s.jt = NULL; /*later*/
5590 s[i]->s.jf = NULL; /*update in next stmt*/
5591 s[i]->s.k = IPPROTO_NONE;
5592 s[fix5]->s.jf = s[i];
5597 if (proto == Q_IPV6) {
5598 int v6start, v6end, v6advance, j;
5601 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5602 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5603 s[i]->s.jt = NULL; /*later*/
5604 s[i]->s.jf = NULL; /*update in next stmt*/
5605 s[i]->s.k = IPPROTO_HOPOPTS;
5606 s[fix2]->s.jf = s[i];
5608 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5609 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5610 s[i]->s.jt = NULL; /*later*/
5611 s[i]->s.jf = NULL; /*update in next stmt*/
5612 s[i]->s.k = IPPROTO_DSTOPTS;
5614 /* if (A == IPPROTO_ROUTING) goto v6advance */
5615 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5616 s[i]->s.jt = NULL; /*later*/
5617 s[i]->s.jf = NULL; /*update in next stmt*/
5618 s[i]->s.k = IPPROTO_ROUTING;
5620 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5621 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5622 s[i]->s.jt = NULL; /*later*/
5623 s[i]->s.jf = NULL; /*later*/
5624 s[i]->s.k = IPPROTO_FRAGMENT;
5634 * A = P[X + packet head];
5635 * X = X + (P[X + packet head + 1] + 1) * 8;
5637 /* A = P[X + packet head] */
5638 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5639 s[i]->s.k = off_macpl + off_nl;
5642 s[i] = new_stmt(BPF_ST);
5645 /* A = P[X + packet head + 1]; */
5646 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5647 s[i]->s.k = off_macpl + off_nl + 1;
5650 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5654 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5658 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
5662 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5665 s[i] = new_stmt(BPF_LD|BPF_MEM);
5669 /* goto again; (must use BPF_JA for backward jump) */
5670 s[i] = new_stmt(BPF_JMP|BPF_JA);
5671 s[i]->s.k = again - i - 1;
5672 s[i - 1]->s.jf = s[i];
5676 for (j = v6start; j <= v6end; j++)
5677 s[j]->s.jt = s[v6advance];
5682 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5684 s[fix2]->s.jf = s[i];
5690 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5691 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5692 s[i]->s.jt = NULL; /*later*/
5693 s[i]->s.jf = NULL; /*later*/
5694 s[i]->s.k = IPPROTO_AH;
5696 s[fix3]->s.jf = s[ahcheck];
5703 * X = X + (P[X + 1] + 2) * 4;
5706 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5708 /* A = P[X + packet head]; */
5709 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5710 s[i]->s.k = off_macpl + off_nl;
5713 s[i] = new_stmt(BPF_ST);
5717 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5720 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5724 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5726 /* A = P[X + packet head] */
5727 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5728 s[i]->s.k = off_macpl + off_nl;
5731 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5735 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5739 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5742 s[i] = new_stmt(BPF_LD|BPF_MEM);
5746 /* goto again; (must use BPF_JA for backward jump) */
5747 s[i] = new_stmt(BPF_JMP|BPF_JA);
5748 s[i]->s.k = again - i - 1;
5753 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5755 s[fix2]->s.jt = s[end];
5756 s[fix4]->s.jf = s[end];
5757 s[fix5]->s.jt = s[end];
5764 for (i = 0; i < max - 1; i++)
5765 s[i]->next = s[i + 1];
5766 s[max - 1]->next = NULL;
5771 b = new_block(JMP(BPF_JEQ));
5772 b->stmts = s[1]; /*remember, s[0] is dummy*/
5782 static struct block *
5783 gen_check_802_11_data_frame()
5786 struct block *b0, *b1;
5789 * A data frame has the 0x08 bit (b3) in the frame control field set
5790 * and the 0x04 bit (b2) clear.
5792 s = gen_load_a(OR_LINK, 0, BPF_B);
5793 b0 = new_block(JMP(BPF_JSET));
5797 s = gen_load_a(OR_LINK, 0, BPF_B);
5798 b1 = new_block(JMP(BPF_JSET));
5809 * Generate code that checks whether the packet is a packet for protocol
5810 * <proto> and whether the type field in that protocol's header has
5811 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5812 * IP packet and checks the protocol number in the IP header against <v>.
5814 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5815 * against Q_IP and Q_IPV6.
5817 static struct block *
5818 gen_proto(v, proto, dir)
5823 struct block *b0, *b1;
5825 if (dir != Q_DEFAULT)
5826 bpf_error("direction applied to 'proto'");
5831 b0 = gen_proto(v, Q_IP, dir);
5832 b1 = gen_proto(v, Q_IPV6, dir);
5840 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5841 * not LLC encapsulation with LLCSAP_IP.
5843 * For IEEE 802 networks - which includes 802.5 token ring
5844 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5845 * says that SNAP encapsulation is used, not LLC encapsulation
5848 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5849 * RFC 2225 say that SNAP encapsulation is used, not LLC
5850 * encapsulation with LLCSAP_IP.
5852 * So we always check for ETHERTYPE_IP.
5854 b0 = gen_linktype(ETHERTYPE_IP);
5856 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
5858 b1 = gen_protochain(v, Q_IP);
5868 * Frame Relay packets typically have an OSI
5869 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5870 * generates code to check for all the OSI
5871 * NLPIDs, so calling it and then adding a check
5872 * for the particular NLPID for which we're
5873 * looking is bogus, as we can just check for
5876 * What we check for is the NLPID and a frame
5877 * control field value of UI, i.e. 0x03 followed
5880 * XXX - assumes a 2-byte Frame Relay header with
5881 * DLCI and flags. What if the address is longer?
5883 * XXX - what about SNAP-encapsulated frames?
5885 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
5891 * Cisco uses an Ethertype lookalike - for OSI,
5894 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
5895 /* OSI in C-HDLC is stuffed with a fudge byte */
5896 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
5901 b0 = gen_linktype(LLCSAP_ISONS);
5902 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
5908 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5910 * 4 is the offset of the PDU type relative to the IS-IS
5913 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
5918 bpf_error("arp does not encapsulate another protocol");
5922 bpf_error("rarp does not encapsulate another protocol");
5926 bpf_error("atalk encapsulation is not specifiable");
5930 bpf_error("decnet encapsulation is not specifiable");
5934 bpf_error("sca does not encapsulate another protocol");
5938 bpf_error("lat does not encapsulate another protocol");
5942 bpf_error("moprc does not encapsulate another protocol");
5946 bpf_error("mopdl does not encapsulate another protocol");
5950 return gen_linktype(v);
5953 bpf_error("'udp proto' is bogus");
5957 bpf_error("'tcp proto' is bogus");
5961 bpf_error("'sctp proto' is bogus");
5965 bpf_error("'icmp proto' is bogus");
5969 bpf_error("'igmp proto' is bogus");
5973 bpf_error("'igrp proto' is bogus");
5977 bpf_error("'pim proto' is bogus");
5981 bpf_error("'vrrp proto' is bogus");
5985 bpf_error("'carp proto' is bogus");
5990 b0 = gen_linktype(ETHERTYPE_IPV6);
5992 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
5994 b1 = gen_protochain(v, Q_IPV6);
6000 bpf_error("'icmp6 proto' is bogus");
6004 bpf_error("'ah proto' is bogus");
6007 bpf_error("'ah proto' is bogus");
6010 bpf_error("'stp proto' is bogus");
6013 bpf_error("'ipx proto' is bogus");
6016 bpf_error("'netbeui proto' is bogus");
6019 bpf_error("'radio proto' is bogus");
6030 register const char *name;
6033 int proto = q.proto;
6037 bpf_u_int32 mask, addr;
6039 bpf_u_int32 **alist;
6042 struct sockaddr_in *sin4;
6043 struct sockaddr_in6 *sin6;
6044 struct addrinfo *res, *res0;
6045 struct in6_addr mask128;
6047 struct block *b, *tmp;
6048 int port, real_proto;
6054 addr = pcap_nametonetaddr(name);
6056 bpf_error("unknown network '%s'", name);
6057 /* Left justify network addr and calculate its network mask */
6059 while (addr && (addr & 0xff000000) == 0) {
6063 return gen_host(addr, mask, proto, dir, q.addr);
6067 if (proto == Q_LINK) {
6071 case DLT_NETANALYZER:
6072 case DLT_NETANALYZER_TRANSPARENT:
6073 eaddr = pcap_ether_hostton(name);
6076 "unknown ether host '%s'", name);
6077 b = gen_ehostop(eaddr, dir);
6082 eaddr = pcap_ether_hostton(name);
6085 "unknown FDDI host '%s'", name);
6086 b = gen_fhostop(eaddr, dir);
6091 eaddr = pcap_ether_hostton(name);
6094 "unknown token ring host '%s'", name);
6095 b = gen_thostop(eaddr, dir);
6099 case DLT_IEEE802_11:
6100 case DLT_PRISM_HEADER:
6101 case DLT_IEEE802_11_RADIO_AVS:
6102 case DLT_IEEE802_11_RADIO:
6104 eaddr = pcap_ether_hostton(name);
6107 "unknown 802.11 host '%s'", name);
6108 b = gen_wlanhostop(eaddr, dir);
6112 case DLT_IP_OVER_FC:
6113 eaddr = pcap_ether_hostton(name);
6116 "unknown Fibre Channel host '%s'", name);
6117 b = gen_ipfchostop(eaddr, dir);
6126 * Check that the packet doesn't begin
6127 * with an LE Control marker. (We've
6128 * already generated a test for LANE.)
6130 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
6134 eaddr = pcap_ether_hostton(name);
6137 "unknown ether host '%s'", name);
6138 b = gen_ehostop(eaddr, dir);
6144 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6145 } else if (proto == Q_DECNET) {
6146 unsigned short dn_addr = __pcap_nametodnaddr(name);
6148 * I don't think DECNET hosts can be multihomed, so
6149 * there is no need to build up a list of addresses
6151 return (gen_host(dn_addr, 0, proto, dir, q.addr));
6154 alist = pcap_nametoaddr(name);
6155 if (alist == NULL || *alist == NULL)
6156 bpf_error("unknown host '%s'", name);
6158 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
6160 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
6162 tmp = gen_host(**alist++, 0xffffffff,
6163 tproto, dir, q.addr);
6169 memset(&mask128, 0xff, sizeof(mask128));
6170 res0 = res = pcap_nametoaddrinfo(name);
6172 bpf_error("unknown host '%s'", name);
6175 tproto = tproto6 = proto;
6176 if (off_linktype == -1 && tproto == Q_DEFAULT) {
6180 for (res = res0; res; res = res->ai_next) {
6181 switch (res->ai_family) {
6183 if (tproto == Q_IPV6)
6186 sin4 = (struct sockaddr_in *)
6188 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
6189 0xffffffff, tproto, dir, q.addr);
6192 if (tproto6 == Q_IP)
6195 sin6 = (struct sockaddr_in6 *)
6197 tmp = gen_host6(&sin6->sin6_addr,
6198 &mask128, tproto6, dir, q.addr);
6210 bpf_error("unknown host '%s'%s", name,
6211 (proto == Q_DEFAULT)
6213 : " for specified address family");
6220 if (proto != Q_DEFAULT &&
6221 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6222 bpf_error("illegal qualifier of 'port'");
6223 if (pcap_nametoport(name, &port, &real_proto) == 0)
6224 bpf_error("unknown port '%s'", name);
6225 if (proto == Q_UDP) {
6226 if (real_proto == IPPROTO_TCP)
6227 bpf_error("port '%s' is tcp", name);
6228 else if (real_proto == IPPROTO_SCTP)
6229 bpf_error("port '%s' is sctp", name);
6231 /* override PROTO_UNDEF */
6232 real_proto = IPPROTO_UDP;
6234 if (proto == Q_TCP) {
6235 if (real_proto == IPPROTO_UDP)
6236 bpf_error("port '%s' is udp", name);
6238 else if (real_proto == IPPROTO_SCTP)
6239 bpf_error("port '%s' is sctp", name);
6241 /* override PROTO_UNDEF */
6242 real_proto = IPPROTO_TCP;
6244 if (proto == Q_SCTP) {
6245 if (real_proto == IPPROTO_UDP)
6246 bpf_error("port '%s' is udp", name);
6248 else if (real_proto == IPPROTO_TCP)
6249 bpf_error("port '%s' is tcp", name);
6251 /* override PROTO_UNDEF */
6252 real_proto = IPPROTO_SCTP;
6255 bpf_error("illegal port number %d < 0", port);
6257 bpf_error("illegal port number %d > 65535", port);
6259 return gen_port(port, real_proto, dir);
6261 b = gen_port(port, real_proto, dir);
6262 gen_or(gen_port6(port, real_proto, dir), b);
6267 if (proto != Q_DEFAULT &&
6268 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6269 bpf_error("illegal qualifier of 'portrange'");
6270 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6271 bpf_error("unknown port in range '%s'", name);
6272 if (proto == Q_UDP) {
6273 if (real_proto == IPPROTO_TCP)
6274 bpf_error("port in range '%s' is tcp", name);
6275 else if (real_proto == IPPROTO_SCTP)
6276 bpf_error("port in range '%s' is sctp", name);
6278 /* override PROTO_UNDEF */
6279 real_proto = IPPROTO_UDP;
6281 if (proto == Q_TCP) {
6282 if (real_proto == IPPROTO_UDP)
6283 bpf_error("port in range '%s' is udp", name);
6284 else if (real_proto == IPPROTO_SCTP)
6285 bpf_error("port in range '%s' is sctp", name);
6287 /* override PROTO_UNDEF */
6288 real_proto = IPPROTO_TCP;
6290 if (proto == Q_SCTP) {
6291 if (real_proto == IPPROTO_UDP)
6292 bpf_error("port in range '%s' is udp", name);
6293 else if (real_proto == IPPROTO_TCP)
6294 bpf_error("port in range '%s' is tcp", name);
6296 /* override PROTO_UNDEF */
6297 real_proto = IPPROTO_SCTP;
6300 bpf_error("illegal port number %d < 0", port1);
6302 bpf_error("illegal port number %d > 65535", port1);
6304 bpf_error("illegal port number %d < 0", port2);
6306 bpf_error("illegal port number %d > 65535", port2);
6309 return gen_portrange(port1, port2, real_proto, dir);
6311 b = gen_portrange(port1, port2, real_proto, dir);
6312 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
6318 eaddr = pcap_ether_hostton(name);
6320 bpf_error("unknown ether host: %s", name);
6322 alist = pcap_nametoaddr(name);
6323 if (alist == NULL || *alist == NULL)
6324 bpf_error("unknown host '%s'", name);
6325 b = gen_gateway(eaddr, alist, proto, dir);
6329 bpf_error("'gateway' not supported in this configuration");
6333 real_proto = lookup_proto(name, proto);
6334 if (real_proto >= 0)
6335 return gen_proto(real_proto, proto, dir);
6337 bpf_error("unknown protocol: %s", name);
6340 real_proto = lookup_proto(name, proto);
6341 if (real_proto >= 0)
6342 return gen_protochain(real_proto, proto, dir);
6344 bpf_error("unknown protocol: %s", name);
6355 gen_mcode(s1, s2, masklen, q)
6356 register const char *s1, *s2;
6357 register int masklen;
6360 register int nlen, mlen;
6363 nlen = __pcap_atoin(s1, &n);
6364 /* Promote short ipaddr */
6368 mlen = __pcap_atoin(s2, &m);
6369 /* Promote short ipaddr */
6372 bpf_error("non-network bits set in \"%s mask %s\"",
6375 /* Convert mask len to mask */
6377 bpf_error("mask length must be <= 32");
6380 * X << 32 is not guaranteed by C to be 0; it's
6385 m = 0xffffffff << (32 - masklen);
6387 bpf_error("non-network bits set in \"%s/%d\"",
6394 return gen_host(n, m, q.proto, q.dir, q.addr);
6397 bpf_error("Mask syntax for networks only");
6406 register const char *s;
6411 int proto = q.proto;
6417 else if (q.proto == Q_DECNET)
6418 vlen = __pcap_atodn(s, &v);
6420 vlen = __pcap_atoin(s, &v);
6427 if (proto == Q_DECNET)
6428 return gen_host(v, 0, proto, dir, q.addr);
6429 else if (proto == Q_LINK) {
6430 bpf_error("illegal link layer address");
6433 if (s == NULL && q.addr == Q_NET) {
6434 /* Promote short net number */
6435 while (v && (v & 0xff000000) == 0) {
6440 /* Promote short ipaddr */
6444 return gen_host(v, mask, proto, dir, q.addr);
6449 proto = IPPROTO_UDP;
6450 else if (proto == Q_TCP)
6451 proto = IPPROTO_TCP;
6452 else if (proto == Q_SCTP)
6453 proto = IPPROTO_SCTP;
6454 else if (proto == Q_DEFAULT)
6455 proto = PROTO_UNDEF;
6457 bpf_error("illegal qualifier of 'port'");
6460 bpf_error("illegal port number %u > 65535", v);
6463 return gen_port((int)v, proto, dir);
6467 b = gen_port((int)v, proto, dir);
6468 gen_or(gen_port6((int)v, proto, dir), b);
6475 proto = IPPROTO_UDP;
6476 else if (proto == Q_TCP)
6477 proto = IPPROTO_TCP;
6478 else if (proto == Q_SCTP)
6479 proto = IPPROTO_SCTP;
6480 else if (proto == Q_DEFAULT)
6481 proto = PROTO_UNDEF;
6483 bpf_error("illegal qualifier of 'portrange'");
6486 bpf_error("illegal port number %u > 65535", v);
6489 return gen_portrange((int)v, (int)v, proto, dir);
6493 b = gen_portrange((int)v, (int)v, proto, dir);
6494 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
6500 bpf_error("'gateway' requires a name");
6504 return gen_proto((int)v, proto, dir);
6507 return gen_protochain((int)v, proto, dir);
6522 gen_mcode6(s1, s2, masklen, q)
6523 register const char *s1, *s2;
6524 register int masklen;
6527 struct addrinfo *res;
6528 struct in6_addr *addr;
6529 struct in6_addr mask;
6534 bpf_error("no mask %s supported", s2);
6536 res = pcap_nametoaddrinfo(s1);
6538 bpf_error("invalid ip6 address %s", s1);
6541 bpf_error("%s resolved to multiple address", s1);
6542 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
6544 if (sizeof(mask) * 8 < masklen)
6545 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6546 memset(&mask, 0, sizeof(mask));
6547 memset(&mask, 0xff, masklen / 8);
6549 mask.s6_addr[masklen / 8] =
6550 (0xff << (8 - masklen % 8)) & 0xff;
6553 a = (u_int32_t *)addr;
6554 m = (u_int32_t *)&mask;
6555 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
6556 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
6557 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
6565 bpf_error("Mask syntax for networks only");
6569 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
6575 bpf_error("invalid qualifier against IPv6 address");
6584 register const u_char *eaddr;
6587 struct block *b, *tmp;
6589 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6592 case DLT_NETANALYZER:
6593 case DLT_NETANALYZER_TRANSPARENT:
6594 return gen_ehostop(eaddr, (int)q.dir);
6596 return gen_fhostop(eaddr, (int)q.dir);
6598 return gen_thostop(eaddr, (int)q.dir);
6599 case DLT_IEEE802_11:
6600 case DLT_PRISM_HEADER:
6601 case DLT_IEEE802_11_RADIO_AVS:
6602 case DLT_IEEE802_11_RADIO:
6604 return gen_wlanhostop(eaddr, (int)q.dir);
6608 * Check that the packet doesn't begin with an
6609 * LE Control marker. (We've already generated
6612 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6617 * Now check the MAC address.
6619 b = gen_ehostop(eaddr, (int)q.dir);
6624 case DLT_IP_OVER_FC:
6625 return gen_ipfchostop(eaddr, (int)q.dir);
6627 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6631 bpf_error("ethernet address used in non-ether expression");
6638 struct slist *s0, *s1;
6641 * This is definitely not the best way to do this, but the
6642 * lists will rarely get long.
6649 static struct slist *
6655 s = new_stmt(BPF_LDX|BPF_MEM);
6660 static struct slist *
6666 s = new_stmt(BPF_LD|BPF_MEM);
6672 * Modify "index" to use the value stored into its register as an
6673 * offset relative to the beginning of the header for the protocol
6674 * "proto", and allocate a register and put an item "size" bytes long
6675 * (1, 2, or 4) at that offset into that register, making it the register
6679 gen_load(proto, inst, size)
6684 struct slist *s, *tmp;
6686 int regno = alloc_reg();
6688 free_reg(inst->regno);
6692 bpf_error("data size must be 1, 2, or 4");
6708 bpf_error("unsupported index operation");
6712 * The offset is relative to the beginning of the packet
6713 * data, if we have a radio header. (If we don't, this
6716 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
6717 linktype != DLT_IEEE802_11_RADIO &&
6718 linktype != DLT_PRISM_HEADER)
6719 bpf_error("radio information not present in capture");
6722 * Load into the X register the offset computed into the
6723 * register specified by "index".
6725 s = xfer_to_x(inst);
6728 * Load the item at that offset.
6730 tmp = new_stmt(BPF_LD|BPF_IND|size);
6732 sappend(inst->s, s);
6737 * The offset is relative to the beginning of
6738 * the link-layer header.
6740 * XXX - what about ATM LANE? Should the index be
6741 * relative to the beginning of the AAL5 frame, so
6742 * that 0 refers to the beginning of the LE Control
6743 * field, or relative to the beginning of the LAN
6744 * frame, so that 0 refers, for Ethernet LANE, to
6745 * the beginning of the destination address?
6747 s = gen_llprefixlen();
6750 * If "s" is non-null, it has code to arrange that the
6751 * X register contains the length of the prefix preceding
6752 * the link-layer header. Add to it the offset computed
6753 * into the register specified by "index", and move that
6754 * into the X register. Otherwise, just load into the X
6755 * register the offset computed into the register specified
6759 sappend(s, xfer_to_a(inst));
6760 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6761 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6763 s = xfer_to_x(inst);
6766 * Load the item at the sum of the offset we've put in the
6767 * X register and the offset of the start of the link
6768 * layer header (which is 0 if the radio header is
6769 * variable-length; that header length is what we put
6770 * into the X register and then added to the index).
6772 tmp = new_stmt(BPF_LD|BPF_IND|size);
6775 sappend(inst->s, s);
6791 * The offset is relative to the beginning of
6792 * the network-layer header.
6793 * XXX - are there any cases where we want
6796 s = gen_off_macpl();
6799 * If "s" is non-null, it has code to arrange that the
6800 * X register contains the offset of the MAC-layer
6801 * payload. Add to it the offset computed into the
6802 * register specified by "index", and move that into
6803 * the X register. Otherwise, just load into the X
6804 * register the offset computed into the register specified
6808 sappend(s, xfer_to_a(inst));
6809 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6810 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6812 s = xfer_to_x(inst);
6815 * Load the item at the sum of the offset we've put in the
6816 * X register, the offset of the start of the network
6817 * layer header from the beginning of the MAC-layer
6818 * payload, and the purported offset of the start of the
6819 * MAC-layer payload (which might be 0 if there's a
6820 * variable-length prefix before the link-layer header
6821 * or the link-layer header itself is variable-length;
6822 * the variable-length offset of the start of the
6823 * MAC-layer payload is what we put into the X register
6824 * and then added to the index).
6826 tmp = new_stmt(BPF_LD|BPF_IND|size);
6827 tmp->s.k = off_macpl + off_nl;
6829 sappend(inst->s, s);
6832 * Do the computation only if the packet contains
6833 * the protocol in question.
6835 b = gen_proto_abbrev(proto);
6837 gen_and(inst->b, b);
6851 * The offset is relative to the beginning of
6852 * the transport-layer header.
6854 * Load the X register with the length of the IPv4 header
6855 * (plus the offset of the link-layer header, if it's
6856 * a variable-length header), in bytes.
6858 * XXX - are there any cases where we want
6860 * XXX - we should, if we're built with
6861 * IPv6 support, generate code to load either
6862 * IPv4, IPv6, or both, as appropriate.
6864 s = gen_loadx_iphdrlen();
6867 * The X register now contains the sum of the length
6868 * of any variable-length header preceding the link-layer
6869 * header, any variable-length link-layer header, and the
6870 * length of the network-layer header.
6872 * Load into the A register the offset relative to
6873 * the beginning of the transport layer header,
6874 * add the X register to that, move that to the
6875 * X register, and load with an offset from the
6876 * X register equal to the offset of the network
6877 * layer header relative to the beginning of
6878 * the MAC-layer payload plus the fixed-length
6879 * portion of the offset of the MAC-layer payload
6880 * from the beginning of the raw packet data.
6882 sappend(s, xfer_to_a(inst));
6883 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6884 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6885 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
6886 tmp->s.k = off_macpl + off_nl;
6887 sappend(inst->s, s);
6890 * Do the computation only if the packet contains
6891 * the protocol in question - which is true only
6892 * if this is an IP datagram and is the first or
6893 * only fragment of that datagram.
6895 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
6897 gen_and(inst->b, b);
6899 gen_and(gen_proto_abbrev(Q_IP), b);
6905 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6909 inst->regno = regno;
6910 s = new_stmt(BPF_ST);
6912 sappend(inst->s, s);
6918 gen_relation(code, a0, a1, reversed)
6920 struct arth *a0, *a1;
6923 struct slist *s0, *s1, *s2;
6924 struct block *b, *tmp;
6928 if (code == BPF_JEQ) {
6929 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
6930 b = new_block(JMP(code));
6934 b = new_block(BPF_JMP|code|BPF_X);
6940 sappend(a0->s, a1->s);
6944 free_reg(a0->regno);
6945 free_reg(a1->regno);
6947 /* 'and' together protocol checks */
6950 gen_and(a0->b, tmp = a1->b);
6966 int regno = alloc_reg();
6967 struct arth *a = (struct arth *)newchunk(sizeof(*a));
6970 s = new_stmt(BPF_LD|BPF_LEN);
6971 s->next = new_stmt(BPF_ST);
6972 s->next->s.k = regno;
6987 a = (struct arth *)newchunk(sizeof(*a));
6991 s = new_stmt(BPF_LD|BPF_IMM);
6993 s->next = new_stmt(BPF_ST);
7009 s = new_stmt(BPF_ALU|BPF_NEG);
7012 s = new_stmt(BPF_ST);
7020 gen_arth(code, a0, a1)
7022 struct arth *a0, *a1;
7024 struct slist *s0, *s1, *s2;
7028 s2 = new_stmt(BPF_ALU|BPF_X|code);
7033 sappend(a0->s, a1->s);
7035 free_reg(a0->regno);
7036 free_reg(a1->regno);
7038 s0 = new_stmt(BPF_ST);
7039 a0->regno = s0->s.k = alloc_reg();
7046 * Here we handle simple allocation of the scratch registers.
7047 * If too many registers are alloc'd, the allocator punts.
7049 static int regused[BPF_MEMWORDS];
7053 * Initialize the table of used registers and the current register.
7059 memset(regused, 0, sizeof regused);
7063 * Return the next free register.
7068 int n = BPF_MEMWORDS;
7071 if (regused[curreg])
7072 curreg = (curreg + 1) % BPF_MEMWORDS;
7074 regused[curreg] = 1;
7078 bpf_error("too many registers needed to evaluate expression");
7084 * Return a register to the table so it can
7094 static struct block *
7101 s = new_stmt(BPF_LD|BPF_LEN);
7102 b = new_block(JMP(jmp));
7113 return gen_len(BPF_JGE, n);
7117 * Actually, this is less than or equal.
7125 b = gen_len(BPF_JGT, n);
7132 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7133 * the beginning of the link-layer header.
7134 * XXX - that means you can't test values in the radiotap header, but
7135 * as that header is difficult if not impossible to parse generally
7136 * without a loop, that might not be a severe problem. A new keyword
7137 * "radio" could be added for that, although what you'd really want
7138 * would be a way of testing particular radio header values, which
7139 * would generate code appropriate to the radio header in question.
7142 gen_byteop(op, idx, val)
7153 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7156 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7160 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7164 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
7168 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
7172 b = new_block(JMP(BPF_JEQ));
7179 static u_char abroadcast[] = { 0x0 };
7182 gen_broadcast(proto)
7185 bpf_u_int32 hostmask;
7186 struct block *b0, *b1, *b2;
7187 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7195 case DLT_ARCNET_LINUX:
7196 return gen_ahostop(abroadcast, Q_DST);
7198 case DLT_NETANALYZER:
7199 case DLT_NETANALYZER_TRANSPARENT:
7200 return gen_ehostop(ebroadcast, Q_DST);
7202 return gen_fhostop(ebroadcast, Q_DST);
7204 return gen_thostop(ebroadcast, Q_DST);
7205 case DLT_IEEE802_11:
7206 case DLT_PRISM_HEADER:
7207 case DLT_IEEE802_11_RADIO_AVS:
7208 case DLT_IEEE802_11_RADIO:
7210 return gen_wlanhostop(ebroadcast, Q_DST);
7211 case DLT_IP_OVER_FC:
7212 return gen_ipfchostop(ebroadcast, Q_DST);
7216 * Check that the packet doesn't begin with an
7217 * LE Control marker. (We've already generated
7220 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7225 * Now check the MAC address.
7227 b0 = gen_ehostop(ebroadcast, Q_DST);
7233 bpf_error("not a broadcast link");
7239 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7240 * as an indication that we don't know the netmask, and fail
7243 if (netmask == PCAP_NETMASK_UNKNOWN)
7244 bpf_error("netmask not known, so 'ip broadcast' not supported");
7245 b0 = gen_linktype(ETHERTYPE_IP);
7246 hostmask = ~netmask;
7247 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
7248 b2 = gen_mcmp(OR_NET, 16, BPF_W,
7249 (bpf_int32)(~0 & hostmask), hostmask);
7254 bpf_error("only link-layer/IP broadcast filters supported");
7260 * Generate code to test the low-order bit of a MAC address (that's
7261 * the bottom bit of the *first* byte).
7263 static struct block *
7264 gen_mac_multicast(offset)
7267 register struct block *b0;
7268 register struct slist *s;
7270 /* link[offset] & 1 != 0 */
7271 s = gen_load_a(OR_LINK, offset, BPF_B);
7272 b0 = new_block(JMP(BPF_JSET));
7279 gen_multicast(proto)
7282 register struct block *b0, *b1, *b2;
7283 register struct slist *s;
7291 case DLT_ARCNET_LINUX:
7292 /* all ARCnet multicasts use the same address */
7293 return gen_ahostop(abroadcast, Q_DST);
7295 case DLT_NETANALYZER:
7296 case DLT_NETANALYZER_TRANSPARENT:
7297 /* ether[0] & 1 != 0 */
7298 return gen_mac_multicast(0);
7301 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7303 * XXX - was that referring to bit-order issues?
7305 /* fddi[1] & 1 != 0 */
7306 return gen_mac_multicast(1);
7308 /* tr[2] & 1 != 0 */
7309 return gen_mac_multicast(2);
7310 case DLT_IEEE802_11:
7311 case DLT_PRISM_HEADER:
7312 case DLT_IEEE802_11_RADIO_AVS:
7313 case DLT_IEEE802_11_RADIO:
7318 * For control frames, there is no DA.
7320 * For management frames, DA is at an
7321 * offset of 4 from the beginning of
7324 * For data frames, DA is at an offset
7325 * of 4 from the beginning of the packet
7326 * if To DS is clear and at an offset of
7327 * 16 from the beginning of the packet
7332 * Generate the tests to be done for data frames.
7334 * First, check for To DS set, i.e. "link[1] & 0x01".
7336 s = gen_load_a(OR_LINK, 1, BPF_B);
7337 b1 = new_block(JMP(BPF_JSET));
7338 b1->s.k = 0x01; /* To DS */
7342 * If To DS is set, the DA is at 16.
7344 b0 = gen_mac_multicast(16);
7348 * Now, check for To DS not set, i.e. check
7349 * "!(link[1] & 0x01)".
7351 s = gen_load_a(OR_LINK, 1, BPF_B);
7352 b2 = new_block(JMP(BPF_JSET));
7353 b2->s.k = 0x01; /* To DS */
7358 * If To DS is not set, the DA is at 4.
7360 b1 = gen_mac_multicast(4);
7364 * Now OR together the last two checks. That gives
7365 * the complete set of checks for data frames.
7370 * Now check for a data frame.
7371 * I.e, check "link[0] & 0x08".
7373 s = gen_load_a(OR_LINK, 0, BPF_B);
7374 b1 = new_block(JMP(BPF_JSET));
7379 * AND that with the checks done for data frames.
7384 * If the high-order bit of the type value is 0, this
7385 * is a management frame.
7386 * I.e, check "!(link[0] & 0x08)".
7388 s = gen_load_a(OR_LINK, 0, BPF_B);
7389 b2 = new_block(JMP(BPF_JSET));
7395 * For management frames, the DA is at 4.
7397 b1 = gen_mac_multicast(4);
7401 * OR that with the checks done for data frames.
7402 * That gives the checks done for management and
7408 * If the low-order bit of the type value is 1,
7409 * this is either a control frame or a frame
7410 * with a reserved type, and thus not a
7413 * I.e., check "!(link[0] & 0x04)".
7415 s = gen_load_a(OR_LINK, 0, BPF_B);
7416 b1 = new_block(JMP(BPF_JSET));
7422 * AND that with the checks for data and management
7427 case DLT_IP_OVER_FC:
7428 b0 = gen_mac_multicast(2);
7433 * Check that the packet doesn't begin with an
7434 * LE Control marker. (We've already generated
7437 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7441 /* ether[off_mac] & 1 != 0 */
7442 b0 = gen_mac_multicast(off_mac);
7450 /* Link not known to support multicasts */
7454 b0 = gen_linktype(ETHERTYPE_IP);
7455 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
7461 b0 = gen_linktype(ETHERTYPE_IPV6);
7462 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
7467 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7473 * generate command for inbound/outbound. It's here so we can
7474 * make it link-type specific. 'dir' = 0 implies "inbound",
7475 * = 1 implies "outbound".
7481 register struct block *b0;
7484 * Only some data link types support inbound/outbound qualifiers.
7488 b0 = gen_relation(BPF_JEQ,
7489 gen_load(Q_LINK, gen_loadi(0), 1),
7496 /* match outgoing packets */
7497 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
7499 /* match incoming packets */
7500 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
7507 * Match packets sent by this machine.
7509 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
7512 * Match packets sent to this machine.
7513 * (No broadcast or multicast packets, or
7514 * packets sent to some other machine and
7515 * received promiscuously.)
7517 * XXX - packets sent to other machines probably
7518 * shouldn't be matched, but what about broadcast
7519 * or multicast packets we received?
7521 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
7525 #ifdef HAVE_NET_PFVAR_H
7527 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
7528 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
7534 /* match outgoing packets */
7535 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
7537 /* match incoming packets */
7538 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
7542 case DLT_JUNIPER_MFR:
7543 case DLT_JUNIPER_MLFR:
7544 case DLT_JUNIPER_MLPPP:
7545 case DLT_JUNIPER_ATM1:
7546 case DLT_JUNIPER_ATM2:
7547 case DLT_JUNIPER_PPPOE:
7548 case DLT_JUNIPER_PPPOE_ATM:
7549 case DLT_JUNIPER_GGSN:
7550 case DLT_JUNIPER_ES:
7551 case DLT_JUNIPER_MONITOR:
7552 case DLT_JUNIPER_SERVICES:
7553 case DLT_JUNIPER_ETHER:
7554 case DLT_JUNIPER_PPP:
7555 case DLT_JUNIPER_FRELAY:
7556 case DLT_JUNIPER_CHDLC:
7557 case DLT_JUNIPER_VP:
7558 case DLT_JUNIPER_ST:
7559 case DLT_JUNIPER_ISM:
7560 case DLT_JUNIPER_VS:
7561 case DLT_JUNIPER_SRX_E2E:
7562 case DLT_JUNIPER_FIBRECHANNEL:
7563 case DLT_JUNIPER_ATM_CEMIC:
7565 /* juniper flags (including direction) are stored
7566 * the byte after the 3-byte magic number */
7568 /* match outgoing packets */
7569 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
7571 /* match incoming packets */
7572 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
7577 bpf_error("inbound/outbound not supported on linktype %d",
7585 #ifdef HAVE_NET_PFVAR_H
7586 /* PF firewall log matched interface */
7588 gen_pf_ifname(const char *ifname)
7593 if (linktype != DLT_PFLOG) {
7594 bpf_error("ifname supported only on PF linktype");
7597 len = sizeof(((struct pfloghdr *)0)->ifname);
7598 off = offsetof(struct pfloghdr, ifname);
7599 if (strlen(ifname) >= len) {
7600 bpf_error("ifname interface names can only be %d characters",
7604 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
7608 /* PF firewall log ruleset name */
7610 gen_pf_ruleset(char *ruleset)
7614 if (linktype != DLT_PFLOG) {
7615 bpf_error("ruleset supported only on PF linktype");
7619 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
7620 bpf_error("ruleset names can only be %ld characters",
7621 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
7625 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
7626 strlen(ruleset), (const u_char *)ruleset);
7630 /* PF firewall log rule number */
7636 if (linktype != DLT_PFLOG) {
7637 bpf_error("rnr supported only on PF linktype");
7641 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
7646 /* PF firewall log sub-rule number */
7648 gen_pf_srnr(int srnr)
7652 if (linktype != DLT_PFLOG) {
7653 bpf_error("srnr supported only on PF linktype");
7657 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
7662 /* PF firewall log reason code */
7664 gen_pf_reason(int reason)
7668 if (linktype != DLT_PFLOG) {
7669 bpf_error("reason supported only on PF linktype");
7673 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
7678 /* PF firewall log action */
7680 gen_pf_action(int action)
7684 if (linktype != DLT_PFLOG) {
7685 bpf_error("action supported only on PF linktype");
7689 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
7693 #else /* !HAVE_NET_PFVAR_H */
7695 gen_pf_ifname(const char *ifname)
7697 bpf_error("libpcap was compiled without pf support");
7703 gen_pf_ruleset(char *ruleset)
7705 bpf_error("libpcap was compiled on a machine without pf support");
7713 bpf_error("libpcap was compiled on a machine without pf support");
7719 gen_pf_srnr(int srnr)
7721 bpf_error("libpcap was compiled on a machine without pf support");
7727 gen_pf_reason(int reason)
7729 bpf_error("libpcap was compiled on a machine without pf support");
7735 gen_pf_action(int action)
7737 bpf_error("libpcap was compiled on a machine without pf support");
7741 #endif /* HAVE_NET_PFVAR_H */
7743 /* IEEE 802.11 wireless header */
7745 gen_p80211_type(int type, int mask)
7751 case DLT_IEEE802_11:
7752 case DLT_PRISM_HEADER:
7753 case DLT_IEEE802_11_RADIO_AVS:
7754 case DLT_IEEE802_11_RADIO:
7755 b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
7760 bpf_error("802.11 link-layer types supported only on 802.11");
7768 gen_p80211_fcdir(int fcdir)
7774 case DLT_IEEE802_11:
7775 case DLT_PRISM_HEADER:
7776 case DLT_IEEE802_11_RADIO_AVS:
7777 case DLT_IEEE802_11_RADIO:
7781 bpf_error("frame direction supported only with 802.11 headers");
7785 b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
7786 (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
7793 register const u_char *eaddr;
7799 case DLT_ARCNET_LINUX:
7800 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
7802 return (gen_ahostop(eaddr, (int)q.dir));
7804 bpf_error("ARCnet address used in non-arc expression");
7810 bpf_error("aid supported only on ARCnet");
7813 bpf_error("ARCnet address used in non-arc expression");
7818 static struct block *
7819 gen_ahostop(eaddr, dir)
7820 register const u_char *eaddr;
7823 register struct block *b0, *b1;
7826 /* src comes first, different from Ethernet */
7828 return gen_bcmp(OR_LINK, 0, 1, eaddr);
7831 return gen_bcmp(OR_LINK, 1, 1, eaddr);
7834 b0 = gen_ahostop(eaddr, Q_SRC);
7835 b1 = gen_ahostop(eaddr, Q_DST);
7841 b0 = gen_ahostop(eaddr, Q_SRC);
7842 b1 = gen_ahostop(eaddr, Q_DST);
7847 bpf_error("'addr1' is only supported on 802.11");
7851 bpf_error("'addr2' is only supported on 802.11");
7855 bpf_error("'addr3' is only supported on 802.11");
7859 bpf_error("'addr4' is only supported on 802.11");
7863 bpf_error("'ra' is only supported on 802.11");
7867 bpf_error("'ta' is only supported on 802.11");
7875 * support IEEE 802.1Q VLAN trunk over ethernet
7881 struct block *b0, *b1;
7883 /* can't check for VLAN-encapsulated packets inside MPLS */
7884 if (label_stack_depth > 0)
7885 bpf_error("no VLAN match after MPLS");
7888 * Check for a VLAN packet, and then change the offsets to point
7889 * to the type and data fields within the VLAN packet. Just
7890 * increment the offsets, so that we can support a hierarchy, e.g.
7891 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7894 * XXX - this is a bit of a kludge. If we were to split the
7895 * compiler into a parser that parses an expression and
7896 * generates an expression tree, and a code generator that
7897 * takes an expression tree (which could come from our
7898 * parser or from some other parser) and generates BPF code,
7899 * we could perhaps make the offsets parameters of routines
7900 * and, in the handler for an "AND" node, pass to subnodes
7901 * other than the VLAN node the adjusted offsets.
7903 * This would mean that "vlan" would, instead of changing the
7904 * behavior of *all* tests after it, change only the behavior
7905 * of tests ANDed with it. That would change the documented
7906 * semantics of "vlan", which might break some expressions.
7907 * However, it would mean that "(vlan and ip) or ip" would check
7908 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7909 * checking only for VLAN-encapsulated IP, so that could still
7910 * be considered worth doing; it wouldn't break expressions
7911 * that are of the form "vlan and ..." or "vlan N and ...",
7912 * which I suspect are the most common expressions involving
7913 * "vlan". "vlan or ..." doesn't necessarily do what the user
7914 * would really want, now, as all the "or ..." tests would
7915 * be done assuming a VLAN, even though the "or" could be viewed
7916 * as meaning "or, if this isn't a VLAN packet...".
7923 case DLT_NETANALYZER:
7924 case DLT_NETANALYZER_TRANSPARENT:
7925 /* check for VLAN, including QinQ */
7926 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7927 (bpf_int32)ETHERTYPE_8021Q);
7928 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7929 (bpf_int32)ETHERTYPE_8021QINQ);
7933 /* If a specific VLAN is requested, check VLAN id */
7934 if (vlan_num >= 0) {
7935 b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
7936 (bpf_int32)vlan_num, 0x0fff);
7950 bpf_error("no VLAN support for data link type %d",
7965 struct block *b0,*b1;
7968 * Change the offsets to point to the type and data fields within
7969 * the MPLS packet. Just increment the offsets, so that we
7970 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7971 * capture packets with an outer label of 100000 and an inner
7974 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7978 if (label_stack_depth > 0) {
7979 /* just match the bottom-of-stack bit clear */
7980 b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
7983 * Indicate that we're checking MPLS-encapsulated headers,
7984 * to make sure higher level code generators don't try to
7985 * match against IP-related protocols such as Q_ARP, Q_RARP
7990 case DLT_C_HDLC: /* fall through */
7992 case DLT_NETANALYZER:
7993 case DLT_NETANALYZER_TRANSPARENT:
7994 b0 = gen_linktype(ETHERTYPE_MPLS);
7998 b0 = gen_linktype(PPP_MPLS_UCAST);
8001 /* FIXME add other DLT_s ...
8002 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8003 * leave it for now */
8006 bpf_error("no MPLS support for data link type %d",
8014 /* If a specific MPLS label is requested, check it */
8015 if (label_num >= 0) {
8016 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
8017 b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
8018 0xfffff000); /* only compare the first 20 bits */
8025 label_stack_depth++;
8030 * Support PPPOE discovery and session.
8035 /* check for PPPoE discovery */
8036 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
8045 * Test against the PPPoE session link-layer type.
8047 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
8050 * Change the offsets to point to the type and data fields within
8051 * the PPP packet, and note that this is PPPoE rather than
8054 * XXX - this is a bit of a kludge. If we were to split the
8055 * compiler into a parser that parses an expression and
8056 * generates an expression tree, and a code generator that
8057 * takes an expression tree (which could come from our
8058 * parser or from some other parser) and generates BPF code,
8059 * we could perhaps make the offsets parameters of routines
8060 * and, in the handler for an "AND" node, pass to subnodes
8061 * other than the PPPoE node the adjusted offsets.
8063 * This would mean that "pppoes" would, instead of changing the
8064 * behavior of *all* tests after it, change only the behavior
8065 * of tests ANDed with it. That would change the documented
8066 * semantics of "pppoes", which might break some expressions.
8067 * However, it would mean that "(pppoes and ip) or ip" would check
8068 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8069 * checking only for VLAN-encapsulated IP, so that could still
8070 * be considered worth doing; it wouldn't break expressions
8071 * that are of the form "pppoes and ..." which I suspect are the
8072 * most common expressions involving "pppoes". "pppoes or ..."
8073 * doesn't necessarily do what the user would really want, now,
8074 * as all the "or ..." tests would be done assuming PPPoE, even
8075 * though the "or" could be viewed as meaning "or, if this isn't
8076 * a PPPoE packet...".
8078 orig_linktype = off_linktype; /* save original values */
8083 * The "network-layer" protocol is PPPoE, which has a 6-byte
8084 * PPPoE header, followed by a PPP packet.
8086 * There is no HDLC encapsulation for the PPP packet (it's
8087 * encapsulated in PPPoES instead), so the link-layer type
8088 * starts at the first byte of the PPP packet. For PPPoE,
8089 * that offset is relative to the beginning of the total
8090 * link-layer payload, including any 802.2 LLC header, so
8091 * it's 6 bytes past off_nl.
8093 off_linktype = off_nl + 6;
8096 * The network-layer offsets are relative to the beginning
8097 * of the MAC-layer payload; that's past the 6-byte
8098 * PPPoE header and the 2-byte PPP header.
8101 off_nl_nosnap = 6+2;
8107 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
8119 bpf_error("'vpi' supported only on raw ATM");
8120 if (off_vpi == (u_int)-1)
8122 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
8128 bpf_error("'vci' supported only on raw ATM");
8129 if (off_vci == (u_int)-1)
8131 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
8136 if (off_proto == (u_int)-1)
8137 abort(); /* XXX - this isn't on FreeBSD */
8138 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
8143 if (off_payload == (u_int)-1)
8145 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
8146 0xffffffff, jtype, reverse, jvalue);
8151 bpf_error("'callref' supported only on raw ATM");
8152 if (off_proto == (u_int)-1)
8154 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
8155 jtype, reverse, jvalue);
8165 gen_atmtype_abbrev(type)
8168 struct block *b0, *b1;
8173 /* Get all packets in Meta signalling Circuit */
8175 bpf_error("'metac' supported only on raw ATM");
8176 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8177 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
8182 /* Get all packets in Broadcast Circuit*/
8184 bpf_error("'bcc' supported only on raw ATM");
8185 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8186 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
8191 /* Get all cells in Segment OAM F4 circuit*/
8193 bpf_error("'oam4sc' supported only on raw ATM");
8194 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8195 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8200 /* Get all cells in End-to-End OAM F4 Circuit*/
8202 bpf_error("'oam4ec' supported only on raw ATM");
8203 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8204 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8209 /* Get all packets in connection Signalling Circuit */
8211 bpf_error("'sc' supported only on raw ATM");
8212 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8213 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
8218 /* Get all packets in ILMI Circuit */
8220 bpf_error("'ilmic' supported only on raw ATM");
8221 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8222 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
8227 /* Get all LANE packets */
8229 bpf_error("'lane' supported only on raw ATM");
8230 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
8233 * Arrange that all subsequent tests assume LANE
8234 * rather than LLC-encapsulated packets, and set
8235 * the offsets appropriately for LANE-encapsulated
8238 * "off_mac" is the offset of the Ethernet header,
8239 * which is 2 bytes past the ATM pseudo-header
8240 * (skipping the pseudo-header and 2-byte LE Client
8241 * field). The other offsets are Ethernet offsets
8242 * relative to "off_mac".
8245 off_mac = off_payload + 2; /* MAC header */
8246 off_linktype = off_mac + 12;
8247 off_macpl = off_mac + 14; /* Ethernet */
8248 off_nl = 0; /* Ethernet II */
8249 off_nl_nosnap = 3; /* 802.3+802.2 */
8253 /* Get all LLC-encapsulated packets */
8255 bpf_error("'llc' supported only on raw ATM");
8256 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
8267 * Filtering for MTP2 messages based on li value
8268 * FISU, length is null
8269 * LSSU, length is 1 or 2
8270 * MSU, length is 3 or more
8273 gen_mtp2type_abbrev(type)
8276 struct block *b0, *b1;
8281 if ( (linktype != DLT_MTP2) &&
8282 (linktype != DLT_ERF) &&
8283 (linktype != DLT_MTP2_WITH_PHDR) )
8284 bpf_error("'fisu' supported only on MTP2");
8285 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8286 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8290 if ( (linktype != DLT_MTP2) &&
8291 (linktype != DLT_ERF) &&
8292 (linktype != DLT_MTP2_WITH_PHDR) )
8293 bpf_error("'lssu' supported only on MTP2");
8294 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8295 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
8300 if ( (linktype != DLT_MTP2) &&
8301 (linktype != DLT_ERF) &&
8302 (linktype != DLT_MTP2_WITH_PHDR) )
8303 bpf_error("'msu' supported only on MTP2");
8304 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
8314 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
8321 bpf_u_int32 val1 , val2 , val3;
8323 switch (mtp3field) {
8326 if (off_sio == (u_int)-1)
8327 bpf_error("'sio' supported only on SS7");
8328 /* sio coded on 1 byte so max value 255 */
8330 bpf_error("sio value %u too big; max value = 255",
8332 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
8333 (u_int)jtype, reverse, (u_int)jvalue);
8337 if (off_opc == (u_int)-1)
8338 bpf_error("'opc' supported only on SS7");
8339 /* opc coded on 14 bits so max value 16383 */
8341 bpf_error("opc value %u too big; max value = 16383",
8343 /* the following instructions are made to convert jvalue
8344 * to the form used to write opc in an ss7 message*/
8345 val1 = jvalue & 0x00003c00;
8347 val2 = jvalue & 0x000003fc;
8349 val3 = jvalue & 0x00000003;
8351 jvalue = val1 + val2 + val3;
8352 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
8353 (u_int)jtype, reverse, (u_int)jvalue);
8357 if (off_dpc == (u_int)-1)
8358 bpf_error("'dpc' supported only on SS7");
8359 /* dpc coded on 14 bits so max value 16383 */
8361 bpf_error("dpc value %u too big; max value = 16383",
8363 /* the following instructions are made to convert jvalue
8364 * to the forme used to write dpc in an ss7 message*/
8365 val1 = jvalue & 0x000000ff;
8367 val2 = jvalue & 0x00003f00;
8369 jvalue = val1 + val2;
8370 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
8371 (u_int)jtype, reverse, (u_int)jvalue);
8375 if (off_sls == (u_int)-1)
8376 bpf_error("'sls' supported only on SS7");
8377 /* sls coded on 4 bits so max value 15 */
8379 bpf_error("sls value %u too big; max value = 15",
8381 /* the following instruction is made to convert jvalue
8382 * to the forme used to write sls in an ss7 message*/
8383 jvalue = jvalue << 4;
8384 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
8385 (u_int)jtype,reverse, (u_int)jvalue);
8394 static struct block *
8395 gen_msg_abbrev(type)
8401 * Q.2931 signalling protocol messages for handling virtual circuits
8402 * establishment and teardown
8407 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
8411 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
8415 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
8419 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
8423 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
8426 case A_RELEASE_DONE:
8427 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
8437 gen_atmmulti_abbrev(type)
8440 struct block *b0, *b1;
8446 bpf_error("'oam' supported only on raw ATM");
8447 b1 = gen_atmmulti_abbrev(A_OAMF4);
8452 bpf_error("'oamf4' supported only on raw ATM");
8454 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8455 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8457 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8463 * Get Q.2931 signalling messages for switched
8464 * virtual connection
8467 bpf_error("'connectmsg' supported only on raw ATM");
8468 b0 = gen_msg_abbrev(A_SETUP);
8469 b1 = gen_msg_abbrev(A_CALLPROCEED);
8471 b0 = gen_msg_abbrev(A_CONNECT);
8473 b0 = gen_msg_abbrev(A_CONNECTACK);
8475 b0 = gen_msg_abbrev(A_RELEASE);
8477 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8479 b0 = gen_atmtype_abbrev(A_SC);
8485 bpf_error("'metaconnect' supported only on raw ATM");
8486 b0 = gen_msg_abbrev(A_SETUP);
8487 b1 = gen_msg_abbrev(A_CALLPROCEED);
8489 b0 = gen_msg_abbrev(A_CONNECT);
8491 b0 = gen_msg_abbrev(A_RELEASE);
8493 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8495 b0 = gen_atmtype_abbrev(A_METAC);