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_PREVMPLSHDR
, /* previous MPLS header */
198 OR_LINKTYPE
, /* link-layer type */
199 OR_LINKPL
, /* link-layer payload */
200 OR_LINKPL_NOSNAP
, /* link-layer payload, 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 is an absolute offset from the beginning
841 * For Ethernet, it's the offset of the Ethernet type field; this
842 * means that it must have a value that skips VLAN tags.
844 * For link-layer types that always use 802.2 headers, it's the
845 * offset of the LLC header; this means that it must have a value
846 * that skips VLAN tags.
848 * For PPP, it's the offset of the PPP type field.
850 * For Cisco HDLC, it's the offset of the CHDLC type field.
852 * For BSD loopback, it's the offset of the AF_ value.
854 * For Linux cooked sockets, it's the offset of the type field.
856 * off_linktype.constant_part is set to -1 for no encapsulation,
857 * in which case, IP is assumed.
859 static bpf_abs_offset off_linktype
;
862 * TRUE if the link layer includes an ATM pseudo-header.
864 static int is_atm
= 0;
867 * These are offsets for the ATM pseudo-header.
869 static u_int off_vpi
;
870 static u_int off_vci
;
871 static u_int off_proto
;
874 * These are offsets for the MTP2 fields.
877 static u_int off_li_hsl
;
880 * These are offsets for the MTP3 fields.
882 static u_int off_sio
;
883 static u_int off_opc
;
884 static u_int off_dpc
;
885 static u_int off_sls
;
888 * This is the offset of the first byte after the ATM pseudo_header,
889 * or -1 if there is no ATM pseudo-header.
891 static u_int off_payload
;
894 * These are offsets to the beginning of the network-layer header.
895 * They are relative to the beginning of the link-layer payload (i.e.,
896 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
898 * If the link layer never uses 802.2 LLC:
900 * "off_nl" and "off_nl_nosnap" are the same.
902 * If the link layer always uses 802.2 LLC:
904 * "off_nl" is the offset if there's a SNAP header following
907 * "off_nl_nosnap" is the offset if there's no SNAP header.
909 * If the link layer is Ethernet:
911 * "off_nl" is the offset if the packet is an Ethernet II packet
912 * (we assume no 802.3+802.2+SNAP);
914 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
915 * with an 802.2 header following it.
918 static u_int off_nl_nosnap
;
921 static int prevlinktype
;
922 static int outermostlinktype
;
928 pcap_fddipad
= p
->fddipad
;
931 * We start out with only one link-layer header.
933 outermostlinktype
= pcap_datalink(p
);
934 off_outermostlinkhdr
.constant_part
= 0;
935 off_outermostlinkhdr
.is_variable
= 0;
936 off_outermostlinkhdr
.reg
= -1;
938 prevlinktype
= outermostlinktype
;
939 off_prevlinkhdr
.constant_part
= 0;
940 off_prevlinkhdr
.is_variable
= 0;
941 off_prevlinkhdr
.reg
= -1;
943 linktype
= outermostlinktype
;
944 off_linkhdr
.constant_part
= 0;
945 off_linkhdr
.is_variable
= 0;
946 off_linkhdr
.reg
= -1;
951 off_linkpl
.constant_part
= 0;
952 off_linkpl
.is_variable
= 0;
955 off_linktype
.constant_part
= 0;
956 off_linktype
.is_variable
= 0;
957 off_linktype
.reg
= -1;
960 * Assume it's not raw ATM with a pseudo-header, for now.
969 * And assume we're not doing SS7.
978 label_stack_depth
= 0;
979 vlan_stack_depth
= 0;
984 off_linktype
.constant_part
= 2;
985 off_linkpl
.constant_part
= 6;
986 off_nl
= 0; /* XXX in reality, variable! */
987 off_nl_nosnap
= 0; /* no 802.2 LLC */
990 case DLT_ARCNET_LINUX
:
991 off_linktype
.constant_part
= 4;
992 off_linkpl
.constant_part
= 8;
993 off_nl
= 0; /* XXX in reality, variable! */
994 off_nl_nosnap
= 0; /* no 802.2 LLC */
998 off_linktype
.constant_part
= 12;
999 off_linkpl
.constant_part
= 14; /* Ethernet header length */
1000 off_nl
= 0; /* Ethernet II */
1001 off_nl_nosnap
= 3; /* 802.3+802.2 */
1006 * SLIP doesn't have a link level type. The 16 byte
1007 * header is hacked into our SLIP driver.
1009 off_linktype
.constant_part
= -1;
1010 off_linkpl
.constant_part
= 16;
1012 off_nl_nosnap
= 0; /* no 802.2 LLC */
1015 case DLT_SLIP_BSDOS
:
1016 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1017 off_linktype
.constant_part
= -1;
1019 off_linkpl
.constant_part
= 24;
1021 off_nl_nosnap
= 0; /* no 802.2 LLC */
1026 off_linktype
.constant_part
= 0;
1027 off_linkpl
.constant_part
= 4;
1029 off_nl_nosnap
= 0; /* no 802.2 LLC */
1033 off_linktype
.constant_part
= 0;
1034 off_linkpl
.constant_part
= 12;
1036 off_nl_nosnap
= 0; /* no 802.2 LLC */
1041 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1042 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1043 off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1044 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1046 off_nl_nosnap
= 0; /* no 802.2 LLC */
1051 * This does no include the Ethernet header, and
1052 * only covers session state.
1054 off_linktype
.constant_part
= 6;
1055 off_linkpl
.constant_part
= 8;
1057 off_nl_nosnap
= 0; /* no 802.2 LLC */
1061 off_linktype
.constant_part
= 5;
1062 off_linkpl
.constant_part
= 24;
1064 off_nl_nosnap
= 0; /* no 802.2 LLC */
1069 * FDDI doesn't really have a link-level type field.
1070 * We set "off_linktype" to the offset of the LLC header.
1072 * To check for Ethernet types, we assume that SSAP = SNAP
1073 * is being used and pick out the encapsulated Ethernet type.
1074 * XXX - should we generate code to check for SNAP?
1076 off_linktype
.constant_part
= 13;
1077 off_linktype
.constant_part
+= pcap_fddipad
;
1078 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1079 off_linkpl
.constant_part
+= pcap_fddipad
;
1080 off_nl
= 8; /* 802.2+SNAP */
1081 off_nl_nosnap
= 3; /* 802.2 */
1086 * Token Ring doesn't really have a link-level type field.
1087 * We set "off_linktype" to the offset of the LLC header.
1089 * To check for Ethernet types, we assume that SSAP = SNAP
1090 * is being used and pick out the encapsulated Ethernet type.
1091 * XXX - should we generate code to check for SNAP?
1093 * XXX - the header is actually variable-length.
1094 * Some various Linux patched versions gave 38
1095 * as "off_linktype" and 40 as "off_nl"; however,
1096 * if a token ring packet has *no* routing
1097 * information, i.e. is not source-routed, the correct
1098 * values are 20 and 22, as they are in the vanilla code.
1100 * A packet is source-routed iff the uppermost bit
1101 * of the first byte of the source address, at an
1102 * offset of 8, has the uppermost bit set. If the
1103 * packet is source-routed, the total number of bytes
1104 * of routing information is 2 plus bits 0x1F00 of
1105 * the 16-bit value at an offset of 14 (shifted right
1106 * 8 - figure out which byte that is).
1108 off_linktype
.constant_part
= 14;
1109 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1110 off_nl
= 8; /* 802.2+SNAP */
1111 off_nl_nosnap
= 3; /* 802.2 */
1114 case DLT_PRISM_HEADER
:
1115 case DLT_IEEE802_11_RADIO_AVS
:
1116 case DLT_IEEE802_11_RADIO
:
1117 off_linkhdr
.is_variable
= 1;
1118 /* Fall through, 802.11 doesn't have a variable link
1119 * prefix but is otherwise the same. */
1121 case DLT_IEEE802_11
:
1123 * 802.11 doesn't really have a link-level type field.
1124 * We set "off_linktype.constant_part" to the offset of
1127 * To check for Ethernet types, we assume that SSAP = SNAP
1128 * is being used and pick out the encapsulated Ethernet type.
1129 * XXX - should we generate code to check for SNAP?
1131 * We also handle variable-length radio headers here.
1132 * The Prism header is in theory variable-length, but in
1133 * practice it's always 144 bytes long. However, some
1134 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1135 * sometimes or always supply an AVS header, so we
1136 * have to check whether the radio header is a Prism
1137 * header or an AVS header, so, in practice, it's
1140 off_linktype
.constant_part
= 24;
1141 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1142 off_linkpl
.is_variable
= 1;
1143 off_nl
= 8; /* 802.2+SNAP */
1144 off_nl_nosnap
= 3; /* 802.2 */
1149 * At the moment we treat PPI the same way that we treat
1150 * normal Radiotap encoded packets. The difference is in
1151 * the function that generates the code at the beginning
1152 * to compute the header length. Since this code generator
1153 * of PPI supports bare 802.11 encapsulation only (i.e.
1154 * the encapsulated DLT should be DLT_IEEE802_11) we
1155 * generate code to check for this too.
1157 off_linktype
.constant_part
= 24;
1158 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1159 off_linkpl
.is_variable
= 1;
1160 off_linkhdr
.is_variable
= 1;
1161 off_nl
= 8; /* 802.2+SNAP */
1162 off_nl_nosnap
= 3; /* 802.2 */
1165 case DLT_ATM_RFC1483
:
1166 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1168 * assume routed, non-ISO PDUs
1169 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1171 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1172 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1173 * latter would presumably be treated the way PPPoE
1174 * should be, so you can do "pppoe and udp port 2049"
1175 * or "pppoa and tcp port 80" and have it check for
1176 * PPPo{A,E} and a PPP protocol of IP and....
1178 off_linktype
.constant_part
= 0;
1179 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1180 off_nl
= 8; /* 802.2+SNAP */
1181 off_nl_nosnap
= 3; /* 802.2 */
1186 * Full Frontal ATM; you get AALn PDUs with an ATM
1190 off_vpi
= SUNATM_VPI_POS
;
1191 off_vci
= SUNATM_VCI_POS
;
1192 off_proto
= PROTO_POS
;
1193 off_payload
= SUNATM_PKT_BEGIN_POS
;
1194 off_linktype
.constant_part
= off_payload
;
1195 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1196 off_nl
= 8; /* 802.2+SNAP */
1197 off_nl_nosnap
= 3; /* 802.2 */
1203 off_linktype
.constant_part
= -1;
1204 off_linkpl
.constant_part
= 0;
1206 off_nl_nosnap
= 0; /* no 802.2 LLC */
1209 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1210 off_linktype
.constant_part
= 14;
1211 off_linkpl
.constant_part
= 16;
1213 off_nl_nosnap
= 0; /* no 802.2 LLC */
1218 * LocalTalk does have a 1-byte type field in the LLAP header,
1219 * but really it just indicates whether there is a "short" or
1220 * "long" DDP packet following.
1222 off_linktype
.constant_part
= -1;
1223 off_linkpl
.constant_part
= 0;
1225 off_nl_nosnap
= 0; /* no 802.2 LLC */
1228 case DLT_IP_OVER_FC
:
1230 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1231 * link-level type field. We set "off_linktype" to the
1232 * offset of the LLC header.
1234 * To check for Ethernet types, we assume that SSAP = SNAP
1235 * is being used and pick out the encapsulated Ethernet type.
1236 * XXX - should we generate code to check for SNAP? RFC
1237 * 2625 says SNAP should be used.
1239 off_linktype
.constant_part
= 16;
1240 off_linkpl
.constant_part
= 16;
1241 off_nl
= 8; /* 802.2+SNAP */
1242 off_nl_nosnap
= 3; /* 802.2 */
1247 * XXX - we should set this to handle SNAP-encapsulated
1248 * frames (NLPID of 0x80).
1250 off_linktype
.constant_part
= -1;
1251 off_linkpl
.constant_part
= 0;
1253 off_nl_nosnap
= 0; /* no 802.2 LLC */
1257 * the only BPF-interesting FRF.16 frames are non-control frames;
1258 * Frame Relay has a variable length link-layer
1259 * so lets start with offset 4 for now and increments later on (FIXME);
1262 off_linktype
.constant_part
= -1;
1263 off_linkpl
.constant_part
= 0;
1265 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1268 case DLT_APPLE_IP_OVER_IEEE1394
:
1269 off_linktype
.constant_part
= 16;
1270 off_linkpl
.constant_part
= 18;
1272 off_nl_nosnap
= 0; /* no 802.2 LLC */
1275 case DLT_SYMANTEC_FIREWALL
:
1276 off_linktype
.constant_part
= 6;
1277 off_linkpl
.constant_part
= 44;
1278 off_nl
= 0; /* Ethernet II */
1279 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1282 #ifdef HAVE_NET_PFVAR_H
1284 off_linktype
.constant_part
= 0;
1285 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1287 off_nl_nosnap
= 0; /* no 802.2 LLC */
1291 case DLT_JUNIPER_MFR
:
1292 case DLT_JUNIPER_MLFR
:
1293 case DLT_JUNIPER_MLPPP
:
1294 case DLT_JUNIPER_PPP
:
1295 case DLT_JUNIPER_CHDLC
:
1296 case DLT_JUNIPER_FRELAY
:
1297 off_linktype
.constant_part
= 4;
1298 off_linkpl
.constant_part
= 4;
1300 off_nl_nosnap
= -1; /* no 802.2 LLC */
1303 case DLT_JUNIPER_ATM1
:
1304 off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1305 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1310 case DLT_JUNIPER_ATM2
:
1311 off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1312 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1317 /* frames captured on a Juniper PPPoE service PIC
1318 * contain raw ethernet frames */
1319 case DLT_JUNIPER_PPPOE
:
1320 case DLT_JUNIPER_ETHER
:
1321 off_linkpl
.constant_part
= 14;
1322 off_linktype
.constant_part
= 16;
1323 off_nl
= 18; /* Ethernet II */
1324 off_nl_nosnap
= 21; /* 802.3+802.2 */
1327 case DLT_JUNIPER_PPPOE_ATM
:
1328 off_linktype
.constant_part
= 4;
1329 off_linkpl
.constant_part
= 6;
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_JUNIPER_GGSN
:
1335 off_linktype
.constant_part
= 6;
1336 off_linkpl
.constant_part
= 12;
1338 off_nl_nosnap
= -1; /* no 802.2 LLC */
1341 case DLT_JUNIPER_ES
:
1342 off_linktype
.constant_part
= 6;
1343 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1344 off_nl
= -1; /* not really a network layer but raw IP addresses */
1345 off_nl_nosnap
= -1; /* no 802.2 LLC */
1348 case DLT_JUNIPER_MONITOR
:
1349 off_linktype
.constant_part
= 12;
1350 off_linkpl
.constant_part
= 12;
1351 off_nl
= 0; /* raw IP/IP6 header */
1352 off_nl_nosnap
= -1; /* no 802.2 LLC */
1355 case DLT_BACNET_MS_TP
:
1356 off_linktype
.constant_part
= -1;
1357 off_linkpl
.constant_part
= -1;
1362 case DLT_JUNIPER_SERVICES
:
1363 off_linktype
.constant_part
= 12;
1364 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1365 off_nl
= -1; /* L3 proto location dep. on cookie type */
1366 off_nl_nosnap
= -1; /* no 802.2 LLC */
1369 case DLT_JUNIPER_VP
:
1370 off_linktype
.constant_part
= 18;
1371 off_linkpl
.constant_part
= -1;
1376 case DLT_JUNIPER_ST
:
1377 off_linktype
.constant_part
= 18;
1378 off_linkpl
.constant_part
= -1;
1383 case DLT_JUNIPER_ISM
:
1384 off_linktype
.constant_part
= 8;
1385 off_linkpl
.constant_part
= -1;
1390 case DLT_JUNIPER_VS
:
1391 case DLT_JUNIPER_SRX_E2E
:
1392 case DLT_JUNIPER_FIBRECHANNEL
:
1393 case DLT_JUNIPER_ATM_CEMIC
:
1394 off_linktype
.constant_part
= 8;
1395 off_linkpl
.constant_part
= -1;
1407 off_linktype
.constant_part
= -1;
1408 off_linkpl
.constant_part
= -1;
1413 case DLT_MTP2_WITH_PHDR
:
1420 off_linktype
.constant_part
= -1;
1421 off_linkpl
.constant_part
= -1;
1433 off_linktype
.constant_part
= -1;
1434 off_linkpl
.constant_part
= -1;
1440 off_linktype
.constant_part
= -1;
1441 off_linkpl
.constant_part
= 4;
1448 * Currently, only raw "link[N:M]" filtering is supported.
1450 off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1451 off_linkpl
.constant_part
= -1;
1452 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1453 off_nl_nosnap
= -1; /* no 802.2 LLC */
1457 off_linktype
.constant_part
= 1;
1458 off_linkpl
.constant_part
= 24; /* ipnet header length */
1463 case DLT_NETANALYZER
:
1464 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1465 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1466 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1467 off_nl
= 0; /* Ethernet II */
1468 off_nl_nosnap
= 3; /* 802.3+802.2 */
1471 case DLT_NETANALYZER_TRANSPARENT
:
1472 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1473 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1474 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1475 off_nl
= 0; /* Ethernet II */
1476 off_nl_nosnap
= 3; /* 802.3+802.2 */
1481 * For values in the range in which we've assigned new
1482 * DLT_ values, only raw "link[N:M]" filtering is supported.
1484 if (linktype
>= DLT_MATCHING_MIN
&&
1485 linktype
<= DLT_MATCHING_MAX
) {
1486 off_linktype
.constant_part
= -1;
1487 off_linkpl
.constant_part
= -1;
1491 bpf_error("unknown data link type %d", linktype
);
1496 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1500 * Load a value relative to the specified absolute offset.
1502 static struct slist
*
1503 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1505 struct slist
*s
, *s2
;
1507 s
= gen_abs_offset_varpart(abs_offset
);
1510 * If "s" is non-null, it has code to arrange that the X register
1511 * contains the variable part of the absolute offset, so we
1512 * generate a load relative to that, with an offset of
1513 * abs_offset->constant_part + offset.
1515 * Otherwise, we can do an absolute load with an offset of
1516 * abs_offset->constant_part + offset.
1520 * "s" points to a list of statements that puts the
1521 * variable part of the absolute offset into the X register.
1522 * Do an indirect load, to use the X register as an offset.
1524 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1525 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1529 * There is no variable part of the absolute offset, so
1530 * just do an absolute load.
1532 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1533 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1539 * Load a value relative to the beginning of the specified header.
1541 static struct slist
*
1542 gen_load_a(offrel
, offset
, size
)
1543 enum e_offrel offrel
;
1546 struct slist
*s
, *s2
;
1551 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1556 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1559 case OR_PREVLINKHDR
:
1560 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1564 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1567 case OR_PREVMPLSHDR
:
1568 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1572 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1575 case OR_LINKPL_NOSNAP
:
1576 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1580 s
= gen_load_absoffsetrel(&off_linktype
, offset
, size
);
1585 * Load the X register with the length of the IPv4 header
1586 * (plus the offset of the link-layer header, if it's
1587 * preceded by a variable-length header such as a radio
1588 * header), in bytes.
1590 s
= gen_loadx_iphdrlen();
1593 * Load the item at {offset of the link-layer payload} +
1594 * {offset, relative to the start of the link-layer
1595 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1596 * {specified offset}.
1598 * If the offset of the link-layer payload is variable,
1599 * the variable part of that offset is included in the
1600 * value in the X register, and we include the constant
1601 * part in the offset of the load.
1603 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1604 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1609 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1620 * Generate code to load into the X register the sum of the length of
1621 * the IPv4 header and the variable part of the offset of the link-layer
1624 static struct slist
*
1625 gen_loadx_iphdrlen()
1627 struct slist
*s
, *s2
;
1629 s
= gen_abs_offset_varpart(&off_linkpl
);
1632 * The offset of the link-layer payload has a variable
1633 * part. "s" points to a list of statements that put
1634 * the variable part of that offset into the X register.
1636 * The 4*([k]&0xf) addressing mode can't be used, as we
1637 * don't have a constant offset, so we have to load the
1638 * value in question into the A register and add to it
1639 * the value from the X register.
1641 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1642 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1644 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1647 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1652 * The A register now contains the length of the IP header.
1653 * We need to add to it the variable part of the offset of
1654 * the link-layer payload, which is still in the X
1655 * register, and move the result into the X register.
1657 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1658 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1661 * The offset of the link-layer payload is a constant,
1662 * so no code was generated to load the (non-existent)
1663 * variable part of that offset.
1665 * This means we can use the 4*([k]&0xf) addressing
1666 * mode. Load the length of the IPv4 header, which
1667 * is at an offset of off_nl from the beginning of
1668 * the link-layer payload, and thus at an offset of
1669 * off_linkpl.constant_part + off_nl from the beginning
1670 * of the raw packet data, using that addressing mode.
1672 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1673 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1678 static struct block
*
1685 s
= new_stmt(BPF_LD
|BPF_IMM
);
1687 b
= new_block(JMP(BPF_JEQ
));
1693 static inline struct block
*
1696 return gen_uncond(1);
1699 static inline struct block
*
1702 return gen_uncond(0);
1706 * Byte-swap a 32-bit number.
1707 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1708 * big-endian platforms.)
1710 #define SWAPLONG(y) \
1711 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1714 * Generate code to match a particular packet type.
1716 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1717 * value, if <= ETHERMTU. We use that to determine whether to
1718 * match the type/length field or to check the type/length field for
1719 * a value <= ETHERMTU to see whether it's a type field and then do
1720 * the appropriate test.
1722 static struct block
*
1723 gen_ether_linktype(proto
)
1726 struct block
*b0
, *b1
;
1732 case LLCSAP_NETBEUI
:
1734 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1735 * so we check the DSAP and SSAP.
1737 * LLCSAP_IP checks for IP-over-802.2, rather
1738 * than IP-over-Ethernet or IP-over-SNAP.
1740 * XXX - should we check both the DSAP and the
1741 * SSAP, like this, or should we check just the
1742 * DSAP, as we do for other types <= ETHERMTU
1743 * (i.e., other SAP values)?
1745 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1747 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1748 ((proto
<< 8) | proto
));
1756 * Ethernet_II frames, which are Ethernet
1757 * frames with a frame type of ETHERTYPE_IPX;
1759 * Ethernet_802.3 frames, which are 802.3
1760 * frames (i.e., the type/length field is
1761 * a length field, <= ETHERMTU, rather than
1762 * a type field) with the first two bytes
1763 * after the Ethernet/802.3 header being
1766 * Ethernet_802.2 frames, which are 802.3
1767 * frames with an 802.2 LLC header and
1768 * with the IPX LSAP as the DSAP in the LLC
1771 * Ethernet_SNAP frames, which are 802.3
1772 * frames with an LLC header and a SNAP
1773 * header and with an OUI of 0x000000
1774 * (encapsulated Ethernet) and a protocol
1775 * ID of ETHERTYPE_IPX in the SNAP header.
1777 * XXX - should we generate the same code both
1778 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1782 * This generates code to check both for the
1783 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1785 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1786 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1790 * Now we add code to check for SNAP frames with
1791 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1793 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1797 * Now we generate code to check for 802.3
1798 * frames in general.
1800 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1804 * Now add the check for 802.3 frames before the
1805 * check for Ethernet_802.2 and Ethernet_802.3,
1806 * as those checks should only be done on 802.3
1807 * frames, not on Ethernet frames.
1812 * Now add the check for Ethernet_II frames, and
1813 * do that before checking for the other frame
1816 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1820 case ETHERTYPE_ATALK
:
1821 case ETHERTYPE_AARP
:
1823 * EtherTalk (AppleTalk protocols on Ethernet link
1824 * layer) may use 802.2 encapsulation.
1828 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1829 * we check for an Ethernet type field less than
1830 * 1500, which means it's an 802.3 length field.
1832 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1836 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1837 * SNAP packets with an organization code of
1838 * 0x080007 (Apple, for Appletalk) and a protocol
1839 * type of ETHERTYPE_ATALK (Appletalk).
1841 * 802.2-encapsulated ETHERTYPE_AARP packets are
1842 * SNAP packets with an organization code of
1843 * 0x000000 (encapsulated Ethernet) and a protocol
1844 * type of ETHERTYPE_AARP (Appletalk ARP).
1846 if (proto
== ETHERTYPE_ATALK
)
1847 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1848 else /* proto == ETHERTYPE_AARP */
1849 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1853 * Check for Ethernet encapsulation (Ethertalk
1854 * phase 1?); we just check for the Ethernet
1857 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1863 if (proto
<= ETHERMTU
) {
1865 * This is an LLC SAP value, so the frames
1866 * that match would be 802.2 frames.
1867 * Check that the frame is an 802.2 frame
1868 * (i.e., that the length/type field is
1869 * a length field, <= ETHERMTU) and
1870 * then check the DSAP.
1872 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1874 b1
= gen_cmp(OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1879 * This is an Ethernet type, so compare
1880 * the length/type field with it (if
1881 * the frame is an 802.2 frame, the length
1882 * field will be <= ETHERMTU, and, as
1883 * "proto" is > ETHERMTU, this test
1884 * will fail and the frame won't match,
1885 * which is what we want).
1887 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
1894 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1895 * or IPv6 then we have an error.
1897 static struct block
*
1898 gen_ipnet_linktype(proto
)
1904 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
1907 case ETHERTYPE_IPV6
:
1908 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
1909 (bpf_int32
)IPH_AF_INET6
);
1920 * Generate code to match a particular packet type.
1922 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1923 * value, if <= ETHERMTU. We use that to determine whether to
1924 * match the type field or to check the type field for the special
1925 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1927 static struct block
*
1928 gen_linux_sll_linktype(proto
)
1931 struct block
*b0
, *b1
;
1937 case LLCSAP_NETBEUI
:
1939 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1940 * so we check the DSAP and SSAP.
1942 * LLCSAP_IP checks for IP-over-802.2, rather
1943 * than IP-over-Ethernet or IP-over-SNAP.
1945 * XXX - should we check both the DSAP and the
1946 * SSAP, like this, or should we check just the
1947 * DSAP, as we do for other types <= ETHERMTU
1948 * (i.e., other SAP values)?
1950 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
1951 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1952 ((proto
<< 8) | proto
));
1958 * Ethernet_II frames, which are Ethernet
1959 * frames with a frame type of ETHERTYPE_IPX;
1961 * Ethernet_802.3 frames, which have a frame
1962 * type of LINUX_SLL_P_802_3;
1964 * Ethernet_802.2 frames, which are 802.3
1965 * frames with an 802.2 LLC header (i.e, have
1966 * a frame type of LINUX_SLL_P_802_2) and
1967 * with the IPX LSAP as the DSAP in the LLC
1970 * Ethernet_SNAP frames, which are 802.3
1971 * frames with an LLC header and a SNAP
1972 * header and with an OUI of 0x000000
1973 * (encapsulated Ethernet) and a protocol
1974 * ID of ETHERTYPE_IPX in the SNAP header.
1976 * First, do the checks on LINUX_SLL_P_802_2
1977 * frames; generate the check for either
1978 * Ethernet_802.2 or Ethernet_SNAP frames, and
1979 * then put a check for LINUX_SLL_P_802_2 frames
1982 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1983 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1985 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
1989 * Now check for 802.3 frames and OR that with
1990 * the previous test.
1992 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
1996 * Now add the check for Ethernet_II frames, and
1997 * do that before checking for the other frame
2000 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2004 case ETHERTYPE_ATALK
:
2005 case ETHERTYPE_AARP
:
2007 * EtherTalk (AppleTalk protocols on Ethernet link
2008 * layer) may use 802.2 encapsulation.
2012 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2013 * we check for the 802.2 protocol type in the
2014 * "Ethernet type" field.
2016 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2019 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2020 * SNAP packets with an organization code of
2021 * 0x080007 (Apple, for Appletalk) and a protocol
2022 * type of ETHERTYPE_ATALK (Appletalk).
2024 * 802.2-encapsulated ETHERTYPE_AARP packets are
2025 * SNAP packets with an organization code of
2026 * 0x000000 (encapsulated Ethernet) and a protocol
2027 * type of ETHERTYPE_AARP (Appletalk ARP).
2029 if (proto
== ETHERTYPE_ATALK
)
2030 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2031 else /* proto == ETHERTYPE_AARP */
2032 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2036 * Check for Ethernet encapsulation (Ethertalk
2037 * phase 1?); we just check for the Ethernet
2040 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2046 if (proto
<= ETHERMTU
) {
2048 * This is an LLC SAP value, so the frames
2049 * that match would be 802.2 frames.
2050 * Check for the 802.2 protocol type
2051 * in the "Ethernet type" field, and
2052 * then check the DSAP.
2054 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2055 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2061 * This is an Ethernet type, so compare
2062 * the length/type field with it (if
2063 * the frame is an 802.2 frame, the length
2064 * field will be <= ETHERMTU, and, as
2065 * "proto" is > ETHERMTU, this test
2066 * will fail and the frame won't match,
2067 * which is what we want).
2069 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2074 static struct slist
*
2075 gen_load_prism_llprefixlen()
2077 struct slist
*s1
, *s2
;
2078 struct slist
*sjeq_avs_cookie
;
2079 struct slist
*sjcommon
;
2082 * This code is not compatible with the optimizer, as
2083 * we are generating jmp instructions within a normal
2084 * slist of instructions
2089 * Generate code to load the length of the radio header into
2090 * the register assigned to hold that length, if one has been
2091 * assigned. (If one hasn't been assigned, no code we've
2092 * generated uses that prefix, so we don't need to generate any
2095 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2096 * or always use the AVS header rather than the Prism header.
2097 * We load a 4-byte big-endian value at the beginning of the
2098 * raw packet data, and see whether, when masked with 0xFFFFF000,
2099 * it's equal to 0x80211000. If so, that indicates that it's
2100 * an AVS header (the masked-out bits are the version number).
2101 * Otherwise, it's a Prism header.
2103 * XXX - the Prism header is also, in theory, variable-length,
2104 * but no known software generates headers that aren't 144
2107 if (off_linkhdr
.reg
!= -1) {
2111 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2115 * AND it with 0xFFFFF000.
2117 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2118 s2
->s
.k
= 0xFFFFF000;
2122 * Compare with 0x80211000.
2124 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2125 sjeq_avs_cookie
->s
.k
= 0x80211000;
2126 sappend(s1
, sjeq_avs_cookie
);
2131 * The 4 bytes at an offset of 4 from the beginning of
2132 * the AVS header are the length of the AVS header.
2133 * That field is big-endian.
2135 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2138 sjeq_avs_cookie
->s
.jt
= s2
;
2141 * Now jump to the code to allocate a register
2142 * into which to save the header length and
2143 * store the length there. (The "jump always"
2144 * instruction needs to have the k field set;
2145 * it's added to the PC, so, as we're jumping
2146 * over a single instruction, it should be 1.)
2148 sjcommon
= new_stmt(JMP(BPF_JA
));
2150 sappend(s1
, sjcommon
);
2153 * Now for the code that handles the Prism header.
2154 * Just load the length of the Prism header (144)
2155 * into the A register. Have the test for an AVS
2156 * header branch here if we don't have an AVS header.
2158 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2161 sjeq_avs_cookie
->s
.jf
= s2
;
2164 * Now allocate a register to hold that value and store
2165 * it. The code for the AVS header will jump here after
2166 * loading the length of the AVS header.
2168 s2
= new_stmt(BPF_ST
);
2169 s2
->s
.k
= off_linkhdr
.reg
;
2171 sjcommon
->s
.jf
= s2
;
2174 * Now move it into the X register.
2176 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2184 static struct slist
*
2185 gen_load_avs_llprefixlen()
2187 struct slist
*s1
, *s2
;
2190 * Generate code to load the length of the AVS header into
2191 * the register assigned to hold that length, if one has been
2192 * assigned. (If one hasn't been assigned, no code we've
2193 * generated uses that prefix, so we don't need to generate any
2196 if (off_linkhdr
.reg
!= -1) {
2198 * The 4 bytes at an offset of 4 from the beginning of
2199 * the AVS header are the length of the AVS header.
2200 * That field is big-endian.
2202 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2206 * Now allocate a register to hold that value and store
2209 s2
= new_stmt(BPF_ST
);
2210 s2
->s
.k
= off_linkhdr
.reg
;
2214 * Now move it into the X register.
2216 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2224 static struct slist
*
2225 gen_load_radiotap_llprefixlen()
2227 struct slist
*s1
, *s2
;
2230 * Generate code to load the length of the radiotap header into
2231 * the register assigned to hold that length, if one has been
2232 * assigned. (If one hasn't been assigned, no code we've
2233 * generated uses that prefix, so we don't need to generate any
2236 if (off_linkhdr
.reg
!= -1) {
2238 * The 2 bytes at offsets of 2 and 3 from the beginning
2239 * of the radiotap header are the length of the radiotap
2240 * header; unfortunately, it's little-endian, so we have
2241 * to load it a byte at a time and construct the value.
2245 * Load the high-order byte, at an offset of 3, shift it
2246 * left a byte, and put the result in the X register.
2248 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2250 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2253 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2257 * Load the next byte, at an offset of 2, and OR the
2258 * value from the X register into it.
2260 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2263 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2267 * Now allocate a register to hold that value and store
2270 s2
= new_stmt(BPF_ST
);
2271 s2
->s
.k
= off_linkhdr
.reg
;
2275 * Now move it into the X register.
2277 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2286 * At the moment we treat PPI as normal Radiotap encoded
2287 * packets. The difference is in the function that generates
2288 * the code at the beginning to compute the header length.
2289 * Since this code generator of PPI supports bare 802.11
2290 * encapsulation only (i.e. the encapsulated DLT should be
2291 * DLT_IEEE802_11) we generate code to check for this too;
2292 * that's done in finish_parse().
2294 static struct slist
*
2295 gen_load_ppi_llprefixlen()
2297 struct slist
*s1
, *s2
;
2300 * Generate code to load the length of the radiotap header
2301 * into the register assigned to hold that length, if one has
2304 if (off_linkhdr
.reg
!= -1) {
2306 * The 2 bytes at offsets of 2 and 3 from the beginning
2307 * of the radiotap header are the length of the radiotap
2308 * header; unfortunately, it's little-endian, so we have
2309 * to load it a byte at a time and construct the value.
2313 * Load the high-order byte, at an offset of 3, shift it
2314 * left a byte, and put the result in the X register.
2316 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2318 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2321 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2325 * Load the next byte, at an offset of 2, and OR the
2326 * value from the X register into it.
2328 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2331 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2335 * Now allocate a register to hold that value and store
2338 s2
= new_stmt(BPF_ST
);
2339 s2
->s
.k
= off_linkhdr
.reg
;
2343 * Now move it into the X register.
2345 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2354 * Load a value relative to the beginning of the link-layer header after the 802.11
2355 * header, i.e. LLC_SNAP.
2356 * The link-layer header doesn't necessarily begin at the beginning
2357 * of the packet data; there might be a variable-length prefix containing
2358 * radio information.
2360 static struct slist
*
2361 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2364 struct slist
*sjset_data_frame_1
;
2365 struct slist
*sjset_data_frame_2
;
2366 struct slist
*sjset_qos
;
2367 struct slist
*sjset_radiotap_flags
;
2368 struct slist
*sjset_radiotap_tsft
;
2369 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2370 struct slist
*s_roundup
;
2372 if (off_linkpl
.reg
== -1) {
2374 * No register has been assigned to the offset of
2375 * the link-layer payload, which means nobody needs
2376 * it; don't bother computing it - just return
2377 * what we already have.
2383 * This code is not compatible with the optimizer, as
2384 * we are generating jmp instructions within a normal
2385 * slist of instructions
2390 * If "s" is non-null, it has code to arrange that the X register
2391 * contains the length of the prefix preceding the link-layer
2394 * Otherwise, the length of the prefix preceding the link-layer
2395 * header is "off_outermostlinkhdr.constant_part".
2399 * There is no variable-length header preceding the
2400 * link-layer header.
2402 * Load the length of the fixed-length prefix preceding
2403 * the link-layer header (if any) into the X register,
2404 * and store it in the off_linkpl.reg register.
2405 * That length is off_outermostlinkhdr.constant_part.
2407 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2408 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2412 * The X register contains the offset of the beginning of the
2413 * link-layer header; add 24, which is the minimum length
2414 * of the MAC header for a data frame, to that, and store it
2415 * in off_linkpl.reg, and then load the Frame Control field,
2416 * which is at the offset in the X register, with an indexed load.
2418 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2420 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2423 s2
= new_stmt(BPF_ST
);
2424 s2
->s
.k
= off_linkpl
.reg
;
2427 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2432 * Check the Frame Control field to see if this is a data frame;
2433 * a data frame has the 0x08 bit (b3) in that field set and the
2434 * 0x04 bit (b2) clear.
2436 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2437 sjset_data_frame_1
->s
.k
= 0x08;
2438 sappend(s
, sjset_data_frame_1
);
2441 * If b3 is set, test b2, otherwise go to the first statement of
2442 * the rest of the program.
2444 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2445 sjset_data_frame_2
->s
.k
= 0x04;
2446 sappend(s
, sjset_data_frame_2
);
2447 sjset_data_frame_1
->s
.jf
= snext
;
2450 * If b2 is not set, this is a data frame; test the QoS bit.
2451 * Otherwise, go to the first statement of the rest of the
2454 sjset_data_frame_2
->s
.jt
= snext
;
2455 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2456 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2457 sappend(s
, sjset_qos
);
2460 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2462 * Otherwise, go to the first statement of the rest of the
2465 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2466 s2
->s
.k
= off_linkpl
.reg
;
2468 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2471 s2
= new_stmt(BPF_ST
);
2472 s2
->s
.k
= off_linkpl
.reg
;
2476 * If we have a radiotap header, look at it to see whether
2477 * there's Atheros padding between the MAC-layer header
2480 * Note: all of the fields in the radiotap header are
2481 * little-endian, so we byte-swap all of the values
2482 * we test against, as they will be loaded as big-endian
2485 if (linktype
== DLT_IEEE802_11_RADIO
) {
2487 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2488 * in the presence flag?
2490 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2494 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2495 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2496 sappend(s
, sjset_radiotap_flags
);
2499 * If not, skip all of this.
2501 sjset_radiotap_flags
->s
.jf
= snext
;
2504 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2506 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2507 new_stmt(JMP(BPF_JSET
));
2508 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2509 sappend(s
, sjset_radiotap_tsft
);
2512 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2513 * at an offset of 16 from the beginning of the raw packet
2514 * data (8 bytes for the radiotap header and 8 bytes for
2517 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2520 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2524 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2525 sjset_tsft_datapad
->s
.k
= 0x20;
2526 sappend(s
, sjset_tsft_datapad
);
2529 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2530 * at an offset of 8 from the beginning of the raw packet
2531 * data (8 bytes for the radiotap header).
2533 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2536 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2540 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2541 sjset_notsft_datapad
->s
.k
= 0x20;
2542 sappend(s
, sjset_notsft_datapad
);
2545 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2546 * set, round the length of the 802.11 header to
2547 * a multiple of 4. Do that by adding 3 and then
2548 * dividing by and multiplying by 4, which we do by
2551 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2552 s_roundup
->s
.k
= off_linkpl
.reg
;
2553 sappend(s
, s_roundup
);
2554 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2557 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2560 s2
= new_stmt(BPF_ST
);
2561 s2
->s
.k
= off_linkpl
.reg
;
2564 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2565 sjset_tsft_datapad
->s
.jf
= snext
;
2566 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2567 sjset_notsft_datapad
->s
.jf
= snext
;
2569 sjset_qos
->s
.jf
= snext
;
2575 insert_compute_vloffsets(b
)
2580 /* There is an implicit dependency between the link
2581 * payload and link header since the payload computation
2582 * includes the variable part of the header. Therefore,
2583 * if nobody else has allocated a register for the link
2584 * header and we need it, do it now. */
2585 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2586 off_linkhdr
.reg
== -1)
2587 off_linkhdr
.reg
= alloc_reg();
2590 * For link-layer types that have a variable-length header
2591 * preceding the link-layer header, generate code to load
2592 * the offset of the link-layer header into the register
2593 * assigned to that offset, if any.
2595 * XXX - this, and the next switch statement, won't handle
2596 * encapsulation of 802.11 or 802.11+radio information in
2597 * some other protocol stack. That's significantly more
2600 switch (outermostlinktype
) {
2602 case DLT_PRISM_HEADER
:
2603 s
= gen_load_prism_llprefixlen();
2606 case DLT_IEEE802_11_RADIO_AVS
:
2607 s
= gen_load_avs_llprefixlen();
2610 case DLT_IEEE802_11_RADIO
:
2611 s
= gen_load_radiotap_llprefixlen();
2615 s
= gen_load_ppi_llprefixlen();
2624 * For link-layer types that have a variable-length link-layer
2625 * header, generate code to load the offset of the link-layer
2626 * payload into the register assigned to that offset, if any.
2628 switch (outermostlinktype
) {
2630 case DLT_IEEE802_11
:
2631 case DLT_PRISM_HEADER
:
2632 case DLT_IEEE802_11_RADIO_AVS
:
2633 case DLT_IEEE802_11_RADIO
:
2635 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2640 * If we have any offset-loading code, append all the
2641 * existing statements in the block to those statements,
2642 * and make the resulting list the list of statements
2646 sappend(s
, b
->stmts
);
2651 static struct block
*
2652 gen_ppi_dlt_check(void)
2654 struct slist
*s_load_dlt
;
2657 if (linktype
== DLT_PPI
)
2659 /* Create the statements that check for the DLT
2661 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2662 s_load_dlt
->s
.k
= 4;
2664 b
= new_block(JMP(BPF_JEQ
));
2666 b
->stmts
= s_load_dlt
;
2667 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2678 * Take an absolute offset, and:
2680 * if it has no variable part, return NULL;
2682 * if it has a variable part, generate code to load the register
2683 * containing that variable part into the X register, returning
2684 * a pointer to that code - if no register for that offset has
2685 * been allocated, allocate it first.
2687 * (The code to set that register will be generated later, but will
2688 * be placed earlier in the code sequence.)
2690 static struct slist
*
2691 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2695 if (off
->is_variable
) {
2696 if (off
->reg
== -1) {
2698 * We haven't yet assigned a register for the
2699 * variable part of the offset of the link-layer
2700 * header; allocate one.
2702 off
->reg
= alloc_reg();
2706 * Load the register containing the variable part of the
2707 * offset of the link-layer header into the X register.
2709 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2714 * That offset isn't variable, there's no variable part,
2715 * so we don't need to generate any code.
2722 * Map an Ethernet type to the equivalent PPP type.
2725 ethertype_to_ppptype(proto
)
2734 case ETHERTYPE_IPV6
:
2742 case ETHERTYPE_ATALK
:
2756 * I'm assuming the "Bridging PDU"s that go
2757 * over PPP are Spanning Tree Protocol
2771 * Generate any tests that, for encapsulation of a link-layer packet
2772 * inside another protocol stack, need to be done to check for those
2773 * link-layer packets (and that haven't already been done by a check
2774 * for that encapsulation).
2776 static struct block
*
2777 gen_prevlinkhdr_check(void)
2781 switch (prevlinktype
) {
2785 * This is LANE-encapsulated Ethernet; check that the LANE
2786 * packet doesn't begin with an LE Control marker, i.e.
2787 * that it's data, not a control message.
2789 * (We've already generated a test for LANE.)
2791 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2797 * No such tests are necessary.
2805 * Generate code to match a particular packet type by matching the
2806 * link-layer type field or fields in the 802.2 LLC header.
2808 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2809 * value, if <= ETHERMTU.
2811 static struct block
*
2815 struct block
*b0
, *b1
, *b2
;
2816 const char *description
;
2818 /* are we checking MPLS-encapsulated packets? */
2819 if (label_stack_depth
> 0) {
2823 /* FIXME add other L3 proto IDs */
2824 return gen_mpls_linktype(Q_IP
);
2826 case ETHERTYPE_IPV6
:
2828 /* FIXME add other L3 proto IDs */
2829 return gen_mpls_linktype(Q_IPV6
);
2832 bpf_error("unsupported protocol over mpls");
2840 case DLT_NETANALYZER
:
2841 case DLT_NETANALYZER_TRANSPARENT
:
2842 b0
= gen_prevlinkhdr_check();
2843 b1
= gen_ether_linktype(proto
);
2854 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2858 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2864 case DLT_IEEE802_11
:
2865 case DLT_PRISM_HEADER
:
2866 case DLT_IEEE802_11_RADIO_AVS
:
2867 case DLT_IEEE802_11_RADIO
:
2870 * Check that we have a data frame.
2872 b0
= gen_check_802_11_data_frame();
2875 * Now check for the specified link-layer type.
2877 b1
= gen_llc_linktype(proto
);
2885 * XXX - check for LLC frames.
2887 return gen_llc_linktype(proto
);
2893 * XXX - check for LLC PDUs, as per IEEE 802.5.
2895 return gen_llc_linktype(proto
);
2899 case DLT_ATM_RFC1483
:
2901 case DLT_IP_OVER_FC
:
2902 return gen_llc_linktype(proto
);
2908 * Check for an LLC-encapsulated version of this protocol;
2909 * if we were checking for LANE, linktype would no longer
2912 * Check for LLC encapsulation and then check the protocol.
2914 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2915 b1
= gen_llc_linktype(proto
);
2922 return gen_linux_sll_linktype(proto
);
2927 case DLT_SLIP_BSDOS
:
2930 * These types don't provide any type field; packets
2931 * are always IPv4 or IPv6.
2933 * XXX - for IPv4, check for a version number of 4, and,
2934 * for IPv6, check for a version number of 6?
2939 /* Check for a version number of 4. */
2940 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
2942 case ETHERTYPE_IPV6
:
2943 /* Check for a version number of 6. */
2944 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
2947 return gen_false(); /* always false */
2954 * Raw IPv4, so no type field.
2956 if (proto
== ETHERTYPE_IP
)
2957 return gen_true(); /* always true */
2959 /* Checking for something other than IPv4; always false */
2966 * Raw IPv6, so no type field.
2968 if (proto
== ETHERTYPE_IPV6
)
2969 return gen_true(); /* always true */
2971 /* Checking for something other than IPv6; always false */
2978 case DLT_PPP_SERIAL
:
2981 * We use Ethernet protocol types inside libpcap;
2982 * map them to the corresponding PPP protocol types.
2984 proto
= ethertype_to_ppptype(proto
);
2985 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2991 * We use Ethernet protocol types inside libpcap;
2992 * map them to the corresponding PPP protocol types.
2998 * Also check for Van Jacobson-compressed IP.
2999 * XXX - do this for other forms of PPP?
3001 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3002 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3004 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3009 proto
= ethertype_to_ppptype(proto
);
3010 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3020 * For DLT_NULL, the link-layer header is a 32-bit
3021 * word containing an AF_ value in *host* byte order,
3022 * and for DLT_ENC, the link-layer header begins
3023 * with a 32-bit work containing an AF_ value in
3026 * In addition, if we're reading a saved capture file,
3027 * the host byte order in the capture may not be the
3028 * same as the host byte order on this machine.
3030 * For DLT_LOOP, the link-layer header is a 32-bit
3031 * word containing an AF_ value in *network* byte order.
3033 * XXX - AF_ values may, unfortunately, be platform-
3034 * dependent; for example, FreeBSD's AF_INET6 is 24
3035 * whilst NetBSD's and OpenBSD's is 26.
3037 * This means that, when reading a capture file, just
3038 * checking for our AF_INET6 value won't work if the
3039 * capture file came from another OS.
3048 case ETHERTYPE_IPV6
:
3055 * Not a type on which we support filtering.
3056 * XXX - support those that have AF_ values
3057 * #defined on this platform, at least?
3062 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3064 * The AF_ value is in host byte order, but
3065 * the BPF interpreter will convert it to
3066 * network byte order.
3068 * If this is a save file, and it's from a
3069 * machine with the opposite byte order to
3070 * ours, we byte-swap the AF_ value.
3072 * Then we run it through "htonl()", and
3073 * generate code to compare against the result.
3075 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3076 proto
= SWAPLONG(proto
);
3077 proto
= htonl(proto
);
3079 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3081 #ifdef HAVE_NET_PFVAR_H
3084 * af field is host byte order in contrast to the rest of
3087 if (proto
== ETHERTYPE_IP
)
3088 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3089 BPF_B
, (bpf_int32
)AF_INET
));
3090 else if (proto
== ETHERTYPE_IPV6
)
3091 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3092 BPF_B
, (bpf_int32
)AF_INET6
));
3097 #endif /* HAVE_NET_PFVAR_H */
3100 case DLT_ARCNET_LINUX
:
3102 * XXX should we check for first fragment if the protocol
3110 case ETHERTYPE_IPV6
:
3111 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3112 (bpf_int32
)ARCTYPE_INET6
));
3115 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3116 (bpf_int32
)ARCTYPE_IP
);
3117 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3118 (bpf_int32
)ARCTYPE_IP_OLD
);
3123 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3124 (bpf_int32
)ARCTYPE_ARP
);
3125 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3126 (bpf_int32
)ARCTYPE_ARP_OLD
);
3130 case ETHERTYPE_REVARP
:
3131 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3132 (bpf_int32
)ARCTYPE_REVARP
));
3134 case ETHERTYPE_ATALK
:
3135 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3136 (bpf_int32
)ARCTYPE_ATALK
));
3143 case ETHERTYPE_ATALK
:
3153 * XXX - assumes a 2-byte Frame Relay header with
3154 * DLCI and flags. What if the address is longer?
3160 * Check for the special NLPID for IP.
3162 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3164 case ETHERTYPE_IPV6
:
3166 * Check for the special NLPID for IPv6.
3168 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3172 * Check for several OSI protocols.
3174 * Frame Relay packets typically have an OSI
3175 * NLPID at the beginning; we check for each
3178 * What we check for is the NLPID and a frame
3179 * control field of UI, i.e. 0x03 followed
3182 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3183 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3184 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3196 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3198 case DLT_JUNIPER_MFR
:
3199 case DLT_JUNIPER_MLFR
:
3200 case DLT_JUNIPER_MLPPP
:
3201 case DLT_JUNIPER_ATM1
:
3202 case DLT_JUNIPER_ATM2
:
3203 case DLT_JUNIPER_PPPOE
:
3204 case DLT_JUNIPER_PPPOE_ATM
:
3205 case DLT_JUNIPER_GGSN
:
3206 case DLT_JUNIPER_ES
:
3207 case DLT_JUNIPER_MONITOR
:
3208 case DLT_JUNIPER_SERVICES
:
3209 case DLT_JUNIPER_ETHER
:
3210 case DLT_JUNIPER_PPP
:
3211 case DLT_JUNIPER_FRELAY
:
3212 case DLT_JUNIPER_CHDLC
:
3213 case DLT_JUNIPER_VP
:
3214 case DLT_JUNIPER_ST
:
3215 case DLT_JUNIPER_ISM
:
3216 case DLT_JUNIPER_VS
:
3217 case DLT_JUNIPER_SRX_E2E
:
3218 case DLT_JUNIPER_FIBRECHANNEL
:
3219 case DLT_JUNIPER_ATM_CEMIC
:
3221 /* just lets verify the magic number for now -
3222 * on ATM we may have up to 6 different encapsulations on the wire
3223 * and need a lot of heuristics to figure out that the payload
3226 * FIXME encapsulation specific BPF_ filters
3228 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3230 case DLT_BACNET_MS_TP
:
3231 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3234 return gen_ipnet_linktype(proto
);
3236 case DLT_LINUX_IRDA
:
3237 bpf_error("IrDA link-layer type filtering not implemented");
3240 bpf_error("DOCSIS link-layer type filtering not implemented");
3243 case DLT_MTP2_WITH_PHDR
:
3244 bpf_error("MTP2 link-layer type filtering not implemented");
3247 bpf_error("ERF link-layer type filtering not implemented");
3250 bpf_error("PFSYNC link-layer type filtering not implemented");
3252 case DLT_LINUX_LAPD
:
3253 bpf_error("LAPD link-layer type filtering not implemented");
3257 case DLT_USB_LINUX_MMAPPED
:
3258 bpf_error("USB link-layer type filtering not implemented");
3260 case DLT_BLUETOOTH_HCI_H4
:
3261 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3262 bpf_error("Bluetooth link-layer type filtering not implemented");
3265 case DLT_CAN_SOCKETCAN
:
3266 bpf_error("CAN link-layer type filtering not implemented");
3268 case DLT_IEEE802_15_4
:
3269 case DLT_IEEE802_15_4_LINUX
:
3270 case DLT_IEEE802_15_4_NONASK_PHY
:
3271 case DLT_IEEE802_15_4_NOFCS
:
3272 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3274 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3275 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3278 bpf_error("SITA link-layer type filtering not implemented");
3281 bpf_error("RAIF1 link-layer type filtering not implemented");
3284 bpf_error("IPMB link-layer type filtering not implemented");
3287 bpf_error("AX.25 link-layer type filtering not implemented");
3290 /* Using the fixed-size NFLOG header it is possible to tell only
3291 * the address family of the packet, other meaningful data is
3292 * either missing or behind TLVs.
3294 bpf_error("NFLOG link-layer type filtering not implemented");
3298 * Does this link-layer header type have a field
3299 * indicating the type of the next protocol? If
3300 * so, off_linktype.constant_part will be the offset of that
3301 * field in the packet; if not, it will be -1.
3303 if (off_linktype
.constant_part
!= (u_int
)-1) {
3305 * Yes; assume it's an Ethernet type. (If
3306 * it's not, it needs to be handled specially
3309 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3312 * No; report an error.
3314 description
= pcap_datalink_val_to_description(linktype
);
3315 if (description
!= NULL
) {
3316 bpf_error("%s link-layer type filtering not implemented",
3319 bpf_error("DLT %u link-layer type filtering not implemented",
3328 * Check for an LLC SNAP packet with a given organization code and
3329 * protocol type; we check the entire contents of the 802.2 LLC and
3330 * snap headers, checking for DSAP and SSAP of SNAP and a control
3331 * field of 0x03 in the LLC header, and for the specified organization
3332 * code and protocol type in the SNAP header.
3334 static struct block
*
3335 gen_snap(orgcode
, ptype
)
3336 bpf_u_int32 orgcode
;
3339 u_char snapblock
[8];
3341 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3342 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3343 snapblock
[2] = 0x03; /* control = UI */
3344 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3345 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3346 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3347 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3348 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3349 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3353 * Generate code to match frames with an LLC header.
3358 struct block
*b0
, *b1
;
3364 * We check for an Ethernet type field less than
3365 * 1500, which means it's an 802.3 length field.
3367 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3371 * Now check for the purported DSAP and SSAP not being
3372 * 0xFF, to rule out NetWare-over-802.3.
3374 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3381 * We check for LLC traffic.
3383 b0
= gen_atmtype_abbrev(A_LLC
);
3386 case DLT_IEEE802
: /* Token Ring */
3388 * XXX - check for LLC frames.
3394 * XXX - check for LLC frames.
3398 case DLT_ATM_RFC1483
:
3400 * For LLC encapsulation, these are defined to have an
3403 * For VC encapsulation, they don't, but there's no
3404 * way to check for that; the protocol used on the VC
3405 * is negotiated out of band.
3409 case DLT_IEEE802_11
:
3410 case DLT_PRISM_HEADER
:
3411 case DLT_IEEE802_11_RADIO
:
3412 case DLT_IEEE802_11_RADIO_AVS
:
3415 * Check that we have a data frame.
3417 b0
= gen_check_802_11_data_frame();
3421 bpf_error("'llc' not supported for linktype %d", linktype
);
3429 struct block
*b0
, *b1
;
3433 * Check whether this is an LLC frame.
3438 * Load the control byte and test the low-order bit; it must
3439 * be clear for I frames.
3441 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3442 b1
= new_block(JMP(BPF_JSET
));
3453 struct block
*b0
, *b1
;
3456 * Check whether this is an LLC frame.
3461 * Now compare the low-order 2 bit of the control byte against
3462 * the appropriate value for S frames.
3464 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3472 struct block
*b0
, *b1
;
3475 * Check whether this is an LLC frame.
3480 * Now compare the low-order 2 bit of the control byte against
3481 * the appropriate value for U frames.
3483 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3489 gen_llc_s_subtype(bpf_u_int32 subtype
)
3491 struct block
*b0
, *b1
;
3494 * Check whether this is an LLC frame.
3499 * Now check for an S frame with the appropriate type.
3501 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3507 gen_llc_u_subtype(bpf_u_int32 subtype
)
3509 struct block
*b0
, *b1
;
3512 * Check whether this is an LLC frame.
3517 * Now check for a U frame with the appropriate type.
3519 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3525 * Generate code to match a particular packet type, for link-layer types
3526 * using 802.2 LLC headers.
3528 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3529 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3531 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3532 * value, if <= ETHERMTU. We use that to determine whether to
3533 * match the DSAP or both DSAP and LSAP or to check the OUI and
3534 * protocol ID in a SNAP header.
3536 static struct block
*
3537 gen_llc_linktype(proto
)
3541 * XXX - handle token-ring variable-length header.
3547 case LLCSAP_NETBEUI
:
3549 * XXX - should we check both the DSAP and the
3550 * SSAP, like this, or should we check just the
3551 * DSAP, as we do for other SAP values?
3553 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3554 ((proto
<< 8) | proto
));
3558 * XXX - are there ever SNAP frames for IPX on
3559 * non-Ethernet 802.x networks?
3561 return gen_cmp(OR_LLC
, 0, BPF_B
,
3562 (bpf_int32
)LLCSAP_IPX
);
3564 case ETHERTYPE_ATALK
:
3566 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3567 * SNAP packets with an organization code of
3568 * 0x080007 (Apple, for Appletalk) and a protocol
3569 * type of ETHERTYPE_ATALK (Appletalk).
3571 * XXX - check for an organization code of
3572 * encapsulated Ethernet as well?
3574 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3578 * XXX - we don't have to check for IPX 802.3
3579 * here, but should we check for the IPX Ethertype?
3581 if (proto
<= ETHERMTU
) {
3583 * This is an LLC SAP value, so check
3586 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3589 * This is an Ethernet type; we assume that it's
3590 * unlikely that it'll appear in the right place
3591 * at random, and therefore check only the
3592 * location that would hold the Ethernet type
3593 * in a SNAP frame with an organization code of
3594 * 0x000000 (encapsulated Ethernet).
3596 * XXX - if we were to check for the SNAP DSAP and
3597 * LSAP, as per XXX, and were also to check for an
3598 * organization code of 0x000000 (encapsulated
3599 * Ethernet), we'd do
3601 * return gen_snap(0x000000, proto);
3603 * here; for now, we don't, as per the above.
3604 * I don't know whether it's worth the extra CPU
3605 * time to do the right check or not.
3607 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3612 static struct block
*
3613 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3617 u_int src_off
, dst_off
;
3619 struct block
*b0
, *b1
;
3633 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3634 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3640 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3641 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3648 b0
= gen_linktype(proto
);
3649 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3655 static struct block
*
3656 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3657 struct in6_addr
*addr
;
3658 struct in6_addr
*mask
;
3660 u_int src_off
, dst_off
;
3662 struct block
*b0
, *b1
;
3677 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3678 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3684 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3685 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3692 /* this order is important */
3693 a
= (u_int32_t
*)addr
;
3694 m
= (u_int32_t
*)mask
;
3695 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3696 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3698 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3700 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3702 b0
= gen_linktype(proto
);
3708 static struct block
*
3709 gen_ehostop(eaddr
, dir
)
3710 register const u_char
*eaddr
;
3713 register struct block
*b0
, *b1
;
3717 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3720 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3723 b0
= gen_ehostop(eaddr
, Q_SRC
);
3724 b1
= gen_ehostop(eaddr
, Q_DST
);
3730 b0
= gen_ehostop(eaddr
, Q_SRC
);
3731 b1
= gen_ehostop(eaddr
, Q_DST
);
3736 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3740 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3744 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3748 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3752 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3756 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3764 * Like gen_ehostop, but for DLT_FDDI
3766 static struct block
*
3767 gen_fhostop(eaddr
, dir
)
3768 register const u_char
*eaddr
;
3771 struct block
*b0
, *b1
;
3775 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3778 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3781 b0
= gen_fhostop(eaddr
, Q_SRC
);
3782 b1
= gen_fhostop(eaddr
, Q_DST
);
3788 b0
= gen_fhostop(eaddr
, Q_SRC
);
3789 b1
= gen_fhostop(eaddr
, Q_DST
);
3794 bpf_error("'addr1' is only supported on 802.11");
3798 bpf_error("'addr2' is only supported on 802.11");
3802 bpf_error("'addr3' is only supported on 802.11");
3806 bpf_error("'addr4' is only supported on 802.11");
3810 bpf_error("'ra' is only supported on 802.11");
3814 bpf_error("'ta' is only supported on 802.11");
3822 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3824 static struct block
*
3825 gen_thostop(eaddr
, dir
)
3826 register const u_char
*eaddr
;
3829 register struct block
*b0
, *b1
;
3833 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3836 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3839 b0
= gen_thostop(eaddr
, Q_SRC
);
3840 b1
= gen_thostop(eaddr
, Q_DST
);
3846 b0
= gen_thostop(eaddr
, Q_SRC
);
3847 b1
= gen_thostop(eaddr
, Q_DST
);
3852 bpf_error("'addr1' is only supported on 802.11");
3856 bpf_error("'addr2' is only supported on 802.11");
3860 bpf_error("'addr3' is only supported on 802.11");
3864 bpf_error("'addr4' is only supported on 802.11");
3868 bpf_error("'ra' is only supported on 802.11");
3872 bpf_error("'ta' is only supported on 802.11");
3880 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3881 * various 802.11 + radio headers.
3883 static struct block
*
3884 gen_wlanhostop(eaddr
, dir
)
3885 register const u_char
*eaddr
;
3888 register struct block
*b0
, *b1
, *b2
;
3889 register struct slist
*s
;
3891 #ifdef ENABLE_WLAN_FILTERING_PATCH
3894 * We need to disable the optimizer because the optimizer is buggy
3895 * and wipes out some LD instructions generated by the below
3896 * code to validate the Frame Control bits
3899 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3906 * For control frames, there is no SA.
3908 * For management frames, SA is at an
3909 * offset of 10 from the beginning of
3912 * For data frames, SA is at an offset
3913 * of 10 from the beginning of the packet
3914 * if From DS is clear, at an offset of
3915 * 16 from the beginning of the packet
3916 * if From DS is set and To DS is clear,
3917 * and an offset of 24 from the beginning
3918 * of the packet if From DS is set and To DS
3923 * Generate the tests to be done for data frames
3926 * First, check for To DS set, i.e. check "link[1] & 0x01".
3928 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3929 b1
= new_block(JMP(BPF_JSET
));
3930 b1
->s
.k
= 0x01; /* To DS */
3934 * If To DS is set, the SA is at 24.
3936 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
3940 * Now, check for To DS not set, i.e. check
3941 * "!(link[1] & 0x01)".
3943 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3944 b2
= new_block(JMP(BPF_JSET
));
3945 b2
->s
.k
= 0x01; /* To DS */
3950 * If To DS is not set, the SA is at 16.
3952 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
3956 * Now OR together the last two checks. That gives
3957 * the complete set of checks for data frames with
3963 * Now check for From DS being set, and AND that with
3964 * the ORed-together checks.
3966 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3967 b1
= new_block(JMP(BPF_JSET
));
3968 b1
->s
.k
= 0x02; /* From DS */
3973 * Now check for data frames with From DS not set.
3975 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3976 b2
= new_block(JMP(BPF_JSET
));
3977 b2
->s
.k
= 0x02; /* From DS */
3982 * If From DS isn't set, the SA is at 10.
3984 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
3988 * Now OR together the checks for data frames with
3989 * From DS not set and for data frames with From DS
3990 * set; that gives the checks done for data frames.
3995 * Now check for a data frame.
3996 * I.e, check "link[0] & 0x08".
3998 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3999 b1
= new_block(JMP(BPF_JSET
));
4004 * AND that with the checks done for data frames.
4009 * If the high-order bit of the type value is 0, this
4010 * is a management frame.
4011 * I.e, check "!(link[0] & 0x08)".
4013 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4014 b2
= new_block(JMP(BPF_JSET
));
4020 * For management frames, the SA is at 10.
4022 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4026 * OR that with the checks done for data frames.
4027 * That gives the checks done for management and
4033 * If the low-order bit of the type value is 1,
4034 * this is either a control frame or a frame
4035 * with a reserved type, and thus not a
4038 * I.e., check "!(link[0] & 0x04)".
4040 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4041 b1
= new_block(JMP(BPF_JSET
));
4047 * AND that with the checks for data and management
4057 * For control frames, there is no DA.
4059 * For management frames, DA is at an
4060 * offset of 4 from the beginning of
4063 * For data frames, DA is at an offset
4064 * of 4 from the beginning of the packet
4065 * if To DS is clear and at an offset of
4066 * 16 from the beginning of the packet
4071 * Generate the tests to be done for data frames.
4073 * First, check for To DS set, i.e. "link[1] & 0x01".
4075 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4076 b1
= new_block(JMP(BPF_JSET
));
4077 b1
->s
.k
= 0x01; /* To DS */
4081 * If To DS is set, the DA is at 16.
4083 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4087 * Now, check for To DS not set, i.e. check
4088 * "!(link[1] & 0x01)".
4090 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4091 b2
= new_block(JMP(BPF_JSET
));
4092 b2
->s
.k
= 0x01; /* To DS */
4097 * If To DS is not set, the DA is at 4.
4099 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4103 * Now OR together the last two checks. That gives
4104 * the complete set of checks for data frames.
4109 * Now check for a data frame.
4110 * I.e, check "link[0] & 0x08".
4112 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4113 b1
= new_block(JMP(BPF_JSET
));
4118 * AND that with the checks done for data frames.
4123 * If the high-order bit of the type value is 0, this
4124 * is a management frame.
4125 * I.e, check "!(link[0] & 0x08)".
4127 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4128 b2
= new_block(JMP(BPF_JSET
));
4134 * For management frames, the DA is at 4.
4136 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4140 * OR that with the checks done for data frames.
4141 * That gives the checks done for management and
4147 * If the low-order bit of the type value is 1,
4148 * this is either a control frame or a frame
4149 * with a reserved type, and thus not a
4152 * I.e., check "!(link[0] & 0x04)".
4154 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4155 b1
= new_block(JMP(BPF_JSET
));
4161 * AND that with the checks for data and management
4169 * Not present in management frames; addr1 in other
4174 * If the high-order bit of the type value is 0, this
4175 * is a management frame.
4176 * I.e, check "(link[0] & 0x08)".
4178 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4179 b1
= new_block(JMP(BPF_JSET
));
4186 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4189 * AND that with the check of addr1.
4196 * Not present in management frames; addr2, if present,
4201 * Not present in CTS or ACK control frames.
4203 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4204 IEEE80211_FC0_TYPE_MASK
);
4206 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4207 IEEE80211_FC0_SUBTYPE_MASK
);
4209 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4210 IEEE80211_FC0_SUBTYPE_MASK
);
4216 * If the high-order bit of the type value is 0, this
4217 * is a management frame.
4218 * I.e, check "(link[0] & 0x08)".
4220 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4221 b1
= new_block(JMP(BPF_JSET
));
4226 * AND that with the check for frames other than
4227 * CTS and ACK frames.
4234 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4239 * XXX - add BSSID keyword?
4242 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4246 * Not present in CTS or ACK control frames.
4248 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4249 IEEE80211_FC0_TYPE_MASK
);
4251 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4252 IEEE80211_FC0_SUBTYPE_MASK
);
4254 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4255 IEEE80211_FC0_SUBTYPE_MASK
);
4259 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4265 * Not present in control frames.
4267 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4268 IEEE80211_FC0_TYPE_MASK
);
4270 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4276 * Present only if the direction mask has both "From DS"
4277 * and "To DS" set. Neither control frames nor management
4278 * frames should have both of those set, so we don't
4279 * check the frame type.
4281 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4282 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4283 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4288 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4289 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4295 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4296 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4305 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4306 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4307 * as the RFC states.)
4309 static struct block
*
4310 gen_ipfchostop(eaddr
, dir
)
4311 register const u_char
*eaddr
;
4314 register struct block
*b0
, *b1
;
4318 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4321 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4324 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4325 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4331 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4332 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4337 bpf_error("'addr1' is only supported on 802.11");
4341 bpf_error("'addr2' is only supported on 802.11");
4345 bpf_error("'addr3' is only supported on 802.11");
4349 bpf_error("'addr4' is only supported on 802.11");
4353 bpf_error("'ra' is only supported on 802.11");
4357 bpf_error("'ta' is only supported on 802.11");
4365 * This is quite tricky because there may be pad bytes in front of the
4366 * DECNET header, and then there are two possible data packet formats that
4367 * carry both src and dst addresses, plus 5 packet types in a format that
4368 * carries only the src node, plus 2 types that use a different format and
4369 * also carry just the src node.
4373 * Instead of doing those all right, we just look for data packets with
4374 * 0 or 1 bytes of padding. If you want to look at other packets, that
4375 * will require a lot more hacking.
4377 * To add support for filtering on DECNET "areas" (network numbers)
4378 * one would want to add a "mask" argument to this routine. That would
4379 * make the filter even more inefficient, although one could be clever
4380 * and not generate masking instructions if the mask is 0xFFFF.
4382 static struct block
*
4383 gen_dnhostop(addr
, dir
)
4387 struct block
*b0
, *b1
, *b2
, *tmp
;
4388 u_int offset_lh
; /* offset if long header is received */
4389 u_int offset_sh
; /* offset if short header is received */
4394 offset_sh
= 1; /* follows flags */
4395 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4399 offset_sh
= 3; /* follows flags, dstnode */
4400 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4404 /* Inefficient because we do our Calvinball dance twice */
4405 b0
= gen_dnhostop(addr
, Q_SRC
);
4406 b1
= gen_dnhostop(addr
, Q_DST
);
4412 /* Inefficient because we do our Calvinball dance twice */
4413 b0
= gen_dnhostop(addr
, Q_SRC
);
4414 b1
= gen_dnhostop(addr
, Q_DST
);
4419 bpf_error("ISO host filtering not implemented");
4424 b0
= gen_linktype(ETHERTYPE_DN
);
4425 /* Check for pad = 1, long header case */
4426 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4427 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4428 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4429 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4431 /* Check for pad = 0, long header case */
4432 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4433 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4436 /* Check for pad = 1, short header case */
4437 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4438 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4439 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4442 /* Check for pad = 0, short header case */
4443 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4444 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4448 /* Combine with test for linktype */
4454 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4455 * test the bottom-of-stack bit, and then check the version number
4456 * field in the IP header.
4458 static struct block
*
4459 gen_mpls_linktype(proto
)
4462 struct block
*b0
, *b1
;
4467 /* match the bottom-of-stack bit */
4468 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4469 /* match the IPv4 version number */
4470 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4475 /* match the bottom-of-stack bit */
4476 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4477 /* match the IPv4 version number */
4478 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4487 static struct block
*
4488 gen_host(addr
, mask
, proto
, dir
, type
)
4495 struct block
*b0
, *b1
;
4496 const char *typestr
;
4506 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4508 * Only check for non-IPv4 addresses if we're not
4509 * checking MPLS-encapsulated packets.
4511 if (label_stack_depth
== 0) {
4512 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4514 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4520 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4523 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4526 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4529 bpf_error("'tcp' modifier applied to %s", typestr
);
4532 bpf_error("'sctp' modifier applied to %s", typestr
);
4535 bpf_error("'udp' modifier applied to %s", typestr
);
4538 bpf_error("'icmp' modifier applied to %s", typestr
);
4541 bpf_error("'igmp' modifier applied to %s", typestr
);
4544 bpf_error("'igrp' modifier applied to %s", typestr
);
4547 bpf_error("'pim' modifier applied to %s", typestr
);
4550 bpf_error("'vrrp' modifier applied to %s", typestr
);
4553 bpf_error("'carp' modifier applied to %s", typestr
);
4556 bpf_error("ATALK host filtering not implemented");
4559 bpf_error("AARP host filtering not implemented");
4562 return gen_dnhostop(addr
, dir
);
4565 bpf_error("SCA host filtering not implemented");
4568 bpf_error("LAT host filtering not implemented");
4571 bpf_error("MOPDL host filtering not implemented");
4574 bpf_error("MOPRC host filtering not implemented");
4577 bpf_error("'ip6' modifier applied to ip host");
4580 bpf_error("'icmp6' modifier applied to %s", typestr
);
4583 bpf_error("'ah' modifier applied to %s", typestr
);
4586 bpf_error("'esp' modifier applied to %s", typestr
);
4589 bpf_error("ISO host filtering not implemented");
4592 bpf_error("'esis' modifier applied to %s", typestr
);
4595 bpf_error("'isis' modifier applied to %s", typestr
);
4598 bpf_error("'clnp' modifier applied to %s", typestr
);
4601 bpf_error("'stp' modifier applied to %s", typestr
);
4604 bpf_error("IPX host filtering not implemented");
4607 bpf_error("'netbeui' modifier applied to %s", typestr
);
4610 bpf_error("'radio' modifier applied to %s", typestr
);
4619 static struct block
*
4620 gen_host6(addr
, mask
, proto
, dir
, type
)
4621 struct in6_addr
*addr
;
4622 struct in6_addr
*mask
;
4627 const char *typestr
;
4637 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4640 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4643 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4646 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4649 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4652 bpf_error("'sctp' modifier applied to %s", typestr
);
4655 bpf_error("'tcp' modifier applied to %s", typestr
);
4658 bpf_error("'udp' modifier applied to %s", typestr
);
4661 bpf_error("'icmp' modifier applied to %s", typestr
);
4664 bpf_error("'igmp' modifier applied to %s", typestr
);
4667 bpf_error("'igrp' modifier applied to %s", typestr
);
4670 bpf_error("'pim' modifier applied to %s", typestr
);
4673 bpf_error("'vrrp' modifier applied to %s", typestr
);
4676 bpf_error("'carp' modifier applied to %s", typestr
);
4679 bpf_error("ATALK host filtering not implemented");
4682 bpf_error("AARP host filtering not implemented");
4685 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4688 bpf_error("SCA host filtering not implemented");
4691 bpf_error("LAT host filtering not implemented");
4694 bpf_error("MOPDL host filtering not implemented");
4697 bpf_error("MOPRC host filtering not implemented");
4700 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4703 bpf_error("'icmp6' modifier applied to %s", typestr
);
4706 bpf_error("'ah' modifier applied to %s", typestr
);
4709 bpf_error("'esp' modifier applied to %s", typestr
);
4712 bpf_error("ISO host filtering not implemented");
4715 bpf_error("'esis' modifier applied to %s", typestr
);
4718 bpf_error("'isis' modifier applied to %s", typestr
);
4721 bpf_error("'clnp' modifier applied to %s", typestr
);
4724 bpf_error("'stp' modifier applied to %s", typestr
);
4727 bpf_error("IPX host filtering not implemented");
4730 bpf_error("'netbeui' modifier applied to %s", typestr
);
4733 bpf_error("'radio' modifier applied to %s", typestr
);
4743 static struct block
*
4744 gen_gateway(eaddr
, alist
, proto
, dir
)
4745 const u_char
*eaddr
;
4746 bpf_u_int32
**alist
;
4750 struct block
*b0
, *b1
, *tmp
;
4753 bpf_error("direction applied to 'gateway'");
4762 case DLT_NETANALYZER
:
4763 case DLT_NETANALYZER_TRANSPARENT
:
4764 b1
= gen_prevlinkhdr_check();
4765 b0
= gen_ehostop(eaddr
, Q_OR
);
4770 b0
= gen_fhostop(eaddr
, Q_OR
);
4773 b0
= gen_thostop(eaddr
, Q_OR
);
4775 case DLT_IEEE802_11
:
4776 case DLT_PRISM_HEADER
:
4777 case DLT_IEEE802_11_RADIO_AVS
:
4778 case DLT_IEEE802_11_RADIO
:
4780 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4784 * This is LLC-multiplexed traffic; if it were
4785 * LANE, linktype would have been set to
4789 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4791 case DLT_IP_OVER_FC
:
4792 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4796 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4798 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4800 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4809 bpf_error("illegal modifier of 'gateway'");
4815 gen_proto_abbrev(proto
)
4824 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4825 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4830 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4831 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4836 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4837 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4842 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4845 #ifndef IPPROTO_IGMP
4846 #define IPPROTO_IGMP 2
4850 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4853 #ifndef IPPROTO_IGRP
4854 #define IPPROTO_IGRP 9
4857 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4861 #define IPPROTO_PIM 103
4865 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4866 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4870 #ifndef IPPROTO_VRRP
4871 #define IPPROTO_VRRP 112
4875 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4878 #ifndef IPPROTO_CARP
4879 #define IPPROTO_CARP 112
4883 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4887 b1
= gen_linktype(ETHERTYPE_IP
);
4891 b1
= gen_linktype(ETHERTYPE_ARP
);
4895 b1
= gen_linktype(ETHERTYPE_REVARP
);
4899 bpf_error("link layer applied in wrong context");
4902 b1
= gen_linktype(ETHERTYPE_ATALK
);
4906 b1
= gen_linktype(ETHERTYPE_AARP
);
4910 b1
= gen_linktype(ETHERTYPE_DN
);
4914 b1
= gen_linktype(ETHERTYPE_SCA
);
4918 b1
= gen_linktype(ETHERTYPE_LAT
);
4922 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4926 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4930 b1
= gen_linktype(ETHERTYPE_IPV6
);
4933 #ifndef IPPROTO_ICMPV6
4934 #define IPPROTO_ICMPV6 58
4937 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4941 #define IPPROTO_AH 51
4944 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4945 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4950 #define IPPROTO_ESP 50
4953 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4954 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4959 b1
= gen_linktype(LLCSAP_ISONS
);
4963 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4967 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4970 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4971 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4972 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4974 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4976 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4978 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4982 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4983 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4984 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4986 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4988 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4990 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4994 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4995 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4996 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4998 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5003 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5004 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5009 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5010 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5012 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5014 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5019 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5020 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5025 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5026 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5031 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5035 b1
= gen_linktype(LLCSAP_8021D
);
5039 b1
= gen_linktype(LLCSAP_IPX
);
5043 b1
= gen_linktype(LLCSAP_NETBEUI
);
5047 bpf_error("'radio' is not a valid protocol type");
5055 static struct block
*
5061 /* not IPv4 frag other than the first frag */
5062 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5063 b
= new_block(JMP(BPF_JSET
));
5072 * Generate a comparison to a port value in the transport-layer header
5073 * at the specified offset from the beginning of that header.
5075 * XXX - this handles a variable-length prefix preceding the link-layer
5076 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5077 * variable-length link-layer headers (such as Token Ring or 802.11
5080 static struct block
*
5081 gen_portatom(off
, v
)
5085 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5088 static struct block
*
5089 gen_portatom6(off
, v
)
5093 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5097 gen_portop(port
, proto
, dir
)
5098 int port
, proto
, dir
;
5100 struct block
*b0
, *b1
, *tmp
;
5102 /* ip proto 'proto' and not a fragment other than the first fragment */
5103 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5109 b1
= gen_portatom(0, (bpf_int32
)port
);
5113 b1
= gen_portatom(2, (bpf_int32
)port
);
5118 tmp
= gen_portatom(0, (bpf_int32
)port
);
5119 b1
= gen_portatom(2, (bpf_int32
)port
);
5124 tmp
= gen_portatom(0, (bpf_int32
)port
);
5125 b1
= gen_portatom(2, (bpf_int32
)port
);
5137 static struct block
*
5138 gen_port(port
, ip_proto
, dir
)
5143 struct block
*b0
, *b1
, *tmp
;
5148 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5149 * not LLC encapsulation with LLCSAP_IP.
5151 * For IEEE 802 networks - which includes 802.5 token ring
5152 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5153 * says that SNAP encapsulation is used, not LLC encapsulation
5156 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5157 * RFC 2225 say that SNAP encapsulation is used, not LLC
5158 * encapsulation with LLCSAP_IP.
5160 * So we always check for ETHERTYPE_IP.
5162 b0
= gen_linktype(ETHERTYPE_IP
);
5168 b1
= gen_portop(port
, ip_proto
, dir
);
5172 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5173 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5175 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5187 gen_portop6(port
, proto
, dir
)
5188 int port
, proto
, dir
;
5190 struct block
*b0
, *b1
, *tmp
;
5192 /* ip6 proto 'proto' */
5193 /* XXX - catch the first fragment of a fragmented packet? */
5194 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5198 b1
= gen_portatom6(0, (bpf_int32
)port
);
5202 b1
= gen_portatom6(2, (bpf_int32
)port
);
5207 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5208 b1
= gen_portatom6(2, (bpf_int32
)port
);
5213 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5214 b1
= gen_portatom6(2, (bpf_int32
)port
);
5226 static struct block
*
5227 gen_port6(port
, ip_proto
, dir
)
5232 struct block
*b0
, *b1
, *tmp
;
5234 /* link proto ip6 */
5235 b0
= gen_linktype(ETHERTYPE_IPV6
);
5241 b1
= gen_portop6(port
, ip_proto
, dir
);
5245 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5246 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5248 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5259 /* gen_portrange code */
5260 static struct block
*
5261 gen_portrangeatom(off
, v1
, v2
)
5265 struct block
*b1
, *b2
;
5269 * Reverse the order of the ports, so v1 is the lower one.
5278 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5279 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5287 gen_portrangeop(port1
, port2
, proto
, dir
)
5292 struct block
*b0
, *b1
, *tmp
;
5294 /* ip proto 'proto' and not a fragment other than the first fragment */
5295 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5301 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5305 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5310 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5311 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5316 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5317 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5329 static struct block
*
5330 gen_portrange(port1
, port2
, ip_proto
, dir
)
5335 struct block
*b0
, *b1
, *tmp
;
5338 b0
= gen_linktype(ETHERTYPE_IP
);
5344 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5348 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5349 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5351 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5362 static struct block
*
5363 gen_portrangeatom6(off
, v1
, v2
)
5367 struct block
*b1
, *b2
;
5371 * Reverse the order of the ports, so v1 is the lower one.
5380 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5381 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5389 gen_portrangeop6(port1
, port2
, proto
, dir
)
5394 struct block
*b0
, *b1
, *tmp
;
5396 /* ip6 proto 'proto' */
5397 /* XXX - catch the first fragment of a fragmented packet? */
5398 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5402 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5406 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5411 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5412 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5417 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5418 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5430 static struct block
*
5431 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5436 struct block
*b0
, *b1
, *tmp
;
5438 /* link proto ip6 */
5439 b0
= gen_linktype(ETHERTYPE_IPV6
);
5445 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5449 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5450 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5452 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5464 lookup_proto(name
, proto
)
5465 register const char *name
;
5475 v
= pcap_nametoproto(name
);
5476 if (v
== PROTO_UNDEF
)
5477 bpf_error("unknown ip proto '%s'", name
);
5481 /* XXX should look up h/w protocol type based on linktype */
5482 v
= pcap_nametoeproto(name
);
5483 if (v
== PROTO_UNDEF
) {
5484 v
= pcap_nametollc(name
);
5485 if (v
== PROTO_UNDEF
)
5486 bpf_error("unknown ether proto '%s'", name
);
5491 if (strcmp(name
, "esis") == 0)
5493 else if (strcmp(name
, "isis") == 0)
5495 else if (strcmp(name
, "clnp") == 0)
5498 bpf_error("unknown osi proto '%s'", name
);
5518 static struct block
*
5519 gen_protochain(v
, proto
, dir
)
5524 #ifdef NO_PROTOCHAIN
5525 return gen_proto(v
, proto
, dir
);
5527 struct block
*b0
, *b
;
5528 struct slist
*s
[100];
5529 int fix2
, fix3
, fix4
, fix5
;
5530 int ahcheck
, again
, end
;
5532 int reg2
= alloc_reg();
5534 memset(s
, 0, sizeof(s
));
5535 fix2
= fix3
= fix4
= fix5
= 0;
5542 b0
= gen_protochain(v
, Q_IP
, dir
);
5543 b
= gen_protochain(v
, Q_IPV6
, dir
);
5547 bpf_error("bad protocol applied for 'protochain'");
5552 * We don't handle variable-length prefixes before the link-layer
5553 * header, or variable-length link-layer headers, here yet.
5554 * We might want to add BPF instructions to do the protochain
5555 * work, to simplify that and, on platforms that have a BPF
5556 * interpreter with the new instructions, let the filtering
5557 * be done in the kernel. (We already require a modified BPF
5558 * engine to do the protochain stuff, to support backward
5559 * branches, and backward branch support is unlikely to appear
5560 * in kernel BPF engines.)
5562 if (off_linkpl
.is_variable
)
5563 bpf_error("'protochain' not supported with variable length headers");
5565 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5568 * s[0] is a dummy entry to protect other BPF insn from damage
5569 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5570 * hard to find interdependency made by jump table fixup.
5573 s
[i
] = new_stmt(0); /*dummy*/
5578 b0
= gen_linktype(ETHERTYPE_IP
);
5581 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5582 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5584 /* X = ip->ip_hl << 2 */
5585 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5586 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5591 b0
= gen_linktype(ETHERTYPE_IPV6
);
5593 /* A = ip6->ip_nxt */
5594 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5595 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5597 /* X = sizeof(struct ip6_hdr) */
5598 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5604 bpf_error("unsupported proto to gen_protochain");
5608 /* again: if (A == v) goto end; else fall through; */
5610 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5612 s
[i
]->s
.jt
= NULL
; /*later*/
5613 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5617 #ifndef IPPROTO_NONE
5618 #define IPPROTO_NONE 59
5620 /* if (A == IPPROTO_NONE) goto end */
5621 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5622 s
[i
]->s
.jt
= NULL
; /*later*/
5623 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5624 s
[i
]->s
.k
= IPPROTO_NONE
;
5625 s
[fix5
]->s
.jf
= s
[i
];
5629 if (proto
== Q_IPV6
) {
5630 int v6start
, v6end
, v6advance
, j
;
5633 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5634 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5635 s
[i
]->s
.jt
= NULL
; /*later*/
5636 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5637 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5638 s
[fix2
]->s
.jf
= s
[i
];
5640 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5641 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5642 s
[i
]->s
.jt
= NULL
; /*later*/
5643 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5644 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5646 /* if (A == IPPROTO_ROUTING) goto v6advance */
5647 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5648 s
[i
]->s
.jt
= NULL
; /*later*/
5649 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5650 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5652 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5653 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5654 s
[i
]->s
.jt
= NULL
; /*later*/
5655 s
[i
]->s
.jf
= NULL
; /*later*/
5656 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5666 * A = P[X + packet head];
5667 * X = X + (P[X + packet head + 1] + 1) * 8;
5669 /* A = P[X + packet head] */
5670 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5671 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5674 s
[i
] = new_stmt(BPF_ST
);
5677 /* A = P[X + packet head + 1]; */
5678 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5679 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5682 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5686 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5690 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5694 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5697 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5701 /* goto again; (must use BPF_JA for backward jump) */
5702 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5703 s
[i
]->s
.k
= again
- i
- 1;
5704 s
[i
- 1]->s
.jf
= s
[i
];
5708 for (j
= v6start
; j
<= v6end
; j
++)
5709 s
[j
]->s
.jt
= s
[v6advance
];
5712 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5714 s
[fix2
]->s
.jf
= s
[i
];
5720 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5721 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5722 s
[i
]->s
.jt
= NULL
; /*later*/
5723 s
[i
]->s
.jf
= NULL
; /*later*/
5724 s
[i
]->s
.k
= IPPROTO_AH
;
5726 s
[fix3
]->s
.jf
= s
[ahcheck
];
5733 * X = X + (P[X + 1] + 2) * 4;
5736 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5738 /* A = P[X + packet head]; */
5739 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5740 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5743 s
[i
] = new_stmt(BPF_ST
);
5747 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5750 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5754 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5756 /* A = P[X + packet head] */
5757 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5758 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5761 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5765 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5769 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5772 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5776 /* goto again; (must use BPF_JA for backward jump) */
5777 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5778 s
[i
]->s
.k
= again
- i
- 1;
5783 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5785 s
[fix2
]->s
.jt
= s
[end
];
5786 s
[fix4
]->s
.jf
= s
[end
];
5787 s
[fix5
]->s
.jt
= s
[end
];
5794 for (i
= 0; i
< max
- 1; i
++)
5795 s
[i
]->next
= s
[i
+ 1];
5796 s
[max
- 1]->next
= NULL
;
5801 b
= new_block(JMP(BPF_JEQ
));
5802 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5812 static struct block
*
5813 gen_check_802_11_data_frame()
5816 struct block
*b0
, *b1
;
5819 * A data frame has the 0x08 bit (b3) in the frame control field set
5820 * and the 0x04 bit (b2) clear.
5822 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5823 b0
= new_block(JMP(BPF_JSET
));
5827 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5828 b1
= new_block(JMP(BPF_JSET
));
5839 * Generate code that checks whether the packet is a packet for protocol
5840 * <proto> and whether the type field in that protocol's header has
5841 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5842 * IP packet and checks the protocol number in the IP header against <v>.
5844 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5845 * against Q_IP and Q_IPV6.
5847 static struct block
*
5848 gen_proto(v
, proto
, dir
)
5853 struct block
*b0
, *b1
;
5858 if (dir
!= Q_DEFAULT
)
5859 bpf_error("direction applied to 'proto'");
5863 b0
= gen_proto(v
, Q_IP
, dir
);
5864 b1
= gen_proto(v
, Q_IPV6
, dir
);
5870 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5871 * not LLC encapsulation with LLCSAP_IP.
5873 * For IEEE 802 networks - which includes 802.5 token ring
5874 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5875 * says that SNAP encapsulation is used, not LLC encapsulation
5878 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5879 * RFC 2225 say that SNAP encapsulation is used, not LLC
5880 * encapsulation with LLCSAP_IP.
5882 * So we always check for ETHERTYPE_IP.
5884 b0
= gen_linktype(ETHERTYPE_IP
);
5886 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5888 b1
= gen_protochain(v
, Q_IP
);
5898 * Frame Relay packets typically have an OSI
5899 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5900 * generates code to check for all the OSI
5901 * NLPIDs, so calling it and then adding a check
5902 * for the particular NLPID for which we're
5903 * looking is bogus, as we can just check for
5906 * What we check for is the NLPID and a frame
5907 * control field value of UI, i.e. 0x03 followed
5910 * XXX - assumes a 2-byte Frame Relay header with
5911 * DLCI and flags. What if the address is longer?
5913 * XXX - what about SNAP-encapsulated frames?
5915 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5921 * Cisco uses an Ethertype lookalike - for OSI,
5924 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5925 /* OSI in C-HDLC is stuffed with a fudge byte */
5926 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
5931 b0
= gen_linktype(LLCSAP_ISONS
);
5932 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
5938 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5940 * 4 is the offset of the PDU type relative to the IS-IS
5943 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
5948 bpf_error("arp does not encapsulate another protocol");
5952 bpf_error("rarp does not encapsulate another protocol");
5956 bpf_error("atalk encapsulation is not specifiable");
5960 bpf_error("decnet encapsulation is not specifiable");
5964 bpf_error("sca does not encapsulate another protocol");
5968 bpf_error("lat does not encapsulate another protocol");
5972 bpf_error("moprc does not encapsulate another protocol");
5976 bpf_error("mopdl does not encapsulate another protocol");
5980 return gen_linktype(v
);
5983 bpf_error("'udp proto' is bogus");
5987 bpf_error("'tcp proto' is bogus");
5991 bpf_error("'sctp proto' is bogus");
5995 bpf_error("'icmp proto' is bogus");
5999 bpf_error("'igmp proto' is bogus");
6003 bpf_error("'igrp proto' is bogus");
6007 bpf_error("'pim proto' is bogus");
6011 bpf_error("'vrrp proto' is bogus");
6015 bpf_error("'carp proto' is bogus");
6019 b0
= gen_linktype(ETHERTYPE_IPV6
);
6022 * Also check for a fragment header before the final
6025 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6026 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6028 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6031 b1
= gen_protochain(v
, Q_IPV6
);
6037 bpf_error("'icmp6 proto' is bogus");
6040 bpf_error("'ah proto' is bogus");
6043 bpf_error("'ah proto' is bogus");
6046 bpf_error("'stp proto' is bogus");
6049 bpf_error("'ipx proto' is bogus");
6052 bpf_error("'netbeui proto' is bogus");
6055 bpf_error("'radio proto' is bogus");
6066 register const char *name
;
6069 int proto
= q
.proto
;
6073 bpf_u_int32 mask
, addr
;
6075 bpf_u_int32
**alist
;
6078 struct sockaddr_in
*sin4
;
6079 struct sockaddr_in6
*sin6
;
6080 struct addrinfo
*res
, *res0
;
6081 struct in6_addr mask128
;
6083 struct block
*b
, *tmp
;
6084 int port
, real_proto
;
6090 addr
= pcap_nametonetaddr(name
);
6092 bpf_error("unknown network '%s'", name
);
6093 /* Left justify network addr and calculate its network mask */
6095 while (addr
&& (addr
& 0xff000000) == 0) {
6099 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6103 if (proto
== Q_LINK
) {
6107 case DLT_NETANALYZER
:
6108 case DLT_NETANALYZER_TRANSPARENT
:
6109 eaddr
= pcap_ether_hostton(name
);
6112 "unknown ether host '%s'", name
);
6113 tmp
= gen_prevlinkhdr_check();
6114 b
= gen_ehostop(eaddr
, dir
);
6121 eaddr
= pcap_ether_hostton(name
);
6124 "unknown FDDI host '%s'", name
);
6125 b
= gen_fhostop(eaddr
, dir
);
6130 eaddr
= pcap_ether_hostton(name
);
6133 "unknown token ring host '%s'", name
);
6134 b
= gen_thostop(eaddr
, dir
);
6138 case DLT_IEEE802_11
:
6139 case DLT_PRISM_HEADER
:
6140 case DLT_IEEE802_11_RADIO_AVS
:
6141 case DLT_IEEE802_11_RADIO
:
6143 eaddr
= pcap_ether_hostton(name
);
6146 "unknown 802.11 host '%s'", name
);
6147 b
= gen_wlanhostop(eaddr
, dir
);
6151 case DLT_IP_OVER_FC
:
6152 eaddr
= pcap_ether_hostton(name
);
6155 "unknown Fibre Channel host '%s'", name
);
6156 b
= gen_ipfchostop(eaddr
, dir
);
6161 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6162 } else if (proto
== Q_DECNET
) {
6163 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6165 * I don't think DECNET hosts can be multihomed, so
6166 * there is no need to build up a list of addresses
6168 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6171 alist
= pcap_nametoaddr(name
);
6172 if (alist
== NULL
|| *alist
== NULL
)
6173 bpf_error("unknown host '%s'", name
);
6175 if (off_linktype
.constant_part
== (u_int
)-1 &&
6176 tproto
== Q_DEFAULT
)
6178 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6180 tmp
= gen_host(**alist
++, 0xffffffff,
6181 tproto
, dir
, q
.addr
);
6187 memset(&mask128
, 0xff, sizeof(mask128
));
6188 res0
= res
= pcap_nametoaddrinfo(name
);
6190 bpf_error("unknown host '%s'", name
);
6193 tproto
= tproto6
= proto
;
6194 if (off_linktype
.constant_part
== -1 &&
6195 tproto
== Q_DEFAULT
) {
6199 for (res
= res0
; res
; res
= res
->ai_next
) {
6200 switch (res
->ai_family
) {
6202 if (tproto
== Q_IPV6
)
6205 sin4
= (struct sockaddr_in
*)
6207 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6208 0xffffffff, tproto
, dir
, q
.addr
);
6211 if (tproto6
== Q_IP
)
6214 sin6
= (struct sockaddr_in6
*)
6216 tmp
= gen_host6(&sin6
->sin6_addr
,
6217 &mask128
, tproto6
, dir
, q
.addr
);
6229 bpf_error("unknown host '%s'%s", name
,
6230 (proto
== Q_DEFAULT
)
6232 : " for specified address family");
6239 if (proto
!= Q_DEFAULT
&&
6240 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6241 bpf_error("illegal qualifier of 'port'");
6242 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6243 bpf_error("unknown port '%s'", name
);
6244 if (proto
== Q_UDP
) {
6245 if (real_proto
== IPPROTO_TCP
)
6246 bpf_error("port '%s' is tcp", name
);
6247 else if (real_proto
== IPPROTO_SCTP
)
6248 bpf_error("port '%s' is sctp", name
);
6250 /* override PROTO_UNDEF */
6251 real_proto
= IPPROTO_UDP
;
6253 if (proto
== Q_TCP
) {
6254 if (real_proto
== IPPROTO_UDP
)
6255 bpf_error("port '%s' is udp", name
);
6257 else if (real_proto
== IPPROTO_SCTP
)
6258 bpf_error("port '%s' is sctp", name
);
6260 /* override PROTO_UNDEF */
6261 real_proto
= IPPROTO_TCP
;
6263 if (proto
== Q_SCTP
) {
6264 if (real_proto
== IPPROTO_UDP
)
6265 bpf_error("port '%s' is udp", name
);
6267 else if (real_proto
== IPPROTO_TCP
)
6268 bpf_error("port '%s' is tcp", name
);
6270 /* override PROTO_UNDEF */
6271 real_proto
= IPPROTO_SCTP
;
6274 bpf_error("illegal port number %d < 0", port
);
6276 bpf_error("illegal port number %d > 65535", port
);
6277 b
= gen_port(port
, real_proto
, dir
);
6278 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6282 if (proto
!= Q_DEFAULT
&&
6283 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6284 bpf_error("illegal qualifier of 'portrange'");
6285 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6286 bpf_error("unknown port in range '%s'", name
);
6287 if (proto
== Q_UDP
) {
6288 if (real_proto
== IPPROTO_TCP
)
6289 bpf_error("port in range '%s' is tcp", name
);
6290 else if (real_proto
== IPPROTO_SCTP
)
6291 bpf_error("port in range '%s' is sctp", name
);
6293 /* override PROTO_UNDEF */
6294 real_proto
= IPPROTO_UDP
;
6296 if (proto
== Q_TCP
) {
6297 if (real_proto
== IPPROTO_UDP
)
6298 bpf_error("port in range '%s' is udp", name
);
6299 else if (real_proto
== IPPROTO_SCTP
)
6300 bpf_error("port in range '%s' is sctp", name
);
6302 /* override PROTO_UNDEF */
6303 real_proto
= IPPROTO_TCP
;
6305 if (proto
== Q_SCTP
) {
6306 if (real_proto
== IPPROTO_UDP
)
6307 bpf_error("port in range '%s' is udp", name
);
6308 else if (real_proto
== IPPROTO_TCP
)
6309 bpf_error("port in range '%s' is tcp", name
);
6311 /* override PROTO_UNDEF */
6312 real_proto
= IPPROTO_SCTP
;
6315 bpf_error("illegal port number %d < 0", port1
);
6317 bpf_error("illegal port number %d > 65535", port1
);
6319 bpf_error("illegal port number %d < 0", port2
);
6321 bpf_error("illegal port number %d > 65535", port2
);
6323 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6324 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6329 eaddr
= pcap_ether_hostton(name
);
6331 bpf_error("unknown ether host: %s", name
);
6333 alist
= pcap_nametoaddr(name
);
6334 if (alist
== NULL
|| *alist
== NULL
)
6335 bpf_error("unknown host '%s'", name
);
6336 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6340 bpf_error("'gateway' not supported in this configuration");
6344 real_proto
= lookup_proto(name
, proto
);
6345 if (real_proto
>= 0)
6346 return gen_proto(real_proto
, proto
, dir
);
6348 bpf_error("unknown protocol: %s", name
);
6351 real_proto
= lookup_proto(name
, proto
);
6352 if (real_proto
>= 0)
6353 return gen_protochain(real_proto
, proto
, dir
);
6355 bpf_error("unknown protocol: %s", name
);
6366 gen_mcode(s1
, s2
, masklen
, q
)
6367 register const char *s1
, *s2
;
6368 register unsigned int masklen
;
6371 register int nlen
, mlen
;
6374 nlen
= __pcap_atoin(s1
, &n
);
6375 /* Promote short ipaddr */
6379 mlen
= __pcap_atoin(s2
, &m
);
6380 /* Promote short ipaddr */
6383 bpf_error("non-network bits set in \"%s mask %s\"",
6386 /* Convert mask len to mask */
6388 bpf_error("mask length must be <= 32");
6391 * X << 32 is not guaranteed by C to be 0; it's
6396 m
= 0xffffffff << (32 - masklen
);
6398 bpf_error("non-network bits set in \"%s/%d\"",
6405 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6408 bpf_error("Mask syntax for networks only");
6417 register const char *s
;
6422 int proto
= q
.proto
;
6428 else if (q
.proto
== Q_DECNET
)
6429 vlen
= __pcap_atodn(s
, &v
);
6431 vlen
= __pcap_atoin(s
, &v
);
6438 if (proto
== Q_DECNET
)
6439 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6440 else if (proto
== Q_LINK
) {
6441 bpf_error("illegal link layer address");
6444 if (s
== NULL
&& q
.addr
== Q_NET
) {
6445 /* Promote short net number */
6446 while (v
&& (v
& 0xff000000) == 0) {
6451 /* Promote short ipaddr */
6455 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6460 proto
= IPPROTO_UDP
;
6461 else if (proto
== Q_TCP
)
6462 proto
= IPPROTO_TCP
;
6463 else if (proto
== Q_SCTP
)
6464 proto
= IPPROTO_SCTP
;
6465 else if (proto
== Q_DEFAULT
)
6466 proto
= PROTO_UNDEF
;
6468 bpf_error("illegal qualifier of 'port'");
6471 bpf_error("illegal port number %u > 65535", v
);
6475 b
= gen_port((int)v
, proto
, dir
);
6476 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6482 proto
= IPPROTO_UDP
;
6483 else if (proto
== Q_TCP
)
6484 proto
= IPPROTO_TCP
;
6485 else if (proto
== Q_SCTP
)
6486 proto
= IPPROTO_SCTP
;
6487 else if (proto
== Q_DEFAULT
)
6488 proto
= PROTO_UNDEF
;
6490 bpf_error("illegal qualifier of 'portrange'");
6493 bpf_error("illegal port number %u > 65535", v
);
6497 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6498 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6503 bpf_error("'gateway' requires a name");
6507 return gen_proto((int)v
, proto
, dir
);
6510 return gen_protochain((int)v
, proto
, dir
);
6525 gen_mcode6(s1
, s2
, masklen
, q
)
6526 register const char *s1
, *s2
;
6527 register unsigned int masklen
;
6530 struct addrinfo
*res
;
6531 struct in6_addr
*addr
;
6532 struct in6_addr mask
;
6537 bpf_error("no mask %s supported", s2
);
6539 res
= pcap_nametoaddrinfo(s1
);
6541 bpf_error("invalid ip6 address %s", s1
);
6544 bpf_error("%s resolved to multiple address", s1
);
6545 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6547 if (sizeof(mask
) * 8 < masklen
)
6548 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6549 memset(&mask
, 0, sizeof(mask
));
6550 memset(&mask
, 0xff, masklen
/ 8);
6552 mask
.s6_addr
[masklen
/ 8] =
6553 (0xff << (8 - masklen
% 8)) & 0xff;
6556 a
= (u_int32_t
*)addr
;
6557 m
= (u_int32_t
*)&mask
;
6558 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6559 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6560 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6568 bpf_error("Mask syntax for networks only");
6572 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6578 bpf_error("invalid qualifier against IPv6 address");
6587 register const u_char
*eaddr
;
6590 struct block
*b
, *tmp
;
6592 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6595 case DLT_NETANALYZER
:
6596 case DLT_NETANALYZER_TRANSPARENT
:
6597 tmp
= gen_prevlinkhdr_check();
6598 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6603 return gen_fhostop(eaddr
, (int)q
.dir
);
6605 return gen_thostop(eaddr
, (int)q
.dir
);
6606 case DLT_IEEE802_11
:
6607 case DLT_PRISM_HEADER
:
6608 case DLT_IEEE802_11_RADIO_AVS
:
6609 case DLT_IEEE802_11_RADIO
:
6611 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6612 case DLT_IP_OVER_FC
:
6613 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6615 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6619 bpf_error("ethernet address used in non-ether expression");
6626 struct slist
*s0
, *s1
;
6629 * This is definitely not the best way to do this, but the
6630 * lists will rarely get long.
6637 static struct slist
*
6643 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6648 static struct slist
*
6654 s
= new_stmt(BPF_LD
|BPF_MEM
);
6660 * Modify "index" to use the value stored into its register as an
6661 * offset relative to the beginning of the header for the protocol
6662 * "proto", and allocate a register and put an item "size" bytes long
6663 * (1, 2, or 4) at that offset into that register, making it the register
6667 gen_load(proto
, inst
, size
)
6672 struct slist
*s
, *tmp
;
6674 int regno
= alloc_reg();
6676 free_reg(inst
->regno
);
6680 bpf_error("data size must be 1, 2, or 4");
6696 bpf_error("unsupported index operation");
6700 * The offset is relative to the beginning of the packet
6701 * data, if we have a radio header. (If we don't, this
6704 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6705 linktype
!= DLT_IEEE802_11_RADIO
&&
6706 linktype
!= DLT_PRISM_HEADER
)
6707 bpf_error("radio information not present in capture");
6710 * Load into the X register the offset computed into the
6711 * register specified by "index".
6713 s
= xfer_to_x(inst
);
6716 * Load the item at that offset.
6718 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6720 sappend(inst
->s
, s
);
6725 * The offset is relative to the beginning of
6726 * the link-layer header.
6728 * XXX - what about ATM LANE? Should the index be
6729 * relative to the beginning of the AAL5 frame, so
6730 * that 0 refers to the beginning of the LE Control
6731 * field, or relative to the beginning of the LAN
6732 * frame, so that 0 refers, for Ethernet LANE, to
6733 * the beginning of the destination address?
6735 s
= gen_abs_offset_varpart(&off_linkhdr
);
6738 * If "s" is non-null, it has code to arrange that the
6739 * X register contains the length of the prefix preceding
6740 * the link-layer header. Add to it the offset computed
6741 * into the register specified by "index", and move that
6742 * into the X register. Otherwise, just load into the X
6743 * register the offset computed into the register specified
6747 sappend(s
, xfer_to_a(inst
));
6748 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6749 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6751 s
= xfer_to_x(inst
);
6754 * Load the item at the sum of the offset we've put in the
6755 * X register and the offset of the start of the link
6756 * layer header (which is 0 if the radio header is
6757 * variable-length; that header length is what we put
6758 * into the X register and then added to the index).
6760 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6761 tmp
->s
.k
= off_linkhdr
.constant_part
;
6763 sappend(inst
->s
, s
);
6777 * The offset is relative to the beginning of
6778 * the network-layer header.
6779 * XXX - are there any cases where we want
6782 s
= gen_abs_offset_varpart(&off_linkpl
);
6785 * If "s" is non-null, it has code to arrange that the
6786 * X register contains the variable part of the offset
6787 * of the link-layer payload. Add to it the offset
6788 * computed into the register specified by "index",
6789 * and move that into the X register. Otherwise, just
6790 * load into the X register the offset computed into
6791 * the register specified by "index".
6794 sappend(s
, xfer_to_a(inst
));
6795 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6796 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6798 s
= xfer_to_x(inst
);
6801 * Load the item at the sum of the offset we've put in the
6802 * X register, the offset of the start of the network
6803 * layer header from the beginning of the link-layer
6804 * payload, and the constant part of the offset of the
6805 * start of the link-layer payload.
6807 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6808 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6810 sappend(inst
->s
, s
);
6813 * Do the computation only if the packet contains
6814 * the protocol in question.
6816 b
= gen_proto_abbrev(proto
);
6818 gen_and(inst
->b
, b
);
6832 * The offset is relative to the beginning of
6833 * the transport-layer header.
6835 * Load the X register with the length of the IPv4 header
6836 * (plus the offset of the link-layer header, if it's
6837 * a variable-length header), in bytes.
6839 * XXX - are there any cases where we want
6841 * XXX - we should, if we're built with
6842 * IPv6 support, generate code to load either
6843 * IPv4, IPv6, or both, as appropriate.
6845 s
= gen_loadx_iphdrlen();
6848 * The X register now contains the sum of the variable
6849 * part of the offset of the link-layer payload and the
6850 * length of the network-layer header.
6852 * Load into the A register the offset relative to
6853 * the beginning of the transport layer header,
6854 * add the X register to that, move that to the
6855 * X register, and load with an offset from the
6856 * X register equal to the sum of the constant part of
6857 * the offset of the link-layer payload and the offset,
6858 * relative to the beginning of the link-layer payload,
6859 * of the network-layer header.
6861 sappend(s
, xfer_to_a(inst
));
6862 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6863 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6864 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6865 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6866 sappend(inst
->s
, s
);
6869 * Do the computation only if the packet contains
6870 * the protocol in question - which is true only
6871 * if this is an IP datagram and is the first or
6872 * only fragment of that datagram.
6874 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6876 gen_and(inst
->b
, b
);
6877 gen_and(gen_proto_abbrev(Q_IP
), b
);
6881 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6884 inst
->regno
= regno
;
6885 s
= new_stmt(BPF_ST
);
6887 sappend(inst
->s
, s
);
6893 gen_relation(code
, a0
, a1
, reversed
)
6895 struct arth
*a0
, *a1
;
6898 struct slist
*s0
, *s1
, *s2
;
6899 struct block
*b
, *tmp
;
6903 if (code
== BPF_JEQ
) {
6904 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6905 b
= new_block(JMP(code
));
6909 b
= new_block(BPF_JMP
|code
|BPF_X
);
6915 sappend(a0
->s
, a1
->s
);
6919 free_reg(a0
->regno
);
6920 free_reg(a1
->regno
);
6922 /* 'and' together protocol checks */
6925 gen_and(a0
->b
, tmp
= a1
->b
);
6941 int regno
= alloc_reg();
6942 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6945 s
= new_stmt(BPF_LD
|BPF_LEN
);
6946 s
->next
= new_stmt(BPF_ST
);
6947 s
->next
->s
.k
= regno
;
6962 a
= (struct arth
*)newchunk(sizeof(*a
));
6966 s
= new_stmt(BPF_LD
|BPF_IMM
);
6968 s
->next
= new_stmt(BPF_ST
);
6984 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6987 s
= new_stmt(BPF_ST
);
6995 gen_arth(code
, a0
, a1
)
6997 struct arth
*a0
, *a1
;
6999 struct slist
*s0
, *s1
, *s2
;
7003 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7008 sappend(a0
->s
, a1
->s
);
7010 free_reg(a0
->regno
);
7011 free_reg(a1
->regno
);
7013 s0
= new_stmt(BPF_ST
);
7014 a0
->regno
= s0
->s
.k
= alloc_reg();
7021 * Here we handle simple allocation of the scratch registers.
7022 * If too many registers are alloc'd, the allocator punts.
7024 static int regused
[BPF_MEMWORDS
];
7028 * Initialize the table of used registers and the current register.
7034 memset(regused
, 0, sizeof regused
);
7038 * Return the next free register.
7043 int n
= BPF_MEMWORDS
;
7046 if (regused
[curreg
])
7047 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7049 regused
[curreg
] = 1;
7053 bpf_error("too many registers needed to evaluate expression");
7059 * Return a register to the table so it can
7069 static struct block
*
7076 s
= new_stmt(BPF_LD
|BPF_LEN
);
7077 b
= new_block(JMP(jmp
));
7088 return gen_len(BPF_JGE
, n
);
7092 * Actually, this is less than or equal.
7100 b
= gen_len(BPF_JGT
, n
);
7107 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7108 * the beginning of the link-layer header.
7109 * XXX - that means you can't test values in the radiotap header, but
7110 * as that header is difficult if not impossible to parse generally
7111 * without a loop, that might not be a severe problem. A new keyword
7112 * "radio" could be added for that, although what you'd really want
7113 * would be a way of testing particular radio header values, which
7114 * would generate code appropriate to the radio header in question.
7117 gen_byteop(op
, idx
, val
)
7128 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7131 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7135 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7139 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7143 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7147 b
= new_block(JMP(BPF_JEQ
));
7154 static u_char abroadcast
[] = { 0x0 };
7157 gen_broadcast(proto
)
7160 bpf_u_int32 hostmask
;
7161 struct block
*b0
, *b1
, *b2
;
7162 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7170 case DLT_ARCNET_LINUX
:
7171 return gen_ahostop(abroadcast
, Q_DST
);
7173 case DLT_NETANALYZER
:
7174 case DLT_NETANALYZER_TRANSPARENT
:
7175 b1
= gen_prevlinkhdr_check();
7176 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7181 return gen_fhostop(ebroadcast
, Q_DST
);
7183 return gen_thostop(ebroadcast
, Q_DST
);
7184 case DLT_IEEE802_11
:
7185 case DLT_PRISM_HEADER
:
7186 case DLT_IEEE802_11_RADIO_AVS
:
7187 case DLT_IEEE802_11_RADIO
:
7189 return gen_wlanhostop(ebroadcast
, Q_DST
);
7190 case DLT_IP_OVER_FC
:
7191 return gen_ipfchostop(ebroadcast
, Q_DST
);
7193 bpf_error("not a broadcast link");
7199 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7200 * as an indication that we don't know the netmask, and fail
7203 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7204 bpf_error("netmask not known, so 'ip broadcast' not supported");
7205 b0
= gen_linktype(ETHERTYPE_IP
);
7206 hostmask
= ~netmask
;
7207 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7208 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7209 (bpf_int32
)(~0 & hostmask
), hostmask
);
7214 bpf_error("only link-layer/IP broadcast filters supported");
7220 * Generate code to test the low-order bit of a MAC address (that's
7221 * the bottom bit of the *first* byte).
7223 static struct block
*
7224 gen_mac_multicast(offset
)
7227 register struct block
*b0
;
7228 register struct slist
*s
;
7230 /* link[offset] & 1 != 0 */
7231 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7232 b0
= new_block(JMP(BPF_JSET
));
7239 gen_multicast(proto
)
7242 register struct block
*b0
, *b1
, *b2
;
7243 register struct slist
*s
;
7251 case DLT_ARCNET_LINUX
:
7252 /* all ARCnet multicasts use the same address */
7253 return gen_ahostop(abroadcast
, Q_DST
);
7255 case DLT_NETANALYZER
:
7256 case DLT_NETANALYZER_TRANSPARENT
:
7257 b1
= gen_prevlinkhdr_check();
7258 /* ether[0] & 1 != 0 */
7259 b0
= gen_mac_multicast(0);
7265 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7267 * XXX - was that referring to bit-order issues?
7269 /* fddi[1] & 1 != 0 */
7270 return gen_mac_multicast(1);
7272 /* tr[2] & 1 != 0 */
7273 return gen_mac_multicast(2);
7274 case DLT_IEEE802_11
:
7275 case DLT_PRISM_HEADER
:
7276 case DLT_IEEE802_11_RADIO_AVS
:
7277 case DLT_IEEE802_11_RADIO
:
7282 * For control frames, there is no DA.
7284 * For management frames, DA is at an
7285 * offset of 4 from the beginning of
7288 * For data frames, DA is at an offset
7289 * of 4 from the beginning of the packet
7290 * if To DS is clear and at an offset of
7291 * 16 from the beginning of the packet
7296 * Generate the tests to be done for data frames.
7298 * First, check for To DS set, i.e. "link[1] & 0x01".
7300 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7301 b1
= new_block(JMP(BPF_JSET
));
7302 b1
->s
.k
= 0x01; /* To DS */
7306 * If To DS is set, the DA is at 16.
7308 b0
= gen_mac_multicast(16);
7312 * Now, check for To DS not set, i.e. check
7313 * "!(link[1] & 0x01)".
7315 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7316 b2
= new_block(JMP(BPF_JSET
));
7317 b2
->s
.k
= 0x01; /* To DS */
7322 * If To DS is not set, the DA is at 4.
7324 b1
= gen_mac_multicast(4);
7328 * Now OR together the last two checks. That gives
7329 * the complete set of checks for data frames.
7334 * Now check for a data frame.
7335 * I.e, check "link[0] & 0x08".
7337 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7338 b1
= new_block(JMP(BPF_JSET
));
7343 * AND that with the checks done for data frames.
7348 * If the high-order bit of the type value is 0, this
7349 * is a management frame.
7350 * I.e, check "!(link[0] & 0x08)".
7352 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7353 b2
= new_block(JMP(BPF_JSET
));
7359 * For management frames, the DA is at 4.
7361 b1
= gen_mac_multicast(4);
7365 * OR that with the checks done for data frames.
7366 * That gives the checks done for management and
7372 * If the low-order bit of the type value is 1,
7373 * this is either a control frame or a frame
7374 * with a reserved type, and thus not a
7377 * I.e., check "!(link[0] & 0x04)".
7379 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7380 b1
= new_block(JMP(BPF_JSET
));
7386 * AND that with the checks for data and management
7391 case DLT_IP_OVER_FC
:
7392 b0
= gen_mac_multicast(2);
7397 /* Link not known to support multicasts */
7401 b0
= gen_linktype(ETHERTYPE_IP
);
7402 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7407 b0
= gen_linktype(ETHERTYPE_IPV6
);
7408 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7412 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7418 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7419 * Outbound traffic is sent by this machine, while inbound traffic is
7420 * sent by a remote machine (and may include packets destined for a
7421 * unicast or multicast link-layer address we are not subscribing to).
7422 * These are the same definitions implemented by pcap_setdirection().
7423 * Capturing only unicast traffic destined for this host is probably
7424 * better accomplished using a higher-layer filter.
7430 register struct block
*b0
;
7433 * Only some data link types support inbound/outbound qualifiers.
7437 b0
= gen_relation(BPF_JEQ
,
7438 gen_load(Q_LINK
, gen_loadi(0), 1),
7445 /* match outgoing packets */
7446 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7448 /* match incoming packets */
7449 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7454 /* match outgoing packets */
7455 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7457 /* to filter on inbound traffic, invert the match */
7462 #ifdef HAVE_NET_PFVAR_H
7464 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7465 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7471 /* match outgoing packets */
7472 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7474 /* match incoming packets */
7475 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7479 case DLT_JUNIPER_MFR
:
7480 case DLT_JUNIPER_MLFR
:
7481 case DLT_JUNIPER_MLPPP
:
7482 case DLT_JUNIPER_ATM1
:
7483 case DLT_JUNIPER_ATM2
:
7484 case DLT_JUNIPER_PPPOE
:
7485 case DLT_JUNIPER_PPPOE_ATM
:
7486 case DLT_JUNIPER_GGSN
:
7487 case DLT_JUNIPER_ES
:
7488 case DLT_JUNIPER_MONITOR
:
7489 case DLT_JUNIPER_SERVICES
:
7490 case DLT_JUNIPER_ETHER
:
7491 case DLT_JUNIPER_PPP
:
7492 case DLT_JUNIPER_FRELAY
:
7493 case DLT_JUNIPER_CHDLC
:
7494 case DLT_JUNIPER_VP
:
7495 case DLT_JUNIPER_ST
:
7496 case DLT_JUNIPER_ISM
:
7497 case DLT_JUNIPER_VS
:
7498 case DLT_JUNIPER_SRX_E2E
:
7499 case DLT_JUNIPER_FIBRECHANNEL
:
7500 case DLT_JUNIPER_ATM_CEMIC
:
7502 /* juniper flags (including direction) are stored
7503 * the byte after the 3-byte magic number */
7505 /* match outgoing packets */
7506 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7508 /* match incoming packets */
7509 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7515 * If we have packet meta-data indicating a direction,
7516 * check it, otherwise give up as this link-layer type
7517 * has nothing in the packet data.
7519 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7521 * This is Linux with PF_PACKET support.
7522 * If this is a *live* capture, we can look at
7523 * special meta-data in the filter expression;
7524 * if it's a savefile, we can't.
7526 if (bpf_pcap
->rfile
!= NULL
) {
7527 /* We have a FILE *, so this is a savefile */
7528 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7533 /* match outgoing packets */
7534 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7537 /* to filter on inbound traffic, invert the match */
7540 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7541 bpf_error("inbound/outbound not supported on linktype %d",
7545 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7550 #ifdef HAVE_NET_PFVAR_H
7551 /* PF firewall log matched interface */
7553 gen_pf_ifname(const char *ifname
)
7558 if (linktype
!= DLT_PFLOG
) {
7559 bpf_error("ifname supported only on PF linktype");
7562 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7563 off
= offsetof(struct pfloghdr
, ifname
);
7564 if (strlen(ifname
) >= len
) {
7565 bpf_error("ifname interface names can only be %d characters",
7569 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7573 /* PF firewall log ruleset name */
7575 gen_pf_ruleset(char *ruleset
)
7579 if (linktype
!= DLT_PFLOG
) {
7580 bpf_error("ruleset supported only on PF linktype");
7584 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7585 bpf_error("ruleset names can only be %ld characters",
7586 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7590 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7591 strlen(ruleset
), (const u_char
*)ruleset
);
7595 /* PF firewall log rule number */
7601 if (linktype
!= DLT_PFLOG
) {
7602 bpf_error("rnr supported only on PF linktype");
7606 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7611 /* PF firewall log sub-rule number */
7613 gen_pf_srnr(int srnr
)
7617 if (linktype
!= DLT_PFLOG
) {
7618 bpf_error("srnr supported only on PF linktype");
7622 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7627 /* PF firewall log reason code */
7629 gen_pf_reason(int reason
)
7633 if (linktype
!= DLT_PFLOG
) {
7634 bpf_error("reason supported only on PF linktype");
7638 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7643 /* PF firewall log action */
7645 gen_pf_action(int action
)
7649 if (linktype
!= DLT_PFLOG
) {
7650 bpf_error("action supported only on PF linktype");
7654 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7658 #else /* !HAVE_NET_PFVAR_H */
7660 gen_pf_ifname(const char *ifname
)
7662 bpf_error("libpcap was compiled without pf support");
7668 gen_pf_ruleset(char *ruleset
)
7670 bpf_error("libpcap was compiled on a machine without pf support");
7678 bpf_error("libpcap was compiled on a machine without pf support");
7684 gen_pf_srnr(int srnr
)
7686 bpf_error("libpcap was compiled on a machine without pf support");
7692 gen_pf_reason(int reason
)
7694 bpf_error("libpcap was compiled on a machine without pf support");
7700 gen_pf_action(int action
)
7702 bpf_error("libpcap was compiled on a machine without pf support");
7706 #endif /* HAVE_NET_PFVAR_H */
7708 /* IEEE 802.11 wireless header */
7710 gen_p80211_type(int type
, int mask
)
7716 case DLT_IEEE802_11
:
7717 case DLT_PRISM_HEADER
:
7718 case DLT_IEEE802_11_RADIO_AVS
:
7719 case DLT_IEEE802_11_RADIO
:
7720 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7725 bpf_error("802.11 link-layer types supported only on 802.11");
7733 gen_p80211_fcdir(int fcdir
)
7739 case DLT_IEEE802_11
:
7740 case DLT_PRISM_HEADER
:
7741 case DLT_IEEE802_11_RADIO_AVS
:
7742 case DLT_IEEE802_11_RADIO
:
7746 bpf_error("frame direction supported only with 802.11 headers");
7750 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7751 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7758 register const u_char
*eaddr
;
7764 case DLT_ARCNET_LINUX
:
7765 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7767 return (gen_ahostop(eaddr
, (int)q
.dir
));
7769 bpf_error("ARCnet address used in non-arc expression");
7775 bpf_error("aid supported only on ARCnet");
7778 bpf_error("ARCnet address used in non-arc expression");
7783 static struct block
*
7784 gen_ahostop(eaddr
, dir
)
7785 register const u_char
*eaddr
;
7788 register struct block
*b0
, *b1
;
7791 /* src comes first, different from Ethernet */
7793 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7796 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7799 b0
= gen_ahostop(eaddr
, Q_SRC
);
7800 b1
= gen_ahostop(eaddr
, Q_DST
);
7806 b0
= gen_ahostop(eaddr
, Q_SRC
);
7807 b1
= gen_ahostop(eaddr
, Q_DST
);
7812 bpf_error("'addr1' is only supported on 802.11");
7816 bpf_error("'addr2' is only supported on 802.11");
7820 bpf_error("'addr3' is only supported on 802.11");
7824 bpf_error("'addr4' is only supported on 802.11");
7828 bpf_error("'ra' is only supported on 802.11");
7832 bpf_error("'ta' is only supported on 802.11");
7839 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7840 static struct block
*
7841 gen_vlan_bpf_extensions(int vlan_num
)
7843 struct block
*b0
, *b1
;
7846 /* generate new filter code based on extracting packet
7848 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7849 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7851 b0
= new_block(JMP(BPF_JEQ
));
7855 if (vlan_num
>= 0) {
7856 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7857 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7859 b1
= new_block(JMP(BPF_JEQ
));
7861 b1
->s
.k
= (bpf_int32
) vlan_num
;
7871 static struct block
*
7872 gen_vlan_no_bpf_extensions(int vlan_num
)
7874 struct block
*b0
, *b1
;
7876 /* check for VLAN, including QinQ */
7877 b0
= gen_linktype(ETHERTYPE_8021Q
);
7878 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7882 /* If a specific VLAN is requested, check VLAN id */
7883 if (vlan_num
>= 0) {
7884 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7885 (bpf_int32
)vlan_num
, 0x0fff);
7891 * The payload follows the full header, including the
7892 * VLAN tags, so skip past this VLAN tag.
7894 off_linkpl
.constant_part
+= 4;
7897 * The link-layer type information follows the VLAN tags, so
7898 * skip past this VLAN tag.
7900 off_linktype
.constant_part
+= 4;
7906 * support IEEE 802.1Q VLAN trunk over ethernet
7914 /* can't check for VLAN-encapsulated packets inside MPLS */
7915 if (label_stack_depth
> 0)
7916 bpf_error("no VLAN match after MPLS");
7919 * Check for a VLAN packet, and then change the offsets to point
7920 * to the type and data fields within the VLAN packet. Just
7921 * increment the offsets, so that we can support a hierarchy, e.g.
7922 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7925 * XXX - this is a bit of a kludge. If we were to split the
7926 * compiler into a parser that parses an expression and
7927 * generates an expression tree, and a code generator that
7928 * takes an expression tree (which could come from our
7929 * parser or from some other parser) and generates BPF code,
7930 * we could perhaps make the offsets parameters of routines
7931 * and, in the handler for an "AND" node, pass to subnodes
7932 * other than the VLAN node the adjusted offsets.
7934 * This would mean that "vlan" would, instead of changing the
7935 * behavior of *all* tests after it, change only the behavior
7936 * of tests ANDed with it. That would change the documented
7937 * semantics of "vlan", which might break some expressions.
7938 * However, it would mean that "(vlan and ip) or ip" would check
7939 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7940 * checking only for VLAN-encapsulated IP, so that could still
7941 * be considered worth doing; it wouldn't break expressions
7942 * that are of the form "vlan and ..." or "vlan N and ...",
7943 * which I suspect are the most common expressions involving
7944 * "vlan". "vlan or ..." doesn't necessarily do what the user
7945 * would really want, now, as all the "or ..." tests would
7946 * be done assuming a VLAN, even though the "or" could be viewed
7947 * as meaning "or, if this isn't a VLAN packet...".
7952 case DLT_NETANALYZER
:
7953 case DLT_NETANALYZER_TRANSPARENT
:
7954 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7955 /* Verify that this is the outer part of the packet and
7956 * not encapsulated somehow. */
7957 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
7958 off_linkhdr
.constant_part
==
7959 off_outermostlinkhdr
.constant_part
) {
7961 * Do we need special VLAN handling?
7963 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
7964 b0
= gen_vlan_bpf_extensions(vlan_num
);
7966 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7969 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7972 case DLT_IEEE802_11
:
7973 case DLT_PRISM_HEADER
:
7974 case DLT_IEEE802_11_RADIO_AVS
:
7975 case DLT_IEEE802_11_RADIO
:
7976 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7980 bpf_error("no VLAN support for data link type %d",
7997 struct block
*b0
, *b1
;
7999 if (label_stack_depth
> 0) {
8000 /* just match the bottom-of-stack bit clear */
8001 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8004 * We're not in an MPLS stack yet, so check the link-layer
8005 * type against MPLS.
8009 case DLT_C_HDLC
: /* fall through */
8011 case DLT_NETANALYZER
:
8012 case DLT_NETANALYZER_TRANSPARENT
:
8013 b0
= gen_linktype(ETHERTYPE_MPLS
);
8017 b0
= gen_linktype(PPP_MPLS_UCAST
);
8020 /* FIXME add other DLT_s ...
8021 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8022 * leave it for now */
8025 bpf_error("no MPLS support for data link type %d",
8033 /* If a specific MPLS label is requested, check it */
8034 if (label_num
>= 0) {
8035 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8036 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8037 0xfffff000); /* only compare the first 20 bits */
8043 * Change the offsets to point to the type and data fields within
8044 * the MPLS packet. Just increment the offsets, so that we
8045 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8046 * capture packets with an outer label of 100000 and an inner
8049 * Increment the MPLS stack depth as well; this indicates that
8050 * we're checking MPLS-encapsulated headers, to make sure higher
8051 * level code generators don't try to match against IP-related
8052 * protocols such as Q_ARP, Q_RARP etc.
8054 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8058 label_stack_depth
++;
8063 * Support PPPOE discovery and session.
8068 /* check for PPPoE discovery */
8069 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8073 gen_pppoes(sess_num
)
8076 struct block
*b0
, *b1
;
8079 * Test against the PPPoE session link-layer type.
8081 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8083 /* If a specific session is requested, check PPPoE session id */
8084 if (sess_num
>= 0) {
8085 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8086 (bpf_int32
)sess_num
, 0x0000ffff);
8092 * Change the offsets to point to the type and data fields within
8093 * the PPP packet, and note that this is PPPoE rather than
8096 * XXX - this is a bit of a kludge. If we were to split the
8097 * compiler into a parser that parses an expression and
8098 * generates an expression tree, and a code generator that
8099 * takes an expression tree (which could come from our
8100 * parser or from some other parser) and generates BPF code,
8101 * we could perhaps make the offsets parameters of routines
8102 * and, in the handler for an "AND" node, pass to subnodes
8103 * other than the PPPoE node the adjusted offsets.
8105 * This would mean that "pppoes" would, instead of changing the
8106 * behavior of *all* tests after it, change only the behavior
8107 * of tests ANDed with it. That would change the documented
8108 * semantics of "pppoes", which might break some expressions.
8109 * However, it would mean that "(pppoes and ip) or ip" would check
8110 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8111 * checking only for VLAN-encapsulated IP, so that could still
8112 * be considered worth doing; it wouldn't break expressions
8113 * that are of the form "pppoes and ..." which I suspect are the
8114 * most common expressions involving "pppoes". "pppoes or ..."
8115 * doesn't necessarily do what the user would really want, now,
8116 * as all the "or ..." tests would be done assuming PPPoE, even
8117 * though the "or" could be viewed as meaning "or, if this isn't
8118 * a PPPoE packet...".
8120 * The "network-layer" protocol is PPPoE, which has a 6-byte
8121 * PPPoE header, followed by a PPP packet.
8123 * There is no HDLC encapsulation for the PPP packet (it's
8124 * encapsulated in PPPoES instead), so the link-layer type
8125 * starts at the first byte of the PPP packet. For PPPoE,
8126 * that offset is relative to the beginning of the total
8127 * link-layer payload, including any 802.2 LLC header, so
8128 * it's 6 bytes past off_nl.
8130 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8131 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8134 off_linktype
= off_linkhdr
;
8135 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8138 off_nl_nosnap
= 0; /* no 802.2 LLC */
8144 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8156 bpf_error("'vpi' supported only on raw ATM");
8157 if (off_vpi
== (u_int
)-1)
8159 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8165 bpf_error("'vci' supported only on raw ATM");
8166 if (off_vci
== (u_int
)-1)
8168 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8173 if (off_proto
== (u_int
)-1)
8174 abort(); /* XXX - this isn't on FreeBSD */
8175 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8180 if (off_payload
== (u_int
)-1)
8182 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8183 0xffffffff, jtype
, reverse
, jvalue
);
8188 bpf_error("'callref' supported only on raw ATM");
8189 if (off_proto
== (u_int
)-1)
8191 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8192 jtype
, reverse
, jvalue
);
8202 gen_atmtype_abbrev(type
)
8205 struct block
*b0
, *b1
;
8210 /* Get all packets in Meta signalling Circuit */
8212 bpf_error("'metac' supported only on raw ATM");
8213 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8214 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8219 /* Get all packets in Broadcast Circuit*/
8221 bpf_error("'bcc' supported only on raw ATM");
8222 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8223 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8228 /* Get all cells in Segment OAM F4 circuit*/
8230 bpf_error("'oam4sc' supported only on raw ATM");
8231 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8232 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8237 /* Get all cells in End-to-End OAM F4 Circuit*/
8239 bpf_error("'oam4ec' supported only on raw ATM");
8240 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8241 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8246 /* Get all packets in connection Signalling Circuit */
8248 bpf_error("'sc' supported only on raw ATM");
8249 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8250 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8255 /* Get all packets in ILMI Circuit */
8257 bpf_error("'ilmic' supported only on raw ATM");
8258 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8259 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8264 /* Get all LANE packets */
8266 bpf_error("'lane' supported only on raw ATM");
8267 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8270 * Arrange that all subsequent tests assume LANE
8271 * rather than LLC-encapsulated packets, and set
8272 * the offsets appropriately for LANE-encapsulated
8275 * We assume LANE means Ethernet, not Token Ring.
8277 PUSH_LINKHDR(DLT_EN10MB
, 0,
8278 off_payload
+ 2, /* Ethernet header */
8280 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8281 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8282 off_nl
= 0; /* Ethernet II */
8283 off_nl_nosnap
= 3; /* 802.3+802.2 */
8287 /* Get all LLC-encapsulated packets */
8289 bpf_error("'llc' supported only on raw ATM");
8290 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8291 linktype
= prevlinktype
;
8301 * Filtering for MTP2 messages based on li value
8302 * FISU, length is null
8303 * LSSU, length is 1 or 2
8304 * MSU, length is 3 or more
8305 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8308 gen_mtp2type_abbrev(type
)
8311 struct block
*b0
, *b1
;
8316 if ( (linktype
!= DLT_MTP2
) &&
8317 (linktype
!= DLT_ERF
) &&
8318 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8319 bpf_error("'fisu' supported only on MTP2");
8320 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8321 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8325 if ( (linktype
!= DLT_MTP2
) &&
8326 (linktype
!= DLT_ERF
) &&
8327 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8328 bpf_error("'lssu' supported only on MTP2");
8329 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8330 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8335 if ( (linktype
!= DLT_MTP2
) &&
8336 (linktype
!= DLT_ERF
) &&
8337 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8338 bpf_error("'msu' supported only on MTP2");
8339 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8343 if ( (linktype
!= DLT_MTP2
) &&
8344 (linktype
!= DLT_ERF
) &&
8345 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8346 bpf_error("'hfisu' supported only on MTP2_HSL");
8347 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8348 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8352 if ( (linktype
!= DLT_MTP2
) &&
8353 (linktype
!= DLT_ERF
) &&
8354 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8355 bpf_error("'hlssu' supported only on MTP2_HSL");
8356 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8357 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8362 if ( (linktype
!= DLT_MTP2
) &&
8363 (linktype
!= DLT_ERF
) &&
8364 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8365 bpf_error("'hmsu' supported only on MTP2_HSL");
8366 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8376 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8383 bpf_u_int32 val1
, val2
, val3
;
8384 u_int newoff_sio
=off_sio
;
8385 u_int newoff_opc
=off_opc
;
8386 u_int newoff_dpc
=off_dpc
;
8387 u_int newoff_sls
=off_sls
;
8389 switch (mtp3field
) {
8392 newoff_sio
+= 3; /* offset for MTP2_HSL */
8396 if (off_sio
== (u_int
)-1)
8397 bpf_error("'sio' supported only on SS7");
8398 /* sio coded on 1 byte so max value 255 */
8400 bpf_error("sio value %u too big; max value = 255",
8402 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8403 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8409 if (off_opc
== (u_int
)-1)
8410 bpf_error("'opc' supported only on SS7");
8411 /* opc coded on 14 bits so max value 16383 */
8413 bpf_error("opc value %u too big; max value = 16383",
8415 /* the following instructions are made to convert jvalue
8416 * to the form used to write opc in an ss7 message*/
8417 val1
= jvalue
& 0x00003c00;
8419 val2
= jvalue
& 0x000003fc;
8421 val3
= jvalue
& 0x00000003;
8423 jvalue
= val1
+ val2
+ val3
;
8424 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8425 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8433 if (off_dpc
== (u_int
)-1)
8434 bpf_error("'dpc' supported only on SS7");
8435 /* dpc coded on 14 bits so max value 16383 */
8437 bpf_error("dpc value %u too big; max value = 16383",
8439 /* the following instructions are made to convert jvalue
8440 * to the forme used to write dpc in an ss7 message*/
8441 val1
= jvalue
& 0x000000ff;
8443 val2
= jvalue
& 0x00003f00;
8445 jvalue
= val1
+ val2
;
8446 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8447 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8453 if (off_sls
== (u_int
)-1)
8454 bpf_error("'sls' supported only on SS7");
8455 /* sls coded on 4 bits so max value 15 */
8457 bpf_error("sls value %u too big; max value = 15",
8459 /* the following instruction is made to convert jvalue
8460 * to the forme used to write sls in an ss7 message*/
8461 jvalue
= jvalue
<< 4;
8462 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8463 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8472 static struct block
*
8473 gen_msg_abbrev(type
)
8479 * Q.2931 signalling protocol messages for handling virtual circuits
8480 * establishment and teardown
8485 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8489 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8493 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8497 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8501 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8504 case A_RELEASE_DONE
:
8505 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8515 gen_atmmulti_abbrev(type
)
8518 struct block
*b0
, *b1
;
8524 bpf_error("'oam' supported only on raw ATM");
8525 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8530 bpf_error("'oamf4' supported only on raw ATM");
8532 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8533 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8535 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8541 * Get Q.2931 signalling messages for switched
8542 * virtual connection
8545 bpf_error("'connectmsg' supported only on raw ATM");
8546 b0
= gen_msg_abbrev(A_SETUP
);
8547 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8549 b0
= gen_msg_abbrev(A_CONNECT
);
8551 b0
= gen_msg_abbrev(A_CONNECTACK
);
8553 b0
= gen_msg_abbrev(A_RELEASE
);
8555 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8557 b0
= gen_atmtype_abbrev(A_SC
);
8563 bpf_error("'metaconnect' supported only on raw ATM");
8564 b0
= gen_msg_abbrev(A_SETUP
);
8565 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8567 b0
= gen_msg_abbrev(A_CONNECT
);
8569 b0
= gen_msg_abbrev(A_RELEASE
);
8571 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8573 b0
= gen_atmtype_abbrev(A_METAC
);