1 /* $NetBSD: gencode.c,v 1.8 2015/03/31 21:39:42 christos Exp $ */
3 /*#define CHASE_CHAIN*/
5 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that: (1) source code distributions
10 * retain the above copyright notice and this paragraph in its entirety, (2)
11 * distributions including binary code include the above copyright notice and
12 * this paragraph in its entirety in the documentation or other materials
13 * provided with the distribution, and (3) all advertising materials mentioning
14 * features or use of this software display the following acknowledgement:
15 * ``This product includes software developed by the University of California,
16 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17 * the University nor the names of its contributors may be used to endorse
18 * or promote products derived from this software without specific prior
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 #include <sys/cdefs.h>
26 __RCSID("$NetBSD: gencode.c,v 1.8 2015/03/31 21:39:42 christos Exp $");
33 #include <pcap-stdinc.h>
40 #ifdef HAVE_SYS_BITYPES_H
41 #include <sys/bitypes.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
48 * XXX - why was this included even on UNIX?
57 #include <sys/param.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
78 #include "ethertype.h"
82 #include "ieee80211.h"
84 #include "sunatmpos.h"
87 #include "pcap/ipnet.h"
89 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
90 #include <linux/types.h>
91 #include <linux/if_packet.h>
92 #include <linux/filter.h>
94 #ifdef HAVE_NET_PFVAR_H
95 #include <sys/socket.h>
97 #include <net/pfvar.h>
98 #include <net/if_pflog.h>
101 #define offsetof(s, e) ((size_t)&((s *)0)->e)
105 #include <netdb.h> /* for "struct addrinfo" */
108 #include <pcap/namedb.h>
110 #define ETHERMTU 1500
112 #ifndef ETHERTYPE_TEB
113 #define ETHERTYPE_TEB 0x6558
116 #ifndef IPPROTO_HOPOPTS
117 #define IPPROTO_HOPOPTS 0
119 #ifndef IPPROTO_ROUTING
120 #define IPPROTO_ROUTING 43
122 #ifndef IPPROTO_FRAGMENT
123 #define IPPROTO_FRAGMENT 44
125 #ifndef IPPROTO_DSTOPTS
126 #define IPPROTO_DSTOPTS 60
129 #define IPPROTO_SCTP 132
132 #define GENEVE_PORT 6081
134 #ifdef HAVE_OS_PROTO_H
135 #include "os-proto.h"
138 #define JMP(c) ((c)|BPF_JMP|BPF_K)
141 static jmp_buf top_ctx
;
142 static pcap_t
*bpf_pcap
;
144 /* Hack for handling VLAN and MPLS stacks. */
146 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
148 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
152 static int pcap_fddipad
;
156 bpf_error(const char *fmt
, ...)
161 if (bpf_pcap
!= NULL
)
162 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
169 static void init_linktype(pcap_t
*);
171 static void init_regs(void);
172 static int alloc_reg(void);
173 static void free_reg(int);
175 static struct block
*root
;
178 * Absolute offsets, which are offsets from the beginning of the raw
179 * packet data, are, in the general case, the sum of a variable value
180 * and a constant value; the variable value may be absent, in which
181 * case the offset is only the constant value, and the constant value
182 * may be zero, in which case the offset is only the variable value.
184 * bpf_abs_offset is a structure containing all that information:
186 * is_variable is 1 if there's a variable part.
188 * constant_part is the constant part of the value, possibly zero;
190 * if is_variable is 1, reg is the register number for a register
191 * containing the variable value if the register has been assigned,
201 * Value passed to gen_load_a() to indicate what the offset argument
202 * is relative to the beginning of.
205 OR_PACKET
, /* full packet data */
206 OR_LINKHDR
, /* link-layer header */
207 OR_PREVLINKHDR
, /* previous link-layer header */
208 OR_LLC
, /* 802.2 LLC header */
209 OR_PREVMPLSHDR
, /* previous MPLS header */
210 OR_LINKTYPE
, /* link-layer type */
211 OR_LINKPL
, /* link-layer payload */
212 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
213 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
214 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
219 * As errors are handled by a longjmp, anything allocated must be freed
220 * in the longjmp handler, so it must be reachable from that handler.
221 * One thing that's allocated is the result of pcap_nametoaddrinfo();
222 * it must be freed with freeaddrinfo(). This variable points to any
223 * addrinfo structure that would need to be freed.
225 static struct addrinfo
*ai
;
229 * We divy out chunks of memory rather than call malloc each time so
230 * we don't have to worry about leaking memory. It's probably
231 * not a big deal if all this memory was wasted but if this ever
232 * goes into a library that would probably not be a good idea.
234 * XXX - this *is* in a library....
237 #define CHUNK0SIZE 1024
243 static struct chunk chunks
[NCHUNKS
];
244 static int cur_chunk
;
246 static void *newchunk(u_int
);
247 static void freechunks(void);
248 static inline struct block
*new_block(int);
249 static inline struct slist
*new_stmt(int);
250 static struct block
*gen_retblk(int);
251 static inline void syntax(void);
253 static void backpatch(struct block
*, struct block
*);
254 static void merge(struct block
*, struct block
*);
255 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
256 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
257 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
258 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
259 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
260 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
262 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
263 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
264 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
265 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
266 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
267 static struct slist
*gen_loadx_iphdrlen(void);
268 static struct block
*gen_uncond(int);
269 static inline struct block
*gen_true(void);
270 static inline struct block
*gen_false(void);
271 static struct block
*gen_ether_linktype(int);
272 static struct block
*gen_ipnet_linktype(int);
273 static struct block
*gen_linux_sll_linktype(int);
274 static struct slist
*gen_load_prism_llprefixlen(void);
275 static struct slist
*gen_load_avs_llprefixlen(void);
276 static struct slist
*gen_load_radiotap_llprefixlen(void);
277 static struct slist
*gen_load_ppi_llprefixlen(void);
278 static void insert_compute_vloffsets(struct block
*);
279 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
280 static int ethertype_to_ppptype(int);
281 static struct block
*gen_linktype(int);
282 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
283 static struct block
*gen_llc_linktype(int);
284 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
286 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
288 static struct block
*gen_ahostop(const u_char
*, int);
289 static struct block
*gen_ehostop(const u_char
*, int);
290 static struct block
*gen_fhostop(const u_char
*, int);
291 static struct block
*gen_thostop(const u_char
*, int);
292 static struct block
*gen_wlanhostop(const u_char
*, int);
293 static struct block
*gen_ipfchostop(const u_char
*, int);
294 static struct block
*gen_dnhostop(bpf_u_int32
, int);
295 static struct block
*gen_mpls_linktype(int);
296 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
298 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
301 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
303 static struct block
*gen_ipfrag(void);
304 static struct block
*gen_portatom(int, bpf_int32
);
305 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
306 static struct block
*gen_portatom6(int, bpf_int32
);
307 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
308 struct block
*gen_portop(int, int, int);
309 static struct block
*gen_port(int, int, int);
310 struct block
*gen_portrangeop(int, int, int, int);
311 static struct block
*gen_portrange(int, int, int, int);
312 struct block
*gen_portop6(int, int, int);
313 static struct block
*gen_port6(int, int, int);
314 struct block
*gen_portrangeop6(int, int, int, int);
315 static struct block
*gen_portrange6(int, int, int, int);
316 static int lookup_proto(const char *, int);
317 static struct block
*gen_protochain(int, int, int);
318 static struct block
*gen_proto(int, int, int);
319 static struct slist
*xfer_to_x(struct arth
*);
320 static struct slist
*xfer_to_a(struct arth
*);
321 static struct block
*gen_mac_multicast(int);
322 static struct block
*gen_len(int, int);
323 static struct block
*gen_check_802_11_data_frame(void);
324 static struct block
*gen_geneve_ll_check(void);
326 static struct block
*gen_ppi_dlt_check(void);
327 static struct block
*gen_msg_abbrev(int type
);
338 /* XXX Round up to nearest long. */
339 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
341 /* XXX Round up to structure boundary. */
345 cp
= &chunks
[cur_chunk
];
346 if (n
> cp
->n_left
) {
347 ++cp
, k
= ++cur_chunk
;
349 bpf_error("out of memory");
350 size
= CHUNK0SIZE
<< k
;
351 cp
->m
= (void *)malloc(size
);
353 bpf_error("out of memory");
354 memset((char *)cp
->m
, 0, size
);
357 bpf_error("out of memory");
360 return (void *)((char *)cp
->m
+ cp
->n_left
);
369 for (i
= 0; i
< NCHUNKS
; ++i
)
370 if (chunks
[i
].m
!= NULL
) {
377 * A strdup whose allocations are freed after code generation is over.
381 register const char *s
;
383 int n
= strlen(s
) + 1;
384 char *cp
= newchunk(n
);
390 static inline struct block
*
396 p
= (struct block
*)newchunk(sizeof(*p
));
403 static inline struct slist
*
409 p
= (struct slist
*)newchunk(sizeof(*p
));
415 static struct block
*
419 struct block
*b
= new_block(BPF_RET
|BPF_K
);
425 __dead
static inline void
428 bpf_error("syntax error in filter expression");
431 static bpf_u_int32 netmask
;
436 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
437 const char *buf
, int optimize
, bpf_u_int32 mask
)
440 const char * volatile xbuf
= buf
;
445 * XXX - single-thread this code path with pthread calls on
446 * UN*X, if the platform supports pthreads? If that requires
447 * a separate -lpthread, we might not want to do that.
450 extern int wsockinit (void);
456 EnterCriticalSection(&g_PcapCompileCriticalSection
);
460 * If this pcap_t hasn't been activated, it doesn't have a
461 * link-layer type, so we can't use it.
464 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
465 "not-yet-activated pcap_t passed to pcap_compile");
475 if (setjmp(top_ctx
)) {
490 snaplen
= pcap_snapshot(p
);
492 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
493 "snaplen of 0 rejects all packets");
498 lex_init(xbuf
? xbuf
: "");
506 root
= gen_retblk(snaplen
);
508 if (optimize
&& !no_optimize
) {
511 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
512 bpf_error("expression rejects all packets");
514 program
->bf_insns
= icode_to_fcode(root
, &len
);
515 program
->bf_len
= len
;
520 rc
= 0; /* We're all okay */
525 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
532 * entry point for using the compiler with no pcap open
533 * pass in all the stuff that is needed explicitly instead.
536 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
537 struct bpf_program
*program
,
538 const char *buf
, int optimize
, bpf_u_int32 mask
)
543 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
546 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
552 * Clean up a "struct bpf_program" by freeing all the memory allocated
556 pcap_freecode(struct bpf_program
*program
)
559 if (program
->bf_insns
!= NULL
) {
560 free((char *)program
->bf_insns
);
561 program
->bf_insns
= NULL
;
566 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
567 * which of the jt and jf fields has been resolved and which is a pointer
568 * back to another unresolved block (or nil). At least one of the fields
569 * in each block is already resolved.
572 backpatch(list
, target
)
573 struct block
*list
, *target
;
590 * Merge the lists in b0 and b1, using the 'sense' field to indicate
591 * which of jt and jf is the link.
595 struct block
*b0
, *b1
;
597 register struct block
**p
= &b0
;
599 /* Find end of list. */
601 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
603 /* Concatenate the lists. */
611 struct block
*ppi_dlt_check
;
614 * Insert before the statements of the first (root) block any
615 * statements needed to load the lengths of any variable-length
616 * headers into registers.
618 * XXX - a fancier strategy would be to insert those before the
619 * statements of all blocks that use those lengths and that
620 * have no predecessors that use them, so that we only compute
621 * the lengths if we need them. There might be even better
622 * approaches than that.
624 * However, those strategies would be more complicated, and
625 * as we don't generate code to compute a length if the
626 * program has no tests that use the length, and as most
627 * tests will probably use those lengths, we would just
628 * postpone computing the lengths so that it's not done
629 * for tests that fail early, and it's not clear that's
632 insert_compute_vloffsets(p
->head
);
635 * For DLT_PPI captures, generate a check of the per-packet
636 * DLT value to make sure it's DLT_IEEE802_11.
638 ppi_dlt_check
= gen_ppi_dlt_check();
639 if (ppi_dlt_check
!= NULL
)
640 gen_and(ppi_dlt_check
, p
);
642 backpatch(p
, gen_retblk(snaplen
));
643 p
->sense
= !p
->sense
;
644 backpatch(p
, gen_retblk(0));
650 struct block
*b0
, *b1
;
652 backpatch(b0
, b1
->head
);
653 b0
->sense
= !b0
->sense
;
654 b1
->sense
= !b1
->sense
;
656 b1
->sense
= !b1
->sense
;
662 struct block
*b0
, *b1
;
664 b0
->sense
= !b0
->sense
;
665 backpatch(b0
, b1
->head
);
666 b0
->sense
= !b0
->sense
;
675 b
->sense
= !b
->sense
;
678 static struct block
*
679 gen_cmp(offrel
, offset
, size
, v
)
680 enum e_offrel offrel
;
684 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
687 static struct block
*
688 gen_cmp_gt(offrel
, offset
, size
, v
)
689 enum e_offrel offrel
;
693 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
696 static struct block
*
697 gen_cmp_ge(offrel
, offset
, size
, v
)
698 enum e_offrel offrel
;
702 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
705 static struct block
*
706 gen_cmp_lt(offrel
, offset
, size
, v
)
707 enum e_offrel offrel
;
711 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
714 static struct block
*
715 gen_cmp_le(offrel
, offset
, size
, v
)
716 enum e_offrel offrel
;
720 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
723 static struct block
*
724 gen_mcmp(offrel
, offset
, size
, v
, mask
)
725 enum e_offrel offrel
;
730 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
733 static struct block
*
734 gen_bcmp(offrel
, offset
, size
, v
)
735 enum e_offrel offrel
;
736 register u_int offset
, size
;
737 register const u_char
*v
;
739 register struct block
*b
, *tmp
;
743 register const u_char
*p
= &v
[size
- 4];
744 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
745 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
747 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
754 register const u_char
*p
= &v
[size
- 2];
755 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
757 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
764 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
773 * AND the field of size "size" at offset "offset" relative to the header
774 * specified by "offrel" with "mask", and compare it with the value "v"
775 * with the test specified by "jtype"; if "reverse" is true, the test
776 * should test the opposite of "jtype".
778 static struct block
*
779 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
780 enum e_offrel offrel
;
782 bpf_u_int32 offset
, size
, mask
, jtype
;
785 struct slist
*s
, *s2
;
788 s
= gen_load_a(offrel
, offset
, size
);
790 if (mask
!= 0xffffffff) {
791 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
796 b
= new_block(JMP(jtype
));
799 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
805 * Various code constructs need to know the layout of the packet.
806 * These variables give the necessary offsets from the beginning
807 * of the packet data.
811 * Absolute offset of the beginning of the link-layer header.
813 static bpf_abs_offset off_linkhdr
;
816 * If we're checking a link-layer header for a packet encapsulated in
817 * another protocol layer, this is the equivalent information for the
818 * previous layers' link-layer header from the beginning of the raw
821 static bpf_abs_offset off_prevlinkhdr
;
824 * This is the equivalent information for the outermost layers' link-layer
827 static bpf_abs_offset off_outermostlinkhdr
;
830 * "Push" the current value of the link-layer header type and link-layer
831 * header offset onto a "stack", and set a new value. (It's not a
832 * full-blown stack; we keep only the top two items.)
834 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
836 prevlinktype = new_linktype; \
837 off_prevlinkhdr = off_linkhdr; \
838 linktype = new_linktype; \
839 off_linkhdr.is_variable = new_is_variable; \
840 off_linkhdr.constant_part = new_constant_part; \
841 off_linkhdr.reg = new_reg; \
846 * Absolute offset of the beginning of the link-layer payload.
848 static bpf_abs_offset off_linkpl
;
851 * "off_linktype" is the offset to information in the link-layer header
852 * giving the packet type. This is an absolute offset from the beginning
855 * For Ethernet, it's the offset of the Ethernet type field; this
856 * means that it must have a value that skips VLAN tags.
858 * For link-layer types that always use 802.2 headers, it's the
859 * offset of the LLC header; this means that it must have a value
860 * that skips VLAN tags.
862 * For PPP, it's the offset of the PPP type field.
864 * For Cisco HDLC, it's the offset of the CHDLC type field.
866 * For BSD loopback, it's the offset of the AF_ value.
868 * For Linux cooked sockets, it's the offset of the type field.
870 * off_linktype.constant_part is set to -1 for no encapsulation,
871 * in which case, IP is assumed.
873 static bpf_abs_offset off_linktype
;
876 * TRUE if the link layer includes an ATM pseudo-header.
878 static int is_atm
= 0;
881 * TRUE if "geneve" appeared in the filter; it causes us to generate
882 * code that checks for a Geneve header and assume that later filters
883 * apply to the encapsulated payload.
885 static int is_geneve
= 0;
888 * These are offsets for the ATM pseudo-header.
890 static u_int off_vpi
;
891 static u_int off_vci
;
892 static u_int off_proto
;
895 * These are offsets for the MTP2 fields.
898 static u_int off_li_hsl
;
901 * These are offsets for the MTP3 fields.
903 static u_int off_sio
;
904 static u_int off_opc
;
905 static u_int off_dpc
;
906 static u_int off_sls
;
909 * This is the offset of the first byte after the ATM pseudo_header,
910 * or -1 if there is no ATM pseudo-header.
912 static u_int off_payload
;
915 * These are offsets to the beginning of the network-layer header.
916 * They are relative to the beginning of the link-layer payload (i.e.,
917 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
919 * If the link layer never uses 802.2 LLC:
921 * "off_nl" and "off_nl_nosnap" are the same.
923 * If the link layer always uses 802.2 LLC:
925 * "off_nl" is the offset if there's a SNAP header following
928 * "off_nl_nosnap" is the offset if there's no SNAP header.
930 * If the link layer is Ethernet:
932 * "off_nl" is the offset if the packet is an Ethernet II packet
933 * (we assume no 802.3+802.2+SNAP);
935 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
936 * with an 802.2 header following it.
939 static u_int off_nl_nosnap
;
942 static int prevlinktype
;
943 static int outermostlinktype
;
949 pcap_fddipad
= p
->fddipad
;
952 * We start out with only one link-layer header.
954 outermostlinktype
= pcap_datalink(p
);
955 off_outermostlinkhdr
.constant_part
= 0;
956 off_outermostlinkhdr
.is_variable
= 0;
957 off_outermostlinkhdr
.reg
= -1;
959 prevlinktype
= outermostlinktype
;
960 off_prevlinkhdr
.constant_part
= 0;
961 off_prevlinkhdr
.is_variable
= 0;
962 off_prevlinkhdr
.reg
= -1;
964 linktype
= outermostlinktype
;
965 off_linkhdr
.constant_part
= 0;
966 off_linkhdr
.is_variable
= 0;
967 off_linkhdr
.reg
= -1;
972 off_linkpl
.constant_part
= 0;
973 off_linkpl
.is_variable
= 0;
976 off_linktype
.constant_part
= 0;
977 off_linktype
.is_variable
= 0;
978 off_linktype
.reg
= -1;
981 * Assume it's not raw ATM with a pseudo-header, for now.
995 * And assume we're not doing SS7.
1004 label_stack_depth
= 0;
1005 vlan_stack_depth
= 0;
1010 off_linktype
.constant_part
= 2;
1011 off_linkpl
.constant_part
= 6;
1012 off_nl
= 0; /* XXX in reality, variable! */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1016 case DLT_ARCNET_LINUX
:
1017 off_linktype
.constant_part
= 4;
1018 off_linkpl
.constant_part
= 8;
1019 off_nl
= 0; /* XXX in reality, variable! */
1020 off_nl_nosnap
= 0; /* no 802.2 LLC */
1024 off_linktype
.constant_part
= 12;
1025 off_linkpl
.constant_part
= 14; /* Ethernet header length */
1026 off_nl
= 0; /* Ethernet II */
1027 off_nl_nosnap
= 3; /* 802.3+802.2 */
1032 * SLIP doesn't have a link level type. The 16 byte
1033 * header is hacked into our SLIP driver.
1035 off_linktype
.constant_part
= -1;
1036 off_linkpl
.constant_part
= 16;
1038 off_nl_nosnap
= 0; /* no 802.2 LLC */
1041 case DLT_SLIP_BSDOS
:
1042 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1043 off_linktype
.constant_part
= -1;
1045 off_linkpl
.constant_part
= 24;
1047 off_nl_nosnap
= 0; /* no 802.2 LLC */
1052 off_linktype
.constant_part
= 0;
1053 off_linkpl
.constant_part
= 4;
1055 off_nl_nosnap
= 0; /* no 802.2 LLC */
1059 off_linktype
.constant_part
= 0;
1060 off_linkpl
.constant_part
= 12;
1062 off_nl_nosnap
= 0; /* no 802.2 LLC */
1067 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1068 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1069 off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1070 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1072 off_nl_nosnap
= 0; /* no 802.2 LLC */
1077 * This does no include the Ethernet header, and
1078 * only covers session state.
1080 off_linktype
.constant_part
= 6;
1081 off_linkpl
.constant_part
= 8;
1083 off_nl_nosnap
= 0; /* no 802.2 LLC */
1087 off_linktype
.constant_part
= 5;
1088 off_linkpl
.constant_part
= 24;
1090 off_nl_nosnap
= 0; /* no 802.2 LLC */
1095 * FDDI doesn't really have a link-level type field.
1096 * We set "off_linktype" to the offset of the LLC header.
1098 * To check for Ethernet types, we assume that SSAP = SNAP
1099 * is being used and pick out the encapsulated Ethernet type.
1100 * XXX - should we generate code to check for SNAP?
1102 off_linktype
.constant_part
= 13;
1103 off_linktype
.constant_part
+= pcap_fddipad
;
1104 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1105 off_linkpl
.constant_part
+= pcap_fddipad
;
1106 off_nl
= 8; /* 802.2+SNAP */
1107 off_nl_nosnap
= 3; /* 802.2 */
1112 * Token Ring doesn't really have a link-level type field.
1113 * We set "off_linktype" to the offset of the LLC header.
1115 * To check for Ethernet types, we assume that SSAP = SNAP
1116 * is being used and pick out the encapsulated Ethernet type.
1117 * XXX - should we generate code to check for SNAP?
1119 * XXX - the header is actually variable-length.
1120 * Some various Linux patched versions gave 38
1121 * as "off_linktype" and 40 as "off_nl"; however,
1122 * if a token ring packet has *no* routing
1123 * information, i.e. is not source-routed, the correct
1124 * values are 20 and 22, as they are in the vanilla code.
1126 * A packet is source-routed iff the uppermost bit
1127 * of the first byte of the source address, at an
1128 * offset of 8, has the uppermost bit set. If the
1129 * packet is source-routed, the total number of bytes
1130 * of routing information is 2 plus bits 0x1F00 of
1131 * the 16-bit value at an offset of 14 (shifted right
1132 * 8 - figure out which byte that is).
1134 off_linktype
.constant_part
= 14;
1135 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1136 off_nl
= 8; /* 802.2+SNAP */
1137 off_nl_nosnap
= 3; /* 802.2 */
1140 case DLT_PRISM_HEADER
:
1141 case DLT_IEEE802_11_RADIO_AVS
:
1142 case DLT_IEEE802_11_RADIO
:
1143 off_linkhdr
.is_variable
= 1;
1144 /* Fall through, 802.11 doesn't have a variable link
1145 * prefix but is otherwise the same. */
1147 case DLT_IEEE802_11
:
1149 * 802.11 doesn't really have a link-level type field.
1150 * We set "off_linktype.constant_part" to the offset of
1153 * To check for Ethernet types, we assume that SSAP = SNAP
1154 * is being used and pick out the encapsulated Ethernet type.
1155 * XXX - should we generate code to check for SNAP?
1157 * We also handle variable-length radio headers here.
1158 * The Prism header is in theory variable-length, but in
1159 * practice it's always 144 bytes long. However, some
1160 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1161 * sometimes or always supply an AVS header, so we
1162 * have to check whether the radio header is a Prism
1163 * header or an AVS header, so, in practice, it's
1166 off_linktype
.constant_part
= 24;
1167 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1168 off_linkpl
.is_variable
= 1;
1169 off_nl
= 8; /* 802.2+SNAP */
1170 off_nl_nosnap
= 3; /* 802.2 */
1175 * At the moment we treat PPI the same way that we treat
1176 * normal Radiotap encoded packets. The difference is in
1177 * the function that generates the code at the beginning
1178 * to compute the header length. Since this code generator
1179 * of PPI supports bare 802.11 encapsulation only (i.e.
1180 * the encapsulated DLT should be DLT_IEEE802_11) we
1181 * generate code to check for this too.
1183 off_linktype
.constant_part
= 24;
1184 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1185 off_linkpl
.is_variable
= 1;
1186 off_linkhdr
.is_variable
= 1;
1187 off_nl
= 8; /* 802.2+SNAP */
1188 off_nl_nosnap
= 3; /* 802.2 */
1191 case DLT_ATM_RFC1483
:
1192 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1194 * assume routed, non-ISO PDUs
1195 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1197 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1198 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1199 * latter would presumably be treated the way PPPoE
1200 * should be, so you can do "pppoe and udp port 2049"
1201 * or "pppoa and tcp port 80" and have it check for
1202 * PPPo{A,E} and a PPP protocol of IP and....
1204 off_linktype
.constant_part
= 0;
1205 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1206 off_nl
= 8; /* 802.2+SNAP */
1207 off_nl_nosnap
= 3; /* 802.2 */
1212 * Full Frontal ATM; you get AALn PDUs with an ATM
1216 off_vpi
= SUNATM_VPI_POS
;
1217 off_vci
= SUNATM_VCI_POS
;
1218 off_proto
= PROTO_POS
;
1219 off_payload
= SUNATM_PKT_BEGIN_POS
;
1220 off_linktype
.constant_part
= off_payload
;
1221 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1222 off_nl
= 8; /* 802.2+SNAP */
1223 off_nl_nosnap
= 3; /* 802.2 */
1229 off_linktype
.constant_part
= -1;
1230 off_linkpl
.constant_part
= 0;
1232 off_nl_nosnap
= 0; /* no 802.2 LLC */
1235 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1236 off_linktype
.constant_part
= 14;
1237 off_linkpl
.constant_part
= 16;
1239 off_nl_nosnap
= 0; /* no 802.2 LLC */
1244 * LocalTalk does have a 1-byte type field in the LLAP header,
1245 * but really it just indicates whether there is a "short" or
1246 * "long" DDP packet following.
1248 off_linktype
.constant_part
= -1;
1249 off_linkpl
.constant_part
= 0;
1251 off_nl_nosnap
= 0; /* no 802.2 LLC */
1254 case DLT_IP_OVER_FC
:
1256 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1257 * link-level type field. We set "off_linktype" to the
1258 * offset of the LLC header.
1260 * To check for Ethernet types, we assume that SSAP = SNAP
1261 * is being used and pick out the encapsulated Ethernet type.
1262 * XXX - should we generate code to check for SNAP? RFC
1263 * 2625 says SNAP should be used.
1265 off_linktype
.constant_part
= 16;
1266 off_linkpl
.constant_part
= 16;
1267 off_nl
= 8; /* 802.2+SNAP */
1268 off_nl_nosnap
= 3; /* 802.2 */
1273 * XXX - we should set this to handle SNAP-encapsulated
1274 * frames (NLPID of 0x80).
1276 off_linktype
.constant_part
= -1;
1277 off_linkpl
.constant_part
= 0;
1279 off_nl_nosnap
= 0; /* no 802.2 LLC */
1283 * the only BPF-interesting FRF.16 frames are non-control frames;
1284 * Frame Relay has a variable length link-layer
1285 * so lets start with offset 4 for now and increments later on (FIXME);
1288 off_linktype
.constant_part
= -1;
1289 off_linkpl
.constant_part
= 0;
1291 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1294 case DLT_APPLE_IP_OVER_IEEE1394
:
1295 off_linktype
.constant_part
= 16;
1296 off_linkpl
.constant_part
= 18;
1298 off_nl_nosnap
= 0; /* no 802.2 LLC */
1301 case DLT_SYMANTEC_FIREWALL
:
1302 off_linktype
.constant_part
= 6;
1303 off_linkpl
.constant_part
= 44;
1304 off_nl
= 0; /* Ethernet II */
1305 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1308 #ifdef HAVE_NET_PFVAR_H
1310 off_linktype
.constant_part
= 0;
1311 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1313 off_nl_nosnap
= 0; /* no 802.2 LLC */
1317 case DLT_JUNIPER_MFR
:
1318 case DLT_JUNIPER_MLFR
:
1319 case DLT_JUNIPER_MLPPP
:
1320 case DLT_JUNIPER_PPP
:
1321 case DLT_JUNIPER_CHDLC
:
1322 case DLT_JUNIPER_FRELAY
:
1323 off_linktype
.constant_part
= 4;
1324 off_linkpl
.constant_part
= 4;
1326 off_nl_nosnap
= -1; /* no 802.2 LLC */
1329 case DLT_JUNIPER_ATM1
:
1330 off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1331 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1336 case DLT_JUNIPER_ATM2
:
1337 off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1338 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1343 /* frames captured on a Juniper PPPoE service PIC
1344 * contain raw ethernet frames */
1345 case DLT_JUNIPER_PPPOE
:
1346 case DLT_JUNIPER_ETHER
:
1347 off_linkpl
.constant_part
= 14;
1348 off_linktype
.constant_part
= 16;
1349 off_nl
= 18; /* Ethernet II */
1350 off_nl_nosnap
= 21; /* 802.3+802.2 */
1353 case DLT_JUNIPER_PPPOE_ATM
:
1354 off_linktype
.constant_part
= 4;
1355 off_linkpl
.constant_part
= 6;
1357 off_nl_nosnap
= -1; /* no 802.2 LLC */
1360 case DLT_JUNIPER_GGSN
:
1361 off_linktype
.constant_part
= 6;
1362 off_linkpl
.constant_part
= 12;
1364 off_nl_nosnap
= -1; /* no 802.2 LLC */
1367 case DLT_JUNIPER_ES
:
1368 off_linktype
.constant_part
= 6;
1369 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1370 off_nl
= -1; /* not really a network layer but raw IP addresses */
1371 off_nl_nosnap
= -1; /* no 802.2 LLC */
1374 case DLT_JUNIPER_MONITOR
:
1375 off_linktype
.constant_part
= 12;
1376 off_linkpl
.constant_part
= 12;
1377 off_nl
= 0; /* raw IP/IP6 header */
1378 off_nl_nosnap
= -1; /* no 802.2 LLC */
1381 case DLT_BACNET_MS_TP
:
1382 off_linktype
.constant_part
= -1;
1383 off_linkpl
.constant_part
= -1;
1388 case DLT_JUNIPER_SERVICES
:
1389 off_linktype
.constant_part
= 12;
1390 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1391 off_nl
= -1; /* L3 proto location dep. on cookie type */
1392 off_nl_nosnap
= -1; /* no 802.2 LLC */
1395 case DLT_JUNIPER_VP
:
1396 off_linktype
.constant_part
= 18;
1397 off_linkpl
.constant_part
= -1;
1402 case DLT_JUNIPER_ST
:
1403 off_linktype
.constant_part
= 18;
1404 off_linkpl
.constant_part
= -1;
1409 case DLT_JUNIPER_ISM
:
1410 off_linktype
.constant_part
= 8;
1411 off_linkpl
.constant_part
= -1;
1416 case DLT_JUNIPER_VS
:
1417 case DLT_JUNIPER_SRX_E2E
:
1418 case DLT_JUNIPER_FIBRECHANNEL
:
1419 case DLT_JUNIPER_ATM_CEMIC
:
1420 off_linktype
.constant_part
= 8;
1421 off_linkpl
.constant_part
= -1;
1433 off_linktype
.constant_part
= -1;
1434 off_linkpl
.constant_part
= -1;
1439 case DLT_MTP2_WITH_PHDR
:
1446 off_linktype
.constant_part
= -1;
1447 off_linkpl
.constant_part
= -1;
1459 off_linktype
.constant_part
= -1;
1460 off_linkpl
.constant_part
= -1;
1466 off_linktype
.constant_part
= -1;
1467 off_linkpl
.constant_part
= 4;
1474 * Currently, only raw "link[N:M]" filtering is supported.
1476 off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1477 off_linkpl
.constant_part
= -1;
1478 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1479 off_nl_nosnap
= -1; /* no 802.2 LLC */
1483 off_linktype
.constant_part
= 1;
1484 off_linkpl
.constant_part
= 24; /* ipnet header length */
1489 case DLT_NETANALYZER
:
1490 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1491 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1492 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1493 off_nl
= 0; /* Ethernet II */
1494 off_nl_nosnap
= 3; /* 802.3+802.2 */
1497 case DLT_NETANALYZER_TRANSPARENT
:
1498 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1499 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1500 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1501 off_nl
= 0; /* Ethernet II */
1502 off_nl_nosnap
= 3; /* 802.3+802.2 */
1507 * For values in the range in which we've assigned new
1508 * DLT_ values, only raw "link[N:M]" filtering is supported.
1510 if (linktype
>= DLT_MATCHING_MIN
&&
1511 linktype
<= DLT_MATCHING_MAX
) {
1512 off_linktype
.constant_part
= -1;
1513 off_linkpl
.constant_part
= -1;
1517 bpf_error("unknown data link type %d", linktype
);
1522 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1526 * Load a value relative to the specified absolute offset.
1528 static struct slist
*
1529 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1531 struct slist
*s
, *s2
;
1533 s
= gen_abs_offset_varpart(abs_offset
);
1536 * If "s" is non-null, it has code to arrange that the X register
1537 * contains the variable part of the absolute offset, so we
1538 * generate a load relative to that, with an offset of
1539 * abs_offset->constant_part + offset.
1541 * Otherwise, we can do an absolute load with an offset of
1542 * abs_offset->constant_part + offset.
1546 * "s" points to a list of statements that puts the
1547 * variable part of the absolute offset into the X register.
1548 * Do an indirect load, to use the X register as an offset.
1550 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1551 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1555 * There is no variable part of the absolute offset, so
1556 * just do an absolute load.
1558 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1559 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1565 * Load a value relative to the beginning of the specified header.
1567 static struct slist
*
1568 gen_load_a(offrel
, offset
, size
)
1569 enum e_offrel offrel
;
1572 struct slist
*s
, *s2
;
1577 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1582 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1585 case OR_PREVLINKHDR
:
1586 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1590 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1593 case OR_PREVMPLSHDR
:
1594 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1598 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1601 case OR_LINKPL_NOSNAP
:
1602 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1606 s
= gen_load_absoffsetrel(&off_linktype
, offset
, size
);
1611 * Load the X register with the length of the IPv4 header
1612 * (plus the offset of the link-layer header, if it's
1613 * preceded by a variable-length header such as a radio
1614 * header), in bytes.
1616 s
= gen_loadx_iphdrlen();
1619 * Load the item at {offset of the link-layer payload} +
1620 * {offset, relative to the start of the link-layer
1621 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1622 * {specified offset}.
1624 * If the offset of the link-layer payload is variable,
1625 * the variable part of that offset is included in the
1626 * value in the X register, and we include the constant
1627 * part in the offset of the load.
1629 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1630 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1635 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1646 * Generate code to load into the X register the sum of the length of
1647 * the IPv4 header and the variable part of the offset of the link-layer
1650 static struct slist
*
1651 gen_loadx_iphdrlen()
1653 struct slist
*s
, *s2
;
1655 s
= gen_abs_offset_varpart(&off_linkpl
);
1658 * The offset of the link-layer payload has a variable
1659 * part. "s" points to a list of statements that put
1660 * the variable part of that offset into the X register.
1662 * The 4*([k]&0xf) addressing mode can't be used, as we
1663 * don't have a constant offset, so we have to load the
1664 * value in question into the A register and add to it
1665 * the value from the X register.
1667 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1668 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1670 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1673 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1678 * The A register now contains the length of the IP header.
1679 * We need to add to it the variable part of the offset of
1680 * the link-layer payload, which is still in the X
1681 * register, and move the result into the X register.
1683 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1684 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1687 * The offset of the link-layer payload is a constant,
1688 * so no code was generated to load the (non-existent)
1689 * variable part of that offset.
1691 * This means we can use the 4*([k]&0xf) addressing
1692 * mode. Load the length of the IPv4 header, which
1693 * is at an offset of off_nl from the beginning of
1694 * the link-layer payload, and thus at an offset of
1695 * off_linkpl.constant_part + off_nl from the beginning
1696 * of the raw packet data, using that addressing mode.
1698 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1699 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1704 static struct block
*
1711 s
= new_stmt(BPF_LD
|BPF_IMM
);
1713 b
= new_block(JMP(BPF_JEQ
));
1719 static inline struct block
*
1722 return gen_uncond(1);
1725 static inline struct block
*
1728 return gen_uncond(0);
1732 * Byte-swap a 32-bit number.
1733 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1734 * big-endian platforms.)
1736 #define SWAPLONG(y) \
1737 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1740 * Generate code to match a particular packet type.
1742 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1743 * value, if <= ETHERMTU. We use that to determine whether to
1744 * match the type/length field or to check the type/length field for
1745 * a value <= ETHERMTU to see whether it's a type field and then do
1746 * the appropriate test.
1748 static struct block
*
1749 gen_ether_linktype(proto
)
1752 struct block
*b0
, *b1
;
1758 case LLCSAP_NETBEUI
:
1760 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1761 * so we check the DSAP and SSAP.
1763 * LLCSAP_IP checks for IP-over-802.2, rather
1764 * than IP-over-Ethernet or IP-over-SNAP.
1766 * XXX - should we check both the DSAP and the
1767 * SSAP, like this, or should we check just the
1768 * DSAP, as we do for other types <= ETHERMTU
1769 * (i.e., other SAP values)?
1771 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1773 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1774 ((proto
<< 8) | proto
));
1782 * Ethernet_II frames, which are Ethernet
1783 * frames with a frame type of ETHERTYPE_IPX;
1785 * Ethernet_802.3 frames, which are 802.3
1786 * frames (i.e., the type/length field is
1787 * a length field, <= ETHERMTU, rather than
1788 * a type field) with the first two bytes
1789 * after the Ethernet/802.3 header being
1792 * Ethernet_802.2 frames, which are 802.3
1793 * frames with an 802.2 LLC header and
1794 * with the IPX LSAP as the DSAP in the LLC
1797 * Ethernet_SNAP frames, which are 802.3
1798 * frames with an LLC header and a SNAP
1799 * header and with an OUI of 0x000000
1800 * (encapsulated Ethernet) and a protocol
1801 * ID of ETHERTYPE_IPX in the SNAP header.
1803 * XXX - should we generate the same code both
1804 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1808 * This generates code to check both for the
1809 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1811 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1812 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1816 * Now we add code to check for SNAP frames with
1817 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1819 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1823 * Now we generate code to check for 802.3
1824 * frames in general.
1826 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1830 * Now add the check for 802.3 frames before the
1831 * check for Ethernet_802.2 and Ethernet_802.3,
1832 * as those checks should only be done on 802.3
1833 * frames, not on Ethernet frames.
1838 * Now add the check for Ethernet_II frames, and
1839 * do that before checking for the other frame
1842 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1846 case ETHERTYPE_ATALK
:
1847 case ETHERTYPE_AARP
:
1849 * EtherTalk (AppleTalk protocols on Ethernet link
1850 * layer) may use 802.2 encapsulation.
1854 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1855 * we check for an Ethernet type field less than
1856 * 1500, which means it's an 802.3 length field.
1858 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1862 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1863 * SNAP packets with an organization code of
1864 * 0x080007 (Apple, for Appletalk) and a protocol
1865 * type of ETHERTYPE_ATALK (Appletalk).
1867 * 802.2-encapsulated ETHERTYPE_AARP packets are
1868 * SNAP packets with an organization code of
1869 * 0x000000 (encapsulated Ethernet) and a protocol
1870 * type of ETHERTYPE_AARP (Appletalk ARP).
1872 if (proto
== ETHERTYPE_ATALK
)
1873 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1874 else /* proto == ETHERTYPE_AARP */
1875 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1879 * Check for Ethernet encapsulation (Ethertalk
1880 * phase 1?); we just check for the Ethernet
1883 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1889 if (proto
<= ETHERMTU
) {
1891 * This is an LLC SAP value, so the frames
1892 * that match would be 802.2 frames.
1893 * Check that the frame is an 802.2 frame
1894 * (i.e., that the length/type field is
1895 * a length field, <= ETHERMTU) and
1896 * then check the DSAP.
1898 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1900 b1
= gen_cmp(OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1905 * This is an Ethernet type, so compare
1906 * the length/type field with it (if
1907 * the frame is an 802.2 frame, the length
1908 * field will be <= ETHERMTU, and, as
1909 * "proto" is > ETHERMTU, this test
1910 * will fail and the frame won't match,
1911 * which is what we want).
1913 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
1920 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1921 * or IPv6 then we have an error.
1923 static struct block
*
1924 gen_ipnet_linktype(proto
)
1930 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
1933 case ETHERTYPE_IPV6
:
1934 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
1935 (bpf_int32
)IPH_AF_INET6
);
1946 * Generate code to match a particular packet type.
1948 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1949 * value, if <= ETHERMTU. We use that to determine whether to
1950 * match the type field or to check the type field for the special
1951 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1953 static struct block
*
1954 gen_linux_sll_linktype(proto
)
1957 struct block
*b0
, *b1
;
1963 case LLCSAP_NETBEUI
:
1965 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1966 * so we check the DSAP and SSAP.
1968 * LLCSAP_IP checks for IP-over-802.2, rather
1969 * than IP-over-Ethernet or IP-over-SNAP.
1971 * XXX - should we check both the DSAP and the
1972 * SSAP, like this, or should we check just the
1973 * DSAP, as we do for other types <= ETHERMTU
1974 * (i.e., other SAP values)?
1976 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
1977 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1978 ((proto
<< 8) | proto
));
1984 * Ethernet_II frames, which are Ethernet
1985 * frames with a frame type of ETHERTYPE_IPX;
1987 * Ethernet_802.3 frames, which have a frame
1988 * type of LINUX_SLL_P_802_3;
1990 * Ethernet_802.2 frames, which are 802.3
1991 * frames with an 802.2 LLC header (i.e, have
1992 * a frame type of LINUX_SLL_P_802_2) and
1993 * with the IPX LSAP as the DSAP in the LLC
1996 * Ethernet_SNAP frames, which are 802.3
1997 * frames with an LLC header and a SNAP
1998 * header and with an OUI of 0x000000
1999 * (encapsulated Ethernet) and a protocol
2000 * ID of ETHERTYPE_IPX in the SNAP header.
2002 * First, do the checks on LINUX_SLL_P_802_2
2003 * frames; generate the check for either
2004 * Ethernet_802.2 or Ethernet_SNAP frames, and
2005 * then put a check for LINUX_SLL_P_802_2 frames
2008 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2009 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2011 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2015 * Now check for 802.3 frames and OR that with
2016 * the previous test.
2018 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2022 * Now add the check for Ethernet_II frames, and
2023 * do that before checking for the other frame
2026 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2030 case ETHERTYPE_ATALK
:
2031 case ETHERTYPE_AARP
:
2033 * EtherTalk (AppleTalk protocols on Ethernet link
2034 * layer) may use 802.2 encapsulation.
2038 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2039 * we check for the 802.2 protocol type in the
2040 * "Ethernet type" field.
2042 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2045 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2046 * SNAP packets with an organization code of
2047 * 0x080007 (Apple, for Appletalk) and a protocol
2048 * type of ETHERTYPE_ATALK (Appletalk).
2050 * 802.2-encapsulated ETHERTYPE_AARP packets are
2051 * SNAP packets with an organization code of
2052 * 0x000000 (encapsulated Ethernet) and a protocol
2053 * type of ETHERTYPE_AARP (Appletalk ARP).
2055 if (proto
== ETHERTYPE_ATALK
)
2056 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2057 else /* proto == ETHERTYPE_AARP */
2058 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2062 * Check for Ethernet encapsulation (Ethertalk
2063 * phase 1?); we just check for the Ethernet
2066 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2072 if (proto
<= ETHERMTU
) {
2074 * This is an LLC SAP value, so the frames
2075 * that match would be 802.2 frames.
2076 * Check for the 802.2 protocol type
2077 * in the "Ethernet type" field, and
2078 * then check the DSAP.
2080 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2081 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2087 * This is an Ethernet type, so compare
2088 * the length/type field with it (if
2089 * the frame is an 802.2 frame, the length
2090 * field will be <= ETHERMTU, and, as
2091 * "proto" is > ETHERMTU, this test
2092 * will fail and the frame won't match,
2093 * which is what we want).
2095 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2100 static struct slist
*
2101 gen_load_prism_llprefixlen()
2103 struct slist
*s1
, *s2
;
2104 struct slist
*sjeq_avs_cookie
;
2105 struct slist
*sjcommon
;
2108 * This code is not compatible with the optimizer, as
2109 * we are generating jmp instructions within a normal
2110 * slist of instructions
2115 * Generate code to load the length of the radio header into
2116 * the register assigned to hold that length, if one has been
2117 * assigned. (If one hasn't been assigned, no code we've
2118 * generated uses that prefix, so we don't need to generate any
2121 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2122 * or always use the AVS header rather than the Prism header.
2123 * We load a 4-byte big-endian value at the beginning of the
2124 * raw packet data, and see whether, when masked with 0xFFFFF000,
2125 * it's equal to 0x80211000. If so, that indicates that it's
2126 * an AVS header (the masked-out bits are the version number).
2127 * Otherwise, it's a Prism header.
2129 * XXX - the Prism header is also, in theory, variable-length,
2130 * but no known software generates headers that aren't 144
2133 if (off_linkhdr
.reg
!= -1) {
2137 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2141 * AND it with 0xFFFFF000.
2143 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2144 s2
->s
.k
= 0xFFFFF000;
2148 * Compare with 0x80211000.
2150 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2151 sjeq_avs_cookie
->s
.k
= 0x80211000;
2152 sappend(s1
, sjeq_avs_cookie
);
2157 * The 4 bytes at an offset of 4 from the beginning of
2158 * the AVS header are the length of the AVS header.
2159 * That field is big-endian.
2161 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2164 sjeq_avs_cookie
->s
.jt
= s2
;
2167 * Now jump to the code to allocate a register
2168 * into which to save the header length and
2169 * store the length there. (The "jump always"
2170 * instruction needs to have the k field set;
2171 * it's added to the PC, so, as we're jumping
2172 * over a single instruction, it should be 1.)
2174 sjcommon
= new_stmt(JMP(BPF_JA
));
2176 sappend(s1
, sjcommon
);
2179 * Now for the code that handles the Prism header.
2180 * Just load the length of the Prism header (144)
2181 * into the A register. Have the test for an AVS
2182 * header branch here if we don't have an AVS header.
2184 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2187 sjeq_avs_cookie
->s
.jf
= s2
;
2190 * Now allocate a register to hold that value and store
2191 * it. The code for the AVS header will jump here after
2192 * loading the length of the AVS header.
2194 s2
= new_stmt(BPF_ST
);
2195 s2
->s
.k
= off_linkhdr
.reg
;
2197 sjcommon
->s
.jf
= s2
;
2200 * Now move it into the X register.
2202 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2210 static struct slist
*
2211 gen_load_avs_llprefixlen()
2213 struct slist
*s1
, *s2
;
2216 * Generate code to load the length of the AVS header into
2217 * the register assigned to hold that length, if one has been
2218 * assigned. (If one hasn't been assigned, no code we've
2219 * generated uses that prefix, so we don't need to generate any
2222 if (off_linkhdr
.reg
!= -1) {
2224 * The 4 bytes at an offset of 4 from the beginning of
2225 * the AVS header are the length of the AVS header.
2226 * That field is big-endian.
2228 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2232 * Now allocate a register to hold that value and store
2235 s2
= new_stmt(BPF_ST
);
2236 s2
->s
.k
= off_linkhdr
.reg
;
2240 * Now move it into the X register.
2242 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2250 static struct slist
*
2251 gen_load_radiotap_llprefixlen()
2253 struct slist
*s1
, *s2
;
2256 * Generate code to load the length of the radiotap header into
2257 * the register assigned to hold that length, if one has been
2258 * assigned. (If one hasn't been assigned, no code we've
2259 * generated uses that prefix, so we don't need to generate any
2262 if (off_linkhdr
.reg
!= -1) {
2264 * The 2 bytes at offsets of 2 and 3 from the beginning
2265 * of the radiotap header are the length of the radiotap
2266 * header; unfortunately, it's little-endian, so we have
2267 * to load it a byte at a time and construct the value.
2271 * Load the high-order byte, at an offset of 3, shift it
2272 * left a byte, and put the result in the X register.
2274 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2276 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2279 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2283 * Load the next byte, at an offset of 2, and OR the
2284 * value from the X register into it.
2286 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2289 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2293 * Now allocate a register to hold that value and store
2296 s2
= new_stmt(BPF_ST
);
2297 s2
->s
.k
= off_linkhdr
.reg
;
2301 * Now move it into the X register.
2303 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2312 * At the moment we treat PPI as normal Radiotap encoded
2313 * packets. The difference is in the function that generates
2314 * the code at the beginning to compute the header length.
2315 * Since this code generator of PPI supports bare 802.11
2316 * encapsulation only (i.e. the encapsulated DLT should be
2317 * DLT_IEEE802_11) we generate code to check for this too;
2318 * that's done in finish_parse().
2320 static struct slist
*
2321 gen_load_ppi_llprefixlen()
2323 struct slist
*s1
, *s2
;
2326 * Generate code to load the length of the radiotap header
2327 * into the register assigned to hold that length, if one has
2330 if (off_linkhdr
.reg
!= -1) {
2332 * The 2 bytes at offsets of 2 and 3 from the beginning
2333 * of the radiotap header are the length of the radiotap
2334 * header; unfortunately, it's little-endian, so we have
2335 * to load it a byte at a time and construct the value.
2339 * Load the high-order byte, at an offset of 3, shift it
2340 * left a byte, and put the result in the X register.
2342 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2344 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2347 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2351 * Load the next byte, at an offset of 2, and OR the
2352 * value from the X register into it.
2354 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2357 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2361 * Now allocate a register to hold that value and store
2364 s2
= new_stmt(BPF_ST
);
2365 s2
->s
.k
= off_linkhdr
.reg
;
2369 * Now move it into the X register.
2371 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2380 * Load a value relative to the beginning of the link-layer header after the 802.11
2381 * header, i.e. LLC_SNAP.
2382 * The link-layer header doesn't necessarily begin at the beginning
2383 * of the packet data; there might be a variable-length prefix containing
2384 * radio information.
2386 static struct slist
*
2387 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2390 struct slist
*sjset_data_frame_1
;
2391 struct slist
*sjset_data_frame_2
;
2392 struct slist
*sjset_qos
;
2393 struct slist
*sjset_radiotap_flags
;
2394 struct slist
*sjset_radiotap_tsft
;
2395 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2396 struct slist
*s_roundup
;
2398 if (off_linkpl
.reg
== -1) {
2400 * No register has been assigned to the offset of
2401 * the link-layer payload, which means nobody needs
2402 * it; don't bother computing it - just return
2403 * what we already have.
2409 * This code is not compatible with the optimizer, as
2410 * we are generating jmp instructions within a normal
2411 * slist of instructions
2416 * If "s" is non-null, it has code to arrange that the X register
2417 * contains the length of the prefix preceding the link-layer
2420 * Otherwise, the length of the prefix preceding the link-layer
2421 * header is "off_outermostlinkhdr.constant_part".
2425 * There is no variable-length header preceding the
2426 * link-layer header.
2428 * Load the length of the fixed-length prefix preceding
2429 * the link-layer header (if any) into the X register,
2430 * and store it in the off_linkpl.reg register.
2431 * That length is off_outermostlinkhdr.constant_part.
2433 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2434 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2438 * The X register contains the offset of the beginning of the
2439 * link-layer header; add 24, which is the minimum length
2440 * of the MAC header for a data frame, to that, and store it
2441 * in off_linkpl.reg, and then load the Frame Control field,
2442 * which is at the offset in the X register, with an indexed load.
2444 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2446 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2449 s2
= new_stmt(BPF_ST
);
2450 s2
->s
.k
= off_linkpl
.reg
;
2453 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2458 * Check the Frame Control field to see if this is a data frame;
2459 * a data frame has the 0x08 bit (b3) in that field set and the
2460 * 0x04 bit (b2) clear.
2462 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2463 sjset_data_frame_1
->s
.k
= 0x08;
2464 sappend(s
, sjset_data_frame_1
);
2467 * If b3 is set, test b2, otherwise go to the first statement of
2468 * the rest of the program.
2470 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2471 sjset_data_frame_2
->s
.k
= 0x04;
2472 sappend(s
, sjset_data_frame_2
);
2473 sjset_data_frame_1
->s
.jf
= snext
;
2476 * If b2 is not set, this is a data frame; test the QoS bit.
2477 * Otherwise, go to the first statement of the rest of the
2480 sjset_data_frame_2
->s
.jt
= snext
;
2481 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2482 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2483 sappend(s
, sjset_qos
);
2486 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2488 * Otherwise, go to the first statement of the rest of the
2491 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2492 s2
->s
.k
= off_linkpl
.reg
;
2494 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2497 s2
= new_stmt(BPF_ST
);
2498 s2
->s
.k
= off_linkpl
.reg
;
2502 * If we have a radiotap header, look at it to see whether
2503 * there's Atheros padding between the MAC-layer header
2506 * Note: all of the fields in the radiotap header are
2507 * little-endian, so we byte-swap all of the values
2508 * we test against, as they will be loaded as big-endian
2511 if (linktype
== DLT_IEEE802_11_RADIO
) {
2513 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2514 * in the presence flag?
2516 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2520 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2521 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2522 sappend(s
, sjset_radiotap_flags
);
2525 * If not, skip all of this.
2527 sjset_radiotap_flags
->s
.jf
= snext
;
2530 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2532 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2533 new_stmt(JMP(BPF_JSET
));
2534 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2535 sappend(s
, sjset_radiotap_tsft
);
2538 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2539 * at an offset of 16 from the beginning of the raw packet
2540 * data (8 bytes for the radiotap header and 8 bytes for
2543 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2546 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2550 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2551 sjset_tsft_datapad
->s
.k
= 0x20;
2552 sappend(s
, sjset_tsft_datapad
);
2555 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2556 * at an offset of 8 from the beginning of the raw packet
2557 * data (8 bytes for the radiotap header).
2559 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2562 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2566 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2567 sjset_notsft_datapad
->s
.k
= 0x20;
2568 sappend(s
, sjset_notsft_datapad
);
2571 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2572 * set, round the length of the 802.11 header to
2573 * a multiple of 4. Do that by adding 3 and then
2574 * dividing by and multiplying by 4, which we do by
2577 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2578 s_roundup
->s
.k
= off_linkpl
.reg
;
2579 sappend(s
, s_roundup
);
2580 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2583 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2586 s2
= new_stmt(BPF_ST
);
2587 s2
->s
.k
= off_linkpl
.reg
;
2590 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2591 sjset_tsft_datapad
->s
.jf
= snext
;
2592 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2593 sjset_notsft_datapad
->s
.jf
= snext
;
2595 sjset_qos
->s
.jf
= snext
;
2601 insert_compute_vloffsets(b
)
2606 /* There is an implicit dependency between the link
2607 * payload and link header since the payload computation
2608 * includes the variable part of the header. Therefore,
2609 * if nobody else has allocated a register for the link
2610 * header and we need it, do it now. */
2611 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2612 off_linkhdr
.reg
== -1)
2613 off_linkhdr
.reg
= alloc_reg();
2616 * For link-layer types that have a variable-length header
2617 * preceding the link-layer header, generate code to load
2618 * the offset of the link-layer header into the register
2619 * assigned to that offset, if any.
2621 * XXX - this, and the next switch statement, won't handle
2622 * encapsulation of 802.11 or 802.11+radio information in
2623 * some other protocol stack. That's significantly more
2626 switch (outermostlinktype
) {
2628 case DLT_PRISM_HEADER
:
2629 s
= gen_load_prism_llprefixlen();
2632 case DLT_IEEE802_11_RADIO_AVS
:
2633 s
= gen_load_avs_llprefixlen();
2636 case DLT_IEEE802_11_RADIO
:
2637 s
= gen_load_radiotap_llprefixlen();
2641 s
= gen_load_ppi_llprefixlen();
2650 * For link-layer types that have a variable-length link-layer
2651 * header, generate code to load the offset of the link-layer
2652 * payload into the register assigned to that offset, if any.
2654 switch (outermostlinktype
) {
2656 case DLT_IEEE802_11
:
2657 case DLT_PRISM_HEADER
:
2658 case DLT_IEEE802_11_RADIO_AVS
:
2659 case DLT_IEEE802_11_RADIO
:
2661 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2666 * If we have any offset-loading code, append all the
2667 * existing statements in the block to those statements,
2668 * and make the resulting list the list of statements
2672 sappend(s
, b
->stmts
);
2677 static struct block
*
2678 gen_ppi_dlt_check(void)
2680 struct slist
*s_load_dlt
;
2683 if (linktype
== DLT_PPI
)
2685 /* Create the statements that check for the DLT
2687 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2688 s_load_dlt
->s
.k
= 4;
2690 b
= new_block(JMP(BPF_JEQ
));
2692 b
->stmts
= s_load_dlt
;
2693 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2704 * Take an absolute offset, and:
2706 * if it has no variable part, return NULL;
2708 * if it has a variable part, generate code to load the register
2709 * containing that variable part into the X register, returning
2710 * a pointer to that code - if no register for that offset has
2711 * been allocated, allocate it first.
2713 * (The code to set that register will be generated later, but will
2714 * be placed earlier in the code sequence.)
2716 static struct slist
*
2717 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2721 if (off
->is_variable
) {
2722 if (off
->reg
== -1) {
2724 * We haven't yet assigned a register for the
2725 * variable part of the offset of the link-layer
2726 * header; allocate one.
2728 off
->reg
= alloc_reg();
2732 * Load the register containing the variable part of the
2733 * offset of the link-layer header into the X register.
2735 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2740 * That offset isn't variable, there's no variable part,
2741 * so we don't need to generate any code.
2748 * Map an Ethernet type to the equivalent PPP type.
2751 ethertype_to_ppptype(proto
)
2760 case ETHERTYPE_IPV6
:
2768 case ETHERTYPE_ATALK
:
2782 * I'm assuming the "Bridging PDU"s that go
2783 * over PPP are Spanning Tree Protocol
2797 * Generate any tests that, for encapsulation of a link-layer packet
2798 * inside another protocol stack, need to be done to check for those
2799 * link-layer packets (and that haven't already been done by a check
2800 * for that encapsulation).
2802 static struct block
*
2803 gen_prevlinkhdr_check(void)
2808 return gen_geneve_ll_check();
2810 switch (prevlinktype
) {
2814 * This is LANE-encapsulated Ethernet; check that the LANE
2815 * packet doesn't begin with an LE Control marker, i.e.
2816 * that it's data, not a control message.
2818 * (We've already generated a test for LANE.)
2820 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2826 * No such tests are necessary.
2834 * Generate code to match a particular packet type by matching the
2835 * link-layer type field or fields in the 802.2 LLC header.
2837 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2838 * value, if <= ETHERMTU.
2840 static struct block
*
2844 struct block
*b0
, *b1
, *b2
;
2845 const char *description
;
2847 /* are we checking MPLS-encapsulated packets? */
2848 if (label_stack_depth
> 0) {
2852 /* FIXME add other L3 proto IDs */
2853 return gen_mpls_linktype(Q_IP
);
2855 case ETHERTYPE_IPV6
:
2857 /* FIXME add other L3 proto IDs */
2858 return gen_mpls_linktype(Q_IPV6
);
2861 bpf_error("unsupported protocol over mpls");
2869 case DLT_NETANALYZER
:
2870 case DLT_NETANALYZER_TRANSPARENT
:
2871 /* Geneve has an EtherType regardless of whether there is an
2874 b0
= gen_prevlinkhdr_check();
2878 b1
= gen_ether_linktype(proto
);
2889 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2893 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2899 case DLT_IEEE802_11
:
2900 case DLT_PRISM_HEADER
:
2901 case DLT_IEEE802_11_RADIO_AVS
:
2902 case DLT_IEEE802_11_RADIO
:
2905 * Check that we have a data frame.
2907 b0
= gen_check_802_11_data_frame();
2910 * Now check for the specified link-layer type.
2912 b1
= gen_llc_linktype(proto
);
2920 * XXX - check for LLC frames.
2922 return gen_llc_linktype(proto
);
2928 * XXX - check for LLC PDUs, as per IEEE 802.5.
2930 return gen_llc_linktype(proto
);
2934 case DLT_ATM_RFC1483
:
2936 case DLT_IP_OVER_FC
:
2937 return gen_llc_linktype(proto
);
2943 * Check for an LLC-encapsulated version of this protocol;
2944 * if we were checking for LANE, linktype would no longer
2947 * Check for LLC encapsulation and then check the protocol.
2949 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2950 b1
= gen_llc_linktype(proto
);
2957 return gen_linux_sll_linktype(proto
);
2962 case DLT_SLIP_BSDOS
:
2965 * These types don't provide any type field; packets
2966 * are always IPv4 or IPv6.
2968 * XXX - for IPv4, check for a version number of 4, and,
2969 * for IPv6, check for a version number of 6?
2974 /* Check for a version number of 4. */
2975 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
2977 case ETHERTYPE_IPV6
:
2978 /* Check for a version number of 6. */
2979 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
2982 return gen_false(); /* always false */
2989 * Raw IPv4, so no type field.
2991 if (proto
== ETHERTYPE_IP
)
2992 return gen_true(); /* always true */
2994 /* Checking for something other than IPv4; always false */
3001 * Raw IPv6, so no type field.
3003 if (proto
== ETHERTYPE_IPV6
)
3004 return gen_true(); /* always true */
3006 /* Checking for something other than IPv6; always false */
3013 case DLT_PPP_SERIAL
:
3016 * We use Ethernet protocol types inside libpcap;
3017 * map them to the corresponding PPP protocol types.
3019 proto
= ethertype_to_ppptype(proto
);
3020 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3026 * We use Ethernet protocol types inside libpcap;
3027 * map them to the corresponding PPP protocol types.
3033 * Also check for Van Jacobson-compressed IP.
3034 * XXX - do this for other forms of PPP?
3036 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3037 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3039 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3044 proto
= ethertype_to_ppptype(proto
);
3045 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3055 * For DLT_NULL, the link-layer header is a 32-bit
3056 * word containing an AF_ value in *host* byte order,
3057 * and for DLT_ENC, the link-layer header begins
3058 * with a 32-bit work containing an AF_ value in
3061 * In addition, if we're reading a saved capture file,
3062 * the host byte order in the capture may not be the
3063 * same as the host byte order on this machine.
3065 * For DLT_LOOP, the link-layer header is a 32-bit
3066 * word containing an AF_ value in *network* byte order.
3068 * XXX - AF_ values may, unfortunately, be platform-
3069 * dependent; for example, FreeBSD's AF_INET6 is 24
3070 * whilst NetBSD's and OpenBSD's is 26.
3072 * This means that, when reading a capture file, just
3073 * checking for our AF_INET6 value won't work if the
3074 * capture file came from another OS.
3083 case ETHERTYPE_IPV6
:
3090 * Not a type on which we support filtering.
3091 * XXX - support those that have AF_ values
3092 * #defined on this platform, at least?
3097 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3099 * The AF_ value is in host byte order, but
3100 * the BPF interpreter will convert it to
3101 * network byte order.
3103 * If this is a save file, and it's from a
3104 * machine with the opposite byte order to
3105 * ours, we byte-swap the AF_ value.
3107 * Then we run it through "htonl()", and
3108 * generate code to compare against the result.
3110 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3111 proto
= SWAPLONG(proto
);
3112 proto
= htonl(proto
);
3114 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3116 #ifdef HAVE_NET_PFVAR_H
3119 * af field is host byte order in contrast to the rest of
3122 if (proto
== ETHERTYPE_IP
)
3123 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3124 BPF_B
, (bpf_int32
)AF_INET
));
3125 else if (proto
== ETHERTYPE_IPV6
)
3126 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3127 BPF_B
, (bpf_int32
)AF_INET6
));
3132 #endif /* HAVE_NET_PFVAR_H */
3135 case DLT_ARCNET_LINUX
:
3137 * XXX should we check for first fragment if the protocol
3145 case ETHERTYPE_IPV6
:
3146 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3147 (bpf_int32
)ARCTYPE_INET6
));
3150 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3151 (bpf_int32
)ARCTYPE_IP
);
3152 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3153 (bpf_int32
)ARCTYPE_IP_OLD
);
3158 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3159 (bpf_int32
)ARCTYPE_ARP
);
3160 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3161 (bpf_int32
)ARCTYPE_ARP_OLD
);
3165 case ETHERTYPE_REVARP
:
3166 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3167 (bpf_int32
)ARCTYPE_REVARP
));
3169 case ETHERTYPE_ATALK
:
3170 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3171 (bpf_int32
)ARCTYPE_ATALK
));
3178 case ETHERTYPE_ATALK
:
3188 * XXX - assumes a 2-byte Frame Relay header with
3189 * DLCI and flags. What if the address is longer?
3195 * Check for the special NLPID for IP.
3197 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3199 case ETHERTYPE_IPV6
:
3201 * Check for the special NLPID for IPv6.
3203 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3207 * Check for several OSI protocols.
3209 * Frame Relay packets typically have an OSI
3210 * NLPID at the beginning; we check for each
3213 * What we check for is the NLPID and a frame
3214 * control field of UI, i.e. 0x03 followed
3217 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3218 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3219 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3231 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3233 case DLT_JUNIPER_MFR
:
3234 case DLT_JUNIPER_MLFR
:
3235 case DLT_JUNIPER_MLPPP
:
3236 case DLT_JUNIPER_ATM1
:
3237 case DLT_JUNIPER_ATM2
:
3238 case DLT_JUNIPER_PPPOE
:
3239 case DLT_JUNIPER_PPPOE_ATM
:
3240 case DLT_JUNIPER_GGSN
:
3241 case DLT_JUNIPER_ES
:
3242 case DLT_JUNIPER_MONITOR
:
3243 case DLT_JUNIPER_SERVICES
:
3244 case DLT_JUNIPER_ETHER
:
3245 case DLT_JUNIPER_PPP
:
3246 case DLT_JUNIPER_FRELAY
:
3247 case DLT_JUNIPER_CHDLC
:
3248 case DLT_JUNIPER_VP
:
3249 case DLT_JUNIPER_ST
:
3250 case DLT_JUNIPER_ISM
:
3251 case DLT_JUNIPER_VS
:
3252 case DLT_JUNIPER_SRX_E2E
:
3253 case DLT_JUNIPER_FIBRECHANNEL
:
3254 case DLT_JUNIPER_ATM_CEMIC
:
3256 /* just lets verify the magic number for now -
3257 * on ATM we may have up to 6 different encapsulations on the wire
3258 * and need a lot of heuristics to figure out that the payload
3261 * FIXME encapsulation specific BPF_ filters
3263 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3265 case DLT_BACNET_MS_TP
:
3266 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3269 return gen_ipnet_linktype(proto
);
3271 case DLT_LINUX_IRDA
:
3272 bpf_error("IrDA link-layer type filtering not implemented");
3275 bpf_error("DOCSIS link-layer type filtering not implemented");
3278 case DLT_MTP2_WITH_PHDR
:
3279 bpf_error("MTP2 link-layer type filtering not implemented");
3282 bpf_error("ERF link-layer type filtering not implemented");
3285 bpf_error("PFSYNC link-layer type filtering not implemented");
3287 case DLT_LINUX_LAPD
:
3288 bpf_error("LAPD link-layer type filtering not implemented");
3292 case DLT_USB_LINUX_MMAPPED
:
3293 bpf_error("USB link-layer type filtering not implemented");
3295 case DLT_BLUETOOTH_HCI_H4
:
3296 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3297 bpf_error("Bluetooth link-layer type filtering not implemented");
3300 case DLT_CAN_SOCKETCAN
:
3301 bpf_error("CAN link-layer type filtering not implemented");
3303 case DLT_IEEE802_15_4
:
3304 case DLT_IEEE802_15_4_LINUX
:
3305 case DLT_IEEE802_15_4_NONASK_PHY
:
3306 case DLT_IEEE802_15_4_NOFCS
:
3307 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3309 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3310 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3313 bpf_error("SITA link-layer type filtering not implemented");
3316 bpf_error("RAIF1 link-layer type filtering not implemented");
3319 bpf_error("IPMB link-layer type filtering not implemented");
3322 bpf_error("AX.25 link-layer type filtering not implemented");
3325 /* Using the fixed-size NFLOG header it is possible to tell only
3326 * the address family of the packet, other meaningful data is
3327 * either missing or behind TLVs.
3329 bpf_error("NFLOG link-layer type filtering not implemented");
3333 * Does this link-layer header type have a field
3334 * indicating the type of the next protocol? If
3335 * so, off_linktype.constant_part will be the offset of that
3336 * field in the packet; if not, it will be -1.
3338 if (off_linktype
.constant_part
!= (u_int
)-1) {
3340 * Yes; assume it's an Ethernet type. (If
3341 * it's not, it needs to be handled specially
3344 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3347 * No; report an error.
3349 description
= pcap_datalink_val_to_description(linktype
);
3350 if (description
!= NULL
) {
3351 bpf_error("%s link-layer type filtering not implemented",
3354 bpf_error("DLT %u link-layer type filtering not implemented",
3363 * Check for an LLC SNAP packet with a given organization code and
3364 * protocol type; we check the entire contents of the 802.2 LLC and
3365 * snap headers, checking for DSAP and SSAP of SNAP and a control
3366 * field of 0x03 in the LLC header, and for the specified organization
3367 * code and protocol type in the SNAP header.
3369 static struct block
*
3370 gen_snap(orgcode
, ptype
)
3371 bpf_u_int32 orgcode
;
3374 u_char snapblock
[8];
3376 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3377 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3378 snapblock
[2] = 0x03; /* control = UI */
3379 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3380 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3381 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3382 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3383 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3384 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3388 * Generate code to match frames with an LLC header.
3393 struct block
*b0
, *b1
;
3399 * We check for an Ethernet type field less than
3400 * 1500, which means it's an 802.3 length field.
3402 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3406 * Now check for the purported DSAP and SSAP not being
3407 * 0xFF, to rule out NetWare-over-802.3.
3409 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3416 * We check for LLC traffic.
3418 b0
= gen_atmtype_abbrev(A_LLC
);
3421 case DLT_IEEE802
: /* Token Ring */
3423 * XXX - check for LLC frames.
3429 * XXX - check for LLC frames.
3433 case DLT_ATM_RFC1483
:
3435 * For LLC encapsulation, these are defined to have an
3438 * For VC encapsulation, they don't, but there's no
3439 * way to check for that; the protocol used on the VC
3440 * is negotiated out of band.
3444 case DLT_IEEE802_11
:
3445 case DLT_PRISM_HEADER
:
3446 case DLT_IEEE802_11_RADIO
:
3447 case DLT_IEEE802_11_RADIO_AVS
:
3450 * Check that we have a data frame.
3452 b0
= gen_check_802_11_data_frame();
3456 bpf_error("'llc' not supported for linktype %d", linktype
);
3464 struct block
*b0
, *b1
;
3468 * Check whether this is an LLC frame.
3473 * Load the control byte and test the low-order bit; it must
3474 * be clear for I frames.
3476 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3477 b1
= new_block(JMP(BPF_JSET
));
3488 struct block
*b0
, *b1
;
3491 * Check whether this is an LLC frame.
3496 * Now compare the low-order 2 bit of the control byte against
3497 * the appropriate value for S frames.
3499 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3507 struct block
*b0
, *b1
;
3510 * Check whether this is an LLC frame.
3515 * Now compare the low-order 2 bit of the control byte against
3516 * the appropriate value for U frames.
3518 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3524 gen_llc_s_subtype(bpf_u_int32 subtype
)
3526 struct block
*b0
, *b1
;
3529 * Check whether this is an LLC frame.
3534 * Now check for an S frame with the appropriate type.
3536 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3542 gen_llc_u_subtype(bpf_u_int32 subtype
)
3544 struct block
*b0
, *b1
;
3547 * Check whether this is an LLC frame.
3552 * Now check for a U frame with the appropriate type.
3554 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3560 * Generate code to match a particular packet type, for link-layer types
3561 * using 802.2 LLC headers.
3563 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3564 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3566 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3567 * value, if <= ETHERMTU. We use that to determine whether to
3568 * match the DSAP or both DSAP and LSAP or to check the OUI and
3569 * protocol ID in a SNAP header.
3571 static struct block
*
3572 gen_llc_linktype(proto
)
3576 * XXX - handle token-ring variable-length header.
3582 case LLCSAP_NETBEUI
:
3584 * XXX - should we check both the DSAP and the
3585 * SSAP, like this, or should we check just the
3586 * DSAP, as we do for other SAP values?
3588 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3589 ((proto
<< 8) | proto
));
3593 * XXX - are there ever SNAP frames for IPX on
3594 * non-Ethernet 802.x networks?
3596 return gen_cmp(OR_LLC
, 0, BPF_B
,
3597 (bpf_int32
)LLCSAP_IPX
);
3599 case ETHERTYPE_ATALK
:
3601 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3602 * SNAP packets with an organization code of
3603 * 0x080007 (Apple, for Appletalk) and a protocol
3604 * type of ETHERTYPE_ATALK (Appletalk).
3606 * XXX - check for an organization code of
3607 * encapsulated Ethernet as well?
3609 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3613 * XXX - we don't have to check for IPX 802.3
3614 * here, but should we check for the IPX Ethertype?
3616 if (proto
<= ETHERMTU
) {
3618 * This is an LLC SAP value, so check
3621 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3624 * This is an Ethernet type; we assume that it's
3625 * unlikely that it'll appear in the right place
3626 * at random, and therefore check only the
3627 * location that would hold the Ethernet type
3628 * in a SNAP frame with an organization code of
3629 * 0x000000 (encapsulated Ethernet).
3631 * XXX - if we were to check for the SNAP DSAP and
3632 * LSAP, as per XXX, and were also to check for an
3633 * organization code of 0x000000 (encapsulated
3634 * Ethernet), we'd do
3636 * return gen_snap(0x000000, proto);
3638 * here; for now, we don't, as per the above.
3639 * I don't know whether it's worth the extra CPU
3640 * time to do the right check or not.
3642 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3647 static struct block
*
3648 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3652 u_int src_off
, dst_off
;
3654 struct block
*b0
, *b1
;
3668 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3669 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3675 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3676 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3683 b0
= gen_linktype(proto
);
3684 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3690 static struct block
*
3691 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3692 struct in6_addr
*addr
;
3693 struct in6_addr
*mask
;
3695 u_int src_off
, dst_off
;
3697 struct block
*b0
, *b1
;
3712 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3713 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3719 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3720 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3727 /* this order is important */
3728 a
= (u_int32_t
*)addr
;
3729 m
= (u_int32_t
*)mask
;
3730 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3731 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3733 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3735 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3737 b0
= gen_linktype(proto
);
3743 static struct block
*
3744 gen_ehostop(eaddr
, dir
)
3745 register const u_char
*eaddr
;
3748 register struct block
*b0
, *b1
;
3752 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3755 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3758 b0
= gen_ehostop(eaddr
, Q_SRC
);
3759 b1
= gen_ehostop(eaddr
, Q_DST
);
3765 b0
= gen_ehostop(eaddr
, Q_SRC
);
3766 b1
= gen_ehostop(eaddr
, Q_DST
);
3771 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3775 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3779 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3783 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3787 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3791 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3799 * Like gen_ehostop, but for DLT_FDDI
3801 static struct block
*
3802 gen_fhostop(eaddr
, dir
)
3803 register const u_char
*eaddr
;
3806 struct block
*b0
, *b1
;
3810 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3813 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3816 b0
= gen_fhostop(eaddr
, Q_SRC
);
3817 b1
= gen_fhostop(eaddr
, Q_DST
);
3823 b0
= gen_fhostop(eaddr
, Q_SRC
);
3824 b1
= gen_fhostop(eaddr
, Q_DST
);
3829 bpf_error("'addr1' is only supported on 802.11");
3833 bpf_error("'addr2' is only supported on 802.11");
3837 bpf_error("'addr3' is only supported on 802.11");
3841 bpf_error("'addr4' is only supported on 802.11");
3845 bpf_error("'ra' is only supported on 802.11");
3849 bpf_error("'ta' is only supported on 802.11");
3857 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3859 static struct block
*
3860 gen_thostop(eaddr
, dir
)
3861 register const u_char
*eaddr
;
3864 register struct block
*b0
, *b1
;
3868 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3871 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3874 b0
= gen_thostop(eaddr
, Q_SRC
);
3875 b1
= gen_thostop(eaddr
, Q_DST
);
3881 b0
= gen_thostop(eaddr
, Q_SRC
);
3882 b1
= gen_thostop(eaddr
, Q_DST
);
3887 bpf_error("'addr1' is only supported on 802.11");
3891 bpf_error("'addr2' is only supported on 802.11");
3895 bpf_error("'addr3' is only supported on 802.11");
3899 bpf_error("'addr4' is only supported on 802.11");
3903 bpf_error("'ra' is only supported on 802.11");
3907 bpf_error("'ta' is only supported on 802.11");
3915 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3916 * various 802.11 + radio headers.
3918 static struct block
*
3919 gen_wlanhostop(eaddr
, dir
)
3920 register const u_char
*eaddr
;
3923 register struct block
*b0
, *b1
, *b2
;
3924 register struct slist
*s
;
3926 #ifdef ENABLE_WLAN_FILTERING_PATCH
3929 * We need to disable the optimizer because the optimizer is buggy
3930 * and wipes out some LD instructions generated by the below
3931 * code to validate the Frame Control bits
3934 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3941 * For control frames, there is no SA.
3943 * For management frames, SA is at an
3944 * offset of 10 from the beginning of
3947 * For data frames, SA is at an offset
3948 * of 10 from the beginning of the packet
3949 * if From DS is clear, at an offset of
3950 * 16 from the beginning of the packet
3951 * if From DS is set and To DS is clear,
3952 * and an offset of 24 from the beginning
3953 * of the packet if From DS is set and To DS
3958 * Generate the tests to be done for data frames
3961 * First, check for To DS set, i.e. check "link[1] & 0x01".
3963 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3964 b1
= new_block(JMP(BPF_JSET
));
3965 b1
->s
.k
= 0x01; /* To DS */
3969 * If To DS is set, the SA is at 24.
3971 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
3975 * Now, check for To DS not set, i.e. check
3976 * "!(link[1] & 0x01)".
3978 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3979 b2
= new_block(JMP(BPF_JSET
));
3980 b2
->s
.k
= 0x01; /* To DS */
3985 * If To DS is not set, the SA is at 16.
3987 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
3991 * Now OR together the last two checks. That gives
3992 * the complete set of checks for data frames with
3998 * Now check for From DS being set, and AND that with
3999 * the ORed-together checks.
4001 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4002 b1
= new_block(JMP(BPF_JSET
));
4003 b1
->s
.k
= 0x02; /* From DS */
4008 * Now check for data frames with From DS not set.
4010 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4011 b2
= new_block(JMP(BPF_JSET
));
4012 b2
->s
.k
= 0x02; /* From DS */
4017 * If From DS isn't set, the SA is at 10.
4019 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4023 * Now OR together the checks for data frames with
4024 * From DS not set and for data frames with From DS
4025 * set; that gives the checks done for data frames.
4030 * Now check for a data frame.
4031 * I.e, check "link[0] & 0x08".
4033 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4034 b1
= new_block(JMP(BPF_JSET
));
4039 * AND that with the checks done for data frames.
4044 * If the high-order bit of the type value is 0, this
4045 * is a management frame.
4046 * I.e, check "!(link[0] & 0x08)".
4048 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4049 b2
= new_block(JMP(BPF_JSET
));
4055 * For management frames, the SA is at 10.
4057 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4061 * OR that with the checks done for data frames.
4062 * That gives the checks done for management and
4068 * If the low-order bit of the type value is 1,
4069 * this is either a control frame or a frame
4070 * with a reserved type, and thus not a
4073 * I.e., check "!(link[0] & 0x04)".
4075 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4076 b1
= new_block(JMP(BPF_JSET
));
4082 * AND that with the checks for data and management
4092 * For control frames, there is no DA.
4094 * For management frames, DA is at an
4095 * offset of 4 from the beginning of
4098 * For data frames, DA is at an offset
4099 * of 4 from the beginning of the packet
4100 * if To DS is clear and at an offset of
4101 * 16 from the beginning of the packet
4106 * Generate the tests to be done for data frames.
4108 * First, check for To DS set, i.e. "link[1] & 0x01".
4110 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4111 b1
= new_block(JMP(BPF_JSET
));
4112 b1
->s
.k
= 0x01; /* To DS */
4116 * If To DS is set, the DA is at 16.
4118 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4122 * Now, check for To DS not set, i.e. check
4123 * "!(link[1] & 0x01)".
4125 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4126 b2
= new_block(JMP(BPF_JSET
));
4127 b2
->s
.k
= 0x01; /* To DS */
4132 * If To DS is not set, the DA is at 4.
4134 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4138 * Now OR together the last two checks. That gives
4139 * the complete set of checks for data frames.
4144 * Now check for a data frame.
4145 * I.e, check "link[0] & 0x08".
4147 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4148 b1
= new_block(JMP(BPF_JSET
));
4153 * AND that with the checks done for data frames.
4158 * If the high-order bit of the type value is 0, this
4159 * is a management frame.
4160 * I.e, check "!(link[0] & 0x08)".
4162 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4163 b2
= new_block(JMP(BPF_JSET
));
4169 * For management frames, the DA is at 4.
4171 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4175 * OR that with the checks done for data frames.
4176 * That gives the checks done for management and
4182 * If the low-order bit of the type value is 1,
4183 * this is either a control frame or a frame
4184 * with a reserved type, and thus not a
4187 * I.e., check "!(link[0] & 0x04)".
4189 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4190 b1
= new_block(JMP(BPF_JSET
));
4196 * AND that with the checks for data and management
4204 * Not present in management frames; addr1 in other
4209 * If the high-order bit of the type value is 0, this
4210 * is a management frame.
4211 * I.e, check "(link[0] & 0x08)".
4213 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4214 b1
= new_block(JMP(BPF_JSET
));
4221 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4224 * AND that with the check of addr1.
4231 * Not present in management frames; addr2, if present,
4236 * Not present in CTS or ACK control frames.
4238 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4239 IEEE80211_FC0_TYPE_MASK
);
4241 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4242 IEEE80211_FC0_SUBTYPE_MASK
);
4244 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4245 IEEE80211_FC0_SUBTYPE_MASK
);
4251 * If the high-order bit of the type value is 0, this
4252 * is a management frame.
4253 * I.e, check "(link[0] & 0x08)".
4255 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4256 b1
= new_block(JMP(BPF_JSET
));
4261 * AND that with the check for frames other than
4262 * CTS and ACK frames.
4269 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4274 * XXX - add BSSID keyword?
4277 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4281 * Not present in CTS or ACK control frames.
4283 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4284 IEEE80211_FC0_TYPE_MASK
);
4286 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4287 IEEE80211_FC0_SUBTYPE_MASK
);
4289 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4290 IEEE80211_FC0_SUBTYPE_MASK
);
4294 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4300 * Not present in control frames.
4302 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4303 IEEE80211_FC0_TYPE_MASK
);
4305 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4311 * Present only if the direction mask has both "From DS"
4312 * and "To DS" set. Neither control frames nor management
4313 * frames should have both of those set, so we don't
4314 * check the frame type.
4316 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4317 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4318 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4323 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4324 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4330 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4331 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4340 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4341 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4342 * as the RFC states.)
4344 static struct block
*
4345 gen_ipfchostop(eaddr
, dir
)
4346 register const u_char
*eaddr
;
4349 register struct block
*b0
, *b1
;
4353 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4356 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4359 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4360 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4366 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4367 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4372 bpf_error("'addr1' is only supported on 802.11");
4376 bpf_error("'addr2' is only supported on 802.11");
4380 bpf_error("'addr3' is only supported on 802.11");
4384 bpf_error("'addr4' is only supported on 802.11");
4388 bpf_error("'ra' is only supported on 802.11");
4392 bpf_error("'ta' is only supported on 802.11");
4400 * This is quite tricky because there may be pad bytes in front of the
4401 * DECNET header, and then there are two possible data packet formats that
4402 * carry both src and dst addresses, plus 5 packet types in a format that
4403 * carries only the src node, plus 2 types that use a different format and
4404 * also carry just the src node.
4408 * Instead of doing those all right, we just look for data packets with
4409 * 0 or 1 bytes of padding. If you want to look at other packets, that
4410 * will require a lot more hacking.
4412 * To add support for filtering on DECNET "areas" (network numbers)
4413 * one would want to add a "mask" argument to this routine. That would
4414 * make the filter even more inefficient, although one could be clever
4415 * and not generate masking instructions if the mask is 0xFFFF.
4417 static struct block
*
4418 gen_dnhostop(addr
, dir
)
4422 struct block
*b0
, *b1
, *b2
, *tmp
;
4423 u_int offset_lh
; /* offset if long header is received */
4424 u_int offset_sh
; /* offset if short header is received */
4429 offset_sh
= 1; /* follows flags */
4430 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4434 offset_sh
= 3; /* follows flags, dstnode */
4435 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4439 /* Inefficient because we do our Calvinball dance twice */
4440 b0
= gen_dnhostop(addr
, Q_SRC
);
4441 b1
= gen_dnhostop(addr
, Q_DST
);
4447 /* Inefficient because we do our Calvinball dance twice */
4448 b0
= gen_dnhostop(addr
, Q_SRC
);
4449 b1
= gen_dnhostop(addr
, Q_DST
);
4454 bpf_error("ISO host filtering not implemented");
4459 b0
= gen_linktype(ETHERTYPE_DN
);
4460 /* Check for pad = 1, long header case */
4461 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4462 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4463 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4464 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4466 /* Check for pad = 0, long header case */
4467 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4468 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4471 /* Check for pad = 1, short header case */
4472 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4473 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4474 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4477 /* Check for pad = 0, short header case */
4478 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4479 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4483 /* Combine with test for linktype */
4489 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4490 * test the bottom-of-stack bit, and then check the version number
4491 * field in the IP header.
4493 static struct block
*
4494 gen_mpls_linktype(proto
)
4497 struct block
*b0
, *b1
;
4502 /* match the bottom-of-stack bit */
4503 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4504 /* match the IPv4 version number */
4505 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4510 /* match the bottom-of-stack bit */
4511 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4512 /* match the IPv4 version number */
4513 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4522 static struct block
*
4523 gen_host(addr
, mask
, proto
, dir
, type
)
4530 struct block
*b0
, *b1
;
4531 const char *typestr
;
4541 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4543 * Only check for non-IPv4 addresses if we're not
4544 * checking MPLS-encapsulated packets.
4546 if (label_stack_depth
== 0) {
4547 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4549 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4555 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4558 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4561 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4564 bpf_error("'tcp' modifier applied to %s", typestr
);
4567 bpf_error("'sctp' modifier applied to %s", typestr
);
4570 bpf_error("'udp' modifier applied to %s", typestr
);
4573 bpf_error("'icmp' modifier applied to %s", typestr
);
4576 bpf_error("'igmp' modifier applied to %s", typestr
);
4579 bpf_error("'igrp' modifier applied to %s", typestr
);
4582 bpf_error("'pim' modifier applied to %s", typestr
);
4585 bpf_error("'vrrp' modifier applied to %s", typestr
);
4588 bpf_error("'carp' modifier applied to %s", typestr
);
4591 bpf_error("ATALK host filtering not implemented");
4594 bpf_error("AARP host filtering not implemented");
4597 return gen_dnhostop(addr
, dir
);
4600 bpf_error("SCA host filtering not implemented");
4603 bpf_error("LAT host filtering not implemented");
4606 bpf_error("MOPDL host filtering not implemented");
4609 bpf_error("MOPRC host filtering not implemented");
4612 bpf_error("'ip6' modifier applied to ip host");
4615 bpf_error("'icmp6' modifier applied to %s", typestr
);
4618 bpf_error("'ah' modifier applied to %s", typestr
);
4621 bpf_error("'esp' modifier applied to %s", typestr
);
4624 bpf_error("ISO host filtering not implemented");
4627 bpf_error("'esis' modifier applied to %s", typestr
);
4630 bpf_error("'isis' modifier applied to %s", typestr
);
4633 bpf_error("'clnp' modifier applied to %s", typestr
);
4636 bpf_error("'stp' modifier applied to %s", typestr
);
4639 bpf_error("IPX host filtering not implemented");
4642 bpf_error("'netbeui' modifier applied to %s", typestr
);
4645 bpf_error("'radio' modifier applied to %s", typestr
);
4654 static struct block
*
4655 gen_host6(addr
, mask
, proto
, dir
, type
)
4656 struct in6_addr
*addr
;
4657 struct in6_addr
*mask
;
4662 const char *typestr
;
4672 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4675 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4678 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4681 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4684 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4687 bpf_error("'sctp' modifier applied to %s", typestr
);
4690 bpf_error("'tcp' modifier applied to %s", typestr
);
4693 bpf_error("'udp' modifier applied to %s", typestr
);
4696 bpf_error("'icmp' modifier applied to %s", typestr
);
4699 bpf_error("'igmp' modifier applied to %s", typestr
);
4702 bpf_error("'igrp' modifier applied to %s", typestr
);
4705 bpf_error("'pim' modifier applied to %s", typestr
);
4708 bpf_error("'vrrp' modifier applied to %s", typestr
);
4711 bpf_error("'carp' modifier applied to %s", typestr
);
4714 bpf_error("ATALK host filtering not implemented");
4717 bpf_error("AARP host filtering not implemented");
4720 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4723 bpf_error("SCA host filtering not implemented");
4726 bpf_error("LAT host filtering not implemented");
4729 bpf_error("MOPDL host filtering not implemented");
4732 bpf_error("MOPRC host filtering not implemented");
4735 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4738 bpf_error("'icmp6' modifier applied to %s", typestr
);
4741 bpf_error("'ah' modifier applied to %s", typestr
);
4744 bpf_error("'esp' modifier applied to %s", typestr
);
4747 bpf_error("ISO host filtering not implemented");
4750 bpf_error("'esis' modifier applied to %s", typestr
);
4753 bpf_error("'isis' modifier applied to %s", typestr
);
4756 bpf_error("'clnp' modifier applied to %s", typestr
);
4759 bpf_error("'stp' modifier applied to %s", typestr
);
4762 bpf_error("IPX host filtering not implemented");
4765 bpf_error("'netbeui' modifier applied to %s", typestr
);
4768 bpf_error("'radio' modifier applied to %s", typestr
);
4778 static struct block
*
4779 gen_gateway(eaddr
, alist
, proto
, dir
)
4780 const u_char
*eaddr
;
4781 bpf_u_int32
**alist
;
4785 struct block
*b0
, *b1
, *tmp
;
4788 bpf_error("direction applied to 'gateway'");
4797 case DLT_NETANALYZER
:
4798 case DLT_NETANALYZER_TRANSPARENT
:
4799 b1
= gen_prevlinkhdr_check();
4800 b0
= gen_ehostop(eaddr
, Q_OR
);
4805 b0
= gen_fhostop(eaddr
, Q_OR
);
4808 b0
= gen_thostop(eaddr
, Q_OR
);
4810 case DLT_IEEE802_11
:
4811 case DLT_PRISM_HEADER
:
4812 case DLT_IEEE802_11_RADIO_AVS
:
4813 case DLT_IEEE802_11_RADIO
:
4815 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4819 * This is LLC-multiplexed traffic; if it were
4820 * LANE, linktype would have been set to
4824 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4826 case DLT_IP_OVER_FC
:
4827 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4831 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4833 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4835 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4844 bpf_error("illegal modifier of 'gateway'");
4850 gen_proto_abbrev(proto
)
4859 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4860 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4865 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4866 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4871 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4872 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4877 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4880 #ifndef IPPROTO_IGMP
4881 #define IPPROTO_IGMP 2
4885 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4888 #ifndef IPPROTO_IGRP
4889 #define IPPROTO_IGRP 9
4892 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4896 #define IPPROTO_PIM 103
4900 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4901 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4905 #ifndef IPPROTO_VRRP
4906 #define IPPROTO_VRRP 112
4910 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4913 #ifndef IPPROTO_CARP
4914 #define IPPROTO_CARP 112
4918 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4922 b1
= gen_linktype(ETHERTYPE_IP
);
4926 b1
= gen_linktype(ETHERTYPE_ARP
);
4930 b1
= gen_linktype(ETHERTYPE_REVARP
);
4934 bpf_error("link layer applied in wrong context");
4937 b1
= gen_linktype(ETHERTYPE_ATALK
);
4941 b1
= gen_linktype(ETHERTYPE_AARP
);
4945 b1
= gen_linktype(ETHERTYPE_DN
);
4949 b1
= gen_linktype(ETHERTYPE_SCA
);
4953 b1
= gen_linktype(ETHERTYPE_LAT
);
4957 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4961 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4965 b1
= gen_linktype(ETHERTYPE_IPV6
);
4968 #ifndef IPPROTO_ICMPV6
4969 #define IPPROTO_ICMPV6 58
4972 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4976 #define IPPROTO_AH 51
4979 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4980 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4985 #define IPPROTO_ESP 50
4988 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4989 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4994 b1
= gen_linktype(LLCSAP_ISONS
);
4998 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5002 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5005 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5006 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5007 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5009 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5011 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5013 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5017 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5018 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5019 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5021 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5023 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5025 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5029 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5030 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5031 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5033 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5038 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5039 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5044 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5045 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5047 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5049 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5054 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5055 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5060 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5061 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5066 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5070 b1
= gen_linktype(LLCSAP_8021D
);
5074 b1
= gen_linktype(LLCSAP_IPX
);
5078 b1
= gen_linktype(LLCSAP_NETBEUI
);
5082 bpf_error("'radio' is not a valid protocol type");
5090 static struct block
*
5096 /* not IPv4 frag other than the first frag */
5097 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5098 b
= new_block(JMP(BPF_JSET
));
5107 * Generate a comparison to a port value in the transport-layer header
5108 * at the specified offset from the beginning of that header.
5110 * XXX - this handles a variable-length prefix preceding the link-layer
5111 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5112 * variable-length link-layer headers (such as Token Ring or 802.11
5115 static struct block
*
5116 gen_portatom(off
, v
)
5120 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5123 static struct block
*
5124 gen_portatom6(off
, v
)
5128 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5132 gen_portop(port
, proto
, dir
)
5133 int port
, proto
, dir
;
5135 struct block
*b0
, *b1
, *tmp
;
5137 /* ip proto 'proto' and not a fragment other than the first fragment */
5138 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5144 b1
= gen_portatom(0, (bpf_int32
)port
);
5148 b1
= gen_portatom(2, (bpf_int32
)port
);
5153 tmp
= gen_portatom(0, (bpf_int32
)port
);
5154 b1
= gen_portatom(2, (bpf_int32
)port
);
5159 tmp
= gen_portatom(0, (bpf_int32
)port
);
5160 b1
= gen_portatom(2, (bpf_int32
)port
);
5172 static struct block
*
5173 gen_port(port
, ip_proto
, dir
)
5178 struct block
*b0
, *b1
, *tmp
;
5183 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5184 * not LLC encapsulation with LLCSAP_IP.
5186 * For IEEE 802 networks - which includes 802.5 token ring
5187 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5188 * says that SNAP encapsulation is used, not LLC encapsulation
5191 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5192 * RFC 2225 say that SNAP encapsulation is used, not LLC
5193 * encapsulation with LLCSAP_IP.
5195 * So we always check for ETHERTYPE_IP.
5197 b0
= gen_linktype(ETHERTYPE_IP
);
5203 b1
= gen_portop(port
, ip_proto
, dir
);
5207 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5208 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5210 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5222 gen_portop6(port
, proto
, dir
)
5223 int port
, proto
, dir
;
5225 struct block
*b0
, *b1
, *tmp
;
5227 /* ip6 proto 'proto' */
5228 /* XXX - catch the first fragment of a fragmented packet? */
5229 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5233 b1
= gen_portatom6(0, (bpf_int32
)port
);
5237 b1
= gen_portatom6(2, (bpf_int32
)port
);
5242 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5243 b1
= gen_portatom6(2, (bpf_int32
)port
);
5248 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5249 b1
= gen_portatom6(2, (bpf_int32
)port
);
5261 static struct block
*
5262 gen_port6(port
, ip_proto
, dir
)
5267 struct block
*b0
, *b1
, *tmp
;
5269 /* link proto ip6 */
5270 b0
= gen_linktype(ETHERTYPE_IPV6
);
5276 b1
= gen_portop6(port
, ip_proto
, dir
);
5280 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5281 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5283 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5294 /* gen_portrange code */
5295 static struct block
*
5296 gen_portrangeatom(off
, v1
, v2
)
5300 struct block
*b1
, *b2
;
5304 * Reverse the order of the ports, so v1 is the lower one.
5313 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5314 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5322 gen_portrangeop(port1
, port2
, proto
, dir
)
5327 struct block
*b0
, *b1
, *tmp
;
5329 /* ip proto 'proto' and not a fragment other than the first fragment */
5330 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5336 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5340 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5345 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5346 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5351 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5352 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5364 static struct block
*
5365 gen_portrange(port1
, port2
, ip_proto
, dir
)
5370 struct block
*b0
, *b1
, *tmp
;
5373 b0
= gen_linktype(ETHERTYPE_IP
);
5379 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5383 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5384 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5386 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5397 static struct block
*
5398 gen_portrangeatom6(off
, v1
, v2
)
5402 struct block
*b1
, *b2
;
5406 * Reverse the order of the ports, so v1 is the lower one.
5415 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5416 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5424 gen_portrangeop6(port1
, port2
, proto
, dir
)
5429 struct block
*b0
, *b1
, *tmp
;
5431 /* ip6 proto 'proto' */
5432 /* XXX - catch the first fragment of a fragmented packet? */
5433 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5437 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5441 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5446 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5447 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5452 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5453 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5465 static struct block
*
5466 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5471 struct block
*b0
, *b1
, *tmp
;
5473 /* link proto ip6 */
5474 b0
= gen_linktype(ETHERTYPE_IPV6
);
5480 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5484 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5485 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5487 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5499 lookup_proto(name
, proto
)
5500 register const char *name
;
5510 v
= pcap_nametoproto(name
);
5511 if (v
== PROTO_UNDEF
)
5512 bpf_error("unknown ip proto '%s'", name
);
5516 /* XXX should look up h/w protocol type based on linktype */
5517 v
= pcap_nametoeproto(name
);
5518 if (v
== PROTO_UNDEF
) {
5519 v
= pcap_nametollc(name
);
5520 if (v
== PROTO_UNDEF
)
5521 bpf_error("unknown ether proto '%s'", name
);
5526 if (strcmp(name
, "esis") == 0)
5528 else if (strcmp(name
, "isis") == 0)
5530 else if (strcmp(name
, "clnp") == 0)
5533 bpf_error("unknown osi proto '%s'", name
);
5553 static struct block
*
5554 gen_protochain(v
, proto
, dir
)
5559 #ifdef NO_PROTOCHAIN
5560 return gen_proto(v
, proto
, dir
);
5562 struct block
*b0
, *b
;
5563 struct slist
*s
[100];
5564 int fix2
, fix3
, fix4
, fix5
;
5565 int ahcheck
, again
, end
;
5567 int reg2
= alloc_reg();
5569 memset(s
, 0, sizeof(s
));
5570 fix2
= fix3
= fix4
= fix5
= 0;
5577 b0
= gen_protochain(v
, Q_IP
, dir
);
5578 b
= gen_protochain(v
, Q_IPV6
, dir
);
5582 bpf_error("bad protocol applied for 'protochain'");
5587 * We don't handle variable-length prefixes before the link-layer
5588 * header, or variable-length link-layer headers, here yet.
5589 * We might want to add BPF instructions to do the protochain
5590 * work, to simplify that and, on platforms that have a BPF
5591 * interpreter with the new instructions, let the filtering
5592 * be done in the kernel. (We already require a modified BPF
5593 * engine to do the protochain stuff, to support backward
5594 * branches, and backward branch support is unlikely to appear
5595 * in kernel BPF engines.)
5597 if (off_linkpl
.is_variable
)
5598 bpf_error("'protochain' not supported with variable length headers");
5600 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5603 * s[0] is a dummy entry to protect other BPF insn from damage
5604 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5605 * hard to find interdependency made by jump table fixup.
5608 s
[i
] = new_stmt(0); /*dummy*/
5613 b0
= gen_linktype(ETHERTYPE_IP
);
5616 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5617 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5619 /* X = ip->ip_hl << 2 */
5620 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5621 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5626 b0
= gen_linktype(ETHERTYPE_IPV6
);
5628 /* A = ip6->ip_nxt */
5629 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5630 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5632 /* X = sizeof(struct ip6_hdr) */
5633 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5639 bpf_error("unsupported proto to gen_protochain");
5643 /* again: if (A == v) goto end; else fall through; */
5645 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5647 s
[i
]->s
.jt
= NULL
; /*later*/
5648 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5652 #ifndef IPPROTO_NONE
5653 #define IPPROTO_NONE 59
5655 /* if (A == IPPROTO_NONE) goto end */
5656 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5657 s
[i
]->s
.jt
= NULL
; /*later*/
5658 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5659 s
[i
]->s
.k
= IPPROTO_NONE
;
5660 s
[fix5
]->s
.jf
= s
[i
];
5664 if (proto
== Q_IPV6
) {
5665 int v6start
, v6end
, v6advance
, j
;
5668 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5669 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5670 s
[i
]->s
.jt
= NULL
; /*later*/
5671 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5672 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5673 s
[fix2
]->s
.jf
= s
[i
];
5675 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5676 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5677 s
[i
]->s
.jt
= NULL
; /*later*/
5678 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5679 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5681 /* if (A == IPPROTO_ROUTING) goto v6advance */
5682 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5683 s
[i
]->s
.jt
= NULL
; /*later*/
5684 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5685 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5687 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5688 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5689 s
[i
]->s
.jt
= NULL
; /*later*/
5690 s
[i
]->s
.jf
= NULL
; /*later*/
5691 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5701 * A = P[X + packet head];
5702 * X = X + (P[X + packet head + 1] + 1) * 8;
5704 /* A = P[X + packet head] */
5705 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5706 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5709 s
[i
] = new_stmt(BPF_ST
);
5712 /* A = P[X + packet head + 1]; */
5713 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5714 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5717 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5721 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5725 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5729 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5732 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5736 /* goto again; (must use BPF_JA for backward jump) */
5737 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5738 s
[i
]->s
.k
= again
- i
- 1;
5739 s
[i
- 1]->s
.jf
= s
[i
];
5743 for (j
= v6start
; j
<= v6end
; j
++)
5744 s
[j
]->s
.jt
= s
[v6advance
];
5747 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5749 s
[fix2
]->s
.jf
= s
[i
];
5755 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5756 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5757 s
[i
]->s
.jt
= NULL
; /*later*/
5758 s
[i
]->s
.jf
= NULL
; /*later*/
5759 s
[i
]->s
.k
= IPPROTO_AH
;
5761 s
[fix3
]->s
.jf
= s
[ahcheck
];
5768 * X = X + (P[X + 1] + 2) * 4;
5771 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5773 /* A = P[X + packet head]; */
5774 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5775 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5778 s
[i
] = new_stmt(BPF_ST
);
5782 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5785 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5789 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5791 /* A = P[X + packet head] */
5792 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5793 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5796 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5800 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5804 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5807 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5811 /* goto again; (must use BPF_JA for backward jump) */
5812 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5813 s
[i
]->s
.k
= again
- i
- 1;
5818 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5820 s
[fix2
]->s
.jt
= s
[end
];
5821 s
[fix4
]->s
.jf
= s
[end
];
5822 s
[fix5
]->s
.jt
= s
[end
];
5829 for (i
= 0; i
< max
- 1; i
++)
5830 s
[i
]->next
= s
[i
+ 1];
5831 s
[max
- 1]->next
= NULL
;
5836 b
= new_block(JMP(BPF_JEQ
));
5837 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5847 static struct block
*
5848 gen_check_802_11_data_frame()
5851 struct block
*b0
, *b1
;
5854 * A data frame has the 0x08 bit (b3) in the frame control field set
5855 * and the 0x04 bit (b2) clear.
5857 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5858 b0
= new_block(JMP(BPF_JSET
));
5862 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5863 b1
= new_block(JMP(BPF_JSET
));
5874 * Generate code that checks whether the packet is a packet for protocol
5875 * <proto> and whether the type field in that protocol's header has
5876 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5877 * IP packet and checks the protocol number in the IP header against <v>.
5879 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5880 * against Q_IP and Q_IPV6.
5882 static struct block
*
5883 gen_proto(v
, proto
, dir
)
5888 struct block
*b0
, *b1
;
5893 if (dir
!= Q_DEFAULT
)
5894 bpf_error("direction applied to 'proto'");
5898 b0
= gen_proto(v
, Q_IP
, dir
);
5899 b1
= gen_proto(v
, Q_IPV6
, dir
);
5905 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5906 * not LLC encapsulation with LLCSAP_IP.
5908 * For IEEE 802 networks - which includes 802.5 token ring
5909 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5910 * says that SNAP encapsulation is used, not LLC encapsulation
5913 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5914 * RFC 2225 say that SNAP encapsulation is used, not LLC
5915 * encapsulation with LLCSAP_IP.
5917 * So we always check for ETHERTYPE_IP.
5919 b0
= gen_linktype(ETHERTYPE_IP
);
5921 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5923 b1
= gen_protochain(v
, Q_IP
);
5933 * Frame Relay packets typically have an OSI
5934 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5935 * generates code to check for all the OSI
5936 * NLPIDs, so calling it and then adding a check
5937 * for the particular NLPID for which we're
5938 * looking is bogus, as we can just check for
5941 * What we check for is the NLPID and a frame
5942 * control field value of UI, i.e. 0x03 followed
5945 * XXX - assumes a 2-byte Frame Relay header with
5946 * DLCI and flags. What if the address is longer?
5948 * XXX - what about SNAP-encapsulated frames?
5950 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5956 * Cisco uses an Ethertype lookalike - for OSI,
5959 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5960 /* OSI in C-HDLC is stuffed with a fudge byte */
5961 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
5966 b0
= gen_linktype(LLCSAP_ISONS
);
5967 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
5973 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5975 * 4 is the offset of the PDU type relative to the IS-IS
5978 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
5983 bpf_error("arp does not encapsulate another protocol");
5987 bpf_error("rarp does not encapsulate another protocol");
5991 bpf_error("atalk encapsulation is not specifiable");
5995 bpf_error("decnet encapsulation is not specifiable");
5999 bpf_error("sca does not encapsulate another protocol");
6003 bpf_error("lat does not encapsulate another protocol");
6007 bpf_error("moprc does not encapsulate another protocol");
6011 bpf_error("mopdl does not encapsulate another protocol");
6015 return gen_linktype(v
);
6018 bpf_error("'udp proto' is bogus");
6022 bpf_error("'tcp proto' is bogus");
6026 bpf_error("'sctp proto' is bogus");
6030 bpf_error("'icmp proto' is bogus");
6034 bpf_error("'igmp proto' is bogus");
6038 bpf_error("'igrp proto' is bogus");
6042 bpf_error("'pim proto' is bogus");
6046 bpf_error("'vrrp proto' is bogus");
6050 bpf_error("'carp proto' is bogus");
6054 b0
= gen_linktype(ETHERTYPE_IPV6
);
6057 * Also check for a fragment header before the final
6060 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6061 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6063 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6066 b1
= gen_protochain(v
, Q_IPV6
);
6072 bpf_error("'icmp6 proto' is bogus");
6075 bpf_error("'ah proto' is bogus");
6078 bpf_error("'ah proto' is bogus");
6081 bpf_error("'stp proto' is bogus");
6084 bpf_error("'ipx proto' is bogus");
6087 bpf_error("'netbeui proto' is bogus");
6090 bpf_error("'radio proto' is bogus");
6101 register const char *name
;
6104 int proto
= q
.proto
;
6108 bpf_u_int32 mask
, addr
;
6110 bpf_u_int32
**alist
;
6113 struct sockaddr_in
*sin4
;
6114 struct sockaddr_in6
*sin6
;
6115 struct addrinfo
*res
, *res0
;
6116 struct in6_addr mask128
;
6118 struct block
*b
, *tmp
;
6119 int port
, real_proto
;
6125 addr
= pcap_nametonetaddr(name
);
6127 bpf_error("unknown network '%s'", name
);
6128 /* Left justify network addr and calculate its network mask */
6130 while (addr
&& (addr
& 0xff000000) == 0) {
6134 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6138 if (proto
== Q_LINK
) {
6142 case DLT_NETANALYZER
:
6143 case DLT_NETANALYZER_TRANSPARENT
:
6144 eaddr
= pcap_ether_hostton(name
);
6147 "unknown ether host '%s'", name
);
6148 tmp
= gen_prevlinkhdr_check();
6149 b
= gen_ehostop(eaddr
, dir
);
6156 eaddr
= pcap_ether_hostton(name
);
6159 "unknown FDDI host '%s'", name
);
6160 b
= gen_fhostop(eaddr
, dir
);
6165 eaddr
= pcap_ether_hostton(name
);
6168 "unknown token ring host '%s'", name
);
6169 b
= gen_thostop(eaddr
, dir
);
6173 case DLT_IEEE802_11
:
6174 case DLT_PRISM_HEADER
:
6175 case DLT_IEEE802_11_RADIO_AVS
:
6176 case DLT_IEEE802_11_RADIO
:
6178 eaddr
= pcap_ether_hostton(name
);
6181 "unknown 802.11 host '%s'", name
);
6182 b
= gen_wlanhostop(eaddr
, dir
);
6186 case DLT_IP_OVER_FC
:
6187 eaddr
= pcap_ether_hostton(name
);
6190 "unknown Fibre Channel host '%s'", name
);
6191 b
= gen_ipfchostop(eaddr
, dir
);
6196 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6197 } else if (proto
== Q_DECNET
) {
6198 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6200 * I don't think DECNET hosts can be multihomed, so
6201 * there is no need to build up a list of addresses
6203 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6206 alist
= pcap_nametoaddr(name
);
6207 if (alist
== NULL
|| *alist
== NULL
)
6208 bpf_error("unknown host '%s'", name
);
6210 if (off_linktype
.constant_part
== (u_int
)-1 &&
6211 tproto
== Q_DEFAULT
)
6213 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6215 tmp
= gen_host(**alist
++, 0xffffffff,
6216 tproto
, dir
, q
.addr
);
6222 memset(&mask128
, 0xff, sizeof(mask128
));
6223 res0
= res
= pcap_nametoaddrinfo(name
);
6225 bpf_error("unknown host '%s'", name
);
6228 tproto
= tproto6
= proto
;
6229 if (off_linktype
.constant_part
== (u_int
)-1 &&
6230 tproto
== Q_DEFAULT
) {
6234 for (res
= res0
; res
; res
= res
->ai_next
) {
6235 switch (res
->ai_family
) {
6237 if (tproto
== Q_IPV6
)
6240 sin4
= (struct sockaddr_in
*)
6242 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6243 0xffffffff, tproto
, dir
, q
.addr
);
6246 if (tproto6
== Q_IP
)
6249 sin6
= (struct sockaddr_in6
*)
6251 tmp
= gen_host6(&sin6
->sin6_addr
,
6252 &mask128
, tproto6
, dir
, q
.addr
);
6264 bpf_error("unknown host '%s'%s", name
,
6265 (proto
== Q_DEFAULT
)
6267 : " for specified address family");
6274 if (proto
!= Q_DEFAULT
&&
6275 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6276 bpf_error("illegal qualifier of 'port'");
6277 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6278 bpf_error("unknown port '%s'", name
);
6279 if (proto
== Q_UDP
) {
6280 if (real_proto
== IPPROTO_TCP
)
6281 bpf_error("port '%s' is tcp", name
);
6282 else if (real_proto
== IPPROTO_SCTP
)
6283 bpf_error("port '%s' is sctp", name
);
6285 /* override PROTO_UNDEF */
6286 real_proto
= IPPROTO_UDP
;
6288 if (proto
== Q_TCP
) {
6289 if (real_proto
== IPPROTO_UDP
)
6290 bpf_error("port '%s' is udp", name
);
6292 else if (real_proto
== IPPROTO_SCTP
)
6293 bpf_error("port '%s' is sctp", name
);
6295 /* override PROTO_UNDEF */
6296 real_proto
= IPPROTO_TCP
;
6298 if (proto
== Q_SCTP
) {
6299 if (real_proto
== IPPROTO_UDP
)
6300 bpf_error("port '%s' is udp", name
);
6302 else if (real_proto
== IPPROTO_TCP
)
6303 bpf_error("port '%s' is tcp", name
);
6305 /* override PROTO_UNDEF */
6306 real_proto
= IPPROTO_SCTP
;
6309 bpf_error("illegal port number %d < 0", port
);
6311 bpf_error("illegal port number %d > 65535", port
);
6312 b
= gen_port(port
, real_proto
, dir
);
6313 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6317 if (proto
!= Q_DEFAULT
&&
6318 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6319 bpf_error("illegal qualifier of 'portrange'");
6320 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6321 bpf_error("unknown port in range '%s'", name
);
6322 if (proto
== Q_UDP
) {
6323 if (real_proto
== IPPROTO_TCP
)
6324 bpf_error("port in range '%s' is tcp", name
);
6325 else if (real_proto
== IPPROTO_SCTP
)
6326 bpf_error("port in range '%s' is sctp", name
);
6328 /* override PROTO_UNDEF */
6329 real_proto
= IPPROTO_UDP
;
6331 if (proto
== Q_TCP
) {
6332 if (real_proto
== IPPROTO_UDP
)
6333 bpf_error("port in range '%s' is udp", name
);
6334 else if (real_proto
== IPPROTO_SCTP
)
6335 bpf_error("port in range '%s' is sctp", name
);
6337 /* override PROTO_UNDEF */
6338 real_proto
= IPPROTO_TCP
;
6340 if (proto
== Q_SCTP
) {
6341 if (real_proto
== IPPROTO_UDP
)
6342 bpf_error("port in range '%s' is udp", name
);
6343 else if (real_proto
== IPPROTO_TCP
)
6344 bpf_error("port in range '%s' is tcp", name
);
6346 /* override PROTO_UNDEF */
6347 real_proto
= IPPROTO_SCTP
;
6350 bpf_error("illegal port number %d < 0", port1
);
6352 bpf_error("illegal port number %d > 65535", port1
);
6354 bpf_error("illegal port number %d < 0", port2
);
6356 bpf_error("illegal port number %d > 65535", port2
);
6358 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6359 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6364 eaddr
= pcap_ether_hostton(name
);
6366 bpf_error("unknown ether host: %s", name
);
6368 alist
= pcap_nametoaddr(name
);
6369 if (alist
== NULL
|| *alist
== NULL
)
6370 bpf_error("unknown host '%s'", name
);
6371 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6375 bpf_error("'gateway' not supported in this configuration");
6379 real_proto
= lookup_proto(name
, proto
);
6380 if (real_proto
>= 0)
6381 return gen_proto(real_proto
, proto
, dir
);
6383 bpf_error("unknown protocol: %s", name
);
6386 real_proto
= lookup_proto(name
, proto
);
6387 if (real_proto
>= 0)
6388 return gen_protochain(real_proto
, proto
, dir
);
6390 bpf_error("unknown protocol: %s", name
);
6401 gen_mcode(s1
, s2
, masklen
, q
)
6402 register const char *s1
, *s2
;
6403 register unsigned int masklen
;
6406 register int nlen
, mlen
;
6409 nlen
= __pcap_atoin(s1
, &n
);
6410 /* Promote short ipaddr */
6414 mlen
= __pcap_atoin(s2
, &m
);
6415 /* Promote short ipaddr */
6418 bpf_error("non-network bits set in \"%s mask %s\"",
6421 /* Convert mask len to mask */
6423 bpf_error("mask length must be <= 32");
6426 * X << 32 is not guaranteed by C to be 0; it's
6431 m
= 0xffffffff << (32 - masklen
);
6433 bpf_error("non-network bits set in \"%s/%d\"",
6440 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6443 bpf_error("Mask syntax for networks only");
6452 register const char *s
;
6457 int proto
= q
.proto
;
6463 else if (q
.proto
== Q_DECNET
)
6464 vlen
= __pcap_atodn(s
, &v
);
6466 vlen
= __pcap_atoin(s
, &v
);
6473 if (proto
== Q_DECNET
)
6474 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6475 else if (proto
== Q_LINK
) {
6476 bpf_error("illegal link layer address");
6479 if (s
== NULL
&& q
.addr
== Q_NET
) {
6480 /* Promote short net number */
6481 while (v
&& (v
& 0xff000000) == 0) {
6486 /* Promote short ipaddr */
6490 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6495 proto
= IPPROTO_UDP
;
6496 else if (proto
== Q_TCP
)
6497 proto
= IPPROTO_TCP
;
6498 else if (proto
== Q_SCTP
)
6499 proto
= IPPROTO_SCTP
;
6500 else if (proto
== Q_DEFAULT
)
6501 proto
= PROTO_UNDEF
;
6503 bpf_error("illegal qualifier of 'port'");
6506 bpf_error("illegal port number %u > 65535", v
);
6510 b
= gen_port((int)v
, proto
, dir
);
6511 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6517 proto
= IPPROTO_UDP
;
6518 else if (proto
== Q_TCP
)
6519 proto
= IPPROTO_TCP
;
6520 else if (proto
== Q_SCTP
)
6521 proto
= IPPROTO_SCTP
;
6522 else if (proto
== Q_DEFAULT
)
6523 proto
= PROTO_UNDEF
;
6525 bpf_error("illegal qualifier of 'portrange'");
6528 bpf_error("illegal port number %u > 65535", v
);
6532 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6533 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6538 bpf_error("'gateway' requires a name");
6542 return gen_proto((int)v
, proto
, dir
);
6545 return gen_protochain((int)v
, proto
, dir
);
6560 gen_mcode6(s1
, s2
, masklen
, q
)
6561 register const char *s1
, *s2
;
6562 register unsigned int masklen
;
6565 struct addrinfo
*res
;
6566 struct in6_addr
*addr
;
6567 struct in6_addr mask
;
6572 bpf_error("no mask %s supported", s2
);
6574 res
= pcap_nametoaddrinfo(s1
);
6576 bpf_error("invalid ip6 address %s", s1
);
6579 bpf_error("%s resolved to multiple address", s1
);
6580 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6582 if ((int)sizeof(mask
) * 8 < masklen
)
6583 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6584 memset(&mask
, 0, sizeof(mask
));
6585 memset(&mask
, 0xff, masklen
/ 8);
6587 mask
.s6_addr
[masklen
/ 8] =
6588 (0xff << (8 - masklen
% 8)) & 0xff;
6591 a
= (u_int32_t
*)addr
;
6592 m
= (u_int32_t
*)&mask
;
6593 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6594 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6595 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6603 bpf_error("Mask syntax for networks only");
6607 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6613 bpf_error("invalid qualifier against IPv6 address");
6622 register const u_char
*eaddr
;
6625 struct block
*b
, *tmp
;
6627 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6630 case DLT_NETANALYZER
:
6631 case DLT_NETANALYZER_TRANSPARENT
:
6632 tmp
= gen_prevlinkhdr_check();
6633 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6638 return gen_fhostop(eaddr
, (int)q
.dir
);
6640 return gen_thostop(eaddr
, (int)q
.dir
);
6641 case DLT_IEEE802_11
:
6642 case DLT_PRISM_HEADER
:
6643 case DLT_IEEE802_11_RADIO_AVS
:
6644 case DLT_IEEE802_11_RADIO
:
6646 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6647 case DLT_IP_OVER_FC
:
6648 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6650 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6654 bpf_error("ethernet address used in non-ether expression");
6661 struct slist
*s0
, *s1
;
6664 * This is definitely not the best way to do this, but the
6665 * lists will rarely get long.
6672 static struct slist
*
6678 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6683 static struct slist
*
6689 s
= new_stmt(BPF_LD
|BPF_MEM
);
6695 * Modify "index" to use the value stored into its register as an
6696 * offset relative to the beginning of the header for the protocol
6697 * "proto", and allocate a register and put an item "size" bytes long
6698 * (1, 2, or 4) at that offset into that register, making it the register
6702 gen_load(proto
, inst
, size
)
6707 struct slist
*s
, *tmp
;
6709 int regno
= alloc_reg();
6711 free_reg(inst
->regno
);
6715 bpf_error("data size must be 1, 2, or 4");
6731 bpf_error("unsupported index operation");
6735 * The offset is relative to the beginning of the packet
6736 * data, if we have a radio header. (If we don't, this
6739 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6740 linktype
!= DLT_IEEE802_11_RADIO
&&
6741 linktype
!= DLT_PRISM_HEADER
)
6742 bpf_error("radio information not present in capture");
6745 * Load into the X register the offset computed into the
6746 * register specified by "index".
6748 s
= xfer_to_x(inst
);
6751 * Load the item at that offset.
6753 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6755 sappend(inst
->s
, s
);
6760 * The offset is relative to the beginning of
6761 * the link-layer header.
6763 * XXX - what about ATM LANE? Should the index be
6764 * relative to the beginning of the AAL5 frame, so
6765 * that 0 refers to the beginning of the LE Control
6766 * field, or relative to the beginning of the LAN
6767 * frame, so that 0 refers, for Ethernet LANE, to
6768 * the beginning of the destination address?
6770 s
= gen_abs_offset_varpart(&off_linkhdr
);
6773 * If "s" is non-null, it has code to arrange that the
6774 * X register contains the length of the prefix preceding
6775 * the link-layer header. Add to it the offset computed
6776 * into the register specified by "index", and move that
6777 * into the X register. Otherwise, just load into the X
6778 * register the offset computed into the register specified
6782 sappend(s
, xfer_to_a(inst
));
6783 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6784 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6786 s
= xfer_to_x(inst
);
6789 * Load the item at the sum of the offset we've put in the
6790 * X register and the offset of the start of the link
6791 * layer header (which is 0 if the radio header is
6792 * variable-length; that header length is what we put
6793 * into the X register and then added to the index).
6795 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6796 tmp
->s
.k
= off_linkhdr
.constant_part
;
6798 sappend(inst
->s
, s
);
6812 * The offset is relative to the beginning of
6813 * the network-layer header.
6814 * XXX - are there any cases where we want
6817 s
= gen_abs_offset_varpart(&off_linkpl
);
6820 * If "s" is non-null, it has code to arrange that the
6821 * X register contains the variable part of the offset
6822 * of the link-layer payload. Add to it the offset
6823 * computed into the register specified by "index",
6824 * and move that into the X register. Otherwise, just
6825 * load into the X register the offset computed into
6826 * the register specified by "index".
6829 sappend(s
, xfer_to_a(inst
));
6830 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6831 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6833 s
= xfer_to_x(inst
);
6836 * Load the item at the sum of the offset we've put in the
6837 * X register, the offset of the start of the network
6838 * layer header from the beginning of the link-layer
6839 * payload, and the constant part of the offset of the
6840 * start of the link-layer payload.
6842 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6843 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6845 sappend(inst
->s
, s
);
6848 * Do the computation only if the packet contains
6849 * the protocol in question.
6851 b
= gen_proto_abbrev(proto
);
6853 gen_and(inst
->b
, b
);
6867 * The offset is relative to the beginning of
6868 * the transport-layer header.
6870 * Load the X register with the length of the IPv4 header
6871 * (plus the offset of the link-layer header, if it's
6872 * a variable-length header), in bytes.
6874 * XXX - are there any cases where we want
6876 * XXX - we should, if we're built with
6877 * IPv6 support, generate code to load either
6878 * IPv4, IPv6, or both, as appropriate.
6880 s
= gen_loadx_iphdrlen();
6883 * The X register now contains the sum of the variable
6884 * part of the offset of the link-layer payload and the
6885 * length of the network-layer header.
6887 * Load into the A register the offset relative to
6888 * the beginning of the transport layer header,
6889 * add the X register to that, move that to the
6890 * X register, and load with an offset from the
6891 * X register equal to the sum of the constant part of
6892 * the offset of the link-layer payload and the offset,
6893 * relative to the beginning of the link-layer payload,
6894 * of the network-layer header.
6896 sappend(s
, xfer_to_a(inst
));
6897 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6898 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6899 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6900 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6901 sappend(inst
->s
, s
);
6904 * Do the computation only if the packet contains
6905 * the protocol in question - which is true only
6906 * if this is an IP datagram and is the first or
6907 * only fragment of that datagram.
6909 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6911 gen_and(inst
->b
, b
);
6912 gen_and(gen_proto_abbrev(Q_IP
), b
);
6916 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6919 inst
->regno
= regno
;
6920 s
= new_stmt(BPF_ST
);
6922 sappend(inst
->s
, s
);
6928 gen_relation(code
, a0
, a1
, reversed
)
6930 struct arth
*a0
, *a1
;
6933 struct slist
*s0
, *s1
, *s2
;
6934 struct block
*b
, *tmp
;
6938 if (code
== BPF_JEQ
) {
6939 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6940 b
= new_block(JMP(code
));
6944 b
= new_block(BPF_JMP
|code
|BPF_X
);
6950 sappend(a0
->s
, a1
->s
);
6954 free_reg(a0
->regno
);
6955 free_reg(a1
->regno
);
6957 /* 'and' together protocol checks */
6960 gen_and(a0
->b
, tmp
= a1
->b
);
6976 int regno
= alloc_reg();
6977 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6980 s
= new_stmt(BPF_LD
|BPF_LEN
);
6981 s
->next
= new_stmt(BPF_ST
);
6982 s
->next
->s
.k
= regno
;
6997 a
= (struct arth
*)newchunk(sizeof(*a
));
7001 s
= new_stmt(BPF_LD
|BPF_IMM
);
7003 s
->next
= new_stmt(BPF_ST
);
7019 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7022 s
= new_stmt(BPF_ST
);
7030 gen_arth(code
, a0
, a1
)
7032 struct arth
*a0
, *a1
;
7034 struct slist
*s0
, *s1
, *s2
;
7038 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7043 sappend(a0
->s
, a1
->s
);
7045 free_reg(a0
->regno
);
7046 free_reg(a1
->regno
);
7048 s0
= new_stmt(BPF_ST
);
7049 a0
->regno
= s0
->s
.k
= alloc_reg();
7056 * Here we handle simple allocation of the scratch registers.
7057 * If too many registers are alloc'd, the allocator punts.
7059 static int regused
[BPF_MEMWORDS
];
7063 * Initialize the table of used registers and the current register.
7069 memset(regused
, 0, sizeof regused
);
7073 * Return the next free register.
7078 int n
= BPF_MEMWORDS
;
7081 if (regused
[curreg
])
7082 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7084 regused
[curreg
] = 1;
7088 bpf_error("too many registers needed to evaluate expression");
7094 * Return a register to the table so it can
7104 static struct block
*
7111 s
= new_stmt(BPF_LD
|BPF_LEN
);
7112 b
= new_block(JMP(jmp
));
7123 return gen_len(BPF_JGE
, n
);
7127 * Actually, this is less than or equal.
7135 b
= gen_len(BPF_JGT
, n
);
7142 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7143 * the beginning of the link-layer header.
7144 * XXX - that means you can't test values in the radiotap header, but
7145 * as that header is difficult if not impossible to parse generally
7146 * without a loop, that might not be a severe problem. A new keyword
7147 * "radio" could be added for that, although what you'd really want
7148 * would be a way of testing particular radio header values, which
7149 * would generate code appropriate to the radio header in question.
7152 gen_byteop(op
, idx
, val
)
7163 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7166 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7170 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7174 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7178 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7182 b
= new_block(JMP(BPF_JEQ
));
7189 static u_char abroadcast
[] = { 0x0 };
7192 gen_broadcast(proto
)
7195 bpf_u_int32 hostmask
;
7196 struct block
*b0
, *b1
, *b2
;
7197 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7205 case DLT_ARCNET_LINUX
:
7206 return gen_ahostop(abroadcast
, Q_DST
);
7208 case DLT_NETANALYZER
:
7209 case DLT_NETANALYZER_TRANSPARENT
:
7210 b1
= gen_prevlinkhdr_check();
7211 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7216 return gen_fhostop(ebroadcast
, Q_DST
);
7218 return gen_thostop(ebroadcast
, Q_DST
);
7219 case DLT_IEEE802_11
:
7220 case DLT_PRISM_HEADER
:
7221 case DLT_IEEE802_11_RADIO_AVS
:
7222 case DLT_IEEE802_11_RADIO
:
7224 return gen_wlanhostop(ebroadcast
, Q_DST
);
7225 case DLT_IP_OVER_FC
:
7226 return gen_ipfchostop(ebroadcast
, Q_DST
);
7228 bpf_error("not a broadcast link");
7234 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7235 * as an indication that we don't know the netmask, and fail
7238 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7239 bpf_error("netmask not known, so 'ip broadcast' not supported");
7240 b0
= gen_linktype(ETHERTYPE_IP
);
7241 hostmask
= ~netmask
;
7242 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7243 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7244 (bpf_int32
)(~0 & hostmask
), hostmask
);
7249 bpf_error("only link-layer/IP broadcast filters supported");
7255 * Generate code to test the low-order bit of a MAC address (that's
7256 * the bottom bit of the *first* byte).
7258 static struct block
*
7259 gen_mac_multicast(offset
)
7262 register struct block
*b0
;
7263 register struct slist
*s
;
7265 /* link[offset] & 1 != 0 */
7266 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7267 b0
= new_block(JMP(BPF_JSET
));
7274 gen_multicast(proto
)
7277 register struct block
*b0
, *b1
, *b2
;
7278 register struct slist
*s
;
7286 case DLT_ARCNET_LINUX
:
7287 /* all ARCnet multicasts use the same address */
7288 return gen_ahostop(abroadcast
, Q_DST
);
7290 case DLT_NETANALYZER
:
7291 case DLT_NETANALYZER_TRANSPARENT
:
7292 b1
= gen_prevlinkhdr_check();
7293 /* ether[0] & 1 != 0 */
7294 b0
= gen_mac_multicast(0);
7300 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7302 * XXX - was that referring to bit-order issues?
7304 /* fddi[1] & 1 != 0 */
7305 return gen_mac_multicast(1);
7307 /* tr[2] & 1 != 0 */
7308 return gen_mac_multicast(2);
7309 case DLT_IEEE802_11
:
7310 case DLT_PRISM_HEADER
:
7311 case DLT_IEEE802_11_RADIO_AVS
:
7312 case DLT_IEEE802_11_RADIO
:
7317 * For control frames, there is no DA.
7319 * For management frames, DA is at an
7320 * offset of 4 from the beginning of
7323 * For data frames, DA is at an offset
7324 * of 4 from the beginning of the packet
7325 * if To DS is clear and at an offset of
7326 * 16 from the beginning of the packet
7331 * Generate the tests to be done for data frames.
7333 * First, check for To DS set, i.e. "link[1] & 0x01".
7335 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7336 b1
= new_block(JMP(BPF_JSET
));
7337 b1
->s
.k
= 0x01; /* To DS */
7341 * If To DS is set, the DA is at 16.
7343 b0
= gen_mac_multicast(16);
7347 * Now, check for To DS not set, i.e. check
7348 * "!(link[1] & 0x01)".
7350 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7351 b2
= new_block(JMP(BPF_JSET
));
7352 b2
->s
.k
= 0x01; /* To DS */
7357 * If To DS is not set, the DA is at 4.
7359 b1
= gen_mac_multicast(4);
7363 * Now OR together the last two checks. That gives
7364 * the complete set of checks for data frames.
7369 * Now check for a data frame.
7370 * I.e, check "link[0] & 0x08".
7372 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7373 b1
= new_block(JMP(BPF_JSET
));
7378 * AND that with the checks done for data frames.
7383 * If the high-order bit of the type value is 0, this
7384 * is a management frame.
7385 * I.e, check "!(link[0] & 0x08)".
7387 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7388 b2
= new_block(JMP(BPF_JSET
));
7394 * For management frames, the DA is at 4.
7396 b1
= gen_mac_multicast(4);
7400 * OR that with the checks done for data frames.
7401 * That gives the checks done for management and
7407 * If the low-order bit of the type value is 1,
7408 * this is either a control frame or a frame
7409 * with a reserved type, and thus not a
7412 * I.e., check "!(link[0] & 0x04)".
7414 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7415 b1
= new_block(JMP(BPF_JSET
));
7421 * AND that with the checks for data and management
7426 case DLT_IP_OVER_FC
:
7427 b0
= gen_mac_multicast(2);
7432 /* Link not known to support multicasts */
7436 b0
= gen_linktype(ETHERTYPE_IP
);
7437 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7442 b0
= gen_linktype(ETHERTYPE_IPV6
);
7443 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7447 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7453 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7454 * Outbound traffic is sent by this machine, while inbound traffic is
7455 * sent by a remote machine (and may include packets destined for a
7456 * unicast or multicast link-layer address we are not subscribing to).
7457 * These are the same definitions implemented by pcap_setdirection().
7458 * Capturing only unicast traffic destined for this host is probably
7459 * better accomplished using a higher-layer filter.
7465 register struct block
*b0
;
7468 * Only some data link types support inbound/outbound qualifiers.
7472 b0
= gen_relation(BPF_JEQ
,
7473 gen_load(Q_LINK
, gen_loadi(0), 1),
7480 /* match outgoing packets */
7481 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7483 /* match incoming packets */
7484 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7489 /* match outgoing packets */
7490 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7492 /* to filter on inbound traffic, invert the match */
7497 #ifdef HAVE_NET_PFVAR_H
7499 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7500 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7506 /* match outgoing packets */
7507 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7509 /* match incoming packets */
7510 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7514 case DLT_JUNIPER_MFR
:
7515 case DLT_JUNIPER_MLFR
:
7516 case DLT_JUNIPER_MLPPP
:
7517 case DLT_JUNIPER_ATM1
:
7518 case DLT_JUNIPER_ATM2
:
7519 case DLT_JUNIPER_PPPOE
:
7520 case DLT_JUNIPER_PPPOE_ATM
:
7521 case DLT_JUNIPER_GGSN
:
7522 case DLT_JUNIPER_ES
:
7523 case DLT_JUNIPER_MONITOR
:
7524 case DLT_JUNIPER_SERVICES
:
7525 case DLT_JUNIPER_ETHER
:
7526 case DLT_JUNIPER_PPP
:
7527 case DLT_JUNIPER_FRELAY
:
7528 case DLT_JUNIPER_CHDLC
:
7529 case DLT_JUNIPER_VP
:
7530 case DLT_JUNIPER_ST
:
7531 case DLT_JUNIPER_ISM
:
7532 case DLT_JUNIPER_VS
:
7533 case DLT_JUNIPER_SRX_E2E
:
7534 case DLT_JUNIPER_FIBRECHANNEL
:
7535 case DLT_JUNIPER_ATM_CEMIC
:
7537 /* juniper flags (including direction) are stored
7538 * the byte after the 3-byte magic number */
7540 /* match outgoing packets */
7541 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7543 /* match incoming packets */
7544 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7550 * If we have packet meta-data indicating a direction,
7551 * check it, otherwise give up as this link-layer type
7552 * has nothing in the packet data.
7554 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7556 * This is Linux with PF_PACKET support.
7557 * If this is a *live* capture, we can look at
7558 * special meta-data in the filter expression;
7559 * if it's a savefile, we can't.
7561 if (bpf_pcap
->rfile
!= NULL
) {
7562 /* We have a FILE *, so this is a savefile */
7563 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7568 /* match outgoing packets */
7569 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7572 /* to filter on inbound traffic, invert the match */
7575 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7576 bpf_error("inbound/outbound not supported on linktype %d",
7580 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7585 #ifdef HAVE_NET_PFVAR_H
7586 /* PF firewall log matched interface */
7588 gen_pf_ifname(const char *ifname
)
7593 if (linktype
!= DLT_PFLOG
) {
7594 bpf_error("ifname supported only on PF linktype");
7597 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7598 off
= offsetof(struct pfloghdr
, ifname
);
7599 if (strlen(ifname
) >= len
) {
7600 bpf_error("ifname interface names can only be %d characters",
7604 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7608 /* PF firewall log ruleset name */
7610 gen_pf_ruleset(char *ruleset
)
7614 if (linktype
!= DLT_PFLOG
) {
7615 bpf_error("ruleset supported only on PF linktype");
7619 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7620 bpf_error("ruleset names can only be %ld characters",
7621 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7625 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7626 strlen(ruleset
), (const u_char
*)ruleset
);
7630 /* PF firewall log rule number */
7636 if (linktype
!= DLT_PFLOG
) {
7637 bpf_error("rnr supported only on PF linktype");
7641 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7646 /* PF firewall log sub-rule number */
7648 gen_pf_srnr(int srnr
)
7652 if (linktype
!= DLT_PFLOG
) {
7653 bpf_error("srnr supported only on PF linktype");
7657 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7662 /* PF firewall log reason code */
7664 gen_pf_reason(int reason
)
7668 if (linktype
!= DLT_PFLOG
) {
7669 bpf_error("reason supported only on PF linktype");
7673 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7678 /* PF firewall log action */
7680 gen_pf_action(int action
)
7684 if (linktype
!= DLT_PFLOG
) {
7685 bpf_error("action supported only on PF linktype");
7689 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7693 #else /* !HAVE_NET_PFVAR_H */
7695 gen_pf_ifname(const char *ifname
)
7697 bpf_error("libpcap was compiled without pf support");
7703 gen_pf_ruleset(char *ruleset
)
7705 bpf_error("libpcap was compiled on a machine without pf support");
7713 bpf_error("libpcap was compiled on a machine without pf support");
7719 gen_pf_srnr(int srnr
)
7721 bpf_error("libpcap was compiled on a machine without pf support");
7727 gen_pf_reason(int reason
)
7729 bpf_error("libpcap was compiled on a machine without pf support");
7735 gen_pf_action(int action
)
7737 bpf_error("libpcap was compiled on a machine without pf support");
7741 #endif /* HAVE_NET_PFVAR_H */
7743 /* IEEE 802.11 wireless header */
7745 gen_p80211_type(int type
, int mask
)
7751 case DLT_IEEE802_11
:
7752 case DLT_PRISM_HEADER
:
7753 case DLT_IEEE802_11_RADIO_AVS
:
7754 case DLT_IEEE802_11_RADIO
:
7755 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7760 bpf_error("802.11 link-layer types supported only on 802.11");
7768 gen_p80211_fcdir(int fcdir
)
7774 case DLT_IEEE802_11
:
7775 case DLT_PRISM_HEADER
:
7776 case DLT_IEEE802_11_RADIO_AVS
:
7777 case DLT_IEEE802_11_RADIO
:
7781 bpf_error("frame direction supported only with 802.11 headers");
7785 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7786 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7793 register const u_char
*eaddr
;
7799 case DLT_ARCNET_LINUX
:
7800 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7802 return (gen_ahostop(eaddr
, (int)q
.dir
));
7804 bpf_error("ARCnet address used in non-arc expression");
7810 bpf_error("aid supported only on ARCnet");
7813 bpf_error("ARCnet address used in non-arc expression");
7818 static struct block
*
7819 gen_ahostop(eaddr
, dir
)
7820 register const u_char
*eaddr
;
7823 register struct block
*b0
, *b1
;
7826 /* src comes first, different from Ethernet */
7828 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7831 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7834 b0
= gen_ahostop(eaddr
, Q_SRC
);
7835 b1
= gen_ahostop(eaddr
, Q_DST
);
7841 b0
= gen_ahostop(eaddr
, Q_SRC
);
7842 b1
= gen_ahostop(eaddr
, Q_DST
);
7847 bpf_error("'addr1' is only supported on 802.11");
7851 bpf_error("'addr2' is only supported on 802.11");
7855 bpf_error("'addr3' is only supported on 802.11");
7859 bpf_error("'addr4' is only supported on 802.11");
7863 bpf_error("'ra' is only supported on 802.11");
7867 bpf_error("'ta' is only supported on 802.11");
7874 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7875 static struct block
*
7876 gen_vlan_bpf_extensions(int vlan_num
)
7878 struct block
*b0
, *b1
;
7881 /* generate new filter code based on extracting packet
7883 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7884 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7886 b0
= new_block(JMP(BPF_JEQ
));
7890 if (vlan_num
>= 0) {
7891 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7892 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7894 b1
= new_block(JMP(BPF_JEQ
));
7896 b1
->s
.k
= (bpf_int32
) vlan_num
;
7906 static struct block
*
7907 gen_vlan_no_bpf_extensions(int vlan_num
)
7909 struct block
*b0
, *b1
;
7911 /* check for VLAN, including QinQ */
7912 b0
= gen_linktype(ETHERTYPE_8021Q
);
7913 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7917 /* If a specific VLAN is requested, check VLAN id */
7918 if (vlan_num
>= 0) {
7919 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7920 (bpf_int32
)vlan_num
, 0x0fff);
7926 * The payload follows the full header, including the
7927 * VLAN tags, so skip past this VLAN tag.
7929 off_linkpl
.constant_part
+= 4;
7932 * The link-layer type information follows the VLAN tags, so
7933 * skip past this VLAN tag.
7935 off_linktype
.constant_part
+= 4;
7941 * support IEEE 802.1Q VLAN trunk over ethernet
7949 /* can't check for VLAN-encapsulated packets inside MPLS */
7950 if (label_stack_depth
> 0)
7951 bpf_error("no VLAN match after MPLS");
7954 * Check for a VLAN packet, and then change the offsets to point
7955 * to the type and data fields within the VLAN packet. Just
7956 * increment the offsets, so that we can support a hierarchy, e.g.
7957 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7960 * XXX - this is a bit of a kludge. If we were to split the
7961 * compiler into a parser that parses an expression and
7962 * generates an expression tree, and a code generator that
7963 * takes an expression tree (which could come from our
7964 * parser or from some other parser) and generates BPF code,
7965 * we could perhaps make the offsets parameters of routines
7966 * and, in the handler for an "AND" node, pass to subnodes
7967 * other than the VLAN node the adjusted offsets.
7969 * This would mean that "vlan" would, instead of changing the
7970 * behavior of *all* tests after it, change only the behavior
7971 * of tests ANDed with it. That would change the documented
7972 * semantics of "vlan", which might break some expressions.
7973 * However, it would mean that "(vlan and ip) or ip" would check
7974 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7975 * checking only for VLAN-encapsulated IP, so that could still
7976 * be considered worth doing; it wouldn't break expressions
7977 * that are of the form "vlan and ..." or "vlan N and ...",
7978 * which I suspect are the most common expressions involving
7979 * "vlan". "vlan or ..." doesn't necessarily do what the user
7980 * would really want, now, as all the "or ..." tests would
7981 * be done assuming a VLAN, even though the "or" could be viewed
7982 * as meaning "or, if this isn't a VLAN packet...".
7987 case DLT_NETANALYZER
:
7988 case DLT_NETANALYZER_TRANSPARENT
:
7989 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7990 /* Verify that this is the outer part of the packet and
7991 * not encapsulated somehow. */
7992 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
7993 off_linkhdr
.constant_part
==
7994 off_outermostlinkhdr
.constant_part
) {
7996 * Do we need special VLAN handling?
7998 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
7999 b0
= gen_vlan_bpf_extensions(vlan_num
);
8001 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8004 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8007 case DLT_IEEE802_11
:
8008 case DLT_PRISM_HEADER
:
8009 case DLT_IEEE802_11_RADIO_AVS
:
8010 case DLT_IEEE802_11_RADIO
:
8011 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8015 bpf_error("no VLAN support for data link type %d",
8032 struct block
*b0
, *b1
;
8034 if (label_stack_depth
> 0) {
8035 /* just match the bottom-of-stack bit clear */
8036 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8039 * We're not in an MPLS stack yet, so check the link-layer
8040 * type against MPLS.
8044 case DLT_C_HDLC
: /* fall through */
8046 case DLT_NETANALYZER
:
8047 case DLT_NETANALYZER_TRANSPARENT
:
8048 b0
= gen_linktype(ETHERTYPE_MPLS
);
8052 b0
= gen_linktype(PPP_MPLS_UCAST
);
8055 /* FIXME add other DLT_s ...
8056 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8057 * leave it for now */
8060 bpf_error("no MPLS support for data link type %d",
8068 /* If a specific MPLS label is requested, check it */
8069 if (label_num
>= 0) {
8070 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8071 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8072 0xfffff000); /* only compare the first 20 bits */
8078 * Change the offsets to point to the type and data fields within
8079 * the MPLS packet. Just increment the offsets, so that we
8080 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8081 * capture packets with an outer label of 100000 and an inner
8084 * Increment the MPLS stack depth as well; this indicates that
8085 * we're checking MPLS-encapsulated headers, to make sure higher
8086 * level code generators don't try to match against IP-related
8087 * protocols such as Q_ARP, Q_RARP etc.
8089 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8093 label_stack_depth
++;
8098 * Support PPPOE discovery and session.
8103 /* check for PPPoE discovery */
8104 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8108 gen_pppoes(sess_num
)
8111 struct block
*b0
, *b1
;
8114 * Test against the PPPoE session link-layer type.
8116 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8118 /* If a specific session is requested, check PPPoE session id */
8119 if (sess_num
>= 0) {
8120 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8121 (bpf_int32
)sess_num
, 0x0000ffff);
8127 * Change the offsets to point to the type and data fields within
8128 * the PPP packet, and note that this is PPPoE rather than
8131 * XXX - this is a bit of a kludge. If we were to split the
8132 * compiler into a parser that parses an expression and
8133 * generates an expression tree, and a code generator that
8134 * takes an expression tree (which could come from our
8135 * parser or from some other parser) and generates BPF code,
8136 * we could perhaps make the offsets parameters of routines
8137 * and, in the handler for an "AND" node, pass to subnodes
8138 * other than the PPPoE node the adjusted offsets.
8140 * This would mean that "pppoes" would, instead of changing the
8141 * behavior of *all* tests after it, change only the behavior
8142 * of tests ANDed with it. That would change the documented
8143 * semantics of "pppoes", which might break some expressions.
8144 * However, it would mean that "(pppoes and ip) or ip" would check
8145 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8146 * checking only for VLAN-encapsulated IP, so that could still
8147 * be considered worth doing; it wouldn't break expressions
8148 * that are of the form "pppoes and ..." which I suspect are the
8149 * most common expressions involving "pppoes". "pppoes or ..."
8150 * doesn't necessarily do what the user would really want, now,
8151 * as all the "or ..." tests would be done assuming PPPoE, even
8152 * though the "or" could be viewed as meaning "or, if this isn't
8153 * a PPPoE packet...".
8155 * The "network-layer" protocol is PPPoE, which has a 6-byte
8156 * PPPoE header, followed by a PPP packet.
8158 * There is no HDLC encapsulation for the PPP packet (it's
8159 * encapsulated in PPPoES instead), so the link-layer type
8160 * starts at the first byte of the PPP packet. For PPPoE,
8161 * that offset is relative to the beginning of the total
8162 * link-layer payload, including any 802.2 LLC header, so
8163 * it's 6 bytes past off_nl.
8165 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8166 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8169 off_linktype
= off_linkhdr
;
8170 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8173 off_nl_nosnap
= 0; /* no 802.2 LLC */
8178 /* Check that this is Geneve and the VNI is correct if
8179 * specified. Parameterized to handle both IPv4 and IPv6. */
8180 static struct block
*
8181 gen_geneve_check(struct block
*(*gen_portfn
)(int, int, int),
8182 enum e_offrel offrel
, int vni
)
8184 struct block
*b0
, *b1
;
8186 b0
= gen_portfn(GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8188 /* Check that we are operating on version 0. Otherwise, we
8189 * can't decode the rest of the fields. The version is 2 bits
8190 * in the first byte of the Geneve header. */
8191 b1
= gen_mcmp(offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8196 vni
<<= 8; /* VNI is in the upper 3 bytes */
8197 b1
= gen_mcmp(offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8206 /* The IPv4 and IPv6 Geneve checks need to do two things:
8207 * - Verify that this actually is Geneve with the right VNI.
8208 * - Place the IP header length (plus variable link prefix if
8209 * needed) into register A to be used later to compute
8210 * the inner packet offsets. */
8211 static struct block
*
8212 gen_geneve4(int vni
)
8214 struct block
*b0
, *b1
;
8215 struct slist
*s
, *s1
;
8217 b0
= gen_geneve_check(gen_port
, OR_TRAN_IPV4
, vni
);
8219 /* Load the IP header length into A. */
8220 s
= gen_loadx_iphdrlen();
8222 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8225 /* Forcibly append these statements to the true condition
8226 * of the protocol check by creating a new block that is
8227 * always true and ANDing them. */
8228 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8237 static struct block
*
8238 gen_geneve6(int vni
)
8240 struct block
*b0
, *b1
;
8241 struct slist
*s
, *s1
;
8243 b0
= gen_geneve_check(gen_port6
, OR_TRAN_IPV6
, vni
);
8245 /* Load the IP header length. We need to account for a
8246 * variable length link prefix if there is one. */
8247 s
= gen_abs_offset_varpart(&off_linkpl
);
8249 s1
= new_stmt(BPF_LD
|BPF_IMM
);
8253 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8257 s
= new_stmt(BPF_LD
|BPF_IMM
);
8261 /* Forcibly append these statements to the true condition
8262 * of the protocol check by creating a new block that is
8263 * always true and ANDing them. */
8264 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8267 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8276 /* We need to store three values based on the Geneve header::
8277 * - The offset of the linktype.
8278 * - The offset of the end of the Geneve header.
8279 * - The offset of the end of the encapsulated MAC header. */
8280 static struct slist
*
8281 gen_geneve_offsets(void)
8283 struct slist
*s
, *s1
, *s_proto
;
8285 /* First we need to calculate the offset of the Geneve header
8286 * itself. This is composed of the IP header previously calculated
8287 * (include any variable link prefix) and stored in A plus the
8288 * fixed sized headers (fixed link prefix, MAC length, and UDP
8290 s
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8291 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 8;
8293 /* Stash this in X since we'll need it later. */
8294 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8297 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8299 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8303 off_linktype
.reg
= alloc_reg();
8304 off_linktype
.is_variable
= 1;
8305 off_linktype
.constant_part
= 0;
8307 s1
= new_stmt(BPF_ST
);
8308 s1
->s
.k
= off_linktype
.reg
;
8311 /* Load the Geneve option length and mask and shift to get the
8312 * number of bytes. It is stored in the first byte of the Geneve
8314 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
8318 s1
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
8322 s1
= new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
8326 /* Add in the rest of the Geneve base header. */
8327 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8331 /* Add the Geneve header length to its offset and store. */
8332 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8336 /* Set the encapsulated type as Ethernet. Even though we may
8337 * not actually have Ethernet inside there are two reasons this
8339 * - The linktype field is always in EtherType format regardless
8340 * of whether it is in Geneve or an inner Ethernet frame.
8341 * - The only link layer that we have specific support for is
8342 * Ethernet. We will confirm that the packet actually is
8343 * Ethernet at runtime before executing these checks. */
8344 PUSH_LINKHDR(DLT_EN10MB
, 1, 0, alloc_reg());
8346 s1
= new_stmt(BPF_ST
);
8347 s1
->s
.k
= off_linkhdr
.reg
;
8350 /* Calculate whether we have an Ethernet header or just raw IP/
8351 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8352 * and linktype by 14 bytes so that the network header can be found
8353 * seamlessly. Otherwise, keep what we've calculated already. */
8355 /* We have a bare jmp so we can't use the optimizer. */
8358 /* Load the EtherType in the Geneve header, 2 bytes in. */
8359 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
8363 /* Load X with the end of the Geneve header. */
8364 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8365 s1
->s
.k
= off_linkhdr
.reg
;
8368 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8369 * end of this check, we should have the total length in X. In
8370 * the non-Ethernet case, it's already there. */
8371 s_proto
= new_stmt(JMP(BPF_JEQ
));
8372 s_proto
->s
.k
= ETHERTYPE_TEB
;
8373 sappend(s
, s_proto
);
8375 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8379 /* Since this is Ethernet, use the EtherType of the payload
8380 * directly as the linktype. Overwrite what we already have. */
8381 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8385 s1
= new_stmt(BPF_ST
);
8386 s1
->s
.k
= off_linktype
.reg
;
8389 /* Advance two bytes further to get the end of the Ethernet
8391 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8395 /* Move the result to X. */
8396 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8399 /* Store the final result of our linkpl calculation. */
8400 off_linkpl
.reg
= alloc_reg();
8401 off_linkpl
.is_variable
= 1;
8402 off_linkpl
.constant_part
= 0;
8404 s1
= new_stmt(BPF_STX
);
8405 s1
->s
.k
= off_linkpl
.reg
;
8414 /* Check to see if this is a Geneve packet. */
8418 struct block
*b0
, *b1
;
8421 b0
= gen_geneve4(vni
);
8422 b1
= gen_geneve6(vni
);
8427 /* Later filters should act on the payload of the Geneve frame,
8428 * update all of the header pointers. Attach this code so that
8429 * it gets executed in the event that the Geneve filter matches. */
8430 s
= gen_geneve_offsets();
8433 sappend(s
, b1
->stmts
);
8443 /* Check that the encapsulated frame has a link layer header
8444 * for Ethernet filters. */
8445 static struct block
*
8446 gen_geneve_ll_check()
8449 struct slist
*s
, *s1
;
8451 /* The easiest way to see if there is a link layer present
8452 * is to check if the link layer header and payload are not
8455 /* Geneve always generates pure variable offsets so we can
8456 * compare only the registers. */
8457 s
= new_stmt(BPF_LD
|BPF_MEM
);
8458 s
->s
.k
= off_linkhdr
.reg
;
8460 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8461 s1
->s
.k
= off_linkpl
.reg
;
8464 b0
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8473 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8485 bpf_error("'vpi' supported only on raw ATM");
8486 if (off_vpi
== (u_int
)-1)
8488 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8494 bpf_error("'vci' supported only on raw ATM");
8495 if (off_vci
== (u_int
)-1)
8497 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8502 if (off_proto
== (u_int
)-1)
8503 abort(); /* XXX - this isn't on FreeBSD */
8504 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8509 if (off_payload
== (u_int
)-1)
8511 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8512 0xffffffff, jtype
, reverse
, jvalue
);
8517 bpf_error("'callref' supported only on raw ATM");
8518 if (off_proto
== (u_int
)-1)
8520 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8521 jtype
, reverse
, jvalue
);
8531 gen_atmtype_abbrev(type
)
8534 struct block
*b0
, *b1
;
8539 /* Get all packets in Meta signalling Circuit */
8541 bpf_error("'metac' supported only on raw ATM");
8542 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8543 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8548 /* Get all packets in Broadcast Circuit*/
8550 bpf_error("'bcc' supported only on raw ATM");
8551 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8552 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8557 /* Get all cells in Segment OAM F4 circuit*/
8559 bpf_error("'oam4sc' supported only on raw ATM");
8560 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8561 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8566 /* Get all cells in End-to-End OAM F4 Circuit*/
8568 bpf_error("'oam4ec' supported only on raw ATM");
8569 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8570 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8575 /* Get all packets in connection Signalling Circuit */
8577 bpf_error("'sc' supported only on raw ATM");
8578 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8579 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8584 /* Get all packets in ILMI Circuit */
8586 bpf_error("'ilmic' supported only on raw ATM");
8587 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8588 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8593 /* Get all LANE packets */
8595 bpf_error("'lane' supported only on raw ATM");
8596 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8599 * Arrange that all subsequent tests assume LANE
8600 * rather than LLC-encapsulated packets, and set
8601 * the offsets appropriately for LANE-encapsulated
8604 * We assume LANE means Ethernet, not Token Ring.
8606 PUSH_LINKHDR(DLT_EN10MB
, 0,
8607 off_payload
+ 2, /* Ethernet header */
8609 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8610 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8611 off_nl
= 0; /* Ethernet II */
8612 off_nl_nosnap
= 3; /* 802.3+802.2 */
8616 /* Get all LLC-encapsulated packets */
8618 bpf_error("'llc' supported only on raw ATM");
8619 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8620 linktype
= prevlinktype
;
8630 * Filtering for MTP2 messages based on li value
8631 * FISU, length is null
8632 * LSSU, length is 1 or 2
8633 * MSU, length is 3 or more
8634 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8637 gen_mtp2type_abbrev(type
)
8640 struct block
*b0
, *b1
;
8645 if ( (linktype
!= DLT_MTP2
) &&
8646 (linktype
!= DLT_ERF
) &&
8647 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8648 bpf_error("'fisu' supported only on MTP2");
8649 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8650 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8654 if ( (linktype
!= DLT_MTP2
) &&
8655 (linktype
!= DLT_ERF
) &&
8656 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8657 bpf_error("'lssu' supported only on MTP2");
8658 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8659 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8664 if ( (linktype
!= DLT_MTP2
) &&
8665 (linktype
!= DLT_ERF
) &&
8666 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8667 bpf_error("'msu' supported only on MTP2");
8668 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8672 if ( (linktype
!= DLT_MTP2
) &&
8673 (linktype
!= DLT_ERF
) &&
8674 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8675 bpf_error("'hfisu' supported only on MTP2_HSL");
8676 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8677 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8681 if ( (linktype
!= DLT_MTP2
) &&
8682 (linktype
!= DLT_ERF
) &&
8683 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8684 bpf_error("'hlssu' supported only on MTP2_HSL");
8685 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8686 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8691 if ( (linktype
!= DLT_MTP2
) &&
8692 (linktype
!= DLT_ERF
) &&
8693 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8694 bpf_error("'hmsu' supported only on MTP2_HSL");
8695 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8705 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8712 bpf_u_int32 val1
, val2
, val3
;
8713 u_int newoff_sio
=off_sio
;
8714 u_int newoff_opc
=off_opc
;
8715 u_int newoff_dpc
=off_dpc
;
8716 u_int newoff_sls
=off_sls
;
8718 switch (mtp3field
) {
8721 newoff_sio
+= 3; /* offset for MTP2_HSL */
8725 if (off_sio
== (u_int
)-1)
8726 bpf_error("'sio' supported only on SS7");
8727 /* sio coded on 1 byte so max value 255 */
8729 bpf_error("sio value %u too big; max value = 255",
8731 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8732 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8738 if (off_opc
== (u_int
)-1)
8739 bpf_error("'opc' supported only on SS7");
8740 /* opc coded on 14 bits so max value 16383 */
8742 bpf_error("opc value %u too big; max value = 16383",
8744 /* the following instructions are made to convert jvalue
8745 * to the form used to write opc in an ss7 message*/
8746 val1
= jvalue
& 0x00003c00;
8748 val2
= jvalue
& 0x000003fc;
8750 val3
= jvalue
& 0x00000003;
8752 jvalue
= val1
+ val2
+ val3
;
8753 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8754 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8762 if (off_dpc
== (u_int
)-1)
8763 bpf_error("'dpc' supported only on SS7");
8764 /* dpc coded on 14 bits so max value 16383 */
8766 bpf_error("dpc value %u too big; max value = 16383",
8768 /* the following instructions are made to convert jvalue
8769 * to the forme used to write dpc in an ss7 message*/
8770 val1
= jvalue
& 0x000000ff;
8772 val2
= jvalue
& 0x00003f00;
8774 jvalue
= val1
+ val2
;
8775 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8776 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8782 if (off_sls
== (u_int
)-1)
8783 bpf_error("'sls' supported only on SS7");
8784 /* sls coded on 4 bits so max value 15 */
8786 bpf_error("sls value %u too big; max value = 15",
8788 /* the following instruction is made to convert jvalue
8789 * to the forme used to write sls in an ss7 message*/
8790 jvalue
= jvalue
<< 4;
8791 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8792 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8801 static struct block
*
8802 gen_msg_abbrev(type
)
8808 * Q.2931 signalling protocol messages for handling virtual circuits
8809 * establishment and teardown
8814 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8818 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8822 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8826 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8830 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8833 case A_RELEASE_DONE
:
8834 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8844 gen_atmmulti_abbrev(type
)
8847 struct block
*b0
, *b1
;
8853 bpf_error("'oam' supported only on raw ATM");
8854 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8859 bpf_error("'oamf4' supported only on raw ATM");
8861 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8862 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8864 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8870 * Get Q.2931 signalling messages for switched
8871 * virtual connection
8874 bpf_error("'connectmsg' supported only on raw ATM");
8875 b0
= gen_msg_abbrev(A_SETUP
);
8876 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8878 b0
= gen_msg_abbrev(A_CONNECT
);
8880 b0
= gen_msg_abbrev(A_CONNECTACK
);
8882 b0
= gen_msg_abbrev(A_RELEASE
);
8884 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8886 b0
= gen_atmtype_abbrev(A_SC
);
8892 bpf_error("'metaconnect' supported only on raw ATM");
8893 b0
= gen_msg_abbrev(A_SETUP
);
8894 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8896 b0
= gen_msg_abbrev(A_CONNECT
);
8898 b0
= gen_msg_abbrev(A_RELEASE
);
8900 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8902 b0
= gen_atmtype_abbrev(A_METAC
);