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
:
1381 case DLT_MTP2_WITH_PHDR
:
1414 case DLT_LINUX_LAPD
:
1416 * Currently, only raw "link[N:M]" filtering is supported.
1426 * Currently, only raw "link[N:M]" filtering is supported.
1434 case DLT_BLUETOOTH_HCI_H4
:
1436 * Currently, only raw "link[N:M]" filtering is supported.
1446 * Currently, only raw "link[N:M]" filtering is supported.
1456 * Currently, only raw "link[N:M]" filtering is supported.
1464 case DLT_IEEE802_15_4_LINUX
:
1466 * Currently, only raw "link[N:M]" filtering is supported.
1474 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1476 * Currently, only raw "link[N:M]" filtering is supported.
1484 case DLT_IEEE802_15_4
:
1486 * Currently, only raw "link[N:M]" filtering is supported.
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.
1524 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1526 * Currently, only raw "link[N:M]" filtering is supported.
1536 * Currently, only raw "link[N:M]" filtering is supported.
1538 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1540 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1541 off_nl_nosnap
= -1; /* no 802.2 LLC */
1542 off_mac
= 1; /* step over the kiss length byte */
1545 case DLT_IEEE802_15_4_NONASK_PHY
:
1547 * Currently, only raw "link[N:M]" filtering is supported.
1557 * Currently, only raw "link[N:M]" filtering is supported.
1565 case DLT_USB_LINUX_MMAPPED
:
1567 * Currently, only raw "link[N:M]" filtering is supported.
1575 case DLT_CAN_SOCKETCAN
:
1577 * Currently, only raw "link[N:M]" filtering is supported.
1587 off_macpl
= 24; /* ipnet header length */
1592 case DLT_IEEE802_15_4_NOFCS
:
1594 * Currently, only raw "link[N:M]" filtering is supported.
1602 bpf_error("unknown data link type %d", linktype
);
1607 * Load a value relative to the beginning of the link-layer header.
1608 * The link-layer header doesn't necessarily begin at the beginning
1609 * of the packet data; there might be a variable-length prefix containing
1610 * radio information.
1612 static struct slist
*
1613 gen_load_llrel(offset
, size
)
1616 struct slist
*s
, *s2
;
1618 s
= gen_llprefixlen();
1621 * If "s" is non-null, it has code to arrange that the X register
1622 * contains the length of the prefix preceding the link-layer
1625 * Otherwise, the length of the prefix preceding the link-layer
1626 * header is "off_ll".
1630 * There's a variable-length prefix preceding the
1631 * link-layer header. "s" points to a list of statements
1632 * that put the length of that prefix into the X register.
1633 * do an indirect load, to use the X register as an offset.
1635 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1640 * There is no variable-length header preceding the
1641 * link-layer header; add in off_ll, which, if there's
1642 * a fixed-length header preceding the link-layer header,
1643 * is the length of that header.
1645 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1646 s
->s
.k
= offset
+ off_ll
;
1652 * Load a value relative to the beginning of the MAC-layer payload.
1654 static struct slist
*
1655 gen_load_macplrel(offset
, size
)
1658 struct slist
*s
, *s2
;
1660 s
= gen_off_macpl();
1663 * If s is non-null, the offset of the MAC-layer payload is
1664 * variable, and s points to a list of instructions that
1665 * arrange that the X register contains that offset.
1667 * Otherwise, the offset of the MAC-layer payload is constant,
1668 * and is in off_macpl.
1672 * The offset of the MAC-layer payload is in the X
1673 * register. Do an indirect load, to use the X register
1676 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1681 * The offset of the MAC-layer payload is constant,
1682 * and is in off_macpl; load the value at that offset
1683 * plus the specified offset.
1685 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1686 s
->s
.k
= off_macpl
+ offset
;
1692 * Load a value relative to the beginning of the specified header.
1694 static struct slist
*
1695 gen_load_a(offrel
, offset
, size
)
1696 enum e_offrel offrel
;
1699 struct slist
*s
, *s2
;
1704 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1709 s
= gen_load_llrel(offset
, size
);
1713 s
= gen_load_macplrel(offset
, size
);
1717 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1721 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1726 * Load the X register with the length of the IPv4 header
1727 * (plus the offset of the link-layer header, if it's
1728 * preceded by a variable-length header such as a radio
1729 * header), in bytes.
1731 s
= gen_loadx_iphdrlen();
1734 * Load the item at {offset of the MAC-layer payload} +
1735 * {offset, relative to the start of the MAC-layer
1736 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1737 * {specified offset}.
1739 * (If the offset of the MAC-layer payload is variable,
1740 * it's included in the value in the X register, and
1743 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1744 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1749 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1760 * Generate code to load into the X register the sum of the length of
1761 * the IPv4 header and any variable-length header preceding the link-layer
1764 static struct slist
*
1765 gen_loadx_iphdrlen()
1767 struct slist
*s
, *s2
;
1769 s
= gen_off_macpl();
1772 * There's a variable-length prefix preceding the
1773 * link-layer header, or the link-layer header is itself
1774 * variable-length. "s" points to a list of statements
1775 * that put the offset of the MAC-layer payload into
1778 * The 4*([k]&0xf) addressing mode can't be used, as we
1779 * don't have a constant offset, so we have to load the
1780 * value in question into the A register and add to it
1781 * the value from the X register.
1783 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1786 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1789 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1794 * The A register now contains the length of the
1795 * IP header. We need to add to it the offset of
1796 * the MAC-layer payload, which is still in the X
1797 * register, and move the result into the X register.
1799 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1800 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1803 * There is no variable-length header preceding the
1804 * link-layer header, and the link-layer header is
1805 * fixed-length; load the length of the IPv4 header,
1806 * which is at an offset of off_nl from the beginning
1807 * of the MAC-layer payload, and thus at an offset
1808 * of off_mac_pl + off_nl from the beginning of the
1811 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1812 s
->s
.k
= off_macpl
+ off_nl
;
1817 static struct block
*
1824 s
= new_stmt(BPF_LD
|BPF_IMM
);
1826 b
= new_block(JMP(BPF_JEQ
));
1832 static inline struct block
*
1835 return gen_uncond(1);
1838 static inline struct block
*
1841 return gen_uncond(0);
1845 * Byte-swap a 32-bit number.
1846 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1847 * big-endian platforms.)
1849 #define SWAPLONG(y) \
1850 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1853 * Generate code to match a particular packet type.
1855 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1856 * value, if <= ETHERMTU. We use that to determine whether to
1857 * match the type/length field or to check the type/length field for
1858 * a value <= ETHERMTU to see whether it's a type field and then do
1859 * the appropriate test.
1861 static struct block
*
1862 gen_ether_linktype(proto
)
1865 struct block
*b0
, *b1
;
1871 case LLCSAP_NETBEUI
:
1873 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1874 * so we check the DSAP and SSAP.
1876 * LLCSAP_IP checks for IP-over-802.2, rather
1877 * than IP-over-Ethernet or IP-over-SNAP.
1879 * XXX - should we check both the DSAP and the
1880 * SSAP, like this, or should we check just the
1881 * DSAP, as we do for other types <= ETHERMTU
1882 * (i.e., other SAP values)?
1884 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1886 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1887 ((proto
<< 8) | proto
));
1895 * Ethernet_II frames, which are Ethernet
1896 * frames with a frame type of ETHERTYPE_IPX;
1898 * Ethernet_802.3 frames, which are 802.3
1899 * frames (i.e., the type/length field is
1900 * a length field, <= ETHERMTU, rather than
1901 * a type field) with the first two bytes
1902 * after the Ethernet/802.3 header being
1905 * Ethernet_802.2 frames, which are 802.3
1906 * frames with an 802.2 LLC header and
1907 * with the IPX LSAP as the DSAP in the LLC
1910 * Ethernet_SNAP frames, which are 802.3
1911 * frames with an LLC header and a SNAP
1912 * header and with an OUI of 0x000000
1913 * (encapsulated Ethernet) and a protocol
1914 * ID of ETHERTYPE_IPX in the SNAP header.
1916 * XXX - should we generate the same code both
1917 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1921 * This generates code to check both for the
1922 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1924 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1925 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1929 * Now we add code to check for SNAP frames with
1930 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1932 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1936 * Now we generate code to check for 802.3
1937 * frames in general.
1939 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1943 * Now add the check for 802.3 frames before the
1944 * check for Ethernet_802.2 and Ethernet_802.3,
1945 * as those checks should only be done on 802.3
1946 * frames, not on Ethernet frames.
1951 * Now add the check for Ethernet_II frames, and
1952 * do that before checking for the other frame
1955 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1956 (bpf_int32
)ETHERTYPE_IPX
);
1960 case ETHERTYPE_ATALK
:
1961 case ETHERTYPE_AARP
:
1963 * EtherTalk (AppleTalk protocols on Ethernet link
1964 * layer) may use 802.2 encapsulation.
1968 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1969 * we check for an Ethernet type field less than
1970 * 1500, which means it's an 802.3 length field.
1972 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1976 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1977 * SNAP packets with an organization code of
1978 * 0x080007 (Apple, for Appletalk) and a protocol
1979 * type of ETHERTYPE_ATALK (Appletalk).
1981 * 802.2-encapsulated ETHERTYPE_AARP packets are
1982 * SNAP packets with an organization code of
1983 * 0x000000 (encapsulated Ethernet) and a protocol
1984 * type of ETHERTYPE_AARP (Appletalk ARP).
1986 if (proto
== ETHERTYPE_ATALK
)
1987 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1988 else /* proto == ETHERTYPE_AARP */
1989 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1993 * Check for Ethernet encapsulation (Ethertalk
1994 * phase 1?); we just check for the Ethernet
1997 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2003 if (proto
<= ETHERMTU
) {
2005 * This is an LLC SAP value, so the frames
2006 * that match would be 802.2 frames.
2007 * Check that the frame is an 802.2 frame
2008 * (i.e., that the length/type field is
2009 * a length field, <= ETHERMTU) and
2010 * then check the DSAP.
2012 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
2014 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
2020 * This is an Ethernet type, so compare
2021 * the length/type field with it (if
2022 * the frame is an 802.2 frame, the length
2023 * field will be <= ETHERMTU, and, as
2024 * "proto" is > ETHERMTU, this test
2025 * will fail and the frame won't match,
2026 * which is what we want).
2028 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2035 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2036 * or IPv6 then we have an error.
2038 static struct block
*
2039 gen_ipnet_linktype(proto
)
2045 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2046 (bpf_int32
)IPH_AF_INET
);
2049 case ETHERTYPE_IPV6
:
2050 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2051 (bpf_int32
)IPH_AF_INET6
);
2062 * Generate code to match a particular packet type.
2064 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2065 * value, if <= ETHERMTU. We use that to determine whether to
2066 * match the type field or to check the type field for the special
2067 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2069 static struct block
*
2070 gen_linux_sll_linktype(proto
)
2073 struct block
*b0
, *b1
;
2079 case LLCSAP_NETBEUI
:
2081 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2082 * so we check the DSAP and SSAP.
2084 * LLCSAP_IP checks for IP-over-802.2, rather
2085 * than IP-over-Ethernet or IP-over-SNAP.
2087 * XXX - should we check both the DSAP and the
2088 * SSAP, like this, or should we check just the
2089 * DSAP, as we do for other types <= ETHERMTU
2090 * (i.e., other SAP values)?
2092 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2093 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2094 ((proto
<< 8) | proto
));
2100 * Ethernet_II frames, which are Ethernet
2101 * frames with a frame type of ETHERTYPE_IPX;
2103 * Ethernet_802.3 frames, which have a frame
2104 * type of LINUX_SLL_P_802_3;
2106 * Ethernet_802.2 frames, which are 802.3
2107 * frames with an 802.2 LLC header (i.e, have
2108 * a frame type of LINUX_SLL_P_802_2) and
2109 * with the IPX LSAP as the DSAP in the LLC
2112 * Ethernet_SNAP frames, which are 802.3
2113 * frames with an LLC header and a SNAP
2114 * header and with an OUI of 0x000000
2115 * (encapsulated Ethernet) and a protocol
2116 * ID of ETHERTYPE_IPX in the SNAP header.
2118 * First, do the checks on LINUX_SLL_P_802_2
2119 * frames; generate the check for either
2120 * Ethernet_802.2 or Ethernet_SNAP frames, and
2121 * then put a check for LINUX_SLL_P_802_2 frames
2124 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2125 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2127 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2131 * Now check for 802.3 frames and OR that with
2132 * the previous test.
2134 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2138 * Now add the check for Ethernet_II frames, and
2139 * do that before checking for the other frame
2142 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2143 (bpf_int32
)ETHERTYPE_IPX
);
2147 case ETHERTYPE_ATALK
:
2148 case ETHERTYPE_AARP
:
2150 * EtherTalk (AppleTalk protocols on Ethernet link
2151 * layer) may use 802.2 encapsulation.
2155 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2156 * we check for the 802.2 protocol type in the
2157 * "Ethernet type" field.
2159 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2162 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2163 * SNAP packets with an organization code of
2164 * 0x080007 (Apple, for Appletalk) and a protocol
2165 * type of ETHERTYPE_ATALK (Appletalk).
2167 * 802.2-encapsulated ETHERTYPE_AARP packets are
2168 * SNAP packets with an organization code of
2169 * 0x000000 (encapsulated Ethernet) and a protocol
2170 * type of ETHERTYPE_AARP (Appletalk ARP).
2172 if (proto
== ETHERTYPE_ATALK
)
2173 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2174 else /* proto == ETHERTYPE_AARP */
2175 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2179 * Check for Ethernet encapsulation (Ethertalk
2180 * phase 1?); we just check for the Ethernet
2183 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2189 if (proto
<= ETHERMTU
) {
2191 * This is an LLC SAP value, so the frames
2192 * that match would be 802.2 frames.
2193 * Check for the 802.2 protocol type
2194 * in the "Ethernet type" field, and
2195 * then check the DSAP.
2197 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2199 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2205 * This is an Ethernet type, so compare
2206 * the length/type field with it (if
2207 * the frame is an 802.2 frame, the length
2208 * field will be <= ETHERMTU, and, as
2209 * "proto" is > ETHERMTU, this test
2210 * will fail and the frame won't match,
2211 * which is what we want).
2213 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2219 static struct slist
*
2220 gen_load_prism_llprefixlen()
2222 struct slist
*s1
, *s2
;
2223 struct slist
*sjeq_avs_cookie
;
2224 struct slist
*sjcommon
;
2227 * This code is not compatible with the optimizer, as
2228 * we are generating jmp instructions within a normal
2229 * slist of instructions
2234 * Generate code to load the length of the radio header into
2235 * the register assigned to hold that length, if one has been
2236 * assigned. (If one hasn't been assigned, no code we've
2237 * generated uses that prefix, so we don't need to generate any
2240 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2241 * or always use the AVS header rather than the Prism header.
2242 * We load a 4-byte big-endian value at the beginning of the
2243 * raw packet data, and see whether, when masked with 0xFFFFF000,
2244 * it's equal to 0x80211000. If so, that indicates that it's
2245 * an AVS header (the masked-out bits are the version number).
2246 * Otherwise, it's a Prism header.
2248 * XXX - the Prism header is also, in theory, variable-length,
2249 * but no known software generates headers that aren't 144
2252 if (reg_off_ll
!= -1) {
2256 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2260 * AND it with 0xFFFFF000.
2262 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2263 s2
->s
.k
= 0xFFFFF000;
2267 * Compare with 0x80211000.
2269 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2270 sjeq_avs_cookie
->s
.k
= 0x80211000;
2271 sappend(s1
, sjeq_avs_cookie
);
2276 * The 4 bytes at an offset of 4 from the beginning of
2277 * the AVS header are the length of the AVS header.
2278 * That field is big-endian.
2280 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2283 sjeq_avs_cookie
->s
.jt
= s2
;
2286 * Now jump to the code to allocate a register
2287 * into which to save the header length and
2288 * store the length there. (The "jump always"
2289 * instruction needs to have the k field set;
2290 * it's added to the PC, so, as we're jumping
2291 * over a single instruction, it should be 1.)
2293 sjcommon
= new_stmt(JMP(BPF_JA
));
2295 sappend(s1
, sjcommon
);
2298 * Now for the code that handles the Prism header.
2299 * Just load the length of the Prism header (144)
2300 * into the A register. Have the test for an AVS
2301 * header branch here if we don't have an AVS header.
2303 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2306 sjeq_avs_cookie
->s
.jf
= s2
;
2309 * Now allocate a register to hold that value and store
2310 * it. The code for the AVS header will jump here after
2311 * loading the length of the AVS header.
2313 s2
= new_stmt(BPF_ST
);
2314 s2
->s
.k
= reg_off_ll
;
2316 sjcommon
->s
.jf
= s2
;
2319 * Now move it into the X register.
2321 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2329 static struct slist
*
2330 gen_load_avs_llprefixlen()
2332 struct slist
*s1
, *s2
;
2335 * Generate code to load the length of the AVS header into
2336 * the register assigned to hold that length, if one has been
2337 * assigned. (If one hasn't been assigned, no code we've
2338 * generated uses that prefix, so we don't need to generate any
2341 if (reg_off_ll
!= -1) {
2343 * The 4 bytes at an offset of 4 from the beginning of
2344 * the AVS header are the length of the AVS header.
2345 * That field is big-endian.
2347 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2351 * Now allocate a register to hold that value and store
2354 s2
= new_stmt(BPF_ST
);
2355 s2
->s
.k
= reg_off_ll
;
2359 * Now move it into the X register.
2361 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2369 static struct slist
*
2370 gen_load_radiotap_llprefixlen()
2372 struct slist
*s1
, *s2
;
2375 * Generate code to load the length of the radiotap header into
2376 * the register assigned to hold that length, if one has been
2377 * assigned. (If one hasn't been assigned, no code we've
2378 * generated uses that prefix, so we don't need to generate any
2381 if (reg_off_ll
!= -1) {
2383 * The 2 bytes at offsets of 2 and 3 from the beginning
2384 * of the radiotap header are the length of the radiotap
2385 * header; unfortunately, it's little-endian, so we have
2386 * to load it a byte at a time and construct the value.
2390 * Load the high-order byte, at an offset of 3, shift it
2391 * left a byte, and put the result in the X register.
2393 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2395 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2398 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2402 * Load the next byte, at an offset of 2, and OR the
2403 * value from the X register into it.
2405 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2408 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2412 * Now allocate a register to hold that value and store
2415 s2
= new_stmt(BPF_ST
);
2416 s2
->s
.k
= reg_off_ll
;
2420 * Now move it into the X register.
2422 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2431 * At the moment we treat PPI as normal Radiotap encoded
2432 * packets. The difference is in the function that generates
2433 * the code at the beginning to compute the header length.
2434 * Since this code generator of PPI supports bare 802.11
2435 * encapsulation only (i.e. the encapsulated DLT should be
2436 * DLT_IEEE802_11) we generate code to check for this too;
2437 * that's done in finish_parse().
2439 static struct slist
*
2440 gen_load_ppi_llprefixlen()
2442 struct slist
*s1
, *s2
;
2445 * Generate code to load the length of the radiotap header
2446 * into the register assigned to hold that length, if one has
2449 if (reg_off_ll
!= -1) {
2451 * The 2 bytes at offsets of 2 and 3 from the beginning
2452 * of the radiotap header are the length of the radiotap
2453 * header; unfortunately, it's little-endian, so we have
2454 * to load it a byte at a time and construct the value.
2458 * Load the high-order byte, at an offset of 3, shift it
2459 * left a byte, and put the result in the X register.
2461 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2463 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2466 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2470 * Load the next byte, at an offset of 2, and OR the
2471 * value from the X register into it.
2473 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2476 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2480 * Now allocate a register to hold that value and store
2483 s2
= new_stmt(BPF_ST
);
2484 s2
->s
.k
= reg_off_ll
;
2488 * Now move it into the X register.
2490 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2499 * Load a value relative to the beginning of the link-layer header after the 802.11
2500 * header, i.e. LLC_SNAP.
2501 * The link-layer header doesn't necessarily begin at the beginning
2502 * of the packet data; there might be a variable-length prefix containing
2503 * radio information.
2505 static struct slist
*
2506 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2509 struct slist
*sjset_data_frame_1
;
2510 struct slist
*sjset_data_frame_2
;
2511 struct slist
*sjset_qos
;
2512 struct slist
*sjset_radiotap_flags
;
2513 struct slist
*sjset_radiotap_tsft
;
2514 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2515 struct slist
*s_roundup
;
2517 if (reg_off_macpl
== -1) {
2519 * No register has been assigned to the offset of
2520 * the MAC-layer payload, which means nobody needs
2521 * it; don't bother computing it - just return
2522 * what we already have.
2528 * This code is not compatible with the optimizer, as
2529 * we are generating jmp instructions within a normal
2530 * slist of instructions
2535 * If "s" is non-null, it has code to arrange that the X register
2536 * contains the length of the prefix preceding the link-layer
2539 * Otherwise, the length of the prefix preceding the link-layer
2540 * header is "off_ll".
2544 * There is no variable-length header preceding the
2545 * link-layer header.
2547 * Load the length of the fixed-length prefix preceding
2548 * the link-layer header (if any) into the X register,
2549 * and store it in the reg_off_macpl register.
2550 * That length is off_ll.
2552 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2557 * The X register contains the offset of the beginning of the
2558 * link-layer header; add 24, which is the minimum length
2559 * of the MAC header for a data frame, to that, and store it
2560 * in reg_off_macpl, and then load the Frame Control field,
2561 * which is at the offset in the X register, with an indexed load.
2563 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2565 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2568 s2
= new_stmt(BPF_ST
);
2569 s2
->s
.k
= reg_off_macpl
;
2572 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2577 * Check the Frame Control field to see if this is a data frame;
2578 * a data frame has the 0x08 bit (b3) in that field set and the
2579 * 0x04 bit (b2) clear.
2581 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2582 sjset_data_frame_1
->s
.k
= 0x08;
2583 sappend(s
, sjset_data_frame_1
);
2586 * If b3 is set, test b2, otherwise go to the first statement of
2587 * the rest of the program.
2589 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2590 sjset_data_frame_2
->s
.k
= 0x04;
2591 sappend(s
, sjset_data_frame_2
);
2592 sjset_data_frame_1
->s
.jf
= snext
;
2595 * If b2 is not set, this is a data frame; test the QoS bit.
2596 * Otherwise, go to the first statement of the rest of the
2599 sjset_data_frame_2
->s
.jt
= snext
;
2600 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2601 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2602 sappend(s
, sjset_qos
);
2605 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2607 * Otherwise, go to the first statement of the rest of the
2610 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2611 s2
->s
.k
= reg_off_macpl
;
2613 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2616 s2
= new_stmt(BPF_ST
);
2617 s2
->s
.k
= reg_off_macpl
;
2621 * If we have a radiotap header, look at it to see whether
2622 * there's Atheros padding between the MAC-layer header
2625 * Note: all of the fields in the radiotap header are
2626 * little-endian, so we byte-swap all of the values
2627 * we test against, as they will be loaded as big-endian
2630 if (linktype
== DLT_IEEE802_11_RADIO
) {
2632 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2633 * in the presence flag?
2635 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2639 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2640 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2641 sappend(s
, sjset_radiotap_flags
);
2644 * If not, skip all of this.
2646 sjset_radiotap_flags
->s
.jf
= snext
;
2649 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2651 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2652 new_stmt(JMP(BPF_JSET
));
2653 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2654 sappend(s
, sjset_radiotap_tsft
);
2657 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2658 * at an offset of 16 from the beginning of the raw packet
2659 * data (8 bytes for the radiotap header and 8 bytes for
2662 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2665 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2669 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2670 sjset_tsft_datapad
->s
.k
= 0x20;
2671 sappend(s
, sjset_tsft_datapad
);
2674 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2675 * at an offset of 8 from the beginning of the raw packet
2676 * data (8 bytes for the radiotap header).
2678 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2681 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2685 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2686 sjset_notsft_datapad
->s
.k
= 0x20;
2687 sappend(s
, sjset_notsft_datapad
);
2690 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2691 * set, round the length of the 802.11 header to
2692 * a multiple of 4. Do that by adding 3 and then
2693 * dividing by and multiplying by 4, which we do by
2696 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2697 s_roundup
->s
.k
= reg_off_macpl
;
2698 sappend(s
, s_roundup
);
2699 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2702 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2705 s2
= new_stmt(BPF_ST
);
2706 s2
->s
.k
= reg_off_macpl
;
2709 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2710 sjset_tsft_datapad
->s
.jf
= snext
;
2711 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2712 sjset_notsft_datapad
->s
.jf
= snext
;
2714 sjset_qos
->s
.jf
= snext
;
2720 insert_compute_vloffsets(b
)
2726 * For link-layer types that have a variable-length header
2727 * preceding the link-layer header, generate code to load
2728 * the offset of the link-layer header into the register
2729 * assigned to that offset, if any.
2733 case DLT_PRISM_HEADER
:
2734 s
= gen_load_prism_llprefixlen();
2737 case DLT_IEEE802_11_RADIO_AVS
:
2738 s
= gen_load_avs_llprefixlen();
2741 case DLT_IEEE802_11_RADIO
:
2742 s
= gen_load_radiotap_llprefixlen();
2746 s
= gen_load_ppi_llprefixlen();
2755 * For link-layer types that have a variable-length link-layer
2756 * header, generate code to load the offset of the MAC-layer
2757 * payload into the register assigned to that offset, if any.
2761 case DLT_IEEE802_11
:
2762 case DLT_PRISM_HEADER
:
2763 case DLT_IEEE802_11_RADIO_AVS
:
2764 case DLT_IEEE802_11_RADIO
:
2766 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2771 * If we have any offset-loading code, append all the
2772 * existing statements in the block to those statements,
2773 * and make the resulting list the list of statements
2777 sappend(s
, b
->stmts
);
2782 static struct block
*
2783 gen_ppi_dlt_check(void)
2785 struct slist
*s_load_dlt
;
2788 if (linktype
== DLT_PPI
)
2790 /* Create the statements that check for the DLT
2792 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2793 s_load_dlt
->s
.k
= 4;
2795 b
= new_block(JMP(BPF_JEQ
));
2797 b
->stmts
= s_load_dlt
;
2798 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2808 static struct slist
*
2809 gen_prism_llprefixlen(void)
2813 if (reg_off_ll
== -1) {
2815 * We haven't yet assigned a register for the length
2816 * of the radio header; allocate one.
2818 reg_off_ll
= alloc_reg();
2822 * Load the register containing the radio length
2823 * into the X register.
2825 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2826 s
->s
.k
= reg_off_ll
;
2830 static struct slist
*
2831 gen_avs_llprefixlen(void)
2835 if (reg_off_ll
== -1) {
2837 * We haven't yet assigned a register for the length
2838 * of the AVS header; allocate one.
2840 reg_off_ll
= alloc_reg();
2844 * Load the register containing the AVS length
2845 * into the X register.
2847 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2848 s
->s
.k
= reg_off_ll
;
2852 static struct slist
*
2853 gen_radiotap_llprefixlen(void)
2857 if (reg_off_ll
== -1) {
2859 * We haven't yet assigned a register for the length
2860 * of the radiotap header; allocate one.
2862 reg_off_ll
= alloc_reg();
2866 * Load the register containing the radiotap length
2867 * into the X register.
2869 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2870 s
->s
.k
= reg_off_ll
;
2875 * At the moment we treat PPI as normal Radiotap encoded
2876 * packets. The difference is in the function that generates
2877 * the code at the beginning to compute the header length.
2878 * Since this code generator of PPI supports bare 802.11
2879 * encapsulation only (i.e. the encapsulated DLT should be
2880 * DLT_IEEE802_11) we generate code to check for this too.
2882 static struct slist
*
2883 gen_ppi_llprefixlen(void)
2887 if (reg_off_ll
== -1) {
2889 * We haven't yet assigned a register for the length
2890 * of the radiotap header; allocate one.
2892 reg_off_ll
= alloc_reg();
2896 * Load the register containing the PPI length
2897 * into the X register.
2899 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2900 s
->s
.k
= reg_off_ll
;
2905 * Generate code to compute the link-layer header length, if necessary,
2906 * putting it into the X register, and to return either a pointer to a
2907 * "struct slist" for the list of statements in that code, or NULL if
2908 * no code is necessary.
2910 static struct slist
*
2911 gen_llprefixlen(void)
2915 case DLT_PRISM_HEADER
:
2916 return gen_prism_llprefixlen();
2918 case DLT_IEEE802_11_RADIO_AVS
:
2919 return gen_avs_llprefixlen();
2921 case DLT_IEEE802_11_RADIO
:
2922 return gen_radiotap_llprefixlen();
2925 return gen_ppi_llprefixlen();
2933 * Generate code to load the register containing the offset of the
2934 * MAC-layer payload into the X register; if no register for that offset
2935 * has been allocated, allocate it first.
2937 static struct slist
*
2942 if (off_macpl_is_variable
) {
2943 if (reg_off_macpl
== -1) {
2945 * We haven't yet assigned a register for the offset
2946 * of the MAC-layer payload; allocate one.
2948 reg_off_macpl
= alloc_reg();
2952 * Load the register containing the offset of the MAC-layer
2953 * payload into the X register.
2955 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2956 s
->s
.k
= reg_off_macpl
;
2960 * That offset isn't variable, so we don't need to
2961 * generate any code.
2968 * Map an Ethernet type to the equivalent PPP type.
2971 ethertype_to_ppptype(proto
)
2981 case ETHERTYPE_IPV6
:
2990 case ETHERTYPE_ATALK
:
3004 * I'm assuming the "Bridging PDU"s that go
3005 * over PPP are Spanning Tree Protocol
3019 * Generate code to match a particular packet type by matching the
3020 * link-layer type field or fields in the 802.2 LLC header.
3022 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3023 * value, if <= ETHERMTU.
3025 static struct block
*
3029 struct block
*b0
, *b1
, *b2
;
3031 /* are we checking MPLS-encapsulated packets? */
3032 if (label_stack_depth
> 0) {
3036 /* FIXME add other L3 proto IDs */
3037 return gen_mpls_linktype(Q_IP
);
3039 case ETHERTYPE_IPV6
:
3041 /* FIXME add other L3 proto IDs */
3042 return gen_mpls_linktype(Q_IPV6
);
3045 bpf_error("unsupported protocol over mpls");
3051 * Are we testing PPPoE packets?
3055 * The PPPoE session header is part of the
3056 * MAC-layer payload, so all references
3057 * should be relative to the beginning of
3062 * We use Ethernet protocol types inside libpcap;
3063 * map them to the corresponding PPP protocol types.
3065 proto
= ethertype_to_ppptype(proto
);
3066 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3072 return gen_ether_linktype(proto
);
3080 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3084 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3091 case DLT_IEEE802_11
:
3092 case DLT_PRISM_HEADER
:
3093 case DLT_IEEE802_11_RADIO_AVS
:
3094 case DLT_IEEE802_11_RADIO
:
3097 * Check that we have a data frame.
3099 b0
= gen_check_802_11_data_frame();
3102 * Now check for the specified link-layer type.
3104 b1
= gen_llc_linktype(proto
);
3112 * XXX - check for asynchronous frames, as per RFC 1103.
3114 return gen_llc_linktype(proto
);
3120 * XXX - check for LLC PDUs, as per IEEE 802.5.
3122 return gen_llc_linktype(proto
);
3126 case DLT_ATM_RFC1483
:
3128 case DLT_IP_OVER_FC
:
3129 return gen_llc_linktype(proto
);
3135 * If "is_lane" is set, check for a LANE-encapsulated
3136 * version of this protocol, otherwise check for an
3137 * LLC-encapsulated version of this protocol.
3139 * We assume LANE means Ethernet, not Token Ring.
3143 * Check that the packet doesn't begin with an
3144 * LE Control marker. (We've already generated
3147 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3152 * Now generate an Ethernet test.
3154 b1
= gen_ether_linktype(proto
);
3159 * Check for LLC encapsulation and then check the
3162 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3163 b1
= gen_llc_linktype(proto
);
3171 return gen_linux_sll_linktype(proto
);
3176 case DLT_SLIP_BSDOS
:
3179 * These types don't provide any type field; packets
3180 * are always IPv4 or IPv6.
3182 * XXX - for IPv4, check for a version number of 4, and,
3183 * for IPv6, check for a version number of 6?
3188 /* Check for a version number of 4. */
3189 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3191 case ETHERTYPE_IPV6
:
3192 /* Check for a version number of 6. */
3193 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3197 return gen_false(); /* always false */
3204 * Raw IPv4, so no type field.
3206 if (proto
== ETHERTYPE_IP
)
3207 return gen_true(); /* always true */
3209 /* Checking for something other than IPv4; always false */
3216 * Raw IPv6, so no type field.
3219 if (proto
== ETHERTYPE_IPV6
)
3220 return gen_true(); /* always true */
3223 /* Checking for something other than IPv6; always false */
3230 case DLT_PPP_SERIAL
:
3233 * We use Ethernet protocol types inside libpcap;
3234 * map them to the corresponding PPP protocol types.
3236 proto
= ethertype_to_ppptype(proto
);
3237 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3243 * We use Ethernet protocol types inside libpcap;
3244 * map them to the corresponding PPP protocol types.
3250 * Also check for Van Jacobson-compressed IP.
3251 * XXX - do this for other forms of PPP?
3253 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3254 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3256 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3261 proto
= ethertype_to_ppptype(proto
);
3262 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3272 * For DLT_NULL, the link-layer header is a 32-bit
3273 * word containing an AF_ value in *host* byte order,
3274 * and for DLT_ENC, the link-layer header begins
3275 * with a 32-bit work containing an AF_ value in
3278 * In addition, if we're reading a saved capture file,
3279 * the host byte order in the capture may not be the
3280 * same as the host byte order on this machine.
3282 * For DLT_LOOP, the link-layer header is a 32-bit
3283 * word containing an AF_ value in *network* byte order.
3285 * XXX - AF_ values may, unfortunately, be platform-
3286 * dependent; for example, FreeBSD's AF_INET6 is 24
3287 * whilst NetBSD's and OpenBSD's is 26.
3289 * This means that, when reading a capture file, just
3290 * checking for our AF_INET6 value won't work if the
3291 * capture file came from another OS.
3300 case ETHERTYPE_IPV6
:
3307 * Not a type on which we support filtering.
3308 * XXX - support those that have AF_ values
3309 * #defined on this platform, at least?
3314 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3316 * The AF_ value is in host byte order, but
3317 * the BPF interpreter will convert it to
3318 * network byte order.
3320 * If this is a save file, and it's from a
3321 * machine with the opposite byte order to
3322 * ours, we byte-swap the AF_ value.
3324 * Then we run it through "htonl()", and
3325 * generate code to compare against the result.
3327 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3328 bpf_pcap
->sf
.swapped
)
3329 proto
= SWAPLONG(proto
);
3330 proto
= htonl(proto
);
3332 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3334 #ifdef HAVE_NET_PFVAR_H
3337 * af field is host byte order in contrast to the rest of
3340 if (proto
== ETHERTYPE_IP
)
3341 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3342 BPF_B
, (bpf_int32
)AF_INET
));
3344 else if (proto
== ETHERTYPE_IPV6
)
3345 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3346 BPF_B
, (bpf_int32
)AF_INET6
));
3352 #endif /* HAVE_NET_PFVAR_H */
3355 case DLT_ARCNET_LINUX
:
3357 * XXX should we check for first fragment if the protocol
3366 case ETHERTYPE_IPV6
:
3367 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3368 (bpf_int32
)ARCTYPE_INET6
));
3372 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3373 (bpf_int32
)ARCTYPE_IP
);
3374 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3375 (bpf_int32
)ARCTYPE_IP_OLD
);
3380 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3381 (bpf_int32
)ARCTYPE_ARP
);
3382 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3383 (bpf_int32
)ARCTYPE_ARP_OLD
);
3387 case ETHERTYPE_REVARP
:
3388 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3389 (bpf_int32
)ARCTYPE_REVARP
));
3391 case ETHERTYPE_ATALK
:
3392 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3393 (bpf_int32
)ARCTYPE_ATALK
));
3400 case ETHERTYPE_ATALK
:
3410 * XXX - assumes a 2-byte Frame Relay header with
3411 * DLCI and flags. What if the address is longer?
3417 * Check for the special NLPID for IP.
3419 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3422 case ETHERTYPE_IPV6
:
3424 * Check for the special NLPID for IPv6.
3426 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3431 * Check for several OSI protocols.
3433 * Frame Relay packets typically have an OSI
3434 * NLPID at the beginning; we check for each
3437 * What we check for is the NLPID and a frame
3438 * control field of UI, i.e. 0x03 followed
3441 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3442 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3443 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3455 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3457 case DLT_JUNIPER_MFR
:
3458 case DLT_JUNIPER_MLFR
:
3459 case DLT_JUNIPER_MLPPP
:
3460 case DLT_JUNIPER_ATM1
:
3461 case DLT_JUNIPER_ATM2
:
3462 case DLT_JUNIPER_PPPOE
:
3463 case DLT_JUNIPER_PPPOE_ATM
:
3464 case DLT_JUNIPER_GGSN
:
3465 case DLT_JUNIPER_ES
:
3466 case DLT_JUNIPER_MONITOR
:
3467 case DLT_JUNIPER_SERVICES
:
3468 case DLT_JUNIPER_ETHER
:
3469 case DLT_JUNIPER_PPP
:
3470 case DLT_JUNIPER_FRELAY
:
3471 case DLT_JUNIPER_CHDLC
:
3472 case DLT_JUNIPER_VP
:
3473 case DLT_JUNIPER_ST
:
3474 case DLT_JUNIPER_ISM
:
3475 /* just lets verify the magic number for now -
3476 * on ATM we may have up to 6 different encapsulations on the wire
3477 * and need a lot of heuristics to figure out that the payload
3480 * FIXME encapsulation specific BPF_ filters
3482 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3485 return gen_ipnet_linktype(proto
);
3487 case DLT_LINUX_IRDA
:
3488 bpf_error("IrDA link-layer type filtering not implemented");
3491 bpf_error("DOCSIS link-layer type filtering not implemented");
3494 case DLT_MTP2_WITH_PHDR
:
3495 bpf_error("MTP2 link-layer type filtering not implemented");
3498 bpf_error("ERF link-layer type filtering not implemented");
3502 bpf_error("PFSYNC link-layer type filtering not implemented");
3505 case DLT_LINUX_LAPD
:
3506 bpf_error("LAPD link-layer type filtering not implemented");
3510 case DLT_USB_LINUX_MMAPPED
:
3511 bpf_error("USB link-layer type filtering not implemented");
3513 case DLT_BLUETOOTH_HCI_H4
:
3514 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3515 bpf_error("Bluetooth link-layer type filtering not implemented");
3518 case DLT_CAN_SOCKETCAN
:
3519 bpf_error("CAN link-layer type filtering not implemented");
3521 case DLT_IEEE802_15_4
:
3522 case DLT_IEEE802_15_4_LINUX
:
3523 case DLT_IEEE802_15_4_NONASK_PHY
:
3524 case DLT_IEEE802_15_4_NOFCS
:
3525 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3527 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3528 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3531 bpf_error("SITA link-layer type filtering not implemented");
3534 bpf_error("RAIF1 link-layer type filtering not implemented");
3537 bpf_error("IPMB link-layer type filtering not implemented");
3540 bpf_error("AX.25 link-layer type filtering not implemented");
3544 * All the types that have no encapsulation should either be
3545 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3546 * all packets are IP packets, or should be handled in some
3547 * special case, if none of them are (if some are and some
3548 * aren't, the lack of encapsulation is a problem, as we'd
3549 * have to find some other way of determining the packet type).
3551 * Therefore, if "off_linktype" is -1, there's an error.
3553 if (off_linktype
== (u_int
)-1)
3557 * Any type not handled above should always have an Ethernet
3558 * type at an offset of "off_linktype".
3560 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3564 * Check for an LLC SNAP packet with a given organization code and
3565 * protocol type; we check the entire contents of the 802.2 LLC and
3566 * snap headers, checking for DSAP and SSAP of SNAP and a control
3567 * field of 0x03 in the LLC header, and for the specified organization
3568 * code and protocol type in the SNAP header.
3570 static struct block
*
3571 gen_snap(orgcode
, ptype
)
3572 bpf_u_int32 orgcode
;
3575 u_char snapblock
[8];
3577 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3578 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3579 snapblock
[2] = 0x03; /* control = UI */
3580 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3581 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3582 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3583 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3584 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3585 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3589 * Generate code to match a particular packet type, for link-layer types
3590 * using 802.2 LLC headers.
3592 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3593 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3595 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3596 * value, if <= ETHERMTU. We use that to determine whether to
3597 * match the DSAP or both DSAP and LSAP or to check the OUI and
3598 * protocol ID in a SNAP header.
3600 static struct block
*
3601 gen_llc_linktype(proto
)
3605 * XXX - handle token-ring variable-length header.
3611 case LLCSAP_NETBEUI
:
3613 * XXX - should we check both the DSAP and the
3614 * SSAP, like this, or should we check just the
3615 * DSAP, as we do for other types <= ETHERMTU
3616 * (i.e., other SAP values)?
3618 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3619 ((proto
<< 8) | proto
));
3623 * XXX - are there ever SNAP frames for IPX on
3624 * non-Ethernet 802.x networks?
3626 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3627 (bpf_int32
)LLCSAP_IPX
);
3629 case ETHERTYPE_ATALK
:
3631 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3632 * SNAP packets with an organization code of
3633 * 0x080007 (Apple, for Appletalk) and a protocol
3634 * type of ETHERTYPE_ATALK (Appletalk).
3636 * XXX - check for an organization code of
3637 * encapsulated Ethernet as well?
3639 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3643 * XXX - we don't have to check for IPX 802.3
3644 * here, but should we check for the IPX Ethertype?
3646 if (proto
<= ETHERMTU
) {
3648 * This is an LLC SAP value, so check
3651 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3654 * This is an Ethernet type; we assume that it's
3655 * unlikely that it'll appear in the right place
3656 * at random, and therefore check only the
3657 * location that would hold the Ethernet type
3658 * in a SNAP frame with an organization code of
3659 * 0x000000 (encapsulated Ethernet).
3661 * XXX - if we were to check for the SNAP DSAP and
3662 * LSAP, as per XXX, and were also to check for an
3663 * organization code of 0x000000 (encapsulated
3664 * Ethernet), we'd do
3666 * return gen_snap(0x000000, proto);
3668 * here; for now, we don't, as per the above.
3669 * I don't know whether it's worth the extra CPU
3670 * time to do the right check or not.
3672 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3677 static struct block
*
3678 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3682 u_int src_off
, dst_off
;
3684 struct block
*b0
, *b1
;
3698 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3699 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3705 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3706 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3713 b0
= gen_linktype(proto
);
3714 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3720 static struct block
*
3721 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3722 struct in6_addr
*addr
;
3723 struct in6_addr
*mask
;
3725 u_int src_off
, dst_off
;
3727 struct block
*b0
, *b1
;
3742 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3743 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3749 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3750 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3757 /* this order is important */
3758 a
= (u_int32_t
*)addr
;
3759 m
= (u_int32_t
*)mask
;
3760 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3761 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3763 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3765 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3767 b0
= gen_linktype(proto
);
3773 static struct block
*
3774 gen_ehostop(eaddr
, dir
)
3775 register const u_char
*eaddr
;
3778 register struct block
*b0
, *b1
;
3782 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3785 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3788 b0
= gen_ehostop(eaddr
, Q_SRC
);
3789 b1
= gen_ehostop(eaddr
, Q_DST
);
3795 b0
= gen_ehostop(eaddr
, Q_SRC
);
3796 b1
= gen_ehostop(eaddr
, Q_DST
);
3801 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3805 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3809 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3813 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3817 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3821 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3829 * Like gen_ehostop, but for DLT_FDDI
3831 static struct block
*
3832 gen_fhostop(eaddr
, dir
)
3833 register const u_char
*eaddr
;
3836 struct block
*b0
, *b1
;
3841 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3843 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3848 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3850 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3854 b0
= gen_fhostop(eaddr
, Q_SRC
);
3855 b1
= gen_fhostop(eaddr
, Q_DST
);
3861 b0
= gen_fhostop(eaddr
, Q_SRC
);
3862 b1
= gen_fhostop(eaddr
, Q_DST
);
3867 bpf_error("'addr1' is only supported on 802.11");
3871 bpf_error("'addr2' is only supported on 802.11");
3875 bpf_error("'addr3' is only supported on 802.11");
3879 bpf_error("'addr4' is only supported on 802.11");
3883 bpf_error("'ra' is only supported on 802.11");
3887 bpf_error("'ta' is only supported on 802.11");
3895 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3897 static struct block
*
3898 gen_thostop(eaddr
, dir
)
3899 register const u_char
*eaddr
;
3902 register struct block
*b0
, *b1
;
3906 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3909 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3912 b0
= gen_thostop(eaddr
, Q_SRC
);
3913 b1
= gen_thostop(eaddr
, Q_DST
);
3919 b0
= gen_thostop(eaddr
, Q_SRC
);
3920 b1
= gen_thostop(eaddr
, Q_DST
);
3925 bpf_error("'addr1' is only supported on 802.11");
3929 bpf_error("'addr2' is only supported on 802.11");
3933 bpf_error("'addr3' is only supported on 802.11");
3937 bpf_error("'addr4' is only supported on 802.11");
3941 bpf_error("'ra' is only supported on 802.11");
3945 bpf_error("'ta' is only supported on 802.11");
3953 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3954 * various 802.11 + radio headers.
3956 static struct block
*
3957 gen_wlanhostop(eaddr
, dir
)
3958 register const u_char
*eaddr
;
3961 register struct block
*b0
, *b1
, *b2
;
3962 register struct slist
*s
;
3964 #ifdef ENABLE_WLAN_FILTERING_PATCH
3967 * We need to disable the optimizer because the optimizer is buggy
3968 * and wipes out some LD instructions generated by the below
3969 * code to validate the Frame Control bits
3972 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3979 * For control frames, there is no SA.
3981 * For management frames, SA is at an
3982 * offset of 10 from the beginning of
3985 * For data frames, SA is at an offset
3986 * of 10 from the beginning of the packet
3987 * if From DS is clear, at an offset of
3988 * 16 from the beginning of the packet
3989 * if From DS is set and To DS is clear,
3990 * and an offset of 24 from the beginning
3991 * of the packet if From DS is set and To DS
3996 * Generate the tests to be done for data frames
3999 * First, check for To DS set, i.e. check "link[1] & 0x01".
4001 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4002 b1
= new_block(JMP(BPF_JSET
));
4003 b1
->s
.k
= 0x01; /* To DS */
4007 * If To DS is set, the SA is at 24.
4009 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4013 * Now, check for To DS not set, i.e. check
4014 * "!(link[1] & 0x01)".
4016 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4017 b2
= new_block(JMP(BPF_JSET
));
4018 b2
->s
.k
= 0x01; /* To DS */
4023 * If To DS is not set, the SA is at 16.
4025 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4029 * Now OR together the last two checks. That gives
4030 * the complete set of checks for data frames with
4036 * Now check for From DS being set, and AND that with
4037 * the ORed-together checks.
4039 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4040 b1
= new_block(JMP(BPF_JSET
));
4041 b1
->s
.k
= 0x02; /* From DS */
4046 * Now check for data frames with From DS not set.
4048 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4049 b2
= new_block(JMP(BPF_JSET
));
4050 b2
->s
.k
= 0x02; /* From DS */
4055 * If From DS isn't set, the SA is at 10.
4057 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4061 * Now OR together the checks for data frames with
4062 * From DS not set and for data frames with From DS
4063 * set; that gives the checks done for data frames.
4068 * Now check for a data frame.
4069 * I.e, check "link[0] & 0x08".
4071 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4072 b1
= new_block(JMP(BPF_JSET
));
4077 * AND that with the checks done for data frames.
4082 * If the high-order bit of the type value is 0, this
4083 * is a management frame.
4084 * I.e, check "!(link[0] & 0x08)".
4086 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4087 b2
= new_block(JMP(BPF_JSET
));
4093 * For management frames, the SA is at 10.
4095 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4099 * OR that with the checks done for data frames.
4100 * That gives the checks done for management and
4106 * If the low-order bit of the type value is 1,
4107 * this is either a control frame or a frame
4108 * with a reserved type, and thus not a
4111 * I.e., check "!(link[0] & 0x04)".
4113 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4114 b1
= new_block(JMP(BPF_JSET
));
4120 * AND that with the checks for data and management
4130 * For control frames, there is no DA.
4132 * For management frames, DA is at an
4133 * offset of 4 from the beginning of
4136 * For data frames, DA is at an offset
4137 * of 4 from the beginning of the packet
4138 * if To DS is clear and at an offset of
4139 * 16 from the beginning of the packet
4144 * Generate the tests to be done for data frames.
4146 * First, check for To DS set, i.e. "link[1] & 0x01".
4148 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4149 b1
= new_block(JMP(BPF_JSET
));
4150 b1
->s
.k
= 0x01; /* To DS */
4154 * If To DS is set, the DA is at 16.
4156 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4160 * Now, check for To DS not set, i.e. check
4161 * "!(link[1] & 0x01)".
4163 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4164 b2
= new_block(JMP(BPF_JSET
));
4165 b2
->s
.k
= 0x01; /* To DS */
4170 * If To DS is not set, the DA is at 4.
4172 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4176 * Now OR together the last two checks. That gives
4177 * the complete set of checks for data frames.
4182 * Now check for a data frame.
4183 * I.e, check "link[0] & 0x08".
4185 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4186 b1
= new_block(JMP(BPF_JSET
));
4191 * AND that with the checks done for data frames.
4196 * If the high-order bit of the type value is 0, this
4197 * is a management frame.
4198 * I.e, check "!(link[0] & 0x08)".
4200 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4201 b2
= new_block(JMP(BPF_JSET
));
4207 * For management frames, the DA is at 4.
4209 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4213 * OR that with the checks done for data frames.
4214 * That gives the checks done for management and
4220 * If the low-order bit of the type value is 1,
4221 * this is either a control frame or a frame
4222 * with a reserved type, and thus not a
4225 * I.e., check "!(link[0] & 0x04)".
4227 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4228 b1
= new_block(JMP(BPF_JSET
));
4234 * AND that with the checks for data and management
4242 * Not present in management frames; addr1 in other
4247 * If the high-order bit of the type value is 0, this
4248 * is a management frame.
4249 * I.e, check "(link[0] & 0x08)".
4251 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4252 b1
= new_block(JMP(BPF_JSET
));
4259 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4262 * AND that with the check of addr1.
4269 * Not present in management frames; addr2, if present,
4274 * Not present in CTS or ACK control frames.
4276 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4277 IEEE80211_FC0_TYPE_MASK
);
4279 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4280 IEEE80211_FC0_SUBTYPE_MASK
);
4282 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4283 IEEE80211_FC0_SUBTYPE_MASK
);
4289 * If the high-order bit of the type value is 0, this
4290 * is a management frame.
4291 * I.e, check "(link[0] & 0x08)".
4293 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4294 b1
= new_block(JMP(BPF_JSET
));
4299 * AND that with the check for frames other than
4300 * CTS and ACK frames.
4307 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4312 * XXX - add BSSID keyword?
4315 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4319 * Not present in CTS or ACK control frames.
4321 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4322 IEEE80211_FC0_TYPE_MASK
);
4324 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4325 IEEE80211_FC0_SUBTYPE_MASK
);
4327 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4328 IEEE80211_FC0_SUBTYPE_MASK
);
4332 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4338 * Not present in control frames.
4340 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4341 IEEE80211_FC0_TYPE_MASK
);
4343 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4349 * Present only if the direction mask has both "From DS"
4350 * and "To DS" set. Neither control frames nor management
4351 * frames should have both of those set, so we don't
4352 * check the frame type.
4354 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4355 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4356 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4361 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4362 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4368 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4369 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4378 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4379 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4380 * as the RFC states.)
4382 static struct block
*
4383 gen_ipfchostop(eaddr
, dir
)
4384 register const u_char
*eaddr
;
4387 register struct block
*b0
, *b1
;
4391 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4394 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4397 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4398 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4404 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4405 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4410 bpf_error("'addr1' is only supported on 802.11");
4414 bpf_error("'addr2' is only supported on 802.11");
4418 bpf_error("'addr3' is only supported on 802.11");
4422 bpf_error("'addr4' is only supported on 802.11");
4426 bpf_error("'ra' is only supported on 802.11");
4430 bpf_error("'ta' is only supported on 802.11");
4438 * This is quite tricky because there may be pad bytes in front of the
4439 * DECNET header, and then there are two possible data packet formats that
4440 * carry both src and dst addresses, plus 5 packet types in a format that
4441 * carries only the src node, plus 2 types that use a different format and
4442 * also carry just the src node.
4446 * Instead of doing those all right, we just look for data packets with
4447 * 0 or 1 bytes of padding. If you want to look at other packets, that
4448 * will require a lot more hacking.
4450 * To add support for filtering on DECNET "areas" (network numbers)
4451 * one would want to add a "mask" argument to this routine. That would
4452 * make the filter even more inefficient, although one could be clever
4453 * and not generate masking instructions if the mask is 0xFFFF.
4455 static struct block
*
4456 gen_dnhostop(addr
, dir
)
4460 struct block
*b0
, *b1
, *b2
, *tmp
;
4461 u_int offset_lh
; /* offset if long header is received */
4462 u_int offset_sh
; /* offset if short header is received */
4467 offset_sh
= 1; /* follows flags */
4468 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4472 offset_sh
= 3; /* follows flags, dstnode */
4473 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4477 /* Inefficient because we do our Calvinball dance twice */
4478 b0
= gen_dnhostop(addr
, Q_SRC
);
4479 b1
= gen_dnhostop(addr
, Q_DST
);
4485 /* Inefficient because we do our Calvinball dance twice */
4486 b0
= gen_dnhostop(addr
, Q_SRC
);
4487 b1
= gen_dnhostop(addr
, Q_DST
);
4492 bpf_error("ISO host filtering not implemented");
4497 b0
= gen_linktype(ETHERTYPE_DN
);
4498 /* Check for pad = 1, long header case */
4499 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4500 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4501 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4502 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4504 /* Check for pad = 0, long header case */
4505 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4506 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4509 /* Check for pad = 1, short header case */
4510 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4511 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4512 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4515 /* Check for pad = 0, short header case */
4516 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4517 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4521 /* Combine with test for linktype */
4527 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4528 * test the bottom-of-stack bit, and then check the version number
4529 * field in the IP header.
4531 static struct block
*
4532 gen_mpls_linktype(proto
)
4535 struct block
*b0
, *b1
;
4540 /* match the bottom-of-stack bit */
4541 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4542 /* match the IPv4 version number */
4543 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4548 /* match the bottom-of-stack bit */
4549 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4550 /* match the IPv4 version number */
4551 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4560 static struct block
*
4561 gen_host(addr
, mask
, proto
, dir
, type
)
4568 struct block
*b0
, *b1
;
4569 const char *typestr
;
4579 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4581 * Only check for non-IPv4 addresses if we're not
4582 * checking MPLS-encapsulated packets.
4584 if (label_stack_depth
== 0) {
4585 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4587 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4593 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4596 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4599 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4602 bpf_error("'tcp' modifier applied to %s", typestr
);
4605 bpf_error("'sctp' modifier applied to %s", typestr
);
4608 bpf_error("'udp' modifier applied to %s", typestr
);
4611 bpf_error("'icmp' modifier applied to %s", typestr
);
4614 bpf_error("'igmp' modifier applied to %s", typestr
);
4617 bpf_error("'igrp' modifier applied to %s", typestr
);
4620 bpf_error("'pim' modifier applied to %s", typestr
);
4623 bpf_error("'vrrp' modifier applied to %s", typestr
);
4626 bpf_error("ATALK host filtering not implemented");
4629 bpf_error("AARP host filtering not implemented");
4632 return gen_dnhostop(addr
, dir
);
4635 bpf_error("SCA host filtering not implemented");
4638 bpf_error("LAT host filtering not implemented");
4641 bpf_error("MOPDL host filtering not implemented");
4644 bpf_error("MOPRC host filtering not implemented");
4648 bpf_error("'ip6' modifier applied to ip host");
4651 bpf_error("'icmp6' modifier applied to %s", typestr
);
4655 bpf_error("'ah' modifier applied to %s", typestr
);
4658 bpf_error("'esp' modifier applied to %s", typestr
);
4661 bpf_error("ISO host filtering not implemented");
4664 bpf_error("'esis' modifier applied to %s", typestr
);
4667 bpf_error("'isis' modifier applied to %s", typestr
);
4670 bpf_error("'clnp' modifier applied to %s", typestr
);
4673 bpf_error("'stp' modifier applied to %s", typestr
);
4676 bpf_error("IPX host filtering not implemented");
4679 bpf_error("'netbeui' modifier applied to %s", typestr
);
4682 bpf_error("'radio' modifier applied to %s", typestr
);
4691 static struct block
*
4692 gen_host6(addr
, mask
, proto
, dir
, type
)
4693 struct in6_addr
*addr
;
4694 struct in6_addr
*mask
;
4699 const char *typestr
;
4709 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4712 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4715 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4718 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4721 bpf_error("'sctp' modifier applied to %s", typestr
);
4724 bpf_error("'tcp' modifier applied to %s", typestr
);
4727 bpf_error("'udp' modifier applied to %s", typestr
);
4730 bpf_error("'icmp' modifier applied to %s", typestr
);
4733 bpf_error("'igmp' modifier applied to %s", typestr
);
4736 bpf_error("'igrp' modifier applied to %s", typestr
);
4739 bpf_error("'pim' modifier applied to %s", typestr
);
4742 bpf_error("'vrrp' modifier applied to %s", typestr
);
4745 bpf_error("ATALK host filtering not implemented");
4748 bpf_error("AARP host filtering not implemented");
4751 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4754 bpf_error("SCA host filtering not implemented");
4757 bpf_error("LAT host filtering not implemented");
4760 bpf_error("MOPDL host filtering not implemented");
4763 bpf_error("MOPRC host filtering not implemented");
4766 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4769 bpf_error("'icmp6' modifier applied to %s", typestr
);
4772 bpf_error("'ah' modifier applied to %s", typestr
);
4775 bpf_error("'esp' modifier applied to %s", typestr
);
4778 bpf_error("ISO host filtering not implemented");
4781 bpf_error("'esis' modifier applied to %s", typestr
);
4784 bpf_error("'isis' modifier applied to %s", typestr
);
4787 bpf_error("'clnp' modifier applied to %s", typestr
);
4790 bpf_error("'stp' modifier applied to %s", typestr
);
4793 bpf_error("IPX host filtering not implemented");
4796 bpf_error("'netbeui' modifier applied to %s", typestr
);
4799 bpf_error("'radio' modifier applied to %s", typestr
);
4809 static struct block
*
4810 gen_gateway(eaddr
, alist
, proto
, dir
)
4811 const u_char
*eaddr
;
4812 bpf_u_int32
**alist
;
4816 struct block
*b0
, *b1
, *tmp
;
4819 bpf_error("direction applied to 'gateway'");
4828 b0
= gen_ehostop(eaddr
, Q_OR
);
4831 b0
= gen_fhostop(eaddr
, Q_OR
);
4834 b0
= gen_thostop(eaddr
, Q_OR
);
4836 case DLT_IEEE802_11
:
4837 case DLT_PRISM_HEADER
:
4838 case DLT_IEEE802_11_RADIO_AVS
:
4839 case DLT_IEEE802_11_RADIO
:
4841 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4846 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4848 * Check that the packet doesn't begin with an
4849 * LE Control marker. (We've already generated
4852 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4857 * Now check the MAC address.
4859 b0
= gen_ehostop(eaddr
, Q_OR
);
4862 case DLT_IP_OVER_FC
:
4863 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4867 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4869 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4871 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4880 bpf_error("illegal modifier of 'gateway'");
4886 gen_proto_abbrev(proto
)
4895 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4897 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4903 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4905 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4911 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4913 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4919 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4922 #ifndef IPPROTO_IGMP
4923 #define IPPROTO_IGMP 2
4927 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4930 #ifndef IPPROTO_IGRP
4931 #define IPPROTO_IGRP 9
4934 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4938 #define IPPROTO_PIM 103
4942 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4944 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4949 #ifndef IPPROTO_VRRP
4950 #define IPPROTO_VRRP 112
4954 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4958 b1
= gen_linktype(ETHERTYPE_IP
);
4962 b1
= gen_linktype(ETHERTYPE_ARP
);
4966 b1
= gen_linktype(ETHERTYPE_REVARP
);
4970 bpf_error("link layer applied in wrong context");
4973 b1
= gen_linktype(ETHERTYPE_ATALK
);
4977 b1
= gen_linktype(ETHERTYPE_AARP
);
4981 b1
= gen_linktype(ETHERTYPE_DN
);
4985 b1
= gen_linktype(ETHERTYPE_SCA
);
4989 b1
= gen_linktype(ETHERTYPE_LAT
);
4993 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4997 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5002 b1
= gen_linktype(ETHERTYPE_IPV6
);
5005 #ifndef IPPROTO_ICMPV6
5006 #define IPPROTO_ICMPV6 58
5009 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5014 #define IPPROTO_AH 51
5017 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5019 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5025 #define IPPROTO_ESP 50
5028 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5030 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5036 b1
= gen_linktype(LLCSAP_ISONS
);
5040 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5044 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5047 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5048 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5049 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5051 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5053 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5055 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5059 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5060 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5061 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5063 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5065 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5067 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5071 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5072 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5073 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5075 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5080 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5081 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5086 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5087 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5089 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5091 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5096 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5097 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5102 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5103 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5108 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5112 b1
= gen_linktype(LLCSAP_8021D
);
5116 b1
= gen_linktype(LLCSAP_IPX
);
5120 b1
= gen_linktype(LLCSAP_NETBEUI
);
5124 bpf_error("'radio' is not a valid protocol type");
5132 static struct block
*
5139 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5140 b
= new_block(JMP(BPF_JSET
));
5149 * Generate a comparison to a port value in the transport-layer header
5150 * at the specified offset from the beginning of that header.
5152 * XXX - this handles a variable-length prefix preceding the link-layer
5153 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5154 * variable-length link-layer headers (such as Token Ring or 802.11
5157 static struct block
*
5158 gen_portatom(off
, v
)
5162 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5166 static struct block
*
5167 gen_portatom6(off
, v
)
5171 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5176 gen_portop(port
, proto
, dir
)
5177 int port
, proto
, dir
;
5179 struct block
*b0
, *b1
, *tmp
;
5181 /* ip proto 'proto' */
5182 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5188 b1
= gen_portatom(0, (bpf_int32
)port
);
5192 b1
= gen_portatom(2, (bpf_int32
)port
);
5197 tmp
= gen_portatom(0, (bpf_int32
)port
);
5198 b1
= gen_portatom(2, (bpf_int32
)port
);
5203 tmp
= gen_portatom(0, (bpf_int32
)port
);
5204 b1
= gen_portatom(2, (bpf_int32
)port
);
5216 static struct block
*
5217 gen_port(port
, ip_proto
, dir
)
5222 struct block
*b0
, *b1
, *tmp
;
5227 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5228 * not LLC encapsulation with LLCSAP_IP.
5230 * For IEEE 802 networks - which includes 802.5 token ring
5231 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5232 * says that SNAP encapsulation is used, not LLC encapsulation
5235 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5236 * RFC 2225 say that SNAP encapsulation is used, not LLC
5237 * encapsulation with LLCSAP_IP.
5239 * So we always check for ETHERTYPE_IP.
5241 b0
= gen_linktype(ETHERTYPE_IP
);
5247 b1
= gen_portop(port
, ip_proto
, dir
);
5251 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5252 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5254 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5267 gen_portop6(port
, proto
, dir
)
5268 int port
, proto
, dir
;
5270 struct block
*b0
, *b1
, *tmp
;
5272 /* ip6 proto 'proto' */
5273 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5277 b1
= gen_portatom6(0, (bpf_int32
)port
);
5281 b1
= gen_portatom6(2, (bpf_int32
)port
);
5286 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5287 b1
= gen_portatom6(2, (bpf_int32
)port
);
5292 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5293 b1
= gen_portatom6(2, (bpf_int32
)port
);
5305 static struct block
*
5306 gen_port6(port
, ip_proto
, dir
)
5311 struct block
*b0
, *b1
, *tmp
;
5313 /* link proto ip6 */
5314 b0
= gen_linktype(ETHERTYPE_IPV6
);
5320 b1
= gen_portop6(port
, ip_proto
, dir
);
5324 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5325 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5327 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5339 /* gen_portrange code */
5340 static struct block
*
5341 gen_portrangeatom(off
, v1
, v2
)
5345 struct block
*b1
, *b2
;
5349 * Reverse the order of the ports, so v1 is the lower one.
5358 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5359 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5367 gen_portrangeop(port1
, port2
, proto
, dir
)
5372 struct block
*b0
, *b1
, *tmp
;
5374 /* ip proto 'proto' */
5375 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5381 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5385 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5390 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5391 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5396 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5397 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5409 static struct block
*
5410 gen_portrange(port1
, port2
, ip_proto
, dir
)
5415 struct block
*b0
, *b1
, *tmp
;
5418 b0
= gen_linktype(ETHERTYPE_IP
);
5424 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5428 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5429 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5431 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5443 static struct block
*
5444 gen_portrangeatom6(off
, v1
, v2
)
5448 struct block
*b1
, *b2
;
5452 * Reverse the order of the ports, so v1 is the lower one.
5461 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5462 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5470 gen_portrangeop6(port1
, port2
, proto
, dir
)
5475 struct block
*b0
, *b1
, *tmp
;
5477 /* ip6 proto 'proto' */
5478 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5482 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5486 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5491 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5492 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5497 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5498 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5510 static struct block
*
5511 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5516 struct block
*b0
, *b1
, *tmp
;
5518 /* link proto ip6 */
5519 b0
= gen_linktype(ETHERTYPE_IPV6
);
5525 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5529 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5530 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5532 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5545 lookup_proto(name
, proto
)
5546 register const char *name
;
5556 v
= pcap_nametoproto(name
);
5557 if (v
== PROTO_UNDEF
)
5558 bpf_error("unknown ip proto '%s'", name
);
5562 /* XXX should look up h/w protocol type based on linktype */
5563 v
= pcap_nametoeproto(name
);
5564 if (v
== PROTO_UNDEF
) {
5565 v
= pcap_nametollc(name
);
5566 if (v
== PROTO_UNDEF
)
5567 bpf_error("unknown ether proto '%s'", name
);
5572 if (strcmp(name
, "esis") == 0)
5574 else if (strcmp(name
, "isis") == 0)
5576 else if (strcmp(name
, "clnp") == 0)
5579 bpf_error("unknown osi proto '%s'", name
);
5599 static struct block
*
5600 gen_protochain(v
, proto
, dir
)
5605 #ifdef NO_PROTOCHAIN
5606 return gen_proto(v
, proto
, dir
);
5608 struct block
*b0
, *b
;
5609 struct slist
*s
[100];
5610 int fix2
, fix3
, fix4
, fix5
;
5611 int ahcheck
, again
, end
;
5613 int reg2
= alloc_reg();
5615 memset(s
, 0, sizeof(s
));
5616 fix2
= fix3
= fix4
= fix5
= 0;
5623 b0
= gen_protochain(v
, Q_IP
, dir
);
5624 b
= gen_protochain(v
, Q_IPV6
, dir
);
5628 bpf_error("bad protocol applied for 'protochain'");
5633 * We don't handle variable-length prefixes before the link-layer
5634 * header, or variable-length link-layer headers, here yet.
5635 * We might want to add BPF instructions to do the protochain
5636 * work, to simplify that and, on platforms that have a BPF
5637 * interpreter with the new instructions, let the filtering
5638 * be done in the kernel. (We already require a modified BPF
5639 * engine to do the protochain stuff, to support backward
5640 * branches, and backward branch support is unlikely to appear
5641 * in kernel BPF engines.)
5645 case DLT_IEEE802_11
:
5646 case DLT_PRISM_HEADER
:
5647 case DLT_IEEE802_11_RADIO_AVS
:
5648 case DLT_IEEE802_11_RADIO
:
5650 bpf_error("'protochain' not supported with 802.11");
5653 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5656 * s[0] is a dummy entry to protect other BPF insn from damage
5657 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5658 * hard to find interdependency made by jump table fixup.
5661 s
[i
] = new_stmt(0); /*dummy*/
5666 b0
= gen_linktype(ETHERTYPE_IP
);
5669 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5670 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5672 /* X = ip->ip_hl << 2 */
5673 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5674 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5679 b0
= gen_linktype(ETHERTYPE_IPV6
);
5681 /* A = ip6->ip_nxt */
5682 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5683 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5685 /* X = sizeof(struct ip6_hdr) */
5686 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5692 bpf_error("unsupported proto to gen_protochain");
5696 /* again: if (A == v) goto end; else fall through; */
5698 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5700 s
[i
]->s
.jt
= NULL
; /*later*/
5701 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5705 #ifndef IPPROTO_NONE
5706 #define IPPROTO_NONE 59
5708 /* if (A == IPPROTO_NONE) goto end */
5709 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5710 s
[i
]->s
.jt
= NULL
; /*later*/
5711 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5712 s
[i
]->s
.k
= IPPROTO_NONE
;
5713 s
[fix5
]->s
.jf
= s
[i
];
5718 if (proto
== Q_IPV6
) {
5719 int v6start
, v6end
, v6advance
, j
;
5722 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5723 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5724 s
[i
]->s
.jt
= NULL
; /*later*/
5725 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5726 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5727 s
[fix2
]->s
.jf
= s
[i
];
5729 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5730 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5731 s
[i
]->s
.jt
= NULL
; /*later*/
5732 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5733 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5735 /* if (A == IPPROTO_ROUTING) goto v6advance */
5736 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5737 s
[i
]->s
.jt
= NULL
; /*later*/
5738 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5739 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5741 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5742 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5743 s
[i
]->s
.jt
= NULL
; /*later*/
5744 s
[i
]->s
.jf
= NULL
; /*later*/
5745 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5756 * X = X + (P[X + 1] + 1) * 8;
5759 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5761 /* A = P[X + packet head] */
5762 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5763 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5766 s
[i
] = new_stmt(BPF_ST
);
5770 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5773 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5777 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5779 /* A = P[X + packet head]; */
5780 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5781 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5784 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5788 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5792 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5795 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5799 /* goto again; (must use BPF_JA for backward jump) */
5800 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5801 s
[i
]->s
.k
= again
- i
- 1;
5802 s
[i
- 1]->s
.jf
= s
[i
];
5806 for (j
= v6start
; j
<= v6end
; j
++)
5807 s
[j
]->s
.jt
= s
[v6advance
];
5812 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5814 s
[fix2
]->s
.jf
= s
[i
];
5820 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5821 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5822 s
[i
]->s
.jt
= NULL
; /*later*/
5823 s
[i
]->s
.jf
= NULL
; /*later*/
5824 s
[i
]->s
.k
= IPPROTO_AH
;
5826 s
[fix3
]->s
.jf
= s
[ahcheck
];
5833 * X = X + (P[X + 1] + 2) * 4;
5836 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5838 /* A = P[X + packet head]; */
5839 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5840 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5843 s
[i
] = new_stmt(BPF_ST
);
5847 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5850 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5854 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5856 /* A = P[X + packet head] */
5857 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5858 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5861 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5865 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5869 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5872 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5876 /* goto again; (must use BPF_JA for backward jump) */
5877 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5878 s
[i
]->s
.k
= again
- i
- 1;
5883 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5885 s
[fix2
]->s
.jt
= s
[end
];
5886 s
[fix4
]->s
.jf
= s
[end
];
5887 s
[fix5
]->s
.jt
= s
[end
];
5894 for (i
= 0; i
< max
- 1; i
++)
5895 s
[i
]->next
= s
[i
+ 1];
5896 s
[max
- 1]->next
= NULL
;
5901 b
= new_block(JMP(BPF_JEQ
));
5902 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5912 static struct block
*
5913 gen_check_802_11_data_frame()
5916 struct block
*b0
, *b1
;
5919 * A data frame has the 0x08 bit (b3) in the frame control field set
5920 * and the 0x04 bit (b2) clear.
5922 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5923 b0
= new_block(JMP(BPF_JSET
));
5927 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5928 b1
= new_block(JMP(BPF_JSET
));
5939 * Generate code that checks whether the packet is a packet for protocol
5940 * <proto> and whether the type field in that protocol's header has
5941 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5942 * IP packet and checks the protocol number in the IP header against <v>.
5944 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5945 * against Q_IP and Q_IPV6.
5947 static struct block
*
5948 gen_proto(v
, proto
, dir
)
5953 struct block
*b0
, *b1
;
5955 if (dir
!= Q_DEFAULT
)
5956 bpf_error("direction applied to 'proto'");
5961 b0
= gen_proto(v
, Q_IP
, dir
);
5962 b1
= gen_proto(v
, Q_IPV6
, dir
);
5970 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5971 * not LLC encapsulation with LLCSAP_IP.
5973 * For IEEE 802 networks - which includes 802.5 token ring
5974 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5975 * says that SNAP encapsulation is used, not LLC encapsulation
5978 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5979 * RFC 2225 say that SNAP encapsulation is used, not LLC
5980 * encapsulation with LLCSAP_IP.
5982 * So we always check for ETHERTYPE_IP.
5984 b0
= gen_linktype(ETHERTYPE_IP
);
5986 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5988 b1
= gen_protochain(v
, Q_IP
);
5998 * Frame Relay packets typically have an OSI
5999 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6000 * generates code to check for all the OSI
6001 * NLPIDs, so calling it and then adding a check
6002 * for the particular NLPID for which we're
6003 * looking is bogus, as we can just check for
6006 * What we check for is the NLPID and a frame
6007 * control field value of UI, i.e. 0x03 followed
6010 * XXX - assumes a 2-byte Frame Relay header with
6011 * DLCI and flags. What if the address is longer?
6013 * XXX - what about SNAP-encapsulated frames?
6015 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6021 * Cisco uses an Ethertype lookalike - for OSI,
6024 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6025 /* OSI in C-HDLC is stuffed with a fudge byte */
6026 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6031 b0
= gen_linktype(LLCSAP_ISONS
);
6032 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6038 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6040 * 4 is the offset of the PDU type relative to the IS-IS
6043 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6048 bpf_error("arp does not encapsulate another protocol");
6052 bpf_error("rarp does not encapsulate another protocol");
6056 bpf_error("atalk encapsulation is not specifiable");
6060 bpf_error("decnet encapsulation is not specifiable");
6064 bpf_error("sca does not encapsulate another protocol");
6068 bpf_error("lat does not encapsulate another protocol");
6072 bpf_error("moprc does not encapsulate another protocol");
6076 bpf_error("mopdl does not encapsulate another protocol");
6080 return gen_linktype(v
);
6083 bpf_error("'udp proto' is bogus");
6087 bpf_error("'tcp proto' is bogus");
6091 bpf_error("'sctp proto' is bogus");
6095 bpf_error("'icmp proto' is bogus");
6099 bpf_error("'igmp proto' is bogus");
6103 bpf_error("'igrp proto' is bogus");
6107 bpf_error("'pim proto' is bogus");
6111 bpf_error("'vrrp proto' is bogus");
6116 b0
= gen_linktype(ETHERTYPE_IPV6
);
6118 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6120 b1
= gen_protochain(v
, Q_IPV6
);
6126 bpf_error("'icmp6 proto' is bogus");
6130 bpf_error("'ah proto' is bogus");
6133 bpf_error("'ah proto' is bogus");
6136 bpf_error("'stp proto' is bogus");
6139 bpf_error("'ipx proto' is bogus");
6142 bpf_error("'netbeui proto' is bogus");
6145 bpf_error("'radio proto' is bogus");
6156 register const char *name
;
6159 int proto
= q
.proto
;
6163 bpf_u_int32 mask
, addr
;
6165 bpf_u_int32
**alist
;
6168 struct sockaddr_in
*sin4
;
6169 struct sockaddr_in6
*sin6
;
6170 struct addrinfo
*res
, *res0
;
6171 struct in6_addr mask128
;
6173 struct block
*b
, *tmp
;
6174 int port
, real_proto
;
6180 addr
= pcap_nametonetaddr(name
);
6182 bpf_error("unknown network '%s'", name
);
6183 /* Left justify network addr and calculate its network mask */
6185 while (addr
&& (addr
& 0xff000000) == 0) {
6189 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6193 if (proto
== Q_LINK
) {
6197 eaddr
= pcap_ether_hostton(name
);
6200 "unknown ether host '%s'", name
);
6201 b
= gen_ehostop(eaddr
, dir
);
6206 eaddr
= pcap_ether_hostton(name
);
6209 "unknown FDDI host '%s'", name
);
6210 b
= gen_fhostop(eaddr
, dir
);
6215 eaddr
= pcap_ether_hostton(name
);
6218 "unknown token ring host '%s'", name
);
6219 b
= gen_thostop(eaddr
, dir
);
6223 case DLT_IEEE802_11
:
6224 case DLT_PRISM_HEADER
:
6225 case DLT_IEEE802_11_RADIO_AVS
:
6226 case DLT_IEEE802_11_RADIO
:
6228 eaddr
= pcap_ether_hostton(name
);
6231 "unknown 802.11 host '%s'", name
);
6232 b
= gen_wlanhostop(eaddr
, dir
);
6236 case DLT_IP_OVER_FC
:
6237 eaddr
= pcap_ether_hostton(name
);
6240 "unknown Fibre Channel host '%s'", name
);
6241 b
= gen_ipfchostop(eaddr
, dir
);
6250 * Check that the packet doesn't begin
6251 * with an LE Control marker. (We've
6252 * already generated a test for LANE.)
6254 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6258 eaddr
= pcap_ether_hostton(name
);
6261 "unknown ether host '%s'", name
);
6262 b
= gen_ehostop(eaddr
, dir
);
6268 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6269 } else if (proto
== Q_DECNET
) {
6270 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6272 * I don't think DECNET hosts can be multihomed, so
6273 * there is no need to build up a list of addresses
6275 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6278 alist
= pcap_nametoaddr(name
);
6279 if (alist
== NULL
|| *alist
== NULL
)
6280 bpf_error("unknown host '%s'", name
);
6282 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6284 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6286 tmp
= gen_host(**alist
++, 0xffffffff,
6287 tproto
, dir
, q
.addr
);
6293 memset(&mask128
, 0xff, sizeof(mask128
));
6294 res0
= res
= pcap_nametoaddrinfo(name
);
6296 bpf_error("unknown host '%s'", name
);
6299 tproto
= tproto6
= proto
;
6300 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6304 for (res
= res0
; res
; res
= res
->ai_next
) {
6305 switch (res
->ai_family
) {
6307 if (tproto
== Q_IPV6
)
6310 sin4
= (struct sockaddr_in
*)
6312 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6313 0xffffffff, tproto
, dir
, q
.addr
);
6316 if (tproto6
== Q_IP
)
6319 sin6
= (struct sockaddr_in6
*)
6321 tmp
= gen_host6(&sin6
->sin6_addr
,
6322 &mask128
, tproto6
, dir
, q
.addr
);
6334 bpf_error("unknown host '%s'%s", name
,
6335 (proto
== Q_DEFAULT
)
6337 : " for specified address family");
6344 if (proto
!= Q_DEFAULT
&&
6345 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6346 bpf_error("illegal qualifier of 'port'");
6347 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6348 bpf_error("unknown port '%s'", name
);
6349 if (proto
== Q_UDP
) {
6350 if (real_proto
== IPPROTO_TCP
)
6351 bpf_error("port '%s' is tcp", name
);
6352 else if (real_proto
== IPPROTO_SCTP
)
6353 bpf_error("port '%s' is sctp", name
);
6355 /* override PROTO_UNDEF */
6356 real_proto
= IPPROTO_UDP
;
6358 if (proto
== Q_TCP
) {
6359 if (real_proto
== IPPROTO_UDP
)
6360 bpf_error("port '%s' is udp", name
);
6362 else if (real_proto
== IPPROTO_SCTP
)
6363 bpf_error("port '%s' is sctp", name
);
6365 /* override PROTO_UNDEF */
6366 real_proto
= IPPROTO_TCP
;
6368 if (proto
== Q_SCTP
) {
6369 if (real_proto
== IPPROTO_UDP
)
6370 bpf_error("port '%s' is udp", name
);
6372 else if (real_proto
== IPPROTO_TCP
)
6373 bpf_error("port '%s' is tcp", name
);
6375 /* override PROTO_UNDEF */
6376 real_proto
= IPPROTO_SCTP
;
6379 bpf_error("illegal port number %d < 0", port
);
6381 bpf_error("illegal port number %d > 65535", port
);
6383 return gen_port(port
, real_proto
, dir
);
6385 b
= gen_port(port
, real_proto
, dir
);
6386 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6391 if (proto
!= Q_DEFAULT
&&
6392 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6393 bpf_error("illegal qualifier of 'portrange'");
6394 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6395 bpf_error("unknown port in range '%s'", name
);
6396 if (proto
== Q_UDP
) {
6397 if (real_proto
== IPPROTO_TCP
)
6398 bpf_error("port in range '%s' is tcp", name
);
6399 else if (real_proto
== IPPROTO_SCTP
)
6400 bpf_error("port in range '%s' is sctp", name
);
6402 /* override PROTO_UNDEF */
6403 real_proto
= IPPROTO_UDP
;
6405 if (proto
== Q_TCP
) {
6406 if (real_proto
== IPPROTO_UDP
)
6407 bpf_error("port in range '%s' is udp", name
);
6408 else if (real_proto
== IPPROTO_SCTP
)
6409 bpf_error("port in range '%s' is sctp", name
);
6411 /* override PROTO_UNDEF */
6412 real_proto
= IPPROTO_TCP
;
6414 if (proto
== Q_SCTP
) {
6415 if (real_proto
== IPPROTO_UDP
)
6416 bpf_error("port in range '%s' is udp", name
);
6417 else if (real_proto
== IPPROTO_TCP
)
6418 bpf_error("port in range '%s' is tcp", name
);
6420 /* override PROTO_UNDEF */
6421 real_proto
= IPPROTO_SCTP
;
6424 bpf_error("illegal port number %d < 0", port1
);
6426 bpf_error("illegal port number %d > 65535", port1
);
6428 bpf_error("illegal port number %d < 0", port2
);
6430 bpf_error("illegal port number %d > 65535", port2
);
6433 return gen_portrange(port1
, port2
, real_proto
, dir
);
6435 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6436 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6442 eaddr
= pcap_ether_hostton(name
);
6444 bpf_error("unknown ether host: %s", name
);
6446 alist
= pcap_nametoaddr(name
);
6447 if (alist
== NULL
|| *alist
== NULL
)
6448 bpf_error("unknown host '%s'", name
);
6449 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6453 bpf_error("'gateway' not supported in this configuration");
6457 real_proto
= lookup_proto(name
, proto
);
6458 if (real_proto
>= 0)
6459 return gen_proto(real_proto
, proto
, dir
);
6461 bpf_error("unknown protocol: %s", name
);
6464 real_proto
= lookup_proto(name
, proto
);
6465 if (real_proto
>= 0)
6466 return gen_protochain(real_proto
, proto
, dir
);
6468 bpf_error("unknown protocol: %s", name
);
6479 gen_mcode(s1
, s2
, masklen
, q
)
6480 register const char *s1
, *s2
;
6481 register int masklen
;
6484 register int nlen
, mlen
;
6487 nlen
= __pcap_atoin(s1
, &n
);
6488 /* Promote short ipaddr */
6492 mlen
= __pcap_atoin(s2
, &m
);
6493 /* Promote short ipaddr */
6496 bpf_error("non-network bits set in \"%s mask %s\"",
6499 /* Convert mask len to mask */
6501 bpf_error("mask length must be <= 32");
6504 * X << 32 is not guaranteed by C to be 0; it's
6509 m
= 0xffffffff << (32 - masklen
);
6511 bpf_error("non-network bits set in \"%s/%d\"",
6518 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6521 bpf_error("Mask syntax for networks only");
6530 register const char *s
;
6535 int proto
= q
.proto
;
6541 else if (q
.proto
== Q_DECNET
)
6542 vlen
= __pcap_atodn(s
, &v
);
6544 vlen
= __pcap_atoin(s
, &v
);
6551 if (proto
== Q_DECNET
)
6552 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6553 else if (proto
== Q_LINK
) {
6554 bpf_error("illegal link layer address");
6557 if (s
== NULL
&& q
.addr
== Q_NET
) {
6558 /* Promote short net number */
6559 while (v
&& (v
& 0xff000000) == 0) {
6564 /* Promote short ipaddr */
6568 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6573 proto
= IPPROTO_UDP
;
6574 else if (proto
== Q_TCP
)
6575 proto
= IPPROTO_TCP
;
6576 else if (proto
== Q_SCTP
)
6577 proto
= IPPROTO_SCTP
;
6578 else if (proto
== Q_DEFAULT
)
6579 proto
= PROTO_UNDEF
;
6581 bpf_error("illegal qualifier of 'port'");
6584 bpf_error("illegal port number %u > 65535", v
);
6587 return gen_port((int)v
, proto
, dir
);
6591 b
= gen_port((int)v
, proto
, dir
);
6592 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6599 proto
= IPPROTO_UDP
;
6600 else if (proto
== Q_TCP
)
6601 proto
= IPPROTO_TCP
;
6602 else if (proto
== Q_SCTP
)
6603 proto
= IPPROTO_SCTP
;
6604 else if (proto
== Q_DEFAULT
)
6605 proto
= PROTO_UNDEF
;
6607 bpf_error("illegal qualifier of 'portrange'");
6610 bpf_error("illegal port number %u > 65535", v
);
6613 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6617 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6618 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6624 bpf_error("'gateway' requires a name");
6628 return gen_proto((int)v
, proto
, dir
);
6631 return gen_protochain((int)v
, proto
, dir
);
6646 gen_mcode6(s1
, s2
, masklen
, q
)
6647 register const char *s1
, *s2
;
6648 register int masklen
;
6651 struct addrinfo
*res
;
6652 struct in6_addr
*addr
;
6653 struct in6_addr mask
;
6658 bpf_error("no mask %s supported", s2
);
6660 res
= pcap_nametoaddrinfo(s1
);
6662 bpf_error("invalid ip6 address %s", s1
);
6665 bpf_error("%s resolved to multiple address", s1
);
6666 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6668 if (sizeof(mask
) * 8 < masklen
)
6669 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6670 memset(&mask
, 0, sizeof(mask
));
6671 memset(&mask
, 0xff, masklen
/ 8);
6673 mask
.s6_addr
[masklen
/ 8] =
6674 (0xff << (8 - masklen
% 8)) & 0xff;
6677 a
= (u_int32_t
*)addr
;
6678 m
= (u_int32_t
*)&mask
;
6679 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6680 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6681 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6689 bpf_error("Mask syntax for networks only");
6693 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6699 bpf_error("invalid qualifier against IPv6 address");
6708 register const u_char
*eaddr
;
6711 struct block
*b
, *tmp
;
6713 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6716 return gen_ehostop(eaddr
, (int)q
.dir
);
6718 return gen_fhostop(eaddr
, (int)q
.dir
);
6720 return gen_thostop(eaddr
, (int)q
.dir
);
6721 case DLT_IEEE802_11
:
6722 case DLT_PRISM_HEADER
:
6723 case DLT_IEEE802_11_RADIO_AVS
:
6724 case DLT_IEEE802_11_RADIO
:
6726 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6730 * Check that the packet doesn't begin with an
6731 * LE Control marker. (We've already generated
6734 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6739 * Now check the MAC address.
6741 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6746 case DLT_IP_OVER_FC
:
6747 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6749 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6753 bpf_error("ethernet address used in non-ether expression");
6760 struct slist
*s0
, *s1
;
6763 * This is definitely not the best way to do this, but the
6764 * lists will rarely get long.
6771 static struct slist
*
6777 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6782 static struct slist
*
6788 s
= new_stmt(BPF_LD
|BPF_MEM
);
6794 * Modify "index" to use the value stored into its register as an
6795 * offset relative to the beginning of the header for the protocol
6796 * "proto", and allocate a register and put an item "size" bytes long
6797 * (1, 2, or 4) at that offset into that register, making it the register
6801 gen_load(proto
, inst
, size
)
6806 struct slist
*s
, *tmp
;
6808 int regno
= alloc_reg();
6810 free_reg(inst
->regno
);
6814 bpf_error("data size must be 1, 2, or 4");
6830 bpf_error("unsupported index operation");
6834 * The offset is relative to the beginning of the packet
6835 * data, if we have a radio header. (If we don't, this
6838 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6839 linktype
!= DLT_IEEE802_11_RADIO
&&
6840 linktype
!= DLT_PRISM_HEADER
)
6841 bpf_error("radio information not present in capture");
6844 * Load into the X register the offset computed into the
6845 * register specified by "index".
6847 s
= xfer_to_x(inst
);
6850 * Load the item at that offset.
6852 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6854 sappend(inst
->s
, s
);
6859 * The offset is relative to the beginning of
6860 * the link-layer header.
6862 * XXX - what about ATM LANE? Should the index be
6863 * relative to the beginning of the AAL5 frame, so
6864 * that 0 refers to the beginning of the LE Control
6865 * field, or relative to the beginning of the LAN
6866 * frame, so that 0 refers, for Ethernet LANE, to
6867 * the beginning of the destination address?
6869 s
= gen_llprefixlen();
6872 * If "s" is non-null, it has code to arrange that the
6873 * X register contains the length of the prefix preceding
6874 * the link-layer header. Add to it the offset computed
6875 * into the register specified by "index", and move that
6876 * into the X register. Otherwise, just load into the X
6877 * register the offset computed into the register specified
6881 sappend(s
, xfer_to_a(inst
));
6882 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6883 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6885 s
= xfer_to_x(inst
);
6888 * Load the item at the sum of the offset we've put in the
6889 * X register and the offset of the start of the link
6890 * layer header (which is 0 if the radio header is
6891 * variable-length; that header length is what we put
6892 * into the X register and then added to the index).
6894 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6897 sappend(inst
->s
, s
);
6913 * The offset is relative to the beginning of
6914 * the network-layer header.
6915 * XXX - are there any cases where we want
6918 s
= gen_off_macpl();
6921 * If "s" is non-null, it has code to arrange that the
6922 * X register contains the offset of the MAC-layer
6923 * payload. Add to it the offset computed into the
6924 * register specified by "index", and move that into
6925 * the X register. Otherwise, just load into the X
6926 * register the offset computed into the register specified
6930 sappend(s
, xfer_to_a(inst
));
6931 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6932 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6934 s
= xfer_to_x(inst
);
6937 * Load the item at the sum of the offset we've put in the
6938 * X register, the offset of the start of the network
6939 * layer header from the beginning of the MAC-layer
6940 * payload, and the purported offset of the start of the
6941 * MAC-layer payload (which might be 0 if there's a
6942 * variable-length prefix before the link-layer header
6943 * or the link-layer header itself is variable-length;
6944 * the variable-length offset of the start of the
6945 * MAC-layer payload is what we put into the X register
6946 * and then added to the index).
6948 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6949 tmp
->s
.k
= off_macpl
+ off_nl
;
6951 sappend(inst
->s
, s
);
6954 * Do the computation only if the packet contains
6955 * the protocol in question.
6957 b
= gen_proto_abbrev(proto
);
6959 gen_and(inst
->b
, b
);
6972 * The offset is relative to the beginning of
6973 * the transport-layer header.
6975 * Load the X register with the length of the IPv4 header
6976 * (plus the offset of the link-layer header, if it's
6977 * a variable-length header), in bytes.
6979 * XXX - are there any cases where we want
6981 * XXX - we should, if we're built with
6982 * IPv6 support, generate code to load either
6983 * IPv4, IPv6, or both, as appropriate.
6985 s
= gen_loadx_iphdrlen();
6988 * The X register now contains the sum of the length
6989 * of any variable-length header preceding the link-layer
6990 * header, any variable-length link-layer header, and the
6991 * length of the network-layer header.
6993 * Load into the A register the offset relative to
6994 * the beginning of the transport layer header,
6995 * add the X register to that, move that to the
6996 * X register, and load with an offset from the
6997 * X register equal to the offset of the network
6998 * layer header relative to the beginning of
6999 * the MAC-layer payload plus the fixed-length
7000 * portion of the offset of the MAC-layer payload
7001 * from the beginning of the raw packet data.
7003 sappend(s
, xfer_to_a(inst
));
7004 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7005 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7006 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7007 tmp
->s
.k
= off_macpl
+ off_nl
;
7008 sappend(inst
->s
, s
);
7011 * Do the computation only if the packet contains
7012 * the protocol in question - which is true only
7013 * if this is an IP datagram and is the first or
7014 * only fragment of that datagram.
7016 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7018 gen_and(inst
->b
, b
);
7020 gen_and(gen_proto_abbrev(Q_IP
), b
);
7026 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7030 inst
->regno
= regno
;
7031 s
= new_stmt(BPF_ST
);
7033 sappend(inst
->s
, s
);
7039 gen_relation(code
, a0
, a1
, reversed
)
7041 struct arth
*a0
, *a1
;
7044 struct slist
*s0
, *s1
, *s2
;
7045 struct block
*b
, *tmp
;
7049 if (code
== BPF_JEQ
) {
7050 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7051 b
= new_block(JMP(code
));
7055 b
= new_block(BPF_JMP
|code
|BPF_X
);
7061 sappend(a0
->s
, a1
->s
);
7065 free_reg(a0
->regno
);
7066 free_reg(a1
->regno
);
7068 /* 'and' together protocol checks */
7071 gen_and(a0
->b
, tmp
= a1
->b
);
7087 int regno
= alloc_reg();
7088 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7091 s
= new_stmt(BPF_LD
|BPF_LEN
);
7092 s
->next
= new_stmt(BPF_ST
);
7093 s
->next
->s
.k
= regno
;
7108 a
= (struct arth
*)newchunk(sizeof(*a
));
7112 s
= new_stmt(BPF_LD
|BPF_IMM
);
7114 s
->next
= new_stmt(BPF_ST
);
7130 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7133 s
= new_stmt(BPF_ST
);
7141 gen_arth(code
, a0
, a1
)
7143 struct arth
*a0
, *a1
;
7145 struct slist
*s0
, *s1
, *s2
;
7149 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7154 sappend(a0
->s
, a1
->s
);
7156 free_reg(a0
->regno
);
7157 free_reg(a1
->regno
);
7159 s0
= new_stmt(BPF_ST
);
7160 a0
->regno
= s0
->s
.k
= alloc_reg();
7167 * Here we handle simple allocation of the scratch registers.
7168 * If too many registers are alloc'd, the allocator punts.
7170 static int regused
[BPF_MEMWORDS
];
7174 * Initialize the table of used registers and the current register.
7180 memset(regused
, 0, sizeof regused
);
7184 * Return the next free register.
7189 int n
= BPF_MEMWORDS
;
7192 if (regused
[curreg
])
7193 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7195 regused
[curreg
] = 1;
7199 bpf_error("too many registers needed to evaluate expression");
7205 * Return a register to the table so it can
7215 static struct block
*
7222 s
= new_stmt(BPF_LD
|BPF_LEN
);
7223 b
= new_block(JMP(jmp
));
7234 return gen_len(BPF_JGE
, n
);
7238 * Actually, this is less than or equal.
7246 b
= gen_len(BPF_JGT
, n
);
7253 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7254 * the beginning of the link-layer header.
7255 * XXX - that means you can't test values in the radiotap header, but
7256 * as that header is difficult if not impossible to parse generally
7257 * without a loop, that might not be a severe problem. A new keyword
7258 * "radio" could be added for that, although what you'd really want
7259 * would be a way of testing particular radio header values, which
7260 * would generate code appropriate to the radio header in question.
7263 gen_byteop(op
, idx
, val
)
7274 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7277 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7281 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7285 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7289 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7293 b
= new_block(JMP(BPF_JEQ
));
7300 static u_char abroadcast
[] = { 0x0 };
7303 gen_broadcast(proto
)
7306 bpf_u_int32 hostmask
;
7307 struct block
*b0
, *b1
, *b2
;
7308 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7316 case DLT_ARCNET_LINUX
:
7317 return gen_ahostop(abroadcast
, Q_DST
);
7319 return gen_ehostop(ebroadcast
, Q_DST
);
7321 return gen_fhostop(ebroadcast
, Q_DST
);
7323 return gen_thostop(ebroadcast
, Q_DST
);
7324 case DLT_IEEE802_11
:
7325 case DLT_PRISM_HEADER
:
7326 case DLT_IEEE802_11_RADIO_AVS
:
7327 case DLT_IEEE802_11_RADIO
:
7329 return gen_wlanhostop(ebroadcast
, Q_DST
);
7330 case DLT_IP_OVER_FC
:
7331 return gen_ipfchostop(ebroadcast
, Q_DST
);
7335 * Check that the packet doesn't begin with an
7336 * LE Control marker. (We've already generated
7339 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7344 * Now check the MAC address.
7346 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7352 bpf_error("not a broadcast link");
7358 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7359 * as an indication that we don't know the netmask, and fail
7362 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7363 bpf_error("netmask not known, so 'ip broadcast' not supported");
7364 b0
= gen_linktype(ETHERTYPE_IP
);
7365 hostmask
= ~netmask
;
7366 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7367 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7368 (bpf_int32
)(~0 & hostmask
), hostmask
);
7373 bpf_error("only link-layer/IP broadcast filters supported");
7379 * Generate code to test the low-order bit of a MAC address (that's
7380 * the bottom bit of the *first* byte).
7382 static struct block
*
7383 gen_mac_multicast(offset
)
7386 register struct block
*b0
;
7387 register struct slist
*s
;
7389 /* link[offset] & 1 != 0 */
7390 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7391 b0
= new_block(JMP(BPF_JSET
));
7398 gen_multicast(proto
)
7401 register struct block
*b0
, *b1
, *b2
;
7402 register struct slist
*s
;
7410 case DLT_ARCNET_LINUX
:
7411 /* all ARCnet multicasts use the same address */
7412 return gen_ahostop(abroadcast
, Q_DST
);
7414 /* ether[0] & 1 != 0 */
7415 return gen_mac_multicast(0);
7418 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7420 * XXX - was that referring to bit-order issues?
7422 /* fddi[1] & 1 != 0 */
7423 return gen_mac_multicast(1);
7425 /* tr[2] & 1 != 0 */
7426 return gen_mac_multicast(2);
7427 case DLT_IEEE802_11
:
7428 case DLT_PRISM_HEADER
:
7429 case DLT_IEEE802_11_RADIO_AVS
:
7430 case DLT_IEEE802_11_RADIO
:
7435 * For control frames, there is no DA.
7437 * For management frames, DA is at an
7438 * offset of 4 from the beginning of
7441 * For data frames, DA is at an offset
7442 * of 4 from the beginning of the packet
7443 * if To DS is clear and at an offset of
7444 * 16 from the beginning of the packet
7449 * Generate the tests to be done for data frames.
7451 * First, check for To DS set, i.e. "link[1] & 0x01".
7453 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7454 b1
= new_block(JMP(BPF_JSET
));
7455 b1
->s
.k
= 0x01; /* To DS */
7459 * If To DS is set, the DA is at 16.
7461 b0
= gen_mac_multicast(16);
7465 * Now, check for To DS not set, i.e. check
7466 * "!(link[1] & 0x01)".
7468 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7469 b2
= new_block(JMP(BPF_JSET
));
7470 b2
->s
.k
= 0x01; /* To DS */
7475 * If To DS is not set, the DA is at 4.
7477 b1
= gen_mac_multicast(4);
7481 * Now OR together the last two checks. That gives
7482 * the complete set of checks for data frames.
7487 * Now check for a data frame.
7488 * I.e, check "link[0] & 0x08".
7490 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7491 b1
= new_block(JMP(BPF_JSET
));
7496 * AND that with the checks done for data frames.
7501 * If the high-order bit of the type value is 0, this
7502 * is a management frame.
7503 * I.e, check "!(link[0] & 0x08)".
7505 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7506 b2
= new_block(JMP(BPF_JSET
));
7512 * For management frames, the DA is at 4.
7514 b1
= gen_mac_multicast(4);
7518 * OR that with the checks done for data frames.
7519 * That gives the checks done for management and
7525 * If the low-order bit of the type value is 1,
7526 * this is either a control frame or a frame
7527 * with a reserved type, and thus not a
7530 * I.e., check "!(link[0] & 0x04)".
7532 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7533 b1
= new_block(JMP(BPF_JSET
));
7539 * AND that with the checks for data and management
7544 case DLT_IP_OVER_FC
:
7545 b0
= gen_mac_multicast(2);
7550 * Check that the packet doesn't begin with an
7551 * LE Control marker. (We've already generated
7554 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7558 /* ether[off_mac] & 1 != 0 */
7559 b0
= gen_mac_multicast(off_mac
);
7567 /* Link not known to support multicasts */
7571 b0
= gen_linktype(ETHERTYPE_IP
);
7572 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7578 b0
= gen_linktype(ETHERTYPE_IPV6
);
7579 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7584 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7590 * generate command for inbound/outbound. It's here so we can
7591 * make it link-type specific. 'dir' = 0 implies "inbound",
7592 * = 1 implies "outbound".
7598 register struct block
*b0
;
7601 * Only some data link types support inbound/outbound qualifiers.
7605 b0
= gen_relation(BPF_JEQ
,
7606 gen_load(Q_LINK
, gen_loadi(0), 1),
7613 /* match outgoing packets */
7614 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7616 /* match incoming packets */
7617 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7624 * Match packets sent by this machine.
7626 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7629 * Match packets sent to this machine.
7630 * (No broadcast or multicast packets, or
7631 * packets sent to some other machine and
7632 * received promiscuously.)
7634 * XXX - packets sent to other machines probably
7635 * shouldn't be matched, but what about broadcast
7636 * or multicast packets we received?
7638 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7642 #ifdef HAVE_NET_PFVAR_H
7644 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7645 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7651 /* match outgoing packets */
7652 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7654 /* match incoming packets */
7655 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7659 case DLT_JUNIPER_MFR
:
7660 case DLT_JUNIPER_MLFR
:
7661 case DLT_JUNIPER_MLPPP
:
7662 case DLT_JUNIPER_ATM1
:
7663 case DLT_JUNIPER_ATM2
:
7664 case DLT_JUNIPER_PPPOE
:
7665 case DLT_JUNIPER_PPPOE_ATM
:
7666 case DLT_JUNIPER_GGSN
:
7667 case DLT_JUNIPER_ES
:
7668 case DLT_JUNIPER_MONITOR
:
7669 case DLT_JUNIPER_SERVICES
:
7670 case DLT_JUNIPER_ETHER
:
7671 case DLT_JUNIPER_PPP
:
7672 case DLT_JUNIPER_FRELAY
:
7673 case DLT_JUNIPER_CHDLC
:
7674 case DLT_JUNIPER_VP
:
7675 case DLT_JUNIPER_ST
:
7676 case DLT_JUNIPER_ISM
:
7677 /* juniper flags (including direction) are stored
7678 * the byte after the 3-byte magic number */
7680 /* match outgoing packets */
7681 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7683 /* match incoming packets */
7684 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7689 bpf_error("inbound/outbound not supported on linktype %d",
7697 #ifdef HAVE_NET_PFVAR_H
7698 /* PF firewall log matched interface */
7700 gen_pf_ifname(const char *ifname
)
7705 if (linktype
!= DLT_PFLOG
) {
7706 bpf_error("ifname supported only on PF linktype");
7709 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7710 off
= offsetof(struct pfloghdr
, ifname
);
7711 if (strlen(ifname
) >= len
) {
7712 bpf_error("ifname interface names can only be %d characters",
7716 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7720 /* PF firewall log ruleset name */
7722 gen_pf_ruleset(char *ruleset
)
7726 if (linktype
!= DLT_PFLOG
) {
7727 bpf_error("ruleset supported only on PF linktype");
7731 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7732 bpf_error("ruleset names can only be %ld characters",
7733 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7737 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7738 strlen(ruleset
), (const u_char
*)ruleset
);
7742 /* PF firewall log rule number */
7748 if (linktype
!= DLT_PFLOG
) {
7749 bpf_error("rnr supported only on PF linktype");
7753 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7758 /* PF firewall log sub-rule number */
7760 gen_pf_srnr(int srnr
)
7764 if (linktype
!= DLT_PFLOG
) {
7765 bpf_error("srnr supported only on PF linktype");
7769 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7774 /* PF firewall log reason code */
7776 gen_pf_reason(int reason
)
7780 if (linktype
!= DLT_PFLOG
) {
7781 bpf_error("reason supported only on PF linktype");
7785 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7790 /* PF firewall log action */
7792 gen_pf_action(int action
)
7796 if (linktype
!= DLT_PFLOG
) {
7797 bpf_error("action supported only on PF linktype");
7801 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7805 #else /* !HAVE_NET_PFVAR_H */
7807 gen_pf_ifname(const char *ifname
)
7809 bpf_error("libpcap was compiled without pf support");
7815 gen_pf_ruleset(char *ruleset
)
7817 bpf_error("libpcap was compiled on a machine without pf support");
7825 bpf_error("libpcap was compiled on a machine without pf support");
7831 gen_pf_srnr(int srnr
)
7833 bpf_error("libpcap was compiled on a machine without pf support");
7839 gen_pf_reason(int reason
)
7841 bpf_error("libpcap was compiled on a machine without pf support");
7847 gen_pf_action(int action
)
7849 bpf_error("libpcap was compiled on a machine without pf support");
7853 #endif /* HAVE_NET_PFVAR_H */
7855 /* IEEE 802.11 wireless header */
7857 gen_p80211_type(int type
, int mask
)
7863 case DLT_IEEE802_11
:
7864 case DLT_PRISM_HEADER
:
7865 case DLT_IEEE802_11_RADIO_AVS
:
7866 case DLT_IEEE802_11_RADIO
:
7867 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7872 bpf_error("802.11 link-layer types supported only on 802.11");
7880 gen_p80211_fcdir(int fcdir
)
7886 case DLT_IEEE802_11
:
7887 case DLT_PRISM_HEADER
:
7888 case DLT_IEEE802_11_RADIO_AVS
:
7889 case DLT_IEEE802_11_RADIO
:
7893 bpf_error("frame direction supported only with 802.11 headers");
7897 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7898 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7905 register const u_char
*eaddr
;
7911 case DLT_ARCNET_LINUX
:
7912 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7914 return (gen_ahostop(eaddr
, (int)q
.dir
));
7916 bpf_error("ARCnet address used in non-arc expression");
7922 bpf_error("aid supported only on ARCnet");
7925 bpf_error("ARCnet address used in non-arc expression");
7930 static struct block
*
7931 gen_ahostop(eaddr
, dir
)
7932 register const u_char
*eaddr
;
7935 register struct block
*b0
, *b1
;
7938 /* src comes first, different from Ethernet */
7940 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7943 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7946 b0
= gen_ahostop(eaddr
, Q_SRC
);
7947 b1
= gen_ahostop(eaddr
, Q_DST
);
7953 b0
= gen_ahostop(eaddr
, Q_SRC
);
7954 b1
= gen_ahostop(eaddr
, Q_DST
);
7959 bpf_error("'addr1' is only supported on 802.11");
7963 bpf_error("'addr2' is only supported on 802.11");
7967 bpf_error("'addr3' is only supported on 802.11");
7971 bpf_error("'addr4' is only supported on 802.11");
7975 bpf_error("'ra' is only supported on 802.11");
7979 bpf_error("'ta' is only supported on 802.11");
7987 * support IEEE 802.1Q VLAN trunk over ethernet
7993 struct block
*b0
, *b1
;
7995 /* can't check for VLAN-encapsulated packets inside MPLS */
7996 if (label_stack_depth
> 0)
7997 bpf_error("no VLAN match after MPLS");
8000 * Check for a VLAN packet, and then change the offsets to point
8001 * to the type and data fields within the VLAN packet. Just
8002 * increment the offsets, so that we can support a hierarchy, e.g.
8003 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8006 * XXX - this is a bit of a kludge. If we were to split the
8007 * compiler into a parser that parses an expression and
8008 * generates an expression tree, and a code generator that
8009 * takes an expression tree (which could come from our
8010 * parser or from some other parser) and generates BPF code,
8011 * we could perhaps make the offsets parameters of routines
8012 * and, in the handler for an "AND" node, pass to subnodes
8013 * other than the VLAN node the adjusted offsets.
8015 * This would mean that "vlan" would, instead of changing the
8016 * behavior of *all* tests after it, change only the behavior
8017 * of tests ANDed with it. That would change the documented
8018 * semantics of "vlan", which might break some expressions.
8019 * However, it would mean that "(vlan and ip) or ip" would check
8020 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8021 * checking only for VLAN-encapsulated IP, so that could still
8022 * be considered worth doing; it wouldn't break expressions
8023 * that are of the form "vlan and ..." or "vlan N and ...",
8024 * which I suspect are the most common expressions involving
8025 * "vlan". "vlan or ..." doesn't necessarily do what the user
8026 * would really want, now, as all the "or ..." tests would
8027 * be done assuming a VLAN, even though the "or" could be viewed
8028 * as meaning "or, if this isn't a VLAN packet...".
8035 /* check for VLAN */
8036 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8037 (bpf_int32
)ETHERTYPE_8021Q
);
8039 /* If a specific VLAN is requested, check VLAN id */
8040 if (vlan_num
>= 0) {
8041 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
8042 (bpf_int32
)vlan_num
, 0x0fff);
8056 bpf_error("no VLAN support for data link type %d",
8071 struct block
*b0
,*b1
;
8074 * Change the offsets to point to the type and data fields within
8075 * the MPLS packet. Just increment the offsets, so that we
8076 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8077 * capture packets with an outer label of 100000 and an inner
8080 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8084 if (label_stack_depth
> 0) {
8085 /* just match the bottom-of-stack bit clear */
8086 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
8089 * Indicate that we're checking MPLS-encapsulated headers,
8090 * to make sure higher level code generators don't try to
8091 * match against IP-related protocols such as Q_ARP, Q_RARP
8096 case DLT_C_HDLC
: /* fall through */
8098 b0
= gen_linktype(ETHERTYPE_MPLS
);
8102 b0
= gen_linktype(PPP_MPLS_UCAST
);
8105 /* FIXME add other DLT_s ...
8106 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8107 * leave it for now */
8110 bpf_error("no MPLS support for data link type %d",
8118 /* If a specific MPLS label is requested, check it */
8119 if (label_num
>= 0) {
8120 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8121 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8122 0xfffff000); /* only compare the first 20 bits */
8129 label_stack_depth
++;
8134 * Support PPPOE discovery and session.
8139 /* check for PPPoE discovery */
8140 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8149 * Test against the PPPoE session link-layer type.
8151 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8154 * Change the offsets to point to the type and data fields within
8155 * the PPP packet, and note that this is PPPoE rather than
8158 * XXX - this is a bit of a kludge. If we were to split the
8159 * compiler into a parser that parses an expression and
8160 * generates an expression tree, and a code generator that
8161 * takes an expression tree (which could come from our
8162 * parser or from some other parser) and generates BPF code,
8163 * we could perhaps make the offsets parameters of routines
8164 * and, in the handler for an "AND" node, pass to subnodes
8165 * other than the PPPoE node the adjusted offsets.
8167 * This would mean that "pppoes" would, instead of changing the
8168 * behavior of *all* tests after it, change only the behavior
8169 * of tests ANDed with it. That would change the documented
8170 * semantics of "pppoes", which might break some expressions.
8171 * However, it would mean that "(pppoes and ip) or ip" would check
8172 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8173 * checking only for VLAN-encapsulated IP, so that could still
8174 * be considered worth doing; it wouldn't break expressions
8175 * that are of the form "pppoes and ..." which I suspect are the
8176 * most common expressions involving "pppoes". "pppoes or ..."
8177 * doesn't necessarily do what the user would really want, now,
8178 * as all the "or ..." tests would be done assuming PPPoE, even
8179 * though the "or" could be viewed as meaning "or, if this isn't
8180 * a PPPoE packet...".
8182 orig_linktype
= off_linktype
; /* save original values */
8187 * The "network-layer" protocol is PPPoE, which has a 6-byte
8188 * PPPoE header, followed by a PPP packet.
8190 * There is no HDLC encapsulation for the PPP packet (it's
8191 * encapsulated in PPPoES instead), so the link-layer type
8192 * starts at the first byte of the PPP packet. For PPPoE,
8193 * that offset is relative to the beginning of the total
8194 * link-layer payload, including any 802.2 LLC header, so
8195 * it's 6 bytes past off_nl.
8197 off_linktype
= off_nl
+ 6;
8200 * The network-layer offsets are relative to the beginning
8201 * of the MAC-layer payload; that's past the 6-byte
8202 * PPPoE header and the 2-byte PPP header.
8205 off_nl_nosnap
= 6+2;
8211 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8223 bpf_error("'vpi' supported only on raw ATM");
8224 if (off_vpi
== (u_int
)-1)
8226 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8232 bpf_error("'vci' supported only on raw ATM");
8233 if (off_vci
== (u_int
)-1)
8235 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8240 if (off_proto
== (u_int
)-1)
8241 abort(); /* XXX - this isn't on FreeBSD */
8242 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8247 if (off_payload
== (u_int
)-1)
8249 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8250 0xffffffff, jtype
, reverse
, jvalue
);
8255 bpf_error("'callref' supported only on raw ATM");
8256 if (off_proto
== (u_int
)-1)
8258 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8259 jtype
, reverse
, jvalue
);
8269 gen_atmtype_abbrev(type
)
8272 struct block
*b0
, *b1
;
8277 /* Get all packets in Meta signalling Circuit */
8279 bpf_error("'metac' supported only on raw ATM");
8280 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8281 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8286 /* Get all packets in Broadcast Circuit*/
8288 bpf_error("'bcc' supported only on raw ATM");
8289 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8290 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8295 /* Get all cells in Segment OAM F4 circuit*/
8297 bpf_error("'oam4sc' supported only on raw ATM");
8298 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8299 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8304 /* Get all cells in End-to-End OAM F4 Circuit*/
8306 bpf_error("'oam4ec' supported only on raw ATM");
8307 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8308 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8313 /* Get all packets in connection Signalling Circuit */
8315 bpf_error("'sc' supported only on raw ATM");
8316 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8317 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8322 /* Get all packets in ILMI Circuit */
8324 bpf_error("'ilmic' supported only on raw ATM");
8325 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8326 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8331 /* Get all LANE packets */
8333 bpf_error("'lane' supported only on raw ATM");
8334 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8337 * Arrange that all subsequent tests assume LANE
8338 * rather than LLC-encapsulated packets, and set
8339 * the offsets appropriately for LANE-encapsulated
8342 * "off_mac" is the offset of the Ethernet header,
8343 * which is 2 bytes past the ATM pseudo-header
8344 * (skipping the pseudo-header and 2-byte LE Client
8345 * field). The other offsets are Ethernet offsets
8346 * relative to "off_mac".
8349 off_mac
= off_payload
+ 2; /* MAC header */
8350 off_linktype
= off_mac
+ 12;
8351 off_macpl
= off_mac
+ 14; /* Ethernet */
8352 off_nl
= 0; /* Ethernet II */
8353 off_nl_nosnap
= 3; /* 802.3+802.2 */
8357 /* Get all LLC-encapsulated packets */
8359 bpf_error("'llc' supported only on raw ATM");
8360 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8371 * Filtering for MTP2 messages based on li value
8372 * FISU, length is null
8373 * LSSU, length is 1 or 2
8374 * MSU, length is 3 or more
8377 gen_mtp2type_abbrev(type
)
8380 struct block
*b0
, *b1
;
8385 if ( (linktype
!= DLT_MTP2
) &&
8386 (linktype
!= DLT_ERF
) &&
8387 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8388 bpf_error("'fisu' supported only on MTP2");
8389 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8390 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8394 if ( (linktype
!= DLT_MTP2
) &&
8395 (linktype
!= DLT_ERF
) &&
8396 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8397 bpf_error("'lssu' supported only on MTP2");
8398 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8399 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8404 if ( (linktype
!= DLT_MTP2
) &&
8405 (linktype
!= DLT_ERF
) &&
8406 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8407 bpf_error("'msu' supported only on MTP2");
8408 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8418 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8425 bpf_u_int32 val1
, val2
, val3
;
8427 switch (mtp3field
) {
8430 if (off_sio
== (u_int
)-1)
8431 bpf_error("'sio' supported only on SS7");
8432 /* sio coded on 1 byte so max value 255 */
8434 bpf_error("sio value %u too big; max value = 255",
8436 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8437 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8441 if (off_opc
== (u_int
)-1)
8442 bpf_error("'opc' supported only on SS7");
8443 /* opc coded on 14 bits so max value 16383 */
8445 bpf_error("opc value %u too big; max value = 16383",
8447 /* the following instructions are made to convert jvalue
8448 * to the form used to write opc in an ss7 message*/
8449 val1
= jvalue
& 0x00003c00;
8451 val2
= jvalue
& 0x000003fc;
8453 val3
= jvalue
& 0x00000003;
8455 jvalue
= val1
+ val2
+ val3
;
8456 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8457 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8461 if (off_dpc
== (u_int
)-1)
8462 bpf_error("'dpc' supported only on SS7");
8463 /* dpc coded on 14 bits so max value 16383 */
8465 bpf_error("dpc value %u too big; max value = 16383",
8467 /* the following instructions are made to convert jvalue
8468 * to the forme used to write dpc in an ss7 message*/
8469 val1
= jvalue
& 0x000000ff;
8471 val2
= jvalue
& 0x00003f00;
8473 jvalue
= val1
+ val2
;
8474 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8475 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8479 if (off_sls
== (u_int
)-1)
8480 bpf_error("'sls' supported only on SS7");
8481 /* sls coded on 4 bits so max value 15 */
8483 bpf_error("sls value %u too big; max value = 15",
8485 /* the following instruction is made to convert jvalue
8486 * to the forme used to write sls in an ss7 message*/
8487 jvalue
= jvalue
<< 4;
8488 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8489 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8498 static struct block
*
8499 gen_msg_abbrev(type
)
8505 * Q.2931 signalling protocol messages for handling virtual circuits
8506 * establishment and teardown
8511 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8515 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8519 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8523 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8527 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8530 case A_RELEASE_DONE
:
8531 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8541 gen_atmmulti_abbrev(type
)
8544 struct block
*b0
, *b1
;
8550 bpf_error("'oam' supported only on raw ATM");
8551 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8556 bpf_error("'oamf4' supported only on raw ATM");
8558 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8559 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8561 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8567 * Get Q.2931 signalling messages for switched
8568 * virtual connection
8571 bpf_error("'connectmsg' supported only on raw ATM");
8572 b0
= gen_msg_abbrev(A_SETUP
);
8573 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8575 b0
= gen_msg_abbrev(A_CONNECT
);
8577 b0
= gen_msg_abbrev(A_CONNECTACK
);
8579 b0
= gen_msg_abbrev(A_RELEASE
);
8581 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8583 b0
= gen_atmtype_abbrev(A_SC
);
8589 bpf_error("'metaconnect' supported only on raw ATM");
8590 b0
= gen_msg_abbrev(A_SETUP
);
8591 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8593 b0
= gen_msg_abbrev(A_CONNECT
);
8595 b0
= gen_msg_abbrev(A_RELEASE
);
8597 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8599 b0
= gen_atmtype_abbrev(A_METAC
);