1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 * XXX - why was this included even on UNIX?
56 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
76 #include "ethertype.h"
80 #include "ieee80211.h"
82 #include "sunatmpos.h"
85 #include "pcap/ipnet.h"
87 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
88 #include <linux/types.h>
89 #include <linux/if_packet.h>
90 #include <linux/filter.h>
92 #ifdef HAVE_NET_PFVAR_H
93 #include <sys/socket.h>
95 #include <net/pfvar.h>
96 #include <net/if_pflog.h>
99 #define offsetof(s, e) ((size_t)&((s *)0)->e)
103 #include <netdb.h> /* for "struct addrinfo" */
106 #include <pcap/namedb.h>
108 #define ETHERMTU 1500
111 #define IPPROTO_SCTP 132
114 #ifdef HAVE_OS_PROTO_H
115 #include "os-proto.h"
118 #define JMP(c) ((c)|BPF_JMP|BPF_K)
121 static jmp_buf top_ctx
;
122 static pcap_t
*bpf_pcap
;
124 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
126 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
128 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
133 static int pcap_fddipad
;
138 bpf_error(const char *fmt
, ...)
143 if (bpf_pcap
!= NULL
)
144 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
151 static void init_linktype(pcap_t
*);
153 static void init_regs(void);
154 static int alloc_reg(void);
155 static void free_reg(int);
157 static struct block
*root
;
160 * Value passed to gen_load_a() to indicate what the offset argument
164 OR_PACKET
, /* relative to the beginning of the packet */
165 OR_LINK
, /* relative to the beginning of the link-layer header */
166 OR_MACPL
, /* relative to the end of the MAC-layer header */
167 OR_NET
, /* relative to the network-layer header */
168 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
169 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
170 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
175 * As errors are handled by a longjmp, anything allocated must be freed
176 * in the longjmp handler, so it must be reachable from that handler.
177 * One thing that's allocated is the result of pcap_nametoaddrinfo();
178 * it must be freed with freeaddrinfo(). This variable points to any
179 * addrinfo structure that would need to be freed.
181 static struct addrinfo
*ai
;
185 * We divy out chunks of memory rather than call malloc each time so
186 * we don't have to worry about leaking memory. It's probably
187 * not a big deal if all this memory was wasted but if this ever
188 * goes into a library that would probably not be a good idea.
190 * XXX - this *is* in a library....
193 #define CHUNK0SIZE 1024
199 static struct chunk chunks
[NCHUNKS
];
200 static int cur_chunk
;
202 static void *newchunk(u_int
);
203 static void freechunks(void);
204 static inline struct block
*new_block(int);
205 static inline struct slist
*new_stmt(int);
206 static struct block
*gen_retblk(int);
207 static inline void syntax(void);
209 static void backpatch(struct block
*, struct block
*);
210 static void merge(struct block
*, struct block
*);
211 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
212 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
213 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
214 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
215 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
216 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
218 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
219 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
220 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
221 static struct slist
*gen_load_llrel(u_int
, u_int
);
222 static struct slist
*gen_load_macplrel(u_int
, u_int
);
223 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
224 static struct slist
*gen_loadx_iphdrlen(void);
225 static struct block
*gen_uncond(int);
226 static inline struct block
*gen_true(void);
227 static inline struct block
*gen_false(void);
228 static struct block
*gen_ether_linktype(int);
229 static struct block
*gen_ipnet_linktype(int);
230 static struct block
*gen_linux_sll_linktype(int);
231 static struct slist
*gen_load_prism_llprefixlen(void);
232 static struct slist
*gen_load_avs_llprefixlen(void);
233 static struct slist
*gen_load_radiotap_llprefixlen(void);
234 static struct slist
*gen_load_ppi_llprefixlen(void);
235 static void insert_compute_vloffsets(struct block
*);
236 static struct slist
*gen_llprefixlen(void);
237 static struct slist
*gen_off_macpl(void);
238 static int ethertype_to_ppptype(int);
239 static struct block
*gen_linktype(int);
240 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
241 static struct block
*gen_llc_linktype(int);
242 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
243 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
244 static struct block
*gen_ahostop(const u_char
*, int);
245 static struct block
*gen_ehostop(const u_char
*, int);
246 static struct block
*gen_fhostop(const u_char
*, int);
247 static struct block
*gen_thostop(const u_char
*, int);
248 static struct block
*gen_wlanhostop(const u_char
*, int);
249 static struct block
*gen_ipfchostop(const u_char
*, int);
250 static struct block
*gen_dnhostop(bpf_u_int32
, int);
251 static struct block
*gen_mpls_linktype(int);
252 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
253 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
255 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
257 static struct block
*gen_ipfrag(void);
258 static struct block
*gen_portatom(int, bpf_int32
);
259 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
260 static struct block
*gen_portatom6(int, bpf_int32
);
261 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
262 struct block
*gen_portop(int, int, int);
263 static struct block
*gen_port(int, int, int);
264 struct block
*gen_portrangeop(int, int, int, int);
265 static struct block
*gen_portrange(int, int, int, int);
266 struct block
*gen_portop6(int, int, int);
267 static struct block
*gen_port6(int, int, int);
268 struct block
*gen_portrangeop6(int, int, int, int);
269 static struct block
*gen_portrange6(int, int, int, int);
270 static int lookup_proto(const char *, int);
271 static struct block
*gen_protochain(int, int, int);
272 static struct block
*gen_proto(int, int, int);
273 static struct slist
*xfer_to_x(struct arth
*);
274 static struct slist
*xfer_to_a(struct arth
*);
275 static struct block
*gen_mac_multicast(int);
276 static struct block
*gen_len(int, int);
277 static struct block
*gen_check_802_11_data_frame(void);
279 static struct block
*gen_ppi_dlt_check(void);
280 static struct block
*gen_msg_abbrev(int type
);
291 /* XXX Round up to nearest long. */
292 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
294 /* XXX Round up to structure boundary. */
298 cp
= &chunks
[cur_chunk
];
299 if (n
> cp
->n_left
) {
300 ++cp
, k
= ++cur_chunk
;
302 bpf_error("out of memory");
303 size
= CHUNK0SIZE
<< k
;
304 cp
->m
= (void *)malloc(size
);
306 bpf_error("out of memory");
307 memset((char *)cp
->m
, 0, size
);
310 bpf_error("out of memory");
313 return (void *)((char *)cp
->m
+ cp
->n_left
);
322 for (i
= 0; i
< NCHUNKS
; ++i
)
323 if (chunks
[i
].m
!= NULL
) {
330 * A strdup whose allocations are freed after code generation is over.
334 register const char *s
;
336 int n
= strlen(s
) + 1;
337 char *cp
= newchunk(n
);
343 static inline struct block
*
349 p
= (struct block
*)newchunk(sizeof(*p
));
356 static inline struct slist
*
362 p
= (struct slist
*)newchunk(sizeof(*p
));
368 static struct block
*
372 struct block
*b
= new_block(BPF_RET
|BPF_K
);
381 bpf_error("syntax error in filter expression");
384 static bpf_u_int32 netmask
;
389 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
390 const char *buf
, int optimize
, bpf_u_int32 mask
);
393 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
394 const char *buf
, int optimize
, bpf_u_int32 mask
)
398 EnterCriticalSection(&g_PcapCompileCriticalSection
);
400 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
402 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
408 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
409 const char *buf
, int optimize
, bpf_u_int32 mask
)
412 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
413 const char *buf
, int optimize
, bpf_u_int32 mask
)
417 const char * volatile xbuf
= buf
;
421 * If this pcap_t hasn't been activated, it doesn't have a
422 * link-layer type, so we can't use it.
425 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
426 "not-yet-activated pcap_t passed to pcap_compile");
434 if (setjmp(top_ctx
)) {
448 snaplen
= pcap_snapshot(p
);
450 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
451 "snaplen of 0 rejects all packets");
455 lex_init(xbuf
? xbuf
: "");
463 root
= gen_retblk(snaplen
);
465 if (optimize
&& !no_optimize
) {
468 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
469 bpf_error("expression rejects all packets");
471 program
->bf_insns
= icode_to_fcode(root
, &len
);
472 program
->bf_len
= len
;
480 * entry point for using the compiler with no pcap open
481 * pass in all the stuff that is needed explicitly instead.
484 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
485 struct bpf_program
*program
,
486 const char *buf
, int optimize
, bpf_u_int32 mask
)
491 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
494 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
500 * Clean up a "struct bpf_program" by freeing all the memory allocated
504 pcap_freecode(struct bpf_program
*program
)
507 if (program
->bf_insns
!= NULL
) {
508 free((char *)program
->bf_insns
);
509 program
->bf_insns
= NULL
;
514 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
515 * which of the jt and jf fields has been resolved and which is a pointer
516 * back to another unresolved block (or nil). At least one of the fields
517 * in each block is already resolved.
520 backpatch(list
, target
)
521 struct block
*list
, *target
;
538 * Merge the lists in b0 and b1, using the 'sense' field to indicate
539 * which of jt and jf is the link.
543 struct block
*b0
, *b1
;
545 register struct block
**p
= &b0
;
547 /* Find end of list. */
549 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
551 /* Concatenate the lists. */
559 struct block
*ppi_dlt_check
;
562 * Insert before the statements of the first (root) block any
563 * statements needed to load the lengths of any variable-length
564 * headers into registers.
566 * XXX - a fancier strategy would be to insert those before the
567 * statements of all blocks that use those lengths and that
568 * have no predecessors that use them, so that we only compute
569 * the lengths if we need them. There might be even better
570 * approaches than that.
572 * However, those strategies would be more complicated, and
573 * as we don't generate code to compute a length if the
574 * program has no tests that use the length, and as most
575 * tests will probably use those lengths, we would just
576 * postpone computing the lengths so that it's not done
577 * for tests that fail early, and it's not clear that's
580 insert_compute_vloffsets(p
->head
);
583 * For DLT_PPI captures, generate a check of the per-packet
584 * DLT value to make sure it's DLT_IEEE802_11.
586 ppi_dlt_check
= gen_ppi_dlt_check();
587 if (ppi_dlt_check
!= NULL
)
588 gen_and(ppi_dlt_check
, p
);
590 backpatch(p
, gen_retblk(snaplen
));
591 p
->sense
= !p
->sense
;
592 backpatch(p
, gen_retblk(0));
598 struct block
*b0
, *b1
;
600 backpatch(b0
, b1
->head
);
601 b0
->sense
= !b0
->sense
;
602 b1
->sense
= !b1
->sense
;
604 b1
->sense
= !b1
->sense
;
610 struct block
*b0
, *b1
;
612 b0
->sense
= !b0
->sense
;
613 backpatch(b0
, b1
->head
);
614 b0
->sense
= !b0
->sense
;
623 b
->sense
= !b
->sense
;
626 static struct block
*
627 gen_cmp(offrel
, offset
, size
, v
)
628 enum e_offrel offrel
;
632 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
635 static struct block
*
636 gen_cmp_gt(offrel
, offset
, size
, v
)
637 enum e_offrel offrel
;
641 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
644 static struct block
*
645 gen_cmp_ge(offrel
, offset
, size
, v
)
646 enum e_offrel offrel
;
650 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
653 static struct block
*
654 gen_cmp_lt(offrel
, offset
, size
, v
)
655 enum e_offrel offrel
;
659 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
662 static struct block
*
663 gen_cmp_le(offrel
, offset
, size
, v
)
664 enum e_offrel offrel
;
668 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
671 static struct block
*
672 gen_mcmp(offrel
, offset
, size
, v
, mask
)
673 enum e_offrel offrel
;
678 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
681 static struct block
*
682 gen_bcmp(offrel
, offset
, size
, v
)
683 enum e_offrel offrel
;
684 register u_int offset
, size
;
685 register const u_char
*v
;
687 register struct block
*b
, *tmp
;
691 register const u_char
*p
= &v
[size
- 4];
692 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
693 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
695 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
702 register const u_char
*p
= &v
[size
- 2];
703 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
705 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
712 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
721 * AND the field of size "size" at offset "offset" relative to the header
722 * specified by "offrel" with "mask", and compare it with the value "v"
723 * with the test specified by "jtype"; if "reverse" is true, the test
724 * should test the opposite of "jtype".
726 static struct block
*
727 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
728 enum e_offrel offrel
;
730 bpf_u_int32 offset
, size
, mask
, jtype
;
733 struct slist
*s
, *s2
;
736 s
= gen_load_a(offrel
, offset
, size
);
738 if (mask
!= 0xffffffff) {
739 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
744 b
= new_block(JMP(jtype
));
747 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
753 * Various code constructs need to know the layout of the data link
754 * layer. These variables give the necessary offsets from the beginning
755 * of the packet data.
759 * This is the offset of the beginning of the link-layer header from
760 * the beginning of the raw packet data.
762 * It's usually 0, except for 802.11 with a fixed-length radio header.
763 * (For 802.11 with a variable-length radio header, we have to generate
764 * code to compute that offset; off_ll is 0 in that case.)
769 * If there's a variable-length header preceding the link-layer header,
770 * "reg_off_ll" is the register number for a register containing the
771 * length of that header, and therefore the offset of the link-layer
772 * header from the beginning of the raw packet data. Otherwise,
773 * "reg_off_ll" is -1.
775 static int reg_off_ll
;
778 * This is the offset of the beginning of the MAC-layer header from
779 * the beginning of the link-layer header.
780 * It's usually 0, except for ATM LANE, where it's the offset, relative
781 * to the beginning of the raw packet data, of the Ethernet header, and
782 * for Ethernet with various additional information.
784 static u_int off_mac
;
787 * This is the offset of the beginning of the MAC-layer payload,
788 * from the beginning of the raw packet data.
790 * I.e., it's the sum of the length of the link-layer header (without,
791 * for example, any 802.2 LLC header, so it's the MAC-layer
792 * portion of that header), plus any prefix preceding the
795 static u_int off_macpl
;
798 * This is 1 if the offset of the beginning of the MAC-layer payload
799 * from the beginning of the link-layer header is variable-length.
801 static int off_macpl_is_variable
;
804 * If the link layer has variable_length headers, "reg_off_macpl"
805 * is the register number for a register containing the length of the
806 * link-layer header plus the length of any variable-length header
807 * preceding the link-layer header. Otherwise, "reg_off_macpl"
810 static int reg_off_macpl
;
813 * "off_linktype" is the offset to information in the link-layer header
814 * giving the packet type. This offset is relative to the beginning
815 * of the link-layer header (i.e., it doesn't include off_ll).
817 * For Ethernet, it's the offset of the Ethernet type field.
819 * For link-layer types that always use 802.2 headers, it's the
820 * offset of the LLC header.
822 * For PPP, it's the offset of the PPP type field.
824 * For Cisco HDLC, it's the offset of the CHDLC type field.
826 * For BSD loopback, it's the offset of the AF_ value.
828 * For Linux cooked sockets, it's the offset of the type field.
830 * It's set to -1 for no encapsulation, in which case, IP is assumed.
832 static u_int off_linktype
;
835 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
836 * checks to check the PPP header, assumed to follow a LAN-style link-
837 * layer header and a PPPoE session header.
839 static int is_pppoes
= 0;
842 * TRUE if the link layer includes an ATM pseudo-header.
844 static int is_atm
= 0;
847 * TRUE if "lane" appeared in the filter; it causes us to generate
848 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
850 static int is_lane
= 0;
853 * These are offsets for the ATM pseudo-header.
855 static u_int off_vpi
;
856 static u_int off_vci
;
857 static u_int off_proto
;
860 * These are offsets for the MTP2 fields.
865 * These are offsets for the MTP3 fields.
867 static u_int off_sio
;
868 static u_int off_opc
;
869 static u_int off_dpc
;
870 static u_int off_sls
;
873 * This is the offset of the first byte after the ATM pseudo_header,
874 * or -1 if there is no ATM pseudo-header.
876 static u_int off_payload
;
879 * These are offsets to the beginning of the network-layer header.
880 * They are relative to the beginning of the MAC-layer payload (i.e.,
881 * they don't include off_ll or off_macpl).
883 * If the link layer never uses 802.2 LLC:
885 * "off_nl" and "off_nl_nosnap" are the same.
887 * If the link layer always uses 802.2 LLC:
889 * "off_nl" is the offset if there's a SNAP header following
892 * "off_nl_nosnap" is the offset if there's no SNAP header.
894 * If the link layer is Ethernet:
896 * "off_nl" is the offset if the packet is an Ethernet II packet
897 * (we assume no 802.3+802.2+SNAP);
899 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
900 * with an 802.2 header following it.
903 static u_int off_nl_nosnap
;
911 linktype
= pcap_datalink(p
);
913 pcap_fddipad
= p
->fddipad
;
917 * Assume it's not raw ATM with a pseudo-header, for now.
928 * And that we're not doing PPPoE.
933 * And assume we're not doing SS7.
942 * Also assume it's not 802.11.
946 off_macpl_is_variable
= 0;
950 label_stack_depth
= 0;
960 off_nl
= 0; /* XXX in reality, variable! */
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
964 case DLT_ARCNET_LINUX
:
967 off_nl
= 0; /* XXX in reality, variable! */
968 off_nl_nosnap
= 0; /* no 802.2 LLC */
973 off_macpl
= 14; /* Ethernet header length */
974 off_nl
= 0; /* Ethernet II */
975 off_nl_nosnap
= 3; /* 802.3+802.2 */
980 * SLIP doesn't have a link level type. The 16 byte
981 * header is hacked into our SLIP driver.
986 off_nl_nosnap
= 0; /* no 802.2 LLC */
990 /* XXX this may be the same as the DLT_PPP_BSDOS case */
995 off_nl_nosnap
= 0; /* no 802.2 LLC */
1003 off_nl_nosnap
= 0; /* no 802.2 LLC */
1010 off_nl_nosnap
= 0; /* no 802.2 LLC */
1015 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1016 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1020 off_nl_nosnap
= 0; /* no 802.2 LLC */
1025 * This does no include the Ethernet header, and
1026 * only covers session state.
1031 off_nl_nosnap
= 0; /* no 802.2 LLC */
1038 off_nl_nosnap
= 0; /* no 802.2 LLC */
1043 * FDDI doesn't really have a link-level type field.
1044 * We set "off_linktype" to the offset of the LLC header.
1046 * To check for Ethernet types, we assume that SSAP = SNAP
1047 * is being used and pick out the encapsulated Ethernet type.
1048 * XXX - should we generate code to check for SNAP?
1052 off_linktype
+= pcap_fddipad
;
1054 off_macpl
= 13; /* FDDI MAC header length */
1056 off_macpl
+= pcap_fddipad
;
1058 off_nl
= 8; /* 802.2+SNAP */
1059 off_nl_nosnap
= 3; /* 802.2 */
1064 * Token Ring doesn't really have a link-level type field.
1065 * We set "off_linktype" to the offset of the LLC header.
1067 * To check for Ethernet types, we assume that SSAP = SNAP
1068 * is being used and pick out the encapsulated Ethernet type.
1069 * XXX - should we generate code to check for SNAP?
1071 * XXX - the header is actually variable-length.
1072 * Some various Linux patched versions gave 38
1073 * as "off_linktype" and 40 as "off_nl"; however,
1074 * if a token ring packet has *no* routing
1075 * information, i.e. is not source-routed, the correct
1076 * values are 20 and 22, as they are in the vanilla code.
1078 * A packet is source-routed iff the uppermost bit
1079 * of the first byte of the source address, at an
1080 * offset of 8, has the uppermost bit set. If the
1081 * packet is source-routed, the total number of bytes
1082 * of routing information is 2 plus bits 0x1F00 of
1083 * the 16-bit value at an offset of 14 (shifted right
1084 * 8 - figure out which byte that is).
1087 off_macpl
= 14; /* Token Ring MAC header length */
1088 off_nl
= 8; /* 802.2+SNAP */
1089 off_nl_nosnap
= 3; /* 802.2 */
1092 case DLT_IEEE802_11
:
1093 case DLT_PRISM_HEADER
:
1094 case DLT_IEEE802_11_RADIO_AVS
:
1095 case DLT_IEEE802_11_RADIO
:
1097 * 802.11 doesn't really have a link-level type field.
1098 * We set "off_linktype" to the offset of the LLC header.
1100 * To check for Ethernet types, we assume that SSAP = SNAP
1101 * is being used and pick out the encapsulated Ethernet type.
1102 * XXX - should we generate code to check for SNAP?
1104 * We also handle variable-length radio headers here.
1105 * The Prism header is in theory variable-length, but in
1106 * practice it's always 144 bytes long. However, some
1107 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1108 * sometimes or always supply an AVS header, so we
1109 * have to check whether the radio header is a Prism
1110 * header or an AVS header, so, in practice, it's
1114 off_macpl
= 0; /* link-layer header is variable-length */
1115 off_macpl_is_variable
= 1;
1116 off_nl
= 8; /* 802.2+SNAP */
1117 off_nl_nosnap
= 3; /* 802.2 */
1122 * At the moment we treat PPI the same way that we treat
1123 * normal Radiotap encoded packets. The difference is in
1124 * the function that generates the code at the beginning
1125 * to compute the header length. Since this code generator
1126 * of PPI supports bare 802.11 encapsulation only (i.e.
1127 * the encapsulated DLT should be DLT_IEEE802_11) we
1128 * generate code to check for this too.
1131 off_macpl
= 0; /* link-layer header is variable-length */
1132 off_macpl_is_variable
= 1;
1133 off_nl
= 8; /* 802.2+SNAP */
1134 off_nl_nosnap
= 3; /* 802.2 */
1137 case DLT_ATM_RFC1483
:
1138 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1140 * assume routed, non-ISO PDUs
1141 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1143 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1144 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1145 * latter would presumably be treated the way PPPoE
1146 * should be, so you can do "pppoe and udp port 2049"
1147 * or "pppoa and tcp port 80" and have it check for
1148 * PPPo{A,E} and a PPP protocol of IP and....
1151 off_macpl
= 0; /* packet begins with LLC header */
1152 off_nl
= 8; /* 802.2+SNAP */
1153 off_nl_nosnap
= 3; /* 802.2 */
1158 * Full Frontal ATM; you get AALn PDUs with an ATM
1162 off_vpi
= SUNATM_VPI_POS
;
1163 off_vci
= SUNATM_VCI_POS
;
1164 off_proto
= PROTO_POS
;
1165 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1166 off_payload
= SUNATM_PKT_BEGIN_POS
;
1167 off_linktype
= off_payload
;
1168 off_macpl
= off_payload
; /* if LLC-encapsulated */
1169 off_nl
= 8; /* 802.2+SNAP */
1170 off_nl_nosnap
= 3; /* 802.2 */
1179 off_nl_nosnap
= 0; /* no 802.2 LLC */
1182 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1186 off_nl_nosnap
= 0; /* no 802.2 LLC */
1191 * LocalTalk does have a 1-byte type field in the LLAP header,
1192 * but really it just indicates whether there is a "short" or
1193 * "long" DDP packet following.
1198 off_nl_nosnap
= 0; /* no 802.2 LLC */
1201 case DLT_IP_OVER_FC
:
1203 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1204 * link-level type field. We set "off_linktype" to the
1205 * offset of the LLC header.
1207 * To check for Ethernet types, we assume that SSAP = SNAP
1208 * is being used and pick out the encapsulated Ethernet type.
1209 * XXX - should we generate code to check for SNAP? RFC
1210 * 2625 says SNAP should be used.
1214 off_nl
= 8; /* 802.2+SNAP */
1215 off_nl_nosnap
= 3; /* 802.2 */
1220 * XXX - we should set this to handle SNAP-encapsulated
1221 * frames (NLPID of 0x80).
1226 off_nl_nosnap
= 0; /* no 802.2 LLC */
1230 * the only BPF-interesting FRF.16 frames are non-control frames;
1231 * Frame Relay has a variable length link-layer
1232 * so lets start with offset 4 for now and increments later on (FIXME);
1238 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1241 case DLT_APPLE_IP_OVER_IEEE1394
:
1245 off_nl_nosnap
= 0; /* no 802.2 LLC */
1248 case DLT_SYMANTEC_FIREWALL
:
1251 off_nl
= 0; /* Ethernet II */
1252 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1255 #ifdef HAVE_NET_PFVAR_H
1258 off_macpl
= PFLOG_HDRLEN
;
1260 off_nl_nosnap
= 0; /* no 802.2 LLC */
1264 case DLT_JUNIPER_MFR
:
1265 case DLT_JUNIPER_MLFR
:
1266 case DLT_JUNIPER_MLPPP
:
1267 case DLT_JUNIPER_PPP
:
1268 case DLT_JUNIPER_CHDLC
:
1269 case DLT_JUNIPER_FRELAY
:
1273 off_nl_nosnap
= -1; /* no 802.2 LLC */
1276 case DLT_JUNIPER_ATM1
:
1277 off_linktype
= 4; /* in reality variable between 4-8 */
1278 off_macpl
= 4; /* in reality variable between 4-8 */
1283 case DLT_JUNIPER_ATM2
:
1284 off_linktype
= 8; /* in reality variable between 8-12 */
1285 off_macpl
= 8; /* in reality variable between 8-12 */
1290 /* frames captured on a Juniper PPPoE service PIC
1291 * contain raw ethernet frames */
1292 case DLT_JUNIPER_PPPOE
:
1293 case DLT_JUNIPER_ETHER
:
1296 off_nl
= 18; /* Ethernet II */
1297 off_nl_nosnap
= 21; /* 802.3+802.2 */
1300 case DLT_JUNIPER_PPPOE_ATM
:
1304 off_nl_nosnap
= -1; /* no 802.2 LLC */
1307 case DLT_JUNIPER_GGSN
:
1311 off_nl_nosnap
= -1; /* no 802.2 LLC */
1314 case DLT_JUNIPER_ES
:
1316 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1317 off_nl
= -1; /* not really a network layer but raw IP addresses */
1318 off_nl_nosnap
= -1; /* no 802.2 LLC */
1321 case DLT_JUNIPER_MONITOR
:
1324 off_nl
= 0; /* raw IP/IP6 header */
1325 off_nl_nosnap
= -1; /* no 802.2 LLC */
1328 case DLT_JUNIPER_SERVICES
:
1330 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1331 off_nl
= -1; /* L3 proto location dep. on cookie type */
1332 off_nl_nosnap
= -1; /* no 802.2 LLC */
1335 case DLT_JUNIPER_VP
:
1342 case DLT_JUNIPER_ST
:
1349 case DLT_JUNIPER_ISM
:
1356 case DLT_JUNIPER_VS
:
1357 case DLT_JUNIPER_SRX_E2E
:
1358 case DLT_JUNIPER_FIBRECHANNEL
:
1359 case DLT_JUNIPER_ATM_CEMIC
:
1378 case DLT_MTP2_WITH_PHDR
:
1411 * Currently, only raw "link[N:M]" filtering is supported.
1413 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1415 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1416 off_nl_nosnap
= -1; /* no 802.2 LLC */
1417 off_mac
= 1; /* step over the kiss length byte */
1422 off_macpl
= 24; /* ipnet header length */
1427 case DLT_NETANALYZER
:
1428 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1429 off_linktype
= 16; /* includes 4-byte pseudo-header */
1430 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1431 off_nl
= 0; /* Ethernet II */
1432 off_nl_nosnap
= 3; /* 802.3+802.2 */
1435 case DLT_NETANALYZER_TRANSPARENT
:
1436 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1437 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1438 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1439 off_nl
= 0; /* Ethernet II */
1440 off_nl_nosnap
= 3; /* 802.3+802.2 */
1445 * For values in the range in which we've assigned new
1446 * DLT_ values, only raw "link[N:M]" filtering is supported.
1448 if (linktype
>= DLT_MATCHING_MIN
&&
1449 linktype
<= DLT_MATCHING_MAX
) {
1458 bpf_error("unknown data link type %d", linktype
);
1463 * Load a value relative to the beginning of the link-layer header.
1464 * The link-layer header doesn't necessarily begin at the beginning
1465 * of the packet data; there might be a variable-length prefix containing
1466 * radio information.
1468 static struct slist
*
1469 gen_load_llrel(offset
, size
)
1472 struct slist
*s
, *s2
;
1474 s
= gen_llprefixlen();
1477 * If "s" is non-null, it has code to arrange that the X register
1478 * contains the length of the prefix preceding the link-layer
1481 * Otherwise, the length of the prefix preceding the link-layer
1482 * header is "off_ll".
1486 * There's a variable-length prefix preceding the
1487 * link-layer header. "s" points to a list of statements
1488 * that put the length of that prefix into the X register.
1489 * do an indirect load, to use the X register as an offset.
1491 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1496 * There is no variable-length header preceding the
1497 * link-layer header; add in off_ll, which, if there's
1498 * a fixed-length header preceding the link-layer header,
1499 * is the length of that header.
1501 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1502 s
->s
.k
= offset
+ off_ll
;
1508 * Load a value relative to the beginning of the MAC-layer payload.
1510 static struct slist
*
1511 gen_load_macplrel(offset
, size
)
1514 struct slist
*s
, *s2
;
1516 s
= gen_off_macpl();
1519 * If s is non-null, the offset of the MAC-layer payload is
1520 * variable, and s points to a list of instructions that
1521 * arrange that the X register contains that offset.
1523 * Otherwise, the offset of the MAC-layer payload is constant,
1524 * and is in off_macpl.
1528 * The offset of the MAC-layer payload is in the X
1529 * register. Do an indirect load, to use the X register
1532 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1537 * The offset of the MAC-layer payload is constant,
1538 * and is in off_macpl; load the value at that offset
1539 * plus the specified offset.
1541 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1542 s
->s
.k
= off_macpl
+ offset
;
1548 * Load a value relative to the beginning of the specified header.
1550 static struct slist
*
1551 gen_load_a(offrel
, offset
, size
)
1552 enum e_offrel offrel
;
1555 struct slist
*s
, *s2
;
1560 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1565 s
= gen_load_llrel(offset
, size
);
1569 s
= gen_load_macplrel(offset
, size
);
1573 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1577 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1582 * Load the X register with the length of the IPv4 header
1583 * (plus the offset of the link-layer header, if it's
1584 * preceded by a variable-length header such as a radio
1585 * header), in bytes.
1587 s
= gen_loadx_iphdrlen();
1590 * Load the item at {offset of the MAC-layer payload} +
1591 * {offset, relative to the start of the MAC-layer
1592 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1593 * {specified offset}.
1595 * (If the offset of the MAC-layer payload is variable,
1596 * it's included in the value in the X register, and
1599 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1600 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1605 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1616 * Generate code to load into the X register the sum of the length of
1617 * the IPv4 header and any variable-length header preceding the link-layer
1620 static struct slist
*
1621 gen_loadx_iphdrlen()
1623 struct slist
*s
, *s2
;
1625 s
= gen_off_macpl();
1628 * There's a variable-length prefix preceding the
1629 * link-layer header, or the link-layer header is itself
1630 * variable-length. "s" points to a list of statements
1631 * that put the offset of the MAC-layer payload into
1634 * The 4*([k]&0xf) addressing mode can't be used, as we
1635 * don't have a constant offset, so we have to load the
1636 * value in question into the A register and add to it
1637 * the value from the X register.
1639 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1642 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1645 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1650 * The A register now contains the length of the
1651 * IP header. We need to add to it the offset of
1652 * the MAC-layer payload, which is still in the X
1653 * register, and move the result into the X register.
1655 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1656 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1659 * There is no variable-length header preceding the
1660 * link-layer header, and the link-layer header is
1661 * fixed-length; load the length of the IPv4 header,
1662 * which is at an offset of off_nl from the beginning
1663 * of the MAC-layer payload, and thus at an offset
1664 * of off_mac_pl + off_nl from the beginning of the
1667 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1668 s
->s
.k
= off_macpl
+ off_nl
;
1673 static struct block
*
1680 s
= new_stmt(BPF_LD
|BPF_IMM
);
1682 b
= new_block(JMP(BPF_JEQ
));
1688 static inline struct block
*
1691 return gen_uncond(1);
1694 static inline struct block
*
1697 return gen_uncond(0);
1701 * Byte-swap a 32-bit number.
1702 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1703 * big-endian platforms.)
1705 #define SWAPLONG(y) \
1706 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1709 * Generate code to match a particular packet type.
1711 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1712 * value, if <= ETHERMTU. We use that to determine whether to
1713 * match the type/length field or to check the type/length field for
1714 * a value <= ETHERMTU to see whether it's a type field and then do
1715 * the appropriate test.
1717 static struct block
*
1718 gen_ether_linktype(proto
)
1721 struct block
*b0
, *b1
;
1727 case LLCSAP_NETBEUI
:
1729 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1730 * so we check the DSAP and SSAP.
1732 * LLCSAP_IP checks for IP-over-802.2, rather
1733 * than IP-over-Ethernet or IP-over-SNAP.
1735 * XXX - should we check both the DSAP and the
1736 * SSAP, like this, or should we check just the
1737 * DSAP, as we do for other types <= ETHERMTU
1738 * (i.e., other SAP values)?
1740 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1742 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1743 ((proto
<< 8) | proto
));
1751 * Ethernet_II frames, which are Ethernet
1752 * frames with a frame type of ETHERTYPE_IPX;
1754 * Ethernet_802.3 frames, which are 802.3
1755 * frames (i.e., the type/length field is
1756 * a length field, <= ETHERMTU, rather than
1757 * a type field) with the first two bytes
1758 * after the Ethernet/802.3 header being
1761 * Ethernet_802.2 frames, which are 802.3
1762 * frames with an 802.2 LLC header and
1763 * with the IPX LSAP as the DSAP in the LLC
1766 * Ethernet_SNAP frames, which are 802.3
1767 * frames with an LLC header and a SNAP
1768 * header and with an OUI of 0x000000
1769 * (encapsulated Ethernet) and a protocol
1770 * ID of ETHERTYPE_IPX in the SNAP header.
1772 * XXX - should we generate the same code both
1773 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1777 * This generates code to check both for the
1778 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1780 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1781 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1785 * Now we add code to check for SNAP frames with
1786 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1788 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1792 * Now we generate code to check for 802.3
1793 * frames in general.
1795 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1799 * Now add the check for 802.3 frames before the
1800 * check for Ethernet_802.2 and Ethernet_802.3,
1801 * as those checks should only be done on 802.3
1802 * frames, not on Ethernet frames.
1807 * Now add the check for Ethernet_II frames, and
1808 * do that before checking for the other frame
1811 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1812 (bpf_int32
)ETHERTYPE_IPX
);
1816 case ETHERTYPE_ATALK
:
1817 case ETHERTYPE_AARP
:
1819 * EtherTalk (AppleTalk protocols on Ethernet link
1820 * layer) may use 802.2 encapsulation.
1824 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1825 * we check for an Ethernet type field less than
1826 * 1500, which means it's an 802.3 length field.
1828 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1832 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1833 * SNAP packets with an organization code of
1834 * 0x080007 (Apple, for Appletalk) and a protocol
1835 * type of ETHERTYPE_ATALK (Appletalk).
1837 * 802.2-encapsulated ETHERTYPE_AARP packets are
1838 * SNAP packets with an organization code of
1839 * 0x000000 (encapsulated Ethernet) and a protocol
1840 * type of ETHERTYPE_AARP (Appletalk ARP).
1842 if (proto
== ETHERTYPE_ATALK
)
1843 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1844 else /* proto == ETHERTYPE_AARP */
1845 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1849 * Check for Ethernet encapsulation (Ethertalk
1850 * phase 1?); we just check for the Ethernet
1853 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1859 if (proto
<= ETHERMTU
) {
1861 * This is an LLC SAP value, so the frames
1862 * that match would be 802.2 frames.
1863 * Check that the frame is an 802.2 frame
1864 * (i.e., that the length/type field is
1865 * a length field, <= ETHERMTU) and
1866 * then check the DSAP.
1868 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1870 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1876 * This is an Ethernet type, so compare
1877 * the length/type field with it (if
1878 * the frame is an 802.2 frame, the length
1879 * field will be <= ETHERMTU, and, as
1880 * "proto" is > ETHERMTU, this test
1881 * will fail and the frame won't match,
1882 * which is what we want).
1884 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1891 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1892 * or IPv6 then we have an error.
1894 static struct block
*
1895 gen_ipnet_linktype(proto
)
1901 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1902 (bpf_int32
)IPH_AF_INET
);
1905 case ETHERTYPE_IPV6
:
1906 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1907 (bpf_int32
)IPH_AF_INET6
);
1918 * Generate code to match a particular packet type.
1920 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1921 * value, if <= ETHERMTU. We use that to determine whether to
1922 * match the type field or to check the type field for the special
1923 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1925 static struct block
*
1926 gen_linux_sll_linktype(proto
)
1929 struct block
*b0
, *b1
;
1935 case LLCSAP_NETBEUI
:
1937 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1938 * so we check the DSAP and SSAP.
1940 * LLCSAP_IP checks for IP-over-802.2, rather
1941 * than IP-over-Ethernet or IP-over-SNAP.
1943 * XXX - should we check both the DSAP and the
1944 * SSAP, like this, or should we check just the
1945 * DSAP, as we do for other types <= ETHERMTU
1946 * (i.e., other SAP values)?
1948 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1949 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1950 ((proto
<< 8) | proto
));
1956 * Ethernet_II frames, which are Ethernet
1957 * frames with a frame type of ETHERTYPE_IPX;
1959 * Ethernet_802.3 frames, which have a frame
1960 * type of LINUX_SLL_P_802_3;
1962 * Ethernet_802.2 frames, which are 802.3
1963 * frames with an 802.2 LLC header (i.e, have
1964 * a frame type of LINUX_SLL_P_802_2) and
1965 * with the IPX LSAP as the DSAP in the LLC
1968 * Ethernet_SNAP frames, which are 802.3
1969 * frames with an LLC header and a SNAP
1970 * header and with an OUI of 0x000000
1971 * (encapsulated Ethernet) and a protocol
1972 * ID of ETHERTYPE_IPX in the SNAP header.
1974 * First, do the checks on LINUX_SLL_P_802_2
1975 * frames; generate the check for either
1976 * Ethernet_802.2 or Ethernet_SNAP frames, and
1977 * then put a check for LINUX_SLL_P_802_2 frames
1980 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1981 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1983 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1987 * Now check for 802.3 frames and OR that with
1988 * the previous test.
1990 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1994 * Now add the check for Ethernet_II frames, and
1995 * do that before checking for the other frame
1998 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1999 (bpf_int32
)ETHERTYPE_IPX
);
2003 case ETHERTYPE_ATALK
:
2004 case ETHERTYPE_AARP
:
2006 * EtherTalk (AppleTalk protocols on Ethernet link
2007 * layer) may use 802.2 encapsulation.
2011 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2012 * we check for the 802.2 protocol type in the
2013 * "Ethernet type" field.
2015 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2018 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2019 * SNAP packets with an organization code of
2020 * 0x080007 (Apple, for Appletalk) and a protocol
2021 * type of ETHERTYPE_ATALK (Appletalk).
2023 * 802.2-encapsulated ETHERTYPE_AARP packets are
2024 * SNAP packets with an organization code of
2025 * 0x000000 (encapsulated Ethernet) and a protocol
2026 * type of ETHERTYPE_AARP (Appletalk ARP).
2028 if (proto
== ETHERTYPE_ATALK
)
2029 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2030 else /* proto == ETHERTYPE_AARP */
2031 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2035 * Check for Ethernet encapsulation (Ethertalk
2036 * phase 1?); we just check for the Ethernet
2039 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2045 if (proto
<= ETHERMTU
) {
2047 * This is an LLC SAP value, so the frames
2048 * that match would be 802.2 frames.
2049 * Check for the 802.2 protocol type
2050 * in the "Ethernet type" field, and
2051 * then check the DSAP.
2053 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2055 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2061 * This is an Ethernet type, so compare
2062 * the length/type field with it (if
2063 * the frame is an 802.2 frame, the length
2064 * field will be <= ETHERMTU, and, as
2065 * "proto" is > ETHERMTU, this test
2066 * will fail and the frame won't match,
2067 * which is what we want).
2069 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2075 static struct slist
*
2076 gen_load_prism_llprefixlen()
2078 struct slist
*s1
, *s2
;
2079 struct slist
*sjeq_avs_cookie
;
2080 struct slist
*sjcommon
;
2083 * This code is not compatible with the optimizer, as
2084 * we are generating jmp instructions within a normal
2085 * slist of instructions
2090 * Generate code to load the length of the radio header into
2091 * the register assigned to hold that length, if one has been
2092 * assigned. (If one hasn't been assigned, no code we've
2093 * generated uses that prefix, so we don't need to generate any
2096 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2097 * or always use the AVS header rather than the Prism header.
2098 * We load a 4-byte big-endian value at the beginning of the
2099 * raw packet data, and see whether, when masked with 0xFFFFF000,
2100 * it's equal to 0x80211000. If so, that indicates that it's
2101 * an AVS header (the masked-out bits are the version number).
2102 * Otherwise, it's a Prism header.
2104 * XXX - the Prism header is also, in theory, variable-length,
2105 * but no known software generates headers that aren't 144
2108 if (reg_off_ll
!= -1) {
2112 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2116 * AND it with 0xFFFFF000.
2118 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2119 s2
->s
.k
= 0xFFFFF000;
2123 * Compare with 0x80211000.
2125 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2126 sjeq_avs_cookie
->s
.k
= 0x80211000;
2127 sappend(s1
, sjeq_avs_cookie
);
2132 * The 4 bytes at an offset of 4 from the beginning of
2133 * the AVS header are the length of the AVS header.
2134 * That field is big-endian.
2136 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2139 sjeq_avs_cookie
->s
.jt
= s2
;
2142 * Now jump to the code to allocate a register
2143 * into which to save the header length and
2144 * store the length there. (The "jump always"
2145 * instruction needs to have the k field set;
2146 * it's added to the PC, so, as we're jumping
2147 * over a single instruction, it should be 1.)
2149 sjcommon
= new_stmt(JMP(BPF_JA
));
2151 sappend(s1
, sjcommon
);
2154 * Now for the code that handles the Prism header.
2155 * Just load the length of the Prism header (144)
2156 * into the A register. Have the test for an AVS
2157 * header branch here if we don't have an AVS header.
2159 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2162 sjeq_avs_cookie
->s
.jf
= s2
;
2165 * Now allocate a register to hold that value and store
2166 * it. The code for the AVS header will jump here after
2167 * loading the length of the AVS header.
2169 s2
= new_stmt(BPF_ST
);
2170 s2
->s
.k
= reg_off_ll
;
2172 sjcommon
->s
.jf
= s2
;
2175 * Now move it into the X register.
2177 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2185 static struct slist
*
2186 gen_load_avs_llprefixlen()
2188 struct slist
*s1
, *s2
;
2191 * Generate code to load the length of the AVS header into
2192 * the register assigned to hold that length, if one has been
2193 * assigned. (If one hasn't been assigned, no code we've
2194 * generated uses that prefix, so we don't need to generate any
2197 if (reg_off_ll
!= -1) {
2199 * The 4 bytes at an offset of 4 from the beginning of
2200 * the AVS header are the length of the AVS header.
2201 * That field is big-endian.
2203 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2207 * Now allocate a register to hold that value and store
2210 s2
= new_stmt(BPF_ST
);
2211 s2
->s
.k
= reg_off_ll
;
2215 * Now move it into the X register.
2217 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2225 static struct slist
*
2226 gen_load_radiotap_llprefixlen()
2228 struct slist
*s1
, *s2
;
2231 * Generate code to load the length of the radiotap header into
2232 * the register assigned to hold that length, if one has been
2233 * assigned. (If one hasn't been assigned, no code we've
2234 * generated uses that prefix, so we don't need to generate any
2237 if (reg_off_ll
!= -1) {
2239 * The 2 bytes at offsets of 2 and 3 from the beginning
2240 * of the radiotap header are the length of the radiotap
2241 * header; unfortunately, it's little-endian, so we have
2242 * to load it a byte at a time and construct the value.
2246 * Load the high-order byte, at an offset of 3, shift it
2247 * left a byte, and put the result in the X register.
2249 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2251 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2254 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2258 * Load the next byte, at an offset of 2, and OR the
2259 * value from the X register into it.
2261 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2264 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2268 * Now allocate a register to hold that value and store
2271 s2
= new_stmt(BPF_ST
);
2272 s2
->s
.k
= reg_off_ll
;
2276 * Now move it into the X register.
2278 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2287 * At the moment we treat PPI as normal Radiotap encoded
2288 * packets. The difference is in the function that generates
2289 * the code at the beginning to compute the header length.
2290 * Since this code generator of PPI supports bare 802.11
2291 * encapsulation only (i.e. the encapsulated DLT should be
2292 * DLT_IEEE802_11) we generate code to check for this too;
2293 * that's done in finish_parse().
2295 static struct slist
*
2296 gen_load_ppi_llprefixlen()
2298 struct slist
*s1
, *s2
;
2301 * Generate code to load the length of the radiotap header
2302 * into the register assigned to hold that length, if one has
2305 if (reg_off_ll
!= -1) {
2307 * The 2 bytes at offsets of 2 and 3 from the beginning
2308 * of the radiotap header are the length of the radiotap
2309 * header; unfortunately, it's little-endian, so we have
2310 * to load it a byte at a time and construct the value.
2314 * Load the high-order byte, at an offset of 3, shift it
2315 * left a byte, and put the result in the X register.
2317 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2319 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2322 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2326 * Load the next byte, at an offset of 2, and OR the
2327 * value from the X register into it.
2329 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2332 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2336 * Now allocate a register to hold that value and store
2339 s2
= new_stmt(BPF_ST
);
2340 s2
->s
.k
= reg_off_ll
;
2344 * Now move it into the X register.
2346 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2355 * Load a value relative to the beginning of the link-layer header after the 802.11
2356 * header, i.e. LLC_SNAP.
2357 * The link-layer header doesn't necessarily begin at the beginning
2358 * of the packet data; there might be a variable-length prefix containing
2359 * radio information.
2361 static struct slist
*
2362 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2365 struct slist
*sjset_data_frame_1
;
2366 struct slist
*sjset_data_frame_2
;
2367 struct slist
*sjset_qos
;
2368 struct slist
*sjset_radiotap_flags
;
2369 struct slist
*sjset_radiotap_tsft
;
2370 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2371 struct slist
*s_roundup
;
2373 if (reg_off_macpl
== -1) {
2375 * No register has been assigned to the offset of
2376 * the MAC-layer payload, which means nobody needs
2377 * it; don't bother computing it - just return
2378 * what we already have.
2384 * This code is not compatible with the optimizer, as
2385 * we are generating jmp instructions within a normal
2386 * slist of instructions
2391 * If "s" is non-null, it has code to arrange that the X register
2392 * contains the length of the prefix preceding the link-layer
2395 * Otherwise, the length of the prefix preceding the link-layer
2396 * header is "off_ll".
2400 * There is no variable-length header preceding the
2401 * link-layer header.
2403 * Load the length of the fixed-length prefix preceding
2404 * the link-layer header (if any) into the X register,
2405 * and store it in the reg_off_macpl register.
2406 * That length is off_ll.
2408 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2413 * The X register contains the offset of the beginning of the
2414 * link-layer header; add 24, which is the minimum length
2415 * of the MAC header for a data frame, to that, and store it
2416 * in reg_off_macpl, and then load the Frame Control field,
2417 * which is at the offset in the X register, with an indexed load.
2419 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2421 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2424 s2
= new_stmt(BPF_ST
);
2425 s2
->s
.k
= reg_off_macpl
;
2428 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2433 * Check the Frame Control field to see if this is a data frame;
2434 * a data frame has the 0x08 bit (b3) in that field set and the
2435 * 0x04 bit (b2) clear.
2437 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2438 sjset_data_frame_1
->s
.k
= 0x08;
2439 sappend(s
, sjset_data_frame_1
);
2442 * If b3 is set, test b2, otherwise go to the first statement of
2443 * the rest of the program.
2445 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2446 sjset_data_frame_2
->s
.k
= 0x04;
2447 sappend(s
, sjset_data_frame_2
);
2448 sjset_data_frame_1
->s
.jf
= snext
;
2451 * If b2 is not set, this is a data frame; test the QoS bit.
2452 * Otherwise, go to the first statement of the rest of the
2455 sjset_data_frame_2
->s
.jt
= snext
;
2456 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2457 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2458 sappend(s
, sjset_qos
);
2461 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2463 * Otherwise, go to the first statement of the rest of the
2466 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2467 s2
->s
.k
= reg_off_macpl
;
2469 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2472 s2
= new_stmt(BPF_ST
);
2473 s2
->s
.k
= reg_off_macpl
;
2477 * If we have a radiotap header, look at it to see whether
2478 * there's Atheros padding between the MAC-layer header
2481 * Note: all of the fields in the radiotap header are
2482 * little-endian, so we byte-swap all of the values
2483 * we test against, as they will be loaded as big-endian
2486 if (linktype
== DLT_IEEE802_11_RADIO
) {
2488 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2489 * in the presence flag?
2491 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2495 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2496 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2497 sappend(s
, sjset_radiotap_flags
);
2500 * If not, skip all of this.
2502 sjset_radiotap_flags
->s
.jf
= snext
;
2505 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2507 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2508 new_stmt(JMP(BPF_JSET
));
2509 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2510 sappend(s
, sjset_radiotap_tsft
);
2513 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2514 * at an offset of 16 from the beginning of the raw packet
2515 * data (8 bytes for the radiotap header and 8 bytes for
2518 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2521 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2525 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2526 sjset_tsft_datapad
->s
.k
= 0x20;
2527 sappend(s
, sjset_tsft_datapad
);
2530 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2531 * at an offset of 8 from the beginning of the raw packet
2532 * data (8 bytes for the radiotap header).
2534 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2537 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2541 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2542 sjset_notsft_datapad
->s
.k
= 0x20;
2543 sappend(s
, sjset_notsft_datapad
);
2546 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2547 * set, round the length of the 802.11 header to
2548 * a multiple of 4. Do that by adding 3 and then
2549 * dividing by and multiplying by 4, which we do by
2552 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2553 s_roundup
->s
.k
= reg_off_macpl
;
2554 sappend(s
, s_roundup
);
2555 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2558 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2561 s2
= new_stmt(BPF_ST
);
2562 s2
->s
.k
= reg_off_macpl
;
2565 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2566 sjset_tsft_datapad
->s
.jf
= snext
;
2567 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2568 sjset_notsft_datapad
->s
.jf
= snext
;
2570 sjset_qos
->s
.jf
= snext
;
2576 insert_compute_vloffsets(b
)
2582 * For link-layer types that have a variable-length header
2583 * preceding the link-layer header, generate code to load
2584 * the offset of the link-layer header into the register
2585 * assigned to that offset, if any.
2589 case DLT_PRISM_HEADER
:
2590 s
= gen_load_prism_llprefixlen();
2593 case DLT_IEEE802_11_RADIO_AVS
:
2594 s
= gen_load_avs_llprefixlen();
2597 case DLT_IEEE802_11_RADIO
:
2598 s
= gen_load_radiotap_llprefixlen();
2602 s
= gen_load_ppi_llprefixlen();
2611 * For link-layer types that have a variable-length link-layer
2612 * header, generate code to load the offset of the MAC-layer
2613 * payload into the register assigned to that offset, if any.
2617 case DLT_IEEE802_11
:
2618 case DLT_PRISM_HEADER
:
2619 case DLT_IEEE802_11_RADIO_AVS
:
2620 case DLT_IEEE802_11_RADIO
:
2622 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2627 * If we have any offset-loading code, append all the
2628 * existing statements in the block to those statements,
2629 * and make the resulting list the list of statements
2633 sappend(s
, b
->stmts
);
2638 static struct block
*
2639 gen_ppi_dlt_check(void)
2641 struct slist
*s_load_dlt
;
2644 if (linktype
== DLT_PPI
)
2646 /* Create the statements that check for the DLT
2648 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2649 s_load_dlt
->s
.k
= 4;
2651 b
= new_block(JMP(BPF_JEQ
));
2653 b
->stmts
= s_load_dlt
;
2654 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2664 static struct slist
*
2665 gen_prism_llprefixlen(void)
2669 if (reg_off_ll
== -1) {
2671 * We haven't yet assigned a register for the length
2672 * of the radio header; allocate one.
2674 reg_off_ll
= alloc_reg();
2678 * Load the register containing the radio length
2679 * into the X register.
2681 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2682 s
->s
.k
= reg_off_ll
;
2686 static struct slist
*
2687 gen_avs_llprefixlen(void)
2691 if (reg_off_ll
== -1) {
2693 * We haven't yet assigned a register for the length
2694 * of the AVS header; allocate one.
2696 reg_off_ll
= alloc_reg();
2700 * Load the register containing the AVS length
2701 * into the X register.
2703 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2704 s
->s
.k
= reg_off_ll
;
2708 static struct slist
*
2709 gen_radiotap_llprefixlen(void)
2713 if (reg_off_ll
== -1) {
2715 * We haven't yet assigned a register for the length
2716 * of the radiotap header; allocate one.
2718 reg_off_ll
= alloc_reg();
2722 * Load the register containing the radiotap length
2723 * into the X register.
2725 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2726 s
->s
.k
= reg_off_ll
;
2731 * At the moment we treat PPI as normal Radiotap encoded
2732 * packets. The difference is in the function that generates
2733 * the code at the beginning to compute the header length.
2734 * Since this code generator of PPI supports bare 802.11
2735 * encapsulation only (i.e. the encapsulated DLT should be
2736 * DLT_IEEE802_11) we generate code to check for this too.
2738 static struct slist
*
2739 gen_ppi_llprefixlen(void)
2743 if (reg_off_ll
== -1) {
2745 * We haven't yet assigned a register for the length
2746 * of the radiotap header; allocate one.
2748 reg_off_ll
= alloc_reg();
2752 * Load the register containing the PPI length
2753 * into the X register.
2755 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2756 s
->s
.k
= reg_off_ll
;
2761 * Generate code to compute the link-layer header length, if necessary,
2762 * putting it into the X register, and to return either a pointer to a
2763 * "struct slist" for the list of statements in that code, or NULL if
2764 * no code is necessary.
2766 static struct slist
*
2767 gen_llprefixlen(void)
2771 case DLT_PRISM_HEADER
:
2772 return gen_prism_llprefixlen();
2774 case DLT_IEEE802_11_RADIO_AVS
:
2775 return gen_avs_llprefixlen();
2777 case DLT_IEEE802_11_RADIO
:
2778 return gen_radiotap_llprefixlen();
2781 return gen_ppi_llprefixlen();
2789 * Generate code to load the register containing the offset of the
2790 * MAC-layer payload into the X register; if no register for that offset
2791 * has been allocated, allocate it first.
2793 static struct slist
*
2798 if (off_macpl_is_variable
) {
2799 if (reg_off_macpl
== -1) {
2801 * We haven't yet assigned a register for the offset
2802 * of the MAC-layer payload; allocate one.
2804 reg_off_macpl
= alloc_reg();
2808 * Load the register containing the offset of the MAC-layer
2809 * payload into the X register.
2811 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2812 s
->s
.k
= reg_off_macpl
;
2816 * That offset isn't variable, so we don't need to
2817 * generate any code.
2824 * Map an Ethernet type to the equivalent PPP type.
2827 ethertype_to_ppptype(proto
)
2836 case ETHERTYPE_IPV6
:
2844 case ETHERTYPE_ATALK
:
2858 * I'm assuming the "Bridging PDU"s that go
2859 * over PPP are Spanning Tree Protocol
2873 * Generate code to match a particular packet type by matching the
2874 * link-layer type field or fields in the 802.2 LLC header.
2876 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2877 * value, if <= ETHERMTU.
2879 static struct block
*
2883 struct block
*b0
, *b1
, *b2
;
2885 /* are we checking MPLS-encapsulated packets? */
2886 if (label_stack_depth
> 0) {
2890 /* FIXME add other L3 proto IDs */
2891 return gen_mpls_linktype(Q_IP
);
2893 case ETHERTYPE_IPV6
:
2895 /* FIXME add other L3 proto IDs */
2896 return gen_mpls_linktype(Q_IPV6
);
2899 bpf_error("unsupported protocol over mpls");
2905 * Are we testing PPPoE packets?
2909 * The PPPoE session header is part of the
2910 * MAC-layer payload, so all references
2911 * should be relative to the beginning of
2916 * We use Ethernet protocol types inside libpcap;
2917 * map them to the corresponding PPP protocol types.
2919 proto
= ethertype_to_ppptype(proto
);
2920 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2926 case DLT_NETANALYZER
:
2927 case DLT_NETANALYZER_TRANSPARENT
:
2928 return gen_ether_linktype(proto
);
2936 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2940 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2947 case DLT_IEEE802_11
:
2948 case DLT_PRISM_HEADER
:
2949 case DLT_IEEE802_11_RADIO_AVS
:
2950 case DLT_IEEE802_11_RADIO
:
2953 * Check that we have a data frame.
2955 b0
= gen_check_802_11_data_frame();
2958 * Now check for the specified link-layer type.
2960 b1
= gen_llc_linktype(proto
);
2968 * XXX - check for asynchronous frames, as per RFC 1103.
2970 return gen_llc_linktype(proto
);
2976 * XXX - check for LLC PDUs, as per IEEE 802.5.
2978 return gen_llc_linktype(proto
);
2982 case DLT_ATM_RFC1483
:
2984 case DLT_IP_OVER_FC
:
2985 return gen_llc_linktype(proto
);
2991 * If "is_lane" is set, check for a LANE-encapsulated
2992 * version of this protocol, otherwise check for an
2993 * LLC-encapsulated version of this protocol.
2995 * We assume LANE means Ethernet, not Token Ring.
2999 * Check that the packet doesn't begin with an
3000 * LE Control marker. (We've already generated
3003 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3008 * Now generate an Ethernet test.
3010 b1
= gen_ether_linktype(proto
);
3015 * Check for LLC encapsulation and then check the
3018 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3019 b1
= gen_llc_linktype(proto
);
3027 return gen_linux_sll_linktype(proto
);
3032 case DLT_SLIP_BSDOS
:
3035 * These types don't provide any type field; packets
3036 * are always IPv4 or IPv6.
3038 * XXX - for IPv4, check for a version number of 4, and,
3039 * for IPv6, check for a version number of 6?
3044 /* Check for a version number of 4. */
3045 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3047 case ETHERTYPE_IPV6
:
3048 /* Check for a version number of 6. */
3049 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3052 return gen_false(); /* always false */
3059 * Raw IPv4, so no type field.
3061 if (proto
== ETHERTYPE_IP
)
3062 return gen_true(); /* always true */
3064 /* Checking for something other than IPv4; always false */
3071 * Raw IPv6, so no type field.
3073 if (proto
== ETHERTYPE_IPV6
)
3074 return gen_true(); /* always true */
3076 /* Checking for something other than IPv6; always false */
3083 case DLT_PPP_SERIAL
:
3086 * We use Ethernet protocol types inside libpcap;
3087 * map them to the corresponding PPP protocol types.
3089 proto
= ethertype_to_ppptype(proto
);
3090 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3096 * We use Ethernet protocol types inside libpcap;
3097 * map them to the corresponding PPP protocol types.
3103 * Also check for Van Jacobson-compressed IP.
3104 * XXX - do this for other forms of PPP?
3106 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3107 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3109 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3114 proto
= ethertype_to_ppptype(proto
);
3115 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3125 * For DLT_NULL, the link-layer header is a 32-bit
3126 * word containing an AF_ value in *host* byte order,
3127 * and for DLT_ENC, the link-layer header begins
3128 * with a 32-bit work containing an AF_ value in
3131 * In addition, if we're reading a saved capture file,
3132 * the host byte order in the capture may not be the
3133 * same as the host byte order on this machine.
3135 * For DLT_LOOP, the link-layer header is a 32-bit
3136 * word containing an AF_ value in *network* byte order.
3138 * XXX - AF_ values may, unfortunately, be platform-
3139 * dependent; for example, FreeBSD's AF_INET6 is 24
3140 * whilst NetBSD's and OpenBSD's is 26.
3142 * This means that, when reading a capture file, just
3143 * checking for our AF_INET6 value won't work if the
3144 * capture file came from another OS.
3153 case ETHERTYPE_IPV6
:
3160 * Not a type on which we support filtering.
3161 * XXX - support those that have AF_ values
3162 * #defined on this platform, at least?
3167 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3169 * The AF_ value is in host byte order, but
3170 * the BPF interpreter will convert it to
3171 * network byte order.
3173 * If this is a save file, and it's from a
3174 * machine with the opposite byte order to
3175 * ours, we byte-swap the AF_ value.
3177 * Then we run it through "htonl()", and
3178 * generate code to compare against the result.
3180 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3181 bpf_pcap
->sf
.swapped
)
3182 proto
= SWAPLONG(proto
);
3183 proto
= htonl(proto
);
3185 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3187 #ifdef HAVE_NET_PFVAR_H
3190 * af field is host byte order in contrast to the rest of
3193 if (proto
== ETHERTYPE_IP
)
3194 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3195 BPF_B
, (bpf_int32
)AF_INET
));
3196 else if (proto
== ETHERTYPE_IPV6
)
3197 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3198 BPF_B
, (bpf_int32
)AF_INET6
));
3203 #endif /* HAVE_NET_PFVAR_H */
3206 case DLT_ARCNET_LINUX
:
3208 * XXX should we check for first fragment if the protocol
3216 case ETHERTYPE_IPV6
:
3217 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3218 (bpf_int32
)ARCTYPE_INET6
));
3221 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3222 (bpf_int32
)ARCTYPE_IP
);
3223 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3224 (bpf_int32
)ARCTYPE_IP_OLD
);
3229 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3230 (bpf_int32
)ARCTYPE_ARP
);
3231 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3232 (bpf_int32
)ARCTYPE_ARP_OLD
);
3236 case ETHERTYPE_REVARP
:
3237 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3238 (bpf_int32
)ARCTYPE_REVARP
));
3240 case ETHERTYPE_ATALK
:
3241 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3242 (bpf_int32
)ARCTYPE_ATALK
));
3249 case ETHERTYPE_ATALK
:
3259 * XXX - assumes a 2-byte Frame Relay header with
3260 * DLCI and flags. What if the address is longer?
3266 * Check for the special NLPID for IP.
3268 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3270 case ETHERTYPE_IPV6
:
3272 * Check for the special NLPID for IPv6.
3274 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3278 * Check for several OSI protocols.
3280 * Frame Relay packets typically have an OSI
3281 * NLPID at the beginning; we check for each
3284 * What we check for is the NLPID and a frame
3285 * control field of UI, i.e. 0x03 followed
3288 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3289 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3290 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3302 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3304 case DLT_JUNIPER_MFR
:
3305 case DLT_JUNIPER_MLFR
:
3306 case DLT_JUNIPER_MLPPP
:
3307 case DLT_JUNIPER_ATM1
:
3308 case DLT_JUNIPER_ATM2
:
3309 case DLT_JUNIPER_PPPOE
:
3310 case DLT_JUNIPER_PPPOE_ATM
:
3311 case DLT_JUNIPER_GGSN
:
3312 case DLT_JUNIPER_ES
:
3313 case DLT_JUNIPER_MONITOR
:
3314 case DLT_JUNIPER_SERVICES
:
3315 case DLT_JUNIPER_ETHER
:
3316 case DLT_JUNIPER_PPP
:
3317 case DLT_JUNIPER_FRELAY
:
3318 case DLT_JUNIPER_CHDLC
:
3319 case DLT_JUNIPER_VP
:
3320 case DLT_JUNIPER_ST
:
3321 case DLT_JUNIPER_ISM
:
3322 case DLT_JUNIPER_VS
:
3323 case DLT_JUNIPER_SRX_E2E
:
3324 case DLT_JUNIPER_FIBRECHANNEL
:
3325 case DLT_JUNIPER_ATM_CEMIC
:
3327 /* just lets verify the magic number for now -
3328 * on ATM we may have up to 6 different encapsulations on the wire
3329 * and need a lot of heuristics to figure out that the payload
3332 * FIXME encapsulation specific BPF_ filters
3334 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3337 return gen_ipnet_linktype(proto
);
3339 case DLT_LINUX_IRDA
:
3340 bpf_error("IrDA link-layer type filtering not implemented");
3343 bpf_error("DOCSIS link-layer type filtering not implemented");
3346 case DLT_MTP2_WITH_PHDR
:
3347 bpf_error("MTP2 link-layer type filtering not implemented");
3350 bpf_error("ERF link-layer type filtering not implemented");
3353 bpf_error("PFSYNC link-layer type filtering not implemented");
3355 case DLT_LINUX_LAPD
:
3356 bpf_error("LAPD link-layer type filtering not implemented");
3360 case DLT_USB_LINUX_MMAPPED
:
3361 bpf_error("USB link-layer type filtering not implemented");
3363 case DLT_BLUETOOTH_HCI_H4
:
3364 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3365 bpf_error("Bluetooth link-layer type filtering not implemented");
3368 case DLT_CAN_SOCKETCAN
:
3369 bpf_error("CAN link-layer type filtering not implemented");
3371 case DLT_IEEE802_15_4
:
3372 case DLT_IEEE802_15_4_LINUX
:
3373 case DLT_IEEE802_15_4_NONASK_PHY
:
3374 case DLT_IEEE802_15_4_NOFCS
:
3375 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3377 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3378 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3381 bpf_error("SITA link-layer type filtering not implemented");
3384 bpf_error("RAIF1 link-layer type filtering not implemented");
3387 bpf_error("IPMB link-layer type filtering not implemented");
3390 bpf_error("AX.25 link-layer type filtering not implemented");
3394 * All the types that have no encapsulation should either be
3395 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3396 * all packets are IP packets, or should be handled in some
3397 * special case, if none of them are (if some are and some
3398 * aren't, the lack of encapsulation is a problem, as we'd
3399 * have to find some other way of determining the packet type).
3401 * Therefore, if "off_linktype" is -1, there's an error.
3403 if (off_linktype
== (u_int
)-1)
3407 * Any type not handled above should always have an Ethernet
3408 * type at an offset of "off_linktype".
3410 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3414 * Check for an LLC SNAP packet with a given organization code and
3415 * protocol type; we check the entire contents of the 802.2 LLC and
3416 * snap headers, checking for DSAP and SSAP of SNAP and a control
3417 * field of 0x03 in the LLC header, and for the specified organization
3418 * code and protocol type in the SNAP header.
3420 static struct block
*
3421 gen_snap(orgcode
, ptype
)
3422 bpf_u_int32 orgcode
;
3425 u_char snapblock
[8];
3427 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3428 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3429 snapblock
[2] = 0x03; /* control = UI */
3430 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3431 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3432 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3433 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3434 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3435 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3439 * Generate code to match a particular packet type, for link-layer types
3440 * using 802.2 LLC headers.
3442 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3443 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3445 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3446 * value, if <= ETHERMTU. We use that to determine whether to
3447 * match the DSAP or both DSAP and LSAP or to check the OUI and
3448 * protocol ID in a SNAP header.
3450 static struct block
*
3451 gen_llc_linktype(proto
)
3455 * XXX - handle token-ring variable-length header.
3461 case LLCSAP_NETBEUI
:
3463 * XXX - should we check both the DSAP and the
3464 * SSAP, like this, or should we check just the
3465 * DSAP, as we do for other types <= ETHERMTU
3466 * (i.e., other SAP values)?
3468 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3469 ((proto
<< 8) | proto
));
3473 * XXX - are there ever SNAP frames for IPX on
3474 * non-Ethernet 802.x networks?
3476 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3477 (bpf_int32
)LLCSAP_IPX
);
3479 case ETHERTYPE_ATALK
:
3481 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3482 * SNAP packets with an organization code of
3483 * 0x080007 (Apple, for Appletalk) and a protocol
3484 * type of ETHERTYPE_ATALK (Appletalk).
3486 * XXX - check for an organization code of
3487 * encapsulated Ethernet as well?
3489 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3493 * XXX - we don't have to check for IPX 802.3
3494 * here, but should we check for the IPX Ethertype?
3496 if (proto
<= ETHERMTU
) {
3498 * This is an LLC SAP value, so check
3501 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3504 * This is an Ethernet type; we assume that it's
3505 * unlikely that it'll appear in the right place
3506 * at random, and therefore check only the
3507 * location that would hold the Ethernet type
3508 * in a SNAP frame with an organization code of
3509 * 0x000000 (encapsulated Ethernet).
3511 * XXX - if we were to check for the SNAP DSAP and
3512 * LSAP, as per XXX, and were also to check for an
3513 * organization code of 0x000000 (encapsulated
3514 * Ethernet), we'd do
3516 * return gen_snap(0x000000, proto);
3518 * here; for now, we don't, as per the above.
3519 * I don't know whether it's worth the extra CPU
3520 * time to do the right check or not.
3522 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3527 static struct block
*
3528 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3532 u_int src_off
, dst_off
;
3534 struct block
*b0
, *b1
;
3548 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3549 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3555 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3556 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3563 b0
= gen_linktype(proto
);
3564 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3569 static struct block
*
3570 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3571 struct in6_addr
*addr
;
3572 struct in6_addr
*mask
;
3574 u_int src_off
, dst_off
;
3576 struct block
*b0
, *b1
;
3591 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3592 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3598 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3599 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3606 /* this order is important */
3607 a
= (u_int32_t
*)addr
;
3608 m
= (u_int32_t
*)mask
;
3609 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3610 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3612 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3614 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3616 b0
= gen_linktype(proto
);
3621 static struct block
*
3622 gen_ehostop(eaddr
, dir
)
3623 register const u_char
*eaddr
;
3626 register struct block
*b0
, *b1
;
3630 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3633 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3636 b0
= gen_ehostop(eaddr
, Q_SRC
);
3637 b1
= gen_ehostop(eaddr
, Q_DST
);
3643 b0
= gen_ehostop(eaddr
, Q_SRC
);
3644 b1
= gen_ehostop(eaddr
, Q_DST
);
3649 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3653 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3657 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3661 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3665 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3669 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3677 * Like gen_ehostop, but for DLT_FDDI
3679 static struct block
*
3680 gen_fhostop(eaddr
, dir
)
3681 register const u_char
*eaddr
;
3684 struct block
*b0
, *b1
;
3689 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3691 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3696 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3698 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3702 b0
= gen_fhostop(eaddr
, Q_SRC
);
3703 b1
= gen_fhostop(eaddr
, Q_DST
);
3709 b0
= gen_fhostop(eaddr
, Q_SRC
);
3710 b1
= gen_fhostop(eaddr
, Q_DST
);
3715 bpf_error("'addr1' is only supported on 802.11");
3719 bpf_error("'addr2' is only supported on 802.11");
3723 bpf_error("'addr3' is only supported on 802.11");
3727 bpf_error("'addr4' is only supported on 802.11");
3731 bpf_error("'ra' is only supported on 802.11");
3735 bpf_error("'ta' is only supported on 802.11");
3743 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3745 static struct block
*
3746 gen_thostop(eaddr
, dir
)
3747 register const u_char
*eaddr
;
3750 register struct block
*b0
, *b1
;
3754 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3757 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3760 b0
= gen_thostop(eaddr
, Q_SRC
);
3761 b1
= gen_thostop(eaddr
, Q_DST
);
3767 b0
= gen_thostop(eaddr
, Q_SRC
);
3768 b1
= gen_thostop(eaddr
, Q_DST
);
3773 bpf_error("'addr1' is only supported on 802.11");
3777 bpf_error("'addr2' is only supported on 802.11");
3781 bpf_error("'addr3' is only supported on 802.11");
3785 bpf_error("'addr4' is only supported on 802.11");
3789 bpf_error("'ra' is only supported on 802.11");
3793 bpf_error("'ta' is only supported on 802.11");
3801 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3802 * various 802.11 + radio headers.
3804 static struct block
*
3805 gen_wlanhostop(eaddr
, dir
)
3806 register const u_char
*eaddr
;
3809 register struct block
*b0
, *b1
, *b2
;
3810 register struct slist
*s
;
3812 #ifdef ENABLE_WLAN_FILTERING_PATCH
3815 * We need to disable the optimizer because the optimizer is buggy
3816 * and wipes out some LD instructions generated by the below
3817 * code to validate the Frame Control bits
3820 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3827 * For control frames, there is no SA.
3829 * For management frames, SA is at an
3830 * offset of 10 from the beginning of
3833 * For data frames, SA is at an offset
3834 * of 10 from the beginning of the packet
3835 * if From DS is clear, at an offset of
3836 * 16 from the beginning of the packet
3837 * if From DS is set and To DS is clear,
3838 * and an offset of 24 from the beginning
3839 * of the packet if From DS is set and To DS
3844 * Generate the tests to be done for data frames
3847 * First, check for To DS set, i.e. check "link[1] & 0x01".
3849 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3850 b1
= new_block(JMP(BPF_JSET
));
3851 b1
->s
.k
= 0x01; /* To DS */
3855 * If To DS is set, the SA is at 24.
3857 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3861 * Now, check for To DS not set, i.e. check
3862 * "!(link[1] & 0x01)".
3864 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3865 b2
= new_block(JMP(BPF_JSET
));
3866 b2
->s
.k
= 0x01; /* To DS */
3871 * If To DS is not set, the SA is at 16.
3873 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3877 * Now OR together the last two checks. That gives
3878 * the complete set of checks for data frames with
3884 * Now check for From DS being set, and AND that with
3885 * the ORed-together checks.
3887 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3888 b1
= new_block(JMP(BPF_JSET
));
3889 b1
->s
.k
= 0x02; /* From DS */
3894 * Now check for data frames with From DS not set.
3896 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3897 b2
= new_block(JMP(BPF_JSET
));
3898 b2
->s
.k
= 0x02; /* From DS */
3903 * If From DS isn't set, the SA is at 10.
3905 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3909 * Now OR together the checks for data frames with
3910 * From DS not set and for data frames with From DS
3911 * set; that gives the checks done for data frames.
3916 * Now check for a data frame.
3917 * I.e, check "link[0] & 0x08".
3919 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3920 b1
= new_block(JMP(BPF_JSET
));
3925 * AND that with the checks done for data frames.
3930 * If the high-order bit of the type value is 0, this
3931 * is a management frame.
3932 * I.e, check "!(link[0] & 0x08)".
3934 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3935 b2
= new_block(JMP(BPF_JSET
));
3941 * For management frames, the SA is at 10.
3943 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3947 * OR that with the checks done for data frames.
3948 * That gives the checks done for management and
3954 * If the low-order bit of the type value is 1,
3955 * this is either a control frame or a frame
3956 * with a reserved type, and thus not a
3959 * I.e., check "!(link[0] & 0x04)".
3961 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3962 b1
= new_block(JMP(BPF_JSET
));
3968 * AND that with the checks for data and management
3978 * For control frames, there is no DA.
3980 * For management frames, DA is at an
3981 * offset of 4 from the beginning of
3984 * For data frames, DA is at an offset
3985 * of 4 from the beginning of the packet
3986 * if To DS is clear and at an offset of
3987 * 16 from the beginning of the packet
3992 * Generate the tests to be done for data frames.
3994 * First, check for To DS set, i.e. "link[1] & 0x01".
3996 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3997 b1
= new_block(JMP(BPF_JSET
));
3998 b1
->s
.k
= 0x01; /* To DS */
4002 * If To DS is set, the DA is at 16.
4004 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4008 * Now, check for To DS not set, i.e. check
4009 * "!(link[1] & 0x01)".
4011 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4012 b2
= new_block(JMP(BPF_JSET
));
4013 b2
->s
.k
= 0x01; /* To DS */
4018 * If To DS is not set, the DA is at 4.
4020 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4024 * Now OR together the last two checks. That gives
4025 * the complete set of checks for data frames.
4030 * Now check for a data frame.
4031 * I.e, check "link[0] & 0x08".
4033 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4034 b1
= new_block(JMP(BPF_JSET
));
4039 * AND that with the checks done for data frames.
4044 * If the high-order bit of the type value is 0, this
4045 * is a management frame.
4046 * I.e, check "!(link[0] & 0x08)".
4048 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4049 b2
= new_block(JMP(BPF_JSET
));
4055 * For management frames, the DA is at 4.
4057 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4061 * OR that with the checks done for data frames.
4062 * That gives the checks done for management and
4068 * If the low-order bit of the type value is 1,
4069 * this is either a control frame or a frame
4070 * with a reserved type, and thus not a
4073 * I.e., check "!(link[0] & 0x04)".
4075 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4076 b1
= new_block(JMP(BPF_JSET
));
4082 * AND that with the checks for data and management
4090 * Not present in management frames; addr1 in other
4095 * If the high-order bit of the type value is 0, this
4096 * is a management frame.
4097 * I.e, check "(link[0] & 0x08)".
4099 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4100 b1
= new_block(JMP(BPF_JSET
));
4107 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4110 * AND that with the check of addr1.
4117 * Not present in management frames; addr2, if present,
4122 * Not present in CTS or ACK control frames.
4124 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4125 IEEE80211_FC0_TYPE_MASK
);
4127 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4128 IEEE80211_FC0_SUBTYPE_MASK
);
4130 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4131 IEEE80211_FC0_SUBTYPE_MASK
);
4137 * If the high-order bit of the type value is 0, this
4138 * is a management frame.
4139 * I.e, check "(link[0] & 0x08)".
4141 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4142 b1
= new_block(JMP(BPF_JSET
));
4147 * AND that with the check for frames other than
4148 * CTS and ACK frames.
4155 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4160 * XXX - add BSSID keyword?
4163 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4167 * Not present in CTS or ACK control frames.
4169 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4170 IEEE80211_FC0_TYPE_MASK
);
4172 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4173 IEEE80211_FC0_SUBTYPE_MASK
);
4175 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4176 IEEE80211_FC0_SUBTYPE_MASK
);
4180 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4186 * Not present in control frames.
4188 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4189 IEEE80211_FC0_TYPE_MASK
);
4191 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4197 * Present only if the direction mask has both "From DS"
4198 * and "To DS" set. Neither control frames nor management
4199 * frames should have both of those set, so we don't
4200 * check the frame type.
4202 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4203 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4204 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4209 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4210 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4216 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4217 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4226 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4227 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4228 * as the RFC states.)
4230 static struct block
*
4231 gen_ipfchostop(eaddr
, dir
)
4232 register const u_char
*eaddr
;
4235 register struct block
*b0
, *b1
;
4239 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4242 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4245 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4246 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4252 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4253 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4258 bpf_error("'addr1' is only supported on 802.11");
4262 bpf_error("'addr2' is only supported on 802.11");
4266 bpf_error("'addr3' is only supported on 802.11");
4270 bpf_error("'addr4' is only supported on 802.11");
4274 bpf_error("'ra' is only supported on 802.11");
4278 bpf_error("'ta' is only supported on 802.11");
4286 * This is quite tricky because there may be pad bytes in front of the
4287 * DECNET header, and then there are two possible data packet formats that
4288 * carry both src and dst addresses, plus 5 packet types in a format that
4289 * carries only the src node, plus 2 types that use a different format and
4290 * also carry just the src node.
4294 * Instead of doing those all right, we just look for data packets with
4295 * 0 or 1 bytes of padding. If you want to look at other packets, that
4296 * will require a lot more hacking.
4298 * To add support for filtering on DECNET "areas" (network numbers)
4299 * one would want to add a "mask" argument to this routine. That would
4300 * make the filter even more inefficient, although one could be clever
4301 * and not generate masking instructions if the mask is 0xFFFF.
4303 static struct block
*
4304 gen_dnhostop(addr
, dir
)
4308 struct block
*b0
, *b1
, *b2
, *tmp
;
4309 u_int offset_lh
; /* offset if long header is received */
4310 u_int offset_sh
; /* offset if short header is received */
4315 offset_sh
= 1; /* follows flags */
4316 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4320 offset_sh
= 3; /* follows flags, dstnode */
4321 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4325 /* Inefficient because we do our Calvinball dance twice */
4326 b0
= gen_dnhostop(addr
, Q_SRC
);
4327 b1
= gen_dnhostop(addr
, Q_DST
);
4333 /* Inefficient because we do our Calvinball dance twice */
4334 b0
= gen_dnhostop(addr
, Q_SRC
);
4335 b1
= gen_dnhostop(addr
, Q_DST
);
4340 bpf_error("ISO host filtering not implemented");
4345 b0
= gen_linktype(ETHERTYPE_DN
);
4346 /* Check for pad = 1, long header case */
4347 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4348 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4349 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4350 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4352 /* Check for pad = 0, long header case */
4353 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4354 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4357 /* Check for pad = 1, short header case */
4358 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4359 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4360 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4363 /* Check for pad = 0, short header case */
4364 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4365 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4369 /* Combine with test for linktype */
4375 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4376 * test the bottom-of-stack bit, and then check the version number
4377 * field in the IP header.
4379 static struct block
*
4380 gen_mpls_linktype(proto
)
4383 struct block
*b0
, *b1
;
4388 /* match the bottom-of-stack bit */
4389 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4390 /* match the IPv4 version number */
4391 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4396 /* match the bottom-of-stack bit */
4397 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4398 /* match the IPv4 version number */
4399 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4408 static struct block
*
4409 gen_host(addr
, mask
, proto
, dir
, type
)
4416 struct block
*b0
, *b1
;
4417 const char *typestr
;
4427 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4429 * Only check for non-IPv4 addresses if we're not
4430 * checking MPLS-encapsulated packets.
4432 if (label_stack_depth
== 0) {
4433 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4435 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4441 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4444 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4447 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4450 bpf_error("'tcp' modifier applied to %s", typestr
);
4453 bpf_error("'sctp' modifier applied to %s", typestr
);
4456 bpf_error("'udp' modifier applied to %s", typestr
);
4459 bpf_error("'icmp' modifier applied to %s", typestr
);
4462 bpf_error("'igmp' modifier applied to %s", typestr
);
4465 bpf_error("'igrp' modifier applied to %s", typestr
);
4468 bpf_error("'pim' modifier applied to %s", typestr
);
4471 bpf_error("'vrrp' modifier applied to %s", typestr
);
4474 bpf_error("'carp' modifier applied to %s", typestr
);
4477 bpf_error("ATALK host filtering not implemented");
4480 bpf_error("AARP host filtering not implemented");
4483 return gen_dnhostop(addr
, dir
);
4486 bpf_error("SCA host filtering not implemented");
4489 bpf_error("LAT host filtering not implemented");
4492 bpf_error("MOPDL host filtering not implemented");
4495 bpf_error("MOPRC host filtering not implemented");
4498 bpf_error("'ip6' modifier applied to ip host");
4501 bpf_error("'icmp6' modifier applied to %s", typestr
);
4504 bpf_error("'ah' modifier applied to %s", typestr
);
4507 bpf_error("'esp' modifier applied to %s", typestr
);
4510 bpf_error("ISO host filtering not implemented");
4513 bpf_error("'esis' modifier applied to %s", typestr
);
4516 bpf_error("'isis' modifier applied to %s", typestr
);
4519 bpf_error("'clnp' modifier applied to %s", typestr
);
4522 bpf_error("'stp' modifier applied to %s", typestr
);
4525 bpf_error("IPX host filtering not implemented");
4528 bpf_error("'netbeui' modifier applied to %s", typestr
);
4531 bpf_error("'radio' modifier applied to %s", typestr
);
4539 static struct block
*
4540 gen_host6(addr
, mask
, proto
, dir
, type
)
4541 struct in6_addr
*addr
;
4542 struct in6_addr
*mask
;
4547 const char *typestr
;
4557 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4560 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4563 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4566 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4569 bpf_error("'sctp' modifier applied to %s", typestr
);
4572 bpf_error("'tcp' modifier applied to %s", typestr
);
4575 bpf_error("'udp' modifier applied to %s", typestr
);
4578 bpf_error("'icmp' modifier applied to %s", typestr
);
4581 bpf_error("'igmp' modifier applied to %s", typestr
);
4584 bpf_error("'igrp' modifier applied to %s", typestr
);
4587 bpf_error("'pim' modifier applied to %s", typestr
);
4590 bpf_error("'vrrp' modifier applied to %s", typestr
);
4593 bpf_error("'carp' modifier applied to %s", typestr
);
4596 bpf_error("ATALK host filtering not implemented");
4599 bpf_error("AARP host filtering not implemented");
4602 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4605 bpf_error("SCA host filtering not implemented");
4608 bpf_error("LAT host filtering not implemented");
4611 bpf_error("MOPDL host filtering not implemented");
4614 bpf_error("MOPRC host filtering not implemented");
4617 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4620 bpf_error("'icmp6' modifier applied to %s", typestr
);
4623 bpf_error("'ah' modifier applied to %s", typestr
);
4626 bpf_error("'esp' modifier applied to %s", typestr
);
4629 bpf_error("ISO host filtering not implemented");
4632 bpf_error("'esis' modifier applied to %s", typestr
);
4635 bpf_error("'isis' modifier applied to %s", typestr
);
4638 bpf_error("'clnp' modifier applied to %s", typestr
);
4641 bpf_error("'stp' modifier applied to %s", typestr
);
4644 bpf_error("IPX host filtering not implemented");
4647 bpf_error("'netbeui' modifier applied to %s", typestr
);
4650 bpf_error("'radio' modifier applied to %s", typestr
);
4659 static struct block
*
4660 gen_gateway(eaddr
, alist
, proto
, dir
)
4661 const u_char
*eaddr
;
4662 bpf_u_int32
**alist
;
4666 struct block
*b0
, *b1
, *tmp
;
4669 bpf_error("direction applied to 'gateway'");
4678 case DLT_NETANALYZER
:
4679 case DLT_NETANALYZER_TRANSPARENT
:
4680 b0
= gen_ehostop(eaddr
, Q_OR
);
4683 b0
= gen_fhostop(eaddr
, Q_OR
);
4686 b0
= gen_thostop(eaddr
, Q_OR
);
4688 case DLT_IEEE802_11
:
4689 case DLT_PRISM_HEADER
:
4690 case DLT_IEEE802_11_RADIO_AVS
:
4691 case DLT_IEEE802_11_RADIO
:
4693 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4698 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4700 * Check that the packet doesn't begin with an
4701 * LE Control marker. (We've already generated
4704 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4709 * Now check the MAC address.
4711 b0
= gen_ehostop(eaddr
, Q_OR
);
4714 case DLT_IP_OVER_FC
:
4715 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4719 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4721 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4723 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4732 bpf_error("illegal modifier of 'gateway'");
4738 gen_proto_abbrev(proto
)
4747 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4748 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4753 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4754 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4759 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4760 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4765 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4768 #ifndef IPPROTO_IGMP
4769 #define IPPROTO_IGMP 2
4773 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4776 #ifndef IPPROTO_IGRP
4777 #define IPPROTO_IGRP 9
4780 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4784 #define IPPROTO_PIM 103
4788 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4789 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4793 #ifndef IPPROTO_VRRP
4794 #define IPPROTO_VRRP 112
4798 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4801 #ifndef IPPROTO_CARP
4802 #define IPPROTO_CARP 112
4806 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4810 b1
= gen_linktype(ETHERTYPE_IP
);
4814 b1
= gen_linktype(ETHERTYPE_ARP
);
4818 b1
= gen_linktype(ETHERTYPE_REVARP
);
4822 bpf_error("link layer applied in wrong context");
4825 b1
= gen_linktype(ETHERTYPE_ATALK
);
4829 b1
= gen_linktype(ETHERTYPE_AARP
);
4833 b1
= gen_linktype(ETHERTYPE_DN
);
4837 b1
= gen_linktype(ETHERTYPE_SCA
);
4841 b1
= gen_linktype(ETHERTYPE_LAT
);
4845 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4849 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4853 b1
= gen_linktype(ETHERTYPE_IPV6
);
4856 #ifndef IPPROTO_ICMPV6
4857 #define IPPROTO_ICMPV6 58
4860 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4864 #define IPPROTO_AH 51
4867 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4868 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4873 #define IPPROTO_ESP 50
4876 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4877 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4882 b1
= gen_linktype(LLCSAP_ISONS
);
4886 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4890 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4893 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4894 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4895 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4897 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4899 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4901 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4905 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4906 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4907 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4909 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4911 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4913 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4917 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4918 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4919 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4921 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4926 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4927 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4932 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4933 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4935 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4937 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4942 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4943 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4948 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4949 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4954 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4958 b1
= gen_linktype(LLCSAP_8021D
);
4962 b1
= gen_linktype(LLCSAP_IPX
);
4966 b1
= gen_linktype(LLCSAP_NETBEUI
);
4970 bpf_error("'radio' is not a valid protocol type");
4978 static struct block
*
4984 /* not IPv4 frag other than the first frag */
4985 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4986 b
= new_block(JMP(BPF_JSET
));
4995 * Generate a comparison to a port value in the transport-layer header
4996 * at the specified offset from the beginning of that header.
4998 * XXX - this handles a variable-length prefix preceding the link-layer
4999 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5000 * variable-length link-layer headers (such as Token Ring or 802.11
5003 static struct block
*
5004 gen_portatom(off
, v
)
5008 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5011 static struct block
*
5012 gen_portatom6(off
, v
)
5016 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5020 gen_portop(port
, proto
, dir
)
5021 int port
, proto
, dir
;
5023 struct block
*b0
, *b1
, *tmp
;
5025 /* ip proto 'proto' and not a fragment other than the first fragment */
5026 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5032 b1
= gen_portatom(0, (bpf_int32
)port
);
5036 b1
= gen_portatom(2, (bpf_int32
)port
);
5041 tmp
= gen_portatom(0, (bpf_int32
)port
);
5042 b1
= gen_portatom(2, (bpf_int32
)port
);
5047 tmp
= gen_portatom(0, (bpf_int32
)port
);
5048 b1
= gen_portatom(2, (bpf_int32
)port
);
5060 static struct block
*
5061 gen_port(port
, ip_proto
, dir
)
5066 struct block
*b0
, *b1
, *tmp
;
5071 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5072 * not LLC encapsulation with LLCSAP_IP.
5074 * For IEEE 802 networks - which includes 802.5 token ring
5075 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5076 * says that SNAP encapsulation is used, not LLC encapsulation
5079 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5080 * RFC 2225 say that SNAP encapsulation is used, not LLC
5081 * encapsulation with LLCSAP_IP.
5083 * So we always check for ETHERTYPE_IP.
5085 b0
= gen_linktype(ETHERTYPE_IP
);
5091 b1
= gen_portop(port
, ip_proto
, dir
);
5095 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5096 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5098 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5110 gen_portop6(port
, proto
, dir
)
5111 int port
, proto
, dir
;
5113 struct block
*b0
, *b1
, *tmp
;
5115 /* ip6 proto 'proto' */
5116 /* XXX - catch the first fragment of a fragmented packet? */
5117 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5121 b1
= gen_portatom6(0, (bpf_int32
)port
);
5125 b1
= gen_portatom6(2, (bpf_int32
)port
);
5130 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5131 b1
= gen_portatom6(2, (bpf_int32
)port
);
5136 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5137 b1
= gen_portatom6(2, (bpf_int32
)port
);
5149 static struct block
*
5150 gen_port6(port
, ip_proto
, dir
)
5155 struct block
*b0
, *b1
, *tmp
;
5157 /* link proto ip6 */
5158 b0
= gen_linktype(ETHERTYPE_IPV6
);
5164 b1
= gen_portop6(port
, ip_proto
, dir
);
5168 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5169 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5171 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5182 /* gen_portrange code */
5183 static struct block
*
5184 gen_portrangeatom(off
, v1
, v2
)
5188 struct block
*b1
, *b2
;
5192 * Reverse the order of the ports, so v1 is the lower one.
5201 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5202 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5210 gen_portrangeop(port1
, port2
, proto
, dir
)
5215 struct block
*b0
, *b1
, *tmp
;
5217 /* ip proto 'proto' and not a fragment other than the first fragment */
5218 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5224 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5228 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5233 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5234 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5239 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5240 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5252 static struct block
*
5253 gen_portrange(port1
, port2
, ip_proto
, dir
)
5258 struct block
*b0
, *b1
, *tmp
;
5261 b0
= gen_linktype(ETHERTYPE_IP
);
5267 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5271 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5272 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5274 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5285 static struct block
*
5286 gen_portrangeatom6(off
, v1
, v2
)
5290 struct block
*b1
, *b2
;
5294 * Reverse the order of the ports, so v1 is the lower one.
5303 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5304 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5312 gen_portrangeop6(port1
, port2
, proto
, dir
)
5317 struct block
*b0
, *b1
, *tmp
;
5319 /* ip6 proto 'proto' */
5320 /* XXX - catch the first fragment of a fragmented packet? */
5321 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5325 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5329 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5334 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5335 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5340 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5341 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5353 static struct block
*
5354 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5359 struct block
*b0
, *b1
, *tmp
;
5361 /* link proto ip6 */
5362 b0
= gen_linktype(ETHERTYPE_IPV6
);
5368 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5372 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5373 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5375 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5387 lookup_proto(name
, proto
)
5388 register const char *name
;
5398 v
= pcap_nametoproto(name
);
5399 if (v
== PROTO_UNDEF
)
5400 bpf_error("unknown ip proto '%s'", name
);
5404 /* XXX should look up h/w protocol type based on linktype */
5405 v
= pcap_nametoeproto(name
);
5406 if (v
== PROTO_UNDEF
) {
5407 v
= pcap_nametollc(name
);
5408 if (v
== PROTO_UNDEF
)
5409 bpf_error("unknown ether proto '%s'", name
);
5414 if (strcmp(name
, "esis") == 0)
5416 else if (strcmp(name
, "isis") == 0)
5418 else if (strcmp(name
, "clnp") == 0)
5421 bpf_error("unknown osi proto '%s'", name
);
5441 static struct block
*
5442 gen_protochain(v
, proto
, dir
)
5447 #ifdef NO_PROTOCHAIN
5448 return gen_proto(v
, proto
, dir
);
5450 struct block
*b0
, *b
;
5451 struct slist
*s
[100];
5452 int fix2
, fix3
, fix4
, fix5
;
5453 int ahcheck
, again
, end
;
5455 int reg2
= alloc_reg();
5457 memset(s
, 0, sizeof(s
));
5458 fix2
= fix3
= fix4
= fix5
= 0;
5465 b0
= gen_protochain(v
, Q_IP
, dir
);
5466 b
= gen_protochain(v
, Q_IPV6
, dir
);
5470 bpf_error("bad protocol applied for 'protochain'");
5475 * We don't handle variable-length prefixes before the link-layer
5476 * header, or variable-length link-layer headers, here yet.
5477 * We might want to add BPF instructions to do the protochain
5478 * work, to simplify that and, on platforms that have a BPF
5479 * interpreter with the new instructions, let the filtering
5480 * be done in the kernel. (We already require a modified BPF
5481 * engine to do the protochain stuff, to support backward
5482 * branches, and backward branch support is unlikely to appear
5483 * in kernel BPF engines.)
5487 case DLT_IEEE802_11
:
5488 case DLT_PRISM_HEADER
:
5489 case DLT_IEEE802_11_RADIO_AVS
:
5490 case DLT_IEEE802_11_RADIO
:
5492 bpf_error("'protochain' not supported with 802.11");
5495 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5498 * s[0] is a dummy entry to protect other BPF insn from damage
5499 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5500 * hard to find interdependency made by jump table fixup.
5503 s
[i
] = new_stmt(0); /*dummy*/
5508 b0
= gen_linktype(ETHERTYPE_IP
);
5511 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5512 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5514 /* X = ip->ip_hl << 2 */
5515 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5516 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5521 b0
= gen_linktype(ETHERTYPE_IPV6
);
5523 /* A = ip6->ip_nxt */
5524 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5525 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5527 /* X = sizeof(struct ip6_hdr) */
5528 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5534 bpf_error("unsupported proto to gen_protochain");
5538 /* again: if (A == v) goto end; else fall through; */
5540 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5542 s
[i
]->s
.jt
= NULL
; /*later*/
5543 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5547 #ifndef IPPROTO_NONE
5548 #define IPPROTO_NONE 59
5550 /* if (A == IPPROTO_NONE) goto end */
5551 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5552 s
[i
]->s
.jt
= NULL
; /*later*/
5553 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5554 s
[i
]->s
.k
= IPPROTO_NONE
;
5555 s
[fix5
]->s
.jf
= s
[i
];
5559 if (proto
== Q_IPV6
) {
5560 int v6start
, v6end
, v6advance
, j
;
5563 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5564 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5565 s
[i
]->s
.jt
= NULL
; /*later*/
5566 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5567 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5568 s
[fix2
]->s
.jf
= s
[i
];
5570 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5571 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5572 s
[i
]->s
.jt
= NULL
; /*later*/
5573 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5574 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5576 /* if (A == IPPROTO_ROUTING) goto v6advance */
5577 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5578 s
[i
]->s
.jt
= NULL
; /*later*/
5579 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5580 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5582 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5583 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5584 s
[i
]->s
.jt
= NULL
; /*later*/
5585 s
[i
]->s
.jf
= NULL
; /*later*/
5586 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5596 * A = P[X + packet head];
5597 * X = X + (P[X + packet head + 1] + 1) * 8;
5599 /* A = P[X + packet head] */
5600 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5601 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5604 s
[i
] = new_stmt(BPF_ST
);
5607 /* A = P[X + packet head + 1]; */
5608 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5609 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 1;
5612 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5616 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5620 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5624 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5627 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5631 /* goto again; (must use BPF_JA for backward jump) */
5632 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5633 s
[i
]->s
.k
= again
- i
- 1;
5634 s
[i
- 1]->s
.jf
= s
[i
];
5638 for (j
= v6start
; j
<= v6end
; j
++)
5639 s
[j
]->s
.jt
= s
[v6advance
];
5642 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5644 s
[fix2
]->s
.jf
= s
[i
];
5650 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5651 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5652 s
[i
]->s
.jt
= NULL
; /*later*/
5653 s
[i
]->s
.jf
= NULL
; /*later*/
5654 s
[i
]->s
.k
= IPPROTO_AH
;
5656 s
[fix3
]->s
.jf
= s
[ahcheck
];
5663 * X = X + (P[X + 1] + 2) * 4;
5666 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5668 /* A = P[X + packet head]; */
5669 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5670 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5673 s
[i
] = new_stmt(BPF_ST
);
5677 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5680 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5684 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5686 /* A = P[X + packet head] */
5687 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5688 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5691 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5695 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5699 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5702 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5706 /* goto again; (must use BPF_JA for backward jump) */
5707 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5708 s
[i
]->s
.k
= again
- i
- 1;
5713 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5715 s
[fix2
]->s
.jt
= s
[end
];
5716 s
[fix4
]->s
.jf
= s
[end
];
5717 s
[fix5
]->s
.jt
= s
[end
];
5724 for (i
= 0; i
< max
- 1; i
++)
5725 s
[i
]->next
= s
[i
+ 1];
5726 s
[max
- 1]->next
= NULL
;
5731 b
= new_block(JMP(BPF_JEQ
));
5732 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5742 static struct block
*
5743 gen_check_802_11_data_frame()
5746 struct block
*b0
, *b1
;
5749 * A data frame has the 0x08 bit (b3) in the frame control field set
5750 * and the 0x04 bit (b2) clear.
5752 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5753 b0
= new_block(JMP(BPF_JSET
));
5757 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5758 b1
= new_block(JMP(BPF_JSET
));
5769 * Generate code that checks whether the packet is a packet for protocol
5770 * <proto> and whether the type field in that protocol's header has
5771 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5772 * IP packet and checks the protocol number in the IP header against <v>.
5774 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5775 * against Q_IP and Q_IPV6.
5777 static struct block
*
5778 gen_proto(v
, proto
, dir
)
5783 struct block
*b0
, *b1
;
5788 if (dir
!= Q_DEFAULT
)
5789 bpf_error("direction applied to 'proto'");
5793 b0
= gen_proto(v
, Q_IP
, dir
);
5794 b1
= gen_proto(v
, Q_IPV6
, dir
);
5800 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5801 * not LLC encapsulation with LLCSAP_IP.
5803 * For IEEE 802 networks - which includes 802.5 token ring
5804 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5805 * says that SNAP encapsulation is used, not LLC encapsulation
5808 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5809 * RFC 2225 say that SNAP encapsulation is used, not LLC
5810 * encapsulation with LLCSAP_IP.
5812 * So we always check for ETHERTYPE_IP.
5814 b0
= gen_linktype(ETHERTYPE_IP
);
5816 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5818 b1
= gen_protochain(v
, Q_IP
);
5828 * Frame Relay packets typically have an OSI
5829 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5830 * generates code to check for all the OSI
5831 * NLPIDs, so calling it and then adding a check
5832 * for the particular NLPID for which we're
5833 * looking is bogus, as we can just check for
5836 * What we check for is the NLPID and a frame
5837 * control field value of UI, i.e. 0x03 followed
5840 * XXX - assumes a 2-byte Frame Relay header with
5841 * DLCI and flags. What if the address is longer?
5843 * XXX - what about SNAP-encapsulated frames?
5845 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5851 * Cisco uses an Ethertype lookalike - for OSI,
5854 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5855 /* OSI in C-HDLC is stuffed with a fudge byte */
5856 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5861 b0
= gen_linktype(LLCSAP_ISONS
);
5862 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5868 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5870 * 4 is the offset of the PDU type relative to the IS-IS
5873 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5878 bpf_error("arp does not encapsulate another protocol");
5882 bpf_error("rarp does not encapsulate another protocol");
5886 bpf_error("atalk encapsulation is not specifiable");
5890 bpf_error("decnet encapsulation is not specifiable");
5894 bpf_error("sca does not encapsulate another protocol");
5898 bpf_error("lat does not encapsulate another protocol");
5902 bpf_error("moprc does not encapsulate another protocol");
5906 bpf_error("mopdl does not encapsulate another protocol");
5910 return gen_linktype(v
);
5913 bpf_error("'udp proto' is bogus");
5917 bpf_error("'tcp proto' is bogus");
5921 bpf_error("'sctp proto' is bogus");
5925 bpf_error("'icmp proto' is bogus");
5929 bpf_error("'igmp proto' is bogus");
5933 bpf_error("'igrp proto' is bogus");
5937 bpf_error("'pim proto' is bogus");
5941 bpf_error("'vrrp proto' is bogus");
5945 bpf_error("'carp proto' is bogus");
5949 b0
= gen_linktype(ETHERTYPE_IPV6
);
5952 * Also check for a fragment header before the final
5955 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
5956 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
5958 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5961 b1
= gen_protochain(v
, Q_IPV6
);
5967 bpf_error("'icmp6 proto' is bogus");
5970 bpf_error("'ah proto' is bogus");
5973 bpf_error("'ah proto' is bogus");
5976 bpf_error("'stp proto' is bogus");
5979 bpf_error("'ipx proto' is bogus");
5982 bpf_error("'netbeui proto' is bogus");
5985 bpf_error("'radio proto' is bogus");
5996 register const char *name
;
5999 int proto
= q
.proto
;
6003 bpf_u_int32 mask
, addr
;
6005 bpf_u_int32
**alist
;
6008 struct sockaddr_in
*sin4
;
6009 struct sockaddr_in6
*sin6
;
6010 struct addrinfo
*res
, *res0
;
6011 struct in6_addr mask128
;
6013 struct block
*b
, *tmp
;
6014 int port
, real_proto
;
6020 addr
= pcap_nametonetaddr(name
);
6022 bpf_error("unknown network '%s'", name
);
6023 /* Left justify network addr and calculate its network mask */
6025 while (addr
&& (addr
& 0xff000000) == 0) {
6029 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6033 if (proto
== Q_LINK
) {
6037 case DLT_NETANALYZER
:
6038 case DLT_NETANALYZER_TRANSPARENT
:
6039 eaddr
= pcap_ether_hostton(name
);
6042 "unknown ether host '%s'", name
);
6043 b
= gen_ehostop(eaddr
, dir
);
6048 eaddr
= pcap_ether_hostton(name
);
6051 "unknown FDDI host '%s'", name
);
6052 b
= gen_fhostop(eaddr
, dir
);
6057 eaddr
= pcap_ether_hostton(name
);
6060 "unknown token ring host '%s'", name
);
6061 b
= gen_thostop(eaddr
, dir
);
6065 case DLT_IEEE802_11
:
6066 case DLT_PRISM_HEADER
:
6067 case DLT_IEEE802_11_RADIO_AVS
:
6068 case DLT_IEEE802_11_RADIO
:
6070 eaddr
= pcap_ether_hostton(name
);
6073 "unknown 802.11 host '%s'", name
);
6074 b
= gen_wlanhostop(eaddr
, dir
);
6078 case DLT_IP_OVER_FC
:
6079 eaddr
= pcap_ether_hostton(name
);
6082 "unknown Fibre Channel host '%s'", name
);
6083 b
= gen_ipfchostop(eaddr
, dir
);
6092 * Check that the packet doesn't begin
6093 * with an LE Control marker. (We've
6094 * already generated a test for LANE.)
6096 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6100 eaddr
= pcap_ether_hostton(name
);
6103 "unknown ether host '%s'", name
);
6104 b
= gen_ehostop(eaddr
, dir
);
6110 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6111 } else if (proto
== Q_DECNET
) {
6112 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6114 * I don't think DECNET hosts can be multihomed, so
6115 * there is no need to build up a list of addresses
6117 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6120 alist
= pcap_nametoaddr(name
);
6121 if (alist
== NULL
|| *alist
== NULL
)
6122 bpf_error("unknown host '%s'", name
);
6124 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6126 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6128 tmp
= gen_host(**alist
++, 0xffffffff,
6129 tproto
, dir
, q
.addr
);
6135 memset(&mask128
, 0xff, sizeof(mask128
));
6136 res0
= res
= pcap_nametoaddrinfo(name
);
6138 bpf_error("unknown host '%s'", name
);
6141 tproto
= tproto6
= proto
;
6142 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6146 for (res
= res0
; res
; res
= res
->ai_next
) {
6147 switch (res
->ai_family
) {
6149 if (tproto
== Q_IPV6
)
6152 sin4
= (struct sockaddr_in
*)
6154 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6155 0xffffffff, tproto
, dir
, q
.addr
);
6158 if (tproto6
== Q_IP
)
6161 sin6
= (struct sockaddr_in6
*)
6163 tmp
= gen_host6(&sin6
->sin6_addr
,
6164 &mask128
, tproto6
, dir
, q
.addr
);
6176 bpf_error("unknown host '%s'%s", name
,
6177 (proto
== Q_DEFAULT
)
6179 : " for specified address family");
6186 if (proto
!= Q_DEFAULT
&&
6187 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6188 bpf_error("illegal qualifier of 'port'");
6189 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6190 bpf_error("unknown port '%s'", name
);
6191 if (proto
== Q_UDP
) {
6192 if (real_proto
== IPPROTO_TCP
)
6193 bpf_error("port '%s' is tcp", name
);
6194 else if (real_proto
== IPPROTO_SCTP
)
6195 bpf_error("port '%s' is sctp", name
);
6197 /* override PROTO_UNDEF */
6198 real_proto
= IPPROTO_UDP
;
6200 if (proto
== Q_TCP
) {
6201 if (real_proto
== IPPROTO_UDP
)
6202 bpf_error("port '%s' is udp", name
);
6204 else if (real_proto
== IPPROTO_SCTP
)
6205 bpf_error("port '%s' is sctp", name
);
6207 /* override PROTO_UNDEF */
6208 real_proto
= IPPROTO_TCP
;
6210 if (proto
== Q_SCTP
) {
6211 if (real_proto
== IPPROTO_UDP
)
6212 bpf_error("port '%s' is udp", name
);
6214 else if (real_proto
== IPPROTO_TCP
)
6215 bpf_error("port '%s' is tcp", name
);
6217 /* override PROTO_UNDEF */
6218 real_proto
= IPPROTO_SCTP
;
6221 bpf_error("illegal port number %d < 0", port
);
6223 bpf_error("illegal port number %d > 65535", port
);
6224 b
= gen_port(port
, real_proto
, dir
);
6225 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6229 if (proto
!= Q_DEFAULT
&&
6230 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6231 bpf_error("illegal qualifier of 'portrange'");
6232 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6233 bpf_error("unknown port in range '%s'", name
);
6234 if (proto
== Q_UDP
) {
6235 if (real_proto
== IPPROTO_TCP
)
6236 bpf_error("port in range '%s' is tcp", name
);
6237 else if (real_proto
== IPPROTO_SCTP
)
6238 bpf_error("port in range '%s' is sctp", name
);
6240 /* override PROTO_UNDEF */
6241 real_proto
= IPPROTO_UDP
;
6243 if (proto
== Q_TCP
) {
6244 if (real_proto
== IPPROTO_UDP
)
6245 bpf_error("port in range '%s' is udp", name
);
6246 else if (real_proto
== IPPROTO_SCTP
)
6247 bpf_error("port in range '%s' is sctp", name
);
6249 /* override PROTO_UNDEF */
6250 real_proto
= IPPROTO_TCP
;
6252 if (proto
== Q_SCTP
) {
6253 if (real_proto
== IPPROTO_UDP
)
6254 bpf_error("port in range '%s' is udp", name
);
6255 else if (real_proto
== IPPROTO_TCP
)
6256 bpf_error("port in range '%s' is tcp", name
);
6258 /* override PROTO_UNDEF */
6259 real_proto
= IPPROTO_SCTP
;
6262 bpf_error("illegal port number %d < 0", port1
);
6264 bpf_error("illegal port number %d > 65535", port1
);
6266 bpf_error("illegal port number %d < 0", port2
);
6268 bpf_error("illegal port number %d > 65535", port2
);
6270 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6271 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6276 eaddr
= pcap_ether_hostton(name
);
6278 bpf_error("unknown ether host: %s", name
);
6280 alist
= pcap_nametoaddr(name
);
6281 if (alist
== NULL
|| *alist
== NULL
)
6282 bpf_error("unknown host '%s'", name
);
6283 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6287 bpf_error("'gateway' not supported in this configuration");
6291 real_proto
= lookup_proto(name
, proto
);
6292 if (real_proto
>= 0)
6293 return gen_proto(real_proto
, proto
, dir
);
6295 bpf_error("unknown protocol: %s", name
);
6298 real_proto
= lookup_proto(name
, proto
);
6299 if (real_proto
>= 0)
6300 return gen_protochain(real_proto
, proto
, dir
);
6302 bpf_error("unknown protocol: %s", name
);
6313 gen_mcode(s1
, s2
, masklen
, q
)
6314 register const char *s1
, *s2
;
6315 register int masklen
;
6318 register int nlen
, mlen
;
6321 nlen
= __pcap_atoin(s1
, &n
);
6322 /* Promote short ipaddr */
6326 mlen
= __pcap_atoin(s2
, &m
);
6327 /* Promote short ipaddr */
6330 bpf_error("non-network bits set in \"%s mask %s\"",
6333 /* Convert mask len to mask */
6335 bpf_error("mask length must be <= 32");
6338 * X << 32 is not guaranteed by C to be 0; it's
6343 m
= 0xffffffff << (32 - masklen
);
6345 bpf_error("non-network bits set in \"%s/%d\"",
6352 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6355 bpf_error("Mask syntax for networks only");
6364 register const char *s
;
6369 int proto
= q
.proto
;
6375 else if (q
.proto
== Q_DECNET
)
6376 vlen
= __pcap_atodn(s
, &v
);
6378 vlen
= __pcap_atoin(s
, &v
);
6385 if (proto
== Q_DECNET
)
6386 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6387 else if (proto
== Q_LINK
) {
6388 bpf_error("illegal link layer address");
6391 if (s
== NULL
&& q
.addr
== Q_NET
) {
6392 /* Promote short net number */
6393 while (v
&& (v
& 0xff000000) == 0) {
6398 /* Promote short ipaddr */
6402 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6407 proto
= IPPROTO_UDP
;
6408 else if (proto
== Q_TCP
)
6409 proto
= IPPROTO_TCP
;
6410 else if (proto
== Q_SCTP
)
6411 proto
= IPPROTO_SCTP
;
6412 else if (proto
== Q_DEFAULT
)
6413 proto
= PROTO_UNDEF
;
6415 bpf_error("illegal qualifier of 'port'");
6418 bpf_error("illegal port number %u > 65535", v
);
6422 b
= gen_port((int)v
, proto
, dir
);
6423 gen_or(gen_port6((int)v
, proto
, dir
), b
);
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 'portrange'");
6440 bpf_error("illegal port number %u > 65535", v
);
6444 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6445 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6450 bpf_error("'gateway' requires a name");
6454 return gen_proto((int)v
, proto
, dir
);
6457 return gen_protochain((int)v
, proto
, dir
);
6472 gen_mcode6(s1
, s2
, masklen
, q
)
6473 register const char *s1
, *s2
;
6474 register int masklen
;
6477 struct addrinfo
*res
;
6478 struct in6_addr
*addr
;
6479 struct in6_addr mask
;
6484 bpf_error("no mask %s supported", s2
);
6486 res
= pcap_nametoaddrinfo(s1
);
6488 bpf_error("invalid ip6 address %s", s1
);
6491 bpf_error("%s resolved to multiple address", s1
);
6492 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6494 if (sizeof(mask
) * 8 < masklen
)
6495 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6496 memset(&mask
, 0, sizeof(mask
));
6497 memset(&mask
, 0xff, masklen
/ 8);
6499 mask
.s6_addr
[masklen
/ 8] =
6500 (0xff << (8 - masklen
% 8)) & 0xff;
6503 a
= (u_int32_t
*)addr
;
6504 m
= (u_int32_t
*)&mask
;
6505 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6506 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6507 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6515 bpf_error("Mask syntax for networks only");
6519 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6525 bpf_error("invalid qualifier against IPv6 address");
6534 register const u_char
*eaddr
;
6537 struct block
*b
, *tmp
;
6539 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6542 case DLT_NETANALYZER
:
6543 case DLT_NETANALYZER_TRANSPARENT
:
6544 return gen_ehostop(eaddr
, (int)q
.dir
);
6546 return gen_fhostop(eaddr
, (int)q
.dir
);
6548 return gen_thostop(eaddr
, (int)q
.dir
);
6549 case DLT_IEEE802_11
:
6550 case DLT_PRISM_HEADER
:
6551 case DLT_IEEE802_11_RADIO_AVS
:
6552 case DLT_IEEE802_11_RADIO
:
6554 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6558 * Check that the packet doesn't begin with an
6559 * LE Control marker. (We've already generated
6562 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6567 * Now check the MAC address.
6569 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6574 case DLT_IP_OVER_FC
:
6575 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6577 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6581 bpf_error("ethernet address used in non-ether expression");
6588 struct slist
*s0
, *s1
;
6591 * This is definitely not the best way to do this, but the
6592 * lists will rarely get long.
6599 static struct slist
*
6605 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6610 static struct slist
*
6616 s
= new_stmt(BPF_LD
|BPF_MEM
);
6622 * Modify "index" to use the value stored into its register as an
6623 * offset relative to the beginning of the header for the protocol
6624 * "proto", and allocate a register and put an item "size" bytes long
6625 * (1, 2, or 4) at that offset into that register, making it the register
6629 gen_load(proto
, inst
, size
)
6634 struct slist
*s
, *tmp
;
6636 int regno
= alloc_reg();
6638 free_reg(inst
->regno
);
6642 bpf_error("data size must be 1, 2, or 4");
6658 bpf_error("unsupported index operation");
6662 * The offset is relative to the beginning of the packet
6663 * data, if we have a radio header. (If we don't, this
6666 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6667 linktype
!= DLT_IEEE802_11_RADIO
&&
6668 linktype
!= DLT_PRISM_HEADER
)
6669 bpf_error("radio information not present in capture");
6672 * Load into the X register the offset computed into the
6673 * register specified by "index".
6675 s
= xfer_to_x(inst
);
6678 * Load the item at that offset.
6680 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6682 sappend(inst
->s
, s
);
6687 * The offset is relative to the beginning of
6688 * the link-layer header.
6690 * XXX - what about ATM LANE? Should the index be
6691 * relative to the beginning of the AAL5 frame, so
6692 * that 0 refers to the beginning of the LE Control
6693 * field, or relative to the beginning of the LAN
6694 * frame, so that 0 refers, for Ethernet LANE, to
6695 * the beginning of the destination address?
6697 s
= gen_llprefixlen();
6700 * If "s" is non-null, it has code to arrange that the
6701 * X register contains the length of the prefix preceding
6702 * the link-layer header. Add to it the offset computed
6703 * into the register specified by "index", and move that
6704 * into the X register. Otherwise, just load into the X
6705 * register the offset computed into the register specified
6709 sappend(s
, xfer_to_a(inst
));
6710 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6711 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6713 s
= xfer_to_x(inst
);
6716 * Load the item at the sum of the offset we've put in the
6717 * X register and the offset of the start of the link
6718 * layer header (which is 0 if the radio header is
6719 * variable-length; that header length is what we put
6720 * into the X register and then added to the index).
6722 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6725 sappend(inst
->s
, s
);
6739 * The offset is relative to the beginning of
6740 * the network-layer header.
6741 * XXX - are there any cases where we want
6744 s
= gen_off_macpl();
6747 * If "s" is non-null, it has code to arrange that the
6748 * X register contains the offset of the MAC-layer
6749 * payload. Add to it the offset computed into the
6750 * register specified by "index", and move that into
6751 * the X register. Otherwise, just load into the X
6752 * register the offset computed into the register specified
6756 sappend(s
, xfer_to_a(inst
));
6757 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6758 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6760 s
= xfer_to_x(inst
);
6763 * Load the item at the sum of the offset we've put in the
6764 * X register, the offset of the start of the network
6765 * layer header from the beginning of the MAC-layer
6766 * payload, and the purported offset of the start of the
6767 * MAC-layer payload (which might be 0 if there's a
6768 * variable-length prefix before the link-layer header
6769 * or the link-layer header itself is variable-length;
6770 * the variable-length offset of the start of the
6771 * MAC-layer payload is what we put into the X register
6772 * and then added to the index).
6774 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6775 tmp
->s
.k
= off_macpl
+ off_nl
;
6777 sappend(inst
->s
, s
);
6780 * Do the computation only if the packet contains
6781 * the protocol in question.
6783 b
= gen_proto_abbrev(proto
);
6785 gen_and(inst
->b
, b
);
6799 * The offset is relative to the beginning of
6800 * the transport-layer header.
6802 * Load the X register with the length of the IPv4 header
6803 * (plus the offset of the link-layer header, if it's
6804 * a variable-length header), in bytes.
6806 * XXX - are there any cases where we want
6808 * XXX - we should, if we're built with
6809 * IPv6 support, generate code to load either
6810 * IPv4, IPv6, or both, as appropriate.
6812 s
= gen_loadx_iphdrlen();
6815 * The X register now contains the sum of the length
6816 * of any variable-length header preceding the link-layer
6817 * header, any variable-length link-layer header, and the
6818 * length of the network-layer header.
6820 * Load into the A register the offset relative to
6821 * the beginning of the transport layer header,
6822 * add the X register to that, move that to the
6823 * X register, and load with an offset from the
6824 * X register equal to the offset of the network
6825 * layer header relative to the beginning of
6826 * the MAC-layer payload plus the fixed-length
6827 * portion of the offset of the MAC-layer payload
6828 * from the beginning of the raw packet data.
6830 sappend(s
, xfer_to_a(inst
));
6831 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6832 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6833 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6834 tmp
->s
.k
= off_macpl
+ off_nl
;
6835 sappend(inst
->s
, s
);
6838 * Do the computation only if the packet contains
6839 * the protocol in question - which is true only
6840 * if this is an IP datagram and is the first or
6841 * only fragment of that datagram.
6843 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6845 gen_and(inst
->b
, b
);
6846 gen_and(gen_proto_abbrev(Q_IP
), b
);
6850 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6853 inst
->regno
= regno
;
6854 s
= new_stmt(BPF_ST
);
6856 sappend(inst
->s
, s
);
6862 gen_relation(code
, a0
, a1
, reversed
)
6864 struct arth
*a0
, *a1
;
6867 struct slist
*s0
, *s1
, *s2
;
6868 struct block
*b
, *tmp
;
6872 if (code
== BPF_JEQ
) {
6873 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6874 b
= new_block(JMP(code
));
6878 b
= new_block(BPF_JMP
|code
|BPF_X
);
6884 sappend(a0
->s
, a1
->s
);
6888 free_reg(a0
->regno
);
6889 free_reg(a1
->regno
);
6891 /* 'and' together protocol checks */
6894 gen_and(a0
->b
, tmp
= a1
->b
);
6910 int regno
= alloc_reg();
6911 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6914 s
= new_stmt(BPF_LD
|BPF_LEN
);
6915 s
->next
= new_stmt(BPF_ST
);
6916 s
->next
->s
.k
= regno
;
6931 a
= (struct arth
*)newchunk(sizeof(*a
));
6935 s
= new_stmt(BPF_LD
|BPF_IMM
);
6937 s
->next
= new_stmt(BPF_ST
);
6953 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6956 s
= new_stmt(BPF_ST
);
6964 gen_arth(code
, a0
, a1
)
6966 struct arth
*a0
, *a1
;
6968 struct slist
*s0
, *s1
, *s2
;
6972 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6977 sappend(a0
->s
, a1
->s
);
6979 free_reg(a0
->regno
);
6980 free_reg(a1
->regno
);
6982 s0
= new_stmt(BPF_ST
);
6983 a0
->regno
= s0
->s
.k
= alloc_reg();
6990 * Here we handle simple allocation of the scratch registers.
6991 * If too many registers are alloc'd, the allocator punts.
6993 static int regused
[BPF_MEMWORDS
];
6997 * Initialize the table of used registers and the current register.
7003 memset(regused
, 0, sizeof regused
);
7007 * Return the next free register.
7012 int n
= BPF_MEMWORDS
;
7015 if (regused
[curreg
])
7016 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7018 regused
[curreg
] = 1;
7022 bpf_error("too many registers needed to evaluate expression");
7028 * Return a register to the table so it can
7038 static struct block
*
7045 s
= new_stmt(BPF_LD
|BPF_LEN
);
7046 b
= new_block(JMP(jmp
));
7057 return gen_len(BPF_JGE
, n
);
7061 * Actually, this is less than or equal.
7069 b
= gen_len(BPF_JGT
, n
);
7076 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7077 * the beginning of the link-layer header.
7078 * XXX - that means you can't test values in the radiotap header, but
7079 * as that header is difficult if not impossible to parse generally
7080 * without a loop, that might not be a severe problem. A new keyword
7081 * "radio" could be added for that, although what you'd really want
7082 * would be a way of testing particular radio header values, which
7083 * would generate code appropriate to the radio header in question.
7086 gen_byteop(op
, idx
, val
)
7097 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7100 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7104 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7108 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7112 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7116 b
= new_block(JMP(BPF_JEQ
));
7123 static u_char abroadcast
[] = { 0x0 };
7126 gen_broadcast(proto
)
7129 bpf_u_int32 hostmask
;
7130 struct block
*b0
, *b1
, *b2
;
7131 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7139 case DLT_ARCNET_LINUX
:
7140 return gen_ahostop(abroadcast
, Q_DST
);
7142 case DLT_NETANALYZER
:
7143 case DLT_NETANALYZER_TRANSPARENT
:
7144 return gen_ehostop(ebroadcast
, Q_DST
);
7146 return gen_fhostop(ebroadcast
, Q_DST
);
7148 return gen_thostop(ebroadcast
, Q_DST
);
7149 case DLT_IEEE802_11
:
7150 case DLT_PRISM_HEADER
:
7151 case DLT_IEEE802_11_RADIO_AVS
:
7152 case DLT_IEEE802_11_RADIO
:
7154 return gen_wlanhostop(ebroadcast
, Q_DST
);
7155 case DLT_IP_OVER_FC
:
7156 return gen_ipfchostop(ebroadcast
, Q_DST
);
7160 * Check that the packet doesn't begin with an
7161 * LE Control marker. (We've already generated
7164 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7169 * Now check the MAC address.
7171 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7177 bpf_error("not a broadcast link");
7183 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7184 * as an indication that we don't know the netmask, and fail
7187 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7188 bpf_error("netmask not known, so 'ip broadcast' not supported");
7189 b0
= gen_linktype(ETHERTYPE_IP
);
7190 hostmask
= ~netmask
;
7191 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7192 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7193 (bpf_int32
)(~0 & hostmask
), hostmask
);
7198 bpf_error("only link-layer/IP broadcast filters supported");
7204 * Generate code to test the low-order bit of a MAC address (that's
7205 * the bottom bit of the *first* byte).
7207 static struct block
*
7208 gen_mac_multicast(offset
)
7211 register struct block
*b0
;
7212 register struct slist
*s
;
7214 /* link[offset] & 1 != 0 */
7215 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7216 b0
= new_block(JMP(BPF_JSET
));
7223 gen_multicast(proto
)
7226 register struct block
*b0
, *b1
, *b2
;
7227 register struct slist
*s
;
7235 case DLT_ARCNET_LINUX
:
7236 /* all ARCnet multicasts use the same address */
7237 return gen_ahostop(abroadcast
, Q_DST
);
7239 case DLT_NETANALYZER
:
7240 case DLT_NETANALYZER_TRANSPARENT
:
7241 /* ether[0] & 1 != 0 */
7242 return gen_mac_multicast(0);
7245 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7247 * XXX - was that referring to bit-order issues?
7249 /* fddi[1] & 1 != 0 */
7250 return gen_mac_multicast(1);
7252 /* tr[2] & 1 != 0 */
7253 return gen_mac_multicast(2);
7254 case DLT_IEEE802_11
:
7255 case DLT_PRISM_HEADER
:
7256 case DLT_IEEE802_11_RADIO_AVS
:
7257 case DLT_IEEE802_11_RADIO
:
7262 * For control frames, there is no DA.
7264 * For management frames, DA is at an
7265 * offset of 4 from the beginning of
7268 * For data frames, DA is at an offset
7269 * of 4 from the beginning of the packet
7270 * if To DS is clear and at an offset of
7271 * 16 from the beginning of the packet
7276 * Generate the tests to be done for data frames.
7278 * First, check for To DS set, i.e. "link[1] & 0x01".
7280 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7281 b1
= new_block(JMP(BPF_JSET
));
7282 b1
->s
.k
= 0x01; /* To DS */
7286 * If To DS is set, the DA is at 16.
7288 b0
= gen_mac_multicast(16);
7292 * Now, check for To DS not set, i.e. check
7293 * "!(link[1] & 0x01)".
7295 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7296 b2
= new_block(JMP(BPF_JSET
));
7297 b2
->s
.k
= 0x01; /* To DS */
7302 * If To DS is not set, the DA is at 4.
7304 b1
= gen_mac_multicast(4);
7308 * Now OR together the last two checks. That gives
7309 * the complete set of checks for data frames.
7314 * Now check for a data frame.
7315 * I.e, check "link[0] & 0x08".
7317 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7318 b1
= new_block(JMP(BPF_JSET
));
7323 * AND that with the checks done for data frames.
7328 * If the high-order bit of the type value is 0, this
7329 * is a management frame.
7330 * I.e, check "!(link[0] & 0x08)".
7332 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7333 b2
= new_block(JMP(BPF_JSET
));
7339 * For management frames, the DA is at 4.
7341 b1
= gen_mac_multicast(4);
7345 * OR that with the checks done for data frames.
7346 * That gives the checks done for management and
7352 * If the low-order bit of the type value is 1,
7353 * this is either a control frame or a frame
7354 * with a reserved type, and thus not a
7357 * I.e., check "!(link[0] & 0x04)".
7359 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7360 b1
= new_block(JMP(BPF_JSET
));
7366 * AND that with the checks for data and management
7371 case DLT_IP_OVER_FC
:
7372 b0
= gen_mac_multicast(2);
7377 * Check that the packet doesn't begin with an
7378 * LE Control marker. (We've already generated
7381 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7385 /* ether[off_mac] & 1 != 0 */
7386 b0
= gen_mac_multicast(off_mac
);
7394 /* Link not known to support multicasts */
7398 b0
= gen_linktype(ETHERTYPE_IP
);
7399 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7404 b0
= gen_linktype(ETHERTYPE_IPV6
);
7405 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7409 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7415 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7416 * Outbound traffic is sent by this machine, while inbound traffic is
7417 * sent by a remote machine (and may include packets destined for a
7418 * unicast or multicast link-layer address we are not subscribing to).
7419 * These are the same definitions implemented by pcap_setdirection().
7420 * Capturing only unicast traffic destined for this host is probably
7421 * better accomplished using a higher-layer filter.
7427 register struct block
*b0
;
7430 * Only some data link types support inbound/outbound qualifiers.
7434 b0
= gen_relation(BPF_JEQ
,
7435 gen_load(Q_LINK
, gen_loadi(0), 1),
7442 /* match outgoing packets */
7443 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7445 /* match incoming packets */
7446 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7451 /* match outgoing packets */
7452 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7454 /* to filter on inbound traffic, invert the match */
7459 #ifdef HAVE_NET_PFVAR_H
7461 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7462 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7468 /* match outgoing packets */
7469 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7471 /* match incoming packets */
7472 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7476 case DLT_JUNIPER_MFR
:
7477 case DLT_JUNIPER_MLFR
:
7478 case DLT_JUNIPER_MLPPP
:
7479 case DLT_JUNIPER_ATM1
:
7480 case DLT_JUNIPER_ATM2
:
7481 case DLT_JUNIPER_PPPOE
:
7482 case DLT_JUNIPER_PPPOE_ATM
:
7483 case DLT_JUNIPER_GGSN
:
7484 case DLT_JUNIPER_ES
:
7485 case DLT_JUNIPER_MONITOR
:
7486 case DLT_JUNIPER_SERVICES
:
7487 case DLT_JUNIPER_ETHER
:
7488 case DLT_JUNIPER_PPP
:
7489 case DLT_JUNIPER_FRELAY
:
7490 case DLT_JUNIPER_CHDLC
:
7491 case DLT_JUNIPER_VP
:
7492 case DLT_JUNIPER_ST
:
7493 case DLT_JUNIPER_ISM
:
7494 case DLT_JUNIPER_VS
:
7495 case DLT_JUNIPER_SRX_E2E
:
7496 case DLT_JUNIPER_FIBRECHANNEL
:
7497 case DLT_JUNIPER_ATM_CEMIC
:
7499 /* juniper flags (including direction) are stored
7500 * the byte after the 3-byte magic number */
7502 /* match outgoing packets */
7503 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7505 /* match incoming packets */
7506 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7512 * If we have packet meta-data indicating a direction,
7513 * check it, otherwise give up as this link-layer type
7514 * has nothing in the packet data.
7516 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7518 * We infer that this is Linux with PF_PACKET support.
7519 * If this is a *live* capture, we can look at
7520 * special meta-data in the filter expression;
7521 * if it's a savefile, we can't.
7523 if (bpf_pcap
->sf
.rfile
!= NULL
) {
7524 /* We have a FILE *, so this is a savefile */
7525 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7530 /* match outgoing packets */
7531 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7534 /* to filter on inbound traffic, invert the match */
7537 #else /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7538 bpf_error("inbound/outbound not supported on linktype %d",
7542 #endif /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7547 #ifdef HAVE_NET_PFVAR_H
7548 /* PF firewall log matched interface */
7550 gen_pf_ifname(const char *ifname
)
7555 if (linktype
!= DLT_PFLOG
) {
7556 bpf_error("ifname supported only on PF linktype");
7559 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7560 off
= offsetof(struct pfloghdr
, ifname
);
7561 if (strlen(ifname
) >= len
) {
7562 bpf_error("ifname interface names can only be %d characters",
7566 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7570 /* PF firewall log ruleset name */
7572 gen_pf_ruleset(char *ruleset
)
7576 if (linktype
!= DLT_PFLOG
) {
7577 bpf_error("ruleset supported only on PF linktype");
7581 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7582 bpf_error("ruleset names can only be %ld characters",
7583 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7587 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7588 strlen(ruleset
), (const u_char
*)ruleset
);
7592 /* PF firewall log rule number */
7598 if (linktype
!= DLT_PFLOG
) {
7599 bpf_error("rnr supported only on PF linktype");
7603 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7608 /* PF firewall log sub-rule number */
7610 gen_pf_srnr(int srnr
)
7614 if (linktype
!= DLT_PFLOG
) {
7615 bpf_error("srnr supported only on PF linktype");
7619 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7624 /* PF firewall log reason code */
7626 gen_pf_reason(int reason
)
7630 if (linktype
!= DLT_PFLOG
) {
7631 bpf_error("reason supported only on PF linktype");
7635 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7640 /* PF firewall log action */
7642 gen_pf_action(int action
)
7646 if (linktype
!= DLT_PFLOG
) {
7647 bpf_error("action supported only on PF linktype");
7651 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7655 #else /* !HAVE_NET_PFVAR_H */
7657 gen_pf_ifname(const char *ifname
)
7659 bpf_error("libpcap was compiled without pf support");
7665 gen_pf_ruleset(char *ruleset
)
7667 bpf_error("libpcap was compiled on a machine without pf support");
7675 bpf_error("libpcap was compiled on a machine without pf support");
7681 gen_pf_srnr(int srnr
)
7683 bpf_error("libpcap was compiled on a machine without pf support");
7689 gen_pf_reason(int reason
)
7691 bpf_error("libpcap was compiled on a machine without pf support");
7697 gen_pf_action(int action
)
7699 bpf_error("libpcap was compiled on a machine without pf support");
7703 #endif /* HAVE_NET_PFVAR_H */
7705 /* IEEE 802.11 wireless header */
7707 gen_p80211_type(int type
, int mask
)
7713 case DLT_IEEE802_11
:
7714 case DLT_PRISM_HEADER
:
7715 case DLT_IEEE802_11_RADIO_AVS
:
7716 case DLT_IEEE802_11_RADIO
:
7717 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7722 bpf_error("802.11 link-layer types supported only on 802.11");
7730 gen_p80211_fcdir(int fcdir
)
7736 case DLT_IEEE802_11
:
7737 case DLT_PRISM_HEADER
:
7738 case DLT_IEEE802_11_RADIO_AVS
:
7739 case DLT_IEEE802_11_RADIO
:
7743 bpf_error("frame direction supported only with 802.11 headers");
7747 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7748 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7755 register const u_char
*eaddr
;
7761 case DLT_ARCNET_LINUX
:
7762 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7764 return (gen_ahostop(eaddr
, (int)q
.dir
));
7766 bpf_error("ARCnet address used in non-arc expression");
7772 bpf_error("aid supported only on ARCnet");
7775 bpf_error("ARCnet address used in non-arc expression");
7780 static struct block
*
7781 gen_ahostop(eaddr
, dir
)
7782 register const u_char
*eaddr
;
7785 register struct block
*b0
, *b1
;
7788 /* src comes first, different from Ethernet */
7790 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7793 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7796 b0
= gen_ahostop(eaddr
, Q_SRC
);
7797 b1
= gen_ahostop(eaddr
, Q_DST
);
7803 b0
= gen_ahostop(eaddr
, Q_SRC
);
7804 b1
= gen_ahostop(eaddr
, Q_DST
);
7809 bpf_error("'addr1' is only supported on 802.11");
7813 bpf_error("'addr2' is only supported on 802.11");
7817 bpf_error("'addr3' is only supported on 802.11");
7821 bpf_error("'addr4' is only supported on 802.11");
7825 bpf_error("'ra' is only supported on 802.11");
7829 bpf_error("'ta' is only supported on 802.11");
7837 * support IEEE 802.1Q VLAN trunk over ethernet
7843 struct block
*b0
, *b1
;
7845 /* can't check for VLAN-encapsulated packets inside MPLS */
7846 if (label_stack_depth
> 0)
7847 bpf_error("no VLAN match after MPLS");
7850 * Check for a VLAN packet, and then change the offsets to point
7851 * to the type and data fields within the VLAN packet. Just
7852 * increment the offsets, so that we can support a hierarchy, e.g.
7853 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7856 * XXX - this is a bit of a kludge. If we were to split the
7857 * compiler into a parser that parses an expression and
7858 * generates an expression tree, and a code generator that
7859 * takes an expression tree (which could come from our
7860 * parser or from some other parser) and generates BPF code,
7861 * we could perhaps make the offsets parameters of routines
7862 * and, in the handler for an "AND" node, pass to subnodes
7863 * other than the VLAN node the adjusted offsets.
7865 * This would mean that "vlan" would, instead of changing the
7866 * behavior of *all* tests after it, change only the behavior
7867 * of tests ANDed with it. That would change the documented
7868 * semantics of "vlan", which might break some expressions.
7869 * However, it would mean that "(vlan and ip) or ip" would check
7870 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7871 * checking only for VLAN-encapsulated IP, so that could still
7872 * be considered worth doing; it wouldn't break expressions
7873 * that are of the form "vlan and ..." or "vlan N and ...",
7874 * which I suspect are the most common expressions involving
7875 * "vlan". "vlan or ..." doesn't necessarily do what the user
7876 * would really want, now, as all the "or ..." tests would
7877 * be done assuming a VLAN, even though the "or" could be viewed
7878 * as meaning "or, if this isn't a VLAN packet...".
7885 case DLT_NETANALYZER
:
7886 case DLT_NETANALYZER_TRANSPARENT
:
7887 /* check for VLAN, including QinQ */
7888 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7889 (bpf_int32
)ETHERTYPE_8021Q
);
7890 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7891 (bpf_int32
)ETHERTYPE_8021QINQ
);
7895 /* If a specific VLAN is requested, check VLAN id */
7896 if (vlan_num
>= 0) {
7897 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7898 (bpf_int32
)vlan_num
, 0x0fff);
7912 bpf_error("no VLAN support for data link type %d",
7927 struct block
*b0
,*b1
;
7930 * Change the offsets to point to the type and data fields within
7931 * the MPLS packet. Just increment the offsets, so that we
7932 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7933 * capture packets with an outer label of 100000 and an inner
7936 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7940 if (label_stack_depth
> 0) {
7941 /* just match the bottom-of-stack bit clear */
7942 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7945 * Indicate that we're checking MPLS-encapsulated headers,
7946 * to make sure higher level code generators don't try to
7947 * match against IP-related protocols such as Q_ARP, Q_RARP
7952 case DLT_C_HDLC
: /* fall through */
7954 case DLT_NETANALYZER
:
7955 case DLT_NETANALYZER_TRANSPARENT
:
7956 b0
= gen_linktype(ETHERTYPE_MPLS
);
7960 b0
= gen_linktype(PPP_MPLS_UCAST
);
7963 /* FIXME add other DLT_s ...
7964 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7965 * leave it for now */
7968 bpf_error("no MPLS support for data link type %d",
7976 /* If a specific MPLS label is requested, check it */
7977 if (label_num
>= 0) {
7978 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7979 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7980 0xfffff000); /* only compare the first 20 bits */
7987 label_stack_depth
++;
7992 * Support PPPOE discovery and session.
7997 /* check for PPPoE discovery */
7998 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8007 * Test against the PPPoE session link-layer type.
8009 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8012 * Change the offsets to point to the type and data fields within
8013 * the PPP packet, and note that this is PPPoE rather than
8016 * XXX - this is a bit of a kludge. If we were to split the
8017 * compiler into a parser that parses an expression and
8018 * generates an expression tree, and a code generator that
8019 * takes an expression tree (which could come from our
8020 * parser or from some other parser) and generates BPF code,
8021 * we could perhaps make the offsets parameters of routines
8022 * and, in the handler for an "AND" node, pass to subnodes
8023 * other than the PPPoE node the adjusted offsets.
8025 * This would mean that "pppoes" would, instead of changing the
8026 * behavior of *all* tests after it, change only the behavior
8027 * of tests ANDed with it. That would change the documented
8028 * semantics of "pppoes", which might break some expressions.
8029 * However, it would mean that "(pppoes and ip) or ip" would check
8030 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8031 * checking only for VLAN-encapsulated IP, so that could still
8032 * be considered worth doing; it wouldn't break expressions
8033 * that are of the form "pppoes and ..." which I suspect are the
8034 * most common expressions involving "pppoes". "pppoes or ..."
8035 * doesn't necessarily do what the user would really want, now,
8036 * as all the "or ..." tests would be done assuming PPPoE, even
8037 * though the "or" could be viewed as meaning "or, if this isn't
8038 * a PPPoE packet...".
8040 orig_linktype
= off_linktype
; /* save original values */
8045 * The "network-layer" protocol is PPPoE, which has a 6-byte
8046 * PPPoE header, followed by a PPP packet.
8048 * There is no HDLC encapsulation for the PPP packet (it's
8049 * encapsulated in PPPoES instead), so the link-layer type
8050 * starts at the first byte of the PPP packet. For PPPoE,
8051 * that offset is relative to the beginning of the total
8052 * link-layer payload, including any 802.2 LLC header, so
8053 * it's 6 bytes past off_nl.
8055 off_linktype
= off_nl
+ 6;
8058 * The network-layer offsets are relative to the beginning
8059 * of the MAC-layer payload; that's past the 6-byte
8060 * PPPoE header and the 2-byte PPP header.
8063 off_nl_nosnap
= 6+2;
8069 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8081 bpf_error("'vpi' supported only on raw ATM");
8082 if (off_vpi
== (u_int
)-1)
8084 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8090 bpf_error("'vci' supported only on raw ATM");
8091 if (off_vci
== (u_int
)-1)
8093 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8098 if (off_proto
== (u_int
)-1)
8099 abort(); /* XXX - this isn't on FreeBSD */
8100 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8105 if (off_payload
== (u_int
)-1)
8107 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8108 0xffffffff, jtype
, reverse
, jvalue
);
8113 bpf_error("'callref' supported only on raw ATM");
8114 if (off_proto
== (u_int
)-1)
8116 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8117 jtype
, reverse
, jvalue
);
8127 gen_atmtype_abbrev(type
)
8130 struct block
*b0
, *b1
;
8135 /* Get all packets in Meta signalling Circuit */
8137 bpf_error("'metac' supported only on raw ATM");
8138 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8139 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8144 /* Get all packets in Broadcast Circuit*/
8146 bpf_error("'bcc' supported only on raw ATM");
8147 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8148 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8153 /* Get all cells in Segment OAM F4 circuit*/
8155 bpf_error("'oam4sc' supported only on raw ATM");
8156 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8157 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8162 /* Get all cells in End-to-End OAM F4 Circuit*/
8164 bpf_error("'oam4ec' supported only on raw ATM");
8165 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8166 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8171 /* Get all packets in connection Signalling Circuit */
8173 bpf_error("'sc' supported only on raw ATM");
8174 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8175 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8180 /* Get all packets in ILMI Circuit */
8182 bpf_error("'ilmic' supported only on raw ATM");
8183 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8184 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8189 /* Get all LANE packets */
8191 bpf_error("'lane' supported only on raw ATM");
8192 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8195 * Arrange that all subsequent tests assume LANE
8196 * rather than LLC-encapsulated packets, and set
8197 * the offsets appropriately for LANE-encapsulated
8200 * "off_mac" is the offset of the Ethernet header,
8201 * which is 2 bytes past the ATM pseudo-header
8202 * (skipping the pseudo-header and 2-byte LE Client
8203 * field). The other offsets are Ethernet offsets
8204 * relative to "off_mac".
8207 off_mac
= off_payload
+ 2; /* MAC header */
8208 off_linktype
= off_mac
+ 12;
8209 off_macpl
= off_mac
+ 14; /* Ethernet */
8210 off_nl
= 0; /* Ethernet II */
8211 off_nl_nosnap
= 3; /* 802.3+802.2 */
8215 /* Get all LLC-encapsulated packets */
8217 bpf_error("'llc' supported only on raw ATM");
8218 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8229 * Filtering for MTP2 messages based on li value
8230 * FISU, length is null
8231 * LSSU, length is 1 or 2
8232 * MSU, length is 3 or more
8235 gen_mtp2type_abbrev(type
)
8238 struct block
*b0
, *b1
;
8243 if ( (linktype
!= DLT_MTP2
) &&
8244 (linktype
!= DLT_ERF
) &&
8245 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8246 bpf_error("'fisu' supported only on MTP2");
8247 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8248 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8252 if ( (linktype
!= DLT_MTP2
) &&
8253 (linktype
!= DLT_ERF
) &&
8254 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8255 bpf_error("'lssu' supported only on MTP2");
8256 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8257 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8262 if ( (linktype
!= DLT_MTP2
) &&
8263 (linktype
!= DLT_ERF
) &&
8264 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8265 bpf_error("'msu' supported only on MTP2");
8266 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8276 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8283 bpf_u_int32 val1
, val2
, val3
;
8285 switch (mtp3field
) {
8288 if (off_sio
== (u_int
)-1)
8289 bpf_error("'sio' supported only on SS7");
8290 /* sio coded on 1 byte so max value 255 */
8292 bpf_error("sio value %u too big; max value = 255",
8294 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8295 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8299 if (off_opc
== (u_int
)-1)
8300 bpf_error("'opc' supported only on SS7");
8301 /* opc coded on 14 bits so max value 16383 */
8303 bpf_error("opc value %u too big; max value = 16383",
8305 /* the following instructions are made to convert jvalue
8306 * to the form used to write opc in an ss7 message*/
8307 val1
= jvalue
& 0x00003c00;
8309 val2
= jvalue
& 0x000003fc;
8311 val3
= jvalue
& 0x00000003;
8313 jvalue
= val1
+ val2
+ val3
;
8314 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8315 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8319 if (off_dpc
== (u_int
)-1)
8320 bpf_error("'dpc' supported only on SS7");
8321 /* dpc coded on 14 bits so max value 16383 */
8323 bpf_error("dpc value %u too big; max value = 16383",
8325 /* the following instructions are made to convert jvalue
8326 * to the forme used to write dpc in an ss7 message*/
8327 val1
= jvalue
& 0x000000ff;
8329 val2
= jvalue
& 0x00003f00;
8331 jvalue
= val1
+ val2
;
8332 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8333 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8337 if (off_sls
== (u_int
)-1)
8338 bpf_error("'sls' supported only on SS7");
8339 /* sls coded on 4 bits so max value 15 */
8341 bpf_error("sls value %u too big; max value = 15",
8343 /* the following instruction is made to convert jvalue
8344 * to the forme used to write sls in an ss7 message*/
8345 jvalue
= jvalue
<< 4;
8346 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8347 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8356 static struct block
*
8357 gen_msg_abbrev(type
)
8363 * Q.2931 signalling protocol messages for handling virtual circuits
8364 * establishment and teardown
8369 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8373 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8377 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8381 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8385 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8388 case A_RELEASE_DONE
:
8389 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8399 gen_atmmulti_abbrev(type
)
8402 struct block
*b0
, *b1
;
8408 bpf_error("'oam' supported only on raw ATM");
8409 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8414 bpf_error("'oamf4' supported only on raw ATM");
8416 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8417 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8419 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8425 * Get Q.2931 signalling messages for switched
8426 * virtual connection
8429 bpf_error("'connectmsg' supported only on raw ATM");
8430 b0
= gen_msg_abbrev(A_SETUP
);
8431 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8433 b0
= gen_msg_abbrev(A_CONNECT
);
8435 b0
= gen_msg_abbrev(A_CONNECTACK
);
8437 b0
= gen_msg_abbrev(A_RELEASE
);
8439 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8441 b0
= gen_atmtype_abbrev(A_SC
);
8447 bpf_error("'metaconnect' supported only on raw ATM");
8448 b0
= gen_msg_abbrev(A_SETUP
);
8449 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8451 b0
= gen_msg_abbrev(A_CONNECT
);
8453 b0
= gen_msg_abbrev(A_RELEASE
);
8455 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8457 b0
= gen_atmtype_abbrev(A_METAC
);