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_LINKPL
, /* link-layer payload */
199 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
200 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
201 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
206 * As errors are handled by a longjmp, anything allocated must be freed
207 * in the longjmp handler, so it must be reachable from that handler.
208 * One thing that's allocated is the result of pcap_nametoaddrinfo();
209 * it must be freed with freeaddrinfo(). This variable points to any
210 * addrinfo structure that would need to be freed.
212 static struct addrinfo
*ai
;
216 * We divy out chunks of memory rather than call malloc each time so
217 * we don't have to worry about leaking memory. It's probably
218 * not a big deal if all this memory was wasted but if this ever
219 * goes into a library that would probably not be a good idea.
221 * XXX - this *is* in a library....
224 #define CHUNK0SIZE 1024
230 static struct chunk chunks
[NCHUNKS
];
231 static int cur_chunk
;
233 static void *newchunk(u_int
);
234 static void freechunks(void);
235 static inline struct block
*new_block(int);
236 static inline struct slist
*new_stmt(int);
237 static struct block
*gen_retblk(int);
238 static inline void syntax(void);
240 static void backpatch(struct block
*, struct block
*);
241 static void merge(struct block
*, struct block
*);
242 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
243 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
244 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
245 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
246 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
247 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
249 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
250 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
251 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
252 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
253 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
254 static struct slist
*gen_loadx_iphdrlen(void);
255 static struct block
*gen_uncond(int);
256 static inline struct block
*gen_true(void);
257 static inline struct block
*gen_false(void);
258 static struct block
*gen_ether_linktype(int);
259 static struct block
*gen_ipnet_linktype(int);
260 static struct block
*gen_linux_sll_linktype(int);
261 static struct slist
*gen_load_prism_llprefixlen(void);
262 static struct slist
*gen_load_avs_llprefixlen(void);
263 static struct slist
*gen_load_radiotap_llprefixlen(void);
264 static struct slist
*gen_load_ppi_llprefixlen(void);
265 static void insert_compute_vloffsets(struct block
*);
266 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
267 static int ethertype_to_ppptype(int);
268 static struct block
*gen_linktype(int);
269 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
270 static struct block
*gen_llc_linktype(int);
271 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
273 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
275 static struct block
*gen_ahostop(const u_char
*, int);
276 static struct block
*gen_ehostop(const u_char
*, int);
277 static struct block
*gen_fhostop(const u_char
*, int);
278 static struct block
*gen_thostop(const u_char
*, int);
279 static struct block
*gen_wlanhostop(const u_char
*, int);
280 static struct block
*gen_ipfchostop(const u_char
*, int);
281 static struct block
*gen_dnhostop(bpf_u_int32
, int);
282 static struct block
*gen_mpls_linktype(int);
283 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
285 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
288 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
290 static struct block
*gen_ipfrag(void);
291 static struct block
*gen_portatom(int, bpf_int32
);
292 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
293 static struct block
*gen_portatom6(int, bpf_int32
);
294 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
295 struct block
*gen_portop(int, int, int);
296 static struct block
*gen_port(int, int, int);
297 struct block
*gen_portrangeop(int, int, int, int);
298 static struct block
*gen_portrange(int, int, int, int);
299 struct block
*gen_portop6(int, int, int);
300 static struct block
*gen_port6(int, int, int);
301 struct block
*gen_portrangeop6(int, int, int, int);
302 static struct block
*gen_portrange6(int, int, int, int);
303 static int lookup_proto(const char *, int);
304 static struct block
*gen_protochain(int, int, int);
305 static struct block
*gen_proto(int, int, int);
306 static struct slist
*xfer_to_x(struct arth
*);
307 static struct slist
*xfer_to_a(struct arth
*);
308 static struct block
*gen_mac_multicast(int);
309 static struct block
*gen_len(int, int);
310 static struct block
*gen_check_802_11_data_frame(void);
312 static struct block
*gen_ppi_dlt_check(void);
313 static struct block
*gen_msg_abbrev(int type
);
324 /* XXX Round up to nearest long. */
325 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
327 /* XXX Round up to structure boundary. */
331 cp
= &chunks
[cur_chunk
];
332 if (n
> cp
->n_left
) {
333 ++cp
, k
= ++cur_chunk
;
335 bpf_error("out of memory");
336 size
= CHUNK0SIZE
<< k
;
337 cp
->m
= (void *)malloc(size
);
339 bpf_error("out of memory");
340 memset((char *)cp
->m
, 0, size
);
343 bpf_error("out of memory");
346 return (void *)((char *)cp
->m
+ cp
->n_left
);
355 for (i
= 0; i
< NCHUNKS
; ++i
)
356 if (chunks
[i
].m
!= NULL
) {
363 * A strdup whose allocations are freed after code generation is over.
367 register const char *s
;
369 int n
= strlen(s
) + 1;
370 char *cp
= newchunk(n
);
376 static inline struct block
*
382 p
= (struct block
*)newchunk(sizeof(*p
));
389 static inline struct slist
*
395 p
= (struct slist
*)newchunk(sizeof(*p
));
401 static struct block
*
405 struct block
*b
= new_block(BPF_RET
|BPF_K
);
414 bpf_error("syntax error in filter expression");
417 static bpf_u_int32 netmask
;
422 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
423 const char *buf
, int optimize
, bpf_u_int32 mask
)
426 const char * volatile xbuf
= buf
;
431 * XXX - single-thread this code path with pthread calls on
432 * UN*X, if the platform supports pthreads? If that requires
433 * a separate -lpthread, we might not want to do that.
436 extern int wsockinit (void);
442 EnterCriticalSection(&g_PcapCompileCriticalSection
);
446 * If this pcap_t hasn't been activated, it doesn't have a
447 * link-layer type, so we can't use it.
450 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
451 "not-yet-activated pcap_t passed to pcap_compile");
461 if (setjmp(top_ctx
)) {
476 snaplen
= pcap_snapshot(p
);
478 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
479 "snaplen of 0 rejects all packets");
484 lex_init(xbuf
? xbuf
: "");
492 root
= gen_retblk(snaplen
);
494 if (optimize
&& !no_optimize
) {
497 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
498 bpf_error("expression rejects all packets");
500 program
->bf_insns
= icode_to_fcode(root
, &len
);
501 program
->bf_len
= len
;
506 rc
= 0; /* We're all okay */
511 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
518 * entry point for using the compiler with no pcap open
519 * pass in all the stuff that is needed explicitly instead.
522 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
523 struct bpf_program
*program
,
524 const char *buf
, int optimize
, bpf_u_int32 mask
)
529 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
532 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
538 * Clean up a "struct bpf_program" by freeing all the memory allocated
542 pcap_freecode(struct bpf_program
*program
)
545 if (program
->bf_insns
!= NULL
) {
546 free((char *)program
->bf_insns
);
547 program
->bf_insns
= NULL
;
552 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
553 * which of the jt and jf fields has been resolved and which is a pointer
554 * back to another unresolved block (or nil). At least one of the fields
555 * in each block is already resolved.
558 backpatch(list
, target
)
559 struct block
*list
, *target
;
576 * Merge the lists in b0 and b1, using the 'sense' field to indicate
577 * which of jt and jf is the link.
581 struct block
*b0
, *b1
;
583 register struct block
**p
= &b0
;
585 /* Find end of list. */
587 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
589 /* Concatenate the lists. */
597 struct block
*ppi_dlt_check
;
600 * Insert before the statements of the first (root) block any
601 * statements needed to load the lengths of any variable-length
602 * headers into registers.
604 * XXX - a fancier strategy would be to insert those before the
605 * statements of all blocks that use those lengths and that
606 * have no predecessors that use them, so that we only compute
607 * the lengths if we need them. There might be even better
608 * approaches than that.
610 * However, those strategies would be more complicated, and
611 * as we don't generate code to compute a length if the
612 * program has no tests that use the length, and as most
613 * tests will probably use those lengths, we would just
614 * postpone computing the lengths so that it's not done
615 * for tests that fail early, and it's not clear that's
618 insert_compute_vloffsets(p
->head
);
621 * For DLT_PPI captures, generate a check of the per-packet
622 * DLT value to make sure it's DLT_IEEE802_11.
624 ppi_dlt_check
= gen_ppi_dlt_check();
625 if (ppi_dlt_check
!= NULL
)
626 gen_and(ppi_dlt_check
, p
);
628 backpatch(p
, gen_retblk(snaplen
));
629 p
->sense
= !p
->sense
;
630 backpatch(p
, gen_retblk(0));
636 struct block
*b0
, *b1
;
638 backpatch(b0
, b1
->head
);
639 b0
->sense
= !b0
->sense
;
640 b1
->sense
= !b1
->sense
;
642 b1
->sense
= !b1
->sense
;
648 struct block
*b0
, *b1
;
650 b0
->sense
= !b0
->sense
;
651 backpatch(b0
, b1
->head
);
652 b0
->sense
= !b0
->sense
;
661 b
->sense
= !b
->sense
;
664 static struct block
*
665 gen_cmp(offrel
, offset
, size
, v
)
666 enum e_offrel offrel
;
670 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
673 static struct block
*
674 gen_cmp_gt(offrel
, offset
, size
, v
)
675 enum e_offrel offrel
;
679 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
682 static struct block
*
683 gen_cmp_ge(offrel
, offset
, size
, v
)
684 enum e_offrel offrel
;
688 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
691 static struct block
*
692 gen_cmp_lt(offrel
, offset
, size
, v
)
693 enum e_offrel offrel
;
697 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
700 static struct block
*
701 gen_cmp_le(offrel
, offset
, size
, v
)
702 enum e_offrel offrel
;
706 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
709 static struct block
*
710 gen_mcmp(offrel
, offset
, size
, v
, mask
)
711 enum e_offrel offrel
;
716 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
719 static struct block
*
720 gen_bcmp(offrel
, offset
, size
, v
)
721 enum e_offrel offrel
;
722 register u_int offset
, size
;
723 register const u_char
*v
;
725 register struct block
*b
, *tmp
;
729 register const u_char
*p
= &v
[size
- 4];
730 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
731 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
733 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
740 register const u_char
*p
= &v
[size
- 2];
741 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
743 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
750 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
759 * AND the field of size "size" at offset "offset" relative to the header
760 * specified by "offrel" with "mask", and compare it with the value "v"
761 * with the test specified by "jtype"; if "reverse" is true, the test
762 * should test the opposite of "jtype".
764 static struct block
*
765 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
766 enum e_offrel offrel
;
768 bpf_u_int32 offset
, size
, mask
, jtype
;
771 struct slist
*s
, *s2
;
774 s
= gen_load_a(offrel
, offset
, size
);
776 if (mask
!= 0xffffffff) {
777 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
782 b
= new_block(JMP(jtype
));
785 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
791 * Various code constructs need to know the layout of the packet.
792 * These variables give the necessary offsets from the beginning
793 * of the packet data.
797 * Absolute offset of the beginning of the link-layer header.
799 static bpf_abs_offset off_linkhdr
;
802 * If we're checking a link-layer header for a packet encapsulated in
803 * another protocol layer, this is the equivalent information for the
804 * previous layers' link-layer header from the beginning of the raw
807 static bpf_abs_offset off_prevlinkhdr
;
810 * This is the equivalent information for the outermost layers' link-layer
813 static bpf_abs_offset off_outermostlinkhdr
;
816 * "Push" the current value of the link-layer header type and link-layer
817 * header offset onto a "stack", and set a new value. (It's not a
818 * full-blown stack; we keep only the top two items.)
820 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
822 prevlinktype = new_linktype; \
823 off_prevlinkhdr = off_linkhdr; \
824 linktype = new_linktype; \
825 off_linkhdr.is_variable = new_is_variable; \
826 off_linkhdr.constant_part = new_constant_part; \
827 off_linkhdr.reg = new_reg; \
831 * Absolute offset of the beginning of the link-layer payload.
833 static bpf_abs_offset off_linkpl
;
836 * "off_linktype" is the offset to information in the link-layer header
837 * giving the packet type. This offset is relative to the beginning
838 * of the link-layer header - i.e., it doesn't include off_linkhdr.constant_part - so
839 * loads with an offset that includes "off_linktype" should use
842 * For Ethernet, it's the offset of the Ethernet type field; this
843 * means that it must have a value that skips VLAN tags.
845 * For link-layer types that always use 802.2 headers, it's the
846 * offset of the LLC header; this means that it must have a value
847 * that skips VLAN tags.
849 * For PPP, it's the offset of the PPP type field.
851 * For Cisco HDLC, it's the offset of the CHDLC type field.
853 * For BSD loopback, it's the offset of the AF_ value.
855 * For Linux cooked sockets, it's the offset of the type field.
857 * It's set to -1 for no encapsulation, in which case, IP is assumed.
859 static u_int 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;
956 * Assume it's not raw ATM with a pseudo-header, for now.
965 * And assume we're not doing SS7.
974 label_stack_depth
= 0;
975 vlan_stack_depth
= 0;
981 off_linkpl
.constant_part
= 6;
982 off_nl
= 0; /* XXX in reality, variable! */
983 off_nl_nosnap
= 0; /* no 802.2 LLC */
986 case DLT_ARCNET_LINUX
:
988 off_linkpl
.constant_part
= 8;
989 off_nl
= 0; /* XXX in reality, variable! */
990 off_nl_nosnap
= 0; /* no 802.2 LLC */
995 off_linkpl
.constant_part
= 14; /* Ethernet header length */
996 off_nl
= 0; /* Ethernet II */
997 off_nl_nosnap
= 3; /* 802.3+802.2 */
1002 * SLIP doesn't have a link level type. The 16 byte
1003 * header is hacked into our SLIP driver.
1006 off_linkpl
.constant_part
= 16;
1008 off_nl_nosnap
= 0; /* no 802.2 LLC */
1011 case DLT_SLIP_BSDOS
:
1012 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1015 off_linkpl
.constant_part
= 24;
1017 off_nl_nosnap
= 0; /* no 802.2 LLC */
1023 off_linkpl
.constant_part
= 4;
1025 off_nl_nosnap
= 0; /* no 802.2 LLC */
1030 off_linkpl
.constant_part
= 12;
1032 off_nl_nosnap
= 0; /* no 802.2 LLC */
1037 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1038 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1039 off_linktype
= 2; /* skip HDLC-like framing */
1040 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1042 off_nl_nosnap
= 0; /* no 802.2 LLC */
1047 * This does no include the Ethernet header, and
1048 * only covers session state.
1051 off_linkpl
.constant_part
= 8;
1053 off_nl_nosnap
= 0; /* no 802.2 LLC */
1058 off_linkpl
.constant_part
= 24;
1060 off_nl_nosnap
= 0; /* no 802.2 LLC */
1065 * FDDI doesn't really have a link-level type field.
1066 * We set "off_linktype" to the offset of the LLC header.
1068 * To check for Ethernet types, we assume that SSAP = SNAP
1069 * is being used and pick out the encapsulated Ethernet type.
1070 * XXX - should we generate code to check for SNAP?
1073 off_linktype
+= pcap_fddipad
;
1074 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1075 off_linkpl
.constant_part
+= pcap_fddipad
;
1076 off_nl
= 8; /* 802.2+SNAP */
1077 off_nl_nosnap
= 3; /* 802.2 */
1082 * Token Ring doesn't really have a link-level type field.
1083 * We set "off_linktype" to the offset of the LLC header.
1085 * To check for Ethernet types, we assume that SSAP = SNAP
1086 * is being used and pick out the encapsulated Ethernet type.
1087 * XXX - should we generate code to check for SNAP?
1089 * XXX - the header is actually variable-length.
1090 * Some various Linux patched versions gave 38
1091 * as "off_linktype" and 40 as "off_nl"; however,
1092 * if a token ring packet has *no* routing
1093 * information, i.e. is not source-routed, the correct
1094 * values are 20 and 22, as they are in the vanilla code.
1096 * A packet is source-routed iff the uppermost bit
1097 * of the first byte of the source address, at an
1098 * offset of 8, has the uppermost bit set. If the
1099 * packet is source-routed, the total number of bytes
1100 * of routing information is 2 plus bits 0x1F00 of
1101 * the 16-bit value at an offset of 14 (shifted right
1102 * 8 - figure out which byte that is).
1105 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1106 off_nl
= 8; /* 802.2+SNAP */
1107 off_nl_nosnap
= 3; /* 802.2 */
1110 case DLT_IEEE802_11
:
1111 case DLT_PRISM_HEADER
:
1112 case DLT_IEEE802_11_RADIO_AVS
:
1113 case DLT_IEEE802_11_RADIO
:
1115 * 802.11 doesn't really have a link-level type field.
1116 * We set "off_linktype" to the offset of the LLC header.
1118 * To check for Ethernet types, we assume that SSAP = SNAP
1119 * is being used and pick out the encapsulated Ethernet type.
1120 * XXX - should we generate code to check for SNAP?
1122 * We also handle variable-length radio headers here.
1123 * The Prism header is in theory variable-length, but in
1124 * practice it's always 144 bytes long. However, some
1125 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1126 * sometimes or always supply an AVS header, so we
1127 * have to check whether the radio header is a Prism
1128 * header or an AVS header, so, in practice, it's
1132 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1133 off_linkpl
.is_variable
= 1;
1134 off_nl
= 8; /* 802.2+SNAP */
1135 off_nl_nosnap
= 3; /* 802.2 */
1140 * At the moment we treat PPI the same way that we treat
1141 * normal Radiotap encoded packets. The difference is in
1142 * the function that generates the code at the beginning
1143 * to compute the header length. Since this code generator
1144 * of PPI supports bare 802.11 encapsulation only (i.e.
1145 * the encapsulated DLT should be DLT_IEEE802_11) we
1146 * generate code to check for this too.
1149 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1150 off_linkpl
.is_variable
= 1;
1151 off_nl
= 8; /* 802.2+SNAP */
1152 off_nl_nosnap
= 3; /* 802.2 */
1155 case DLT_ATM_RFC1483
:
1156 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1158 * assume routed, non-ISO PDUs
1159 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1161 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1162 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1163 * latter would presumably be treated the way PPPoE
1164 * should be, so you can do "pppoe and udp port 2049"
1165 * or "pppoa and tcp port 80" and have it check for
1166 * PPPo{A,E} and a PPP protocol of IP and....
1169 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1170 off_nl
= 8; /* 802.2+SNAP */
1171 off_nl_nosnap
= 3; /* 802.2 */
1176 * Full Frontal ATM; you get AALn PDUs with an ATM
1180 off_vpi
= SUNATM_VPI_POS
;
1181 off_vci
= SUNATM_VCI_POS
;
1182 off_proto
= PROTO_POS
;
1183 off_payload
= SUNATM_PKT_BEGIN_POS
;
1184 off_linktype
= off_payload
;
1185 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1186 off_nl
= 8; /* 802.2+SNAP */
1187 off_nl_nosnap
= 3; /* 802.2 */
1194 off_linkpl
.constant_part
= 0;
1196 off_nl_nosnap
= 0; /* no 802.2 LLC */
1199 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1201 off_linkpl
.constant_part
= 16;
1203 off_nl_nosnap
= 0; /* no 802.2 LLC */
1208 * LocalTalk does have a 1-byte type field in the LLAP header,
1209 * but really it just indicates whether there is a "short" or
1210 * "long" DDP packet following.
1213 off_linkpl
.constant_part
= 0;
1215 off_nl_nosnap
= 0; /* no 802.2 LLC */
1218 case DLT_IP_OVER_FC
:
1220 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1221 * link-level type field. We set "off_linktype" to the
1222 * offset of the LLC header.
1224 * To check for Ethernet types, we assume that SSAP = SNAP
1225 * is being used and pick out the encapsulated Ethernet type.
1226 * XXX - should we generate code to check for SNAP? RFC
1227 * 2625 says SNAP should be used.
1230 off_linkpl
.constant_part
= 16;
1231 off_nl
= 8; /* 802.2+SNAP */
1232 off_nl_nosnap
= 3; /* 802.2 */
1237 * XXX - we should set this to handle SNAP-encapsulated
1238 * frames (NLPID of 0x80).
1241 off_linkpl
.constant_part
= 0;
1243 off_nl_nosnap
= 0; /* no 802.2 LLC */
1247 * the only BPF-interesting FRF.16 frames are non-control frames;
1248 * Frame Relay has a variable length link-layer
1249 * so lets start with offset 4 for now and increments later on (FIXME);
1253 off_linkpl
.constant_part
= 0;
1255 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1258 case DLT_APPLE_IP_OVER_IEEE1394
:
1260 off_linkpl
.constant_part
= 18;
1262 off_nl_nosnap
= 0; /* no 802.2 LLC */
1265 case DLT_SYMANTEC_FIREWALL
:
1267 off_linkpl
.constant_part
= 44;
1268 off_nl
= 0; /* Ethernet II */
1269 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1272 #ifdef HAVE_NET_PFVAR_H
1275 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1277 off_nl_nosnap
= 0; /* no 802.2 LLC */
1281 case DLT_JUNIPER_MFR
:
1282 case DLT_JUNIPER_MLFR
:
1283 case DLT_JUNIPER_MLPPP
:
1284 case DLT_JUNIPER_PPP
:
1285 case DLT_JUNIPER_CHDLC
:
1286 case DLT_JUNIPER_FRELAY
:
1288 off_linkpl
.constant_part
= 4;
1290 off_nl_nosnap
= -1; /* no 802.2 LLC */
1293 case DLT_JUNIPER_ATM1
:
1294 off_linktype
= 4; /* in reality variable between 4-8 */
1295 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1300 case DLT_JUNIPER_ATM2
:
1301 off_linktype
= 8; /* in reality variable between 8-12 */
1302 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1307 /* frames captured on a Juniper PPPoE service PIC
1308 * contain raw ethernet frames */
1309 case DLT_JUNIPER_PPPOE
:
1310 case DLT_JUNIPER_ETHER
:
1311 off_linkpl
.constant_part
= 14;
1313 off_nl
= 18; /* Ethernet II */
1314 off_nl_nosnap
= 21; /* 802.3+802.2 */
1317 case DLT_JUNIPER_PPPOE_ATM
:
1319 off_linkpl
.constant_part
= 6;
1321 off_nl_nosnap
= -1; /* no 802.2 LLC */
1324 case DLT_JUNIPER_GGSN
:
1326 off_linkpl
.constant_part
= 12;
1328 off_nl_nosnap
= -1; /* no 802.2 LLC */
1331 case DLT_JUNIPER_ES
:
1333 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1334 off_nl
= -1; /* not really a network layer but raw IP addresses */
1335 off_nl_nosnap
= -1; /* no 802.2 LLC */
1338 case DLT_JUNIPER_MONITOR
:
1340 off_linkpl
.constant_part
= 12;
1341 off_nl
= 0; /* raw IP/IP6 header */
1342 off_nl_nosnap
= -1; /* no 802.2 LLC */
1345 case DLT_BACNET_MS_TP
:
1347 off_linkpl
.constant_part
= -1;
1352 case DLT_JUNIPER_SERVICES
:
1354 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1355 off_nl
= -1; /* L3 proto location dep. on cookie type */
1356 off_nl_nosnap
= -1; /* no 802.2 LLC */
1359 case DLT_JUNIPER_VP
:
1361 off_linkpl
.constant_part
= -1;
1366 case DLT_JUNIPER_ST
:
1368 off_linkpl
.constant_part
= -1;
1373 case DLT_JUNIPER_ISM
:
1375 off_linkpl
.constant_part
= -1;
1380 case DLT_JUNIPER_VS
:
1381 case DLT_JUNIPER_SRX_E2E
:
1382 case DLT_JUNIPER_FIBRECHANNEL
:
1383 case DLT_JUNIPER_ATM_CEMIC
:
1385 off_linkpl
.constant_part
= -1;
1398 off_linkpl
.constant_part
= -1;
1403 case DLT_MTP2_WITH_PHDR
:
1411 off_linkpl
.constant_part
= -1;
1424 off_linkpl
.constant_part
= -1;
1431 off_linkpl
.constant_part
= 4;
1438 * Currently, only raw "link[N:M]" filtering is supported.
1440 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1441 off_linkpl
.constant_part
= -1;
1442 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1443 off_nl_nosnap
= -1; /* no 802.2 LLC */
1448 off_linkpl
.constant_part
= 24; /* ipnet header length */
1453 case DLT_NETANALYZER
:
1454 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1456 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1457 off_nl
= 0; /* Ethernet II */
1458 off_nl_nosnap
= 3; /* 802.3+802.2 */
1461 case DLT_NETANALYZER_TRANSPARENT
:
1462 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1464 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1465 off_nl
= 0; /* Ethernet II */
1466 off_nl_nosnap
= 3; /* 802.3+802.2 */
1471 * For values in the range in which we've assigned new
1472 * DLT_ values, only raw "link[N:M]" filtering is supported.
1474 if (linktype
>= DLT_MATCHING_MIN
&&
1475 linktype
<= DLT_MATCHING_MAX
) {
1477 off_linkpl
.constant_part
= -1;
1481 bpf_error("unknown data link type %d", linktype
);
1486 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1490 * Load a value relative to the specified absolute offset.
1492 static struct slist
*
1493 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1495 struct slist
*s
, *s2
;
1497 s
= gen_abs_offset_varpart(abs_offset
);
1500 * If "s" is non-null, it has code to arrange that the X register
1501 * contains the variable part of the absolute offset, so we
1502 * generate a load relative to that, with an offset of
1503 * abs_offset->constant_part + offset.
1505 * Otherwise, we can do an absolute load with an offset of
1506 * abs_offset->constant_part + offset.
1510 * "s" points to a list of statements that puts the
1511 * variable part of the absolute offset into the X register.
1512 * Do an indirect load, to use the X register as an offset.
1514 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1515 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1519 * There is no variable part of the absolute offset, so
1520 * just do an absolute load.
1522 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1523 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1529 * Load a value relative to the beginning of the specified header.
1531 static struct slist
*
1532 gen_load_a(offrel
, offset
, size
)
1533 enum e_offrel offrel
;
1536 struct slist
*s
, *s2
;
1541 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1546 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1549 case OR_PREVLINKHDR
:
1550 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1554 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1557 case OR_PREVMPLSHDR
:
1558 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1562 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1565 case OR_LINKPL_NOSNAP
:
1566 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1571 * Load the X register with the length of the IPv4 header
1572 * (plus the offset of the link-layer header, if it's
1573 * preceded by a variable-length header such as a radio
1574 * header), in bytes.
1576 s
= gen_loadx_iphdrlen();
1579 * Load the item at {offset of the link-layer payload} +
1580 * {offset, relative to the start of the link-layer
1581 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1582 * {specified offset}.
1584 * If the offset of the link-layer payload is variable,
1585 * the variable part of that offset is included in the
1586 * value in the X register, and we include the constant
1587 * part in the offset of the load.
1589 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1590 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1595 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1606 * Generate code to load into the X register the sum of the length of
1607 * the IPv4 header and the variable part of the offset of the link-layer
1610 static struct slist
*
1611 gen_loadx_iphdrlen()
1613 struct slist
*s
, *s2
;
1615 s
= gen_abs_offset_varpart(&off_linkpl
);
1618 * The offset of the link-layer payload has a variable
1619 * part. "s" points to a list of statements that put
1620 * the variable part of that offset into the X register.
1622 * The 4*([k]&0xf) addressing mode can't be used, as we
1623 * don't have a constant offset, so we have to load the
1624 * value in question into the A register and add to it
1625 * the value from the X register.
1627 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1630 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1633 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1638 * The A register now contains the length of the IP header.
1639 * We need to add to it the variable part of the offset of
1640 * the link-layer payload, which is still in the X
1641 * register, and move the result into the X register.
1643 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1644 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1647 * The offset of the link-layer payload is a constant,
1648 * so no code was generated to load the (non-existent)
1649 * variable part of that offset.
1651 * This means we can use the 4*([k]&0xf) addressing
1652 * mode. Load the length of the IPv4 header, which
1653 * is at an offset of off_nl from the beginning of
1654 * the link-layer payload, and thus at an offset of
1655 * off_linkpl.constant_part + off_nl from the beginning
1656 * of the raw packet data, using that addressing mode.
1658 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1659 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1664 static struct block
*
1671 s
= new_stmt(BPF_LD
|BPF_IMM
);
1673 b
= new_block(JMP(BPF_JEQ
));
1679 static inline struct block
*
1682 return gen_uncond(1);
1685 static inline struct block
*
1688 return gen_uncond(0);
1692 * Byte-swap a 32-bit number.
1693 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1694 * big-endian platforms.)
1696 #define SWAPLONG(y) \
1697 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1700 * Generate code to match a particular packet type.
1702 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1703 * value, if <= ETHERMTU. We use that to determine whether to
1704 * match the type/length field or to check the type/length field for
1705 * a value <= ETHERMTU to see whether it's a type field and then do
1706 * the appropriate test.
1708 static struct block
*
1709 gen_ether_linktype(proto
)
1712 struct block
*b0
, *b1
;
1718 case LLCSAP_NETBEUI
:
1720 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1721 * so we check the DSAP and SSAP.
1723 * LLCSAP_IP checks for IP-over-802.2, rather
1724 * than IP-over-Ethernet or IP-over-SNAP.
1726 * XXX - should we check both the DSAP and the
1727 * SSAP, like this, or should we check just the
1728 * DSAP, as we do for other types <= ETHERMTU
1729 * (i.e., other SAP values)?
1731 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1733 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1734 ((proto
<< 8) | proto
));
1742 * Ethernet_II frames, which are Ethernet
1743 * frames with a frame type of ETHERTYPE_IPX;
1745 * Ethernet_802.3 frames, which are 802.3
1746 * frames (i.e., the type/length field is
1747 * a length field, <= ETHERMTU, rather than
1748 * a type field) with the first two bytes
1749 * after the Ethernet/802.3 header being
1752 * Ethernet_802.2 frames, which are 802.3
1753 * frames with an 802.2 LLC header and
1754 * with the IPX LSAP as the DSAP in the LLC
1757 * Ethernet_SNAP frames, which are 802.3
1758 * frames with an LLC header and a SNAP
1759 * header and with an OUI of 0x000000
1760 * (encapsulated Ethernet) and a protocol
1761 * ID of ETHERTYPE_IPX in the SNAP header.
1763 * XXX - should we generate the same code both
1764 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1768 * This generates code to check both for the
1769 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1771 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1772 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1776 * Now we add code to check for SNAP frames with
1777 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1779 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1783 * Now we generate code to check for 802.3
1784 * frames in general.
1786 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1790 * Now add the check for 802.3 frames before the
1791 * check for Ethernet_802.2 and Ethernet_802.3,
1792 * as those checks should only be done on 802.3
1793 * frames, not on Ethernet frames.
1798 * Now add the check for Ethernet_II frames, and
1799 * do that before checking for the other frame
1802 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1803 (bpf_int32
)ETHERTYPE_IPX
);
1807 case ETHERTYPE_ATALK
:
1808 case ETHERTYPE_AARP
:
1810 * EtherTalk (AppleTalk protocols on Ethernet link
1811 * layer) may use 802.2 encapsulation.
1815 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1816 * we check for an Ethernet type field less than
1817 * 1500, which means it's an 802.3 length field.
1819 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1823 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1824 * SNAP packets with an organization code of
1825 * 0x080007 (Apple, for Appletalk) and a protocol
1826 * type of ETHERTYPE_ATALK (Appletalk).
1828 * 802.2-encapsulated ETHERTYPE_AARP packets are
1829 * SNAP packets with an organization code of
1830 * 0x000000 (encapsulated Ethernet) and a protocol
1831 * type of ETHERTYPE_AARP (Appletalk ARP).
1833 if (proto
== ETHERTYPE_ATALK
)
1834 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1835 else /* proto == ETHERTYPE_AARP */
1836 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1840 * Check for Ethernet encapsulation (Ethertalk
1841 * phase 1?); we just check for the Ethernet
1844 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1850 if (proto
<= ETHERMTU
) {
1852 * This is an LLC SAP value, so the frames
1853 * that match would be 802.2 frames.
1854 * Check that the frame is an 802.2 frame
1855 * (i.e., that the length/type field is
1856 * a length field, <= ETHERMTU) and
1857 * then check the DSAP.
1859 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1861 b1
= gen_cmp(OR_LINKHDR
, off_linktype
+ 2, BPF_B
,
1867 * This is an Ethernet type, so compare
1868 * the length/type field with it (if
1869 * the frame is an 802.2 frame, the length
1870 * field will be <= ETHERMTU, and, as
1871 * "proto" is > ETHERMTU, this test
1872 * will fail and the frame won't match,
1873 * which is what we want).
1875 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1882 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1883 * or IPv6 then we have an error.
1885 static struct block
*
1886 gen_ipnet_linktype(proto
)
1892 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1893 (bpf_int32
)IPH_AF_INET
);
1896 case ETHERTYPE_IPV6
:
1897 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1898 (bpf_int32
)IPH_AF_INET6
);
1909 * Generate code to match a particular packet type.
1911 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1912 * value, if <= ETHERMTU. We use that to determine whether to
1913 * match the type field or to check the type field for the special
1914 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1916 static struct block
*
1917 gen_linux_sll_linktype(proto
)
1920 struct block
*b0
, *b1
;
1926 case LLCSAP_NETBEUI
:
1928 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1929 * so we check the DSAP and SSAP.
1931 * LLCSAP_IP checks for IP-over-802.2, rather
1932 * than IP-over-Ethernet or IP-over-SNAP.
1934 * XXX - should we check both the DSAP and the
1935 * SSAP, like this, or should we check just the
1936 * DSAP, as we do for other types <= ETHERMTU
1937 * (i.e., other SAP values)?
1939 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1940 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1941 ((proto
<< 8) | proto
));
1947 * Ethernet_II frames, which are Ethernet
1948 * frames with a frame type of ETHERTYPE_IPX;
1950 * Ethernet_802.3 frames, which have a frame
1951 * type of LINUX_SLL_P_802_3;
1953 * Ethernet_802.2 frames, which are 802.3
1954 * frames with an 802.2 LLC header (i.e, have
1955 * a frame type of LINUX_SLL_P_802_2) and
1956 * with the IPX LSAP as the DSAP in the LLC
1959 * Ethernet_SNAP frames, which are 802.3
1960 * frames with an LLC header and a SNAP
1961 * header and with an OUI of 0x000000
1962 * (encapsulated Ethernet) and a protocol
1963 * ID of ETHERTYPE_IPX in the SNAP header.
1965 * First, do the checks on LINUX_SLL_P_802_2
1966 * frames; generate the check for either
1967 * Ethernet_802.2 or Ethernet_SNAP frames, and
1968 * then put a check for LINUX_SLL_P_802_2 frames
1971 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1972 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1974 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1978 * Now check for 802.3 frames and OR that with
1979 * the previous test.
1981 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1985 * Now add the check for Ethernet_II frames, and
1986 * do that before checking for the other frame
1989 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1990 (bpf_int32
)ETHERTYPE_IPX
);
1994 case ETHERTYPE_ATALK
:
1995 case ETHERTYPE_AARP
:
1997 * EtherTalk (AppleTalk protocols on Ethernet link
1998 * layer) may use 802.2 encapsulation.
2002 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2003 * we check for the 802.2 protocol type in the
2004 * "Ethernet type" field.
2006 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2009 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2010 * SNAP packets with an organization code of
2011 * 0x080007 (Apple, for Appletalk) and a protocol
2012 * type of ETHERTYPE_ATALK (Appletalk).
2014 * 802.2-encapsulated ETHERTYPE_AARP packets are
2015 * SNAP packets with an organization code of
2016 * 0x000000 (encapsulated Ethernet) and a protocol
2017 * type of ETHERTYPE_AARP (Appletalk ARP).
2019 if (proto
== ETHERTYPE_ATALK
)
2020 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2021 else /* proto == ETHERTYPE_AARP */
2022 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2026 * Check for Ethernet encapsulation (Ethertalk
2027 * phase 1?); we just check for the Ethernet
2030 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2036 if (proto
<= ETHERMTU
) {
2038 * This is an LLC SAP value, so the frames
2039 * that match would be 802.2 frames.
2040 * Check for the 802.2 protocol type
2041 * in the "Ethernet type" field, and
2042 * then check the DSAP.
2044 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2046 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2052 * This is an Ethernet type, so compare
2053 * the length/type field with it (if
2054 * the frame is an 802.2 frame, the length
2055 * field will be <= ETHERMTU, and, as
2056 * "proto" is > ETHERMTU, this test
2057 * will fail and the frame won't match,
2058 * which is what we want).
2060 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2066 static struct slist
*
2067 gen_load_prism_llprefixlen()
2069 struct slist
*s1
, *s2
;
2070 struct slist
*sjeq_avs_cookie
;
2071 struct slist
*sjcommon
;
2074 * This code is not compatible with the optimizer, as
2075 * we are generating jmp instructions within a normal
2076 * slist of instructions
2081 * Generate code to load the length of the radio header into
2082 * the register assigned to hold that length, if one has been
2083 * assigned. (If one hasn't been assigned, no code we've
2084 * generated uses that prefix, so we don't need to generate any
2087 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2088 * or always use the AVS header rather than the Prism header.
2089 * We load a 4-byte big-endian value at the beginning of the
2090 * raw packet data, and see whether, when masked with 0xFFFFF000,
2091 * it's equal to 0x80211000. If so, that indicates that it's
2092 * an AVS header (the masked-out bits are the version number).
2093 * Otherwise, it's a Prism header.
2095 * XXX - the Prism header is also, in theory, variable-length,
2096 * but no known software generates headers that aren't 144
2099 if (off_linkhdr
.reg
!= -1) {
2103 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2107 * AND it with 0xFFFFF000.
2109 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2110 s2
->s
.k
= 0xFFFFF000;
2114 * Compare with 0x80211000.
2116 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2117 sjeq_avs_cookie
->s
.k
= 0x80211000;
2118 sappend(s1
, sjeq_avs_cookie
);
2123 * The 4 bytes at an offset of 4 from the beginning of
2124 * the AVS header are the length of the AVS header.
2125 * That field is big-endian.
2127 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2130 sjeq_avs_cookie
->s
.jt
= s2
;
2133 * Now jump to the code to allocate a register
2134 * into which to save the header length and
2135 * store the length there. (The "jump always"
2136 * instruction needs to have the k field set;
2137 * it's added to the PC, so, as we're jumping
2138 * over a single instruction, it should be 1.)
2140 sjcommon
= new_stmt(JMP(BPF_JA
));
2142 sappend(s1
, sjcommon
);
2145 * Now for the code that handles the Prism header.
2146 * Just load the length of the Prism header (144)
2147 * into the A register. Have the test for an AVS
2148 * header branch here if we don't have an AVS header.
2150 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2153 sjeq_avs_cookie
->s
.jf
= s2
;
2156 * Now allocate a register to hold that value and store
2157 * it. The code for the AVS header will jump here after
2158 * loading the length of the AVS header.
2160 s2
= new_stmt(BPF_ST
);
2161 s2
->s
.k
= off_linkhdr
.reg
;
2163 sjcommon
->s
.jf
= s2
;
2166 * Now move it into the X register.
2168 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2176 static struct slist
*
2177 gen_load_avs_llprefixlen()
2179 struct slist
*s1
, *s2
;
2182 * Generate code to load the length of the AVS header into
2183 * the register assigned to hold that length, if one has been
2184 * assigned. (If one hasn't been assigned, no code we've
2185 * generated uses that prefix, so we don't need to generate any
2188 if (off_linkhdr
.reg
!= -1) {
2190 * The 4 bytes at an offset of 4 from the beginning of
2191 * the AVS header are the length of the AVS header.
2192 * That field is big-endian.
2194 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2198 * Now allocate a register to hold that value and store
2201 s2
= new_stmt(BPF_ST
);
2202 s2
->s
.k
= off_linkhdr
.reg
;
2206 * Now move it into the X register.
2208 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2216 static struct slist
*
2217 gen_load_radiotap_llprefixlen()
2219 struct slist
*s1
, *s2
;
2222 * Generate code to load the length of the radiotap header into
2223 * the register assigned to hold that length, if one has been
2224 * assigned. (If one hasn't been assigned, no code we've
2225 * generated uses that prefix, so we don't need to generate any
2228 if (off_linkhdr
.reg
!= -1) {
2230 * The 2 bytes at offsets of 2 and 3 from the beginning
2231 * of the radiotap header are the length of the radiotap
2232 * header; unfortunately, it's little-endian, so we have
2233 * to load it a byte at a time and construct the value.
2237 * Load the high-order byte, at an offset of 3, shift it
2238 * left a byte, and put the result in the X register.
2240 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2242 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2245 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2249 * Load the next byte, at an offset of 2, and OR the
2250 * value from the X register into it.
2252 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2255 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2259 * Now allocate a register to hold that value and store
2262 s2
= new_stmt(BPF_ST
);
2263 s2
->s
.k
= off_linkhdr
.reg
;
2267 * Now move it into the X register.
2269 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2278 * At the moment we treat PPI as normal Radiotap encoded
2279 * packets. The difference is in the function that generates
2280 * the code at the beginning to compute the header length.
2281 * Since this code generator of PPI supports bare 802.11
2282 * encapsulation only (i.e. the encapsulated DLT should be
2283 * DLT_IEEE802_11) we generate code to check for this too;
2284 * that's done in finish_parse().
2286 static struct slist
*
2287 gen_load_ppi_llprefixlen()
2289 struct slist
*s1
, *s2
;
2292 * Generate code to load the length of the radiotap header
2293 * into the register assigned to hold that length, if one has
2296 if (off_linkhdr
.reg
!= -1) {
2298 * The 2 bytes at offsets of 2 and 3 from the beginning
2299 * of the radiotap header are the length of the radiotap
2300 * header; unfortunately, it's little-endian, so we have
2301 * to load it a byte at a time and construct the value.
2305 * Load the high-order byte, at an offset of 3, shift it
2306 * left a byte, and put the result in the X register.
2308 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2310 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2313 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2317 * Load the next byte, at an offset of 2, and OR the
2318 * value from the X register into it.
2320 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2323 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2327 * Now allocate a register to hold that value and store
2330 s2
= new_stmt(BPF_ST
);
2331 s2
->s
.k
= off_linkhdr
.reg
;
2335 * Now move it into the X register.
2337 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2346 * Load a value relative to the beginning of the link-layer header after the 802.11
2347 * header, i.e. LLC_SNAP.
2348 * The link-layer header doesn't necessarily begin at the beginning
2349 * of the packet data; there might be a variable-length prefix containing
2350 * radio information.
2352 static struct slist
*
2353 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2356 struct slist
*sjset_data_frame_1
;
2357 struct slist
*sjset_data_frame_2
;
2358 struct slist
*sjset_qos
;
2359 struct slist
*sjset_radiotap_flags
;
2360 struct slist
*sjset_radiotap_tsft
;
2361 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2362 struct slist
*s_roundup
;
2364 if (off_linkpl
.reg
== -1) {
2366 * No register has been assigned to the offset of
2367 * the link-layer payload, which means nobody needs
2368 * it; don't bother computing it - just return
2369 * what we already have.
2375 * This code is not compatible with the optimizer, as
2376 * we are generating jmp instructions within a normal
2377 * slist of instructions
2382 * If "s" is non-null, it has code to arrange that the X register
2383 * contains the length of the prefix preceding the link-layer
2386 * Otherwise, the length of the prefix preceding the link-layer
2387 * header is "off_outermostlinkhdr.constant_part".
2391 * There is no variable-length header preceding the
2392 * link-layer header.
2394 * Load the length of the fixed-length prefix preceding
2395 * the link-layer header (if any) into the X register,
2396 * and store it in the off_linkpl.reg register.
2397 * That length is off_outermostlinkhdr.constant_part.
2399 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2400 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2404 * The X register contains the offset of the beginning of the
2405 * link-layer header; add 24, which is the minimum length
2406 * of the MAC header for a data frame, to that, and store it
2407 * in off_linkpl.reg, and then load the Frame Control field,
2408 * which is at the offset in the X register, with an indexed load.
2410 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2412 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2415 s2
= new_stmt(BPF_ST
);
2416 s2
->s
.k
= off_linkpl
.reg
;
2419 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2424 * Check the Frame Control field to see if this is a data frame;
2425 * a data frame has the 0x08 bit (b3) in that field set and the
2426 * 0x04 bit (b2) clear.
2428 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2429 sjset_data_frame_1
->s
.k
= 0x08;
2430 sappend(s
, sjset_data_frame_1
);
2433 * If b3 is set, test b2, otherwise go to the first statement of
2434 * the rest of the program.
2436 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2437 sjset_data_frame_2
->s
.k
= 0x04;
2438 sappend(s
, sjset_data_frame_2
);
2439 sjset_data_frame_1
->s
.jf
= snext
;
2442 * If b2 is not set, this is a data frame; test the QoS bit.
2443 * Otherwise, go to the first statement of the rest of the
2446 sjset_data_frame_2
->s
.jt
= snext
;
2447 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2448 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2449 sappend(s
, sjset_qos
);
2452 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2454 * Otherwise, go to the first statement of the rest of the
2457 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2458 s2
->s
.k
= off_linkpl
.reg
;
2460 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2463 s2
= new_stmt(BPF_ST
);
2464 s2
->s
.k
= off_linkpl
.reg
;
2468 * If we have a radiotap header, look at it to see whether
2469 * there's Atheros padding between the MAC-layer header
2472 * Note: all of the fields in the radiotap header are
2473 * little-endian, so we byte-swap all of the values
2474 * we test against, as they will be loaded as big-endian
2477 if (linktype
== DLT_IEEE802_11_RADIO
) {
2479 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2480 * in the presence flag?
2482 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2486 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2487 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2488 sappend(s
, sjset_radiotap_flags
);
2491 * If not, skip all of this.
2493 sjset_radiotap_flags
->s
.jf
= snext
;
2496 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2498 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2499 new_stmt(JMP(BPF_JSET
));
2500 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2501 sappend(s
, sjset_radiotap_tsft
);
2504 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2505 * at an offset of 16 from the beginning of the raw packet
2506 * data (8 bytes for the radiotap header and 8 bytes for
2509 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2512 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2516 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2517 sjset_tsft_datapad
->s
.k
= 0x20;
2518 sappend(s
, sjset_tsft_datapad
);
2521 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2522 * at an offset of 8 from the beginning of the raw packet
2523 * data (8 bytes for the radiotap header).
2525 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2528 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2532 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2533 sjset_notsft_datapad
->s
.k
= 0x20;
2534 sappend(s
, sjset_notsft_datapad
);
2537 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2538 * set, round the length of the 802.11 header to
2539 * a multiple of 4. Do that by adding 3 and then
2540 * dividing by and multiplying by 4, which we do by
2543 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2544 s_roundup
->s
.k
= off_linkpl
.reg
;
2545 sappend(s
, s_roundup
);
2546 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2549 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2552 s2
= new_stmt(BPF_ST
);
2553 s2
->s
.k
= off_linkpl
.reg
;
2556 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2557 sjset_tsft_datapad
->s
.jf
= snext
;
2558 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2559 sjset_notsft_datapad
->s
.jf
= snext
;
2561 sjset_qos
->s
.jf
= snext
;
2567 insert_compute_vloffsets(b
)
2573 * For link-layer types that have a variable-length header
2574 * preceding the link-layer header, generate code to load
2575 * the offset of the link-layer header into the register
2576 * assigned to that offset, if any.
2578 * XXX - this, and the next switch statement, won't handle
2579 * encapsulation of 802.11 or 802.11+radio information in
2580 * some other protocol stack. That's significantly more
2583 switch (outermostlinktype
) {
2585 case DLT_PRISM_HEADER
:
2586 s
= gen_load_prism_llprefixlen();
2589 case DLT_IEEE802_11_RADIO_AVS
:
2590 s
= gen_load_avs_llprefixlen();
2593 case DLT_IEEE802_11_RADIO
:
2594 s
= gen_load_radiotap_llprefixlen();
2598 s
= gen_load_ppi_llprefixlen();
2607 * For link-layer types that have a variable-length link-layer
2608 * header, generate code to load the offset of the link-layer
2609 * payload into the register assigned to that offset, if any.
2611 switch (outermostlinktype
) {
2613 case DLT_IEEE802_11
:
2614 case DLT_PRISM_HEADER
:
2615 case DLT_IEEE802_11_RADIO_AVS
:
2616 case DLT_IEEE802_11_RADIO
:
2618 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2623 * If we have any offset-loading code, append all the
2624 * existing statements in the block to those statements,
2625 * and make the resulting list the list of statements
2629 sappend(s
, b
->stmts
);
2634 static struct block
*
2635 gen_ppi_dlt_check(void)
2637 struct slist
*s_load_dlt
;
2640 if (linktype
== DLT_PPI
)
2642 /* Create the statements that check for the DLT
2644 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2645 s_load_dlt
->s
.k
= 4;
2647 b
= new_block(JMP(BPF_JEQ
));
2649 b
->stmts
= s_load_dlt
;
2650 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2661 * Take an absolute offset, and:
2663 * if it has no variable part, return NULL;
2665 * if it has a variable part, generate code to load the register
2666 * containing that variable part into the X register, returning
2667 * a pointer to that code - if no register for that offset has
2668 * been allocated, allocate it first.
2670 * (The code to set that register will be generated later, but will
2671 * be placed earlier in the code sequence.)
2673 static struct slist
*
2674 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2678 if (off
->is_variable
) {
2679 if (off
->reg
== -1) {
2681 * We haven't yet assigned a register for the
2682 * variable part of the offset of the link-layer
2683 * header; allocate one.
2685 off
->reg
= alloc_reg();
2689 * Load the register containing the variable part of the
2690 * offset of the link-layer header into the X register.
2692 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2697 * That offset isn't variable, there's no variable part,
2698 * so we don't need to generate any code.
2705 * Map an Ethernet type to the equivalent PPP type.
2708 ethertype_to_ppptype(proto
)
2717 case ETHERTYPE_IPV6
:
2725 case ETHERTYPE_ATALK
:
2739 * I'm assuming the "Bridging PDU"s that go
2740 * over PPP are Spanning Tree Protocol
2754 * Generate any tests that, for encapsulation of a link-layer packet
2755 * inside another protocol stack, need to be done to check for those
2756 * link-layer packets (and that haven't already been done by a check
2757 * for that encapsulation).
2759 static struct block
*
2760 gen_prevlinkhdr_check(void)
2764 switch (prevlinktype
) {
2768 * This is LANE-encapsulated Ethernet; check that the LANE
2769 * packet doesn't begin with an LE Control marker, i.e.
2770 * that it's data, not a control message.
2772 * (We've already generated a test for LANE.)
2774 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2780 * No such tests are necessary.
2788 * Generate code to match a particular packet type by matching the
2789 * link-layer type field or fields in the 802.2 LLC header.
2791 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2792 * value, if <= ETHERMTU.
2794 static struct block
*
2798 struct block
*b0
, *b1
, *b2
;
2799 const char *description
;
2801 /* are we checking MPLS-encapsulated packets? */
2802 if (label_stack_depth
> 0) {
2806 /* FIXME add other L3 proto IDs */
2807 return gen_mpls_linktype(Q_IP
);
2809 case ETHERTYPE_IPV6
:
2811 /* FIXME add other L3 proto IDs */
2812 return gen_mpls_linktype(Q_IPV6
);
2815 bpf_error("unsupported protocol over mpls");
2823 case DLT_NETANALYZER
:
2824 case DLT_NETANALYZER_TRANSPARENT
:
2825 b0
= gen_prevlinkhdr_check();
2826 b1
= gen_ether_linktype(proto
);
2837 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2841 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2848 case DLT_IEEE802_11
:
2849 case DLT_PRISM_HEADER
:
2850 case DLT_IEEE802_11_RADIO_AVS
:
2851 case DLT_IEEE802_11_RADIO
:
2854 * Check that we have a data frame.
2856 b0
= gen_check_802_11_data_frame();
2859 * Now check for the specified link-layer type.
2861 b1
= gen_llc_linktype(proto
);
2869 * XXX - check for LLC frames.
2871 return gen_llc_linktype(proto
);
2877 * XXX - check for LLC PDUs, as per IEEE 802.5.
2879 return gen_llc_linktype(proto
);
2883 case DLT_ATM_RFC1483
:
2885 case DLT_IP_OVER_FC
:
2886 return gen_llc_linktype(proto
);
2892 * Check for an LLC-encapsulated version of this protocol;
2893 * if we were checking for LANE, linktype would no longer
2896 * Check for LLC encapsulation and then check the protocol.
2898 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2899 b1
= gen_llc_linktype(proto
);
2906 return gen_linux_sll_linktype(proto
);
2911 case DLT_SLIP_BSDOS
:
2914 * These types don't provide any type field; packets
2915 * are always IPv4 or IPv6.
2917 * XXX - for IPv4, check for a version number of 4, and,
2918 * for IPv6, check for a version number of 6?
2923 /* Check for a version number of 4. */
2924 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
2926 case ETHERTYPE_IPV6
:
2927 /* Check for a version number of 6. */
2928 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
2931 return gen_false(); /* always false */
2938 * Raw IPv4, so no type field.
2940 if (proto
== ETHERTYPE_IP
)
2941 return gen_true(); /* always true */
2943 /* Checking for something other than IPv4; always false */
2950 * Raw IPv6, so no type field.
2952 if (proto
== ETHERTYPE_IPV6
)
2953 return gen_true(); /* always true */
2955 /* Checking for something other than IPv6; always false */
2962 case DLT_PPP_SERIAL
:
2965 * We use Ethernet protocol types inside libpcap;
2966 * map them to the corresponding PPP protocol types.
2968 proto
= ethertype_to_ppptype(proto
);
2969 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2975 * We use Ethernet protocol types inside libpcap;
2976 * map them to the corresponding PPP protocol types.
2982 * Also check for Van Jacobson-compressed IP.
2983 * XXX - do this for other forms of PPP?
2985 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_IP
);
2986 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJC
);
2988 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJNC
);
2993 proto
= ethertype_to_ppptype(proto
);
2994 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
3004 * For DLT_NULL, the link-layer header is a 32-bit
3005 * word containing an AF_ value in *host* byte order,
3006 * and for DLT_ENC, the link-layer header begins
3007 * with a 32-bit work containing an AF_ value in
3010 * In addition, if we're reading a saved capture file,
3011 * the host byte order in the capture may not be the
3012 * same as the host byte order on this machine.
3014 * For DLT_LOOP, the link-layer header is a 32-bit
3015 * word containing an AF_ value in *network* byte order.
3017 * XXX - AF_ values may, unfortunately, be platform-
3018 * dependent; for example, FreeBSD's AF_INET6 is 24
3019 * whilst NetBSD's and OpenBSD's is 26.
3021 * This means that, when reading a capture file, just
3022 * checking for our AF_INET6 value won't work if the
3023 * capture file came from another OS.
3032 case ETHERTYPE_IPV6
:
3039 * Not a type on which we support filtering.
3040 * XXX - support those that have AF_ values
3041 * #defined on this platform, at least?
3046 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3048 * The AF_ value is in host byte order, but
3049 * the BPF interpreter will convert it to
3050 * network byte order.
3052 * If this is a save file, and it's from a
3053 * machine with the opposite byte order to
3054 * ours, we byte-swap the AF_ value.
3056 * Then we run it through "htonl()", and
3057 * generate code to compare against the result.
3059 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3060 proto
= SWAPLONG(proto
);
3061 proto
= htonl(proto
);
3063 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3065 #ifdef HAVE_NET_PFVAR_H
3068 * af field is host byte order in contrast to the rest of
3071 if (proto
== ETHERTYPE_IP
)
3072 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3073 BPF_B
, (bpf_int32
)AF_INET
));
3074 else if (proto
== ETHERTYPE_IPV6
)
3075 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3076 BPF_B
, (bpf_int32
)AF_INET6
));
3081 #endif /* HAVE_NET_PFVAR_H */
3084 case DLT_ARCNET_LINUX
:
3086 * XXX should we check for first fragment if the protocol
3094 case ETHERTYPE_IPV6
:
3095 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3096 (bpf_int32
)ARCTYPE_INET6
));
3099 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3100 (bpf_int32
)ARCTYPE_IP
);
3101 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3102 (bpf_int32
)ARCTYPE_IP_OLD
);
3107 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3108 (bpf_int32
)ARCTYPE_ARP
);
3109 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3110 (bpf_int32
)ARCTYPE_ARP_OLD
);
3114 case ETHERTYPE_REVARP
:
3115 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3116 (bpf_int32
)ARCTYPE_REVARP
));
3118 case ETHERTYPE_ATALK
:
3119 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3120 (bpf_int32
)ARCTYPE_ATALK
));
3127 case ETHERTYPE_ATALK
:
3137 * XXX - assumes a 2-byte Frame Relay header with
3138 * DLCI and flags. What if the address is longer?
3144 * Check for the special NLPID for IP.
3146 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3148 case ETHERTYPE_IPV6
:
3150 * Check for the special NLPID for IPv6.
3152 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3156 * Check for several OSI protocols.
3158 * Frame Relay packets typically have an OSI
3159 * NLPID at the beginning; we check for each
3162 * What we check for is the NLPID and a frame
3163 * control field of UI, i.e. 0x03 followed
3166 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3167 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3168 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3180 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3182 case DLT_JUNIPER_MFR
:
3183 case DLT_JUNIPER_MLFR
:
3184 case DLT_JUNIPER_MLPPP
:
3185 case DLT_JUNIPER_ATM1
:
3186 case DLT_JUNIPER_ATM2
:
3187 case DLT_JUNIPER_PPPOE
:
3188 case DLT_JUNIPER_PPPOE_ATM
:
3189 case DLT_JUNIPER_GGSN
:
3190 case DLT_JUNIPER_ES
:
3191 case DLT_JUNIPER_MONITOR
:
3192 case DLT_JUNIPER_SERVICES
:
3193 case DLT_JUNIPER_ETHER
:
3194 case DLT_JUNIPER_PPP
:
3195 case DLT_JUNIPER_FRELAY
:
3196 case DLT_JUNIPER_CHDLC
:
3197 case DLT_JUNIPER_VP
:
3198 case DLT_JUNIPER_ST
:
3199 case DLT_JUNIPER_ISM
:
3200 case DLT_JUNIPER_VS
:
3201 case DLT_JUNIPER_SRX_E2E
:
3202 case DLT_JUNIPER_FIBRECHANNEL
:
3203 case DLT_JUNIPER_ATM_CEMIC
:
3205 /* just lets verify the magic number for now -
3206 * on ATM we may have up to 6 different encapsulations on the wire
3207 * and need a lot of heuristics to figure out that the payload
3210 * FIXME encapsulation specific BPF_ filters
3212 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3214 case DLT_BACNET_MS_TP
:
3215 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3218 return gen_ipnet_linktype(proto
);
3220 case DLT_LINUX_IRDA
:
3221 bpf_error("IrDA link-layer type filtering not implemented");
3224 bpf_error("DOCSIS link-layer type filtering not implemented");
3227 case DLT_MTP2_WITH_PHDR
:
3228 bpf_error("MTP2 link-layer type filtering not implemented");
3231 bpf_error("ERF link-layer type filtering not implemented");
3234 bpf_error("PFSYNC link-layer type filtering not implemented");
3236 case DLT_LINUX_LAPD
:
3237 bpf_error("LAPD link-layer type filtering not implemented");
3241 case DLT_USB_LINUX_MMAPPED
:
3242 bpf_error("USB link-layer type filtering not implemented");
3244 case DLT_BLUETOOTH_HCI_H4
:
3245 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3246 bpf_error("Bluetooth link-layer type filtering not implemented");
3249 case DLT_CAN_SOCKETCAN
:
3250 bpf_error("CAN link-layer type filtering not implemented");
3252 case DLT_IEEE802_15_4
:
3253 case DLT_IEEE802_15_4_LINUX
:
3254 case DLT_IEEE802_15_4_NONASK_PHY
:
3255 case DLT_IEEE802_15_4_NOFCS
:
3256 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3258 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3259 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3262 bpf_error("SITA link-layer type filtering not implemented");
3265 bpf_error("RAIF1 link-layer type filtering not implemented");
3268 bpf_error("IPMB link-layer type filtering not implemented");
3271 bpf_error("AX.25 link-layer type filtering not implemented");
3274 /* Using the fixed-size NFLOG header it is possible to tell only
3275 * the address family of the packet, other meaningful data is
3276 * either missing or behind TLVs.
3278 bpf_error("NFLOG link-layer type filtering not implemented");
3282 * Does this link-layer header type have a field
3283 * indicating the type of the next protocol? If
3284 * so, off_linktype will be the offset of that
3285 * field in the packet; if not, it will be -1.
3287 if (off_linktype
!= (u_int
)-1) {
3289 * Yes; assume it's an Ethernet type. (If
3290 * it's not, it needs to be handled specially
3293 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3296 * No; report an error.
3298 description
= pcap_datalink_val_to_description(linktype
);
3299 if (description
!= NULL
) {
3300 bpf_error("%s link-layer type filtering not implemented",
3303 bpf_error("DLT %u link-layer type filtering not implemented",
3312 * Check for an LLC SNAP packet with a given organization code and
3313 * protocol type; we check the entire contents of the 802.2 LLC and
3314 * snap headers, checking for DSAP and SSAP of SNAP and a control
3315 * field of 0x03 in the LLC header, and for the specified organization
3316 * code and protocol type in the SNAP header.
3318 static struct block
*
3319 gen_snap(orgcode
, ptype
)
3320 bpf_u_int32 orgcode
;
3323 u_char snapblock
[8];
3325 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3326 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3327 snapblock
[2] = 0x03; /* control = UI */
3328 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3329 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3330 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3331 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3332 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3333 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3337 * Generate code to match frames with an LLC header.
3342 struct block
*b0
, *b1
;
3348 * We check for an Ethernet type field less than
3349 * 1500, which means it's an 802.3 length field.
3351 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
3355 * Now check for the purported DSAP and SSAP not being
3356 * 0xFF, to rule out NetWare-over-802.3.
3358 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3365 * We check for LLC traffic.
3367 b0
= gen_atmtype_abbrev(A_LLC
);
3370 case DLT_IEEE802
: /* Token Ring */
3372 * XXX - check for LLC frames.
3378 * XXX - check for LLC frames.
3382 case DLT_ATM_RFC1483
:
3384 * For LLC encapsulation, these are defined to have an
3387 * For VC encapsulation, they don't, but there's no
3388 * way to check for that; the protocol used on the VC
3389 * is negotiated out of band.
3393 case DLT_IEEE802_11
:
3394 case DLT_PRISM_HEADER
:
3395 case DLT_IEEE802_11_RADIO
:
3396 case DLT_IEEE802_11_RADIO_AVS
:
3399 * Check that we have a data frame.
3401 b0
= gen_check_802_11_data_frame();
3405 bpf_error("'llc' not supported for linktype %d", linktype
);
3413 struct block
*b0
, *b1
;
3417 * Check whether this is an LLC frame.
3422 * Load the control byte and test the low-order bit; it must
3423 * be clear for I frames.
3425 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3426 b1
= new_block(JMP(BPF_JSET
));
3437 struct block
*b0
, *b1
;
3440 * Check whether this is an LLC frame.
3445 * Now compare the low-order 2 bit of the control byte against
3446 * the appropriate value for S frames.
3448 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3456 struct block
*b0
, *b1
;
3459 * Check whether this is an LLC frame.
3464 * Now compare the low-order 2 bit of the control byte against
3465 * the appropriate value for U frames.
3467 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3473 gen_llc_s_subtype(bpf_u_int32 subtype
)
3475 struct block
*b0
, *b1
;
3478 * Check whether this is an LLC frame.
3483 * Now check for an S frame with the appropriate type.
3485 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3491 gen_llc_u_subtype(bpf_u_int32 subtype
)
3493 struct block
*b0
, *b1
;
3496 * Check whether this is an LLC frame.
3501 * Now check for a U frame with the appropriate type.
3503 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3509 * Generate code to match a particular packet type, for link-layer types
3510 * using 802.2 LLC headers.
3512 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3513 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3515 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3516 * value, if <= ETHERMTU. We use that to determine whether to
3517 * match the DSAP or both DSAP and LSAP or to check the OUI and
3518 * protocol ID in a SNAP header.
3520 static struct block
*
3521 gen_llc_linktype(proto
)
3525 * XXX - handle token-ring variable-length header.
3531 case LLCSAP_NETBEUI
:
3533 * XXX - should we check both the DSAP and the
3534 * SSAP, like this, or should we check just the
3535 * DSAP, as we do for other SAP values?
3537 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3538 ((proto
<< 8) | proto
));
3542 * XXX - are there ever SNAP frames for IPX on
3543 * non-Ethernet 802.x networks?
3545 return gen_cmp(OR_LLC
, 0, BPF_B
,
3546 (bpf_int32
)LLCSAP_IPX
);
3548 case ETHERTYPE_ATALK
:
3550 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3551 * SNAP packets with an organization code of
3552 * 0x080007 (Apple, for Appletalk) and a protocol
3553 * type of ETHERTYPE_ATALK (Appletalk).
3555 * XXX - check for an organization code of
3556 * encapsulated Ethernet as well?
3558 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3562 * XXX - we don't have to check for IPX 802.3
3563 * here, but should we check for the IPX Ethertype?
3565 if (proto
<= ETHERMTU
) {
3567 * This is an LLC SAP value, so check
3570 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3573 * This is an Ethernet type; we assume that it's
3574 * unlikely that it'll appear in the right place
3575 * at random, and therefore check only the
3576 * location that would hold the Ethernet type
3577 * in a SNAP frame with an organization code of
3578 * 0x000000 (encapsulated Ethernet).
3580 * XXX - if we were to check for the SNAP DSAP and
3581 * LSAP, as per XXX, and were also to check for an
3582 * organization code of 0x000000 (encapsulated
3583 * Ethernet), we'd do
3585 * return gen_snap(0x000000, proto);
3587 * here; for now, we don't, as per the above.
3588 * I don't know whether it's worth the extra CPU
3589 * time to do the right check or not.
3591 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3596 static struct block
*
3597 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3601 u_int src_off
, dst_off
;
3603 struct block
*b0
, *b1
;
3617 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3618 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3624 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3625 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3632 b0
= gen_linktype(proto
);
3633 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3639 static struct block
*
3640 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3641 struct in6_addr
*addr
;
3642 struct in6_addr
*mask
;
3644 u_int src_off
, dst_off
;
3646 struct block
*b0
, *b1
;
3661 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3662 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3668 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3669 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3676 /* this order is important */
3677 a
= (u_int32_t
*)addr
;
3678 m
= (u_int32_t
*)mask
;
3679 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3680 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3682 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3684 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3686 b0
= gen_linktype(proto
);
3692 static struct block
*
3693 gen_ehostop(eaddr
, dir
)
3694 register const u_char
*eaddr
;
3697 register struct block
*b0
, *b1
;
3701 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3704 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3707 b0
= gen_ehostop(eaddr
, Q_SRC
);
3708 b1
= gen_ehostop(eaddr
, Q_DST
);
3714 b0
= gen_ehostop(eaddr
, Q_SRC
);
3715 b1
= gen_ehostop(eaddr
, Q_DST
);
3720 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3724 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3728 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3732 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3736 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3740 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3748 * Like gen_ehostop, but for DLT_FDDI
3750 static struct block
*
3751 gen_fhostop(eaddr
, dir
)
3752 register const u_char
*eaddr
;
3755 struct block
*b0
, *b1
;
3759 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3762 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3765 b0
= gen_fhostop(eaddr
, Q_SRC
);
3766 b1
= gen_fhostop(eaddr
, Q_DST
);
3772 b0
= gen_fhostop(eaddr
, Q_SRC
);
3773 b1
= gen_fhostop(eaddr
, Q_DST
);
3778 bpf_error("'addr1' is only supported on 802.11");
3782 bpf_error("'addr2' is only supported on 802.11");
3786 bpf_error("'addr3' is only supported on 802.11");
3790 bpf_error("'addr4' is only supported on 802.11");
3794 bpf_error("'ra' is only supported on 802.11");
3798 bpf_error("'ta' is only supported on 802.11");
3806 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3808 static struct block
*
3809 gen_thostop(eaddr
, dir
)
3810 register const u_char
*eaddr
;
3813 register struct block
*b0
, *b1
;
3817 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3820 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3823 b0
= gen_thostop(eaddr
, Q_SRC
);
3824 b1
= gen_thostop(eaddr
, Q_DST
);
3830 b0
= gen_thostop(eaddr
, Q_SRC
);
3831 b1
= gen_thostop(eaddr
, Q_DST
);
3836 bpf_error("'addr1' is only supported on 802.11");
3840 bpf_error("'addr2' is only supported on 802.11");
3844 bpf_error("'addr3' is only supported on 802.11");
3848 bpf_error("'addr4' is only supported on 802.11");
3852 bpf_error("'ra' is only supported on 802.11");
3856 bpf_error("'ta' is only supported on 802.11");
3864 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3865 * various 802.11 + radio headers.
3867 static struct block
*
3868 gen_wlanhostop(eaddr
, dir
)
3869 register const u_char
*eaddr
;
3872 register struct block
*b0
, *b1
, *b2
;
3873 register struct slist
*s
;
3875 #ifdef ENABLE_WLAN_FILTERING_PATCH
3878 * We need to disable the optimizer because the optimizer is buggy
3879 * and wipes out some LD instructions generated by the below
3880 * code to validate the Frame Control bits
3883 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3890 * For control frames, there is no SA.
3892 * For management frames, SA is at an
3893 * offset of 10 from the beginning of
3896 * For data frames, SA is at an offset
3897 * of 10 from the beginning of the packet
3898 * if From DS is clear, at an offset of
3899 * 16 from the beginning of the packet
3900 * if From DS is set and To DS is clear,
3901 * and an offset of 24 from the beginning
3902 * of the packet if From DS is set and To DS
3907 * Generate the tests to be done for data frames
3910 * First, check for To DS set, i.e. check "link[1] & 0x01".
3912 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3913 b1
= new_block(JMP(BPF_JSET
));
3914 b1
->s
.k
= 0x01; /* To DS */
3918 * If To DS is set, the SA is at 24.
3920 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
3924 * Now, check for To DS not set, i.e. check
3925 * "!(link[1] & 0x01)".
3927 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3928 b2
= new_block(JMP(BPF_JSET
));
3929 b2
->s
.k
= 0x01; /* To DS */
3934 * If To DS is not set, the SA is at 16.
3936 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
3940 * Now OR together the last two checks. That gives
3941 * the complete set of checks for data frames with
3947 * Now check for From DS being set, and AND that with
3948 * the ORed-together checks.
3950 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3951 b1
= new_block(JMP(BPF_JSET
));
3952 b1
->s
.k
= 0x02; /* From DS */
3957 * Now check for data frames with From DS not set.
3959 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3960 b2
= new_block(JMP(BPF_JSET
));
3961 b2
->s
.k
= 0x02; /* From DS */
3966 * If From DS isn't set, the SA is at 10.
3968 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
3972 * Now OR together the checks for data frames with
3973 * From DS not set and for data frames with From DS
3974 * set; that gives the checks done for data frames.
3979 * Now check for a data frame.
3980 * I.e, check "link[0] & 0x08".
3982 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3983 b1
= new_block(JMP(BPF_JSET
));
3988 * AND that with the checks done for data frames.
3993 * If the high-order bit of the type value is 0, this
3994 * is a management frame.
3995 * I.e, check "!(link[0] & 0x08)".
3997 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3998 b2
= new_block(JMP(BPF_JSET
));
4004 * For management frames, the SA is at 10.
4006 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4010 * OR that with the checks done for data frames.
4011 * That gives the checks done for management and
4017 * If the low-order bit of the type value is 1,
4018 * this is either a control frame or a frame
4019 * with a reserved type, and thus not a
4022 * I.e., check "!(link[0] & 0x04)".
4024 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4025 b1
= new_block(JMP(BPF_JSET
));
4031 * AND that with the checks for data and management
4041 * For control frames, there is no DA.
4043 * For management frames, DA is at an
4044 * offset of 4 from the beginning of
4047 * For data frames, DA is at an offset
4048 * of 4 from the beginning of the packet
4049 * if To DS is clear and at an offset of
4050 * 16 from the beginning of the packet
4055 * Generate the tests to be done for data frames.
4057 * First, check for To DS set, i.e. "link[1] & 0x01".
4059 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4060 b1
= new_block(JMP(BPF_JSET
));
4061 b1
->s
.k
= 0x01; /* To DS */
4065 * If To DS is set, the DA is at 16.
4067 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4071 * Now, check for To DS not set, i.e. check
4072 * "!(link[1] & 0x01)".
4074 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4075 b2
= new_block(JMP(BPF_JSET
));
4076 b2
->s
.k
= 0x01; /* To DS */
4081 * If To DS is not set, the DA is at 4.
4083 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4087 * Now OR together the last two checks. That gives
4088 * the complete set of checks for data frames.
4093 * Now check for a data frame.
4094 * I.e, check "link[0] & 0x08".
4096 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4097 b1
= new_block(JMP(BPF_JSET
));
4102 * AND that with the checks done for data frames.
4107 * If the high-order bit of the type value is 0, this
4108 * is a management frame.
4109 * I.e, check "!(link[0] & 0x08)".
4111 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4112 b2
= new_block(JMP(BPF_JSET
));
4118 * For management frames, the DA is at 4.
4120 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4124 * OR that with the checks done for data frames.
4125 * That gives the checks done for management and
4131 * If the low-order bit of the type value is 1,
4132 * this is either a control frame or a frame
4133 * with a reserved type, and thus not a
4136 * I.e., check "!(link[0] & 0x04)".
4138 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4139 b1
= new_block(JMP(BPF_JSET
));
4145 * AND that with the checks for data and management
4153 * Not present in management frames; addr1 in other
4158 * If the high-order bit of the type value is 0, this
4159 * is a management frame.
4160 * I.e, check "(link[0] & 0x08)".
4162 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4163 b1
= new_block(JMP(BPF_JSET
));
4170 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4173 * AND that with the check of addr1.
4180 * Not present in management frames; addr2, if present,
4185 * Not present in CTS or ACK control frames.
4187 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4188 IEEE80211_FC0_TYPE_MASK
);
4190 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4191 IEEE80211_FC0_SUBTYPE_MASK
);
4193 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4194 IEEE80211_FC0_SUBTYPE_MASK
);
4200 * If the high-order bit of the type value is 0, this
4201 * is a management frame.
4202 * I.e, check "(link[0] & 0x08)".
4204 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4205 b1
= new_block(JMP(BPF_JSET
));
4210 * AND that with the check for frames other than
4211 * CTS and ACK frames.
4218 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4223 * XXX - add BSSID keyword?
4226 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4230 * Not present in CTS or ACK control frames.
4232 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4233 IEEE80211_FC0_TYPE_MASK
);
4235 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4236 IEEE80211_FC0_SUBTYPE_MASK
);
4238 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4239 IEEE80211_FC0_SUBTYPE_MASK
);
4243 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4249 * Not present in control frames.
4251 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4252 IEEE80211_FC0_TYPE_MASK
);
4254 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4260 * Present only if the direction mask has both "From DS"
4261 * and "To DS" set. Neither control frames nor management
4262 * frames should have both of those set, so we don't
4263 * check the frame type.
4265 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4266 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4267 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4272 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4273 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4279 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4280 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4289 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4290 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4291 * as the RFC states.)
4293 static struct block
*
4294 gen_ipfchostop(eaddr
, dir
)
4295 register const u_char
*eaddr
;
4298 register struct block
*b0
, *b1
;
4302 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4305 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4308 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4309 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4315 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4316 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4321 bpf_error("'addr1' is only supported on 802.11");
4325 bpf_error("'addr2' is only supported on 802.11");
4329 bpf_error("'addr3' is only supported on 802.11");
4333 bpf_error("'addr4' is only supported on 802.11");
4337 bpf_error("'ra' is only supported on 802.11");
4341 bpf_error("'ta' is only supported on 802.11");
4349 * This is quite tricky because there may be pad bytes in front of the
4350 * DECNET header, and then there are two possible data packet formats that
4351 * carry both src and dst addresses, plus 5 packet types in a format that
4352 * carries only the src node, plus 2 types that use a different format and
4353 * also carry just the src node.
4357 * Instead of doing those all right, we just look for data packets with
4358 * 0 or 1 bytes of padding. If you want to look at other packets, that
4359 * will require a lot more hacking.
4361 * To add support for filtering on DECNET "areas" (network numbers)
4362 * one would want to add a "mask" argument to this routine. That would
4363 * make the filter even more inefficient, although one could be clever
4364 * and not generate masking instructions if the mask is 0xFFFF.
4366 static struct block
*
4367 gen_dnhostop(addr
, dir
)
4371 struct block
*b0
, *b1
, *b2
, *tmp
;
4372 u_int offset_lh
; /* offset if long header is received */
4373 u_int offset_sh
; /* offset if short header is received */
4378 offset_sh
= 1; /* follows flags */
4379 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4383 offset_sh
= 3; /* follows flags, dstnode */
4384 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4388 /* Inefficient because we do our Calvinball dance twice */
4389 b0
= gen_dnhostop(addr
, Q_SRC
);
4390 b1
= gen_dnhostop(addr
, Q_DST
);
4396 /* Inefficient because we do our Calvinball dance twice */
4397 b0
= gen_dnhostop(addr
, Q_SRC
);
4398 b1
= gen_dnhostop(addr
, Q_DST
);
4403 bpf_error("ISO host filtering not implemented");
4408 b0
= gen_linktype(ETHERTYPE_DN
);
4409 /* Check for pad = 1, long header case */
4410 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4411 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4412 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4413 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4415 /* Check for pad = 0, long header case */
4416 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4417 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4420 /* Check for pad = 1, short header case */
4421 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4422 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4423 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4426 /* Check for pad = 0, short header case */
4427 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4428 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4432 /* Combine with test for linktype */
4438 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4439 * test the bottom-of-stack bit, and then check the version number
4440 * field in the IP header.
4442 static struct block
*
4443 gen_mpls_linktype(proto
)
4446 struct block
*b0
, *b1
;
4451 /* match the bottom-of-stack bit */
4452 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4453 /* match the IPv4 version number */
4454 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4459 /* match the bottom-of-stack bit */
4460 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4461 /* match the IPv4 version number */
4462 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4471 static struct block
*
4472 gen_host(addr
, mask
, proto
, dir
, type
)
4479 struct block
*b0
, *b1
;
4480 const char *typestr
;
4490 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4492 * Only check for non-IPv4 addresses if we're not
4493 * checking MPLS-encapsulated packets.
4495 if (label_stack_depth
== 0) {
4496 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4498 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4504 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4507 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4510 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4513 bpf_error("'tcp' modifier applied to %s", typestr
);
4516 bpf_error("'sctp' modifier applied to %s", typestr
);
4519 bpf_error("'udp' modifier applied to %s", typestr
);
4522 bpf_error("'icmp' modifier applied to %s", typestr
);
4525 bpf_error("'igmp' modifier applied to %s", typestr
);
4528 bpf_error("'igrp' modifier applied to %s", typestr
);
4531 bpf_error("'pim' modifier applied to %s", typestr
);
4534 bpf_error("'vrrp' modifier applied to %s", typestr
);
4537 bpf_error("'carp' modifier applied to %s", typestr
);
4540 bpf_error("ATALK host filtering not implemented");
4543 bpf_error("AARP host filtering not implemented");
4546 return gen_dnhostop(addr
, dir
);
4549 bpf_error("SCA host filtering not implemented");
4552 bpf_error("LAT host filtering not implemented");
4555 bpf_error("MOPDL host filtering not implemented");
4558 bpf_error("MOPRC host filtering not implemented");
4561 bpf_error("'ip6' modifier applied to ip host");
4564 bpf_error("'icmp6' modifier applied to %s", typestr
);
4567 bpf_error("'ah' modifier applied to %s", typestr
);
4570 bpf_error("'esp' modifier applied to %s", typestr
);
4573 bpf_error("ISO host filtering not implemented");
4576 bpf_error("'esis' modifier applied to %s", typestr
);
4579 bpf_error("'isis' modifier applied to %s", typestr
);
4582 bpf_error("'clnp' modifier applied to %s", typestr
);
4585 bpf_error("'stp' modifier applied to %s", typestr
);
4588 bpf_error("IPX host filtering not implemented");
4591 bpf_error("'netbeui' modifier applied to %s", typestr
);
4594 bpf_error("'radio' modifier applied to %s", typestr
);
4603 static struct block
*
4604 gen_host6(addr
, mask
, proto
, dir
, type
)
4605 struct in6_addr
*addr
;
4606 struct in6_addr
*mask
;
4611 const char *typestr
;
4621 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4624 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4627 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4630 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4633 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4636 bpf_error("'sctp' modifier applied to %s", typestr
);
4639 bpf_error("'tcp' modifier applied to %s", typestr
);
4642 bpf_error("'udp' modifier applied to %s", typestr
);
4645 bpf_error("'icmp' modifier applied to %s", typestr
);
4648 bpf_error("'igmp' modifier applied to %s", typestr
);
4651 bpf_error("'igrp' modifier applied to %s", typestr
);
4654 bpf_error("'pim' modifier applied to %s", typestr
);
4657 bpf_error("'vrrp' modifier applied to %s", typestr
);
4660 bpf_error("'carp' modifier applied to %s", typestr
);
4663 bpf_error("ATALK host filtering not implemented");
4666 bpf_error("AARP host filtering not implemented");
4669 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4672 bpf_error("SCA host filtering not implemented");
4675 bpf_error("LAT host filtering not implemented");
4678 bpf_error("MOPDL host filtering not implemented");
4681 bpf_error("MOPRC host filtering not implemented");
4684 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4687 bpf_error("'icmp6' modifier applied to %s", typestr
);
4690 bpf_error("'ah' modifier applied to %s", typestr
);
4693 bpf_error("'esp' modifier applied to %s", typestr
);
4696 bpf_error("ISO host filtering not implemented");
4699 bpf_error("'esis' modifier applied to %s", typestr
);
4702 bpf_error("'isis' modifier applied to %s", typestr
);
4705 bpf_error("'clnp' modifier applied to %s", typestr
);
4708 bpf_error("'stp' modifier applied to %s", typestr
);
4711 bpf_error("IPX host filtering not implemented");
4714 bpf_error("'netbeui' modifier applied to %s", typestr
);
4717 bpf_error("'radio' modifier applied to %s", typestr
);
4727 static struct block
*
4728 gen_gateway(eaddr
, alist
, proto
, dir
)
4729 const u_char
*eaddr
;
4730 bpf_u_int32
**alist
;
4734 struct block
*b0
, *b1
, *tmp
;
4737 bpf_error("direction applied to 'gateway'");
4746 case DLT_NETANALYZER
:
4747 case DLT_NETANALYZER_TRANSPARENT
:
4748 b1
= gen_prevlinkhdr_check();
4749 b0
= gen_ehostop(eaddr
, Q_OR
);
4754 b0
= gen_fhostop(eaddr
, Q_OR
);
4757 b0
= gen_thostop(eaddr
, Q_OR
);
4759 case DLT_IEEE802_11
:
4760 case DLT_PRISM_HEADER
:
4761 case DLT_IEEE802_11_RADIO_AVS
:
4762 case DLT_IEEE802_11_RADIO
:
4764 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4768 * This is LLC-multiplexed traffic; if it were
4769 * LANE, linktype would have been set to
4773 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4775 case DLT_IP_OVER_FC
:
4776 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4780 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4782 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4784 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4793 bpf_error("illegal modifier of 'gateway'");
4799 gen_proto_abbrev(proto
)
4808 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4809 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4814 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4815 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4820 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4821 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4826 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4829 #ifndef IPPROTO_IGMP
4830 #define IPPROTO_IGMP 2
4834 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4837 #ifndef IPPROTO_IGRP
4838 #define IPPROTO_IGRP 9
4841 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4845 #define IPPROTO_PIM 103
4849 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4850 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4854 #ifndef IPPROTO_VRRP
4855 #define IPPROTO_VRRP 112
4859 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4862 #ifndef IPPROTO_CARP
4863 #define IPPROTO_CARP 112
4867 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4871 b1
= gen_linktype(ETHERTYPE_IP
);
4875 b1
= gen_linktype(ETHERTYPE_ARP
);
4879 b1
= gen_linktype(ETHERTYPE_REVARP
);
4883 bpf_error("link layer applied in wrong context");
4886 b1
= gen_linktype(ETHERTYPE_ATALK
);
4890 b1
= gen_linktype(ETHERTYPE_AARP
);
4894 b1
= gen_linktype(ETHERTYPE_DN
);
4898 b1
= gen_linktype(ETHERTYPE_SCA
);
4902 b1
= gen_linktype(ETHERTYPE_LAT
);
4906 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4910 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4914 b1
= gen_linktype(ETHERTYPE_IPV6
);
4917 #ifndef IPPROTO_ICMPV6
4918 #define IPPROTO_ICMPV6 58
4921 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4925 #define IPPROTO_AH 51
4928 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4929 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4934 #define IPPROTO_ESP 50
4937 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4938 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4943 b1
= gen_linktype(LLCSAP_ISONS
);
4947 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4951 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4954 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4955 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4956 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4958 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4960 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4962 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4966 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4967 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4968 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4970 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4972 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4974 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4978 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4979 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4980 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4982 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4987 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4988 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4993 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4994 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4996 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4998 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5003 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5004 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5009 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5010 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5015 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5019 b1
= gen_linktype(LLCSAP_8021D
);
5023 b1
= gen_linktype(LLCSAP_IPX
);
5027 b1
= gen_linktype(LLCSAP_NETBEUI
);
5031 bpf_error("'radio' is not a valid protocol type");
5039 static struct block
*
5045 /* not IPv4 frag other than the first frag */
5046 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5047 b
= new_block(JMP(BPF_JSET
));
5056 * Generate a comparison to a port value in the transport-layer header
5057 * at the specified offset from the beginning of that header.
5059 * XXX - this handles a variable-length prefix preceding the link-layer
5060 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5061 * variable-length link-layer headers (such as Token Ring or 802.11
5064 static struct block
*
5065 gen_portatom(off
, v
)
5069 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5072 static struct block
*
5073 gen_portatom6(off
, v
)
5077 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5081 gen_portop(port
, proto
, dir
)
5082 int port
, proto
, dir
;
5084 struct block
*b0
, *b1
, *tmp
;
5086 /* ip proto 'proto' and not a fragment other than the first fragment */
5087 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5093 b1
= gen_portatom(0, (bpf_int32
)port
);
5097 b1
= gen_portatom(2, (bpf_int32
)port
);
5102 tmp
= gen_portatom(0, (bpf_int32
)port
);
5103 b1
= gen_portatom(2, (bpf_int32
)port
);
5108 tmp
= gen_portatom(0, (bpf_int32
)port
);
5109 b1
= gen_portatom(2, (bpf_int32
)port
);
5121 static struct block
*
5122 gen_port(port
, ip_proto
, dir
)
5127 struct block
*b0
, *b1
, *tmp
;
5132 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5133 * not LLC encapsulation with LLCSAP_IP.
5135 * For IEEE 802 networks - which includes 802.5 token ring
5136 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5137 * says that SNAP encapsulation is used, not LLC encapsulation
5140 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5141 * RFC 2225 say that SNAP encapsulation is used, not LLC
5142 * encapsulation with LLCSAP_IP.
5144 * So we always check for ETHERTYPE_IP.
5146 b0
= gen_linktype(ETHERTYPE_IP
);
5152 b1
= gen_portop(port
, ip_proto
, dir
);
5156 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5157 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5159 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5171 gen_portop6(port
, proto
, dir
)
5172 int port
, proto
, dir
;
5174 struct block
*b0
, *b1
, *tmp
;
5176 /* ip6 proto 'proto' */
5177 /* XXX - catch the first fragment of a fragmented packet? */
5178 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5182 b1
= gen_portatom6(0, (bpf_int32
)port
);
5186 b1
= gen_portatom6(2, (bpf_int32
)port
);
5191 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5192 b1
= gen_portatom6(2, (bpf_int32
)port
);
5197 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5198 b1
= gen_portatom6(2, (bpf_int32
)port
);
5210 static struct block
*
5211 gen_port6(port
, ip_proto
, dir
)
5216 struct block
*b0
, *b1
, *tmp
;
5218 /* link proto ip6 */
5219 b0
= gen_linktype(ETHERTYPE_IPV6
);
5225 b1
= gen_portop6(port
, ip_proto
, dir
);
5229 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5230 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5232 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5243 /* gen_portrange code */
5244 static struct block
*
5245 gen_portrangeatom(off
, v1
, v2
)
5249 struct block
*b1
, *b2
;
5253 * Reverse the order of the ports, so v1 is the lower one.
5262 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5263 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5271 gen_portrangeop(port1
, port2
, proto
, dir
)
5276 struct block
*b0
, *b1
, *tmp
;
5278 /* ip proto 'proto' and not a fragment other than the first fragment */
5279 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5285 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5289 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5294 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5295 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5300 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5301 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5313 static struct block
*
5314 gen_portrange(port1
, port2
, ip_proto
, dir
)
5319 struct block
*b0
, *b1
, *tmp
;
5322 b0
= gen_linktype(ETHERTYPE_IP
);
5328 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5332 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5333 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5335 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5346 static struct block
*
5347 gen_portrangeatom6(off
, v1
, v2
)
5351 struct block
*b1
, *b2
;
5355 * Reverse the order of the ports, so v1 is the lower one.
5364 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5365 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5373 gen_portrangeop6(port1
, port2
, proto
, dir
)
5378 struct block
*b0
, *b1
, *tmp
;
5380 /* ip6 proto 'proto' */
5381 /* XXX - catch the first fragment of a fragmented packet? */
5382 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5386 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5390 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5395 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5396 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5401 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5402 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5414 static struct block
*
5415 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5420 struct block
*b0
, *b1
, *tmp
;
5422 /* link proto ip6 */
5423 b0
= gen_linktype(ETHERTYPE_IPV6
);
5429 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5433 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5434 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5436 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5448 lookup_proto(name
, proto
)
5449 register const char *name
;
5459 v
= pcap_nametoproto(name
);
5460 if (v
== PROTO_UNDEF
)
5461 bpf_error("unknown ip proto '%s'", name
);
5465 /* XXX should look up h/w protocol type based on linktype */
5466 v
= pcap_nametoeproto(name
);
5467 if (v
== PROTO_UNDEF
) {
5468 v
= pcap_nametollc(name
);
5469 if (v
== PROTO_UNDEF
)
5470 bpf_error("unknown ether proto '%s'", name
);
5475 if (strcmp(name
, "esis") == 0)
5477 else if (strcmp(name
, "isis") == 0)
5479 else if (strcmp(name
, "clnp") == 0)
5482 bpf_error("unknown osi proto '%s'", name
);
5502 static struct block
*
5503 gen_protochain(v
, proto
, dir
)
5508 #ifdef NO_PROTOCHAIN
5509 return gen_proto(v
, proto
, dir
);
5511 struct block
*b0
, *b
;
5512 struct slist
*s
[100];
5513 int fix2
, fix3
, fix4
, fix5
;
5514 int ahcheck
, again
, end
;
5516 int reg2
= alloc_reg();
5518 memset(s
, 0, sizeof(s
));
5519 fix2
= fix3
= fix4
= fix5
= 0;
5526 b0
= gen_protochain(v
, Q_IP
, dir
);
5527 b
= gen_protochain(v
, Q_IPV6
, dir
);
5531 bpf_error("bad protocol applied for 'protochain'");
5536 * We don't handle variable-length prefixes before the link-layer
5537 * header, or variable-length link-layer headers, here yet.
5538 * We might want to add BPF instructions to do the protochain
5539 * work, to simplify that and, on platforms that have a BPF
5540 * interpreter with the new instructions, let the filtering
5541 * be done in the kernel. (We already require a modified BPF
5542 * engine to do the protochain stuff, to support backward
5543 * branches, and backward branch support is unlikely to appear
5544 * in kernel BPF engines.)
5548 case DLT_IEEE802_11
:
5549 case DLT_PRISM_HEADER
:
5550 case DLT_IEEE802_11_RADIO_AVS
:
5551 case DLT_IEEE802_11_RADIO
:
5553 bpf_error("'protochain' not supported with 802.11");
5556 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5559 * s[0] is a dummy entry to protect other BPF insn from damage
5560 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5561 * hard to find interdependency made by jump table fixup.
5564 s
[i
] = new_stmt(0); /*dummy*/
5569 b0
= gen_linktype(ETHERTYPE_IP
);
5572 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5573 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5575 /* X = ip->ip_hl << 2 */
5576 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5577 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5582 b0
= gen_linktype(ETHERTYPE_IPV6
);
5584 /* A = ip6->ip_nxt */
5585 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5586 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5588 /* X = sizeof(struct ip6_hdr) */
5589 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5595 bpf_error("unsupported proto to gen_protochain");
5599 /* again: if (A == v) goto end; else fall through; */
5601 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5603 s
[i
]->s
.jt
= NULL
; /*later*/
5604 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5608 #ifndef IPPROTO_NONE
5609 #define IPPROTO_NONE 59
5611 /* if (A == IPPROTO_NONE) goto end */
5612 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5613 s
[i
]->s
.jt
= NULL
; /*later*/
5614 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5615 s
[i
]->s
.k
= IPPROTO_NONE
;
5616 s
[fix5
]->s
.jf
= s
[i
];
5620 if (proto
== Q_IPV6
) {
5621 int v6start
, v6end
, v6advance
, j
;
5624 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5625 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5626 s
[i
]->s
.jt
= NULL
; /*later*/
5627 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5628 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5629 s
[fix2
]->s
.jf
= s
[i
];
5631 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5632 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5633 s
[i
]->s
.jt
= NULL
; /*later*/
5634 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5635 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5637 /* if (A == IPPROTO_ROUTING) goto v6advance */
5638 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5639 s
[i
]->s
.jt
= NULL
; /*later*/
5640 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5641 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5643 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5644 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5645 s
[i
]->s
.jt
= NULL
; /*later*/
5646 s
[i
]->s
.jf
= NULL
; /*later*/
5647 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5657 * A = P[X + packet head];
5658 * X = X + (P[X + packet head + 1] + 1) * 8;
5660 /* A = P[X + packet head] */
5661 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5662 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5665 s
[i
] = new_stmt(BPF_ST
);
5668 /* A = P[X + packet head + 1]; */
5669 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5670 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5673 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5677 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5681 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5685 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5688 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5692 /* goto again; (must use BPF_JA for backward jump) */
5693 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5694 s
[i
]->s
.k
= again
- i
- 1;
5695 s
[i
- 1]->s
.jf
= s
[i
];
5699 for (j
= v6start
; j
<= v6end
; j
++)
5700 s
[j
]->s
.jt
= s
[v6advance
];
5703 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5705 s
[fix2
]->s
.jf
= s
[i
];
5711 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5712 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5713 s
[i
]->s
.jt
= NULL
; /*later*/
5714 s
[i
]->s
.jf
= NULL
; /*later*/
5715 s
[i
]->s
.k
= IPPROTO_AH
;
5717 s
[fix3
]->s
.jf
= s
[ahcheck
];
5724 * X = X + (P[X + 1] + 2) * 4;
5727 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5729 /* A = P[X + packet head]; */
5730 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5731 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5734 s
[i
] = new_stmt(BPF_ST
);
5738 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5741 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5745 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5747 /* A = P[X + packet head] */
5748 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5749 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5752 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5756 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5760 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5763 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5767 /* goto again; (must use BPF_JA for backward jump) */
5768 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5769 s
[i
]->s
.k
= again
- i
- 1;
5774 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5776 s
[fix2
]->s
.jt
= s
[end
];
5777 s
[fix4
]->s
.jf
= s
[end
];
5778 s
[fix5
]->s
.jt
= s
[end
];
5785 for (i
= 0; i
< max
- 1; i
++)
5786 s
[i
]->next
= s
[i
+ 1];
5787 s
[max
- 1]->next
= NULL
;
5792 b
= new_block(JMP(BPF_JEQ
));
5793 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5803 static struct block
*
5804 gen_check_802_11_data_frame()
5807 struct block
*b0
, *b1
;
5810 * A data frame has the 0x08 bit (b3) in the frame control field set
5811 * and the 0x04 bit (b2) clear.
5813 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5814 b0
= new_block(JMP(BPF_JSET
));
5818 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5819 b1
= new_block(JMP(BPF_JSET
));
5830 * Generate code that checks whether the packet is a packet for protocol
5831 * <proto> and whether the type field in that protocol's header has
5832 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5833 * IP packet and checks the protocol number in the IP header against <v>.
5835 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5836 * against Q_IP and Q_IPV6.
5838 static struct block
*
5839 gen_proto(v
, proto
, dir
)
5844 struct block
*b0
, *b1
;
5849 if (dir
!= Q_DEFAULT
)
5850 bpf_error("direction applied to 'proto'");
5854 b0
= gen_proto(v
, Q_IP
, dir
);
5855 b1
= gen_proto(v
, Q_IPV6
, dir
);
5861 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5862 * not LLC encapsulation with LLCSAP_IP.
5864 * For IEEE 802 networks - which includes 802.5 token ring
5865 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5866 * says that SNAP encapsulation is used, not LLC encapsulation
5869 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5870 * RFC 2225 say that SNAP encapsulation is used, not LLC
5871 * encapsulation with LLCSAP_IP.
5873 * So we always check for ETHERTYPE_IP.
5875 b0
= gen_linktype(ETHERTYPE_IP
);
5877 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5879 b1
= gen_protochain(v
, Q_IP
);
5889 * Frame Relay packets typically have an OSI
5890 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5891 * generates code to check for all the OSI
5892 * NLPIDs, so calling it and then adding a check
5893 * for the particular NLPID for which we're
5894 * looking is bogus, as we can just check for
5897 * What we check for is the NLPID and a frame
5898 * control field value of UI, i.e. 0x03 followed
5901 * XXX - assumes a 2-byte Frame Relay header with
5902 * DLCI and flags. What if the address is longer?
5904 * XXX - what about SNAP-encapsulated frames?
5906 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5912 * Cisco uses an Ethertype lookalike - for OSI,
5915 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5916 /* OSI in C-HDLC is stuffed with a fudge byte */
5917 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
5922 b0
= gen_linktype(LLCSAP_ISONS
);
5923 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
5929 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5931 * 4 is the offset of the PDU type relative to the IS-IS
5934 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
5939 bpf_error("arp does not encapsulate another protocol");
5943 bpf_error("rarp does not encapsulate another protocol");
5947 bpf_error("atalk encapsulation is not specifiable");
5951 bpf_error("decnet encapsulation is not specifiable");
5955 bpf_error("sca does not encapsulate another protocol");
5959 bpf_error("lat does not encapsulate another protocol");
5963 bpf_error("moprc does not encapsulate another protocol");
5967 bpf_error("mopdl does not encapsulate another protocol");
5971 return gen_linktype(v
);
5974 bpf_error("'udp proto' is bogus");
5978 bpf_error("'tcp proto' is bogus");
5982 bpf_error("'sctp proto' is bogus");
5986 bpf_error("'icmp proto' is bogus");
5990 bpf_error("'igmp proto' is bogus");
5994 bpf_error("'igrp proto' is bogus");
5998 bpf_error("'pim proto' is bogus");
6002 bpf_error("'vrrp proto' is bogus");
6006 bpf_error("'carp proto' is bogus");
6010 b0
= gen_linktype(ETHERTYPE_IPV6
);
6013 * Also check for a fragment header before the final
6016 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6017 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6019 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6022 b1
= gen_protochain(v
, Q_IPV6
);
6028 bpf_error("'icmp6 proto' is bogus");
6031 bpf_error("'ah proto' is bogus");
6034 bpf_error("'ah proto' is bogus");
6037 bpf_error("'stp proto' is bogus");
6040 bpf_error("'ipx proto' is bogus");
6043 bpf_error("'netbeui proto' is bogus");
6046 bpf_error("'radio proto' is bogus");
6057 register const char *name
;
6060 int proto
= q
.proto
;
6064 bpf_u_int32 mask
, addr
;
6066 bpf_u_int32
**alist
;
6069 struct sockaddr_in
*sin4
;
6070 struct sockaddr_in6
*sin6
;
6071 struct addrinfo
*res
, *res0
;
6072 struct in6_addr mask128
;
6074 struct block
*b
, *tmp
;
6075 int port
, real_proto
;
6081 addr
= pcap_nametonetaddr(name
);
6083 bpf_error("unknown network '%s'", name
);
6084 /* Left justify network addr and calculate its network mask */
6086 while (addr
&& (addr
& 0xff000000) == 0) {
6090 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6094 if (proto
== Q_LINK
) {
6098 case DLT_NETANALYZER
:
6099 case DLT_NETANALYZER_TRANSPARENT
:
6100 eaddr
= pcap_ether_hostton(name
);
6103 "unknown ether host '%s'", name
);
6104 tmp
= gen_prevlinkhdr_check();
6105 b
= gen_ehostop(eaddr
, dir
);
6112 eaddr
= pcap_ether_hostton(name
);
6115 "unknown FDDI host '%s'", name
);
6116 b
= gen_fhostop(eaddr
, dir
);
6121 eaddr
= pcap_ether_hostton(name
);
6124 "unknown token ring host '%s'", name
);
6125 b
= gen_thostop(eaddr
, dir
);
6129 case DLT_IEEE802_11
:
6130 case DLT_PRISM_HEADER
:
6131 case DLT_IEEE802_11_RADIO_AVS
:
6132 case DLT_IEEE802_11_RADIO
:
6134 eaddr
= pcap_ether_hostton(name
);
6137 "unknown 802.11 host '%s'", name
);
6138 b
= gen_wlanhostop(eaddr
, dir
);
6142 case DLT_IP_OVER_FC
:
6143 eaddr
= pcap_ether_hostton(name
);
6146 "unknown Fibre Channel host '%s'", name
);
6147 b
= gen_ipfchostop(eaddr
, dir
);
6152 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6153 } else if (proto
== Q_DECNET
) {
6154 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6156 * I don't think DECNET hosts can be multihomed, so
6157 * there is no need to build up a list of addresses
6159 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6162 alist
= pcap_nametoaddr(name
);
6163 if (alist
== NULL
|| *alist
== NULL
)
6164 bpf_error("unknown host '%s'", name
);
6166 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6168 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6170 tmp
= gen_host(**alist
++, 0xffffffff,
6171 tproto
, dir
, q
.addr
);
6177 memset(&mask128
, 0xff, sizeof(mask128
));
6178 res0
= res
= pcap_nametoaddrinfo(name
);
6180 bpf_error("unknown host '%s'", name
);
6183 tproto
= tproto6
= proto
;
6184 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6188 for (res
= res0
; res
; res
= res
->ai_next
) {
6189 switch (res
->ai_family
) {
6191 if (tproto
== Q_IPV6
)
6194 sin4
= (struct sockaddr_in
*)
6196 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6197 0xffffffff, tproto
, dir
, q
.addr
);
6200 if (tproto6
== Q_IP
)
6203 sin6
= (struct sockaddr_in6
*)
6205 tmp
= gen_host6(&sin6
->sin6_addr
,
6206 &mask128
, tproto6
, dir
, q
.addr
);
6218 bpf_error("unknown host '%s'%s", name
,
6219 (proto
== Q_DEFAULT
)
6221 : " for specified address family");
6228 if (proto
!= Q_DEFAULT
&&
6229 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6230 bpf_error("illegal qualifier of 'port'");
6231 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6232 bpf_error("unknown port '%s'", name
);
6233 if (proto
== Q_UDP
) {
6234 if (real_proto
== IPPROTO_TCP
)
6235 bpf_error("port '%s' is tcp", name
);
6236 else if (real_proto
== IPPROTO_SCTP
)
6237 bpf_error("port '%s' is sctp", name
);
6239 /* override PROTO_UNDEF */
6240 real_proto
= IPPROTO_UDP
;
6242 if (proto
== Q_TCP
) {
6243 if (real_proto
== IPPROTO_UDP
)
6244 bpf_error("port '%s' is udp", name
);
6246 else if (real_proto
== IPPROTO_SCTP
)
6247 bpf_error("port '%s' is sctp", name
);
6249 /* override PROTO_UNDEF */
6250 real_proto
= IPPROTO_TCP
;
6252 if (proto
== Q_SCTP
) {
6253 if (real_proto
== IPPROTO_UDP
)
6254 bpf_error("port '%s' is udp", name
);
6256 else if (real_proto
== IPPROTO_TCP
)
6257 bpf_error("port '%s' is tcp", name
);
6259 /* override PROTO_UNDEF */
6260 real_proto
= IPPROTO_SCTP
;
6263 bpf_error("illegal port number %d < 0", port
);
6265 bpf_error("illegal port number %d > 65535", port
);
6266 b
= gen_port(port
, real_proto
, dir
);
6267 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6271 if (proto
!= Q_DEFAULT
&&
6272 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6273 bpf_error("illegal qualifier of 'portrange'");
6274 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6275 bpf_error("unknown port in range '%s'", name
);
6276 if (proto
== Q_UDP
) {
6277 if (real_proto
== IPPROTO_TCP
)
6278 bpf_error("port in range '%s' is tcp", name
);
6279 else if (real_proto
== IPPROTO_SCTP
)
6280 bpf_error("port in range '%s' is sctp", name
);
6282 /* override PROTO_UNDEF */
6283 real_proto
= IPPROTO_UDP
;
6285 if (proto
== Q_TCP
) {
6286 if (real_proto
== IPPROTO_UDP
)
6287 bpf_error("port in range '%s' is udp", name
);
6288 else if (real_proto
== IPPROTO_SCTP
)
6289 bpf_error("port in range '%s' is sctp", name
);
6291 /* override PROTO_UNDEF */
6292 real_proto
= IPPROTO_TCP
;
6294 if (proto
== Q_SCTP
) {
6295 if (real_proto
== IPPROTO_UDP
)
6296 bpf_error("port in range '%s' is udp", name
);
6297 else if (real_proto
== IPPROTO_TCP
)
6298 bpf_error("port in range '%s' is tcp", name
);
6300 /* override PROTO_UNDEF */
6301 real_proto
= IPPROTO_SCTP
;
6304 bpf_error("illegal port number %d < 0", port1
);
6306 bpf_error("illegal port number %d > 65535", port1
);
6308 bpf_error("illegal port number %d < 0", port2
);
6310 bpf_error("illegal port number %d > 65535", port2
);
6312 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6313 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6318 eaddr
= pcap_ether_hostton(name
);
6320 bpf_error("unknown ether host: %s", name
);
6322 alist
= pcap_nametoaddr(name
);
6323 if (alist
== NULL
|| *alist
== NULL
)
6324 bpf_error("unknown host '%s'", name
);
6325 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6329 bpf_error("'gateway' not supported in this configuration");
6333 real_proto
= lookup_proto(name
, proto
);
6334 if (real_proto
>= 0)
6335 return gen_proto(real_proto
, proto
, dir
);
6337 bpf_error("unknown protocol: %s", name
);
6340 real_proto
= lookup_proto(name
, proto
);
6341 if (real_proto
>= 0)
6342 return gen_protochain(real_proto
, proto
, dir
);
6344 bpf_error("unknown protocol: %s", name
);
6355 gen_mcode(s1
, s2
, masklen
, q
)
6356 register const char *s1
, *s2
;
6357 register unsigned int masklen
;
6360 register int nlen
, mlen
;
6363 nlen
= __pcap_atoin(s1
, &n
);
6364 /* Promote short ipaddr */
6368 mlen
= __pcap_atoin(s2
, &m
);
6369 /* Promote short ipaddr */
6372 bpf_error("non-network bits set in \"%s mask %s\"",
6375 /* Convert mask len to mask */
6377 bpf_error("mask length must be <= 32");
6380 * X << 32 is not guaranteed by C to be 0; it's
6385 m
= 0xffffffff << (32 - masklen
);
6387 bpf_error("non-network bits set in \"%s/%d\"",
6394 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6397 bpf_error("Mask syntax for networks only");
6406 register const char *s
;
6411 int proto
= q
.proto
;
6417 else if (q
.proto
== Q_DECNET
)
6418 vlen
= __pcap_atodn(s
, &v
);
6420 vlen
= __pcap_atoin(s
, &v
);
6427 if (proto
== Q_DECNET
)
6428 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6429 else if (proto
== Q_LINK
) {
6430 bpf_error("illegal link layer address");
6433 if (s
== NULL
&& q
.addr
== Q_NET
) {
6434 /* Promote short net number */
6435 while (v
&& (v
& 0xff000000) == 0) {
6440 /* Promote short ipaddr */
6444 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6449 proto
= IPPROTO_UDP
;
6450 else if (proto
== Q_TCP
)
6451 proto
= IPPROTO_TCP
;
6452 else if (proto
== Q_SCTP
)
6453 proto
= IPPROTO_SCTP
;
6454 else if (proto
== Q_DEFAULT
)
6455 proto
= PROTO_UNDEF
;
6457 bpf_error("illegal qualifier of 'port'");
6460 bpf_error("illegal port number %u > 65535", v
);
6464 b
= gen_port((int)v
, proto
, dir
);
6465 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6471 proto
= IPPROTO_UDP
;
6472 else if (proto
== Q_TCP
)
6473 proto
= IPPROTO_TCP
;
6474 else if (proto
== Q_SCTP
)
6475 proto
= IPPROTO_SCTP
;
6476 else if (proto
== Q_DEFAULT
)
6477 proto
= PROTO_UNDEF
;
6479 bpf_error("illegal qualifier of 'portrange'");
6482 bpf_error("illegal port number %u > 65535", v
);
6486 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6487 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6492 bpf_error("'gateway' requires a name");
6496 return gen_proto((int)v
, proto
, dir
);
6499 return gen_protochain((int)v
, proto
, dir
);
6514 gen_mcode6(s1
, s2
, masklen
, q
)
6515 register const char *s1
, *s2
;
6516 register unsigned int masklen
;
6519 struct addrinfo
*res
;
6520 struct in6_addr
*addr
;
6521 struct in6_addr mask
;
6526 bpf_error("no mask %s supported", s2
);
6528 res
= pcap_nametoaddrinfo(s1
);
6530 bpf_error("invalid ip6 address %s", s1
);
6533 bpf_error("%s resolved to multiple address", s1
);
6534 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6536 if (sizeof(mask
) * 8 < masklen
)
6537 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6538 memset(&mask
, 0, sizeof(mask
));
6539 memset(&mask
, 0xff, masklen
/ 8);
6541 mask
.s6_addr
[masklen
/ 8] =
6542 (0xff << (8 - masklen
% 8)) & 0xff;
6545 a
= (u_int32_t
*)addr
;
6546 m
= (u_int32_t
*)&mask
;
6547 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6548 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6549 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6557 bpf_error("Mask syntax for networks only");
6561 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6567 bpf_error("invalid qualifier against IPv6 address");
6576 register const u_char
*eaddr
;
6579 struct block
*b
, *tmp
;
6581 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6584 case DLT_NETANALYZER
:
6585 case DLT_NETANALYZER_TRANSPARENT
:
6586 tmp
= gen_prevlinkhdr_check();
6587 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6592 return gen_fhostop(eaddr
, (int)q
.dir
);
6594 return gen_thostop(eaddr
, (int)q
.dir
);
6595 case DLT_IEEE802_11
:
6596 case DLT_PRISM_HEADER
:
6597 case DLT_IEEE802_11_RADIO_AVS
:
6598 case DLT_IEEE802_11_RADIO
:
6600 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6601 case DLT_IP_OVER_FC
:
6602 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6604 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6608 bpf_error("ethernet address used in non-ether expression");
6615 struct slist
*s0
, *s1
;
6618 * This is definitely not the best way to do this, but the
6619 * lists will rarely get long.
6626 static struct slist
*
6632 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6637 static struct slist
*
6643 s
= new_stmt(BPF_LD
|BPF_MEM
);
6649 * Modify "index" to use the value stored into its register as an
6650 * offset relative to the beginning of the header for the protocol
6651 * "proto", and allocate a register and put an item "size" bytes long
6652 * (1, 2, or 4) at that offset into that register, making it the register
6656 gen_load(proto
, inst
, size
)
6661 struct slist
*s
, *tmp
;
6663 int regno
= alloc_reg();
6665 free_reg(inst
->regno
);
6669 bpf_error("data size must be 1, 2, or 4");
6685 bpf_error("unsupported index operation");
6689 * The offset is relative to the beginning of the packet
6690 * data, if we have a radio header. (If we don't, this
6693 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6694 linktype
!= DLT_IEEE802_11_RADIO
&&
6695 linktype
!= DLT_PRISM_HEADER
)
6696 bpf_error("radio information not present in capture");
6699 * Load into the X register the offset computed into the
6700 * register specified by "index".
6702 s
= xfer_to_x(inst
);
6705 * Load the item at that offset.
6707 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6709 sappend(inst
->s
, s
);
6714 * The offset is relative to the beginning of
6715 * the link-layer header.
6717 * XXX - what about ATM LANE? Should the index be
6718 * relative to the beginning of the AAL5 frame, so
6719 * that 0 refers to the beginning of the LE Control
6720 * field, or relative to the beginning of the LAN
6721 * frame, so that 0 refers, for Ethernet LANE, to
6722 * the beginning of the destination address?
6724 s
= gen_abs_offset_varpart(&off_linkhdr
);
6727 * If "s" is non-null, it has code to arrange that the
6728 * X register contains the length of the prefix preceding
6729 * the link-layer header. Add to it the offset computed
6730 * into the register specified by "index", and move that
6731 * into the X register. Otherwise, just load into the X
6732 * register the offset computed into the register specified
6736 sappend(s
, xfer_to_a(inst
));
6737 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6738 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6740 s
= xfer_to_x(inst
);
6743 * Load the item at the sum of the offset we've put in the
6744 * X register and the offset of the start of the link
6745 * layer header (which is 0 if the radio header is
6746 * variable-length; that header length is what we put
6747 * into the X register and then added to the index).
6749 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6750 tmp
->s
.k
= off_linkhdr
.constant_part
;
6752 sappend(inst
->s
, s
);
6766 * The offset is relative to the beginning of
6767 * the network-layer header.
6768 * XXX - are there any cases where we want
6771 s
= gen_abs_offset_varpart(&off_linkpl
);
6774 * If "s" is non-null, it has code to arrange that the
6775 * X register contains the variable part of the offset
6776 * of the link-layer payload. Add to it the offset
6777 * computed into the register specified by "index",
6778 * and move that into the X register. Otherwise, just
6779 * load into the X register the offset computed into
6780 * the register specified by "index".
6783 sappend(s
, xfer_to_a(inst
));
6784 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6785 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6787 s
= xfer_to_x(inst
);
6790 * Load the item at the sum of the offset we've put in the
6791 * X register, the offset of the start of the network
6792 * layer header from the beginning of the link-layer
6793 * payload, and the constant part of the offset of the
6794 * start of the link-layer payload.
6796 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6797 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6799 sappend(inst
->s
, s
);
6802 * Do the computation only if the packet contains
6803 * the protocol in question.
6805 b
= gen_proto_abbrev(proto
);
6807 gen_and(inst
->b
, b
);
6821 * The offset is relative to the beginning of
6822 * the transport-layer header.
6824 * Load the X register with the length of the IPv4 header
6825 * (plus the offset of the link-layer header, if it's
6826 * a variable-length header), in bytes.
6828 * XXX - are there any cases where we want
6830 * XXX - we should, if we're built with
6831 * IPv6 support, generate code to load either
6832 * IPv4, IPv6, or both, as appropriate.
6834 s
= gen_loadx_iphdrlen();
6837 * The X register now contains the sum of the variable
6838 * part of the offset of the link-layer payload and the
6839 * length of the network-layer header.
6841 * Load into the A register the offset relative to
6842 * the beginning of the transport layer header,
6843 * add the X register to that, move that to the
6844 * X register, and load with an offset from the
6845 * X register equal to the sum of the constant part of
6846 * the offset of the link-layer payload and the offset,
6847 * relative to the beginning of the link-layer payload,
6848 * of the network-layer header.
6850 sappend(s
, xfer_to_a(inst
));
6851 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6852 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6853 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6854 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6855 sappend(inst
->s
, s
);
6858 * Do the computation only if the packet contains
6859 * the protocol in question - which is true only
6860 * if this is an IP datagram and is the first or
6861 * only fragment of that datagram.
6863 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6865 gen_and(inst
->b
, b
);
6866 gen_and(gen_proto_abbrev(Q_IP
), b
);
6870 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6873 inst
->regno
= regno
;
6874 s
= new_stmt(BPF_ST
);
6876 sappend(inst
->s
, s
);
6882 gen_relation(code
, a0
, a1
, reversed
)
6884 struct arth
*a0
, *a1
;
6887 struct slist
*s0
, *s1
, *s2
;
6888 struct block
*b
, *tmp
;
6892 if (code
== BPF_JEQ
) {
6893 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6894 b
= new_block(JMP(code
));
6898 b
= new_block(BPF_JMP
|code
|BPF_X
);
6904 sappend(a0
->s
, a1
->s
);
6908 free_reg(a0
->regno
);
6909 free_reg(a1
->regno
);
6911 /* 'and' together protocol checks */
6914 gen_and(a0
->b
, tmp
= a1
->b
);
6930 int regno
= alloc_reg();
6931 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6934 s
= new_stmt(BPF_LD
|BPF_LEN
);
6935 s
->next
= new_stmt(BPF_ST
);
6936 s
->next
->s
.k
= regno
;
6951 a
= (struct arth
*)newchunk(sizeof(*a
));
6955 s
= new_stmt(BPF_LD
|BPF_IMM
);
6957 s
->next
= new_stmt(BPF_ST
);
6973 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6976 s
= new_stmt(BPF_ST
);
6984 gen_arth(code
, a0
, a1
)
6986 struct arth
*a0
, *a1
;
6988 struct slist
*s0
, *s1
, *s2
;
6992 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6997 sappend(a0
->s
, a1
->s
);
6999 free_reg(a0
->regno
);
7000 free_reg(a1
->regno
);
7002 s0
= new_stmt(BPF_ST
);
7003 a0
->regno
= s0
->s
.k
= alloc_reg();
7010 * Here we handle simple allocation of the scratch registers.
7011 * If too many registers are alloc'd, the allocator punts.
7013 static int regused
[BPF_MEMWORDS
];
7017 * Initialize the table of used registers and the current register.
7023 memset(regused
, 0, sizeof regused
);
7027 * Return the next free register.
7032 int n
= BPF_MEMWORDS
;
7035 if (regused
[curreg
])
7036 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7038 regused
[curreg
] = 1;
7042 bpf_error("too many registers needed to evaluate expression");
7048 * Return a register to the table so it can
7058 static struct block
*
7065 s
= new_stmt(BPF_LD
|BPF_LEN
);
7066 b
= new_block(JMP(jmp
));
7077 return gen_len(BPF_JGE
, n
);
7081 * Actually, this is less than or equal.
7089 b
= gen_len(BPF_JGT
, n
);
7096 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7097 * the beginning of the link-layer header.
7098 * XXX - that means you can't test values in the radiotap header, but
7099 * as that header is difficult if not impossible to parse generally
7100 * without a loop, that might not be a severe problem. A new keyword
7101 * "radio" could be added for that, although what you'd really want
7102 * would be a way of testing particular radio header values, which
7103 * would generate code appropriate to the radio header in question.
7106 gen_byteop(op
, idx
, val
)
7117 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7120 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7124 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7128 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7132 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7136 b
= new_block(JMP(BPF_JEQ
));
7143 static u_char abroadcast
[] = { 0x0 };
7146 gen_broadcast(proto
)
7149 bpf_u_int32 hostmask
;
7150 struct block
*b0
, *b1
, *b2
;
7151 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7159 case DLT_ARCNET_LINUX
:
7160 return gen_ahostop(abroadcast
, Q_DST
);
7162 case DLT_NETANALYZER
:
7163 case DLT_NETANALYZER_TRANSPARENT
:
7164 b1
= gen_prevlinkhdr_check();
7165 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7170 return gen_fhostop(ebroadcast
, Q_DST
);
7172 return gen_thostop(ebroadcast
, Q_DST
);
7173 case DLT_IEEE802_11
:
7174 case DLT_PRISM_HEADER
:
7175 case DLT_IEEE802_11_RADIO_AVS
:
7176 case DLT_IEEE802_11_RADIO
:
7178 return gen_wlanhostop(ebroadcast
, Q_DST
);
7179 case DLT_IP_OVER_FC
:
7180 return gen_ipfchostop(ebroadcast
, Q_DST
);
7182 bpf_error("not a broadcast link");
7188 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7189 * as an indication that we don't know the netmask, and fail
7192 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7193 bpf_error("netmask not known, so 'ip broadcast' not supported");
7194 b0
= gen_linktype(ETHERTYPE_IP
);
7195 hostmask
= ~netmask
;
7196 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7197 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7198 (bpf_int32
)(~0 & hostmask
), hostmask
);
7203 bpf_error("only link-layer/IP broadcast filters supported");
7209 * Generate code to test the low-order bit of a MAC address (that's
7210 * the bottom bit of the *first* byte).
7212 static struct block
*
7213 gen_mac_multicast(offset
)
7216 register struct block
*b0
;
7217 register struct slist
*s
;
7219 /* link[offset] & 1 != 0 */
7220 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7221 b0
= new_block(JMP(BPF_JSET
));
7228 gen_multicast(proto
)
7231 register struct block
*b0
, *b1
, *b2
;
7232 register struct slist
*s
;
7240 case DLT_ARCNET_LINUX
:
7241 /* all ARCnet multicasts use the same address */
7242 return gen_ahostop(abroadcast
, Q_DST
);
7244 case DLT_NETANALYZER
:
7245 case DLT_NETANALYZER_TRANSPARENT
:
7246 b1
= gen_prevlinkhdr_check();
7247 /* ether[0] & 1 != 0 */
7248 b0
= gen_mac_multicast(0);
7254 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7256 * XXX - was that referring to bit-order issues?
7258 /* fddi[1] & 1 != 0 */
7259 return gen_mac_multicast(1);
7261 /* tr[2] & 1 != 0 */
7262 return gen_mac_multicast(2);
7263 case DLT_IEEE802_11
:
7264 case DLT_PRISM_HEADER
:
7265 case DLT_IEEE802_11_RADIO_AVS
:
7266 case DLT_IEEE802_11_RADIO
:
7271 * For control frames, there is no DA.
7273 * For management frames, DA is at an
7274 * offset of 4 from the beginning of
7277 * For data frames, DA is at an offset
7278 * of 4 from the beginning of the packet
7279 * if To DS is clear and at an offset of
7280 * 16 from the beginning of the packet
7285 * Generate the tests to be done for data frames.
7287 * First, check for To DS set, i.e. "link[1] & 0x01".
7289 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7290 b1
= new_block(JMP(BPF_JSET
));
7291 b1
->s
.k
= 0x01; /* To DS */
7295 * If To DS is set, the DA is at 16.
7297 b0
= gen_mac_multicast(16);
7301 * Now, check for To DS not set, i.e. check
7302 * "!(link[1] & 0x01)".
7304 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7305 b2
= new_block(JMP(BPF_JSET
));
7306 b2
->s
.k
= 0x01; /* To DS */
7311 * If To DS is not set, the DA is at 4.
7313 b1
= gen_mac_multicast(4);
7317 * Now OR together the last two checks. That gives
7318 * the complete set of checks for data frames.
7323 * Now check for a data frame.
7324 * I.e, check "link[0] & 0x08".
7326 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7327 b1
= new_block(JMP(BPF_JSET
));
7332 * AND that with the checks done for data frames.
7337 * If the high-order bit of the type value is 0, this
7338 * is a management frame.
7339 * I.e, check "!(link[0] & 0x08)".
7341 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7342 b2
= new_block(JMP(BPF_JSET
));
7348 * For management frames, the DA is at 4.
7350 b1
= gen_mac_multicast(4);
7354 * OR that with the checks done for data frames.
7355 * That gives the checks done for management and
7361 * If the low-order bit of the type value is 1,
7362 * this is either a control frame or a frame
7363 * with a reserved type, and thus not a
7366 * I.e., check "!(link[0] & 0x04)".
7368 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7369 b1
= new_block(JMP(BPF_JSET
));
7375 * AND that with the checks for data and management
7380 case DLT_IP_OVER_FC
:
7381 b0
= gen_mac_multicast(2);
7386 /* Link not known to support multicasts */
7390 b0
= gen_linktype(ETHERTYPE_IP
);
7391 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7396 b0
= gen_linktype(ETHERTYPE_IPV6
);
7397 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7401 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7407 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7408 * Outbound traffic is sent by this machine, while inbound traffic is
7409 * sent by a remote machine (and may include packets destined for a
7410 * unicast or multicast link-layer address we are not subscribing to).
7411 * These are the same definitions implemented by pcap_setdirection().
7412 * Capturing only unicast traffic destined for this host is probably
7413 * better accomplished using a higher-layer filter.
7419 register struct block
*b0
;
7422 * Only some data link types support inbound/outbound qualifiers.
7426 b0
= gen_relation(BPF_JEQ
,
7427 gen_load(Q_LINK
, gen_loadi(0), 1),
7434 /* match outgoing packets */
7435 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7437 /* match incoming packets */
7438 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7443 /* match outgoing packets */
7444 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7446 /* to filter on inbound traffic, invert the match */
7451 #ifdef HAVE_NET_PFVAR_H
7453 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7454 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7460 /* match outgoing packets */
7461 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7463 /* match incoming packets */
7464 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7468 case DLT_JUNIPER_MFR
:
7469 case DLT_JUNIPER_MLFR
:
7470 case DLT_JUNIPER_MLPPP
:
7471 case DLT_JUNIPER_ATM1
:
7472 case DLT_JUNIPER_ATM2
:
7473 case DLT_JUNIPER_PPPOE
:
7474 case DLT_JUNIPER_PPPOE_ATM
:
7475 case DLT_JUNIPER_GGSN
:
7476 case DLT_JUNIPER_ES
:
7477 case DLT_JUNIPER_MONITOR
:
7478 case DLT_JUNIPER_SERVICES
:
7479 case DLT_JUNIPER_ETHER
:
7480 case DLT_JUNIPER_PPP
:
7481 case DLT_JUNIPER_FRELAY
:
7482 case DLT_JUNIPER_CHDLC
:
7483 case DLT_JUNIPER_VP
:
7484 case DLT_JUNIPER_ST
:
7485 case DLT_JUNIPER_ISM
:
7486 case DLT_JUNIPER_VS
:
7487 case DLT_JUNIPER_SRX_E2E
:
7488 case DLT_JUNIPER_FIBRECHANNEL
:
7489 case DLT_JUNIPER_ATM_CEMIC
:
7491 /* juniper flags (including direction) are stored
7492 * the byte after the 3-byte magic number */
7494 /* match outgoing packets */
7495 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7497 /* match incoming packets */
7498 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7504 * If we have packet meta-data indicating a direction,
7505 * check it, otherwise give up as this link-layer type
7506 * has nothing in the packet data.
7508 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7510 * This is Linux with PF_PACKET support.
7511 * If this is a *live* capture, we can look at
7512 * special meta-data in the filter expression;
7513 * if it's a savefile, we can't.
7515 if (bpf_pcap
->rfile
!= NULL
) {
7516 /* We have a FILE *, so this is a savefile */
7517 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7522 /* match outgoing packets */
7523 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7526 /* to filter on inbound traffic, invert the match */
7529 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7530 bpf_error("inbound/outbound not supported on linktype %d",
7534 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7539 #ifdef HAVE_NET_PFVAR_H
7540 /* PF firewall log matched interface */
7542 gen_pf_ifname(const char *ifname
)
7547 if (linktype
!= DLT_PFLOG
) {
7548 bpf_error("ifname supported only on PF linktype");
7551 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7552 off
= offsetof(struct pfloghdr
, ifname
);
7553 if (strlen(ifname
) >= len
) {
7554 bpf_error("ifname interface names can only be %d characters",
7558 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7562 /* PF firewall log ruleset name */
7564 gen_pf_ruleset(char *ruleset
)
7568 if (linktype
!= DLT_PFLOG
) {
7569 bpf_error("ruleset supported only on PF linktype");
7573 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7574 bpf_error("ruleset names can only be %ld characters",
7575 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7579 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7580 strlen(ruleset
), (const u_char
*)ruleset
);
7584 /* PF firewall log rule number */
7590 if (linktype
!= DLT_PFLOG
) {
7591 bpf_error("rnr supported only on PF linktype");
7595 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7600 /* PF firewall log sub-rule number */
7602 gen_pf_srnr(int srnr
)
7606 if (linktype
!= DLT_PFLOG
) {
7607 bpf_error("srnr supported only on PF linktype");
7611 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7616 /* PF firewall log reason code */
7618 gen_pf_reason(int reason
)
7622 if (linktype
!= DLT_PFLOG
) {
7623 bpf_error("reason supported only on PF linktype");
7627 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7632 /* PF firewall log action */
7634 gen_pf_action(int action
)
7638 if (linktype
!= DLT_PFLOG
) {
7639 bpf_error("action supported only on PF linktype");
7643 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7647 #else /* !HAVE_NET_PFVAR_H */
7649 gen_pf_ifname(const char *ifname
)
7651 bpf_error("libpcap was compiled without pf support");
7657 gen_pf_ruleset(char *ruleset
)
7659 bpf_error("libpcap was compiled on a machine without pf support");
7667 bpf_error("libpcap was compiled on a machine without pf support");
7673 gen_pf_srnr(int srnr
)
7675 bpf_error("libpcap was compiled on a machine without pf support");
7681 gen_pf_reason(int reason
)
7683 bpf_error("libpcap was compiled on a machine without pf support");
7689 gen_pf_action(int action
)
7691 bpf_error("libpcap was compiled on a machine without pf support");
7695 #endif /* HAVE_NET_PFVAR_H */
7697 /* IEEE 802.11 wireless header */
7699 gen_p80211_type(int type
, int mask
)
7705 case DLT_IEEE802_11
:
7706 case DLT_PRISM_HEADER
:
7707 case DLT_IEEE802_11_RADIO_AVS
:
7708 case DLT_IEEE802_11_RADIO
:
7709 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7714 bpf_error("802.11 link-layer types supported only on 802.11");
7722 gen_p80211_fcdir(int fcdir
)
7728 case DLT_IEEE802_11
:
7729 case DLT_PRISM_HEADER
:
7730 case DLT_IEEE802_11_RADIO_AVS
:
7731 case DLT_IEEE802_11_RADIO
:
7735 bpf_error("frame direction supported only with 802.11 headers");
7739 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7740 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7747 register const u_char
*eaddr
;
7753 case DLT_ARCNET_LINUX
:
7754 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7756 return (gen_ahostop(eaddr
, (int)q
.dir
));
7758 bpf_error("ARCnet address used in non-arc expression");
7764 bpf_error("aid supported only on ARCnet");
7767 bpf_error("ARCnet address used in non-arc expression");
7772 static struct block
*
7773 gen_ahostop(eaddr
, dir
)
7774 register const u_char
*eaddr
;
7777 register struct block
*b0
, *b1
;
7780 /* src comes first, different from Ethernet */
7782 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7785 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7788 b0
= gen_ahostop(eaddr
, Q_SRC
);
7789 b1
= gen_ahostop(eaddr
, Q_DST
);
7795 b0
= gen_ahostop(eaddr
, Q_SRC
);
7796 b1
= gen_ahostop(eaddr
, Q_DST
);
7801 bpf_error("'addr1' is only supported on 802.11");
7805 bpf_error("'addr2' is only supported on 802.11");
7809 bpf_error("'addr3' is only supported on 802.11");
7813 bpf_error("'addr4' is only supported on 802.11");
7817 bpf_error("'ra' is only supported on 802.11");
7821 bpf_error("'ta' is only supported on 802.11");
7828 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7829 static struct block
*
7830 gen_vlan_bpf_extensions(int vlan_num
)
7832 struct block
*b0
, *b1
;
7835 /* generate new filter code based on extracting packet
7837 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7838 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7840 b0
= new_block(JMP(BPF_JEQ
));
7844 if (vlan_num
>= 0) {
7845 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7846 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7848 b1
= new_block(JMP(BPF_JEQ
));
7850 b1
->s
.k
= (bpf_int32
) vlan_num
;
7860 static struct block
*
7861 gen_vlan_no_bpf_extensions(int vlan_num
)
7863 struct block
*b0
, *b1
;
7865 /* check for VLAN, including QinQ */
7866 b0
= gen_linktype(ETHERTYPE_8021Q
);
7867 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7871 /* If a specific VLAN is requested, check VLAN id */
7872 if (vlan_num
>= 0) {
7873 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7874 (bpf_int32
)vlan_num
, 0x0fff);
7880 * The payload follows the full header, including the
7881 * VLAN tags, so skip past this VLAN tag.
7883 off_linkpl
.constant_part
+= 4;
7886 * The link-layer type information follows the VLAN tags, so
7887 * skip past this VLAN tag.
7895 * support IEEE 802.1Q VLAN trunk over ethernet
7903 /* can't check for VLAN-encapsulated packets inside MPLS */
7904 if (label_stack_depth
> 0)
7905 bpf_error("no VLAN match after MPLS");
7908 * Check for a VLAN packet, and then change the offsets to point
7909 * to the type and data fields within the VLAN packet. Just
7910 * increment the offsets, so that we can support a hierarchy, e.g.
7911 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7914 * XXX - this is a bit of a kludge. If we were to split the
7915 * compiler into a parser that parses an expression and
7916 * generates an expression tree, and a code generator that
7917 * takes an expression tree (which could come from our
7918 * parser or from some other parser) and generates BPF code,
7919 * we could perhaps make the offsets parameters of routines
7920 * and, in the handler for an "AND" node, pass to subnodes
7921 * other than the VLAN node the adjusted offsets.
7923 * This would mean that "vlan" would, instead of changing the
7924 * behavior of *all* tests after it, change only the behavior
7925 * of tests ANDed with it. That would change the documented
7926 * semantics of "vlan", which might break some expressions.
7927 * However, it would mean that "(vlan and ip) or ip" would check
7928 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7929 * checking only for VLAN-encapsulated IP, so that could still
7930 * be considered worth doing; it wouldn't break expressions
7931 * that are of the form "vlan and ..." or "vlan N and ...",
7932 * which I suspect are the most common expressions involving
7933 * "vlan". "vlan or ..." doesn't necessarily do what the user
7934 * would really want, now, as all the "or ..." tests would
7935 * be done assuming a VLAN, even though the "or" could be viewed
7936 * as meaning "or, if this isn't a VLAN packet...".
7941 case DLT_NETANALYZER
:
7942 case DLT_NETANALYZER_TRANSPARENT
:
7943 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7944 if (vlan_stack_depth
== 0) {
7946 * Do we need special VLAN handling?
7948 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
7949 b0
= gen_vlan_bpf_extensions(vlan_num
);
7951 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7954 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7957 case DLT_IEEE802_11
:
7958 case DLT_PRISM_HEADER
:
7959 case DLT_IEEE802_11_RADIO_AVS
:
7960 case DLT_IEEE802_11_RADIO
:
7961 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7965 bpf_error("no VLAN support for data link type %d",
7982 struct block
*b0
, *b1
;
7984 if (label_stack_depth
> 0) {
7985 /* just match the bottom-of-stack bit clear */
7986 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
7989 * We're not in an MPLS stack yet, so check the link-layer
7990 * type against MPLS.
7994 case DLT_C_HDLC
: /* fall through */
7996 case DLT_NETANALYZER
:
7997 case DLT_NETANALYZER_TRANSPARENT
:
7998 b0
= gen_linktype(ETHERTYPE_MPLS
);
8002 b0
= gen_linktype(PPP_MPLS_UCAST
);
8005 /* FIXME add other DLT_s ...
8006 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8007 * leave it for now */
8010 bpf_error("no MPLS support for data link type %d",
8018 /* If a specific MPLS label is requested, check it */
8019 if (label_num
>= 0) {
8020 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8021 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8022 0xfffff000); /* only compare the first 20 bits */
8028 * Change the offsets to point to the type and data fields within
8029 * the MPLS packet. Just increment the offsets, so that we
8030 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8031 * capture packets with an outer label of 100000 and an inner
8034 * Increment the MPLS stack depth as well; this indicates that
8035 * we're checking MPLS-encapsulated headers, to make sure higher
8036 * level code generators don't try to match against IP-related
8037 * protocols such as Q_ARP, Q_RARP etc.
8039 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8043 label_stack_depth
++;
8048 * Support PPPOE discovery and session.
8053 /* check for PPPoE discovery */
8054 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8058 gen_pppoes(sess_num
)
8061 struct block
*b0
, *b1
;
8064 * Test against the PPPoE session link-layer type.
8066 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8068 /* If a specific session is requested, check PPPoE session id */
8069 if (sess_num
>= 0) {
8070 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8071 (bpf_int32
)sess_num
, 0x0000ffff);
8077 * Change the offsets to point to the type and data fields within
8078 * the PPP packet, and note that this is PPPoE rather than
8081 * XXX - this is a bit of a kludge. If we were to split the
8082 * compiler into a parser that parses an expression and
8083 * generates an expression tree, and a code generator that
8084 * takes an expression tree (which could come from our
8085 * parser or from some other parser) and generates BPF code,
8086 * we could perhaps make the offsets parameters of routines
8087 * and, in the handler for an "AND" node, pass to subnodes
8088 * other than the PPPoE node the adjusted offsets.
8090 * This would mean that "pppoes" would, instead of changing the
8091 * behavior of *all* tests after it, change only the behavior
8092 * of tests ANDed with it. That would change the documented
8093 * semantics of "pppoes", which might break some expressions.
8094 * However, it would mean that "(pppoes and ip) or ip" would check
8095 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8096 * checking only for VLAN-encapsulated IP, so that could still
8097 * be considered worth doing; it wouldn't break expressions
8098 * that are of the form "pppoes and ..." which I suspect are the
8099 * most common expressions involving "pppoes". "pppoes or ..."
8100 * doesn't necessarily do what the user would really want, now,
8101 * as all the "or ..." tests would be done assuming PPPoE, even
8102 * though the "or" could be viewed as meaning "or, if this isn't
8103 * a PPPoE packet...".
8105 * The "network-layer" protocol is PPPoE, which has a 6-byte
8106 * PPPoE header, followed by a PPP packet.
8108 * There is no HDLC encapsulation for the PPP packet (it's
8109 * encapsulated in PPPoES instead), so the link-layer type
8110 * starts at the first byte of the PPP packet. For PPPoE,
8111 * that offset is relative to the beginning of the total
8112 * link-layer payload, including any 802.2 LLC header, so
8113 * it's 6 bytes past off_nl.
8115 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8116 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8120 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8123 off_nl_nosnap
= 0; /* no 802.2 LLC */
8129 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8141 bpf_error("'vpi' supported only on raw ATM");
8142 if (off_vpi
== (u_int
)-1)
8144 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8150 bpf_error("'vci' supported only on raw ATM");
8151 if (off_vci
== (u_int
)-1)
8153 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8158 if (off_proto
== (u_int
)-1)
8159 abort(); /* XXX - this isn't on FreeBSD */
8160 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8165 if (off_payload
== (u_int
)-1)
8167 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8168 0xffffffff, jtype
, reverse
, jvalue
);
8173 bpf_error("'callref' supported only on raw ATM");
8174 if (off_proto
== (u_int
)-1)
8176 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8177 jtype
, reverse
, jvalue
);
8187 gen_atmtype_abbrev(type
)
8190 struct block
*b0
, *b1
;
8195 /* Get all packets in Meta signalling Circuit */
8197 bpf_error("'metac' supported only on raw ATM");
8198 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8199 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8204 /* Get all packets in Broadcast Circuit*/
8206 bpf_error("'bcc' supported only on raw ATM");
8207 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8208 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8213 /* Get all cells in Segment OAM F4 circuit*/
8215 bpf_error("'oam4sc' supported only on raw ATM");
8216 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8217 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8222 /* Get all cells in End-to-End OAM F4 Circuit*/
8224 bpf_error("'oam4ec' supported only on raw ATM");
8225 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8226 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8231 /* Get all packets in connection Signalling Circuit */
8233 bpf_error("'sc' supported only on raw ATM");
8234 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8235 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8240 /* Get all packets in ILMI Circuit */
8242 bpf_error("'ilmic' supported only on raw ATM");
8243 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8244 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8249 /* Get all LANE packets */
8251 bpf_error("'lane' supported only on raw ATM");
8252 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8255 * Arrange that all subsequent tests assume LANE
8256 * rather than LLC-encapsulated packets, and set
8257 * the offsets appropriately for LANE-encapsulated
8260 * We assume LANE means Ethernet, not Token Ring.
8262 PUSH_LINKHDR(DLT_EN10MB
, 0,
8263 off_payload
+ 2, /* Ethernet header */
8266 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8267 off_nl
= 0; /* Ethernet II */
8268 off_nl_nosnap
= 3; /* 802.3+802.2 */
8272 /* Get all LLC-encapsulated packets */
8274 bpf_error("'llc' supported only on raw ATM");
8275 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8276 linktype
= prevlinktype
;
8286 * Filtering for MTP2 messages based on li value
8287 * FISU, length is null
8288 * LSSU, length is 1 or 2
8289 * MSU, length is 3 or more
8290 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8293 gen_mtp2type_abbrev(type
)
8296 struct block
*b0
, *b1
;
8301 if ( (linktype
!= DLT_MTP2
) &&
8302 (linktype
!= DLT_ERF
) &&
8303 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8304 bpf_error("'fisu' supported only on MTP2");
8305 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8306 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8310 if ( (linktype
!= DLT_MTP2
) &&
8311 (linktype
!= DLT_ERF
) &&
8312 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8313 bpf_error("'lssu' supported only on MTP2");
8314 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8315 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8320 if ( (linktype
!= DLT_MTP2
) &&
8321 (linktype
!= DLT_ERF
) &&
8322 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8323 bpf_error("'msu' supported only on MTP2");
8324 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8328 if ( (linktype
!= DLT_MTP2
) &&
8329 (linktype
!= DLT_ERF
) &&
8330 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8331 bpf_error("'hfisu' supported only on MTP2_HSL");
8332 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8333 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8337 if ( (linktype
!= DLT_MTP2
) &&
8338 (linktype
!= DLT_ERF
) &&
8339 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8340 bpf_error("'hlssu' supported only on MTP2_HSL");
8341 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8342 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8347 if ( (linktype
!= DLT_MTP2
) &&
8348 (linktype
!= DLT_ERF
) &&
8349 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8350 bpf_error("'hmsu' supported only on MTP2_HSL");
8351 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8361 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8368 bpf_u_int32 val1
, val2
, val3
;
8369 u_int newoff_sio
=off_sio
;
8370 u_int newoff_opc
=off_opc
;
8371 u_int newoff_dpc
=off_dpc
;
8372 u_int newoff_sls
=off_sls
;
8374 switch (mtp3field
) {
8377 newoff_sio
+= 3; /* offset for MTP2_HSL */
8381 if (off_sio
== (u_int
)-1)
8382 bpf_error("'sio' supported only on SS7");
8383 /* sio coded on 1 byte so max value 255 */
8385 bpf_error("sio value %u too big; max value = 255",
8387 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8388 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8394 if (off_opc
== (u_int
)-1)
8395 bpf_error("'opc' supported only on SS7");
8396 /* opc coded on 14 bits so max value 16383 */
8398 bpf_error("opc value %u too big; max value = 16383",
8400 /* the following instructions are made to convert jvalue
8401 * to the form used to write opc in an ss7 message*/
8402 val1
= jvalue
& 0x00003c00;
8404 val2
= jvalue
& 0x000003fc;
8406 val3
= jvalue
& 0x00000003;
8408 jvalue
= val1
+ val2
+ val3
;
8409 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8410 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8418 if (off_dpc
== (u_int
)-1)
8419 bpf_error("'dpc' supported only on SS7");
8420 /* dpc coded on 14 bits so max value 16383 */
8422 bpf_error("dpc value %u too big; max value = 16383",
8424 /* the following instructions are made to convert jvalue
8425 * to the forme used to write dpc in an ss7 message*/
8426 val1
= jvalue
& 0x000000ff;
8428 val2
= jvalue
& 0x00003f00;
8430 jvalue
= val1
+ val2
;
8431 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8432 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8438 if (off_sls
== (u_int
)-1)
8439 bpf_error("'sls' supported only on SS7");
8440 /* sls coded on 4 bits so max value 15 */
8442 bpf_error("sls value %u too big; max value = 15",
8444 /* the following instruction is made to convert jvalue
8445 * to the forme used to write sls in an ss7 message*/
8446 jvalue
= jvalue
<< 4;
8447 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8448 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8457 static struct block
*
8458 gen_msg_abbrev(type
)
8464 * Q.2931 signalling protocol messages for handling virtual circuits
8465 * establishment and teardown
8470 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8474 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8478 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8482 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8486 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8489 case A_RELEASE_DONE
:
8490 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8500 gen_atmmulti_abbrev(type
)
8503 struct block
*b0
, *b1
;
8509 bpf_error("'oam' supported only on raw ATM");
8510 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8515 bpf_error("'oamf4' supported only on raw ATM");
8517 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8518 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8520 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8526 * Get Q.2931 signalling messages for switched
8527 * virtual connection
8530 bpf_error("'connectmsg' supported only on raw ATM");
8531 b0
= gen_msg_abbrev(A_SETUP
);
8532 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8534 b0
= gen_msg_abbrev(A_CONNECT
);
8536 b0
= gen_msg_abbrev(A_CONNECTACK
);
8538 b0
= gen_msg_abbrev(A_RELEASE
);
8540 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8542 b0
= gen_atmtype_abbrev(A_SC
);
8548 bpf_error("'metaconnect' supported only on raw ATM");
8549 b0
= gen_msg_abbrev(A_SETUP
);
8550 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8552 b0
= gen_msg_abbrev(A_CONNECT
);
8554 b0
= gen_msg_abbrev(A_RELEASE
);
8556 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8558 b0
= gen_atmtype_abbrev(A_METAC
);