1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 * XXX - why was this included even on UNIX?
56 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
76 #include "ethertype.h"
80 #include "ieee80211.h"
82 #include "sunatmpos.h"
85 #include "pcap/ipnet.h"
87 #ifdef HAVE_NET_PFVAR_H
88 #include <sys/socket.h>
90 #include <net/pfvar.h>
91 #include <net/if_pflog.h>
94 #define offsetof(s, e) ((size_t)&((s *)0)->e)
98 #include <netdb.h> /* for "struct addrinfo" */
101 #include <pcap/namedb.h>
103 #define ETHERMTU 1500
106 #define IPPROTO_SCTP 132
109 #ifdef HAVE_OS_PROTO_H
110 #include "os-proto.h"
113 #define JMP(c) ((c)|BPF_JMP|BPF_K)
116 static jmp_buf top_ctx
;
117 static pcap_t
*bpf_pcap
;
119 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
121 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
123 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
128 static int pcap_fddipad
;
133 bpf_error(const char *fmt
, ...)
138 if (bpf_pcap
!= NULL
)
139 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
146 static void init_linktype(pcap_t
*);
148 static void init_regs(void);
149 static int alloc_reg(void);
150 static void free_reg(int);
152 static struct block
*root
;
155 * Value passed to gen_load_a() to indicate what the offset argument
159 OR_PACKET
, /* relative to the beginning of the packet */
160 OR_LINK
, /* relative to the beginning of the link-layer header */
161 OR_MACPL
, /* relative to the end of the MAC-layer header */
162 OR_NET
, /* relative to the network-layer header */
163 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
164 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
165 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
170 * As errors are handled by a longjmp, anything allocated must be freed
171 * in the longjmp handler, so it must be reachable from that handler.
172 * One thing that's allocated is the result of pcap_nametoaddrinfo();
173 * it must be freed with freeaddrinfo(). This variable points to any
174 * addrinfo structure that would need to be freed.
176 static struct addrinfo
*ai
;
180 * We divy out chunks of memory rather than call malloc each time so
181 * we don't have to worry about leaking memory. It's probably
182 * not a big deal if all this memory was wasted but if this ever
183 * goes into a library that would probably not be a good idea.
185 * XXX - this *is* in a library....
188 #define CHUNK0SIZE 1024
194 static struct chunk chunks
[NCHUNKS
];
195 static int cur_chunk
;
197 static void *newchunk(u_int
);
198 static void freechunks(void);
199 static inline struct block
*new_block(int);
200 static inline struct slist
*new_stmt(int);
201 static struct block
*gen_retblk(int);
202 static inline void syntax(void);
204 static void backpatch(struct block
*, struct block
*);
205 static void merge(struct block
*, struct block
*);
206 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
207 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
208 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
209 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
210 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
211 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
213 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
214 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
215 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
216 static struct slist
*gen_load_llrel(u_int
, u_int
);
217 static struct slist
*gen_load_macplrel(u_int
, u_int
);
218 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
219 static struct slist
*gen_loadx_iphdrlen(void);
220 static struct block
*gen_uncond(int);
221 static inline struct block
*gen_true(void);
222 static inline struct block
*gen_false(void);
223 static struct block
*gen_ether_linktype(int);
224 static struct block
*gen_ipnet_linktype(int);
225 static struct block
*gen_linux_sll_linktype(int);
226 static struct slist
*gen_load_prism_llprefixlen(void);
227 static struct slist
*gen_load_avs_llprefixlen(void);
228 static struct slist
*gen_load_radiotap_llprefixlen(void);
229 static struct slist
*gen_load_ppi_llprefixlen(void);
230 static void insert_compute_vloffsets(struct block
*);
231 static struct slist
*gen_llprefixlen(void);
232 static struct slist
*gen_off_macpl(void);
233 static int ethertype_to_ppptype(int);
234 static struct block
*gen_linktype(int);
235 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
236 static struct block
*gen_llc_linktype(int);
237 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
239 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
241 static struct block
*gen_ahostop(const u_char
*, int);
242 static struct block
*gen_ehostop(const u_char
*, int);
243 static struct block
*gen_fhostop(const u_char
*, int);
244 static struct block
*gen_thostop(const u_char
*, int);
245 static struct block
*gen_wlanhostop(const u_char
*, int);
246 static struct block
*gen_ipfchostop(const u_char
*, int);
247 static struct block
*gen_dnhostop(bpf_u_int32
, int);
248 static struct block
*gen_mpls_linktype(int);
249 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
251 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
254 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
256 static struct block
*gen_ipfrag(void);
257 static struct block
*gen_portatom(int, bpf_int32
);
258 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
260 static struct block
*gen_portatom6(int, bpf_int32
);
261 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
263 struct block
*gen_portop(int, int, int);
264 static struct block
*gen_port(int, int, int);
265 struct block
*gen_portrangeop(int, int, int, int);
266 static struct block
*gen_portrange(int, int, int, int);
268 struct block
*gen_portop6(int, int, int);
269 static struct block
*gen_port6(int, int, int);
270 struct block
*gen_portrangeop6(int, int, int, int);
271 static struct block
*gen_portrange6(int, int, int, int);
273 static int lookup_proto(const char *, int);
274 static struct block
*gen_protochain(int, int, int);
275 static struct block
*gen_proto(int, int, int);
276 static struct slist
*xfer_to_x(struct arth
*);
277 static struct slist
*xfer_to_a(struct arth
*);
278 static struct block
*gen_mac_multicast(int);
279 static struct block
*gen_len(int, int);
280 static struct block
*gen_check_802_11_data_frame(void);
282 static struct block
*gen_ppi_dlt_check(void);
283 static struct block
*gen_msg_abbrev(int type
);
294 /* XXX Round up to nearest long. */
295 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
297 /* XXX Round up to structure boundary. */
301 cp
= &chunks
[cur_chunk
];
302 if (n
> cp
->n_left
) {
303 ++cp
, k
= ++cur_chunk
;
305 bpf_error("out of memory");
306 size
= CHUNK0SIZE
<< k
;
307 cp
->m
= (void *)malloc(size
);
309 bpf_error("out of memory");
310 memset((char *)cp
->m
, 0, size
);
313 bpf_error("out of memory");
316 return (void *)((char *)cp
->m
+ cp
->n_left
);
325 for (i
= 0; i
< NCHUNKS
; ++i
)
326 if (chunks
[i
].m
!= NULL
) {
333 * A strdup whose allocations are freed after code generation is over.
337 register const char *s
;
339 int n
= strlen(s
) + 1;
340 char *cp
= newchunk(n
);
346 static inline struct block
*
352 p
= (struct block
*)newchunk(sizeof(*p
));
359 static inline struct slist
*
365 p
= (struct slist
*)newchunk(sizeof(*p
));
371 static struct block
*
375 struct block
*b
= new_block(BPF_RET
|BPF_K
);
384 bpf_error("syntax error in filter expression");
387 static bpf_u_int32 netmask
;
392 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
393 const char *buf
, int optimize
, bpf_u_int32 mask
);
396 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
397 const char *buf
, int optimize
, bpf_u_int32 mask
)
401 EnterCriticalSection(&g_PcapCompileCriticalSection
);
403 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
405 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
411 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
412 const char *buf
, int optimize
, bpf_u_int32 mask
)
415 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
416 const char *buf
, int optimize
, bpf_u_int32 mask
)
420 const char * volatile xbuf
= buf
;
428 if (setjmp(top_ctx
)) {
442 snaplen
= pcap_snapshot(p
);
444 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
445 "snaplen of 0 rejects all packets");
449 lex_init(xbuf
? xbuf
: "");
457 root
= gen_retblk(snaplen
);
459 if (optimize
&& !no_optimize
) {
462 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
463 bpf_error("expression rejects all packets");
465 program
->bf_insns
= icode_to_fcode(root
, &len
);
466 program
->bf_len
= len
;
474 * entry point for using the compiler with no pcap open
475 * pass in all the stuff that is needed explicitly instead.
478 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
479 struct bpf_program
*program
,
480 const char *buf
, int optimize
, bpf_u_int32 mask
)
485 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
488 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
494 * Clean up a "struct bpf_program" by freeing all the memory allocated
498 pcap_freecode(struct bpf_program
*program
)
501 if (program
->bf_insns
!= NULL
) {
502 free((char *)program
->bf_insns
);
503 program
->bf_insns
= NULL
;
508 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
509 * which of the jt and jf fields has been resolved and which is a pointer
510 * back to another unresolved block (or nil). At least one of the fields
511 * in each block is already resolved.
514 backpatch(list
, target
)
515 struct block
*list
, *target
;
532 * Merge the lists in b0 and b1, using the 'sense' field to indicate
533 * which of jt and jf is the link.
537 struct block
*b0
, *b1
;
539 register struct block
**p
= &b0
;
541 /* Find end of list. */
543 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
545 /* Concatenate the lists. */
553 struct block
*ppi_dlt_check
;
556 * Insert before the statements of the first (root) block any
557 * statements needed to load the lengths of any variable-length
558 * headers into registers.
560 * XXX - a fancier strategy would be to insert those before the
561 * statements of all blocks that use those lengths and that
562 * have no predecessors that use them, so that we only compute
563 * the lengths if we need them. There might be even better
564 * approaches than that.
566 * However, those strategies would be more complicated, and
567 * as we don't generate code to compute a length if the
568 * program has no tests that use the length, and as most
569 * tests will probably use those lengths, we would just
570 * postpone computing the lengths so that it's not done
571 * for tests that fail early, and it's not clear that's
574 insert_compute_vloffsets(p
->head
);
577 * For DLT_PPI captures, generate a check of the per-packet
578 * DLT value to make sure it's DLT_IEEE802_11.
580 ppi_dlt_check
= gen_ppi_dlt_check();
581 if (ppi_dlt_check
!= NULL
)
582 gen_and(ppi_dlt_check
, p
);
584 backpatch(p
, gen_retblk(snaplen
));
585 p
->sense
= !p
->sense
;
586 backpatch(p
, gen_retblk(0));
592 struct block
*b0
, *b1
;
594 backpatch(b0
, b1
->head
);
595 b0
->sense
= !b0
->sense
;
596 b1
->sense
= !b1
->sense
;
598 b1
->sense
= !b1
->sense
;
604 struct block
*b0
, *b1
;
606 b0
->sense
= !b0
->sense
;
607 backpatch(b0
, b1
->head
);
608 b0
->sense
= !b0
->sense
;
617 b
->sense
= !b
->sense
;
620 static struct block
*
621 gen_cmp(offrel
, offset
, size
, v
)
622 enum e_offrel offrel
;
626 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
629 static struct block
*
630 gen_cmp_gt(offrel
, offset
, size
, v
)
631 enum e_offrel offrel
;
635 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
638 static struct block
*
639 gen_cmp_ge(offrel
, offset
, size
, v
)
640 enum e_offrel offrel
;
644 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
647 static struct block
*
648 gen_cmp_lt(offrel
, offset
, size
, v
)
649 enum e_offrel offrel
;
653 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
656 static struct block
*
657 gen_cmp_le(offrel
, offset
, size
, v
)
658 enum e_offrel offrel
;
662 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
665 static struct block
*
666 gen_mcmp(offrel
, offset
, size
, v
, mask
)
667 enum e_offrel offrel
;
672 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
675 static struct block
*
676 gen_bcmp(offrel
, offset
, size
, v
)
677 enum e_offrel offrel
;
678 register u_int offset
, size
;
679 register const u_char
*v
;
681 register struct block
*b
, *tmp
;
685 register const u_char
*p
= &v
[size
- 4];
686 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
687 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
689 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
696 register const u_char
*p
= &v
[size
- 2];
697 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
699 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
706 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
715 * AND the field of size "size" at offset "offset" relative to the header
716 * specified by "offrel" with "mask", and compare it with the value "v"
717 * with the test specified by "jtype"; if "reverse" is true, the test
718 * should test the opposite of "jtype".
720 static struct block
*
721 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
722 enum e_offrel offrel
;
724 bpf_u_int32 offset
, size
, mask
, jtype
;
727 struct slist
*s
, *s2
;
730 s
= gen_load_a(offrel
, offset
, size
);
732 if (mask
!= 0xffffffff) {
733 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
738 b
= new_block(JMP(jtype
));
741 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
747 * Various code constructs need to know the layout of the data link
748 * layer. These variables give the necessary offsets from the beginning
749 * of the packet data.
753 * This is the offset of the beginning of the link-layer header from
754 * the beginning of the raw packet data.
756 * It's usually 0, except for 802.11 with a fixed-length radio header.
757 * (For 802.11 with a variable-length radio header, we have to generate
758 * code to compute that offset; off_ll is 0 in that case.)
763 * If there's a variable-length header preceding the link-layer header,
764 * "reg_off_ll" is the register number for a register containing the
765 * length of that header, and therefore the offset of the link-layer
766 * header from the beginning of the raw packet data. Otherwise,
767 * "reg_off_ll" is -1.
769 static int reg_off_ll
;
772 * This is the offset of the beginning of the MAC-layer header from
773 * the beginning of the link-layer header.
774 * It's usually 0, except for ATM LANE, where it's the offset, relative
775 * to the beginning of the raw packet data, of the Ethernet header.
777 static u_int off_mac
;
780 * This is the offset of the beginning of the MAC-layer payload,
781 * from the beginning of the raw packet data.
783 * I.e., it's the sum of the length of the link-layer header (without,
784 * for example, any 802.2 LLC header, so it's the MAC-layer
785 * portion of that header), plus any prefix preceding the
788 static u_int off_macpl
;
791 * This is 1 if the offset of the beginning of the MAC-layer payload
792 * from the beginning of the link-layer header is variable-length.
794 static int off_macpl_is_variable
;
797 * If the link layer has variable_length headers, "reg_off_macpl"
798 * is the register number for a register containing the length of the
799 * link-layer header plus the length of any variable-length header
800 * preceding the link-layer header. Otherwise, "reg_off_macpl"
803 static int reg_off_macpl
;
806 * "off_linktype" is the offset to information in the link-layer header
807 * giving the packet type. This offset is relative to the beginning
808 * of the link-layer header (i.e., it doesn't include off_ll).
810 * For Ethernet, it's the offset of the Ethernet type field.
812 * For link-layer types that always use 802.2 headers, it's the
813 * offset of the LLC header.
815 * For PPP, it's the offset of the PPP type field.
817 * For Cisco HDLC, it's the offset of the CHDLC type field.
819 * For BSD loopback, it's the offset of the AF_ value.
821 * For Linux cooked sockets, it's the offset of the type field.
823 * It's set to -1 for no encapsulation, in which case, IP is assumed.
825 static u_int off_linktype
;
828 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
829 * checks to check the PPP header, assumed to follow a LAN-style link-
830 * layer header and a PPPoE session header.
832 static int is_pppoes
= 0;
835 * TRUE if the link layer includes an ATM pseudo-header.
837 static int is_atm
= 0;
840 * TRUE if "lane" appeared in the filter; it causes us to generate
841 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
843 static int is_lane
= 0;
846 * These are offsets for the ATM pseudo-header.
848 static u_int off_vpi
;
849 static u_int off_vci
;
850 static u_int off_proto
;
853 * These are offsets for the MTP2 fields.
858 * These are offsets for the MTP3 fields.
860 static u_int off_sio
;
861 static u_int off_opc
;
862 static u_int off_dpc
;
863 static u_int off_sls
;
866 * This is the offset of the first byte after the ATM pseudo_header,
867 * or -1 if there is no ATM pseudo-header.
869 static u_int off_payload
;
872 * These are offsets to the beginning of the network-layer header.
873 * They are relative to the beginning of the MAC-layer payload (i.e.,
874 * they don't include off_ll or off_macpl).
876 * If the link layer never uses 802.2 LLC:
878 * "off_nl" and "off_nl_nosnap" are the same.
880 * If the link layer always uses 802.2 LLC:
882 * "off_nl" is the offset if there's a SNAP header following
885 * "off_nl_nosnap" is the offset if there's no SNAP header.
887 * If the link layer is Ethernet:
889 * "off_nl" is the offset if the packet is an Ethernet II packet
890 * (we assume no 802.3+802.2+SNAP);
892 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
893 * with an 802.2 header following it.
896 static u_int off_nl_nosnap
;
904 linktype
= pcap_datalink(p
);
906 pcap_fddipad
= p
->fddipad
;
910 * Assume it's not raw ATM with a pseudo-header, for now.
921 * And that we're not doing PPPoE.
926 * And assume we're not doing SS7.
935 * Also assume it's not 802.11.
939 off_macpl_is_variable
= 0;
943 label_stack_depth
= 0;
953 off_nl
= 0; /* XXX in reality, variable! */
954 off_nl_nosnap
= 0; /* no 802.2 LLC */
957 case DLT_ARCNET_LINUX
:
960 off_nl
= 0; /* XXX in reality, variable! */
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
966 off_macpl
= 14; /* Ethernet header length */
967 off_nl
= 0; /* Ethernet II */
968 off_nl_nosnap
= 3; /* 802.3+802.2 */
973 * SLIP doesn't have a link level type. The 16 byte
974 * header is hacked into our SLIP driver.
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 /* XXX this may be the same as the DLT_PPP_BSDOS case */
988 off_nl_nosnap
= 0; /* no 802.2 LLC */
996 off_nl_nosnap
= 0; /* no 802.2 LLC */
1003 off_nl_nosnap
= 0; /* no 802.2 LLC */
1008 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1009 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1018 * This does no include the Ethernet header, and
1019 * only covers session state.
1024 off_nl_nosnap
= 0; /* no 802.2 LLC */
1031 off_nl_nosnap
= 0; /* no 802.2 LLC */
1036 * FDDI doesn't really have a link-level type field.
1037 * We set "off_linktype" to the offset of the LLC header.
1039 * To check for Ethernet types, we assume that SSAP = SNAP
1040 * is being used and pick out the encapsulated Ethernet type.
1041 * XXX - should we generate code to check for SNAP?
1045 off_linktype
+= pcap_fddipad
;
1047 off_macpl
= 13; /* FDDI MAC header length */
1049 off_macpl
+= pcap_fddipad
;
1051 off_nl
= 8; /* 802.2+SNAP */
1052 off_nl_nosnap
= 3; /* 802.2 */
1057 * Token Ring doesn't really have a link-level type field.
1058 * We set "off_linktype" to the offset of the LLC header.
1060 * To check for Ethernet types, we assume that SSAP = SNAP
1061 * is being used and pick out the encapsulated Ethernet type.
1062 * XXX - should we generate code to check for SNAP?
1064 * XXX - the header is actually variable-length.
1065 * Some various Linux patched versions gave 38
1066 * as "off_linktype" and 40 as "off_nl"; however,
1067 * if a token ring packet has *no* routing
1068 * information, i.e. is not source-routed, the correct
1069 * values are 20 and 22, as they are in the vanilla code.
1071 * A packet is source-routed iff the uppermost bit
1072 * of the first byte of the source address, at an
1073 * offset of 8, has the uppermost bit set. If the
1074 * packet is source-routed, the total number of bytes
1075 * of routing information is 2 plus bits 0x1F00 of
1076 * the 16-bit value at an offset of 14 (shifted right
1077 * 8 - figure out which byte that is).
1080 off_macpl
= 14; /* Token Ring MAC header length */
1081 off_nl
= 8; /* 802.2+SNAP */
1082 off_nl_nosnap
= 3; /* 802.2 */
1085 case DLT_IEEE802_11
:
1086 case DLT_PRISM_HEADER
:
1087 case DLT_IEEE802_11_RADIO_AVS
:
1088 case DLT_IEEE802_11_RADIO
:
1090 * 802.11 doesn't really have a link-level type field.
1091 * We set "off_linktype" to the offset of the LLC header.
1093 * To check for Ethernet types, we assume that SSAP = SNAP
1094 * is being used and pick out the encapsulated Ethernet type.
1095 * XXX - should we generate code to check for SNAP?
1097 * We also handle variable-length radio headers here.
1098 * The Prism header is in theory variable-length, but in
1099 * practice it's always 144 bytes long. However, some
1100 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1101 * sometimes or always supply an AVS header, so we
1102 * have to check whether the radio header is a Prism
1103 * header or an AVS header, so, in practice, it's
1107 off_macpl
= 0; /* link-layer header is variable-length */
1108 off_macpl_is_variable
= 1;
1109 off_nl
= 8; /* 802.2+SNAP */
1110 off_nl_nosnap
= 3; /* 802.2 */
1115 * At the moment we treat PPI the same way that we treat
1116 * normal Radiotap encoded packets. The difference is in
1117 * the function that generates the code at the beginning
1118 * to compute the header length. Since this code generator
1119 * of PPI supports bare 802.11 encapsulation only (i.e.
1120 * the encapsulated DLT should be DLT_IEEE802_11) we
1121 * generate code to check for this too.
1124 off_macpl
= 0; /* link-layer header is variable-length */
1125 off_macpl_is_variable
= 1;
1126 off_nl
= 8; /* 802.2+SNAP */
1127 off_nl_nosnap
= 3; /* 802.2 */
1130 case DLT_ATM_RFC1483
:
1131 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1133 * assume routed, non-ISO PDUs
1134 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1136 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1137 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1138 * latter would presumably be treated the way PPPoE
1139 * should be, so you can do "pppoe and udp port 2049"
1140 * or "pppoa and tcp port 80" and have it check for
1141 * PPPo{A,E} and a PPP protocol of IP and....
1144 off_macpl
= 0; /* packet begins with LLC header */
1145 off_nl
= 8; /* 802.2+SNAP */
1146 off_nl_nosnap
= 3; /* 802.2 */
1151 * Full Frontal ATM; you get AALn PDUs with an ATM
1155 off_vpi
= SUNATM_VPI_POS
;
1156 off_vci
= SUNATM_VCI_POS
;
1157 off_proto
= PROTO_POS
;
1158 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1159 off_payload
= SUNATM_PKT_BEGIN_POS
;
1160 off_linktype
= off_payload
;
1161 off_macpl
= off_payload
; /* if LLC-encapsulated */
1162 off_nl
= 8; /* 802.2+SNAP */
1163 off_nl_nosnap
= 3; /* 802.2 */
1172 off_nl_nosnap
= 0; /* no 802.2 LLC */
1175 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1179 off_nl_nosnap
= 0; /* no 802.2 LLC */
1184 * LocalTalk does have a 1-byte type field in the LLAP header,
1185 * but really it just indicates whether there is a "short" or
1186 * "long" DDP packet following.
1191 off_nl_nosnap
= 0; /* no 802.2 LLC */
1194 case DLT_IP_OVER_FC
:
1196 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1197 * link-level type field. We set "off_linktype" to the
1198 * offset of the LLC header.
1200 * To check for Ethernet types, we assume that SSAP = SNAP
1201 * is being used and pick out the encapsulated Ethernet type.
1202 * XXX - should we generate code to check for SNAP? RFC
1203 * 2625 says SNAP should be used.
1207 off_nl
= 8; /* 802.2+SNAP */
1208 off_nl_nosnap
= 3; /* 802.2 */
1213 * XXX - we should set this to handle SNAP-encapsulated
1214 * frames (NLPID of 0x80).
1219 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 * the only BPF-interesting FRF.16 frames are non-control frames;
1224 * Frame Relay has a variable length link-layer
1225 * so lets start with offset 4 for now and increments later on (FIXME);
1231 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1234 case DLT_APPLE_IP_OVER_IEEE1394
:
1238 off_nl_nosnap
= 0; /* no 802.2 LLC */
1241 case DLT_LINUX_IRDA
:
1243 * Currently, only raw "link[N:M]" filtering is supported.
1253 * Currently, only raw "link[N:M]" filtering is supported.
1261 case DLT_SYMANTEC_FIREWALL
:
1264 off_nl
= 0; /* Ethernet II */
1265 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1268 #ifdef HAVE_NET_PFVAR_H
1271 off_macpl
= PFLOG_HDRLEN
;
1273 off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 case DLT_JUNIPER_MFR
:
1278 case DLT_JUNIPER_MLFR
:
1279 case DLT_JUNIPER_MLPPP
:
1280 case DLT_JUNIPER_PPP
:
1281 case DLT_JUNIPER_CHDLC
:
1282 case DLT_JUNIPER_FRELAY
:
1286 off_nl_nosnap
= -1; /* no 802.2 LLC */
1289 case DLT_JUNIPER_ATM1
:
1290 off_linktype
= 4; /* in reality variable between 4-8 */
1291 off_macpl
= 4; /* in reality variable between 4-8 */
1296 case DLT_JUNIPER_ATM2
:
1297 off_linktype
= 8; /* in reality variable between 8-12 */
1298 off_macpl
= 8; /* in reality variable between 8-12 */
1303 /* frames captured on a Juniper PPPoE service PIC
1304 * contain raw ethernet frames */
1305 case DLT_JUNIPER_PPPOE
:
1306 case DLT_JUNIPER_ETHER
:
1309 off_nl
= 18; /* Ethernet II */
1310 off_nl_nosnap
= 21; /* 802.3+802.2 */
1313 case DLT_JUNIPER_PPPOE_ATM
:
1317 off_nl_nosnap
= -1; /* no 802.2 LLC */
1320 case DLT_JUNIPER_GGSN
:
1324 off_nl_nosnap
= -1; /* no 802.2 LLC */
1327 case DLT_JUNIPER_ES
:
1329 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1330 off_nl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_JUNIPER_MONITOR
:
1337 off_nl
= 0; /* raw IP/IP6 header */
1338 off_nl_nosnap
= -1; /* no 802.2 LLC */
1341 case DLT_JUNIPER_SERVICES
:
1343 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1344 off_nl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl_nosnap
= -1; /* no 802.2 LLC */
1348 case DLT_JUNIPER_VP
:
1355 case DLT_JUNIPER_ST
:
1362 case DLT_JUNIPER_ISM
:
1369 case DLT_JUNIPER_VS
:
1370 case DLT_JUNIPER_SRX_E2E
:
1371 case DLT_JUNIPER_FIBRECHANNEL
:
1390 case DLT_MTP2_WITH_PHDR
:
1423 case DLT_LINUX_LAPD
:
1425 * Currently, only raw "link[N:M]" filtering is supported.
1435 * Currently, only raw "link[N:M]" filtering is supported.
1443 case DLT_BLUETOOTH_HCI_H4
:
1445 * Currently, only raw "link[N:M]" filtering is supported.
1455 * Currently, only raw "link[N:M]" filtering is supported.
1465 * Currently, only raw "link[N:M]" filtering is supported.
1473 case DLT_IEEE802_15_4_LINUX
:
1475 * Currently, only raw "link[N:M]" filtering is supported.
1483 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1485 * Currently, only raw "link[N:M]" filtering is supported.
1493 case DLT_IEEE802_15_4
:
1495 * Currently, only raw "link[N:M]" filtering is supported.
1505 * Currently, only raw "link[N:M]" filtering is supported.
1515 * Currently, only raw "link[N:M]" filtering is supported.
1525 * Currently, only raw "link[N:M]" filtering is supported.
1533 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1535 * Currently, only raw "link[N:M]" filtering is supported.
1545 * Currently, only raw "link[N:M]" filtering is supported.
1547 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1549 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1550 off_nl_nosnap
= -1; /* no 802.2 LLC */
1551 off_mac
= 1; /* step over the kiss length byte */
1554 case DLT_IEEE802_15_4_NONASK_PHY
:
1556 * Currently, only raw "link[N:M]" filtering is supported.
1566 * Currently, only raw "link[N:M]" filtering is supported.
1574 case DLT_USB_LINUX_MMAPPED
:
1576 * Currently, only raw "link[N:M]" filtering is supported.
1584 case DLT_CAN_SOCKETCAN
:
1586 * Currently, only raw "link[N:M]" filtering is supported.
1596 off_macpl
= 24; /* ipnet header length */
1601 case DLT_IEEE802_15_4_NOFCS
:
1603 * Currently, only raw "link[N:M]" filtering is supported.
1611 bpf_error("unknown data link type %d", linktype
);
1616 * Load a value relative to the beginning of the link-layer header.
1617 * The link-layer header doesn't necessarily begin at the beginning
1618 * of the packet data; there might be a variable-length prefix containing
1619 * radio information.
1621 static struct slist
*
1622 gen_load_llrel(offset
, size
)
1625 struct slist
*s
, *s2
;
1627 s
= gen_llprefixlen();
1630 * If "s" is non-null, it has code to arrange that the X register
1631 * contains the length of the prefix preceding the link-layer
1634 * Otherwise, the length of the prefix preceding the link-layer
1635 * header is "off_ll".
1639 * There's a variable-length prefix preceding the
1640 * link-layer header. "s" points to a list of statements
1641 * that put the length of that prefix into the X register.
1642 * do an indirect load, to use the X register as an offset.
1644 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1649 * There is no variable-length header preceding the
1650 * link-layer header; add in off_ll, which, if there's
1651 * a fixed-length header preceding the link-layer header,
1652 * is the length of that header.
1654 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1655 s
->s
.k
= offset
+ off_ll
;
1661 * Load a value relative to the beginning of the MAC-layer payload.
1663 static struct slist
*
1664 gen_load_macplrel(offset
, size
)
1667 struct slist
*s
, *s2
;
1669 s
= gen_off_macpl();
1672 * If s is non-null, the offset of the MAC-layer payload is
1673 * variable, and s points to a list of instructions that
1674 * arrange that the X register contains that offset.
1676 * Otherwise, the offset of the MAC-layer payload is constant,
1677 * and is in off_macpl.
1681 * The offset of the MAC-layer payload is in the X
1682 * register. Do an indirect load, to use the X register
1685 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1690 * The offset of the MAC-layer payload is constant,
1691 * and is in off_macpl; load the value at that offset
1692 * plus the specified offset.
1694 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1695 s
->s
.k
= off_macpl
+ offset
;
1701 * Load a value relative to the beginning of the specified header.
1703 static struct slist
*
1704 gen_load_a(offrel
, offset
, size
)
1705 enum e_offrel offrel
;
1708 struct slist
*s
, *s2
;
1713 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1718 s
= gen_load_llrel(offset
, size
);
1722 s
= gen_load_macplrel(offset
, size
);
1726 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1730 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1735 * Load the X register with the length of the IPv4 header
1736 * (plus the offset of the link-layer header, if it's
1737 * preceded by a variable-length header such as a radio
1738 * header), in bytes.
1740 s
= gen_loadx_iphdrlen();
1743 * Load the item at {offset of the MAC-layer payload} +
1744 * {offset, relative to the start of the MAC-layer
1745 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1746 * {specified offset}.
1748 * (If the offset of the MAC-layer payload is variable,
1749 * it's included in the value in the X register, and
1752 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1753 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1758 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1769 * Generate code to load into the X register the sum of the length of
1770 * the IPv4 header and any variable-length header preceding the link-layer
1773 static struct slist
*
1774 gen_loadx_iphdrlen()
1776 struct slist
*s
, *s2
;
1778 s
= gen_off_macpl();
1781 * There's a variable-length prefix preceding the
1782 * link-layer header, or the link-layer header is itself
1783 * variable-length. "s" points to a list of statements
1784 * that put the offset of the MAC-layer payload into
1787 * The 4*([k]&0xf) addressing mode can't be used, as we
1788 * don't have a constant offset, so we have to load the
1789 * value in question into the A register and add to it
1790 * the value from the X register.
1792 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1795 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1798 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1803 * The A register now contains the length of the
1804 * IP header. We need to add to it the offset of
1805 * the MAC-layer payload, which is still in the X
1806 * register, and move the result into the X register.
1808 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1809 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1812 * There is no variable-length header preceding the
1813 * link-layer header, and the link-layer header is
1814 * fixed-length; load the length of the IPv4 header,
1815 * which is at an offset of off_nl from the beginning
1816 * of the MAC-layer payload, and thus at an offset
1817 * of off_mac_pl + off_nl from the beginning of the
1820 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1821 s
->s
.k
= off_macpl
+ off_nl
;
1826 static struct block
*
1833 s
= new_stmt(BPF_LD
|BPF_IMM
);
1835 b
= new_block(JMP(BPF_JEQ
));
1841 static inline struct block
*
1844 return gen_uncond(1);
1847 static inline struct block
*
1850 return gen_uncond(0);
1854 * Byte-swap a 32-bit number.
1855 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1856 * big-endian platforms.)
1858 #define SWAPLONG(y) \
1859 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1862 * Generate code to match a particular packet type.
1864 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1865 * value, if <= ETHERMTU. We use that to determine whether to
1866 * match the type/length field or to check the type/length field for
1867 * a value <= ETHERMTU to see whether it's a type field and then do
1868 * the appropriate test.
1870 static struct block
*
1871 gen_ether_linktype(proto
)
1874 struct block
*b0
, *b1
;
1880 case LLCSAP_NETBEUI
:
1882 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1883 * so we check the DSAP and SSAP.
1885 * LLCSAP_IP checks for IP-over-802.2, rather
1886 * than IP-over-Ethernet or IP-over-SNAP.
1888 * XXX - should we check both the DSAP and the
1889 * SSAP, like this, or should we check just the
1890 * DSAP, as we do for other types <= ETHERMTU
1891 * (i.e., other SAP values)?
1893 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1895 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1896 ((proto
<< 8) | proto
));
1904 * Ethernet_II frames, which are Ethernet
1905 * frames with a frame type of ETHERTYPE_IPX;
1907 * Ethernet_802.3 frames, which are 802.3
1908 * frames (i.e., the type/length field is
1909 * a length field, <= ETHERMTU, rather than
1910 * a type field) with the first two bytes
1911 * after the Ethernet/802.3 header being
1914 * Ethernet_802.2 frames, which are 802.3
1915 * frames with an 802.2 LLC header and
1916 * with the IPX LSAP as the DSAP in the LLC
1919 * Ethernet_SNAP frames, which are 802.3
1920 * frames with an LLC header and a SNAP
1921 * header and with an OUI of 0x000000
1922 * (encapsulated Ethernet) and a protocol
1923 * ID of ETHERTYPE_IPX in the SNAP header.
1925 * XXX - should we generate the same code both
1926 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1930 * This generates code to check both for the
1931 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1933 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1934 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1938 * Now we add code to check for SNAP frames with
1939 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1941 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1945 * Now we generate code to check for 802.3
1946 * frames in general.
1948 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1952 * Now add the check for 802.3 frames before the
1953 * check for Ethernet_802.2 and Ethernet_802.3,
1954 * as those checks should only be done on 802.3
1955 * frames, not on Ethernet frames.
1960 * Now add the check for Ethernet_II frames, and
1961 * do that before checking for the other frame
1964 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1965 (bpf_int32
)ETHERTYPE_IPX
);
1969 case ETHERTYPE_ATALK
:
1970 case ETHERTYPE_AARP
:
1972 * EtherTalk (AppleTalk protocols on Ethernet link
1973 * layer) may use 802.2 encapsulation.
1977 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1978 * we check for an Ethernet type field less than
1979 * 1500, which means it's an 802.3 length field.
1981 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1985 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1986 * SNAP packets with an organization code of
1987 * 0x080007 (Apple, for Appletalk) and a protocol
1988 * type of ETHERTYPE_ATALK (Appletalk).
1990 * 802.2-encapsulated ETHERTYPE_AARP packets are
1991 * SNAP packets with an organization code of
1992 * 0x000000 (encapsulated Ethernet) and a protocol
1993 * type of ETHERTYPE_AARP (Appletalk ARP).
1995 if (proto
== ETHERTYPE_ATALK
)
1996 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1997 else /* proto == ETHERTYPE_AARP */
1998 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2002 * Check for Ethernet encapsulation (Ethertalk
2003 * phase 1?); we just check for the Ethernet
2006 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2012 if (proto
<= ETHERMTU
) {
2014 * This is an LLC SAP value, so the frames
2015 * that match would be 802.2 frames.
2016 * Check that the frame is an 802.2 frame
2017 * (i.e., that the length/type field is
2018 * a length field, <= ETHERMTU) and
2019 * then check the DSAP.
2021 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
2023 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
2029 * This is an Ethernet type, so compare
2030 * the length/type field with it (if
2031 * the frame is an 802.2 frame, the length
2032 * field will be <= ETHERMTU, and, as
2033 * "proto" is > ETHERMTU, this test
2034 * will fail and the frame won't match,
2035 * which is what we want).
2037 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2044 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2045 * or IPv6 then we have an error.
2047 static struct block
*
2048 gen_ipnet_linktype(proto
)
2054 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2055 (bpf_int32
)IPH_AF_INET
);
2058 case ETHERTYPE_IPV6
:
2059 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2060 (bpf_int32
)IPH_AF_INET6
);
2071 * Generate code to match a particular packet type.
2073 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2074 * value, if <= ETHERMTU. We use that to determine whether to
2075 * match the type field or to check the type field for the special
2076 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2078 static struct block
*
2079 gen_linux_sll_linktype(proto
)
2082 struct block
*b0
, *b1
;
2088 case LLCSAP_NETBEUI
:
2090 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2091 * so we check the DSAP and SSAP.
2093 * LLCSAP_IP checks for IP-over-802.2, rather
2094 * than IP-over-Ethernet or IP-over-SNAP.
2096 * XXX - should we check both the DSAP and the
2097 * SSAP, like this, or should we check just the
2098 * DSAP, as we do for other types <= ETHERMTU
2099 * (i.e., other SAP values)?
2101 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2102 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2103 ((proto
<< 8) | proto
));
2109 * Ethernet_II frames, which are Ethernet
2110 * frames with a frame type of ETHERTYPE_IPX;
2112 * Ethernet_802.3 frames, which have a frame
2113 * type of LINUX_SLL_P_802_3;
2115 * Ethernet_802.2 frames, which are 802.3
2116 * frames with an 802.2 LLC header (i.e, have
2117 * a frame type of LINUX_SLL_P_802_2) and
2118 * with the IPX LSAP as the DSAP in the LLC
2121 * Ethernet_SNAP frames, which are 802.3
2122 * frames with an LLC header and a SNAP
2123 * header and with an OUI of 0x000000
2124 * (encapsulated Ethernet) and a protocol
2125 * ID of ETHERTYPE_IPX in the SNAP header.
2127 * First, do the checks on LINUX_SLL_P_802_2
2128 * frames; generate the check for either
2129 * Ethernet_802.2 or Ethernet_SNAP frames, and
2130 * then put a check for LINUX_SLL_P_802_2 frames
2133 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2134 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2136 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2140 * Now check for 802.3 frames and OR that with
2141 * the previous test.
2143 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2147 * Now add the check for Ethernet_II frames, and
2148 * do that before checking for the other frame
2151 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2152 (bpf_int32
)ETHERTYPE_IPX
);
2156 case ETHERTYPE_ATALK
:
2157 case ETHERTYPE_AARP
:
2159 * EtherTalk (AppleTalk protocols on Ethernet link
2160 * layer) may use 802.2 encapsulation.
2164 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2165 * we check for the 802.2 protocol type in the
2166 * "Ethernet type" field.
2168 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2171 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2172 * SNAP packets with an organization code of
2173 * 0x080007 (Apple, for Appletalk) and a protocol
2174 * type of ETHERTYPE_ATALK (Appletalk).
2176 * 802.2-encapsulated ETHERTYPE_AARP packets are
2177 * SNAP packets with an organization code of
2178 * 0x000000 (encapsulated Ethernet) and a protocol
2179 * type of ETHERTYPE_AARP (Appletalk ARP).
2181 if (proto
== ETHERTYPE_ATALK
)
2182 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2183 else /* proto == ETHERTYPE_AARP */
2184 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2188 * Check for Ethernet encapsulation (Ethertalk
2189 * phase 1?); we just check for the Ethernet
2192 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2198 if (proto
<= ETHERMTU
) {
2200 * This is an LLC SAP value, so the frames
2201 * that match would be 802.2 frames.
2202 * Check for the 802.2 protocol type
2203 * in the "Ethernet type" field, and
2204 * then check the DSAP.
2206 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2208 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2214 * This is an Ethernet type, so compare
2215 * the length/type field with it (if
2216 * the frame is an 802.2 frame, the length
2217 * field will be <= ETHERMTU, and, as
2218 * "proto" is > ETHERMTU, this test
2219 * will fail and the frame won't match,
2220 * which is what we want).
2222 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2228 static struct slist
*
2229 gen_load_prism_llprefixlen()
2231 struct slist
*s1
, *s2
;
2232 struct slist
*sjeq_avs_cookie
;
2233 struct slist
*sjcommon
;
2236 * This code is not compatible with the optimizer, as
2237 * we are generating jmp instructions within a normal
2238 * slist of instructions
2243 * Generate code to load the length of the radio header into
2244 * the register assigned to hold that length, if one has been
2245 * assigned. (If one hasn't been assigned, no code we've
2246 * generated uses that prefix, so we don't need to generate any
2249 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2250 * or always use the AVS header rather than the Prism header.
2251 * We load a 4-byte big-endian value at the beginning of the
2252 * raw packet data, and see whether, when masked with 0xFFFFF000,
2253 * it's equal to 0x80211000. If so, that indicates that it's
2254 * an AVS header (the masked-out bits are the version number).
2255 * Otherwise, it's a Prism header.
2257 * XXX - the Prism header is also, in theory, variable-length,
2258 * but no known software generates headers that aren't 144
2261 if (reg_off_ll
!= -1) {
2265 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2269 * AND it with 0xFFFFF000.
2271 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2272 s2
->s
.k
= 0xFFFFF000;
2276 * Compare with 0x80211000.
2278 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2279 sjeq_avs_cookie
->s
.k
= 0x80211000;
2280 sappend(s1
, sjeq_avs_cookie
);
2285 * The 4 bytes at an offset of 4 from the beginning of
2286 * the AVS header are the length of the AVS header.
2287 * That field is big-endian.
2289 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2292 sjeq_avs_cookie
->s
.jt
= s2
;
2295 * Now jump to the code to allocate a register
2296 * into which to save the header length and
2297 * store the length there. (The "jump always"
2298 * instruction needs to have the k field set;
2299 * it's added to the PC, so, as we're jumping
2300 * over a single instruction, it should be 1.)
2302 sjcommon
= new_stmt(JMP(BPF_JA
));
2304 sappend(s1
, sjcommon
);
2307 * Now for the code that handles the Prism header.
2308 * Just load the length of the Prism header (144)
2309 * into the A register. Have the test for an AVS
2310 * header branch here if we don't have an AVS header.
2312 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2315 sjeq_avs_cookie
->s
.jf
= s2
;
2318 * Now allocate a register to hold that value and store
2319 * it. The code for the AVS header will jump here after
2320 * loading the length of the AVS header.
2322 s2
= new_stmt(BPF_ST
);
2323 s2
->s
.k
= reg_off_ll
;
2325 sjcommon
->s
.jf
= s2
;
2328 * Now move it into the X register.
2330 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2338 static struct slist
*
2339 gen_load_avs_llprefixlen()
2341 struct slist
*s1
, *s2
;
2344 * Generate code to load the length of the AVS header into
2345 * the register assigned to hold that length, if one has been
2346 * assigned. (If one hasn't been assigned, no code we've
2347 * generated uses that prefix, so we don't need to generate any
2350 if (reg_off_ll
!= -1) {
2352 * The 4 bytes at an offset of 4 from the beginning of
2353 * the AVS header are the length of the AVS header.
2354 * That field is big-endian.
2356 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2360 * Now allocate a register to hold that value and store
2363 s2
= new_stmt(BPF_ST
);
2364 s2
->s
.k
= reg_off_ll
;
2368 * Now move it into the X register.
2370 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2378 static struct slist
*
2379 gen_load_radiotap_llprefixlen()
2381 struct slist
*s1
, *s2
;
2384 * Generate code to load the length of the radiotap header into
2385 * the register assigned to hold that length, if one has been
2386 * assigned. (If one hasn't been assigned, no code we've
2387 * generated uses that prefix, so we don't need to generate any
2390 if (reg_off_ll
!= -1) {
2392 * The 2 bytes at offsets of 2 and 3 from the beginning
2393 * of the radiotap header are the length of the radiotap
2394 * header; unfortunately, it's little-endian, so we have
2395 * to load it a byte at a time and construct the value.
2399 * Load the high-order byte, at an offset of 3, shift it
2400 * left a byte, and put the result in the X register.
2402 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2404 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2407 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2411 * Load the next byte, at an offset of 2, and OR the
2412 * value from the X register into it.
2414 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2417 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2421 * Now allocate a register to hold that value and store
2424 s2
= new_stmt(BPF_ST
);
2425 s2
->s
.k
= reg_off_ll
;
2429 * Now move it into the X register.
2431 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2440 * At the moment we treat PPI as normal Radiotap encoded
2441 * packets. The difference is in the function that generates
2442 * the code at the beginning to compute the header length.
2443 * Since this code generator of PPI supports bare 802.11
2444 * encapsulation only (i.e. the encapsulated DLT should be
2445 * DLT_IEEE802_11) we generate code to check for this too;
2446 * that's done in finish_parse().
2448 static struct slist
*
2449 gen_load_ppi_llprefixlen()
2451 struct slist
*s1
, *s2
;
2454 * Generate code to load the length of the radiotap header
2455 * into the register assigned to hold that length, if one has
2458 if (reg_off_ll
!= -1) {
2460 * The 2 bytes at offsets of 2 and 3 from the beginning
2461 * of the radiotap header are the length of the radiotap
2462 * header; unfortunately, it's little-endian, so we have
2463 * to load it a byte at a time and construct the value.
2467 * Load the high-order byte, at an offset of 3, shift it
2468 * left a byte, and put the result in the X register.
2470 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2472 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2475 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2479 * Load the next byte, at an offset of 2, and OR the
2480 * value from the X register into it.
2482 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2485 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2489 * Now allocate a register to hold that value and store
2492 s2
= new_stmt(BPF_ST
);
2493 s2
->s
.k
= reg_off_ll
;
2497 * Now move it into the X register.
2499 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2508 * Load a value relative to the beginning of the link-layer header after the 802.11
2509 * header, i.e. LLC_SNAP.
2510 * The link-layer header doesn't necessarily begin at the beginning
2511 * of the packet data; there might be a variable-length prefix containing
2512 * radio information.
2514 static struct slist
*
2515 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2518 struct slist
*sjset_data_frame_1
;
2519 struct slist
*sjset_data_frame_2
;
2520 struct slist
*sjset_qos
;
2521 struct slist
*sjset_radiotap_flags
;
2522 struct slist
*sjset_radiotap_tsft
;
2523 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2524 struct slist
*s_roundup
;
2526 if (reg_off_macpl
== -1) {
2528 * No register has been assigned to the offset of
2529 * the MAC-layer payload, which means nobody needs
2530 * it; don't bother computing it - just return
2531 * what we already have.
2537 * This code is not compatible with the optimizer, as
2538 * we are generating jmp instructions within a normal
2539 * slist of instructions
2544 * If "s" is non-null, it has code to arrange that the X register
2545 * contains the length of the prefix preceding the link-layer
2548 * Otherwise, the length of the prefix preceding the link-layer
2549 * header is "off_ll".
2553 * There is no variable-length header preceding the
2554 * link-layer header.
2556 * Load the length of the fixed-length prefix preceding
2557 * the link-layer header (if any) into the X register,
2558 * and store it in the reg_off_macpl register.
2559 * That length is off_ll.
2561 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2566 * The X register contains the offset of the beginning of the
2567 * link-layer header; add 24, which is the minimum length
2568 * of the MAC header for a data frame, to that, and store it
2569 * in reg_off_macpl, and then load the Frame Control field,
2570 * which is at the offset in the X register, with an indexed load.
2572 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2574 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2577 s2
= new_stmt(BPF_ST
);
2578 s2
->s
.k
= reg_off_macpl
;
2581 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2586 * Check the Frame Control field to see if this is a data frame;
2587 * a data frame has the 0x08 bit (b3) in that field set and the
2588 * 0x04 bit (b2) clear.
2590 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2591 sjset_data_frame_1
->s
.k
= 0x08;
2592 sappend(s
, sjset_data_frame_1
);
2595 * If b3 is set, test b2, otherwise go to the first statement of
2596 * the rest of the program.
2598 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2599 sjset_data_frame_2
->s
.k
= 0x04;
2600 sappend(s
, sjset_data_frame_2
);
2601 sjset_data_frame_1
->s
.jf
= snext
;
2604 * If b2 is not set, this is a data frame; test the QoS bit.
2605 * Otherwise, go to the first statement of the rest of the
2608 sjset_data_frame_2
->s
.jt
= snext
;
2609 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2610 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2611 sappend(s
, sjset_qos
);
2614 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2616 * Otherwise, go to the first statement of the rest of the
2619 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2620 s2
->s
.k
= reg_off_macpl
;
2622 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2625 s2
= new_stmt(BPF_ST
);
2626 s2
->s
.k
= reg_off_macpl
;
2630 * If we have a radiotap header, look at it to see whether
2631 * there's Atheros padding between the MAC-layer header
2634 * Note: all of the fields in the radiotap header are
2635 * little-endian, so we byte-swap all of the values
2636 * we test against, as they will be loaded as big-endian
2639 if (linktype
== DLT_IEEE802_11_RADIO
) {
2641 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2642 * in the presence flag?
2644 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2648 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2649 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2650 sappend(s
, sjset_radiotap_flags
);
2653 * If not, skip all of this.
2655 sjset_radiotap_flags
->s
.jf
= snext
;
2658 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2660 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2661 new_stmt(JMP(BPF_JSET
));
2662 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2663 sappend(s
, sjset_radiotap_tsft
);
2666 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2667 * at an offset of 16 from the beginning of the raw packet
2668 * data (8 bytes for the radiotap header and 8 bytes for
2671 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2674 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2678 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2679 sjset_tsft_datapad
->s
.k
= 0x20;
2680 sappend(s
, sjset_tsft_datapad
);
2683 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2684 * at an offset of 8 from the beginning of the raw packet
2685 * data (8 bytes for the radiotap header).
2687 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2690 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2694 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2695 sjset_notsft_datapad
->s
.k
= 0x20;
2696 sappend(s
, sjset_notsft_datapad
);
2699 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2700 * set, round the length of the 802.11 header to
2701 * a multiple of 4. Do that by adding 3 and then
2702 * dividing by and multiplying by 4, which we do by
2705 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2706 s_roundup
->s
.k
= reg_off_macpl
;
2707 sappend(s
, s_roundup
);
2708 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2711 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2714 s2
= new_stmt(BPF_ST
);
2715 s2
->s
.k
= reg_off_macpl
;
2718 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2719 sjset_tsft_datapad
->s
.jf
= snext
;
2720 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2721 sjset_notsft_datapad
->s
.jf
= snext
;
2723 sjset_qos
->s
.jf
= snext
;
2729 insert_compute_vloffsets(b
)
2735 * For link-layer types that have a variable-length header
2736 * preceding the link-layer header, generate code to load
2737 * the offset of the link-layer header into the register
2738 * assigned to that offset, if any.
2742 case DLT_PRISM_HEADER
:
2743 s
= gen_load_prism_llprefixlen();
2746 case DLT_IEEE802_11_RADIO_AVS
:
2747 s
= gen_load_avs_llprefixlen();
2750 case DLT_IEEE802_11_RADIO
:
2751 s
= gen_load_radiotap_llprefixlen();
2755 s
= gen_load_ppi_llprefixlen();
2764 * For link-layer types that have a variable-length link-layer
2765 * header, generate code to load the offset of the MAC-layer
2766 * payload into the register assigned to that offset, if any.
2770 case DLT_IEEE802_11
:
2771 case DLT_PRISM_HEADER
:
2772 case DLT_IEEE802_11_RADIO_AVS
:
2773 case DLT_IEEE802_11_RADIO
:
2775 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2780 * If we have any offset-loading code, append all the
2781 * existing statements in the block to those statements,
2782 * and make the resulting list the list of statements
2786 sappend(s
, b
->stmts
);
2791 static struct block
*
2792 gen_ppi_dlt_check(void)
2794 struct slist
*s_load_dlt
;
2797 if (linktype
== DLT_PPI
)
2799 /* Create the statements that check for the DLT
2801 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2802 s_load_dlt
->s
.k
= 4;
2804 b
= new_block(JMP(BPF_JEQ
));
2806 b
->stmts
= s_load_dlt
;
2807 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2817 static struct slist
*
2818 gen_prism_llprefixlen(void)
2822 if (reg_off_ll
== -1) {
2824 * We haven't yet assigned a register for the length
2825 * of the radio header; allocate one.
2827 reg_off_ll
= alloc_reg();
2831 * Load the register containing the radio length
2832 * into the X register.
2834 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2835 s
->s
.k
= reg_off_ll
;
2839 static struct slist
*
2840 gen_avs_llprefixlen(void)
2844 if (reg_off_ll
== -1) {
2846 * We haven't yet assigned a register for the length
2847 * of the AVS header; allocate one.
2849 reg_off_ll
= alloc_reg();
2853 * Load the register containing the AVS length
2854 * into the X register.
2856 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2857 s
->s
.k
= reg_off_ll
;
2861 static struct slist
*
2862 gen_radiotap_llprefixlen(void)
2866 if (reg_off_ll
== -1) {
2868 * We haven't yet assigned a register for the length
2869 * of the radiotap header; allocate one.
2871 reg_off_ll
= alloc_reg();
2875 * Load the register containing the radiotap length
2876 * into the X register.
2878 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2879 s
->s
.k
= reg_off_ll
;
2884 * At the moment we treat PPI as normal Radiotap encoded
2885 * packets. The difference is in the function that generates
2886 * the code at the beginning to compute the header length.
2887 * Since this code generator of PPI supports bare 802.11
2888 * encapsulation only (i.e. the encapsulated DLT should be
2889 * DLT_IEEE802_11) we generate code to check for this too.
2891 static struct slist
*
2892 gen_ppi_llprefixlen(void)
2896 if (reg_off_ll
== -1) {
2898 * We haven't yet assigned a register for the length
2899 * of the radiotap header; allocate one.
2901 reg_off_ll
= alloc_reg();
2905 * Load the register containing the PPI length
2906 * into the X register.
2908 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2909 s
->s
.k
= reg_off_ll
;
2914 * Generate code to compute the link-layer header length, if necessary,
2915 * putting it into the X register, and to return either a pointer to a
2916 * "struct slist" for the list of statements in that code, or NULL if
2917 * no code is necessary.
2919 static struct slist
*
2920 gen_llprefixlen(void)
2924 case DLT_PRISM_HEADER
:
2925 return gen_prism_llprefixlen();
2927 case DLT_IEEE802_11_RADIO_AVS
:
2928 return gen_avs_llprefixlen();
2930 case DLT_IEEE802_11_RADIO
:
2931 return gen_radiotap_llprefixlen();
2934 return gen_ppi_llprefixlen();
2942 * Generate code to load the register containing the offset of the
2943 * MAC-layer payload into the X register; if no register for that offset
2944 * has been allocated, allocate it first.
2946 static struct slist
*
2951 if (off_macpl_is_variable
) {
2952 if (reg_off_macpl
== -1) {
2954 * We haven't yet assigned a register for the offset
2955 * of the MAC-layer payload; allocate one.
2957 reg_off_macpl
= alloc_reg();
2961 * Load the register containing the offset of the MAC-layer
2962 * payload into the X register.
2964 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2965 s
->s
.k
= reg_off_macpl
;
2969 * That offset isn't variable, so we don't need to
2970 * generate any code.
2977 * Map an Ethernet type to the equivalent PPP type.
2980 ethertype_to_ppptype(proto
)
2990 case ETHERTYPE_IPV6
:
2999 case ETHERTYPE_ATALK
:
3013 * I'm assuming the "Bridging PDU"s that go
3014 * over PPP are Spanning Tree Protocol
3028 * Generate code to match a particular packet type by matching the
3029 * link-layer type field or fields in the 802.2 LLC header.
3031 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3032 * value, if <= ETHERMTU.
3034 static struct block
*
3038 struct block
*b0
, *b1
, *b2
;
3040 /* are we checking MPLS-encapsulated packets? */
3041 if (label_stack_depth
> 0) {
3045 /* FIXME add other L3 proto IDs */
3046 return gen_mpls_linktype(Q_IP
);
3048 case ETHERTYPE_IPV6
:
3050 /* FIXME add other L3 proto IDs */
3051 return gen_mpls_linktype(Q_IPV6
);
3054 bpf_error("unsupported protocol over mpls");
3060 * Are we testing PPPoE packets?
3064 * The PPPoE session header is part of the
3065 * MAC-layer payload, so all references
3066 * should be relative to the beginning of
3071 * We use Ethernet protocol types inside libpcap;
3072 * map them to the corresponding PPP protocol types.
3074 proto
= ethertype_to_ppptype(proto
);
3075 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3081 return gen_ether_linktype(proto
);
3089 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3093 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3100 case DLT_IEEE802_11
:
3101 case DLT_PRISM_HEADER
:
3102 case DLT_IEEE802_11_RADIO_AVS
:
3103 case DLT_IEEE802_11_RADIO
:
3106 * Check that we have a data frame.
3108 b0
= gen_check_802_11_data_frame();
3111 * Now check for the specified link-layer type.
3113 b1
= gen_llc_linktype(proto
);
3121 * XXX - check for asynchronous frames, as per RFC 1103.
3123 return gen_llc_linktype(proto
);
3129 * XXX - check for LLC PDUs, as per IEEE 802.5.
3131 return gen_llc_linktype(proto
);
3135 case DLT_ATM_RFC1483
:
3137 case DLT_IP_OVER_FC
:
3138 return gen_llc_linktype(proto
);
3144 * If "is_lane" is set, check for a LANE-encapsulated
3145 * version of this protocol, otherwise check for an
3146 * LLC-encapsulated version of this protocol.
3148 * We assume LANE means Ethernet, not Token Ring.
3152 * Check that the packet doesn't begin with an
3153 * LE Control marker. (We've already generated
3156 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3161 * Now generate an Ethernet test.
3163 b1
= gen_ether_linktype(proto
);
3168 * Check for LLC encapsulation and then check the
3171 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3172 b1
= gen_llc_linktype(proto
);
3180 return gen_linux_sll_linktype(proto
);
3185 case DLT_SLIP_BSDOS
:
3188 * These types don't provide any type field; packets
3189 * are always IPv4 or IPv6.
3191 * XXX - for IPv4, check for a version number of 4, and,
3192 * for IPv6, check for a version number of 6?
3197 /* Check for a version number of 4. */
3198 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3200 case ETHERTYPE_IPV6
:
3201 /* Check for a version number of 6. */
3202 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3206 return gen_false(); /* always false */
3213 * Raw IPv4, so no type field.
3215 if (proto
== ETHERTYPE_IP
)
3216 return gen_true(); /* always true */
3218 /* Checking for something other than IPv4; always false */
3225 * Raw IPv6, so no type field.
3228 if (proto
== ETHERTYPE_IPV6
)
3229 return gen_true(); /* always true */
3232 /* Checking for something other than IPv6; always false */
3239 case DLT_PPP_SERIAL
:
3242 * We use Ethernet protocol types inside libpcap;
3243 * map them to the corresponding PPP protocol types.
3245 proto
= ethertype_to_ppptype(proto
);
3246 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3252 * We use Ethernet protocol types inside libpcap;
3253 * map them to the corresponding PPP protocol types.
3259 * Also check for Van Jacobson-compressed IP.
3260 * XXX - do this for other forms of PPP?
3262 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3263 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3265 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3270 proto
= ethertype_to_ppptype(proto
);
3271 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3281 * For DLT_NULL, the link-layer header is a 32-bit
3282 * word containing an AF_ value in *host* byte order,
3283 * and for DLT_ENC, the link-layer header begins
3284 * with a 32-bit work containing an AF_ value in
3287 * In addition, if we're reading a saved capture file,
3288 * the host byte order in the capture may not be the
3289 * same as the host byte order on this machine.
3291 * For DLT_LOOP, the link-layer header is a 32-bit
3292 * word containing an AF_ value in *network* byte order.
3294 * XXX - AF_ values may, unfortunately, be platform-
3295 * dependent; for example, FreeBSD's AF_INET6 is 24
3296 * whilst NetBSD's and OpenBSD's is 26.
3298 * This means that, when reading a capture file, just
3299 * checking for our AF_INET6 value won't work if the
3300 * capture file came from another OS.
3309 case ETHERTYPE_IPV6
:
3316 * Not a type on which we support filtering.
3317 * XXX - support those that have AF_ values
3318 * #defined on this platform, at least?
3323 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3325 * The AF_ value is in host byte order, but
3326 * the BPF interpreter will convert it to
3327 * network byte order.
3329 * If this is a save file, and it's from a
3330 * machine with the opposite byte order to
3331 * ours, we byte-swap the AF_ value.
3333 * Then we run it through "htonl()", and
3334 * generate code to compare against the result.
3336 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3337 bpf_pcap
->sf
.swapped
)
3338 proto
= SWAPLONG(proto
);
3339 proto
= htonl(proto
);
3341 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3343 #ifdef HAVE_NET_PFVAR_H
3346 * af field is host byte order in contrast to the rest of
3349 if (proto
== ETHERTYPE_IP
)
3350 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3351 BPF_B
, (bpf_int32
)AF_INET
));
3353 else if (proto
== ETHERTYPE_IPV6
)
3354 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3355 BPF_B
, (bpf_int32
)AF_INET6
));
3361 #endif /* HAVE_NET_PFVAR_H */
3364 case DLT_ARCNET_LINUX
:
3366 * XXX should we check for first fragment if the protocol
3375 case ETHERTYPE_IPV6
:
3376 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3377 (bpf_int32
)ARCTYPE_INET6
));
3381 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3382 (bpf_int32
)ARCTYPE_IP
);
3383 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3384 (bpf_int32
)ARCTYPE_IP_OLD
);
3389 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3390 (bpf_int32
)ARCTYPE_ARP
);
3391 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3392 (bpf_int32
)ARCTYPE_ARP_OLD
);
3396 case ETHERTYPE_REVARP
:
3397 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3398 (bpf_int32
)ARCTYPE_REVARP
));
3400 case ETHERTYPE_ATALK
:
3401 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3402 (bpf_int32
)ARCTYPE_ATALK
));
3409 case ETHERTYPE_ATALK
:
3419 * XXX - assumes a 2-byte Frame Relay header with
3420 * DLCI and flags. What if the address is longer?
3426 * Check for the special NLPID for IP.
3428 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3431 case ETHERTYPE_IPV6
:
3433 * Check for the special NLPID for IPv6.
3435 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3440 * Check for several OSI protocols.
3442 * Frame Relay packets typically have an OSI
3443 * NLPID at the beginning; we check for each
3446 * What we check for is the NLPID and a frame
3447 * control field of UI, i.e. 0x03 followed
3450 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3451 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3452 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3464 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3466 case DLT_JUNIPER_MFR
:
3467 case DLT_JUNIPER_MLFR
:
3468 case DLT_JUNIPER_MLPPP
:
3469 case DLT_JUNIPER_ATM1
:
3470 case DLT_JUNIPER_ATM2
:
3471 case DLT_JUNIPER_PPPOE
:
3472 case DLT_JUNIPER_PPPOE_ATM
:
3473 case DLT_JUNIPER_GGSN
:
3474 case DLT_JUNIPER_ES
:
3475 case DLT_JUNIPER_MONITOR
:
3476 case DLT_JUNIPER_SERVICES
:
3477 case DLT_JUNIPER_ETHER
:
3478 case DLT_JUNIPER_PPP
:
3479 case DLT_JUNIPER_FRELAY
:
3480 case DLT_JUNIPER_CHDLC
:
3481 case DLT_JUNIPER_VP
:
3482 case DLT_JUNIPER_ST
:
3483 case DLT_JUNIPER_ISM
:
3484 case DLT_JUNIPER_VS
:
3485 case DLT_JUNIPER_SRX_E2E
:
3486 case DLT_JUNIPER_FIBRECHANNEL
:
3488 /* just lets verify the magic number for now -
3489 * on ATM we may have up to 6 different encapsulations on the wire
3490 * and need a lot of heuristics to figure out that the payload
3493 * FIXME encapsulation specific BPF_ filters
3495 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3498 return gen_ipnet_linktype(proto
);
3500 case DLT_LINUX_IRDA
:
3501 bpf_error("IrDA link-layer type filtering not implemented");
3504 bpf_error("DOCSIS link-layer type filtering not implemented");
3507 case DLT_MTP2_WITH_PHDR
:
3508 bpf_error("MTP2 link-layer type filtering not implemented");
3511 bpf_error("ERF link-layer type filtering not implemented");
3515 bpf_error("PFSYNC link-layer type filtering not implemented");
3518 case DLT_LINUX_LAPD
:
3519 bpf_error("LAPD link-layer type filtering not implemented");
3523 case DLT_USB_LINUX_MMAPPED
:
3524 bpf_error("USB link-layer type filtering not implemented");
3526 case DLT_BLUETOOTH_HCI_H4
:
3527 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3528 bpf_error("Bluetooth link-layer type filtering not implemented");
3531 case DLT_CAN_SOCKETCAN
:
3532 bpf_error("CAN link-layer type filtering not implemented");
3534 case DLT_IEEE802_15_4
:
3535 case DLT_IEEE802_15_4_LINUX
:
3536 case DLT_IEEE802_15_4_NONASK_PHY
:
3537 case DLT_IEEE802_15_4_NOFCS
:
3538 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3540 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3541 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3544 bpf_error("SITA link-layer type filtering not implemented");
3547 bpf_error("RAIF1 link-layer type filtering not implemented");
3550 bpf_error("IPMB link-layer type filtering not implemented");
3553 bpf_error("AX.25 link-layer type filtering not implemented");
3557 * All the types that have no encapsulation should either be
3558 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3559 * all packets are IP packets, or should be handled in some
3560 * special case, if none of them are (if some are and some
3561 * aren't, the lack of encapsulation is a problem, as we'd
3562 * have to find some other way of determining the packet type).
3564 * Therefore, if "off_linktype" is -1, there's an error.
3566 if (off_linktype
== (u_int
)-1)
3570 * Any type not handled above should always have an Ethernet
3571 * type at an offset of "off_linktype".
3573 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3577 * Check for an LLC SNAP packet with a given organization code and
3578 * protocol type; we check the entire contents of the 802.2 LLC and
3579 * snap headers, checking for DSAP and SSAP of SNAP and a control
3580 * field of 0x03 in the LLC header, and for the specified organization
3581 * code and protocol type in the SNAP header.
3583 static struct block
*
3584 gen_snap(orgcode
, ptype
)
3585 bpf_u_int32 orgcode
;
3588 u_char snapblock
[8];
3590 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3591 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3592 snapblock
[2] = 0x03; /* control = UI */
3593 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3594 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3595 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3596 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3597 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3598 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3602 * Generate code to match a particular packet type, for link-layer types
3603 * using 802.2 LLC headers.
3605 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3606 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3608 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3609 * value, if <= ETHERMTU. We use that to determine whether to
3610 * match the DSAP or both DSAP and LSAP or to check the OUI and
3611 * protocol ID in a SNAP header.
3613 static struct block
*
3614 gen_llc_linktype(proto
)
3618 * XXX - handle token-ring variable-length header.
3624 case LLCSAP_NETBEUI
:
3626 * XXX - should we check both the DSAP and the
3627 * SSAP, like this, or should we check just the
3628 * DSAP, as we do for other types <= ETHERMTU
3629 * (i.e., other SAP values)?
3631 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3632 ((proto
<< 8) | proto
));
3636 * XXX - are there ever SNAP frames for IPX on
3637 * non-Ethernet 802.x networks?
3639 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3640 (bpf_int32
)LLCSAP_IPX
);
3642 case ETHERTYPE_ATALK
:
3644 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3645 * SNAP packets with an organization code of
3646 * 0x080007 (Apple, for Appletalk) and a protocol
3647 * type of ETHERTYPE_ATALK (Appletalk).
3649 * XXX - check for an organization code of
3650 * encapsulated Ethernet as well?
3652 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3656 * XXX - we don't have to check for IPX 802.3
3657 * here, but should we check for the IPX Ethertype?
3659 if (proto
<= ETHERMTU
) {
3661 * This is an LLC SAP value, so check
3664 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3667 * This is an Ethernet type; we assume that it's
3668 * unlikely that it'll appear in the right place
3669 * at random, and therefore check only the
3670 * location that would hold the Ethernet type
3671 * in a SNAP frame with an organization code of
3672 * 0x000000 (encapsulated Ethernet).
3674 * XXX - if we were to check for the SNAP DSAP and
3675 * LSAP, as per XXX, and were also to check for an
3676 * organization code of 0x000000 (encapsulated
3677 * Ethernet), we'd do
3679 * return gen_snap(0x000000, proto);
3681 * here; for now, we don't, as per the above.
3682 * I don't know whether it's worth the extra CPU
3683 * time to do the right check or not.
3685 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3690 static struct block
*
3691 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3695 u_int src_off
, dst_off
;
3697 struct block
*b0
, *b1
;
3711 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3712 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3718 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3719 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3726 b0
= gen_linktype(proto
);
3727 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3733 static struct block
*
3734 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3735 struct in6_addr
*addr
;
3736 struct in6_addr
*mask
;
3738 u_int src_off
, dst_off
;
3740 struct block
*b0
, *b1
;
3755 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3756 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3762 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3763 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3770 /* this order is important */
3771 a
= (u_int32_t
*)addr
;
3772 m
= (u_int32_t
*)mask
;
3773 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3774 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3776 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3778 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3780 b0
= gen_linktype(proto
);
3786 static struct block
*
3787 gen_ehostop(eaddr
, dir
)
3788 register const u_char
*eaddr
;
3791 register struct block
*b0
, *b1
;
3795 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3798 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3801 b0
= gen_ehostop(eaddr
, Q_SRC
);
3802 b1
= gen_ehostop(eaddr
, Q_DST
);
3808 b0
= gen_ehostop(eaddr
, Q_SRC
);
3809 b1
= gen_ehostop(eaddr
, Q_DST
);
3814 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3818 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3822 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3826 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3830 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3834 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3842 * Like gen_ehostop, but for DLT_FDDI
3844 static struct block
*
3845 gen_fhostop(eaddr
, dir
)
3846 register const u_char
*eaddr
;
3849 struct block
*b0
, *b1
;
3854 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3856 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3861 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3863 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3867 b0
= gen_fhostop(eaddr
, Q_SRC
);
3868 b1
= gen_fhostop(eaddr
, Q_DST
);
3874 b0
= gen_fhostop(eaddr
, Q_SRC
);
3875 b1
= gen_fhostop(eaddr
, Q_DST
);
3880 bpf_error("'addr1' is only supported on 802.11");
3884 bpf_error("'addr2' is only supported on 802.11");
3888 bpf_error("'addr3' is only supported on 802.11");
3892 bpf_error("'addr4' is only supported on 802.11");
3896 bpf_error("'ra' is only supported on 802.11");
3900 bpf_error("'ta' is only supported on 802.11");
3908 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3910 static struct block
*
3911 gen_thostop(eaddr
, dir
)
3912 register const u_char
*eaddr
;
3915 register struct block
*b0
, *b1
;
3919 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3922 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3925 b0
= gen_thostop(eaddr
, Q_SRC
);
3926 b1
= gen_thostop(eaddr
, Q_DST
);
3932 b0
= gen_thostop(eaddr
, Q_SRC
);
3933 b1
= gen_thostop(eaddr
, Q_DST
);
3938 bpf_error("'addr1' is only supported on 802.11");
3942 bpf_error("'addr2' is only supported on 802.11");
3946 bpf_error("'addr3' is only supported on 802.11");
3950 bpf_error("'addr4' is only supported on 802.11");
3954 bpf_error("'ra' is only supported on 802.11");
3958 bpf_error("'ta' is only supported on 802.11");
3966 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3967 * various 802.11 + radio headers.
3969 static struct block
*
3970 gen_wlanhostop(eaddr
, dir
)
3971 register const u_char
*eaddr
;
3974 register struct block
*b0
, *b1
, *b2
;
3975 register struct slist
*s
;
3977 #ifdef ENABLE_WLAN_FILTERING_PATCH
3980 * We need to disable the optimizer because the optimizer is buggy
3981 * and wipes out some LD instructions generated by the below
3982 * code to validate the Frame Control bits
3985 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3992 * For control frames, there is no SA.
3994 * For management frames, SA is at an
3995 * offset of 10 from the beginning of
3998 * For data frames, SA is at an offset
3999 * of 10 from the beginning of the packet
4000 * if From DS is clear, at an offset of
4001 * 16 from the beginning of the packet
4002 * if From DS is set and To DS is clear,
4003 * and an offset of 24 from the beginning
4004 * of the packet if From DS is set and To DS
4009 * Generate the tests to be done for data frames
4012 * First, check for To DS set, i.e. check "link[1] & 0x01".
4014 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4015 b1
= new_block(JMP(BPF_JSET
));
4016 b1
->s
.k
= 0x01; /* To DS */
4020 * If To DS is set, the SA is at 24.
4022 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4026 * Now, check for To DS not set, i.e. check
4027 * "!(link[1] & 0x01)".
4029 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4030 b2
= new_block(JMP(BPF_JSET
));
4031 b2
->s
.k
= 0x01; /* To DS */
4036 * If To DS is not set, the SA is at 16.
4038 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4042 * Now OR together the last two checks. That gives
4043 * the complete set of checks for data frames with
4049 * Now check for From DS being set, and AND that with
4050 * the ORed-together checks.
4052 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4053 b1
= new_block(JMP(BPF_JSET
));
4054 b1
->s
.k
= 0x02; /* From DS */
4059 * Now check for data frames with From DS not set.
4061 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4062 b2
= new_block(JMP(BPF_JSET
));
4063 b2
->s
.k
= 0x02; /* From DS */
4068 * If From DS isn't set, the SA is at 10.
4070 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4074 * Now OR together the checks for data frames with
4075 * From DS not set and for data frames with From DS
4076 * set; that gives the checks done for data frames.
4081 * Now check for a data frame.
4082 * I.e, check "link[0] & 0x08".
4084 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4085 b1
= new_block(JMP(BPF_JSET
));
4090 * AND that with the checks done for data frames.
4095 * If the high-order bit of the type value is 0, this
4096 * is a management frame.
4097 * I.e, check "!(link[0] & 0x08)".
4099 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4100 b2
= new_block(JMP(BPF_JSET
));
4106 * For management frames, the SA is at 10.
4108 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4112 * OR that with the checks done for data frames.
4113 * That gives the checks done for management and
4119 * If the low-order bit of the type value is 1,
4120 * this is either a control frame or a frame
4121 * with a reserved type, and thus not a
4124 * I.e., check "!(link[0] & 0x04)".
4126 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4127 b1
= new_block(JMP(BPF_JSET
));
4133 * AND that with the checks for data and management
4143 * For control frames, there is no DA.
4145 * For management frames, DA is at an
4146 * offset of 4 from the beginning of
4149 * For data frames, DA is at an offset
4150 * of 4 from the beginning of the packet
4151 * if To DS is clear and at an offset of
4152 * 16 from the beginning of the packet
4157 * Generate the tests to be done for data frames.
4159 * First, check for To DS set, i.e. "link[1] & 0x01".
4161 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4162 b1
= new_block(JMP(BPF_JSET
));
4163 b1
->s
.k
= 0x01; /* To DS */
4167 * If To DS is set, the DA is at 16.
4169 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4173 * Now, check for To DS not set, i.e. check
4174 * "!(link[1] & 0x01)".
4176 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4177 b2
= new_block(JMP(BPF_JSET
));
4178 b2
->s
.k
= 0x01; /* To DS */
4183 * If To DS is not set, the DA is at 4.
4185 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4189 * Now OR together the last two checks. That gives
4190 * the complete set of checks for data frames.
4195 * Now check for a data frame.
4196 * I.e, check "link[0] & 0x08".
4198 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4199 b1
= new_block(JMP(BPF_JSET
));
4204 * AND that with the checks done for data frames.
4209 * If the high-order bit of the type value is 0, this
4210 * is a management frame.
4211 * I.e, check "!(link[0] & 0x08)".
4213 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4214 b2
= new_block(JMP(BPF_JSET
));
4220 * For management frames, the DA is at 4.
4222 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4226 * OR that with the checks done for data frames.
4227 * That gives the checks done for management and
4233 * If the low-order bit of the type value is 1,
4234 * this is either a control frame or a frame
4235 * with a reserved type, and thus not a
4238 * I.e., check "!(link[0] & 0x04)".
4240 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4241 b1
= new_block(JMP(BPF_JSET
));
4247 * AND that with the checks for data and management
4255 * Not present in management frames; addr1 in other
4260 * If the high-order bit of the type value is 0, this
4261 * is a management frame.
4262 * I.e, check "(link[0] & 0x08)".
4264 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4265 b1
= new_block(JMP(BPF_JSET
));
4272 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4275 * AND that with the check of addr1.
4282 * Not present in management frames; addr2, if present,
4287 * Not present in CTS or ACK control frames.
4289 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4290 IEEE80211_FC0_TYPE_MASK
);
4292 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4293 IEEE80211_FC0_SUBTYPE_MASK
);
4295 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4296 IEEE80211_FC0_SUBTYPE_MASK
);
4302 * If the high-order bit of the type value is 0, this
4303 * is a management frame.
4304 * I.e, check "(link[0] & 0x08)".
4306 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4307 b1
= new_block(JMP(BPF_JSET
));
4312 * AND that with the check for frames other than
4313 * CTS and ACK frames.
4320 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4325 * XXX - add BSSID keyword?
4328 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4332 * Not present in CTS or ACK control frames.
4334 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4335 IEEE80211_FC0_TYPE_MASK
);
4337 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4338 IEEE80211_FC0_SUBTYPE_MASK
);
4340 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4341 IEEE80211_FC0_SUBTYPE_MASK
);
4345 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4351 * Not present in control frames.
4353 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4354 IEEE80211_FC0_TYPE_MASK
);
4356 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4362 * Present only if the direction mask has both "From DS"
4363 * and "To DS" set. Neither control frames nor management
4364 * frames should have both of those set, so we don't
4365 * check the frame type.
4367 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4368 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4369 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4374 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4375 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4381 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4382 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4391 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4392 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4393 * as the RFC states.)
4395 static struct block
*
4396 gen_ipfchostop(eaddr
, dir
)
4397 register const u_char
*eaddr
;
4400 register struct block
*b0
, *b1
;
4404 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4407 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4410 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4411 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4417 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4418 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4423 bpf_error("'addr1' is only supported on 802.11");
4427 bpf_error("'addr2' is only supported on 802.11");
4431 bpf_error("'addr3' is only supported on 802.11");
4435 bpf_error("'addr4' is only supported on 802.11");
4439 bpf_error("'ra' is only supported on 802.11");
4443 bpf_error("'ta' is only supported on 802.11");
4451 * This is quite tricky because there may be pad bytes in front of the
4452 * DECNET header, and then there are two possible data packet formats that
4453 * carry both src and dst addresses, plus 5 packet types in a format that
4454 * carries only the src node, plus 2 types that use a different format and
4455 * also carry just the src node.
4459 * Instead of doing those all right, we just look for data packets with
4460 * 0 or 1 bytes of padding. If you want to look at other packets, that
4461 * will require a lot more hacking.
4463 * To add support for filtering on DECNET "areas" (network numbers)
4464 * one would want to add a "mask" argument to this routine. That would
4465 * make the filter even more inefficient, although one could be clever
4466 * and not generate masking instructions if the mask is 0xFFFF.
4468 static struct block
*
4469 gen_dnhostop(addr
, dir
)
4473 struct block
*b0
, *b1
, *b2
, *tmp
;
4474 u_int offset_lh
; /* offset if long header is received */
4475 u_int offset_sh
; /* offset if short header is received */
4480 offset_sh
= 1; /* follows flags */
4481 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4485 offset_sh
= 3; /* follows flags, dstnode */
4486 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4490 /* Inefficient because we do our Calvinball dance twice */
4491 b0
= gen_dnhostop(addr
, Q_SRC
);
4492 b1
= gen_dnhostop(addr
, Q_DST
);
4498 /* Inefficient because we do our Calvinball dance twice */
4499 b0
= gen_dnhostop(addr
, Q_SRC
);
4500 b1
= gen_dnhostop(addr
, Q_DST
);
4505 bpf_error("ISO host filtering not implemented");
4510 b0
= gen_linktype(ETHERTYPE_DN
);
4511 /* Check for pad = 1, long header case */
4512 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4513 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4514 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4515 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4517 /* Check for pad = 0, long header case */
4518 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4519 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4522 /* Check for pad = 1, short header case */
4523 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4524 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4525 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4528 /* Check for pad = 0, short header case */
4529 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4530 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4534 /* Combine with test for linktype */
4540 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4541 * test the bottom-of-stack bit, and then check the version number
4542 * field in the IP header.
4544 static struct block
*
4545 gen_mpls_linktype(proto
)
4548 struct block
*b0
, *b1
;
4553 /* match the bottom-of-stack bit */
4554 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4555 /* match the IPv4 version number */
4556 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4561 /* match the bottom-of-stack bit */
4562 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4563 /* match the IPv4 version number */
4564 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4573 static struct block
*
4574 gen_host(addr
, mask
, proto
, dir
, type
)
4581 struct block
*b0
, *b1
;
4582 const char *typestr
;
4592 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4594 * Only check for non-IPv4 addresses if we're not
4595 * checking MPLS-encapsulated packets.
4597 if (label_stack_depth
== 0) {
4598 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4600 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4606 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4609 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4612 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4615 bpf_error("'tcp' modifier applied to %s", typestr
);
4618 bpf_error("'sctp' modifier applied to %s", typestr
);
4621 bpf_error("'udp' modifier applied to %s", typestr
);
4624 bpf_error("'icmp' modifier applied to %s", typestr
);
4627 bpf_error("'igmp' modifier applied to %s", typestr
);
4630 bpf_error("'igrp' modifier applied to %s", typestr
);
4633 bpf_error("'pim' modifier applied to %s", typestr
);
4636 bpf_error("'vrrp' modifier applied to %s", typestr
);
4639 bpf_error("ATALK host filtering not implemented");
4642 bpf_error("AARP host filtering not implemented");
4645 return gen_dnhostop(addr
, dir
);
4648 bpf_error("SCA host filtering not implemented");
4651 bpf_error("LAT host filtering not implemented");
4654 bpf_error("MOPDL host filtering not implemented");
4657 bpf_error("MOPRC host filtering not implemented");
4661 bpf_error("'ip6' modifier applied to ip host");
4664 bpf_error("'icmp6' modifier applied to %s", typestr
);
4668 bpf_error("'ah' modifier applied to %s", typestr
);
4671 bpf_error("'esp' modifier applied to %s", typestr
);
4674 bpf_error("ISO host filtering not implemented");
4677 bpf_error("'esis' modifier applied to %s", typestr
);
4680 bpf_error("'isis' modifier applied to %s", typestr
);
4683 bpf_error("'clnp' modifier applied to %s", typestr
);
4686 bpf_error("'stp' modifier applied to %s", typestr
);
4689 bpf_error("IPX host filtering not implemented");
4692 bpf_error("'netbeui' modifier applied to %s", typestr
);
4695 bpf_error("'radio' modifier applied to %s", typestr
);
4704 static struct block
*
4705 gen_host6(addr
, mask
, proto
, dir
, type
)
4706 struct in6_addr
*addr
;
4707 struct in6_addr
*mask
;
4712 const char *typestr
;
4722 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4725 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4728 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4731 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4734 bpf_error("'sctp' modifier applied to %s", typestr
);
4737 bpf_error("'tcp' modifier applied to %s", typestr
);
4740 bpf_error("'udp' modifier applied to %s", typestr
);
4743 bpf_error("'icmp' modifier applied to %s", typestr
);
4746 bpf_error("'igmp' modifier applied to %s", typestr
);
4749 bpf_error("'igrp' modifier applied to %s", typestr
);
4752 bpf_error("'pim' modifier applied to %s", typestr
);
4755 bpf_error("'vrrp' modifier applied to %s", typestr
);
4758 bpf_error("ATALK host filtering not implemented");
4761 bpf_error("AARP host filtering not implemented");
4764 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4767 bpf_error("SCA host filtering not implemented");
4770 bpf_error("LAT host filtering not implemented");
4773 bpf_error("MOPDL host filtering not implemented");
4776 bpf_error("MOPRC host filtering not implemented");
4779 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4782 bpf_error("'icmp6' modifier applied to %s", typestr
);
4785 bpf_error("'ah' modifier applied to %s", typestr
);
4788 bpf_error("'esp' modifier applied to %s", typestr
);
4791 bpf_error("ISO host filtering not implemented");
4794 bpf_error("'esis' modifier applied to %s", typestr
);
4797 bpf_error("'isis' modifier applied to %s", typestr
);
4800 bpf_error("'clnp' modifier applied to %s", typestr
);
4803 bpf_error("'stp' modifier applied to %s", typestr
);
4806 bpf_error("IPX host filtering not implemented");
4809 bpf_error("'netbeui' modifier applied to %s", typestr
);
4812 bpf_error("'radio' modifier applied to %s", typestr
);
4822 static struct block
*
4823 gen_gateway(eaddr
, alist
, proto
, dir
)
4824 const u_char
*eaddr
;
4825 bpf_u_int32
**alist
;
4829 struct block
*b0
, *b1
, *tmp
;
4832 bpf_error("direction applied to 'gateway'");
4841 b0
= gen_ehostop(eaddr
, Q_OR
);
4844 b0
= gen_fhostop(eaddr
, Q_OR
);
4847 b0
= gen_thostop(eaddr
, Q_OR
);
4849 case DLT_IEEE802_11
:
4850 case DLT_PRISM_HEADER
:
4851 case DLT_IEEE802_11_RADIO_AVS
:
4852 case DLT_IEEE802_11_RADIO
:
4854 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4859 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4861 * Check that the packet doesn't begin with an
4862 * LE Control marker. (We've already generated
4865 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4870 * Now check the MAC address.
4872 b0
= gen_ehostop(eaddr
, Q_OR
);
4875 case DLT_IP_OVER_FC
:
4876 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4880 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4882 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4884 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4893 bpf_error("illegal modifier of 'gateway'");
4899 gen_proto_abbrev(proto
)
4908 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4910 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4916 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4918 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4924 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4926 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4932 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4935 #ifndef IPPROTO_IGMP
4936 #define IPPROTO_IGMP 2
4940 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4943 #ifndef IPPROTO_IGRP
4944 #define IPPROTO_IGRP 9
4947 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4951 #define IPPROTO_PIM 103
4955 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4957 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4962 #ifndef IPPROTO_VRRP
4963 #define IPPROTO_VRRP 112
4967 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4971 b1
= gen_linktype(ETHERTYPE_IP
);
4975 b1
= gen_linktype(ETHERTYPE_ARP
);
4979 b1
= gen_linktype(ETHERTYPE_REVARP
);
4983 bpf_error("link layer applied in wrong context");
4986 b1
= gen_linktype(ETHERTYPE_ATALK
);
4990 b1
= gen_linktype(ETHERTYPE_AARP
);
4994 b1
= gen_linktype(ETHERTYPE_DN
);
4998 b1
= gen_linktype(ETHERTYPE_SCA
);
5002 b1
= gen_linktype(ETHERTYPE_LAT
);
5006 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5010 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5015 b1
= gen_linktype(ETHERTYPE_IPV6
);
5018 #ifndef IPPROTO_ICMPV6
5019 #define IPPROTO_ICMPV6 58
5022 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5027 #define IPPROTO_AH 51
5030 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5032 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5038 #define IPPROTO_ESP 50
5041 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5043 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5049 b1
= gen_linktype(LLCSAP_ISONS
);
5053 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5057 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5060 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5061 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5062 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5064 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5066 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5068 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5072 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5073 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5074 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5076 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5078 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5080 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5084 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5085 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5086 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5088 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5093 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5094 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5099 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5100 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5102 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5104 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5109 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5110 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5115 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5116 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5121 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5125 b1
= gen_linktype(LLCSAP_8021D
);
5129 b1
= gen_linktype(LLCSAP_IPX
);
5133 b1
= gen_linktype(LLCSAP_NETBEUI
);
5137 bpf_error("'radio' is not a valid protocol type");
5145 static struct block
*
5152 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5153 b
= new_block(JMP(BPF_JSET
));
5162 * Generate a comparison to a port value in the transport-layer header
5163 * at the specified offset from the beginning of that header.
5165 * XXX - this handles a variable-length prefix preceding the link-layer
5166 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5167 * variable-length link-layer headers (such as Token Ring or 802.11
5170 static struct block
*
5171 gen_portatom(off
, v
)
5175 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5179 static struct block
*
5180 gen_portatom6(off
, v
)
5184 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5189 gen_portop(port
, proto
, dir
)
5190 int port
, proto
, dir
;
5192 struct block
*b0
, *b1
, *tmp
;
5194 /* ip proto 'proto' */
5195 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5201 b1
= gen_portatom(0, (bpf_int32
)port
);
5205 b1
= gen_portatom(2, (bpf_int32
)port
);
5210 tmp
= gen_portatom(0, (bpf_int32
)port
);
5211 b1
= gen_portatom(2, (bpf_int32
)port
);
5216 tmp
= gen_portatom(0, (bpf_int32
)port
);
5217 b1
= gen_portatom(2, (bpf_int32
)port
);
5229 static struct block
*
5230 gen_port(port
, ip_proto
, dir
)
5235 struct block
*b0
, *b1
, *tmp
;
5240 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5241 * not LLC encapsulation with LLCSAP_IP.
5243 * For IEEE 802 networks - which includes 802.5 token ring
5244 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5245 * says that SNAP encapsulation is used, not LLC encapsulation
5248 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5249 * RFC 2225 say that SNAP encapsulation is used, not LLC
5250 * encapsulation with LLCSAP_IP.
5252 * So we always check for ETHERTYPE_IP.
5254 b0
= gen_linktype(ETHERTYPE_IP
);
5260 b1
= gen_portop(port
, ip_proto
, dir
);
5264 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5265 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5267 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5280 gen_portop6(port
, proto
, dir
)
5281 int port
, proto
, dir
;
5283 struct block
*b0
, *b1
, *tmp
;
5285 /* ip6 proto 'proto' */
5286 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5290 b1
= gen_portatom6(0, (bpf_int32
)port
);
5294 b1
= gen_portatom6(2, (bpf_int32
)port
);
5299 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5300 b1
= gen_portatom6(2, (bpf_int32
)port
);
5305 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5306 b1
= gen_portatom6(2, (bpf_int32
)port
);
5318 static struct block
*
5319 gen_port6(port
, ip_proto
, dir
)
5324 struct block
*b0
, *b1
, *tmp
;
5326 /* link proto ip6 */
5327 b0
= gen_linktype(ETHERTYPE_IPV6
);
5333 b1
= gen_portop6(port
, ip_proto
, dir
);
5337 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5338 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5340 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5352 /* gen_portrange code */
5353 static struct block
*
5354 gen_portrangeatom(off
, v1
, v2
)
5358 struct block
*b1
, *b2
;
5362 * Reverse the order of the ports, so v1 is the lower one.
5371 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5372 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5380 gen_portrangeop(port1
, port2
, proto
, dir
)
5385 struct block
*b0
, *b1
, *tmp
;
5387 /* ip proto 'proto' */
5388 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5394 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5398 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5403 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5404 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5409 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5410 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5422 static struct block
*
5423 gen_portrange(port1
, port2
, ip_proto
, dir
)
5428 struct block
*b0
, *b1
, *tmp
;
5431 b0
= gen_linktype(ETHERTYPE_IP
);
5437 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5441 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5442 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5444 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5456 static struct block
*
5457 gen_portrangeatom6(off
, v1
, v2
)
5461 struct block
*b1
, *b2
;
5465 * Reverse the order of the ports, so v1 is the lower one.
5474 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5475 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5483 gen_portrangeop6(port1
, port2
, proto
, dir
)
5488 struct block
*b0
, *b1
, *tmp
;
5490 /* ip6 proto 'proto' */
5491 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5495 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5499 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5504 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5505 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5510 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5511 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5523 static struct block
*
5524 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5529 struct block
*b0
, *b1
, *tmp
;
5531 /* link proto ip6 */
5532 b0
= gen_linktype(ETHERTYPE_IPV6
);
5538 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5542 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5543 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5545 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5558 lookup_proto(name
, proto
)
5559 register const char *name
;
5569 v
= pcap_nametoproto(name
);
5570 if (v
== PROTO_UNDEF
)
5571 bpf_error("unknown ip proto '%s'", name
);
5575 /* XXX should look up h/w protocol type based on linktype */
5576 v
= pcap_nametoeproto(name
);
5577 if (v
== PROTO_UNDEF
) {
5578 v
= pcap_nametollc(name
);
5579 if (v
== PROTO_UNDEF
)
5580 bpf_error("unknown ether proto '%s'", name
);
5585 if (strcmp(name
, "esis") == 0)
5587 else if (strcmp(name
, "isis") == 0)
5589 else if (strcmp(name
, "clnp") == 0)
5592 bpf_error("unknown osi proto '%s'", name
);
5612 static struct block
*
5613 gen_protochain(v
, proto
, dir
)
5618 #ifdef NO_PROTOCHAIN
5619 return gen_proto(v
, proto
, dir
);
5621 struct block
*b0
, *b
;
5622 struct slist
*s
[100];
5623 int fix2
, fix3
, fix4
, fix5
;
5624 int ahcheck
, again
, end
;
5626 int reg2
= alloc_reg();
5628 memset(s
, 0, sizeof(s
));
5629 fix2
= fix3
= fix4
= fix5
= 0;
5636 b0
= gen_protochain(v
, Q_IP
, dir
);
5637 b
= gen_protochain(v
, Q_IPV6
, dir
);
5641 bpf_error("bad protocol applied for 'protochain'");
5646 * We don't handle variable-length prefixes before the link-layer
5647 * header, or variable-length link-layer headers, here yet.
5648 * We might want to add BPF instructions to do the protochain
5649 * work, to simplify that and, on platforms that have a BPF
5650 * interpreter with the new instructions, let the filtering
5651 * be done in the kernel. (We already require a modified BPF
5652 * engine to do the protochain stuff, to support backward
5653 * branches, and backward branch support is unlikely to appear
5654 * in kernel BPF engines.)
5658 case DLT_IEEE802_11
:
5659 case DLT_PRISM_HEADER
:
5660 case DLT_IEEE802_11_RADIO_AVS
:
5661 case DLT_IEEE802_11_RADIO
:
5663 bpf_error("'protochain' not supported with 802.11");
5666 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5669 * s[0] is a dummy entry to protect other BPF insn from damage
5670 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5671 * hard to find interdependency made by jump table fixup.
5674 s
[i
] = new_stmt(0); /*dummy*/
5679 b0
= gen_linktype(ETHERTYPE_IP
);
5682 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5683 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5685 /* X = ip->ip_hl << 2 */
5686 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5687 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5692 b0
= gen_linktype(ETHERTYPE_IPV6
);
5694 /* A = ip6->ip_nxt */
5695 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5696 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5698 /* X = sizeof(struct ip6_hdr) */
5699 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5705 bpf_error("unsupported proto to gen_protochain");
5709 /* again: if (A == v) goto end; else fall through; */
5711 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5713 s
[i
]->s
.jt
= NULL
; /*later*/
5714 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5718 #ifndef IPPROTO_NONE
5719 #define IPPROTO_NONE 59
5721 /* if (A == IPPROTO_NONE) goto end */
5722 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5723 s
[i
]->s
.jt
= NULL
; /*later*/
5724 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5725 s
[i
]->s
.k
= IPPROTO_NONE
;
5726 s
[fix5
]->s
.jf
= s
[i
];
5731 if (proto
== Q_IPV6
) {
5732 int v6start
, v6end
, v6advance
, j
;
5735 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5736 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_HOPOPTS
;
5740 s
[fix2
]->s
.jf
= s
[i
];
5742 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5743 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5744 s
[i
]->s
.jt
= NULL
; /*later*/
5745 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5746 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5748 /* if (A == IPPROTO_ROUTING) goto v6advance */
5749 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5750 s
[i
]->s
.jt
= NULL
; /*later*/
5751 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5752 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5754 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5755 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5756 s
[i
]->s
.jt
= NULL
; /*later*/
5757 s
[i
]->s
.jf
= NULL
; /*later*/
5758 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5769 * X = X + (P[X + 1] + 1) * 8;
5772 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5774 /* A = P[X + packet head] */
5775 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5776 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5779 s
[i
] = new_stmt(BPF_ST
);
5783 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5786 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5790 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5792 /* A = P[X + packet head]; */
5793 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5794 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5797 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5801 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5805 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5808 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5812 /* goto again; (must use BPF_JA for backward jump) */
5813 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5814 s
[i
]->s
.k
= again
- i
- 1;
5815 s
[i
- 1]->s
.jf
= s
[i
];
5819 for (j
= v6start
; j
<= v6end
; j
++)
5820 s
[j
]->s
.jt
= s
[v6advance
];
5825 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5827 s
[fix2
]->s
.jf
= s
[i
];
5833 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5834 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5835 s
[i
]->s
.jt
= NULL
; /*later*/
5836 s
[i
]->s
.jf
= NULL
; /*later*/
5837 s
[i
]->s
.k
= IPPROTO_AH
;
5839 s
[fix3
]->s
.jf
= s
[ahcheck
];
5846 * X = X + (P[X + 1] + 2) * 4;
5849 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5851 /* A = P[X + packet head]; */
5852 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5853 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5856 s
[i
] = new_stmt(BPF_ST
);
5860 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5863 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5867 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5869 /* A = P[X + packet head] */
5870 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5871 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5874 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5878 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5882 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5885 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5889 /* goto again; (must use BPF_JA for backward jump) */
5890 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5891 s
[i
]->s
.k
= again
- i
- 1;
5896 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5898 s
[fix2
]->s
.jt
= s
[end
];
5899 s
[fix4
]->s
.jf
= s
[end
];
5900 s
[fix5
]->s
.jt
= s
[end
];
5907 for (i
= 0; i
< max
- 1; i
++)
5908 s
[i
]->next
= s
[i
+ 1];
5909 s
[max
- 1]->next
= NULL
;
5914 b
= new_block(JMP(BPF_JEQ
));
5915 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5925 static struct block
*
5926 gen_check_802_11_data_frame()
5929 struct block
*b0
, *b1
;
5932 * A data frame has the 0x08 bit (b3) in the frame control field set
5933 * and the 0x04 bit (b2) clear.
5935 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5936 b0
= new_block(JMP(BPF_JSET
));
5940 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5941 b1
= new_block(JMP(BPF_JSET
));
5952 * Generate code that checks whether the packet is a packet for protocol
5953 * <proto> and whether the type field in that protocol's header has
5954 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5955 * IP packet and checks the protocol number in the IP header against <v>.
5957 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5958 * against Q_IP and Q_IPV6.
5960 static struct block
*
5961 gen_proto(v
, proto
, dir
)
5966 struct block
*b0
, *b1
;
5968 if (dir
!= Q_DEFAULT
)
5969 bpf_error("direction applied to 'proto'");
5974 b0
= gen_proto(v
, Q_IP
, dir
);
5975 b1
= gen_proto(v
, Q_IPV6
, dir
);
5983 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5984 * not LLC encapsulation with LLCSAP_IP.
5986 * For IEEE 802 networks - which includes 802.5 token ring
5987 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5988 * says that SNAP encapsulation is used, not LLC encapsulation
5991 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5992 * RFC 2225 say that SNAP encapsulation is used, not LLC
5993 * encapsulation with LLCSAP_IP.
5995 * So we always check for ETHERTYPE_IP.
5997 b0
= gen_linktype(ETHERTYPE_IP
);
5999 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6001 b1
= gen_protochain(v
, Q_IP
);
6011 * Frame Relay packets typically have an OSI
6012 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6013 * generates code to check for all the OSI
6014 * NLPIDs, so calling it and then adding a check
6015 * for the particular NLPID for which we're
6016 * looking is bogus, as we can just check for
6019 * What we check for is the NLPID and a frame
6020 * control field value of UI, i.e. 0x03 followed
6023 * XXX - assumes a 2-byte Frame Relay header with
6024 * DLCI and flags. What if the address is longer?
6026 * XXX - what about SNAP-encapsulated frames?
6028 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6034 * Cisco uses an Ethertype lookalike - for OSI,
6037 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6038 /* OSI in C-HDLC is stuffed with a fudge byte */
6039 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6044 b0
= gen_linktype(LLCSAP_ISONS
);
6045 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6051 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6053 * 4 is the offset of the PDU type relative to the IS-IS
6056 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6061 bpf_error("arp does not encapsulate another protocol");
6065 bpf_error("rarp does not encapsulate another protocol");
6069 bpf_error("atalk encapsulation is not specifiable");
6073 bpf_error("decnet encapsulation is not specifiable");
6077 bpf_error("sca does not encapsulate another protocol");
6081 bpf_error("lat does not encapsulate another protocol");
6085 bpf_error("moprc does not encapsulate another protocol");
6089 bpf_error("mopdl does not encapsulate another protocol");
6093 return gen_linktype(v
);
6096 bpf_error("'udp proto' is bogus");
6100 bpf_error("'tcp proto' is bogus");
6104 bpf_error("'sctp proto' is bogus");
6108 bpf_error("'icmp proto' is bogus");
6112 bpf_error("'igmp proto' is bogus");
6116 bpf_error("'igrp proto' is bogus");
6120 bpf_error("'pim proto' is bogus");
6124 bpf_error("'vrrp proto' is bogus");
6129 b0
= gen_linktype(ETHERTYPE_IPV6
);
6131 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6133 b1
= gen_protochain(v
, Q_IPV6
);
6139 bpf_error("'icmp6 proto' is bogus");
6143 bpf_error("'ah proto' is bogus");
6146 bpf_error("'ah proto' is bogus");
6149 bpf_error("'stp proto' is bogus");
6152 bpf_error("'ipx proto' is bogus");
6155 bpf_error("'netbeui proto' is bogus");
6158 bpf_error("'radio proto' is bogus");
6169 register const char *name
;
6172 int proto
= q
.proto
;
6176 bpf_u_int32 mask
, addr
;
6178 bpf_u_int32
**alist
;
6181 struct sockaddr_in
*sin4
;
6182 struct sockaddr_in6
*sin6
;
6183 struct addrinfo
*res
, *res0
;
6184 struct in6_addr mask128
;
6186 struct block
*b
, *tmp
;
6187 int port
, real_proto
;
6193 addr
= pcap_nametonetaddr(name
);
6195 bpf_error("unknown network '%s'", name
);
6196 /* Left justify network addr and calculate its network mask */
6198 while (addr
&& (addr
& 0xff000000) == 0) {
6202 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6206 if (proto
== Q_LINK
) {
6210 eaddr
= pcap_ether_hostton(name
);
6213 "unknown ether host '%s'", name
);
6214 b
= gen_ehostop(eaddr
, dir
);
6219 eaddr
= pcap_ether_hostton(name
);
6222 "unknown FDDI host '%s'", name
);
6223 b
= gen_fhostop(eaddr
, dir
);
6228 eaddr
= pcap_ether_hostton(name
);
6231 "unknown token ring host '%s'", name
);
6232 b
= gen_thostop(eaddr
, dir
);
6236 case DLT_IEEE802_11
:
6237 case DLT_PRISM_HEADER
:
6238 case DLT_IEEE802_11_RADIO_AVS
:
6239 case DLT_IEEE802_11_RADIO
:
6241 eaddr
= pcap_ether_hostton(name
);
6244 "unknown 802.11 host '%s'", name
);
6245 b
= gen_wlanhostop(eaddr
, dir
);
6249 case DLT_IP_OVER_FC
:
6250 eaddr
= pcap_ether_hostton(name
);
6253 "unknown Fibre Channel host '%s'", name
);
6254 b
= gen_ipfchostop(eaddr
, dir
);
6263 * Check that the packet doesn't begin
6264 * with an LE Control marker. (We've
6265 * already generated a test for LANE.)
6267 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6271 eaddr
= pcap_ether_hostton(name
);
6274 "unknown ether host '%s'", name
);
6275 b
= gen_ehostop(eaddr
, dir
);
6281 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6282 } else if (proto
== Q_DECNET
) {
6283 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6285 * I don't think DECNET hosts can be multihomed, so
6286 * there is no need to build up a list of addresses
6288 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6291 alist
= pcap_nametoaddr(name
);
6292 if (alist
== NULL
|| *alist
== NULL
)
6293 bpf_error("unknown host '%s'", name
);
6295 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6297 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6299 tmp
= gen_host(**alist
++, 0xffffffff,
6300 tproto
, dir
, q
.addr
);
6306 memset(&mask128
, 0xff, sizeof(mask128
));
6307 res0
= res
= pcap_nametoaddrinfo(name
);
6309 bpf_error("unknown host '%s'", name
);
6312 tproto
= tproto6
= proto
;
6313 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6317 for (res
= res0
; res
; res
= res
->ai_next
) {
6318 switch (res
->ai_family
) {
6320 if (tproto
== Q_IPV6
)
6323 sin4
= (struct sockaddr_in
*)
6325 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6326 0xffffffff, tproto
, dir
, q
.addr
);
6329 if (tproto6
== Q_IP
)
6332 sin6
= (struct sockaddr_in6
*)
6334 tmp
= gen_host6(&sin6
->sin6_addr
,
6335 &mask128
, tproto6
, dir
, q
.addr
);
6347 bpf_error("unknown host '%s'%s", name
,
6348 (proto
== Q_DEFAULT
)
6350 : " for specified address family");
6357 if (proto
!= Q_DEFAULT
&&
6358 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6359 bpf_error("illegal qualifier of 'port'");
6360 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6361 bpf_error("unknown port '%s'", name
);
6362 if (proto
== Q_UDP
) {
6363 if (real_proto
== IPPROTO_TCP
)
6364 bpf_error("port '%s' is tcp", name
);
6365 else if (real_proto
== IPPROTO_SCTP
)
6366 bpf_error("port '%s' is sctp", name
);
6368 /* override PROTO_UNDEF */
6369 real_proto
= IPPROTO_UDP
;
6371 if (proto
== Q_TCP
) {
6372 if (real_proto
== IPPROTO_UDP
)
6373 bpf_error("port '%s' is udp", name
);
6375 else if (real_proto
== IPPROTO_SCTP
)
6376 bpf_error("port '%s' is sctp", name
);
6378 /* override PROTO_UNDEF */
6379 real_proto
= IPPROTO_TCP
;
6381 if (proto
== Q_SCTP
) {
6382 if (real_proto
== IPPROTO_UDP
)
6383 bpf_error("port '%s' is udp", name
);
6385 else if (real_proto
== IPPROTO_TCP
)
6386 bpf_error("port '%s' is tcp", name
);
6388 /* override PROTO_UNDEF */
6389 real_proto
= IPPROTO_SCTP
;
6392 bpf_error("illegal port number %d < 0", port
);
6394 bpf_error("illegal port number %d > 65535", port
);
6396 return gen_port(port
, real_proto
, dir
);
6398 b
= gen_port(port
, real_proto
, dir
);
6399 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6404 if (proto
!= Q_DEFAULT
&&
6405 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6406 bpf_error("illegal qualifier of 'portrange'");
6407 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6408 bpf_error("unknown port in range '%s'", name
);
6409 if (proto
== Q_UDP
) {
6410 if (real_proto
== IPPROTO_TCP
)
6411 bpf_error("port in range '%s' is tcp", name
);
6412 else if (real_proto
== IPPROTO_SCTP
)
6413 bpf_error("port in range '%s' is sctp", name
);
6415 /* override PROTO_UNDEF */
6416 real_proto
= IPPROTO_UDP
;
6418 if (proto
== Q_TCP
) {
6419 if (real_proto
== IPPROTO_UDP
)
6420 bpf_error("port in range '%s' is udp", name
);
6421 else if (real_proto
== IPPROTO_SCTP
)
6422 bpf_error("port in range '%s' is sctp", name
);
6424 /* override PROTO_UNDEF */
6425 real_proto
= IPPROTO_TCP
;
6427 if (proto
== Q_SCTP
) {
6428 if (real_proto
== IPPROTO_UDP
)
6429 bpf_error("port in range '%s' is udp", name
);
6430 else if (real_proto
== IPPROTO_TCP
)
6431 bpf_error("port in range '%s' is tcp", name
);
6433 /* override PROTO_UNDEF */
6434 real_proto
= IPPROTO_SCTP
;
6437 bpf_error("illegal port number %d < 0", port1
);
6439 bpf_error("illegal port number %d > 65535", port1
);
6441 bpf_error("illegal port number %d < 0", port2
);
6443 bpf_error("illegal port number %d > 65535", port2
);
6446 return gen_portrange(port1
, port2
, real_proto
, dir
);
6448 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6449 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6455 eaddr
= pcap_ether_hostton(name
);
6457 bpf_error("unknown ether host: %s", name
);
6459 alist
= pcap_nametoaddr(name
);
6460 if (alist
== NULL
|| *alist
== NULL
)
6461 bpf_error("unknown host '%s'", name
);
6462 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6466 bpf_error("'gateway' not supported in this configuration");
6470 real_proto
= lookup_proto(name
, proto
);
6471 if (real_proto
>= 0)
6472 return gen_proto(real_proto
, proto
, dir
);
6474 bpf_error("unknown protocol: %s", name
);
6477 real_proto
= lookup_proto(name
, proto
);
6478 if (real_proto
>= 0)
6479 return gen_protochain(real_proto
, proto
, dir
);
6481 bpf_error("unknown protocol: %s", name
);
6492 gen_mcode(s1
, s2
, masklen
, q
)
6493 register const char *s1
, *s2
;
6494 register int masklen
;
6497 register int nlen
, mlen
;
6500 nlen
= __pcap_atoin(s1
, &n
);
6501 /* Promote short ipaddr */
6505 mlen
= __pcap_atoin(s2
, &m
);
6506 /* Promote short ipaddr */
6509 bpf_error("non-network bits set in \"%s mask %s\"",
6512 /* Convert mask len to mask */
6514 bpf_error("mask length must be <= 32");
6517 * X << 32 is not guaranteed by C to be 0; it's
6522 m
= 0xffffffff << (32 - masklen
);
6524 bpf_error("non-network bits set in \"%s/%d\"",
6531 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6534 bpf_error("Mask syntax for networks only");
6543 register const char *s
;
6548 int proto
= q
.proto
;
6554 else if (q
.proto
== Q_DECNET
)
6555 vlen
= __pcap_atodn(s
, &v
);
6557 vlen
= __pcap_atoin(s
, &v
);
6564 if (proto
== Q_DECNET
)
6565 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6566 else if (proto
== Q_LINK
) {
6567 bpf_error("illegal link layer address");
6570 if (s
== NULL
&& q
.addr
== Q_NET
) {
6571 /* Promote short net number */
6572 while (v
&& (v
& 0xff000000) == 0) {
6577 /* Promote short ipaddr */
6581 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6586 proto
= IPPROTO_UDP
;
6587 else if (proto
== Q_TCP
)
6588 proto
= IPPROTO_TCP
;
6589 else if (proto
== Q_SCTP
)
6590 proto
= IPPROTO_SCTP
;
6591 else if (proto
== Q_DEFAULT
)
6592 proto
= PROTO_UNDEF
;
6594 bpf_error("illegal qualifier of 'port'");
6597 bpf_error("illegal port number %u > 65535", v
);
6600 return gen_port((int)v
, proto
, dir
);
6604 b
= gen_port((int)v
, proto
, dir
);
6605 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6612 proto
= IPPROTO_UDP
;
6613 else if (proto
== Q_TCP
)
6614 proto
= IPPROTO_TCP
;
6615 else if (proto
== Q_SCTP
)
6616 proto
= IPPROTO_SCTP
;
6617 else if (proto
== Q_DEFAULT
)
6618 proto
= PROTO_UNDEF
;
6620 bpf_error("illegal qualifier of 'portrange'");
6623 bpf_error("illegal port number %u > 65535", v
);
6626 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6630 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6631 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6637 bpf_error("'gateway' requires a name");
6641 return gen_proto((int)v
, proto
, dir
);
6644 return gen_protochain((int)v
, proto
, dir
);
6659 gen_mcode6(s1
, s2
, masklen
, q
)
6660 register const char *s1
, *s2
;
6661 register int masklen
;
6664 struct addrinfo
*res
;
6665 struct in6_addr
*addr
;
6666 struct in6_addr mask
;
6671 bpf_error("no mask %s supported", s2
);
6673 res
= pcap_nametoaddrinfo(s1
);
6675 bpf_error("invalid ip6 address %s", s1
);
6678 bpf_error("%s resolved to multiple address", s1
);
6679 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6681 if (sizeof(mask
) * 8 < masklen
)
6682 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6683 memset(&mask
, 0, sizeof(mask
));
6684 memset(&mask
, 0xff, masklen
/ 8);
6686 mask
.s6_addr
[masklen
/ 8] =
6687 (0xff << (8 - masklen
% 8)) & 0xff;
6690 a
= (u_int32_t
*)addr
;
6691 m
= (u_int32_t
*)&mask
;
6692 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6693 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6694 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6702 bpf_error("Mask syntax for networks only");
6706 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6712 bpf_error("invalid qualifier against IPv6 address");
6721 register const u_char
*eaddr
;
6724 struct block
*b
, *tmp
;
6726 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6729 return gen_ehostop(eaddr
, (int)q
.dir
);
6731 return gen_fhostop(eaddr
, (int)q
.dir
);
6733 return gen_thostop(eaddr
, (int)q
.dir
);
6734 case DLT_IEEE802_11
:
6735 case DLT_PRISM_HEADER
:
6736 case DLT_IEEE802_11_RADIO_AVS
:
6737 case DLT_IEEE802_11_RADIO
:
6739 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6743 * Check that the packet doesn't begin with an
6744 * LE Control marker. (We've already generated
6747 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6752 * Now check the MAC address.
6754 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6759 case DLT_IP_OVER_FC
:
6760 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6762 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6766 bpf_error("ethernet address used in non-ether expression");
6773 struct slist
*s0
, *s1
;
6776 * This is definitely not the best way to do this, but the
6777 * lists will rarely get long.
6784 static struct slist
*
6790 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6795 static struct slist
*
6801 s
= new_stmt(BPF_LD
|BPF_MEM
);
6807 * Modify "index" to use the value stored into its register as an
6808 * offset relative to the beginning of the header for the protocol
6809 * "proto", and allocate a register and put an item "size" bytes long
6810 * (1, 2, or 4) at that offset into that register, making it the register
6814 gen_load(proto
, inst
, size
)
6819 struct slist
*s
, *tmp
;
6821 int regno
= alloc_reg();
6823 free_reg(inst
->regno
);
6827 bpf_error("data size must be 1, 2, or 4");
6843 bpf_error("unsupported index operation");
6847 * The offset is relative to the beginning of the packet
6848 * data, if we have a radio header. (If we don't, this
6851 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6852 linktype
!= DLT_IEEE802_11_RADIO
&&
6853 linktype
!= DLT_PRISM_HEADER
)
6854 bpf_error("radio information not present in capture");
6857 * Load into the X register the offset computed into the
6858 * register specified by "index".
6860 s
= xfer_to_x(inst
);
6863 * Load the item at that offset.
6865 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6867 sappend(inst
->s
, s
);
6872 * The offset is relative to the beginning of
6873 * the link-layer header.
6875 * XXX - what about ATM LANE? Should the index be
6876 * relative to the beginning of the AAL5 frame, so
6877 * that 0 refers to the beginning of the LE Control
6878 * field, or relative to the beginning of the LAN
6879 * frame, so that 0 refers, for Ethernet LANE, to
6880 * the beginning of the destination address?
6882 s
= gen_llprefixlen();
6885 * If "s" is non-null, it has code to arrange that the
6886 * X register contains the length of the prefix preceding
6887 * the link-layer header. Add to it the offset computed
6888 * into the register specified by "index", and move that
6889 * into the X register. Otherwise, just load into the X
6890 * register the offset computed into the register specified
6894 sappend(s
, xfer_to_a(inst
));
6895 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6896 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6898 s
= xfer_to_x(inst
);
6901 * Load the item at the sum of the offset we've put in the
6902 * X register and the offset of the start of the link
6903 * layer header (which is 0 if the radio header is
6904 * variable-length; that header length is what we put
6905 * into the X register and then added to the index).
6907 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6910 sappend(inst
->s
, s
);
6926 * The offset is relative to the beginning of
6927 * the network-layer header.
6928 * XXX - are there any cases where we want
6931 s
= gen_off_macpl();
6934 * If "s" is non-null, it has code to arrange that the
6935 * X register contains the offset of the MAC-layer
6936 * payload. Add to it the offset computed into the
6937 * register specified by "index", and move that into
6938 * the X register. Otherwise, just load into the X
6939 * register the offset computed into the register specified
6943 sappend(s
, xfer_to_a(inst
));
6944 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6945 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6947 s
= xfer_to_x(inst
);
6950 * Load the item at the sum of the offset we've put in the
6951 * X register, the offset of the start of the network
6952 * layer header from the beginning of the MAC-layer
6953 * payload, and the purported offset of the start of the
6954 * MAC-layer payload (which might be 0 if there's a
6955 * variable-length prefix before the link-layer header
6956 * or the link-layer header itself is variable-length;
6957 * the variable-length offset of the start of the
6958 * MAC-layer payload is what we put into the X register
6959 * and then added to the index).
6961 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6962 tmp
->s
.k
= off_macpl
+ off_nl
;
6964 sappend(inst
->s
, s
);
6967 * Do the computation only if the packet contains
6968 * the protocol in question.
6970 b
= gen_proto_abbrev(proto
);
6972 gen_and(inst
->b
, b
);
6985 * The offset is relative to the beginning of
6986 * the transport-layer header.
6988 * Load the X register with the length of the IPv4 header
6989 * (plus the offset of the link-layer header, if it's
6990 * a variable-length header), in bytes.
6992 * XXX - are there any cases where we want
6994 * XXX - we should, if we're built with
6995 * IPv6 support, generate code to load either
6996 * IPv4, IPv6, or both, as appropriate.
6998 s
= gen_loadx_iphdrlen();
7001 * The X register now contains the sum of the length
7002 * of any variable-length header preceding the link-layer
7003 * header, any variable-length link-layer header, and the
7004 * length of the network-layer header.
7006 * Load into the A register the offset relative to
7007 * the beginning of the transport layer header,
7008 * add the X register to that, move that to the
7009 * X register, and load with an offset from the
7010 * X register equal to the offset of the network
7011 * layer header relative to the beginning of
7012 * the MAC-layer payload plus the fixed-length
7013 * portion of the offset of the MAC-layer payload
7014 * from the beginning of the raw packet data.
7016 sappend(s
, xfer_to_a(inst
));
7017 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7018 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7019 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7020 tmp
->s
.k
= off_macpl
+ off_nl
;
7021 sappend(inst
->s
, s
);
7024 * Do the computation only if the packet contains
7025 * the protocol in question - which is true only
7026 * if this is an IP datagram and is the first or
7027 * only fragment of that datagram.
7029 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7031 gen_and(inst
->b
, b
);
7033 gen_and(gen_proto_abbrev(Q_IP
), b
);
7039 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7043 inst
->regno
= regno
;
7044 s
= new_stmt(BPF_ST
);
7046 sappend(inst
->s
, s
);
7052 gen_relation(code
, a0
, a1
, reversed
)
7054 struct arth
*a0
, *a1
;
7057 struct slist
*s0
, *s1
, *s2
;
7058 struct block
*b
, *tmp
;
7062 if (code
== BPF_JEQ
) {
7063 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7064 b
= new_block(JMP(code
));
7068 b
= new_block(BPF_JMP
|code
|BPF_X
);
7074 sappend(a0
->s
, a1
->s
);
7078 free_reg(a0
->regno
);
7079 free_reg(a1
->regno
);
7081 /* 'and' together protocol checks */
7084 gen_and(a0
->b
, tmp
= a1
->b
);
7100 int regno
= alloc_reg();
7101 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7104 s
= new_stmt(BPF_LD
|BPF_LEN
);
7105 s
->next
= new_stmt(BPF_ST
);
7106 s
->next
->s
.k
= regno
;
7121 a
= (struct arth
*)newchunk(sizeof(*a
));
7125 s
= new_stmt(BPF_LD
|BPF_IMM
);
7127 s
->next
= new_stmt(BPF_ST
);
7143 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7146 s
= new_stmt(BPF_ST
);
7154 gen_arth(code
, a0
, a1
)
7156 struct arth
*a0
, *a1
;
7158 struct slist
*s0
, *s1
, *s2
;
7162 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7167 sappend(a0
->s
, a1
->s
);
7169 free_reg(a0
->regno
);
7170 free_reg(a1
->regno
);
7172 s0
= new_stmt(BPF_ST
);
7173 a0
->regno
= s0
->s
.k
= alloc_reg();
7180 * Here we handle simple allocation of the scratch registers.
7181 * If too many registers are alloc'd, the allocator punts.
7183 static int regused
[BPF_MEMWORDS
];
7187 * Initialize the table of used registers and the current register.
7193 memset(regused
, 0, sizeof regused
);
7197 * Return the next free register.
7202 int n
= BPF_MEMWORDS
;
7205 if (regused
[curreg
])
7206 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7208 regused
[curreg
] = 1;
7212 bpf_error("too many registers needed to evaluate expression");
7218 * Return a register to the table so it can
7228 static struct block
*
7235 s
= new_stmt(BPF_LD
|BPF_LEN
);
7236 b
= new_block(JMP(jmp
));
7247 return gen_len(BPF_JGE
, n
);
7251 * Actually, this is less than or equal.
7259 b
= gen_len(BPF_JGT
, n
);
7266 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7267 * the beginning of the link-layer header.
7268 * XXX - that means you can't test values in the radiotap header, but
7269 * as that header is difficult if not impossible to parse generally
7270 * without a loop, that might not be a severe problem. A new keyword
7271 * "radio" could be added for that, although what you'd really want
7272 * would be a way of testing particular radio header values, which
7273 * would generate code appropriate to the radio header in question.
7276 gen_byteop(op
, idx
, val
)
7287 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7290 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7294 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7298 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7302 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7306 b
= new_block(JMP(BPF_JEQ
));
7313 static u_char abroadcast
[] = { 0x0 };
7316 gen_broadcast(proto
)
7319 bpf_u_int32 hostmask
;
7320 struct block
*b0
, *b1
, *b2
;
7321 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7329 case DLT_ARCNET_LINUX
:
7330 return gen_ahostop(abroadcast
, Q_DST
);
7332 return gen_ehostop(ebroadcast
, Q_DST
);
7334 return gen_fhostop(ebroadcast
, Q_DST
);
7336 return gen_thostop(ebroadcast
, Q_DST
);
7337 case DLT_IEEE802_11
:
7338 case DLT_PRISM_HEADER
:
7339 case DLT_IEEE802_11_RADIO_AVS
:
7340 case DLT_IEEE802_11_RADIO
:
7342 return gen_wlanhostop(ebroadcast
, Q_DST
);
7343 case DLT_IP_OVER_FC
:
7344 return gen_ipfchostop(ebroadcast
, Q_DST
);
7348 * Check that the packet doesn't begin with an
7349 * LE Control marker. (We've already generated
7352 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7357 * Now check the MAC address.
7359 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7365 bpf_error("not a broadcast link");
7371 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7372 * as an indication that we don't know the netmask, and fail
7375 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7376 bpf_error("netmask not known, so 'ip broadcast' not supported");
7377 b0
= gen_linktype(ETHERTYPE_IP
);
7378 hostmask
= ~netmask
;
7379 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7380 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7381 (bpf_int32
)(~0 & hostmask
), hostmask
);
7386 bpf_error("only link-layer/IP broadcast filters supported");
7392 * Generate code to test the low-order bit of a MAC address (that's
7393 * the bottom bit of the *first* byte).
7395 static struct block
*
7396 gen_mac_multicast(offset
)
7399 register struct block
*b0
;
7400 register struct slist
*s
;
7402 /* link[offset] & 1 != 0 */
7403 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7404 b0
= new_block(JMP(BPF_JSET
));
7411 gen_multicast(proto
)
7414 register struct block
*b0
, *b1
, *b2
;
7415 register struct slist
*s
;
7423 case DLT_ARCNET_LINUX
:
7424 /* all ARCnet multicasts use the same address */
7425 return gen_ahostop(abroadcast
, Q_DST
);
7427 /* ether[0] & 1 != 0 */
7428 return gen_mac_multicast(0);
7431 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7433 * XXX - was that referring to bit-order issues?
7435 /* fddi[1] & 1 != 0 */
7436 return gen_mac_multicast(1);
7438 /* tr[2] & 1 != 0 */
7439 return gen_mac_multicast(2);
7440 case DLT_IEEE802_11
:
7441 case DLT_PRISM_HEADER
:
7442 case DLT_IEEE802_11_RADIO_AVS
:
7443 case DLT_IEEE802_11_RADIO
:
7448 * For control frames, there is no DA.
7450 * For management frames, DA is at an
7451 * offset of 4 from the beginning of
7454 * For data frames, DA is at an offset
7455 * of 4 from the beginning of the packet
7456 * if To DS is clear and at an offset of
7457 * 16 from the beginning of the packet
7462 * Generate the tests to be done for data frames.
7464 * First, check for To DS set, i.e. "link[1] & 0x01".
7466 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7467 b1
= new_block(JMP(BPF_JSET
));
7468 b1
->s
.k
= 0x01; /* To DS */
7472 * If To DS is set, the DA is at 16.
7474 b0
= gen_mac_multicast(16);
7478 * Now, check for To DS not set, i.e. check
7479 * "!(link[1] & 0x01)".
7481 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7482 b2
= new_block(JMP(BPF_JSET
));
7483 b2
->s
.k
= 0x01; /* To DS */
7488 * If To DS is not set, the DA is at 4.
7490 b1
= gen_mac_multicast(4);
7494 * Now OR together the last two checks. That gives
7495 * the complete set of checks for data frames.
7500 * Now check for a data frame.
7501 * I.e, check "link[0] & 0x08".
7503 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7504 b1
= new_block(JMP(BPF_JSET
));
7509 * AND that with the checks done for data frames.
7514 * If the high-order bit of the type value is 0, this
7515 * is a management frame.
7516 * I.e, check "!(link[0] & 0x08)".
7518 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7519 b2
= new_block(JMP(BPF_JSET
));
7525 * For management frames, the DA is at 4.
7527 b1
= gen_mac_multicast(4);
7531 * OR that with the checks done for data frames.
7532 * That gives the checks done for management and
7538 * If the low-order bit of the type value is 1,
7539 * this is either a control frame or a frame
7540 * with a reserved type, and thus not a
7543 * I.e., check "!(link[0] & 0x04)".
7545 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7546 b1
= new_block(JMP(BPF_JSET
));
7552 * AND that with the checks for data and management
7557 case DLT_IP_OVER_FC
:
7558 b0
= gen_mac_multicast(2);
7563 * Check that the packet doesn't begin with an
7564 * LE Control marker. (We've already generated
7567 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7571 /* ether[off_mac] & 1 != 0 */
7572 b0
= gen_mac_multicast(off_mac
);
7580 /* Link not known to support multicasts */
7584 b0
= gen_linktype(ETHERTYPE_IP
);
7585 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7591 b0
= gen_linktype(ETHERTYPE_IPV6
);
7592 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7597 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7603 * generate command for inbound/outbound. It's here so we can
7604 * make it link-type specific. 'dir' = 0 implies "inbound",
7605 * = 1 implies "outbound".
7611 register struct block
*b0
;
7614 * Only some data link types support inbound/outbound qualifiers.
7618 b0
= gen_relation(BPF_JEQ
,
7619 gen_load(Q_LINK
, gen_loadi(0), 1),
7626 /* match outgoing packets */
7627 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7629 /* match incoming packets */
7630 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7637 * Match packets sent by this machine.
7639 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7642 * Match packets sent to this machine.
7643 * (No broadcast or multicast packets, or
7644 * packets sent to some other machine and
7645 * received promiscuously.)
7647 * XXX - packets sent to other machines probably
7648 * shouldn't be matched, but what about broadcast
7649 * or multicast packets we received?
7651 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7655 #ifdef HAVE_NET_PFVAR_H
7657 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7658 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7664 /* match outgoing packets */
7665 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7667 /* match incoming packets */
7668 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7672 case DLT_JUNIPER_MFR
:
7673 case DLT_JUNIPER_MLFR
:
7674 case DLT_JUNIPER_MLPPP
:
7675 case DLT_JUNIPER_ATM1
:
7676 case DLT_JUNIPER_ATM2
:
7677 case DLT_JUNIPER_PPPOE
:
7678 case DLT_JUNIPER_PPPOE_ATM
:
7679 case DLT_JUNIPER_GGSN
:
7680 case DLT_JUNIPER_ES
:
7681 case DLT_JUNIPER_MONITOR
:
7682 case DLT_JUNIPER_SERVICES
:
7683 case DLT_JUNIPER_ETHER
:
7684 case DLT_JUNIPER_PPP
:
7685 case DLT_JUNIPER_FRELAY
:
7686 case DLT_JUNIPER_CHDLC
:
7687 case DLT_JUNIPER_VP
:
7688 case DLT_JUNIPER_ST
:
7689 case DLT_JUNIPER_ISM
:
7690 case DLT_JUNIPER_VS
:
7691 case DLT_JUNIPER_SRX_E2E
:
7692 case DLT_JUNIPER_FIBRECHANNEL
:
7694 /* juniper flags (including direction) are stored
7695 * the byte after the 3-byte magic number */
7697 /* match outgoing packets */
7698 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7700 /* match incoming packets */
7701 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7706 bpf_error("inbound/outbound not supported on linktype %d",
7714 #ifdef HAVE_NET_PFVAR_H
7715 /* PF firewall log matched interface */
7717 gen_pf_ifname(const char *ifname
)
7722 if (linktype
!= DLT_PFLOG
) {
7723 bpf_error("ifname supported only on PF linktype");
7726 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7727 off
= offsetof(struct pfloghdr
, ifname
);
7728 if (strlen(ifname
) >= len
) {
7729 bpf_error("ifname interface names can only be %d characters",
7733 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7737 /* PF firewall log ruleset name */
7739 gen_pf_ruleset(char *ruleset
)
7743 if (linktype
!= DLT_PFLOG
) {
7744 bpf_error("ruleset supported only on PF linktype");
7748 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7749 bpf_error("ruleset names can only be %ld characters",
7750 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7754 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7755 strlen(ruleset
), (const u_char
*)ruleset
);
7759 /* PF firewall log rule number */
7765 if (linktype
!= DLT_PFLOG
) {
7766 bpf_error("rnr supported only on PF linktype");
7770 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7775 /* PF firewall log sub-rule number */
7777 gen_pf_srnr(int srnr
)
7781 if (linktype
!= DLT_PFLOG
) {
7782 bpf_error("srnr supported only on PF linktype");
7786 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7791 /* PF firewall log reason code */
7793 gen_pf_reason(int reason
)
7797 if (linktype
!= DLT_PFLOG
) {
7798 bpf_error("reason supported only on PF linktype");
7802 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7807 /* PF firewall log action */
7809 gen_pf_action(int action
)
7813 if (linktype
!= DLT_PFLOG
) {
7814 bpf_error("action supported only on PF linktype");
7818 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7822 #else /* !HAVE_NET_PFVAR_H */
7824 gen_pf_ifname(const char *ifname
)
7826 bpf_error("libpcap was compiled without pf support");
7832 gen_pf_ruleset(char *ruleset
)
7834 bpf_error("libpcap was compiled on a machine without pf support");
7842 bpf_error("libpcap was compiled on a machine without pf support");
7848 gen_pf_srnr(int srnr
)
7850 bpf_error("libpcap was compiled on a machine without pf support");
7856 gen_pf_reason(int reason
)
7858 bpf_error("libpcap was compiled on a machine without pf support");
7864 gen_pf_action(int action
)
7866 bpf_error("libpcap was compiled on a machine without pf support");
7870 #endif /* HAVE_NET_PFVAR_H */
7872 /* IEEE 802.11 wireless header */
7874 gen_p80211_type(int type
, int mask
)
7880 case DLT_IEEE802_11
:
7881 case DLT_PRISM_HEADER
:
7882 case DLT_IEEE802_11_RADIO_AVS
:
7883 case DLT_IEEE802_11_RADIO
:
7884 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7889 bpf_error("802.11 link-layer types supported only on 802.11");
7897 gen_p80211_fcdir(int fcdir
)
7903 case DLT_IEEE802_11
:
7904 case DLT_PRISM_HEADER
:
7905 case DLT_IEEE802_11_RADIO_AVS
:
7906 case DLT_IEEE802_11_RADIO
:
7910 bpf_error("frame direction supported only with 802.11 headers");
7914 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7915 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7922 register const u_char
*eaddr
;
7928 case DLT_ARCNET_LINUX
:
7929 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7931 return (gen_ahostop(eaddr
, (int)q
.dir
));
7933 bpf_error("ARCnet address used in non-arc expression");
7939 bpf_error("aid supported only on ARCnet");
7942 bpf_error("ARCnet address used in non-arc expression");
7947 static struct block
*
7948 gen_ahostop(eaddr
, dir
)
7949 register const u_char
*eaddr
;
7952 register struct block
*b0
, *b1
;
7955 /* src comes first, different from Ethernet */
7957 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7960 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7963 b0
= gen_ahostop(eaddr
, Q_SRC
);
7964 b1
= gen_ahostop(eaddr
, Q_DST
);
7970 b0
= gen_ahostop(eaddr
, Q_SRC
);
7971 b1
= gen_ahostop(eaddr
, Q_DST
);
7976 bpf_error("'addr1' is only supported on 802.11");
7980 bpf_error("'addr2' is only supported on 802.11");
7984 bpf_error("'addr3' is only supported on 802.11");
7988 bpf_error("'addr4' is only supported on 802.11");
7992 bpf_error("'ra' is only supported on 802.11");
7996 bpf_error("'ta' is only supported on 802.11");
8004 * support IEEE 802.1Q VLAN trunk over ethernet
8010 struct block
*b0
, *b1
;
8012 /* can't check for VLAN-encapsulated packets inside MPLS */
8013 if (label_stack_depth
> 0)
8014 bpf_error("no VLAN match after MPLS");
8017 * Check for a VLAN packet, and then change the offsets to point
8018 * to the type and data fields within the VLAN packet. Just
8019 * increment the offsets, so that we can support a hierarchy, e.g.
8020 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8023 * XXX - this is a bit of a kludge. If we were to split the
8024 * compiler into a parser that parses an expression and
8025 * generates an expression tree, and a code generator that
8026 * takes an expression tree (which could come from our
8027 * parser or from some other parser) and generates BPF code,
8028 * we could perhaps make the offsets parameters of routines
8029 * and, in the handler for an "AND" node, pass to subnodes
8030 * other than the VLAN node the adjusted offsets.
8032 * This would mean that "vlan" would, instead of changing the
8033 * behavior of *all* tests after it, change only the behavior
8034 * of tests ANDed with it. That would change the documented
8035 * semantics of "vlan", which might break some expressions.
8036 * However, it would mean that "(vlan and ip) or ip" would check
8037 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8038 * checking only for VLAN-encapsulated IP, so that could still
8039 * be considered worth doing; it wouldn't break expressions
8040 * that are of the form "vlan and ..." or "vlan N and ...",
8041 * which I suspect are the most common expressions involving
8042 * "vlan". "vlan or ..." doesn't necessarily do what the user
8043 * would really want, now, as all the "or ..." tests would
8044 * be done assuming a VLAN, even though the "or" could be viewed
8045 * as meaning "or, if this isn't a VLAN packet...".
8052 /* check for VLAN */
8053 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8054 (bpf_int32
)ETHERTYPE_8021Q
);
8056 /* If a specific VLAN is requested, check VLAN id */
8057 if (vlan_num
>= 0) {
8058 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
8059 (bpf_int32
)vlan_num
, 0x0fff);
8073 bpf_error("no VLAN support for data link type %d",
8088 struct block
*b0
,*b1
;
8091 * Change the offsets to point to the type and data fields within
8092 * the MPLS packet. Just increment the offsets, so that we
8093 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8094 * capture packets with an outer label of 100000 and an inner
8097 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8101 if (label_stack_depth
> 0) {
8102 /* just match the bottom-of-stack bit clear */
8103 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
8106 * Indicate that we're checking MPLS-encapsulated headers,
8107 * to make sure higher level code generators don't try to
8108 * match against IP-related protocols such as Q_ARP, Q_RARP
8113 case DLT_C_HDLC
: /* fall through */
8115 b0
= gen_linktype(ETHERTYPE_MPLS
);
8119 b0
= gen_linktype(PPP_MPLS_UCAST
);
8122 /* FIXME add other DLT_s ...
8123 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8124 * leave it for now */
8127 bpf_error("no MPLS support for data link type %d",
8135 /* If a specific MPLS label is requested, check it */
8136 if (label_num
>= 0) {
8137 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8138 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8139 0xfffff000); /* only compare the first 20 bits */
8146 label_stack_depth
++;
8151 * Support PPPOE discovery and session.
8156 /* check for PPPoE discovery */
8157 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8166 * Test against the PPPoE session link-layer type.
8168 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8171 * Change the offsets to point to the type and data fields within
8172 * the PPP packet, and note that this is PPPoE rather than
8175 * XXX - this is a bit of a kludge. If we were to split the
8176 * compiler into a parser that parses an expression and
8177 * generates an expression tree, and a code generator that
8178 * takes an expression tree (which could come from our
8179 * parser or from some other parser) and generates BPF code,
8180 * we could perhaps make the offsets parameters of routines
8181 * and, in the handler for an "AND" node, pass to subnodes
8182 * other than the PPPoE node the adjusted offsets.
8184 * This would mean that "pppoes" would, instead of changing the
8185 * behavior of *all* tests after it, change only the behavior
8186 * of tests ANDed with it. That would change the documented
8187 * semantics of "pppoes", which might break some expressions.
8188 * However, it would mean that "(pppoes and ip) or ip" would check
8189 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8190 * checking only for VLAN-encapsulated IP, so that could still
8191 * be considered worth doing; it wouldn't break expressions
8192 * that are of the form "pppoes and ..." which I suspect are the
8193 * most common expressions involving "pppoes". "pppoes or ..."
8194 * doesn't necessarily do what the user would really want, now,
8195 * as all the "or ..." tests would be done assuming PPPoE, even
8196 * though the "or" could be viewed as meaning "or, if this isn't
8197 * a PPPoE packet...".
8199 orig_linktype
= off_linktype
; /* save original values */
8204 * The "network-layer" protocol is PPPoE, which has a 6-byte
8205 * PPPoE header, followed by a PPP packet.
8207 * There is no HDLC encapsulation for the PPP packet (it's
8208 * encapsulated in PPPoES instead), so the link-layer type
8209 * starts at the first byte of the PPP packet. For PPPoE,
8210 * that offset is relative to the beginning of the total
8211 * link-layer payload, including any 802.2 LLC header, so
8212 * it's 6 bytes past off_nl.
8214 off_linktype
= off_nl
+ 6;
8217 * The network-layer offsets are relative to the beginning
8218 * of the MAC-layer payload; that's past the 6-byte
8219 * PPPoE header and the 2-byte PPP header.
8222 off_nl_nosnap
= 6+2;
8228 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8240 bpf_error("'vpi' supported only on raw ATM");
8241 if (off_vpi
== (u_int
)-1)
8243 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8249 bpf_error("'vci' supported only on raw ATM");
8250 if (off_vci
== (u_int
)-1)
8252 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8257 if (off_proto
== (u_int
)-1)
8258 abort(); /* XXX - this isn't on FreeBSD */
8259 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8264 if (off_payload
== (u_int
)-1)
8266 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8267 0xffffffff, jtype
, reverse
, jvalue
);
8272 bpf_error("'callref' supported only on raw ATM");
8273 if (off_proto
== (u_int
)-1)
8275 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8276 jtype
, reverse
, jvalue
);
8286 gen_atmtype_abbrev(type
)
8289 struct block
*b0
, *b1
;
8294 /* Get all packets in Meta signalling Circuit */
8296 bpf_error("'metac' supported only on raw ATM");
8297 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8298 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8303 /* Get all packets in Broadcast Circuit*/
8305 bpf_error("'bcc' supported only on raw ATM");
8306 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8307 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8312 /* Get all cells in Segment OAM F4 circuit*/
8314 bpf_error("'oam4sc' supported only on raw ATM");
8315 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8316 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8321 /* Get all cells in End-to-End OAM F4 Circuit*/
8323 bpf_error("'oam4ec' supported only on raw ATM");
8324 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8325 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8330 /* Get all packets in connection Signalling Circuit */
8332 bpf_error("'sc' supported only on raw ATM");
8333 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8334 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8339 /* Get all packets in ILMI Circuit */
8341 bpf_error("'ilmic' supported only on raw ATM");
8342 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8343 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8348 /* Get all LANE packets */
8350 bpf_error("'lane' supported only on raw ATM");
8351 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8354 * Arrange that all subsequent tests assume LANE
8355 * rather than LLC-encapsulated packets, and set
8356 * the offsets appropriately for LANE-encapsulated
8359 * "off_mac" is the offset of the Ethernet header,
8360 * which is 2 bytes past the ATM pseudo-header
8361 * (skipping the pseudo-header and 2-byte LE Client
8362 * field). The other offsets are Ethernet offsets
8363 * relative to "off_mac".
8366 off_mac
= off_payload
+ 2; /* MAC header */
8367 off_linktype
= off_mac
+ 12;
8368 off_macpl
= off_mac
+ 14; /* Ethernet */
8369 off_nl
= 0; /* Ethernet II */
8370 off_nl_nosnap
= 3; /* 802.3+802.2 */
8374 /* Get all LLC-encapsulated packets */
8376 bpf_error("'llc' supported only on raw ATM");
8377 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8388 * Filtering for MTP2 messages based on li value
8389 * FISU, length is null
8390 * LSSU, length is 1 or 2
8391 * MSU, length is 3 or more
8394 gen_mtp2type_abbrev(type
)
8397 struct block
*b0
, *b1
;
8402 if ( (linktype
!= DLT_MTP2
) &&
8403 (linktype
!= DLT_ERF
) &&
8404 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8405 bpf_error("'fisu' supported only on MTP2");
8406 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8407 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8411 if ( (linktype
!= DLT_MTP2
) &&
8412 (linktype
!= DLT_ERF
) &&
8413 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8414 bpf_error("'lssu' supported only on MTP2");
8415 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8416 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8421 if ( (linktype
!= DLT_MTP2
) &&
8422 (linktype
!= DLT_ERF
) &&
8423 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8424 bpf_error("'msu' supported only on MTP2");
8425 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8435 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8442 bpf_u_int32 val1
, val2
, val3
;
8444 switch (mtp3field
) {
8447 if (off_sio
== (u_int
)-1)
8448 bpf_error("'sio' supported only on SS7");
8449 /* sio coded on 1 byte so max value 255 */
8451 bpf_error("sio value %u too big; max value = 255",
8453 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8454 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8458 if (off_opc
== (u_int
)-1)
8459 bpf_error("'opc' supported only on SS7");
8460 /* opc coded on 14 bits so max value 16383 */
8462 bpf_error("opc value %u too big; max value = 16383",
8464 /* the following instructions are made to convert jvalue
8465 * to the form used to write opc in an ss7 message*/
8466 val1
= jvalue
& 0x00003c00;
8468 val2
= jvalue
& 0x000003fc;
8470 val3
= jvalue
& 0x00000003;
8472 jvalue
= val1
+ val2
+ val3
;
8473 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8474 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8478 if (off_dpc
== (u_int
)-1)
8479 bpf_error("'dpc' supported only on SS7");
8480 /* dpc coded on 14 bits so max value 16383 */
8482 bpf_error("dpc value %u too big; max value = 16383",
8484 /* the following instructions are made to convert jvalue
8485 * to the forme used to write dpc in an ss7 message*/
8486 val1
= jvalue
& 0x000000ff;
8488 val2
= jvalue
& 0x00003f00;
8490 jvalue
= val1
+ val2
;
8491 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8492 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8496 if (off_sls
== (u_int
)-1)
8497 bpf_error("'sls' supported only on SS7");
8498 /* sls coded on 4 bits so max value 15 */
8500 bpf_error("sls value %u too big; max value = 15",
8502 /* the following instruction is made to convert jvalue
8503 * to the forme used to write sls in an ss7 message*/
8504 jvalue
= jvalue
<< 4;
8505 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8506 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8515 static struct block
*
8516 gen_msg_abbrev(type
)
8522 * Q.2931 signalling protocol messages for handling virtual circuits
8523 * establishment and teardown
8528 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8532 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8536 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8540 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8544 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8547 case A_RELEASE_DONE
:
8548 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8558 gen_atmmulti_abbrev(type
)
8561 struct block
*b0
, *b1
;
8567 bpf_error("'oam' supported only on raw ATM");
8568 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8573 bpf_error("'oamf4' supported only on raw ATM");
8575 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8576 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8578 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8584 * Get Q.2931 signalling messages for switched
8585 * virtual connection
8588 bpf_error("'connectmsg' supported only on raw ATM");
8589 b0
= gen_msg_abbrev(A_SETUP
);
8590 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8592 b0
= gen_msg_abbrev(A_CONNECT
);
8594 b0
= gen_msg_abbrev(A_CONNECTACK
);
8596 b0
= gen_msg_abbrev(A_RELEASE
);
8598 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8600 b0
= gen_atmtype_abbrev(A_SC
);
8606 bpf_error("'metaconnect' supported only on raw ATM");
8607 b0
= gen_msg_abbrev(A_SETUP
);
8608 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8610 b0
= gen_msg_abbrev(A_CONNECT
);
8612 b0
= gen_msg_abbrev(A_RELEASE
);
8614 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8616 b0
= gen_atmtype_abbrev(A_METAC
);