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.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 * XXX - why was this included even on UNIX?
56 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
76 #include "ethertype.h"
80 #include "ieee80211.h"
82 #include "sunatmpos.h"
85 #include "pcap/ipnet.h"
87 #ifdef HAVE_NET_PFVAR_H
88 #include <sys/socket.h>
90 #include <net/pfvar.h>
91 #include <net/if_pflog.h>
94 #define offsetof(s, e) ((size_t)&((s *)0)->e)
98 #include <netdb.h> /* for "struct addrinfo" */
101 #include <pcap/namedb.h>
103 #define ETHERMTU 1500
106 #define IPPROTO_SCTP 132
109 #ifdef HAVE_OS_PROTO_H
110 #include "os-proto.h"
113 #define JMP(c) ((c)|BPF_JMP|BPF_K)
116 static jmp_buf top_ctx
;
117 static pcap_t
*bpf_pcap
;
119 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
121 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
123 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
128 static int pcap_fddipad
;
133 bpf_error(const char *fmt
, ...)
138 if (bpf_pcap
!= NULL
)
139 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
146 static void init_linktype(pcap_t
*);
148 static void init_regs(void);
149 static int alloc_reg(void);
150 static void free_reg(int);
152 static struct block
*root
;
155 * Value passed to gen_load_a() to indicate what the offset argument
159 OR_PACKET
, /* relative to the beginning of the packet */
160 OR_LINK
, /* relative to the beginning of the link-layer header */
161 OR_MACPL
, /* relative to the end of the MAC-layer header */
162 OR_NET
, /* relative to the network-layer header */
163 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
164 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
165 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
170 * As errors are handled by a longjmp, anything allocated must be freed
171 * in the longjmp handler, so it must be reachable from that handler.
172 * One thing that's allocated is the result of pcap_nametoaddrinfo();
173 * it must be freed with freeaddrinfo(). This variable points to any
174 * addrinfo structure that would need to be freed.
176 static struct addrinfo
*ai
;
180 * We divy out chunks of memory rather than call malloc each time so
181 * we don't have to worry about leaking memory. It's probably
182 * not a big deal if all this memory was wasted but if this ever
183 * goes into a library that would probably not be a good idea.
185 * XXX - this *is* in a library....
188 #define CHUNK0SIZE 1024
194 static struct chunk chunks
[NCHUNKS
];
195 static int cur_chunk
;
197 static void *newchunk(u_int
);
198 static void freechunks(void);
199 static inline struct block
*new_block(int);
200 static inline struct slist
*new_stmt(int);
201 static struct block
*gen_retblk(int);
202 static inline void syntax(void);
204 static void backpatch(struct block
*, struct block
*);
205 static void merge(struct block
*, struct block
*);
206 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
207 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
208 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
209 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
210 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
211 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
213 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
214 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
215 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
216 static struct slist
*gen_load_llrel(u_int
, u_int
);
217 static struct slist
*gen_load_macplrel(u_int
, u_int
);
218 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
219 static struct slist
*gen_loadx_iphdrlen(void);
220 static struct block
*gen_uncond(int);
221 static inline struct block
*gen_true(void);
222 static inline struct block
*gen_false(void);
223 static struct block
*gen_ether_linktype(int);
224 static struct block
*gen_ipnet_linktype(int);
225 static struct block
*gen_linux_sll_linktype(int);
226 static struct slist
*gen_load_prism_llprefixlen(void);
227 static struct slist
*gen_load_avs_llprefixlen(void);
228 static struct slist
*gen_load_radiotap_llprefixlen(void);
229 static struct slist
*gen_load_ppi_llprefixlen(void);
230 static void insert_compute_vloffsets(struct block
*);
231 static struct slist
*gen_llprefixlen(void);
232 static struct slist
*gen_off_macpl(void);
233 static int ethertype_to_ppptype(int);
234 static struct block
*gen_linktype(int);
235 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
236 static struct block
*gen_llc_linktype(int);
237 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
239 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
241 static struct block
*gen_ahostop(const u_char
*, int);
242 static struct block
*gen_ehostop(const u_char
*, int);
243 static struct block
*gen_fhostop(const u_char
*, int);
244 static struct block
*gen_thostop(const u_char
*, int);
245 static struct block
*gen_wlanhostop(const u_char
*, int);
246 static struct block
*gen_ipfchostop(const u_char
*, int);
247 static struct block
*gen_dnhostop(bpf_u_int32
, int);
248 static struct block
*gen_mpls_linktype(int);
249 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
251 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
254 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
256 static struct block
*gen_ipfrag(void);
257 static struct block
*gen_portatom(int, bpf_int32
);
258 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
260 static struct block
*gen_portatom6(int, bpf_int32
);
261 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
263 struct block
*gen_portop(int, int, int);
264 static struct block
*gen_port(int, int, int);
265 struct block
*gen_portrangeop(int, int, int, int);
266 static struct block
*gen_portrange(int, int, int, int);
268 struct block
*gen_portop6(int, int, int);
269 static struct block
*gen_port6(int, int, int);
270 struct block
*gen_portrangeop6(int, int, int, int);
271 static struct block
*gen_portrange6(int, int, int, int);
273 static int lookup_proto(const char *, int);
274 static struct block
*gen_protochain(int, int, int);
275 static struct block
*gen_proto(int, int, int);
276 static struct slist
*xfer_to_x(struct arth
*);
277 static struct slist
*xfer_to_a(struct arth
*);
278 static struct block
*gen_mac_multicast(int);
279 static struct block
*gen_len(int, int);
280 static struct block
*gen_check_802_11_data_frame(void);
282 static struct block
*gen_ppi_dlt_check(void);
283 static struct block
*gen_msg_abbrev(int type
);
294 /* XXX Round up to nearest long. */
295 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
297 /* XXX Round up to structure boundary. */
301 cp
= &chunks
[cur_chunk
];
302 if (n
> cp
->n_left
) {
303 ++cp
, k
= ++cur_chunk
;
305 bpf_error("out of memory");
306 size
= CHUNK0SIZE
<< k
;
307 cp
->m
= (void *)malloc(size
);
309 bpf_error("out of memory");
310 memset((char *)cp
->m
, 0, size
);
313 bpf_error("out of memory");
316 return (void *)((char *)cp
->m
+ cp
->n_left
);
325 for (i
= 0; i
< NCHUNKS
; ++i
)
326 if (chunks
[i
].m
!= NULL
) {
333 * A strdup whose allocations are freed after code generation is over.
337 register const char *s
;
339 int n
= strlen(s
) + 1;
340 char *cp
= newchunk(n
);
346 static inline struct block
*
352 p
= (struct block
*)newchunk(sizeof(*p
));
359 static inline struct slist
*
365 p
= (struct slist
*)newchunk(sizeof(*p
));
371 static struct block
*
375 struct block
*b
= new_block(BPF_RET
|BPF_K
);
384 bpf_error("syntax error in filter expression");
387 static bpf_u_int32 netmask
;
392 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
393 const char *buf
, int optimize
, bpf_u_int32 mask
);
396 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
397 const char *buf
, int optimize
, bpf_u_int32 mask
)
401 EnterCriticalSection(&g_PcapCompileCriticalSection
);
403 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
405 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
411 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
412 const char *buf
, int optimize
, bpf_u_int32 mask
)
415 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
416 const char *buf
, int optimize
, bpf_u_int32 mask
)
420 const char * volatile xbuf
= buf
;
428 if (setjmp(top_ctx
)) {
442 snaplen
= pcap_snapshot(p
);
444 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
445 "snaplen of 0 rejects all packets");
449 lex_init(xbuf
? xbuf
: "");
457 root
= gen_retblk(snaplen
);
459 if (optimize
&& !no_optimize
) {
462 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
463 bpf_error("expression rejects all packets");
465 program
->bf_insns
= icode_to_fcode(root
, &len
);
466 program
->bf_len
= len
;
474 * entry point for using the compiler with no pcap open
475 * pass in all the stuff that is needed explicitly instead.
478 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
479 struct bpf_program
*program
,
480 const char *buf
, int optimize
, bpf_u_int32 mask
)
485 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
488 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
494 * Clean up a "struct bpf_program" by freeing all the memory allocated
498 pcap_freecode(struct bpf_program
*program
)
501 if (program
->bf_insns
!= NULL
) {
502 free((char *)program
->bf_insns
);
503 program
->bf_insns
= NULL
;
508 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
509 * which of the jt and jf fields has been resolved and which is a pointer
510 * back to another unresolved block (or nil). At least one of the fields
511 * in each block is already resolved.
514 backpatch(list
, target
)
515 struct block
*list
, *target
;
532 * Merge the lists in b0 and b1, using the 'sense' field to indicate
533 * which of jt and jf is the link.
537 struct block
*b0
, *b1
;
539 register struct block
**p
= &b0
;
541 /* Find end of list. */
543 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
545 /* Concatenate the lists. */
553 struct block
*ppi_dlt_check
;
556 * Insert before the statements of the first (root) block any
557 * statements needed to load the lengths of any variable-length
558 * headers into registers.
560 * XXX - a fancier strategy would be to insert those before the
561 * statements of all blocks that use those lengths and that
562 * have no predecessors that use them, so that we only compute
563 * the lengths if we need them. There might be even better
564 * approaches than that.
566 * However, those strategies would be more complicated, and
567 * as we don't generate code to compute a length if the
568 * program has no tests that use the length, and as most
569 * tests will probably use those lengths, we would just
570 * postpone computing the lengths so that it's not done
571 * for tests that fail early, and it's not clear that's
574 insert_compute_vloffsets(p
->head
);
577 * For DLT_PPI captures, generate a check of the per-packet
578 * DLT value to make sure it's DLT_IEEE802_11.
580 ppi_dlt_check
= gen_ppi_dlt_check();
581 if (ppi_dlt_check
!= NULL
)
582 gen_and(ppi_dlt_check
, p
);
584 backpatch(p
, gen_retblk(snaplen
));
585 p
->sense
= !p
->sense
;
586 backpatch(p
, gen_retblk(0));
592 struct block
*b0
, *b1
;
594 backpatch(b0
, b1
->head
);
595 b0
->sense
= !b0
->sense
;
596 b1
->sense
= !b1
->sense
;
598 b1
->sense
= !b1
->sense
;
604 struct block
*b0
, *b1
;
606 b0
->sense
= !b0
->sense
;
607 backpatch(b0
, b1
->head
);
608 b0
->sense
= !b0
->sense
;
617 b
->sense
= !b
->sense
;
620 static struct block
*
621 gen_cmp(offrel
, offset
, size
, v
)
622 enum e_offrel offrel
;
626 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
629 static struct block
*
630 gen_cmp_gt(offrel
, offset
, size
, v
)
631 enum e_offrel offrel
;
635 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
638 static struct block
*
639 gen_cmp_ge(offrel
, offset
, size
, v
)
640 enum e_offrel offrel
;
644 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
647 static struct block
*
648 gen_cmp_lt(offrel
, offset
, size
, v
)
649 enum e_offrel offrel
;
653 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
656 static struct block
*
657 gen_cmp_le(offrel
, offset
, size
, v
)
658 enum e_offrel offrel
;
662 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
665 static struct block
*
666 gen_mcmp(offrel
, offset
, size
, v
, mask
)
667 enum e_offrel offrel
;
672 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
675 static struct block
*
676 gen_bcmp(offrel
, offset
, size
, v
)
677 enum e_offrel offrel
;
678 register u_int offset
, size
;
679 register const u_char
*v
;
681 register struct block
*b
, *tmp
;
685 register const u_char
*p
= &v
[size
- 4];
686 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
687 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
689 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
696 register const u_char
*p
= &v
[size
- 2];
697 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
699 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
706 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
715 * AND the field of size "size" at offset "offset" relative to the header
716 * specified by "offrel" with "mask", and compare it with the value "v"
717 * with the test specified by "jtype"; if "reverse" is true, the test
718 * should test the opposite of "jtype".
720 static struct block
*
721 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
722 enum e_offrel offrel
;
724 bpf_u_int32 offset
, size
, mask
, jtype
;
727 struct slist
*s
, *s2
;
730 s
= gen_load_a(offrel
, offset
, size
);
732 if (mask
!= 0xffffffff) {
733 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
738 b
= new_block(JMP(jtype
));
741 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
747 * Various code constructs need to know the layout of the data link
748 * layer. These variables give the necessary offsets from the beginning
749 * of the packet data.
753 * This is the offset of the beginning of the link-layer header from
754 * the beginning of the raw packet data.
756 * It's usually 0, except for 802.11 with a fixed-length radio header.
757 * (For 802.11 with a variable-length radio header, we have to generate
758 * code to compute that offset; off_ll is 0 in that case.)
763 * If there's a variable-length header preceding the link-layer header,
764 * "reg_off_ll" is the register number for a register containing the
765 * length of that header, and therefore the offset of the link-layer
766 * header from the beginning of the raw packet data. Otherwise,
767 * "reg_off_ll" is -1.
769 static int reg_off_ll
;
772 * This is the offset of the beginning of the MAC-layer header from
773 * the beginning of the link-layer header.
774 * It's usually 0, except for ATM LANE, where it's the offset, relative
775 * to the beginning of the raw packet data, of the Ethernet header.
777 static u_int off_mac
;
780 * This is the offset of the beginning of the MAC-layer payload,
781 * from the beginning of the raw packet data.
783 * I.e., it's the sum of the length of the link-layer header (without,
784 * for example, any 802.2 LLC header, so it's the MAC-layer
785 * portion of that header), plus any prefix preceding the
788 static u_int off_macpl
;
791 * This is 1 if the offset of the beginning of the MAC-layer payload
792 * from the beginning of the link-layer header is variable-length.
794 static int off_macpl_is_variable
;
797 * If the link layer has variable_length headers, "reg_off_macpl"
798 * is the register number for a register containing the length of the
799 * link-layer header plus the length of any variable-length header
800 * preceding the link-layer header. Otherwise, "reg_off_macpl"
803 static int reg_off_macpl
;
806 * "off_linktype" is the offset to information in the link-layer header
807 * giving the packet type. This offset is relative to the beginning
808 * of the link-layer header (i.e., it doesn't include off_ll).
810 * For Ethernet, it's the offset of the Ethernet type field.
812 * For link-layer types that always use 802.2 headers, it's the
813 * offset of the LLC header.
815 * For PPP, it's the offset of the PPP type field.
817 * For Cisco HDLC, it's the offset of the CHDLC type field.
819 * For BSD loopback, it's the offset of the AF_ value.
821 * For Linux cooked sockets, it's the offset of the type field.
823 * It's set to -1 for no encapsulation, in which case, IP is assumed.
825 static u_int off_linktype
;
828 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
829 * checks to check the PPP header, assumed to follow a LAN-style link-
830 * layer header and a PPPoE session header.
832 static int is_pppoes
= 0;
835 * TRUE if the link layer includes an ATM pseudo-header.
837 static int is_atm
= 0;
840 * TRUE if "lane" appeared in the filter; it causes us to generate
841 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
843 static int is_lane
= 0;
846 * These are offsets for the ATM pseudo-header.
848 static u_int off_vpi
;
849 static u_int off_vci
;
850 static u_int off_proto
;
853 * These are offsets for the MTP2 fields.
858 * These are offsets for the MTP3 fields.
860 static u_int off_sio
;
861 static u_int off_opc
;
862 static u_int off_dpc
;
863 static u_int off_sls
;
866 * This is the offset of the first byte after the ATM pseudo_header,
867 * or -1 if there is no ATM pseudo-header.
869 static u_int off_payload
;
872 * These are offsets to the beginning of the network-layer header.
873 * They are relative to the beginning of the MAC-layer payload (i.e.,
874 * they don't include off_ll or off_macpl).
876 * If the link layer never uses 802.2 LLC:
878 * "off_nl" and "off_nl_nosnap" are the same.
880 * If the link layer always uses 802.2 LLC:
882 * "off_nl" is the offset if there's a SNAP header following
885 * "off_nl_nosnap" is the offset if there's no SNAP header.
887 * If the link layer is Ethernet:
889 * "off_nl" is the offset if the packet is an Ethernet II packet
890 * (we assume no 802.3+802.2+SNAP);
892 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
893 * with an 802.2 header following it.
896 static u_int off_nl_nosnap
;
904 linktype
= pcap_datalink(p
);
906 pcap_fddipad
= p
->fddipad
;
910 * Assume it's not raw ATM with a pseudo-header, for now.
921 * And that we're not doing PPPoE.
926 * And assume we're not doing SS7.
935 * Also assume it's not 802.11.
939 off_macpl_is_variable
= 0;
943 label_stack_depth
= 0;
953 off_nl
= 0; /* XXX in reality, variable! */
954 off_nl_nosnap
= 0; /* no 802.2 LLC */
957 case DLT_ARCNET_LINUX
:
960 off_nl
= 0; /* XXX in reality, variable! */
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
966 off_macpl
= 14; /* Ethernet header length */
967 off_nl
= 0; /* Ethernet II */
968 off_nl_nosnap
= 3; /* 802.3+802.2 */
973 * SLIP doesn't have a link level type. The 16 byte
974 * header is hacked into our SLIP driver.
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 /* XXX this may be the same as the DLT_PPP_BSDOS case */
988 off_nl_nosnap
= 0; /* no 802.2 LLC */
996 off_nl_nosnap
= 0; /* no 802.2 LLC */
1003 off_nl_nosnap
= 0; /* no 802.2 LLC */
1008 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1009 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1018 * This does no include the Ethernet header, and
1019 * only covers session state.
1024 off_nl_nosnap
= 0; /* no 802.2 LLC */
1031 off_nl_nosnap
= 0; /* no 802.2 LLC */
1036 * FDDI doesn't really have a link-level type field.
1037 * We set "off_linktype" to the offset of the LLC header.
1039 * To check for Ethernet types, we assume that SSAP = SNAP
1040 * is being used and pick out the encapsulated Ethernet type.
1041 * XXX - should we generate code to check for SNAP?
1045 off_linktype
+= pcap_fddipad
;
1047 off_macpl
= 13; /* FDDI MAC header length */
1049 off_macpl
+= pcap_fddipad
;
1051 off_nl
= 8; /* 802.2+SNAP */
1052 off_nl_nosnap
= 3; /* 802.2 */
1057 * Token Ring doesn't really have a link-level type field.
1058 * We set "off_linktype" to the offset of the LLC header.
1060 * To check for Ethernet types, we assume that SSAP = SNAP
1061 * is being used and pick out the encapsulated Ethernet type.
1062 * XXX - should we generate code to check for SNAP?
1064 * XXX - the header is actually variable-length.
1065 * Some various Linux patched versions gave 38
1066 * as "off_linktype" and 40 as "off_nl"; however,
1067 * if a token ring packet has *no* routing
1068 * information, i.e. is not source-routed, the correct
1069 * values are 20 and 22, as they are in the vanilla code.
1071 * A packet is source-routed iff the uppermost bit
1072 * of the first byte of the source address, at an
1073 * offset of 8, has the uppermost bit set. If the
1074 * packet is source-routed, the total number of bytes
1075 * of routing information is 2 plus bits 0x1F00 of
1076 * the 16-bit value at an offset of 14 (shifted right
1077 * 8 - figure out which byte that is).
1080 off_macpl
= 14; /* Token Ring MAC header length */
1081 off_nl
= 8; /* 802.2+SNAP */
1082 off_nl_nosnap
= 3; /* 802.2 */
1085 case DLT_IEEE802_11
:
1086 case DLT_PRISM_HEADER
:
1087 case DLT_IEEE802_11_RADIO_AVS
:
1088 case DLT_IEEE802_11_RADIO
:
1090 * 802.11 doesn't really have a link-level type field.
1091 * We set "off_linktype" to the offset of the LLC header.
1093 * To check for Ethernet types, we assume that SSAP = SNAP
1094 * is being used and pick out the encapsulated Ethernet type.
1095 * XXX - should we generate code to check for SNAP?
1097 * We also handle variable-length radio headers here.
1098 * The Prism header is in theory variable-length, but in
1099 * practice it's always 144 bytes long. However, some
1100 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1101 * sometimes or always supply an AVS header, so we
1102 * have to check whether the radio header is a Prism
1103 * header or an AVS header, so, in practice, it's
1107 off_macpl
= 0; /* link-layer header is variable-length */
1108 off_macpl_is_variable
= 1;
1109 off_nl
= 8; /* 802.2+SNAP */
1110 off_nl_nosnap
= 3; /* 802.2 */
1115 * At the moment we treat PPI the same way that we treat
1116 * normal Radiotap encoded packets. The difference is in
1117 * the function that generates the code at the beginning
1118 * to compute the header length. Since this code generator
1119 * of PPI supports bare 802.11 encapsulation only (i.e.
1120 * the encapsulated DLT should be DLT_IEEE802_11) we
1121 * generate code to check for this too.
1124 off_macpl
= 0; /* link-layer header is variable-length */
1125 off_macpl_is_variable
= 1;
1126 off_nl
= 8; /* 802.2+SNAP */
1127 off_nl_nosnap
= 3; /* 802.2 */
1130 case DLT_ATM_RFC1483
:
1131 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1133 * assume routed, non-ISO PDUs
1134 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1136 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1137 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1138 * latter would presumably be treated the way PPPoE
1139 * should be, so you can do "pppoe and udp port 2049"
1140 * or "pppoa and tcp port 80" and have it check for
1141 * PPPo{A,E} and a PPP protocol of IP and....
1144 off_macpl
= 0; /* packet begins with LLC header */
1145 off_nl
= 8; /* 802.2+SNAP */
1146 off_nl_nosnap
= 3; /* 802.2 */
1151 * Full Frontal ATM; you get AALn PDUs with an ATM
1155 off_vpi
= SUNATM_VPI_POS
;
1156 off_vci
= SUNATM_VCI_POS
;
1157 off_proto
= PROTO_POS
;
1158 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1159 off_payload
= SUNATM_PKT_BEGIN_POS
;
1160 off_linktype
= off_payload
;
1161 off_macpl
= off_payload
; /* if LLC-encapsulated */
1162 off_nl
= 8; /* 802.2+SNAP */
1163 off_nl_nosnap
= 3; /* 802.2 */
1172 off_nl_nosnap
= 0; /* no 802.2 LLC */
1175 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1179 off_nl_nosnap
= 0; /* no 802.2 LLC */
1184 * LocalTalk does have a 1-byte type field in the LLAP header,
1185 * but really it just indicates whether there is a "short" or
1186 * "long" DDP packet following.
1191 off_nl_nosnap
= 0; /* no 802.2 LLC */
1194 case DLT_IP_OVER_FC
:
1196 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1197 * link-level type field. We set "off_linktype" to the
1198 * offset of the LLC header.
1200 * To check for Ethernet types, we assume that SSAP = SNAP
1201 * is being used and pick out the encapsulated Ethernet type.
1202 * XXX - should we generate code to check for SNAP? RFC
1203 * 2625 says SNAP should be used.
1207 off_nl
= 8; /* 802.2+SNAP */
1208 off_nl_nosnap
= 3; /* 802.2 */
1213 * XXX - we should set this to handle SNAP-encapsulated
1214 * frames (NLPID of 0x80).
1219 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 * the only BPF-interesting FRF.16 frames are non-control frames;
1224 * Frame Relay has a variable length link-layer
1225 * so lets start with offset 4 for now and increments later on (FIXME);
1231 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1234 case DLT_APPLE_IP_OVER_IEEE1394
:
1238 off_nl_nosnap
= 0; /* no 802.2 LLC */
1241 case DLT_LINUX_IRDA
:
1243 * Currently, only raw "link[N:M]" filtering is supported.
1253 * Currently, only raw "link[N:M]" filtering is supported.
1261 case DLT_SYMANTEC_FIREWALL
:
1264 off_nl
= 0; /* Ethernet II */
1265 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1268 #ifdef HAVE_NET_PFVAR_H
1271 off_macpl
= PFLOG_HDRLEN
;
1273 off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 case DLT_JUNIPER_MFR
:
1278 case DLT_JUNIPER_MLFR
:
1279 case DLT_JUNIPER_MLPPP
:
1280 case DLT_JUNIPER_PPP
:
1281 case DLT_JUNIPER_CHDLC
:
1282 case DLT_JUNIPER_FRELAY
:
1286 off_nl_nosnap
= -1; /* no 802.2 LLC */
1289 case DLT_JUNIPER_ATM1
:
1290 off_linktype
= 4; /* in reality variable between 4-8 */
1291 off_macpl
= 4; /* in reality variable between 4-8 */
1296 case DLT_JUNIPER_ATM2
:
1297 off_linktype
= 8; /* in reality variable between 8-12 */
1298 off_macpl
= 8; /* in reality variable between 8-12 */
1303 /* frames captured on a Juniper PPPoE service PIC
1304 * contain raw ethernet frames */
1305 case DLT_JUNIPER_PPPOE
:
1306 case DLT_JUNIPER_ETHER
:
1309 off_nl
= 18; /* Ethernet II */
1310 off_nl_nosnap
= 21; /* 802.3+802.2 */
1313 case DLT_JUNIPER_PPPOE_ATM
:
1317 off_nl_nosnap
= -1; /* no 802.2 LLC */
1320 case DLT_JUNIPER_GGSN
:
1324 off_nl_nosnap
= -1; /* no 802.2 LLC */
1327 case DLT_JUNIPER_ES
:
1329 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1330 off_nl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_JUNIPER_MONITOR
:
1337 off_nl
= 0; /* raw IP/IP6 header */
1338 off_nl_nosnap
= -1; /* no 802.2 LLC */
1341 case DLT_JUNIPER_SERVICES
:
1343 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1344 off_nl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl_nosnap
= -1; /* no 802.2 LLC */
1348 case DLT_JUNIPER_VP
:
1355 case DLT_JUNIPER_ST
:
1362 case DLT_JUNIPER_ISM
:
1369 case DLT_JUNIPER_VS
:
1370 case DLT_JUNIPER_SRX_E2E
:
1371 case DLT_JUNIPER_FIBRECHANNEL
:
1372 case DLT_JUNIPER_ATM_CEMIC
:
1391 case DLT_MTP2_WITH_PHDR
:
1424 case DLT_LINUX_LAPD
:
1426 * Currently, only raw "link[N:M]" filtering is supported.
1436 * Currently, only raw "link[N:M]" filtering is supported.
1444 case DLT_BLUETOOTH_HCI_H4
:
1446 * Currently, only raw "link[N:M]" filtering is supported.
1456 * Currently, only raw "link[N:M]" filtering is supported.
1466 * Currently, only raw "link[N:M]" filtering is supported.
1474 case DLT_IEEE802_15_4_LINUX
:
1476 * Currently, only raw "link[N:M]" filtering is supported.
1484 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1486 * Currently, only raw "link[N:M]" filtering is supported.
1494 case DLT_IEEE802_15_4
:
1496 * Currently, only raw "link[N:M]" filtering is supported.
1506 * Currently, only raw "link[N:M]" filtering is supported.
1516 * Currently, only raw "link[N:M]" filtering is supported.
1526 * Currently, only raw "link[N:M]" filtering is supported.
1534 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1536 * Currently, only raw "link[N:M]" filtering is supported.
1546 * Currently, only raw "link[N:M]" filtering is supported.
1548 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1550 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1551 off_nl_nosnap
= -1; /* no 802.2 LLC */
1552 off_mac
= 1; /* step over the kiss length byte */
1555 case DLT_IEEE802_15_4_NONASK_PHY
:
1557 * Currently, only raw "link[N:M]" filtering is supported.
1567 * Currently, only raw "link[N:M]" filtering is supported.
1575 case DLT_USB_LINUX_MMAPPED
:
1577 * Currently, only raw "link[N:M]" filtering is supported.
1585 case DLT_CAN_SOCKETCAN
:
1587 * Currently, only raw "link[N:M]" filtering is supported.
1597 off_macpl
= 24; /* ipnet header length */
1602 case DLT_IEEE802_15_4_NOFCS
:
1604 * Currently, only raw "link[N:M]" filtering is supported.
1614 * Currently, only raw "link[N:M]" filtering is supported.
1622 bpf_error("unknown data link type %d", linktype
);
1627 * Load a value relative to the beginning of the link-layer header.
1628 * The link-layer header doesn't necessarily begin at the beginning
1629 * of the packet data; there might be a variable-length prefix containing
1630 * radio information.
1632 static struct slist
*
1633 gen_load_llrel(offset
, size
)
1636 struct slist
*s
, *s2
;
1638 s
= gen_llprefixlen();
1641 * If "s" is non-null, it has code to arrange that the X register
1642 * contains the length of the prefix preceding the link-layer
1645 * Otherwise, the length of the prefix preceding the link-layer
1646 * header is "off_ll".
1650 * There's a variable-length prefix preceding the
1651 * link-layer header. "s" points to a list of statements
1652 * that put the length of that prefix into the X register.
1653 * do an indirect load, to use the X register as an offset.
1655 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1660 * There is no variable-length header preceding the
1661 * link-layer header; add in off_ll, which, if there's
1662 * a fixed-length header preceding the link-layer header,
1663 * is the length of that header.
1665 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1666 s
->s
.k
= offset
+ off_ll
;
1672 * Load a value relative to the beginning of the MAC-layer payload.
1674 static struct slist
*
1675 gen_load_macplrel(offset
, size
)
1678 struct slist
*s
, *s2
;
1680 s
= gen_off_macpl();
1683 * If s is non-null, the offset of the MAC-layer payload is
1684 * variable, and s points to a list of instructions that
1685 * arrange that the X register contains that offset.
1687 * Otherwise, the offset of the MAC-layer payload is constant,
1688 * and is in off_macpl.
1692 * The offset of the MAC-layer payload is in the X
1693 * register. Do an indirect load, to use the X register
1696 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1701 * The offset of the MAC-layer payload is constant,
1702 * and is in off_macpl; load the value at that offset
1703 * plus the specified offset.
1705 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1706 s
->s
.k
= off_macpl
+ offset
;
1712 * Load a value relative to the beginning of the specified header.
1714 static struct slist
*
1715 gen_load_a(offrel
, offset
, size
)
1716 enum e_offrel offrel
;
1719 struct slist
*s
, *s2
;
1724 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1729 s
= gen_load_llrel(offset
, size
);
1733 s
= gen_load_macplrel(offset
, size
);
1737 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1741 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1746 * Load the X register with the length of the IPv4 header
1747 * (plus the offset of the link-layer header, if it's
1748 * preceded by a variable-length header such as a radio
1749 * header), in bytes.
1751 s
= gen_loadx_iphdrlen();
1754 * Load the item at {offset of the MAC-layer payload} +
1755 * {offset, relative to the start of the MAC-layer
1756 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1757 * {specified offset}.
1759 * (If the offset of the MAC-layer payload is variable,
1760 * it's included in the value in the X register, and
1763 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1764 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1769 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1780 * Generate code to load into the X register the sum of the length of
1781 * the IPv4 header and any variable-length header preceding the link-layer
1784 static struct slist
*
1785 gen_loadx_iphdrlen()
1787 struct slist
*s
, *s2
;
1789 s
= gen_off_macpl();
1792 * There's a variable-length prefix preceding the
1793 * link-layer header, or the link-layer header is itself
1794 * variable-length. "s" points to a list of statements
1795 * that put the offset of the MAC-layer payload into
1798 * The 4*([k]&0xf) addressing mode can't be used, as we
1799 * don't have a constant offset, so we have to load the
1800 * value in question into the A register and add to it
1801 * the value from the X register.
1803 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1806 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1809 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1814 * The A register now contains the length of the
1815 * IP header. We need to add to it the offset of
1816 * the MAC-layer payload, which is still in the X
1817 * register, and move the result into the X register.
1819 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1820 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1823 * There is no variable-length header preceding the
1824 * link-layer header, and the link-layer header is
1825 * fixed-length; load the length of the IPv4 header,
1826 * which is at an offset of off_nl from the beginning
1827 * of the MAC-layer payload, and thus at an offset
1828 * of off_mac_pl + off_nl from the beginning of the
1831 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1832 s
->s
.k
= off_macpl
+ off_nl
;
1837 static struct block
*
1844 s
= new_stmt(BPF_LD
|BPF_IMM
);
1846 b
= new_block(JMP(BPF_JEQ
));
1852 static inline struct block
*
1855 return gen_uncond(1);
1858 static inline struct block
*
1861 return gen_uncond(0);
1865 * Byte-swap a 32-bit number.
1866 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1867 * big-endian platforms.)
1869 #define SWAPLONG(y) \
1870 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1873 * Generate code to match a particular packet type.
1875 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1876 * value, if <= ETHERMTU. We use that to determine whether to
1877 * match the type/length field or to check the type/length field for
1878 * a value <= ETHERMTU to see whether it's a type field and then do
1879 * the appropriate test.
1881 static struct block
*
1882 gen_ether_linktype(proto
)
1885 struct block
*b0
, *b1
;
1891 case LLCSAP_NETBEUI
:
1893 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1894 * so we check the DSAP and SSAP.
1896 * LLCSAP_IP checks for IP-over-802.2, rather
1897 * than IP-over-Ethernet or IP-over-SNAP.
1899 * XXX - should we check both the DSAP and the
1900 * SSAP, like this, or should we check just the
1901 * DSAP, as we do for other types <= ETHERMTU
1902 * (i.e., other SAP values)?
1904 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1906 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1907 ((proto
<< 8) | proto
));
1915 * Ethernet_II frames, which are Ethernet
1916 * frames with a frame type of ETHERTYPE_IPX;
1918 * Ethernet_802.3 frames, which are 802.3
1919 * frames (i.e., the type/length field is
1920 * a length field, <= ETHERMTU, rather than
1921 * a type field) with the first two bytes
1922 * after the Ethernet/802.3 header being
1925 * Ethernet_802.2 frames, which are 802.3
1926 * frames with an 802.2 LLC header and
1927 * with the IPX LSAP as the DSAP in the LLC
1930 * Ethernet_SNAP frames, which are 802.3
1931 * frames with an LLC header and a SNAP
1932 * header and with an OUI of 0x000000
1933 * (encapsulated Ethernet) and a protocol
1934 * ID of ETHERTYPE_IPX in the SNAP header.
1936 * XXX - should we generate the same code both
1937 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1941 * This generates code to check both for the
1942 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1944 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1945 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1949 * Now we add code to check for SNAP frames with
1950 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1952 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1956 * Now we generate code to check for 802.3
1957 * frames in general.
1959 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1963 * Now add the check for 802.3 frames before the
1964 * check for Ethernet_802.2 and Ethernet_802.3,
1965 * as those checks should only be done on 802.3
1966 * frames, not on Ethernet frames.
1971 * Now add the check for Ethernet_II frames, and
1972 * do that before checking for the other frame
1975 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1976 (bpf_int32
)ETHERTYPE_IPX
);
1980 case ETHERTYPE_ATALK
:
1981 case ETHERTYPE_AARP
:
1983 * EtherTalk (AppleTalk protocols on Ethernet link
1984 * layer) may use 802.2 encapsulation.
1988 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1989 * we check for an Ethernet type field less than
1990 * 1500, which means it's an 802.3 length field.
1992 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1996 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1997 * SNAP packets with an organization code of
1998 * 0x080007 (Apple, for Appletalk) and a protocol
1999 * type of ETHERTYPE_ATALK (Appletalk).
2001 * 802.2-encapsulated ETHERTYPE_AARP packets are
2002 * SNAP packets with an organization code of
2003 * 0x000000 (encapsulated Ethernet) and a protocol
2004 * type of ETHERTYPE_AARP (Appletalk ARP).
2006 if (proto
== ETHERTYPE_ATALK
)
2007 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2008 else /* proto == ETHERTYPE_AARP */
2009 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2013 * Check for Ethernet encapsulation (Ethertalk
2014 * phase 1?); we just check for the Ethernet
2017 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2023 if (proto
<= ETHERMTU
) {
2025 * This is an LLC SAP value, so the frames
2026 * that match would be 802.2 frames.
2027 * Check that the frame is an 802.2 frame
2028 * (i.e., that the length/type field is
2029 * a length field, <= ETHERMTU) and
2030 * then check the DSAP.
2032 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
2034 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
2040 * This is an Ethernet type, so compare
2041 * the length/type field with it (if
2042 * the frame is an 802.2 frame, the length
2043 * field will be <= ETHERMTU, and, as
2044 * "proto" is > ETHERMTU, this test
2045 * will fail and the frame won't match,
2046 * which is what we want).
2048 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2055 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2056 * or IPv6 then we have an error.
2058 static struct block
*
2059 gen_ipnet_linktype(proto
)
2065 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2066 (bpf_int32
)IPH_AF_INET
);
2069 case ETHERTYPE_IPV6
:
2070 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2071 (bpf_int32
)IPH_AF_INET6
);
2082 * Generate code to match a particular packet type.
2084 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2085 * value, if <= ETHERMTU. We use that to determine whether to
2086 * match the type field or to check the type field for the special
2087 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2089 static struct block
*
2090 gen_linux_sll_linktype(proto
)
2093 struct block
*b0
, *b1
;
2099 case LLCSAP_NETBEUI
:
2101 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2102 * so we check the DSAP and SSAP.
2104 * LLCSAP_IP checks for IP-over-802.2, rather
2105 * than IP-over-Ethernet or IP-over-SNAP.
2107 * XXX - should we check both the DSAP and the
2108 * SSAP, like this, or should we check just the
2109 * DSAP, as we do for other types <= ETHERMTU
2110 * (i.e., other SAP values)?
2112 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2113 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2114 ((proto
<< 8) | proto
));
2120 * Ethernet_II frames, which are Ethernet
2121 * frames with a frame type of ETHERTYPE_IPX;
2123 * Ethernet_802.3 frames, which have a frame
2124 * type of LINUX_SLL_P_802_3;
2126 * Ethernet_802.2 frames, which are 802.3
2127 * frames with an 802.2 LLC header (i.e, have
2128 * a frame type of LINUX_SLL_P_802_2) and
2129 * with the IPX LSAP as the DSAP in the LLC
2132 * Ethernet_SNAP frames, which are 802.3
2133 * frames with an LLC header and a SNAP
2134 * header and with an OUI of 0x000000
2135 * (encapsulated Ethernet) and a protocol
2136 * ID of ETHERTYPE_IPX in the SNAP header.
2138 * First, do the checks on LINUX_SLL_P_802_2
2139 * frames; generate the check for either
2140 * Ethernet_802.2 or Ethernet_SNAP frames, and
2141 * then put a check for LINUX_SLL_P_802_2 frames
2144 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2145 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2147 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2151 * Now check for 802.3 frames and OR that with
2152 * the previous test.
2154 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2158 * Now add the check for Ethernet_II frames, and
2159 * do that before checking for the other frame
2162 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2163 (bpf_int32
)ETHERTYPE_IPX
);
2167 case ETHERTYPE_ATALK
:
2168 case ETHERTYPE_AARP
:
2170 * EtherTalk (AppleTalk protocols on Ethernet link
2171 * layer) may use 802.2 encapsulation.
2175 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2176 * we check for the 802.2 protocol type in the
2177 * "Ethernet type" field.
2179 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2182 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2183 * SNAP packets with an organization code of
2184 * 0x080007 (Apple, for Appletalk) and a protocol
2185 * type of ETHERTYPE_ATALK (Appletalk).
2187 * 802.2-encapsulated ETHERTYPE_AARP packets are
2188 * SNAP packets with an organization code of
2189 * 0x000000 (encapsulated Ethernet) and a protocol
2190 * type of ETHERTYPE_AARP (Appletalk ARP).
2192 if (proto
== ETHERTYPE_ATALK
)
2193 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2194 else /* proto == ETHERTYPE_AARP */
2195 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2199 * Check for Ethernet encapsulation (Ethertalk
2200 * phase 1?); we just check for the Ethernet
2203 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2209 if (proto
<= ETHERMTU
) {
2211 * This is an LLC SAP value, so the frames
2212 * that match would be 802.2 frames.
2213 * Check for the 802.2 protocol type
2214 * in the "Ethernet type" field, and
2215 * then check the DSAP.
2217 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2219 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2225 * This is an Ethernet type, so compare
2226 * the length/type field with it (if
2227 * the frame is an 802.2 frame, the length
2228 * field will be <= ETHERMTU, and, as
2229 * "proto" is > ETHERMTU, this test
2230 * will fail and the frame won't match,
2231 * which is what we want).
2233 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2239 static struct slist
*
2240 gen_load_prism_llprefixlen()
2242 struct slist
*s1
, *s2
;
2243 struct slist
*sjeq_avs_cookie
;
2244 struct slist
*sjcommon
;
2247 * This code is not compatible with the optimizer, as
2248 * we are generating jmp instructions within a normal
2249 * slist of instructions
2254 * Generate code to load the length of the radio header into
2255 * the register assigned to hold that length, if one has been
2256 * assigned. (If one hasn't been assigned, no code we've
2257 * generated uses that prefix, so we don't need to generate any
2260 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2261 * or always use the AVS header rather than the Prism header.
2262 * We load a 4-byte big-endian value at the beginning of the
2263 * raw packet data, and see whether, when masked with 0xFFFFF000,
2264 * it's equal to 0x80211000. If so, that indicates that it's
2265 * an AVS header (the masked-out bits are the version number).
2266 * Otherwise, it's a Prism header.
2268 * XXX - the Prism header is also, in theory, variable-length,
2269 * but no known software generates headers that aren't 144
2272 if (reg_off_ll
!= -1) {
2276 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2280 * AND it with 0xFFFFF000.
2282 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2283 s2
->s
.k
= 0xFFFFF000;
2287 * Compare with 0x80211000.
2289 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2290 sjeq_avs_cookie
->s
.k
= 0x80211000;
2291 sappend(s1
, sjeq_avs_cookie
);
2296 * The 4 bytes at an offset of 4 from the beginning of
2297 * the AVS header are the length of the AVS header.
2298 * That field is big-endian.
2300 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2303 sjeq_avs_cookie
->s
.jt
= s2
;
2306 * Now jump to the code to allocate a register
2307 * into which to save the header length and
2308 * store the length there. (The "jump always"
2309 * instruction needs to have the k field set;
2310 * it's added to the PC, so, as we're jumping
2311 * over a single instruction, it should be 1.)
2313 sjcommon
= new_stmt(JMP(BPF_JA
));
2315 sappend(s1
, sjcommon
);
2318 * Now for the code that handles the Prism header.
2319 * Just load the length of the Prism header (144)
2320 * into the A register. Have the test for an AVS
2321 * header branch here if we don't have an AVS header.
2323 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2326 sjeq_avs_cookie
->s
.jf
= s2
;
2329 * Now allocate a register to hold that value and store
2330 * it. The code for the AVS header will jump here after
2331 * loading the length of the AVS header.
2333 s2
= new_stmt(BPF_ST
);
2334 s2
->s
.k
= reg_off_ll
;
2336 sjcommon
->s
.jf
= s2
;
2339 * Now move it into the X register.
2341 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2349 static struct slist
*
2350 gen_load_avs_llprefixlen()
2352 struct slist
*s1
, *s2
;
2355 * Generate code to load the length of the AVS header into
2356 * the register assigned to hold that length, if one has been
2357 * assigned. (If one hasn't been assigned, no code we've
2358 * generated uses that prefix, so we don't need to generate any
2361 if (reg_off_ll
!= -1) {
2363 * The 4 bytes at an offset of 4 from the beginning of
2364 * the AVS header are the length of the AVS header.
2365 * That field is big-endian.
2367 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2371 * Now allocate a register to hold that value and store
2374 s2
= new_stmt(BPF_ST
);
2375 s2
->s
.k
= reg_off_ll
;
2379 * Now move it into the X register.
2381 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2389 static struct slist
*
2390 gen_load_radiotap_llprefixlen()
2392 struct slist
*s1
, *s2
;
2395 * Generate code to load the length of the radiotap header into
2396 * the register assigned to hold that length, if one has been
2397 * assigned. (If one hasn't been assigned, no code we've
2398 * generated uses that prefix, so we don't need to generate any
2401 if (reg_off_ll
!= -1) {
2403 * The 2 bytes at offsets of 2 and 3 from the beginning
2404 * of the radiotap header are the length of the radiotap
2405 * header; unfortunately, it's little-endian, so we have
2406 * to load it a byte at a time and construct the value.
2410 * Load the high-order byte, at an offset of 3, shift it
2411 * left a byte, and put the result in the X register.
2413 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2415 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2418 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2422 * Load the next byte, at an offset of 2, and OR the
2423 * value from the X register into it.
2425 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2428 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2432 * Now allocate a register to hold that value and store
2435 s2
= new_stmt(BPF_ST
);
2436 s2
->s
.k
= reg_off_ll
;
2440 * Now move it into the X register.
2442 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2451 * At the moment we treat PPI as normal Radiotap encoded
2452 * packets. The difference is in the function that generates
2453 * the code at the beginning to compute the header length.
2454 * Since this code generator of PPI supports bare 802.11
2455 * encapsulation only (i.e. the encapsulated DLT should be
2456 * DLT_IEEE802_11) we generate code to check for this too;
2457 * that's done in finish_parse().
2459 static struct slist
*
2460 gen_load_ppi_llprefixlen()
2462 struct slist
*s1
, *s2
;
2465 * Generate code to load the length of the radiotap header
2466 * into the register assigned to hold that length, if one has
2469 if (reg_off_ll
!= -1) {
2471 * The 2 bytes at offsets of 2 and 3 from the beginning
2472 * of the radiotap header are the length of the radiotap
2473 * header; unfortunately, it's little-endian, so we have
2474 * to load it a byte at a time and construct the value.
2478 * Load the high-order byte, at an offset of 3, shift it
2479 * left a byte, and put the result in the X register.
2481 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2483 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2486 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2490 * Load the next byte, at an offset of 2, and OR the
2491 * value from the X register into it.
2493 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2496 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2500 * Now allocate a register to hold that value and store
2503 s2
= new_stmt(BPF_ST
);
2504 s2
->s
.k
= reg_off_ll
;
2508 * Now move it into the X register.
2510 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2519 * Load a value relative to the beginning of the link-layer header after the 802.11
2520 * header, i.e. LLC_SNAP.
2521 * The link-layer header doesn't necessarily begin at the beginning
2522 * of the packet data; there might be a variable-length prefix containing
2523 * radio information.
2525 static struct slist
*
2526 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2529 struct slist
*sjset_data_frame_1
;
2530 struct slist
*sjset_data_frame_2
;
2531 struct slist
*sjset_qos
;
2532 struct slist
*sjset_radiotap_flags
;
2533 struct slist
*sjset_radiotap_tsft
;
2534 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2535 struct slist
*s_roundup
;
2537 if (reg_off_macpl
== -1) {
2539 * No register has been assigned to the offset of
2540 * the MAC-layer payload, which means nobody needs
2541 * it; don't bother computing it - just return
2542 * what we already have.
2548 * This code is not compatible with the optimizer, as
2549 * we are generating jmp instructions within a normal
2550 * slist of instructions
2555 * If "s" is non-null, it has code to arrange that the X register
2556 * contains the length of the prefix preceding the link-layer
2559 * Otherwise, the length of the prefix preceding the link-layer
2560 * header is "off_ll".
2564 * There is no variable-length header preceding the
2565 * link-layer header.
2567 * Load the length of the fixed-length prefix preceding
2568 * the link-layer header (if any) into the X register,
2569 * and store it in the reg_off_macpl register.
2570 * That length is off_ll.
2572 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2577 * The X register contains the offset of the beginning of the
2578 * link-layer header; add 24, which is the minimum length
2579 * of the MAC header for a data frame, to that, and store it
2580 * in reg_off_macpl, and then load the Frame Control field,
2581 * which is at the offset in the X register, with an indexed load.
2583 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2585 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2588 s2
= new_stmt(BPF_ST
);
2589 s2
->s
.k
= reg_off_macpl
;
2592 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2597 * Check the Frame Control field to see if this is a data frame;
2598 * a data frame has the 0x08 bit (b3) in that field set and the
2599 * 0x04 bit (b2) clear.
2601 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2602 sjset_data_frame_1
->s
.k
= 0x08;
2603 sappend(s
, sjset_data_frame_1
);
2606 * If b3 is set, test b2, otherwise go to the first statement of
2607 * the rest of the program.
2609 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2610 sjset_data_frame_2
->s
.k
= 0x04;
2611 sappend(s
, sjset_data_frame_2
);
2612 sjset_data_frame_1
->s
.jf
= snext
;
2615 * If b2 is not set, this is a data frame; test the QoS bit.
2616 * Otherwise, go to the first statement of the rest of the
2619 sjset_data_frame_2
->s
.jt
= snext
;
2620 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2621 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2622 sappend(s
, sjset_qos
);
2625 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2627 * Otherwise, go to the first statement of the rest of the
2630 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2631 s2
->s
.k
= reg_off_macpl
;
2633 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2636 s2
= new_stmt(BPF_ST
);
2637 s2
->s
.k
= reg_off_macpl
;
2641 * If we have a radiotap header, look at it to see whether
2642 * there's Atheros padding between the MAC-layer header
2645 * Note: all of the fields in the radiotap header are
2646 * little-endian, so we byte-swap all of the values
2647 * we test against, as they will be loaded as big-endian
2650 if (linktype
== DLT_IEEE802_11_RADIO
) {
2652 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2653 * in the presence flag?
2655 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2659 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2660 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2661 sappend(s
, sjset_radiotap_flags
);
2664 * If not, skip all of this.
2666 sjset_radiotap_flags
->s
.jf
= snext
;
2669 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2671 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2672 new_stmt(JMP(BPF_JSET
));
2673 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2674 sappend(s
, sjset_radiotap_tsft
);
2677 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2678 * at an offset of 16 from the beginning of the raw packet
2679 * data (8 bytes for the radiotap header and 8 bytes for
2682 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2685 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2689 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2690 sjset_tsft_datapad
->s
.k
= 0x20;
2691 sappend(s
, sjset_tsft_datapad
);
2694 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2695 * at an offset of 8 from the beginning of the raw packet
2696 * data (8 bytes for the radiotap header).
2698 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2701 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2705 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2706 sjset_notsft_datapad
->s
.k
= 0x20;
2707 sappend(s
, sjset_notsft_datapad
);
2710 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2711 * set, round the length of the 802.11 header to
2712 * a multiple of 4. Do that by adding 3 and then
2713 * dividing by and multiplying by 4, which we do by
2716 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2717 s_roundup
->s
.k
= reg_off_macpl
;
2718 sappend(s
, s_roundup
);
2719 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2722 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2725 s2
= new_stmt(BPF_ST
);
2726 s2
->s
.k
= reg_off_macpl
;
2729 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2730 sjset_tsft_datapad
->s
.jf
= snext
;
2731 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2732 sjset_notsft_datapad
->s
.jf
= snext
;
2734 sjset_qos
->s
.jf
= snext
;
2740 insert_compute_vloffsets(b
)
2746 * For link-layer types that have a variable-length header
2747 * preceding the link-layer header, generate code to load
2748 * the offset of the link-layer header into the register
2749 * assigned to that offset, if any.
2753 case DLT_PRISM_HEADER
:
2754 s
= gen_load_prism_llprefixlen();
2757 case DLT_IEEE802_11_RADIO_AVS
:
2758 s
= gen_load_avs_llprefixlen();
2761 case DLT_IEEE802_11_RADIO
:
2762 s
= gen_load_radiotap_llprefixlen();
2766 s
= gen_load_ppi_llprefixlen();
2775 * For link-layer types that have a variable-length link-layer
2776 * header, generate code to load the offset of the MAC-layer
2777 * payload into the register assigned to that offset, if any.
2781 case DLT_IEEE802_11
:
2782 case DLT_PRISM_HEADER
:
2783 case DLT_IEEE802_11_RADIO_AVS
:
2784 case DLT_IEEE802_11_RADIO
:
2786 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2791 * If we have any offset-loading code, append all the
2792 * existing statements in the block to those statements,
2793 * and make the resulting list the list of statements
2797 sappend(s
, b
->stmts
);
2802 static struct block
*
2803 gen_ppi_dlt_check(void)
2805 struct slist
*s_load_dlt
;
2808 if (linktype
== DLT_PPI
)
2810 /* Create the statements that check for the DLT
2812 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2813 s_load_dlt
->s
.k
= 4;
2815 b
= new_block(JMP(BPF_JEQ
));
2817 b
->stmts
= s_load_dlt
;
2818 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2828 static struct slist
*
2829 gen_prism_llprefixlen(void)
2833 if (reg_off_ll
== -1) {
2835 * We haven't yet assigned a register for the length
2836 * of the radio header; allocate one.
2838 reg_off_ll
= alloc_reg();
2842 * Load the register containing the radio length
2843 * into the X register.
2845 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2846 s
->s
.k
= reg_off_ll
;
2850 static struct slist
*
2851 gen_avs_llprefixlen(void)
2855 if (reg_off_ll
== -1) {
2857 * We haven't yet assigned a register for the length
2858 * of the AVS header; allocate one.
2860 reg_off_ll
= alloc_reg();
2864 * Load the register containing the AVS length
2865 * into the X register.
2867 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2868 s
->s
.k
= reg_off_ll
;
2872 static struct slist
*
2873 gen_radiotap_llprefixlen(void)
2877 if (reg_off_ll
== -1) {
2879 * We haven't yet assigned a register for the length
2880 * of the radiotap header; allocate one.
2882 reg_off_ll
= alloc_reg();
2886 * Load the register containing the radiotap length
2887 * into the X register.
2889 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2890 s
->s
.k
= reg_off_ll
;
2895 * At the moment we treat PPI as normal Radiotap encoded
2896 * packets. The difference is in the function that generates
2897 * the code at the beginning to compute the header length.
2898 * Since this code generator of PPI supports bare 802.11
2899 * encapsulation only (i.e. the encapsulated DLT should be
2900 * DLT_IEEE802_11) we generate code to check for this too.
2902 static struct slist
*
2903 gen_ppi_llprefixlen(void)
2907 if (reg_off_ll
== -1) {
2909 * We haven't yet assigned a register for the length
2910 * of the radiotap header; allocate one.
2912 reg_off_ll
= alloc_reg();
2916 * Load the register containing the PPI length
2917 * into the X register.
2919 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2920 s
->s
.k
= reg_off_ll
;
2925 * Generate code to compute the link-layer header length, if necessary,
2926 * putting it into the X register, and to return either a pointer to a
2927 * "struct slist" for the list of statements in that code, or NULL if
2928 * no code is necessary.
2930 static struct slist
*
2931 gen_llprefixlen(void)
2935 case DLT_PRISM_HEADER
:
2936 return gen_prism_llprefixlen();
2938 case DLT_IEEE802_11_RADIO_AVS
:
2939 return gen_avs_llprefixlen();
2941 case DLT_IEEE802_11_RADIO
:
2942 return gen_radiotap_llprefixlen();
2945 return gen_ppi_llprefixlen();
2953 * Generate code to load the register containing the offset of the
2954 * MAC-layer payload into the X register; if no register for that offset
2955 * has been allocated, allocate it first.
2957 static struct slist
*
2962 if (off_macpl_is_variable
) {
2963 if (reg_off_macpl
== -1) {
2965 * We haven't yet assigned a register for the offset
2966 * of the MAC-layer payload; allocate one.
2968 reg_off_macpl
= alloc_reg();
2972 * Load the register containing the offset of the MAC-layer
2973 * payload into the X register.
2975 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2976 s
->s
.k
= reg_off_macpl
;
2980 * That offset isn't variable, so we don't need to
2981 * generate any code.
2988 * Map an Ethernet type to the equivalent PPP type.
2991 ethertype_to_ppptype(proto
)
3001 case ETHERTYPE_IPV6
:
3010 case ETHERTYPE_ATALK
:
3024 * I'm assuming the "Bridging PDU"s that go
3025 * over PPP are Spanning Tree Protocol
3039 * Generate code to match a particular packet type by matching the
3040 * link-layer type field or fields in the 802.2 LLC header.
3042 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3043 * value, if <= ETHERMTU.
3045 static struct block
*
3049 struct block
*b0
, *b1
, *b2
;
3051 /* are we checking MPLS-encapsulated packets? */
3052 if (label_stack_depth
> 0) {
3056 /* FIXME add other L3 proto IDs */
3057 return gen_mpls_linktype(Q_IP
);
3059 case ETHERTYPE_IPV6
:
3061 /* FIXME add other L3 proto IDs */
3062 return gen_mpls_linktype(Q_IPV6
);
3065 bpf_error("unsupported protocol over mpls");
3071 * Are we testing PPPoE packets?
3075 * The PPPoE session header is part of the
3076 * MAC-layer payload, so all references
3077 * should be relative to the beginning of
3082 * We use Ethernet protocol types inside libpcap;
3083 * map them to the corresponding PPP protocol types.
3085 proto
= ethertype_to_ppptype(proto
);
3086 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3092 return gen_ether_linktype(proto
);
3100 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3104 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3111 case DLT_IEEE802_11
:
3112 case DLT_PRISM_HEADER
:
3113 case DLT_IEEE802_11_RADIO_AVS
:
3114 case DLT_IEEE802_11_RADIO
:
3117 * Check that we have a data frame.
3119 b0
= gen_check_802_11_data_frame();
3122 * Now check for the specified link-layer type.
3124 b1
= gen_llc_linktype(proto
);
3132 * XXX - check for asynchronous frames, as per RFC 1103.
3134 return gen_llc_linktype(proto
);
3140 * XXX - check for LLC PDUs, as per IEEE 802.5.
3142 return gen_llc_linktype(proto
);
3146 case DLT_ATM_RFC1483
:
3148 case DLT_IP_OVER_FC
:
3149 return gen_llc_linktype(proto
);
3155 * If "is_lane" is set, check for a LANE-encapsulated
3156 * version of this protocol, otherwise check for an
3157 * LLC-encapsulated version of this protocol.
3159 * We assume LANE means Ethernet, not Token Ring.
3163 * Check that the packet doesn't begin with an
3164 * LE Control marker. (We've already generated
3167 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3172 * Now generate an Ethernet test.
3174 b1
= gen_ether_linktype(proto
);
3179 * Check for LLC encapsulation and then check the
3182 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3183 b1
= gen_llc_linktype(proto
);
3191 return gen_linux_sll_linktype(proto
);
3196 case DLT_SLIP_BSDOS
:
3199 * These types don't provide any type field; packets
3200 * are always IPv4 or IPv6.
3202 * XXX - for IPv4, check for a version number of 4, and,
3203 * for IPv6, check for a version number of 6?
3208 /* Check for a version number of 4. */
3209 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3211 case ETHERTYPE_IPV6
:
3212 /* Check for a version number of 6. */
3213 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3217 return gen_false(); /* always false */
3224 * Raw IPv4, so no type field.
3226 if (proto
== ETHERTYPE_IP
)
3227 return gen_true(); /* always true */
3229 /* Checking for something other than IPv4; always false */
3236 * Raw IPv6, so no type field.
3239 if (proto
== ETHERTYPE_IPV6
)
3240 return gen_true(); /* always true */
3243 /* Checking for something other than IPv6; always false */
3250 case DLT_PPP_SERIAL
:
3253 * We use Ethernet protocol types inside libpcap;
3254 * map them to the corresponding PPP protocol types.
3256 proto
= ethertype_to_ppptype(proto
);
3257 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3263 * We use Ethernet protocol types inside libpcap;
3264 * map them to the corresponding PPP protocol types.
3270 * Also check for Van Jacobson-compressed IP.
3271 * XXX - do this for other forms of PPP?
3273 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3274 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3276 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3281 proto
= ethertype_to_ppptype(proto
);
3282 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3292 * For DLT_NULL, the link-layer header is a 32-bit
3293 * word containing an AF_ value in *host* byte order,
3294 * and for DLT_ENC, the link-layer header begins
3295 * with a 32-bit work containing an AF_ value in
3298 * In addition, if we're reading a saved capture file,
3299 * the host byte order in the capture may not be the
3300 * same as the host byte order on this machine.
3302 * For DLT_LOOP, the link-layer header is a 32-bit
3303 * word containing an AF_ value in *network* byte order.
3305 * XXX - AF_ values may, unfortunately, be platform-
3306 * dependent; for example, FreeBSD's AF_INET6 is 24
3307 * whilst NetBSD's and OpenBSD's is 26.
3309 * This means that, when reading a capture file, just
3310 * checking for our AF_INET6 value won't work if the
3311 * capture file came from another OS.
3320 case ETHERTYPE_IPV6
:
3327 * Not a type on which we support filtering.
3328 * XXX - support those that have AF_ values
3329 * #defined on this platform, at least?
3334 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3336 * The AF_ value is in host byte order, but
3337 * the BPF interpreter will convert it to
3338 * network byte order.
3340 * If this is a save file, and it's from a
3341 * machine with the opposite byte order to
3342 * ours, we byte-swap the AF_ value.
3344 * Then we run it through "htonl()", and
3345 * generate code to compare against the result.
3347 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3348 bpf_pcap
->sf
.swapped
)
3349 proto
= SWAPLONG(proto
);
3350 proto
= htonl(proto
);
3352 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3354 #ifdef HAVE_NET_PFVAR_H
3357 * af field is host byte order in contrast to the rest of
3360 if (proto
== ETHERTYPE_IP
)
3361 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3362 BPF_B
, (bpf_int32
)AF_INET
));
3364 else if (proto
== ETHERTYPE_IPV6
)
3365 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3366 BPF_B
, (bpf_int32
)AF_INET6
));
3372 #endif /* HAVE_NET_PFVAR_H */
3375 case DLT_ARCNET_LINUX
:
3377 * XXX should we check for first fragment if the protocol
3386 case ETHERTYPE_IPV6
:
3387 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3388 (bpf_int32
)ARCTYPE_INET6
));
3392 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3393 (bpf_int32
)ARCTYPE_IP
);
3394 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3395 (bpf_int32
)ARCTYPE_IP_OLD
);
3400 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3401 (bpf_int32
)ARCTYPE_ARP
);
3402 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3403 (bpf_int32
)ARCTYPE_ARP_OLD
);
3407 case ETHERTYPE_REVARP
:
3408 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3409 (bpf_int32
)ARCTYPE_REVARP
));
3411 case ETHERTYPE_ATALK
:
3412 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3413 (bpf_int32
)ARCTYPE_ATALK
));
3420 case ETHERTYPE_ATALK
:
3430 * XXX - assumes a 2-byte Frame Relay header with
3431 * DLCI and flags. What if the address is longer?
3437 * Check for the special NLPID for IP.
3439 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3442 case ETHERTYPE_IPV6
:
3444 * Check for the special NLPID for IPv6.
3446 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3451 * Check for several OSI protocols.
3453 * Frame Relay packets typically have an OSI
3454 * NLPID at the beginning; we check for each
3457 * What we check for is the NLPID and a frame
3458 * control field of UI, i.e. 0x03 followed
3461 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3462 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3463 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3475 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3477 case DLT_JUNIPER_MFR
:
3478 case DLT_JUNIPER_MLFR
:
3479 case DLT_JUNIPER_MLPPP
:
3480 case DLT_JUNIPER_ATM1
:
3481 case DLT_JUNIPER_ATM2
:
3482 case DLT_JUNIPER_PPPOE
:
3483 case DLT_JUNIPER_PPPOE_ATM
:
3484 case DLT_JUNIPER_GGSN
:
3485 case DLT_JUNIPER_ES
:
3486 case DLT_JUNIPER_MONITOR
:
3487 case DLT_JUNIPER_SERVICES
:
3488 case DLT_JUNIPER_ETHER
:
3489 case DLT_JUNIPER_PPP
:
3490 case DLT_JUNIPER_FRELAY
:
3491 case DLT_JUNIPER_CHDLC
:
3492 case DLT_JUNIPER_VP
:
3493 case DLT_JUNIPER_ST
:
3494 case DLT_JUNIPER_ISM
:
3495 case DLT_JUNIPER_VS
:
3496 case DLT_JUNIPER_SRX_E2E
:
3497 case DLT_JUNIPER_FIBRECHANNEL
:
3498 case DLT_JUNIPER_ATM_CEMIC
:
3500 /* just lets verify the magic number for now -
3501 * on ATM we may have up to 6 different encapsulations on the wire
3502 * and need a lot of heuristics to figure out that the payload
3505 * FIXME encapsulation specific BPF_ filters
3507 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3510 return gen_ipnet_linktype(proto
);
3512 case DLT_LINUX_IRDA
:
3513 bpf_error("IrDA link-layer type filtering not implemented");
3516 bpf_error("DOCSIS link-layer type filtering not implemented");
3519 case DLT_MTP2_WITH_PHDR
:
3520 bpf_error("MTP2 link-layer type filtering not implemented");
3523 bpf_error("ERF link-layer type filtering not implemented");
3527 bpf_error("PFSYNC link-layer type filtering not implemented");
3530 case DLT_LINUX_LAPD
:
3531 bpf_error("LAPD link-layer type filtering not implemented");
3535 case DLT_USB_LINUX_MMAPPED
:
3536 bpf_error("USB link-layer type filtering not implemented");
3538 case DLT_BLUETOOTH_HCI_H4
:
3539 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3540 bpf_error("Bluetooth link-layer type filtering not implemented");
3543 case DLT_CAN_SOCKETCAN
:
3544 bpf_error("CAN link-layer type filtering not implemented");
3546 case DLT_IEEE802_15_4
:
3547 case DLT_IEEE802_15_4_LINUX
:
3548 case DLT_IEEE802_15_4_NONASK_PHY
:
3549 case DLT_IEEE802_15_4_NOFCS
:
3550 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3552 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3553 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3556 bpf_error("SITA link-layer type filtering not implemented");
3559 bpf_error("RAIF1 link-layer type filtering not implemented");
3562 bpf_error("IPMB link-layer type filtering not implemented");
3565 bpf_error("AX.25 link-layer type filtering not implemented");
3569 * All the types that have no encapsulation should either be
3570 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3571 * all packets are IP packets, or should be handled in some
3572 * special case, if none of them are (if some are and some
3573 * aren't, the lack of encapsulation is a problem, as we'd
3574 * have to find some other way of determining the packet type).
3576 * Therefore, if "off_linktype" is -1, there's an error.
3578 if (off_linktype
== (u_int
)-1)
3582 * Any type not handled above should always have an Ethernet
3583 * type at an offset of "off_linktype".
3585 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3589 * Check for an LLC SNAP packet with a given organization code and
3590 * protocol type; we check the entire contents of the 802.2 LLC and
3591 * snap headers, checking for DSAP and SSAP of SNAP and a control
3592 * field of 0x03 in the LLC header, and for the specified organization
3593 * code and protocol type in the SNAP header.
3595 static struct block
*
3596 gen_snap(orgcode
, ptype
)
3597 bpf_u_int32 orgcode
;
3600 u_char snapblock
[8];
3602 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3603 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3604 snapblock
[2] = 0x03; /* control = UI */
3605 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3606 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3607 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3608 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3609 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3610 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3614 * Generate code to match a particular packet type, for link-layer types
3615 * using 802.2 LLC headers.
3617 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3618 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3620 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3621 * value, if <= ETHERMTU. We use that to determine whether to
3622 * match the DSAP or both DSAP and LSAP or to check the OUI and
3623 * protocol ID in a SNAP header.
3625 static struct block
*
3626 gen_llc_linktype(proto
)
3630 * XXX - handle token-ring variable-length header.
3636 case LLCSAP_NETBEUI
:
3638 * XXX - should we check both the DSAP and the
3639 * SSAP, like this, or should we check just the
3640 * DSAP, as we do for other types <= ETHERMTU
3641 * (i.e., other SAP values)?
3643 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3644 ((proto
<< 8) | proto
));
3648 * XXX - are there ever SNAP frames for IPX on
3649 * non-Ethernet 802.x networks?
3651 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3652 (bpf_int32
)LLCSAP_IPX
);
3654 case ETHERTYPE_ATALK
:
3656 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3657 * SNAP packets with an organization code of
3658 * 0x080007 (Apple, for Appletalk) and a protocol
3659 * type of ETHERTYPE_ATALK (Appletalk).
3661 * XXX - check for an organization code of
3662 * encapsulated Ethernet as well?
3664 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3668 * XXX - we don't have to check for IPX 802.3
3669 * here, but should we check for the IPX Ethertype?
3671 if (proto
<= ETHERMTU
) {
3673 * This is an LLC SAP value, so check
3676 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3679 * This is an Ethernet type; we assume that it's
3680 * unlikely that it'll appear in the right place
3681 * at random, and therefore check only the
3682 * location that would hold the Ethernet type
3683 * in a SNAP frame with an organization code of
3684 * 0x000000 (encapsulated Ethernet).
3686 * XXX - if we were to check for the SNAP DSAP and
3687 * LSAP, as per XXX, and were also to check for an
3688 * organization code of 0x000000 (encapsulated
3689 * Ethernet), we'd do
3691 * return gen_snap(0x000000, proto);
3693 * here; for now, we don't, as per the above.
3694 * I don't know whether it's worth the extra CPU
3695 * time to do the right check or not.
3697 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3702 static struct block
*
3703 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3707 u_int src_off
, dst_off
;
3709 struct block
*b0
, *b1
;
3723 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3724 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3730 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3731 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3738 b0
= gen_linktype(proto
);
3739 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3745 static struct block
*
3746 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3747 struct in6_addr
*addr
;
3748 struct in6_addr
*mask
;
3750 u_int src_off
, dst_off
;
3752 struct block
*b0
, *b1
;
3767 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3768 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3774 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3775 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3782 /* this order is important */
3783 a
= (u_int32_t
*)addr
;
3784 m
= (u_int32_t
*)mask
;
3785 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3786 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3788 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3790 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3792 b0
= gen_linktype(proto
);
3798 static struct block
*
3799 gen_ehostop(eaddr
, dir
)
3800 register const u_char
*eaddr
;
3803 register struct block
*b0
, *b1
;
3807 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3810 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3813 b0
= gen_ehostop(eaddr
, Q_SRC
);
3814 b1
= gen_ehostop(eaddr
, Q_DST
);
3820 b0
= gen_ehostop(eaddr
, Q_SRC
);
3821 b1
= gen_ehostop(eaddr
, Q_DST
);
3826 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3830 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3834 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3838 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3842 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3846 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3854 * Like gen_ehostop, but for DLT_FDDI
3856 static struct block
*
3857 gen_fhostop(eaddr
, dir
)
3858 register const u_char
*eaddr
;
3861 struct block
*b0
, *b1
;
3866 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3868 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3873 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3875 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3879 b0
= gen_fhostop(eaddr
, Q_SRC
);
3880 b1
= gen_fhostop(eaddr
, Q_DST
);
3886 b0
= gen_fhostop(eaddr
, Q_SRC
);
3887 b1
= gen_fhostop(eaddr
, Q_DST
);
3892 bpf_error("'addr1' is only supported on 802.11");
3896 bpf_error("'addr2' is only supported on 802.11");
3900 bpf_error("'addr3' is only supported on 802.11");
3904 bpf_error("'addr4' is only supported on 802.11");
3908 bpf_error("'ra' is only supported on 802.11");
3912 bpf_error("'ta' is only supported on 802.11");
3920 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3922 static struct block
*
3923 gen_thostop(eaddr
, dir
)
3924 register const u_char
*eaddr
;
3927 register struct block
*b0
, *b1
;
3931 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3934 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3937 b0
= gen_thostop(eaddr
, Q_SRC
);
3938 b1
= gen_thostop(eaddr
, Q_DST
);
3944 b0
= gen_thostop(eaddr
, Q_SRC
);
3945 b1
= gen_thostop(eaddr
, Q_DST
);
3950 bpf_error("'addr1' is only supported on 802.11");
3954 bpf_error("'addr2' is only supported on 802.11");
3958 bpf_error("'addr3' is only supported on 802.11");
3962 bpf_error("'addr4' is only supported on 802.11");
3966 bpf_error("'ra' is only supported on 802.11");
3970 bpf_error("'ta' is only supported on 802.11");
3978 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3979 * various 802.11 + radio headers.
3981 static struct block
*
3982 gen_wlanhostop(eaddr
, dir
)
3983 register const u_char
*eaddr
;
3986 register struct block
*b0
, *b1
, *b2
;
3987 register struct slist
*s
;
3989 #ifdef ENABLE_WLAN_FILTERING_PATCH
3992 * We need to disable the optimizer because the optimizer is buggy
3993 * and wipes out some LD instructions generated by the below
3994 * code to validate the Frame Control bits
3997 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4004 * For control frames, there is no SA.
4006 * For management frames, SA is at an
4007 * offset of 10 from the beginning of
4010 * For data frames, SA is at an offset
4011 * of 10 from the beginning of the packet
4012 * if From DS is clear, at an offset of
4013 * 16 from the beginning of the packet
4014 * if From DS is set and To DS is clear,
4015 * and an offset of 24 from the beginning
4016 * of the packet if From DS is set and To DS
4021 * Generate the tests to be done for data frames
4024 * First, check for To DS set, i.e. check "link[1] & 0x01".
4026 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4027 b1
= new_block(JMP(BPF_JSET
));
4028 b1
->s
.k
= 0x01; /* To DS */
4032 * If To DS is set, the SA is at 24.
4034 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4038 * Now, check for To DS not set, i.e. check
4039 * "!(link[1] & 0x01)".
4041 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4042 b2
= new_block(JMP(BPF_JSET
));
4043 b2
->s
.k
= 0x01; /* To DS */
4048 * If To DS is not set, the SA is at 16.
4050 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4054 * Now OR together the last two checks. That gives
4055 * the complete set of checks for data frames with
4061 * Now check for From DS being set, and AND that with
4062 * the ORed-together checks.
4064 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4065 b1
= new_block(JMP(BPF_JSET
));
4066 b1
->s
.k
= 0x02; /* From DS */
4071 * Now check for data frames with From DS not set.
4073 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4074 b2
= new_block(JMP(BPF_JSET
));
4075 b2
->s
.k
= 0x02; /* From DS */
4080 * If From DS isn't set, the SA is at 10.
4082 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4086 * Now OR together the checks for data frames with
4087 * From DS not set and for data frames with From DS
4088 * set; that gives the checks done for data frames.
4093 * Now check for a data frame.
4094 * I.e, check "link[0] & 0x08".
4096 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4097 b1
= new_block(JMP(BPF_JSET
));
4102 * AND that with the checks done for data frames.
4107 * If the high-order bit of the type value is 0, this
4108 * is a management frame.
4109 * I.e, check "!(link[0] & 0x08)".
4111 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4112 b2
= new_block(JMP(BPF_JSET
));
4118 * For management frames, the SA is at 10.
4120 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4124 * OR that with the checks done for data frames.
4125 * That gives the checks done for management and
4131 * If the low-order bit of the type value is 1,
4132 * this is either a control frame or a frame
4133 * with a reserved type, and thus not a
4136 * I.e., check "!(link[0] & 0x04)".
4138 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4139 b1
= new_block(JMP(BPF_JSET
));
4145 * AND that with the checks for data and management
4155 * For control frames, there is no DA.
4157 * For management frames, DA is at an
4158 * offset of 4 from the beginning of
4161 * For data frames, DA is at an offset
4162 * of 4 from the beginning of the packet
4163 * if To DS is clear and at an offset of
4164 * 16 from the beginning of the packet
4169 * Generate the tests to be done for data frames.
4171 * First, check for To DS set, i.e. "link[1] & 0x01".
4173 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4174 b1
= new_block(JMP(BPF_JSET
));
4175 b1
->s
.k
= 0x01; /* To DS */
4179 * If To DS is set, the DA is at 16.
4181 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4185 * Now, check for To DS not set, i.e. check
4186 * "!(link[1] & 0x01)".
4188 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4189 b2
= new_block(JMP(BPF_JSET
));
4190 b2
->s
.k
= 0x01; /* To DS */
4195 * If To DS is not set, the DA is at 4.
4197 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4201 * Now OR together the last two checks. That gives
4202 * the complete set of checks for data frames.
4207 * Now check for a data frame.
4208 * I.e, check "link[0] & 0x08".
4210 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4211 b1
= new_block(JMP(BPF_JSET
));
4216 * AND that with the checks done for data frames.
4221 * If the high-order bit of the type value is 0, this
4222 * is a management frame.
4223 * I.e, check "!(link[0] & 0x08)".
4225 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4226 b2
= new_block(JMP(BPF_JSET
));
4232 * For management frames, the DA is at 4.
4234 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4238 * OR that with the checks done for data frames.
4239 * That gives the checks done for management and
4245 * If the low-order bit of the type value is 1,
4246 * this is either a control frame or a frame
4247 * with a reserved type, and thus not a
4250 * I.e., check "!(link[0] & 0x04)".
4252 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4253 b1
= new_block(JMP(BPF_JSET
));
4259 * AND that with the checks for data and management
4267 * Not present in management frames; addr1 in other
4272 * If the high-order bit of the type value is 0, this
4273 * is a management frame.
4274 * I.e, check "(link[0] & 0x08)".
4276 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4277 b1
= new_block(JMP(BPF_JSET
));
4284 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4287 * AND that with the check of addr1.
4294 * Not present in management frames; addr2, if present,
4299 * Not present in CTS or ACK control frames.
4301 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4302 IEEE80211_FC0_TYPE_MASK
);
4304 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4305 IEEE80211_FC0_SUBTYPE_MASK
);
4307 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4308 IEEE80211_FC0_SUBTYPE_MASK
);
4314 * If the high-order bit of the type value is 0, this
4315 * is a management frame.
4316 * I.e, check "(link[0] & 0x08)".
4318 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4319 b1
= new_block(JMP(BPF_JSET
));
4324 * AND that with the check for frames other than
4325 * CTS and ACK frames.
4332 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4337 * XXX - add BSSID keyword?
4340 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4344 * Not present in CTS or ACK control frames.
4346 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4347 IEEE80211_FC0_TYPE_MASK
);
4349 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4350 IEEE80211_FC0_SUBTYPE_MASK
);
4352 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4353 IEEE80211_FC0_SUBTYPE_MASK
);
4357 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4363 * Not present in control frames.
4365 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4366 IEEE80211_FC0_TYPE_MASK
);
4368 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4374 * Present only if the direction mask has both "From DS"
4375 * and "To DS" set. Neither control frames nor management
4376 * frames should have both of those set, so we don't
4377 * check the frame type.
4379 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4380 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4381 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4386 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4387 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4393 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4394 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4403 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4404 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4405 * as the RFC states.)
4407 static struct block
*
4408 gen_ipfchostop(eaddr
, dir
)
4409 register const u_char
*eaddr
;
4412 register struct block
*b0
, *b1
;
4416 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4419 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4422 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4423 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4429 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4430 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4435 bpf_error("'addr1' is only supported on 802.11");
4439 bpf_error("'addr2' is only supported on 802.11");
4443 bpf_error("'addr3' is only supported on 802.11");
4447 bpf_error("'addr4' is only supported on 802.11");
4451 bpf_error("'ra' is only supported on 802.11");
4455 bpf_error("'ta' is only supported on 802.11");
4463 * This is quite tricky because there may be pad bytes in front of the
4464 * DECNET header, and then there are two possible data packet formats that
4465 * carry both src and dst addresses, plus 5 packet types in a format that
4466 * carries only the src node, plus 2 types that use a different format and
4467 * also carry just the src node.
4471 * Instead of doing those all right, we just look for data packets with
4472 * 0 or 1 bytes of padding. If you want to look at other packets, that
4473 * will require a lot more hacking.
4475 * To add support for filtering on DECNET "areas" (network numbers)
4476 * one would want to add a "mask" argument to this routine. That would
4477 * make the filter even more inefficient, although one could be clever
4478 * and not generate masking instructions if the mask is 0xFFFF.
4480 static struct block
*
4481 gen_dnhostop(addr
, dir
)
4485 struct block
*b0
, *b1
, *b2
, *tmp
;
4486 u_int offset_lh
; /* offset if long header is received */
4487 u_int offset_sh
; /* offset if short header is received */
4492 offset_sh
= 1; /* follows flags */
4493 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4497 offset_sh
= 3; /* follows flags, dstnode */
4498 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4502 /* Inefficient because we do our Calvinball dance twice */
4503 b0
= gen_dnhostop(addr
, Q_SRC
);
4504 b1
= gen_dnhostop(addr
, Q_DST
);
4510 /* Inefficient because we do our Calvinball dance twice */
4511 b0
= gen_dnhostop(addr
, Q_SRC
);
4512 b1
= gen_dnhostop(addr
, Q_DST
);
4517 bpf_error("ISO host filtering not implemented");
4522 b0
= gen_linktype(ETHERTYPE_DN
);
4523 /* Check for pad = 1, long header case */
4524 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4525 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4526 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4527 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4529 /* Check for pad = 0, long header case */
4530 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4531 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4534 /* Check for pad = 1, short header case */
4535 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4536 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4537 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4540 /* Check for pad = 0, short header case */
4541 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4542 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4546 /* Combine with test for linktype */
4552 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4553 * test the bottom-of-stack bit, and then check the version number
4554 * field in the IP header.
4556 static struct block
*
4557 gen_mpls_linktype(proto
)
4560 struct block
*b0
, *b1
;
4565 /* match the bottom-of-stack bit */
4566 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4567 /* match the IPv4 version number */
4568 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4573 /* match the bottom-of-stack bit */
4574 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4575 /* match the IPv4 version number */
4576 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4585 static struct block
*
4586 gen_host(addr
, mask
, proto
, dir
, type
)
4593 struct block
*b0
, *b1
;
4594 const char *typestr
;
4604 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4606 * Only check for non-IPv4 addresses if we're not
4607 * checking MPLS-encapsulated packets.
4609 if (label_stack_depth
== 0) {
4610 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4612 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4618 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4621 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4624 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4627 bpf_error("'tcp' modifier applied to %s", typestr
);
4630 bpf_error("'sctp' modifier applied to %s", typestr
);
4633 bpf_error("'udp' modifier applied to %s", typestr
);
4636 bpf_error("'icmp' modifier applied to %s", typestr
);
4639 bpf_error("'igmp' modifier applied to %s", typestr
);
4642 bpf_error("'igrp' modifier applied to %s", typestr
);
4645 bpf_error("'pim' modifier applied to %s", typestr
);
4648 bpf_error("'vrrp' modifier applied to %s", typestr
);
4651 bpf_error("ATALK host filtering not implemented");
4654 bpf_error("AARP host filtering not implemented");
4657 return gen_dnhostop(addr
, dir
);
4660 bpf_error("SCA host filtering not implemented");
4663 bpf_error("LAT host filtering not implemented");
4666 bpf_error("MOPDL host filtering not implemented");
4669 bpf_error("MOPRC host filtering not implemented");
4673 bpf_error("'ip6' modifier applied to ip host");
4676 bpf_error("'icmp6' modifier applied to %s", typestr
);
4680 bpf_error("'ah' modifier applied to %s", typestr
);
4683 bpf_error("'esp' modifier applied to %s", typestr
);
4686 bpf_error("ISO host filtering not implemented");
4689 bpf_error("'esis' modifier applied to %s", typestr
);
4692 bpf_error("'isis' modifier applied to %s", typestr
);
4695 bpf_error("'clnp' modifier applied to %s", typestr
);
4698 bpf_error("'stp' modifier applied to %s", typestr
);
4701 bpf_error("IPX host filtering not implemented");
4704 bpf_error("'netbeui' modifier applied to %s", typestr
);
4707 bpf_error("'radio' modifier applied to %s", typestr
);
4716 static struct block
*
4717 gen_host6(addr
, mask
, proto
, dir
, type
)
4718 struct in6_addr
*addr
;
4719 struct in6_addr
*mask
;
4724 const char *typestr
;
4734 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4737 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4740 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4743 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4746 bpf_error("'sctp' modifier applied to %s", typestr
);
4749 bpf_error("'tcp' modifier applied to %s", typestr
);
4752 bpf_error("'udp' modifier applied to %s", typestr
);
4755 bpf_error("'icmp' modifier applied to %s", typestr
);
4758 bpf_error("'igmp' modifier applied to %s", typestr
);
4761 bpf_error("'igrp' modifier applied to %s", typestr
);
4764 bpf_error("'pim' modifier applied to %s", typestr
);
4767 bpf_error("'vrrp' modifier applied to %s", typestr
);
4770 bpf_error("ATALK host filtering not implemented");
4773 bpf_error("AARP host filtering not implemented");
4776 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4779 bpf_error("SCA host filtering not implemented");
4782 bpf_error("LAT host filtering not implemented");
4785 bpf_error("MOPDL host filtering not implemented");
4788 bpf_error("MOPRC host filtering not implemented");
4791 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4794 bpf_error("'icmp6' modifier applied to %s", typestr
);
4797 bpf_error("'ah' modifier applied to %s", typestr
);
4800 bpf_error("'esp' modifier applied to %s", typestr
);
4803 bpf_error("ISO host filtering not implemented");
4806 bpf_error("'esis' modifier applied to %s", typestr
);
4809 bpf_error("'isis' modifier applied to %s", typestr
);
4812 bpf_error("'clnp' modifier applied to %s", typestr
);
4815 bpf_error("'stp' modifier applied to %s", typestr
);
4818 bpf_error("IPX host filtering not implemented");
4821 bpf_error("'netbeui' modifier applied to %s", typestr
);
4824 bpf_error("'radio' modifier applied to %s", typestr
);
4834 static struct block
*
4835 gen_gateway(eaddr
, alist
, proto
, dir
)
4836 const u_char
*eaddr
;
4837 bpf_u_int32
**alist
;
4841 struct block
*b0
, *b1
, *tmp
;
4844 bpf_error("direction applied to 'gateway'");
4853 b0
= gen_ehostop(eaddr
, Q_OR
);
4856 b0
= gen_fhostop(eaddr
, Q_OR
);
4859 b0
= gen_thostop(eaddr
, Q_OR
);
4861 case DLT_IEEE802_11
:
4862 case DLT_PRISM_HEADER
:
4863 case DLT_IEEE802_11_RADIO_AVS
:
4864 case DLT_IEEE802_11_RADIO
:
4866 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4871 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4873 * Check that the packet doesn't begin with an
4874 * LE Control marker. (We've already generated
4877 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4882 * Now check the MAC address.
4884 b0
= gen_ehostop(eaddr
, Q_OR
);
4887 case DLT_IP_OVER_FC
:
4888 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4892 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4894 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4896 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4905 bpf_error("illegal modifier of 'gateway'");
4911 gen_proto_abbrev(proto
)
4920 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4922 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4928 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4930 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4936 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4938 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4944 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4947 #ifndef IPPROTO_IGMP
4948 #define IPPROTO_IGMP 2
4952 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4955 #ifndef IPPROTO_IGRP
4956 #define IPPROTO_IGRP 9
4959 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4963 #define IPPROTO_PIM 103
4967 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4969 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4974 #ifndef IPPROTO_VRRP
4975 #define IPPROTO_VRRP 112
4979 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4983 b1
= gen_linktype(ETHERTYPE_IP
);
4987 b1
= gen_linktype(ETHERTYPE_ARP
);
4991 b1
= gen_linktype(ETHERTYPE_REVARP
);
4995 bpf_error("link layer applied in wrong context");
4998 b1
= gen_linktype(ETHERTYPE_ATALK
);
5002 b1
= gen_linktype(ETHERTYPE_AARP
);
5006 b1
= gen_linktype(ETHERTYPE_DN
);
5010 b1
= gen_linktype(ETHERTYPE_SCA
);
5014 b1
= gen_linktype(ETHERTYPE_LAT
);
5018 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5022 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5027 b1
= gen_linktype(ETHERTYPE_IPV6
);
5030 #ifndef IPPROTO_ICMPV6
5031 #define IPPROTO_ICMPV6 58
5034 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5039 #define IPPROTO_AH 51
5042 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5044 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5050 #define IPPROTO_ESP 50
5053 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5055 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5061 b1
= gen_linktype(LLCSAP_ISONS
);
5065 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5069 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5072 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5073 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5074 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5076 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5078 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5080 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5084 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5085 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5086 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5088 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5090 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5092 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5096 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5097 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5098 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5100 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5105 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5106 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5111 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5112 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5114 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5116 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5121 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5122 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5127 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5128 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5133 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5137 b1
= gen_linktype(LLCSAP_8021D
);
5141 b1
= gen_linktype(LLCSAP_IPX
);
5145 b1
= gen_linktype(LLCSAP_NETBEUI
);
5149 bpf_error("'radio' is not a valid protocol type");
5157 static struct block
*
5164 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5165 b
= new_block(JMP(BPF_JSET
));
5174 * Generate a comparison to a port value in the transport-layer header
5175 * at the specified offset from the beginning of that header.
5177 * XXX - this handles a variable-length prefix preceding the link-layer
5178 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5179 * variable-length link-layer headers (such as Token Ring or 802.11
5182 static struct block
*
5183 gen_portatom(off
, v
)
5187 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5191 static struct block
*
5192 gen_portatom6(off
, v
)
5196 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5201 gen_portop(port
, proto
, dir
)
5202 int port
, proto
, dir
;
5204 struct block
*b0
, *b1
, *tmp
;
5206 /* ip proto 'proto' */
5207 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5213 b1
= gen_portatom(0, (bpf_int32
)port
);
5217 b1
= gen_portatom(2, (bpf_int32
)port
);
5222 tmp
= gen_portatom(0, (bpf_int32
)port
);
5223 b1
= gen_portatom(2, (bpf_int32
)port
);
5228 tmp
= gen_portatom(0, (bpf_int32
)port
);
5229 b1
= gen_portatom(2, (bpf_int32
)port
);
5241 static struct block
*
5242 gen_port(port
, ip_proto
, dir
)
5247 struct block
*b0
, *b1
, *tmp
;
5252 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5253 * not LLC encapsulation with LLCSAP_IP.
5255 * For IEEE 802 networks - which includes 802.5 token ring
5256 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5257 * says that SNAP encapsulation is used, not LLC encapsulation
5260 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5261 * RFC 2225 say that SNAP encapsulation is used, not LLC
5262 * encapsulation with LLCSAP_IP.
5264 * So we always check for ETHERTYPE_IP.
5266 b0
= gen_linktype(ETHERTYPE_IP
);
5272 b1
= gen_portop(port
, ip_proto
, dir
);
5276 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5277 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5279 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5292 gen_portop6(port
, proto
, dir
)
5293 int port
, proto
, dir
;
5295 struct block
*b0
, *b1
, *tmp
;
5297 /* ip6 proto 'proto' */
5298 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5302 b1
= gen_portatom6(0, (bpf_int32
)port
);
5306 b1
= gen_portatom6(2, (bpf_int32
)port
);
5311 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5312 b1
= gen_portatom6(2, (bpf_int32
)port
);
5317 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5318 b1
= gen_portatom6(2, (bpf_int32
)port
);
5330 static struct block
*
5331 gen_port6(port
, ip_proto
, dir
)
5336 struct block
*b0
, *b1
, *tmp
;
5338 /* link proto ip6 */
5339 b0
= gen_linktype(ETHERTYPE_IPV6
);
5345 b1
= gen_portop6(port
, ip_proto
, dir
);
5349 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5350 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5352 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5364 /* gen_portrange code */
5365 static struct block
*
5366 gen_portrangeatom(off
, v1
, v2
)
5370 struct block
*b1
, *b2
;
5374 * Reverse the order of the ports, so v1 is the lower one.
5383 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5384 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5392 gen_portrangeop(port1
, port2
, proto
, dir
)
5397 struct block
*b0
, *b1
, *tmp
;
5399 /* ip proto 'proto' */
5400 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5406 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5410 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5415 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5416 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5421 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5422 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5434 static struct block
*
5435 gen_portrange(port1
, port2
, ip_proto
, dir
)
5440 struct block
*b0
, *b1
, *tmp
;
5443 b0
= gen_linktype(ETHERTYPE_IP
);
5449 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5453 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5454 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5456 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5468 static struct block
*
5469 gen_portrangeatom6(off
, v1
, v2
)
5473 struct block
*b1
, *b2
;
5477 * Reverse the order of the ports, so v1 is the lower one.
5486 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5487 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5495 gen_portrangeop6(port1
, port2
, proto
, dir
)
5500 struct block
*b0
, *b1
, *tmp
;
5502 /* ip6 proto 'proto' */
5503 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5507 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5511 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5516 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5517 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5522 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5523 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5535 static struct block
*
5536 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5541 struct block
*b0
, *b1
, *tmp
;
5543 /* link proto ip6 */
5544 b0
= gen_linktype(ETHERTYPE_IPV6
);
5550 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5554 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5555 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5557 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5570 lookup_proto(name
, proto
)
5571 register const char *name
;
5581 v
= pcap_nametoproto(name
);
5582 if (v
== PROTO_UNDEF
)
5583 bpf_error("unknown ip proto '%s'", name
);
5587 /* XXX should look up h/w protocol type based on linktype */
5588 v
= pcap_nametoeproto(name
);
5589 if (v
== PROTO_UNDEF
) {
5590 v
= pcap_nametollc(name
);
5591 if (v
== PROTO_UNDEF
)
5592 bpf_error("unknown ether proto '%s'", name
);
5597 if (strcmp(name
, "esis") == 0)
5599 else if (strcmp(name
, "isis") == 0)
5601 else if (strcmp(name
, "clnp") == 0)
5604 bpf_error("unknown osi proto '%s'", name
);
5624 static struct block
*
5625 gen_protochain(v
, proto
, dir
)
5630 #ifdef NO_PROTOCHAIN
5631 return gen_proto(v
, proto
, dir
);
5633 struct block
*b0
, *b
;
5634 struct slist
*s
[100];
5635 int fix2
, fix3
, fix4
, fix5
;
5636 int ahcheck
, again
, end
;
5638 int reg2
= alloc_reg();
5640 memset(s
, 0, sizeof(s
));
5641 fix2
= fix3
= fix4
= fix5
= 0;
5648 b0
= gen_protochain(v
, Q_IP
, dir
);
5649 b
= gen_protochain(v
, Q_IPV6
, dir
);
5653 bpf_error("bad protocol applied for 'protochain'");
5658 * We don't handle variable-length prefixes before the link-layer
5659 * header, or variable-length link-layer headers, here yet.
5660 * We might want to add BPF instructions to do the protochain
5661 * work, to simplify that and, on platforms that have a BPF
5662 * interpreter with the new instructions, let the filtering
5663 * be done in the kernel. (We already require a modified BPF
5664 * engine to do the protochain stuff, to support backward
5665 * branches, and backward branch support is unlikely to appear
5666 * in kernel BPF engines.)
5670 case DLT_IEEE802_11
:
5671 case DLT_PRISM_HEADER
:
5672 case DLT_IEEE802_11_RADIO_AVS
:
5673 case DLT_IEEE802_11_RADIO
:
5675 bpf_error("'protochain' not supported with 802.11");
5678 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5681 * s[0] is a dummy entry to protect other BPF insn from damage
5682 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5683 * hard to find interdependency made by jump table fixup.
5686 s
[i
] = new_stmt(0); /*dummy*/
5691 b0
= gen_linktype(ETHERTYPE_IP
);
5694 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5695 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5697 /* X = ip->ip_hl << 2 */
5698 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5699 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5704 b0
= gen_linktype(ETHERTYPE_IPV6
);
5706 /* A = ip6->ip_nxt */
5707 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5708 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5710 /* X = sizeof(struct ip6_hdr) */
5711 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5717 bpf_error("unsupported proto to gen_protochain");
5721 /* again: if (A == v) goto end; else fall through; */
5723 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5725 s
[i
]->s
.jt
= NULL
; /*later*/
5726 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5730 #ifndef IPPROTO_NONE
5731 #define IPPROTO_NONE 59
5733 /* if (A == IPPROTO_NONE) goto end */
5734 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5735 s
[i
]->s
.jt
= NULL
; /*later*/
5736 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5737 s
[i
]->s
.k
= IPPROTO_NONE
;
5738 s
[fix5
]->s
.jf
= s
[i
];
5743 if (proto
== Q_IPV6
) {
5744 int v6start
, v6end
, v6advance
, j
;
5747 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5748 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5749 s
[i
]->s
.jt
= NULL
; /*later*/
5750 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5751 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5752 s
[fix2
]->s
.jf
= s
[i
];
5754 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5755 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5756 s
[i
]->s
.jt
= NULL
; /*later*/
5757 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5758 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5760 /* if (A == IPPROTO_ROUTING) goto v6advance */
5761 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5762 s
[i
]->s
.jt
= NULL
; /*later*/
5763 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5764 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5766 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5767 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5768 s
[i
]->s
.jt
= NULL
; /*later*/
5769 s
[i
]->s
.jf
= NULL
; /*later*/
5770 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5781 * X = X + (P[X + 1] + 1) * 8;
5784 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5786 /* A = P[X + packet head] */
5787 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5788 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5791 s
[i
] = new_stmt(BPF_ST
);
5795 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5798 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5802 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5804 /* A = P[X + packet head]; */
5805 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5806 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5809 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5813 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5817 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5820 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5824 /* goto again; (must use BPF_JA for backward jump) */
5825 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5826 s
[i
]->s
.k
= again
- i
- 1;
5827 s
[i
- 1]->s
.jf
= s
[i
];
5831 for (j
= v6start
; j
<= v6end
; j
++)
5832 s
[j
]->s
.jt
= s
[v6advance
];
5837 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5839 s
[fix2
]->s
.jf
= s
[i
];
5845 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5846 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5847 s
[i
]->s
.jt
= NULL
; /*later*/
5848 s
[i
]->s
.jf
= NULL
; /*later*/
5849 s
[i
]->s
.k
= IPPROTO_AH
;
5851 s
[fix3
]->s
.jf
= s
[ahcheck
];
5858 * X = X + (P[X + 1] + 2) * 4;
5861 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5863 /* A = P[X + packet head]; */
5864 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5865 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5868 s
[i
] = new_stmt(BPF_ST
);
5872 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5875 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5879 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5881 /* A = P[X + packet head] */
5882 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5883 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5886 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5890 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5894 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5897 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5901 /* goto again; (must use BPF_JA for backward jump) */
5902 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5903 s
[i
]->s
.k
= again
- i
- 1;
5908 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5910 s
[fix2
]->s
.jt
= s
[end
];
5911 s
[fix4
]->s
.jf
= s
[end
];
5912 s
[fix5
]->s
.jt
= s
[end
];
5919 for (i
= 0; i
< max
- 1; i
++)
5920 s
[i
]->next
= s
[i
+ 1];
5921 s
[max
- 1]->next
= NULL
;
5926 b
= new_block(JMP(BPF_JEQ
));
5927 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5937 static struct block
*
5938 gen_check_802_11_data_frame()
5941 struct block
*b0
, *b1
;
5944 * A data frame has the 0x08 bit (b3) in the frame control field set
5945 * and the 0x04 bit (b2) clear.
5947 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5948 b0
= new_block(JMP(BPF_JSET
));
5952 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5953 b1
= new_block(JMP(BPF_JSET
));
5964 * Generate code that checks whether the packet is a packet for protocol
5965 * <proto> and whether the type field in that protocol's header has
5966 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5967 * IP packet and checks the protocol number in the IP header against <v>.
5969 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5970 * against Q_IP and Q_IPV6.
5972 static struct block
*
5973 gen_proto(v
, proto
, dir
)
5978 struct block
*b0
, *b1
;
5980 if (dir
!= Q_DEFAULT
)
5981 bpf_error("direction applied to 'proto'");
5986 b0
= gen_proto(v
, Q_IP
, dir
);
5987 b1
= gen_proto(v
, Q_IPV6
, dir
);
5995 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5996 * not LLC encapsulation with LLCSAP_IP.
5998 * For IEEE 802 networks - which includes 802.5 token ring
5999 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6000 * says that SNAP encapsulation is used, not LLC encapsulation
6003 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6004 * RFC 2225 say that SNAP encapsulation is used, not LLC
6005 * encapsulation with LLCSAP_IP.
6007 * So we always check for ETHERTYPE_IP.
6009 b0
= gen_linktype(ETHERTYPE_IP
);
6011 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6013 b1
= gen_protochain(v
, Q_IP
);
6023 * Frame Relay packets typically have an OSI
6024 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6025 * generates code to check for all the OSI
6026 * NLPIDs, so calling it and then adding a check
6027 * for the particular NLPID for which we're
6028 * looking is bogus, as we can just check for
6031 * What we check for is the NLPID and a frame
6032 * control field value of UI, i.e. 0x03 followed
6035 * XXX - assumes a 2-byte Frame Relay header with
6036 * DLCI and flags. What if the address is longer?
6038 * XXX - what about SNAP-encapsulated frames?
6040 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6046 * Cisco uses an Ethertype lookalike - for OSI,
6049 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6050 /* OSI in C-HDLC is stuffed with a fudge byte */
6051 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6056 b0
= gen_linktype(LLCSAP_ISONS
);
6057 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6063 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6065 * 4 is the offset of the PDU type relative to the IS-IS
6068 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6073 bpf_error("arp does not encapsulate another protocol");
6077 bpf_error("rarp does not encapsulate another protocol");
6081 bpf_error("atalk encapsulation is not specifiable");
6085 bpf_error("decnet encapsulation is not specifiable");
6089 bpf_error("sca does not encapsulate another protocol");
6093 bpf_error("lat does not encapsulate another protocol");
6097 bpf_error("moprc does not encapsulate another protocol");
6101 bpf_error("mopdl does not encapsulate another protocol");
6105 return gen_linktype(v
);
6108 bpf_error("'udp proto' is bogus");
6112 bpf_error("'tcp proto' is bogus");
6116 bpf_error("'sctp proto' is bogus");
6120 bpf_error("'icmp proto' is bogus");
6124 bpf_error("'igmp proto' is bogus");
6128 bpf_error("'igrp proto' is bogus");
6132 bpf_error("'pim proto' is bogus");
6136 bpf_error("'vrrp proto' is bogus");
6141 b0
= gen_linktype(ETHERTYPE_IPV6
);
6143 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6145 b1
= gen_protochain(v
, Q_IPV6
);
6151 bpf_error("'icmp6 proto' is bogus");
6155 bpf_error("'ah proto' is bogus");
6158 bpf_error("'ah proto' is bogus");
6161 bpf_error("'stp proto' is bogus");
6164 bpf_error("'ipx proto' is bogus");
6167 bpf_error("'netbeui proto' is bogus");
6170 bpf_error("'radio proto' is bogus");
6181 register const char *name
;
6184 int proto
= q
.proto
;
6188 bpf_u_int32 mask
, addr
;
6190 bpf_u_int32
**alist
;
6193 struct sockaddr_in
*sin4
;
6194 struct sockaddr_in6
*sin6
;
6195 struct addrinfo
*res
, *res0
;
6196 struct in6_addr mask128
;
6198 struct block
*b
, *tmp
;
6199 int port
, real_proto
;
6205 addr
= pcap_nametonetaddr(name
);
6207 bpf_error("unknown network '%s'", name
);
6208 /* Left justify network addr and calculate its network mask */
6210 while (addr
&& (addr
& 0xff000000) == 0) {
6214 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6218 if (proto
== Q_LINK
) {
6222 eaddr
= pcap_ether_hostton(name
);
6225 "unknown ether host '%s'", name
);
6226 b
= gen_ehostop(eaddr
, dir
);
6231 eaddr
= pcap_ether_hostton(name
);
6234 "unknown FDDI host '%s'", name
);
6235 b
= gen_fhostop(eaddr
, dir
);
6240 eaddr
= pcap_ether_hostton(name
);
6243 "unknown token ring host '%s'", name
);
6244 b
= gen_thostop(eaddr
, dir
);
6248 case DLT_IEEE802_11
:
6249 case DLT_PRISM_HEADER
:
6250 case DLT_IEEE802_11_RADIO_AVS
:
6251 case DLT_IEEE802_11_RADIO
:
6253 eaddr
= pcap_ether_hostton(name
);
6256 "unknown 802.11 host '%s'", name
);
6257 b
= gen_wlanhostop(eaddr
, dir
);
6261 case DLT_IP_OVER_FC
:
6262 eaddr
= pcap_ether_hostton(name
);
6265 "unknown Fibre Channel host '%s'", name
);
6266 b
= gen_ipfchostop(eaddr
, dir
);
6275 * Check that the packet doesn't begin
6276 * with an LE Control marker. (We've
6277 * already generated a test for LANE.)
6279 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6283 eaddr
= pcap_ether_hostton(name
);
6286 "unknown ether host '%s'", name
);
6287 b
= gen_ehostop(eaddr
, dir
);
6293 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6294 } else if (proto
== Q_DECNET
) {
6295 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6297 * I don't think DECNET hosts can be multihomed, so
6298 * there is no need to build up a list of addresses
6300 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6303 alist
= pcap_nametoaddr(name
);
6304 if (alist
== NULL
|| *alist
== NULL
)
6305 bpf_error("unknown host '%s'", name
);
6307 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6309 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6311 tmp
= gen_host(**alist
++, 0xffffffff,
6312 tproto
, dir
, q
.addr
);
6318 memset(&mask128
, 0xff, sizeof(mask128
));
6319 res0
= res
= pcap_nametoaddrinfo(name
);
6321 bpf_error("unknown host '%s'", name
);
6324 tproto
= tproto6
= proto
;
6325 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6329 for (res
= res0
; res
; res
= res
->ai_next
) {
6330 switch (res
->ai_family
) {
6332 if (tproto
== Q_IPV6
)
6335 sin4
= (struct sockaddr_in
*)
6337 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6338 0xffffffff, tproto
, dir
, q
.addr
);
6341 if (tproto6
== Q_IP
)
6344 sin6
= (struct sockaddr_in6
*)
6346 tmp
= gen_host6(&sin6
->sin6_addr
,
6347 &mask128
, tproto6
, dir
, q
.addr
);
6359 bpf_error("unknown host '%s'%s", name
,
6360 (proto
== Q_DEFAULT
)
6362 : " for specified address family");
6369 if (proto
!= Q_DEFAULT
&&
6370 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6371 bpf_error("illegal qualifier of 'port'");
6372 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6373 bpf_error("unknown port '%s'", name
);
6374 if (proto
== Q_UDP
) {
6375 if (real_proto
== IPPROTO_TCP
)
6376 bpf_error("port '%s' is tcp", name
);
6377 else if (real_proto
== IPPROTO_SCTP
)
6378 bpf_error("port '%s' is sctp", name
);
6380 /* override PROTO_UNDEF */
6381 real_proto
= IPPROTO_UDP
;
6383 if (proto
== Q_TCP
) {
6384 if (real_proto
== IPPROTO_UDP
)
6385 bpf_error("port '%s' is udp", name
);
6387 else if (real_proto
== IPPROTO_SCTP
)
6388 bpf_error("port '%s' is sctp", name
);
6390 /* override PROTO_UNDEF */
6391 real_proto
= IPPROTO_TCP
;
6393 if (proto
== Q_SCTP
) {
6394 if (real_proto
== IPPROTO_UDP
)
6395 bpf_error("port '%s' is udp", name
);
6397 else if (real_proto
== IPPROTO_TCP
)
6398 bpf_error("port '%s' is tcp", name
);
6400 /* override PROTO_UNDEF */
6401 real_proto
= IPPROTO_SCTP
;
6404 bpf_error("illegal port number %d < 0", port
);
6406 bpf_error("illegal port number %d > 65535", port
);
6408 return gen_port(port
, real_proto
, dir
);
6410 b
= gen_port(port
, real_proto
, dir
);
6411 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6416 if (proto
!= Q_DEFAULT
&&
6417 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6418 bpf_error("illegal qualifier of 'portrange'");
6419 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6420 bpf_error("unknown port in range '%s'", name
);
6421 if (proto
== Q_UDP
) {
6422 if (real_proto
== IPPROTO_TCP
)
6423 bpf_error("port in range '%s' is tcp", name
);
6424 else if (real_proto
== IPPROTO_SCTP
)
6425 bpf_error("port in range '%s' is sctp", name
);
6427 /* override PROTO_UNDEF */
6428 real_proto
= IPPROTO_UDP
;
6430 if (proto
== Q_TCP
) {
6431 if (real_proto
== IPPROTO_UDP
)
6432 bpf_error("port in range '%s' is udp", name
);
6433 else if (real_proto
== IPPROTO_SCTP
)
6434 bpf_error("port in range '%s' is sctp", name
);
6436 /* override PROTO_UNDEF */
6437 real_proto
= IPPROTO_TCP
;
6439 if (proto
== Q_SCTP
) {
6440 if (real_proto
== IPPROTO_UDP
)
6441 bpf_error("port in range '%s' is udp", name
);
6442 else if (real_proto
== IPPROTO_TCP
)
6443 bpf_error("port in range '%s' is tcp", name
);
6445 /* override PROTO_UNDEF */
6446 real_proto
= IPPROTO_SCTP
;
6449 bpf_error("illegal port number %d < 0", port1
);
6451 bpf_error("illegal port number %d > 65535", port1
);
6453 bpf_error("illegal port number %d < 0", port2
);
6455 bpf_error("illegal port number %d > 65535", port2
);
6458 return gen_portrange(port1
, port2
, real_proto
, dir
);
6460 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6461 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6467 eaddr
= pcap_ether_hostton(name
);
6469 bpf_error("unknown ether host: %s", name
);
6471 alist
= pcap_nametoaddr(name
);
6472 if (alist
== NULL
|| *alist
== NULL
)
6473 bpf_error("unknown host '%s'", name
);
6474 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6478 bpf_error("'gateway' not supported in this configuration");
6482 real_proto
= lookup_proto(name
, proto
);
6483 if (real_proto
>= 0)
6484 return gen_proto(real_proto
, proto
, dir
);
6486 bpf_error("unknown protocol: %s", name
);
6489 real_proto
= lookup_proto(name
, proto
);
6490 if (real_proto
>= 0)
6491 return gen_protochain(real_proto
, proto
, dir
);
6493 bpf_error("unknown protocol: %s", name
);
6504 gen_mcode(s1
, s2
, masklen
, q
)
6505 register const char *s1
, *s2
;
6506 register int masklen
;
6509 register int nlen
, mlen
;
6512 nlen
= __pcap_atoin(s1
, &n
);
6513 /* Promote short ipaddr */
6517 mlen
= __pcap_atoin(s2
, &m
);
6518 /* Promote short ipaddr */
6521 bpf_error("non-network bits set in \"%s mask %s\"",
6524 /* Convert mask len to mask */
6526 bpf_error("mask length must be <= 32");
6529 * X << 32 is not guaranteed by C to be 0; it's
6534 m
= 0xffffffff << (32 - masklen
);
6536 bpf_error("non-network bits set in \"%s/%d\"",
6543 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6546 bpf_error("Mask syntax for networks only");
6555 register const char *s
;
6560 int proto
= q
.proto
;
6566 else if (q
.proto
== Q_DECNET
)
6567 vlen
= __pcap_atodn(s
, &v
);
6569 vlen
= __pcap_atoin(s
, &v
);
6576 if (proto
== Q_DECNET
)
6577 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6578 else if (proto
== Q_LINK
) {
6579 bpf_error("illegal link layer address");
6582 if (s
== NULL
&& q
.addr
== Q_NET
) {
6583 /* Promote short net number */
6584 while (v
&& (v
& 0xff000000) == 0) {
6589 /* Promote short ipaddr */
6593 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6598 proto
= IPPROTO_UDP
;
6599 else if (proto
== Q_TCP
)
6600 proto
= IPPROTO_TCP
;
6601 else if (proto
== Q_SCTP
)
6602 proto
= IPPROTO_SCTP
;
6603 else if (proto
== Q_DEFAULT
)
6604 proto
= PROTO_UNDEF
;
6606 bpf_error("illegal qualifier of 'port'");
6609 bpf_error("illegal port number %u > 65535", v
);
6612 return gen_port((int)v
, proto
, dir
);
6616 b
= gen_port((int)v
, proto
, dir
);
6617 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6624 proto
= IPPROTO_UDP
;
6625 else if (proto
== Q_TCP
)
6626 proto
= IPPROTO_TCP
;
6627 else if (proto
== Q_SCTP
)
6628 proto
= IPPROTO_SCTP
;
6629 else if (proto
== Q_DEFAULT
)
6630 proto
= PROTO_UNDEF
;
6632 bpf_error("illegal qualifier of 'portrange'");
6635 bpf_error("illegal port number %u > 65535", v
);
6638 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6642 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6643 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6649 bpf_error("'gateway' requires a name");
6653 return gen_proto((int)v
, proto
, dir
);
6656 return gen_protochain((int)v
, proto
, dir
);
6671 gen_mcode6(s1
, s2
, masklen
, q
)
6672 register const char *s1
, *s2
;
6673 register int masklen
;
6676 struct addrinfo
*res
;
6677 struct in6_addr
*addr
;
6678 struct in6_addr mask
;
6683 bpf_error("no mask %s supported", s2
);
6685 res
= pcap_nametoaddrinfo(s1
);
6687 bpf_error("invalid ip6 address %s", s1
);
6690 bpf_error("%s resolved to multiple address", s1
);
6691 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6693 if (sizeof(mask
) * 8 < masklen
)
6694 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6695 memset(&mask
, 0, sizeof(mask
));
6696 memset(&mask
, 0xff, masklen
/ 8);
6698 mask
.s6_addr
[masklen
/ 8] =
6699 (0xff << (8 - masklen
% 8)) & 0xff;
6702 a
= (u_int32_t
*)addr
;
6703 m
= (u_int32_t
*)&mask
;
6704 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6705 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6706 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6714 bpf_error("Mask syntax for networks only");
6718 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6724 bpf_error("invalid qualifier against IPv6 address");
6733 register const u_char
*eaddr
;
6736 struct block
*b
, *tmp
;
6738 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6741 return gen_ehostop(eaddr
, (int)q
.dir
);
6743 return gen_fhostop(eaddr
, (int)q
.dir
);
6745 return gen_thostop(eaddr
, (int)q
.dir
);
6746 case DLT_IEEE802_11
:
6747 case DLT_PRISM_HEADER
:
6748 case DLT_IEEE802_11_RADIO_AVS
:
6749 case DLT_IEEE802_11_RADIO
:
6751 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6755 * Check that the packet doesn't begin with an
6756 * LE Control marker. (We've already generated
6759 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6764 * Now check the MAC address.
6766 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6771 case DLT_IP_OVER_FC
:
6772 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6774 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6778 bpf_error("ethernet address used in non-ether expression");
6785 struct slist
*s0
, *s1
;
6788 * This is definitely not the best way to do this, but the
6789 * lists will rarely get long.
6796 static struct slist
*
6802 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6807 static struct slist
*
6813 s
= new_stmt(BPF_LD
|BPF_MEM
);
6819 * Modify "index" to use the value stored into its register as an
6820 * offset relative to the beginning of the header for the protocol
6821 * "proto", and allocate a register and put an item "size" bytes long
6822 * (1, 2, or 4) at that offset into that register, making it the register
6826 gen_load(proto
, inst
, size
)
6831 struct slist
*s
, *tmp
;
6833 int regno
= alloc_reg();
6835 free_reg(inst
->regno
);
6839 bpf_error("data size must be 1, 2, or 4");
6855 bpf_error("unsupported index operation");
6859 * The offset is relative to the beginning of the packet
6860 * data, if we have a radio header. (If we don't, this
6863 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6864 linktype
!= DLT_IEEE802_11_RADIO
&&
6865 linktype
!= DLT_PRISM_HEADER
)
6866 bpf_error("radio information not present in capture");
6869 * Load into the X register the offset computed into the
6870 * register specified by "index".
6872 s
= xfer_to_x(inst
);
6875 * Load the item at that offset.
6877 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6879 sappend(inst
->s
, s
);
6884 * The offset is relative to the beginning of
6885 * the link-layer header.
6887 * XXX - what about ATM LANE? Should the index be
6888 * relative to the beginning of the AAL5 frame, so
6889 * that 0 refers to the beginning of the LE Control
6890 * field, or relative to the beginning of the LAN
6891 * frame, so that 0 refers, for Ethernet LANE, to
6892 * the beginning of the destination address?
6894 s
= gen_llprefixlen();
6897 * If "s" is non-null, it has code to arrange that the
6898 * X register contains the length of the prefix preceding
6899 * the link-layer header. Add to it the offset computed
6900 * into the register specified by "index", and move that
6901 * into the X register. Otherwise, just load into the X
6902 * register the offset computed into the register specified
6906 sappend(s
, xfer_to_a(inst
));
6907 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6908 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6910 s
= xfer_to_x(inst
);
6913 * Load the item at the sum of the offset we've put in the
6914 * X register and the offset of the start of the link
6915 * layer header (which is 0 if the radio header is
6916 * variable-length; that header length is what we put
6917 * into the X register and then added to the index).
6919 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6922 sappend(inst
->s
, s
);
6938 * The offset is relative to the beginning of
6939 * the network-layer header.
6940 * XXX - are there any cases where we want
6943 s
= gen_off_macpl();
6946 * If "s" is non-null, it has code to arrange that the
6947 * X register contains the offset of the MAC-layer
6948 * payload. Add to it the offset computed into the
6949 * register specified by "index", and move that into
6950 * the X register. Otherwise, just load into the X
6951 * register the offset computed into the register specified
6955 sappend(s
, xfer_to_a(inst
));
6956 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6957 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6959 s
= xfer_to_x(inst
);
6962 * Load the item at the sum of the offset we've put in the
6963 * X register, the offset of the start of the network
6964 * layer header from the beginning of the MAC-layer
6965 * payload, and the purported offset of the start of the
6966 * MAC-layer payload (which might be 0 if there's a
6967 * variable-length prefix before the link-layer header
6968 * or the link-layer header itself is variable-length;
6969 * the variable-length offset of the start of the
6970 * MAC-layer payload is what we put into the X register
6971 * and then added to the index).
6973 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6974 tmp
->s
.k
= off_macpl
+ off_nl
;
6976 sappend(inst
->s
, s
);
6979 * Do the computation only if the packet contains
6980 * the protocol in question.
6982 b
= gen_proto_abbrev(proto
);
6984 gen_and(inst
->b
, b
);
6997 * The offset is relative to the beginning of
6998 * the transport-layer header.
7000 * Load the X register with the length of the IPv4 header
7001 * (plus the offset of the link-layer header, if it's
7002 * a variable-length header), in bytes.
7004 * XXX - are there any cases where we want
7006 * XXX - we should, if we're built with
7007 * IPv6 support, generate code to load either
7008 * IPv4, IPv6, or both, as appropriate.
7010 s
= gen_loadx_iphdrlen();
7013 * The X register now contains the sum of the length
7014 * of any variable-length header preceding the link-layer
7015 * header, any variable-length link-layer header, and the
7016 * length of the network-layer header.
7018 * Load into the A register the offset relative to
7019 * the beginning of the transport layer header,
7020 * add the X register to that, move that to the
7021 * X register, and load with an offset from the
7022 * X register equal to the offset of the network
7023 * layer header relative to the beginning of
7024 * the MAC-layer payload plus the fixed-length
7025 * portion of the offset of the MAC-layer payload
7026 * from the beginning of the raw packet data.
7028 sappend(s
, xfer_to_a(inst
));
7029 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7030 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7031 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7032 tmp
->s
.k
= off_macpl
+ off_nl
;
7033 sappend(inst
->s
, s
);
7036 * Do the computation only if the packet contains
7037 * the protocol in question - which is true only
7038 * if this is an IP datagram and is the first or
7039 * only fragment of that datagram.
7041 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7043 gen_and(inst
->b
, b
);
7045 gen_and(gen_proto_abbrev(Q_IP
), b
);
7051 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7055 inst
->regno
= regno
;
7056 s
= new_stmt(BPF_ST
);
7058 sappend(inst
->s
, s
);
7064 gen_relation(code
, a0
, a1
, reversed
)
7066 struct arth
*a0
, *a1
;
7069 struct slist
*s0
, *s1
, *s2
;
7070 struct block
*b
, *tmp
;
7074 if (code
== BPF_JEQ
) {
7075 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7076 b
= new_block(JMP(code
));
7080 b
= new_block(BPF_JMP
|code
|BPF_X
);
7086 sappend(a0
->s
, a1
->s
);
7090 free_reg(a0
->regno
);
7091 free_reg(a1
->regno
);
7093 /* 'and' together protocol checks */
7096 gen_and(a0
->b
, tmp
= a1
->b
);
7112 int regno
= alloc_reg();
7113 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7116 s
= new_stmt(BPF_LD
|BPF_LEN
);
7117 s
->next
= new_stmt(BPF_ST
);
7118 s
->next
->s
.k
= regno
;
7133 a
= (struct arth
*)newchunk(sizeof(*a
));
7137 s
= new_stmt(BPF_LD
|BPF_IMM
);
7139 s
->next
= new_stmt(BPF_ST
);
7155 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7158 s
= new_stmt(BPF_ST
);
7166 gen_arth(code
, a0
, a1
)
7168 struct arth
*a0
, *a1
;
7170 struct slist
*s0
, *s1
, *s2
;
7174 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7179 sappend(a0
->s
, a1
->s
);
7181 free_reg(a0
->regno
);
7182 free_reg(a1
->regno
);
7184 s0
= new_stmt(BPF_ST
);
7185 a0
->regno
= s0
->s
.k
= alloc_reg();
7192 * Here we handle simple allocation of the scratch registers.
7193 * If too many registers are alloc'd, the allocator punts.
7195 static int regused
[BPF_MEMWORDS
];
7199 * Initialize the table of used registers and the current register.
7205 memset(regused
, 0, sizeof regused
);
7209 * Return the next free register.
7214 int n
= BPF_MEMWORDS
;
7217 if (regused
[curreg
])
7218 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7220 regused
[curreg
] = 1;
7224 bpf_error("too many registers needed to evaluate expression");
7230 * Return a register to the table so it can
7240 static struct block
*
7247 s
= new_stmt(BPF_LD
|BPF_LEN
);
7248 b
= new_block(JMP(jmp
));
7259 return gen_len(BPF_JGE
, n
);
7263 * Actually, this is less than or equal.
7271 b
= gen_len(BPF_JGT
, n
);
7278 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7279 * the beginning of the link-layer header.
7280 * XXX - that means you can't test values in the radiotap header, but
7281 * as that header is difficult if not impossible to parse generally
7282 * without a loop, that might not be a severe problem. A new keyword
7283 * "radio" could be added for that, although what you'd really want
7284 * would be a way of testing particular radio header values, which
7285 * would generate code appropriate to the radio header in question.
7288 gen_byteop(op
, idx
, val
)
7299 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7302 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7306 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7310 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7314 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7318 b
= new_block(JMP(BPF_JEQ
));
7325 static u_char abroadcast
[] = { 0x0 };
7328 gen_broadcast(proto
)
7331 bpf_u_int32 hostmask
;
7332 struct block
*b0
, *b1
, *b2
;
7333 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7341 case DLT_ARCNET_LINUX
:
7342 return gen_ahostop(abroadcast
, Q_DST
);
7344 return gen_ehostop(ebroadcast
, Q_DST
);
7346 return gen_fhostop(ebroadcast
, Q_DST
);
7348 return gen_thostop(ebroadcast
, Q_DST
);
7349 case DLT_IEEE802_11
:
7350 case DLT_PRISM_HEADER
:
7351 case DLT_IEEE802_11_RADIO_AVS
:
7352 case DLT_IEEE802_11_RADIO
:
7354 return gen_wlanhostop(ebroadcast
, Q_DST
);
7355 case DLT_IP_OVER_FC
:
7356 return gen_ipfchostop(ebroadcast
, Q_DST
);
7360 * Check that the packet doesn't begin with an
7361 * LE Control marker. (We've already generated
7364 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7369 * Now check the MAC address.
7371 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7377 bpf_error("not a broadcast link");
7383 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7384 * as an indication that we don't know the netmask, and fail
7387 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7388 bpf_error("netmask not known, so 'ip broadcast' not supported");
7389 b0
= gen_linktype(ETHERTYPE_IP
);
7390 hostmask
= ~netmask
;
7391 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7392 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7393 (bpf_int32
)(~0 & hostmask
), hostmask
);
7398 bpf_error("only link-layer/IP broadcast filters supported");
7404 * Generate code to test the low-order bit of a MAC address (that's
7405 * the bottom bit of the *first* byte).
7407 static struct block
*
7408 gen_mac_multicast(offset
)
7411 register struct block
*b0
;
7412 register struct slist
*s
;
7414 /* link[offset] & 1 != 0 */
7415 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7416 b0
= new_block(JMP(BPF_JSET
));
7423 gen_multicast(proto
)
7426 register struct block
*b0
, *b1
, *b2
;
7427 register struct slist
*s
;
7435 case DLT_ARCNET_LINUX
:
7436 /* all ARCnet multicasts use the same address */
7437 return gen_ahostop(abroadcast
, Q_DST
);
7439 /* ether[0] & 1 != 0 */
7440 return gen_mac_multicast(0);
7443 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7445 * XXX - was that referring to bit-order issues?
7447 /* fddi[1] & 1 != 0 */
7448 return gen_mac_multicast(1);
7450 /* tr[2] & 1 != 0 */
7451 return gen_mac_multicast(2);
7452 case DLT_IEEE802_11
:
7453 case DLT_PRISM_HEADER
:
7454 case DLT_IEEE802_11_RADIO_AVS
:
7455 case DLT_IEEE802_11_RADIO
:
7460 * For control frames, there is no DA.
7462 * For management frames, DA is at an
7463 * offset of 4 from the beginning of
7466 * For data frames, DA is at an offset
7467 * of 4 from the beginning of the packet
7468 * if To DS is clear and at an offset of
7469 * 16 from the beginning of the packet
7474 * Generate the tests to be done for data frames.
7476 * First, check for To DS set, i.e. "link[1] & 0x01".
7478 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7479 b1
= new_block(JMP(BPF_JSET
));
7480 b1
->s
.k
= 0x01; /* To DS */
7484 * If To DS is set, the DA is at 16.
7486 b0
= gen_mac_multicast(16);
7490 * Now, check for To DS not set, i.e. check
7491 * "!(link[1] & 0x01)".
7493 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7494 b2
= new_block(JMP(BPF_JSET
));
7495 b2
->s
.k
= 0x01; /* To DS */
7500 * If To DS is not set, the DA is at 4.
7502 b1
= gen_mac_multicast(4);
7506 * Now OR together the last two checks. That gives
7507 * the complete set of checks for data frames.
7512 * Now check for a data frame.
7513 * I.e, check "link[0] & 0x08".
7515 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7516 b1
= new_block(JMP(BPF_JSET
));
7521 * AND that with the checks done for data frames.
7526 * If the high-order bit of the type value is 0, this
7527 * is a management frame.
7528 * I.e, check "!(link[0] & 0x08)".
7530 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7531 b2
= new_block(JMP(BPF_JSET
));
7537 * For management frames, the DA is at 4.
7539 b1
= gen_mac_multicast(4);
7543 * OR that with the checks done for data frames.
7544 * That gives the checks done for management and
7550 * If the low-order bit of the type value is 1,
7551 * this is either a control frame or a frame
7552 * with a reserved type, and thus not a
7555 * I.e., check "!(link[0] & 0x04)".
7557 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7558 b1
= new_block(JMP(BPF_JSET
));
7564 * AND that with the checks for data and management
7569 case DLT_IP_OVER_FC
:
7570 b0
= gen_mac_multicast(2);
7575 * Check that the packet doesn't begin with an
7576 * LE Control marker. (We've already generated
7579 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7583 /* ether[off_mac] & 1 != 0 */
7584 b0
= gen_mac_multicast(off_mac
);
7592 /* Link not known to support multicasts */
7596 b0
= gen_linktype(ETHERTYPE_IP
);
7597 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7603 b0
= gen_linktype(ETHERTYPE_IPV6
);
7604 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7609 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7615 * generate command for inbound/outbound. It's here so we can
7616 * make it link-type specific. 'dir' = 0 implies "inbound",
7617 * = 1 implies "outbound".
7623 register struct block
*b0
;
7626 * Only some data link types support inbound/outbound qualifiers.
7630 b0
= gen_relation(BPF_JEQ
,
7631 gen_load(Q_LINK
, gen_loadi(0), 1),
7638 /* match outgoing packets */
7639 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7641 /* match incoming packets */
7642 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7649 * Match packets sent by this machine.
7651 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7654 * Match packets sent to this machine.
7655 * (No broadcast or multicast packets, or
7656 * packets sent to some other machine and
7657 * received promiscuously.)
7659 * XXX - packets sent to other machines probably
7660 * shouldn't be matched, but what about broadcast
7661 * or multicast packets we received?
7663 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7667 #ifdef HAVE_NET_PFVAR_H
7669 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7670 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7676 /* match outgoing packets */
7677 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7679 /* match incoming packets */
7680 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7684 case DLT_JUNIPER_MFR
:
7685 case DLT_JUNIPER_MLFR
:
7686 case DLT_JUNIPER_MLPPP
:
7687 case DLT_JUNIPER_ATM1
:
7688 case DLT_JUNIPER_ATM2
:
7689 case DLT_JUNIPER_PPPOE
:
7690 case DLT_JUNIPER_PPPOE_ATM
:
7691 case DLT_JUNIPER_GGSN
:
7692 case DLT_JUNIPER_ES
:
7693 case DLT_JUNIPER_MONITOR
:
7694 case DLT_JUNIPER_SERVICES
:
7695 case DLT_JUNIPER_ETHER
:
7696 case DLT_JUNIPER_PPP
:
7697 case DLT_JUNIPER_FRELAY
:
7698 case DLT_JUNIPER_CHDLC
:
7699 case DLT_JUNIPER_VP
:
7700 case DLT_JUNIPER_ST
:
7701 case DLT_JUNIPER_ISM
:
7702 case DLT_JUNIPER_VS
:
7703 case DLT_JUNIPER_SRX_E2E
:
7704 case DLT_JUNIPER_FIBRECHANNEL
:
7705 case DLT_JUNIPER_ATM_CEMIC
:
7707 /* juniper flags (including direction) are stored
7708 * the byte after the 3-byte magic number */
7710 /* match outgoing packets */
7711 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7713 /* match incoming packets */
7714 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7719 bpf_error("inbound/outbound not supported on linktype %d",
7727 #ifdef HAVE_NET_PFVAR_H
7728 /* PF firewall log matched interface */
7730 gen_pf_ifname(const char *ifname
)
7735 if (linktype
!= DLT_PFLOG
) {
7736 bpf_error("ifname supported only on PF linktype");
7739 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7740 off
= offsetof(struct pfloghdr
, ifname
);
7741 if (strlen(ifname
) >= len
) {
7742 bpf_error("ifname interface names can only be %d characters",
7746 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7750 /* PF firewall log ruleset name */
7752 gen_pf_ruleset(char *ruleset
)
7756 if (linktype
!= DLT_PFLOG
) {
7757 bpf_error("ruleset supported only on PF linktype");
7761 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7762 bpf_error("ruleset names can only be %ld characters",
7763 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7767 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7768 strlen(ruleset
), (const u_char
*)ruleset
);
7772 /* PF firewall log rule number */
7778 if (linktype
!= DLT_PFLOG
) {
7779 bpf_error("rnr supported only on PF linktype");
7783 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7788 /* PF firewall log sub-rule number */
7790 gen_pf_srnr(int srnr
)
7794 if (linktype
!= DLT_PFLOG
) {
7795 bpf_error("srnr supported only on PF linktype");
7799 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7804 /* PF firewall log reason code */
7806 gen_pf_reason(int reason
)
7810 if (linktype
!= DLT_PFLOG
) {
7811 bpf_error("reason supported only on PF linktype");
7815 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7820 /* PF firewall log action */
7822 gen_pf_action(int action
)
7826 if (linktype
!= DLT_PFLOG
) {
7827 bpf_error("action supported only on PF linktype");
7831 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7835 #else /* !HAVE_NET_PFVAR_H */
7837 gen_pf_ifname(const char *ifname
)
7839 bpf_error("libpcap was compiled without pf support");
7845 gen_pf_ruleset(char *ruleset
)
7847 bpf_error("libpcap was compiled on a machine without pf support");
7855 bpf_error("libpcap was compiled on a machine without pf support");
7861 gen_pf_srnr(int srnr
)
7863 bpf_error("libpcap was compiled on a machine without pf support");
7869 gen_pf_reason(int reason
)
7871 bpf_error("libpcap was compiled on a machine without pf support");
7877 gen_pf_action(int action
)
7879 bpf_error("libpcap was compiled on a machine without pf support");
7883 #endif /* HAVE_NET_PFVAR_H */
7885 /* IEEE 802.11 wireless header */
7887 gen_p80211_type(int type
, int mask
)
7893 case DLT_IEEE802_11
:
7894 case DLT_PRISM_HEADER
:
7895 case DLT_IEEE802_11_RADIO_AVS
:
7896 case DLT_IEEE802_11_RADIO
:
7897 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7902 bpf_error("802.11 link-layer types supported only on 802.11");
7910 gen_p80211_fcdir(int fcdir
)
7916 case DLT_IEEE802_11
:
7917 case DLT_PRISM_HEADER
:
7918 case DLT_IEEE802_11_RADIO_AVS
:
7919 case DLT_IEEE802_11_RADIO
:
7923 bpf_error("frame direction supported only with 802.11 headers");
7927 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7928 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7935 register const u_char
*eaddr
;
7941 case DLT_ARCNET_LINUX
:
7942 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7944 return (gen_ahostop(eaddr
, (int)q
.dir
));
7946 bpf_error("ARCnet address used in non-arc expression");
7952 bpf_error("aid supported only on ARCnet");
7955 bpf_error("ARCnet address used in non-arc expression");
7960 static struct block
*
7961 gen_ahostop(eaddr
, dir
)
7962 register const u_char
*eaddr
;
7965 register struct block
*b0
, *b1
;
7968 /* src comes first, different from Ethernet */
7970 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7973 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7976 b0
= gen_ahostop(eaddr
, Q_SRC
);
7977 b1
= gen_ahostop(eaddr
, Q_DST
);
7983 b0
= gen_ahostop(eaddr
, Q_SRC
);
7984 b1
= gen_ahostop(eaddr
, Q_DST
);
7989 bpf_error("'addr1' is only supported on 802.11");
7993 bpf_error("'addr2' is only supported on 802.11");
7997 bpf_error("'addr3' is only supported on 802.11");
8001 bpf_error("'addr4' is only supported on 802.11");
8005 bpf_error("'ra' is only supported on 802.11");
8009 bpf_error("'ta' is only supported on 802.11");
8017 * support IEEE 802.1Q VLAN trunk over ethernet
8023 struct block
*b0
, *b1
;
8025 /* can't check for VLAN-encapsulated packets inside MPLS */
8026 if (label_stack_depth
> 0)
8027 bpf_error("no VLAN match after MPLS");
8030 * Check for a VLAN packet, and then change the offsets to point
8031 * to the type and data fields within the VLAN packet. Just
8032 * increment the offsets, so that we can support a hierarchy, e.g.
8033 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8036 * XXX - this is a bit of a kludge. If we were to split the
8037 * compiler into a parser that parses an expression and
8038 * generates an expression tree, and a code generator that
8039 * takes an expression tree (which could come from our
8040 * parser or from some other parser) and generates BPF code,
8041 * we could perhaps make the offsets parameters of routines
8042 * and, in the handler for an "AND" node, pass to subnodes
8043 * other than the VLAN node the adjusted offsets.
8045 * This would mean that "vlan" would, instead of changing the
8046 * behavior of *all* tests after it, change only the behavior
8047 * of tests ANDed with it. That would change the documented
8048 * semantics of "vlan", which might break some expressions.
8049 * However, it would mean that "(vlan and ip) or ip" would check
8050 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8051 * checking only for VLAN-encapsulated IP, so that could still
8052 * be considered worth doing; it wouldn't break expressions
8053 * that are of the form "vlan and ..." or "vlan N and ...",
8054 * which I suspect are the most common expressions involving
8055 * "vlan". "vlan or ..." doesn't necessarily do what the user
8056 * would really want, now, as all the "or ..." tests would
8057 * be done assuming a VLAN, even though the "or" could be viewed
8058 * as meaning "or, if this isn't a VLAN packet...".
8065 /* check for VLAN */
8066 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8067 (bpf_int32
)ETHERTYPE_8021Q
);
8069 /* If a specific VLAN is requested, check VLAN id */
8070 if (vlan_num
>= 0) {
8071 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
8072 (bpf_int32
)vlan_num
, 0x0fff);
8086 bpf_error("no VLAN support for data link type %d",
8101 struct block
*b0
,*b1
;
8104 * Change the offsets to point to the type and data fields within
8105 * the MPLS packet. Just increment the offsets, so that we
8106 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8107 * capture packets with an outer label of 100000 and an inner
8110 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8114 if (label_stack_depth
> 0) {
8115 /* just match the bottom-of-stack bit clear */
8116 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
8119 * Indicate that we're checking MPLS-encapsulated headers,
8120 * to make sure higher level code generators don't try to
8121 * match against IP-related protocols such as Q_ARP, Q_RARP
8126 case DLT_C_HDLC
: /* fall through */
8128 b0
= gen_linktype(ETHERTYPE_MPLS
);
8132 b0
= gen_linktype(PPP_MPLS_UCAST
);
8135 /* FIXME add other DLT_s ...
8136 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8137 * leave it for now */
8140 bpf_error("no MPLS support for data link type %d",
8148 /* If a specific MPLS label is requested, check it */
8149 if (label_num
>= 0) {
8150 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8151 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8152 0xfffff000); /* only compare the first 20 bits */
8159 label_stack_depth
++;
8164 * Support PPPOE discovery and session.
8169 /* check for PPPoE discovery */
8170 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8179 * Test against the PPPoE session link-layer type.
8181 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8184 * Change the offsets to point to the type and data fields within
8185 * the PPP packet, and note that this is PPPoE rather than
8188 * XXX - this is a bit of a kludge. If we were to split the
8189 * compiler into a parser that parses an expression and
8190 * generates an expression tree, and a code generator that
8191 * takes an expression tree (which could come from our
8192 * parser or from some other parser) and generates BPF code,
8193 * we could perhaps make the offsets parameters of routines
8194 * and, in the handler for an "AND" node, pass to subnodes
8195 * other than the PPPoE node the adjusted offsets.
8197 * This would mean that "pppoes" would, instead of changing the
8198 * behavior of *all* tests after it, change only the behavior
8199 * of tests ANDed with it. That would change the documented
8200 * semantics of "pppoes", which might break some expressions.
8201 * However, it would mean that "(pppoes and ip) or ip" would check
8202 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8203 * checking only for VLAN-encapsulated IP, so that could still
8204 * be considered worth doing; it wouldn't break expressions
8205 * that are of the form "pppoes and ..." which I suspect are the
8206 * most common expressions involving "pppoes". "pppoes or ..."
8207 * doesn't necessarily do what the user would really want, now,
8208 * as all the "or ..." tests would be done assuming PPPoE, even
8209 * though the "or" could be viewed as meaning "or, if this isn't
8210 * a PPPoE packet...".
8212 orig_linktype
= off_linktype
; /* save original values */
8217 * The "network-layer" protocol is PPPoE, which has a 6-byte
8218 * PPPoE header, followed by a PPP packet.
8220 * There is no HDLC encapsulation for the PPP packet (it's
8221 * encapsulated in PPPoES instead), so the link-layer type
8222 * starts at the first byte of the PPP packet. For PPPoE,
8223 * that offset is relative to the beginning of the total
8224 * link-layer payload, including any 802.2 LLC header, so
8225 * it's 6 bytes past off_nl.
8227 off_linktype
= off_nl
+ 6;
8230 * The network-layer offsets are relative to the beginning
8231 * of the MAC-layer payload; that's past the 6-byte
8232 * PPPoE header and the 2-byte PPP header.
8235 off_nl_nosnap
= 6+2;
8241 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8253 bpf_error("'vpi' supported only on raw ATM");
8254 if (off_vpi
== (u_int
)-1)
8256 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8262 bpf_error("'vci' supported only on raw ATM");
8263 if (off_vci
== (u_int
)-1)
8265 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8270 if (off_proto
== (u_int
)-1)
8271 abort(); /* XXX - this isn't on FreeBSD */
8272 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8277 if (off_payload
== (u_int
)-1)
8279 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8280 0xffffffff, jtype
, reverse
, jvalue
);
8285 bpf_error("'callref' supported only on raw ATM");
8286 if (off_proto
== (u_int
)-1)
8288 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8289 jtype
, reverse
, jvalue
);
8299 gen_atmtype_abbrev(type
)
8302 struct block
*b0
, *b1
;
8307 /* Get all packets in Meta signalling Circuit */
8309 bpf_error("'metac' supported only on raw ATM");
8310 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8311 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8316 /* Get all packets in Broadcast Circuit*/
8318 bpf_error("'bcc' supported only on raw ATM");
8319 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8320 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8325 /* Get all cells in Segment OAM F4 circuit*/
8327 bpf_error("'oam4sc' supported only on raw ATM");
8328 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8329 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8334 /* Get all cells in End-to-End OAM F4 Circuit*/
8336 bpf_error("'oam4ec' supported only on raw ATM");
8337 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8338 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8343 /* Get all packets in connection Signalling Circuit */
8345 bpf_error("'sc' supported only on raw ATM");
8346 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8347 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8352 /* Get all packets in ILMI Circuit */
8354 bpf_error("'ilmic' supported only on raw ATM");
8355 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8356 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8361 /* Get all LANE packets */
8363 bpf_error("'lane' supported only on raw ATM");
8364 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8367 * Arrange that all subsequent tests assume LANE
8368 * rather than LLC-encapsulated packets, and set
8369 * the offsets appropriately for LANE-encapsulated
8372 * "off_mac" is the offset of the Ethernet header,
8373 * which is 2 bytes past the ATM pseudo-header
8374 * (skipping the pseudo-header and 2-byte LE Client
8375 * field). The other offsets are Ethernet offsets
8376 * relative to "off_mac".
8379 off_mac
= off_payload
+ 2; /* MAC header */
8380 off_linktype
= off_mac
+ 12;
8381 off_macpl
= off_mac
+ 14; /* Ethernet */
8382 off_nl
= 0; /* Ethernet II */
8383 off_nl_nosnap
= 3; /* 802.3+802.2 */
8387 /* Get all LLC-encapsulated packets */
8389 bpf_error("'llc' supported only on raw ATM");
8390 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8401 * Filtering for MTP2 messages based on li value
8402 * FISU, length is null
8403 * LSSU, length is 1 or 2
8404 * MSU, length is 3 or more
8407 gen_mtp2type_abbrev(type
)
8410 struct block
*b0
, *b1
;
8415 if ( (linktype
!= DLT_MTP2
) &&
8416 (linktype
!= DLT_ERF
) &&
8417 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8418 bpf_error("'fisu' supported only on MTP2");
8419 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8420 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8424 if ( (linktype
!= DLT_MTP2
) &&
8425 (linktype
!= DLT_ERF
) &&
8426 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8427 bpf_error("'lssu' supported only on MTP2");
8428 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8429 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8434 if ( (linktype
!= DLT_MTP2
) &&
8435 (linktype
!= DLT_ERF
) &&
8436 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8437 bpf_error("'msu' supported only on MTP2");
8438 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8448 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8455 bpf_u_int32 val1
, val2
, val3
;
8457 switch (mtp3field
) {
8460 if (off_sio
== (u_int
)-1)
8461 bpf_error("'sio' supported only on SS7");
8462 /* sio coded on 1 byte so max value 255 */
8464 bpf_error("sio value %u too big; max value = 255",
8466 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8467 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8471 if (off_opc
== (u_int
)-1)
8472 bpf_error("'opc' supported only on SS7");
8473 /* opc coded on 14 bits so max value 16383 */
8475 bpf_error("opc value %u too big; max value = 16383",
8477 /* the following instructions are made to convert jvalue
8478 * to the form used to write opc in an ss7 message*/
8479 val1
= jvalue
& 0x00003c00;
8481 val2
= jvalue
& 0x000003fc;
8483 val3
= jvalue
& 0x00000003;
8485 jvalue
= val1
+ val2
+ val3
;
8486 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8487 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8491 if (off_dpc
== (u_int
)-1)
8492 bpf_error("'dpc' supported only on SS7");
8493 /* dpc coded on 14 bits so max value 16383 */
8495 bpf_error("dpc value %u too big; max value = 16383",
8497 /* the following instructions are made to convert jvalue
8498 * to the forme used to write dpc in an ss7 message*/
8499 val1
= jvalue
& 0x000000ff;
8501 val2
= jvalue
& 0x00003f00;
8503 jvalue
= val1
+ val2
;
8504 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8505 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8509 if (off_sls
== (u_int
)-1)
8510 bpf_error("'sls' supported only on SS7");
8511 /* sls coded on 4 bits so max value 15 */
8513 bpf_error("sls value %u too big; max value = 15",
8515 /* the following instruction is made to convert jvalue
8516 * to the forme used to write sls in an ss7 message*/
8517 jvalue
= jvalue
<< 4;
8518 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8519 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8528 static struct block
*
8529 gen_msg_abbrev(type
)
8535 * Q.2931 signalling protocol messages for handling virtual circuits
8536 * establishment and teardown
8541 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8545 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8549 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8553 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8557 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8560 case A_RELEASE_DONE
:
8561 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8571 gen_atmmulti_abbrev(type
)
8574 struct block
*b0
, *b1
;
8580 bpf_error("'oam' supported only on raw ATM");
8581 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8586 bpf_error("'oamf4' supported only on raw ATM");
8588 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8589 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8591 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8597 * Get Q.2931 signalling messages for switched
8598 * virtual connection
8601 bpf_error("'connectmsg' supported only on raw ATM");
8602 b0
= gen_msg_abbrev(A_SETUP
);
8603 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8605 b0
= gen_msg_abbrev(A_CONNECT
);
8607 b0
= gen_msg_abbrev(A_CONNECTACK
);
8609 b0
= gen_msg_abbrev(A_RELEASE
);
8611 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8613 b0
= gen_atmtype_abbrev(A_SC
);
8619 bpf_error("'metaconnect' supported only on raw ATM");
8620 b0
= gen_msg_abbrev(A_SETUP
);
8621 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8623 b0
= gen_msg_abbrev(A_CONNECT
);
8625 b0
= gen_msg_abbrev(A_RELEASE
);
8627 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8629 b0
= gen_atmtype_abbrev(A_METAC
);