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 * Value passed to gen_load_a() to indicate what the offset argument
167 * is relative to the beginning of.
170 OR_PACKET
, /* full packet data */
171 OR_LINK
, /* link-layer header */
172 OR_LINKPL
, /* link-layer payload */
173 OR_NET
, /* network-layer header */
174 OR_NET_NOSNAP
, /* network-layer header, with no SNAP header at the link layer */
175 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
176 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
181 * As errors are handled by a longjmp, anything allocated must be freed
182 * in the longjmp handler, so it must be reachable from that handler.
183 * One thing that's allocated is the result of pcap_nametoaddrinfo();
184 * it must be freed with freeaddrinfo(). This variable points to any
185 * addrinfo structure that would need to be freed.
187 static struct addrinfo
*ai
;
191 * We divy out chunks of memory rather than call malloc each time so
192 * we don't have to worry about leaking memory. It's probably
193 * not a big deal if all this memory was wasted but if this ever
194 * goes into a library that would probably not be a good idea.
196 * XXX - this *is* in a library....
199 #define CHUNK0SIZE 1024
205 static struct chunk chunks
[NCHUNKS
];
206 static int cur_chunk
;
208 static void *newchunk(u_int
);
209 static void freechunks(void);
210 static inline struct block
*new_block(int);
211 static inline struct slist
*new_stmt(int);
212 static struct block
*gen_retblk(int);
213 static inline void syntax(void);
215 static void backpatch(struct block
*, struct block
*);
216 static void merge(struct block
*, struct block
*);
217 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
218 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
219 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
220 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
221 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
222 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
224 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
225 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
226 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
227 static struct slist
*gen_load_llrel(u_int
, u_int
);
228 static struct slist
*gen_load_linkplrel(u_int
, u_int
);
229 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
230 static struct slist
*gen_loadx_iphdrlen(void);
231 static struct block
*gen_uncond(int);
232 static inline struct block
*gen_true(void);
233 static inline struct block
*gen_false(void);
234 static struct block
*gen_ether_linktype(int);
235 static struct block
*gen_ipnet_linktype(int);
236 static struct block
*gen_linux_sll_linktype(int);
237 static struct slist
*gen_load_prism_llprefixlen(void);
238 static struct slist
*gen_load_avs_llprefixlen(void);
239 static struct slist
*gen_load_radiotap_llprefixlen(void);
240 static struct slist
*gen_load_ppi_llprefixlen(void);
241 static void insert_compute_vloffsets(struct block
*);
242 static struct slist
*gen_llprefixlen(void);
243 static struct slist
*gen_off_linkpl(void);
244 static int ethertype_to_ppptype(int);
245 static struct block
*gen_linktype(int);
246 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
247 static struct block
*gen_llc_linktype(int);
248 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
250 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
252 static struct block
*gen_ahostop(const u_char
*, int);
253 static struct block
*gen_ehostop(const u_char
*, int);
254 static struct block
*gen_fhostop(const u_char
*, int);
255 static struct block
*gen_thostop(const u_char
*, int);
256 static struct block
*gen_wlanhostop(const u_char
*, int);
257 static struct block
*gen_ipfchostop(const u_char
*, int);
258 static struct block
*gen_dnhostop(bpf_u_int32
, int);
259 static struct block
*gen_mpls_linktype(int);
260 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
262 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
265 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
267 static struct block
*gen_ipfrag(void);
268 static struct block
*gen_portatom(int, bpf_int32
);
269 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
270 static struct block
*gen_portatom6(int, bpf_int32
);
271 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
272 struct block
*gen_portop(int, int, int);
273 static struct block
*gen_port(int, int, int);
274 struct block
*gen_portrangeop(int, int, int, int);
275 static struct block
*gen_portrange(int, int, int, int);
276 struct block
*gen_portop6(int, int, int);
277 static struct block
*gen_port6(int, int, int);
278 struct block
*gen_portrangeop6(int, int, int, int);
279 static struct block
*gen_portrange6(int, int, int, int);
280 static int lookup_proto(const char *, int);
281 static struct block
*gen_protochain(int, int, int);
282 static struct block
*gen_proto(int, int, int);
283 static struct slist
*xfer_to_x(struct arth
*);
284 static struct slist
*xfer_to_a(struct arth
*);
285 static struct block
*gen_mac_multicast(int);
286 static struct block
*gen_len(int, int);
287 static struct block
*gen_check_802_11_data_frame(void);
289 static struct block
*gen_ppi_dlt_check(void);
290 static struct block
*gen_msg_abbrev(int type
);
301 /* XXX Round up to nearest long. */
302 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
304 /* XXX Round up to structure boundary. */
308 cp
= &chunks
[cur_chunk
];
309 if (n
> cp
->n_left
) {
310 ++cp
, k
= ++cur_chunk
;
312 bpf_error("out of memory");
313 size
= CHUNK0SIZE
<< k
;
314 cp
->m
= (void *)malloc(size
);
316 bpf_error("out of memory");
317 memset((char *)cp
->m
, 0, size
);
320 bpf_error("out of memory");
323 return (void *)((char *)cp
->m
+ cp
->n_left
);
332 for (i
= 0; i
< NCHUNKS
; ++i
)
333 if (chunks
[i
].m
!= NULL
) {
340 * A strdup whose allocations are freed after code generation is over.
344 register const char *s
;
346 int n
= strlen(s
) + 1;
347 char *cp
= newchunk(n
);
353 static inline struct block
*
359 p
= (struct block
*)newchunk(sizeof(*p
));
366 static inline struct slist
*
372 p
= (struct slist
*)newchunk(sizeof(*p
));
378 static struct block
*
382 struct block
*b
= new_block(BPF_RET
|BPF_K
);
391 bpf_error("syntax error in filter expression");
394 static bpf_u_int32 netmask
;
399 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
400 const char *buf
, int optimize
, bpf_u_int32 mask
)
403 const char * volatile xbuf
= buf
;
408 * XXX - single-thread this code path with pthread calls on
409 * UN*X, if the platform supports pthreads? If that requires
410 * a separate -lpthread, we might not want to do that.
413 extern int wsockinit (void);
419 EnterCriticalSection(&g_PcapCompileCriticalSection
);
423 * If this pcap_t hasn't been activated, it doesn't have a
424 * link-layer type, so we can't use it.
427 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
428 "not-yet-activated pcap_t passed to pcap_compile");
438 if (setjmp(top_ctx
)) {
453 snaplen
= pcap_snapshot(p
);
455 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
456 "snaplen of 0 rejects all packets");
461 lex_init(xbuf
? xbuf
: "");
469 root
= gen_retblk(snaplen
);
471 if (optimize
&& !no_optimize
) {
474 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
475 bpf_error("expression rejects all packets");
477 program
->bf_insns
= icode_to_fcode(root
, &len
);
478 program
->bf_len
= len
;
483 rc
= 0; /* We're all okay */
488 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
495 * entry point for using the compiler with no pcap open
496 * pass in all the stuff that is needed explicitly instead.
499 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
500 struct bpf_program
*program
,
501 const char *buf
, int optimize
, bpf_u_int32 mask
)
506 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
509 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
515 * Clean up a "struct bpf_program" by freeing all the memory allocated
519 pcap_freecode(struct bpf_program
*program
)
522 if (program
->bf_insns
!= NULL
) {
523 free((char *)program
->bf_insns
);
524 program
->bf_insns
= NULL
;
529 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
530 * which of the jt and jf fields has been resolved and which is a pointer
531 * back to another unresolved block (or nil). At least one of the fields
532 * in each block is already resolved.
535 backpatch(list
, target
)
536 struct block
*list
, *target
;
553 * Merge the lists in b0 and b1, using the 'sense' field to indicate
554 * which of jt and jf is the link.
558 struct block
*b0
, *b1
;
560 register struct block
**p
= &b0
;
562 /* Find end of list. */
564 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
566 /* Concatenate the lists. */
574 struct block
*ppi_dlt_check
;
577 * Insert before the statements of the first (root) block any
578 * statements needed to load the lengths of any variable-length
579 * headers into registers.
581 * XXX - a fancier strategy would be to insert those before the
582 * statements of all blocks that use those lengths and that
583 * have no predecessors that use them, so that we only compute
584 * the lengths if we need them. There might be even better
585 * approaches than that.
587 * However, those strategies would be more complicated, and
588 * as we don't generate code to compute a length if the
589 * program has no tests that use the length, and as most
590 * tests will probably use those lengths, we would just
591 * postpone computing the lengths so that it's not done
592 * for tests that fail early, and it's not clear that's
595 insert_compute_vloffsets(p
->head
);
598 * For DLT_PPI captures, generate a check of the per-packet
599 * DLT value to make sure it's DLT_IEEE802_11.
601 ppi_dlt_check
= gen_ppi_dlt_check();
602 if (ppi_dlt_check
!= NULL
)
603 gen_and(ppi_dlt_check
, p
);
605 backpatch(p
, gen_retblk(snaplen
));
606 p
->sense
= !p
->sense
;
607 backpatch(p
, gen_retblk(0));
613 struct block
*b0
, *b1
;
615 backpatch(b0
, b1
->head
);
616 b0
->sense
= !b0
->sense
;
617 b1
->sense
= !b1
->sense
;
619 b1
->sense
= !b1
->sense
;
625 struct block
*b0
, *b1
;
627 b0
->sense
= !b0
->sense
;
628 backpatch(b0
, b1
->head
);
629 b0
->sense
= !b0
->sense
;
638 b
->sense
= !b
->sense
;
641 static struct block
*
642 gen_cmp(offrel
, offset
, size
, v
)
643 enum e_offrel offrel
;
647 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
650 static struct block
*
651 gen_cmp_gt(offrel
, offset
, size
, v
)
652 enum e_offrel offrel
;
656 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
659 static struct block
*
660 gen_cmp_ge(offrel
, offset
, size
, v
)
661 enum e_offrel offrel
;
665 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
668 static struct block
*
669 gen_cmp_lt(offrel
, offset
, size
, v
)
670 enum e_offrel offrel
;
674 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
677 static struct block
*
678 gen_cmp_le(offrel
, offset
, size
, v
)
679 enum e_offrel offrel
;
683 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
686 static struct block
*
687 gen_mcmp(offrel
, offset
, size
, v
, mask
)
688 enum e_offrel offrel
;
693 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
696 static struct block
*
697 gen_bcmp(offrel
, offset
, size
, v
)
698 enum e_offrel offrel
;
699 register u_int offset
, size
;
700 register const u_char
*v
;
702 register struct block
*b
, *tmp
;
706 register const u_char
*p
= &v
[size
- 4];
707 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
708 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
710 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
717 register const u_char
*p
= &v
[size
- 2];
718 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
720 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
727 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
736 * AND the field of size "size" at offset "offset" relative to the header
737 * specified by "offrel" with "mask", and compare it with the value "v"
738 * with the test specified by "jtype"; if "reverse" is true, the test
739 * should test the opposite of "jtype".
741 static struct block
*
742 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
743 enum e_offrel offrel
;
745 bpf_u_int32 offset
, size
, mask
, jtype
;
748 struct slist
*s
, *s2
;
751 s
= gen_load_a(offrel
, offset
, size
);
753 if (mask
!= 0xffffffff) {
754 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
759 b
= new_block(JMP(jtype
));
762 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
768 * Various code constructs need to know the layout of the data link
769 * layer. These variables give the necessary offsets from the beginning
770 * of the packet data.
774 * This is the offset of the beginning of the link-layer header from
775 * the beginning of the raw packet data.
777 * It's usually 0, except for 802.11 with a fixed-length radio header.
778 * (For 802.11 with a variable-length radio header, we have to generate
779 * code to compute that offset; off_ll is 0 in that case.)
784 * If there's a variable-length header preceding the link-layer header,
785 * "reg_off_ll" is the register number for a register containing the
786 * length of that header, and therefore the offset of the link-layer
787 * header from the beginning of the raw packet data. Otherwise,
788 * "reg_off_ll" is -1.
790 static int reg_off_ll
;
793 * This is the offset of the beginning of the MAC-layer header from
794 * the beginning of the link-layer header.
795 * It's usually 0, except for ATM LANE, where it's the offset, relative
796 * to the beginning of the raw packet data, of the Ethernet header, and
797 * for Ethernet with various additional information.
799 static u_int off_mac
;
802 * The offset of the beginning of the link-layer payload, from the beginning
803 * of the raw packet data, is, in the general case, the sum of a variable
804 * value and a constant value; the variable value may be absent, in which
805 * case the offset is only the constant value, and the constant value may
806 * be zero, in which case the offset is only the variable value.
808 * off_linkpl_constant_part is the constant value.
810 * reg_off_linkpl is the register number for a register containing the
811 * variable value, and -1 otherwise.
813 * off_linkpl_is_variable is 1 if there's a variable part.
815 static u_int off_linkpl_constant_part
;
816 static int off_linkpl_is_variable
;
817 static int reg_off_linkpl
;
820 * "off_linktype" is the offset to information in the link-layer header
821 * giving the packet type. This offset is relative to the beginning
822 * of the link-layer header - i.e., it doesn't include off_ll - so
823 * loads with an offset that includes "off_linktype" should use
826 * For Ethernet, it's the offset of the Ethernet type field; this
827 * means that it must have a value that skips VLAN tags.
829 * For link-layer types that always use 802.2 headers, it's the
830 * offset of the LLC header; this means that it must have a value
831 * that skips VLAN tags.
833 * For PPP, it's the offset of the PPP type field.
835 * For Cisco HDLC, it's the offset of the CHDLC type field.
837 * For BSD loopback, it's the offset of the AF_ value.
839 * For Linux cooked sockets, it's the offset of the type field.
841 * It's set to -1 for no encapsulation, in which case, IP is assumed.
843 static u_int off_linktype
;
846 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
847 * checks to check the PPP header, assumed to follow a LAN-style link-
848 * layer header and a PPPoE session header.
850 static int is_pppoes
= 0;
853 * TRUE if the link layer includes an ATM pseudo-header.
855 static int is_atm
= 0;
858 * TRUE if "lane" appeared in the filter; it causes us to generate
859 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
861 static int is_lane
= 0;
864 * These are offsets for the ATM pseudo-header.
866 static u_int off_vpi
;
867 static u_int off_vci
;
868 static u_int off_proto
;
871 * These are offsets for the MTP2 fields.
874 static u_int off_li_hsl
;
877 * These are offsets for the MTP3 fields.
879 static u_int off_sio
;
880 static u_int off_opc
;
881 static u_int off_dpc
;
882 static u_int off_sls
;
885 * This is the offset of the first byte after the ATM pseudo_header,
886 * or -1 if there is no ATM pseudo-header.
888 static u_int off_payload
;
891 * These are offsets to the beginning of the network-layer header.
892 * They are relative to the beginning of the link-layer payload (i.e.,
893 * they don't include off_ll or off_linkpl).
895 * If the link layer never uses 802.2 LLC:
897 * "off_nl" and "off_nl_nosnap" are the same.
899 * If the link layer always uses 802.2 LLC:
901 * "off_nl" is the offset if there's a SNAP header following
904 * "off_nl_nosnap" is the offset if there's no SNAP header.
906 * If the link layer is Ethernet:
908 * "off_nl" is the offset if the packet is an Ethernet II packet
909 * (we assume no 802.3+802.2+SNAP);
911 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
912 * with an 802.2 header following it.
915 static u_int off_nl_nosnap
;
923 linktype
= pcap_datalink(p
);
924 pcap_fddipad
= p
->fddipad
;
927 * Assume it's not raw ATM with a pseudo-header, for now.
938 * And that we're not doing PPPoE.
943 * And assume we're not doing SS7.
953 * Also assume it's not 802.11.
956 off_linkpl_constant_part
= 0;
957 off_linkpl_is_variable
= 0;
959 label_stack_depth
= 0;
960 vlan_stack_depth
= 0;
969 off_linkpl_constant_part
= 6;
970 off_nl
= 0; /* XXX in reality, variable! */
971 off_nl_nosnap
= 0; /* no 802.2 LLC */
974 case DLT_ARCNET_LINUX
:
976 off_linkpl_constant_part
= 8;
977 off_nl
= 0; /* XXX in reality, variable! */
978 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 off_linkpl_constant_part
= 14; /* Ethernet header length */
984 off_nl
= 0; /* Ethernet II */
985 off_nl_nosnap
= 3; /* 802.3+802.2 */
990 * SLIP doesn't have a link level type. The 16 byte
991 * header is hacked into our SLIP driver.
994 off_linkpl_constant_part
= 16;
996 off_nl_nosnap
= 0; /* no 802.2 LLC */
1000 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1003 off_linkpl_constant_part
= 24;
1005 off_nl_nosnap
= 0; /* no 802.2 LLC */
1011 off_linkpl_constant_part
= 4;
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1018 off_linkpl_constant_part
= 12;
1020 off_nl_nosnap
= 0; /* no 802.2 LLC */
1025 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1026 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1028 off_linkpl_constant_part
= 4;
1030 off_nl_nosnap
= 0; /* no 802.2 LLC */
1035 * This does no include the Ethernet header, and
1036 * only covers session state.
1039 off_linkpl_constant_part
= 8;
1041 off_nl_nosnap
= 0; /* no 802.2 LLC */
1046 off_linkpl_constant_part
= 24;
1048 off_nl_nosnap
= 0; /* no 802.2 LLC */
1053 * FDDI doesn't really have a link-level type field.
1054 * We set "off_linktype" to the offset of the LLC header.
1056 * To check for Ethernet types, we assume that SSAP = SNAP
1057 * is being used and pick out the encapsulated Ethernet type.
1058 * XXX - should we generate code to check for SNAP?
1061 off_linktype
+= pcap_fddipad
;
1062 off_linkpl_constant_part
= 13; /* FDDI MAC header length */
1063 off_linkpl_constant_part
+= pcap_fddipad
;
1064 off_nl
= 8; /* 802.2+SNAP */
1065 off_nl_nosnap
= 3; /* 802.2 */
1070 * Token Ring doesn't really have a link-level type field.
1071 * We set "off_linktype" to the offset of the LLC header.
1073 * To check for Ethernet types, we assume that SSAP = SNAP
1074 * is being used and pick out the encapsulated Ethernet type.
1075 * XXX - should we generate code to check for SNAP?
1077 * XXX - the header is actually variable-length.
1078 * Some various Linux patched versions gave 38
1079 * as "off_linktype" and 40 as "off_nl"; however,
1080 * if a token ring packet has *no* routing
1081 * information, i.e. is not source-routed, the correct
1082 * values are 20 and 22, as they are in the vanilla code.
1084 * A packet is source-routed iff the uppermost bit
1085 * of the first byte of the source address, at an
1086 * offset of 8, has the uppermost bit set. If the
1087 * packet is source-routed, the total number of bytes
1088 * of routing information is 2 plus bits 0x1F00 of
1089 * the 16-bit value at an offset of 14 (shifted right
1090 * 8 - figure out which byte that is).
1093 off_linkpl_constant_part
= 14; /* Token Ring MAC header length */
1094 off_nl
= 8; /* 802.2+SNAP */
1095 off_nl_nosnap
= 3; /* 802.2 */
1098 case DLT_IEEE802_11
:
1099 case DLT_PRISM_HEADER
:
1100 case DLT_IEEE802_11_RADIO_AVS
:
1101 case DLT_IEEE802_11_RADIO
:
1103 * 802.11 doesn't really have a link-level type field.
1104 * We set "off_linktype" to the offset of the LLC header.
1106 * To check for Ethernet types, we assume that SSAP = SNAP
1107 * is being used and pick out the encapsulated Ethernet type.
1108 * XXX - should we generate code to check for SNAP?
1110 * We also handle variable-length radio headers here.
1111 * The Prism header is in theory variable-length, but in
1112 * practice it's always 144 bytes long. However, some
1113 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1114 * sometimes or always supply an AVS header, so we
1115 * have to check whether the radio header is a Prism
1116 * header or an AVS header, so, in practice, it's
1120 off_linkpl_constant_part
= 0; /* link-layer header is variable-length */
1121 off_linkpl_is_variable
= 1;
1122 off_nl
= 8; /* 802.2+SNAP */
1123 off_nl_nosnap
= 3; /* 802.2 */
1128 * At the moment we treat PPI the same way that we treat
1129 * normal Radiotap encoded packets. The difference is in
1130 * the function that generates the code at the beginning
1131 * to compute the header length. Since this code generator
1132 * of PPI supports bare 802.11 encapsulation only (i.e.
1133 * the encapsulated DLT should be DLT_IEEE802_11) we
1134 * generate code to check for this too.
1137 off_linkpl_constant_part
= 0; /* link-layer header is variable-length */
1138 off_linkpl_is_variable
= 1;
1139 off_nl
= 8; /* 802.2+SNAP */
1140 off_nl_nosnap
= 3; /* 802.2 */
1143 case DLT_ATM_RFC1483
:
1144 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1146 * assume routed, non-ISO PDUs
1147 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1149 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1150 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1151 * latter would presumably be treated the way PPPoE
1152 * should be, so you can do "pppoe and udp port 2049"
1153 * or "pppoa and tcp port 80" and have it check for
1154 * PPPo{A,E} and a PPP protocol of IP and....
1157 off_linkpl_constant_part
= 0; /* packet begins with LLC header */
1158 off_nl
= 8; /* 802.2+SNAP */
1159 off_nl_nosnap
= 3; /* 802.2 */
1164 * Full Frontal ATM; you get AALn PDUs with an ATM
1168 off_vpi
= SUNATM_VPI_POS
;
1169 off_vci
= SUNATM_VCI_POS
;
1170 off_proto
= PROTO_POS
;
1171 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1172 off_payload
= SUNATM_PKT_BEGIN_POS
;
1173 off_linktype
= off_payload
;
1174 off_linkpl_constant_part
= off_payload
; /* if LLC-encapsulated */
1175 off_nl
= 8; /* 802.2+SNAP */
1176 off_nl_nosnap
= 3; /* 802.2 */
1183 off_linkpl_constant_part
= 0;
1185 off_nl_nosnap
= 0; /* no 802.2 LLC */
1188 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1190 off_linkpl_constant_part
= 16;
1192 off_nl_nosnap
= 0; /* no 802.2 LLC */
1197 * LocalTalk does have a 1-byte type field in the LLAP header,
1198 * but really it just indicates whether there is a "short" or
1199 * "long" DDP packet following.
1202 off_linkpl_constant_part
= 0;
1204 off_nl_nosnap
= 0; /* no 802.2 LLC */
1207 case DLT_IP_OVER_FC
:
1209 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1210 * link-level type field. We set "off_linktype" to the
1211 * offset of the LLC header.
1213 * To check for Ethernet types, we assume that SSAP = SNAP
1214 * is being used and pick out the encapsulated Ethernet type.
1215 * XXX - should we generate code to check for SNAP? RFC
1216 * 2625 says SNAP should be used.
1219 off_linkpl_constant_part
= 16;
1220 off_nl
= 8; /* 802.2+SNAP */
1221 off_nl_nosnap
= 3; /* 802.2 */
1226 * XXX - we should set this to handle SNAP-encapsulated
1227 * frames (NLPID of 0x80).
1230 off_linkpl_constant_part
= 0;
1232 off_nl_nosnap
= 0; /* no 802.2 LLC */
1236 * the only BPF-interesting FRF.16 frames are non-control frames;
1237 * Frame Relay has a variable length link-layer
1238 * so lets start with offset 4 for now and increments later on (FIXME);
1242 off_linkpl_constant_part
= 0;
1244 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1247 case DLT_APPLE_IP_OVER_IEEE1394
:
1249 off_linkpl_constant_part
= 18;
1251 off_nl_nosnap
= 0; /* no 802.2 LLC */
1254 case DLT_SYMANTEC_FIREWALL
:
1256 off_linkpl_constant_part
= 44;
1257 off_nl
= 0; /* Ethernet II */
1258 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1261 #ifdef HAVE_NET_PFVAR_H
1264 off_linkpl_constant_part
= PFLOG_HDRLEN
;
1266 off_nl_nosnap
= 0; /* no 802.2 LLC */
1270 case DLT_JUNIPER_MFR
:
1271 case DLT_JUNIPER_MLFR
:
1272 case DLT_JUNIPER_MLPPP
:
1273 case DLT_JUNIPER_PPP
:
1274 case DLT_JUNIPER_CHDLC
:
1275 case DLT_JUNIPER_FRELAY
:
1277 off_linkpl_constant_part
= 4;
1279 off_nl_nosnap
= -1; /* no 802.2 LLC */
1282 case DLT_JUNIPER_ATM1
:
1283 off_linktype
= 4; /* in reality variable between 4-8 */
1284 off_linkpl_constant_part
= 4; /* in reality variable between 4-8 */
1289 case DLT_JUNIPER_ATM2
:
1290 off_linktype
= 8; /* in reality variable between 8-12 */
1291 off_linkpl_constant_part
= 8; /* in reality variable between 8-12 */
1296 /* frames captured on a Juniper PPPoE service PIC
1297 * contain raw ethernet frames */
1298 case DLT_JUNIPER_PPPOE
:
1299 case DLT_JUNIPER_ETHER
:
1300 off_linkpl_constant_part
= 14;
1302 off_nl
= 18; /* Ethernet II */
1303 off_nl_nosnap
= 21; /* 802.3+802.2 */
1306 case DLT_JUNIPER_PPPOE_ATM
:
1308 off_linkpl_constant_part
= 6;
1310 off_nl_nosnap
= -1; /* no 802.2 LLC */
1313 case DLT_JUNIPER_GGSN
:
1315 off_linkpl_constant_part
= 12;
1317 off_nl_nosnap
= -1; /* no 802.2 LLC */
1320 case DLT_JUNIPER_ES
:
1322 off_linkpl_constant_part
= -1; /* not really a network layer but raw IP addresses */
1323 off_nl
= -1; /* not really a network layer but raw IP addresses */
1324 off_nl_nosnap
= -1; /* no 802.2 LLC */
1327 case DLT_JUNIPER_MONITOR
:
1329 off_linkpl_constant_part
= 12;
1330 off_nl
= 0; /* raw IP/IP6 header */
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_BACNET_MS_TP
:
1336 off_linkpl_constant_part
= -1;
1341 case DLT_JUNIPER_SERVICES
:
1343 off_linkpl_constant_part
= -1; /* L3 proto location dep. on cookie type */
1344 off_nl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl_nosnap
= -1; /* no 802.2 LLC */
1348 case DLT_JUNIPER_VP
:
1350 off_linkpl_constant_part
= -1;
1355 case DLT_JUNIPER_ST
:
1357 off_linkpl_constant_part
= -1;
1362 case DLT_JUNIPER_ISM
:
1364 off_linkpl_constant_part
= -1;
1369 case DLT_JUNIPER_VS
:
1370 case DLT_JUNIPER_SRX_E2E
:
1371 case DLT_JUNIPER_FIBRECHANNEL
:
1372 case DLT_JUNIPER_ATM_CEMIC
:
1374 off_linkpl_constant_part
= -1;
1387 off_linkpl_constant_part
= -1;
1392 case DLT_MTP2_WITH_PHDR
:
1400 off_linkpl_constant_part
= -1;
1413 off_linkpl_constant_part
= -1;
1420 off_linkpl_constant_part
= 4;
1427 * Currently, only raw "link[N:M]" filtering is supported.
1429 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1430 off_linkpl_constant_part
= -1;
1431 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1432 off_nl_nosnap
= -1; /* no 802.2 LLC */
1433 off_mac
= 1; /* step over the kiss length byte */
1438 off_linkpl_constant_part
= 24; /* ipnet header length */
1443 case DLT_NETANALYZER
:
1444 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1445 off_linktype
= 16; /* includes 4-byte pseudo-header */
1446 off_linkpl_constant_part
= 18; /* pseudo-header+Ethernet header length */
1447 off_nl
= 0; /* Ethernet II */
1448 off_nl_nosnap
= 3; /* 802.3+802.2 */
1451 case DLT_NETANALYZER_TRANSPARENT
:
1452 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1453 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1454 off_linkpl_constant_part
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1455 off_nl
= 0; /* Ethernet II */
1456 off_nl_nosnap
= 3; /* 802.3+802.2 */
1461 * For values in the range in which we've assigned new
1462 * DLT_ values, only raw "link[N:M]" filtering is supported.
1464 if (linktype
>= DLT_MATCHING_MIN
&&
1465 linktype
<= DLT_MATCHING_MAX
) {
1467 off_linkpl_constant_part
= -1;
1474 bpf_error("unknown data link type %d", linktype
);
1479 * Load a value relative to the beginning of the link-layer header.
1480 * The link-layer header doesn't necessarily begin at the beginning
1481 * of the packet data; there might be a variable-length prefix containing
1482 * radio information.
1484 static struct slist
*
1485 gen_load_llrel(offset
, size
)
1488 struct slist
*s
, *s2
;
1490 s
= gen_llprefixlen();
1493 * If "s" is non-null, it has code to arrange that the X register
1494 * contains the length of the prefix preceding the link-layer
1497 * Otherwise, the length of the prefix preceding the link-layer
1498 * header is "off_ll".
1502 * There's a variable-length prefix preceding the
1503 * link-layer header. "s" points to a list of statements
1504 * that put the length of that prefix into the X register.
1505 * do an indirect load, to use the X register as an offset.
1507 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1512 * There is no variable-length header preceding the
1513 * link-layer header; add in off_ll, which, if there's
1514 * a fixed-length header preceding the link-layer header,
1515 * is the length of that header.
1517 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1518 s
->s
.k
= offset
+ off_ll
;
1524 * Load a value relative to the beginning of the link-layer payload.
1526 static struct slist
*
1527 gen_load_linkplrel(offset
, size
)
1530 struct slist
*s
, *s2
;
1532 s
= gen_off_linkpl();
1535 * If s is non-null, the offset of the link-layer payload is
1536 * variable, and s points to a list of instructions that
1537 * arrange that the X register contains the variable part
1538 * of that offset. The sum of that variable part and
1539 * off_linkpl_constant_part is the offset.
1541 * Otherwise, the offset of the link-layer payload is constant,
1542 * and is in off_linkpl_constant_part.
1546 * The variable part of the offset of the link-layer payload
1547 * is in the X register. Do an indirect load, to use the X
1548 * register as part of the offset of the load.
1550 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1551 s2
->s
.k
= off_linkpl_constant_part
+ offset
;
1555 * The offset of the link-layer payload is constant,
1556 * and is in off_linkpl_constant_part; load the value
1557 * at that offset plus the specified offset.
1559 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1560 s
->s
.k
= off_linkpl_constant_part
+ offset
;
1566 * Load a value relative to the beginning of the specified header.
1568 static struct slist
*
1569 gen_load_a(offrel
, offset
, size
)
1570 enum e_offrel offrel
;
1573 struct slist
*s
, *s2
;
1578 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1583 s
= gen_load_llrel(offset
, size
);
1587 s
= gen_load_linkplrel(offset
, size
);
1591 s
= gen_load_linkplrel(off_nl
+ offset
, size
);
1595 s
= gen_load_linkplrel(off_nl_nosnap
+ offset
, size
);
1600 * Load the X register with the length of the IPv4 header
1601 * (plus the offset of the link-layer header, if it's
1602 * preceded by a variable-length header such as a radio
1603 * header), in bytes.
1605 s
= gen_loadx_iphdrlen();
1608 * Load the item at {offset of the link-layer payload} +
1609 * {offset, relative to the start of the link-layer
1610 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1611 * {specified offset}.
1613 * If the offset of the link-layer payload is variable,
1614 * the variable part of that offset is included in the
1615 * value in the X register, and we include the constant
1616 * part in the offset of the load.
1618 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1619 s2
->s
.k
= off_linkpl_constant_part
+ off_nl
+ offset
;
1624 s
= gen_load_linkplrel(off_nl
+ 40 + offset
, size
);
1635 * Generate code to load into the X register the sum of the length of
1636 * the IPv4 header and the variable part of the offset of the link-layer
1639 static struct slist
*
1640 gen_loadx_iphdrlen()
1642 struct slist
*s
, *s2
;
1644 s
= gen_off_linkpl();
1647 * The offset of the link-layer payload has a variable
1648 * part. "s" points to a list of statements that put
1649 * the variable part of that offset into the X register.
1651 * The 4*([k]&0xf) addressing mode can't be used, as we
1652 * don't have a constant offset, so we have to load the
1653 * value in question into the A register and add to it
1654 * the value from the X register.
1656 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1659 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1662 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1667 * The A register now contains the length of the IP header.
1668 * We need to add to it the variable part of the offset of
1669 * the link-layer payload, which is still in the X
1670 * register, and move the result into the X register.
1672 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1673 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1676 * The offset of the link-layer payload is a constant,
1677 * so no code was generated to load the (non-existent)
1678 * variable part of that offset.
1680 * This means we can use the 4*([k]&0xf) addressing
1681 * mode. Load the length of the IPv4 header, which
1682 * is at an offset of off_nl from the beginning of
1683 * the link-layer payload, and thus at an offset of
1684 * off_linkpl_constant_part + off_nl from the beginning
1685 * of the raw packet data, using that addressing mode.
1687 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1688 s
->s
.k
= off_linkpl_constant_part
+ off_nl
;
1693 static struct block
*
1700 s
= new_stmt(BPF_LD
|BPF_IMM
);
1702 b
= new_block(JMP(BPF_JEQ
));
1708 static inline struct block
*
1711 return gen_uncond(1);
1714 static inline struct block
*
1717 return gen_uncond(0);
1721 * Byte-swap a 32-bit number.
1722 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1723 * big-endian platforms.)
1725 #define SWAPLONG(y) \
1726 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1729 * Generate code to match a particular packet type.
1731 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1732 * value, if <= ETHERMTU. We use that to determine whether to
1733 * match the type/length field or to check the type/length field for
1734 * a value <= ETHERMTU to see whether it's a type field and then do
1735 * the appropriate test.
1737 static struct block
*
1738 gen_ether_linktype(proto
)
1741 struct block
*b0
, *b1
;
1747 case LLCSAP_NETBEUI
:
1749 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1750 * so we check the DSAP and SSAP.
1752 * LLCSAP_IP checks for IP-over-802.2, rather
1753 * than IP-over-Ethernet or IP-over-SNAP.
1755 * XXX - should we check both the DSAP and the
1756 * SSAP, like this, or should we check just the
1757 * DSAP, as we do for other types <= ETHERMTU
1758 * (i.e., other SAP values)?
1760 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1762 b1
= gen_cmp(OR_LINKPL
, 0, BPF_H
, (bpf_int32
)
1763 ((proto
<< 8) | proto
));
1771 * Ethernet_II frames, which are Ethernet
1772 * frames with a frame type of ETHERTYPE_IPX;
1774 * Ethernet_802.3 frames, which are 802.3
1775 * frames (i.e., the type/length field is
1776 * a length field, <= ETHERMTU, rather than
1777 * a type field) with the first two bytes
1778 * after the Ethernet/802.3 header being
1781 * Ethernet_802.2 frames, which are 802.3
1782 * frames with an 802.2 LLC header and
1783 * with the IPX LSAP as the DSAP in the LLC
1786 * Ethernet_SNAP frames, which are 802.3
1787 * frames with an LLC header and a SNAP
1788 * header and with an OUI of 0x000000
1789 * (encapsulated Ethernet) and a protocol
1790 * ID of ETHERTYPE_IPX in the SNAP header.
1792 * XXX - should we generate the same code both
1793 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1797 * This generates code to check both for the
1798 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1800 b0
= gen_cmp(OR_LINKPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1801 b1
= gen_cmp(OR_LINKPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1805 * Now we add code to check for SNAP frames with
1806 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1808 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1812 * Now we generate code to check for 802.3
1813 * frames in general.
1815 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1819 * Now add the check for 802.3 frames before the
1820 * check for Ethernet_802.2 and Ethernet_802.3,
1821 * as those checks should only be done on 802.3
1822 * frames, not on Ethernet frames.
1827 * Now add the check for Ethernet_II frames, and
1828 * do that before checking for the other frame
1831 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1832 (bpf_int32
)ETHERTYPE_IPX
);
1836 case ETHERTYPE_ATALK
:
1837 case ETHERTYPE_AARP
:
1839 * EtherTalk (AppleTalk protocols on Ethernet link
1840 * layer) may use 802.2 encapsulation.
1844 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1845 * we check for an Ethernet type field less than
1846 * 1500, which means it's an 802.3 length field.
1848 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1852 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1853 * SNAP packets with an organization code of
1854 * 0x080007 (Apple, for Appletalk) and a protocol
1855 * type of ETHERTYPE_ATALK (Appletalk).
1857 * 802.2-encapsulated ETHERTYPE_AARP packets are
1858 * SNAP packets with an organization code of
1859 * 0x000000 (encapsulated Ethernet) and a protocol
1860 * type of ETHERTYPE_AARP (Appletalk ARP).
1862 if (proto
== ETHERTYPE_ATALK
)
1863 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1864 else /* proto == ETHERTYPE_AARP */
1865 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1869 * Check for Ethernet encapsulation (Ethertalk
1870 * phase 1?); we just check for the Ethernet
1873 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1879 if (proto
<= ETHERMTU
) {
1881 * This is an LLC SAP value, so the frames
1882 * that match would be 802.2 frames.
1883 * Check that the frame is an 802.2 frame
1884 * (i.e., that the length/type field is
1885 * a length field, <= ETHERMTU) and
1886 * then check the DSAP.
1888 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1890 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1896 * This is an Ethernet type, so compare
1897 * the length/type field with it (if
1898 * the frame is an 802.2 frame, the length
1899 * field will be <= ETHERMTU, and, as
1900 * "proto" is > ETHERMTU, this test
1901 * will fail and the frame won't match,
1902 * which is what we want).
1904 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1911 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1912 * or IPv6 then we have an error.
1914 static struct block
*
1915 gen_ipnet_linktype(proto
)
1921 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1922 (bpf_int32
)IPH_AF_INET
);
1925 case ETHERTYPE_IPV6
:
1926 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1927 (bpf_int32
)IPH_AF_INET6
);
1938 * Generate code to match a particular packet type.
1940 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1941 * value, if <= ETHERMTU. We use that to determine whether to
1942 * match the type field or to check the type field for the special
1943 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1945 static struct block
*
1946 gen_linux_sll_linktype(proto
)
1949 struct block
*b0
, *b1
;
1955 case LLCSAP_NETBEUI
:
1957 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1958 * so we check the DSAP and SSAP.
1960 * LLCSAP_IP checks for IP-over-802.2, rather
1961 * than IP-over-Ethernet or IP-over-SNAP.
1963 * XXX - should we check both the DSAP and the
1964 * SSAP, like this, or should we check just the
1965 * DSAP, as we do for other types <= ETHERMTU
1966 * (i.e., other SAP values)?
1968 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1969 b1
= gen_cmp(OR_LINKPL
, 0, BPF_H
, (bpf_int32
)
1970 ((proto
<< 8) | proto
));
1976 * Ethernet_II frames, which are Ethernet
1977 * frames with a frame type of ETHERTYPE_IPX;
1979 * Ethernet_802.3 frames, which have a frame
1980 * type of LINUX_SLL_P_802_3;
1982 * Ethernet_802.2 frames, which are 802.3
1983 * frames with an 802.2 LLC header (i.e, have
1984 * a frame type of LINUX_SLL_P_802_2) and
1985 * with the IPX LSAP as the DSAP in the LLC
1988 * Ethernet_SNAP frames, which are 802.3
1989 * frames with an LLC header and a SNAP
1990 * header and with an OUI of 0x000000
1991 * (encapsulated Ethernet) and a protocol
1992 * ID of ETHERTYPE_IPX in the SNAP header.
1994 * First, do the checks on LINUX_SLL_P_802_2
1995 * frames; generate the check for either
1996 * Ethernet_802.2 or Ethernet_SNAP frames, and
1997 * then put a check for LINUX_SLL_P_802_2 frames
2000 b0
= gen_cmp(OR_LINKPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2001 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2003 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2007 * Now check for 802.3 frames and OR that with
2008 * the previous test.
2010 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2014 * Now add the check for Ethernet_II frames, and
2015 * do that before checking for the other frame
2018 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2019 (bpf_int32
)ETHERTYPE_IPX
);
2023 case ETHERTYPE_ATALK
:
2024 case ETHERTYPE_AARP
:
2026 * EtherTalk (AppleTalk protocols on Ethernet link
2027 * layer) may use 802.2 encapsulation.
2031 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2032 * we check for the 802.2 protocol type in the
2033 * "Ethernet type" field.
2035 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2038 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2039 * SNAP packets with an organization code of
2040 * 0x080007 (Apple, for Appletalk) and a protocol
2041 * type of ETHERTYPE_ATALK (Appletalk).
2043 * 802.2-encapsulated ETHERTYPE_AARP packets are
2044 * SNAP packets with an organization code of
2045 * 0x000000 (encapsulated Ethernet) and a protocol
2046 * type of ETHERTYPE_AARP (Appletalk ARP).
2048 if (proto
== ETHERTYPE_ATALK
)
2049 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2050 else /* proto == ETHERTYPE_AARP */
2051 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2055 * Check for Ethernet encapsulation (Ethertalk
2056 * phase 1?); we just check for the Ethernet
2059 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2065 if (proto
<= ETHERMTU
) {
2067 * This is an LLC SAP value, so the frames
2068 * that match would be 802.2 frames.
2069 * Check for the 802.2 protocol type
2070 * in the "Ethernet type" field, and
2071 * then check the DSAP.
2073 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2075 b1
= gen_cmp(OR_LINK
, off_linkpl_constant_part
, BPF_B
,
2081 * This is an Ethernet type, so compare
2082 * the length/type field with it (if
2083 * the frame is an 802.2 frame, the length
2084 * field will be <= ETHERMTU, and, as
2085 * "proto" is > ETHERMTU, this test
2086 * will fail and the frame won't match,
2087 * which is what we want).
2089 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2095 static struct slist
*
2096 gen_load_prism_llprefixlen()
2098 struct slist
*s1
, *s2
;
2099 struct slist
*sjeq_avs_cookie
;
2100 struct slist
*sjcommon
;
2103 * This code is not compatible with the optimizer, as
2104 * we are generating jmp instructions within a normal
2105 * slist of instructions
2110 * Generate code to load the length of the radio header into
2111 * the register assigned to hold that length, if one has been
2112 * assigned. (If one hasn't been assigned, no code we've
2113 * generated uses that prefix, so we don't need to generate any
2116 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2117 * or always use the AVS header rather than the Prism header.
2118 * We load a 4-byte big-endian value at the beginning of the
2119 * raw packet data, and see whether, when masked with 0xFFFFF000,
2120 * it's equal to 0x80211000. If so, that indicates that it's
2121 * an AVS header (the masked-out bits are the version number).
2122 * Otherwise, it's a Prism header.
2124 * XXX - the Prism header is also, in theory, variable-length,
2125 * but no known software generates headers that aren't 144
2128 if (reg_off_ll
!= -1) {
2132 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2136 * AND it with 0xFFFFF000.
2138 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2139 s2
->s
.k
= 0xFFFFF000;
2143 * Compare with 0x80211000.
2145 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2146 sjeq_avs_cookie
->s
.k
= 0x80211000;
2147 sappend(s1
, sjeq_avs_cookie
);
2152 * The 4 bytes at an offset of 4 from the beginning of
2153 * the AVS header are the length of the AVS header.
2154 * That field is big-endian.
2156 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2159 sjeq_avs_cookie
->s
.jt
= s2
;
2162 * Now jump to the code to allocate a register
2163 * into which to save the header length and
2164 * store the length there. (The "jump always"
2165 * instruction needs to have the k field set;
2166 * it's added to the PC, so, as we're jumping
2167 * over a single instruction, it should be 1.)
2169 sjcommon
= new_stmt(JMP(BPF_JA
));
2171 sappend(s1
, sjcommon
);
2174 * Now for the code that handles the Prism header.
2175 * Just load the length of the Prism header (144)
2176 * into the A register. Have the test for an AVS
2177 * header branch here if we don't have an AVS header.
2179 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2182 sjeq_avs_cookie
->s
.jf
= s2
;
2185 * Now allocate a register to hold that value and store
2186 * it. The code for the AVS header will jump here after
2187 * loading the length of the AVS header.
2189 s2
= new_stmt(BPF_ST
);
2190 s2
->s
.k
= reg_off_ll
;
2192 sjcommon
->s
.jf
= s2
;
2195 * Now move it into the X register.
2197 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2205 static struct slist
*
2206 gen_load_avs_llprefixlen()
2208 struct slist
*s1
, *s2
;
2211 * Generate code to load the length of the AVS header into
2212 * the register assigned to hold that length, if one has been
2213 * assigned. (If one hasn't been assigned, no code we've
2214 * generated uses that prefix, so we don't need to generate any
2217 if (reg_off_ll
!= -1) {
2219 * The 4 bytes at an offset of 4 from the beginning of
2220 * the AVS header are the length of the AVS header.
2221 * That field is big-endian.
2223 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2227 * Now allocate a register to hold that value and store
2230 s2
= new_stmt(BPF_ST
);
2231 s2
->s
.k
= reg_off_ll
;
2235 * Now move it into the X register.
2237 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2245 static struct slist
*
2246 gen_load_radiotap_llprefixlen()
2248 struct slist
*s1
, *s2
;
2251 * Generate code to load the length of the radiotap header into
2252 * the register assigned to hold that length, if one has been
2253 * assigned. (If one hasn't been assigned, no code we've
2254 * generated uses that prefix, so we don't need to generate any
2257 if (reg_off_ll
!= -1) {
2259 * The 2 bytes at offsets of 2 and 3 from the beginning
2260 * of the radiotap header are the length of the radiotap
2261 * header; unfortunately, it's little-endian, so we have
2262 * to load it a byte at a time and construct the value.
2266 * Load the high-order byte, at an offset of 3, shift it
2267 * left a byte, and put the result in the X register.
2269 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2271 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2274 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2278 * Load the next byte, at an offset of 2, and OR the
2279 * value from the X register into it.
2281 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2284 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2288 * Now allocate a register to hold that value and store
2291 s2
= new_stmt(BPF_ST
);
2292 s2
->s
.k
= reg_off_ll
;
2296 * Now move it into the X register.
2298 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2307 * At the moment we treat PPI as normal Radiotap encoded
2308 * packets. The difference is in the function that generates
2309 * the code at the beginning to compute the header length.
2310 * Since this code generator of PPI supports bare 802.11
2311 * encapsulation only (i.e. the encapsulated DLT should be
2312 * DLT_IEEE802_11) we generate code to check for this too;
2313 * that's done in finish_parse().
2315 static struct slist
*
2316 gen_load_ppi_llprefixlen()
2318 struct slist
*s1
, *s2
;
2321 * Generate code to load the length of the radiotap header
2322 * into the register assigned to hold that length, if one has
2325 if (reg_off_ll
!= -1) {
2327 * The 2 bytes at offsets of 2 and 3 from the beginning
2328 * of the radiotap header are the length of the radiotap
2329 * header; unfortunately, it's little-endian, so we have
2330 * to load it a byte at a time and construct the value.
2334 * Load the high-order byte, at an offset of 3, shift it
2335 * left a byte, and put the result in the X register.
2337 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2339 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2342 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2346 * Load the next byte, at an offset of 2, and OR the
2347 * value from the X register into it.
2349 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2352 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2356 * Now allocate a register to hold that value and store
2359 s2
= new_stmt(BPF_ST
);
2360 s2
->s
.k
= reg_off_ll
;
2364 * Now move it into the X register.
2366 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2375 * Load a value relative to the beginning of the link-layer header after the 802.11
2376 * header, i.e. LLC_SNAP.
2377 * The link-layer header doesn't necessarily begin at the beginning
2378 * of the packet data; there might be a variable-length prefix containing
2379 * radio information.
2381 static struct slist
*
2382 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2385 struct slist
*sjset_data_frame_1
;
2386 struct slist
*sjset_data_frame_2
;
2387 struct slist
*sjset_qos
;
2388 struct slist
*sjset_radiotap_flags
;
2389 struct slist
*sjset_radiotap_tsft
;
2390 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2391 struct slist
*s_roundup
;
2393 if (reg_off_linkpl
== -1) {
2395 * No register has been assigned to the offset of
2396 * the link-layer payload, which means nobody needs
2397 * it; don't bother computing it - just return
2398 * what we already have.
2404 * This code is not compatible with the optimizer, as
2405 * we are generating jmp instructions within a normal
2406 * slist of instructions
2411 * If "s" is non-null, it has code to arrange that the X register
2412 * contains the length of the prefix preceding the link-layer
2415 * Otherwise, the length of the prefix preceding the link-layer
2416 * header is "off_ll".
2420 * There is no variable-length header preceding the
2421 * link-layer header.
2423 * Load the length of the fixed-length prefix preceding
2424 * the link-layer header (if any) into the X register,
2425 * and store it in the reg_off_linkpl register.
2426 * That length is off_ll.
2428 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2433 * The X register contains the offset of the beginning of the
2434 * link-layer header; add 24, which is the minimum length
2435 * of the MAC header for a data frame, to that, and store it
2436 * in reg_off_linkpl, and then load the Frame Control field,
2437 * which is at the offset in the X register, with an indexed load.
2439 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2441 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2444 s2
= new_stmt(BPF_ST
);
2445 s2
->s
.k
= reg_off_linkpl
;
2448 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2453 * Check the Frame Control field to see if this is a data frame;
2454 * a data frame has the 0x08 bit (b3) in that field set and the
2455 * 0x04 bit (b2) clear.
2457 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2458 sjset_data_frame_1
->s
.k
= 0x08;
2459 sappend(s
, sjset_data_frame_1
);
2462 * If b3 is set, test b2, otherwise go to the first statement of
2463 * the rest of the program.
2465 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2466 sjset_data_frame_2
->s
.k
= 0x04;
2467 sappend(s
, sjset_data_frame_2
);
2468 sjset_data_frame_1
->s
.jf
= snext
;
2471 * If b2 is not set, this is a data frame; test the QoS bit.
2472 * Otherwise, go to the first statement of the rest of the
2475 sjset_data_frame_2
->s
.jt
= snext
;
2476 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2477 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2478 sappend(s
, sjset_qos
);
2481 * If it's set, add 2 to reg_off_linkpl, to skip the QoS
2483 * Otherwise, go to the first statement of the rest of the
2486 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2487 s2
->s
.k
= reg_off_linkpl
;
2489 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2492 s2
= new_stmt(BPF_ST
);
2493 s2
->s
.k
= reg_off_linkpl
;
2497 * If we have a radiotap header, look at it to see whether
2498 * there's Atheros padding between the MAC-layer header
2501 * Note: all of the fields in the radiotap header are
2502 * little-endian, so we byte-swap all of the values
2503 * we test against, as they will be loaded as big-endian
2506 if (linktype
== DLT_IEEE802_11_RADIO
) {
2508 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2509 * in the presence flag?
2511 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2515 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2516 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2517 sappend(s
, sjset_radiotap_flags
);
2520 * If not, skip all of this.
2522 sjset_radiotap_flags
->s
.jf
= snext
;
2525 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2527 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2528 new_stmt(JMP(BPF_JSET
));
2529 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2530 sappend(s
, sjset_radiotap_tsft
);
2533 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2534 * at an offset of 16 from the beginning of the raw packet
2535 * data (8 bytes for the radiotap header and 8 bytes for
2538 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2541 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2545 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2546 sjset_tsft_datapad
->s
.k
= 0x20;
2547 sappend(s
, sjset_tsft_datapad
);
2550 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2551 * at an offset of 8 from the beginning of the raw packet
2552 * data (8 bytes for the radiotap header).
2554 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2557 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2561 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2562 sjset_notsft_datapad
->s
.k
= 0x20;
2563 sappend(s
, sjset_notsft_datapad
);
2566 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2567 * set, round the length of the 802.11 header to
2568 * a multiple of 4. Do that by adding 3 and then
2569 * dividing by and multiplying by 4, which we do by
2572 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2573 s_roundup
->s
.k
= reg_off_linkpl
;
2574 sappend(s
, s_roundup
);
2575 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2578 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2581 s2
= new_stmt(BPF_ST
);
2582 s2
->s
.k
= reg_off_linkpl
;
2585 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2586 sjset_tsft_datapad
->s
.jf
= snext
;
2587 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2588 sjset_notsft_datapad
->s
.jf
= snext
;
2590 sjset_qos
->s
.jf
= snext
;
2596 insert_compute_vloffsets(b
)
2602 * For link-layer types that have a variable-length header
2603 * preceding the link-layer header, generate code to load
2604 * the offset of the link-layer header into the register
2605 * assigned to that offset, if any.
2609 case DLT_PRISM_HEADER
:
2610 s
= gen_load_prism_llprefixlen();
2613 case DLT_IEEE802_11_RADIO_AVS
:
2614 s
= gen_load_avs_llprefixlen();
2617 case DLT_IEEE802_11_RADIO
:
2618 s
= gen_load_radiotap_llprefixlen();
2622 s
= gen_load_ppi_llprefixlen();
2631 * For link-layer types that have a variable-length link-layer
2632 * header, generate code to load the offset of the link-layer
2633 * payload into the register assigned to that offset, if any.
2637 case DLT_IEEE802_11
:
2638 case DLT_PRISM_HEADER
:
2639 case DLT_IEEE802_11_RADIO_AVS
:
2640 case DLT_IEEE802_11_RADIO
:
2642 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2647 * If we have any offset-loading code, append all the
2648 * existing statements in the block to those statements,
2649 * and make the resulting list the list of statements
2653 sappend(s
, b
->stmts
);
2658 static struct block
*
2659 gen_ppi_dlt_check(void)
2661 struct slist
*s_load_dlt
;
2664 if (linktype
== DLT_PPI
)
2666 /* Create the statements that check for the DLT
2668 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2669 s_load_dlt
->s
.k
= 4;
2671 b
= new_block(JMP(BPF_JEQ
));
2673 b
->stmts
= s_load_dlt
;
2674 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2684 static struct slist
*
2685 gen_prism_llprefixlen(void)
2689 if (reg_off_ll
== -1) {
2691 * We haven't yet assigned a register for the length
2692 * of the radio header; allocate one.
2694 reg_off_ll
= alloc_reg();
2698 * Load the register containing the radio length
2699 * into the X register.
2701 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2702 s
->s
.k
= reg_off_ll
;
2706 static struct slist
*
2707 gen_avs_llprefixlen(void)
2711 if (reg_off_ll
== -1) {
2713 * We haven't yet assigned a register for the length
2714 * of the AVS header; allocate one.
2716 reg_off_ll
= alloc_reg();
2720 * Load the register containing the AVS length
2721 * into the X register.
2723 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2724 s
->s
.k
= reg_off_ll
;
2728 static struct slist
*
2729 gen_radiotap_llprefixlen(void)
2733 if (reg_off_ll
== -1) {
2735 * We haven't yet assigned a register for the length
2736 * of the radiotap header; allocate one.
2738 reg_off_ll
= alloc_reg();
2742 * Load the register containing the radiotap length
2743 * into the X register.
2745 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2746 s
->s
.k
= reg_off_ll
;
2751 * At the moment we treat PPI as normal Radiotap encoded
2752 * packets. The difference is in the function that generates
2753 * the code at the beginning to compute the header length.
2754 * Since this code generator of PPI supports bare 802.11
2755 * encapsulation only (i.e. the encapsulated DLT should be
2756 * DLT_IEEE802_11) we generate code to check for this too.
2758 static struct slist
*
2759 gen_ppi_llprefixlen(void)
2763 if (reg_off_ll
== -1) {
2765 * We haven't yet assigned a register for the length
2766 * of the radiotap header; allocate one.
2768 reg_off_ll
= alloc_reg();
2772 * Load the register containing the PPI length
2773 * into the X register.
2775 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2776 s
->s
.k
= reg_off_ll
;
2781 * Generate code to compute the link-layer header length, if necessary,
2782 * putting it into the X register, and to return either a pointer to a
2783 * "struct slist" for the list of statements in that code, or NULL if
2784 * no code is necessary.
2786 static struct slist
*
2787 gen_llprefixlen(void)
2791 case DLT_PRISM_HEADER
:
2792 return gen_prism_llprefixlen();
2794 case DLT_IEEE802_11_RADIO_AVS
:
2795 return gen_avs_llprefixlen();
2797 case DLT_IEEE802_11_RADIO
:
2798 return gen_radiotap_llprefixlen();
2801 return gen_ppi_llprefixlen();
2809 * Generate code to load the register containing the variable part of
2810 * the offset of the link-layer payload into the X register; if no
2811 * register for that offset has been allocated, allocate it first.
2812 * (The code to set that register will be generated later, but will
2813 * be placed earlier in the code sequence.)
2815 static struct slist
*
2816 gen_off_linkpl(void)
2820 if (off_linkpl_is_variable
) {
2821 if (reg_off_linkpl
== -1) {
2823 * We haven't yet assigned a register for the
2824 * variable part of the offset of the link-layer
2825 * payload; allocate one.
2827 reg_off_linkpl
= alloc_reg();
2831 * Load the register containing the variable part of the
2832 * offset of the link-layer payload into the X register.
2834 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2835 s
->s
.k
= reg_off_linkpl
;
2839 * That offset isn't variable, there's no variable part,
2840 * so we don't need to generate any code.
2847 * Map an Ethernet type to the equivalent PPP type.
2850 ethertype_to_ppptype(proto
)
2859 case ETHERTYPE_IPV6
:
2867 case ETHERTYPE_ATALK
:
2881 * I'm assuming the "Bridging PDU"s that go
2882 * over PPP are Spanning Tree Protocol
2896 * Generate code to match a particular packet type by matching the
2897 * link-layer type field or fields in the 802.2 LLC header.
2899 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2900 * value, if <= ETHERMTU.
2902 static struct block
*
2906 struct block
*b0
, *b1
, *b2
;
2907 const char *description
;
2909 /* are we checking MPLS-encapsulated packets? */
2910 if (label_stack_depth
> 0) {
2914 /* FIXME add other L3 proto IDs */
2915 return gen_mpls_linktype(Q_IP
);
2917 case ETHERTYPE_IPV6
:
2919 /* FIXME add other L3 proto IDs */
2920 return gen_mpls_linktype(Q_IPV6
);
2923 bpf_error("unsupported protocol over mpls");
2929 * Are we testing PPPoE packets?
2933 * The PPPoE session header is part of the
2934 * link-layer payload, so all references
2935 * should be relative to the beginning of
2940 * We use Ethernet protocol types inside libpcap;
2941 * map them to the corresponding PPP protocol types.
2943 proto
= ethertype_to_ppptype(proto
);
2944 return gen_cmp(OR_LINKPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2950 case DLT_NETANALYZER
:
2951 case DLT_NETANALYZER_TRANSPARENT
:
2952 return gen_ether_linktype(proto
);
2960 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2964 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2971 case DLT_IEEE802_11
:
2972 case DLT_PRISM_HEADER
:
2973 case DLT_IEEE802_11_RADIO_AVS
:
2974 case DLT_IEEE802_11_RADIO
:
2977 * Check that we have a data frame.
2979 b0
= gen_check_802_11_data_frame();
2982 * Now check for the specified link-layer type.
2984 b1
= gen_llc_linktype(proto
);
2992 * XXX - check for LLC frames.
2994 return gen_llc_linktype(proto
);
3000 * XXX - check for LLC PDUs, as per IEEE 802.5.
3002 return gen_llc_linktype(proto
);
3006 case DLT_ATM_RFC1483
:
3008 case DLT_IP_OVER_FC
:
3009 return gen_llc_linktype(proto
);
3015 * If "is_lane" is set, check for a LANE-encapsulated
3016 * version of this protocol, otherwise check for an
3017 * LLC-encapsulated version of this protocol.
3019 * We assume LANE means Ethernet, not Token Ring.
3023 * Check that the packet doesn't begin with an
3024 * LE Control marker. (We've already generated
3027 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3032 * Now generate an Ethernet test.
3034 b1
= gen_ether_linktype(proto
);
3039 * Check for LLC encapsulation and then check the
3042 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3043 b1
= gen_llc_linktype(proto
);
3051 return gen_linux_sll_linktype(proto
);
3056 case DLT_SLIP_BSDOS
:
3059 * These types don't provide any type field; packets
3060 * are always IPv4 or IPv6.
3062 * XXX - for IPv4, check for a version number of 4, and,
3063 * for IPv6, check for a version number of 6?
3068 /* Check for a version number of 4. */
3069 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3071 case ETHERTYPE_IPV6
:
3072 /* Check for a version number of 6. */
3073 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3076 return gen_false(); /* always false */
3083 * Raw IPv4, so no type field.
3085 if (proto
== ETHERTYPE_IP
)
3086 return gen_true(); /* always true */
3088 /* Checking for something other than IPv4; always false */
3095 * Raw IPv6, so no type field.
3097 if (proto
== ETHERTYPE_IPV6
)
3098 return gen_true(); /* always true */
3100 /* Checking for something other than IPv6; always false */
3107 case DLT_PPP_SERIAL
:
3110 * We use Ethernet protocol types inside libpcap;
3111 * map them to the corresponding PPP protocol types.
3113 proto
= ethertype_to_ppptype(proto
);
3114 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3120 * We use Ethernet protocol types inside libpcap;
3121 * map them to the corresponding PPP protocol types.
3127 * Also check for Van Jacobson-compressed IP.
3128 * XXX - do this for other forms of PPP?
3130 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3131 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3133 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3138 proto
= ethertype_to_ppptype(proto
);
3139 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3149 * For DLT_NULL, the link-layer header is a 32-bit
3150 * word containing an AF_ value in *host* byte order,
3151 * and for DLT_ENC, the link-layer header begins
3152 * with a 32-bit work containing an AF_ value in
3155 * In addition, if we're reading a saved capture file,
3156 * the host byte order in the capture may not be the
3157 * same as the host byte order on this machine.
3159 * For DLT_LOOP, the link-layer header is a 32-bit
3160 * word containing an AF_ value in *network* byte order.
3162 * XXX - AF_ values may, unfortunately, be platform-
3163 * dependent; for example, FreeBSD's AF_INET6 is 24
3164 * whilst NetBSD's and OpenBSD's is 26.
3166 * This means that, when reading a capture file, just
3167 * checking for our AF_INET6 value won't work if the
3168 * capture file came from another OS.
3177 case ETHERTYPE_IPV6
:
3184 * Not a type on which we support filtering.
3185 * XXX - support those that have AF_ values
3186 * #defined on this platform, at least?
3191 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3193 * The AF_ value is in host byte order, but
3194 * the BPF interpreter will convert it to
3195 * network byte order.
3197 * If this is a save file, and it's from a
3198 * machine with the opposite byte order to
3199 * ours, we byte-swap the AF_ value.
3201 * Then we run it through "htonl()", and
3202 * generate code to compare against the result.
3204 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3205 proto
= SWAPLONG(proto
);
3206 proto
= htonl(proto
);
3208 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3210 #ifdef HAVE_NET_PFVAR_H
3213 * af field is host byte order in contrast to the rest of
3216 if (proto
== ETHERTYPE_IP
)
3217 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3218 BPF_B
, (bpf_int32
)AF_INET
));
3219 else if (proto
== ETHERTYPE_IPV6
)
3220 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3221 BPF_B
, (bpf_int32
)AF_INET6
));
3226 #endif /* HAVE_NET_PFVAR_H */
3229 case DLT_ARCNET_LINUX
:
3231 * XXX should we check for first fragment if the protocol
3239 case ETHERTYPE_IPV6
:
3240 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3241 (bpf_int32
)ARCTYPE_INET6
));
3244 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3245 (bpf_int32
)ARCTYPE_IP
);
3246 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3247 (bpf_int32
)ARCTYPE_IP_OLD
);
3252 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3253 (bpf_int32
)ARCTYPE_ARP
);
3254 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3255 (bpf_int32
)ARCTYPE_ARP_OLD
);
3259 case ETHERTYPE_REVARP
:
3260 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3261 (bpf_int32
)ARCTYPE_REVARP
));
3263 case ETHERTYPE_ATALK
:
3264 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3265 (bpf_int32
)ARCTYPE_ATALK
));
3272 case ETHERTYPE_ATALK
:
3282 * XXX - assumes a 2-byte Frame Relay header with
3283 * DLCI and flags. What if the address is longer?
3289 * Check for the special NLPID for IP.
3291 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3293 case ETHERTYPE_IPV6
:
3295 * Check for the special NLPID for IPv6.
3297 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3301 * Check for several OSI protocols.
3303 * Frame Relay packets typically have an OSI
3304 * NLPID at the beginning; we check for each
3307 * What we check for is the NLPID and a frame
3308 * control field of UI, i.e. 0x03 followed
3311 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3312 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3313 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3325 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3327 case DLT_JUNIPER_MFR
:
3328 case DLT_JUNIPER_MLFR
:
3329 case DLT_JUNIPER_MLPPP
:
3330 case DLT_JUNIPER_ATM1
:
3331 case DLT_JUNIPER_ATM2
:
3332 case DLT_JUNIPER_PPPOE
:
3333 case DLT_JUNIPER_PPPOE_ATM
:
3334 case DLT_JUNIPER_GGSN
:
3335 case DLT_JUNIPER_ES
:
3336 case DLT_JUNIPER_MONITOR
:
3337 case DLT_JUNIPER_SERVICES
:
3338 case DLT_JUNIPER_ETHER
:
3339 case DLT_JUNIPER_PPP
:
3340 case DLT_JUNIPER_FRELAY
:
3341 case DLT_JUNIPER_CHDLC
:
3342 case DLT_JUNIPER_VP
:
3343 case DLT_JUNIPER_ST
:
3344 case DLT_JUNIPER_ISM
:
3345 case DLT_JUNIPER_VS
:
3346 case DLT_JUNIPER_SRX_E2E
:
3347 case DLT_JUNIPER_FIBRECHANNEL
:
3348 case DLT_JUNIPER_ATM_CEMIC
:
3350 /* just lets verify the magic number for now -
3351 * on ATM we may have up to 6 different encapsulations on the wire
3352 * and need a lot of heuristics to figure out that the payload
3355 * FIXME encapsulation specific BPF_ filters
3357 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3359 case DLT_BACNET_MS_TP
:
3360 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3363 return gen_ipnet_linktype(proto
);
3365 case DLT_LINUX_IRDA
:
3366 bpf_error("IrDA link-layer type filtering not implemented");
3369 bpf_error("DOCSIS link-layer type filtering not implemented");
3372 case DLT_MTP2_WITH_PHDR
:
3373 bpf_error("MTP2 link-layer type filtering not implemented");
3376 bpf_error("ERF link-layer type filtering not implemented");
3379 bpf_error("PFSYNC link-layer type filtering not implemented");
3381 case DLT_LINUX_LAPD
:
3382 bpf_error("LAPD link-layer type filtering not implemented");
3386 case DLT_USB_LINUX_MMAPPED
:
3387 bpf_error("USB link-layer type filtering not implemented");
3389 case DLT_BLUETOOTH_HCI_H4
:
3390 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3391 bpf_error("Bluetooth link-layer type filtering not implemented");
3394 case DLT_CAN_SOCKETCAN
:
3395 bpf_error("CAN link-layer type filtering not implemented");
3397 case DLT_IEEE802_15_4
:
3398 case DLT_IEEE802_15_4_LINUX
:
3399 case DLT_IEEE802_15_4_NONASK_PHY
:
3400 case DLT_IEEE802_15_4_NOFCS
:
3401 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3403 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3404 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3407 bpf_error("SITA link-layer type filtering not implemented");
3410 bpf_error("RAIF1 link-layer type filtering not implemented");
3413 bpf_error("IPMB link-layer type filtering not implemented");
3416 bpf_error("AX.25 link-layer type filtering not implemented");
3419 /* Using the fixed-size NFLOG header it is possible to tell only
3420 * the address family of the packet, other meaningful data is
3421 * either missing or behind TLVs.
3423 bpf_error("NFLOG link-layer type filtering not implemented");
3427 * Does this link-layer header type have a field
3428 * indicating the type of the next protocol? If
3429 * so, off_linktype will be the offset of that
3430 * field in the packet; if not, it will be -1.
3432 if (off_linktype
!= (u_int
)-1) {
3434 * Yes; assume it's an Ethernet type. (If
3435 * it's not, it needs to be handled specially
3438 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3441 * No; report an error.
3443 description
= pcap_datalink_val_to_description(linktype
);
3444 if (description
!= NULL
) {
3445 bpf_error("%s link-layer type filtering not implemented",
3448 bpf_error("DLT %u link-layer type filtering not implemented",
3457 * Check for an LLC SNAP packet with a given organization code and
3458 * protocol type; we check the entire contents of the 802.2 LLC and
3459 * snap headers, checking for DSAP and SSAP of SNAP and a control
3460 * field of 0x03 in the LLC header, and for the specified organization
3461 * code and protocol type in the SNAP header.
3463 static struct block
*
3464 gen_snap(orgcode
, ptype
)
3465 bpf_u_int32 orgcode
;
3468 u_char snapblock
[8];
3470 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3471 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3472 snapblock
[2] = 0x03; /* control = UI */
3473 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3474 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3475 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3476 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3477 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3478 return gen_bcmp(OR_LINKPL
, 0, 8, snapblock
);
3482 * Generate code to match frames with an LLC header.
3487 struct block
*b0
, *b1
;
3493 * We check for an Ethernet type field less than
3494 * 1500, which means it's an 802.3 length field.
3496 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
3500 * Now check for the purported DSAP and SSAP not being
3501 * 0xFF, to rule out NetWare-over-802.3.
3503 b1
= gen_cmp(OR_LINKPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3510 * We check for LLC traffic.
3512 b0
= gen_atmtype_abbrev(A_LLC
);
3515 case DLT_IEEE802
: /* Token Ring */
3517 * XXX - check for LLC frames.
3523 * XXX - check for LLC frames.
3527 case DLT_ATM_RFC1483
:
3529 * For LLC encapsulation, these are defined to have an
3532 * For VC encapsulation, they don't, but there's no
3533 * way to check for that; the protocol used on the VC
3534 * is negotiated out of band.
3538 case DLT_IEEE802_11
:
3539 case DLT_PRISM_HEADER
:
3540 case DLT_IEEE802_11_RADIO
:
3541 case DLT_IEEE802_11_RADIO_AVS
:
3544 * Check that we have a data frame.
3546 b0
= gen_check_802_11_data_frame();
3550 bpf_error("'llc' not supported for linktype %d", linktype
);
3558 struct block
*b0
, *b1
;
3562 * Check whether this is an LLC frame.
3567 * Load the control byte and test the low-order bit; it must
3568 * be clear for I frames.
3570 s
= gen_load_a(OR_LINKPL
, 2, BPF_B
);
3571 b1
= new_block(JMP(BPF_JSET
));
3582 struct block
*b0
, *b1
;
3585 * Check whether this is an LLC frame.
3590 * Now compare the low-order 2 bit of the control byte against
3591 * the appropriate value for S frames.
3593 b1
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3601 struct block
*b0
, *b1
;
3604 * Check whether this is an LLC frame.
3609 * Now compare the low-order 2 bit of the control byte against
3610 * the appropriate value for U frames.
3612 b1
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3618 gen_llc_s_subtype(bpf_u_int32 subtype
)
3620 struct block
*b0
, *b1
;
3623 * Check whether this is an LLC frame.
3628 * Now check for an S frame with the appropriate type.
3630 b1
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3636 gen_llc_u_subtype(bpf_u_int32 subtype
)
3638 struct block
*b0
, *b1
;
3641 * Check whether this is an LLC frame.
3646 * Now check for a U frame with the appropriate type.
3648 b1
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3654 * Generate code to match a particular packet type, for link-layer types
3655 * using 802.2 LLC headers.
3657 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3658 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3660 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3661 * value, if <= ETHERMTU. We use that to determine whether to
3662 * match the DSAP or both DSAP and LSAP or to check the OUI and
3663 * protocol ID in a SNAP header.
3665 static struct block
*
3666 gen_llc_linktype(proto
)
3670 * XXX - handle token-ring variable-length header.
3676 case LLCSAP_NETBEUI
:
3678 * XXX - should we check both the DSAP and the
3679 * SSAP, like this, or should we check just the
3680 * DSAP, as we do for other types <= ETHERMTU
3681 * (i.e., other SAP values)?
3683 return gen_cmp(OR_LINKPL
, 0, BPF_H
, (bpf_u_int32
)
3684 ((proto
<< 8) | proto
));
3688 * XXX - are there ever SNAP frames for IPX on
3689 * non-Ethernet 802.x networks?
3691 return gen_cmp(OR_LINKPL
, 0, BPF_B
,
3692 (bpf_int32
)LLCSAP_IPX
);
3694 case ETHERTYPE_ATALK
:
3696 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3697 * SNAP packets with an organization code of
3698 * 0x080007 (Apple, for Appletalk) and a protocol
3699 * type of ETHERTYPE_ATALK (Appletalk).
3701 * XXX - check for an organization code of
3702 * encapsulated Ethernet as well?
3704 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3708 * XXX - we don't have to check for IPX 802.3
3709 * here, but should we check for the IPX Ethertype?
3711 if (proto
<= ETHERMTU
) {
3713 * This is an LLC SAP value, so check
3716 return gen_cmp(OR_LINKPL
, 0, BPF_B
, (bpf_int32
)proto
);
3719 * This is an Ethernet type; we assume that it's
3720 * unlikely that it'll appear in the right place
3721 * at random, and therefore check only the
3722 * location that would hold the Ethernet type
3723 * in a SNAP frame with an organization code of
3724 * 0x000000 (encapsulated Ethernet).
3726 * XXX - if we were to check for the SNAP DSAP and
3727 * LSAP, as per XXX, and were also to check for an
3728 * organization code of 0x000000 (encapsulated
3729 * Ethernet), we'd do
3731 * return gen_snap(0x000000, proto);
3733 * here; for now, we don't, as per the above.
3734 * I don't know whether it's worth the extra CPU
3735 * time to do the right check or not.
3737 return gen_cmp(OR_LINKPL
, 6, BPF_H
, (bpf_int32
)proto
);
3742 static struct block
*
3743 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3747 u_int src_off
, dst_off
;
3749 struct block
*b0
, *b1
;
3763 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3764 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3770 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3771 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3778 b0
= gen_linktype(proto
);
3779 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3785 static struct block
*
3786 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3787 struct in6_addr
*addr
;
3788 struct in6_addr
*mask
;
3790 u_int src_off
, dst_off
;
3792 struct block
*b0
, *b1
;
3807 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3808 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3814 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3815 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3822 /* this order is important */
3823 a
= (u_int32_t
*)addr
;
3824 m
= (u_int32_t
*)mask
;
3825 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3826 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3828 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3830 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3832 b0
= gen_linktype(proto
);
3838 static struct block
*
3839 gen_ehostop(eaddr
, dir
)
3840 register const u_char
*eaddr
;
3843 register struct block
*b0
, *b1
;
3847 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3850 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3853 b0
= gen_ehostop(eaddr
, Q_SRC
);
3854 b1
= gen_ehostop(eaddr
, Q_DST
);
3860 b0
= gen_ehostop(eaddr
, Q_SRC
);
3861 b1
= gen_ehostop(eaddr
, Q_DST
);
3866 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3870 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3874 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3878 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3882 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3886 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3894 * Like gen_ehostop, but for DLT_FDDI
3896 static struct block
*
3897 gen_fhostop(eaddr
, dir
)
3898 register const u_char
*eaddr
;
3901 struct block
*b0
, *b1
;
3905 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3908 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3911 b0
= gen_fhostop(eaddr
, Q_SRC
);
3912 b1
= gen_fhostop(eaddr
, Q_DST
);
3918 b0
= gen_fhostop(eaddr
, Q_SRC
);
3919 b1
= gen_fhostop(eaddr
, Q_DST
);
3924 bpf_error("'addr1' is only supported on 802.11");
3928 bpf_error("'addr2' is only supported on 802.11");
3932 bpf_error("'addr3' is only supported on 802.11");
3936 bpf_error("'addr4' is only supported on 802.11");
3940 bpf_error("'ra' is only supported on 802.11");
3944 bpf_error("'ta' is only supported on 802.11");
3952 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3954 static struct block
*
3955 gen_thostop(eaddr
, dir
)
3956 register const u_char
*eaddr
;
3959 register struct block
*b0
, *b1
;
3963 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3966 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3969 b0
= gen_thostop(eaddr
, Q_SRC
);
3970 b1
= gen_thostop(eaddr
, Q_DST
);
3976 b0
= gen_thostop(eaddr
, Q_SRC
);
3977 b1
= gen_thostop(eaddr
, Q_DST
);
3982 bpf_error("'addr1' is only supported on 802.11");
3986 bpf_error("'addr2' is only supported on 802.11");
3990 bpf_error("'addr3' is only supported on 802.11");
3994 bpf_error("'addr4' is only supported on 802.11");
3998 bpf_error("'ra' is only supported on 802.11");
4002 bpf_error("'ta' is only supported on 802.11");
4010 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4011 * various 802.11 + radio headers.
4013 static struct block
*
4014 gen_wlanhostop(eaddr
, dir
)
4015 register const u_char
*eaddr
;
4018 register struct block
*b0
, *b1
, *b2
;
4019 register struct slist
*s
;
4021 #ifdef ENABLE_WLAN_FILTERING_PATCH
4024 * We need to disable the optimizer because the optimizer is buggy
4025 * and wipes out some LD instructions generated by the below
4026 * code to validate the Frame Control bits
4029 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4036 * For control frames, there is no SA.
4038 * For management frames, SA is at an
4039 * offset of 10 from the beginning of
4042 * For data frames, SA is at an offset
4043 * of 10 from the beginning of the packet
4044 * if From DS is clear, at an offset of
4045 * 16 from the beginning of the packet
4046 * if From DS is set and To DS is clear,
4047 * and an offset of 24 from the beginning
4048 * of the packet if From DS is set and To DS
4053 * Generate the tests to be done for data frames
4056 * First, check for To DS set, i.e. check "link[1] & 0x01".
4058 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4059 b1
= new_block(JMP(BPF_JSET
));
4060 b1
->s
.k
= 0x01; /* To DS */
4064 * If To DS is set, the SA is at 24.
4066 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4070 * Now, check for To DS not set, i.e. check
4071 * "!(link[1] & 0x01)".
4073 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4074 b2
= new_block(JMP(BPF_JSET
));
4075 b2
->s
.k
= 0x01; /* To DS */
4080 * If To DS is not set, the SA is at 16.
4082 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4086 * Now OR together the last two checks. That gives
4087 * the complete set of checks for data frames with
4093 * Now check for From DS being set, and AND that with
4094 * the ORed-together checks.
4096 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4097 b1
= new_block(JMP(BPF_JSET
));
4098 b1
->s
.k
= 0x02; /* From DS */
4103 * Now check for data frames with From DS not set.
4105 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4106 b2
= new_block(JMP(BPF_JSET
));
4107 b2
->s
.k
= 0x02; /* From DS */
4112 * If From DS isn't set, the SA is at 10.
4114 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4118 * Now OR together the checks for data frames with
4119 * From DS not set and for data frames with From DS
4120 * set; that gives the checks done for data frames.
4125 * Now check for a data frame.
4126 * I.e, check "link[0] & 0x08".
4128 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4129 b1
= new_block(JMP(BPF_JSET
));
4134 * AND that with the checks done for data frames.
4139 * If the high-order bit of the type value is 0, this
4140 * is a management frame.
4141 * I.e, check "!(link[0] & 0x08)".
4143 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4144 b2
= new_block(JMP(BPF_JSET
));
4150 * For management frames, the SA is at 10.
4152 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4156 * OR that with the checks done for data frames.
4157 * That gives the checks done for management and
4163 * If the low-order bit of the type value is 1,
4164 * this is either a control frame or a frame
4165 * with a reserved type, and thus not a
4168 * I.e., check "!(link[0] & 0x04)".
4170 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4171 b1
= new_block(JMP(BPF_JSET
));
4177 * AND that with the checks for data and management
4187 * For control frames, there is no DA.
4189 * For management frames, DA is at an
4190 * offset of 4 from the beginning of
4193 * For data frames, DA is at an offset
4194 * of 4 from the beginning of the packet
4195 * if To DS is clear and at an offset of
4196 * 16 from the beginning of the packet
4201 * Generate the tests to be done for data frames.
4203 * First, check for To DS set, i.e. "link[1] & 0x01".
4205 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4206 b1
= new_block(JMP(BPF_JSET
));
4207 b1
->s
.k
= 0x01; /* To DS */
4211 * If To DS is set, the DA is at 16.
4213 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4217 * Now, check for To DS not set, i.e. check
4218 * "!(link[1] & 0x01)".
4220 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4221 b2
= new_block(JMP(BPF_JSET
));
4222 b2
->s
.k
= 0x01; /* To DS */
4227 * If To DS is not set, the DA is at 4.
4229 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4233 * Now OR together the last two checks. That gives
4234 * the complete set of checks for data frames.
4239 * Now check for a data frame.
4240 * I.e, check "link[0] & 0x08".
4242 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4243 b1
= new_block(JMP(BPF_JSET
));
4248 * AND that with the checks done for data frames.
4253 * If the high-order bit of the type value is 0, this
4254 * is a management frame.
4255 * I.e, check "!(link[0] & 0x08)".
4257 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4258 b2
= new_block(JMP(BPF_JSET
));
4264 * For management frames, the DA is at 4.
4266 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4270 * OR that with the checks done for data frames.
4271 * That gives the checks done for management and
4277 * If the low-order bit of the type value is 1,
4278 * this is either a control frame or a frame
4279 * with a reserved type, and thus not a
4282 * I.e., check "!(link[0] & 0x04)".
4284 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4285 b1
= new_block(JMP(BPF_JSET
));
4291 * AND that with the checks for data and management
4299 * Not present in management frames; addr1 in other
4304 * If the high-order bit of the type value is 0, this
4305 * is a management frame.
4306 * I.e, check "(link[0] & 0x08)".
4308 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4309 b1
= new_block(JMP(BPF_JSET
));
4316 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4319 * AND that with the check of addr1.
4326 * Not present in management frames; addr2, if present,
4331 * Not present in CTS or ACK control frames.
4333 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4334 IEEE80211_FC0_TYPE_MASK
);
4336 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4337 IEEE80211_FC0_SUBTYPE_MASK
);
4339 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4340 IEEE80211_FC0_SUBTYPE_MASK
);
4346 * If the high-order bit of the type value is 0, this
4347 * is a management frame.
4348 * I.e, check "(link[0] & 0x08)".
4350 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4351 b1
= new_block(JMP(BPF_JSET
));
4356 * AND that with the check for frames other than
4357 * CTS and ACK frames.
4364 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4369 * XXX - add BSSID keyword?
4372 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4376 * Not present in CTS or ACK control frames.
4378 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4379 IEEE80211_FC0_TYPE_MASK
);
4381 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4382 IEEE80211_FC0_SUBTYPE_MASK
);
4384 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4385 IEEE80211_FC0_SUBTYPE_MASK
);
4389 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4395 * Not present in control frames.
4397 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4398 IEEE80211_FC0_TYPE_MASK
);
4400 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4406 * Present only if the direction mask has both "From DS"
4407 * and "To DS" set. Neither control frames nor management
4408 * frames should have both of those set, so we don't
4409 * check the frame type.
4411 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4412 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4413 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4418 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4419 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4425 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4426 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4435 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4436 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4437 * as the RFC states.)
4439 static struct block
*
4440 gen_ipfchostop(eaddr
, dir
)
4441 register const u_char
*eaddr
;
4444 register struct block
*b0
, *b1
;
4448 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4451 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4454 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4455 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4461 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4462 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4467 bpf_error("'addr1' is only supported on 802.11");
4471 bpf_error("'addr2' is only supported on 802.11");
4475 bpf_error("'addr3' is only supported on 802.11");
4479 bpf_error("'addr4' is only supported on 802.11");
4483 bpf_error("'ra' is only supported on 802.11");
4487 bpf_error("'ta' is only supported on 802.11");
4495 * This is quite tricky because there may be pad bytes in front of the
4496 * DECNET header, and then there are two possible data packet formats that
4497 * carry both src and dst addresses, plus 5 packet types in a format that
4498 * carries only the src node, plus 2 types that use a different format and
4499 * also carry just the src node.
4503 * Instead of doing those all right, we just look for data packets with
4504 * 0 or 1 bytes of padding. If you want to look at other packets, that
4505 * will require a lot more hacking.
4507 * To add support for filtering on DECNET "areas" (network numbers)
4508 * one would want to add a "mask" argument to this routine. That would
4509 * make the filter even more inefficient, although one could be clever
4510 * and not generate masking instructions if the mask is 0xFFFF.
4512 static struct block
*
4513 gen_dnhostop(addr
, dir
)
4517 struct block
*b0
, *b1
, *b2
, *tmp
;
4518 u_int offset_lh
; /* offset if long header is received */
4519 u_int offset_sh
; /* offset if short header is received */
4524 offset_sh
= 1; /* follows flags */
4525 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4529 offset_sh
= 3; /* follows flags, dstnode */
4530 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4534 /* Inefficient because we do our Calvinball dance twice */
4535 b0
= gen_dnhostop(addr
, Q_SRC
);
4536 b1
= gen_dnhostop(addr
, Q_DST
);
4542 /* Inefficient because we do our Calvinball dance twice */
4543 b0
= gen_dnhostop(addr
, Q_SRC
);
4544 b1
= gen_dnhostop(addr
, Q_DST
);
4549 bpf_error("ISO host filtering not implemented");
4554 b0
= gen_linktype(ETHERTYPE_DN
);
4555 /* Check for pad = 1, long header case */
4556 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4557 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4558 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4559 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4561 /* Check for pad = 0, long header case */
4562 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4563 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4566 /* Check for pad = 1, short header case */
4567 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4568 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4569 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4572 /* Check for pad = 0, short header case */
4573 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4574 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4578 /* Combine with test for linktype */
4584 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4585 * test the bottom-of-stack bit, and then check the version number
4586 * field in the IP header.
4588 static struct block
*
4589 gen_mpls_linktype(proto
)
4592 struct block
*b0
, *b1
;
4597 /* match the bottom-of-stack bit */
4598 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4599 /* match the IPv4 version number */
4600 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4605 /* match the bottom-of-stack bit */
4606 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4607 /* match the IPv4 version number */
4608 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4617 static struct block
*
4618 gen_host(addr
, mask
, proto
, dir
, type
)
4625 struct block
*b0
, *b1
;
4626 const char *typestr
;
4636 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4638 * Only check for non-IPv4 addresses if we're not
4639 * checking MPLS-encapsulated packets.
4641 if (label_stack_depth
== 0) {
4642 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4644 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4650 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4653 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4656 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4659 bpf_error("'tcp' modifier applied to %s", typestr
);
4662 bpf_error("'sctp' modifier applied to %s", typestr
);
4665 bpf_error("'udp' modifier applied to %s", typestr
);
4668 bpf_error("'icmp' modifier applied to %s", typestr
);
4671 bpf_error("'igmp' modifier applied to %s", typestr
);
4674 bpf_error("'igrp' modifier applied to %s", typestr
);
4677 bpf_error("'pim' modifier applied to %s", typestr
);
4680 bpf_error("'vrrp' modifier applied to %s", typestr
);
4683 bpf_error("'carp' modifier applied to %s", typestr
);
4686 bpf_error("ATALK host filtering not implemented");
4689 bpf_error("AARP host filtering not implemented");
4692 return gen_dnhostop(addr
, dir
);
4695 bpf_error("SCA host filtering not implemented");
4698 bpf_error("LAT host filtering not implemented");
4701 bpf_error("MOPDL host filtering not implemented");
4704 bpf_error("MOPRC host filtering not implemented");
4707 bpf_error("'ip6' modifier applied to ip host");
4710 bpf_error("'icmp6' modifier applied to %s", typestr
);
4713 bpf_error("'ah' modifier applied to %s", typestr
);
4716 bpf_error("'esp' modifier applied to %s", typestr
);
4719 bpf_error("ISO host filtering not implemented");
4722 bpf_error("'esis' modifier applied to %s", typestr
);
4725 bpf_error("'isis' modifier applied to %s", typestr
);
4728 bpf_error("'clnp' modifier applied to %s", typestr
);
4731 bpf_error("'stp' modifier applied to %s", typestr
);
4734 bpf_error("IPX host filtering not implemented");
4737 bpf_error("'netbeui' modifier applied to %s", typestr
);
4740 bpf_error("'radio' modifier applied to %s", typestr
);
4749 static struct block
*
4750 gen_host6(addr
, mask
, proto
, dir
, type
)
4751 struct in6_addr
*addr
;
4752 struct in6_addr
*mask
;
4757 const char *typestr
;
4767 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4770 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4773 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4776 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4779 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4782 bpf_error("'sctp' modifier applied to %s", typestr
);
4785 bpf_error("'tcp' modifier applied to %s", typestr
);
4788 bpf_error("'udp' modifier applied to %s", typestr
);
4791 bpf_error("'icmp' modifier applied to %s", typestr
);
4794 bpf_error("'igmp' modifier applied to %s", typestr
);
4797 bpf_error("'igrp' modifier applied to %s", typestr
);
4800 bpf_error("'pim' modifier applied to %s", typestr
);
4803 bpf_error("'vrrp' modifier applied to %s", typestr
);
4806 bpf_error("'carp' modifier applied to %s", typestr
);
4809 bpf_error("ATALK host filtering not implemented");
4812 bpf_error("AARP host filtering not implemented");
4815 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4818 bpf_error("SCA host filtering not implemented");
4821 bpf_error("LAT host filtering not implemented");
4824 bpf_error("MOPDL host filtering not implemented");
4827 bpf_error("MOPRC host filtering not implemented");
4830 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4833 bpf_error("'icmp6' modifier applied to %s", typestr
);
4836 bpf_error("'ah' modifier applied to %s", typestr
);
4839 bpf_error("'esp' modifier applied to %s", typestr
);
4842 bpf_error("ISO host filtering not implemented");
4845 bpf_error("'esis' modifier applied to %s", typestr
);
4848 bpf_error("'isis' modifier applied to %s", typestr
);
4851 bpf_error("'clnp' modifier applied to %s", typestr
);
4854 bpf_error("'stp' modifier applied to %s", typestr
);
4857 bpf_error("IPX host filtering not implemented");
4860 bpf_error("'netbeui' modifier applied to %s", typestr
);
4863 bpf_error("'radio' modifier applied to %s", typestr
);
4873 static struct block
*
4874 gen_gateway(eaddr
, alist
, proto
, dir
)
4875 const u_char
*eaddr
;
4876 bpf_u_int32
**alist
;
4880 struct block
*b0
, *b1
, *tmp
;
4883 bpf_error("direction applied to 'gateway'");
4892 case DLT_NETANALYZER
:
4893 case DLT_NETANALYZER_TRANSPARENT
:
4894 b0
= gen_ehostop(eaddr
, Q_OR
);
4897 b0
= gen_fhostop(eaddr
, Q_OR
);
4900 b0
= gen_thostop(eaddr
, Q_OR
);
4902 case DLT_IEEE802_11
:
4903 case DLT_PRISM_HEADER
:
4904 case DLT_IEEE802_11_RADIO_AVS
:
4905 case DLT_IEEE802_11_RADIO
:
4907 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4912 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4914 * Check that the packet doesn't begin with an
4915 * LE Control marker. (We've already generated
4918 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4923 * Now check the MAC address.
4925 b0
= gen_ehostop(eaddr
, Q_OR
);
4928 case DLT_IP_OVER_FC
:
4929 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4933 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4935 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4937 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4946 bpf_error("illegal modifier of 'gateway'");
4952 gen_proto_abbrev(proto
)
4961 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4962 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4967 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4968 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4973 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4974 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4979 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4982 #ifndef IPPROTO_IGMP
4983 #define IPPROTO_IGMP 2
4987 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4990 #ifndef IPPROTO_IGRP
4991 #define IPPROTO_IGRP 9
4994 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4998 #define IPPROTO_PIM 103
5002 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5003 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5007 #ifndef IPPROTO_VRRP
5008 #define IPPROTO_VRRP 112
5012 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5015 #ifndef IPPROTO_CARP
5016 #define IPPROTO_CARP 112
5020 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5024 b1
= gen_linktype(ETHERTYPE_IP
);
5028 b1
= gen_linktype(ETHERTYPE_ARP
);
5032 b1
= gen_linktype(ETHERTYPE_REVARP
);
5036 bpf_error("link layer applied in wrong context");
5039 b1
= gen_linktype(ETHERTYPE_ATALK
);
5043 b1
= gen_linktype(ETHERTYPE_AARP
);
5047 b1
= gen_linktype(ETHERTYPE_DN
);
5051 b1
= gen_linktype(ETHERTYPE_SCA
);
5055 b1
= gen_linktype(ETHERTYPE_LAT
);
5059 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5063 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5067 b1
= gen_linktype(ETHERTYPE_IPV6
);
5070 #ifndef IPPROTO_ICMPV6
5071 #define IPPROTO_ICMPV6 58
5074 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5078 #define IPPROTO_AH 51
5081 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5082 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5087 #define IPPROTO_ESP 50
5090 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5091 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5096 b1
= gen_linktype(LLCSAP_ISONS
);
5100 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5104 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5107 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5108 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5109 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5111 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5113 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5115 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5119 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5120 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5121 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5123 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5125 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5127 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5131 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5132 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5133 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5135 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5140 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5141 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5146 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5147 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5149 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5151 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5156 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5157 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5162 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5163 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5168 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5172 b1
= gen_linktype(LLCSAP_8021D
);
5176 b1
= gen_linktype(LLCSAP_IPX
);
5180 b1
= gen_linktype(LLCSAP_NETBEUI
);
5184 bpf_error("'radio' is not a valid protocol type");
5192 static struct block
*
5198 /* not IPv4 frag other than the first frag */
5199 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5200 b
= new_block(JMP(BPF_JSET
));
5209 * Generate a comparison to a port value in the transport-layer header
5210 * at the specified offset from the beginning of that header.
5212 * XXX - this handles a variable-length prefix preceding the link-layer
5213 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5214 * variable-length link-layer headers (such as Token Ring or 802.11
5217 static struct block
*
5218 gen_portatom(off
, v
)
5222 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5225 static struct block
*
5226 gen_portatom6(off
, v
)
5230 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5234 gen_portop(port
, proto
, dir
)
5235 int port
, proto
, dir
;
5237 struct block
*b0
, *b1
, *tmp
;
5239 /* ip proto 'proto' and not a fragment other than the first fragment */
5240 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5246 b1
= gen_portatom(0, (bpf_int32
)port
);
5250 b1
= gen_portatom(2, (bpf_int32
)port
);
5255 tmp
= gen_portatom(0, (bpf_int32
)port
);
5256 b1
= gen_portatom(2, (bpf_int32
)port
);
5261 tmp
= gen_portatom(0, (bpf_int32
)port
);
5262 b1
= gen_portatom(2, (bpf_int32
)port
);
5274 static struct block
*
5275 gen_port(port
, ip_proto
, dir
)
5280 struct block
*b0
, *b1
, *tmp
;
5285 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5286 * not LLC encapsulation with LLCSAP_IP.
5288 * For IEEE 802 networks - which includes 802.5 token ring
5289 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5290 * says that SNAP encapsulation is used, not LLC encapsulation
5293 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5294 * RFC 2225 say that SNAP encapsulation is used, not LLC
5295 * encapsulation with LLCSAP_IP.
5297 * So we always check for ETHERTYPE_IP.
5299 b0
= gen_linktype(ETHERTYPE_IP
);
5305 b1
= gen_portop(port
, ip_proto
, dir
);
5309 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5310 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5312 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5324 gen_portop6(port
, proto
, dir
)
5325 int port
, proto
, dir
;
5327 struct block
*b0
, *b1
, *tmp
;
5329 /* ip6 proto 'proto' */
5330 /* XXX - catch the first fragment of a fragmented packet? */
5331 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5335 b1
= gen_portatom6(0, (bpf_int32
)port
);
5339 b1
= gen_portatom6(2, (bpf_int32
)port
);
5344 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5345 b1
= gen_portatom6(2, (bpf_int32
)port
);
5350 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5351 b1
= gen_portatom6(2, (bpf_int32
)port
);
5363 static struct block
*
5364 gen_port6(port
, ip_proto
, dir
)
5369 struct block
*b0
, *b1
, *tmp
;
5371 /* link proto ip6 */
5372 b0
= gen_linktype(ETHERTYPE_IPV6
);
5378 b1
= gen_portop6(port
, ip_proto
, dir
);
5382 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5383 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5385 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5396 /* gen_portrange code */
5397 static struct block
*
5398 gen_portrangeatom(off
, v1
, v2
)
5402 struct block
*b1
, *b2
;
5406 * Reverse the order of the ports, so v1 is the lower one.
5415 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5416 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5424 gen_portrangeop(port1
, port2
, proto
, dir
)
5429 struct block
*b0
, *b1
, *tmp
;
5431 /* ip proto 'proto' and not a fragment other than the first fragment */
5432 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5438 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5442 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5447 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5448 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5453 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5454 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5466 static struct block
*
5467 gen_portrange(port1
, port2
, ip_proto
, dir
)
5472 struct block
*b0
, *b1
, *tmp
;
5475 b0
= gen_linktype(ETHERTYPE_IP
);
5481 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5485 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5486 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5488 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5499 static struct block
*
5500 gen_portrangeatom6(off
, v1
, v2
)
5504 struct block
*b1
, *b2
;
5508 * Reverse the order of the ports, so v1 is the lower one.
5517 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5518 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5526 gen_portrangeop6(port1
, port2
, proto
, dir
)
5531 struct block
*b0
, *b1
, *tmp
;
5533 /* ip6 proto 'proto' */
5534 /* XXX - catch the first fragment of a fragmented packet? */
5535 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5539 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5543 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5548 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5549 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5554 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5555 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5567 static struct block
*
5568 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5573 struct block
*b0
, *b1
, *tmp
;
5575 /* link proto ip6 */
5576 b0
= gen_linktype(ETHERTYPE_IPV6
);
5582 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5586 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5587 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5589 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5601 lookup_proto(name
, proto
)
5602 register const char *name
;
5612 v
= pcap_nametoproto(name
);
5613 if (v
== PROTO_UNDEF
)
5614 bpf_error("unknown ip proto '%s'", name
);
5618 /* XXX should look up h/w protocol type based on linktype */
5619 v
= pcap_nametoeproto(name
);
5620 if (v
== PROTO_UNDEF
) {
5621 v
= pcap_nametollc(name
);
5622 if (v
== PROTO_UNDEF
)
5623 bpf_error("unknown ether proto '%s'", name
);
5628 if (strcmp(name
, "esis") == 0)
5630 else if (strcmp(name
, "isis") == 0)
5632 else if (strcmp(name
, "clnp") == 0)
5635 bpf_error("unknown osi proto '%s'", name
);
5655 static struct block
*
5656 gen_protochain(v
, proto
, dir
)
5661 #ifdef NO_PROTOCHAIN
5662 return gen_proto(v
, proto
, dir
);
5664 struct block
*b0
, *b
;
5665 struct slist
*s
[100];
5666 int fix2
, fix3
, fix4
, fix5
;
5667 int ahcheck
, again
, end
;
5669 int reg2
= alloc_reg();
5671 memset(s
, 0, sizeof(s
));
5672 fix2
= fix3
= fix4
= fix5
= 0;
5679 b0
= gen_protochain(v
, Q_IP
, dir
);
5680 b
= gen_protochain(v
, Q_IPV6
, dir
);
5684 bpf_error("bad protocol applied for 'protochain'");
5689 * We don't handle variable-length prefixes before the link-layer
5690 * header, or variable-length link-layer headers, here yet.
5691 * We might want to add BPF instructions to do the protochain
5692 * work, to simplify that and, on platforms that have a BPF
5693 * interpreter with the new instructions, let the filtering
5694 * be done in the kernel. (We already require a modified BPF
5695 * engine to do the protochain stuff, to support backward
5696 * branches, and backward branch support is unlikely to appear
5697 * in kernel BPF engines.)
5701 case DLT_IEEE802_11
:
5702 case DLT_PRISM_HEADER
:
5703 case DLT_IEEE802_11_RADIO_AVS
:
5704 case DLT_IEEE802_11_RADIO
:
5706 bpf_error("'protochain' not supported with 802.11");
5709 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5712 * s[0] is a dummy entry to protect other BPF insn from damage
5713 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5714 * hard to find interdependency made by jump table fixup.
5717 s
[i
] = new_stmt(0); /*dummy*/
5722 b0
= gen_linktype(ETHERTYPE_IP
);
5725 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5726 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
+ 9;
5728 /* X = ip->ip_hl << 2 */
5729 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5730 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5735 b0
= gen_linktype(ETHERTYPE_IPV6
);
5737 /* A = ip6->ip_nxt */
5738 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5739 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
+ 6;
5741 /* X = sizeof(struct ip6_hdr) */
5742 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5748 bpf_error("unsupported proto to gen_protochain");
5752 /* again: if (A == v) goto end; else fall through; */
5754 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5756 s
[i
]->s
.jt
= NULL
; /*later*/
5757 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5761 #ifndef IPPROTO_NONE
5762 #define IPPROTO_NONE 59
5764 /* if (A == IPPROTO_NONE) goto end */
5765 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5766 s
[i
]->s
.jt
= NULL
; /*later*/
5767 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5768 s
[i
]->s
.k
= IPPROTO_NONE
;
5769 s
[fix5
]->s
.jf
= s
[i
];
5773 if (proto
== Q_IPV6
) {
5774 int v6start
, v6end
, v6advance
, j
;
5777 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5778 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5779 s
[i
]->s
.jt
= NULL
; /*later*/
5780 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5781 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5782 s
[fix2
]->s
.jf
= s
[i
];
5784 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5785 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5786 s
[i
]->s
.jt
= NULL
; /*later*/
5787 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5788 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5790 /* if (A == IPPROTO_ROUTING) goto v6advance */
5791 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5792 s
[i
]->s
.jt
= NULL
; /*later*/
5793 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5794 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5796 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5797 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5798 s
[i
]->s
.jt
= NULL
; /*later*/
5799 s
[i
]->s
.jf
= NULL
; /*later*/
5800 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5810 * A = P[X + packet head];
5811 * X = X + (P[X + packet head + 1] + 1) * 8;
5813 /* A = P[X + packet head] */
5814 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5815 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5818 s
[i
] = new_stmt(BPF_ST
);
5821 /* A = P[X + packet head + 1]; */
5822 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5823 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
+ 1;
5826 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5830 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5834 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5838 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5841 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5845 /* goto again; (must use BPF_JA for backward jump) */
5846 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5847 s
[i
]->s
.k
= again
- i
- 1;
5848 s
[i
- 1]->s
.jf
= s
[i
];
5852 for (j
= v6start
; j
<= v6end
; j
++)
5853 s
[j
]->s
.jt
= s
[v6advance
];
5856 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5858 s
[fix2
]->s
.jf
= s
[i
];
5864 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5865 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5866 s
[i
]->s
.jt
= NULL
; /*later*/
5867 s
[i
]->s
.jf
= NULL
; /*later*/
5868 s
[i
]->s
.k
= IPPROTO_AH
;
5870 s
[fix3
]->s
.jf
= s
[ahcheck
];
5877 * X = X + (P[X + 1] + 2) * 4;
5880 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5882 /* A = P[X + packet head]; */
5883 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5884 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5887 s
[i
] = new_stmt(BPF_ST
);
5891 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5894 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5898 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5900 /* A = P[X + packet head] */
5901 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5902 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5905 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5909 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5913 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5916 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5920 /* goto again; (must use BPF_JA for backward jump) */
5921 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5922 s
[i
]->s
.k
= again
- i
- 1;
5927 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5929 s
[fix2
]->s
.jt
= s
[end
];
5930 s
[fix4
]->s
.jf
= s
[end
];
5931 s
[fix5
]->s
.jt
= s
[end
];
5938 for (i
= 0; i
< max
- 1; i
++)
5939 s
[i
]->next
= s
[i
+ 1];
5940 s
[max
- 1]->next
= NULL
;
5945 b
= new_block(JMP(BPF_JEQ
));
5946 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5956 static struct block
*
5957 gen_check_802_11_data_frame()
5960 struct block
*b0
, *b1
;
5963 * A data frame has the 0x08 bit (b3) in the frame control field set
5964 * and the 0x04 bit (b2) clear.
5966 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5967 b0
= new_block(JMP(BPF_JSET
));
5971 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5972 b1
= new_block(JMP(BPF_JSET
));
5983 * Generate code that checks whether the packet is a packet for protocol
5984 * <proto> and whether the type field in that protocol's header has
5985 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5986 * IP packet and checks the protocol number in the IP header against <v>.
5988 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5989 * against Q_IP and Q_IPV6.
5991 static struct block
*
5992 gen_proto(v
, proto
, dir
)
5997 struct block
*b0
, *b1
;
6002 if (dir
!= Q_DEFAULT
)
6003 bpf_error("direction applied to 'proto'");
6007 b0
= gen_proto(v
, Q_IP
, dir
);
6008 b1
= gen_proto(v
, Q_IPV6
, dir
);
6014 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6015 * not LLC encapsulation with LLCSAP_IP.
6017 * For IEEE 802 networks - which includes 802.5 token ring
6018 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6019 * says that SNAP encapsulation is used, not LLC encapsulation
6022 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6023 * RFC 2225 say that SNAP encapsulation is used, not LLC
6024 * encapsulation with LLCSAP_IP.
6026 * So we always check for ETHERTYPE_IP.
6028 b0
= gen_linktype(ETHERTYPE_IP
);
6030 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6032 b1
= gen_protochain(v
, Q_IP
);
6042 * Frame Relay packets typically have an OSI
6043 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6044 * generates code to check for all the OSI
6045 * NLPIDs, so calling it and then adding a check
6046 * for the particular NLPID for which we're
6047 * looking is bogus, as we can just check for
6050 * What we check for is the NLPID and a frame
6051 * control field value of UI, i.e. 0x03 followed
6054 * XXX - assumes a 2-byte Frame Relay header with
6055 * DLCI and flags. What if the address is longer?
6057 * XXX - what about SNAP-encapsulated frames?
6059 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6065 * Cisco uses an Ethertype lookalike - for OSI,
6068 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6069 /* OSI in C-HDLC is stuffed with a fudge byte */
6070 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6075 b0
= gen_linktype(LLCSAP_ISONS
);
6076 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6082 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6084 * 4 is the offset of the PDU type relative to the IS-IS
6087 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6092 bpf_error("arp does not encapsulate another protocol");
6096 bpf_error("rarp does not encapsulate another protocol");
6100 bpf_error("atalk encapsulation is not specifiable");
6104 bpf_error("decnet encapsulation is not specifiable");
6108 bpf_error("sca does not encapsulate another protocol");
6112 bpf_error("lat does not encapsulate another protocol");
6116 bpf_error("moprc does not encapsulate another protocol");
6120 bpf_error("mopdl does not encapsulate another protocol");
6124 return gen_linktype(v
);
6127 bpf_error("'udp proto' is bogus");
6131 bpf_error("'tcp proto' is bogus");
6135 bpf_error("'sctp proto' is bogus");
6139 bpf_error("'icmp proto' is bogus");
6143 bpf_error("'igmp proto' is bogus");
6147 bpf_error("'igrp proto' is bogus");
6151 bpf_error("'pim proto' is bogus");
6155 bpf_error("'vrrp proto' is bogus");
6159 bpf_error("'carp proto' is bogus");
6163 b0
= gen_linktype(ETHERTYPE_IPV6
);
6166 * Also check for a fragment header before the final
6169 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6170 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
6172 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6175 b1
= gen_protochain(v
, Q_IPV6
);
6181 bpf_error("'icmp6 proto' is bogus");
6184 bpf_error("'ah proto' is bogus");
6187 bpf_error("'ah proto' is bogus");
6190 bpf_error("'stp proto' is bogus");
6193 bpf_error("'ipx proto' is bogus");
6196 bpf_error("'netbeui proto' is bogus");
6199 bpf_error("'radio proto' is bogus");
6210 register const char *name
;
6213 int proto
= q
.proto
;
6217 bpf_u_int32 mask
, addr
;
6219 bpf_u_int32
**alist
;
6222 struct sockaddr_in
*sin4
;
6223 struct sockaddr_in6
*sin6
;
6224 struct addrinfo
*res
, *res0
;
6225 struct in6_addr mask128
;
6227 struct block
*b
, *tmp
;
6228 int port
, real_proto
;
6234 addr
= pcap_nametonetaddr(name
);
6236 bpf_error("unknown network '%s'", name
);
6237 /* Left justify network addr and calculate its network mask */
6239 while (addr
&& (addr
& 0xff000000) == 0) {
6243 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6247 if (proto
== Q_LINK
) {
6251 case DLT_NETANALYZER
:
6252 case DLT_NETANALYZER_TRANSPARENT
:
6253 eaddr
= pcap_ether_hostton(name
);
6256 "unknown ether host '%s'", name
);
6257 b
= gen_ehostop(eaddr
, dir
);
6262 eaddr
= pcap_ether_hostton(name
);
6265 "unknown FDDI host '%s'", name
);
6266 b
= gen_fhostop(eaddr
, dir
);
6271 eaddr
= pcap_ether_hostton(name
);
6274 "unknown token ring host '%s'", name
);
6275 b
= gen_thostop(eaddr
, dir
);
6279 case DLT_IEEE802_11
:
6280 case DLT_PRISM_HEADER
:
6281 case DLT_IEEE802_11_RADIO_AVS
:
6282 case DLT_IEEE802_11_RADIO
:
6284 eaddr
= pcap_ether_hostton(name
);
6287 "unknown 802.11 host '%s'", name
);
6288 b
= gen_wlanhostop(eaddr
, dir
);
6292 case DLT_IP_OVER_FC
:
6293 eaddr
= pcap_ether_hostton(name
);
6296 "unknown Fibre Channel host '%s'", name
);
6297 b
= gen_ipfchostop(eaddr
, dir
);
6306 * Check that the packet doesn't begin
6307 * with an LE Control marker. (We've
6308 * already generated a test for LANE.)
6310 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6314 eaddr
= pcap_ether_hostton(name
);
6317 "unknown ether host '%s'", name
);
6318 b
= gen_ehostop(eaddr
, dir
);
6324 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6325 } else if (proto
== Q_DECNET
) {
6326 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6328 * I don't think DECNET hosts can be multihomed, so
6329 * there is no need to build up a list of addresses
6331 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6334 alist
= pcap_nametoaddr(name
);
6335 if (alist
== NULL
|| *alist
== NULL
)
6336 bpf_error("unknown host '%s'", name
);
6338 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6340 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6342 tmp
= gen_host(**alist
++, 0xffffffff,
6343 tproto
, dir
, q
.addr
);
6349 memset(&mask128
, 0xff, sizeof(mask128
));
6350 res0
= res
= pcap_nametoaddrinfo(name
);
6352 bpf_error("unknown host '%s'", name
);
6355 tproto
= tproto6
= proto
;
6356 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6360 for (res
= res0
; res
; res
= res
->ai_next
) {
6361 switch (res
->ai_family
) {
6363 if (tproto
== Q_IPV6
)
6366 sin4
= (struct sockaddr_in
*)
6368 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6369 0xffffffff, tproto
, dir
, q
.addr
);
6372 if (tproto6
== Q_IP
)
6375 sin6
= (struct sockaddr_in6
*)
6377 tmp
= gen_host6(&sin6
->sin6_addr
,
6378 &mask128
, tproto6
, dir
, q
.addr
);
6390 bpf_error("unknown host '%s'%s", name
,
6391 (proto
== Q_DEFAULT
)
6393 : " for specified address family");
6400 if (proto
!= Q_DEFAULT
&&
6401 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6402 bpf_error("illegal qualifier of 'port'");
6403 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6404 bpf_error("unknown port '%s'", name
);
6405 if (proto
== Q_UDP
) {
6406 if (real_proto
== IPPROTO_TCP
)
6407 bpf_error("port '%s' is tcp", name
);
6408 else if (real_proto
== IPPROTO_SCTP
)
6409 bpf_error("port '%s' is sctp", name
);
6411 /* override PROTO_UNDEF */
6412 real_proto
= IPPROTO_UDP
;
6414 if (proto
== Q_TCP
) {
6415 if (real_proto
== IPPROTO_UDP
)
6416 bpf_error("port '%s' is udp", name
);
6418 else if (real_proto
== IPPROTO_SCTP
)
6419 bpf_error("port '%s' is sctp", name
);
6421 /* override PROTO_UNDEF */
6422 real_proto
= IPPROTO_TCP
;
6424 if (proto
== Q_SCTP
) {
6425 if (real_proto
== IPPROTO_UDP
)
6426 bpf_error("port '%s' is udp", name
);
6428 else if (real_proto
== IPPROTO_TCP
)
6429 bpf_error("port '%s' is tcp", name
);
6431 /* override PROTO_UNDEF */
6432 real_proto
= IPPROTO_SCTP
;
6435 bpf_error("illegal port number %d < 0", port
);
6437 bpf_error("illegal port number %d > 65535", port
);
6438 b
= gen_port(port
, real_proto
, dir
);
6439 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6443 if (proto
!= Q_DEFAULT
&&
6444 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6445 bpf_error("illegal qualifier of 'portrange'");
6446 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6447 bpf_error("unknown port in range '%s'", name
);
6448 if (proto
== Q_UDP
) {
6449 if (real_proto
== IPPROTO_TCP
)
6450 bpf_error("port in range '%s' is tcp", name
);
6451 else if (real_proto
== IPPROTO_SCTP
)
6452 bpf_error("port in range '%s' is sctp", name
);
6454 /* override PROTO_UNDEF */
6455 real_proto
= IPPROTO_UDP
;
6457 if (proto
== Q_TCP
) {
6458 if (real_proto
== IPPROTO_UDP
)
6459 bpf_error("port in range '%s' is udp", name
);
6460 else if (real_proto
== IPPROTO_SCTP
)
6461 bpf_error("port in range '%s' is sctp", name
);
6463 /* override PROTO_UNDEF */
6464 real_proto
= IPPROTO_TCP
;
6466 if (proto
== Q_SCTP
) {
6467 if (real_proto
== IPPROTO_UDP
)
6468 bpf_error("port in range '%s' is udp", name
);
6469 else if (real_proto
== IPPROTO_TCP
)
6470 bpf_error("port in range '%s' is tcp", name
);
6472 /* override PROTO_UNDEF */
6473 real_proto
= IPPROTO_SCTP
;
6476 bpf_error("illegal port number %d < 0", port1
);
6478 bpf_error("illegal port number %d > 65535", port1
);
6480 bpf_error("illegal port number %d < 0", port2
);
6482 bpf_error("illegal port number %d > 65535", port2
);
6484 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6485 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6490 eaddr
= pcap_ether_hostton(name
);
6492 bpf_error("unknown ether host: %s", name
);
6494 alist
= pcap_nametoaddr(name
);
6495 if (alist
== NULL
|| *alist
== NULL
)
6496 bpf_error("unknown host '%s'", name
);
6497 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6501 bpf_error("'gateway' not supported in this configuration");
6505 real_proto
= lookup_proto(name
, proto
);
6506 if (real_proto
>= 0)
6507 return gen_proto(real_proto
, proto
, dir
);
6509 bpf_error("unknown protocol: %s", name
);
6512 real_proto
= lookup_proto(name
, proto
);
6513 if (real_proto
>= 0)
6514 return gen_protochain(real_proto
, proto
, dir
);
6516 bpf_error("unknown protocol: %s", name
);
6527 gen_mcode(s1
, s2
, masklen
, q
)
6528 register const char *s1
, *s2
;
6529 register unsigned int masklen
;
6532 register int nlen
, mlen
;
6535 nlen
= __pcap_atoin(s1
, &n
);
6536 /* Promote short ipaddr */
6540 mlen
= __pcap_atoin(s2
, &m
);
6541 /* Promote short ipaddr */
6544 bpf_error("non-network bits set in \"%s mask %s\"",
6547 /* Convert mask len to mask */
6549 bpf_error("mask length must be <= 32");
6552 * X << 32 is not guaranteed by C to be 0; it's
6557 m
= 0xffffffff << (32 - masklen
);
6559 bpf_error("non-network bits set in \"%s/%d\"",
6566 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6569 bpf_error("Mask syntax for networks only");
6578 register const char *s
;
6583 int proto
= q
.proto
;
6589 else if (q
.proto
== Q_DECNET
)
6590 vlen
= __pcap_atodn(s
, &v
);
6592 vlen
= __pcap_atoin(s
, &v
);
6599 if (proto
== Q_DECNET
)
6600 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6601 else if (proto
== Q_LINK
) {
6602 bpf_error("illegal link layer address");
6605 if (s
== NULL
&& q
.addr
== Q_NET
) {
6606 /* Promote short net number */
6607 while (v
&& (v
& 0xff000000) == 0) {
6612 /* Promote short ipaddr */
6616 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6621 proto
= IPPROTO_UDP
;
6622 else if (proto
== Q_TCP
)
6623 proto
= IPPROTO_TCP
;
6624 else if (proto
== Q_SCTP
)
6625 proto
= IPPROTO_SCTP
;
6626 else if (proto
== Q_DEFAULT
)
6627 proto
= PROTO_UNDEF
;
6629 bpf_error("illegal qualifier of 'port'");
6632 bpf_error("illegal port number %u > 65535", v
);
6636 b
= gen_port((int)v
, proto
, dir
);
6637 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6643 proto
= IPPROTO_UDP
;
6644 else if (proto
== Q_TCP
)
6645 proto
= IPPROTO_TCP
;
6646 else if (proto
== Q_SCTP
)
6647 proto
= IPPROTO_SCTP
;
6648 else if (proto
== Q_DEFAULT
)
6649 proto
= PROTO_UNDEF
;
6651 bpf_error("illegal qualifier of 'portrange'");
6654 bpf_error("illegal port number %u > 65535", v
);
6658 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6659 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6664 bpf_error("'gateway' requires a name");
6668 return gen_proto((int)v
, proto
, dir
);
6671 return gen_protochain((int)v
, proto
, dir
);
6686 gen_mcode6(s1
, s2
, masklen
, q
)
6687 register const char *s1
, *s2
;
6688 register unsigned int masklen
;
6691 struct addrinfo
*res
;
6692 struct in6_addr
*addr
;
6693 struct in6_addr mask
;
6698 bpf_error("no mask %s supported", s2
);
6700 res
= pcap_nametoaddrinfo(s1
);
6702 bpf_error("invalid ip6 address %s", s1
);
6705 bpf_error("%s resolved to multiple address", s1
);
6706 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6708 if (sizeof(mask
) * 8 < masklen
)
6709 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6710 memset(&mask
, 0, sizeof(mask
));
6711 memset(&mask
, 0xff, masklen
/ 8);
6713 mask
.s6_addr
[masklen
/ 8] =
6714 (0xff << (8 - masklen
% 8)) & 0xff;
6717 a
= (u_int32_t
*)addr
;
6718 m
= (u_int32_t
*)&mask
;
6719 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6720 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6721 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6729 bpf_error("Mask syntax for networks only");
6733 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6739 bpf_error("invalid qualifier against IPv6 address");
6748 register const u_char
*eaddr
;
6751 struct block
*b
, *tmp
;
6753 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6756 case DLT_NETANALYZER
:
6757 case DLT_NETANALYZER_TRANSPARENT
:
6758 return gen_ehostop(eaddr
, (int)q
.dir
);
6760 return gen_fhostop(eaddr
, (int)q
.dir
);
6762 return gen_thostop(eaddr
, (int)q
.dir
);
6763 case DLT_IEEE802_11
:
6764 case DLT_PRISM_HEADER
:
6765 case DLT_IEEE802_11_RADIO_AVS
:
6766 case DLT_IEEE802_11_RADIO
:
6768 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6772 * Check that the packet doesn't begin with an
6773 * LE Control marker. (We've already generated
6776 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6781 * Now check the MAC address.
6783 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6788 case DLT_IP_OVER_FC
:
6789 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6791 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6795 bpf_error("ethernet address used in non-ether expression");
6802 struct slist
*s0
, *s1
;
6805 * This is definitely not the best way to do this, but the
6806 * lists will rarely get long.
6813 static struct slist
*
6819 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6824 static struct slist
*
6830 s
= new_stmt(BPF_LD
|BPF_MEM
);
6836 * Modify "index" to use the value stored into its register as an
6837 * offset relative to the beginning of the header for the protocol
6838 * "proto", and allocate a register and put an item "size" bytes long
6839 * (1, 2, or 4) at that offset into that register, making it the register
6843 gen_load(proto
, inst
, size
)
6848 struct slist
*s
, *tmp
;
6850 int regno
= alloc_reg();
6852 free_reg(inst
->regno
);
6856 bpf_error("data size must be 1, 2, or 4");
6872 bpf_error("unsupported index operation");
6876 * The offset is relative to the beginning of the packet
6877 * data, if we have a radio header. (If we don't, this
6880 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6881 linktype
!= DLT_IEEE802_11_RADIO
&&
6882 linktype
!= DLT_PRISM_HEADER
)
6883 bpf_error("radio information not present in capture");
6886 * Load into the X register the offset computed into the
6887 * register specified by "index".
6889 s
= xfer_to_x(inst
);
6892 * Load the item at that offset.
6894 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6896 sappend(inst
->s
, s
);
6901 * The offset is relative to the beginning of
6902 * the link-layer header.
6904 * XXX - what about ATM LANE? Should the index be
6905 * relative to the beginning of the AAL5 frame, so
6906 * that 0 refers to the beginning of the LE Control
6907 * field, or relative to the beginning of the LAN
6908 * frame, so that 0 refers, for Ethernet LANE, to
6909 * the beginning of the destination address?
6911 s
= gen_llprefixlen();
6914 * If "s" is non-null, it has code to arrange that the
6915 * X register contains the length of the prefix preceding
6916 * the link-layer header. Add to it the offset computed
6917 * into the register specified by "index", and move that
6918 * into the X register. Otherwise, just load into the X
6919 * register the offset computed into the register specified
6923 sappend(s
, xfer_to_a(inst
));
6924 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6925 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6927 s
= xfer_to_x(inst
);
6930 * Load the item at the sum of the offset we've put in the
6931 * X register and the offset of the start of the link
6932 * layer header (which is 0 if the radio header is
6933 * variable-length; that header length is what we put
6934 * into the X register and then added to the index).
6936 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6939 sappend(inst
->s
, s
);
6953 * The offset is relative to the beginning of
6954 * the network-layer header.
6955 * XXX - are there any cases where we want
6958 s
= gen_off_linkpl();
6961 * If "s" is non-null, it has code to arrange that the
6962 * X register contains the variable part of the offset
6963 * of the link-layer payload. Add to it the offset
6964 * computed into the register specified by "index",
6965 * and move that into the X register. Otherwise, just
6966 * load into the X register the offset computed into
6967 * the register specified by "index".
6970 sappend(s
, xfer_to_a(inst
));
6971 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6972 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6974 s
= xfer_to_x(inst
);
6977 * Load the item at the sum of the offset we've put in the
6978 * X register, the offset of the start of the network
6979 * layer header from the beginning of the link-layer
6980 * payload, and the constant part of the offset of the
6981 * start of the link-layer payload.
6983 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6984 tmp
->s
.k
= off_linkpl_constant_part
+ off_nl
;
6986 sappend(inst
->s
, s
);
6989 * Do the computation only if the packet contains
6990 * the protocol in question.
6992 b
= gen_proto_abbrev(proto
);
6994 gen_and(inst
->b
, b
);
7008 * The offset is relative to the beginning of
7009 * the transport-layer header.
7011 * Load the X register with the length of the IPv4 header
7012 * (plus the offset of the link-layer header, if it's
7013 * a variable-length header), in bytes.
7015 * XXX - are there any cases where we want
7017 * XXX - we should, if we're built with
7018 * IPv6 support, generate code to load either
7019 * IPv4, IPv6, or both, as appropriate.
7021 s
= gen_loadx_iphdrlen();
7024 * The X register now contains the sum of the variable
7025 * part of the offset of the link-layer payload and the
7026 * length of the network-layer header.
7028 * Load into the A register the offset relative to
7029 * the beginning of the transport layer header,
7030 * add the X register to that, move that to the
7031 * X register, and load with an offset from the
7032 * X register equal to the sum of the constant part of
7033 * the offset of the link-layer payload and the offset,
7034 * relative to the beginning of the link-layer payload,
7035 * of the network-layer header.
7037 sappend(s
, xfer_to_a(inst
));
7038 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7039 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7040 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7041 tmp
->s
.k
= off_linkpl_constant_part
+ off_nl
;
7042 sappend(inst
->s
, s
);
7045 * Do the computation only if the packet contains
7046 * the protocol in question - which is true only
7047 * if this is an IP datagram and is the first or
7048 * only fragment of that datagram.
7050 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7052 gen_and(inst
->b
, b
);
7053 gen_and(gen_proto_abbrev(Q_IP
), b
);
7057 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7060 inst
->regno
= regno
;
7061 s
= new_stmt(BPF_ST
);
7063 sappend(inst
->s
, s
);
7069 gen_relation(code
, a0
, a1
, reversed
)
7071 struct arth
*a0
, *a1
;
7074 struct slist
*s0
, *s1
, *s2
;
7075 struct block
*b
, *tmp
;
7079 if (code
== BPF_JEQ
) {
7080 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7081 b
= new_block(JMP(code
));
7085 b
= new_block(BPF_JMP
|code
|BPF_X
);
7091 sappend(a0
->s
, a1
->s
);
7095 free_reg(a0
->regno
);
7096 free_reg(a1
->regno
);
7098 /* 'and' together protocol checks */
7101 gen_and(a0
->b
, tmp
= a1
->b
);
7117 int regno
= alloc_reg();
7118 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7121 s
= new_stmt(BPF_LD
|BPF_LEN
);
7122 s
->next
= new_stmt(BPF_ST
);
7123 s
->next
->s
.k
= regno
;
7138 a
= (struct arth
*)newchunk(sizeof(*a
));
7142 s
= new_stmt(BPF_LD
|BPF_IMM
);
7144 s
->next
= new_stmt(BPF_ST
);
7160 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7163 s
= new_stmt(BPF_ST
);
7171 gen_arth(code
, a0
, a1
)
7173 struct arth
*a0
, *a1
;
7175 struct slist
*s0
, *s1
, *s2
;
7179 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7184 sappend(a0
->s
, a1
->s
);
7186 free_reg(a0
->regno
);
7187 free_reg(a1
->regno
);
7189 s0
= new_stmt(BPF_ST
);
7190 a0
->regno
= s0
->s
.k
= alloc_reg();
7197 * Here we handle simple allocation of the scratch registers.
7198 * If too many registers are alloc'd, the allocator punts.
7200 static int regused
[BPF_MEMWORDS
];
7204 * Initialize the table of used registers and the current register.
7210 memset(regused
, 0, sizeof regused
);
7214 * Return the next free register.
7219 int n
= BPF_MEMWORDS
;
7222 if (regused
[curreg
])
7223 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7225 regused
[curreg
] = 1;
7229 bpf_error("too many registers needed to evaluate expression");
7235 * Return a register to the table so it can
7245 static struct block
*
7252 s
= new_stmt(BPF_LD
|BPF_LEN
);
7253 b
= new_block(JMP(jmp
));
7264 return gen_len(BPF_JGE
, n
);
7268 * Actually, this is less than or equal.
7276 b
= gen_len(BPF_JGT
, n
);
7283 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7284 * the beginning of the link-layer header.
7285 * XXX - that means you can't test values in the radiotap header, but
7286 * as that header is difficult if not impossible to parse generally
7287 * without a loop, that might not be a severe problem. A new keyword
7288 * "radio" could be added for that, although what you'd really want
7289 * would be a way of testing particular radio header values, which
7290 * would generate code appropriate to the radio header in question.
7293 gen_byteop(op
, idx
, val
)
7304 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7307 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7311 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7315 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7319 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7323 b
= new_block(JMP(BPF_JEQ
));
7330 static u_char abroadcast
[] = { 0x0 };
7333 gen_broadcast(proto
)
7336 bpf_u_int32 hostmask
;
7337 struct block
*b0
, *b1
, *b2
;
7338 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7346 case DLT_ARCNET_LINUX
:
7347 return gen_ahostop(abroadcast
, Q_DST
);
7349 case DLT_NETANALYZER
:
7350 case DLT_NETANALYZER_TRANSPARENT
:
7351 return gen_ehostop(ebroadcast
, Q_DST
);
7353 return gen_fhostop(ebroadcast
, Q_DST
);
7355 return gen_thostop(ebroadcast
, Q_DST
);
7356 case DLT_IEEE802_11
:
7357 case DLT_PRISM_HEADER
:
7358 case DLT_IEEE802_11_RADIO_AVS
:
7359 case DLT_IEEE802_11_RADIO
:
7361 return gen_wlanhostop(ebroadcast
, Q_DST
);
7362 case DLT_IP_OVER_FC
:
7363 return gen_ipfchostop(ebroadcast
, Q_DST
);
7367 * Check that the packet doesn't begin with an
7368 * LE Control marker. (We've already generated
7371 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7376 * Now check the MAC address.
7378 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7384 bpf_error("not a broadcast link");
7390 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7391 * as an indication that we don't know the netmask, and fail
7394 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7395 bpf_error("netmask not known, so 'ip broadcast' not supported");
7396 b0
= gen_linktype(ETHERTYPE_IP
);
7397 hostmask
= ~netmask
;
7398 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7399 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7400 (bpf_int32
)(~0 & hostmask
), hostmask
);
7405 bpf_error("only link-layer/IP broadcast filters supported");
7411 * Generate code to test the low-order bit of a MAC address (that's
7412 * the bottom bit of the *first* byte).
7414 static struct block
*
7415 gen_mac_multicast(offset
)
7418 register struct block
*b0
;
7419 register struct slist
*s
;
7421 /* link[offset] & 1 != 0 */
7422 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7423 b0
= new_block(JMP(BPF_JSET
));
7430 gen_multicast(proto
)
7433 register struct block
*b0
, *b1
, *b2
;
7434 register struct slist
*s
;
7442 case DLT_ARCNET_LINUX
:
7443 /* all ARCnet multicasts use the same address */
7444 return gen_ahostop(abroadcast
, Q_DST
);
7446 case DLT_NETANALYZER
:
7447 case DLT_NETANALYZER_TRANSPARENT
:
7448 /* ether[0] & 1 != 0 */
7449 return gen_mac_multicast(0);
7452 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7454 * XXX - was that referring to bit-order issues?
7456 /* fddi[1] & 1 != 0 */
7457 return gen_mac_multicast(1);
7459 /* tr[2] & 1 != 0 */
7460 return gen_mac_multicast(2);
7461 case DLT_IEEE802_11
:
7462 case DLT_PRISM_HEADER
:
7463 case DLT_IEEE802_11_RADIO_AVS
:
7464 case DLT_IEEE802_11_RADIO
:
7469 * For control frames, there is no DA.
7471 * For management frames, DA is at an
7472 * offset of 4 from the beginning of
7475 * For data frames, DA is at an offset
7476 * of 4 from the beginning of the packet
7477 * if To DS is clear and at an offset of
7478 * 16 from the beginning of the packet
7483 * Generate the tests to be done for data frames.
7485 * First, check for To DS set, i.e. "link[1] & 0x01".
7487 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7488 b1
= new_block(JMP(BPF_JSET
));
7489 b1
->s
.k
= 0x01; /* To DS */
7493 * If To DS is set, the DA is at 16.
7495 b0
= gen_mac_multicast(16);
7499 * Now, check for To DS not set, i.e. check
7500 * "!(link[1] & 0x01)".
7502 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7503 b2
= new_block(JMP(BPF_JSET
));
7504 b2
->s
.k
= 0x01; /* To DS */
7509 * If To DS is not set, the DA is at 4.
7511 b1
= gen_mac_multicast(4);
7515 * Now OR together the last two checks. That gives
7516 * the complete set of checks for data frames.
7521 * Now check for a data frame.
7522 * I.e, check "link[0] & 0x08".
7524 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7525 b1
= new_block(JMP(BPF_JSET
));
7530 * AND that with the checks done for data frames.
7535 * If the high-order bit of the type value is 0, this
7536 * is a management frame.
7537 * I.e, check "!(link[0] & 0x08)".
7539 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7540 b2
= new_block(JMP(BPF_JSET
));
7546 * For management frames, the DA is at 4.
7548 b1
= gen_mac_multicast(4);
7552 * OR that with the checks done for data frames.
7553 * That gives the checks done for management and
7559 * If the low-order bit of the type value is 1,
7560 * this is either a control frame or a frame
7561 * with a reserved type, and thus not a
7564 * I.e., check "!(link[0] & 0x04)".
7566 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7567 b1
= new_block(JMP(BPF_JSET
));
7573 * AND that with the checks for data and management
7578 case DLT_IP_OVER_FC
:
7579 b0
= gen_mac_multicast(2);
7584 * Check that the packet doesn't begin with an
7585 * LE Control marker. (We've already generated
7588 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7592 /* ether[off_mac] & 1 != 0 */
7593 b0
= gen_mac_multicast(off_mac
);
7601 /* Link not known to support multicasts */
7605 b0
= gen_linktype(ETHERTYPE_IP
);
7606 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7611 b0
= gen_linktype(ETHERTYPE_IPV6
);
7612 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7616 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7622 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7623 * Outbound traffic is sent by this machine, while inbound traffic is
7624 * sent by a remote machine (and may include packets destined for a
7625 * unicast or multicast link-layer address we are not subscribing to).
7626 * These are the same definitions implemented by pcap_setdirection().
7627 * Capturing only unicast traffic destined for this host is probably
7628 * better accomplished using a higher-layer filter.
7634 register struct block
*b0
;
7637 * Only some data link types support inbound/outbound qualifiers.
7641 b0
= gen_relation(BPF_JEQ
,
7642 gen_load(Q_LINK
, gen_loadi(0), 1),
7649 /* match outgoing packets */
7650 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7652 /* match incoming packets */
7653 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7658 /* match outgoing packets */
7659 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7661 /* to filter on inbound traffic, invert the match */
7666 #ifdef HAVE_NET_PFVAR_H
7668 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7669 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7675 /* match outgoing packets */
7676 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7678 /* match incoming packets */
7679 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7683 case DLT_JUNIPER_MFR
:
7684 case DLT_JUNIPER_MLFR
:
7685 case DLT_JUNIPER_MLPPP
:
7686 case DLT_JUNIPER_ATM1
:
7687 case DLT_JUNIPER_ATM2
:
7688 case DLT_JUNIPER_PPPOE
:
7689 case DLT_JUNIPER_PPPOE_ATM
:
7690 case DLT_JUNIPER_GGSN
:
7691 case DLT_JUNIPER_ES
:
7692 case DLT_JUNIPER_MONITOR
:
7693 case DLT_JUNIPER_SERVICES
:
7694 case DLT_JUNIPER_ETHER
:
7695 case DLT_JUNIPER_PPP
:
7696 case DLT_JUNIPER_FRELAY
:
7697 case DLT_JUNIPER_CHDLC
:
7698 case DLT_JUNIPER_VP
:
7699 case DLT_JUNIPER_ST
:
7700 case DLT_JUNIPER_ISM
:
7701 case DLT_JUNIPER_VS
:
7702 case DLT_JUNIPER_SRX_E2E
:
7703 case DLT_JUNIPER_FIBRECHANNEL
:
7704 case DLT_JUNIPER_ATM_CEMIC
:
7706 /* juniper flags (including direction) are stored
7707 * the byte after the 3-byte magic number */
7709 /* match outgoing packets */
7710 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7712 /* match incoming packets */
7713 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7719 * If we have packet meta-data indicating a direction,
7720 * check it, otherwise give up as this link-layer type
7721 * has nothing in the packet data.
7723 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7725 * This is Linux with PF_PACKET support.
7726 * If this is a *live* capture, we can look at
7727 * special meta-data in the filter expression;
7728 * if it's a savefile, we can't.
7730 if (bpf_pcap
->rfile
!= NULL
) {
7731 /* We have a FILE *, so this is a savefile */
7732 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7737 /* match outgoing packets */
7738 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7741 /* to filter on inbound traffic, invert the match */
7744 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7745 bpf_error("inbound/outbound not supported on linktype %d",
7749 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7754 #ifdef HAVE_NET_PFVAR_H
7755 /* PF firewall log matched interface */
7757 gen_pf_ifname(const char *ifname
)
7762 if (linktype
!= DLT_PFLOG
) {
7763 bpf_error("ifname supported only on PF linktype");
7766 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7767 off
= offsetof(struct pfloghdr
, ifname
);
7768 if (strlen(ifname
) >= len
) {
7769 bpf_error("ifname interface names can only be %d characters",
7773 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7777 /* PF firewall log ruleset name */
7779 gen_pf_ruleset(char *ruleset
)
7783 if (linktype
!= DLT_PFLOG
) {
7784 bpf_error("ruleset supported only on PF linktype");
7788 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7789 bpf_error("ruleset names can only be %ld characters",
7790 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7794 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7795 strlen(ruleset
), (const u_char
*)ruleset
);
7799 /* PF firewall log rule number */
7805 if (linktype
!= DLT_PFLOG
) {
7806 bpf_error("rnr supported only on PF linktype");
7810 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7815 /* PF firewall log sub-rule number */
7817 gen_pf_srnr(int srnr
)
7821 if (linktype
!= DLT_PFLOG
) {
7822 bpf_error("srnr supported only on PF linktype");
7826 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7831 /* PF firewall log reason code */
7833 gen_pf_reason(int reason
)
7837 if (linktype
!= DLT_PFLOG
) {
7838 bpf_error("reason supported only on PF linktype");
7842 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7847 /* PF firewall log action */
7849 gen_pf_action(int action
)
7853 if (linktype
!= DLT_PFLOG
) {
7854 bpf_error("action supported only on PF linktype");
7858 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7862 #else /* !HAVE_NET_PFVAR_H */
7864 gen_pf_ifname(const char *ifname
)
7866 bpf_error("libpcap was compiled without pf support");
7872 gen_pf_ruleset(char *ruleset
)
7874 bpf_error("libpcap was compiled on a machine without pf support");
7882 bpf_error("libpcap was compiled on a machine without pf support");
7888 gen_pf_srnr(int srnr
)
7890 bpf_error("libpcap was compiled on a machine without pf support");
7896 gen_pf_reason(int reason
)
7898 bpf_error("libpcap was compiled on a machine without pf support");
7904 gen_pf_action(int action
)
7906 bpf_error("libpcap was compiled on a machine without pf support");
7910 #endif /* HAVE_NET_PFVAR_H */
7912 /* IEEE 802.11 wireless header */
7914 gen_p80211_type(int type
, int mask
)
7920 case DLT_IEEE802_11
:
7921 case DLT_PRISM_HEADER
:
7922 case DLT_IEEE802_11_RADIO_AVS
:
7923 case DLT_IEEE802_11_RADIO
:
7924 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7929 bpf_error("802.11 link-layer types supported only on 802.11");
7937 gen_p80211_fcdir(int fcdir
)
7943 case DLT_IEEE802_11
:
7944 case DLT_PRISM_HEADER
:
7945 case DLT_IEEE802_11_RADIO_AVS
:
7946 case DLT_IEEE802_11_RADIO
:
7950 bpf_error("frame direction supported only with 802.11 headers");
7954 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7955 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7962 register const u_char
*eaddr
;
7968 case DLT_ARCNET_LINUX
:
7969 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7971 return (gen_ahostop(eaddr
, (int)q
.dir
));
7973 bpf_error("ARCnet address used in non-arc expression");
7979 bpf_error("aid supported only on ARCnet");
7982 bpf_error("ARCnet address used in non-arc expression");
7987 static struct block
*
7988 gen_ahostop(eaddr
, dir
)
7989 register const u_char
*eaddr
;
7992 register struct block
*b0
, *b1
;
7995 /* src comes first, different from Ethernet */
7997 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
8000 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
8003 b0
= gen_ahostop(eaddr
, Q_SRC
);
8004 b1
= gen_ahostop(eaddr
, Q_DST
);
8010 b0
= gen_ahostop(eaddr
, Q_SRC
);
8011 b1
= gen_ahostop(eaddr
, Q_DST
);
8016 bpf_error("'addr1' is only supported on 802.11");
8020 bpf_error("'addr2' is only supported on 802.11");
8024 bpf_error("'addr3' is only supported on 802.11");
8028 bpf_error("'addr4' is only supported on 802.11");
8032 bpf_error("'ra' is only supported on 802.11");
8036 bpf_error("'ta' is only supported on 802.11");
8043 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8044 static struct block
*
8045 gen_vlan_bpf_extensions(int vlan_num
)
8047 struct block
*b0
, *b1
;
8050 /* generate new filter code based on extracting packet
8052 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8053 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8055 b0
= new_block(JMP(BPF_JEQ
));
8059 if (vlan_num
>= 0) {
8060 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8061 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8063 b1
= new_block(JMP(BPF_JEQ
));
8065 b1
->s
.k
= (bpf_int32
) vlan_num
;
8075 static struct block
*
8076 gen_vlan_no_bpf_extensions(int vlan_num
)
8078 struct block
*b0
, *b1
;
8080 /* check for VLAN, including QinQ */
8081 b0
= gen_linktype(ETHERTYPE_8021Q
);
8082 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
8086 /* If a specific VLAN is requested, check VLAN id */
8087 if (vlan_num
>= 0) {
8088 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
8089 (bpf_int32
)vlan_num
, 0x0fff);
8095 * The payload follows the full header, including the
8096 * VLAN tags, so skip past this VLAN tag.
8098 off_linkpl_constant_part
+= 4;
8101 * The link-layer type information follows the VLAN tags, so
8102 * skip past this VLAN tag.
8110 * support IEEE 802.1Q VLAN trunk over ethernet
8118 /* can't check for VLAN-encapsulated packets inside MPLS */
8119 if (label_stack_depth
> 0)
8120 bpf_error("no VLAN match after MPLS");
8123 * Check for a VLAN packet, and then change the offsets to point
8124 * to the type and data fields within the VLAN packet. Just
8125 * increment the offsets, so that we can support a hierarchy, e.g.
8126 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8129 * XXX - this is a bit of a kludge. If we were to split the
8130 * compiler into a parser that parses an expression and
8131 * generates an expression tree, and a code generator that
8132 * takes an expression tree (which could come from our
8133 * parser or from some other parser) and generates BPF code,
8134 * we could perhaps make the offsets parameters of routines
8135 * and, in the handler for an "AND" node, pass to subnodes
8136 * other than the VLAN node the adjusted offsets.
8138 * This would mean that "vlan" would, instead of changing the
8139 * behavior of *all* tests after it, change only the behavior
8140 * of tests ANDed with it. That would change the documented
8141 * semantics of "vlan", which might break some expressions.
8142 * However, it would mean that "(vlan and ip) or ip" would check
8143 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8144 * checking only for VLAN-encapsulated IP, so that could still
8145 * be considered worth doing; it wouldn't break expressions
8146 * that are of the form "vlan and ..." or "vlan N and ...",
8147 * which I suspect are the most common expressions involving
8148 * "vlan". "vlan or ..." doesn't necessarily do what the user
8149 * would really want, now, as all the "or ..." tests would
8150 * be done assuming a VLAN, even though the "or" could be viewed
8151 * as meaning "or, if this isn't a VLAN packet...".
8156 case DLT_NETANALYZER
:
8157 case DLT_NETANALYZER_TRANSPARENT
:
8158 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8159 if (vlan_stack_depth
== 0) {
8161 * Do we need special VLAN handling?
8163 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8164 b0
= gen_vlan_bpf_extensions(vlan_num
);
8166 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8169 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8172 bpf_error("no VLAN support for data link type %d",
8189 struct block
*b0
, *b1
;
8191 if (label_stack_depth
> 0) {
8192 /* just match the bottom-of-stack bit clear */
8193 b0
= gen_mcmp(OR_LINKPL
, off_nl
-2, BPF_B
, 0, 0x01);
8196 * We're not in an MPLS stack yet, so check the link-layer
8197 * type against MPLS.
8201 case DLT_C_HDLC
: /* fall through */
8203 case DLT_NETANALYZER
:
8204 case DLT_NETANALYZER_TRANSPARENT
:
8205 b0
= gen_linktype(ETHERTYPE_MPLS
);
8209 b0
= gen_linktype(PPP_MPLS_UCAST
);
8212 /* FIXME add other DLT_s ...
8213 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8214 * leave it for now */
8217 bpf_error("no MPLS support for data link type %d",
8225 /* If a specific MPLS label is requested, check it */
8226 if (label_num
>= 0) {
8227 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8228 b1
= gen_mcmp(OR_LINKPL
, off_nl
, BPF_W
, (bpf_int32
)label_num
,
8229 0xfffff000); /* only compare the first 20 bits */
8235 * Change the offsets to point to the type and data fields within
8236 * the MPLS packet. Just increment the offsets, so that we
8237 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8238 * capture packets with an outer label of 100000 and an inner
8241 * Increment the MPLS stack depth as well; this indicates that
8242 * we're checking MPLS-encapsulated headers, to make sure higher
8243 * level code generators don't try to match against IP-related
8244 * protocols such as Q_ARP, Q_RARP etc.
8246 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8250 label_stack_depth
++;
8255 * Support PPPOE discovery and session.
8260 /* check for PPPoE discovery */
8261 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8265 gen_pppoes(sess_num
)
8268 struct block
*b0
, *b1
;
8271 * Test against the PPPoE session link-layer type.
8273 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8275 /* If a specific session is requested, check PPPoE session id */
8276 if (sess_num
>= 0) {
8277 b1
= gen_mcmp(OR_LINKPL
, off_nl
, BPF_W
,
8278 (bpf_int32
)sess_num
, 0x0000ffff);
8286 * Change the offsets to point to the type and data fields within
8287 * the PPP packet, and note that this is PPPoE rather than
8290 * XXX - this is a bit of a kludge. If we were to split the
8291 * compiler into a parser that parses an expression and
8292 * generates an expression tree, and a code generator that
8293 * takes an expression tree (which could come from our
8294 * parser or from some other parser) and generates BPF code,
8295 * we could perhaps make the offsets parameters of routines
8296 * and, in the handler for an "AND" node, pass to subnodes
8297 * other than the PPPoE node the adjusted offsets.
8299 * This would mean that "pppoes" would, instead of changing the
8300 * behavior of *all* tests after it, change only the behavior
8301 * of tests ANDed with it. That would change the documented
8302 * semantics of "pppoes", which might break some expressions.
8303 * However, it would mean that "(pppoes and ip) or ip" would check
8304 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8305 * checking only for VLAN-encapsulated IP, so that could still
8306 * be considered worth doing; it wouldn't break expressions
8307 * that are of the form "pppoes and ..." which I suspect are the
8308 * most common expressions involving "pppoes". "pppoes or ..."
8309 * doesn't necessarily do what the user would really want, now,
8310 * as all the "or ..." tests would be done assuming PPPoE, even
8311 * though the "or" could be viewed as meaning "or, if this isn't
8312 * a PPPoE packet...".
8314 * The "network-layer" protocol is PPPoE, which has a 6-byte
8315 * PPPoE header, followed by a PPP packet.
8317 * There is no HDLC encapsulation for the PPP packet (it's
8318 * encapsulated in PPPoES instead), so the link-layer type
8319 * starts at the first byte of the PPP packet. For PPPoE,
8320 * that offset is relative to the beginning of the total
8321 * link-layer payload, including any 802.2 LLC header, so
8322 * it's 6 bytes past off_nl.
8324 off_linktype
= off_nl
+ 6;
8327 * The network-layer offsets are relative to the beginning
8328 * of the link-layer payload; that's past the 6-byte
8329 * PPPoE header and the 2-byte PPP header.
8332 off_nl_nosnap
= 6+2;
8338 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8350 bpf_error("'vpi' supported only on raw ATM");
8351 if (off_vpi
== (u_int
)-1)
8353 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8359 bpf_error("'vci' supported only on raw ATM");
8360 if (off_vci
== (u_int
)-1)
8362 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8367 if (off_proto
== (u_int
)-1)
8368 abort(); /* XXX - this isn't on FreeBSD */
8369 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8374 if (off_payload
== (u_int
)-1)
8376 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8377 0xffffffff, jtype
, reverse
, jvalue
);
8382 bpf_error("'callref' supported only on raw ATM");
8383 if (off_proto
== (u_int
)-1)
8385 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8386 jtype
, reverse
, jvalue
);
8396 gen_atmtype_abbrev(type
)
8399 struct block
*b0
, *b1
;
8404 /* Get all packets in Meta signalling Circuit */
8406 bpf_error("'metac' supported only on raw ATM");
8407 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8408 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8413 /* Get all packets in Broadcast Circuit*/
8415 bpf_error("'bcc' supported only on raw ATM");
8416 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8417 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8422 /* Get all cells in Segment OAM F4 circuit*/
8424 bpf_error("'oam4sc' supported only on raw ATM");
8425 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8426 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8431 /* Get all cells in End-to-End OAM F4 Circuit*/
8433 bpf_error("'oam4ec' supported only on raw ATM");
8434 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8435 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8440 /* Get all packets in connection Signalling Circuit */
8442 bpf_error("'sc' supported only on raw ATM");
8443 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8444 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8449 /* Get all packets in ILMI Circuit */
8451 bpf_error("'ilmic' supported only on raw ATM");
8452 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8453 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8458 /* Get all LANE packets */
8460 bpf_error("'lane' supported only on raw ATM");
8461 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8464 * Arrange that all subsequent tests assume LANE
8465 * rather than LLC-encapsulated packets, and set
8466 * the offsets appropriately for LANE-encapsulated
8469 * "off_mac" is the offset of the Ethernet header,
8470 * which is 2 bytes past the ATM pseudo-header
8471 * (skipping the pseudo-header and 2-byte LE Client
8472 * field). The other offsets are Ethernet offsets
8473 * relative to "off_mac".
8476 off_mac
= off_payload
+ 2; /* MAC header */
8477 off_linktype
= off_mac
+ 12;
8478 off_linkpl_constant_part
= off_mac
+ 14; /* Ethernet */
8479 off_nl
= 0; /* Ethernet II */
8480 off_nl_nosnap
= 3; /* 802.3+802.2 */
8484 /* Get all LLC-encapsulated packets */
8486 bpf_error("'llc' supported only on raw ATM");
8487 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8498 * Filtering for MTP2 messages based on li value
8499 * FISU, length is null
8500 * LSSU, length is 1 or 2
8501 * MSU, length is 3 or more
8502 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8505 gen_mtp2type_abbrev(type
)
8508 struct block
*b0
, *b1
;
8513 if ( (linktype
!= DLT_MTP2
) &&
8514 (linktype
!= DLT_ERF
) &&
8515 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8516 bpf_error("'fisu' supported only on MTP2");
8517 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8518 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8522 if ( (linktype
!= DLT_MTP2
) &&
8523 (linktype
!= DLT_ERF
) &&
8524 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8525 bpf_error("'lssu' supported only on MTP2");
8526 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8527 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8532 if ( (linktype
!= DLT_MTP2
) &&
8533 (linktype
!= DLT_ERF
) &&
8534 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8535 bpf_error("'msu' supported only on MTP2");
8536 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8540 if ( (linktype
!= DLT_MTP2
) &&
8541 (linktype
!= DLT_ERF
) &&
8542 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8543 bpf_error("'hfisu' supported only on MTP2_HSL");
8544 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8545 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8549 if ( (linktype
!= DLT_MTP2
) &&
8550 (linktype
!= DLT_ERF
) &&
8551 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8552 bpf_error("'hlssu' supported only on MTP2_HSL");
8553 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8554 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8559 if ( (linktype
!= DLT_MTP2
) &&
8560 (linktype
!= DLT_ERF
) &&
8561 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8562 bpf_error("'hmsu' supported only on MTP2_HSL");
8563 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8573 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8580 bpf_u_int32 val1
, val2
, val3
;
8581 u_int newoff_sio
=off_sio
;
8582 u_int newoff_opc
=off_opc
;
8583 u_int newoff_dpc
=off_dpc
;
8584 u_int newoff_sls
=off_sls
;
8586 switch (mtp3field
) {
8589 newoff_sio
+= 3; /* offset for MTP2_HSL */
8593 if (off_sio
== (u_int
)-1)
8594 bpf_error("'sio' supported only on SS7");
8595 /* sio coded on 1 byte so max value 255 */
8597 bpf_error("sio value %u too big; max value = 255",
8599 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8600 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8606 if (off_opc
== (u_int
)-1)
8607 bpf_error("'opc' supported only on SS7");
8608 /* opc coded on 14 bits so max value 16383 */
8610 bpf_error("opc value %u too big; max value = 16383",
8612 /* the following instructions are made to convert jvalue
8613 * to the form used to write opc in an ss7 message*/
8614 val1
= jvalue
& 0x00003c00;
8616 val2
= jvalue
& 0x000003fc;
8618 val3
= jvalue
& 0x00000003;
8620 jvalue
= val1
+ val2
+ val3
;
8621 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8622 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8630 if (off_dpc
== (u_int
)-1)
8631 bpf_error("'dpc' supported only on SS7");
8632 /* dpc coded on 14 bits so max value 16383 */
8634 bpf_error("dpc value %u too big; max value = 16383",
8636 /* the following instructions are made to convert jvalue
8637 * to the forme used to write dpc in an ss7 message*/
8638 val1
= jvalue
& 0x000000ff;
8640 val2
= jvalue
& 0x00003f00;
8642 jvalue
= val1
+ val2
;
8643 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8644 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8650 if (off_sls
== (u_int
)-1)
8651 bpf_error("'sls' supported only on SS7");
8652 /* sls coded on 4 bits so max value 15 */
8654 bpf_error("sls value %u too big; max value = 15",
8656 /* the following instruction is made to convert jvalue
8657 * to the forme used to write sls in an ss7 message*/
8658 jvalue
= jvalue
<< 4;
8659 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8660 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8669 static struct block
*
8670 gen_msg_abbrev(type
)
8676 * Q.2931 signalling protocol messages for handling virtual circuits
8677 * establishment and teardown
8682 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8686 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8690 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8694 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8698 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8701 case A_RELEASE_DONE
:
8702 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8712 gen_atmmulti_abbrev(type
)
8715 struct block
*b0
, *b1
;
8721 bpf_error("'oam' supported only on raw ATM");
8722 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8727 bpf_error("'oamf4' supported only on raw ATM");
8729 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8730 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8732 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8738 * Get Q.2931 signalling messages for switched
8739 * virtual connection
8742 bpf_error("'connectmsg' supported only on raw ATM");
8743 b0
= gen_msg_abbrev(A_SETUP
);
8744 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8746 b0
= gen_msg_abbrev(A_CONNECT
);
8748 b0
= gen_msg_abbrev(A_CONNECTACK
);
8750 b0
= gen_msg_abbrev(A_RELEASE
);
8752 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8754 b0
= gen_atmtype_abbrev(A_SC
);
8760 bpf_error("'metaconnect' supported only on raw ATM");
8761 b0
= gen_msg_abbrev(A_SETUP
);
8762 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8764 b0
= gen_msg_abbrev(A_CONNECT
);
8766 b0
= gen_msg_abbrev(A_RELEASE
);
8768 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8770 b0
= gen_atmtype_abbrev(A_METAC
);