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;
1484 bpf_error("unknown data link type %d", linktype
);
1489 * Load a value relative to the specified absolute offset.
1491 static struct slist
*
1492 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1494 struct slist
*s
, *s2
;
1496 s
= gen_abs_offset_varpart(abs_offset
);
1499 * If "s" is non-null, it has code to arrange that the X register
1500 * contains the variable part of the absolute offset, so we
1501 * generate a load relative to that, with an offset of
1502 * abs_offset->constant_part + offset.
1504 * Otherwise, we can do an absolute load with an offset of
1505 * abs_offset->constant_part + offset.
1509 * "s" points to a list of statements that puts the
1510 * variable part of the absolute offset into the X register.
1511 * Do an indirect load, to use the X register as an offset.
1513 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1514 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1518 * There is no variable part of the absolute offset, so
1519 * just do an absolute load.
1521 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1522 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1528 * Load a value relative to the beginning of the specified header.
1530 static struct slist
*
1531 gen_load_a(offrel
, offset
, size
)
1532 enum e_offrel offrel
;
1535 struct slist
*s
, *s2
;
1540 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1545 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1548 case OR_PREVLINKHDR
:
1549 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1553 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1556 case OR_PREVMPLSHDR
:
1557 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1561 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1564 case OR_LINKPL_NOSNAP
:
1565 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1570 * Load the X register with the length of the IPv4 header
1571 * (plus the offset of the link-layer header, if it's
1572 * preceded by a variable-length header such as a radio
1573 * header), in bytes.
1575 s
= gen_loadx_iphdrlen();
1578 * Load the item at {offset of the link-layer payload} +
1579 * {offset, relative to the start of the link-layer
1580 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1581 * {specified offset}.
1583 * If the offset of the link-layer payload is variable,
1584 * the variable part of that offset is included in the
1585 * value in the X register, and we include the constant
1586 * part in the offset of the load.
1588 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1589 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1594 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1605 * Generate code to load into the X register the sum of the length of
1606 * the IPv4 header and the variable part of the offset of the link-layer
1609 static struct slist
*
1610 gen_loadx_iphdrlen()
1612 struct slist
*s
, *s2
;
1614 s
= gen_abs_offset_varpart(&off_linkpl
);
1617 * The offset of the link-layer payload has a variable
1618 * part. "s" points to a list of statements that put
1619 * the variable part of that offset into the X register.
1621 * The 4*([k]&0xf) addressing mode can't be used, as we
1622 * don't have a constant offset, so we have to load the
1623 * value in question into the A register and add to it
1624 * the value from the X register.
1626 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1629 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1632 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1637 * The A register now contains the length of the IP header.
1638 * We need to add to it the variable part of the offset of
1639 * the link-layer payload, which is still in the X
1640 * register, and move the result into the X register.
1642 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1643 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1646 * The offset of the link-layer payload is a constant,
1647 * so no code was generated to load the (non-existent)
1648 * variable part of that offset.
1650 * This means we can use the 4*([k]&0xf) addressing
1651 * mode. Load the length of the IPv4 header, which
1652 * is at an offset of off_nl from the beginning of
1653 * the link-layer payload, and thus at an offset of
1654 * off_linkpl.constant_part + off_nl from the beginning
1655 * of the raw packet data, using that addressing mode.
1657 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1658 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1663 static struct block
*
1670 s
= new_stmt(BPF_LD
|BPF_IMM
);
1672 b
= new_block(JMP(BPF_JEQ
));
1678 static inline struct block
*
1681 return gen_uncond(1);
1684 static inline struct block
*
1687 return gen_uncond(0);
1691 * Byte-swap a 32-bit number.
1692 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1693 * big-endian platforms.)
1695 #define SWAPLONG(y) \
1696 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1699 * Generate code to match a particular packet type.
1701 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1702 * value, if <= ETHERMTU. We use that to determine whether to
1703 * match the type/length field or to check the type/length field for
1704 * a value <= ETHERMTU to see whether it's a type field and then do
1705 * the appropriate test.
1707 static struct block
*
1708 gen_ether_linktype(proto
)
1711 struct block
*b0
, *b1
;
1717 case LLCSAP_NETBEUI
:
1719 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1720 * so we check the DSAP and SSAP.
1722 * LLCSAP_IP checks for IP-over-802.2, rather
1723 * than IP-over-Ethernet or IP-over-SNAP.
1725 * XXX - should we check both the DSAP and the
1726 * SSAP, like this, or should we check just the
1727 * DSAP, as we do for other types <= ETHERMTU
1728 * (i.e., other SAP values)?
1730 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1732 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1733 ((proto
<< 8) | proto
));
1741 * Ethernet_II frames, which are Ethernet
1742 * frames with a frame type of ETHERTYPE_IPX;
1744 * Ethernet_802.3 frames, which are 802.3
1745 * frames (i.e., the type/length field is
1746 * a length field, <= ETHERMTU, rather than
1747 * a type field) with the first two bytes
1748 * after the Ethernet/802.3 header being
1751 * Ethernet_802.2 frames, which are 802.3
1752 * frames with an 802.2 LLC header and
1753 * with the IPX LSAP as the DSAP in the LLC
1756 * Ethernet_SNAP frames, which are 802.3
1757 * frames with an LLC header and a SNAP
1758 * header and with an OUI of 0x000000
1759 * (encapsulated Ethernet) and a protocol
1760 * ID of ETHERTYPE_IPX in the SNAP header.
1762 * XXX - should we generate the same code both
1763 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1767 * This generates code to check both for the
1768 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1770 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1771 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1775 * Now we add code to check for SNAP frames with
1776 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1778 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1782 * Now we generate code to check for 802.3
1783 * frames in general.
1785 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1789 * Now add the check for 802.3 frames before the
1790 * check for Ethernet_802.2 and Ethernet_802.3,
1791 * as those checks should only be done on 802.3
1792 * frames, not on Ethernet frames.
1797 * Now add the check for Ethernet_II frames, and
1798 * do that before checking for the other frame
1801 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1802 (bpf_int32
)ETHERTYPE_IPX
);
1806 case ETHERTYPE_ATALK
:
1807 case ETHERTYPE_AARP
:
1809 * EtherTalk (AppleTalk protocols on Ethernet link
1810 * layer) may use 802.2 encapsulation.
1814 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1815 * we check for an Ethernet type field less than
1816 * 1500, which means it's an 802.3 length field.
1818 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1822 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1823 * SNAP packets with an organization code of
1824 * 0x080007 (Apple, for Appletalk) and a protocol
1825 * type of ETHERTYPE_ATALK (Appletalk).
1827 * 802.2-encapsulated ETHERTYPE_AARP packets are
1828 * SNAP packets with an organization code of
1829 * 0x000000 (encapsulated Ethernet) and a protocol
1830 * type of ETHERTYPE_AARP (Appletalk ARP).
1832 if (proto
== ETHERTYPE_ATALK
)
1833 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1834 else /* proto == ETHERTYPE_AARP */
1835 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1839 * Check for Ethernet encapsulation (Ethertalk
1840 * phase 1?); we just check for the Ethernet
1843 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1849 if (proto
<= ETHERMTU
) {
1851 * This is an LLC SAP value, so the frames
1852 * that match would be 802.2 frames.
1853 * Check that the frame is an 802.2 frame
1854 * (i.e., that the length/type field is
1855 * a length field, <= ETHERMTU) and
1856 * then check the DSAP.
1858 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1860 b1
= gen_cmp(OR_LINKHDR
, off_linktype
+ 2, BPF_B
,
1866 * This is an Ethernet type, so compare
1867 * the length/type field with it (if
1868 * the frame is an 802.2 frame, the length
1869 * field will be <= ETHERMTU, and, as
1870 * "proto" is > ETHERMTU, this test
1871 * will fail and the frame won't match,
1872 * which is what we want).
1874 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1881 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1882 * or IPv6 then we have an error.
1884 static struct block
*
1885 gen_ipnet_linktype(proto
)
1891 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1892 (bpf_int32
)IPH_AF_INET
);
1895 case ETHERTYPE_IPV6
:
1896 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1897 (bpf_int32
)IPH_AF_INET6
);
1908 * Generate code to match a particular packet type.
1910 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1911 * value, if <= ETHERMTU. We use that to determine whether to
1912 * match the type field or to check the type field for the special
1913 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1915 static struct block
*
1916 gen_linux_sll_linktype(proto
)
1919 struct block
*b0
, *b1
;
1925 case LLCSAP_NETBEUI
:
1927 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1928 * so we check the DSAP and SSAP.
1930 * LLCSAP_IP checks for IP-over-802.2, rather
1931 * than IP-over-Ethernet or IP-over-SNAP.
1933 * XXX - should we check both the DSAP and the
1934 * SSAP, like this, or should we check just the
1935 * DSAP, as we do for other types <= ETHERMTU
1936 * (i.e., other SAP values)?
1938 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1939 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1940 ((proto
<< 8) | proto
));
1946 * Ethernet_II frames, which are Ethernet
1947 * frames with a frame type of ETHERTYPE_IPX;
1949 * Ethernet_802.3 frames, which have a frame
1950 * type of LINUX_SLL_P_802_3;
1952 * Ethernet_802.2 frames, which are 802.3
1953 * frames with an 802.2 LLC header (i.e, have
1954 * a frame type of LINUX_SLL_P_802_2) and
1955 * with the IPX LSAP as the DSAP in the LLC
1958 * Ethernet_SNAP frames, which are 802.3
1959 * frames with an LLC header and a SNAP
1960 * header and with an OUI of 0x000000
1961 * (encapsulated Ethernet) and a protocol
1962 * ID of ETHERTYPE_IPX in the SNAP header.
1964 * First, do the checks on LINUX_SLL_P_802_2
1965 * frames; generate the check for either
1966 * Ethernet_802.2 or Ethernet_SNAP frames, and
1967 * then put a check for LINUX_SLL_P_802_2 frames
1970 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1971 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1973 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1977 * Now check for 802.3 frames and OR that with
1978 * the previous test.
1980 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1984 * Now add the check for Ethernet_II frames, and
1985 * do that before checking for the other frame
1988 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1989 (bpf_int32
)ETHERTYPE_IPX
);
1993 case ETHERTYPE_ATALK
:
1994 case ETHERTYPE_AARP
:
1996 * EtherTalk (AppleTalk protocols on Ethernet link
1997 * layer) may use 802.2 encapsulation.
2001 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2002 * we check for the 802.2 protocol type in the
2003 * "Ethernet type" field.
2005 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2008 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2009 * SNAP packets with an organization code of
2010 * 0x080007 (Apple, for Appletalk) and a protocol
2011 * type of ETHERTYPE_ATALK (Appletalk).
2013 * 802.2-encapsulated ETHERTYPE_AARP packets are
2014 * SNAP packets with an organization code of
2015 * 0x000000 (encapsulated Ethernet) and a protocol
2016 * type of ETHERTYPE_AARP (Appletalk ARP).
2018 if (proto
== ETHERTYPE_ATALK
)
2019 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2020 else /* proto == ETHERTYPE_AARP */
2021 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2025 * Check for Ethernet encapsulation (Ethertalk
2026 * phase 1?); we just check for the Ethernet
2029 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2035 if (proto
<= ETHERMTU
) {
2037 * This is an LLC SAP value, so the frames
2038 * that match would be 802.2 frames.
2039 * Check for the 802.2 protocol type
2040 * in the "Ethernet type" field, and
2041 * then check the DSAP.
2043 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2045 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2051 * This is an Ethernet type, so compare
2052 * the length/type field with it (if
2053 * the frame is an 802.2 frame, the length
2054 * field will be <= ETHERMTU, and, as
2055 * "proto" is > ETHERMTU, this test
2056 * will fail and the frame won't match,
2057 * which is what we want).
2059 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2065 static struct slist
*
2066 gen_load_prism_llprefixlen()
2068 struct slist
*s1
, *s2
;
2069 struct slist
*sjeq_avs_cookie
;
2070 struct slist
*sjcommon
;
2073 * This code is not compatible with the optimizer, as
2074 * we are generating jmp instructions within a normal
2075 * slist of instructions
2080 * Generate code to load the length of the radio header into
2081 * the register assigned to hold that length, if one has been
2082 * assigned. (If one hasn't been assigned, no code we've
2083 * generated uses that prefix, so we don't need to generate any
2086 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2087 * or always use the AVS header rather than the Prism header.
2088 * We load a 4-byte big-endian value at the beginning of the
2089 * raw packet data, and see whether, when masked with 0xFFFFF000,
2090 * it's equal to 0x80211000. If so, that indicates that it's
2091 * an AVS header (the masked-out bits are the version number).
2092 * Otherwise, it's a Prism header.
2094 * XXX - the Prism header is also, in theory, variable-length,
2095 * but no known software generates headers that aren't 144
2098 if (off_linkhdr
.reg
!= -1) {
2102 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2106 * AND it with 0xFFFFF000.
2108 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2109 s2
->s
.k
= 0xFFFFF000;
2113 * Compare with 0x80211000.
2115 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2116 sjeq_avs_cookie
->s
.k
= 0x80211000;
2117 sappend(s1
, sjeq_avs_cookie
);
2122 * The 4 bytes at an offset of 4 from the beginning of
2123 * the AVS header are the length of the AVS header.
2124 * That field is big-endian.
2126 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2129 sjeq_avs_cookie
->s
.jt
= s2
;
2132 * Now jump to the code to allocate a register
2133 * into which to save the header length and
2134 * store the length there. (The "jump always"
2135 * instruction needs to have the k field set;
2136 * it's added to the PC, so, as we're jumping
2137 * over a single instruction, it should be 1.)
2139 sjcommon
= new_stmt(JMP(BPF_JA
));
2141 sappend(s1
, sjcommon
);
2144 * Now for the code that handles the Prism header.
2145 * Just load the length of the Prism header (144)
2146 * into the A register. Have the test for an AVS
2147 * header branch here if we don't have an AVS header.
2149 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2152 sjeq_avs_cookie
->s
.jf
= s2
;
2155 * Now allocate a register to hold that value and store
2156 * it. The code for the AVS header will jump here after
2157 * loading the length of the AVS header.
2159 s2
= new_stmt(BPF_ST
);
2160 s2
->s
.k
= off_linkhdr
.reg
;
2162 sjcommon
->s
.jf
= s2
;
2165 * Now move it into the X register.
2167 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2175 static struct slist
*
2176 gen_load_avs_llprefixlen()
2178 struct slist
*s1
, *s2
;
2181 * Generate code to load the length of the AVS header into
2182 * the register assigned to hold that length, if one has been
2183 * assigned. (If one hasn't been assigned, no code we've
2184 * generated uses that prefix, so we don't need to generate any
2187 if (off_linkhdr
.reg
!= -1) {
2189 * The 4 bytes at an offset of 4 from the beginning of
2190 * the AVS header are the length of the AVS header.
2191 * That field is big-endian.
2193 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2197 * Now allocate a register to hold that value and store
2200 s2
= new_stmt(BPF_ST
);
2201 s2
->s
.k
= off_linkhdr
.reg
;
2205 * Now move it into the X register.
2207 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2215 static struct slist
*
2216 gen_load_radiotap_llprefixlen()
2218 struct slist
*s1
, *s2
;
2221 * Generate code to load the length of the radiotap header into
2222 * the register assigned to hold that length, if one has been
2223 * assigned. (If one hasn't been assigned, no code we've
2224 * generated uses that prefix, so we don't need to generate any
2227 if (off_linkhdr
.reg
!= -1) {
2229 * The 2 bytes at offsets of 2 and 3 from the beginning
2230 * of the radiotap header are the length of the radiotap
2231 * header; unfortunately, it's little-endian, so we have
2232 * to load it a byte at a time and construct the value.
2236 * Load the high-order byte, at an offset of 3, shift it
2237 * left a byte, and put the result in the X register.
2239 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2241 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2244 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2248 * Load the next byte, at an offset of 2, and OR the
2249 * value from the X register into it.
2251 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2254 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2258 * Now allocate a register to hold that value and store
2261 s2
= new_stmt(BPF_ST
);
2262 s2
->s
.k
= off_linkhdr
.reg
;
2266 * Now move it into the X register.
2268 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2277 * At the moment we treat PPI as normal Radiotap encoded
2278 * packets. The difference is in the function that generates
2279 * the code at the beginning to compute the header length.
2280 * Since this code generator of PPI supports bare 802.11
2281 * encapsulation only (i.e. the encapsulated DLT should be
2282 * DLT_IEEE802_11) we generate code to check for this too;
2283 * that's done in finish_parse().
2285 static struct slist
*
2286 gen_load_ppi_llprefixlen()
2288 struct slist
*s1
, *s2
;
2291 * Generate code to load the length of the radiotap header
2292 * into the register assigned to hold that length, if one has
2295 if (off_linkhdr
.reg
!= -1) {
2297 * The 2 bytes at offsets of 2 and 3 from the beginning
2298 * of the radiotap header are the length of the radiotap
2299 * header; unfortunately, it's little-endian, so we have
2300 * to load it a byte at a time and construct the value.
2304 * Load the high-order byte, at an offset of 3, shift it
2305 * left a byte, and put the result in the X register.
2307 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2309 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2312 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2316 * Load the next byte, at an offset of 2, and OR the
2317 * value from the X register into it.
2319 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2322 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2326 * Now allocate a register to hold that value and store
2329 s2
= new_stmt(BPF_ST
);
2330 s2
->s
.k
= off_linkhdr
.reg
;
2334 * Now move it into the X register.
2336 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2345 * Load a value relative to the beginning of the link-layer header after the 802.11
2346 * header, i.e. LLC_SNAP.
2347 * The link-layer header doesn't necessarily begin at the beginning
2348 * of the packet data; there might be a variable-length prefix containing
2349 * radio information.
2351 static struct slist
*
2352 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2355 struct slist
*sjset_data_frame_1
;
2356 struct slist
*sjset_data_frame_2
;
2357 struct slist
*sjset_qos
;
2358 struct slist
*sjset_radiotap_flags
;
2359 struct slist
*sjset_radiotap_tsft
;
2360 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2361 struct slist
*s_roundup
;
2363 if (off_linkpl
.reg
== -1) {
2365 * No register has been assigned to the offset of
2366 * the link-layer payload, which means nobody needs
2367 * it; don't bother computing it - just return
2368 * what we already have.
2374 * This code is not compatible with the optimizer, as
2375 * we are generating jmp instructions within a normal
2376 * slist of instructions
2381 * If "s" is non-null, it has code to arrange that the X register
2382 * contains the length of the prefix preceding the link-layer
2385 * Otherwise, the length of the prefix preceding the link-layer
2386 * header is "off_outermostlinkhdr.constant_part".
2390 * There is no variable-length header preceding the
2391 * link-layer header.
2393 * Load the length of the fixed-length prefix preceding
2394 * the link-layer header (if any) into the X register,
2395 * and store it in the off_linkpl.reg register.
2396 * That length is off_outermostlinkhdr.constant_part.
2398 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2399 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2403 * The X register contains the offset of the beginning of the
2404 * link-layer header; add 24, which is the minimum length
2405 * of the MAC header for a data frame, to that, and store it
2406 * in off_linkpl.reg, and then load the Frame Control field,
2407 * which is at the offset in the X register, with an indexed load.
2409 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2411 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2414 s2
= new_stmt(BPF_ST
);
2415 s2
->s
.k
= off_linkpl
.reg
;
2418 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2423 * Check the Frame Control field to see if this is a data frame;
2424 * a data frame has the 0x08 bit (b3) in that field set and the
2425 * 0x04 bit (b2) clear.
2427 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2428 sjset_data_frame_1
->s
.k
= 0x08;
2429 sappend(s
, sjset_data_frame_1
);
2432 * If b3 is set, test b2, otherwise go to the first statement of
2433 * the rest of the program.
2435 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2436 sjset_data_frame_2
->s
.k
= 0x04;
2437 sappend(s
, sjset_data_frame_2
);
2438 sjset_data_frame_1
->s
.jf
= snext
;
2441 * If b2 is not set, this is a data frame; test the QoS bit.
2442 * Otherwise, go to the first statement of the rest of the
2445 sjset_data_frame_2
->s
.jt
= snext
;
2446 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2447 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2448 sappend(s
, sjset_qos
);
2451 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2453 * Otherwise, go to the first statement of the rest of the
2456 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2457 s2
->s
.k
= off_linkpl
.reg
;
2459 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2462 s2
= new_stmt(BPF_ST
);
2463 s2
->s
.k
= off_linkpl
.reg
;
2467 * If we have a radiotap header, look at it to see whether
2468 * there's Atheros padding between the MAC-layer header
2471 * Note: all of the fields in the radiotap header are
2472 * little-endian, so we byte-swap all of the values
2473 * we test against, as they will be loaded as big-endian
2476 if (linktype
== DLT_IEEE802_11_RADIO
) {
2478 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2479 * in the presence flag?
2481 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2485 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2486 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2487 sappend(s
, sjset_radiotap_flags
);
2490 * If not, skip all of this.
2492 sjset_radiotap_flags
->s
.jf
= snext
;
2495 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2497 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2498 new_stmt(JMP(BPF_JSET
));
2499 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2500 sappend(s
, sjset_radiotap_tsft
);
2503 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2504 * at an offset of 16 from the beginning of the raw packet
2505 * data (8 bytes for the radiotap header and 8 bytes for
2508 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2511 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2515 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2516 sjset_tsft_datapad
->s
.k
= 0x20;
2517 sappend(s
, sjset_tsft_datapad
);
2520 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2521 * at an offset of 8 from the beginning of the raw packet
2522 * data (8 bytes for the radiotap header).
2524 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2527 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2531 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2532 sjset_notsft_datapad
->s
.k
= 0x20;
2533 sappend(s
, sjset_notsft_datapad
);
2536 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2537 * set, round the length of the 802.11 header to
2538 * a multiple of 4. Do that by adding 3 and then
2539 * dividing by and multiplying by 4, which we do by
2542 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2543 s_roundup
->s
.k
= off_linkpl
.reg
;
2544 sappend(s
, s_roundup
);
2545 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2548 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2551 s2
= new_stmt(BPF_ST
);
2552 s2
->s
.k
= off_linkpl
.reg
;
2555 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2556 sjset_tsft_datapad
->s
.jf
= snext
;
2557 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2558 sjset_notsft_datapad
->s
.jf
= snext
;
2560 sjset_qos
->s
.jf
= snext
;
2566 insert_compute_vloffsets(b
)
2572 * For link-layer types that have a variable-length header
2573 * preceding the link-layer header, generate code to load
2574 * the offset of the link-layer header into the register
2575 * assigned to that offset, if any.
2577 * XXX - this, and the next switch statement, won't handle
2578 * encapsulation of 802.11 or 802.11+radio information in
2579 * some other protocol stack. That's significantly more
2582 switch (outermostlinktype
) {
2584 case DLT_PRISM_HEADER
:
2585 s
= gen_load_prism_llprefixlen();
2588 case DLT_IEEE802_11_RADIO_AVS
:
2589 s
= gen_load_avs_llprefixlen();
2592 case DLT_IEEE802_11_RADIO
:
2593 s
= gen_load_radiotap_llprefixlen();
2597 s
= gen_load_ppi_llprefixlen();
2606 * For link-layer types that have a variable-length link-layer
2607 * header, generate code to load the offset of the link-layer
2608 * payload into the register assigned to that offset, if any.
2610 switch (outermostlinktype
) {
2612 case DLT_IEEE802_11
:
2613 case DLT_PRISM_HEADER
:
2614 case DLT_IEEE802_11_RADIO_AVS
:
2615 case DLT_IEEE802_11_RADIO
:
2617 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2622 * If we have any offset-loading code, append all the
2623 * existing statements in the block to those statements,
2624 * and make the resulting list the list of statements
2628 sappend(s
, b
->stmts
);
2633 static struct block
*
2634 gen_ppi_dlt_check(void)
2636 struct slist
*s_load_dlt
;
2639 if (linktype
== DLT_PPI
)
2641 /* Create the statements that check for the DLT
2643 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2644 s_load_dlt
->s
.k
= 4;
2646 b
= new_block(JMP(BPF_JEQ
));
2648 b
->stmts
= s_load_dlt
;
2649 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2660 * Take an absolute offset, and:
2662 * if it has no variable part, return NULL;
2664 * if it has a variable part, generate code to load the register
2665 * containing that variable part into the X register, returning
2666 * a pointer to that code - if no register for that offset has
2667 * been allocated, allocate it first.
2669 * (The code to set that register will be generated later, but will
2670 * be placed earlier in the code sequence.)
2672 static struct slist
*
2673 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2677 if (off
->is_variable
) {
2678 if (off
->reg
== -1) {
2680 * We haven't yet assigned a register for the
2681 * variable part of the offset of the link-layer
2682 * header; allocate one.
2684 off
->reg
= alloc_reg();
2688 * Load the register containing the variable part of the
2689 * offset of the link-layer header into the X register.
2691 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2696 * That offset isn't variable, there's no variable part,
2697 * so we don't need to generate any code.
2704 * Map an Ethernet type to the equivalent PPP type.
2707 ethertype_to_ppptype(proto
)
2716 case ETHERTYPE_IPV6
:
2724 case ETHERTYPE_ATALK
:
2738 * I'm assuming the "Bridging PDU"s that go
2739 * over PPP are Spanning Tree Protocol
2753 * Generate any tests that, for encapsulation of a link-layer packet
2754 * inside another protocol stack, need to be done to check for those
2755 * link-layer packets (and that haven't already been done by a check
2756 * for that encapsulation).
2758 static struct block
*
2759 gen_prevlinkhdr_check(void)
2763 switch (prevlinktype
) {
2767 * This is LANE-encapsulated Ethernet; check that the LANE
2768 * packet doesn't begin with an LE Control marker, i.e.
2769 * that it's data, not a control message.
2771 * (We've already generated a test for LANE.)
2773 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2779 * No such tests are necessary.
2787 * Generate code to match a particular packet type by matching the
2788 * link-layer type field or fields in the 802.2 LLC header.
2790 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2791 * value, if <= ETHERMTU.
2793 static struct block
*
2797 struct block
*b0
, *b1
, *b2
;
2798 const char *description
;
2800 /* are we checking MPLS-encapsulated packets? */
2801 if (label_stack_depth
> 0) {
2805 /* FIXME add other L3 proto IDs */
2806 return gen_mpls_linktype(Q_IP
);
2808 case ETHERTYPE_IPV6
:
2810 /* FIXME add other L3 proto IDs */
2811 return gen_mpls_linktype(Q_IPV6
);
2814 bpf_error("unsupported protocol over mpls");
2822 case DLT_NETANALYZER
:
2823 case DLT_NETANALYZER_TRANSPARENT
:
2824 b0
= gen_prevlinkhdr_check();
2825 b1
= gen_ether_linktype(proto
);
2836 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2840 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2847 case DLT_IEEE802_11
:
2848 case DLT_PRISM_HEADER
:
2849 case DLT_IEEE802_11_RADIO_AVS
:
2850 case DLT_IEEE802_11_RADIO
:
2853 * Check that we have a data frame.
2855 b0
= gen_check_802_11_data_frame();
2858 * Now check for the specified link-layer type.
2860 b1
= gen_llc_linktype(proto
);
2868 * XXX - check for LLC frames.
2870 return gen_llc_linktype(proto
);
2876 * XXX - check for LLC PDUs, as per IEEE 802.5.
2878 return gen_llc_linktype(proto
);
2882 case DLT_ATM_RFC1483
:
2884 case DLT_IP_OVER_FC
:
2885 return gen_llc_linktype(proto
);
2891 * Check for an LLC-encapsulated version of this protocol;
2892 * if we were checking for LANE, linktype would no longer
2895 * Check for LLC encapsulation and then check the protocol.
2897 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2898 b1
= gen_llc_linktype(proto
);
2905 return gen_linux_sll_linktype(proto
);
2910 case DLT_SLIP_BSDOS
:
2913 * These types don't provide any type field; packets
2914 * are always IPv4 or IPv6.
2916 * XXX - for IPv4, check for a version number of 4, and,
2917 * for IPv6, check for a version number of 6?
2922 /* Check for a version number of 4. */
2923 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
2925 case ETHERTYPE_IPV6
:
2926 /* Check for a version number of 6. */
2927 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
2930 return gen_false(); /* always false */
2937 * Raw IPv4, so no type field.
2939 if (proto
== ETHERTYPE_IP
)
2940 return gen_true(); /* always true */
2942 /* Checking for something other than IPv4; always false */
2949 * Raw IPv6, so no type field.
2951 if (proto
== ETHERTYPE_IPV6
)
2952 return gen_true(); /* always true */
2954 /* Checking for something other than IPv6; always false */
2961 case DLT_PPP_SERIAL
:
2964 * We use Ethernet protocol types inside libpcap;
2965 * map them to the corresponding PPP protocol types.
2967 proto
= ethertype_to_ppptype(proto
);
2968 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2974 * We use Ethernet protocol types inside libpcap;
2975 * map them to the corresponding PPP protocol types.
2981 * Also check for Van Jacobson-compressed IP.
2982 * XXX - do this for other forms of PPP?
2984 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_IP
);
2985 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJC
);
2987 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJNC
);
2992 proto
= ethertype_to_ppptype(proto
);
2993 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
3003 * For DLT_NULL, the link-layer header is a 32-bit
3004 * word containing an AF_ value in *host* byte order,
3005 * and for DLT_ENC, the link-layer header begins
3006 * with a 32-bit work containing an AF_ value in
3009 * In addition, if we're reading a saved capture file,
3010 * the host byte order in the capture may not be the
3011 * same as the host byte order on this machine.
3013 * For DLT_LOOP, the link-layer header is a 32-bit
3014 * word containing an AF_ value in *network* byte order.
3016 * XXX - AF_ values may, unfortunately, be platform-
3017 * dependent; for example, FreeBSD's AF_INET6 is 24
3018 * whilst NetBSD's and OpenBSD's is 26.
3020 * This means that, when reading a capture file, just
3021 * checking for our AF_INET6 value won't work if the
3022 * capture file came from another OS.
3031 case ETHERTYPE_IPV6
:
3038 * Not a type on which we support filtering.
3039 * XXX - support those that have AF_ values
3040 * #defined on this platform, at least?
3045 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3047 * The AF_ value is in host byte order, but
3048 * the BPF interpreter will convert it to
3049 * network byte order.
3051 * If this is a save file, and it's from a
3052 * machine with the opposite byte order to
3053 * ours, we byte-swap the AF_ value.
3055 * Then we run it through "htonl()", and
3056 * generate code to compare against the result.
3058 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3059 proto
= SWAPLONG(proto
);
3060 proto
= htonl(proto
);
3062 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3064 #ifdef HAVE_NET_PFVAR_H
3067 * af field is host byte order in contrast to the rest of
3070 if (proto
== ETHERTYPE_IP
)
3071 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3072 BPF_B
, (bpf_int32
)AF_INET
));
3073 else if (proto
== ETHERTYPE_IPV6
)
3074 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3075 BPF_B
, (bpf_int32
)AF_INET6
));
3080 #endif /* HAVE_NET_PFVAR_H */
3083 case DLT_ARCNET_LINUX
:
3085 * XXX should we check for first fragment if the protocol
3093 case ETHERTYPE_IPV6
:
3094 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3095 (bpf_int32
)ARCTYPE_INET6
));
3098 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3099 (bpf_int32
)ARCTYPE_IP
);
3100 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3101 (bpf_int32
)ARCTYPE_IP_OLD
);
3106 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3107 (bpf_int32
)ARCTYPE_ARP
);
3108 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3109 (bpf_int32
)ARCTYPE_ARP_OLD
);
3113 case ETHERTYPE_REVARP
:
3114 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3115 (bpf_int32
)ARCTYPE_REVARP
));
3117 case ETHERTYPE_ATALK
:
3118 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3119 (bpf_int32
)ARCTYPE_ATALK
));
3126 case ETHERTYPE_ATALK
:
3136 * XXX - assumes a 2-byte Frame Relay header with
3137 * DLCI and flags. What if the address is longer?
3143 * Check for the special NLPID for IP.
3145 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3147 case ETHERTYPE_IPV6
:
3149 * Check for the special NLPID for IPv6.
3151 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3155 * Check for several OSI protocols.
3157 * Frame Relay packets typically have an OSI
3158 * NLPID at the beginning; we check for each
3161 * What we check for is the NLPID and a frame
3162 * control field of UI, i.e. 0x03 followed
3165 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3166 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3167 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3179 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3181 case DLT_JUNIPER_MFR
:
3182 case DLT_JUNIPER_MLFR
:
3183 case DLT_JUNIPER_MLPPP
:
3184 case DLT_JUNIPER_ATM1
:
3185 case DLT_JUNIPER_ATM2
:
3186 case DLT_JUNIPER_PPPOE
:
3187 case DLT_JUNIPER_PPPOE_ATM
:
3188 case DLT_JUNIPER_GGSN
:
3189 case DLT_JUNIPER_ES
:
3190 case DLT_JUNIPER_MONITOR
:
3191 case DLT_JUNIPER_SERVICES
:
3192 case DLT_JUNIPER_ETHER
:
3193 case DLT_JUNIPER_PPP
:
3194 case DLT_JUNIPER_FRELAY
:
3195 case DLT_JUNIPER_CHDLC
:
3196 case DLT_JUNIPER_VP
:
3197 case DLT_JUNIPER_ST
:
3198 case DLT_JUNIPER_ISM
:
3199 case DLT_JUNIPER_VS
:
3200 case DLT_JUNIPER_SRX_E2E
:
3201 case DLT_JUNIPER_FIBRECHANNEL
:
3202 case DLT_JUNIPER_ATM_CEMIC
:
3204 /* just lets verify the magic number for now -
3205 * on ATM we may have up to 6 different encapsulations on the wire
3206 * and need a lot of heuristics to figure out that the payload
3209 * FIXME encapsulation specific BPF_ filters
3211 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3213 case DLT_BACNET_MS_TP
:
3214 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3217 return gen_ipnet_linktype(proto
);
3219 case DLT_LINUX_IRDA
:
3220 bpf_error("IrDA link-layer type filtering not implemented");
3223 bpf_error("DOCSIS link-layer type filtering not implemented");
3226 case DLT_MTP2_WITH_PHDR
:
3227 bpf_error("MTP2 link-layer type filtering not implemented");
3230 bpf_error("ERF link-layer type filtering not implemented");
3233 bpf_error("PFSYNC link-layer type filtering not implemented");
3235 case DLT_LINUX_LAPD
:
3236 bpf_error("LAPD link-layer type filtering not implemented");
3240 case DLT_USB_LINUX_MMAPPED
:
3241 bpf_error("USB link-layer type filtering not implemented");
3243 case DLT_BLUETOOTH_HCI_H4
:
3244 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3245 bpf_error("Bluetooth link-layer type filtering not implemented");
3248 case DLT_CAN_SOCKETCAN
:
3249 bpf_error("CAN link-layer type filtering not implemented");
3251 case DLT_IEEE802_15_4
:
3252 case DLT_IEEE802_15_4_LINUX
:
3253 case DLT_IEEE802_15_4_NONASK_PHY
:
3254 case DLT_IEEE802_15_4_NOFCS
:
3255 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3257 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3258 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3261 bpf_error("SITA link-layer type filtering not implemented");
3264 bpf_error("RAIF1 link-layer type filtering not implemented");
3267 bpf_error("IPMB link-layer type filtering not implemented");
3270 bpf_error("AX.25 link-layer type filtering not implemented");
3273 /* Using the fixed-size NFLOG header it is possible to tell only
3274 * the address family of the packet, other meaningful data is
3275 * either missing or behind TLVs.
3277 bpf_error("NFLOG link-layer type filtering not implemented");
3281 * Does this link-layer header type have a field
3282 * indicating the type of the next protocol? If
3283 * so, off_linktype will be the offset of that
3284 * field in the packet; if not, it will be -1.
3286 if (off_linktype
!= (u_int
)-1) {
3288 * Yes; assume it's an Ethernet type. (If
3289 * it's not, it needs to be handled specially
3292 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3295 * No; report an error.
3297 description
= pcap_datalink_val_to_description(linktype
);
3298 if (description
!= NULL
) {
3299 bpf_error("%s link-layer type filtering not implemented",
3302 bpf_error("DLT %u link-layer type filtering not implemented",
3311 * Check for an LLC SNAP packet with a given organization code and
3312 * protocol type; we check the entire contents of the 802.2 LLC and
3313 * snap headers, checking for DSAP and SSAP of SNAP and a control
3314 * field of 0x03 in the LLC header, and for the specified organization
3315 * code and protocol type in the SNAP header.
3317 static struct block
*
3318 gen_snap(orgcode
, ptype
)
3319 bpf_u_int32 orgcode
;
3322 u_char snapblock
[8];
3324 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3325 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3326 snapblock
[2] = 0x03; /* control = UI */
3327 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3328 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3329 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3330 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3331 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3332 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3336 * Generate code to match frames with an LLC header.
3341 struct block
*b0
, *b1
;
3347 * We check for an Ethernet type field less than
3348 * 1500, which means it's an 802.3 length field.
3350 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
3354 * Now check for the purported DSAP and SSAP not being
3355 * 0xFF, to rule out NetWare-over-802.3.
3357 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3364 * We check for LLC traffic.
3366 b0
= gen_atmtype_abbrev(A_LLC
);
3369 case DLT_IEEE802
: /* Token Ring */
3371 * XXX - check for LLC frames.
3377 * XXX - check for LLC frames.
3381 case DLT_ATM_RFC1483
:
3383 * For LLC encapsulation, these are defined to have an
3386 * For VC encapsulation, they don't, but there's no
3387 * way to check for that; the protocol used on the VC
3388 * is negotiated out of band.
3392 case DLT_IEEE802_11
:
3393 case DLT_PRISM_HEADER
:
3394 case DLT_IEEE802_11_RADIO
:
3395 case DLT_IEEE802_11_RADIO_AVS
:
3398 * Check that we have a data frame.
3400 b0
= gen_check_802_11_data_frame();
3404 bpf_error("'llc' not supported for linktype %d", linktype
);
3412 struct block
*b0
, *b1
;
3416 * Check whether this is an LLC frame.
3421 * Load the control byte and test the low-order bit; it must
3422 * be clear for I frames.
3424 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3425 b1
= new_block(JMP(BPF_JSET
));
3436 struct block
*b0
, *b1
;
3439 * Check whether this is an LLC frame.
3444 * Now compare the low-order 2 bit of the control byte against
3445 * the appropriate value for S frames.
3447 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3455 struct block
*b0
, *b1
;
3458 * Check whether this is an LLC frame.
3463 * Now compare the low-order 2 bit of the control byte against
3464 * the appropriate value for U frames.
3466 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3472 gen_llc_s_subtype(bpf_u_int32 subtype
)
3474 struct block
*b0
, *b1
;
3477 * Check whether this is an LLC frame.
3482 * Now check for an S frame with the appropriate type.
3484 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3490 gen_llc_u_subtype(bpf_u_int32 subtype
)
3492 struct block
*b0
, *b1
;
3495 * Check whether this is an LLC frame.
3500 * Now check for a U frame with the appropriate type.
3502 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3508 * Generate code to match a particular packet type, for link-layer types
3509 * using 802.2 LLC headers.
3511 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3512 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3514 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3515 * value, if <= ETHERMTU. We use that to determine whether to
3516 * match the DSAP or both DSAP and LSAP or to check the OUI and
3517 * protocol ID in a SNAP header.
3519 static struct block
*
3520 gen_llc_linktype(proto
)
3524 * XXX - handle token-ring variable-length header.
3530 case LLCSAP_NETBEUI
:
3532 * XXX - should we check both the DSAP and the
3533 * SSAP, like this, or should we check just the
3534 * DSAP, as we do for other SAP values?
3536 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3537 ((proto
<< 8) | proto
));
3541 * XXX - are there ever SNAP frames for IPX on
3542 * non-Ethernet 802.x networks?
3544 return gen_cmp(OR_LLC
, 0, BPF_B
,
3545 (bpf_int32
)LLCSAP_IPX
);
3547 case ETHERTYPE_ATALK
:
3549 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3550 * SNAP packets with an organization code of
3551 * 0x080007 (Apple, for Appletalk) and a protocol
3552 * type of ETHERTYPE_ATALK (Appletalk).
3554 * XXX - check for an organization code of
3555 * encapsulated Ethernet as well?
3557 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3561 * XXX - we don't have to check for IPX 802.3
3562 * here, but should we check for the IPX Ethertype?
3564 if (proto
<= ETHERMTU
) {
3566 * This is an LLC SAP value, so check
3569 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3572 * This is an Ethernet type; we assume that it's
3573 * unlikely that it'll appear in the right place
3574 * at random, and therefore check only the
3575 * location that would hold the Ethernet type
3576 * in a SNAP frame with an organization code of
3577 * 0x000000 (encapsulated Ethernet).
3579 * XXX - if we were to check for the SNAP DSAP and
3580 * LSAP, as per XXX, and were also to check for an
3581 * organization code of 0x000000 (encapsulated
3582 * Ethernet), we'd do
3584 * return gen_snap(0x000000, proto);
3586 * here; for now, we don't, as per the above.
3587 * I don't know whether it's worth the extra CPU
3588 * time to do the right check or not.
3590 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3595 static struct block
*
3596 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3600 u_int src_off
, dst_off
;
3602 struct block
*b0
, *b1
;
3616 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3617 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3623 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3624 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3631 b0
= gen_linktype(proto
);
3632 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3638 static struct block
*
3639 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3640 struct in6_addr
*addr
;
3641 struct in6_addr
*mask
;
3643 u_int src_off
, dst_off
;
3645 struct block
*b0
, *b1
;
3660 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3661 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3667 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3668 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3675 /* this order is important */
3676 a
= (u_int32_t
*)addr
;
3677 m
= (u_int32_t
*)mask
;
3678 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3679 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3681 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3683 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3685 b0
= gen_linktype(proto
);
3691 static struct block
*
3692 gen_ehostop(eaddr
, dir
)
3693 register const u_char
*eaddr
;
3696 register struct block
*b0
, *b1
;
3700 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3703 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3706 b0
= gen_ehostop(eaddr
, Q_SRC
);
3707 b1
= gen_ehostop(eaddr
, Q_DST
);
3713 b0
= gen_ehostop(eaddr
, Q_SRC
);
3714 b1
= gen_ehostop(eaddr
, Q_DST
);
3719 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3723 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3727 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3731 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3735 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3739 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3747 * Like gen_ehostop, but for DLT_FDDI
3749 static struct block
*
3750 gen_fhostop(eaddr
, dir
)
3751 register const u_char
*eaddr
;
3754 struct block
*b0
, *b1
;
3758 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3761 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3764 b0
= gen_fhostop(eaddr
, Q_SRC
);
3765 b1
= gen_fhostop(eaddr
, Q_DST
);
3771 b0
= gen_fhostop(eaddr
, Q_SRC
);
3772 b1
= gen_fhostop(eaddr
, Q_DST
);
3777 bpf_error("'addr1' is only supported on 802.11");
3781 bpf_error("'addr2' is only supported on 802.11");
3785 bpf_error("'addr3' is only supported on 802.11");
3789 bpf_error("'addr4' is only supported on 802.11");
3793 bpf_error("'ra' is only supported on 802.11");
3797 bpf_error("'ta' is only supported on 802.11");
3805 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3807 static struct block
*
3808 gen_thostop(eaddr
, dir
)
3809 register const u_char
*eaddr
;
3812 register struct block
*b0
, *b1
;
3816 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3819 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3822 b0
= gen_thostop(eaddr
, Q_SRC
);
3823 b1
= gen_thostop(eaddr
, Q_DST
);
3829 b0
= gen_thostop(eaddr
, Q_SRC
);
3830 b1
= gen_thostop(eaddr
, Q_DST
);
3835 bpf_error("'addr1' is only supported on 802.11");
3839 bpf_error("'addr2' is only supported on 802.11");
3843 bpf_error("'addr3' is only supported on 802.11");
3847 bpf_error("'addr4' is only supported on 802.11");
3851 bpf_error("'ra' is only supported on 802.11");
3855 bpf_error("'ta' is only supported on 802.11");
3863 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3864 * various 802.11 + radio headers.
3866 static struct block
*
3867 gen_wlanhostop(eaddr
, dir
)
3868 register const u_char
*eaddr
;
3871 register struct block
*b0
, *b1
, *b2
;
3872 register struct slist
*s
;
3874 #ifdef ENABLE_WLAN_FILTERING_PATCH
3877 * We need to disable the optimizer because the optimizer is buggy
3878 * and wipes out some LD instructions generated by the below
3879 * code to validate the Frame Control bits
3882 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3889 * For control frames, there is no SA.
3891 * For management frames, SA is at an
3892 * offset of 10 from the beginning of
3895 * For data frames, SA is at an offset
3896 * of 10 from the beginning of the packet
3897 * if From DS is clear, at an offset of
3898 * 16 from the beginning of the packet
3899 * if From DS is set and To DS is clear,
3900 * and an offset of 24 from the beginning
3901 * of the packet if From DS is set and To DS
3906 * Generate the tests to be done for data frames
3909 * First, check for To DS set, i.e. check "link[1] & 0x01".
3911 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3912 b1
= new_block(JMP(BPF_JSET
));
3913 b1
->s
.k
= 0x01; /* To DS */
3917 * If To DS is set, the SA is at 24.
3919 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
3923 * Now, check for To DS not set, i.e. check
3924 * "!(link[1] & 0x01)".
3926 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3927 b2
= new_block(JMP(BPF_JSET
));
3928 b2
->s
.k
= 0x01; /* To DS */
3933 * If To DS is not set, the SA is at 16.
3935 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
3939 * Now OR together the last two checks. That gives
3940 * the complete set of checks for data frames with
3946 * Now check for From DS being set, and AND that with
3947 * the ORed-together checks.
3949 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3950 b1
= new_block(JMP(BPF_JSET
));
3951 b1
->s
.k
= 0x02; /* From DS */
3956 * Now check for data frames with From DS not set.
3958 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3959 b2
= new_block(JMP(BPF_JSET
));
3960 b2
->s
.k
= 0x02; /* From DS */
3965 * If From DS isn't set, the SA is at 10.
3967 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
3971 * Now OR together the checks for data frames with
3972 * From DS not set and for data frames with From DS
3973 * set; that gives the checks done for data frames.
3978 * Now check for a data frame.
3979 * I.e, check "link[0] & 0x08".
3981 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3982 b1
= new_block(JMP(BPF_JSET
));
3987 * AND that with the checks done for data frames.
3992 * If the high-order bit of the type value is 0, this
3993 * is a management frame.
3994 * I.e, check "!(link[0] & 0x08)".
3996 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3997 b2
= new_block(JMP(BPF_JSET
));
4003 * For management frames, the SA is at 10.
4005 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4009 * OR that with the checks done for data frames.
4010 * That gives the checks done for management and
4016 * If the low-order bit of the type value is 1,
4017 * this is either a control frame or a frame
4018 * with a reserved type, and thus not a
4021 * I.e., check "!(link[0] & 0x04)".
4023 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4024 b1
= new_block(JMP(BPF_JSET
));
4030 * AND that with the checks for data and management
4040 * For control frames, there is no DA.
4042 * For management frames, DA is at an
4043 * offset of 4 from the beginning of
4046 * For data frames, DA is at an offset
4047 * of 4 from the beginning of the packet
4048 * if To DS is clear and at an offset of
4049 * 16 from the beginning of the packet
4054 * Generate the tests to be done for data frames.
4056 * First, check for To DS set, i.e. "link[1] & 0x01".
4058 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4059 b1
= new_block(JMP(BPF_JSET
));
4060 b1
->s
.k
= 0x01; /* To DS */
4064 * If To DS is set, the DA is at 16.
4066 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4070 * Now, check for To DS not set, i.e. check
4071 * "!(link[1] & 0x01)".
4073 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4074 b2
= new_block(JMP(BPF_JSET
));
4075 b2
->s
.k
= 0x01; /* To DS */
4080 * If To DS is not set, the DA is at 4.
4082 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4086 * Now OR together the last two checks. That gives
4087 * the complete set of checks for data frames.
4092 * Now check for a data frame.
4093 * I.e, check "link[0] & 0x08".
4095 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4096 b1
= new_block(JMP(BPF_JSET
));
4101 * AND that with the checks done for data frames.
4106 * If the high-order bit of the type value is 0, this
4107 * is a management frame.
4108 * I.e, check "!(link[0] & 0x08)".
4110 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4111 b2
= new_block(JMP(BPF_JSET
));
4117 * For management frames, the DA is at 4.
4119 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4123 * OR that with the checks done for data frames.
4124 * That gives the checks done for management and
4130 * If the low-order bit of the type value is 1,
4131 * this is either a control frame or a frame
4132 * with a reserved type, and thus not a
4135 * I.e., check "!(link[0] & 0x04)".
4137 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4138 b1
= new_block(JMP(BPF_JSET
));
4144 * AND that with the checks for data and management
4152 * Not present in management frames; addr1 in other
4157 * If the high-order bit of the type value is 0, this
4158 * is a management frame.
4159 * I.e, check "(link[0] & 0x08)".
4161 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4162 b1
= new_block(JMP(BPF_JSET
));
4169 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4172 * AND that with the check of addr1.
4179 * Not present in management frames; addr2, if present,
4184 * Not present in CTS or ACK control frames.
4186 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4187 IEEE80211_FC0_TYPE_MASK
);
4189 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4190 IEEE80211_FC0_SUBTYPE_MASK
);
4192 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4193 IEEE80211_FC0_SUBTYPE_MASK
);
4199 * If the high-order bit of the type value is 0, this
4200 * is a management frame.
4201 * I.e, check "(link[0] & 0x08)".
4203 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4204 b1
= new_block(JMP(BPF_JSET
));
4209 * AND that with the check for frames other than
4210 * CTS and ACK frames.
4217 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4222 * XXX - add BSSID keyword?
4225 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4229 * Not present in CTS or ACK control frames.
4231 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4232 IEEE80211_FC0_TYPE_MASK
);
4234 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4235 IEEE80211_FC0_SUBTYPE_MASK
);
4237 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4238 IEEE80211_FC0_SUBTYPE_MASK
);
4242 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4248 * Not present in control frames.
4250 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4251 IEEE80211_FC0_TYPE_MASK
);
4253 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4259 * Present only if the direction mask has both "From DS"
4260 * and "To DS" set. Neither control frames nor management
4261 * frames should have both of those set, so we don't
4262 * check the frame type.
4264 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4265 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4266 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4271 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4272 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4278 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4279 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4288 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4289 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4290 * as the RFC states.)
4292 static struct block
*
4293 gen_ipfchostop(eaddr
, dir
)
4294 register const u_char
*eaddr
;
4297 register struct block
*b0
, *b1
;
4301 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4304 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4307 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4308 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4314 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4315 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4320 bpf_error("'addr1' is only supported on 802.11");
4324 bpf_error("'addr2' is only supported on 802.11");
4328 bpf_error("'addr3' is only supported on 802.11");
4332 bpf_error("'addr4' is only supported on 802.11");
4336 bpf_error("'ra' is only supported on 802.11");
4340 bpf_error("'ta' is only supported on 802.11");
4348 * This is quite tricky because there may be pad bytes in front of the
4349 * DECNET header, and then there are two possible data packet formats that
4350 * carry both src and dst addresses, plus 5 packet types in a format that
4351 * carries only the src node, plus 2 types that use a different format and
4352 * also carry just the src node.
4356 * Instead of doing those all right, we just look for data packets with
4357 * 0 or 1 bytes of padding. If you want to look at other packets, that
4358 * will require a lot more hacking.
4360 * To add support for filtering on DECNET "areas" (network numbers)
4361 * one would want to add a "mask" argument to this routine. That would
4362 * make the filter even more inefficient, although one could be clever
4363 * and not generate masking instructions if the mask is 0xFFFF.
4365 static struct block
*
4366 gen_dnhostop(addr
, dir
)
4370 struct block
*b0
, *b1
, *b2
, *tmp
;
4371 u_int offset_lh
; /* offset if long header is received */
4372 u_int offset_sh
; /* offset if short header is received */
4377 offset_sh
= 1; /* follows flags */
4378 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4382 offset_sh
= 3; /* follows flags, dstnode */
4383 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4387 /* Inefficient because we do our Calvinball dance twice */
4388 b0
= gen_dnhostop(addr
, Q_SRC
);
4389 b1
= gen_dnhostop(addr
, Q_DST
);
4395 /* Inefficient because we do our Calvinball dance twice */
4396 b0
= gen_dnhostop(addr
, Q_SRC
);
4397 b1
= gen_dnhostop(addr
, Q_DST
);
4402 bpf_error("ISO host filtering not implemented");
4407 b0
= gen_linktype(ETHERTYPE_DN
);
4408 /* Check for pad = 1, long header case */
4409 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4410 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4411 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4412 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4414 /* Check for pad = 0, long header case */
4415 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4416 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4419 /* Check for pad = 1, short header case */
4420 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4421 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4422 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4425 /* Check for pad = 0, short header case */
4426 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4427 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4431 /* Combine with test for linktype */
4437 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4438 * test the bottom-of-stack bit, and then check the version number
4439 * field in the IP header.
4441 static struct block
*
4442 gen_mpls_linktype(proto
)
4445 struct block
*b0
, *b1
;
4450 /* match the bottom-of-stack bit */
4451 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4452 /* match the IPv4 version number */
4453 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4458 /* match the bottom-of-stack bit */
4459 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4460 /* match the IPv4 version number */
4461 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4470 static struct block
*
4471 gen_host(addr
, mask
, proto
, dir
, type
)
4478 struct block
*b0
, *b1
;
4479 const char *typestr
;
4489 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4491 * Only check for non-IPv4 addresses if we're not
4492 * checking MPLS-encapsulated packets.
4494 if (label_stack_depth
== 0) {
4495 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4497 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4503 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4506 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4509 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4512 bpf_error("'tcp' modifier applied to %s", typestr
);
4515 bpf_error("'sctp' modifier applied to %s", typestr
);
4518 bpf_error("'udp' modifier applied to %s", typestr
);
4521 bpf_error("'icmp' modifier applied to %s", typestr
);
4524 bpf_error("'igmp' modifier applied to %s", typestr
);
4527 bpf_error("'igrp' modifier applied to %s", typestr
);
4530 bpf_error("'pim' modifier applied to %s", typestr
);
4533 bpf_error("'vrrp' modifier applied to %s", typestr
);
4536 bpf_error("'carp' modifier applied to %s", typestr
);
4539 bpf_error("ATALK host filtering not implemented");
4542 bpf_error("AARP host filtering not implemented");
4545 return gen_dnhostop(addr
, dir
);
4548 bpf_error("SCA host filtering not implemented");
4551 bpf_error("LAT host filtering not implemented");
4554 bpf_error("MOPDL host filtering not implemented");
4557 bpf_error("MOPRC host filtering not implemented");
4560 bpf_error("'ip6' modifier applied to ip host");
4563 bpf_error("'icmp6' modifier applied to %s", typestr
);
4566 bpf_error("'ah' modifier applied to %s", typestr
);
4569 bpf_error("'esp' modifier applied to %s", typestr
);
4572 bpf_error("ISO host filtering not implemented");
4575 bpf_error("'esis' modifier applied to %s", typestr
);
4578 bpf_error("'isis' modifier applied to %s", typestr
);
4581 bpf_error("'clnp' modifier applied to %s", typestr
);
4584 bpf_error("'stp' modifier applied to %s", typestr
);
4587 bpf_error("IPX host filtering not implemented");
4590 bpf_error("'netbeui' modifier applied to %s", typestr
);
4593 bpf_error("'radio' modifier applied to %s", typestr
);
4602 static struct block
*
4603 gen_host6(addr
, mask
, proto
, dir
, type
)
4604 struct in6_addr
*addr
;
4605 struct in6_addr
*mask
;
4610 const char *typestr
;
4620 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4623 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4626 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4629 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4632 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4635 bpf_error("'sctp' modifier applied to %s", typestr
);
4638 bpf_error("'tcp' modifier applied to %s", typestr
);
4641 bpf_error("'udp' modifier applied to %s", typestr
);
4644 bpf_error("'icmp' modifier applied to %s", typestr
);
4647 bpf_error("'igmp' modifier applied to %s", typestr
);
4650 bpf_error("'igrp' modifier applied to %s", typestr
);
4653 bpf_error("'pim' modifier applied to %s", typestr
);
4656 bpf_error("'vrrp' modifier applied to %s", typestr
);
4659 bpf_error("'carp' modifier applied to %s", typestr
);
4662 bpf_error("ATALK host filtering not implemented");
4665 bpf_error("AARP host filtering not implemented");
4668 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4671 bpf_error("SCA host filtering not implemented");
4674 bpf_error("LAT host filtering not implemented");
4677 bpf_error("MOPDL host filtering not implemented");
4680 bpf_error("MOPRC host filtering not implemented");
4683 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4686 bpf_error("'icmp6' modifier applied to %s", typestr
);
4689 bpf_error("'ah' modifier applied to %s", typestr
);
4692 bpf_error("'esp' modifier applied to %s", typestr
);
4695 bpf_error("ISO host filtering not implemented");
4698 bpf_error("'esis' modifier applied to %s", typestr
);
4701 bpf_error("'isis' modifier applied to %s", typestr
);
4704 bpf_error("'clnp' modifier applied to %s", typestr
);
4707 bpf_error("'stp' modifier applied to %s", typestr
);
4710 bpf_error("IPX host filtering not implemented");
4713 bpf_error("'netbeui' modifier applied to %s", typestr
);
4716 bpf_error("'radio' modifier applied to %s", typestr
);
4726 static struct block
*
4727 gen_gateway(eaddr
, alist
, proto
, dir
)
4728 const u_char
*eaddr
;
4729 bpf_u_int32
**alist
;
4733 struct block
*b0
, *b1
, *tmp
;
4736 bpf_error("direction applied to 'gateway'");
4745 case DLT_NETANALYZER
:
4746 case DLT_NETANALYZER_TRANSPARENT
:
4747 b1
= gen_prevlinkhdr_check();
4748 b0
= gen_ehostop(eaddr
, Q_OR
);
4753 b0
= gen_fhostop(eaddr
, Q_OR
);
4756 b0
= gen_thostop(eaddr
, Q_OR
);
4758 case DLT_IEEE802_11
:
4759 case DLT_PRISM_HEADER
:
4760 case DLT_IEEE802_11_RADIO_AVS
:
4761 case DLT_IEEE802_11_RADIO
:
4763 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4767 * This is LLC-multiplexed traffic; if it were
4768 * LANE, linktype would have been set to
4772 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4774 case DLT_IP_OVER_FC
:
4775 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4779 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4781 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4783 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4792 bpf_error("illegal modifier of 'gateway'");
4798 gen_proto_abbrev(proto
)
4807 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4808 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4813 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4814 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4819 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4820 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4825 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4828 #ifndef IPPROTO_IGMP
4829 #define IPPROTO_IGMP 2
4833 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4836 #ifndef IPPROTO_IGRP
4837 #define IPPROTO_IGRP 9
4840 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4844 #define IPPROTO_PIM 103
4848 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4849 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4853 #ifndef IPPROTO_VRRP
4854 #define IPPROTO_VRRP 112
4858 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4861 #ifndef IPPROTO_CARP
4862 #define IPPROTO_CARP 112
4866 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4870 b1
= gen_linktype(ETHERTYPE_IP
);
4874 b1
= gen_linktype(ETHERTYPE_ARP
);
4878 b1
= gen_linktype(ETHERTYPE_REVARP
);
4882 bpf_error("link layer applied in wrong context");
4885 b1
= gen_linktype(ETHERTYPE_ATALK
);
4889 b1
= gen_linktype(ETHERTYPE_AARP
);
4893 b1
= gen_linktype(ETHERTYPE_DN
);
4897 b1
= gen_linktype(ETHERTYPE_SCA
);
4901 b1
= gen_linktype(ETHERTYPE_LAT
);
4905 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4909 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4913 b1
= gen_linktype(ETHERTYPE_IPV6
);
4916 #ifndef IPPROTO_ICMPV6
4917 #define IPPROTO_ICMPV6 58
4920 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4924 #define IPPROTO_AH 51
4927 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4928 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4933 #define IPPROTO_ESP 50
4936 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4937 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4942 b1
= gen_linktype(LLCSAP_ISONS
);
4946 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4950 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4953 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4954 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4955 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4957 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4959 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4961 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4965 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4966 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4967 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4969 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4971 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4973 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4977 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4978 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4979 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4981 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4986 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4987 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4992 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4993 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4995 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4997 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5002 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5003 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5008 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5009 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5014 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5018 b1
= gen_linktype(LLCSAP_8021D
);
5022 b1
= gen_linktype(LLCSAP_IPX
);
5026 b1
= gen_linktype(LLCSAP_NETBEUI
);
5030 bpf_error("'radio' is not a valid protocol type");
5038 static struct block
*
5044 /* not IPv4 frag other than the first frag */
5045 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5046 b
= new_block(JMP(BPF_JSET
));
5055 * Generate a comparison to a port value in the transport-layer header
5056 * at the specified offset from the beginning of that header.
5058 * XXX - this handles a variable-length prefix preceding the link-layer
5059 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5060 * variable-length link-layer headers (such as Token Ring or 802.11
5063 static struct block
*
5064 gen_portatom(off
, v
)
5068 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5071 static struct block
*
5072 gen_portatom6(off
, v
)
5076 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5080 gen_portop(port
, proto
, dir
)
5081 int port
, proto
, dir
;
5083 struct block
*b0
, *b1
, *tmp
;
5085 /* ip proto 'proto' and not a fragment other than the first fragment */
5086 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5092 b1
= gen_portatom(0, (bpf_int32
)port
);
5096 b1
= gen_portatom(2, (bpf_int32
)port
);
5101 tmp
= gen_portatom(0, (bpf_int32
)port
);
5102 b1
= gen_portatom(2, (bpf_int32
)port
);
5107 tmp
= gen_portatom(0, (bpf_int32
)port
);
5108 b1
= gen_portatom(2, (bpf_int32
)port
);
5120 static struct block
*
5121 gen_port(port
, ip_proto
, dir
)
5126 struct block
*b0
, *b1
, *tmp
;
5131 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5132 * not LLC encapsulation with LLCSAP_IP.
5134 * For IEEE 802 networks - which includes 802.5 token ring
5135 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5136 * says that SNAP encapsulation is used, not LLC encapsulation
5139 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5140 * RFC 2225 say that SNAP encapsulation is used, not LLC
5141 * encapsulation with LLCSAP_IP.
5143 * So we always check for ETHERTYPE_IP.
5145 b0
= gen_linktype(ETHERTYPE_IP
);
5151 b1
= gen_portop(port
, ip_proto
, dir
);
5155 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5156 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5158 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5170 gen_portop6(port
, proto
, dir
)
5171 int port
, proto
, dir
;
5173 struct block
*b0
, *b1
, *tmp
;
5175 /* ip6 proto 'proto' */
5176 /* XXX - catch the first fragment of a fragmented packet? */
5177 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5181 b1
= gen_portatom6(0, (bpf_int32
)port
);
5185 b1
= gen_portatom6(2, (bpf_int32
)port
);
5190 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5191 b1
= gen_portatom6(2, (bpf_int32
)port
);
5196 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5197 b1
= gen_portatom6(2, (bpf_int32
)port
);
5209 static struct block
*
5210 gen_port6(port
, ip_proto
, dir
)
5215 struct block
*b0
, *b1
, *tmp
;
5217 /* link proto ip6 */
5218 b0
= gen_linktype(ETHERTYPE_IPV6
);
5224 b1
= gen_portop6(port
, ip_proto
, dir
);
5228 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5229 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5231 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5242 /* gen_portrange code */
5243 static struct block
*
5244 gen_portrangeatom(off
, v1
, v2
)
5248 struct block
*b1
, *b2
;
5252 * Reverse the order of the ports, so v1 is the lower one.
5261 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5262 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5270 gen_portrangeop(port1
, port2
, proto
, dir
)
5275 struct block
*b0
, *b1
, *tmp
;
5277 /* ip proto 'proto' and not a fragment other than the first fragment */
5278 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5284 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5288 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5293 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5294 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5299 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5300 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5312 static struct block
*
5313 gen_portrange(port1
, port2
, ip_proto
, dir
)
5318 struct block
*b0
, *b1
, *tmp
;
5321 b0
= gen_linktype(ETHERTYPE_IP
);
5327 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5331 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5332 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5334 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5345 static struct block
*
5346 gen_portrangeatom6(off
, v1
, v2
)
5350 struct block
*b1
, *b2
;
5354 * Reverse the order of the ports, so v1 is the lower one.
5363 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5364 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5372 gen_portrangeop6(port1
, port2
, proto
, dir
)
5377 struct block
*b0
, *b1
, *tmp
;
5379 /* ip6 proto 'proto' */
5380 /* XXX - catch the first fragment of a fragmented packet? */
5381 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5385 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5389 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5394 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5395 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5400 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5401 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5413 static struct block
*
5414 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5419 struct block
*b0
, *b1
, *tmp
;
5421 /* link proto ip6 */
5422 b0
= gen_linktype(ETHERTYPE_IPV6
);
5428 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5432 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5433 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5435 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5447 lookup_proto(name
, proto
)
5448 register const char *name
;
5458 v
= pcap_nametoproto(name
);
5459 if (v
== PROTO_UNDEF
)
5460 bpf_error("unknown ip proto '%s'", name
);
5464 /* XXX should look up h/w protocol type based on linktype */
5465 v
= pcap_nametoeproto(name
);
5466 if (v
== PROTO_UNDEF
) {
5467 v
= pcap_nametollc(name
);
5468 if (v
== PROTO_UNDEF
)
5469 bpf_error("unknown ether proto '%s'", name
);
5474 if (strcmp(name
, "esis") == 0)
5476 else if (strcmp(name
, "isis") == 0)
5478 else if (strcmp(name
, "clnp") == 0)
5481 bpf_error("unknown osi proto '%s'", name
);
5501 static struct block
*
5502 gen_protochain(v
, proto
, dir
)
5507 #ifdef NO_PROTOCHAIN
5508 return gen_proto(v
, proto
, dir
);
5510 struct block
*b0
, *b
;
5511 struct slist
*s
[100];
5512 int fix2
, fix3
, fix4
, fix5
;
5513 int ahcheck
, again
, end
;
5515 int reg2
= alloc_reg();
5517 memset(s
, 0, sizeof(s
));
5518 fix2
= fix3
= fix4
= fix5
= 0;
5525 b0
= gen_protochain(v
, Q_IP
, dir
);
5526 b
= gen_protochain(v
, Q_IPV6
, dir
);
5530 bpf_error("bad protocol applied for 'protochain'");
5535 * We don't handle variable-length prefixes before the link-layer
5536 * header, or variable-length link-layer headers, here yet.
5537 * We might want to add BPF instructions to do the protochain
5538 * work, to simplify that and, on platforms that have a BPF
5539 * interpreter with the new instructions, let the filtering
5540 * be done in the kernel. (We already require a modified BPF
5541 * engine to do the protochain stuff, to support backward
5542 * branches, and backward branch support is unlikely to appear
5543 * in kernel BPF engines.)
5547 case DLT_IEEE802_11
:
5548 case DLT_PRISM_HEADER
:
5549 case DLT_IEEE802_11_RADIO_AVS
:
5550 case DLT_IEEE802_11_RADIO
:
5552 bpf_error("'protochain' not supported with 802.11");
5555 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5558 * s[0] is a dummy entry to protect other BPF insn from damage
5559 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5560 * hard to find interdependency made by jump table fixup.
5563 s
[i
] = new_stmt(0); /*dummy*/
5568 b0
= gen_linktype(ETHERTYPE_IP
);
5571 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5572 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5574 /* X = ip->ip_hl << 2 */
5575 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5576 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5581 b0
= gen_linktype(ETHERTYPE_IPV6
);
5583 /* A = ip6->ip_nxt */
5584 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5585 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5587 /* X = sizeof(struct ip6_hdr) */
5588 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5594 bpf_error("unsupported proto to gen_protochain");
5598 /* again: if (A == v) goto end; else fall through; */
5600 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5602 s
[i
]->s
.jt
= NULL
; /*later*/
5603 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5607 #ifndef IPPROTO_NONE
5608 #define IPPROTO_NONE 59
5610 /* if (A == IPPROTO_NONE) goto end */
5611 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5612 s
[i
]->s
.jt
= NULL
; /*later*/
5613 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5614 s
[i
]->s
.k
= IPPROTO_NONE
;
5615 s
[fix5
]->s
.jf
= s
[i
];
5619 if (proto
== Q_IPV6
) {
5620 int v6start
, v6end
, v6advance
, j
;
5623 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5624 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5625 s
[i
]->s
.jt
= NULL
; /*later*/
5626 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5627 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5628 s
[fix2
]->s
.jf
= s
[i
];
5630 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5631 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5632 s
[i
]->s
.jt
= NULL
; /*later*/
5633 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5634 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5636 /* if (A == IPPROTO_ROUTING) goto v6advance */
5637 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5638 s
[i
]->s
.jt
= NULL
; /*later*/
5639 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5640 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5642 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5643 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5644 s
[i
]->s
.jt
= NULL
; /*later*/
5645 s
[i
]->s
.jf
= NULL
; /*later*/
5646 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5656 * A = P[X + packet head];
5657 * X = X + (P[X + packet head + 1] + 1) * 8;
5659 /* A = P[X + packet head] */
5660 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5661 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5664 s
[i
] = new_stmt(BPF_ST
);
5667 /* A = P[X + packet head + 1]; */
5668 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5669 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5672 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5676 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5680 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5684 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5687 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5691 /* goto again; (must use BPF_JA for backward jump) */
5692 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5693 s
[i
]->s
.k
= again
- i
- 1;
5694 s
[i
- 1]->s
.jf
= s
[i
];
5698 for (j
= v6start
; j
<= v6end
; j
++)
5699 s
[j
]->s
.jt
= s
[v6advance
];
5702 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5704 s
[fix2
]->s
.jf
= s
[i
];
5710 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5711 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5712 s
[i
]->s
.jt
= NULL
; /*later*/
5713 s
[i
]->s
.jf
= NULL
; /*later*/
5714 s
[i
]->s
.k
= IPPROTO_AH
;
5716 s
[fix3
]->s
.jf
= s
[ahcheck
];
5723 * X = X + (P[X + 1] + 2) * 4;
5726 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5728 /* A = P[X + packet head]; */
5729 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5730 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5733 s
[i
] = new_stmt(BPF_ST
);
5737 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5740 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5744 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5746 /* A = P[X + packet head] */
5747 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5748 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5751 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5755 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5759 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5762 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5766 /* goto again; (must use BPF_JA for backward jump) */
5767 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5768 s
[i
]->s
.k
= again
- i
- 1;
5773 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5775 s
[fix2
]->s
.jt
= s
[end
];
5776 s
[fix4
]->s
.jf
= s
[end
];
5777 s
[fix5
]->s
.jt
= s
[end
];
5784 for (i
= 0; i
< max
- 1; i
++)
5785 s
[i
]->next
= s
[i
+ 1];
5786 s
[max
- 1]->next
= NULL
;
5791 b
= new_block(JMP(BPF_JEQ
));
5792 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5802 static struct block
*
5803 gen_check_802_11_data_frame()
5806 struct block
*b0
, *b1
;
5809 * A data frame has the 0x08 bit (b3) in the frame control field set
5810 * and the 0x04 bit (b2) clear.
5812 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5813 b0
= new_block(JMP(BPF_JSET
));
5817 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5818 b1
= new_block(JMP(BPF_JSET
));
5829 * Generate code that checks whether the packet is a packet for protocol
5830 * <proto> and whether the type field in that protocol's header has
5831 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5832 * IP packet and checks the protocol number in the IP header against <v>.
5834 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5835 * against Q_IP and Q_IPV6.
5837 static struct block
*
5838 gen_proto(v
, proto
, dir
)
5843 struct block
*b0
, *b1
;
5848 if (dir
!= Q_DEFAULT
)
5849 bpf_error("direction applied to 'proto'");
5853 b0
= gen_proto(v
, Q_IP
, dir
);
5854 b1
= gen_proto(v
, Q_IPV6
, dir
);
5860 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5861 * not LLC encapsulation with LLCSAP_IP.
5863 * For IEEE 802 networks - which includes 802.5 token ring
5864 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5865 * says that SNAP encapsulation is used, not LLC encapsulation
5868 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5869 * RFC 2225 say that SNAP encapsulation is used, not LLC
5870 * encapsulation with LLCSAP_IP.
5872 * So we always check for ETHERTYPE_IP.
5874 b0
= gen_linktype(ETHERTYPE_IP
);
5876 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5878 b1
= gen_protochain(v
, Q_IP
);
5888 * Frame Relay packets typically have an OSI
5889 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5890 * generates code to check for all the OSI
5891 * NLPIDs, so calling it and then adding a check
5892 * for the particular NLPID for which we're
5893 * looking is bogus, as we can just check for
5896 * What we check for is the NLPID and a frame
5897 * control field value of UI, i.e. 0x03 followed
5900 * XXX - assumes a 2-byte Frame Relay header with
5901 * DLCI and flags. What if the address is longer?
5903 * XXX - what about SNAP-encapsulated frames?
5905 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5911 * Cisco uses an Ethertype lookalike - for OSI,
5914 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5915 /* OSI in C-HDLC is stuffed with a fudge byte */
5916 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
5921 b0
= gen_linktype(LLCSAP_ISONS
);
5922 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
5928 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5930 * 4 is the offset of the PDU type relative to the IS-IS
5933 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
5938 bpf_error("arp does not encapsulate another protocol");
5942 bpf_error("rarp does not encapsulate another protocol");
5946 bpf_error("atalk encapsulation is not specifiable");
5950 bpf_error("decnet encapsulation is not specifiable");
5954 bpf_error("sca does not encapsulate another protocol");
5958 bpf_error("lat does not encapsulate another protocol");
5962 bpf_error("moprc does not encapsulate another protocol");
5966 bpf_error("mopdl does not encapsulate another protocol");
5970 return gen_linktype(v
);
5973 bpf_error("'udp proto' is bogus");
5977 bpf_error("'tcp proto' is bogus");
5981 bpf_error("'sctp proto' is bogus");
5985 bpf_error("'icmp proto' is bogus");
5989 bpf_error("'igmp proto' is bogus");
5993 bpf_error("'igrp proto' is bogus");
5997 bpf_error("'pim proto' is bogus");
6001 bpf_error("'vrrp proto' is bogus");
6005 bpf_error("'carp proto' is bogus");
6009 b0
= gen_linktype(ETHERTYPE_IPV6
);
6012 * Also check for a fragment header before the final
6015 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6016 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6018 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6021 b1
= gen_protochain(v
, Q_IPV6
);
6027 bpf_error("'icmp6 proto' is bogus");
6030 bpf_error("'ah proto' is bogus");
6033 bpf_error("'ah proto' is bogus");
6036 bpf_error("'stp proto' is bogus");
6039 bpf_error("'ipx proto' is bogus");
6042 bpf_error("'netbeui proto' is bogus");
6045 bpf_error("'radio proto' is bogus");
6056 register const char *name
;
6059 int proto
= q
.proto
;
6063 bpf_u_int32 mask
, addr
;
6065 bpf_u_int32
**alist
;
6068 struct sockaddr_in
*sin4
;
6069 struct sockaddr_in6
*sin6
;
6070 struct addrinfo
*res
, *res0
;
6071 struct in6_addr mask128
;
6073 struct block
*b
, *tmp
;
6074 int port
, real_proto
;
6080 addr
= pcap_nametonetaddr(name
);
6082 bpf_error("unknown network '%s'", name
);
6083 /* Left justify network addr and calculate its network mask */
6085 while (addr
&& (addr
& 0xff000000) == 0) {
6089 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6093 if (proto
== Q_LINK
) {
6097 case DLT_NETANALYZER
:
6098 case DLT_NETANALYZER_TRANSPARENT
:
6099 eaddr
= pcap_ether_hostton(name
);
6102 "unknown ether host '%s'", name
);
6103 tmp
= gen_prevlinkhdr_check();
6104 b
= gen_ehostop(eaddr
, dir
);
6111 eaddr
= pcap_ether_hostton(name
);
6114 "unknown FDDI host '%s'", name
);
6115 b
= gen_fhostop(eaddr
, dir
);
6120 eaddr
= pcap_ether_hostton(name
);
6123 "unknown token ring host '%s'", name
);
6124 b
= gen_thostop(eaddr
, dir
);
6128 case DLT_IEEE802_11
:
6129 case DLT_PRISM_HEADER
:
6130 case DLT_IEEE802_11_RADIO_AVS
:
6131 case DLT_IEEE802_11_RADIO
:
6133 eaddr
= pcap_ether_hostton(name
);
6136 "unknown 802.11 host '%s'", name
);
6137 b
= gen_wlanhostop(eaddr
, dir
);
6141 case DLT_IP_OVER_FC
:
6142 eaddr
= pcap_ether_hostton(name
);
6145 "unknown Fibre Channel host '%s'", name
);
6146 b
= gen_ipfchostop(eaddr
, dir
);
6151 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6152 } else if (proto
== Q_DECNET
) {
6153 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6155 * I don't think DECNET hosts can be multihomed, so
6156 * there is no need to build up a list of addresses
6158 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6161 alist
= pcap_nametoaddr(name
);
6162 if (alist
== NULL
|| *alist
== NULL
)
6163 bpf_error("unknown host '%s'", name
);
6165 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6167 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6169 tmp
= gen_host(**alist
++, 0xffffffff,
6170 tproto
, dir
, q
.addr
);
6176 memset(&mask128
, 0xff, sizeof(mask128
));
6177 res0
= res
= pcap_nametoaddrinfo(name
);
6179 bpf_error("unknown host '%s'", name
);
6182 tproto
= tproto6
= proto
;
6183 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6187 for (res
= res0
; res
; res
= res
->ai_next
) {
6188 switch (res
->ai_family
) {
6190 if (tproto
== Q_IPV6
)
6193 sin4
= (struct sockaddr_in
*)
6195 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6196 0xffffffff, tproto
, dir
, q
.addr
);
6199 if (tproto6
== Q_IP
)
6202 sin6
= (struct sockaddr_in6
*)
6204 tmp
= gen_host6(&sin6
->sin6_addr
,
6205 &mask128
, tproto6
, dir
, q
.addr
);
6217 bpf_error("unknown host '%s'%s", name
,
6218 (proto
== Q_DEFAULT
)
6220 : " for specified address family");
6227 if (proto
!= Q_DEFAULT
&&
6228 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6229 bpf_error("illegal qualifier of 'port'");
6230 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6231 bpf_error("unknown port '%s'", name
);
6232 if (proto
== Q_UDP
) {
6233 if (real_proto
== IPPROTO_TCP
)
6234 bpf_error("port '%s' is tcp", name
);
6235 else if (real_proto
== IPPROTO_SCTP
)
6236 bpf_error("port '%s' is sctp", name
);
6238 /* override PROTO_UNDEF */
6239 real_proto
= IPPROTO_UDP
;
6241 if (proto
== Q_TCP
) {
6242 if (real_proto
== IPPROTO_UDP
)
6243 bpf_error("port '%s' is udp", name
);
6245 else if (real_proto
== IPPROTO_SCTP
)
6246 bpf_error("port '%s' is sctp", name
);
6248 /* override PROTO_UNDEF */
6249 real_proto
= IPPROTO_TCP
;
6251 if (proto
== Q_SCTP
) {
6252 if (real_proto
== IPPROTO_UDP
)
6253 bpf_error("port '%s' is udp", name
);
6255 else if (real_proto
== IPPROTO_TCP
)
6256 bpf_error("port '%s' is tcp", name
);
6258 /* override PROTO_UNDEF */
6259 real_proto
= IPPROTO_SCTP
;
6262 bpf_error("illegal port number %d < 0", port
);
6264 bpf_error("illegal port number %d > 65535", port
);
6265 b
= gen_port(port
, real_proto
, dir
);
6266 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6270 if (proto
!= Q_DEFAULT
&&
6271 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6272 bpf_error("illegal qualifier of 'portrange'");
6273 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6274 bpf_error("unknown port in range '%s'", name
);
6275 if (proto
== Q_UDP
) {
6276 if (real_proto
== IPPROTO_TCP
)
6277 bpf_error("port in range '%s' is tcp", name
);
6278 else if (real_proto
== IPPROTO_SCTP
)
6279 bpf_error("port in range '%s' is sctp", name
);
6281 /* override PROTO_UNDEF */
6282 real_proto
= IPPROTO_UDP
;
6284 if (proto
== Q_TCP
) {
6285 if (real_proto
== IPPROTO_UDP
)
6286 bpf_error("port in range '%s' is udp", name
);
6287 else if (real_proto
== IPPROTO_SCTP
)
6288 bpf_error("port in range '%s' is sctp", name
);
6290 /* override PROTO_UNDEF */
6291 real_proto
= IPPROTO_TCP
;
6293 if (proto
== Q_SCTP
) {
6294 if (real_proto
== IPPROTO_UDP
)
6295 bpf_error("port in range '%s' is udp", name
);
6296 else if (real_proto
== IPPROTO_TCP
)
6297 bpf_error("port in range '%s' is tcp", name
);
6299 /* override PROTO_UNDEF */
6300 real_proto
= IPPROTO_SCTP
;
6303 bpf_error("illegal port number %d < 0", port1
);
6305 bpf_error("illegal port number %d > 65535", port1
);
6307 bpf_error("illegal port number %d < 0", port2
);
6309 bpf_error("illegal port number %d > 65535", port2
);
6311 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6312 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6317 eaddr
= pcap_ether_hostton(name
);
6319 bpf_error("unknown ether host: %s", name
);
6321 alist
= pcap_nametoaddr(name
);
6322 if (alist
== NULL
|| *alist
== NULL
)
6323 bpf_error("unknown host '%s'", name
);
6324 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6328 bpf_error("'gateway' not supported in this configuration");
6332 real_proto
= lookup_proto(name
, proto
);
6333 if (real_proto
>= 0)
6334 return gen_proto(real_proto
, proto
, dir
);
6336 bpf_error("unknown protocol: %s", name
);
6339 real_proto
= lookup_proto(name
, proto
);
6340 if (real_proto
>= 0)
6341 return gen_protochain(real_proto
, proto
, dir
);
6343 bpf_error("unknown protocol: %s", name
);
6354 gen_mcode(s1
, s2
, masklen
, q
)
6355 register const char *s1
, *s2
;
6356 register unsigned int masklen
;
6359 register int nlen
, mlen
;
6362 nlen
= __pcap_atoin(s1
, &n
);
6363 /* Promote short ipaddr */
6367 mlen
= __pcap_atoin(s2
, &m
);
6368 /* Promote short ipaddr */
6371 bpf_error("non-network bits set in \"%s mask %s\"",
6374 /* Convert mask len to mask */
6376 bpf_error("mask length must be <= 32");
6379 * X << 32 is not guaranteed by C to be 0; it's
6384 m
= 0xffffffff << (32 - masklen
);
6386 bpf_error("non-network bits set in \"%s/%d\"",
6393 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6396 bpf_error("Mask syntax for networks only");
6405 register const char *s
;
6410 int proto
= q
.proto
;
6416 else if (q
.proto
== Q_DECNET
)
6417 vlen
= __pcap_atodn(s
, &v
);
6419 vlen
= __pcap_atoin(s
, &v
);
6426 if (proto
== Q_DECNET
)
6427 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6428 else if (proto
== Q_LINK
) {
6429 bpf_error("illegal link layer address");
6432 if (s
== NULL
&& q
.addr
== Q_NET
) {
6433 /* Promote short net number */
6434 while (v
&& (v
& 0xff000000) == 0) {
6439 /* Promote short ipaddr */
6443 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6448 proto
= IPPROTO_UDP
;
6449 else if (proto
== Q_TCP
)
6450 proto
= IPPROTO_TCP
;
6451 else if (proto
== Q_SCTP
)
6452 proto
= IPPROTO_SCTP
;
6453 else if (proto
== Q_DEFAULT
)
6454 proto
= PROTO_UNDEF
;
6456 bpf_error("illegal qualifier of 'port'");
6459 bpf_error("illegal port number %u > 65535", v
);
6463 b
= gen_port((int)v
, proto
, dir
);
6464 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6470 proto
= IPPROTO_UDP
;
6471 else if (proto
== Q_TCP
)
6472 proto
= IPPROTO_TCP
;
6473 else if (proto
== Q_SCTP
)
6474 proto
= IPPROTO_SCTP
;
6475 else if (proto
== Q_DEFAULT
)
6476 proto
= PROTO_UNDEF
;
6478 bpf_error("illegal qualifier of 'portrange'");
6481 bpf_error("illegal port number %u > 65535", v
);
6485 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6486 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6491 bpf_error("'gateway' requires a name");
6495 return gen_proto((int)v
, proto
, dir
);
6498 return gen_protochain((int)v
, proto
, dir
);
6513 gen_mcode6(s1
, s2
, masklen
, q
)
6514 register const char *s1
, *s2
;
6515 register unsigned int masklen
;
6518 struct addrinfo
*res
;
6519 struct in6_addr
*addr
;
6520 struct in6_addr mask
;
6525 bpf_error("no mask %s supported", s2
);
6527 res
= pcap_nametoaddrinfo(s1
);
6529 bpf_error("invalid ip6 address %s", s1
);
6532 bpf_error("%s resolved to multiple address", s1
);
6533 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6535 if (sizeof(mask
) * 8 < masklen
)
6536 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6537 memset(&mask
, 0, sizeof(mask
));
6538 memset(&mask
, 0xff, masklen
/ 8);
6540 mask
.s6_addr
[masklen
/ 8] =
6541 (0xff << (8 - masklen
% 8)) & 0xff;
6544 a
= (u_int32_t
*)addr
;
6545 m
= (u_int32_t
*)&mask
;
6546 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6547 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6548 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6556 bpf_error("Mask syntax for networks only");
6560 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6566 bpf_error("invalid qualifier against IPv6 address");
6575 register const u_char
*eaddr
;
6578 struct block
*b
, *tmp
;
6580 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6583 case DLT_NETANALYZER
:
6584 case DLT_NETANALYZER_TRANSPARENT
:
6585 tmp
= gen_prevlinkhdr_check();
6586 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6591 return gen_fhostop(eaddr
, (int)q
.dir
);
6593 return gen_thostop(eaddr
, (int)q
.dir
);
6594 case DLT_IEEE802_11
:
6595 case DLT_PRISM_HEADER
:
6596 case DLT_IEEE802_11_RADIO_AVS
:
6597 case DLT_IEEE802_11_RADIO
:
6599 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6600 case DLT_IP_OVER_FC
:
6601 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6603 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6607 bpf_error("ethernet address used in non-ether expression");
6614 struct slist
*s0
, *s1
;
6617 * This is definitely not the best way to do this, but the
6618 * lists will rarely get long.
6625 static struct slist
*
6631 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6636 static struct slist
*
6642 s
= new_stmt(BPF_LD
|BPF_MEM
);
6648 * Modify "index" to use the value stored into its register as an
6649 * offset relative to the beginning of the header for the protocol
6650 * "proto", and allocate a register and put an item "size" bytes long
6651 * (1, 2, or 4) at that offset into that register, making it the register
6655 gen_load(proto
, inst
, size
)
6660 struct slist
*s
, *tmp
;
6662 int regno
= alloc_reg();
6664 free_reg(inst
->regno
);
6668 bpf_error("data size must be 1, 2, or 4");
6684 bpf_error("unsupported index operation");
6688 * The offset is relative to the beginning of the packet
6689 * data, if we have a radio header. (If we don't, this
6692 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6693 linktype
!= DLT_IEEE802_11_RADIO
&&
6694 linktype
!= DLT_PRISM_HEADER
)
6695 bpf_error("radio information not present in capture");
6698 * Load into the X register the offset computed into the
6699 * register specified by "index".
6701 s
= xfer_to_x(inst
);
6704 * Load the item at that offset.
6706 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6708 sappend(inst
->s
, s
);
6713 * The offset is relative to the beginning of
6714 * the link-layer header.
6716 * XXX - what about ATM LANE? Should the index be
6717 * relative to the beginning of the AAL5 frame, so
6718 * that 0 refers to the beginning of the LE Control
6719 * field, or relative to the beginning of the LAN
6720 * frame, so that 0 refers, for Ethernet LANE, to
6721 * the beginning of the destination address?
6723 s
= gen_abs_offset_varpart(&off_linkhdr
);
6726 * If "s" is non-null, it has code to arrange that the
6727 * X register contains the length of the prefix preceding
6728 * the link-layer header. Add to it the offset computed
6729 * into the register specified by "index", and move that
6730 * into the X register. Otherwise, just load into the X
6731 * register the offset computed into the register specified
6735 sappend(s
, xfer_to_a(inst
));
6736 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6737 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6739 s
= xfer_to_x(inst
);
6742 * Load the item at the sum of the offset we've put in the
6743 * X register and the offset of the start of the link
6744 * layer header (which is 0 if the radio header is
6745 * variable-length; that header length is what we put
6746 * into the X register and then added to the index).
6748 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6749 tmp
->s
.k
= off_linkhdr
.constant_part
;
6751 sappend(inst
->s
, s
);
6765 * The offset is relative to the beginning of
6766 * the network-layer header.
6767 * XXX - are there any cases where we want
6770 s
= gen_abs_offset_varpart(&off_linkpl
);
6773 * If "s" is non-null, it has code to arrange that the
6774 * X register contains the variable part of the offset
6775 * of the link-layer payload. Add to it the offset
6776 * computed into the register specified by "index",
6777 * and move that into the X register. Otherwise, just
6778 * load into the X register the offset computed into
6779 * the register specified by "index".
6782 sappend(s
, xfer_to_a(inst
));
6783 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6784 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6786 s
= xfer_to_x(inst
);
6789 * Load the item at the sum of the offset we've put in the
6790 * X register, the offset of the start of the network
6791 * layer header from the beginning of the link-layer
6792 * payload, and the constant part of the offset of the
6793 * start of the link-layer payload.
6795 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6796 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6798 sappend(inst
->s
, s
);
6801 * Do the computation only if the packet contains
6802 * the protocol in question.
6804 b
= gen_proto_abbrev(proto
);
6806 gen_and(inst
->b
, b
);
6820 * The offset is relative to the beginning of
6821 * the transport-layer header.
6823 * Load the X register with the length of the IPv4 header
6824 * (plus the offset of the link-layer header, if it's
6825 * a variable-length header), in bytes.
6827 * XXX - are there any cases where we want
6829 * XXX - we should, if we're built with
6830 * IPv6 support, generate code to load either
6831 * IPv4, IPv6, or both, as appropriate.
6833 s
= gen_loadx_iphdrlen();
6836 * The X register now contains the sum of the variable
6837 * part of the offset of the link-layer payload and the
6838 * length of the network-layer header.
6840 * Load into the A register the offset relative to
6841 * the beginning of the transport layer header,
6842 * add the X register to that, move that to the
6843 * X register, and load with an offset from the
6844 * X register equal to the sum of the constant part of
6845 * the offset of the link-layer payload and the offset,
6846 * relative to the beginning of the link-layer payload,
6847 * of the network-layer header.
6849 sappend(s
, xfer_to_a(inst
));
6850 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6851 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6852 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6853 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6854 sappend(inst
->s
, s
);
6857 * Do the computation only if the packet contains
6858 * the protocol in question - which is true only
6859 * if this is an IP datagram and is the first or
6860 * only fragment of that datagram.
6862 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6864 gen_and(inst
->b
, b
);
6865 gen_and(gen_proto_abbrev(Q_IP
), b
);
6869 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6872 inst
->regno
= regno
;
6873 s
= new_stmt(BPF_ST
);
6875 sappend(inst
->s
, s
);
6881 gen_relation(code
, a0
, a1
, reversed
)
6883 struct arth
*a0
, *a1
;
6886 struct slist
*s0
, *s1
, *s2
;
6887 struct block
*b
, *tmp
;
6891 if (code
== BPF_JEQ
) {
6892 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6893 b
= new_block(JMP(code
));
6897 b
= new_block(BPF_JMP
|code
|BPF_X
);
6903 sappend(a0
->s
, a1
->s
);
6907 free_reg(a0
->regno
);
6908 free_reg(a1
->regno
);
6910 /* 'and' together protocol checks */
6913 gen_and(a0
->b
, tmp
= a1
->b
);
6929 int regno
= alloc_reg();
6930 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6933 s
= new_stmt(BPF_LD
|BPF_LEN
);
6934 s
->next
= new_stmt(BPF_ST
);
6935 s
->next
->s
.k
= regno
;
6950 a
= (struct arth
*)newchunk(sizeof(*a
));
6954 s
= new_stmt(BPF_LD
|BPF_IMM
);
6956 s
->next
= new_stmt(BPF_ST
);
6972 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6975 s
= new_stmt(BPF_ST
);
6983 gen_arth(code
, a0
, a1
)
6985 struct arth
*a0
, *a1
;
6987 struct slist
*s0
, *s1
, *s2
;
6991 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6996 sappend(a0
->s
, a1
->s
);
6998 free_reg(a0
->regno
);
6999 free_reg(a1
->regno
);
7001 s0
= new_stmt(BPF_ST
);
7002 a0
->regno
= s0
->s
.k
= alloc_reg();
7009 * Here we handle simple allocation of the scratch registers.
7010 * If too many registers are alloc'd, the allocator punts.
7012 static int regused
[BPF_MEMWORDS
];
7016 * Initialize the table of used registers and the current register.
7022 memset(regused
, 0, sizeof regused
);
7026 * Return the next free register.
7031 int n
= BPF_MEMWORDS
;
7034 if (regused
[curreg
])
7035 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7037 regused
[curreg
] = 1;
7041 bpf_error("too many registers needed to evaluate expression");
7047 * Return a register to the table so it can
7057 static struct block
*
7064 s
= new_stmt(BPF_LD
|BPF_LEN
);
7065 b
= new_block(JMP(jmp
));
7076 return gen_len(BPF_JGE
, n
);
7080 * Actually, this is less than or equal.
7088 b
= gen_len(BPF_JGT
, n
);
7095 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7096 * the beginning of the link-layer header.
7097 * XXX - that means you can't test values in the radiotap header, but
7098 * as that header is difficult if not impossible to parse generally
7099 * without a loop, that might not be a severe problem. A new keyword
7100 * "radio" could be added for that, although what you'd really want
7101 * would be a way of testing particular radio header values, which
7102 * would generate code appropriate to the radio header in question.
7105 gen_byteop(op
, idx
, val
)
7116 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7119 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7123 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7127 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7131 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7135 b
= new_block(JMP(BPF_JEQ
));
7142 static u_char abroadcast
[] = { 0x0 };
7145 gen_broadcast(proto
)
7148 bpf_u_int32 hostmask
;
7149 struct block
*b0
, *b1
, *b2
;
7150 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7158 case DLT_ARCNET_LINUX
:
7159 return gen_ahostop(abroadcast
, Q_DST
);
7161 case DLT_NETANALYZER
:
7162 case DLT_NETANALYZER_TRANSPARENT
:
7163 b1
= gen_prevlinkhdr_check();
7164 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7169 return gen_fhostop(ebroadcast
, Q_DST
);
7171 return gen_thostop(ebroadcast
, Q_DST
);
7172 case DLT_IEEE802_11
:
7173 case DLT_PRISM_HEADER
:
7174 case DLT_IEEE802_11_RADIO_AVS
:
7175 case DLT_IEEE802_11_RADIO
:
7177 return gen_wlanhostop(ebroadcast
, Q_DST
);
7178 case DLT_IP_OVER_FC
:
7179 return gen_ipfchostop(ebroadcast
, Q_DST
);
7181 bpf_error("not a broadcast link");
7187 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7188 * as an indication that we don't know the netmask, and fail
7191 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7192 bpf_error("netmask not known, so 'ip broadcast' not supported");
7193 b0
= gen_linktype(ETHERTYPE_IP
);
7194 hostmask
= ~netmask
;
7195 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7196 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7197 (bpf_int32
)(~0 & hostmask
), hostmask
);
7202 bpf_error("only link-layer/IP broadcast filters supported");
7208 * Generate code to test the low-order bit of a MAC address (that's
7209 * the bottom bit of the *first* byte).
7211 static struct block
*
7212 gen_mac_multicast(offset
)
7215 register struct block
*b0
;
7216 register struct slist
*s
;
7218 /* link[offset] & 1 != 0 */
7219 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7220 b0
= new_block(JMP(BPF_JSET
));
7227 gen_multicast(proto
)
7230 register struct block
*b0
, *b1
, *b2
;
7231 register struct slist
*s
;
7239 case DLT_ARCNET_LINUX
:
7240 /* all ARCnet multicasts use the same address */
7241 return gen_ahostop(abroadcast
, Q_DST
);
7243 case DLT_NETANALYZER
:
7244 case DLT_NETANALYZER_TRANSPARENT
:
7245 b1
= gen_prevlinkhdr_check();
7246 /* ether[0] & 1 != 0 */
7247 b0
= gen_mac_multicast(0);
7253 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7255 * XXX - was that referring to bit-order issues?
7257 /* fddi[1] & 1 != 0 */
7258 return gen_mac_multicast(1);
7260 /* tr[2] & 1 != 0 */
7261 return gen_mac_multicast(2);
7262 case DLT_IEEE802_11
:
7263 case DLT_PRISM_HEADER
:
7264 case DLT_IEEE802_11_RADIO_AVS
:
7265 case DLT_IEEE802_11_RADIO
:
7270 * For control frames, there is no DA.
7272 * For management frames, DA is at an
7273 * offset of 4 from the beginning of
7276 * For data frames, DA is at an offset
7277 * of 4 from the beginning of the packet
7278 * if To DS is clear and at an offset of
7279 * 16 from the beginning of the packet
7284 * Generate the tests to be done for data frames.
7286 * First, check for To DS set, i.e. "link[1] & 0x01".
7288 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7289 b1
= new_block(JMP(BPF_JSET
));
7290 b1
->s
.k
= 0x01; /* To DS */
7294 * If To DS is set, the DA is at 16.
7296 b0
= gen_mac_multicast(16);
7300 * Now, check for To DS not set, i.e. check
7301 * "!(link[1] & 0x01)".
7303 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7304 b2
= new_block(JMP(BPF_JSET
));
7305 b2
->s
.k
= 0x01; /* To DS */
7310 * If To DS is not set, the DA is at 4.
7312 b1
= gen_mac_multicast(4);
7316 * Now OR together the last two checks. That gives
7317 * the complete set of checks for data frames.
7322 * Now check for a data frame.
7323 * I.e, check "link[0] & 0x08".
7325 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7326 b1
= new_block(JMP(BPF_JSET
));
7331 * AND that with the checks done for data frames.
7336 * If the high-order bit of the type value is 0, this
7337 * is a management frame.
7338 * I.e, check "!(link[0] & 0x08)".
7340 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7341 b2
= new_block(JMP(BPF_JSET
));
7347 * For management frames, the DA is at 4.
7349 b1
= gen_mac_multicast(4);
7353 * OR that with the checks done for data frames.
7354 * That gives the checks done for management and
7360 * If the low-order bit of the type value is 1,
7361 * this is either a control frame or a frame
7362 * with a reserved type, and thus not a
7365 * I.e., check "!(link[0] & 0x04)".
7367 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7368 b1
= new_block(JMP(BPF_JSET
));
7374 * AND that with the checks for data and management
7379 case DLT_IP_OVER_FC
:
7380 b0
= gen_mac_multicast(2);
7385 /* Link not known to support multicasts */
7389 b0
= gen_linktype(ETHERTYPE_IP
);
7390 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7395 b0
= gen_linktype(ETHERTYPE_IPV6
);
7396 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7400 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7406 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7407 * Outbound traffic is sent by this machine, while inbound traffic is
7408 * sent by a remote machine (and may include packets destined for a
7409 * unicast or multicast link-layer address we are not subscribing to).
7410 * These are the same definitions implemented by pcap_setdirection().
7411 * Capturing only unicast traffic destined for this host is probably
7412 * better accomplished using a higher-layer filter.
7418 register struct block
*b0
;
7421 * Only some data link types support inbound/outbound qualifiers.
7425 b0
= gen_relation(BPF_JEQ
,
7426 gen_load(Q_LINK
, gen_loadi(0), 1),
7433 /* match outgoing packets */
7434 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7436 /* match incoming packets */
7437 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7442 /* match outgoing packets */
7443 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7445 /* to filter on inbound traffic, invert the match */
7450 #ifdef HAVE_NET_PFVAR_H
7452 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7453 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7459 /* match outgoing packets */
7460 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7462 /* match incoming packets */
7463 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7467 case DLT_JUNIPER_MFR
:
7468 case DLT_JUNIPER_MLFR
:
7469 case DLT_JUNIPER_MLPPP
:
7470 case DLT_JUNIPER_ATM1
:
7471 case DLT_JUNIPER_ATM2
:
7472 case DLT_JUNIPER_PPPOE
:
7473 case DLT_JUNIPER_PPPOE_ATM
:
7474 case DLT_JUNIPER_GGSN
:
7475 case DLT_JUNIPER_ES
:
7476 case DLT_JUNIPER_MONITOR
:
7477 case DLT_JUNIPER_SERVICES
:
7478 case DLT_JUNIPER_ETHER
:
7479 case DLT_JUNIPER_PPP
:
7480 case DLT_JUNIPER_FRELAY
:
7481 case DLT_JUNIPER_CHDLC
:
7482 case DLT_JUNIPER_VP
:
7483 case DLT_JUNIPER_ST
:
7484 case DLT_JUNIPER_ISM
:
7485 case DLT_JUNIPER_VS
:
7486 case DLT_JUNIPER_SRX_E2E
:
7487 case DLT_JUNIPER_FIBRECHANNEL
:
7488 case DLT_JUNIPER_ATM_CEMIC
:
7490 /* juniper flags (including direction) are stored
7491 * the byte after the 3-byte magic number */
7493 /* match outgoing packets */
7494 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7496 /* match incoming packets */
7497 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7503 * If we have packet meta-data indicating a direction,
7504 * check it, otherwise give up as this link-layer type
7505 * has nothing in the packet data.
7507 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7509 * This is Linux with PF_PACKET support.
7510 * If this is a *live* capture, we can look at
7511 * special meta-data in the filter expression;
7512 * if it's a savefile, we can't.
7514 if (bpf_pcap
->rfile
!= NULL
) {
7515 /* We have a FILE *, so this is a savefile */
7516 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7521 /* match outgoing packets */
7522 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7525 /* to filter on inbound traffic, invert the match */
7528 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7529 bpf_error("inbound/outbound not supported on linktype %d",
7533 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7538 #ifdef HAVE_NET_PFVAR_H
7539 /* PF firewall log matched interface */
7541 gen_pf_ifname(const char *ifname
)
7546 if (linktype
!= DLT_PFLOG
) {
7547 bpf_error("ifname supported only on PF linktype");
7550 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7551 off
= offsetof(struct pfloghdr
, ifname
);
7552 if (strlen(ifname
) >= len
) {
7553 bpf_error("ifname interface names can only be %d characters",
7557 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7561 /* PF firewall log ruleset name */
7563 gen_pf_ruleset(char *ruleset
)
7567 if (linktype
!= DLT_PFLOG
) {
7568 bpf_error("ruleset supported only on PF linktype");
7572 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7573 bpf_error("ruleset names can only be %ld characters",
7574 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7578 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7579 strlen(ruleset
), (const u_char
*)ruleset
);
7583 /* PF firewall log rule number */
7589 if (linktype
!= DLT_PFLOG
) {
7590 bpf_error("rnr supported only on PF linktype");
7594 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7599 /* PF firewall log sub-rule number */
7601 gen_pf_srnr(int srnr
)
7605 if (linktype
!= DLT_PFLOG
) {
7606 bpf_error("srnr supported only on PF linktype");
7610 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7615 /* PF firewall log reason code */
7617 gen_pf_reason(int reason
)
7621 if (linktype
!= DLT_PFLOG
) {
7622 bpf_error("reason supported only on PF linktype");
7626 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7631 /* PF firewall log action */
7633 gen_pf_action(int action
)
7637 if (linktype
!= DLT_PFLOG
) {
7638 bpf_error("action supported only on PF linktype");
7642 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7646 #else /* !HAVE_NET_PFVAR_H */
7648 gen_pf_ifname(const char *ifname
)
7650 bpf_error("libpcap was compiled without pf support");
7656 gen_pf_ruleset(char *ruleset
)
7658 bpf_error("libpcap was compiled on a machine without pf support");
7666 bpf_error("libpcap was compiled on a machine without pf support");
7672 gen_pf_srnr(int srnr
)
7674 bpf_error("libpcap was compiled on a machine without pf support");
7680 gen_pf_reason(int reason
)
7682 bpf_error("libpcap was compiled on a machine without pf support");
7688 gen_pf_action(int action
)
7690 bpf_error("libpcap was compiled on a machine without pf support");
7694 #endif /* HAVE_NET_PFVAR_H */
7696 /* IEEE 802.11 wireless header */
7698 gen_p80211_type(int type
, int mask
)
7704 case DLT_IEEE802_11
:
7705 case DLT_PRISM_HEADER
:
7706 case DLT_IEEE802_11_RADIO_AVS
:
7707 case DLT_IEEE802_11_RADIO
:
7708 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7713 bpf_error("802.11 link-layer types supported only on 802.11");
7721 gen_p80211_fcdir(int fcdir
)
7727 case DLT_IEEE802_11
:
7728 case DLT_PRISM_HEADER
:
7729 case DLT_IEEE802_11_RADIO_AVS
:
7730 case DLT_IEEE802_11_RADIO
:
7734 bpf_error("frame direction supported only with 802.11 headers");
7738 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7739 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7746 register const u_char
*eaddr
;
7752 case DLT_ARCNET_LINUX
:
7753 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7755 return (gen_ahostop(eaddr
, (int)q
.dir
));
7757 bpf_error("ARCnet address used in non-arc expression");
7763 bpf_error("aid supported only on ARCnet");
7766 bpf_error("ARCnet address used in non-arc expression");
7771 static struct block
*
7772 gen_ahostop(eaddr
, dir
)
7773 register const u_char
*eaddr
;
7776 register struct block
*b0
, *b1
;
7779 /* src comes first, different from Ethernet */
7781 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7784 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7787 b0
= gen_ahostop(eaddr
, Q_SRC
);
7788 b1
= gen_ahostop(eaddr
, Q_DST
);
7794 b0
= gen_ahostop(eaddr
, Q_SRC
);
7795 b1
= gen_ahostop(eaddr
, Q_DST
);
7800 bpf_error("'addr1' is only supported on 802.11");
7804 bpf_error("'addr2' is only supported on 802.11");
7808 bpf_error("'addr3' is only supported on 802.11");
7812 bpf_error("'addr4' is only supported on 802.11");
7816 bpf_error("'ra' is only supported on 802.11");
7820 bpf_error("'ta' is only supported on 802.11");
7827 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7828 static struct block
*
7829 gen_vlan_bpf_extensions(int vlan_num
)
7831 struct block
*b0
, *b1
;
7834 /* generate new filter code based on extracting packet
7836 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7837 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7839 b0
= new_block(JMP(BPF_JEQ
));
7843 if (vlan_num
>= 0) {
7844 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7845 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7847 b1
= new_block(JMP(BPF_JEQ
));
7849 b1
->s
.k
= (bpf_int32
) vlan_num
;
7859 static struct block
*
7860 gen_vlan_no_bpf_extensions(int vlan_num
)
7862 struct block
*b0
, *b1
;
7864 /* check for VLAN, including QinQ */
7865 b0
= gen_linktype(ETHERTYPE_8021Q
);
7866 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7870 /* If a specific VLAN is requested, check VLAN id */
7871 if (vlan_num
>= 0) {
7872 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7873 (bpf_int32
)vlan_num
, 0x0fff);
7879 * The payload follows the full header, including the
7880 * VLAN tags, so skip past this VLAN tag.
7882 off_linkpl
.constant_part
+= 4;
7885 * The link-layer type information follows the VLAN tags, so
7886 * skip past this VLAN tag.
7894 * support IEEE 802.1Q VLAN trunk over ethernet
7902 /* can't check for VLAN-encapsulated packets inside MPLS */
7903 if (label_stack_depth
> 0)
7904 bpf_error("no VLAN match after MPLS");
7907 * Check for a VLAN packet, and then change the offsets to point
7908 * to the type and data fields within the VLAN packet. Just
7909 * increment the offsets, so that we can support a hierarchy, e.g.
7910 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7913 * XXX - this is a bit of a kludge. If we were to split the
7914 * compiler into a parser that parses an expression and
7915 * generates an expression tree, and a code generator that
7916 * takes an expression tree (which could come from our
7917 * parser or from some other parser) and generates BPF code,
7918 * we could perhaps make the offsets parameters of routines
7919 * and, in the handler for an "AND" node, pass to subnodes
7920 * other than the VLAN node the adjusted offsets.
7922 * This would mean that "vlan" would, instead of changing the
7923 * behavior of *all* tests after it, change only the behavior
7924 * of tests ANDed with it. That would change the documented
7925 * semantics of "vlan", which might break some expressions.
7926 * However, it would mean that "(vlan and ip) or ip" would check
7927 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7928 * checking only for VLAN-encapsulated IP, so that could still
7929 * be considered worth doing; it wouldn't break expressions
7930 * that are of the form "vlan and ..." or "vlan N and ...",
7931 * which I suspect are the most common expressions involving
7932 * "vlan". "vlan or ..." doesn't necessarily do what the user
7933 * would really want, now, as all the "or ..." tests would
7934 * be done assuming a VLAN, even though the "or" could be viewed
7935 * as meaning "or, if this isn't a VLAN packet...".
7940 case DLT_NETANALYZER
:
7941 case DLT_NETANALYZER_TRANSPARENT
:
7942 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7943 if (vlan_stack_depth
== 0) {
7945 * Do we need special VLAN handling?
7947 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
7948 b0
= gen_vlan_bpf_extensions(vlan_num
);
7950 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7953 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7956 case DLT_IEEE802_11
:
7957 case DLT_PRISM_HEADER
:
7958 case DLT_IEEE802_11_RADIO_AVS
:
7959 case DLT_IEEE802_11_RADIO
:
7960 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7964 bpf_error("no VLAN support for data link type %d",
7981 struct block
*b0
, *b1
;
7983 if (label_stack_depth
> 0) {
7984 /* just match the bottom-of-stack bit clear */
7985 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
7988 * We're not in an MPLS stack yet, so check the link-layer
7989 * type against MPLS.
7993 case DLT_C_HDLC
: /* fall through */
7995 case DLT_NETANALYZER
:
7996 case DLT_NETANALYZER_TRANSPARENT
:
7997 b0
= gen_linktype(ETHERTYPE_MPLS
);
8001 b0
= gen_linktype(PPP_MPLS_UCAST
);
8004 /* FIXME add other DLT_s ...
8005 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8006 * leave it for now */
8009 bpf_error("no MPLS support for data link type %d",
8017 /* If a specific MPLS label is requested, check it */
8018 if (label_num
>= 0) {
8019 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8020 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8021 0xfffff000); /* only compare the first 20 bits */
8027 * Change the offsets to point to the type and data fields within
8028 * the MPLS packet. Just increment the offsets, so that we
8029 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8030 * capture packets with an outer label of 100000 and an inner
8033 * Increment the MPLS stack depth as well; this indicates that
8034 * we're checking MPLS-encapsulated headers, to make sure higher
8035 * level code generators don't try to match against IP-related
8036 * protocols such as Q_ARP, Q_RARP etc.
8038 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8042 label_stack_depth
++;
8047 * Support PPPOE discovery and session.
8052 /* check for PPPoE discovery */
8053 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8057 gen_pppoes(sess_num
)
8060 struct block
*b0
, *b1
;
8063 * Test against the PPPoE session link-layer type.
8065 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8067 /* If a specific session is requested, check PPPoE session id */
8068 if (sess_num
>= 0) {
8069 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8070 (bpf_int32
)sess_num
, 0x0000ffff);
8076 * Change the offsets to point to the type and data fields within
8077 * the PPP packet, and note that this is PPPoE rather than
8080 * XXX - this is a bit of a kludge. If we were to split the
8081 * compiler into a parser that parses an expression and
8082 * generates an expression tree, and a code generator that
8083 * takes an expression tree (which could come from our
8084 * parser or from some other parser) and generates BPF code,
8085 * we could perhaps make the offsets parameters of routines
8086 * and, in the handler for an "AND" node, pass to subnodes
8087 * other than the PPPoE node the adjusted offsets.
8089 * This would mean that "pppoes" would, instead of changing the
8090 * behavior of *all* tests after it, change only the behavior
8091 * of tests ANDed with it. That would change the documented
8092 * semantics of "pppoes", which might break some expressions.
8093 * However, it would mean that "(pppoes and ip) or ip" would check
8094 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8095 * checking only for VLAN-encapsulated IP, so that could still
8096 * be considered worth doing; it wouldn't break expressions
8097 * that are of the form "pppoes and ..." which I suspect are the
8098 * most common expressions involving "pppoes". "pppoes or ..."
8099 * doesn't necessarily do what the user would really want, now,
8100 * as all the "or ..." tests would be done assuming PPPoE, even
8101 * though the "or" could be viewed as meaning "or, if this isn't
8102 * a PPPoE packet...".
8104 * The "network-layer" protocol is PPPoE, which has a 6-byte
8105 * PPPoE header, followed by a PPP packet.
8107 * There is no HDLC encapsulation for the PPP packet (it's
8108 * encapsulated in PPPoES instead), so the link-layer type
8109 * starts at the first byte of the PPP packet. For PPPoE,
8110 * that offset is relative to the beginning of the total
8111 * link-layer payload, including any 802.2 LLC header, so
8112 * it's 6 bytes past off_nl.
8114 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8115 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8119 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8122 off_nl_nosnap
= 0; /* no 802.2 LLC */
8128 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8140 bpf_error("'vpi' supported only on raw ATM");
8141 if (off_vpi
== (u_int
)-1)
8143 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8149 bpf_error("'vci' supported only on raw ATM");
8150 if (off_vci
== (u_int
)-1)
8152 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8157 if (off_proto
== (u_int
)-1)
8158 abort(); /* XXX - this isn't on FreeBSD */
8159 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8164 if (off_payload
== (u_int
)-1)
8166 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8167 0xffffffff, jtype
, reverse
, jvalue
);
8172 bpf_error("'callref' supported only on raw ATM");
8173 if (off_proto
== (u_int
)-1)
8175 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8176 jtype
, reverse
, jvalue
);
8186 gen_atmtype_abbrev(type
)
8189 struct block
*b0
, *b1
;
8194 /* Get all packets in Meta signalling Circuit */
8196 bpf_error("'metac' supported only on raw ATM");
8197 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8198 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8203 /* Get all packets in Broadcast Circuit*/
8205 bpf_error("'bcc' supported only on raw ATM");
8206 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8207 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8212 /* Get all cells in Segment OAM F4 circuit*/
8214 bpf_error("'oam4sc' supported only on raw ATM");
8215 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8216 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8221 /* Get all cells in End-to-End OAM F4 Circuit*/
8223 bpf_error("'oam4ec' supported only on raw ATM");
8224 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8225 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8230 /* Get all packets in connection Signalling Circuit */
8232 bpf_error("'sc' supported only on raw ATM");
8233 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8234 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8239 /* Get all packets in ILMI Circuit */
8241 bpf_error("'ilmic' supported only on raw ATM");
8242 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8243 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8248 /* Get all LANE packets */
8250 bpf_error("'lane' supported only on raw ATM");
8251 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8254 * Arrange that all subsequent tests assume LANE
8255 * rather than LLC-encapsulated packets, and set
8256 * the offsets appropriately for LANE-encapsulated
8259 * We assume LANE means Ethernet, not Token Ring.
8261 PUSH_LINKHDR(DLT_EN10MB
, 0,
8262 off_payload
+ 2, /* Ethernet header */
8265 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8266 off_nl
= 0; /* Ethernet II */
8267 off_nl_nosnap
= 3; /* 802.3+802.2 */
8271 /* Get all LLC-encapsulated packets */
8273 bpf_error("'llc' supported only on raw ATM");
8274 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8275 linktype
= prevlinktype
;
8285 * Filtering for MTP2 messages based on li value
8286 * FISU, length is null
8287 * LSSU, length is 1 or 2
8288 * MSU, length is 3 or more
8289 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8292 gen_mtp2type_abbrev(type
)
8295 struct block
*b0
, *b1
;
8300 if ( (linktype
!= DLT_MTP2
) &&
8301 (linktype
!= DLT_ERF
) &&
8302 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8303 bpf_error("'fisu' supported only on MTP2");
8304 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8305 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8309 if ( (linktype
!= DLT_MTP2
) &&
8310 (linktype
!= DLT_ERF
) &&
8311 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8312 bpf_error("'lssu' supported only on MTP2");
8313 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8314 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8319 if ( (linktype
!= DLT_MTP2
) &&
8320 (linktype
!= DLT_ERF
) &&
8321 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8322 bpf_error("'msu' supported only on MTP2");
8323 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8327 if ( (linktype
!= DLT_MTP2
) &&
8328 (linktype
!= DLT_ERF
) &&
8329 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8330 bpf_error("'hfisu' supported only on MTP2_HSL");
8331 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8332 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8336 if ( (linktype
!= DLT_MTP2
) &&
8337 (linktype
!= DLT_ERF
) &&
8338 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8339 bpf_error("'hlssu' supported only on MTP2_HSL");
8340 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8341 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8346 if ( (linktype
!= DLT_MTP2
) &&
8347 (linktype
!= DLT_ERF
) &&
8348 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8349 bpf_error("'hmsu' supported only on MTP2_HSL");
8350 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8360 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8367 bpf_u_int32 val1
, val2
, val3
;
8368 u_int newoff_sio
=off_sio
;
8369 u_int newoff_opc
=off_opc
;
8370 u_int newoff_dpc
=off_dpc
;
8371 u_int newoff_sls
=off_sls
;
8373 switch (mtp3field
) {
8376 newoff_sio
+= 3; /* offset for MTP2_HSL */
8380 if (off_sio
== (u_int
)-1)
8381 bpf_error("'sio' supported only on SS7");
8382 /* sio coded on 1 byte so max value 255 */
8384 bpf_error("sio value %u too big; max value = 255",
8386 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8387 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8393 if (off_opc
== (u_int
)-1)
8394 bpf_error("'opc' supported only on SS7");
8395 /* opc coded on 14 bits so max value 16383 */
8397 bpf_error("opc value %u too big; max value = 16383",
8399 /* the following instructions are made to convert jvalue
8400 * to the form used to write opc in an ss7 message*/
8401 val1
= jvalue
& 0x00003c00;
8403 val2
= jvalue
& 0x000003fc;
8405 val3
= jvalue
& 0x00000003;
8407 jvalue
= val1
+ val2
+ val3
;
8408 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8409 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8417 if (off_dpc
== (u_int
)-1)
8418 bpf_error("'dpc' supported only on SS7");
8419 /* dpc coded on 14 bits so max value 16383 */
8421 bpf_error("dpc value %u too big; max value = 16383",
8423 /* the following instructions are made to convert jvalue
8424 * to the forme used to write dpc in an ss7 message*/
8425 val1
= jvalue
& 0x000000ff;
8427 val2
= jvalue
& 0x00003f00;
8429 jvalue
= val1
+ val2
;
8430 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8431 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8437 if (off_sls
== (u_int
)-1)
8438 bpf_error("'sls' supported only on SS7");
8439 /* sls coded on 4 bits so max value 15 */
8441 bpf_error("sls value %u too big; max value = 15",
8443 /* the following instruction is made to convert jvalue
8444 * to the forme used to write sls in an ss7 message*/
8445 jvalue
= jvalue
<< 4;
8446 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8447 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8456 static struct block
*
8457 gen_msg_abbrev(type
)
8463 * Q.2931 signalling protocol messages for handling virtual circuits
8464 * establishment and teardown
8469 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8473 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8477 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8481 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8485 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8488 case A_RELEASE_DONE
:
8489 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8499 gen_atmmulti_abbrev(type
)
8502 struct block
*b0
, *b1
;
8508 bpf_error("'oam' supported only on raw ATM");
8509 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8514 bpf_error("'oamf4' supported only on raw ATM");
8516 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8517 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8519 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8525 * Get Q.2931 signalling messages for switched
8526 * virtual connection
8529 bpf_error("'connectmsg' supported only on raw ATM");
8530 b0
= gen_msg_abbrev(A_SETUP
);
8531 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8533 b0
= gen_msg_abbrev(A_CONNECT
);
8535 b0
= gen_msg_abbrev(A_CONNECTACK
);
8537 b0
= gen_msg_abbrev(A_RELEASE
);
8539 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8541 b0
= gen_atmtype_abbrev(A_SC
);
8547 bpf_error("'metaconnect' supported only on raw ATM");
8548 b0
= gen_msg_abbrev(A_SETUP
);
8549 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8551 b0
= gen_msg_abbrev(A_CONNECT
);
8553 b0
= gen_msg_abbrev(A_RELEASE
);
8555 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8557 b0
= gen_atmtype_abbrev(A_METAC
);