1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
43 * XXX - why was this included even on UNIX?
52 #include <sys/param.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
72 #include "ethertype.h"
76 #include "ieee80211.h"
78 #include "sunatmpos.h"
81 #include "pcap/ipnet.h"
83 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
84 #include <linux/types.h>
85 #include <linux/if_packet.h>
86 #include <linux/filter.h>
88 #ifdef HAVE_NET_PFVAR_H
89 #include <sys/socket.h>
91 #include <net/pfvar.h>
92 #include <net/if_pflog.h>
95 #define offsetof(s, e) ((size_t)&((s *)0)->e)
99 #include <netdb.h> /* for "struct addrinfo" */
102 #include <pcap/namedb.h>
104 #define ETHERMTU 1500
106 #ifndef ETHERTYPE_TEB
107 #define ETHERTYPE_TEB 0x6558
110 #ifndef IPPROTO_HOPOPTS
111 #define IPPROTO_HOPOPTS 0
113 #ifndef IPPROTO_ROUTING
114 #define IPPROTO_ROUTING 43
116 #ifndef IPPROTO_FRAGMENT
117 #define IPPROTO_FRAGMENT 44
119 #ifndef IPPROTO_DSTOPTS
120 #define IPPROTO_DSTOPTS 60
123 #define IPPROTO_SCTP 132
126 #define GENEVE_PORT 6081
128 #ifdef HAVE_OS_PROTO_H
129 #include "os-proto.h"
132 #define JMP(c) ((c)|BPF_JMP|BPF_K)
135 static jmp_buf top_ctx
;
136 static pcap_t
*bpf_pcap
;
138 /* Hack for handling VLAN and MPLS stacks. */
140 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
142 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
146 static int pcap_fddipad
;
150 bpf_error(const char *fmt
, ...)
155 if (bpf_pcap
!= NULL
)
156 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
163 static void init_linktype(pcap_t
*);
165 static void init_regs(void);
166 static int alloc_reg(void);
167 static void free_reg(int);
169 static struct block
*root
;
172 * Absolute offsets, which are offsets from the beginning of the raw
173 * packet data, are, in the general case, the sum of a variable value
174 * and a constant value; the variable value may be absent, in which
175 * case the offset is only the constant value, and the constant value
176 * may be zero, in which case the offset is only the variable value.
178 * bpf_abs_offset is a structure containing all that information:
180 * is_variable is 1 if there's a variable part.
182 * constant_part is the constant part of the value, possibly zero;
184 * if is_variable is 1, reg is the register number for a register
185 * containing the variable value if the register has been assigned,
195 * Value passed to gen_load_a() to indicate what the offset argument
196 * is relative to the beginning of.
199 OR_PACKET
, /* full packet data */
200 OR_LINKHDR
, /* link-layer header */
201 OR_PREVLINKHDR
, /* previous link-layer header */
202 OR_LLC
, /* 802.2 LLC header */
203 OR_PREVMPLSHDR
, /* previous MPLS header */
204 OR_LINKTYPE
, /* link-layer type */
205 OR_LINKPL
, /* link-layer payload */
206 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
207 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
208 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
213 * As errors are handled by a longjmp, anything allocated must be freed
214 * in the longjmp handler, so it must be reachable from that handler.
215 * One thing that's allocated is the result of pcap_nametoaddrinfo();
216 * it must be freed with freeaddrinfo(). This variable points to any
217 * addrinfo structure that would need to be freed.
219 static struct addrinfo
*ai
;
223 * We divy out chunks of memory rather than call malloc each time so
224 * we don't have to worry about leaking memory. It's probably
225 * not a big deal if all this memory was wasted but if this ever
226 * goes into a library that would probably not be a good idea.
228 * XXX - this *is* in a library....
231 #define CHUNK0SIZE 1024
237 static struct chunk chunks
[NCHUNKS
];
238 static int cur_chunk
;
240 static void *newchunk(u_int
);
241 static void freechunks(void);
242 static inline struct block
*new_block(int);
243 static inline struct slist
*new_stmt(int);
244 static struct block
*gen_retblk(int);
245 static inline void syntax(void);
247 static void backpatch(struct block
*, struct block
*);
248 static void merge(struct block
*, struct block
*);
249 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
250 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
251 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
252 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
253 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
254 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
256 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
257 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
258 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
259 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
260 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
261 static struct slist
*gen_loadx_iphdrlen(void);
262 static struct block
*gen_uncond(int);
263 static inline struct block
*gen_true(void);
264 static inline struct block
*gen_false(void);
265 static struct block
*gen_ether_linktype(int);
266 static struct block
*gen_ipnet_linktype(int);
267 static struct block
*gen_linux_sll_linktype(int);
268 static struct slist
*gen_load_prism_llprefixlen(void);
269 static struct slist
*gen_load_avs_llprefixlen(void);
270 static struct slist
*gen_load_radiotap_llprefixlen(void);
271 static struct slist
*gen_load_ppi_llprefixlen(void);
272 static void insert_compute_vloffsets(struct block
*);
273 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
274 static int ethertype_to_ppptype(int);
275 static struct block
*gen_linktype(int);
276 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
277 static struct block
*gen_llc_linktype(int);
278 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
280 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
282 static struct block
*gen_ahostop(const u_char
*, int);
283 static struct block
*gen_ehostop(const u_char
*, int);
284 static struct block
*gen_fhostop(const u_char
*, int);
285 static struct block
*gen_thostop(const u_char
*, int);
286 static struct block
*gen_wlanhostop(const u_char
*, int);
287 static struct block
*gen_ipfchostop(const u_char
*, int);
288 static struct block
*gen_dnhostop(bpf_u_int32
, int);
289 static struct block
*gen_mpls_linktype(int);
290 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
292 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
295 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
297 static struct block
*gen_ipfrag(void);
298 static struct block
*gen_portatom(int, bpf_int32
);
299 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
300 static struct block
*gen_portatom6(int, bpf_int32
);
301 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
302 struct block
*gen_portop(int, int, int);
303 static struct block
*gen_port(int, int, int);
304 struct block
*gen_portrangeop(int, int, int, int);
305 static struct block
*gen_portrange(int, int, int, int);
306 struct block
*gen_portop6(int, int, int);
307 static struct block
*gen_port6(int, int, int);
308 struct block
*gen_portrangeop6(int, int, int, int);
309 static struct block
*gen_portrange6(int, int, int, int);
310 static int lookup_proto(const char *, int);
311 static struct block
*gen_protochain(int, int, int);
312 static struct block
*gen_proto(int, int, int);
313 static struct slist
*xfer_to_x(struct arth
*);
314 static struct slist
*xfer_to_a(struct arth
*);
315 static struct block
*gen_mac_multicast(int);
316 static struct block
*gen_len(int, int);
317 static struct block
*gen_check_802_11_data_frame(void);
318 static struct block
*gen_geneve_ll_check(void);
320 static struct block
*gen_ppi_dlt_check(void);
321 static struct block
*gen_msg_abbrev(int type
);
332 /* XXX Round up to nearest long. */
333 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
335 /* XXX Round up to structure boundary. */
339 cp
= &chunks
[cur_chunk
];
340 if (n
> cp
->n_left
) {
341 ++cp
, k
= ++cur_chunk
;
343 bpf_error("out of memory");
344 size
= CHUNK0SIZE
<< k
;
345 cp
->m
= (void *)malloc(size
);
347 bpf_error("out of memory");
348 memset((char *)cp
->m
, 0, size
);
351 bpf_error("out of memory");
354 return (void *)((char *)cp
->m
+ cp
->n_left
);
363 for (i
= 0; i
< NCHUNKS
; ++i
)
364 if (chunks
[i
].m
!= NULL
) {
371 * A strdup whose allocations are freed after code generation is over.
375 register const char *s
;
377 int n
= strlen(s
) + 1;
378 char *cp
= newchunk(n
);
384 static inline struct block
*
390 p
= (struct block
*)newchunk(sizeof(*p
));
397 static inline struct slist
*
403 p
= (struct slist
*)newchunk(sizeof(*p
));
409 static struct block
*
413 struct block
*b
= new_block(BPF_RET
|BPF_K
);
422 bpf_error("syntax error in filter expression");
425 static bpf_u_int32 netmask
;
430 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
431 const char *buf
, int optimize
, bpf_u_int32 mask
)
434 const char * volatile xbuf
= buf
;
439 * XXX - single-thread this code path with pthread calls on
440 * UN*X, if the platform supports pthreads? If that requires
441 * a separate -lpthread, we might not want to do that.
444 extern int wsockinit (void);
450 EnterCriticalSection(&g_PcapCompileCriticalSection
);
454 * If this pcap_t hasn't been activated, it doesn't have a
455 * link-layer type, so we can't use it.
458 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
459 "not-yet-activated pcap_t passed to pcap_compile");
469 if (setjmp(top_ctx
)) {
484 snaplen
= pcap_snapshot(p
);
486 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
487 "snaplen of 0 rejects all packets");
492 lex_init(xbuf
? xbuf
: "");
500 root
= gen_retblk(snaplen
);
502 if (optimize
&& !no_optimize
) {
505 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
506 bpf_error("expression rejects all packets");
508 program
->bf_insns
= icode_to_fcode(root
, &len
);
509 program
->bf_len
= len
;
514 rc
= 0; /* We're all okay */
519 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
526 * entry point for using the compiler with no pcap open
527 * pass in all the stuff that is needed explicitly instead.
530 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
531 struct bpf_program
*program
,
532 const char *buf
, int optimize
, bpf_u_int32 mask
)
537 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
540 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
546 * Clean up a "struct bpf_program" by freeing all the memory allocated
550 pcap_freecode(struct bpf_program
*program
)
553 if (program
->bf_insns
!= NULL
) {
554 free((char *)program
->bf_insns
);
555 program
->bf_insns
= NULL
;
560 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
561 * which of the jt and jf fields has been resolved and which is a pointer
562 * back to another unresolved block (or nil). At least one of the fields
563 * in each block is already resolved.
566 backpatch(list
, target
)
567 struct block
*list
, *target
;
584 * Merge the lists in b0 and b1, using the 'sense' field to indicate
585 * which of jt and jf is the link.
589 struct block
*b0
, *b1
;
591 register struct block
**p
= &b0
;
593 /* Find end of list. */
595 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
597 /* Concatenate the lists. */
605 struct block
*ppi_dlt_check
;
608 * Insert before the statements of the first (root) block any
609 * statements needed to load the lengths of any variable-length
610 * headers into registers.
612 * XXX - a fancier strategy would be to insert those before the
613 * statements of all blocks that use those lengths and that
614 * have no predecessors that use them, so that we only compute
615 * the lengths if we need them. There might be even better
616 * approaches than that.
618 * However, those strategies would be more complicated, and
619 * as we don't generate code to compute a length if the
620 * program has no tests that use the length, and as most
621 * tests will probably use those lengths, we would just
622 * postpone computing the lengths so that it's not done
623 * for tests that fail early, and it's not clear that's
626 insert_compute_vloffsets(p
->head
);
629 * For DLT_PPI captures, generate a check of the per-packet
630 * DLT value to make sure it's DLT_IEEE802_11.
632 ppi_dlt_check
= gen_ppi_dlt_check();
633 if (ppi_dlt_check
!= NULL
)
634 gen_and(ppi_dlt_check
, p
);
636 backpatch(p
, gen_retblk(snaplen
));
637 p
->sense
= !p
->sense
;
638 backpatch(p
, gen_retblk(0));
644 struct block
*b0
, *b1
;
646 backpatch(b0
, b1
->head
);
647 b0
->sense
= !b0
->sense
;
648 b1
->sense
= !b1
->sense
;
650 b1
->sense
= !b1
->sense
;
656 struct block
*b0
, *b1
;
658 b0
->sense
= !b0
->sense
;
659 backpatch(b0
, b1
->head
);
660 b0
->sense
= !b0
->sense
;
669 b
->sense
= !b
->sense
;
672 static struct block
*
673 gen_cmp(offrel
, offset
, size
, v
)
674 enum e_offrel offrel
;
678 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
681 static struct block
*
682 gen_cmp_gt(offrel
, offset
, size
, v
)
683 enum e_offrel offrel
;
687 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
690 static struct block
*
691 gen_cmp_ge(offrel
, offset
, size
, v
)
692 enum e_offrel offrel
;
696 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
699 static struct block
*
700 gen_cmp_lt(offrel
, offset
, size
, v
)
701 enum e_offrel offrel
;
705 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
708 static struct block
*
709 gen_cmp_le(offrel
, offset
, size
, v
)
710 enum e_offrel offrel
;
714 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
717 static struct block
*
718 gen_mcmp(offrel
, offset
, size
, v
, mask
)
719 enum e_offrel offrel
;
724 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
727 static struct block
*
728 gen_bcmp(offrel
, offset
, size
, v
)
729 enum e_offrel offrel
;
730 register u_int offset
, size
;
731 register const u_char
*v
;
733 register struct block
*b
, *tmp
;
737 register const u_char
*p
= &v
[size
- 4];
738 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
739 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
741 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
748 register const u_char
*p
= &v
[size
- 2];
749 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
751 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
758 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
767 * AND the field of size "size" at offset "offset" relative to the header
768 * specified by "offrel" with "mask", and compare it with the value "v"
769 * with the test specified by "jtype"; if "reverse" is true, the test
770 * should test the opposite of "jtype".
772 static struct block
*
773 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
774 enum e_offrel offrel
;
776 bpf_u_int32 offset
, size
, mask
, jtype
;
779 struct slist
*s
, *s2
;
782 s
= gen_load_a(offrel
, offset
, size
);
784 if (mask
!= 0xffffffff) {
785 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
790 b
= new_block(JMP(jtype
));
793 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
799 * Various code constructs need to know the layout of the packet.
800 * These variables give the necessary offsets from the beginning
801 * of the packet data.
805 * Absolute offset of the beginning of the link-layer header.
807 static bpf_abs_offset off_linkhdr
;
810 * If we're checking a link-layer header for a packet encapsulated in
811 * another protocol layer, this is the equivalent information for the
812 * previous layers' link-layer header from the beginning of the raw
815 static bpf_abs_offset off_prevlinkhdr
;
818 * This is the equivalent information for the outermost layers' link-layer
821 static bpf_abs_offset off_outermostlinkhdr
;
824 * "Push" the current value of the link-layer header type and link-layer
825 * header offset onto a "stack", and set a new value. (It's not a
826 * full-blown stack; we keep only the top two items.)
828 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
830 prevlinktype = new_linktype; \
831 off_prevlinkhdr = off_linkhdr; \
832 linktype = new_linktype; \
833 off_linkhdr.is_variable = new_is_variable; \
834 off_linkhdr.constant_part = new_constant_part; \
835 off_linkhdr.reg = new_reg; \
840 * Absolute offset of the beginning of the link-layer payload.
842 static bpf_abs_offset off_linkpl
;
845 * "off_linktype" is the offset to information in the link-layer header
846 * giving the packet type. This is an absolute offset from the beginning
849 * For Ethernet, it's the offset of the Ethernet type field; this
850 * means that it must have a value that skips VLAN tags.
852 * For link-layer types that always use 802.2 headers, it's the
853 * offset of the LLC header; this means that it must have a value
854 * that skips VLAN tags.
856 * For PPP, it's the offset of the PPP type field.
858 * For Cisco HDLC, it's the offset of the CHDLC type field.
860 * For BSD loopback, it's the offset of the AF_ value.
862 * For Linux cooked sockets, it's the offset of the type field.
864 * off_linktype.constant_part is set to -1 for no encapsulation,
865 * in which case, IP is assumed.
867 static bpf_abs_offset off_linktype
;
870 * TRUE if the link layer includes an ATM pseudo-header.
872 static int is_atm
= 0;
875 * TRUE if "geneve" appeared in the filter; it causes us to generate
876 * code that checks for a Geneve header and assume that later filters
877 * apply to the encapsulated payload.
879 static int is_geneve
= 0;
882 * These are offsets for the ATM pseudo-header.
884 static u_int off_vpi
;
885 static u_int off_vci
;
886 static u_int off_proto
;
889 * These are offsets for the MTP2 fields.
892 static u_int off_li_hsl
;
895 * These are offsets for the MTP3 fields.
897 static u_int off_sio
;
898 static u_int off_opc
;
899 static u_int off_dpc
;
900 static u_int off_sls
;
903 * This is the offset of the first byte after the ATM pseudo_header,
904 * or -1 if there is no ATM pseudo-header.
906 static u_int off_payload
;
909 * These are offsets to the beginning of the network-layer header.
910 * They are relative to the beginning of the link-layer payload (i.e.,
911 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
913 * If the link layer never uses 802.2 LLC:
915 * "off_nl" and "off_nl_nosnap" are the same.
917 * If the link layer always uses 802.2 LLC:
919 * "off_nl" is the offset if there's a SNAP header following
922 * "off_nl_nosnap" is the offset if there's no SNAP header.
924 * If the link layer is Ethernet:
926 * "off_nl" is the offset if the packet is an Ethernet II packet
927 * (we assume no 802.3+802.2+SNAP);
929 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
930 * with an 802.2 header following it.
933 static u_int off_nl_nosnap
;
936 static int prevlinktype
;
937 static int outermostlinktype
;
943 pcap_fddipad
= p
->fddipad
;
946 * We start out with only one link-layer header.
948 outermostlinktype
= pcap_datalink(p
);
949 off_outermostlinkhdr
.constant_part
= 0;
950 off_outermostlinkhdr
.is_variable
= 0;
951 off_outermostlinkhdr
.reg
= -1;
953 prevlinktype
= outermostlinktype
;
954 off_prevlinkhdr
.constant_part
= 0;
955 off_prevlinkhdr
.is_variable
= 0;
956 off_prevlinkhdr
.reg
= -1;
958 linktype
= outermostlinktype
;
959 off_linkhdr
.constant_part
= 0;
960 off_linkhdr
.is_variable
= 0;
961 off_linkhdr
.reg
= -1;
966 off_linkpl
.constant_part
= 0;
967 off_linkpl
.is_variable
= 0;
970 off_linktype
.constant_part
= 0;
971 off_linktype
.is_variable
= 0;
972 off_linktype
.reg
= -1;
975 * Assume it's not raw ATM with a pseudo-header, for now.
989 * And assume we're not doing SS7.
998 label_stack_depth
= 0;
999 vlan_stack_depth
= 0;
1004 off_linktype
.constant_part
= 2;
1005 off_linkpl
.constant_part
= 6;
1006 off_nl
= 0; /* XXX in reality, variable! */
1007 off_nl_nosnap
= 0; /* no 802.2 LLC */
1010 case DLT_ARCNET_LINUX
:
1011 off_linktype
.constant_part
= 4;
1012 off_linkpl
.constant_part
= 8;
1013 off_nl
= 0; /* XXX in reality, variable! */
1014 off_nl_nosnap
= 0; /* no 802.2 LLC */
1018 off_linktype
.constant_part
= 12;
1019 off_linkpl
.constant_part
= 14; /* Ethernet header length */
1020 off_nl
= 0; /* Ethernet II */
1021 off_nl_nosnap
= 3; /* 802.3+802.2 */
1026 * SLIP doesn't have a link level type. The 16 byte
1027 * header is hacked into our SLIP driver.
1029 off_linktype
.constant_part
= -1;
1030 off_linkpl
.constant_part
= 16;
1032 off_nl_nosnap
= 0; /* no 802.2 LLC */
1035 case DLT_SLIP_BSDOS
:
1036 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1037 off_linktype
.constant_part
= -1;
1039 off_linkpl
.constant_part
= 24;
1041 off_nl_nosnap
= 0; /* no 802.2 LLC */
1046 off_linktype
.constant_part
= 0;
1047 off_linkpl
.constant_part
= 4;
1049 off_nl_nosnap
= 0; /* no 802.2 LLC */
1053 off_linktype
.constant_part
= 0;
1054 off_linkpl
.constant_part
= 12;
1056 off_nl_nosnap
= 0; /* no 802.2 LLC */
1061 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1062 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1063 off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1064 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1066 off_nl_nosnap
= 0; /* no 802.2 LLC */
1071 * This does no include the Ethernet header, and
1072 * only covers session state.
1074 off_linktype
.constant_part
= 6;
1075 off_linkpl
.constant_part
= 8;
1077 off_nl_nosnap
= 0; /* no 802.2 LLC */
1081 off_linktype
.constant_part
= 5;
1082 off_linkpl
.constant_part
= 24;
1084 off_nl_nosnap
= 0; /* no 802.2 LLC */
1089 * FDDI doesn't really have a link-level type field.
1090 * We set "off_linktype" to the offset of the LLC header.
1092 * To check for Ethernet types, we assume that SSAP = SNAP
1093 * is being used and pick out the encapsulated Ethernet type.
1094 * XXX - should we generate code to check for SNAP?
1096 off_linktype
.constant_part
= 13;
1097 off_linktype
.constant_part
+= pcap_fddipad
;
1098 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1099 off_linkpl
.constant_part
+= pcap_fddipad
;
1100 off_nl
= 8; /* 802.2+SNAP */
1101 off_nl_nosnap
= 3; /* 802.2 */
1106 * Token Ring doesn't really have a link-level type field.
1107 * We set "off_linktype" to the offset of the LLC header.
1109 * To check for Ethernet types, we assume that SSAP = SNAP
1110 * is being used and pick out the encapsulated Ethernet type.
1111 * XXX - should we generate code to check for SNAP?
1113 * XXX - the header is actually variable-length.
1114 * Some various Linux patched versions gave 38
1115 * as "off_linktype" and 40 as "off_nl"; however,
1116 * if a token ring packet has *no* routing
1117 * information, i.e. is not source-routed, the correct
1118 * values are 20 and 22, as they are in the vanilla code.
1120 * A packet is source-routed iff the uppermost bit
1121 * of the first byte of the source address, at an
1122 * offset of 8, has the uppermost bit set. If the
1123 * packet is source-routed, the total number of bytes
1124 * of routing information is 2 plus bits 0x1F00 of
1125 * the 16-bit value at an offset of 14 (shifted right
1126 * 8 - figure out which byte that is).
1128 off_linktype
.constant_part
= 14;
1129 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1130 off_nl
= 8; /* 802.2+SNAP */
1131 off_nl_nosnap
= 3; /* 802.2 */
1134 case DLT_PRISM_HEADER
:
1135 case DLT_IEEE802_11_RADIO_AVS
:
1136 case DLT_IEEE802_11_RADIO
:
1137 off_linkhdr
.is_variable
= 1;
1138 /* Fall through, 802.11 doesn't have a variable link
1139 * prefix but is otherwise the same. */
1141 case DLT_IEEE802_11
:
1143 * 802.11 doesn't really have a link-level type field.
1144 * We set "off_linktype.constant_part" to the offset of
1147 * To check for Ethernet types, we assume that SSAP = SNAP
1148 * is being used and pick out the encapsulated Ethernet type.
1149 * XXX - should we generate code to check for SNAP?
1151 * We also handle variable-length radio headers here.
1152 * The Prism header is in theory variable-length, but in
1153 * practice it's always 144 bytes long. However, some
1154 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1155 * sometimes or always supply an AVS header, so we
1156 * have to check whether the radio header is a Prism
1157 * header or an AVS header, so, in practice, it's
1160 off_linktype
.constant_part
= 24;
1161 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1162 off_linkpl
.is_variable
= 1;
1163 off_nl
= 8; /* 802.2+SNAP */
1164 off_nl_nosnap
= 3; /* 802.2 */
1169 * At the moment we treat PPI the same way that we treat
1170 * normal Radiotap encoded packets. The difference is in
1171 * the function that generates the code at the beginning
1172 * to compute the header length. Since this code generator
1173 * of PPI supports bare 802.11 encapsulation only (i.e.
1174 * the encapsulated DLT should be DLT_IEEE802_11) we
1175 * generate code to check for this too.
1177 off_linktype
.constant_part
= 24;
1178 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1179 off_linkpl
.is_variable
= 1;
1180 off_linkhdr
.is_variable
= 1;
1181 off_nl
= 8; /* 802.2+SNAP */
1182 off_nl_nosnap
= 3; /* 802.2 */
1185 case DLT_ATM_RFC1483
:
1186 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1188 * assume routed, non-ISO PDUs
1189 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1191 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1192 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1193 * latter would presumably be treated the way PPPoE
1194 * should be, so you can do "pppoe and udp port 2049"
1195 * or "pppoa and tcp port 80" and have it check for
1196 * PPPo{A,E} and a PPP protocol of IP and....
1198 off_linktype
.constant_part
= 0;
1199 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1200 off_nl
= 8; /* 802.2+SNAP */
1201 off_nl_nosnap
= 3; /* 802.2 */
1206 * Full Frontal ATM; you get AALn PDUs with an ATM
1210 off_vpi
= SUNATM_VPI_POS
;
1211 off_vci
= SUNATM_VCI_POS
;
1212 off_proto
= PROTO_POS
;
1213 off_payload
= SUNATM_PKT_BEGIN_POS
;
1214 off_linktype
.constant_part
= off_payload
;
1215 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1216 off_nl
= 8; /* 802.2+SNAP */
1217 off_nl_nosnap
= 3; /* 802.2 */
1223 off_linktype
.constant_part
= -1;
1224 off_linkpl
.constant_part
= 0;
1226 off_nl_nosnap
= 0; /* no 802.2 LLC */
1229 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1230 off_linktype
.constant_part
= 14;
1231 off_linkpl
.constant_part
= 16;
1233 off_nl_nosnap
= 0; /* no 802.2 LLC */
1238 * LocalTalk does have a 1-byte type field in the LLAP header,
1239 * but really it just indicates whether there is a "short" or
1240 * "long" DDP packet following.
1242 off_linktype
.constant_part
= -1;
1243 off_linkpl
.constant_part
= 0;
1245 off_nl_nosnap
= 0; /* no 802.2 LLC */
1248 case DLT_IP_OVER_FC
:
1250 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1251 * link-level type field. We set "off_linktype" to the
1252 * offset of the LLC header.
1254 * To check for Ethernet types, we assume that SSAP = SNAP
1255 * is being used and pick out the encapsulated Ethernet type.
1256 * XXX - should we generate code to check for SNAP? RFC
1257 * 2625 says SNAP should be used.
1259 off_linktype
.constant_part
= 16;
1260 off_linkpl
.constant_part
= 16;
1261 off_nl
= 8; /* 802.2+SNAP */
1262 off_nl_nosnap
= 3; /* 802.2 */
1267 * XXX - we should set this to handle SNAP-encapsulated
1268 * frames (NLPID of 0x80).
1270 off_linktype
.constant_part
= -1;
1271 off_linkpl
.constant_part
= 0;
1273 off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 * the only BPF-interesting FRF.16 frames are non-control frames;
1278 * Frame Relay has a variable length link-layer
1279 * so lets start with offset 4 for now and increments later on (FIXME);
1282 off_linktype
.constant_part
= -1;
1283 off_linkpl
.constant_part
= 0;
1285 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1288 case DLT_APPLE_IP_OVER_IEEE1394
:
1289 off_linktype
.constant_part
= 16;
1290 off_linkpl
.constant_part
= 18;
1292 off_nl_nosnap
= 0; /* no 802.2 LLC */
1295 case DLT_SYMANTEC_FIREWALL
:
1296 off_linktype
.constant_part
= 6;
1297 off_linkpl
.constant_part
= 44;
1298 off_nl
= 0; /* Ethernet II */
1299 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1302 #ifdef HAVE_NET_PFVAR_H
1304 off_linktype
.constant_part
= 0;
1305 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1307 off_nl_nosnap
= 0; /* no 802.2 LLC */
1311 case DLT_JUNIPER_MFR
:
1312 case DLT_JUNIPER_MLFR
:
1313 case DLT_JUNIPER_MLPPP
:
1314 case DLT_JUNIPER_PPP
:
1315 case DLT_JUNIPER_CHDLC
:
1316 case DLT_JUNIPER_FRELAY
:
1317 off_linktype
.constant_part
= 4;
1318 off_linkpl
.constant_part
= 4;
1320 off_nl_nosnap
= -1; /* no 802.2 LLC */
1323 case DLT_JUNIPER_ATM1
:
1324 off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1325 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1330 case DLT_JUNIPER_ATM2
:
1331 off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1332 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1337 /* frames captured on a Juniper PPPoE service PIC
1338 * contain raw ethernet frames */
1339 case DLT_JUNIPER_PPPOE
:
1340 case DLT_JUNIPER_ETHER
:
1341 off_linkpl
.constant_part
= 14;
1342 off_linktype
.constant_part
= 16;
1343 off_nl
= 18; /* Ethernet II */
1344 off_nl_nosnap
= 21; /* 802.3+802.2 */
1347 case DLT_JUNIPER_PPPOE_ATM
:
1348 off_linktype
.constant_part
= 4;
1349 off_linkpl
.constant_part
= 6;
1351 off_nl_nosnap
= -1; /* no 802.2 LLC */
1354 case DLT_JUNIPER_GGSN
:
1355 off_linktype
.constant_part
= 6;
1356 off_linkpl
.constant_part
= 12;
1358 off_nl_nosnap
= -1; /* no 802.2 LLC */
1361 case DLT_JUNIPER_ES
:
1362 off_linktype
.constant_part
= 6;
1363 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1364 off_nl
= -1; /* not really a network layer but raw IP addresses */
1365 off_nl_nosnap
= -1; /* no 802.2 LLC */
1368 case DLT_JUNIPER_MONITOR
:
1369 off_linktype
.constant_part
= 12;
1370 off_linkpl
.constant_part
= 12;
1371 off_nl
= 0; /* raw IP/IP6 header */
1372 off_nl_nosnap
= -1; /* no 802.2 LLC */
1375 case DLT_BACNET_MS_TP
:
1376 off_linktype
.constant_part
= -1;
1377 off_linkpl
.constant_part
= -1;
1382 case DLT_JUNIPER_SERVICES
:
1383 off_linktype
.constant_part
= 12;
1384 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1385 off_nl
= -1; /* L3 proto location dep. on cookie type */
1386 off_nl_nosnap
= -1; /* no 802.2 LLC */
1389 case DLT_JUNIPER_VP
:
1390 off_linktype
.constant_part
= 18;
1391 off_linkpl
.constant_part
= -1;
1396 case DLT_JUNIPER_ST
:
1397 off_linktype
.constant_part
= 18;
1398 off_linkpl
.constant_part
= -1;
1403 case DLT_JUNIPER_ISM
:
1404 off_linktype
.constant_part
= 8;
1405 off_linkpl
.constant_part
= -1;
1410 case DLT_JUNIPER_VS
:
1411 case DLT_JUNIPER_SRX_E2E
:
1412 case DLT_JUNIPER_FIBRECHANNEL
:
1413 case DLT_JUNIPER_ATM_CEMIC
:
1414 off_linktype
.constant_part
= 8;
1415 off_linkpl
.constant_part
= -1;
1427 off_linktype
.constant_part
= -1;
1428 off_linkpl
.constant_part
= -1;
1433 case DLT_MTP2_WITH_PHDR
:
1440 off_linktype
.constant_part
= -1;
1441 off_linkpl
.constant_part
= -1;
1453 off_linktype
.constant_part
= -1;
1454 off_linkpl
.constant_part
= -1;
1460 off_linktype
.constant_part
= -1;
1461 off_linkpl
.constant_part
= 4;
1468 * Currently, only raw "link[N:M]" filtering is supported.
1470 off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1471 off_linkpl
.constant_part
= -1;
1472 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1473 off_nl_nosnap
= -1; /* no 802.2 LLC */
1477 off_linktype
.constant_part
= 1;
1478 off_linkpl
.constant_part
= 24; /* ipnet header length */
1483 case DLT_NETANALYZER
:
1484 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1485 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1486 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1487 off_nl
= 0; /* Ethernet II */
1488 off_nl_nosnap
= 3; /* 802.3+802.2 */
1491 case DLT_NETANALYZER_TRANSPARENT
:
1492 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1493 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1494 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1495 off_nl
= 0; /* Ethernet II */
1496 off_nl_nosnap
= 3; /* 802.3+802.2 */
1501 * For values in the range in which we've assigned new
1502 * DLT_ values, only raw "link[N:M]" filtering is supported.
1504 if (linktype
>= DLT_MATCHING_MIN
&&
1505 linktype
<= DLT_MATCHING_MAX
) {
1506 off_linktype
.constant_part
= -1;
1507 off_linkpl
.constant_part
= -1;
1511 bpf_error("unknown data link type %d", linktype
);
1516 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1520 * Load a value relative to the specified absolute offset.
1522 static struct slist
*
1523 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1525 struct slist
*s
, *s2
;
1527 s
= gen_abs_offset_varpart(abs_offset
);
1530 * If "s" is non-null, it has code to arrange that the X register
1531 * contains the variable part of the absolute offset, so we
1532 * generate a load relative to that, with an offset of
1533 * abs_offset->constant_part + offset.
1535 * Otherwise, we can do an absolute load with an offset of
1536 * abs_offset->constant_part + offset.
1540 * "s" points to a list of statements that puts the
1541 * variable part of the absolute offset into the X register.
1542 * Do an indirect load, to use the X register as an offset.
1544 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1545 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1549 * There is no variable part of the absolute offset, so
1550 * just do an absolute load.
1552 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1553 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1559 * Load a value relative to the beginning of the specified header.
1561 static struct slist
*
1562 gen_load_a(offrel
, offset
, size
)
1563 enum e_offrel offrel
;
1566 struct slist
*s
, *s2
;
1571 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1576 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1579 case OR_PREVLINKHDR
:
1580 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1584 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1587 case OR_PREVMPLSHDR
:
1588 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1592 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1595 case OR_LINKPL_NOSNAP
:
1596 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1600 s
= gen_load_absoffsetrel(&off_linktype
, offset
, size
);
1605 * Load the X register with the length of the IPv4 header
1606 * (plus the offset of the link-layer header, if it's
1607 * preceded by a variable-length header such as a radio
1608 * header), in bytes.
1610 s
= gen_loadx_iphdrlen();
1613 * Load the item at {offset of the link-layer payload} +
1614 * {offset, relative to the start of the link-layer
1615 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1616 * {specified offset}.
1618 * If the offset of the link-layer payload is variable,
1619 * the variable part of that offset is included in the
1620 * value in the X register, and we include the constant
1621 * part in the offset of the load.
1623 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1624 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1629 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1640 * Generate code to load into the X register the sum of the length of
1641 * the IPv4 header and the variable part of the offset of the link-layer
1644 static struct slist
*
1645 gen_loadx_iphdrlen()
1647 struct slist
*s
, *s2
;
1649 s
= gen_abs_offset_varpart(&off_linkpl
);
1652 * The offset of the link-layer payload has a variable
1653 * part. "s" points to a list of statements that put
1654 * the variable part of that offset into the X register.
1656 * The 4*([k]&0xf) addressing mode can't be used, as we
1657 * don't have a constant offset, so we have to load the
1658 * value in question into the A register and add to it
1659 * the value from the X register.
1661 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1662 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1664 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1667 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1672 * The A register now contains the length of the IP header.
1673 * We need to add to it the variable part of the offset of
1674 * the link-layer payload, which is still in the X
1675 * register, and move the result into the X register.
1677 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1678 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1681 * The offset of the link-layer payload is a constant,
1682 * so no code was generated to load the (non-existent)
1683 * variable part of that offset.
1685 * This means we can use the 4*([k]&0xf) addressing
1686 * mode. Load the length of the IPv4 header, which
1687 * is at an offset of off_nl from the beginning of
1688 * the link-layer payload, and thus at an offset of
1689 * off_linkpl.constant_part + off_nl from the beginning
1690 * of the raw packet data, using that addressing mode.
1692 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1693 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1698 static struct block
*
1705 s
= new_stmt(BPF_LD
|BPF_IMM
);
1707 b
= new_block(JMP(BPF_JEQ
));
1713 static inline struct block
*
1716 return gen_uncond(1);
1719 static inline struct block
*
1722 return gen_uncond(0);
1726 * Byte-swap a 32-bit number.
1727 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1728 * big-endian platforms.)
1730 #define SWAPLONG(y) \
1731 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1734 * Generate code to match a particular packet type.
1736 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1737 * value, if <= ETHERMTU. We use that to determine whether to
1738 * match the type/length field or to check the type/length field for
1739 * a value <= ETHERMTU to see whether it's a type field and then do
1740 * the appropriate test.
1742 static struct block
*
1743 gen_ether_linktype(proto
)
1746 struct block
*b0
, *b1
;
1752 case LLCSAP_NETBEUI
:
1754 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1755 * so we check the DSAP and SSAP.
1757 * LLCSAP_IP checks for IP-over-802.2, rather
1758 * than IP-over-Ethernet or IP-over-SNAP.
1760 * XXX - should we check both the DSAP and the
1761 * SSAP, like this, or should we check just the
1762 * DSAP, as we do for other types <= ETHERMTU
1763 * (i.e., other SAP values)?
1765 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1767 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1768 ((proto
<< 8) | proto
));
1776 * Ethernet_II frames, which are Ethernet
1777 * frames with a frame type of ETHERTYPE_IPX;
1779 * Ethernet_802.3 frames, which are 802.3
1780 * frames (i.e., the type/length field is
1781 * a length field, <= ETHERMTU, rather than
1782 * a type field) with the first two bytes
1783 * after the Ethernet/802.3 header being
1786 * Ethernet_802.2 frames, which are 802.3
1787 * frames with an 802.2 LLC header and
1788 * with the IPX LSAP as the DSAP in the LLC
1791 * Ethernet_SNAP frames, which are 802.3
1792 * frames with an LLC header and a SNAP
1793 * header and with an OUI of 0x000000
1794 * (encapsulated Ethernet) and a protocol
1795 * ID of ETHERTYPE_IPX in the SNAP header.
1797 * XXX - should we generate the same code both
1798 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1802 * This generates code to check both for the
1803 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1805 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1806 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1810 * Now we add code to check for SNAP frames with
1811 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1813 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1817 * Now we generate code to check for 802.3
1818 * frames in general.
1820 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1824 * Now add the check for 802.3 frames before the
1825 * check for Ethernet_802.2 and Ethernet_802.3,
1826 * as those checks should only be done on 802.3
1827 * frames, not on Ethernet frames.
1832 * Now add the check for Ethernet_II frames, and
1833 * do that before checking for the other frame
1836 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1840 case ETHERTYPE_ATALK
:
1841 case ETHERTYPE_AARP
:
1843 * EtherTalk (AppleTalk protocols on Ethernet link
1844 * layer) may use 802.2 encapsulation.
1848 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1849 * we check for an Ethernet type field less than
1850 * 1500, which means it's an 802.3 length field.
1852 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1856 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1857 * SNAP packets with an organization code of
1858 * 0x080007 (Apple, for Appletalk) and a protocol
1859 * type of ETHERTYPE_ATALK (Appletalk).
1861 * 802.2-encapsulated ETHERTYPE_AARP packets are
1862 * SNAP packets with an organization code of
1863 * 0x000000 (encapsulated Ethernet) and a protocol
1864 * type of ETHERTYPE_AARP (Appletalk ARP).
1866 if (proto
== ETHERTYPE_ATALK
)
1867 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1868 else /* proto == ETHERTYPE_AARP */
1869 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1873 * Check for Ethernet encapsulation (Ethertalk
1874 * phase 1?); we just check for the Ethernet
1877 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1883 if (proto
<= ETHERMTU
) {
1885 * This is an LLC SAP value, so the frames
1886 * that match would be 802.2 frames.
1887 * Check that the frame is an 802.2 frame
1888 * (i.e., that the length/type field is
1889 * a length field, <= ETHERMTU) and
1890 * then check the DSAP.
1892 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1894 b1
= gen_cmp(OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1899 * This is an Ethernet type, so compare
1900 * the length/type field with it (if
1901 * the frame is an 802.2 frame, the length
1902 * field will be <= ETHERMTU, and, as
1903 * "proto" is > ETHERMTU, this test
1904 * will fail and the frame won't match,
1905 * which is what we want).
1907 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
1914 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1915 * or IPv6 then we have an error.
1917 static struct block
*
1918 gen_ipnet_linktype(proto
)
1924 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
1927 case ETHERTYPE_IPV6
:
1928 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
1929 (bpf_int32
)IPH_AF_INET6
);
1940 * Generate code to match a particular packet type.
1942 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1943 * value, if <= ETHERMTU. We use that to determine whether to
1944 * match the type field or to check the type field for the special
1945 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1947 static struct block
*
1948 gen_linux_sll_linktype(proto
)
1951 struct block
*b0
, *b1
;
1957 case LLCSAP_NETBEUI
:
1959 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1960 * so we check the DSAP and SSAP.
1962 * LLCSAP_IP checks for IP-over-802.2, rather
1963 * than IP-over-Ethernet or IP-over-SNAP.
1965 * XXX - should we check both the DSAP and the
1966 * SSAP, like this, or should we check just the
1967 * DSAP, as we do for other types <= ETHERMTU
1968 * (i.e., other SAP values)?
1970 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
1971 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1972 ((proto
<< 8) | proto
));
1978 * Ethernet_II frames, which are Ethernet
1979 * frames with a frame type of ETHERTYPE_IPX;
1981 * Ethernet_802.3 frames, which have a frame
1982 * type of LINUX_SLL_P_802_3;
1984 * Ethernet_802.2 frames, which are 802.3
1985 * frames with an 802.2 LLC header (i.e, have
1986 * a frame type of LINUX_SLL_P_802_2) and
1987 * with the IPX LSAP as the DSAP in the LLC
1990 * Ethernet_SNAP frames, which are 802.3
1991 * frames with an LLC header and a SNAP
1992 * header and with an OUI of 0x000000
1993 * (encapsulated Ethernet) and a protocol
1994 * ID of ETHERTYPE_IPX in the SNAP header.
1996 * First, do the checks on LINUX_SLL_P_802_2
1997 * frames; generate the check for either
1998 * Ethernet_802.2 or Ethernet_SNAP frames, and
1999 * then put a check for LINUX_SLL_P_802_2 frames
2002 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2003 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2005 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2009 * Now check for 802.3 frames and OR that with
2010 * the previous test.
2012 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2016 * Now add the check for Ethernet_II frames, and
2017 * do that before checking for the other frame
2020 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2024 case ETHERTYPE_ATALK
:
2025 case ETHERTYPE_AARP
:
2027 * EtherTalk (AppleTalk protocols on Ethernet link
2028 * layer) may use 802.2 encapsulation.
2032 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2033 * we check for the 802.2 protocol type in the
2034 * "Ethernet type" field.
2036 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2039 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2040 * SNAP packets with an organization code of
2041 * 0x080007 (Apple, for Appletalk) and a protocol
2042 * type of ETHERTYPE_ATALK (Appletalk).
2044 * 802.2-encapsulated ETHERTYPE_AARP packets are
2045 * SNAP packets with an organization code of
2046 * 0x000000 (encapsulated Ethernet) and a protocol
2047 * type of ETHERTYPE_AARP (Appletalk ARP).
2049 if (proto
== ETHERTYPE_ATALK
)
2050 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2051 else /* proto == ETHERTYPE_AARP */
2052 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2056 * Check for Ethernet encapsulation (Ethertalk
2057 * phase 1?); we just check for the Ethernet
2060 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2066 if (proto
<= ETHERMTU
) {
2068 * This is an LLC SAP value, so the frames
2069 * that match would be 802.2 frames.
2070 * Check for the 802.2 protocol type
2071 * in the "Ethernet type" field, and
2072 * then check the DSAP.
2074 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2075 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2081 * This is an Ethernet type, so compare
2082 * the length/type field with it (if
2083 * the frame is an 802.2 frame, the length
2084 * field will be <= ETHERMTU, and, as
2085 * "proto" is > ETHERMTU, this test
2086 * will fail and the frame won't match,
2087 * which is what we want).
2089 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2094 static struct slist
*
2095 gen_load_prism_llprefixlen()
2097 struct slist
*s1
, *s2
;
2098 struct slist
*sjeq_avs_cookie
;
2099 struct slist
*sjcommon
;
2102 * This code is not compatible with the optimizer, as
2103 * we are generating jmp instructions within a normal
2104 * slist of instructions
2109 * Generate code to load the length of the radio header into
2110 * the register assigned to hold that length, if one has been
2111 * assigned. (If one hasn't been assigned, no code we've
2112 * generated uses that prefix, so we don't need to generate any
2115 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2116 * or always use the AVS header rather than the Prism header.
2117 * We load a 4-byte big-endian value at the beginning of the
2118 * raw packet data, and see whether, when masked with 0xFFFFF000,
2119 * it's equal to 0x80211000. If so, that indicates that it's
2120 * an AVS header (the masked-out bits are the version number).
2121 * Otherwise, it's a Prism header.
2123 * XXX - the Prism header is also, in theory, variable-length,
2124 * but no known software generates headers that aren't 144
2127 if (off_linkhdr
.reg
!= -1) {
2131 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2135 * AND it with 0xFFFFF000.
2137 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2138 s2
->s
.k
= 0xFFFFF000;
2142 * Compare with 0x80211000.
2144 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2145 sjeq_avs_cookie
->s
.k
= 0x80211000;
2146 sappend(s1
, sjeq_avs_cookie
);
2151 * The 4 bytes at an offset of 4 from the beginning of
2152 * the AVS header are the length of the AVS header.
2153 * That field is big-endian.
2155 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2158 sjeq_avs_cookie
->s
.jt
= s2
;
2161 * Now jump to the code to allocate a register
2162 * into which to save the header length and
2163 * store the length there. (The "jump always"
2164 * instruction needs to have the k field set;
2165 * it's added to the PC, so, as we're jumping
2166 * over a single instruction, it should be 1.)
2168 sjcommon
= new_stmt(JMP(BPF_JA
));
2170 sappend(s1
, sjcommon
);
2173 * Now for the code that handles the Prism header.
2174 * Just load the length of the Prism header (144)
2175 * into the A register. Have the test for an AVS
2176 * header branch here if we don't have an AVS header.
2178 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2181 sjeq_avs_cookie
->s
.jf
= s2
;
2184 * Now allocate a register to hold that value and store
2185 * it. The code for the AVS header will jump here after
2186 * loading the length of the AVS header.
2188 s2
= new_stmt(BPF_ST
);
2189 s2
->s
.k
= off_linkhdr
.reg
;
2191 sjcommon
->s
.jf
= s2
;
2194 * Now move it into the X register.
2196 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2204 static struct slist
*
2205 gen_load_avs_llprefixlen()
2207 struct slist
*s1
, *s2
;
2210 * Generate code to load the length of the AVS header into
2211 * the register assigned to hold that length, if one has been
2212 * assigned. (If one hasn't been assigned, no code we've
2213 * generated uses that prefix, so we don't need to generate any
2216 if (off_linkhdr
.reg
!= -1) {
2218 * The 4 bytes at an offset of 4 from the beginning of
2219 * the AVS header are the length of the AVS header.
2220 * That field is big-endian.
2222 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2226 * Now allocate a register to hold that value and store
2229 s2
= new_stmt(BPF_ST
);
2230 s2
->s
.k
= off_linkhdr
.reg
;
2234 * Now move it into the X register.
2236 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2244 static struct slist
*
2245 gen_load_radiotap_llprefixlen()
2247 struct slist
*s1
, *s2
;
2250 * Generate code to load the length of the radiotap header into
2251 * the register assigned to hold that length, if one has been
2252 * assigned. (If one hasn't been assigned, no code we've
2253 * generated uses that prefix, so we don't need to generate any
2256 if (off_linkhdr
.reg
!= -1) {
2258 * The 2 bytes at offsets of 2 and 3 from the beginning
2259 * of the radiotap header are the length of the radiotap
2260 * header; unfortunately, it's little-endian, so we have
2261 * to load it a byte at a time and construct the value.
2265 * Load the high-order byte, at an offset of 3, shift it
2266 * left a byte, and put the result in the X register.
2268 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2270 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2273 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2277 * Load the next byte, at an offset of 2, and OR the
2278 * value from the X register into it.
2280 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2283 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2287 * Now allocate a register to hold that value and store
2290 s2
= new_stmt(BPF_ST
);
2291 s2
->s
.k
= off_linkhdr
.reg
;
2295 * Now move it into the X register.
2297 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2306 * At the moment we treat PPI as normal Radiotap encoded
2307 * packets. The difference is in the function that generates
2308 * the code at the beginning to compute the header length.
2309 * Since this code generator of PPI supports bare 802.11
2310 * encapsulation only (i.e. the encapsulated DLT should be
2311 * DLT_IEEE802_11) we generate code to check for this too;
2312 * that's done in finish_parse().
2314 static struct slist
*
2315 gen_load_ppi_llprefixlen()
2317 struct slist
*s1
, *s2
;
2320 * Generate code to load the length of the radiotap header
2321 * into the register assigned to hold that length, if one has
2324 if (off_linkhdr
.reg
!= -1) {
2326 * The 2 bytes at offsets of 2 and 3 from the beginning
2327 * of the radiotap header are the length of the radiotap
2328 * header; unfortunately, it's little-endian, so we have
2329 * to load it a byte at a time and construct the value.
2333 * Load the high-order byte, at an offset of 3, shift it
2334 * left a byte, and put the result in the X register.
2336 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2338 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2341 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2345 * Load the next byte, at an offset of 2, and OR the
2346 * value from the X register into it.
2348 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2351 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2355 * Now allocate a register to hold that value and store
2358 s2
= new_stmt(BPF_ST
);
2359 s2
->s
.k
= off_linkhdr
.reg
;
2363 * Now move it into the X register.
2365 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2374 * Load a value relative to the beginning of the link-layer header after the 802.11
2375 * header, i.e. LLC_SNAP.
2376 * The link-layer header doesn't necessarily begin at the beginning
2377 * of the packet data; there might be a variable-length prefix containing
2378 * radio information.
2380 static struct slist
*
2381 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2384 struct slist
*sjset_data_frame_1
;
2385 struct slist
*sjset_data_frame_2
;
2386 struct slist
*sjset_qos
;
2387 struct slist
*sjset_radiotap_flags
;
2388 struct slist
*sjset_radiotap_tsft
;
2389 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2390 struct slist
*s_roundup
;
2392 if (off_linkpl
.reg
== -1) {
2394 * No register has been assigned to the offset of
2395 * the link-layer payload, which means nobody needs
2396 * it; don't bother computing it - just return
2397 * what we already have.
2403 * This code is not compatible with the optimizer, as
2404 * we are generating jmp instructions within a normal
2405 * slist of instructions
2410 * If "s" is non-null, it has code to arrange that the X register
2411 * contains the length of the prefix preceding the link-layer
2414 * Otherwise, the length of the prefix preceding the link-layer
2415 * header is "off_outermostlinkhdr.constant_part".
2419 * There is no variable-length header preceding the
2420 * link-layer header.
2422 * Load the length of the fixed-length prefix preceding
2423 * the link-layer header (if any) into the X register,
2424 * and store it in the off_linkpl.reg register.
2425 * That length is off_outermostlinkhdr.constant_part.
2427 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2428 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2432 * The X register contains the offset of the beginning of the
2433 * link-layer header; add 24, which is the minimum length
2434 * of the MAC header for a data frame, to that, and store it
2435 * in off_linkpl.reg, and then load the Frame Control field,
2436 * which is at the offset in the X register, with an indexed load.
2438 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2440 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2443 s2
= new_stmt(BPF_ST
);
2444 s2
->s
.k
= off_linkpl
.reg
;
2447 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2452 * Check the Frame Control field to see if this is a data frame;
2453 * a data frame has the 0x08 bit (b3) in that field set and the
2454 * 0x04 bit (b2) clear.
2456 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2457 sjset_data_frame_1
->s
.k
= 0x08;
2458 sappend(s
, sjset_data_frame_1
);
2461 * If b3 is set, test b2, otherwise go to the first statement of
2462 * the rest of the program.
2464 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2465 sjset_data_frame_2
->s
.k
= 0x04;
2466 sappend(s
, sjset_data_frame_2
);
2467 sjset_data_frame_1
->s
.jf
= snext
;
2470 * If b2 is not set, this is a data frame; test the QoS bit.
2471 * Otherwise, go to the first statement of the rest of the
2474 sjset_data_frame_2
->s
.jt
= snext
;
2475 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2476 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2477 sappend(s
, sjset_qos
);
2480 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2482 * Otherwise, go to the first statement of the rest of the
2485 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2486 s2
->s
.k
= off_linkpl
.reg
;
2488 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2491 s2
= new_stmt(BPF_ST
);
2492 s2
->s
.k
= off_linkpl
.reg
;
2496 * If we have a radiotap header, look at it to see whether
2497 * there's Atheros padding between the MAC-layer header
2500 * Note: all of the fields in the radiotap header are
2501 * little-endian, so we byte-swap all of the values
2502 * we test against, as they will be loaded as big-endian
2505 if (linktype
== DLT_IEEE802_11_RADIO
) {
2507 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2508 * in the presence flag?
2510 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2514 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2515 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2516 sappend(s
, sjset_radiotap_flags
);
2519 * If not, skip all of this.
2521 sjset_radiotap_flags
->s
.jf
= snext
;
2524 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2526 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2527 new_stmt(JMP(BPF_JSET
));
2528 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2529 sappend(s
, sjset_radiotap_tsft
);
2532 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2533 * at an offset of 16 from the beginning of the raw packet
2534 * data (8 bytes for the radiotap header and 8 bytes for
2537 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2540 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2544 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2545 sjset_tsft_datapad
->s
.k
= 0x20;
2546 sappend(s
, sjset_tsft_datapad
);
2549 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2550 * at an offset of 8 from the beginning of the raw packet
2551 * data (8 bytes for the radiotap header).
2553 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2556 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2560 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2561 sjset_notsft_datapad
->s
.k
= 0x20;
2562 sappend(s
, sjset_notsft_datapad
);
2565 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2566 * set, round the length of the 802.11 header to
2567 * a multiple of 4. Do that by adding 3 and then
2568 * dividing by and multiplying by 4, which we do by
2571 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2572 s_roundup
->s
.k
= off_linkpl
.reg
;
2573 sappend(s
, s_roundup
);
2574 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2577 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2580 s2
= new_stmt(BPF_ST
);
2581 s2
->s
.k
= off_linkpl
.reg
;
2584 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2585 sjset_tsft_datapad
->s
.jf
= snext
;
2586 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2587 sjset_notsft_datapad
->s
.jf
= snext
;
2589 sjset_qos
->s
.jf
= snext
;
2595 insert_compute_vloffsets(b
)
2600 /* There is an implicit dependency between the link
2601 * payload and link header since the payload computation
2602 * includes the variable part of the header. Therefore,
2603 * if nobody else has allocated a register for the link
2604 * header and we need it, do it now. */
2605 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2606 off_linkhdr
.reg
== -1)
2607 off_linkhdr
.reg
= alloc_reg();
2610 * For link-layer types that have a variable-length header
2611 * preceding the link-layer header, generate code to load
2612 * the offset of the link-layer header into the register
2613 * assigned to that offset, if any.
2615 * XXX - this, and the next switch statement, won't handle
2616 * encapsulation of 802.11 or 802.11+radio information in
2617 * some other protocol stack. That's significantly more
2620 switch (outermostlinktype
) {
2622 case DLT_PRISM_HEADER
:
2623 s
= gen_load_prism_llprefixlen();
2626 case DLT_IEEE802_11_RADIO_AVS
:
2627 s
= gen_load_avs_llprefixlen();
2630 case DLT_IEEE802_11_RADIO
:
2631 s
= gen_load_radiotap_llprefixlen();
2635 s
= gen_load_ppi_llprefixlen();
2644 * For link-layer types that have a variable-length link-layer
2645 * header, generate code to load the offset of the link-layer
2646 * payload into the register assigned to that offset, if any.
2648 switch (outermostlinktype
) {
2650 case DLT_IEEE802_11
:
2651 case DLT_PRISM_HEADER
:
2652 case DLT_IEEE802_11_RADIO_AVS
:
2653 case DLT_IEEE802_11_RADIO
:
2655 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2660 * If we have any offset-loading code, append all the
2661 * existing statements in the block to those statements,
2662 * and make the resulting list the list of statements
2666 sappend(s
, b
->stmts
);
2671 static struct block
*
2672 gen_ppi_dlt_check(void)
2674 struct slist
*s_load_dlt
;
2677 if (linktype
== DLT_PPI
)
2679 /* Create the statements that check for the DLT
2681 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2682 s_load_dlt
->s
.k
= 4;
2684 b
= new_block(JMP(BPF_JEQ
));
2686 b
->stmts
= s_load_dlt
;
2687 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2698 * Take an absolute offset, and:
2700 * if it has no variable part, return NULL;
2702 * if it has a variable part, generate code to load the register
2703 * containing that variable part into the X register, returning
2704 * a pointer to that code - if no register for that offset has
2705 * been allocated, allocate it first.
2707 * (The code to set that register will be generated later, but will
2708 * be placed earlier in the code sequence.)
2710 static struct slist
*
2711 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2715 if (off
->is_variable
) {
2716 if (off
->reg
== -1) {
2718 * We haven't yet assigned a register for the
2719 * variable part of the offset of the link-layer
2720 * header; allocate one.
2722 off
->reg
= alloc_reg();
2726 * Load the register containing the variable part of the
2727 * offset of the link-layer header into the X register.
2729 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2734 * That offset isn't variable, there's no variable part,
2735 * so we don't need to generate any code.
2742 * Map an Ethernet type to the equivalent PPP type.
2745 ethertype_to_ppptype(proto
)
2754 case ETHERTYPE_IPV6
:
2762 case ETHERTYPE_ATALK
:
2776 * I'm assuming the "Bridging PDU"s that go
2777 * over PPP are Spanning Tree Protocol
2791 * Generate any tests that, for encapsulation of a link-layer packet
2792 * inside another protocol stack, need to be done to check for those
2793 * link-layer packets (and that haven't already been done by a check
2794 * for that encapsulation).
2796 static struct block
*
2797 gen_prevlinkhdr_check(void)
2802 return gen_geneve_ll_check();
2804 switch (prevlinktype
) {
2808 * This is LANE-encapsulated Ethernet; check that the LANE
2809 * packet doesn't begin with an LE Control marker, i.e.
2810 * that it's data, not a control message.
2812 * (We've already generated a test for LANE.)
2814 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2820 * No such tests are necessary.
2828 * Generate code to match a particular packet type by matching the
2829 * link-layer type field or fields in the 802.2 LLC header.
2831 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2832 * value, if <= ETHERMTU.
2834 static struct block
*
2838 struct block
*b0
, *b1
, *b2
;
2839 const char *description
;
2841 /* are we checking MPLS-encapsulated packets? */
2842 if (label_stack_depth
> 0) {
2846 /* FIXME add other L3 proto IDs */
2847 return gen_mpls_linktype(Q_IP
);
2849 case ETHERTYPE_IPV6
:
2851 /* FIXME add other L3 proto IDs */
2852 return gen_mpls_linktype(Q_IPV6
);
2855 bpf_error("unsupported protocol over mpls");
2863 case DLT_NETANALYZER
:
2864 case DLT_NETANALYZER_TRANSPARENT
:
2865 /* Geneve has an EtherType regardless of whether there is an
2868 b0
= gen_prevlinkhdr_check();
2872 b1
= gen_ether_linktype(proto
);
2883 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2887 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2893 case DLT_IEEE802_11
:
2894 case DLT_PRISM_HEADER
:
2895 case DLT_IEEE802_11_RADIO_AVS
:
2896 case DLT_IEEE802_11_RADIO
:
2899 * Check that we have a data frame.
2901 b0
= gen_check_802_11_data_frame();
2904 * Now check for the specified link-layer type.
2906 b1
= gen_llc_linktype(proto
);
2914 * XXX - check for LLC frames.
2916 return gen_llc_linktype(proto
);
2922 * XXX - check for LLC PDUs, as per IEEE 802.5.
2924 return gen_llc_linktype(proto
);
2928 case DLT_ATM_RFC1483
:
2930 case DLT_IP_OVER_FC
:
2931 return gen_llc_linktype(proto
);
2937 * Check for an LLC-encapsulated version of this protocol;
2938 * if we were checking for LANE, linktype would no longer
2941 * Check for LLC encapsulation and then check the protocol.
2943 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2944 b1
= gen_llc_linktype(proto
);
2951 return gen_linux_sll_linktype(proto
);
2956 case DLT_SLIP_BSDOS
:
2959 * These types don't provide any type field; packets
2960 * are always IPv4 or IPv6.
2962 * XXX - for IPv4, check for a version number of 4, and,
2963 * for IPv6, check for a version number of 6?
2968 /* Check for a version number of 4. */
2969 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
2971 case ETHERTYPE_IPV6
:
2972 /* Check for a version number of 6. */
2973 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
2976 return gen_false(); /* always false */
2983 * Raw IPv4, so no type field.
2985 if (proto
== ETHERTYPE_IP
)
2986 return gen_true(); /* always true */
2988 /* Checking for something other than IPv4; always false */
2995 * Raw IPv6, so no type field.
2997 if (proto
== ETHERTYPE_IPV6
)
2998 return gen_true(); /* always true */
3000 /* Checking for something other than IPv6; always false */
3007 case DLT_PPP_SERIAL
:
3010 * We use Ethernet protocol types inside libpcap;
3011 * map them to the corresponding PPP protocol types.
3013 proto
= ethertype_to_ppptype(proto
);
3014 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3020 * We use Ethernet protocol types inside libpcap;
3021 * map them to the corresponding PPP protocol types.
3027 * Also check for Van Jacobson-compressed IP.
3028 * XXX - do this for other forms of PPP?
3030 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3031 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3033 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3038 proto
= ethertype_to_ppptype(proto
);
3039 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3049 * For DLT_NULL, the link-layer header is a 32-bit
3050 * word containing an AF_ value in *host* byte order,
3051 * and for DLT_ENC, the link-layer header begins
3052 * with a 32-bit work containing an AF_ value in
3055 * In addition, if we're reading a saved capture file,
3056 * the host byte order in the capture may not be the
3057 * same as the host byte order on this machine.
3059 * For DLT_LOOP, the link-layer header is a 32-bit
3060 * word containing an AF_ value in *network* byte order.
3062 * XXX - AF_ values may, unfortunately, be platform-
3063 * dependent; for example, FreeBSD's AF_INET6 is 24
3064 * whilst NetBSD's and OpenBSD's is 26.
3066 * This means that, when reading a capture file, just
3067 * checking for our AF_INET6 value won't work if the
3068 * capture file came from another OS.
3077 case ETHERTYPE_IPV6
:
3084 * Not a type on which we support filtering.
3085 * XXX - support those that have AF_ values
3086 * #defined on this platform, at least?
3091 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3093 * The AF_ value is in host byte order, but
3094 * the BPF interpreter will convert it to
3095 * network byte order.
3097 * If this is a save file, and it's from a
3098 * machine with the opposite byte order to
3099 * ours, we byte-swap the AF_ value.
3101 * Then we run it through "htonl()", and
3102 * generate code to compare against the result.
3104 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3105 proto
= SWAPLONG(proto
);
3106 proto
= htonl(proto
);
3108 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3110 #ifdef HAVE_NET_PFVAR_H
3113 * af field is host byte order in contrast to the rest of
3116 if (proto
== ETHERTYPE_IP
)
3117 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3118 BPF_B
, (bpf_int32
)AF_INET
));
3119 else if (proto
== ETHERTYPE_IPV6
)
3120 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3121 BPF_B
, (bpf_int32
)AF_INET6
));
3126 #endif /* HAVE_NET_PFVAR_H */
3129 case DLT_ARCNET_LINUX
:
3131 * XXX should we check for first fragment if the protocol
3139 case ETHERTYPE_IPV6
:
3140 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3141 (bpf_int32
)ARCTYPE_INET6
));
3144 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3145 (bpf_int32
)ARCTYPE_IP
);
3146 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3147 (bpf_int32
)ARCTYPE_IP_OLD
);
3152 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3153 (bpf_int32
)ARCTYPE_ARP
);
3154 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3155 (bpf_int32
)ARCTYPE_ARP_OLD
);
3159 case ETHERTYPE_REVARP
:
3160 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3161 (bpf_int32
)ARCTYPE_REVARP
));
3163 case ETHERTYPE_ATALK
:
3164 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3165 (bpf_int32
)ARCTYPE_ATALK
));
3172 case ETHERTYPE_ATALK
:
3182 * XXX - assumes a 2-byte Frame Relay header with
3183 * DLCI and flags. What if the address is longer?
3189 * Check for the special NLPID for IP.
3191 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3193 case ETHERTYPE_IPV6
:
3195 * Check for the special NLPID for IPv6.
3197 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3201 * Check for several OSI protocols.
3203 * Frame Relay packets typically have an OSI
3204 * NLPID at the beginning; we check for each
3207 * What we check for is the NLPID and a frame
3208 * control field of UI, i.e. 0x03 followed
3211 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3212 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3213 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3225 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3227 case DLT_JUNIPER_MFR
:
3228 case DLT_JUNIPER_MLFR
:
3229 case DLT_JUNIPER_MLPPP
:
3230 case DLT_JUNIPER_ATM1
:
3231 case DLT_JUNIPER_ATM2
:
3232 case DLT_JUNIPER_PPPOE
:
3233 case DLT_JUNIPER_PPPOE_ATM
:
3234 case DLT_JUNIPER_GGSN
:
3235 case DLT_JUNIPER_ES
:
3236 case DLT_JUNIPER_MONITOR
:
3237 case DLT_JUNIPER_SERVICES
:
3238 case DLT_JUNIPER_ETHER
:
3239 case DLT_JUNIPER_PPP
:
3240 case DLT_JUNIPER_FRELAY
:
3241 case DLT_JUNIPER_CHDLC
:
3242 case DLT_JUNIPER_VP
:
3243 case DLT_JUNIPER_ST
:
3244 case DLT_JUNIPER_ISM
:
3245 case DLT_JUNIPER_VS
:
3246 case DLT_JUNIPER_SRX_E2E
:
3247 case DLT_JUNIPER_FIBRECHANNEL
:
3248 case DLT_JUNIPER_ATM_CEMIC
:
3250 /* just lets verify the magic number for now -
3251 * on ATM we may have up to 6 different encapsulations on the wire
3252 * and need a lot of heuristics to figure out that the payload
3255 * FIXME encapsulation specific BPF_ filters
3257 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3259 case DLT_BACNET_MS_TP
:
3260 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3263 return gen_ipnet_linktype(proto
);
3265 case DLT_LINUX_IRDA
:
3266 bpf_error("IrDA link-layer type filtering not implemented");
3269 bpf_error("DOCSIS link-layer type filtering not implemented");
3272 case DLT_MTP2_WITH_PHDR
:
3273 bpf_error("MTP2 link-layer type filtering not implemented");
3276 bpf_error("ERF link-layer type filtering not implemented");
3279 bpf_error("PFSYNC link-layer type filtering not implemented");
3281 case DLT_LINUX_LAPD
:
3282 bpf_error("LAPD link-layer type filtering not implemented");
3286 case DLT_USB_LINUX_MMAPPED
:
3287 bpf_error("USB link-layer type filtering not implemented");
3289 case DLT_BLUETOOTH_HCI_H4
:
3290 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3291 bpf_error("Bluetooth link-layer type filtering not implemented");
3294 case DLT_CAN_SOCKETCAN
:
3295 bpf_error("CAN link-layer type filtering not implemented");
3297 case DLT_IEEE802_15_4
:
3298 case DLT_IEEE802_15_4_LINUX
:
3299 case DLT_IEEE802_15_4_NONASK_PHY
:
3300 case DLT_IEEE802_15_4_NOFCS
:
3301 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3303 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3304 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3307 bpf_error("SITA link-layer type filtering not implemented");
3310 bpf_error("RAIF1 link-layer type filtering not implemented");
3313 bpf_error("IPMB link-layer type filtering not implemented");
3316 bpf_error("AX.25 link-layer type filtering not implemented");
3319 /* Using the fixed-size NFLOG header it is possible to tell only
3320 * the address family of the packet, other meaningful data is
3321 * either missing or behind TLVs.
3323 bpf_error("NFLOG link-layer type filtering not implemented");
3327 * Does this link-layer header type have a field
3328 * indicating the type of the next protocol? If
3329 * so, off_linktype.constant_part will be the offset of that
3330 * field in the packet; if not, it will be -1.
3332 if (off_linktype
.constant_part
!= (u_int
)-1) {
3334 * Yes; assume it's an Ethernet type. (If
3335 * it's not, it needs to be handled specially
3338 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3341 * No; report an error.
3343 description
= pcap_datalink_val_to_description(linktype
);
3344 if (description
!= NULL
) {
3345 bpf_error("%s link-layer type filtering not implemented",
3348 bpf_error("DLT %u link-layer type filtering not implemented",
3357 * Check for an LLC SNAP packet with a given organization code and
3358 * protocol type; we check the entire contents of the 802.2 LLC and
3359 * snap headers, checking for DSAP and SSAP of SNAP and a control
3360 * field of 0x03 in the LLC header, and for the specified organization
3361 * code and protocol type in the SNAP header.
3363 static struct block
*
3364 gen_snap(orgcode
, ptype
)
3365 bpf_u_int32 orgcode
;
3368 u_char snapblock
[8];
3370 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3371 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3372 snapblock
[2] = 0x03; /* control = UI */
3373 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3374 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3375 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3376 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3377 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3378 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3382 * Generate code to match frames with an LLC header.
3387 struct block
*b0
, *b1
;
3393 * We check for an Ethernet type field less than
3394 * 1500, which means it's an 802.3 length field.
3396 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3400 * Now check for the purported DSAP and SSAP not being
3401 * 0xFF, to rule out NetWare-over-802.3.
3403 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3410 * We check for LLC traffic.
3412 b0
= gen_atmtype_abbrev(A_LLC
);
3415 case DLT_IEEE802
: /* Token Ring */
3417 * XXX - check for LLC frames.
3423 * XXX - check for LLC frames.
3427 case DLT_ATM_RFC1483
:
3429 * For LLC encapsulation, these are defined to have an
3432 * For VC encapsulation, they don't, but there's no
3433 * way to check for that; the protocol used on the VC
3434 * is negotiated out of band.
3438 case DLT_IEEE802_11
:
3439 case DLT_PRISM_HEADER
:
3440 case DLT_IEEE802_11_RADIO
:
3441 case DLT_IEEE802_11_RADIO_AVS
:
3444 * Check that we have a data frame.
3446 b0
= gen_check_802_11_data_frame();
3450 bpf_error("'llc' not supported for linktype %d", linktype
);
3458 struct block
*b0
, *b1
;
3462 * Check whether this is an LLC frame.
3467 * Load the control byte and test the low-order bit; it must
3468 * be clear for I frames.
3470 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3471 b1
= new_block(JMP(BPF_JSET
));
3482 struct block
*b0
, *b1
;
3485 * Check whether this is an LLC frame.
3490 * Now compare the low-order 2 bit of the control byte against
3491 * the appropriate value for S frames.
3493 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3501 struct block
*b0
, *b1
;
3504 * Check whether this is an LLC frame.
3509 * Now compare the low-order 2 bit of the control byte against
3510 * the appropriate value for U frames.
3512 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3518 gen_llc_s_subtype(bpf_u_int32 subtype
)
3520 struct block
*b0
, *b1
;
3523 * Check whether this is an LLC frame.
3528 * Now check for an S frame with the appropriate type.
3530 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3536 gen_llc_u_subtype(bpf_u_int32 subtype
)
3538 struct block
*b0
, *b1
;
3541 * Check whether this is an LLC frame.
3546 * Now check for a U frame with the appropriate type.
3548 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3554 * Generate code to match a particular packet type, for link-layer types
3555 * using 802.2 LLC headers.
3557 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3558 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3560 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3561 * value, if <= ETHERMTU. We use that to determine whether to
3562 * match the DSAP or both DSAP and LSAP or to check the OUI and
3563 * protocol ID in a SNAP header.
3565 static struct block
*
3566 gen_llc_linktype(proto
)
3570 * XXX - handle token-ring variable-length header.
3576 case LLCSAP_NETBEUI
:
3578 * XXX - should we check both the DSAP and the
3579 * SSAP, like this, or should we check just the
3580 * DSAP, as we do for other SAP values?
3582 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3583 ((proto
<< 8) | proto
));
3587 * XXX - are there ever SNAP frames for IPX on
3588 * non-Ethernet 802.x networks?
3590 return gen_cmp(OR_LLC
, 0, BPF_B
,
3591 (bpf_int32
)LLCSAP_IPX
);
3593 case ETHERTYPE_ATALK
:
3595 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3596 * SNAP packets with an organization code of
3597 * 0x080007 (Apple, for Appletalk) and a protocol
3598 * type of ETHERTYPE_ATALK (Appletalk).
3600 * XXX - check for an organization code of
3601 * encapsulated Ethernet as well?
3603 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3607 * XXX - we don't have to check for IPX 802.3
3608 * here, but should we check for the IPX Ethertype?
3610 if (proto
<= ETHERMTU
) {
3612 * This is an LLC SAP value, so check
3615 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3618 * This is an Ethernet type; we assume that it's
3619 * unlikely that it'll appear in the right place
3620 * at random, and therefore check only the
3621 * location that would hold the Ethernet type
3622 * in a SNAP frame with an organization code of
3623 * 0x000000 (encapsulated Ethernet).
3625 * XXX - if we were to check for the SNAP DSAP and
3626 * LSAP, as per XXX, and were also to check for an
3627 * organization code of 0x000000 (encapsulated
3628 * Ethernet), we'd do
3630 * return gen_snap(0x000000, proto);
3632 * here; for now, we don't, as per the above.
3633 * I don't know whether it's worth the extra CPU
3634 * time to do the right check or not.
3636 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3641 static struct block
*
3642 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3646 u_int src_off
, dst_off
;
3648 struct block
*b0
, *b1
;
3662 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3663 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3669 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3670 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3677 b0
= gen_linktype(proto
);
3678 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3684 static struct block
*
3685 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3686 struct in6_addr
*addr
;
3687 struct in6_addr
*mask
;
3689 u_int src_off
, dst_off
;
3691 struct block
*b0
, *b1
;
3706 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3707 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3713 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3714 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3721 /* this order is important */
3722 a
= (u_int32_t
*)addr
;
3723 m
= (u_int32_t
*)mask
;
3724 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3725 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3727 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3729 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3731 b0
= gen_linktype(proto
);
3737 static struct block
*
3738 gen_ehostop(eaddr
, dir
)
3739 register const u_char
*eaddr
;
3742 register struct block
*b0
, *b1
;
3746 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3749 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3752 b0
= gen_ehostop(eaddr
, Q_SRC
);
3753 b1
= gen_ehostop(eaddr
, Q_DST
);
3759 b0
= gen_ehostop(eaddr
, Q_SRC
);
3760 b1
= gen_ehostop(eaddr
, Q_DST
);
3765 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3769 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3773 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3777 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3781 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3785 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3793 * Like gen_ehostop, but for DLT_FDDI
3795 static struct block
*
3796 gen_fhostop(eaddr
, dir
)
3797 register const u_char
*eaddr
;
3800 struct block
*b0
, *b1
;
3804 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3807 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3810 b0
= gen_fhostop(eaddr
, Q_SRC
);
3811 b1
= gen_fhostop(eaddr
, Q_DST
);
3817 b0
= gen_fhostop(eaddr
, Q_SRC
);
3818 b1
= gen_fhostop(eaddr
, Q_DST
);
3823 bpf_error("'addr1' is only supported on 802.11");
3827 bpf_error("'addr2' is only supported on 802.11");
3831 bpf_error("'addr3' is only supported on 802.11");
3835 bpf_error("'addr4' is only supported on 802.11");
3839 bpf_error("'ra' is only supported on 802.11");
3843 bpf_error("'ta' is only supported on 802.11");
3851 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3853 static struct block
*
3854 gen_thostop(eaddr
, dir
)
3855 register const u_char
*eaddr
;
3858 register struct block
*b0
, *b1
;
3862 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3865 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3868 b0
= gen_thostop(eaddr
, Q_SRC
);
3869 b1
= gen_thostop(eaddr
, Q_DST
);
3875 b0
= gen_thostop(eaddr
, Q_SRC
);
3876 b1
= gen_thostop(eaddr
, Q_DST
);
3881 bpf_error("'addr1' is only supported on 802.11");
3885 bpf_error("'addr2' is only supported on 802.11");
3889 bpf_error("'addr3' is only supported on 802.11");
3893 bpf_error("'addr4' is only supported on 802.11");
3897 bpf_error("'ra' is only supported on 802.11");
3901 bpf_error("'ta' is only supported on 802.11");
3909 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3910 * various 802.11 + radio headers.
3912 static struct block
*
3913 gen_wlanhostop(eaddr
, dir
)
3914 register const u_char
*eaddr
;
3917 register struct block
*b0
, *b1
, *b2
;
3918 register struct slist
*s
;
3920 #ifdef ENABLE_WLAN_FILTERING_PATCH
3923 * We need to disable the optimizer because the optimizer is buggy
3924 * and wipes out some LD instructions generated by the below
3925 * code to validate the Frame Control bits
3928 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3935 * For control frames, there is no SA.
3937 * For management frames, SA is at an
3938 * offset of 10 from the beginning of
3941 * For data frames, SA is at an offset
3942 * of 10 from the beginning of the packet
3943 * if From DS is clear, at an offset of
3944 * 16 from the beginning of the packet
3945 * if From DS is set and To DS is clear,
3946 * and an offset of 24 from the beginning
3947 * of the packet if From DS is set and To DS
3952 * Generate the tests to be done for data frames
3955 * First, check for To DS set, i.e. check "link[1] & 0x01".
3957 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3958 b1
= new_block(JMP(BPF_JSET
));
3959 b1
->s
.k
= 0x01; /* To DS */
3963 * If To DS is set, the SA is at 24.
3965 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
3969 * Now, check for To DS not set, i.e. check
3970 * "!(link[1] & 0x01)".
3972 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3973 b2
= new_block(JMP(BPF_JSET
));
3974 b2
->s
.k
= 0x01; /* To DS */
3979 * If To DS is not set, the SA is at 16.
3981 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
3985 * Now OR together the last two checks. That gives
3986 * the complete set of checks for data frames with
3992 * Now check for From DS being set, and AND that with
3993 * the ORed-together checks.
3995 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
3996 b1
= new_block(JMP(BPF_JSET
));
3997 b1
->s
.k
= 0x02; /* From DS */
4002 * Now check for data frames with From DS not set.
4004 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4005 b2
= new_block(JMP(BPF_JSET
));
4006 b2
->s
.k
= 0x02; /* From DS */
4011 * If From DS isn't set, the SA is at 10.
4013 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4017 * Now OR together the checks for data frames with
4018 * From DS not set and for data frames with From DS
4019 * set; that gives the checks done for data frames.
4024 * Now check for a data frame.
4025 * I.e, check "link[0] & 0x08".
4027 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4028 b1
= new_block(JMP(BPF_JSET
));
4033 * AND that with the checks done for data frames.
4038 * If the high-order bit of the type value is 0, this
4039 * is a management frame.
4040 * I.e, check "!(link[0] & 0x08)".
4042 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4043 b2
= new_block(JMP(BPF_JSET
));
4049 * For management frames, the SA is at 10.
4051 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4055 * OR that with the checks done for data frames.
4056 * That gives the checks done for management and
4062 * If the low-order bit of the type value is 1,
4063 * this is either a control frame or a frame
4064 * with a reserved type, and thus not a
4067 * I.e., check "!(link[0] & 0x04)".
4069 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4070 b1
= new_block(JMP(BPF_JSET
));
4076 * AND that with the checks for data and management
4086 * For control frames, there is no DA.
4088 * For management frames, DA is at an
4089 * offset of 4 from the beginning of
4092 * For data frames, DA is at an offset
4093 * of 4 from the beginning of the packet
4094 * if To DS is clear and at an offset of
4095 * 16 from the beginning of the packet
4100 * Generate the tests to be done for data frames.
4102 * First, check for To DS set, i.e. "link[1] & 0x01".
4104 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4105 b1
= new_block(JMP(BPF_JSET
));
4106 b1
->s
.k
= 0x01; /* To DS */
4110 * If To DS is set, the DA is at 16.
4112 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4116 * Now, check for To DS not set, i.e. check
4117 * "!(link[1] & 0x01)".
4119 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4120 b2
= new_block(JMP(BPF_JSET
));
4121 b2
->s
.k
= 0x01; /* To DS */
4126 * If To DS is not set, the DA is at 4.
4128 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4132 * Now OR together the last two checks. That gives
4133 * the complete set of checks for data frames.
4138 * Now check for a data frame.
4139 * I.e, check "link[0] & 0x08".
4141 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4142 b1
= new_block(JMP(BPF_JSET
));
4147 * AND that with the checks done for data frames.
4152 * If the high-order bit of the type value is 0, this
4153 * is a management frame.
4154 * I.e, check "!(link[0] & 0x08)".
4156 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4157 b2
= new_block(JMP(BPF_JSET
));
4163 * For management frames, the DA is at 4.
4165 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4169 * OR that with the checks done for data frames.
4170 * That gives the checks done for management and
4176 * If the low-order bit of the type value is 1,
4177 * this is either a control frame or a frame
4178 * with a reserved type, and thus not a
4181 * I.e., check "!(link[0] & 0x04)".
4183 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4184 b1
= new_block(JMP(BPF_JSET
));
4190 * AND that with the checks for data and management
4198 * Not present in management frames; addr1 in other
4203 * If the high-order bit of the type value is 0, this
4204 * is a management frame.
4205 * I.e, check "(link[0] & 0x08)".
4207 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4208 b1
= new_block(JMP(BPF_JSET
));
4215 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4218 * AND that with the check of addr1.
4225 * Not present in management frames; addr2, if present,
4230 * Not present in CTS or ACK control frames.
4232 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4233 IEEE80211_FC0_TYPE_MASK
);
4235 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4236 IEEE80211_FC0_SUBTYPE_MASK
);
4238 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4239 IEEE80211_FC0_SUBTYPE_MASK
);
4245 * If the high-order bit of the type value is 0, this
4246 * is a management frame.
4247 * I.e, check "(link[0] & 0x08)".
4249 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4250 b1
= new_block(JMP(BPF_JSET
));
4255 * AND that with the check for frames other than
4256 * CTS and ACK frames.
4263 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4268 * XXX - add BSSID keyword?
4271 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4275 * Not present in CTS or ACK control frames.
4277 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4278 IEEE80211_FC0_TYPE_MASK
);
4280 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4281 IEEE80211_FC0_SUBTYPE_MASK
);
4283 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4284 IEEE80211_FC0_SUBTYPE_MASK
);
4288 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4294 * Not present in control frames.
4296 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4297 IEEE80211_FC0_TYPE_MASK
);
4299 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4305 * Present only if the direction mask has both "From DS"
4306 * and "To DS" set. Neither control frames nor management
4307 * frames should have both of those set, so we don't
4308 * check the frame type.
4310 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4311 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4312 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4317 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4318 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4324 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4325 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4334 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4335 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4336 * as the RFC states.)
4338 static struct block
*
4339 gen_ipfchostop(eaddr
, dir
)
4340 register const u_char
*eaddr
;
4343 register struct block
*b0
, *b1
;
4347 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4350 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4353 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4354 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4360 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4361 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4366 bpf_error("'addr1' is only supported on 802.11");
4370 bpf_error("'addr2' is only supported on 802.11");
4374 bpf_error("'addr3' is only supported on 802.11");
4378 bpf_error("'addr4' is only supported on 802.11");
4382 bpf_error("'ra' is only supported on 802.11");
4386 bpf_error("'ta' is only supported on 802.11");
4394 * This is quite tricky because there may be pad bytes in front of the
4395 * DECNET header, and then there are two possible data packet formats that
4396 * carry both src and dst addresses, plus 5 packet types in a format that
4397 * carries only the src node, plus 2 types that use a different format and
4398 * also carry just the src node.
4402 * Instead of doing those all right, we just look for data packets with
4403 * 0 or 1 bytes of padding. If you want to look at other packets, that
4404 * will require a lot more hacking.
4406 * To add support for filtering on DECNET "areas" (network numbers)
4407 * one would want to add a "mask" argument to this routine. That would
4408 * make the filter even more inefficient, although one could be clever
4409 * and not generate masking instructions if the mask is 0xFFFF.
4411 static struct block
*
4412 gen_dnhostop(addr
, dir
)
4416 struct block
*b0
, *b1
, *b2
, *tmp
;
4417 u_int offset_lh
; /* offset if long header is received */
4418 u_int offset_sh
; /* offset if short header is received */
4423 offset_sh
= 1; /* follows flags */
4424 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4428 offset_sh
= 3; /* follows flags, dstnode */
4429 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4433 /* Inefficient because we do our Calvinball dance twice */
4434 b0
= gen_dnhostop(addr
, Q_SRC
);
4435 b1
= gen_dnhostop(addr
, Q_DST
);
4441 /* Inefficient because we do our Calvinball dance twice */
4442 b0
= gen_dnhostop(addr
, Q_SRC
);
4443 b1
= gen_dnhostop(addr
, Q_DST
);
4448 bpf_error("ISO host filtering not implemented");
4453 b0
= gen_linktype(ETHERTYPE_DN
);
4454 /* Check for pad = 1, long header case */
4455 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4456 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4457 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4458 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4460 /* Check for pad = 0, long header case */
4461 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4462 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4465 /* Check for pad = 1, short header case */
4466 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4467 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4468 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4471 /* Check for pad = 0, short header case */
4472 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4473 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4477 /* Combine with test for linktype */
4483 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4484 * test the bottom-of-stack bit, and then check the version number
4485 * field in the IP header.
4487 static struct block
*
4488 gen_mpls_linktype(proto
)
4491 struct block
*b0
, *b1
;
4496 /* match the bottom-of-stack bit */
4497 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4498 /* match the IPv4 version number */
4499 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4504 /* match the bottom-of-stack bit */
4505 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4506 /* match the IPv4 version number */
4507 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4516 static struct block
*
4517 gen_host(addr
, mask
, proto
, dir
, type
)
4524 struct block
*b0
, *b1
;
4525 const char *typestr
;
4535 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4537 * Only check for non-IPv4 addresses if we're not
4538 * checking MPLS-encapsulated packets.
4540 if (label_stack_depth
== 0) {
4541 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4543 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4549 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4552 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4555 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4558 bpf_error("'tcp' modifier applied to %s", typestr
);
4561 bpf_error("'sctp' modifier applied to %s", typestr
);
4564 bpf_error("'udp' modifier applied to %s", typestr
);
4567 bpf_error("'icmp' modifier applied to %s", typestr
);
4570 bpf_error("'igmp' modifier applied to %s", typestr
);
4573 bpf_error("'igrp' modifier applied to %s", typestr
);
4576 bpf_error("'pim' modifier applied to %s", typestr
);
4579 bpf_error("'vrrp' modifier applied to %s", typestr
);
4582 bpf_error("'carp' modifier applied to %s", typestr
);
4585 bpf_error("ATALK host filtering not implemented");
4588 bpf_error("AARP host filtering not implemented");
4591 return gen_dnhostop(addr
, dir
);
4594 bpf_error("SCA host filtering not implemented");
4597 bpf_error("LAT host filtering not implemented");
4600 bpf_error("MOPDL host filtering not implemented");
4603 bpf_error("MOPRC host filtering not implemented");
4606 bpf_error("'ip6' modifier applied to ip host");
4609 bpf_error("'icmp6' modifier applied to %s", typestr
);
4612 bpf_error("'ah' modifier applied to %s", typestr
);
4615 bpf_error("'esp' modifier applied to %s", typestr
);
4618 bpf_error("ISO host filtering not implemented");
4621 bpf_error("'esis' modifier applied to %s", typestr
);
4624 bpf_error("'isis' modifier applied to %s", typestr
);
4627 bpf_error("'clnp' modifier applied to %s", typestr
);
4630 bpf_error("'stp' modifier applied to %s", typestr
);
4633 bpf_error("IPX host filtering not implemented");
4636 bpf_error("'netbeui' modifier applied to %s", typestr
);
4639 bpf_error("'radio' modifier applied to %s", typestr
);
4648 static struct block
*
4649 gen_host6(addr
, mask
, proto
, dir
, type
)
4650 struct in6_addr
*addr
;
4651 struct in6_addr
*mask
;
4656 const char *typestr
;
4666 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4669 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4672 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4675 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4678 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4681 bpf_error("'sctp' modifier applied to %s", typestr
);
4684 bpf_error("'tcp' modifier applied to %s", typestr
);
4687 bpf_error("'udp' modifier applied to %s", typestr
);
4690 bpf_error("'icmp' modifier applied to %s", typestr
);
4693 bpf_error("'igmp' modifier applied to %s", typestr
);
4696 bpf_error("'igrp' modifier applied to %s", typestr
);
4699 bpf_error("'pim' modifier applied to %s", typestr
);
4702 bpf_error("'vrrp' modifier applied to %s", typestr
);
4705 bpf_error("'carp' modifier applied to %s", typestr
);
4708 bpf_error("ATALK host filtering not implemented");
4711 bpf_error("AARP host filtering not implemented");
4714 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4717 bpf_error("SCA host filtering not implemented");
4720 bpf_error("LAT host filtering not implemented");
4723 bpf_error("MOPDL host filtering not implemented");
4726 bpf_error("MOPRC host filtering not implemented");
4729 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4732 bpf_error("'icmp6' modifier applied to %s", typestr
);
4735 bpf_error("'ah' modifier applied to %s", typestr
);
4738 bpf_error("'esp' modifier applied to %s", typestr
);
4741 bpf_error("ISO host filtering not implemented");
4744 bpf_error("'esis' modifier applied to %s", typestr
);
4747 bpf_error("'isis' modifier applied to %s", typestr
);
4750 bpf_error("'clnp' modifier applied to %s", typestr
);
4753 bpf_error("'stp' modifier applied to %s", typestr
);
4756 bpf_error("IPX host filtering not implemented");
4759 bpf_error("'netbeui' modifier applied to %s", typestr
);
4762 bpf_error("'radio' modifier applied to %s", typestr
);
4772 static struct block
*
4773 gen_gateway(eaddr
, alist
, proto
, dir
)
4774 const u_char
*eaddr
;
4775 bpf_u_int32
**alist
;
4779 struct block
*b0
, *b1
, *tmp
;
4782 bpf_error("direction applied to 'gateway'");
4791 case DLT_NETANALYZER
:
4792 case DLT_NETANALYZER_TRANSPARENT
:
4793 b1
= gen_prevlinkhdr_check();
4794 b0
= gen_ehostop(eaddr
, Q_OR
);
4799 b0
= gen_fhostop(eaddr
, Q_OR
);
4802 b0
= gen_thostop(eaddr
, Q_OR
);
4804 case DLT_IEEE802_11
:
4805 case DLT_PRISM_HEADER
:
4806 case DLT_IEEE802_11_RADIO_AVS
:
4807 case DLT_IEEE802_11_RADIO
:
4809 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4813 * This is LLC-multiplexed traffic; if it were
4814 * LANE, linktype would have been set to
4818 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4820 case DLT_IP_OVER_FC
:
4821 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4825 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4827 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4829 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4838 bpf_error("illegal modifier of 'gateway'");
4844 gen_proto_abbrev(proto
)
4853 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4854 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4859 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4860 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4865 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4866 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4871 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4874 #ifndef IPPROTO_IGMP
4875 #define IPPROTO_IGMP 2
4879 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4882 #ifndef IPPROTO_IGRP
4883 #define IPPROTO_IGRP 9
4886 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4890 #define IPPROTO_PIM 103
4894 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4895 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4899 #ifndef IPPROTO_VRRP
4900 #define IPPROTO_VRRP 112
4904 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4907 #ifndef IPPROTO_CARP
4908 #define IPPROTO_CARP 112
4912 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4916 b1
= gen_linktype(ETHERTYPE_IP
);
4920 b1
= gen_linktype(ETHERTYPE_ARP
);
4924 b1
= gen_linktype(ETHERTYPE_REVARP
);
4928 bpf_error("link layer applied in wrong context");
4931 b1
= gen_linktype(ETHERTYPE_ATALK
);
4935 b1
= gen_linktype(ETHERTYPE_AARP
);
4939 b1
= gen_linktype(ETHERTYPE_DN
);
4943 b1
= gen_linktype(ETHERTYPE_SCA
);
4947 b1
= gen_linktype(ETHERTYPE_LAT
);
4951 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4955 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4959 b1
= gen_linktype(ETHERTYPE_IPV6
);
4962 #ifndef IPPROTO_ICMPV6
4963 #define IPPROTO_ICMPV6 58
4966 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4970 #define IPPROTO_AH 51
4973 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4974 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4979 #define IPPROTO_ESP 50
4982 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4983 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4988 b1
= gen_linktype(LLCSAP_ISONS
);
4992 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4996 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4999 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5000 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5001 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5003 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5005 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5007 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5011 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5012 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5013 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5015 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5017 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5019 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5023 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5024 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5025 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5027 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5032 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5033 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5038 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5039 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5041 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5043 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5048 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5049 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5054 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5055 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5060 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5064 b1
= gen_linktype(LLCSAP_8021D
);
5068 b1
= gen_linktype(LLCSAP_IPX
);
5072 b1
= gen_linktype(LLCSAP_NETBEUI
);
5076 bpf_error("'radio' is not a valid protocol type");
5084 static struct block
*
5090 /* not IPv4 frag other than the first frag */
5091 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5092 b
= new_block(JMP(BPF_JSET
));
5101 * Generate a comparison to a port value in the transport-layer header
5102 * at the specified offset from the beginning of that header.
5104 * XXX - this handles a variable-length prefix preceding the link-layer
5105 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5106 * variable-length link-layer headers (such as Token Ring or 802.11
5109 static struct block
*
5110 gen_portatom(off
, v
)
5114 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5117 static struct block
*
5118 gen_portatom6(off
, v
)
5122 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5126 gen_portop(port
, proto
, dir
)
5127 int port
, proto
, dir
;
5129 struct block
*b0
, *b1
, *tmp
;
5131 /* ip proto 'proto' and not a fragment other than the first fragment */
5132 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5138 b1
= gen_portatom(0, (bpf_int32
)port
);
5142 b1
= gen_portatom(2, (bpf_int32
)port
);
5147 tmp
= 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
);
5166 static struct block
*
5167 gen_port(port
, ip_proto
, dir
)
5172 struct block
*b0
, *b1
, *tmp
;
5177 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5178 * not LLC encapsulation with LLCSAP_IP.
5180 * For IEEE 802 networks - which includes 802.5 token ring
5181 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5182 * says that SNAP encapsulation is used, not LLC encapsulation
5185 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5186 * RFC 2225 say that SNAP encapsulation is used, not LLC
5187 * encapsulation with LLCSAP_IP.
5189 * So we always check for ETHERTYPE_IP.
5191 b0
= gen_linktype(ETHERTYPE_IP
);
5197 b1
= gen_portop(port
, ip_proto
, dir
);
5201 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5202 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5204 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5216 gen_portop6(port
, proto
, dir
)
5217 int port
, proto
, dir
;
5219 struct block
*b0
, *b1
, *tmp
;
5221 /* ip6 proto 'proto' */
5222 /* XXX - catch the first fragment of a fragmented packet? */
5223 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5227 b1
= gen_portatom6(0, (bpf_int32
)port
);
5231 b1
= gen_portatom6(2, (bpf_int32
)port
);
5236 tmp
= 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
);
5255 static struct block
*
5256 gen_port6(port
, ip_proto
, dir
)
5261 struct block
*b0
, *b1
, *tmp
;
5263 /* link proto ip6 */
5264 b0
= gen_linktype(ETHERTYPE_IPV6
);
5270 b1
= gen_portop6(port
, ip_proto
, dir
);
5274 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5275 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5277 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5288 /* gen_portrange code */
5289 static struct block
*
5290 gen_portrangeatom(off
, v1
, v2
)
5294 struct block
*b1
, *b2
;
5298 * Reverse the order of the ports, so v1 is the lower one.
5307 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5308 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5316 gen_portrangeop(port1
, port2
, proto
, dir
)
5321 struct block
*b0
, *b1
, *tmp
;
5323 /* ip proto 'proto' and not a fragment other than the first fragment */
5324 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5330 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5334 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5339 tmp
= 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
);
5358 static struct block
*
5359 gen_portrange(port1
, port2
, ip_proto
, dir
)
5364 struct block
*b0
, *b1
, *tmp
;
5367 b0
= gen_linktype(ETHERTYPE_IP
);
5373 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5377 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5378 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5380 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5391 static struct block
*
5392 gen_portrangeatom6(off
, v1
, v2
)
5396 struct block
*b1
, *b2
;
5400 * Reverse the order of the ports, so v1 is the lower one.
5409 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5410 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5418 gen_portrangeop6(port1
, port2
, proto
, dir
)
5423 struct block
*b0
, *b1
, *tmp
;
5425 /* ip6 proto 'proto' */
5426 /* XXX - catch the first fragment of a fragmented packet? */
5427 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5431 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5435 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5440 tmp
= 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
);
5459 static struct block
*
5460 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5465 struct block
*b0
, *b1
, *tmp
;
5467 /* link proto ip6 */
5468 b0
= gen_linktype(ETHERTYPE_IPV6
);
5474 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5478 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5479 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5481 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5493 lookup_proto(name
, proto
)
5494 register const char *name
;
5504 v
= pcap_nametoproto(name
);
5505 if (v
== PROTO_UNDEF
)
5506 bpf_error("unknown ip proto '%s'", name
);
5510 /* XXX should look up h/w protocol type based on linktype */
5511 v
= pcap_nametoeproto(name
);
5512 if (v
== PROTO_UNDEF
) {
5513 v
= pcap_nametollc(name
);
5514 if (v
== PROTO_UNDEF
)
5515 bpf_error("unknown ether proto '%s'", name
);
5520 if (strcmp(name
, "esis") == 0)
5522 else if (strcmp(name
, "isis") == 0)
5524 else if (strcmp(name
, "clnp") == 0)
5527 bpf_error("unknown osi proto '%s'", name
);
5547 static struct block
*
5548 gen_protochain(v
, proto
, dir
)
5553 #ifdef NO_PROTOCHAIN
5554 return gen_proto(v
, proto
, dir
);
5556 struct block
*b0
, *b
;
5557 struct slist
*s
[100];
5558 int fix2
, fix3
, fix4
, fix5
;
5559 int ahcheck
, again
, end
;
5561 int reg2
= alloc_reg();
5563 memset(s
, 0, sizeof(s
));
5564 fix3
= fix4
= fix5
= 0;
5571 b0
= gen_protochain(v
, Q_IP
, dir
);
5572 b
= gen_protochain(v
, Q_IPV6
, dir
);
5576 bpf_error("bad protocol applied for 'protochain'");
5581 * We don't handle variable-length prefixes before the link-layer
5582 * header, or variable-length link-layer headers, here yet.
5583 * We might want to add BPF instructions to do the protochain
5584 * work, to simplify that and, on platforms that have a BPF
5585 * interpreter with the new instructions, let the filtering
5586 * be done in the kernel. (We already require a modified BPF
5587 * engine to do the protochain stuff, to support backward
5588 * branches, and backward branch support is unlikely to appear
5589 * in kernel BPF engines.)
5591 if (off_linkpl
.is_variable
)
5592 bpf_error("'protochain' not supported with variable length headers");
5594 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5597 * s[0] is a dummy entry to protect other BPF insn from damage
5598 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5599 * hard to find interdependency made by jump table fixup.
5602 s
[i
] = new_stmt(0); /*dummy*/
5607 b0
= gen_linktype(ETHERTYPE_IP
);
5610 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5611 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5613 /* X = ip->ip_hl << 2 */
5614 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5615 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5620 b0
= gen_linktype(ETHERTYPE_IPV6
);
5622 /* A = ip6->ip_nxt */
5623 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5624 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5626 /* X = sizeof(struct ip6_hdr) */
5627 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5633 bpf_error("unsupported proto to gen_protochain");
5637 /* again: if (A == v) goto end; else fall through; */
5639 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5641 s
[i
]->s
.jt
= NULL
; /*later*/
5642 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5646 #ifndef IPPROTO_NONE
5647 #define IPPROTO_NONE 59
5649 /* if (A == IPPROTO_NONE) goto end */
5650 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5651 s
[i
]->s
.jt
= NULL
; /*later*/
5652 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5653 s
[i
]->s
.k
= IPPROTO_NONE
;
5654 s
[fix5
]->s
.jf
= s
[i
];
5658 if (proto
== Q_IPV6
) {
5659 int v6start
, v6end
, v6advance
, j
;
5662 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5663 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5664 s
[i
]->s
.jt
= NULL
; /*later*/
5665 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5666 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5667 s
[fix2
]->s
.jf
= s
[i
];
5669 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5670 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5671 s
[i
]->s
.jt
= NULL
; /*later*/
5672 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5673 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5675 /* if (A == IPPROTO_ROUTING) 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_ROUTING
;
5681 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
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
; /*later*/
5685 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5695 * A = P[X + packet head];
5696 * X = X + (P[X + packet head + 1] + 1) * 8;
5698 /* A = P[X + packet head] */
5699 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5700 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5703 s
[i
] = new_stmt(BPF_ST
);
5706 /* A = P[X + packet head + 1]; */
5707 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5708 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5711 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5715 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5719 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5723 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5726 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5730 /* goto again; (must use BPF_JA for backward jump) */
5731 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5732 s
[i
]->s
.k
= again
- i
- 1;
5733 s
[i
- 1]->s
.jf
= s
[i
];
5737 for (j
= v6start
; j
<= v6end
; j
++)
5738 s
[j
]->s
.jt
= s
[v6advance
];
5741 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5743 s
[fix2
]->s
.jf
= s
[i
];
5749 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5750 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5751 s
[i
]->s
.jt
= NULL
; /*later*/
5752 s
[i
]->s
.jf
= NULL
; /*later*/
5753 s
[i
]->s
.k
= IPPROTO_AH
;
5755 s
[fix3
]->s
.jf
= s
[ahcheck
];
5762 * X = X + (P[X + 1] + 2) * 4;
5765 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5767 /* A = P[X + packet head]; */
5768 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5769 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5772 s
[i
] = new_stmt(BPF_ST
);
5776 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5779 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5783 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5785 /* A = P[X + packet head] */
5786 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5787 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5790 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5794 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5798 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5801 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5805 /* goto again; (must use BPF_JA for backward jump) */
5806 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5807 s
[i
]->s
.k
= again
- i
- 1;
5812 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5814 s
[fix2
]->s
.jt
= s
[end
];
5815 s
[fix4
]->s
.jf
= s
[end
];
5816 s
[fix5
]->s
.jt
= s
[end
];
5823 for (i
= 0; i
< max
- 1; i
++)
5824 s
[i
]->next
= s
[i
+ 1];
5825 s
[max
- 1]->next
= NULL
;
5830 b
= new_block(JMP(BPF_JEQ
));
5831 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5841 static struct block
*
5842 gen_check_802_11_data_frame()
5845 struct block
*b0
, *b1
;
5848 * A data frame has the 0x08 bit (b3) in the frame control field set
5849 * and the 0x04 bit (b2) clear.
5851 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5852 b0
= new_block(JMP(BPF_JSET
));
5856 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5857 b1
= new_block(JMP(BPF_JSET
));
5868 * Generate code that checks whether the packet is a packet for protocol
5869 * <proto> and whether the type field in that protocol's header has
5870 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5871 * IP packet and checks the protocol number in the IP header against <v>.
5873 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5874 * against Q_IP and Q_IPV6.
5876 static struct block
*
5877 gen_proto(v
, proto
, dir
)
5882 struct block
*b0
, *b1
;
5887 if (dir
!= Q_DEFAULT
)
5888 bpf_error("direction applied to 'proto'");
5892 b0
= gen_proto(v
, Q_IP
, dir
);
5893 b1
= gen_proto(v
, Q_IPV6
, dir
);
5899 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5900 * not LLC encapsulation with LLCSAP_IP.
5902 * For IEEE 802 networks - which includes 802.5 token ring
5903 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5904 * says that SNAP encapsulation is used, not LLC encapsulation
5907 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5908 * RFC 2225 say that SNAP encapsulation is used, not LLC
5909 * encapsulation with LLCSAP_IP.
5911 * So we always check for ETHERTYPE_IP.
5913 b0
= gen_linktype(ETHERTYPE_IP
);
5915 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5917 b1
= gen_protochain(v
, Q_IP
);
5927 * Frame Relay packets typically have an OSI
5928 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5929 * generates code to check for all the OSI
5930 * NLPIDs, so calling it and then adding a check
5931 * for the particular NLPID for which we're
5932 * looking is bogus, as we can just check for
5935 * What we check for is the NLPID and a frame
5936 * control field value of UI, i.e. 0x03 followed
5939 * XXX - assumes a 2-byte Frame Relay header with
5940 * DLCI and flags. What if the address is longer?
5942 * XXX - what about SNAP-encapsulated frames?
5944 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5950 * Cisco uses an Ethertype lookalike - for OSI,
5953 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5954 /* OSI in C-HDLC is stuffed with a fudge byte */
5955 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
5960 b0
= gen_linktype(LLCSAP_ISONS
);
5961 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
5967 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5969 * 4 is the offset of the PDU type relative to the IS-IS
5972 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
5977 bpf_error("arp does not encapsulate another protocol");
5981 bpf_error("rarp does not encapsulate another protocol");
5985 bpf_error("atalk encapsulation is not specifiable");
5989 bpf_error("decnet encapsulation is not specifiable");
5993 bpf_error("sca does not encapsulate another protocol");
5997 bpf_error("lat does not encapsulate another protocol");
6001 bpf_error("moprc does not encapsulate another protocol");
6005 bpf_error("mopdl does not encapsulate another protocol");
6009 return gen_linktype(v
);
6012 bpf_error("'udp proto' is bogus");
6016 bpf_error("'tcp proto' is bogus");
6020 bpf_error("'sctp proto' is bogus");
6024 bpf_error("'icmp proto' is bogus");
6028 bpf_error("'igmp proto' is bogus");
6032 bpf_error("'igrp proto' is bogus");
6036 bpf_error("'pim proto' is bogus");
6040 bpf_error("'vrrp proto' is bogus");
6044 bpf_error("'carp proto' is bogus");
6048 b0
= gen_linktype(ETHERTYPE_IPV6
);
6051 * Also check for a fragment header before the final
6054 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6055 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6057 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6060 b1
= gen_protochain(v
, Q_IPV6
);
6066 bpf_error("'icmp6 proto' is bogus");
6069 bpf_error("'ah proto' is bogus");
6072 bpf_error("'ah proto' is bogus");
6075 bpf_error("'stp proto' is bogus");
6078 bpf_error("'ipx proto' is bogus");
6081 bpf_error("'netbeui proto' is bogus");
6084 bpf_error("'radio proto' is bogus");
6095 register const char *name
;
6098 int proto
= q
.proto
;
6102 bpf_u_int32 mask
, addr
;
6104 bpf_u_int32
**alist
;
6107 struct sockaddr_in
*sin4
;
6108 struct sockaddr_in6
*sin6
;
6109 struct addrinfo
*res
, *res0
;
6110 struct in6_addr mask128
;
6112 struct block
*b
, *tmp
;
6113 int port
, real_proto
;
6119 addr
= pcap_nametonetaddr(name
);
6121 bpf_error("unknown network '%s'", name
);
6122 /* Left justify network addr and calculate its network mask */
6124 while (addr
&& (addr
& 0xff000000) == 0) {
6128 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6132 if (proto
== Q_LINK
) {
6136 case DLT_NETANALYZER
:
6137 case DLT_NETANALYZER_TRANSPARENT
:
6138 eaddr
= pcap_ether_hostton(name
);
6141 "unknown ether host '%s'", name
);
6142 tmp
= gen_prevlinkhdr_check();
6143 b
= gen_ehostop(eaddr
, dir
);
6150 eaddr
= pcap_ether_hostton(name
);
6153 "unknown FDDI host '%s'", name
);
6154 b
= gen_fhostop(eaddr
, dir
);
6159 eaddr
= pcap_ether_hostton(name
);
6162 "unknown token ring host '%s'", name
);
6163 b
= gen_thostop(eaddr
, dir
);
6167 case DLT_IEEE802_11
:
6168 case DLT_PRISM_HEADER
:
6169 case DLT_IEEE802_11_RADIO_AVS
:
6170 case DLT_IEEE802_11_RADIO
:
6172 eaddr
= pcap_ether_hostton(name
);
6175 "unknown 802.11 host '%s'", name
);
6176 b
= gen_wlanhostop(eaddr
, dir
);
6180 case DLT_IP_OVER_FC
:
6181 eaddr
= pcap_ether_hostton(name
);
6184 "unknown Fibre Channel host '%s'", name
);
6185 b
= gen_ipfchostop(eaddr
, dir
);
6190 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6191 } else if (proto
== Q_DECNET
) {
6192 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6194 * I don't think DECNET hosts can be multihomed, so
6195 * there is no need to build up a list of addresses
6197 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6200 alist
= pcap_nametoaddr(name
);
6201 if (alist
== NULL
|| *alist
== NULL
)
6202 bpf_error("unknown host '%s'", name
);
6204 if (off_linktype
.constant_part
== (u_int
)-1 &&
6205 tproto
== Q_DEFAULT
)
6207 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6209 tmp
= gen_host(**alist
++, 0xffffffff,
6210 tproto
, dir
, q
.addr
);
6216 memset(&mask128
, 0xff, sizeof(mask128
));
6217 res0
= res
= pcap_nametoaddrinfo(name
);
6219 bpf_error("unknown host '%s'", name
);
6222 tproto
= tproto6
= proto
;
6223 if (off_linktype
.constant_part
== -1 &&
6224 tproto
== Q_DEFAULT
) {
6228 for (res
= res0
; res
; res
= res
->ai_next
) {
6229 switch (res
->ai_family
) {
6231 if (tproto
== Q_IPV6
)
6234 sin4
= (struct sockaddr_in
*)
6236 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6237 0xffffffff, tproto
, dir
, q
.addr
);
6240 if (tproto6
== Q_IP
)
6243 sin6
= (struct sockaddr_in6
*)
6245 tmp
= gen_host6(&sin6
->sin6_addr
,
6246 &mask128
, tproto6
, dir
, q
.addr
);
6258 bpf_error("unknown host '%s'%s", name
,
6259 (proto
== Q_DEFAULT
)
6261 : " for specified address family");
6268 if (proto
!= Q_DEFAULT
&&
6269 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6270 bpf_error("illegal qualifier of 'port'");
6271 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6272 bpf_error("unknown port '%s'", name
);
6273 if (proto
== Q_UDP
) {
6274 if (real_proto
== IPPROTO_TCP
)
6275 bpf_error("port '%s' is tcp", name
);
6276 else if (real_proto
== IPPROTO_SCTP
)
6277 bpf_error("port '%s' is sctp", name
);
6279 /* override PROTO_UNDEF */
6280 real_proto
= IPPROTO_UDP
;
6282 if (proto
== Q_TCP
) {
6283 if (real_proto
== IPPROTO_UDP
)
6284 bpf_error("port '%s' is udp", name
);
6286 else if (real_proto
== IPPROTO_SCTP
)
6287 bpf_error("port '%s' is sctp", name
);
6289 /* override PROTO_UNDEF */
6290 real_proto
= IPPROTO_TCP
;
6292 if (proto
== Q_SCTP
) {
6293 if (real_proto
== IPPROTO_UDP
)
6294 bpf_error("port '%s' is udp", name
);
6296 else if (real_proto
== IPPROTO_TCP
)
6297 bpf_error("port '%s' is tcp", name
);
6299 /* override PROTO_UNDEF */
6300 real_proto
= IPPROTO_SCTP
;
6303 bpf_error("illegal port number %d < 0", port
);
6305 bpf_error("illegal port number %d > 65535", port
);
6306 b
= gen_port(port
, real_proto
, dir
);
6307 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6311 if (proto
!= Q_DEFAULT
&&
6312 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6313 bpf_error("illegal qualifier of 'portrange'");
6314 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6315 bpf_error("unknown port in range '%s'", name
);
6316 if (proto
== Q_UDP
) {
6317 if (real_proto
== IPPROTO_TCP
)
6318 bpf_error("port in range '%s' is tcp", name
);
6319 else if (real_proto
== IPPROTO_SCTP
)
6320 bpf_error("port in range '%s' is sctp", name
);
6322 /* override PROTO_UNDEF */
6323 real_proto
= IPPROTO_UDP
;
6325 if (proto
== Q_TCP
) {
6326 if (real_proto
== IPPROTO_UDP
)
6327 bpf_error("port in range '%s' is udp", name
);
6328 else if (real_proto
== IPPROTO_SCTP
)
6329 bpf_error("port in range '%s' is sctp", name
);
6331 /* override PROTO_UNDEF */
6332 real_proto
= IPPROTO_TCP
;
6334 if (proto
== Q_SCTP
) {
6335 if (real_proto
== IPPROTO_UDP
)
6336 bpf_error("port in range '%s' is udp", name
);
6337 else if (real_proto
== IPPROTO_TCP
)
6338 bpf_error("port in range '%s' is tcp", name
);
6340 /* override PROTO_UNDEF */
6341 real_proto
= IPPROTO_SCTP
;
6344 bpf_error("illegal port number %d < 0", port1
);
6346 bpf_error("illegal port number %d > 65535", port1
);
6348 bpf_error("illegal port number %d < 0", port2
);
6350 bpf_error("illegal port number %d > 65535", port2
);
6352 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6353 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6358 eaddr
= pcap_ether_hostton(name
);
6360 bpf_error("unknown ether host: %s", name
);
6362 alist
= pcap_nametoaddr(name
);
6363 if (alist
== NULL
|| *alist
== NULL
)
6364 bpf_error("unknown host '%s'", name
);
6365 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6369 bpf_error("'gateway' not supported in this configuration");
6373 real_proto
= lookup_proto(name
, proto
);
6374 if (real_proto
>= 0)
6375 return gen_proto(real_proto
, proto
, dir
);
6377 bpf_error("unknown protocol: %s", name
);
6380 real_proto
= lookup_proto(name
, proto
);
6381 if (real_proto
>= 0)
6382 return gen_protochain(real_proto
, proto
, dir
);
6384 bpf_error("unknown protocol: %s", name
);
6395 gen_mcode(s1
, s2
, masklen
, q
)
6396 register const char *s1
, *s2
;
6397 register unsigned int masklen
;
6400 register int nlen
, mlen
;
6403 nlen
= __pcap_atoin(s1
, &n
);
6404 /* Promote short ipaddr */
6408 mlen
= __pcap_atoin(s2
, &m
);
6409 /* Promote short ipaddr */
6412 bpf_error("non-network bits set in \"%s mask %s\"",
6415 /* Convert mask len to mask */
6417 bpf_error("mask length must be <= 32");
6420 * X << 32 is not guaranteed by C to be 0; it's
6425 m
= 0xffffffff << (32 - masklen
);
6427 bpf_error("non-network bits set in \"%s/%d\"",
6434 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6437 bpf_error("Mask syntax for networks only");
6446 register const char *s
;
6451 int proto
= q
.proto
;
6457 else if (q
.proto
== Q_DECNET
)
6458 vlen
= __pcap_atodn(s
, &v
);
6460 vlen
= __pcap_atoin(s
, &v
);
6467 if (proto
== Q_DECNET
)
6468 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6469 else if (proto
== Q_LINK
) {
6470 bpf_error("illegal link layer address");
6473 if (s
== NULL
&& q
.addr
== Q_NET
) {
6474 /* Promote short net number */
6475 while (v
&& (v
& 0xff000000) == 0) {
6480 /* Promote short ipaddr */
6484 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6489 proto
= IPPROTO_UDP
;
6490 else if (proto
== Q_TCP
)
6491 proto
= IPPROTO_TCP
;
6492 else if (proto
== Q_SCTP
)
6493 proto
= IPPROTO_SCTP
;
6494 else if (proto
== Q_DEFAULT
)
6495 proto
= PROTO_UNDEF
;
6497 bpf_error("illegal qualifier of 'port'");
6500 bpf_error("illegal port number %u > 65535", v
);
6504 b
= gen_port((int)v
, proto
, dir
);
6505 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6511 proto
= IPPROTO_UDP
;
6512 else if (proto
== Q_TCP
)
6513 proto
= IPPROTO_TCP
;
6514 else if (proto
== Q_SCTP
)
6515 proto
= IPPROTO_SCTP
;
6516 else if (proto
== Q_DEFAULT
)
6517 proto
= PROTO_UNDEF
;
6519 bpf_error("illegal qualifier of 'portrange'");
6522 bpf_error("illegal port number %u > 65535", v
);
6526 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6527 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6532 bpf_error("'gateway' requires a name");
6536 return gen_proto((int)v
, proto
, dir
);
6539 return gen_protochain((int)v
, proto
, dir
);
6554 gen_mcode6(s1
, s2
, masklen
, q
)
6555 register const char *s1
, *s2
;
6556 register unsigned int masklen
;
6559 struct addrinfo
*res
;
6560 struct in6_addr
*addr
;
6561 struct in6_addr mask
;
6566 bpf_error("no mask %s supported", s2
);
6568 res
= pcap_nametoaddrinfo(s1
);
6570 bpf_error("invalid ip6 address %s", s1
);
6573 bpf_error("%s resolved to multiple address", s1
);
6574 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6576 if (sizeof(mask
) * 8 < masklen
)
6577 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6578 memset(&mask
, 0, sizeof(mask
));
6579 memset(&mask
, 0xff, masklen
/ 8);
6581 mask
.s6_addr
[masklen
/ 8] =
6582 (0xff << (8 - masklen
% 8)) & 0xff;
6585 a
= (u_int32_t
*)addr
;
6586 m
= (u_int32_t
*)&mask
;
6587 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6588 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6589 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6597 bpf_error("Mask syntax for networks only");
6601 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6607 bpf_error("invalid qualifier against IPv6 address");
6616 register const u_char
*eaddr
;
6619 struct block
*b
, *tmp
;
6621 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6624 case DLT_NETANALYZER
:
6625 case DLT_NETANALYZER_TRANSPARENT
:
6626 tmp
= gen_prevlinkhdr_check();
6627 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6632 return gen_fhostop(eaddr
, (int)q
.dir
);
6634 return gen_thostop(eaddr
, (int)q
.dir
);
6635 case DLT_IEEE802_11
:
6636 case DLT_PRISM_HEADER
:
6637 case DLT_IEEE802_11_RADIO_AVS
:
6638 case DLT_IEEE802_11_RADIO
:
6640 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6641 case DLT_IP_OVER_FC
:
6642 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6644 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6648 bpf_error("ethernet address used in non-ether expression");
6655 struct slist
*s0
, *s1
;
6658 * This is definitely not the best way to do this, but the
6659 * lists will rarely get long.
6666 static struct slist
*
6672 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6677 static struct slist
*
6683 s
= new_stmt(BPF_LD
|BPF_MEM
);
6689 * Modify "index" to use the value stored into its register as an
6690 * offset relative to the beginning of the header for the protocol
6691 * "proto", and allocate a register and put an item "size" bytes long
6692 * (1, 2, or 4) at that offset into that register, making it the register
6696 gen_load(proto
, inst
, size
)
6701 struct slist
*s
, *tmp
;
6703 int regno
= alloc_reg();
6705 free_reg(inst
->regno
);
6709 bpf_error("data size must be 1, 2, or 4");
6725 bpf_error("unsupported index operation");
6729 * The offset is relative to the beginning of the packet
6730 * data, if we have a radio header. (If we don't, this
6733 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6734 linktype
!= DLT_IEEE802_11_RADIO
&&
6735 linktype
!= DLT_PRISM_HEADER
)
6736 bpf_error("radio information not present in capture");
6739 * Load into the X register the offset computed into the
6740 * register specified by "index".
6742 s
= xfer_to_x(inst
);
6745 * Load the item at that offset.
6747 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6749 sappend(inst
->s
, s
);
6754 * The offset is relative to the beginning of
6755 * the link-layer header.
6757 * XXX - what about ATM LANE? Should the index be
6758 * relative to the beginning of the AAL5 frame, so
6759 * that 0 refers to the beginning of the LE Control
6760 * field, or relative to the beginning of the LAN
6761 * frame, so that 0 refers, for Ethernet LANE, to
6762 * the beginning of the destination address?
6764 s
= gen_abs_offset_varpart(&off_linkhdr
);
6767 * If "s" is non-null, it has code to arrange that the
6768 * X register contains the length of the prefix preceding
6769 * the link-layer header. Add to it the offset computed
6770 * into the register specified by "index", and move that
6771 * into the X register. Otherwise, just load into the X
6772 * register the offset computed into the register specified
6776 sappend(s
, xfer_to_a(inst
));
6777 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6778 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6780 s
= xfer_to_x(inst
);
6783 * Load the item at the sum of the offset we've put in the
6784 * X register and the offset of the start of the link
6785 * layer header (which is 0 if the radio header is
6786 * variable-length; that header length is what we put
6787 * into the X register and then added to the index).
6789 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6790 tmp
->s
.k
= off_linkhdr
.constant_part
;
6792 sappend(inst
->s
, s
);
6806 * The offset is relative to the beginning of
6807 * the network-layer header.
6808 * XXX - are there any cases where we want
6811 s
= gen_abs_offset_varpart(&off_linkpl
);
6814 * If "s" is non-null, it has code to arrange that the
6815 * X register contains the variable part of the offset
6816 * of the link-layer payload. Add to it the offset
6817 * computed into the register specified by "index",
6818 * and move that into the X register. Otherwise, just
6819 * load into the X register the offset computed into
6820 * the register specified by "index".
6823 sappend(s
, xfer_to_a(inst
));
6824 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6825 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6827 s
= xfer_to_x(inst
);
6830 * Load the item at the sum of the offset we've put in the
6831 * X register, the offset of the start of the network
6832 * layer header from the beginning of the link-layer
6833 * payload, and the constant part of the offset of the
6834 * start of the link-layer payload.
6836 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6837 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6839 sappend(inst
->s
, s
);
6842 * Do the computation only if the packet contains
6843 * the protocol in question.
6845 b
= gen_proto_abbrev(proto
);
6847 gen_and(inst
->b
, b
);
6861 * The offset is relative to the beginning of
6862 * the transport-layer header.
6864 * Load the X register with the length of the IPv4 header
6865 * (plus the offset of the link-layer header, if it's
6866 * a variable-length header), in bytes.
6868 * XXX - are there any cases where we want
6870 * XXX - we should, if we're built with
6871 * IPv6 support, generate code to load either
6872 * IPv4, IPv6, or both, as appropriate.
6874 s
= gen_loadx_iphdrlen();
6877 * The X register now contains the sum of the variable
6878 * part of the offset of the link-layer payload and the
6879 * length of the network-layer header.
6881 * Load into the A register the offset relative to
6882 * the beginning of the transport layer header,
6883 * add the X register to that, move that to the
6884 * X register, and load with an offset from the
6885 * X register equal to the sum of the constant part of
6886 * the offset of the link-layer payload and the offset,
6887 * relative to the beginning of the link-layer payload,
6888 * of the network-layer header.
6890 sappend(s
, xfer_to_a(inst
));
6891 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6892 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6893 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6894 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6895 sappend(inst
->s
, s
);
6898 * Do the computation only if the packet contains
6899 * the protocol in question - which is true only
6900 * if this is an IP datagram and is the first or
6901 * only fragment of that datagram.
6903 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6905 gen_and(inst
->b
, b
);
6906 gen_and(gen_proto_abbrev(Q_IP
), b
);
6910 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6913 inst
->regno
= regno
;
6914 s
= new_stmt(BPF_ST
);
6916 sappend(inst
->s
, s
);
6922 gen_relation(code
, a0
, a1
, reversed
)
6924 struct arth
*a0
, *a1
;
6927 struct slist
*s0
, *s1
, *s2
;
6928 struct block
*b
, *tmp
;
6932 if (code
== BPF_JEQ
) {
6933 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6934 b
= new_block(JMP(code
));
6938 b
= new_block(BPF_JMP
|code
|BPF_X
);
6944 sappend(a0
->s
, a1
->s
);
6948 free_reg(a0
->regno
);
6949 free_reg(a1
->regno
);
6951 /* 'and' together protocol checks */
6954 gen_and(a0
->b
, tmp
= a1
->b
);
6970 int regno
= alloc_reg();
6971 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6974 s
= new_stmt(BPF_LD
|BPF_LEN
);
6975 s
->next
= new_stmt(BPF_ST
);
6976 s
->next
->s
.k
= regno
;
6991 a
= (struct arth
*)newchunk(sizeof(*a
));
6995 s
= new_stmt(BPF_LD
|BPF_IMM
);
6997 s
->next
= new_stmt(BPF_ST
);
7013 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7016 s
= new_stmt(BPF_ST
);
7024 gen_arth(code
, a0
, a1
)
7026 struct arth
*a0
, *a1
;
7028 struct slist
*s0
, *s1
, *s2
;
7031 * Disallow division by, or modulus by, zero; we do this here
7032 * so that it gets done even if the optimizer is disabled.
7034 if (code
== BPF_DIV
) {
7035 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7036 bpf_error("division by zero");
7037 } else if (code
== BPF_MOD
) {
7038 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7039 bpf_error("modulus by zero");
7043 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7048 sappend(a0
->s
, a1
->s
);
7050 free_reg(a0
->regno
);
7051 free_reg(a1
->regno
);
7053 s0
= new_stmt(BPF_ST
);
7054 a0
->regno
= s0
->s
.k
= alloc_reg();
7061 * Here we handle simple allocation of the scratch registers.
7062 * If too many registers are alloc'd, the allocator punts.
7064 static int regused
[BPF_MEMWORDS
];
7068 * Initialize the table of used registers and the current register.
7074 memset(regused
, 0, sizeof regused
);
7078 * Return the next free register.
7083 int n
= BPF_MEMWORDS
;
7086 if (regused
[curreg
])
7087 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7089 regused
[curreg
] = 1;
7093 bpf_error("too many registers needed to evaluate expression");
7099 * Return a register to the table so it can
7109 static struct block
*
7116 s
= new_stmt(BPF_LD
|BPF_LEN
);
7117 b
= new_block(JMP(jmp
));
7128 return gen_len(BPF_JGE
, n
);
7132 * Actually, this is less than or equal.
7140 b
= gen_len(BPF_JGT
, n
);
7147 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7148 * the beginning of the link-layer header.
7149 * XXX - that means you can't test values in the radiotap header, but
7150 * as that header is difficult if not impossible to parse generally
7151 * without a loop, that might not be a severe problem. A new keyword
7152 * "radio" could be added for that, although what you'd really want
7153 * would be a way of testing particular radio header values, which
7154 * would generate code appropriate to the radio header in question.
7157 gen_byteop(op
, idx
, val
)
7168 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7171 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7175 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7179 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7183 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7187 b
= new_block(JMP(BPF_JEQ
));
7194 static u_char abroadcast
[] = { 0x0 };
7197 gen_broadcast(proto
)
7200 bpf_u_int32 hostmask
;
7201 struct block
*b0
, *b1
, *b2
;
7202 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7210 case DLT_ARCNET_LINUX
:
7211 return gen_ahostop(abroadcast
, Q_DST
);
7213 case DLT_NETANALYZER
:
7214 case DLT_NETANALYZER_TRANSPARENT
:
7215 b1
= gen_prevlinkhdr_check();
7216 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7221 return gen_fhostop(ebroadcast
, Q_DST
);
7223 return gen_thostop(ebroadcast
, Q_DST
);
7224 case DLT_IEEE802_11
:
7225 case DLT_PRISM_HEADER
:
7226 case DLT_IEEE802_11_RADIO_AVS
:
7227 case DLT_IEEE802_11_RADIO
:
7229 return gen_wlanhostop(ebroadcast
, Q_DST
);
7230 case DLT_IP_OVER_FC
:
7231 return gen_ipfchostop(ebroadcast
, Q_DST
);
7233 bpf_error("not a broadcast link");
7239 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7240 * as an indication that we don't know the netmask, and fail
7243 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7244 bpf_error("netmask not known, so 'ip broadcast' not supported");
7245 b0
= gen_linktype(ETHERTYPE_IP
);
7246 hostmask
= ~netmask
;
7247 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7248 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7249 (bpf_int32
)(~0 & hostmask
), hostmask
);
7254 bpf_error("only link-layer/IP broadcast filters supported");
7260 * Generate code to test the low-order bit of a MAC address (that's
7261 * the bottom bit of the *first* byte).
7263 static struct block
*
7264 gen_mac_multicast(offset
)
7267 register struct block
*b0
;
7268 register struct slist
*s
;
7270 /* link[offset] & 1 != 0 */
7271 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7272 b0
= new_block(JMP(BPF_JSET
));
7279 gen_multicast(proto
)
7282 register struct block
*b0
, *b1
, *b2
;
7283 register struct slist
*s
;
7291 case DLT_ARCNET_LINUX
:
7292 /* all ARCnet multicasts use the same address */
7293 return gen_ahostop(abroadcast
, Q_DST
);
7295 case DLT_NETANALYZER
:
7296 case DLT_NETANALYZER_TRANSPARENT
:
7297 b1
= gen_prevlinkhdr_check();
7298 /* ether[0] & 1 != 0 */
7299 b0
= gen_mac_multicast(0);
7305 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7307 * XXX - was that referring to bit-order issues?
7309 /* fddi[1] & 1 != 0 */
7310 return gen_mac_multicast(1);
7312 /* tr[2] & 1 != 0 */
7313 return gen_mac_multicast(2);
7314 case DLT_IEEE802_11
:
7315 case DLT_PRISM_HEADER
:
7316 case DLT_IEEE802_11_RADIO_AVS
:
7317 case DLT_IEEE802_11_RADIO
:
7322 * For control frames, there is no DA.
7324 * For management frames, DA is at an
7325 * offset of 4 from the beginning of
7328 * For data frames, DA is at an offset
7329 * of 4 from the beginning of the packet
7330 * if To DS is clear and at an offset of
7331 * 16 from the beginning of the packet
7336 * Generate the tests to be done for data frames.
7338 * First, check for To DS set, i.e. "link[1] & 0x01".
7340 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7341 b1
= new_block(JMP(BPF_JSET
));
7342 b1
->s
.k
= 0x01; /* To DS */
7346 * If To DS is set, the DA is at 16.
7348 b0
= gen_mac_multicast(16);
7352 * Now, check for To DS not set, i.e. check
7353 * "!(link[1] & 0x01)".
7355 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7356 b2
= new_block(JMP(BPF_JSET
));
7357 b2
->s
.k
= 0x01; /* To DS */
7362 * If To DS is not set, the DA is at 4.
7364 b1
= gen_mac_multicast(4);
7368 * Now OR together the last two checks. That gives
7369 * the complete set of checks for data frames.
7374 * Now check for a data frame.
7375 * I.e, check "link[0] & 0x08".
7377 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7378 b1
= new_block(JMP(BPF_JSET
));
7383 * AND that with the checks done for data frames.
7388 * If the high-order bit of the type value is 0, this
7389 * is a management frame.
7390 * I.e, check "!(link[0] & 0x08)".
7392 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7393 b2
= new_block(JMP(BPF_JSET
));
7399 * For management frames, the DA is at 4.
7401 b1
= gen_mac_multicast(4);
7405 * OR that with the checks done for data frames.
7406 * That gives the checks done for management and
7412 * If the low-order bit of the type value is 1,
7413 * this is either a control frame or a frame
7414 * with a reserved type, and thus not a
7417 * I.e., check "!(link[0] & 0x04)".
7419 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7420 b1
= new_block(JMP(BPF_JSET
));
7426 * AND that with the checks for data and management
7431 case DLT_IP_OVER_FC
:
7432 b0
= gen_mac_multicast(2);
7437 /* Link not known to support multicasts */
7441 b0
= gen_linktype(ETHERTYPE_IP
);
7442 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7447 b0
= gen_linktype(ETHERTYPE_IPV6
);
7448 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7452 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7458 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7459 * Outbound traffic is sent by this machine, while inbound traffic is
7460 * sent by a remote machine (and may include packets destined for a
7461 * unicast or multicast link-layer address we are not subscribing to).
7462 * These are the same definitions implemented by pcap_setdirection().
7463 * Capturing only unicast traffic destined for this host is probably
7464 * better accomplished using a higher-layer filter.
7470 register struct block
*b0
;
7473 * Only some data link types support inbound/outbound qualifiers.
7477 b0
= gen_relation(BPF_JEQ
,
7478 gen_load(Q_LINK
, gen_loadi(0), 1),
7485 /* match outgoing packets */
7486 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7488 /* match incoming packets */
7489 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7494 /* match outgoing packets */
7495 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7497 /* to filter on inbound traffic, invert the match */
7502 #ifdef HAVE_NET_PFVAR_H
7504 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7505 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7511 /* match outgoing packets */
7512 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7514 /* match incoming packets */
7515 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7519 case DLT_JUNIPER_MFR
:
7520 case DLT_JUNIPER_MLFR
:
7521 case DLT_JUNIPER_MLPPP
:
7522 case DLT_JUNIPER_ATM1
:
7523 case DLT_JUNIPER_ATM2
:
7524 case DLT_JUNIPER_PPPOE
:
7525 case DLT_JUNIPER_PPPOE_ATM
:
7526 case DLT_JUNIPER_GGSN
:
7527 case DLT_JUNIPER_ES
:
7528 case DLT_JUNIPER_MONITOR
:
7529 case DLT_JUNIPER_SERVICES
:
7530 case DLT_JUNIPER_ETHER
:
7531 case DLT_JUNIPER_PPP
:
7532 case DLT_JUNIPER_FRELAY
:
7533 case DLT_JUNIPER_CHDLC
:
7534 case DLT_JUNIPER_VP
:
7535 case DLT_JUNIPER_ST
:
7536 case DLT_JUNIPER_ISM
:
7537 case DLT_JUNIPER_VS
:
7538 case DLT_JUNIPER_SRX_E2E
:
7539 case DLT_JUNIPER_FIBRECHANNEL
:
7540 case DLT_JUNIPER_ATM_CEMIC
:
7542 /* juniper flags (including direction) are stored
7543 * the byte after the 3-byte magic number */
7545 /* match outgoing packets */
7546 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7548 /* match incoming packets */
7549 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7555 * If we have packet meta-data indicating a direction,
7556 * check it, otherwise give up as this link-layer type
7557 * has nothing in the packet data.
7559 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7561 * This is Linux with PF_PACKET support.
7562 * If this is a *live* capture, we can look at
7563 * special meta-data in the filter expression;
7564 * if it's a savefile, we can't.
7566 if (bpf_pcap
->rfile
!= NULL
) {
7567 /* We have a FILE *, so this is a savefile */
7568 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7573 /* match outgoing packets */
7574 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7577 /* to filter on inbound traffic, invert the match */
7580 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7581 bpf_error("inbound/outbound not supported on linktype %d",
7585 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7590 #ifdef HAVE_NET_PFVAR_H
7591 /* PF firewall log matched interface */
7593 gen_pf_ifname(const char *ifname
)
7598 if (linktype
!= DLT_PFLOG
) {
7599 bpf_error("ifname supported only on PF linktype");
7602 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7603 off
= offsetof(struct pfloghdr
, ifname
);
7604 if (strlen(ifname
) >= len
) {
7605 bpf_error("ifname interface names can only be %d characters",
7609 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7613 /* PF firewall log ruleset name */
7615 gen_pf_ruleset(char *ruleset
)
7619 if (linktype
!= DLT_PFLOG
) {
7620 bpf_error("ruleset supported only on PF linktype");
7624 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7625 bpf_error("ruleset names can only be %ld characters",
7626 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7630 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7631 strlen(ruleset
), (const u_char
*)ruleset
);
7635 /* PF firewall log rule number */
7641 if (linktype
!= DLT_PFLOG
) {
7642 bpf_error("rnr supported only on PF linktype");
7646 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7651 /* PF firewall log sub-rule number */
7653 gen_pf_srnr(int srnr
)
7657 if (linktype
!= DLT_PFLOG
) {
7658 bpf_error("srnr supported only on PF linktype");
7662 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7667 /* PF firewall log reason code */
7669 gen_pf_reason(int reason
)
7673 if (linktype
!= DLT_PFLOG
) {
7674 bpf_error("reason supported only on PF linktype");
7678 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7683 /* PF firewall log action */
7685 gen_pf_action(int action
)
7689 if (linktype
!= DLT_PFLOG
) {
7690 bpf_error("action supported only on PF linktype");
7694 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7698 #else /* !HAVE_NET_PFVAR_H */
7700 gen_pf_ifname(const char *ifname
)
7702 bpf_error("libpcap was compiled without pf support");
7708 gen_pf_ruleset(char *ruleset
)
7710 bpf_error("libpcap was compiled on a machine without pf support");
7718 bpf_error("libpcap was compiled on a machine without pf support");
7724 gen_pf_srnr(int srnr
)
7726 bpf_error("libpcap was compiled on a machine without pf support");
7732 gen_pf_reason(int reason
)
7734 bpf_error("libpcap was compiled on a machine without pf support");
7740 gen_pf_action(int action
)
7742 bpf_error("libpcap was compiled on a machine without pf support");
7746 #endif /* HAVE_NET_PFVAR_H */
7748 /* IEEE 802.11 wireless header */
7750 gen_p80211_type(int type
, int mask
)
7756 case DLT_IEEE802_11
:
7757 case DLT_PRISM_HEADER
:
7758 case DLT_IEEE802_11_RADIO_AVS
:
7759 case DLT_IEEE802_11_RADIO
:
7760 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7765 bpf_error("802.11 link-layer types supported only on 802.11");
7773 gen_p80211_fcdir(int fcdir
)
7779 case DLT_IEEE802_11
:
7780 case DLT_PRISM_HEADER
:
7781 case DLT_IEEE802_11_RADIO_AVS
:
7782 case DLT_IEEE802_11_RADIO
:
7786 bpf_error("frame direction supported only with 802.11 headers");
7790 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7791 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7798 register const u_char
*eaddr
;
7804 case DLT_ARCNET_LINUX
:
7805 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7807 return (gen_ahostop(eaddr
, (int)q
.dir
));
7809 bpf_error("ARCnet address used in non-arc expression");
7815 bpf_error("aid supported only on ARCnet");
7818 bpf_error("ARCnet address used in non-arc expression");
7823 static struct block
*
7824 gen_ahostop(eaddr
, dir
)
7825 register const u_char
*eaddr
;
7828 register struct block
*b0
, *b1
;
7831 /* src comes first, different from Ethernet */
7833 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7836 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7839 b0
= gen_ahostop(eaddr
, Q_SRC
);
7840 b1
= gen_ahostop(eaddr
, Q_DST
);
7846 b0
= gen_ahostop(eaddr
, Q_SRC
);
7847 b1
= gen_ahostop(eaddr
, Q_DST
);
7852 bpf_error("'addr1' is only supported on 802.11");
7856 bpf_error("'addr2' is only supported on 802.11");
7860 bpf_error("'addr3' is only supported on 802.11");
7864 bpf_error("'addr4' is only supported on 802.11");
7868 bpf_error("'ra' is only supported on 802.11");
7872 bpf_error("'ta' is only supported on 802.11");
7879 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7880 static struct block
*
7881 gen_vlan_bpf_extensions(int vlan_num
)
7883 struct block
*b0
, *b1
;
7886 /* generate new filter code based on extracting packet
7888 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7889 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7891 b0
= new_block(JMP(BPF_JEQ
));
7895 if (vlan_num
>= 0) {
7896 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7897 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7899 b1
= new_block(JMP(BPF_JEQ
));
7901 b1
->s
.k
= (bpf_int32
) vlan_num
;
7911 static struct block
*
7912 gen_vlan_no_bpf_extensions(int vlan_num
)
7914 struct block
*b0
, *b1
;
7916 /* check for VLAN, including QinQ */
7917 b0
= gen_linktype(ETHERTYPE_8021Q
);
7918 b1
= gen_linktype(ETHERTYPE_8021AD
);
7921 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7925 /* If a specific VLAN is requested, check VLAN id */
7926 if (vlan_num
>= 0) {
7927 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7928 (bpf_int32
)vlan_num
, 0x0fff);
7934 * The payload follows the full header, including the
7935 * VLAN tags, so skip past this VLAN tag.
7937 off_linkpl
.constant_part
+= 4;
7940 * The link-layer type information follows the VLAN tags, so
7941 * skip past this VLAN tag.
7943 off_linktype
.constant_part
+= 4;
7949 * support IEEE 802.1Q VLAN trunk over ethernet
7957 /* can't check for VLAN-encapsulated packets inside MPLS */
7958 if (label_stack_depth
> 0)
7959 bpf_error("no VLAN match after MPLS");
7962 * Check for a VLAN packet, and then change the offsets to point
7963 * to the type and data fields within the VLAN packet. Just
7964 * increment the offsets, so that we can support a hierarchy, e.g.
7965 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7968 * XXX - this is a bit of a kludge. If we were to split the
7969 * compiler into a parser that parses an expression and
7970 * generates an expression tree, and a code generator that
7971 * takes an expression tree (which could come from our
7972 * parser or from some other parser) and generates BPF code,
7973 * we could perhaps make the offsets parameters of routines
7974 * and, in the handler for an "AND" node, pass to subnodes
7975 * other than the VLAN node the adjusted offsets.
7977 * This would mean that "vlan" would, instead of changing the
7978 * behavior of *all* tests after it, change only the behavior
7979 * of tests ANDed with it. That would change the documented
7980 * semantics of "vlan", which might break some expressions.
7981 * However, it would mean that "(vlan and ip) or ip" would check
7982 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7983 * checking only for VLAN-encapsulated IP, so that could still
7984 * be considered worth doing; it wouldn't break expressions
7985 * that are of the form "vlan and ..." or "vlan N and ...",
7986 * which I suspect are the most common expressions involving
7987 * "vlan". "vlan or ..." doesn't necessarily do what the user
7988 * would really want, now, as all the "or ..." tests would
7989 * be done assuming a VLAN, even though the "or" could be viewed
7990 * as meaning "or, if this isn't a VLAN packet...".
7995 case DLT_NETANALYZER
:
7996 case DLT_NETANALYZER_TRANSPARENT
:
7997 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7998 /* Verify that this is the outer part of the packet and
7999 * not encapsulated somehow. */
8000 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
8001 off_linkhdr
.constant_part
==
8002 off_outermostlinkhdr
.constant_part
) {
8004 * Do we need special VLAN handling?
8006 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8007 b0
= gen_vlan_bpf_extensions(vlan_num
);
8009 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8012 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8015 case DLT_IEEE802_11
:
8016 case DLT_PRISM_HEADER
:
8017 case DLT_IEEE802_11_RADIO_AVS
:
8018 case DLT_IEEE802_11_RADIO
:
8019 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8023 bpf_error("no VLAN support for data link type %d",
8040 struct block
*b0
, *b1
;
8042 if (label_stack_depth
> 0) {
8043 /* just match the bottom-of-stack bit clear */
8044 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8047 * We're not in an MPLS stack yet, so check the link-layer
8048 * type against MPLS.
8052 case DLT_C_HDLC
: /* fall through */
8054 case DLT_NETANALYZER
:
8055 case DLT_NETANALYZER_TRANSPARENT
:
8056 b0
= gen_linktype(ETHERTYPE_MPLS
);
8060 b0
= gen_linktype(PPP_MPLS_UCAST
);
8063 /* FIXME add other DLT_s ...
8064 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8065 * leave it for now */
8068 bpf_error("no MPLS support for data link type %d",
8076 /* If a specific MPLS label is requested, check it */
8077 if (label_num
>= 0) {
8078 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8079 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8080 0xfffff000); /* only compare the first 20 bits */
8086 * Change the offsets to point to the type and data fields within
8087 * the MPLS packet. Just increment the offsets, so that we
8088 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8089 * capture packets with an outer label of 100000 and an inner
8092 * Increment the MPLS stack depth as well; this indicates that
8093 * we're checking MPLS-encapsulated headers, to make sure higher
8094 * level code generators don't try to match against IP-related
8095 * protocols such as Q_ARP, Q_RARP etc.
8097 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8101 label_stack_depth
++;
8106 * Support PPPOE discovery and session.
8111 /* check for PPPoE discovery */
8112 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8116 gen_pppoes(sess_num
)
8119 struct block
*b0
, *b1
;
8122 * Test against the PPPoE session link-layer type.
8124 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8126 /* If a specific session is requested, check PPPoE session id */
8127 if (sess_num
>= 0) {
8128 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8129 (bpf_int32
)sess_num
, 0x0000ffff);
8135 * Change the offsets to point to the type and data fields within
8136 * the PPP packet, and note that this is PPPoE rather than
8139 * XXX - this is a bit of a kludge. If we were to split the
8140 * compiler into a parser that parses an expression and
8141 * generates an expression tree, and a code generator that
8142 * takes an expression tree (which could come from our
8143 * parser or from some other parser) and generates BPF code,
8144 * we could perhaps make the offsets parameters of routines
8145 * and, in the handler for an "AND" node, pass to subnodes
8146 * other than the PPPoE node the adjusted offsets.
8148 * This would mean that "pppoes" would, instead of changing the
8149 * behavior of *all* tests after it, change only the behavior
8150 * of tests ANDed with it. That would change the documented
8151 * semantics of "pppoes", which might break some expressions.
8152 * However, it would mean that "(pppoes and ip) or ip" would check
8153 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8154 * checking only for VLAN-encapsulated IP, so that could still
8155 * be considered worth doing; it wouldn't break expressions
8156 * that are of the form "pppoes and ..." which I suspect are the
8157 * most common expressions involving "pppoes". "pppoes or ..."
8158 * doesn't necessarily do what the user would really want, now,
8159 * as all the "or ..." tests would be done assuming PPPoE, even
8160 * though the "or" could be viewed as meaning "or, if this isn't
8161 * a PPPoE packet...".
8163 * The "network-layer" protocol is PPPoE, which has a 6-byte
8164 * PPPoE header, followed by a PPP packet.
8166 * There is no HDLC encapsulation for the PPP packet (it's
8167 * encapsulated in PPPoES instead), so the link-layer type
8168 * starts at the first byte of the PPP packet. For PPPoE,
8169 * that offset is relative to the beginning of the total
8170 * link-layer payload, including any 802.2 LLC header, so
8171 * it's 6 bytes past off_nl.
8173 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8174 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8177 off_linktype
= off_linkhdr
;
8178 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8181 off_nl_nosnap
= 0; /* no 802.2 LLC */
8186 /* Check that this is Geneve and the VNI is correct if
8187 * specified. Parameterized to handle both IPv4 and IPv6. */
8188 static struct block
*
8189 gen_geneve_check(struct block
*(*gen_portfn
)(int, int, int),
8190 enum e_offrel offrel
, int vni
)
8192 struct block
*b0
, *b1
;
8194 b0
= gen_portfn(GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8196 /* Check that we are operating on version 0. Otherwise, we
8197 * can't decode the rest of the fields. The version is 2 bits
8198 * in the first byte of the Geneve header. */
8199 b1
= gen_mcmp(offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8204 vni
<<= 8; /* VNI is in the upper 3 bytes */
8205 b1
= gen_mcmp(offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8214 /* The IPv4 and IPv6 Geneve checks need to do two things:
8215 * - Verify that this actually is Geneve with the right VNI.
8216 * - Place the IP header length (plus variable link prefix if
8217 * needed) into register A to be used later to compute
8218 * the inner packet offsets. */
8219 static struct block
*
8220 gen_geneve4(int vni
)
8222 struct block
*b0
, *b1
;
8223 struct slist
*s
, *s1
;
8225 b0
= gen_geneve_check(gen_port
, OR_TRAN_IPV4
, vni
);
8227 /* Load the IP header length into A. */
8228 s
= gen_loadx_iphdrlen();
8230 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8233 /* Forcibly append these statements to the true condition
8234 * of the protocol check by creating a new block that is
8235 * always true and ANDing them. */
8236 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8245 static struct block
*
8246 gen_geneve6(int vni
)
8248 struct block
*b0
, *b1
;
8249 struct slist
*s
, *s1
;
8251 b0
= gen_geneve_check(gen_port6
, OR_TRAN_IPV6
, vni
);
8253 /* Load the IP header length. We need to account for a
8254 * variable length link prefix if there is one. */
8255 s
= gen_abs_offset_varpart(&off_linkpl
);
8257 s1
= new_stmt(BPF_LD
|BPF_IMM
);
8261 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8265 s
= new_stmt(BPF_LD
|BPF_IMM
);
8269 /* Forcibly append these statements to the true condition
8270 * of the protocol check by creating a new block that is
8271 * always true and ANDing them. */
8272 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8275 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8284 /* We need to store three values based on the Geneve header::
8285 * - The offset of the linktype.
8286 * - The offset of the end of the Geneve header.
8287 * - The offset of the end of the encapsulated MAC header. */
8288 static struct slist
*
8289 gen_geneve_offsets(void)
8291 struct slist
*s
, *s1
, *s_proto
;
8293 /* First we need to calculate the offset of the Geneve header
8294 * itself. This is composed of the IP header previously calculated
8295 * (include any variable link prefix) and stored in A plus the
8296 * fixed sized headers (fixed link prefix, MAC length, and UDP
8298 s
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8299 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 8;
8301 /* Stash this in X since we'll need it later. */
8302 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8305 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8307 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8311 off_linktype
.reg
= alloc_reg();
8312 off_linktype
.is_variable
= 1;
8313 off_linktype
.constant_part
= 0;
8315 s1
= new_stmt(BPF_ST
);
8316 s1
->s
.k
= off_linktype
.reg
;
8319 /* Load the Geneve option length and mask and shift to get the
8320 * number of bytes. It is stored in the first byte of the Geneve
8322 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
8326 s1
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
8330 s1
= new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
8334 /* Add in the rest of the Geneve base header. */
8335 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8339 /* Add the Geneve header length to its offset and store. */
8340 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8344 /* Set the encapsulated type as Ethernet. Even though we may
8345 * not actually have Ethernet inside there are two reasons this
8347 * - The linktype field is always in EtherType format regardless
8348 * of whether it is in Geneve or an inner Ethernet frame.
8349 * - The only link layer that we have specific support for is
8350 * Ethernet. We will confirm that the packet actually is
8351 * Ethernet at runtime before executing these checks. */
8352 PUSH_LINKHDR(DLT_EN10MB
, 1, 0, alloc_reg());
8354 s1
= new_stmt(BPF_ST
);
8355 s1
->s
.k
= off_linkhdr
.reg
;
8358 /* Calculate whether we have an Ethernet header or just raw IP/
8359 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8360 * and linktype by 14 bytes so that the network header can be found
8361 * seamlessly. Otherwise, keep what we've calculated already. */
8363 /* We have a bare jmp so we can't use the optimizer. */
8366 /* Load the EtherType in the Geneve header, 2 bytes in. */
8367 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
8371 /* Load X with the end of the Geneve header. */
8372 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8373 s1
->s
.k
= off_linkhdr
.reg
;
8376 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8377 * end of this check, we should have the total length in X. In
8378 * the non-Ethernet case, it's already there. */
8379 s_proto
= new_stmt(JMP(BPF_JEQ
));
8380 s_proto
->s
.k
= ETHERTYPE_TEB
;
8381 sappend(s
, s_proto
);
8383 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8387 /* Since this is Ethernet, use the EtherType of the payload
8388 * directly as the linktype. Overwrite what we already have. */
8389 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8393 s1
= new_stmt(BPF_ST
);
8394 s1
->s
.k
= off_linktype
.reg
;
8397 /* Advance two bytes further to get the end of the Ethernet
8399 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8403 /* Move the result to X. */
8404 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8407 /* Store the final result of our linkpl calculation. */
8408 off_linkpl
.reg
= alloc_reg();
8409 off_linkpl
.is_variable
= 1;
8410 off_linkpl
.constant_part
= 0;
8412 s1
= new_stmt(BPF_STX
);
8413 s1
->s
.k
= off_linkpl
.reg
;
8422 /* Check to see if this is a Geneve packet. */
8426 struct block
*b0
, *b1
;
8429 b0
= gen_geneve4(vni
);
8430 b1
= gen_geneve6(vni
);
8435 /* Later filters should act on the payload of the Geneve frame,
8436 * update all of the header pointers. Attach this code so that
8437 * it gets executed in the event that the Geneve filter matches. */
8438 s
= gen_geneve_offsets();
8441 sappend(s
, b1
->stmts
);
8451 /* Check that the encapsulated frame has a link layer header
8452 * for Ethernet filters. */
8453 static struct block
*
8454 gen_geneve_ll_check()
8457 struct slist
*s
, *s1
;
8459 /* The easiest way to see if there is a link layer present
8460 * is to check if the link layer header and payload are not
8463 /* Geneve always generates pure variable offsets so we can
8464 * compare only the registers. */
8465 s
= new_stmt(BPF_LD
|BPF_MEM
);
8466 s
->s
.k
= off_linkhdr
.reg
;
8468 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8469 s1
->s
.k
= off_linkpl
.reg
;
8472 b0
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8481 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8493 bpf_error("'vpi' supported only on raw ATM");
8494 if (off_vpi
== (u_int
)-1)
8496 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8502 bpf_error("'vci' supported only on raw ATM");
8503 if (off_vci
== (u_int
)-1)
8505 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8510 if (off_proto
== (u_int
)-1)
8511 abort(); /* XXX - this isn't on FreeBSD */
8512 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8517 if (off_payload
== (u_int
)-1)
8519 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8520 0xffffffff, jtype
, reverse
, jvalue
);
8525 bpf_error("'callref' supported only on raw ATM");
8526 if (off_proto
== (u_int
)-1)
8528 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8529 jtype
, reverse
, jvalue
);
8539 gen_atmtype_abbrev(type
)
8542 struct block
*b0
, *b1
;
8547 /* Get all packets in Meta signalling Circuit */
8549 bpf_error("'metac' supported only on raw ATM");
8550 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8551 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8556 /* Get all packets in Broadcast Circuit*/
8558 bpf_error("'bcc' supported only on raw ATM");
8559 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8560 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8565 /* Get all cells in Segment OAM F4 circuit*/
8567 bpf_error("'oam4sc' supported only on raw ATM");
8568 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8569 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8574 /* Get all cells in End-to-End OAM F4 Circuit*/
8576 bpf_error("'oam4ec' supported only on raw ATM");
8577 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8578 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8583 /* Get all packets in connection Signalling Circuit */
8585 bpf_error("'sc' supported only on raw ATM");
8586 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8587 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8592 /* Get all packets in ILMI Circuit */
8594 bpf_error("'ilmic' supported only on raw ATM");
8595 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8596 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8601 /* Get all LANE packets */
8603 bpf_error("'lane' supported only on raw ATM");
8604 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8607 * Arrange that all subsequent tests assume LANE
8608 * rather than LLC-encapsulated packets, and set
8609 * the offsets appropriately for LANE-encapsulated
8612 * We assume LANE means Ethernet, not Token Ring.
8614 PUSH_LINKHDR(DLT_EN10MB
, 0,
8615 off_payload
+ 2, /* Ethernet header */
8617 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8618 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8619 off_nl
= 0; /* Ethernet II */
8620 off_nl_nosnap
= 3; /* 802.3+802.2 */
8624 /* Get all LLC-encapsulated packets */
8626 bpf_error("'llc' supported only on raw ATM");
8627 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8628 linktype
= prevlinktype
;
8638 * Filtering for MTP2 messages based on li value
8639 * FISU, length is null
8640 * LSSU, length is 1 or 2
8641 * MSU, length is 3 or more
8642 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8645 gen_mtp2type_abbrev(type
)
8648 struct block
*b0
, *b1
;
8653 if ( (linktype
!= DLT_MTP2
) &&
8654 (linktype
!= DLT_ERF
) &&
8655 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8656 bpf_error("'fisu' supported only on MTP2");
8657 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8658 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8662 if ( (linktype
!= DLT_MTP2
) &&
8663 (linktype
!= DLT_ERF
) &&
8664 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8665 bpf_error("'lssu' supported only on MTP2");
8666 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8667 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8672 if ( (linktype
!= DLT_MTP2
) &&
8673 (linktype
!= DLT_ERF
) &&
8674 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8675 bpf_error("'msu' supported only on MTP2");
8676 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8680 if ( (linktype
!= DLT_MTP2
) &&
8681 (linktype
!= DLT_ERF
) &&
8682 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8683 bpf_error("'hfisu' supported only on MTP2_HSL");
8684 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8685 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8689 if ( (linktype
!= DLT_MTP2
) &&
8690 (linktype
!= DLT_ERF
) &&
8691 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8692 bpf_error("'hlssu' supported only on MTP2_HSL");
8693 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8694 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8699 if ( (linktype
!= DLT_MTP2
) &&
8700 (linktype
!= DLT_ERF
) &&
8701 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8702 bpf_error("'hmsu' supported only on MTP2_HSL");
8703 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8713 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8720 bpf_u_int32 val1
, val2
, val3
;
8721 u_int newoff_sio
=off_sio
;
8722 u_int newoff_opc
=off_opc
;
8723 u_int newoff_dpc
=off_dpc
;
8724 u_int newoff_sls
=off_sls
;
8726 switch (mtp3field
) {
8729 newoff_sio
+= 3; /* offset for MTP2_HSL */
8733 if (off_sio
== (u_int
)-1)
8734 bpf_error("'sio' supported only on SS7");
8735 /* sio coded on 1 byte so max value 255 */
8737 bpf_error("sio value %u too big; max value = 255",
8739 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8740 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8746 if (off_opc
== (u_int
)-1)
8747 bpf_error("'opc' supported only on SS7");
8748 /* opc coded on 14 bits so max value 16383 */
8750 bpf_error("opc value %u too big; max value = 16383",
8752 /* the following instructions are made to convert jvalue
8753 * to the form used to write opc in an ss7 message*/
8754 val1
= jvalue
& 0x00003c00;
8756 val2
= jvalue
& 0x000003fc;
8758 val3
= jvalue
& 0x00000003;
8760 jvalue
= val1
+ val2
+ val3
;
8761 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8762 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8770 if (off_dpc
== (u_int
)-1)
8771 bpf_error("'dpc' supported only on SS7");
8772 /* dpc coded on 14 bits so max value 16383 */
8774 bpf_error("dpc value %u too big; max value = 16383",
8776 /* the following instructions are made to convert jvalue
8777 * to the forme used to write dpc in an ss7 message*/
8778 val1
= jvalue
& 0x000000ff;
8780 val2
= jvalue
& 0x00003f00;
8782 jvalue
= val1
+ val2
;
8783 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8784 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8790 if (off_sls
== (u_int
)-1)
8791 bpf_error("'sls' supported only on SS7");
8792 /* sls coded on 4 bits so max value 15 */
8794 bpf_error("sls value %u too big; max value = 15",
8796 /* the following instruction is made to convert jvalue
8797 * to the forme used to write sls in an ss7 message*/
8798 jvalue
= jvalue
<< 4;
8799 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8800 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8809 static struct block
*
8810 gen_msg_abbrev(type
)
8816 * Q.2931 signalling protocol messages for handling virtual circuits
8817 * establishment and teardown
8822 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8826 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8830 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8834 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8838 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8841 case A_RELEASE_DONE
:
8842 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8852 gen_atmmulti_abbrev(type
)
8855 struct block
*b0
, *b1
;
8861 bpf_error("'oam' supported only on raw ATM");
8862 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8867 bpf_error("'oamf4' supported only on raw ATM");
8869 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8870 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8872 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8878 * Get Q.2931 signalling messages for switched
8879 * virtual connection
8882 bpf_error("'connectmsg' supported only on raw ATM");
8883 b0
= gen_msg_abbrev(A_SETUP
);
8884 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8886 b0
= gen_msg_abbrev(A_CONNECT
);
8888 b0
= gen_msg_abbrev(A_CONNECTACK
);
8890 b0
= gen_msg_abbrev(A_RELEASE
);
8892 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8894 b0
= gen_atmtype_abbrev(A_SC
);
8900 bpf_error("'metaconnect' supported only on raw ATM");
8901 b0
= gen_msg_abbrev(A_SETUP
);
8902 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8904 b0
= gen_msg_abbrev(A_CONNECT
);
8906 b0
= gen_msg_abbrev(A_RELEASE
);
8908 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8910 b0
= gen_atmtype_abbrev(A_METAC
);