1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
43 * XXX - why was this included even on UNIX?
52 #include <sys/param.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
72 #include "ethertype.h"
76 #include "ieee80211.h"
78 #include "sunatmpos.h"
81 #include "pcap/ipnet.h"
83 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
84 #include <linux/types.h>
85 #include <linux/if_packet.h>
86 #include <linux/filter.h>
88 #ifdef HAVE_NET_PFVAR_H
89 #include <sys/socket.h>
91 #include <net/pfvar.h>
92 #include <net/if_pflog.h>
95 #define offsetof(s, e) ((size_t)&((s *)0)->e)
99 #include <netdb.h> /* for "struct addrinfo" */
102 #include <pcap/namedb.h>
104 #define ETHERMTU 1500
106 #ifndef IPPROTO_HOPOPTS
107 #define IPPROTO_HOPOPTS 0
109 #ifndef IPPROTO_ROUTING
110 #define IPPROTO_ROUTING 43
112 #ifndef IPPROTO_FRAGMENT
113 #define IPPROTO_FRAGMENT 44
115 #ifndef IPPROTO_DSTOPTS
116 #define IPPROTO_DSTOPTS 60
119 #define IPPROTO_SCTP 132
122 #ifdef HAVE_OS_PROTO_H
123 #include "os-proto.h"
126 #define JMP(c) ((c)|BPF_JMP|BPF_K)
129 static jmp_buf top_ctx
;
130 static pcap_t
*bpf_pcap
;
132 /* Hack for handling VLAN and MPLS stacks. */
134 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
136 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
140 static int pcap_fddipad
;
144 bpf_error(const char *fmt
, ...)
149 if (bpf_pcap
!= NULL
)
150 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
157 static void init_linktype(pcap_t
*);
159 static void init_regs(void);
160 static int alloc_reg(void);
161 static void free_reg(int);
163 static struct block
*root
;
166 * Value passed to gen_load_a() to indicate what the offset argument
167 * is relative to the beginning of.
170 OR_PACKET
, /* full packet data */
171 OR_LINK
, /* link-layer header */
172 OR_OUTERLINK
, /* outer link-layer header */
173 OR_LLC
, /* 802.2 LLC header */
174 OR_LINKPL
, /* link-layer payload */
175 OR_MPLSPL
, /* MPLS payload */
176 OR_NET
, /* network-layer header */
177 OR_NET_NOSNAP
, /* network-layer header, with no SNAP header at the link layer */
178 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
179 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
184 * As errors are handled by a longjmp, anything allocated must be freed
185 * in the longjmp handler, so it must be reachable from that handler.
186 * One thing that's allocated is the result of pcap_nametoaddrinfo();
187 * it must be freed with freeaddrinfo(). This variable points to any
188 * addrinfo structure that would need to be freed.
190 static struct addrinfo
*ai
;
194 * We divy out chunks of memory rather than call malloc each time so
195 * we don't have to worry about leaking memory. It's probably
196 * not a big deal if all this memory was wasted but if this ever
197 * goes into a library that would probably not be a good idea.
199 * XXX - this *is* in a library....
202 #define CHUNK0SIZE 1024
208 static struct chunk chunks
[NCHUNKS
];
209 static int cur_chunk
;
211 static void *newchunk(u_int
);
212 static void freechunks(void);
213 static inline struct block
*new_block(int);
214 static inline struct slist
*new_stmt(int);
215 static struct block
*gen_retblk(int);
216 static inline void syntax(void);
218 static void backpatch(struct block
*, struct block
*);
219 static void merge(struct block
*, struct block
*);
220 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
221 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
222 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
223 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
224 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
225 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
227 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
228 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
229 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
230 static struct slist
*gen_load_llrel(u_int
, u_int
);
231 static struct slist
*gen_load_outerllrel(u_int
, u_int
);
232 static struct slist
*gen_load_linkplrel(u_int
, u_int
);
233 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
234 static struct slist
*gen_loadx_iphdrlen(void);
235 static struct block
*gen_uncond(int);
236 static inline struct block
*gen_true(void);
237 static inline struct block
*gen_false(void);
238 static struct block
*gen_ether_linktype(int);
239 static struct block
*gen_ipnet_linktype(int);
240 static struct block
*gen_linux_sll_linktype(int);
241 static struct slist
*gen_load_prism_llprefixlen(void);
242 static struct slist
*gen_load_avs_llprefixlen(void);
243 static struct slist
*gen_load_radiotap_llprefixlen(void);
244 static struct slist
*gen_load_ppi_llprefixlen(void);
245 static void insert_compute_vloffsets(struct block
*);
246 static struct slist
*gen_llprefixlen(void);
247 static struct slist
*gen_outerllprefixlen(void);
248 static struct slist
*gen_off_linkpl(void);
249 static int ethertype_to_ppptype(int);
250 static struct block
*gen_linktype(int);
251 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
252 static struct block
*gen_llc_linktype(int);
253 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
255 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
257 static struct block
*gen_ahostop(const u_char
*, int);
258 static struct block
*gen_ehostop(const u_char
*, int);
259 static struct block
*gen_fhostop(const u_char
*, int);
260 static struct block
*gen_thostop(const u_char
*, int);
261 static struct block
*gen_wlanhostop(const u_char
*, int);
262 static struct block
*gen_ipfchostop(const u_char
*, int);
263 static struct block
*gen_dnhostop(bpf_u_int32
, int);
264 static struct block
*gen_mpls_linktype(int);
265 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
267 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
270 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
272 static struct block
*gen_ipfrag(void);
273 static struct block
*gen_portatom(int, bpf_int32
);
274 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
275 static struct block
*gen_portatom6(int, bpf_int32
);
276 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
277 struct block
*gen_portop(int, int, int);
278 static struct block
*gen_port(int, int, int);
279 struct block
*gen_portrangeop(int, int, int, int);
280 static struct block
*gen_portrange(int, int, int, int);
281 struct block
*gen_portop6(int, int, int);
282 static struct block
*gen_port6(int, int, int);
283 struct block
*gen_portrangeop6(int, int, int, int);
284 static struct block
*gen_portrange6(int, int, int, int);
285 static int lookup_proto(const char *, int);
286 static struct block
*gen_protochain(int, int, int);
287 static struct block
*gen_proto(int, int, int);
288 static struct slist
*xfer_to_x(struct arth
*);
289 static struct slist
*xfer_to_a(struct arth
*);
290 static struct block
*gen_mac_multicast(int);
291 static struct block
*gen_len(int, int);
292 static struct block
*gen_check_802_11_data_frame(void);
294 static struct block
*gen_ppi_dlt_check(void);
295 static struct block
*gen_msg_abbrev(int type
);
306 /* XXX Round up to nearest long. */
307 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
309 /* XXX Round up to structure boundary. */
313 cp
= &chunks
[cur_chunk
];
314 if (n
> cp
->n_left
) {
315 ++cp
, k
= ++cur_chunk
;
317 bpf_error("out of memory");
318 size
= CHUNK0SIZE
<< k
;
319 cp
->m
= (void *)malloc(size
);
321 bpf_error("out of memory");
322 memset((char *)cp
->m
, 0, size
);
325 bpf_error("out of memory");
328 return (void *)((char *)cp
->m
+ cp
->n_left
);
337 for (i
= 0; i
< NCHUNKS
; ++i
)
338 if (chunks
[i
].m
!= NULL
) {
345 * A strdup whose allocations are freed after code generation is over.
349 register const char *s
;
351 int n
= strlen(s
) + 1;
352 char *cp
= newchunk(n
);
358 static inline struct block
*
364 p
= (struct block
*)newchunk(sizeof(*p
));
371 static inline struct slist
*
377 p
= (struct slist
*)newchunk(sizeof(*p
));
383 static struct block
*
387 struct block
*b
= new_block(BPF_RET
|BPF_K
);
396 bpf_error("syntax error in filter expression");
399 static bpf_u_int32 netmask
;
404 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
405 const char *buf
, int optimize
, bpf_u_int32 mask
)
408 const char * volatile xbuf
= buf
;
413 * XXX - single-thread this code path with pthread calls on
414 * UN*X, if the platform supports pthreads? If that requires
415 * a separate -lpthread, we might not want to do that.
418 extern int wsockinit (void);
424 EnterCriticalSection(&g_PcapCompileCriticalSection
);
428 * If this pcap_t hasn't been activated, it doesn't have a
429 * link-layer type, so we can't use it.
432 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
433 "not-yet-activated pcap_t passed to pcap_compile");
443 if (setjmp(top_ctx
)) {
458 snaplen
= pcap_snapshot(p
);
460 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
461 "snaplen of 0 rejects all packets");
466 lex_init(xbuf
? xbuf
: "");
474 root
= gen_retblk(snaplen
);
476 if (optimize
&& !no_optimize
) {
479 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
480 bpf_error("expression rejects all packets");
482 program
->bf_insns
= icode_to_fcode(root
, &len
);
483 program
->bf_len
= len
;
488 rc
= 0; /* We're all okay */
493 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
500 * entry point for using the compiler with no pcap open
501 * pass in all the stuff that is needed explicitly instead.
504 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
505 struct bpf_program
*program
,
506 const char *buf
, int optimize
, bpf_u_int32 mask
)
511 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
514 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
520 * Clean up a "struct bpf_program" by freeing all the memory allocated
524 pcap_freecode(struct bpf_program
*program
)
527 if (program
->bf_insns
!= NULL
) {
528 free((char *)program
->bf_insns
);
529 program
->bf_insns
= NULL
;
534 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
535 * which of the jt and jf fields has been resolved and which is a pointer
536 * back to another unresolved block (or nil). At least one of the fields
537 * in each block is already resolved.
540 backpatch(list
, target
)
541 struct block
*list
, *target
;
558 * Merge the lists in b0 and b1, using the 'sense' field to indicate
559 * which of jt and jf is the link.
563 struct block
*b0
, *b1
;
565 register struct block
**p
= &b0
;
567 /* Find end of list. */
569 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
571 /* Concatenate the lists. */
579 struct block
*ppi_dlt_check
;
582 * Insert before the statements of the first (root) block any
583 * statements needed to load the lengths of any variable-length
584 * headers into registers.
586 * XXX - a fancier strategy would be to insert those before the
587 * statements of all blocks that use those lengths and that
588 * have no predecessors that use them, so that we only compute
589 * the lengths if we need them. There might be even better
590 * approaches than that.
592 * However, those strategies would be more complicated, and
593 * as we don't generate code to compute a length if the
594 * program has no tests that use the length, and as most
595 * tests will probably use those lengths, we would just
596 * postpone computing the lengths so that it's not done
597 * for tests that fail early, and it's not clear that's
600 insert_compute_vloffsets(p
->head
);
603 * For DLT_PPI captures, generate a check of the per-packet
604 * DLT value to make sure it's DLT_IEEE802_11.
606 ppi_dlt_check
= gen_ppi_dlt_check();
607 if (ppi_dlt_check
!= NULL
)
608 gen_and(ppi_dlt_check
, p
);
610 backpatch(p
, gen_retblk(snaplen
));
611 p
->sense
= !p
->sense
;
612 backpatch(p
, gen_retblk(0));
618 struct block
*b0
, *b1
;
620 backpatch(b0
, b1
->head
);
621 b0
->sense
= !b0
->sense
;
622 b1
->sense
= !b1
->sense
;
624 b1
->sense
= !b1
->sense
;
630 struct block
*b0
, *b1
;
632 b0
->sense
= !b0
->sense
;
633 backpatch(b0
, b1
->head
);
634 b0
->sense
= !b0
->sense
;
643 b
->sense
= !b
->sense
;
646 static struct block
*
647 gen_cmp(offrel
, offset
, size
, v
)
648 enum e_offrel offrel
;
652 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
655 static struct block
*
656 gen_cmp_gt(offrel
, offset
, size
, v
)
657 enum e_offrel offrel
;
661 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
664 static struct block
*
665 gen_cmp_ge(offrel
, offset
, size
, v
)
666 enum e_offrel offrel
;
670 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
673 static struct block
*
674 gen_cmp_lt(offrel
, offset
, size
, v
)
675 enum e_offrel offrel
;
679 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
682 static struct block
*
683 gen_cmp_le(offrel
, offset
, size
, v
)
684 enum e_offrel offrel
;
688 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
691 static struct block
*
692 gen_mcmp(offrel
, offset
, size
, v
, mask
)
693 enum e_offrel offrel
;
698 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
701 static struct block
*
702 gen_bcmp(offrel
, offset
, size
, v
)
703 enum e_offrel offrel
;
704 register u_int offset
, size
;
705 register const u_char
*v
;
707 register struct block
*b
, *tmp
;
711 register const u_char
*p
= &v
[size
- 4];
712 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
713 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
715 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
722 register const u_char
*p
= &v
[size
- 2];
723 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
725 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
732 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
741 * AND the field of size "size" at offset "offset" relative to the header
742 * specified by "offrel" with "mask", and compare it with the value "v"
743 * with the test specified by "jtype"; if "reverse" is true, the test
744 * should test the opposite of "jtype".
746 static struct block
*
747 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
748 enum e_offrel offrel
;
750 bpf_u_int32 offset
, size
, mask
, jtype
;
753 struct slist
*s
, *s2
;
756 s
= gen_load_a(offrel
, offset
, size
);
758 if (mask
!= 0xffffffff) {
759 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
764 b
= new_block(JMP(jtype
));
767 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
773 * Various code constructs need to know the layout of the data link
774 * layer. These variables give the necessary offsets from the beginning
775 * of the packet data.
779 * This is the offset of the beginning of the link-layer header from
780 * the beginning of the raw packet data.
782 * It's usually 0, except for 802.11 with a fixed-length radio header.
783 * (For 802.11 with a variable-length radio header, we have to generate
784 * code to compute that offset; off_ll is 0 in that case.)
789 * If there's a variable-length header preceding the link-layer header,
790 * "reg_off_ll" is the register number for a register containing the
791 * length of that header, and therefore the offset of the link-layer
792 * header from the beginning of the raw packet data. Otherwise,
793 * "reg_off_ll" is -1.
795 static int reg_off_ll
;
798 * If we're checking a link-layer header for a packet encapsulated in
799 * another protocol layer, this is the offset of the outer layers'
800 * link-layer header from the beginning of the raw packet data.
802 static u_int off_outerll
;
805 * This is the offset of the outermost layers' link-layer header from
806 * the beginning of the raw packet data.
808 static u_int off_outermostll
;
811 * The offset of the beginning of the link-layer payload, from the beginning
812 * of the raw packet data, is, in the general case, the sum of a variable
813 * value and a constant value; the variable value may be absent, in which
814 * case the offset is only the constant value, and the constant value may
815 * be zero, in which case the offset is only the variable value.
817 * off_linkpl_constant_part is the constant value.
819 * reg_off_linkpl is the register number for a register containing the
820 * variable value, and -1 otherwise.
822 * off_linkpl_is_variable is 1 if there's a variable part.
824 static u_int off_linkpl_constant_part
;
825 static int off_linkpl_is_variable
;
826 static int reg_off_linkpl
;
829 * "off_linktype" is the offset to information in the link-layer header
830 * giving the packet type. This offset is relative to the beginning
831 * of the link-layer header - i.e., it doesn't include off_ll - so
832 * loads with an offset that includes "off_linktype" should use
835 * For Ethernet, it's the offset of the Ethernet type field; this
836 * means that it must have a value that skips VLAN tags.
838 * For link-layer types that always use 802.2 headers, it's the
839 * offset of the LLC header; this means that it must have a value
840 * that skips VLAN tags.
842 * For PPP, it's the offset of the PPP type field.
844 * For Cisco HDLC, it's the offset of the CHDLC type field.
846 * For BSD loopback, it's the offset of the AF_ value.
848 * For Linux cooked sockets, it's the offset of the type field.
850 * It's set to -1 for no encapsulation, in which case, IP is assumed.
852 static u_int off_linktype
;
855 * TRUE if the link layer includes an ATM pseudo-header.
857 static int is_atm
= 0;
860 * These are offsets for the ATM pseudo-header.
862 static u_int off_vpi
;
863 static u_int off_vci
;
864 static u_int off_proto
;
867 * These are offsets for the MTP2 fields.
870 static u_int off_li_hsl
;
873 * These are offsets for the MTP3 fields.
875 static u_int off_sio
;
876 static u_int off_opc
;
877 static u_int off_dpc
;
878 static u_int off_sls
;
881 * This is the offset of the first byte after the ATM pseudo_header,
882 * or -1 if there is no ATM pseudo-header.
884 static u_int off_payload
;
887 * These are offsets to the beginning of the network-layer header.
888 * They are relative to the beginning of the link-layer payload (i.e.,
889 * they don't include off_ll or off_linkpl).
891 * If the link layer never uses 802.2 LLC:
893 * "off_nl" and "off_nl_nosnap" are the same.
895 * If the link layer always uses 802.2 LLC:
897 * "off_nl" is the offset if there's a SNAP header following
900 * "off_nl_nosnap" is the offset if there's no SNAP header.
902 * If the link layer is Ethernet:
904 * "off_nl" is the offset if the packet is an Ethernet II packet
905 * (we assume no 802.3+802.2+SNAP);
907 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
908 * with an 802.2 header following it.
911 static u_int off_nl_nosnap
;
914 static int outerlinktype
;
915 static int outermostlinktype
;
921 outermostlinktype
= pcap_datalink(p
);
922 pcap_fddipad
= p
->fddipad
;
923 outerlinktype
= outermostlinktype
;
927 * No link-layer header inside the payload for another
930 linktype
= outerlinktype
;
934 * Assume it's not raw ATM with a pseudo-header, for now.
943 * And assume we're not doing SS7.
953 * Also assume it's not 802.11.
956 off_linkpl_constant_part
= 0;
957 off_linkpl_is_variable
= 0;
959 label_stack_depth
= 0;
960 vlan_stack_depth
= 0;
969 off_linkpl_constant_part
= 6;
970 off_nl
= 0; /* XXX in reality, variable! */
971 off_nl_nosnap
= 0; /* no 802.2 LLC */
974 case DLT_ARCNET_LINUX
:
976 off_linkpl_constant_part
= 8;
977 off_nl
= 0; /* XXX in reality, variable! */
978 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 off_linkpl_constant_part
= 14; /* Ethernet header length */
984 off_nl
= 0; /* Ethernet II */
985 off_nl_nosnap
= 3; /* 802.3+802.2 */
990 * SLIP doesn't have a link level type. The 16 byte
991 * header is hacked into our SLIP driver.
994 off_linkpl_constant_part
= 16;
996 off_nl_nosnap
= 0; /* no 802.2 LLC */
1000 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1003 off_linkpl_constant_part
= 24;
1005 off_nl_nosnap
= 0; /* no 802.2 LLC */
1011 off_linkpl_constant_part
= 4;
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1018 off_linkpl_constant_part
= 12;
1020 off_nl_nosnap
= 0; /* no 802.2 LLC */
1025 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1026 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1027 off_linktype
= 2; /* skip HDLC-like framing */
1028 off_linkpl_constant_part
= 4; /* skip HDLC-like framing and protocol field */
1030 off_nl_nosnap
= 0; /* no 802.2 LLC */
1035 * This does no include the Ethernet header, and
1036 * only covers session state.
1039 off_linkpl_constant_part
= 8;
1041 off_nl_nosnap
= 0; /* no 802.2 LLC */
1046 off_linkpl_constant_part
= 24;
1048 off_nl_nosnap
= 0; /* no 802.2 LLC */
1053 * FDDI doesn't really have a link-level type field.
1054 * We set "off_linktype" to the offset of the LLC header.
1056 * To check for Ethernet types, we assume that SSAP = SNAP
1057 * is being used and pick out the encapsulated Ethernet type.
1058 * XXX - should we generate code to check for SNAP?
1061 off_linktype
+= pcap_fddipad
;
1062 off_linkpl_constant_part
= 13; /* FDDI MAC header length */
1063 off_linkpl_constant_part
+= pcap_fddipad
;
1064 off_nl
= 8; /* 802.2+SNAP */
1065 off_nl_nosnap
= 3; /* 802.2 */
1070 * Token Ring doesn't really have a link-level type field.
1071 * We set "off_linktype" to the offset of the LLC header.
1073 * To check for Ethernet types, we assume that SSAP = SNAP
1074 * is being used and pick out the encapsulated Ethernet type.
1075 * XXX - should we generate code to check for SNAP?
1077 * XXX - the header is actually variable-length.
1078 * Some various Linux patched versions gave 38
1079 * as "off_linktype" and 40 as "off_nl"; however,
1080 * if a token ring packet has *no* routing
1081 * information, i.e. is not source-routed, the correct
1082 * values are 20 and 22, as they are in the vanilla code.
1084 * A packet is source-routed iff the uppermost bit
1085 * of the first byte of the source address, at an
1086 * offset of 8, has the uppermost bit set. If the
1087 * packet is source-routed, the total number of bytes
1088 * of routing information is 2 plus bits 0x1F00 of
1089 * the 16-bit value at an offset of 14 (shifted right
1090 * 8 - figure out which byte that is).
1093 off_linkpl_constant_part
= 14; /* Token Ring MAC header length */
1094 off_nl
= 8; /* 802.2+SNAP */
1095 off_nl_nosnap
= 3; /* 802.2 */
1098 case DLT_IEEE802_11
:
1099 case DLT_PRISM_HEADER
:
1100 case DLT_IEEE802_11_RADIO_AVS
:
1101 case DLT_IEEE802_11_RADIO
:
1103 * 802.11 doesn't really have a link-level type field.
1104 * We set "off_linktype" to the offset of the LLC header.
1106 * To check for Ethernet types, we assume that SSAP = SNAP
1107 * is being used and pick out the encapsulated Ethernet type.
1108 * XXX - should we generate code to check for SNAP?
1110 * We also handle variable-length radio headers here.
1111 * The Prism header is in theory variable-length, but in
1112 * practice it's always 144 bytes long. However, some
1113 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1114 * sometimes or always supply an AVS header, so we
1115 * have to check whether the radio header is a Prism
1116 * header or an AVS header, so, in practice, it's
1120 off_linkpl_constant_part
= 0; /* link-layer header is variable-length */
1121 off_linkpl_is_variable
= 1;
1122 off_nl
= 8; /* 802.2+SNAP */
1123 off_nl_nosnap
= 3; /* 802.2 */
1128 * At the moment we treat PPI the same way that we treat
1129 * normal Radiotap encoded packets. The difference is in
1130 * the function that generates the code at the beginning
1131 * to compute the header length. Since this code generator
1132 * of PPI supports bare 802.11 encapsulation only (i.e.
1133 * the encapsulated DLT should be DLT_IEEE802_11) we
1134 * generate code to check for this too.
1137 off_linkpl_constant_part
= 0; /* link-layer header is variable-length */
1138 off_linkpl_is_variable
= 1;
1139 off_nl
= 8; /* 802.2+SNAP */
1140 off_nl_nosnap
= 3; /* 802.2 */
1143 case DLT_ATM_RFC1483
:
1144 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1146 * assume routed, non-ISO PDUs
1147 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1149 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1150 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1151 * latter would presumably be treated the way PPPoE
1152 * should be, so you can do "pppoe and udp port 2049"
1153 * or "pppoa and tcp port 80" and have it check for
1154 * PPPo{A,E} and a PPP protocol of IP and....
1157 off_linkpl_constant_part
= 0; /* packet begins with LLC header */
1158 off_nl
= 8; /* 802.2+SNAP */
1159 off_nl_nosnap
= 3; /* 802.2 */
1164 * Full Frontal ATM; you get AALn PDUs with an ATM
1168 off_vpi
= SUNATM_VPI_POS
;
1169 off_vci
= SUNATM_VCI_POS
;
1170 off_proto
= PROTO_POS
;
1171 off_payload
= SUNATM_PKT_BEGIN_POS
;
1172 off_linktype
= off_payload
;
1173 off_linkpl_constant_part
= off_payload
; /* if LLC-encapsulated */
1174 off_nl
= 8; /* 802.2+SNAP */
1175 off_nl_nosnap
= 3; /* 802.2 */
1182 off_linkpl_constant_part
= 0;
1184 off_nl_nosnap
= 0; /* no 802.2 LLC */
1187 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1189 off_linkpl_constant_part
= 16;
1191 off_nl_nosnap
= 0; /* no 802.2 LLC */
1196 * LocalTalk does have a 1-byte type field in the LLAP header,
1197 * but really it just indicates whether there is a "short" or
1198 * "long" DDP packet following.
1201 off_linkpl_constant_part
= 0;
1203 off_nl_nosnap
= 0; /* no 802.2 LLC */
1206 case DLT_IP_OVER_FC
:
1208 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1209 * link-level type field. We set "off_linktype" to the
1210 * offset of the LLC header.
1212 * To check for Ethernet types, we assume that SSAP = SNAP
1213 * is being used and pick out the encapsulated Ethernet type.
1214 * XXX - should we generate code to check for SNAP? RFC
1215 * 2625 says SNAP should be used.
1218 off_linkpl_constant_part
= 16;
1219 off_nl
= 8; /* 802.2+SNAP */
1220 off_nl_nosnap
= 3; /* 802.2 */
1225 * XXX - we should set this to handle SNAP-encapsulated
1226 * frames (NLPID of 0x80).
1229 off_linkpl_constant_part
= 0;
1231 off_nl_nosnap
= 0; /* no 802.2 LLC */
1235 * the only BPF-interesting FRF.16 frames are non-control frames;
1236 * Frame Relay has a variable length link-layer
1237 * so lets start with offset 4 for now and increments later on (FIXME);
1241 off_linkpl_constant_part
= 0;
1243 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1246 case DLT_APPLE_IP_OVER_IEEE1394
:
1248 off_linkpl_constant_part
= 18;
1250 off_nl_nosnap
= 0; /* no 802.2 LLC */
1253 case DLT_SYMANTEC_FIREWALL
:
1255 off_linkpl_constant_part
= 44;
1256 off_nl
= 0; /* Ethernet II */
1257 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1260 #ifdef HAVE_NET_PFVAR_H
1263 off_linkpl_constant_part
= PFLOG_HDRLEN
;
1265 off_nl_nosnap
= 0; /* no 802.2 LLC */
1269 case DLT_JUNIPER_MFR
:
1270 case DLT_JUNIPER_MLFR
:
1271 case DLT_JUNIPER_MLPPP
:
1272 case DLT_JUNIPER_PPP
:
1273 case DLT_JUNIPER_CHDLC
:
1274 case DLT_JUNIPER_FRELAY
:
1276 off_linkpl_constant_part
= 4;
1278 off_nl_nosnap
= -1; /* no 802.2 LLC */
1281 case DLT_JUNIPER_ATM1
:
1282 off_linktype
= 4; /* in reality variable between 4-8 */
1283 off_linkpl_constant_part
= 4; /* in reality variable between 4-8 */
1288 case DLT_JUNIPER_ATM2
:
1289 off_linktype
= 8; /* in reality variable between 8-12 */
1290 off_linkpl_constant_part
= 8; /* in reality variable between 8-12 */
1295 /* frames captured on a Juniper PPPoE service PIC
1296 * contain raw ethernet frames */
1297 case DLT_JUNIPER_PPPOE
:
1298 case DLT_JUNIPER_ETHER
:
1299 off_linkpl_constant_part
= 14;
1301 off_nl
= 18; /* Ethernet II */
1302 off_nl_nosnap
= 21; /* 802.3+802.2 */
1305 case DLT_JUNIPER_PPPOE_ATM
:
1307 off_linkpl_constant_part
= 6;
1309 off_nl_nosnap
= -1; /* no 802.2 LLC */
1312 case DLT_JUNIPER_GGSN
:
1314 off_linkpl_constant_part
= 12;
1316 off_nl_nosnap
= -1; /* no 802.2 LLC */
1319 case DLT_JUNIPER_ES
:
1321 off_linkpl_constant_part
= -1; /* not really a network layer but raw IP addresses */
1322 off_nl
= -1; /* not really a network layer but raw IP addresses */
1323 off_nl_nosnap
= -1; /* no 802.2 LLC */
1326 case DLT_JUNIPER_MONITOR
:
1328 off_linkpl_constant_part
= 12;
1329 off_nl
= 0; /* raw IP/IP6 header */
1330 off_nl_nosnap
= -1; /* no 802.2 LLC */
1333 case DLT_BACNET_MS_TP
:
1335 off_linkpl_constant_part
= -1;
1340 case DLT_JUNIPER_SERVICES
:
1342 off_linkpl_constant_part
= -1; /* L3 proto location dep. on cookie type */
1343 off_nl
= -1; /* L3 proto location dep. on cookie type */
1344 off_nl_nosnap
= -1; /* no 802.2 LLC */
1347 case DLT_JUNIPER_VP
:
1349 off_linkpl_constant_part
= -1;
1354 case DLT_JUNIPER_ST
:
1356 off_linkpl_constant_part
= -1;
1361 case DLT_JUNIPER_ISM
:
1363 off_linkpl_constant_part
= -1;
1368 case DLT_JUNIPER_VS
:
1369 case DLT_JUNIPER_SRX_E2E
:
1370 case DLT_JUNIPER_FIBRECHANNEL
:
1371 case DLT_JUNIPER_ATM_CEMIC
:
1373 off_linkpl_constant_part
= -1;
1386 off_linkpl_constant_part
= -1;
1391 case DLT_MTP2_WITH_PHDR
:
1399 off_linkpl_constant_part
= -1;
1412 off_linkpl_constant_part
= -1;
1419 off_linkpl_constant_part
= 4;
1426 * Currently, only raw "link[N:M]" filtering is supported.
1428 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1429 off_linkpl_constant_part
= -1;
1430 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1431 off_nl_nosnap
= -1; /* no 802.2 LLC */
1436 off_linkpl_constant_part
= 24; /* ipnet header length */
1441 case DLT_NETANALYZER
:
1442 off_ll
= 4; /* Ethernet header is past 4-byte pseudo-header */
1444 off_linkpl_constant_part
= off_ll
+ 14; /* pseudo-header+Ethernet header length */
1445 off_nl
= 0; /* Ethernet II */
1446 off_nl_nosnap
= 3; /* 802.3+802.2 */
1449 case DLT_NETANALYZER_TRANSPARENT
:
1450 off_ll
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1452 off_linkpl_constant_part
= off_ll
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1453 off_nl
= 0; /* Ethernet II */
1454 off_nl_nosnap
= 3; /* 802.3+802.2 */
1459 * For values in the range in which we've assigned new
1460 * DLT_ values, only raw "link[N:M]" filtering is supported.
1462 if (linktype
>= DLT_MATCHING_MIN
&&
1463 linktype
<= DLT_MATCHING_MAX
) {
1465 off_linkpl_constant_part
= -1;
1472 bpf_error("unknown data link type %d", linktype
);
1477 * Load a value relative to the beginning of the link-layer header.
1478 * The link-layer header doesn't necessarily begin at the beginning
1479 * of the packet data; there might be a variable-length prefix containing
1480 * radio information.
1482 static struct slist
*
1483 gen_load_llrel(offset
, size
)
1486 struct slist
*s
, *s2
;
1488 s
= gen_llprefixlen();
1491 * If "s" is non-null, it has code to arrange that the X register
1492 * contains the length of the prefix preceding the link-layer
1495 * Otherwise, the length of the prefix preceding the link-layer
1496 * header is "off_ll".
1500 * There's a variable-length prefix preceding the
1501 * link-layer header. "s" points to a list of statements
1502 * that put the length of that prefix into the X register.
1503 * do an indirect load, to use the X register as an offset.
1505 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1510 * There is no variable-length header preceding the
1511 * link-layer header; add in off_ll, which, if there's
1512 * a fixed-length header preceding the link-layer header,
1513 * is the length of that header.
1515 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1516 s
->s
.k
= offset
+ off_ll
;
1522 * Load a value relative to the beginning of the outer link-layer header,
1523 * if we're also looking at an inner link-layer header encapsulated
1524 * within other protocol layers.
1526 * The link-layer header doesn't necessarily begin at the beginning
1527 * of the packet data; there might be a variable-length prefix containing
1528 * radio information.
1530 static struct slist
*
1531 gen_load_outerllrel(offset
, size
)
1534 struct slist
*s
, *s2
;
1536 s
= gen_outerllprefixlen();
1539 * If "s" is non-null, it has code to arrange that the X register
1540 * contains the length of the prefix preceding the link-layer
1543 * Otherwise, the length of the prefix preceding the link-layer
1544 * header is "off_outerll".
1548 * There's a variable-length prefix preceding the
1549 * link-layer header. "s" points to a list of statements
1550 * that put the length of that prefix into the X register.
1551 * do an indirect load, to use the X register as an offset.
1553 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1558 * There is no variable-length header preceding the
1559 * link-layer header; add in outer_off_ll, which, if there's
1560 * a fixed-length header preceding the link-layer header,
1561 * is the length of that header.
1563 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1564 s
->s
.k
= off_outerll
+ offset
;
1570 * Load a value relative to the beginning of the link-layer payload.
1572 static struct slist
*
1573 gen_load_linkplrel(offset
, size
)
1576 struct slist
*s
, *s2
;
1578 s
= gen_off_linkpl();
1581 * If s is non-null, the offset of the link-layer payload is
1582 * variable, and s points to a list of instructions that
1583 * arrange that the X register contains the variable part
1584 * of that offset. The sum of that variable part and
1585 * off_linkpl_constant_part is the offset.
1587 * Otherwise, the offset of the link-layer payload is constant,
1588 * and is in off_linkpl_constant_part.
1592 * The variable part of the offset of the link-layer payload
1593 * is in the X register. Do an indirect load, to use the X
1594 * register as part of the offset of the load.
1596 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1597 s2
->s
.k
= off_linkpl_constant_part
+ offset
;
1601 * The offset of the link-layer payload is constant,
1602 * and is in off_linkpl_constant_part; load the value
1603 * at that offset plus the specified offset.
1605 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1606 s
->s
.k
= off_linkpl_constant_part
+ offset
;
1612 * Load a value relative to the beginning of the specified header.
1614 static struct slist
*
1615 gen_load_a(offrel
, offset
, size
)
1616 enum e_offrel offrel
;
1619 struct slist
*s
, *s2
;
1624 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1629 s
= gen_load_llrel(offset
, size
);
1633 s
= gen_load_outerllrel(offset
, size
);
1639 s
= gen_load_linkplrel(offset
, size
);
1643 s
= gen_load_linkplrel(off_nl
+ offset
, size
);
1647 s
= gen_load_linkplrel(off_nl_nosnap
+ offset
, size
);
1652 * Load the X register with the length of the IPv4 header
1653 * (plus the offset of the link-layer header, if it's
1654 * preceded by a variable-length header such as a radio
1655 * header), in bytes.
1657 s
= gen_loadx_iphdrlen();
1660 * Load the item at {offset of the link-layer payload} +
1661 * {offset, relative to the start of the link-layer
1662 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1663 * {specified offset}.
1665 * If the offset of the link-layer payload is variable,
1666 * the variable part of that offset is included in the
1667 * value in the X register, and we include the constant
1668 * part in the offset of the load.
1670 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1671 s2
->s
.k
= off_linkpl_constant_part
+ off_nl
+ offset
;
1676 s
= gen_load_linkplrel(off_nl
+ 40 + offset
, size
);
1687 * Generate code to load into the X register the sum of the length of
1688 * the IPv4 header and the variable part of the offset of the link-layer
1691 static struct slist
*
1692 gen_loadx_iphdrlen()
1694 struct slist
*s
, *s2
;
1696 s
= gen_off_linkpl();
1699 * The offset of the link-layer payload has a variable
1700 * part. "s" points to a list of statements that put
1701 * the variable part of that offset into the X register.
1703 * The 4*([k]&0xf) addressing mode can't be used, as we
1704 * don't have a constant offset, so we have to load the
1705 * value in question into the A register and add to it
1706 * the value from the X register.
1708 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1711 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1714 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1719 * The A register now contains the length of the IP header.
1720 * We need to add to it the variable part of the offset of
1721 * the link-layer payload, which is still in the X
1722 * register, and move the result into the X register.
1724 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1725 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1728 * The offset of the link-layer payload is a constant,
1729 * so no code was generated to load the (non-existent)
1730 * variable part of that offset.
1732 * This means we can use the 4*([k]&0xf) addressing
1733 * mode. Load the length of the IPv4 header, which
1734 * is at an offset of off_nl from the beginning of
1735 * the link-layer payload, and thus at an offset of
1736 * off_linkpl_constant_part + off_nl from the beginning
1737 * of the raw packet data, using that addressing mode.
1739 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1740 s
->s
.k
= off_linkpl_constant_part
+ off_nl
;
1745 static struct block
*
1752 s
= new_stmt(BPF_LD
|BPF_IMM
);
1754 b
= new_block(JMP(BPF_JEQ
));
1760 static inline struct block
*
1763 return gen_uncond(1);
1766 static inline struct block
*
1769 return gen_uncond(0);
1773 * Byte-swap a 32-bit number.
1774 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1775 * big-endian platforms.)
1777 #define SWAPLONG(y) \
1778 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1781 * Generate code to match a particular packet type.
1783 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1784 * value, if <= ETHERMTU. We use that to determine whether to
1785 * match the type/length field or to check the type/length field for
1786 * a value <= ETHERMTU to see whether it's a type field and then do
1787 * the appropriate test.
1789 static struct block
*
1790 gen_ether_linktype(proto
)
1793 struct block
*b0
, *b1
;
1799 case LLCSAP_NETBEUI
:
1801 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1802 * so we check the DSAP and SSAP.
1804 * LLCSAP_IP checks for IP-over-802.2, rather
1805 * than IP-over-Ethernet or IP-over-SNAP.
1807 * XXX - should we check both the DSAP and the
1808 * SSAP, like this, or should we check just the
1809 * DSAP, as we do for other types <= ETHERMTU
1810 * (i.e., other SAP values)?
1812 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1814 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1815 ((proto
<< 8) | proto
));
1823 * Ethernet_II frames, which are Ethernet
1824 * frames with a frame type of ETHERTYPE_IPX;
1826 * Ethernet_802.3 frames, which are 802.3
1827 * frames (i.e., the type/length field is
1828 * a length field, <= ETHERMTU, rather than
1829 * a type field) with the first two bytes
1830 * after the Ethernet/802.3 header being
1833 * Ethernet_802.2 frames, which are 802.3
1834 * frames with an 802.2 LLC header and
1835 * with the IPX LSAP as the DSAP in the LLC
1838 * Ethernet_SNAP frames, which are 802.3
1839 * frames with an LLC header and a SNAP
1840 * header and with an OUI of 0x000000
1841 * (encapsulated Ethernet) and a protocol
1842 * ID of ETHERTYPE_IPX in the SNAP header.
1844 * XXX - should we generate the same code both
1845 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1849 * This generates code to check both for the
1850 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1852 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1853 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1857 * Now we add code to check for SNAP frames with
1858 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1860 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1864 * Now we generate code to check for 802.3
1865 * frames in general.
1867 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1871 * Now add the check for 802.3 frames before the
1872 * check for Ethernet_802.2 and Ethernet_802.3,
1873 * as those checks should only be done on 802.3
1874 * frames, not on Ethernet frames.
1879 * Now add the check for Ethernet_II frames, and
1880 * do that before checking for the other frame
1883 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1884 (bpf_int32
)ETHERTYPE_IPX
);
1888 case ETHERTYPE_ATALK
:
1889 case ETHERTYPE_AARP
:
1891 * EtherTalk (AppleTalk protocols on Ethernet link
1892 * layer) may use 802.2 encapsulation.
1896 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1897 * we check for an Ethernet type field less than
1898 * 1500, which means it's an 802.3 length field.
1900 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1904 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1905 * SNAP packets with an organization code of
1906 * 0x080007 (Apple, for Appletalk) and a protocol
1907 * type of ETHERTYPE_ATALK (Appletalk).
1909 * 802.2-encapsulated ETHERTYPE_AARP packets are
1910 * SNAP packets with an organization code of
1911 * 0x000000 (encapsulated Ethernet) and a protocol
1912 * type of ETHERTYPE_AARP (Appletalk ARP).
1914 if (proto
== ETHERTYPE_ATALK
)
1915 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1916 else /* proto == ETHERTYPE_AARP */
1917 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1921 * Check for Ethernet encapsulation (Ethertalk
1922 * phase 1?); we just check for the Ethernet
1925 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1931 if (proto
<= ETHERMTU
) {
1933 * This is an LLC SAP value, so the frames
1934 * that match would be 802.2 frames.
1935 * Check that the frame is an 802.2 frame
1936 * (i.e., that the length/type field is
1937 * a length field, <= ETHERMTU) and
1938 * then check the DSAP.
1940 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1942 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1948 * This is an Ethernet type, so compare
1949 * the length/type field with it (if
1950 * the frame is an 802.2 frame, the length
1951 * field will be <= ETHERMTU, and, as
1952 * "proto" is > ETHERMTU, this test
1953 * will fail and the frame won't match,
1954 * which is what we want).
1956 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1963 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1964 * or IPv6 then we have an error.
1966 static struct block
*
1967 gen_ipnet_linktype(proto
)
1973 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1974 (bpf_int32
)IPH_AF_INET
);
1977 case ETHERTYPE_IPV6
:
1978 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1979 (bpf_int32
)IPH_AF_INET6
);
1990 * Generate code to match a particular packet type.
1992 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1993 * value, if <= ETHERMTU. We use that to determine whether to
1994 * match the type field or to check the type field for the special
1995 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1997 static struct block
*
1998 gen_linux_sll_linktype(proto
)
2001 struct block
*b0
, *b1
;
2007 case LLCSAP_NETBEUI
:
2009 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2010 * so we check the DSAP and SSAP.
2012 * LLCSAP_IP checks for IP-over-802.2, rather
2013 * than IP-over-Ethernet or IP-over-SNAP.
2015 * XXX - should we check both the DSAP and the
2016 * SSAP, like this, or should we check just the
2017 * DSAP, as we do for other types <= ETHERMTU
2018 * (i.e., other SAP values)?
2020 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2021 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
2022 ((proto
<< 8) | proto
));
2028 * Ethernet_II frames, which are Ethernet
2029 * frames with a frame type of ETHERTYPE_IPX;
2031 * Ethernet_802.3 frames, which have a frame
2032 * type of LINUX_SLL_P_802_3;
2034 * Ethernet_802.2 frames, which are 802.3
2035 * frames with an 802.2 LLC header (i.e, have
2036 * a frame type of LINUX_SLL_P_802_2) and
2037 * with the IPX LSAP as the DSAP in the LLC
2040 * Ethernet_SNAP frames, which are 802.3
2041 * frames with an LLC header and a SNAP
2042 * header and with an OUI of 0x000000
2043 * (encapsulated Ethernet) and a protocol
2044 * ID of ETHERTYPE_IPX in the SNAP header.
2046 * First, do the checks on LINUX_SLL_P_802_2
2047 * frames; generate the check for either
2048 * Ethernet_802.2 or Ethernet_SNAP frames, and
2049 * then put a check for LINUX_SLL_P_802_2 frames
2052 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2053 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2055 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2059 * Now check for 802.3 frames and OR that with
2060 * the previous test.
2062 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2066 * Now add the check for Ethernet_II frames, and
2067 * do that before checking for the other frame
2070 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2071 (bpf_int32
)ETHERTYPE_IPX
);
2075 case ETHERTYPE_ATALK
:
2076 case ETHERTYPE_AARP
:
2078 * EtherTalk (AppleTalk protocols on Ethernet link
2079 * layer) may use 802.2 encapsulation.
2083 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2084 * we check for the 802.2 protocol type in the
2085 * "Ethernet type" field.
2087 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2090 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2091 * SNAP packets with an organization code of
2092 * 0x080007 (Apple, for Appletalk) and a protocol
2093 * type of ETHERTYPE_ATALK (Appletalk).
2095 * 802.2-encapsulated ETHERTYPE_AARP packets are
2096 * SNAP packets with an organization code of
2097 * 0x000000 (encapsulated Ethernet) and a protocol
2098 * type of ETHERTYPE_AARP (Appletalk ARP).
2100 if (proto
== ETHERTYPE_ATALK
)
2101 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2102 else /* proto == ETHERTYPE_AARP */
2103 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2107 * Check for Ethernet encapsulation (Ethertalk
2108 * phase 1?); we just check for the Ethernet
2111 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2117 if (proto
<= ETHERMTU
) {
2119 * This is an LLC SAP value, so the frames
2120 * that match would be 802.2 frames.
2121 * Check for the 802.2 protocol type
2122 * in the "Ethernet type" field, and
2123 * then check the DSAP.
2125 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2127 b1
= gen_cmp(OR_LINK
, off_linkpl_constant_part
, BPF_B
,
2133 * This is an Ethernet type, so compare
2134 * the length/type field with it (if
2135 * the frame is an 802.2 frame, the length
2136 * field will be <= ETHERMTU, and, as
2137 * "proto" is > ETHERMTU, this test
2138 * will fail and the frame won't match,
2139 * which is what we want).
2141 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2147 static struct slist
*
2148 gen_load_prism_llprefixlen()
2150 struct slist
*s1
, *s2
;
2151 struct slist
*sjeq_avs_cookie
;
2152 struct slist
*sjcommon
;
2155 * This code is not compatible with the optimizer, as
2156 * we are generating jmp instructions within a normal
2157 * slist of instructions
2162 * Generate code to load the length of the radio header into
2163 * the register assigned to hold that length, if one has been
2164 * assigned. (If one hasn't been assigned, no code we've
2165 * generated uses that prefix, so we don't need to generate any
2168 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2169 * or always use the AVS header rather than the Prism header.
2170 * We load a 4-byte big-endian value at the beginning of the
2171 * raw packet data, and see whether, when masked with 0xFFFFF000,
2172 * it's equal to 0x80211000. If so, that indicates that it's
2173 * an AVS header (the masked-out bits are the version number).
2174 * Otherwise, it's a Prism header.
2176 * XXX - the Prism header is also, in theory, variable-length,
2177 * but no known software generates headers that aren't 144
2180 if (reg_off_ll
!= -1) {
2184 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2188 * AND it with 0xFFFFF000.
2190 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2191 s2
->s
.k
= 0xFFFFF000;
2195 * Compare with 0x80211000.
2197 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2198 sjeq_avs_cookie
->s
.k
= 0x80211000;
2199 sappend(s1
, sjeq_avs_cookie
);
2204 * The 4 bytes at an offset of 4 from the beginning of
2205 * the AVS header are the length of the AVS header.
2206 * That field is big-endian.
2208 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2211 sjeq_avs_cookie
->s
.jt
= s2
;
2214 * Now jump to the code to allocate a register
2215 * into which to save the header length and
2216 * store the length there. (The "jump always"
2217 * instruction needs to have the k field set;
2218 * it's added to the PC, so, as we're jumping
2219 * over a single instruction, it should be 1.)
2221 sjcommon
= new_stmt(JMP(BPF_JA
));
2223 sappend(s1
, sjcommon
);
2226 * Now for the code that handles the Prism header.
2227 * Just load the length of the Prism header (144)
2228 * into the A register. Have the test for an AVS
2229 * header branch here if we don't have an AVS header.
2231 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2234 sjeq_avs_cookie
->s
.jf
= s2
;
2237 * Now allocate a register to hold that value and store
2238 * it. The code for the AVS header will jump here after
2239 * loading the length of the AVS header.
2241 s2
= new_stmt(BPF_ST
);
2242 s2
->s
.k
= reg_off_ll
;
2244 sjcommon
->s
.jf
= s2
;
2247 * Now move it into the X register.
2249 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2257 static struct slist
*
2258 gen_load_avs_llprefixlen()
2260 struct slist
*s1
, *s2
;
2263 * Generate code to load the length of the AVS header into
2264 * the register assigned to hold that length, if one has been
2265 * assigned. (If one hasn't been assigned, no code we've
2266 * generated uses that prefix, so we don't need to generate any
2269 if (reg_off_ll
!= -1) {
2271 * The 4 bytes at an offset of 4 from the beginning of
2272 * the AVS header are the length of the AVS header.
2273 * That field is big-endian.
2275 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2279 * Now allocate a register to hold that value and store
2282 s2
= new_stmt(BPF_ST
);
2283 s2
->s
.k
= reg_off_ll
;
2287 * Now move it into the X register.
2289 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2297 static struct slist
*
2298 gen_load_radiotap_llprefixlen()
2300 struct slist
*s1
, *s2
;
2303 * Generate code to load the length of the radiotap header into
2304 * the register assigned to hold that length, if one has been
2305 * assigned. (If one hasn't been assigned, no code we've
2306 * generated uses that prefix, so we don't need to generate any
2309 if (reg_off_ll
!= -1) {
2311 * The 2 bytes at offsets of 2 and 3 from the beginning
2312 * of the radiotap header are the length of the radiotap
2313 * header; unfortunately, it's little-endian, so we have
2314 * to load it a byte at a time and construct the value.
2318 * Load the high-order byte, at an offset of 3, shift it
2319 * left a byte, and put the result in the X register.
2321 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2323 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2326 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2330 * Load the next byte, at an offset of 2, and OR the
2331 * value from the X register into it.
2333 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2336 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2340 * Now allocate a register to hold that value and store
2343 s2
= new_stmt(BPF_ST
);
2344 s2
->s
.k
= reg_off_ll
;
2348 * Now move it into the X register.
2350 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2359 * At the moment we treat PPI as normal Radiotap encoded
2360 * packets. The difference is in the function that generates
2361 * the code at the beginning to compute the header length.
2362 * Since this code generator of PPI supports bare 802.11
2363 * encapsulation only (i.e. the encapsulated DLT should be
2364 * DLT_IEEE802_11) we generate code to check for this too;
2365 * that's done in finish_parse().
2367 static struct slist
*
2368 gen_load_ppi_llprefixlen()
2370 struct slist
*s1
, *s2
;
2373 * Generate code to load the length of the radiotap header
2374 * into the register assigned to hold that length, if one has
2377 if (reg_off_ll
!= -1) {
2379 * The 2 bytes at offsets of 2 and 3 from the beginning
2380 * of the radiotap header are the length of the radiotap
2381 * header; unfortunately, it's little-endian, so we have
2382 * to load it a byte at a time and construct the value.
2386 * Load the high-order byte, at an offset of 3, shift it
2387 * left a byte, and put the result in the X register.
2389 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2391 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2394 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2398 * Load the next byte, at an offset of 2, and OR the
2399 * value from the X register into it.
2401 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2404 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2408 * Now allocate a register to hold that value and store
2411 s2
= new_stmt(BPF_ST
);
2412 s2
->s
.k
= reg_off_ll
;
2416 * Now move it into the X register.
2418 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2427 * Load a value relative to the beginning of the link-layer header after the 802.11
2428 * header, i.e. LLC_SNAP.
2429 * The link-layer header doesn't necessarily begin at the beginning
2430 * of the packet data; there might be a variable-length prefix containing
2431 * radio information.
2433 static struct slist
*
2434 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2437 struct slist
*sjset_data_frame_1
;
2438 struct slist
*sjset_data_frame_2
;
2439 struct slist
*sjset_qos
;
2440 struct slist
*sjset_radiotap_flags
;
2441 struct slist
*sjset_radiotap_tsft
;
2442 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2443 struct slist
*s_roundup
;
2445 if (reg_off_linkpl
== -1) {
2447 * No register has been assigned to the offset of
2448 * the link-layer payload, which means nobody needs
2449 * it; don't bother computing it - just return
2450 * what we already have.
2456 * This code is not compatible with the optimizer, as
2457 * we are generating jmp instructions within a normal
2458 * slist of instructions
2463 * If "s" is non-null, it has code to arrange that the X register
2464 * contains the length of the prefix preceding the link-layer
2467 * Otherwise, the length of the prefix preceding the link-layer
2468 * header is "off_outermostll".
2472 * There is no variable-length header preceding the
2473 * link-layer header.
2475 * Load the length of the fixed-length prefix preceding
2476 * the link-layer header (if any) into the X register,
2477 * and store it in the reg_off_linkpl register.
2478 * That length is off_outermostll.
2480 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2481 s
->s
.k
= off_outermostll
;
2485 * The X register contains the offset of the beginning of the
2486 * link-layer header; add 24, which is the minimum length
2487 * of the MAC header for a data frame, to that, and store it
2488 * in reg_off_linkpl, and then load the Frame Control field,
2489 * which is at the offset in the X register, with an indexed load.
2491 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2493 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2496 s2
= new_stmt(BPF_ST
);
2497 s2
->s
.k
= reg_off_linkpl
;
2500 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2505 * Check the Frame Control field to see if this is a data frame;
2506 * a data frame has the 0x08 bit (b3) in that field set and the
2507 * 0x04 bit (b2) clear.
2509 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2510 sjset_data_frame_1
->s
.k
= 0x08;
2511 sappend(s
, sjset_data_frame_1
);
2514 * If b3 is set, test b2, otherwise go to the first statement of
2515 * the rest of the program.
2517 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2518 sjset_data_frame_2
->s
.k
= 0x04;
2519 sappend(s
, sjset_data_frame_2
);
2520 sjset_data_frame_1
->s
.jf
= snext
;
2523 * If b2 is not set, this is a data frame; test the QoS bit.
2524 * Otherwise, go to the first statement of the rest of the
2527 sjset_data_frame_2
->s
.jt
= snext
;
2528 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2529 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2530 sappend(s
, sjset_qos
);
2533 * If it's set, add 2 to reg_off_linkpl, to skip the QoS
2535 * Otherwise, go to the first statement of the rest of the
2538 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2539 s2
->s
.k
= reg_off_linkpl
;
2541 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2544 s2
= new_stmt(BPF_ST
);
2545 s2
->s
.k
= reg_off_linkpl
;
2549 * If we have a radiotap header, look at it to see whether
2550 * there's Atheros padding between the MAC-layer header
2553 * Note: all of the fields in the radiotap header are
2554 * little-endian, so we byte-swap all of the values
2555 * we test against, as they will be loaded as big-endian
2558 if (linktype
== DLT_IEEE802_11_RADIO
) {
2560 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2561 * in the presence flag?
2563 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2567 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2568 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2569 sappend(s
, sjset_radiotap_flags
);
2572 * If not, skip all of this.
2574 sjset_radiotap_flags
->s
.jf
= snext
;
2577 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2579 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2580 new_stmt(JMP(BPF_JSET
));
2581 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2582 sappend(s
, sjset_radiotap_tsft
);
2585 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2586 * at an offset of 16 from the beginning of the raw packet
2587 * data (8 bytes for the radiotap header and 8 bytes for
2590 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2593 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2597 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2598 sjset_tsft_datapad
->s
.k
= 0x20;
2599 sappend(s
, sjset_tsft_datapad
);
2602 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2603 * at an offset of 8 from the beginning of the raw packet
2604 * data (8 bytes for the radiotap header).
2606 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2609 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2613 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2614 sjset_notsft_datapad
->s
.k
= 0x20;
2615 sappend(s
, sjset_notsft_datapad
);
2618 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2619 * set, round the length of the 802.11 header to
2620 * a multiple of 4. Do that by adding 3 and then
2621 * dividing by and multiplying by 4, which we do by
2624 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2625 s_roundup
->s
.k
= reg_off_linkpl
;
2626 sappend(s
, s_roundup
);
2627 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2630 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2633 s2
= new_stmt(BPF_ST
);
2634 s2
->s
.k
= reg_off_linkpl
;
2637 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2638 sjset_tsft_datapad
->s
.jf
= snext
;
2639 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2640 sjset_notsft_datapad
->s
.jf
= snext
;
2642 sjset_qos
->s
.jf
= snext
;
2648 insert_compute_vloffsets(b
)
2654 * For link-layer types that have a variable-length header
2655 * preceding the link-layer header, generate code to load
2656 * the offset of the link-layer header into the register
2657 * assigned to that offset, if any.
2659 * XXX - this, and the next switch statement, won't handle
2660 * encapsulation of 802.11 or 802.11+radio information in
2661 * some other protocol stack. That's significantly more
2664 switch (outermostlinktype
) {
2666 case DLT_PRISM_HEADER
:
2667 s
= gen_load_prism_llprefixlen();
2670 case DLT_IEEE802_11_RADIO_AVS
:
2671 s
= gen_load_avs_llprefixlen();
2674 case DLT_IEEE802_11_RADIO
:
2675 s
= gen_load_radiotap_llprefixlen();
2679 s
= gen_load_ppi_llprefixlen();
2688 * For link-layer types that have a variable-length link-layer
2689 * header, generate code to load the offset of the link-layer
2690 * payload into the register assigned to that offset, if any.
2692 switch (outermostlinktype
) {
2694 case DLT_IEEE802_11
:
2695 case DLT_PRISM_HEADER
:
2696 case DLT_IEEE802_11_RADIO_AVS
:
2697 case DLT_IEEE802_11_RADIO
:
2699 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2704 * If we have any offset-loading code, append all the
2705 * existing statements in the block to those statements,
2706 * and make the resulting list the list of statements
2710 sappend(s
, b
->stmts
);
2715 static struct block
*
2716 gen_ppi_dlt_check(void)
2718 struct slist
*s_load_dlt
;
2721 if (linktype
== DLT_PPI
)
2723 /* Create the statements that check for the DLT
2725 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2726 s_load_dlt
->s
.k
= 4;
2728 b
= new_block(JMP(BPF_JEQ
));
2730 b
->stmts
= s_load_dlt
;
2731 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2741 static struct slist
*
2742 gen_prism_llprefixlen(void)
2746 if (reg_off_ll
== -1) {
2748 * We haven't yet assigned a register for the length
2749 * of the radio header; allocate one.
2751 reg_off_ll
= alloc_reg();
2755 * Load the register containing the radio length
2756 * into the X register.
2758 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2759 s
->s
.k
= reg_off_ll
;
2763 static struct slist
*
2764 gen_avs_llprefixlen(void)
2768 if (reg_off_ll
== -1) {
2770 * We haven't yet assigned a register for the length
2771 * of the AVS header; allocate one.
2773 reg_off_ll
= alloc_reg();
2777 * Load the register containing the AVS length
2778 * into the X register.
2780 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2781 s
->s
.k
= reg_off_ll
;
2785 static struct slist
*
2786 gen_radiotap_llprefixlen(void)
2790 if (reg_off_ll
== -1) {
2792 * We haven't yet assigned a register for the length
2793 * of the radiotap header; allocate one.
2795 reg_off_ll
= alloc_reg();
2799 * Load the register containing the radiotap length
2800 * into the X register.
2802 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2803 s
->s
.k
= reg_off_ll
;
2808 * At the moment we treat PPI as normal Radiotap encoded
2809 * packets. The difference is in the function that generates
2810 * the code at the beginning to compute the header length.
2811 * Since this code generator of PPI supports bare 802.11
2812 * encapsulation only (i.e. the encapsulated DLT should be
2813 * DLT_IEEE802_11) we generate code to check for this too.
2815 static struct slist
*
2816 gen_ppi_llprefixlen(void)
2820 if (reg_off_ll
== -1) {
2822 * We haven't yet assigned a register for the length
2823 * of the radiotap header; allocate one.
2825 reg_off_ll
= alloc_reg();
2829 * Load the register containing the PPI length
2830 * into the X register.
2832 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2833 s
->s
.k
= reg_off_ll
;
2838 * Generate code to compute the link-layer header length, if necessary,
2839 * putting it into the X register, and to return either a pointer to a
2840 * "struct slist" for the list of statements in that code, or NULL if
2841 * no code is necessary.
2843 static struct slist
*
2844 gen_llprefixlen(void)
2848 case DLT_PRISM_HEADER
:
2849 return gen_prism_llprefixlen();
2851 case DLT_IEEE802_11_RADIO_AVS
:
2852 return gen_avs_llprefixlen();
2854 case DLT_IEEE802_11_RADIO
:
2855 return gen_radiotap_llprefixlen();
2858 return gen_ppi_llprefixlen();
2866 * Generate code to compute the outer link-layer header length, if necessary,
2867 * putting it into the X register, and to return either a pointer to a
2868 * "struct slist" for the list of statements in that code, or NULL if
2869 * no code is necessary.
2871 static struct slist
*
2872 gen_outerllprefixlen(void)
2874 switch (outerlinktype
) {
2876 case DLT_PRISM_HEADER
:
2877 return gen_prism_llprefixlen();
2879 case DLT_IEEE802_11_RADIO_AVS
:
2880 return gen_avs_llprefixlen();
2882 case DLT_IEEE802_11_RADIO
:
2883 return gen_radiotap_llprefixlen();
2886 return gen_ppi_llprefixlen();
2894 * Generate code to load the register containing the variable part of
2895 * the offset of the link-layer payload into the X register; if no
2896 * register for that offset has been allocated, allocate it first.
2897 * (The code to set that register will be generated later, but will
2898 * be placed earlier in the code sequence.)
2900 static struct slist
*
2901 gen_off_linkpl(void)
2905 if (off_linkpl_is_variable
) {
2906 if (reg_off_linkpl
== -1) {
2908 * We haven't yet assigned a register for the
2909 * variable part of the offset of the link-layer
2910 * payload; allocate one.
2912 reg_off_linkpl
= alloc_reg();
2916 * Load the register containing the variable part of the
2917 * offset of the link-layer payload into the X register.
2919 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2920 s
->s
.k
= reg_off_linkpl
;
2924 * That offset isn't variable, there's no variable part,
2925 * so we don't need to generate any code.
2932 * Map an Ethernet type to the equivalent PPP type.
2935 ethertype_to_ppptype(proto
)
2944 case ETHERTYPE_IPV6
:
2952 case ETHERTYPE_ATALK
:
2966 * I'm assuming the "Bridging PDU"s that go
2967 * over PPP are Spanning Tree Protocol
2981 * Generate any tests that, for encapsulation of a link-layer packet
2982 * inside another protocol stack, need to be done to check for those
2983 * link-layer packets (and that haven't already been done by a check
2984 * for that encapsulation).
2986 static struct block
*
2987 gen_outerll_check(void)
2991 switch (outerlinktype
) {
2995 * This is LANE-encapsulated Ethernet; check that the LANE
2996 * packet doesn't begin with an LE Control marker, i.e.
2997 * that it's data, not a control message.
2999 * (We've already generated a test for LANE.)
3001 b0
= gen_cmp(OR_OUTERLINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3007 * No such tests are necessary.
3015 * Generate code to match a particular packet type by matching the
3016 * link-layer type field or fields in the 802.2 LLC header.
3018 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3019 * value, if <= ETHERMTU.
3021 static struct block
*
3025 struct block
*b0
, *b1
, *b2
;
3026 const char *description
;
3028 /* are we checking MPLS-encapsulated packets? */
3029 if (label_stack_depth
> 0) {
3033 /* FIXME add other L3 proto IDs */
3034 return gen_mpls_linktype(Q_IP
);
3036 case ETHERTYPE_IPV6
:
3038 /* FIXME add other L3 proto IDs */
3039 return gen_mpls_linktype(Q_IPV6
);
3042 bpf_error("unsupported protocol over mpls");
3050 case DLT_NETANALYZER
:
3051 case DLT_NETANALYZER_TRANSPARENT
:
3052 b0
= gen_outerll_check();
3053 b1
= gen_ether_linktype(proto
);
3064 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3068 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3075 case DLT_IEEE802_11
:
3076 case DLT_PRISM_HEADER
:
3077 case DLT_IEEE802_11_RADIO_AVS
:
3078 case DLT_IEEE802_11_RADIO
:
3081 * Check that we have a data frame.
3083 b0
= gen_check_802_11_data_frame();
3086 * Now check for the specified link-layer type.
3088 b1
= gen_llc_linktype(proto
);
3096 * XXX - check for LLC frames.
3098 return gen_llc_linktype(proto
);
3104 * XXX - check for LLC PDUs, as per IEEE 802.5.
3106 return gen_llc_linktype(proto
);
3110 case DLT_ATM_RFC1483
:
3112 case DLT_IP_OVER_FC
:
3113 return gen_llc_linktype(proto
);
3119 * Check for an LLC-encapsulated version of this protocol;
3120 * if we were checking for LANE, linktype would no longer
3123 * Check for LLC encapsulation and then check the protocol.
3125 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3126 b1
= gen_llc_linktype(proto
);
3133 return gen_linux_sll_linktype(proto
);
3138 case DLT_SLIP_BSDOS
:
3141 * These types don't provide any type field; packets
3142 * are always IPv4 or IPv6.
3144 * XXX - for IPv4, check for a version number of 4, and,
3145 * for IPv6, check for a version number of 6?
3150 /* Check for a version number of 4. */
3151 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3153 case ETHERTYPE_IPV6
:
3154 /* Check for a version number of 6. */
3155 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3158 return gen_false(); /* always false */
3165 * Raw IPv4, so no type field.
3167 if (proto
== ETHERTYPE_IP
)
3168 return gen_true(); /* always true */
3170 /* Checking for something other than IPv4; always false */
3177 * Raw IPv6, so no type field.
3179 if (proto
== ETHERTYPE_IPV6
)
3180 return gen_true(); /* always true */
3182 /* Checking for something other than IPv6; always false */
3189 case DLT_PPP_SERIAL
:
3192 * We use Ethernet protocol types inside libpcap;
3193 * map them to the corresponding PPP protocol types.
3195 proto
= ethertype_to_ppptype(proto
);
3196 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3202 * We use Ethernet protocol types inside libpcap;
3203 * map them to the corresponding PPP protocol types.
3209 * Also check for Van Jacobson-compressed IP.
3210 * XXX - do this for other forms of PPP?
3212 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3213 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3215 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3220 proto
= ethertype_to_ppptype(proto
);
3221 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3231 * For DLT_NULL, the link-layer header is a 32-bit
3232 * word containing an AF_ value in *host* byte order,
3233 * and for DLT_ENC, the link-layer header begins
3234 * with a 32-bit work containing an AF_ value in
3237 * In addition, if we're reading a saved capture file,
3238 * the host byte order in the capture may not be the
3239 * same as the host byte order on this machine.
3241 * For DLT_LOOP, the link-layer header is a 32-bit
3242 * word containing an AF_ value in *network* byte order.
3244 * XXX - AF_ values may, unfortunately, be platform-
3245 * dependent; for example, FreeBSD's AF_INET6 is 24
3246 * whilst NetBSD's and OpenBSD's is 26.
3248 * This means that, when reading a capture file, just
3249 * checking for our AF_INET6 value won't work if the
3250 * capture file came from another OS.
3259 case ETHERTYPE_IPV6
:
3266 * Not a type on which we support filtering.
3267 * XXX - support those that have AF_ values
3268 * #defined on this platform, at least?
3273 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3275 * The AF_ value is in host byte order, but
3276 * the BPF interpreter will convert it to
3277 * network byte order.
3279 * If this is a save file, and it's from a
3280 * machine with the opposite byte order to
3281 * ours, we byte-swap the AF_ value.
3283 * Then we run it through "htonl()", and
3284 * generate code to compare against the result.
3286 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3287 proto
= SWAPLONG(proto
);
3288 proto
= htonl(proto
);
3290 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3292 #ifdef HAVE_NET_PFVAR_H
3295 * af field is host byte order in contrast to the rest of
3298 if (proto
== ETHERTYPE_IP
)
3299 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3300 BPF_B
, (bpf_int32
)AF_INET
));
3301 else if (proto
== ETHERTYPE_IPV6
)
3302 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3303 BPF_B
, (bpf_int32
)AF_INET6
));
3308 #endif /* HAVE_NET_PFVAR_H */
3311 case DLT_ARCNET_LINUX
:
3313 * XXX should we check for first fragment if the protocol
3321 case ETHERTYPE_IPV6
:
3322 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3323 (bpf_int32
)ARCTYPE_INET6
));
3326 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3327 (bpf_int32
)ARCTYPE_IP
);
3328 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3329 (bpf_int32
)ARCTYPE_IP_OLD
);
3334 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3335 (bpf_int32
)ARCTYPE_ARP
);
3336 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3337 (bpf_int32
)ARCTYPE_ARP_OLD
);
3341 case ETHERTYPE_REVARP
:
3342 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3343 (bpf_int32
)ARCTYPE_REVARP
));
3345 case ETHERTYPE_ATALK
:
3346 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3347 (bpf_int32
)ARCTYPE_ATALK
));
3354 case ETHERTYPE_ATALK
:
3364 * XXX - assumes a 2-byte Frame Relay header with
3365 * DLCI and flags. What if the address is longer?
3371 * Check for the special NLPID for IP.
3373 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3375 case ETHERTYPE_IPV6
:
3377 * Check for the special NLPID for IPv6.
3379 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3383 * Check for several OSI protocols.
3385 * Frame Relay packets typically have an OSI
3386 * NLPID at the beginning; we check for each
3389 * What we check for is the NLPID and a frame
3390 * control field of UI, i.e. 0x03 followed
3393 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3394 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3395 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3407 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3409 case DLT_JUNIPER_MFR
:
3410 case DLT_JUNIPER_MLFR
:
3411 case DLT_JUNIPER_MLPPP
:
3412 case DLT_JUNIPER_ATM1
:
3413 case DLT_JUNIPER_ATM2
:
3414 case DLT_JUNIPER_PPPOE
:
3415 case DLT_JUNIPER_PPPOE_ATM
:
3416 case DLT_JUNIPER_GGSN
:
3417 case DLT_JUNIPER_ES
:
3418 case DLT_JUNIPER_MONITOR
:
3419 case DLT_JUNIPER_SERVICES
:
3420 case DLT_JUNIPER_ETHER
:
3421 case DLT_JUNIPER_PPP
:
3422 case DLT_JUNIPER_FRELAY
:
3423 case DLT_JUNIPER_CHDLC
:
3424 case DLT_JUNIPER_VP
:
3425 case DLT_JUNIPER_ST
:
3426 case DLT_JUNIPER_ISM
:
3427 case DLT_JUNIPER_VS
:
3428 case DLT_JUNIPER_SRX_E2E
:
3429 case DLT_JUNIPER_FIBRECHANNEL
:
3430 case DLT_JUNIPER_ATM_CEMIC
:
3432 /* just lets verify the magic number for now -
3433 * on ATM we may have up to 6 different encapsulations on the wire
3434 * and need a lot of heuristics to figure out that the payload
3437 * FIXME encapsulation specific BPF_ filters
3439 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3441 case DLT_BACNET_MS_TP
:
3442 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3445 return gen_ipnet_linktype(proto
);
3447 case DLT_LINUX_IRDA
:
3448 bpf_error("IrDA link-layer type filtering not implemented");
3451 bpf_error("DOCSIS link-layer type filtering not implemented");
3454 case DLT_MTP2_WITH_PHDR
:
3455 bpf_error("MTP2 link-layer type filtering not implemented");
3458 bpf_error("ERF link-layer type filtering not implemented");
3461 bpf_error("PFSYNC link-layer type filtering not implemented");
3463 case DLT_LINUX_LAPD
:
3464 bpf_error("LAPD link-layer type filtering not implemented");
3468 case DLT_USB_LINUX_MMAPPED
:
3469 bpf_error("USB link-layer type filtering not implemented");
3471 case DLT_BLUETOOTH_HCI_H4
:
3472 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3473 bpf_error("Bluetooth link-layer type filtering not implemented");
3476 case DLT_CAN_SOCKETCAN
:
3477 bpf_error("CAN link-layer type filtering not implemented");
3479 case DLT_IEEE802_15_4
:
3480 case DLT_IEEE802_15_4_LINUX
:
3481 case DLT_IEEE802_15_4_NONASK_PHY
:
3482 case DLT_IEEE802_15_4_NOFCS
:
3483 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3485 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3486 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3489 bpf_error("SITA link-layer type filtering not implemented");
3492 bpf_error("RAIF1 link-layer type filtering not implemented");
3495 bpf_error("IPMB link-layer type filtering not implemented");
3498 bpf_error("AX.25 link-layer type filtering not implemented");
3501 /* Using the fixed-size NFLOG header it is possible to tell only
3502 * the address family of the packet, other meaningful data is
3503 * either missing or behind TLVs.
3505 bpf_error("NFLOG link-layer type filtering not implemented");
3509 * Does this link-layer header type have a field
3510 * indicating the type of the next protocol? If
3511 * so, off_linktype will be the offset of that
3512 * field in the packet; if not, it will be -1.
3514 if (off_linktype
!= (u_int
)-1) {
3516 * Yes; assume it's an Ethernet type. (If
3517 * it's not, it needs to be handled specially
3520 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3523 * No; report an error.
3525 description
= pcap_datalink_val_to_description(linktype
);
3526 if (description
!= NULL
) {
3527 bpf_error("%s link-layer type filtering not implemented",
3530 bpf_error("DLT %u link-layer type filtering not implemented",
3539 * Check for an LLC SNAP packet with a given organization code and
3540 * protocol type; we check the entire contents of the 802.2 LLC and
3541 * snap headers, checking for DSAP and SSAP of SNAP and a control
3542 * field of 0x03 in the LLC header, and for the specified organization
3543 * code and protocol type in the SNAP header.
3545 static struct block
*
3546 gen_snap(orgcode
, ptype
)
3547 bpf_u_int32 orgcode
;
3550 u_char snapblock
[8];
3552 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3553 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3554 snapblock
[2] = 0x03; /* control = UI */
3555 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3556 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3557 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3558 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3559 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3560 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3564 * Generate code to match frames with an LLC header.
3569 struct block
*b0
, *b1
;
3575 * We check for an Ethernet type field less than
3576 * 1500, which means it's an 802.3 length field.
3578 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
3582 * Now check for the purported DSAP and SSAP not being
3583 * 0xFF, to rule out NetWare-over-802.3.
3585 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3592 * We check for LLC traffic.
3594 b0
= gen_atmtype_abbrev(A_LLC
);
3597 case DLT_IEEE802
: /* Token Ring */
3599 * XXX - check for LLC frames.
3605 * XXX - check for LLC frames.
3609 case DLT_ATM_RFC1483
:
3611 * For LLC encapsulation, these are defined to have an
3614 * For VC encapsulation, they don't, but there's no
3615 * way to check for that; the protocol used on the VC
3616 * is negotiated out of band.
3620 case DLT_IEEE802_11
:
3621 case DLT_PRISM_HEADER
:
3622 case DLT_IEEE802_11_RADIO
:
3623 case DLT_IEEE802_11_RADIO_AVS
:
3626 * Check that we have a data frame.
3628 b0
= gen_check_802_11_data_frame();
3632 bpf_error("'llc' not supported for linktype %d", linktype
);
3640 struct block
*b0
, *b1
;
3644 * Check whether this is an LLC frame.
3649 * Load the control byte and test the low-order bit; it must
3650 * be clear for I frames.
3652 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3653 b1
= new_block(JMP(BPF_JSET
));
3664 struct block
*b0
, *b1
;
3667 * Check whether this is an LLC frame.
3672 * Now compare the low-order 2 bit of the control byte against
3673 * the appropriate value for S frames.
3675 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3683 struct block
*b0
, *b1
;
3686 * Check whether this is an LLC frame.
3691 * Now compare the low-order 2 bit of the control byte against
3692 * the appropriate value for U frames.
3694 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3700 gen_llc_s_subtype(bpf_u_int32 subtype
)
3702 struct block
*b0
, *b1
;
3705 * Check whether this is an LLC frame.
3710 * Now check for an S frame with the appropriate type.
3712 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3718 gen_llc_u_subtype(bpf_u_int32 subtype
)
3720 struct block
*b0
, *b1
;
3723 * Check whether this is an LLC frame.
3728 * Now check for a U frame with the appropriate type.
3730 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3736 * Generate code to match a particular packet type, for link-layer types
3737 * using 802.2 LLC headers.
3739 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3740 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3742 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3743 * value, if <= ETHERMTU. We use that to determine whether to
3744 * match the DSAP or both DSAP and LSAP or to check the OUI and
3745 * protocol ID in a SNAP header.
3747 static struct block
*
3748 gen_llc_linktype(proto
)
3752 * XXX - handle token-ring variable-length header.
3758 case LLCSAP_NETBEUI
:
3760 * XXX - should we check both the DSAP and the
3761 * SSAP, like this, or should we check just the
3762 * DSAP, as we do for other SAP values?
3764 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3765 ((proto
<< 8) | proto
));
3769 * XXX - are there ever SNAP frames for IPX on
3770 * non-Ethernet 802.x networks?
3772 return gen_cmp(OR_LLC
, 0, BPF_B
,
3773 (bpf_int32
)LLCSAP_IPX
);
3775 case ETHERTYPE_ATALK
:
3777 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3778 * SNAP packets with an organization code of
3779 * 0x080007 (Apple, for Appletalk) and a protocol
3780 * type of ETHERTYPE_ATALK (Appletalk).
3782 * XXX - check for an organization code of
3783 * encapsulated Ethernet as well?
3785 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3789 * XXX - we don't have to check for IPX 802.3
3790 * here, but should we check for the IPX Ethertype?
3792 if (proto
<= ETHERMTU
) {
3794 * This is an LLC SAP value, so check
3797 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3800 * This is an Ethernet type; we assume that it's
3801 * unlikely that it'll appear in the right place
3802 * at random, and therefore check only the
3803 * location that would hold the Ethernet type
3804 * in a SNAP frame with an organization code of
3805 * 0x000000 (encapsulated Ethernet).
3807 * XXX - if we were to check for the SNAP DSAP and
3808 * LSAP, as per XXX, and were also to check for an
3809 * organization code of 0x000000 (encapsulated
3810 * Ethernet), we'd do
3812 * return gen_snap(0x000000, proto);
3814 * here; for now, we don't, as per the above.
3815 * I don't know whether it's worth the extra CPU
3816 * time to do the right check or not.
3818 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3823 static struct block
*
3824 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3828 u_int src_off
, dst_off
;
3830 struct block
*b0
, *b1
;
3844 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3845 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3851 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3852 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3859 b0
= gen_linktype(proto
);
3860 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3866 static struct block
*
3867 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3868 struct in6_addr
*addr
;
3869 struct in6_addr
*mask
;
3871 u_int src_off
, dst_off
;
3873 struct block
*b0
, *b1
;
3888 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3889 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3895 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3896 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3903 /* this order is important */
3904 a
= (u_int32_t
*)addr
;
3905 m
= (u_int32_t
*)mask
;
3906 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3907 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3909 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3911 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3913 b0
= gen_linktype(proto
);
3919 static struct block
*
3920 gen_ehostop(eaddr
, dir
)
3921 register const u_char
*eaddr
;
3924 register struct block
*b0
, *b1
;
3928 return gen_bcmp(OR_LINK
, 6, 6, eaddr
);
3931 return gen_bcmp(OR_LINK
, 0, 6, eaddr
);
3934 b0
= gen_ehostop(eaddr
, Q_SRC
);
3935 b1
= gen_ehostop(eaddr
, Q_DST
);
3941 b0
= gen_ehostop(eaddr
, Q_SRC
);
3942 b1
= gen_ehostop(eaddr
, Q_DST
);
3947 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3951 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3955 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3959 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3963 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3967 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3975 * Like gen_ehostop, but for DLT_FDDI
3977 static struct block
*
3978 gen_fhostop(eaddr
, dir
)
3979 register const u_char
*eaddr
;
3982 struct block
*b0
, *b1
;
3986 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3989 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3992 b0
= gen_fhostop(eaddr
, Q_SRC
);
3993 b1
= gen_fhostop(eaddr
, Q_DST
);
3999 b0
= gen_fhostop(eaddr
, Q_SRC
);
4000 b1
= gen_fhostop(eaddr
, Q_DST
);
4005 bpf_error("'addr1' is only supported on 802.11");
4009 bpf_error("'addr2' is only supported on 802.11");
4013 bpf_error("'addr3' is only supported on 802.11");
4017 bpf_error("'addr4' is only supported on 802.11");
4021 bpf_error("'ra' is only supported on 802.11");
4025 bpf_error("'ta' is only supported on 802.11");
4033 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4035 static struct block
*
4036 gen_thostop(eaddr
, dir
)
4037 register const u_char
*eaddr
;
4040 register struct block
*b0
, *b1
;
4044 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
4047 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4050 b0
= gen_thostop(eaddr
, Q_SRC
);
4051 b1
= gen_thostop(eaddr
, Q_DST
);
4057 b0
= gen_thostop(eaddr
, Q_SRC
);
4058 b1
= gen_thostop(eaddr
, Q_DST
);
4063 bpf_error("'addr1' is only supported on 802.11");
4067 bpf_error("'addr2' is only supported on 802.11");
4071 bpf_error("'addr3' is only supported on 802.11");
4075 bpf_error("'addr4' is only supported on 802.11");
4079 bpf_error("'ra' is only supported on 802.11");
4083 bpf_error("'ta' is only supported on 802.11");
4091 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4092 * various 802.11 + radio headers.
4094 static struct block
*
4095 gen_wlanhostop(eaddr
, dir
)
4096 register const u_char
*eaddr
;
4099 register struct block
*b0
, *b1
, *b2
;
4100 register struct slist
*s
;
4102 #ifdef ENABLE_WLAN_FILTERING_PATCH
4105 * We need to disable the optimizer because the optimizer is buggy
4106 * and wipes out some LD instructions generated by the below
4107 * code to validate the Frame Control bits
4110 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4117 * For control frames, there is no SA.
4119 * For management frames, SA is at an
4120 * offset of 10 from the beginning of
4123 * For data frames, SA is at an offset
4124 * of 10 from the beginning of the packet
4125 * if From DS is clear, at an offset of
4126 * 16 from the beginning of the packet
4127 * if From DS is set and To DS is clear,
4128 * and an offset of 24 from the beginning
4129 * of the packet if From DS is set and To DS
4134 * Generate the tests to be done for data frames
4137 * First, check for To DS set, i.e. check "link[1] & 0x01".
4139 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4140 b1
= new_block(JMP(BPF_JSET
));
4141 b1
->s
.k
= 0x01; /* To DS */
4145 * If To DS is set, the SA is at 24.
4147 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4151 * Now, check for To DS not set, i.e. check
4152 * "!(link[1] & 0x01)".
4154 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4155 b2
= new_block(JMP(BPF_JSET
));
4156 b2
->s
.k
= 0x01; /* To DS */
4161 * If To DS is not set, the SA is at 16.
4163 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4167 * Now OR together the last two checks. That gives
4168 * the complete set of checks for data frames with
4174 * Now check for From DS being set, and AND that with
4175 * the ORed-together checks.
4177 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4178 b1
= new_block(JMP(BPF_JSET
));
4179 b1
->s
.k
= 0x02; /* From DS */
4184 * Now check for data frames with From DS not set.
4186 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4187 b2
= new_block(JMP(BPF_JSET
));
4188 b2
->s
.k
= 0x02; /* From DS */
4193 * If From DS isn't set, the SA is at 10.
4195 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4199 * Now OR together the checks for data frames with
4200 * From DS not set and for data frames with From DS
4201 * set; that gives the checks done for data frames.
4206 * Now check for a data frame.
4207 * I.e, check "link[0] & 0x08".
4209 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4210 b1
= new_block(JMP(BPF_JSET
));
4215 * AND that with the checks done for data frames.
4220 * If the high-order bit of the type value is 0, this
4221 * is a management frame.
4222 * I.e, check "!(link[0] & 0x08)".
4224 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4225 b2
= new_block(JMP(BPF_JSET
));
4231 * For management frames, the SA is at 10.
4233 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4237 * OR that with the checks done for data frames.
4238 * That gives the checks done for management and
4244 * If the low-order bit of the type value is 1,
4245 * this is either a control frame or a frame
4246 * with a reserved type, and thus not a
4249 * I.e., check "!(link[0] & 0x04)".
4251 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4252 b1
= new_block(JMP(BPF_JSET
));
4258 * AND that with the checks for data and management
4268 * For control frames, there is no DA.
4270 * For management frames, DA is at an
4271 * offset of 4 from the beginning of
4274 * For data frames, DA is at an offset
4275 * of 4 from the beginning of the packet
4276 * if To DS is clear and at an offset of
4277 * 16 from the beginning of the packet
4282 * Generate the tests to be done for data frames.
4284 * First, check for To DS set, i.e. "link[1] & 0x01".
4286 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4287 b1
= new_block(JMP(BPF_JSET
));
4288 b1
->s
.k
= 0x01; /* To DS */
4292 * If To DS is set, the DA is at 16.
4294 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4298 * Now, check for To DS not set, i.e. check
4299 * "!(link[1] & 0x01)".
4301 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4302 b2
= new_block(JMP(BPF_JSET
));
4303 b2
->s
.k
= 0x01; /* To DS */
4308 * If To DS is not set, the DA is at 4.
4310 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4314 * Now OR together the last two checks. That gives
4315 * the complete set of checks for data frames.
4320 * Now check for a data frame.
4321 * I.e, check "link[0] & 0x08".
4323 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4324 b1
= new_block(JMP(BPF_JSET
));
4329 * AND that with the checks done for data frames.
4334 * If the high-order bit of the type value is 0, this
4335 * is a management frame.
4336 * I.e, check "!(link[0] & 0x08)".
4338 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4339 b2
= new_block(JMP(BPF_JSET
));
4345 * For management frames, the DA is at 4.
4347 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4351 * OR that with the checks done for data frames.
4352 * That gives the checks done for management and
4358 * If the low-order bit of the type value is 1,
4359 * this is either a control frame or a frame
4360 * with a reserved type, and thus not a
4363 * I.e., check "!(link[0] & 0x04)".
4365 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4366 b1
= new_block(JMP(BPF_JSET
));
4372 * AND that with the checks for data and management
4380 * Not present in management frames; addr1 in other
4385 * If the high-order bit of the type value is 0, this
4386 * is a management frame.
4387 * I.e, check "(link[0] & 0x08)".
4389 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4390 b1
= new_block(JMP(BPF_JSET
));
4397 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4400 * AND that with the check of addr1.
4407 * Not present in management frames; addr2, if present,
4412 * Not present in CTS or ACK control frames.
4414 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4415 IEEE80211_FC0_TYPE_MASK
);
4417 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4418 IEEE80211_FC0_SUBTYPE_MASK
);
4420 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4421 IEEE80211_FC0_SUBTYPE_MASK
);
4427 * If the high-order bit of the type value is 0, this
4428 * is a management frame.
4429 * I.e, check "(link[0] & 0x08)".
4431 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4432 b1
= new_block(JMP(BPF_JSET
));
4437 * AND that with the check for frames other than
4438 * CTS and ACK frames.
4445 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4450 * XXX - add BSSID keyword?
4453 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4457 * Not present in CTS or ACK control frames.
4459 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4460 IEEE80211_FC0_TYPE_MASK
);
4462 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4463 IEEE80211_FC0_SUBTYPE_MASK
);
4465 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4466 IEEE80211_FC0_SUBTYPE_MASK
);
4470 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4476 * Not present in control frames.
4478 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4479 IEEE80211_FC0_TYPE_MASK
);
4481 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4487 * Present only if the direction mask has both "From DS"
4488 * and "To DS" set. Neither control frames nor management
4489 * frames should have both of those set, so we don't
4490 * check the frame type.
4492 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4493 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4494 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4499 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4500 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4506 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4507 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4516 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4517 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4518 * as the RFC states.)
4520 static struct block
*
4521 gen_ipfchostop(eaddr
, dir
)
4522 register const u_char
*eaddr
;
4525 register struct block
*b0
, *b1
;
4529 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4532 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4535 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4536 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4542 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4543 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4548 bpf_error("'addr1' is only supported on 802.11");
4552 bpf_error("'addr2' is only supported on 802.11");
4556 bpf_error("'addr3' is only supported on 802.11");
4560 bpf_error("'addr4' is only supported on 802.11");
4564 bpf_error("'ra' is only supported on 802.11");
4568 bpf_error("'ta' is only supported on 802.11");
4576 * This is quite tricky because there may be pad bytes in front of the
4577 * DECNET header, and then there are two possible data packet formats that
4578 * carry both src and dst addresses, plus 5 packet types in a format that
4579 * carries only the src node, plus 2 types that use a different format and
4580 * also carry just the src node.
4584 * Instead of doing those all right, we just look for data packets with
4585 * 0 or 1 bytes of padding. If you want to look at other packets, that
4586 * will require a lot more hacking.
4588 * To add support for filtering on DECNET "areas" (network numbers)
4589 * one would want to add a "mask" argument to this routine. That would
4590 * make the filter even more inefficient, although one could be clever
4591 * and not generate masking instructions if the mask is 0xFFFF.
4593 static struct block
*
4594 gen_dnhostop(addr
, dir
)
4598 struct block
*b0
, *b1
, *b2
, *tmp
;
4599 u_int offset_lh
; /* offset if long header is received */
4600 u_int offset_sh
; /* offset if short header is received */
4605 offset_sh
= 1; /* follows flags */
4606 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4610 offset_sh
= 3; /* follows flags, dstnode */
4611 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4615 /* Inefficient because we do our Calvinball dance twice */
4616 b0
= gen_dnhostop(addr
, Q_SRC
);
4617 b1
= gen_dnhostop(addr
, Q_DST
);
4623 /* Inefficient because we do our Calvinball dance twice */
4624 b0
= gen_dnhostop(addr
, Q_SRC
);
4625 b1
= gen_dnhostop(addr
, Q_DST
);
4630 bpf_error("ISO host filtering not implemented");
4635 b0
= gen_linktype(ETHERTYPE_DN
);
4636 /* Check for pad = 1, long header case */
4637 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4638 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4639 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4640 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4642 /* Check for pad = 0, long header case */
4643 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4644 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4647 /* Check for pad = 1, short header case */
4648 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4649 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4650 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4653 /* Check for pad = 0, short header case */
4654 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4655 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4659 /* Combine with test for linktype */
4665 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4666 * test the bottom-of-stack bit, and then check the version number
4667 * field in the IP header.
4669 static struct block
*
4670 gen_mpls_linktype(proto
)
4673 struct block
*b0
, *b1
;
4678 /* match the bottom-of-stack bit */
4679 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4680 /* match the IPv4 version number */
4681 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4686 /* match the bottom-of-stack bit */
4687 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4688 /* match the IPv4 version number */
4689 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4698 static struct block
*
4699 gen_host(addr
, mask
, proto
, dir
, type
)
4706 struct block
*b0
, *b1
;
4707 const char *typestr
;
4717 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4719 * Only check for non-IPv4 addresses if we're not
4720 * checking MPLS-encapsulated packets.
4722 if (label_stack_depth
== 0) {
4723 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4725 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4731 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4734 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4737 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4740 bpf_error("'tcp' modifier applied to %s", typestr
);
4743 bpf_error("'sctp' modifier applied to %s", typestr
);
4746 bpf_error("'udp' modifier applied to %s", typestr
);
4749 bpf_error("'icmp' modifier applied to %s", typestr
);
4752 bpf_error("'igmp' modifier applied to %s", typestr
);
4755 bpf_error("'igrp' modifier applied to %s", typestr
);
4758 bpf_error("'pim' modifier applied to %s", typestr
);
4761 bpf_error("'vrrp' modifier applied to %s", typestr
);
4764 bpf_error("'carp' modifier applied to %s", typestr
);
4767 bpf_error("ATALK host filtering not implemented");
4770 bpf_error("AARP host filtering not implemented");
4773 return gen_dnhostop(addr
, dir
);
4776 bpf_error("SCA host filtering not implemented");
4779 bpf_error("LAT host filtering not implemented");
4782 bpf_error("MOPDL host filtering not implemented");
4785 bpf_error("MOPRC host filtering not implemented");
4788 bpf_error("'ip6' modifier applied to ip host");
4791 bpf_error("'icmp6' modifier applied to %s", typestr
);
4794 bpf_error("'ah' modifier applied to %s", typestr
);
4797 bpf_error("'esp' modifier applied to %s", typestr
);
4800 bpf_error("ISO host filtering not implemented");
4803 bpf_error("'esis' modifier applied to %s", typestr
);
4806 bpf_error("'isis' modifier applied to %s", typestr
);
4809 bpf_error("'clnp' modifier applied to %s", typestr
);
4812 bpf_error("'stp' modifier applied to %s", typestr
);
4815 bpf_error("IPX host filtering not implemented");
4818 bpf_error("'netbeui' modifier applied to %s", typestr
);
4821 bpf_error("'radio' modifier applied to %s", typestr
);
4830 static struct block
*
4831 gen_host6(addr
, mask
, proto
, dir
, type
)
4832 struct in6_addr
*addr
;
4833 struct in6_addr
*mask
;
4838 const char *typestr
;
4848 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4851 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4854 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4857 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4860 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4863 bpf_error("'sctp' modifier applied to %s", typestr
);
4866 bpf_error("'tcp' modifier applied to %s", typestr
);
4869 bpf_error("'udp' modifier applied to %s", typestr
);
4872 bpf_error("'icmp' modifier applied to %s", typestr
);
4875 bpf_error("'igmp' modifier applied to %s", typestr
);
4878 bpf_error("'igrp' modifier applied to %s", typestr
);
4881 bpf_error("'pim' modifier applied to %s", typestr
);
4884 bpf_error("'vrrp' modifier applied to %s", typestr
);
4887 bpf_error("'carp' modifier applied to %s", typestr
);
4890 bpf_error("ATALK host filtering not implemented");
4893 bpf_error("AARP host filtering not implemented");
4896 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4899 bpf_error("SCA host filtering not implemented");
4902 bpf_error("LAT host filtering not implemented");
4905 bpf_error("MOPDL host filtering not implemented");
4908 bpf_error("MOPRC host filtering not implemented");
4911 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4914 bpf_error("'icmp6' modifier applied to %s", typestr
);
4917 bpf_error("'ah' modifier applied to %s", typestr
);
4920 bpf_error("'esp' modifier applied to %s", typestr
);
4923 bpf_error("ISO host filtering not implemented");
4926 bpf_error("'esis' modifier applied to %s", typestr
);
4929 bpf_error("'isis' modifier applied to %s", typestr
);
4932 bpf_error("'clnp' modifier applied to %s", typestr
);
4935 bpf_error("'stp' modifier applied to %s", typestr
);
4938 bpf_error("IPX host filtering not implemented");
4941 bpf_error("'netbeui' modifier applied to %s", typestr
);
4944 bpf_error("'radio' modifier applied to %s", typestr
);
4954 static struct block
*
4955 gen_gateway(eaddr
, alist
, proto
, dir
)
4956 const u_char
*eaddr
;
4957 bpf_u_int32
**alist
;
4961 struct block
*b0
, *b1
, *tmp
;
4964 bpf_error("direction applied to 'gateway'");
4973 case DLT_NETANALYZER
:
4974 case DLT_NETANALYZER_TRANSPARENT
:
4975 b1
= gen_outerll_check();
4976 b0
= gen_ehostop(eaddr
, Q_OR
);
4981 b0
= gen_fhostop(eaddr
, Q_OR
);
4984 b0
= gen_thostop(eaddr
, Q_OR
);
4986 case DLT_IEEE802_11
:
4987 case DLT_PRISM_HEADER
:
4988 case DLT_IEEE802_11_RADIO_AVS
:
4989 case DLT_IEEE802_11_RADIO
:
4991 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4995 * This is LLC-multiplexed traffic; if it were
4996 * LANE, linktype would have been set to
5000 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5002 case DLT_IP_OVER_FC
:
5003 b0
= gen_ipfchostop(eaddr
, Q_OR
);
5007 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5009 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
5011 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
5020 bpf_error("illegal modifier of 'gateway'");
5026 gen_proto_abbrev(proto
)
5035 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5036 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5041 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5042 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5047 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5048 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5053 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5056 #ifndef IPPROTO_IGMP
5057 #define IPPROTO_IGMP 2
5061 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5064 #ifndef IPPROTO_IGRP
5065 #define IPPROTO_IGRP 9
5068 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5072 #define IPPROTO_PIM 103
5076 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5077 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5081 #ifndef IPPROTO_VRRP
5082 #define IPPROTO_VRRP 112
5086 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5089 #ifndef IPPROTO_CARP
5090 #define IPPROTO_CARP 112
5094 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5098 b1
= gen_linktype(ETHERTYPE_IP
);
5102 b1
= gen_linktype(ETHERTYPE_ARP
);
5106 b1
= gen_linktype(ETHERTYPE_REVARP
);
5110 bpf_error("link layer applied in wrong context");
5113 b1
= gen_linktype(ETHERTYPE_ATALK
);
5117 b1
= gen_linktype(ETHERTYPE_AARP
);
5121 b1
= gen_linktype(ETHERTYPE_DN
);
5125 b1
= gen_linktype(ETHERTYPE_SCA
);
5129 b1
= gen_linktype(ETHERTYPE_LAT
);
5133 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5137 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5141 b1
= gen_linktype(ETHERTYPE_IPV6
);
5144 #ifndef IPPROTO_ICMPV6
5145 #define IPPROTO_ICMPV6 58
5148 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5152 #define IPPROTO_AH 51
5155 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5156 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5161 #define IPPROTO_ESP 50
5164 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5165 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5170 b1
= gen_linktype(LLCSAP_ISONS
);
5174 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5178 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5181 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5182 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5183 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5185 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5187 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5189 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5193 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5194 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5195 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5197 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5199 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5201 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5205 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5206 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5207 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5209 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5214 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5215 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5220 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5221 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5223 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5225 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5230 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5231 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5236 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5237 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5242 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5246 b1
= gen_linktype(LLCSAP_8021D
);
5250 b1
= gen_linktype(LLCSAP_IPX
);
5254 b1
= gen_linktype(LLCSAP_NETBEUI
);
5258 bpf_error("'radio' is not a valid protocol type");
5266 static struct block
*
5272 /* not IPv4 frag other than the first frag */
5273 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5274 b
= new_block(JMP(BPF_JSET
));
5283 * Generate a comparison to a port value in the transport-layer header
5284 * at the specified offset from the beginning of that header.
5286 * XXX - this handles a variable-length prefix preceding the link-layer
5287 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5288 * variable-length link-layer headers (such as Token Ring or 802.11
5291 static struct block
*
5292 gen_portatom(off
, v
)
5296 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5299 static struct block
*
5300 gen_portatom6(off
, v
)
5304 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5308 gen_portop(port
, proto
, dir
)
5309 int port
, proto
, dir
;
5311 struct block
*b0
, *b1
, *tmp
;
5313 /* ip proto 'proto' and not a fragment other than the first fragment */
5314 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5320 b1
= gen_portatom(0, (bpf_int32
)port
);
5324 b1
= gen_portatom(2, (bpf_int32
)port
);
5329 tmp
= gen_portatom(0, (bpf_int32
)port
);
5330 b1
= gen_portatom(2, (bpf_int32
)port
);
5335 tmp
= gen_portatom(0, (bpf_int32
)port
);
5336 b1
= gen_portatom(2, (bpf_int32
)port
);
5348 static struct block
*
5349 gen_port(port
, ip_proto
, dir
)
5354 struct block
*b0
, *b1
, *tmp
;
5359 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5360 * not LLC encapsulation with LLCSAP_IP.
5362 * For IEEE 802 networks - which includes 802.5 token ring
5363 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5364 * says that SNAP encapsulation is used, not LLC encapsulation
5367 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5368 * RFC 2225 say that SNAP encapsulation is used, not LLC
5369 * encapsulation with LLCSAP_IP.
5371 * So we always check for ETHERTYPE_IP.
5373 b0
= gen_linktype(ETHERTYPE_IP
);
5379 b1
= gen_portop(port
, ip_proto
, dir
);
5383 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5384 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5386 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5398 gen_portop6(port
, proto
, dir
)
5399 int port
, proto
, dir
;
5401 struct block
*b0
, *b1
, *tmp
;
5403 /* ip6 proto 'proto' */
5404 /* XXX - catch the first fragment of a fragmented packet? */
5405 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5409 b1
= gen_portatom6(0, (bpf_int32
)port
);
5413 b1
= gen_portatom6(2, (bpf_int32
)port
);
5418 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5419 b1
= gen_portatom6(2, (bpf_int32
)port
);
5424 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5425 b1
= gen_portatom6(2, (bpf_int32
)port
);
5437 static struct block
*
5438 gen_port6(port
, ip_proto
, dir
)
5443 struct block
*b0
, *b1
, *tmp
;
5445 /* link proto ip6 */
5446 b0
= gen_linktype(ETHERTYPE_IPV6
);
5452 b1
= gen_portop6(port
, ip_proto
, dir
);
5456 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5457 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5459 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5470 /* gen_portrange code */
5471 static struct block
*
5472 gen_portrangeatom(off
, v1
, v2
)
5476 struct block
*b1
, *b2
;
5480 * Reverse the order of the ports, so v1 is the lower one.
5489 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5490 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5498 gen_portrangeop(port1
, port2
, proto
, dir
)
5503 struct block
*b0
, *b1
, *tmp
;
5505 /* ip proto 'proto' and not a fragment other than the first fragment */
5506 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5512 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5516 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5521 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5522 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5527 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5528 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5540 static struct block
*
5541 gen_portrange(port1
, port2
, ip_proto
, dir
)
5546 struct block
*b0
, *b1
, *tmp
;
5549 b0
= gen_linktype(ETHERTYPE_IP
);
5555 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5559 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5560 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5562 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5573 static struct block
*
5574 gen_portrangeatom6(off
, v1
, v2
)
5578 struct block
*b1
, *b2
;
5582 * Reverse the order of the ports, so v1 is the lower one.
5591 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5592 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5600 gen_portrangeop6(port1
, port2
, proto
, dir
)
5605 struct block
*b0
, *b1
, *tmp
;
5607 /* ip6 proto 'proto' */
5608 /* XXX - catch the first fragment of a fragmented packet? */
5609 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5613 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5617 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5622 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5623 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5628 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5629 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5641 static struct block
*
5642 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5647 struct block
*b0
, *b1
, *tmp
;
5649 /* link proto ip6 */
5650 b0
= gen_linktype(ETHERTYPE_IPV6
);
5656 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5660 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5661 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5663 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5675 lookup_proto(name
, proto
)
5676 register const char *name
;
5686 v
= pcap_nametoproto(name
);
5687 if (v
== PROTO_UNDEF
)
5688 bpf_error("unknown ip proto '%s'", name
);
5692 /* XXX should look up h/w protocol type based on linktype */
5693 v
= pcap_nametoeproto(name
);
5694 if (v
== PROTO_UNDEF
) {
5695 v
= pcap_nametollc(name
);
5696 if (v
== PROTO_UNDEF
)
5697 bpf_error("unknown ether proto '%s'", name
);
5702 if (strcmp(name
, "esis") == 0)
5704 else if (strcmp(name
, "isis") == 0)
5706 else if (strcmp(name
, "clnp") == 0)
5709 bpf_error("unknown osi proto '%s'", name
);
5729 static struct block
*
5730 gen_protochain(v
, proto
, dir
)
5735 #ifdef NO_PROTOCHAIN
5736 return gen_proto(v
, proto
, dir
);
5738 struct block
*b0
, *b
;
5739 struct slist
*s
[100];
5740 int fix2
, fix3
, fix4
, fix5
;
5741 int ahcheck
, again
, end
;
5743 int reg2
= alloc_reg();
5745 memset(s
, 0, sizeof(s
));
5746 fix2
= fix3
= fix4
= fix5
= 0;
5753 b0
= gen_protochain(v
, Q_IP
, dir
);
5754 b
= gen_protochain(v
, Q_IPV6
, dir
);
5758 bpf_error("bad protocol applied for 'protochain'");
5763 * We don't handle variable-length prefixes before the link-layer
5764 * header, or variable-length link-layer headers, here yet.
5765 * We might want to add BPF instructions to do the protochain
5766 * work, to simplify that and, on platforms that have a BPF
5767 * interpreter with the new instructions, let the filtering
5768 * be done in the kernel. (We already require a modified BPF
5769 * engine to do the protochain stuff, to support backward
5770 * branches, and backward branch support is unlikely to appear
5771 * in kernel BPF engines.)
5775 case DLT_IEEE802_11
:
5776 case DLT_PRISM_HEADER
:
5777 case DLT_IEEE802_11_RADIO_AVS
:
5778 case DLT_IEEE802_11_RADIO
:
5780 bpf_error("'protochain' not supported with 802.11");
5783 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5786 * s[0] is a dummy entry to protect other BPF insn from damage
5787 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5788 * hard to find interdependency made by jump table fixup.
5791 s
[i
] = new_stmt(0); /*dummy*/
5796 b0
= gen_linktype(ETHERTYPE_IP
);
5799 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5800 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
+ 9;
5802 /* X = ip->ip_hl << 2 */
5803 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5804 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5809 b0
= gen_linktype(ETHERTYPE_IPV6
);
5811 /* A = ip6->ip_nxt */
5812 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5813 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
+ 6;
5815 /* X = sizeof(struct ip6_hdr) */
5816 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5822 bpf_error("unsupported proto to gen_protochain");
5826 /* again: if (A == v) goto end; else fall through; */
5828 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5830 s
[i
]->s
.jt
= NULL
; /*later*/
5831 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5835 #ifndef IPPROTO_NONE
5836 #define IPPROTO_NONE 59
5838 /* if (A == IPPROTO_NONE) goto end */
5839 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5840 s
[i
]->s
.jt
= NULL
; /*later*/
5841 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5842 s
[i
]->s
.k
= IPPROTO_NONE
;
5843 s
[fix5
]->s
.jf
= s
[i
];
5847 if (proto
== Q_IPV6
) {
5848 int v6start
, v6end
, v6advance
, j
;
5851 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5852 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5853 s
[i
]->s
.jt
= NULL
; /*later*/
5854 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5855 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5856 s
[fix2
]->s
.jf
= s
[i
];
5858 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5859 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5860 s
[i
]->s
.jt
= NULL
; /*later*/
5861 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5862 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5864 /* if (A == IPPROTO_ROUTING) goto v6advance */
5865 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5866 s
[i
]->s
.jt
= NULL
; /*later*/
5867 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5868 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5870 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5871 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5872 s
[i
]->s
.jt
= NULL
; /*later*/
5873 s
[i
]->s
.jf
= NULL
; /*later*/
5874 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5884 * A = P[X + packet head];
5885 * X = X + (P[X + packet head + 1] + 1) * 8;
5887 /* A = P[X + packet head] */
5888 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5889 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5892 s
[i
] = new_stmt(BPF_ST
);
5895 /* A = P[X + packet head + 1]; */
5896 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5897 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
+ 1;
5900 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5904 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5908 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
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;
5922 s
[i
- 1]->s
.jf
= s
[i
];
5926 for (j
= v6start
; j
<= v6end
; j
++)
5927 s
[j
]->s
.jt
= s
[v6advance
];
5930 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5932 s
[fix2
]->s
.jf
= s
[i
];
5938 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5939 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5940 s
[i
]->s
.jt
= NULL
; /*later*/
5941 s
[i
]->s
.jf
= NULL
; /*later*/
5942 s
[i
]->s
.k
= IPPROTO_AH
;
5944 s
[fix3
]->s
.jf
= s
[ahcheck
];
5951 * X = X + (P[X + 1] + 2) * 4;
5954 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5956 /* A = P[X + packet head]; */
5957 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5958 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5961 s
[i
] = new_stmt(BPF_ST
);
5965 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5968 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5972 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5974 /* A = P[X + packet head] */
5975 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5976 s
[i
]->s
.k
= off_linkpl_constant_part
+ off_nl
;
5979 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5983 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5987 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5990 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5994 /* goto again; (must use BPF_JA for backward jump) */
5995 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5996 s
[i
]->s
.k
= again
- i
- 1;
6001 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
6003 s
[fix2
]->s
.jt
= s
[end
];
6004 s
[fix4
]->s
.jf
= s
[end
];
6005 s
[fix5
]->s
.jt
= s
[end
];
6012 for (i
= 0; i
< max
- 1; i
++)
6013 s
[i
]->next
= s
[i
+ 1];
6014 s
[max
- 1]->next
= NULL
;
6019 b
= new_block(JMP(BPF_JEQ
));
6020 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6030 static struct block
*
6031 gen_check_802_11_data_frame()
6034 struct block
*b0
, *b1
;
6037 * A data frame has the 0x08 bit (b3) in the frame control field set
6038 * and the 0x04 bit (b2) clear.
6040 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6041 b0
= new_block(JMP(BPF_JSET
));
6045 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6046 b1
= new_block(JMP(BPF_JSET
));
6057 * Generate code that checks whether the packet is a packet for protocol
6058 * <proto> and whether the type field in that protocol's header has
6059 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6060 * IP packet and checks the protocol number in the IP header against <v>.
6062 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6063 * against Q_IP and Q_IPV6.
6065 static struct block
*
6066 gen_proto(v
, proto
, dir
)
6071 struct block
*b0
, *b1
;
6076 if (dir
!= Q_DEFAULT
)
6077 bpf_error("direction applied to 'proto'");
6081 b0
= gen_proto(v
, Q_IP
, dir
);
6082 b1
= gen_proto(v
, Q_IPV6
, dir
);
6088 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6089 * not LLC encapsulation with LLCSAP_IP.
6091 * For IEEE 802 networks - which includes 802.5 token ring
6092 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6093 * says that SNAP encapsulation is used, not LLC encapsulation
6096 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6097 * RFC 2225 say that SNAP encapsulation is used, not LLC
6098 * encapsulation with LLCSAP_IP.
6100 * So we always check for ETHERTYPE_IP.
6102 b0
= gen_linktype(ETHERTYPE_IP
);
6104 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6106 b1
= gen_protochain(v
, Q_IP
);
6116 * Frame Relay packets typically have an OSI
6117 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6118 * generates code to check for all the OSI
6119 * NLPIDs, so calling it and then adding a check
6120 * for the particular NLPID for which we're
6121 * looking is bogus, as we can just check for
6124 * What we check for is the NLPID and a frame
6125 * control field value of UI, i.e. 0x03 followed
6128 * XXX - assumes a 2-byte Frame Relay header with
6129 * DLCI and flags. What if the address is longer?
6131 * XXX - what about SNAP-encapsulated frames?
6133 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6139 * Cisco uses an Ethertype lookalike - for OSI,
6142 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6143 /* OSI in C-HDLC is stuffed with a fudge byte */
6144 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6149 b0
= gen_linktype(LLCSAP_ISONS
);
6150 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6156 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6158 * 4 is the offset of the PDU type relative to the IS-IS
6161 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6166 bpf_error("arp does not encapsulate another protocol");
6170 bpf_error("rarp does not encapsulate another protocol");
6174 bpf_error("atalk encapsulation is not specifiable");
6178 bpf_error("decnet encapsulation is not specifiable");
6182 bpf_error("sca does not encapsulate another protocol");
6186 bpf_error("lat does not encapsulate another protocol");
6190 bpf_error("moprc does not encapsulate another protocol");
6194 bpf_error("mopdl does not encapsulate another protocol");
6198 return gen_linktype(v
);
6201 bpf_error("'udp proto' is bogus");
6205 bpf_error("'tcp proto' is bogus");
6209 bpf_error("'sctp proto' is bogus");
6213 bpf_error("'icmp proto' is bogus");
6217 bpf_error("'igmp proto' is bogus");
6221 bpf_error("'igrp proto' is bogus");
6225 bpf_error("'pim proto' is bogus");
6229 bpf_error("'vrrp proto' is bogus");
6233 bpf_error("'carp proto' is bogus");
6237 b0
= gen_linktype(ETHERTYPE_IPV6
);
6240 * Also check for a fragment header before the final
6243 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6244 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
6246 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6249 b1
= gen_protochain(v
, Q_IPV6
);
6255 bpf_error("'icmp6 proto' is bogus");
6258 bpf_error("'ah proto' is bogus");
6261 bpf_error("'ah proto' is bogus");
6264 bpf_error("'stp proto' is bogus");
6267 bpf_error("'ipx proto' is bogus");
6270 bpf_error("'netbeui proto' is bogus");
6273 bpf_error("'radio proto' is bogus");
6284 register const char *name
;
6287 int proto
= q
.proto
;
6291 bpf_u_int32 mask
, addr
;
6293 bpf_u_int32
**alist
;
6296 struct sockaddr_in
*sin4
;
6297 struct sockaddr_in6
*sin6
;
6298 struct addrinfo
*res
, *res0
;
6299 struct in6_addr mask128
;
6301 struct block
*b
, *tmp
;
6302 int port
, real_proto
;
6308 addr
= pcap_nametonetaddr(name
);
6310 bpf_error("unknown network '%s'", name
);
6311 /* Left justify network addr and calculate its network mask */
6313 while (addr
&& (addr
& 0xff000000) == 0) {
6317 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6321 if (proto
== Q_LINK
) {
6325 case DLT_NETANALYZER
:
6326 case DLT_NETANALYZER_TRANSPARENT
:
6327 eaddr
= pcap_ether_hostton(name
);
6330 "unknown ether host '%s'", name
);
6331 tmp
= gen_outerll_check();
6332 b
= gen_ehostop(eaddr
, dir
);
6339 eaddr
= pcap_ether_hostton(name
);
6342 "unknown FDDI host '%s'", name
);
6343 b
= gen_fhostop(eaddr
, dir
);
6348 eaddr
= pcap_ether_hostton(name
);
6351 "unknown token ring host '%s'", name
);
6352 b
= gen_thostop(eaddr
, dir
);
6356 case DLT_IEEE802_11
:
6357 case DLT_PRISM_HEADER
:
6358 case DLT_IEEE802_11_RADIO_AVS
:
6359 case DLT_IEEE802_11_RADIO
:
6361 eaddr
= pcap_ether_hostton(name
);
6364 "unknown 802.11 host '%s'", name
);
6365 b
= gen_wlanhostop(eaddr
, dir
);
6369 case DLT_IP_OVER_FC
:
6370 eaddr
= pcap_ether_hostton(name
);
6373 "unknown Fibre Channel host '%s'", name
);
6374 b
= gen_ipfchostop(eaddr
, dir
);
6379 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6380 } else if (proto
== Q_DECNET
) {
6381 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6383 * I don't think DECNET hosts can be multihomed, so
6384 * there is no need to build up a list of addresses
6386 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6389 alist
= pcap_nametoaddr(name
);
6390 if (alist
== NULL
|| *alist
== NULL
)
6391 bpf_error("unknown host '%s'", name
);
6393 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6395 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6397 tmp
= gen_host(**alist
++, 0xffffffff,
6398 tproto
, dir
, q
.addr
);
6404 memset(&mask128
, 0xff, sizeof(mask128
));
6405 res0
= res
= pcap_nametoaddrinfo(name
);
6407 bpf_error("unknown host '%s'", name
);
6410 tproto
= tproto6
= proto
;
6411 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6415 for (res
= res0
; res
; res
= res
->ai_next
) {
6416 switch (res
->ai_family
) {
6418 if (tproto
== Q_IPV6
)
6421 sin4
= (struct sockaddr_in
*)
6423 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6424 0xffffffff, tproto
, dir
, q
.addr
);
6427 if (tproto6
== Q_IP
)
6430 sin6
= (struct sockaddr_in6
*)
6432 tmp
= gen_host6(&sin6
->sin6_addr
,
6433 &mask128
, tproto6
, dir
, q
.addr
);
6445 bpf_error("unknown host '%s'%s", name
,
6446 (proto
== Q_DEFAULT
)
6448 : " for specified address family");
6455 if (proto
!= Q_DEFAULT
&&
6456 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6457 bpf_error("illegal qualifier of 'port'");
6458 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6459 bpf_error("unknown port '%s'", name
);
6460 if (proto
== Q_UDP
) {
6461 if (real_proto
== IPPROTO_TCP
)
6462 bpf_error("port '%s' is tcp", name
);
6463 else if (real_proto
== IPPROTO_SCTP
)
6464 bpf_error("port '%s' is sctp", name
);
6466 /* override PROTO_UNDEF */
6467 real_proto
= IPPROTO_UDP
;
6469 if (proto
== Q_TCP
) {
6470 if (real_proto
== IPPROTO_UDP
)
6471 bpf_error("port '%s' is udp", name
);
6473 else if (real_proto
== IPPROTO_SCTP
)
6474 bpf_error("port '%s' is sctp", name
);
6476 /* override PROTO_UNDEF */
6477 real_proto
= IPPROTO_TCP
;
6479 if (proto
== Q_SCTP
) {
6480 if (real_proto
== IPPROTO_UDP
)
6481 bpf_error("port '%s' is udp", name
);
6483 else if (real_proto
== IPPROTO_TCP
)
6484 bpf_error("port '%s' is tcp", name
);
6486 /* override PROTO_UNDEF */
6487 real_proto
= IPPROTO_SCTP
;
6490 bpf_error("illegal port number %d < 0", port
);
6492 bpf_error("illegal port number %d > 65535", port
);
6493 b
= gen_port(port
, real_proto
, dir
);
6494 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6498 if (proto
!= Q_DEFAULT
&&
6499 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6500 bpf_error("illegal qualifier of 'portrange'");
6501 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6502 bpf_error("unknown port in range '%s'", name
);
6503 if (proto
== Q_UDP
) {
6504 if (real_proto
== IPPROTO_TCP
)
6505 bpf_error("port in range '%s' is tcp", name
);
6506 else if (real_proto
== IPPROTO_SCTP
)
6507 bpf_error("port in range '%s' is sctp", name
);
6509 /* override PROTO_UNDEF */
6510 real_proto
= IPPROTO_UDP
;
6512 if (proto
== Q_TCP
) {
6513 if (real_proto
== IPPROTO_UDP
)
6514 bpf_error("port in range '%s' is udp", name
);
6515 else if (real_proto
== IPPROTO_SCTP
)
6516 bpf_error("port in range '%s' is sctp", name
);
6518 /* override PROTO_UNDEF */
6519 real_proto
= IPPROTO_TCP
;
6521 if (proto
== Q_SCTP
) {
6522 if (real_proto
== IPPROTO_UDP
)
6523 bpf_error("port in range '%s' is udp", name
);
6524 else if (real_proto
== IPPROTO_TCP
)
6525 bpf_error("port in range '%s' is tcp", name
);
6527 /* override PROTO_UNDEF */
6528 real_proto
= IPPROTO_SCTP
;
6531 bpf_error("illegal port number %d < 0", port1
);
6533 bpf_error("illegal port number %d > 65535", port1
);
6535 bpf_error("illegal port number %d < 0", port2
);
6537 bpf_error("illegal port number %d > 65535", port2
);
6539 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6540 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6545 eaddr
= pcap_ether_hostton(name
);
6547 bpf_error("unknown ether host: %s", name
);
6549 alist
= pcap_nametoaddr(name
);
6550 if (alist
== NULL
|| *alist
== NULL
)
6551 bpf_error("unknown host '%s'", name
);
6552 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6556 bpf_error("'gateway' not supported in this configuration");
6560 real_proto
= lookup_proto(name
, proto
);
6561 if (real_proto
>= 0)
6562 return gen_proto(real_proto
, proto
, dir
);
6564 bpf_error("unknown protocol: %s", name
);
6567 real_proto
= lookup_proto(name
, proto
);
6568 if (real_proto
>= 0)
6569 return gen_protochain(real_proto
, proto
, dir
);
6571 bpf_error("unknown protocol: %s", name
);
6582 gen_mcode(s1
, s2
, masklen
, q
)
6583 register const char *s1
, *s2
;
6584 register unsigned int masklen
;
6587 register int nlen
, mlen
;
6590 nlen
= __pcap_atoin(s1
, &n
);
6591 /* Promote short ipaddr */
6595 mlen
= __pcap_atoin(s2
, &m
);
6596 /* Promote short ipaddr */
6599 bpf_error("non-network bits set in \"%s mask %s\"",
6602 /* Convert mask len to mask */
6604 bpf_error("mask length must be <= 32");
6607 * X << 32 is not guaranteed by C to be 0; it's
6612 m
= 0xffffffff << (32 - masklen
);
6614 bpf_error("non-network bits set in \"%s/%d\"",
6621 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6624 bpf_error("Mask syntax for networks only");
6633 register const char *s
;
6638 int proto
= q
.proto
;
6644 else if (q
.proto
== Q_DECNET
)
6645 vlen
= __pcap_atodn(s
, &v
);
6647 vlen
= __pcap_atoin(s
, &v
);
6654 if (proto
== Q_DECNET
)
6655 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6656 else if (proto
== Q_LINK
) {
6657 bpf_error("illegal link layer address");
6660 if (s
== NULL
&& q
.addr
== Q_NET
) {
6661 /* Promote short net number */
6662 while (v
&& (v
& 0xff000000) == 0) {
6667 /* Promote short ipaddr */
6671 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6676 proto
= IPPROTO_UDP
;
6677 else if (proto
== Q_TCP
)
6678 proto
= IPPROTO_TCP
;
6679 else if (proto
== Q_SCTP
)
6680 proto
= IPPROTO_SCTP
;
6681 else if (proto
== Q_DEFAULT
)
6682 proto
= PROTO_UNDEF
;
6684 bpf_error("illegal qualifier of 'port'");
6687 bpf_error("illegal port number %u > 65535", v
);
6691 b
= gen_port((int)v
, proto
, dir
);
6692 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6698 proto
= IPPROTO_UDP
;
6699 else if (proto
== Q_TCP
)
6700 proto
= IPPROTO_TCP
;
6701 else if (proto
== Q_SCTP
)
6702 proto
= IPPROTO_SCTP
;
6703 else if (proto
== Q_DEFAULT
)
6704 proto
= PROTO_UNDEF
;
6706 bpf_error("illegal qualifier of 'portrange'");
6709 bpf_error("illegal port number %u > 65535", v
);
6713 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6714 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6719 bpf_error("'gateway' requires a name");
6723 return gen_proto((int)v
, proto
, dir
);
6726 return gen_protochain((int)v
, proto
, dir
);
6741 gen_mcode6(s1
, s2
, masklen
, q
)
6742 register const char *s1
, *s2
;
6743 register unsigned int masklen
;
6746 struct addrinfo
*res
;
6747 struct in6_addr
*addr
;
6748 struct in6_addr mask
;
6753 bpf_error("no mask %s supported", s2
);
6755 res
= pcap_nametoaddrinfo(s1
);
6757 bpf_error("invalid ip6 address %s", s1
);
6760 bpf_error("%s resolved to multiple address", s1
);
6761 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6763 if (sizeof(mask
) * 8 < masklen
)
6764 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6765 memset(&mask
, 0, sizeof(mask
));
6766 memset(&mask
, 0xff, masklen
/ 8);
6768 mask
.s6_addr
[masklen
/ 8] =
6769 (0xff << (8 - masklen
% 8)) & 0xff;
6772 a
= (u_int32_t
*)addr
;
6773 m
= (u_int32_t
*)&mask
;
6774 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6775 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6776 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6784 bpf_error("Mask syntax for networks only");
6788 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6794 bpf_error("invalid qualifier against IPv6 address");
6803 register const u_char
*eaddr
;
6806 struct block
*b
, *tmp
;
6808 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6811 case DLT_NETANALYZER
:
6812 case DLT_NETANALYZER_TRANSPARENT
:
6813 tmp
= gen_outerll_check();
6814 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6819 return gen_fhostop(eaddr
, (int)q
.dir
);
6821 return gen_thostop(eaddr
, (int)q
.dir
);
6822 case DLT_IEEE802_11
:
6823 case DLT_PRISM_HEADER
:
6824 case DLT_IEEE802_11_RADIO_AVS
:
6825 case DLT_IEEE802_11_RADIO
:
6827 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6828 case DLT_IP_OVER_FC
:
6829 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6831 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6835 bpf_error("ethernet address used in non-ether expression");
6842 struct slist
*s0
, *s1
;
6845 * This is definitely not the best way to do this, but the
6846 * lists will rarely get long.
6853 static struct slist
*
6859 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6864 static struct slist
*
6870 s
= new_stmt(BPF_LD
|BPF_MEM
);
6876 * Modify "index" to use the value stored into its register as an
6877 * offset relative to the beginning of the header for the protocol
6878 * "proto", and allocate a register and put an item "size" bytes long
6879 * (1, 2, or 4) at that offset into that register, making it the register
6883 gen_load(proto
, inst
, size
)
6888 struct slist
*s
, *tmp
;
6890 int regno
= alloc_reg();
6892 free_reg(inst
->regno
);
6896 bpf_error("data size must be 1, 2, or 4");
6912 bpf_error("unsupported index operation");
6916 * The offset is relative to the beginning of the packet
6917 * data, if we have a radio header. (If we don't, this
6920 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6921 linktype
!= DLT_IEEE802_11_RADIO
&&
6922 linktype
!= DLT_PRISM_HEADER
)
6923 bpf_error("radio information not present in capture");
6926 * Load into the X register the offset computed into the
6927 * register specified by "index".
6929 s
= xfer_to_x(inst
);
6932 * Load the item at that offset.
6934 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6936 sappend(inst
->s
, s
);
6941 * The offset is relative to the beginning of
6942 * the link-layer header.
6944 * XXX - what about ATM LANE? Should the index be
6945 * relative to the beginning of the AAL5 frame, so
6946 * that 0 refers to the beginning of the LE Control
6947 * field, or relative to the beginning of the LAN
6948 * frame, so that 0 refers, for Ethernet LANE, to
6949 * the beginning of the destination address?
6951 s
= gen_llprefixlen();
6954 * If "s" is non-null, it has code to arrange that the
6955 * X register contains the length of the prefix preceding
6956 * the link-layer header. Add to it the offset computed
6957 * into the register specified by "index", and move that
6958 * into the X register. Otherwise, just load into the X
6959 * register the offset computed into the register specified
6963 sappend(s
, xfer_to_a(inst
));
6964 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6965 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6967 s
= xfer_to_x(inst
);
6970 * Load the item at the sum of the offset we've put in the
6971 * X register and the offset of the start of the link
6972 * layer header (which is 0 if the radio header is
6973 * variable-length; that header length is what we put
6974 * into the X register and then added to the index).
6976 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6979 sappend(inst
->s
, s
);
6993 * The offset is relative to the beginning of
6994 * the network-layer header.
6995 * XXX - are there any cases where we want
6998 s
= gen_off_linkpl();
7001 * If "s" is non-null, it has code to arrange that the
7002 * X register contains the variable part of the offset
7003 * of the link-layer payload. Add to it the offset
7004 * computed into the register specified by "index",
7005 * and move that into the X register. Otherwise, just
7006 * load into the X register the offset computed into
7007 * the register specified by "index".
7010 sappend(s
, xfer_to_a(inst
));
7011 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7012 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7014 s
= xfer_to_x(inst
);
7017 * Load the item at the sum of the offset we've put in the
7018 * X register, the offset of the start of the network
7019 * layer header from the beginning of the link-layer
7020 * payload, and the constant part of the offset of the
7021 * start of the link-layer payload.
7023 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
7024 tmp
->s
.k
= off_linkpl_constant_part
+ off_nl
;
7026 sappend(inst
->s
, s
);
7029 * Do the computation only if the packet contains
7030 * the protocol in question.
7032 b
= gen_proto_abbrev(proto
);
7034 gen_and(inst
->b
, b
);
7048 * The offset is relative to the beginning of
7049 * the transport-layer header.
7051 * Load the X register with the length of the IPv4 header
7052 * (plus the offset of the link-layer header, if it's
7053 * a variable-length header), in bytes.
7055 * XXX - are there any cases where we want
7057 * XXX - we should, if we're built with
7058 * IPv6 support, generate code to load either
7059 * IPv4, IPv6, or both, as appropriate.
7061 s
= gen_loadx_iphdrlen();
7064 * The X register now contains the sum of the variable
7065 * part of the offset of the link-layer payload and the
7066 * length of the network-layer header.
7068 * Load into the A register the offset relative to
7069 * the beginning of the transport layer header,
7070 * add the X register to that, move that to the
7071 * X register, and load with an offset from the
7072 * X register equal to the sum of the constant part of
7073 * the offset of the link-layer payload and the offset,
7074 * relative to the beginning of the link-layer payload,
7075 * of the network-layer header.
7077 sappend(s
, xfer_to_a(inst
));
7078 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7079 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7080 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7081 tmp
->s
.k
= off_linkpl_constant_part
+ off_nl
;
7082 sappend(inst
->s
, s
);
7085 * Do the computation only if the packet contains
7086 * the protocol in question - which is true only
7087 * if this is an IP datagram and is the first or
7088 * only fragment of that datagram.
7090 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7092 gen_and(inst
->b
, b
);
7093 gen_and(gen_proto_abbrev(Q_IP
), b
);
7097 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7100 inst
->regno
= regno
;
7101 s
= new_stmt(BPF_ST
);
7103 sappend(inst
->s
, s
);
7109 gen_relation(code
, a0
, a1
, reversed
)
7111 struct arth
*a0
, *a1
;
7114 struct slist
*s0
, *s1
, *s2
;
7115 struct block
*b
, *tmp
;
7119 if (code
== BPF_JEQ
) {
7120 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7121 b
= new_block(JMP(code
));
7125 b
= new_block(BPF_JMP
|code
|BPF_X
);
7131 sappend(a0
->s
, a1
->s
);
7135 free_reg(a0
->regno
);
7136 free_reg(a1
->regno
);
7138 /* 'and' together protocol checks */
7141 gen_and(a0
->b
, tmp
= a1
->b
);
7157 int regno
= alloc_reg();
7158 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7161 s
= new_stmt(BPF_LD
|BPF_LEN
);
7162 s
->next
= new_stmt(BPF_ST
);
7163 s
->next
->s
.k
= regno
;
7178 a
= (struct arth
*)newchunk(sizeof(*a
));
7182 s
= new_stmt(BPF_LD
|BPF_IMM
);
7184 s
->next
= new_stmt(BPF_ST
);
7200 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7203 s
= new_stmt(BPF_ST
);
7211 gen_arth(code
, a0
, a1
)
7213 struct arth
*a0
, *a1
;
7215 struct slist
*s0
, *s1
, *s2
;
7219 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7224 sappend(a0
->s
, a1
->s
);
7226 free_reg(a0
->regno
);
7227 free_reg(a1
->regno
);
7229 s0
= new_stmt(BPF_ST
);
7230 a0
->regno
= s0
->s
.k
= alloc_reg();
7237 * Here we handle simple allocation of the scratch registers.
7238 * If too many registers are alloc'd, the allocator punts.
7240 static int regused
[BPF_MEMWORDS
];
7244 * Initialize the table of used registers and the current register.
7250 memset(regused
, 0, sizeof regused
);
7254 * Return the next free register.
7259 int n
= BPF_MEMWORDS
;
7262 if (regused
[curreg
])
7263 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7265 regused
[curreg
] = 1;
7269 bpf_error("too many registers needed to evaluate expression");
7275 * Return a register to the table so it can
7285 static struct block
*
7292 s
= new_stmt(BPF_LD
|BPF_LEN
);
7293 b
= new_block(JMP(jmp
));
7304 return gen_len(BPF_JGE
, n
);
7308 * Actually, this is less than or equal.
7316 b
= gen_len(BPF_JGT
, n
);
7323 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7324 * the beginning of the link-layer header.
7325 * XXX - that means you can't test values in the radiotap header, but
7326 * as that header is difficult if not impossible to parse generally
7327 * without a loop, that might not be a severe problem. A new keyword
7328 * "radio" could be added for that, although what you'd really want
7329 * would be a way of testing particular radio header values, which
7330 * would generate code appropriate to the radio header in question.
7333 gen_byteop(op
, idx
, val
)
7344 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7347 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7351 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7355 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7359 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7363 b
= new_block(JMP(BPF_JEQ
));
7370 static u_char abroadcast
[] = { 0x0 };
7373 gen_broadcast(proto
)
7376 bpf_u_int32 hostmask
;
7377 struct block
*b0
, *b1
, *b2
;
7378 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7386 case DLT_ARCNET_LINUX
:
7387 return gen_ahostop(abroadcast
, Q_DST
);
7389 case DLT_NETANALYZER
:
7390 case DLT_NETANALYZER_TRANSPARENT
:
7391 b1
= gen_outerll_check();
7392 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7397 return gen_fhostop(ebroadcast
, Q_DST
);
7399 return gen_thostop(ebroadcast
, Q_DST
);
7400 case DLT_IEEE802_11
:
7401 case DLT_PRISM_HEADER
:
7402 case DLT_IEEE802_11_RADIO_AVS
:
7403 case DLT_IEEE802_11_RADIO
:
7405 return gen_wlanhostop(ebroadcast
, Q_DST
);
7406 case DLT_IP_OVER_FC
:
7407 return gen_ipfchostop(ebroadcast
, Q_DST
);
7409 bpf_error("not a broadcast link");
7415 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7416 * as an indication that we don't know the netmask, and fail
7419 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7420 bpf_error("netmask not known, so 'ip broadcast' not supported");
7421 b0
= gen_linktype(ETHERTYPE_IP
);
7422 hostmask
= ~netmask
;
7423 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7424 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7425 (bpf_int32
)(~0 & hostmask
), hostmask
);
7430 bpf_error("only link-layer/IP broadcast filters supported");
7436 * Generate code to test the low-order bit of a MAC address (that's
7437 * the bottom bit of the *first* byte).
7439 static struct block
*
7440 gen_mac_multicast(offset
)
7443 register struct block
*b0
;
7444 register struct slist
*s
;
7446 /* link[offset] & 1 != 0 */
7447 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7448 b0
= new_block(JMP(BPF_JSET
));
7455 gen_multicast(proto
)
7458 register struct block
*b0
, *b1
, *b2
;
7459 register struct slist
*s
;
7467 case DLT_ARCNET_LINUX
:
7468 /* all ARCnet multicasts use the same address */
7469 return gen_ahostop(abroadcast
, Q_DST
);
7471 case DLT_NETANALYZER
:
7472 case DLT_NETANALYZER_TRANSPARENT
:
7473 b1
= gen_outerll_check();
7474 /* ether[0] & 1 != 0 */
7475 b0
= gen_mac_multicast(0);
7481 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7483 * XXX - was that referring to bit-order issues?
7485 /* fddi[1] & 1 != 0 */
7486 return gen_mac_multicast(1);
7488 /* tr[2] & 1 != 0 */
7489 return gen_mac_multicast(2);
7490 case DLT_IEEE802_11
:
7491 case DLT_PRISM_HEADER
:
7492 case DLT_IEEE802_11_RADIO_AVS
:
7493 case DLT_IEEE802_11_RADIO
:
7498 * For control frames, there is no DA.
7500 * For management frames, DA is at an
7501 * offset of 4 from the beginning of
7504 * For data frames, DA is at an offset
7505 * of 4 from the beginning of the packet
7506 * if To DS is clear and at an offset of
7507 * 16 from the beginning of the packet
7512 * Generate the tests to be done for data frames.
7514 * First, check for To DS set, i.e. "link[1] & 0x01".
7516 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7517 b1
= new_block(JMP(BPF_JSET
));
7518 b1
->s
.k
= 0x01; /* To DS */
7522 * If To DS is set, the DA is at 16.
7524 b0
= gen_mac_multicast(16);
7528 * Now, check for To DS not set, i.e. check
7529 * "!(link[1] & 0x01)".
7531 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7532 b2
= new_block(JMP(BPF_JSET
));
7533 b2
->s
.k
= 0x01; /* To DS */
7538 * If To DS is not set, the DA is at 4.
7540 b1
= gen_mac_multicast(4);
7544 * Now OR together the last two checks. That gives
7545 * the complete set of checks for data frames.
7550 * Now check for a data frame.
7551 * I.e, check "link[0] & 0x08".
7553 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7554 b1
= new_block(JMP(BPF_JSET
));
7559 * AND that with the checks done for data frames.
7564 * If the high-order bit of the type value is 0, this
7565 * is a management frame.
7566 * I.e, check "!(link[0] & 0x08)".
7568 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7569 b2
= new_block(JMP(BPF_JSET
));
7575 * For management frames, the DA is at 4.
7577 b1
= gen_mac_multicast(4);
7581 * OR that with the checks done for data frames.
7582 * That gives the checks done for management and
7588 * If the low-order bit of the type value is 1,
7589 * this is either a control frame or a frame
7590 * with a reserved type, and thus not a
7593 * I.e., check "!(link[0] & 0x04)".
7595 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7596 b1
= new_block(JMP(BPF_JSET
));
7602 * AND that with the checks for data and management
7607 case DLT_IP_OVER_FC
:
7608 b0
= gen_mac_multicast(2);
7613 /* Link not known to support multicasts */
7617 b0
= gen_linktype(ETHERTYPE_IP
);
7618 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7623 b0
= gen_linktype(ETHERTYPE_IPV6
);
7624 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7628 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7634 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7635 * Outbound traffic is sent by this machine, while inbound traffic is
7636 * sent by a remote machine (and may include packets destined for a
7637 * unicast or multicast link-layer address we are not subscribing to).
7638 * These are the same definitions implemented by pcap_setdirection().
7639 * Capturing only unicast traffic destined for this host is probably
7640 * better accomplished using a higher-layer filter.
7646 register struct block
*b0
;
7649 * Only some data link types support inbound/outbound qualifiers.
7653 b0
= gen_relation(BPF_JEQ
,
7654 gen_load(Q_LINK
, gen_loadi(0), 1),
7661 /* match outgoing packets */
7662 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7664 /* match incoming packets */
7665 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7670 /* match outgoing packets */
7671 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7673 /* to filter on inbound traffic, invert the match */
7678 #ifdef HAVE_NET_PFVAR_H
7680 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7681 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7687 /* match outgoing packets */
7688 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7690 /* match incoming packets */
7691 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7695 case DLT_JUNIPER_MFR
:
7696 case DLT_JUNIPER_MLFR
:
7697 case DLT_JUNIPER_MLPPP
:
7698 case DLT_JUNIPER_ATM1
:
7699 case DLT_JUNIPER_ATM2
:
7700 case DLT_JUNIPER_PPPOE
:
7701 case DLT_JUNIPER_PPPOE_ATM
:
7702 case DLT_JUNIPER_GGSN
:
7703 case DLT_JUNIPER_ES
:
7704 case DLT_JUNIPER_MONITOR
:
7705 case DLT_JUNIPER_SERVICES
:
7706 case DLT_JUNIPER_ETHER
:
7707 case DLT_JUNIPER_PPP
:
7708 case DLT_JUNIPER_FRELAY
:
7709 case DLT_JUNIPER_CHDLC
:
7710 case DLT_JUNIPER_VP
:
7711 case DLT_JUNIPER_ST
:
7712 case DLT_JUNIPER_ISM
:
7713 case DLT_JUNIPER_VS
:
7714 case DLT_JUNIPER_SRX_E2E
:
7715 case DLT_JUNIPER_FIBRECHANNEL
:
7716 case DLT_JUNIPER_ATM_CEMIC
:
7718 /* juniper flags (including direction) are stored
7719 * the byte after the 3-byte magic number */
7721 /* match outgoing packets */
7722 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7724 /* match incoming packets */
7725 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7731 * If we have packet meta-data indicating a direction,
7732 * check it, otherwise give up as this link-layer type
7733 * has nothing in the packet data.
7735 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7737 * This is Linux with PF_PACKET support.
7738 * If this is a *live* capture, we can look at
7739 * special meta-data in the filter expression;
7740 * if it's a savefile, we can't.
7742 if (bpf_pcap
->rfile
!= NULL
) {
7743 /* We have a FILE *, so this is a savefile */
7744 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7749 /* match outgoing packets */
7750 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7753 /* to filter on inbound traffic, invert the match */
7756 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7757 bpf_error("inbound/outbound not supported on linktype %d",
7761 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7766 #ifdef HAVE_NET_PFVAR_H
7767 /* PF firewall log matched interface */
7769 gen_pf_ifname(const char *ifname
)
7774 if (linktype
!= DLT_PFLOG
) {
7775 bpf_error("ifname supported only on PF linktype");
7778 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7779 off
= offsetof(struct pfloghdr
, ifname
);
7780 if (strlen(ifname
) >= len
) {
7781 bpf_error("ifname interface names can only be %d characters",
7785 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7789 /* PF firewall log ruleset name */
7791 gen_pf_ruleset(char *ruleset
)
7795 if (linktype
!= DLT_PFLOG
) {
7796 bpf_error("ruleset supported only on PF linktype");
7800 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7801 bpf_error("ruleset names can only be %ld characters",
7802 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7806 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7807 strlen(ruleset
), (const u_char
*)ruleset
);
7811 /* PF firewall log rule number */
7817 if (linktype
!= DLT_PFLOG
) {
7818 bpf_error("rnr supported only on PF linktype");
7822 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7827 /* PF firewall log sub-rule number */
7829 gen_pf_srnr(int srnr
)
7833 if (linktype
!= DLT_PFLOG
) {
7834 bpf_error("srnr supported only on PF linktype");
7838 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7843 /* PF firewall log reason code */
7845 gen_pf_reason(int reason
)
7849 if (linktype
!= DLT_PFLOG
) {
7850 bpf_error("reason supported only on PF linktype");
7854 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7859 /* PF firewall log action */
7861 gen_pf_action(int action
)
7865 if (linktype
!= DLT_PFLOG
) {
7866 bpf_error("action supported only on PF linktype");
7870 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7874 #else /* !HAVE_NET_PFVAR_H */
7876 gen_pf_ifname(const char *ifname
)
7878 bpf_error("libpcap was compiled without pf support");
7884 gen_pf_ruleset(char *ruleset
)
7886 bpf_error("libpcap was compiled on a machine without pf support");
7894 bpf_error("libpcap was compiled on a machine without pf support");
7900 gen_pf_srnr(int srnr
)
7902 bpf_error("libpcap was compiled on a machine without pf support");
7908 gen_pf_reason(int reason
)
7910 bpf_error("libpcap was compiled on a machine without pf support");
7916 gen_pf_action(int action
)
7918 bpf_error("libpcap was compiled on a machine without pf support");
7922 #endif /* HAVE_NET_PFVAR_H */
7924 /* IEEE 802.11 wireless header */
7926 gen_p80211_type(int type
, int mask
)
7932 case DLT_IEEE802_11
:
7933 case DLT_PRISM_HEADER
:
7934 case DLT_IEEE802_11_RADIO_AVS
:
7935 case DLT_IEEE802_11_RADIO
:
7936 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7941 bpf_error("802.11 link-layer types supported only on 802.11");
7949 gen_p80211_fcdir(int fcdir
)
7955 case DLT_IEEE802_11
:
7956 case DLT_PRISM_HEADER
:
7957 case DLT_IEEE802_11_RADIO_AVS
:
7958 case DLT_IEEE802_11_RADIO
:
7962 bpf_error("frame direction supported only with 802.11 headers");
7966 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7967 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7974 register const u_char
*eaddr
;
7980 case DLT_ARCNET_LINUX
:
7981 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7983 return (gen_ahostop(eaddr
, (int)q
.dir
));
7985 bpf_error("ARCnet address used in non-arc expression");
7991 bpf_error("aid supported only on ARCnet");
7994 bpf_error("ARCnet address used in non-arc expression");
7999 static struct block
*
8000 gen_ahostop(eaddr
, dir
)
8001 register const u_char
*eaddr
;
8004 register struct block
*b0
, *b1
;
8007 /* src comes first, different from Ethernet */
8009 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
8012 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
8015 b0
= gen_ahostop(eaddr
, Q_SRC
);
8016 b1
= gen_ahostop(eaddr
, Q_DST
);
8022 b0
= gen_ahostop(eaddr
, Q_SRC
);
8023 b1
= gen_ahostop(eaddr
, Q_DST
);
8028 bpf_error("'addr1' is only supported on 802.11");
8032 bpf_error("'addr2' is only supported on 802.11");
8036 bpf_error("'addr3' is only supported on 802.11");
8040 bpf_error("'addr4' is only supported on 802.11");
8044 bpf_error("'ra' is only supported on 802.11");
8048 bpf_error("'ta' is only supported on 802.11");
8055 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8056 static struct block
*
8057 gen_vlan_bpf_extensions(int vlan_num
)
8059 struct block
*b0
, *b1
;
8062 /* generate new filter code based on extracting packet
8064 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8065 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8067 b0
= new_block(JMP(BPF_JEQ
));
8071 if (vlan_num
>= 0) {
8072 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
8073 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8075 b1
= new_block(JMP(BPF_JEQ
));
8077 b1
->s
.k
= (bpf_int32
) vlan_num
;
8087 static struct block
*
8088 gen_vlan_no_bpf_extensions(int vlan_num
)
8090 struct block
*b0
, *b1
;
8092 /* check for VLAN, including QinQ */
8093 b0
= gen_linktype(ETHERTYPE_8021Q
);
8094 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
8098 /* If a specific VLAN is requested, check VLAN id */
8099 if (vlan_num
>= 0) {
8100 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
8101 (bpf_int32
)vlan_num
, 0x0fff);
8107 * The payload follows the full header, including the
8108 * VLAN tags, so skip past this VLAN tag.
8110 off_linkpl_constant_part
+= 4;
8113 * The link-layer type information follows the VLAN tags, so
8114 * skip past this VLAN tag.
8122 * support IEEE 802.1Q VLAN trunk over ethernet
8130 /* can't check for VLAN-encapsulated packets inside MPLS */
8131 if (label_stack_depth
> 0)
8132 bpf_error("no VLAN match after MPLS");
8135 * Check for a VLAN packet, and then change the offsets to point
8136 * to the type and data fields within the VLAN packet. Just
8137 * increment the offsets, so that we can support a hierarchy, e.g.
8138 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8141 * XXX - this is a bit of a kludge. If we were to split the
8142 * compiler into a parser that parses an expression and
8143 * generates an expression tree, and a code generator that
8144 * takes an expression tree (which could come from our
8145 * parser or from some other parser) and generates BPF code,
8146 * we could perhaps make the offsets parameters of routines
8147 * and, in the handler for an "AND" node, pass to subnodes
8148 * other than the VLAN node the adjusted offsets.
8150 * This would mean that "vlan" would, instead of changing the
8151 * behavior of *all* tests after it, change only the behavior
8152 * of tests ANDed with it. That would change the documented
8153 * semantics of "vlan", which might break some expressions.
8154 * However, it would mean that "(vlan and ip) or ip" would check
8155 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8156 * checking only for VLAN-encapsulated IP, so that could still
8157 * be considered worth doing; it wouldn't break expressions
8158 * that are of the form "vlan and ..." or "vlan N and ...",
8159 * which I suspect are the most common expressions involving
8160 * "vlan". "vlan or ..." doesn't necessarily do what the user
8161 * would really want, now, as all the "or ..." tests would
8162 * be done assuming a VLAN, even though the "or" could be viewed
8163 * as meaning "or, if this isn't a VLAN packet...".
8168 case DLT_NETANALYZER
:
8169 case DLT_NETANALYZER_TRANSPARENT
:
8170 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8171 if (vlan_stack_depth
== 0) {
8173 * Do we need special VLAN handling?
8175 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8176 b0
= gen_vlan_bpf_extensions(vlan_num
);
8178 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8181 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8184 bpf_error("no VLAN support for data link type %d",
8201 struct block
*b0
, *b1
;
8203 if (label_stack_depth
> 0) {
8204 /* just match the bottom-of-stack bit clear */
8205 b0
= gen_mcmp(OR_MPLSPL
, off_nl
-2, BPF_B
, 0, 0x01);
8208 * We're not in an MPLS stack yet, so check the link-layer
8209 * type against MPLS.
8213 case DLT_C_HDLC
: /* fall through */
8215 case DLT_NETANALYZER
:
8216 case DLT_NETANALYZER_TRANSPARENT
:
8217 b0
= gen_linktype(ETHERTYPE_MPLS
);
8221 b0
= gen_linktype(PPP_MPLS_UCAST
);
8224 /* FIXME add other DLT_s ...
8225 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8226 * leave it for now */
8229 bpf_error("no MPLS support for data link type %d",
8237 /* If a specific MPLS label is requested, check it */
8238 if (label_num
>= 0) {
8239 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8240 b1
= gen_mcmp(OR_MPLSPL
, off_nl
, BPF_W
, (bpf_int32
)label_num
,
8241 0xfffff000); /* only compare the first 20 bits */
8247 * Change the offsets to point to the type and data fields within
8248 * the MPLS packet. Just increment the offsets, so that we
8249 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8250 * capture packets with an outer label of 100000 and an inner
8253 * Increment the MPLS stack depth as well; this indicates that
8254 * we're checking MPLS-encapsulated headers, to make sure higher
8255 * level code generators don't try to match against IP-related
8256 * protocols such as Q_ARP, Q_RARP etc.
8258 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8262 label_stack_depth
++;
8267 * Support PPPOE discovery and session.
8272 /* check for PPPoE discovery */
8273 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8277 gen_pppoes(sess_num
)
8280 struct block
*b0
, *b1
;
8283 * Test against the PPPoE session link-layer type.
8285 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8287 /* If a specific session is requested, check PPPoE session id */
8288 if (sess_num
>= 0) {
8289 b1
= gen_mcmp(OR_LINKPL
, off_nl
, BPF_W
,
8290 (bpf_int32
)sess_num
, 0x0000ffff);
8296 * Change the offsets to point to the type and data fields within
8297 * the PPP packet, and note that this is PPPoE rather than
8300 * XXX - this is a bit of a kludge. If we were to split the
8301 * compiler into a parser that parses an expression and
8302 * generates an expression tree, and a code generator that
8303 * takes an expression tree (which could come from our
8304 * parser or from some other parser) and generates BPF code,
8305 * we could perhaps make the offsets parameters of routines
8306 * and, in the handler for an "AND" node, pass to subnodes
8307 * other than the PPPoE node the adjusted offsets.
8309 * This would mean that "pppoes" would, instead of changing the
8310 * behavior of *all* tests after it, change only the behavior
8311 * of tests ANDed with it. That would change the documented
8312 * semantics of "pppoes", which might break some expressions.
8313 * However, it would mean that "(pppoes and ip) or ip" would check
8314 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8315 * checking only for VLAN-encapsulated IP, so that could still
8316 * be considered worth doing; it wouldn't break expressions
8317 * that are of the form "pppoes and ..." which I suspect are the
8318 * most common expressions involving "pppoes". "pppoes or ..."
8319 * doesn't necessarily do what the user would really want, now,
8320 * as all the "or ..." tests would be done assuming PPPoE, even
8321 * though the "or" could be viewed as meaning "or, if this isn't
8322 * a PPPoE packet...".
8324 * The "network-layer" protocol is PPPoE, which has a 6-byte
8325 * PPPoE header, followed by a PPP packet.
8327 * There is no HDLC encapsulation for the PPP packet (it's
8328 * encapsulated in PPPoES instead), so the link-layer type
8329 * starts at the first byte of the PPP packet. For PPPoE,
8330 * that offset is relative to the beginning of the total
8331 * link-layer payload, including any 802.2 LLC header, so
8332 * it's 6 bytes past off_nl.
8335 off_ll
= off_linkpl_constant_part
+ off_nl
+ 6; /* 6 bytes past the PPPoE header */
8337 off_linkpl_constant_part
= off_ll
+ 2;
8339 off_nl_nosnap
= 0; /* no 802.2 LLC */
8345 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8357 bpf_error("'vpi' supported only on raw ATM");
8358 if (off_vpi
== (u_int
)-1)
8360 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8366 bpf_error("'vci' supported only on raw ATM");
8367 if (off_vci
== (u_int
)-1)
8369 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8374 if (off_proto
== (u_int
)-1)
8375 abort(); /* XXX - this isn't on FreeBSD */
8376 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8381 if (off_payload
== (u_int
)-1)
8383 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8384 0xffffffff, jtype
, reverse
, jvalue
);
8389 bpf_error("'callref' supported only on raw ATM");
8390 if (off_proto
== (u_int
)-1)
8392 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8393 jtype
, reverse
, jvalue
);
8403 gen_atmtype_abbrev(type
)
8406 struct block
*b0
, *b1
;
8411 /* Get all packets in Meta signalling Circuit */
8413 bpf_error("'metac' supported only on raw ATM");
8414 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8415 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8420 /* Get all packets in Broadcast Circuit*/
8422 bpf_error("'bcc' supported only on raw ATM");
8423 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8424 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8429 /* Get all cells in Segment OAM F4 circuit*/
8431 bpf_error("'oam4sc' supported only on raw ATM");
8432 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8433 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8438 /* Get all cells in End-to-End OAM F4 Circuit*/
8440 bpf_error("'oam4ec' supported only on raw ATM");
8441 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8442 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8447 /* Get all packets in connection Signalling Circuit */
8449 bpf_error("'sc' supported only on raw ATM");
8450 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8451 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8456 /* Get all packets in ILMI Circuit */
8458 bpf_error("'ilmic' supported only on raw ATM");
8459 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8460 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8465 /* Get all LANE packets */
8467 bpf_error("'lane' supported only on raw ATM");
8468 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8471 * Arrange that all subsequent tests assume LANE
8472 * rather than LLC-encapsulated packets, and set
8473 * the offsets appropriately for LANE-encapsulated
8476 * We assume LANE means Ethernet, not Token Ring.
8478 linktype
= DLT_EN10MB
;
8479 off_ll
= off_payload
+ 2; /* Ethernet header */
8481 off_linkpl_constant_part
= off_ll
+ 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);
8491 linktype
= outerlinktype
;
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
);