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_PRISM_HEADER
:
1111 case DLT_IEEE802_11_RADIO_AVS
:
1112 case DLT_IEEE802_11_RADIO
:
1113 off_linkhdr
.is_variable
= 1;
1114 /* Fall through, 802.11 doesn't have a variable link
1115 * prefix but is otherwise the same. */
1117 case DLT_IEEE802_11
:
1119 * 802.11 doesn't really have a link-level type field.
1120 * We set "off_linktype" to the offset of the LLC header.
1122 * To check for Ethernet types, we assume that SSAP = SNAP
1123 * is being used and pick out the encapsulated Ethernet type.
1124 * XXX - should we generate code to check for SNAP?
1126 * We also handle variable-length radio headers here.
1127 * The Prism header is in theory variable-length, but in
1128 * practice it's always 144 bytes long. However, some
1129 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1130 * sometimes or always supply an AVS header, so we
1131 * have to check whether the radio header is a Prism
1132 * header or an AVS header, so, in practice, it's
1136 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1137 off_linkpl
.is_variable
= 1;
1138 off_nl
= 8; /* 802.2+SNAP */
1139 off_nl_nosnap
= 3; /* 802.2 */
1144 * At the moment we treat PPI the same way that we treat
1145 * normal Radiotap encoded packets. The difference is in
1146 * the function that generates the code at the beginning
1147 * to compute the header length. Since this code generator
1148 * of PPI supports bare 802.11 encapsulation only (i.e.
1149 * the encapsulated DLT should be DLT_IEEE802_11) we
1150 * generate code to check for this too.
1153 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1154 off_linkpl
.is_variable
= 1;
1155 off_linkhdr
.is_variable
= 1;
1156 off_nl
= 8; /* 802.2+SNAP */
1157 off_nl_nosnap
= 3; /* 802.2 */
1160 case DLT_ATM_RFC1483
:
1161 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1163 * assume routed, non-ISO PDUs
1164 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1166 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1167 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1168 * latter would presumably be treated the way PPPoE
1169 * should be, so you can do "pppoe and udp port 2049"
1170 * or "pppoa and tcp port 80" and have it check for
1171 * PPPo{A,E} and a PPP protocol of IP and....
1174 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1175 off_nl
= 8; /* 802.2+SNAP */
1176 off_nl_nosnap
= 3; /* 802.2 */
1181 * Full Frontal ATM; you get AALn PDUs with an ATM
1185 off_vpi
= SUNATM_VPI_POS
;
1186 off_vci
= SUNATM_VCI_POS
;
1187 off_proto
= PROTO_POS
;
1188 off_payload
= SUNATM_PKT_BEGIN_POS
;
1189 off_linktype
= off_payload
;
1190 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1191 off_nl
= 8; /* 802.2+SNAP */
1192 off_nl_nosnap
= 3; /* 802.2 */
1199 off_linkpl
.constant_part
= 0;
1201 off_nl_nosnap
= 0; /* no 802.2 LLC */
1204 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1206 off_linkpl
.constant_part
= 16;
1208 off_nl_nosnap
= 0; /* no 802.2 LLC */
1213 * LocalTalk does have a 1-byte type field in the LLAP header,
1214 * but really it just indicates whether there is a "short" or
1215 * "long" DDP packet following.
1218 off_linkpl
.constant_part
= 0;
1220 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 case DLT_IP_OVER_FC
:
1225 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1226 * link-level type field. We set "off_linktype" to the
1227 * offset of the LLC header.
1229 * To check for Ethernet types, we assume that SSAP = SNAP
1230 * is being used and pick out the encapsulated Ethernet type.
1231 * XXX - should we generate code to check for SNAP? RFC
1232 * 2625 says SNAP should be used.
1235 off_linkpl
.constant_part
= 16;
1236 off_nl
= 8; /* 802.2+SNAP */
1237 off_nl_nosnap
= 3; /* 802.2 */
1242 * XXX - we should set this to handle SNAP-encapsulated
1243 * frames (NLPID of 0x80).
1246 off_linkpl
.constant_part
= 0;
1248 off_nl_nosnap
= 0; /* no 802.2 LLC */
1252 * the only BPF-interesting FRF.16 frames are non-control frames;
1253 * Frame Relay has a variable length link-layer
1254 * so lets start with offset 4 for now and increments later on (FIXME);
1258 off_linkpl
.constant_part
= 0;
1260 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1263 case DLT_APPLE_IP_OVER_IEEE1394
:
1265 off_linkpl
.constant_part
= 18;
1267 off_nl_nosnap
= 0; /* no 802.2 LLC */
1270 case DLT_SYMANTEC_FIREWALL
:
1272 off_linkpl
.constant_part
= 44;
1273 off_nl
= 0; /* Ethernet II */
1274 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1277 #ifdef HAVE_NET_PFVAR_H
1280 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1282 off_nl_nosnap
= 0; /* no 802.2 LLC */
1286 case DLT_JUNIPER_MFR
:
1287 case DLT_JUNIPER_MLFR
:
1288 case DLT_JUNIPER_MLPPP
:
1289 case DLT_JUNIPER_PPP
:
1290 case DLT_JUNIPER_CHDLC
:
1291 case DLT_JUNIPER_FRELAY
:
1293 off_linkpl
.constant_part
= 4;
1295 off_nl_nosnap
= -1; /* no 802.2 LLC */
1298 case DLT_JUNIPER_ATM1
:
1299 off_linktype
= 4; /* in reality variable between 4-8 */
1300 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1305 case DLT_JUNIPER_ATM2
:
1306 off_linktype
= 8; /* in reality variable between 8-12 */
1307 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1312 /* frames captured on a Juniper PPPoE service PIC
1313 * contain raw ethernet frames */
1314 case DLT_JUNIPER_PPPOE
:
1315 case DLT_JUNIPER_ETHER
:
1316 off_linkpl
.constant_part
= 14;
1318 off_nl
= 18; /* Ethernet II */
1319 off_nl_nosnap
= 21; /* 802.3+802.2 */
1322 case DLT_JUNIPER_PPPOE_ATM
:
1324 off_linkpl
.constant_part
= 6;
1326 off_nl_nosnap
= -1; /* no 802.2 LLC */
1329 case DLT_JUNIPER_GGSN
:
1331 off_linkpl
.constant_part
= 12;
1333 off_nl_nosnap
= -1; /* no 802.2 LLC */
1336 case DLT_JUNIPER_ES
:
1338 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1339 off_nl
= -1; /* not really a network layer but raw IP addresses */
1340 off_nl_nosnap
= -1; /* no 802.2 LLC */
1343 case DLT_JUNIPER_MONITOR
:
1345 off_linkpl
.constant_part
= 12;
1346 off_nl
= 0; /* raw IP/IP6 header */
1347 off_nl_nosnap
= -1; /* no 802.2 LLC */
1350 case DLT_BACNET_MS_TP
:
1352 off_linkpl
.constant_part
= -1;
1357 case DLT_JUNIPER_SERVICES
:
1359 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1360 off_nl
= -1; /* L3 proto location dep. on cookie type */
1361 off_nl_nosnap
= -1; /* no 802.2 LLC */
1364 case DLT_JUNIPER_VP
:
1366 off_linkpl
.constant_part
= -1;
1371 case DLT_JUNIPER_ST
:
1373 off_linkpl
.constant_part
= -1;
1378 case DLT_JUNIPER_ISM
:
1380 off_linkpl
.constant_part
= -1;
1385 case DLT_JUNIPER_VS
:
1386 case DLT_JUNIPER_SRX_E2E
:
1387 case DLT_JUNIPER_FIBRECHANNEL
:
1388 case DLT_JUNIPER_ATM_CEMIC
:
1390 off_linkpl
.constant_part
= -1;
1403 off_linkpl
.constant_part
= -1;
1408 case DLT_MTP2_WITH_PHDR
:
1416 off_linkpl
.constant_part
= -1;
1429 off_linkpl
.constant_part
= -1;
1436 off_linkpl
.constant_part
= 4;
1443 * Currently, only raw "link[N:M]" filtering is supported.
1445 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1446 off_linkpl
.constant_part
= -1;
1447 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1448 off_nl_nosnap
= -1; /* no 802.2 LLC */
1453 off_linkpl
.constant_part
= 24; /* ipnet header length */
1458 case DLT_NETANALYZER
:
1459 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1461 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1462 off_nl
= 0; /* Ethernet II */
1463 off_nl_nosnap
= 3; /* 802.3+802.2 */
1466 case DLT_NETANALYZER_TRANSPARENT
:
1467 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1469 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1470 off_nl
= 0; /* Ethernet II */
1471 off_nl_nosnap
= 3; /* 802.3+802.2 */
1476 * For values in the range in which we've assigned new
1477 * DLT_ values, only raw "link[N:M]" filtering is supported.
1479 if (linktype
>= DLT_MATCHING_MIN
&&
1480 linktype
<= DLT_MATCHING_MAX
) {
1482 off_linkpl
.constant_part
= -1;
1486 bpf_error("unknown data link type %d", linktype
);
1491 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1495 * Load a value relative to the specified absolute offset.
1497 static struct slist
*
1498 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1500 struct slist
*s
, *s2
;
1502 s
= gen_abs_offset_varpart(abs_offset
);
1505 * If "s" is non-null, it has code to arrange that the X register
1506 * contains the variable part of the absolute offset, so we
1507 * generate a load relative to that, with an offset of
1508 * abs_offset->constant_part + offset.
1510 * Otherwise, we can do an absolute load with an offset of
1511 * abs_offset->constant_part + offset.
1515 * "s" points to a list of statements that puts the
1516 * variable part of the absolute offset into the X register.
1517 * Do an indirect load, to use the X register as an offset.
1519 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1520 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1524 * There is no variable part of the absolute offset, so
1525 * just do an absolute load.
1527 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1528 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1534 * Load a value relative to the beginning of the specified header.
1536 static struct slist
*
1537 gen_load_a(offrel
, offset
, size
)
1538 enum e_offrel offrel
;
1541 struct slist
*s
, *s2
;
1546 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1551 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1554 case OR_PREVLINKHDR
:
1555 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1559 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1562 case OR_PREVMPLSHDR
:
1563 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1567 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1570 case OR_LINKPL_NOSNAP
:
1571 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1576 * Load the X register with the length of the IPv4 header
1577 * (plus the offset of the link-layer header, if it's
1578 * preceded by a variable-length header such as a radio
1579 * header), in bytes.
1581 s
= gen_loadx_iphdrlen();
1584 * Load the item at {offset of the link-layer payload} +
1585 * {offset, relative to the start of the link-layer
1586 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1587 * {specified offset}.
1589 * If the offset of the link-layer payload is variable,
1590 * the variable part of that offset is included in the
1591 * value in the X register, and we include the constant
1592 * part in the offset of the load.
1594 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1595 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1600 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1611 * Generate code to load into the X register the sum of the length of
1612 * the IPv4 header and the variable part of the offset of the link-layer
1615 static struct slist
*
1616 gen_loadx_iphdrlen()
1618 struct slist
*s
, *s2
;
1620 s
= gen_abs_offset_varpart(&off_linkpl
);
1623 * The offset of the link-layer payload has a variable
1624 * part. "s" points to a list of statements that put
1625 * the variable part of that offset into the X register.
1627 * The 4*([k]&0xf) addressing mode can't be used, as we
1628 * don't have a constant offset, so we have to load the
1629 * value in question into the A register and add to it
1630 * the value from the X register.
1632 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1633 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1635 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1638 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1643 * The A register now contains the length of the IP header.
1644 * We need to add to it the variable part of the offset of
1645 * the link-layer payload, which is still in the X
1646 * register, and move the result into the X register.
1648 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1649 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1652 * The offset of the link-layer payload is a constant,
1653 * so no code was generated to load the (non-existent)
1654 * variable part of that offset.
1656 * This means we can use the 4*([k]&0xf) addressing
1657 * mode. Load the length of the IPv4 header, which
1658 * is at an offset of off_nl from the beginning of
1659 * the link-layer payload, and thus at an offset of
1660 * off_linkpl.constant_part + off_nl from the beginning
1661 * of the raw packet data, using that addressing mode.
1663 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1664 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1669 static struct block
*
1676 s
= new_stmt(BPF_LD
|BPF_IMM
);
1678 b
= new_block(JMP(BPF_JEQ
));
1684 static inline struct block
*
1687 return gen_uncond(1);
1690 static inline struct block
*
1693 return gen_uncond(0);
1697 * Byte-swap a 32-bit number.
1698 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1699 * big-endian platforms.)
1701 #define SWAPLONG(y) \
1702 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1705 * Generate code to match a particular packet type.
1707 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1708 * value, if <= ETHERMTU. We use that to determine whether to
1709 * match the type/length field or to check the type/length field for
1710 * a value <= ETHERMTU to see whether it's a type field and then do
1711 * the appropriate test.
1713 static struct block
*
1714 gen_ether_linktype(proto
)
1717 struct block
*b0
, *b1
;
1723 case LLCSAP_NETBEUI
:
1725 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1726 * so we check the DSAP and SSAP.
1728 * LLCSAP_IP checks for IP-over-802.2, rather
1729 * than IP-over-Ethernet or IP-over-SNAP.
1731 * XXX - should we check both the DSAP and the
1732 * SSAP, like this, or should we check just the
1733 * DSAP, as we do for other types <= ETHERMTU
1734 * (i.e., other SAP values)?
1736 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1738 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1739 ((proto
<< 8) | proto
));
1747 * Ethernet_II frames, which are Ethernet
1748 * frames with a frame type of ETHERTYPE_IPX;
1750 * Ethernet_802.3 frames, which are 802.3
1751 * frames (i.e., the type/length field is
1752 * a length field, <= ETHERMTU, rather than
1753 * a type field) with the first two bytes
1754 * after the Ethernet/802.3 header being
1757 * Ethernet_802.2 frames, which are 802.3
1758 * frames with an 802.2 LLC header and
1759 * with the IPX LSAP as the DSAP in the LLC
1762 * Ethernet_SNAP frames, which are 802.3
1763 * frames with an LLC header and a SNAP
1764 * header and with an OUI of 0x000000
1765 * (encapsulated Ethernet) and a protocol
1766 * ID of ETHERTYPE_IPX in the SNAP header.
1768 * XXX - should we generate the same code both
1769 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1773 * This generates code to check both for the
1774 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1776 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1777 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1781 * Now we add code to check for SNAP frames with
1782 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1784 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1788 * Now we generate code to check for 802.3
1789 * frames in general.
1791 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1795 * Now add the check for 802.3 frames before the
1796 * check for Ethernet_802.2 and Ethernet_802.3,
1797 * as those checks should only be done on 802.3
1798 * frames, not on Ethernet frames.
1803 * Now add the check for Ethernet_II frames, and
1804 * do that before checking for the other frame
1807 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1808 (bpf_int32
)ETHERTYPE_IPX
);
1812 case ETHERTYPE_ATALK
:
1813 case ETHERTYPE_AARP
:
1815 * EtherTalk (AppleTalk protocols on Ethernet link
1816 * layer) may use 802.2 encapsulation.
1820 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1821 * we check for an Ethernet type field less than
1822 * 1500, which means it's an 802.3 length field.
1824 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1828 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1829 * SNAP packets with an organization code of
1830 * 0x080007 (Apple, for Appletalk) and a protocol
1831 * type of ETHERTYPE_ATALK (Appletalk).
1833 * 802.2-encapsulated ETHERTYPE_AARP packets are
1834 * SNAP packets with an organization code of
1835 * 0x000000 (encapsulated Ethernet) and a protocol
1836 * type of ETHERTYPE_AARP (Appletalk ARP).
1838 if (proto
== ETHERTYPE_ATALK
)
1839 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1840 else /* proto == ETHERTYPE_AARP */
1841 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1845 * Check for Ethernet encapsulation (Ethertalk
1846 * phase 1?); we just check for the Ethernet
1849 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1855 if (proto
<= ETHERMTU
) {
1857 * This is an LLC SAP value, so the frames
1858 * that match would be 802.2 frames.
1859 * Check that the frame is an 802.2 frame
1860 * (i.e., that the length/type field is
1861 * a length field, <= ETHERMTU) and
1862 * then check the DSAP.
1864 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
1866 b1
= gen_cmp(OR_LINKHDR
, off_linktype
+ 2, BPF_B
,
1872 * This is an Ethernet type, so compare
1873 * the length/type field with it (if
1874 * the frame is an 802.2 frame, the length
1875 * field will be <= ETHERMTU, and, as
1876 * "proto" is > ETHERMTU, this test
1877 * will fail and the frame won't match,
1878 * which is what we want).
1880 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1887 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1888 * or IPv6 then we have an error.
1890 static struct block
*
1891 gen_ipnet_linktype(proto
)
1897 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1898 (bpf_int32
)IPH_AF_INET
);
1901 case ETHERTYPE_IPV6
:
1902 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
1903 (bpf_int32
)IPH_AF_INET6
);
1914 * Generate code to match a particular packet type.
1916 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1917 * value, if <= ETHERMTU. We use that to determine whether to
1918 * match the type field or to check the type field for the special
1919 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1921 static struct block
*
1922 gen_linux_sll_linktype(proto
)
1925 struct block
*b0
, *b1
;
1931 case LLCSAP_NETBEUI
:
1933 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1934 * so we check the DSAP and SSAP.
1936 * LLCSAP_IP checks for IP-over-802.2, rather
1937 * than IP-over-Ethernet or IP-over-SNAP.
1939 * XXX - should we check both the DSAP and the
1940 * SSAP, like this, or should we check just the
1941 * DSAP, as we do for other types <= ETHERMTU
1942 * (i.e., other SAP values)?
1944 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1945 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1946 ((proto
<< 8) | proto
));
1952 * Ethernet_II frames, which are Ethernet
1953 * frames with a frame type of ETHERTYPE_IPX;
1955 * Ethernet_802.3 frames, which have a frame
1956 * type of LINUX_SLL_P_802_3;
1958 * Ethernet_802.2 frames, which are 802.3
1959 * frames with an 802.2 LLC header (i.e, have
1960 * a frame type of LINUX_SLL_P_802_2) and
1961 * with the IPX LSAP as the DSAP in the LLC
1964 * Ethernet_SNAP frames, which are 802.3
1965 * frames with an LLC header and a SNAP
1966 * header and with an OUI of 0x000000
1967 * (encapsulated Ethernet) and a protocol
1968 * ID of ETHERTYPE_IPX in the SNAP header.
1970 * First, do the checks on LINUX_SLL_P_802_2
1971 * frames; generate the check for either
1972 * Ethernet_802.2 or Ethernet_SNAP frames, and
1973 * then put a check for LINUX_SLL_P_802_2 frames
1976 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1977 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1979 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1983 * Now check for 802.3 frames and OR that with
1984 * the previous test.
1986 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1990 * Now add the check for Ethernet_II frames, and
1991 * do that before checking for the other frame
1994 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
1995 (bpf_int32
)ETHERTYPE_IPX
);
1999 case ETHERTYPE_ATALK
:
2000 case ETHERTYPE_AARP
:
2002 * EtherTalk (AppleTalk protocols on Ethernet link
2003 * layer) may use 802.2 encapsulation.
2007 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2008 * we check for the 802.2 protocol type in the
2009 * "Ethernet type" field.
2011 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2014 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2015 * SNAP packets with an organization code of
2016 * 0x080007 (Apple, for Appletalk) and a protocol
2017 * type of ETHERTYPE_ATALK (Appletalk).
2019 * 802.2-encapsulated ETHERTYPE_AARP packets are
2020 * SNAP packets with an organization code of
2021 * 0x000000 (encapsulated Ethernet) and a protocol
2022 * type of ETHERTYPE_AARP (Appletalk ARP).
2024 if (proto
== ETHERTYPE_ATALK
)
2025 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2026 else /* proto == ETHERTYPE_AARP */
2027 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2031 * Check for Ethernet encapsulation (Ethertalk
2032 * phase 1?); we just check for the Ethernet
2035 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2041 if (proto
<= ETHERMTU
) {
2043 * This is an LLC SAP value, so the frames
2044 * that match would be 802.2 frames.
2045 * Check for the 802.2 protocol type
2046 * in the "Ethernet type" field, and
2047 * then check the DSAP.
2049 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2051 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2057 * This is an Ethernet type, so compare
2058 * the length/type field with it (if
2059 * the frame is an 802.2 frame, the length
2060 * field will be <= ETHERMTU, and, as
2061 * "proto" is > ETHERMTU, this test
2062 * will fail and the frame won't match,
2063 * which is what we want).
2065 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2071 static struct slist
*
2072 gen_load_prism_llprefixlen()
2074 struct slist
*s1
, *s2
;
2075 struct slist
*sjeq_avs_cookie
;
2076 struct slist
*sjcommon
;
2079 * This code is not compatible with the optimizer, as
2080 * we are generating jmp instructions within a normal
2081 * slist of instructions
2086 * Generate code to load the length of the radio header into
2087 * the register assigned to hold that length, if one has been
2088 * assigned. (If one hasn't been assigned, no code we've
2089 * generated uses that prefix, so we don't need to generate any
2092 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2093 * or always use the AVS header rather than the Prism header.
2094 * We load a 4-byte big-endian value at the beginning of the
2095 * raw packet data, and see whether, when masked with 0xFFFFF000,
2096 * it's equal to 0x80211000. If so, that indicates that it's
2097 * an AVS header (the masked-out bits are the version number).
2098 * Otherwise, it's a Prism header.
2100 * XXX - the Prism header is also, in theory, variable-length,
2101 * but no known software generates headers that aren't 144
2104 if (off_linkhdr
.reg
!= -1) {
2108 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2112 * AND it with 0xFFFFF000.
2114 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2115 s2
->s
.k
= 0xFFFFF000;
2119 * Compare with 0x80211000.
2121 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2122 sjeq_avs_cookie
->s
.k
= 0x80211000;
2123 sappend(s1
, sjeq_avs_cookie
);
2128 * The 4 bytes at an offset of 4 from the beginning of
2129 * the AVS header are the length of the AVS header.
2130 * That field is big-endian.
2132 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2135 sjeq_avs_cookie
->s
.jt
= s2
;
2138 * Now jump to the code to allocate a register
2139 * into which to save the header length and
2140 * store the length there. (The "jump always"
2141 * instruction needs to have the k field set;
2142 * it's added to the PC, so, as we're jumping
2143 * over a single instruction, it should be 1.)
2145 sjcommon
= new_stmt(JMP(BPF_JA
));
2147 sappend(s1
, sjcommon
);
2150 * Now for the code that handles the Prism header.
2151 * Just load the length of the Prism header (144)
2152 * into the A register. Have the test for an AVS
2153 * header branch here if we don't have an AVS header.
2155 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2158 sjeq_avs_cookie
->s
.jf
= s2
;
2161 * Now allocate a register to hold that value and store
2162 * it. The code for the AVS header will jump here after
2163 * loading the length of the AVS header.
2165 s2
= new_stmt(BPF_ST
);
2166 s2
->s
.k
= off_linkhdr
.reg
;
2168 sjcommon
->s
.jf
= s2
;
2171 * Now move it into the X register.
2173 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2181 static struct slist
*
2182 gen_load_avs_llprefixlen()
2184 struct slist
*s1
, *s2
;
2187 * Generate code to load the length of the AVS header into
2188 * the register assigned to hold that length, if one has been
2189 * assigned. (If one hasn't been assigned, no code we've
2190 * generated uses that prefix, so we don't need to generate any
2193 if (off_linkhdr
.reg
!= -1) {
2195 * The 4 bytes at an offset of 4 from the beginning of
2196 * the AVS header are the length of the AVS header.
2197 * That field is big-endian.
2199 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2203 * Now allocate a register to hold that value and store
2206 s2
= new_stmt(BPF_ST
);
2207 s2
->s
.k
= off_linkhdr
.reg
;
2211 * Now move it into the X register.
2213 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2221 static struct slist
*
2222 gen_load_radiotap_llprefixlen()
2224 struct slist
*s1
, *s2
;
2227 * Generate code to load the length of the radiotap header into
2228 * the register assigned to hold that length, if one has been
2229 * assigned. (If one hasn't been assigned, no code we've
2230 * generated uses that prefix, so we don't need to generate any
2233 if (off_linkhdr
.reg
!= -1) {
2235 * The 2 bytes at offsets of 2 and 3 from the beginning
2236 * of the radiotap header are the length of the radiotap
2237 * header; unfortunately, it's little-endian, so we have
2238 * to load it a byte at a time and construct the value.
2242 * Load the high-order byte, at an offset of 3, shift it
2243 * left a byte, and put the result in the X register.
2245 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2247 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2250 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2254 * Load the next byte, at an offset of 2, and OR the
2255 * value from the X register into it.
2257 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2260 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2264 * Now allocate a register to hold that value and store
2267 s2
= new_stmt(BPF_ST
);
2268 s2
->s
.k
= off_linkhdr
.reg
;
2272 * Now move it into the X register.
2274 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2283 * At the moment we treat PPI as normal Radiotap encoded
2284 * packets. The difference is in the function that generates
2285 * the code at the beginning to compute the header length.
2286 * Since this code generator of PPI supports bare 802.11
2287 * encapsulation only (i.e. the encapsulated DLT should be
2288 * DLT_IEEE802_11) we generate code to check for this too;
2289 * that's done in finish_parse().
2291 static struct slist
*
2292 gen_load_ppi_llprefixlen()
2294 struct slist
*s1
, *s2
;
2297 * Generate code to load the length of the radiotap header
2298 * into the register assigned to hold that length, if one has
2301 if (off_linkhdr
.reg
!= -1) {
2303 * The 2 bytes at offsets of 2 and 3 from the beginning
2304 * of the radiotap header are the length of the radiotap
2305 * header; unfortunately, it's little-endian, so we have
2306 * to load it a byte at a time and construct the value.
2310 * Load the high-order byte, at an offset of 3, shift it
2311 * left a byte, and put the result in the X register.
2313 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2315 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2318 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2322 * Load the next byte, at an offset of 2, and OR the
2323 * value from the X register into it.
2325 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2328 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2332 * Now allocate a register to hold that value and store
2335 s2
= new_stmt(BPF_ST
);
2336 s2
->s
.k
= off_linkhdr
.reg
;
2340 * Now move it into the X register.
2342 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2351 * Load a value relative to the beginning of the link-layer header after the 802.11
2352 * header, i.e. LLC_SNAP.
2353 * The link-layer header doesn't necessarily begin at the beginning
2354 * of the packet data; there might be a variable-length prefix containing
2355 * radio information.
2357 static struct slist
*
2358 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2361 struct slist
*sjset_data_frame_1
;
2362 struct slist
*sjset_data_frame_2
;
2363 struct slist
*sjset_qos
;
2364 struct slist
*sjset_radiotap_flags
;
2365 struct slist
*sjset_radiotap_tsft
;
2366 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2367 struct slist
*s_roundup
;
2369 if (off_linkpl
.reg
== -1) {
2371 * No register has been assigned to the offset of
2372 * the link-layer payload, which means nobody needs
2373 * it; don't bother computing it - just return
2374 * what we already have.
2380 * This code is not compatible with the optimizer, as
2381 * we are generating jmp instructions within a normal
2382 * slist of instructions
2387 * If "s" is non-null, it has code to arrange that the X register
2388 * contains the length of the prefix preceding the link-layer
2391 * Otherwise, the length of the prefix preceding the link-layer
2392 * header is "off_outermostlinkhdr.constant_part".
2396 * There is no variable-length header preceding the
2397 * link-layer header.
2399 * Load the length of the fixed-length prefix preceding
2400 * the link-layer header (if any) into the X register,
2401 * and store it in the off_linkpl.reg register.
2402 * That length is off_outermostlinkhdr.constant_part.
2404 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2405 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2409 * The X register contains the offset of the beginning of the
2410 * link-layer header; add 24, which is the minimum length
2411 * of the MAC header for a data frame, to that, and store it
2412 * in off_linkpl.reg, and then load the Frame Control field,
2413 * which is at the offset in the X register, with an indexed load.
2415 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2417 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2420 s2
= new_stmt(BPF_ST
);
2421 s2
->s
.k
= off_linkpl
.reg
;
2424 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2429 * Check the Frame Control field to see if this is a data frame;
2430 * a data frame has the 0x08 bit (b3) in that field set and the
2431 * 0x04 bit (b2) clear.
2433 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2434 sjset_data_frame_1
->s
.k
= 0x08;
2435 sappend(s
, sjset_data_frame_1
);
2438 * If b3 is set, test b2, otherwise go to the first statement of
2439 * the rest of the program.
2441 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2442 sjset_data_frame_2
->s
.k
= 0x04;
2443 sappend(s
, sjset_data_frame_2
);
2444 sjset_data_frame_1
->s
.jf
= snext
;
2447 * If b2 is not set, this is a data frame; test the QoS bit.
2448 * Otherwise, go to the first statement of the rest of the
2451 sjset_data_frame_2
->s
.jt
= snext
;
2452 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2453 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2454 sappend(s
, sjset_qos
);
2457 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2459 * Otherwise, go to the first statement of the rest of the
2462 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2463 s2
->s
.k
= off_linkpl
.reg
;
2465 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2468 s2
= new_stmt(BPF_ST
);
2469 s2
->s
.k
= off_linkpl
.reg
;
2473 * If we have a radiotap header, look at it to see whether
2474 * there's Atheros padding between the MAC-layer header
2477 * Note: all of the fields in the radiotap header are
2478 * little-endian, so we byte-swap all of the values
2479 * we test against, as they will be loaded as big-endian
2482 if (linktype
== DLT_IEEE802_11_RADIO
) {
2484 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2485 * in the presence flag?
2487 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2491 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2492 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2493 sappend(s
, sjset_radiotap_flags
);
2496 * If not, skip all of this.
2498 sjset_radiotap_flags
->s
.jf
= snext
;
2501 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2503 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2504 new_stmt(JMP(BPF_JSET
));
2505 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2506 sappend(s
, sjset_radiotap_tsft
);
2509 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2510 * at an offset of 16 from the beginning of the raw packet
2511 * data (8 bytes for the radiotap header and 8 bytes for
2514 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2517 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2521 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2522 sjset_tsft_datapad
->s
.k
= 0x20;
2523 sappend(s
, sjset_tsft_datapad
);
2526 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2527 * at an offset of 8 from the beginning of the raw packet
2528 * data (8 bytes for the radiotap header).
2530 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2533 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2537 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2538 sjset_notsft_datapad
->s
.k
= 0x20;
2539 sappend(s
, sjset_notsft_datapad
);
2542 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2543 * set, round the length of the 802.11 header to
2544 * a multiple of 4. Do that by adding 3 and then
2545 * dividing by and multiplying by 4, which we do by
2548 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2549 s_roundup
->s
.k
= off_linkpl
.reg
;
2550 sappend(s
, s_roundup
);
2551 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2554 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2557 s2
= new_stmt(BPF_ST
);
2558 s2
->s
.k
= off_linkpl
.reg
;
2561 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2562 sjset_tsft_datapad
->s
.jf
= snext
;
2563 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2564 sjset_notsft_datapad
->s
.jf
= snext
;
2566 sjset_qos
->s
.jf
= snext
;
2572 insert_compute_vloffsets(b
)
2577 /* There is an implicit dependency between the link
2578 * payload and link header since the payload computation
2579 * includes the variable part of the header. Therefore,
2580 * if nobody else has allocated a register for the link
2581 * header and we need it, do it now. */
2582 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2583 off_linkhdr
.reg
== -1)
2584 off_linkhdr
.reg
= alloc_reg();
2587 * For link-layer types that have a variable-length header
2588 * preceding the link-layer header, generate code to load
2589 * the offset of the link-layer header into the register
2590 * assigned to that offset, if any.
2592 * XXX - this, and the next switch statement, won't handle
2593 * encapsulation of 802.11 or 802.11+radio information in
2594 * some other protocol stack. That's significantly more
2597 switch (outermostlinktype
) {
2599 case DLT_PRISM_HEADER
:
2600 s
= gen_load_prism_llprefixlen();
2603 case DLT_IEEE802_11_RADIO_AVS
:
2604 s
= gen_load_avs_llprefixlen();
2607 case DLT_IEEE802_11_RADIO
:
2608 s
= gen_load_radiotap_llprefixlen();
2612 s
= gen_load_ppi_llprefixlen();
2621 * For link-layer types that have a variable-length link-layer
2622 * header, generate code to load the offset of the link-layer
2623 * payload into the register assigned to that offset, if any.
2625 switch (outermostlinktype
) {
2627 case DLT_IEEE802_11
:
2628 case DLT_PRISM_HEADER
:
2629 case DLT_IEEE802_11_RADIO_AVS
:
2630 case DLT_IEEE802_11_RADIO
:
2632 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2637 * If we have any offset-loading code, append all the
2638 * existing statements in the block to those statements,
2639 * and make the resulting list the list of statements
2643 sappend(s
, b
->stmts
);
2648 static struct block
*
2649 gen_ppi_dlt_check(void)
2651 struct slist
*s_load_dlt
;
2654 if (linktype
== DLT_PPI
)
2656 /* Create the statements that check for the DLT
2658 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2659 s_load_dlt
->s
.k
= 4;
2661 b
= new_block(JMP(BPF_JEQ
));
2663 b
->stmts
= s_load_dlt
;
2664 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2675 * Take an absolute offset, and:
2677 * if it has no variable part, return NULL;
2679 * if it has a variable part, generate code to load the register
2680 * containing that variable part into the X register, returning
2681 * a pointer to that code - if no register for that offset has
2682 * been allocated, allocate it first.
2684 * (The code to set that register will be generated later, but will
2685 * be placed earlier in the code sequence.)
2687 static struct slist
*
2688 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2692 if (off
->is_variable
) {
2693 if (off
->reg
== -1) {
2695 * We haven't yet assigned a register for the
2696 * variable part of the offset of the link-layer
2697 * header; allocate one.
2699 off
->reg
= alloc_reg();
2703 * Load the register containing the variable part of the
2704 * offset of the link-layer header into the X register.
2706 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2711 * That offset isn't variable, there's no variable part,
2712 * so we don't need to generate any code.
2719 * Map an Ethernet type to the equivalent PPP type.
2722 ethertype_to_ppptype(proto
)
2731 case ETHERTYPE_IPV6
:
2739 case ETHERTYPE_ATALK
:
2753 * I'm assuming the "Bridging PDU"s that go
2754 * over PPP are Spanning Tree Protocol
2768 * Generate any tests that, for encapsulation of a link-layer packet
2769 * inside another protocol stack, need to be done to check for those
2770 * link-layer packets (and that haven't already been done by a check
2771 * for that encapsulation).
2773 static struct block
*
2774 gen_prevlinkhdr_check(void)
2778 switch (prevlinktype
) {
2782 * This is LANE-encapsulated Ethernet; check that the LANE
2783 * packet doesn't begin with an LE Control marker, i.e.
2784 * that it's data, not a control message.
2786 * (We've already generated a test for LANE.)
2788 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2794 * No such tests are necessary.
2802 * Generate code to match a particular packet type by matching the
2803 * link-layer type field or fields in the 802.2 LLC header.
2805 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2806 * value, if <= ETHERMTU.
2808 static struct block
*
2812 struct block
*b0
, *b1
, *b2
;
2813 const char *description
;
2815 /* are we checking MPLS-encapsulated packets? */
2816 if (label_stack_depth
> 0) {
2820 /* FIXME add other L3 proto IDs */
2821 return gen_mpls_linktype(Q_IP
);
2823 case ETHERTYPE_IPV6
:
2825 /* FIXME add other L3 proto IDs */
2826 return gen_mpls_linktype(Q_IPV6
);
2829 bpf_error("unsupported protocol over mpls");
2837 case DLT_NETANALYZER
:
2838 case DLT_NETANALYZER_TRANSPARENT
:
2839 b0
= gen_prevlinkhdr_check();
2840 b1
= gen_ether_linktype(proto
);
2851 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2855 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
2862 case DLT_IEEE802_11
:
2863 case DLT_PRISM_HEADER
:
2864 case DLT_IEEE802_11_RADIO_AVS
:
2865 case DLT_IEEE802_11_RADIO
:
2868 * Check that we have a data frame.
2870 b0
= gen_check_802_11_data_frame();
2873 * Now check for the specified link-layer type.
2875 b1
= gen_llc_linktype(proto
);
2883 * XXX - check for LLC frames.
2885 return gen_llc_linktype(proto
);
2891 * XXX - check for LLC PDUs, as per IEEE 802.5.
2893 return gen_llc_linktype(proto
);
2897 case DLT_ATM_RFC1483
:
2899 case DLT_IP_OVER_FC
:
2900 return gen_llc_linktype(proto
);
2906 * Check for an LLC-encapsulated version of this protocol;
2907 * if we were checking for LANE, linktype would no longer
2910 * Check for LLC encapsulation and then check the protocol.
2912 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2913 b1
= gen_llc_linktype(proto
);
2920 return gen_linux_sll_linktype(proto
);
2925 case DLT_SLIP_BSDOS
:
2928 * These types don't provide any type field; packets
2929 * are always IPv4 or IPv6.
2931 * XXX - for IPv4, check for a version number of 4, and,
2932 * for IPv6, check for a version number of 6?
2937 /* Check for a version number of 4. */
2938 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
2940 case ETHERTYPE_IPV6
:
2941 /* Check for a version number of 6. */
2942 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
2945 return gen_false(); /* always false */
2952 * Raw IPv4, so no type field.
2954 if (proto
== ETHERTYPE_IP
)
2955 return gen_true(); /* always true */
2957 /* Checking for something other than IPv4; always false */
2964 * Raw IPv6, so no type field.
2966 if (proto
== ETHERTYPE_IPV6
)
2967 return gen_true(); /* always true */
2969 /* Checking for something other than IPv6; always false */
2976 case DLT_PPP_SERIAL
:
2979 * We use Ethernet protocol types inside libpcap;
2980 * map them to the corresponding PPP protocol types.
2982 proto
= ethertype_to_ppptype(proto
);
2983 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2989 * We use Ethernet protocol types inside libpcap;
2990 * map them to the corresponding PPP protocol types.
2996 * Also check for Van Jacobson-compressed IP.
2997 * XXX - do this for other forms of PPP?
2999 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_IP
);
3000 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJC
);
3002 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, PPP_VJNC
);
3007 proto
= ethertype_to_ppptype(proto
);
3008 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
,
3018 * For DLT_NULL, the link-layer header is a 32-bit
3019 * word containing an AF_ value in *host* byte order,
3020 * and for DLT_ENC, the link-layer header begins
3021 * with a 32-bit work containing an AF_ value in
3024 * In addition, if we're reading a saved capture file,
3025 * the host byte order in the capture may not be the
3026 * same as the host byte order on this machine.
3028 * For DLT_LOOP, the link-layer header is a 32-bit
3029 * word containing an AF_ value in *network* byte order.
3031 * XXX - AF_ values may, unfortunately, be platform-
3032 * dependent; for example, FreeBSD's AF_INET6 is 24
3033 * whilst NetBSD's and OpenBSD's is 26.
3035 * This means that, when reading a capture file, just
3036 * checking for our AF_INET6 value won't work if the
3037 * capture file came from another OS.
3046 case ETHERTYPE_IPV6
:
3053 * Not a type on which we support filtering.
3054 * XXX - support those that have AF_ values
3055 * #defined on this platform, at least?
3060 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3062 * The AF_ value is in host byte order, but
3063 * the BPF interpreter will convert it to
3064 * network byte order.
3066 * If this is a save file, and it's from a
3067 * machine with the opposite byte order to
3068 * ours, we byte-swap the AF_ value.
3070 * Then we run it through "htonl()", and
3071 * generate code to compare against the result.
3073 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3074 proto
= SWAPLONG(proto
);
3075 proto
= htonl(proto
);
3077 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3079 #ifdef HAVE_NET_PFVAR_H
3082 * af field is host byte order in contrast to the rest of
3085 if (proto
== ETHERTYPE_IP
)
3086 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3087 BPF_B
, (bpf_int32
)AF_INET
));
3088 else if (proto
== ETHERTYPE_IPV6
)
3089 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3090 BPF_B
, (bpf_int32
)AF_INET6
));
3095 #endif /* HAVE_NET_PFVAR_H */
3098 case DLT_ARCNET_LINUX
:
3100 * XXX should we check for first fragment if the protocol
3108 case ETHERTYPE_IPV6
:
3109 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3110 (bpf_int32
)ARCTYPE_INET6
));
3113 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3114 (bpf_int32
)ARCTYPE_IP
);
3115 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3116 (bpf_int32
)ARCTYPE_IP_OLD
);
3121 b0
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3122 (bpf_int32
)ARCTYPE_ARP
);
3123 b1
= gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3124 (bpf_int32
)ARCTYPE_ARP_OLD
);
3128 case ETHERTYPE_REVARP
:
3129 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3130 (bpf_int32
)ARCTYPE_REVARP
));
3132 case ETHERTYPE_ATALK
:
3133 return (gen_cmp(OR_LINKHDR
, off_linktype
, BPF_B
,
3134 (bpf_int32
)ARCTYPE_ATALK
));
3141 case ETHERTYPE_ATALK
:
3151 * XXX - assumes a 2-byte Frame Relay header with
3152 * DLCI and flags. What if the address is longer?
3158 * Check for the special NLPID for IP.
3160 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3162 case ETHERTYPE_IPV6
:
3164 * Check for the special NLPID for IPv6.
3166 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3170 * Check for several OSI protocols.
3172 * Frame Relay packets typically have an OSI
3173 * NLPID at the beginning; we check for each
3176 * What we check for is the NLPID and a frame
3177 * control field of UI, i.e. 0x03 followed
3180 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3181 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3182 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3194 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3196 case DLT_JUNIPER_MFR
:
3197 case DLT_JUNIPER_MLFR
:
3198 case DLT_JUNIPER_MLPPP
:
3199 case DLT_JUNIPER_ATM1
:
3200 case DLT_JUNIPER_ATM2
:
3201 case DLT_JUNIPER_PPPOE
:
3202 case DLT_JUNIPER_PPPOE_ATM
:
3203 case DLT_JUNIPER_GGSN
:
3204 case DLT_JUNIPER_ES
:
3205 case DLT_JUNIPER_MONITOR
:
3206 case DLT_JUNIPER_SERVICES
:
3207 case DLT_JUNIPER_ETHER
:
3208 case DLT_JUNIPER_PPP
:
3209 case DLT_JUNIPER_FRELAY
:
3210 case DLT_JUNIPER_CHDLC
:
3211 case DLT_JUNIPER_VP
:
3212 case DLT_JUNIPER_ST
:
3213 case DLT_JUNIPER_ISM
:
3214 case DLT_JUNIPER_VS
:
3215 case DLT_JUNIPER_SRX_E2E
:
3216 case DLT_JUNIPER_FIBRECHANNEL
:
3217 case DLT_JUNIPER_ATM_CEMIC
:
3219 /* just lets verify the magic number for now -
3220 * on ATM we may have up to 6 different encapsulations on the wire
3221 * and need a lot of heuristics to figure out that the payload
3224 * FIXME encapsulation specific BPF_ filters
3226 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3228 case DLT_BACNET_MS_TP
:
3229 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3232 return gen_ipnet_linktype(proto
);
3234 case DLT_LINUX_IRDA
:
3235 bpf_error("IrDA link-layer type filtering not implemented");
3238 bpf_error("DOCSIS link-layer type filtering not implemented");
3241 case DLT_MTP2_WITH_PHDR
:
3242 bpf_error("MTP2 link-layer type filtering not implemented");
3245 bpf_error("ERF link-layer type filtering not implemented");
3248 bpf_error("PFSYNC link-layer type filtering not implemented");
3250 case DLT_LINUX_LAPD
:
3251 bpf_error("LAPD link-layer type filtering not implemented");
3255 case DLT_USB_LINUX_MMAPPED
:
3256 bpf_error("USB link-layer type filtering not implemented");
3258 case DLT_BLUETOOTH_HCI_H4
:
3259 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3260 bpf_error("Bluetooth link-layer type filtering not implemented");
3263 case DLT_CAN_SOCKETCAN
:
3264 bpf_error("CAN link-layer type filtering not implemented");
3266 case DLT_IEEE802_15_4
:
3267 case DLT_IEEE802_15_4_LINUX
:
3268 case DLT_IEEE802_15_4_NONASK_PHY
:
3269 case DLT_IEEE802_15_4_NOFCS
:
3270 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3272 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3273 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3276 bpf_error("SITA link-layer type filtering not implemented");
3279 bpf_error("RAIF1 link-layer type filtering not implemented");
3282 bpf_error("IPMB link-layer type filtering not implemented");
3285 bpf_error("AX.25 link-layer type filtering not implemented");
3288 /* Using the fixed-size NFLOG header it is possible to tell only
3289 * the address family of the packet, other meaningful data is
3290 * either missing or behind TLVs.
3292 bpf_error("NFLOG link-layer type filtering not implemented");
3296 * Does this link-layer header type have a field
3297 * indicating the type of the next protocol? If
3298 * so, off_linktype will be the offset of that
3299 * field in the packet; if not, it will be -1.
3301 if (off_linktype
!= (u_int
)-1) {
3303 * Yes; assume it's an Ethernet type. (If
3304 * it's not, it needs to be handled specially
3307 return gen_cmp(OR_LINKHDR
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3310 * No; report an error.
3312 description
= pcap_datalink_val_to_description(linktype
);
3313 if (description
!= NULL
) {
3314 bpf_error("%s link-layer type filtering not implemented",
3317 bpf_error("DLT %u link-layer type filtering not implemented",
3326 * Check for an LLC SNAP packet with a given organization code and
3327 * protocol type; we check the entire contents of the 802.2 LLC and
3328 * snap headers, checking for DSAP and SSAP of SNAP and a control
3329 * field of 0x03 in the LLC header, and for the specified organization
3330 * code and protocol type in the SNAP header.
3332 static struct block
*
3333 gen_snap(orgcode
, ptype
)
3334 bpf_u_int32 orgcode
;
3337 u_char snapblock
[8];
3339 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3340 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3341 snapblock
[2] = 0x03; /* control = UI */
3342 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3343 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3344 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3345 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3346 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3347 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3351 * Generate code to match frames with an LLC header.
3356 struct block
*b0
, *b1
;
3362 * We check for an Ethernet type field less than
3363 * 1500, which means it's an 802.3 length field.
3365 b0
= gen_cmp_gt(OR_LINKHDR
, off_linktype
, BPF_H
, ETHERMTU
);
3369 * Now check for the purported DSAP and SSAP not being
3370 * 0xFF, to rule out NetWare-over-802.3.
3372 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3379 * We check for LLC traffic.
3381 b0
= gen_atmtype_abbrev(A_LLC
);
3384 case DLT_IEEE802
: /* Token Ring */
3386 * XXX - check for LLC frames.
3392 * XXX - check for LLC frames.
3396 case DLT_ATM_RFC1483
:
3398 * For LLC encapsulation, these are defined to have an
3401 * For VC encapsulation, they don't, but there's no
3402 * way to check for that; the protocol used on the VC
3403 * is negotiated out of band.
3407 case DLT_IEEE802_11
:
3408 case DLT_PRISM_HEADER
:
3409 case DLT_IEEE802_11_RADIO
:
3410 case DLT_IEEE802_11_RADIO_AVS
:
3413 * Check that we have a data frame.
3415 b0
= gen_check_802_11_data_frame();
3419 bpf_error("'llc' not supported for linktype %d", linktype
);
3427 struct block
*b0
, *b1
;
3431 * Check whether this is an LLC frame.
3436 * Load the control byte and test the low-order bit; it must
3437 * be clear for I frames.
3439 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3440 b1
= new_block(JMP(BPF_JSET
));
3451 struct block
*b0
, *b1
;
3454 * Check whether this is an LLC frame.
3459 * Now compare the low-order 2 bit of the control byte against
3460 * the appropriate value for S frames.
3462 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3470 struct block
*b0
, *b1
;
3473 * Check whether this is an LLC frame.
3478 * Now compare the low-order 2 bit of the control byte against
3479 * the appropriate value for U frames.
3481 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3487 gen_llc_s_subtype(bpf_u_int32 subtype
)
3489 struct block
*b0
, *b1
;
3492 * Check whether this is an LLC frame.
3497 * Now check for an S frame with the appropriate type.
3499 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3505 gen_llc_u_subtype(bpf_u_int32 subtype
)
3507 struct block
*b0
, *b1
;
3510 * Check whether this is an LLC frame.
3515 * Now check for a U frame with the appropriate type.
3517 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3523 * Generate code to match a particular packet type, for link-layer types
3524 * using 802.2 LLC headers.
3526 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3527 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3529 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3530 * value, if <= ETHERMTU. We use that to determine whether to
3531 * match the DSAP or both DSAP and LSAP or to check the OUI and
3532 * protocol ID in a SNAP header.
3534 static struct block
*
3535 gen_llc_linktype(proto
)
3539 * XXX - handle token-ring variable-length header.
3545 case LLCSAP_NETBEUI
:
3547 * XXX - should we check both the DSAP and the
3548 * SSAP, like this, or should we check just the
3549 * DSAP, as we do for other SAP values?
3551 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3552 ((proto
<< 8) | proto
));
3556 * XXX - are there ever SNAP frames for IPX on
3557 * non-Ethernet 802.x networks?
3559 return gen_cmp(OR_LLC
, 0, BPF_B
,
3560 (bpf_int32
)LLCSAP_IPX
);
3562 case ETHERTYPE_ATALK
:
3564 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3565 * SNAP packets with an organization code of
3566 * 0x080007 (Apple, for Appletalk) and a protocol
3567 * type of ETHERTYPE_ATALK (Appletalk).
3569 * XXX - check for an organization code of
3570 * encapsulated Ethernet as well?
3572 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3576 * XXX - we don't have to check for IPX 802.3
3577 * here, but should we check for the IPX Ethertype?
3579 if (proto
<= ETHERMTU
) {
3581 * This is an LLC SAP value, so check
3584 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3587 * This is an Ethernet type; we assume that it's
3588 * unlikely that it'll appear in the right place
3589 * at random, and therefore check only the
3590 * location that would hold the Ethernet type
3591 * in a SNAP frame with an organization code of
3592 * 0x000000 (encapsulated Ethernet).
3594 * XXX - if we were to check for the SNAP DSAP and
3595 * LSAP, as per XXX, and were also to check for an
3596 * organization code of 0x000000 (encapsulated
3597 * Ethernet), we'd do
3599 * return gen_snap(0x000000, proto);
3601 * here; for now, we don't, as per the above.
3602 * I don't know whether it's worth the extra CPU
3603 * time to do the right check or not.
3605 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3610 static struct block
*
3611 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3615 u_int src_off
, dst_off
;
3617 struct block
*b0
, *b1
;
3631 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3632 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3638 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3639 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3646 b0
= gen_linktype(proto
);
3647 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3653 static struct block
*
3654 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3655 struct in6_addr
*addr
;
3656 struct in6_addr
*mask
;
3658 u_int src_off
, dst_off
;
3660 struct block
*b0
, *b1
;
3675 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3676 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3682 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3683 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3690 /* this order is important */
3691 a
= (u_int32_t
*)addr
;
3692 m
= (u_int32_t
*)mask
;
3693 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3694 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3696 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3698 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3700 b0
= gen_linktype(proto
);
3706 static struct block
*
3707 gen_ehostop(eaddr
, dir
)
3708 register const u_char
*eaddr
;
3711 register struct block
*b0
, *b1
;
3715 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3718 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3721 b0
= gen_ehostop(eaddr
, Q_SRC
);
3722 b1
= gen_ehostop(eaddr
, Q_DST
);
3728 b0
= gen_ehostop(eaddr
, Q_SRC
);
3729 b1
= gen_ehostop(eaddr
, Q_DST
);
3734 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3738 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3742 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3746 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3750 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3754 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3762 * Like gen_ehostop, but for DLT_FDDI
3764 static struct block
*
3765 gen_fhostop(eaddr
, dir
)
3766 register const u_char
*eaddr
;
3769 struct block
*b0
, *b1
;
3773 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3776 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3779 b0
= gen_fhostop(eaddr
, Q_SRC
);
3780 b1
= gen_fhostop(eaddr
, Q_DST
);
3786 b0
= gen_fhostop(eaddr
, Q_SRC
);
3787 b1
= gen_fhostop(eaddr
, Q_DST
);
3792 bpf_error("'addr1' is only supported on 802.11");
3796 bpf_error("'addr2' is only supported on 802.11");
3800 bpf_error("'addr3' is only supported on 802.11");
3804 bpf_error("'addr4' is only supported on 802.11");
3808 bpf_error("'ra' is only supported on 802.11");
3812 bpf_error("'ta' is only supported on 802.11");
3820 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3822 static struct block
*
3823 gen_thostop(eaddr
, dir
)
3824 register const u_char
*eaddr
;
3827 register struct block
*b0
, *b1
;
3831 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3834 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3837 b0
= gen_thostop(eaddr
, Q_SRC
);
3838 b1
= gen_thostop(eaddr
, Q_DST
);
3844 b0
= gen_thostop(eaddr
, Q_SRC
);
3845 b1
= gen_thostop(eaddr
, Q_DST
);
3850 bpf_error("'addr1' is only supported on 802.11");
3854 bpf_error("'addr2' is only supported on 802.11");
3858 bpf_error("'addr3' is only supported on 802.11");
3862 bpf_error("'addr4' is only supported on 802.11");
3866 bpf_error("'ra' is only supported on 802.11");
3870 bpf_error("'ta' is only supported on 802.11");
3878 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3879 * various 802.11 + radio headers.
3881 static struct block
*
3882 gen_wlanhostop(eaddr
, dir
)
3883 register const u_char
*eaddr
;
3886 register struct block
*b0
, *b1
, *b2
;
3887 register struct slist
*s
;
3889 #ifdef ENABLE_WLAN_FILTERING_PATCH
3892 * We need to disable the optimizer because the optimizer is buggy
3893 * and wipes out some LD instructions generated by the below
3894 * code to validate the Frame Control bits
3897 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3904 * For control frames, there is no SA.
3906 * For management frames, SA is at an
3907 * offset of 10 from the beginning of
3910 * For data frames, SA is at an offset
3911 * of 10 from the beginning of the packet
3912 * if From DS is clear, at an offset of
3913 * 16 from the beginning of the packet
3914 * if From DS is set and To DS is clear,
3915 * and an offset of 24 from the beginning
3916 * of the packet if From DS is set and To DS
3921 * Generate the tests to be done for data frames
3924 * First, check for To DS set, i.e. check "link[1] & 0x01".
3926 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3927 b1
= new_block(JMP(BPF_JSET
));
3928 b1
->s
.k
= 0x01; /* To DS */
3932 * If To DS is set, the SA is at 24.
3934 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
3938 * Now, check for To DS not set, i.e. check
3939 * "!(link[1] & 0x01)".
3941 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3942 b2
= new_block(JMP(BPF_JSET
));
3943 b2
->s
.k
= 0x01; /* To DS */
3948 * If To DS is not set, the SA is at 16.
3950 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
3954 * Now OR together the last two checks. That gives
3955 * the complete set of checks for data frames with
3961 * Now check for From DS being set, and AND that with
3962 * the ORed-together checks.
3964 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3965 b1
= new_block(JMP(BPF_JSET
));
3966 b1
->s
.k
= 0x02; /* From DS */
3971 * Now check for data frames with From DS not set.
3973 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3974 b2
= new_block(JMP(BPF_JSET
));
3975 b2
->s
.k
= 0x02; /* From DS */
3980 * If From DS isn't set, the SA is at 10.
3982 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
3986 * Now OR together the checks for data frames with
3987 * From DS not set and for data frames with From DS
3988 * set; that gives the checks done for data frames.
3993 * Now check for a data frame.
3994 * I.e, check "link[0] & 0x08".
3996 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
3997 b1
= new_block(JMP(BPF_JSET
));
4002 * AND that with the checks done for data frames.
4007 * If the high-order bit of the type value is 0, this
4008 * is a management frame.
4009 * I.e, check "!(link[0] & 0x08)".
4011 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4012 b2
= new_block(JMP(BPF_JSET
));
4018 * For management frames, the SA is at 10.
4020 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4024 * OR that with the checks done for data frames.
4025 * That gives the checks done for management and
4031 * If the low-order bit of the type value is 1,
4032 * this is either a control frame or a frame
4033 * with a reserved type, and thus not a
4036 * I.e., check "!(link[0] & 0x04)".
4038 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4039 b1
= new_block(JMP(BPF_JSET
));
4045 * AND that with the checks for data and management
4055 * For control frames, there is no DA.
4057 * For management frames, DA is at an
4058 * offset of 4 from the beginning of
4061 * For data frames, DA is at an offset
4062 * of 4 from the beginning of the packet
4063 * if To DS is clear and at an offset of
4064 * 16 from the beginning of the packet
4069 * Generate the tests to be done for data frames.
4071 * First, check for To DS set, i.e. "link[1] & 0x01".
4073 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4074 b1
= new_block(JMP(BPF_JSET
));
4075 b1
->s
.k
= 0x01; /* To DS */
4079 * If To DS is set, the DA is at 16.
4081 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4085 * Now, check for To DS not set, i.e. check
4086 * "!(link[1] & 0x01)".
4088 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4089 b2
= new_block(JMP(BPF_JSET
));
4090 b2
->s
.k
= 0x01; /* To DS */
4095 * If To DS is not set, the DA is at 4.
4097 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4101 * Now OR together the last two checks. That gives
4102 * the complete set of checks for data frames.
4107 * Now check for a data frame.
4108 * I.e, check "link[0] & 0x08".
4110 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4111 b1
= new_block(JMP(BPF_JSET
));
4116 * AND that with the checks done for data frames.
4121 * If the high-order bit of the type value is 0, this
4122 * is a management frame.
4123 * I.e, check "!(link[0] & 0x08)".
4125 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4126 b2
= new_block(JMP(BPF_JSET
));
4132 * For management frames, the DA is at 4.
4134 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4138 * OR that with the checks done for data frames.
4139 * That gives the checks done for management and
4145 * If the low-order bit of the type value is 1,
4146 * this is either a control frame or a frame
4147 * with a reserved type, and thus not a
4150 * I.e., check "!(link[0] & 0x04)".
4152 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4153 b1
= new_block(JMP(BPF_JSET
));
4159 * AND that with the checks for data and management
4167 * Not present in management frames; addr1 in other
4172 * If the high-order bit of the type value is 0, this
4173 * is a management frame.
4174 * I.e, check "(link[0] & 0x08)".
4176 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4177 b1
= new_block(JMP(BPF_JSET
));
4184 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4187 * AND that with the check of addr1.
4194 * Not present in management frames; addr2, if present,
4199 * Not present in CTS or ACK control frames.
4201 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4202 IEEE80211_FC0_TYPE_MASK
);
4204 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4205 IEEE80211_FC0_SUBTYPE_MASK
);
4207 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4208 IEEE80211_FC0_SUBTYPE_MASK
);
4214 * If the high-order bit of the type value is 0, this
4215 * is a management frame.
4216 * I.e, check "(link[0] & 0x08)".
4218 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4219 b1
= new_block(JMP(BPF_JSET
));
4224 * AND that with the check for frames other than
4225 * CTS and ACK frames.
4232 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4237 * XXX - add BSSID keyword?
4240 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4244 * Not present in CTS or ACK control frames.
4246 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4247 IEEE80211_FC0_TYPE_MASK
);
4249 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4250 IEEE80211_FC0_SUBTYPE_MASK
);
4252 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4253 IEEE80211_FC0_SUBTYPE_MASK
);
4257 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4263 * Not present in control frames.
4265 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4266 IEEE80211_FC0_TYPE_MASK
);
4268 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4274 * Present only if the direction mask has both "From DS"
4275 * and "To DS" set. Neither control frames nor management
4276 * frames should have both of those set, so we don't
4277 * check the frame type.
4279 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4280 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4281 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4286 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4287 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4293 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4294 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4303 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4304 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4305 * as the RFC states.)
4307 static struct block
*
4308 gen_ipfchostop(eaddr
, dir
)
4309 register const u_char
*eaddr
;
4312 register struct block
*b0
, *b1
;
4316 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4319 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4322 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4323 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4329 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4330 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4335 bpf_error("'addr1' is only supported on 802.11");
4339 bpf_error("'addr2' is only supported on 802.11");
4343 bpf_error("'addr3' is only supported on 802.11");
4347 bpf_error("'addr4' is only supported on 802.11");
4351 bpf_error("'ra' is only supported on 802.11");
4355 bpf_error("'ta' is only supported on 802.11");
4363 * This is quite tricky because there may be pad bytes in front of the
4364 * DECNET header, and then there are two possible data packet formats that
4365 * carry both src and dst addresses, plus 5 packet types in a format that
4366 * carries only the src node, plus 2 types that use a different format and
4367 * also carry just the src node.
4371 * Instead of doing those all right, we just look for data packets with
4372 * 0 or 1 bytes of padding. If you want to look at other packets, that
4373 * will require a lot more hacking.
4375 * To add support for filtering on DECNET "areas" (network numbers)
4376 * one would want to add a "mask" argument to this routine. That would
4377 * make the filter even more inefficient, although one could be clever
4378 * and not generate masking instructions if the mask is 0xFFFF.
4380 static struct block
*
4381 gen_dnhostop(addr
, dir
)
4385 struct block
*b0
, *b1
, *b2
, *tmp
;
4386 u_int offset_lh
; /* offset if long header is received */
4387 u_int offset_sh
; /* offset if short header is received */
4392 offset_sh
= 1; /* follows flags */
4393 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4397 offset_sh
= 3; /* follows flags, dstnode */
4398 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4402 /* Inefficient because we do our Calvinball dance twice */
4403 b0
= gen_dnhostop(addr
, Q_SRC
);
4404 b1
= gen_dnhostop(addr
, Q_DST
);
4410 /* Inefficient because we do our Calvinball dance twice */
4411 b0
= gen_dnhostop(addr
, Q_SRC
);
4412 b1
= gen_dnhostop(addr
, Q_DST
);
4417 bpf_error("ISO host filtering not implemented");
4422 b0
= gen_linktype(ETHERTYPE_DN
);
4423 /* Check for pad = 1, long header case */
4424 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4425 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4426 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4427 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4429 /* Check for pad = 0, long header case */
4430 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4431 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4434 /* Check for pad = 1, short header case */
4435 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4436 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4437 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4440 /* Check for pad = 0, short header case */
4441 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4442 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4446 /* Combine with test for linktype */
4452 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4453 * test the bottom-of-stack bit, and then check the version number
4454 * field in the IP header.
4456 static struct block
*
4457 gen_mpls_linktype(proto
)
4460 struct block
*b0
, *b1
;
4465 /* match the bottom-of-stack bit */
4466 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4467 /* match the IPv4 version number */
4468 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4473 /* match the bottom-of-stack bit */
4474 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4475 /* match the IPv4 version number */
4476 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4485 static struct block
*
4486 gen_host(addr
, mask
, proto
, dir
, type
)
4493 struct block
*b0
, *b1
;
4494 const char *typestr
;
4504 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4506 * Only check for non-IPv4 addresses if we're not
4507 * checking MPLS-encapsulated packets.
4509 if (label_stack_depth
== 0) {
4510 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4512 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4518 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4521 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4524 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4527 bpf_error("'tcp' modifier applied to %s", typestr
);
4530 bpf_error("'sctp' modifier applied to %s", typestr
);
4533 bpf_error("'udp' modifier applied to %s", typestr
);
4536 bpf_error("'icmp' modifier applied to %s", typestr
);
4539 bpf_error("'igmp' modifier applied to %s", typestr
);
4542 bpf_error("'igrp' modifier applied to %s", typestr
);
4545 bpf_error("'pim' modifier applied to %s", typestr
);
4548 bpf_error("'vrrp' modifier applied to %s", typestr
);
4551 bpf_error("'carp' modifier applied to %s", typestr
);
4554 bpf_error("ATALK host filtering not implemented");
4557 bpf_error("AARP host filtering not implemented");
4560 return gen_dnhostop(addr
, dir
);
4563 bpf_error("SCA host filtering not implemented");
4566 bpf_error("LAT host filtering not implemented");
4569 bpf_error("MOPDL host filtering not implemented");
4572 bpf_error("MOPRC host filtering not implemented");
4575 bpf_error("'ip6' modifier applied to ip host");
4578 bpf_error("'icmp6' modifier applied to %s", typestr
);
4581 bpf_error("'ah' modifier applied to %s", typestr
);
4584 bpf_error("'esp' modifier applied to %s", typestr
);
4587 bpf_error("ISO host filtering not implemented");
4590 bpf_error("'esis' modifier applied to %s", typestr
);
4593 bpf_error("'isis' modifier applied to %s", typestr
);
4596 bpf_error("'clnp' modifier applied to %s", typestr
);
4599 bpf_error("'stp' modifier applied to %s", typestr
);
4602 bpf_error("IPX host filtering not implemented");
4605 bpf_error("'netbeui' modifier applied to %s", typestr
);
4608 bpf_error("'radio' modifier applied to %s", typestr
);
4617 static struct block
*
4618 gen_host6(addr
, mask
, proto
, dir
, type
)
4619 struct in6_addr
*addr
;
4620 struct in6_addr
*mask
;
4625 const char *typestr
;
4635 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4638 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4641 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4644 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4647 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4650 bpf_error("'sctp' modifier applied to %s", typestr
);
4653 bpf_error("'tcp' modifier applied to %s", typestr
);
4656 bpf_error("'udp' modifier applied to %s", typestr
);
4659 bpf_error("'icmp' modifier applied to %s", typestr
);
4662 bpf_error("'igmp' modifier applied to %s", typestr
);
4665 bpf_error("'igrp' modifier applied to %s", typestr
);
4668 bpf_error("'pim' modifier applied to %s", typestr
);
4671 bpf_error("'vrrp' modifier applied to %s", typestr
);
4674 bpf_error("'carp' modifier applied to %s", typestr
);
4677 bpf_error("ATALK host filtering not implemented");
4680 bpf_error("AARP host filtering not implemented");
4683 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4686 bpf_error("SCA host filtering not implemented");
4689 bpf_error("LAT host filtering not implemented");
4692 bpf_error("MOPDL host filtering not implemented");
4695 bpf_error("MOPRC host filtering not implemented");
4698 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4701 bpf_error("'icmp6' modifier applied to %s", typestr
);
4704 bpf_error("'ah' modifier applied to %s", typestr
);
4707 bpf_error("'esp' modifier applied to %s", typestr
);
4710 bpf_error("ISO host filtering not implemented");
4713 bpf_error("'esis' modifier applied to %s", typestr
);
4716 bpf_error("'isis' modifier applied to %s", typestr
);
4719 bpf_error("'clnp' modifier applied to %s", typestr
);
4722 bpf_error("'stp' modifier applied to %s", typestr
);
4725 bpf_error("IPX host filtering not implemented");
4728 bpf_error("'netbeui' modifier applied to %s", typestr
);
4731 bpf_error("'radio' modifier applied to %s", typestr
);
4741 static struct block
*
4742 gen_gateway(eaddr
, alist
, proto
, dir
)
4743 const u_char
*eaddr
;
4744 bpf_u_int32
**alist
;
4748 struct block
*b0
, *b1
, *tmp
;
4751 bpf_error("direction applied to 'gateway'");
4760 case DLT_NETANALYZER
:
4761 case DLT_NETANALYZER_TRANSPARENT
:
4762 b1
= gen_prevlinkhdr_check();
4763 b0
= gen_ehostop(eaddr
, Q_OR
);
4768 b0
= gen_fhostop(eaddr
, Q_OR
);
4771 b0
= gen_thostop(eaddr
, Q_OR
);
4773 case DLT_IEEE802_11
:
4774 case DLT_PRISM_HEADER
:
4775 case DLT_IEEE802_11_RADIO_AVS
:
4776 case DLT_IEEE802_11_RADIO
:
4778 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4782 * This is LLC-multiplexed traffic; if it were
4783 * LANE, linktype would have been set to
4787 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4789 case DLT_IP_OVER_FC
:
4790 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4794 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4796 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4798 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4807 bpf_error("illegal modifier of 'gateway'");
4813 gen_proto_abbrev(proto
)
4822 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4823 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4828 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4829 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4834 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4835 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4840 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4843 #ifndef IPPROTO_IGMP
4844 #define IPPROTO_IGMP 2
4848 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4851 #ifndef IPPROTO_IGRP
4852 #define IPPROTO_IGRP 9
4855 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4859 #define IPPROTO_PIM 103
4863 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4864 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4868 #ifndef IPPROTO_VRRP
4869 #define IPPROTO_VRRP 112
4873 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4876 #ifndef IPPROTO_CARP
4877 #define IPPROTO_CARP 112
4881 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4885 b1
= gen_linktype(ETHERTYPE_IP
);
4889 b1
= gen_linktype(ETHERTYPE_ARP
);
4893 b1
= gen_linktype(ETHERTYPE_REVARP
);
4897 bpf_error("link layer applied in wrong context");
4900 b1
= gen_linktype(ETHERTYPE_ATALK
);
4904 b1
= gen_linktype(ETHERTYPE_AARP
);
4908 b1
= gen_linktype(ETHERTYPE_DN
);
4912 b1
= gen_linktype(ETHERTYPE_SCA
);
4916 b1
= gen_linktype(ETHERTYPE_LAT
);
4920 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4924 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4928 b1
= gen_linktype(ETHERTYPE_IPV6
);
4931 #ifndef IPPROTO_ICMPV6
4932 #define IPPROTO_ICMPV6 58
4935 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4939 #define IPPROTO_AH 51
4942 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4943 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4948 #define IPPROTO_ESP 50
4951 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4952 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4957 b1
= gen_linktype(LLCSAP_ISONS
);
4961 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4965 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4968 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4969 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4970 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4972 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4974 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4976 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4980 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4981 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4982 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4984 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4986 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4988 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4992 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4993 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4994 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4996 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5001 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5002 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5007 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5008 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5010 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5012 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5017 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5018 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5023 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5024 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5029 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5033 b1
= gen_linktype(LLCSAP_8021D
);
5037 b1
= gen_linktype(LLCSAP_IPX
);
5041 b1
= gen_linktype(LLCSAP_NETBEUI
);
5045 bpf_error("'radio' is not a valid protocol type");
5053 static struct block
*
5059 /* not IPv4 frag other than the first frag */
5060 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5061 b
= new_block(JMP(BPF_JSET
));
5070 * Generate a comparison to a port value in the transport-layer header
5071 * at the specified offset from the beginning of that header.
5073 * XXX - this handles a variable-length prefix preceding the link-layer
5074 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5075 * variable-length link-layer headers (such as Token Ring or 802.11
5078 static struct block
*
5079 gen_portatom(off
, v
)
5083 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5086 static struct block
*
5087 gen_portatom6(off
, v
)
5091 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5095 gen_portop(port
, proto
, dir
)
5096 int port
, proto
, dir
;
5098 struct block
*b0
, *b1
, *tmp
;
5100 /* ip proto 'proto' and not a fragment other than the first fragment */
5101 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5107 b1
= gen_portatom(0, (bpf_int32
)port
);
5111 b1
= gen_portatom(2, (bpf_int32
)port
);
5116 tmp
= gen_portatom(0, (bpf_int32
)port
);
5117 b1
= gen_portatom(2, (bpf_int32
)port
);
5122 tmp
= gen_portatom(0, (bpf_int32
)port
);
5123 b1
= gen_portatom(2, (bpf_int32
)port
);
5135 static struct block
*
5136 gen_port(port
, ip_proto
, dir
)
5141 struct block
*b0
, *b1
, *tmp
;
5146 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5147 * not LLC encapsulation with LLCSAP_IP.
5149 * For IEEE 802 networks - which includes 802.5 token ring
5150 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5151 * says that SNAP encapsulation is used, not LLC encapsulation
5154 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5155 * RFC 2225 say that SNAP encapsulation is used, not LLC
5156 * encapsulation with LLCSAP_IP.
5158 * So we always check for ETHERTYPE_IP.
5160 b0
= gen_linktype(ETHERTYPE_IP
);
5166 b1
= gen_portop(port
, ip_proto
, dir
);
5170 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5171 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5173 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5185 gen_portop6(port
, proto
, dir
)
5186 int port
, proto
, dir
;
5188 struct block
*b0
, *b1
, *tmp
;
5190 /* ip6 proto 'proto' */
5191 /* XXX - catch the first fragment of a fragmented packet? */
5192 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5196 b1
= gen_portatom6(0, (bpf_int32
)port
);
5200 b1
= gen_portatom6(2, (bpf_int32
)port
);
5205 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5206 b1
= gen_portatom6(2, (bpf_int32
)port
);
5211 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5212 b1
= gen_portatom6(2, (bpf_int32
)port
);
5224 static struct block
*
5225 gen_port6(port
, ip_proto
, dir
)
5230 struct block
*b0
, *b1
, *tmp
;
5232 /* link proto ip6 */
5233 b0
= gen_linktype(ETHERTYPE_IPV6
);
5239 b1
= gen_portop6(port
, ip_proto
, dir
);
5243 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5244 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5246 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5257 /* gen_portrange code */
5258 static struct block
*
5259 gen_portrangeatom(off
, v1
, v2
)
5263 struct block
*b1
, *b2
;
5267 * Reverse the order of the ports, so v1 is the lower one.
5276 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5277 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5285 gen_portrangeop(port1
, port2
, proto
, dir
)
5290 struct block
*b0
, *b1
, *tmp
;
5292 /* ip proto 'proto' and not a fragment other than the first fragment */
5293 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5299 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5303 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5308 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5309 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5314 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5315 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5327 static struct block
*
5328 gen_portrange(port1
, port2
, ip_proto
, dir
)
5333 struct block
*b0
, *b1
, *tmp
;
5336 b0
= gen_linktype(ETHERTYPE_IP
);
5342 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5346 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5347 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5349 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5360 static struct block
*
5361 gen_portrangeatom6(off
, v1
, v2
)
5365 struct block
*b1
, *b2
;
5369 * Reverse the order of the ports, so v1 is the lower one.
5378 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5379 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5387 gen_portrangeop6(port1
, port2
, proto
, dir
)
5392 struct block
*b0
, *b1
, *tmp
;
5394 /* ip6 proto 'proto' */
5395 /* XXX - catch the first fragment of a fragmented packet? */
5396 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5400 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5404 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5409 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5410 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5415 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5416 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5428 static struct block
*
5429 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5434 struct block
*b0
, *b1
, *tmp
;
5436 /* link proto ip6 */
5437 b0
= gen_linktype(ETHERTYPE_IPV6
);
5443 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5447 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5448 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5450 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5462 lookup_proto(name
, proto
)
5463 register const char *name
;
5473 v
= pcap_nametoproto(name
);
5474 if (v
== PROTO_UNDEF
)
5475 bpf_error("unknown ip proto '%s'", name
);
5479 /* XXX should look up h/w protocol type based on linktype */
5480 v
= pcap_nametoeproto(name
);
5481 if (v
== PROTO_UNDEF
) {
5482 v
= pcap_nametollc(name
);
5483 if (v
== PROTO_UNDEF
)
5484 bpf_error("unknown ether proto '%s'", name
);
5489 if (strcmp(name
, "esis") == 0)
5491 else if (strcmp(name
, "isis") == 0)
5493 else if (strcmp(name
, "clnp") == 0)
5496 bpf_error("unknown osi proto '%s'", name
);
5516 static struct block
*
5517 gen_protochain(v
, proto
, dir
)
5522 #ifdef NO_PROTOCHAIN
5523 return gen_proto(v
, proto
, dir
);
5525 struct block
*b0
, *b
;
5526 struct slist
*s
[100];
5527 int fix2
, fix3
, fix4
, fix5
;
5528 int ahcheck
, again
, end
;
5530 int reg2
= alloc_reg();
5532 memset(s
, 0, sizeof(s
));
5533 fix2
= fix3
= fix4
= fix5
= 0;
5540 b0
= gen_protochain(v
, Q_IP
, dir
);
5541 b
= gen_protochain(v
, Q_IPV6
, dir
);
5545 bpf_error("bad protocol applied for 'protochain'");
5550 * We don't handle variable-length prefixes before the link-layer
5551 * header, or variable-length link-layer headers, here yet.
5552 * We might want to add BPF instructions to do the protochain
5553 * work, to simplify that and, on platforms that have a BPF
5554 * interpreter with the new instructions, let the filtering
5555 * be done in the kernel. (We already require a modified BPF
5556 * engine to do the protochain stuff, to support backward
5557 * branches, and backward branch support is unlikely to appear
5558 * in kernel BPF engines.)
5562 case DLT_IEEE802_11
:
5563 case DLT_PRISM_HEADER
:
5564 case DLT_IEEE802_11_RADIO_AVS
:
5565 case DLT_IEEE802_11_RADIO
:
5567 bpf_error("'protochain' not supported with 802.11");
5570 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5573 * s[0] is a dummy entry to protect other BPF insn from damage
5574 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5575 * hard to find interdependency made by jump table fixup.
5578 s
[i
] = new_stmt(0); /*dummy*/
5583 b0
= gen_linktype(ETHERTYPE_IP
);
5586 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5587 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5589 /* X = ip->ip_hl << 2 */
5590 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5591 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5596 b0
= gen_linktype(ETHERTYPE_IPV6
);
5598 /* A = ip6->ip_nxt */
5599 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5600 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5602 /* X = sizeof(struct ip6_hdr) */
5603 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5609 bpf_error("unsupported proto to gen_protochain");
5613 /* again: if (A == v) goto end; else fall through; */
5615 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5617 s
[i
]->s
.jt
= NULL
; /*later*/
5618 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5622 #ifndef IPPROTO_NONE
5623 #define IPPROTO_NONE 59
5625 /* if (A == IPPROTO_NONE) goto end */
5626 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5627 s
[i
]->s
.jt
= NULL
; /*later*/
5628 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5629 s
[i
]->s
.k
= IPPROTO_NONE
;
5630 s
[fix5
]->s
.jf
= s
[i
];
5634 if (proto
== Q_IPV6
) {
5635 int v6start
, v6end
, v6advance
, j
;
5638 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5639 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5640 s
[i
]->s
.jt
= NULL
; /*later*/
5641 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5642 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5643 s
[fix2
]->s
.jf
= s
[i
];
5645 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5646 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5647 s
[i
]->s
.jt
= NULL
; /*later*/
5648 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5649 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5651 /* if (A == IPPROTO_ROUTING) goto v6advance */
5652 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5653 s
[i
]->s
.jt
= NULL
; /*later*/
5654 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5655 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5657 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5658 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5659 s
[i
]->s
.jt
= NULL
; /*later*/
5660 s
[i
]->s
.jf
= NULL
; /*later*/
5661 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5671 * A = P[X + packet head];
5672 * X = X + (P[X + packet head + 1] + 1) * 8;
5674 /* A = P[X + packet head] */
5675 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5676 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5679 s
[i
] = new_stmt(BPF_ST
);
5682 /* A = P[X + packet head + 1]; */
5683 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5684 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5687 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5691 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5695 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5699 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5702 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5706 /* goto again; (must use BPF_JA for backward jump) */
5707 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5708 s
[i
]->s
.k
= again
- i
- 1;
5709 s
[i
- 1]->s
.jf
= s
[i
];
5713 for (j
= v6start
; j
<= v6end
; j
++)
5714 s
[j
]->s
.jt
= s
[v6advance
];
5717 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5719 s
[fix2
]->s
.jf
= s
[i
];
5725 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5726 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5727 s
[i
]->s
.jt
= NULL
; /*later*/
5728 s
[i
]->s
.jf
= NULL
; /*later*/
5729 s
[i
]->s
.k
= IPPROTO_AH
;
5731 s
[fix3
]->s
.jf
= s
[ahcheck
];
5738 * X = X + (P[X + 1] + 2) * 4;
5741 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5743 /* A = P[X + packet head]; */
5744 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5745 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5748 s
[i
] = new_stmt(BPF_ST
);
5752 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5755 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5759 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5761 /* A = P[X + packet head] */
5762 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5763 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5766 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5770 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5774 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5777 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5781 /* goto again; (must use BPF_JA for backward jump) */
5782 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5783 s
[i
]->s
.k
= again
- i
- 1;
5788 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5790 s
[fix2
]->s
.jt
= s
[end
];
5791 s
[fix4
]->s
.jf
= s
[end
];
5792 s
[fix5
]->s
.jt
= s
[end
];
5799 for (i
= 0; i
< max
- 1; i
++)
5800 s
[i
]->next
= s
[i
+ 1];
5801 s
[max
- 1]->next
= NULL
;
5806 b
= new_block(JMP(BPF_JEQ
));
5807 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5817 static struct block
*
5818 gen_check_802_11_data_frame()
5821 struct block
*b0
, *b1
;
5824 * A data frame has the 0x08 bit (b3) in the frame control field set
5825 * and the 0x04 bit (b2) clear.
5827 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5828 b0
= new_block(JMP(BPF_JSET
));
5832 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5833 b1
= new_block(JMP(BPF_JSET
));
5844 * Generate code that checks whether the packet is a packet for protocol
5845 * <proto> and whether the type field in that protocol's header has
5846 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5847 * IP packet and checks the protocol number in the IP header against <v>.
5849 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5850 * against Q_IP and Q_IPV6.
5852 static struct block
*
5853 gen_proto(v
, proto
, dir
)
5858 struct block
*b0
, *b1
;
5863 if (dir
!= Q_DEFAULT
)
5864 bpf_error("direction applied to 'proto'");
5868 b0
= gen_proto(v
, Q_IP
, dir
);
5869 b1
= gen_proto(v
, Q_IPV6
, dir
);
5875 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5876 * not LLC encapsulation with LLCSAP_IP.
5878 * For IEEE 802 networks - which includes 802.5 token ring
5879 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5880 * says that SNAP encapsulation is used, not LLC encapsulation
5883 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5884 * RFC 2225 say that SNAP encapsulation is used, not LLC
5885 * encapsulation with LLCSAP_IP.
5887 * So we always check for ETHERTYPE_IP.
5889 b0
= gen_linktype(ETHERTYPE_IP
);
5891 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5893 b1
= gen_protochain(v
, Q_IP
);
5903 * Frame Relay packets typically have an OSI
5904 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5905 * generates code to check for all the OSI
5906 * NLPIDs, so calling it and then adding a check
5907 * for the particular NLPID for which we're
5908 * looking is bogus, as we can just check for
5911 * What we check for is the NLPID and a frame
5912 * control field value of UI, i.e. 0x03 followed
5915 * XXX - assumes a 2-byte Frame Relay header with
5916 * DLCI and flags. What if the address is longer?
5918 * XXX - what about SNAP-encapsulated frames?
5920 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5926 * Cisco uses an Ethertype lookalike - for OSI,
5929 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5930 /* OSI in C-HDLC is stuffed with a fudge byte */
5931 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
5936 b0
= gen_linktype(LLCSAP_ISONS
);
5937 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
5943 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5945 * 4 is the offset of the PDU type relative to the IS-IS
5948 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
5953 bpf_error("arp does not encapsulate another protocol");
5957 bpf_error("rarp does not encapsulate another protocol");
5961 bpf_error("atalk encapsulation is not specifiable");
5965 bpf_error("decnet encapsulation is not specifiable");
5969 bpf_error("sca does not encapsulate another protocol");
5973 bpf_error("lat does not encapsulate another protocol");
5977 bpf_error("moprc does not encapsulate another protocol");
5981 bpf_error("mopdl does not encapsulate another protocol");
5985 return gen_linktype(v
);
5988 bpf_error("'udp proto' is bogus");
5992 bpf_error("'tcp proto' is bogus");
5996 bpf_error("'sctp proto' is bogus");
6000 bpf_error("'icmp proto' is bogus");
6004 bpf_error("'igmp proto' is bogus");
6008 bpf_error("'igrp proto' is bogus");
6012 bpf_error("'pim proto' is bogus");
6016 bpf_error("'vrrp proto' is bogus");
6020 bpf_error("'carp proto' is bogus");
6024 b0
= gen_linktype(ETHERTYPE_IPV6
);
6027 * Also check for a fragment header before the final
6030 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6031 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6033 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6036 b1
= gen_protochain(v
, Q_IPV6
);
6042 bpf_error("'icmp6 proto' is bogus");
6045 bpf_error("'ah proto' is bogus");
6048 bpf_error("'ah proto' is bogus");
6051 bpf_error("'stp proto' is bogus");
6054 bpf_error("'ipx proto' is bogus");
6057 bpf_error("'netbeui proto' is bogus");
6060 bpf_error("'radio proto' is bogus");
6071 register const char *name
;
6074 int proto
= q
.proto
;
6078 bpf_u_int32 mask
, addr
;
6080 bpf_u_int32
**alist
;
6083 struct sockaddr_in
*sin4
;
6084 struct sockaddr_in6
*sin6
;
6085 struct addrinfo
*res
, *res0
;
6086 struct in6_addr mask128
;
6088 struct block
*b
, *tmp
;
6089 int port
, real_proto
;
6095 addr
= pcap_nametonetaddr(name
);
6097 bpf_error("unknown network '%s'", name
);
6098 /* Left justify network addr and calculate its network mask */
6100 while (addr
&& (addr
& 0xff000000) == 0) {
6104 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6108 if (proto
== Q_LINK
) {
6112 case DLT_NETANALYZER
:
6113 case DLT_NETANALYZER_TRANSPARENT
:
6114 eaddr
= pcap_ether_hostton(name
);
6117 "unknown ether host '%s'", name
);
6118 tmp
= gen_prevlinkhdr_check();
6119 b
= gen_ehostop(eaddr
, dir
);
6126 eaddr
= pcap_ether_hostton(name
);
6129 "unknown FDDI host '%s'", name
);
6130 b
= gen_fhostop(eaddr
, dir
);
6135 eaddr
= pcap_ether_hostton(name
);
6138 "unknown token ring host '%s'", name
);
6139 b
= gen_thostop(eaddr
, dir
);
6143 case DLT_IEEE802_11
:
6144 case DLT_PRISM_HEADER
:
6145 case DLT_IEEE802_11_RADIO_AVS
:
6146 case DLT_IEEE802_11_RADIO
:
6148 eaddr
= pcap_ether_hostton(name
);
6151 "unknown 802.11 host '%s'", name
);
6152 b
= gen_wlanhostop(eaddr
, dir
);
6156 case DLT_IP_OVER_FC
:
6157 eaddr
= pcap_ether_hostton(name
);
6160 "unknown Fibre Channel host '%s'", name
);
6161 b
= gen_ipfchostop(eaddr
, dir
);
6166 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6167 } else if (proto
== Q_DECNET
) {
6168 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6170 * I don't think DECNET hosts can be multihomed, so
6171 * there is no need to build up a list of addresses
6173 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6176 alist
= pcap_nametoaddr(name
);
6177 if (alist
== NULL
|| *alist
== NULL
)
6178 bpf_error("unknown host '%s'", name
);
6180 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6182 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6184 tmp
= gen_host(**alist
++, 0xffffffff,
6185 tproto
, dir
, q
.addr
);
6191 memset(&mask128
, 0xff, sizeof(mask128
));
6192 res0
= res
= pcap_nametoaddrinfo(name
);
6194 bpf_error("unknown host '%s'", name
);
6197 tproto
= tproto6
= proto
;
6198 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6202 for (res
= res0
; res
; res
= res
->ai_next
) {
6203 switch (res
->ai_family
) {
6205 if (tproto
== Q_IPV6
)
6208 sin4
= (struct sockaddr_in
*)
6210 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6211 0xffffffff, tproto
, dir
, q
.addr
);
6214 if (tproto6
== Q_IP
)
6217 sin6
= (struct sockaddr_in6
*)
6219 tmp
= gen_host6(&sin6
->sin6_addr
,
6220 &mask128
, tproto6
, dir
, q
.addr
);
6232 bpf_error("unknown host '%s'%s", name
,
6233 (proto
== Q_DEFAULT
)
6235 : " for specified address family");
6242 if (proto
!= Q_DEFAULT
&&
6243 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6244 bpf_error("illegal qualifier of 'port'");
6245 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6246 bpf_error("unknown port '%s'", name
);
6247 if (proto
== Q_UDP
) {
6248 if (real_proto
== IPPROTO_TCP
)
6249 bpf_error("port '%s' is tcp", name
);
6250 else if (real_proto
== IPPROTO_SCTP
)
6251 bpf_error("port '%s' is sctp", name
);
6253 /* override PROTO_UNDEF */
6254 real_proto
= IPPROTO_UDP
;
6256 if (proto
== Q_TCP
) {
6257 if (real_proto
== IPPROTO_UDP
)
6258 bpf_error("port '%s' is udp", name
);
6260 else if (real_proto
== IPPROTO_SCTP
)
6261 bpf_error("port '%s' is sctp", name
);
6263 /* override PROTO_UNDEF */
6264 real_proto
= IPPROTO_TCP
;
6266 if (proto
== Q_SCTP
) {
6267 if (real_proto
== IPPROTO_UDP
)
6268 bpf_error("port '%s' is udp", name
);
6270 else if (real_proto
== IPPROTO_TCP
)
6271 bpf_error("port '%s' is tcp", name
);
6273 /* override PROTO_UNDEF */
6274 real_proto
= IPPROTO_SCTP
;
6277 bpf_error("illegal port number %d < 0", port
);
6279 bpf_error("illegal port number %d > 65535", port
);
6280 b
= gen_port(port
, real_proto
, dir
);
6281 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6285 if (proto
!= Q_DEFAULT
&&
6286 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6287 bpf_error("illegal qualifier of 'portrange'");
6288 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6289 bpf_error("unknown port in range '%s'", name
);
6290 if (proto
== Q_UDP
) {
6291 if (real_proto
== IPPROTO_TCP
)
6292 bpf_error("port in range '%s' is tcp", name
);
6293 else if (real_proto
== IPPROTO_SCTP
)
6294 bpf_error("port in range '%s' is sctp", name
);
6296 /* override PROTO_UNDEF */
6297 real_proto
= IPPROTO_UDP
;
6299 if (proto
== Q_TCP
) {
6300 if (real_proto
== IPPROTO_UDP
)
6301 bpf_error("port in range '%s' is udp", name
);
6302 else if (real_proto
== IPPROTO_SCTP
)
6303 bpf_error("port in range '%s' is sctp", name
);
6305 /* override PROTO_UNDEF */
6306 real_proto
= IPPROTO_TCP
;
6308 if (proto
== Q_SCTP
) {
6309 if (real_proto
== IPPROTO_UDP
)
6310 bpf_error("port in range '%s' is udp", name
);
6311 else if (real_proto
== IPPROTO_TCP
)
6312 bpf_error("port in range '%s' is tcp", name
);
6314 /* override PROTO_UNDEF */
6315 real_proto
= IPPROTO_SCTP
;
6318 bpf_error("illegal port number %d < 0", port1
);
6320 bpf_error("illegal port number %d > 65535", port1
);
6322 bpf_error("illegal port number %d < 0", port2
);
6324 bpf_error("illegal port number %d > 65535", port2
);
6326 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6327 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6332 eaddr
= pcap_ether_hostton(name
);
6334 bpf_error("unknown ether host: %s", name
);
6336 alist
= pcap_nametoaddr(name
);
6337 if (alist
== NULL
|| *alist
== NULL
)
6338 bpf_error("unknown host '%s'", name
);
6339 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6343 bpf_error("'gateway' not supported in this configuration");
6347 real_proto
= lookup_proto(name
, proto
);
6348 if (real_proto
>= 0)
6349 return gen_proto(real_proto
, proto
, dir
);
6351 bpf_error("unknown protocol: %s", name
);
6354 real_proto
= lookup_proto(name
, proto
);
6355 if (real_proto
>= 0)
6356 return gen_protochain(real_proto
, proto
, dir
);
6358 bpf_error("unknown protocol: %s", name
);
6369 gen_mcode(s1
, s2
, masklen
, q
)
6370 register const char *s1
, *s2
;
6371 register unsigned int masklen
;
6374 register int nlen
, mlen
;
6377 nlen
= __pcap_atoin(s1
, &n
);
6378 /* Promote short ipaddr */
6382 mlen
= __pcap_atoin(s2
, &m
);
6383 /* Promote short ipaddr */
6386 bpf_error("non-network bits set in \"%s mask %s\"",
6389 /* Convert mask len to mask */
6391 bpf_error("mask length must be <= 32");
6394 * X << 32 is not guaranteed by C to be 0; it's
6399 m
= 0xffffffff << (32 - masklen
);
6401 bpf_error("non-network bits set in \"%s/%d\"",
6408 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6411 bpf_error("Mask syntax for networks only");
6420 register const char *s
;
6425 int proto
= q
.proto
;
6431 else if (q
.proto
== Q_DECNET
)
6432 vlen
= __pcap_atodn(s
, &v
);
6434 vlen
= __pcap_atoin(s
, &v
);
6441 if (proto
== Q_DECNET
)
6442 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6443 else if (proto
== Q_LINK
) {
6444 bpf_error("illegal link layer address");
6447 if (s
== NULL
&& q
.addr
== Q_NET
) {
6448 /* Promote short net number */
6449 while (v
&& (v
& 0xff000000) == 0) {
6454 /* Promote short ipaddr */
6458 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6463 proto
= IPPROTO_UDP
;
6464 else if (proto
== Q_TCP
)
6465 proto
= IPPROTO_TCP
;
6466 else if (proto
== Q_SCTP
)
6467 proto
= IPPROTO_SCTP
;
6468 else if (proto
== Q_DEFAULT
)
6469 proto
= PROTO_UNDEF
;
6471 bpf_error("illegal qualifier of 'port'");
6474 bpf_error("illegal port number %u > 65535", v
);
6478 b
= gen_port((int)v
, proto
, dir
);
6479 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6485 proto
= IPPROTO_UDP
;
6486 else if (proto
== Q_TCP
)
6487 proto
= IPPROTO_TCP
;
6488 else if (proto
== Q_SCTP
)
6489 proto
= IPPROTO_SCTP
;
6490 else if (proto
== Q_DEFAULT
)
6491 proto
= PROTO_UNDEF
;
6493 bpf_error("illegal qualifier of 'portrange'");
6496 bpf_error("illegal port number %u > 65535", v
);
6500 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6501 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6506 bpf_error("'gateway' requires a name");
6510 return gen_proto((int)v
, proto
, dir
);
6513 return gen_protochain((int)v
, proto
, dir
);
6528 gen_mcode6(s1
, s2
, masklen
, q
)
6529 register const char *s1
, *s2
;
6530 register unsigned int masklen
;
6533 struct addrinfo
*res
;
6534 struct in6_addr
*addr
;
6535 struct in6_addr mask
;
6540 bpf_error("no mask %s supported", s2
);
6542 res
= pcap_nametoaddrinfo(s1
);
6544 bpf_error("invalid ip6 address %s", s1
);
6547 bpf_error("%s resolved to multiple address", s1
);
6548 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6550 if (sizeof(mask
) * 8 < masklen
)
6551 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6552 memset(&mask
, 0, sizeof(mask
));
6553 memset(&mask
, 0xff, masklen
/ 8);
6555 mask
.s6_addr
[masklen
/ 8] =
6556 (0xff << (8 - masklen
% 8)) & 0xff;
6559 a
= (u_int32_t
*)addr
;
6560 m
= (u_int32_t
*)&mask
;
6561 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6562 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6563 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6571 bpf_error("Mask syntax for networks only");
6575 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6581 bpf_error("invalid qualifier against IPv6 address");
6590 register const u_char
*eaddr
;
6593 struct block
*b
, *tmp
;
6595 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6598 case DLT_NETANALYZER
:
6599 case DLT_NETANALYZER_TRANSPARENT
:
6600 tmp
= gen_prevlinkhdr_check();
6601 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6606 return gen_fhostop(eaddr
, (int)q
.dir
);
6608 return gen_thostop(eaddr
, (int)q
.dir
);
6609 case DLT_IEEE802_11
:
6610 case DLT_PRISM_HEADER
:
6611 case DLT_IEEE802_11_RADIO_AVS
:
6612 case DLT_IEEE802_11_RADIO
:
6614 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6615 case DLT_IP_OVER_FC
:
6616 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6618 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6622 bpf_error("ethernet address used in non-ether expression");
6629 struct slist
*s0
, *s1
;
6632 * This is definitely not the best way to do this, but the
6633 * lists will rarely get long.
6640 static struct slist
*
6646 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6651 static struct slist
*
6657 s
= new_stmt(BPF_LD
|BPF_MEM
);
6663 * Modify "index" to use the value stored into its register as an
6664 * offset relative to the beginning of the header for the protocol
6665 * "proto", and allocate a register and put an item "size" bytes long
6666 * (1, 2, or 4) at that offset into that register, making it the register
6670 gen_load(proto
, inst
, size
)
6675 struct slist
*s
, *tmp
;
6677 int regno
= alloc_reg();
6679 free_reg(inst
->regno
);
6683 bpf_error("data size must be 1, 2, or 4");
6699 bpf_error("unsupported index operation");
6703 * The offset is relative to the beginning of the packet
6704 * data, if we have a radio header. (If we don't, this
6707 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6708 linktype
!= DLT_IEEE802_11_RADIO
&&
6709 linktype
!= DLT_PRISM_HEADER
)
6710 bpf_error("radio information not present in capture");
6713 * Load into the X register the offset computed into the
6714 * register specified by "index".
6716 s
= xfer_to_x(inst
);
6719 * Load the item at that offset.
6721 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6723 sappend(inst
->s
, s
);
6728 * The offset is relative to the beginning of
6729 * the link-layer header.
6731 * XXX - what about ATM LANE? Should the index be
6732 * relative to the beginning of the AAL5 frame, so
6733 * that 0 refers to the beginning of the LE Control
6734 * field, or relative to the beginning of the LAN
6735 * frame, so that 0 refers, for Ethernet LANE, to
6736 * the beginning of the destination address?
6738 s
= gen_abs_offset_varpart(&off_linkhdr
);
6741 * If "s" is non-null, it has code to arrange that the
6742 * X register contains the length of the prefix preceding
6743 * the link-layer header. Add to it the offset computed
6744 * into the register specified by "index", and move that
6745 * into the X register. Otherwise, just load into the X
6746 * register the offset computed into the register specified
6750 sappend(s
, xfer_to_a(inst
));
6751 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6752 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6754 s
= xfer_to_x(inst
);
6757 * Load the item at the sum of the offset we've put in the
6758 * X register and the offset of the start of the link
6759 * layer header (which is 0 if the radio header is
6760 * variable-length; that header length is what we put
6761 * into the X register and then added to the index).
6763 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6764 tmp
->s
.k
= off_linkhdr
.constant_part
;
6766 sappend(inst
->s
, s
);
6780 * The offset is relative to the beginning of
6781 * the network-layer header.
6782 * XXX - are there any cases where we want
6785 s
= gen_abs_offset_varpart(&off_linkpl
);
6788 * If "s" is non-null, it has code to arrange that the
6789 * X register contains the variable part of the offset
6790 * of the link-layer payload. Add to it the offset
6791 * computed into the register specified by "index",
6792 * and move that into the X register. Otherwise, just
6793 * load into the X register the offset computed into
6794 * the register specified by "index".
6797 sappend(s
, xfer_to_a(inst
));
6798 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6799 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6801 s
= xfer_to_x(inst
);
6804 * Load the item at the sum of the offset we've put in the
6805 * X register, the offset of the start of the network
6806 * layer header from the beginning of the link-layer
6807 * payload, and the constant part of the offset of the
6808 * start of the link-layer payload.
6810 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6811 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6813 sappend(inst
->s
, s
);
6816 * Do the computation only if the packet contains
6817 * the protocol in question.
6819 b
= gen_proto_abbrev(proto
);
6821 gen_and(inst
->b
, b
);
6835 * The offset is relative to the beginning of
6836 * the transport-layer header.
6838 * Load the X register with the length of the IPv4 header
6839 * (plus the offset of the link-layer header, if it's
6840 * a variable-length header), in bytes.
6842 * XXX - are there any cases where we want
6844 * XXX - we should, if we're built with
6845 * IPv6 support, generate code to load either
6846 * IPv4, IPv6, or both, as appropriate.
6848 s
= gen_loadx_iphdrlen();
6851 * The X register now contains the sum of the variable
6852 * part of the offset of the link-layer payload and the
6853 * length of the network-layer header.
6855 * Load into the A register the offset relative to
6856 * the beginning of the transport layer header,
6857 * add the X register to that, move that to the
6858 * X register, and load with an offset from the
6859 * X register equal to the sum of the constant part of
6860 * the offset of the link-layer payload and the offset,
6861 * relative to the beginning of the link-layer payload,
6862 * of the network-layer header.
6864 sappend(s
, xfer_to_a(inst
));
6865 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6866 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6867 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6868 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6869 sappend(inst
->s
, s
);
6872 * Do the computation only if the packet contains
6873 * the protocol in question - which is true only
6874 * if this is an IP datagram and is the first or
6875 * only fragment of that datagram.
6877 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6879 gen_and(inst
->b
, b
);
6880 gen_and(gen_proto_abbrev(Q_IP
), b
);
6884 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6887 inst
->regno
= regno
;
6888 s
= new_stmt(BPF_ST
);
6890 sappend(inst
->s
, s
);
6896 gen_relation(code
, a0
, a1
, reversed
)
6898 struct arth
*a0
, *a1
;
6901 struct slist
*s0
, *s1
, *s2
;
6902 struct block
*b
, *tmp
;
6906 if (code
== BPF_JEQ
) {
6907 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6908 b
= new_block(JMP(code
));
6912 b
= new_block(BPF_JMP
|code
|BPF_X
);
6918 sappend(a0
->s
, a1
->s
);
6922 free_reg(a0
->regno
);
6923 free_reg(a1
->regno
);
6925 /* 'and' together protocol checks */
6928 gen_and(a0
->b
, tmp
= a1
->b
);
6944 int regno
= alloc_reg();
6945 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6948 s
= new_stmt(BPF_LD
|BPF_LEN
);
6949 s
->next
= new_stmt(BPF_ST
);
6950 s
->next
->s
.k
= regno
;
6965 a
= (struct arth
*)newchunk(sizeof(*a
));
6969 s
= new_stmt(BPF_LD
|BPF_IMM
);
6971 s
->next
= new_stmt(BPF_ST
);
6987 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6990 s
= new_stmt(BPF_ST
);
6998 gen_arth(code
, a0
, a1
)
7000 struct arth
*a0
, *a1
;
7002 struct slist
*s0
, *s1
, *s2
;
7006 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7011 sappend(a0
->s
, a1
->s
);
7013 free_reg(a0
->regno
);
7014 free_reg(a1
->regno
);
7016 s0
= new_stmt(BPF_ST
);
7017 a0
->regno
= s0
->s
.k
= alloc_reg();
7024 * Here we handle simple allocation of the scratch registers.
7025 * If too many registers are alloc'd, the allocator punts.
7027 static int regused
[BPF_MEMWORDS
];
7031 * Initialize the table of used registers and the current register.
7037 memset(regused
, 0, sizeof regused
);
7041 * Return the next free register.
7046 int n
= BPF_MEMWORDS
;
7049 if (regused
[curreg
])
7050 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7052 regused
[curreg
] = 1;
7056 bpf_error("too many registers needed to evaluate expression");
7062 * Return a register to the table so it can
7072 static struct block
*
7079 s
= new_stmt(BPF_LD
|BPF_LEN
);
7080 b
= new_block(JMP(jmp
));
7091 return gen_len(BPF_JGE
, n
);
7095 * Actually, this is less than or equal.
7103 b
= gen_len(BPF_JGT
, n
);
7110 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7111 * the beginning of the link-layer header.
7112 * XXX - that means you can't test values in the radiotap header, but
7113 * as that header is difficult if not impossible to parse generally
7114 * without a loop, that might not be a severe problem. A new keyword
7115 * "radio" could be added for that, although what you'd really want
7116 * would be a way of testing particular radio header values, which
7117 * would generate code appropriate to the radio header in question.
7120 gen_byteop(op
, idx
, val
)
7131 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7134 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7138 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7142 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7146 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7150 b
= new_block(JMP(BPF_JEQ
));
7157 static u_char abroadcast
[] = { 0x0 };
7160 gen_broadcast(proto
)
7163 bpf_u_int32 hostmask
;
7164 struct block
*b0
, *b1
, *b2
;
7165 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7173 case DLT_ARCNET_LINUX
:
7174 return gen_ahostop(abroadcast
, Q_DST
);
7176 case DLT_NETANALYZER
:
7177 case DLT_NETANALYZER_TRANSPARENT
:
7178 b1
= gen_prevlinkhdr_check();
7179 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7184 return gen_fhostop(ebroadcast
, Q_DST
);
7186 return gen_thostop(ebroadcast
, Q_DST
);
7187 case DLT_IEEE802_11
:
7188 case DLT_PRISM_HEADER
:
7189 case DLT_IEEE802_11_RADIO_AVS
:
7190 case DLT_IEEE802_11_RADIO
:
7192 return gen_wlanhostop(ebroadcast
, Q_DST
);
7193 case DLT_IP_OVER_FC
:
7194 return gen_ipfchostop(ebroadcast
, Q_DST
);
7196 bpf_error("not a broadcast link");
7202 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7203 * as an indication that we don't know the netmask, and fail
7206 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7207 bpf_error("netmask not known, so 'ip broadcast' not supported");
7208 b0
= gen_linktype(ETHERTYPE_IP
);
7209 hostmask
= ~netmask
;
7210 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7211 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7212 (bpf_int32
)(~0 & hostmask
), hostmask
);
7217 bpf_error("only link-layer/IP broadcast filters supported");
7223 * Generate code to test the low-order bit of a MAC address (that's
7224 * the bottom bit of the *first* byte).
7226 static struct block
*
7227 gen_mac_multicast(offset
)
7230 register struct block
*b0
;
7231 register struct slist
*s
;
7233 /* link[offset] & 1 != 0 */
7234 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7235 b0
= new_block(JMP(BPF_JSET
));
7242 gen_multicast(proto
)
7245 register struct block
*b0
, *b1
, *b2
;
7246 register struct slist
*s
;
7254 case DLT_ARCNET_LINUX
:
7255 /* all ARCnet multicasts use the same address */
7256 return gen_ahostop(abroadcast
, Q_DST
);
7258 case DLT_NETANALYZER
:
7259 case DLT_NETANALYZER_TRANSPARENT
:
7260 b1
= gen_prevlinkhdr_check();
7261 /* ether[0] & 1 != 0 */
7262 b0
= gen_mac_multicast(0);
7268 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7270 * XXX - was that referring to bit-order issues?
7272 /* fddi[1] & 1 != 0 */
7273 return gen_mac_multicast(1);
7275 /* tr[2] & 1 != 0 */
7276 return gen_mac_multicast(2);
7277 case DLT_IEEE802_11
:
7278 case DLT_PRISM_HEADER
:
7279 case DLT_IEEE802_11_RADIO_AVS
:
7280 case DLT_IEEE802_11_RADIO
:
7285 * For control frames, there is no DA.
7287 * For management frames, DA is at an
7288 * offset of 4 from the beginning of
7291 * For data frames, DA is at an offset
7292 * of 4 from the beginning of the packet
7293 * if To DS is clear and at an offset of
7294 * 16 from the beginning of the packet
7299 * Generate the tests to be done for data frames.
7301 * First, check for To DS set, i.e. "link[1] & 0x01".
7303 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7304 b1
= new_block(JMP(BPF_JSET
));
7305 b1
->s
.k
= 0x01; /* To DS */
7309 * If To DS is set, the DA is at 16.
7311 b0
= gen_mac_multicast(16);
7315 * Now, check for To DS not set, i.e. check
7316 * "!(link[1] & 0x01)".
7318 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7319 b2
= new_block(JMP(BPF_JSET
));
7320 b2
->s
.k
= 0x01; /* To DS */
7325 * If To DS is not set, the DA is at 4.
7327 b1
= gen_mac_multicast(4);
7331 * Now OR together the last two checks. That gives
7332 * the complete set of checks for data frames.
7337 * Now check for a data frame.
7338 * I.e, check "link[0] & 0x08".
7340 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7341 b1
= new_block(JMP(BPF_JSET
));
7346 * AND that with the checks done for data frames.
7351 * If the high-order bit of the type value is 0, this
7352 * is a management frame.
7353 * I.e, check "!(link[0] & 0x08)".
7355 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7356 b2
= new_block(JMP(BPF_JSET
));
7362 * For management frames, the DA is at 4.
7364 b1
= gen_mac_multicast(4);
7368 * OR that with the checks done for data frames.
7369 * That gives the checks done for management and
7375 * If the low-order bit of the type value is 1,
7376 * this is either a control frame or a frame
7377 * with a reserved type, and thus not a
7380 * I.e., check "!(link[0] & 0x04)".
7382 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7383 b1
= new_block(JMP(BPF_JSET
));
7389 * AND that with the checks for data and management
7394 case DLT_IP_OVER_FC
:
7395 b0
= gen_mac_multicast(2);
7400 /* Link not known to support multicasts */
7404 b0
= gen_linktype(ETHERTYPE_IP
);
7405 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7410 b0
= gen_linktype(ETHERTYPE_IPV6
);
7411 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7415 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7421 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7422 * Outbound traffic is sent by this machine, while inbound traffic is
7423 * sent by a remote machine (and may include packets destined for a
7424 * unicast or multicast link-layer address we are not subscribing to).
7425 * These are the same definitions implemented by pcap_setdirection().
7426 * Capturing only unicast traffic destined for this host is probably
7427 * better accomplished using a higher-layer filter.
7433 register struct block
*b0
;
7436 * Only some data link types support inbound/outbound qualifiers.
7440 b0
= gen_relation(BPF_JEQ
,
7441 gen_load(Q_LINK
, gen_loadi(0), 1),
7448 /* match outgoing packets */
7449 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7451 /* match incoming packets */
7452 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7457 /* match outgoing packets */
7458 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7460 /* to filter on inbound traffic, invert the match */
7465 #ifdef HAVE_NET_PFVAR_H
7467 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7468 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7474 /* match outgoing packets */
7475 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7477 /* match incoming packets */
7478 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7482 case DLT_JUNIPER_MFR
:
7483 case DLT_JUNIPER_MLFR
:
7484 case DLT_JUNIPER_MLPPP
:
7485 case DLT_JUNIPER_ATM1
:
7486 case DLT_JUNIPER_ATM2
:
7487 case DLT_JUNIPER_PPPOE
:
7488 case DLT_JUNIPER_PPPOE_ATM
:
7489 case DLT_JUNIPER_GGSN
:
7490 case DLT_JUNIPER_ES
:
7491 case DLT_JUNIPER_MONITOR
:
7492 case DLT_JUNIPER_SERVICES
:
7493 case DLT_JUNIPER_ETHER
:
7494 case DLT_JUNIPER_PPP
:
7495 case DLT_JUNIPER_FRELAY
:
7496 case DLT_JUNIPER_CHDLC
:
7497 case DLT_JUNIPER_VP
:
7498 case DLT_JUNIPER_ST
:
7499 case DLT_JUNIPER_ISM
:
7500 case DLT_JUNIPER_VS
:
7501 case DLT_JUNIPER_SRX_E2E
:
7502 case DLT_JUNIPER_FIBRECHANNEL
:
7503 case DLT_JUNIPER_ATM_CEMIC
:
7505 /* juniper flags (including direction) are stored
7506 * the byte after the 3-byte magic number */
7508 /* match outgoing packets */
7509 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7511 /* match incoming packets */
7512 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7518 * If we have packet meta-data indicating a direction,
7519 * check it, otherwise give up as this link-layer type
7520 * has nothing in the packet data.
7522 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7524 * This is Linux with PF_PACKET support.
7525 * If this is a *live* capture, we can look at
7526 * special meta-data in the filter expression;
7527 * if it's a savefile, we can't.
7529 if (bpf_pcap
->rfile
!= NULL
) {
7530 /* We have a FILE *, so this is a savefile */
7531 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7536 /* match outgoing packets */
7537 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7540 /* to filter on inbound traffic, invert the match */
7543 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7544 bpf_error("inbound/outbound not supported on linktype %d",
7548 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7553 #ifdef HAVE_NET_PFVAR_H
7554 /* PF firewall log matched interface */
7556 gen_pf_ifname(const char *ifname
)
7561 if (linktype
!= DLT_PFLOG
) {
7562 bpf_error("ifname supported only on PF linktype");
7565 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7566 off
= offsetof(struct pfloghdr
, ifname
);
7567 if (strlen(ifname
) >= len
) {
7568 bpf_error("ifname interface names can only be %d characters",
7572 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7576 /* PF firewall log ruleset name */
7578 gen_pf_ruleset(char *ruleset
)
7582 if (linktype
!= DLT_PFLOG
) {
7583 bpf_error("ruleset supported only on PF linktype");
7587 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7588 bpf_error("ruleset names can only be %ld characters",
7589 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7593 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7594 strlen(ruleset
), (const u_char
*)ruleset
);
7598 /* PF firewall log rule number */
7604 if (linktype
!= DLT_PFLOG
) {
7605 bpf_error("rnr supported only on PF linktype");
7609 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7614 /* PF firewall log sub-rule number */
7616 gen_pf_srnr(int srnr
)
7620 if (linktype
!= DLT_PFLOG
) {
7621 bpf_error("srnr supported only on PF linktype");
7625 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7630 /* PF firewall log reason code */
7632 gen_pf_reason(int reason
)
7636 if (linktype
!= DLT_PFLOG
) {
7637 bpf_error("reason supported only on PF linktype");
7641 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7646 /* PF firewall log action */
7648 gen_pf_action(int action
)
7652 if (linktype
!= DLT_PFLOG
) {
7653 bpf_error("action supported only on PF linktype");
7657 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7661 #else /* !HAVE_NET_PFVAR_H */
7663 gen_pf_ifname(const char *ifname
)
7665 bpf_error("libpcap was compiled without pf support");
7671 gen_pf_ruleset(char *ruleset
)
7673 bpf_error("libpcap was compiled on a machine without pf support");
7681 bpf_error("libpcap was compiled on a machine without pf support");
7687 gen_pf_srnr(int srnr
)
7689 bpf_error("libpcap was compiled on a machine without pf support");
7695 gen_pf_reason(int reason
)
7697 bpf_error("libpcap was compiled on a machine without pf support");
7703 gen_pf_action(int action
)
7705 bpf_error("libpcap was compiled on a machine without pf support");
7709 #endif /* HAVE_NET_PFVAR_H */
7711 /* IEEE 802.11 wireless header */
7713 gen_p80211_type(int type
, int mask
)
7719 case DLT_IEEE802_11
:
7720 case DLT_PRISM_HEADER
:
7721 case DLT_IEEE802_11_RADIO_AVS
:
7722 case DLT_IEEE802_11_RADIO
:
7723 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7728 bpf_error("802.11 link-layer types supported only on 802.11");
7736 gen_p80211_fcdir(int fcdir
)
7742 case DLT_IEEE802_11
:
7743 case DLT_PRISM_HEADER
:
7744 case DLT_IEEE802_11_RADIO_AVS
:
7745 case DLT_IEEE802_11_RADIO
:
7749 bpf_error("frame direction supported only with 802.11 headers");
7753 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7754 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7761 register const u_char
*eaddr
;
7767 case DLT_ARCNET_LINUX
:
7768 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7770 return (gen_ahostop(eaddr
, (int)q
.dir
));
7772 bpf_error("ARCnet address used in non-arc expression");
7778 bpf_error("aid supported only on ARCnet");
7781 bpf_error("ARCnet address used in non-arc expression");
7786 static struct block
*
7787 gen_ahostop(eaddr
, dir
)
7788 register const u_char
*eaddr
;
7791 register struct block
*b0
, *b1
;
7794 /* src comes first, different from Ethernet */
7796 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7799 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7802 b0
= gen_ahostop(eaddr
, Q_SRC
);
7803 b1
= gen_ahostop(eaddr
, Q_DST
);
7809 b0
= gen_ahostop(eaddr
, Q_SRC
);
7810 b1
= gen_ahostop(eaddr
, Q_DST
);
7815 bpf_error("'addr1' is only supported on 802.11");
7819 bpf_error("'addr2' is only supported on 802.11");
7823 bpf_error("'addr3' is only supported on 802.11");
7827 bpf_error("'addr4' is only supported on 802.11");
7831 bpf_error("'ra' is only supported on 802.11");
7835 bpf_error("'ta' is only supported on 802.11");
7842 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7843 static struct block
*
7844 gen_vlan_bpf_extensions(int vlan_num
)
7846 struct block
*b0
, *b1
;
7849 /* generate new filter code based on extracting packet
7851 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7852 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7854 b0
= new_block(JMP(BPF_JEQ
));
7858 if (vlan_num
>= 0) {
7859 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7860 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7862 b1
= new_block(JMP(BPF_JEQ
));
7864 b1
->s
.k
= (bpf_int32
) vlan_num
;
7874 static struct block
*
7875 gen_vlan_no_bpf_extensions(int vlan_num
)
7877 struct block
*b0
, *b1
;
7879 /* check for VLAN, including QinQ */
7880 b0
= gen_linktype(ETHERTYPE_8021Q
);
7881 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7885 /* If a specific VLAN is requested, check VLAN id */
7886 if (vlan_num
>= 0) {
7887 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7888 (bpf_int32
)vlan_num
, 0x0fff);
7894 * The payload follows the full header, including the
7895 * VLAN tags, so skip past this VLAN tag.
7897 off_linkpl
.constant_part
+= 4;
7900 * The link-layer type information follows the VLAN tags, so
7901 * skip past this VLAN tag.
7909 * support IEEE 802.1Q VLAN trunk over ethernet
7917 /* can't check for VLAN-encapsulated packets inside MPLS */
7918 if (label_stack_depth
> 0)
7919 bpf_error("no VLAN match after MPLS");
7922 * Check for a VLAN packet, and then change the offsets to point
7923 * to the type and data fields within the VLAN packet. Just
7924 * increment the offsets, so that we can support a hierarchy, e.g.
7925 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7928 * XXX - this is a bit of a kludge. If we were to split the
7929 * compiler into a parser that parses an expression and
7930 * generates an expression tree, and a code generator that
7931 * takes an expression tree (which could come from our
7932 * parser or from some other parser) and generates BPF code,
7933 * we could perhaps make the offsets parameters of routines
7934 * and, in the handler for an "AND" node, pass to subnodes
7935 * other than the VLAN node the adjusted offsets.
7937 * This would mean that "vlan" would, instead of changing the
7938 * behavior of *all* tests after it, change only the behavior
7939 * of tests ANDed with it. That would change the documented
7940 * semantics of "vlan", which might break some expressions.
7941 * However, it would mean that "(vlan and ip) or ip" would check
7942 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7943 * checking only for VLAN-encapsulated IP, so that could still
7944 * be considered worth doing; it wouldn't break expressions
7945 * that are of the form "vlan and ..." or "vlan N and ...",
7946 * which I suspect are the most common expressions involving
7947 * "vlan". "vlan or ..." doesn't necessarily do what the user
7948 * would really want, now, as all the "or ..." tests would
7949 * be done assuming a VLAN, even though the "or" could be viewed
7950 * as meaning "or, if this isn't a VLAN packet...".
7955 case DLT_NETANALYZER
:
7956 case DLT_NETANALYZER_TRANSPARENT
:
7957 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7958 if (vlan_stack_depth
== 0) {
7960 * Do we need special VLAN handling?
7962 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
7963 b0
= gen_vlan_bpf_extensions(vlan_num
);
7965 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7968 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7971 case DLT_IEEE802_11
:
7972 case DLT_PRISM_HEADER
:
7973 case DLT_IEEE802_11_RADIO_AVS
:
7974 case DLT_IEEE802_11_RADIO
:
7975 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
7979 bpf_error("no VLAN support for data link type %d",
7996 struct block
*b0
, *b1
;
7998 if (label_stack_depth
> 0) {
7999 /* just match the bottom-of-stack bit clear */
8000 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8003 * We're not in an MPLS stack yet, so check the link-layer
8004 * type against MPLS.
8008 case DLT_C_HDLC
: /* fall through */
8010 case DLT_NETANALYZER
:
8011 case DLT_NETANALYZER_TRANSPARENT
:
8012 b0
= gen_linktype(ETHERTYPE_MPLS
);
8016 b0
= gen_linktype(PPP_MPLS_UCAST
);
8019 /* FIXME add other DLT_s ...
8020 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8021 * leave it for now */
8024 bpf_error("no MPLS support for data link type %d",
8032 /* If a specific MPLS label is requested, check it */
8033 if (label_num
>= 0) {
8034 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8035 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8036 0xfffff000); /* only compare the first 20 bits */
8042 * Change the offsets to point to the type and data fields within
8043 * the MPLS packet. Just increment the offsets, so that we
8044 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8045 * capture packets with an outer label of 100000 and an inner
8048 * Increment the MPLS stack depth as well; this indicates that
8049 * we're checking MPLS-encapsulated headers, to make sure higher
8050 * level code generators don't try to match against IP-related
8051 * protocols such as Q_ARP, Q_RARP etc.
8053 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8057 label_stack_depth
++;
8062 * Support PPPOE discovery and session.
8067 /* check for PPPoE discovery */
8068 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8072 gen_pppoes(sess_num
)
8075 struct block
*b0
, *b1
;
8078 * Test against the PPPoE session link-layer type.
8080 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8082 /* If a specific session is requested, check PPPoE session id */
8083 if (sess_num
>= 0) {
8084 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8085 (bpf_int32
)sess_num
, 0x0000ffff);
8091 * Change the offsets to point to the type and data fields within
8092 * the PPP packet, and note that this is PPPoE rather than
8095 * XXX - this is a bit of a kludge. If we were to split the
8096 * compiler into a parser that parses an expression and
8097 * generates an expression tree, and a code generator that
8098 * takes an expression tree (which could come from our
8099 * parser or from some other parser) and generates BPF code,
8100 * we could perhaps make the offsets parameters of routines
8101 * and, in the handler for an "AND" node, pass to subnodes
8102 * other than the PPPoE node the adjusted offsets.
8104 * This would mean that "pppoes" would, instead of changing the
8105 * behavior of *all* tests after it, change only the behavior
8106 * of tests ANDed with it. That would change the documented
8107 * semantics of "pppoes", which might break some expressions.
8108 * However, it would mean that "(pppoes and ip) or ip" would check
8109 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8110 * checking only for VLAN-encapsulated IP, so that could still
8111 * be considered worth doing; it wouldn't break expressions
8112 * that are of the form "pppoes and ..." which I suspect are the
8113 * most common expressions involving "pppoes". "pppoes or ..."
8114 * doesn't necessarily do what the user would really want, now,
8115 * as all the "or ..." tests would be done assuming PPPoE, even
8116 * though the "or" could be viewed as meaning "or, if this isn't
8117 * a PPPoE packet...".
8119 * The "network-layer" protocol is PPPoE, which has a 6-byte
8120 * PPPoE header, followed by a PPP packet.
8122 * There is no HDLC encapsulation for the PPP packet (it's
8123 * encapsulated in PPPoES instead), so the link-layer type
8124 * starts at the first byte of the PPP packet. For PPPoE,
8125 * that offset is relative to the beginning of the total
8126 * link-layer payload, including any 802.2 LLC header, so
8127 * it's 6 bytes past off_nl.
8129 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8130 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8134 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8137 off_nl_nosnap
= 0; /* no 802.2 LLC */
8143 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8155 bpf_error("'vpi' supported only on raw ATM");
8156 if (off_vpi
== (u_int
)-1)
8158 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8164 bpf_error("'vci' supported only on raw ATM");
8165 if (off_vci
== (u_int
)-1)
8167 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8172 if (off_proto
== (u_int
)-1)
8173 abort(); /* XXX - this isn't on FreeBSD */
8174 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8179 if (off_payload
== (u_int
)-1)
8181 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8182 0xffffffff, jtype
, reverse
, jvalue
);
8187 bpf_error("'callref' supported only on raw ATM");
8188 if (off_proto
== (u_int
)-1)
8190 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8191 jtype
, reverse
, jvalue
);
8201 gen_atmtype_abbrev(type
)
8204 struct block
*b0
, *b1
;
8209 /* Get all packets in Meta signalling Circuit */
8211 bpf_error("'metac' supported only on raw ATM");
8212 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8213 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8218 /* Get all packets in Broadcast Circuit*/
8220 bpf_error("'bcc' supported only on raw ATM");
8221 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8222 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8227 /* Get all cells in Segment OAM F4 circuit*/
8229 bpf_error("'oam4sc' supported only on raw ATM");
8230 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8231 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8236 /* Get all cells in End-to-End OAM F4 Circuit*/
8238 bpf_error("'oam4ec' supported only on raw ATM");
8239 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8240 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8245 /* Get all packets in connection Signalling Circuit */
8247 bpf_error("'sc' supported only on raw ATM");
8248 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8249 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8254 /* Get all packets in ILMI Circuit */
8256 bpf_error("'ilmic' supported only on raw ATM");
8257 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8258 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8263 /* Get all LANE packets */
8265 bpf_error("'lane' supported only on raw ATM");
8266 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8269 * Arrange that all subsequent tests assume LANE
8270 * rather than LLC-encapsulated packets, and set
8271 * the offsets appropriately for LANE-encapsulated
8274 * We assume LANE means Ethernet, not Token Ring.
8276 PUSH_LINKHDR(DLT_EN10MB
, 0,
8277 off_payload
+ 2, /* Ethernet header */
8280 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8281 off_nl
= 0; /* Ethernet II */
8282 off_nl_nosnap
= 3; /* 802.3+802.2 */
8286 /* Get all LLC-encapsulated packets */
8288 bpf_error("'llc' supported only on raw ATM");
8289 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8290 linktype
= prevlinktype
;
8300 * Filtering for MTP2 messages based on li value
8301 * FISU, length is null
8302 * LSSU, length is 1 or 2
8303 * MSU, length is 3 or more
8304 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8307 gen_mtp2type_abbrev(type
)
8310 struct block
*b0
, *b1
;
8315 if ( (linktype
!= DLT_MTP2
) &&
8316 (linktype
!= DLT_ERF
) &&
8317 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8318 bpf_error("'fisu' supported only on MTP2");
8319 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8320 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8324 if ( (linktype
!= DLT_MTP2
) &&
8325 (linktype
!= DLT_ERF
) &&
8326 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8327 bpf_error("'lssu' supported only on MTP2");
8328 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8329 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8334 if ( (linktype
!= DLT_MTP2
) &&
8335 (linktype
!= DLT_ERF
) &&
8336 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8337 bpf_error("'msu' supported only on MTP2");
8338 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8342 if ( (linktype
!= DLT_MTP2
) &&
8343 (linktype
!= DLT_ERF
) &&
8344 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8345 bpf_error("'hfisu' supported only on MTP2_HSL");
8346 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8347 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8351 if ( (linktype
!= DLT_MTP2
) &&
8352 (linktype
!= DLT_ERF
) &&
8353 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8354 bpf_error("'hlssu' supported only on MTP2_HSL");
8355 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8356 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8361 if ( (linktype
!= DLT_MTP2
) &&
8362 (linktype
!= DLT_ERF
) &&
8363 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8364 bpf_error("'hmsu' supported only on MTP2_HSL");
8365 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8375 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8382 bpf_u_int32 val1
, val2
, val3
;
8383 u_int newoff_sio
=off_sio
;
8384 u_int newoff_opc
=off_opc
;
8385 u_int newoff_dpc
=off_dpc
;
8386 u_int newoff_sls
=off_sls
;
8388 switch (mtp3field
) {
8391 newoff_sio
+= 3; /* offset for MTP2_HSL */
8395 if (off_sio
== (u_int
)-1)
8396 bpf_error("'sio' supported only on SS7");
8397 /* sio coded on 1 byte so max value 255 */
8399 bpf_error("sio value %u too big; max value = 255",
8401 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8402 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8408 if (off_opc
== (u_int
)-1)
8409 bpf_error("'opc' supported only on SS7");
8410 /* opc coded on 14 bits so max value 16383 */
8412 bpf_error("opc value %u too big; max value = 16383",
8414 /* the following instructions are made to convert jvalue
8415 * to the form used to write opc in an ss7 message*/
8416 val1
= jvalue
& 0x00003c00;
8418 val2
= jvalue
& 0x000003fc;
8420 val3
= jvalue
& 0x00000003;
8422 jvalue
= val1
+ val2
+ val3
;
8423 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8424 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8432 if (off_dpc
== (u_int
)-1)
8433 bpf_error("'dpc' supported only on SS7");
8434 /* dpc coded on 14 bits so max value 16383 */
8436 bpf_error("dpc value %u too big; max value = 16383",
8438 /* the following instructions are made to convert jvalue
8439 * to the forme used to write dpc in an ss7 message*/
8440 val1
= jvalue
& 0x000000ff;
8442 val2
= jvalue
& 0x00003f00;
8444 jvalue
= val1
+ val2
;
8445 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8446 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8452 if (off_sls
== (u_int
)-1)
8453 bpf_error("'sls' supported only on SS7");
8454 /* sls coded on 4 bits so max value 15 */
8456 bpf_error("sls value %u too big; max value = 15",
8458 /* the following instruction is made to convert jvalue
8459 * to the forme used to write sls in an ss7 message*/
8460 jvalue
= jvalue
<< 4;
8461 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8462 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8471 static struct block
*
8472 gen_msg_abbrev(type
)
8478 * Q.2931 signalling protocol messages for handling virtual circuits
8479 * establishment and teardown
8484 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8488 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8492 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8496 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8500 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8503 case A_RELEASE_DONE
:
8504 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8514 gen_atmmulti_abbrev(type
)
8517 struct block
*b0
, *b1
;
8523 bpf_error("'oam' supported only on raw ATM");
8524 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8529 bpf_error("'oamf4' supported only on raw ATM");
8531 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8532 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8534 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8540 * Get Q.2931 signalling messages for switched
8541 * virtual connection
8544 bpf_error("'connectmsg' supported only on raw ATM");
8545 b0
= gen_msg_abbrev(A_SETUP
);
8546 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8548 b0
= gen_msg_abbrev(A_CONNECT
);
8550 b0
= gen_msg_abbrev(A_CONNECTACK
);
8552 b0
= gen_msg_abbrev(A_RELEASE
);
8554 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8556 b0
= gen_atmtype_abbrev(A_SC
);
8562 bpf_error("'metaconnect' supported only on raw ATM");
8563 b0
= gen_msg_abbrev(A_SETUP
);
8564 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8566 b0
= gen_msg_abbrev(A_CONNECT
);
8568 b0
= gen_msg_abbrev(A_RELEASE
);
8570 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8572 b0
= gen_atmtype_abbrev(A_METAC
);