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.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
43 * XXX - why was this included even on UNIX?
52 #include <sys/param.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
72 #include "ethertype.h"
76 #include "ieee80211.h"
78 #include "sunatmpos.h"
81 #include "pcap/ipnet.h"
83 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
84 #include <linux/types.h>
85 #include <linux/if_packet.h>
86 #include <linux/filter.h>
88 #ifdef HAVE_NET_PFVAR_H
89 #include <sys/socket.h>
91 #include <net/pfvar.h>
92 #include <net/if_pflog.h>
95 #define offsetof(s, e) ((size_t)&((s *)0)->e)
99 #include <netdb.h> /* for "struct addrinfo" */
102 #include <pcap/namedb.h>
104 #define ETHERMTU 1500
106 #ifndef IPPROTO_HOPOPTS
107 #define IPPROTO_HOPOPTS 0
109 #ifndef IPPROTO_ROUTING
110 #define IPPROTO_ROUTING 43
112 #ifndef IPPROTO_FRAGMENT
113 #define IPPROTO_FRAGMENT 44
115 #ifndef IPPROTO_DSTOPTS
116 #define IPPROTO_DSTOPTS 60
119 #define IPPROTO_SCTP 132
122 #ifdef HAVE_OS_PROTO_H
123 #include "os-proto.h"
126 #define JMP(c) ((c)|BPF_JMP|BPF_K)
129 static jmp_buf top_ctx
;
130 static pcap_t
*bpf_pcap
;
132 /* Hack for handling VLAN and MPLS stacks. */
134 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
136 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
140 static int pcap_fddipad
;
144 bpf_error(const char *fmt
, ...)
149 if (bpf_pcap
!= NULL
)
150 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
157 static void init_linktype(pcap_t
*);
159 static void init_regs(void);
160 static int alloc_reg(void);
161 static void free_reg(int);
163 static struct block
*root
;
166 * Absolute offsets, which are offsets from the beginning of the raw
167 * packet data, are, in the general case, the sum of a variable value
168 * and a constant value; the variable value may be absent, in which
169 * case the offset is only the constant value, and the constant value
170 * may be zero, in which case the offset is only the variable value.
172 * bpf_abs_offset is a structure containing all that information:
174 * is_variable is 1 if there's a variable part.
176 * constant_part is the constant part of the value, possibly zero;
178 * if is_variable is 1, reg is the register number for a register
179 * containing the variable value if the register has been assigned,
189 * Value passed to gen_load_a() to indicate what the offset argument
190 * is relative to the beginning of.
193 OR_PACKET
, /* full packet data */
194 OR_LINKHDR
, /* link-layer header */
195 OR_PREVLINKHDR
, /* previous link-layer header */
196 OR_LLC
, /* 802.2 LLC header */
197 OR_LINKPL
, /* link-layer payload */
198 OR_MPLSPL
, /* MPLS payload */
199 OR_NET
, /* network-layer header */
200 OR_NET_NOSNAP
, /* network-layer header, with no SNAP header at the link layer */
201 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
202 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
207 * As errors are handled by a longjmp, anything allocated must be freed
208 * in the longjmp handler, so it must be reachable from that handler.
209 * One thing that's allocated is the result of pcap_nametoaddrinfo();
210 * it must be freed with freeaddrinfo(). This variable points to any
211 * addrinfo structure that would need to be freed.
213 static struct addrinfo
*ai
;
217 * We divy out chunks of memory rather than call malloc each time so
218 * we don't have to worry about leaking memory. It's probably
219 * not a big deal if all this memory was wasted but if this ever
220 * goes into a library that would probably not be a good idea.
222 * XXX - this *is* in a library....
225 #define CHUNK0SIZE 1024
231 static struct chunk chunks
[NCHUNKS
];
232 static int cur_chunk
;
234 static void *newchunk(u_int
);
235 static void freechunks(void);
236 static inline struct block
*new_block(int);
237 static inline struct slist
*new_stmt(int);
238 static struct block
*gen_retblk(int);
239 static inline void syntax(void);
241 static void backpatch(struct block
*, struct block
*);
242 static void merge(struct block
*, struct block
*);
243 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
244 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
245 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
246 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
247 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
248 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
250 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
251 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
252 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
253 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
254 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
255 static struct slist
*gen_loadx_iphdrlen(void);
256 static struct block
*gen_uncond(int);
257 static inline struct block
*gen_true(void);
258 static inline struct block
*gen_false(void);
259 static struct block
*gen_ether_linktype(int);
260 static struct block
*gen_ipnet_linktype(int);
261 static struct block
*gen_linux_sll_linktype(int);
262 static struct slist
*gen_load_prism_llprefixlen(void);
263 static struct slist
*gen_load_avs_llprefixlen(void);
264 static struct slist
*gen_load_radiotap_llprefixlen(void);
265 static struct slist
*gen_load_ppi_llprefixlen(void);
266 static void insert_compute_vloffsets(struct block
*);
267 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
268 static int ethertype_to_ppptype(int);
269 static struct block
*gen_linktype(int);
270 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
271 static struct block
*gen_llc_linktype(int);
272 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
274 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
276 static struct block
*gen_ahostop(const u_char
*, int);
277 static struct block
*gen_ehostop(const u_char
*, int);
278 static struct block
*gen_fhostop(const u_char
*, int);
279 static struct block
*gen_thostop(const u_char
*, int);
280 static struct block
*gen_wlanhostop(const u_char
*, int);
281 static struct block
*gen_ipfchostop(const u_char
*, int);
282 static struct block
*gen_dnhostop(bpf_u_int32
, int);
283 static struct block
*gen_mpls_linktype(int);
284 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
286 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
289 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
291 static struct block
*gen_ipfrag(void);
292 static struct block
*gen_portatom(int, bpf_int32
);
293 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
294 static struct block
*gen_portatom6(int, bpf_int32
);
295 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
296 struct block
*gen_portop(int, int, int);
297 static struct block
*gen_port(int, int, int);
298 struct block
*gen_portrangeop(int, int, int, int);
299 static struct block
*gen_portrange(int, int, int, int);
300 struct block
*gen_portop6(int, int, int);
301 static struct block
*gen_port6(int, int, int);
302 struct block
*gen_portrangeop6(int, int, int, int);
303 static struct block
*gen_portrange6(int, int, int, int);
304 static int lookup_proto(const char *, int);
305 static struct block
*gen_protochain(int, int, int);
306 static struct block
*gen_proto(int, int, int);
307 static struct slist
*xfer_to_x(struct arth
*);
308 static struct slist
*xfer_to_a(struct arth
*);
309 static struct block
*gen_mac_multicast(int);
310 static struct block
*gen_len(int, int);
311 static struct block
*gen_check_802_11_data_frame(void);
313 static struct block
*gen_ppi_dlt_check(void);
314 static struct block
*gen_msg_abbrev(int type
);
325 /* XXX Round up to nearest long. */
326 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
328 /* XXX Round up to structure boundary. */
332 cp
= &chunks
[cur_chunk
];
333 if (n
> cp
->n_left
) {
334 ++cp
, k
= ++cur_chunk
;
336 bpf_error("out of memory");
337 size
= CHUNK0SIZE
<< k
;
338 cp
->m
= (void *)malloc(size
);
340 bpf_error("out of memory");
341 memset((char *)cp
->m
, 0, size
);
344 bpf_error("out of memory");
347 return (void *)((char *)cp
->m
+ cp
->n_left
);
356 for (i
= 0; i
< NCHUNKS
; ++i
)
357 if (chunks
[i
].m
!= NULL
) {
364 * A strdup whose allocations are freed after code generation is over.
368 register const char *s
;
370 int n
= strlen(s
) + 1;
371 char *cp
= newchunk(n
);
377 static inline struct block
*
383 p
= (struct block
*)newchunk(sizeof(*p
));
390 static inline struct slist
*
396 p
= (struct slist
*)newchunk(sizeof(*p
));
402 static struct block
*
406 struct block
*b
= new_block(BPF_RET
|BPF_K
);
415 bpf_error("syntax error in filter expression");
418 static bpf_u_int32 netmask
;
423 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
424 const char *buf
, int optimize
, bpf_u_int32 mask
)
427 const char * volatile xbuf
= buf
;
432 * XXX - single-thread this code path with pthread calls on
433 * UN*X, if the platform supports pthreads? If that requires
434 * a separate -lpthread, we might not want to do that.
437 extern int wsockinit (void);
443 EnterCriticalSection(&g_PcapCompileCriticalSection
);
447 * If this pcap_t hasn't been activated, it doesn't have a
448 * link-layer type, so we can't use it.
451 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
452 "not-yet-activated pcap_t passed to pcap_compile");
462 if (setjmp(top_ctx
)) {
477 snaplen
= pcap_snapshot(p
);
479 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
480 "snaplen of 0 rejects all packets");
485 lex_init(xbuf
? xbuf
: "");
493 root
= gen_retblk(snaplen
);
495 if (optimize
&& !no_optimize
) {
498 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
499 bpf_error("expression rejects all packets");
501 program
->bf_insns
= icode_to_fcode(root
, &len
);
502 program
->bf_len
= len
;
507 rc
= 0; /* We're all okay */
512 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
519 * entry point for using the compiler with no pcap open
520 * pass in all the stuff that is needed explicitly instead.
523 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
524 struct bpf_program
*program
,
525 const char *buf
, int optimize
, bpf_u_int32 mask
)
530 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
533 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
539 * Clean up a "struct bpf_program" by freeing all the memory allocated
543 pcap_freecode(struct bpf_program
*program
)
546 if (program
->bf_insns
!= NULL
) {
547 free((char *)program
->bf_insns
);
548 program
->bf_insns
= NULL
;
553 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
554 * which of the jt and jf fields has been resolved and which is a pointer
555 * back to another unresolved block (or nil). At least one of the fields
556 * in each block is already resolved.
559 backpatch(list
, target
)
560 struct block
*list
, *target
;
577 * Merge the lists in b0 and b1, using the 'sense' field to indicate
578 * which of jt and jf is the link.
582 struct block
*b0
, *b1
;
584 register struct block
**p
= &b0
;
586 /* Find end of list. */
588 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
590 /* Concatenate the lists. */
598 struct block
*ppi_dlt_check
;
601 * Insert before the statements of the first (root) block any
602 * statements needed to load the lengths of any variable-length
603 * headers into registers.
605 * XXX - a fancier strategy would be to insert those before the
606 * statements of all blocks that use those lengths and that
607 * have no predecessors that use them, so that we only compute
608 * the lengths if we need them. There might be even better
609 * approaches than that.
611 * However, those strategies would be more complicated, and
612 * as we don't generate code to compute a length if the
613 * program has no tests that use the length, and as most
614 * tests will probably use those lengths, we would just
615 * postpone computing the lengths so that it's not done
616 * for tests that fail early, and it's not clear that's
619 insert_compute_vloffsets(p
->head
);
622 * For DLT_PPI captures, generate a check of the per-packet
623 * DLT value to make sure it's DLT_IEEE802_11.
625 ppi_dlt_check
= gen_ppi_dlt_check();
626 if (ppi_dlt_check
!= NULL
)
627 gen_and(ppi_dlt_check
, p
);
629 backpatch(p
, gen_retblk(snaplen
));
630 p
->sense
= !p
->sense
;
631 backpatch(p
, gen_retblk(0));
637 struct block
*b0
, *b1
;
639 backpatch(b0
, b1
->head
);
640 b0
->sense
= !b0
->sense
;
641 b1
->sense
= !b1
->sense
;
643 b1
->sense
= !b1
->sense
;
649 struct block
*b0
, *b1
;
651 b0
->sense
= !b0
->sense
;
652 backpatch(b0
, b1
->head
);
653 b0
->sense
= !b0
->sense
;
662 b
->sense
= !b
->sense
;
665 static struct block
*
666 gen_cmp(offrel
, offset
, size
, v
)
667 enum e_offrel offrel
;
671 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
674 static struct block
*
675 gen_cmp_gt(offrel
, offset
, size
, v
)
676 enum e_offrel offrel
;
680 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
683 static struct block
*
684 gen_cmp_ge(offrel
, offset
, size
, v
)
685 enum e_offrel offrel
;
689 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
692 static struct block
*
693 gen_cmp_lt(offrel
, offset
, size
, v
)
694 enum e_offrel offrel
;
698 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
701 static struct block
*
702 gen_cmp_le(offrel
, offset
, size
, v
)
703 enum e_offrel offrel
;
707 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
710 static struct block
*
711 gen_mcmp(offrel
, offset
, size
, v
, mask
)
712 enum e_offrel offrel
;
717 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
720 static struct block
*
721 gen_bcmp(offrel
, offset
, size
, v
)
722 enum e_offrel offrel
;
723 register u_int offset
, size
;
724 register const u_char
*v
;
726 register struct block
*b
, *tmp
;
730 register const u_char
*p
= &v
[size
- 4];
731 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
732 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
734 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
741 register const u_char
*p
= &v
[size
- 2];
742 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
744 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
751 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
760 * AND the field of size "size" at offset "offset" relative to the header
761 * specified by "offrel" with "mask", and compare it with the value "v"
762 * with the test specified by "jtype"; if "reverse" is true, the test
763 * should test the opposite of "jtype".
765 static struct block
*
766 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
767 enum e_offrel offrel
;
769 bpf_u_int32 offset
, size
, mask
, jtype
;
772 struct slist
*s
, *s2
;
775 s
= gen_load_a(offrel
, offset
, size
);
777 if (mask
!= 0xffffffff) {
778 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
783 b
= new_block(JMP(jtype
));
786 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
792 * Various code constructs need to know the layout of the packet.
793 * These variables give the necessary offsets from the beginning
794 * of the packet data.
798 * Absolute offset of the beginning of the link-layer header.
800 static bpf_abs_offset off_linkhdr
;
803 * If we're checking a link-layer header for a packet encapsulated in
804 * another protocol layer, this is the equivalent information for the
805 * previous layers' link-layer header from the beginning of the raw
808 static bpf_abs_offset off_prevlinkhdr
;
811 * This is the equivalent information for the outermost layers' link-layer
814 static bpf_abs_offset off_outermostlinkhdr
;
817 * "Push" the current value of the link-layer header type and link-layer
818 * header offset onto a "stack", and set a new value. (It's not a
819 * full-blown stack; we keep only the top two items.)
821 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
823 prevlinktype = new_linktype; \
824 off_prevlinkhdr = off_linkhdr; \
825 linktype = new_linktype; \
826 off_linkhdr.is_variable = new_is_variable; \
827 off_linkhdr.constant_part = new_constant_part; \
828 off_linkhdr.reg = new_reg; \
832 * Absolute offset of the beginning of the link-layer payload.
834 static bpf_abs_offset off_linkpl
;
837 * "off_linktype" is the offset to information in the link-layer header
838 * giving the packet type. This offset is relative to the beginning
839 * of the link-layer header - i.e., it doesn't include off_linkhdr.constant_part - so
840 * loads with an offset that includes "off_linktype" should use
843 * For Ethernet, it's the offset of the Ethernet type field; this
844 * means that it must have a value that skips VLAN tags.
846 * For link-layer types that always use 802.2 headers, it's the
847 * offset of the LLC header; this means that it must have a value
848 * that skips VLAN tags.
850 * For PPP, it's the offset of the PPP type field.
852 * For Cisco HDLC, it's the offset of the CHDLC type field.
854 * For BSD loopback, it's the offset of the AF_ value.
856 * For Linux cooked sockets, it's the offset of the type field.
858 * It's set to -1 for no encapsulation, in which case, IP is assumed.
860 static u_int off_linktype
;
863 * TRUE if the link layer includes an ATM pseudo-header.
865 static int is_atm
= 0;
868 * These are offsets for the ATM pseudo-header.
870 static u_int off_vpi
;
871 static u_int off_vci
;
872 static u_int off_proto
;
875 * These are offsets for the MTP2 fields.
878 static u_int off_li_hsl
;
881 * These are offsets for the MTP3 fields.
883 static u_int off_sio
;
884 static u_int off_opc
;
885 static u_int off_dpc
;
886 static u_int off_sls
;
889 * This is the offset of the first byte after the ATM pseudo_header,
890 * or -1 if there is no ATM pseudo-header.
892 static u_int off_payload
;
895 * These are offsets to the beginning of the network-layer header.
896 * They are relative to the beginning of the link-layer payload (i.e.,
897 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
899 * If the link layer never uses 802.2 LLC:
901 * "off_nl" and "off_nl_nosnap" are the same.
903 * If the link layer always uses 802.2 LLC:
905 * "off_nl" is the offset if there's a SNAP header following
908 * "off_nl_nosnap" is the offset if there's no SNAP header.
910 * If the link layer is Ethernet:
912 * "off_nl" is the offset if the packet is an Ethernet II packet
913 * (we assume no 802.3+802.2+SNAP);
915 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
916 * with an 802.2 header following it.
919 static u_int off_nl_nosnap
;
922 static int prevlinktype
;
923 static int outermostlinktype
;
929 pcap_fddipad
= p
->fddipad
;
932 * We start out with only one link-layer header.
934 outermostlinktype
= pcap_datalink(p
);
935 off_outermostlinkhdr
.constant_part
= 0;
936 off_outermostlinkhdr
.is_variable
= 0;
937 off_outermostlinkhdr
.reg
= -1;
939 prevlinktype
= outermostlinktype
;
940 off_prevlinkhdr
.constant_part
= 0;
941 off_prevlinkhdr
.is_variable
= 0;
942 off_prevlinkhdr
.reg
= -1;
944 linktype
= outermostlinktype
;
945 off_linkhdr
.constant_part
= 0;
946 off_linkhdr
.is_variable
= 0;
947 off_linkhdr
.reg
= -1;
952 off_linkpl
.constant_part
= 0;
953 off_linkpl
.is_variable
= 0;
957 * Assume it's not raw ATM with a pseudo-header, for now.
966 * And assume we're not doing SS7.
975 label_stack_depth
= 0;
976 vlan_stack_depth
= 0;
982 off_linkpl
.constant_part
= 6;
983 off_nl
= 0; /* XXX in reality, variable! */
984 off_nl_nosnap
= 0; /* no 802.2 LLC */
987 case DLT_ARCNET_LINUX
:
989 off_linkpl
.constant_part
= 8;
990 off_nl
= 0; /* XXX in reality, variable! */
991 off_nl_nosnap
= 0; /* no 802.2 LLC */
996 off_linkpl
.constant_part
= 14; /* Ethernet header length */
997 off_nl
= 0; /* Ethernet II */
998 off_nl_nosnap
= 3; /* 802.3+802.2 */
1003 * SLIP doesn't have a link level type. The 16 byte
1004 * header is hacked into our SLIP driver.
1007 off_linkpl
.constant_part
= 16;
1009 off_nl_nosnap
= 0; /* no 802.2 LLC */
1012 case DLT_SLIP_BSDOS
:
1013 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1016 off_linkpl
.constant_part
= 24;
1018 off_nl_nosnap
= 0; /* no 802.2 LLC */
1024 off_linkpl
.constant_part
= 4;
1026 off_nl_nosnap
= 0; /* no 802.2 LLC */
1031 off_linkpl
.constant_part
= 12;
1033 off_nl_nosnap
= 0; /* no 802.2 LLC */
1038 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1039 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1040 off_linktype
= 2; /* skip HDLC-like framing */
1041 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1043 off_nl_nosnap
= 0; /* no 802.2 LLC */
1048 * This does no include the Ethernet header, and
1049 * only covers session state.
1052 off_linkpl
.constant_part
= 8;
1054 off_nl_nosnap
= 0; /* no 802.2 LLC */
1059 off_linkpl
.constant_part
= 24;
1061 off_nl_nosnap
= 0; /* no 802.2 LLC */
1066 * FDDI doesn't really have a link-level type field.
1067 * We set "off_linktype" to the offset of the LLC header.
1069 * To check for Ethernet types, we assume that SSAP = SNAP
1070 * is being used and pick out the encapsulated Ethernet type.
1071 * XXX - should we generate code to check for SNAP?
1074 off_linktype
+= pcap_fddipad
;
1075 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1076 off_linkpl
.constant_part
+= pcap_fddipad
;
1077 off_nl
= 8; /* 802.2+SNAP */
1078 off_nl_nosnap
= 3; /* 802.2 */
1083 * Token Ring doesn't really have a link-level type field.
1084 * We set "off_linktype" to the offset of the LLC header.
1086 * To check for Ethernet types, we assume that SSAP = SNAP
1087 * is being used and pick out the encapsulated Ethernet type.
1088 * XXX - should we generate code to check for SNAP?
1090 * XXX - the header is actually variable-length.
1091 * Some various Linux patched versions gave 38
1092 * as "off_linktype" and 40 as "off_nl"; however,
1093 * if a token ring packet has *no* routing
1094 * information, i.e. is not source-routed, the correct
1095 * values are 20 and 22, as they are in the vanilla code.
1097 * A packet is source-routed iff the uppermost bit
1098 * of the first byte of the source address, at an
1099 * offset of 8, has the uppermost bit set. If the
1100 * packet is source-routed, the total number of bytes
1101 * of routing information is 2 plus bits 0x1F00 of
1102 * the 16-bit value at an offset of 14 (shifted right
1103 * 8 - figure out which byte that is).
1106 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1107 off_nl
= 8; /* 802.2+SNAP */
1108 off_nl_nosnap
= 3; /* 802.2 */
1111 case DLT_IEEE802_11
:
1112 case DLT_PRISM_HEADER
:
1113 case DLT_IEEE802_11_RADIO_AVS
:
1114 case DLT_IEEE802_11_RADIO
:
1116 * 802.11 doesn't really have a link-level type field.
1117 * We set "off_linktype" to the offset of the LLC header.
1119 * To check for Ethernet types, we assume that SSAP = SNAP
1120 * is being used and pick out the encapsulated Ethernet type.
1121 * XXX - should we generate code to check for SNAP?
1123 * We also handle variable-length radio headers here.
1124 * The Prism header is in theory variable-length, but in
1125 * practice it's always 144 bytes long. However, some
1126 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1127 * sometimes or always supply an AVS header, so we
1128 * have to check whether the radio header is a Prism
1129 * header or an AVS header, so, in practice, it's
1133 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1134 off_linkpl
.is_variable
= 1;
1135 off_nl
= 8; /* 802.2+SNAP */
1136 off_nl_nosnap
= 3; /* 802.2 */
1141 * At the moment we treat PPI the same way that we treat
1142 * normal Radiotap encoded packets. The difference is in
1143 * the function that generates the code at the beginning
1144 * to compute the header length. Since this code generator
1145 * of PPI supports bare 802.11 encapsulation only (i.e.
1146 * the encapsulated DLT should be DLT_IEEE802_11) we
1147 * generate code to check for this too.
1150 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1151 off_linkpl
.is_variable
= 1;
1152 off_nl
= 8; /* 802.2+SNAP */
1153 off_nl_nosnap
= 3; /* 802.2 */
1156 case DLT_ATM_RFC1483
:
1157 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1159 * assume routed, non-ISO PDUs
1160 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1162 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1163 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1164 * latter would presumably be treated the way PPPoE
1165 * should be, so you can do "pppoe and udp port 2049"
1166 * or "pppoa and tcp port 80" and have it check for
1167 * PPPo{A,E} and a PPP protocol of IP and....
1170 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1171 off_nl
= 8; /* 802.2+SNAP */
1172 off_nl_nosnap
= 3; /* 802.2 */
1177 * Full Frontal ATM; you get AALn PDUs with an ATM
1181 off_vpi
= SUNATM_VPI_POS
;
1182 off_vci
= SUNATM_VCI_POS
;
1183 off_proto
= PROTO_POS
;
1184 off_payload
= SUNATM_PKT_BEGIN_POS
;
1185 off_linktype
= off_payload
;
1186 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1187 off_nl
= 8; /* 802.2+SNAP */
1188 off_nl_nosnap
= 3; /* 802.2 */
1195 off_linkpl
.constant_part
= 0;
1197 off_nl_nosnap
= 0; /* no 802.2 LLC */
1200 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1202 off_linkpl
.constant_part
= 16;
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.
1214 off_linkpl
.constant_part
= 0;
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.
1231 off_linkpl
.constant_part
= 16;
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).
1242 off_linkpl
.constant_part
= 0;
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);
1254 off_linkpl
.constant_part
= 0;
1256 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1259 case DLT_APPLE_IP_OVER_IEEE1394
:
1261 off_linkpl
.constant_part
= 18;
1263 off_nl_nosnap
= 0; /* no 802.2 LLC */
1266 case DLT_SYMANTEC_FIREWALL
:
1268 off_linkpl
.constant_part
= 44;
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_linkpl
.constant_part
= 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
:
1289 off_linkpl
.constant_part
= 4;
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_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1301 case DLT_JUNIPER_ATM2
:
1302 off_linktype
= 8; /* in reality variable between 8-12 */
1303 off_linkpl
.constant_part
= 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
:
1312 off_linkpl
.constant_part
= 14;
1314 off_nl
= 18; /* Ethernet II */
1315 off_nl_nosnap
= 21; /* 802.3+802.2 */
1318 case DLT_JUNIPER_PPPOE_ATM
:
1320 off_linkpl
.constant_part
= 6;
1322 off_nl_nosnap
= -1; /* no 802.2 LLC */
1325 case DLT_JUNIPER_GGSN
:
1327 off_linkpl
.constant_part
= 12;
1329 off_nl_nosnap
= -1; /* no 802.2 LLC */
1332 case DLT_JUNIPER_ES
:
1334 off_linkpl
.constant_part
= -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
:
1341 off_linkpl
.constant_part
= 12;
1342 off_nl
= 0; /* raw IP/IP6 header */
1343 off_nl_nosnap
= -1; /* no 802.2 LLC */
1346 case DLT_BACNET_MS_TP
:
1348 off_linkpl
.constant_part
= -1;
1353 case DLT_JUNIPER_SERVICES
:
1355 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1356 off_nl
= -1; /* L3 proto location dep. on cookie type */
1357 off_nl_nosnap
= -1; /* no 802.2 LLC */
1360 case DLT_JUNIPER_VP
:
1362 off_linkpl
.constant_part
= -1;
1367 case DLT_JUNIPER_ST
:
1369 off_linkpl
.constant_part
= -1;
1374 case DLT_JUNIPER_ISM
:
1376 off_linkpl
.constant_part
= -1;
1381 case DLT_JUNIPER_VS
:
1382 case DLT_JUNIPER_SRX_E2E
:
1383 case DLT_JUNIPER_FIBRECHANNEL
:
1384 case DLT_JUNIPER_ATM_CEMIC
:
1386 off_linkpl
.constant_part
= -1;
1399 off_linkpl
.constant_part
= -1;
1404 case DLT_MTP2_WITH_PHDR
:
1412 off_linkpl
.constant_part
= -1;
1425 off_linkpl
.constant_part
= -1;
1432 off_linkpl
.constant_part
= 4;
1439 * Currently, only raw "link[N:M]" filtering is supported.
1441 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1442 off_linkpl
.constant_part
= -1;
1443 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1444 off_nl_nosnap
= -1; /* no 802.2 LLC */
1449 off_linkpl
.constant_part
= 24; /* ipnet header length */
1454 case DLT_NETANALYZER
:
1455 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1457 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1458 off_nl
= 0; /* Ethernet II */
1459 off_nl_nosnap
= 3; /* 802.3+802.2 */
1462 case DLT_NETANALYZER_TRANSPARENT
:
1463 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1465 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1466 off_nl
= 0; /* Ethernet II */
1467 off_nl_nosnap
= 3; /* 802.3+802.2 */
1472 * For values in the range in which we've assigned new
1473 * DLT_ values, only raw "link[N:M]" filtering is supported.
1475 if (linktype
>= DLT_MATCHING_MIN
&&
1476 linktype
<= DLT_MATCHING_MAX
) {
1478 off_linkpl
.constant_part
= -1;
1485 bpf_error("unknown data link type %d", linktype
);
1490 * Load a value relative to the specified absolute offset.
1492 static struct slist
*
1493 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1495 struct slist
*s
, *s2
;
1497 s
= gen_abs_offset_varpart(abs_offset
);
1500 * If "s" is non-null, it has code to arrange that the X register
1501 * contains the variable part of the absolute offset, so we
1502 * generate a load relative to that, with an offset of
1503 * abs_offset->constant_part + offset.
1505 * Otherwise, we can do an absolute load with an offset of
1506 * abs_offset->constant_part + offset.
1510 * "s" points to a list of statements that puts the
1511 * variable part of the absolute offset into the X register.
1512 * Do an indirect load, to use the X register as an offset.
1514 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1515 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1519 * There is no variable part of the absolute offset, so
1520 * just do an absolute load.
1522 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1523 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1529 * Load a value relative to the beginning of the specified header.
1531 static struct slist
*
1532 gen_load_a(offrel
, offset
, size
)
1533 enum e_offrel offrel
;
1536 struct slist
*s
, *s2
;
1541 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1546 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1549 case OR_PREVLINKHDR
:
1550 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1556 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1560 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1564 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1569 * Load the X register with the length of the IPv4 header
1570 * (plus the offset of the link-layer header, if it's
1571 * preceded by a variable-length header such as a radio
1572 * header), in bytes.
1574 s
= gen_loadx_iphdrlen();
1577 * Load the item at {offset of the link-layer payload} +
1578 * {offset, relative to the start of the link-layer
1579 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1580 * {specified offset}.
1582 * If the offset of the link-layer payload is variable,
1583 * the variable part of that offset is included in the
1584 * value in the X register, and we include the constant
1585 * part in the offset of the load.
1587 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1588 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1593 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1604 * Generate code to load into the X register the sum of the length of
1605 * the IPv4 header and the variable part of the offset of the link-layer
1608 static struct slist
*
1609 gen_loadx_iphdrlen()
1611 struct slist
*s
, *s2
;
1613 s
= gen_abs_offset_varpart(&off_linkpl
);
1616 * The offset of the link-layer payload has a variable
1617 * part. "s" points to a list of statements that put
1618 * the variable part of that offset into the X register.
1620 * The 4*([k]&0xf) addressing mode can't be used, as we
1621 * don't have a constant offset, so we have to load the
1622 * value in question into the A register and add to it
1623 * the value from the X register.
1625 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1628 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1631 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1636 * The A register now contains the length of the IP header.
1637 * We need to add to it the variable part of the offset of
1638 * the link-layer payload, which is still in the X
1639 * register, and move the result into the X register.
1641 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1642 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1645 * The offset of the link-layer payload is a constant,
1646 * so no code was generated to load the (non-existent)
1647 * variable part of that offset.
1649 * This means we can use the 4*([k]&0xf) addressing
1650 * mode. Load the length of the IPv4 header, which
1651 * is at an offset of off_nl from the beginning of
1652 * the link-layer payload, and thus at an offset of
1653 * off_linkpl.constant_part + off_nl from the beginning
1654 * of the raw packet data, using that addressing mode.
1656 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1657 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1662 static struct block
*
1669 s
= new_stmt(BPF_LD
|BPF_IMM
);
1671 b
= new_block(JMP(BPF_JEQ
));
1677 static inline struct block
*
1680 return gen_uncond(1);
1683 static inline struct block
*
1686 return gen_uncond(0);
1690 * Byte-swap a 32-bit number.
1691 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1692 * big-endian platforms.)
1694 #define SWAPLONG(y) \
1695 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1698 * Generate code to match a particular packet type.
1700 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1701 * value, if <= ETHERMTU. We use that to determine whether to
1702 * match the type/length field or to check the type/length field for
1703 * a value <= ETHERMTU to see whether it's a type field and then do
1704 * the appropriate test.
1706 static struct block
*
1707 gen_ether_linktype(proto
)
1710 struct block
*b0
, *b1
;
1716 case LLCSAP_NETBEUI
:
1718 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1719 * so we check the DSAP and SSAP.
1721 * LLCSAP_IP checks for IP-over-802.2, rather
1722 * than IP-over-Ethernet or IP-over-SNAP.
1724 * XXX - should we check both the DSAP and the
1725 * SSAP, like this, or should we check just the
1726 * DSAP, as we do for other types <= ETHERMTU
1727 * (i.e., other SAP values)?
1729 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1731 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1732 ((proto
<< 8) | proto
));
1740 * Ethernet_II frames, which are Ethernet
1741 * frames with a frame type of ETHERTYPE_IPX;
1743 * Ethernet_802.3 frames, which are 802.3
1744 * frames (i.e., the type/length field is
1745 * a length field, <= ETHERMTU, rather than
1746 * a type field) with the first two bytes
1747 * after the Ethernet/802.3 header being
1750 * Ethernet_802.2 frames, which are 802.3
1751 * frames with an 802.2 LLC header and
1752 * with the IPX LSAP as the DSAP in the LLC
1755 * Ethernet_SNAP frames, which are 802.3
1756 * frames with an LLC header and a SNAP
1757 * header and with an OUI of 0x000000
1758 * (encapsulated Ethernet) and a protocol
1759 * ID of ETHERTYPE_IPX in the SNAP header.
1761 * XXX - should we generate the same code both
1762 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1766 * This generates code to check both for the
1767 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1769 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1770 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1774 * Now we add code to check for SNAP frames with
1775 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1777 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1781 * Now we generate code to check for 802.3
1782 * frames in general.
1784 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1788 * Now add the check for 802.3 frames before the
1789 * check for Ethernet_802.2 and Ethernet_802.3,
1790 * as those checks should only be done on 802.3
1791 * frames, not on Ethernet frames.
1796 * Now add the check for Ethernet_II frames, and
1797 * do that before checking for the other frame
1800 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1801 (bpf_int32
)ETHERTYPE_IPX
);
1805 case ETHERTYPE_ATALK
:
1806 case ETHERTYPE_AARP
:
1808 * EtherTalk (AppleTalk protocols on Ethernet link
1809 * layer) may use 802.2 encapsulation.
1813 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1814 * we check for an Ethernet type field less than
1815 * 1500, which means it's an 802.3 length field.
1817 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1821 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1822 * SNAP packets with an organization code of
1823 * 0x080007 (Apple, for Appletalk) and a protocol
1824 * type of ETHERTYPE_ATALK (Appletalk).
1826 * 802.2-encapsulated ETHERTYPE_AARP packets are
1827 * SNAP packets with an organization code of
1828 * 0x000000 (encapsulated Ethernet) and a protocol
1829 * type of ETHERTYPE_AARP (Appletalk ARP).
1831 if (proto
== ETHERTYPE_ATALK
)
1832 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1833 else /* proto == ETHERTYPE_AARP */
1834 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1838 * Check for Ethernet encapsulation (Ethertalk
1839 * phase 1?); we just check for the Ethernet
1842 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1848 if (proto
<= ETHERMTU
) {
1850 * This is an LLC SAP value, so the frames
1851 * that match would be 802.2 frames.
1852 * Check that the frame is an 802.2 frame
1853 * (i.e., that the length/type field is
1854 * a length field, <= ETHERMTU) and
1855 * then check the DSAP.
1857 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1859 b1
= gen_cmp(OR_LINKHDR
, off_linktype
+ 2, BPF_B
,
1865 * This is an Ethernet type, so compare
1866 * the length/type field with it (if
1867 * the frame is an 802.2 frame, the length
1868 * field will be <= ETHERMTU, and, as
1869 * "proto" is > ETHERMTU, this test
1870 * will fail and the frame won't match,
1871 * which is what we want).
1873 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1880 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1881 * or IPv6 then we have an error.
1883 static struct block
*
1884 gen_ipnet_linktype(proto
)
1890 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1891 (bpf_int32
)IPH_AF_INET
);
1894 case ETHERTYPE_IPV6
:
1895 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1896 (bpf_int32
)IPH_AF_INET6
);
1907 * Generate code to match a particular packet type.
1909 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1910 * value, if <= ETHERMTU. We use that to determine whether to
1911 * match the type field or to check the type field for the special
1912 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1914 static struct block
*
1915 gen_linux_sll_linktype(proto
)
1918 struct block
*b0
, *b1
;
1924 case LLCSAP_NETBEUI
:
1926 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1927 * so we check the DSAP and SSAP.
1929 * LLCSAP_IP checks for IP-over-802.2, rather
1930 * than IP-over-Ethernet or IP-over-SNAP.
1932 * XXX - should we check both the DSAP and the
1933 * SSAP, like this, or should we check just the
1934 * DSAP, as we do for other types <= ETHERMTU
1935 * (i.e., other SAP values)?
1937 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1938 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1939 ((proto
<< 8) | proto
));
1945 * Ethernet_II frames, which are Ethernet
1946 * frames with a frame type of ETHERTYPE_IPX;
1948 * Ethernet_802.3 frames, which have a frame
1949 * type of LINUX_SLL_P_802_3;
1951 * Ethernet_802.2 frames, which are 802.3
1952 * frames with an 802.2 LLC header (i.e, have
1953 * a frame type of LINUX_SLL_P_802_2) and
1954 * with the IPX LSAP as the DSAP in the LLC
1957 * Ethernet_SNAP frames, which are 802.3
1958 * frames with an LLC header and a SNAP
1959 * header and with an OUI of 0x000000
1960 * (encapsulated Ethernet) and a protocol
1961 * ID of ETHERTYPE_IPX in the SNAP header.
1963 * First, do the checks on LINUX_SLL_P_802_2
1964 * frames; generate the check for either
1965 * Ethernet_802.2 or Ethernet_SNAP frames, and
1966 * then put a check for LINUX_SLL_P_802_2 frames
1969 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1970 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1972 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1976 * Now check for 802.3 frames and OR that with
1977 * the previous test.
1979 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1983 * Now add the check for Ethernet_II frames, and
1984 * do that before checking for the other frame
1987 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1988 (bpf_int32
)ETHERTYPE_IPX
);
1992 case ETHERTYPE_ATALK
:
1993 case ETHERTYPE_AARP
:
1995 * EtherTalk (AppleTalk protocols on Ethernet link
1996 * layer) may use 802.2 encapsulation.
2000 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2001 * we check for the 802.2 protocol type in the
2002 * "Ethernet type" field.
2004 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2007 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2008 * SNAP packets with an organization code of
2009 * 0x080007 (Apple, for Appletalk) and a protocol
2010 * type of ETHERTYPE_ATALK (Appletalk).
2012 * 802.2-encapsulated ETHERTYPE_AARP packets are
2013 * SNAP packets with an organization code of
2014 * 0x000000 (encapsulated Ethernet) and a protocol
2015 * type of ETHERTYPE_AARP (Appletalk ARP).
2017 if (proto
== ETHERTYPE_ATALK
)
2018 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2019 else /* proto == ETHERTYPE_AARP */
2020 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2024 * Check for Ethernet encapsulation (Ethertalk
2025 * phase 1?); we just check for the Ethernet
2028 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2034 if (proto
<= ETHERMTU
) {
2036 * This is an LLC SAP value, so the frames
2037 * that match would be 802.2 frames.
2038 * Check for the 802.2 protocol type
2039 * in the "Ethernet type" field, and
2040 * then check the DSAP.
2042 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2044 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2050 * This is an Ethernet type, so compare
2051 * the length/type field with it (if
2052 * the frame is an 802.2 frame, the length
2053 * field will be <= ETHERMTU, and, as
2054 * "proto" is > ETHERMTU, this test
2055 * will fail and the frame won't match,
2056 * which is what we want).
2058 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2064 static struct slist
*
2065 gen_load_prism_llprefixlen()
2067 struct slist
*s1
, *s2
;
2068 struct slist
*sjeq_avs_cookie
;
2069 struct slist
*sjcommon
;
2072 * This code is not compatible with the optimizer, as
2073 * we are generating jmp instructions within a normal
2074 * slist of instructions
2079 * Generate code to load the length of the radio header into
2080 * the register assigned to hold that length, if one has been
2081 * assigned. (If one hasn't been assigned, no code we've
2082 * generated uses that prefix, so we don't need to generate any
2085 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2086 * or always use the AVS header rather than the Prism header.
2087 * We load a 4-byte big-endian value at the beginning of the
2088 * raw packet data, and see whether, when masked with 0xFFFFF000,
2089 * it's equal to 0x80211000. If so, that indicates that it's
2090 * an AVS header (the masked-out bits are the version number).
2091 * Otherwise, it's a Prism header.
2093 * XXX - the Prism header is also, in theory, variable-length,
2094 * but no known software generates headers that aren't 144
2097 if (off_linkhdr
.reg
!= -1) {
2101 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2105 * AND it with 0xFFFFF000.
2107 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2108 s2
->s
.k
= 0xFFFFF000;
2112 * Compare with 0x80211000.
2114 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2115 sjeq_avs_cookie
->s
.k
= 0x80211000;
2116 sappend(s1
, sjeq_avs_cookie
);
2121 * The 4 bytes at an offset of 4 from the beginning of
2122 * the AVS header are the length of the AVS header.
2123 * That field is big-endian.
2125 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2128 sjeq_avs_cookie
->s
.jt
= s2
;
2131 * Now jump to the code to allocate a register
2132 * into which to save the header length and
2133 * store the length there. (The "jump always"
2134 * instruction needs to have the k field set;
2135 * it's added to the PC, so, as we're jumping
2136 * over a single instruction, it should be 1.)
2138 sjcommon
= new_stmt(JMP(BPF_JA
));
2140 sappend(s1
, sjcommon
);
2143 * Now for the code that handles the Prism header.
2144 * Just load the length of the Prism header (144)
2145 * into the A register. Have the test for an AVS
2146 * header branch here if we don't have an AVS header.
2148 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2151 sjeq_avs_cookie
->s
.jf
= s2
;
2154 * Now allocate a register to hold that value and store
2155 * it. The code for the AVS header will jump here after
2156 * loading the length of the AVS header.
2158 s2
= new_stmt(BPF_ST
);
2159 s2
->s
.k
= off_linkhdr
.reg
;
2161 sjcommon
->s
.jf
= s2
;
2164 * Now move it into the X register.
2166 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2174 static struct slist
*
2175 gen_load_avs_llprefixlen()
2177 struct slist
*s1
, *s2
;
2180 * Generate code to load the length of the AVS header into
2181 * the register assigned to hold that length, if one has been
2182 * assigned. (If one hasn't been assigned, no code we've
2183 * generated uses that prefix, so we don't need to generate any
2186 if (off_linkhdr
.reg
!= -1) {
2188 * The 4 bytes at an offset of 4 from the beginning of
2189 * the AVS header are the length of the AVS header.
2190 * That field is big-endian.
2192 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2196 * Now allocate a register to hold that value and store
2199 s2
= new_stmt(BPF_ST
);
2200 s2
->s
.k
= off_linkhdr
.reg
;
2204 * Now move it into the X register.
2206 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2214 static struct slist
*
2215 gen_load_radiotap_llprefixlen()
2217 struct slist
*s1
, *s2
;
2220 * Generate code to load the length of the radiotap header into
2221 * the register assigned to hold that length, if one has been
2222 * assigned. (If one hasn't been assigned, no code we've
2223 * generated uses that prefix, so we don't need to generate any
2226 if (off_linkhdr
.reg
!= -1) {
2228 * The 2 bytes at offsets of 2 and 3 from the beginning
2229 * of the radiotap header are the length of the radiotap
2230 * header; unfortunately, it's little-endian, so we have
2231 * to load it a byte at a time and construct the value.
2235 * Load the high-order byte, at an offset of 3, shift it
2236 * left a byte, and put the result in the X register.
2238 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2240 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2243 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2247 * Load the next byte, at an offset of 2, and OR the
2248 * value from the X register into it.
2250 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2253 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2257 * Now allocate a register to hold that value and store
2260 s2
= new_stmt(BPF_ST
);
2261 s2
->s
.k
= off_linkhdr
.reg
;
2265 * Now move it into the X register.
2267 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2276 * At the moment we treat PPI as normal Radiotap encoded
2277 * packets. The difference is in the function that generates
2278 * the code at the beginning to compute the header length.
2279 * Since this code generator of PPI supports bare 802.11
2280 * encapsulation only (i.e. the encapsulated DLT should be
2281 * DLT_IEEE802_11) we generate code to check for this too;
2282 * that's done in finish_parse().
2284 static struct slist
*
2285 gen_load_ppi_llprefixlen()
2287 struct slist
*s1
, *s2
;
2290 * Generate code to load the length of the radiotap header
2291 * into the register assigned to hold that length, if one has
2294 if (off_linkhdr
.reg
!= -1) {
2296 * The 2 bytes at offsets of 2 and 3 from the beginning
2297 * of the radiotap header are the length of the radiotap
2298 * header; unfortunately, it's little-endian, so we have
2299 * to load it a byte at a time and construct the value.
2303 * Load the high-order byte, at an offset of 3, shift it
2304 * left a byte, and put the result in the X register.
2306 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2308 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2311 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2315 * Load the next byte, at an offset of 2, and OR the
2316 * value from the X register into it.
2318 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2321 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2325 * Now allocate a register to hold that value and store
2328 s2
= new_stmt(BPF_ST
);
2329 s2
->s
.k
= off_linkhdr
.reg
;
2333 * Now move it into the X register.
2335 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2344 * Load a value relative to the beginning of the link-layer header after the 802.11
2345 * header, i.e. LLC_SNAP.
2346 * The link-layer header doesn't necessarily begin at the beginning
2347 * of the packet data; there might be a variable-length prefix containing
2348 * radio information.
2350 static struct slist
*
2351 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2354 struct slist
*sjset_data_frame_1
;
2355 struct slist
*sjset_data_frame_2
;
2356 struct slist
*sjset_qos
;
2357 struct slist
*sjset_radiotap_flags
;
2358 struct slist
*sjset_radiotap_tsft
;
2359 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2360 struct slist
*s_roundup
;
2362 if (off_linkpl
.reg
== -1) {
2364 * No register has been assigned to the offset of
2365 * the link-layer payload, which means nobody needs
2366 * it; don't bother computing it - just return
2367 * what we already have.
2373 * This code is not compatible with the optimizer, as
2374 * we are generating jmp instructions within a normal
2375 * slist of instructions
2380 * If "s" is non-null, it has code to arrange that the X register
2381 * contains the length of the prefix preceding the link-layer
2384 * Otherwise, the length of the prefix preceding the link-layer
2385 * header is "off_outermostlinkhdr.constant_part".
2389 * There is no variable-length header preceding the
2390 * link-layer header.
2392 * Load the length of the fixed-length prefix preceding
2393 * the link-layer header (if any) into the X register,
2394 * and store it in the off_linkpl.reg register.
2395 * That length is off_outermostlinkhdr.constant_part.
2397 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2398 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2402 * The X register contains the offset of the beginning of the
2403 * link-layer header; add 24, which is the minimum length
2404 * of the MAC header for a data frame, to that, and store it
2405 * in off_linkpl.reg, and then load the Frame Control field,
2406 * which is at the offset in the X register, with an indexed load.
2408 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2410 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2413 s2
= new_stmt(BPF_ST
);
2414 s2
->s
.k
= off_linkpl
.reg
;
2417 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2422 * Check the Frame Control field to see if this is a data frame;
2423 * a data frame has the 0x08 bit (b3) in that field set and the
2424 * 0x04 bit (b2) clear.
2426 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2427 sjset_data_frame_1
->s
.k
= 0x08;
2428 sappend(s
, sjset_data_frame_1
);
2431 * If b3 is set, test b2, otherwise go to the first statement of
2432 * the rest of the program.
2434 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2435 sjset_data_frame_2
->s
.k
= 0x04;
2436 sappend(s
, sjset_data_frame_2
);
2437 sjset_data_frame_1
->s
.jf
= snext
;
2440 * If b2 is not set, this is a data frame; test the QoS bit.
2441 * Otherwise, go to the first statement of the rest of the
2444 sjset_data_frame_2
->s
.jt
= snext
;
2445 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2446 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2447 sappend(s
, sjset_qos
);
2450 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2452 * Otherwise, go to the first statement of the rest of the
2455 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2456 s2
->s
.k
= off_linkpl
.reg
;
2458 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2461 s2
= new_stmt(BPF_ST
);
2462 s2
->s
.k
= off_linkpl
.reg
;
2466 * If we have a radiotap header, look at it to see whether
2467 * there's Atheros padding between the MAC-layer header
2470 * Note: all of the fields in the radiotap header are
2471 * little-endian, so we byte-swap all of the values
2472 * we test against, as they will be loaded as big-endian
2475 if (linktype
== DLT_IEEE802_11_RADIO
) {
2477 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2478 * in the presence flag?
2480 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2484 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2485 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2486 sappend(s
, sjset_radiotap_flags
);
2489 * If not, skip all of this.
2491 sjset_radiotap_flags
->s
.jf
= snext
;
2494 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2496 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2497 new_stmt(JMP(BPF_JSET
));
2498 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2499 sappend(s
, sjset_radiotap_tsft
);
2502 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2503 * at an offset of 16 from the beginning of the raw packet
2504 * data (8 bytes for the radiotap header and 8 bytes for
2507 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2510 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2514 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2515 sjset_tsft_datapad
->s
.k
= 0x20;
2516 sappend(s
, sjset_tsft_datapad
);
2519 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2520 * at an offset of 8 from the beginning of the raw packet
2521 * data (8 bytes for the radiotap header).
2523 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2526 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2530 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2531 sjset_notsft_datapad
->s
.k
= 0x20;
2532 sappend(s
, sjset_notsft_datapad
);
2535 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2536 * set, round the length of the 802.11 header to
2537 * a multiple of 4. Do that by adding 3 and then
2538 * dividing by and multiplying by 4, which we do by
2541 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2542 s_roundup
->s
.k
= off_linkpl
.reg
;
2543 sappend(s
, s_roundup
);
2544 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2547 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2550 s2
= new_stmt(BPF_ST
);
2551 s2
->s
.k
= off_linkpl
.reg
;
2554 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2555 sjset_tsft_datapad
->s
.jf
= snext
;
2556 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2557 sjset_notsft_datapad
->s
.jf
= snext
;
2559 sjset_qos
->s
.jf
= snext
;
2565 insert_compute_vloffsets(b
)
2571 * For link-layer types that have a variable-length header
2572 * preceding the link-layer header, generate code to load
2573 * the offset of the link-layer header into the register
2574 * assigned to that offset, if any.
2576 * XXX - this, and the next switch statement, won't handle
2577 * encapsulation of 802.11 or 802.11+radio information in
2578 * some other protocol stack. That's significantly more
2581 switch (outermostlinktype
) {
2583 case DLT_PRISM_HEADER
:
2584 s
= gen_load_prism_llprefixlen();
2587 case DLT_IEEE802_11_RADIO_AVS
:
2588 s
= gen_load_avs_llprefixlen();
2591 case DLT_IEEE802_11_RADIO
:
2592 s
= gen_load_radiotap_llprefixlen();
2596 s
= gen_load_ppi_llprefixlen();
2605 * For link-layer types that have a variable-length link-layer
2606 * header, generate code to load the offset of the link-layer
2607 * payload into the register assigned to that offset, if any.
2609 switch (outermostlinktype
) {
2611 case DLT_IEEE802_11
:
2612 case DLT_PRISM_HEADER
:
2613 case DLT_IEEE802_11_RADIO_AVS
:
2614 case DLT_IEEE802_11_RADIO
:
2616 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2621 * If we have any offset-loading code, append all the
2622 * existing statements in the block to those statements,
2623 * and make the resulting list the list of statements
2627 sappend(s
, b
->stmts
);
2632 static struct block
*
2633 gen_ppi_dlt_check(void)
2635 struct slist
*s_load_dlt
;
2638 if (linktype
== DLT_PPI
)
2640 /* Create the statements that check for the DLT
2642 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2643 s_load_dlt
->s
.k
= 4;
2645 b
= new_block(JMP(BPF_JEQ
));
2647 b
->stmts
= s_load_dlt
;
2648 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2659 * Take an absolute offset, and:
2661 * if it has no variable part, return NULL;
2663 * if it has a variable part, generate code to load the register
2664 * containing that variable part into the X register, returning
2665 * a pointer to that code - if no register for that offset has
2666 * been allocated, allocate it first.
2668 * (The code to set that register will be generated later, but will
2669 * be placed earlier in the code sequence.)
2671 static struct slist
*
2672 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2676 if (off
->is_variable
) {
2677 if (off
->reg
== -1) {
2679 * We haven't yet assigned a register for the
2680 * variable part of the offset of the link-layer
2681 * header; allocate one.
2683 off
->reg
= alloc_reg();
2687 * Load the register containing the variable part of the
2688 * offset of the link-layer header into the X register.
2690 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2695 * That offset isn't variable, there's no variable part,
2696 * so we don't need to generate any code.
2703 * Map an Ethernet type to the equivalent PPP type.
2706 ethertype_to_ppptype(proto
)
2715 case ETHERTYPE_IPV6
:
2723 case ETHERTYPE_ATALK
:
2737 * I'm assuming the "Bridging PDU"s that go
2738 * over PPP are Spanning Tree Protocol
2752 * Generate any tests that, for encapsulation of a link-layer packet
2753 * inside another protocol stack, need to be done to check for those
2754 * link-layer packets (and that haven't already been done by a check
2755 * for that encapsulation).
2757 static struct block
*
2758 gen_prevlinkhdr_check(void)
2762 switch (prevlinktype
) {
2766 * This is LANE-encapsulated Ethernet; check that the LANE
2767 * packet doesn't begin with an LE Control marker, i.e.
2768 * that it's data, not a control message.
2770 * (We've already generated a test for LANE.)
2772 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2778 * No such tests are necessary.
2786 * Generate code to match a particular packet type by matching the
2787 * link-layer type field or fields in the 802.2 LLC header.
2789 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2790 * value, if <= ETHERMTU.
2792 static struct block
*
2796 struct block
*b0
, *b1
, *b2
;
2797 const char *description
;
2799 /* are we checking MPLS-encapsulated packets? */
2800 if (label_stack_depth
> 0) {
2804 /* FIXME add other L3 proto IDs */
2805 return gen_mpls_linktype(Q_IP
);
2807 case ETHERTYPE_IPV6
:
2809 /* FIXME add other L3 proto IDs */
2810 return gen_mpls_linktype(Q_IPV6
);
2813 bpf_error("unsupported protocol over mpls");
2821 case DLT_NETANALYZER
:
2822 case DLT_NETANALYZER_TRANSPARENT
:
2823 b0
= gen_prevlinkhdr_check();
2824 b1
= gen_ether_linktype(proto
);
2835 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2839 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2846 case DLT_IEEE802_11
:
2847 case DLT_PRISM_HEADER
:
2848 case DLT_IEEE802_11_RADIO_AVS
:
2849 case DLT_IEEE802_11_RADIO
:
2852 * Check that we have a data frame.
2854 b0
= gen_check_802_11_data_frame();
2857 * Now check for the specified link-layer type.
2859 b1
= gen_llc_linktype(proto
);
2867 * XXX - check for LLC frames.
2869 return gen_llc_linktype(proto
);
2875 * XXX - check for LLC PDUs, as per IEEE 802.5.
2877 return gen_llc_linktype(proto
);
2881 case DLT_ATM_RFC1483
:
2883 case DLT_IP_OVER_FC
:
2884 return gen_llc_linktype(proto
);
2890 * Check for an LLC-encapsulated version of this protocol;
2891 * if we were checking for LANE, linktype would no longer
2894 * Check for LLC encapsulation and then check the protocol.
2896 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2897 b1
= gen_llc_linktype(proto
);
2904 return gen_linux_sll_linktype(proto
);
2909 case DLT_SLIP_BSDOS
:
2912 * These types don't provide any type field; packets
2913 * are always IPv4 or IPv6.
2915 * XXX - for IPv4, check for a version number of 4, and,
2916 * for IPv6, check for a version number of 6?
2921 /* Check for a version number of 4. */
2922 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
2924 case ETHERTYPE_IPV6
:
2925 /* Check for a version number of 6. */
2926 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
2929 return gen_false(); /* always false */
2936 * Raw IPv4, so no type field.
2938 if (proto
== ETHERTYPE_IP
)
2939 return gen_true(); /* always true */
2941 /* Checking for something other than IPv4; always false */
2948 * Raw IPv6, so no type field.
2950 if (proto
== ETHERTYPE_IPV6
)
2951 return gen_true(); /* always true */
2953 /* Checking for something other than IPv6; always false */
2960 case DLT_PPP_SERIAL
:
2963 * We use Ethernet protocol types inside libpcap;
2964 * map them to the corresponding PPP protocol types.
2966 proto
= ethertype_to_ppptype(proto
);
2967 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2973 * We use Ethernet protocol types inside libpcap;
2974 * map them to the corresponding PPP protocol types.
2980 * Also check for Van Jacobson-compressed IP.
2981 * XXX - do this for other forms of PPP?
2983 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_IP
);
2984 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJC
);
2986 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJNC
);
2991 proto
= ethertype_to_ppptype(proto
);
2992 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
3002 * For DLT_NULL, the link-layer header is a 32-bit
3003 * word containing an AF_ value in *host* byte order,
3004 * and for DLT_ENC, the link-layer header begins
3005 * with a 32-bit work containing an AF_ value in
3008 * In addition, if we're reading a saved capture file,
3009 * the host byte order in the capture may not be the
3010 * same as the host byte order on this machine.
3012 * For DLT_LOOP, the link-layer header is a 32-bit
3013 * word containing an AF_ value in *network* byte order.
3015 * XXX - AF_ values may, unfortunately, be platform-
3016 * dependent; for example, FreeBSD's AF_INET6 is 24
3017 * whilst NetBSD's and OpenBSD's is 26.
3019 * This means that, when reading a capture file, just
3020 * checking for our AF_INET6 value won't work if the
3021 * capture file came from another OS.
3030 case ETHERTYPE_IPV6
:
3037 * Not a type on which we support filtering.
3038 * XXX - support those that have AF_ values
3039 * #defined on this platform, at least?
3044 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3046 * The AF_ value is in host byte order, but
3047 * the BPF interpreter will convert it to
3048 * network byte order.
3050 * If this is a save file, and it's from a
3051 * machine with the opposite byte order to
3052 * ours, we byte-swap the AF_ value.
3054 * Then we run it through "htonl()", and
3055 * generate code to compare against the result.
3057 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3058 proto
= SWAPLONG(proto
);
3059 proto
= htonl(proto
);
3061 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3063 #ifdef HAVE_NET_PFVAR_H
3066 * af field is host byte order in contrast to the rest of
3069 if (proto
== ETHERTYPE_IP
)
3070 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3071 BPF_B
, (bpf_int32
)AF_INET
));
3072 else if (proto
== ETHERTYPE_IPV6
)
3073 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3074 BPF_B
, (bpf_int32
)AF_INET6
));
3079 #endif /* HAVE_NET_PFVAR_H */
3082 case DLT_ARCNET_LINUX
:
3084 * XXX should we check for first fragment if the protocol
3092 case ETHERTYPE_IPV6
:
3093 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3094 (bpf_int32
)ARCTYPE_INET6
));
3097 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3098 (bpf_int32
)ARCTYPE_IP
);
3099 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3100 (bpf_int32
)ARCTYPE_IP_OLD
);
3105 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3106 (bpf_int32
)ARCTYPE_ARP
);
3107 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3108 (bpf_int32
)ARCTYPE_ARP_OLD
);
3112 case ETHERTYPE_REVARP
:
3113 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3114 (bpf_int32
)ARCTYPE_REVARP
));
3116 case ETHERTYPE_ATALK
:
3117 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3118 (bpf_int32
)ARCTYPE_ATALK
));
3125 case ETHERTYPE_ATALK
:
3135 * XXX - assumes a 2-byte Frame Relay header with
3136 * DLCI and flags. What if the address is longer?
3142 * Check for the special NLPID for IP.
3144 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3146 case ETHERTYPE_IPV6
:
3148 * Check for the special NLPID for IPv6.
3150 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3154 * Check for several OSI protocols.
3156 * Frame Relay packets typically have an OSI
3157 * NLPID at the beginning; we check for each
3160 * What we check for is the NLPID and a frame
3161 * control field of UI, i.e. 0x03 followed
3164 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3165 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3166 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3178 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3180 case DLT_JUNIPER_MFR
:
3181 case DLT_JUNIPER_MLFR
:
3182 case DLT_JUNIPER_MLPPP
:
3183 case DLT_JUNIPER_ATM1
:
3184 case DLT_JUNIPER_ATM2
:
3185 case DLT_JUNIPER_PPPOE
:
3186 case DLT_JUNIPER_PPPOE_ATM
:
3187 case DLT_JUNIPER_GGSN
:
3188 case DLT_JUNIPER_ES
:
3189 case DLT_JUNIPER_MONITOR
:
3190 case DLT_JUNIPER_SERVICES
:
3191 case DLT_JUNIPER_ETHER
:
3192 case DLT_JUNIPER_PPP
:
3193 case DLT_JUNIPER_FRELAY
:
3194 case DLT_JUNIPER_CHDLC
:
3195 case DLT_JUNIPER_VP
:
3196 case DLT_JUNIPER_ST
:
3197 case DLT_JUNIPER_ISM
:
3198 case DLT_JUNIPER_VS
:
3199 case DLT_JUNIPER_SRX_E2E
:
3200 case DLT_JUNIPER_FIBRECHANNEL
:
3201 case DLT_JUNIPER_ATM_CEMIC
:
3203 /* just lets verify the magic number for now -
3204 * on ATM we may have up to 6 different encapsulations on the wire
3205 * and need a lot of heuristics to figure out that the payload
3208 * FIXME encapsulation specific BPF_ filters
3210 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3212 case DLT_BACNET_MS_TP
:
3213 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3216 return gen_ipnet_linktype(proto
);
3218 case DLT_LINUX_IRDA
:
3219 bpf_error("IrDA link-layer type filtering not implemented");
3222 bpf_error("DOCSIS link-layer type filtering not implemented");
3225 case DLT_MTP2_WITH_PHDR
:
3226 bpf_error("MTP2 link-layer type filtering not implemented");
3229 bpf_error("ERF link-layer type filtering not implemented");
3232 bpf_error("PFSYNC link-layer type filtering not implemented");
3234 case DLT_LINUX_LAPD
:
3235 bpf_error("LAPD link-layer type filtering not implemented");
3239 case DLT_USB_LINUX_MMAPPED
:
3240 bpf_error("USB link-layer type filtering not implemented");
3242 case DLT_BLUETOOTH_HCI_H4
:
3243 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3244 bpf_error("Bluetooth link-layer type filtering not implemented");
3247 case DLT_CAN_SOCKETCAN
:
3248 bpf_error("CAN link-layer type filtering not implemented");
3250 case DLT_IEEE802_15_4
:
3251 case DLT_IEEE802_15_4_LINUX
:
3252 case DLT_IEEE802_15_4_NONASK_PHY
:
3253 case DLT_IEEE802_15_4_NOFCS
:
3254 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3256 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3257 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3260 bpf_error("SITA link-layer type filtering not implemented");
3263 bpf_error("RAIF1 link-layer type filtering not implemented");
3266 bpf_error("IPMB link-layer type filtering not implemented");
3269 bpf_error("AX.25 link-layer type filtering not implemented");
3272 /* Using the fixed-size NFLOG header it is possible to tell only
3273 * the address family of the packet, other meaningful data is
3274 * either missing or behind TLVs.
3276 bpf_error("NFLOG link-layer type filtering not implemented");
3280 * Does this link-layer header type have a field
3281 * indicating the type of the next protocol? If
3282 * so, off_linktype will be the offset of that
3283 * field in the packet; if not, it will be -1.
3285 if (off_linktype
!= (u_int
)-1) {
3287 * Yes; assume it's an Ethernet type. (If
3288 * it's not, it needs to be handled specially
3291 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3294 * No; report an error.
3296 description
= pcap_datalink_val_to_description(linktype
);
3297 if (description
!= NULL
) {
3298 bpf_error("%s link-layer type filtering not implemented",
3301 bpf_error("DLT %u link-layer type filtering not implemented",
3310 * Check for an LLC SNAP packet with a given organization code and
3311 * protocol type; we check the entire contents of the 802.2 LLC and
3312 * snap headers, checking for DSAP and SSAP of SNAP and a control
3313 * field of 0x03 in the LLC header, and for the specified organization
3314 * code and protocol type in the SNAP header.
3316 static struct block
*
3317 gen_snap(orgcode
, ptype
)
3318 bpf_u_int32 orgcode
;
3321 u_char snapblock
[8];
3323 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3324 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3325 snapblock
[2] = 0x03; /* control = UI */
3326 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3327 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3328 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3329 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3330 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3331 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3335 * Generate code to match frames with an LLC header.
3340 struct block
*b0
, *b1
;
3346 * We check for an Ethernet type field less than
3347 * 1500, which means it's an 802.3 length field.
3349 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
3353 * Now check for the purported DSAP and SSAP not being
3354 * 0xFF, to rule out NetWare-over-802.3.
3356 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3363 * We check for LLC traffic.
3365 b0
= gen_atmtype_abbrev(A_LLC
);
3368 case DLT_IEEE802
: /* Token Ring */
3370 * XXX - check for LLC frames.
3376 * XXX - check for LLC frames.
3380 case DLT_ATM_RFC1483
:
3382 * For LLC encapsulation, these are defined to have an
3385 * For VC encapsulation, they don't, but there's no
3386 * way to check for that; the protocol used on the VC
3387 * is negotiated out of band.
3391 case DLT_IEEE802_11
:
3392 case DLT_PRISM_HEADER
:
3393 case DLT_IEEE802_11_RADIO
:
3394 case DLT_IEEE802_11_RADIO_AVS
:
3397 * Check that we have a data frame.
3399 b0
= gen_check_802_11_data_frame();
3403 bpf_error("'llc' not supported for linktype %d", linktype
);
3411 struct block
*b0
, *b1
;
3415 * Check whether this is an LLC frame.
3420 * Load the control byte and test the low-order bit; it must
3421 * be clear for I frames.
3423 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3424 b1
= new_block(JMP(BPF_JSET
));
3435 struct block
*b0
, *b1
;
3438 * Check whether this is an LLC frame.
3443 * Now compare the low-order 2 bit of the control byte against
3444 * the appropriate value for S frames.
3446 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3454 struct block
*b0
, *b1
;
3457 * Check whether this is an LLC frame.
3462 * Now compare the low-order 2 bit of the control byte against
3463 * the appropriate value for U frames.
3465 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3471 gen_llc_s_subtype(bpf_u_int32 subtype
)
3473 struct block
*b0
, *b1
;
3476 * Check whether this is an LLC frame.
3481 * Now check for an S frame with the appropriate type.
3483 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3489 gen_llc_u_subtype(bpf_u_int32 subtype
)
3491 struct block
*b0
, *b1
;
3494 * Check whether this is an LLC frame.
3499 * Now check for a U frame with the appropriate type.
3501 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3507 * Generate code to match a particular packet type, for link-layer types
3508 * using 802.2 LLC headers.
3510 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3511 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3513 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3514 * value, if <= ETHERMTU. We use that to determine whether to
3515 * match the DSAP or both DSAP and LSAP or to check the OUI and
3516 * protocol ID in a SNAP header.
3518 static struct block
*
3519 gen_llc_linktype(proto
)
3523 * XXX - handle token-ring variable-length header.
3529 case LLCSAP_NETBEUI
:
3531 * XXX - should we check both the DSAP and the
3532 * SSAP, like this, or should we check just the
3533 * DSAP, as we do for other SAP values?
3535 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3536 ((proto
<< 8) | proto
));
3540 * XXX - are there ever SNAP frames for IPX on
3541 * non-Ethernet 802.x networks?
3543 return gen_cmp(OR_LLC
, 0, BPF_B
,
3544 (bpf_int32
)LLCSAP_IPX
);
3546 case ETHERTYPE_ATALK
:
3548 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3549 * SNAP packets with an organization code of
3550 * 0x080007 (Apple, for Appletalk) and a protocol
3551 * type of ETHERTYPE_ATALK (Appletalk).
3553 * XXX - check for an organization code of
3554 * encapsulated Ethernet as well?
3556 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3560 * XXX - we don't have to check for IPX 802.3
3561 * here, but should we check for the IPX Ethertype?
3563 if (proto
<= ETHERMTU
) {
3565 * This is an LLC SAP value, so check
3568 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3571 * This is an Ethernet type; we assume that it's
3572 * unlikely that it'll appear in the right place
3573 * at random, and therefore check only the
3574 * location that would hold the Ethernet type
3575 * in a SNAP frame with an organization code of
3576 * 0x000000 (encapsulated Ethernet).
3578 * XXX - if we were to check for the SNAP DSAP and
3579 * LSAP, as per XXX, and were also to check for an
3580 * organization code of 0x000000 (encapsulated
3581 * Ethernet), we'd do
3583 * return gen_snap(0x000000, proto);
3585 * here; for now, we don't, as per the above.
3586 * I don't know whether it's worth the extra CPU
3587 * time to do the right check or not.
3589 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3594 static struct block
*
3595 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3599 u_int src_off
, dst_off
;
3601 struct block
*b0
, *b1
;
3615 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3616 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3622 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3623 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3630 b0
= gen_linktype(proto
);
3631 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3637 static struct block
*
3638 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3639 struct in6_addr
*addr
;
3640 struct in6_addr
*mask
;
3642 u_int src_off
, dst_off
;
3644 struct block
*b0
, *b1
;
3659 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3660 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3666 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3667 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3674 /* this order is important */
3675 a
= (u_int32_t
*)addr
;
3676 m
= (u_int32_t
*)mask
;
3677 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3678 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3680 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3682 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3684 b0
= gen_linktype(proto
);
3690 static struct block
*
3691 gen_ehostop(eaddr
, dir
)
3692 register const u_char
*eaddr
;
3695 register struct block
*b0
, *b1
;
3699 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3702 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3705 b0
= gen_ehostop(eaddr
, Q_SRC
);
3706 b1
= gen_ehostop(eaddr
, Q_DST
);
3712 b0
= gen_ehostop(eaddr
, Q_SRC
);
3713 b1
= gen_ehostop(eaddr
, Q_DST
);
3718 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3722 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3726 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3730 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3734 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3738 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3746 * Like gen_ehostop, but for DLT_FDDI
3748 static struct block
*
3749 gen_fhostop(eaddr
, dir
)
3750 register const u_char
*eaddr
;
3753 struct block
*b0
, *b1
;
3757 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3760 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3763 b0
= gen_fhostop(eaddr
, Q_SRC
);
3764 b1
= gen_fhostop(eaddr
, Q_DST
);
3770 b0
= gen_fhostop(eaddr
, Q_SRC
);
3771 b1
= gen_fhostop(eaddr
, Q_DST
);
3776 bpf_error("'addr1' is only supported on 802.11");
3780 bpf_error("'addr2' is only supported on 802.11");
3784 bpf_error("'addr3' is only supported on 802.11");
3788 bpf_error("'addr4' is only supported on 802.11");
3792 bpf_error("'ra' is only supported on 802.11");
3796 bpf_error("'ta' is only supported on 802.11");
3804 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3806 static struct block
*
3807 gen_thostop(eaddr
, dir
)
3808 register const u_char
*eaddr
;
3811 register struct block
*b0
, *b1
;
3815 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3818 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3821 b0
= gen_thostop(eaddr
, Q_SRC
);
3822 b1
= gen_thostop(eaddr
, Q_DST
);
3828 b0
= gen_thostop(eaddr
, Q_SRC
);
3829 b1
= gen_thostop(eaddr
, Q_DST
);
3834 bpf_error("'addr1' is only supported on 802.11");
3838 bpf_error("'addr2' is only supported on 802.11");
3842 bpf_error("'addr3' is only supported on 802.11");
3846 bpf_error("'addr4' is only supported on 802.11");
3850 bpf_error("'ra' is only supported on 802.11");
3854 bpf_error("'ta' is only supported on 802.11");
3862 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3863 * various 802.11 + radio headers.
3865 static struct block
*
3866 gen_wlanhostop(eaddr
, dir
)
3867 register const u_char
*eaddr
;
3870 register struct block
*b0
, *b1
, *b2
;
3871 register struct slist
*s
;
3873 #ifdef ENABLE_WLAN_FILTERING_PATCH
3876 * We need to disable the optimizer because the optimizer is buggy
3877 * and wipes out some LD instructions generated by the below
3878 * code to validate the Frame Control bits
3881 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3888 * For control frames, there is no SA.
3890 * For management frames, SA is at an
3891 * offset of 10 from the beginning of
3894 * For data frames, SA is at an offset
3895 * of 10 from the beginning of the packet
3896 * if From DS is clear, at an offset of
3897 * 16 from the beginning of the packet
3898 * if From DS is set and To DS is clear,
3899 * and an offset of 24 from the beginning
3900 * of the packet if From DS is set and To DS
3905 * Generate the tests to be done for data frames
3908 * First, check for To DS set, i.e. check "link[1] & 0x01".
3910 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3911 b1
= new_block(JMP(BPF_JSET
));
3912 b1
->s
.k
= 0x01; /* To DS */
3916 * If To DS is set, the SA is at 24.
3918 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
3922 * Now, check for To DS not set, i.e. check
3923 * "!(link[1] & 0x01)".
3925 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3926 b2
= new_block(JMP(BPF_JSET
));
3927 b2
->s
.k
= 0x01; /* To DS */
3932 * If To DS is not set, the SA is at 16.
3934 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
3938 * Now OR together the last two checks. That gives
3939 * the complete set of checks for data frames with
3945 * Now check for From DS being set, and AND that with
3946 * the ORed-together checks.
3948 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3949 b1
= new_block(JMP(BPF_JSET
));
3950 b1
->s
.k
= 0x02; /* From DS */
3955 * Now check for data frames with From DS not set.
3957 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3958 b2
= new_block(JMP(BPF_JSET
));
3959 b2
->s
.k
= 0x02; /* From DS */
3964 * If From DS isn't set, the SA is at 10.
3966 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
3970 * Now OR together the checks for data frames with
3971 * From DS not set and for data frames with From DS
3972 * set; that gives the checks done for data frames.
3977 * Now check for a data frame.
3978 * I.e, check "link[0] & 0x08".
3980 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3981 b1
= new_block(JMP(BPF_JSET
));
3986 * AND that with the checks done for data frames.
3991 * If the high-order bit of the type value is 0, this
3992 * is a management frame.
3993 * I.e, check "!(link[0] & 0x08)".
3995 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3996 b2
= new_block(JMP(BPF_JSET
));
4002 * For management frames, the SA is at 10.
4004 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4008 * OR that with the checks done for data frames.
4009 * That gives the checks done for management and
4015 * If the low-order bit of the type value is 1,
4016 * this is either a control frame or a frame
4017 * with a reserved type, and thus not a
4020 * I.e., check "!(link[0] & 0x04)".
4022 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4023 b1
= new_block(JMP(BPF_JSET
));
4029 * AND that with the checks for data and management
4039 * For control frames, there is no DA.
4041 * For management frames, DA is at an
4042 * offset of 4 from the beginning of
4045 * For data frames, DA is at an offset
4046 * of 4 from the beginning of the packet
4047 * if To DS is clear and at an offset of
4048 * 16 from the beginning of the packet
4053 * Generate the tests to be done for data frames.
4055 * First, check for To DS set, i.e. "link[1] & 0x01".
4057 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4058 b1
= new_block(JMP(BPF_JSET
));
4059 b1
->s
.k
= 0x01; /* To DS */
4063 * If To DS is set, the DA is at 16.
4065 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4069 * Now, check for To DS not set, i.e. check
4070 * "!(link[1] & 0x01)".
4072 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4073 b2
= new_block(JMP(BPF_JSET
));
4074 b2
->s
.k
= 0x01; /* To DS */
4079 * If To DS is not set, the DA is at 4.
4081 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4085 * Now OR together the last two checks. That gives
4086 * the complete set of checks for data frames.
4091 * Now check for a data frame.
4092 * I.e, check "link[0] & 0x08".
4094 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4095 b1
= new_block(JMP(BPF_JSET
));
4100 * AND that with the checks done for data frames.
4105 * If the high-order bit of the type value is 0, this
4106 * is a management frame.
4107 * I.e, check "!(link[0] & 0x08)".
4109 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4110 b2
= new_block(JMP(BPF_JSET
));
4116 * For management frames, the DA is at 4.
4118 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4122 * OR that with the checks done for data frames.
4123 * That gives the checks done for management and
4129 * If the low-order bit of the type value is 1,
4130 * this is either a control frame or a frame
4131 * with a reserved type, and thus not a
4134 * I.e., check "!(link[0] & 0x04)".
4136 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4137 b1
= new_block(JMP(BPF_JSET
));
4143 * AND that with the checks for data and management
4151 * Not present in management frames; addr1 in other
4156 * If the high-order bit of the type value is 0, this
4157 * is a management frame.
4158 * I.e, check "(link[0] & 0x08)".
4160 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4161 b1
= new_block(JMP(BPF_JSET
));
4168 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4171 * AND that with the check of addr1.
4178 * Not present in management frames; addr2, if present,
4183 * Not present in CTS or ACK control frames.
4185 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4186 IEEE80211_FC0_TYPE_MASK
);
4188 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4189 IEEE80211_FC0_SUBTYPE_MASK
);
4191 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4192 IEEE80211_FC0_SUBTYPE_MASK
);
4198 * If the high-order bit of the type value is 0, this
4199 * is a management frame.
4200 * I.e, check "(link[0] & 0x08)".
4202 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4203 b1
= new_block(JMP(BPF_JSET
));
4208 * AND that with the check for frames other than
4209 * CTS and ACK frames.
4216 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4221 * XXX - add BSSID keyword?
4224 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4228 * Not present in CTS or ACK control frames.
4230 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4231 IEEE80211_FC0_TYPE_MASK
);
4233 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4234 IEEE80211_FC0_SUBTYPE_MASK
);
4236 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4237 IEEE80211_FC0_SUBTYPE_MASK
);
4241 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4247 * Not present in control frames.
4249 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4250 IEEE80211_FC0_TYPE_MASK
);
4252 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4258 * Present only if the direction mask has both "From DS"
4259 * and "To DS" set. Neither control frames nor management
4260 * frames should have both of those set, so we don't
4261 * check the frame type.
4263 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4264 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4265 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4270 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4271 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4277 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4278 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4287 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4288 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4289 * as the RFC states.)
4291 static struct block
*
4292 gen_ipfchostop(eaddr
, dir
)
4293 register const u_char
*eaddr
;
4296 register struct block
*b0
, *b1
;
4300 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4303 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4306 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4307 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4313 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4314 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4319 bpf_error("'addr1' is only supported on 802.11");
4323 bpf_error("'addr2' is only supported on 802.11");
4327 bpf_error("'addr3' is only supported on 802.11");
4331 bpf_error("'addr4' is only supported on 802.11");
4335 bpf_error("'ra' is only supported on 802.11");
4339 bpf_error("'ta' is only supported on 802.11");
4347 * This is quite tricky because there may be pad bytes in front of the
4348 * DECNET header, and then there are two possible data packet formats that
4349 * carry both src and dst addresses, plus 5 packet types in a format that
4350 * carries only the src node, plus 2 types that use a different format and
4351 * also carry just the src node.
4355 * Instead of doing those all right, we just look for data packets with
4356 * 0 or 1 bytes of padding. If you want to look at other packets, that
4357 * will require a lot more hacking.
4359 * To add support for filtering on DECNET "areas" (network numbers)
4360 * one would want to add a "mask" argument to this routine. That would
4361 * make the filter even more inefficient, although one could be clever
4362 * and not generate masking instructions if the mask is 0xFFFF.
4364 static struct block
*
4365 gen_dnhostop(addr
, dir
)
4369 struct block
*b0
, *b1
, *b2
, *tmp
;
4370 u_int offset_lh
; /* offset if long header is received */
4371 u_int offset_sh
; /* offset if short header is received */
4376 offset_sh
= 1; /* follows flags */
4377 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4381 offset_sh
= 3; /* follows flags, dstnode */
4382 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4386 /* Inefficient because we do our Calvinball dance twice */
4387 b0
= gen_dnhostop(addr
, Q_SRC
);
4388 b1
= gen_dnhostop(addr
, Q_DST
);
4394 /* Inefficient because we do our Calvinball dance twice */
4395 b0
= gen_dnhostop(addr
, Q_SRC
);
4396 b1
= gen_dnhostop(addr
, Q_DST
);
4401 bpf_error("ISO host filtering not implemented");
4406 b0
= gen_linktype(ETHERTYPE_DN
);
4407 /* Check for pad = 1, long header case */
4408 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4409 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4410 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4411 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4413 /* Check for pad = 0, long header case */
4414 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4415 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4418 /* Check for pad = 1, short header case */
4419 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4420 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4421 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4424 /* Check for pad = 0, short header case */
4425 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4426 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4430 /* Combine with test for linktype */
4436 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4437 * test the bottom-of-stack bit, and then check the version number
4438 * field in the IP header.
4440 static struct block
*
4441 gen_mpls_linktype(proto
)
4444 struct block
*b0
, *b1
;
4449 /* match the bottom-of-stack bit */
4450 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4451 /* match the IPv4 version number */
4452 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4457 /* match the bottom-of-stack bit */
4458 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4459 /* match the IPv4 version number */
4460 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4469 static struct block
*
4470 gen_host(addr
, mask
, proto
, dir
, type
)
4477 struct block
*b0
, *b1
;
4478 const char *typestr
;
4488 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4490 * Only check for non-IPv4 addresses if we're not
4491 * checking MPLS-encapsulated packets.
4493 if (label_stack_depth
== 0) {
4494 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4496 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4502 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4505 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4508 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4511 bpf_error("'tcp' modifier applied to %s", typestr
);
4514 bpf_error("'sctp' modifier applied to %s", typestr
);
4517 bpf_error("'udp' modifier applied to %s", typestr
);
4520 bpf_error("'icmp' modifier applied to %s", typestr
);
4523 bpf_error("'igmp' modifier applied to %s", typestr
);
4526 bpf_error("'igrp' modifier applied to %s", typestr
);
4529 bpf_error("'pim' modifier applied to %s", typestr
);
4532 bpf_error("'vrrp' modifier applied to %s", typestr
);
4535 bpf_error("'carp' modifier applied to %s", typestr
);
4538 bpf_error("ATALK host filtering not implemented");
4541 bpf_error("AARP host filtering not implemented");
4544 return gen_dnhostop(addr
, dir
);
4547 bpf_error("SCA host filtering not implemented");
4550 bpf_error("LAT host filtering not implemented");
4553 bpf_error("MOPDL host filtering not implemented");
4556 bpf_error("MOPRC host filtering not implemented");
4559 bpf_error("'ip6' modifier applied to ip host");
4562 bpf_error("'icmp6' modifier applied to %s", typestr
);
4565 bpf_error("'ah' modifier applied to %s", typestr
);
4568 bpf_error("'esp' modifier applied to %s", typestr
);
4571 bpf_error("ISO host filtering not implemented");
4574 bpf_error("'esis' modifier applied to %s", typestr
);
4577 bpf_error("'isis' modifier applied to %s", typestr
);
4580 bpf_error("'clnp' modifier applied to %s", typestr
);
4583 bpf_error("'stp' modifier applied to %s", typestr
);
4586 bpf_error("IPX host filtering not implemented");
4589 bpf_error("'netbeui' modifier applied to %s", typestr
);
4592 bpf_error("'radio' modifier applied to %s", typestr
);
4601 static struct block
*
4602 gen_host6(addr
, mask
, proto
, dir
, type
)
4603 struct in6_addr
*addr
;
4604 struct in6_addr
*mask
;
4609 const char *typestr
;
4619 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4622 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4625 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4628 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4631 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4634 bpf_error("'sctp' modifier applied to %s", typestr
);
4637 bpf_error("'tcp' modifier applied to %s", typestr
);
4640 bpf_error("'udp' modifier applied to %s", typestr
);
4643 bpf_error("'icmp' modifier applied to %s", typestr
);
4646 bpf_error("'igmp' modifier applied to %s", typestr
);
4649 bpf_error("'igrp' modifier applied to %s", typestr
);
4652 bpf_error("'pim' modifier applied to %s", typestr
);
4655 bpf_error("'vrrp' modifier applied to %s", typestr
);
4658 bpf_error("'carp' modifier applied to %s", typestr
);
4661 bpf_error("ATALK host filtering not implemented");
4664 bpf_error("AARP host filtering not implemented");
4667 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4670 bpf_error("SCA host filtering not implemented");
4673 bpf_error("LAT host filtering not implemented");
4676 bpf_error("MOPDL host filtering not implemented");
4679 bpf_error("MOPRC host filtering not implemented");
4682 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4685 bpf_error("'icmp6' modifier applied to %s", typestr
);
4688 bpf_error("'ah' modifier applied to %s", typestr
);
4691 bpf_error("'esp' modifier applied to %s", typestr
);
4694 bpf_error("ISO host filtering not implemented");
4697 bpf_error("'esis' modifier applied to %s", typestr
);
4700 bpf_error("'isis' modifier applied to %s", typestr
);
4703 bpf_error("'clnp' modifier applied to %s", typestr
);
4706 bpf_error("'stp' modifier applied to %s", typestr
);
4709 bpf_error("IPX host filtering not implemented");
4712 bpf_error("'netbeui' modifier applied to %s", typestr
);
4715 bpf_error("'radio' modifier applied to %s", typestr
);
4725 static struct block
*
4726 gen_gateway(eaddr
, alist
, proto
, dir
)
4727 const u_char
*eaddr
;
4728 bpf_u_int32
**alist
;
4732 struct block
*b0
, *b1
, *tmp
;
4735 bpf_error("direction applied to 'gateway'");
4744 case DLT_NETANALYZER
:
4745 case DLT_NETANALYZER_TRANSPARENT
:
4746 b1
= gen_prevlinkhdr_check();
4747 b0
= gen_ehostop(eaddr
, Q_OR
);
4752 b0
= gen_fhostop(eaddr
, Q_OR
);
4755 b0
= gen_thostop(eaddr
, Q_OR
);
4757 case DLT_IEEE802_11
:
4758 case DLT_PRISM_HEADER
:
4759 case DLT_IEEE802_11_RADIO_AVS
:
4760 case DLT_IEEE802_11_RADIO
:
4762 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4766 * This is LLC-multiplexed traffic; if it were
4767 * LANE, linktype would have been set to
4771 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4773 case DLT_IP_OVER_FC
:
4774 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4778 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4780 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4782 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4791 bpf_error("illegal modifier of 'gateway'");
4797 gen_proto_abbrev(proto
)
4806 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4807 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4812 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4813 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4818 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4819 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4824 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4827 #ifndef IPPROTO_IGMP
4828 #define IPPROTO_IGMP 2
4832 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4835 #ifndef IPPROTO_IGRP
4836 #define IPPROTO_IGRP 9
4839 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4843 #define IPPROTO_PIM 103
4847 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4848 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4852 #ifndef IPPROTO_VRRP
4853 #define IPPROTO_VRRP 112
4857 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4860 #ifndef IPPROTO_CARP
4861 #define IPPROTO_CARP 112
4865 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4869 b1
= gen_linktype(ETHERTYPE_IP
);
4873 b1
= gen_linktype(ETHERTYPE_ARP
);
4877 b1
= gen_linktype(ETHERTYPE_REVARP
);
4881 bpf_error("link layer applied in wrong context");
4884 b1
= gen_linktype(ETHERTYPE_ATALK
);
4888 b1
= gen_linktype(ETHERTYPE_AARP
);
4892 b1
= gen_linktype(ETHERTYPE_DN
);
4896 b1
= gen_linktype(ETHERTYPE_SCA
);
4900 b1
= gen_linktype(ETHERTYPE_LAT
);
4904 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4908 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4912 b1
= gen_linktype(ETHERTYPE_IPV6
);
4915 #ifndef IPPROTO_ICMPV6
4916 #define IPPROTO_ICMPV6 58
4919 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4923 #define IPPROTO_AH 51
4926 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4927 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4932 #define IPPROTO_ESP 50
4935 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4936 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4941 b1
= gen_linktype(LLCSAP_ISONS
);
4945 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4949 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4952 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4953 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4954 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4956 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4958 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4960 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4964 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4965 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4966 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4968 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4970 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4972 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4976 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4977 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4978 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4980 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4985 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4986 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4991 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4992 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4994 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4996 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5001 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5002 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5007 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5008 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5013 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5017 b1
= gen_linktype(LLCSAP_8021D
);
5021 b1
= gen_linktype(LLCSAP_IPX
);
5025 b1
= gen_linktype(LLCSAP_NETBEUI
);
5029 bpf_error("'radio' is not a valid protocol type");
5037 static struct block
*
5043 /* not IPv4 frag other than the first frag */
5044 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5045 b
= new_block(JMP(BPF_JSET
));
5054 * Generate a comparison to a port value in the transport-layer header
5055 * at the specified offset from the beginning of that header.
5057 * XXX - this handles a variable-length prefix preceding the link-layer
5058 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5059 * variable-length link-layer headers (such as Token Ring or 802.11
5062 static struct block
*
5063 gen_portatom(off
, v
)
5067 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5070 static struct block
*
5071 gen_portatom6(off
, v
)
5075 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5079 gen_portop(port
, proto
, dir
)
5080 int port
, proto
, dir
;
5082 struct block
*b0
, *b1
, *tmp
;
5084 /* ip proto 'proto' and not a fragment other than the first fragment */
5085 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5091 b1
= gen_portatom(0, (bpf_int32
)port
);
5095 b1
= gen_portatom(2, (bpf_int32
)port
);
5100 tmp
= gen_portatom(0, (bpf_int32
)port
);
5101 b1
= gen_portatom(2, (bpf_int32
)port
);
5106 tmp
= gen_portatom(0, (bpf_int32
)port
);
5107 b1
= gen_portatom(2, (bpf_int32
)port
);
5119 static struct block
*
5120 gen_port(port
, ip_proto
, dir
)
5125 struct block
*b0
, *b1
, *tmp
;
5130 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5131 * not LLC encapsulation with LLCSAP_IP.
5133 * For IEEE 802 networks - which includes 802.5 token ring
5134 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5135 * says that SNAP encapsulation is used, not LLC encapsulation
5138 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5139 * RFC 2225 say that SNAP encapsulation is used, not LLC
5140 * encapsulation with LLCSAP_IP.
5142 * So we always check for ETHERTYPE_IP.
5144 b0
= gen_linktype(ETHERTYPE_IP
);
5150 b1
= gen_portop(port
, ip_proto
, dir
);
5154 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5155 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5157 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5169 gen_portop6(port
, proto
, dir
)
5170 int port
, proto
, dir
;
5172 struct block
*b0
, *b1
, *tmp
;
5174 /* ip6 proto 'proto' */
5175 /* XXX - catch the first fragment of a fragmented packet? */
5176 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5180 b1
= gen_portatom6(0, (bpf_int32
)port
);
5184 b1
= gen_portatom6(2, (bpf_int32
)port
);
5189 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5190 b1
= gen_portatom6(2, (bpf_int32
)port
);
5195 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5196 b1
= gen_portatom6(2, (bpf_int32
)port
);
5208 static struct block
*
5209 gen_port6(port
, ip_proto
, dir
)
5214 struct block
*b0
, *b1
, *tmp
;
5216 /* link proto ip6 */
5217 b0
= gen_linktype(ETHERTYPE_IPV6
);
5223 b1
= gen_portop6(port
, ip_proto
, dir
);
5227 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5228 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5230 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5241 /* gen_portrange code */
5242 static struct block
*
5243 gen_portrangeatom(off
, v1
, v2
)
5247 struct block
*b1
, *b2
;
5251 * Reverse the order of the ports, so v1 is the lower one.
5260 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5261 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5269 gen_portrangeop(port1
, port2
, proto
, dir
)
5274 struct block
*b0
, *b1
, *tmp
;
5276 /* ip proto 'proto' and not a fragment other than the first fragment */
5277 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5283 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5287 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5292 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5293 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5298 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5299 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5311 static struct block
*
5312 gen_portrange(port1
, port2
, ip_proto
, dir
)
5317 struct block
*b0
, *b1
, *tmp
;
5320 b0
= gen_linktype(ETHERTYPE_IP
);
5326 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5330 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5331 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5333 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5344 static struct block
*
5345 gen_portrangeatom6(off
, v1
, v2
)
5349 struct block
*b1
, *b2
;
5353 * Reverse the order of the ports, so v1 is the lower one.
5362 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5363 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5371 gen_portrangeop6(port1
, port2
, proto
, dir
)
5376 struct block
*b0
, *b1
, *tmp
;
5378 /* ip6 proto 'proto' */
5379 /* XXX - catch the first fragment of a fragmented packet? */
5380 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5384 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5388 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5393 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5394 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5399 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5400 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5412 static struct block
*
5413 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5418 struct block
*b0
, *b1
, *tmp
;
5420 /* link proto ip6 */
5421 b0
= gen_linktype(ETHERTYPE_IPV6
);
5427 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5431 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5432 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5434 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5446 lookup_proto(name
, proto
)
5447 register const char *name
;
5457 v
= pcap_nametoproto(name
);
5458 if (v
== PROTO_UNDEF
)
5459 bpf_error("unknown ip proto '%s'", name
);
5463 /* XXX should look up h/w protocol type based on linktype */
5464 v
= pcap_nametoeproto(name
);
5465 if (v
== PROTO_UNDEF
) {
5466 v
= pcap_nametollc(name
);
5467 if (v
== PROTO_UNDEF
)
5468 bpf_error("unknown ether proto '%s'", name
);
5473 if (strcmp(name
, "esis") == 0)
5475 else if (strcmp(name
, "isis") == 0)
5477 else if (strcmp(name
, "clnp") == 0)
5480 bpf_error("unknown osi proto '%s'", name
);
5500 static struct block
*
5501 gen_protochain(v
, proto
, dir
)
5506 #ifdef NO_PROTOCHAIN
5507 return gen_proto(v
, proto
, dir
);
5509 struct block
*b0
, *b
;
5510 struct slist
*s
[100];
5511 int fix2
, fix3
, fix4
, fix5
;
5512 int ahcheck
, again
, end
;
5514 int reg2
= alloc_reg();
5516 memset(s
, 0, sizeof(s
));
5517 fix2
= fix3
= fix4
= fix5
= 0;
5524 b0
= gen_protochain(v
, Q_IP
, dir
);
5525 b
= gen_protochain(v
, Q_IPV6
, dir
);
5529 bpf_error("bad protocol applied for 'protochain'");
5534 * We don't handle variable-length prefixes before the link-layer
5535 * header, or variable-length link-layer headers, here yet.
5536 * We might want to add BPF instructions to do the protochain
5537 * work, to simplify that and, on platforms that have a BPF
5538 * interpreter with the new instructions, let the filtering
5539 * be done in the kernel. (We already require a modified BPF
5540 * engine to do the protochain stuff, to support backward
5541 * branches, and backward branch support is unlikely to appear
5542 * in kernel BPF engines.)
5546 case DLT_IEEE802_11
:
5547 case DLT_PRISM_HEADER
:
5548 case DLT_IEEE802_11_RADIO_AVS
:
5549 case DLT_IEEE802_11_RADIO
:
5551 bpf_error("'protochain' not supported with 802.11");
5554 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5557 * s[0] is a dummy entry to protect other BPF insn from damage
5558 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5559 * hard to find interdependency made by jump table fixup.
5562 s
[i
] = new_stmt(0); /*dummy*/
5567 b0
= gen_linktype(ETHERTYPE_IP
);
5570 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5571 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5573 /* X = ip->ip_hl << 2 */
5574 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5575 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5580 b0
= gen_linktype(ETHERTYPE_IPV6
);
5582 /* A = ip6->ip_nxt */
5583 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5584 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5586 /* X = sizeof(struct ip6_hdr) */
5587 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5593 bpf_error("unsupported proto to gen_protochain");
5597 /* again: if (A == v) goto end; else fall through; */
5599 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5601 s
[i
]->s
.jt
= NULL
; /*later*/
5602 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5606 #ifndef IPPROTO_NONE
5607 #define IPPROTO_NONE 59
5609 /* if (A == IPPROTO_NONE) goto end */
5610 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5611 s
[i
]->s
.jt
= NULL
; /*later*/
5612 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5613 s
[i
]->s
.k
= IPPROTO_NONE
;
5614 s
[fix5
]->s
.jf
= s
[i
];
5618 if (proto
== Q_IPV6
) {
5619 int v6start
, v6end
, v6advance
, j
;
5622 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5623 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5624 s
[i
]->s
.jt
= NULL
; /*later*/
5625 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5626 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5627 s
[fix2
]->s
.jf
= s
[i
];
5629 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5630 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5631 s
[i
]->s
.jt
= NULL
; /*later*/
5632 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5633 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5635 /* if (A == IPPROTO_ROUTING) goto v6advance */
5636 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5637 s
[i
]->s
.jt
= NULL
; /*later*/
5638 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5639 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5641 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5642 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5643 s
[i
]->s
.jt
= NULL
; /*later*/
5644 s
[i
]->s
.jf
= NULL
; /*later*/
5645 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5655 * A = P[X + packet head];
5656 * X = X + (P[X + packet head + 1] + 1) * 8;
5658 /* A = P[X + packet head] */
5659 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5660 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5663 s
[i
] = new_stmt(BPF_ST
);
5666 /* A = P[X + packet head + 1]; */
5667 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5668 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5671 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5675 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5679 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5683 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5686 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5690 /* goto again; (must use BPF_JA for backward jump) */
5691 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5692 s
[i
]->s
.k
= again
- i
- 1;
5693 s
[i
- 1]->s
.jf
= s
[i
];
5697 for (j
= v6start
; j
<= v6end
; j
++)
5698 s
[j
]->s
.jt
= s
[v6advance
];
5701 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5703 s
[fix2
]->s
.jf
= s
[i
];
5709 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5710 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5711 s
[i
]->s
.jt
= NULL
; /*later*/
5712 s
[i
]->s
.jf
= NULL
; /*later*/
5713 s
[i
]->s
.k
= IPPROTO_AH
;
5715 s
[fix3
]->s
.jf
= s
[ahcheck
];
5722 * X = X + (P[X + 1] + 2) * 4;
5725 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5727 /* A = P[X + packet head]; */
5728 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5729 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5732 s
[i
] = new_stmt(BPF_ST
);
5736 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5739 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5743 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5745 /* A = P[X + packet head] */
5746 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5747 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5750 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5754 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5758 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5761 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5765 /* goto again; (must use BPF_JA for backward jump) */
5766 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5767 s
[i
]->s
.k
= again
- i
- 1;
5772 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5774 s
[fix2
]->s
.jt
= s
[end
];
5775 s
[fix4
]->s
.jf
= s
[end
];
5776 s
[fix5
]->s
.jt
= s
[end
];
5783 for (i
= 0; i
< max
- 1; i
++)
5784 s
[i
]->next
= s
[i
+ 1];
5785 s
[max
- 1]->next
= NULL
;
5790 b
= new_block(JMP(BPF_JEQ
));
5791 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5801 static struct block
*
5802 gen_check_802_11_data_frame()
5805 struct block
*b0
, *b1
;
5808 * A data frame has the 0x08 bit (b3) in the frame control field set
5809 * and the 0x04 bit (b2) clear.
5811 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5812 b0
= new_block(JMP(BPF_JSET
));
5816 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5817 b1
= new_block(JMP(BPF_JSET
));
5828 * Generate code that checks whether the packet is a packet for protocol
5829 * <proto> and whether the type field in that protocol's header has
5830 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5831 * IP packet and checks the protocol number in the IP header against <v>.
5833 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5834 * against Q_IP and Q_IPV6.
5836 static struct block
*
5837 gen_proto(v
, proto
, dir
)
5842 struct block
*b0
, *b1
;
5847 if (dir
!= Q_DEFAULT
)
5848 bpf_error("direction applied to 'proto'");
5852 b0
= gen_proto(v
, Q_IP
, dir
);
5853 b1
= gen_proto(v
, Q_IPV6
, dir
);
5859 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5860 * not LLC encapsulation with LLCSAP_IP.
5862 * For IEEE 802 networks - which includes 802.5 token ring
5863 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5864 * says that SNAP encapsulation is used, not LLC encapsulation
5867 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5868 * RFC 2225 say that SNAP encapsulation is used, not LLC
5869 * encapsulation with LLCSAP_IP.
5871 * So we always check for ETHERTYPE_IP.
5873 b0
= gen_linktype(ETHERTYPE_IP
);
5875 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5877 b1
= gen_protochain(v
, Q_IP
);
5887 * Frame Relay packets typically have an OSI
5888 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5889 * generates code to check for all the OSI
5890 * NLPIDs, so calling it and then adding a check
5891 * for the particular NLPID for which we're
5892 * looking is bogus, as we can just check for
5895 * What we check for is the NLPID and a frame
5896 * control field value of UI, i.e. 0x03 followed
5899 * XXX - assumes a 2-byte Frame Relay header with
5900 * DLCI and flags. What if the address is longer?
5902 * XXX - what about SNAP-encapsulated frames?
5904 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5910 * Cisco uses an Ethertype lookalike - for OSI,
5913 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5914 /* OSI in C-HDLC is stuffed with a fudge byte */
5915 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5920 b0
= gen_linktype(LLCSAP_ISONS
);
5921 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5927 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5929 * 4 is the offset of the PDU type relative to the IS-IS
5932 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5937 bpf_error("arp does not encapsulate another protocol");
5941 bpf_error("rarp does not encapsulate another protocol");
5945 bpf_error("atalk encapsulation is not specifiable");
5949 bpf_error("decnet encapsulation is not specifiable");
5953 bpf_error("sca does not encapsulate another protocol");
5957 bpf_error("lat does not encapsulate another protocol");
5961 bpf_error("moprc does not encapsulate another protocol");
5965 bpf_error("mopdl does not encapsulate another protocol");
5969 return gen_linktype(v
);
5972 bpf_error("'udp proto' is bogus");
5976 bpf_error("'tcp proto' is bogus");
5980 bpf_error("'sctp proto' is bogus");
5984 bpf_error("'icmp proto' is bogus");
5988 bpf_error("'igmp proto' is bogus");
5992 bpf_error("'igrp proto' is bogus");
5996 bpf_error("'pim proto' is bogus");
6000 bpf_error("'vrrp proto' is bogus");
6004 bpf_error("'carp proto' is bogus");
6008 b0
= gen_linktype(ETHERTYPE_IPV6
);
6011 * Also check for a fragment header before the final
6014 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6015 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
6017 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6020 b1
= gen_protochain(v
, Q_IPV6
);
6026 bpf_error("'icmp6 proto' is bogus");
6029 bpf_error("'ah proto' is bogus");
6032 bpf_error("'ah proto' is bogus");
6035 bpf_error("'stp proto' is bogus");
6038 bpf_error("'ipx proto' is bogus");
6041 bpf_error("'netbeui proto' is bogus");
6044 bpf_error("'radio proto' is bogus");
6055 register const char *name
;
6058 int proto
= q
.proto
;
6062 bpf_u_int32 mask
, addr
;
6064 bpf_u_int32
**alist
;
6067 struct sockaddr_in
*sin4
;
6068 struct sockaddr_in6
*sin6
;
6069 struct addrinfo
*res
, *res0
;
6070 struct in6_addr mask128
;
6072 struct block
*b
, *tmp
;
6073 int port
, real_proto
;
6079 addr
= pcap_nametonetaddr(name
);
6081 bpf_error("unknown network '%s'", name
);
6082 /* Left justify network addr and calculate its network mask */
6084 while (addr
&& (addr
& 0xff000000) == 0) {
6088 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6092 if (proto
== Q_LINK
) {
6096 case DLT_NETANALYZER
:
6097 case DLT_NETANALYZER_TRANSPARENT
:
6098 eaddr
= pcap_ether_hostton(name
);
6101 "unknown ether host '%s'", name
);
6102 tmp
= gen_prevlinkhdr_check();
6103 b
= gen_ehostop(eaddr
, dir
);
6110 eaddr
= pcap_ether_hostton(name
);
6113 "unknown FDDI host '%s'", name
);
6114 b
= gen_fhostop(eaddr
, dir
);
6119 eaddr
= pcap_ether_hostton(name
);
6122 "unknown token ring host '%s'", name
);
6123 b
= gen_thostop(eaddr
, dir
);
6127 case DLT_IEEE802_11
:
6128 case DLT_PRISM_HEADER
:
6129 case DLT_IEEE802_11_RADIO_AVS
:
6130 case DLT_IEEE802_11_RADIO
:
6132 eaddr
= pcap_ether_hostton(name
);
6135 "unknown 802.11 host '%s'", name
);
6136 b
= gen_wlanhostop(eaddr
, dir
);
6140 case DLT_IP_OVER_FC
:
6141 eaddr
= pcap_ether_hostton(name
);
6144 "unknown Fibre Channel host '%s'", name
);
6145 b
= gen_ipfchostop(eaddr
, dir
);
6150 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6151 } else if (proto
== Q_DECNET
) {
6152 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6154 * I don't think DECNET hosts can be multihomed, so
6155 * there is no need to build up a list of addresses
6157 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6160 alist
= pcap_nametoaddr(name
);
6161 if (alist
== NULL
|| *alist
== NULL
)
6162 bpf_error("unknown host '%s'", name
);
6164 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6166 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6168 tmp
= gen_host(**alist
++, 0xffffffff,
6169 tproto
, dir
, q
.addr
);
6175 memset(&mask128
, 0xff, sizeof(mask128
));
6176 res0
= res
= pcap_nametoaddrinfo(name
);
6178 bpf_error("unknown host '%s'", name
);
6181 tproto
= tproto6
= proto
;
6182 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6186 for (res
= res0
; res
; res
= res
->ai_next
) {
6187 switch (res
->ai_family
) {
6189 if (tproto
== Q_IPV6
)
6192 sin4
= (struct sockaddr_in
*)
6194 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6195 0xffffffff, tproto
, dir
, q
.addr
);
6198 if (tproto6
== Q_IP
)
6201 sin6
= (struct sockaddr_in6
*)
6203 tmp
= gen_host6(&sin6
->sin6_addr
,
6204 &mask128
, tproto6
, dir
, q
.addr
);
6216 bpf_error("unknown host '%s'%s", name
,
6217 (proto
== Q_DEFAULT
)
6219 : " for specified address family");
6226 if (proto
!= Q_DEFAULT
&&
6227 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6228 bpf_error("illegal qualifier of 'port'");
6229 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6230 bpf_error("unknown port '%s'", name
);
6231 if (proto
== Q_UDP
) {
6232 if (real_proto
== IPPROTO_TCP
)
6233 bpf_error("port '%s' is tcp", name
);
6234 else if (real_proto
== IPPROTO_SCTP
)
6235 bpf_error("port '%s' is sctp", name
);
6237 /* override PROTO_UNDEF */
6238 real_proto
= IPPROTO_UDP
;
6240 if (proto
== Q_TCP
) {
6241 if (real_proto
== IPPROTO_UDP
)
6242 bpf_error("port '%s' is udp", name
);
6244 else if (real_proto
== IPPROTO_SCTP
)
6245 bpf_error("port '%s' is sctp", name
);
6247 /* override PROTO_UNDEF */
6248 real_proto
= IPPROTO_TCP
;
6250 if (proto
== Q_SCTP
) {
6251 if (real_proto
== IPPROTO_UDP
)
6252 bpf_error("port '%s' is udp", name
);
6254 else if (real_proto
== IPPROTO_TCP
)
6255 bpf_error("port '%s' is tcp", name
);
6257 /* override PROTO_UNDEF */
6258 real_proto
= IPPROTO_SCTP
;
6261 bpf_error("illegal port number %d < 0", port
);
6263 bpf_error("illegal port number %d > 65535", port
);
6264 b
= gen_port(port
, real_proto
, dir
);
6265 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6269 if (proto
!= Q_DEFAULT
&&
6270 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6271 bpf_error("illegal qualifier of 'portrange'");
6272 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6273 bpf_error("unknown port in range '%s'", name
);
6274 if (proto
== Q_UDP
) {
6275 if (real_proto
== IPPROTO_TCP
)
6276 bpf_error("port in range '%s' is tcp", name
);
6277 else if (real_proto
== IPPROTO_SCTP
)
6278 bpf_error("port in range '%s' is sctp", name
);
6280 /* override PROTO_UNDEF */
6281 real_proto
= IPPROTO_UDP
;
6283 if (proto
== Q_TCP
) {
6284 if (real_proto
== IPPROTO_UDP
)
6285 bpf_error("port in range '%s' is udp", name
);
6286 else if (real_proto
== IPPROTO_SCTP
)
6287 bpf_error("port in range '%s' is sctp", name
);
6289 /* override PROTO_UNDEF */
6290 real_proto
= IPPROTO_TCP
;
6292 if (proto
== Q_SCTP
) {
6293 if (real_proto
== IPPROTO_UDP
)
6294 bpf_error("port in range '%s' is udp", name
);
6295 else if (real_proto
== IPPROTO_TCP
)
6296 bpf_error("port in range '%s' is tcp", name
);
6298 /* override PROTO_UNDEF */
6299 real_proto
= IPPROTO_SCTP
;
6302 bpf_error("illegal port number %d < 0", port1
);
6304 bpf_error("illegal port number %d > 65535", port1
);
6306 bpf_error("illegal port number %d < 0", port2
);
6308 bpf_error("illegal port number %d > 65535", port2
);
6310 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6311 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6316 eaddr
= pcap_ether_hostton(name
);
6318 bpf_error("unknown ether host: %s", name
);
6320 alist
= pcap_nametoaddr(name
);
6321 if (alist
== NULL
|| *alist
== NULL
)
6322 bpf_error("unknown host '%s'", name
);
6323 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6327 bpf_error("'gateway' not supported in this configuration");
6331 real_proto
= lookup_proto(name
, proto
);
6332 if (real_proto
>= 0)
6333 return gen_proto(real_proto
, proto
, dir
);
6335 bpf_error("unknown protocol: %s", name
);
6338 real_proto
= lookup_proto(name
, proto
);
6339 if (real_proto
>= 0)
6340 return gen_protochain(real_proto
, proto
, dir
);
6342 bpf_error("unknown protocol: %s", name
);
6353 gen_mcode(s1
, s2
, masklen
, q
)
6354 register const char *s1
, *s2
;
6355 register unsigned int masklen
;
6358 register int nlen
, mlen
;
6361 nlen
= __pcap_atoin(s1
, &n
);
6362 /* Promote short ipaddr */
6366 mlen
= __pcap_atoin(s2
, &m
);
6367 /* Promote short ipaddr */
6370 bpf_error("non-network bits set in \"%s mask %s\"",
6373 /* Convert mask len to mask */
6375 bpf_error("mask length must be <= 32");
6378 * X << 32 is not guaranteed by C to be 0; it's
6383 m
= 0xffffffff << (32 - masklen
);
6385 bpf_error("non-network bits set in \"%s/%d\"",
6392 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6395 bpf_error("Mask syntax for networks only");
6404 register const char *s
;
6409 int proto
= q
.proto
;
6415 else if (q
.proto
== Q_DECNET
)
6416 vlen
= __pcap_atodn(s
, &v
);
6418 vlen
= __pcap_atoin(s
, &v
);
6425 if (proto
== Q_DECNET
)
6426 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6427 else if (proto
== Q_LINK
) {
6428 bpf_error("illegal link layer address");
6431 if (s
== NULL
&& q
.addr
== Q_NET
) {
6432 /* Promote short net number */
6433 while (v
&& (v
& 0xff000000) == 0) {
6438 /* Promote short ipaddr */
6442 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6447 proto
= IPPROTO_UDP
;
6448 else if (proto
== Q_TCP
)
6449 proto
= IPPROTO_TCP
;
6450 else if (proto
== Q_SCTP
)
6451 proto
= IPPROTO_SCTP
;
6452 else if (proto
== Q_DEFAULT
)
6453 proto
= PROTO_UNDEF
;
6455 bpf_error("illegal qualifier of 'port'");
6458 bpf_error("illegal port number %u > 65535", v
);
6462 b
= gen_port((int)v
, proto
, dir
);
6463 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6469 proto
= IPPROTO_UDP
;
6470 else if (proto
== Q_TCP
)
6471 proto
= IPPROTO_TCP
;
6472 else if (proto
== Q_SCTP
)
6473 proto
= IPPROTO_SCTP
;
6474 else if (proto
== Q_DEFAULT
)
6475 proto
= PROTO_UNDEF
;
6477 bpf_error("illegal qualifier of 'portrange'");
6480 bpf_error("illegal port number %u > 65535", v
);
6484 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6485 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6490 bpf_error("'gateway' requires a name");
6494 return gen_proto((int)v
, proto
, dir
);
6497 return gen_protochain((int)v
, proto
, dir
);
6512 gen_mcode6(s1
, s2
, masklen
, q
)
6513 register const char *s1
, *s2
;
6514 register unsigned int masklen
;
6517 struct addrinfo
*res
;
6518 struct in6_addr
*addr
;
6519 struct in6_addr mask
;
6524 bpf_error("no mask %s supported", s2
);
6526 res
= pcap_nametoaddrinfo(s1
);
6528 bpf_error("invalid ip6 address %s", s1
);
6531 bpf_error("%s resolved to multiple address", s1
);
6532 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6534 if (sizeof(mask
) * 8 < masklen
)
6535 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6536 memset(&mask
, 0, sizeof(mask
));
6537 memset(&mask
, 0xff, masklen
/ 8);
6539 mask
.s6_addr
[masklen
/ 8] =
6540 (0xff << (8 - masklen
% 8)) & 0xff;
6543 a
= (u_int32_t
*)addr
;
6544 m
= (u_int32_t
*)&mask
;
6545 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6546 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6547 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6555 bpf_error("Mask syntax for networks only");
6559 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6565 bpf_error("invalid qualifier against IPv6 address");
6574 register const u_char
*eaddr
;
6577 struct block
*b
, *tmp
;
6579 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6582 case DLT_NETANALYZER
:
6583 case DLT_NETANALYZER_TRANSPARENT
:
6584 tmp
= gen_prevlinkhdr_check();
6585 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6590 return gen_fhostop(eaddr
, (int)q
.dir
);
6592 return gen_thostop(eaddr
, (int)q
.dir
);
6593 case DLT_IEEE802_11
:
6594 case DLT_PRISM_HEADER
:
6595 case DLT_IEEE802_11_RADIO_AVS
:
6596 case DLT_IEEE802_11_RADIO
:
6598 return gen_wlanhostop(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_abs_offset_varpart(&off_linkhdr
);
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
);
6748 tmp
->s
.k
= off_linkhdr
.constant_part
;
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_abs_offset_varpart(&off_linkpl
);
6772 * If "s" is non-null, it has code to arrange that the
6773 * X register contains the variable part of the offset
6774 * of the link-layer payload. Add to it the offset
6775 * computed into the register specified by "index",
6776 * and move that into the X register. Otherwise, just
6777 * load into the X register the offset computed into
6778 * the register specified by "index".
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 link-layer
6791 * payload, and the constant part of the offset of the
6792 * start of the link-layer payload.
6794 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6795 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6797 sappend(inst
->s
, s
);
6800 * Do the computation only if the packet contains
6801 * the protocol in question.
6803 b
= gen_proto_abbrev(proto
);
6805 gen_and(inst
->b
, b
);
6819 * The offset is relative to the beginning of
6820 * the transport-layer header.
6822 * Load the X register with the length of the IPv4 header
6823 * (plus the offset of the link-layer header, if it's
6824 * a variable-length header), in bytes.
6826 * XXX - are there any cases where we want
6828 * XXX - we should, if we're built with
6829 * IPv6 support, generate code to load either
6830 * IPv4, IPv6, or both, as appropriate.
6832 s
= gen_loadx_iphdrlen();
6835 * The X register now contains the sum of the variable
6836 * part of the offset of the link-layer payload and the
6837 * length of the network-layer header.
6839 * Load into the A register the offset relative to
6840 * the beginning of the transport layer header,
6841 * add the X register to that, move that to the
6842 * X register, and load with an offset from the
6843 * X register equal to the sum of the constant part of
6844 * the offset of the link-layer payload and the offset,
6845 * relative to the beginning of the link-layer payload,
6846 * of the network-layer header.
6848 sappend(s
, xfer_to_a(inst
));
6849 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6850 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6851 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6852 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6853 sappend(inst
->s
, s
);
6856 * Do the computation only if the packet contains
6857 * the protocol in question - which is true only
6858 * if this is an IP datagram and is the first or
6859 * only fragment of that datagram.
6861 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6863 gen_and(inst
->b
, b
);
6864 gen_and(gen_proto_abbrev(Q_IP
), b
);
6868 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6871 inst
->regno
= regno
;
6872 s
= new_stmt(BPF_ST
);
6874 sappend(inst
->s
, s
);
6880 gen_relation(code
, a0
, a1
, reversed
)
6882 struct arth
*a0
, *a1
;
6885 struct slist
*s0
, *s1
, *s2
;
6886 struct block
*b
, *tmp
;
6890 if (code
== BPF_JEQ
) {
6891 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6892 b
= new_block(JMP(code
));
6896 b
= new_block(BPF_JMP
|code
|BPF_X
);
6902 sappend(a0
->s
, a1
->s
);
6906 free_reg(a0
->regno
);
6907 free_reg(a1
->regno
);
6909 /* 'and' together protocol checks */
6912 gen_and(a0
->b
, tmp
= a1
->b
);
6928 int regno
= alloc_reg();
6929 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6932 s
= new_stmt(BPF_LD
|BPF_LEN
);
6933 s
->next
= new_stmt(BPF_ST
);
6934 s
->next
->s
.k
= regno
;
6949 a
= (struct arth
*)newchunk(sizeof(*a
));
6953 s
= new_stmt(BPF_LD
|BPF_IMM
);
6955 s
->next
= new_stmt(BPF_ST
);
6971 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6974 s
= new_stmt(BPF_ST
);
6982 gen_arth(code
, a0
, a1
)
6984 struct arth
*a0
, *a1
;
6986 struct slist
*s0
, *s1
, *s2
;
6990 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6995 sappend(a0
->s
, a1
->s
);
6997 free_reg(a0
->regno
);
6998 free_reg(a1
->regno
);
7000 s0
= new_stmt(BPF_ST
);
7001 a0
->regno
= s0
->s
.k
= alloc_reg();
7008 * Here we handle simple allocation of the scratch registers.
7009 * If too many registers are alloc'd, the allocator punts.
7011 static int regused
[BPF_MEMWORDS
];
7015 * Initialize the table of used registers and the current register.
7021 memset(regused
, 0, sizeof regused
);
7025 * Return the next free register.
7030 int n
= BPF_MEMWORDS
;
7033 if (regused
[curreg
])
7034 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7036 regused
[curreg
] = 1;
7040 bpf_error("too many registers needed to evaluate expression");
7046 * Return a register to the table so it can
7056 static struct block
*
7063 s
= new_stmt(BPF_LD
|BPF_LEN
);
7064 b
= new_block(JMP(jmp
));
7075 return gen_len(BPF_JGE
, n
);
7079 * Actually, this is less than or equal.
7087 b
= gen_len(BPF_JGT
, n
);
7094 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7095 * the beginning of the link-layer header.
7096 * XXX - that means you can't test values in the radiotap header, but
7097 * as that header is difficult if not impossible to parse generally
7098 * without a loop, that might not be a severe problem. A new keyword
7099 * "radio" could be added for that, although what you'd really want
7100 * would be a way of testing particular radio header values, which
7101 * would generate code appropriate to the radio header in question.
7104 gen_byteop(op
, idx
, val
)
7115 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7118 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7122 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7126 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7130 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7134 b
= new_block(JMP(BPF_JEQ
));
7141 static u_char abroadcast
[] = { 0x0 };
7144 gen_broadcast(proto
)
7147 bpf_u_int32 hostmask
;
7148 struct block
*b0
, *b1
, *b2
;
7149 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7157 case DLT_ARCNET_LINUX
:
7158 return gen_ahostop(abroadcast
, Q_DST
);
7160 case DLT_NETANALYZER
:
7161 case DLT_NETANALYZER_TRANSPARENT
:
7162 b1
= gen_prevlinkhdr_check();
7163 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7168 return gen_fhostop(ebroadcast
, Q_DST
);
7170 return gen_thostop(ebroadcast
, Q_DST
);
7171 case DLT_IEEE802_11
:
7172 case DLT_PRISM_HEADER
:
7173 case DLT_IEEE802_11_RADIO_AVS
:
7174 case DLT_IEEE802_11_RADIO
:
7176 return gen_wlanhostop(ebroadcast
, Q_DST
);
7177 case DLT_IP_OVER_FC
:
7178 return gen_ipfchostop(ebroadcast
, Q_DST
);
7180 bpf_error("not a broadcast link");
7186 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7187 * as an indication that we don't know the netmask, and fail
7190 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7191 bpf_error("netmask not known, so 'ip broadcast' not supported");
7192 b0
= gen_linktype(ETHERTYPE_IP
);
7193 hostmask
= ~netmask
;
7194 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7195 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7196 (bpf_int32
)(~0 & hostmask
), hostmask
);
7201 bpf_error("only link-layer/IP broadcast filters supported");
7207 * Generate code to test the low-order bit of a MAC address (that's
7208 * the bottom bit of the *first* byte).
7210 static struct block
*
7211 gen_mac_multicast(offset
)
7214 register struct block
*b0
;
7215 register struct slist
*s
;
7217 /* link[offset] & 1 != 0 */
7218 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7219 b0
= new_block(JMP(BPF_JSET
));
7226 gen_multicast(proto
)
7229 register struct block
*b0
, *b1
, *b2
;
7230 register struct slist
*s
;
7238 case DLT_ARCNET_LINUX
:
7239 /* all ARCnet multicasts use the same address */
7240 return gen_ahostop(abroadcast
, Q_DST
);
7242 case DLT_NETANALYZER
:
7243 case DLT_NETANALYZER_TRANSPARENT
:
7244 b1
= gen_prevlinkhdr_check();
7245 /* ether[0] & 1 != 0 */
7246 b0
= gen_mac_multicast(0);
7252 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7254 * XXX - was that referring to bit-order issues?
7256 /* fddi[1] & 1 != 0 */
7257 return gen_mac_multicast(1);
7259 /* tr[2] & 1 != 0 */
7260 return gen_mac_multicast(2);
7261 case DLT_IEEE802_11
:
7262 case DLT_PRISM_HEADER
:
7263 case DLT_IEEE802_11_RADIO_AVS
:
7264 case DLT_IEEE802_11_RADIO
:
7269 * For control frames, there is no DA.
7271 * For management frames, DA is at an
7272 * offset of 4 from the beginning of
7275 * For data frames, DA is at an offset
7276 * of 4 from the beginning of the packet
7277 * if To DS is clear and at an offset of
7278 * 16 from the beginning of the packet
7283 * Generate the tests to be done for data frames.
7285 * First, check for To DS set, i.e. "link[1] & 0x01".
7287 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7288 b1
= new_block(JMP(BPF_JSET
));
7289 b1
->s
.k
= 0x01; /* To DS */
7293 * If To DS is set, the DA is at 16.
7295 b0
= gen_mac_multicast(16);
7299 * Now, check for To DS not set, i.e. check
7300 * "!(link[1] & 0x01)".
7302 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7303 b2
= new_block(JMP(BPF_JSET
));
7304 b2
->s
.k
= 0x01; /* To DS */
7309 * If To DS is not set, the DA is at 4.
7311 b1
= gen_mac_multicast(4);
7315 * Now OR together the last two checks. That gives
7316 * the complete set of checks for data frames.
7321 * Now check for a data frame.
7322 * I.e, check "link[0] & 0x08".
7324 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7325 b1
= new_block(JMP(BPF_JSET
));
7330 * AND that with the checks done for data frames.
7335 * If the high-order bit of the type value is 0, this
7336 * is a management frame.
7337 * I.e, check "!(link[0] & 0x08)".
7339 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7340 b2
= new_block(JMP(BPF_JSET
));
7346 * For management frames, the DA is at 4.
7348 b1
= gen_mac_multicast(4);
7352 * OR that with the checks done for data frames.
7353 * That gives the checks done for management and
7359 * If the low-order bit of the type value is 1,
7360 * this is either a control frame or a frame
7361 * with a reserved type, and thus not a
7364 * I.e., check "!(link[0] & 0x04)".
7366 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7367 b1
= new_block(JMP(BPF_JSET
));
7373 * AND that with the checks for data and management
7378 case DLT_IP_OVER_FC
:
7379 b0
= gen_mac_multicast(2);
7384 /* Link not known to support multicasts */
7388 b0
= gen_linktype(ETHERTYPE_IP
);
7389 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7394 b0
= gen_linktype(ETHERTYPE_IPV6
);
7395 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7399 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7405 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7406 * Outbound traffic is sent by this machine, while inbound traffic is
7407 * sent by a remote machine (and may include packets destined for a
7408 * unicast or multicast link-layer address we are not subscribing to).
7409 * These are the same definitions implemented by pcap_setdirection().
7410 * Capturing only unicast traffic destined for this host is probably
7411 * better accomplished using a higher-layer filter.
7417 register struct block
*b0
;
7420 * Only some data link types support inbound/outbound qualifiers.
7424 b0
= gen_relation(BPF_JEQ
,
7425 gen_load(Q_LINK
, gen_loadi(0), 1),
7432 /* match outgoing packets */
7433 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7435 /* match incoming packets */
7436 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7441 /* match outgoing packets */
7442 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7444 /* to filter on inbound traffic, invert the match */
7449 #ifdef HAVE_NET_PFVAR_H
7451 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7452 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7458 /* match outgoing packets */
7459 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7461 /* match incoming packets */
7462 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7466 case DLT_JUNIPER_MFR
:
7467 case DLT_JUNIPER_MLFR
:
7468 case DLT_JUNIPER_MLPPP
:
7469 case DLT_JUNIPER_ATM1
:
7470 case DLT_JUNIPER_ATM2
:
7471 case DLT_JUNIPER_PPPOE
:
7472 case DLT_JUNIPER_PPPOE_ATM
:
7473 case DLT_JUNIPER_GGSN
:
7474 case DLT_JUNIPER_ES
:
7475 case DLT_JUNIPER_MONITOR
:
7476 case DLT_JUNIPER_SERVICES
:
7477 case DLT_JUNIPER_ETHER
:
7478 case DLT_JUNIPER_PPP
:
7479 case DLT_JUNIPER_FRELAY
:
7480 case DLT_JUNIPER_CHDLC
:
7481 case DLT_JUNIPER_VP
:
7482 case DLT_JUNIPER_ST
:
7483 case DLT_JUNIPER_ISM
:
7484 case DLT_JUNIPER_VS
:
7485 case DLT_JUNIPER_SRX_E2E
:
7486 case DLT_JUNIPER_FIBRECHANNEL
:
7487 case DLT_JUNIPER_ATM_CEMIC
:
7489 /* juniper flags (including direction) are stored
7490 * the byte after the 3-byte magic number */
7492 /* match outgoing packets */
7493 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7495 /* match incoming packets */
7496 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7502 * If we have packet meta-data indicating a direction,
7503 * check it, otherwise give up as this link-layer type
7504 * has nothing in the packet data.
7506 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7508 * This is Linux with PF_PACKET support.
7509 * If this is a *live* capture, we can look at
7510 * special meta-data in the filter expression;
7511 * if it's a savefile, we can't.
7513 if (bpf_pcap
->rfile
!= NULL
) {
7514 /* We have a FILE *, so this is a savefile */
7515 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7520 /* match outgoing packets */
7521 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7524 /* to filter on inbound traffic, invert the match */
7527 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7528 bpf_error("inbound/outbound not supported on linktype %d",
7532 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7537 #ifdef HAVE_NET_PFVAR_H
7538 /* PF firewall log matched interface */
7540 gen_pf_ifname(const char *ifname
)
7545 if (linktype
!= DLT_PFLOG
) {
7546 bpf_error("ifname supported only on PF linktype");
7549 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7550 off
= offsetof(struct pfloghdr
, ifname
);
7551 if (strlen(ifname
) >= len
) {
7552 bpf_error("ifname interface names can only be %d characters",
7556 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7560 /* PF firewall log ruleset name */
7562 gen_pf_ruleset(char *ruleset
)
7566 if (linktype
!= DLT_PFLOG
) {
7567 bpf_error("ruleset supported only on PF linktype");
7571 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7572 bpf_error("ruleset names can only be %ld characters",
7573 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7577 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7578 strlen(ruleset
), (const u_char
*)ruleset
);
7582 /* PF firewall log rule number */
7588 if (linktype
!= DLT_PFLOG
) {
7589 bpf_error("rnr supported only on PF linktype");
7593 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7598 /* PF firewall log sub-rule number */
7600 gen_pf_srnr(int srnr
)
7604 if (linktype
!= DLT_PFLOG
) {
7605 bpf_error("srnr supported only on PF linktype");
7609 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7614 /* PF firewall log reason code */
7616 gen_pf_reason(int reason
)
7620 if (linktype
!= DLT_PFLOG
) {
7621 bpf_error("reason supported only on PF linktype");
7625 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7630 /* PF firewall log action */
7632 gen_pf_action(int action
)
7636 if (linktype
!= DLT_PFLOG
) {
7637 bpf_error("action supported only on PF linktype");
7641 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7645 #else /* !HAVE_NET_PFVAR_H */
7647 gen_pf_ifname(const char *ifname
)
7649 bpf_error("libpcap was compiled without pf support");
7655 gen_pf_ruleset(char *ruleset
)
7657 bpf_error("libpcap was compiled on a machine without pf support");
7665 bpf_error("libpcap was compiled on a machine without pf support");
7671 gen_pf_srnr(int srnr
)
7673 bpf_error("libpcap was compiled on a machine without pf support");
7679 gen_pf_reason(int reason
)
7681 bpf_error("libpcap was compiled on a machine without pf support");
7687 gen_pf_action(int action
)
7689 bpf_error("libpcap was compiled on a machine without pf support");
7693 #endif /* HAVE_NET_PFVAR_H */
7695 /* IEEE 802.11 wireless header */
7697 gen_p80211_type(int type
, int mask
)
7703 case DLT_IEEE802_11
:
7704 case DLT_PRISM_HEADER
:
7705 case DLT_IEEE802_11_RADIO_AVS
:
7706 case DLT_IEEE802_11_RADIO
:
7707 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7712 bpf_error("802.11 link-layer types supported only on 802.11");
7720 gen_p80211_fcdir(int fcdir
)
7726 case DLT_IEEE802_11
:
7727 case DLT_PRISM_HEADER
:
7728 case DLT_IEEE802_11_RADIO_AVS
:
7729 case DLT_IEEE802_11_RADIO
:
7733 bpf_error("frame direction supported only with 802.11 headers");
7737 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7738 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7745 register const u_char
*eaddr
;
7751 case DLT_ARCNET_LINUX
:
7752 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7754 return (gen_ahostop(eaddr
, (int)q
.dir
));
7756 bpf_error("ARCnet address used in non-arc expression");
7762 bpf_error("aid supported only on ARCnet");
7765 bpf_error("ARCnet address used in non-arc expression");
7770 static struct block
*
7771 gen_ahostop(eaddr
, dir
)
7772 register const u_char
*eaddr
;
7775 register struct block
*b0
, *b1
;
7778 /* src comes first, different from Ethernet */
7780 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7783 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7786 b0
= gen_ahostop(eaddr
, Q_SRC
);
7787 b1
= gen_ahostop(eaddr
, Q_DST
);
7793 b0
= gen_ahostop(eaddr
, Q_SRC
);
7794 b1
= gen_ahostop(eaddr
, Q_DST
);
7799 bpf_error("'addr1' is only supported on 802.11");
7803 bpf_error("'addr2' is only supported on 802.11");
7807 bpf_error("'addr3' is only supported on 802.11");
7811 bpf_error("'addr4' is only supported on 802.11");
7815 bpf_error("'ra' is only supported on 802.11");
7819 bpf_error("'ta' is only supported on 802.11");
7826 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7827 static struct block
*
7828 gen_vlan_bpf_extensions(int vlan_num
)
7830 struct block
*b0
, *b1
;
7833 /* generate new filter code based on extracting packet
7835 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7836 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7838 b0
= new_block(JMP(BPF_JEQ
));
7842 if (vlan_num
>= 0) {
7843 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7844 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7846 b1
= new_block(JMP(BPF_JEQ
));
7848 b1
->s
.k
= (bpf_int32
) vlan_num
;
7858 static struct block
*
7859 gen_vlan_no_bpf_extensions(int vlan_num
)
7861 struct block
*b0
, *b1
;
7863 /* check for VLAN, including QinQ */
7864 b0
= gen_linktype(ETHERTYPE_8021Q
);
7865 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7869 /* If a specific VLAN is requested, check VLAN id */
7870 if (vlan_num
>= 0) {
7871 b1
= gen_mcmp(OR_NET
, 0, BPF_H
,
7872 (bpf_int32
)vlan_num
, 0x0fff);
7878 * The payload follows the full header, including the
7879 * VLAN tags, so skip past this VLAN tag.
7881 off_linkpl
.constant_part
+= 4;
7884 * The link-layer type information follows the VLAN tags, so
7885 * skip past this VLAN tag.
7893 * support IEEE 802.1Q VLAN trunk over ethernet
7901 /* can't check for VLAN-encapsulated packets inside MPLS */
7902 if (label_stack_depth
> 0)
7903 bpf_error("no VLAN match after MPLS");
7906 * Check for a VLAN packet, and then change the offsets to point
7907 * to the type and data fields within the VLAN packet. Just
7908 * increment the offsets, so that we can support a hierarchy, e.g.
7909 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7912 * XXX - this is a bit of a kludge. If we were to split the
7913 * compiler into a parser that parses an expression and
7914 * generates an expression tree, and a code generator that
7915 * takes an expression tree (which could come from our
7916 * parser or from some other parser) and generates BPF code,
7917 * we could perhaps make the offsets parameters of routines
7918 * and, in the handler for an "AND" node, pass to subnodes
7919 * other than the VLAN node the adjusted offsets.
7921 * This would mean that "vlan" would, instead of changing the
7922 * behavior of *all* tests after it, change only the behavior
7923 * of tests ANDed with it. That would change the documented
7924 * semantics of "vlan", which might break some expressions.
7925 * However, it would mean that "(vlan and ip) or ip" would check
7926 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7927 * checking only for VLAN-encapsulated IP, so that could still
7928 * be considered worth doing; it wouldn't break expressions
7929 * that are of the form "vlan and ..." or "vlan N and ...",
7930 * which I suspect are the most common expressions involving
7931 * "vlan". "vlan or ..." doesn't necessarily do what the user
7932 * would really want, now, as all the "or ..." tests would
7933 * be done assuming a VLAN, even though the "or" could be viewed
7934 * as meaning "or, if this isn't a VLAN packet...".
7939 case DLT_NETANALYZER
:
7940 case DLT_NETANALYZER_TRANSPARENT
:
7941 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7942 if (vlan_stack_depth
== 0) {
7944 * Do we need special VLAN handling?
7946 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
7947 b0
= gen_vlan_bpf_extensions(vlan_num
);
7949 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7952 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7955 case DLT_IEEE802_11
:
7956 case DLT_PRISM_HEADER
:
7957 case DLT_IEEE802_11_RADIO_AVS
:
7958 case DLT_IEEE802_11_RADIO
:
7959 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7963 bpf_error("no VLAN support for data link type %d",
7980 struct block
*b0
, *b1
;
7982 if (label_stack_depth
> 0) {
7983 /* just match the bottom-of-stack bit clear */
7984 b0
= gen_mcmp(OR_MPLSPL
, off_nl
-2, BPF_B
, 0, 0x01);
7987 * We're not in an MPLS stack yet, so check the link-layer
7988 * type against MPLS.
7992 case DLT_C_HDLC
: /* fall through */
7994 case DLT_NETANALYZER
:
7995 case DLT_NETANALYZER_TRANSPARENT
:
7996 b0
= gen_linktype(ETHERTYPE_MPLS
);
8000 b0
= gen_linktype(PPP_MPLS_UCAST
);
8003 /* FIXME add other DLT_s ...
8004 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8005 * leave it for now */
8008 bpf_error("no MPLS support for data link type %d",
8016 /* If a specific MPLS label is requested, check it */
8017 if (label_num
>= 0) {
8018 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8019 b1
= gen_mcmp(OR_MPLSPL
, off_nl
, BPF_W
, (bpf_int32
)label_num
,
8020 0xfffff000); /* only compare the first 20 bits */
8026 * Change the offsets to point to the type and data fields within
8027 * the MPLS packet. Just increment the offsets, so that we
8028 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8029 * capture packets with an outer label of 100000 and an inner
8032 * Increment the MPLS stack depth as well; this indicates that
8033 * we're checking MPLS-encapsulated headers, to make sure higher
8034 * level code generators don't try to match against IP-related
8035 * protocols such as Q_ARP, Q_RARP etc.
8037 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8041 label_stack_depth
++;
8046 * Support PPPOE discovery and session.
8051 /* check for PPPoE discovery */
8052 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8056 gen_pppoes(sess_num
)
8059 struct block
*b0
, *b1
;
8062 * Test against the PPPoE session link-layer type.
8064 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8066 /* If a specific session is requested, check PPPoE session id */
8067 if (sess_num
>= 0) {
8068 b1
= gen_mcmp(OR_LINKPL
, off_nl
, BPF_W
,
8069 (bpf_int32
)sess_num
, 0x0000ffff);
8075 * Change the offsets to point to the type and data fields within
8076 * the PPP packet, and note that this is PPPoE rather than
8079 * XXX - this is a bit of a kludge. If we were to split the
8080 * compiler into a parser that parses an expression and
8081 * generates an expression tree, and a code generator that
8082 * takes an expression tree (which could come from our
8083 * parser or from some other parser) and generates BPF code,
8084 * we could perhaps make the offsets parameters of routines
8085 * and, in the handler for an "AND" node, pass to subnodes
8086 * other than the PPPoE node the adjusted offsets.
8088 * This would mean that "pppoes" would, instead of changing the
8089 * behavior of *all* tests after it, change only the behavior
8090 * of tests ANDed with it. That would change the documented
8091 * semantics of "pppoes", which might break some expressions.
8092 * However, it would mean that "(pppoes and ip) or ip" would check
8093 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8094 * checking only for VLAN-encapsulated IP, so that could still
8095 * be considered worth doing; it wouldn't break expressions
8096 * that are of the form "pppoes and ..." which I suspect are the
8097 * most common expressions involving "pppoes". "pppoes or ..."
8098 * doesn't necessarily do what the user would really want, now,
8099 * as all the "or ..." tests would be done assuming PPPoE, even
8100 * though the "or" could be viewed as meaning "or, if this isn't
8101 * a PPPoE packet...".
8103 * The "network-layer" protocol is PPPoE, which has a 6-byte
8104 * PPPoE header, followed by a PPP packet.
8106 * There is no HDLC encapsulation for the PPP packet (it's
8107 * encapsulated in PPPoES instead), so the link-layer type
8108 * starts at the first byte of the PPP packet. For PPPoE,
8109 * that offset is relative to the beginning of the total
8110 * link-layer payload, including any 802.2 LLC header, so
8111 * it's 6 bytes past off_nl.
8113 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8114 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8118 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8121 off_nl_nosnap
= 0; /* no 802.2 LLC */
8127 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8139 bpf_error("'vpi' supported only on raw ATM");
8140 if (off_vpi
== (u_int
)-1)
8142 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8148 bpf_error("'vci' supported only on raw ATM");
8149 if (off_vci
== (u_int
)-1)
8151 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8156 if (off_proto
== (u_int
)-1)
8157 abort(); /* XXX - this isn't on FreeBSD */
8158 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8163 if (off_payload
== (u_int
)-1)
8165 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8166 0xffffffff, jtype
, reverse
, jvalue
);
8171 bpf_error("'callref' supported only on raw ATM");
8172 if (off_proto
== (u_int
)-1)
8174 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8175 jtype
, reverse
, jvalue
);
8185 gen_atmtype_abbrev(type
)
8188 struct block
*b0
, *b1
;
8193 /* Get all packets in Meta signalling Circuit */
8195 bpf_error("'metac' supported only on raw ATM");
8196 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8197 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8202 /* Get all packets in Broadcast Circuit*/
8204 bpf_error("'bcc' supported only on raw ATM");
8205 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8206 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8211 /* Get all cells in Segment OAM F4 circuit*/
8213 bpf_error("'oam4sc' supported only on raw ATM");
8214 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8215 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8220 /* Get all cells in End-to-End OAM F4 Circuit*/
8222 bpf_error("'oam4ec' supported only on raw ATM");
8223 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8224 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8229 /* Get all packets in connection Signalling Circuit */
8231 bpf_error("'sc' supported only on raw ATM");
8232 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8233 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8238 /* Get all packets in ILMI Circuit */
8240 bpf_error("'ilmic' supported only on raw ATM");
8241 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8242 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8247 /* Get all LANE packets */
8249 bpf_error("'lane' supported only on raw ATM");
8250 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8253 * Arrange that all subsequent tests assume LANE
8254 * rather than LLC-encapsulated packets, and set
8255 * the offsets appropriately for LANE-encapsulated
8258 * We assume LANE means Ethernet, not Token Ring.
8260 PUSH_LINKHDR(DLT_EN10MB
, 0,
8261 off_payload
+ 2, /* Ethernet header */
8264 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8265 off_nl
= 0; /* Ethernet II */
8266 off_nl_nosnap
= 3; /* 802.3+802.2 */
8270 /* Get all LLC-encapsulated packets */
8272 bpf_error("'llc' supported only on raw ATM");
8273 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8274 linktype
= prevlinktype
;
8284 * Filtering for MTP2 messages based on li value
8285 * FISU, length is null
8286 * LSSU, length is 1 or 2
8287 * MSU, length is 3 or more
8288 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8291 gen_mtp2type_abbrev(type
)
8294 struct block
*b0
, *b1
;
8299 if ( (linktype
!= DLT_MTP2
) &&
8300 (linktype
!= DLT_ERF
) &&
8301 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8302 bpf_error("'fisu' supported only on MTP2");
8303 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8304 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8308 if ( (linktype
!= DLT_MTP2
) &&
8309 (linktype
!= DLT_ERF
) &&
8310 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8311 bpf_error("'lssu' supported only on MTP2");
8312 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8313 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8318 if ( (linktype
!= DLT_MTP2
) &&
8319 (linktype
!= DLT_ERF
) &&
8320 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8321 bpf_error("'msu' supported only on MTP2");
8322 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8326 if ( (linktype
!= DLT_MTP2
) &&
8327 (linktype
!= DLT_ERF
) &&
8328 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8329 bpf_error("'hfisu' supported only on MTP2_HSL");
8330 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8331 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8335 if ( (linktype
!= DLT_MTP2
) &&
8336 (linktype
!= DLT_ERF
) &&
8337 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8338 bpf_error("'hlssu' supported only on MTP2_HSL");
8339 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8340 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8345 if ( (linktype
!= DLT_MTP2
) &&
8346 (linktype
!= DLT_ERF
) &&
8347 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8348 bpf_error("'hmsu' supported only on MTP2_HSL");
8349 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8359 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8366 bpf_u_int32 val1
, val2
, val3
;
8367 u_int newoff_sio
=off_sio
;
8368 u_int newoff_opc
=off_opc
;
8369 u_int newoff_dpc
=off_dpc
;
8370 u_int newoff_sls
=off_sls
;
8372 switch (mtp3field
) {
8375 newoff_sio
+= 3; /* offset for MTP2_HSL */
8379 if (off_sio
== (u_int
)-1)
8380 bpf_error("'sio' supported only on SS7");
8381 /* sio coded on 1 byte so max value 255 */
8383 bpf_error("sio value %u too big; max value = 255",
8385 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8386 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8392 if (off_opc
== (u_int
)-1)
8393 bpf_error("'opc' supported only on SS7");
8394 /* opc coded on 14 bits so max value 16383 */
8396 bpf_error("opc value %u too big; max value = 16383",
8398 /* the following instructions are made to convert jvalue
8399 * to the form used to write opc in an ss7 message*/
8400 val1
= jvalue
& 0x00003c00;
8402 val2
= jvalue
& 0x000003fc;
8404 val3
= jvalue
& 0x00000003;
8406 jvalue
= val1
+ val2
+ val3
;
8407 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8408 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8416 if (off_dpc
== (u_int
)-1)
8417 bpf_error("'dpc' supported only on SS7");
8418 /* dpc coded on 14 bits so max value 16383 */
8420 bpf_error("dpc value %u too big; max value = 16383",
8422 /* the following instructions are made to convert jvalue
8423 * to the forme used to write dpc in an ss7 message*/
8424 val1
= jvalue
& 0x000000ff;
8426 val2
= jvalue
& 0x00003f00;
8428 jvalue
= val1
+ val2
;
8429 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8430 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8436 if (off_sls
== (u_int
)-1)
8437 bpf_error("'sls' supported only on SS7");
8438 /* sls coded on 4 bits so max value 15 */
8440 bpf_error("sls value %u too big; max value = 15",
8442 /* the following instruction is made to convert jvalue
8443 * to the forme used to write sls in an ss7 message*/
8444 jvalue
= jvalue
<< 4;
8445 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8446 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8455 static struct block
*
8456 gen_msg_abbrev(type
)
8462 * Q.2931 signalling protocol messages for handling virtual circuits
8463 * establishment and teardown
8468 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8472 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8476 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8480 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8484 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8487 case A_RELEASE_DONE
:
8488 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8498 gen_atmmulti_abbrev(type
)
8501 struct block
*b0
, *b1
;
8507 bpf_error("'oam' supported only on raw ATM");
8508 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8513 bpf_error("'oamf4' supported only on raw ATM");
8515 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8516 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8518 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8524 * Get Q.2931 signalling messages for switched
8525 * virtual connection
8528 bpf_error("'connectmsg' supported only on raw ATM");
8529 b0
= gen_msg_abbrev(A_SETUP
);
8530 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8532 b0
= gen_msg_abbrev(A_CONNECT
);
8534 b0
= gen_msg_abbrev(A_CONNECTACK
);
8536 b0
= gen_msg_abbrev(A_RELEASE
);
8538 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8540 b0
= gen_atmtype_abbrev(A_SC
);
8546 bpf_error("'metaconnect' supported only on raw ATM");
8547 b0
= gen_msg_abbrev(A_SETUP
);
8548 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8550 b0
= gen_msg_abbrev(A_CONNECT
);
8552 b0
= gen_msg_abbrev(A_RELEASE
);
8554 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8556 b0
= gen_atmtype_abbrev(A_METAC
);