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 updating VLAN, MPLS, and PPPoE offsets. */
134 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
136 static u_int orig_linktype
= -1U, orig_nl
= -1U, 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).
832 * For Ethernet, it's the offset of the Ethernet type field.
834 * For link-layer types that always use 802.2 headers, it's the
835 * offset of the LLC header.
837 * For PPP, it's the offset of the PPP type field.
839 * For Cisco HDLC, it's the offset of the CHDLC type field.
841 * For BSD loopback, it's the offset of the AF_ value.
843 * For Linux cooked sockets, it's the offset of the type field.
845 * It's set to -1 for no encapsulation, in which case, IP is assumed.
847 static u_int off_linktype
;
850 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
851 * checks to check the PPP header, assumed to follow a LAN-style link-
852 * layer header and a PPPoE session header.
854 static int is_pppoes
= 0;
857 * TRUE if the link layer includes an ATM pseudo-header.
859 static int is_atm
= 0;
862 * TRUE if "lane" appeared in the filter; it causes us to generate
863 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
865 static int is_lane
= 0;
868 * These are offsets for the ATM pseudo-header.
870 static u_int off_vpi
;
871 static u_int off_vci
;
872 static u_int off_proto
;
875 * These are offsets for the MTP2 fields.
878 static u_int off_li_hsl
;
881 * These are offsets for the MTP3 fields.
883 static u_int off_sio
;
884 static u_int off_opc
;
885 static u_int off_dpc
;
886 static u_int off_sls
;
889 * This is the offset of the first byte after the ATM pseudo_header,
890 * or -1 if there is no ATM pseudo-header.
892 static u_int off_payload
;
895 * These are offsets to the beginning of the network-layer header.
896 * They are relative to the beginning of the MAC-layer payload (i.e.,
897 * they don't include off_ll or off_macpl).
899 * If the link layer never uses 802.2 LLC:
901 * "off_nl" and "off_nl_nosnap" are the same.
903 * If the link layer always uses 802.2 LLC:
905 * "off_nl" is the offset if there's a SNAP header following
908 * "off_nl_nosnap" is the offset if there's no SNAP header.
910 * If the link layer is Ethernet:
912 * "off_nl" is the offset if the packet is an Ethernet II packet
913 * (we assume no 802.3+802.2+SNAP);
915 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
916 * with an 802.2 header following it.
919 static u_int off_nl_nosnap
;
927 linktype
= pcap_datalink(p
);
928 pcap_fddipad
= p
->fddipad
;
931 * Assume it's not raw ATM with a pseudo-header, for now.
942 * And that we're not doing PPPoE.
947 * And assume we're not doing SS7.
957 * Also assume it's not 802.11.
961 off_macpl_is_variable
= 0;
965 label_stack_depth
= 0;
966 vlan_stack_depth
= 0;
976 off_nl
= 0; /* XXX in reality, variable! */
977 off_nl_nosnap
= 0; /* no 802.2 LLC */
980 case DLT_ARCNET_LINUX
:
983 off_nl
= 0; /* XXX in reality, variable! */
984 off_nl_nosnap
= 0; /* no 802.2 LLC */
989 off_macpl
= 14; /* Ethernet header length */
990 off_nl
= 0; /* Ethernet II */
991 off_nl_nosnap
= 3; /* 802.3+802.2 */
996 * SLIP doesn't have a link level type. The 16 byte
997 * header is hacked into our SLIP driver.
1002 off_nl_nosnap
= 0; /* no 802.2 LLC */
1005 case DLT_SLIP_BSDOS
:
1006 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1011 off_nl_nosnap
= 0; /* no 802.2 LLC */
1019 off_nl_nosnap
= 0; /* no 802.2 LLC */
1026 off_nl_nosnap
= 0; /* no 802.2 LLC */
1031 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1032 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1036 off_nl_nosnap
= 0; /* no 802.2 LLC */
1041 * This does no include the Ethernet header, and
1042 * only covers session state.
1047 off_nl_nosnap
= 0; /* no 802.2 LLC */
1054 off_nl_nosnap
= 0; /* no 802.2 LLC */
1059 * FDDI doesn't really have a link-level type field.
1060 * We set "off_linktype" to the offset of the LLC header.
1062 * To check for Ethernet types, we assume that SSAP = SNAP
1063 * is being used and pick out the encapsulated Ethernet type.
1064 * XXX - should we generate code to check for SNAP?
1067 off_linktype
+= pcap_fddipad
;
1068 off_macpl
= 13; /* FDDI MAC header length */
1069 off_macpl
+= pcap_fddipad
;
1070 off_nl
= 8; /* 802.2+SNAP */
1071 off_nl_nosnap
= 3; /* 802.2 */
1076 * Token Ring doesn't really have a link-level type field.
1077 * We set "off_linktype" to the offset of the LLC header.
1079 * To check for Ethernet types, we assume that SSAP = SNAP
1080 * is being used and pick out the encapsulated Ethernet type.
1081 * XXX - should we generate code to check for SNAP?
1083 * XXX - the header is actually variable-length.
1084 * Some various Linux patched versions gave 38
1085 * as "off_linktype" and 40 as "off_nl"; however,
1086 * if a token ring packet has *no* routing
1087 * information, i.e. is not source-routed, the correct
1088 * values are 20 and 22, as they are in the vanilla code.
1090 * A packet is source-routed iff the uppermost bit
1091 * of the first byte of the source address, at an
1092 * offset of 8, has the uppermost bit set. If the
1093 * packet is source-routed, the total number of bytes
1094 * of routing information is 2 plus bits 0x1F00 of
1095 * the 16-bit value at an offset of 14 (shifted right
1096 * 8 - figure out which byte that is).
1099 off_macpl
= 14; /* Token Ring MAC header length */
1100 off_nl
= 8; /* 802.2+SNAP */
1101 off_nl_nosnap
= 3; /* 802.2 */
1104 case DLT_IEEE802_11
:
1105 case DLT_PRISM_HEADER
:
1106 case DLT_IEEE802_11_RADIO_AVS
:
1107 case DLT_IEEE802_11_RADIO
:
1109 * 802.11 doesn't really have a link-level type field.
1110 * We set "off_linktype" to the offset of the LLC header.
1112 * To check for Ethernet types, we assume that SSAP = SNAP
1113 * is being used and pick out the encapsulated Ethernet type.
1114 * XXX - should we generate code to check for SNAP?
1116 * We also handle variable-length radio headers here.
1117 * The Prism header is in theory variable-length, but in
1118 * practice it's always 144 bytes long. However, some
1119 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1120 * sometimes or always supply an AVS header, so we
1121 * have to check whether the radio header is a Prism
1122 * header or an AVS header, so, in practice, it's
1126 off_macpl
= 0; /* link-layer header is variable-length */
1127 off_macpl_is_variable
= 1;
1128 off_nl
= 8; /* 802.2+SNAP */
1129 off_nl_nosnap
= 3; /* 802.2 */
1134 * At the moment we treat PPI the same way that we treat
1135 * normal Radiotap encoded packets. The difference is in
1136 * the function that generates the code at the beginning
1137 * to compute the header length. Since this code generator
1138 * of PPI supports bare 802.11 encapsulation only (i.e.
1139 * the encapsulated DLT should be DLT_IEEE802_11) we
1140 * generate code to check for this too.
1143 off_macpl
= 0; /* link-layer header is variable-length */
1144 off_macpl_is_variable
= 1;
1145 off_nl
= 8; /* 802.2+SNAP */
1146 off_nl_nosnap
= 3; /* 802.2 */
1149 case DLT_ATM_RFC1483
:
1150 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1152 * assume routed, non-ISO PDUs
1153 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1155 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1156 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1157 * latter would presumably be treated the way PPPoE
1158 * should be, so you can do "pppoe and udp port 2049"
1159 * or "pppoa and tcp port 80" and have it check for
1160 * PPPo{A,E} and a PPP protocol of IP and....
1163 off_macpl
= 0; /* packet begins with LLC header */
1164 off_nl
= 8; /* 802.2+SNAP */
1165 off_nl_nosnap
= 3; /* 802.2 */
1170 * Full Frontal ATM; you get AALn PDUs with an ATM
1174 off_vpi
= SUNATM_VPI_POS
;
1175 off_vci
= SUNATM_VCI_POS
;
1176 off_proto
= PROTO_POS
;
1177 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1178 off_payload
= SUNATM_PKT_BEGIN_POS
;
1179 off_linktype
= off_payload
;
1180 off_macpl
= off_payload
; /* if LLC-encapsulated */
1181 off_nl
= 8; /* 802.2+SNAP */
1182 off_nl_nosnap
= 3; /* 802.2 */
1191 off_nl_nosnap
= 0; /* no 802.2 LLC */
1194 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1198 off_nl_nosnap
= 0; /* no 802.2 LLC */
1203 * LocalTalk does have a 1-byte type field in the LLAP header,
1204 * but really it just indicates whether there is a "short" or
1205 * "long" DDP packet following.
1210 off_nl_nosnap
= 0; /* no 802.2 LLC */
1213 case DLT_IP_OVER_FC
:
1215 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1216 * link-level type field. We set "off_linktype" to the
1217 * offset of the LLC header.
1219 * To check for Ethernet types, we assume that SSAP = SNAP
1220 * is being used and pick out the encapsulated Ethernet type.
1221 * XXX - should we generate code to check for SNAP? RFC
1222 * 2625 says SNAP should be used.
1226 off_nl
= 8; /* 802.2+SNAP */
1227 off_nl_nosnap
= 3; /* 802.2 */
1232 * XXX - we should set this to handle SNAP-encapsulated
1233 * frames (NLPID of 0x80).
1238 off_nl_nosnap
= 0; /* no 802.2 LLC */
1242 * the only BPF-interesting FRF.16 frames are non-control frames;
1243 * Frame Relay has a variable length link-layer
1244 * so lets start with offset 4 for now and increments later on (FIXME);
1250 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1253 case DLT_APPLE_IP_OVER_IEEE1394
:
1257 off_nl_nosnap
= 0; /* no 802.2 LLC */
1260 case DLT_SYMANTEC_FIREWALL
:
1263 off_nl
= 0; /* Ethernet II */
1264 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1267 #ifdef HAVE_NET_PFVAR_H
1270 off_macpl
= PFLOG_HDRLEN
;
1272 off_nl_nosnap
= 0; /* no 802.2 LLC */
1276 case DLT_JUNIPER_MFR
:
1277 case DLT_JUNIPER_MLFR
:
1278 case DLT_JUNIPER_MLPPP
:
1279 case DLT_JUNIPER_PPP
:
1280 case DLT_JUNIPER_CHDLC
:
1281 case DLT_JUNIPER_FRELAY
:
1285 off_nl_nosnap
= -1; /* no 802.2 LLC */
1288 case DLT_JUNIPER_ATM1
:
1289 off_linktype
= 4; /* in reality variable between 4-8 */
1290 off_macpl
= 4; /* in reality variable between 4-8 */
1295 case DLT_JUNIPER_ATM2
:
1296 off_linktype
= 8; /* in reality variable between 8-12 */
1297 off_macpl
= 8; /* in reality variable between 8-12 */
1302 /* frames captured on a Juniper PPPoE service PIC
1303 * contain raw ethernet frames */
1304 case DLT_JUNIPER_PPPOE
:
1305 case DLT_JUNIPER_ETHER
:
1308 off_nl
= 18; /* Ethernet II */
1309 off_nl_nosnap
= 21; /* 802.3+802.2 */
1312 case DLT_JUNIPER_PPPOE_ATM
:
1316 off_nl_nosnap
= -1; /* no 802.2 LLC */
1319 case DLT_JUNIPER_GGSN
:
1323 off_nl_nosnap
= -1; /* no 802.2 LLC */
1326 case DLT_JUNIPER_ES
:
1328 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1329 off_nl
= -1; /* not really a network layer but raw IP addresses */
1330 off_nl_nosnap
= -1; /* no 802.2 LLC */
1333 case DLT_JUNIPER_MONITOR
:
1336 off_nl
= 0; /* raw IP/IP6 header */
1337 off_nl_nosnap
= -1; /* no 802.2 LLC */
1340 case DLT_BACNET_MS_TP
:
1347 case DLT_JUNIPER_SERVICES
:
1349 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1350 off_nl
= -1; /* L3 proto location dep. on cookie type */
1351 off_nl_nosnap
= -1; /* no 802.2 LLC */
1354 case DLT_JUNIPER_VP
:
1361 case DLT_JUNIPER_ST
:
1368 case DLT_JUNIPER_ISM
:
1375 case DLT_JUNIPER_VS
:
1376 case DLT_JUNIPER_SRX_E2E
:
1377 case DLT_JUNIPER_FIBRECHANNEL
:
1378 case DLT_JUNIPER_ATM_CEMIC
:
1398 case DLT_MTP2_WITH_PHDR
:
1433 * Currently, only raw "link[N:M]" filtering is supported.
1435 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1437 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1438 off_nl_nosnap
= -1; /* no 802.2 LLC */
1439 off_mac
= 1; /* step over the kiss length byte */
1444 off_macpl
= 24; /* ipnet header length */
1449 case DLT_NETANALYZER
:
1450 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1451 off_linktype
= 16; /* includes 4-byte pseudo-header */
1452 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1453 off_nl
= 0; /* Ethernet II */
1454 off_nl_nosnap
= 3; /* 802.3+802.2 */
1457 case DLT_NETANALYZER_TRANSPARENT
:
1458 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1459 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1460 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1461 off_nl
= 0; /* Ethernet II */
1462 off_nl_nosnap
= 3; /* 802.3+802.2 */
1467 * For values in the range in which we've assigned new
1468 * DLT_ values, only raw "link[N:M]" filtering is supported.
1470 if (linktype
>= DLT_MATCHING_MIN
&&
1471 linktype
<= DLT_MATCHING_MAX
) {
1480 bpf_error("unknown data link type %d", linktype
);
1485 * Load a value relative to the beginning of the link-layer header.
1486 * The link-layer header doesn't necessarily begin at the beginning
1487 * of the packet data; there might be a variable-length prefix containing
1488 * radio information.
1490 static struct slist
*
1491 gen_load_llrel(offset
, size
)
1494 struct slist
*s
, *s2
;
1496 s
= gen_llprefixlen();
1499 * If "s" is non-null, it has code to arrange that the X register
1500 * contains the length of the prefix preceding the link-layer
1503 * Otherwise, the length of the prefix preceding the link-layer
1504 * header is "off_ll".
1508 * There's a variable-length prefix preceding the
1509 * link-layer header. "s" points to a list of statements
1510 * that put the length of that prefix into the X register.
1511 * do an indirect load, to use the X register as an offset.
1513 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1518 * There is no variable-length header preceding the
1519 * link-layer header; add in off_ll, which, if there's
1520 * a fixed-length header preceding the link-layer header,
1521 * is the length of that header.
1523 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1524 s
->s
.k
= offset
+ off_ll
;
1530 * Load a value relative to the beginning of the MAC-layer payload.
1532 static struct slist
*
1533 gen_load_macplrel(offset
, size
)
1536 struct slist
*s
, *s2
;
1538 s
= gen_off_macpl();
1541 * If s is non-null, the offset of the MAC-layer payload is
1542 * variable, and s points to a list of instructions that
1543 * arrange that the X register contains that offset.
1545 * Otherwise, the offset of the MAC-layer payload is constant,
1546 * and is in off_macpl.
1550 * The offset of the MAC-layer payload is in the X
1551 * register. Do an indirect load, to use the X register
1554 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1559 * The offset of the MAC-layer payload is constant,
1560 * and is in off_macpl; load the value at that offset
1561 * plus the specified offset.
1563 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1564 s
->s
.k
= off_macpl
+ offset
;
1570 * Load a value relative to the beginning of the specified header.
1572 static struct slist
*
1573 gen_load_a(offrel
, offset
, size
)
1574 enum e_offrel offrel
;
1577 struct slist
*s
, *s2
;
1582 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1587 s
= gen_load_llrel(offset
, size
);
1591 s
= gen_load_macplrel(offset
, size
);
1595 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1599 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1604 * Load the X register with the length of the IPv4 header
1605 * (plus the offset of the link-layer header, if it's
1606 * preceded by a variable-length header such as a radio
1607 * header), in bytes.
1609 s
= gen_loadx_iphdrlen();
1612 * Load the item at {offset of the MAC-layer payload} +
1613 * {offset, relative to the start of the MAC-layer
1614 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1615 * {specified offset}.
1617 * (If the offset of the MAC-layer payload is variable,
1618 * it's included in the value in the X register, and
1621 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1622 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1627 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1638 * Generate code to load into the X register the sum of the length of
1639 * the IPv4 header and any variable-length header preceding the link-layer
1642 static struct slist
*
1643 gen_loadx_iphdrlen()
1645 struct slist
*s
, *s2
;
1647 s
= gen_off_macpl();
1650 * There's a variable-length prefix preceding the
1651 * link-layer header, or the link-layer header is itself
1652 * variable-length. "s" points to a list of statements
1653 * that put the offset of the MAC-layer payload into
1656 * The 4*([k]&0xf) addressing mode can't be used, as we
1657 * don't have a constant offset, so we have to load the
1658 * value in question into the A register and add to it
1659 * the value from the X register.
1661 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1664 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1667 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1672 * The A register now contains the length of the
1673 * IP header. We need to add to it the offset of
1674 * the MAC-layer payload, which is still in the X
1675 * register, and move the result into the X register.
1677 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1678 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1681 * There is no variable-length header preceding the
1682 * link-layer header, and the link-layer header is
1683 * fixed-length; load the length of the IPv4 header,
1684 * which is at an offset of off_nl from the beginning
1685 * of the MAC-layer payload, and thus at an offset
1686 * of off_mac_pl + off_nl from the beginning of the
1689 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1690 s
->s
.k
= off_macpl
+ off_nl
;
1695 static struct block
*
1702 s
= new_stmt(BPF_LD
|BPF_IMM
);
1704 b
= new_block(JMP(BPF_JEQ
));
1710 static inline struct block
*
1713 return gen_uncond(1);
1716 static inline struct block
*
1719 return gen_uncond(0);
1723 * Byte-swap a 32-bit number.
1724 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1725 * big-endian platforms.)
1727 #define SWAPLONG(y) \
1728 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1731 * Generate code to match a particular packet type.
1733 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1734 * value, if <= ETHERMTU. We use that to determine whether to
1735 * match the type/length field or to check the type/length field for
1736 * a value <= ETHERMTU to see whether it's a type field and then do
1737 * the appropriate test.
1739 static struct block
*
1740 gen_ether_linktype(proto
)
1743 struct block
*b0
, *b1
;
1749 case LLCSAP_NETBEUI
:
1751 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1752 * so we check the DSAP and SSAP.
1754 * LLCSAP_IP checks for IP-over-802.2, rather
1755 * than IP-over-Ethernet or IP-over-SNAP.
1757 * XXX - should we check both the DSAP and the
1758 * SSAP, like this, or should we check just the
1759 * DSAP, as we do for other types <= ETHERMTU
1760 * (i.e., other SAP values)?
1762 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1764 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1765 ((proto
<< 8) | proto
));
1773 * Ethernet_II frames, which are Ethernet
1774 * frames with a frame type of ETHERTYPE_IPX;
1776 * Ethernet_802.3 frames, which are 802.3
1777 * frames (i.e., the type/length field is
1778 * a length field, <= ETHERMTU, rather than
1779 * a type field) with the first two bytes
1780 * after the Ethernet/802.3 header being
1783 * Ethernet_802.2 frames, which are 802.3
1784 * frames with an 802.2 LLC header and
1785 * with the IPX LSAP as the DSAP in the LLC
1788 * Ethernet_SNAP frames, which are 802.3
1789 * frames with an LLC header and a SNAP
1790 * header and with an OUI of 0x000000
1791 * (encapsulated Ethernet) and a protocol
1792 * ID of ETHERTYPE_IPX in the SNAP header.
1794 * XXX - should we generate the same code both
1795 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1799 * This generates code to check both for the
1800 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1802 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1803 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1807 * Now we add code to check for SNAP frames with
1808 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1810 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1814 * Now we generate code to check for 802.3
1815 * frames in general.
1817 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1821 * Now add the check for 802.3 frames before the
1822 * check for Ethernet_802.2 and Ethernet_802.3,
1823 * as those checks should only be done on 802.3
1824 * frames, not on Ethernet frames.
1829 * Now add the check for Ethernet_II frames, and
1830 * do that before checking for the other frame
1833 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1834 (bpf_int32
)ETHERTYPE_IPX
);
1838 case ETHERTYPE_ATALK
:
1839 case ETHERTYPE_AARP
:
1841 * EtherTalk (AppleTalk protocols on Ethernet link
1842 * layer) may use 802.2 encapsulation.
1846 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1847 * we check for an Ethernet type field less than
1848 * 1500, which means it's an 802.3 length field.
1850 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1854 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1855 * SNAP packets with an organization code of
1856 * 0x080007 (Apple, for Appletalk) and a protocol
1857 * type of ETHERTYPE_ATALK (Appletalk).
1859 * 802.2-encapsulated ETHERTYPE_AARP packets are
1860 * SNAP packets with an organization code of
1861 * 0x000000 (encapsulated Ethernet) and a protocol
1862 * type of ETHERTYPE_AARP (Appletalk ARP).
1864 if (proto
== ETHERTYPE_ATALK
)
1865 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1866 else /* proto == ETHERTYPE_AARP */
1867 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1871 * Check for Ethernet encapsulation (Ethertalk
1872 * phase 1?); we just check for the Ethernet
1875 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1881 if (proto
<= ETHERMTU
) {
1883 * This is an LLC SAP value, so the frames
1884 * that match would be 802.2 frames.
1885 * Check that the frame is an 802.2 frame
1886 * (i.e., that the length/type field is
1887 * a length field, <= ETHERMTU) and
1888 * then check the DSAP.
1890 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1892 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1898 * This is an Ethernet type, so compare
1899 * the length/type field with it (if
1900 * the frame is an 802.2 frame, the length
1901 * field will be <= ETHERMTU, and, as
1902 * "proto" is > ETHERMTU, this test
1903 * will fail and the frame won't match,
1904 * which is what we want).
1906 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1913 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1914 * or IPv6 then we have an error.
1916 static struct block
*
1917 gen_ipnet_linktype(proto
)
1923 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1924 (bpf_int32
)IPH_AF_INET
);
1927 case ETHERTYPE_IPV6
:
1928 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1929 (bpf_int32
)IPH_AF_INET6
);
1940 * Generate code to match a particular packet type.
1942 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1943 * value, if <= ETHERMTU. We use that to determine whether to
1944 * match the type field or to check the type field for the special
1945 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1947 static struct block
*
1948 gen_linux_sll_linktype(proto
)
1951 struct block
*b0
, *b1
;
1957 case LLCSAP_NETBEUI
:
1959 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1960 * so we check the DSAP and SSAP.
1962 * LLCSAP_IP checks for IP-over-802.2, rather
1963 * than IP-over-Ethernet or IP-over-SNAP.
1965 * XXX - should we check both the DSAP and the
1966 * SSAP, like this, or should we check just the
1967 * DSAP, as we do for other types <= ETHERMTU
1968 * (i.e., other SAP values)?
1970 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1971 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1972 ((proto
<< 8) | proto
));
1978 * Ethernet_II frames, which are Ethernet
1979 * frames with a frame type of ETHERTYPE_IPX;
1981 * Ethernet_802.3 frames, which have a frame
1982 * type of LINUX_SLL_P_802_3;
1984 * Ethernet_802.2 frames, which are 802.3
1985 * frames with an 802.2 LLC header (i.e, have
1986 * a frame type of LINUX_SLL_P_802_2) and
1987 * with the IPX LSAP as the DSAP in the LLC
1990 * Ethernet_SNAP frames, which are 802.3
1991 * frames with an LLC header and a SNAP
1992 * header and with an OUI of 0x000000
1993 * (encapsulated Ethernet) and a protocol
1994 * ID of ETHERTYPE_IPX in the SNAP header.
1996 * First, do the checks on LINUX_SLL_P_802_2
1997 * frames; generate the check for either
1998 * Ethernet_802.2 or Ethernet_SNAP frames, and
1999 * then put a check for LINUX_SLL_P_802_2 frames
2002 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2003 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2005 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2009 * Now check for 802.3 frames and OR that with
2010 * the previous test.
2012 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2016 * Now add the check for Ethernet_II frames, and
2017 * do that before checking for the other frame
2020 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2021 (bpf_int32
)ETHERTYPE_IPX
);
2025 case ETHERTYPE_ATALK
:
2026 case ETHERTYPE_AARP
:
2028 * EtherTalk (AppleTalk protocols on Ethernet link
2029 * layer) may use 802.2 encapsulation.
2033 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2034 * we check for the 802.2 protocol type in the
2035 * "Ethernet type" field.
2037 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2040 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2041 * SNAP packets with an organization code of
2042 * 0x080007 (Apple, for Appletalk) and a protocol
2043 * type of ETHERTYPE_ATALK (Appletalk).
2045 * 802.2-encapsulated ETHERTYPE_AARP packets are
2046 * SNAP packets with an organization code of
2047 * 0x000000 (encapsulated Ethernet) and a protocol
2048 * type of ETHERTYPE_AARP (Appletalk ARP).
2050 if (proto
== ETHERTYPE_ATALK
)
2051 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2052 else /* proto == ETHERTYPE_AARP */
2053 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2057 * Check for Ethernet encapsulation (Ethertalk
2058 * phase 1?); we just check for the Ethernet
2061 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2067 if (proto
<= ETHERMTU
) {
2069 * This is an LLC SAP value, so the frames
2070 * that match would be 802.2 frames.
2071 * Check for the 802.2 protocol type
2072 * in the "Ethernet type" field, and
2073 * then check the DSAP.
2075 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2077 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2083 * This is an Ethernet type, so compare
2084 * the length/type field with it (if
2085 * the frame is an 802.2 frame, the length
2086 * field will be <= ETHERMTU, and, as
2087 * "proto" is > ETHERMTU, this test
2088 * will fail and the frame won't match,
2089 * which is what we want).
2091 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2097 static struct slist
*
2098 gen_load_prism_llprefixlen()
2100 struct slist
*s1
, *s2
;
2101 struct slist
*sjeq_avs_cookie
;
2102 struct slist
*sjcommon
;
2105 * This code is not compatible with the optimizer, as
2106 * we are generating jmp instructions within a normal
2107 * slist of instructions
2112 * Generate code to load the length of the radio header into
2113 * the register assigned to hold that length, if one has been
2114 * assigned. (If one hasn't been assigned, no code we've
2115 * generated uses that prefix, so we don't need to generate any
2118 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2119 * or always use the AVS header rather than the Prism header.
2120 * We load a 4-byte big-endian value at the beginning of the
2121 * raw packet data, and see whether, when masked with 0xFFFFF000,
2122 * it's equal to 0x80211000. If so, that indicates that it's
2123 * an AVS header (the masked-out bits are the version number).
2124 * Otherwise, it's a Prism header.
2126 * XXX - the Prism header is also, in theory, variable-length,
2127 * but no known software generates headers that aren't 144
2130 if (reg_off_ll
!= -1) {
2134 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2138 * AND it with 0xFFFFF000.
2140 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2141 s2
->s
.k
= 0xFFFFF000;
2145 * Compare with 0x80211000.
2147 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2148 sjeq_avs_cookie
->s
.k
= 0x80211000;
2149 sappend(s1
, sjeq_avs_cookie
);
2154 * The 4 bytes at an offset of 4 from the beginning of
2155 * the AVS header are the length of the AVS header.
2156 * That field is big-endian.
2158 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2161 sjeq_avs_cookie
->s
.jt
= s2
;
2164 * Now jump to the code to allocate a register
2165 * into which to save the header length and
2166 * store the length there. (The "jump always"
2167 * instruction needs to have the k field set;
2168 * it's added to the PC, so, as we're jumping
2169 * over a single instruction, it should be 1.)
2171 sjcommon
= new_stmt(JMP(BPF_JA
));
2173 sappend(s1
, sjcommon
);
2176 * Now for the code that handles the Prism header.
2177 * Just load the length of the Prism header (144)
2178 * into the A register. Have the test for an AVS
2179 * header branch here if we don't have an AVS header.
2181 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2184 sjeq_avs_cookie
->s
.jf
= s2
;
2187 * Now allocate a register to hold that value and store
2188 * it. The code for the AVS header will jump here after
2189 * loading the length of the AVS header.
2191 s2
= new_stmt(BPF_ST
);
2192 s2
->s
.k
= reg_off_ll
;
2194 sjcommon
->s
.jf
= s2
;
2197 * Now move it into the X register.
2199 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2207 static struct slist
*
2208 gen_load_avs_llprefixlen()
2210 struct slist
*s1
, *s2
;
2213 * Generate code to load the length of the AVS header into
2214 * the register assigned to hold that length, if one has been
2215 * assigned. (If one hasn't been assigned, no code we've
2216 * generated uses that prefix, so we don't need to generate any
2219 if (reg_off_ll
!= -1) {
2221 * The 4 bytes at an offset of 4 from the beginning of
2222 * the AVS header are the length of the AVS header.
2223 * That field is big-endian.
2225 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2229 * Now allocate a register to hold that value and store
2232 s2
= new_stmt(BPF_ST
);
2233 s2
->s
.k
= reg_off_ll
;
2237 * Now move it into the X register.
2239 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2247 static struct slist
*
2248 gen_load_radiotap_llprefixlen()
2250 struct slist
*s1
, *s2
;
2253 * Generate code to load the length of the radiotap header into
2254 * the register assigned to hold that length, if one has been
2255 * assigned. (If one hasn't been assigned, no code we've
2256 * generated uses that prefix, so we don't need to generate any
2259 if (reg_off_ll
!= -1) {
2261 * The 2 bytes at offsets of 2 and 3 from the beginning
2262 * of the radiotap header are the length of the radiotap
2263 * header; unfortunately, it's little-endian, so we have
2264 * to load it a byte at a time and construct the value.
2268 * Load the high-order byte, at an offset of 3, shift it
2269 * left a byte, and put the result in the X register.
2271 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2273 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2276 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2280 * Load the next byte, at an offset of 2, and OR the
2281 * value from the X register into it.
2283 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2286 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2290 * Now allocate a register to hold that value and store
2293 s2
= new_stmt(BPF_ST
);
2294 s2
->s
.k
= reg_off_ll
;
2298 * Now move it into the X register.
2300 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2309 * At the moment we treat PPI as normal Radiotap encoded
2310 * packets. The difference is in the function that generates
2311 * the code at the beginning to compute the header length.
2312 * Since this code generator of PPI supports bare 802.11
2313 * encapsulation only (i.e. the encapsulated DLT should be
2314 * DLT_IEEE802_11) we generate code to check for this too;
2315 * that's done in finish_parse().
2317 static struct slist
*
2318 gen_load_ppi_llprefixlen()
2320 struct slist
*s1
, *s2
;
2323 * Generate code to load the length of the radiotap header
2324 * into the register assigned to hold that length, if one has
2327 if (reg_off_ll
!= -1) {
2329 * The 2 bytes at offsets of 2 and 3 from the beginning
2330 * of the radiotap header are the length of the radiotap
2331 * header; unfortunately, it's little-endian, so we have
2332 * to load it a byte at a time and construct the value.
2336 * Load the high-order byte, at an offset of 3, shift it
2337 * left a byte, and put the result in the X register.
2339 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2341 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2344 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2348 * Load the next byte, at an offset of 2, and OR the
2349 * value from the X register into it.
2351 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2354 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2358 * Now allocate a register to hold that value and store
2361 s2
= new_stmt(BPF_ST
);
2362 s2
->s
.k
= reg_off_ll
;
2366 * Now move it into the X register.
2368 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2377 * Load a value relative to the beginning of the link-layer header after the 802.11
2378 * header, i.e. LLC_SNAP.
2379 * The link-layer header doesn't necessarily begin at the beginning
2380 * of the packet data; there might be a variable-length prefix containing
2381 * radio information.
2383 static struct slist
*
2384 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2387 struct slist
*sjset_data_frame_1
;
2388 struct slist
*sjset_data_frame_2
;
2389 struct slist
*sjset_qos
;
2390 struct slist
*sjset_radiotap_flags
;
2391 struct slist
*sjset_radiotap_tsft
;
2392 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2393 struct slist
*s_roundup
;
2395 if (reg_off_macpl
== -1) {
2397 * No register has been assigned to the offset of
2398 * the MAC-layer payload, which means nobody needs
2399 * it; don't bother computing it - just return
2400 * what we already have.
2406 * This code is not compatible with the optimizer, as
2407 * we are generating jmp instructions within a normal
2408 * slist of instructions
2413 * If "s" is non-null, it has code to arrange that the X register
2414 * contains the length of the prefix preceding the link-layer
2417 * Otherwise, the length of the prefix preceding the link-layer
2418 * header is "off_ll".
2422 * There is no variable-length header preceding the
2423 * link-layer header.
2425 * Load the length of the fixed-length prefix preceding
2426 * the link-layer header (if any) into the X register,
2427 * and store it in the reg_off_macpl register.
2428 * That length is off_ll.
2430 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2435 * The X register contains the offset of the beginning of the
2436 * link-layer header; add 24, which is the minimum length
2437 * of the MAC header for a data frame, to that, and store it
2438 * in reg_off_macpl, and then load the Frame Control field,
2439 * which is at the offset in the X register, with an indexed load.
2441 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2443 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2446 s2
= new_stmt(BPF_ST
);
2447 s2
->s
.k
= reg_off_macpl
;
2450 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2455 * Check the Frame Control field to see if this is a data frame;
2456 * a data frame has the 0x08 bit (b3) in that field set and the
2457 * 0x04 bit (b2) clear.
2459 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2460 sjset_data_frame_1
->s
.k
= 0x08;
2461 sappend(s
, sjset_data_frame_1
);
2464 * If b3 is set, test b2, otherwise go to the first statement of
2465 * the rest of the program.
2467 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2468 sjset_data_frame_2
->s
.k
= 0x04;
2469 sappend(s
, sjset_data_frame_2
);
2470 sjset_data_frame_1
->s
.jf
= snext
;
2473 * If b2 is not set, this is a data frame; test the QoS bit.
2474 * Otherwise, go to the first statement of the rest of the
2477 sjset_data_frame_2
->s
.jt
= snext
;
2478 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2479 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2480 sappend(s
, sjset_qos
);
2483 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2485 * Otherwise, go to the first statement of the rest of the
2488 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2489 s2
->s
.k
= reg_off_macpl
;
2491 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2494 s2
= new_stmt(BPF_ST
);
2495 s2
->s
.k
= reg_off_macpl
;
2499 * If we have a radiotap header, look at it to see whether
2500 * there's Atheros padding between the MAC-layer header
2503 * Note: all of the fields in the radiotap header are
2504 * little-endian, so we byte-swap all of the values
2505 * we test against, as they will be loaded as big-endian
2508 if (linktype
== DLT_IEEE802_11_RADIO
) {
2510 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2511 * in the presence flag?
2513 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2517 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2518 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2519 sappend(s
, sjset_radiotap_flags
);
2522 * If not, skip all of this.
2524 sjset_radiotap_flags
->s
.jf
= snext
;
2527 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2529 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2530 new_stmt(JMP(BPF_JSET
));
2531 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2532 sappend(s
, sjset_radiotap_tsft
);
2535 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2536 * at an offset of 16 from the beginning of the raw packet
2537 * data (8 bytes for the radiotap header and 8 bytes for
2540 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2543 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2547 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2548 sjset_tsft_datapad
->s
.k
= 0x20;
2549 sappend(s
, sjset_tsft_datapad
);
2552 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2553 * at an offset of 8 from the beginning of the raw packet
2554 * data (8 bytes for the radiotap header).
2556 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2559 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2563 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2564 sjset_notsft_datapad
->s
.k
= 0x20;
2565 sappend(s
, sjset_notsft_datapad
);
2568 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2569 * set, round the length of the 802.11 header to
2570 * a multiple of 4. Do that by adding 3 and then
2571 * dividing by and multiplying by 4, which we do by
2574 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2575 s_roundup
->s
.k
= reg_off_macpl
;
2576 sappend(s
, s_roundup
);
2577 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2580 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2583 s2
= new_stmt(BPF_ST
);
2584 s2
->s
.k
= reg_off_macpl
;
2587 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2588 sjset_tsft_datapad
->s
.jf
= snext
;
2589 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2590 sjset_notsft_datapad
->s
.jf
= snext
;
2592 sjset_qos
->s
.jf
= snext
;
2598 insert_compute_vloffsets(b
)
2604 * For link-layer types that have a variable-length header
2605 * preceding the link-layer header, generate code to load
2606 * the offset of the link-layer header into the register
2607 * assigned to that offset, if any.
2611 case DLT_PRISM_HEADER
:
2612 s
= gen_load_prism_llprefixlen();
2615 case DLT_IEEE802_11_RADIO_AVS
:
2616 s
= gen_load_avs_llprefixlen();
2619 case DLT_IEEE802_11_RADIO
:
2620 s
= gen_load_radiotap_llprefixlen();
2624 s
= gen_load_ppi_llprefixlen();
2633 * For link-layer types that have a variable-length link-layer
2634 * header, generate code to load the offset of the MAC-layer
2635 * payload into the register assigned to that offset, if any.
2639 case DLT_IEEE802_11
:
2640 case DLT_PRISM_HEADER
:
2641 case DLT_IEEE802_11_RADIO_AVS
:
2642 case DLT_IEEE802_11_RADIO
:
2644 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2649 * If we have any offset-loading code, append all the
2650 * existing statements in the block to those statements,
2651 * and make the resulting list the list of statements
2655 sappend(s
, b
->stmts
);
2660 static struct block
*
2661 gen_ppi_dlt_check(void)
2663 struct slist
*s_load_dlt
;
2666 if (linktype
== DLT_PPI
)
2668 /* Create the statements that check for the DLT
2670 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2671 s_load_dlt
->s
.k
= 4;
2673 b
= new_block(JMP(BPF_JEQ
));
2675 b
->stmts
= s_load_dlt
;
2676 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2686 static struct slist
*
2687 gen_prism_llprefixlen(void)
2691 if (reg_off_ll
== -1) {
2693 * We haven't yet assigned a register for the length
2694 * of the radio header; allocate one.
2696 reg_off_ll
= alloc_reg();
2700 * Load the register containing the radio length
2701 * into the X register.
2703 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2704 s
->s
.k
= reg_off_ll
;
2708 static struct slist
*
2709 gen_avs_llprefixlen(void)
2713 if (reg_off_ll
== -1) {
2715 * We haven't yet assigned a register for the length
2716 * of the AVS header; allocate one.
2718 reg_off_ll
= alloc_reg();
2722 * Load the register containing the AVS length
2723 * into the X register.
2725 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2726 s
->s
.k
= reg_off_ll
;
2730 static struct slist
*
2731 gen_radiotap_llprefixlen(void)
2735 if (reg_off_ll
== -1) {
2737 * We haven't yet assigned a register for the length
2738 * of the radiotap header; allocate one.
2740 reg_off_ll
= alloc_reg();
2744 * Load the register containing the radiotap length
2745 * into the X register.
2747 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2748 s
->s
.k
= reg_off_ll
;
2753 * At the moment we treat PPI as normal Radiotap encoded
2754 * packets. The difference is in the function that generates
2755 * the code at the beginning to compute the header length.
2756 * Since this code generator of PPI supports bare 802.11
2757 * encapsulation only (i.e. the encapsulated DLT should be
2758 * DLT_IEEE802_11) we generate code to check for this too.
2760 static struct slist
*
2761 gen_ppi_llprefixlen(void)
2765 if (reg_off_ll
== -1) {
2767 * We haven't yet assigned a register for the length
2768 * of the radiotap header; allocate one.
2770 reg_off_ll
= alloc_reg();
2774 * Load the register containing the PPI length
2775 * into the X register.
2777 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2778 s
->s
.k
= reg_off_ll
;
2783 * Generate code to compute the link-layer header length, if necessary,
2784 * putting it into the X register, and to return either a pointer to a
2785 * "struct slist" for the list of statements in that code, or NULL if
2786 * no code is necessary.
2788 static struct slist
*
2789 gen_llprefixlen(void)
2793 case DLT_PRISM_HEADER
:
2794 return gen_prism_llprefixlen();
2796 case DLT_IEEE802_11_RADIO_AVS
:
2797 return gen_avs_llprefixlen();
2799 case DLT_IEEE802_11_RADIO
:
2800 return gen_radiotap_llprefixlen();
2803 return gen_ppi_llprefixlen();
2811 * Generate code to load the register containing the offset of the
2812 * MAC-layer payload into the X register; if no register for that offset
2813 * has been allocated, allocate it first.
2815 static struct slist
*
2820 if (off_macpl_is_variable
) {
2821 if (reg_off_macpl
== -1) {
2823 * We haven't yet assigned a register for the offset
2824 * of the MAC-layer payload; allocate one.
2826 reg_off_macpl
= alloc_reg();
2830 * Load the register containing the offset of the MAC-layer
2831 * payload into the X register.
2833 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2834 s
->s
.k
= reg_off_macpl
;
2838 * That offset isn't variable, so we don't need to
2839 * generate any code.
2846 * Map an Ethernet type to the equivalent PPP type.
2849 ethertype_to_ppptype(proto
)
2858 case ETHERTYPE_IPV6
:
2866 case ETHERTYPE_ATALK
:
2880 * I'm assuming the "Bridging PDU"s that go
2881 * over PPP are Spanning Tree Protocol
2895 * Generate code to match a particular packet type by matching the
2896 * link-layer type field or fields in the 802.2 LLC header.
2898 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2899 * value, if <= ETHERMTU.
2901 static struct block
*
2905 struct block
*b0
, *b1
, *b2
;
2906 const char *description
;
2908 /* are we checking MPLS-encapsulated packets? */
2909 if (label_stack_depth
> 0) {
2913 /* FIXME add other L3 proto IDs */
2914 return gen_mpls_linktype(Q_IP
);
2916 case ETHERTYPE_IPV6
:
2918 /* FIXME add other L3 proto IDs */
2919 return gen_mpls_linktype(Q_IPV6
);
2922 bpf_error("unsupported protocol over mpls");
2928 * Are we testing PPPoE packets?
2932 * The PPPoE session header is part of the
2933 * MAC-layer payload, so all references
2934 * should be relative to the beginning of
2939 * We use Ethernet protocol types inside libpcap;
2940 * map them to the corresponding PPP protocol types.
2942 proto
= ethertype_to_ppptype(proto
);
2943 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2949 case DLT_NETANALYZER
:
2950 case DLT_NETANALYZER_TRANSPARENT
:
2951 return gen_ether_linktype(proto
);
2959 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2963 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2970 case DLT_IEEE802_11
:
2971 case DLT_PRISM_HEADER
:
2972 case DLT_IEEE802_11_RADIO_AVS
:
2973 case DLT_IEEE802_11_RADIO
:
2976 * Check that we have a data frame.
2978 b0
= gen_check_802_11_data_frame();
2981 * Now check for the specified link-layer type.
2983 b1
= gen_llc_linktype(proto
);
2991 * XXX - check for LLC frames.
2993 return gen_llc_linktype(proto
);
2999 * XXX - check for LLC PDUs, as per IEEE 802.5.
3001 return gen_llc_linktype(proto
);
3005 case DLT_ATM_RFC1483
:
3007 case DLT_IP_OVER_FC
:
3008 return gen_llc_linktype(proto
);
3014 * If "is_lane" is set, check for a LANE-encapsulated
3015 * version of this protocol, otherwise check for an
3016 * LLC-encapsulated version of this protocol.
3018 * We assume LANE means Ethernet, not Token Ring.
3022 * Check that the packet doesn't begin with an
3023 * LE Control marker. (We've already generated
3026 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3031 * Now generate an Ethernet test.
3033 b1
= gen_ether_linktype(proto
);
3038 * Check for LLC encapsulation and then check the
3041 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3042 b1
= gen_llc_linktype(proto
);
3050 return gen_linux_sll_linktype(proto
);
3055 case DLT_SLIP_BSDOS
:
3058 * These types don't provide any type field; packets
3059 * are always IPv4 or IPv6.
3061 * XXX - for IPv4, check for a version number of 4, and,
3062 * for IPv6, check for a version number of 6?
3067 /* Check for a version number of 4. */
3068 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3070 case ETHERTYPE_IPV6
:
3071 /* Check for a version number of 6. */
3072 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3075 return gen_false(); /* always false */
3082 * Raw IPv4, so no type field.
3084 if (proto
== ETHERTYPE_IP
)
3085 return gen_true(); /* always true */
3087 /* Checking for something other than IPv4; always false */
3094 * Raw IPv6, so no type field.
3096 if (proto
== ETHERTYPE_IPV6
)
3097 return gen_true(); /* always true */
3099 /* Checking for something other than IPv6; always false */
3106 case DLT_PPP_SERIAL
:
3109 * We use Ethernet protocol types inside libpcap;
3110 * map them to the corresponding PPP protocol types.
3112 proto
= ethertype_to_ppptype(proto
);
3113 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3119 * We use Ethernet protocol types inside libpcap;
3120 * map them to the corresponding PPP protocol types.
3126 * Also check for Van Jacobson-compressed IP.
3127 * XXX - do this for other forms of PPP?
3129 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3130 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3132 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3137 proto
= ethertype_to_ppptype(proto
);
3138 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3148 * For DLT_NULL, the link-layer header is a 32-bit
3149 * word containing an AF_ value in *host* byte order,
3150 * and for DLT_ENC, the link-layer header begins
3151 * with a 32-bit work containing an AF_ value in
3154 * In addition, if we're reading a saved capture file,
3155 * the host byte order in the capture may not be the
3156 * same as the host byte order on this machine.
3158 * For DLT_LOOP, the link-layer header is a 32-bit
3159 * word containing an AF_ value in *network* byte order.
3161 * XXX - AF_ values may, unfortunately, be platform-
3162 * dependent; for example, FreeBSD's AF_INET6 is 24
3163 * whilst NetBSD's and OpenBSD's is 26.
3165 * This means that, when reading a capture file, just
3166 * checking for our AF_INET6 value won't work if the
3167 * capture file came from another OS.
3176 case ETHERTYPE_IPV6
:
3183 * Not a type on which we support filtering.
3184 * XXX - support those that have AF_ values
3185 * #defined on this platform, at least?
3190 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3192 * The AF_ value is in host byte order, but
3193 * the BPF interpreter will convert it to
3194 * network byte order.
3196 * If this is a save file, and it's from a
3197 * machine with the opposite byte order to
3198 * ours, we byte-swap the AF_ value.
3200 * Then we run it through "htonl()", and
3201 * generate code to compare against the result.
3203 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3204 proto
= SWAPLONG(proto
);
3205 proto
= htonl(proto
);
3207 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3209 #ifdef HAVE_NET_PFVAR_H
3212 * af field is host byte order in contrast to the rest of
3215 if (proto
== ETHERTYPE_IP
)
3216 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3217 BPF_B
, (bpf_int32
)AF_INET
));
3218 else if (proto
== ETHERTYPE_IPV6
)
3219 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3220 BPF_B
, (bpf_int32
)AF_INET6
));
3225 #endif /* HAVE_NET_PFVAR_H */
3228 case DLT_ARCNET_LINUX
:
3230 * XXX should we check for first fragment if the protocol
3238 case ETHERTYPE_IPV6
:
3239 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3240 (bpf_int32
)ARCTYPE_INET6
));
3243 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3244 (bpf_int32
)ARCTYPE_IP
);
3245 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3246 (bpf_int32
)ARCTYPE_IP_OLD
);
3251 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3252 (bpf_int32
)ARCTYPE_ARP
);
3253 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3254 (bpf_int32
)ARCTYPE_ARP_OLD
);
3258 case ETHERTYPE_REVARP
:
3259 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3260 (bpf_int32
)ARCTYPE_REVARP
));
3262 case ETHERTYPE_ATALK
:
3263 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3264 (bpf_int32
)ARCTYPE_ATALK
));
3271 case ETHERTYPE_ATALK
:
3281 * XXX - assumes a 2-byte Frame Relay header with
3282 * DLCI and flags. What if the address is longer?
3288 * Check for the special NLPID for IP.
3290 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3292 case ETHERTYPE_IPV6
:
3294 * Check for the special NLPID for IPv6.
3296 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3300 * Check for several OSI protocols.
3302 * Frame Relay packets typically have an OSI
3303 * NLPID at the beginning; we check for each
3306 * What we check for is the NLPID and a frame
3307 * control field of UI, i.e. 0x03 followed
3310 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3311 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3312 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3324 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3326 case DLT_JUNIPER_MFR
:
3327 case DLT_JUNIPER_MLFR
:
3328 case DLT_JUNIPER_MLPPP
:
3329 case DLT_JUNIPER_ATM1
:
3330 case DLT_JUNIPER_ATM2
:
3331 case DLT_JUNIPER_PPPOE
:
3332 case DLT_JUNIPER_PPPOE_ATM
:
3333 case DLT_JUNIPER_GGSN
:
3334 case DLT_JUNIPER_ES
:
3335 case DLT_JUNIPER_MONITOR
:
3336 case DLT_JUNIPER_SERVICES
:
3337 case DLT_JUNIPER_ETHER
:
3338 case DLT_JUNIPER_PPP
:
3339 case DLT_JUNIPER_FRELAY
:
3340 case DLT_JUNIPER_CHDLC
:
3341 case DLT_JUNIPER_VP
:
3342 case DLT_JUNIPER_ST
:
3343 case DLT_JUNIPER_ISM
:
3344 case DLT_JUNIPER_VS
:
3345 case DLT_JUNIPER_SRX_E2E
:
3346 case DLT_JUNIPER_FIBRECHANNEL
:
3347 case DLT_JUNIPER_ATM_CEMIC
:
3349 /* just lets verify the magic number for now -
3350 * on ATM we may have up to 6 different encapsulations on the wire
3351 * and need a lot of heuristics to figure out that the payload
3354 * FIXME encapsulation specific BPF_ filters
3356 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3358 case DLT_BACNET_MS_TP
:
3359 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3362 return gen_ipnet_linktype(proto
);
3364 case DLT_LINUX_IRDA
:
3365 bpf_error("IrDA link-layer type filtering not implemented");
3368 bpf_error("DOCSIS link-layer type filtering not implemented");
3371 case DLT_MTP2_WITH_PHDR
:
3372 bpf_error("MTP2 link-layer type filtering not implemented");
3375 bpf_error("ERF link-layer type filtering not implemented");
3378 bpf_error("PFSYNC link-layer type filtering not implemented");
3380 case DLT_LINUX_LAPD
:
3381 bpf_error("LAPD link-layer type filtering not implemented");
3385 case DLT_USB_LINUX_MMAPPED
:
3386 bpf_error("USB link-layer type filtering not implemented");
3388 case DLT_BLUETOOTH_HCI_H4
:
3389 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3390 bpf_error("Bluetooth link-layer type filtering not implemented");
3393 case DLT_CAN_SOCKETCAN
:
3394 bpf_error("CAN link-layer type filtering not implemented");
3396 case DLT_IEEE802_15_4
:
3397 case DLT_IEEE802_15_4_LINUX
:
3398 case DLT_IEEE802_15_4_NONASK_PHY
:
3399 case DLT_IEEE802_15_4_NOFCS
:
3400 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3402 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3403 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3406 bpf_error("SITA link-layer type filtering not implemented");
3409 bpf_error("RAIF1 link-layer type filtering not implemented");
3412 bpf_error("IPMB link-layer type filtering not implemented");
3415 bpf_error("AX.25 link-layer type filtering not implemented");
3418 /* Using the fixed-size NFLOG header it is possible to tell only
3419 * the address family of the packet, other meaningful data is
3420 * either missing or behind TLVs.
3422 bpf_error("NFLOG link-layer type filtering not implemented");
3426 * Does this link-layer header type have a field
3427 * indicating the type of the next protocol? If
3428 * so, off_linktype will be the offset of that
3429 * field in the packet; if not, it will be -1.
3431 if (off_linktype
!= (u_int
)-1) {
3433 * Yes; assume it's an Ethernet type. (If
3434 * it's not, it needs to be handled specially
3437 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3440 * No; report an error.
3442 description
= pcap_datalink_val_to_description(linktype
);
3443 if (description
!= NULL
) {
3444 bpf_error("%s link-layer type filtering not implemented",
3447 bpf_error("DLT %u link-layer type filtering not implemented",
3456 * Check for an LLC SNAP packet with a given organization code and
3457 * protocol type; we check the entire contents of the 802.2 LLC and
3458 * snap headers, checking for DSAP and SSAP of SNAP and a control
3459 * field of 0x03 in the LLC header, and for the specified organization
3460 * code and protocol type in the SNAP header.
3462 static struct block
*
3463 gen_snap(orgcode
, ptype
)
3464 bpf_u_int32 orgcode
;
3467 u_char snapblock
[8];
3469 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3470 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3471 snapblock
[2] = 0x03; /* control = UI */
3472 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3473 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3474 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3475 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3476 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3477 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3481 * Generate code to match frames with an LLC header.
3486 struct block
*b0
, *b1
;
3492 * We check for an Ethernet type field less than
3493 * 1500, which means it's an 802.3 length field.
3495 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
3499 * Now check for the purported DSAP and SSAP not being
3500 * 0xFF, to rule out NetWare-over-802.3.
3502 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3509 * We check for LLC traffic.
3511 b0
= gen_atmtype_abbrev(A_LLC
);
3514 case DLT_IEEE802
: /* Token Ring */
3516 * XXX - check for LLC frames.
3522 * XXX - check for LLC frames.
3526 case DLT_ATM_RFC1483
:
3528 * For LLC encapsulation, these are defined to have an
3531 * For VC encapsulation, they don't, but there's no
3532 * way to check for that; the protocol used on the VC
3533 * is negotiated out of band.
3537 case DLT_IEEE802_11
:
3538 case DLT_PRISM_HEADER
:
3539 case DLT_IEEE802_11_RADIO
:
3540 case DLT_IEEE802_11_RADIO_AVS
:
3543 * Check that we have a data frame.
3545 b0
= gen_check_802_11_data_frame();
3549 bpf_error("'llc' not supported for linktype %d", linktype
);
3557 struct block
*b0
, *b1
;
3561 * Check whether this is an LLC frame.
3566 * Load the control byte and test the low-order bit; it must
3567 * be clear for I frames.
3569 s
= gen_load_a(OR_MACPL
, 2, BPF_B
);
3570 b1
= new_block(JMP(BPF_JSET
));
3581 struct block
*b0
, *b1
;
3584 * Check whether this is an LLC frame.
3589 * Now compare the low-order 2 bit of the control byte against
3590 * the appropriate value for S frames.
3592 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3600 struct block
*b0
, *b1
;
3603 * Check whether this is an LLC frame.
3608 * Now compare the low-order 2 bit of the control byte against
3609 * the appropriate value for U frames.
3611 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3617 gen_llc_s_subtype(bpf_u_int32 subtype
)
3619 struct block
*b0
, *b1
;
3622 * Check whether this is an LLC frame.
3627 * Now check for an S frame with the appropriate type.
3629 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3635 gen_llc_u_subtype(bpf_u_int32 subtype
)
3637 struct block
*b0
, *b1
;
3640 * Check whether this is an LLC frame.
3645 * Now check for a U frame with the appropriate type.
3647 b1
= gen_mcmp(OR_MACPL
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3653 * Generate code to match a particular packet type, for link-layer types
3654 * using 802.2 LLC headers.
3656 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3657 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3659 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3660 * value, if <= ETHERMTU. We use that to determine whether to
3661 * match the DSAP or both DSAP and LSAP or to check the OUI and
3662 * protocol ID in a SNAP header.
3664 static struct block
*
3665 gen_llc_linktype(proto
)
3669 * XXX - handle token-ring variable-length header.
3675 case LLCSAP_NETBEUI
:
3677 * XXX - should we check both the DSAP and the
3678 * SSAP, like this, or should we check just the
3679 * DSAP, as we do for other types <= ETHERMTU
3680 * (i.e., other SAP values)?
3682 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3683 ((proto
<< 8) | proto
));
3687 * XXX - are there ever SNAP frames for IPX on
3688 * non-Ethernet 802.x networks?
3690 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3691 (bpf_int32
)LLCSAP_IPX
);
3693 case ETHERTYPE_ATALK
:
3695 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3696 * SNAP packets with an organization code of
3697 * 0x080007 (Apple, for Appletalk) and a protocol
3698 * type of ETHERTYPE_ATALK (Appletalk).
3700 * XXX - check for an organization code of
3701 * encapsulated Ethernet as well?
3703 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3707 * XXX - we don't have to check for IPX 802.3
3708 * here, but should we check for the IPX Ethertype?
3710 if (proto
<= ETHERMTU
) {
3712 * This is an LLC SAP value, so check
3715 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3718 * This is an Ethernet type; we assume that it's
3719 * unlikely that it'll appear in the right place
3720 * at random, and therefore check only the
3721 * location that would hold the Ethernet type
3722 * in a SNAP frame with an organization code of
3723 * 0x000000 (encapsulated Ethernet).
3725 * XXX - if we were to check for the SNAP DSAP and
3726 * LSAP, as per XXX, and were also to check for an
3727 * organization code of 0x000000 (encapsulated
3728 * Ethernet), we'd do
3730 * return gen_snap(0x000000, proto);
3732 * here; for now, we don't, as per the above.
3733 * I don't know whether it's worth the extra CPU
3734 * time to do the right check or not.
3736 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3741 static struct block
*
3742 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3746 u_int src_off
, dst_off
;
3748 struct block
*b0
, *b1
;
3762 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3763 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3769 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3770 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3777 b0
= gen_linktype(proto
);
3778 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3784 static struct block
*
3785 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3786 struct in6_addr
*addr
;
3787 struct in6_addr
*mask
;
3789 u_int src_off
, dst_off
;
3791 struct block
*b0
, *b1
;
3806 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3807 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3813 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3814 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3821 /* this order is important */
3822 a
= (u_int32_t
*)addr
;
3823 m
= (u_int32_t
*)mask
;
3824 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3825 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3827 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3829 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3831 b0
= gen_linktype(proto
);
3837 static struct block
*
3838 gen_ehostop(eaddr
, dir
)
3839 register const u_char
*eaddr
;
3842 register struct block
*b0
, *b1
;
3846 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3849 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3852 b0
= gen_ehostop(eaddr
, Q_SRC
);
3853 b1
= gen_ehostop(eaddr
, Q_DST
);
3859 b0
= gen_ehostop(eaddr
, Q_SRC
);
3860 b1
= gen_ehostop(eaddr
, Q_DST
);
3865 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3869 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3873 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3877 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3881 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3885 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3893 * Like gen_ehostop, but for DLT_FDDI
3895 static struct block
*
3896 gen_fhostop(eaddr
, dir
)
3897 register const u_char
*eaddr
;
3900 struct block
*b0
, *b1
;
3904 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3907 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3910 b0
= gen_fhostop(eaddr
, Q_SRC
);
3911 b1
= gen_fhostop(eaddr
, Q_DST
);
3917 b0
= gen_fhostop(eaddr
, Q_SRC
);
3918 b1
= gen_fhostop(eaddr
, Q_DST
);
3923 bpf_error("'addr1' is only supported on 802.11");
3927 bpf_error("'addr2' is only supported on 802.11");
3931 bpf_error("'addr3' is only supported on 802.11");
3935 bpf_error("'addr4' is only supported on 802.11");
3939 bpf_error("'ra' is only supported on 802.11");
3943 bpf_error("'ta' is only supported on 802.11");
3951 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3953 static struct block
*
3954 gen_thostop(eaddr
, dir
)
3955 register const u_char
*eaddr
;
3958 register struct block
*b0
, *b1
;
3962 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3965 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3968 b0
= gen_thostop(eaddr
, Q_SRC
);
3969 b1
= gen_thostop(eaddr
, Q_DST
);
3975 b0
= gen_thostop(eaddr
, Q_SRC
);
3976 b1
= gen_thostop(eaddr
, Q_DST
);
3981 bpf_error("'addr1' is only supported on 802.11");
3985 bpf_error("'addr2' is only supported on 802.11");
3989 bpf_error("'addr3' is only supported on 802.11");
3993 bpf_error("'addr4' is only supported on 802.11");
3997 bpf_error("'ra' is only supported on 802.11");
4001 bpf_error("'ta' is only supported on 802.11");
4009 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4010 * various 802.11 + radio headers.
4012 static struct block
*
4013 gen_wlanhostop(eaddr
, dir
)
4014 register const u_char
*eaddr
;
4017 register struct block
*b0
, *b1
, *b2
;
4018 register struct slist
*s
;
4020 #ifdef ENABLE_WLAN_FILTERING_PATCH
4023 * We need to disable the optimizer because the optimizer is buggy
4024 * and wipes out some LD instructions generated by the below
4025 * code to validate the Frame Control bits
4028 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4035 * For control frames, there is no SA.
4037 * For management frames, SA is at an
4038 * offset of 10 from the beginning of
4041 * For data frames, SA is at an offset
4042 * of 10 from the beginning of the packet
4043 * if From DS is clear, at an offset of
4044 * 16 from the beginning of the packet
4045 * if From DS is set and To DS is clear,
4046 * and an offset of 24 from the beginning
4047 * of the packet if From DS is set and To DS
4052 * Generate the tests to be done for data frames
4055 * First, check for To DS set, i.e. check "link[1] & 0x01".
4057 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4058 b1
= new_block(JMP(BPF_JSET
));
4059 b1
->s
.k
= 0x01; /* To DS */
4063 * If To DS is set, the SA is at 24.
4065 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4069 * Now, check for To DS not set, i.e. check
4070 * "!(link[1] & 0x01)".
4072 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4073 b2
= new_block(JMP(BPF_JSET
));
4074 b2
->s
.k
= 0x01; /* To DS */
4079 * If To DS is not set, the SA is at 16.
4081 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4085 * Now OR together the last two checks. That gives
4086 * the complete set of checks for data frames with
4092 * Now check for From DS being set, and AND that with
4093 * the ORed-together checks.
4095 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4096 b1
= new_block(JMP(BPF_JSET
));
4097 b1
->s
.k
= 0x02; /* From DS */
4102 * Now check for data frames with From DS not set.
4104 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4105 b2
= new_block(JMP(BPF_JSET
));
4106 b2
->s
.k
= 0x02; /* From DS */
4111 * If From DS isn't set, the SA is at 10.
4113 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4117 * Now OR together the checks for data frames with
4118 * From DS not set and for data frames with From DS
4119 * set; that gives the checks done for data frames.
4124 * Now check for a data frame.
4125 * I.e, check "link[0] & 0x08".
4127 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4128 b1
= new_block(JMP(BPF_JSET
));
4133 * AND that with the checks done for data frames.
4138 * If the high-order bit of the type value is 0, this
4139 * is a management frame.
4140 * I.e, check "!(link[0] & 0x08)".
4142 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4143 b2
= new_block(JMP(BPF_JSET
));
4149 * For management frames, the SA is at 10.
4151 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4155 * OR that with the checks done for data frames.
4156 * That gives the checks done for management and
4162 * If the low-order bit of the type value is 1,
4163 * this is either a control frame or a frame
4164 * with a reserved type, and thus not a
4167 * I.e., check "!(link[0] & 0x04)".
4169 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4170 b1
= new_block(JMP(BPF_JSET
));
4176 * AND that with the checks for data and management
4186 * For control frames, there is no DA.
4188 * For management frames, DA is at an
4189 * offset of 4 from the beginning of
4192 * For data frames, DA is at an offset
4193 * of 4 from the beginning of the packet
4194 * if To DS is clear and at an offset of
4195 * 16 from the beginning of the packet
4200 * Generate the tests to be done for data frames.
4202 * First, check for To DS set, i.e. "link[1] & 0x01".
4204 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4205 b1
= new_block(JMP(BPF_JSET
));
4206 b1
->s
.k
= 0x01; /* To DS */
4210 * If To DS is set, the DA is at 16.
4212 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4216 * Now, check for To DS not set, i.e. check
4217 * "!(link[1] & 0x01)".
4219 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4220 b2
= new_block(JMP(BPF_JSET
));
4221 b2
->s
.k
= 0x01; /* To DS */
4226 * If To DS is not set, the DA is at 4.
4228 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4232 * Now OR together the last two checks. That gives
4233 * the complete set of checks for data frames.
4238 * Now check for a data frame.
4239 * I.e, check "link[0] & 0x08".
4241 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4242 b1
= new_block(JMP(BPF_JSET
));
4247 * AND that with the checks done for data frames.
4252 * If the high-order bit of the type value is 0, this
4253 * is a management frame.
4254 * I.e, check "!(link[0] & 0x08)".
4256 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4257 b2
= new_block(JMP(BPF_JSET
));
4263 * For management frames, the DA is at 4.
4265 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4269 * OR that with the checks done for data frames.
4270 * That gives the checks done for management and
4276 * If the low-order bit of the type value is 1,
4277 * this is either a control frame or a frame
4278 * with a reserved type, and thus not a
4281 * I.e., check "!(link[0] & 0x04)".
4283 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4284 b1
= new_block(JMP(BPF_JSET
));
4290 * AND that with the checks for data and management
4298 * Not present in management frames; addr1 in other
4303 * If the high-order bit of the type value is 0, this
4304 * is a management frame.
4305 * I.e, check "(link[0] & 0x08)".
4307 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4308 b1
= new_block(JMP(BPF_JSET
));
4315 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4318 * AND that with the check of addr1.
4325 * Not present in management frames; addr2, if present,
4330 * Not present in CTS or ACK control frames.
4332 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4333 IEEE80211_FC0_TYPE_MASK
);
4335 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4336 IEEE80211_FC0_SUBTYPE_MASK
);
4338 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4339 IEEE80211_FC0_SUBTYPE_MASK
);
4345 * If the high-order bit of the type value is 0, this
4346 * is a management frame.
4347 * I.e, check "(link[0] & 0x08)".
4349 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4350 b1
= new_block(JMP(BPF_JSET
));
4355 * AND that with the check for frames other than
4356 * CTS and ACK frames.
4363 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4368 * XXX - add BSSID keyword?
4371 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4375 * Not present in CTS or ACK control frames.
4377 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4378 IEEE80211_FC0_TYPE_MASK
);
4380 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4381 IEEE80211_FC0_SUBTYPE_MASK
);
4383 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4384 IEEE80211_FC0_SUBTYPE_MASK
);
4388 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4394 * Not present in control frames.
4396 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4397 IEEE80211_FC0_TYPE_MASK
);
4399 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4405 * Present only if the direction mask has both "From DS"
4406 * and "To DS" set. Neither control frames nor management
4407 * frames should have both of those set, so we don't
4408 * check the frame type.
4410 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4411 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4412 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4417 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4418 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4424 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4425 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4434 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4435 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4436 * as the RFC states.)
4438 static struct block
*
4439 gen_ipfchostop(eaddr
, dir
)
4440 register const u_char
*eaddr
;
4443 register struct block
*b0
, *b1
;
4447 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4450 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4453 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4454 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4460 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4461 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4466 bpf_error("'addr1' is only supported on 802.11");
4470 bpf_error("'addr2' is only supported on 802.11");
4474 bpf_error("'addr3' is only supported on 802.11");
4478 bpf_error("'addr4' is only supported on 802.11");
4482 bpf_error("'ra' is only supported on 802.11");
4486 bpf_error("'ta' is only supported on 802.11");
4494 * This is quite tricky because there may be pad bytes in front of the
4495 * DECNET header, and then there are two possible data packet formats that
4496 * carry both src and dst addresses, plus 5 packet types in a format that
4497 * carries only the src node, plus 2 types that use a different format and
4498 * also carry just the src node.
4502 * Instead of doing those all right, we just look for data packets with
4503 * 0 or 1 bytes of padding. If you want to look at other packets, that
4504 * will require a lot more hacking.
4506 * To add support for filtering on DECNET "areas" (network numbers)
4507 * one would want to add a "mask" argument to this routine. That would
4508 * make the filter even more inefficient, although one could be clever
4509 * and not generate masking instructions if the mask is 0xFFFF.
4511 static struct block
*
4512 gen_dnhostop(addr
, dir
)
4516 struct block
*b0
, *b1
, *b2
, *tmp
;
4517 u_int offset_lh
; /* offset if long header is received */
4518 u_int offset_sh
; /* offset if short header is received */
4523 offset_sh
= 1; /* follows flags */
4524 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4528 offset_sh
= 3; /* follows flags, dstnode */
4529 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4533 /* Inefficient because we do our Calvinball dance twice */
4534 b0
= gen_dnhostop(addr
, Q_SRC
);
4535 b1
= gen_dnhostop(addr
, Q_DST
);
4541 /* Inefficient because we do our Calvinball dance twice */
4542 b0
= gen_dnhostop(addr
, Q_SRC
);
4543 b1
= gen_dnhostop(addr
, Q_DST
);
4548 bpf_error("ISO host filtering not implemented");
4553 b0
= gen_linktype(ETHERTYPE_DN
);
4554 /* Check for pad = 1, long header case */
4555 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4556 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4557 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4558 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4560 /* Check for pad = 0, long header case */
4561 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4562 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4565 /* Check for pad = 1, short header case */
4566 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4567 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4568 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4571 /* Check for pad = 0, short header case */
4572 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4573 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4577 /* Combine with test for linktype */
4583 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4584 * test the bottom-of-stack bit, and then check the version number
4585 * field in the IP header.
4587 static struct block
*
4588 gen_mpls_linktype(proto
)
4591 struct block
*b0
, *b1
;
4596 /* match the bottom-of-stack bit */
4597 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4598 /* match the IPv4 version number */
4599 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4604 /* match the bottom-of-stack bit */
4605 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4606 /* match the IPv4 version number */
4607 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4616 static struct block
*
4617 gen_host(addr
, mask
, proto
, dir
, type
)
4624 struct block
*b0
, *b1
;
4625 const char *typestr
;
4635 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4637 * Only check for non-IPv4 addresses if we're not
4638 * checking MPLS-encapsulated packets.
4640 if (label_stack_depth
== 0) {
4641 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4643 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4649 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4652 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4655 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4658 bpf_error("'tcp' modifier applied to %s", typestr
);
4661 bpf_error("'sctp' modifier applied to %s", typestr
);
4664 bpf_error("'udp' modifier applied to %s", typestr
);
4667 bpf_error("'icmp' modifier applied to %s", typestr
);
4670 bpf_error("'igmp' modifier applied to %s", typestr
);
4673 bpf_error("'igrp' modifier applied to %s", typestr
);
4676 bpf_error("'pim' modifier applied to %s", typestr
);
4679 bpf_error("'vrrp' modifier applied to %s", typestr
);
4682 bpf_error("'carp' modifier applied to %s", typestr
);
4685 bpf_error("ATALK host filtering not implemented");
4688 bpf_error("AARP host filtering not implemented");
4691 return gen_dnhostop(addr
, dir
);
4694 bpf_error("SCA host filtering not implemented");
4697 bpf_error("LAT host filtering not implemented");
4700 bpf_error("MOPDL host filtering not implemented");
4703 bpf_error("MOPRC host filtering not implemented");
4706 bpf_error("'ip6' modifier applied to ip host");
4709 bpf_error("'icmp6' modifier applied to %s", typestr
);
4712 bpf_error("'ah' modifier applied to %s", typestr
);
4715 bpf_error("'esp' modifier applied to %s", typestr
);
4718 bpf_error("ISO host filtering not implemented");
4721 bpf_error("'esis' modifier applied to %s", typestr
);
4724 bpf_error("'isis' modifier applied to %s", typestr
);
4727 bpf_error("'clnp' modifier applied to %s", typestr
);
4730 bpf_error("'stp' modifier applied to %s", typestr
);
4733 bpf_error("IPX host filtering not implemented");
4736 bpf_error("'netbeui' modifier applied to %s", typestr
);
4739 bpf_error("'radio' modifier applied to %s", typestr
);
4748 static struct block
*
4749 gen_host6(addr
, mask
, proto
, dir
, type
)
4750 struct in6_addr
*addr
;
4751 struct in6_addr
*mask
;
4756 const char *typestr
;
4766 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4769 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4772 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4775 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4778 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4781 bpf_error("'sctp' modifier applied to %s", typestr
);
4784 bpf_error("'tcp' modifier applied to %s", typestr
);
4787 bpf_error("'udp' modifier applied to %s", typestr
);
4790 bpf_error("'icmp' modifier applied to %s", typestr
);
4793 bpf_error("'igmp' modifier applied to %s", typestr
);
4796 bpf_error("'igrp' modifier applied to %s", typestr
);
4799 bpf_error("'pim' modifier applied to %s", typestr
);
4802 bpf_error("'vrrp' modifier applied to %s", typestr
);
4805 bpf_error("'carp' modifier applied to %s", typestr
);
4808 bpf_error("ATALK host filtering not implemented");
4811 bpf_error("AARP host filtering not implemented");
4814 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4817 bpf_error("SCA host filtering not implemented");
4820 bpf_error("LAT host filtering not implemented");
4823 bpf_error("MOPDL host filtering not implemented");
4826 bpf_error("MOPRC host filtering not implemented");
4829 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4832 bpf_error("'icmp6' modifier applied to %s", typestr
);
4835 bpf_error("'ah' modifier applied to %s", typestr
);
4838 bpf_error("'esp' modifier applied to %s", typestr
);
4841 bpf_error("ISO host filtering not implemented");
4844 bpf_error("'esis' modifier applied to %s", typestr
);
4847 bpf_error("'isis' modifier applied to %s", typestr
);
4850 bpf_error("'clnp' modifier applied to %s", typestr
);
4853 bpf_error("'stp' modifier applied to %s", typestr
);
4856 bpf_error("IPX host filtering not implemented");
4859 bpf_error("'netbeui' modifier applied to %s", typestr
);
4862 bpf_error("'radio' modifier applied to %s", typestr
);
4872 static struct block
*
4873 gen_gateway(eaddr
, alist
, proto
, dir
)
4874 const u_char
*eaddr
;
4875 bpf_u_int32
**alist
;
4879 struct block
*b0
, *b1
, *tmp
;
4882 bpf_error("direction applied to 'gateway'");
4891 case DLT_NETANALYZER
:
4892 case DLT_NETANALYZER_TRANSPARENT
:
4893 b0
= gen_ehostop(eaddr
, Q_OR
);
4896 b0
= gen_fhostop(eaddr
, Q_OR
);
4899 b0
= gen_thostop(eaddr
, Q_OR
);
4901 case DLT_IEEE802_11
:
4902 case DLT_PRISM_HEADER
:
4903 case DLT_IEEE802_11_RADIO_AVS
:
4904 case DLT_IEEE802_11_RADIO
:
4906 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4911 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4913 * Check that the packet doesn't begin with an
4914 * LE Control marker. (We've already generated
4917 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4922 * Now check the MAC address.
4924 b0
= gen_ehostop(eaddr
, Q_OR
);
4927 case DLT_IP_OVER_FC
:
4928 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4932 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4934 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4936 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4945 bpf_error("illegal modifier of 'gateway'");
4951 gen_proto_abbrev(proto
)
4960 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4961 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4966 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4967 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4972 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4973 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4978 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4981 #ifndef IPPROTO_IGMP
4982 #define IPPROTO_IGMP 2
4986 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4989 #ifndef IPPROTO_IGRP
4990 #define IPPROTO_IGRP 9
4993 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4997 #define IPPROTO_PIM 103
5001 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5002 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5006 #ifndef IPPROTO_VRRP
5007 #define IPPROTO_VRRP 112
5011 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5014 #ifndef IPPROTO_CARP
5015 #define IPPROTO_CARP 112
5019 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5023 b1
= gen_linktype(ETHERTYPE_IP
);
5027 b1
= gen_linktype(ETHERTYPE_ARP
);
5031 b1
= gen_linktype(ETHERTYPE_REVARP
);
5035 bpf_error("link layer applied in wrong context");
5038 b1
= gen_linktype(ETHERTYPE_ATALK
);
5042 b1
= gen_linktype(ETHERTYPE_AARP
);
5046 b1
= gen_linktype(ETHERTYPE_DN
);
5050 b1
= gen_linktype(ETHERTYPE_SCA
);
5054 b1
= gen_linktype(ETHERTYPE_LAT
);
5058 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5062 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5066 b1
= gen_linktype(ETHERTYPE_IPV6
);
5069 #ifndef IPPROTO_ICMPV6
5070 #define IPPROTO_ICMPV6 58
5073 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5077 #define IPPROTO_AH 51
5080 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5081 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5086 #define IPPROTO_ESP 50
5089 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5090 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5095 b1
= gen_linktype(LLCSAP_ISONS
);
5099 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5103 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5106 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5107 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5108 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5110 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5112 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5114 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5118 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5119 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5120 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5122 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5124 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5126 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5130 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5131 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5132 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5134 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5139 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5140 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5145 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5146 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5148 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5150 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5155 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5156 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5161 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5162 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5167 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5171 b1
= gen_linktype(LLCSAP_8021D
);
5175 b1
= gen_linktype(LLCSAP_IPX
);
5179 b1
= gen_linktype(LLCSAP_NETBEUI
);
5183 bpf_error("'radio' is not a valid protocol type");
5191 static struct block
*
5197 /* not IPv4 frag other than the first frag */
5198 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5199 b
= new_block(JMP(BPF_JSET
));
5208 * Generate a comparison to a port value in the transport-layer header
5209 * at the specified offset from the beginning of that header.
5211 * XXX - this handles a variable-length prefix preceding the link-layer
5212 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5213 * variable-length link-layer headers (such as Token Ring or 802.11
5216 static struct block
*
5217 gen_portatom(off
, v
)
5221 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5224 static struct block
*
5225 gen_portatom6(off
, v
)
5229 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5233 gen_portop(port
, proto
, dir
)
5234 int port
, proto
, dir
;
5236 struct block
*b0
, *b1
, *tmp
;
5238 /* ip proto 'proto' and not a fragment other than the first fragment */
5239 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5245 b1
= gen_portatom(0, (bpf_int32
)port
);
5249 b1
= gen_portatom(2, (bpf_int32
)port
);
5254 tmp
= gen_portatom(0, (bpf_int32
)port
);
5255 b1
= gen_portatom(2, (bpf_int32
)port
);
5260 tmp
= gen_portatom(0, (bpf_int32
)port
);
5261 b1
= gen_portatom(2, (bpf_int32
)port
);
5273 static struct block
*
5274 gen_port(port
, ip_proto
, dir
)
5279 struct block
*b0
, *b1
, *tmp
;
5284 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5285 * not LLC encapsulation with LLCSAP_IP.
5287 * For IEEE 802 networks - which includes 802.5 token ring
5288 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5289 * says that SNAP encapsulation is used, not LLC encapsulation
5292 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5293 * RFC 2225 say that SNAP encapsulation is used, not LLC
5294 * encapsulation with LLCSAP_IP.
5296 * So we always check for ETHERTYPE_IP.
5298 b0
= gen_linktype(ETHERTYPE_IP
);
5304 b1
= gen_portop(port
, ip_proto
, dir
);
5308 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5309 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5311 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5323 gen_portop6(port
, proto
, dir
)
5324 int port
, proto
, dir
;
5326 struct block
*b0
, *b1
, *tmp
;
5328 /* ip6 proto 'proto' */
5329 /* XXX - catch the first fragment of a fragmented packet? */
5330 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5334 b1
= gen_portatom6(0, (bpf_int32
)port
);
5338 b1
= gen_portatom6(2, (bpf_int32
)port
);
5343 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5344 b1
= gen_portatom6(2, (bpf_int32
)port
);
5349 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5350 b1
= gen_portatom6(2, (bpf_int32
)port
);
5362 static struct block
*
5363 gen_port6(port
, ip_proto
, dir
)
5368 struct block
*b0
, *b1
, *tmp
;
5370 /* link proto ip6 */
5371 b0
= gen_linktype(ETHERTYPE_IPV6
);
5377 b1
= gen_portop6(port
, ip_proto
, dir
);
5381 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5382 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5384 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5395 /* gen_portrange code */
5396 static struct block
*
5397 gen_portrangeatom(off
, v1
, v2
)
5401 struct block
*b1
, *b2
;
5405 * Reverse the order of the ports, so v1 is the lower one.
5414 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5415 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5423 gen_portrangeop(port1
, port2
, proto
, dir
)
5428 struct block
*b0
, *b1
, *tmp
;
5430 /* ip proto 'proto' and not a fragment other than the first fragment */
5431 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5437 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5441 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5446 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5447 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5452 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5453 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5465 static struct block
*
5466 gen_portrange(port1
, port2
, ip_proto
, dir
)
5471 struct block
*b0
, *b1
, *tmp
;
5474 b0
= gen_linktype(ETHERTYPE_IP
);
5480 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5484 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5485 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5487 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5498 static struct block
*
5499 gen_portrangeatom6(off
, v1
, v2
)
5503 struct block
*b1
, *b2
;
5507 * Reverse the order of the ports, so v1 is the lower one.
5516 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5517 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5525 gen_portrangeop6(port1
, port2
, proto
, dir
)
5530 struct block
*b0
, *b1
, *tmp
;
5532 /* ip6 proto 'proto' */
5533 /* XXX - catch the first fragment of a fragmented packet? */
5534 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5538 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5542 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5547 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5548 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5553 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5554 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5566 static struct block
*
5567 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5572 struct block
*b0
, *b1
, *tmp
;
5574 /* link proto ip6 */
5575 b0
= gen_linktype(ETHERTYPE_IPV6
);
5581 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5585 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5586 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5588 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5600 lookup_proto(name
, proto
)
5601 register const char *name
;
5611 v
= pcap_nametoproto(name
);
5612 if (v
== PROTO_UNDEF
)
5613 bpf_error("unknown ip proto '%s'", name
);
5617 /* XXX should look up h/w protocol type based on linktype */
5618 v
= pcap_nametoeproto(name
);
5619 if (v
== PROTO_UNDEF
) {
5620 v
= pcap_nametollc(name
);
5621 if (v
== PROTO_UNDEF
)
5622 bpf_error("unknown ether proto '%s'", name
);
5627 if (strcmp(name
, "esis") == 0)
5629 else if (strcmp(name
, "isis") == 0)
5631 else if (strcmp(name
, "clnp") == 0)
5634 bpf_error("unknown osi proto '%s'", name
);
5654 static struct block
*
5655 gen_protochain(v
, proto
, dir
)
5660 #ifdef NO_PROTOCHAIN
5661 return gen_proto(v
, proto
, dir
);
5663 struct block
*b0
, *b
;
5664 struct slist
*s
[100];
5665 int fix2
, fix3
, fix4
, fix5
;
5666 int ahcheck
, again
, end
;
5668 int reg2
= alloc_reg();
5670 memset(s
, 0, sizeof(s
));
5671 fix2
= fix3
= fix4
= fix5
= 0;
5678 b0
= gen_protochain(v
, Q_IP
, dir
);
5679 b
= gen_protochain(v
, Q_IPV6
, dir
);
5683 bpf_error("bad protocol applied for 'protochain'");
5688 * We don't handle variable-length prefixes before the link-layer
5689 * header, or variable-length link-layer headers, here yet.
5690 * We might want to add BPF instructions to do the protochain
5691 * work, to simplify that and, on platforms that have a BPF
5692 * interpreter with the new instructions, let the filtering
5693 * be done in the kernel. (We already require a modified BPF
5694 * engine to do the protochain stuff, to support backward
5695 * branches, and backward branch support is unlikely to appear
5696 * in kernel BPF engines.)
5700 case DLT_IEEE802_11
:
5701 case DLT_PRISM_HEADER
:
5702 case DLT_IEEE802_11_RADIO_AVS
:
5703 case DLT_IEEE802_11_RADIO
:
5705 bpf_error("'protochain' not supported with 802.11");
5708 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5711 * s[0] is a dummy entry to protect other BPF insn from damage
5712 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5713 * hard to find interdependency made by jump table fixup.
5716 s
[i
] = new_stmt(0); /*dummy*/
5721 b0
= gen_linktype(ETHERTYPE_IP
);
5724 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5725 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5727 /* X = ip->ip_hl << 2 */
5728 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5729 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5734 b0
= gen_linktype(ETHERTYPE_IPV6
);
5736 /* A = ip6->ip_nxt */
5737 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5738 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5740 /* X = sizeof(struct ip6_hdr) */
5741 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5747 bpf_error("unsupported proto to gen_protochain");
5751 /* again: if (A == v) goto end; else fall through; */
5753 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5755 s
[i
]->s
.jt
= NULL
; /*later*/
5756 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5760 #ifndef IPPROTO_NONE
5761 #define IPPROTO_NONE 59
5763 /* if (A == IPPROTO_NONE) goto end */
5764 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5765 s
[i
]->s
.jt
= NULL
; /*later*/
5766 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5767 s
[i
]->s
.k
= IPPROTO_NONE
;
5768 s
[fix5
]->s
.jf
= s
[i
];
5772 if (proto
== Q_IPV6
) {
5773 int v6start
, v6end
, v6advance
, j
;
5776 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5777 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5778 s
[i
]->s
.jt
= NULL
; /*later*/
5779 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5780 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5781 s
[fix2
]->s
.jf
= s
[i
];
5783 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5784 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5785 s
[i
]->s
.jt
= NULL
; /*later*/
5786 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5787 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5789 /* if (A == IPPROTO_ROUTING) goto v6advance */
5790 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5791 s
[i
]->s
.jt
= NULL
; /*later*/
5792 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5793 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5795 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5796 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5797 s
[i
]->s
.jt
= NULL
; /*later*/
5798 s
[i
]->s
.jf
= NULL
; /*later*/
5799 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5809 * A = P[X + packet head];
5810 * X = X + (P[X + packet head + 1] + 1) * 8;
5812 /* A = P[X + packet head] */
5813 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5814 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5817 s
[i
] = new_stmt(BPF_ST
);
5820 /* A = P[X + packet head + 1]; */
5821 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5822 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 1;
5825 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5829 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5833 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5837 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5840 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5844 /* goto again; (must use BPF_JA for backward jump) */
5845 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5846 s
[i
]->s
.k
= again
- i
- 1;
5847 s
[i
- 1]->s
.jf
= s
[i
];
5851 for (j
= v6start
; j
<= v6end
; j
++)
5852 s
[j
]->s
.jt
= s
[v6advance
];
5855 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5857 s
[fix2
]->s
.jf
= s
[i
];
5863 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5864 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5865 s
[i
]->s
.jt
= NULL
; /*later*/
5866 s
[i
]->s
.jf
= NULL
; /*later*/
5867 s
[i
]->s
.k
= IPPROTO_AH
;
5869 s
[fix3
]->s
.jf
= s
[ahcheck
];
5876 * X = X + (P[X + 1] + 2) * 4;
5879 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5881 /* A = P[X + packet head]; */
5882 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5883 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5886 s
[i
] = new_stmt(BPF_ST
);
5890 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5893 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5897 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5899 /* A = P[X + packet head] */
5900 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5901 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5904 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5908 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5912 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5915 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5919 /* goto again; (must use BPF_JA for backward jump) */
5920 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5921 s
[i
]->s
.k
= again
- i
- 1;
5926 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5928 s
[fix2
]->s
.jt
= s
[end
];
5929 s
[fix4
]->s
.jf
= s
[end
];
5930 s
[fix5
]->s
.jt
= s
[end
];
5937 for (i
= 0; i
< max
- 1; i
++)
5938 s
[i
]->next
= s
[i
+ 1];
5939 s
[max
- 1]->next
= NULL
;
5944 b
= new_block(JMP(BPF_JEQ
));
5945 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5955 static struct block
*
5956 gen_check_802_11_data_frame()
5959 struct block
*b0
, *b1
;
5962 * A data frame has the 0x08 bit (b3) in the frame control field set
5963 * and the 0x04 bit (b2) clear.
5965 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5966 b0
= new_block(JMP(BPF_JSET
));
5970 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5971 b1
= new_block(JMP(BPF_JSET
));
5982 * Generate code that checks whether the packet is a packet for protocol
5983 * <proto> and whether the type field in that protocol's header has
5984 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5985 * IP packet and checks the protocol number in the IP header against <v>.
5987 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5988 * against Q_IP and Q_IPV6.
5990 static struct block
*
5991 gen_proto(v
, proto
, dir
)
5996 struct block
*b0
, *b1
;
6001 if (dir
!= Q_DEFAULT
)
6002 bpf_error("direction applied to 'proto'");
6006 b0
= gen_proto(v
, Q_IP
, dir
);
6007 b1
= gen_proto(v
, Q_IPV6
, dir
);
6013 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6014 * not LLC encapsulation with LLCSAP_IP.
6016 * For IEEE 802 networks - which includes 802.5 token ring
6017 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6018 * says that SNAP encapsulation is used, not LLC encapsulation
6021 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6022 * RFC 2225 say that SNAP encapsulation is used, not LLC
6023 * encapsulation with LLCSAP_IP.
6025 * So we always check for ETHERTYPE_IP.
6027 b0
= gen_linktype(ETHERTYPE_IP
);
6029 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6031 b1
= gen_protochain(v
, Q_IP
);
6041 * Frame Relay packets typically have an OSI
6042 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6043 * generates code to check for all the OSI
6044 * NLPIDs, so calling it and then adding a check
6045 * for the particular NLPID for which we're
6046 * looking is bogus, as we can just check for
6049 * What we check for is the NLPID and a frame
6050 * control field value of UI, i.e. 0x03 followed
6053 * XXX - assumes a 2-byte Frame Relay header with
6054 * DLCI and flags. What if the address is longer?
6056 * XXX - what about SNAP-encapsulated frames?
6058 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6064 * Cisco uses an Ethertype lookalike - for OSI,
6067 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6068 /* OSI in C-HDLC is stuffed with a fudge byte */
6069 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6074 b0
= gen_linktype(LLCSAP_ISONS
);
6075 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6081 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6083 * 4 is the offset of the PDU type relative to the IS-IS
6086 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6091 bpf_error("arp does not encapsulate another protocol");
6095 bpf_error("rarp does not encapsulate another protocol");
6099 bpf_error("atalk encapsulation is not specifiable");
6103 bpf_error("decnet encapsulation is not specifiable");
6107 bpf_error("sca does not encapsulate another protocol");
6111 bpf_error("lat does not encapsulate another protocol");
6115 bpf_error("moprc does not encapsulate another protocol");
6119 bpf_error("mopdl does not encapsulate another protocol");
6123 return gen_linktype(v
);
6126 bpf_error("'udp proto' is bogus");
6130 bpf_error("'tcp proto' is bogus");
6134 bpf_error("'sctp proto' is bogus");
6138 bpf_error("'icmp proto' is bogus");
6142 bpf_error("'igmp proto' is bogus");
6146 bpf_error("'igrp proto' is bogus");
6150 bpf_error("'pim proto' is bogus");
6154 bpf_error("'vrrp proto' is bogus");
6158 bpf_error("'carp proto' is bogus");
6162 b0
= gen_linktype(ETHERTYPE_IPV6
);
6165 * Also check for a fragment header before the final
6168 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6169 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
6171 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6174 b1
= gen_protochain(v
, Q_IPV6
);
6180 bpf_error("'icmp6 proto' is bogus");
6183 bpf_error("'ah proto' is bogus");
6186 bpf_error("'ah proto' is bogus");
6189 bpf_error("'stp proto' is bogus");
6192 bpf_error("'ipx proto' is bogus");
6195 bpf_error("'netbeui proto' is bogus");
6198 bpf_error("'radio proto' is bogus");
6209 register const char *name
;
6212 int proto
= q
.proto
;
6216 bpf_u_int32 mask
, addr
;
6218 bpf_u_int32
**alist
;
6221 struct sockaddr_in
*sin4
;
6222 struct sockaddr_in6
*sin6
;
6223 struct addrinfo
*res
, *res0
;
6224 struct in6_addr mask128
;
6226 struct block
*b
, *tmp
;
6227 int port
, real_proto
;
6233 addr
= pcap_nametonetaddr(name
);
6235 bpf_error("unknown network '%s'", name
);
6236 /* Left justify network addr and calculate its network mask */
6238 while (addr
&& (addr
& 0xff000000) == 0) {
6242 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6246 if (proto
== Q_LINK
) {
6250 case DLT_NETANALYZER
:
6251 case DLT_NETANALYZER_TRANSPARENT
:
6252 eaddr
= pcap_ether_hostton(name
);
6255 "unknown ether host '%s'", name
);
6256 b
= gen_ehostop(eaddr
, dir
);
6261 eaddr
= pcap_ether_hostton(name
);
6264 "unknown FDDI host '%s'", name
);
6265 b
= gen_fhostop(eaddr
, dir
);
6270 eaddr
= pcap_ether_hostton(name
);
6273 "unknown token ring host '%s'", name
);
6274 b
= gen_thostop(eaddr
, dir
);
6278 case DLT_IEEE802_11
:
6279 case DLT_PRISM_HEADER
:
6280 case DLT_IEEE802_11_RADIO_AVS
:
6281 case DLT_IEEE802_11_RADIO
:
6283 eaddr
= pcap_ether_hostton(name
);
6286 "unknown 802.11 host '%s'", name
);
6287 b
= gen_wlanhostop(eaddr
, dir
);
6291 case DLT_IP_OVER_FC
:
6292 eaddr
= pcap_ether_hostton(name
);
6295 "unknown Fibre Channel host '%s'", name
);
6296 b
= gen_ipfchostop(eaddr
, dir
);
6305 * Check that the packet doesn't begin
6306 * with an LE Control marker. (We've
6307 * already generated a test for LANE.)
6309 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6313 eaddr
= pcap_ether_hostton(name
);
6316 "unknown ether host '%s'", name
);
6317 b
= gen_ehostop(eaddr
, dir
);
6323 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6324 } else if (proto
== Q_DECNET
) {
6325 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6327 * I don't think DECNET hosts can be multihomed, so
6328 * there is no need to build up a list of addresses
6330 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6333 alist
= pcap_nametoaddr(name
);
6334 if (alist
== NULL
|| *alist
== NULL
)
6335 bpf_error("unknown host '%s'", name
);
6337 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6339 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6341 tmp
= gen_host(**alist
++, 0xffffffff,
6342 tproto
, dir
, q
.addr
);
6348 memset(&mask128
, 0xff, sizeof(mask128
));
6349 res0
= res
= pcap_nametoaddrinfo(name
);
6351 bpf_error("unknown host '%s'", name
);
6354 tproto
= tproto6
= proto
;
6355 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6359 for (res
= res0
; res
; res
= res
->ai_next
) {
6360 switch (res
->ai_family
) {
6362 if (tproto
== Q_IPV6
)
6365 sin4
= (struct sockaddr_in
*)
6367 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6368 0xffffffff, tproto
, dir
, q
.addr
);
6371 if (tproto6
== Q_IP
)
6374 sin6
= (struct sockaddr_in6
*)
6376 tmp
= gen_host6(&sin6
->sin6_addr
,
6377 &mask128
, tproto6
, dir
, q
.addr
);
6389 bpf_error("unknown host '%s'%s", name
,
6390 (proto
== Q_DEFAULT
)
6392 : " for specified address family");
6399 if (proto
!= Q_DEFAULT
&&
6400 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6401 bpf_error("illegal qualifier of 'port'");
6402 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6403 bpf_error("unknown port '%s'", name
);
6404 if (proto
== Q_UDP
) {
6405 if (real_proto
== IPPROTO_TCP
)
6406 bpf_error("port '%s' is tcp", name
);
6407 else if (real_proto
== IPPROTO_SCTP
)
6408 bpf_error("port '%s' is sctp", name
);
6410 /* override PROTO_UNDEF */
6411 real_proto
= IPPROTO_UDP
;
6413 if (proto
== Q_TCP
) {
6414 if (real_proto
== IPPROTO_UDP
)
6415 bpf_error("port '%s' is udp", name
);
6417 else if (real_proto
== IPPROTO_SCTP
)
6418 bpf_error("port '%s' is sctp", name
);
6420 /* override PROTO_UNDEF */
6421 real_proto
= IPPROTO_TCP
;
6423 if (proto
== Q_SCTP
) {
6424 if (real_proto
== IPPROTO_UDP
)
6425 bpf_error("port '%s' is udp", name
);
6427 else if (real_proto
== IPPROTO_TCP
)
6428 bpf_error("port '%s' is tcp", name
);
6430 /* override PROTO_UNDEF */
6431 real_proto
= IPPROTO_SCTP
;
6434 bpf_error("illegal port number %d < 0", port
);
6436 bpf_error("illegal port number %d > 65535", port
);
6437 b
= gen_port(port
, real_proto
, dir
);
6438 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6442 if (proto
!= Q_DEFAULT
&&
6443 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6444 bpf_error("illegal qualifier of 'portrange'");
6445 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6446 bpf_error("unknown port in range '%s'", name
);
6447 if (proto
== Q_UDP
) {
6448 if (real_proto
== IPPROTO_TCP
)
6449 bpf_error("port in range '%s' is tcp", name
);
6450 else if (real_proto
== IPPROTO_SCTP
)
6451 bpf_error("port in range '%s' is sctp", name
);
6453 /* override PROTO_UNDEF */
6454 real_proto
= IPPROTO_UDP
;
6456 if (proto
== Q_TCP
) {
6457 if (real_proto
== IPPROTO_UDP
)
6458 bpf_error("port in range '%s' is udp", name
);
6459 else if (real_proto
== IPPROTO_SCTP
)
6460 bpf_error("port in range '%s' is sctp", name
);
6462 /* override PROTO_UNDEF */
6463 real_proto
= IPPROTO_TCP
;
6465 if (proto
== Q_SCTP
) {
6466 if (real_proto
== IPPROTO_UDP
)
6467 bpf_error("port in range '%s' is udp", name
);
6468 else if (real_proto
== IPPROTO_TCP
)
6469 bpf_error("port in range '%s' is tcp", name
);
6471 /* override PROTO_UNDEF */
6472 real_proto
= IPPROTO_SCTP
;
6475 bpf_error("illegal port number %d < 0", port1
);
6477 bpf_error("illegal port number %d > 65535", port1
);
6479 bpf_error("illegal port number %d < 0", port2
);
6481 bpf_error("illegal port number %d > 65535", port2
);
6483 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6484 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6489 eaddr
= pcap_ether_hostton(name
);
6491 bpf_error("unknown ether host: %s", name
);
6493 alist
= pcap_nametoaddr(name
);
6494 if (alist
== NULL
|| *alist
== NULL
)
6495 bpf_error("unknown host '%s'", name
);
6496 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6500 bpf_error("'gateway' not supported in this configuration");
6504 real_proto
= lookup_proto(name
, proto
);
6505 if (real_proto
>= 0)
6506 return gen_proto(real_proto
, proto
, dir
);
6508 bpf_error("unknown protocol: %s", name
);
6511 real_proto
= lookup_proto(name
, proto
);
6512 if (real_proto
>= 0)
6513 return gen_protochain(real_proto
, proto
, dir
);
6515 bpf_error("unknown protocol: %s", name
);
6526 gen_mcode(s1
, s2
, masklen
, q
)
6527 register const char *s1
, *s2
;
6528 register unsigned int masklen
;
6531 register int nlen
, mlen
;
6534 nlen
= __pcap_atoin(s1
, &n
);
6535 /* Promote short ipaddr */
6539 mlen
= __pcap_atoin(s2
, &m
);
6540 /* Promote short ipaddr */
6543 bpf_error("non-network bits set in \"%s mask %s\"",
6546 /* Convert mask len to mask */
6548 bpf_error("mask length must be <= 32");
6551 * X << 32 is not guaranteed by C to be 0; it's
6556 m
= 0xffffffff << (32 - masklen
);
6558 bpf_error("non-network bits set in \"%s/%d\"",
6565 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6568 bpf_error("Mask syntax for networks only");
6577 register const char *s
;
6582 int proto
= q
.proto
;
6588 else if (q
.proto
== Q_DECNET
)
6589 vlen
= __pcap_atodn(s
, &v
);
6591 vlen
= __pcap_atoin(s
, &v
);
6598 if (proto
== Q_DECNET
)
6599 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6600 else if (proto
== Q_LINK
) {
6601 bpf_error("illegal link layer address");
6604 if (s
== NULL
&& q
.addr
== Q_NET
) {
6605 /* Promote short net number */
6606 while (v
&& (v
& 0xff000000) == 0) {
6611 /* Promote short ipaddr */
6615 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6620 proto
= IPPROTO_UDP
;
6621 else if (proto
== Q_TCP
)
6622 proto
= IPPROTO_TCP
;
6623 else if (proto
== Q_SCTP
)
6624 proto
= IPPROTO_SCTP
;
6625 else if (proto
== Q_DEFAULT
)
6626 proto
= PROTO_UNDEF
;
6628 bpf_error("illegal qualifier of 'port'");
6631 bpf_error("illegal port number %u > 65535", v
);
6635 b
= gen_port((int)v
, proto
, dir
);
6636 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6642 proto
= IPPROTO_UDP
;
6643 else if (proto
== Q_TCP
)
6644 proto
= IPPROTO_TCP
;
6645 else if (proto
== Q_SCTP
)
6646 proto
= IPPROTO_SCTP
;
6647 else if (proto
== Q_DEFAULT
)
6648 proto
= PROTO_UNDEF
;
6650 bpf_error("illegal qualifier of 'portrange'");
6653 bpf_error("illegal port number %u > 65535", v
);
6657 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6658 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6663 bpf_error("'gateway' requires a name");
6667 return gen_proto((int)v
, proto
, dir
);
6670 return gen_protochain((int)v
, proto
, dir
);
6685 gen_mcode6(s1
, s2
, masklen
, q
)
6686 register const char *s1
, *s2
;
6687 register unsigned int masklen
;
6690 struct addrinfo
*res
;
6691 struct in6_addr
*addr
;
6692 struct in6_addr mask
;
6697 bpf_error("no mask %s supported", s2
);
6699 res
= pcap_nametoaddrinfo(s1
);
6701 bpf_error("invalid ip6 address %s", s1
);
6704 bpf_error("%s resolved to multiple address", s1
);
6705 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6707 if (sizeof(mask
) * 8 < masklen
)
6708 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6709 memset(&mask
, 0, sizeof(mask
));
6710 memset(&mask
, 0xff, masklen
/ 8);
6712 mask
.s6_addr
[masklen
/ 8] =
6713 (0xff << (8 - masklen
% 8)) & 0xff;
6716 a
= (u_int32_t
*)addr
;
6717 m
= (u_int32_t
*)&mask
;
6718 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6719 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6720 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6728 bpf_error("Mask syntax for networks only");
6732 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6738 bpf_error("invalid qualifier against IPv6 address");
6747 register const u_char
*eaddr
;
6750 struct block
*b
, *tmp
;
6752 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6755 case DLT_NETANALYZER
:
6756 case DLT_NETANALYZER_TRANSPARENT
:
6757 return gen_ehostop(eaddr
, (int)q
.dir
);
6759 return gen_fhostop(eaddr
, (int)q
.dir
);
6761 return gen_thostop(eaddr
, (int)q
.dir
);
6762 case DLT_IEEE802_11
:
6763 case DLT_PRISM_HEADER
:
6764 case DLT_IEEE802_11_RADIO_AVS
:
6765 case DLT_IEEE802_11_RADIO
:
6767 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6771 * Check that the packet doesn't begin with an
6772 * LE Control marker. (We've already generated
6775 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6780 * Now check the MAC address.
6782 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6787 case DLT_IP_OVER_FC
:
6788 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6790 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6794 bpf_error("ethernet address used in non-ether expression");
6801 struct slist
*s0
, *s1
;
6804 * This is definitely not the best way to do this, but the
6805 * lists will rarely get long.
6812 static struct slist
*
6818 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6823 static struct slist
*
6829 s
= new_stmt(BPF_LD
|BPF_MEM
);
6835 * Modify "index" to use the value stored into its register as an
6836 * offset relative to the beginning of the header for the protocol
6837 * "proto", and allocate a register and put an item "size" bytes long
6838 * (1, 2, or 4) at that offset into that register, making it the register
6842 gen_load(proto
, inst
, size
)
6847 struct slist
*s
, *tmp
;
6849 int regno
= alloc_reg();
6851 free_reg(inst
->regno
);
6855 bpf_error("data size must be 1, 2, or 4");
6871 bpf_error("unsupported index operation");
6875 * The offset is relative to the beginning of the packet
6876 * data, if we have a radio header. (If we don't, this
6879 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6880 linktype
!= DLT_IEEE802_11_RADIO
&&
6881 linktype
!= DLT_PRISM_HEADER
)
6882 bpf_error("radio information not present in capture");
6885 * Load into the X register the offset computed into the
6886 * register specified by "index".
6888 s
= xfer_to_x(inst
);
6891 * Load the item at that offset.
6893 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6895 sappend(inst
->s
, s
);
6900 * The offset is relative to the beginning of
6901 * the link-layer header.
6903 * XXX - what about ATM LANE? Should the index be
6904 * relative to the beginning of the AAL5 frame, so
6905 * that 0 refers to the beginning of the LE Control
6906 * field, or relative to the beginning of the LAN
6907 * frame, so that 0 refers, for Ethernet LANE, to
6908 * the beginning of the destination address?
6910 s
= gen_llprefixlen();
6913 * If "s" is non-null, it has code to arrange that the
6914 * X register contains the length of the prefix preceding
6915 * the link-layer header. Add to it the offset computed
6916 * into the register specified by "index", and move that
6917 * into the X register. Otherwise, just load into the X
6918 * register the offset computed into the register specified
6922 sappend(s
, xfer_to_a(inst
));
6923 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6924 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6926 s
= xfer_to_x(inst
);
6929 * Load the item at the sum of the offset we've put in the
6930 * X register and the offset of the start of the link
6931 * layer header (which is 0 if the radio header is
6932 * variable-length; that header length is what we put
6933 * into the X register and then added to the index).
6935 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6938 sappend(inst
->s
, s
);
6952 * The offset is relative to the beginning of
6953 * the network-layer header.
6954 * XXX - are there any cases where we want
6957 s
= gen_off_macpl();
6960 * If "s" is non-null, it has code to arrange that the
6961 * X register contains the offset of the MAC-layer
6962 * payload. Add to it the offset computed into the
6963 * register specified by "index", and move that into
6964 * the X register. Otherwise, just load into the X
6965 * register the offset computed into the register specified
6969 sappend(s
, xfer_to_a(inst
));
6970 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6971 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6973 s
= xfer_to_x(inst
);
6976 * Load the item at the sum of the offset we've put in the
6977 * X register, the offset of the start of the network
6978 * layer header from the beginning of the MAC-layer
6979 * payload, and the purported offset of the start of the
6980 * MAC-layer payload (which might be 0 if there's a
6981 * variable-length prefix before the link-layer header
6982 * or the link-layer header itself is variable-length;
6983 * the variable-length offset of the start of the
6984 * MAC-layer payload is what we put into the X register
6985 * and then added to the index).
6987 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6988 tmp
->s
.k
= off_macpl
+ off_nl
;
6990 sappend(inst
->s
, s
);
6993 * Do the computation only if the packet contains
6994 * the protocol in question.
6996 b
= gen_proto_abbrev(proto
);
6998 gen_and(inst
->b
, b
);
7012 * The offset is relative to the beginning of
7013 * the transport-layer header.
7015 * Load the X register with the length of the IPv4 header
7016 * (plus the offset of the link-layer header, if it's
7017 * a variable-length header), in bytes.
7019 * XXX - are there any cases where we want
7021 * XXX - we should, if we're built with
7022 * IPv6 support, generate code to load either
7023 * IPv4, IPv6, or both, as appropriate.
7025 s
= gen_loadx_iphdrlen();
7028 * The X register now contains the sum of the length
7029 * of any variable-length header preceding the link-layer
7030 * header, any variable-length link-layer header, and the
7031 * length of the network-layer header.
7033 * Load into the A register the offset relative to
7034 * the beginning of the transport layer header,
7035 * add the X register to that, move that to the
7036 * X register, and load with an offset from the
7037 * X register equal to the offset of the network
7038 * layer header relative to the beginning of
7039 * the MAC-layer payload plus the fixed-length
7040 * portion of the offset of the MAC-layer payload
7041 * from the beginning of the raw packet data.
7043 sappend(s
, xfer_to_a(inst
));
7044 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7045 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7046 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7047 tmp
->s
.k
= off_macpl
+ off_nl
;
7048 sappend(inst
->s
, s
);
7051 * Do the computation only if the packet contains
7052 * the protocol in question - which is true only
7053 * if this is an IP datagram and is the first or
7054 * only fragment of that datagram.
7056 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7058 gen_and(inst
->b
, b
);
7059 gen_and(gen_proto_abbrev(Q_IP
), b
);
7063 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7066 inst
->regno
= regno
;
7067 s
= new_stmt(BPF_ST
);
7069 sappend(inst
->s
, s
);
7075 gen_relation(code
, a0
, a1
, reversed
)
7077 struct arth
*a0
, *a1
;
7080 struct slist
*s0
, *s1
, *s2
;
7081 struct block
*b
, *tmp
;
7085 if (code
== BPF_JEQ
) {
7086 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7087 b
= new_block(JMP(code
));
7091 b
= new_block(BPF_JMP
|code
|BPF_X
);
7097 sappend(a0
->s
, a1
->s
);
7101 free_reg(a0
->regno
);
7102 free_reg(a1
->regno
);
7104 /* 'and' together protocol checks */
7107 gen_and(a0
->b
, tmp
= a1
->b
);
7123 int regno
= alloc_reg();
7124 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7127 s
= new_stmt(BPF_LD
|BPF_LEN
);
7128 s
->next
= new_stmt(BPF_ST
);
7129 s
->next
->s
.k
= regno
;
7144 a
= (struct arth
*)newchunk(sizeof(*a
));
7148 s
= new_stmt(BPF_LD
|BPF_IMM
);
7150 s
->next
= new_stmt(BPF_ST
);
7166 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7169 s
= new_stmt(BPF_ST
);
7177 gen_arth(code
, a0
, a1
)
7179 struct arth
*a0
, *a1
;
7181 struct slist
*s0
, *s1
, *s2
;
7185 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7190 sappend(a0
->s
, a1
->s
);
7192 free_reg(a0
->regno
);
7193 free_reg(a1
->regno
);
7195 s0
= new_stmt(BPF_ST
);
7196 a0
->regno
= s0
->s
.k
= alloc_reg();
7203 * Here we handle simple allocation of the scratch registers.
7204 * If too many registers are alloc'd, the allocator punts.
7206 static int regused
[BPF_MEMWORDS
];
7210 * Initialize the table of used registers and the current register.
7216 memset(regused
, 0, sizeof regused
);
7220 * Return the next free register.
7225 int n
= BPF_MEMWORDS
;
7228 if (regused
[curreg
])
7229 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7231 regused
[curreg
] = 1;
7235 bpf_error("too many registers needed to evaluate expression");
7241 * Return a register to the table so it can
7251 static struct block
*
7258 s
= new_stmt(BPF_LD
|BPF_LEN
);
7259 b
= new_block(JMP(jmp
));
7270 return gen_len(BPF_JGE
, n
);
7274 * Actually, this is less than or equal.
7282 b
= gen_len(BPF_JGT
, n
);
7289 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7290 * the beginning of the link-layer header.
7291 * XXX - that means you can't test values in the radiotap header, but
7292 * as that header is difficult if not impossible to parse generally
7293 * without a loop, that might not be a severe problem. A new keyword
7294 * "radio" could be added for that, although what you'd really want
7295 * would be a way of testing particular radio header values, which
7296 * would generate code appropriate to the radio header in question.
7299 gen_byteop(op
, idx
, val
)
7310 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7313 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7317 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7321 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7325 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7329 b
= new_block(JMP(BPF_JEQ
));
7336 static u_char abroadcast
[] = { 0x0 };
7339 gen_broadcast(proto
)
7342 bpf_u_int32 hostmask
;
7343 struct block
*b0
, *b1
, *b2
;
7344 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7352 case DLT_ARCNET_LINUX
:
7353 return gen_ahostop(abroadcast
, Q_DST
);
7355 case DLT_NETANALYZER
:
7356 case DLT_NETANALYZER_TRANSPARENT
:
7357 return gen_ehostop(ebroadcast
, Q_DST
);
7359 return gen_fhostop(ebroadcast
, Q_DST
);
7361 return gen_thostop(ebroadcast
, Q_DST
);
7362 case DLT_IEEE802_11
:
7363 case DLT_PRISM_HEADER
:
7364 case DLT_IEEE802_11_RADIO_AVS
:
7365 case DLT_IEEE802_11_RADIO
:
7367 return gen_wlanhostop(ebroadcast
, Q_DST
);
7368 case DLT_IP_OVER_FC
:
7369 return gen_ipfchostop(ebroadcast
, Q_DST
);
7373 * Check that the packet doesn't begin with an
7374 * LE Control marker. (We've already generated
7377 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7382 * Now check the MAC address.
7384 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7390 bpf_error("not a broadcast link");
7396 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7397 * as an indication that we don't know the netmask, and fail
7400 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7401 bpf_error("netmask not known, so 'ip broadcast' not supported");
7402 b0
= gen_linktype(ETHERTYPE_IP
);
7403 hostmask
= ~netmask
;
7404 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7405 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7406 (bpf_int32
)(~0 & hostmask
), hostmask
);
7411 bpf_error("only link-layer/IP broadcast filters supported");
7417 * Generate code to test the low-order bit of a MAC address (that's
7418 * the bottom bit of the *first* byte).
7420 static struct block
*
7421 gen_mac_multicast(offset
)
7424 register struct block
*b0
;
7425 register struct slist
*s
;
7427 /* link[offset] & 1 != 0 */
7428 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7429 b0
= new_block(JMP(BPF_JSET
));
7436 gen_multicast(proto
)
7439 register struct block
*b0
, *b1
, *b2
;
7440 register struct slist
*s
;
7448 case DLT_ARCNET_LINUX
:
7449 /* all ARCnet multicasts use the same address */
7450 return gen_ahostop(abroadcast
, Q_DST
);
7452 case DLT_NETANALYZER
:
7453 case DLT_NETANALYZER_TRANSPARENT
:
7454 /* ether[0] & 1 != 0 */
7455 return gen_mac_multicast(0);
7458 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7460 * XXX - was that referring to bit-order issues?
7462 /* fddi[1] & 1 != 0 */
7463 return gen_mac_multicast(1);
7465 /* tr[2] & 1 != 0 */
7466 return gen_mac_multicast(2);
7467 case DLT_IEEE802_11
:
7468 case DLT_PRISM_HEADER
:
7469 case DLT_IEEE802_11_RADIO_AVS
:
7470 case DLT_IEEE802_11_RADIO
:
7475 * For control frames, there is no DA.
7477 * For management frames, DA is at an
7478 * offset of 4 from the beginning of
7481 * For data frames, DA is at an offset
7482 * of 4 from the beginning of the packet
7483 * if To DS is clear and at an offset of
7484 * 16 from the beginning of the packet
7489 * Generate the tests to be done for data frames.
7491 * First, check for To DS set, i.e. "link[1] & 0x01".
7493 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7494 b1
= new_block(JMP(BPF_JSET
));
7495 b1
->s
.k
= 0x01; /* To DS */
7499 * If To DS is set, the DA is at 16.
7501 b0
= gen_mac_multicast(16);
7505 * Now, check for To DS not set, i.e. check
7506 * "!(link[1] & 0x01)".
7508 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7509 b2
= new_block(JMP(BPF_JSET
));
7510 b2
->s
.k
= 0x01; /* To DS */
7515 * If To DS is not set, the DA is at 4.
7517 b1
= gen_mac_multicast(4);
7521 * Now OR together the last two checks. That gives
7522 * the complete set of checks for data frames.
7527 * Now check for a data frame.
7528 * I.e, check "link[0] & 0x08".
7530 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7531 b1
= new_block(JMP(BPF_JSET
));
7536 * AND that with the checks done for data frames.
7541 * If the high-order bit of the type value is 0, this
7542 * is a management frame.
7543 * I.e, check "!(link[0] & 0x08)".
7545 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7546 b2
= new_block(JMP(BPF_JSET
));
7552 * For management frames, the DA is at 4.
7554 b1
= gen_mac_multicast(4);
7558 * OR that with the checks done for data frames.
7559 * That gives the checks done for management and
7565 * If the low-order bit of the type value is 1,
7566 * this is either a control frame or a frame
7567 * with a reserved type, and thus not a
7570 * I.e., check "!(link[0] & 0x04)".
7572 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7573 b1
= new_block(JMP(BPF_JSET
));
7579 * AND that with the checks for data and management
7584 case DLT_IP_OVER_FC
:
7585 b0
= gen_mac_multicast(2);
7590 * Check that the packet doesn't begin with an
7591 * LE Control marker. (We've already generated
7594 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7598 /* ether[off_mac] & 1 != 0 */
7599 b0
= gen_mac_multicast(off_mac
);
7607 /* Link not known to support multicasts */
7611 b0
= gen_linktype(ETHERTYPE_IP
);
7612 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7617 b0
= gen_linktype(ETHERTYPE_IPV6
);
7618 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7622 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7628 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7629 * Outbound traffic is sent by this machine, while inbound traffic is
7630 * sent by a remote machine (and may include packets destined for a
7631 * unicast or multicast link-layer address we are not subscribing to).
7632 * These are the same definitions implemented by pcap_setdirection().
7633 * Capturing only unicast traffic destined for this host is probably
7634 * better accomplished using a higher-layer filter.
7640 register struct block
*b0
;
7643 * Only some data link types support inbound/outbound qualifiers.
7647 b0
= gen_relation(BPF_JEQ
,
7648 gen_load(Q_LINK
, gen_loadi(0), 1),
7655 /* match outgoing packets */
7656 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7658 /* match incoming packets */
7659 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7664 /* match outgoing packets */
7665 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7667 /* to filter on inbound traffic, invert the match */
7672 #ifdef HAVE_NET_PFVAR_H
7674 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7675 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7681 /* match outgoing packets */
7682 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7684 /* match incoming packets */
7685 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7689 case DLT_JUNIPER_MFR
:
7690 case DLT_JUNIPER_MLFR
:
7691 case DLT_JUNIPER_MLPPP
:
7692 case DLT_JUNIPER_ATM1
:
7693 case DLT_JUNIPER_ATM2
:
7694 case DLT_JUNIPER_PPPOE
:
7695 case DLT_JUNIPER_PPPOE_ATM
:
7696 case DLT_JUNIPER_GGSN
:
7697 case DLT_JUNIPER_ES
:
7698 case DLT_JUNIPER_MONITOR
:
7699 case DLT_JUNIPER_SERVICES
:
7700 case DLT_JUNIPER_ETHER
:
7701 case DLT_JUNIPER_PPP
:
7702 case DLT_JUNIPER_FRELAY
:
7703 case DLT_JUNIPER_CHDLC
:
7704 case DLT_JUNIPER_VP
:
7705 case DLT_JUNIPER_ST
:
7706 case DLT_JUNIPER_ISM
:
7707 case DLT_JUNIPER_VS
:
7708 case DLT_JUNIPER_SRX_E2E
:
7709 case DLT_JUNIPER_FIBRECHANNEL
:
7710 case DLT_JUNIPER_ATM_CEMIC
:
7712 /* juniper flags (including direction) are stored
7713 * the byte after the 3-byte magic number */
7715 /* match outgoing packets */
7716 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7718 /* match incoming packets */
7719 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7725 * If we have packet meta-data indicating a direction,
7726 * check it, otherwise give up as this link-layer type
7727 * has nothing in the packet data.
7729 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7731 * This is Linux with PF_PACKET support.
7732 * If this is a *live* capture, we can look at
7733 * special meta-data in the filter expression;
7734 * if it's a savefile, we can't.
7736 if (bpf_pcap
->rfile
!= NULL
) {
7737 /* We have a FILE *, so this is a savefile */
7738 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7743 /* match outgoing packets */
7744 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7747 /* to filter on inbound traffic, invert the match */
7750 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7751 bpf_error("inbound/outbound not supported on linktype %d",
7755 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7760 #ifdef HAVE_NET_PFVAR_H
7761 /* PF firewall log matched interface */
7763 gen_pf_ifname(const char *ifname
)
7768 if (linktype
!= DLT_PFLOG
) {
7769 bpf_error("ifname supported only on PF linktype");
7772 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7773 off
= offsetof(struct pfloghdr
, ifname
);
7774 if (strlen(ifname
) >= len
) {
7775 bpf_error("ifname interface names can only be %d characters",
7779 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7783 /* PF firewall log ruleset name */
7785 gen_pf_ruleset(char *ruleset
)
7789 if (linktype
!= DLT_PFLOG
) {
7790 bpf_error("ruleset supported only on PF linktype");
7794 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7795 bpf_error("ruleset names can only be %ld characters",
7796 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7800 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7801 strlen(ruleset
), (const u_char
*)ruleset
);
7805 /* PF firewall log rule number */
7811 if (linktype
!= DLT_PFLOG
) {
7812 bpf_error("rnr supported only on PF linktype");
7816 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7821 /* PF firewall log sub-rule number */
7823 gen_pf_srnr(int srnr
)
7827 if (linktype
!= DLT_PFLOG
) {
7828 bpf_error("srnr supported only on PF linktype");
7832 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7837 /* PF firewall log reason code */
7839 gen_pf_reason(int reason
)
7843 if (linktype
!= DLT_PFLOG
) {
7844 bpf_error("reason supported only on PF linktype");
7848 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7853 /* PF firewall log action */
7855 gen_pf_action(int action
)
7859 if (linktype
!= DLT_PFLOG
) {
7860 bpf_error("action supported only on PF linktype");
7864 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7868 #else /* !HAVE_NET_PFVAR_H */
7870 gen_pf_ifname(const char *ifname
)
7872 bpf_error("libpcap was compiled without pf support");
7878 gen_pf_ruleset(char *ruleset
)
7880 bpf_error("libpcap was compiled on a machine without pf support");
7888 bpf_error("libpcap was compiled on a machine without pf support");
7894 gen_pf_srnr(int srnr
)
7896 bpf_error("libpcap was compiled on a machine without pf support");
7902 gen_pf_reason(int reason
)
7904 bpf_error("libpcap was compiled on a machine without pf support");
7910 gen_pf_action(int action
)
7912 bpf_error("libpcap was compiled on a machine without pf support");
7916 #endif /* HAVE_NET_PFVAR_H */
7918 /* IEEE 802.11 wireless header */
7920 gen_p80211_type(int type
, int mask
)
7926 case DLT_IEEE802_11
:
7927 case DLT_PRISM_HEADER
:
7928 case DLT_IEEE802_11_RADIO_AVS
:
7929 case DLT_IEEE802_11_RADIO
:
7930 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7935 bpf_error("802.11 link-layer types supported only on 802.11");
7943 gen_p80211_fcdir(int fcdir
)
7949 case DLT_IEEE802_11
:
7950 case DLT_PRISM_HEADER
:
7951 case DLT_IEEE802_11_RADIO_AVS
:
7952 case DLT_IEEE802_11_RADIO
:
7956 bpf_error("frame direction supported only with 802.11 headers");
7960 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7961 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7968 register const u_char
*eaddr
;
7974 case DLT_ARCNET_LINUX
:
7975 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7977 return (gen_ahostop(eaddr
, (int)q
.dir
));
7979 bpf_error("ARCnet address used in non-arc expression");
7985 bpf_error("aid supported only on ARCnet");
7988 bpf_error("ARCnet address used in non-arc expression");
7993 static struct block
*
7994 gen_ahostop(eaddr
, dir
)
7995 register const u_char
*eaddr
;
7998 register struct block
*b0
, *b1
;
8001 /* src comes first, different from Ethernet */
8003 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
8006 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
8009 b0
= gen_ahostop(eaddr
, Q_SRC
);
8010 b1
= gen_ahostop(eaddr
, Q_DST
);
8016 b0
= gen_ahostop(eaddr
, Q_SRC
);
8017 b1
= gen_ahostop(eaddr
, Q_DST
);
8022 bpf_error("'addr1' is only supported on 802.11");
8026 bpf_error("'addr2' is only supported on 802.11");
8030 bpf_error("'addr3' is only supported on 802.11");
8034 bpf_error("'addr4' is only supported on 802.11");
8038 bpf_error("'ra' is only supported on 802.11");
8042 bpf_error("'ta' is only supported on 802.11");
8049 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8050 static struct block
*
8051 gen_vlan_bpf_extensions(int vlan_num
)
8053 struct block
*b0
, *b1
;
8056 /* generate new filter code based on extracting packet
8058 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8059 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8061 b0
= new_block(JMP(BPF_JEQ
));
8065 if (vlan_num
>= 0) {
8066 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8067 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8069 b1
= new_block(JMP(BPF_JEQ
));
8071 b1
->s
.k
= (bpf_int32
) vlan_num
;
8081 static struct block
*
8082 gen_vlan_no_bpf_extensions(int vlan_num
)
8084 struct block
*b0
, *b1
;
8086 /* check for VLAN, including QinQ */
8087 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8088 (bpf_int32
)ETHERTYPE_8021Q
);
8089 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8090 (bpf_int32
)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);
8109 * support IEEE 802.1Q VLAN trunk over ethernet
8117 /* can't check for VLAN-encapsulated packets inside MPLS */
8118 if (label_stack_depth
> 0)
8119 bpf_error("no VLAN match after MPLS");
8122 * Check for a VLAN packet, and then change the offsets to point
8123 * to the type and data fields within the VLAN packet. Just
8124 * increment the offsets, so that we can support a hierarchy, e.g.
8125 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8128 * XXX - this is a bit of a kludge. If we were to split the
8129 * compiler into a parser that parses an expression and
8130 * generates an expression tree, and a code generator that
8131 * takes an expression tree (which could come from our
8132 * parser or from some other parser) and generates BPF code,
8133 * we could perhaps make the offsets parameters of routines
8134 * and, in the handler for an "AND" node, pass to subnodes
8135 * other than the VLAN node the adjusted offsets.
8137 * This would mean that "vlan" would, instead of changing the
8138 * behavior of *all* tests after it, change only the behavior
8139 * of tests ANDed with it. That would change the documented
8140 * semantics of "vlan", which might break some expressions.
8141 * However, it would mean that "(vlan and ip) or ip" would check
8142 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8143 * checking only for VLAN-encapsulated IP, so that could still
8144 * be considered worth doing; it wouldn't break expressions
8145 * that are of the form "vlan and ..." or "vlan N and ...",
8146 * which I suspect are the most common expressions involving
8147 * "vlan". "vlan or ..." doesn't necessarily do what the user
8148 * would really want, now, as all the "or ..." tests would
8149 * be done assuming a VLAN, even though the "or" could be viewed
8150 * as meaning "or, if this isn't a VLAN packet...".
8157 case DLT_NETANALYZER
:
8158 case DLT_NETANALYZER_TRANSPARENT
:
8159 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8160 if (vlan_stack_depth
== 0) {
8162 * Do we need special VLAN handling?
8164 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8165 b0
= gen_vlan_bpf_extensions(vlan_num
);
8167 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8170 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8173 bpf_error("no VLAN support for data link type %d",
8190 struct block
*b0
, *b1
;
8193 * Change the offsets to point to the type and data fields within
8194 * the MPLS packet. Just increment the offsets, so that we
8195 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8196 * capture packets with an outer label of 100000 and an inner
8199 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8203 if (label_stack_depth
> 0) {
8204 /* just match the bottom-of-stack bit clear */
8205 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
8208 * Indicate that we're checking MPLS-encapsulated headers,
8209 * to make sure higher level code generators don't try to
8210 * match against IP-related protocols such as Q_ARP, Q_RARP
8215 case DLT_C_HDLC
: /* fall through */
8217 case DLT_NETANALYZER
:
8218 case DLT_NETANALYZER_TRANSPARENT
:
8219 b0
= gen_linktype(ETHERTYPE_MPLS
);
8223 b0
= gen_linktype(PPP_MPLS_UCAST
);
8226 /* FIXME add other DLT_s ...
8227 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8228 * leave it for now */
8231 bpf_error("no MPLS support for data link type %d",
8239 /* If a specific MPLS label is requested, check it */
8240 if (label_num
>= 0) {
8241 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8242 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8243 0xfffff000); /* only compare the first 20 bits */
8250 label_stack_depth
++;
8255 * Support PPPOE discovery and session.
8260 /* check for PPPoE discovery */
8261 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8265 gen_pppoes(sess_num
)
8268 struct block
*b0
, *b1
;
8271 * Test against the PPPoE session link-layer type.
8273 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8276 * Change the offsets to point to the type and data fields within
8277 * the PPP packet, and note that this is PPPoE rather than
8280 * XXX - this is a bit of a kludge. If we were to split the
8281 * compiler into a parser that parses an expression and
8282 * generates an expression tree, and a code generator that
8283 * takes an expression tree (which could come from our
8284 * parser or from some other parser) and generates BPF code,
8285 * we could perhaps make the offsets parameters of routines
8286 * and, in the handler for an "AND" node, pass to subnodes
8287 * other than the PPPoE node the adjusted offsets.
8289 * This would mean that "pppoes" would, instead of changing the
8290 * behavior of *all* tests after it, change only the behavior
8291 * of tests ANDed with it. That would change the documented
8292 * semantics of "pppoes", which might break some expressions.
8293 * However, it would mean that "(pppoes and ip) or ip" would check
8294 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8295 * checking only for VLAN-encapsulated IP, so that could still
8296 * be considered worth doing; it wouldn't break expressions
8297 * that are of the form "pppoes and ..." which I suspect are the
8298 * most common expressions involving "pppoes". "pppoes or ..."
8299 * doesn't necessarily do what the user would really want, now,
8300 * as all the "or ..." tests would be done assuming PPPoE, even
8301 * though the "or" could be viewed as meaning "or, if this isn't
8302 * a PPPoE packet...".
8304 orig_linktype
= off_linktype
; /* save original values */
8308 /* If a specific session is requested, check PPPoE session id */
8309 if (sess_num
>= 0) {
8310 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
,
8311 (bpf_int32
)sess_num
, 0x0000ffff);
8317 * The "network-layer" protocol is PPPoE, which has a 6-byte
8318 * PPPoE header, followed by a PPP packet.
8320 * There is no HDLC encapsulation for the PPP packet (it's
8321 * encapsulated in PPPoES instead), so the link-layer type
8322 * starts at the first byte of the PPP packet. For PPPoE,
8323 * that offset is relative to the beginning of the total
8324 * link-layer payload, including any 802.2 LLC header, so
8325 * it's 6 bytes past off_nl.
8327 off_linktype
= off_nl
+ 6;
8330 * The network-layer offsets are relative to the beginning
8331 * of the MAC-layer payload; that's past the 6-byte
8332 * PPPoE header and the 2-byte PPP header.
8335 off_nl_nosnap
= 6+2;
8341 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8353 bpf_error("'vpi' supported only on raw ATM");
8354 if (off_vpi
== (u_int
)-1)
8356 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8362 bpf_error("'vci' supported only on raw ATM");
8363 if (off_vci
== (u_int
)-1)
8365 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8370 if (off_proto
== (u_int
)-1)
8371 abort(); /* XXX - this isn't on FreeBSD */
8372 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8377 if (off_payload
== (u_int
)-1)
8379 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8380 0xffffffff, jtype
, reverse
, jvalue
);
8385 bpf_error("'callref' supported only on raw ATM");
8386 if (off_proto
== (u_int
)-1)
8388 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8389 jtype
, reverse
, jvalue
);
8399 gen_atmtype_abbrev(type
)
8402 struct block
*b0
, *b1
;
8407 /* Get all packets in Meta signalling Circuit */
8409 bpf_error("'metac' supported only on raw ATM");
8410 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8411 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8416 /* Get all packets in Broadcast Circuit*/
8418 bpf_error("'bcc' supported only on raw ATM");
8419 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8420 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8425 /* Get all cells in Segment OAM F4 circuit*/
8427 bpf_error("'oam4sc' supported only on raw ATM");
8428 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8429 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8434 /* Get all cells in End-to-End OAM F4 Circuit*/
8436 bpf_error("'oam4ec' supported only on raw ATM");
8437 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8438 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8443 /* Get all packets in connection Signalling Circuit */
8445 bpf_error("'sc' supported only on raw ATM");
8446 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8447 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8452 /* Get all packets in ILMI Circuit */
8454 bpf_error("'ilmic' supported only on raw ATM");
8455 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8456 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8461 /* Get all LANE packets */
8463 bpf_error("'lane' supported only on raw ATM");
8464 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8467 * Arrange that all subsequent tests assume LANE
8468 * rather than LLC-encapsulated packets, and set
8469 * the offsets appropriately for LANE-encapsulated
8472 * "off_mac" is the offset of the Ethernet header,
8473 * which is 2 bytes past the ATM pseudo-header
8474 * (skipping the pseudo-header and 2-byte LE Client
8475 * field). The other offsets are Ethernet offsets
8476 * relative to "off_mac".
8479 off_mac
= off_payload
+ 2; /* MAC header */
8480 off_linktype
= off_mac
+ 12;
8481 off_macpl
= off_mac
+ 14; /* Ethernet */
8482 off_nl
= 0; /* Ethernet II */
8483 off_nl_nosnap
= 3; /* 802.3+802.2 */
8487 /* Get all LLC-encapsulated packets */
8489 bpf_error("'llc' supported only on raw ATM");
8490 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8501 * Filtering for MTP2 messages based on li value
8502 * FISU, length is null
8503 * LSSU, length is 1 or 2
8504 * MSU, length is 3 or more
8505 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8508 gen_mtp2type_abbrev(type
)
8511 struct block
*b0
, *b1
;
8516 if ( (linktype
!= DLT_MTP2
) &&
8517 (linktype
!= DLT_ERF
) &&
8518 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8519 bpf_error("'fisu' supported only on MTP2");
8520 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8521 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8525 if ( (linktype
!= DLT_MTP2
) &&
8526 (linktype
!= DLT_ERF
) &&
8527 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8528 bpf_error("'lssu' supported only on MTP2");
8529 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8530 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8535 if ( (linktype
!= DLT_MTP2
) &&
8536 (linktype
!= DLT_ERF
) &&
8537 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8538 bpf_error("'msu' supported only on MTP2");
8539 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8543 if ( (linktype
!= DLT_MTP2
) &&
8544 (linktype
!= DLT_ERF
) &&
8545 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8546 bpf_error("'hfisu' supported only on MTP2_HSL");
8547 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8548 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8552 if ( (linktype
!= DLT_MTP2
) &&
8553 (linktype
!= DLT_ERF
) &&
8554 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8555 bpf_error("'hlssu' supported only on MTP2_HSL");
8556 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8557 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8562 if ( (linktype
!= DLT_MTP2
) &&
8563 (linktype
!= DLT_ERF
) &&
8564 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8565 bpf_error("'hmsu' supported only on MTP2_HSL");
8566 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8576 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8583 bpf_u_int32 val1
, val2
, val3
;
8584 u_int newoff_sio
=off_sio
;
8585 u_int newoff_opc
=off_opc
;
8586 u_int newoff_dpc
=off_dpc
;
8587 u_int newoff_sls
=off_sls
;
8589 switch (mtp3field
) {
8592 newoff_sio
+= 3; /* offset for MTP2_HSL */
8596 if (off_sio
== (u_int
)-1)
8597 bpf_error("'sio' supported only on SS7");
8598 /* sio coded on 1 byte so max value 255 */
8600 bpf_error("sio value %u too big; max value = 255",
8602 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8603 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8609 if (off_opc
== (u_int
)-1)
8610 bpf_error("'opc' supported only on SS7");
8611 /* opc coded on 14 bits so max value 16383 */
8613 bpf_error("opc value %u too big; max value = 16383",
8615 /* the following instructions are made to convert jvalue
8616 * to the form used to write opc in an ss7 message*/
8617 val1
= jvalue
& 0x00003c00;
8619 val2
= jvalue
& 0x000003fc;
8621 val3
= jvalue
& 0x00000003;
8623 jvalue
= val1
+ val2
+ val3
;
8624 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8625 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8633 if (off_dpc
== (u_int
)-1)
8634 bpf_error("'dpc' supported only on SS7");
8635 /* dpc coded on 14 bits so max value 16383 */
8637 bpf_error("dpc value %u too big; max value = 16383",
8639 /* the following instructions are made to convert jvalue
8640 * to the forme used to write dpc in an ss7 message*/
8641 val1
= jvalue
& 0x000000ff;
8643 val2
= jvalue
& 0x00003f00;
8645 jvalue
= val1
+ val2
;
8646 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8647 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8653 if (off_sls
== (u_int
)-1)
8654 bpf_error("'sls' supported only on SS7");
8655 /* sls coded on 4 bits so max value 15 */
8657 bpf_error("sls value %u too big; max value = 15",
8659 /* the following instruction is made to convert jvalue
8660 * to the forme used to write sls in an ss7 message*/
8661 jvalue
= jvalue
<< 4;
8662 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8663 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8672 static struct block
*
8673 gen_msg_abbrev(type
)
8679 * Q.2931 signalling protocol messages for handling virtual circuits
8680 * establishment and teardown
8685 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8689 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8693 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8697 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8701 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8704 case A_RELEASE_DONE
:
8705 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8715 gen_atmmulti_abbrev(type
)
8718 struct block
*b0
, *b1
;
8724 bpf_error("'oam' supported only on raw ATM");
8725 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8730 bpf_error("'oamf4' supported only on raw ATM");
8732 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8733 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8735 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8741 * Get Q.2931 signalling messages for switched
8742 * virtual connection
8745 bpf_error("'connectmsg' supported only on raw ATM");
8746 b0
= gen_msg_abbrev(A_SETUP
);
8747 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8749 b0
= gen_msg_abbrev(A_CONNECT
);
8751 b0
= gen_msg_abbrev(A_CONNECTACK
);
8753 b0
= gen_msg_abbrev(A_RELEASE
);
8755 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8757 b0
= gen_atmtype_abbrev(A_SC
);
8763 bpf_error("'metaconnect' supported only on raw ATM");
8764 b0
= gen_msg_abbrev(A_SETUP
);
8765 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8767 b0
= gen_msg_abbrev(A_CONNECT
);
8769 b0
= gen_msg_abbrev(A_RELEASE
);
8771 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8773 b0
= gen_atmtype_abbrev(A_METAC
);