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_SYMANTEC_FIREWALL
:
1245 off_nl
= 0; /* Ethernet II */
1246 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1249 #ifdef HAVE_NET_PFVAR_H
1252 off_macpl
= PFLOG_HDRLEN
;
1254 off_nl_nosnap
= 0; /* no 802.2 LLC */
1258 case DLT_JUNIPER_MFR
:
1259 case DLT_JUNIPER_MLFR
:
1260 case DLT_JUNIPER_MLPPP
:
1261 case DLT_JUNIPER_PPP
:
1262 case DLT_JUNIPER_CHDLC
:
1263 case DLT_JUNIPER_FRELAY
:
1267 off_nl_nosnap
= -1; /* no 802.2 LLC */
1270 case DLT_JUNIPER_ATM1
:
1271 off_linktype
= 4; /* in reality variable between 4-8 */
1272 off_macpl
= 4; /* in reality variable between 4-8 */
1277 case DLT_JUNIPER_ATM2
:
1278 off_linktype
= 8; /* in reality variable between 8-12 */
1279 off_macpl
= 8; /* in reality variable between 8-12 */
1284 /* frames captured on a Juniper PPPoE service PIC
1285 * contain raw ethernet frames */
1286 case DLT_JUNIPER_PPPOE
:
1287 case DLT_JUNIPER_ETHER
:
1290 off_nl
= 18; /* Ethernet II */
1291 off_nl_nosnap
= 21; /* 802.3+802.2 */
1294 case DLT_JUNIPER_PPPOE_ATM
:
1298 off_nl_nosnap
= -1; /* no 802.2 LLC */
1301 case DLT_JUNIPER_GGSN
:
1305 off_nl_nosnap
= -1; /* no 802.2 LLC */
1308 case DLT_JUNIPER_ES
:
1310 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1311 off_nl
= -1; /* not really a network layer but raw IP addresses */
1312 off_nl_nosnap
= -1; /* no 802.2 LLC */
1315 case DLT_JUNIPER_MONITOR
:
1318 off_nl
= 0; /* raw IP/IP6 header */
1319 off_nl_nosnap
= -1; /* no 802.2 LLC */
1322 case DLT_JUNIPER_SERVICES
:
1324 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1325 off_nl
= -1; /* L3 proto location dep. on cookie type */
1326 off_nl_nosnap
= -1; /* no 802.2 LLC */
1329 case DLT_JUNIPER_VP
:
1336 case DLT_JUNIPER_ST
:
1343 case DLT_JUNIPER_ISM
:
1350 case DLT_JUNIPER_VS
:
1351 case DLT_JUNIPER_SRX_E2E
:
1352 case DLT_JUNIPER_FIBRECHANNEL
:
1353 case DLT_JUNIPER_ATM_CEMIC
:
1372 case DLT_MTP2_WITH_PHDR
:
1407 * Currently, only raw "link[N:M]" filtering is supported.
1409 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1411 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1412 off_nl_nosnap
= -1; /* no 802.2 LLC */
1413 off_mac
= 1; /* step over the kiss length byte */
1418 off_macpl
= 24; /* ipnet header length */
1423 case DLT_NETANALYZER
:
1424 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1425 off_linktype
= 16; /* includes 4-byte pseudo-header */
1426 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1427 off_nl
= 0; /* Ethernet II */
1428 off_nl_nosnap
= 3; /* 802.3+802.2 */
1431 case DLT_NETANALYZER_TRANSPARENT
:
1432 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1433 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1434 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1435 off_nl
= 0; /* Ethernet II */
1436 off_nl_nosnap
= 3; /* 802.3+802.2 */
1441 * For values in the range in which we've assigned new
1442 * DLT_ values, only raw "link[N:M]" filtering is supported.
1444 if (linktype
>= DLT_MATCHING_MIN
&&
1445 linktype
<= DLT_MATCHING_MAX
) {
1454 bpf_error("unknown data link type %d", linktype
);
1459 * Load a value relative to the beginning of the link-layer header.
1460 * The link-layer header doesn't necessarily begin at the beginning
1461 * of the packet data; there might be a variable-length prefix containing
1462 * radio information.
1464 static struct slist
*
1465 gen_load_llrel(offset
, size
)
1468 struct slist
*s
, *s2
;
1470 s
= gen_llprefixlen();
1473 * If "s" is non-null, it has code to arrange that the X register
1474 * contains the length of the prefix preceding the link-layer
1477 * Otherwise, the length of the prefix preceding the link-layer
1478 * header is "off_ll".
1482 * There's a variable-length prefix preceding the
1483 * link-layer header. "s" points to a list of statements
1484 * that put the length of that prefix into the X register.
1485 * do an indirect load, to use the X register as an offset.
1487 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1492 * There is no variable-length header preceding the
1493 * link-layer header; add in off_ll, which, if there's
1494 * a fixed-length header preceding the link-layer header,
1495 * is the length of that header.
1497 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1498 s
->s
.k
= offset
+ off_ll
;
1504 * Load a value relative to the beginning of the MAC-layer payload.
1506 static struct slist
*
1507 gen_load_macplrel(offset
, size
)
1510 struct slist
*s
, *s2
;
1512 s
= gen_off_macpl();
1515 * If s is non-null, the offset of the MAC-layer payload is
1516 * variable, and s points to a list of instructions that
1517 * arrange that the X register contains that offset.
1519 * Otherwise, the offset of the MAC-layer payload is constant,
1520 * and is in off_macpl.
1524 * The offset of the MAC-layer payload is in the X
1525 * register. Do an indirect load, to use the X register
1528 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1533 * The offset of the MAC-layer payload is constant,
1534 * and is in off_macpl; load the value at that offset
1535 * plus the specified offset.
1537 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1538 s
->s
.k
= off_macpl
+ offset
;
1544 * Load a value relative to the beginning of the specified header.
1546 static struct slist
*
1547 gen_load_a(offrel
, offset
, size
)
1548 enum e_offrel offrel
;
1551 struct slist
*s
, *s2
;
1556 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1561 s
= gen_load_llrel(offset
, size
);
1565 s
= gen_load_macplrel(offset
, size
);
1569 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1573 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1578 * Load the X register with the length of the IPv4 header
1579 * (plus the offset of the link-layer header, if it's
1580 * preceded by a variable-length header such as a radio
1581 * header), in bytes.
1583 s
= gen_loadx_iphdrlen();
1586 * Load the item at {offset of the MAC-layer payload} +
1587 * {offset, relative to the start of the MAC-layer
1588 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1589 * {specified offset}.
1591 * (If the offset of the MAC-layer payload is variable,
1592 * it's included in the value in the X register, and
1595 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1596 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1601 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1612 * Generate code to load into the X register the sum of the length of
1613 * the IPv4 header and any variable-length header preceding the link-layer
1616 static struct slist
*
1617 gen_loadx_iphdrlen()
1619 struct slist
*s
, *s2
;
1621 s
= gen_off_macpl();
1624 * There's a variable-length prefix preceding the
1625 * link-layer header, or the link-layer header is itself
1626 * variable-length. "s" points to a list of statements
1627 * that put the offset of the MAC-layer payload into
1630 * The 4*([k]&0xf) addressing mode can't be used, as we
1631 * don't have a constant offset, so we have to load the
1632 * value in question into the A register and add to it
1633 * the value from the X register.
1635 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1638 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1641 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1646 * The A register now contains the length of the
1647 * IP header. We need to add to it the offset of
1648 * the MAC-layer payload, which is still in the X
1649 * register, and move the result into the X register.
1651 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1652 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1655 * There is no variable-length header preceding the
1656 * link-layer header, and the link-layer header is
1657 * fixed-length; load the length of the IPv4 header,
1658 * which is at an offset of off_nl from the beginning
1659 * of the MAC-layer payload, and thus at an offset
1660 * of off_mac_pl + off_nl from the beginning of the
1663 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1664 s
->s
.k
= off_macpl
+ off_nl
;
1669 static struct block
*
1676 s
= new_stmt(BPF_LD
|BPF_IMM
);
1678 b
= new_block(JMP(BPF_JEQ
));
1684 static inline struct block
*
1687 return gen_uncond(1);
1690 static inline struct block
*
1693 return gen_uncond(0);
1697 * Byte-swap a 32-bit number.
1698 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1699 * big-endian platforms.)
1701 #define SWAPLONG(y) \
1702 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1705 * Generate code to match a particular packet type.
1707 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1708 * value, if <= ETHERMTU. We use that to determine whether to
1709 * match the type/length field or to check the type/length field for
1710 * a value <= ETHERMTU to see whether it's a type field and then do
1711 * the appropriate test.
1713 static struct block
*
1714 gen_ether_linktype(proto
)
1717 struct block
*b0
, *b1
;
1723 case LLCSAP_NETBEUI
:
1725 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1726 * so we check the DSAP and SSAP.
1728 * LLCSAP_IP checks for IP-over-802.2, rather
1729 * than IP-over-Ethernet or IP-over-SNAP.
1731 * XXX - should we check both the DSAP and the
1732 * SSAP, like this, or should we check just the
1733 * DSAP, as we do for other types <= ETHERMTU
1734 * (i.e., other SAP values)?
1736 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1738 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1739 ((proto
<< 8) | proto
));
1747 * Ethernet_II frames, which are Ethernet
1748 * frames with a frame type of ETHERTYPE_IPX;
1750 * Ethernet_802.3 frames, which are 802.3
1751 * frames (i.e., the type/length field is
1752 * a length field, <= ETHERMTU, rather than
1753 * a type field) with the first two bytes
1754 * after the Ethernet/802.3 header being
1757 * Ethernet_802.2 frames, which are 802.3
1758 * frames with an 802.2 LLC header and
1759 * with the IPX LSAP as the DSAP in the LLC
1762 * Ethernet_SNAP frames, which are 802.3
1763 * frames with an LLC header and a SNAP
1764 * header and with an OUI of 0x000000
1765 * (encapsulated Ethernet) and a protocol
1766 * ID of ETHERTYPE_IPX in the SNAP header.
1768 * XXX - should we generate the same code both
1769 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1773 * This generates code to check both for the
1774 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1776 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1777 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1781 * Now we add code to check for SNAP frames with
1782 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1784 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1788 * Now we generate code to check for 802.3
1789 * frames in general.
1791 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1795 * Now add the check for 802.3 frames before the
1796 * check for Ethernet_802.2 and Ethernet_802.3,
1797 * as those checks should only be done on 802.3
1798 * frames, not on Ethernet frames.
1803 * Now add the check for Ethernet_II frames, and
1804 * do that before checking for the other frame
1807 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1808 (bpf_int32
)ETHERTYPE_IPX
);
1812 case ETHERTYPE_ATALK
:
1813 case ETHERTYPE_AARP
:
1815 * EtherTalk (AppleTalk protocols on Ethernet link
1816 * layer) may use 802.2 encapsulation.
1820 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1821 * we check for an Ethernet type field less than
1822 * 1500, which means it's an 802.3 length field.
1824 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1828 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1829 * SNAP packets with an organization code of
1830 * 0x080007 (Apple, for Appletalk) and a protocol
1831 * type of ETHERTYPE_ATALK (Appletalk).
1833 * 802.2-encapsulated ETHERTYPE_AARP packets are
1834 * SNAP packets with an organization code of
1835 * 0x000000 (encapsulated Ethernet) and a protocol
1836 * type of ETHERTYPE_AARP (Appletalk ARP).
1838 if (proto
== ETHERTYPE_ATALK
)
1839 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1840 else /* proto == ETHERTYPE_AARP */
1841 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1845 * Check for Ethernet encapsulation (Ethertalk
1846 * phase 1?); we just check for the Ethernet
1849 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1855 if (proto
<= ETHERMTU
) {
1857 * This is an LLC SAP value, so the frames
1858 * that match would be 802.2 frames.
1859 * Check that the frame is an 802.2 frame
1860 * (i.e., that the length/type field is
1861 * a length field, <= ETHERMTU) and
1862 * then check the DSAP.
1864 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1866 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1872 * This is an Ethernet type, so compare
1873 * the length/type field with it (if
1874 * the frame is an 802.2 frame, the length
1875 * field will be <= ETHERMTU, and, as
1876 * "proto" is > ETHERMTU, this test
1877 * will fail and the frame won't match,
1878 * which is what we want).
1880 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1887 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1888 * or IPv6 then we have an error.
1890 static struct block
*
1891 gen_ipnet_linktype(proto
)
1897 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1898 (bpf_int32
)IPH_AF_INET
);
1901 case ETHERTYPE_IPV6
:
1902 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1903 (bpf_int32
)IPH_AF_INET6
);
1914 * Generate code to match a particular packet type.
1916 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1917 * value, if <= ETHERMTU. We use that to determine whether to
1918 * match the type field or to check the type field for the special
1919 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1921 static struct block
*
1922 gen_linux_sll_linktype(proto
)
1925 struct block
*b0
, *b1
;
1931 case LLCSAP_NETBEUI
:
1933 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1934 * so we check the DSAP and SSAP.
1936 * LLCSAP_IP checks for IP-over-802.2, rather
1937 * than IP-over-Ethernet or IP-over-SNAP.
1939 * XXX - should we check both the DSAP and the
1940 * SSAP, like this, or should we check just the
1941 * DSAP, as we do for other types <= ETHERMTU
1942 * (i.e., other SAP values)?
1944 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1945 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1946 ((proto
<< 8) | proto
));
1952 * Ethernet_II frames, which are Ethernet
1953 * frames with a frame type of ETHERTYPE_IPX;
1955 * Ethernet_802.3 frames, which have a frame
1956 * type of LINUX_SLL_P_802_3;
1958 * Ethernet_802.2 frames, which are 802.3
1959 * frames with an 802.2 LLC header (i.e, have
1960 * a frame type of LINUX_SLL_P_802_2) and
1961 * with the IPX LSAP as the DSAP in the LLC
1964 * Ethernet_SNAP frames, which are 802.3
1965 * frames with an LLC header and a SNAP
1966 * header and with an OUI of 0x000000
1967 * (encapsulated Ethernet) and a protocol
1968 * ID of ETHERTYPE_IPX in the SNAP header.
1970 * First, do the checks on LINUX_SLL_P_802_2
1971 * frames; generate the check for either
1972 * Ethernet_802.2 or Ethernet_SNAP frames, and
1973 * then put a check for LINUX_SLL_P_802_2 frames
1976 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1977 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1979 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1983 * Now check for 802.3 frames and OR that with
1984 * the previous test.
1986 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1990 * Now add the check for Ethernet_II frames, and
1991 * do that before checking for the other frame
1994 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1995 (bpf_int32
)ETHERTYPE_IPX
);
1999 case ETHERTYPE_ATALK
:
2000 case ETHERTYPE_AARP
:
2002 * EtherTalk (AppleTalk protocols on Ethernet link
2003 * layer) may use 802.2 encapsulation.
2007 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2008 * we check for the 802.2 protocol type in the
2009 * "Ethernet type" field.
2011 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2014 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2015 * SNAP packets with an organization code of
2016 * 0x080007 (Apple, for Appletalk) and a protocol
2017 * type of ETHERTYPE_ATALK (Appletalk).
2019 * 802.2-encapsulated ETHERTYPE_AARP packets are
2020 * SNAP packets with an organization code of
2021 * 0x000000 (encapsulated Ethernet) and a protocol
2022 * type of ETHERTYPE_AARP (Appletalk ARP).
2024 if (proto
== ETHERTYPE_ATALK
)
2025 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2026 else /* proto == ETHERTYPE_AARP */
2027 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2031 * Check for Ethernet encapsulation (Ethertalk
2032 * phase 1?); we just check for the Ethernet
2035 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2041 if (proto
<= ETHERMTU
) {
2043 * This is an LLC SAP value, so the frames
2044 * that match would be 802.2 frames.
2045 * Check for the 802.2 protocol type
2046 * in the "Ethernet type" field, and
2047 * then check the DSAP.
2049 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2051 b1
= gen_cmp(OR_LINK
, off_macpl
, 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
,
2071 static struct slist
*
2072 gen_load_prism_llprefixlen()
2074 struct slist
*s1
, *s2
;
2075 struct slist
*sjeq_avs_cookie
;
2076 struct slist
*sjcommon
;
2079 * This code is not compatible with the optimizer, as
2080 * we are generating jmp instructions within a normal
2081 * slist of instructions
2086 * Generate code to load the length of the radio header into
2087 * the register assigned to hold that length, if one has been
2088 * assigned. (If one hasn't been assigned, no code we've
2089 * generated uses that prefix, so we don't need to generate any
2092 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2093 * or always use the AVS header rather than the Prism header.
2094 * We load a 4-byte big-endian value at the beginning of the
2095 * raw packet data, and see whether, when masked with 0xFFFFF000,
2096 * it's equal to 0x80211000. If so, that indicates that it's
2097 * an AVS header (the masked-out bits are the version number).
2098 * Otherwise, it's a Prism header.
2100 * XXX - the Prism header is also, in theory, variable-length,
2101 * but no known software generates headers that aren't 144
2104 if (reg_off_ll
!= -1) {
2108 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2112 * AND it with 0xFFFFF000.
2114 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2115 s2
->s
.k
= 0xFFFFF000;
2119 * Compare with 0x80211000.
2121 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2122 sjeq_avs_cookie
->s
.k
= 0x80211000;
2123 sappend(s1
, sjeq_avs_cookie
);
2128 * The 4 bytes at an offset of 4 from the beginning of
2129 * the AVS header are the length of the AVS header.
2130 * That field is big-endian.
2132 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2135 sjeq_avs_cookie
->s
.jt
= s2
;
2138 * Now jump to the code to allocate a register
2139 * into which to save the header length and
2140 * store the length there. (The "jump always"
2141 * instruction needs to have the k field set;
2142 * it's added to the PC, so, as we're jumping
2143 * over a single instruction, it should be 1.)
2145 sjcommon
= new_stmt(JMP(BPF_JA
));
2147 sappend(s1
, sjcommon
);
2150 * Now for the code that handles the Prism header.
2151 * Just load the length of the Prism header (144)
2152 * into the A register. Have the test for an AVS
2153 * header branch here if we don't have an AVS header.
2155 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2158 sjeq_avs_cookie
->s
.jf
= s2
;
2161 * Now allocate a register to hold that value and store
2162 * it. The code for the AVS header will jump here after
2163 * loading the length of the AVS header.
2165 s2
= new_stmt(BPF_ST
);
2166 s2
->s
.k
= reg_off_ll
;
2168 sjcommon
->s
.jf
= s2
;
2171 * Now move it into the X register.
2173 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2181 static struct slist
*
2182 gen_load_avs_llprefixlen()
2184 struct slist
*s1
, *s2
;
2187 * Generate code to load the length of the AVS header into
2188 * the register assigned to hold that length, if one has been
2189 * assigned. (If one hasn't been assigned, no code we've
2190 * generated uses that prefix, so we don't need to generate any
2193 if (reg_off_ll
!= -1) {
2195 * The 4 bytes at an offset of 4 from the beginning of
2196 * the AVS header are the length of the AVS header.
2197 * That field is big-endian.
2199 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2203 * Now allocate a register to hold that value and store
2206 s2
= new_stmt(BPF_ST
);
2207 s2
->s
.k
= reg_off_ll
;
2211 * Now move it into the X register.
2213 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2221 static struct slist
*
2222 gen_load_radiotap_llprefixlen()
2224 struct slist
*s1
, *s2
;
2227 * Generate code to load the length of the radiotap header into
2228 * the register assigned to hold that length, if one has been
2229 * assigned. (If one hasn't been assigned, no code we've
2230 * generated uses that prefix, so we don't need to generate any
2233 if (reg_off_ll
!= -1) {
2235 * The 2 bytes at offsets of 2 and 3 from the beginning
2236 * of the radiotap header are the length of the radiotap
2237 * header; unfortunately, it's little-endian, so we have
2238 * to load it a byte at a time and construct the value.
2242 * Load the high-order byte, at an offset of 3, shift it
2243 * left a byte, and put the result in the X register.
2245 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2247 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2250 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2254 * Load the next byte, at an offset of 2, and OR the
2255 * value from the X register into it.
2257 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2260 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2264 * Now allocate a register to hold that value and store
2267 s2
= new_stmt(BPF_ST
);
2268 s2
->s
.k
= reg_off_ll
;
2272 * Now move it into the X register.
2274 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2283 * At the moment we treat PPI as normal Radiotap encoded
2284 * packets. The difference is in the function that generates
2285 * the code at the beginning to compute the header length.
2286 * Since this code generator of PPI supports bare 802.11
2287 * encapsulation only (i.e. the encapsulated DLT should be
2288 * DLT_IEEE802_11) we generate code to check for this too;
2289 * that's done in finish_parse().
2291 static struct slist
*
2292 gen_load_ppi_llprefixlen()
2294 struct slist
*s1
, *s2
;
2297 * Generate code to load the length of the radiotap header
2298 * into the register assigned to hold that length, if one has
2301 if (reg_off_ll
!= -1) {
2303 * The 2 bytes at offsets of 2 and 3 from the beginning
2304 * of the radiotap header are the length of the radiotap
2305 * header; unfortunately, it's little-endian, so we have
2306 * to load it a byte at a time and construct the value.
2310 * Load the high-order byte, at an offset of 3, shift it
2311 * left a byte, and put the result in the X register.
2313 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2315 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2318 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2322 * Load the next byte, at an offset of 2, and OR the
2323 * value from the X register into it.
2325 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2328 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2332 * Now allocate a register to hold that value and store
2335 s2
= new_stmt(BPF_ST
);
2336 s2
->s
.k
= reg_off_ll
;
2340 * Now move it into the X register.
2342 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2351 * Load a value relative to the beginning of the link-layer header after the 802.11
2352 * header, i.e. LLC_SNAP.
2353 * The link-layer header doesn't necessarily begin at the beginning
2354 * of the packet data; there might be a variable-length prefix containing
2355 * radio information.
2357 static struct slist
*
2358 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2361 struct slist
*sjset_data_frame_1
;
2362 struct slist
*sjset_data_frame_2
;
2363 struct slist
*sjset_qos
;
2364 struct slist
*sjset_radiotap_flags
;
2365 struct slist
*sjset_radiotap_tsft
;
2366 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2367 struct slist
*s_roundup
;
2369 if (reg_off_macpl
== -1) {
2371 * No register has been assigned to the offset of
2372 * the MAC-layer payload, which means nobody needs
2373 * it; don't bother computing it - just return
2374 * what we already have.
2380 * This code is not compatible with the optimizer, as
2381 * we are generating jmp instructions within a normal
2382 * slist of instructions
2387 * If "s" is non-null, it has code to arrange that the X register
2388 * contains the length of the prefix preceding the link-layer
2391 * Otherwise, the length of the prefix preceding the link-layer
2392 * header is "off_ll".
2396 * There is no variable-length header preceding the
2397 * link-layer header.
2399 * Load the length of the fixed-length prefix preceding
2400 * the link-layer header (if any) into the X register,
2401 * and store it in the reg_off_macpl register.
2402 * That length is off_ll.
2404 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2409 * The X register contains the offset of the beginning of the
2410 * link-layer header; add 24, which is the minimum length
2411 * of the MAC header for a data frame, to that, and store it
2412 * in reg_off_macpl, and then load the Frame Control field,
2413 * which is at the offset in the X register, with an indexed load.
2415 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2417 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2420 s2
= new_stmt(BPF_ST
);
2421 s2
->s
.k
= reg_off_macpl
;
2424 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2429 * Check the Frame Control field to see if this is a data frame;
2430 * a data frame has the 0x08 bit (b3) in that field set and the
2431 * 0x04 bit (b2) clear.
2433 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2434 sjset_data_frame_1
->s
.k
= 0x08;
2435 sappend(s
, sjset_data_frame_1
);
2438 * If b3 is set, test b2, otherwise go to the first statement of
2439 * the rest of the program.
2441 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2442 sjset_data_frame_2
->s
.k
= 0x04;
2443 sappend(s
, sjset_data_frame_2
);
2444 sjset_data_frame_1
->s
.jf
= snext
;
2447 * If b2 is not set, this is a data frame; test the QoS bit.
2448 * Otherwise, go to the first statement of the rest of the
2451 sjset_data_frame_2
->s
.jt
= snext
;
2452 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2453 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2454 sappend(s
, sjset_qos
);
2457 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2459 * Otherwise, go to the first statement of the rest of the
2462 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2463 s2
->s
.k
= reg_off_macpl
;
2465 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2468 s2
= new_stmt(BPF_ST
);
2469 s2
->s
.k
= reg_off_macpl
;
2473 * If we have a radiotap header, look at it to see whether
2474 * there's Atheros padding between the MAC-layer header
2477 * Note: all of the fields in the radiotap header are
2478 * little-endian, so we byte-swap all of the values
2479 * we test against, as they will be loaded as big-endian
2482 if (linktype
== DLT_IEEE802_11_RADIO
) {
2484 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2485 * in the presence flag?
2487 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2491 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2492 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2493 sappend(s
, sjset_radiotap_flags
);
2496 * If not, skip all of this.
2498 sjset_radiotap_flags
->s
.jf
= snext
;
2501 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2503 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2504 new_stmt(JMP(BPF_JSET
));
2505 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2506 sappend(s
, sjset_radiotap_tsft
);
2509 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2510 * at an offset of 16 from the beginning of the raw packet
2511 * data (8 bytes for the radiotap header and 8 bytes for
2514 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2517 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2521 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2522 sjset_tsft_datapad
->s
.k
= 0x20;
2523 sappend(s
, sjset_tsft_datapad
);
2526 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2527 * at an offset of 8 from the beginning of the raw packet
2528 * data (8 bytes for the radiotap header).
2530 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2533 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2537 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2538 sjset_notsft_datapad
->s
.k
= 0x20;
2539 sappend(s
, sjset_notsft_datapad
);
2542 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2543 * set, round the length of the 802.11 header to
2544 * a multiple of 4. Do that by adding 3 and then
2545 * dividing by and multiplying by 4, which we do by
2548 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2549 s_roundup
->s
.k
= reg_off_macpl
;
2550 sappend(s
, s_roundup
);
2551 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2554 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2557 s2
= new_stmt(BPF_ST
);
2558 s2
->s
.k
= reg_off_macpl
;
2561 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2562 sjset_tsft_datapad
->s
.jf
= snext
;
2563 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2564 sjset_notsft_datapad
->s
.jf
= snext
;
2566 sjset_qos
->s
.jf
= snext
;
2572 insert_compute_vloffsets(b
)
2578 * For link-layer types that have a variable-length header
2579 * preceding the link-layer header, generate code to load
2580 * the offset of the link-layer header into the register
2581 * assigned to that offset, if any.
2585 case DLT_PRISM_HEADER
:
2586 s
= gen_load_prism_llprefixlen();
2589 case DLT_IEEE802_11_RADIO_AVS
:
2590 s
= gen_load_avs_llprefixlen();
2593 case DLT_IEEE802_11_RADIO
:
2594 s
= gen_load_radiotap_llprefixlen();
2598 s
= gen_load_ppi_llprefixlen();
2607 * For link-layer types that have a variable-length link-layer
2608 * header, generate code to load the offset of the MAC-layer
2609 * payload into the register assigned to that offset, if any.
2613 case DLT_IEEE802_11
:
2614 case DLT_PRISM_HEADER
:
2615 case DLT_IEEE802_11_RADIO_AVS
:
2616 case DLT_IEEE802_11_RADIO
:
2618 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2623 * If we have any offset-loading code, append all the
2624 * existing statements in the block to those statements,
2625 * and make the resulting list the list of statements
2629 sappend(s
, b
->stmts
);
2634 static struct block
*
2635 gen_ppi_dlt_check(void)
2637 struct slist
*s_load_dlt
;
2640 if (linktype
== DLT_PPI
)
2642 /* Create the statements that check for the DLT
2644 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2645 s_load_dlt
->s
.k
= 4;
2647 b
= new_block(JMP(BPF_JEQ
));
2649 b
->stmts
= s_load_dlt
;
2650 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2660 static struct slist
*
2661 gen_prism_llprefixlen(void)
2665 if (reg_off_ll
== -1) {
2667 * We haven't yet assigned a register for the length
2668 * of the radio header; allocate one.
2670 reg_off_ll
= alloc_reg();
2674 * Load the register containing the radio length
2675 * into the X register.
2677 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2678 s
->s
.k
= reg_off_ll
;
2682 static struct slist
*
2683 gen_avs_llprefixlen(void)
2687 if (reg_off_ll
== -1) {
2689 * We haven't yet assigned a register for the length
2690 * of the AVS header; allocate one.
2692 reg_off_ll
= alloc_reg();
2696 * Load the register containing the AVS length
2697 * into the X register.
2699 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2700 s
->s
.k
= reg_off_ll
;
2704 static struct slist
*
2705 gen_radiotap_llprefixlen(void)
2709 if (reg_off_ll
== -1) {
2711 * We haven't yet assigned a register for the length
2712 * of the radiotap header; allocate one.
2714 reg_off_ll
= alloc_reg();
2718 * Load the register containing the radiotap length
2719 * into the X register.
2721 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2722 s
->s
.k
= reg_off_ll
;
2727 * At the moment we treat PPI as normal Radiotap encoded
2728 * packets. The difference is in the function that generates
2729 * the code at the beginning to compute the header length.
2730 * Since this code generator of PPI supports bare 802.11
2731 * encapsulation only (i.e. the encapsulated DLT should be
2732 * DLT_IEEE802_11) we generate code to check for this too.
2734 static struct slist
*
2735 gen_ppi_llprefixlen(void)
2739 if (reg_off_ll
== -1) {
2741 * We haven't yet assigned a register for the length
2742 * of the radiotap header; allocate one.
2744 reg_off_ll
= alloc_reg();
2748 * Load the register containing the PPI length
2749 * into the X register.
2751 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2752 s
->s
.k
= reg_off_ll
;
2757 * Generate code to compute the link-layer header length, if necessary,
2758 * putting it into the X register, and to return either a pointer to a
2759 * "struct slist" for the list of statements in that code, or NULL if
2760 * no code is necessary.
2762 static struct slist
*
2763 gen_llprefixlen(void)
2767 case DLT_PRISM_HEADER
:
2768 return gen_prism_llprefixlen();
2770 case DLT_IEEE802_11_RADIO_AVS
:
2771 return gen_avs_llprefixlen();
2773 case DLT_IEEE802_11_RADIO
:
2774 return gen_radiotap_llprefixlen();
2777 return gen_ppi_llprefixlen();
2785 * Generate code to load the register containing the offset of the
2786 * MAC-layer payload into the X register; if no register for that offset
2787 * has been allocated, allocate it first.
2789 static struct slist
*
2794 if (off_macpl_is_variable
) {
2795 if (reg_off_macpl
== -1) {
2797 * We haven't yet assigned a register for the offset
2798 * of the MAC-layer payload; allocate one.
2800 reg_off_macpl
= alloc_reg();
2804 * Load the register containing the offset of the MAC-layer
2805 * payload into the X register.
2807 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2808 s
->s
.k
= reg_off_macpl
;
2812 * That offset isn't variable, so we don't need to
2813 * generate any code.
2820 * Map an Ethernet type to the equivalent PPP type.
2823 ethertype_to_ppptype(proto
)
2833 case ETHERTYPE_IPV6
:
2842 case ETHERTYPE_ATALK
:
2856 * I'm assuming the "Bridging PDU"s that go
2857 * over PPP are Spanning Tree Protocol
2871 * Generate code to match a particular packet type by matching the
2872 * link-layer type field or fields in the 802.2 LLC header.
2874 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2875 * value, if <= ETHERMTU.
2877 static struct block
*
2881 struct block
*b0
, *b1
, *b2
;
2883 /* are we checking MPLS-encapsulated packets? */
2884 if (label_stack_depth
> 0) {
2888 /* FIXME add other L3 proto IDs */
2889 return gen_mpls_linktype(Q_IP
);
2891 case ETHERTYPE_IPV6
:
2893 /* FIXME add other L3 proto IDs */
2894 return gen_mpls_linktype(Q_IPV6
);
2897 bpf_error("unsupported protocol over mpls");
2903 * Are we testing PPPoE packets?
2907 * The PPPoE session header is part of the
2908 * MAC-layer payload, so all references
2909 * should be relative to the beginning of
2914 * We use Ethernet protocol types inside libpcap;
2915 * map them to the corresponding PPP protocol types.
2917 proto
= ethertype_to_ppptype(proto
);
2918 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2924 case DLT_NETANALYZER
:
2925 case DLT_NETANALYZER_TRANSPARENT
:
2926 return gen_ether_linktype(proto
);
2934 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2938 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2945 case DLT_IEEE802_11
:
2946 case DLT_PRISM_HEADER
:
2947 case DLT_IEEE802_11_RADIO_AVS
:
2948 case DLT_IEEE802_11_RADIO
:
2951 * Check that we have a data frame.
2953 b0
= gen_check_802_11_data_frame();
2956 * Now check for the specified link-layer type.
2958 b1
= gen_llc_linktype(proto
);
2966 * XXX - check for asynchronous frames, as per RFC 1103.
2968 return gen_llc_linktype(proto
);
2974 * XXX - check for LLC PDUs, as per IEEE 802.5.
2976 return gen_llc_linktype(proto
);
2980 case DLT_ATM_RFC1483
:
2982 case DLT_IP_OVER_FC
:
2983 return gen_llc_linktype(proto
);
2989 * If "is_lane" is set, check for a LANE-encapsulated
2990 * version of this protocol, otherwise check for an
2991 * LLC-encapsulated version of this protocol.
2993 * We assume LANE means Ethernet, not Token Ring.
2997 * Check that the packet doesn't begin with an
2998 * LE Control marker. (We've already generated
3001 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3006 * Now generate an Ethernet test.
3008 b1
= gen_ether_linktype(proto
);
3013 * Check for LLC encapsulation and then check the
3016 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3017 b1
= gen_llc_linktype(proto
);
3025 return gen_linux_sll_linktype(proto
);
3030 case DLT_SLIP_BSDOS
:
3033 * These types don't provide any type field; packets
3034 * are always IPv4 or IPv6.
3036 * XXX - for IPv4, check for a version number of 4, and,
3037 * for IPv6, check for a version number of 6?
3042 /* Check for a version number of 4. */
3043 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3045 case ETHERTYPE_IPV6
:
3046 /* Check for a version number of 6. */
3047 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3051 return gen_false(); /* always false */
3058 * Raw IPv4, so no type field.
3060 if (proto
== ETHERTYPE_IP
)
3061 return gen_true(); /* always true */
3063 /* Checking for something other than IPv4; always false */
3070 * Raw IPv6, so no type field.
3073 if (proto
== ETHERTYPE_IPV6
)
3074 return gen_true(); /* always true */
3077 /* Checking for something other than IPv6; always false */
3084 case DLT_PPP_SERIAL
:
3087 * We use Ethernet protocol types inside libpcap;
3088 * map them to the corresponding PPP protocol types.
3090 proto
= ethertype_to_ppptype(proto
);
3091 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3097 * We use Ethernet protocol types inside libpcap;
3098 * map them to the corresponding PPP protocol types.
3104 * Also check for Van Jacobson-compressed IP.
3105 * XXX - do this for other forms of PPP?
3107 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3108 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3110 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3115 proto
= ethertype_to_ppptype(proto
);
3116 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3126 * For DLT_NULL, the link-layer header is a 32-bit
3127 * word containing an AF_ value in *host* byte order,
3128 * and for DLT_ENC, the link-layer header begins
3129 * with a 32-bit work containing an AF_ value in
3132 * In addition, if we're reading a saved capture file,
3133 * the host byte order in the capture may not be the
3134 * same as the host byte order on this machine.
3136 * For DLT_LOOP, the link-layer header is a 32-bit
3137 * word containing an AF_ value in *network* byte order.
3139 * XXX - AF_ values may, unfortunately, be platform-
3140 * dependent; for example, FreeBSD's AF_INET6 is 24
3141 * whilst NetBSD's and OpenBSD's is 26.
3143 * This means that, when reading a capture file, just
3144 * checking for our AF_INET6 value won't work if the
3145 * capture file came from another OS.
3154 case ETHERTYPE_IPV6
:
3161 * Not a type on which we support filtering.
3162 * XXX - support those that have AF_ values
3163 * #defined on this platform, at least?
3168 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3170 * The AF_ value is in host byte order, but
3171 * the BPF interpreter will convert it to
3172 * network byte order.
3174 * If this is a save file, and it's from a
3175 * machine with the opposite byte order to
3176 * ours, we byte-swap the AF_ value.
3178 * Then we run it through "htonl()", and
3179 * generate code to compare against the result.
3181 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3182 bpf_pcap
->sf
.swapped
)
3183 proto
= SWAPLONG(proto
);
3184 proto
= htonl(proto
);
3186 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3188 #ifdef HAVE_NET_PFVAR_H
3191 * af field is host byte order in contrast to the rest of
3194 if (proto
== ETHERTYPE_IP
)
3195 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3196 BPF_B
, (bpf_int32
)AF_INET
));
3198 else if (proto
== ETHERTYPE_IPV6
)
3199 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3200 BPF_B
, (bpf_int32
)AF_INET6
));
3206 #endif /* HAVE_NET_PFVAR_H */
3209 case DLT_ARCNET_LINUX
:
3211 * XXX should we check for first fragment if the protocol
3220 case ETHERTYPE_IPV6
:
3221 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3222 (bpf_int32
)ARCTYPE_INET6
));
3226 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3227 (bpf_int32
)ARCTYPE_IP
);
3228 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3229 (bpf_int32
)ARCTYPE_IP_OLD
);
3234 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3235 (bpf_int32
)ARCTYPE_ARP
);
3236 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3237 (bpf_int32
)ARCTYPE_ARP_OLD
);
3241 case ETHERTYPE_REVARP
:
3242 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3243 (bpf_int32
)ARCTYPE_REVARP
));
3245 case ETHERTYPE_ATALK
:
3246 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3247 (bpf_int32
)ARCTYPE_ATALK
));
3254 case ETHERTYPE_ATALK
:
3264 * XXX - assumes a 2-byte Frame Relay header with
3265 * DLCI and flags. What if the address is longer?
3271 * Check for the special NLPID for IP.
3273 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3276 case ETHERTYPE_IPV6
:
3278 * Check for the special NLPID for IPv6.
3280 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3285 * Check for several OSI protocols.
3287 * Frame Relay packets typically have an OSI
3288 * NLPID at the beginning; we check for each
3291 * What we check for is the NLPID and a frame
3292 * control field of UI, i.e. 0x03 followed
3295 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3296 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3297 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3309 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3311 case DLT_JUNIPER_MFR
:
3312 case DLT_JUNIPER_MLFR
:
3313 case DLT_JUNIPER_MLPPP
:
3314 case DLT_JUNIPER_ATM1
:
3315 case DLT_JUNIPER_ATM2
:
3316 case DLT_JUNIPER_PPPOE
:
3317 case DLT_JUNIPER_PPPOE_ATM
:
3318 case DLT_JUNIPER_GGSN
:
3319 case DLT_JUNIPER_ES
:
3320 case DLT_JUNIPER_MONITOR
:
3321 case DLT_JUNIPER_SERVICES
:
3322 case DLT_JUNIPER_ETHER
:
3323 case DLT_JUNIPER_PPP
:
3324 case DLT_JUNIPER_FRELAY
:
3325 case DLT_JUNIPER_CHDLC
:
3326 case DLT_JUNIPER_VP
:
3327 case DLT_JUNIPER_ST
:
3328 case DLT_JUNIPER_ISM
:
3329 case DLT_JUNIPER_VS
:
3330 case DLT_JUNIPER_SRX_E2E
:
3331 case DLT_JUNIPER_FIBRECHANNEL
:
3332 case DLT_JUNIPER_ATM_CEMIC
:
3334 /* just lets verify the magic number for now -
3335 * on ATM we may have up to 6 different encapsulations on the wire
3336 * and need a lot of heuristics to figure out that the payload
3339 * FIXME encapsulation specific BPF_ filters
3341 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3344 return gen_ipnet_linktype(proto
);
3346 case DLT_LINUX_IRDA
:
3347 bpf_error("IrDA link-layer type filtering not implemented");
3350 bpf_error("DOCSIS link-layer type filtering not implemented");
3353 case DLT_MTP2_WITH_PHDR
:
3354 bpf_error("MTP2 link-layer type filtering not implemented");
3357 bpf_error("ERF link-layer type filtering not implemented");
3361 bpf_error("PFSYNC link-layer type filtering not implemented");
3364 case DLT_LINUX_LAPD
:
3365 bpf_error("LAPD link-layer type filtering not implemented");
3369 case DLT_USB_LINUX_MMAPPED
:
3370 bpf_error("USB link-layer type filtering not implemented");
3372 case DLT_BLUETOOTH_HCI_H4
:
3373 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3374 bpf_error("Bluetooth link-layer type filtering not implemented");
3377 case DLT_CAN_SOCKETCAN
:
3378 bpf_error("CAN link-layer type filtering not implemented");
3380 case DLT_IEEE802_15_4
:
3381 case DLT_IEEE802_15_4_LINUX
:
3382 case DLT_IEEE802_15_4_NONASK_PHY
:
3383 case DLT_IEEE802_15_4_NOFCS
:
3384 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3386 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3387 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3390 bpf_error("SITA link-layer type filtering not implemented");
3393 bpf_error("RAIF1 link-layer type filtering not implemented");
3396 bpf_error("IPMB link-layer type filtering not implemented");
3399 bpf_error("AX.25 link-layer type filtering not implemented");
3403 * All the types that have no encapsulation should either be
3404 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3405 * all packets are IP packets, or should be handled in some
3406 * special case, if none of them are (if some are and some
3407 * aren't, the lack of encapsulation is a problem, as we'd
3408 * have to find some other way of determining the packet type).
3410 * Therefore, if "off_linktype" is -1, there's an error.
3412 if (off_linktype
== (u_int
)-1)
3416 * Any type not handled above should always have an Ethernet
3417 * type at an offset of "off_linktype".
3419 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3423 * Check for an LLC SNAP packet with a given organization code and
3424 * protocol type; we check the entire contents of the 802.2 LLC and
3425 * snap headers, checking for DSAP and SSAP of SNAP and a control
3426 * field of 0x03 in the LLC header, and for the specified organization
3427 * code and protocol type in the SNAP header.
3429 static struct block
*
3430 gen_snap(orgcode
, ptype
)
3431 bpf_u_int32 orgcode
;
3434 u_char snapblock
[8];
3436 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3437 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3438 snapblock
[2] = 0x03; /* control = UI */
3439 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3440 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3441 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3442 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3443 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3444 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3448 * Generate code to match a particular packet type, for link-layer types
3449 * using 802.2 LLC headers.
3451 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3452 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3454 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3455 * value, if <= ETHERMTU. We use that to determine whether to
3456 * match the DSAP or both DSAP and LSAP or to check the OUI and
3457 * protocol ID in a SNAP header.
3459 static struct block
*
3460 gen_llc_linktype(proto
)
3464 * XXX - handle token-ring variable-length header.
3470 case LLCSAP_NETBEUI
:
3472 * XXX - should we check both the DSAP and the
3473 * SSAP, like this, or should we check just the
3474 * DSAP, as we do for other types <= ETHERMTU
3475 * (i.e., other SAP values)?
3477 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3478 ((proto
<< 8) | proto
));
3482 * XXX - are there ever SNAP frames for IPX on
3483 * non-Ethernet 802.x networks?
3485 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3486 (bpf_int32
)LLCSAP_IPX
);
3488 case ETHERTYPE_ATALK
:
3490 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3491 * SNAP packets with an organization code of
3492 * 0x080007 (Apple, for Appletalk) and a protocol
3493 * type of ETHERTYPE_ATALK (Appletalk).
3495 * XXX - check for an organization code of
3496 * encapsulated Ethernet as well?
3498 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3502 * XXX - we don't have to check for IPX 802.3
3503 * here, but should we check for the IPX Ethertype?
3505 if (proto
<= ETHERMTU
) {
3507 * This is an LLC SAP value, so check
3510 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3513 * This is an Ethernet type; we assume that it's
3514 * unlikely that it'll appear in the right place
3515 * at random, and therefore check only the
3516 * location that would hold the Ethernet type
3517 * in a SNAP frame with an organization code of
3518 * 0x000000 (encapsulated Ethernet).
3520 * XXX - if we were to check for the SNAP DSAP and
3521 * LSAP, as per XXX, and were also to check for an
3522 * organization code of 0x000000 (encapsulated
3523 * Ethernet), we'd do
3525 * return gen_snap(0x000000, proto);
3527 * here; for now, we don't, as per the above.
3528 * I don't know whether it's worth the extra CPU
3529 * time to do the right check or not.
3531 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3536 static struct block
*
3537 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3541 u_int src_off
, dst_off
;
3543 struct block
*b0
, *b1
;
3557 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3558 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3564 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3565 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3572 b0
= gen_linktype(proto
);
3573 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3579 static struct block
*
3580 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3581 struct in6_addr
*addr
;
3582 struct in6_addr
*mask
;
3584 u_int src_off
, dst_off
;
3586 struct block
*b0
, *b1
;
3601 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3602 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3608 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3609 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3616 /* this order is important */
3617 a
= (u_int32_t
*)addr
;
3618 m
= (u_int32_t
*)mask
;
3619 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3620 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3622 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3624 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3626 b0
= gen_linktype(proto
);
3632 static struct block
*
3633 gen_ehostop(eaddr
, dir
)
3634 register const u_char
*eaddr
;
3637 register struct block
*b0
, *b1
;
3641 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3644 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3647 b0
= gen_ehostop(eaddr
, Q_SRC
);
3648 b1
= gen_ehostop(eaddr
, Q_DST
);
3654 b0
= gen_ehostop(eaddr
, Q_SRC
);
3655 b1
= gen_ehostop(eaddr
, Q_DST
);
3660 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3664 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3668 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3672 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3676 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3680 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3688 * Like gen_ehostop, but for DLT_FDDI
3690 static struct block
*
3691 gen_fhostop(eaddr
, dir
)
3692 register const u_char
*eaddr
;
3695 struct block
*b0
, *b1
;
3700 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3702 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3707 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3709 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3713 b0
= gen_fhostop(eaddr
, Q_SRC
);
3714 b1
= gen_fhostop(eaddr
, Q_DST
);
3720 b0
= gen_fhostop(eaddr
, Q_SRC
);
3721 b1
= gen_fhostop(eaddr
, Q_DST
);
3726 bpf_error("'addr1' is only supported on 802.11");
3730 bpf_error("'addr2' is only supported on 802.11");
3734 bpf_error("'addr3' is only supported on 802.11");
3738 bpf_error("'addr4' is only supported on 802.11");
3742 bpf_error("'ra' is only supported on 802.11");
3746 bpf_error("'ta' is only supported on 802.11");
3754 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3756 static struct block
*
3757 gen_thostop(eaddr
, dir
)
3758 register const u_char
*eaddr
;
3761 register struct block
*b0
, *b1
;
3765 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3768 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3771 b0
= gen_thostop(eaddr
, Q_SRC
);
3772 b1
= gen_thostop(eaddr
, Q_DST
);
3778 b0
= gen_thostop(eaddr
, Q_SRC
);
3779 b1
= gen_thostop(eaddr
, Q_DST
);
3784 bpf_error("'addr1' is only supported on 802.11");
3788 bpf_error("'addr2' is only supported on 802.11");
3792 bpf_error("'addr3' is only supported on 802.11");
3796 bpf_error("'addr4' is only supported on 802.11");
3800 bpf_error("'ra' is only supported on 802.11");
3804 bpf_error("'ta' is only supported on 802.11");
3812 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3813 * various 802.11 + radio headers.
3815 static struct block
*
3816 gen_wlanhostop(eaddr
, dir
)
3817 register const u_char
*eaddr
;
3820 register struct block
*b0
, *b1
, *b2
;
3821 register struct slist
*s
;
3823 #ifdef ENABLE_WLAN_FILTERING_PATCH
3826 * We need to disable the optimizer because the optimizer is buggy
3827 * and wipes out some LD instructions generated by the below
3828 * code to validate the Frame Control bits
3831 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3838 * For control frames, there is no SA.
3840 * For management frames, SA is at an
3841 * offset of 10 from the beginning of
3844 * For data frames, SA is at an offset
3845 * of 10 from the beginning of the packet
3846 * if From DS is clear, at an offset of
3847 * 16 from the beginning of the packet
3848 * if From DS is set and To DS is clear,
3849 * and an offset of 24 from the beginning
3850 * of the packet if From DS is set and To DS
3855 * Generate the tests to be done for data frames
3858 * First, check for To DS set, i.e. check "link[1] & 0x01".
3860 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3861 b1
= new_block(JMP(BPF_JSET
));
3862 b1
->s
.k
= 0x01; /* To DS */
3866 * If To DS is set, the SA is at 24.
3868 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3872 * Now, check for To DS not set, i.e. check
3873 * "!(link[1] & 0x01)".
3875 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3876 b2
= new_block(JMP(BPF_JSET
));
3877 b2
->s
.k
= 0x01; /* To DS */
3882 * If To DS is not set, the SA is at 16.
3884 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3888 * Now OR together the last two checks. That gives
3889 * the complete set of checks for data frames with
3895 * Now check for From DS being set, and AND that with
3896 * the ORed-together checks.
3898 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3899 b1
= new_block(JMP(BPF_JSET
));
3900 b1
->s
.k
= 0x02; /* From DS */
3905 * Now check for data frames with From DS not set.
3907 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3908 b2
= new_block(JMP(BPF_JSET
));
3909 b2
->s
.k
= 0x02; /* From DS */
3914 * If From DS isn't set, the SA is at 10.
3916 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3920 * Now OR together the checks for data frames with
3921 * From DS not set and for data frames with From DS
3922 * set; that gives the checks done for data frames.
3927 * Now check for a data frame.
3928 * I.e, check "link[0] & 0x08".
3930 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3931 b1
= new_block(JMP(BPF_JSET
));
3936 * AND that with the checks done for data frames.
3941 * If the high-order bit of the type value is 0, this
3942 * is a management frame.
3943 * I.e, check "!(link[0] & 0x08)".
3945 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3946 b2
= new_block(JMP(BPF_JSET
));
3952 * For management frames, the SA is at 10.
3954 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3958 * OR that with the checks done for data frames.
3959 * That gives the checks done for management and
3965 * If the low-order bit of the type value is 1,
3966 * this is either a control frame or a frame
3967 * with a reserved type, and thus not a
3970 * I.e., check "!(link[0] & 0x04)".
3972 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3973 b1
= new_block(JMP(BPF_JSET
));
3979 * AND that with the checks for data and management
3989 * For control frames, there is no DA.
3991 * For management frames, DA is at an
3992 * offset of 4 from the beginning of
3995 * For data frames, DA is at an offset
3996 * of 4 from the beginning of the packet
3997 * if To DS is clear and at an offset of
3998 * 16 from the beginning of the packet
4003 * Generate the tests to be done for data frames.
4005 * First, check for To DS set, i.e. "link[1] & 0x01".
4007 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4008 b1
= new_block(JMP(BPF_JSET
));
4009 b1
->s
.k
= 0x01; /* To DS */
4013 * If To DS is set, the DA is at 16.
4015 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4019 * Now, check for To DS not set, i.e. check
4020 * "!(link[1] & 0x01)".
4022 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4023 b2
= new_block(JMP(BPF_JSET
));
4024 b2
->s
.k
= 0x01; /* To DS */
4029 * If To DS is not set, the DA is at 4.
4031 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4035 * Now OR together the last two checks. That gives
4036 * the complete set of checks for data frames.
4041 * Now check for a data frame.
4042 * I.e, check "link[0] & 0x08".
4044 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4045 b1
= new_block(JMP(BPF_JSET
));
4050 * AND that with the checks done for data frames.
4055 * If the high-order bit of the type value is 0, this
4056 * is a management frame.
4057 * I.e, check "!(link[0] & 0x08)".
4059 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4060 b2
= new_block(JMP(BPF_JSET
));
4066 * For management frames, the DA is at 4.
4068 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4072 * OR that with the checks done for data frames.
4073 * That gives the checks done for management and
4079 * If the low-order bit of the type value is 1,
4080 * this is either a control frame or a frame
4081 * with a reserved type, and thus not a
4084 * I.e., check "!(link[0] & 0x04)".
4086 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4087 b1
= new_block(JMP(BPF_JSET
));
4093 * AND that with the checks for data and management
4101 * Not present in management frames; addr1 in other
4106 * If the high-order bit of the type value is 0, this
4107 * is a management frame.
4108 * I.e, check "(link[0] & 0x08)".
4110 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4111 b1
= new_block(JMP(BPF_JSET
));
4118 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4121 * AND that with the check of addr1.
4128 * Not present in management frames; addr2, if present,
4133 * Not present in CTS or ACK control frames.
4135 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4136 IEEE80211_FC0_TYPE_MASK
);
4138 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4139 IEEE80211_FC0_SUBTYPE_MASK
);
4141 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4142 IEEE80211_FC0_SUBTYPE_MASK
);
4148 * If the high-order bit of the type value is 0, this
4149 * is a management frame.
4150 * I.e, check "(link[0] & 0x08)".
4152 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4153 b1
= new_block(JMP(BPF_JSET
));
4158 * AND that with the check for frames other than
4159 * CTS and ACK frames.
4166 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4171 * XXX - add BSSID keyword?
4174 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4178 * Not present in CTS or ACK control frames.
4180 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4181 IEEE80211_FC0_TYPE_MASK
);
4183 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4184 IEEE80211_FC0_SUBTYPE_MASK
);
4186 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4187 IEEE80211_FC0_SUBTYPE_MASK
);
4191 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4197 * Not present in control frames.
4199 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4200 IEEE80211_FC0_TYPE_MASK
);
4202 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4208 * Present only if the direction mask has both "From DS"
4209 * and "To DS" set. Neither control frames nor management
4210 * frames should have both of those set, so we don't
4211 * check the frame type.
4213 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4214 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4215 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4220 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4221 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4227 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4228 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4237 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4238 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4239 * as the RFC states.)
4241 static struct block
*
4242 gen_ipfchostop(eaddr
, dir
)
4243 register const u_char
*eaddr
;
4246 register struct block
*b0
, *b1
;
4250 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4253 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4256 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4257 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4263 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4264 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4269 bpf_error("'addr1' is only supported on 802.11");
4273 bpf_error("'addr2' is only supported on 802.11");
4277 bpf_error("'addr3' is only supported on 802.11");
4281 bpf_error("'addr4' is only supported on 802.11");
4285 bpf_error("'ra' is only supported on 802.11");
4289 bpf_error("'ta' is only supported on 802.11");
4297 * This is quite tricky because there may be pad bytes in front of the
4298 * DECNET header, and then there are two possible data packet formats that
4299 * carry both src and dst addresses, plus 5 packet types in a format that
4300 * carries only the src node, plus 2 types that use a different format and
4301 * also carry just the src node.
4305 * Instead of doing those all right, we just look for data packets with
4306 * 0 or 1 bytes of padding. If you want to look at other packets, that
4307 * will require a lot more hacking.
4309 * To add support for filtering on DECNET "areas" (network numbers)
4310 * one would want to add a "mask" argument to this routine. That would
4311 * make the filter even more inefficient, although one could be clever
4312 * and not generate masking instructions if the mask is 0xFFFF.
4314 static struct block
*
4315 gen_dnhostop(addr
, dir
)
4319 struct block
*b0
, *b1
, *b2
, *tmp
;
4320 u_int offset_lh
; /* offset if long header is received */
4321 u_int offset_sh
; /* offset if short header is received */
4326 offset_sh
= 1; /* follows flags */
4327 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4331 offset_sh
= 3; /* follows flags, dstnode */
4332 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4336 /* Inefficient because we do our Calvinball dance twice */
4337 b0
= gen_dnhostop(addr
, Q_SRC
);
4338 b1
= gen_dnhostop(addr
, Q_DST
);
4344 /* Inefficient because we do our Calvinball dance twice */
4345 b0
= gen_dnhostop(addr
, Q_SRC
);
4346 b1
= gen_dnhostop(addr
, Q_DST
);
4351 bpf_error("ISO host filtering not implemented");
4356 b0
= gen_linktype(ETHERTYPE_DN
);
4357 /* Check for pad = 1, long header case */
4358 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4359 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4360 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4361 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4363 /* Check for pad = 0, long header case */
4364 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4365 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4368 /* Check for pad = 1, short header case */
4369 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4370 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4371 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4374 /* Check for pad = 0, short header case */
4375 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4376 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4380 /* Combine with test for linktype */
4386 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4387 * test the bottom-of-stack bit, and then check the version number
4388 * field in the IP header.
4390 static struct block
*
4391 gen_mpls_linktype(proto
)
4394 struct block
*b0
, *b1
;
4399 /* match the bottom-of-stack bit */
4400 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4401 /* match the IPv4 version number */
4402 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4407 /* match the bottom-of-stack bit */
4408 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4409 /* match the IPv4 version number */
4410 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4419 static struct block
*
4420 gen_host(addr
, mask
, proto
, dir
, type
)
4427 struct block
*b0
, *b1
;
4428 const char *typestr
;
4438 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4440 * Only check for non-IPv4 addresses if we're not
4441 * checking MPLS-encapsulated packets.
4443 if (label_stack_depth
== 0) {
4444 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4446 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4452 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4455 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4458 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4461 bpf_error("'tcp' modifier applied to %s", typestr
);
4464 bpf_error("'sctp' modifier applied to %s", typestr
);
4467 bpf_error("'udp' modifier applied to %s", typestr
);
4470 bpf_error("'icmp' modifier applied to %s", typestr
);
4473 bpf_error("'igmp' modifier applied to %s", typestr
);
4476 bpf_error("'igrp' modifier applied to %s", typestr
);
4479 bpf_error("'pim' modifier applied to %s", typestr
);
4482 bpf_error("'vrrp' modifier applied to %s", typestr
);
4485 bpf_error("'carp' modifier applied to %s", typestr
);
4488 bpf_error("ATALK host filtering not implemented");
4491 bpf_error("AARP host filtering not implemented");
4494 return gen_dnhostop(addr
, dir
);
4497 bpf_error("SCA host filtering not implemented");
4500 bpf_error("LAT host filtering not implemented");
4503 bpf_error("MOPDL host filtering not implemented");
4506 bpf_error("MOPRC host filtering not implemented");
4510 bpf_error("'ip6' modifier applied to ip host");
4513 bpf_error("'icmp6' modifier applied to %s", typestr
);
4517 bpf_error("'ah' modifier applied to %s", typestr
);
4520 bpf_error("'esp' modifier applied to %s", typestr
);
4523 bpf_error("ISO host filtering not implemented");
4526 bpf_error("'esis' modifier applied to %s", typestr
);
4529 bpf_error("'isis' modifier applied to %s", typestr
);
4532 bpf_error("'clnp' modifier applied to %s", typestr
);
4535 bpf_error("'stp' modifier applied to %s", typestr
);
4538 bpf_error("IPX host filtering not implemented");
4541 bpf_error("'netbeui' modifier applied to %s", typestr
);
4544 bpf_error("'radio' modifier applied to %s", typestr
);
4553 static struct block
*
4554 gen_host6(addr
, mask
, proto
, dir
, type
)
4555 struct in6_addr
*addr
;
4556 struct in6_addr
*mask
;
4561 const char *typestr
;
4571 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4574 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4577 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4580 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4583 bpf_error("'sctp' modifier applied to %s", typestr
);
4586 bpf_error("'tcp' modifier applied to %s", typestr
);
4589 bpf_error("'udp' modifier applied to %s", typestr
);
4592 bpf_error("'icmp' modifier applied to %s", typestr
);
4595 bpf_error("'igmp' modifier applied to %s", typestr
);
4598 bpf_error("'igrp' modifier applied to %s", typestr
);
4601 bpf_error("'pim' modifier applied to %s", typestr
);
4604 bpf_error("'vrrp' modifier applied to %s", typestr
);
4607 bpf_error("'carp' modifier applied to %s", typestr
);
4610 bpf_error("ATALK host filtering not implemented");
4613 bpf_error("AARP host filtering not implemented");
4616 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4619 bpf_error("SCA host filtering not implemented");
4622 bpf_error("LAT host filtering not implemented");
4625 bpf_error("MOPDL host filtering not implemented");
4628 bpf_error("MOPRC host filtering not implemented");
4631 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4634 bpf_error("'icmp6' modifier applied to %s", typestr
);
4637 bpf_error("'ah' modifier applied to %s", typestr
);
4640 bpf_error("'esp' modifier applied to %s", typestr
);
4643 bpf_error("ISO host filtering not implemented");
4646 bpf_error("'esis' modifier applied to %s", typestr
);
4649 bpf_error("'isis' modifier applied to %s", typestr
);
4652 bpf_error("'clnp' modifier applied to %s", typestr
);
4655 bpf_error("'stp' modifier applied to %s", typestr
);
4658 bpf_error("IPX host filtering not implemented");
4661 bpf_error("'netbeui' modifier applied to %s", typestr
);
4664 bpf_error("'radio' modifier applied to %s", typestr
);
4674 static struct block
*
4675 gen_gateway(eaddr
, alist
, proto
, dir
)
4676 const u_char
*eaddr
;
4677 bpf_u_int32
**alist
;
4681 struct block
*b0
, *b1
, *tmp
;
4684 bpf_error("direction applied to 'gateway'");
4693 case DLT_NETANALYZER
:
4694 case DLT_NETANALYZER_TRANSPARENT
:
4695 b0
= gen_ehostop(eaddr
, Q_OR
);
4698 b0
= gen_fhostop(eaddr
, Q_OR
);
4701 b0
= gen_thostop(eaddr
, Q_OR
);
4703 case DLT_IEEE802_11
:
4704 case DLT_PRISM_HEADER
:
4705 case DLT_IEEE802_11_RADIO_AVS
:
4706 case DLT_IEEE802_11_RADIO
:
4708 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4713 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4715 * Check that the packet doesn't begin with an
4716 * LE Control marker. (We've already generated
4719 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4724 * Now check the MAC address.
4726 b0
= gen_ehostop(eaddr
, Q_OR
);
4729 case DLT_IP_OVER_FC
:
4730 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4734 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4736 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4738 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4747 bpf_error("illegal modifier of 'gateway'");
4753 gen_proto_abbrev(proto
)
4762 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4764 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4770 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4772 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4778 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4780 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4786 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4789 #ifndef IPPROTO_IGMP
4790 #define IPPROTO_IGMP 2
4794 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4797 #ifndef IPPROTO_IGRP
4798 #define IPPROTO_IGRP 9
4801 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4805 #define IPPROTO_PIM 103
4809 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4811 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4816 #ifndef IPPROTO_VRRP
4817 #define IPPROTO_VRRP 112
4821 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4824 #ifndef IPPROTO_CARP
4825 #define IPPROTO_CARP 112
4829 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4833 b1
= gen_linktype(ETHERTYPE_IP
);
4837 b1
= gen_linktype(ETHERTYPE_ARP
);
4841 b1
= gen_linktype(ETHERTYPE_REVARP
);
4845 bpf_error("link layer applied in wrong context");
4848 b1
= gen_linktype(ETHERTYPE_ATALK
);
4852 b1
= gen_linktype(ETHERTYPE_AARP
);
4856 b1
= gen_linktype(ETHERTYPE_DN
);
4860 b1
= gen_linktype(ETHERTYPE_SCA
);
4864 b1
= gen_linktype(ETHERTYPE_LAT
);
4868 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4872 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4877 b1
= gen_linktype(ETHERTYPE_IPV6
);
4880 #ifndef IPPROTO_ICMPV6
4881 #define IPPROTO_ICMPV6 58
4884 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4889 #define IPPROTO_AH 51
4892 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4894 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4900 #define IPPROTO_ESP 50
4903 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4905 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4911 b1
= gen_linktype(LLCSAP_ISONS
);
4915 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4919 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4922 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4923 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4924 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4926 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4928 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4930 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4934 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4935 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4936 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4938 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4940 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4942 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4946 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4947 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4948 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4950 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4955 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4956 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4961 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4962 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4964 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4966 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4971 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4972 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4977 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4978 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4983 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4987 b1
= gen_linktype(LLCSAP_8021D
);
4991 b1
= gen_linktype(LLCSAP_IPX
);
4995 b1
= gen_linktype(LLCSAP_NETBEUI
);
4999 bpf_error("'radio' is not a valid protocol type");
5007 static struct block
*
5013 /* not IPv4 frag other than the first frag */
5014 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5015 b
= new_block(JMP(BPF_JSET
));
5024 * Generate a comparison to a port value in the transport-layer header
5025 * at the specified offset from the beginning of that header.
5027 * XXX - this handles a variable-length prefix preceding the link-layer
5028 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5029 * variable-length link-layer headers (such as Token Ring or 802.11
5032 static struct block
*
5033 gen_portatom(off
, v
)
5037 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5041 static struct block
*
5042 gen_portatom6(off
, v
)
5046 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5051 gen_portop(port
, proto
, dir
)
5052 int port
, proto
, dir
;
5054 struct block
*b0
, *b1
, *tmp
;
5056 /* ip proto 'proto' and not a fragment other than the first fragment */
5057 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5063 b1
= gen_portatom(0, (bpf_int32
)port
);
5067 b1
= gen_portatom(2, (bpf_int32
)port
);
5072 tmp
= gen_portatom(0, (bpf_int32
)port
);
5073 b1
= gen_portatom(2, (bpf_int32
)port
);
5078 tmp
= gen_portatom(0, (bpf_int32
)port
);
5079 b1
= gen_portatom(2, (bpf_int32
)port
);
5091 static struct block
*
5092 gen_port(port
, ip_proto
, dir
)
5097 struct block
*b0
, *b1
, *tmp
;
5102 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5103 * not LLC encapsulation with LLCSAP_IP.
5105 * For IEEE 802 networks - which includes 802.5 token ring
5106 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5107 * says that SNAP encapsulation is used, not LLC encapsulation
5110 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5111 * RFC 2225 say that SNAP encapsulation is used, not LLC
5112 * encapsulation with LLCSAP_IP.
5114 * So we always check for ETHERTYPE_IP.
5116 b0
= gen_linktype(ETHERTYPE_IP
);
5122 b1
= gen_portop(port
, ip_proto
, dir
);
5126 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5127 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5129 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5142 gen_portop6(port
, proto
, dir
)
5143 int port
, proto
, dir
;
5145 struct block
*b0
, *b1
, *tmp
;
5147 /* ip6 proto 'proto' */
5148 /* XXX - catch the first fragment of a fragmented packet? */
5149 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5153 b1
= gen_portatom6(0, (bpf_int32
)port
);
5157 b1
= gen_portatom6(2, (bpf_int32
)port
);
5162 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5163 b1
= gen_portatom6(2, (bpf_int32
)port
);
5168 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5169 b1
= gen_portatom6(2, (bpf_int32
)port
);
5181 static struct block
*
5182 gen_port6(port
, ip_proto
, dir
)
5187 struct block
*b0
, *b1
, *tmp
;
5189 /* link proto ip6 */
5190 b0
= gen_linktype(ETHERTYPE_IPV6
);
5196 b1
= gen_portop6(port
, ip_proto
, dir
);
5200 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5201 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5203 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5215 /* gen_portrange code */
5216 static struct block
*
5217 gen_portrangeatom(off
, v1
, v2
)
5221 struct block
*b1
, *b2
;
5225 * Reverse the order of the ports, so v1 is the lower one.
5234 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5235 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5243 gen_portrangeop(port1
, port2
, proto
, dir
)
5248 struct block
*b0
, *b1
, *tmp
;
5250 /* ip proto 'proto' and not a fragment other than the first fragment */
5251 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5257 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5261 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5266 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5267 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5272 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5273 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5285 static struct block
*
5286 gen_portrange(port1
, port2
, ip_proto
, dir
)
5291 struct block
*b0
, *b1
, *tmp
;
5294 b0
= gen_linktype(ETHERTYPE_IP
);
5300 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5304 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5305 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5307 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5319 static struct block
*
5320 gen_portrangeatom6(off
, v1
, v2
)
5324 struct block
*b1
, *b2
;
5328 * Reverse the order of the ports, so v1 is the lower one.
5337 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5338 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5346 gen_portrangeop6(port1
, port2
, proto
, dir
)
5351 struct block
*b0
, *b1
, *tmp
;
5353 /* ip6 proto 'proto' */
5354 /* XXX - catch the first fragment of a fragmented packet? */
5355 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5359 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5363 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5368 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5369 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5374 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5375 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5387 static struct block
*
5388 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5393 struct block
*b0
, *b1
, *tmp
;
5395 /* link proto ip6 */
5396 b0
= gen_linktype(ETHERTYPE_IPV6
);
5402 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5406 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5407 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5409 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5422 lookup_proto(name
, proto
)
5423 register const char *name
;
5433 v
= pcap_nametoproto(name
);
5434 if (v
== PROTO_UNDEF
)
5435 bpf_error("unknown ip proto '%s'", name
);
5439 /* XXX should look up h/w protocol type based on linktype */
5440 v
= pcap_nametoeproto(name
);
5441 if (v
== PROTO_UNDEF
) {
5442 v
= pcap_nametollc(name
);
5443 if (v
== PROTO_UNDEF
)
5444 bpf_error("unknown ether proto '%s'", name
);
5449 if (strcmp(name
, "esis") == 0)
5451 else if (strcmp(name
, "isis") == 0)
5453 else if (strcmp(name
, "clnp") == 0)
5456 bpf_error("unknown osi proto '%s'", name
);
5476 static struct block
*
5477 gen_protochain(v
, proto
, dir
)
5482 #ifdef NO_PROTOCHAIN
5483 return gen_proto(v
, proto
, dir
);
5485 struct block
*b0
, *b
;
5486 struct slist
*s
[100];
5487 int fix2
, fix3
, fix4
, fix5
;
5488 int ahcheck
, again
, end
;
5490 int reg2
= alloc_reg();
5492 memset(s
, 0, sizeof(s
));
5493 fix2
= fix3
= fix4
= fix5
= 0;
5500 b0
= gen_protochain(v
, Q_IP
, dir
);
5501 b
= gen_protochain(v
, Q_IPV6
, dir
);
5505 bpf_error("bad protocol applied for 'protochain'");
5510 * We don't handle variable-length prefixes before the link-layer
5511 * header, or variable-length link-layer headers, here yet.
5512 * We might want to add BPF instructions to do the protochain
5513 * work, to simplify that and, on platforms that have a BPF
5514 * interpreter with the new instructions, let the filtering
5515 * be done in the kernel. (We already require a modified BPF
5516 * engine to do the protochain stuff, to support backward
5517 * branches, and backward branch support is unlikely to appear
5518 * in kernel BPF engines.)
5522 case DLT_IEEE802_11
:
5523 case DLT_PRISM_HEADER
:
5524 case DLT_IEEE802_11_RADIO_AVS
:
5525 case DLT_IEEE802_11_RADIO
:
5527 bpf_error("'protochain' not supported with 802.11");
5530 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5533 * s[0] is a dummy entry to protect other BPF insn from damage
5534 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5535 * hard to find interdependency made by jump table fixup.
5538 s
[i
] = new_stmt(0); /*dummy*/
5543 b0
= gen_linktype(ETHERTYPE_IP
);
5546 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5547 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5549 /* X = ip->ip_hl << 2 */
5550 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5551 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5556 b0
= gen_linktype(ETHERTYPE_IPV6
);
5558 /* A = ip6->ip_nxt */
5559 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5560 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5562 /* X = sizeof(struct ip6_hdr) */
5563 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5569 bpf_error("unsupported proto to gen_protochain");
5573 /* again: if (A == v) goto end; else fall through; */
5575 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5577 s
[i
]->s
.jt
= NULL
; /*later*/
5578 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5582 #ifndef IPPROTO_NONE
5583 #define IPPROTO_NONE 59
5585 /* if (A == IPPROTO_NONE) goto end */
5586 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5587 s
[i
]->s
.jt
= NULL
; /*later*/
5588 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5589 s
[i
]->s
.k
= IPPROTO_NONE
;
5590 s
[fix5
]->s
.jf
= s
[i
];
5595 if (proto
== Q_IPV6
) {
5596 int v6start
, v6end
, v6advance
, j
;
5599 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5600 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5601 s
[i
]->s
.jt
= NULL
; /*later*/
5602 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5603 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5604 s
[fix2
]->s
.jf
= s
[i
];
5606 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5607 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5608 s
[i
]->s
.jt
= NULL
; /*later*/
5609 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5610 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5612 /* if (A == IPPROTO_ROUTING) goto v6advance */
5613 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5614 s
[i
]->s
.jt
= NULL
; /*later*/
5615 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5616 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5618 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5619 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5620 s
[i
]->s
.jt
= NULL
; /*later*/
5621 s
[i
]->s
.jf
= NULL
; /*later*/
5622 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5632 * A = P[X + packet head];
5633 * X = X + (P[X + packet head + 1] + 1) * 8;
5635 /* A = P[X + packet head] */
5636 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5637 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5640 s
[i
] = new_stmt(BPF_ST
);
5643 /* A = P[X + packet head + 1]; */
5644 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5645 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 1;
5648 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5652 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5656 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5660 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5663 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5667 /* goto again; (must use BPF_JA for backward jump) */
5668 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5669 s
[i
]->s
.k
= again
- i
- 1;
5670 s
[i
- 1]->s
.jf
= s
[i
];
5674 for (j
= v6start
; j
<= v6end
; j
++)
5675 s
[j
]->s
.jt
= s
[v6advance
];
5680 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5682 s
[fix2
]->s
.jf
= s
[i
];
5688 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5689 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5690 s
[i
]->s
.jt
= NULL
; /*later*/
5691 s
[i
]->s
.jf
= NULL
; /*later*/
5692 s
[i
]->s
.k
= IPPROTO_AH
;
5694 s
[fix3
]->s
.jf
= s
[ahcheck
];
5701 * X = X + (P[X + 1] + 2) * 4;
5704 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5706 /* A = P[X + packet head]; */
5707 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5708 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5711 s
[i
] = new_stmt(BPF_ST
);
5715 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5718 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5722 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5724 /* A = P[X + packet head] */
5725 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5726 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5729 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5733 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5737 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5740 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5744 /* goto again; (must use BPF_JA for backward jump) */
5745 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5746 s
[i
]->s
.k
= again
- i
- 1;
5751 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5753 s
[fix2
]->s
.jt
= s
[end
];
5754 s
[fix4
]->s
.jf
= s
[end
];
5755 s
[fix5
]->s
.jt
= s
[end
];
5762 for (i
= 0; i
< max
- 1; i
++)
5763 s
[i
]->next
= s
[i
+ 1];
5764 s
[max
- 1]->next
= NULL
;
5769 b
= new_block(JMP(BPF_JEQ
));
5770 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5780 static struct block
*
5781 gen_check_802_11_data_frame()
5784 struct block
*b0
, *b1
;
5787 * A data frame has the 0x08 bit (b3) in the frame control field set
5788 * and the 0x04 bit (b2) clear.
5790 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5791 b0
= new_block(JMP(BPF_JSET
));
5795 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5796 b1
= new_block(JMP(BPF_JSET
));
5807 * Generate code that checks whether the packet is a packet for protocol
5808 * <proto> and whether the type field in that protocol's header has
5809 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5810 * IP packet and checks the protocol number in the IP header against <v>.
5812 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5813 * against Q_IP and Q_IPV6.
5815 static struct block
*
5816 gen_proto(v
, proto
, dir
)
5821 struct block
*b0
, *b1
;
5828 if (dir
!= Q_DEFAULT
)
5829 bpf_error("direction applied to 'proto'");
5834 b0
= gen_proto(v
, Q_IP
, dir
);
5835 b1
= gen_proto(v
, Q_IPV6
, dir
);
5843 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5844 * not LLC encapsulation with LLCSAP_IP.
5846 * For IEEE 802 networks - which includes 802.5 token ring
5847 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5848 * says that SNAP encapsulation is used, not LLC encapsulation
5851 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5852 * RFC 2225 say that SNAP encapsulation is used, not LLC
5853 * encapsulation with LLCSAP_IP.
5855 * So we always check for ETHERTYPE_IP.
5857 b0
= gen_linktype(ETHERTYPE_IP
);
5859 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5861 b1
= gen_protochain(v
, Q_IP
);
5871 * Frame Relay packets typically have an OSI
5872 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5873 * generates code to check for all the OSI
5874 * NLPIDs, so calling it and then adding a check
5875 * for the particular NLPID for which we're
5876 * looking is bogus, as we can just check for
5879 * What we check for is the NLPID and a frame
5880 * control field value of UI, i.e. 0x03 followed
5883 * XXX - assumes a 2-byte Frame Relay header with
5884 * DLCI and flags. What if the address is longer?
5886 * XXX - what about SNAP-encapsulated frames?
5888 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5894 * Cisco uses an Ethertype lookalike - for OSI,
5897 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5898 /* OSI in C-HDLC is stuffed with a fudge byte */
5899 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5904 b0
= gen_linktype(LLCSAP_ISONS
);
5905 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5911 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5913 * 4 is the offset of the PDU type relative to the IS-IS
5916 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5921 bpf_error("arp does not encapsulate another protocol");
5925 bpf_error("rarp does not encapsulate another protocol");
5929 bpf_error("atalk encapsulation is not specifiable");
5933 bpf_error("decnet encapsulation is not specifiable");
5937 bpf_error("sca does not encapsulate another protocol");
5941 bpf_error("lat does not encapsulate another protocol");
5945 bpf_error("moprc does not encapsulate another protocol");
5949 bpf_error("mopdl does not encapsulate another protocol");
5953 return gen_linktype(v
);
5956 bpf_error("'udp proto' is bogus");
5960 bpf_error("'tcp proto' is bogus");
5964 bpf_error("'sctp proto' is bogus");
5968 bpf_error("'icmp proto' is bogus");
5972 bpf_error("'igmp proto' is bogus");
5976 bpf_error("'igrp proto' is bogus");
5980 bpf_error("'pim proto' is bogus");
5984 bpf_error("'vrrp proto' is bogus");
5988 bpf_error("'carp proto' is bogus");
5993 b0
= gen_linktype(ETHERTYPE_IPV6
);
5996 * Also check for a fragment header before the final
5999 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6000 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
6002 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
6005 b1
= gen_protochain(v
, Q_IPV6
);
6011 bpf_error("'icmp6 proto' is bogus");
6015 bpf_error("'ah proto' is bogus");
6018 bpf_error("'ah proto' is bogus");
6021 bpf_error("'stp proto' is bogus");
6024 bpf_error("'ipx proto' is bogus");
6027 bpf_error("'netbeui proto' is bogus");
6030 bpf_error("'radio proto' is bogus");
6041 register const char *name
;
6044 int proto
= q
.proto
;
6048 bpf_u_int32 mask
, addr
;
6050 bpf_u_int32
**alist
;
6053 struct sockaddr_in
*sin4
;
6054 struct sockaddr_in6
*sin6
;
6055 struct addrinfo
*res
, *res0
;
6056 struct in6_addr mask128
;
6058 struct block
*b
, *tmp
;
6059 int port
, real_proto
;
6065 addr
= pcap_nametonetaddr(name
);
6067 bpf_error("unknown network '%s'", name
);
6068 /* Left justify network addr and calculate its network mask */
6070 while (addr
&& (addr
& 0xff000000) == 0) {
6074 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6078 if (proto
== Q_LINK
) {
6082 case DLT_NETANALYZER
:
6083 case DLT_NETANALYZER_TRANSPARENT
:
6084 eaddr
= pcap_ether_hostton(name
);
6087 "unknown ether host '%s'", name
);
6088 b
= gen_ehostop(eaddr
, dir
);
6093 eaddr
= pcap_ether_hostton(name
);
6096 "unknown FDDI host '%s'", name
);
6097 b
= gen_fhostop(eaddr
, dir
);
6102 eaddr
= pcap_ether_hostton(name
);
6105 "unknown token ring host '%s'", name
);
6106 b
= gen_thostop(eaddr
, dir
);
6110 case DLT_IEEE802_11
:
6111 case DLT_PRISM_HEADER
:
6112 case DLT_IEEE802_11_RADIO_AVS
:
6113 case DLT_IEEE802_11_RADIO
:
6115 eaddr
= pcap_ether_hostton(name
);
6118 "unknown 802.11 host '%s'", name
);
6119 b
= gen_wlanhostop(eaddr
, dir
);
6123 case DLT_IP_OVER_FC
:
6124 eaddr
= pcap_ether_hostton(name
);
6127 "unknown Fibre Channel host '%s'", name
);
6128 b
= gen_ipfchostop(eaddr
, dir
);
6137 * Check that the packet doesn't begin
6138 * with an LE Control marker. (We've
6139 * already generated a test for LANE.)
6141 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6145 eaddr
= pcap_ether_hostton(name
);
6148 "unknown ether host '%s'", name
);
6149 b
= gen_ehostop(eaddr
, dir
);
6155 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6156 } else if (proto
== Q_DECNET
) {
6157 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6159 * I don't think DECNET hosts can be multihomed, so
6160 * there is no need to build up a list of addresses
6162 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6165 alist
= pcap_nametoaddr(name
);
6166 if (alist
== NULL
|| *alist
== NULL
)
6167 bpf_error("unknown host '%s'", name
);
6169 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6171 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6173 tmp
= gen_host(**alist
++, 0xffffffff,
6174 tproto
, dir
, q
.addr
);
6180 memset(&mask128
, 0xff, sizeof(mask128
));
6181 res0
= res
= pcap_nametoaddrinfo(name
);
6183 bpf_error("unknown host '%s'", name
);
6186 tproto
= tproto6
= proto
;
6187 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6191 for (res
= res0
; res
; res
= res
->ai_next
) {
6192 switch (res
->ai_family
) {
6194 if (tproto
== Q_IPV6
)
6197 sin4
= (struct sockaddr_in
*)
6199 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6200 0xffffffff, tproto
, dir
, q
.addr
);
6203 if (tproto6
== Q_IP
)
6206 sin6
= (struct sockaddr_in6
*)
6208 tmp
= gen_host6(&sin6
->sin6_addr
,
6209 &mask128
, tproto6
, dir
, q
.addr
);
6221 bpf_error("unknown host '%s'%s", name
,
6222 (proto
== Q_DEFAULT
)
6224 : " for specified address family");
6231 if (proto
!= Q_DEFAULT
&&
6232 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6233 bpf_error("illegal qualifier of 'port'");
6234 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6235 bpf_error("unknown port '%s'", name
);
6236 if (proto
== Q_UDP
) {
6237 if (real_proto
== IPPROTO_TCP
)
6238 bpf_error("port '%s' is tcp", name
);
6239 else if (real_proto
== IPPROTO_SCTP
)
6240 bpf_error("port '%s' is sctp", name
);
6242 /* override PROTO_UNDEF */
6243 real_proto
= IPPROTO_UDP
;
6245 if (proto
== Q_TCP
) {
6246 if (real_proto
== IPPROTO_UDP
)
6247 bpf_error("port '%s' is udp", name
);
6249 else if (real_proto
== IPPROTO_SCTP
)
6250 bpf_error("port '%s' is sctp", name
);
6252 /* override PROTO_UNDEF */
6253 real_proto
= IPPROTO_TCP
;
6255 if (proto
== Q_SCTP
) {
6256 if (real_proto
== IPPROTO_UDP
)
6257 bpf_error("port '%s' is udp", name
);
6259 else if (real_proto
== IPPROTO_TCP
)
6260 bpf_error("port '%s' is tcp", name
);
6262 /* override PROTO_UNDEF */
6263 real_proto
= IPPROTO_SCTP
;
6266 bpf_error("illegal port number %d < 0", port
);
6268 bpf_error("illegal port number %d > 65535", port
);
6270 return gen_port(port
, real_proto
, dir
);
6272 b
= gen_port(port
, real_proto
, dir
);
6273 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6278 if (proto
!= Q_DEFAULT
&&
6279 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6280 bpf_error("illegal qualifier of 'portrange'");
6281 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6282 bpf_error("unknown port in range '%s'", name
);
6283 if (proto
== Q_UDP
) {
6284 if (real_proto
== IPPROTO_TCP
)
6285 bpf_error("port in range '%s' is tcp", name
);
6286 else if (real_proto
== IPPROTO_SCTP
)
6287 bpf_error("port in range '%s' is sctp", name
);
6289 /* override PROTO_UNDEF */
6290 real_proto
= IPPROTO_UDP
;
6292 if (proto
== Q_TCP
) {
6293 if (real_proto
== IPPROTO_UDP
)
6294 bpf_error("port in range '%s' is udp", name
);
6295 else if (real_proto
== IPPROTO_SCTP
)
6296 bpf_error("port in range '%s' is sctp", name
);
6298 /* override PROTO_UNDEF */
6299 real_proto
= IPPROTO_TCP
;
6301 if (proto
== Q_SCTP
) {
6302 if (real_proto
== IPPROTO_UDP
)
6303 bpf_error("port in range '%s' is udp", name
);
6304 else if (real_proto
== IPPROTO_TCP
)
6305 bpf_error("port in range '%s' is tcp", name
);
6307 /* override PROTO_UNDEF */
6308 real_proto
= IPPROTO_SCTP
;
6311 bpf_error("illegal port number %d < 0", port1
);
6313 bpf_error("illegal port number %d > 65535", port1
);
6315 bpf_error("illegal port number %d < 0", port2
);
6317 bpf_error("illegal port number %d > 65535", port2
);
6320 return gen_portrange(port1
, port2
, real_proto
, dir
);
6322 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6323 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6329 eaddr
= pcap_ether_hostton(name
);
6331 bpf_error("unknown ether host: %s", name
);
6333 alist
= pcap_nametoaddr(name
);
6334 if (alist
== NULL
|| *alist
== NULL
)
6335 bpf_error("unknown host '%s'", name
);
6336 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6340 bpf_error("'gateway' not supported in this configuration");
6344 real_proto
= lookup_proto(name
, proto
);
6345 if (real_proto
>= 0)
6346 return gen_proto(real_proto
, proto
, dir
);
6348 bpf_error("unknown protocol: %s", name
);
6351 real_proto
= lookup_proto(name
, proto
);
6352 if (real_proto
>= 0)
6353 return gen_protochain(real_proto
, proto
, dir
);
6355 bpf_error("unknown protocol: %s", name
);
6366 gen_mcode(s1
, s2
, masklen
, q
)
6367 register const char *s1
, *s2
;
6368 register int masklen
;
6371 register int nlen
, mlen
;
6374 nlen
= __pcap_atoin(s1
, &n
);
6375 /* Promote short ipaddr */
6379 mlen
= __pcap_atoin(s2
, &m
);
6380 /* Promote short ipaddr */
6383 bpf_error("non-network bits set in \"%s mask %s\"",
6386 /* Convert mask len to mask */
6388 bpf_error("mask length must be <= 32");
6391 * X << 32 is not guaranteed by C to be 0; it's
6396 m
= 0xffffffff << (32 - masklen
);
6398 bpf_error("non-network bits set in \"%s/%d\"",
6405 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6408 bpf_error("Mask syntax for networks only");
6417 register const char *s
;
6422 int proto
= q
.proto
;
6428 else if (q
.proto
== Q_DECNET
)
6429 vlen
= __pcap_atodn(s
, &v
);
6431 vlen
= __pcap_atoin(s
, &v
);
6438 if (proto
== Q_DECNET
)
6439 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6440 else if (proto
== Q_LINK
) {
6441 bpf_error("illegal link layer address");
6444 if (s
== NULL
&& q
.addr
== Q_NET
) {
6445 /* Promote short net number */
6446 while (v
&& (v
& 0xff000000) == 0) {
6451 /* Promote short ipaddr */
6455 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6460 proto
= IPPROTO_UDP
;
6461 else if (proto
== Q_TCP
)
6462 proto
= IPPROTO_TCP
;
6463 else if (proto
== Q_SCTP
)
6464 proto
= IPPROTO_SCTP
;
6465 else if (proto
== Q_DEFAULT
)
6466 proto
= PROTO_UNDEF
;
6468 bpf_error("illegal qualifier of 'port'");
6471 bpf_error("illegal port number %u > 65535", v
);
6474 return gen_port((int)v
, proto
, dir
);
6478 b
= gen_port((int)v
, proto
, dir
);
6479 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6486 proto
= IPPROTO_UDP
;
6487 else if (proto
== Q_TCP
)
6488 proto
= IPPROTO_TCP
;
6489 else if (proto
== Q_SCTP
)
6490 proto
= IPPROTO_SCTP
;
6491 else if (proto
== Q_DEFAULT
)
6492 proto
= PROTO_UNDEF
;
6494 bpf_error("illegal qualifier of 'portrange'");
6497 bpf_error("illegal port number %u > 65535", v
);
6500 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6504 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6505 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6511 bpf_error("'gateway' requires a name");
6515 return gen_proto((int)v
, proto
, dir
);
6518 return gen_protochain((int)v
, proto
, dir
);
6533 gen_mcode6(s1
, s2
, masklen
, q
)
6534 register const char *s1
, *s2
;
6535 register int masklen
;
6538 struct addrinfo
*res
;
6539 struct in6_addr
*addr
;
6540 struct in6_addr mask
;
6545 bpf_error("no mask %s supported", s2
);
6547 res
= pcap_nametoaddrinfo(s1
);
6549 bpf_error("invalid ip6 address %s", s1
);
6552 bpf_error("%s resolved to multiple address", s1
);
6553 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6555 if (sizeof(mask
) * 8 < masklen
)
6556 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6557 memset(&mask
, 0, sizeof(mask
));
6558 memset(&mask
, 0xff, masklen
/ 8);
6560 mask
.s6_addr
[masklen
/ 8] =
6561 (0xff << (8 - masklen
% 8)) & 0xff;
6564 a
= (u_int32_t
*)addr
;
6565 m
= (u_int32_t
*)&mask
;
6566 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6567 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6568 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6576 bpf_error("Mask syntax for networks only");
6580 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6586 bpf_error("invalid qualifier against IPv6 address");
6595 register const u_char
*eaddr
;
6598 struct block
*b
, *tmp
;
6600 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6603 case DLT_NETANALYZER
:
6604 case DLT_NETANALYZER_TRANSPARENT
:
6605 return gen_ehostop(eaddr
, (int)q
.dir
);
6607 return gen_fhostop(eaddr
, (int)q
.dir
);
6609 return gen_thostop(eaddr
, (int)q
.dir
);
6610 case DLT_IEEE802_11
:
6611 case DLT_PRISM_HEADER
:
6612 case DLT_IEEE802_11_RADIO_AVS
:
6613 case DLT_IEEE802_11_RADIO
:
6615 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6619 * Check that the packet doesn't begin with an
6620 * LE Control marker. (We've already generated
6623 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6628 * Now check the MAC address.
6630 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6635 case DLT_IP_OVER_FC
:
6636 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6638 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6642 bpf_error("ethernet address used in non-ether expression");
6649 struct slist
*s0
, *s1
;
6652 * This is definitely not the best way to do this, but the
6653 * lists will rarely get long.
6660 static struct slist
*
6666 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6671 static struct slist
*
6677 s
= new_stmt(BPF_LD
|BPF_MEM
);
6683 * Modify "index" to use the value stored into its register as an
6684 * offset relative to the beginning of the header for the protocol
6685 * "proto", and allocate a register and put an item "size" bytes long
6686 * (1, 2, or 4) at that offset into that register, making it the register
6690 gen_load(proto
, inst
, size
)
6695 struct slist
*s
, *tmp
;
6697 int regno
= alloc_reg();
6699 free_reg(inst
->regno
);
6703 bpf_error("data size must be 1, 2, or 4");
6719 bpf_error("unsupported index operation");
6723 * The offset is relative to the beginning of the packet
6724 * data, if we have a radio header. (If we don't, this
6727 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6728 linktype
!= DLT_IEEE802_11_RADIO
&&
6729 linktype
!= DLT_PRISM_HEADER
)
6730 bpf_error("radio information not present in capture");
6733 * Load into the X register the offset computed into the
6734 * register specified by "index".
6736 s
= xfer_to_x(inst
);
6739 * Load the item at that offset.
6741 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6743 sappend(inst
->s
, s
);
6748 * The offset is relative to the beginning of
6749 * the link-layer header.
6751 * XXX - what about ATM LANE? Should the index be
6752 * relative to the beginning of the AAL5 frame, so
6753 * that 0 refers to the beginning of the LE Control
6754 * field, or relative to the beginning of the LAN
6755 * frame, so that 0 refers, for Ethernet LANE, to
6756 * the beginning of the destination address?
6758 s
= gen_llprefixlen();
6761 * If "s" is non-null, it has code to arrange that the
6762 * X register contains the length of the prefix preceding
6763 * the link-layer header. Add to it the offset computed
6764 * into the register specified by "index", and move that
6765 * into the X register. Otherwise, just load into the X
6766 * register the offset computed into the register specified
6770 sappend(s
, xfer_to_a(inst
));
6771 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6772 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6774 s
= xfer_to_x(inst
);
6777 * Load the item at the sum of the offset we've put in the
6778 * X register and the offset of the start of the link
6779 * layer header (which is 0 if the radio header is
6780 * variable-length; that header length is what we put
6781 * into the X register and then added to the index).
6783 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6786 sappend(inst
->s
, s
);
6802 * The offset is relative to the beginning of
6803 * the network-layer header.
6804 * XXX - are there any cases where we want
6807 s
= gen_off_macpl();
6810 * If "s" is non-null, it has code to arrange that the
6811 * X register contains the offset of the MAC-layer
6812 * payload. Add to it the offset computed into the
6813 * register specified by "index", and move that into
6814 * the X register. Otherwise, just load into the X
6815 * register the offset computed into the register specified
6819 sappend(s
, xfer_to_a(inst
));
6820 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6821 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6823 s
= xfer_to_x(inst
);
6826 * Load the item at the sum of the offset we've put in the
6827 * X register, the offset of the start of the network
6828 * layer header from the beginning of the MAC-layer
6829 * payload, and the purported offset of the start of the
6830 * MAC-layer payload (which might be 0 if there's a
6831 * variable-length prefix before the link-layer header
6832 * or the link-layer header itself is variable-length;
6833 * the variable-length offset of the start of the
6834 * MAC-layer payload is what we put into the X register
6835 * and then added to the index).
6837 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6838 tmp
->s
.k
= off_macpl
+ off_nl
;
6840 sappend(inst
->s
, s
);
6843 * Do the computation only if the packet contains
6844 * the protocol in question.
6846 b
= gen_proto_abbrev(proto
);
6848 gen_and(inst
->b
, b
);
6862 * The offset is relative to the beginning of
6863 * the transport-layer header.
6865 * Load the X register with the length of the IPv4 header
6866 * (plus the offset of the link-layer header, if it's
6867 * a variable-length header), in bytes.
6869 * XXX - are there any cases where we want
6871 * XXX - we should, if we're built with
6872 * IPv6 support, generate code to load either
6873 * IPv4, IPv6, or both, as appropriate.
6875 s
= gen_loadx_iphdrlen();
6878 * The X register now contains the sum of the length
6879 * of any variable-length header preceding the link-layer
6880 * header, any variable-length link-layer header, and the
6881 * length of the network-layer header.
6883 * Load into the A register the offset relative to
6884 * the beginning of the transport layer header,
6885 * add the X register to that, move that to the
6886 * X register, and load with an offset from the
6887 * X register equal to the offset of the network
6888 * layer header relative to the beginning of
6889 * the MAC-layer payload plus the fixed-length
6890 * portion of the offset of the MAC-layer payload
6891 * from the beginning of the raw packet data.
6893 sappend(s
, xfer_to_a(inst
));
6894 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6895 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6896 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6897 tmp
->s
.k
= off_macpl
+ off_nl
;
6898 sappend(inst
->s
, s
);
6901 * Do the computation only if the packet contains
6902 * the protocol in question - which is true only
6903 * if this is an IP datagram and is the first or
6904 * only fragment of that datagram.
6906 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6908 gen_and(inst
->b
, b
);
6910 gen_and(gen_proto_abbrev(Q_IP
), b
);
6916 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6920 inst
->regno
= regno
;
6921 s
= new_stmt(BPF_ST
);
6923 sappend(inst
->s
, s
);
6929 gen_relation(code
, a0
, a1
, reversed
)
6931 struct arth
*a0
, *a1
;
6934 struct slist
*s0
, *s1
, *s2
;
6935 struct block
*b
, *tmp
;
6939 if (code
== BPF_JEQ
) {
6940 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6941 b
= new_block(JMP(code
));
6945 b
= new_block(BPF_JMP
|code
|BPF_X
);
6951 sappend(a0
->s
, a1
->s
);
6955 free_reg(a0
->regno
);
6956 free_reg(a1
->regno
);
6958 /* 'and' together protocol checks */
6961 gen_and(a0
->b
, tmp
= a1
->b
);
6977 int regno
= alloc_reg();
6978 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6981 s
= new_stmt(BPF_LD
|BPF_LEN
);
6982 s
->next
= new_stmt(BPF_ST
);
6983 s
->next
->s
.k
= regno
;
6998 a
= (struct arth
*)newchunk(sizeof(*a
));
7002 s
= new_stmt(BPF_LD
|BPF_IMM
);
7004 s
->next
= new_stmt(BPF_ST
);
7020 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7023 s
= new_stmt(BPF_ST
);
7031 gen_arth(code
, a0
, a1
)
7033 struct arth
*a0
, *a1
;
7035 struct slist
*s0
, *s1
, *s2
;
7039 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7044 sappend(a0
->s
, a1
->s
);
7046 free_reg(a0
->regno
);
7047 free_reg(a1
->regno
);
7049 s0
= new_stmt(BPF_ST
);
7050 a0
->regno
= s0
->s
.k
= alloc_reg();
7057 * Here we handle simple allocation of the scratch registers.
7058 * If too many registers are alloc'd, the allocator punts.
7060 static int regused
[BPF_MEMWORDS
];
7064 * Initialize the table of used registers and the current register.
7070 memset(regused
, 0, sizeof regused
);
7074 * Return the next free register.
7079 int n
= BPF_MEMWORDS
;
7082 if (regused
[curreg
])
7083 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7085 regused
[curreg
] = 1;
7089 bpf_error("too many registers needed to evaluate expression");
7095 * Return a register to the table so it can
7105 static struct block
*
7112 s
= new_stmt(BPF_LD
|BPF_LEN
);
7113 b
= new_block(JMP(jmp
));
7124 return gen_len(BPF_JGE
, n
);
7128 * Actually, this is less than or equal.
7136 b
= gen_len(BPF_JGT
, n
);
7143 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7144 * the beginning of the link-layer header.
7145 * XXX - that means you can't test values in the radiotap header, but
7146 * as that header is difficult if not impossible to parse generally
7147 * without a loop, that might not be a severe problem. A new keyword
7148 * "radio" could be added for that, although what you'd really want
7149 * would be a way of testing particular radio header values, which
7150 * would generate code appropriate to the radio header in question.
7153 gen_byteop(op
, idx
, val
)
7164 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7167 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7171 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7175 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7179 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7183 b
= new_block(JMP(BPF_JEQ
));
7190 static u_char abroadcast
[] = { 0x0 };
7193 gen_broadcast(proto
)
7196 bpf_u_int32 hostmask
;
7197 struct block
*b0
, *b1
, *b2
;
7198 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7206 case DLT_ARCNET_LINUX
:
7207 return gen_ahostop(abroadcast
, Q_DST
);
7209 case DLT_NETANALYZER
:
7210 case DLT_NETANALYZER_TRANSPARENT
:
7211 return gen_ehostop(ebroadcast
, Q_DST
);
7213 return gen_fhostop(ebroadcast
, Q_DST
);
7215 return gen_thostop(ebroadcast
, Q_DST
);
7216 case DLT_IEEE802_11
:
7217 case DLT_PRISM_HEADER
:
7218 case DLT_IEEE802_11_RADIO_AVS
:
7219 case DLT_IEEE802_11_RADIO
:
7221 return gen_wlanhostop(ebroadcast
, Q_DST
);
7222 case DLT_IP_OVER_FC
:
7223 return gen_ipfchostop(ebroadcast
, Q_DST
);
7227 * Check that the packet doesn't begin with an
7228 * LE Control marker. (We've already generated
7231 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7236 * Now check the MAC address.
7238 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7244 bpf_error("not a broadcast link");
7250 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7251 * as an indication that we don't know the netmask, and fail
7254 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7255 bpf_error("netmask not known, so 'ip broadcast' not supported");
7256 b0
= gen_linktype(ETHERTYPE_IP
);
7257 hostmask
= ~netmask
;
7258 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7259 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7260 (bpf_int32
)(~0 & hostmask
), hostmask
);
7265 bpf_error("only link-layer/IP broadcast filters supported");
7271 * Generate code to test the low-order bit of a MAC address (that's
7272 * the bottom bit of the *first* byte).
7274 static struct block
*
7275 gen_mac_multicast(offset
)
7278 register struct block
*b0
;
7279 register struct slist
*s
;
7281 /* link[offset] & 1 != 0 */
7282 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7283 b0
= new_block(JMP(BPF_JSET
));
7290 gen_multicast(proto
)
7293 register struct block
*b0
, *b1
, *b2
;
7294 register struct slist
*s
;
7302 case DLT_ARCNET_LINUX
:
7303 /* all ARCnet multicasts use the same address */
7304 return gen_ahostop(abroadcast
, Q_DST
);
7306 case DLT_NETANALYZER
:
7307 case DLT_NETANALYZER_TRANSPARENT
:
7308 /* ether[0] & 1 != 0 */
7309 return gen_mac_multicast(0);
7312 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7314 * XXX - was that referring to bit-order issues?
7316 /* fddi[1] & 1 != 0 */
7317 return gen_mac_multicast(1);
7319 /* tr[2] & 1 != 0 */
7320 return gen_mac_multicast(2);
7321 case DLT_IEEE802_11
:
7322 case DLT_PRISM_HEADER
:
7323 case DLT_IEEE802_11_RADIO_AVS
:
7324 case DLT_IEEE802_11_RADIO
:
7329 * For control frames, there is no DA.
7331 * For management frames, DA is at an
7332 * offset of 4 from the beginning of
7335 * For data frames, DA is at an offset
7336 * of 4 from the beginning of the packet
7337 * if To DS is clear and at an offset of
7338 * 16 from the beginning of the packet
7343 * Generate the tests to be done for data frames.
7345 * First, check for To DS set, i.e. "link[1] & 0x01".
7347 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7348 b1
= new_block(JMP(BPF_JSET
));
7349 b1
->s
.k
= 0x01; /* To DS */
7353 * If To DS is set, the DA is at 16.
7355 b0
= gen_mac_multicast(16);
7359 * Now, check for To DS not set, i.e. check
7360 * "!(link[1] & 0x01)".
7362 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7363 b2
= new_block(JMP(BPF_JSET
));
7364 b2
->s
.k
= 0x01; /* To DS */
7369 * If To DS is not set, the DA is at 4.
7371 b1
= gen_mac_multicast(4);
7375 * Now OR together the last two checks. That gives
7376 * the complete set of checks for data frames.
7381 * Now check for a data frame.
7382 * I.e, check "link[0] & 0x08".
7384 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7385 b1
= new_block(JMP(BPF_JSET
));
7390 * AND that with the checks done for data frames.
7395 * If the high-order bit of the type value is 0, this
7396 * is a management frame.
7397 * I.e, check "!(link[0] & 0x08)".
7399 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7400 b2
= new_block(JMP(BPF_JSET
));
7406 * For management frames, the DA is at 4.
7408 b1
= gen_mac_multicast(4);
7412 * OR that with the checks done for data frames.
7413 * That gives the checks done for management and
7419 * If the low-order bit of the type value is 1,
7420 * this is either a control frame or a frame
7421 * with a reserved type, and thus not a
7424 * I.e., check "!(link[0] & 0x04)".
7426 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7427 b1
= new_block(JMP(BPF_JSET
));
7433 * AND that with the checks for data and management
7438 case DLT_IP_OVER_FC
:
7439 b0
= gen_mac_multicast(2);
7444 * Check that the packet doesn't begin with an
7445 * LE Control marker. (We've already generated
7448 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7452 /* ether[off_mac] & 1 != 0 */
7453 b0
= gen_mac_multicast(off_mac
);
7461 /* Link not known to support multicasts */
7465 b0
= gen_linktype(ETHERTYPE_IP
);
7466 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7472 b0
= gen_linktype(ETHERTYPE_IPV6
);
7473 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7478 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7484 * generate command for inbound/outbound. It's here so we can
7485 * make it link-type specific. 'dir' = 0 implies "inbound",
7486 * = 1 implies "outbound".
7492 register struct block
*b0
;
7495 * Only some data link types support inbound/outbound qualifiers.
7499 b0
= gen_relation(BPF_JEQ
,
7500 gen_load(Q_LINK
, gen_loadi(0), 1),
7507 /* match outgoing packets */
7508 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7510 /* match incoming packets */
7511 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7518 * Match packets sent by this machine.
7520 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7523 * Match packets sent to this machine.
7524 * (No broadcast or multicast packets, or
7525 * packets sent to some other machine and
7526 * received promiscuously.)
7528 * XXX - packets sent to other machines probably
7529 * shouldn't be matched, but what about broadcast
7530 * or multicast packets we received?
7532 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7536 #ifdef HAVE_NET_PFVAR_H
7538 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7539 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7545 /* match outgoing packets */
7546 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7548 /* match incoming packets */
7549 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7553 case DLT_JUNIPER_MFR
:
7554 case DLT_JUNIPER_MLFR
:
7555 case DLT_JUNIPER_MLPPP
:
7556 case DLT_JUNIPER_ATM1
:
7557 case DLT_JUNIPER_ATM2
:
7558 case DLT_JUNIPER_PPPOE
:
7559 case DLT_JUNIPER_PPPOE_ATM
:
7560 case DLT_JUNIPER_GGSN
:
7561 case DLT_JUNIPER_ES
:
7562 case DLT_JUNIPER_MONITOR
:
7563 case DLT_JUNIPER_SERVICES
:
7564 case DLT_JUNIPER_ETHER
:
7565 case DLT_JUNIPER_PPP
:
7566 case DLT_JUNIPER_FRELAY
:
7567 case DLT_JUNIPER_CHDLC
:
7568 case DLT_JUNIPER_VP
:
7569 case DLT_JUNIPER_ST
:
7570 case DLT_JUNIPER_ISM
:
7571 case DLT_JUNIPER_VS
:
7572 case DLT_JUNIPER_SRX_E2E
:
7573 case DLT_JUNIPER_FIBRECHANNEL
:
7574 case DLT_JUNIPER_ATM_CEMIC
:
7576 /* juniper flags (including direction) are stored
7577 * the byte after the 3-byte magic number */
7579 /* match outgoing packets */
7580 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7582 /* match incoming packets */
7583 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7588 bpf_error("inbound/outbound not supported on linktype %d",
7596 #ifdef HAVE_NET_PFVAR_H
7597 /* PF firewall log matched interface */
7599 gen_pf_ifname(const char *ifname
)
7604 if (linktype
!= DLT_PFLOG
) {
7605 bpf_error("ifname supported only on PF linktype");
7608 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7609 off
= offsetof(struct pfloghdr
, ifname
);
7610 if (strlen(ifname
) >= len
) {
7611 bpf_error("ifname interface names can only be %d characters",
7615 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7619 /* PF firewall log ruleset name */
7621 gen_pf_ruleset(char *ruleset
)
7625 if (linktype
!= DLT_PFLOG
) {
7626 bpf_error("ruleset supported only on PF linktype");
7630 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7631 bpf_error("ruleset names can only be %ld characters",
7632 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7636 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7637 strlen(ruleset
), (const u_char
*)ruleset
);
7641 /* PF firewall log rule number */
7647 if (linktype
!= DLT_PFLOG
) {
7648 bpf_error("rnr supported only on PF linktype");
7652 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7657 /* PF firewall log sub-rule number */
7659 gen_pf_srnr(int srnr
)
7663 if (linktype
!= DLT_PFLOG
) {
7664 bpf_error("srnr supported only on PF linktype");
7668 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7673 /* PF firewall log reason code */
7675 gen_pf_reason(int reason
)
7679 if (linktype
!= DLT_PFLOG
) {
7680 bpf_error("reason supported only on PF linktype");
7684 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7689 /* PF firewall log action */
7691 gen_pf_action(int action
)
7695 if (linktype
!= DLT_PFLOG
) {
7696 bpf_error("action supported only on PF linktype");
7700 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7704 #else /* !HAVE_NET_PFVAR_H */
7706 gen_pf_ifname(const char *ifname
)
7708 bpf_error("libpcap was compiled without pf support");
7714 gen_pf_ruleset(char *ruleset
)
7716 bpf_error("libpcap was compiled on a machine without pf support");
7724 bpf_error("libpcap was compiled on a machine without pf support");
7730 gen_pf_srnr(int srnr
)
7732 bpf_error("libpcap was compiled on a machine without pf support");
7738 gen_pf_reason(int reason
)
7740 bpf_error("libpcap was compiled on a machine without pf support");
7746 gen_pf_action(int action
)
7748 bpf_error("libpcap was compiled on a machine without pf support");
7752 #endif /* HAVE_NET_PFVAR_H */
7754 /* IEEE 802.11 wireless header */
7756 gen_p80211_type(int type
, int mask
)
7762 case DLT_IEEE802_11
:
7763 case DLT_PRISM_HEADER
:
7764 case DLT_IEEE802_11_RADIO_AVS
:
7765 case DLT_IEEE802_11_RADIO
:
7766 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7771 bpf_error("802.11 link-layer types supported only on 802.11");
7779 gen_p80211_fcdir(int fcdir
)
7785 case DLT_IEEE802_11
:
7786 case DLT_PRISM_HEADER
:
7787 case DLT_IEEE802_11_RADIO_AVS
:
7788 case DLT_IEEE802_11_RADIO
:
7792 bpf_error("frame direction supported only with 802.11 headers");
7796 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7797 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7804 register const u_char
*eaddr
;
7810 case DLT_ARCNET_LINUX
:
7811 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7813 return (gen_ahostop(eaddr
, (int)q
.dir
));
7815 bpf_error("ARCnet address used in non-arc expression");
7821 bpf_error("aid supported only on ARCnet");
7824 bpf_error("ARCnet address used in non-arc expression");
7829 static struct block
*
7830 gen_ahostop(eaddr
, dir
)
7831 register const u_char
*eaddr
;
7834 register struct block
*b0
, *b1
;
7837 /* src comes first, different from Ethernet */
7839 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7842 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7845 b0
= gen_ahostop(eaddr
, Q_SRC
);
7846 b1
= gen_ahostop(eaddr
, Q_DST
);
7852 b0
= gen_ahostop(eaddr
, Q_SRC
);
7853 b1
= gen_ahostop(eaddr
, Q_DST
);
7858 bpf_error("'addr1' is only supported on 802.11");
7862 bpf_error("'addr2' is only supported on 802.11");
7866 bpf_error("'addr3' is only supported on 802.11");
7870 bpf_error("'addr4' is only supported on 802.11");
7874 bpf_error("'ra' is only supported on 802.11");
7878 bpf_error("'ta' is only supported on 802.11");
7886 * support IEEE 802.1Q VLAN trunk over ethernet
7892 struct block
*b0
, *b1
;
7894 /* can't check for VLAN-encapsulated packets inside MPLS */
7895 if (label_stack_depth
> 0)
7896 bpf_error("no VLAN match after MPLS");
7899 * Check for a VLAN packet, and then change the offsets to point
7900 * to the type and data fields within the VLAN packet. Just
7901 * increment the offsets, so that we can support a hierarchy, e.g.
7902 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7905 * XXX - this is a bit of a kludge. If we were to split the
7906 * compiler into a parser that parses an expression and
7907 * generates an expression tree, and a code generator that
7908 * takes an expression tree (which could come from our
7909 * parser or from some other parser) and generates BPF code,
7910 * we could perhaps make the offsets parameters of routines
7911 * and, in the handler for an "AND" node, pass to subnodes
7912 * other than the VLAN node the adjusted offsets.
7914 * This would mean that "vlan" would, instead of changing the
7915 * behavior of *all* tests after it, change only the behavior
7916 * of tests ANDed with it. That would change the documented
7917 * semantics of "vlan", which might break some expressions.
7918 * However, it would mean that "(vlan and ip) or ip" would check
7919 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7920 * checking only for VLAN-encapsulated IP, so that could still
7921 * be considered worth doing; it wouldn't break expressions
7922 * that are of the form "vlan and ..." or "vlan N and ...",
7923 * which I suspect are the most common expressions involving
7924 * "vlan". "vlan or ..." doesn't necessarily do what the user
7925 * would really want, now, as all the "or ..." tests would
7926 * be done assuming a VLAN, even though the "or" could be viewed
7927 * as meaning "or, if this isn't a VLAN packet...".
7934 case DLT_NETANALYZER
:
7935 case DLT_NETANALYZER_TRANSPARENT
:
7936 /* check for VLAN, including QinQ */
7937 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7938 (bpf_int32
)ETHERTYPE_8021Q
);
7939 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7940 (bpf_int32
)ETHERTYPE_8021QINQ
);
7944 /* If a specific VLAN is requested, check VLAN id */
7945 if (vlan_num
>= 0) {
7946 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7947 (bpf_int32
)vlan_num
, 0x0fff);
7961 bpf_error("no VLAN support for data link type %d",
7976 struct block
*b0
,*b1
;
7979 * Change the offsets to point to the type and data fields within
7980 * the MPLS packet. Just increment the offsets, so that we
7981 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7982 * capture packets with an outer label of 100000 and an inner
7985 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7989 if (label_stack_depth
> 0) {
7990 /* just match the bottom-of-stack bit clear */
7991 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7994 * Indicate that we're checking MPLS-encapsulated headers,
7995 * to make sure higher level code generators don't try to
7996 * match against IP-related protocols such as Q_ARP, Q_RARP
8001 case DLT_C_HDLC
: /* fall through */
8003 case DLT_NETANALYZER
:
8004 case DLT_NETANALYZER_TRANSPARENT
:
8005 b0
= gen_linktype(ETHERTYPE_MPLS
);
8009 b0
= gen_linktype(PPP_MPLS_UCAST
);
8012 /* FIXME add other DLT_s ...
8013 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8014 * leave it for now */
8017 bpf_error("no MPLS support for data link type %d",
8025 /* If a specific MPLS label is requested, check it */
8026 if (label_num
>= 0) {
8027 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8028 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8029 0xfffff000); /* only compare the first 20 bits */
8036 label_stack_depth
++;
8041 * Support PPPOE discovery and session.
8046 /* check for PPPoE discovery */
8047 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8056 * Test against the PPPoE session link-layer type.
8058 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8061 * Change the offsets to point to the type and data fields within
8062 * the PPP packet, and note that this is PPPoE rather than
8065 * XXX - this is a bit of a kludge. If we were to split the
8066 * compiler into a parser that parses an expression and
8067 * generates an expression tree, and a code generator that
8068 * takes an expression tree (which could come from our
8069 * parser or from some other parser) and generates BPF code,
8070 * we could perhaps make the offsets parameters of routines
8071 * and, in the handler for an "AND" node, pass to subnodes
8072 * other than the PPPoE node the adjusted offsets.
8074 * This would mean that "pppoes" would, instead of changing the
8075 * behavior of *all* tests after it, change only the behavior
8076 * of tests ANDed with it. That would change the documented
8077 * semantics of "pppoes", which might break some expressions.
8078 * However, it would mean that "(pppoes and ip) or ip" would check
8079 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8080 * checking only for VLAN-encapsulated IP, so that could still
8081 * be considered worth doing; it wouldn't break expressions
8082 * that are of the form "pppoes and ..." which I suspect are the
8083 * most common expressions involving "pppoes". "pppoes or ..."
8084 * doesn't necessarily do what the user would really want, now,
8085 * as all the "or ..." tests would be done assuming PPPoE, even
8086 * though the "or" could be viewed as meaning "or, if this isn't
8087 * a PPPoE packet...".
8089 orig_linktype
= off_linktype
; /* save original values */
8094 * The "network-layer" protocol is PPPoE, which has a 6-byte
8095 * PPPoE header, followed by a PPP packet.
8097 * There is no HDLC encapsulation for the PPP packet (it's
8098 * encapsulated in PPPoES instead), so the link-layer type
8099 * starts at the first byte of the PPP packet. For PPPoE,
8100 * that offset is relative to the beginning of the total
8101 * link-layer payload, including any 802.2 LLC header, so
8102 * it's 6 bytes past off_nl.
8104 off_linktype
= off_nl
+ 6;
8107 * The network-layer offsets are relative to the beginning
8108 * of the MAC-layer payload; that's past the 6-byte
8109 * PPPoE header and the 2-byte PPP header.
8112 off_nl_nosnap
= 6+2;
8118 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8130 bpf_error("'vpi' supported only on raw ATM");
8131 if (off_vpi
== (u_int
)-1)
8133 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8139 bpf_error("'vci' supported only on raw ATM");
8140 if (off_vci
== (u_int
)-1)
8142 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8147 if (off_proto
== (u_int
)-1)
8148 abort(); /* XXX - this isn't on FreeBSD */
8149 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8154 if (off_payload
== (u_int
)-1)
8156 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8157 0xffffffff, jtype
, reverse
, jvalue
);
8162 bpf_error("'callref' supported only on raw ATM");
8163 if (off_proto
== (u_int
)-1)
8165 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8166 jtype
, reverse
, jvalue
);
8176 gen_atmtype_abbrev(type
)
8179 struct block
*b0
, *b1
;
8184 /* Get all packets in Meta signalling Circuit */
8186 bpf_error("'metac' supported only on raw ATM");
8187 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8188 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8193 /* Get all packets in Broadcast Circuit*/
8195 bpf_error("'bcc' supported only on raw ATM");
8196 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8197 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8202 /* Get all cells in Segment OAM F4 circuit*/
8204 bpf_error("'oam4sc' supported only on raw ATM");
8205 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8206 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8211 /* Get all cells in End-to-End OAM F4 Circuit*/
8213 bpf_error("'oam4ec' supported only on raw ATM");
8214 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8215 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8220 /* Get all packets in connection Signalling Circuit */
8222 bpf_error("'sc' supported only on raw ATM");
8223 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8224 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8229 /* Get all packets in ILMI Circuit */
8231 bpf_error("'ilmic' supported only on raw ATM");
8232 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8233 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8238 /* Get all LANE packets */
8240 bpf_error("'lane' supported only on raw ATM");
8241 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8244 * Arrange that all subsequent tests assume LANE
8245 * rather than LLC-encapsulated packets, and set
8246 * the offsets appropriately for LANE-encapsulated
8249 * "off_mac" is the offset of the Ethernet header,
8250 * which is 2 bytes past the ATM pseudo-header
8251 * (skipping the pseudo-header and 2-byte LE Client
8252 * field). The other offsets are Ethernet offsets
8253 * relative to "off_mac".
8256 off_mac
= off_payload
+ 2; /* MAC header */
8257 off_linktype
= off_mac
+ 12;
8258 off_macpl
= off_mac
+ 14; /* Ethernet */
8259 off_nl
= 0; /* Ethernet II */
8260 off_nl_nosnap
= 3; /* 802.3+802.2 */
8264 /* Get all LLC-encapsulated packets */
8266 bpf_error("'llc' supported only on raw ATM");
8267 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8278 * Filtering for MTP2 messages based on li value
8279 * FISU, length is null
8280 * LSSU, length is 1 or 2
8281 * MSU, length is 3 or more
8284 gen_mtp2type_abbrev(type
)
8287 struct block
*b0
, *b1
;
8292 if ( (linktype
!= DLT_MTP2
) &&
8293 (linktype
!= DLT_ERF
) &&
8294 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8295 bpf_error("'fisu' supported only on MTP2");
8296 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8297 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8301 if ( (linktype
!= DLT_MTP2
) &&
8302 (linktype
!= DLT_ERF
) &&
8303 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8304 bpf_error("'lssu' supported only on MTP2");
8305 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8306 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8311 if ( (linktype
!= DLT_MTP2
) &&
8312 (linktype
!= DLT_ERF
) &&
8313 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8314 bpf_error("'msu' supported only on MTP2");
8315 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8325 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8332 bpf_u_int32 val1
, val2
, val3
;
8334 switch (mtp3field
) {
8337 if (off_sio
== (u_int
)-1)
8338 bpf_error("'sio' supported only on SS7");
8339 /* sio coded on 1 byte so max value 255 */
8341 bpf_error("sio value %u too big; max value = 255",
8343 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8344 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8348 if (off_opc
== (u_int
)-1)
8349 bpf_error("'opc' supported only on SS7");
8350 /* opc coded on 14 bits so max value 16383 */
8352 bpf_error("opc value %u too big; max value = 16383",
8354 /* the following instructions are made to convert jvalue
8355 * to the form used to write opc in an ss7 message*/
8356 val1
= jvalue
& 0x00003c00;
8358 val2
= jvalue
& 0x000003fc;
8360 val3
= jvalue
& 0x00000003;
8362 jvalue
= val1
+ val2
+ val3
;
8363 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8364 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8368 if (off_dpc
== (u_int
)-1)
8369 bpf_error("'dpc' supported only on SS7");
8370 /* dpc coded on 14 bits so max value 16383 */
8372 bpf_error("dpc value %u too big; max value = 16383",
8374 /* the following instructions are made to convert jvalue
8375 * to the forme used to write dpc in an ss7 message*/
8376 val1
= jvalue
& 0x000000ff;
8378 val2
= jvalue
& 0x00003f00;
8380 jvalue
= val1
+ val2
;
8381 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8382 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8386 if (off_sls
== (u_int
)-1)
8387 bpf_error("'sls' supported only on SS7");
8388 /* sls coded on 4 bits so max value 15 */
8390 bpf_error("sls value %u too big; max value = 15",
8392 /* the following instruction is made to convert jvalue
8393 * to the forme used to write sls in an ss7 message*/
8394 jvalue
= jvalue
<< 4;
8395 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8396 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8405 static struct block
*
8406 gen_msg_abbrev(type
)
8412 * Q.2931 signalling protocol messages for handling virtual circuits
8413 * establishment and teardown
8418 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8422 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8426 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8430 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8434 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8437 case A_RELEASE_DONE
:
8438 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8448 gen_atmmulti_abbrev(type
)
8451 struct block
*b0
, *b1
;
8457 bpf_error("'oam' supported only on raw ATM");
8458 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8463 bpf_error("'oamf4' supported only on raw ATM");
8465 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8466 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8468 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8474 * Get Q.2931 signalling messages for switched
8475 * virtual connection
8478 bpf_error("'connectmsg' supported only on raw ATM");
8479 b0
= gen_msg_abbrev(A_SETUP
);
8480 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8482 b0
= gen_msg_abbrev(A_CONNECT
);
8484 b0
= gen_msg_abbrev(A_CONNECTACK
);
8486 b0
= gen_msg_abbrev(A_RELEASE
);
8488 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8490 b0
= gen_atmtype_abbrev(A_SC
);
8496 bpf_error("'metaconnect' supported only on raw ATM");
8497 b0
= gen_msg_abbrev(A_SETUP
);
8498 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8500 b0
= gen_msg_abbrev(A_CONNECT
);
8502 b0
= gen_msg_abbrev(A_RELEASE
);
8504 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8506 b0
= gen_atmtype_abbrev(A_METAC
);