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
170 OR_PACKET
, /* relative to the beginning of the packet */
171 OR_LINK
, /* relative to the beginning of the link-layer header */
172 OR_MACPL
, /* relative to the end of the MAC-layer header */
173 OR_NET
, /* relative to the network-layer header */
174 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
175 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
176 OR_TRAN_IPV6
/* relative to the 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_macplrel(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_macpl(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 * This is the offset of the beginning of the MAC-layer payload,
803 * from the beginning of the raw packet data.
805 * I.e., it's the sum of the length of the link-layer header (without,
806 * for example, any 802.2 LLC header, so it's the MAC-layer
807 * portion of that header), plus any prefix preceding the
810 static u_int off_macpl
;
813 * This is 1 if the offset of the beginning of the MAC-layer payload
814 * from the beginning of the link-layer header is variable-length.
816 static int off_macpl_is_variable
;
819 * If the link layer has variable_length headers, "reg_off_macpl"
820 * is the register number for a register containing the length of the
821 * link-layer header plus the length of any variable-length header
822 * preceding the link-layer header. Otherwise, "reg_off_macpl"
825 static int reg_off_macpl
;
828 * "off_linktype" is the offset to information in the link-layer header
829 * giving the packet type. This offset is relative to the beginning
830 * of the link-layer header - i.e., it doesn't include off_ll - so
831 * loads with an offset that includes "off_linktype" should use
834 * For Ethernet, it's the offset of the Ethernet type field; this
835 * means that it must have a value that skips VLAN tags.
837 * For link-layer types that always use 802.2 headers, it's the
838 * offset of the LLC header; this means that it must have a value
839 * that skips VLAN tags.
841 * For PPP, it's the offset of the PPP type field.
843 * For Cisco HDLC, it's the offset of the CHDLC type field.
845 * For BSD loopback, it's the offset of the AF_ value.
847 * For Linux cooked sockets, it's the offset of the type field.
849 * It's set to -1 for no encapsulation, in which case, IP is assumed.
851 static u_int off_linktype
;
854 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
855 * checks to check the PPP header, assumed to follow a LAN-style link-
856 * layer header and a PPPoE session header.
858 static int is_pppoes
= 0;
861 * TRUE if the link layer includes an ATM pseudo-header.
863 static int is_atm
= 0;
866 * TRUE if "lane" appeared in the filter; it causes us to generate
867 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
869 static int is_lane
= 0;
872 * These are offsets for the ATM pseudo-header.
874 static u_int off_vpi
;
875 static u_int off_vci
;
876 static u_int off_proto
;
879 * These are offsets for the MTP2 fields.
882 static u_int off_li_hsl
;
885 * These are offsets for the MTP3 fields.
887 static u_int off_sio
;
888 static u_int off_opc
;
889 static u_int off_dpc
;
890 static u_int off_sls
;
893 * This is the offset of the first byte after the ATM pseudo_header,
894 * or -1 if there is no ATM pseudo-header.
896 static u_int off_payload
;
899 * These are offsets to the beginning of the network-layer header.
900 * They are relative to the beginning of the MAC-layer payload (i.e.,
901 * they don't include off_ll or off_macpl).
903 * If the link layer never uses 802.2 LLC:
905 * "off_nl" and "off_nl_nosnap" are the same.
907 * If the link layer always uses 802.2 LLC:
909 * "off_nl" is the offset if there's a SNAP header following
912 * "off_nl_nosnap" is the offset if there's no SNAP header.
914 * If the link layer is Ethernet:
916 * "off_nl" is the offset if the packet is an Ethernet II packet
917 * (we assume no 802.3+802.2+SNAP);
919 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
920 * with an 802.2 header following it.
923 static u_int off_nl_nosnap
;
931 linktype
= pcap_datalink(p
);
932 pcap_fddipad
= p
->fddipad
;
935 * Assume it's not raw ATM with a pseudo-header, for now.
946 * And that we're not doing PPPoE.
951 * And assume we're not doing SS7.
961 * Also assume it's not 802.11.
965 off_macpl_is_variable
= 0;
967 label_stack_depth
= 0;
968 vlan_stack_depth
= 0;
978 off_nl
= 0; /* XXX in reality, variable! */
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
982 case DLT_ARCNET_LINUX
:
985 off_nl
= 0; /* XXX in reality, variable! */
986 off_nl_nosnap
= 0; /* no 802.2 LLC */
991 off_macpl
= 14; /* Ethernet header length */
992 off_nl
= 0; /* Ethernet II */
993 off_nl_nosnap
= 3; /* 802.3+802.2 */
998 * SLIP doesn't have a link level type. The 16 byte
999 * header is hacked into our SLIP driver.
1004 off_nl_nosnap
= 0; /* no 802.2 LLC */
1007 case DLT_SLIP_BSDOS
:
1008 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1021 off_nl_nosnap
= 0; /* no 802.2 LLC */
1028 off_nl_nosnap
= 0; /* no 802.2 LLC */
1033 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1034 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1038 off_nl_nosnap
= 0; /* no 802.2 LLC */
1043 * This does no include the Ethernet header, and
1044 * only covers session state.
1049 off_nl_nosnap
= 0; /* no 802.2 LLC */
1056 off_nl_nosnap
= 0; /* no 802.2 LLC */
1061 * FDDI doesn't really have a link-level type field.
1062 * We set "off_linktype" to the offset of the LLC header.
1064 * To check for Ethernet types, we assume that SSAP = SNAP
1065 * is being used and pick out the encapsulated Ethernet type.
1066 * XXX - should we generate code to check for SNAP?
1069 off_linktype
+= pcap_fddipad
;
1070 off_macpl
= 13; /* FDDI MAC header length */
1071 off_macpl
+= pcap_fddipad
;
1072 off_nl
= 8; /* 802.2+SNAP */
1073 off_nl_nosnap
= 3; /* 802.2 */
1078 * Token Ring doesn't really have a link-level type field.
1079 * We set "off_linktype" to the offset of the LLC header.
1081 * To check for Ethernet types, we assume that SSAP = SNAP
1082 * is being used and pick out the encapsulated Ethernet type.
1083 * XXX - should we generate code to check for SNAP?
1085 * XXX - the header is actually variable-length.
1086 * Some various Linux patched versions gave 38
1087 * as "off_linktype" and 40 as "off_nl"; however,
1088 * if a token ring packet has *no* routing
1089 * information, i.e. is not source-routed, the correct
1090 * values are 20 and 22, as they are in the vanilla code.
1092 * A packet is source-routed iff the uppermost bit
1093 * of the first byte of the source address, at an
1094 * offset of 8, has the uppermost bit set. If the
1095 * packet is source-routed, the total number of bytes
1096 * of routing information is 2 plus bits 0x1F00 of
1097 * the 16-bit value at an offset of 14 (shifted right
1098 * 8 - figure out which byte that is).
1101 off_macpl
= 14; /* Token Ring MAC header length */
1102 off_nl
= 8; /* 802.2+SNAP */
1103 off_nl_nosnap
= 3; /* 802.2 */
1106 case DLT_IEEE802_11
:
1107 case DLT_PRISM_HEADER
:
1108 case DLT_IEEE802_11_RADIO_AVS
:
1109 case DLT_IEEE802_11_RADIO
:
1111 * 802.11 doesn't really have a link-level type field.
1112 * We set "off_linktype" to the offset of the LLC header.
1114 * To check for Ethernet types, we assume that SSAP = SNAP
1115 * is being used and pick out the encapsulated Ethernet type.
1116 * XXX - should we generate code to check for SNAP?
1118 * We also handle variable-length radio headers here.
1119 * The Prism header is in theory variable-length, but in
1120 * practice it's always 144 bytes long. However, some
1121 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1122 * sometimes or always supply an AVS header, so we
1123 * have to check whether the radio header is a Prism
1124 * header or an AVS header, so, in practice, it's
1128 off_macpl
= 0; /* link-layer header is variable-length */
1129 off_macpl_is_variable
= 1;
1130 off_nl
= 8; /* 802.2+SNAP */
1131 off_nl_nosnap
= 3; /* 802.2 */
1136 * At the moment we treat PPI the same way that we treat
1137 * normal Radiotap encoded packets. The difference is in
1138 * the function that generates the code at the beginning
1139 * to compute the header length. Since this code generator
1140 * of PPI supports bare 802.11 encapsulation only (i.e.
1141 * the encapsulated DLT should be DLT_IEEE802_11) we
1142 * generate code to check for this too.
1145 off_macpl
= 0; /* link-layer header is variable-length */
1146 off_macpl_is_variable
= 1;
1147 off_nl
= 8; /* 802.2+SNAP */
1148 off_nl_nosnap
= 3; /* 802.2 */
1151 case DLT_ATM_RFC1483
:
1152 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1154 * assume routed, non-ISO PDUs
1155 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1157 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1158 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1159 * latter would presumably be treated the way PPPoE
1160 * should be, so you can do "pppoe and udp port 2049"
1161 * or "pppoa and tcp port 80" and have it check for
1162 * PPPo{A,E} and a PPP protocol of IP and....
1165 off_macpl
= 0; /* packet begins with LLC header */
1166 off_nl
= 8; /* 802.2+SNAP */
1167 off_nl_nosnap
= 3; /* 802.2 */
1172 * Full Frontal ATM; you get AALn PDUs with an ATM
1176 off_vpi
= SUNATM_VPI_POS
;
1177 off_vci
= SUNATM_VCI_POS
;
1178 off_proto
= PROTO_POS
;
1179 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1180 off_payload
= SUNATM_PKT_BEGIN_POS
;
1181 off_linktype
= off_payload
;
1182 off_macpl
= off_payload
; /* if LLC-encapsulated */
1183 off_nl
= 8; /* 802.2+SNAP */
1184 off_nl_nosnap
= 3; /* 802.2 */
1193 off_nl_nosnap
= 0; /* no 802.2 LLC */
1196 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1200 off_nl_nosnap
= 0; /* no 802.2 LLC */
1205 * LocalTalk does have a 1-byte type field in the LLAP header,
1206 * but really it just indicates whether there is a "short" or
1207 * "long" DDP packet following.
1212 off_nl_nosnap
= 0; /* no 802.2 LLC */
1215 case DLT_IP_OVER_FC
:
1217 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1218 * link-level type field. We set "off_linktype" to the
1219 * offset of the LLC header.
1221 * To check for Ethernet types, we assume that SSAP = SNAP
1222 * is being used and pick out the encapsulated Ethernet type.
1223 * XXX - should we generate code to check for SNAP? RFC
1224 * 2625 says SNAP should be used.
1228 off_nl
= 8; /* 802.2+SNAP */
1229 off_nl_nosnap
= 3; /* 802.2 */
1234 * XXX - we should set this to handle SNAP-encapsulated
1235 * frames (NLPID of 0x80).
1240 off_nl_nosnap
= 0; /* no 802.2 LLC */
1244 * the only BPF-interesting FRF.16 frames are non-control frames;
1245 * Frame Relay has a variable length link-layer
1246 * so lets start with offset 4 for now and increments later on (FIXME);
1252 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1255 case DLT_APPLE_IP_OVER_IEEE1394
:
1259 off_nl_nosnap
= 0; /* no 802.2 LLC */
1262 case DLT_SYMANTEC_FIREWALL
:
1265 off_nl
= 0; /* Ethernet II */
1266 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1269 #ifdef HAVE_NET_PFVAR_H
1272 off_macpl
= PFLOG_HDRLEN
;
1274 off_nl_nosnap
= 0; /* no 802.2 LLC */
1278 case DLT_JUNIPER_MFR
:
1279 case DLT_JUNIPER_MLFR
:
1280 case DLT_JUNIPER_MLPPP
:
1281 case DLT_JUNIPER_PPP
:
1282 case DLT_JUNIPER_CHDLC
:
1283 case DLT_JUNIPER_FRELAY
:
1287 off_nl_nosnap
= -1; /* no 802.2 LLC */
1290 case DLT_JUNIPER_ATM1
:
1291 off_linktype
= 4; /* in reality variable between 4-8 */
1292 off_macpl
= 4; /* in reality variable between 4-8 */
1297 case DLT_JUNIPER_ATM2
:
1298 off_linktype
= 8; /* in reality variable between 8-12 */
1299 off_macpl
= 8; /* in reality variable between 8-12 */
1304 /* frames captured on a Juniper PPPoE service PIC
1305 * contain raw ethernet frames */
1306 case DLT_JUNIPER_PPPOE
:
1307 case DLT_JUNIPER_ETHER
:
1310 off_nl
= 18; /* Ethernet II */
1311 off_nl_nosnap
= 21; /* 802.3+802.2 */
1314 case DLT_JUNIPER_PPPOE_ATM
:
1318 off_nl_nosnap
= -1; /* no 802.2 LLC */
1321 case DLT_JUNIPER_GGSN
:
1325 off_nl_nosnap
= -1; /* no 802.2 LLC */
1328 case DLT_JUNIPER_ES
:
1330 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl
= -1; /* not really a network layer but raw IP addresses */
1332 off_nl_nosnap
= -1; /* no 802.2 LLC */
1335 case DLT_JUNIPER_MONITOR
:
1338 off_nl
= 0; /* raw IP/IP6 header */
1339 off_nl_nosnap
= -1; /* no 802.2 LLC */
1342 case DLT_BACNET_MS_TP
:
1349 case DLT_JUNIPER_SERVICES
:
1351 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1352 off_nl
= -1; /* L3 proto location dep. on cookie type */
1353 off_nl_nosnap
= -1; /* no 802.2 LLC */
1356 case DLT_JUNIPER_VP
:
1363 case DLT_JUNIPER_ST
:
1370 case DLT_JUNIPER_ISM
:
1377 case DLT_JUNIPER_VS
:
1378 case DLT_JUNIPER_SRX_E2E
:
1379 case DLT_JUNIPER_FIBRECHANNEL
:
1380 case DLT_JUNIPER_ATM_CEMIC
:
1400 case DLT_MTP2_WITH_PHDR
:
1435 * Currently, only raw "link[N:M]" filtering is supported.
1437 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1439 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1440 off_nl_nosnap
= -1; /* no 802.2 LLC */
1441 off_mac
= 1; /* step over the kiss length byte */
1446 off_macpl
= 24; /* ipnet header length */
1451 case DLT_NETANALYZER
:
1452 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1453 off_linktype
= 16; /* includes 4-byte pseudo-header */
1454 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1455 off_nl
= 0; /* Ethernet II */
1456 off_nl_nosnap
= 3; /* 802.3+802.2 */
1459 case DLT_NETANALYZER_TRANSPARENT
:
1460 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1461 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1462 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1463 off_nl
= 0; /* Ethernet II */
1464 off_nl_nosnap
= 3; /* 802.3+802.2 */
1469 * For values in the range in which we've assigned new
1470 * DLT_ values, only raw "link[N:M]" filtering is supported.
1472 if (linktype
>= DLT_MATCHING_MIN
&&
1473 linktype
<= DLT_MATCHING_MAX
) {
1482 bpf_error("unknown data link type %d", linktype
);
1487 * Load a value relative to the beginning of the link-layer header.
1488 * The link-layer header doesn't necessarily begin at the beginning
1489 * of the packet data; there might be a variable-length prefix containing
1490 * radio information.
1492 static struct slist
*
1493 gen_load_llrel(offset
, size
)
1496 struct slist
*s
, *s2
;
1498 s
= gen_llprefixlen();
1501 * If "s" is non-null, it has code to arrange that the X register
1502 * contains the length of the prefix preceding the link-layer
1505 * Otherwise, the length of the prefix preceding the link-layer
1506 * header is "off_ll".
1510 * There's a variable-length prefix preceding the
1511 * link-layer header. "s" points to a list of statements
1512 * that put the length of that prefix into the X register.
1513 * do an indirect load, to use the X register as an offset.
1515 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1520 * There is no variable-length header preceding the
1521 * link-layer header; add in off_ll, which, if there's
1522 * a fixed-length header preceding the link-layer header,
1523 * is the length of that header.
1525 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1526 s
->s
.k
= offset
+ off_ll
;
1532 * Load a value relative to the beginning of the MAC-layer payload.
1534 static struct slist
*
1535 gen_load_macplrel(offset
, size
)
1538 struct slist
*s
, *s2
;
1540 s
= gen_off_macpl();
1543 * If s is non-null, the offset of the MAC-layer payload is
1544 * variable, and s points to a list of instructions that
1545 * arrange that the X register contains that offset.
1547 * Otherwise, the offset of the MAC-layer payload is constant,
1548 * and is in off_macpl.
1552 * The offset of the MAC-layer payload is in the X
1553 * register. Do an indirect load, to use the X register
1556 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1561 * The offset of the MAC-layer payload is constant,
1562 * and is in off_macpl; load the value at that offset
1563 * plus the specified offset.
1565 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1566 s
->s
.k
= off_macpl
+ offset
;
1572 * Load a value relative to the beginning of the specified header.
1574 static struct slist
*
1575 gen_load_a(offrel
, offset
, size
)
1576 enum e_offrel offrel
;
1579 struct slist
*s
, *s2
;
1584 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1589 s
= gen_load_llrel(offset
, size
);
1593 s
= gen_load_macplrel(offset
, size
);
1597 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1601 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1606 * Load the X register with the length of the IPv4 header
1607 * (plus the offset of the link-layer header, if it's
1608 * preceded by a variable-length header such as a radio
1609 * header), in bytes.
1611 s
= gen_loadx_iphdrlen();
1614 * Load the item at {offset of the MAC-layer payload} +
1615 * {offset, relative to the start of the MAC-layer
1616 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1617 * {specified offset}.
1619 * (If the offset of the MAC-layer payload is variable,
1620 * it's included in the value in the X register, and
1623 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1624 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1629 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1640 * Generate code to load into the X register the sum of the length of
1641 * the IPv4 header and any variable-length header preceding the link-layer
1644 static struct slist
*
1645 gen_loadx_iphdrlen()
1647 struct slist
*s
, *s2
;
1649 s
= gen_off_macpl();
1652 * There's a variable-length prefix preceding the
1653 * link-layer header, or the link-layer header is itself
1654 * variable-length. "s" points to a list of statements
1655 * that put the offset of the MAC-layer payload into
1658 * The 4*([k]&0xf) addressing mode can't be used, as we
1659 * don't have a constant offset, so we have to load the
1660 * value in question into the A register and add to it
1661 * the value from the X register.
1663 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1666 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1669 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1674 * The A register now contains the length of the
1675 * IP header. We need to add to it the offset of
1676 * the MAC-layer payload, which is still in the X
1677 * register, and move the result into the X register.
1679 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1680 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1683 * There is no variable-length header preceding the
1684 * link-layer header, and the link-layer header is
1685 * fixed-length; load the length of the IPv4 header,
1686 * which is at an offset of off_nl from the beginning
1687 * of the MAC-layer payload, and thus at an offset
1688 * of off_mac_pl + off_nl from the beginning of the
1691 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1692 s
->s
.k
= off_macpl
+ off_nl
;
1697 static struct block
*
1704 s
= new_stmt(BPF_LD
|BPF_IMM
);
1706 b
= new_block(JMP(BPF_JEQ
));
1712 static inline struct block
*
1715 return gen_uncond(1);
1718 static inline struct block
*
1721 return gen_uncond(0);
1725 * Byte-swap a 32-bit number.
1726 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1727 * big-endian platforms.)
1729 #define SWAPLONG(y) \
1730 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1733 * Generate code to match a particular packet type.
1735 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1736 * value, if <= ETHERMTU. We use that to determine whether to
1737 * match the type/length field or to check the type/length field for
1738 * a value <= ETHERMTU to see whether it's a type field and then do
1739 * the appropriate test.
1741 static struct block
*
1742 gen_ether_linktype(proto
)
1745 struct block
*b0
, *b1
;
1751 case LLCSAP_NETBEUI
:
1753 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1754 * so we check the DSAP and SSAP.
1756 * LLCSAP_IP checks for IP-over-802.2, rather
1757 * than IP-over-Ethernet or IP-over-SNAP.
1759 * XXX - should we check both the DSAP and the
1760 * SSAP, like this, or should we check just the
1761 * DSAP, as we do for other types <= ETHERMTU
1762 * (i.e., other SAP values)?
1764 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1766 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1767 ((proto
<< 8) | proto
));
1775 * Ethernet_II frames, which are Ethernet
1776 * frames with a frame type of ETHERTYPE_IPX;
1778 * Ethernet_802.3 frames, which are 802.3
1779 * frames (i.e., the type/length field is
1780 * a length field, <= ETHERMTU, rather than
1781 * a type field) with the first two bytes
1782 * after the Ethernet/802.3 header being
1785 * Ethernet_802.2 frames, which are 802.3
1786 * frames with an 802.2 LLC header and
1787 * with the IPX LSAP as the DSAP in the LLC
1790 * Ethernet_SNAP frames, which are 802.3
1791 * frames with an LLC header and a SNAP
1792 * header and with an OUI of 0x000000
1793 * (encapsulated Ethernet) and a protocol
1794 * ID of ETHERTYPE_IPX in the SNAP header.
1796 * XXX - should we generate the same code both
1797 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1801 * This generates code to check both for the
1802 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1804 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1805 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1809 * Now we add code to check for SNAP frames with
1810 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1812 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1816 * Now we generate code to check for 802.3
1817 * frames in general.
1819 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1823 * Now add the check for 802.3 frames before the
1824 * check for Ethernet_802.2 and Ethernet_802.3,
1825 * as those checks should only be done on 802.3
1826 * frames, not on Ethernet frames.
1831 * Now add the check for Ethernet_II frames, and
1832 * do that before checking for the other frame
1835 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1836 (bpf_int32
)ETHERTYPE_IPX
);
1840 case ETHERTYPE_ATALK
:
1841 case ETHERTYPE_AARP
:
1843 * EtherTalk (AppleTalk protocols on Ethernet link
1844 * layer) may use 802.2 encapsulation.
1848 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1849 * we check for an Ethernet type field less than
1850 * 1500, which means it's an 802.3 length field.
1852 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1856 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1857 * SNAP packets with an organization code of
1858 * 0x080007 (Apple, for Appletalk) and a protocol
1859 * type of ETHERTYPE_ATALK (Appletalk).
1861 * 802.2-encapsulated ETHERTYPE_AARP packets are
1862 * SNAP packets with an organization code of
1863 * 0x000000 (encapsulated Ethernet) and a protocol
1864 * type of ETHERTYPE_AARP (Appletalk ARP).
1866 if (proto
== ETHERTYPE_ATALK
)
1867 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1868 else /* proto == ETHERTYPE_AARP */
1869 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1873 * Check for Ethernet encapsulation (Ethertalk
1874 * phase 1?); we just check for the Ethernet
1877 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1883 if (proto
<= ETHERMTU
) {
1885 * This is an LLC SAP value, so the frames
1886 * that match would be 802.2 frames.
1887 * Check that the frame is an 802.2 frame
1888 * (i.e., that the length/type field is
1889 * a length field, <= ETHERMTU) and
1890 * then check the DSAP.
1892 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1894 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1900 * This is an Ethernet type, so compare
1901 * the length/type field with it (if
1902 * the frame is an 802.2 frame, the length
1903 * field will be <= ETHERMTU, and, as
1904 * "proto" is > ETHERMTU, this test
1905 * will fail and the frame won't match,
1906 * which is what we want).
1908 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1915 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1916 * or IPv6 then we have an error.
1918 static struct block
*
1919 gen_ipnet_linktype(proto
)
1925 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1926 (bpf_int32
)IPH_AF_INET
);
1929 case ETHERTYPE_IPV6
:
1930 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1931 (bpf_int32
)IPH_AF_INET6
);
1942 * Generate code to match a particular packet type.
1944 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1945 * value, if <= ETHERMTU. We use that to determine whether to
1946 * match the type field or to check the type field for the special
1947 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1949 static struct block
*
1950 gen_linux_sll_linktype(proto
)
1953 struct block
*b0
, *b1
;
1959 case LLCSAP_NETBEUI
:
1961 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1962 * so we check the DSAP and SSAP.
1964 * LLCSAP_IP checks for IP-over-802.2, rather
1965 * than IP-over-Ethernet or IP-over-SNAP.
1967 * XXX - should we check both the DSAP and the
1968 * SSAP, like this, or should we check just the
1969 * DSAP, as we do for other types <= ETHERMTU
1970 * (i.e., other SAP values)?
1972 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1973 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1974 ((proto
<< 8) | proto
));
1980 * Ethernet_II frames, which are Ethernet
1981 * frames with a frame type of ETHERTYPE_IPX;
1983 * Ethernet_802.3 frames, which have a frame
1984 * type of LINUX_SLL_P_802_3;
1986 * Ethernet_802.2 frames, which are 802.3
1987 * frames with an 802.2 LLC header (i.e, have
1988 * a frame type of LINUX_SLL_P_802_2) and
1989 * with the IPX LSAP as the DSAP in the LLC
1992 * Ethernet_SNAP frames, which are 802.3
1993 * frames with an LLC header and a SNAP
1994 * header and with an OUI of 0x000000
1995 * (encapsulated Ethernet) and a protocol
1996 * ID of ETHERTYPE_IPX in the SNAP header.
1998 * First, do the checks on LINUX_SLL_P_802_2
1999 * frames; generate the check for either
2000 * Ethernet_802.2 or Ethernet_SNAP frames, and
2001 * then put a check for LINUX_SLL_P_802_2 frames
2004 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2005 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2007 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2011 * Now check for 802.3 frames and OR that with
2012 * the previous test.
2014 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2018 * Now add the check for Ethernet_II frames, and
2019 * do that before checking for the other frame
2022 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2023 (bpf_int32
)ETHERTYPE_IPX
);
2027 case ETHERTYPE_ATALK
:
2028 case ETHERTYPE_AARP
:
2030 * EtherTalk (AppleTalk protocols on Ethernet link
2031 * layer) may use 802.2 encapsulation.
2035 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2036 * we check for the 802.2 protocol type in the
2037 * "Ethernet type" field.
2039 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2042 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2043 * SNAP packets with an organization code of
2044 * 0x080007 (Apple, for Appletalk) and a protocol
2045 * type of ETHERTYPE_ATALK (Appletalk).
2047 * 802.2-encapsulated ETHERTYPE_AARP packets are
2048 * SNAP packets with an organization code of
2049 * 0x000000 (encapsulated Ethernet) and a protocol
2050 * type of ETHERTYPE_AARP (Appletalk ARP).
2052 if (proto
== ETHERTYPE_ATALK
)
2053 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2054 else /* proto == ETHERTYPE_AARP */
2055 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2059 * Check for Ethernet encapsulation (Ethertalk
2060 * phase 1?); we just check for the Ethernet
2063 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2069 if (proto
<= ETHERMTU
) {
2071 * This is an LLC SAP value, so the frames
2072 * that match would be 802.2 frames.
2073 * Check for the 802.2 protocol type
2074 * in the "Ethernet type" field, and
2075 * then check the DSAP.
2077 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2079 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2085 * This is an Ethernet type, so compare
2086 * the length/type field with it (if
2087 * the frame is an 802.2 frame, the length
2088 * field will be <= ETHERMTU, and, as
2089 * "proto" is > ETHERMTU, this test
2090 * will fail and the frame won't match,
2091 * which is what we want).
2093 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2099 static struct slist
*
2100 gen_load_prism_llprefixlen()
2102 struct slist
*s1
, *s2
;
2103 struct slist
*sjeq_avs_cookie
;
2104 struct slist
*sjcommon
;
2107 * This code is not compatible with the optimizer, as
2108 * we are generating jmp instructions within a normal
2109 * slist of instructions
2114 * Generate code to load the length of the radio header into
2115 * the register assigned to hold that length, if one has been
2116 * assigned. (If one hasn't been assigned, no code we've
2117 * generated uses that prefix, so we don't need to generate any
2120 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2121 * or always use the AVS header rather than the Prism header.
2122 * We load a 4-byte big-endian value at the beginning of the
2123 * raw packet data, and see whether, when masked with 0xFFFFF000,
2124 * it's equal to 0x80211000. If so, that indicates that it's
2125 * an AVS header (the masked-out bits are the version number).
2126 * Otherwise, it's a Prism header.
2128 * XXX - the Prism header is also, in theory, variable-length,
2129 * but no known software generates headers that aren't 144
2132 if (reg_off_ll
!= -1) {
2136 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2140 * AND it with 0xFFFFF000.
2142 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2143 s2
->s
.k
= 0xFFFFF000;
2147 * Compare with 0x80211000.
2149 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2150 sjeq_avs_cookie
->s
.k
= 0x80211000;
2151 sappend(s1
, sjeq_avs_cookie
);
2156 * The 4 bytes at an offset of 4 from the beginning of
2157 * the AVS header are the length of the AVS header.
2158 * That field is big-endian.
2160 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2163 sjeq_avs_cookie
->s
.jt
= s2
;
2166 * Now jump to the code to allocate a register
2167 * into which to save the header length and
2168 * store the length there. (The "jump always"
2169 * instruction needs to have the k field set;
2170 * it's added to the PC, so, as we're jumping
2171 * over a single instruction, it should be 1.)
2173 sjcommon
= new_stmt(JMP(BPF_JA
));
2175 sappend(s1
, sjcommon
);
2178 * Now for the code that handles the Prism header.
2179 * Just load the length of the Prism header (144)
2180 * into the A register. Have the test for an AVS
2181 * header branch here if we don't have an AVS header.
2183 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2186 sjeq_avs_cookie
->s
.jf
= s2
;
2189 * Now allocate a register to hold that value and store
2190 * it. The code for the AVS header will jump here after
2191 * loading the length of the AVS header.
2193 s2
= new_stmt(BPF_ST
);
2194 s2
->s
.k
= reg_off_ll
;
2196 sjcommon
->s
.jf
= s2
;
2199 * Now move it into the X register.
2201 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2209 static struct slist
*
2210 gen_load_avs_llprefixlen()
2212 struct slist
*s1
, *s2
;
2215 * Generate code to load the length of the AVS header into
2216 * the register assigned to hold that length, if one has been
2217 * assigned. (If one hasn't been assigned, no code we've
2218 * generated uses that prefix, so we don't need to generate any
2221 if (reg_off_ll
!= -1) {
2223 * The 4 bytes at an offset of 4 from the beginning of
2224 * the AVS header are the length of the AVS header.
2225 * That field is big-endian.
2227 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2231 * Now allocate a register to hold that value and store
2234 s2
= new_stmt(BPF_ST
);
2235 s2
->s
.k
= reg_off_ll
;
2239 * Now move it into the X register.
2241 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2249 static struct slist
*
2250 gen_load_radiotap_llprefixlen()
2252 struct slist
*s1
, *s2
;
2255 * Generate code to load the length of the radiotap header into
2256 * the register assigned to hold that length, if one has been
2257 * assigned. (If one hasn't been assigned, no code we've
2258 * generated uses that prefix, so we don't need to generate any
2261 if (reg_off_ll
!= -1) {
2263 * The 2 bytes at offsets of 2 and 3 from the beginning
2264 * of the radiotap header are the length of the radiotap
2265 * header; unfortunately, it's little-endian, so we have
2266 * to load it a byte at a time and construct the value.
2270 * Load the high-order byte, at an offset of 3, shift it
2271 * left a byte, and put the result in the X register.
2273 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2275 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2278 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2282 * Load the next byte, at an offset of 2, and OR the
2283 * value from the X register into it.
2285 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2288 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2292 * Now allocate a register to hold that value and store
2295 s2
= new_stmt(BPF_ST
);
2296 s2
->s
.k
= reg_off_ll
;
2300 * Now move it into the X register.
2302 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2311 * At the moment we treat PPI as normal Radiotap encoded
2312 * packets. The difference is in the function that generates
2313 * the code at the beginning to compute the header length.
2314 * Since this code generator of PPI supports bare 802.11
2315 * encapsulation only (i.e. the encapsulated DLT should be
2316 * DLT_IEEE802_11) we generate code to check for this too;
2317 * that's done in finish_parse().
2319 static struct slist
*
2320 gen_load_ppi_llprefixlen()
2322 struct slist
*s1
, *s2
;
2325 * Generate code to load the length of the radiotap header
2326 * into the register assigned to hold that length, if one has
2329 if (reg_off_ll
!= -1) {
2331 * The 2 bytes at offsets of 2 and 3 from the beginning
2332 * of the radiotap header are the length of the radiotap
2333 * header; unfortunately, it's little-endian, so we have
2334 * to load it a byte at a time and construct the value.
2338 * Load the high-order byte, at an offset of 3, shift it
2339 * left a byte, and put the result in the X register.
2341 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2343 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2346 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2350 * Load the next byte, at an offset of 2, and OR the
2351 * value from the X register into it.
2353 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2356 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2360 * Now allocate a register to hold that value and store
2363 s2
= new_stmt(BPF_ST
);
2364 s2
->s
.k
= reg_off_ll
;
2368 * Now move it into the X register.
2370 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2379 * Load a value relative to the beginning of the link-layer header after the 802.11
2380 * header, i.e. LLC_SNAP.
2381 * The link-layer header doesn't necessarily begin at the beginning
2382 * of the packet data; there might be a variable-length prefix containing
2383 * radio information.
2385 static struct slist
*
2386 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2389 struct slist
*sjset_data_frame_1
;
2390 struct slist
*sjset_data_frame_2
;
2391 struct slist
*sjset_qos
;
2392 struct slist
*sjset_radiotap_flags
;
2393 struct slist
*sjset_radiotap_tsft
;
2394 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2395 struct slist
*s_roundup
;
2397 if (reg_off_macpl
== -1) {
2399 * No register has been assigned to the offset of
2400 * the MAC-layer payload, which means nobody needs
2401 * it; don't bother computing it - just return
2402 * what we already have.
2408 * This code is not compatible with the optimizer, as
2409 * we are generating jmp instructions within a normal
2410 * slist of instructions
2415 * If "s" is non-null, it has code to arrange that the X register
2416 * contains the length of the prefix preceding the link-layer
2419 * Otherwise, the length of the prefix preceding the link-layer
2420 * header is "off_ll".
2424 * There is no variable-length header preceding the
2425 * link-layer header.
2427 * Load the length of the fixed-length prefix preceding
2428 * the link-layer header (if any) into the X register,
2429 * and store it in the reg_off_macpl register.
2430 * That length is off_ll.
2432 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2437 * The X register contains the offset of the beginning of the
2438 * link-layer header; add 24, which is the minimum length
2439 * of the MAC header for a data frame, to that, and store it
2440 * in reg_off_macpl, and then load the Frame Control field,
2441 * which is at the offset in the X register, with an indexed load.
2443 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2445 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2448 s2
= new_stmt(BPF_ST
);
2449 s2
->s
.k
= reg_off_macpl
;
2452 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2457 * Check the Frame Control field to see if this is a data frame;
2458 * a data frame has the 0x08 bit (b3) in that field set and the
2459 * 0x04 bit (b2) clear.
2461 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2462 sjset_data_frame_1
->s
.k
= 0x08;
2463 sappend(s
, sjset_data_frame_1
);
2466 * If b3 is set, test b2, otherwise go to the first statement of
2467 * the rest of the program.
2469 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2470 sjset_data_frame_2
->s
.k
= 0x04;
2471 sappend(s
, sjset_data_frame_2
);
2472 sjset_data_frame_1
->s
.jf
= snext
;
2475 * If b2 is not set, this is a data frame; test the QoS bit.
2476 * Otherwise, go to the first statement of the rest of the
2479 sjset_data_frame_2
->s
.jt
= snext
;
2480 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2481 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2482 sappend(s
, sjset_qos
);
2485 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2487 * Otherwise, go to the first statement of the rest of the
2490 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2491 s2
->s
.k
= reg_off_macpl
;
2493 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2496 s2
= new_stmt(BPF_ST
);
2497 s2
->s
.k
= reg_off_macpl
;
2501 * If we have a radiotap header, look at it to see whether
2502 * there's Atheros padding between the MAC-layer header
2505 * Note: all of the fields in the radiotap header are
2506 * little-endian, so we byte-swap all of the values
2507 * we test against, as they will be loaded as big-endian
2510 if (linktype
== DLT_IEEE802_11_RADIO
) {
2512 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2513 * in the presence flag?
2515 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2519 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2520 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2521 sappend(s
, sjset_radiotap_flags
);
2524 * If not, skip all of this.
2526 sjset_radiotap_flags
->s
.jf
= snext
;
2529 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2531 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2532 new_stmt(JMP(BPF_JSET
));
2533 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2534 sappend(s
, sjset_radiotap_tsft
);
2537 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2538 * at an offset of 16 from the beginning of the raw packet
2539 * data (8 bytes for the radiotap header and 8 bytes for
2542 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2545 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2549 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2550 sjset_tsft_datapad
->s
.k
= 0x20;
2551 sappend(s
, sjset_tsft_datapad
);
2554 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2555 * at an offset of 8 from the beginning of the raw packet
2556 * data (8 bytes for the radiotap header).
2558 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2561 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2565 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2566 sjset_notsft_datapad
->s
.k
= 0x20;
2567 sappend(s
, sjset_notsft_datapad
);
2570 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2571 * set, round the length of the 802.11 header to
2572 * a multiple of 4. Do that by adding 3 and then
2573 * dividing by and multiplying by 4, which we do by
2576 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2577 s_roundup
->s
.k
= reg_off_macpl
;
2578 sappend(s
, s_roundup
);
2579 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2582 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2585 s2
= new_stmt(BPF_ST
);
2586 s2
->s
.k
= reg_off_macpl
;
2589 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2590 sjset_tsft_datapad
->s
.jf
= snext
;
2591 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2592 sjset_notsft_datapad
->s
.jf
= snext
;
2594 sjset_qos
->s
.jf
= snext
;
2600 insert_compute_vloffsets(b
)
2606 * For link-layer types that have a variable-length header
2607 * preceding the link-layer header, generate code to load
2608 * the offset of the link-layer header into the register
2609 * assigned to that offset, if any.
2613 case DLT_PRISM_HEADER
:
2614 s
= gen_load_prism_llprefixlen();
2617 case DLT_IEEE802_11_RADIO_AVS
:
2618 s
= gen_load_avs_llprefixlen();
2621 case DLT_IEEE802_11_RADIO
:
2622 s
= gen_load_radiotap_llprefixlen();
2626 s
= gen_load_ppi_llprefixlen();
2635 * For link-layer types that have a variable-length link-layer
2636 * header, generate code to load the offset of the MAC-layer
2637 * payload into the register assigned to that offset, if any.
2641 case DLT_IEEE802_11
:
2642 case DLT_PRISM_HEADER
:
2643 case DLT_IEEE802_11_RADIO_AVS
:
2644 case DLT_IEEE802_11_RADIO
:
2646 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2651 * If we have any offset-loading code, append all the
2652 * existing statements in the block to those statements,
2653 * and make the resulting list the list of statements
2657 sappend(s
, b
->stmts
);
2662 static struct block
*
2663 gen_ppi_dlt_check(void)
2665 struct slist
*s_load_dlt
;
2668 if (linktype
== DLT_PPI
)
2670 /* Create the statements that check for the DLT
2672 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2673 s_load_dlt
->s
.k
= 4;
2675 b
= new_block(JMP(BPF_JEQ
));
2677 b
->stmts
= s_load_dlt
;
2678 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2688 static struct slist
*
2689 gen_prism_llprefixlen(void)
2693 if (reg_off_ll
== -1) {
2695 * We haven't yet assigned a register for the length
2696 * of the radio header; allocate one.
2698 reg_off_ll
= alloc_reg();
2702 * Load the register containing the radio length
2703 * into the X register.
2705 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2706 s
->s
.k
= reg_off_ll
;
2710 static struct slist
*
2711 gen_avs_llprefixlen(void)
2715 if (reg_off_ll
== -1) {
2717 * We haven't yet assigned a register for the length
2718 * of the AVS header; allocate one.
2720 reg_off_ll
= alloc_reg();
2724 * Load the register containing the AVS length
2725 * into the X register.
2727 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2728 s
->s
.k
= reg_off_ll
;
2732 static struct slist
*
2733 gen_radiotap_llprefixlen(void)
2737 if (reg_off_ll
== -1) {
2739 * We haven't yet assigned a register for the length
2740 * of the radiotap header; allocate one.
2742 reg_off_ll
= alloc_reg();
2746 * Load the register containing the radiotap length
2747 * into the X register.
2749 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2750 s
->s
.k
= reg_off_ll
;
2755 * At the moment we treat PPI as normal Radiotap encoded
2756 * packets. The difference is in the function that generates
2757 * the code at the beginning to compute the header length.
2758 * Since this code generator of PPI supports bare 802.11
2759 * encapsulation only (i.e. the encapsulated DLT should be
2760 * DLT_IEEE802_11) we generate code to check for this too.
2762 static struct slist
*
2763 gen_ppi_llprefixlen(void)
2767 if (reg_off_ll
== -1) {
2769 * We haven't yet assigned a register for the length
2770 * of the radiotap header; allocate one.
2772 reg_off_ll
= alloc_reg();
2776 * Load the register containing the PPI length
2777 * into the X register.
2779 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2780 s
->s
.k
= reg_off_ll
;
2785 * Generate code to compute the link-layer header length, if necessary,
2786 * putting it into the X register, and to return either a pointer to a
2787 * "struct slist" for the list of statements in that code, or NULL if
2788 * no code is necessary.
2790 static struct slist
*
2791 gen_llprefixlen(void)
2795 case DLT_PRISM_HEADER
:
2796 return gen_prism_llprefixlen();
2798 case DLT_IEEE802_11_RADIO_AVS
:
2799 return gen_avs_llprefixlen();
2801 case DLT_IEEE802_11_RADIO
:
2802 return gen_radiotap_llprefixlen();
2805 return gen_ppi_llprefixlen();
2813 * Generate code to load the register containing the offset of the
2814 * MAC-layer payload into the X register; if no register for that offset
2815 * has been allocated, allocate it first.
2817 static struct slist
*
2822 if (off_macpl_is_variable
) {
2823 if (reg_off_macpl
== -1) {
2825 * We haven't yet assigned a register for the offset
2826 * of the MAC-layer payload; allocate one.
2828 reg_off_macpl
= alloc_reg();
2832 * Load the register containing the offset of the MAC-layer
2833 * payload into the X register.
2835 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2836 s
->s
.k
= reg_off_macpl
;
2840 * That offset isn't variable, so we don't need to
2841 * generate any code.
2848 * Map an Ethernet type to the equivalent PPP type.
2851 ethertype_to_ppptype(proto
)
2860 case ETHERTYPE_IPV6
:
2868 case ETHERTYPE_ATALK
:
2882 * I'm assuming the "Bridging PDU"s that go
2883 * over PPP are Spanning Tree Protocol
2897 * Generate code to match a particular packet type by matching the
2898 * link-layer type field or fields in the 802.2 LLC header.
2900 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2901 * value, if <= ETHERMTU.
2903 static struct block
*
2907 struct block
*b0
, *b1
, *b2
;
2908 const char *description
;
2910 /* are we checking MPLS-encapsulated packets? */
2911 if (label_stack_depth
> 0) {
2915 /* FIXME add other L3 proto IDs */
2916 return gen_mpls_linktype(Q_IP
);
2918 case ETHERTYPE_IPV6
:
2920 /* FIXME add other L3 proto IDs */
2921 return gen_mpls_linktype(Q_IPV6
);
2924 bpf_error("unsupported protocol over mpls");
2930 * Are we testing PPPoE packets?
2934 * The PPPoE session header is part of the
2935 * MAC-layer payload, so all references
2936 * should be relative to the beginning of
2941 * We use Ethernet protocol types inside libpcap;
2942 * map them to the corresponding PPP protocol types.
2944 proto
= ethertype_to_ppptype(proto
);
2945 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2951 case DLT_NETANALYZER
:
2952 case DLT_NETANALYZER_TRANSPARENT
:
2953 return gen_ether_linktype(proto
);
2961 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2965 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2972 case DLT_IEEE802_11
:
2973 case DLT_PRISM_HEADER
:
2974 case DLT_IEEE802_11_RADIO_AVS
:
2975 case DLT_IEEE802_11_RADIO
:
2978 * Check that we have a data frame.
2980 b0
= gen_check_802_11_data_frame();
2983 * Now check for the specified link-layer type.
2985 b1
= gen_llc_linktype(proto
);
2993 * XXX - check for LLC frames.
2995 return gen_llc_linktype(proto
);
3001 * XXX - check for LLC PDUs, as per IEEE 802.5.
3003 return gen_llc_linktype(proto
);
3007 case DLT_ATM_RFC1483
:
3009 case DLT_IP_OVER_FC
:
3010 return gen_llc_linktype(proto
);
3016 * If "is_lane" is set, check for a LANE-encapsulated
3017 * version of this protocol, otherwise check for an
3018 * LLC-encapsulated version of this protocol.
3020 * We assume LANE means Ethernet, not Token Ring.
3024 * Check that the packet doesn't begin with an
3025 * LE Control marker. (We've already generated
3028 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3033 * Now generate an Ethernet test.
3035 b1
= gen_ether_linktype(proto
);
3040 * Check for LLC encapsulation and then check the
3043 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3044 b1
= gen_llc_linktype(proto
);
3052 return gen_linux_sll_linktype(proto
);
3057 case DLT_SLIP_BSDOS
:
3060 * These types don't provide any type field; packets
3061 * are always IPv4 or IPv6.
3063 * XXX - for IPv4, check for a version number of 4, and,
3064 * for IPv6, check for a version number of 6?
3069 /* Check for a version number of 4. */
3070 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3072 case ETHERTYPE_IPV6
:
3073 /* Check for a version number of 6. */
3074 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3077 return gen_false(); /* always false */
3084 * Raw IPv4, so no type field.
3086 if (proto
== ETHERTYPE_IP
)
3087 return gen_true(); /* always true */
3089 /* Checking for something other than IPv4; always false */
3096 * Raw IPv6, so no type field.
3098 if (proto
== ETHERTYPE_IPV6
)
3099 return gen_true(); /* always true */
3101 /* Checking for something other than IPv6; always false */
3108 case DLT_PPP_SERIAL
:
3111 * We use Ethernet protocol types inside libpcap;
3112 * map them to the corresponding PPP protocol types.
3114 proto
= ethertype_to_ppptype(proto
);
3115 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3121 * We use Ethernet protocol types inside libpcap;
3122 * map them to the corresponding PPP protocol types.
3128 * Also check for Van Jacobson-compressed IP.
3129 * XXX - do this for other forms of PPP?
3131 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3132 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3134 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3139 proto
= ethertype_to_ppptype(proto
);
3140 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3150 * For DLT_NULL, the link-layer header is a 32-bit
3151 * word containing an AF_ value in *host* byte order,
3152 * and for DLT_ENC, the link-layer header begins
3153 * with a 32-bit work containing an AF_ value in
3156 * In addition, if we're reading a saved capture file,
3157 * the host byte order in the capture may not be the
3158 * same as the host byte order on this machine.
3160 * For DLT_LOOP, the link-layer header is a 32-bit
3161 * word containing an AF_ value in *network* byte order.
3163 * XXX - AF_ values may, unfortunately, be platform-
3164 * dependent; for example, FreeBSD's AF_INET6 is 24
3165 * whilst NetBSD's and OpenBSD's is 26.
3167 * This means that, when reading a capture file, just
3168 * checking for our AF_INET6 value won't work if the
3169 * capture file came from another OS.
3178 case ETHERTYPE_IPV6
:
3185 * Not a type on which we support filtering.
3186 * XXX - support those that have AF_ values
3187 * #defined on this platform, at least?
3192 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3194 * The AF_ value is in host byte order, but
3195 * the BPF interpreter will convert it to
3196 * network byte order.
3198 * If this is a save file, and it's from a
3199 * machine with the opposite byte order to
3200 * ours, we byte-swap the AF_ value.
3202 * Then we run it through "htonl()", and
3203 * generate code to compare against the result.
3205 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3206 proto
= SWAPLONG(proto
);
3207 proto
= htonl(proto
);
3209 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3211 #ifdef HAVE_NET_PFVAR_H
3214 * af field is host byte order in contrast to the rest of
3217 if (proto
== ETHERTYPE_IP
)
3218 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3219 BPF_B
, (bpf_int32
)AF_INET
));
3220 else if (proto
== ETHERTYPE_IPV6
)
3221 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3222 BPF_B
, (bpf_int32
)AF_INET6
));
3227 #endif /* HAVE_NET_PFVAR_H */
3230 case DLT_ARCNET_LINUX
:
3232 * XXX should we check for first fragment if the protocol
3240 case ETHERTYPE_IPV6
:
3241 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3242 (bpf_int32
)ARCTYPE_INET6
));
3245 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3246 (bpf_int32
)ARCTYPE_IP
);
3247 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3248 (bpf_int32
)ARCTYPE_IP_OLD
);
3253 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3254 (bpf_int32
)ARCTYPE_ARP
);
3255 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3256 (bpf_int32
)ARCTYPE_ARP_OLD
);
3260 case ETHERTYPE_REVARP
:
3261 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3262 (bpf_int32
)ARCTYPE_REVARP
));
3264 case ETHERTYPE_ATALK
:
3265 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3266 (bpf_int32
)ARCTYPE_ATALK
));
3273 case ETHERTYPE_ATALK
:
3283 * XXX - assumes a 2-byte Frame Relay header with
3284 * DLCI and flags. What if the address is longer?
3290 * Check for the special NLPID for IP.
3292 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3294 case ETHERTYPE_IPV6
:
3296 * Check for the special NLPID for IPv6.
3298 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3302 * Check for several OSI protocols.
3304 * Frame Relay packets typically have an OSI
3305 * NLPID at the beginning; we check for each
3308 * What we check for is the NLPID and a frame
3309 * control field of UI, i.e. 0x03 followed
3312 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3313 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3314 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3326 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3328 case DLT_JUNIPER_MFR
:
3329 case DLT_JUNIPER_MLFR
:
3330 case DLT_JUNIPER_MLPPP
:
3331 case DLT_JUNIPER_ATM1
:
3332 case DLT_JUNIPER_ATM2
:
3333 case DLT_JUNIPER_PPPOE
:
3334 case DLT_JUNIPER_PPPOE_ATM
:
3335 case DLT_JUNIPER_GGSN
:
3336 case DLT_JUNIPER_ES
:
3337 case DLT_JUNIPER_MONITOR
:
3338 case DLT_JUNIPER_SERVICES
:
3339 case DLT_JUNIPER_ETHER
:
3340 case DLT_JUNIPER_PPP
:
3341 case DLT_JUNIPER_FRELAY
:
3342 case DLT_JUNIPER_CHDLC
:
3343 case DLT_JUNIPER_VP
:
3344 case DLT_JUNIPER_ST
:
3345 case DLT_JUNIPER_ISM
:
3346 case DLT_JUNIPER_VS
:
3347 case DLT_JUNIPER_SRX_E2E
:
3348 case DLT_JUNIPER_FIBRECHANNEL
:
3349 case DLT_JUNIPER_ATM_CEMIC
:
3351 /* just lets verify the magic number for now -
3352 * on ATM we may have up to 6 different encapsulations on the wire
3353 * and need a lot of heuristics to figure out that the payload
3356 * FIXME encapsulation specific BPF_ filters
3358 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3360 case DLT_BACNET_MS_TP
:
3361 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3364 return gen_ipnet_linktype(proto
);
3366 case DLT_LINUX_IRDA
:
3367 bpf_error("IrDA link-layer type filtering not implemented");
3370 bpf_error("DOCSIS link-layer type filtering not implemented");
3373 case DLT_MTP2_WITH_PHDR
:
3374 bpf_error("MTP2 link-layer type filtering not implemented");
3377 bpf_error("ERF link-layer type filtering not implemented");
3380 bpf_error("PFSYNC link-layer type filtering not implemented");
3382 case DLT_LINUX_LAPD
:
3383 bpf_error("LAPD link-layer type filtering not implemented");
3387 case DLT_USB_LINUX_MMAPPED
:
3388 bpf_error("USB link-layer type filtering not implemented");
3390 case DLT_BLUETOOTH_HCI_H4
:
3391 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3392 bpf_error("Bluetooth link-layer type filtering not implemented");
3395 case DLT_CAN_SOCKETCAN
:
3396 bpf_error("CAN link-layer type filtering not implemented");
3398 case DLT_IEEE802_15_4
:
3399 case DLT_IEEE802_15_4_LINUX
:
3400 case DLT_IEEE802_15_4_NONASK_PHY
:
3401 case DLT_IEEE802_15_4_NOFCS
:
3402 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3404 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3405 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3408 bpf_error("SITA link-layer type filtering not implemented");
3411 bpf_error("RAIF1 link-layer type filtering not implemented");
3414 bpf_error("IPMB link-layer type filtering not implemented");
3417 bpf_error("AX.25 link-layer type filtering not implemented");
3420 /* Using the fixed-size NFLOG header it is possible to tell only
3421 * the address family of the packet, other meaningful data is
3422 * either missing or behind TLVs.
3424 bpf_error("NFLOG link-layer type filtering not implemented");
3428 * Does this link-layer header type have a field
3429 * indicating the type of the next protocol? If
3430 * so, off_linktype will be the offset of that
3431 * field in the packet; if not, it will be -1.
3433 if (off_linktype
!= (u_int
)-1) {
3435 * Yes; assume it's an Ethernet type. (If
3436 * it's not, it needs to be handled specially
3439 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3442 * No; report an error.
3444 description
= pcap_datalink_val_to_description(linktype
);
3445 if (description
!= NULL
) {
3446 bpf_error("%s link-layer type filtering not implemented",
3449 bpf_error("DLT %u link-layer type filtering not implemented",
3458 * Check for an LLC SNAP packet with a given organization code and
3459 * protocol type; we check the entire contents of the 802.2 LLC and
3460 * snap headers, checking for DSAP and SSAP of SNAP and a control
3461 * field of 0x03 in the LLC header, and for the specified organization
3462 * code and protocol type in the SNAP header.
3464 static struct block
*
3465 gen_snap(orgcode
, ptype
)
3466 bpf_u_int32 orgcode
;
3469 u_char snapblock
[8];
3471 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3472 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3473 snapblock
[2] = 0x03; /* control = UI */
3474 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3475 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3476 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3477 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3478 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3479 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3483 * Generate code to match frames with an LLC header.
3488 struct block
*b0
, *b1
;
3494 * We check for an Ethernet type field less than
3495 * 1500, which means it's an 802.3 length field.
3497 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
3501 * Now check for the purported DSAP and SSAP not being
3502 * 0xFF, to rule out NetWare-over-802.3.
3504 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3511 * We check for LLC traffic.
3513 b0
= gen_atmtype_abbrev(A_LLC
);
3516 case DLT_IEEE802
: /* Token Ring */
3518 * XXX - check for LLC frames.
3524 * XXX - check for LLC frames.
3528 case DLT_ATM_RFC1483
:
3530 * For LLC encapsulation, these are defined to have an
3533 * For VC encapsulation, they don't, but there's no
3534 * way to check for that; the protocol used on the VC
3535 * is negotiated out of band.
3539 case DLT_IEEE802_11
:
3540 case DLT_PRISM_HEADER
:
3541 case DLT_IEEE802_11_RADIO
:
3542 case DLT_IEEE802_11_RADIO_AVS
:
3545 * Check that we have a data frame.
3547 b0
= gen_check_802_11_data_frame();
3551 bpf_error("'llc' not supported for linktype %d", linktype
);
3559 struct block
*b0
, *b1
;
3563 * Check whether this is an LLC frame.
3568 * Load the control byte and test the low-order bit; it must
3569 * be clear for I frames.
3571 s
= gen_load_a(OR_MACPL
, 2, BPF_B
);
3572 b1
= new_block(JMP(BPF_JSET
));
3583 struct block
*b0
, *b1
;
3586 * Check whether this is an LLC frame.
3591 * Now compare the low-order 2 bit of the control byte against
3592 * the appropriate value for S frames.
3594 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3602 struct block
*b0
, *b1
;
3605 * Check whether this is an LLC frame.
3610 * Now compare the low-order 2 bit of the control byte against
3611 * the appropriate value for U frames.
3613 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3619 gen_llc_s_subtype(bpf_u_int32 subtype
)
3621 struct block
*b0
, *b1
;
3624 * Check whether this is an LLC frame.
3629 * Now check for an S frame with the appropriate type.
3631 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3637 gen_llc_u_subtype(bpf_u_int32 subtype
)
3639 struct block
*b0
, *b1
;
3642 * Check whether this is an LLC frame.
3647 * Now check for a U frame with the appropriate type.
3649 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3655 * Generate code to match a particular packet type, for link-layer types
3656 * using 802.2 LLC headers.
3658 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3659 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3661 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3662 * value, if <= ETHERMTU. We use that to determine whether to
3663 * match the DSAP or both DSAP and LSAP or to check the OUI and
3664 * protocol ID in a SNAP header.
3666 static struct block
*
3667 gen_llc_linktype(proto
)
3671 * XXX - handle token-ring variable-length header.
3677 case LLCSAP_NETBEUI
:
3679 * XXX - should we check both the DSAP and the
3680 * SSAP, like this, or should we check just the
3681 * DSAP, as we do for other types <= ETHERMTU
3682 * (i.e., other SAP values)?
3684 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3685 ((proto
<< 8) | proto
));
3689 * XXX - are there ever SNAP frames for IPX on
3690 * non-Ethernet 802.x networks?
3692 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3693 (bpf_int32
)LLCSAP_IPX
);
3695 case ETHERTYPE_ATALK
:
3697 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3698 * SNAP packets with an organization code of
3699 * 0x080007 (Apple, for Appletalk) and a protocol
3700 * type of ETHERTYPE_ATALK (Appletalk).
3702 * XXX - check for an organization code of
3703 * encapsulated Ethernet as well?
3705 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3709 * XXX - we don't have to check for IPX 802.3
3710 * here, but should we check for the IPX Ethertype?
3712 if (proto
<= ETHERMTU
) {
3714 * This is an LLC SAP value, so check
3717 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3720 * This is an Ethernet type; we assume that it's
3721 * unlikely that it'll appear in the right place
3722 * at random, and therefore check only the
3723 * location that would hold the Ethernet type
3724 * in a SNAP frame with an organization code of
3725 * 0x000000 (encapsulated Ethernet).
3727 * XXX - if we were to check for the SNAP DSAP and
3728 * LSAP, as per XXX, and were also to check for an
3729 * organization code of 0x000000 (encapsulated
3730 * Ethernet), we'd do
3732 * return gen_snap(0x000000, proto);
3734 * here; for now, we don't, as per the above.
3735 * I don't know whether it's worth the extra CPU
3736 * time to do the right check or not.
3738 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3743 static struct block
*
3744 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3748 u_int src_off
, dst_off
;
3750 struct block
*b0
, *b1
;
3764 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3765 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3771 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3772 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3779 b0
= gen_linktype(proto
);
3780 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3786 static struct block
*
3787 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3788 struct in6_addr
*addr
;
3789 struct in6_addr
*mask
;
3791 u_int src_off
, dst_off
;
3793 struct block
*b0
, *b1
;
3808 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3809 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3815 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3816 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3823 /* this order is important */
3824 a
= (u_int32_t
*)addr
;
3825 m
= (u_int32_t
*)mask
;
3826 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3827 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3829 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3831 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3833 b0
= gen_linktype(proto
);
3839 static struct block
*
3840 gen_ehostop(eaddr
, dir
)
3841 register const u_char
*eaddr
;
3844 register struct block
*b0
, *b1
;
3848 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3851 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3854 b0
= gen_ehostop(eaddr
, Q_SRC
);
3855 b1
= gen_ehostop(eaddr
, Q_DST
);
3861 b0
= gen_ehostop(eaddr
, Q_SRC
);
3862 b1
= gen_ehostop(eaddr
, Q_DST
);
3867 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3871 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3875 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3879 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3883 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3887 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3895 * Like gen_ehostop, but for DLT_FDDI
3897 static struct block
*
3898 gen_fhostop(eaddr
, dir
)
3899 register const u_char
*eaddr
;
3902 struct block
*b0
, *b1
;
3906 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3909 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3912 b0
= gen_fhostop(eaddr
, Q_SRC
);
3913 b1
= gen_fhostop(eaddr
, Q_DST
);
3919 b0
= gen_fhostop(eaddr
, Q_SRC
);
3920 b1
= gen_fhostop(eaddr
, Q_DST
);
3925 bpf_error("'addr1' is only supported on 802.11");
3929 bpf_error("'addr2' is only supported on 802.11");
3933 bpf_error("'addr3' is only supported on 802.11");
3937 bpf_error("'addr4' is only supported on 802.11");
3941 bpf_error("'ra' is only supported on 802.11");
3945 bpf_error("'ta' is only supported on 802.11");
3953 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3955 static struct block
*
3956 gen_thostop(eaddr
, dir
)
3957 register const u_char
*eaddr
;
3960 register struct block
*b0
, *b1
;
3964 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3967 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3970 b0
= gen_thostop(eaddr
, Q_SRC
);
3971 b1
= gen_thostop(eaddr
, Q_DST
);
3977 b0
= gen_thostop(eaddr
, Q_SRC
);
3978 b1
= gen_thostop(eaddr
, Q_DST
);
3983 bpf_error("'addr1' is only supported on 802.11");
3987 bpf_error("'addr2' is only supported on 802.11");
3991 bpf_error("'addr3' is only supported on 802.11");
3995 bpf_error("'addr4' is only supported on 802.11");
3999 bpf_error("'ra' is only supported on 802.11");
4003 bpf_error("'ta' is only supported on 802.11");
4011 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4012 * various 802.11 + radio headers.
4014 static struct block
*
4015 gen_wlanhostop(eaddr
, dir
)
4016 register const u_char
*eaddr
;
4019 register struct block
*b0
, *b1
, *b2
;
4020 register struct slist
*s
;
4022 #ifdef ENABLE_WLAN_FILTERING_PATCH
4025 * We need to disable the optimizer because the optimizer is buggy
4026 * and wipes out some LD instructions generated by the below
4027 * code to validate the Frame Control bits
4030 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4037 * For control frames, there is no SA.
4039 * For management frames, SA is at an
4040 * offset of 10 from the beginning of
4043 * For data frames, SA is at an offset
4044 * of 10 from the beginning of the packet
4045 * if From DS is clear, at an offset of
4046 * 16 from the beginning of the packet
4047 * if From DS is set and To DS is clear,
4048 * and an offset of 24 from the beginning
4049 * of the packet if From DS is set and To DS
4054 * Generate the tests to be done for data frames
4057 * First, check for To DS set, i.e. check "link[1] & 0x01".
4059 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4060 b1
= new_block(JMP(BPF_JSET
));
4061 b1
->s
.k
= 0x01; /* To DS */
4065 * If To DS is set, the SA is at 24.
4067 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4071 * Now, check for To DS not set, i.e. check
4072 * "!(link[1] & 0x01)".
4074 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4075 b2
= new_block(JMP(BPF_JSET
));
4076 b2
->s
.k
= 0x01; /* To DS */
4081 * If To DS is not set, the SA is at 16.
4083 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4087 * Now OR together the last two checks. That gives
4088 * the complete set of checks for data frames with
4094 * Now check for From DS being set, and AND that with
4095 * the ORed-together checks.
4097 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4098 b1
= new_block(JMP(BPF_JSET
));
4099 b1
->s
.k
= 0x02; /* From DS */
4104 * Now check for data frames with From DS not set.
4106 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4107 b2
= new_block(JMP(BPF_JSET
));
4108 b2
->s
.k
= 0x02; /* From DS */
4113 * If From DS isn't set, the SA is at 10.
4115 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4119 * Now OR together the checks for data frames with
4120 * From DS not set and for data frames with From DS
4121 * set; that gives the checks done for data frames.
4126 * Now check for a data frame.
4127 * I.e, check "link[0] & 0x08".
4129 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4130 b1
= new_block(JMP(BPF_JSET
));
4135 * AND that with the checks done for data frames.
4140 * If the high-order bit of the type value is 0, this
4141 * is a management frame.
4142 * I.e, check "!(link[0] & 0x08)".
4144 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4145 b2
= new_block(JMP(BPF_JSET
));
4151 * For management frames, the SA is at 10.
4153 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4157 * OR that with the checks done for data frames.
4158 * That gives the checks done for management and
4164 * If the low-order bit of the type value is 1,
4165 * this is either a control frame or a frame
4166 * with a reserved type, and thus not a
4169 * I.e., check "!(link[0] & 0x04)".
4171 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4172 b1
= new_block(JMP(BPF_JSET
));
4178 * AND that with the checks for data and management
4188 * For control frames, there is no DA.
4190 * For management frames, DA is at an
4191 * offset of 4 from the beginning of
4194 * For data frames, DA is at an offset
4195 * of 4 from the beginning of the packet
4196 * if To DS is clear and at an offset of
4197 * 16 from the beginning of the packet
4202 * Generate the tests to be done for data frames.
4204 * First, check for To DS set, i.e. "link[1] & 0x01".
4206 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4207 b1
= new_block(JMP(BPF_JSET
));
4208 b1
->s
.k
= 0x01; /* To DS */
4212 * If To DS is set, the DA is at 16.
4214 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4218 * Now, check for To DS not set, i.e. check
4219 * "!(link[1] & 0x01)".
4221 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4222 b2
= new_block(JMP(BPF_JSET
));
4223 b2
->s
.k
= 0x01; /* To DS */
4228 * If To DS is not set, the DA is at 4.
4230 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4234 * Now OR together the last two checks. That gives
4235 * the complete set of checks for data frames.
4240 * Now check for a data frame.
4241 * I.e, check "link[0] & 0x08".
4243 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4244 b1
= new_block(JMP(BPF_JSET
));
4249 * AND that with the checks done for data frames.
4254 * If the high-order bit of the type value is 0, this
4255 * is a management frame.
4256 * I.e, check "!(link[0] & 0x08)".
4258 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4259 b2
= new_block(JMP(BPF_JSET
));
4265 * For management frames, the DA is at 4.
4267 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4271 * OR that with the checks done for data frames.
4272 * That gives the checks done for management and
4278 * If the low-order bit of the type value is 1,
4279 * this is either a control frame or a frame
4280 * with a reserved type, and thus not a
4283 * I.e., check "!(link[0] & 0x04)".
4285 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4286 b1
= new_block(JMP(BPF_JSET
));
4292 * AND that with the checks for data and management
4300 * Not present in management frames; addr1 in other
4305 * If the high-order bit of the type value is 0, this
4306 * is a management frame.
4307 * I.e, check "(link[0] & 0x08)".
4309 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4310 b1
= new_block(JMP(BPF_JSET
));
4317 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4320 * AND that with the check of addr1.
4327 * Not present in management frames; addr2, if present,
4332 * Not present in CTS or ACK control frames.
4334 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4335 IEEE80211_FC0_TYPE_MASK
);
4337 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4338 IEEE80211_FC0_SUBTYPE_MASK
);
4340 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4341 IEEE80211_FC0_SUBTYPE_MASK
);
4347 * If the high-order bit of the type value is 0, this
4348 * is a management frame.
4349 * I.e, check "(link[0] & 0x08)".
4351 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4352 b1
= new_block(JMP(BPF_JSET
));
4357 * AND that with the check for frames other than
4358 * CTS and ACK frames.
4365 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4370 * XXX - add BSSID keyword?
4373 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4377 * Not present in CTS or ACK control frames.
4379 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4380 IEEE80211_FC0_TYPE_MASK
);
4382 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4383 IEEE80211_FC0_SUBTYPE_MASK
);
4385 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4386 IEEE80211_FC0_SUBTYPE_MASK
);
4390 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4396 * Not present in control frames.
4398 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4399 IEEE80211_FC0_TYPE_MASK
);
4401 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4407 * Present only if the direction mask has both "From DS"
4408 * and "To DS" set. Neither control frames nor management
4409 * frames should have both of those set, so we don't
4410 * check the frame type.
4412 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4413 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4414 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4419 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4420 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4426 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4427 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4436 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4437 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4438 * as the RFC states.)
4440 static struct block
*
4441 gen_ipfchostop(eaddr
, dir
)
4442 register const u_char
*eaddr
;
4445 register struct block
*b0
, *b1
;
4449 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4452 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4455 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4456 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4462 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4463 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4468 bpf_error("'addr1' is only supported on 802.11");
4472 bpf_error("'addr2' is only supported on 802.11");
4476 bpf_error("'addr3' is only supported on 802.11");
4480 bpf_error("'addr4' is only supported on 802.11");
4484 bpf_error("'ra' is only supported on 802.11");
4488 bpf_error("'ta' is only supported on 802.11");
4496 * This is quite tricky because there may be pad bytes in front of the
4497 * DECNET header, and then there are two possible data packet formats that
4498 * carry both src and dst addresses, plus 5 packet types in a format that
4499 * carries only the src node, plus 2 types that use a different format and
4500 * also carry just the src node.
4504 * Instead of doing those all right, we just look for data packets with
4505 * 0 or 1 bytes of padding. If you want to look at other packets, that
4506 * will require a lot more hacking.
4508 * To add support for filtering on DECNET "areas" (network numbers)
4509 * one would want to add a "mask" argument to this routine. That would
4510 * make the filter even more inefficient, although one could be clever
4511 * and not generate masking instructions if the mask is 0xFFFF.
4513 static struct block
*
4514 gen_dnhostop(addr
, dir
)
4518 struct block
*b0
, *b1
, *b2
, *tmp
;
4519 u_int offset_lh
; /* offset if long header is received */
4520 u_int offset_sh
; /* offset if short header is received */
4525 offset_sh
= 1; /* follows flags */
4526 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4530 offset_sh
= 3; /* follows flags, dstnode */
4531 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4535 /* Inefficient because we do our Calvinball dance twice */
4536 b0
= gen_dnhostop(addr
, Q_SRC
);
4537 b1
= gen_dnhostop(addr
, Q_DST
);
4543 /* Inefficient because we do our Calvinball dance twice */
4544 b0
= gen_dnhostop(addr
, Q_SRC
);
4545 b1
= gen_dnhostop(addr
, Q_DST
);
4550 bpf_error("ISO host filtering not implemented");
4555 b0
= gen_linktype(ETHERTYPE_DN
);
4556 /* Check for pad = 1, long header case */
4557 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4558 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4559 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4560 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4562 /* Check for pad = 0, long header case */
4563 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4564 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4567 /* Check for pad = 1, short header case */
4568 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4569 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4570 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4573 /* Check for pad = 0, short header case */
4574 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4575 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4579 /* Combine with test for linktype */
4585 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4586 * test the bottom-of-stack bit, and then check the version number
4587 * field in the IP header.
4589 static struct block
*
4590 gen_mpls_linktype(proto
)
4593 struct block
*b0
, *b1
;
4598 /* match the bottom-of-stack bit */
4599 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4600 /* match the IPv4 version number */
4601 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4606 /* match the bottom-of-stack bit */
4607 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4608 /* match the IPv4 version number */
4609 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4618 static struct block
*
4619 gen_host(addr
, mask
, proto
, dir
, type
)
4626 struct block
*b0
, *b1
;
4627 const char *typestr
;
4637 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4639 * Only check for non-IPv4 addresses if we're not
4640 * checking MPLS-encapsulated packets.
4642 if (label_stack_depth
== 0) {
4643 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4645 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4651 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4654 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4657 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4660 bpf_error("'tcp' modifier applied to %s", typestr
);
4663 bpf_error("'sctp' modifier applied to %s", typestr
);
4666 bpf_error("'udp' modifier applied to %s", typestr
);
4669 bpf_error("'icmp' modifier applied to %s", typestr
);
4672 bpf_error("'igmp' modifier applied to %s", typestr
);
4675 bpf_error("'igrp' modifier applied to %s", typestr
);
4678 bpf_error("'pim' modifier applied to %s", typestr
);
4681 bpf_error("'vrrp' modifier applied to %s", typestr
);
4684 bpf_error("'carp' modifier applied to %s", typestr
);
4687 bpf_error("ATALK host filtering not implemented");
4690 bpf_error("AARP host filtering not implemented");
4693 return gen_dnhostop(addr
, dir
);
4696 bpf_error("SCA host filtering not implemented");
4699 bpf_error("LAT host filtering not implemented");
4702 bpf_error("MOPDL host filtering not implemented");
4705 bpf_error("MOPRC host filtering not implemented");
4708 bpf_error("'ip6' modifier applied to ip host");
4711 bpf_error("'icmp6' modifier applied to %s", typestr
);
4714 bpf_error("'ah' modifier applied to %s", typestr
);
4717 bpf_error("'esp' modifier applied to %s", typestr
);
4720 bpf_error("ISO host filtering not implemented");
4723 bpf_error("'esis' modifier applied to %s", typestr
);
4726 bpf_error("'isis' modifier applied to %s", typestr
);
4729 bpf_error("'clnp' modifier applied to %s", typestr
);
4732 bpf_error("'stp' modifier applied to %s", typestr
);
4735 bpf_error("IPX host filtering not implemented");
4738 bpf_error("'netbeui' modifier applied to %s", typestr
);
4741 bpf_error("'radio' modifier applied to %s", typestr
);
4750 static struct block
*
4751 gen_host6(addr
, mask
, proto
, dir
, type
)
4752 struct in6_addr
*addr
;
4753 struct in6_addr
*mask
;
4758 const char *typestr
;
4768 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4771 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4774 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4777 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4780 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4783 bpf_error("'sctp' modifier applied to %s", typestr
);
4786 bpf_error("'tcp' modifier applied to %s", typestr
);
4789 bpf_error("'udp' modifier applied to %s", typestr
);
4792 bpf_error("'icmp' modifier applied to %s", typestr
);
4795 bpf_error("'igmp' modifier applied to %s", typestr
);
4798 bpf_error("'igrp' modifier applied to %s", typestr
);
4801 bpf_error("'pim' modifier applied to %s", typestr
);
4804 bpf_error("'vrrp' modifier applied to %s", typestr
);
4807 bpf_error("'carp' modifier applied to %s", typestr
);
4810 bpf_error("ATALK host filtering not implemented");
4813 bpf_error("AARP host filtering not implemented");
4816 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4819 bpf_error("SCA host filtering not implemented");
4822 bpf_error("LAT host filtering not implemented");
4825 bpf_error("MOPDL host filtering not implemented");
4828 bpf_error("MOPRC host filtering not implemented");
4831 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4834 bpf_error("'icmp6' modifier applied to %s", typestr
);
4837 bpf_error("'ah' modifier applied to %s", typestr
);
4840 bpf_error("'esp' modifier applied to %s", typestr
);
4843 bpf_error("ISO host filtering not implemented");
4846 bpf_error("'esis' modifier applied to %s", typestr
);
4849 bpf_error("'isis' modifier applied to %s", typestr
);
4852 bpf_error("'clnp' modifier applied to %s", typestr
);
4855 bpf_error("'stp' modifier applied to %s", typestr
);
4858 bpf_error("IPX host filtering not implemented");
4861 bpf_error("'netbeui' modifier applied to %s", typestr
);
4864 bpf_error("'radio' modifier applied to %s", typestr
);
4874 static struct block
*
4875 gen_gateway(eaddr
, alist
, proto
, dir
)
4876 const u_char
*eaddr
;
4877 bpf_u_int32
**alist
;
4881 struct block
*b0
, *b1
, *tmp
;
4884 bpf_error("direction applied to 'gateway'");
4893 case DLT_NETANALYZER
:
4894 case DLT_NETANALYZER_TRANSPARENT
:
4895 b0
= gen_ehostop(eaddr
, Q_OR
);
4898 b0
= gen_fhostop(eaddr
, Q_OR
);
4901 b0
= gen_thostop(eaddr
, Q_OR
);
4903 case DLT_IEEE802_11
:
4904 case DLT_PRISM_HEADER
:
4905 case DLT_IEEE802_11_RADIO_AVS
:
4906 case DLT_IEEE802_11_RADIO
:
4908 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4913 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4915 * Check that the packet doesn't begin with an
4916 * LE Control marker. (We've already generated
4919 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4924 * Now check the MAC address.
4926 b0
= gen_ehostop(eaddr
, Q_OR
);
4929 case DLT_IP_OVER_FC
:
4930 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4934 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4936 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4938 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4947 bpf_error("illegal modifier of 'gateway'");
4953 gen_proto_abbrev(proto
)
4962 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4963 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4968 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4969 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4974 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4975 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4980 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4983 #ifndef IPPROTO_IGMP
4984 #define IPPROTO_IGMP 2
4988 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4991 #ifndef IPPROTO_IGRP
4992 #define IPPROTO_IGRP 9
4995 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4999 #define IPPROTO_PIM 103
5003 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5004 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5008 #ifndef IPPROTO_VRRP
5009 #define IPPROTO_VRRP 112
5013 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5016 #ifndef IPPROTO_CARP
5017 #define IPPROTO_CARP 112
5021 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5025 b1
= gen_linktype(ETHERTYPE_IP
);
5029 b1
= gen_linktype(ETHERTYPE_ARP
);
5033 b1
= gen_linktype(ETHERTYPE_REVARP
);
5037 bpf_error("link layer applied in wrong context");
5040 b1
= gen_linktype(ETHERTYPE_ATALK
);
5044 b1
= gen_linktype(ETHERTYPE_AARP
);
5048 b1
= gen_linktype(ETHERTYPE_DN
);
5052 b1
= gen_linktype(ETHERTYPE_SCA
);
5056 b1
= gen_linktype(ETHERTYPE_LAT
);
5060 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5064 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5068 b1
= gen_linktype(ETHERTYPE_IPV6
);
5071 #ifndef IPPROTO_ICMPV6
5072 #define IPPROTO_ICMPV6 58
5075 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5079 #define IPPROTO_AH 51
5082 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5083 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5088 #define IPPROTO_ESP 50
5091 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5092 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5097 b1
= gen_linktype(LLCSAP_ISONS
);
5101 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5105 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5108 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5109 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5110 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5112 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5114 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5116 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5120 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5121 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5122 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5124 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5126 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5128 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5132 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5133 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5134 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5136 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5141 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5142 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5147 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5148 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5150 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5152 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5157 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5158 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5163 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5164 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5169 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5173 b1
= gen_linktype(LLCSAP_8021D
);
5177 b1
= gen_linktype(LLCSAP_IPX
);
5181 b1
= gen_linktype(LLCSAP_NETBEUI
);
5185 bpf_error("'radio' is not a valid protocol type");
5193 static struct block
*
5199 /* not IPv4 frag other than the first frag */
5200 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5201 b
= new_block(JMP(BPF_JSET
));
5210 * Generate a comparison to a port value in the transport-layer header
5211 * at the specified offset from the beginning of that header.
5213 * XXX - this handles a variable-length prefix preceding the link-layer
5214 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5215 * variable-length link-layer headers (such as Token Ring or 802.11
5218 static struct block
*
5219 gen_portatom(off
, v
)
5223 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5226 static struct block
*
5227 gen_portatom6(off
, v
)
5231 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5235 gen_portop(port
, proto
, dir
)
5236 int port
, proto
, dir
;
5238 struct block
*b0
, *b1
, *tmp
;
5240 /* ip proto 'proto' and not a fragment other than the first fragment */
5241 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5247 b1
= gen_portatom(0, (bpf_int32
)port
);
5251 b1
= gen_portatom(2, (bpf_int32
)port
);
5256 tmp
= gen_portatom(0, (bpf_int32
)port
);
5257 b1
= gen_portatom(2, (bpf_int32
)port
);
5262 tmp
= gen_portatom(0, (bpf_int32
)port
);
5263 b1
= gen_portatom(2, (bpf_int32
)port
);
5275 static struct block
*
5276 gen_port(port
, ip_proto
, dir
)
5281 struct block
*b0
, *b1
, *tmp
;
5286 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5287 * not LLC encapsulation with LLCSAP_IP.
5289 * For IEEE 802 networks - which includes 802.5 token ring
5290 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5291 * says that SNAP encapsulation is used, not LLC encapsulation
5294 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5295 * RFC 2225 say that SNAP encapsulation is used, not LLC
5296 * encapsulation with LLCSAP_IP.
5298 * So we always check for ETHERTYPE_IP.
5300 b0
= gen_linktype(ETHERTYPE_IP
);
5306 b1
= gen_portop(port
, ip_proto
, dir
);
5310 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5311 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5313 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5325 gen_portop6(port
, proto
, dir
)
5326 int port
, proto
, dir
;
5328 struct block
*b0
, *b1
, *tmp
;
5330 /* ip6 proto 'proto' */
5331 /* XXX - catch the first fragment of a fragmented packet? */
5332 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5336 b1
= gen_portatom6(0, (bpf_int32
)port
);
5340 b1
= gen_portatom6(2, (bpf_int32
)port
);
5345 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5346 b1
= gen_portatom6(2, (bpf_int32
)port
);
5351 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5352 b1
= gen_portatom6(2, (bpf_int32
)port
);
5364 static struct block
*
5365 gen_port6(port
, ip_proto
, dir
)
5370 struct block
*b0
, *b1
, *tmp
;
5372 /* link proto ip6 */
5373 b0
= gen_linktype(ETHERTYPE_IPV6
);
5379 b1
= gen_portop6(port
, ip_proto
, dir
);
5383 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5384 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5386 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5397 /* gen_portrange code */
5398 static struct block
*
5399 gen_portrangeatom(off
, v1
, v2
)
5403 struct block
*b1
, *b2
;
5407 * Reverse the order of the ports, so v1 is the lower one.
5416 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5417 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5425 gen_portrangeop(port1
, port2
, proto
, dir
)
5430 struct block
*b0
, *b1
, *tmp
;
5432 /* ip proto 'proto' and not a fragment other than the first fragment */
5433 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5439 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5443 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5448 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5449 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5454 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5455 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5467 static struct block
*
5468 gen_portrange(port1
, port2
, ip_proto
, dir
)
5473 struct block
*b0
, *b1
, *tmp
;
5476 b0
= gen_linktype(ETHERTYPE_IP
);
5482 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5486 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5487 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5489 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5500 static struct block
*
5501 gen_portrangeatom6(off
, v1
, v2
)
5505 struct block
*b1
, *b2
;
5509 * Reverse the order of the ports, so v1 is the lower one.
5518 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5519 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5527 gen_portrangeop6(port1
, port2
, proto
, dir
)
5532 struct block
*b0
, *b1
, *tmp
;
5534 /* ip6 proto 'proto' */
5535 /* XXX - catch the first fragment of a fragmented packet? */
5536 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5540 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5544 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5549 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5550 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5555 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5556 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5568 static struct block
*
5569 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5574 struct block
*b0
, *b1
, *tmp
;
5576 /* link proto ip6 */
5577 b0
= gen_linktype(ETHERTYPE_IPV6
);
5583 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5587 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5588 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5590 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5602 lookup_proto(name
, proto
)
5603 register const char *name
;
5613 v
= pcap_nametoproto(name
);
5614 if (v
== PROTO_UNDEF
)
5615 bpf_error("unknown ip proto '%s'", name
);
5619 /* XXX should look up h/w protocol type based on linktype */
5620 v
= pcap_nametoeproto(name
);
5621 if (v
== PROTO_UNDEF
) {
5622 v
= pcap_nametollc(name
);
5623 if (v
== PROTO_UNDEF
)
5624 bpf_error("unknown ether proto '%s'", name
);
5629 if (strcmp(name
, "esis") == 0)
5631 else if (strcmp(name
, "isis") == 0)
5633 else if (strcmp(name
, "clnp") == 0)
5636 bpf_error("unknown osi proto '%s'", name
);
5656 static struct block
*
5657 gen_protochain(v
, proto
, dir
)
5662 #ifdef NO_PROTOCHAIN
5663 return gen_proto(v
, proto
, dir
);
5665 struct block
*b0
, *b
;
5666 struct slist
*s
[100];
5667 int fix2
, fix3
, fix4
, fix5
;
5668 int ahcheck
, again
, end
;
5670 int reg2
= alloc_reg();
5672 memset(s
, 0, sizeof(s
));
5673 fix2
= fix3
= fix4
= fix5
= 0;
5680 b0
= gen_protochain(v
, Q_IP
, dir
);
5681 b
= gen_protochain(v
, Q_IPV6
, dir
);
5685 bpf_error("bad protocol applied for 'protochain'");
5690 * We don't handle variable-length prefixes before the link-layer
5691 * header, or variable-length link-layer headers, here yet.
5692 * We might want to add BPF instructions to do the protochain
5693 * work, to simplify that and, on platforms that have a BPF
5694 * interpreter with the new instructions, let the filtering
5695 * be done in the kernel. (We already require a modified BPF
5696 * engine to do the protochain stuff, to support backward
5697 * branches, and backward branch support is unlikely to appear
5698 * in kernel BPF engines.)
5702 case DLT_IEEE802_11
:
5703 case DLT_PRISM_HEADER
:
5704 case DLT_IEEE802_11_RADIO_AVS
:
5705 case DLT_IEEE802_11_RADIO
:
5707 bpf_error("'protochain' not supported with 802.11");
5710 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5713 * s[0] is a dummy entry to protect other BPF insn from damage
5714 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5715 * hard to find interdependency made by jump table fixup.
5718 s
[i
] = new_stmt(0); /*dummy*/
5723 b0
= gen_linktype(ETHERTYPE_IP
);
5726 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5727 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5729 /* X = ip->ip_hl << 2 */
5730 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5731 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5736 b0
= gen_linktype(ETHERTYPE_IPV6
);
5738 /* A = ip6->ip_nxt */
5739 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5740 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5742 /* X = sizeof(struct ip6_hdr) */
5743 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5749 bpf_error("unsupported proto to gen_protochain");
5753 /* again: if (A == v) goto end; else fall through; */
5755 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5757 s
[i
]->s
.jt
= NULL
; /*later*/
5758 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5762 #ifndef IPPROTO_NONE
5763 #define IPPROTO_NONE 59
5765 /* if (A == IPPROTO_NONE) goto end */
5766 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5767 s
[i
]->s
.jt
= NULL
; /*later*/
5768 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5769 s
[i
]->s
.k
= IPPROTO_NONE
;
5770 s
[fix5
]->s
.jf
= s
[i
];
5774 if (proto
== Q_IPV6
) {
5775 int v6start
, v6end
, v6advance
, j
;
5778 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5779 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5780 s
[i
]->s
.jt
= NULL
; /*later*/
5781 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5782 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5783 s
[fix2
]->s
.jf
= s
[i
];
5785 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5786 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5787 s
[i
]->s
.jt
= NULL
; /*later*/
5788 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5789 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5791 /* if (A == IPPROTO_ROUTING) goto v6advance */
5792 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5793 s
[i
]->s
.jt
= NULL
; /*later*/
5794 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5795 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5797 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5798 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5799 s
[i
]->s
.jt
= NULL
; /*later*/
5800 s
[i
]->s
.jf
= NULL
; /*later*/
5801 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5811 * A = P[X + packet head];
5812 * X = X + (P[X + packet head + 1] + 1) * 8;
5814 /* A = P[X + packet head] */
5815 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5816 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5819 s
[i
] = new_stmt(BPF_ST
);
5822 /* A = P[X + packet head + 1]; */
5823 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5824 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 1;
5827 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5831 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5835 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5839 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5842 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5846 /* goto again; (must use BPF_JA for backward jump) */
5847 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5848 s
[i
]->s
.k
= again
- i
- 1;
5849 s
[i
- 1]->s
.jf
= s
[i
];
5853 for (j
= v6start
; j
<= v6end
; j
++)
5854 s
[j
]->s
.jt
= s
[v6advance
];
5857 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5859 s
[fix2
]->s
.jf
= s
[i
];
5865 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5866 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5867 s
[i
]->s
.jt
= NULL
; /*later*/
5868 s
[i
]->s
.jf
= NULL
; /*later*/
5869 s
[i
]->s
.k
= IPPROTO_AH
;
5871 s
[fix3
]->s
.jf
= s
[ahcheck
];
5878 * X = X + (P[X + 1] + 2) * 4;
5881 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5883 /* A = P[X + packet head]; */
5884 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5885 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5888 s
[i
] = new_stmt(BPF_ST
);
5892 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5895 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5899 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5901 /* A = P[X + packet head] */
5902 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5903 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5906 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5910 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5914 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5917 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5921 /* goto again; (must use BPF_JA for backward jump) */
5922 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5923 s
[i
]->s
.k
= again
- i
- 1;
5928 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5930 s
[fix2
]->s
.jt
= s
[end
];
5931 s
[fix4
]->s
.jf
= s
[end
];
5932 s
[fix5
]->s
.jt
= s
[end
];
5939 for (i
= 0; i
< max
- 1; i
++)
5940 s
[i
]->next
= s
[i
+ 1];
5941 s
[max
- 1]->next
= NULL
;
5946 b
= new_block(JMP(BPF_JEQ
));
5947 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5957 static struct block
*
5958 gen_check_802_11_data_frame()
5961 struct block
*b0
, *b1
;
5964 * A data frame has the 0x08 bit (b3) in the frame control field set
5965 * and the 0x04 bit (b2) clear.
5967 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5968 b0
= new_block(JMP(BPF_JSET
));
5972 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5973 b1
= new_block(JMP(BPF_JSET
));
5984 * Generate code that checks whether the packet is a packet for protocol
5985 * <proto> and whether the type field in that protocol's header has
5986 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5987 * IP packet and checks the protocol number in the IP header against <v>.
5989 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5990 * against Q_IP and Q_IPV6.
5992 static struct block
*
5993 gen_proto(v
, proto
, dir
)
5998 struct block
*b0
, *b1
;
6003 if (dir
!= Q_DEFAULT
)
6004 bpf_error("direction applied to 'proto'");
6008 b0
= gen_proto(v
, Q_IP
, dir
);
6009 b1
= gen_proto(v
, Q_IPV6
, dir
);
6015 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6016 * not LLC encapsulation with LLCSAP_IP.
6018 * For IEEE 802 networks - which includes 802.5 token ring
6019 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6020 * says that SNAP encapsulation is used, not LLC encapsulation
6023 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6024 * RFC 2225 say that SNAP encapsulation is used, not LLC
6025 * encapsulation with LLCSAP_IP.
6027 * So we always check for ETHERTYPE_IP.
6029 b0
= gen_linktype(ETHERTYPE_IP
);
6031 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6033 b1
= gen_protochain(v
, Q_IP
);
6043 * Frame Relay packets typically have an OSI
6044 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6045 * generates code to check for all the OSI
6046 * NLPIDs, so calling it and then adding a check
6047 * for the particular NLPID for which we're
6048 * looking is bogus, as we can just check for
6051 * What we check for is the NLPID and a frame
6052 * control field value of UI, i.e. 0x03 followed
6055 * XXX - assumes a 2-byte Frame Relay header with
6056 * DLCI and flags. What if the address is longer?
6058 * XXX - what about SNAP-encapsulated frames?
6060 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6066 * Cisco uses an Ethertype lookalike - for OSI,
6069 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6070 /* OSI in C-HDLC is stuffed with a fudge byte */
6071 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6076 b0
= gen_linktype(LLCSAP_ISONS
);
6077 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6083 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6085 * 4 is the offset of the PDU type relative to the IS-IS
6088 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6093 bpf_error("arp does not encapsulate another protocol");
6097 bpf_error("rarp does not encapsulate another protocol");
6101 bpf_error("atalk encapsulation is not specifiable");
6105 bpf_error("decnet encapsulation is not specifiable");
6109 bpf_error("sca does not encapsulate another protocol");
6113 bpf_error("lat does not encapsulate another protocol");
6117 bpf_error("moprc does not encapsulate another protocol");
6121 bpf_error("mopdl does not encapsulate another protocol");
6125 return gen_linktype(v
);
6128 bpf_error("'udp proto' is bogus");
6132 bpf_error("'tcp proto' is bogus");
6136 bpf_error("'sctp proto' is bogus");
6140 bpf_error("'icmp proto' is bogus");
6144 bpf_error("'igmp proto' is bogus");
6148 bpf_error("'igrp proto' is bogus");
6152 bpf_error("'pim proto' is bogus");
6156 bpf_error("'vrrp proto' is bogus");
6160 bpf_error("'carp proto' is bogus");
6164 b0
= gen_linktype(ETHERTYPE_IPV6
);
6167 * Also check for a fragment header before the final
6170 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6171 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
6173 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6176 b1
= gen_protochain(v
, Q_IPV6
);
6182 bpf_error("'icmp6 proto' is bogus");
6185 bpf_error("'ah proto' is bogus");
6188 bpf_error("'ah proto' is bogus");
6191 bpf_error("'stp proto' is bogus");
6194 bpf_error("'ipx proto' is bogus");
6197 bpf_error("'netbeui proto' is bogus");
6200 bpf_error("'radio proto' is bogus");
6211 register const char *name
;
6214 int proto
= q
.proto
;
6218 bpf_u_int32 mask
, addr
;
6220 bpf_u_int32
**alist
;
6223 struct sockaddr_in
*sin4
;
6224 struct sockaddr_in6
*sin6
;
6225 struct addrinfo
*res
, *res0
;
6226 struct in6_addr mask128
;
6228 struct block
*b
, *tmp
;
6229 int port
, real_proto
;
6235 addr
= pcap_nametonetaddr(name
);
6237 bpf_error("unknown network '%s'", name
);
6238 /* Left justify network addr and calculate its network mask */
6240 while (addr
&& (addr
& 0xff000000) == 0) {
6244 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6248 if (proto
== Q_LINK
) {
6252 case DLT_NETANALYZER
:
6253 case DLT_NETANALYZER_TRANSPARENT
:
6254 eaddr
= pcap_ether_hostton(name
);
6257 "unknown ether host '%s'", name
);
6258 b
= gen_ehostop(eaddr
, dir
);
6263 eaddr
= pcap_ether_hostton(name
);
6266 "unknown FDDI host '%s'", name
);
6267 b
= gen_fhostop(eaddr
, dir
);
6272 eaddr
= pcap_ether_hostton(name
);
6275 "unknown token ring host '%s'", name
);
6276 b
= gen_thostop(eaddr
, dir
);
6280 case DLT_IEEE802_11
:
6281 case DLT_PRISM_HEADER
:
6282 case DLT_IEEE802_11_RADIO_AVS
:
6283 case DLT_IEEE802_11_RADIO
:
6285 eaddr
= pcap_ether_hostton(name
);
6288 "unknown 802.11 host '%s'", name
);
6289 b
= gen_wlanhostop(eaddr
, dir
);
6293 case DLT_IP_OVER_FC
:
6294 eaddr
= pcap_ether_hostton(name
);
6297 "unknown Fibre Channel host '%s'", name
);
6298 b
= gen_ipfchostop(eaddr
, dir
);
6307 * Check that the packet doesn't begin
6308 * with an LE Control marker. (We've
6309 * already generated a test for LANE.)
6311 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6315 eaddr
= pcap_ether_hostton(name
);
6318 "unknown ether host '%s'", name
);
6319 b
= gen_ehostop(eaddr
, dir
);
6325 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6326 } else if (proto
== Q_DECNET
) {
6327 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6329 * I don't think DECNET hosts can be multihomed, so
6330 * there is no need to build up a list of addresses
6332 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6335 alist
= pcap_nametoaddr(name
);
6336 if (alist
== NULL
|| *alist
== NULL
)
6337 bpf_error("unknown host '%s'", name
);
6339 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6341 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6343 tmp
= gen_host(**alist
++, 0xffffffff,
6344 tproto
, dir
, q
.addr
);
6350 memset(&mask128
, 0xff, sizeof(mask128
));
6351 res0
= res
= pcap_nametoaddrinfo(name
);
6353 bpf_error("unknown host '%s'", name
);
6356 tproto
= tproto6
= proto
;
6357 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6361 for (res
= res0
; res
; res
= res
->ai_next
) {
6362 switch (res
->ai_family
) {
6364 if (tproto
== Q_IPV6
)
6367 sin4
= (struct sockaddr_in
*)
6369 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6370 0xffffffff, tproto
, dir
, q
.addr
);
6373 if (tproto6
== Q_IP
)
6376 sin6
= (struct sockaddr_in6
*)
6378 tmp
= gen_host6(&sin6
->sin6_addr
,
6379 &mask128
, tproto6
, dir
, q
.addr
);
6391 bpf_error("unknown host '%s'%s", name
,
6392 (proto
== Q_DEFAULT
)
6394 : " for specified address family");
6401 if (proto
!= Q_DEFAULT
&&
6402 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6403 bpf_error("illegal qualifier of 'port'");
6404 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6405 bpf_error("unknown port '%s'", name
);
6406 if (proto
== Q_UDP
) {
6407 if (real_proto
== IPPROTO_TCP
)
6408 bpf_error("port '%s' is tcp", name
);
6409 else if (real_proto
== IPPROTO_SCTP
)
6410 bpf_error("port '%s' is sctp", name
);
6412 /* override PROTO_UNDEF */
6413 real_proto
= IPPROTO_UDP
;
6415 if (proto
== Q_TCP
) {
6416 if (real_proto
== IPPROTO_UDP
)
6417 bpf_error("port '%s' is udp", name
);
6419 else if (real_proto
== IPPROTO_SCTP
)
6420 bpf_error("port '%s' is sctp", name
);
6422 /* override PROTO_UNDEF */
6423 real_proto
= IPPROTO_TCP
;
6425 if (proto
== Q_SCTP
) {
6426 if (real_proto
== IPPROTO_UDP
)
6427 bpf_error("port '%s' is udp", name
);
6429 else if (real_proto
== IPPROTO_TCP
)
6430 bpf_error("port '%s' is tcp", name
);
6432 /* override PROTO_UNDEF */
6433 real_proto
= IPPROTO_SCTP
;
6436 bpf_error("illegal port number %d < 0", port
);
6438 bpf_error("illegal port number %d > 65535", port
);
6439 b
= gen_port(port
, real_proto
, dir
);
6440 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6444 if (proto
!= Q_DEFAULT
&&
6445 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6446 bpf_error("illegal qualifier of 'portrange'");
6447 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6448 bpf_error("unknown port in range '%s'", name
);
6449 if (proto
== Q_UDP
) {
6450 if (real_proto
== IPPROTO_TCP
)
6451 bpf_error("port in range '%s' is tcp", name
);
6452 else if (real_proto
== IPPROTO_SCTP
)
6453 bpf_error("port in range '%s' is sctp", name
);
6455 /* override PROTO_UNDEF */
6456 real_proto
= IPPROTO_UDP
;
6458 if (proto
== Q_TCP
) {
6459 if (real_proto
== IPPROTO_UDP
)
6460 bpf_error("port in range '%s' is udp", name
);
6461 else if (real_proto
== IPPROTO_SCTP
)
6462 bpf_error("port in range '%s' is sctp", name
);
6464 /* override PROTO_UNDEF */
6465 real_proto
= IPPROTO_TCP
;
6467 if (proto
== Q_SCTP
) {
6468 if (real_proto
== IPPROTO_UDP
)
6469 bpf_error("port in range '%s' is udp", name
);
6470 else if (real_proto
== IPPROTO_TCP
)
6471 bpf_error("port in range '%s' is tcp", name
);
6473 /* override PROTO_UNDEF */
6474 real_proto
= IPPROTO_SCTP
;
6477 bpf_error("illegal port number %d < 0", port1
);
6479 bpf_error("illegal port number %d > 65535", port1
);
6481 bpf_error("illegal port number %d < 0", port2
);
6483 bpf_error("illegal port number %d > 65535", port2
);
6485 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6486 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6491 eaddr
= pcap_ether_hostton(name
);
6493 bpf_error("unknown ether host: %s", name
);
6495 alist
= pcap_nametoaddr(name
);
6496 if (alist
== NULL
|| *alist
== NULL
)
6497 bpf_error("unknown host '%s'", name
);
6498 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6502 bpf_error("'gateway' not supported in this configuration");
6506 real_proto
= lookup_proto(name
, proto
);
6507 if (real_proto
>= 0)
6508 return gen_proto(real_proto
, proto
, dir
);
6510 bpf_error("unknown protocol: %s", name
);
6513 real_proto
= lookup_proto(name
, proto
);
6514 if (real_proto
>= 0)
6515 return gen_protochain(real_proto
, proto
, dir
);
6517 bpf_error("unknown protocol: %s", name
);
6528 gen_mcode(s1
, s2
, masklen
, q
)
6529 register const char *s1
, *s2
;
6530 register unsigned int masklen
;
6533 register int nlen
, mlen
;
6536 nlen
= __pcap_atoin(s1
, &n
);
6537 /* Promote short ipaddr */
6541 mlen
= __pcap_atoin(s2
, &m
);
6542 /* Promote short ipaddr */
6545 bpf_error("non-network bits set in \"%s mask %s\"",
6548 /* Convert mask len to mask */
6550 bpf_error("mask length must be <= 32");
6553 * X << 32 is not guaranteed by C to be 0; it's
6558 m
= 0xffffffff << (32 - masklen
);
6560 bpf_error("non-network bits set in \"%s/%d\"",
6567 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6570 bpf_error("Mask syntax for networks only");
6579 register const char *s
;
6584 int proto
= q
.proto
;
6590 else if (q
.proto
== Q_DECNET
)
6591 vlen
= __pcap_atodn(s
, &v
);
6593 vlen
= __pcap_atoin(s
, &v
);
6600 if (proto
== Q_DECNET
)
6601 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6602 else if (proto
== Q_LINK
) {
6603 bpf_error("illegal link layer address");
6606 if (s
== NULL
&& q
.addr
== Q_NET
) {
6607 /* Promote short net number */
6608 while (v
&& (v
& 0xff000000) == 0) {
6613 /* Promote short ipaddr */
6617 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6622 proto
= IPPROTO_UDP
;
6623 else if (proto
== Q_TCP
)
6624 proto
= IPPROTO_TCP
;
6625 else if (proto
== Q_SCTP
)
6626 proto
= IPPROTO_SCTP
;
6627 else if (proto
== Q_DEFAULT
)
6628 proto
= PROTO_UNDEF
;
6630 bpf_error("illegal qualifier of 'port'");
6633 bpf_error("illegal port number %u > 65535", v
);
6637 b
= gen_port((int)v
, proto
, dir
);
6638 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6644 proto
= IPPROTO_UDP
;
6645 else if (proto
== Q_TCP
)
6646 proto
= IPPROTO_TCP
;
6647 else if (proto
== Q_SCTP
)
6648 proto
= IPPROTO_SCTP
;
6649 else if (proto
== Q_DEFAULT
)
6650 proto
= PROTO_UNDEF
;
6652 bpf_error("illegal qualifier of 'portrange'");
6655 bpf_error("illegal port number %u > 65535", v
);
6659 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6660 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6665 bpf_error("'gateway' requires a name");
6669 return gen_proto((int)v
, proto
, dir
);
6672 return gen_protochain((int)v
, proto
, dir
);
6687 gen_mcode6(s1
, s2
, masklen
, q
)
6688 register const char *s1
, *s2
;
6689 register unsigned int masklen
;
6692 struct addrinfo
*res
;
6693 struct in6_addr
*addr
;
6694 struct in6_addr mask
;
6699 bpf_error("no mask %s supported", s2
);
6701 res
= pcap_nametoaddrinfo(s1
);
6703 bpf_error("invalid ip6 address %s", s1
);
6706 bpf_error("%s resolved to multiple address", s1
);
6707 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6709 if (sizeof(mask
) * 8 < masklen
)
6710 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6711 memset(&mask
, 0, sizeof(mask
));
6712 memset(&mask
, 0xff, masklen
/ 8);
6714 mask
.s6_addr
[masklen
/ 8] =
6715 (0xff << (8 - masklen
% 8)) & 0xff;
6718 a
= (u_int32_t
*)addr
;
6719 m
= (u_int32_t
*)&mask
;
6720 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6721 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6722 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6730 bpf_error("Mask syntax for networks only");
6734 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6740 bpf_error("invalid qualifier against IPv6 address");
6749 register const u_char
*eaddr
;
6752 struct block
*b
, *tmp
;
6754 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6757 case DLT_NETANALYZER
:
6758 case DLT_NETANALYZER_TRANSPARENT
:
6759 return gen_ehostop(eaddr
, (int)q
.dir
);
6761 return gen_fhostop(eaddr
, (int)q
.dir
);
6763 return gen_thostop(eaddr
, (int)q
.dir
);
6764 case DLT_IEEE802_11
:
6765 case DLT_PRISM_HEADER
:
6766 case DLT_IEEE802_11_RADIO_AVS
:
6767 case DLT_IEEE802_11_RADIO
:
6769 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6773 * Check that the packet doesn't begin with an
6774 * LE Control marker. (We've already generated
6777 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6782 * Now check the MAC address.
6784 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6789 case DLT_IP_OVER_FC
:
6790 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6792 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6796 bpf_error("ethernet address used in non-ether expression");
6803 struct slist
*s0
, *s1
;
6806 * This is definitely not the best way to do this, but the
6807 * lists will rarely get long.
6814 static struct slist
*
6820 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6825 static struct slist
*
6831 s
= new_stmt(BPF_LD
|BPF_MEM
);
6837 * Modify "index" to use the value stored into its register as an
6838 * offset relative to the beginning of the header for the protocol
6839 * "proto", and allocate a register and put an item "size" bytes long
6840 * (1, 2, or 4) at that offset into that register, making it the register
6844 gen_load(proto
, inst
, size
)
6849 struct slist
*s
, *tmp
;
6851 int regno
= alloc_reg();
6853 free_reg(inst
->regno
);
6857 bpf_error("data size must be 1, 2, or 4");
6873 bpf_error("unsupported index operation");
6877 * The offset is relative to the beginning of the packet
6878 * data, if we have a radio header. (If we don't, this
6881 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6882 linktype
!= DLT_IEEE802_11_RADIO
&&
6883 linktype
!= DLT_PRISM_HEADER
)
6884 bpf_error("radio information not present in capture");
6887 * Load into the X register the offset computed into the
6888 * register specified by "index".
6890 s
= xfer_to_x(inst
);
6893 * Load the item at that offset.
6895 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6897 sappend(inst
->s
, s
);
6902 * The offset is relative to the beginning of
6903 * the link-layer header.
6905 * XXX - what about ATM LANE? Should the index be
6906 * relative to the beginning of the AAL5 frame, so
6907 * that 0 refers to the beginning of the LE Control
6908 * field, or relative to the beginning of the LAN
6909 * frame, so that 0 refers, for Ethernet LANE, to
6910 * the beginning of the destination address?
6912 s
= gen_llprefixlen();
6915 * If "s" is non-null, it has code to arrange that the
6916 * X register contains the length of the prefix preceding
6917 * the link-layer header. Add to it the offset computed
6918 * into the register specified by "index", and move that
6919 * into the X register. Otherwise, just load into the X
6920 * register the offset computed into the register specified
6924 sappend(s
, xfer_to_a(inst
));
6925 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6926 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6928 s
= xfer_to_x(inst
);
6931 * Load the item at the sum of the offset we've put in the
6932 * X register and the offset of the start of the link
6933 * layer header (which is 0 if the radio header is
6934 * variable-length; that header length is what we put
6935 * into the X register and then added to the index).
6937 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6940 sappend(inst
->s
, s
);
6954 * The offset is relative to the beginning of
6955 * the network-layer header.
6956 * XXX - are there any cases where we want
6959 s
= gen_off_macpl();
6962 * If "s" is non-null, it has code to arrange that the
6963 * X register contains the offset of the MAC-layer
6964 * payload. Add to it the offset computed into the
6965 * register specified by "index", and move that into
6966 * the X register. Otherwise, just load into the X
6967 * register the offset computed into the register specified
6971 sappend(s
, xfer_to_a(inst
));
6972 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6973 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6975 s
= xfer_to_x(inst
);
6978 * Load the item at the sum of the offset we've put in the
6979 * X register, the offset of the start of the network
6980 * layer header from the beginning of the MAC-layer
6981 * payload, and the purported offset of the start of the
6982 * MAC-layer payload (which might be 0 if there's a
6983 * variable-length prefix before the link-layer header
6984 * or the link-layer header itself is variable-length;
6985 * the variable-length offset of the start of the
6986 * MAC-layer payload is what we put into the X register
6987 * and then added to the index).
6989 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6990 tmp
->s
.k
= off_macpl
+ off_nl
;
6992 sappend(inst
->s
, s
);
6995 * Do the computation only if the packet contains
6996 * the protocol in question.
6998 b
= gen_proto_abbrev(proto
);
7000 gen_and(inst
->b
, b
);
7014 * The offset is relative to the beginning of
7015 * the transport-layer header.
7017 * Load the X register with the length of the IPv4 header
7018 * (plus the offset of the link-layer header, if it's
7019 * a variable-length header), in bytes.
7021 * XXX - are there any cases where we want
7023 * XXX - we should, if we're built with
7024 * IPv6 support, generate code to load either
7025 * IPv4, IPv6, or both, as appropriate.
7027 s
= gen_loadx_iphdrlen();
7030 * The X register now contains the sum of the length
7031 * of any variable-length header preceding the link-layer
7032 * header, any variable-length link-layer header, and the
7033 * length of the network-layer header.
7035 * Load into the A register the offset relative to
7036 * the beginning of the transport layer header,
7037 * add the X register to that, move that to the
7038 * X register, and load with an offset from the
7039 * X register equal to the offset of the network
7040 * layer header relative to the beginning of
7041 * the MAC-layer payload plus the fixed-length
7042 * portion of the offset of the MAC-layer payload
7043 * from the beginning of the raw packet data.
7045 sappend(s
, xfer_to_a(inst
));
7046 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7047 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7048 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7049 tmp
->s
.k
= off_macpl
+ off_nl
;
7050 sappend(inst
->s
, s
);
7053 * Do the computation only if the packet contains
7054 * the protocol in question - which is true only
7055 * if this is an IP datagram and is the first or
7056 * only fragment of that datagram.
7058 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7060 gen_and(inst
->b
, b
);
7061 gen_and(gen_proto_abbrev(Q_IP
), b
);
7065 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7068 inst
->regno
= regno
;
7069 s
= new_stmt(BPF_ST
);
7071 sappend(inst
->s
, s
);
7077 gen_relation(code
, a0
, a1
, reversed
)
7079 struct arth
*a0
, *a1
;
7082 struct slist
*s0
, *s1
, *s2
;
7083 struct block
*b
, *tmp
;
7087 if (code
== BPF_JEQ
) {
7088 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7089 b
= new_block(JMP(code
));
7093 b
= new_block(BPF_JMP
|code
|BPF_X
);
7099 sappend(a0
->s
, a1
->s
);
7103 free_reg(a0
->regno
);
7104 free_reg(a1
->regno
);
7106 /* 'and' together protocol checks */
7109 gen_and(a0
->b
, tmp
= a1
->b
);
7125 int regno
= alloc_reg();
7126 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7129 s
= new_stmt(BPF_LD
|BPF_LEN
);
7130 s
->next
= new_stmt(BPF_ST
);
7131 s
->next
->s
.k
= regno
;
7146 a
= (struct arth
*)newchunk(sizeof(*a
));
7150 s
= new_stmt(BPF_LD
|BPF_IMM
);
7152 s
->next
= new_stmt(BPF_ST
);
7168 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7171 s
= new_stmt(BPF_ST
);
7179 gen_arth(code
, a0
, a1
)
7181 struct arth
*a0
, *a1
;
7183 struct slist
*s0
, *s1
, *s2
;
7187 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7192 sappend(a0
->s
, a1
->s
);
7194 free_reg(a0
->regno
);
7195 free_reg(a1
->regno
);
7197 s0
= new_stmt(BPF_ST
);
7198 a0
->regno
= s0
->s
.k
= alloc_reg();
7205 * Here we handle simple allocation of the scratch registers.
7206 * If too many registers are alloc'd, the allocator punts.
7208 static int regused
[BPF_MEMWORDS
];
7212 * Initialize the table of used registers and the current register.
7218 memset(regused
, 0, sizeof regused
);
7222 * Return the next free register.
7227 int n
= BPF_MEMWORDS
;
7230 if (regused
[curreg
])
7231 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7233 regused
[curreg
] = 1;
7237 bpf_error("too many registers needed to evaluate expression");
7243 * Return a register to the table so it can
7253 static struct block
*
7260 s
= new_stmt(BPF_LD
|BPF_LEN
);
7261 b
= new_block(JMP(jmp
));
7272 return gen_len(BPF_JGE
, n
);
7276 * Actually, this is less than or equal.
7284 b
= gen_len(BPF_JGT
, n
);
7291 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7292 * the beginning of the link-layer header.
7293 * XXX - that means you can't test values in the radiotap header, but
7294 * as that header is difficult if not impossible to parse generally
7295 * without a loop, that might not be a severe problem. A new keyword
7296 * "radio" could be added for that, although what you'd really want
7297 * would be a way of testing particular radio header values, which
7298 * would generate code appropriate to the radio header in question.
7301 gen_byteop(op
, idx
, val
)
7312 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7315 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7319 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7323 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7327 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7331 b
= new_block(JMP(BPF_JEQ
));
7338 static u_char abroadcast
[] = { 0x0 };
7341 gen_broadcast(proto
)
7344 bpf_u_int32 hostmask
;
7345 struct block
*b0
, *b1
, *b2
;
7346 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7354 case DLT_ARCNET_LINUX
:
7355 return gen_ahostop(abroadcast
, Q_DST
);
7357 case DLT_NETANALYZER
:
7358 case DLT_NETANALYZER_TRANSPARENT
:
7359 return gen_ehostop(ebroadcast
, Q_DST
);
7361 return gen_fhostop(ebroadcast
, Q_DST
);
7363 return gen_thostop(ebroadcast
, Q_DST
);
7364 case DLT_IEEE802_11
:
7365 case DLT_PRISM_HEADER
:
7366 case DLT_IEEE802_11_RADIO_AVS
:
7367 case DLT_IEEE802_11_RADIO
:
7369 return gen_wlanhostop(ebroadcast
, Q_DST
);
7370 case DLT_IP_OVER_FC
:
7371 return gen_ipfchostop(ebroadcast
, Q_DST
);
7375 * Check that the packet doesn't begin with an
7376 * LE Control marker. (We've already generated
7379 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7384 * Now check the MAC address.
7386 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7392 bpf_error("not a broadcast link");
7398 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7399 * as an indication that we don't know the netmask, and fail
7402 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7403 bpf_error("netmask not known, so 'ip broadcast' not supported");
7404 b0
= gen_linktype(ETHERTYPE_IP
);
7405 hostmask
= ~netmask
;
7406 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7407 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7408 (bpf_int32
)(~0 & hostmask
), hostmask
);
7413 bpf_error("only link-layer/IP broadcast filters supported");
7419 * Generate code to test the low-order bit of a MAC address (that's
7420 * the bottom bit of the *first* byte).
7422 static struct block
*
7423 gen_mac_multicast(offset
)
7426 register struct block
*b0
;
7427 register struct slist
*s
;
7429 /* link[offset] & 1 != 0 */
7430 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7431 b0
= new_block(JMP(BPF_JSET
));
7438 gen_multicast(proto
)
7441 register struct block
*b0
, *b1
, *b2
;
7442 register struct slist
*s
;
7450 case DLT_ARCNET_LINUX
:
7451 /* all ARCnet multicasts use the same address */
7452 return gen_ahostop(abroadcast
, Q_DST
);
7454 case DLT_NETANALYZER
:
7455 case DLT_NETANALYZER_TRANSPARENT
:
7456 /* ether[0] & 1 != 0 */
7457 return gen_mac_multicast(0);
7460 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7462 * XXX - was that referring to bit-order issues?
7464 /* fddi[1] & 1 != 0 */
7465 return gen_mac_multicast(1);
7467 /* tr[2] & 1 != 0 */
7468 return gen_mac_multicast(2);
7469 case DLT_IEEE802_11
:
7470 case DLT_PRISM_HEADER
:
7471 case DLT_IEEE802_11_RADIO_AVS
:
7472 case DLT_IEEE802_11_RADIO
:
7477 * For control frames, there is no DA.
7479 * For management frames, DA is at an
7480 * offset of 4 from the beginning of
7483 * For data frames, DA is at an offset
7484 * of 4 from the beginning of the packet
7485 * if To DS is clear and at an offset of
7486 * 16 from the beginning of the packet
7491 * Generate the tests to be done for data frames.
7493 * First, check for To DS set, i.e. "link[1] & 0x01".
7495 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7496 b1
= new_block(JMP(BPF_JSET
));
7497 b1
->s
.k
= 0x01; /* To DS */
7501 * If To DS is set, the DA is at 16.
7503 b0
= gen_mac_multicast(16);
7507 * Now, check for To DS not set, i.e. check
7508 * "!(link[1] & 0x01)".
7510 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7511 b2
= new_block(JMP(BPF_JSET
));
7512 b2
->s
.k
= 0x01; /* To DS */
7517 * If To DS is not set, the DA is at 4.
7519 b1
= gen_mac_multicast(4);
7523 * Now OR together the last two checks. That gives
7524 * the complete set of checks for data frames.
7529 * Now check for a data frame.
7530 * I.e, check "link[0] & 0x08".
7532 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7533 b1
= new_block(JMP(BPF_JSET
));
7538 * AND that with the checks done for data frames.
7543 * If the high-order bit of the type value is 0, this
7544 * is a management frame.
7545 * I.e, check "!(link[0] & 0x08)".
7547 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7548 b2
= new_block(JMP(BPF_JSET
));
7554 * For management frames, the DA is at 4.
7556 b1
= gen_mac_multicast(4);
7560 * OR that with the checks done for data frames.
7561 * That gives the checks done for management and
7567 * If the low-order bit of the type value is 1,
7568 * this is either a control frame or a frame
7569 * with a reserved type, and thus not a
7572 * I.e., check "!(link[0] & 0x04)".
7574 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7575 b1
= new_block(JMP(BPF_JSET
));
7581 * AND that with the checks for data and management
7586 case DLT_IP_OVER_FC
:
7587 b0
= gen_mac_multicast(2);
7592 * Check that the packet doesn't begin with an
7593 * LE Control marker. (We've already generated
7596 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7600 /* ether[off_mac] & 1 != 0 */
7601 b0
= gen_mac_multicast(off_mac
);
7609 /* Link not known to support multicasts */
7613 b0
= gen_linktype(ETHERTYPE_IP
);
7614 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7619 b0
= gen_linktype(ETHERTYPE_IPV6
);
7620 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7624 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7630 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7631 * Outbound traffic is sent by this machine, while inbound traffic is
7632 * sent by a remote machine (and may include packets destined for a
7633 * unicast or multicast link-layer address we are not subscribing to).
7634 * These are the same definitions implemented by pcap_setdirection().
7635 * Capturing only unicast traffic destined for this host is probably
7636 * better accomplished using a higher-layer filter.
7642 register struct block
*b0
;
7645 * Only some data link types support inbound/outbound qualifiers.
7649 b0
= gen_relation(BPF_JEQ
,
7650 gen_load(Q_LINK
, gen_loadi(0), 1),
7657 /* match outgoing packets */
7658 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7660 /* match incoming packets */
7661 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7666 /* match outgoing packets */
7667 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7669 /* to filter on inbound traffic, invert the match */
7674 #ifdef HAVE_NET_PFVAR_H
7676 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7677 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7683 /* match outgoing packets */
7684 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7686 /* match incoming packets */
7687 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7691 case DLT_JUNIPER_MFR
:
7692 case DLT_JUNIPER_MLFR
:
7693 case DLT_JUNIPER_MLPPP
:
7694 case DLT_JUNIPER_ATM1
:
7695 case DLT_JUNIPER_ATM2
:
7696 case DLT_JUNIPER_PPPOE
:
7697 case DLT_JUNIPER_PPPOE_ATM
:
7698 case DLT_JUNIPER_GGSN
:
7699 case DLT_JUNIPER_ES
:
7700 case DLT_JUNIPER_MONITOR
:
7701 case DLT_JUNIPER_SERVICES
:
7702 case DLT_JUNIPER_ETHER
:
7703 case DLT_JUNIPER_PPP
:
7704 case DLT_JUNIPER_FRELAY
:
7705 case DLT_JUNIPER_CHDLC
:
7706 case DLT_JUNIPER_VP
:
7707 case DLT_JUNIPER_ST
:
7708 case DLT_JUNIPER_ISM
:
7709 case DLT_JUNIPER_VS
:
7710 case DLT_JUNIPER_SRX_E2E
:
7711 case DLT_JUNIPER_FIBRECHANNEL
:
7712 case DLT_JUNIPER_ATM_CEMIC
:
7714 /* juniper flags (including direction) are stored
7715 * the byte after the 3-byte magic number */
7717 /* match outgoing packets */
7718 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7720 /* match incoming packets */
7721 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7727 * If we have packet meta-data indicating a direction,
7728 * check it, otherwise give up as this link-layer type
7729 * has nothing in the packet data.
7731 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7733 * This is Linux with PF_PACKET support.
7734 * If this is a *live* capture, we can look at
7735 * special meta-data in the filter expression;
7736 * if it's a savefile, we can't.
7738 if (bpf_pcap
->rfile
!= NULL
) {
7739 /* We have a FILE *, so this is a savefile */
7740 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7745 /* match outgoing packets */
7746 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7749 /* to filter on inbound traffic, invert the match */
7752 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7753 bpf_error("inbound/outbound not supported on linktype %d",
7757 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7762 #ifdef HAVE_NET_PFVAR_H
7763 /* PF firewall log matched interface */
7765 gen_pf_ifname(const char *ifname
)
7770 if (linktype
!= DLT_PFLOG
) {
7771 bpf_error("ifname supported only on PF linktype");
7774 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7775 off
= offsetof(struct pfloghdr
, ifname
);
7776 if (strlen(ifname
) >= len
) {
7777 bpf_error("ifname interface names can only be %d characters",
7781 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7785 /* PF firewall log ruleset name */
7787 gen_pf_ruleset(char *ruleset
)
7791 if (linktype
!= DLT_PFLOG
) {
7792 bpf_error("ruleset supported only on PF linktype");
7796 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7797 bpf_error("ruleset names can only be %ld characters",
7798 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7802 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7803 strlen(ruleset
), (const u_char
*)ruleset
);
7807 /* PF firewall log rule number */
7813 if (linktype
!= DLT_PFLOG
) {
7814 bpf_error("rnr supported only on PF linktype");
7818 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7823 /* PF firewall log sub-rule number */
7825 gen_pf_srnr(int srnr
)
7829 if (linktype
!= DLT_PFLOG
) {
7830 bpf_error("srnr supported only on PF linktype");
7834 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7839 /* PF firewall log reason code */
7841 gen_pf_reason(int reason
)
7845 if (linktype
!= DLT_PFLOG
) {
7846 bpf_error("reason supported only on PF linktype");
7850 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7855 /* PF firewall log action */
7857 gen_pf_action(int action
)
7861 if (linktype
!= DLT_PFLOG
) {
7862 bpf_error("action supported only on PF linktype");
7866 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7870 #else /* !HAVE_NET_PFVAR_H */
7872 gen_pf_ifname(const char *ifname
)
7874 bpf_error("libpcap was compiled without pf support");
7880 gen_pf_ruleset(char *ruleset
)
7882 bpf_error("libpcap was compiled on a machine without pf support");
7890 bpf_error("libpcap was compiled on a machine without pf support");
7896 gen_pf_srnr(int srnr
)
7898 bpf_error("libpcap was compiled on a machine without pf support");
7904 gen_pf_reason(int reason
)
7906 bpf_error("libpcap was compiled on a machine without pf support");
7912 gen_pf_action(int action
)
7914 bpf_error("libpcap was compiled on a machine without pf support");
7918 #endif /* HAVE_NET_PFVAR_H */
7920 /* IEEE 802.11 wireless header */
7922 gen_p80211_type(int type
, int mask
)
7928 case DLT_IEEE802_11
:
7929 case DLT_PRISM_HEADER
:
7930 case DLT_IEEE802_11_RADIO_AVS
:
7931 case DLT_IEEE802_11_RADIO
:
7932 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7937 bpf_error("802.11 link-layer types supported only on 802.11");
7945 gen_p80211_fcdir(int fcdir
)
7951 case DLT_IEEE802_11
:
7952 case DLT_PRISM_HEADER
:
7953 case DLT_IEEE802_11_RADIO_AVS
:
7954 case DLT_IEEE802_11_RADIO
:
7958 bpf_error("frame direction supported only with 802.11 headers");
7962 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7963 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7970 register const u_char
*eaddr
;
7976 case DLT_ARCNET_LINUX
:
7977 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7979 return (gen_ahostop(eaddr
, (int)q
.dir
));
7981 bpf_error("ARCnet address used in non-arc expression");
7987 bpf_error("aid supported only on ARCnet");
7990 bpf_error("ARCnet address used in non-arc expression");
7995 static struct block
*
7996 gen_ahostop(eaddr
, dir
)
7997 register const u_char
*eaddr
;
8000 register struct block
*b0
, *b1
;
8003 /* src comes first, different from Ethernet */
8005 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
8008 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
8011 b0
= gen_ahostop(eaddr
, Q_SRC
);
8012 b1
= gen_ahostop(eaddr
, Q_DST
);
8018 b0
= gen_ahostop(eaddr
, Q_SRC
);
8019 b1
= gen_ahostop(eaddr
, Q_DST
);
8024 bpf_error("'addr1' is only supported on 802.11");
8028 bpf_error("'addr2' is only supported on 802.11");
8032 bpf_error("'addr3' is only supported on 802.11");
8036 bpf_error("'addr4' is only supported on 802.11");
8040 bpf_error("'ra' is only supported on 802.11");
8044 bpf_error("'ta' is only supported on 802.11");
8051 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8052 static struct block
*
8053 gen_vlan_bpf_extensions(int vlan_num
)
8055 struct block
*b0
, *b1
;
8058 /* generate new filter code based on extracting packet
8060 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8061 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8063 b0
= new_block(JMP(BPF_JEQ
));
8067 if (vlan_num
>= 0) {
8068 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8069 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8071 b1
= new_block(JMP(BPF_JEQ
));
8073 b1
->s
.k
= (bpf_int32
) vlan_num
;
8083 static struct block
*
8084 gen_vlan_no_bpf_extensions(int vlan_num
)
8086 struct block
*b0
, *b1
;
8088 /* check for VLAN, including QinQ */
8089 b0
= gen_linktype(ETHERTYPE_8021Q
);
8090 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
8094 /* If a specific VLAN is requested, check VLAN id */
8095 if (vlan_num
>= 0) {
8096 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
8097 (bpf_int32
)vlan_num
, 0x0fff);
8103 * The payload follows the full header, including the
8104 * VLAN tags, so skip past this VLAN tag.
8109 * The link-layer type information follows the VLAN tags, so
8110 * skip past this VLAN tag.
8118 * support IEEE 802.1Q VLAN trunk over ethernet
8126 /* can't check for VLAN-encapsulated packets inside MPLS */
8127 if (label_stack_depth
> 0)
8128 bpf_error("no VLAN match after MPLS");
8131 * Check for a VLAN packet, and then change the offsets to point
8132 * to the type and data fields within the VLAN packet. Just
8133 * increment the offsets, so that we can support a hierarchy, e.g.
8134 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8137 * XXX - this is a bit of a kludge. If we were to split the
8138 * compiler into a parser that parses an expression and
8139 * generates an expression tree, and a code generator that
8140 * takes an expression tree (which could come from our
8141 * parser or from some other parser) and generates BPF code,
8142 * we could perhaps make the offsets parameters of routines
8143 * and, in the handler for an "AND" node, pass to subnodes
8144 * other than the VLAN node the adjusted offsets.
8146 * This would mean that "vlan" would, instead of changing the
8147 * behavior of *all* tests after it, change only the behavior
8148 * of tests ANDed with it. That would change the documented
8149 * semantics of "vlan", which might break some expressions.
8150 * However, it would mean that "(vlan and ip) or ip" would check
8151 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8152 * checking only for VLAN-encapsulated IP, so that could still
8153 * be considered worth doing; it wouldn't break expressions
8154 * that are of the form "vlan and ..." or "vlan N and ...",
8155 * which I suspect are the most common expressions involving
8156 * "vlan". "vlan or ..." doesn't necessarily do what the user
8157 * would really want, now, as all the "or ..." tests would
8158 * be done assuming a VLAN, even though the "or" could be viewed
8159 * as meaning "or, if this isn't a VLAN packet...".
8164 case DLT_NETANALYZER
:
8165 case DLT_NETANALYZER_TRANSPARENT
:
8166 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8167 if (vlan_stack_depth
== 0) {
8169 * Do we need special VLAN handling?
8171 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8172 b0
= gen_vlan_bpf_extensions(vlan_num
);
8174 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8177 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8180 bpf_error("no VLAN support for data link type %d",
8197 struct block
*b0
, *b1
;
8199 if (label_stack_depth
> 0) {
8200 /* just match the bottom-of-stack bit clear */
8201 b0
= gen_mcmp(OR_MACPL
, off_nl
-2, BPF_B
, 0, 0x01);
8204 * We're not in an MPLS stack yet, so check the link-layer
8205 * type against MPLS.
8209 case DLT_C_HDLC
: /* fall through */
8211 case DLT_NETANALYZER
:
8212 case DLT_NETANALYZER_TRANSPARENT
:
8213 b0
= gen_linktype(ETHERTYPE_MPLS
);
8217 b0
= gen_linktype(PPP_MPLS_UCAST
);
8220 /* FIXME add other DLT_s ...
8221 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8222 * leave it for now */
8225 bpf_error("no MPLS support for data link type %d",
8233 /* If a specific MPLS label is requested, check it */
8234 if (label_num
>= 0) {
8235 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8236 b1
= gen_mcmp(OR_MACPL
, off_nl
, BPF_W
, (bpf_int32
)label_num
,
8237 0xfffff000); /* only compare the first 20 bits */
8243 * Change the offsets to point to the type and data fields within
8244 * the MPLS packet. Just increment the offsets, so that we
8245 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8246 * capture packets with an outer label of 100000 and an inner
8249 * Increment the MPLS stack depth as well; this indicates that
8250 * we're checking MPLS-encapsulated headers, to make sure higher
8251 * level code generators don't try to match against IP-related
8252 * protocols such as Q_ARP, Q_RARP etc.
8254 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8258 label_stack_depth
++;
8263 * Support PPPOE discovery and session.
8268 /* check for PPPoE discovery */
8269 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8273 gen_pppoes(sess_num
)
8276 struct block
*b0
, *b1
;
8279 * Test against the PPPoE session link-layer type.
8281 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8283 /* If a specific session is requested, check PPPoE session id */
8284 if (sess_num
>= 0) {
8285 b1
= gen_mcmp(OR_MACPL
, off_nl
, BPF_W
,
8286 (bpf_int32
)sess_num
, 0x0000ffff);
8294 * Change the offsets to point to the type and data fields within
8295 * the PPP packet, and note that this is PPPoE rather than
8298 * XXX - this is a bit of a kludge. If we were to split the
8299 * compiler into a parser that parses an expression and
8300 * generates an expression tree, and a code generator that
8301 * takes an expression tree (which could come from our
8302 * parser or from some other parser) and generates BPF code,
8303 * we could perhaps make the offsets parameters of routines
8304 * and, in the handler for an "AND" node, pass to subnodes
8305 * other than the PPPoE node the adjusted offsets.
8307 * This would mean that "pppoes" would, instead of changing the
8308 * behavior of *all* tests after it, change only the behavior
8309 * of tests ANDed with it. That would change the documented
8310 * semantics of "pppoes", which might break some expressions.
8311 * However, it would mean that "(pppoes and ip) or ip" would check
8312 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8313 * checking only for VLAN-encapsulated IP, so that could still
8314 * be considered worth doing; it wouldn't break expressions
8315 * that are of the form "pppoes and ..." which I suspect are the
8316 * most common expressions involving "pppoes". "pppoes or ..."
8317 * doesn't necessarily do what the user would really want, now,
8318 * as all the "or ..." tests would be done assuming PPPoE, even
8319 * though the "or" could be viewed as meaning "or, if this isn't
8320 * a PPPoE packet...".
8322 * The "network-layer" protocol is PPPoE, which has a 6-byte
8323 * PPPoE header, followed by a PPP packet.
8325 * There is no HDLC encapsulation for the PPP packet (it's
8326 * encapsulated in PPPoES instead), so the link-layer type
8327 * starts at the first byte of the PPP packet. For PPPoE,
8328 * that offset is relative to the beginning of the total
8329 * link-layer payload, including any 802.2 LLC header, so
8330 * it's 6 bytes past off_nl.
8332 off_linktype
= off_nl
+ 6;
8335 * The network-layer offsets are relative to the beginning
8336 * of the MAC-layer payload; that's past the 6-byte
8337 * PPPoE header and the 2-byte PPP header.
8340 off_nl_nosnap
= 6+2;
8346 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8358 bpf_error("'vpi' supported only on raw ATM");
8359 if (off_vpi
== (u_int
)-1)
8361 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8367 bpf_error("'vci' supported only on raw ATM");
8368 if (off_vci
== (u_int
)-1)
8370 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8375 if (off_proto
== (u_int
)-1)
8376 abort(); /* XXX - this isn't on FreeBSD */
8377 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8382 if (off_payload
== (u_int
)-1)
8384 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8385 0xffffffff, jtype
, reverse
, jvalue
);
8390 bpf_error("'callref' supported only on raw ATM");
8391 if (off_proto
== (u_int
)-1)
8393 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8394 jtype
, reverse
, jvalue
);
8404 gen_atmtype_abbrev(type
)
8407 struct block
*b0
, *b1
;
8412 /* Get all packets in Meta signalling Circuit */
8414 bpf_error("'metac' supported only on raw ATM");
8415 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8416 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8421 /* Get all packets in Broadcast Circuit*/
8423 bpf_error("'bcc' supported only on raw ATM");
8424 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8425 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8430 /* Get all cells in Segment OAM F4 circuit*/
8432 bpf_error("'oam4sc' supported only on raw ATM");
8433 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8434 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8439 /* Get all cells in End-to-End OAM F4 Circuit*/
8441 bpf_error("'oam4ec' supported only on raw ATM");
8442 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8443 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8448 /* Get all packets in connection Signalling Circuit */
8450 bpf_error("'sc' supported only on raw ATM");
8451 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8452 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8457 /* Get all packets in ILMI Circuit */
8459 bpf_error("'ilmic' supported only on raw ATM");
8460 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8461 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8466 /* Get all LANE packets */
8468 bpf_error("'lane' supported only on raw ATM");
8469 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8472 * Arrange that all subsequent tests assume LANE
8473 * rather than LLC-encapsulated packets, and set
8474 * the offsets appropriately for LANE-encapsulated
8477 * "off_mac" is the offset of the Ethernet header,
8478 * which is 2 bytes past the ATM pseudo-header
8479 * (skipping the pseudo-header and 2-byte LE Client
8480 * field). The other offsets are Ethernet offsets
8481 * relative to "off_mac".
8484 off_mac
= off_payload
+ 2; /* MAC header */
8485 off_linktype
= off_mac
+ 12;
8486 off_macpl
= off_mac
+ 14; /* Ethernet */
8487 off_nl
= 0; /* Ethernet II */
8488 off_nl_nosnap
= 3; /* 802.3+802.2 */
8492 /* Get all LLC-encapsulated packets */
8494 bpf_error("'llc' supported only on raw ATM");
8495 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8506 * Filtering for MTP2 messages based on li value
8507 * FISU, length is null
8508 * LSSU, length is 1 or 2
8509 * MSU, length is 3 or more
8510 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8513 gen_mtp2type_abbrev(type
)
8516 struct block
*b0
, *b1
;
8521 if ( (linktype
!= DLT_MTP2
) &&
8522 (linktype
!= DLT_ERF
) &&
8523 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8524 bpf_error("'fisu' supported only on MTP2");
8525 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8526 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8530 if ( (linktype
!= DLT_MTP2
) &&
8531 (linktype
!= DLT_ERF
) &&
8532 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8533 bpf_error("'lssu' supported only on MTP2");
8534 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8535 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8540 if ( (linktype
!= DLT_MTP2
) &&
8541 (linktype
!= DLT_ERF
) &&
8542 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8543 bpf_error("'msu' supported only on MTP2");
8544 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8548 if ( (linktype
!= DLT_MTP2
) &&
8549 (linktype
!= DLT_ERF
) &&
8550 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8551 bpf_error("'hfisu' supported only on MTP2_HSL");
8552 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8553 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8557 if ( (linktype
!= DLT_MTP2
) &&
8558 (linktype
!= DLT_ERF
) &&
8559 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8560 bpf_error("'hlssu' supported only on MTP2_HSL");
8561 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8562 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8567 if ( (linktype
!= DLT_MTP2
) &&
8568 (linktype
!= DLT_ERF
) &&
8569 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8570 bpf_error("'hmsu' supported only on MTP2_HSL");
8571 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8581 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8588 bpf_u_int32 val1
, val2
, val3
;
8589 u_int newoff_sio
=off_sio
;
8590 u_int newoff_opc
=off_opc
;
8591 u_int newoff_dpc
=off_dpc
;
8592 u_int newoff_sls
=off_sls
;
8594 switch (mtp3field
) {
8597 newoff_sio
+= 3; /* offset for MTP2_HSL */
8601 if (off_sio
== (u_int
)-1)
8602 bpf_error("'sio' supported only on SS7");
8603 /* sio coded on 1 byte so max value 255 */
8605 bpf_error("sio value %u too big; max value = 255",
8607 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8608 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8614 if (off_opc
== (u_int
)-1)
8615 bpf_error("'opc' supported only on SS7");
8616 /* opc coded on 14 bits so max value 16383 */
8618 bpf_error("opc value %u too big; max value = 16383",
8620 /* the following instructions are made to convert jvalue
8621 * to the form used to write opc in an ss7 message*/
8622 val1
= jvalue
& 0x00003c00;
8624 val2
= jvalue
& 0x000003fc;
8626 val3
= jvalue
& 0x00000003;
8628 jvalue
= val1
+ val2
+ val3
;
8629 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8630 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8638 if (off_dpc
== (u_int
)-1)
8639 bpf_error("'dpc' supported only on SS7");
8640 /* dpc coded on 14 bits so max value 16383 */
8642 bpf_error("dpc value %u too big; max value = 16383",
8644 /* the following instructions are made to convert jvalue
8645 * to the forme used to write dpc in an ss7 message*/
8646 val1
= jvalue
& 0x000000ff;
8648 val2
= jvalue
& 0x00003f00;
8650 jvalue
= val1
+ val2
;
8651 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8652 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8658 if (off_sls
== (u_int
)-1)
8659 bpf_error("'sls' supported only on SS7");
8660 /* sls coded on 4 bits so max value 15 */
8662 bpf_error("sls value %u too big; max value = 15",
8664 /* the following instruction is made to convert jvalue
8665 * to the forme used to write sls in an ss7 message*/
8666 jvalue
= jvalue
<< 4;
8667 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8668 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8677 static struct block
*
8678 gen_msg_abbrev(type
)
8684 * Q.2931 signalling protocol messages for handling virtual circuits
8685 * establishment and teardown
8690 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8694 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8698 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8702 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8706 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8709 case A_RELEASE_DONE
:
8710 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8720 gen_atmmulti_abbrev(type
)
8723 struct block
*b0
, *b1
;
8729 bpf_error("'oam' supported only on raw ATM");
8730 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8735 bpf_error("'oamf4' supported only on raw ATM");
8737 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8738 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8740 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8746 * Get Q.2931 signalling messages for switched
8747 * virtual connection
8750 bpf_error("'connectmsg' supported only on raw ATM");
8751 b0
= gen_msg_abbrev(A_SETUP
);
8752 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8754 b0
= gen_msg_abbrev(A_CONNECT
);
8756 b0
= gen_msg_abbrev(A_CONNECTACK
);
8758 b0
= gen_msg_abbrev(A_RELEASE
);
8760 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8762 b0
= gen_atmtype_abbrev(A_SC
);
8768 bpf_error("'metaconnect' supported only on raw ATM");
8769 b0
= gen_msg_abbrev(A_SETUP
);
8770 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8772 b0
= gen_msg_abbrev(A_CONNECT
);
8774 b0
= gen_msg_abbrev(A_RELEASE
);
8776 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8778 b0
= gen_atmtype_abbrev(A_METAC
);