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(linux) && 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
110 #ifndef IPPROTO_HOPOPTS
111 #define IPPROTO_HOPOPTS 0
113 #ifndef IPPROTO_ROUTING
114 #define IPPROTO_ROUTING 43
116 #ifndef IPPROTO_FRAGMENT
117 #define IPPROTO_FRAGMENT 44
119 #ifndef IPPROTO_DSTOPTS
120 #define IPPROTO_DSTOPTS 60
123 #define IPPROTO_SCTP 132
126 #ifdef HAVE_OS_PROTO_H
127 #include "os-proto.h"
130 #define JMP(c) ((c)|BPF_JMP|BPF_K)
133 static jmp_buf top_ctx
;
134 static pcap_t
*bpf_pcap
;
136 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
138 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
140 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
145 static int pcap_fddipad
;
150 bpf_error(const char *fmt
, ...)
155 if (bpf_pcap
!= NULL
)
156 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
163 static void init_linktype(pcap_t
*);
165 static void init_regs(void);
166 static int alloc_reg(void);
167 static void free_reg(int);
169 static struct block
*root
;
172 * Value passed to gen_load_a() to indicate what the offset argument
176 OR_PACKET
, /* relative to the beginning of the packet */
177 OR_LINK
, /* relative to the beginning of the link-layer header */
178 OR_MACPL
, /* relative to the end of the MAC-layer header */
179 OR_NET
, /* relative to the network-layer header */
180 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
181 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
182 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
187 * As errors are handled by a longjmp, anything allocated must be freed
188 * in the longjmp handler, so it must be reachable from that handler.
189 * One thing that's allocated is the result of pcap_nametoaddrinfo();
190 * it must be freed with freeaddrinfo(). This variable points to any
191 * addrinfo structure that would need to be freed.
193 static struct addrinfo
*ai
;
197 * We divy out chunks of memory rather than call malloc each time so
198 * we don't have to worry about leaking memory. It's probably
199 * not a big deal if all this memory was wasted but if this ever
200 * goes into a library that would probably not be a good idea.
202 * XXX - this *is* in a library....
205 #define CHUNK0SIZE 1024
211 static struct chunk chunks
[NCHUNKS
];
212 static int cur_chunk
;
214 static void *newchunk(u_int
);
215 static void freechunks(void);
216 static inline struct block
*new_block(int);
217 static inline struct slist
*new_stmt(int);
218 static struct block
*gen_retblk(int);
219 static inline void syntax(void);
221 static void backpatch(struct block
*, struct block
*);
222 static void merge(struct block
*, struct block
*);
223 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
224 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
225 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
226 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
227 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
228 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
230 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
231 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
232 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
233 static struct slist
*gen_load_llrel(u_int
, u_int
);
234 static struct slist
*gen_load_macplrel(u_int
, u_int
);
235 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
236 static struct slist
*gen_loadx_iphdrlen(void);
237 static struct block
*gen_uncond(int);
238 static inline struct block
*gen_true(void);
239 static inline struct block
*gen_false(void);
240 static struct block
*gen_ether_linktype(int);
241 static struct block
*gen_ipnet_linktype(int);
242 static struct block
*gen_linux_sll_linktype(int);
243 static struct slist
*gen_load_prism_llprefixlen(void);
244 static struct slist
*gen_load_avs_llprefixlen(void);
245 static struct slist
*gen_load_radiotap_llprefixlen(void);
246 static struct slist
*gen_load_ppi_llprefixlen(void);
247 static void insert_compute_vloffsets(struct block
*);
248 static struct slist
*gen_llprefixlen(void);
249 static struct slist
*gen_off_macpl(void);
250 static int ethertype_to_ppptype(int);
251 static struct block
*gen_linktype(int);
252 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
253 static struct block
*gen_llc_linktype(int);
254 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
256 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
258 static struct block
*gen_ahostop(const u_char
*, int);
259 static struct block
*gen_ehostop(const u_char
*, int);
260 static struct block
*gen_fhostop(const u_char
*, int);
261 static struct block
*gen_thostop(const u_char
*, int);
262 static struct block
*gen_wlanhostop(const u_char
*, int);
263 static struct block
*gen_ipfchostop(const u_char
*, int);
264 static struct block
*gen_dnhostop(bpf_u_int32
, int);
265 static struct block
*gen_mpls_linktype(int);
266 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
268 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
271 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
273 static struct block
*gen_ipfrag(void);
274 static struct block
*gen_portatom(int, bpf_int32
);
275 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
276 static struct block
*gen_portatom6(int, bpf_int32
);
277 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
278 struct block
*gen_portop(int, int, int);
279 static struct block
*gen_port(int, int, int);
280 struct block
*gen_portrangeop(int, int, int, int);
281 static struct block
*gen_portrange(int, int, int, int);
282 struct block
*gen_portop6(int, int, int);
283 static struct block
*gen_port6(int, int, int);
284 struct block
*gen_portrangeop6(int, int, int, int);
285 static struct block
*gen_portrange6(int, int, int, int);
286 static int lookup_proto(const char *, int);
287 static struct block
*gen_protochain(int, int, int);
288 static struct block
*gen_proto(int, int, int);
289 static struct slist
*xfer_to_x(struct arth
*);
290 static struct slist
*xfer_to_a(struct arth
*);
291 static struct block
*gen_mac_multicast(int);
292 static struct block
*gen_len(int, int);
293 static struct block
*gen_check_802_11_data_frame(void);
295 static struct block
*gen_ppi_dlt_check(void);
296 static struct block
*gen_msg_abbrev(int type
);
307 /* XXX Round up to nearest long. */
308 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
310 /* XXX Round up to structure boundary. */
314 cp
= &chunks
[cur_chunk
];
315 if (n
> cp
->n_left
) {
316 ++cp
, k
= ++cur_chunk
;
318 bpf_error("out of memory");
319 size
= CHUNK0SIZE
<< k
;
320 cp
->m
= (void *)malloc(size
);
322 bpf_error("out of memory");
323 memset((char *)cp
->m
, 0, size
);
326 bpf_error("out of memory");
329 return (void *)((char *)cp
->m
+ cp
->n_left
);
338 for (i
= 0; i
< NCHUNKS
; ++i
)
339 if (chunks
[i
].m
!= NULL
) {
346 * A strdup whose allocations are freed after code generation is over.
350 register const char *s
;
352 int n
= strlen(s
) + 1;
353 char *cp
= newchunk(n
);
359 static inline struct block
*
365 p
= (struct block
*)newchunk(sizeof(*p
));
372 static inline struct slist
*
378 p
= (struct slist
*)newchunk(sizeof(*p
));
384 static struct block
*
388 struct block
*b
= new_block(BPF_RET
|BPF_K
);
397 bpf_error("syntax error in filter expression");
400 static bpf_u_int32 netmask
;
405 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
406 const char *buf
, int optimize
, bpf_u_int32 mask
);
409 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
410 const char *buf
, int optimize
, bpf_u_int32 mask
)
414 EnterCriticalSection(&g_PcapCompileCriticalSection
);
416 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
418 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
424 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
425 const char *buf
, int optimize
, bpf_u_int32 mask
)
428 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
429 const char *buf
, int optimize
, bpf_u_int32 mask
)
433 const char * volatile xbuf
= buf
;
437 * If this pcap_t hasn't been activated, it doesn't have a
438 * link-layer type, so we can't use it.
441 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
442 "not-yet-activated pcap_t passed to pcap_compile");
450 if (setjmp(top_ctx
)) {
464 snaplen
= pcap_snapshot(p
);
466 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
467 "snaplen of 0 rejects all packets");
471 lex_init(xbuf
? xbuf
: "");
479 root
= gen_retblk(snaplen
);
481 if (optimize
&& !no_optimize
) {
484 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
485 bpf_error("expression rejects all packets");
487 program
->bf_insns
= icode_to_fcode(root
, &len
);
488 program
->bf_len
= len
;
496 * entry point for using the compiler with no pcap open
497 * pass in all the stuff that is needed explicitly instead.
500 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
501 struct bpf_program
*program
,
502 const char *buf
, int optimize
, bpf_u_int32 mask
)
507 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
510 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
516 * Clean up a "struct bpf_program" by freeing all the memory allocated
520 pcap_freecode(struct bpf_program
*program
)
523 if (program
->bf_insns
!= NULL
) {
524 free((char *)program
->bf_insns
);
525 program
->bf_insns
= NULL
;
530 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
531 * which of the jt and jf fields has been resolved and which is a pointer
532 * back to another unresolved block (or nil). At least one of the fields
533 * in each block is already resolved.
536 backpatch(list
, target
)
537 struct block
*list
, *target
;
554 * Merge the lists in b0 and b1, using the 'sense' field to indicate
555 * which of jt and jf is the link.
559 struct block
*b0
, *b1
;
561 register struct block
**p
= &b0
;
563 /* Find end of list. */
565 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
567 /* Concatenate the lists. */
575 struct block
*ppi_dlt_check
;
578 * Insert before the statements of the first (root) block any
579 * statements needed to load the lengths of any variable-length
580 * headers into registers.
582 * XXX - a fancier strategy would be to insert those before the
583 * statements of all blocks that use those lengths and that
584 * have no predecessors that use them, so that we only compute
585 * the lengths if we need them. There might be even better
586 * approaches than that.
588 * However, those strategies would be more complicated, and
589 * as we don't generate code to compute a length if the
590 * program has no tests that use the length, and as most
591 * tests will probably use those lengths, we would just
592 * postpone computing the lengths so that it's not done
593 * for tests that fail early, and it's not clear that's
596 insert_compute_vloffsets(p
->head
);
599 * For DLT_PPI captures, generate a check of the per-packet
600 * DLT value to make sure it's DLT_IEEE802_11.
602 ppi_dlt_check
= gen_ppi_dlt_check();
603 if (ppi_dlt_check
!= NULL
)
604 gen_and(ppi_dlt_check
, p
);
606 backpatch(p
, gen_retblk(snaplen
));
607 p
->sense
= !p
->sense
;
608 backpatch(p
, gen_retblk(0));
614 struct block
*b0
, *b1
;
616 backpatch(b0
, b1
->head
);
617 b0
->sense
= !b0
->sense
;
618 b1
->sense
= !b1
->sense
;
620 b1
->sense
= !b1
->sense
;
626 struct block
*b0
, *b1
;
628 b0
->sense
= !b0
->sense
;
629 backpatch(b0
, b1
->head
);
630 b0
->sense
= !b0
->sense
;
639 b
->sense
= !b
->sense
;
642 static struct block
*
643 gen_cmp(offrel
, offset
, size
, v
)
644 enum e_offrel offrel
;
648 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
651 static struct block
*
652 gen_cmp_gt(offrel
, offset
, size
, v
)
653 enum e_offrel offrel
;
657 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
660 static struct block
*
661 gen_cmp_ge(offrel
, offset
, size
, v
)
662 enum e_offrel offrel
;
666 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
669 static struct block
*
670 gen_cmp_lt(offrel
, offset
, size
, v
)
671 enum e_offrel offrel
;
675 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
678 static struct block
*
679 gen_cmp_le(offrel
, offset
, size
, v
)
680 enum e_offrel offrel
;
684 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
687 static struct block
*
688 gen_mcmp(offrel
, offset
, size
, v
, mask
)
689 enum e_offrel offrel
;
694 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
697 static struct block
*
698 gen_bcmp(offrel
, offset
, size
, v
)
699 enum e_offrel offrel
;
700 register u_int offset
, size
;
701 register const u_char
*v
;
703 register struct block
*b
, *tmp
;
707 register const u_char
*p
= &v
[size
- 4];
708 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
709 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
711 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
718 register const u_char
*p
= &v
[size
- 2];
719 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
721 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
728 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
737 * AND the field of size "size" at offset "offset" relative to the header
738 * specified by "offrel" with "mask", and compare it with the value "v"
739 * with the test specified by "jtype"; if "reverse" is true, the test
740 * should test the opposite of "jtype".
742 static struct block
*
743 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
744 enum e_offrel offrel
;
746 bpf_u_int32 offset
, size
, mask
, jtype
;
749 struct slist
*s
, *s2
;
752 s
= gen_load_a(offrel
, offset
, size
);
754 if (mask
!= 0xffffffff) {
755 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
760 b
= new_block(JMP(jtype
));
763 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
769 * Various code constructs need to know the layout of the data link
770 * layer. These variables give the necessary offsets from the beginning
771 * of the packet data.
775 * This is the offset of the beginning of the link-layer header from
776 * the beginning of the raw packet data.
778 * It's usually 0, except for 802.11 with a fixed-length radio header.
779 * (For 802.11 with a variable-length radio header, we have to generate
780 * code to compute that offset; off_ll is 0 in that case.)
785 * If there's a variable-length header preceding the link-layer header,
786 * "reg_off_ll" is the register number for a register containing the
787 * length of that header, and therefore the offset of the link-layer
788 * header from the beginning of the raw packet data. Otherwise,
789 * "reg_off_ll" is -1.
791 static int reg_off_ll
;
794 * This is the offset of the beginning of the MAC-layer header from
795 * the beginning of the link-layer header.
796 * It's usually 0, except for ATM LANE, where it's the offset, relative
797 * to the beginning of the raw packet data, of the Ethernet header, and
798 * for Ethernet with various additional information.
800 static u_int off_mac
;
803 * This is the offset of the beginning of the MAC-layer payload,
804 * from the beginning of the raw packet data.
806 * I.e., it's the sum of the length of the link-layer header (without,
807 * for example, any 802.2 LLC header, so it's the MAC-layer
808 * portion of that header), plus any prefix preceding the
811 static u_int off_macpl
;
814 * This is 1 if the offset of the beginning of the MAC-layer payload
815 * from the beginning of the link-layer header is variable-length.
817 static int off_macpl_is_variable
;
820 * If the link layer has variable_length headers, "reg_off_macpl"
821 * is the register number for a register containing the length of the
822 * link-layer header plus the length of any variable-length header
823 * preceding the link-layer header. Otherwise, "reg_off_macpl"
826 static int reg_off_macpl
;
829 * "off_linktype" is the offset to information in the link-layer header
830 * giving the packet type. This offset is relative to the beginning
831 * of the link-layer header (i.e., it doesn't include off_ll).
833 * For Ethernet, it's the offset of the Ethernet type field.
835 * For link-layer types that always use 802.2 headers, it's the
836 * offset of the LLC header.
838 * For PPP, it's the offset of the PPP type field.
840 * For Cisco HDLC, it's the offset of the CHDLC type field.
842 * For BSD loopback, it's the offset of the AF_ value.
844 * For Linux cooked sockets, it's the offset of the type field.
846 * It's set to -1 for no encapsulation, in which case, IP is assumed.
848 static u_int off_linktype
;
851 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
852 * checks to check the PPP header, assumed to follow a LAN-style link-
853 * layer header and a PPPoE session header.
855 static int is_pppoes
= 0;
858 * TRUE if the link layer includes an ATM pseudo-header.
860 static int is_atm
= 0;
863 * TRUE if "lane" appeared in the filter; it causes us to generate
864 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
866 static int is_lane
= 0;
869 * These are offsets for the ATM pseudo-header.
871 static u_int off_vpi
;
872 static u_int off_vci
;
873 static u_int off_proto
;
876 * These are offsets for the MTP2 fields.
879 static u_int off_li_hsl
;
882 * These are offsets for the MTP3 fields.
884 static u_int off_sio
;
885 static u_int off_opc
;
886 static u_int off_dpc
;
887 static u_int off_sls
;
890 * This is the offset of the first byte after the ATM pseudo_header,
891 * or -1 if there is no ATM pseudo-header.
893 static u_int off_payload
;
896 * These are offsets to the beginning of the network-layer header.
897 * They are relative to the beginning of the MAC-layer payload (i.e.,
898 * they don't include off_ll or off_macpl).
900 * If the link layer never uses 802.2 LLC:
902 * "off_nl" and "off_nl_nosnap" are the same.
904 * If the link layer always uses 802.2 LLC:
906 * "off_nl" is the offset if there's a SNAP header following
909 * "off_nl_nosnap" is the offset if there's no SNAP header.
911 * If the link layer is Ethernet:
913 * "off_nl" is the offset if the packet is an Ethernet II packet
914 * (we assume no 802.3+802.2+SNAP);
916 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
917 * with an 802.2 header following it.
920 static u_int off_nl_nosnap
;
928 linktype
= pcap_datalink(p
);
930 pcap_fddipad
= p
->fddipad
;
934 * Assume it's not raw ATM with a pseudo-header, for now.
945 * And that we're not doing PPPoE.
950 * And assume we're not doing SS7.
960 * Also assume it's not 802.11.
964 off_macpl_is_variable
= 0;
968 label_stack_depth
= 0;
978 off_nl
= 0; /* XXX in reality, variable! */
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
982 case DLT_ARCNET_LINUX
:
985 off_nl
= 0; /* XXX in reality, variable! */
986 off_nl_nosnap
= 0; /* no 802.2 LLC */
991 off_macpl
= 14; /* Ethernet header length */
992 off_nl
= 0; /* Ethernet II */
993 off_nl_nosnap
= 3; /* 802.3+802.2 */
998 * SLIP doesn't have a link level type. The 16 byte
999 * header is hacked into our SLIP driver.
1004 off_nl_nosnap
= 0; /* no 802.2 LLC */
1007 case DLT_SLIP_BSDOS
:
1008 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1021 off_nl_nosnap
= 0; /* no 802.2 LLC */
1028 off_nl_nosnap
= 0; /* no 802.2 LLC */
1033 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1034 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1038 off_nl_nosnap
= 0; /* no 802.2 LLC */
1043 * This does no include the Ethernet header, and
1044 * only covers session state.
1049 off_nl_nosnap
= 0; /* no 802.2 LLC */
1056 off_nl_nosnap
= 0; /* no 802.2 LLC */
1061 * FDDI doesn't really have a link-level type field.
1062 * We set "off_linktype" to the offset of the LLC header.
1064 * To check for Ethernet types, we assume that SSAP = SNAP
1065 * is being used and pick out the encapsulated Ethernet type.
1066 * XXX - should we generate code to check for SNAP?
1070 off_linktype
+= pcap_fddipad
;
1072 off_macpl
= 13; /* FDDI MAC header length */
1074 off_macpl
+= pcap_fddipad
;
1076 off_nl
= 8; /* 802.2+SNAP */
1077 off_nl_nosnap
= 3; /* 802.2 */
1082 * Token Ring doesn't really have a link-level type field.
1083 * We set "off_linktype" to the offset of the LLC header.
1085 * To check for Ethernet types, we assume that SSAP = SNAP
1086 * is being used and pick out the encapsulated Ethernet type.
1087 * XXX - should we generate code to check for SNAP?
1089 * XXX - the header is actually variable-length.
1090 * Some various Linux patched versions gave 38
1091 * as "off_linktype" and 40 as "off_nl"; however,
1092 * if a token ring packet has *no* routing
1093 * information, i.e. is not source-routed, the correct
1094 * values are 20 and 22, as they are in the vanilla code.
1096 * A packet is source-routed iff the uppermost bit
1097 * of the first byte of the source address, at an
1098 * offset of 8, has the uppermost bit set. If the
1099 * packet is source-routed, the total number of bytes
1100 * of routing information is 2 plus bits 0x1F00 of
1101 * the 16-bit value at an offset of 14 (shifted right
1102 * 8 - figure out which byte that is).
1105 off_macpl
= 14; /* Token Ring MAC header length */
1106 off_nl
= 8; /* 802.2+SNAP */
1107 off_nl_nosnap
= 3; /* 802.2 */
1110 case DLT_IEEE802_11
:
1111 case DLT_PRISM_HEADER
:
1112 case DLT_IEEE802_11_RADIO_AVS
:
1113 case DLT_IEEE802_11_RADIO
:
1115 * 802.11 doesn't really have a link-level type field.
1116 * We set "off_linktype" to the offset of the LLC header.
1118 * To check for Ethernet types, we assume that SSAP = SNAP
1119 * is being used and pick out the encapsulated Ethernet type.
1120 * XXX - should we generate code to check for SNAP?
1122 * We also handle variable-length radio headers here.
1123 * The Prism header is in theory variable-length, but in
1124 * practice it's always 144 bytes long. However, some
1125 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1126 * sometimes or always supply an AVS header, so we
1127 * have to check whether the radio header is a Prism
1128 * header or an AVS header, so, in practice, it's
1132 off_macpl
= 0; /* link-layer header is variable-length */
1133 off_macpl_is_variable
= 1;
1134 off_nl
= 8; /* 802.2+SNAP */
1135 off_nl_nosnap
= 3; /* 802.2 */
1140 * At the moment we treat PPI the same way that we treat
1141 * normal Radiotap encoded packets. The difference is in
1142 * the function that generates the code at the beginning
1143 * to compute the header length. Since this code generator
1144 * of PPI supports bare 802.11 encapsulation only (i.e.
1145 * the encapsulated DLT should be DLT_IEEE802_11) we
1146 * generate code to check for this too.
1149 off_macpl
= 0; /* link-layer header is variable-length */
1150 off_macpl_is_variable
= 1;
1151 off_nl
= 8; /* 802.2+SNAP */
1152 off_nl_nosnap
= 3; /* 802.2 */
1155 case DLT_ATM_RFC1483
:
1156 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1158 * assume routed, non-ISO PDUs
1159 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1161 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1162 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1163 * latter would presumably be treated the way PPPoE
1164 * should be, so you can do "pppoe and udp port 2049"
1165 * or "pppoa and tcp port 80" and have it check for
1166 * PPPo{A,E} and a PPP protocol of IP and....
1169 off_macpl
= 0; /* packet begins with LLC header */
1170 off_nl
= 8; /* 802.2+SNAP */
1171 off_nl_nosnap
= 3; /* 802.2 */
1176 * Full Frontal ATM; you get AALn PDUs with an ATM
1180 off_vpi
= SUNATM_VPI_POS
;
1181 off_vci
= SUNATM_VCI_POS
;
1182 off_proto
= PROTO_POS
;
1183 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1184 off_payload
= SUNATM_PKT_BEGIN_POS
;
1185 off_linktype
= off_payload
;
1186 off_macpl
= off_payload
; /* if LLC-encapsulated */
1187 off_nl
= 8; /* 802.2+SNAP */
1188 off_nl_nosnap
= 3; /* 802.2 */
1197 off_nl_nosnap
= 0; /* no 802.2 LLC */
1200 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1204 off_nl_nosnap
= 0; /* no 802.2 LLC */
1209 * LocalTalk does have a 1-byte type field in the LLAP header,
1210 * but really it just indicates whether there is a "short" or
1211 * "long" DDP packet following.
1216 off_nl_nosnap
= 0; /* no 802.2 LLC */
1219 case DLT_IP_OVER_FC
:
1221 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1222 * link-level type field. We set "off_linktype" to the
1223 * offset of the LLC header.
1225 * To check for Ethernet types, we assume that SSAP = SNAP
1226 * is being used and pick out the encapsulated Ethernet type.
1227 * XXX - should we generate code to check for SNAP? RFC
1228 * 2625 says SNAP should be used.
1232 off_nl
= 8; /* 802.2+SNAP */
1233 off_nl_nosnap
= 3; /* 802.2 */
1238 * XXX - we should set this to handle SNAP-encapsulated
1239 * frames (NLPID of 0x80).
1244 off_nl_nosnap
= 0; /* no 802.2 LLC */
1248 * the only BPF-interesting FRF.16 frames are non-control frames;
1249 * Frame Relay has a variable length link-layer
1250 * so lets start with offset 4 for now and increments later on (FIXME);
1256 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1259 case DLT_APPLE_IP_OVER_IEEE1394
:
1263 off_nl_nosnap
= 0; /* no 802.2 LLC */
1266 case DLT_SYMANTEC_FIREWALL
:
1269 off_nl
= 0; /* Ethernet II */
1270 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1273 #ifdef HAVE_NET_PFVAR_H
1276 off_macpl
= PFLOG_HDRLEN
;
1278 off_nl_nosnap
= 0; /* no 802.2 LLC */
1282 case DLT_JUNIPER_MFR
:
1283 case DLT_JUNIPER_MLFR
:
1284 case DLT_JUNIPER_MLPPP
:
1285 case DLT_JUNIPER_PPP
:
1286 case DLT_JUNIPER_CHDLC
:
1287 case DLT_JUNIPER_FRELAY
:
1291 off_nl_nosnap
= -1; /* no 802.2 LLC */
1294 case DLT_JUNIPER_ATM1
:
1295 off_linktype
= 4; /* in reality variable between 4-8 */
1296 off_macpl
= 4; /* in reality variable between 4-8 */
1301 case DLT_JUNIPER_ATM2
:
1302 off_linktype
= 8; /* in reality variable between 8-12 */
1303 off_macpl
= 8; /* in reality variable between 8-12 */
1308 /* frames captured on a Juniper PPPoE service PIC
1309 * contain raw ethernet frames */
1310 case DLT_JUNIPER_PPPOE
:
1311 case DLT_JUNIPER_ETHER
:
1314 off_nl
= 18; /* Ethernet II */
1315 off_nl_nosnap
= 21; /* 802.3+802.2 */
1318 case DLT_JUNIPER_PPPOE_ATM
:
1322 off_nl_nosnap
= -1; /* no 802.2 LLC */
1325 case DLT_JUNIPER_GGSN
:
1329 off_nl_nosnap
= -1; /* no 802.2 LLC */
1332 case DLT_JUNIPER_ES
:
1334 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1335 off_nl
= -1; /* not really a network layer but raw IP addresses */
1336 off_nl_nosnap
= -1; /* no 802.2 LLC */
1339 case DLT_JUNIPER_MONITOR
:
1342 off_nl
= 0; /* raw IP/IP6 header */
1343 off_nl_nosnap
= -1; /* no 802.2 LLC */
1346 case DLT_JUNIPER_SERVICES
:
1348 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1349 off_nl
= -1; /* L3 proto location dep. on cookie type */
1350 off_nl_nosnap
= -1; /* no 802.2 LLC */
1353 case DLT_JUNIPER_VP
:
1360 case DLT_JUNIPER_ST
:
1367 case DLT_JUNIPER_ISM
:
1374 case DLT_JUNIPER_VS
:
1375 case DLT_JUNIPER_SRX_E2E
:
1376 case DLT_JUNIPER_FIBRECHANNEL
:
1377 case DLT_JUNIPER_ATM_CEMIC
:
1397 case DLT_MTP2_WITH_PHDR
:
1432 * Currently, only raw "link[N:M]" filtering is supported.
1434 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1436 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1437 off_nl_nosnap
= -1; /* no 802.2 LLC */
1438 off_mac
= 1; /* step over the kiss length byte */
1443 off_macpl
= 24; /* ipnet header length */
1448 case DLT_NETANALYZER
:
1449 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1450 off_linktype
= 16; /* includes 4-byte pseudo-header */
1451 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1452 off_nl
= 0; /* Ethernet II */
1453 off_nl_nosnap
= 3; /* 802.3+802.2 */
1456 case DLT_NETANALYZER_TRANSPARENT
:
1457 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1458 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1459 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1460 off_nl
= 0; /* Ethernet II */
1461 off_nl_nosnap
= 3; /* 802.3+802.2 */
1466 * For values in the range in which we've assigned new
1467 * DLT_ values, only raw "link[N:M]" filtering is supported.
1469 if (linktype
>= DLT_MATCHING_MIN
&&
1470 linktype
<= DLT_MATCHING_MAX
) {
1479 bpf_error("unknown data link type %d", linktype
);
1484 * Load a value relative to the beginning of the link-layer header.
1485 * The link-layer header doesn't necessarily begin at the beginning
1486 * of the packet data; there might be a variable-length prefix containing
1487 * radio information.
1489 static struct slist
*
1490 gen_load_llrel(offset
, size
)
1493 struct slist
*s
, *s2
;
1495 s
= gen_llprefixlen();
1498 * If "s" is non-null, it has code to arrange that the X register
1499 * contains the length of the prefix preceding the link-layer
1502 * Otherwise, the length of the prefix preceding the link-layer
1503 * header is "off_ll".
1507 * There's a variable-length prefix preceding the
1508 * link-layer header. "s" points to a list of statements
1509 * that put the length of that prefix into the X register.
1510 * do an indirect load, to use the X register as an offset.
1512 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1517 * There is no variable-length header preceding the
1518 * link-layer header; add in off_ll, which, if there's
1519 * a fixed-length header preceding the link-layer header,
1520 * is the length of that header.
1522 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1523 s
->s
.k
= offset
+ off_ll
;
1529 * Load a value relative to the beginning of the MAC-layer payload.
1531 static struct slist
*
1532 gen_load_macplrel(offset
, size
)
1535 struct slist
*s
, *s2
;
1537 s
= gen_off_macpl();
1540 * If s is non-null, the offset of the MAC-layer payload is
1541 * variable, and s points to a list of instructions that
1542 * arrange that the X register contains that offset.
1544 * Otherwise, the offset of the MAC-layer payload is constant,
1545 * and is in off_macpl.
1549 * The offset of the MAC-layer payload is in the X
1550 * register. Do an indirect load, to use the X register
1553 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1558 * The offset of the MAC-layer payload is constant,
1559 * and is in off_macpl; load the value at that offset
1560 * plus the specified offset.
1562 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1563 s
->s
.k
= off_macpl
+ offset
;
1569 * Load a value relative to the beginning of the specified header.
1571 static struct slist
*
1572 gen_load_a(offrel
, offset
, size
)
1573 enum e_offrel offrel
;
1576 struct slist
*s
, *s2
;
1581 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1586 s
= gen_load_llrel(offset
, size
);
1590 s
= gen_load_macplrel(offset
, size
);
1594 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1598 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1603 * Load the X register with the length of the IPv4 header
1604 * (plus the offset of the link-layer header, if it's
1605 * preceded by a variable-length header such as a radio
1606 * header), in bytes.
1608 s
= gen_loadx_iphdrlen();
1611 * Load the item at {offset of the MAC-layer payload} +
1612 * {offset, relative to the start of the MAC-layer
1613 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1614 * {specified offset}.
1616 * (If the offset of the MAC-layer payload is variable,
1617 * it's included in the value in the X register, and
1620 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1621 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1626 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1637 * Generate code to load into the X register the sum of the length of
1638 * the IPv4 header and any variable-length header preceding the link-layer
1641 static struct slist
*
1642 gen_loadx_iphdrlen()
1644 struct slist
*s
, *s2
;
1646 s
= gen_off_macpl();
1649 * There's a variable-length prefix preceding the
1650 * link-layer header, or the link-layer header is itself
1651 * variable-length. "s" points to a list of statements
1652 * that put the offset of the MAC-layer payload into
1655 * The 4*([k]&0xf) addressing mode can't be used, as we
1656 * don't have a constant offset, so we have to load the
1657 * value in question into the A register and add to it
1658 * the value from the X register.
1660 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1663 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1666 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1671 * The A register now contains the length of the
1672 * IP header. We need to add to it the offset of
1673 * the MAC-layer payload, which is still in the X
1674 * register, and move the result into the X register.
1676 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1677 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1680 * There is no variable-length header preceding the
1681 * link-layer header, and the link-layer header is
1682 * fixed-length; load the length of the IPv4 header,
1683 * which is at an offset of off_nl from the beginning
1684 * of the MAC-layer payload, and thus at an offset
1685 * of off_mac_pl + off_nl from the beginning of the
1688 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1689 s
->s
.k
= off_macpl
+ off_nl
;
1694 static struct block
*
1701 s
= new_stmt(BPF_LD
|BPF_IMM
);
1703 b
= new_block(JMP(BPF_JEQ
));
1709 static inline struct block
*
1712 return gen_uncond(1);
1715 static inline struct block
*
1718 return gen_uncond(0);
1722 * Byte-swap a 32-bit number.
1723 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1724 * big-endian platforms.)
1726 #define SWAPLONG(y) \
1727 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1730 * Generate code to match a particular packet type.
1732 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1733 * value, if <= ETHERMTU. We use that to determine whether to
1734 * match the type/length field or to check the type/length field for
1735 * a value <= ETHERMTU to see whether it's a type field and then do
1736 * the appropriate test.
1738 static struct block
*
1739 gen_ether_linktype(proto
)
1742 struct block
*b0
, *b1
;
1748 case LLCSAP_NETBEUI
:
1750 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1751 * so we check the DSAP and SSAP.
1753 * LLCSAP_IP checks for IP-over-802.2, rather
1754 * than IP-over-Ethernet or IP-over-SNAP.
1756 * XXX - should we check both the DSAP and the
1757 * SSAP, like this, or should we check just the
1758 * DSAP, as we do for other types <= ETHERMTU
1759 * (i.e., other SAP values)?
1761 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1763 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1764 ((proto
<< 8) | proto
));
1772 * Ethernet_II frames, which are Ethernet
1773 * frames with a frame type of ETHERTYPE_IPX;
1775 * Ethernet_802.3 frames, which are 802.3
1776 * frames (i.e., the type/length field is
1777 * a length field, <= ETHERMTU, rather than
1778 * a type field) with the first two bytes
1779 * after the Ethernet/802.3 header being
1782 * Ethernet_802.2 frames, which are 802.3
1783 * frames with an 802.2 LLC header and
1784 * with the IPX LSAP as the DSAP in the LLC
1787 * Ethernet_SNAP frames, which are 802.3
1788 * frames with an LLC header and a SNAP
1789 * header and with an OUI of 0x000000
1790 * (encapsulated Ethernet) and a protocol
1791 * ID of ETHERTYPE_IPX in the SNAP header.
1793 * XXX - should we generate the same code both
1794 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1798 * This generates code to check both for the
1799 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1801 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1802 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1806 * Now we add code to check for SNAP frames with
1807 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1809 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1813 * Now we generate code to check for 802.3
1814 * frames in general.
1816 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1820 * Now add the check for 802.3 frames before the
1821 * check for Ethernet_802.2 and Ethernet_802.3,
1822 * as those checks should only be done on 802.3
1823 * frames, not on Ethernet frames.
1828 * Now add the check for Ethernet_II frames, and
1829 * do that before checking for the other frame
1832 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1833 (bpf_int32
)ETHERTYPE_IPX
);
1837 case ETHERTYPE_ATALK
:
1838 case ETHERTYPE_AARP
:
1840 * EtherTalk (AppleTalk protocols on Ethernet link
1841 * layer) may use 802.2 encapsulation.
1845 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1846 * we check for an Ethernet type field less than
1847 * 1500, which means it's an 802.3 length field.
1849 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1853 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1854 * SNAP packets with an organization code of
1855 * 0x080007 (Apple, for Appletalk) and a protocol
1856 * type of ETHERTYPE_ATALK (Appletalk).
1858 * 802.2-encapsulated ETHERTYPE_AARP packets are
1859 * SNAP packets with an organization code of
1860 * 0x000000 (encapsulated Ethernet) and a protocol
1861 * type of ETHERTYPE_AARP (Appletalk ARP).
1863 if (proto
== ETHERTYPE_ATALK
)
1864 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1865 else /* proto == ETHERTYPE_AARP */
1866 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1870 * Check for Ethernet encapsulation (Ethertalk
1871 * phase 1?); we just check for the Ethernet
1874 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1880 if (proto
<= ETHERMTU
) {
1882 * This is an LLC SAP value, so the frames
1883 * that match would be 802.2 frames.
1884 * Check that the frame is an 802.2 frame
1885 * (i.e., that the length/type field is
1886 * a length field, <= ETHERMTU) and
1887 * then check the DSAP.
1889 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1891 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1897 * This is an Ethernet type, so compare
1898 * the length/type field with it (if
1899 * the frame is an 802.2 frame, the length
1900 * field will be <= ETHERMTU, and, as
1901 * "proto" is > ETHERMTU, this test
1902 * will fail and the frame won't match,
1903 * which is what we want).
1905 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1912 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1913 * or IPv6 then we have an error.
1915 static struct block
*
1916 gen_ipnet_linktype(proto
)
1922 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1923 (bpf_int32
)IPH_AF_INET
);
1926 case ETHERTYPE_IPV6
:
1927 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1928 (bpf_int32
)IPH_AF_INET6
);
1939 * Generate code to match a particular packet type.
1941 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1942 * value, if <= ETHERMTU. We use that to determine whether to
1943 * match the type field or to check the type field for the special
1944 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1946 static struct block
*
1947 gen_linux_sll_linktype(proto
)
1950 struct block
*b0
, *b1
;
1956 case LLCSAP_NETBEUI
:
1958 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1959 * so we check the DSAP and SSAP.
1961 * LLCSAP_IP checks for IP-over-802.2, rather
1962 * than IP-over-Ethernet or IP-over-SNAP.
1964 * XXX - should we check both the DSAP and the
1965 * SSAP, like this, or should we check just the
1966 * DSAP, as we do for other types <= ETHERMTU
1967 * (i.e., other SAP values)?
1969 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1970 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1971 ((proto
<< 8) | proto
));
1977 * Ethernet_II frames, which are Ethernet
1978 * frames with a frame type of ETHERTYPE_IPX;
1980 * Ethernet_802.3 frames, which have a frame
1981 * type of LINUX_SLL_P_802_3;
1983 * Ethernet_802.2 frames, which are 802.3
1984 * frames with an 802.2 LLC header (i.e, have
1985 * a frame type of LINUX_SLL_P_802_2) and
1986 * with the IPX LSAP as the DSAP in the LLC
1989 * Ethernet_SNAP frames, which are 802.3
1990 * frames with an LLC header and a SNAP
1991 * header and with an OUI of 0x000000
1992 * (encapsulated Ethernet) and a protocol
1993 * ID of ETHERTYPE_IPX in the SNAP header.
1995 * First, do the checks on LINUX_SLL_P_802_2
1996 * frames; generate the check for either
1997 * Ethernet_802.2 or Ethernet_SNAP frames, and
1998 * then put a check for LINUX_SLL_P_802_2 frames
2001 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2002 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2004 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2008 * Now check for 802.3 frames and OR that with
2009 * the previous test.
2011 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2015 * Now add the check for Ethernet_II frames, and
2016 * do that before checking for the other frame
2019 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2020 (bpf_int32
)ETHERTYPE_IPX
);
2024 case ETHERTYPE_ATALK
:
2025 case ETHERTYPE_AARP
:
2027 * EtherTalk (AppleTalk protocols on Ethernet link
2028 * layer) may use 802.2 encapsulation.
2032 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2033 * we check for the 802.2 protocol type in the
2034 * "Ethernet type" field.
2036 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2039 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2040 * SNAP packets with an organization code of
2041 * 0x080007 (Apple, for Appletalk) and a protocol
2042 * type of ETHERTYPE_ATALK (Appletalk).
2044 * 802.2-encapsulated ETHERTYPE_AARP packets are
2045 * SNAP packets with an organization code of
2046 * 0x000000 (encapsulated Ethernet) and a protocol
2047 * type of ETHERTYPE_AARP (Appletalk ARP).
2049 if (proto
== ETHERTYPE_ATALK
)
2050 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2051 else /* proto == ETHERTYPE_AARP */
2052 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2056 * Check for Ethernet encapsulation (Ethertalk
2057 * phase 1?); we just check for the Ethernet
2060 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2066 if (proto
<= ETHERMTU
) {
2068 * This is an LLC SAP value, so the frames
2069 * that match would be 802.2 frames.
2070 * Check for the 802.2 protocol type
2071 * in the "Ethernet type" field, and
2072 * then check the DSAP.
2074 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2076 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2082 * This is an Ethernet type, so compare
2083 * the length/type field with it (if
2084 * the frame is an 802.2 frame, the length
2085 * field will be <= ETHERMTU, and, as
2086 * "proto" is > ETHERMTU, this test
2087 * will fail and the frame won't match,
2088 * which is what we want).
2090 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2096 static struct slist
*
2097 gen_load_prism_llprefixlen()
2099 struct slist
*s1
, *s2
;
2100 struct slist
*sjeq_avs_cookie
;
2101 struct slist
*sjcommon
;
2104 * This code is not compatible with the optimizer, as
2105 * we are generating jmp instructions within a normal
2106 * slist of instructions
2111 * Generate code to load the length of the radio header into
2112 * the register assigned to hold that length, if one has been
2113 * assigned. (If one hasn't been assigned, no code we've
2114 * generated uses that prefix, so we don't need to generate any
2117 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2118 * or always use the AVS header rather than the Prism header.
2119 * We load a 4-byte big-endian value at the beginning of the
2120 * raw packet data, and see whether, when masked with 0xFFFFF000,
2121 * it's equal to 0x80211000. If so, that indicates that it's
2122 * an AVS header (the masked-out bits are the version number).
2123 * Otherwise, it's a Prism header.
2125 * XXX - the Prism header is also, in theory, variable-length,
2126 * but no known software generates headers that aren't 144
2129 if (reg_off_ll
!= -1) {
2133 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2137 * AND it with 0xFFFFF000.
2139 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2140 s2
->s
.k
= 0xFFFFF000;
2144 * Compare with 0x80211000.
2146 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2147 sjeq_avs_cookie
->s
.k
= 0x80211000;
2148 sappend(s1
, sjeq_avs_cookie
);
2153 * The 4 bytes at an offset of 4 from the beginning of
2154 * the AVS header are the length of the AVS header.
2155 * That field is big-endian.
2157 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2160 sjeq_avs_cookie
->s
.jt
= s2
;
2163 * Now jump to the code to allocate a register
2164 * into which to save the header length and
2165 * store the length there. (The "jump always"
2166 * instruction needs to have the k field set;
2167 * it's added to the PC, so, as we're jumping
2168 * over a single instruction, it should be 1.)
2170 sjcommon
= new_stmt(JMP(BPF_JA
));
2172 sappend(s1
, sjcommon
);
2175 * Now for the code that handles the Prism header.
2176 * Just load the length of the Prism header (144)
2177 * into the A register. Have the test for an AVS
2178 * header branch here if we don't have an AVS header.
2180 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2183 sjeq_avs_cookie
->s
.jf
= s2
;
2186 * Now allocate a register to hold that value and store
2187 * it. The code for the AVS header will jump here after
2188 * loading the length of the AVS header.
2190 s2
= new_stmt(BPF_ST
);
2191 s2
->s
.k
= reg_off_ll
;
2193 sjcommon
->s
.jf
= s2
;
2196 * Now move it into the X register.
2198 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2206 static struct slist
*
2207 gen_load_avs_llprefixlen()
2209 struct slist
*s1
, *s2
;
2212 * Generate code to load the length of the AVS header into
2213 * the register assigned to hold that length, if one has been
2214 * assigned. (If one hasn't been assigned, no code we've
2215 * generated uses that prefix, so we don't need to generate any
2218 if (reg_off_ll
!= -1) {
2220 * The 4 bytes at an offset of 4 from the beginning of
2221 * the AVS header are the length of the AVS header.
2222 * That field is big-endian.
2224 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2228 * Now allocate a register to hold that value and store
2231 s2
= new_stmt(BPF_ST
);
2232 s2
->s
.k
= reg_off_ll
;
2236 * Now move it into the X register.
2238 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2246 static struct slist
*
2247 gen_load_radiotap_llprefixlen()
2249 struct slist
*s1
, *s2
;
2252 * Generate code to load the length of the radiotap header into
2253 * the register assigned to hold that length, if one has been
2254 * assigned. (If one hasn't been assigned, no code we've
2255 * generated uses that prefix, so we don't need to generate any
2258 if (reg_off_ll
!= -1) {
2260 * The 2 bytes at offsets of 2 and 3 from the beginning
2261 * of the radiotap header are the length of the radiotap
2262 * header; unfortunately, it's little-endian, so we have
2263 * to load it a byte at a time and construct the value.
2267 * Load the high-order byte, at an offset of 3, shift it
2268 * left a byte, and put the result in the X register.
2270 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2272 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2275 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2279 * Load the next byte, at an offset of 2, and OR the
2280 * value from the X register into it.
2282 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2285 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2289 * Now allocate a register to hold that value and store
2292 s2
= new_stmt(BPF_ST
);
2293 s2
->s
.k
= reg_off_ll
;
2297 * Now move it into the X register.
2299 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2308 * At the moment we treat PPI as normal Radiotap encoded
2309 * packets. The difference is in the function that generates
2310 * the code at the beginning to compute the header length.
2311 * Since this code generator of PPI supports bare 802.11
2312 * encapsulation only (i.e. the encapsulated DLT should be
2313 * DLT_IEEE802_11) we generate code to check for this too;
2314 * that's done in finish_parse().
2316 static struct slist
*
2317 gen_load_ppi_llprefixlen()
2319 struct slist
*s1
, *s2
;
2322 * Generate code to load the length of the radiotap header
2323 * into the register assigned to hold that length, if one has
2326 if (reg_off_ll
!= -1) {
2328 * The 2 bytes at offsets of 2 and 3 from the beginning
2329 * of the radiotap header are the length of the radiotap
2330 * header; unfortunately, it's little-endian, so we have
2331 * to load it a byte at a time and construct the value.
2335 * Load the high-order byte, at an offset of 3, shift it
2336 * left a byte, and put the result in the X register.
2338 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2340 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2343 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2347 * Load the next byte, at an offset of 2, and OR the
2348 * value from the X register into it.
2350 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2353 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2357 * Now allocate a register to hold that value and store
2360 s2
= new_stmt(BPF_ST
);
2361 s2
->s
.k
= reg_off_ll
;
2365 * Now move it into the X register.
2367 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2376 * Load a value relative to the beginning of the link-layer header after the 802.11
2377 * header, i.e. LLC_SNAP.
2378 * The link-layer header doesn't necessarily begin at the beginning
2379 * of the packet data; there might be a variable-length prefix containing
2380 * radio information.
2382 static struct slist
*
2383 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2386 struct slist
*sjset_data_frame_1
;
2387 struct slist
*sjset_data_frame_2
;
2388 struct slist
*sjset_qos
;
2389 struct slist
*sjset_radiotap_flags
;
2390 struct slist
*sjset_radiotap_tsft
;
2391 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2392 struct slist
*s_roundup
;
2394 if (reg_off_macpl
== -1) {
2396 * No register has been assigned to the offset of
2397 * the MAC-layer payload, which means nobody needs
2398 * it; don't bother computing it - just return
2399 * what we already have.
2405 * This code is not compatible with the optimizer, as
2406 * we are generating jmp instructions within a normal
2407 * slist of instructions
2412 * If "s" is non-null, it has code to arrange that the X register
2413 * contains the length of the prefix preceding the link-layer
2416 * Otherwise, the length of the prefix preceding the link-layer
2417 * header is "off_ll".
2421 * There is no variable-length header preceding the
2422 * link-layer header.
2424 * Load the length of the fixed-length prefix preceding
2425 * the link-layer header (if any) into the X register,
2426 * and store it in the reg_off_macpl register.
2427 * That length is off_ll.
2429 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2434 * The X register contains the offset of the beginning of the
2435 * link-layer header; add 24, which is the minimum length
2436 * of the MAC header for a data frame, to that, and store it
2437 * in reg_off_macpl, and then load the Frame Control field,
2438 * which is at the offset in the X register, with an indexed load.
2440 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2442 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2445 s2
= new_stmt(BPF_ST
);
2446 s2
->s
.k
= reg_off_macpl
;
2449 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2454 * Check the Frame Control field to see if this is a data frame;
2455 * a data frame has the 0x08 bit (b3) in that field set and the
2456 * 0x04 bit (b2) clear.
2458 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2459 sjset_data_frame_1
->s
.k
= 0x08;
2460 sappend(s
, sjset_data_frame_1
);
2463 * If b3 is set, test b2, otherwise go to the first statement of
2464 * the rest of the program.
2466 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2467 sjset_data_frame_2
->s
.k
= 0x04;
2468 sappend(s
, sjset_data_frame_2
);
2469 sjset_data_frame_1
->s
.jf
= snext
;
2472 * If b2 is not set, this is a data frame; test the QoS bit.
2473 * Otherwise, go to the first statement of the rest of the
2476 sjset_data_frame_2
->s
.jt
= snext
;
2477 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2478 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2479 sappend(s
, sjset_qos
);
2482 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2484 * Otherwise, go to the first statement of the rest of the
2487 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2488 s2
->s
.k
= reg_off_macpl
;
2490 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2493 s2
= new_stmt(BPF_ST
);
2494 s2
->s
.k
= reg_off_macpl
;
2498 * If we have a radiotap header, look at it to see whether
2499 * there's Atheros padding between the MAC-layer header
2502 * Note: all of the fields in the radiotap header are
2503 * little-endian, so we byte-swap all of the values
2504 * we test against, as they will be loaded as big-endian
2507 if (linktype
== DLT_IEEE802_11_RADIO
) {
2509 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2510 * in the presence flag?
2512 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2516 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2517 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2518 sappend(s
, sjset_radiotap_flags
);
2521 * If not, skip all of this.
2523 sjset_radiotap_flags
->s
.jf
= snext
;
2526 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2528 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2529 new_stmt(JMP(BPF_JSET
));
2530 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2531 sappend(s
, sjset_radiotap_tsft
);
2534 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2535 * at an offset of 16 from the beginning of the raw packet
2536 * data (8 bytes for the radiotap header and 8 bytes for
2539 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2542 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2546 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2547 sjset_tsft_datapad
->s
.k
= 0x20;
2548 sappend(s
, sjset_tsft_datapad
);
2551 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2552 * at an offset of 8 from the beginning of the raw packet
2553 * data (8 bytes for the radiotap header).
2555 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2558 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2562 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2563 sjset_notsft_datapad
->s
.k
= 0x20;
2564 sappend(s
, sjset_notsft_datapad
);
2567 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2568 * set, round the length of the 802.11 header to
2569 * a multiple of 4. Do that by adding 3 and then
2570 * dividing by and multiplying by 4, which we do by
2573 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2574 s_roundup
->s
.k
= reg_off_macpl
;
2575 sappend(s
, s_roundup
);
2576 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2579 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2582 s2
= new_stmt(BPF_ST
);
2583 s2
->s
.k
= reg_off_macpl
;
2586 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2587 sjset_tsft_datapad
->s
.jf
= snext
;
2588 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2589 sjset_notsft_datapad
->s
.jf
= snext
;
2591 sjset_qos
->s
.jf
= snext
;
2597 insert_compute_vloffsets(b
)
2603 * For link-layer types that have a variable-length header
2604 * preceding the link-layer header, generate code to load
2605 * the offset of the link-layer header into the register
2606 * assigned to that offset, if any.
2610 case DLT_PRISM_HEADER
:
2611 s
= gen_load_prism_llprefixlen();
2614 case DLT_IEEE802_11_RADIO_AVS
:
2615 s
= gen_load_avs_llprefixlen();
2618 case DLT_IEEE802_11_RADIO
:
2619 s
= gen_load_radiotap_llprefixlen();
2623 s
= gen_load_ppi_llprefixlen();
2632 * For link-layer types that have a variable-length link-layer
2633 * header, generate code to load the offset of the MAC-layer
2634 * payload into the register assigned to that offset, if any.
2638 case DLT_IEEE802_11
:
2639 case DLT_PRISM_HEADER
:
2640 case DLT_IEEE802_11_RADIO_AVS
:
2641 case DLT_IEEE802_11_RADIO
:
2643 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2648 * If we have any offset-loading code, append all the
2649 * existing statements in the block to those statements,
2650 * and make the resulting list the list of statements
2654 sappend(s
, b
->stmts
);
2659 static struct block
*
2660 gen_ppi_dlt_check(void)
2662 struct slist
*s_load_dlt
;
2665 if (linktype
== DLT_PPI
)
2667 /* Create the statements that check for the DLT
2669 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2670 s_load_dlt
->s
.k
= 4;
2672 b
= new_block(JMP(BPF_JEQ
));
2674 b
->stmts
= s_load_dlt
;
2675 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2685 static struct slist
*
2686 gen_prism_llprefixlen(void)
2690 if (reg_off_ll
== -1) {
2692 * We haven't yet assigned a register for the length
2693 * of the radio header; allocate one.
2695 reg_off_ll
= alloc_reg();
2699 * Load the register containing the radio length
2700 * into the X register.
2702 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2703 s
->s
.k
= reg_off_ll
;
2707 static struct slist
*
2708 gen_avs_llprefixlen(void)
2712 if (reg_off_ll
== -1) {
2714 * We haven't yet assigned a register for the length
2715 * of the AVS header; allocate one.
2717 reg_off_ll
= alloc_reg();
2721 * Load the register containing the AVS length
2722 * into the X register.
2724 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2725 s
->s
.k
= reg_off_ll
;
2729 static struct slist
*
2730 gen_radiotap_llprefixlen(void)
2734 if (reg_off_ll
== -1) {
2736 * We haven't yet assigned a register for the length
2737 * of the radiotap header; allocate one.
2739 reg_off_ll
= alloc_reg();
2743 * Load the register containing the radiotap length
2744 * into the X register.
2746 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2747 s
->s
.k
= reg_off_ll
;
2752 * At the moment we treat PPI as normal Radiotap encoded
2753 * packets. The difference is in the function that generates
2754 * the code at the beginning to compute the header length.
2755 * Since this code generator of PPI supports bare 802.11
2756 * encapsulation only (i.e. the encapsulated DLT should be
2757 * DLT_IEEE802_11) we generate code to check for this too.
2759 static struct slist
*
2760 gen_ppi_llprefixlen(void)
2764 if (reg_off_ll
== -1) {
2766 * We haven't yet assigned a register for the length
2767 * of the radiotap header; allocate one.
2769 reg_off_ll
= alloc_reg();
2773 * Load the register containing the PPI length
2774 * into the X register.
2776 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2777 s
->s
.k
= reg_off_ll
;
2782 * Generate code to compute the link-layer header length, if necessary,
2783 * putting it into the X register, and to return either a pointer to a
2784 * "struct slist" for the list of statements in that code, or NULL if
2785 * no code is necessary.
2787 static struct slist
*
2788 gen_llprefixlen(void)
2792 case DLT_PRISM_HEADER
:
2793 return gen_prism_llprefixlen();
2795 case DLT_IEEE802_11_RADIO_AVS
:
2796 return gen_avs_llprefixlen();
2798 case DLT_IEEE802_11_RADIO
:
2799 return gen_radiotap_llprefixlen();
2802 return gen_ppi_llprefixlen();
2810 * Generate code to load the register containing the offset of the
2811 * MAC-layer payload into the X register; if no register for that offset
2812 * has been allocated, allocate it first.
2814 static struct slist
*
2819 if (off_macpl_is_variable
) {
2820 if (reg_off_macpl
== -1) {
2822 * We haven't yet assigned a register for the offset
2823 * of the MAC-layer payload; allocate one.
2825 reg_off_macpl
= alloc_reg();
2829 * Load the register containing the offset of the MAC-layer
2830 * payload into the X register.
2832 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2833 s
->s
.k
= reg_off_macpl
;
2837 * That offset isn't variable, so we don't need to
2838 * generate any code.
2845 * Map an Ethernet type to the equivalent PPP type.
2848 ethertype_to_ppptype(proto
)
2857 case ETHERTYPE_IPV6
:
2865 case ETHERTYPE_ATALK
:
2879 * I'm assuming the "Bridging PDU"s that go
2880 * over PPP are Spanning Tree Protocol
2894 * Generate code to match a particular packet type by matching the
2895 * link-layer type field or fields in the 802.2 LLC header.
2897 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2898 * value, if <= ETHERMTU.
2900 static struct block
*
2904 struct block
*b0
, *b1
, *b2
;
2906 /* are we checking MPLS-encapsulated packets? */
2907 if (label_stack_depth
> 0) {
2911 /* FIXME add other L3 proto IDs */
2912 return gen_mpls_linktype(Q_IP
);
2914 case ETHERTYPE_IPV6
:
2916 /* FIXME add other L3 proto IDs */
2917 return gen_mpls_linktype(Q_IPV6
);
2920 bpf_error("unsupported protocol over mpls");
2926 * Are we testing PPPoE packets?
2930 * The PPPoE session header is part of the
2931 * MAC-layer payload, so all references
2932 * should be relative to the beginning of
2937 * We use Ethernet protocol types inside libpcap;
2938 * map them to the corresponding PPP protocol types.
2940 proto
= ethertype_to_ppptype(proto
);
2941 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2947 case DLT_NETANALYZER
:
2948 case DLT_NETANALYZER_TRANSPARENT
:
2949 return gen_ether_linktype(proto
);
2957 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2961 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2968 case DLT_IEEE802_11
:
2969 case DLT_PRISM_HEADER
:
2970 case DLT_IEEE802_11_RADIO_AVS
:
2971 case DLT_IEEE802_11_RADIO
:
2974 * Check that we have a data frame.
2976 b0
= gen_check_802_11_data_frame();
2979 * Now check for the specified link-layer type.
2981 b1
= gen_llc_linktype(proto
);
2989 * XXX - check for asynchronous frames, as per RFC 1103.
2991 return gen_llc_linktype(proto
);
2997 * XXX - check for LLC PDUs, as per IEEE 802.5.
2999 return gen_llc_linktype(proto
);
3003 case DLT_ATM_RFC1483
:
3005 case DLT_IP_OVER_FC
:
3006 return gen_llc_linktype(proto
);
3012 * If "is_lane" is set, check for a LANE-encapsulated
3013 * version of this protocol, otherwise check for an
3014 * LLC-encapsulated version of this protocol.
3016 * We assume LANE means Ethernet, not Token Ring.
3020 * Check that the packet doesn't begin with an
3021 * LE Control marker. (We've already generated
3024 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3029 * Now generate an Ethernet test.
3031 b1
= gen_ether_linktype(proto
);
3036 * Check for LLC encapsulation and then check the
3039 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3040 b1
= gen_llc_linktype(proto
);
3048 return gen_linux_sll_linktype(proto
);
3053 case DLT_SLIP_BSDOS
:
3056 * These types don't provide any type field; packets
3057 * are always IPv4 or IPv6.
3059 * XXX - for IPv4, check for a version number of 4, and,
3060 * for IPv6, check for a version number of 6?
3065 /* Check for a version number of 4. */
3066 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3068 case ETHERTYPE_IPV6
:
3069 /* Check for a version number of 6. */
3070 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3073 return gen_false(); /* always false */
3080 * Raw IPv4, so no type field.
3082 if (proto
== ETHERTYPE_IP
)
3083 return gen_true(); /* always true */
3085 /* Checking for something other than IPv4; always false */
3092 * Raw IPv6, so no type field.
3094 if (proto
== ETHERTYPE_IPV6
)
3095 return gen_true(); /* always true */
3097 /* Checking for something other than IPv6; always false */
3104 case DLT_PPP_SERIAL
:
3107 * We use Ethernet protocol types inside libpcap;
3108 * map them to the corresponding PPP protocol types.
3110 proto
= ethertype_to_ppptype(proto
);
3111 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3117 * We use Ethernet protocol types inside libpcap;
3118 * map them to the corresponding PPP protocol types.
3124 * Also check for Van Jacobson-compressed IP.
3125 * XXX - do this for other forms of PPP?
3127 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3128 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3130 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3135 proto
= ethertype_to_ppptype(proto
);
3136 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3146 * For DLT_NULL, the link-layer header is a 32-bit
3147 * word containing an AF_ value in *host* byte order,
3148 * and for DLT_ENC, the link-layer header begins
3149 * with a 32-bit work containing an AF_ value in
3152 * In addition, if we're reading a saved capture file,
3153 * the host byte order in the capture may not be the
3154 * same as the host byte order on this machine.
3156 * For DLT_LOOP, the link-layer header is a 32-bit
3157 * word containing an AF_ value in *network* byte order.
3159 * XXX - AF_ values may, unfortunately, be platform-
3160 * dependent; for example, FreeBSD's AF_INET6 is 24
3161 * whilst NetBSD's and OpenBSD's is 26.
3163 * This means that, when reading a capture file, just
3164 * checking for our AF_INET6 value won't work if the
3165 * capture file came from another OS.
3174 case ETHERTYPE_IPV6
:
3181 * Not a type on which we support filtering.
3182 * XXX - support those that have AF_ values
3183 * #defined on this platform, at least?
3188 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3190 * The AF_ value is in host byte order, but
3191 * the BPF interpreter will convert it to
3192 * network byte order.
3194 * If this is a save file, and it's from a
3195 * machine with the opposite byte order to
3196 * ours, we byte-swap the AF_ value.
3198 * Then we run it through "htonl()", and
3199 * generate code to compare against the result.
3201 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3202 bpf_pcap
->sf
.swapped
)
3203 proto
= SWAPLONG(proto
);
3204 proto
= htonl(proto
);
3206 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3208 #ifdef HAVE_NET_PFVAR_H
3211 * af field is host byte order in contrast to the rest of
3214 if (proto
== ETHERTYPE_IP
)
3215 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3216 BPF_B
, (bpf_int32
)AF_INET
));
3217 else if (proto
== ETHERTYPE_IPV6
)
3218 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3219 BPF_B
, (bpf_int32
)AF_INET6
));
3224 #endif /* HAVE_NET_PFVAR_H */
3227 case DLT_ARCNET_LINUX
:
3229 * XXX should we check for first fragment if the protocol
3237 case ETHERTYPE_IPV6
:
3238 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3239 (bpf_int32
)ARCTYPE_INET6
));
3242 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3243 (bpf_int32
)ARCTYPE_IP
);
3244 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3245 (bpf_int32
)ARCTYPE_IP_OLD
);
3250 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3251 (bpf_int32
)ARCTYPE_ARP
);
3252 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3253 (bpf_int32
)ARCTYPE_ARP_OLD
);
3257 case ETHERTYPE_REVARP
:
3258 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3259 (bpf_int32
)ARCTYPE_REVARP
));
3261 case ETHERTYPE_ATALK
:
3262 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3263 (bpf_int32
)ARCTYPE_ATALK
));
3270 case ETHERTYPE_ATALK
:
3280 * XXX - assumes a 2-byte Frame Relay header with
3281 * DLCI and flags. What if the address is longer?
3287 * Check for the special NLPID for IP.
3289 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3291 case ETHERTYPE_IPV6
:
3293 * Check for the special NLPID for IPv6.
3295 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3299 * Check for several OSI protocols.
3301 * Frame Relay packets typically have an OSI
3302 * NLPID at the beginning; we check for each
3305 * What we check for is the NLPID and a frame
3306 * control field of UI, i.e. 0x03 followed
3309 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3310 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3311 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3323 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3325 case DLT_JUNIPER_MFR
:
3326 case DLT_JUNIPER_MLFR
:
3327 case DLT_JUNIPER_MLPPP
:
3328 case DLT_JUNIPER_ATM1
:
3329 case DLT_JUNIPER_ATM2
:
3330 case DLT_JUNIPER_PPPOE
:
3331 case DLT_JUNIPER_PPPOE_ATM
:
3332 case DLT_JUNIPER_GGSN
:
3333 case DLT_JUNIPER_ES
:
3334 case DLT_JUNIPER_MONITOR
:
3335 case DLT_JUNIPER_SERVICES
:
3336 case DLT_JUNIPER_ETHER
:
3337 case DLT_JUNIPER_PPP
:
3338 case DLT_JUNIPER_FRELAY
:
3339 case DLT_JUNIPER_CHDLC
:
3340 case DLT_JUNIPER_VP
:
3341 case DLT_JUNIPER_ST
:
3342 case DLT_JUNIPER_ISM
:
3343 case DLT_JUNIPER_VS
:
3344 case DLT_JUNIPER_SRX_E2E
:
3345 case DLT_JUNIPER_FIBRECHANNEL
:
3346 case DLT_JUNIPER_ATM_CEMIC
:
3348 /* just lets verify the magic number for now -
3349 * on ATM we may have up to 6 different encapsulations on the wire
3350 * and need a lot of heuristics to figure out that the payload
3353 * FIXME encapsulation specific BPF_ filters
3355 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3358 return gen_ipnet_linktype(proto
);
3360 case DLT_LINUX_IRDA
:
3361 bpf_error("IrDA link-layer type filtering not implemented");
3364 bpf_error("DOCSIS link-layer type filtering not implemented");
3367 case DLT_MTP2_WITH_PHDR
:
3368 bpf_error("MTP2 link-layer type filtering not implemented");
3371 bpf_error("ERF link-layer type filtering not implemented");
3374 bpf_error("PFSYNC link-layer type filtering not implemented");
3376 case DLT_LINUX_LAPD
:
3377 bpf_error("LAPD link-layer type filtering not implemented");
3381 case DLT_USB_LINUX_MMAPPED
:
3382 bpf_error("USB link-layer type filtering not implemented");
3384 case DLT_BLUETOOTH_HCI_H4
:
3385 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3386 bpf_error("Bluetooth link-layer type filtering not implemented");
3389 case DLT_CAN_SOCKETCAN
:
3390 bpf_error("CAN link-layer type filtering not implemented");
3392 case DLT_IEEE802_15_4
:
3393 case DLT_IEEE802_15_4_LINUX
:
3394 case DLT_IEEE802_15_4_NONASK_PHY
:
3395 case DLT_IEEE802_15_4_NOFCS
:
3396 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3398 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3399 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3402 bpf_error("SITA link-layer type filtering not implemented");
3405 bpf_error("RAIF1 link-layer type filtering not implemented");
3408 bpf_error("IPMB link-layer type filtering not implemented");
3411 bpf_error("AX.25 link-layer type filtering not implemented");
3415 * All the types that have no encapsulation should either be
3416 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3417 * all packets are IP packets, or should be handled in some
3418 * special case, if none of them are (if some are and some
3419 * aren't, the lack of encapsulation is a problem, as we'd
3420 * have to find some other way of determining the packet type).
3422 * Therefore, if "off_linktype" is -1, there's an error.
3424 if (off_linktype
== (u_int
)-1)
3428 * Any type not handled above should always have an Ethernet
3429 * type at an offset of "off_linktype".
3431 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3435 * Check for an LLC SNAP packet with a given organization code and
3436 * protocol type; we check the entire contents of the 802.2 LLC and
3437 * snap headers, checking for DSAP and SSAP of SNAP and a control
3438 * field of 0x03 in the LLC header, and for the specified organization
3439 * code and protocol type in the SNAP header.
3441 static struct block
*
3442 gen_snap(orgcode
, ptype
)
3443 bpf_u_int32 orgcode
;
3446 u_char snapblock
[8];
3448 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3449 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3450 snapblock
[2] = 0x03; /* control = UI */
3451 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3452 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3453 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3454 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3455 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3456 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3460 * Generate code to match a particular packet type, for link-layer types
3461 * using 802.2 LLC headers.
3463 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3464 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3466 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3467 * value, if <= ETHERMTU. We use that to determine whether to
3468 * match the DSAP or both DSAP and LSAP or to check the OUI and
3469 * protocol ID in a SNAP header.
3471 static struct block
*
3472 gen_llc_linktype(proto
)
3476 * XXX - handle token-ring variable-length header.
3482 case LLCSAP_NETBEUI
:
3484 * XXX - should we check both the DSAP and the
3485 * SSAP, like this, or should we check just the
3486 * DSAP, as we do for other types <= ETHERMTU
3487 * (i.e., other SAP values)?
3489 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3490 ((proto
<< 8) | proto
));
3494 * XXX - are there ever SNAP frames for IPX on
3495 * non-Ethernet 802.x networks?
3497 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3498 (bpf_int32
)LLCSAP_IPX
);
3500 case ETHERTYPE_ATALK
:
3502 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3503 * SNAP packets with an organization code of
3504 * 0x080007 (Apple, for Appletalk) and a protocol
3505 * type of ETHERTYPE_ATALK (Appletalk).
3507 * XXX - check for an organization code of
3508 * encapsulated Ethernet as well?
3510 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3514 * XXX - we don't have to check for IPX 802.3
3515 * here, but should we check for the IPX Ethertype?
3517 if (proto
<= ETHERMTU
) {
3519 * This is an LLC SAP value, so check
3522 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3525 * This is an Ethernet type; we assume that it's
3526 * unlikely that it'll appear in the right place
3527 * at random, and therefore check only the
3528 * location that would hold the Ethernet type
3529 * in a SNAP frame with an organization code of
3530 * 0x000000 (encapsulated Ethernet).
3532 * XXX - if we were to check for the SNAP DSAP and
3533 * LSAP, as per XXX, and were also to check for an
3534 * organization code of 0x000000 (encapsulated
3535 * Ethernet), we'd do
3537 * return gen_snap(0x000000, proto);
3539 * here; for now, we don't, as per the above.
3540 * I don't know whether it's worth the extra CPU
3541 * time to do the right check or not.
3543 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3548 static struct block
*
3549 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3553 u_int src_off
, dst_off
;
3555 struct block
*b0
, *b1
;
3569 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3570 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3576 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3577 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3584 b0
= gen_linktype(proto
);
3585 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3591 static struct block
*
3592 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3593 struct in6_addr
*addr
;
3594 struct in6_addr
*mask
;
3596 u_int src_off
, dst_off
;
3598 struct block
*b0
, *b1
;
3613 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3614 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3620 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3621 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3628 /* this order is important */
3629 a
= (u_int32_t
*)addr
;
3630 m
= (u_int32_t
*)mask
;
3631 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3632 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3634 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3636 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3638 b0
= gen_linktype(proto
);
3644 static struct block
*
3645 gen_ehostop(eaddr
, dir
)
3646 register const u_char
*eaddr
;
3649 register struct block
*b0
, *b1
;
3653 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3656 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3659 b0
= gen_ehostop(eaddr
, Q_SRC
);
3660 b1
= gen_ehostop(eaddr
, Q_DST
);
3666 b0
= gen_ehostop(eaddr
, Q_SRC
);
3667 b1
= gen_ehostop(eaddr
, Q_DST
);
3672 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3676 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3680 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3684 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3688 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3692 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3700 * Like gen_ehostop, but for DLT_FDDI
3702 static struct block
*
3703 gen_fhostop(eaddr
, dir
)
3704 register const u_char
*eaddr
;
3707 struct block
*b0
, *b1
;
3712 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3714 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3719 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3721 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3725 b0
= gen_fhostop(eaddr
, Q_SRC
);
3726 b1
= gen_fhostop(eaddr
, Q_DST
);
3732 b0
= gen_fhostop(eaddr
, Q_SRC
);
3733 b1
= gen_fhostop(eaddr
, Q_DST
);
3738 bpf_error("'addr1' is only supported on 802.11");
3742 bpf_error("'addr2' is only supported on 802.11");
3746 bpf_error("'addr3' is only supported on 802.11");
3750 bpf_error("'addr4' is only supported on 802.11");
3754 bpf_error("'ra' is only supported on 802.11");
3758 bpf_error("'ta' is only supported on 802.11");
3766 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3768 static struct block
*
3769 gen_thostop(eaddr
, dir
)
3770 register const u_char
*eaddr
;
3773 register struct block
*b0
, *b1
;
3777 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3780 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3783 b0
= gen_thostop(eaddr
, Q_SRC
);
3784 b1
= gen_thostop(eaddr
, Q_DST
);
3790 b0
= gen_thostop(eaddr
, Q_SRC
);
3791 b1
= gen_thostop(eaddr
, Q_DST
);
3796 bpf_error("'addr1' is only supported on 802.11");
3800 bpf_error("'addr2' is only supported on 802.11");
3804 bpf_error("'addr3' is only supported on 802.11");
3808 bpf_error("'addr4' is only supported on 802.11");
3812 bpf_error("'ra' is only supported on 802.11");
3816 bpf_error("'ta' is only supported on 802.11");
3824 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3825 * various 802.11 + radio headers.
3827 static struct block
*
3828 gen_wlanhostop(eaddr
, dir
)
3829 register const u_char
*eaddr
;
3832 register struct block
*b0
, *b1
, *b2
;
3833 register struct slist
*s
;
3835 #ifdef ENABLE_WLAN_FILTERING_PATCH
3838 * We need to disable the optimizer because the optimizer is buggy
3839 * and wipes out some LD instructions generated by the below
3840 * code to validate the Frame Control bits
3843 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3850 * For control frames, there is no SA.
3852 * For management frames, SA is at an
3853 * offset of 10 from the beginning of
3856 * For data frames, SA is at an offset
3857 * of 10 from the beginning of the packet
3858 * if From DS is clear, at an offset of
3859 * 16 from the beginning of the packet
3860 * if From DS is set and To DS is clear,
3861 * and an offset of 24 from the beginning
3862 * of the packet if From DS is set and To DS
3867 * Generate the tests to be done for data frames
3870 * First, check for To DS set, i.e. check "link[1] & 0x01".
3872 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3873 b1
= new_block(JMP(BPF_JSET
));
3874 b1
->s
.k
= 0x01; /* To DS */
3878 * If To DS is set, the SA is at 24.
3880 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3884 * Now, check for To DS not set, i.e. check
3885 * "!(link[1] & 0x01)".
3887 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3888 b2
= new_block(JMP(BPF_JSET
));
3889 b2
->s
.k
= 0x01; /* To DS */
3894 * If To DS is not set, the SA is at 16.
3896 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3900 * Now OR together the last two checks. That gives
3901 * the complete set of checks for data frames with
3907 * Now check for From DS being set, and AND that with
3908 * the ORed-together checks.
3910 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3911 b1
= new_block(JMP(BPF_JSET
));
3912 b1
->s
.k
= 0x02; /* From DS */
3917 * Now check for data frames with From DS not set.
3919 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3920 b2
= new_block(JMP(BPF_JSET
));
3921 b2
->s
.k
= 0x02; /* From DS */
3926 * If From DS isn't set, the SA is at 10.
3928 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3932 * Now OR together the checks for data frames with
3933 * From DS not set and for data frames with From DS
3934 * set; that gives the checks done for data frames.
3939 * Now check for a data frame.
3940 * I.e, check "link[0] & 0x08".
3942 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3943 b1
= new_block(JMP(BPF_JSET
));
3948 * AND that with the checks done for data frames.
3953 * If the high-order bit of the type value is 0, this
3954 * is a management frame.
3955 * I.e, check "!(link[0] & 0x08)".
3957 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3958 b2
= new_block(JMP(BPF_JSET
));
3964 * For management frames, the SA is at 10.
3966 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3970 * OR that with the checks done for data frames.
3971 * That gives the checks done for management and
3977 * If the low-order bit of the type value is 1,
3978 * this is either a control frame or a frame
3979 * with a reserved type, and thus not a
3982 * I.e., check "!(link[0] & 0x04)".
3984 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3985 b1
= new_block(JMP(BPF_JSET
));
3991 * AND that with the checks for data and management
4001 * For control frames, there is no DA.
4003 * For management frames, DA is at an
4004 * offset of 4 from the beginning of
4007 * For data frames, DA is at an offset
4008 * of 4 from the beginning of the packet
4009 * if To DS is clear and at an offset of
4010 * 16 from the beginning of the packet
4015 * Generate the tests to be done for data frames.
4017 * First, check for To DS set, i.e. "link[1] & 0x01".
4019 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4020 b1
= new_block(JMP(BPF_JSET
));
4021 b1
->s
.k
= 0x01; /* To DS */
4025 * If To DS is set, the DA is at 16.
4027 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4031 * Now, check for To DS not set, i.e. check
4032 * "!(link[1] & 0x01)".
4034 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4035 b2
= new_block(JMP(BPF_JSET
));
4036 b2
->s
.k
= 0x01; /* To DS */
4041 * If To DS is not set, the DA is at 4.
4043 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4047 * Now OR together the last two checks. That gives
4048 * the complete set of checks for data frames.
4053 * Now check for a data frame.
4054 * I.e, check "link[0] & 0x08".
4056 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4057 b1
= new_block(JMP(BPF_JSET
));
4062 * AND that with the checks done for data frames.
4067 * If the high-order bit of the type value is 0, this
4068 * is a management frame.
4069 * I.e, check "!(link[0] & 0x08)".
4071 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4072 b2
= new_block(JMP(BPF_JSET
));
4078 * For management frames, the DA is at 4.
4080 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4084 * OR that with the checks done for data frames.
4085 * That gives the checks done for management and
4091 * If the low-order bit of the type value is 1,
4092 * this is either a control frame or a frame
4093 * with a reserved type, and thus not a
4096 * I.e., check "!(link[0] & 0x04)".
4098 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4099 b1
= new_block(JMP(BPF_JSET
));
4105 * AND that with the checks for data and management
4113 * Not present in management frames; addr1 in other
4118 * If the high-order bit of the type value is 0, this
4119 * is a management frame.
4120 * I.e, check "(link[0] & 0x08)".
4122 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4123 b1
= new_block(JMP(BPF_JSET
));
4130 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4133 * AND that with the check of addr1.
4140 * Not present in management frames; addr2, if present,
4145 * Not present in CTS or ACK control frames.
4147 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4148 IEEE80211_FC0_TYPE_MASK
);
4150 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4151 IEEE80211_FC0_SUBTYPE_MASK
);
4153 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4154 IEEE80211_FC0_SUBTYPE_MASK
);
4160 * If the high-order bit of the type value is 0, this
4161 * is a management frame.
4162 * I.e, check "(link[0] & 0x08)".
4164 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4165 b1
= new_block(JMP(BPF_JSET
));
4170 * AND that with the check for frames other than
4171 * CTS and ACK frames.
4178 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4183 * XXX - add BSSID keyword?
4186 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4190 * Not present in CTS or ACK control frames.
4192 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4193 IEEE80211_FC0_TYPE_MASK
);
4195 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4196 IEEE80211_FC0_SUBTYPE_MASK
);
4198 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4199 IEEE80211_FC0_SUBTYPE_MASK
);
4203 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4209 * Not present in control frames.
4211 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4212 IEEE80211_FC0_TYPE_MASK
);
4214 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4220 * Present only if the direction mask has both "From DS"
4221 * and "To DS" set. Neither control frames nor management
4222 * frames should have both of those set, so we don't
4223 * check the frame type.
4225 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4226 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4227 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4232 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4233 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4239 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4240 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4249 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4250 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4251 * as the RFC states.)
4253 static struct block
*
4254 gen_ipfchostop(eaddr
, dir
)
4255 register const u_char
*eaddr
;
4258 register struct block
*b0
, *b1
;
4262 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4265 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4268 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4269 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4275 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4276 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4281 bpf_error("'addr1' is only supported on 802.11");
4285 bpf_error("'addr2' is only supported on 802.11");
4289 bpf_error("'addr3' is only supported on 802.11");
4293 bpf_error("'addr4' is only supported on 802.11");
4297 bpf_error("'ra' is only supported on 802.11");
4301 bpf_error("'ta' is only supported on 802.11");
4309 * This is quite tricky because there may be pad bytes in front of the
4310 * DECNET header, and then there are two possible data packet formats that
4311 * carry both src and dst addresses, plus 5 packet types in a format that
4312 * carries only the src node, plus 2 types that use a different format and
4313 * also carry just the src node.
4317 * Instead of doing those all right, we just look for data packets with
4318 * 0 or 1 bytes of padding. If you want to look at other packets, that
4319 * will require a lot more hacking.
4321 * To add support for filtering on DECNET "areas" (network numbers)
4322 * one would want to add a "mask" argument to this routine. That would
4323 * make the filter even more inefficient, although one could be clever
4324 * and not generate masking instructions if the mask is 0xFFFF.
4326 static struct block
*
4327 gen_dnhostop(addr
, dir
)
4331 struct block
*b0
, *b1
, *b2
, *tmp
;
4332 u_int offset_lh
; /* offset if long header is received */
4333 u_int offset_sh
; /* offset if short header is received */
4338 offset_sh
= 1; /* follows flags */
4339 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4343 offset_sh
= 3; /* follows flags, dstnode */
4344 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4348 /* Inefficient because we do our Calvinball dance twice */
4349 b0
= gen_dnhostop(addr
, Q_SRC
);
4350 b1
= gen_dnhostop(addr
, Q_DST
);
4356 /* Inefficient because we do our Calvinball dance twice */
4357 b0
= gen_dnhostop(addr
, Q_SRC
);
4358 b1
= gen_dnhostop(addr
, Q_DST
);
4363 bpf_error("ISO host filtering not implemented");
4368 b0
= gen_linktype(ETHERTYPE_DN
);
4369 /* Check for pad = 1, long header case */
4370 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4371 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4372 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4373 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4375 /* Check for pad = 0, long header case */
4376 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4377 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4380 /* Check for pad = 1, short header case */
4381 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4382 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4383 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4386 /* Check for pad = 0, short header case */
4387 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4388 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4392 /* Combine with test for linktype */
4398 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4399 * test the bottom-of-stack bit, and then check the version number
4400 * field in the IP header.
4402 static struct block
*
4403 gen_mpls_linktype(proto
)
4406 struct block
*b0
, *b1
;
4411 /* match the bottom-of-stack bit */
4412 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4413 /* match the IPv4 version number */
4414 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4419 /* match the bottom-of-stack bit */
4420 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4421 /* match the IPv4 version number */
4422 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4431 static struct block
*
4432 gen_host(addr
, mask
, proto
, dir
, type
)
4439 struct block
*b0
, *b1
;
4440 const char *typestr
;
4450 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4452 * Only check for non-IPv4 addresses if we're not
4453 * checking MPLS-encapsulated packets.
4455 if (label_stack_depth
== 0) {
4456 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4458 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4464 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4467 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4470 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4473 bpf_error("'tcp' modifier applied to %s", typestr
);
4476 bpf_error("'sctp' modifier applied to %s", typestr
);
4479 bpf_error("'udp' modifier applied to %s", typestr
);
4482 bpf_error("'icmp' modifier applied to %s", typestr
);
4485 bpf_error("'igmp' modifier applied to %s", typestr
);
4488 bpf_error("'igrp' modifier applied to %s", typestr
);
4491 bpf_error("'pim' modifier applied to %s", typestr
);
4494 bpf_error("'vrrp' modifier applied to %s", typestr
);
4497 bpf_error("'carp' modifier applied to %s", typestr
);
4500 bpf_error("ATALK host filtering not implemented");
4503 bpf_error("AARP host filtering not implemented");
4506 return gen_dnhostop(addr
, dir
);
4509 bpf_error("SCA host filtering not implemented");
4512 bpf_error("LAT host filtering not implemented");
4515 bpf_error("MOPDL host filtering not implemented");
4518 bpf_error("MOPRC host filtering not implemented");
4521 bpf_error("'ip6' modifier applied to ip host");
4524 bpf_error("'icmp6' modifier applied to %s", typestr
);
4527 bpf_error("'ah' modifier applied to %s", typestr
);
4530 bpf_error("'esp' modifier applied to %s", typestr
);
4533 bpf_error("ISO host filtering not implemented");
4536 bpf_error("'esis' modifier applied to %s", typestr
);
4539 bpf_error("'isis' modifier applied to %s", typestr
);
4542 bpf_error("'clnp' modifier applied to %s", typestr
);
4545 bpf_error("'stp' modifier applied to %s", typestr
);
4548 bpf_error("IPX host filtering not implemented");
4551 bpf_error("'netbeui' modifier applied to %s", typestr
);
4554 bpf_error("'radio' modifier applied to %s", typestr
);
4563 static struct block
*
4564 gen_host6(addr
, mask
, proto
, dir
, type
)
4565 struct in6_addr
*addr
;
4566 struct in6_addr
*mask
;
4571 const char *typestr
;
4581 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4584 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4587 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4590 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4593 bpf_error("'sctp' modifier applied to %s", typestr
);
4596 bpf_error("'tcp' modifier applied to %s", typestr
);
4599 bpf_error("'udp' modifier applied to %s", typestr
);
4602 bpf_error("'icmp' modifier applied to %s", typestr
);
4605 bpf_error("'igmp' modifier applied to %s", typestr
);
4608 bpf_error("'igrp' modifier applied to %s", typestr
);
4611 bpf_error("'pim' modifier applied to %s", typestr
);
4614 bpf_error("'vrrp' modifier applied to %s", typestr
);
4617 bpf_error("'carp' modifier applied to %s", typestr
);
4620 bpf_error("ATALK host filtering not implemented");
4623 bpf_error("AARP host filtering not implemented");
4626 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4629 bpf_error("SCA host filtering not implemented");
4632 bpf_error("LAT host filtering not implemented");
4635 bpf_error("MOPDL host filtering not implemented");
4638 bpf_error("MOPRC host filtering not implemented");
4641 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4644 bpf_error("'icmp6' modifier applied to %s", typestr
);
4647 bpf_error("'ah' modifier applied to %s", typestr
);
4650 bpf_error("'esp' modifier applied to %s", typestr
);
4653 bpf_error("ISO host filtering not implemented");
4656 bpf_error("'esis' modifier applied to %s", typestr
);
4659 bpf_error("'isis' modifier applied to %s", typestr
);
4662 bpf_error("'clnp' modifier applied to %s", typestr
);
4665 bpf_error("'stp' modifier applied to %s", typestr
);
4668 bpf_error("IPX host filtering not implemented");
4671 bpf_error("'netbeui' modifier applied to %s", typestr
);
4674 bpf_error("'radio' modifier applied to %s", typestr
);
4684 static struct block
*
4685 gen_gateway(eaddr
, alist
, proto
, dir
)
4686 const u_char
*eaddr
;
4687 bpf_u_int32
**alist
;
4691 struct block
*b0
, *b1
, *tmp
;
4694 bpf_error("direction applied to 'gateway'");
4703 case DLT_NETANALYZER
:
4704 case DLT_NETANALYZER_TRANSPARENT
:
4705 b0
= gen_ehostop(eaddr
, Q_OR
);
4708 b0
= gen_fhostop(eaddr
, Q_OR
);
4711 b0
= gen_thostop(eaddr
, Q_OR
);
4713 case DLT_IEEE802_11
:
4714 case DLT_PRISM_HEADER
:
4715 case DLT_IEEE802_11_RADIO_AVS
:
4716 case DLT_IEEE802_11_RADIO
:
4718 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4723 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4725 * Check that the packet doesn't begin with an
4726 * LE Control marker. (We've already generated
4729 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4734 * Now check the MAC address.
4736 b0
= gen_ehostop(eaddr
, Q_OR
);
4739 case DLT_IP_OVER_FC
:
4740 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4744 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4746 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4748 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4757 bpf_error("illegal modifier of 'gateway'");
4763 gen_proto_abbrev(proto
)
4772 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4773 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4778 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4779 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4784 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4785 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4790 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4793 #ifndef IPPROTO_IGMP
4794 #define IPPROTO_IGMP 2
4798 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4801 #ifndef IPPROTO_IGRP
4802 #define IPPROTO_IGRP 9
4805 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4809 #define IPPROTO_PIM 103
4813 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4814 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4818 #ifndef IPPROTO_VRRP
4819 #define IPPROTO_VRRP 112
4823 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4826 #ifndef IPPROTO_CARP
4827 #define IPPROTO_CARP 112
4831 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4835 b1
= gen_linktype(ETHERTYPE_IP
);
4839 b1
= gen_linktype(ETHERTYPE_ARP
);
4843 b1
= gen_linktype(ETHERTYPE_REVARP
);
4847 bpf_error("link layer applied in wrong context");
4850 b1
= gen_linktype(ETHERTYPE_ATALK
);
4854 b1
= gen_linktype(ETHERTYPE_AARP
);
4858 b1
= gen_linktype(ETHERTYPE_DN
);
4862 b1
= gen_linktype(ETHERTYPE_SCA
);
4866 b1
= gen_linktype(ETHERTYPE_LAT
);
4870 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4874 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4878 b1
= gen_linktype(ETHERTYPE_IPV6
);
4881 #ifndef IPPROTO_ICMPV6
4882 #define IPPROTO_ICMPV6 58
4885 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4889 #define IPPROTO_AH 51
4892 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4893 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4898 #define IPPROTO_ESP 50
4901 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4902 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4907 b1
= gen_linktype(LLCSAP_ISONS
);
4911 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4915 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4918 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4919 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4920 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4922 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4924 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4926 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4930 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4931 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4932 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4934 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4936 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4938 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4942 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4943 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4944 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4946 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4951 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4952 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4957 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4958 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4960 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4962 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4967 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4968 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4973 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4974 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4979 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4983 b1
= gen_linktype(LLCSAP_8021D
);
4987 b1
= gen_linktype(LLCSAP_IPX
);
4991 b1
= gen_linktype(LLCSAP_NETBEUI
);
4995 bpf_error("'radio' is not a valid protocol type");
5003 static struct block
*
5009 /* not IPv4 frag other than the first frag */
5010 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5011 b
= new_block(JMP(BPF_JSET
));
5020 * Generate a comparison to a port value in the transport-layer header
5021 * at the specified offset from the beginning of that header.
5023 * XXX - this handles a variable-length prefix preceding the link-layer
5024 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5025 * variable-length link-layer headers (such as Token Ring or 802.11
5028 static struct block
*
5029 gen_portatom(off
, v
)
5033 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5036 static struct block
*
5037 gen_portatom6(off
, v
)
5041 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5045 gen_portop(port
, proto
, dir
)
5046 int port
, proto
, dir
;
5048 struct block
*b0
, *b1
, *tmp
;
5050 /* ip proto 'proto' and not a fragment other than the first fragment */
5051 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5057 b1
= gen_portatom(0, (bpf_int32
)port
);
5061 b1
= gen_portatom(2, (bpf_int32
)port
);
5066 tmp
= gen_portatom(0, (bpf_int32
)port
);
5067 b1
= gen_portatom(2, (bpf_int32
)port
);
5072 tmp
= gen_portatom(0, (bpf_int32
)port
);
5073 b1
= gen_portatom(2, (bpf_int32
)port
);
5085 static struct block
*
5086 gen_port(port
, ip_proto
, dir
)
5091 struct block
*b0
, *b1
, *tmp
;
5096 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5097 * not LLC encapsulation with LLCSAP_IP.
5099 * For IEEE 802 networks - which includes 802.5 token ring
5100 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5101 * says that SNAP encapsulation is used, not LLC encapsulation
5104 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5105 * RFC 2225 say that SNAP encapsulation is used, not LLC
5106 * encapsulation with LLCSAP_IP.
5108 * So we always check for ETHERTYPE_IP.
5110 b0
= gen_linktype(ETHERTYPE_IP
);
5116 b1
= gen_portop(port
, ip_proto
, dir
);
5120 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5121 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5123 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5135 gen_portop6(port
, proto
, dir
)
5136 int port
, proto
, dir
;
5138 struct block
*b0
, *b1
, *tmp
;
5140 /* ip6 proto 'proto' */
5141 /* XXX - catch the first fragment of a fragmented packet? */
5142 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5146 b1
= gen_portatom6(0, (bpf_int32
)port
);
5150 b1
= gen_portatom6(2, (bpf_int32
)port
);
5155 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5156 b1
= gen_portatom6(2, (bpf_int32
)port
);
5161 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5162 b1
= gen_portatom6(2, (bpf_int32
)port
);
5174 static struct block
*
5175 gen_port6(port
, ip_proto
, dir
)
5180 struct block
*b0
, *b1
, *tmp
;
5182 /* link proto ip6 */
5183 b0
= gen_linktype(ETHERTYPE_IPV6
);
5189 b1
= gen_portop6(port
, ip_proto
, dir
);
5193 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5194 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5196 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5207 /* gen_portrange code */
5208 static struct block
*
5209 gen_portrangeatom(off
, v1
, v2
)
5213 struct block
*b1
, *b2
;
5217 * Reverse the order of the ports, so v1 is the lower one.
5226 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5227 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5235 gen_portrangeop(port1
, port2
, proto
, dir
)
5240 struct block
*b0
, *b1
, *tmp
;
5242 /* ip proto 'proto' and not a fragment other than the first fragment */
5243 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5249 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5253 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5258 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5259 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5264 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5265 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5277 static struct block
*
5278 gen_portrange(port1
, port2
, ip_proto
, dir
)
5283 struct block
*b0
, *b1
, *tmp
;
5286 b0
= gen_linktype(ETHERTYPE_IP
);
5292 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5296 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5297 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5299 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5310 static struct block
*
5311 gen_portrangeatom6(off
, v1
, v2
)
5315 struct block
*b1
, *b2
;
5319 * Reverse the order of the ports, so v1 is the lower one.
5328 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5329 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5337 gen_portrangeop6(port1
, port2
, proto
, dir
)
5342 struct block
*b0
, *b1
, *tmp
;
5344 /* ip6 proto 'proto' */
5345 /* XXX - catch the first fragment of a fragmented packet? */
5346 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5350 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5354 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5359 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5360 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5365 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5366 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5378 static struct block
*
5379 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5384 struct block
*b0
, *b1
, *tmp
;
5386 /* link proto ip6 */
5387 b0
= gen_linktype(ETHERTYPE_IPV6
);
5393 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5397 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5398 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5400 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5412 lookup_proto(name
, proto
)
5413 register const char *name
;
5423 v
= pcap_nametoproto(name
);
5424 if (v
== PROTO_UNDEF
)
5425 bpf_error("unknown ip proto '%s'", name
);
5429 /* XXX should look up h/w protocol type based on linktype */
5430 v
= pcap_nametoeproto(name
);
5431 if (v
== PROTO_UNDEF
) {
5432 v
= pcap_nametollc(name
);
5433 if (v
== PROTO_UNDEF
)
5434 bpf_error("unknown ether proto '%s'", name
);
5439 if (strcmp(name
, "esis") == 0)
5441 else if (strcmp(name
, "isis") == 0)
5443 else if (strcmp(name
, "clnp") == 0)
5446 bpf_error("unknown osi proto '%s'", name
);
5466 static struct block
*
5467 gen_protochain(v
, proto
, dir
)
5472 #ifdef NO_PROTOCHAIN
5473 return gen_proto(v
, proto
, dir
);
5475 struct block
*b0
, *b
;
5476 struct slist
*s
[100];
5477 int fix2
, fix3
, fix4
, fix5
;
5478 int ahcheck
, again
, end
;
5480 int reg2
= alloc_reg();
5482 memset(s
, 0, sizeof(s
));
5483 fix2
= fix3
= fix4
= fix5
= 0;
5490 b0
= gen_protochain(v
, Q_IP
, dir
);
5491 b
= gen_protochain(v
, Q_IPV6
, dir
);
5495 bpf_error("bad protocol applied for 'protochain'");
5500 * We don't handle variable-length prefixes before the link-layer
5501 * header, or variable-length link-layer headers, here yet.
5502 * We might want to add BPF instructions to do the protochain
5503 * work, to simplify that and, on platforms that have a BPF
5504 * interpreter with the new instructions, let the filtering
5505 * be done in the kernel. (We already require a modified BPF
5506 * engine to do the protochain stuff, to support backward
5507 * branches, and backward branch support is unlikely to appear
5508 * in kernel BPF engines.)
5512 case DLT_IEEE802_11
:
5513 case DLT_PRISM_HEADER
:
5514 case DLT_IEEE802_11_RADIO_AVS
:
5515 case DLT_IEEE802_11_RADIO
:
5517 bpf_error("'protochain' not supported with 802.11");
5520 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5523 * s[0] is a dummy entry to protect other BPF insn from damage
5524 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5525 * hard to find interdependency made by jump table fixup.
5528 s
[i
] = new_stmt(0); /*dummy*/
5533 b0
= gen_linktype(ETHERTYPE_IP
);
5536 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5537 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5539 /* X = ip->ip_hl << 2 */
5540 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5541 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5546 b0
= gen_linktype(ETHERTYPE_IPV6
);
5548 /* A = ip6->ip_nxt */
5549 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5550 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5552 /* X = sizeof(struct ip6_hdr) */
5553 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5559 bpf_error("unsupported proto to gen_protochain");
5563 /* again: if (A == v) goto end; else fall through; */
5565 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5567 s
[i
]->s
.jt
= NULL
; /*later*/
5568 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5572 #ifndef IPPROTO_NONE
5573 #define IPPROTO_NONE 59
5575 /* if (A == IPPROTO_NONE) goto end */
5576 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5577 s
[i
]->s
.jt
= NULL
; /*later*/
5578 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5579 s
[i
]->s
.k
= IPPROTO_NONE
;
5580 s
[fix5
]->s
.jf
= s
[i
];
5584 if (proto
== Q_IPV6
) {
5585 int v6start
, v6end
, v6advance
, j
;
5588 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5589 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5590 s
[i
]->s
.jt
= NULL
; /*later*/
5591 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5592 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5593 s
[fix2
]->s
.jf
= s
[i
];
5595 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5596 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5597 s
[i
]->s
.jt
= NULL
; /*later*/
5598 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5599 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5601 /* if (A == IPPROTO_ROUTING) goto v6advance */
5602 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5603 s
[i
]->s
.jt
= NULL
; /*later*/
5604 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5605 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5607 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5608 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5609 s
[i
]->s
.jt
= NULL
; /*later*/
5610 s
[i
]->s
.jf
= NULL
; /*later*/
5611 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5621 * A = P[X + packet head];
5622 * X = X + (P[X + packet head + 1] + 1) * 8;
5624 /* A = P[X + packet head] */
5625 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5626 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5629 s
[i
] = new_stmt(BPF_ST
);
5632 /* A = P[X + packet head + 1]; */
5633 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5634 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 1;
5637 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5641 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5645 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5649 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5652 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5656 /* goto again; (must use BPF_JA for backward jump) */
5657 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5658 s
[i
]->s
.k
= again
- i
- 1;
5659 s
[i
- 1]->s
.jf
= s
[i
];
5663 for (j
= v6start
; j
<= v6end
; j
++)
5664 s
[j
]->s
.jt
= s
[v6advance
];
5667 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5669 s
[fix2
]->s
.jf
= s
[i
];
5675 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5676 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5677 s
[i
]->s
.jt
= NULL
; /*later*/
5678 s
[i
]->s
.jf
= NULL
; /*later*/
5679 s
[i
]->s
.k
= IPPROTO_AH
;
5681 s
[fix3
]->s
.jf
= s
[ahcheck
];
5688 * X = X + (P[X + 1] + 2) * 4;
5691 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5693 /* A = P[X + packet head]; */
5694 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5695 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5698 s
[i
] = new_stmt(BPF_ST
);
5702 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5705 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5709 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5711 /* A = P[X + packet head] */
5712 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5713 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5716 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5720 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5724 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5727 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5731 /* goto again; (must use BPF_JA for backward jump) */
5732 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5733 s
[i
]->s
.k
= again
- i
- 1;
5738 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5740 s
[fix2
]->s
.jt
= s
[end
];
5741 s
[fix4
]->s
.jf
= s
[end
];
5742 s
[fix5
]->s
.jt
= s
[end
];
5749 for (i
= 0; i
< max
- 1; i
++)
5750 s
[i
]->next
= s
[i
+ 1];
5751 s
[max
- 1]->next
= NULL
;
5756 b
= new_block(JMP(BPF_JEQ
));
5757 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5767 static struct block
*
5768 gen_check_802_11_data_frame()
5771 struct block
*b0
, *b1
;
5774 * A data frame has the 0x08 bit (b3) in the frame control field set
5775 * and the 0x04 bit (b2) clear.
5777 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5778 b0
= new_block(JMP(BPF_JSET
));
5782 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5783 b1
= new_block(JMP(BPF_JSET
));
5794 * Generate code that checks whether the packet is a packet for protocol
5795 * <proto> and whether the type field in that protocol's header has
5796 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5797 * IP packet and checks the protocol number in the IP header against <v>.
5799 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5800 * against Q_IP and Q_IPV6.
5802 static struct block
*
5803 gen_proto(v
, proto
, dir
)
5808 struct block
*b0
, *b1
;
5813 if (dir
!= Q_DEFAULT
)
5814 bpf_error("direction applied to 'proto'");
5818 b0
= gen_proto(v
, Q_IP
, dir
);
5819 b1
= gen_proto(v
, Q_IPV6
, dir
);
5825 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5826 * not LLC encapsulation with LLCSAP_IP.
5828 * For IEEE 802 networks - which includes 802.5 token ring
5829 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5830 * says that SNAP encapsulation is used, not LLC encapsulation
5833 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5834 * RFC 2225 say that SNAP encapsulation is used, not LLC
5835 * encapsulation with LLCSAP_IP.
5837 * So we always check for ETHERTYPE_IP.
5839 b0
= gen_linktype(ETHERTYPE_IP
);
5841 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5843 b1
= gen_protochain(v
, Q_IP
);
5853 * Frame Relay packets typically have an OSI
5854 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5855 * generates code to check for all the OSI
5856 * NLPIDs, so calling it and then adding a check
5857 * for the particular NLPID for which we're
5858 * looking is bogus, as we can just check for
5861 * What we check for is the NLPID and a frame
5862 * control field value of UI, i.e. 0x03 followed
5865 * XXX - assumes a 2-byte Frame Relay header with
5866 * DLCI and flags. What if the address is longer?
5868 * XXX - what about SNAP-encapsulated frames?
5870 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5876 * Cisco uses an Ethertype lookalike - for OSI,
5879 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5880 /* OSI in C-HDLC is stuffed with a fudge byte */
5881 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5886 b0
= gen_linktype(LLCSAP_ISONS
);
5887 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5893 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5895 * 4 is the offset of the PDU type relative to the IS-IS
5898 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5903 bpf_error("arp does not encapsulate another protocol");
5907 bpf_error("rarp does not encapsulate another protocol");
5911 bpf_error("atalk encapsulation is not specifiable");
5915 bpf_error("decnet encapsulation is not specifiable");
5919 bpf_error("sca does not encapsulate another protocol");
5923 bpf_error("lat does not encapsulate another protocol");
5927 bpf_error("moprc does not encapsulate another protocol");
5931 bpf_error("mopdl does not encapsulate another protocol");
5935 return gen_linktype(v
);
5938 bpf_error("'udp proto' is bogus");
5942 bpf_error("'tcp proto' is bogus");
5946 bpf_error("'sctp proto' is bogus");
5950 bpf_error("'icmp proto' is bogus");
5954 bpf_error("'igmp proto' is bogus");
5958 bpf_error("'igrp proto' is bogus");
5962 bpf_error("'pim proto' is bogus");
5966 bpf_error("'vrrp proto' is bogus");
5970 bpf_error("'carp proto' is bogus");
5974 b0
= gen_linktype(ETHERTYPE_IPV6
);
5977 * Also check for a fragment header before the final
5980 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
5981 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
5983 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5986 b1
= gen_protochain(v
, Q_IPV6
);
5992 bpf_error("'icmp6 proto' is bogus");
5995 bpf_error("'ah proto' is bogus");
5998 bpf_error("'ah proto' is bogus");
6001 bpf_error("'stp proto' is bogus");
6004 bpf_error("'ipx proto' is bogus");
6007 bpf_error("'netbeui proto' is bogus");
6010 bpf_error("'radio proto' is bogus");
6021 register const char *name
;
6024 int proto
= q
.proto
;
6028 bpf_u_int32 mask
, addr
;
6030 bpf_u_int32
**alist
;
6033 struct sockaddr_in
*sin4
;
6034 struct sockaddr_in6
*sin6
;
6035 struct addrinfo
*res
, *res0
;
6036 struct in6_addr mask128
;
6038 struct block
*b
, *tmp
;
6039 int port
, real_proto
;
6045 addr
= pcap_nametonetaddr(name
);
6047 bpf_error("unknown network '%s'", name
);
6048 /* Left justify network addr and calculate its network mask */
6050 while (addr
&& (addr
& 0xff000000) == 0) {
6054 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6058 if (proto
== Q_LINK
) {
6062 case DLT_NETANALYZER
:
6063 case DLT_NETANALYZER_TRANSPARENT
:
6064 eaddr
= pcap_ether_hostton(name
);
6067 "unknown ether host '%s'", name
);
6068 b
= gen_ehostop(eaddr
, dir
);
6073 eaddr
= pcap_ether_hostton(name
);
6076 "unknown FDDI host '%s'", name
);
6077 b
= gen_fhostop(eaddr
, dir
);
6082 eaddr
= pcap_ether_hostton(name
);
6085 "unknown token ring host '%s'", name
);
6086 b
= gen_thostop(eaddr
, dir
);
6090 case DLT_IEEE802_11
:
6091 case DLT_PRISM_HEADER
:
6092 case DLT_IEEE802_11_RADIO_AVS
:
6093 case DLT_IEEE802_11_RADIO
:
6095 eaddr
= pcap_ether_hostton(name
);
6098 "unknown 802.11 host '%s'", name
);
6099 b
= gen_wlanhostop(eaddr
, dir
);
6103 case DLT_IP_OVER_FC
:
6104 eaddr
= pcap_ether_hostton(name
);
6107 "unknown Fibre Channel host '%s'", name
);
6108 b
= gen_ipfchostop(eaddr
, dir
);
6117 * Check that the packet doesn't begin
6118 * with an LE Control marker. (We've
6119 * already generated a test for LANE.)
6121 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6125 eaddr
= pcap_ether_hostton(name
);
6128 "unknown ether host '%s'", name
);
6129 b
= gen_ehostop(eaddr
, dir
);
6135 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6136 } else if (proto
== Q_DECNET
) {
6137 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6139 * I don't think DECNET hosts can be multihomed, so
6140 * there is no need to build up a list of addresses
6142 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6145 alist
= pcap_nametoaddr(name
);
6146 if (alist
== NULL
|| *alist
== NULL
)
6147 bpf_error("unknown host '%s'", name
);
6149 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6151 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6153 tmp
= gen_host(**alist
++, 0xffffffff,
6154 tproto
, dir
, q
.addr
);
6160 memset(&mask128
, 0xff, sizeof(mask128
));
6161 res0
= res
= pcap_nametoaddrinfo(name
);
6163 bpf_error("unknown host '%s'", name
);
6166 tproto
= tproto6
= proto
;
6167 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6171 for (res
= res0
; res
; res
= res
->ai_next
) {
6172 switch (res
->ai_family
) {
6174 if (tproto
== Q_IPV6
)
6177 sin4
= (struct sockaddr_in
*)
6179 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6180 0xffffffff, tproto
, dir
, q
.addr
);
6183 if (tproto6
== Q_IP
)
6186 sin6
= (struct sockaddr_in6
*)
6188 tmp
= gen_host6(&sin6
->sin6_addr
,
6189 &mask128
, tproto6
, dir
, q
.addr
);
6201 bpf_error("unknown host '%s'%s", name
,
6202 (proto
== Q_DEFAULT
)
6204 : " for specified address family");
6211 if (proto
!= Q_DEFAULT
&&
6212 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6213 bpf_error("illegal qualifier of 'port'");
6214 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6215 bpf_error("unknown port '%s'", name
);
6216 if (proto
== Q_UDP
) {
6217 if (real_proto
== IPPROTO_TCP
)
6218 bpf_error("port '%s' is tcp", name
);
6219 else if (real_proto
== IPPROTO_SCTP
)
6220 bpf_error("port '%s' is sctp", name
);
6222 /* override PROTO_UNDEF */
6223 real_proto
= IPPROTO_UDP
;
6225 if (proto
== Q_TCP
) {
6226 if (real_proto
== IPPROTO_UDP
)
6227 bpf_error("port '%s' is udp", name
);
6229 else if (real_proto
== IPPROTO_SCTP
)
6230 bpf_error("port '%s' is sctp", name
);
6232 /* override PROTO_UNDEF */
6233 real_proto
= IPPROTO_TCP
;
6235 if (proto
== Q_SCTP
) {
6236 if (real_proto
== IPPROTO_UDP
)
6237 bpf_error("port '%s' is udp", name
);
6239 else if (real_proto
== IPPROTO_TCP
)
6240 bpf_error("port '%s' is tcp", name
);
6242 /* override PROTO_UNDEF */
6243 real_proto
= IPPROTO_SCTP
;
6246 bpf_error("illegal port number %d < 0", port
);
6248 bpf_error("illegal port number %d > 65535", port
);
6249 b
= gen_port(port
, real_proto
, dir
);
6250 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6254 if (proto
!= Q_DEFAULT
&&
6255 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6256 bpf_error("illegal qualifier of 'portrange'");
6257 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6258 bpf_error("unknown port in range '%s'", name
);
6259 if (proto
== Q_UDP
) {
6260 if (real_proto
== IPPROTO_TCP
)
6261 bpf_error("port in range '%s' is tcp", name
);
6262 else if (real_proto
== IPPROTO_SCTP
)
6263 bpf_error("port in range '%s' is sctp", name
);
6265 /* override PROTO_UNDEF */
6266 real_proto
= IPPROTO_UDP
;
6268 if (proto
== Q_TCP
) {
6269 if (real_proto
== IPPROTO_UDP
)
6270 bpf_error("port in range '%s' is udp", name
);
6271 else if (real_proto
== IPPROTO_SCTP
)
6272 bpf_error("port in range '%s' is sctp", name
);
6274 /* override PROTO_UNDEF */
6275 real_proto
= IPPROTO_TCP
;
6277 if (proto
== Q_SCTP
) {
6278 if (real_proto
== IPPROTO_UDP
)
6279 bpf_error("port in range '%s' is udp", name
);
6280 else if (real_proto
== IPPROTO_TCP
)
6281 bpf_error("port in range '%s' is tcp", name
);
6283 /* override PROTO_UNDEF */
6284 real_proto
= IPPROTO_SCTP
;
6287 bpf_error("illegal port number %d < 0", port1
);
6289 bpf_error("illegal port number %d > 65535", port1
);
6291 bpf_error("illegal port number %d < 0", port2
);
6293 bpf_error("illegal port number %d > 65535", port2
);
6295 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6296 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6301 eaddr
= pcap_ether_hostton(name
);
6303 bpf_error("unknown ether host: %s", name
);
6305 alist
= pcap_nametoaddr(name
);
6306 if (alist
== NULL
|| *alist
== NULL
)
6307 bpf_error("unknown host '%s'", name
);
6308 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6312 bpf_error("'gateway' not supported in this configuration");
6316 real_proto
= lookup_proto(name
, proto
);
6317 if (real_proto
>= 0)
6318 return gen_proto(real_proto
, proto
, dir
);
6320 bpf_error("unknown protocol: %s", name
);
6323 real_proto
= lookup_proto(name
, proto
);
6324 if (real_proto
>= 0)
6325 return gen_protochain(real_proto
, proto
, dir
);
6327 bpf_error("unknown protocol: %s", name
);
6338 gen_mcode(s1
, s2
, masklen
, q
)
6339 register const char *s1
, *s2
;
6340 register int masklen
;
6343 register int nlen
, mlen
;
6346 nlen
= __pcap_atoin(s1
, &n
);
6347 /* Promote short ipaddr */
6351 mlen
= __pcap_atoin(s2
, &m
);
6352 /* Promote short ipaddr */
6355 bpf_error("non-network bits set in \"%s mask %s\"",
6358 /* Convert mask len to mask */
6360 bpf_error("mask length must be <= 32");
6363 * X << 32 is not guaranteed by C to be 0; it's
6368 m
= 0xffffffff << (32 - masklen
);
6370 bpf_error("non-network bits set in \"%s/%d\"",
6377 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6380 bpf_error("Mask syntax for networks only");
6389 register const char *s
;
6394 int proto
= q
.proto
;
6400 else if (q
.proto
== Q_DECNET
)
6401 vlen
= __pcap_atodn(s
, &v
);
6403 vlen
= __pcap_atoin(s
, &v
);
6410 if (proto
== Q_DECNET
)
6411 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6412 else if (proto
== Q_LINK
) {
6413 bpf_error("illegal link layer address");
6416 if (s
== NULL
&& q
.addr
== Q_NET
) {
6417 /* Promote short net number */
6418 while (v
&& (v
& 0xff000000) == 0) {
6423 /* Promote short ipaddr */
6427 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6432 proto
= IPPROTO_UDP
;
6433 else if (proto
== Q_TCP
)
6434 proto
= IPPROTO_TCP
;
6435 else if (proto
== Q_SCTP
)
6436 proto
= IPPROTO_SCTP
;
6437 else if (proto
== Q_DEFAULT
)
6438 proto
= PROTO_UNDEF
;
6440 bpf_error("illegal qualifier of 'port'");
6443 bpf_error("illegal port number %u > 65535", v
);
6447 b
= gen_port((int)v
, proto
, dir
);
6448 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6454 proto
= IPPROTO_UDP
;
6455 else if (proto
== Q_TCP
)
6456 proto
= IPPROTO_TCP
;
6457 else if (proto
== Q_SCTP
)
6458 proto
= IPPROTO_SCTP
;
6459 else if (proto
== Q_DEFAULT
)
6460 proto
= PROTO_UNDEF
;
6462 bpf_error("illegal qualifier of 'portrange'");
6465 bpf_error("illegal port number %u > 65535", v
);
6469 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6470 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6475 bpf_error("'gateway' requires a name");
6479 return gen_proto((int)v
, proto
, dir
);
6482 return gen_protochain((int)v
, proto
, dir
);
6497 gen_mcode6(s1
, s2
, masklen
, q
)
6498 register const char *s1
, *s2
;
6499 register int masklen
;
6502 struct addrinfo
*res
;
6503 struct in6_addr
*addr
;
6504 struct in6_addr mask
;
6509 bpf_error("no mask %s supported", s2
);
6511 res
= pcap_nametoaddrinfo(s1
);
6513 bpf_error("invalid ip6 address %s", s1
);
6516 bpf_error("%s resolved to multiple address", s1
);
6517 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6519 if (sizeof(mask
) * 8 < masklen
)
6520 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6521 memset(&mask
, 0, sizeof(mask
));
6522 memset(&mask
, 0xff, masklen
/ 8);
6524 mask
.s6_addr
[masklen
/ 8] =
6525 (0xff << (8 - masklen
% 8)) & 0xff;
6528 a
= (u_int32_t
*)addr
;
6529 m
= (u_int32_t
*)&mask
;
6530 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6531 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6532 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6540 bpf_error("Mask syntax for networks only");
6544 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6550 bpf_error("invalid qualifier against IPv6 address");
6559 register const u_char
*eaddr
;
6562 struct block
*b
, *tmp
;
6564 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6567 case DLT_NETANALYZER
:
6568 case DLT_NETANALYZER_TRANSPARENT
:
6569 return gen_ehostop(eaddr
, (int)q
.dir
);
6571 return gen_fhostop(eaddr
, (int)q
.dir
);
6573 return gen_thostop(eaddr
, (int)q
.dir
);
6574 case DLT_IEEE802_11
:
6575 case DLT_PRISM_HEADER
:
6576 case DLT_IEEE802_11_RADIO_AVS
:
6577 case DLT_IEEE802_11_RADIO
:
6579 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6583 * Check that the packet doesn't begin with an
6584 * LE Control marker. (We've already generated
6587 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6592 * Now check the MAC address.
6594 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6599 case DLT_IP_OVER_FC
:
6600 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6602 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6606 bpf_error("ethernet address used in non-ether expression");
6613 struct slist
*s0
, *s1
;
6616 * This is definitely not the best way to do this, but the
6617 * lists will rarely get long.
6624 static struct slist
*
6630 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6635 static struct slist
*
6641 s
= new_stmt(BPF_LD
|BPF_MEM
);
6647 * Modify "index" to use the value stored into its register as an
6648 * offset relative to the beginning of the header for the protocol
6649 * "proto", and allocate a register and put an item "size" bytes long
6650 * (1, 2, or 4) at that offset into that register, making it the register
6654 gen_load(proto
, inst
, size
)
6659 struct slist
*s
, *tmp
;
6661 int regno
= alloc_reg();
6663 free_reg(inst
->regno
);
6667 bpf_error("data size must be 1, 2, or 4");
6683 bpf_error("unsupported index operation");
6687 * The offset is relative to the beginning of the packet
6688 * data, if we have a radio header. (If we don't, this
6691 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6692 linktype
!= DLT_IEEE802_11_RADIO
&&
6693 linktype
!= DLT_PRISM_HEADER
)
6694 bpf_error("radio information not present in capture");
6697 * Load into the X register the offset computed into the
6698 * register specified by "index".
6700 s
= xfer_to_x(inst
);
6703 * Load the item at that offset.
6705 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6707 sappend(inst
->s
, s
);
6712 * The offset is relative to the beginning of
6713 * the link-layer header.
6715 * XXX - what about ATM LANE? Should the index be
6716 * relative to the beginning of the AAL5 frame, so
6717 * that 0 refers to the beginning of the LE Control
6718 * field, or relative to the beginning of the LAN
6719 * frame, so that 0 refers, for Ethernet LANE, to
6720 * the beginning of the destination address?
6722 s
= gen_llprefixlen();
6725 * If "s" is non-null, it has code to arrange that the
6726 * X register contains the length of the prefix preceding
6727 * the link-layer header. Add to it the offset computed
6728 * into the register specified by "index", and move that
6729 * into the X register. Otherwise, just load into the X
6730 * register the offset computed into the register specified
6734 sappend(s
, xfer_to_a(inst
));
6735 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6736 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6738 s
= xfer_to_x(inst
);
6741 * Load the item at the sum of the offset we've put in the
6742 * X register and the offset of the start of the link
6743 * layer header (which is 0 if the radio header is
6744 * variable-length; that header length is what we put
6745 * into the X register and then added to the index).
6747 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6750 sappend(inst
->s
, s
);
6764 * The offset is relative to the beginning of
6765 * the network-layer header.
6766 * XXX - are there any cases where we want
6769 s
= gen_off_macpl();
6772 * If "s" is non-null, it has code to arrange that the
6773 * X register contains the offset of the MAC-layer
6774 * payload. Add to it the offset computed into the
6775 * register specified by "index", and move that into
6776 * the X register. Otherwise, just load into the X
6777 * register the offset computed into the register specified
6781 sappend(s
, xfer_to_a(inst
));
6782 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6783 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6785 s
= xfer_to_x(inst
);
6788 * Load the item at the sum of the offset we've put in the
6789 * X register, the offset of the start of the network
6790 * layer header from the beginning of the MAC-layer
6791 * payload, and the purported offset of the start of the
6792 * MAC-layer payload (which might be 0 if there's a
6793 * variable-length prefix before the link-layer header
6794 * or the link-layer header itself is variable-length;
6795 * the variable-length offset of the start of the
6796 * MAC-layer payload is what we put into the X register
6797 * and then added to the index).
6799 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6800 tmp
->s
.k
= off_macpl
+ off_nl
;
6802 sappend(inst
->s
, s
);
6805 * Do the computation only if the packet contains
6806 * the protocol in question.
6808 b
= gen_proto_abbrev(proto
);
6810 gen_and(inst
->b
, b
);
6824 * The offset is relative to the beginning of
6825 * the transport-layer header.
6827 * Load the X register with the length of the IPv4 header
6828 * (plus the offset of the link-layer header, if it's
6829 * a variable-length header), in bytes.
6831 * XXX - are there any cases where we want
6833 * XXX - we should, if we're built with
6834 * IPv6 support, generate code to load either
6835 * IPv4, IPv6, or both, as appropriate.
6837 s
= gen_loadx_iphdrlen();
6840 * The X register now contains the sum of the length
6841 * of any variable-length header preceding the link-layer
6842 * header, any variable-length link-layer header, and the
6843 * length of the network-layer header.
6845 * Load into the A register the offset relative to
6846 * the beginning of the transport layer header,
6847 * add the X register to that, move that to the
6848 * X register, and load with an offset from the
6849 * X register equal to the offset of the network
6850 * layer header relative to the beginning of
6851 * the MAC-layer payload plus the fixed-length
6852 * portion of the offset of the MAC-layer payload
6853 * from the beginning of the raw packet data.
6855 sappend(s
, xfer_to_a(inst
));
6856 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6857 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6858 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6859 tmp
->s
.k
= off_macpl
+ off_nl
;
6860 sappend(inst
->s
, s
);
6863 * Do the computation only if the packet contains
6864 * the protocol in question - which is true only
6865 * if this is an IP datagram and is the first or
6866 * only fragment of that datagram.
6868 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6870 gen_and(inst
->b
, b
);
6871 gen_and(gen_proto_abbrev(Q_IP
), b
);
6875 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6878 inst
->regno
= regno
;
6879 s
= new_stmt(BPF_ST
);
6881 sappend(inst
->s
, s
);
6887 gen_relation(code
, a0
, a1
, reversed
)
6889 struct arth
*a0
, *a1
;
6892 struct slist
*s0
, *s1
, *s2
;
6893 struct block
*b
, *tmp
;
6897 if (code
== BPF_JEQ
) {
6898 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6899 b
= new_block(JMP(code
));
6903 b
= new_block(BPF_JMP
|code
|BPF_X
);
6909 sappend(a0
->s
, a1
->s
);
6913 free_reg(a0
->regno
);
6914 free_reg(a1
->regno
);
6916 /* 'and' together protocol checks */
6919 gen_and(a0
->b
, tmp
= a1
->b
);
6935 int regno
= alloc_reg();
6936 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6939 s
= new_stmt(BPF_LD
|BPF_LEN
);
6940 s
->next
= new_stmt(BPF_ST
);
6941 s
->next
->s
.k
= regno
;
6956 a
= (struct arth
*)newchunk(sizeof(*a
));
6960 s
= new_stmt(BPF_LD
|BPF_IMM
);
6962 s
->next
= new_stmt(BPF_ST
);
6978 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6981 s
= new_stmt(BPF_ST
);
6989 gen_arth(code
, a0
, a1
)
6991 struct arth
*a0
, *a1
;
6993 struct slist
*s0
, *s1
, *s2
;
6997 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7002 sappend(a0
->s
, a1
->s
);
7004 free_reg(a0
->regno
);
7005 free_reg(a1
->regno
);
7007 s0
= new_stmt(BPF_ST
);
7008 a0
->regno
= s0
->s
.k
= alloc_reg();
7015 * Here we handle simple allocation of the scratch registers.
7016 * If too many registers are alloc'd, the allocator punts.
7018 static int regused
[BPF_MEMWORDS
];
7022 * Initialize the table of used registers and the current register.
7028 memset(regused
, 0, sizeof regused
);
7032 * Return the next free register.
7037 int n
= BPF_MEMWORDS
;
7040 if (regused
[curreg
])
7041 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7043 regused
[curreg
] = 1;
7047 bpf_error("too many registers needed to evaluate expression");
7053 * Return a register to the table so it can
7063 static struct block
*
7070 s
= new_stmt(BPF_LD
|BPF_LEN
);
7071 b
= new_block(JMP(jmp
));
7082 return gen_len(BPF_JGE
, n
);
7086 * Actually, this is less than or equal.
7094 b
= gen_len(BPF_JGT
, n
);
7101 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7102 * the beginning of the link-layer header.
7103 * XXX - that means you can't test values in the radiotap header, but
7104 * as that header is difficult if not impossible to parse generally
7105 * without a loop, that might not be a severe problem. A new keyword
7106 * "radio" could be added for that, although what you'd really want
7107 * would be a way of testing particular radio header values, which
7108 * would generate code appropriate to the radio header in question.
7111 gen_byteop(op
, idx
, val
)
7122 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7125 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7129 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7133 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7137 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7141 b
= new_block(JMP(BPF_JEQ
));
7148 static u_char abroadcast
[] = { 0x0 };
7151 gen_broadcast(proto
)
7154 bpf_u_int32 hostmask
;
7155 struct block
*b0
, *b1
, *b2
;
7156 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7164 case DLT_ARCNET_LINUX
:
7165 return gen_ahostop(abroadcast
, Q_DST
);
7167 case DLT_NETANALYZER
:
7168 case DLT_NETANALYZER_TRANSPARENT
:
7169 return gen_ehostop(ebroadcast
, Q_DST
);
7171 return gen_fhostop(ebroadcast
, Q_DST
);
7173 return gen_thostop(ebroadcast
, Q_DST
);
7174 case DLT_IEEE802_11
:
7175 case DLT_PRISM_HEADER
:
7176 case DLT_IEEE802_11_RADIO_AVS
:
7177 case DLT_IEEE802_11_RADIO
:
7179 return gen_wlanhostop(ebroadcast
, Q_DST
);
7180 case DLT_IP_OVER_FC
:
7181 return gen_ipfchostop(ebroadcast
, Q_DST
);
7185 * Check that the packet doesn't begin with an
7186 * LE Control marker. (We've already generated
7189 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7194 * Now check the MAC address.
7196 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7202 bpf_error("not a broadcast link");
7208 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7209 * as an indication that we don't know the netmask, and fail
7212 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7213 bpf_error("netmask not known, so 'ip broadcast' not supported");
7214 b0
= gen_linktype(ETHERTYPE_IP
);
7215 hostmask
= ~netmask
;
7216 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7217 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7218 (bpf_int32
)(~0 & hostmask
), hostmask
);
7223 bpf_error("only link-layer/IP broadcast filters supported");
7229 * Generate code to test the low-order bit of a MAC address (that's
7230 * the bottom bit of the *first* byte).
7232 static struct block
*
7233 gen_mac_multicast(offset
)
7236 register struct block
*b0
;
7237 register struct slist
*s
;
7239 /* link[offset] & 1 != 0 */
7240 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7241 b0
= new_block(JMP(BPF_JSET
));
7248 gen_multicast(proto
)
7251 register struct block
*b0
, *b1
, *b2
;
7252 register struct slist
*s
;
7260 case DLT_ARCNET_LINUX
:
7261 /* all ARCnet multicasts use the same address */
7262 return gen_ahostop(abroadcast
, Q_DST
);
7264 case DLT_NETANALYZER
:
7265 case DLT_NETANALYZER_TRANSPARENT
:
7266 /* ether[0] & 1 != 0 */
7267 return gen_mac_multicast(0);
7270 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7272 * XXX - was that referring to bit-order issues?
7274 /* fddi[1] & 1 != 0 */
7275 return gen_mac_multicast(1);
7277 /* tr[2] & 1 != 0 */
7278 return gen_mac_multicast(2);
7279 case DLT_IEEE802_11
:
7280 case DLT_PRISM_HEADER
:
7281 case DLT_IEEE802_11_RADIO_AVS
:
7282 case DLT_IEEE802_11_RADIO
:
7287 * For control frames, there is no DA.
7289 * For management frames, DA is at an
7290 * offset of 4 from the beginning of
7293 * For data frames, DA is at an offset
7294 * of 4 from the beginning of the packet
7295 * if To DS is clear and at an offset of
7296 * 16 from the beginning of the packet
7301 * Generate the tests to be done for data frames.
7303 * First, check for To DS set, i.e. "link[1] & 0x01".
7305 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7306 b1
= new_block(JMP(BPF_JSET
));
7307 b1
->s
.k
= 0x01; /* To DS */
7311 * If To DS is set, the DA is at 16.
7313 b0
= gen_mac_multicast(16);
7317 * Now, check for To DS not set, i.e. check
7318 * "!(link[1] & 0x01)".
7320 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7321 b2
= new_block(JMP(BPF_JSET
));
7322 b2
->s
.k
= 0x01; /* To DS */
7327 * If To DS is not set, the DA is at 4.
7329 b1
= gen_mac_multicast(4);
7333 * Now OR together the last two checks. That gives
7334 * the complete set of checks for data frames.
7339 * Now check for a data frame.
7340 * I.e, check "link[0] & 0x08".
7342 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7343 b1
= new_block(JMP(BPF_JSET
));
7348 * AND that with the checks done for data frames.
7353 * If the high-order bit of the type value is 0, this
7354 * is a management frame.
7355 * I.e, check "!(link[0] & 0x08)".
7357 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7358 b2
= new_block(JMP(BPF_JSET
));
7364 * For management frames, the DA is at 4.
7366 b1
= gen_mac_multicast(4);
7370 * OR that with the checks done for data frames.
7371 * That gives the checks done for management and
7377 * If the low-order bit of the type value is 1,
7378 * this is either a control frame or a frame
7379 * with a reserved type, and thus not a
7382 * I.e., check "!(link[0] & 0x04)".
7384 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7385 b1
= new_block(JMP(BPF_JSET
));
7391 * AND that with the checks for data and management
7396 case DLT_IP_OVER_FC
:
7397 b0
= gen_mac_multicast(2);
7402 * Check that the packet doesn't begin with an
7403 * LE Control marker. (We've already generated
7406 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7410 /* ether[off_mac] & 1 != 0 */
7411 b0
= gen_mac_multicast(off_mac
);
7419 /* Link not known to support multicasts */
7423 b0
= gen_linktype(ETHERTYPE_IP
);
7424 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7429 b0
= gen_linktype(ETHERTYPE_IPV6
);
7430 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7434 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7440 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7441 * Outbound traffic is sent by this machine, while inbound traffic is
7442 * sent by a remote machine (and may include packets destined for a
7443 * unicast or multicast link-layer address we are not subscribing to).
7444 * These are the same definitions implemented by pcap_setdirection().
7445 * Capturing only unicast traffic destined for this host is probably
7446 * better accomplished using a higher-layer filter.
7452 register struct block
*b0
;
7455 * Only some data link types support inbound/outbound qualifiers.
7459 b0
= gen_relation(BPF_JEQ
,
7460 gen_load(Q_LINK
, gen_loadi(0), 1),
7467 /* match outgoing packets */
7468 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7470 /* match incoming packets */
7471 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7476 /* match outgoing packets */
7477 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7479 /* to filter on inbound traffic, invert the match */
7484 #ifdef HAVE_NET_PFVAR_H
7486 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7487 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7493 /* match outgoing packets */
7494 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7496 /* match incoming packets */
7497 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7501 case DLT_JUNIPER_MFR
:
7502 case DLT_JUNIPER_MLFR
:
7503 case DLT_JUNIPER_MLPPP
:
7504 case DLT_JUNIPER_ATM1
:
7505 case DLT_JUNIPER_ATM2
:
7506 case DLT_JUNIPER_PPPOE
:
7507 case DLT_JUNIPER_PPPOE_ATM
:
7508 case DLT_JUNIPER_GGSN
:
7509 case DLT_JUNIPER_ES
:
7510 case DLT_JUNIPER_MONITOR
:
7511 case DLT_JUNIPER_SERVICES
:
7512 case DLT_JUNIPER_ETHER
:
7513 case DLT_JUNIPER_PPP
:
7514 case DLT_JUNIPER_FRELAY
:
7515 case DLT_JUNIPER_CHDLC
:
7516 case DLT_JUNIPER_VP
:
7517 case DLT_JUNIPER_ST
:
7518 case DLT_JUNIPER_ISM
:
7519 case DLT_JUNIPER_VS
:
7520 case DLT_JUNIPER_SRX_E2E
:
7521 case DLT_JUNIPER_FIBRECHANNEL
:
7522 case DLT_JUNIPER_ATM_CEMIC
:
7524 /* juniper flags (including direction) are stored
7525 * the byte after the 3-byte magic number */
7527 /* match outgoing packets */
7528 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7530 /* match incoming packets */
7531 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7537 * If we have packet meta-data indicating a direction,
7538 * check it, otherwise give up as this link-layer type
7539 * has nothing in the packet data.
7541 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7543 * This is Linux with PF_PACKET support.
7544 * If this is a *live* capture, we can look at
7545 * special meta-data in the filter expression;
7546 * if it's a savefile, we can't.
7548 if (bpf_pcap
->sf
.rfile
!= NULL
) {
7549 /* We have a FILE *, so this is a savefile */
7550 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7555 /* match outgoing packets */
7556 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7559 /* to filter on inbound traffic, invert the match */
7562 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7563 bpf_error("inbound/outbound not supported on linktype %d",
7567 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7572 #ifdef HAVE_NET_PFVAR_H
7573 /* PF firewall log matched interface */
7575 gen_pf_ifname(const char *ifname
)
7580 if (linktype
!= DLT_PFLOG
) {
7581 bpf_error("ifname supported only on PF linktype");
7584 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7585 off
= offsetof(struct pfloghdr
, ifname
);
7586 if (strlen(ifname
) >= len
) {
7587 bpf_error("ifname interface names can only be %d characters",
7591 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7595 /* PF firewall log ruleset name */
7597 gen_pf_ruleset(char *ruleset
)
7601 if (linktype
!= DLT_PFLOG
) {
7602 bpf_error("ruleset supported only on PF linktype");
7606 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7607 bpf_error("ruleset names can only be %ld characters",
7608 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7612 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7613 strlen(ruleset
), (const u_char
*)ruleset
);
7617 /* PF firewall log rule number */
7623 if (linktype
!= DLT_PFLOG
) {
7624 bpf_error("rnr supported only on PF linktype");
7628 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7633 /* PF firewall log sub-rule number */
7635 gen_pf_srnr(int srnr
)
7639 if (linktype
!= DLT_PFLOG
) {
7640 bpf_error("srnr supported only on PF linktype");
7644 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7649 /* PF firewall log reason code */
7651 gen_pf_reason(int reason
)
7655 if (linktype
!= DLT_PFLOG
) {
7656 bpf_error("reason supported only on PF linktype");
7660 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7665 /* PF firewall log action */
7667 gen_pf_action(int action
)
7671 if (linktype
!= DLT_PFLOG
) {
7672 bpf_error("action supported only on PF linktype");
7676 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7680 #else /* !HAVE_NET_PFVAR_H */
7682 gen_pf_ifname(const char *ifname
)
7684 bpf_error("libpcap was compiled without pf support");
7690 gen_pf_ruleset(char *ruleset
)
7692 bpf_error("libpcap was compiled on a machine without pf support");
7700 bpf_error("libpcap was compiled on a machine without pf support");
7706 gen_pf_srnr(int srnr
)
7708 bpf_error("libpcap was compiled on a machine without pf support");
7714 gen_pf_reason(int reason
)
7716 bpf_error("libpcap was compiled on a machine without pf support");
7722 gen_pf_action(int action
)
7724 bpf_error("libpcap was compiled on a machine without pf support");
7728 #endif /* HAVE_NET_PFVAR_H */
7730 /* IEEE 802.11 wireless header */
7732 gen_p80211_type(int type
, int mask
)
7738 case DLT_IEEE802_11
:
7739 case DLT_PRISM_HEADER
:
7740 case DLT_IEEE802_11_RADIO_AVS
:
7741 case DLT_IEEE802_11_RADIO
:
7742 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7747 bpf_error("802.11 link-layer types supported only on 802.11");
7755 gen_p80211_fcdir(int fcdir
)
7761 case DLT_IEEE802_11
:
7762 case DLT_PRISM_HEADER
:
7763 case DLT_IEEE802_11_RADIO_AVS
:
7764 case DLT_IEEE802_11_RADIO
:
7768 bpf_error("frame direction supported only with 802.11 headers");
7772 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7773 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7780 register const u_char
*eaddr
;
7786 case DLT_ARCNET_LINUX
:
7787 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7789 return (gen_ahostop(eaddr
, (int)q
.dir
));
7791 bpf_error("ARCnet address used in non-arc expression");
7797 bpf_error("aid supported only on ARCnet");
7800 bpf_error("ARCnet address used in non-arc expression");
7805 static struct block
*
7806 gen_ahostop(eaddr
, dir
)
7807 register const u_char
*eaddr
;
7810 register struct block
*b0
, *b1
;
7813 /* src comes first, different from Ethernet */
7815 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7818 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7821 b0
= gen_ahostop(eaddr
, Q_SRC
);
7822 b1
= gen_ahostop(eaddr
, Q_DST
);
7828 b0
= gen_ahostop(eaddr
, Q_SRC
);
7829 b1
= gen_ahostop(eaddr
, Q_DST
);
7834 bpf_error("'addr1' is only supported on 802.11");
7838 bpf_error("'addr2' is only supported on 802.11");
7842 bpf_error("'addr3' is only supported on 802.11");
7846 bpf_error("'addr4' is only supported on 802.11");
7850 bpf_error("'ra' is only supported on 802.11");
7854 bpf_error("'ta' is only supported on 802.11");
7862 * support IEEE 802.1Q VLAN trunk over ethernet
7868 struct block
*b0
, *b1
;
7870 /* can't check for VLAN-encapsulated packets inside MPLS */
7871 if (label_stack_depth
> 0)
7872 bpf_error("no VLAN match after MPLS");
7875 * Check for a VLAN packet, and then change the offsets to point
7876 * to the type and data fields within the VLAN packet. Just
7877 * increment the offsets, so that we can support a hierarchy, e.g.
7878 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7881 * XXX - this is a bit of a kludge. If we were to split the
7882 * compiler into a parser that parses an expression and
7883 * generates an expression tree, and a code generator that
7884 * takes an expression tree (which could come from our
7885 * parser or from some other parser) and generates BPF code,
7886 * we could perhaps make the offsets parameters of routines
7887 * and, in the handler for an "AND" node, pass to subnodes
7888 * other than the VLAN node the adjusted offsets.
7890 * This would mean that "vlan" would, instead of changing the
7891 * behavior of *all* tests after it, change only the behavior
7892 * of tests ANDed with it. That would change the documented
7893 * semantics of "vlan", which might break some expressions.
7894 * However, it would mean that "(vlan and ip) or ip" would check
7895 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7896 * checking only for VLAN-encapsulated IP, so that could still
7897 * be considered worth doing; it wouldn't break expressions
7898 * that are of the form "vlan and ..." or "vlan N and ...",
7899 * which I suspect are the most common expressions involving
7900 * "vlan". "vlan or ..." doesn't necessarily do what the user
7901 * would really want, now, as all the "or ..." tests would
7902 * be done assuming a VLAN, even though the "or" could be viewed
7903 * as meaning "or, if this isn't a VLAN packet...".
7910 case DLT_NETANALYZER
:
7911 case DLT_NETANALYZER_TRANSPARENT
:
7912 /* check for VLAN, including QinQ */
7913 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7914 (bpf_int32
)ETHERTYPE_8021Q
);
7915 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7916 (bpf_int32
)ETHERTYPE_8021QINQ
);
7920 /* If a specific VLAN is requested, check VLAN id */
7921 if (vlan_num
>= 0) {
7922 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7923 (bpf_int32
)vlan_num
, 0x0fff);
7937 bpf_error("no VLAN support for data link type %d",
7952 struct block
*b0
,*b1
;
7955 * Change the offsets to point to the type and data fields within
7956 * the MPLS packet. Just increment the offsets, so that we
7957 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7958 * capture packets with an outer label of 100000 and an inner
7961 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7965 if (label_stack_depth
> 0) {
7966 /* just match the bottom-of-stack bit clear */
7967 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7970 * Indicate that we're checking MPLS-encapsulated headers,
7971 * to make sure higher level code generators don't try to
7972 * match against IP-related protocols such as Q_ARP, Q_RARP
7977 case DLT_C_HDLC
: /* fall through */
7979 case DLT_NETANALYZER
:
7980 case DLT_NETANALYZER_TRANSPARENT
:
7981 b0
= gen_linktype(ETHERTYPE_MPLS
);
7985 b0
= gen_linktype(PPP_MPLS_UCAST
);
7988 /* FIXME add other DLT_s ...
7989 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7990 * leave it for now */
7993 bpf_error("no MPLS support for data link type %d",
8001 /* If a specific MPLS label is requested, check it */
8002 if (label_num
>= 0) {
8003 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8004 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8005 0xfffff000); /* only compare the first 20 bits */
8012 label_stack_depth
++;
8017 * Support PPPOE discovery and session.
8022 /* check for PPPoE discovery */
8023 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8032 * Test against the PPPoE session link-layer type.
8034 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8037 * Change the offsets to point to the type and data fields within
8038 * the PPP packet, and note that this is PPPoE rather than
8041 * XXX - this is a bit of a kludge. If we were to split the
8042 * compiler into a parser that parses an expression and
8043 * generates an expression tree, and a code generator that
8044 * takes an expression tree (which could come from our
8045 * parser or from some other parser) and generates BPF code,
8046 * we could perhaps make the offsets parameters of routines
8047 * and, in the handler for an "AND" node, pass to subnodes
8048 * other than the PPPoE node the adjusted offsets.
8050 * This would mean that "pppoes" would, instead of changing the
8051 * behavior of *all* tests after it, change only the behavior
8052 * of tests ANDed with it. That would change the documented
8053 * semantics of "pppoes", which might break some expressions.
8054 * However, it would mean that "(pppoes and ip) or ip" would check
8055 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8056 * checking only for VLAN-encapsulated IP, so that could still
8057 * be considered worth doing; it wouldn't break expressions
8058 * that are of the form "pppoes and ..." which I suspect are the
8059 * most common expressions involving "pppoes". "pppoes or ..."
8060 * doesn't necessarily do what the user would really want, now,
8061 * as all the "or ..." tests would be done assuming PPPoE, even
8062 * though the "or" could be viewed as meaning "or, if this isn't
8063 * a PPPoE packet...".
8065 orig_linktype
= off_linktype
; /* save original values */
8070 * The "network-layer" protocol is PPPoE, which has a 6-byte
8071 * PPPoE header, followed by a PPP packet.
8073 * There is no HDLC encapsulation for the PPP packet (it's
8074 * encapsulated in PPPoES instead), so the link-layer type
8075 * starts at the first byte of the PPP packet. For PPPoE,
8076 * that offset is relative to the beginning of the total
8077 * link-layer payload, including any 802.2 LLC header, so
8078 * it's 6 bytes past off_nl.
8080 off_linktype
= off_nl
+ 6;
8083 * The network-layer offsets are relative to the beginning
8084 * of the MAC-layer payload; that's past the 6-byte
8085 * PPPoE header and the 2-byte PPP header.
8088 off_nl_nosnap
= 6+2;
8094 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8106 bpf_error("'vpi' supported only on raw ATM");
8107 if (off_vpi
== (u_int
)-1)
8109 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8115 bpf_error("'vci' supported only on raw ATM");
8116 if (off_vci
== (u_int
)-1)
8118 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8123 if (off_proto
== (u_int
)-1)
8124 abort(); /* XXX - this isn't on FreeBSD */
8125 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8130 if (off_payload
== (u_int
)-1)
8132 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8133 0xffffffff, jtype
, reverse
, jvalue
);
8138 bpf_error("'callref' supported only on raw ATM");
8139 if (off_proto
== (u_int
)-1)
8141 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8142 jtype
, reverse
, jvalue
);
8152 gen_atmtype_abbrev(type
)
8155 struct block
*b0
, *b1
;
8160 /* Get all packets in Meta signalling Circuit */
8162 bpf_error("'metac' supported only on raw ATM");
8163 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8164 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8169 /* Get all packets in Broadcast Circuit*/
8171 bpf_error("'bcc' supported only on raw ATM");
8172 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8173 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8178 /* Get all cells in Segment OAM F4 circuit*/
8180 bpf_error("'oam4sc' supported only on raw ATM");
8181 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8182 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8187 /* Get all cells in End-to-End OAM F4 Circuit*/
8189 bpf_error("'oam4ec' supported only on raw ATM");
8190 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8191 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8196 /* Get all packets in connection Signalling Circuit */
8198 bpf_error("'sc' supported only on raw ATM");
8199 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8200 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8205 /* Get all packets in ILMI Circuit */
8207 bpf_error("'ilmic' supported only on raw ATM");
8208 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8209 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8214 /* Get all LANE packets */
8216 bpf_error("'lane' supported only on raw ATM");
8217 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8220 * Arrange that all subsequent tests assume LANE
8221 * rather than LLC-encapsulated packets, and set
8222 * the offsets appropriately for LANE-encapsulated
8225 * "off_mac" is the offset of the Ethernet header,
8226 * which is 2 bytes past the ATM pseudo-header
8227 * (skipping the pseudo-header and 2-byte LE Client
8228 * field). The other offsets are Ethernet offsets
8229 * relative to "off_mac".
8232 off_mac
= off_payload
+ 2; /* MAC header */
8233 off_linktype
= off_mac
+ 12;
8234 off_macpl
= off_mac
+ 14; /* Ethernet */
8235 off_nl
= 0; /* Ethernet II */
8236 off_nl_nosnap
= 3; /* 802.3+802.2 */
8240 /* Get all LLC-encapsulated packets */
8242 bpf_error("'llc' supported only on raw ATM");
8243 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8254 * Filtering for MTP2 messages based on li value
8255 * FISU, length is null
8256 * LSSU, length is 1 or 2
8257 * MSU, length is 3 or more
8258 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8261 gen_mtp2type_abbrev(type
)
8264 struct block
*b0
, *b1
;
8269 if ( (linktype
!= DLT_MTP2
) &&
8270 (linktype
!= DLT_ERF
) &&
8271 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8272 bpf_error("'fisu' supported only on MTP2");
8273 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8274 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8278 if ( (linktype
!= DLT_MTP2
) &&
8279 (linktype
!= DLT_ERF
) &&
8280 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8281 bpf_error("'lssu' supported only on MTP2");
8282 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8283 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8288 if ( (linktype
!= DLT_MTP2
) &&
8289 (linktype
!= DLT_ERF
) &&
8290 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8291 bpf_error("'msu' supported only on MTP2");
8292 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8296 if ( (linktype
!= DLT_MTP2
) &&
8297 (linktype
!= DLT_ERF
) &&
8298 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8299 bpf_error("'hfisu' supported only on MTP2_HSL");
8300 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8301 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8305 if ( (linktype
!= DLT_MTP2
) &&
8306 (linktype
!= DLT_ERF
) &&
8307 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8308 bpf_error("'hlssu' supported only on MTP2_HSL");
8309 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8310 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8315 if ( (linktype
!= DLT_MTP2
) &&
8316 (linktype
!= DLT_ERF
) &&
8317 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8318 bpf_error("'hmsu' supported only on MTP2_HSL");
8319 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8329 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8336 bpf_u_int32 val1
, val2
, val3
;
8337 u_int newoff_sio
=off_sio
;
8338 u_int newoff_opc
=off_opc
;
8339 u_int newoff_dpc
=off_dpc
;
8340 u_int newoff_sls
=off_sls
;
8342 switch (mtp3field
) {
8345 newoff_sio
+= 3; /* offset for MTP2_HSL */
8349 if (off_sio
== (u_int
)-1)
8350 bpf_error("'sio' supported only on SS7");
8351 /* sio coded on 1 byte so max value 255 */
8353 bpf_error("sio value %u too big; max value = 255",
8355 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8356 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8362 if (off_opc
== (u_int
)-1)
8363 bpf_error("'opc' supported only on SS7");
8364 /* opc coded on 14 bits so max value 16383 */
8366 bpf_error("opc value %u too big; max value = 16383",
8368 /* the following instructions are made to convert jvalue
8369 * to the form used to write opc in an ss7 message*/
8370 val1
= jvalue
& 0x00003c00;
8372 val2
= jvalue
& 0x000003fc;
8374 val3
= jvalue
& 0x00000003;
8376 jvalue
= val1
+ val2
+ val3
;
8377 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8378 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8386 if (off_dpc
== (u_int
)-1)
8387 bpf_error("'dpc' supported only on SS7");
8388 /* dpc coded on 14 bits so max value 16383 */
8390 bpf_error("dpc value %u too big; max value = 16383",
8392 /* the following instructions are made to convert jvalue
8393 * to the forme used to write dpc in an ss7 message*/
8394 val1
= jvalue
& 0x000000ff;
8396 val2
= jvalue
& 0x00003f00;
8398 jvalue
= val1
+ val2
;
8399 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8400 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8406 if (off_sls
== (u_int
)-1)
8407 bpf_error("'sls' supported only on SS7");
8408 /* sls coded on 4 bits so max value 15 */
8410 bpf_error("sls value %u too big; max value = 15",
8412 /* the following instruction is made to convert jvalue
8413 * to the forme used to write sls in an ss7 message*/
8414 jvalue
= jvalue
<< 4;
8415 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8416 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8425 static struct block
*
8426 gen_msg_abbrev(type
)
8432 * Q.2931 signalling protocol messages for handling virtual circuits
8433 * establishment and teardown
8438 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8442 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8446 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8450 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8454 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8457 case A_RELEASE_DONE
:
8458 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8468 gen_atmmulti_abbrev(type
)
8471 struct block
*b0
, *b1
;
8477 bpf_error("'oam' supported only on raw ATM");
8478 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8483 bpf_error("'oamf4' supported only on raw ATM");
8485 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8486 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8488 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8494 * Get Q.2931 signalling messages for switched
8495 * virtual connection
8498 bpf_error("'connectmsg' supported only on raw ATM");
8499 b0
= gen_msg_abbrev(A_SETUP
);
8500 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8502 b0
= gen_msg_abbrev(A_CONNECT
);
8504 b0
= gen_msg_abbrev(A_CONNECTACK
);
8506 b0
= gen_msg_abbrev(A_RELEASE
);
8508 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8510 b0
= gen_atmtype_abbrev(A_SC
);
8516 bpf_error("'metaconnect' supported only on raw ATM");
8517 b0
= gen_msg_abbrev(A_SETUP
);
8518 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8520 b0
= gen_msg_abbrev(A_CONNECT
);
8522 b0
= gen_msg_abbrev(A_RELEASE
);
8524 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8526 b0
= gen_atmtype_abbrev(A_METAC
);