1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
34 #include <pcap-stdinc.h>
41 #ifdef HAVE_SYS_BITYPES_H
42 #include <sys/bitypes.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
49 * XXX - why was this included even on UNIX?
58 #include <sys/param.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
78 #include "ethertype.h"
82 #include "ieee80211.h"
84 #include "sunatmpos.h"
87 #include "pcap/ipnet.h"
89 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
90 #include <linux/types.h>
91 #include <linux/if_packet.h>
92 #include <linux/filter.h>
94 #ifdef HAVE_NET_PFVAR_H
95 #include <sys/socket.h>
97 #include <net/pfvar.h>
98 #include <net/if_pflog.h>
101 #define offsetof(s, e) ((size_t)&((s *)0)->e)
105 #include <netdb.h> /* for "struct addrinfo" */
108 #include <pcap/namedb.h>
110 #define ETHERMTU 1500
112 #ifndef IPPROTO_HOPOPTS
113 #define IPPROTO_HOPOPTS 0
115 #ifndef IPPROTO_ROUTING
116 #define IPPROTO_ROUTING 43
118 #ifndef IPPROTO_FRAGMENT
119 #define IPPROTO_FRAGMENT 44
121 #ifndef IPPROTO_DSTOPTS
122 #define IPPROTO_DSTOPTS 60
125 #define IPPROTO_SCTP 132
128 #ifdef HAVE_OS_PROTO_H
129 #include "os-proto.h"
132 #define JMP(c) ((c)|BPF_JMP|BPF_K)
135 static jmp_buf top_ctx;
136 static pcap_t *bpf_pcap;
138 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
140 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
142 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
147 static int pcap_fddipad;
152 bpf_error(const char *fmt, ...)
157 if (bpf_pcap != NULL)
158 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
165 static void init_linktype(pcap_t *);
167 static void init_regs(void);
168 static int alloc_reg(void);
169 static void free_reg(int);
171 static struct block *root;
174 * Value passed to gen_load_a() to indicate what the offset argument
178 OR_PACKET, /* relative to the beginning of the packet */
179 OR_LINK, /* relative to the beginning of the link-layer header */
180 OR_MACPL, /* relative to the end of the MAC-layer header */
181 OR_NET, /* relative to the network-layer header */
182 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
183 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
184 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
189 * As errors are handled by a longjmp, anything allocated must be freed
190 * in the longjmp handler, so it must be reachable from that handler.
191 * One thing that's allocated is the result of pcap_nametoaddrinfo();
192 * it must be freed with freeaddrinfo(). This variable points to any
193 * addrinfo structure that would need to be freed.
195 static struct addrinfo *ai;
199 * We divy out chunks of memory rather than call malloc each time so
200 * we don't have to worry about leaking memory. It's probably
201 * not a big deal if all this memory was wasted but if this ever
202 * goes into a library that would probably not be a good idea.
204 * XXX - this *is* in a library....
207 #define CHUNK0SIZE 1024
213 static struct chunk chunks[NCHUNKS];
214 static int cur_chunk;
216 static void *newchunk(u_int);
217 static void freechunks(void);
218 static inline struct block *new_block(int);
219 static inline struct slist *new_stmt(int);
220 static struct block *gen_retblk(int);
221 static inline void syntax(void);
223 static void backpatch(struct block *, struct block *);
224 static void merge(struct block *, struct block *);
225 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
226 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
227 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
228 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
229 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
230 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
232 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
233 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
234 bpf_u_int32, bpf_u_int32, int, bpf_int32);
235 static struct slist *gen_load_llrel(u_int, u_int);
236 static struct slist *gen_load_macplrel(u_int, u_int);
237 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
238 static struct slist *gen_loadx_iphdrlen(void);
239 static struct block *gen_uncond(int);
240 static inline struct block *gen_true(void);
241 static inline struct block *gen_false(void);
242 static struct block *gen_ether_linktype(int);
243 static struct block *gen_ipnet_linktype(int);
244 static struct block *gen_linux_sll_linktype(int);
245 static struct slist *gen_load_prism_llprefixlen(void);
246 static struct slist *gen_load_avs_llprefixlen(void);
247 static struct slist *gen_load_radiotap_llprefixlen(void);
248 static struct slist *gen_load_ppi_llprefixlen(void);
249 static void insert_compute_vloffsets(struct block *);
250 static struct slist *gen_llprefixlen(void);
251 static struct slist *gen_off_macpl(void);
252 static int ethertype_to_ppptype(int);
253 static struct block *gen_linktype(int);
254 static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
255 static struct block *gen_llc_linktype(int);
256 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
258 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
260 static struct block *gen_ahostop(const u_char *, int);
261 static struct block *gen_ehostop(const u_char *, int);
262 static struct block *gen_fhostop(const u_char *, int);
263 static struct block *gen_thostop(const u_char *, int);
264 static struct block *gen_wlanhostop(const u_char *, int);
265 static struct block *gen_ipfchostop(const u_char *, int);
266 static struct block *gen_dnhostop(bpf_u_int32, int);
267 static struct block *gen_mpls_linktype(int);
268 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
270 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
273 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
275 static struct block *gen_ipfrag(void);
276 static struct block *gen_portatom(int, bpf_int32);
277 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
278 static struct block *gen_portatom6(int, bpf_int32);
279 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
280 struct block *gen_portop(int, int, int);
281 static struct block *gen_port(int, int, int);
282 struct block *gen_portrangeop(int, int, int, int);
283 static struct block *gen_portrange(int, int, int, int);
284 struct block *gen_portop6(int, int, int);
285 static struct block *gen_port6(int, int, int);
286 struct block *gen_portrangeop6(int, int, int, int);
287 static struct block *gen_portrange6(int, int, int, int);
288 static int lookup_proto(const char *, int);
289 static struct block *gen_protochain(int, int, int);
290 static struct block *gen_proto(int, int, int);
291 static struct slist *xfer_to_x(struct arth *);
292 static struct slist *xfer_to_a(struct arth *);
293 static struct block *gen_mac_multicast(int);
294 static struct block *gen_len(int, int);
295 static struct block *gen_check_802_11_data_frame(void);
297 static struct block *gen_ppi_dlt_check(void);
298 static struct block *gen_msg_abbrev(int type);
309 /* XXX Round up to nearest long. */
310 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
312 /* XXX Round up to structure boundary. */
316 cp = &chunks[cur_chunk];
317 if (n > cp->n_left) {
318 ++cp, k = ++cur_chunk;
320 bpf_error("out of memory");
321 size = CHUNK0SIZE << k;
322 cp->m = (void *)malloc(size);
324 bpf_error("out of memory");
325 memset((char *)cp->m, 0, size);
328 bpf_error("out of memory");
331 return (void *)((char *)cp->m + cp->n_left);
340 for (i = 0; i < NCHUNKS; ++i)
341 if (chunks[i].m != NULL) {
348 * A strdup whose allocations are freed after code generation is over.
352 register const char *s;
354 int n = strlen(s) + 1;
355 char *cp = newchunk(n);
361 static inline struct block *
367 p = (struct block *)newchunk(sizeof(*p));
374 static inline struct slist *
380 p = (struct slist *)newchunk(sizeof(*p));
386 static struct block *
390 struct block *b = new_block(BPF_RET|BPF_K);
399 bpf_error("syntax error in filter expression");
402 static bpf_u_int32 netmask;
407 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
408 const char *buf, int optimize, bpf_u_int32 mask);
411 pcap_compile(pcap_t *p, struct bpf_program *program,
412 const char *buf, int optimize, bpf_u_int32 mask)
416 EnterCriticalSection(&g_PcapCompileCriticalSection);
418 result = pcap_compile_unsafe(p, program, buf, optimize, mask);
420 LeaveCriticalSection(&g_PcapCompileCriticalSection);
426 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
427 const char *buf, int optimize, bpf_u_int32 mask)
430 pcap_compile(pcap_t *p, struct bpf_program *program,
431 const char *buf, int optimize, bpf_u_int32 mask)
435 const char * volatile xbuf = buf;
439 * If this pcap_t hasn't been activated, it doesn't have a
440 * link-layer type, so we can't use it.
443 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
444 "not-yet-activated pcap_t passed to pcap_compile");
452 if (setjmp(top_ctx)) {
466 snaplen = pcap_snapshot(p);
468 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
469 "snaplen of 0 rejects all packets");
473 lex_init(xbuf ? xbuf : "");
481 root = gen_retblk(snaplen);
483 if (optimize && !no_optimize) {
486 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
487 bpf_error("expression rejects all packets");
489 program->bf_insns = icode_to_fcode(root, &len);
490 program->bf_len = len;
498 * entry point for using the compiler with no pcap open
499 * pass in all the stuff that is needed explicitly instead.
502 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
503 struct bpf_program *program,
504 const char *buf, int optimize, bpf_u_int32 mask)
509 p = pcap_open_dead(linktype_arg, snaplen_arg);
512 ret = pcap_compile(p, program, buf, optimize, mask);
518 * Clean up a "struct bpf_program" by freeing all the memory allocated
522 pcap_freecode(struct bpf_program *program)
525 if (program->bf_insns != NULL) {
526 free((char *)program->bf_insns);
527 program->bf_insns = NULL;
532 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
533 * which of the jt and jf fields has been resolved and which is a pointer
534 * back to another unresolved block (or nil). At least one of the fields
535 * in each block is already resolved.
538 backpatch(list, target)
539 struct block *list, *target;
556 * Merge the lists in b0 and b1, using the 'sense' field to indicate
557 * which of jt and jf is the link.
561 struct block *b0, *b1;
563 register struct block **p = &b0;
565 /* Find end of list. */
567 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
569 /* Concatenate the lists. */
577 struct block *ppi_dlt_check;
580 * Insert before the statements of the first (root) block any
581 * statements needed to load the lengths of any variable-length
582 * headers into registers.
584 * XXX - a fancier strategy would be to insert those before the
585 * statements of all blocks that use those lengths and that
586 * have no predecessors that use them, so that we only compute
587 * the lengths if we need them. There might be even better
588 * approaches than that.
590 * However, those strategies would be more complicated, and
591 * as we don't generate code to compute a length if the
592 * program has no tests that use the length, and as most
593 * tests will probably use those lengths, we would just
594 * postpone computing the lengths so that it's not done
595 * for tests that fail early, and it's not clear that's
598 insert_compute_vloffsets(p->head);
601 * For DLT_PPI captures, generate a check of the per-packet
602 * DLT value to make sure it's DLT_IEEE802_11.
604 ppi_dlt_check = gen_ppi_dlt_check();
605 if (ppi_dlt_check != NULL)
606 gen_and(ppi_dlt_check, p);
608 backpatch(p, gen_retblk(snaplen));
609 p->sense = !p->sense;
610 backpatch(p, gen_retblk(0));
616 struct block *b0, *b1;
618 backpatch(b0, b1->head);
619 b0->sense = !b0->sense;
620 b1->sense = !b1->sense;
622 b1->sense = !b1->sense;
628 struct block *b0, *b1;
630 b0->sense = !b0->sense;
631 backpatch(b0, b1->head);
632 b0->sense = !b0->sense;
641 b->sense = !b->sense;
644 static struct block *
645 gen_cmp(offrel, offset, size, v)
646 enum e_offrel offrel;
650 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
653 static struct block *
654 gen_cmp_gt(offrel, offset, size, v)
655 enum e_offrel offrel;
659 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
662 static struct block *
663 gen_cmp_ge(offrel, offset, size, v)
664 enum e_offrel offrel;
668 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
671 static struct block *
672 gen_cmp_lt(offrel, offset, size, v)
673 enum e_offrel offrel;
677 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
680 static struct block *
681 gen_cmp_le(offrel, offset, size, v)
682 enum e_offrel offrel;
686 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
689 static struct block *
690 gen_mcmp(offrel, offset, size, v, mask)
691 enum e_offrel offrel;
696 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
699 static struct block *
700 gen_bcmp(offrel, offset, size, v)
701 enum e_offrel offrel;
702 register u_int offset, size;
703 register const u_char *v;
705 register struct block *b, *tmp;
709 register const u_char *p = &v[size - 4];
710 bpf_int32 w = ((bpf_int32)p[0] << 24) |
711 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
713 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
720 register const u_char *p = &v[size - 2];
721 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
723 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
730 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
739 * AND the field of size "size" at offset "offset" relative to the header
740 * specified by "offrel" with "mask", and compare it with the value "v"
741 * with the test specified by "jtype"; if "reverse" is true, the test
742 * should test the opposite of "jtype".
744 static struct block *
745 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
746 enum e_offrel offrel;
748 bpf_u_int32 offset, size, mask, jtype;
751 struct slist *s, *s2;
754 s = gen_load_a(offrel, offset, size);
756 if (mask != 0xffffffff) {
757 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
762 b = new_block(JMP(jtype));
765 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
771 * Various code constructs need to know the layout of the data link
772 * layer. These variables give the necessary offsets from the beginning
773 * of the packet data.
777 * This is the offset of the beginning of the link-layer header from
778 * the beginning of the raw packet data.
780 * It's usually 0, except for 802.11 with a fixed-length radio header.
781 * (For 802.11 with a variable-length radio header, we have to generate
782 * code to compute that offset; off_ll is 0 in that case.)
787 * If there's a variable-length header preceding the link-layer header,
788 * "reg_off_ll" is the register number for a register containing the
789 * length of that header, and therefore the offset of the link-layer
790 * header from the beginning of the raw packet data. Otherwise,
791 * "reg_off_ll" is -1.
793 static int reg_off_ll;
796 * This is the offset of the beginning of the MAC-layer header from
797 * the beginning of the link-layer header.
798 * It's usually 0, except for ATM LANE, where it's the offset, relative
799 * to the beginning of the raw packet data, of the Ethernet header, and
800 * for Ethernet with various additional information.
802 static u_int off_mac;
805 * This is the offset of the beginning of the MAC-layer payload,
806 * from the beginning of the raw packet data.
808 * I.e., it's the sum of the length of the link-layer header (without,
809 * for example, any 802.2 LLC header, so it's the MAC-layer
810 * portion of that header), plus any prefix preceding the
813 static u_int off_macpl;
816 * This is 1 if the offset of the beginning of the MAC-layer payload
817 * from the beginning of the link-layer header is variable-length.
819 static int off_macpl_is_variable;
822 * If the link layer has variable_length headers, "reg_off_macpl"
823 * is the register number for a register containing the length of the
824 * link-layer header plus the length of any variable-length header
825 * preceding the link-layer header. Otherwise, "reg_off_macpl"
828 static int reg_off_macpl;
831 * "off_linktype" is the offset to information in the link-layer header
832 * giving the packet type. This offset is relative to the beginning
833 * of the link-layer header (i.e., it doesn't include off_ll).
835 * For Ethernet, it's the offset of the Ethernet type field.
837 * For link-layer types that always use 802.2 headers, it's the
838 * offset of the LLC header.
840 * For PPP, it's the offset of the PPP type field.
842 * For Cisco HDLC, it's the offset of the CHDLC type field.
844 * For BSD loopback, it's the offset of the AF_ value.
846 * For Linux cooked sockets, it's the offset of the type field.
848 * It's set to -1 for no encapsulation, in which case, IP is assumed.
850 static u_int off_linktype;
853 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
854 * checks to check the PPP header, assumed to follow a LAN-style link-
855 * layer header and a PPPoE session header.
857 static int is_pppoes = 0;
860 * TRUE if the link layer includes an ATM pseudo-header.
862 static int is_atm = 0;
865 * TRUE if "lane" appeared in the filter; it causes us to generate
866 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
868 static int is_lane = 0;
871 * These are offsets for the ATM pseudo-header.
873 static u_int off_vpi;
874 static u_int off_vci;
875 static u_int off_proto;
878 * These are offsets for the MTP2 fields.
883 * These are offsets for the MTP3 fields.
885 static u_int off_sio;
886 static u_int off_opc;
887 static u_int off_dpc;
888 static u_int off_sls;
891 * This is the offset of the first byte after the ATM pseudo_header,
892 * or -1 if there is no ATM pseudo-header.
894 static u_int off_payload;
897 * These are offsets to the beginning of the network-layer header.
898 * They are relative to the beginning of the MAC-layer payload (i.e.,
899 * they don't include off_ll or off_macpl).
901 * If the link layer never uses 802.2 LLC:
903 * "off_nl" and "off_nl_nosnap" are the same.
905 * If the link layer always uses 802.2 LLC:
907 * "off_nl" is the offset if there's a SNAP header following
910 * "off_nl_nosnap" is the offset if there's no SNAP header.
912 * If the link layer is Ethernet:
914 * "off_nl" is the offset if the packet is an Ethernet II packet
915 * (we assume no 802.3+802.2+SNAP);
917 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
918 * with an 802.2 header following it.
921 static u_int off_nl_nosnap;
929 linktype = pcap_datalink(p);
931 pcap_fddipad = p->fddipad;
935 * Assume it's not raw ATM with a pseudo-header, for now.
946 * And that we're not doing PPPoE.
951 * And assume we're not doing SS7.
960 * Also assume it's not 802.11.
964 off_macpl_is_variable = 0;
968 label_stack_depth = 0;
978 off_nl = 0; /* XXX in reality, variable! */
979 off_nl_nosnap = 0; /* no 802.2 LLC */
982 case DLT_ARCNET_LINUX:
985 off_nl = 0; /* XXX in reality, variable! */
986 off_nl_nosnap = 0; /* no 802.2 LLC */
991 off_macpl = 14; /* Ethernet header length */
992 off_nl = 0; /* Ethernet II */
993 off_nl_nosnap = 3; /* 802.3+802.2 */
998 * SLIP doesn't have a link level type. The 16 byte
999 * header is hacked into our SLIP driver.
1004 off_nl_nosnap = 0; /* no 802.2 LLC */
1007 case DLT_SLIP_BSDOS:
1008 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1013 off_nl_nosnap = 0; /* no 802.2 LLC */
1021 off_nl_nosnap = 0; /* no 802.2 LLC */
1028 off_nl_nosnap = 0; /* no 802.2 LLC */
1033 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1034 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1038 off_nl_nosnap = 0; /* no 802.2 LLC */
1043 * This does no include the Ethernet header, and
1044 * only covers session state.
1049 off_nl_nosnap = 0; /* no 802.2 LLC */
1056 off_nl_nosnap = 0; /* no 802.2 LLC */
1061 * FDDI doesn't really have a link-level type field.
1062 * We set "off_linktype" to the offset of the LLC header.
1064 * To check for Ethernet types, we assume that SSAP = SNAP
1065 * is being used and pick out the encapsulated Ethernet type.
1066 * XXX - should we generate code to check for SNAP?
1070 off_linktype += pcap_fddipad;
1072 off_macpl = 13; /* FDDI MAC header length */
1074 off_macpl += pcap_fddipad;
1076 off_nl = 8; /* 802.2+SNAP */
1077 off_nl_nosnap = 3; /* 802.2 */
1082 * Token Ring doesn't really have a link-level type field.
1083 * We set "off_linktype" to the offset of the LLC header.
1085 * To check for Ethernet types, we assume that SSAP = SNAP
1086 * is being used and pick out the encapsulated Ethernet type.
1087 * XXX - should we generate code to check for SNAP?
1089 * XXX - the header is actually variable-length.
1090 * Some various Linux patched versions gave 38
1091 * as "off_linktype" and 40 as "off_nl"; however,
1092 * if a token ring packet has *no* routing
1093 * information, i.e. is not source-routed, the correct
1094 * values are 20 and 22, as they are in the vanilla code.
1096 * A packet is source-routed iff the uppermost bit
1097 * of the first byte of the source address, at an
1098 * offset of 8, has the uppermost bit set. If the
1099 * packet is source-routed, the total number of bytes
1100 * of routing information is 2 plus bits 0x1F00 of
1101 * the 16-bit value at an offset of 14 (shifted right
1102 * 8 - figure out which byte that is).
1105 off_macpl = 14; /* Token Ring MAC header length */
1106 off_nl = 8; /* 802.2+SNAP */
1107 off_nl_nosnap = 3; /* 802.2 */
1110 case DLT_IEEE802_11:
1111 case DLT_PRISM_HEADER:
1112 case DLT_IEEE802_11_RADIO_AVS:
1113 case DLT_IEEE802_11_RADIO:
1115 * 802.11 doesn't really have a link-level type field.
1116 * We set "off_linktype" to the offset of the LLC header.
1118 * To check for Ethernet types, we assume that SSAP = SNAP
1119 * is being used and pick out the encapsulated Ethernet type.
1120 * XXX - should we generate code to check for SNAP?
1122 * We also handle variable-length radio headers here.
1123 * The Prism header is in theory variable-length, but in
1124 * practice it's always 144 bytes long. However, some
1125 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1126 * sometimes or always supply an AVS header, so we
1127 * have to check whether the radio header is a Prism
1128 * header or an AVS header, so, in practice, it's
1132 off_macpl = 0; /* link-layer header is variable-length */
1133 off_macpl_is_variable = 1;
1134 off_nl = 8; /* 802.2+SNAP */
1135 off_nl_nosnap = 3; /* 802.2 */
1140 * At the moment we treat PPI the same way that we treat
1141 * normal Radiotap encoded packets. The difference is in
1142 * the function that generates the code at the beginning
1143 * to compute the header length. Since this code generator
1144 * of PPI supports bare 802.11 encapsulation only (i.e.
1145 * the encapsulated DLT should be DLT_IEEE802_11) we
1146 * generate code to check for this too.
1149 off_macpl = 0; /* link-layer header is variable-length */
1150 off_macpl_is_variable = 1;
1151 off_nl = 8; /* 802.2+SNAP */
1152 off_nl_nosnap = 3; /* 802.2 */
1155 case DLT_ATM_RFC1483:
1156 case DLT_ATM_CLIP: /* Linux ATM defines this */
1158 * assume routed, non-ISO PDUs
1159 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1161 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1162 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1163 * latter would presumably be treated the way PPPoE
1164 * should be, so you can do "pppoe and udp port 2049"
1165 * or "pppoa and tcp port 80" and have it check for
1166 * PPPo{A,E} and a PPP protocol of IP and....
1169 off_macpl = 0; /* packet begins with LLC header */
1170 off_nl = 8; /* 802.2+SNAP */
1171 off_nl_nosnap = 3; /* 802.2 */
1176 * Full Frontal ATM; you get AALn PDUs with an ATM
1180 off_vpi = SUNATM_VPI_POS;
1181 off_vci = SUNATM_VCI_POS;
1182 off_proto = PROTO_POS;
1183 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1184 off_payload = SUNATM_PKT_BEGIN_POS;
1185 off_linktype = off_payload;
1186 off_macpl = off_payload; /* if LLC-encapsulated */
1187 off_nl = 8; /* 802.2+SNAP */
1188 off_nl_nosnap = 3; /* 802.2 */
1197 off_nl_nosnap = 0; /* no 802.2 LLC */
1200 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1204 off_nl_nosnap = 0; /* no 802.2 LLC */
1209 * LocalTalk does have a 1-byte type field in the LLAP header,
1210 * but really it just indicates whether there is a "short" or
1211 * "long" DDP packet following.
1216 off_nl_nosnap = 0; /* no 802.2 LLC */
1219 case DLT_IP_OVER_FC:
1221 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1222 * link-level type field. We set "off_linktype" to the
1223 * offset of the LLC header.
1225 * To check for Ethernet types, we assume that SSAP = SNAP
1226 * is being used and pick out the encapsulated Ethernet type.
1227 * XXX - should we generate code to check for SNAP? RFC
1228 * 2625 says SNAP should be used.
1232 off_nl = 8; /* 802.2+SNAP */
1233 off_nl_nosnap = 3; /* 802.2 */
1238 * XXX - we should set this to handle SNAP-encapsulated
1239 * frames (NLPID of 0x80).
1244 off_nl_nosnap = 0; /* no 802.2 LLC */
1248 * the only BPF-interesting FRF.16 frames are non-control frames;
1249 * Frame Relay has a variable length link-layer
1250 * so lets start with offset 4 for now and increments later on (FIXME);
1256 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1259 case DLT_APPLE_IP_OVER_IEEE1394:
1263 off_nl_nosnap = 0; /* no 802.2 LLC */
1266 case DLT_SYMANTEC_FIREWALL:
1269 off_nl = 0; /* Ethernet II */
1270 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1273 #ifdef HAVE_NET_PFVAR_H
1276 off_macpl = PFLOG_HDRLEN;
1278 off_nl_nosnap = 0; /* no 802.2 LLC */
1282 case DLT_JUNIPER_MFR:
1283 case DLT_JUNIPER_MLFR:
1284 case DLT_JUNIPER_MLPPP:
1285 case DLT_JUNIPER_PPP:
1286 case DLT_JUNIPER_CHDLC:
1287 case DLT_JUNIPER_FRELAY:
1291 off_nl_nosnap = -1; /* no 802.2 LLC */
1294 case DLT_JUNIPER_ATM1:
1295 off_linktype = 4; /* in reality variable between 4-8 */
1296 off_macpl = 4; /* in reality variable between 4-8 */
1301 case DLT_JUNIPER_ATM2:
1302 off_linktype = 8; /* in reality variable between 8-12 */
1303 off_macpl = 8; /* in reality variable between 8-12 */
1308 /* frames captured on a Juniper PPPoE service PIC
1309 * contain raw ethernet frames */
1310 case DLT_JUNIPER_PPPOE:
1311 case DLT_JUNIPER_ETHER:
1314 off_nl = 18; /* Ethernet II */
1315 off_nl_nosnap = 21; /* 802.3+802.2 */
1318 case DLT_JUNIPER_PPPOE_ATM:
1322 off_nl_nosnap = -1; /* no 802.2 LLC */
1325 case DLT_JUNIPER_GGSN:
1329 off_nl_nosnap = -1; /* no 802.2 LLC */
1332 case DLT_JUNIPER_ES:
1334 off_macpl = -1; /* not really a network layer but raw IP addresses */
1335 off_nl = -1; /* not really a network layer but raw IP addresses */
1336 off_nl_nosnap = -1; /* no 802.2 LLC */
1339 case DLT_JUNIPER_MONITOR:
1342 off_nl = 0; /* raw IP/IP6 header */
1343 off_nl_nosnap = -1; /* no 802.2 LLC */
1346 case DLT_JUNIPER_SERVICES:
1348 off_macpl = -1; /* L3 proto location dep. on cookie type */
1349 off_nl = -1; /* L3 proto location dep. on cookie type */
1350 off_nl_nosnap = -1; /* no 802.2 LLC */
1353 case DLT_JUNIPER_VP:
1360 case DLT_JUNIPER_ST:
1367 case DLT_JUNIPER_ISM:
1374 case DLT_JUNIPER_VS:
1375 case DLT_JUNIPER_SRX_E2E:
1376 case DLT_JUNIPER_FIBRECHANNEL:
1377 case DLT_JUNIPER_ATM_CEMIC:
1396 case DLT_MTP2_WITH_PHDR:
1429 * Currently, only raw "link[N:M]" filtering is supported.
1431 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1433 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1434 off_nl_nosnap = -1; /* no 802.2 LLC */
1435 off_mac = 1; /* step over the kiss length byte */
1440 off_macpl = 24; /* ipnet header length */
1445 case DLT_NETANALYZER:
1446 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1447 off_linktype = 16; /* includes 4-byte pseudo-header */
1448 off_macpl = 18; /* pseudo-header+Ethernet header length */
1449 off_nl = 0; /* Ethernet II */
1450 off_nl_nosnap = 3; /* 802.3+802.2 */
1453 case DLT_NETANALYZER_TRANSPARENT:
1454 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1455 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1456 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1457 off_nl = 0; /* Ethernet II */
1458 off_nl_nosnap = 3; /* 802.3+802.2 */
1463 * For values in the range in which we've assigned new
1464 * DLT_ values, only raw "link[N:M]" filtering is supported.
1466 if (linktype >= DLT_MATCHING_MIN &&
1467 linktype <= DLT_MATCHING_MAX) {
1476 bpf_error("unknown data link type %d", linktype);
1481 * Load a value relative to the beginning of the link-layer header.
1482 * The link-layer header doesn't necessarily begin at the beginning
1483 * of the packet data; there might be a variable-length prefix containing
1484 * radio information.
1486 static struct slist *
1487 gen_load_llrel(offset, size)
1490 struct slist *s, *s2;
1492 s = gen_llprefixlen();
1495 * If "s" is non-null, it has code to arrange that the X register
1496 * contains the length of the prefix preceding the link-layer
1499 * Otherwise, the length of the prefix preceding the link-layer
1500 * header is "off_ll".
1504 * There's a variable-length prefix preceding the
1505 * link-layer header. "s" points to a list of statements
1506 * that put the length of that prefix into the X register.
1507 * do an indirect load, to use the X register as an offset.
1509 s2 = new_stmt(BPF_LD|BPF_IND|size);
1514 * There is no variable-length header preceding the
1515 * link-layer header; add in off_ll, which, if there's
1516 * a fixed-length header preceding the link-layer header,
1517 * is the length of that header.
1519 s = new_stmt(BPF_LD|BPF_ABS|size);
1520 s->s.k = offset + off_ll;
1526 * Load a value relative to the beginning of the MAC-layer payload.
1528 static struct slist *
1529 gen_load_macplrel(offset, size)
1532 struct slist *s, *s2;
1534 s = gen_off_macpl();
1537 * If s is non-null, the offset of the MAC-layer payload is
1538 * variable, and s points to a list of instructions that
1539 * arrange that the X register contains that offset.
1541 * Otherwise, the offset of the MAC-layer payload is constant,
1542 * and is in off_macpl.
1546 * The offset of the MAC-layer payload is in the X
1547 * register. Do an indirect load, to use the X register
1550 s2 = new_stmt(BPF_LD|BPF_IND|size);
1555 * The offset of the MAC-layer payload is constant,
1556 * and is in off_macpl; load the value at that offset
1557 * plus the specified offset.
1559 s = new_stmt(BPF_LD|BPF_ABS|size);
1560 s->s.k = off_macpl + offset;
1566 * Load a value relative to the beginning of the specified header.
1568 static struct slist *
1569 gen_load_a(offrel, offset, size)
1570 enum e_offrel offrel;
1573 struct slist *s, *s2;
1578 s = new_stmt(BPF_LD|BPF_ABS|size);
1583 s = gen_load_llrel(offset, size);
1587 s = gen_load_macplrel(offset, size);
1591 s = gen_load_macplrel(off_nl + offset, size);
1595 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1600 * Load the X register with the length of the IPv4 header
1601 * (plus the offset of the link-layer header, if it's
1602 * preceded by a variable-length header such as a radio
1603 * header), in bytes.
1605 s = gen_loadx_iphdrlen();
1608 * Load the item at {offset of the MAC-layer payload} +
1609 * {offset, relative to the start of the MAC-layer
1610 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1611 * {specified offset}.
1613 * (If the offset of the MAC-layer payload is variable,
1614 * it's included in the value in the X register, and
1617 s2 = new_stmt(BPF_LD|BPF_IND|size);
1618 s2->s.k = off_macpl + off_nl + offset;
1623 s = gen_load_macplrel(off_nl + 40 + offset, size);
1634 * Generate code to load into the X register the sum of the length of
1635 * the IPv4 header and any variable-length header preceding the link-layer
1638 static struct slist *
1639 gen_loadx_iphdrlen()
1641 struct slist *s, *s2;
1643 s = gen_off_macpl();
1646 * There's a variable-length prefix preceding the
1647 * link-layer header, or the link-layer header is itself
1648 * variable-length. "s" points to a list of statements
1649 * that put the offset of the MAC-layer payload into
1652 * The 4*([k]&0xf) addressing mode can't be used, as we
1653 * don't have a constant offset, so we have to load the
1654 * value in question into the A register and add to it
1655 * the value from the X register.
1657 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1660 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1663 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1668 * The A register now contains the length of the
1669 * IP header. We need to add to it the offset of
1670 * the MAC-layer payload, which is still in the X
1671 * register, and move the result into the X register.
1673 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1674 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1677 * There is no variable-length header preceding the
1678 * link-layer header, and the link-layer header is
1679 * fixed-length; load the length of the IPv4 header,
1680 * which is at an offset of off_nl from the beginning
1681 * of the MAC-layer payload, and thus at an offset
1682 * of off_mac_pl + off_nl from the beginning of the
1685 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1686 s->s.k = off_macpl + off_nl;
1691 static struct block *
1698 s = new_stmt(BPF_LD|BPF_IMM);
1700 b = new_block(JMP(BPF_JEQ));
1706 static inline struct block *
1709 return gen_uncond(1);
1712 static inline struct block *
1715 return gen_uncond(0);
1719 * Byte-swap a 32-bit number.
1720 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1721 * big-endian platforms.)
1723 #define SWAPLONG(y) \
1724 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1727 * Generate code to match a particular packet type.
1729 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1730 * value, if <= ETHERMTU. We use that to determine whether to
1731 * match the type/length field or to check the type/length field for
1732 * a value <= ETHERMTU to see whether it's a type field and then do
1733 * the appropriate test.
1735 static struct block *
1736 gen_ether_linktype(proto)
1739 struct block *b0, *b1;
1745 case LLCSAP_NETBEUI:
1747 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1748 * so we check the DSAP and SSAP.
1750 * LLCSAP_IP checks for IP-over-802.2, rather
1751 * than IP-over-Ethernet or IP-over-SNAP.
1753 * XXX - should we check both the DSAP and the
1754 * SSAP, like this, or should we check just the
1755 * DSAP, as we do for other types <= ETHERMTU
1756 * (i.e., other SAP values)?
1758 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1760 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1761 ((proto << 8) | proto));
1769 * Ethernet_II frames, which are Ethernet
1770 * frames with a frame type of ETHERTYPE_IPX;
1772 * Ethernet_802.3 frames, which are 802.3
1773 * frames (i.e., the type/length field is
1774 * a length field, <= ETHERMTU, rather than
1775 * a type field) with the first two bytes
1776 * after the Ethernet/802.3 header being
1779 * Ethernet_802.2 frames, which are 802.3
1780 * frames with an 802.2 LLC header and
1781 * with the IPX LSAP as the DSAP in the LLC
1784 * Ethernet_SNAP frames, which are 802.3
1785 * frames with an LLC header and a SNAP
1786 * header and with an OUI of 0x000000
1787 * (encapsulated Ethernet) and a protocol
1788 * ID of ETHERTYPE_IPX in the SNAP header.
1790 * XXX - should we generate the same code both
1791 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1795 * This generates code to check both for the
1796 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1798 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1799 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1803 * Now we add code to check for SNAP frames with
1804 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1806 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1810 * Now we generate code to check for 802.3
1811 * frames in general.
1813 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1817 * Now add the check for 802.3 frames before the
1818 * check for Ethernet_802.2 and Ethernet_802.3,
1819 * as those checks should only be done on 802.3
1820 * frames, not on Ethernet frames.
1825 * Now add the check for Ethernet_II frames, and
1826 * do that before checking for the other frame
1829 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1830 (bpf_int32)ETHERTYPE_IPX);
1834 case ETHERTYPE_ATALK:
1835 case ETHERTYPE_AARP:
1837 * EtherTalk (AppleTalk protocols on Ethernet link
1838 * layer) may use 802.2 encapsulation.
1842 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1843 * we check for an Ethernet type field less than
1844 * 1500, which means it's an 802.3 length field.
1846 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1850 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1851 * SNAP packets with an organization code of
1852 * 0x080007 (Apple, for Appletalk) and a protocol
1853 * type of ETHERTYPE_ATALK (Appletalk).
1855 * 802.2-encapsulated ETHERTYPE_AARP packets are
1856 * SNAP packets with an organization code of
1857 * 0x000000 (encapsulated Ethernet) and a protocol
1858 * type of ETHERTYPE_AARP (Appletalk ARP).
1860 if (proto == ETHERTYPE_ATALK)
1861 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1862 else /* proto == ETHERTYPE_AARP */
1863 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1867 * Check for Ethernet encapsulation (Ethertalk
1868 * phase 1?); we just check for the Ethernet
1871 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1877 if (proto <= ETHERMTU) {
1879 * This is an LLC SAP value, so the frames
1880 * that match would be 802.2 frames.
1881 * Check that the frame is an 802.2 frame
1882 * (i.e., that the length/type field is
1883 * a length field, <= ETHERMTU) and
1884 * then check the DSAP.
1886 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1888 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1894 * This is an Ethernet type, so compare
1895 * the length/type field with it (if
1896 * the frame is an 802.2 frame, the length
1897 * field will be <= ETHERMTU, and, as
1898 * "proto" is > ETHERMTU, this test
1899 * will fail and the frame won't match,
1900 * which is what we want).
1902 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1909 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1910 * or IPv6 then we have an error.
1912 static struct block *
1913 gen_ipnet_linktype(proto)
1919 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1920 (bpf_int32)IPH_AF_INET);
1923 case ETHERTYPE_IPV6:
1924 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1925 (bpf_int32)IPH_AF_INET6);
1936 * Generate code to match a particular packet type.
1938 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1939 * value, if <= ETHERMTU. We use that to determine whether to
1940 * match the type field or to check the type field for the special
1941 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1943 static struct block *
1944 gen_linux_sll_linktype(proto)
1947 struct block *b0, *b1;
1953 case LLCSAP_NETBEUI:
1955 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1956 * so we check the DSAP and SSAP.
1958 * LLCSAP_IP checks for IP-over-802.2, rather
1959 * than IP-over-Ethernet or IP-over-SNAP.
1961 * XXX - should we check both the DSAP and the
1962 * SSAP, like this, or should we check just the
1963 * DSAP, as we do for other types <= ETHERMTU
1964 * (i.e., other SAP values)?
1966 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1967 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1968 ((proto << 8) | proto));
1974 * Ethernet_II frames, which are Ethernet
1975 * frames with a frame type of ETHERTYPE_IPX;
1977 * Ethernet_802.3 frames, which have a frame
1978 * type of LINUX_SLL_P_802_3;
1980 * Ethernet_802.2 frames, which are 802.3
1981 * frames with an 802.2 LLC header (i.e, have
1982 * a frame type of LINUX_SLL_P_802_2) and
1983 * with the IPX LSAP as the DSAP in the LLC
1986 * Ethernet_SNAP frames, which are 802.3
1987 * frames with an LLC header and a SNAP
1988 * header and with an OUI of 0x000000
1989 * (encapsulated Ethernet) and a protocol
1990 * ID of ETHERTYPE_IPX in the SNAP header.
1992 * First, do the checks on LINUX_SLL_P_802_2
1993 * frames; generate the check for either
1994 * Ethernet_802.2 or Ethernet_SNAP frames, and
1995 * then put a check for LINUX_SLL_P_802_2 frames
1998 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1999 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
2001 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2005 * Now check for 802.3 frames and OR that with
2006 * the previous test.
2008 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
2012 * Now add the check for Ethernet_II frames, and
2013 * do that before checking for the other frame
2016 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2017 (bpf_int32)ETHERTYPE_IPX);
2021 case ETHERTYPE_ATALK:
2022 case ETHERTYPE_AARP:
2024 * EtherTalk (AppleTalk protocols on Ethernet link
2025 * layer) may use 802.2 encapsulation.
2029 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2030 * we check for the 802.2 protocol type in the
2031 * "Ethernet type" field.
2033 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2036 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2037 * SNAP packets with an organization code of
2038 * 0x080007 (Apple, for Appletalk) and a protocol
2039 * type of ETHERTYPE_ATALK (Appletalk).
2041 * 802.2-encapsulated ETHERTYPE_AARP packets are
2042 * SNAP packets with an organization code of
2043 * 0x000000 (encapsulated Ethernet) and a protocol
2044 * type of ETHERTYPE_AARP (Appletalk ARP).
2046 if (proto == ETHERTYPE_ATALK)
2047 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2048 else /* proto == ETHERTYPE_AARP */
2049 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2053 * Check for Ethernet encapsulation (Ethertalk
2054 * phase 1?); we just check for the Ethernet
2057 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2063 if (proto <= ETHERMTU) {
2065 * This is an LLC SAP value, so the frames
2066 * that match would be 802.2 frames.
2067 * Check for the 802.2 protocol type
2068 * in the "Ethernet type" field, and
2069 * then check the DSAP.
2071 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2073 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2079 * This is an Ethernet type, so compare
2080 * the length/type field with it (if
2081 * the frame is an 802.2 frame, the length
2082 * field will be <= ETHERMTU, and, as
2083 * "proto" is > ETHERMTU, this test
2084 * will fail and the frame won't match,
2085 * which is what we want).
2087 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2093 static struct slist *
2094 gen_load_prism_llprefixlen()
2096 struct slist *s1, *s2;
2097 struct slist *sjeq_avs_cookie;
2098 struct slist *sjcommon;
2101 * This code is not compatible with the optimizer, as
2102 * we are generating jmp instructions within a normal
2103 * slist of instructions
2108 * Generate code to load the length of the radio header into
2109 * the register assigned to hold that length, if one has been
2110 * assigned. (If one hasn't been assigned, no code we've
2111 * generated uses that prefix, so we don't need to generate any
2114 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2115 * or always use the AVS header rather than the Prism header.
2116 * We load a 4-byte big-endian value at the beginning of the
2117 * raw packet data, and see whether, when masked with 0xFFFFF000,
2118 * it's equal to 0x80211000. If so, that indicates that it's
2119 * an AVS header (the masked-out bits are the version number).
2120 * Otherwise, it's a Prism header.
2122 * XXX - the Prism header is also, in theory, variable-length,
2123 * but no known software generates headers that aren't 144
2126 if (reg_off_ll != -1) {
2130 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2134 * AND it with 0xFFFFF000.
2136 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2137 s2->s.k = 0xFFFFF000;
2141 * Compare with 0x80211000.
2143 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2144 sjeq_avs_cookie->s.k = 0x80211000;
2145 sappend(s1, sjeq_avs_cookie);
2150 * The 4 bytes at an offset of 4 from the beginning of
2151 * the AVS header are the length of the AVS header.
2152 * That field is big-endian.
2154 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2157 sjeq_avs_cookie->s.jt = s2;
2160 * Now jump to the code to allocate a register
2161 * into which to save the header length and
2162 * store the length there. (The "jump always"
2163 * instruction needs to have the k field set;
2164 * it's added to the PC, so, as we're jumping
2165 * over a single instruction, it should be 1.)
2167 sjcommon = new_stmt(JMP(BPF_JA));
2169 sappend(s1, sjcommon);
2172 * Now for the code that handles the Prism header.
2173 * Just load the length of the Prism header (144)
2174 * into the A register. Have the test for an AVS
2175 * header branch here if we don't have an AVS header.
2177 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2180 sjeq_avs_cookie->s.jf = s2;
2183 * Now allocate a register to hold that value and store
2184 * it. The code for the AVS header will jump here after
2185 * loading the length of the AVS header.
2187 s2 = new_stmt(BPF_ST);
2188 s2->s.k = reg_off_ll;
2190 sjcommon->s.jf = s2;
2193 * Now move it into the X register.
2195 s2 = new_stmt(BPF_MISC|BPF_TAX);
2203 static struct slist *
2204 gen_load_avs_llprefixlen()
2206 struct slist *s1, *s2;
2209 * Generate code to load the length of the AVS header into
2210 * the register assigned to hold that length, if one has been
2211 * assigned. (If one hasn't been assigned, no code we've
2212 * generated uses that prefix, so we don't need to generate any
2215 if (reg_off_ll != -1) {
2217 * The 4 bytes at an offset of 4 from the beginning of
2218 * the AVS header are the length of the AVS header.
2219 * That field is big-endian.
2221 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2225 * Now allocate a register to hold that value and store
2228 s2 = new_stmt(BPF_ST);
2229 s2->s.k = reg_off_ll;
2233 * Now move it into the X register.
2235 s2 = new_stmt(BPF_MISC|BPF_TAX);
2243 static struct slist *
2244 gen_load_radiotap_llprefixlen()
2246 struct slist *s1, *s2;
2249 * Generate code to load the length of the radiotap header into
2250 * the register assigned to hold that length, if one has been
2251 * assigned. (If one hasn't been assigned, no code we've
2252 * generated uses that prefix, so we don't need to generate any
2255 if (reg_off_ll != -1) {
2257 * The 2 bytes at offsets of 2 and 3 from the beginning
2258 * of the radiotap header are the length of the radiotap
2259 * header; unfortunately, it's little-endian, so we have
2260 * to load it a byte at a time and construct the value.
2264 * Load the high-order byte, at an offset of 3, shift it
2265 * left a byte, and put the result in the X register.
2267 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2269 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2272 s2 = new_stmt(BPF_MISC|BPF_TAX);
2276 * Load the next byte, at an offset of 2, and OR the
2277 * value from the X register into it.
2279 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2282 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2286 * Now allocate a register to hold that value and store
2289 s2 = new_stmt(BPF_ST);
2290 s2->s.k = reg_off_ll;
2294 * Now move it into the X register.
2296 s2 = new_stmt(BPF_MISC|BPF_TAX);
2305 * At the moment we treat PPI as normal Radiotap encoded
2306 * packets. The difference is in the function that generates
2307 * the code at the beginning to compute the header length.
2308 * Since this code generator of PPI supports bare 802.11
2309 * encapsulation only (i.e. the encapsulated DLT should be
2310 * DLT_IEEE802_11) we generate code to check for this too;
2311 * that's done in finish_parse().
2313 static struct slist *
2314 gen_load_ppi_llprefixlen()
2316 struct slist *s1, *s2;
2319 * Generate code to load the length of the radiotap header
2320 * into the register assigned to hold that length, if one has
2323 if (reg_off_ll != -1) {
2325 * The 2 bytes at offsets of 2 and 3 from the beginning
2326 * of the radiotap header are the length of the radiotap
2327 * header; unfortunately, it's little-endian, so we have
2328 * to load it a byte at a time and construct the value.
2332 * Load the high-order byte, at an offset of 3, shift it
2333 * left a byte, and put the result in the X register.
2335 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2337 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2340 s2 = new_stmt(BPF_MISC|BPF_TAX);
2344 * Load the next byte, at an offset of 2, and OR the
2345 * value from the X register into it.
2347 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2350 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2354 * Now allocate a register to hold that value and store
2357 s2 = new_stmt(BPF_ST);
2358 s2->s.k = reg_off_ll;
2362 * Now move it into the X register.
2364 s2 = new_stmt(BPF_MISC|BPF_TAX);
2373 * Load a value relative to the beginning of the link-layer header after the 802.11
2374 * header, i.e. LLC_SNAP.
2375 * The link-layer header doesn't necessarily begin at the beginning
2376 * of the packet data; there might be a variable-length prefix containing
2377 * radio information.
2379 static struct slist *
2380 gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2383 struct slist *sjset_data_frame_1;
2384 struct slist *sjset_data_frame_2;
2385 struct slist *sjset_qos;
2386 struct slist *sjset_radiotap_flags;
2387 struct slist *sjset_radiotap_tsft;
2388 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2389 struct slist *s_roundup;
2391 if (reg_off_macpl == -1) {
2393 * No register has been assigned to the offset of
2394 * the MAC-layer payload, which means nobody needs
2395 * it; don't bother computing it - just return
2396 * what we already have.
2402 * This code is not compatible with the optimizer, as
2403 * we are generating jmp instructions within a normal
2404 * slist of instructions
2409 * If "s" is non-null, it has code to arrange that the X register
2410 * contains the length of the prefix preceding the link-layer
2413 * Otherwise, the length of the prefix preceding the link-layer
2414 * header is "off_ll".
2418 * There is no variable-length header preceding the
2419 * link-layer header.
2421 * Load the length of the fixed-length prefix preceding
2422 * the link-layer header (if any) into the X register,
2423 * and store it in the reg_off_macpl register.
2424 * That length is off_ll.
2426 s = new_stmt(BPF_LDX|BPF_IMM);
2431 * The X register contains the offset of the beginning of the
2432 * link-layer header; add 24, which is the minimum length
2433 * of the MAC header for a data frame, to that, and store it
2434 * in reg_off_macpl, and then load the Frame Control field,
2435 * which is at the offset in the X register, with an indexed load.
2437 s2 = new_stmt(BPF_MISC|BPF_TXA);
2439 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2442 s2 = new_stmt(BPF_ST);
2443 s2->s.k = reg_off_macpl;
2446 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2451 * Check the Frame Control field to see if this is a data frame;
2452 * a data frame has the 0x08 bit (b3) in that field set and the
2453 * 0x04 bit (b2) clear.
2455 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2456 sjset_data_frame_1->s.k = 0x08;
2457 sappend(s, sjset_data_frame_1);
2460 * If b3 is set, test b2, otherwise go to the first statement of
2461 * the rest of the program.
2463 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2464 sjset_data_frame_2->s.k = 0x04;
2465 sappend(s, sjset_data_frame_2);
2466 sjset_data_frame_1->s.jf = snext;
2469 * If b2 is not set, this is a data frame; test the QoS bit.
2470 * Otherwise, go to the first statement of the rest of the
2473 sjset_data_frame_2->s.jt = snext;
2474 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2475 sjset_qos->s.k = 0x80; /* QoS bit */
2476 sappend(s, sjset_qos);
2479 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2481 * Otherwise, go to the first statement of the rest of the
2484 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2485 s2->s.k = reg_off_macpl;
2487 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2490 s2 = new_stmt(BPF_ST);
2491 s2->s.k = reg_off_macpl;
2495 * If we have a radiotap header, look at it to see whether
2496 * there's Atheros padding between the MAC-layer header
2499 * Note: all of the fields in the radiotap header are
2500 * little-endian, so we byte-swap all of the values
2501 * we test against, as they will be loaded as big-endian
2504 if (linktype == DLT_IEEE802_11_RADIO) {
2506 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2507 * in the presence flag?
2509 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2513 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2514 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2515 sappend(s, sjset_radiotap_flags);
2518 * If not, skip all of this.
2520 sjset_radiotap_flags->s.jf = snext;
2523 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2525 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2526 new_stmt(JMP(BPF_JSET));
2527 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2528 sappend(s, sjset_radiotap_tsft);
2531 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2532 * at an offset of 16 from the beginning of the raw packet
2533 * data (8 bytes for the radiotap header and 8 bytes for
2536 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2539 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2543 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2544 sjset_tsft_datapad->s.k = 0x20;
2545 sappend(s, sjset_tsft_datapad);
2548 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2549 * at an offset of 8 from the beginning of the raw packet
2550 * data (8 bytes for the radiotap header).
2552 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2555 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2559 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2560 sjset_notsft_datapad->s.k = 0x20;
2561 sappend(s, sjset_notsft_datapad);
2564 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2565 * set, round the length of the 802.11 header to
2566 * a multiple of 4. Do that by adding 3 and then
2567 * dividing by and multiplying by 4, which we do by
2570 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2571 s_roundup->s.k = reg_off_macpl;
2572 sappend(s, s_roundup);
2573 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2576 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2579 s2 = new_stmt(BPF_ST);
2580 s2->s.k = reg_off_macpl;
2583 sjset_tsft_datapad->s.jt = s_roundup;
2584 sjset_tsft_datapad->s.jf = snext;
2585 sjset_notsft_datapad->s.jt = s_roundup;
2586 sjset_notsft_datapad->s.jf = snext;
2588 sjset_qos->s.jf = snext;
2594 insert_compute_vloffsets(b)
2600 * For link-layer types that have a variable-length header
2601 * preceding the link-layer header, generate code to load
2602 * the offset of the link-layer header into the register
2603 * assigned to that offset, if any.
2607 case DLT_PRISM_HEADER:
2608 s = gen_load_prism_llprefixlen();
2611 case DLT_IEEE802_11_RADIO_AVS:
2612 s = gen_load_avs_llprefixlen();
2615 case DLT_IEEE802_11_RADIO:
2616 s = gen_load_radiotap_llprefixlen();
2620 s = gen_load_ppi_llprefixlen();
2629 * For link-layer types that have a variable-length link-layer
2630 * header, generate code to load the offset of the MAC-layer
2631 * payload into the register assigned to that offset, if any.
2635 case DLT_IEEE802_11:
2636 case DLT_PRISM_HEADER:
2637 case DLT_IEEE802_11_RADIO_AVS:
2638 case DLT_IEEE802_11_RADIO:
2640 s = gen_load_802_11_header_len(s, b->stmts);
2645 * If we have any offset-loading code, append all the
2646 * existing statements in the block to those statements,
2647 * and make the resulting list the list of statements
2651 sappend(s, b->stmts);
2656 static struct block *
2657 gen_ppi_dlt_check(void)
2659 struct slist *s_load_dlt;
2662 if (linktype == DLT_PPI)
2664 /* Create the statements that check for the DLT
2666 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2667 s_load_dlt->s.k = 4;
2669 b = new_block(JMP(BPF_JEQ));
2671 b->stmts = s_load_dlt;
2672 b->s.k = SWAPLONG(DLT_IEEE802_11);
2682 static struct slist *
2683 gen_prism_llprefixlen(void)
2687 if (reg_off_ll == -1) {
2689 * We haven't yet assigned a register for the length
2690 * of the radio header; allocate one.
2692 reg_off_ll = alloc_reg();
2696 * Load the register containing the radio length
2697 * into the X register.
2699 s = new_stmt(BPF_LDX|BPF_MEM);
2700 s->s.k = reg_off_ll;
2704 static struct slist *
2705 gen_avs_llprefixlen(void)
2709 if (reg_off_ll == -1) {
2711 * We haven't yet assigned a register for the length
2712 * of the AVS header; allocate one.
2714 reg_off_ll = alloc_reg();
2718 * Load the register containing the AVS length
2719 * into the X register.
2721 s = new_stmt(BPF_LDX|BPF_MEM);
2722 s->s.k = reg_off_ll;
2726 static struct slist *
2727 gen_radiotap_llprefixlen(void)
2731 if (reg_off_ll == -1) {
2733 * We haven't yet assigned a register for the length
2734 * of the radiotap header; allocate one.
2736 reg_off_ll = alloc_reg();
2740 * Load the register containing the radiotap length
2741 * into the X register.
2743 s = new_stmt(BPF_LDX|BPF_MEM);
2744 s->s.k = reg_off_ll;
2749 * At the moment we treat PPI as normal Radiotap encoded
2750 * packets. The difference is in the function that generates
2751 * the code at the beginning to compute the header length.
2752 * Since this code generator of PPI supports bare 802.11
2753 * encapsulation only (i.e. the encapsulated DLT should be
2754 * DLT_IEEE802_11) we generate code to check for this too.
2756 static struct slist *
2757 gen_ppi_llprefixlen(void)
2761 if (reg_off_ll == -1) {
2763 * We haven't yet assigned a register for the length
2764 * of the radiotap header; allocate one.
2766 reg_off_ll = alloc_reg();
2770 * Load the register containing the PPI length
2771 * into the X register.
2773 s = new_stmt(BPF_LDX|BPF_MEM);
2774 s->s.k = reg_off_ll;
2779 * Generate code to compute the link-layer header length, if necessary,
2780 * putting it into the X register, and to return either a pointer to a
2781 * "struct slist" for the list of statements in that code, or NULL if
2782 * no code is necessary.
2784 static struct slist *
2785 gen_llprefixlen(void)
2789 case DLT_PRISM_HEADER:
2790 return gen_prism_llprefixlen();
2792 case DLT_IEEE802_11_RADIO_AVS:
2793 return gen_avs_llprefixlen();
2795 case DLT_IEEE802_11_RADIO:
2796 return gen_radiotap_llprefixlen();
2799 return gen_ppi_llprefixlen();
2807 * Generate code to load the register containing the offset of the
2808 * MAC-layer payload into the X register; if no register for that offset
2809 * has been allocated, allocate it first.
2811 static struct slist *
2816 if (off_macpl_is_variable) {
2817 if (reg_off_macpl == -1) {
2819 * We haven't yet assigned a register for the offset
2820 * of the MAC-layer payload; allocate one.
2822 reg_off_macpl = alloc_reg();
2826 * Load the register containing the offset of the MAC-layer
2827 * payload into the X register.
2829 s = new_stmt(BPF_LDX|BPF_MEM);
2830 s->s.k = reg_off_macpl;
2834 * That offset isn't variable, so we don't need to
2835 * generate any code.
2842 * Map an Ethernet type to the equivalent PPP type.
2845 ethertype_to_ppptype(proto)
2854 case ETHERTYPE_IPV6:
2862 case ETHERTYPE_ATALK:
2876 * I'm assuming the "Bridging PDU"s that go
2877 * over PPP are Spanning Tree Protocol
2891 * Generate code to match a particular packet type by matching the
2892 * link-layer type field or fields in the 802.2 LLC header.
2894 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2895 * value, if <= ETHERMTU.
2897 static struct block *
2901 struct block *b0, *b1, *b2;
2903 /* are we checking MPLS-encapsulated packets? */
2904 if (label_stack_depth > 0) {
2908 /* FIXME add other L3 proto IDs */
2909 return gen_mpls_linktype(Q_IP);
2911 case ETHERTYPE_IPV6:
2913 /* FIXME add other L3 proto IDs */
2914 return gen_mpls_linktype(Q_IPV6);
2917 bpf_error("unsupported protocol over mpls");
2923 * Are we testing PPPoE packets?
2927 * The PPPoE session header is part of the
2928 * MAC-layer payload, so all references
2929 * should be relative to the beginning of
2934 * We use Ethernet protocol types inside libpcap;
2935 * map them to the corresponding PPP protocol types.
2937 proto = ethertype_to_ppptype(proto);
2938 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2944 case DLT_NETANALYZER:
2945 case DLT_NETANALYZER_TRANSPARENT:
2946 return gen_ether_linktype(proto);
2954 proto = (proto << 8 | LLCSAP_ISONS);
2958 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2965 case DLT_IEEE802_11:
2966 case DLT_PRISM_HEADER:
2967 case DLT_IEEE802_11_RADIO_AVS:
2968 case DLT_IEEE802_11_RADIO:
2971 * Check that we have a data frame.
2973 b0 = gen_check_802_11_data_frame();
2976 * Now check for the specified link-layer type.
2978 b1 = gen_llc_linktype(proto);
2986 * XXX - check for asynchronous frames, as per RFC 1103.
2988 return gen_llc_linktype(proto);
2994 * XXX - check for LLC PDUs, as per IEEE 802.5.
2996 return gen_llc_linktype(proto);
3000 case DLT_ATM_RFC1483:
3002 case DLT_IP_OVER_FC:
3003 return gen_llc_linktype(proto);
3009 * If "is_lane" is set, check for a LANE-encapsulated
3010 * version of this protocol, otherwise check for an
3011 * LLC-encapsulated version of this protocol.
3013 * We assume LANE means Ethernet, not Token Ring.
3017 * Check that the packet doesn't begin with an
3018 * LE Control marker. (We've already generated
3021 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3026 * Now generate an Ethernet test.
3028 b1 = gen_ether_linktype(proto);
3033 * Check for LLC encapsulation and then check the
3036 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3037 b1 = gen_llc_linktype(proto);
3045 return gen_linux_sll_linktype(proto);
3050 case DLT_SLIP_BSDOS:
3053 * These types don't provide any type field; packets
3054 * are always IPv4 or IPv6.
3056 * XXX - for IPv4, check for a version number of 4, and,
3057 * for IPv6, check for a version number of 6?
3062 /* Check for a version number of 4. */
3063 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3065 case ETHERTYPE_IPV6:
3066 /* Check for a version number of 6. */
3067 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3070 return gen_false(); /* always false */
3077 * Raw IPv4, so no type field.
3079 if (proto == ETHERTYPE_IP)
3080 return gen_true(); /* always true */
3082 /* Checking for something other than IPv4; always false */
3089 * Raw IPv6, so no type field.
3091 if (proto == ETHERTYPE_IPV6)
3092 return gen_true(); /* always true */
3094 /* Checking for something other than IPv6; always false */
3101 case DLT_PPP_SERIAL:
3104 * We use Ethernet protocol types inside libpcap;
3105 * map them to the corresponding PPP protocol types.
3107 proto = ethertype_to_ppptype(proto);
3108 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3114 * We use Ethernet protocol types inside libpcap;
3115 * map them to the corresponding PPP protocol types.
3121 * Also check for Van Jacobson-compressed IP.
3122 * XXX - do this for other forms of PPP?
3124 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3125 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3127 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3132 proto = ethertype_to_ppptype(proto);
3133 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3143 * For DLT_NULL, the link-layer header is a 32-bit
3144 * word containing an AF_ value in *host* byte order,
3145 * and for DLT_ENC, the link-layer header begins
3146 * with a 32-bit work containing an AF_ value in
3149 * In addition, if we're reading a saved capture file,
3150 * the host byte order in the capture may not be the
3151 * same as the host byte order on this machine.
3153 * For DLT_LOOP, the link-layer header is a 32-bit
3154 * word containing an AF_ value in *network* byte order.
3156 * XXX - AF_ values may, unfortunately, be platform-
3157 * dependent; for example, FreeBSD's AF_INET6 is 24
3158 * whilst NetBSD's and OpenBSD's is 26.
3160 * This means that, when reading a capture file, just
3161 * checking for our AF_INET6 value won't work if the
3162 * capture file came from another OS.
3171 case ETHERTYPE_IPV6:
3178 * Not a type on which we support filtering.
3179 * XXX - support those that have AF_ values
3180 * #defined on this platform, at least?
3185 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3187 * The AF_ value is in host byte order, but
3188 * the BPF interpreter will convert it to
3189 * network byte order.
3191 * If this is a save file, and it's from a
3192 * machine with the opposite byte order to
3193 * ours, we byte-swap the AF_ value.
3195 * Then we run it through "htonl()", and
3196 * generate code to compare against the result.
3198 if (bpf_pcap->sf.rfile != NULL &&
3199 bpf_pcap->sf.swapped)
3200 proto = SWAPLONG(proto);
3201 proto = htonl(proto);
3203 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3205 #ifdef HAVE_NET_PFVAR_H
3208 * af field is host byte order in contrast to the rest of
3211 if (proto == ETHERTYPE_IP)
3212 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3213 BPF_B, (bpf_int32)AF_INET));
3214 else if (proto == ETHERTYPE_IPV6)
3215 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3216 BPF_B, (bpf_int32)AF_INET6));
3221 #endif /* HAVE_NET_PFVAR_H */
3224 case DLT_ARCNET_LINUX:
3226 * XXX should we check for first fragment if the protocol
3234 case ETHERTYPE_IPV6:
3235 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3236 (bpf_int32)ARCTYPE_INET6));
3239 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3240 (bpf_int32)ARCTYPE_IP);
3241 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3242 (bpf_int32)ARCTYPE_IP_OLD);
3247 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3248 (bpf_int32)ARCTYPE_ARP);
3249 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3250 (bpf_int32)ARCTYPE_ARP_OLD);
3254 case ETHERTYPE_REVARP:
3255 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3256 (bpf_int32)ARCTYPE_REVARP));
3258 case ETHERTYPE_ATALK:
3259 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3260 (bpf_int32)ARCTYPE_ATALK));
3267 case ETHERTYPE_ATALK:
3277 * XXX - assumes a 2-byte Frame Relay header with
3278 * DLCI and flags. What if the address is longer?
3284 * Check for the special NLPID for IP.
3286 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3288 case ETHERTYPE_IPV6:
3290 * Check for the special NLPID for IPv6.
3292 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3296 * Check for several OSI protocols.
3298 * Frame Relay packets typically have an OSI
3299 * NLPID at the beginning; we check for each
3302 * What we check for is the NLPID and a frame
3303 * control field of UI, i.e. 0x03 followed
3306 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3307 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3308 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3320 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3322 case DLT_JUNIPER_MFR:
3323 case DLT_JUNIPER_MLFR:
3324 case DLT_JUNIPER_MLPPP:
3325 case DLT_JUNIPER_ATM1:
3326 case DLT_JUNIPER_ATM2:
3327 case DLT_JUNIPER_PPPOE:
3328 case DLT_JUNIPER_PPPOE_ATM:
3329 case DLT_JUNIPER_GGSN:
3330 case DLT_JUNIPER_ES:
3331 case DLT_JUNIPER_MONITOR:
3332 case DLT_JUNIPER_SERVICES:
3333 case DLT_JUNIPER_ETHER:
3334 case DLT_JUNIPER_PPP:
3335 case DLT_JUNIPER_FRELAY:
3336 case DLT_JUNIPER_CHDLC:
3337 case DLT_JUNIPER_VP:
3338 case DLT_JUNIPER_ST:
3339 case DLT_JUNIPER_ISM:
3340 case DLT_JUNIPER_VS:
3341 case DLT_JUNIPER_SRX_E2E:
3342 case DLT_JUNIPER_FIBRECHANNEL:
3343 case DLT_JUNIPER_ATM_CEMIC:
3345 /* just lets verify the magic number for now -
3346 * on ATM we may have up to 6 different encapsulations on the wire
3347 * and need a lot of heuristics to figure out that the payload
3350 * FIXME encapsulation specific BPF_ filters
3352 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3355 return gen_ipnet_linktype(proto);
3357 case DLT_LINUX_IRDA:
3358 bpf_error("IrDA link-layer type filtering not implemented");
3361 bpf_error("DOCSIS link-layer type filtering not implemented");
3364 case DLT_MTP2_WITH_PHDR:
3365 bpf_error("MTP2 link-layer type filtering not implemented");
3368 bpf_error("ERF link-layer type filtering not implemented");
3371 bpf_error("PFSYNC link-layer type filtering not implemented");
3373 case DLT_LINUX_LAPD:
3374 bpf_error("LAPD link-layer type filtering not implemented");
3378 case DLT_USB_LINUX_MMAPPED:
3379 bpf_error("USB link-layer type filtering not implemented");
3381 case DLT_BLUETOOTH_HCI_H4:
3382 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3383 bpf_error("Bluetooth link-layer type filtering not implemented");
3386 case DLT_CAN_SOCKETCAN:
3387 bpf_error("CAN link-layer type filtering not implemented");
3389 case DLT_IEEE802_15_4:
3390 case DLT_IEEE802_15_4_LINUX:
3391 case DLT_IEEE802_15_4_NONASK_PHY:
3392 case DLT_IEEE802_15_4_NOFCS:
3393 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3395 case DLT_IEEE802_16_MAC_CPS_RADIO:
3396 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3399 bpf_error("SITA link-layer type filtering not implemented");
3402 bpf_error("RAIF1 link-layer type filtering not implemented");
3405 bpf_error("IPMB link-layer type filtering not implemented");
3408 bpf_error("AX.25 link-layer type filtering not implemented");
3412 * All the types that have no encapsulation should either be
3413 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3414 * all packets are IP packets, or should be handled in some
3415 * special case, if none of them are (if some are and some
3416 * aren't, the lack of encapsulation is a problem, as we'd
3417 * have to find some other way of determining the packet type).
3419 * Therefore, if "off_linktype" is -1, there's an error.
3421 if (off_linktype == (u_int)-1)
3425 * Any type not handled above should always have an Ethernet
3426 * type at an offset of "off_linktype".
3428 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3432 * Check for an LLC SNAP packet with a given organization code and
3433 * protocol type; we check the entire contents of the 802.2 LLC and
3434 * snap headers, checking for DSAP and SSAP of SNAP and a control
3435 * field of 0x03 in the LLC header, and for the specified organization
3436 * code and protocol type in the SNAP header.
3438 static struct block *
3439 gen_snap(orgcode, ptype)
3440 bpf_u_int32 orgcode;
3443 u_char snapblock[8];
3445 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3446 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3447 snapblock[2] = 0x03; /* control = UI */
3448 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3449 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3450 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3451 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3452 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3453 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3457 * Generate code to match a particular packet type, for link-layer types
3458 * using 802.2 LLC headers.
3460 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3461 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3463 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3464 * value, if <= ETHERMTU. We use that to determine whether to
3465 * match the DSAP or both DSAP and LSAP or to check the OUI and
3466 * protocol ID in a SNAP header.
3468 static struct block *
3469 gen_llc_linktype(proto)
3473 * XXX - handle token-ring variable-length header.
3479 case LLCSAP_NETBEUI:
3481 * XXX - should we check both the DSAP and the
3482 * SSAP, like this, or should we check just the
3483 * DSAP, as we do for other types <= ETHERMTU
3484 * (i.e., other SAP values)?
3486 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3487 ((proto << 8) | proto));
3491 * XXX - are there ever SNAP frames for IPX on
3492 * non-Ethernet 802.x networks?
3494 return gen_cmp(OR_MACPL, 0, BPF_B,
3495 (bpf_int32)LLCSAP_IPX);
3497 case ETHERTYPE_ATALK:
3499 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3500 * SNAP packets with an organization code of
3501 * 0x080007 (Apple, for Appletalk) and a protocol
3502 * type of ETHERTYPE_ATALK (Appletalk).
3504 * XXX - check for an organization code of
3505 * encapsulated Ethernet as well?
3507 return gen_snap(0x080007, ETHERTYPE_ATALK);
3511 * XXX - we don't have to check for IPX 802.3
3512 * here, but should we check for the IPX Ethertype?
3514 if (proto <= ETHERMTU) {
3516 * This is an LLC SAP value, so check
3519 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3522 * This is an Ethernet type; we assume that it's
3523 * unlikely that it'll appear in the right place
3524 * at random, and therefore check only the
3525 * location that would hold the Ethernet type
3526 * in a SNAP frame with an organization code of
3527 * 0x000000 (encapsulated Ethernet).
3529 * XXX - if we were to check for the SNAP DSAP and
3530 * LSAP, as per XXX, and were also to check for an
3531 * organization code of 0x000000 (encapsulated
3532 * Ethernet), we'd do
3534 * return gen_snap(0x000000, proto);
3536 * here; for now, we don't, as per the above.
3537 * I don't know whether it's worth the extra CPU
3538 * time to do the right check or not.
3540 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3545 static struct block *
3546 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3550 u_int src_off, dst_off;
3552 struct block *b0, *b1;
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);
3573 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3574 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3581 b0 = gen_linktype(proto);
3582 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3588 static struct block *
3589 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3590 struct in6_addr *addr;
3591 struct in6_addr *mask;
3593 u_int src_off, dst_off;
3595 struct block *b0, *b1;
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);
3617 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3618 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3625 /* this order is important */
3626 a = (u_int32_t *)addr;
3627 m = (u_int32_t *)mask;
3628 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3629 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3631 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3633 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3635 b0 = gen_linktype(proto);
3641 static struct block *
3642 gen_ehostop(eaddr, dir)
3643 register const u_char *eaddr;
3646 register struct block *b0, *b1;
3650 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3653 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3656 b0 = gen_ehostop(eaddr, Q_SRC);
3657 b1 = gen_ehostop(eaddr, Q_DST);
3663 b0 = gen_ehostop(eaddr, Q_SRC);
3664 b1 = gen_ehostop(eaddr, Q_DST);
3669 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3673 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3677 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3681 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3685 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3689 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3697 * Like gen_ehostop, but for DLT_FDDI
3699 static struct block *
3700 gen_fhostop(eaddr, dir)
3701 register const u_char *eaddr;
3704 struct block *b0, *b1;
3709 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3711 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3716 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3718 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3722 b0 = gen_fhostop(eaddr, Q_SRC);
3723 b1 = gen_fhostop(eaddr, Q_DST);
3729 b0 = gen_fhostop(eaddr, Q_SRC);
3730 b1 = gen_fhostop(eaddr, Q_DST);
3735 bpf_error("'addr1' is only supported on 802.11");
3739 bpf_error("'addr2' is only supported on 802.11");
3743 bpf_error("'addr3' is only supported on 802.11");
3747 bpf_error("'addr4' is only supported on 802.11");
3751 bpf_error("'ra' is only supported on 802.11");
3755 bpf_error("'ta' is only supported on 802.11");
3763 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3765 static struct block *
3766 gen_thostop(eaddr, dir)
3767 register const u_char *eaddr;
3770 register struct block *b0, *b1;
3774 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3777 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3780 b0 = gen_thostop(eaddr, Q_SRC);
3781 b1 = gen_thostop(eaddr, Q_DST);
3787 b0 = gen_thostop(eaddr, Q_SRC);
3788 b1 = gen_thostop(eaddr, Q_DST);
3793 bpf_error("'addr1' is only supported on 802.11");
3797 bpf_error("'addr2' is only supported on 802.11");
3801 bpf_error("'addr3' is only supported on 802.11");
3805 bpf_error("'addr4' is only supported on 802.11");
3809 bpf_error("'ra' is only supported on 802.11");
3813 bpf_error("'ta' is only supported on 802.11");
3821 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3822 * various 802.11 + radio headers.
3824 static struct block *
3825 gen_wlanhostop(eaddr, dir)
3826 register const u_char *eaddr;
3829 register struct block *b0, *b1, *b2;
3830 register struct slist *s;
3832 #ifdef ENABLE_WLAN_FILTERING_PATCH
3835 * We need to disable the optimizer because the optimizer is buggy
3836 * and wipes out some LD instructions generated by the below
3837 * code to validate the Frame Control bits
3840 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3847 * For control frames, there is no SA.
3849 * For management frames, SA is at an
3850 * offset of 10 from the beginning of
3853 * For data frames, SA is at an offset
3854 * of 10 from the beginning of the packet
3855 * if From DS is clear, at an offset of
3856 * 16 from the beginning of the packet
3857 * if From DS is set and To DS is clear,
3858 * and an offset of 24 from the beginning
3859 * of the packet if From DS is set and To DS
3864 * Generate the tests to be done for data frames
3867 * First, check for To DS set, i.e. check "link[1] & 0x01".
3869 s = gen_load_a(OR_LINK, 1, BPF_B);
3870 b1 = new_block(JMP(BPF_JSET));
3871 b1->s.k = 0x01; /* To DS */
3875 * If To DS is set, the SA is at 24.
3877 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3881 * Now, check for To DS not set, i.e. check
3882 * "!(link[1] & 0x01)".
3884 s = gen_load_a(OR_LINK, 1, BPF_B);
3885 b2 = new_block(JMP(BPF_JSET));
3886 b2->s.k = 0x01; /* To DS */
3891 * If To DS is not set, the SA is at 16.
3893 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3897 * Now OR together the last two checks. That gives
3898 * the complete set of checks for data frames with
3904 * Now check for From DS being set, and AND that with
3905 * the ORed-together checks.
3907 s = gen_load_a(OR_LINK, 1, BPF_B);
3908 b1 = new_block(JMP(BPF_JSET));
3909 b1->s.k = 0x02; /* From DS */
3914 * Now check for data frames with From DS not set.
3916 s = gen_load_a(OR_LINK, 1, BPF_B);
3917 b2 = new_block(JMP(BPF_JSET));
3918 b2->s.k = 0x02; /* From DS */
3923 * If From DS isn't set, the SA is at 10.
3925 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3929 * Now OR together the checks for data frames with
3930 * From DS not set and for data frames with From DS
3931 * set; that gives the checks done for data frames.
3936 * Now check for a data frame.
3937 * I.e, check "link[0] & 0x08".
3939 s = gen_load_a(OR_LINK, 0, BPF_B);
3940 b1 = new_block(JMP(BPF_JSET));
3945 * AND that with the checks done for data frames.
3950 * If the high-order bit of the type value is 0, this
3951 * is a management frame.
3952 * I.e, check "!(link[0] & 0x08)".
3954 s = gen_load_a(OR_LINK, 0, BPF_B);
3955 b2 = new_block(JMP(BPF_JSET));
3961 * For management frames, the SA is at 10.
3963 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3967 * OR that with the checks done for data frames.
3968 * That gives the checks done for management and
3974 * If the low-order bit of the type value is 1,
3975 * this is either a control frame or a frame
3976 * with a reserved type, and thus not a
3979 * I.e., check "!(link[0] & 0x04)".
3981 s = gen_load_a(OR_LINK, 0, BPF_B);
3982 b1 = new_block(JMP(BPF_JSET));
3988 * AND that with the checks for data and management
3998 * For control frames, there is no DA.
4000 * For management frames, DA is at an
4001 * offset of 4 from the beginning of
4004 * For data frames, DA is at an offset
4005 * of 4 from the beginning of the packet
4006 * if To DS is clear and at an offset of
4007 * 16 from the beginning of the packet
4012 * Generate the tests to be done for data frames.
4014 * First, check for To DS set, i.e. "link[1] & 0x01".
4016 s = gen_load_a(OR_LINK, 1, BPF_B);
4017 b1 = new_block(JMP(BPF_JSET));
4018 b1->s.k = 0x01; /* To DS */
4022 * If To DS is set, the DA is at 16.
4024 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4028 * Now, check for To DS not set, i.e. check
4029 * "!(link[1] & 0x01)".
4031 s = gen_load_a(OR_LINK, 1, BPF_B);
4032 b2 = new_block(JMP(BPF_JSET));
4033 b2->s.k = 0x01; /* To DS */
4038 * If To DS is not set, the DA is at 4.
4040 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4044 * Now OR together the last two checks. That gives
4045 * the complete set of checks for data frames.
4050 * Now check for a data frame.
4051 * I.e, check "link[0] & 0x08".
4053 s = gen_load_a(OR_LINK, 0, BPF_B);
4054 b1 = new_block(JMP(BPF_JSET));
4059 * AND that with the checks done for data frames.
4064 * If the high-order bit of the type value is 0, this
4065 * is a management frame.
4066 * I.e, check "!(link[0] & 0x08)".
4068 s = gen_load_a(OR_LINK, 0, BPF_B);
4069 b2 = new_block(JMP(BPF_JSET));
4075 * For management frames, the DA is at 4.
4077 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4081 * OR that with the checks done for data frames.
4082 * That gives the checks done for management and
4088 * If the low-order bit of the type value is 1,
4089 * this is either a control frame or a frame
4090 * with a reserved type, and thus not a
4093 * I.e., check "!(link[0] & 0x04)".
4095 s = gen_load_a(OR_LINK, 0, BPF_B);
4096 b1 = new_block(JMP(BPF_JSET));
4102 * AND that with the checks for data and management
4110 * Not present in management frames; addr1 in other
4115 * If the high-order bit of the type value is 0, this
4116 * is a management frame.
4117 * I.e, check "(link[0] & 0x08)".
4119 s = gen_load_a(OR_LINK, 0, BPF_B);
4120 b1 = new_block(JMP(BPF_JSET));
4127 b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4130 * AND that with the check of addr1.
4137 * Not present in management frames; addr2, if present,
4142 * Not present in CTS or ACK control frames.
4144 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4145 IEEE80211_FC0_TYPE_MASK);
4147 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4148 IEEE80211_FC0_SUBTYPE_MASK);
4150 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4151 IEEE80211_FC0_SUBTYPE_MASK);
4157 * If the high-order bit of the type value is 0, this
4158 * is a management frame.
4159 * I.e, check "(link[0] & 0x08)".
4161 s = gen_load_a(OR_LINK, 0, BPF_B);
4162 b1 = new_block(JMP(BPF_JSET));
4167 * AND that with the check for frames other than
4168 * CTS and ACK frames.
4175 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4180 * XXX - add BSSID keyword?
4183 return (gen_bcmp(OR_LINK, 4, 6, eaddr));
4187 * Not present in CTS or ACK control frames.
4189 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4190 IEEE80211_FC0_TYPE_MASK);
4192 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4193 IEEE80211_FC0_SUBTYPE_MASK);
4195 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4196 IEEE80211_FC0_SUBTYPE_MASK);
4200 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4206 * Not present in control frames.
4208 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4209 IEEE80211_FC0_TYPE_MASK);
4211 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4217 * Present only if the direction mask has both "From DS"
4218 * and "To DS" set. Neither control frames nor management
4219 * frames should have both of those set, so we don't
4220 * check the frame type.
4222 b0 = gen_mcmp(OR_LINK, 1, BPF_B,
4223 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4224 b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
4229 b0 = gen_wlanhostop(eaddr, Q_SRC);
4230 b1 = gen_wlanhostop(eaddr, Q_DST);
4236 b0 = gen_wlanhostop(eaddr, Q_SRC);
4237 b1 = gen_wlanhostop(eaddr, Q_DST);
4246 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4247 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4248 * as the RFC states.)
4250 static struct block *
4251 gen_ipfchostop(eaddr, dir)
4252 register const u_char *eaddr;
4255 register struct block *b0, *b1;
4259 return gen_bcmp(OR_LINK, 10, 6, eaddr);
4262 return gen_bcmp(OR_LINK, 2, 6, eaddr);
4265 b0 = gen_ipfchostop(eaddr, Q_SRC);
4266 b1 = gen_ipfchostop(eaddr, Q_DST);
4272 b0 = gen_ipfchostop(eaddr, Q_SRC);
4273 b1 = gen_ipfchostop(eaddr, Q_DST);
4278 bpf_error("'addr1' is only supported on 802.11");
4282 bpf_error("'addr2' is only supported on 802.11");
4286 bpf_error("'addr3' is only supported on 802.11");
4290 bpf_error("'addr4' is only supported on 802.11");
4294 bpf_error("'ra' is only supported on 802.11");
4298 bpf_error("'ta' is only supported on 802.11");
4306 * This is quite tricky because there may be pad bytes in front of the
4307 * DECNET header, and then there are two possible data packet formats that
4308 * carry both src and dst addresses, plus 5 packet types in a format that
4309 * carries only the src node, plus 2 types that use a different format and
4310 * also carry just the src node.
4314 * Instead of doing those all right, we just look for data packets with
4315 * 0 or 1 bytes of padding. If you want to look at other packets, that
4316 * will require a lot more hacking.
4318 * To add support for filtering on DECNET "areas" (network numbers)
4319 * one would want to add a "mask" argument to this routine. That would
4320 * make the filter even more inefficient, although one could be clever
4321 * and not generate masking instructions if the mask is 0xFFFF.
4323 static struct block *
4324 gen_dnhostop(addr, dir)
4328 struct block *b0, *b1, *b2, *tmp;
4329 u_int offset_lh; /* offset if long header is received */
4330 u_int offset_sh; /* offset if short header is received */
4335 offset_sh = 1; /* follows flags */
4336 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4340 offset_sh = 3; /* follows flags, dstnode */
4341 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4345 /* Inefficient because we do our Calvinball dance twice */
4346 b0 = gen_dnhostop(addr, Q_SRC);
4347 b1 = gen_dnhostop(addr, Q_DST);
4353 /* Inefficient because we do our Calvinball dance twice */
4354 b0 = gen_dnhostop(addr, Q_SRC);
4355 b1 = gen_dnhostop(addr, Q_DST);
4360 bpf_error("ISO host filtering not implemented");
4365 b0 = gen_linktype(ETHERTYPE_DN);
4366 /* Check for pad = 1, long header case */
4367 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4368 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
4369 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
4370 BPF_H, (bpf_int32)ntohs((u_short)addr));
4372 /* Check for pad = 0, long header case */
4373 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4374 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4377 /* Check for pad = 1, short header case */
4378 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4379 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
4380 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4383 /* Check for pad = 0, short header case */
4384 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4385 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4389 /* Combine with test for linktype */
4395 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4396 * test the bottom-of-stack bit, and then check the version number
4397 * field in the IP header.
4399 static struct block *
4400 gen_mpls_linktype(proto)
4403 struct block *b0, *b1;
4408 /* match the bottom-of-stack bit */
4409 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4410 /* match the IPv4 version number */
4411 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
4416 /* match the bottom-of-stack bit */
4417 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4418 /* match the IPv4 version number */
4419 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
4428 static struct block *
4429 gen_host(addr, mask, proto, dir, type)
4436 struct block *b0, *b1;
4437 const char *typestr;
4447 b0 = gen_host(addr, mask, Q_IP, dir, type);
4449 * Only check for non-IPv4 addresses if we're not
4450 * checking MPLS-encapsulated packets.
4452 if (label_stack_depth == 0) {
4453 b1 = gen_host(addr, mask, Q_ARP, dir, type);
4455 b0 = gen_host(addr, mask, Q_RARP, dir, type);
4461 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
4464 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4467 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4470 bpf_error("'tcp' modifier applied to %s", typestr);
4473 bpf_error("'sctp' modifier applied to %s", typestr);
4476 bpf_error("'udp' modifier applied to %s", typestr);
4479 bpf_error("'icmp' modifier applied to %s", typestr);
4482 bpf_error("'igmp' modifier applied to %s", typestr);
4485 bpf_error("'igrp' modifier applied to %s", typestr);
4488 bpf_error("'pim' modifier applied to %s", typestr);
4491 bpf_error("'vrrp' modifier applied to %s", typestr);
4494 bpf_error("'carp' modifier applied to %s", typestr);
4497 bpf_error("ATALK host filtering not implemented");
4500 bpf_error("AARP host filtering not implemented");
4503 return gen_dnhostop(addr, dir);
4506 bpf_error("SCA host filtering not implemented");
4509 bpf_error("LAT host filtering not implemented");
4512 bpf_error("MOPDL host filtering not implemented");
4515 bpf_error("MOPRC host filtering not implemented");
4518 bpf_error("'ip6' modifier applied to ip host");
4521 bpf_error("'icmp6' modifier applied to %s", typestr);
4524 bpf_error("'ah' modifier applied to %s", typestr);
4527 bpf_error("'esp' modifier applied to %s", typestr);
4530 bpf_error("ISO host filtering not implemented");
4533 bpf_error("'esis' modifier applied to %s", typestr);
4536 bpf_error("'isis' modifier applied to %s", typestr);
4539 bpf_error("'clnp' modifier applied to %s", typestr);
4542 bpf_error("'stp' modifier applied to %s", typestr);
4545 bpf_error("IPX host filtering not implemented");
4548 bpf_error("'netbeui' modifier applied to %s", typestr);
4551 bpf_error("'radio' modifier applied to %s", typestr);
4560 static struct block *
4561 gen_host6(addr, mask, proto, dir, type)
4562 struct in6_addr *addr;
4563 struct in6_addr *mask;
4568 const char *typestr;
4578 return gen_host6(addr, mask, Q_IPV6, dir, type);
4581 bpf_error("'ip' modifier applied to ip6 %s", typestr);
4584 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
4587 bpf_error("'arp' modifier applied to ip6 %s", typestr);
4590 bpf_error("'sctp' modifier applied to %s", typestr);
4593 bpf_error("'tcp' modifier applied to %s", typestr);
4596 bpf_error("'udp' modifier applied to %s", typestr);
4599 bpf_error("'icmp' modifier applied to %s", typestr);
4602 bpf_error("'igmp' modifier applied to %s", typestr);
4605 bpf_error("'igrp' modifier applied to %s", typestr);
4608 bpf_error("'pim' modifier applied to %s", typestr);
4611 bpf_error("'vrrp' modifier applied to %s", typestr);
4614 bpf_error("'carp' modifier applied to %s", typestr);
4617 bpf_error("ATALK host filtering not implemented");
4620 bpf_error("AARP host filtering not implemented");
4623 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
4626 bpf_error("SCA host filtering not implemented");
4629 bpf_error("LAT host filtering not implemented");
4632 bpf_error("MOPDL host filtering not implemented");
4635 bpf_error("MOPRC host filtering not implemented");
4638 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4641 bpf_error("'icmp6' modifier applied to %s", typestr);
4644 bpf_error("'ah' modifier applied to %s", typestr);
4647 bpf_error("'esp' modifier applied to %s", typestr);
4650 bpf_error("ISO host filtering not implemented");
4653 bpf_error("'esis' modifier applied to %s", typestr);
4656 bpf_error("'isis' modifier applied to %s", typestr);
4659 bpf_error("'clnp' modifier applied to %s", typestr);
4662 bpf_error("'stp' modifier applied to %s", typestr);
4665 bpf_error("IPX host filtering not implemented");
4668 bpf_error("'netbeui' modifier applied to %s", typestr);
4671 bpf_error("'radio' modifier applied to %s", typestr);
4681 static struct block *
4682 gen_gateway(eaddr, alist, proto, dir)
4683 const u_char *eaddr;
4684 bpf_u_int32 **alist;
4688 struct block *b0, *b1, *tmp;
4691 bpf_error("direction applied to 'gateway'");
4700 case DLT_NETANALYZER:
4701 case DLT_NETANALYZER_TRANSPARENT:
4702 b0 = gen_ehostop(eaddr, Q_OR);
4705 b0 = gen_fhostop(eaddr, Q_OR);
4708 b0 = gen_thostop(eaddr, Q_OR);
4710 case DLT_IEEE802_11:
4711 case DLT_PRISM_HEADER:
4712 case DLT_IEEE802_11_RADIO_AVS:
4713 case DLT_IEEE802_11_RADIO:
4715 b0 = gen_wlanhostop(eaddr, Q_OR);
4720 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4722 * Check that the packet doesn't begin with an
4723 * LE Control marker. (We've already generated
4726 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4731 * Now check the MAC address.
4733 b0 = gen_ehostop(eaddr, Q_OR);
4736 case DLT_IP_OVER_FC:
4737 b0 = gen_ipfchostop(eaddr, Q_OR);
4741 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4743 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
4745 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
4754 bpf_error("illegal modifier of 'gateway'");
4760 gen_proto_abbrev(proto)
4769 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
4770 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
4775 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
4776 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
4781 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
4782 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
4787 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
4790 #ifndef IPPROTO_IGMP
4791 #define IPPROTO_IGMP 2
4795 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
4798 #ifndef IPPROTO_IGRP
4799 #define IPPROTO_IGRP 9
4802 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
4806 #define IPPROTO_PIM 103
4810 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
4811 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
4815 #ifndef IPPROTO_VRRP
4816 #define IPPROTO_VRRP 112
4820 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
4823 #ifndef IPPROTO_CARP
4824 #define IPPROTO_CARP 112
4828 b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT);
4832 b1 = gen_linktype(ETHERTYPE_IP);
4836 b1 = gen_linktype(ETHERTYPE_ARP);
4840 b1 = gen_linktype(ETHERTYPE_REVARP);
4844 bpf_error("link layer applied in wrong context");
4847 b1 = gen_linktype(ETHERTYPE_ATALK);
4851 b1 = gen_linktype(ETHERTYPE_AARP);
4855 b1 = gen_linktype(ETHERTYPE_DN);
4859 b1 = gen_linktype(ETHERTYPE_SCA);
4863 b1 = gen_linktype(ETHERTYPE_LAT);
4867 b1 = gen_linktype(ETHERTYPE_MOPDL);
4871 b1 = gen_linktype(ETHERTYPE_MOPRC);
4875 b1 = gen_linktype(ETHERTYPE_IPV6);
4878 #ifndef IPPROTO_ICMPV6
4879 #define IPPROTO_ICMPV6 58
4882 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
4886 #define IPPROTO_AH 51
4889 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
4890 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
4895 #define IPPROTO_ESP 50
4898 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
4899 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
4904 b1 = gen_linktype(LLCSAP_ISONS);
4908 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
4912 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4915 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
4916 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4917 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4919 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4921 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4923 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4927 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
4928 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4929 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4931 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4933 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4935 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4939 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
4940 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4941 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4943 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
4948 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4949 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4954 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4955 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4957 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4959 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4964 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4965 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4970 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4971 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4976 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
4980 b1 = gen_linktype(LLCSAP_8021D);
4984 b1 = gen_linktype(LLCSAP_IPX);
4988 b1 = gen_linktype(LLCSAP_NETBEUI);
4992 bpf_error("'radio' is not a valid protocol type");
5000 static struct block *
5006 /* not IPv4 frag other than the first frag */
5007 s = gen_load_a(OR_NET, 6, BPF_H);
5008 b = new_block(JMP(BPF_JSET));
5017 * Generate a comparison to a port value in the transport-layer header
5018 * at the specified offset from the beginning of that header.
5020 * XXX - this handles a variable-length prefix preceding the link-layer
5021 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5022 * variable-length link-layer headers (such as Token Ring or 802.11
5025 static struct block *
5026 gen_portatom(off, v)
5030 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
5033 static struct block *
5034 gen_portatom6(off, v)
5038 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
5042 gen_portop(port, proto, dir)
5043 int port, proto, dir;
5045 struct block *b0, *b1, *tmp;
5047 /* ip proto 'proto' and not a fragment other than the first fragment */
5048 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5054 b1 = gen_portatom(0, (bpf_int32)port);
5058 b1 = gen_portatom(2, (bpf_int32)port);
5063 tmp = gen_portatom(0, (bpf_int32)port);
5064 b1 = gen_portatom(2, (bpf_int32)port);
5069 tmp = gen_portatom(0, (bpf_int32)port);
5070 b1 = gen_portatom(2, (bpf_int32)port);
5082 static struct block *
5083 gen_port(port, ip_proto, dir)
5088 struct block *b0, *b1, *tmp;
5093 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5094 * not LLC encapsulation with LLCSAP_IP.
5096 * For IEEE 802 networks - which includes 802.5 token ring
5097 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5098 * says that SNAP encapsulation is used, not LLC encapsulation
5101 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5102 * RFC 2225 say that SNAP encapsulation is used, not LLC
5103 * encapsulation with LLCSAP_IP.
5105 * So we always check for ETHERTYPE_IP.
5107 b0 = gen_linktype(ETHERTYPE_IP);
5113 b1 = gen_portop(port, ip_proto, dir);
5117 tmp = gen_portop(port, IPPROTO_TCP, dir);
5118 b1 = gen_portop(port, IPPROTO_UDP, dir);
5120 tmp = gen_portop(port, IPPROTO_SCTP, dir);
5132 gen_portop6(port, proto, dir)
5133 int port, proto, dir;
5135 struct block *b0, *b1, *tmp;
5137 /* ip6 proto 'proto' */
5138 /* XXX - catch the first fragment of a fragmented packet? */
5139 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5143 b1 = gen_portatom6(0, (bpf_int32)port);
5147 b1 = gen_portatom6(2, (bpf_int32)port);
5152 tmp = gen_portatom6(0, (bpf_int32)port);
5153 b1 = gen_portatom6(2, (bpf_int32)port);
5158 tmp = gen_portatom6(0, (bpf_int32)port);
5159 b1 = gen_portatom6(2, (bpf_int32)port);
5171 static struct block *
5172 gen_port6(port, ip_proto, dir)
5177 struct block *b0, *b1, *tmp;
5179 /* link proto ip6 */
5180 b0 = gen_linktype(ETHERTYPE_IPV6);
5186 b1 = gen_portop6(port, ip_proto, dir);
5190 tmp = gen_portop6(port, IPPROTO_TCP, dir);
5191 b1 = gen_portop6(port, IPPROTO_UDP, dir);
5193 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
5204 /* gen_portrange code */
5205 static struct block *
5206 gen_portrangeatom(off, v1, v2)
5210 struct block *b1, *b2;
5214 * Reverse the order of the ports, so v1 is the lower one.
5223 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
5224 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
5232 gen_portrangeop(port1, port2, proto, dir)
5237 struct block *b0, *b1, *tmp;
5239 /* ip proto 'proto' and not a fragment other than the first fragment */
5240 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5246 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5250 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5255 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5256 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5261 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5262 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5274 static struct block *
5275 gen_portrange(port1, port2, ip_proto, dir)
5280 struct block *b0, *b1, *tmp;
5283 b0 = gen_linktype(ETHERTYPE_IP);
5289 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
5293 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
5294 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
5296 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
5307 static struct block *
5308 gen_portrangeatom6(off, v1, v2)
5312 struct block *b1, *b2;
5316 * Reverse the order of the ports, so v1 is the lower one.
5325 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
5326 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
5334 gen_portrangeop6(port1, port2, proto, dir)
5339 struct block *b0, *b1, *tmp;
5341 /* ip6 proto 'proto' */
5342 /* XXX - catch the first fragment of a fragmented packet? */
5343 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5347 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5351 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5356 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5357 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5362 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5363 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5375 static struct block *
5376 gen_portrange6(port1, port2, ip_proto, dir)
5381 struct block *b0, *b1, *tmp;
5383 /* link proto ip6 */
5384 b0 = gen_linktype(ETHERTYPE_IPV6);
5390 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
5394 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
5395 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
5397 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
5409 lookup_proto(name, proto)
5410 register const char *name;
5420 v = pcap_nametoproto(name);
5421 if (v == PROTO_UNDEF)
5422 bpf_error("unknown ip proto '%s'", name);
5426 /* XXX should look up h/w protocol type based on linktype */
5427 v = pcap_nametoeproto(name);
5428 if (v == PROTO_UNDEF) {
5429 v = pcap_nametollc(name);
5430 if (v == PROTO_UNDEF)
5431 bpf_error("unknown ether proto '%s'", name);
5436 if (strcmp(name, "esis") == 0)
5438 else if (strcmp(name, "isis") == 0)
5440 else if (strcmp(name, "clnp") == 0)
5443 bpf_error("unknown osi proto '%s'", name);
5463 static struct block *
5464 gen_protochain(v, proto, dir)
5469 #ifdef NO_PROTOCHAIN
5470 return gen_proto(v, proto, dir);
5472 struct block *b0, *b;
5473 struct slist *s[100];
5474 int fix2, fix3, fix4, fix5;
5475 int ahcheck, again, end;
5477 int reg2 = alloc_reg();
5479 memset(s, 0, sizeof(s));
5480 fix2 = fix3 = fix4 = fix5 = 0;
5487 b0 = gen_protochain(v, Q_IP, dir);
5488 b = gen_protochain(v, Q_IPV6, dir);
5492 bpf_error("bad protocol applied for 'protochain'");
5497 * We don't handle variable-length prefixes before the link-layer
5498 * header, or variable-length link-layer headers, here yet.
5499 * We might want to add BPF instructions to do the protochain
5500 * work, to simplify that and, on platforms that have a BPF
5501 * interpreter with the new instructions, let the filtering
5502 * be done in the kernel. (We already require a modified BPF
5503 * engine to do the protochain stuff, to support backward
5504 * branches, and backward branch support is unlikely to appear
5505 * in kernel BPF engines.)
5509 case DLT_IEEE802_11:
5510 case DLT_PRISM_HEADER:
5511 case DLT_IEEE802_11_RADIO_AVS:
5512 case DLT_IEEE802_11_RADIO:
5514 bpf_error("'protochain' not supported with 802.11");
5517 no_optimize = 1; /*this code is not compatible with optimzer yet */
5520 * s[0] is a dummy entry to protect other BPF insn from damage
5521 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5522 * hard to find interdependency made by jump table fixup.
5525 s[i] = new_stmt(0); /*dummy*/
5530 b0 = gen_linktype(ETHERTYPE_IP);
5533 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5534 s[i]->s.k = off_macpl + off_nl + 9;
5536 /* X = ip->ip_hl << 2 */
5537 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
5538 s[i]->s.k = off_macpl + off_nl;
5543 b0 = gen_linktype(ETHERTYPE_IPV6);
5545 /* A = ip6->ip_nxt */
5546 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5547 s[i]->s.k = off_macpl + off_nl + 6;
5549 /* X = sizeof(struct ip6_hdr) */
5550 s[i] = new_stmt(BPF_LDX|BPF_IMM);
5556 bpf_error("unsupported proto to gen_protochain");
5560 /* again: if (A == v) goto end; else fall through; */
5562 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5564 s[i]->s.jt = NULL; /*later*/
5565 s[i]->s.jf = NULL; /*update in next stmt*/
5569 #ifndef IPPROTO_NONE
5570 #define IPPROTO_NONE 59
5572 /* if (A == IPPROTO_NONE) goto end */
5573 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5574 s[i]->s.jt = NULL; /*later*/
5575 s[i]->s.jf = NULL; /*update in next stmt*/
5576 s[i]->s.k = IPPROTO_NONE;
5577 s[fix5]->s.jf = s[i];
5581 if (proto == Q_IPV6) {
5582 int v6start, v6end, v6advance, j;
5585 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5586 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5587 s[i]->s.jt = NULL; /*later*/
5588 s[i]->s.jf = NULL; /*update in next stmt*/
5589 s[i]->s.k = IPPROTO_HOPOPTS;
5590 s[fix2]->s.jf = s[i];
5592 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5593 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5594 s[i]->s.jt = NULL; /*later*/
5595 s[i]->s.jf = NULL; /*update in next stmt*/
5596 s[i]->s.k = IPPROTO_DSTOPTS;
5598 /* if (A == IPPROTO_ROUTING) goto v6advance */
5599 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5600 s[i]->s.jt = NULL; /*later*/
5601 s[i]->s.jf = NULL; /*update in next stmt*/
5602 s[i]->s.k = IPPROTO_ROUTING;
5604 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5605 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5606 s[i]->s.jt = NULL; /*later*/
5607 s[i]->s.jf = NULL; /*later*/
5608 s[i]->s.k = IPPROTO_FRAGMENT;
5618 * A = P[X + packet head];
5619 * X = X + (P[X + packet head + 1] + 1) * 8;
5621 /* A = P[X + packet head] */
5622 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5623 s[i]->s.k = off_macpl + off_nl;
5626 s[i] = new_stmt(BPF_ST);
5629 /* A = P[X + packet head + 1]; */
5630 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5631 s[i]->s.k = off_macpl + off_nl + 1;
5634 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5638 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5642 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
5646 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5649 s[i] = new_stmt(BPF_LD|BPF_MEM);
5653 /* goto again; (must use BPF_JA for backward jump) */
5654 s[i] = new_stmt(BPF_JMP|BPF_JA);
5655 s[i]->s.k = again - i - 1;
5656 s[i - 1]->s.jf = s[i];
5660 for (j = v6start; j <= v6end; j++)
5661 s[j]->s.jt = s[v6advance];
5664 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5666 s[fix2]->s.jf = s[i];
5672 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5673 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5674 s[i]->s.jt = NULL; /*later*/
5675 s[i]->s.jf = NULL; /*later*/
5676 s[i]->s.k = IPPROTO_AH;
5678 s[fix3]->s.jf = s[ahcheck];
5685 * X = X + (P[X + 1] + 2) * 4;
5688 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5690 /* A = P[X + packet head]; */
5691 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5692 s[i]->s.k = off_macpl + off_nl;
5695 s[i] = new_stmt(BPF_ST);
5699 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5702 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5706 s[i] = new_stmt(BPF_MISC|BPF_TAX);
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_ALU|BPF_ADD|BPF_K);
5717 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5721 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5724 s[i] = new_stmt(BPF_LD|BPF_MEM);
5728 /* goto again; (must use BPF_JA for backward jump) */
5729 s[i] = new_stmt(BPF_JMP|BPF_JA);
5730 s[i]->s.k = again - i - 1;
5735 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5737 s[fix2]->s.jt = s[end];
5738 s[fix4]->s.jf = s[end];
5739 s[fix5]->s.jt = s[end];
5746 for (i = 0; i < max - 1; i++)
5747 s[i]->next = s[i + 1];
5748 s[max - 1]->next = NULL;
5753 b = new_block(JMP(BPF_JEQ));
5754 b->stmts = s[1]; /*remember, s[0] is dummy*/
5764 static struct block *
5765 gen_check_802_11_data_frame()
5768 struct block *b0, *b1;
5771 * A data frame has the 0x08 bit (b3) in the frame control field set
5772 * and the 0x04 bit (b2) clear.
5774 s = gen_load_a(OR_LINK, 0, BPF_B);
5775 b0 = new_block(JMP(BPF_JSET));
5779 s = gen_load_a(OR_LINK, 0, BPF_B);
5780 b1 = new_block(JMP(BPF_JSET));
5791 * Generate code that checks whether the packet is a packet for protocol
5792 * <proto> and whether the type field in that protocol's header has
5793 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5794 * IP packet and checks the protocol number in the IP header against <v>.
5796 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5797 * against Q_IP and Q_IPV6.
5799 static struct block *
5800 gen_proto(v, proto, dir)
5805 struct block *b0, *b1;
5810 if (dir != Q_DEFAULT)
5811 bpf_error("direction applied to 'proto'");
5815 b0 = gen_proto(v, Q_IP, dir);
5816 b1 = gen_proto(v, Q_IPV6, dir);
5822 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5823 * not LLC encapsulation with LLCSAP_IP.
5825 * For IEEE 802 networks - which includes 802.5 token ring
5826 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5827 * says that SNAP encapsulation is used, not LLC encapsulation
5830 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5831 * RFC 2225 say that SNAP encapsulation is used, not LLC
5832 * encapsulation with LLCSAP_IP.
5834 * So we always check for ETHERTYPE_IP.
5836 b0 = gen_linktype(ETHERTYPE_IP);
5838 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
5840 b1 = gen_protochain(v, Q_IP);
5850 * Frame Relay packets typically have an OSI
5851 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5852 * generates code to check for all the OSI
5853 * NLPIDs, so calling it and then adding a check
5854 * for the particular NLPID for which we're
5855 * looking is bogus, as we can just check for
5858 * What we check for is the NLPID and a frame
5859 * control field value of UI, i.e. 0x03 followed
5862 * XXX - assumes a 2-byte Frame Relay header with
5863 * DLCI and flags. What if the address is longer?
5865 * XXX - what about SNAP-encapsulated frames?
5867 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
5873 * Cisco uses an Ethertype lookalike - for OSI,
5876 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
5877 /* OSI in C-HDLC is stuffed with a fudge byte */
5878 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
5883 b0 = gen_linktype(LLCSAP_ISONS);
5884 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
5890 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5892 * 4 is the offset of the PDU type relative to the IS-IS
5895 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
5900 bpf_error("arp does not encapsulate another protocol");
5904 bpf_error("rarp does not encapsulate another protocol");
5908 bpf_error("atalk encapsulation is not specifiable");
5912 bpf_error("decnet encapsulation is not specifiable");
5916 bpf_error("sca does not encapsulate another protocol");
5920 bpf_error("lat does not encapsulate another protocol");
5924 bpf_error("moprc does not encapsulate another protocol");
5928 bpf_error("mopdl does not encapsulate another protocol");
5932 return gen_linktype(v);
5935 bpf_error("'udp proto' is bogus");
5939 bpf_error("'tcp proto' is bogus");
5943 bpf_error("'sctp proto' is bogus");
5947 bpf_error("'icmp proto' is bogus");
5951 bpf_error("'igmp proto' is bogus");
5955 bpf_error("'igrp proto' is bogus");
5959 bpf_error("'pim proto' is bogus");
5963 bpf_error("'vrrp proto' is bogus");
5967 bpf_error("'carp proto' is bogus");
5971 b0 = gen_linktype(ETHERTYPE_IPV6);
5974 * Also check for a fragment header before the final
5977 b2 = gen_cmp(OR_NET, 6, BPF_B, IPPROTO_FRAGMENT);
5978 b1 = gen_cmp(OR_NET, 40, BPF_B, (bpf_int32)v);
5980 b2 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
5983 b1 = gen_protochain(v, Q_IPV6);
5989 bpf_error("'icmp6 proto' is bogus");
5992 bpf_error("'ah proto' is bogus");
5995 bpf_error("'ah proto' is bogus");
5998 bpf_error("'stp proto' is bogus");
6001 bpf_error("'ipx proto' is bogus");
6004 bpf_error("'netbeui proto' is bogus");
6007 bpf_error("'radio proto' is bogus");
6018 register const char *name;
6021 int proto = q.proto;
6025 bpf_u_int32 mask, addr;
6027 bpf_u_int32 **alist;
6030 struct sockaddr_in *sin4;
6031 struct sockaddr_in6 *sin6;
6032 struct addrinfo *res, *res0;
6033 struct in6_addr mask128;
6035 struct block *b, *tmp;
6036 int port, real_proto;
6042 addr = pcap_nametonetaddr(name);
6044 bpf_error("unknown network '%s'", name);
6045 /* Left justify network addr and calculate its network mask */
6047 while (addr && (addr & 0xff000000) == 0) {
6051 return gen_host(addr, mask, proto, dir, q.addr);
6055 if (proto == Q_LINK) {
6059 case DLT_NETANALYZER:
6060 case DLT_NETANALYZER_TRANSPARENT:
6061 eaddr = pcap_ether_hostton(name);
6064 "unknown ether host '%s'", name);
6065 b = gen_ehostop(eaddr, dir);
6070 eaddr = pcap_ether_hostton(name);
6073 "unknown FDDI host '%s'", name);
6074 b = gen_fhostop(eaddr, dir);
6079 eaddr = pcap_ether_hostton(name);
6082 "unknown token ring host '%s'", name);
6083 b = gen_thostop(eaddr, dir);
6087 case DLT_IEEE802_11:
6088 case DLT_PRISM_HEADER:
6089 case DLT_IEEE802_11_RADIO_AVS:
6090 case DLT_IEEE802_11_RADIO:
6092 eaddr = pcap_ether_hostton(name);
6095 "unknown 802.11 host '%s'", name);
6096 b = gen_wlanhostop(eaddr, dir);
6100 case DLT_IP_OVER_FC:
6101 eaddr = pcap_ether_hostton(name);
6104 "unknown Fibre Channel host '%s'", name);
6105 b = gen_ipfchostop(eaddr, dir);
6114 * Check that the packet doesn't begin
6115 * with an LE Control marker. (We've
6116 * already generated a test for LANE.)
6118 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
6122 eaddr = pcap_ether_hostton(name);
6125 "unknown ether host '%s'", name);
6126 b = gen_ehostop(eaddr, dir);
6132 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6133 } else if (proto == Q_DECNET) {
6134 unsigned short dn_addr = __pcap_nametodnaddr(name);
6136 * I don't think DECNET hosts can be multihomed, so
6137 * there is no need to build up a list of addresses
6139 return (gen_host(dn_addr, 0, proto, dir, q.addr));
6142 alist = pcap_nametoaddr(name);
6143 if (alist == NULL || *alist == NULL)
6144 bpf_error("unknown host '%s'", name);
6146 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
6148 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
6150 tmp = gen_host(**alist++, 0xffffffff,
6151 tproto, dir, q.addr);
6157 memset(&mask128, 0xff, sizeof(mask128));
6158 res0 = res = pcap_nametoaddrinfo(name);
6160 bpf_error("unknown host '%s'", name);
6163 tproto = tproto6 = proto;
6164 if (off_linktype == -1 && tproto == Q_DEFAULT) {
6168 for (res = res0; res; res = res->ai_next) {
6169 switch (res->ai_family) {
6171 if (tproto == Q_IPV6)
6174 sin4 = (struct sockaddr_in *)
6176 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
6177 0xffffffff, tproto, dir, q.addr);
6180 if (tproto6 == Q_IP)
6183 sin6 = (struct sockaddr_in6 *)
6185 tmp = gen_host6(&sin6->sin6_addr,
6186 &mask128, tproto6, dir, q.addr);
6198 bpf_error("unknown host '%s'%s", name,
6199 (proto == Q_DEFAULT)
6201 : " for specified address family");
6208 if (proto != Q_DEFAULT &&
6209 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6210 bpf_error("illegal qualifier of 'port'");
6211 if (pcap_nametoport(name, &port, &real_proto) == 0)
6212 bpf_error("unknown port '%s'", name);
6213 if (proto == Q_UDP) {
6214 if (real_proto == IPPROTO_TCP)
6215 bpf_error("port '%s' is tcp", name);
6216 else if (real_proto == IPPROTO_SCTP)
6217 bpf_error("port '%s' is sctp", name);
6219 /* override PROTO_UNDEF */
6220 real_proto = IPPROTO_UDP;
6222 if (proto == Q_TCP) {
6223 if (real_proto == IPPROTO_UDP)
6224 bpf_error("port '%s' is udp", name);
6226 else if (real_proto == IPPROTO_SCTP)
6227 bpf_error("port '%s' is sctp", name);
6229 /* override PROTO_UNDEF */
6230 real_proto = IPPROTO_TCP;
6232 if (proto == Q_SCTP) {
6233 if (real_proto == IPPROTO_UDP)
6234 bpf_error("port '%s' is udp", name);
6236 else if (real_proto == IPPROTO_TCP)
6237 bpf_error("port '%s' is tcp", name);
6239 /* override PROTO_UNDEF */
6240 real_proto = IPPROTO_SCTP;
6243 bpf_error("illegal port number %d < 0", port);
6245 bpf_error("illegal port number %d > 65535", port);
6246 b = gen_port(port, real_proto, dir);
6247 gen_or(gen_port6(port, real_proto, dir), b);
6251 if (proto != Q_DEFAULT &&
6252 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6253 bpf_error("illegal qualifier of 'portrange'");
6254 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6255 bpf_error("unknown port in range '%s'", name);
6256 if (proto == Q_UDP) {
6257 if (real_proto == IPPROTO_TCP)
6258 bpf_error("port in range '%s' is tcp", name);
6259 else if (real_proto == IPPROTO_SCTP)
6260 bpf_error("port in range '%s' is sctp", name);
6262 /* override PROTO_UNDEF */
6263 real_proto = IPPROTO_UDP;
6265 if (proto == Q_TCP) {
6266 if (real_proto == IPPROTO_UDP)
6267 bpf_error("port in range '%s' is udp", name);
6268 else if (real_proto == IPPROTO_SCTP)
6269 bpf_error("port in range '%s' is sctp", name);
6271 /* override PROTO_UNDEF */
6272 real_proto = IPPROTO_TCP;
6274 if (proto == Q_SCTP) {
6275 if (real_proto == IPPROTO_UDP)
6276 bpf_error("port in range '%s' is udp", name);
6277 else if (real_proto == IPPROTO_TCP)
6278 bpf_error("port in range '%s' is tcp", name);
6280 /* override PROTO_UNDEF */
6281 real_proto = IPPROTO_SCTP;
6284 bpf_error("illegal port number %d < 0", port1);
6286 bpf_error("illegal port number %d > 65535", port1);
6288 bpf_error("illegal port number %d < 0", port2);
6290 bpf_error("illegal port number %d > 65535", port2);
6292 b = gen_portrange(port1, port2, real_proto, dir);
6293 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
6298 eaddr = pcap_ether_hostton(name);
6300 bpf_error("unknown ether host: %s", name);
6302 alist = pcap_nametoaddr(name);
6303 if (alist == NULL || *alist == NULL)
6304 bpf_error("unknown host '%s'", name);
6305 b = gen_gateway(eaddr, alist, proto, dir);
6309 bpf_error("'gateway' not supported in this configuration");
6313 real_proto = lookup_proto(name, proto);
6314 if (real_proto >= 0)
6315 return gen_proto(real_proto, proto, dir);
6317 bpf_error("unknown protocol: %s", name);
6320 real_proto = lookup_proto(name, proto);
6321 if (real_proto >= 0)
6322 return gen_protochain(real_proto, proto, dir);
6324 bpf_error("unknown protocol: %s", name);
6335 gen_mcode(s1, s2, masklen, q)
6336 register const char *s1, *s2;
6337 register int masklen;
6340 register int nlen, mlen;
6343 nlen = __pcap_atoin(s1, &n);
6344 /* Promote short ipaddr */
6348 mlen = __pcap_atoin(s2, &m);
6349 /* Promote short ipaddr */
6352 bpf_error("non-network bits set in \"%s mask %s\"",
6355 /* Convert mask len to mask */
6357 bpf_error("mask length must be <= 32");
6360 * X << 32 is not guaranteed by C to be 0; it's
6365 m = 0xffffffff << (32 - masklen);
6367 bpf_error("non-network bits set in \"%s/%d\"",
6374 return gen_host(n, m, q.proto, q.dir, q.addr);
6377 bpf_error("Mask syntax for networks only");
6386 register const char *s;
6391 int proto = q.proto;
6397 else if (q.proto == Q_DECNET)
6398 vlen = __pcap_atodn(s, &v);
6400 vlen = __pcap_atoin(s, &v);
6407 if (proto == Q_DECNET)
6408 return gen_host(v, 0, proto, dir, q.addr);
6409 else if (proto == Q_LINK) {
6410 bpf_error("illegal link layer address");
6413 if (s == NULL && q.addr == Q_NET) {
6414 /* Promote short net number */
6415 while (v && (v & 0xff000000) == 0) {
6420 /* Promote short ipaddr */
6424 return gen_host(v, mask, proto, dir, q.addr);
6429 proto = IPPROTO_UDP;
6430 else if (proto == Q_TCP)
6431 proto = IPPROTO_TCP;
6432 else if (proto == Q_SCTP)
6433 proto = IPPROTO_SCTP;
6434 else if (proto == Q_DEFAULT)
6435 proto = PROTO_UNDEF;
6437 bpf_error("illegal qualifier of 'port'");
6440 bpf_error("illegal port number %u > 65535", v);
6444 b = gen_port((int)v, proto, dir);
6445 gen_or(gen_port6((int)v, proto, dir), b);
6451 proto = IPPROTO_UDP;
6452 else if (proto == Q_TCP)
6453 proto = IPPROTO_TCP;
6454 else if (proto == Q_SCTP)
6455 proto = IPPROTO_SCTP;
6456 else if (proto == Q_DEFAULT)
6457 proto = PROTO_UNDEF;
6459 bpf_error("illegal qualifier of 'portrange'");
6462 bpf_error("illegal port number %u > 65535", v);
6466 b = gen_portrange((int)v, (int)v, proto, dir);
6467 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
6472 bpf_error("'gateway' requires a name");
6476 return gen_proto((int)v, proto, dir);
6479 return gen_protochain((int)v, proto, dir);
6494 gen_mcode6(s1, s2, masklen, q)
6495 register const char *s1, *s2;
6496 register int masklen;
6499 struct addrinfo *res;
6500 struct in6_addr *addr;
6501 struct in6_addr mask;
6506 bpf_error("no mask %s supported", s2);
6508 res = pcap_nametoaddrinfo(s1);
6510 bpf_error("invalid ip6 address %s", s1);
6513 bpf_error("%s resolved to multiple address", s1);
6514 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
6516 if (sizeof(mask) * 8 < masklen)
6517 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6518 memset(&mask, 0, sizeof(mask));
6519 memset(&mask, 0xff, masklen / 8);
6521 mask.s6_addr[masklen / 8] =
6522 (0xff << (8 - masklen % 8)) & 0xff;
6525 a = (u_int32_t *)addr;
6526 m = (u_int32_t *)&mask;
6527 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
6528 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
6529 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
6537 bpf_error("Mask syntax for networks only");
6541 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
6547 bpf_error("invalid qualifier against IPv6 address");
6556 register const u_char *eaddr;
6559 struct block *b, *tmp;
6561 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6564 case DLT_NETANALYZER:
6565 case DLT_NETANALYZER_TRANSPARENT:
6566 return gen_ehostop(eaddr, (int)q.dir);
6568 return gen_fhostop(eaddr, (int)q.dir);
6570 return gen_thostop(eaddr, (int)q.dir);
6571 case DLT_IEEE802_11:
6572 case DLT_PRISM_HEADER:
6573 case DLT_IEEE802_11_RADIO_AVS:
6574 case DLT_IEEE802_11_RADIO:
6576 return gen_wlanhostop(eaddr, (int)q.dir);
6580 * Check that the packet doesn't begin with an
6581 * LE Control marker. (We've already generated
6584 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6589 * Now check the MAC address.
6591 b = gen_ehostop(eaddr, (int)q.dir);
6596 case DLT_IP_OVER_FC:
6597 return gen_ipfchostop(eaddr, (int)q.dir);
6599 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6603 bpf_error("ethernet address used in non-ether expression");
6610 struct slist *s0, *s1;
6613 * This is definitely not the best way to do this, but the
6614 * lists will rarely get long.
6621 static struct slist *
6627 s = new_stmt(BPF_LDX|BPF_MEM);
6632 static struct slist *
6638 s = new_stmt(BPF_LD|BPF_MEM);
6644 * Modify "index" to use the value stored into its register as an
6645 * offset relative to the beginning of the header for the protocol
6646 * "proto", and allocate a register and put an item "size" bytes long
6647 * (1, 2, or 4) at that offset into that register, making it the register
6651 gen_load(proto, inst, size)
6656 struct slist *s, *tmp;
6658 int regno = alloc_reg();
6660 free_reg(inst->regno);
6664 bpf_error("data size must be 1, 2, or 4");
6680 bpf_error("unsupported index operation");
6684 * The offset is relative to the beginning of the packet
6685 * data, if we have a radio header. (If we don't, this
6688 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
6689 linktype != DLT_IEEE802_11_RADIO &&
6690 linktype != DLT_PRISM_HEADER)
6691 bpf_error("radio information not present in capture");
6694 * Load into the X register the offset computed into the
6695 * register specified by "index".
6697 s = xfer_to_x(inst);
6700 * Load the item at that offset.
6702 tmp = new_stmt(BPF_LD|BPF_IND|size);
6704 sappend(inst->s, s);
6709 * The offset is relative to the beginning of
6710 * the link-layer header.
6712 * XXX - what about ATM LANE? Should the index be
6713 * relative to the beginning of the AAL5 frame, so
6714 * that 0 refers to the beginning of the LE Control
6715 * field, or relative to the beginning of the LAN
6716 * frame, so that 0 refers, for Ethernet LANE, to
6717 * the beginning of the destination address?
6719 s = gen_llprefixlen();
6722 * If "s" is non-null, it has code to arrange that the
6723 * X register contains the length of the prefix preceding
6724 * the link-layer header. Add to it the offset computed
6725 * into the register specified by "index", and move that
6726 * into the X register. Otherwise, just load into the X
6727 * register the offset computed into the register specified
6731 sappend(s, xfer_to_a(inst));
6732 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6733 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6735 s = xfer_to_x(inst);
6738 * Load the item at the sum of the offset we've put in the
6739 * X register and the offset of the start of the link
6740 * layer header (which is 0 if the radio header is
6741 * variable-length; that header length is what we put
6742 * into the X register and then added to the index).
6744 tmp = new_stmt(BPF_LD|BPF_IND|size);
6747 sappend(inst->s, s);
6761 * The offset is relative to the beginning of
6762 * the network-layer header.
6763 * XXX - are there any cases where we want
6766 s = gen_off_macpl();
6769 * If "s" is non-null, it has code to arrange that the
6770 * X register contains the offset of the MAC-layer
6771 * payload. Add to it the offset computed into the
6772 * register specified by "index", and move that into
6773 * the X register. Otherwise, just load into the X
6774 * register the offset computed into the register specified
6778 sappend(s, xfer_to_a(inst));
6779 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6780 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6782 s = xfer_to_x(inst);
6785 * Load the item at the sum of the offset we've put in the
6786 * X register, the offset of the start of the network
6787 * layer header from the beginning of the MAC-layer
6788 * payload, and the purported offset of the start of the
6789 * MAC-layer payload (which might be 0 if there's a
6790 * variable-length prefix before the link-layer header
6791 * or the link-layer header itself is variable-length;
6792 * the variable-length offset of the start of the
6793 * MAC-layer payload is what we put into the X register
6794 * and then added to the index).
6796 tmp = new_stmt(BPF_LD|BPF_IND|size);
6797 tmp->s.k = off_macpl + off_nl;
6799 sappend(inst->s, s);
6802 * Do the computation only if the packet contains
6803 * the protocol in question.
6805 b = gen_proto_abbrev(proto);
6807 gen_and(inst->b, b);
6821 * The offset is relative to the beginning of
6822 * the transport-layer header.
6824 * Load the X register with the length of the IPv4 header
6825 * (plus the offset of the link-layer header, if it's
6826 * a variable-length header), in bytes.
6828 * XXX - are there any cases where we want
6830 * XXX - we should, if we're built with
6831 * IPv6 support, generate code to load either
6832 * IPv4, IPv6, or both, as appropriate.
6834 s = gen_loadx_iphdrlen();
6837 * The X register now contains the sum of the length
6838 * of any variable-length header preceding the link-layer
6839 * header, any variable-length link-layer header, and the
6840 * length of the network-layer header.
6842 * Load into the A register the offset relative to
6843 * the beginning of the transport layer header,
6844 * add the X register to that, move that to the
6845 * X register, and load with an offset from the
6846 * X register equal to the offset of the network
6847 * layer header relative to the beginning of
6848 * the MAC-layer payload plus the fixed-length
6849 * portion of the offset of the MAC-layer payload
6850 * from the beginning of the raw packet data.
6852 sappend(s, xfer_to_a(inst));
6853 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6854 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6855 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
6856 tmp->s.k = off_macpl + off_nl;
6857 sappend(inst->s, s);
6860 * Do the computation only if the packet contains
6861 * the protocol in question - which is true only
6862 * if this is an IP datagram and is the first or
6863 * only fragment of that datagram.
6865 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
6867 gen_and(inst->b, b);
6868 gen_and(gen_proto_abbrev(Q_IP), b);
6872 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6875 inst->regno = regno;
6876 s = new_stmt(BPF_ST);
6878 sappend(inst->s, s);
6884 gen_relation(code, a0, a1, reversed)
6886 struct arth *a0, *a1;
6889 struct slist *s0, *s1, *s2;
6890 struct block *b, *tmp;
6894 if (code == BPF_JEQ) {
6895 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
6896 b = new_block(JMP(code));
6900 b = new_block(BPF_JMP|code|BPF_X);
6906 sappend(a0->s, a1->s);
6910 free_reg(a0->regno);
6911 free_reg(a1->regno);
6913 /* 'and' together protocol checks */
6916 gen_and(a0->b, tmp = a1->b);
6932 int regno = alloc_reg();
6933 struct arth *a = (struct arth *)newchunk(sizeof(*a));
6936 s = new_stmt(BPF_LD|BPF_LEN);
6937 s->next = new_stmt(BPF_ST);
6938 s->next->s.k = regno;
6953 a = (struct arth *)newchunk(sizeof(*a));
6957 s = new_stmt(BPF_LD|BPF_IMM);
6959 s->next = new_stmt(BPF_ST);
6975 s = new_stmt(BPF_ALU|BPF_NEG);
6978 s = new_stmt(BPF_ST);
6986 gen_arth(code, a0, a1)
6988 struct arth *a0, *a1;
6990 struct slist *s0, *s1, *s2;
6994 s2 = new_stmt(BPF_ALU|BPF_X|code);
6999 sappend(a0->s, a1->s);
7001 free_reg(a0->regno);
7002 free_reg(a1->regno);
7004 s0 = new_stmt(BPF_ST);
7005 a0->regno = s0->s.k = alloc_reg();
7012 * Here we handle simple allocation of the scratch registers.
7013 * If too many registers are alloc'd, the allocator punts.
7015 static int regused[BPF_MEMWORDS];
7019 * Initialize the table of used registers and the current register.
7025 memset(regused, 0, sizeof regused);
7029 * Return the next free register.
7034 int n = BPF_MEMWORDS;
7037 if (regused[curreg])
7038 curreg = (curreg + 1) % BPF_MEMWORDS;
7040 regused[curreg] = 1;
7044 bpf_error("too many registers needed to evaluate expression");
7050 * Return a register to the table so it can
7060 static struct block *
7067 s = new_stmt(BPF_LD|BPF_LEN);
7068 b = new_block(JMP(jmp));
7079 return gen_len(BPF_JGE, n);
7083 * Actually, this is less than or equal.
7091 b = gen_len(BPF_JGT, n);
7098 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7099 * the beginning of the link-layer header.
7100 * XXX - that means you can't test values in the radiotap header, but
7101 * as that header is difficult if not impossible to parse generally
7102 * without a loop, that might not be a severe problem. A new keyword
7103 * "radio" could be added for that, although what you'd really want
7104 * would be a way of testing particular radio header values, which
7105 * would generate code appropriate to the radio header in question.
7108 gen_byteop(op, idx, val)
7119 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7122 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7126 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7130 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
7134 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
7138 b = new_block(JMP(BPF_JEQ));
7145 static u_char abroadcast[] = { 0x0 };
7148 gen_broadcast(proto)
7151 bpf_u_int32 hostmask;
7152 struct block *b0, *b1, *b2;
7153 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7161 case DLT_ARCNET_LINUX:
7162 return gen_ahostop(abroadcast, Q_DST);
7164 case DLT_NETANALYZER:
7165 case DLT_NETANALYZER_TRANSPARENT:
7166 return gen_ehostop(ebroadcast, Q_DST);
7168 return gen_fhostop(ebroadcast, Q_DST);
7170 return gen_thostop(ebroadcast, Q_DST);
7171 case DLT_IEEE802_11:
7172 case DLT_PRISM_HEADER:
7173 case DLT_IEEE802_11_RADIO_AVS:
7174 case DLT_IEEE802_11_RADIO:
7176 return gen_wlanhostop(ebroadcast, Q_DST);
7177 case DLT_IP_OVER_FC:
7178 return gen_ipfchostop(ebroadcast, Q_DST);
7182 * Check that the packet doesn't begin with an
7183 * LE Control marker. (We've already generated
7186 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7191 * Now check the MAC address.
7193 b0 = gen_ehostop(ebroadcast, Q_DST);
7199 bpf_error("not a broadcast link");
7205 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7206 * as an indication that we don't know the netmask, and fail
7209 if (netmask == PCAP_NETMASK_UNKNOWN)
7210 bpf_error("netmask not known, so 'ip broadcast' not supported");
7211 b0 = gen_linktype(ETHERTYPE_IP);
7212 hostmask = ~netmask;
7213 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
7214 b2 = gen_mcmp(OR_NET, 16, BPF_W,
7215 (bpf_int32)(~0 & hostmask), hostmask);
7220 bpf_error("only link-layer/IP broadcast filters supported");
7226 * Generate code to test the low-order bit of a MAC address (that's
7227 * the bottom bit of the *first* byte).
7229 static struct block *
7230 gen_mac_multicast(offset)
7233 register struct block *b0;
7234 register struct slist *s;
7236 /* link[offset] & 1 != 0 */
7237 s = gen_load_a(OR_LINK, offset, BPF_B);
7238 b0 = new_block(JMP(BPF_JSET));
7245 gen_multicast(proto)
7248 register struct block *b0, *b1, *b2;
7249 register struct slist *s;
7257 case DLT_ARCNET_LINUX:
7258 /* all ARCnet multicasts use the same address */
7259 return gen_ahostop(abroadcast, Q_DST);
7261 case DLT_NETANALYZER:
7262 case DLT_NETANALYZER_TRANSPARENT:
7263 /* ether[0] & 1 != 0 */
7264 return gen_mac_multicast(0);
7267 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7269 * XXX - was that referring to bit-order issues?
7271 /* fddi[1] & 1 != 0 */
7272 return gen_mac_multicast(1);
7274 /* tr[2] & 1 != 0 */
7275 return gen_mac_multicast(2);
7276 case DLT_IEEE802_11:
7277 case DLT_PRISM_HEADER:
7278 case DLT_IEEE802_11_RADIO_AVS:
7279 case DLT_IEEE802_11_RADIO:
7284 * For control frames, there is no DA.
7286 * For management frames, DA is at an
7287 * offset of 4 from the beginning of
7290 * For data frames, DA is at an offset
7291 * of 4 from the beginning of the packet
7292 * if To DS is clear and at an offset of
7293 * 16 from the beginning of the packet
7298 * Generate the tests to be done for data frames.
7300 * First, check for To DS set, i.e. "link[1] & 0x01".
7302 s = gen_load_a(OR_LINK, 1, BPF_B);
7303 b1 = new_block(JMP(BPF_JSET));
7304 b1->s.k = 0x01; /* To DS */
7308 * If To DS is set, the DA is at 16.
7310 b0 = gen_mac_multicast(16);
7314 * Now, check for To DS not set, i.e. check
7315 * "!(link[1] & 0x01)".
7317 s = gen_load_a(OR_LINK, 1, BPF_B);
7318 b2 = new_block(JMP(BPF_JSET));
7319 b2->s.k = 0x01; /* To DS */
7324 * If To DS is not set, the DA is at 4.
7326 b1 = gen_mac_multicast(4);
7330 * Now OR together the last two checks. That gives
7331 * the complete set of checks for data frames.
7336 * Now check for a data frame.
7337 * I.e, check "link[0] & 0x08".
7339 s = gen_load_a(OR_LINK, 0, BPF_B);
7340 b1 = new_block(JMP(BPF_JSET));
7345 * AND that with the checks done for data frames.
7350 * If the high-order bit of the type value is 0, this
7351 * is a management frame.
7352 * I.e, check "!(link[0] & 0x08)".
7354 s = gen_load_a(OR_LINK, 0, BPF_B);
7355 b2 = new_block(JMP(BPF_JSET));
7361 * For management frames, the DA is at 4.
7363 b1 = gen_mac_multicast(4);
7367 * OR that with the checks done for data frames.
7368 * That gives the checks done for management and
7374 * If the low-order bit of the type value is 1,
7375 * this is either a control frame or a frame
7376 * with a reserved type, and thus not a
7379 * I.e., check "!(link[0] & 0x04)".
7381 s = gen_load_a(OR_LINK, 0, BPF_B);
7382 b1 = new_block(JMP(BPF_JSET));
7388 * AND that with the checks for data and management
7393 case DLT_IP_OVER_FC:
7394 b0 = gen_mac_multicast(2);
7399 * Check that the packet doesn't begin with an
7400 * LE Control marker. (We've already generated
7403 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7407 /* ether[off_mac] & 1 != 0 */
7408 b0 = gen_mac_multicast(off_mac);
7416 /* Link not known to support multicasts */
7420 b0 = gen_linktype(ETHERTYPE_IP);
7421 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
7426 b0 = gen_linktype(ETHERTYPE_IPV6);
7427 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
7431 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7437 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7438 * Outbound traffic is sent by this machine, while inbound traffic is
7439 * sent by a remote machine (and may include packets destined for a
7440 * unicast or multicast link-layer address we are not subscribing to).
7441 * These are the same definitions implemented by pcap_setdirection().
7442 * Capturing only unicast traffic destined for this host is probably
7443 * better accomplished using a higher-layer filter.
7449 register struct block *b0;
7452 * Only some data link types support inbound/outbound qualifiers.
7456 b0 = gen_relation(BPF_JEQ,
7457 gen_load(Q_LINK, gen_loadi(0), 1),
7464 /* match outgoing packets */
7465 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
7467 /* match incoming packets */
7468 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
7473 /* match outgoing packets */
7474 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
7476 /* to filter on inbound traffic, invert the match */
7481 #ifdef HAVE_NET_PFVAR_H
7483 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
7484 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
7490 /* match outgoing packets */
7491 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
7493 /* match incoming packets */
7494 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
7498 case DLT_JUNIPER_MFR:
7499 case DLT_JUNIPER_MLFR:
7500 case DLT_JUNIPER_MLPPP:
7501 case DLT_JUNIPER_ATM1:
7502 case DLT_JUNIPER_ATM2:
7503 case DLT_JUNIPER_PPPOE:
7504 case DLT_JUNIPER_PPPOE_ATM:
7505 case DLT_JUNIPER_GGSN:
7506 case DLT_JUNIPER_ES:
7507 case DLT_JUNIPER_MONITOR:
7508 case DLT_JUNIPER_SERVICES:
7509 case DLT_JUNIPER_ETHER:
7510 case DLT_JUNIPER_PPP:
7511 case DLT_JUNIPER_FRELAY:
7512 case DLT_JUNIPER_CHDLC:
7513 case DLT_JUNIPER_VP:
7514 case DLT_JUNIPER_ST:
7515 case DLT_JUNIPER_ISM:
7516 case DLT_JUNIPER_VS:
7517 case DLT_JUNIPER_SRX_E2E:
7518 case DLT_JUNIPER_FIBRECHANNEL:
7519 case DLT_JUNIPER_ATM_CEMIC:
7521 /* juniper flags (including direction) are stored
7522 * the byte after the 3-byte magic number */
7524 /* match outgoing packets */
7525 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
7527 /* match incoming packets */
7528 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
7534 * If we have packet meta-data indicating a direction,
7535 * check it, otherwise give up as this link-layer type
7536 * has nothing in the packet data.
7538 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7540 * We infer that this is Linux with PF_PACKET support.
7541 * If this is a *live* capture, we can look at
7542 * special meta-data in the filter expression;
7543 * if it's a savefile, we can't.
7545 if (bpf_pcap->sf.rfile != NULL) {
7546 /* We have a FILE *, so this is a savefile */
7547 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7552 /* match outgoing packets */
7553 b0 = gen_cmp(OR_LINK, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
7556 /* to filter on inbound traffic, invert the match */
7559 #else /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7560 bpf_error("inbound/outbound not supported on linktype %d",
7564 #endif /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7569 #ifdef HAVE_NET_PFVAR_H
7570 /* PF firewall log matched interface */
7572 gen_pf_ifname(const char *ifname)
7577 if (linktype != DLT_PFLOG) {
7578 bpf_error("ifname supported only on PF linktype");
7581 len = sizeof(((struct pfloghdr *)0)->ifname);
7582 off = offsetof(struct pfloghdr, ifname);
7583 if (strlen(ifname) >= len) {
7584 bpf_error("ifname interface names can only be %d characters",
7588 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
7592 /* PF firewall log ruleset name */
7594 gen_pf_ruleset(char *ruleset)
7598 if (linktype != DLT_PFLOG) {
7599 bpf_error("ruleset supported only on PF linktype");
7603 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
7604 bpf_error("ruleset names can only be %ld characters",
7605 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
7609 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
7610 strlen(ruleset), (const u_char *)ruleset);
7614 /* PF firewall log rule number */
7620 if (linktype != DLT_PFLOG) {
7621 bpf_error("rnr supported only on PF linktype");
7625 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
7630 /* PF firewall log sub-rule number */
7632 gen_pf_srnr(int srnr)
7636 if (linktype != DLT_PFLOG) {
7637 bpf_error("srnr supported only on PF linktype");
7641 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
7646 /* PF firewall log reason code */
7648 gen_pf_reason(int reason)
7652 if (linktype != DLT_PFLOG) {
7653 bpf_error("reason supported only on PF linktype");
7657 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
7662 /* PF firewall log action */
7664 gen_pf_action(int action)
7668 if (linktype != DLT_PFLOG) {
7669 bpf_error("action supported only on PF linktype");
7673 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
7677 #else /* !HAVE_NET_PFVAR_H */
7679 gen_pf_ifname(const char *ifname)
7681 bpf_error("libpcap was compiled without pf support");
7687 gen_pf_ruleset(char *ruleset)
7689 bpf_error("libpcap was compiled on a machine without pf support");
7697 bpf_error("libpcap was compiled on a machine without pf support");
7703 gen_pf_srnr(int srnr)
7705 bpf_error("libpcap was compiled on a machine without pf support");
7711 gen_pf_reason(int reason)
7713 bpf_error("libpcap was compiled on a machine without pf support");
7719 gen_pf_action(int action)
7721 bpf_error("libpcap was compiled on a machine without pf support");
7725 #endif /* HAVE_NET_PFVAR_H */
7727 /* IEEE 802.11 wireless header */
7729 gen_p80211_type(int type, int mask)
7735 case DLT_IEEE802_11:
7736 case DLT_PRISM_HEADER:
7737 case DLT_IEEE802_11_RADIO_AVS:
7738 case DLT_IEEE802_11_RADIO:
7739 b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
7744 bpf_error("802.11 link-layer types supported only on 802.11");
7752 gen_p80211_fcdir(int fcdir)
7758 case DLT_IEEE802_11:
7759 case DLT_PRISM_HEADER:
7760 case DLT_IEEE802_11_RADIO_AVS:
7761 case DLT_IEEE802_11_RADIO:
7765 bpf_error("frame direction supported only with 802.11 headers");
7769 b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
7770 (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
7777 register const u_char *eaddr;
7783 case DLT_ARCNET_LINUX:
7784 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
7786 return (gen_ahostop(eaddr, (int)q.dir));
7788 bpf_error("ARCnet address used in non-arc expression");
7794 bpf_error("aid supported only on ARCnet");
7797 bpf_error("ARCnet address used in non-arc expression");
7802 static struct block *
7803 gen_ahostop(eaddr, dir)
7804 register const u_char *eaddr;
7807 register struct block *b0, *b1;
7810 /* src comes first, different from Ethernet */
7812 return gen_bcmp(OR_LINK, 0, 1, eaddr);
7815 return gen_bcmp(OR_LINK, 1, 1, eaddr);
7818 b0 = gen_ahostop(eaddr, Q_SRC);
7819 b1 = gen_ahostop(eaddr, Q_DST);
7825 b0 = gen_ahostop(eaddr, Q_SRC);
7826 b1 = gen_ahostop(eaddr, Q_DST);
7831 bpf_error("'addr1' is only supported on 802.11");
7835 bpf_error("'addr2' is only supported on 802.11");
7839 bpf_error("'addr3' is only supported on 802.11");
7843 bpf_error("'addr4' is only supported on 802.11");
7847 bpf_error("'ra' is only supported on 802.11");
7851 bpf_error("'ta' is only supported on 802.11");
7859 * support IEEE 802.1Q VLAN trunk over ethernet
7865 struct block *b0, *b1;
7867 /* can't check for VLAN-encapsulated packets inside MPLS */
7868 if (label_stack_depth > 0)
7869 bpf_error("no VLAN match after MPLS");
7872 * Check for a VLAN packet, and then change the offsets to point
7873 * to the type and data fields within the VLAN packet. Just
7874 * increment the offsets, so that we can support a hierarchy, e.g.
7875 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7878 * XXX - this is a bit of a kludge. If we were to split the
7879 * compiler into a parser that parses an expression and
7880 * generates an expression tree, and a code generator that
7881 * takes an expression tree (which could come from our
7882 * parser or from some other parser) and generates BPF code,
7883 * we could perhaps make the offsets parameters of routines
7884 * and, in the handler for an "AND" node, pass to subnodes
7885 * other than the VLAN node the adjusted offsets.
7887 * This would mean that "vlan" would, instead of changing the
7888 * behavior of *all* tests after it, change only the behavior
7889 * of tests ANDed with it. That would change the documented
7890 * semantics of "vlan", which might break some expressions.
7891 * However, it would mean that "(vlan and ip) or ip" would check
7892 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7893 * checking only for VLAN-encapsulated IP, so that could still
7894 * be considered worth doing; it wouldn't break expressions
7895 * that are of the form "vlan and ..." or "vlan N and ...",
7896 * which I suspect are the most common expressions involving
7897 * "vlan". "vlan or ..." doesn't necessarily do what the user
7898 * would really want, now, as all the "or ..." tests would
7899 * be done assuming a VLAN, even though the "or" could be viewed
7900 * as meaning "or, if this isn't a VLAN packet...".
7907 case DLT_NETANALYZER:
7908 case DLT_NETANALYZER_TRANSPARENT:
7909 /* check for VLAN, including QinQ */
7910 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7911 (bpf_int32)ETHERTYPE_8021Q);
7912 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7913 (bpf_int32)ETHERTYPE_8021QINQ);
7917 /* If a specific VLAN is requested, check VLAN id */
7918 if (vlan_num >= 0) {
7919 b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
7920 (bpf_int32)vlan_num, 0x0fff);
7934 bpf_error("no VLAN support for data link type %d",
7949 struct block *b0,*b1;
7952 * Change the offsets to point to the type and data fields within
7953 * the MPLS packet. Just increment the offsets, so that we
7954 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7955 * capture packets with an outer label of 100000 and an inner
7958 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7962 if (label_stack_depth > 0) {
7963 /* just match the bottom-of-stack bit clear */
7964 b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
7967 * Indicate that we're checking MPLS-encapsulated headers,
7968 * to make sure higher level code generators don't try to
7969 * match against IP-related protocols such as Q_ARP, Q_RARP
7974 case DLT_C_HDLC: /* fall through */
7976 case DLT_NETANALYZER:
7977 case DLT_NETANALYZER_TRANSPARENT:
7978 b0 = gen_linktype(ETHERTYPE_MPLS);
7982 b0 = gen_linktype(PPP_MPLS_UCAST);
7985 /* FIXME add other DLT_s ...
7986 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7987 * leave it for now */
7990 bpf_error("no MPLS support for data link type %d",
7998 /* If a specific MPLS label is requested, check it */
7999 if (label_num >= 0) {
8000 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
8001 b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
8002 0xfffff000); /* only compare the first 20 bits */
8009 label_stack_depth++;
8014 * Support PPPOE discovery and session.
8019 /* check for PPPoE discovery */
8020 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
8029 * Test against the PPPoE session link-layer type.
8031 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
8034 * Change the offsets to point to the type and data fields within
8035 * the PPP packet, and note that this is PPPoE rather than
8038 * XXX - this is a bit of a kludge. If we were to split the
8039 * compiler into a parser that parses an expression and
8040 * generates an expression tree, and a code generator that
8041 * takes an expression tree (which could come from our
8042 * parser or from some other parser) and generates BPF code,
8043 * we could perhaps make the offsets parameters of routines
8044 * and, in the handler for an "AND" node, pass to subnodes
8045 * other than the PPPoE node the adjusted offsets.
8047 * This would mean that "pppoes" would, instead of changing the
8048 * behavior of *all* tests after it, change only the behavior
8049 * of tests ANDed with it. That would change the documented
8050 * semantics of "pppoes", which might break some expressions.
8051 * However, it would mean that "(pppoes and ip) or ip" would check
8052 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8053 * checking only for VLAN-encapsulated IP, so that could still
8054 * be considered worth doing; it wouldn't break expressions
8055 * that are of the form "pppoes and ..." which I suspect are the
8056 * most common expressions involving "pppoes". "pppoes or ..."
8057 * doesn't necessarily do what the user would really want, now,
8058 * as all the "or ..." tests would be done assuming PPPoE, even
8059 * though the "or" could be viewed as meaning "or, if this isn't
8060 * a PPPoE packet...".
8062 orig_linktype = off_linktype; /* save original values */
8067 * The "network-layer" protocol is PPPoE, which has a 6-byte
8068 * PPPoE header, followed by a PPP packet.
8070 * There is no HDLC encapsulation for the PPP packet (it's
8071 * encapsulated in PPPoES instead), so the link-layer type
8072 * starts at the first byte of the PPP packet. For PPPoE,
8073 * that offset is relative to the beginning of the total
8074 * link-layer payload, including any 802.2 LLC header, so
8075 * it's 6 bytes past off_nl.
8077 off_linktype = off_nl + 6;
8080 * The network-layer offsets are relative to the beginning
8081 * of the MAC-layer payload; that's past the 6-byte
8082 * PPPoE header and the 2-byte PPP header.
8085 off_nl_nosnap = 6+2;
8091 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
8103 bpf_error("'vpi' supported only on raw ATM");
8104 if (off_vpi == (u_int)-1)
8106 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
8112 bpf_error("'vci' supported only on raw ATM");
8113 if (off_vci == (u_int)-1)
8115 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
8120 if (off_proto == (u_int)-1)
8121 abort(); /* XXX - this isn't on FreeBSD */
8122 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
8127 if (off_payload == (u_int)-1)
8129 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
8130 0xffffffff, jtype, reverse, jvalue);
8135 bpf_error("'callref' supported only on raw ATM");
8136 if (off_proto == (u_int)-1)
8138 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
8139 jtype, reverse, jvalue);
8149 gen_atmtype_abbrev(type)
8152 struct block *b0, *b1;
8157 /* Get all packets in Meta signalling Circuit */
8159 bpf_error("'metac' supported only on raw ATM");
8160 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8161 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
8166 /* Get all packets in Broadcast Circuit*/
8168 bpf_error("'bcc' supported only on raw ATM");
8169 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8170 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
8175 /* Get all cells in Segment OAM F4 circuit*/
8177 bpf_error("'oam4sc' supported only on raw ATM");
8178 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8179 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8184 /* Get all cells in End-to-End OAM F4 Circuit*/
8186 bpf_error("'oam4ec' supported only on raw ATM");
8187 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8188 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8193 /* Get all packets in connection Signalling Circuit */
8195 bpf_error("'sc' supported only on raw ATM");
8196 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8197 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
8202 /* Get all packets in ILMI Circuit */
8204 bpf_error("'ilmic' supported only on raw ATM");
8205 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8206 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
8211 /* Get all LANE packets */
8213 bpf_error("'lane' supported only on raw ATM");
8214 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
8217 * Arrange that all subsequent tests assume LANE
8218 * rather than LLC-encapsulated packets, and set
8219 * the offsets appropriately for LANE-encapsulated
8222 * "off_mac" is the offset of the Ethernet header,
8223 * which is 2 bytes past the ATM pseudo-header
8224 * (skipping the pseudo-header and 2-byte LE Client
8225 * field). The other offsets are Ethernet offsets
8226 * relative to "off_mac".
8229 off_mac = off_payload + 2; /* MAC header */
8230 off_linktype = off_mac + 12;
8231 off_macpl = off_mac + 14; /* Ethernet */
8232 off_nl = 0; /* Ethernet II */
8233 off_nl_nosnap = 3; /* 802.3+802.2 */
8237 /* Get all LLC-encapsulated packets */
8239 bpf_error("'llc' supported only on raw ATM");
8240 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
8251 * Filtering for MTP2 messages based on li value
8252 * FISU, length is null
8253 * LSSU, length is 1 or 2
8254 * MSU, length is 3 or more
8257 gen_mtp2type_abbrev(type)
8260 struct block *b0, *b1;
8265 if ( (linktype != DLT_MTP2) &&
8266 (linktype != DLT_ERF) &&
8267 (linktype != DLT_MTP2_WITH_PHDR) )
8268 bpf_error("'fisu' supported only on MTP2");
8269 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8270 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8274 if ( (linktype != DLT_MTP2) &&
8275 (linktype != DLT_ERF) &&
8276 (linktype != DLT_MTP2_WITH_PHDR) )
8277 bpf_error("'lssu' supported only on MTP2");
8278 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8279 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
8284 if ( (linktype != DLT_MTP2) &&
8285 (linktype != DLT_ERF) &&
8286 (linktype != DLT_MTP2_WITH_PHDR) )
8287 bpf_error("'msu' supported only on MTP2");
8288 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
8298 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
8305 bpf_u_int32 val1 , val2 , val3;
8307 switch (mtp3field) {
8310 if (off_sio == (u_int)-1)
8311 bpf_error("'sio' supported only on SS7");
8312 /* sio coded on 1 byte so max value 255 */
8314 bpf_error("sio value %u too big; max value = 255",
8316 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
8317 (u_int)jtype, reverse, (u_int)jvalue);
8321 if (off_opc == (u_int)-1)
8322 bpf_error("'opc' supported only on SS7");
8323 /* opc coded on 14 bits so max value 16383 */
8325 bpf_error("opc value %u too big; max value = 16383",
8327 /* the following instructions are made to convert jvalue
8328 * to the form used to write opc in an ss7 message*/
8329 val1 = jvalue & 0x00003c00;
8331 val2 = jvalue & 0x000003fc;
8333 val3 = jvalue & 0x00000003;
8335 jvalue = val1 + val2 + val3;
8336 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
8337 (u_int)jtype, reverse, (u_int)jvalue);
8341 if (off_dpc == (u_int)-1)
8342 bpf_error("'dpc' supported only on SS7");
8343 /* dpc coded on 14 bits so max value 16383 */
8345 bpf_error("dpc value %u too big; max value = 16383",
8347 /* the following instructions are made to convert jvalue
8348 * to the forme used to write dpc in an ss7 message*/
8349 val1 = jvalue & 0x000000ff;
8351 val2 = jvalue & 0x00003f00;
8353 jvalue = val1 + val2;
8354 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
8355 (u_int)jtype, reverse, (u_int)jvalue);
8359 if (off_sls == (u_int)-1)
8360 bpf_error("'sls' supported only on SS7");
8361 /* sls coded on 4 bits so max value 15 */
8363 bpf_error("sls value %u too big; max value = 15",
8365 /* the following instruction is made to convert jvalue
8366 * to the forme used to write sls in an ss7 message*/
8367 jvalue = jvalue << 4;
8368 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
8369 (u_int)jtype,reverse, (u_int)jvalue);
8378 static struct block *
8379 gen_msg_abbrev(type)
8385 * Q.2931 signalling protocol messages for handling virtual circuits
8386 * establishment and teardown
8391 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
8395 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
8399 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
8403 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
8407 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
8410 case A_RELEASE_DONE:
8411 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
8421 gen_atmmulti_abbrev(type)
8424 struct block *b0, *b1;
8430 bpf_error("'oam' supported only on raw ATM");
8431 b1 = gen_atmmulti_abbrev(A_OAMF4);
8436 bpf_error("'oamf4' supported only on raw ATM");
8438 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8439 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8441 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8447 * Get Q.2931 signalling messages for switched
8448 * virtual connection
8451 bpf_error("'connectmsg' supported only on raw ATM");
8452 b0 = gen_msg_abbrev(A_SETUP);
8453 b1 = gen_msg_abbrev(A_CALLPROCEED);
8455 b0 = gen_msg_abbrev(A_CONNECT);
8457 b0 = gen_msg_abbrev(A_CONNECTACK);
8459 b0 = gen_msg_abbrev(A_RELEASE);
8461 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8463 b0 = gen_atmtype_abbrev(A_SC);
8469 bpf_error("'metaconnect' supported only on raw ATM");
8470 b0 = gen_msg_abbrev(A_SETUP);
8471 b1 = gen_msg_abbrev(A_CALLPROCEED);
8473 b0 = gen_msg_abbrev(A_CONNECT);
8475 b0 = gen_msg_abbrev(A_RELEASE);
8477 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8479 b0 = gen_atmtype_abbrev(A_METAC);