1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 * XXX - why was this included even on UNIX?
56 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
76 #include "ethertype.h"
80 #include "ieee80211.h"
82 #include "sunatmpos.h"
85 #include "pcap/ipnet.h"
87 #ifdef HAVE_NET_PFVAR_H
88 #include <sys/socket.h>
90 #include <net/pfvar.h>
91 #include <net/if_pflog.h>
94 #define offsetof(s, e) ((size_t)&((s *)0)->e)
98 #include <netdb.h> /* for "struct addrinfo" */
101 #include <pcap/namedb.h>
103 #define ETHERMTU 1500
106 #define IPPROTO_SCTP 132
109 #ifdef HAVE_OS_PROTO_H
110 #include "os-proto.h"
113 #define JMP(c) ((c)|BPF_JMP|BPF_K)
116 static jmp_buf top_ctx
;
117 static pcap_t
*bpf_pcap
;
119 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
121 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
123 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
128 static int pcap_fddipad
;
133 bpf_error(const char *fmt
, ...)
138 if (bpf_pcap
!= NULL
)
139 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
146 static void init_linktype(pcap_t
*);
148 static void init_regs(void);
149 static int alloc_reg(void);
150 static void free_reg(int);
152 static struct block
*root
;
155 * Value passed to gen_load_a() to indicate what the offset argument
159 OR_PACKET
, /* relative to the beginning of the packet */
160 OR_LINK
, /* relative to the beginning of the link-layer header */
161 OR_MACPL
, /* relative to the end of the MAC-layer header */
162 OR_NET
, /* relative to the network-layer header */
163 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
164 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
165 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
170 * As errors are handled by a longjmp, anything allocated must be freed
171 * in the longjmp handler, so it must be reachable from that handler.
172 * One thing that's allocated is the result of pcap_nametoaddrinfo();
173 * it must be freed with freeaddrinfo(). This variable points to any
174 * addrinfo structure that would need to be freed.
176 static struct addrinfo
*ai
;
180 * We divy out chunks of memory rather than call malloc each time so
181 * we don't have to worry about leaking memory. It's probably
182 * not a big deal if all this memory was wasted but if this ever
183 * goes into a library that would probably not be a good idea.
185 * XXX - this *is* in a library....
188 #define CHUNK0SIZE 1024
194 static struct chunk chunks
[NCHUNKS
];
195 static int cur_chunk
;
197 static void *newchunk(u_int
);
198 static void freechunks(void);
199 static inline struct block
*new_block(int);
200 static inline struct slist
*new_stmt(int);
201 static struct block
*gen_retblk(int);
202 static inline void syntax(void);
204 static void backpatch(struct block
*, struct block
*);
205 static void merge(struct block
*, struct block
*);
206 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
207 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
208 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
209 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
210 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
211 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
213 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
214 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
215 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
216 static struct slist
*gen_load_llrel(u_int
, u_int
);
217 static struct slist
*gen_load_macplrel(u_int
, u_int
);
218 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
219 static struct slist
*gen_loadx_iphdrlen(void);
220 static struct block
*gen_uncond(int);
221 static inline struct block
*gen_true(void);
222 static inline struct block
*gen_false(void);
223 static struct block
*gen_ether_linktype(int);
224 static struct block
*gen_ipnet_linktype(int);
225 static struct block
*gen_linux_sll_linktype(int);
226 static struct slist
*gen_load_prism_llprefixlen(void);
227 static struct slist
*gen_load_avs_llprefixlen(void);
228 static struct slist
*gen_load_radiotap_llprefixlen(void);
229 static struct slist
*gen_load_ppi_llprefixlen(void);
230 static void insert_compute_vloffsets(struct block
*);
231 static struct slist
*gen_llprefixlen(void);
232 static struct slist
*gen_off_macpl(void);
233 static int ethertype_to_ppptype(int);
234 static struct block
*gen_linktype(int);
235 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
236 static struct block
*gen_llc_linktype(int);
237 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
239 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
241 static struct block
*gen_ahostop(const u_char
*, int);
242 static struct block
*gen_ehostop(const u_char
*, int);
243 static struct block
*gen_fhostop(const u_char
*, int);
244 static struct block
*gen_thostop(const u_char
*, int);
245 static struct block
*gen_wlanhostop(const u_char
*, int);
246 static struct block
*gen_ipfchostop(const u_char
*, int);
247 static struct block
*gen_dnhostop(bpf_u_int32
, int);
248 static struct block
*gen_mpls_linktype(int);
249 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
251 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
254 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
256 static struct block
*gen_ipfrag(void);
257 static struct block
*gen_portatom(int, bpf_int32
);
258 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
260 static struct block
*gen_portatom6(int, bpf_int32
);
261 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
263 struct block
*gen_portop(int, int, int);
264 static struct block
*gen_port(int, int, int);
265 struct block
*gen_portrangeop(int, int, int, int);
266 static struct block
*gen_portrange(int, int, int, int);
268 struct block
*gen_portop6(int, int, int);
269 static struct block
*gen_port6(int, int, int);
270 struct block
*gen_portrangeop6(int, int, int, int);
271 static struct block
*gen_portrange6(int, int, int, int);
273 static int lookup_proto(const char *, int);
274 static struct block
*gen_protochain(int, int, int);
275 static struct block
*gen_proto(int, int, int);
276 static struct slist
*xfer_to_x(struct arth
*);
277 static struct slist
*xfer_to_a(struct arth
*);
278 static struct block
*gen_mac_multicast(int);
279 static struct block
*gen_len(int, int);
280 static struct block
*gen_check_802_11_data_frame(void);
282 static struct block
*gen_ppi_dlt_check(void);
283 static struct block
*gen_msg_abbrev(int type
);
294 /* XXX Round up to nearest long. */
295 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
297 /* XXX Round up to structure boundary. */
301 cp
= &chunks
[cur_chunk
];
302 if (n
> cp
->n_left
) {
303 ++cp
, k
= ++cur_chunk
;
305 bpf_error("out of memory");
306 size
= CHUNK0SIZE
<< k
;
307 cp
->m
= (void *)malloc(size
);
309 bpf_error("out of memory");
310 memset((char *)cp
->m
, 0, size
);
313 bpf_error("out of memory");
316 return (void *)((char *)cp
->m
+ cp
->n_left
);
325 for (i
= 0; i
< NCHUNKS
; ++i
)
326 if (chunks
[i
].m
!= NULL
) {
333 * A strdup whose allocations are freed after code generation is over.
337 register const char *s
;
339 int n
= strlen(s
) + 1;
340 char *cp
= newchunk(n
);
346 static inline struct block
*
352 p
= (struct block
*)newchunk(sizeof(*p
));
359 static inline struct slist
*
365 p
= (struct slist
*)newchunk(sizeof(*p
));
371 static struct block
*
375 struct block
*b
= new_block(BPF_RET
|BPF_K
);
384 bpf_error("syntax error in filter expression");
387 static bpf_u_int32 netmask
;
392 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
393 const char *buf
, int optimize
, bpf_u_int32 mask
);
396 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
397 const char *buf
, int optimize
, bpf_u_int32 mask
)
401 EnterCriticalSection(&g_PcapCompileCriticalSection
);
403 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
405 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
411 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
412 const char *buf
, int optimize
, bpf_u_int32 mask
)
415 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
416 const char *buf
, int optimize
, bpf_u_int32 mask
)
420 const char * volatile xbuf
= buf
;
428 if (setjmp(top_ctx
)) {
442 snaplen
= pcap_snapshot(p
);
444 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
445 "snaplen of 0 rejects all packets");
449 lex_init(xbuf
? xbuf
: "");
457 root
= gen_retblk(snaplen
);
459 if (optimize
&& !no_optimize
) {
462 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
463 bpf_error("expression rejects all packets");
465 program
->bf_insns
= icode_to_fcode(root
, &len
);
466 program
->bf_len
= len
;
474 * entry point for using the compiler with no pcap open
475 * pass in all the stuff that is needed explicitly instead.
478 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
479 struct bpf_program
*program
,
480 const char *buf
, int optimize
, bpf_u_int32 mask
)
485 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
488 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
494 * Clean up a "struct bpf_program" by freeing all the memory allocated
498 pcap_freecode(struct bpf_program
*program
)
501 if (program
->bf_insns
!= NULL
) {
502 free((char *)program
->bf_insns
);
503 program
->bf_insns
= NULL
;
508 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
509 * which of the jt and jf fields has been resolved and which is a pointer
510 * back to another unresolved block (or nil). At least one of the fields
511 * in each block is already resolved.
514 backpatch(list
, target
)
515 struct block
*list
, *target
;
532 * Merge the lists in b0 and b1, using the 'sense' field to indicate
533 * which of jt and jf is the link.
537 struct block
*b0
, *b1
;
539 register struct block
**p
= &b0
;
541 /* Find end of list. */
543 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
545 /* Concatenate the lists. */
553 struct block
*ppi_dlt_check
;
556 * Insert before the statements of the first (root) block any
557 * statements needed to load the lengths of any variable-length
558 * headers into registers.
560 * XXX - a fancier strategy would be to insert those before the
561 * statements of all blocks that use those lengths and that
562 * have no predecessors that use them, so that we only compute
563 * the lengths if we need them. There might be even better
564 * approaches than that.
566 * However, those strategies would be more complicated, and
567 * as we don't generate code to compute a length if the
568 * program has no tests that use the length, and as most
569 * tests will probably use those lengths, we would just
570 * postpone computing the lengths so that it's not done
571 * for tests that fail early, and it's not clear that's
574 insert_compute_vloffsets(p
->head
);
577 * For DLT_PPI captures, generate a check of the per-packet
578 * DLT value to make sure it's DLT_IEEE802_11.
580 ppi_dlt_check
= gen_ppi_dlt_check();
581 if (ppi_dlt_check
!= NULL
)
582 gen_and(ppi_dlt_check
, p
);
584 backpatch(p
, gen_retblk(snaplen
));
585 p
->sense
= !p
->sense
;
586 backpatch(p
, gen_retblk(0));
592 struct block
*b0
, *b1
;
594 backpatch(b0
, b1
->head
);
595 b0
->sense
= !b0
->sense
;
596 b1
->sense
= !b1
->sense
;
598 b1
->sense
= !b1
->sense
;
604 struct block
*b0
, *b1
;
606 b0
->sense
= !b0
->sense
;
607 backpatch(b0
, b1
->head
);
608 b0
->sense
= !b0
->sense
;
617 b
->sense
= !b
->sense
;
620 static struct block
*
621 gen_cmp(offrel
, offset
, size
, v
)
622 enum e_offrel offrel
;
626 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
629 static struct block
*
630 gen_cmp_gt(offrel
, offset
, size
, v
)
631 enum e_offrel offrel
;
635 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
638 static struct block
*
639 gen_cmp_ge(offrel
, offset
, size
, v
)
640 enum e_offrel offrel
;
644 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
647 static struct block
*
648 gen_cmp_lt(offrel
, offset
, size
, v
)
649 enum e_offrel offrel
;
653 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
656 static struct block
*
657 gen_cmp_le(offrel
, offset
, size
, v
)
658 enum e_offrel offrel
;
662 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
665 static struct block
*
666 gen_mcmp(offrel
, offset
, size
, v
, mask
)
667 enum e_offrel offrel
;
672 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
675 static struct block
*
676 gen_bcmp(offrel
, offset
, size
, v
)
677 enum e_offrel offrel
;
678 register u_int offset
, size
;
679 register const u_char
*v
;
681 register struct block
*b
, *tmp
;
685 register const u_char
*p
= &v
[size
- 4];
686 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
687 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
689 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
696 register const u_char
*p
= &v
[size
- 2];
697 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
699 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
706 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
715 * AND the field of size "size" at offset "offset" relative to the header
716 * specified by "offrel" with "mask", and compare it with the value "v"
717 * with the test specified by "jtype"; if "reverse" is true, the test
718 * should test the opposite of "jtype".
720 static struct block
*
721 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
722 enum e_offrel offrel
;
724 bpf_u_int32 offset
, size
, mask
, jtype
;
727 struct slist
*s
, *s2
;
730 s
= gen_load_a(offrel
, offset
, size
);
732 if (mask
!= 0xffffffff) {
733 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
738 b
= new_block(JMP(jtype
));
741 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
747 * Various code constructs need to know the layout of the data link
748 * layer. These variables give the necessary offsets from the beginning
749 * of the packet data.
753 * This is the offset of the beginning of the link-layer header from
754 * the beginning of the raw packet data.
756 * It's usually 0, except for 802.11 with a fixed-length radio header.
757 * (For 802.11 with a variable-length radio header, we have to generate
758 * code to compute that offset; off_ll is 0 in that case.)
763 * If there's a variable-length header preceding the link-layer header,
764 * "reg_off_ll" is the register number for a register containing the
765 * length of that header, and therefore the offset of the link-layer
766 * header from the beginning of the raw packet data. Otherwise,
767 * "reg_off_ll" is -1.
769 static int reg_off_ll
;
772 * This is the offset of the beginning of the MAC-layer header from
773 * the beginning of the link-layer header.
774 * It's usually 0, except for ATM LANE, where it's the offset, relative
775 * to the beginning of the raw packet data, of the Ethernet header.
777 static u_int off_mac
;
780 * This is the offset of the beginning of the MAC-layer payload,
781 * from the beginning of the raw packet data.
783 * I.e., it's the sum of the length of the link-layer header (without,
784 * for example, any 802.2 LLC header, so it's the MAC-layer
785 * portion of that header), plus any prefix preceding the
788 static u_int off_macpl
;
791 * This is 1 if the offset of the beginning of the MAC-layer payload
792 * from the beginning of the link-layer header is variable-length.
794 static int off_macpl_is_variable
;
797 * If the link layer has variable_length headers, "reg_off_macpl"
798 * is the register number for a register containing the length of the
799 * link-layer header plus the length of any variable-length header
800 * preceding the link-layer header. Otherwise, "reg_off_macpl"
803 static int reg_off_macpl
;
806 * "off_linktype" is the offset to information in the link-layer header
807 * giving the packet type. This offset is relative to the beginning
808 * of the link-layer header (i.e., it doesn't include off_ll).
810 * For Ethernet, it's the offset of the Ethernet type field.
812 * For link-layer types that always use 802.2 headers, it's the
813 * offset of the LLC header.
815 * For PPP, it's the offset of the PPP type field.
817 * For Cisco HDLC, it's the offset of the CHDLC type field.
819 * For BSD loopback, it's the offset of the AF_ value.
821 * For Linux cooked sockets, it's the offset of the type field.
823 * It's set to -1 for no encapsulation, in which case, IP is assumed.
825 static u_int off_linktype
;
828 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
829 * checks to check the PPP header, assumed to follow a LAN-style link-
830 * layer header and a PPPoE session header.
832 static int is_pppoes
= 0;
835 * TRUE if the link layer includes an ATM pseudo-header.
837 static int is_atm
= 0;
840 * TRUE if "lane" appeared in the filter; it causes us to generate
841 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
843 static int is_lane
= 0;
846 * These are offsets for the ATM pseudo-header.
848 static u_int off_vpi
;
849 static u_int off_vci
;
850 static u_int off_proto
;
853 * These are offsets for the MTP2 fields.
858 * These are offsets for the MTP3 fields.
860 static u_int off_sio
;
861 static u_int off_opc
;
862 static u_int off_dpc
;
863 static u_int off_sls
;
866 * This is the offset of the first byte after the ATM pseudo_header,
867 * or -1 if there is no ATM pseudo-header.
869 static u_int off_payload
;
872 * These are offsets to the beginning of the network-layer header.
873 * They are relative to the beginning of the MAC-layer payload (i.e.,
874 * they don't include off_ll or off_macpl).
876 * If the link layer never uses 802.2 LLC:
878 * "off_nl" and "off_nl_nosnap" are the same.
880 * If the link layer always uses 802.2 LLC:
882 * "off_nl" is the offset if there's a SNAP header following
885 * "off_nl_nosnap" is the offset if there's no SNAP header.
887 * If the link layer is Ethernet:
889 * "off_nl" is the offset if the packet is an Ethernet II packet
890 * (we assume no 802.3+802.2+SNAP);
892 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
893 * with an 802.2 header following it.
896 static u_int off_nl_nosnap
;
904 linktype
= pcap_datalink(p
);
906 pcap_fddipad
= p
->fddipad
;
910 * Assume it's not raw ATM with a pseudo-header, for now.
921 * And that we're not doing PPPoE.
926 * And assume we're not doing SS7.
935 * Also assume it's not 802.11.
939 off_macpl_is_variable
= 0;
943 label_stack_depth
= 0;
953 off_nl
= 0; /* XXX in reality, variable! */
954 off_nl_nosnap
= 0; /* no 802.2 LLC */
957 case DLT_ARCNET_LINUX
:
960 off_nl
= 0; /* XXX in reality, variable! */
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
966 off_macpl
= 14; /* Ethernet header length */
967 off_nl
= 0; /* Ethernet II */
968 off_nl_nosnap
= 3; /* 802.3+802.2 */
973 * SLIP doesn't have a link level type. The 16 byte
974 * header is hacked into our SLIP driver.
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 /* XXX this may be the same as the DLT_PPP_BSDOS case */
988 off_nl_nosnap
= 0; /* no 802.2 LLC */
996 off_nl_nosnap
= 0; /* no 802.2 LLC */
1003 off_nl_nosnap
= 0; /* no 802.2 LLC */
1008 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1009 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1018 * This does no include the Ethernet header, and
1019 * only covers session state.
1024 off_nl_nosnap
= 0; /* no 802.2 LLC */
1031 off_nl_nosnap
= 0; /* no 802.2 LLC */
1036 * FDDI doesn't really have a link-level type field.
1037 * We set "off_linktype" to the offset of the LLC header.
1039 * To check for Ethernet types, we assume that SSAP = SNAP
1040 * is being used and pick out the encapsulated Ethernet type.
1041 * XXX - should we generate code to check for SNAP?
1045 off_linktype
+= pcap_fddipad
;
1047 off_macpl
= 13; /* FDDI MAC header length */
1049 off_macpl
+= pcap_fddipad
;
1051 off_nl
= 8; /* 802.2+SNAP */
1052 off_nl_nosnap
= 3; /* 802.2 */
1057 * Token Ring doesn't really have a link-level type field.
1058 * We set "off_linktype" to the offset of the LLC header.
1060 * To check for Ethernet types, we assume that SSAP = SNAP
1061 * is being used and pick out the encapsulated Ethernet type.
1062 * XXX - should we generate code to check for SNAP?
1064 * XXX - the header is actually variable-length.
1065 * Some various Linux patched versions gave 38
1066 * as "off_linktype" and 40 as "off_nl"; however,
1067 * if a token ring packet has *no* routing
1068 * information, i.e. is not source-routed, the correct
1069 * values are 20 and 22, as they are in the vanilla code.
1071 * A packet is source-routed iff the uppermost bit
1072 * of the first byte of the source address, at an
1073 * offset of 8, has the uppermost bit set. If the
1074 * packet is source-routed, the total number of bytes
1075 * of routing information is 2 plus bits 0x1F00 of
1076 * the 16-bit value at an offset of 14 (shifted right
1077 * 8 - figure out which byte that is).
1080 off_macpl
= 14; /* Token Ring MAC header length */
1081 off_nl
= 8; /* 802.2+SNAP */
1082 off_nl_nosnap
= 3; /* 802.2 */
1085 case DLT_IEEE802_11
:
1086 case DLT_PRISM_HEADER
:
1087 case DLT_IEEE802_11_RADIO_AVS
:
1088 case DLT_IEEE802_11_RADIO
:
1090 * 802.11 doesn't really have a link-level type field.
1091 * We set "off_linktype" to the offset of the LLC header.
1093 * To check for Ethernet types, we assume that SSAP = SNAP
1094 * is being used and pick out the encapsulated Ethernet type.
1095 * XXX - should we generate code to check for SNAP?
1097 * We also handle variable-length radio headers here.
1098 * The Prism header is in theory variable-length, but in
1099 * practice it's always 144 bytes long. However, some
1100 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1101 * sometimes or always supply an AVS header, so we
1102 * have to check whether the radio header is a Prism
1103 * header or an AVS header, so, in practice, it's
1107 off_macpl
= 0; /* link-layer header is variable-length */
1108 off_macpl_is_variable
= 1;
1109 off_nl
= 8; /* 802.2+SNAP */
1110 off_nl_nosnap
= 3; /* 802.2 */
1115 * At the moment we treat PPI the same way that we treat
1116 * normal Radiotap encoded packets. The difference is in
1117 * the function that generates the code at the beginning
1118 * to compute the header length. Since this code generator
1119 * of PPI supports bare 802.11 encapsulation only (i.e.
1120 * the encapsulated DLT should be DLT_IEEE802_11) we
1121 * generate code to check for this too.
1124 off_macpl
= 0; /* link-layer header is variable-length */
1125 off_macpl_is_variable
= 1;
1126 off_nl
= 8; /* 802.2+SNAP */
1127 off_nl_nosnap
= 3; /* 802.2 */
1130 case DLT_ATM_RFC1483
:
1131 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1133 * assume routed, non-ISO PDUs
1134 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1136 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1137 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1138 * latter would presumably be treated the way PPPoE
1139 * should be, so you can do "pppoe and udp port 2049"
1140 * or "pppoa and tcp port 80" and have it check for
1141 * PPPo{A,E} and a PPP protocol of IP and....
1144 off_macpl
= 0; /* packet begins with LLC header */
1145 off_nl
= 8; /* 802.2+SNAP */
1146 off_nl_nosnap
= 3; /* 802.2 */
1151 * Full Frontal ATM; you get AALn PDUs with an ATM
1155 off_vpi
= SUNATM_VPI_POS
;
1156 off_vci
= SUNATM_VCI_POS
;
1157 off_proto
= PROTO_POS
;
1158 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1159 off_payload
= SUNATM_PKT_BEGIN_POS
;
1160 off_linktype
= off_payload
;
1161 off_macpl
= off_payload
; /* if LLC-encapsulated */
1162 off_nl
= 8; /* 802.2+SNAP */
1163 off_nl_nosnap
= 3; /* 802.2 */
1172 off_nl_nosnap
= 0; /* no 802.2 LLC */
1175 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1179 off_nl_nosnap
= 0; /* no 802.2 LLC */
1184 * LocalTalk does have a 1-byte type field in the LLAP header,
1185 * but really it just indicates whether there is a "short" or
1186 * "long" DDP packet following.
1191 off_nl_nosnap
= 0; /* no 802.2 LLC */
1194 case DLT_IP_OVER_FC
:
1196 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1197 * link-level type field. We set "off_linktype" to the
1198 * offset of the LLC header.
1200 * To check for Ethernet types, we assume that SSAP = SNAP
1201 * is being used and pick out the encapsulated Ethernet type.
1202 * XXX - should we generate code to check for SNAP? RFC
1203 * 2625 says SNAP should be used.
1207 off_nl
= 8; /* 802.2+SNAP */
1208 off_nl_nosnap
= 3; /* 802.2 */
1213 * XXX - we should set this to handle SNAP-encapsulated
1214 * frames (NLPID of 0x80).
1219 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 * the only BPF-interesting FRF.16 frames are non-control frames;
1224 * Frame Relay has a variable length link-layer
1225 * so lets start with offset 4 for now and increments later on (FIXME);
1231 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1234 case DLT_APPLE_IP_OVER_IEEE1394
:
1238 off_nl_nosnap
= 0; /* no 802.2 LLC */
1241 case DLT_LINUX_IRDA
:
1243 * Currently, only raw "link[N:M]" filtering is supported.
1253 * Currently, only raw "link[N:M]" filtering is supported.
1261 case DLT_SYMANTEC_FIREWALL
:
1264 off_nl
= 0; /* Ethernet II */
1265 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1268 #ifdef HAVE_NET_PFVAR_H
1271 off_macpl
= PFLOG_HDRLEN
;
1273 off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 case DLT_JUNIPER_MFR
:
1278 case DLT_JUNIPER_MLFR
:
1279 case DLT_JUNIPER_MLPPP
:
1280 case DLT_JUNIPER_PPP
:
1281 case DLT_JUNIPER_CHDLC
:
1282 case DLT_JUNIPER_FRELAY
:
1286 off_nl_nosnap
= -1; /* no 802.2 LLC */
1289 case DLT_JUNIPER_ATM1
:
1290 off_linktype
= 4; /* in reality variable between 4-8 */
1291 off_macpl
= 4; /* in reality variable between 4-8 */
1296 case DLT_JUNIPER_ATM2
:
1297 off_linktype
= 8; /* in reality variable between 8-12 */
1298 off_macpl
= 8; /* in reality variable between 8-12 */
1303 /* frames captured on a Juniper PPPoE service PIC
1304 * contain raw ethernet frames */
1305 case DLT_JUNIPER_PPPOE
:
1306 case DLT_JUNIPER_ETHER
:
1309 off_nl
= 18; /* Ethernet II */
1310 off_nl_nosnap
= 21; /* 802.3+802.2 */
1313 case DLT_JUNIPER_PPPOE_ATM
:
1317 off_nl_nosnap
= -1; /* no 802.2 LLC */
1320 case DLT_JUNIPER_GGSN
:
1324 off_nl_nosnap
= -1; /* no 802.2 LLC */
1327 case DLT_JUNIPER_ES
:
1329 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1330 off_nl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_JUNIPER_MONITOR
:
1337 off_nl
= 0; /* raw IP/IP6 header */
1338 off_nl_nosnap
= -1; /* no 802.2 LLC */
1341 case DLT_JUNIPER_SERVICES
:
1343 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1344 off_nl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl_nosnap
= -1; /* no 802.2 LLC */
1348 case DLT_JUNIPER_VP
:
1355 case DLT_JUNIPER_ST
:
1362 case DLT_JUNIPER_ISM
:
1369 case DLT_JUNIPER_VS
:
1370 case DLT_JUNIPER_SRX_E2E
:
1371 case DLT_JUNIPER_FIBRECHANNEL
:
1372 case DLT_JUNIPER_ATM_CEMIC
:
1391 case DLT_MTP2_WITH_PHDR
:
1424 case DLT_LINUX_LAPD
:
1426 * Currently, only raw "link[N:M]" filtering is supported.
1436 * Currently, only raw "link[N:M]" filtering is supported.
1444 case DLT_BLUETOOTH_HCI_H4
:
1446 * Currently, only raw "link[N:M]" filtering is supported.
1456 * Currently, only raw "link[N:M]" filtering is supported.
1466 * Currently, only raw "link[N:M]" filtering is supported.
1474 case DLT_IEEE802_15_4_LINUX
:
1476 * Currently, only raw "link[N:M]" filtering is supported.
1484 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1486 * Currently, only raw "link[N:M]" filtering is supported.
1494 case DLT_IEEE802_15_4
:
1496 * Currently, only raw "link[N:M]" filtering is supported.
1506 * Currently, only raw "link[N:M]" filtering is supported.
1516 * Currently, only raw "link[N:M]" filtering is supported.
1526 * Currently, only raw "link[N:M]" filtering is supported.
1534 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1536 * Currently, only raw "link[N:M]" filtering is supported.
1546 * Currently, only raw "link[N:M]" filtering is supported.
1548 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1550 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1551 off_nl_nosnap
= -1; /* no 802.2 LLC */
1552 off_mac
= 1; /* step over the kiss length byte */
1555 case DLT_IEEE802_15_4_NONASK_PHY
:
1557 * Currently, only raw "link[N:M]" filtering is supported.
1567 * Currently, only raw "link[N:M]" filtering is supported.
1575 case DLT_USB_LINUX_MMAPPED
:
1577 * Currently, only raw "link[N:M]" filtering is supported.
1585 case DLT_CAN_SOCKETCAN
:
1587 * Currently, only raw "link[N:M]" filtering is supported.
1597 off_macpl
= 24; /* ipnet header length */
1602 case DLT_IEEE802_15_4_NOFCS
:
1604 * Currently, only raw "link[N:M]" filtering is supported.
1612 bpf_error("unknown data link type %d", linktype
);
1617 * Load a value relative to the beginning of the link-layer header.
1618 * The link-layer header doesn't necessarily begin at the beginning
1619 * of the packet data; there might be a variable-length prefix containing
1620 * radio information.
1622 static struct slist
*
1623 gen_load_llrel(offset
, size
)
1626 struct slist
*s
, *s2
;
1628 s
= gen_llprefixlen();
1631 * If "s" is non-null, it has code to arrange that the X register
1632 * contains the length of the prefix preceding the link-layer
1635 * Otherwise, the length of the prefix preceding the link-layer
1636 * header is "off_ll".
1640 * There's a variable-length prefix preceding the
1641 * link-layer header. "s" points to a list of statements
1642 * that put the length of that prefix into the X register.
1643 * do an indirect load, to use the X register as an offset.
1645 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1650 * There is no variable-length header preceding the
1651 * link-layer header; add in off_ll, which, if there's
1652 * a fixed-length header preceding the link-layer header,
1653 * is the length of that header.
1655 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1656 s
->s
.k
= offset
+ off_ll
;
1662 * Load a value relative to the beginning of the MAC-layer payload.
1664 static struct slist
*
1665 gen_load_macplrel(offset
, size
)
1668 struct slist
*s
, *s2
;
1670 s
= gen_off_macpl();
1673 * If s is non-null, the offset of the MAC-layer payload is
1674 * variable, and s points to a list of instructions that
1675 * arrange that the X register contains that offset.
1677 * Otherwise, the offset of the MAC-layer payload is constant,
1678 * and is in off_macpl.
1682 * The offset of the MAC-layer payload is in the X
1683 * register. Do an indirect load, to use the X register
1686 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1691 * The offset of the MAC-layer payload is constant,
1692 * and is in off_macpl; load the value at that offset
1693 * plus the specified offset.
1695 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1696 s
->s
.k
= off_macpl
+ offset
;
1702 * Load a value relative to the beginning of the specified header.
1704 static struct slist
*
1705 gen_load_a(offrel
, offset
, size
)
1706 enum e_offrel offrel
;
1709 struct slist
*s
, *s2
;
1714 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1719 s
= gen_load_llrel(offset
, size
);
1723 s
= gen_load_macplrel(offset
, size
);
1727 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1731 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1736 * Load the X register with the length of the IPv4 header
1737 * (plus the offset of the link-layer header, if it's
1738 * preceded by a variable-length header such as a radio
1739 * header), in bytes.
1741 s
= gen_loadx_iphdrlen();
1744 * Load the item at {offset of the MAC-layer payload} +
1745 * {offset, relative to the start of the MAC-layer
1746 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1747 * {specified offset}.
1749 * (If the offset of the MAC-layer payload is variable,
1750 * it's included in the value in the X register, and
1753 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1754 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1759 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1770 * Generate code to load into the X register the sum of the length of
1771 * the IPv4 header and any variable-length header preceding the link-layer
1774 static struct slist
*
1775 gen_loadx_iphdrlen()
1777 struct slist
*s
, *s2
;
1779 s
= gen_off_macpl();
1782 * There's a variable-length prefix preceding the
1783 * link-layer header, or the link-layer header is itself
1784 * variable-length. "s" points to a list of statements
1785 * that put the offset of the MAC-layer payload into
1788 * The 4*([k]&0xf) addressing mode can't be used, as we
1789 * don't have a constant offset, so we have to load the
1790 * value in question into the A register and add to it
1791 * the value from the X register.
1793 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1796 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1799 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1804 * The A register now contains the length of the
1805 * IP header. We need to add to it the offset of
1806 * the MAC-layer payload, which is still in the X
1807 * register, and move the result into the X register.
1809 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1810 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1813 * There is no variable-length header preceding the
1814 * link-layer header, and the link-layer header is
1815 * fixed-length; load the length of the IPv4 header,
1816 * which is at an offset of off_nl from the beginning
1817 * of the MAC-layer payload, and thus at an offset
1818 * of off_mac_pl + off_nl from the beginning of the
1821 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1822 s
->s
.k
= off_macpl
+ off_nl
;
1827 static struct block
*
1834 s
= new_stmt(BPF_LD
|BPF_IMM
);
1836 b
= new_block(JMP(BPF_JEQ
));
1842 static inline struct block
*
1845 return gen_uncond(1);
1848 static inline struct block
*
1851 return gen_uncond(0);
1855 * Byte-swap a 32-bit number.
1856 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1857 * big-endian platforms.)
1859 #define SWAPLONG(y) \
1860 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1863 * Generate code to match a particular packet type.
1865 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1866 * value, if <= ETHERMTU. We use that to determine whether to
1867 * match the type/length field or to check the type/length field for
1868 * a value <= ETHERMTU to see whether it's a type field and then do
1869 * the appropriate test.
1871 static struct block
*
1872 gen_ether_linktype(proto
)
1875 struct block
*b0
, *b1
;
1881 case LLCSAP_NETBEUI
:
1883 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1884 * so we check the DSAP and SSAP.
1886 * LLCSAP_IP checks for IP-over-802.2, rather
1887 * than IP-over-Ethernet or IP-over-SNAP.
1889 * XXX - should we check both the DSAP and the
1890 * SSAP, like this, or should we check just the
1891 * DSAP, as we do for other types <= ETHERMTU
1892 * (i.e., other SAP values)?
1894 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1896 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1897 ((proto
<< 8) | proto
));
1905 * Ethernet_II frames, which are Ethernet
1906 * frames with a frame type of ETHERTYPE_IPX;
1908 * Ethernet_802.3 frames, which are 802.3
1909 * frames (i.e., the type/length field is
1910 * a length field, <= ETHERMTU, rather than
1911 * a type field) with the first two bytes
1912 * after the Ethernet/802.3 header being
1915 * Ethernet_802.2 frames, which are 802.3
1916 * frames with an 802.2 LLC header and
1917 * with the IPX LSAP as the DSAP in the LLC
1920 * Ethernet_SNAP frames, which are 802.3
1921 * frames with an LLC header and a SNAP
1922 * header and with an OUI of 0x000000
1923 * (encapsulated Ethernet) and a protocol
1924 * ID of ETHERTYPE_IPX in the SNAP header.
1926 * XXX - should we generate the same code both
1927 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1931 * This generates code to check both for the
1932 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1934 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1935 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1939 * Now we add code to check for SNAP frames with
1940 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1942 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1946 * Now we generate code to check for 802.3
1947 * frames in general.
1949 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1953 * Now add the check for 802.3 frames before the
1954 * check for Ethernet_802.2 and Ethernet_802.3,
1955 * as those checks should only be done on 802.3
1956 * frames, not on Ethernet frames.
1961 * Now add the check for Ethernet_II frames, and
1962 * do that before checking for the other frame
1965 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1966 (bpf_int32
)ETHERTYPE_IPX
);
1970 case ETHERTYPE_ATALK
:
1971 case ETHERTYPE_AARP
:
1973 * EtherTalk (AppleTalk protocols on Ethernet link
1974 * layer) may use 802.2 encapsulation.
1978 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1979 * we check for an Ethernet type field less than
1980 * 1500, which means it's an 802.3 length field.
1982 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1986 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1987 * SNAP packets with an organization code of
1988 * 0x080007 (Apple, for Appletalk) and a protocol
1989 * type of ETHERTYPE_ATALK (Appletalk).
1991 * 802.2-encapsulated ETHERTYPE_AARP packets are
1992 * SNAP packets with an organization code of
1993 * 0x000000 (encapsulated Ethernet) and a protocol
1994 * type of ETHERTYPE_AARP (Appletalk ARP).
1996 if (proto
== ETHERTYPE_ATALK
)
1997 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1998 else /* proto == ETHERTYPE_AARP */
1999 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2003 * Check for Ethernet encapsulation (Ethertalk
2004 * phase 1?); we just check for the Ethernet
2007 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2013 if (proto
<= ETHERMTU
) {
2015 * This is an LLC SAP value, so the frames
2016 * that match would be 802.2 frames.
2017 * Check that the frame is an 802.2 frame
2018 * (i.e., that the length/type field is
2019 * a length field, <= ETHERMTU) and
2020 * then check the DSAP.
2022 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
2024 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
2030 * This is an Ethernet type, so compare
2031 * the length/type field with it (if
2032 * the frame is an 802.2 frame, the length
2033 * field will be <= ETHERMTU, and, as
2034 * "proto" is > ETHERMTU, this test
2035 * will fail and the frame won't match,
2036 * which is what we want).
2038 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2045 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2046 * or IPv6 then we have an error.
2048 static struct block
*
2049 gen_ipnet_linktype(proto
)
2055 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2056 (bpf_int32
)IPH_AF_INET
);
2059 case ETHERTYPE_IPV6
:
2060 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2061 (bpf_int32
)IPH_AF_INET6
);
2072 * Generate code to match a particular packet type.
2074 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2075 * value, if <= ETHERMTU. We use that to determine whether to
2076 * match the type field or to check the type field for the special
2077 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2079 static struct block
*
2080 gen_linux_sll_linktype(proto
)
2083 struct block
*b0
, *b1
;
2089 case LLCSAP_NETBEUI
:
2091 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2092 * so we check the DSAP and SSAP.
2094 * LLCSAP_IP checks for IP-over-802.2, rather
2095 * than IP-over-Ethernet or IP-over-SNAP.
2097 * XXX - should we check both the DSAP and the
2098 * SSAP, like this, or should we check just the
2099 * DSAP, as we do for other types <= ETHERMTU
2100 * (i.e., other SAP values)?
2102 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2103 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2104 ((proto
<< 8) | proto
));
2110 * Ethernet_II frames, which are Ethernet
2111 * frames with a frame type of ETHERTYPE_IPX;
2113 * Ethernet_802.3 frames, which have a frame
2114 * type of LINUX_SLL_P_802_3;
2116 * Ethernet_802.2 frames, which are 802.3
2117 * frames with an 802.2 LLC header (i.e, have
2118 * a frame type of LINUX_SLL_P_802_2) and
2119 * with the IPX LSAP as the DSAP in the LLC
2122 * Ethernet_SNAP frames, which are 802.3
2123 * frames with an LLC header and a SNAP
2124 * header and with an OUI of 0x000000
2125 * (encapsulated Ethernet) and a protocol
2126 * ID of ETHERTYPE_IPX in the SNAP header.
2128 * First, do the checks on LINUX_SLL_P_802_2
2129 * frames; generate the check for either
2130 * Ethernet_802.2 or Ethernet_SNAP frames, and
2131 * then put a check for LINUX_SLL_P_802_2 frames
2134 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2135 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2137 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2141 * Now check for 802.3 frames and OR that with
2142 * the previous test.
2144 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2148 * Now add the check for Ethernet_II frames, and
2149 * do that before checking for the other frame
2152 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2153 (bpf_int32
)ETHERTYPE_IPX
);
2157 case ETHERTYPE_ATALK
:
2158 case ETHERTYPE_AARP
:
2160 * EtherTalk (AppleTalk protocols on Ethernet link
2161 * layer) may use 802.2 encapsulation.
2165 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2166 * we check for the 802.2 protocol type in the
2167 * "Ethernet type" field.
2169 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2172 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2173 * SNAP packets with an organization code of
2174 * 0x080007 (Apple, for Appletalk) and a protocol
2175 * type of ETHERTYPE_ATALK (Appletalk).
2177 * 802.2-encapsulated ETHERTYPE_AARP packets are
2178 * SNAP packets with an organization code of
2179 * 0x000000 (encapsulated Ethernet) and a protocol
2180 * type of ETHERTYPE_AARP (Appletalk ARP).
2182 if (proto
== ETHERTYPE_ATALK
)
2183 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2184 else /* proto == ETHERTYPE_AARP */
2185 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2189 * Check for Ethernet encapsulation (Ethertalk
2190 * phase 1?); we just check for the Ethernet
2193 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2199 if (proto
<= ETHERMTU
) {
2201 * This is an LLC SAP value, so the frames
2202 * that match would be 802.2 frames.
2203 * Check for the 802.2 protocol type
2204 * in the "Ethernet type" field, and
2205 * then check the DSAP.
2207 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2209 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2215 * This is an Ethernet type, so compare
2216 * the length/type field with it (if
2217 * the frame is an 802.2 frame, the length
2218 * field will be <= ETHERMTU, and, as
2219 * "proto" is > ETHERMTU, this test
2220 * will fail and the frame won't match,
2221 * which is what we want).
2223 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2229 static struct slist
*
2230 gen_load_prism_llprefixlen()
2232 struct slist
*s1
, *s2
;
2233 struct slist
*sjeq_avs_cookie
;
2234 struct slist
*sjcommon
;
2237 * This code is not compatible with the optimizer, as
2238 * we are generating jmp instructions within a normal
2239 * slist of instructions
2244 * Generate code to load the length of the radio header into
2245 * the register assigned to hold that length, if one has been
2246 * assigned. (If one hasn't been assigned, no code we've
2247 * generated uses that prefix, so we don't need to generate any
2250 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2251 * or always use the AVS header rather than the Prism header.
2252 * We load a 4-byte big-endian value at the beginning of the
2253 * raw packet data, and see whether, when masked with 0xFFFFF000,
2254 * it's equal to 0x80211000. If so, that indicates that it's
2255 * an AVS header (the masked-out bits are the version number).
2256 * Otherwise, it's a Prism header.
2258 * XXX - the Prism header is also, in theory, variable-length,
2259 * but no known software generates headers that aren't 144
2262 if (reg_off_ll
!= -1) {
2266 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2270 * AND it with 0xFFFFF000.
2272 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2273 s2
->s
.k
= 0xFFFFF000;
2277 * Compare with 0x80211000.
2279 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2280 sjeq_avs_cookie
->s
.k
= 0x80211000;
2281 sappend(s1
, sjeq_avs_cookie
);
2286 * The 4 bytes at an offset of 4 from the beginning of
2287 * the AVS header are the length of the AVS header.
2288 * That field is big-endian.
2290 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2293 sjeq_avs_cookie
->s
.jt
= s2
;
2296 * Now jump to the code to allocate a register
2297 * into which to save the header length and
2298 * store the length there. (The "jump always"
2299 * instruction needs to have the k field set;
2300 * it's added to the PC, so, as we're jumping
2301 * over a single instruction, it should be 1.)
2303 sjcommon
= new_stmt(JMP(BPF_JA
));
2305 sappend(s1
, sjcommon
);
2308 * Now for the code that handles the Prism header.
2309 * Just load the length of the Prism header (144)
2310 * into the A register. Have the test for an AVS
2311 * header branch here if we don't have an AVS header.
2313 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2316 sjeq_avs_cookie
->s
.jf
= s2
;
2319 * Now allocate a register to hold that value and store
2320 * it. The code for the AVS header will jump here after
2321 * loading the length of the AVS header.
2323 s2
= new_stmt(BPF_ST
);
2324 s2
->s
.k
= reg_off_ll
;
2326 sjcommon
->s
.jf
= s2
;
2329 * Now move it into the X register.
2331 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2339 static struct slist
*
2340 gen_load_avs_llprefixlen()
2342 struct slist
*s1
, *s2
;
2345 * Generate code to load the length of the AVS header into
2346 * the register assigned to hold that length, if one has been
2347 * assigned. (If one hasn't been assigned, no code we've
2348 * generated uses that prefix, so we don't need to generate any
2351 if (reg_off_ll
!= -1) {
2353 * The 4 bytes at an offset of 4 from the beginning of
2354 * the AVS header are the length of the AVS header.
2355 * That field is big-endian.
2357 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2361 * Now allocate a register to hold that value and store
2364 s2
= new_stmt(BPF_ST
);
2365 s2
->s
.k
= reg_off_ll
;
2369 * Now move it into the X register.
2371 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2379 static struct slist
*
2380 gen_load_radiotap_llprefixlen()
2382 struct slist
*s1
, *s2
;
2385 * Generate code to load the length of the radiotap header into
2386 * the register assigned to hold that length, if one has been
2387 * assigned. (If one hasn't been assigned, no code we've
2388 * generated uses that prefix, so we don't need to generate any
2391 if (reg_off_ll
!= -1) {
2393 * The 2 bytes at offsets of 2 and 3 from the beginning
2394 * of the radiotap header are the length of the radiotap
2395 * header; unfortunately, it's little-endian, so we have
2396 * to load it a byte at a time and construct the value.
2400 * Load the high-order byte, at an offset of 3, shift it
2401 * left a byte, and put the result in the X register.
2403 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2405 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2408 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2412 * Load the next byte, at an offset of 2, and OR the
2413 * value from the X register into it.
2415 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2418 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2422 * Now allocate a register to hold that value and store
2425 s2
= new_stmt(BPF_ST
);
2426 s2
->s
.k
= reg_off_ll
;
2430 * Now move it into the X register.
2432 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2441 * At the moment we treat PPI as normal Radiotap encoded
2442 * packets. The difference is in the function that generates
2443 * the code at the beginning to compute the header length.
2444 * Since this code generator of PPI supports bare 802.11
2445 * encapsulation only (i.e. the encapsulated DLT should be
2446 * DLT_IEEE802_11) we generate code to check for this too;
2447 * that's done in finish_parse().
2449 static struct slist
*
2450 gen_load_ppi_llprefixlen()
2452 struct slist
*s1
, *s2
;
2455 * Generate code to load the length of the radiotap header
2456 * into the register assigned to hold that length, if one has
2459 if (reg_off_ll
!= -1) {
2461 * The 2 bytes at offsets of 2 and 3 from the beginning
2462 * of the radiotap header are the length of the radiotap
2463 * header; unfortunately, it's little-endian, so we have
2464 * to load it a byte at a time and construct the value.
2468 * Load the high-order byte, at an offset of 3, shift it
2469 * left a byte, and put the result in the X register.
2471 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2473 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2476 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2480 * Load the next byte, at an offset of 2, and OR the
2481 * value from the X register into it.
2483 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2486 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2490 * Now allocate a register to hold that value and store
2493 s2
= new_stmt(BPF_ST
);
2494 s2
->s
.k
= reg_off_ll
;
2498 * Now move it into the X register.
2500 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2509 * Load a value relative to the beginning of the link-layer header after the 802.11
2510 * header, i.e. LLC_SNAP.
2511 * The link-layer header doesn't necessarily begin at the beginning
2512 * of the packet data; there might be a variable-length prefix containing
2513 * radio information.
2515 static struct slist
*
2516 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2519 struct slist
*sjset_data_frame_1
;
2520 struct slist
*sjset_data_frame_2
;
2521 struct slist
*sjset_qos
;
2522 struct slist
*sjset_radiotap_flags
;
2523 struct slist
*sjset_radiotap_tsft
;
2524 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2525 struct slist
*s_roundup
;
2527 if (reg_off_macpl
== -1) {
2529 * No register has been assigned to the offset of
2530 * the MAC-layer payload, which means nobody needs
2531 * it; don't bother computing it - just return
2532 * what we already have.
2538 * This code is not compatible with the optimizer, as
2539 * we are generating jmp instructions within a normal
2540 * slist of instructions
2545 * If "s" is non-null, it has code to arrange that the X register
2546 * contains the length of the prefix preceding the link-layer
2549 * Otherwise, the length of the prefix preceding the link-layer
2550 * header is "off_ll".
2554 * There is no variable-length header preceding the
2555 * link-layer header.
2557 * Load the length of the fixed-length prefix preceding
2558 * the link-layer header (if any) into the X register,
2559 * and store it in the reg_off_macpl register.
2560 * That length is off_ll.
2562 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2567 * The X register contains the offset of the beginning of the
2568 * link-layer header; add 24, which is the minimum length
2569 * of the MAC header for a data frame, to that, and store it
2570 * in reg_off_macpl, and then load the Frame Control field,
2571 * which is at the offset in the X register, with an indexed load.
2573 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2575 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2578 s2
= new_stmt(BPF_ST
);
2579 s2
->s
.k
= reg_off_macpl
;
2582 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2587 * Check the Frame Control field to see if this is a data frame;
2588 * a data frame has the 0x08 bit (b3) in that field set and the
2589 * 0x04 bit (b2) clear.
2591 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2592 sjset_data_frame_1
->s
.k
= 0x08;
2593 sappend(s
, sjset_data_frame_1
);
2596 * If b3 is set, test b2, otherwise go to the first statement of
2597 * the rest of the program.
2599 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2600 sjset_data_frame_2
->s
.k
= 0x04;
2601 sappend(s
, sjset_data_frame_2
);
2602 sjset_data_frame_1
->s
.jf
= snext
;
2605 * If b2 is not set, this is a data frame; test the QoS bit.
2606 * Otherwise, go to the first statement of the rest of the
2609 sjset_data_frame_2
->s
.jt
= snext
;
2610 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2611 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2612 sappend(s
, sjset_qos
);
2615 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2617 * Otherwise, go to the first statement of the rest of the
2620 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2621 s2
->s
.k
= reg_off_macpl
;
2623 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2626 s2
= new_stmt(BPF_ST
);
2627 s2
->s
.k
= reg_off_macpl
;
2631 * If we have a radiotap header, look at it to see whether
2632 * there's Atheros padding between the MAC-layer header
2635 * Note: all of the fields in the radiotap header are
2636 * little-endian, so we byte-swap all of the values
2637 * we test against, as they will be loaded as big-endian
2640 if (linktype
== DLT_IEEE802_11_RADIO
) {
2642 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2643 * in the presence flag?
2645 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2649 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2650 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2651 sappend(s
, sjset_radiotap_flags
);
2654 * If not, skip all of this.
2656 sjset_radiotap_flags
->s
.jf
= snext
;
2659 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2661 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2662 new_stmt(JMP(BPF_JSET
));
2663 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2664 sappend(s
, sjset_radiotap_tsft
);
2667 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2668 * at an offset of 16 from the beginning of the raw packet
2669 * data (8 bytes for the radiotap header and 8 bytes for
2672 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2675 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2679 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2680 sjset_tsft_datapad
->s
.k
= 0x20;
2681 sappend(s
, sjset_tsft_datapad
);
2684 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2685 * at an offset of 8 from the beginning of the raw packet
2686 * data (8 bytes for the radiotap header).
2688 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2691 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2695 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2696 sjset_notsft_datapad
->s
.k
= 0x20;
2697 sappend(s
, sjset_notsft_datapad
);
2700 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2701 * set, round the length of the 802.11 header to
2702 * a multiple of 4. Do that by adding 3 and then
2703 * dividing by and multiplying by 4, which we do by
2706 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2707 s_roundup
->s
.k
= reg_off_macpl
;
2708 sappend(s
, s_roundup
);
2709 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2712 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2715 s2
= new_stmt(BPF_ST
);
2716 s2
->s
.k
= reg_off_macpl
;
2719 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2720 sjset_tsft_datapad
->s
.jf
= snext
;
2721 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2722 sjset_notsft_datapad
->s
.jf
= snext
;
2724 sjset_qos
->s
.jf
= snext
;
2730 insert_compute_vloffsets(b
)
2736 * For link-layer types that have a variable-length header
2737 * preceding the link-layer header, generate code to load
2738 * the offset of the link-layer header into the register
2739 * assigned to that offset, if any.
2743 case DLT_PRISM_HEADER
:
2744 s
= gen_load_prism_llprefixlen();
2747 case DLT_IEEE802_11_RADIO_AVS
:
2748 s
= gen_load_avs_llprefixlen();
2751 case DLT_IEEE802_11_RADIO
:
2752 s
= gen_load_radiotap_llprefixlen();
2756 s
= gen_load_ppi_llprefixlen();
2765 * For link-layer types that have a variable-length link-layer
2766 * header, generate code to load the offset of the MAC-layer
2767 * payload into the register assigned to that offset, if any.
2771 case DLT_IEEE802_11
:
2772 case DLT_PRISM_HEADER
:
2773 case DLT_IEEE802_11_RADIO_AVS
:
2774 case DLT_IEEE802_11_RADIO
:
2776 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2781 * If we have any offset-loading code, append all the
2782 * existing statements in the block to those statements,
2783 * and make the resulting list the list of statements
2787 sappend(s
, b
->stmts
);
2792 static struct block
*
2793 gen_ppi_dlt_check(void)
2795 struct slist
*s_load_dlt
;
2798 if (linktype
== DLT_PPI
)
2800 /* Create the statements that check for the DLT
2802 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2803 s_load_dlt
->s
.k
= 4;
2805 b
= new_block(JMP(BPF_JEQ
));
2807 b
->stmts
= s_load_dlt
;
2808 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2818 static struct slist
*
2819 gen_prism_llprefixlen(void)
2823 if (reg_off_ll
== -1) {
2825 * We haven't yet assigned a register for the length
2826 * of the radio header; allocate one.
2828 reg_off_ll
= alloc_reg();
2832 * Load the register containing the radio length
2833 * into the X register.
2835 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2836 s
->s
.k
= reg_off_ll
;
2840 static struct slist
*
2841 gen_avs_llprefixlen(void)
2845 if (reg_off_ll
== -1) {
2847 * We haven't yet assigned a register for the length
2848 * of the AVS header; allocate one.
2850 reg_off_ll
= alloc_reg();
2854 * Load the register containing the AVS length
2855 * into the X register.
2857 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2858 s
->s
.k
= reg_off_ll
;
2862 static struct slist
*
2863 gen_radiotap_llprefixlen(void)
2867 if (reg_off_ll
== -1) {
2869 * We haven't yet assigned a register for the length
2870 * of the radiotap header; allocate one.
2872 reg_off_ll
= alloc_reg();
2876 * Load the register containing the radiotap length
2877 * into the X register.
2879 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2880 s
->s
.k
= reg_off_ll
;
2885 * At the moment we treat PPI as normal Radiotap encoded
2886 * packets. The difference is in the function that generates
2887 * the code at the beginning to compute the header length.
2888 * Since this code generator of PPI supports bare 802.11
2889 * encapsulation only (i.e. the encapsulated DLT should be
2890 * DLT_IEEE802_11) we generate code to check for this too.
2892 static struct slist
*
2893 gen_ppi_llprefixlen(void)
2897 if (reg_off_ll
== -1) {
2899 * We haven't yet assigned a register for the length
2900 * of the radiotap header; allocate one.
2902 reg_off_ll
= alloc_reg();
2906 * Load the register containing the PPI length
2907 * into the X register.
2909 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2910 s
->s
.k
= reg_off_ll
;
2915 * Generate code to compute the link-layer header length, if necessary,
2916 * putting it into the X register, and to return either a pointer to a
2917 * "struct slist" for the list of statements in that code, or NULL if
2918 * no code is necessary.
2920 static struct slist
*
2921 gen_llprefixlen(void)
2925 case DLT_PRISM_HEADER
:
2926 return gen_prism_llprefixlen();
2928 case DLT_IEEE802_11_RADIO_AVS
:
2929 return gen_avs_llprefixlen();
2931 case DLT_IEEE802_11_RADIO
:
2932 return gen_radiotap_llprefixlen();
2935 return gen_ppi_llprefixlen();
2943 * Generate code to load the register containing the offset of the
2944 * MAC-layer payload into the X register; if no register for that offset
2945 * has been allocated, allocate it first.
2947 static struct slist
*
2952 if (off_macpl_is_variable
) {
2953 if (reg_off_macpl
== -1) {
2955 * We haven't yet assigned a register for the offset
2956 * of the MAC-layer payload; allocate one.
2958 reg_off_macpl
= alloc_reg();
2962 * Load the register containing the offset of the MAC-layer
2963 * payload into the X register.
2965 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2966 s
->s
.k
= reg_off_macpl
;
2970 * That offset isn't variable, so we don't need to
2971 * generate any code.
2978 * Map an Ethernet type to the equivalent PPP type.
2981 ethertype_to_ppptype(proto
)
2991 case ETHERTYPE_IPV6
:
3000 case ETHERTYPE_ATALK
:
3014 * I'm assuming the "Bridging PDU"s that go
3015 * over PPP are Spanning Tree Protocol
3029 * Generate code to match a particular packet type by matching the
3030 * link-layer type field or fields in the 802.2 LLC header.
3032 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3033 * value, if <= ETHERMTU.
3035 static struct block
*
3039 struct block
*b0
, *b1
, *b2
;
3041 /* are we checking MPLS-encapsulated packets? */
3042 if (label_stack_depth
> 0) {
3046 /* FIXME add other L3 proto IDs */
3047 return gen_mpls_linktype(Q_IP
);
3049 case ETHERTYPE_IPV6
:
3051 /* FIXME add other L3 proto IDs */
3052 return gen_mpls_linktype(Q_IPV6
);
3055 bpf_error("unsupported protocol over mpls");
3061 * Are we testing PPPoE packets?
3065 * The PPPoE session header is part of the
3066 * MAC-layer payload, so all references
3067 * should be relative to the beginning of
3072 * We use Ethernet protocol types inside libpcap;
3073 * map them to the corresponding PPP protocol types.
3075 proto
= ethertype_to_ppptype(proto
);
3076 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3082 return gen_ether_linktype(proto
);
3090 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3094 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3101 case DLT_IEEE802_11
:
3102 case DLT_PRISM_HEADER
:
3103 case DLT_IEEE802_11_RADIO_AVS
:
3104 case DLT_IEEE802_11_RADIO
:
3107 * Check that we have a data frame.
3109 b0
= gen_check_802_11_data_frame();
3112 * Now check for the specified link-layer type.
3114 b1
= gen_llc_linktype(proto
);
3122 * XXX - check for asynchronous frames, as per RFC 1103.
3124 return gen_llc_linktype(proto
);
3130 * XXX - check for LLC PDUs, as per IEEE 802.5.
3132 return gen_llc_linktype(proto
);
3136 case DLT_ATM_RFC1483
:
3138 case DLT_IP_OVER_FC
:
3139 return gen_llc_linktype(proto
);
3145 * If "is_lane" is set, check for a LANE-encapsulated
3146 * version of this protocol, otherwise check for an
3147 * LLC-encapsulated version of this protocol.
3149 * We assume LANE means Ethernet, not Token Ring.
3153 * Check that the packet doesn't begin with an
3154 * LE Control marker. (We've already generated
3157 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3162 * Now generate an Ethernet test.
3164 b1
= gen_ether_linktype(proto
);
3169 * Check for LLC encapsulation and then check the
3172 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3173 b1
= gen_llc_linktype(proto
);
3181 return gen_linux_sll_linktype(proto
);
3186 case DLT_SLIP_BSDOS
:
3189 * These types don't provide any type field; packets
3190 * are always IPv4 or IPv6.
3192 * XXX - for IPv4, check for a version number of 4, and,
3193 * for IPv6, check for a version number of 6?
3198 /* Check for a version number of 4. */
3199 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3201 case ETHERTYPE_IPV6
:
3202 /* Check for a version number of 6. */
3203 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3207 return gen_false(); /* always false */
3214 * Raw IPv4, so no type field.
3216 if (proto
== ETHERTYPE_IP
)
3217 return gen_true(); /* always true */
3219 /* Checking for something other than IPv4; always false */
3226 * Raw IPv6, so no type field.
3229 if (proto
== ETHERTYPE_IPV6
)
3230 return gen_true(); /* always true */
3233 /* Checking for something other than IPv6; always false */
3240 case DLT_PPP_SERIAL
:
3243 * We use Ethernet protocol types inside libpcap;
3244 * map them to the corresponding PPP protocol types.
3246 proto
= ethertype_to_ppptype(proto
);
3247 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3253 * We use Ethernet protocol types inside libpcap;
3254 * map them to the corresponding PPP protocol types.
3260 * Also check for Van Jacobson-compressed IP.
3261 * XXX - do this for other forms of PPP?
3263 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3264 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3266 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3271 proto
= ethertype_to_ppptype(proto
);
3272 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3282 * For DLT_NULL, the link-layer header is a 32-bit
3283 * word containing an AF_ value in *host* byte order,
3284 * and for DLT_ENC, the link-layer header begins
3285 * with a 32-bit work containing an AF_ value in
3288 * In addition, if we're reading a saved capture file,
3289 * the host byte order in the capture may not be the
3290 * same as the host byte order on this machine.
3292 * For DLT_LOOP, the link-layer header is a 32-bit
3293 * word containing an AF_ value in *network* byte order.
3295 * XXX - AF_ values may, unfortunately, be platform-
3296 * dependent; for example, FreeBSD's AF_INET6 is 24
3297 * whilst NetBSD's and OpenBSD's is 26.
3299 * This means that, when reading a capture file, just
3300 * checking for our AF_INET6 value won't work if the
3301 * capture file came from another OS.
3310 case ETHERTYPE_IPV6
:
3317 * Not a type on which we support filtering.
3318 * XXX - support those that have AF_ values
3319 * #defined on this platform, at least?
3324 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3326 * The AF_ value is in host byte order, but
3327 * the BPF interpreter will convert it to
3328 * network byte order.
3330 * If this is a save file, and it's from a
3331 * machine with the opposite byte order to
3332 * ours, we byte-swap the AF_ value.
3334 * Then we run it through "htonl()", and
3335 * generate code to compare against the result.
3337 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3338 bpf_pcap
->sf
.swapped
)
3339 proto
= SWAPLONG(proto
);
3340 proto
= htonl(proto
);
3342 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3344 #ifdef HAVE_NET_PFVAR_H
3347 * af field is host byte order in contrast to the rest of
3350 if (proto
== ETHERTYPE_IP
)
3351 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3352 BPF_B
, (bpf_int32
)AF_INET
));
3354 else if (proto
== ETHERTYPE_IPV6
)
3355 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3356 BPF_B
, (bpf_int32
)AF_INET6
));
3362 #endif /* HAVE_NET_PFVAR_H */
3365 case DLT_ARCNET_LINUX
:
3367 * XXX should we check for first fragment if the protocol
3376 case ETHERTYPE_IPV6
:
3377 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3378 (bpf_int32
)ARCTYPE_INET6
));
3382 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3383 (bpf_int32
)ARCTYPE_IP
);
3384 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3385 (bpf_int32
)ARCTYPE_IP_OLD
);
3390 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3391 (bpf_int32
)ARCTYPE_ARP
);
3392 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3393 (bpf_int32
)ARCTYPE_ARP_OLD
);
3397 case ETHERTYPE_REVARP
:
3398 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3399 (bpf_int32
)ARCTYPE_REVARP
));
3401 case ETHERTYPE_ATALK
:
3402 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3403 (bpf_int32
)ARCTYPE_ATALK
));
3410 case ETHERTYPE_ATALK
:
3420 * XXX - assumes a 2-byte Frame Relay header with
3421 * DLCI and flags. What if the address is longer?
3427 * Check for the special NLPID for IP.
3429 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3432 case ETHERTYPE_IPV6
:
3434 * Check for the special NLPID for IPv6.
3436 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3441 * Check for several OSI protocols.
3443 * Frame Relay packets typically have an OSI
3444 * NLPID at the beginning; we check for each
3447 * What we check for is the NLPID and a frame
3448 * control field of UI, i.e. 0x03 followed
3451 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3452 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3453 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3465 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3467 case DLT_JUNIPER_MFR
:
3468 case DLT_JUNIPER_MLFR
:
3469 case DLT_JUNIPER_MLPPP
:
3470 case DLT_JUNIPER_ATM1
:
3471 case DLT_JUNIPER_ATM2
:
3472 case DLT_JUNIPER_PPPOE
:
3473 case DLT_JUNIPER_PPPOE_ATM
:
3474 case DLT_JUNIPER_GGSN
:
3475 case DLT_JUNIPER_ES
:
3476 case DLT_JUNIPER_MONITOR
:
3477 case DLT_JUNIPER_SERVICES
:
3478 case DLT_JUNIPER_ETHER
:
3479 case DLT_JUNIPER_PPP
:
3480 case DLT_JUNIPER_FRELAY
:
3481 case DLT_JUNIPER_CHDLC
:
3482 case DLT_JUNIPER_VP
:
3483 case DLT_JUNIPER_ST
:
3484 case DLT_JUNIPER_ISM
:
3485 case DLT_JUNIPER_VS
:
3486 case DLT_JUNIPER_SRX_E2E
:
3487 case DLT_JUNIPER_FIBRECHANNEL
:
3488 case DLT_JUNIPER_ATM_CEMIC
:
3490 /* just lets verify the magic number for now -
3491 * on ATM we may have up to 6 different encapsulations on the wire
3492 * and need a lot of heuristics to figure out that the payload
3495 * FIXME encapsulation specific BPF_ filters
3497 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3500 return gen_ipnet_linktype(proto
);
3502 case DLT_LINUX_IRDA
:
3503 bpf_error("IrDA link-layer type filtering not implemented");
3506 bpf_error("DOCSIS link-layer type filtering not implemented");
3509 case DLT_MTP2_WITH_PHDR
:
3510 bpf_error("MTP2 link-layer type filtering not implemented");
3513 bpf_error("ERF link-layer type filtering not implemented");
3517 bpf_error("PFSYNC link-layer type filtering not implemented");
3520 case DLT_LINUX_LAPD
:
3521 bpf_error("LAPD link-layer type filtering not implemented");
3525 case DLT_USB_LINUX_MMAPPED
:
3526 bpf_error("USB link-layer type filtering not implemented");
3528 case DLT_BLUETOOTH_HCI_H4
:
3529 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3530 bpf_error("Bluetooth link-layer type filtering not implemented");
3533 case DLT_CAN_SOCKETCAN
:
3534 bpf_error("CAN link-layer type filtering not implemented");
3536 case DLT_IEEE802_15_4
:
3537 case DLT_IEEE802_15_4_LINUX
:
3538 case DLT_IEEE802_15_4_NONASK_PHY
:
3539 case DLT_IEEE802_15_4_NOFCS
:
3540 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3542 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3543 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3546 bpf_error("SITA link-layer type filtering not implemented");
3549 bpf_error("RAIF1 link-layer type filtering not implemented");
3552 bpf_error("IPMB link-layer type filtering not implemented");
3555 bpf_error("AX.25 link-layer type filtering not implemented");
3559 * All the types that have no encapsulation should either be
3560 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3561 * all packets are IP packets, or should be handled in some
3562 * special case, if none of them are (if some are and some
3563 * aren't, the lack of encapsulation is a problem, as we'd
3564 * have to find some other way of determining the packet type).
3566 * Therefore, if "off_linktype" is -1, there's an error.
3568 if (off_linktype
== (u_int
)-1)
3572 * Any type not handled above should always have an Ethernet
3573 * type at an offset of "off_linktype".
3575 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3579 * Check for an LLC SNAP packet with a given organization code and
3580 * protocol type; we check the entire contents of the 802.2 LLC and
3581 * snap headers, checking for DSAP and SSAP of SNAP and a control
3582 * field of 0x03 in the LLC header, and for the specified organization
3583 * code and protocol type in the SNAP header.
3585 static struct block
*
3586 gen_snap(orgcode
, ptype
)
3587 bpf_u_int32 orgcode
;
3590 u_char snapblock
[8];
3592 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3593 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3594 snapblock
[2] = 0x03; /* control = UI */
3595 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3596 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3597 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3598 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3599 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3600 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3604 * Generate code to match a particular packet type, for link-layer types
3605 * using 802.2 LLC headers.
3607 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3608 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3610 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3611 * value, if <= ETHERMTU. We use that to determine whether to
3612 * match the DSAP or both DSAP and LSAP or to check the OUI and
3613 * protocol ID in a SNAP header.
3615 static struct block
*
3616 gen_llc_linktype(proto
)
3620 * XXX - handle token-ring variable-length header.
3626 case LLCSAP_NETBEUI
:
3628 * XXX - should we check both the DSAP and the
3629 * SSAP, like this, or should we check just the
3630 * DSAP, as we do for other types <= ETHERMTU
3631 * (i.e., other SAP values)?
3633 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3634 ((proto
<< 8) | proto
));
3638 * XXX - are there ever SNAP frames for IPX on
3639 * non-Ethernet 802.x networks?
3641 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3642 (bpf_int32
)LLCSAP_IPX
);
3644 case ETHERTYPE_ATALK
:
3646 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3647 * SNAP packets with an organization code of
3648 * 0x080007 (Apple, for Appletalk) and a protocol
3649 * type of ETHERTYPE_ATALK (Appletalk).
3651 * XXX - check for an organization code of
3652 * encapsulated Ethernet as well?
3654 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3658 * XXX - we don't have to check for IPX 802.3
3659 * here, but should we check for the IPX Ethertype?
3661 if (proto
<= ETHERMTU
) {
3663 * This is an LLC SAP value, so check
3666 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3669 * This is an Ethernet type; we assume that it's
3670 * unlikely that it'll appear in the right place
3671 * at random, and therefore check only the
3672 * location that would hold the Ethernet type
3673 * in a SNAP frame with an organization code of
3674 * 0x000000 (encapsulated Ethernet).
3676 * XXX - if we were to check for the SNAP DSAP and
3677 * LSAP, as per XXX, and were also to check for an
3678 * organization code of 0x000000 (encapsulated
3679 * Ethernet), we'd do
3681 * return gen_snap(0x000000, proto);
3683 * here; for now, we don't, as per the above.
3684 * I don't know whether it's worth the extra CPU
3685 * time to do the right check or not.
3687 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3692 static struct block
*
3693 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3697 u_int src_off
, dst_off
;
3699 struct block
*b0
, *b1
;
3713 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3714 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3720 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3721 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3728 b0
= gen_linktype(proto
);
3729 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3735 static struct block
*
3736 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3737 struct in6_addr
*addr
;
3738 struct in6_addr
*mask
;
3740 u_int src_off
, dst_off
;
3742 struct block
*b0
, *b1
;
3757 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3758 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3764 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3765 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3772 /* this order is important */
3773 a
= (u_int32_t
*)addr
;
3774 m
= (u_int32_t
*)mask
;
3775 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3776 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3778 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3780 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3782 b0
= gen_linktype(proto
);
3788 static struct block
*
3789 gen_ehostop(eaddr
, dir
)
3790 register const u_char
*eaddr
;
3793 register struct block
*b0
, *b1
;
3797 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3800 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3803 b0
= gen_ehostop(eaddr
, Q_SRC
);
3804 b1
= gen_ehostop(eaddr
, Q_DST
);
3810 b0
= gen_ehostop(eaddr
, Q_SRC
);
3811 b1
= gen_ehostop(eaddr
, Q_DST
);
3816 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3820 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3824 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3828 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3832 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3836 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3844 * Like gen_ehostop, but for DLT_FDDI
3846 static struct block
*
3847 gen_fhostop(eaddr
, dir
)
3848 register const u_char
*eaddr
;
3851 struct block
*b0
, *b1
;
3856 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3858 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3863 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3865 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3869 b0
= gen_fhostop(eaddr
, Q_SRC
);
3870 b1
= gen_fhostop(eaddr
, Q_DST
);
3876 b0
= gen_fhostop(eaddr
, Q_SRC
);
3877 b1
= gen_fhostop(eaddr
, Q_DST
);
3882 bpf_error("'addr1' is only supported on 802.11");
3886 bpf_error("'addr2' is only supported on 802.11");
3890 bpf_error("'addr3' is only supported on 802.11");
3894 bpf_error("'addr4' is only supported on 802.11");
3898 bpf_error("'ra' is only supported on 802.11");
3902 bpf_error("'ta' is only supported on 802.11");
3910 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3912 static struct block
*
3913 gen_thostop(eaddr
, dir
)
3914 register const u_char
*eaddr
;
3917 register struct block
*b0
, *b1
;
3921 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3924 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3927 b0
= gen_thostop(eaddr
, Q_SRC
);
3928 b1
= gen_thostop(eaddr
, Q_DST
);
3934 b0
= gen_thostop(eaddr
, Q_SRC
);
3935 b1
= gen_thostop(eaddr
, Q_DST
);
3940 bpf_error("'addr1' is only supported on 802.11");
3944 bpf_error("'addr2' is only supported on 802.11");
3948 bpf_error("'addr3' is only supported on 802.11");
3952 bpf_error("'addr4' is only supported on 802.11");
3956 bpf_error("'ra' is only supported on 802.11");
3960 bpf_error("'ta' is only supported on 802.11");
3968 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3969 * various 802.11 + radio headers.
3971 static struct block
*
3972 gen_wlanhostop(eaddr
, dir
)
3973 register const u_char
*eaddr
;
3976 register struct block
*b0
, *b1
, *b2
;
3977 register struct slist
*s
;
3979 #ifdef ENABLE_WLAN_FILTERING_PATCH
3982 * We need to disable the optimizer because the optimizer is buggy
3983 * and wipes out some LD instructions generated by the below
3984 * code to validate the Frame Control bits
3987 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3994 * For control frames, there is no SA.
3996 * For management frames, SA is at an
3997 * offset of 10 from the beginning of
4000 * For data frames, SA is at an offset
4001 * of 10 from the beginning of the packet
4002 * if From DS is clear, at an offset of
4003 * 16 from the beginning of the packet
4004 * if From DS is set and To DS is clear,
4005 * and an offset of 24 from the beginning
4006 * of the packet if From DS is set and To DS
4011 * Generate the tests to be done for data frames
4014 * First, check for To DS set, i.e. check "link[1] & 0x01".
4016 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4017 b1
= new_block(JMP(BPF_JSET
));
4018 b1
->s
.k
= 0x01; /* To DS */
4022 * If To DS is set, the SA is at 24.
4024 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4028 * Now, check for To DS not set, i.e. check
4029 * "!(link[1] & 0x01)".
4031 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4032 b2
= new_block(JMP(BPF_JSET
));
4033 b2
->s
.k
= 0x01; /* To DS */
4038 * If To DS is not set, the SA is at 16.
4040 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4044 * Now OR together the last two checks. That gives
4045 * the complete set of checks for data frames with
4051 * Now check for From DS being set, and AND that with
4052 * the ORed-together checks.
4054 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4055 b1
= new_block(JMP(BPF_JSET
));
4056 b1
->s
.k
= 0x02; /* From DS */
4061 * Now check for data frames with From DS not set.
4063 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4064 b2
= new_block(JMP(BPF_JSET
));
4065 b2
->s
.k
= 0x02; /* From DS */
4070 * If From DS isn't set, the SA is at 10.
4072 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4076 * Now OR together the checks for data frames with
4077 * From DS not set and for data frames with From DS
4078 * set; that gives the checks done for data frames.
4083 * Now check for a data frame.
4084 * I.e, check "link[0] & 0x08".
4086 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4087 b1
= new_block(JMP(BPF_JSET
));
4092 * AND that with the checks done for data frames.
4097 * If the high-order bit of the type value is 0, this
4098 * is a management frame.
4099 * I.e, check "!(link[0] & 0x08)".
4101 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4102 b2
= new_block(JMP(BPF_JSET
));
4108 * For management frames, the SA is at 10.
4110 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4114 * OR that with the checks done for data frames.
4115 * That gives the checks done for management and
4121 * If the low-order bit of the type value is 1,
4122 * this is either a control frame or a frame
4123 * with a reserved type, and thus not a
4126 * I.e., check "!(link[0] & 0x04)".
4128 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4129 b1
= new_block(JMP(BPF_JSET
));
4135 * AND that with the checks for data and management
4145 * For control frames, there is no DA.
4147 * For management frames, DA is at an
4148 * offset of 4 from the beginning of
4151 * For data frames, DA is at an offset
4152 * of 4 from the beginning of the packet
4153 * if To DS is clear and at an offset of
4154 * 16 from the beginning of the packet
4159 * Generate the tests to be done for data frames.
4161 * First, check for To DS set, i.e. "link[1] & 0x01".
4163 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4164 b1
= new_block(JMP(BPF_JSET
));
4165 b1
->s
.k
= 0x01; /* To DS */
4169 * If To DS is set, the DA is at 16.
4171 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4175 * Now, check for To DS not set, i.e. check
4176 * "!(link[1] & 0x01)".
4178 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4179 b2
= new_block(JMP(BPF_JSET
));
4180 b2
->s
.k
= 0x01; /* To DS */
4185 * If To DS is not set, the DA is at 4.
4187 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4191 * Now OR together the last two checks. That gives
4192 * the complete set of checks for data frames.
4197 * Now check for a data frame.
4198 * I.e, check "link[0] & 0x08".
4200 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4201 b1
= new_block(JMP(BPF_JSET
));
4206 * AND that with the checks done for data frames.
4211 * If the high-order bit of the type value is 0, this
4212 * is a management frame.
4213 * I.e, check "!(link[0] & 0x08)".
4215 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4216 b2
= new_block(JMP(BPF_JSET
));
4222 * For management frames, the DA is at 4.
4224 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4228 * OR that with the checks done for data frames.
4229 * That gives the checks done for management and
4235 * If the low-order bit of the type value is 1,
4236 * this is either a control frame or a frame
4237 * with a reserved type, and thus not a
4240 * I.e., check "!(link[0] & 0x04)".
4242 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4243 b1
= new_block(JMP(BPF_JSET
));
4249 * AND that with the checks for data and management
4257 * Not present in management frames; addr1 in other
4262 * If the high-order bit of the type value is 0, this
4263 * is a management frame.
4264 * I.e, check "(link[0] & 0x08)".
4266 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4267 b1
= new_block(JMP(BPF_JSET
));
4274 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4277 * AND that with the check of addr1.
4284 * Not present in management frames; addr2, if present,
4289 * Not present in CTS or ACK control frames.
4291 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4292 IEEE80211_FC0_TYPE_MASK
);
4294 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4295 IEEE80211_FC0_SUBTYPE_MASK
);
4297 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4298 IEEE80211_FC0_SUBTYPE_MASK
);
4304 * If the high-order bit of the type value is 0, this
4305 * is a management frame.
4306 * I.e, check "(link[0] & 0x08)".
4308 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4309 b1
= new_block(JMP(BPF_JSET
));
4314 * AND that with the check for frames other than
4315 * CTS and ACK frames.
4322 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4327 * XXX - add BSSID keyword?
4330 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4334 * Not present in CTS or ACK control frames.
4336 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4337 IEEE80211_FC0_TYPE_MASK
);
4339 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4340 IEEE80211_FC0_SUBTYPE_MASK
);
4342 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4343 IEEE80211_FC0_SUBTYPE_MASK
);
4347 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4353 * Not present in control frames.
4355 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4356 IEEE80211_FC0_TYPE_MASK
);
4358 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4364 * Present only if the direction mask has both "From DS"
4365 * and "To DS" set. Neither control frames nor management
4366 * frames should have both of those set, so we don't
4367 * check the frame type.
4369 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4370 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4371 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4376 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4377 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4383 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4384 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4393 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4394 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4395 * as the RFC states.)
4397 static struct block
*
4398 gen_ipfchostop(eaddr
, dir
)
4399 register const u_char
*eaddr
;
4402 register struct block
*b0
, *b1
;
4406 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4409 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4412 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4413 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4419 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4420 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4425 bpf_error("'addr1' is only supported on 802.11");
4429 bpf_error("'addr2' is only supported on 802.11");
4433 bpf_error("'addr3' is only supported on 802.11");
4437 bpf_error("'addr4' is only supported on 802.11");
4441 bpf_error("'ra' is only supported on 802.11");
4445 bpf_error("'ta' is only supported on 802.11");
4453 * This is quite tricky because there may be pad bytes in front of the
4454 * DECNET header, and then there are two possible data packet formats that
4455 * carry both src and dst addresses, plus 5 packet types in a format that
4456 * carries only the src node, plus 2 types that use a different format and
4457 * also carry just the src node.
4461 * Instead of doing those all right, we just look for data packets with
4462 * 0 or 1 bytes of padding. If you want to look at other packets, that
4463 * will require a lot more hacking.
4465 * To add support for filtering on DECNET "areas" (network numbers)
4466 * one would want to add a "mask" argument to this routine. That would
4467 * make the filter even more inefficient, although one could be clever
4468 * and not generate masking instructions if the mask is 0xFFFF.
4470 static struct block
*
4471 gen_dnhostop(addr
, dir
)
4475 struct block
*b0
, *b1
, *b2
, *tmp
;
4476 u_int offset_lh
; /* offset if long header is received */
4477 u_int offset_sh
; /* offset if short header is received */
4482 offset_sh
= 1; /* follows flags */
4483 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4487 offset_sh
= 3; /* follows flags, dstnode */
4488 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4492 /* Inefficient because we do our Calvinball dance twice */
4493 b0
= gen_dnhostop(addr
, Q_SRC
);
4494 b1
= gen_dnhostop(addr
, Q_DST
);
4500 /* Inefficient because we do our Calvinball dance twice */
4501 b0
= gen_dnhostop(addr
, Q_SRC
);
4502 b1
= gen_dnhostop(addr
, Q_DST
);
4507 bpf_error("ISO host filtering not implemented");
4512 b0
= gen_linktype(ETHERTYPE_DN
);
4513 /* Check for pad = 1, long header case */
4514 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4515 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4516 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4517 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4519 /* Check for pad = 0, long header case */
4520 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4521 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4524 /* Check for pad = 1, short header case */
4525 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4526 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4527 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4530 /* Check for pad = 0, short header case */
4531 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4532 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4536 /* Combine with test for linktype */
4542 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4543 * test the bottom-of-stack bit, and then check the version number
4544 * field in the IP header.
4546 static struct block
*
4547 gen_mpls_linktype(proto
)
4550 struct block
*b0
, *b1
;
4555 /* match the bottom-of-stack bit */
4556 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4557 /* match the IPv4 version number */
4558 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4563 /* match the bottom-of-stack bit */
4564 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4565 /* match the IPv4 version number */
4566 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4575 static struct block
*
4576 gen_host(addr
, mask
, proto
, dir
, type
)
4583 struct block
*b0
, *b1
;
4584 const char *typestr
;
4594 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4596 * Only check for non-IPv4 addresses if we're not
4597 * checking MPLS-encapsulated packets.
4599 if (label_stack_depth
== 0) {
4600 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4602 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4608 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4611 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4614 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4617 bpf_error("'tcp' modifier applied to %s", typestr
);
4620 bpf_error("'sctp' modifier applied to %s", typestr
);
4623 bpf_error("'udp' modifier applied to %s", typestr
);
4626 bpf_error("'icmp' modifier applied to %s", typestr
);
4629 bpf_error("'igmp' modifier applied to %s", typestr
);
4632 bpf_error("'igrp' modifier applied to %s", typestr
);
4635 bpf_error("'pim' modifier applied to %s", typestr
);
4638 bpf_error("'vrrp' modifier applied to %s", typestr
);
4641 bpf_error("ATALK host filtering not implemented");
4644 bpf_error("AARP host filtering not implemented");
4647 return gen_dnhostop(addr
, dir
);
4650 bpf_error("SCA host filtering not implemented");
4653 bpf_error("LAT host filtering not implemented");
4656 bpf_error("MOPDL host filtering not implemented");
4659 bpf_error("MOPRC host filtering not implemented");
4663 bpf_error("'ip6' modifier applied to ip host");
4666 bpf_error("'icmp6' modifier applied to %s", typestr
);
4670 bpf_error("'ah' modifier applied to %s", typestr
);
4673 bpf_error("'esp' modifier applied to %s", typestr
);
4676 bpf_error("ISO host filtering not implemented");
4679 bpf_error("'esis' modifier applied to %s", typestr
);
4682 bpf_error("'isis' modifier applied to %s", typestr
);
4685 bpf_error("'clnp' modifier applied to %s", typestr
);
4688 bpf_error("'stp' modifier applied to %s", typestr
);
4691 bpf_error("IPX host filtering not implemented");
4694 bpf_error("'netbeui' modifier applied to %s", typestr
);
4697 bpf_error("'radio' modifier applied to %s", typestr
);
4706 static struct block
*
4707 gen_host6(addr
, mask
, proto
, dir
, type
)
4708 struct in6_addr
*addr
;
4709 struct in6_addr
*mask
;
4714 const char *typestr
;
4724 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4727 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4730 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4733 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4736 bpf_error("'sctp' modifier applied to %s", typestr
);
4739 bpf_error("'tcp' modifier applied to %s", typestr
);
4742 bpf_error("'udp' modifier applied to %s", typestr
);
4745 bpf_error("'icmp' modifier applied to %s", typestr
);
4748 bpf_error("'igmp' modifier applied to %s", typestr
);
4751 bpf_error("'igrp' modifier applied to %s", typestr
);
4754 bpf_error("'pim' modifier applied to %s", typestr
);
4757 bpf_error("'vrrp' modifier applied to %s", typestr
);
4760 bpf_error("ATALK host filtering not implemented");
4763 bpf_error("AARP host filtering not implemented");
4766 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4769 bpf_error("SCA host filtering not implemented");
4772 bpf_error("LAT host filtering not implemented");
4775 bpf_error("MOPDL host filtering not implemented");
4778 bpf_error("MOPRC host filtering not implemented");
4781 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4784 bpf_error("'icmp6' modifier applied to %s", typestr
);
4787 bpf_error("'ah' modifier applied to %s", typestr
);
4790 bpf_error("'esp' modifier applied to %s", typestr
);
4793 bpf_error("ISO host filtering not implemented");
4796 bpf_error("'esis' modifier applied to %s", typestr
);
4799 bpf_error("'isis' modifier applied to %s", typestr
);
4802 bpf_error("'clnp' modifier applied to %s", typestr
);
4805 bpf_error("'stp' modifier applied to %s", typestr
);
4808 bpf_error("IPX host filtering not implemented");
4811 bpf_error("'netbeui' modifier applied to %s", typestr
);
4814 bpf_error("'radio' modifier applied to %s", typestr
);
4824 static struct block
*
4825 gen_gateway(eaddr
, alist
, proto
, dir
)
4826 const u_char
*eaddr
;
4827 bpf_u_int32
**alist
;
4831 struct block
*b0
, *b1
, *tmp
;
4834 bpf_error("direction applied to 'gateway'");
4843 b0
= gen_ehostop(eaddr
, Q_OR
);
4846 b0
= gen_fhostop(eaddr
, Q_OR
);
4849 b0
= gen_thostop(eaddr
, Q_OR
);
4851 case DLT_IEEE802_11
:
4852 case DLT_PRISM_HEADER
:
4853 case DLT_IEEE802_11_RADIO_AVS
:
4854 case DLT_IEEE802_11_RADIO
:
4856 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4861 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4863 * Check that the packet doesn't begin with an
4864 * LE Control marker. (We've already generated
4867 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4872 * Now check the MAC address.
4874 b0
= gen_ehostop(eaddr
, Q_OR
);
4877 case DLT_IP_OVER_FC
:
4878 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4882 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4884 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4886 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4895 bpf_error("illegal modifier of 'gateway'");
4901 gen_proto_abbrev(proto
)
4910 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4912 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4918 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4920 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4926 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4928 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4934 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4937 #ifndef IPPROTO_IGMP
4938 #define IPPROTO_IGMP 2
4942 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4945 #ifndef IPPROTO_IGRP
4946 #define IPPROTO_IGRP 9
4949 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4953 #define IPPROTO_PIM 103
4957 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4959 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4964 #ifndef IPPROTO_VRRP
4965 #define IPPROTO_VRRP 112
4969 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4973 b1
= gen_linktype(ETHERTYPE_IP
);
4977 b1
= gen_linktype(ETHERTYPE_ARP
);
4981 b1
= gen_linktype(ETHERTYPE_REVARP
);
4985 bpf_error("link layer applied in wrong context");
4988 b1
= gen_linktype(ETHERTYPE_ATALK
);
4992 b1
= gen_linktype(ETHERTYPE_AARP
);
4996 b1
= gen_linktype(ETHERTYPE_DN
);
5000 b1
= gen_linktype(ETHERTYPE_SCA
);
5004 b1
= gen_linktype(ETHERTYPE_LAT
);
5008 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5012 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5017 b1
= gen_linktype(ETHERTYPE_IPV6
);
5020 #ifndef IPPROTO_ICMPV6
5021 #define IPPROTO_ICMPV6 58
5024 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5029 #define IPPROTO_AH 51
5032 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5034 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5040 #define IPPROTO_ESP 50
5043 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5045 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5051 b1
= gen_linktype(LLCSAP_ISONS
);
5055 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5059 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5062 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5063 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5064 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5066 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5068 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5070 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5074 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5075 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5076 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5078 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5080 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5082 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5086 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5087 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5088 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5090 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5095 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5096 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5101 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5102 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5104 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5106 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5111 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5112 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5117 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5118 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5123 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5127 b1
= gen_linktype(LLCSAP_8021D
);
5131 b1
= gen_linktype(LLCSAP_IPX
);
5135 b1
= gen_linktype(LLCSAP_NETBEUI
);
5139 bpf_error("'radio' is not a valid protocol type");
5147 static struct block
*
5154 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5155 b
= new_block(JMP(BPF_JSET
));
5164 * Generate a comparison to a port value in the transport-layer header
5165 * at the specified offset from the beginning of that header.
5167 * XXX - this handles a variable-length prefix preceding the link-layer
5168 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5169 * variable-length link-layer headers (such as Token Ring or 802.11
5172 static struct block
*
5173 gen_portatom(off
, v
)
5177 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5181 static struct block
*
5182 gen_portatom6(off
, v
)
5186 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5191 gen_portop(port
, proto
, dir
)
5192 int port
, proto
, dir
;
5194 struct block
*b0
, *b1
, *tmp
;
5196 /* ip proto 'proto' */
5197 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5203 b1
= gen_portatom(0, (bpf_int32
)port
);
5207 b1
= gen_portatom(2, (bpf_int32
)port
);
5212 tmp
= gen_portatom(0, (bpf_int32
)port
);
5213 b1
= gen_portatom(2, (bpf_int32
)port
);
5218 tmp
= gen_portatom(0, (bpf_int32
)port
);
5219 b1
= gen_portatom(2, (bpf_int32
)port
);
5231 static struct block
*
5232 gen_port(port
, ip_proto
, dir
)
5237 struct block
*b0
, *b1
, *tmp
;
5242 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5243 * not LLC encapsulation with LLCSAP_IP.
5245 * For IEEE 802 networks - which includes 802.5 token ring
5246 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5247 * says that SNAP encapsulation is used, not LLC encapsulation
5250 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5251 * RFC 2225 say that SNAP encapsulation is used, not LLC
5252 * encapsulation with LLCSAP_IP.
5254 * So we always check for ETHERTYPE_IP.
5256 b0
= gen_linktype(ETHERTYPE_IP
);
5262 b1
= gen_portop(port
, ip_proto
, dir
);
5266 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5267 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5269 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5282 gen_portop6(port
, proto
, dir
)
5283 int port
, proto
, dir
;
5285 struct block
*b0
, *b1
, *tmp
;
5287 /* ip6 proto 'proto' */
5288 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5292 b1
= gen_portatom6(0, (bpf_int32
)port
);
5296 b1
= gen_portatom6(2, (bpf_int32
)port
);
5301 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5302 b1
= gen_portatom6(2, (bpf_int32
)port
);
5307 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5308 b1
= gen_portatom6(2, (bpf_int32
)port
);
5320 static struct block
*
5321 gen_port6(port
, ip_proto
, dir
)
5326 struct block
*b0
, *b1
, *tmp
;
5328 /* link proto ip6 */
5329 b0
= gen_linktype(ETHERTYPE_IPV6
);
5335 b1
= gen_portop6(port
, ip_proto
, dir
);
5339 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5340 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5342 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5354 /* gen_portrange code */
5355 static struct block
*
5356 gen_portrangeatom(off
, v1
, v2
)
5360 struct block
*b1
, *b2
;
5364 * Reverse the order of the ports, so v1 is the lower one.
5373 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5374 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5382 gen_portrangeop(port1
, port2
, proto
, dir
)
5387 struct block
*b0
, *b1
, *tmp
;
5389 /* ip proto 'proto' */
5390 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5396 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5400 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5405 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5406 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5411 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5412 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5424 static struct block
*
5425 gen_portrange(port1
, port2
, ip_proto
, dir
)
5430 struct block
*b0
, *b1
, *tmp
;
5433 b0
= gen_linktype(ETHERTYPE_IP
);
5439 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5443 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5444 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5446 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5458 static struct block
*
5459 gen_portrangeatom6(off
, v1
, v2
)
5463 struct block
*b1
, *b2
;
5467 * Reverse the order of the ports, so v1 is the lower one.
5476 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5477 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5485 gen_portrangeop6(port1
, port2
, proto
, dir
)
5490 struct block
*b0
, *b1
, *tmp
;
5492 /* ip6 proto 'proto' */
5493 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5497 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5501 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5506 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5507 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5512 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5513 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5525 static struct block
*
5526 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5531 struct block
*b0
, *b1
, *tmp
;
5533 /* link proto ip6 */
5534 b0
= gen_linktype(ETHERTYPE_IPV6
);
5540 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5544 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5545 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5547 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5560 lookup_proto(name
, proto
)
5561 register const char *name
;
5571 v
= pcap_nametoproto(name
);
5572 if (v
== PROTO_UNDEF
)
5573 bpf_error("unknown ip proto '%s'", name
);
5577 /* XXX should look up h/w protocol type based on linktype */
5578 v
= pcap_nametoeproto(name
);
5579 if (v
== PROTO_UNDEF
) {
5580 v
= pcap_nametollc(name
);
5581 if (v
== PROTO_UNDEF
)
5582 bpf_error("unknown ether proto '%s'", name
);
5587 if (strcmp(name
, "esis") == 0)
5589 else if (strcmp(name
, "isis") == 0)
5591 else if (strcmp(name
, "clnp") == 0)
5594 bpf_error("unknown osi proto '%s'", name
);
5614 static struct block
*
5615 gen_protochain(v
, proto
, dir
)
5620 #ifdef NO_PROTOCHAIN
5621 return gen_proto(v
, proto
, dir
);
5623 struct block
*b0
, *b
;
5624 struct slist
*s
[100];
5625 int fix2
, fix3
, fix4
, fix5
;
5626 int ahcheck
, again
, end
;
5628 int reg2
= alloc_reg();
5630 memset(s
, 0, sizeof(s
));
5631 fix2
= fix3
= fix4
= fix5
= 0;
5638 b0
= gen_protochain(v
, Q_IP
, dir
);
5639 b
= gen_protochain(v
, Q_IPV6
, dir
);
5643 bpf_error("bad protocol applied for 'protochain'");
5648 * We don't handle variable-length prefixes before the link-layer
5649 * header, or variable-length link-layer headers, here yet.
5650 * We might want to add BPF instructions to do the protochain
5651 * work, to simplify that and, on platforms that have a BPF
5652 * interpreter with the new instructions, let the filtering
5653 * be done in the kernel. (We already require a modified BPF
5654 * engine to do the protochain stuff, to support backward
5655 * branches, and backward branch support is unlikely to appear
5656 * in kernel BPF engines.)
5660 case DLT_IEEE802_11
:
5661 case DLT_PRISM_HEADER
:
5662 case DLT_IEEE802_11_RADIO_AVS
:
5663 case DLT_IEEE802_11_RADIO
:
5665 bpf_error("'protochain' not supported with 802.11");
5668 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5671 * s[0] is a dummy entry to protect other BPF insn from damage
5672 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5673 * hard to find interdependency made by jump table fixup.
5676 s
[i
] = new_stmt(0); /*dummy*/
5681 b0
= gen_linktype(ETHERTYPE_IP
);
5684 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5685 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5687 /* X = ip->ip_hl << 2 */
5688 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5689 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5694 b0
= gen_linktype(ETHERTYPE_IPV6
);
5696 /* A = ip6->ip_nxt */
5697 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5698 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5700 /* X = sizeof(struct ip6_hdr) */
5701 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5707 bpf_error("unsupported proto to gen_protochain");
5711 /* again: if (A == v) goto end; else fall through; */
5713 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5715 s
[i
]->s
.jt
= NULL
; /*later*/
5716 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5720 #ifndef IPPROTO_NONE
5721 #define IPPROTO_NONE 59
5723 /* if (A == IPPROTO_NONE) goto end */
5724 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5725 s
[i
]->s
.jt
= NULL
; /*later*/
5726 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5727 s
[i
]->s
.k
= IPPROTO_NONE
;
5728 s
[fix5
]->s
.jf
= s
[i
];
5733 if (proto
== Q_IPV6
) {
5734 int v6start
, v6end
, v6advance
, j
;
5737 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5738 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5739 s
[i
]->s
.jt
= NULL
; /*later*/
5740 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5741 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5742 s
[fix2
]->s
.jf
= s
[i
];
5744 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5745 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5746 s
[i
]->s
.jt
= NULL
; /*later*/
5747 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5748 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5750 /* if (A == IPPROTO_ROUTING) goto v6advance */
5751 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5752 s
[i
]->s
.jt
= NULL
; /*later*/
5753 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5754 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5756 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5757 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5758 s
[i
]->s
.jt
= NULL
; /*later*/
5759 s
[i
]->s
.jf
= NULL
; /*later*/
5760 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5771 * X = X + (P[X + 1] + 1) * 8;
5774 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5776 /* A = P[X + packet head] */
5777 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5778 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5781 s
[i
] = new_stmt(BPF_ST
);
5785 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5788 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5792 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5794 /* A = P[X + packet head]; */
5795 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5796 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5799 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5803 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5807 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5810 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5814 /* goto again; (must use BPF_JA for backward jump) */
5815 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5816 s
[i
]->s
.k
= again
- i
- 1;
5817 s
[i
- 1]->s
.jf
= s
[i
];
5821 for (j
= v6start
; j
<= v6end
; j
++)
5822 s
[j
]->s
.jt
= s
[v6advance
];
5827 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5829 s
[fix2
]->s
.jf
= s
[i
];
5835 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5836 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5837 s
[i
]->s
.jt
= NULL
; /*later*/
5838 s
[i
]->s
.jf
= NULL
; /*later*/
5839 s
[i
]->s
.k
= IPPROTO_AH
;
5841 s
[fix3
]->s
.jf
= s
[ahcheck
];
5848 * X = X + (P[X + 1] + 2) * 4;
5851 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5853 /* A = P[X + packet head]; */
5854 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5855 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5858 s
[i
] = new_stmt(BPF_ST
);
5862 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5865 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5869 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5871 /* A = P[X + packet head] */
5872 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5873 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5876 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5880 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5884 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5887 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5891 /* goto again; (must use BPF_JA for backward jump) */
5892 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5893 s
[i
]->s
.k
= again
- i
- 1;
5898 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5900 s
[fix2
]->s
.jt
= s
[end
];
5901 s
[fix4
]->s
.jf
= s
[end
];
5902 s
[fix5
]->s
.jt
= s
[end
];
5909 for (i
= 0; i
< max
- 1; i
++)
5910 s
[i
]->next
= s
[i
+ 1];
5911 s
[max
- 1]->next
= NULL
;
5916 b
= new_block(JMP(BPF_JEQ
));
5917 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5927 static struct block
*
5928 gen_check_802_11_data_frame()
5931 struct block
*b0
, *b1
;
5934 * A data frame has the 0x08 bit (b3) in the frame control field set
5935 * and the 0x04 bit (b2) clear.
5937 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5938 b0
= new_block(JMP(BPF_JSET
));
5942 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5943 b1
= new_block(JMP(BPF_JSET
));
5954 * Generate code that checks whether the packet is a packet for protocol
5955 * <proto> and whether the type field in that protocol's header has
5956 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5957 * IP packet and checks the protocol number in the IP header against <v>.
5959 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5960 * against Q_IP and Q_IPV6.
5962 static struct block
*
5963 gen_proto(v
, proto
, dir
)
5968 struct block
*b0
, *b1
;
5970 if (dir
!= Q_DEFAULT
)
5971 bpf_error("direction applied to 'proto'");
5976 b0
= gen_proto(v
, Q_IP
, dir
);
5977 b1
= gen_proto(v
, Q_IPV6
, dir
);
5985 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5986 * not LLC encapsulation with LLCSAP_IP.
5988 * For IEEE 802 networks - which includes 802.5 token ring
5989 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5990 * says that SNAP encapsulation is used, not LLC encapsulation
5993 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5994 * RFC 2225 say that SNAP encapsulation is used, not LLC
5995 * encapsulation with LLCSAP_IP.
5997 * So we always check for ETHERTYPE_IP.
5999 b0
= gen_linktype(ETHERTYPE_IP
);
6001 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6003 b1
= gen_protochain(v
, Q_IP
);
6013 * Frame Relay packets typically have an OSI
6014 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6015 * generates code to check for all the OSI
6016 * NLPIDs, so calling it and then adding a check
6017 * for the particular NLPID for which we're
6018 * looking is bogus, as we can just check for
6021 * What we check for is the NLPID and a frame
6022 * control field value of UI, i.e. 0x03 followed
6025 * XXX - assumes a 2-byte Frame Relay header with
6026 * DLCI and flags. What if the address is longer?
6028 * XXX - what about SNAP-encapsulated frames?
6030 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6036 * Cisco uses an Ethertype lookalike - for OSI,
6039 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6040 /* OSI in C-HDLC is stuffed with a fudge byte */
6041 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6046 b0
= gen_linktype(LLCSAP_ISONS
);
6047 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6053 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6055 * 4 is the offset of the PDU type relative to the IS-IS
6058 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6063 bpf_error("arp does not encapsulate another protocol");
6067 bpf_error("rarp does not encapsulate another protocol");
6071 bpf_error("atalk encapsulation is not specifiable");
6075 bpf_error("decnet encapsulation is not specifiable");
6079 bpf_error("sca does not encapsulate another protocol");
6083 bpf_error("lat does not encapsulate another protocol");
6087 bpf_error("moprc does not encapsulate another protocol");
6091 bpf_error("mopdl does not encapsulate another protocol");
6095 return gen_linktype(v
);
6098 bpf_error("'udp proto' is bogus");
6102 bpf_error("'tcp proto' is bogus");
6106 bpf_error("'sctp proto' is bogus");
6110 bpf_error("'icmp proto' is bogus");
6114 bpf_error("'igmp proto' is bogus");
6118 bpf_error("'igrp proto' is bogus");
6122 bpf_error("'pim proto' is bogus");
6126 bpf_error("'vrrp proto' is bogus");
6131 b0
= gen_linktype(ETHERTYPE_IPV6
);
6133 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6135 b1
= gen_protochain(v
, Q_IPV6
);
6141 bpf_error("'icmp6 proto' is bogus");
6145 bpf_error("'ah proto' is bogus");
6148 bpf_error("'ah proto' is bogus");
6151 bpf_error("'stp proto' is bogus");
6154 bpf_error("'ipx proto' is bogus");
6157 bpf_error("'netbeui proto' is bogus");
6160 bpf_error("'radio proto' is bogus");
6171 register const char *name
;
6174 int proto
= q
.proto
;
6178 bpf_u_int32 mask
, addr
;
6180 bpf_u_int32
**alist
;
6183 struct sockaddr_in
*sin4
;
6184 struct sockaddr_in6
*sin6
;
6185 struct addrinfo
*res
, *res0
;
6186 struct in6_addr mask128
;
6188 struct block
*b
, *tmp
;
6189 int port
, real_proto
;
6195 addr
= pcap_nametonetaddr(name
);
6197 bpf_error("unknown network '%s'", name
);
6198 /* Left justify network addr and calculate its network mask */
6200 while (addr
&& (addr
& 0xff000000) == 0) {
6204 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6208 if (proto
== Q_LINK
) {
6212 eaddr
= pcap_ether_hostton(name
);
6215 "unknown ether host '%s'", name
);
6216 b
= gen_ehostop(eaddr
, dir
);
6221 eaddr
= pcap_ether_hostton(name
);
6224 "unknown FDDI host '%s'", name
);
6225 b
= gen_fhostop(eaddr
, dir
);
6230 eaddr
= pcap_ether_hostton(name
);
6233 "unknown token ring host '%s'", name
);
6234 b
= gen_thostop(eaddr
, dir
);
6238 case DLT_IEEE802_11
:
6239 case DLT_PRISM_HEADER
:
6240 case DLT_IEEE802_11_RADIO_AVS
:
6241 case DLT_IEEE802_11_RADIO
:
6243 eaddr
= pcap_ether_hostton(name
);
6246 "unknown 802.11 host '%s'", name
);
6247 b
= gen_wlanhostop(eaddr
, dir
);
6251 case DLT_IP_OVER_FC
:
6252 eaddr
= pcap_ether_hostton(name
);
6255 "unknown Fibre Channel host '%s'", name
);
6256 b
= gen_ipfchostop(eaddr
, dir
);
6265 * Check that the packet doesn't begin
6266 * with an LE Control marker. (We've
6267 * already generated a test for LANE.)
6269 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6273 eaddr
= pcap_ether_hostton(name
);
6276 "unknown ether host '%s'", name
);
6277 b
= gen_ehostop(eaddr
, dir
);
6283 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6284 } else if (proto
== Q_DECNET
) {
6285 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6287 * I don't think DECNET hosts can be multihomed, so
6288 * there is no need to build up a list of addresses
6290 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6293 alist
= pcap_nametoaddr(name
);
6294 if (alist
== NULL
|| *alist
== NULL
)
6295 bpf_error("unknown host '%s'", name
);
6297 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6299 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6301 tmp
= gen_host(**alist
++, 0xffffffff,
6302 tproto
, dir
, q
.addr
);
6308 memset(&mask128
, 0xff, sizeof(mask128
));
6309 res0
= res
= pcap_nametoaddrinfo(name
);
6311 bpf_error("unknown host '%s'", name
);
6314 tproto
= tproto6
= proto
;
6315 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6319 for (res
= res0
; res
; res
= res
->ai_next
) {
6320 switch (res
->ai_family
) {
6322 if (tproto
== Q_IPV6
)
6325 sin4
= (struct sockaddr_in
*)
6327 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6328 0xffffffff, tproto
, dir
, q
.addr
);
6331 if (tproto6
== Q_IP
)
6334 sin6
= (struct sockaddr_in6
*)
6336 tmp
= gen_host6(&sin6
->sin6_addr
,
6337 &mask128
, tproto6
, dir
, q
.addr
);
6349 bpf_error("unknown host '%s'%s", name
,
6350 (proto
== Q_DEFAULT
)
6352 : " for specified address family");
6359 if (proto
!= Q_DEFAULT
&&
6360 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6361 bpf_error("illegal qualifier of 'port'");
6362 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6363 bpf_error("unknown port '%s'", name
);
6364 if (proto
== Q_UDP
) {
6365 if (real_proto
== IPPROTO_TCP
)
6366 bpf_error("port '%s' is tcp", name
);
6367 else if (real_proto
== IPPROTO_SCTP
)
6368 bpf_error("port '%s' is sctp", name
);
6370 /* override PROTO_UNDEF */
6371 real_proto
= IPPROTO_UDP
;
6373 if (proto
== Q_TCP
) {
6374 if (real_proto
== IPPROTO_UDP
)
6375 bpf_error("port '%s' is udp", name
);
6377 else if (real_proto
== IPPROTO_SCTP
)
6378 bpf_error("port '%s' is sctp", name
);
6380 /* override PROTO_UNDEF */
6381 real_proto
= IPPROTO_TCP
;
6383 if (proto
== Q_SCTP
) {
6384 if (real_proto
== IPPROTO_UDP
)
6385 bpf_error("port '%s' is udp", name
);
6387 else if (real_proto
== IPPROTO_TCP
)
6388 bpf_error("port '%s' is tcp", name
);
6390 /* override PROTO_UNDEF */
6391 real_proto
= IPPROTO_SCTP
;
6394 bpf_error("illegal port number %d < 0", port
);
6396 bpf_error("illegal port number %d > 65535", port
);
6398 return gen_port(port
, real_proto
, dir
);
6400 b
= gen_port(port
, real_proto
, dir
);
6401 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6406 if (proto
!= Q_DEFAULT
&&
6407 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6408 bpf_error("illegal qualifier of 'portrange'");
6409 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6410 bpf_error("unknown port in range '%s'", name
);
6411 if (proto
== Q_UDP
) {
6412 if (real_proto
== IPPROTO_TCP
)
6413 bpf_error("port in range '%s' is tcp", name
);
6414 else if (real_proto
== IPPROTO_SCTP
)
6415 bpf_error("port in range '%s' is sctp", name
);
6417 /* override PROTO_UNDEF */
6418 real_proto
= IPPROTO_UDP
;
6420 if (proto
== Q_TCP
) {
6421 if (real_proto
== IPPROTO_UDP
)
6422 bpf_error("port in range '%s' is udp", name
);
6423 else if (real_proto
== IPPROTO_SCTP
)
6424 bpf_error("port in range '%s' is sctp", name
);
6426 /* override PROTO_UNDEF */
6427 real_proto
= IPPROTO_TCP
;
6429 if (proto
== Q_SCTP
) {
6430 if (real_proto
== IPPROTO_UDP
)
6431 bpf_error("port in range '%s' is udp", name
);
6432 else if (real_proto
== IPPROTO_TCP
)
6433 bpf_error("port in range '%s' is tcp", name
);
6435 /* override PROTO_UNDEF */
6436 real_proto
= IPPROTO_SCTP
;
6439 bpf_error("illegal port number %d < 0", port1
);
6441 bpf_error("illegal port number %d > 65535", port1
);
6443 bpf_error("illegal port number %d < 0", port2
);
6445 bpf_error("illegal port number %d > 65535", port2
);
6448 return gen_portrange(port1
, port2
, real_proto
, dir
);
6450 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6451 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6457 eaddr
= pcap_ether_hostton(name
);
6459 bpf_error("unknown ether host: %s", name
);
6461 alist
= pcap_nametoaddr(name
);
6462 if (alist
== NULL
|| *alist
== NULL
)
6463 bpf_error("unknown host '%s'", name
);
6464 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6468 bpf_error("'gateway' not supported in this configuration");
6472 real_proto
= lookup_proto(name
, proto
);
6473 if (real_proto
>= 0)
6474 return gen_proto(real_proto
, proto
, dir
);
6476 bpf_error("unknown protocol: %s", name
);
6479 real_proto
= lookup_proto(name
, proto
);
6480 if (real_proto
>= 0)
6481 return gen_protochain(real_proto
, proto
, dir
);
6483 bpf_error("unknown protocol: %s", name
);
6494 gen_mcode(s1
, s2
, masklen
, q
)
6495 register const char *s1
, *s2
;
6496 register int masklen
;
6499 register int nlen
, mlen
;
6502 nlen
= __pcap_atoin(s1
, &n
);
6503 /* Promote short ipaddr */
6507 mlen
= __pcap_atoin(s2
, &m
);
6508 /* Promote short ipaddr */
6511 bpf_error("non-network bits set in \"%s mask %s\"",
6514 /* Convert mask len to mask */
6516 bpf_error("mask length must be <= 32");
6519 * X << 32 is not guaranteed by C to be 0; it's
6524 m
= 0xffffffff << (32 - masklen
);
6526 bpf_error("non-network bits set in \"%s/%d\"",
6533 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6536 bpf_error("Mask syntax for networks only");
6545 register const char *s
;
6550 int proto
= q
.proto
;
6556 else if (q
.proto
== Q_DECNET
)
6557 vlen
= __pcap_atodn(s
, &v
);
6559 vlen
= __pcap_atoin(s
, &v
);
6566 if (proto
== Q_DECNET
)
6567 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6568 else if (proto
== Q_LINK
) {
6569 bpf_error("illegal link layer address");
6572 if (s
== NULL
&& q
.addr
== Q_NET
) {
6573 /* Promote short net number */
6574 while (v
&& (v
& 0xff000000) == 0) {
6579 /* Promote short ipaddr */
6583 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6588 proto
= IPPROTO_UDP
;
6589 else if (proto
== Q_TCP
)
6590 proto
= IPPROTO_TCP
;
6591 else if (proto
== Q_SCTP
)
6592 proto
= IPPROTO_SCTP
;
6593 else if (proto
== Q_DEFAULT
)
6594 proto
= PROTO_UNDEF
;
6596 bpf_error("illegal qualifier of 'port'");
6599 bpf_error("illegal port number %u > 65535", v
);
6602 return gen_port((int)v
, proto
, dir
);
6606 b
= gen_port((int)v
, proto
, dir
);
6607 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6614 proto
= IPPROTO_UDP
;
6615 else if (proto
== Q_TCP
)
6616 proto
= IPPROTO_TCP
;
6617 else if (proto
== Q_SCTP
)
6618 proto
= IPPROTO_SCTP
;
6619 else if (proto
== Q_DEFAULT
)
6620 proto
= PROTO_UNDEF
;
6622 bpf_error("illegal qualifier of 'portrange'");
6625 bpf_error("illegal port number %u > 65535", v
);
6628 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6632 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6633 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6639 bpf_error("'gateway' requires a name");
6643 return gen_proto((int)v
, proto
, dir
);
6646 return gen_protochain((int)v
, proto
, dir
);
6661 gen_mcode6(s1
, s2
, masklen
, q
)
6662 register const char *s1
, *s2
;
6663 register int masklen
;
6666 struct addrinfo
*res
;
6667 struct in6_addr
*addr
;
6668 struct in6_addr mask
;
6673 bpf_error("no mask %s supported", s2
);
6675 res
= pcap_nametoaddrinfo(s1
);
6677 bpf_error("invalid ip6 address %s", s1
);
6680 bpf_error("%s resolved to multiple address", s1
);
6681 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6683 if (sizeof(mask
) * 8 < masklen
)
6684 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6685 memset(&mask
, 0, sizeof(mask
));
6686 memset(&mask
, 0xff, masklen
/ 8);
6688 mask
.s6_addr
[masklen
/ 8] =
6689 (0xff << (8 - masklen
% 8)) & 0xff;
6692 a
= (u_int32_t
*)addr
;
6693 m
= (u_int32_t
*)&mask
;
6694 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6695 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6696 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6704 bpf_error("Mask syntax for networks only");
6708 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6714 bpf_error("invalid qualifier against IPv6 address");
6723 register const u_char
*eaddr
;
6726 struct block
*b
, *tmp
;
6728 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6731 return gen_ehostop(eaddr
, (int)q
.dir
);
6733 return gen_fhostop(eaddr
, (int)q
.dir
);
6735 return gen_thostop(eaddr
, (int)q
.dir
);
6736 case DLT_IEEE802_11
:
6737 case DLT_PRISM_HEADER
:
6738 case DLT_IEEE802_11_RADIO_AVS
:
6739 case DLT_IEEE802_11_RADIO
:
6741 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6745 * Check that the packet doesn't begin with an
6746 * LE Control marker. (We've already generated
6749 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6754 * Now check the MAC address.
6756 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6761 case DLT_IP_OVER_FC
:
6762 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6764 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6768 bpf_error("ethernet address used in non-ether expression");
6775 struct slist
*s0
, *s1
;
6778 * This is definitely not the best way to do this, but the
6779 * lists will rarely get long.
6786 static struct slist
*
6792 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6797 static struct slist
*
6803 s
= new_stmt(BPF_LD
|BPF_MEM
);
6809 * Modify "index" to use the value stored into its register as an
6810 * offset relative to the beginning of the header for the protocol
6811 * "proto", and allocate a register and put an item "size" bytes long
6812 * (1, 2, or 4) at that offset into that register, making it the register
6816 gen_load(proto
, inst
, size
)
6821 struct slist
*s
, *tmp
;
6823 int regno
= alloc_reg();
6825 free_reg(inst
->regno
);
6829 bpf_error("data size must be 1, 2, or 4");
6845 bpf_error("unsupported index operation");
6849 * The offset is relative to the beginning of the packet
6850 * data, if we have a radio header. (If we don't, this
6853 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6854 linktype
!= DLT_IEEE802_11_RADIO
&&
6855 linktype
!= DLT_PRISM_HEADER
)
6856 bpf_error("radio information not present in capture");
6859 * Load into the X register the offset computed into the
6860 * register specified by "index".
6862 s
= xfer_to_x(inst
);
6865 * Load the item at that offset.
6867 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6869 sappend(inst
->s
, s
);
6874 * The offset is relative to the beginning of
6875 * the link-layer header.
6877 * XXX - what about ATM LANE? Should the index be
6878 * relative to the beginning of the AAL5 frame, so
6879 * that 0 refers to the beginning of the LE Control
6880 * field, or relative to the beginning of the LAN
6881 * frame, so that 0 refers, for Ethernet LANE, to
6882 * the beginning of the destination address?
6884 s
= gen_llprefixlen();
6887 * If "s" is non-null, it has code to arrange that the
6888 * X register contains the length of the prefix preceding
6889 * the link-layer header. Add to it the offset computed
6890 * into the register specified by "index", and move that
6891 * into the X register. Otherwise, just load into the X
6892 * register the offset computed into the register specified
6896 sappend(s
, xfer_to_a(inst
));
6897 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6898 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6900 s
= xfer_to_x(inst
);
6903 * Load the item at the sum of the offset we've put in the
6904 * X register and the offset of the start of the link
6905 * layer header (which is 0 if the radio header is
6906 * variable-length; that header length is what we put
6907 * into the X register and then added to the index).
6909 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6912 sappend(inst
->s
, s
);
6928 * The offset is relative to the beginning of
6929 * the network-layer header.
6930 * XXX - are there any cases where we want
6933 s
= gen_off_macpl();
6936 * If "s" is non-null, it has code to arrange that the
6937 * X register contains the offset of the MAC-layer
6938 * payload. Add to it the offset computed into the
6939 * register specified by "index", and move that into
6940 * the X register. Otherwise, just load into the X
6941 * register the offset computed into the register specified
6945 sappend(s
, xfer_to_a(inst
));
6946 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6947 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6949 s
= xfer_to_x(inst
);
6952 * Load the item at the sum of the offset we've put in the
6953 * X register, the offset of the start of the network
6954 * layer header from the beginning of the MAC-layer
6955 * payload, and the purported offset of the start of the
6956 * MAC-layer payload (which might be 0 if there's a
6957 * variable-length prefix before the link-layer header
6958 * or the link-layer header itself is variable-length;
6959 * the variable-length offset of the start of the
6960 * MAC-layer payload is what we put into the X register
6961 * and then added to the index).
6963 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6964 tmp
->s
.k
= off_macpl
+ off_nl
;
6966 sappend(inst
->s
, s
);
6969 * Do the computation only if the packet contains
6970 * the protocol in question.
6972 b
= gen_proto_abbrev(proto
);
6974 gen_and(inst
->b
, b
);
6987 * The offset is relative to the beginning of
6988 * the transport-layer header.
6990 * Load the X register with the length of the IPv4 header
6991 * (plus the offset of the link-layer header, if it's
6992 * a variable-length header), in bytes.
6994 * XXX - are there any cases where we want
6996 * XXX - we should, if we're built with
6997 * IPv6 support, generate code to load either
6998 * IPv4, IPv6, or both, as appropriate.
7000 s
= gen_loadx_iphdrlen();
7003 * The X register now contains the sum of the length
7004 * of any variable-length header preceding the link-layer
7005 * header, any variable-length link-layer header, and the
7006 * length of the network-layer header.
7008 * Load into the A register the offset relative to
7009 * the beginning of the transport layer header,
7010 * add the X register to that, move that to the
7011 * X register, and load with an offset from the
7012 * X register equal to the offset of the network
7013 * layer header relative to the beginning of
7014 * the MAC-layer payload plus the fixed-length
7015 * portion of the offset of the MAC-layer payload
7016 * from the beginning of the raw packet data.
7018 sappend(s
, xfer_to_a(inst
));
7019 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7020 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7021 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7022 tmp
->s
.k
= off_macpl
+ off_nl
;
7023 sappend(inst
->s
, s
);
7026 * Do the computation only if the packet contains
7027 * the protocol in question - which is true only
7028 * if this is an IP datagram and is the first or
7029 * only fragment of that datagram.
7031 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7033 gen_and(inst
->b
, b
);
7035 gen_and(gen_proto_abbrev(Q_IP
), b
);
7041 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7045 inst
->regno
= regno
;
7046 s
= new_stmt(BPF_ST
);
7048 sappend(inst
->s
, s
);
7054 gen_relation(code
, a0
, a1
, reversed
)
7056 struct arth
*a0
, *a1
;
7059 struct slist
*s0
, *s1
, *s2
;
7060 struct block
*b
, *tmp
;
7064 if (code
== BPF_JEQ
) {
7065 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7066 b
= new_block(JMP(code
));
7070 b
= new_block(BPF_JMP
|code
|BPF_X
);
7076 sappend(a0
->s
, a1
->s
);
7080 free_reg(a0
->regno
);
7081 free_reg(a1
->regno
);
7083 /* 'and' together protocol checks */
7086 gen_and(a0
->b
, tmp
= a1
->b
);
7102 int regno
= alloc_reg();
7103 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7106 s
= new_stmt(BPF_LD
|BPF_LEN
);
7107 s
->next
= new_stmt(BPF_ST
);
7108 s
->next
->s
.k
= regno
;
7123 a
= (struct arth
*)newchunk(sizeof(*a
));
7127 s
= new_stmt(BPF_LD
|BPF_IMM
);
7129 s
->next
= new_stmt(BPF_ST
);
7145 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7148 s
= new_stmt(BPF_ST
);
7156 gen_arth(code
, a0
, a1
)
7158 struct arth
*a0
, *a1
;
7160 struct slist
*s0
, *s1
, *s2
;
7164 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7169 sappend(a0
->s
, a1
->s
);
7171 free_reg(a0
->regno
);
7172 free_reg(a1
->regno
);
7174 s0
= new_stmt(BPF_ST
);
7175 a0
->regno
= s0
->s
.k
= alloc_reg();
7182 * Here we handle simple allocation of the scratch registers.
7183 * If too many registers are alloc'd, the allocator punts.
7185 static int regused
[BPF_MEMWORDS
];
7189 * Initialize the table of used registers and the current register.
7195 memset(regused
, 0, sizeof regused
);
7199 * Return the next free register.
7204 int n
= BPF_MEMWORDS
;
7207 if (regused
[curreg
])
7208 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7210 regused
[curreg
] = 1;
7214 bpf_error("too many registers needed to evaluate expression");
7220 * Return a register to the table so it can
7230 static struct block
*
7237 s
= new_stmt(BPF_LD
|BPF_LEN
);
7238 b
= new_block(JMP(jmp
));
7249 return gen_len(BPF_JGE
, n
);
7253 * Actually, this is less than or equal.
7261 b
= gen_len(BPF_JGT
, n
);
7268 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7269 * the beginning of the link-layer header.
7270 * XXX - that means you can't test values in the radiotap header, but
7271 * as that header is difficult if not impossible to parse generally
7272 * without a loop, that might not be a severe problem. A new keyword
7273 * "radio" could be added for that, although what you'd really want
7274 * would be a way of testing particular radio header values, which
7275 * would generate code appropriate to the radio header in question.
7278 gen_byteop(op
, idx
, val
)
7289 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7292 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7296 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7300 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7304 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7308 b
= new_block(JMP(BPF_JEQ
));
7315 static u_char abroadcast
[] = { 0x0 };
7318 gen_broadcast(proto
)
7321 bpf_u_int32 hostmask
;
7322 struct block
*b0
, *b1
, *b2
;
7323 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7331 case DLT_ARCNET_LINUX
:
7332 return gen_ahostop(abroadcast
, Q_DST
);
7334 return gen_ehostop(ebroadcast
, Q_DST
);
7336 return gen_fhostop(ebroadcast
, Q_DST
);
7338 return gen_thostop(ebroadcast
, Q_DST
);
7339 case DLT_IEEE802_11
:
7340 case DLT_PRISM_HEADER
:
7341 case DLT_IEEE802_11_RADIO_AVS
:
7342 case DLT_IEEE802_11_RADIO
:
7344 return gen_wlanhostop(ebroadcast
, Q_DST
);
7345 case DLT_IP_OVER_FC
:
7346 return gen_ipfchostop(ebroadcast
, Q_DST
);
7350 * Check that the packet doesn't begin with an
7351 * LE Control marker. (We've already generated
7354 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7359 * Now check the MAC address.
7361 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7367 bpf_error("not a broadcast link");
7373 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7374 * as an indication that we don't know the netmask, and fail
7377 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7378 bpf_error("netmask not known, so 'ip broadcast' not supported");
7379 b0
= gen_linktype(ETHERTYPE_IP
);
7380 hostmask
= ~netmask
;
7381 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7382 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7383 (bpf_int32
)(~0 & hostmask
), hostmask
);
7388 bpf_error("only link-layer/IP broadcast filters supported");
7394 * Generate code to test the low-order bit of a MAC address (that's
7395 * the bottom bit of the *first* byte).
7397 static struct block
*
7398 gen_mac_multicast(offset
)
7401 register struct block
*b0
;
7402 register struct slist
*s
;
7404 /* link[offset] & 1 != 0 */
7405 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7406 b0
= new_block(JMP(BPF_JSET
));
7413 gen_multicast(proto
)
7416 register struct block
*b0
, *b1
, *b2
;
7417 register struct slist
*s
;
7425 case DLT_ARCNET_LINUX
:
7426 /* all ARCnet multicasts use the same address */
7427 return gen_ahostop(abroadcast
, Q_DST
);
7429 /* ether[0] & 1 != 0 */
7430 return gen_mac_multicast(0);
7433 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7435 * XXX - was that referring to bit-order issues?
7437 /* fddi[1] & 1 != 0 */
7438 return gen_mac_multicast(1);
7440 /* tr[2] & 1 != 0 */
7441 return gen_mac_multicast(2);
7442 case DLT_IEEE802_11
:
7443 case DLT_PRISM_HEADER
:
7444 case DLT_IEEE802_11_RADIO_AVS
:
7445 case DLT_IEEE802_11_RADIO
:
7450 * For control frames, there is no DA.
7452 * For management frames, DA is at an
7453 * offset of 4 from the beginning of
7456 * For data frames, DA is at an offset
7457 * of 4 from the beginning of the packet
7458 * if To DS is clear and at an offset of
7459 * 16 from the beginning of the packet
7464 * Generate the tests to be done for data frames.
7466 * First, check for To DS set, i.e. "link[1] & 0x01".
7468 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7469 b1
= new_block(JMP(BPF_JSET
));
7470 b1
->s
.k
= 0x01; /* To DS */
7474 * If To DS is set, the DA is at 16.
7476 b0
= gen_mac_multicast(16);
7480 * Now, check for To DS not set, i.e. check
7481 * "!(link[1] & 0x01)".
7483 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7484 b2
= new_block(JMP(BPF_JSET
));
7485 b2
->s
.k
= 0x01; /* To DS */
7490 * If To DS is not set, the DA is at 4.
7492 b1
= gen_mac_multicast(4);
7496 * Now OR together the last two checks. That gives
7497 * the complete set of checks for data frames.
7502 * Now check for a data frame.
7503 * I.e, check "link[0] & 0x08".
7505 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7506 b1
= new_block(JMP(BPF_JSET
));
7511 * AND that with the checks done for data frames.
7516 * If the high-order bit of the type value is 0, this
7517 * is a management frame.
7518 * I.e, check "!(link[0] & 0x08)".
7520 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7521 b2
= new_block(JMP(BPF_JSET
));
7527 * For management frames, the DA is at 4.
7529 b1
= gen_mac_multicast(4);
7533 * OR that with the checks done for data frames.
7534 * That gives the checks done for management and
7540 * If the low-order bit of the type value is 1,
7541 * this is either a control frame or a frame
7542 * with a reserved type, and thus not a
7545 * I.e., check "!(link[0] & 0x04)".
7547 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7548 b1
= new_block(JMP(BPF_JSET
));
7554 * AND that with the checks for data and management
7559 case DLT_IP_OVER_FC
:
7560 b0
= gen_mac_multicast(2);
7565 * Check that the packet doesn't begin with an
7566 * LE Control marker. (We've already generated
7569 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7573 /* ether[off_mac] & 1 != 0 */
7574 b0
= gen_mac_multicast(off_mac
);
7582 /* Link not known to support multicasts */
7586 b0
= gen_linktype(ETHERTYPE_IP
);
7587 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7593 b0
= gen_linktype(ETHERTYPE_IPV6
);
7594 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7599 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7605 * generate command for inbound/outbound. It's here so we can
7606 * make it link-type specific. 'dir' = 0 implies "inbound",
7607 * = 1 implies "outbound".
7613 register struct block
*b0
;
7616 * Only some data link types support inbound/outbound qualifiers.
7620 b0
= gen_relation(BPF_JEQ
,
7621 gen_load(Q_LINK
, gen_loadi(0), 1),
7628 /* match outgoing packets */
7629 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7631 /* match incoming packets */
7632 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7639 * Match packets sent by this machine.
7641 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7644 * Match packets sent to this machine.
7645 * (No broadcast or multicast packets, or
7646 * packets sent to some other machine and
7647 * received promiscuously.)
7649 * XXX - packets sent to other machines probably
7650 * shouldn't be matched, but what about broadcast
7651 * or multicast packets we received?
7653 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7657 #ifdef HAVE_NET_PFVAR_H
7659 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7660 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7666 /* match outgoing packets */
7667 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7669 /* match incoming packets */
7670 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7674 case DLT_JUNIPER_MFR
:
7675 case DLT_JUNIPER_MLFR
:
7676 case DLT_JUNIPER_MLPPP
:
7677 case DLT_JUNIPER_ATM1
:
7678 case DLT_JUNIPER_ATM2
:
7679 case DLT_JUNIPER_PPPOE
:
7680 case DLT_JUNIPER_PPPOE_ATM
:
7681 case DLT_JUNIPER_GGSN
:
7682 case DLT_JUNIPER_ES
:
7683 case DLT_JUNIPER_MONITOR
:
7684 case DLT_JUNIPER_SERVICES
:
7685 case DLT_JUNIPER_ETHER
:
7686 case DLT_JUNIPER_PPP
:
7687 case DLT_JUNIPER_FRELAY
:
7688 case DLT_JUNIPER_CHDLC
:
7689 case DLT_JUNIPER_VP
:
7690 case DLT_JUNIPER_ST
:
7691 case DLT_JUNIPER_ISM
:
7692 case DLT_JUNIPER_VS
:
7693 case DLT_JUNIPER_SRX_E2E
:
7694 case DLT_JUNIPER_FIBRECHANNEL
:
7695 case DLT_JUNIPER_ATM_CEMIC
:
7697 /* juniper flags (including direction) are stored
7698 * the byte after the 3-byte magic number */
7700 /* match outgoing packets */
7701 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7703 /* match incoming packets */
7704 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7709 bpf_error("inbound/outbound not supported on linktype %d",
7717 #ifdef HAVE_NET_PFVAR_H
7718 /* PF firewall log matched interface */
7720 gen_pf_ifname(const char *ifname
)
7725 if (linktype
!= DLT_PFLOG
) {
7726 bpf_error("ifname supported only on PF linktype");
7729 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7730 off
= offsetof(struct pfloghdr
, ifname
);
7731 if (strlen(ifname
) >= len
) {
7732 bpf_error("ifname interface names can only be %d characters",
7736 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7740 /* PF firewall log ruleset name */
7742 gen_pf_ruleset(char *ruleset
)
7746 if (linktype
!= DLT_PFLOG
) {
7747 bpf_error("ruleset supported only on PF linktype");
7751 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7752 bpf_error("ruleset names can only be %ld characters",
7753 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7757 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7758 strlen(ruleset
), (const u_char
*)ruleset
);
7762 /* PF firewall log rule number */
7768 if (linktype
!= DLT_PFLOG
) {
7769 bpf_error("rnr supported only on PF linktype");
7773 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7778 /* PF firewall log sub-rule number */
7780 gen_pf_srnr(int srnr
)
7784 if (linktype
!= DLT_PFLOG
) {
7785 bpf_error("srnr supported only on PF linktype");
7789 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7794 /* PF firewall log reason code */
7796 gen_pf_reason(int reason
)
7800 if (linktype
!= DLT_PFLOG
) {
7801 bpf_error("reason supported only on PF linktype");
7805 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7810 /* PF firewall log action */
7812 gen_pf_action(int action
)
7816 if (linktype
!= DLT_PFLOG
) {
7817 bpf_error("action supported only on PF linktype");
7821 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7825 #else /* !HAVE_NET_PFVAR_H */
7827 gen_pf_ifname(const char *ifname
)
7829 bpf_error("libpcap was compiled without pf support");
7835 gen_pf_ruleset(char *ruleset
)
7837 bpf_error("libpcap was compiled on a machine without pf support");
7845 bpf_error("libpcap was compiled on a machine without pf support");
7851 gen_pf_srnr(int srnr
)
7853 bpf_error("libpcap was compiled on a machine without pf support");
7859 gen_pf_reason(int reason
)
7861 bpf_error("libpcap was compiled on a machine without pf support");
7867 gen_pf_action(int action
)
7869 bpf_error("libpcap was compiled on a machine without pf support");
7873 #endif /* HAVE_NET_PFVAR_H */
7875 /* IEEE 802.11 wireless header */
7877 gen_p80211_type(int type
, int mask
)
7883 case DLT_IEEE802_11
:
7884 case DLT_PRISM_HEADER
:
7885 case DLT_IEEE802_11_RADIO_AVS
:
7886 case DLT_IEEE802_11_RADIO
:
7887 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7892 bpf_error("802.11 link-layer types supported only on 802.11");
7900 gen_p80211_fcdir(int fcdir
)
7906 case DLT_IEEE802_11
:
7907 case DLT_PRISM_HEADER
:
7908 case DLT_IEEE802_11_RADIO_AVS
:
7909 case DLT_IEEE802_11_RADIO
:
7913 bpf_error("frame direction supported only with 802.11 headers");
7917 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7918 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7925 register const u_char
*eaddr
;
7931 case DLT_ARCNET_LINUX
:
7932 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7934 return (gen_ahostop(eaddr
, (int)q
.dir
));
7936 bpf_error("ARCnet address used in non-arc expression");
7942 bpf_error("aid supported only on ARCnet");
7945 bpf_error("ARCnet address used in non-arc expression");
7950 static struct block
*
7951 gen_ahostop(eaddr
, dir
)
7952 register const u_char
*eaddr
;
7955 register struct block
*b0
, *b1
;
7958 /* src comes first, different from Ethernet */
7960 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7963 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7966 b0
= gen_ahostop(eaddr
, Q_SRC
);
7967 b1
= gen_ahostop(eaddr
, Q_DST
);
7973 b0
= gen_ahostop(eaddr
, Q_SRC
);
7974 b1
= gen_ahostop(eaddr
, Q_DST
);
7979 bpf_error("'addr1' is only supported on 802.11");
7983 bpf_error("'addr2' is only supported on 802.11");
7987 bpf_error("'addr3' is only supported on 802.11");
7991 bpf_error("'addr4' is only supported on 802.11");
7995 bpf_error("'ra' is only supported on 802.11");
7999 bpf_error("'ta' is only supported on 802.11");
8007 * support IEEE 802.1Q VLAN trunk over ethernet
8013 struct block
*b0
, *b1
;
8015 /* can't check for VLAN-encapsulated packets inside MPLS */
8016 if (label_stack_depth
> 0)
8017 bpf_error("no VLAN match after MPLS");
8020 * Check for a VLAN packet, and then change the offsets to point
8021 * to the type and data fields within the VLAN packet. Just
8022 * increment the offsets, so that we can support a hierarchy, e.g.
8023 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8026 * XXX - this is a bit of a kludge. If we were to split the
8027 * compiler into a parser that parses an expression and
8028 * generates an expression tree, and a code generator that
8029 * takes an expression tree (which could come from our
8030 * parser or from some other parser) and generates BPF code,
8031 * we could perhaps make the offsets parameters of routines
8032 * and, in the handler for an "AND" node, pass to subnodes
8033 * other than the VLAN node the adjusted offsets.
8035 * This would mean that "vlan" would, instead of changing the
8036 * behavior of *all* tests after it, change only the behavior
8037 * of tests ANDed with it. That would change the documented
8038 * semantics of "vlan", which might break some expressions.
8039 * However, it would mean that "(vlan and ip) or ip" would check
8040 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8041 * checking only for VLAN-encapsulated IP, so that could still
8042 * be considered worth doing; it wouldn't break expressions
8043 * that are of the form "vlan and ..." or "vlan N and ...",
8044 * which I suspect are the most common expressions involving
8045 * "vlan". "vlan or ..." doesn't necessarily do what the user
8046 * would really want, now, as all the "or ..." tests would
8047 * be done assuming a VLAN, even though the "or" could be viewed
8048 * as meaning "or, if this isn't a VLAN packet...".
8055 /* check for VLAN */
8056 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8057 (bpf_int32
)ETHERTYPE_8021Q
);
8059 /* If a specific VLAN is requested, check VLAN id */
8060 if (vlan_num
>= 0) {
8061 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
8062 (bpf_int32
)vlan_num
, 0x0fff);
8076 bpf_error("no VLAN support for data link type %d",
8091 struct block
*b0
,*b1
;
8094 * Change the offsets to point to the type and data fields within
8095 * the MPLS packet. Just increment the offsets, so that we
8096 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8097 * capture packets with an outer label of 100000 and an inner
8100 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8104 if (label_stack_depth
> 0) {
8105 /* just match the bottom-of-stack bit clear */
8106 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
8109 * Indicate that we're checking MPLS-encapsulated headers,
8110 * to make sure higher level code generators don't try to
8111 * match against IP-related protocols such as Q_ARP, Q_RARP
8116 case DLT_C_HDLC
: /* fall through */
8118 b0
= gen_linktype(ETHERTYPE_MPLS
);
8122 b0
= gen_linktype(PPP_MPLS_UCAST
);
8125 /* FIXME add other DLT_s ...
8126 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8127 * leave it for now */
8130 bpf_error("no MPLS support for data link type %d",
8138 /* If a specific MPLS label is requested, check it */
8139 if (label_num
>= 0) {
8140 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8141 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8142 0xfffff000); /* only compare the first 20 bits */
8149 label_stack_depth
++;
8154 * Support PPPOE discovery and session.
8159 /* check for PPPoE discovery */
8160 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8169 * Test against the PPPoE session link-layer type.
8171 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8174 * Change the offsets to point to the type and data fields within
8175 * the PPP packet, and note that this is PPPoE rather than
8178 * XXX - this is a bit of a kludge. If we were to split the
8179 * compiler into a parser that parses an expression and
8180 * generates an expression tree, and a code generator that
8181 * takes an expression tree (which could come from our
8182 * parser or from some other parser) and generates BPF code,
8183 * we could perhaps make the offsets parameters of routines
8184 * and, in the handler for an "AND" node, pass to subnodes
8185 * other than the PPPoE node the adjusted offsets.
8187 * This would mean that "pppoes" would, instead of changing the
8188 * behavior of *all* tests after it, change only the behavior
8189 * of tests ANDed with it. That would change the documented
8190 * semantics of "pppoes", which might break some expressions.
8191 * However, it would mean that "(pppoes and ip) or ip" would check
8192 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8193 * checking only for VLAN-encapsulated IP, so that could still
8194 * be considered worth doing; it wouldn't break expressions
8195 * that are of the form "pppoes and ..." which I suspect are the
8196 * most common expressions involving "pppoes". "pppoes or ..."
8197 * doesn't necessarily do what the user would really want, now,
8198 * as all the "or ..." tests would be done assuming PPPoE, even
8199 * though the "or" could be viewed as meaning "or, if this isn't
8200 * a PPPoE packet...".
8202 orig_linktype
= off_linktype
; /* save original values */
8207 * The "network-layer" protocol is PPPoE, which has a 6-byte
8208 * PPPoE header, followed by a PPP packet.
8210 * There is no HDLC encapsulation for the PPP packet (it's
8211 * encapsulated in PPPoES instead), so the link-layer type
8212 * starts at the first byte of the PPP packet. For PPPoE,
8213 * that offset is relative to the beginning of the total
8214 * link-layer payload, including any 802.2 LLC header, so
8215 * it's 6 bytes past off_nl.
8217 off_linktype
= off_nl
+ 6;
8220 * The network-layer offsets are relative to the beginning
8221 * of the MAC-layer payload; that's past the 6-byte
8222 * PPPoE header and the 2-byte PPP header.
8225 off_nl_nosnap
= 6+2;
8231 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8243 bpf_error("'vpi' supported only on raw ATM");
8244 if (off_vpi
== (u_int
)-1)
8246 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8252 bpf_error("'vci' supported only on raw ATM");
8253 if (off_vci
== (u_int
)-1)
8255 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8260 if (off_proto
== (u_int
)-1)
8261 abort(); /* XXX - this isn't on FreeBSD */
8262 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8267 if (off_payload
== (u_int
)-1)
8269 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8270 0xffffffff, jtype
, reverse
, jvalue
);
8275 bpf_error("'callref' supported only on raw ATM");
8276 if (off_proto
== (u_int
)-1)
8278 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8279 jtype
, reverse
, jvalue
);
8289 gen_atmtype_abbrev(type
)
8292 struct block
*b0
, *b1
;
8297 /* Get all packets in Meta signalling Circuit */
8299 bpf_error("'metac' supported only on raw ATM");
8300 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8301 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8306 /* Get all packets in Broadcast Circuit*/
8308 bpf_error("'bcc' supported only on raw ATM");
8309 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8310 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8315 /* Get all cells in Segment OAM F4 circuit*/
8317 bpf_error("'oam4sc' supported only on raw ATM");
8318 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8319 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8324 /* Get all cells in End-to-End OAM F4 Circuit*/
8326 bpf_error("'oam4ec' supported only on raw ATM");
8327 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8328 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8333 /* Get all packets in connection Signalling Circuit */
8335 bpf_error("'sc' supported only on raw ATM");
8336 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8337 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8342 /* Get all packets in ILMI Circuit */
8344 bpf_error("'ilmic' supported only on raw ATM");
8345 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8346 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8351 /* Get all LANE packets */
8353 bpf_error("'lane' supported only on raw ATM");
8354 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8357 * Arrange that all subsequent tests assume LANE
8358 * rather than LLC-encapsulated packets, and set
8359 * the offsets appropriately for LANE-encapsulated
8362 * "off_mac" is the offset of the Ethernet header,
8363 * which is 2 bytes past the ATM pseudo-header
8364 * (skipping the pseudo-header and 2-byte LE Client
8365 * field). The other offsets are Ethernet offsets
8366 * relative to "off_mac".
8369 off_mac
= off_payload
+ 2; /* MAC header */
8370 off_linktype
= off_mac
+ 12;
8371 off_macpl
= off_mac
+ 14; /* Ethernet */
8372 off_nl
= 0; /* Ethernet II */
8373 off_nl_nosnap
= 3; /* 802.3+802.2 */
8377 /* Get all LLC-encapsulated packets */
8379 bpf_error("'llc' supported only on raw ATM");
8380 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8391 * Filtering for MTP2 messages based on li value
8392 * FISU, length is null
8393 * LSSU, length is 1 or 2
8394 * MSU, length is 3 or more
8397 gen_mtp2type_abbrev(type
)
8400 struct block
*b0
, *b1
;
8405 if ( (linktype
!= DLT_MTP2
) &&
8406 (linktype
!= DLT_ERF
) &&
8407 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8408 bpf_error("'fisu' supported only on MTP2");
8409 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8410 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8414 if ( (linktype
!= DLT_MTP2
) &&
8415 (linktype
!= DLT_ERF
) &&
8416 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8417 bpf_error("'lssu' supported only on MTP2");
8418 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8419 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8424 if ( (linktype
!= DLT_MTP2
) &&
8425 (linktype
!= DLT_ERF
) &&
8426 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8427 bpf_error("'msu' supported only on MTP2");
8428 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8438 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8445 bpf_u_int32 val1
, val2
, val3
;
8447 switch (mtp3field
) {
8450 if (off_sio
== (u_int
)-1)
8451 bpf_error("'sio' supported only on SS7");
8452 /* sio coded on 1 byte so max value 255 */
8454 bpf_error("sio value %u too big; max value = 255",
8456 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8457 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8461 if (off_opc
== (u_int
)-1)
8462 bpf_error("'opc' supported only on SS7");
8463 /* opc coded on 14 bits so max value 16383 */
8465 bpf_error("opc value %u too big; max value = 16383",
8467 /* the following instructions are made to convert jvalue
8468 * to the form used to write opc in an ss7 message*/
8469 val1
= jvalue
& 0x00003c00;
8471 val2
= jvalue
& 0x000003fc;
8473 val3
= jvalue
& 0x00000003;
8475 jvalue
= val1
+ val2
+ val3
;
8476 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8477 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8481 if (off_dpc
== (u_int
)-1)
8482 bpf_error("'dpc' supported only on SS7");
8483 /* dpc coded on 14 bits so max value 16383 */
8485 bpf_error("dpc value %u too big; max value = 16383",
8487 /* the following instructions are made to convert jvalue
8488 * to the forme used to write dpc in an ss7 message*/
8489 val1
= jvalue
& 0x000000ff;
8491 val2
= jvalue
& 0x00003f00;
8493 jvalue
= val1
+ val2
;
8494 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8495 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8499 if (off_sls
== (u_int
)-1)
8500 bpf_error("'sls' supported only on SS7");
8501 /* sls coded on 4 bits so max value 15 */
8503 bpf_error("sls value %u too big; max value = 15",
8505 /* the following instruction is made to convert jvalue
8506 * to the forme used to write sls in an ss7 message*/
8507 jvalue
= jvalue
<< 4;
8508 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8509 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8518 static struct block
*
8519 gen_msg_abbrev(type
)
8525 * Q.2931 signalling protocol messages for handling virtual circuits
8526 * establishment and teardown
8531 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8535 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8539 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8543 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8547 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8550 case A_RELEASE_DONE
:
8551 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8561 gen_atmmulti_abbrev(type
)
8564 struct block
*b0
, *b1
;
8570 bpf_error("'oam' supported only on raw ATM");
8571 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8576 bpf_error("'oamf4' supported only on raw ATM");
8578 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8579 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8581 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8587 * Get Q.2931 signalling messages for switched
8588 * virtual connection
8591 bpf_error("'connectmsg' supported only on raw ATM");
8592 b0
= gen_msg_abbrev(A_SETUP
);
8593 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8595 b0
= gen_msg_abbrev(A_CONNECT
);
8597 b0
= gen_msg_abbrev(A_CONNECTACK
);
8599 b0
= gen_msg_abbrev(A_RELEASE
);
8601 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8603 b0
= gen_atmtype_abbrev(A_SC
);
8609 bpf_error("'metaconnect' supported only on raw ATM");
8610 b0
= gen_msg_abbrev(A_SETUP
);
8611 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8613 b0
= gen_msg_abbrev(A_CONNECT
);
8615 b0
= gen_msg_abbrev(A_RELEASE
);
8617 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8619 b0
= gen_atmtype_abbrev(A_METAC
);