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, and
776 * for Ethernet with various additional information.
778 static u_int off_mac
;
781 * This is the offset of the beginning of the MAC-layer payload,
782 * from the beginning of the raw packet data.
784 * I.e., it's the sum of the length of the link-layer header (without,
785 * for example, any 802.2 LLC header, so it's the MAC-layer
786 * portion of that header), plus any prefix preceding the
789 static u_int off_macpl
;
792 * This is 1 if the offset of the beginning of the MAC-layer payload
793 * from the beginning of the link-layer header is variable-length.
795 static int off_macpl_is_variable
;
798 * If the link layer has variable_length headers, "reg_off_macpl"
799 * is the register number for a register containing the length of the
800 * link-layer header plus the length of any variable-length header
801 * preceding the link-layer header. Otherwise, "reg_off_macpl"
804 static int reg_off_macpl
;
807 * "off_linktype" is the offset to information in the link-layer header
808 * giving the packet type. This offset is relative to the beginning
809 * of the link-layer header (i.e., it doesn't include off_ll).
811 * For Ethernet, it's the offset of the Ethernet type field.
813 * For link-layer types that always use 802.2 headers, it's the
814 * offset of the LLC header.
816 * For PPP, it's the offset of the PPP type field.
818 * For Cisco HDLC, it's the offset of the CHDLC type field.
820 * For BSD loopback, it's the offset of the AF_ value.
822 * For Linux cooked sockets, it's the offset of the type field.
824 * It's set to -1 for no encapsulation, in which case, IP is assumed.
826 static u_int off_linktype
;
829 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
830 * checks to check the PPP header, assumed to follow a LAN-style link-
831 * layer header and a PPPoE session header.
833 static int is_pppoes
= 0;
836 * TRUE if the link layer includes an ATM pseudo-header.
838 static int is_atm
= 0;
841 * TRUE if "lane" appeared in the filter; it causes us to generate
842 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
844 static int is_lane
= 0;
847 * These are offsets for the ATM pseudo-header.
849 static u_int off_vpi
;
850 static u_int off_vci
;
851 static u_int off_proto
;
854 * These are offsets for the MTP2 fields.
859 * These are offsets for the MTP3 fields.
861 static u_int off_sio
;
862 static u_int off_opc
;
863 static u_int off_dpc
;
864 static u_int off_sls
;
867 * This is the offset of the first byte after the ATM pseudo_header,
868 * or -1 if there is no ATM pseudo-header.
870 static u_int off_payload
;
873 * These are offsets to the beginning of the network-layer header.
874 * They are relative to the beginning of the MAC-layer payload (i.e.,
875 * they don't include off_ll or off_macpl).
877 * If the link layer never uses 802.2 LLC:
879 * "off_nl" and "off_nl_nosnap" are the same.
881 * If the link layer always uses 802.2 LLC:
883 * "off_nl" is the offset if there's a SNAP header following
886 * "off_nl_nosnap" is the offset if there's no SNAP header.
888 * If the link layer is Ethernet:
890 * "off_nl" is the offset if the packet is an Ethernet II packet
891 * (we assume no 802.3+802.2+SNAP);
893 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
894 * with an 802.2 header following it.
897 static u_int off_nl_nosnap
;
905 linktype
= pcap_datalink(p
);
907 pcap_fddipad
= p
->fddipad
;
911 * Assume it's not raw ATM with a pseudo-header, for now.
922 * And that we're not doing PPPoE.
927 * And assume we're not doing SS7.
936 * Also assume it's not 802.11.
940 off_macpl_is_variable
= 0;
944 label_stack_depth
= 0;
954 off_nl
= 0; /* XXX in reality, variable! */
955 off_nl_nosnap
= 0; /* no 802.2 LLC */
958 case DLT_ARCNET_LINUX
:
961 off_nl
= 0; /* XXX in reality, variable! */
962 off_nl_nosnap
= 0; /* no 802.2 LLC */
967 off_macpl
= 14; /* Ethernet header length */
968 off_nl
= 0; /* Ethernet II */
969 off_nl_nosnap
= 3; /* 802.3+802.2 */
974 * SLIP doesn't have a link level type. The 16 byte
975 * header is hacked into our SLIP driver.
980 off_nl_nosnap
= 0; /* no 802.2 LLC */
984 /* XXX this may be the same as the DLT_PPP_BSDOS case */
989 off_nl_nosnap
= 0; /* no 802.2 LLC */
997 off_nl_nosnap
= 0; /* no 802.2 LLC */
1004 off_nl_nosnap
= 0; /* no 802.2 LLC */
1009 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1010 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1014 off_nl_nosnap
= 0; /* no 802.2 LLC */
1019 * This does no include the Ethernet header, and
1020 * only covers session state.
1025 off_nl_nosnap
= 0; /* no 802.2 LLC */
1032 off_nl_nosnap
= 0; /* no 802.2 LLC */
1037 * FDDI doesn't really have a link-level type field.
1038 * We set "off_linktype" to the offset of the LLC header.
1040 * To check for Ethernet types, we assume that SSAP = SNAP
1041 * is being used and pick out the encapsulated Ethernet type.
1042 * XXX - should we generate code to check for SNAP?
1046 off_linktype
+= pcap_fddipad
;
1048 off_macpl
= 13; /* FDDI MAC header length */
1050 off_macpl
+= pcap_fddipad
;
1052 off_nl
= 8; /* 802.2+SNAP */
1053 off_nl_nosnap
= 3; /* 802.2 */
1058 * Token Ring doesn't really have a link-level type field.
1059 * We set "off_linktype" to the offset of the LLC header.
1061 * To check for Ethernet types, we assume that SSAP = SNAP
1062 * is being used and pick out the encapsulated Ethernet type.
1063 * XXX - should we generate code to check for SNAP?
1065 * XXX - the header is actually variable-length.
1066 * Some various Linux patched versions gave 38
1067 * as "off_linktype" and 40 as "off_nl"; however,
1068 * if a token ring packet has *no* routing
1069 * information, i.e. is not source-routed, the correct
1070 * values are 20 and 22, as they are in the vanilla code.
1072 * A packet is source-routed iff the uppermost bit
1073 * of the first byte of the source address, at an
1074 * offset of 8, has the uppermost bit set. If the
1075 * packet is source-routed, the total number of bytes
1076 * of routing information is 2 plus bits 0x1F00 of
1077 * the 16-bit value at an offset of 14 (shifted right
1078 * 8 - figure out which byte that is).
1081 off_macpl
= 14; /* Token Ring MAC header length */
1082 off_nl
= 8; /* 802.2+SNAP */
1083 off_nl_nosnap
= 3; /* 802.2 */
1086 case DLT_IEEE802_11
:
1087 case DLT_PRISM_HEADER
:
1088 case DLT_IEEE802_11_RADIO_AVS
:
1089 case DLT_IEEE802_11_RADIO
:
1091 * 802.11 doesn't really have a link-level type field.
1092 * We set "off_linktype" to the offset of the LLC header.
1094 * To check for Ethernet types, we assume that SSAP = SNAP
1095 * is being used and pick out the encapsulated Ethernet type.
1096 * XXX - should we generate code to check for SNAP?
1098 * We also handle variable-length radio headers here.
1099 * The Prism header is in theory variable-length, but in
1100 * practice it's always 144 bytes long. However, some
1101 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1102 * sometimes or always supply an AVS header, so we
1103 * have to check whether the radio header is a Prism
1104 * header or an AVS header, so, in practice, it's
1108 off_macpl
= 0; /* link-layer header is variable-length */
1109 off_macpl_is_variable
= 1;
1110 off_nl
= 8; /* 802.2+SNAP */
1111 off_nl_nosnap
= 3; /* 802.2 */
1116 * At the moment we treat PPI the same way that we treat
1117 * normal Radiotap encoded packets. The difference is in
1118 * the function that generates the code at the beginning
1119 * to compute the header length. Since this code generator
1120 * of PPI supports bare 802.11 encapsulation only (i.e.
1121 * the encapsulated DLT should be DLT_IEEE802_11) we
1122 * generate code to check for this too.
1125 off_macpl
= 0; /* link-layer header is variable-length */
1126 off_macpl_is_variable
= 1;
1127 off_nl
= 8; /* 802.2+SNAP */
1128 off_nl_nosnap
= 3; /* 802.2 */
1131 case DLT_ATM_RFC1483
:
1132 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1134 * assume routed, non-ISO PDUs
1135 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1137 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1138 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1139 * latter would presumably be treated the way PPPoE
1140 * should be, so you can do "pppoe and udp port 2049"
1141 * or "pppoa and tcp port 80" and have it check for
1142 * PPPo{A,E} and a PPP protocol of IP and....
1145 off_macpl
= 0; /* packet begins with LLC header */
1146 off_nl
= 8; /* 802.2+SNAP */
1147 off_nl_nosnap
= 3; /* 802.2 */
1152 * Full Frontal ATM; you get AALn PDUs with an ATM
1156 off_vpi
= SUNATM_VPI_POS
;
1157 off_vci
= SUNATM_VCI_POS
;
1158 off_proto
= PROTO_POS
;
1159 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1160 off_payload
= SUNATM_PKT_BEGIN_POS
;
1161 off_linktype
= off_payload
;
1162 off_macpl
= off_payload
; /* if LLC-encapsulated */
1163 off_nl
= 8; /* 802.2+SNAP */
1164 off_nl_nosnap
= 3; /* 802.2 */
1173 off_nl_nosnap
= 0; /* no 802.2 LLC */
1176 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1180 off_nl_nosnap
= 0; /* no 802.2 LLC */
1185 * LocalTalk does have a 1-byte type field in the LLAP header,
1186 * but really it just indicates whether there is a "short" or
1187 * "long" DDP packet following.
1192 off_nl_nosnap
= 0; /* no 802.2 LLC */
1195 case DLT_IP_OVER_FC
:
1197 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1198 * link-level type field. We set "off_linktype" to the
1199 * offset of the LLC header.
1201 * To check for Ethernet types, we assume that SSAP = SNAP
1202 * is being used and pick out the encapsulated Ethernet type.
1203 * XXX - should we generate code to check for SNAP? RFC
1204 * 2625 says SNAP should be used.
1208 off_nl
= 8; /* 802.2+SNAP */
1209 off_nl_nosnap
= 3; /* 802.2 */
1214 * XXX - we should set this to handle SNAP-encapsulated
1215 * frames (NLPID of 0x80).
1220 off_nl_nosnap
= 0; /* no 802.2 LLC */
1224 * the only BPF-interesting FRF.16 frames are non-control frames;
1225 * Frame Relay has a variable length link-layer
1226 * so lets start with offset 4 for now and increments later on (FIXME);
1232 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1235 case DLT_APPLE_IP_OVER_IEEE1394
:
1239 off_nl_nosnap
= 0; /* no 802.2 LLC */
1242 case DLT_LINUX_IRDA
:
1244 * Currently, only raw "link[N:M]" filtering is supported.
1254 * Currently, only raw "link[N:M]" filtering is supported.
1262 case DLT_SYMANTEC_FIREWALL
:
1265 off_nl
= 0; /* Ethernet II */
1266 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1269 #ifdef HAVE_NET_PFVAR_H
1272 off_macpl
= PFLOG_HDRLEN
;
1274 off_nl_nosnap
= 0; /* no 802.2 LLC */
1278 case DLT_JUNIPER_MFR
:
1279 case DLT_JUNIPER_MLFR
:
1280 case DLT_JUNIPER_MLPPP
:
1281 case DLT_JUNIPER_PPP
:
1282 case DLT_JUNIPER_CHDLC
:
1283 case DLT_JUNIPER_FRELAY
:
1287 off_nl_nosnap
= -1; /* no 802.2 LLC */
1290 case DLT_JUNIPER_ATM1
:
1291 off_linktype
= 4; /* in reality variable between 4-8 */
1292 off_macpl
= 4; /* in reality variable between 4-8 */
1297 case DLT_JUNIPER_ATM2
:
1298 off_linktype
= 8; /* in reality variable between 8-12 */
1299 off_macpl
= 8; /* in reality variable between 8-12 */
1304 /* frames captured on a Juniper PPPoE service PIC
1305 * contain raw ethernet frames */
1306 case DLT_JUNIPER_PPPOE
:
1307 case DLT_JUNIPER_ETHER
:
1310 off_nl
= 18; /* Ethernet II */
1311 off_nl_nosnap
= 21; /* 802.3+802.2 */
1314 case DLT_JUNIPER_PPPOE_ATM
:
1318 off_nl_nosnap
= -1; /* no 802.2 LLC */
1321 case DLT_JUNIPER_GGSN
:
1325 off_nl_nosnap
= -1; /* no 802.2 LLC */
1328 case DLT_JUNIPER_ES
:
1330 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl
= -1; /* not really a network layer but raw IP addresses */
1332 off_nl_nosnap
= -1; /* no 802.2 LLC */
1335 case DLT_JUNIPER_MONITOR
:
1338 off_nl
= 0; /* raw IP/IP6 header */
1339 off_nl_nosnap
= -1; /* no 802.2 LLC */
1342 case DLT_JUNIPER_SERVICES
:
1344 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl
= -1; /* L3 proto location dep. on cookie type */
1346 off_nl_nosnap
= -1; /* no 802.2 LLC */
1349 case DLT_JUNIPER_VP
:
1356 case DLT_JUNIPER_ST
:
1363 case DLT_JUNIPER_ISM
:
1370 case DLT_JUNIPER_VS
:
1371 case DLT_JUNIPER_SRX_E2E
:
1372 case DLT_JUNIPER_FIBRECHANNEL
:
1373 case DLT_JUNIPER_ATM_CEMIC
:
1392 case DLT_MTP2_WITH_PHDR
:
1425 case DLT_LINUX_LAPD
:
1427 * Currently, only raw "link[N:M]" filtering is supported.
1437 * Currently, only raw "link[N:M]" filtering is supported.
1445 case DLT_BLUETOOTH_HCI_H4
:
1447 * Currently, only raw "link[N:M]" filtering is supported.
1457 * Currently, only raw "link[N:M]" filtering is supported.
1467 * Currently, only raw "link[N:M]" filtering is supported.
1475 case DLT_IEEE802_15_4_LINUX
:
1477 * Currently, only raw "link[N:M]" filtering is supported.
1485 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1487 * Currently, only raw "link[N:M]" filtering is supported.
1495 case DLT_IEEE802_15_4
:
1497 * Currently, only raw "link[N:M]" filtering is supported.
1507 * Currently, only raw "link[N:M]" filtering is supported.
1517 * Currently, only raw "link[N:M]" filtering is supported.
1527 * Currently, only raw "link[N:M]" filtering is supported.
1535 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1537 * Currently, only raw "link[N:M]" filtering is supported.
1547 * Currently, only raw "link[N:M]" filtering is supported.
1549 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1551 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1552 off_nl_nosnap
= -1; /* no 802.2 LLC */
1553 off_mac
= 1; /* step over the kiss length byte */
1556 case DLT_IEEE802_15_4_NONASK_PHY
:
1558 * Currently, only raw "link[N:M]" filtering is supported.
1568 * Currently, only raw "link[N:M]" filtering is supported.
1576 case DLT_USB_LINUX_MMAPPED
:
1578 * Currently, only raw "link[N:M]" filtering is supported.
1586 case DLT_CAN_SOCKETCAN
:
1588 * Currently, only raw "link[N:M]" filtering is supported.
1598 off_macpl
= 24; /* ipnet header length */
1603 case DLT_IEEE802_15_4_NOFCS
:
1605 * Currently, only raw "link[N:M]" filtering is supported.
1615 * Currently, only raw "link[N:M]" filtering is supported.
1623 case DLT_NETANALYZER
:
1624 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1625 off_linktype
= 16; /* includes 4-byte pseudo-header */
1626 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1627 off_nl
= 0; /* Ethernet II */
1628 off_nl_nosnap
= 3; /* 802.3+802.2 */
1631 case DLT_NETANALYZER_TRANSPARENT
:
1632 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1633 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1634 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1635 off_nl
= 0; /* Ethernet II */
1636 off_nl_nosnap
= 3; /* 802.3+802.2 */
1639 bpf_error("unknown data link type %d", linktype
);
1644 * Load a value relative to the beginning of the link-layer header.
1645 * The link-layer header doesn't necessarily begin at the beginning
1646 * of the packet data; there might be a variable-length prefix containing
1647 * radio information.
1649 static struct slist
*
1650 gen_load_llrel(offset
, size
)
1653 struct slist
*s
, *s2
;
1655 s
= gen_llprefixlen();
1658 * If "s" is non-null, it has code to arrange that the X register
1659 * contains the length of the prefix preceding the link-layer
1662 * Otherwise, the length of the prefix preceding the link-layer
1663 * header is "off_ll".
1667 * There's a variable-length prefix preceding the
1668 * link-layer header. "s" points to a list of statements
1669 * that put the length of that prefix into the X register.
1670 * do an indirect load, to use the X register as an offset.
1672 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1677 * There is no variable-length header preceding the
1678 * link-layer header; add in off_ll, which, if there's
1679 * a fixed-length header preceding the link-layer header,
1680 * is the length of that header.
1682 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1683 s
->s
.k
= offset
+ off_ll
;
1689 * Load a value relative to the beginning of the MAC-layer payload.
1691 static struct slist
*
1692 gen_load_macplrel(offset
, size
)
1695 struct slist
*s
, *s2
;
1697 s
= gen_off_macpl();
1700 * If s is non-null, the offset of the MAC-layer payload is
1701 * variable, and s points to a list of instructions that
1702 * arrange that the X register contains that offset.
1704 * Otherwise, the offset of the MAC-layer payload is constant,
1705 * and is in off_macpl.
1709 * The offset of the MAC-layer payload is in the X
1710 * register. Do an indirect load, to use the X register
1713 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1718 * The offset of the MAC-layer payload is constant,
1719 * and is in off_macpl; load the value at that offset
1720 * plus the specified offset.
1722 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1723 s
->s
.k
= off_macpl
+ offset
;
1729 * Load a value relative to the beginning of the specified header.
1731 static struct slist
*
1732 gen_load_a(offrel
, offset
, size
)
1733 enum e_offrel offrel
;
1736 struct slist
*s
, *s2
;
1741 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1746 s
= gen_load_llrel(offset
, size
);
1750 s
= gen_load_macplrel(offset
, size
);
1754 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1758 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1763 * Load the X register with the length of the IPv4 header
1764 * (plus the offset of the link-layer header, if it's
1765 * preceded by a variable-length header such as a radio
1766 * header), in bytes.
1768 s
= gen_loadx_iphdrlen();
1771 * Load the item at {offset of the MAC-layer payload} +
1772 * {offset, relative to the start of the MAC-layer
1773 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1774 * {specified offset}.
1776 * (If the offset of the MAC-layer payload is variable,
1777 * it's included in the value in the X register, and
1780 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1781 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1786 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1797 * Generate code to load into the X register the sum of the length of
1798 * the IPv4 header and any variable-length header preceding the link-layer
1801 static struct slist
*
1802 gen_loadx_iphdrlen()
1804 struct slist
*s
, *s2
;
1806 s
= gen_off_macpl();
1809 * There's a variable-length prefix preceding the
1810 * link-layer header, or the link-layer header is itself
1811 * variable-length. "s" points to a list of statements
1812 * that put the offset of the MAC-layer payload into
1815 * The 4*([k]&0xf) addressing mode can't be used, as we
1816 * don't have a constant offset, so we have to load the
1817 * value in question into the A register and add to it
1818 * the value from the X register.
1820 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1823 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1826 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1831 * The A register now contains the length of the
1832 * IP header. We need to add to it the offset of
1833 * the MAC-layer payload, which is still in the X
1834 * register, and move the result into the X register.
1836 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1837 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1840 * There is no variable-length header preceding the
1841 * link-layer header, and the link-layer header is
1842 * fixed-length; load the length of the IPv4 header,
1843 * which is at an offset of off_nl from the beginning
1844 * of the MAC-layer payload, and thus at an offset
1845 * of off_mac_pl + off_nl from the beginning of the
1848 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1849 s
->s
.k
= off_macpl
+ off_nl
;
1854 static struct block
*
1861 s
= new_stmt(BPF_LD
|BPF_IMM
);
1863 b
= new_block(JMP(BPF_JEQ
));
1869 static inline struct block
*
1872 return gen_uncond(1);
1875 static inline struct block
*
1878 return gen_uncond(0);
1882 * Byte-swap a 32-bit number.
1883 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1884 * big-endian platforms.)
1886 #define SWAPLONG(y) \
1887 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1890 * Generate code to match a particular packet type.
1892 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1893 * value, if <= ETHERMTU. We use that to determine whether to
1894 * match the type/length field or to check the type/length field for
1895 * a value <= ETHERMTU to see whether it's a type field and then do
1896 * the appropriate test.
1898 static struct block
*
1899 gen_ether_linktype(proto
)
1902 struct block
*b0
, *b1
;
1908 case LLCSAP_NETBEUI
:
1910 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1911 * so we check the DSAP and SSAP.
1913 * LLCSAP_IP checks for IP-over-802.2, rather
1914 * than IP-over-Ethernet or IP-over-SNAP.
1916 * XXX - should we check both the DSAP and the
1917 * SSAP, like this, or should we check just the
1918 * DSAP, as we do for other types <= ETHERMTU
1919 * (i.e., other SAP values)?
1921 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1923 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1924 ((proto
<< 8) | proto
));
1932 * Ethernet_II frames, which are Ethernet
1933 * frames with a frame type of ETHERTYPE_IPX;
1935 * Ethernet_802.3 frames, which are 802.3
1936 * frames (i.e., the type/length field is
1937 * a length field, <= ETHERMTU, rather than
1938 * a type field) with the first two bytes
1939 * after the Ethernet/802.3 header being
1942 * Ethernet_802.2 frames, which are 802.3
1943 * frames with an 802.2 LLC header and
1944 * with the IPX LSAP as the DSAP in the LLC
1947 * Ethernet_SNAP frames, which are 802.3
1948 * frames with an LLC header and a SNAP
1949 * header and with an OUI of 0x000000
1950 * (encapsulated Ethernet) and a protocol
1951 * ID of ETHERTYPE_IPX in the SNAP header.
1953 * XXX - should we generate the same code both
1954 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1958 * This generates code to check both for the
1959 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1961 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1962 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1966 * Now we add code to check for SNAP frames with
1967 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1969 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1973 * Now we generate code to check for 802.3
1974 * frames in general.
1976 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1980 * Now add the check for 802.3 frames before the
1981 * check for Ethernet_802.2 and Ethernet_802.3,
1982 * as those checks should only be done on 802.3
1983 * frames, not on Ethernet frames.
1988 * Now add the check for Ethernet_II frames, and
1989 * do that before checking for the other frame
1992 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1993 (bpf_int32
)ETHERTYPE_IPX
);
1997 case ETHERTYPE_ATALK
:
1998 case ETHERTYPE_AARP
:
2000 * EtherTalk (AppleTalk protocols on Ethernet link
2001 * layer) may use 802.2 encapsulation.
2005 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2006 * we check for an Ethernet type field less than
2007 * 1500, which means it's an 802.3 length field.
2009 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
2013 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2014 * SNAP packets with an organization code of
2015 * 0x080007 (Apple, for Appletalk) and a protocol
2016 * type of ETHERTYPE_ATALK (Appletalk).
2018 * 802.2-encapsulated ETHERTYPE_AARP packets are
2019 * SNAP packets with an organization code of
2020 * 0x000000 (encapsulated Ethernet) and a protocol
2021 * type of ETHERTYPE_AARP (Appletalk ARP).
2023 if (proto
== ETHERTYPE_ATALK
)
2024 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2025 else /* proto == ETHERTYPE_AARP */
2026 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2030 * Check for Ethernet encapsulation (Ethertalk
2031 * phase 1?); we just check for the Ethernet
2034 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2040 if (proto
<= ETHERMTU
) {
2042 * This is an LLC SAP value, so the frames
2043 * that match would be 802.2 frames.
2044 * Check that the frame is an 802.2 frame
2045 * (i.e., that the length/type field is
2046 * a length field, <= ETHERMTU) and
2047 * then check the DSAP.
2049 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
2051 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
2057 * This is an Ethernet type, so compare
2058 * the length/type field with it (if
2059 * the frame is an 802.2 frame, the length
2060 * field will be <= ETHERMTU, and, as
2061 * "proto" is > ETHERMTU, this test
2062 * will fail and the frame won't match,
2063 * which is what we want).
2065 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2072 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2073 * or IPv6 then we have an error.
2075 static struct block
*
2076 gen_ipnet_linktype(proto
)
2082 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2083 (bpf_int32
)IPH_AF_INET
);
2086 case ETHERTYPE_IPV6
:
2087 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2088 (bpf_int32
)IPH_AF_INET6
);
2099 * Generate code to match a particular packet type.
2101 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2102 * value, if <= ETHERMTU. We use that to determine whether to
2103 * match the type field or to check the type field for the special
2104 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2106 static struct block
*
2107 gen_linux_sll_linktype(proto
)
2110 struct block
*b0
, *b1
;
2116 case LLCSAP_NETBEUI
:
2118 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2119 * so we check the DSAP and SSAP.
2121 * LLCSAP_IP checks for IP-over-802.2, rather
2122 * than IP-over-Ethernet or IP-over-SNAP.
2124 * XXX - should we check both the DSAP and the
2125 * SSAP, like this, or should we check just the
2126 * DSAP, as we do for other types <= ETHERMTU
2127 * (i.e., other SAP values)?
2129 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2130 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2131 ((proto
<< 8) | proto
));
2137 * Ethernet_II frames, which are Ethernet
2138 * frames with a frame type of ETHERTYPE_IPX;
2140 * Ethernet_802.3 frames, which have a frame
2141 * type of LINUX_SLL_P_802_3;
2143 * Ethernet_802.2 frames, which are 802.3
2144 * frames with an 802.2 LLC header (i.e, have
2145 * a frame type of LINUX_SLL_P_802_2) and
2146 * with the IPX LSAP as the DSAP in the LLC
2149 * Ethernet_SNAP frames, which are 802.3
2150 * frames with an LLC header and a SNAP
2151 * header and with an OUI of 0x000000
2152 * (encapsulated Ethernet) and a protocol
2153 * ID of ETHERTYPE_IPX in the SNAP header.
2155 * First, do the checks on LINUX_SLL_P_802_2
2156 * frames; generate the check for either
2157 * Ethernet_802.2 or Ethernet_SNAP frames, and
2158 * then put a check for LINUX_SLL_P_802_2 frames
2161 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2162 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2164 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2168 * Now check for 802.3 frames and OR that with
2169 * the previous test.
2171 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2175 * Now add the check for Ethernet_II frames, and
2176 * do that before checking for the other frame
2179 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2180 (bpf_int32
)ETHERTYPE_IPX
);
2184 case ETHERTYPE_ATALK
:
2185 case ETHERTYPE_AARP
:
2187 * EtherTalk (AppleTalk protocols on Ethernet link
2188 * layer) may use 802.2 encapsulation.
2192 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2193 * we check for the 802.2 protocol type in the
2194 * "Ethernet type" field.
2196 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2199 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2200 * SNAP packets with an organization code of
2201 * 0x080007 (Apple, for Appletalk) and a protocol
2202 * type of ETHERTYPE_ATALK (Appletalk).
2204 * 802.2-encapsulated ETHERTYPE_AARP packets are
2205 * SNAP packets with an organization code of
2206 * 0x000000 (encapsulated Ethernet) and a protocol
2207 * type of ETHERTYPE_AARP (Appletalk ARP).
2209 if (proto
== ETHERTYPE_ATALK
)
2210 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2211 else /* proto == ETHERTYPE_AARP */
2212 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2216 * Check for Ethernet encapsulation (Ethertalk
2217 * phase 1?); we just check for the Ethernet
2220 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2226 if (proto
<= ETHERMTU
) {
2228 * This is an LLC SAP value, so the frames
2229 * that match would be 802.2 frames.
2230 * Check for the 802.2 protocol type
2231 * in the "Ethernet type" field, and
2232 * then check the DSAP.
2234 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2236 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2242 * This is an Ethernet type, so compare
2243 * the length/type field with it (if
2244 * the frame is an 802.2 frame, the length
2245 * field will be <= ETHERMTU, and, as
2246 * "proto" is > ETHERMTU, this test
2247 * will fail and the frame won't match,
2248 * which is what we want).
2250 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2256 static struct slist
*
2257 gen_load_prism_llprefixlen()
2259 struct slist
*s1
, *s2
;
2260 struct slist
*sjeq_avs_cookie
;
2261 struct slist
*sjcommon
;
2264 * This code is not compatible with the optimizer, as
2265 * we are generating jmp instructions within a normal
2266 * slist of instructions
2271 * Generate code to load the length of the radio header into
2272 * the register assigned to hold that length, if one has been
2273 * assigned. (If one hasn't been assigned, no code we've
2274 * generated uses that prefix, so we don't need to generate any
2277 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2278 * or always use the AVS header rather than the Prism header.
2279 * We load a 4-byte big-endian value at the beginning of the
2280 * raw packet data, and see whether, when masked with 0xFFFFF000,
2281 * it's equal to 0x80211000. If so, that indicates that it's
2282 * an AVS header (the masked-out bits are the version number).
2283 * Otherwise, it's a Prism header.
2285 * XXX - the Prism header is also, in theory, variable-length,
2286 * but no known software generates headers that aren't 144
2289 if (reg_off_ll
!= -1) {
2293 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2297 * AND it with 0xFFFFF000.
2299 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2300 s2
->s
.k
= 0xFFFFF000;
2304 * Compare with 0x80211000.
2306 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2307 sjeq_avs_cookie
->s
.k
= 0x80211000;
2308 sappend(s1
, sjeq_avs_cookie
);
2313 * The 4 bytes at an offset of 4 from the beginning of
2314 * the AVS header are the length of the AVS header.
2315 * That field is big-endian.
2317 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2320 sjeq_avs_cookie
->s
.jt
= s2
;
2323 * Now jump to the code to allocate a register
2324 * into which to save the header length and
2325 * store the length there. (The "jump always"
2326 * instruction needs to have the k field set;
2327 * it's added to the PC, so, as we're jumping
2328 * over a single instruction, it should be 1.)
2330 sjcommon
= new_stmt(JMP(BPF_JA
));
2332 sappend(s1
, sjcommon
);
2335 * Now for the code that handles the Prism header.
2336 * Just load the length of the Prism header (144)
2337 * into the A register. Have the test for an AVS
2338 * header branch here if we don't have an AVS header.
2340 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2343 sjeq_avs_cookie
->s
.jf
= s2
;
2346 * Now allocate a register to hold that value and store
2347 * it. The code for the AVS header will jump here after
2348 * loading the length of the AVS header.
2350 s2
= new_stmt(BPF_ST
);
2351 s2
->s
.k
= reg_off_ll
;
2353 sjcommon
->s
.jf
= s2
;
2356 * Now move it into the X register.
2358 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2366 static struct slist
*
2367 gen_load_avs_llprefixlen()
2369 struct slist
*s1
, *s2
;
2372 * Generate code to load the length of the AVS header into
2373 * the register assigned to hold that length, if one has been
2374 * assigned. (If one hasn't been assigned, no code we've
2375 * generated uses that prefix, so we don't need to generate any
2378 if (reg_off_ll
!= -1) {
2380 * The 4 bytes at an offset of 4 from the beginning of
2381 * the AVS header are the length of the AVS header.
2382 * That field is big-endian.
2384 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2388 * Now allocate a register to hold that value and store
2391 s2
= new_stmt(BPF_ST
);
2392 s2
->s
.k
= reg_off_ll
;
2396 * Now move it into the X register.
2398 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2406 static struct slist
*
2407 gen_load_radiotap_llprefixlen()
2409 struct slist
*s1
, *s2
;
2412 * Generate code to load the length of the radiotap header into
2413 * the register assigned to hold that length, if one has been
2414 * assigned. (If one hasn't been assigned, no code we've
2415 * generated uses that prefix, so we don't need to generate any
2418 if (reg_off_ll
!= -1) {
2420 * The 2 bytes at offsets of 2 and 3 from the beginning
2421 * of the radiotap header are the length of the radiotap
2422 * header; unfortunately, it's little-endian, so we have
2423 * to load it a byte at a time and construct the value.
2427 * Load the high-order byte, at an offset of 3, shift it
2428 * left a byte, and put the result in the X register.
2430 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2432 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2435 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2439 * Load the next byte, at an offset of 2, and OR the
2440 * value from the X register into it.
2442 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2445 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2449 * Now allocate a register to hold that value and store
2452 s2
= new_stmt(BPF_ST
);
2453 s2
->s
.k
= reg_off_ll
;
2457 * Now move it into the X register.
2459 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2468 * At the moment we treat PPI as normal Radiotap encoded
2469 * packets. The difference is in the function that generates
2470 * the code at the beginning to compute the header length.
2471 * Since this code generator of PPI supports bare 802.11
2472 * encapsulation only (i.e. the encapsulated DLT should be
2473 * DLT_IEEE802_11) we generate code to check for this too;
2474 * that's done in finish_parse().
2476 static struct slist
*
2477 gen_load_ppi_llprefixlen()
2479 struct slist
*s1
, *s2
;
2482 * Generate code to load the length of the radiotap header
2483 * into the register assigned to hold that length, if one has
2486 if (reg_off_ll
!= -1) {
2488 * The 2 bytes at offsets of 2 and 3 from the beginning
2489 * of the radiotap header are the length of the radiotap
2490 * header; unfortunately, it's little-endian, so we have
2491 * to load it a byte at a time and construct the value.
2495 * Load the high-order byte, at an offset of 3, shift it
2496 * left a byte, and put the result in the X register.
2498 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2500 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2503 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2507 * Load the next byte, at an offset of 2, and OR the
2508 * value from the X register into it.
2510 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2513 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2517 * Now allocate a register to hold that value and store
2520 s2
= new_stmt(BPF_ST
);
2521 s2
->s
.k
= reg_off_ll
;
2525 * Now move it into the X register.
2527 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2536 * Load a value relative to the beginning of the link-layer header after the 802.11
2537 * header, i.e. LLC_SNAP.
2538 * The link-layer header doesn't necessarily begin at the beginning
2539 * of the packet data; there might be a variable-length prefix containing
2540 * radio information.
2542 static struct slist
*
2543 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2546 struct slist
*sjset_data_frame_1
;
2547 struct slist
*sjset_data_frame_2
;
2548 struct slist
*sjset_qos
;
2549 struct slist
*sjset_radiotap_flags
;
2550 struct slist
*sjset_radiotap_tsft
;
2551 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2552 struct slist
*s_roundup
;
2554 if (reg_off_macpl
== -1) {
2556 * No register has been assigned to the offset of
2557 * the MAC-layer payload, which means nobody needs
2558 * it; don't bother computing it - just return
2559 * what we already have.
2565 * This code is not compatible with the optimizer, as
2566 * we are generating jmp instructions within a normal
2567 * slist of instructions
2572 * If "s" is non-null, it has code to arrange that the X register
2573 * contains the length of the prefix preceding the link-layer
2576 * Otherwise, the length of the prefix preceding the link-layer
2577 * header is "off_ll".
2581 * There is no variable-length header preceding the
2582 * link-layer header.
2584 * Load the length of the fixed-length prefix preceding
2585 * the link-layer header (if any) into the X register,
2586 * and store it in the reg_off_macpl register.
2587 * That length is off_ll.
2589 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2594 * The X register contains the offset of the beginning of the
2595 * link-layer header; add 24, which is the minimum length
2596 * of the MAC header for a data frame, to that, and store it
2597 * in reg_off_macpl, and then load the Frame Control field,
2598 * which is at the offset in the X register, with an indexed load.
2600 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2602 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2605 s2
= new_stmt(BPF_ST
);
2606 s2
->s
.k
= reg_off_macpl
;
2609 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2614 * Check the Frame Control field to see if this is a data frame;
2615 * a data frame has the 0x08 bit (b3) in that field set and the
2616 * 0x04 bit (b2) clear.
2618 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2619 sjset_data_frame_1
->s
.k
= 0x08;
2620 sappend(s
, sjset_data_frame_1
);
2623 * If b3 is set, test b2, otherwise go to the first statement of
2624 * the rest of the program.
2626 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2627 sjset_data_frame_2
->s
.k
= 0x04;
2628 sappend(s
, sjset_data_frame_2
);
2629 sjset_data_frame_1
->s
.jf
= snext
;
2632 * If b2 is not set, this is a data frame; test the QoS bit.
2633 * Otherwise, go to the first statement of the rest of the
2636 sjset_data_frame_2
->s
.jt
= snext
;
2637 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2638 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2639 sappend(s
, sjset_qos
);
2642 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2644 * Otherwise, go to the first statement of the rest of the
2647 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2648 s2
->s
.k
= reg_off_macpl
;
2650 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2653 s2
= new_stmt(BPF_ST
);
2654 s2
->s
.k
= reg_off_macpl
;
2658 * If we have a radiotap header, look at it to see whether
2659 * there's Atheros padding between the MAC-layer header
2662 * Note: all of the fields in the radiotap header are
2663 * little-endian, so we byte-swap all of the values
2664 * we test against, as they will be loaded as big-endian
2667 if (linktype
== DLT_IEEE802_11_RADIO
) {
2669 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2670 * in the presence flag?
2672 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2676 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2677 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2678 sappend(s
, sjset_radiotap_flags
);
2681 * If not, skip all of this.
2683 sjset_radiotap_flags
->s
.jf
= snext
;
2686 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2688 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2689 new_stmt(JMP(BPF_JSET
));
2690 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2691 sappend(s
, sjset_radiotap_tsft
);
2694 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2695 * at an offset of 16 from the beginning of the raw packet
2696 * data (8 bytes for the radiotap header and 8 bytes for
2699 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2702 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2706 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2707 sjset_tsft_datapad
->s
.k
= 0x20;
2708 sappend(s
, sjset_tsft_datapad
);
2711 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2712 * at an offset of 8 from the beginning of the raw packet
2713 * data (8 bytes for the radiotap header).
2715 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2718 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2722 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2723 sjset_notsft_datapad
->s
.k
= 0x20;
2724 sappend(s
, sjset_notsft_datapad
);
2727 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2728 * set, round the length of the 802.11 header to
2729 * a multiple of 4. Do that by adding 3 and then
2730 * dividing by and multiplying by 4, which we do by
2733 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2734 s_roundup
->s
.k
= reg_off_macpl
;
2735 sappend(s
, s_roundup
);
2736 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2739 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2742 s2
= new_stmt(BPF_ST
);
2743 s2
->s
.k
= reg_off_macpl
;
2746 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2747 sjset_tsft_datapad
->s
.jf
= snext
;
2748 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2749 sjset_notsft_datapad
->s
.jf
= snext
;
2751 sjset_qos
->s
.jf
= snext
;
2757 insert_compute_vloffsets(b
)
2763 * For link-layer types that have a variable-length header
2764 * preceding the link-layer header, generate code to load
2765 * the offset of the link-layer header into the register
2766 * assigned to that offset, if any.
2770 case DLT_PRISM_HEADER
:
2771 s
= gen_load_prism_llprefixlen();
2774 case DLT_IEEE802_11_RADIO_AVS
:
2775 s
= gen_load_avs_llprefixlen();
2778 case DLT_IEEE802_11_RADIO
:
2779 s
= gen_load_radiotap_llprefixlen();
2783 s
= gen_load_ppi_llprefixlen();
2792 * For link-layer types that have a variable-length link-layer
2793 * header, generate code to load the offset of the MAC-layer
2794 * payload into the register assigned to that offset, if any.
2798 case DLT_IEEE802_11
:
2799 case DLT_PRISM_HEADER
:
2800 case DLT_IEEE802_11_RADIO_AVS
:
2801 case DLT_IEEE802_11_RADIO
:
2803 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2808 * If we have any offset-loading code, append all the
2809 * existing statements in the block to those statements,
2810 * and make the resulting list the list of statements
2814 sappend(s
, b
->stmts
);
2819 static struct block
*
2820 gen_ppi_dlt_check(void)
2822 struct slist
*s_load_dlt
;
2825 if (linktype
== DLT_PPI
)
2827 /* Create the statements that check for the DLT
2829 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2830 s_load_dlt
->s
.k
= 4;
2832 b
= new_block(JMP(BPF_JEQ
));
2834 b
->stmts
= s_load_dlt
;
2835 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2845 static struct slist
*
2846 gen_prism_llprefixlen(void)
2850 if (reg_off_ll
== -1) {
2852 * We haven't yet assigned a register for the length
2853 * of the radio header; allocate one.
2855 reg_off_ll
= alloc_reg();
2859 * Load the register containing the radio length
2860 * into the X register.
2862 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2863 s
->s
.k
= reg_off_ll
;
2867 static struct slist
*
2868 gen_avs_llprefixlen(void)
2872 if (reg_off_ll
== -1) {
2874 * We haven't yet assigned a register for the length
2875 * of the AVS header; allocate one.
2877 reg_off_ll
= alloc_reg();
2881 * Load the register containing the AVS length
2882 * into the X register.
2884 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2885 s
->s
.k
= reg_off_ll
;
2889 static struct slist
*
2890 gen_radiotap_llprefixlen(void)
2894 if (reg_off_ll
== -1) {
2896 * We haven't yet assigned a register for the length
2897 * of the radiotap header; allocate one.
2899 reg_off_ll
= alloc_reg();
2903 * Load the register containing the radiotap length
2904 * into the X register.
2906 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2907 s
->s
.k
= reg_off_ll
;
2912 * At the moment we treat PPI as normal Radiotap encoded
2913 * packets. The difference is in the function that generates
2914 * the code at the beginning to compute the header length.
2915 * Since this code generator of PPI supports bare 802.11
2916 * encapsulation only (i.e. the encapsulated DLT should be
2917 * DLT_IEEE802_11) we generate code to check for this too.
2919 static struct slist
*
2920 gen_ppi_llprefixlen(void)
2924 if (reg_off_ll
== -1) {
2926 * We haven't yet assigned a register for the length
2927 * of the radiotap header; allocate one.
2929 reg_off_ll
= alloc_reg();
2933 * Load the register containing the PPI length
2934 * into the X register.
2936 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2937 s
->s
.k
= reg_off_ll
;
2942 * Generate code to compute the link-layer header length, if necessary,
2943 * putting it into the X register, and to return either a pointer to a
2944 * "struct slist" for the list of statements in that code, or NULL if
2945 * no code is necessary.
2947 static struct slist
*
2948 gen_llprefixlen(void)
2952 case DLT_PRISM_HEADER
:
2953 return gen_prism_llprefixlen();
2955 case DLT_IEEE802_11_RADIO_AVS
:
2956 return gen_avs_llprefixlen();
2958 case DLT_IEEE802_11_RADIO
:
2959 return gen_radiotap_llprefixlen();
2962 return gen_ppi_llprefixlen();
2970 * Generate code to load the register containing the offset of the
2971 * MAC-layer payload into the X register; if no register for that offset
2972 * has been allocated, allocate it first.
2974 static struct slist
*
2979 if (off_macpl_is_variable
) {
2980 if (reg_off_macpl
== -1) {
2982 * We haven't yet assigned a register for the offset
2983 * of the MAC-layer payload; allocate one.
2985 reg_off_macpl
= alloc_reg();
2989 * Load the register containing the offset of the MAC-layer
2990 * payload into the X register.
2992 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2993 s
->s
.k
= reg_off_macpl
;
2997 * That offset isn't variable, so we don't need to
2998 * generate any code.
3005 * Map an Ethernet type to the equivalent PPP type.
3008 ethertype_to_ppptype(proto
)
3018 case ETHERTYPE_IPV6
:
3027 case ETHERTYPE_ATALK
:
3041 * I'm assuming the "Bridging PDU"s that go
3042 * over PPP are Spanning Tree Protocol
3056 * Generate code to match a particular packet type by matching the
3057 * link-layer type field or fields in the 802.2 LLC header.
3059 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3060 * value, if <= ETHERMTU.
3062 static struct block
*
3066 struct block
*b0
, *b1
, *b2
;
3068 /* are we checking MPLS-encapsulated packets? */
3069 if (label_stack_depth
> 0) {
3073 /* FIXME add other L3 proto IDs */
3074 return gen_mpls_linktype(Q_IP
);
3076 case ETHERTYPE_IPV6
:
3078 /* FIXME add other L3 proto IDs */
3079 return gen_mpls_linktype(Q_IPV6
);
3082 bpf_error("unsupported protocol over mpls");
3088 * Are we testing PPPoE packets?
3092 * The PPPoE session header is part of the
3093 * MAC-layer payload, so all references
3094 * should be relative to the beginning of
3099 * We use Ethernet protocol types inside libpcap;
3100 * map them to the corresponding PPP protocol types.
3102 proto
= ethertype_to_ppptype(proto
);
3103 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3109 case DLT_NETANALYZER
:
3110 case DLT_NETANALYZER_TRANSPARENT
:
3111 return gen_ether_linktype(proto
);
3119 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3123 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3130 case DLT_IEEE802_11
:
3131 case DLT_PRISM_HEADER
:
3132 case DLT_IEEE802_11_RADIO_AVS
:
3133 case DLT_IEEE802_11_RADIO
:
3136 * Check that we have a data frame.
3138 b0
= gen_check_802_11_data_frame();
3141 * Now check for the specified link-layer type.
3143 b1
= gen_llc_linktype(proto
);
3151 * XXX - check for asynchronous frames, as per RFC 1103.
3153 return gen_llc_linktype(proto
);
3159 * XXX - check for LLC PDUs, as per IEEE 802.5.
3161 return gen_llc_linktype(proto
);
3165 case DLT_ATM_RFC1483
:
3167 case DLT_IP_OVER_FC
:
3168 return gen_llc_linktype(proto
);
3174 * If "is_lane" is set, check for a LANE-encapsulated
3175 * version of this protocol, otherwise check for an
3176 * LLC-encapsulated version of this protocol.
3178 * We assume LANE means Ethernet, not Token Ring.
3182 * Check that the packet doesn't begin with an
3183 * LE Control marker. (We've already generated
3186 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3191 * Now generate an Ethernet test.
3193 b1
= gen_ether_linktype(proto
);
3198 * Check for LLC encapsulation and then check the
3201 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3202 b1
= gen_llc_linktype(proto
);
3210 return gen_linux_sll_linktype(proto
);
3215 case DLT_SLIP_BSDOS
:
3218 * These types don't provide any type field; packets
3219 * are always IPv4 or IPv6.
3221 * XXX - for IPv4, check for a version number of 4, and,
3222 * for IPv6, check for a version number of 6?
3227 /* Check for a version number of 4. */
3228 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3230 case ETHERTYPE_IPV6
:
3231 /* Check for a version number of 6. */
3232 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3236 return gen_false(); /* always false */
3243 * Raw IPv4, so no type field.
3245 if (proto
== ETHERTYPE_IP
)
3246 return gen_true(); /* always true */
3248 /* Checking for something other than IPv4; always false */
3255 * Raw IPv6, so no type field.
3258 if (proto
== ETHERTYPE_IPV6
)
3259 return gen_true(); /* always true */
3262 /* Checking for something other than IPv6; always false */
3269 case DLT_PPP_SERIAL
:
3272 * We use Ethernet protocol types inside libpcap;
3273 * map them to the corresponding PPP protocol types.
3275 proto
= ethertype_to_ppptype(proto
);
3276 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3282 * We use Ethernet protocol types inside libpcap;
3283 * map them to the corresponding PPP protocol types.
3289 * Also check for Van Jacobson-compressed IP.
3290 * XXX - do this for other forms of PPP?
3292 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3293 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3295 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3300 proto
= ethertype_to_ppptype(proto
);
3301 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3311 * For DLT_NULL, the link-layer header is a 32-bit
3312 * word containing an AF_ value in *host* byte order,
3313 * and for DLT_ENC, the link-layer header begins
3314 * with a 32-bit work containing an AF_ value in
3317 * In addition, if we're reading a saved capture file,
3318 * the host byte order in the capture may not be the
3319 * same as the host byte order on this machine.
3321 * For DLT_LOOP, the link-layer header is a 32-bit
3322 * word containing an AF_ value in *network* byte order.
3324 * XXX - AF_ values may, unfortunately, be platform-
3325 * dependent; for example, FreeBSD's AF_INET6 is 24
3326 * whilst NetBSD's and OpenBSD's is 26.
3328 * This means that, when reading a capture file, just
3329 * checking for our AF_INET6 value won't work if the
3330 * capture file came from another OS.
3339 case ETHERTYPE_IPV6
:
3346 * Not a type on which we support filtering.
3347 * XXX - support those that have AF_ values
3348 * #defined on this platform, at least?
3353 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3355 * The AF_ value is in host byte order, but
3356 * the BPF interpreter will convert it to
3357 * network byte order.
3359 * If this is a save file, and it's from a
3360 * machine with the opposite byte order to
3361 * ours, we byte-swap the AF_ value.
3363 * Then we run it through "htonl()", and
3364 * generate code to compare against the result.
3366 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3367 bpf_pcap
->sf
.swapped
)
3368 proto
= SWAPLONG(proto
);
3369 proto
= htonl(proto
);
3371 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3373 #ifdef HAVE_NET_PFVAR_H
3376 * af field is host byte order in contrast to the rest of
3379 if (proto
== ETHERTYPE_IP
)
3380 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3381 BPF_B
, (bpf_int32
)AF_INET
));
3383 else if (proto
== ETHERTYPE_IPV6
)
3384 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3385 BPF_B
, (bpf_int32
)AF_INET6
));
3391 #endif /* HAVE_NET_PFVAR_H */
3394 case DLT_ARCNET_LINUX
:
3396 * XXX should we check for first fragment if the protocol
3405 case ETHERTYPE_IPV6
:
3406 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3407 (bpf_int32
)ARCTYPE_INET6
));
3411 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3412 (bpf_int32
)ARCTYPE_IP
);
3413 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3414 (bpf_int32
)ARCTYPE_IP_OLD
);
3419 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3420 (bpf_int32
)ARCTYPE_ARP
);
3421 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3422 (bpf_int32
)ARCTYPE_ARP_OLD
);
3426 case ETHERTYPE_REVARP
:
3427 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3428 (bpf_int32
)ARCTYPE_REVARP
));
3430 case ETHERTYPE_ATALK
:
3431 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3432 (bpf_int32
)ARCTYPE_ATALK
));
3439 case ETHERTYPE_ATALK
:
3449 * XXX - assumes a 2-byte Frame Relay header with
3450 * DLCI and flags. What if the address is longer?
3456 * Check for the special NLPID for IP.
3458 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3461 case ETHERTYPE_IPV6
:
3463 * Check for the special NLPID for IPv6.
3465 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3470 * Check for several OSI protocols.
3472 * Frame Relay packets typically have an OSI
3473 * NLPID at the beginning; we check for each
3476 * What we check for is the NLPID and a frame
3477 * control field of UI, i.e. 0x03 followed
3480 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3481 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3482 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3494 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3496 case DLT_JUNIPER_MFR
:
3497 case DLT_JUNIPER_MLFR
:
3498 case DLT_JUNIPER_MLPPP
:
3499 case DLT_JUNIPER_ATM1
:
3500 case DLT_JUNIPER_ATM2
:
3501 case DLT_JUNIPER_PPPOE
:
3502 case DLT_JUNIPER_PPPOE_ATM
:
3503 case DLT_JUNIPER_GGSN
:
3504 case DLT_JUNIPER_ES
:
3505 case DLT_JUNIPER_MONITOR
:
3506 case DLT_JUNIPER_SERVICES
:
3507 case DLT_JUNIPER_ETHER
:
3508 case DLT_JUNIPER_PPP
:
3509 case DLT_JUNIPER_FRELAY
:
3510 case DLT_JUNIPER_CHDLC
:
3511 case DLT_JUNIPER_VP
:
3512 case DLT_JUNIPER_ST
:
3513 case DLT_JUNIPER_ISM
:
3514 case DLT_JUNIPER_VS
:
3515 case DLT_JUNIPER_SRX_E2E
:
3516 case DLT_JUNIPER_FIBRECHANNEL
:
3517 case DLT_JUNIPER_ATM_CEMIC
:
3519 /* just lets verify the magic number for now -
3520 * on ATM we may have up to 6 different encapsulations on the wire
3521 * and need a lot of heuristics to figure out that the payload
3524 * FIXME encapsulation specific BPF_ filters
3526 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3529 return gen_ipnet_linktype(proto
);
3531 case DLT_LINUX_IRDA
:
3532 bpf_error("IrDA link-layer type filtering not implemented");
3535 bpf_error("DOCSIS link-layer type filtering not implemented");
3538 case DLT_MTP2_WITH_PHDR
:
3539 bpf_error("MTP2 link-layer type filtering not implemented");
3542 bpf_error("ERF link-layer type filtering not implemented");
3546 bpf_error("PFSYNC link-layer type filtering not implemented");
3549 case DLT_LINUX_LAPD
:
3550 bpf_error("LAPD link-layer type filtering not implemented");
3554 case DLT_USB_LINUX_MMAPPED
:
3555 bpf_error("USB link-layer type filtering not implemented");
3557 case DLT_BLUETOOTH_HCI_H4
:
3558 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3559 bpf_error("Bluetooth link-layer type filtering not implemented");
3562 case DLT_CAN_SOCKETCAN
:
3563 bpf_error("CAN link-layer type filtering not implemented");
3565 case DLT_IEEE802_15_4
:
3566 case DLT_IEEE802_15_4_LINUX
:
3567 case DLT_IEEE802_15_4_NONASK_PHY
:
3568 case DLT_IEEE802_15_4_NOFCS
:
3569 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3571 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3572 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3575 bpf_error("SITA link-layer type filtering not implemented");
3578 bpf_error("RAIF1 link-layer type filtering not implemented");
3581 bpf_error("IPMB link-layer type filtering not implemented");
3584 bpf_error("AX.25 link-layer type filtering not implemented");
3588 * All the types that have no encapsulation should either be
3589 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3590 * all packets are IP packets, or should be handled in some
3591 * special case, if none of them are (if some are and some
3592 * aren't, the lack of encapsulation is a problem, as we'd
3593 * have to find some other way of determining the packet type).
3595 * Therefore, if "off_linktype" is -1, there's an error.
3597 if (off_linktype
== (u_int
)-1)
3601 * Any type not handled above should always have an Ethernet
3602 * type at an offset of "off_linktype".
3604 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3608 * Check for an LLC SNAP packet with a given organization code and
3609 * protocol type; we check the entire contents of the 802.2 LLC and
3610 * snap headers, checking for DSAP and SSAP of SNAP and a control
3611 * field of 0x03 in the LLC header, and for the specified organization
3612 * code and protocol type in the SNAP header.
3614 static struct block
*
3615 gen_snap(orgcode
, ptype
)
3616 bpf_u_int32 orgcode
;
3619 u_char snapblock
[8];
3621 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3622 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3623 snapblock
[2] = 0x03; /* control = UI */
3624 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3625 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3626 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3627 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3628 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3629 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3633 * Generate code to match a particular packet type, for link-layer types
3634 * using 802.2 LLC headers.
3636 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3637 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3639 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3640 * value, if <= ETHERMTU. We use that to determine whether to
3641 * match the DSAP or both DSAP and LSAP or to check the OUI and
3642 * protocol ID in a SNAP header.
3644 static struct block
*
3645 gen_llc_linktype(proto
)
3649 * XXX - handle token-ring variable-length header.
3655 case LLCSAP_NETBEUI
:
3657 * XXX - should we check both the DSAP and the
3658 * SSAP, like this, or should we check just the
3659 * DSAP, as we do for other types <= ETHERMTU
3660 * (i.e., other SAP values)?
3662 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3663 ((proto
<< 8) | proto
));
3667 * XXX - are there ever SNAP frames for IPX on
3668 * non-Ethernet 802.x networks?
3670 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3671 (bpf_int32
)LLCSAP_IPX
);
3673 case ETHERTYPE_ATALK
:
3675 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3676 * SNAP packets with an organization code of
3677 * 0x080007 (Apple, for Appletalk) and a protocol
3678 * type of ETHERTYPE_ATALK (Appletalk).
3680 * XXX - check for an organization code of
3681 * encapsulated Ethernet as well?
3683 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3687 * XXX - we don't have to check for IPX 802.3
3688 * here, but should we check for the IPX Ethertype?
3690 if (proto
<= ETHERMTU
) {
3692 * This is an LLC SAP value, so check
3695 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3698 * This is an Ethernet type; we assume that it's
3699 * unlikely that it'll appear in the right place
3700 * at random, and therefore check only the
3701 * location that would hold the Ethernet type
3702 * in a SNAP frame with an organization code of
3703 * 0x000000 (encapsulated Ethernet).
3705 * XXX - if we were to check for the SNAP DSAP and
3706 * LSAP, as per XXX, and were also to check for an
3707 * organization code of 0x000000 (encapsulated
3708 * Ethernet), we'd do
3710 * return gen_snap(0x000000, proto);
3712 * here; for now, we don't, as per the above.
3713 * I don't know whether it's worth the extra CPU
3714 * time to do the right check or not.
3716 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3721 static struct block
*
3722 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3726 u_int src_off
, dst_off
;
3728 struct block
*b0
, *b1
;
3742 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3743 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3749 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3750 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3757 b0
= gen_linktype(proto
);
3758 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3764 static struct block
*
3765 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3766 struct in6_addr
*addr
;
3767 struct in6_addr
*mask
;
3769 u_int src_off
, dst_off
;
3771 struct block
*b0
, *b1
;
3786 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3787 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3793 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3794 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3801 /* this order is important */
3802 a
= (u_int32_t
*)addr
;
3803 m
= (u_int32_t
*)mask
;
3804 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3805 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3807 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3809 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3811 b0
= gen_linktype(proto
);
3817 static struct block
*
3818 gen_ehostop(eaddr
, dir
)
3819 register const u_char
*eaddr
;
3822 register struct block
*b0
, *b1
;
3826 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3829 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3832 b0
= gen_ehostop(eaddr
, Q_SRC
);
3833 b1
= gen_ehostop(eaddr
, Q_DST
);
3839 b0
= gen_ehostop(eaddr
, Q_SRC
);
3840 b1
= gen_ehostop(eaddr
, Q_DST
);
3845 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3849 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3853 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3857 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3861 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3865 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3873 * Like gen_ehostop, but for DLT_FDDI
3875 static struct block
*
3876 gen_fhostop(eaddr
, dir
)
3877 register const u_char
*eaddr
;
3880 struct block
*b0
, *b1
;
3885 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3887 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3892 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3894 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3898 b0
= gen_fhostop(eaddr
, Q_SRC
);
3899 b1
= gen_fhostop(eaddr
, Q_DST
);
3905 b0
= gen_fhostop(eaddr
, Q_SRC
);
3906 b1
= gen_fhostop(eaddr
, Q_DST
);
3911 bpf_error("'addr1' is only supported on 802.11");
3915 bpf_error("'addr2' is only supported on 802.11");
3919 bpf_error("'addr3' is only supported on 802.11");
3923 bpf_error("'addr4' is only supported on 802.11");
3927 bpf_error("'ra' is only supported on 802.11");
3931 bpf_error("'ta' is only supported on 802.11");
3939 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3941 static struct block
*
3942 gen_thostop(eaddr
, dir
)
3943 register const u_char
*eaddr
;
3946 register struct block
*b0
, *b1
;
3950 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3953 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3956 b0
= gen_thostop(eaddr
, Q_SRC
);
3957 b1
= gen_thostop(eaddr
, Q_DST
);
3963 b0
= gen_thostop(eaddr
, Q_SRC
);
3964 b1
= gen_thostop(eaddr
, Q_DST
);
3969 bpf_error("'addr1' is only supported on 802.11");
3973 bpf_error("'addr2' is only supported on 802.11");
3977 bpf_error("'addr3' is only supported on 802.11");
3981 bpf_error("'addr4' is only supported on 802.11");
3985 bpf_error("'ra' is only supported on 802.11");
3989 bpf_error("'ta' is only supported on 802.11");
3997 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3998 * various 802.11 + radio headers.
4000 static struct block
*
4001 gen_wlanhostop(eaddr
, dir
)
4002 register const u_char
*eaddr
;
4005 register struct block
*b0
, *b1
, *b2
;
4006 register struct slist
*s
;
4008 #ifdef ENABLE_WLAN_FILTERING_PATCH
4011 * We need to disable the optimizer because the optimizer is buggy
4012 * and wipes out some LD instructions generated by the below
4013 * code to validate the Frame Control bits
4016 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4023 * For control frames, there is no SA.
4025 * For management frames, SA is at an
4026 * offset of 10 from the beginning of
4029 * For data frames, SA is at an offset
4030 * of 10 from the beginning of the packet
4031 * if From DS is clear, at an offset of
4032 * 16 from the beginning of the packet
4033 * if From DS is set and To DS is clear,
4034 * and an offset of 24 from the beginning
4035 * of the packet if From DS is set and To DS
4040 * Generate the tests to be done for data frames
4043 * First, check for To DS set, i.e. check "link[1] & 0x01".
4045 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4046 b1
= new_block(JMP(BPF_JSET
));
4047 b1
->s
.k
= 0x01; /* To DS */
4051 * If To DS is set, the SA is at 24.
4053 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4057 * Now, check for To DS not set, i.e. check
4058 * "!(link[1] & 0x01)".
4060 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4061 b2
= new_block(JMP(BPF_JSET
));
4062 b2
->s
.k
= 0x01; /* To DS */
4067 * If To DS is not set, the SA is at 16.
4069 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4073 * Now OR together the last two checks. That gives
4074 * the complete set of checks for data frames with
4080 * Now check for From DS being set, and AND that with
4081 * the ORed-together checks.
4083 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4084 b1
= new_block(JMP(BPF_JSET
));
4085 b1
->s
.k
= 0x02; /* From DS */
4090 * Now check for data frames with From DS not set.
4092 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4093 b2
= new_block(JMP(BPF_JSET
));
4094 b2
->s
.k
= 0x02; /* From DS */
4099 * If From DS isn't set, the SA is at 10.
4101 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4105 * Now OR together the checks for data frames with
4106 * From DS not set and for data frames with From DS
4107 * set; that gives the checks done for data frames.
4112 * Now check for a data frame.
4113 * I.e, check "link[0] & 0x08".
4115 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4116 b1
= new_block(JMP(BPF_JSET
));
4121 * AND that with the checks done for data frames.
4126 * If the high-order bit of the type value is 0, this
4127 * is a management frame.
4128 * I.e, check "!(link[0] & 0x08)".
4130 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4131 b2
= new_block(JMP(BPF_JSET
));
4137 * For management frames, the SA is at 10.
4139 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4143 * OR that with the checks done for data frames.
4144 * That gives the checks done for management and
4150 * If the low-order bit of the type value is 1,
4151 * this is either a control frame or a frame
4152 * with a reserved type, and thus not a
4155 * I.e., check "!(link[0] & 0x04)".
4157 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4158 b1
= new_block(JMP(BPF_JSET
));
4164 * AND that with the checks for data and management
4174 * For control frames, there is no DA.
4176 * For management frames, DA is at an
4177 * offset of 4 from the beginning of
4180 * For data frames, DA is at an offset
4181 * of 4 from the beginning of the packet
4182 * if To DS is clear and at an offset of
4183 * 16 from the beginning of the packet
4188 * Generate the tests to be done for data frames.
4190 * First, check for To DS set, i.e. "link[1] & 0x01".
4192 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4193 b1
= new_block(JMP(BPF_JSET
));
4194 b1
->s
.k
= 0x01; /* To DS */
4198 * If To DS is set, the DA is at 16.
4200 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4204 * Now, check for To DS not set, i.e. check
4205 * "!(link[1] & 0x01)".
4207 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4208 b2
= new_block(JMP(BPF_JSET
));
4209 b2
->s
.k
= 0x01; /* To DS */
4214 * If To DS is not set, the DA is at 4.
4216 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4220 * Now OR together the last two checks. That gives
4221 * the complete set of checks for data frames.
4226 * Now check for a data frame.
4227 * I.e, check "link[0] & 0x08".
4229 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4230 b1
= new_block(JMP(BPF_JSET
));
4235 * AND that with the checks done for data frames.
4240 * If the high-order bit of the type value is 0, this
4241 * is a management frame.
4242 * I.e, check "!(link[0] & 0x08)".
4244 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4245 b2
= new_block(JMP(BPF_JSET
));
4251 * For management frames, the DA is at 4.
4253 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4257 * OR that with the checks done for data frames.
4258 * That gives the checks done for management and
4264 * If the low-order bit of the type value is 1,
4265 * this is either a control frame or a frame
4266 * with a reserved type, and thus not a
4269 * I.e., check "!(link[0] & 0x04)".
4271 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4272 b1
= new_block(JMP(BPF_JSET
));
4278 * AND that with the checks for data and management
4286 * Not present in management frames; addr1 in other
4291 * If the high-order bit of the type value is 0, this
4292 * is a management frame.
4293 * I.e, check "(link[0] & 0x08)".
4295 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4296 b1
= new_block(JMP(BPF_JSET
));
4303 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4306 * AND that with the check of addr1.
4313 * Not present in management frames; addr2, if present,
4318 * Not present in CTS or ACK control frames.
4320 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4321 IEEE80211_FC0_TYPE_MASK
);
4323 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4324 IEEE80211_FC0_SUBTYPE_MASK
);
4326 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4327 IEEE80211_FC0_SUBTYPE_MASK
);
4333 * If the high-order bit of the type value is 0, this
4334 * is a management frame.
4335 * I.e, check "(link[0] & 0x08)".
4337 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4338 b1
= new_block(JMP(BPF_JSET
));
4343 * AND that with the check for frames other than
4344 * CTS and ACK frames.
4351 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4356 * XXX - add BSSID keyword?
4359 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4363 * Not present in CTS or ACK control frames.
4365 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4366 IEEE80211_FC0_TYPE_MASK
);
4368 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4369 IEEE80211_FC0_SUBTYPE_MASK
);
4371 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4372 IEEE80211_FC0_SUBTYPE_MASK
);
4376 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4382 * Not present in control frames.
4384 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4385 IEEE80211_FC0_TYPE_MASK
);
4387 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4393 * Present only if the direction mask has both "From DS"
4394 * and "To DS" set. Neither control frames nor management
4395 * frames should have both of those set, so we don't
4396 * check the frame type.
4398 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4399 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4400 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4405 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4406 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4412 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4413 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4422 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4423 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4424 * as the RFC states.)
4426 static struct block
*
4427 gen_ipfchostop(eaddr
, dir
)
4428 register const u_char
*eaddr
;
4431 register struct block
*b0
, *b1
;
4435 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4438 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4441 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4442 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4448 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4449 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4454 bpf_error("'addr1' is only supported on 802.11");
4458 bpf_error("'addr2' is only supported on 802.11");
4462 bpf_error("'addr3' is only supported on 802.11");
4466 bpf_error("'addr4' is only supported on 802.11");
4470 bpf_error("'ra' is only supported on 802.11");
4474 bpf_error("'ta' is only supported on 802.11");
4482 * This is quite tricky because there may be pad bytes in front of the
4483 * DECNET header, and then there are two possible data packet formats that
4484 * carry both src and dst addresses, plus 5 packet types in a format that
4485 * carries only the src node, plus 2 types that use a different format and
4486 * also carry just the src node.
4490 * Instead of doing those all right, we just look for data packets with
4491 * 0 or 1 bytes of padding. If you want to look at other packets, that
4492 * will require a lot more hacking.
4494 * To add support for filtering on DECNET "areas" (network numbers)
4495 * one would want to add a "mask" argument to this routine. That would
4496 * make the filter even more inefficient, although one could be clever
4497 * and not generate masking instructions if the mask is 0xFFFF.
4499 static struct block
*
4500 gen_dnhostop(addr
, dir
)
4504 struct block
*b0
, *b1
, *b2
, *tmp
;
4505 u_int offset_lh
; /* offset if long header is received */
4506 u_int offset_sh
; /* offset if short header is received */
4511 offset_sh
= 1; /* follows flags */
4512 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4516 offset_sh
= 3; /* follows flags, dstnode */
4517 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4521 /* Inefficient because we do our Calvinball dance twice */
4522 b0
= gen_dnhostop(addr
, Q_SRC
);
4523 b1
= gen_dnhostop(addr
, Q_DST
);
4529 /* Inefficient because we do our Calvinball dance twice */
4530 b0
= gen_dnhostop(addr
, Q_SRC
);
4531 b1
= gen_dnhostop(addr
, Q_DST
);
4536 bpf_error("ISO host filtering not implemented");
4541 b0
= gen_linktype(ETHERTYPE_DN
);
4542 /* Check for pad = 1, long header case */
4543 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4544 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4545 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4546 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4548 /* Check for pad = 0, long header case */
4549 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4550 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4553 /* Check for pad = 1, short header case */
4554 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4555 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4556 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4559 /* Check for pad = 0, short header case */
4560 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4561 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4565 /* Combine with test for linktype */
4571 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4572 * test the bottom-of-stack bit, and then check the version number
4573 * field in the IP header.
4575 static struct block
*
4576 gen_mpls_linktype(proto
)
4579 struct block
*b0
, *b1
;
4584 /* match the bottom-of-stack bit */
4585 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4586 /* match the IPv4 version number */
4587 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4592 /* match the bottom-of-stack bit */
4593 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4594 /* match the IPv4 version number */
4595 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4604 static struct block
*
4605 gen_host(addr
, mask
, proto
, dir
, type
)
4612 struct block
*b0
, *b1
;
4613 const char *typestr
;
4623 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4625 * Only check for non-IPv4 addresses if we're not
4626 * checking MPLS-encapsulated packets.
4628 if (label_stack_depth
== 0) {
4629 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4631 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4637 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4640 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4643 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4646 bpf_error("'tcp' modifier applied to %s", typestr
);
4649 bpf_error("'sctp' modifier applied to %s", typestr
);
4652 bpf_error("'udp' modifier applied to %s", typestr
);
4655 bpf_error("'icmp' modifier applied to %s", typestr
);
4658 bpf_error("'igmp' modifier applied to %s", typestr
);
4661 bpf_error("'igrp' modifier applied to %s", typestr
);
4664 bpf_error("'pim' modifier applied to %s", typestr
);
4667 bpf_error("'vrrp' modifier applied to %s", typestr
);
4670 bpf_error("ATALK host filtering not implemented");
4673 bpf_error("AARP host filtering not implemented");
4676 return gen_dnhostop(addr
, dir
);
4679 bpf_error("SCA host filtering not implemented");
4682 bpf_error("LAT host filtering not implemented");
4685 bpf_error("MOPDL host filtering not implemented");
4688 bpf_error("MOPRC host filtering not implemented");
4692 bpf_error("'ip6' modifier applied to ip host");
4695 bpf_error("'icmp6' modifier applied to %s", typestr
);
4699 bpf_error("'ah' modifier applied to %s", typestr
);
4702 bpf_error("'esp' modifier applied to %s", typestr
);
4705 bpf_error("ISO host filtering not implemented");
4708 bpf_error("'esis' modifier applied to %s", typestr
);
4711 bpf_error("'isis' modifier applied to %s", typestr
);
4714 bpf_error("'clnp' modifier applied to %s", typestr
);
4717 bpf_error("'stp' modifier applied to %s", typestr
);
4720 bpf_error("IPX host filtering not implemented");
4723 bpf_error("'netbeui' modifier applied to %s", typestr
);
4726 bpf_error("'radio' modifier applied to %s", typestr
);
4735 static struct block
*
4736 gen_host6(addr
, mask
, proto
, dir
, type
)
4737 struct in6_addr
*addr
;
4738 struct in6_addr
*mask
;
4743 const char *typestr
;
4753 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4756 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4759 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4762 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4765 bpf_error("'sctp' modifier applied to %s", typestr
);
4768 bpf_error("'tcp' modifier applied to %s", typestr
);
4771 bpf_error("'udp' modifier applied to %s", typestr
);
4774 bpf_error("'icmp' modifier applied to %s", typestr
);
4777 bpf_error("'igmp' modifier applied to %s", typestr
);
4780 bpf_error("'igrp' modifier applied to %s", typestr
);
4783 bpf_error("'pim' modifier applied to %s", typestr
);
4786 bpf_error("'vrrp' modifier applied to %s", typestr
);
4789 bpf_error("ATALK host filtering not implemented");
4792 bpf_error("AARP host filtering not implemented");
4795 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4798 bpf_error("SCA host filtering not implemented");
4801 bpf_error("LAT host filtering not implemented");
4804 bpf_error("MOPDL host filtering not implemented");
4807 bpf_error("MOPRC host filtering not implemented");
4810 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4813 bpf_error("'icmp6' modifier applied to %s", typestr
);
4816 bpf_error("'ah' modifier applied to %s", typestr
);
4819 bpf_error("'esp' modifier applied to %s", typestr
);
4822 bpf_error("ISO host filtering not implemented");
4825 bpf_error("'esis' modifier applied to %s", typestr
);
4828 bpf_error("'isis' modifier applied to %s", typestr
);
4831 bpf_error("'clnp' modifier applied to %s", typestr
);
4834 bpf_error("'stp' modifier applied to %s", typestr
);
4837 bpf_error("IPX host filtering not implemented");
4840 bpf_error("'netbeui' modifier applied to %s", typestr
);
4843 bpf_error("'radio' modifier applied to %s", typestr
);
4853 static struct block
*
4854 gen_gateway(eaddr
, alist
, proto
, dir
)
4855 const u_char
*eaddr
;
4856 bpf_u_int32
**alist
;
4860 struct block
*b0
, *b1
, *tmp
;
4863 bpf_error("direction applied to 'gateway'");
4872 case DLT_NETANALYZER
:
4873 case DLT_NETANALYZER_TRANSPARENT
:
4874 b0
= gen_ehostop(eaddr
, Q_OR
);
4877 b0
= gen_fhostop(eaddr
, Q_OR
);
4880 b0
= gen_thostop(eaddr
, Q_OR
);
4882 case DLT_IEEE802_11
:
4883 case DLT_PRISM_HEADER
:
4884 case DLT_IEEE802_11_RADIO_AVS
:
4885 case DLT_IEEE802_11_RADIO
:
4887 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4892 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4894 * Check that the packet doesn't begin with an
4895 * LE Control marker. (We've already generated
4898 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4903 * Now check the MAC address.
4905 b0
= gen_ehostop(eaddr
, Q_OR
);
4908 case DLT_IP_OVER_FC
:
4909 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4913 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4915 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4917 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4926 bpf_error("illegal modifier of 'gateway'");
4932 gen_proto_abbrev(proto
)
4941 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4943 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4949 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4951 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4957 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4959 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4965 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4968 #ifndef IPPROTO_IGMP
4969 #define IPPROTO_IGMP 2
4973 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4976 #ifndef IPPROTO_IGRP
4977 #define IPPROTO_IGRP 9
4980 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4984 #define IPPROTO_PIM 103
4988 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4990 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4995 #ifndef IPPROTO_VRRP
4996 #define IPPROTO_VRRP 112
5000 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5004 b1
= gen_linktype(ETHERTYPE_IP
);
5008 b1
= gen_linktype(ETHERTYPE_ARP
);
5012 b1
= gen_linktype(ETHERTYPE_REVARP
);
5016 bpf_error("link layer applied in wrong context");
5019 b1
= gen_linktype(ETHERTYPE_ATALK
);
5023 b1
= gen_linktype(ETHERTYPE_AARP
);
5027 b1
= gen_linktype(ETHERTYPE_DN
);
5031 b1
= gen_linktype(ETHERTYPE_SCA
);
5035 b1
= gen_linktype(ETHERTYPE_LAT
);
5039 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5043 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5048 b1
= gen_linktype(ETHERTYPE_IPV6
);
5051 #ifndef IPPROTO_ICMPV6
5052 #define IPPROTO_ICMPV6 58
5055 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5060 #define IPPROTO_AH 51
5063 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5065 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5071 #define IPPROTO_ESP 50
5074 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5076 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5082 b1
= gen_linktype(LLCSAP_ISONS
);
5086 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5090 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5093 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5094 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5095 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5097 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5099 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5101 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5105 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5106 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5107 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5109 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5111 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5113 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5117 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5118 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5119 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5121 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5126 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5127 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5132 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5133 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5135 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5137 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5142 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5143 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5148 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5149 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5154 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5158 b1
= gen_linktype(LLCSAP_8021D
);
5162 b1
= gen_linktype(LLCSAP_IPX
);
5166 b1
= gen_linktype(LLCSAP_NETBEUI
);
5170 bpf_error("'radio' is not a valid protocol type");
5178 static struct block
*
5184 /* not IPv4 frag other than the first frag */
5185 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5186 b
= new_block(JMP(BPF_JSET
));
5195 * Generate a comparison to a port value in the transport-layer header
5196 * at the specified offset from the beginning of that header.
5198 * XXX - this handles a variable-length prefix preceding the link-layer
5199 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5200 * variable-length link-layer headers (such as Token Ring or 802.11
5203 static struct block
*
5204 gen_portatom(off
, v
)
5208 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5212 static struct block
*
5213 gen_portatom6(off
, v
)
5217 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5222 gen_portop(port
, proto
, dir
)
5223 int port
, proto
, dir
;
5225 struct block
*b0
, *b1
, *tmp
;
5227 /* ip proto 'proto' and not a fragment other than the first fragment */
5228 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5234 b1
= gen_portatom(0, (bpf_int32
)port
);
5238 b1
= gen_portatom(2, (bpf_int32
)port
);
5243 tmp
= gen_portatom(0, (bpf_int32
)port
);
5244 b1
= gen_portatom(2, (bpf_int32
)port
);
5249 tmp
= gen_portatom(0, (bpf_int32
)port
);
5250 b1
= gen_portatom(2, (bpf_int32
)port
);
5262 static struct block
*
5263 gen_port(port
, ip_proto
, dir
)
5268 struct block
*b0
, *b1
, *tmp
;
5273 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5274 * not LLC encapsulation with LLCSAP_IP.
5276 * For IEEE 802 networks - which includes 802.5 token ring
5277 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5278 * says that SNAP encapsulation is used, not LLC encapsulation
5281 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5282 * RFC 2225 say that SNAP encapsulation is used, not LLC
5283 * encapsulation with LLCSAP_IP.
5285 * So we always check for ETHERTYPE_IP.
5287 b0
= gen_linktype(ETHERTYPE_IP
);
5293 b1
= gen_portop(port
, ip_proto
, dir
);
5297 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5298 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5300 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5313 gen_portop6(port
, proto
, dir
)
5314 int port
, proto
, dir
;
5316 struct block
*b0
, *b1
, *tmp
;
5318 /* ip6 proto 'proto' */
5319 /* XXX - catch the first fragment of a fragmented packet? */
5320 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5324 b1
= gen_portatom6(0, (bpf_int32
)port
);
5328 b1
= gen_portatom6(2, (bpf_int32
)port
);
5333 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5334 b1
= gen_portatom6(2, (bpf_int32
)port
);
5339 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5340 b1
= gen_portatom6(2, (bpf_int32
)port
);
5352 static struct block
*
5353 gen_port6(port
, ip_proto
, dir
)
5358 struct block
*b0
, *b1
, *tmp
;
5360 /* link proto ip6 */
5361 b0
= gen_linktype(ETHERTYPE_IPV6
);
5367 b1
= gen_portop6(port
, ip_proto
, dir
);
5371 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5372 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5374 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5386 /* gen_portrange code */
5387 static struct block
*
5388 gen_portrangeatom(off
, v1
, v2
)
5392 struct block
*b1
, *b2
;
5396 * Reverse the order of the ports, so v1 is the lower one.
5405 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5406 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5414 gen_portrangeop(port1
, port2
, proto
, dir
)
5419 struct block
*b0
, *b1
, *tmp
;
5421 /* ip proto 'proto' and not a fragment other than the first fragment */
5422 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5428 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5432 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5437 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5438 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5443 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5444 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5456 static struct block
*
5457 gen_portrange(port1
, port2
, ip_proto
, dir
)
5462 struct block
*b0
, *b1
, *tmp
;
5465 b0
= gen_linktype(ETHERTYPE_IP
);
5471 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5475 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5476 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5478 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5490 static struct block
*
5491 gen_portrangeatom6(off
, v1
, v2
)
5495 struct block
*b1
, *b2
;
5499 * Reverse the order of the ports, so v1 is the lower one.
5508 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5509 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5517 gen_portrangeop6(port1
, port2
, proto
, dir
)
5522 struct block
*b0
, *b1
, *tmp
;
5524 /* ip6 proto 'proto' */
5525 /* XXX - catch the first fragment of a fragmented packet? */
5526 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5530 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5534 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5539 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5540 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5545 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5546 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5558 static struct block
*
5559 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5564 struct block
*b0
, *b1
, *tmp
;
5566 /* link proto ip6 */
5567 b0
= gen_linktype(ETHERTYPE_IPV6
);
5573 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5577 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5578 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5580 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5593 lookup_proto(name
, proto
)
5594 register const char *name
;
5604 v
= pcap_nametoproto(name
);
5605 if (v
== PROTO_UNDEF
)
5606 bpf_error("unknown ip proto '%s'", name
);
5610 /* XXX should look up h/w protocol type based on linktype */
5611 v
= pcap_nametoeproto(name
);
5612 if (v
== PROTO_UNDEF
) {
5613 v
= pcap_nametollc(name
);
5614 if (v
== PROTO_UNDEF
)
5615 bpf_error("unknown ether proto '%s'", name
);
5620 if (strcmp(name
, "esis") == 0)
5622 else if (strcmp(name
, "isis") == 0)
5624 else if (strcmp(name
, "clnp") == 0)
5627 bpf_error("unknown osi proto '%s'", name
);
5647 static struct block
*
5648 gen_protochain(v
, proto
, dir
)
5653 #ifdef NO_PROTOCHAIN
5654 return gen_proto(v
, proto
, dir
);
5656 struct block
*b0
, *b
;
5657 struct slist
*s
[100];
5658 int fix2
, fix3
, fix4
, fix5
;
5659 int ahcheck
, again
, end
;
5661 int reg2
= alloc_reg();
5663 memset(s
, 0, sizeof(s
));
5664 fix2
= fix3
= fix4
= fix5
= 0;
5671 b0
= gen_protochain(v
, Q_IP
, dir
);
5672 b
= gen_protochain(v
, Q_IPV6
, dir
);
5676 bpf_error("bad protocol applied for 'protochain'");
5681 * We don't handle variable-length prefixes before the link-layer
5682 * header, or variable-length link-layer headers, here yet.
5683 * We might want to add BPF instructions to do the protochain
5684 * work, to simplify that and, on platforms that have a BPF
5685 * interpreter with the new instructions, let the filtering
5686 * be done in the kernel. (We already require a modified BPF
5687 * engine to do the protochain stuff, to support backward
5688 * branches, and backward branch support is unlikely to appear
5689 * in kernel BPF engines.)
5693 case DLT_IEEE802_11
:
5694 case DLT_PRISM_HEADER
:
5695 case DLT_IEEE802_11_RADIO_AVS
:
5696 case DLT_IEEE802_11_RADIO
:
5698 bpf_error("'protochain' not supported with 802.11");
5701 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5704 * s[0] is a dummy entry to protect other BPF insn from damage
5705 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5706 * hard to find interdependency made by jump table fixup.
5709 s
[i
] = new_stmt(0); /*dummy*/
5714 b0
= gen_linktype(ETHERTYPE_IP
);
5717 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5718 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5720 /* X = ip->ip_hl << 2 */
5721 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5722 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5727 b0
= gen_linktype(ETHERTYPE_IPV6
);
5729 /* A = ip6->ip_nxt */
5730 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5731 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5733 /* X = sizeof(struct ip6_hdr) */
5734 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5740 bpf_error("unsupported proto to gen_protochain");
5744 /* again: if (A == v) goto end; else fall through; */
5746 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5748 s
[i
]->s
.jt
= NULL
; /*later*/
5749 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5753 #ifndef IPPROTO_NONE
5754 #define IPPROTO_NONE 59
5756 /* if (A == IPPROTO_NONE) goto end */
5757 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5758 s
[i
]->s
.jt
= NULL
; /*later*/
5759 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5760 s
[i
]->s
.k
= IPPROTO_NONE
;
5761 s
[fix5
]->s
.jf
= s
[i
];
5766 if (proto
== Q_IPV6
) {
5767 int v6start
, v6end
, v6advance
, j
;
5770 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5771 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5772 s
[i
]->s
.jt
= NULL
; /*later*/
5773 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5774 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5775 s
[fix2
]->s
.jf
= s
[i
];
5777 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5778 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5779 s
[i
]->s
.jt
= NULL
; /*later*/
5780 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5781 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5783 /* if (A == IPPROTO_ROUTING) goto v6advance */
5784 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5785 s
[i
]->s
.jt
= NULL
; /*later*/
5786 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5787 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5789 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5790 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5791 s
[i
]->s
.jt
= NULL
; /*later*/
5792 s
[i
]->s
.jf
= NULL
; /*later*/
5793 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5804 * X = X + (P[X + 1] + 1) * 8;
5807 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5809 /* A = P[X + packet head] */
5810 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5811 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5814 s
[i
] = new_stmt(BPF_ST
);
5818 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5821 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5825 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5827 /* A = P[X + packet head]; */
5828 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5829 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5832 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5836 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5840 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5843 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5847 /* goto again; (must use BPF_JA for backward jump) */
5848 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5849 s
[i
]->s
.k
= again
- i
- 1;
5850 s
[i
- 1]->s
.jf
= s
[i
];
5854 for (j
= v6start
; j
<= v6end
; j
++)
5855 s
[j
]->s
.jt
= s
[v6advance
];
5860 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5862 s
[fix2
]->s
.jf
= s
[i
];
5868 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5869 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5870 s
[i
]->s
.jt
= NULL
; /*later*/
5871 s
[i
]->s
.jf
= NULL
; /*later*/
5872 s
[i
]->s
.k
= IPPROTO_AH
;
5874 s
[fix3
]->s
.jf
= s
[ahcheck
];
5881 * X = X + (P[X + 1] + 2) * 4;
5884 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5886 /* A = P[X + packet head]; */
5887 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5888 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5891 s
[i
] = new_stmt(BPF_ST
);
5895 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5898 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5902 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5904 /* A = P[X + packet head] */
5905 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5906 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5909 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5913 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5917 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5920 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5924 /* goto again; (must use BPF_JA for backward jump) */
5925 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5926 s
[i
]->s
.k
= again
- i
- 1;
5931 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5933 s
[fix2
]->s
.jt
= s
[end
];
5934 s
[fix4
]->s
.jf
= s
[end
];
5935 s
[fix5
]->s
.jt
= s
[end
];
5942 for (i
= 0; i
< max
- 1; i
++)
5943 s
[i
]->next
= s
[i
+ 1];
5944 s
[max
- 1]->next
= NULL
;
5949 b
= new_block(JMP(BPF_JEQ
));
5950 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5960 static struct block
*
5961 gen_check_802_11_data_frame()
5964 struct block
*b0
, *b1
;
5967 * A data frame has the 0x08 bit (b3) in the frame control field set
5968 * and the 0x04 bit (b2) clear.
5970 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5971 b0
= new_block(JMP(BPF_JSET
));
5975 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5976 b1
= new_block(JMP(BPF_JSET
));
5987 * Generate code that checks whether the packet is a packet for protocol
5988 * <proto> and whether the type field in that protocol's header has
5989 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5990 * IP packet and checks the protocol number in the IP header against <v>.
5992 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5993 * against Q_IP and Q_IPV6.
5995 static struct block
*
5996 gen_proto(v
, proto
, dir
)
6001 struct block
*b0
, *b1
;
6003 if (dir
!= Q_DEFAULT
)
6004 bpf_error("direction applied to 'proto'");
6009 b0
= gen_proto(v
, Q_IP
, dir
);
6010 b1
= gen_proto(v
, Q_IPV6
, dir
);
6018 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6019 * not LLC encapsulation with LLCSAP_IP.
6021 * For IEEE 802 networks - which includes 802.5 token ring
6022 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6023 * says that SNAP encapsulation is used, not LLC encapsulation
6026 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6027 * RFC 2225 say that SNAP encapsulation is used, not LLC
6028 * encapsulation with LLCSAP_IP.
6030 * So we always check for ETHERTYPE_IP.
6032 b0
= gen_linktype(ETHERTYPE_IP
);
6034 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
6036 b1
= gen_protochain(v
, Q_IP
);
6046 * Frame Relay packets typically have an OSI
6047 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6048 * generates code to check for all the OSI
6049 * NLPIDs, so calling it and then adding a check
6050 * for the particular NLPID for which we're
6051 * looking is bogus, as we can just check for
6054 * What we check for is the NLPID and a frame
6055 * control field value of UI, i.e. 0x03 followed
6058 * XXX - assumes a 2-byte Frame Relay header with
6059 * DLCI and flags. What if the address is longer?
6061 * XXX - what about SNAP-encapsulated frames?
6063 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
6069 * Cisco uses an Ethertype lookalike - for OSI,
6072 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6073 /* OSI in C-HDLC is stuffed with a fudge byte */
6074 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
6079 b0
= gen_linktype(LLCSAP_ISONS
);
6080 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
6086 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6088 * 4 is the offset of the PDU type relative to the IS-IS
6091 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
6096 bpf_error("arp does not encapsulate another protocol");
6100 bpf_error("rarp does not encapsulate another protocol");
6104 bpf_error("atalk encapsulation is not specifiable");
6108 bpf_error("decnet encapsulation is not specifiable");
6112 bpf_error("sca does not encapsulate another protocol");
6116 bpf_error("lat does not encapsulate another protocol");
6120 bpf_error("moprc does not encapsulate another protocol");
6124 bpf_error("mopdl does not encapsulate another protocol");
6128 return gen_linktype(v
);
6131 bpf_error("'udp proto' is bogus");
6135 bpf_error("'tcp proto' is bogus");
6139 bpf_error("'sctp proto' is bogus");
6143 bpf_error("'icmp proto' is bogus");
6147 bpf_error("'igmp proto' is bogus");
6151 bpf_error("'igrp proto' is bogus");
6155 bpf_error("'pim proto' is bogus");
6159 bpf_error("'vrrp proto' is bogus");
6164 b0
= gen_linktype(ETHERTYPE_IPV6
);
6166 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6168 b1
= gen_protochain(v
, Q_IPV6
);
6174 bpf_error("'icmp6 proto' is bogus");
6178 bpf_error("'ah proto' is bogus");
6181 bpf_error("'ah proto' is bogus");
6184 bpf_error("'stp proto' is bogus");
6187 bpf_error("'ipx proto' is bogus");
6190 bpf_error("'netbeui proto' is bogus");
6193 bpf_error("'radio proto' is bogus");
6204 register const char *name
;
6207 int proto
= q
.proto
;
6211 bpf_u_int32 mask
, addr
;
6213 bpf_u_int32
**alist
;
6216 struct sockaddr_in
*sin4
;
6217 struct sockaddr_in6
*sin6
;
6218 struct addrinfo
*res
, *res0
;
6219 struct in6_addr mask128
;
6221 struct block
*b
, *tmp
;
6222 int port
, real_proto
;
6228 addr
= pcap_nametonetaddr(name
);
6230 bpf_error("unknown network '%s'", name
);
6231 /* Left justify network addr and calculate its network mask */
6233 while (addr
&& (addr
& 0xff000000) == 0) {
6237 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6241 if (proto
== Q_LINK
) {
6245 case DLT_NETANALYZER
:
6246 case DLT_NETANALYZER_TRANSPARENT
:
6247 eaddr
= pcap_ether_hostton(name
);
6250 "unknown ether host '%s'", name
);
6251 b
= gen_ehostop(eaddr
, dir
);
6256 eaddr
= pcap_ether_hostton(name
);
6259 "unknown FDDI host '%s'", name
);
6260 b
= gen_fhostop(eaddr
, dir
);
6265 eaddr
= pcap_ether_hostton(name
);
6268 "unknown token ring host '%s'", name
);
6269 b
= gen_thostop(eaddr
, dir
);
6273 case DLT_IEEE802_11
:
6274 case DLT_PRISM_HEADER
:
6275 case DLT_IEEE802_11_RADIO_AVS
:
6276 case DLT_IEEE802_11_RADIO
:
6278 eaddr
= pcap_ether_hostton(name
);
6281 "unknown 802.11 host '%s'", name
);
6282 b
= gen_wlanhostop(eaddr
, dir
);
6286 case DLT_IP_OVER_FC
:
6287 eaddr
= pcap_ether_hostton(name
);
6290 "unknown Fibre Channel host '%s'", name
);
6291 b
= gen_ipfchostop(eaddr
, dir
);
6300 * Check that the packet doesn't begin
6301 * with an LE Control marker. (We've
6302 * already generated a test for LANE.)
6304 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6308 eaddr
= pcap_ether_hostton(name
);
6311 "unknown ether host '%s'", name
);
6312 b
= gen_ehostop(eaddr
, dir
);
6318 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6319 } else if (proto
== Q_DECNET
) {
6320 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6322 * I don't think DECNET hosts can be multihomed, so
6323 * there is no need to build up a list of addresses
6325 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6328 alist
= pcap_nametoaddr(name
);
6329 if (alist
== NULL
|| *alist
== NULL
)
6330 bpf_error("unknown host '%s'", name
);
6332 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6334 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6336 tmp
= gen_host(**alist
++, 0xffffffff,
6337 tproto
, dir
, q
.addr
);
6343 memset(&mask128
, 0xff, sizeof(mask128
));
6344 res0
= res
= pcap_nametoaddrinfo(name
);
6346 bpf_error("unknown host '%s'", name
);
6349 tproto
= tproto6
= proto
;
6350 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6354 for (res
= res0
; res
; res
= res
->ai_next
) {
6355 switch (res
->ai_family
) {
6357 if (tproto
== Q_IPV6
)
6360 sin4
= (struct sockaddr_in
*)
6362 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6363 0xffffffff, tproto
, dir
, q
.addr
);
6366 if (tproto6
== Q_IP
)
6369 sin6
= (struct sockaddr_in6
*)
6371 tmp
= gen_host6(&sin6
->sin6_addr
,
6372 &mask128
, tproto6
, dir
, q
.addr
);
6384 bpf_error("unknown host '%s'%s", name
,
6385 (proto
== Q_DEFAULT
)
6387 : " for specified address family");
6394 if (proto
!= Q_DEFAULT
&&
6395 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6396 bpf_error("illegal qualifier of 'port'");
6397 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6398 bpf_error("unknown port '%s'", name
);
6399 if (proto
== Q_UDP
) {
6400 if (real_proto
== IPPROTO_TCP
)
6401 bpf_error("port '%s' is tcp", name
);
6402 else if (real_proto
== IPPROTO_SCTP
)
6403 bpf_error("port '%s' is sctp", name
);
6405 /* override PROTO_UNDEF */
6406 real_proto
= IPPROTO_UDP
;
6408 if (proto
== Q_TCP
) {
6409 if (real_proto
== IPPROTO_UDP
)
6410 bpf_error("port '%s' is udp", name
);
6412 else if (real_proto
== IPPROTO_SCTP
)
6413 bpf_error("port '%s' is sctp", name
);
6415 /* override PROTO_UNDEF */
6416 real_proto
= IPPROTO_TCP
;
6418 if (proto
== Q_SCTP
) {
6419 if (real_proto
== IPPROTO_UDP
)
6420 bpf_error("port '%s' is udp", name
);
6422 else if (real_proto
== IPPROTO_TCP
)
6423 bpf_error("port '%s' is tcp", name
);
6425 /* override PROTO_UNDEF */
6426 real_proto
= IPPROTO_SCTP
;
6429 bpf_error("illegal port number %d < 0", port
);
6431 bpf_error("illegal port number %d > 65535", port
);
6433 return gen_port(port
, real_proto
, dir
);
6435 b
= gen_port(port
, real_proto
, dir
);
6436 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6441 if (proto
!= Q_DEFAULT
&&
6442 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6443 bpf_error("illegal qualifier of 'portrange'");
6444 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6445 bpf_error("unknown port in range '%s'", name
);
6446 if (proto
== Q_UDP
) {
6447 if (real_proto
== IPPROTO_TCP
)
6448 bpf_error("port in range '%s' is tcp", name
);
6449 else if (real_proto
== IPPROTO_SCTP
)
6450 bpf_error("port in range '%s' is sctp", name
);
6452 /* override PROTO_UNDEF */
6453 real_proto
= IPPROTO_UDP
;
6455 if (proto
== Q_TCP
) {
6456 if (real_proto
== IPPROTO_UDP
)
6457 bpf_error("port in range '%s' is udp", name
);
6458 else if (real_proto
== IPPROTO_SCTP
)
6459 bpf_error("port in range '%s' is sctp", name
);
6461 /* override PROTO_UNDEF */
6462 real_proto
= IPPROTO_TCP
;
6464 if (proto
== Q_SCTP
) {
6465 if (real_proto
== IPPROTO_UDP
)
6466 bpf_error("port in range '%s' is udp", name
);
6467 else if (real_proto
== IPPROTO_TCP
)
6468 bpf_error("port in range '%s' is tcp", name
);
6470 /* override PROTO_UNDEF */
6471 real_proto
= IPPROTO_SCTP
;
6474 bpf_error("illegal port number %d < 0", port1
);
6476 bpf_error("illegal port number %d > 65535", port1
);
6478 bpf_error("illegal port number %d < 0", port2
);
6480 bpf_error("illegal port number %d > 65535", port2
);
6483 return gen_portrange(port1
, port2
, real_proto
, dir
);
6485 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6486 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6492 eaddr
= pcap_ether_hostton(name
);
6494 bpf_error("unknown ether host: %s", name
);
6496 alist
= pcap_nametoaddr(name
);
6497 if (alist
== NULL
|| *alist
== NULL
)
6498 bpf_error("unknown host '%s'", name
);
6499 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6503 bpf_error("'gateway' not supported in this configuration");
6507 real_proto
= lookup_proto(name
, proto
);
6508 if (real_proto
>= 0)
6509 return gen_proto(real_proto
, proto
, dir
);
6511 bpf_error("unknown protocol: %s", name
);
6514 real_proto
= lookup_proto(name
, proto
);
6515 if (real_proto
>= 0)
6516 return gen_protochain(real_proto
, proto
, dir
);
6518 bpf_error("unknown protocol: %s", name
);
6529 gen_mcode(s1
, s2
, masklen
, q
)
6530 register const char *s1
, *s2
;
6531 register int masklen
;
6534 register int nlen
, mlen
;
6537 nlen
= __pcap_atoin(s1
, &n
);
6538 /* Promote short ipaddr */
6542 mlen
= __pcap_atoin(s2
, &m
);
6543 /* Promote short ipaddr */
6546 bpf_error("non-network bits set in \"%s mask %s\"",
6549 /* Convert mask len to mask */
6551 bpf_error("mask length must be <= 32");
6554 * X << 32 is not guaranteed by C to be 0; it's
6559 m
= 0xffffffff << (32 - masklen
);
6561 bpf_error("non-network bits set in \"%s/%d\"",
6568 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6571 bpf_error("Mask syntax for networks only");
6580 register const char *s
;
6585 int proto
= q
.proto
;
6591 else if (q
.proto
== Q_DECNET
)
6592 vlen
= __pcap_atodn(s
, &v
);
6594 vlen
= __pcap_atoin(s
, &v
);
6601 if (proto
== Q_DECNET
)
6602 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6603 else if (proto
== Q_LINK
) {
6604 bpf_error("illegal link layer address");
6607 if (s
== NULL
&& q
.addr
== Q_NET
) {
6608 /* Promote short net number */
6609 while (v
&& (v
& 0xff000000) == 0) {
6614 /* Promote short ipaddr */
6618 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6623 proto
= IPPROTO_UDP
;
6624 else if (proto
== Q_TCP
)
6625 proto
= IPPROTO_TCP
;
6626 else if (proto
== Q_SCTP
)
6627 proto
= IPPROTO_SCTP
;
6628 else if (proto
== Q_DEFAULT
)
6629 proto
= PROTO_UNDEF
;
6631 bpf_error("illegal qualifier of 'port'");
6634 bpf_error("illegal port number %u > 65535", v
);
6637 return gen_port((int)v
, proto
, dir
);
6641 b
= gen_port((int)v
, proto
, dir
);
6642 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6649 proto
= IPPROTO_UDP
;
6650 else if (proto
== Q_TCP
)
6651 proto
= IPPROTO_TCP
;
6652 else if (proto
== Q_SCTP
)
6653 proto
= IPPROTO_SCTP
;
6654 else if (proto
== Q_DEFAULT
)
6655 proto
= PROTO_UNDEF
;
6657 bpf_error("illegal qualifier of 'portrange'");
6660 bpf_error("illegal port number %u > 65535", v
);
6663 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6667 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6668 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6674 bpf_error("'gateway' requires a name");
6678 return gen_proto((int)v
, proto
, dir
);
6681 return gen_protochain((int)v
, proto
, dir
);
6696 gen_mcode6(s1
, s2
, masklen
, q
)
6697 register const char *s1
, *s2
;
6698 register int masklen
;
6701 struct addrinfo
*res
;
6702 struct in6_addr
*addr
;
6703 struct in6_addr mask
;
6708 bpf_error("no mask %s supported", s2
);
6710 res
= pcap_nametoaddrinfo(s1
);
6712 bpf_error("invalid ip6 address %s", s1
);
6715 bpf_error("%s resolved to multiple address", s1
);
6716 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6718 if (sizeof(mask
) * 8 < masklen
)
6719 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6720 memset(&mask
, 0, sizeof(mask
));
6721 memset(&mask
, 0xff, masklen
/ 8);
6723 mask
.s6_addr
[masklen
/ 8] =
6724 (0xff << (8 - masklen
% 8)) & 0xff;
6727 a
= (u_int32_t
*)addr
;
6728 m
= (u_int32_t
*)&mask
;
6729 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6730 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6731 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6739 bpf_error("Mask syntax for networks only");
6743 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6749 bpf_error("invalid qualifier against IPv6 address");
6758 register const u_char
*eaddr
;
6761 struct block
*b
, *tmp
;
6763 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6766 case DLT_NETANALYZER
:
6767 case DLT_NETANALYZER_TRANSPARENT
:
6768 return gen_ehostop(eaddr
, (int)q
.dir
);
6770 return gen_fhostop(eaddr
, (int)q
.dir
);
6772 return gen_thostop(eaddr
, (int)q
.dir
);
6773 case DLT_IEEE802_11
:
6774 case DLT_PRISM_HEADER
:
6775 case DLT_IEEE802_11_RADIO_AVS
:
6776 case DLT_IEEE802_11_RADIO
:
6778 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6782 * Check that the packet doesn't begin with an
6783 * LE Control marker. (We've already generated
6786 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6791 * Now check the MAC address.
6793 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6798 case DLT_IP_OVER_FC
:
6799 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6801 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6805 bpf_error("ethernet address used in non-ether expression");
6812 struct slist
*s0
, *s1
;
6815 * This is definitely not the best way to do this, but the
6816 * lists will rarely get long.
6823 static struct slist
*
6829 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6834 static struct slist
*
6840 s
= new_stmt(BPF_LD
|BPF_MEM
);
6846 * Modify "index" to use the value stored into its register as an
6847 * offset relative to the beginning of the header for the protocol
6848 * "proto", and allocate a register and put an item "size" bytes long
6849 * (1, 2, or 4) at that offset into that register, making it the register
6853 gen_load(proto
, inst
, size
)
6858 struct slist
*s
, *tmp
;
6860 int regno
= alloc_reg();
6862 free_reg(inst
->regno
);
6866 bpf_error("data size must be 1, 2, or 4");
6882 bpf_error("unsupported index operation");
6886 * The offset is relative to the beginning of the packet
6887 * data, if we have a radio header. (If we don't, this
6890 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6891 linktype
!= DLT_IEEE802_11_RADIO
&&
6892 linktype
!= DLT_PRISM_HEADER
)
6893 bpf_error("radio information not present in capture");
6896 * Load into the X register the offset computed into the
6897 * register specified by "index".
6899 s
= xfer_to_x(inst
);
6902 * Load the item at that offset.
6904 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6906 sappend(inst
->s
, s
);
6911 * The offset is relative to the beginning of
6912 * the link-layer header.
6914 * XXX - what about ATM LANE? Should the index be
6915 * relative to the beginning of the AAL5 frame, so
6916 * that 0 refers to the beginning of the LE Control
6917 * field, or relative to the beginning of the LAN
6918 * frame, so that 0 refers, for Ethernet LANE, to
6919 * the beginning of the destination address?
6921 s
= gen_llprefixlen();
6924 * If "s" is non-null, it has code to arrange that the
6925 * X register contains the length of the prefix preceding
6926 * the link-layer header. Add to it the offset computed
6927 * into the register specified by "index", and move that
6928 * into the X register. Otherwise, just load into the X
6929 * register the offset computed into the register specified
6933 sappend(s
, xfer_to_a(inst
));
6934 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6935 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6937 s
= xfer_to_x(inst
);
6940 * Load the item at the sum of the offset we've put in the
6941 * X register and the offset of the start of the link
6942 * layer header (which is 0 if the radio header is
6943 * variable-length; that header length is what we put
6944 * into the X register and then added to the index).
6946 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6949 sappend(inst
->s
, s
);
6965 * The offset is relative to the beginning of
6966 * the network-layer header.
6967 * XXX - are there any cases where we want
6970 s
= gen_off_macpl();
6973 * If "s" is non-null, it has code to arrange that the
6974 * X register contains the offset of the MAC-layer
6975 * payload. Add to it the offset computed into the
6976 * register specified by "index", and move that into
6977 * the X register. Otherwise, just load into the X
6978 * register the offset computed into the register specified
6982 sappend(s
, xfer_to_a(inst
));
6983 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6984 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6986 s
= xfer_to_x(inst
);
6989 * Load the item at the sum of the offset we've put in the
6990 * X register, the offset of the start of the network
6991 * layer header from the beginning of the MAC-layer
6992 * payload, and the purported offset of the start of the
6993 * MAC-layer payload (which might be 0 if there's a
6994 * variable-length prefix before the link-layer header
6995 * or the link-layer header itself is variable-length;
6996 * the variable-length offset of the start of the
6997 * MAC-layer payload is what we put into the X register
6998 * and then added to the index).
7000 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
7001 tmp
->s
.k
= off_macpl
+ off_nl
;
7003 sappend(inst
->s
, s
);
7006 * Do the computation only if the packet contains
7007 * the protocol in question.
7009 b
= gen_proto_abbrev(proto
);
7011 gen_and(inst
->b
, b
);
7024 * The offset is relative to the beginning of
7025 * the transport-layer header.
7027 * Load the X register with the length of the IPv4 header
7028 * (plus the offset of the link-layer header, if it's
7029 * a variable-length header), in bytes.
7031 * XXX - are there any cases where we want
7033 * XXX - we should, if we're built with
7034 * IPv6 support, generate code to load either
7035 * IPv4, IPv6, or both, as appropriate.
7037 s
= gen_loadx_iphdrlen();
7040 * The X register now contains the sum of the length
7041 * of any variable-length header preceding the link-layer
7042 * header, any variable-length link-layer header, and the
7043 * length of the network-layer header.
7045 * Load into the A register the offset relative to
7046 * the beginning of the transport layer header,
7047 * add the X register to that, move that to the
7048 * X register, and load with an offset from the
7049 * X register equal to the offset of the network
7050 * layer header relative to the beginning of
7051 * the MAC-layer payload plus the fixed-length
7052 * portion of the offset of the MAC-layer payload
7053 * from the beginning of the raw packet data.
7055 sappend(s
, xfer_to_a(inst
));
7056 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
7057 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
7058 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
7059 tmp
->s
.k
= off_macpl
+ off_nl
;
7060 sappend(inst
->s
, s
);
7063 * Do the computation only if the packet contains
7064 * the protocol in question - which is true only
7065 * if this is an IP datagram and is the first or
7066 * only fragment of that datagram.
7068 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
7070 gen_and(inst
->b
, b
);
7072 gen_and(gen_proto_abbrev(Q_IP
), b
);
7078 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7082 inst
->regno
= regno
;
7083 s
= new_stmt(BPF_ST
);
7085 sappend(inst
->s
, s
);
7091 gen_relation(code
, a0
, a1
, reversed
)
7093 struct arth
*a0
, *a1
;
7096 struct slist
*s0
, *s1
, *s2
;
7097 struct block
*b
, *tmp
;
7101 if (code
== BPF_JEQ
) {
7102 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7103 b
= new_block(JMP(code
));
7107 b
= new_block(BPF_JMP
|code
|BPF_X
);
7113 sappend(a0
->s
, a1
->s
);
7117 free_reg(a0
->regno
);
7118 free_reg(a1
->regno
);
7120 /* 'and' together protocol checks */
7123 gen_and(a0
->b
, tmp
= a1
->b
);
7139 int regno
= alloc_reg();
7140 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7143 s
= new_stmt(BPF_LD
|BPF_LEN
);
7144 s
->next
= new_stmt(BPF_ST
);
7145 s
->next
->s
.k
= regno
;
7160 a
= (struct arth
*)newchunk(sizeof(*a
));
7164 s
= new_stmt(BPF_LD
|BPF_IMM
);
7166 s
->next
= new_stmt(BPF_ST
);
7182 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7185 s
= new_stmt(BPF_ST
);
7193 gen_arth(code
, a0
, a1
)
7195 struct arth
*a0
, *a1
;
7197 struct slist
*s0
, *s1
, *s2
;
7201 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7206 sappend(a0
->s
, a1
->s
);
7208 free_reg(a0
->regno
);
7209 free_reg(a1
->regno
);
7211 s0
= new_stmt(BPF_ST
);
7212 a0
->regno
= s0
->s
.k
= alloc_reg();
7219 * Here we handle simple allocation of the scratch registers.
7220 * If too many registers are alloc'd, the allocator punts.
7222 static int regused
[BPF_MEMWORDS
];
7226 * Initialize the table of used registers and the current register.
7232 memset(regused
, 0, sizeof regused
);
7236 * Return the next free register.
7241 int n
= BPF_MEMWORDS
;
7244 if (regused
[curreg
])
7245 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7247 regused
[curreg
] = 1;
7251 bpf_error("too many registers needed to evaluate expression");
7257 * Return a register to the table so it can
7267 static struct block
*
7274 s
= new_stmt(BPF_LD
|BPF_LEN
);
7275 b
= new_block(JMP(jmp
));
7286 return gen_len(BPF_JGE
, n
);
7290 * Actually, this is less than or equal.
7298 b
= gen_len(BPF_JGT
, n
);
7305 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7306 * the beginning of the link-layer header.
7307 * XXX - that means you can't test values in the radiotap header, but
7308 * as that header is difficult if not impossible to parse generally
7309 * without a loop, that might not be a severe problem. A new keyword
7310 * "radio" could be added for that, although what you'd really want
7311 * would be a way of testing particular radio header values, which
7312 * would generate code appropriate to the radio header in question.
7315 gen_byteop(op
, idx
, val
)
7326 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7329 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7333 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7337 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7341 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7345 b
= new_block(JMP(BPF_JEQ
));
7352 static u_char abroadcast
[] = { 0x0 };
7355 gen_broadcast(proto
)
7358 bpf_u_int32 hostmask
;
7359 struct block
*b0
, *b1
, *b2
;
7360 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7368 case DLT_ARCNET_LINUX
:
7369 return gen_ahostop(abroadcast
, Q_DST
);
7371 case DLT_NETANALYZER
:
7372 case DLT_NETANALYZER_TRANSPARENT
:
7373 return gen_ehostop(ebroadcast
, Q_DST
);
7375 return gen_fhostop(ebroadcast
, Q_DST
);
7377 return gen_thostop(ebroadcast
, Q_DST
);
7378 case DLT_IEEE802_11
:
7379 case DLT_PRISM_HEADER
:
7380 case DLT_IEEE802_11_RADIO_AVS
:
7381 case DLT_IEEE802_11_RADIO
:
7383 return gen_wlanhostop(ebroadcast
, Q_DST
);
7384 case DLT_IP_OVER_FC
:
7385 return gen_ipfchostop(ebroadcast
, Q_DST
);
7389 * Check that the packet doesn't begin with an
7390 * LE Control marker. (We've already generated
7393 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7398 * Now check the MAC address.
7400 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7406 bpf_error("not a broadcast link");
7412 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7413 * as an indication that we don't know the netmask, and fail
7416 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7417 bpf_error("netmask not known, so 'ip broadcast' not supported");
7418 b0
= gen_linktype(ETHERTYPE_IP
);
7419 hostmask
= ~netmask
;
7420 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7421 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7422 (bpf_int32
)(~0 & hostmask
), hostmask
);
7427 bpf_error("only link-layer/IP broadcast filters supported");
7433 * Generate code to test the low-order bit of a MAC address (that's
7434 * the bottom bit of the *first* byte).
7436 static struct block
*
7437 gen_mac_multicast(offset
)
7440 register struct block
*b0
;
7441 register struct slist
*s
;
7443 /* link[offset] & 1 != 0 */
7444 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7445 b0
= new_block(JMP(BPF_JSET
));
7452 gen_multicast(proto
)
7455 register struct block
*b0
, *b1
, *b2
;
7456 register struct slist
*s
;
7464 case DLT_ARCNET_LINUX
:
7465 /* all ARCnet multicasts use the same address */
7466 return gen_ahostop(abroadcast
, Q_DST
);
7468 case DLT_NETANALYZER
:
7469 case DLT_NETANALYZER_TRANSPARENT
:
7470 /* ether[0] & 1 != 0 */
7471 return gen_mac_multicast(0);
7474 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7476 * XXX - was that referring to bit-order issues?
7478 /* fddi[1] & 1 != 0 */
7479 return gen_mac_multicast(1);
7481 /* tr[2] & 1 != 0 */
7482 return gen_mac_multicast(2);
7483 case DLT_IEEE802_11
:
7484 case DLT_PRISM_HEADER
:
7485 case DLT_IEEE802_11_RADIO_AVS
:
7486 case DLT_IEEE802_11_RADIO
:
7491 * For control frames, there is no DA.
7493 * For management frames, DA is at an
7494 * offset of 4 from the beginning of
7497 * For data frames, DA is at an offset
7498 * of 4 from the beginning of the packet
7499 * if To DS is clear and at an offset of
7500 * 16 from the beginning of the packet
7505 * Generate the tests to be done for data frames.
7507 * First, check for To DS set, i.e. "link[1] & 0x01".
7509 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7510 b1
= new_block(JMP(BPF_JSET
));
7511 b1
->s
.k
= 0x01; /* To DS */
7515 * If To DS is set, the DA is at 16.
7517 b0
= gen_mac_multicast(16);
7521 * Now, check for To DS not set, i.e. check
7522 * "!(link[1] & 0x01)".
7524 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7525 b2
= new_block(JMP(BPF_JSET
));
7526 b2
->s
.k
= 0x01; /* To DS */
7531 * If To DS is not set, the DA is at 4.
7533 b1
= gen_mac_multicast(4);
7537 * Now OR together the last two checks. That gives
7538 * the complete set of checks for data frames.
7543 * Now check for a data frame.
7544 * I.e, check "link[0] & 0x08".
7546 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7547 b1
= new_block(JMP(BPF_JSET
));
7552 * AND that with the checks done for data frames.
7557 * If the high-order bit of the type value is 0, this
7558 * is a management frame.
7559 * I.e, check "!(link[0] & 0x08)".
7561 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7562 b2
= new_block(JMP(BPF_JSET
));
7568 * For management frames, the DA is at 4.
7570 b1
= gen_mac_multicast(4);
7574 * OR that with the checks done for data frames.
7575 * That gives the checks done for management and
7581 * If the low-order bit of the type value is 1,
7582 * this is either a control frame or a frame
7583 * with a reserved type, and thus not a
7586 * I.e., check "!(link[0] & 0x04)".
7588 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7589 b1
= new_block(JMP(BPF_JSET
));
7595 * AND that with the checks for data and management
7600 case DLT_IP_OVER_FC
:
7601 b0
= gen_mac_multicast(2);
7606 * Check that the packet doesn't begin with an
7607 * LE Control marker. (We've already generated
7610 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7614 /* ether[off_mac] & 1 != 0 */
7615 b0
= gen_mac_multicast(off_mac
);
7623 /* Link not known to support multicasts */
7627 b0
= gen_linktype(ETHERTYPE_IP
);
7628 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7634 b0
= gen_linktype(ETHERTYPE_IPV6
);
7635 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7640 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7646 * generate command for inbound/outbound. It's here so we can
7647 * make it link-type specific. 'dir' = 0 implies "inbound",
7648 * = 1 implies "outbound".
7654 register struct block
*b0
;
7657 * Only some data link types support inbound/outbound qualifiers.
7661 b0
= gen_relation(BPF_JEQ
,
7662 gen_load(Q_LINK
, gen_loadi(0), 1),
7669 /* match outgoing packets */
7670 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7672 /* match incoming packets */
7673 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7680 * Match packets sent by this machine.
7682 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7685 * Match packets sent to this machine.
7686 * (No broadcast or multicast packets, or
7687 * packets sent to some other machine and
7688 * received promiscuously.)
7690 * XXX - packets sent to other machines probably
7691 * shouldn't be matched, but what about broadcast
7692 * or multicast packets we received?
7694 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7698 #ifdef HAVE_NET_PFVAR_H
7700 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7701 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7707 /* match outgoing packets */
7708 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7710 /* match incoming packets */
7711 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7715 case DLT_JUNIPER_MFR
:
7716 case DLT_JUNIPER_MLFR
:
7717 case DLT_JUNIPER_MLPPP
:
7718 case DLT_JUNIPER_ATM1
:
7719 case DLT_JUNIPER_ATM2
:
7720 case DLT_JUNIPER_PPPOE
:
7721 case DLT_JUNIPER_PPPOE_ATM
:
7722 case DLT_JUNIPER_GGSN
:
7723 case DLT_JUNIPER_ES
:
7724 case DLT_JUNIPER_MONITOR
:
7725 case DLT_JUNIPER_SERVICES
:
7726 case DLT_JUNIPER_ETHER
:
7727 case DLT_JUNIPER_PPP
:
7728 case DLT_JUNIPER_FRELAY
:
7729 case DLT_JUNIPER_CHDLC
:
7730 case DLT_JUNIPER_VP
:
7731 case DLT_JUNIPER_ST
:
7732 case DLT_JUNIPER_ISM
:
7733 case DLT_JUNIPER_VS
:
7734 case DLT_JUNIPER_SRX_E2E
:
7735 case DLT_JUNIPER_FIBRECHANNEL
:
7736 case DLT_JUNIPER_ATM_CEMIC
:
7738 /* juniper flags (including direction) are stored
7739 * the byte after the 3-byte magic number */
7741 /* match outgoing packets */
7742 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7744 /* match incoming packets */
7745 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7750 bpf_error("inbound/outbound not supported on linktype %d",
7758 #ifdef HAVE_NET_PFVAR_H
7759 /* PF firewall log matched interface */
7761 gen_pf_ifname(const char *ifname
)
7766 if (linktype
!= DLT_PFLOG
) {
7767 bpf_error("ifname supported only on PF linktype");
7770 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7771 off
= offsetof(struct pfloghdr
, ifname
);
7772 if (strlen(ifname
) >= len
) {
7773 bpf_error("ifname interface names can only be %d characters",
7777 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7781 /* PF firewall log ruleset name */
7783 gen_pf_ruleset(char *ruleset
)
7787 if (linktype
!= DLT_PFLOG
) {
7788 bpf_error("ruleset supported only on PF linktype");
7792 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7793 bpf_error("ruleset names can only be %ld characters",
7794 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7798 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7799 strlen(ruleset
), (const u_char
*)ruleset
);
7803 /* PF firewall log rule number */
7809 if (linktype
!= DLT_PFLOG
) {
7810 bpf_error("rnr supported only on PF linktype");
7814 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7819 /* PF firewall log sub-rule number */
7821 gen_pf_srnr(int srnr
)
7825 if (linktype
!= DLT_PFLOG
) {
7826 bpf_error("srnr supported only on PF linktype");
7830 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7835 /* PF firewall log reason code */
7837 gen_pf_reason(int reason
)
7841 if (linktype
!= DLT_PFLOG
) {
7842 bpf_error("reason supported only on PF linktype");
7846 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7851 /* PF firewall log action */
7853 gen_pf_action(int action
)
7857 if (linktype
!= DLT_PFLOG
) {
7858 bpf_error("action supported only on PF linktype");
7862 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7866 #else /* !HAVE_NET_PFVAR_H */
7868 gen_pf_ifname(const char *ifname
)
7870 bpf_error("libpcap was compiled without pf support");
7876 gen_pf_ruleset(char *ruleset
)
7878 bpf_error("libpcap was compiled on a machine without pf support");
7886 bpf_error("libpcap was compiled on a machine without pf support");
7892 gen_pf_srnr(int srnr
)
7894 bpf_error("libpcap was compiled on a machine without pf support");
7900 gen_pf_reason(int reason
)
7902 bpf_error("libpcap was compiled on a machine without pf support");
7908 gen_pf_action(int action
)
7910 bpf_error("libpcap was compiled on a machine without pf support");
7914 #endif /* HAVE_NET_PFVAR_H */
7916 /* IEEE 802.11 wireless header */
7918 gen_p80211_type(int type
, int mask
)
7924 case DLT_IEEE802_11
:
7925 case DLT_PRISM_HEADER
:
7926 case DLT_IEEE802_11_RADIO_AVS
:
7927 case DLT_IEEE802_11_RADIO
:
7928 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7933 bpf_error("802.11 link-layer types supported only on 802.11");
7941 gen_p80211_fcdir(int fcdir
)
7947 case DLT_IEEE802_11
:
7948 case DLT_PRISM_HEADER
:
7949 case DLT_IEEE802_11_RADIO_AVS
:
7950 case DLT_IEEE802_11_RADIO
:
7954 bpf_error("frame direction supported only with 802.11 headers");
7958 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7959 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7966 register const u_char
*eaddr
;
7972 case DLT_ARCNET_LINUX
:
7973 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7975 return (gen_ahostop(eaddr
, (int)q
.dir
));
7977 bpf_error("ARCnet address used in non-arc expression");
7983 bpf_error("aid supported only on ARCnet");
7986 bpf_error("ARCnet address used in non-arc expression");
7991 static struct block
*
7992 gen_ahostop(eaddr
, dir
)
7993 register const u_char
*eaddr
;
7996 register struct block
*b0
, *b1
;
7999 /* src comes first, different from Ethernet */
8001 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
8004 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
8007 b0
= gen_ahostop(eaddr
, Q_SRC
);
8008 b1
= gen_ahostop(eaddr
, Q_DST
);
8014 b0
= gen_ahostop(eaddr
, Q_SRC
);
8015 b1
= gen_ahostop(eaddr
, Q_DST
);
8020 bpf_error("'addr1' is only supported on 802.11");
8024 bpf_error("'addr2' is only supported on 802.11");
8028 bpf_error("'addr3' is only supported on 802.11");
8032 bpf_error("'addr4' is only supported on 802.11");
8036 bpf_error("'ra' is only supported on 802.11");
8040 bpf_error("'ta' is only supported on 802.11");
8048 * support IEEE 802.1Q VLAN trunk over ethernet
8054 struct block
*b0
, *b1
;
8056 /* can't check for VLAN-encapsulated packets inside MPLS */
8057 if (label_stack_depth
> 0)
8058 bpf_error("no VLAN match after MPLS");
8061 * Check for a VLAN packet, and then change the offsets to point
8062 * to the type and data fields within the VLAN packet. Just
8063 * increment the offsets, so that we can support a hierarchy, e.g.
8064 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8067 * XXX - this is a bit of a kludge. If we were to split the
8068 * compiler into a parser that parses an expression and
8069 * generates an expression tree, and a code generator that
8070 * takes an expression tree (which could come from our
8071 * parser or from some other parser) and generates BPF code,
8072 * we could perhaps make the offsets parameters of routines
8073 * and, in the handler for an "AND" node, pass to subnodes
8074 * other than the VLAN node the adjusted offsets.
8076 * This would mean that "vlan" would, instead of changing the
8077 * behavior of *all* tests after it, change only the behavior
8078 * of tests ANDed with it. That would change the documented
8079 * semantics of "vlan", which might break some expressions.
8080 * However, it would mean that "(vlan and ip) or ip" would check
8081 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8082 * checking only for VLAN-encapsulated IP, so that could still
8083 * be considered worth doing; it wouldn't break expressions
8084 * that are of the form "vlan and ..." or "vlan N and ...",
8085 * which I suspect are the most common expressions involving
8086 * "vlan". "vlan or ..." doesn't necessarily do what the user
8087 * would really want, now, as all the "or ..." tests would
8088 * be done assuming a VLAN, even though the "or" could be viewed
8089 * as meaning "or, if this isn't a VLAN packet...".
8096 case DLT_NETANALYZER
:
8097 case DLT_NETANALYZER_TRANSPARENT
:
8098 /* check for VLAN, including QinQ */
8099 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8100 (bpf_int32
)ETHERTYPE_8021Q
);
8101 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
8102 (bpf_int32
)ETHERTYPE_8021QINQ
);
8106 /* If a specific VLAN is requested, check VLAN id */
8107 if (vlan_num
>= 0) {
8108 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
8109 (bpf_int32
)vlan_num
, 0x0fff);
8123 bpf_error("no VLAN support for data link type %d",
8138 struct block
*b0
,*b1
;
8141 * Change the offsets to point to the type and data fields within
8142 * the MPLS packet. Just increment the offsets, so that we
8143 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8144 * capture packets with an outer label of 100000 and an inner
8147 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8151 if (label_stack_depth
> 0) {
8152 /* just match the bottom-of-stack bit clear */
8153 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
8156 * Indicate that we're checking MPLS-encapsulated headers,
8157 * to make sure higher level code generators don't try to
8158 * match against IP-related protocols such as Q_ARP, Q_RARP
8163 case DLT_C_HDLC
: /* fall through */
8165 case DLT_NETANALYZER
:
8166 case DLT_NETANALYZER_TRANSPARENT
:
8167 b0
= gen_linktype(ETHERTYPE_MPLS
);
8171 b0
= gen_linktype(PPP_MPLS_UCAST
);
8174 /* FIXME add other DLT_s ...
8175 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8176 * leave it for now */
8179 bpf_error("no MPLS support for data link type %d",
8187 /* If a specific MPLS label is requested, check it */
8188 if (label_num
>= 0) {
8189 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8190 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8191 0xfffff000); /* only compare the first 20 bits */
8198 label_stack_depth
++;
8203 * Support PPPOE discovery and session.
8208 /* check for PPPoE discovery */
8209 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8218 * Test against the PPPoE session link-layer type.
8220 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8223 * Change the offsets to point to the type and data fields within
8224 * the PPP packet, and note that this is PPPoE rather than
8227 * XXX - this is a bit of a kludge. If we were to split the
8228 * compiler into a parser that parses an expression and
8229 * generates an expression tree, and a code generator that
8230 * takes an expression tree (which could come from our
8231 * parser or from some other parser) and generates BPF code,
8232 * we could perhaps make the offsets parameters of routines
8233 * and, in the handler for an "AND" node, pass to subnodes
8234 * other than the PPPoE node the adjusted offsets.
8236 * This would mean that "pppoes" would, instead of changing the
8237 * behavior of *all* tests after it, change only the behavior
8238 * of tests ANDed with it. That would change the documented
8239 * semantics of "pppoes", which might break some expressions.
8240 * However, it would mean that "(pppoes and ip) or ip" would check
8241 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8242 * checking only for VLAN-encapsulated IP, so that could still
8243 * be considered worth doing; it wouldn't break expressions
8244 * that are of the form "pppoes and ..." which I suspect are the
8245 * most common expressions involving "pppoes". "pppoes or ..."
8246 * doesn't necessarily do what the user would really want, now,
8247 * as all the "or ..." tests would be done assuming PPPoE, even
8248 * though the "or" could be viewed as meaning "or, if this isn't
8249 * a PPPoE packet...".
8251 orig_linktype
= off_linktype
; /* save original values */
8256 * The "network-layer" protocol is PPPoE, which has a 6-byte
8257 * PPPoE header, followed by a PPP packet.
8259 * There is no HDLC encapsulation for the PPP packet (it's
8260 * encapsulated in PPPoES instead), so the link-layer type
8261 * starts at the first byte of the PPP packet. For PPPoE,
8262 * that offset is relative to the beginning of the total
8263 * link-layer payload, including any 802.2 LLC header, so
8264 * it's 6 bytes past off_nl.
8266 off_linktype
= off_nl
+ 6;
8269 * The network-layer offsets are relative to the beginning
8270 * of the MAC-layer payload; that's past the 6-byte
8271 * PPPoE header and the 2-byte PPP header.
8274 off_nl_nosnap
= 6+2;
8280 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8292 bpf_error("'vpi' supported only on raw ATM");
8293 if (off_vpi
== (u_int
)-1)
8295 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8301 bpf_error("'vci' supported only on raw ATM");
8302 if (off_vci
== (u_int
)-1)
8304 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8309 if (off_proto
== (u_int
)-1)
8310 abort(); /* XXX - this isn't on FreeBSD */
8311 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8316 if (off_payload
== (u_int
)-1)
8318 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8319 0xffffffff, jtype
, reverse
, jvalue
);
8324 bpf_error("'callref' supported only on raw ATM");
8325 if (off_proto
== (u_int
)-1)
8327 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8328 jtype
, reverse
, jvalue
);
8338 gen_atmtype_abbrev(type
)
8341 struct block
*b0
, *b1
;
8346 /* Get all packets in Meta signalling Circuit */
8348 bpf_error("'metac' supported only on raw ATM");
8349 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8350 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8355 /* Get all packets in Broadcast Circuit*/
8357 bpf_error("'bcc' supported only on raw ATM");
8358 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8359 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8364 /* Get all cells in Segment OAM F4 circuit*/
8366 bpf_error("'oam4sc' supported only on raw ATM");
8367 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8368 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8373 /* Get all cells in End-to-End OAM F4 Circuit*/
8375 bpf_error("'oam4ec' supported only on raw ATM");
8376 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8377 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8382 /* Get all packets in connection Signalling Circuit */
8384 bpf_error("'sc' supported only on raw ATM");
8385 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8386 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8391 /* Get all packets in ILMI Circuit */
8393 bpf_error("'ilmic' supported only on raw ATM");
8394 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8395 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8400 /* Get all LANE packets */
8402 bpf_error("'lane' supported only on raw ATM");
8403 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8406 * Arrange that all subsequent tests assume LANE
8407 * rather than LLC-encapsulated packets, and set
8408 * the offsets appropriately for LANE-encapsulated
8411 * "off_mac" is the offset of the Ethernet header,
8412 * which is 2 bytes past the ATM pseudo-header
8413 * (skipping the pseudo-header and 2-byte LE Client
8414 * field). The other offsets are Ethernet offsets
8415 * relative to "off_mac".
8418 off_mac
= off_payload
+ 2; /* MAC header */
8419 off_linktype
= off_mac
+ 12;
8420 off_macpl
= off_mac
+ 14; /* Ethernet */
8421 off_nl
= 0; /* Ethernet II */
8422 off_nl_nosnap
= 3; /* 802.3+802.2 */
8426 /* Get all LLC-encapsulated packets */
8428 bpf_error("'llc' supported only on raw ATM");
8429 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8440 * Filtering for MTP2 messages based on li value
8441 * FISU, length is null
8442 * LSSU, length is 1 or 2
8443 * MSU, length is 3 or more
8446 gen_mtp2type_abbrev(type
)
8449 struct block
*b0
, *b1
;
8454 if ( (linktype
!= DLT_MTP2
) &&
8455 (linktype
!= DLT_ERF
) &&
8456 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8457 bpf_error("'fisu' supported only on MTP2");
8458 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8459 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8463 if ( (linktype
!= DLT_MTP2
) &&
8464 (linktype
!= DLT_ERF
) &&
8465 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8466 bpf_error("'lssu' supported only on MTP2");
8467 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8468 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8473 if ( (linktype
!= DLT_MTP2
) &&
8474 (linktype
!= DLT_ERF
) &&
8475 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8476 bpf_error("'msu' supported only on MTP2");
8477 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8487 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8494 bpf_u_int32 val1
, val2
, val3
;
8496 switch (mtp3field
) {
8499 if (off_sio
== (u_int
)-1)
8500 bpf_error("'sio' supported only on SS7");
8501 /* sio coded on 1 byte so max value 255 */
8503 bpf_error("sio value %u too big; max value = 255",
8505 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8506 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8510 if (off_opc
== (u_int
)-1)
8511 bpf_error("'opc' supported only on SS7");
8512 /* opc coded on 14 bits so max value 16383 */
8514 bpf_error("opc value %u too big; max value = 16383",
8516 /* the following instructions are made to convert jvalue
8517 * to the form used to write opc in an ss7 message*/
8518 val1
= jvalue
& 0x00003c00;
8520 val2
= jvalue
& 0x000003fc;
8522 val3
= jvalue
& 0x00000003;
8524 jvalue
= val1
+ val2
+ val3
;
8525 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8526 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8530 if (off_dpc
== (u_int
)-1)
8531 bpf_error("'dpc' supported only on SS7");
8532 /* dpc coded on 14 bits so max value 16383 */
8534 bpf_error("dpc value %u too big; max value = 16383",
8536 /* the following instructions are made to convert jvalue
8537 * to the forme used to write dpc in an ss7 message*/
8538 val1
= jvalue
& 0x000000ff;
8540 val2
= jvalue
& 0x00003f00;
8542 jvalue
= val1
+ val2
;
8543 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8544 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8548 if (off_sls
== (u_int
)-1)
8549 bpf_error("'sls' supported only on SS7");
8550 /* sls coded on 4 bits so max value 15 */
8552 bpf_error("sls value %u too big; max value = 15",
8554 /* the following instruction is made to convert jvalue
8555 * to the forme used to write sls in an ss7 message*/
8556 jvalue
= jvalue
<< 4;
8557 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8558 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8567 static struct block
*
8568 gen_msg_abbrev(type
)
8574 * Q.2931 signalling protocol messages for handling virtual circuits
8575 * establishment and teardown
8580 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8584 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8588 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8592 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8596 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8599 case A_RELEASE_DONE
:
8600 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8610 gen_atmmulti_abbrev(type
)
8613 struct block
*b0
, *b1
;
8619 bpf_error("'oam' supported only on raw ATM");
8620 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8625 bpf_error("'oamf4' supported only on raw ATM");
8627 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8628 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8630 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8636 * Get Q.2931 signalling messages for switched
8637 * virtual connection
8640 bpf_error("'connectmsg' supported only on raw ATM");
8641 b0
= gen_msg_abbrev(A_SETUP
);
8642 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8644 b0
= gen_msg_abbrev(A_CONNECT
);
8646 b0
= gen_msg_abbrev(A_CONNECTACK
);
8648 b0
= gen_msg_abbrev(A_RELEASE
);
8650 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8652 b0
= gen_atmtype_abbrev(A_SC
);
8658 bpf_error("'metaconnect' supported only on raw ATM");
8659 b0
= gen_msg_abbrev(A_SETUP
);
8660 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8662 b0
= gen_msg_abbrev(A_CONNECT
);
8664 b0
= gen_msg_abbrev(A_RELEASE
);
8666 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8668 b0
= gen_atmtype_abbrev(A_METAC
);