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>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
68 #include "ethertype.h"
72 #include "ieee80211.h"
74 #include "sunatmpos.h"
77 #include "pcap/ipnet.h"
79 #ifdef HAVE_NET_PFVAR_H
80 #include <sys/socket.h>
82 #include <net/pfvar.h>
83 #include <net/if_pflog.h>
86 #define offsetof(s, e) ((size_t)&((s *)0)->e)
90 #include <netdb.h> /* for "struct addrinfo" */
93 #include <pcap/namedb.h>
98 #define IPPROTO_SCTP 132
101 #ifdef HAVE_OS_PROTO_H
102 #include "os-proto.h"
105 #define JMP(c) ((c)|BPF_JMP|BPF_K)
108 static jmp_buf top_ctx
;
109 static pcap_t
*bpf_pcap
;
111 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
113 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
115 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
120 static int pcap_fddipad
;
125 bpf_error(const char *fmt
, ...)
130 if (bpf_pcap
!= NULL
)
131 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
138 static void init_linktype(pcap_t
*);
140 static void init_regs(void);
141 static int alloc_reg(void);
142 static void free_reg(int);
144 static struct block
*root
;
147 * Value passed to gen_load_a() to indicate what the offset argument
151 OR_PACKET
, /* relative to the beginning of the packet */
152 OR_LINK
, /* relative to the beginning of the link-layer header */
153 OR_MACPL
, /* relative to the end of the MAC-layer header */
154 OR_NET
, /* relative to the network-layer header */
155 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
156 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
157 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
162 * As errors are handled by a longjmp, anything allocated must be freed
163 * in the longjmp handler, so it must be reachable from that handler.
164 * One thing that's allocated is the result of pcap_nametoaddrinfo();
165 * it must be freed with freeaddrinfo(). This variable points to any
166 * addrinfo structure that would need to be freed.
168 static struct addrinfo
*ai
;
172 * We divy out chunks of memory rather than call malloc each time so
173 * we don't have to worry about leaking memory. It's probably
174 * not a big deal if all this memory was wasted but if this ever
175 * goes into a library that would probably not be a good idea.
177 * XXX - this *is* in a library....
180 #define CHUNK0SIZE 1024
186 static struct chunk chunks
[NCHUNKS
];
187 static int cur_chunk
;
189 static void *newchunk(u_int
);
190 static void freechunks(void);
191 static inline struct block
*new_block(int);
192 static inline struct slist
*new_stmt(int);
193 static struct block
*gen_retblk(int);
194 static inline void syntax(void);
196 static void backpatch(struct block
*, struct block
*);
197 static void merge(struct block
*, struct block
*);
198 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
199 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
200 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
201 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
202 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
203 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
205 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
206 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
207 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
208 static struct slist
*gen_load_llrel(u_int
, u_int
);
209 static struct slist
*gen_load_macplrel(u_int
, u_int
);
210 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
211 static struct slist
*gen_loadx_iphdrlen(void);
212 static struct block
*gen_uncond(int);
213 static inline struct block
*gen_true(void);
214 static inline struct block
*gen_false(void);
215 static struct block
*gen_ether_linktype(int);
216 static struct block
*gen_ipnet_linktype(int);
217 static struct block
*gen_linux_sll_linktype(int);
218 static struct slist
*gen_load_prism_llprefixlen(void);
219 static struct slist
*gen_load_avs_llprefixlen(void);
220 static struct slist
*gen_load_radiotap_llprefixlen(void);
221 static struct slist
*gen_load_ppi_llprefixlen(void);
222 static void insert_compute_vloffsets(struct block
*);
223 static struct slist
*gen_llprefixlen(void);
224 static struct slist
*gen_off_macpl(void);
225 static int ethertype_to_ppptype(int);
226 static struct block
*gen_linktype(int);
227 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
228 static struct block
*gen_llc_linktype(int);
229 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
231 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
233 static struct block
*gen_ahostop(const u_char
*, int);
234 static struct block
*gen_ehostop(const u_char
*, int);
235 static struct block
*gen_fhostop(const u_char
*, int);
236 static struct block
*gen_thostop(const u_char
*, int);
237 static struct block
*gen_wlanhostop(const u_char
*, int);
238 static struct block
*gen_ipfchostop(const u_char
*, int);
239 static struct block
*gen_dnhostop(bpf_u_int32
, int);
240 static struct block
*gen_mpls_linktype(int);
241 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
243 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
246 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
248 static struct block
*gen_ipfrag(void);
249 static struct block
*gen_portatom(int, bpf_int32
);
250 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
252 static struct block
*gen_portatom6(int, bpf_int32
);
253 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
255 struct block
*gen_portop(int, int, int);
256 static struct block
*gen_port(int, int, int);
257 struct block
*gen_portrangeop(int, int, int, int);
258 static struct block
*gen_portrange(int, int, int, int);
260 struct block
*gen_portop6(int, int, int);
261 static struct block
*gen_port6(int, int, int);
262 struct block
*gen_portrangeop6(int, int, int, int);
263 static struct block
*gen_portrange6(int, int, int, int);
265 static int lookup_proto(const char *, int);
266 static struct block
*gen_protochain(int, int, int);
267 static struct block
*gen_proto(int, int, int);
268 static struct slist
*xfer_to_x(struct arth
*);
269 static struct slist
*xfer_to_a(struct arth
*);
270 static struct block
*gen_mac_multicast(int);
271 static struct block
*gen_len(int, int);
272 static struct block
*gen_check_802_11_data_frame(void);
274 static struct block
*gen_ppi_dlt_check(void);
275 static struct block
*gen_msg_abbrev(int type
);
286 /* XXX Round up to nearest long. */
287 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
289 /* XXX Round up to structure boundary. */
293 cp
= &chunks
[cur_chunk
];
294 if (n
> cp
->n_left
) {
295 ++cp
, k
= ++cur_chunk
;
297 bpf_error("out of memory");
298 size
= CHUNK0SIZE
<< k
;
299 cp
->m
= (void *)malloc(size
);
301 bpf_error("out of memory");
302 memset((char *)cp
->m
, 0, size
);
305 bpf_error("out of memory");
308 return (void *)((char *)cp
->m
+ cp
->n_left
);
317 for (i
= 0; i
< NCHUNKS
; ++i
)
318 if (chunks
[i
].m
!= NULL
) {
325 * A strdup whose allocations are freed after code generation is over.
329 register const char *s
;
331 int n
= strlen(s
) + 1;
332 char *cp
= newchunk(n
);
338 static inline struct block
*
344 p
= (struct block
*)newchunk(sizeof(*p
));
351 static inline struct slist
*
357 p
= (struct slist
*)newchunk(sizeof(*p
));
363 static struct block
*
367 struct block
*b
= new_block(BPF_RET
|BPF_K
);
376 bpf_error("syntax error in filter expression");
379 static bpf_u_int32 netmask
;
384 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
385 const char *buf
, int optimize
, bpf_u_int32 mask
);
388 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
389 const char *buf
, int optimize
, bpf_u_int32 mask
)
393 EnterCriticalSection(&g_PcapCompileCriticalSection
);
395 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
397 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
403 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
404 const char *buf
, int optimize
, bpf_u_int32 mask
)
407 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
408 const char *buf
, int optimize
, bpf_u_int32 mask
)
412 const char * volatile xbuf
= buf
;
420 if (setjmp(top_ctx
)) {
434 snaplen
= pcap_snapshot(p
);
436 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
437 "snaplen of 0 rejects all packets");
441 lex_init(xbuf
? xbuf
: "");
449 root
= gen_retblk(snaplen
);
451 if (optimize
&& !no_optimize
) {
454 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
455 bpf_error("expression rejects all packets");
457 program
->bf_insns
= icode_to_fcode(root
, &len
);
458 program
->bf_len
= len
;
466 * entry point for using the compiler with no pcap open
467 * pass in all the stuff that is needed explicitly instead.
470 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
471 struct bpf_program
*program
,
472 const char *buf
, int optimize
, bpf_u_int32 mask
)
477 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
480 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
486 * Clean up a "struct bpf_program" by freeing all the memory allocated
490 pcap_freecode(struct bpf_program
*program
)
493 if (program
->bf_insns
!= NULL
) {
494 free((char *)program
->bf_insns
);
495 program
->bf_insns
= NULL
;
500 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
501 * which of the jt and jf fields has been resolved and which is a pointer
502 * back to another unresolved block (or nil). At least one of the fields
503 * in each block is already resolved.
506 backpatch(list
, target
)
507 struct block
*list
, *target
;
524 * Merge the lists in b0 and b1, using the 'sense' field to indicate
525 * which of jt and jf is the link.
529 struct block
*b0
, *b1
;
531 register struct block
**p
= &b0
;
533 /* Find end of list. */
535 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
537 /* Concatenate the lists. */
545 struct block
*ppi_dlt_check
;
548 * Insert before the statements of the first (root) block any
549 * statements needed to load the lengths of any variable-length
550 * headers into registers.
552 * XXX - a fancier strategy would be to insert those before the
553 * statements of all blocks that use those lengths and that
554 * have no predecessors that use them, so that we only compute
555 * the lengths if we need them. There might be even better
556 * approaches than that.
558 * However, those strategies would be more complicated, and
559 * as we don't generate code to compute a length if the
560 * program has no tests that use the length, and as most
561 * tests will probably use those lengths, we would just
562 * postpone computing the lengths so that it's not done
563 * for tests that fail early, and it's not clear that's
566 insert_compute_vloffsets(p
->head
);
569 * For DLT_PPI captures, generate a check of the per-packet
570 * DLT value to make sure it's DLT_IEEE802_11.
572 ppi_dlt_check
= gen_ppi_dlt_check();
573 if (ppi_dlt_check
!= NULL
)
574 gen_and(ppi_dlt_check
, p
);
576 backpatch(p
, gen_retblk(snaplen
));
577 p
->sense
= !p
->sense
;
578 backpatch(p
, gen_retblk(0));
584 struct block
*b0
, *b1
;
586 backpatch(b0
, b1
->head
);
587 b0
->sense
= !b0
->sense
;
588 b1
->sense
= !b1
->sense
;
590 b1
->sense
= !b1
->sense
;
596 struct block
*b0
, *b1
;
598 b0
->sense
= !b0
->sense
;
599 backpatch(b0
, b1
->head
);
600 b0
->sense
= !b0
->sense
;
609 b
->sense
= !b
->sense
;
612 static struct block
*
613 gen_cmp(offrel
, offset
, size
, v
)
614 enum e_offrel offrel
;
618 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
621 static struct block
*
622 gen_cmp_gt(offrel
, offset
, size
, v
)
623 enum e_offrel offrel
;
627 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
630 static struct block
*
631 gen_cmp_ge(offrel
, offset
, size
, v
)
632 enum e_offrel offrel
;
636 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
639 static struct block
*
640 gen_cmp_lt(offrel
, offset
, size
, v
)
641 enum e_offrel offrel
;
645 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
648 static struct block
*
649 gen_cmp_le(offrel
, offset
, size
, v
)
650 enum e_offrel offrel
;
654 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
657 static struct block
*
658 gen_mcmp(offrel
, offset
, size
, v
, mask
)
659 enum e_offrel offrel
;
664 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
667 static struct block
*
668 gen_bcmp(offrel
, offset
, size
, v
)
669 enum e_offrel offrel
;
670 register u_int offset
, size
;
671 register const u_char
*v
;
673 register struct block
*b
, *tmp
;
677 register const u_char
*p
= &v
[size
- 4];
678 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
679 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
681 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
688 register const u_char
*p
= &v
[size
- 2];
689 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
691 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
698 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
707 * AND the field of size "size" at offset "offset" relative to the header
708 * specified by "offrel" with "mask", and compare it with the value "v"
709 * with the test specified by "jtype"; if "reverse" is true, the test
710 * should test the opposite of "jtype".
712 static struct block
*
713 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
714 enum e_offrel offrel
;
716 bpf_u_int32 offset
, size
, mask
, jtype
;
719 struct slist
*s
, *s2
;
722 s
= gen_load_a(offrel
, offset
, size
);
724 if (mask
!= 0xffffffff) {
725 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
730 b
= new_block(JMP(jtype
));
733 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
739 * Various code constructs need to know the layout of the data link
740 * layer. These variables give the necessary offsets from the beginning
741 * of the packet data.
745 * This is the offset of the beginning of the link-layer header from
746 * the beginning of the raw packet data.
748 * It's usually 0, except for 802.11 with a fixed-length radio header.
749 * (For 802.11 with a variable-length radio header, we have to generate
750 * code to compute that offset; off_ll is 0 in that case.)
755 * If there's a variable-length header preceding the link-layer header,
756 * "reg_off_ll" is the register number for a register containing the
757 * length of that header, and therefore the offset of the link-layer
758 * header from the beginning of the raw packet data. Otherwise,
759 * "reg_off_ll" is -1.
761 static int reg_off_ll
;
764 * This is the offset of the beginning of the MAC-layer header from
765 * the beginning of the link-layer header.
766 * It's usually 0, except for ATM LANE, where it's the offset, relative
767 * to the beginning of the raw packet data, of the Ethernet header.
769 static u_int off_mac
;
772 * This is the offset of the beginning of the MAC-layer payload,
773 * from the beginning of the raw packet data.
775 * I.e., it's the sum of the length of the link-layer header (without,
776 * for example, any 802.2 LLC header, so it's the MAC-layer
777 * portion of that header), plus any prefix preceding the
780 static u_int off_macpl
;
783 * This is 1 if the offset of the beginning of the MAC-layer payload
784 * from the beginning of the link-layer header is variable-length.
786 static int off_macpl_is_variable
;
789 * If the link layer has variable_length headers, "reg_off_macpl"
790 * is the register number for a register containing the length of the
791 * link-layer header plus the length of any variable-length header
792 * preceding the link-layer header. Otherwise, "reg_off_macpl"
795 static int reg_off_macpl
;
798 * "off_linktype" is the offset to information in the link-layer header
799 * giving the packet type. This offset is relative to the beginning
800 * of the link-layer header (i.e., it doesn't include off_ll).
802 * For Ethernet, it's the offset of the Ethernet type field.
804 * For link-layer types that always use 802.2 headers, it's the
805 * offset of the LLC header.
807 * For PPP, it's the offset of the PPP type field.
809 * For Cisco HDLC, it's the offset of the CHDLC type field.
811 * For BSD loopback, it's the offset of the AF_ value.
813 * For Linux cooked sockets, it's the offset of the type field.
815 * It's set to -1 for no encapsulation, in which case, IP is assumed.
817 static u_int off_linktype
;
820 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
821 * checks to check the PPP header, assumed to follow a LAN-style link-
822 * layer header and a PPPoE session header.
824 static int is_pppoes
= 0;
827 * TRUE if the link layer includes an ATM pseudo-header.
829 static int is_atm
= 0;
832 * TRUE if "lane" appeared in the filter; it causes us to generate
833 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
835 static int is_lane
= 0;
838 * These are offsets for the ATM pseudo-header.
840 static u_int off_vpi
;
841 static u_int off_vci
;
842 static u_int off_proto
;
845 * These are offsets for the MTP2 fields.
850 * These are offsets for the MTP3 fields.
852 static u_int off_sio
;
853 static u_int off_opc
;
854 static u_int off_dpc
;
855 static u_int off_sls
;
858 * This is the offset of the first byte after the ATM pseudo_header,
859 * or -1 if there is no ATM pseudo-header.
861 static u_int off_payload
;
864 * These are offsets to the beginning of the network-layer header.
865 * They are relative to the beginning of the MAC-layer payload (i.e.,
866 * they don't include off_ll or off_macpl).
868 * If the link layer never uses 802.2 LLC:
870 * "off_nl" and "off_nl_nosnap" are the same.
872 * If the link layer always uses 802.2 LLC:
874 * "off_nl" is the offset if there's a SNAP header following
877 * "off_nl_nosnap" is the offset if there's no SNAP header.
879 * If the link layer is Ethernet:
881 * "off_nl" is the offset if the packet is an Ethernet II packet
882 * (we assume no 802.3+802.2+SNAP);
884 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
885 * with an 802.2 header following it.
888 static u_int off_nl_nosnap
;
896 linktype
= pcap_datalink(p
);
898 pcap_fddipad
= p
->fddipad
;
902 * Assume it's not raw ATM with a pseudo-header, for now.
913 * And that we're not doing PPPoE.
918 * And assume we're not doing SS7.
927 * Also assume it's not 802.11.
931 off_macpl_is_variable
= 0;
935 label_stack_depth
= 0;
945 off_nl
= 0; /* XXX in reality, variable! */
946 off_nl_nosnap
= 0; /* no 802.2 LLC */
949 case DLT_ARCNET_LINUX
:
952 off_nl
= 0; /* XXX in reality, variable! */
953 off_nl_nosnap
= 0; /* no 802.2 LLC */
958 off_macpl
= 14; /* Ethernet header length */
959 off_nl
= 0; /* Ethernet II */
960 off_nl_nosnap
= 3; /* 802.3+802.2 */
965 * SLIP doesn't have a link level type. The 16 byte
966 * header is hacked into our SLIP driver.
971 off_nl_nosnap
= 0; /* no 802.2 LLC */
975 /* XXX this may be the same as the DLT_PPP_BSDOS case */
980 off_nl_nosnap
= 0; /* no 802.2 LLC */
988 off_nl_nosnap
= 0; /* no 802.2 LLC */
995 off_nl_nosnap
= 0; /* no 802.2 LLC */
1000 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1001 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1005 off_nl_nosnap
= 0; /* no 802.2 LLC */
1010 * This does no include the Ethernet header, and
1011 * only covers session state.
1016 off_nl_nosnap
= 0; /* no 802.2 LLC */
1023 off_nl_nosnap
= 0; /* no 802.2 LLC */
1028 * FDDI doesn't really have a link-level type field.
1029 * We set "off_linktype" to the offset of the LLC header.
1031 * To check for Ethernet types, we assume that SSAP = SNAP
1032 * is being used and pick out the encapsulated Ethernet type.
1033 * XXX - should we generate code to check for SNAP?
1037 off_linktype
+= pcap_fddipad
;
1039 off_macpl
= 13; /* FDDI MAC header length */
1041 off_macpl
+= pcap_fddipad
;
1043 off_nl
= 8; /* 802.2+SNAP */
1044 off_nl_nosnap
= 3; /* 802.2 */
1049 * Token Ring doesn't really have a link-level type field.
1050 * We set "off_linktype" to the offset of the LLC header.
1052 * To check for Ethernet types, we assume that SSAP = SNAP
1053 * is being used and pick out the encapsulated Ethernet type.
1054 * XXX - should we generate code to check for SNAP?
1056 * XXX - the header is actually variable-length.
1057 * Some various Linux patched versions gave 38
1058 * as "off_linktype" and 40 as "off_nl"; however,
1059 * if a token ring packet has *no* routing
1060 * information, i.e. is not source-routed, the correct
1061 * values are 20 and 22, as they are in the vanilla code.
1063 * A packet is source-routed iff the uppermost bit
1064 * of the first byte of the source address, at an
1065 * offset of 8, has the uppermost bit set. If the
1066 * packet is source-routed, the total number of bytes
1067 * of routing information is 2 plus bits 0x1F00 of
1068 * the 16-bit value at an offset of 14 (shifted right
1069 * 8 - figure out which byte that is).
1072 off_macpl
= 14; /* Token Ring MAC header length */
1073 off_nl
= 8; /* 802.2+SNAP */
1074 off_nl_nosnap
= 3; /* 802.2 */
1077 case DLT_IEEE802_11
:
1078 case DLT_PRISM_HEADER
:
1079 case DLT_IEEE802_11_RADIO_AVS
:
1080 case DLT_IEEE802_11_RADIO
:
1082 * 802.11 doesn't really have a link-level type field.
1083 * We set "off_linktype" to the offset of the LLC header.
1085 * To check for Ethernet types, we assume that SSAP = SNAP
1086 * is being used and pick out the encapsulated Ethernet type.
1087 * XXX - should we generate code to check for SNAP?
1089 * We also handle variable-length radio headers here.
1090 * The Prism header is in theory variable-length, but in
1091 * practice it's always 144 bytes long. However, some
1092 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1093 * sometimes or always supply an AVS header, so we
1094 * have to check whether the radio header is a Prism
1095 * header or an AVS header, so, in practice, it's
1099 off_macpl
= 0; /* link-layer header is variable-length */
1100 off_macpl_is_variable
= 1;
1101 off_nl
= 8; /* 802.2+SNAP */
1102 off_nl_nosnap
= 3; /* 802.2 */
1107 * At the moment we treat PPI the same way that we treat
1108 * normal Radiotap encoded packets. The difference is in
1109 * the function that generates the code at the beginning
1110 * to compute the header length. Since this code generator
1111 * of PPI supports bare 802.11 encapsulation only (i.e.
1112 * the encapsulated DLT should be DLT_IEEE802_11) we
1113 * generate code to check for this too.
1116 off_macpl
= 0; /* link-layer header is variable-length */
1117 off_macpl_is_variable
= 1;
1118 off_nl
= 8; /* 802.2+SNAP */
1119 off_nl_nosnap
= 3; /* 802.2 */
1122 case DLT_ATM_RFC1483
:
1123 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1125 * assume routed, non-ISO PDUs
1126 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1128 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1129 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1130 * latter would presumably be treated the way PPPoE
1131 * should be, so you can do "pppoe and udp port 2049"
1132 * or "pppoa and tcp port 80" and have it check for
1133 * PPPo{A,E} and a PPP protocol of IP and....
1136 off_macpl
= 0; /* packet begins with LLC header */
1137 off_nl
= 8; /* 802.2+SNAP */
1138 off_nl_nosnap
= 3; /* 802.2 */
1143 * Full Frontal ATM; you get AALn PDUs with an ATM
1147 off_vpi
= SUNATM_VPI_POS
;
1148 off_vci
= SUNATM_VCI_POS
;
1149 off_proto
= PROTO_POS
;
1150 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1151 off_payload
= SUNATM_PKT_BEGIN_POS
;
1152 off_linktype
= off_payload
;
1153 off_macpl
= off_payload
; /* if LLC-encapsulated */
1154 off_nl
= 8; /* 802.2+SNAP */
1155 off_nl_nosnap
= 3; /* 802.2 */
1164 off_nl_nosnap
= 0; /* no 802.2 LLC */
1167 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1171 off_nl_nosnap
= 0; /* no 802.2 LLC */
1176 * LocalTalk does have a 1-byte type field in the LLAP header,
1177 * but really it just indicates whether there is a "short" or
1178 * "long" DDP packet following.
1183 off_nl_nosnap
= 0; /* no 802.2 LLC */
1186 case DLT_IP_OVER_FC
:
1188 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1189 * link-level type field. We set "off_linktype" to the
1190 * offset of the LLC header.
1192 * To check for Ethernet types, we assume that SSAP = SNAP
1193 * is being used and pick out the encapsulated Ethernet type.
1194 * XXX - should we generate code to check for SNAP? RFC
1195 * 2625 says SNAP should be used.
1199 off_nl
= 8; /* 802.2+SNAP */
1200 off_nl_nosnap
= 3; /* 802.2 */
1205 * XXX - we should set this to handle SNAP-encapsulated
1206 * frames (NLPID of 0x80).
1211 off_nl_nosnap
= 0; /* no 802.2 LLC */
1215 * the only BPF-interesting FRF.16 frames are non-control frames;
1216 * Frame Relay has a variable length link-layer
1217 * so lets start with offset 4 for now and increments later on (FIXME);
1223 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1226 case DLT_APPLE_IP_OVER_IEEE1394
:
1230 off_nl_nosnap
= 0; /* no 802.2 LLC */
1233 case DLT_LINUX_IRDA
:
1235 * Currently, only raw "link[N:M]" filtering is supported.
1245 * Currently, only raw "link[N:M]" filtering is supported.
1253 case DLT_SYMANTEC_FIREWALL
:
1256 off_nl
= 0; /* Ethernet II */
1257 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1260 #ifdef HAVE_NET_PFVAR_H
1263 off_macpl
= PFLOG_HDRLEN
;
1265 off_nl_nosnap
= 0; /* no 802.2 LLC */
1269 case DLT_JUNIPER_MFR
:
1270 case DLT_JUNIPER_MLFR
:
1271 case DLT_JUNIPER_MLPPP
:
1272 case DLT_JUNIPER_PPP
:
1273 case DLT_JUNIPER_CHDLC
:
1274 case DLT_JUNIPER_FRELAY
:
1278 off_nl_nosnap
= -1; /* no 802.2 LLC */
1281 case DLT_JUNIPER_ATM1
:
1282 off_linktype
= 4; /* in reality variable between 4-8 */
1283 off_macpl
= 4; /* in reality variable between 4-8 */
1288 case DLT_JUNIPER_ATM2
:
1289 off_linktype
= 8; /* in reality variable between 8-12 */
1290 off_macpl
= 8; /* in reality variable between 8-12 */
1295 /* frames captured on a Juniper PPPoE service PIC
1296 * contain raw ethernet frames */
1297 case DLT_JUNIPER_PPPOE
:
1298 case DLT_JUNIPER_ETHER
:
1301 off_nl
= 18; /* Ethernet II */
1302 off_nl_nosnap
= 21; /* 802.3+802.2 */
1305 case DLT_JUNIPER_PPPOE_ATM
:
1309 off_nl_nosnap
= -1; /* no 802.2 LLC */
1312 case DLT_JUNIPER_GGSN
:
1316 off_nl_nosnap
= -1; /* no 802.2 LLC */
1319 case DLT_JUNIPER_ES
:
1321 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1322 off_nl
= -1; /* not really a network layer but raw IP addresses */
1323 off_nl_nosnap
= -1; /* no 802.2 LLC */
1326 case DLT_JUNIPER_MONITOR
:
1329 off_nl
= 0; /* raw IP/IP6 header */
1330 off_nl_nosnap
= -1; /* no 802.2 LLC */
1333 case DLT_JUNIPER_SERVICES
:
1335 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1336 off_nl
= -1; /* L3 proto location dep. on cookie type */
1337 off_nl_nosnap
= -1; /* no 802.2 LLC */
1340 case DLT_JUNIPER_VP
:
1347 case DLT_JUNIPER_ST
:
1354 case DLT_JUNIPER_ISM
:
1373 case DLT_MTP2_WITH_PHDR
:
1406 case DLT_LINUX_LAPD
:
1408 * Currently, only raw "link[N:M]" filtering is supported.
1418 * Currently, only raw "link[N:M]" filtering is supported.
1426 case DLT_BLUETOOTH_HCI_H4
:
1428 * Currently, only raw "link[N:M]" filtering is supported.
1438 * Currently, only raw "link[N:M]" filtering is supported.
1448 * Currently, only raw "link[N:M]" filtering is supported.
1456 case DLT_IEEE802_15_4_LINUX
:
1458 * Currently, only raw "link[N:M]" filtering is supported.
1466 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1468 * Currently, only raw "link[N:M]" filtering is supported.
1476 case DLT_IEEE802_15_4
:
1478 * Currently, only raw "link[N:M]" filtering is supported.
1488 * Currently, only raw "link[N:M]" filtering is supported.
1498 * Currently, only raw "link[N:M]" filtering is supported.
1508 * Currently, only raw "link[N:M]" filtering is supported.
1516 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1518 * Currently, only raw "link[N:M]" filtering is supported.
1528 * Currently, only raw "link[N:M]" filtering is supported.
1530 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1532 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1533 off_nl_nosnap
= -1; /* no 802.2 LLC */
1534 off_mac
= 1; /* step over the kiss length byte */
1537 case DLT_IEEE802_15_4_NONASK_PHY
:
1539 * Currently, only raw "link[N:M]" filtering is supported.
1549 * Currently, only raw "link[N:M]" filtering is supported.
1557 case DLT_USB_LINUX_MMAPPED
:
1559 * Currently, only raw "link[N:M]" filtering is supported.
1567 case DLT_CAN_SOCKETCAN
:
1569 * Currently, only raw "link[N:M]" filtering is supported.
1579 off_macpl
= 24; /* ipnet header length */
1584 bpf_error("unknown data link type %d", linktype
);
1589 * Load a value relative to the beginning of the link-layer header.
1590 * The link-layer header doesn't necessarily begin at the beginning
1591 * of the packet data; there might be a variable-length prefix containing
1592 * radio information.
1594 static struct slist
*
1595 gen_load_llrel(offset
, size
)
1598 struct slist
*s
, *s2
;
1600 s
= gen_llprefixlen();
1603 * If "s" is non-null, it has code to arrange that the X register
1604 * contains the length of the prefix preceding the link-layer
1607 * Otherwise, the length of the prefix preceding the link-layer
1608 * header is "off_ll".
1612 * There's a variable-length prefix preceding the
1613 * link-layer header. "s" points to a list of statements
1614 * that put the length of that prefix into the X register.
1615 * do an indirect load, to use the X register as an offset.
1617 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1622 * There is no variable-length header preceding the
1623 * link-layer header; add in off_ll, which, if there's
1624 * a fixed-length header preceding the link-layer header,
1625 * is the length of that header.
1627 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1628 s
->s
.k
= offset
+ off_ll
;
1634 * Load a value relative to the beginning of the MAC-layer payload.
1636 static struct slist
*
1637 gen_load_macplrel(offset
, size
)
1640 struct slist
*s
, *s2
;
1642 s
= gen_off_macpl();
1645 * If s is non-null, the offset of the MAC-layer payload is
1646 * variable, and s points to a list of instructions that
1647 * arrange that the X register contains that offset.
1649 * Otherwise, the offset of the MAC-layer payload is constant,
1650 * and is in off_macpl.
1654 * The offset of the MAC-layer payload is in the X
1655 * register. Do an indirect load, to use the X register
1658 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1663 * The offset of the MAC-layer payload is constant,
1664 * and is in off_macpl; load the value at that offset
1665 * plus the specified offset.
1667 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1668 s
->s
.k
= off_macpl
+ offset
;
1674 * Load a value relative to the beginning of the specified header.
1676 static struct slist
*
1677 gen_load_a(offrel
, offset
, size
)
1678 enum e_offrel offrel
;
1681 struct slist
*s
, *s2
;
1686 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1691 s
= gen_load_llrel(offset
, size
);
1695 s
= gen_load_macplrel(offset
, size
);
1699 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1703 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1708 * Load the X register with the length of the IPv4 header
1709 * (plus the offset of the link-layer header, if it's
1710 * preceded by a variable-length header such as a radio
1711 * header), in bytes.
1713 s
= gen_loadx_iphdrlen();
1716 * Load the item at {offset of the MAC-layer payload} +
1717 * {offset, relative to the start of the MAC-layer
1718 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1719 * {specified offset}.
1721 * (If the offset of the MAC-layer payload is variable,
1722 * it's included in the value in the X register, and
1725 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1726 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1731 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1742 * Generate code to load into the X register the sum of the length of
1743 * the IPv4 header and any variable-length header preceding the link-layer
1746 static struct slist
*
1747 gen_loadx_iphdrlen()
1749 struct slist
*s
, *s2
;
1751 s
= gen_off_macpl();
1754 * There's a variable-length prefix preceding the
1755 * link-layer header, or the link-layer header is itself
1756 * variable-length. "s" points to a list of statements
1757 * that put the offset of the MAC-layer payload into
1760 * The 4*([k]&0xf) addressing mode can't be used, as we
1761 * don't have a constant offset, so we have to load the
1762 * value in question into the A register and add to it
1763 * the value from the X register.
1765 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1768 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1771 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1776 * The A register now contains the length of the
1777 * IP header. We need to add to it the offset of
1778 * the MAC-layer payload, which is still in the X
1779 * register, and move the result into the X register.
1781 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1782 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1785 * There is no variable-length header preceding the
1786 * link-layer header, and the link-layer header is
1787 * fixed-length; load the length of the IPv4 header,
1788 * which is at an offset of off_nl from the beginning
1789 * of the MAC-layer payload, and thus at an offset
1790 * of off_mac_pl + off_nl from the beginning of the
1793 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1794 s
->s
.k
= off_macpl
+ off_nl
;
1799 static struct block
*
1806 s
= new_stmt(BPF_LD
|BPF_IMM
);
1808 b
= new_block(JMP(BPF_JEQ
));
1814 static inline struct block
*
1817 return gen_uncond(1);
1820 static inline struct block
*
1823 return gen_uncond(0);
1827 * Byte-swap a 32-bit number.
1828 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1829 * big-endian platforms.)
1831 #define SWAPLONG(y) \
1832 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1835 * Generate code to match a particular packet type.
1837 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1838 * value, if <= ETHERMTU. We use that to determine whether to
1839 * match the type/length field or to check the type/length field for
1840 * a value <= ETHERMTU to see whether it's a type field and then do
1841 * the appropriate test.
1843 static struct block
*
1844 gen_ether_linktype(proto
)
1847 struct block
*b0
, *b1
;
1853 case LLCSAP_NETBEUI
:
1855 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1856 * so we check the DSAP and SSAP.
1858 * LLCSAP_IP checks for IP-over-802.2, rather
1859 * than IP-over-Ethernet or IP-over-SNAP.
1861 * XXX - should we check both the DSAP and the
1862 * SSAP, like this, or should we check just the
1863 * DSAP, as we do for other types <= ETHERMTU
1864 * (i.e., other SAP values)?
1866 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1868 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1869 ((proto
<< 8) | proto
));
1877 * Ethernet_II frames, which are Ethernet
1878 * frames with a frame type of ETHERTYPE_IPX;
1880 * Ethernet_802.3 frames, which are 802.3
1881 * frames (i.e., the type/length field is
1882 * a length field, <= ETHERMTU, rather than
1883 * a type field) with the first two bytes
1884 * after the Ethernet/802.3 header being
1887 * Ethernet_802.2 frames, which are 802.3
1888 * frames with an 802.2 LLC header and
1889 * with the IPX LSAP as the DSAP in the LLC
1892 * Ethernet_SNAP frames, which are 802.3
1893 * frames with an LLC header and a SNAP
1894 * header and with an OUI of 0x000000
1895 * (encapsulated Ethernet) and a protocol
1896 * ID of ETHERTYPE_IPX in the SNAP header.
1898 * XXX - should we generate the same code both
1899 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1903 * This generates code to check both for the
1904 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1906 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1907 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1911 * Now we add code to check for SNAP frames with
1912 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1914 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1918 * Now we generate code to check for 802.3
1919 * frames in general.
1921 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1925 * Now add the check for 802.3 frames before the
1926 * check for Ethernet_802.2 and Ethernet_802.3,
1927 * as those checks should only be done on 802.3
1928 * frames, not on Ethernet frames.
1933 * Now add the check for Ethernet_II frames, and
1934 * do that before checking for the other frame
1937 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1938 (bpf_int32
)ETHERTYPE_IPX
);
1942 case ETHERTYPE_ATALK
:
1943 case ETHERTYPE_AARP
:
1945 * EtherTalk (AppleTalk protocols on Ethernet link
1946 * layer) may use 802.2 encapsulation.
1950 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1951 * we check for an Ethernet type field less than
1952 * 1500, which means it's an 802.3 length field.
1954 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1958 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1959 * SNAP packets with an organization code of
1960 * 0x080007 (Apple, for Appletalk) and a protocol
1961 * type of ETHERTYPE_ATALK (Appletalk).
1963 * 802.2-encapsulated ETHERTYPE_AARP packets are
1964 * SNAP packets with an organization code of
1965 * 0x000000 (encapsulated Ethernet) and a protocol
1966 * type of ETHERTYPE_AARP (Appletalk ARP).
1968 if (proto
== ETHERTYPE_ATALK
)
1969 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1970 else /* proto == ETHERTYPE_AARP */
1971 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1975 * Check for Ethernet encapsulation (Ethertalk
1976 * phase 1?); we just check for the Ethernet
1979 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1985 if (proto
<= ETHERMTU
) {
1987 * This is an LLC SAP value, so the frames
1988 * that match would be 802.2 frames.
1989 * Check that the frame is an 802.2 frame
1990 * (i.e., that the length/type field is
1991 * a length field, <= ETHERMTU) and
1992 * then check the DSAP.
1994 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1996 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
2002 * This is an Ethernet type, so compare
2003 * the length/type field with it (if
2004 * the frame is an 802.2 frame, the length
2005 * field will be <= ETHERMTU, and, as
2006 * "proto" is > ETHERMTU, this test
2007 * will fail and the frame won't match,
2008 * which is what we want).
2010 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2017 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2018 * or IPv6 then we have an error.
2020 static struct block
*
2021 gen_ipnet_linktype(proto
)
2027 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2028 (bpf_int32
)IPH_AF_INET
);
2031 case ETHERTYPE_IPV6
:
2032 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2033 (bpf_int32
)IPH_AF_INET6
);
2044 * Generate code to match a particular packet type.
2046 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2047 * value, if <= ETHERMTU. We use that to determine whether to
2048 * match the type field or to check the type field for the special
2049 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2051 static struct block
*
2052 gen_linux_sll_linktype(proto
)
2055 struct block
*b0
, *b1
;
2061 case LLCSAP_NETBEUI
:
2063 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2064 * so we check the DSAP and SSAP.
2066 * LLCSAP_IP checks for IP-over-802.2, rather
2067 * than IP-over-Ethernet or IP-over-SNAP.
2069 * XXX - should we check both the DSAP and the
2070 * SSAP, like this, or should we check just the
2071 * DSAP, as we do for other types <= ETHERMTU
2072 * (i.e., other SAP values)?
2074 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2075 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2076 ((proto
<< 8) | proto
));
2082 * Ethernet_II frames, which are Ethernet
2083 * frames with a frame type of ETHERTYPE_IPX;
2085 * Ethernet_802.3 frames, which have a frame
2086 * type of LINUX_SLL_P_802_3;
2088 * Ethernet_802.2 frames, which are 802.3
2089 * frames with an 802.2 LLC header (i.e, have
2090 * a frame type of LINUX_SLL_P_802_2) and
2091 * with the IPX LSAP as the DSAP in the LLC
2094 * Ethernet_SNAP frames, which are 802.3
2095 * frames with an LLC header and a SNAP
2096 * header and with an OUI of 0x000000
2097 * (encapsulated Ethernet) and a protocol
2098 * ID of ETHERTYPE_IPX in the SNAP header.
2100 * First, do the checks on LINUX_SLL_P_802_2
2101 * frames; generate the check for either
2102 * Ethernet_802.2 or Ethernet_SNAP frames, and
2103 * then put a check for LINUX_SLL_P_802_2 frames
2106 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2107 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2109 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2113 * Now check for 802.3 frames and OR that with
2114 * the previous test.
2116 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2120 * Now add the check for Ethernet_II frames, and
2121 * do that before checking for the other frame
2124 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2125 (bpf_int32
)ETHERTYPE_IPX
);
2129 case ETHERTYPE_ATALK
:
2130 case ETHERTYPE_AARP
:
2132 * EtherTalk (AppleTalk protocols on Ethernet link
2133 * layer) may use 802.2 encapsulation.
2137 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2138 * we check for the 802.2 protocol type in the
2139 * "Ethernet type" field.
2141 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2144 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2145 * SNAP packets with an organization code of
2146 * 0x080007 (Apple, for Appletalk) and a protocol
2147 * type of ETHERTYPE_ATALK (Appletalk).
2149 * 802.2-encapsulated ETHERTYPE_AARP packets are
2150 * SNAP packets with an organization code of
2151 * 0x000000 (encapsulated Ethernet) and a protocol
2152 * type of ETHERTYPE_AARP (Appletalk ARP).
2154 if (proto
== ETHERTYPE_ATALK
)
2155 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2156 else /* proto == ETHERTYPE_AARP */
2157 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2161 * Check for Ethernet encapsulation (Ethertalk
2162 * phase 1?); we just check for the Ethernet
2165 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2171 if (proto
<= ETHERMTU
) {
2173 * This is an LLC SAP value, so the frames
2174 * that match would be 802.2 frames.
2175 * Check for the 802.2 protocol type
2176 * in the "Ethernet type" field, and
2177 * then check the DSAP.
2179 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2181 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2187 * This is an Ethernet type, so compare
2188 * the length/type field with it (if
2189 * the frame is an 802.2 frame, the length
2190 * field will be <= ETHERMTU, and, as
2191 * "proto" is > ETHERMTU, this test
2192 * will fail and the frame won't match,
2193 * which is what we want).
2195 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2201 static struct slist
*
2202 gen_load_prism_llprefixlen()
2204 struct slist
*s1
, *s2
;
2205 struct slist
*sjeq_avs_cookie
;
2206 struct slist
*sjcommon
;
2209 * This code is not compatible with the optimizer, as
2210 * we are generating jmp instructions within a normal
2211 * slist of instructions
2216 * Generate code to load the length of the radio header into
2217 * the register assigned to hold that length, if one has been
2218 * assigned. (If one hasn't been assigned, no code we've
2219 * generated uses that prefix, so we don't need to generate any
2222 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2223 * or always use the AVS header rather than the Prism header.
2224 * We load a 4-byte big-endian value at the beginning of the
2225 * raw packet data, and see whether, when masked with 0xFFFFF000,
2226 * it's equal to 0x80211000. If so, that indicates that it's
2227 * an AVS header (the masked-out bits are the version number).
2228 * Otherwise, it's a Prism header.
2230 * XXX - the Prism header is also, in theory, variable-length,
2231 * but no known software generates headers that aren't 144
2234 if (reg_off_ll
!= -1) {
2238 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2242 * AND it with 0xFFFFF000.
2244 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2245 s2
->s
.k
= 0xFFFFF000;
2249 * Compare with 0x80211000.
2251 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2252 sjeq_avs_cookie
->s
.k
= 0x80211000;
2253 sappend(s1
, sjeq_avs_cookie
);
2258 * The 4 bytes at an offset of 4 from the beginning of
2259 * the AVS header are the length of the AVS header.
2260 * That field is big-endian.
2262 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2265 sjeq_avs_cookie
->s
.jt
= s2
;
2268 * Now jump to the code to allocate a register
2269 * into which to save the header length and
2270 * store the length there. (The "jump always"
2271 * instruction needs to have the k field set;
2272 * it's added to the PC, so, as we're jumping
2273 * over a single instruction, it should be 1.)
2275 sjcommon
= new_stmt(JMP(BPF_JA
));
2277 sappend(s1
, sjcommon
);
2280 * Now for the code that handles the Prism header.
2281 * Just load the length of the Prism header (144)
2282 * into the A register. Have the test for an AVS
2283 * header branch here if we don't have an AVS header.
2285 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2288 sjeq_avs_cookie
->s
.jf
= s2
;
2291 * Now allocate a register to hold that value and store
2292 * it. The code for the AVS header will jump here after
2293 * loading the length of the AVS header.
2295 s2
= new_stmt(BPF_ST
);
2296 s2
->s
.k
= reg_off_ll
;
2298 sjcommon
->s
.jf
= s2
;
2301 * Now move it into the X register.
2303 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2311 static struct slist
*
2312 gen_load_avs_llprefixlen()
2314 struct slist
*s1
, *s2
;
2317 * Generate code to load the length of the AVS header into
2318 * the register assigned to hold that length, if one has been
2319 * assigned. (If one hasn't been assigned, no code we've
2320 * generated uses that prefix, so we don't need to generate any
2323 if (reg_off_ll
!= -1) {
2325 * The 4 bytes at an offset of 4 from the beginning of
2326 * the AVS header are the length of the AVS header.
2327 * That field is big-endian.
2329 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2333 * Now allocate a register to hold that value and store
2336 s2
= new_stmt(BPF_ST
);
2337 s2
->s
.k
= reg_off_ll
;
2341 * Now move it into the X register.
2343 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2351 static struct slist
*
2352 gen_load_radiotap_llprefixlen()
2354 struct slist
*s1
, *s2
;
2357 * Generate code to load the length of the radiotap header into
2358 * the register assigned to hold that length, if one has been
2359 * assigned. (If one hasn't been assigned, no code we've
2360 * generated uses that prefix, so we don't need to generate any
2363 if (reg_off_ll
!= -1) {
2365 * The 2 bytes at offsets of 2 and 3 from the beginning
2366 * of the radiotap header are the length of the radiotap
2367 * header; unfortunately, it's little-endian, so we have
2368 * to load it a byte at a time and construct the value.
2372 * Load the high-order byte, at an offset of 3, shift it
2373 * left a byte, and put the result in the X register.
2375 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2377 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2380 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2384 * Load the next byte, at an offset of 2, and OR the
2385 * value from the X register into it.
2387 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2390 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2394 * Now allocate a register to hold that value and store
2397 s2
= new_stmt(BPF_ST
);
2398 s2
->s
.k
= reg_off_ll
;
2402 * Now move it into the X register.
2404 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2413 * At the moment we treat PPI as normal Radiotap encoded
2414 * packets. The difference is in the function that generates
2415 * the code at the beginning to compute the header length.
2416 * Since this code generator of PPI supports bare 802.11
2417 * encapsulation only (i.e. the encapsulated DLT should be
2418 * DLT_IEEE802_11) we generate code to check for this too;
2419 * that's done in finish_parse().
2421 static struct slist
*
2422 gen_load_ppi_llprefixlen()
2424 struct slist
*s1
, *s2
;
2427 * Generate code to load the length of the radiotap header
2428 * into the register assigned to hold that length, if one has
2431 if (reg_off_ll
!= -1) {
2433 * The 2 bytes at offsets of 2 and 3 from the beginning
2434 * of the radiotap header are the length of the radiotap
2435 * header; unfortunately, it's little-endian, so we have
2436 * to load it a byte at a time and construct the value.
2440 * Load the high-order byte, at an offset of 3, shift it
2441 * left a byte, and put the result in the X register.
2443 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2445 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2448 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2452 * Load the next byte, at an offset of 2, and OR the
2453 * value from the X register into it.
2455 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2458 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2462 * Now allocate a register to hold that value and store
2465 s2
= new_stmt(BPF_ST
);
2466 s2
->s
.k
= reg_off_ll
;
2470 * Now move it into the X register.
2472 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2481 * Load a value relative to the beginning of the link-layer header after the 802.11
2482 * header, i.e. LLC_SNAP.
2483 * The link-layer header doesn't necessarily begin at the beginning
2484 * of the packet data; there might be a variable-length prefix containing
2485 * radio information.
2487 static struct slist
*
2488 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2491 struct slist
*sjset_data_frame_1
;
2492 struct slist
*sjset_data_frame_2
;
2493 struct slist
*sjset_qos
;
2494 struct slist
*sjset_radiotap_flags
;
2495 struct slist
*sjset_radiotap_tsft
;
2496 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2497 struct slist
*s_roundup
;
2499 if (reg_off_macpl
== -1) {
2501 * No register has been assigned to the offset of
2502 * the MAC-layer payload, which means nobody needs
2503 * it; don't bother computing it - just return
2504 * what we already have.
2510 * This code is not compatible with the optimizer, as
2511 * we are generating jmp instructions within a normal
2512 * slist of instructions
2517 * If "s" is non-null, it has code to arrange that the X register
2518 * contains the length of the prefix preceding the link-layer
2521 * Otherwise, the length of the prefix preceding the link-layer
2522 * header is "off_ll".
2526 * There is no variable-length header preceding the
2527 * link-layer header.
2529 * Load the length of the fixed-length prefix preceding
2530 * the link-layer header (if any) into the X register,
2531 * and store it in the reg_off_macpl register.
2532 * That length is off_ll.
2534 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2539 * The X register contains the offset of the beginning of the
2540 * link-layer header; add 24, which is the minimum length
2541 * of the MAC header for a data frame, to that, and store it
2542 * in reg_off_macpl, and then load the Frame Control field,
2543 * which is at the offset in the X register, with an indexed load.
2545 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2547 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2550 s2
= new_stmt(BPF_ST
);
2551 s2
->s
.k
= reg_off_macpl
;
2554 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2559 * Check the Frame Control field to see if this is a data frame;
2560 * a data frame has the 0x08 bit (b3) in that field set and the
2561 * 0x04 bit (b2) clear.
2563 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2564 sjset_data_frame_1
->s
.k
= 0x08;
2565 sappend(s
, sjset_data_frame_1
);
2568 * If b3 is set, test b2, otherwise go to the first statement of
2569 * the rest of the program.
2571 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2572 sjset_data_frame_2
->s
.k
= 0x04;
2573 sappend(s
, sjset_data_frame_2
);
2574 sjset_data_frame_1
->s
.jf
= snext
;
2577 * If b2 is not set, this is a data frame; test the QoS bit.
2578 * Otherwise, go to the first statement of the rest of the
2581 sjset_data_frame_2
->s
.jt
= snext
;
2582 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2583 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2584 sappend(s
, sjset_qos
);
2587 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2589 * Otherwise, go to the first statement of the rest of the
2592 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2593 s2
->s
.k
= reg_off_macpl
;
2595 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2598 s2
= new_stmt(BPF_ST
);
2599 s2
->s
.k
= reg_off_macpl
;
2603 * If we have a radiotap header, look at it to see whether
2604 * there's Atheros padding between the MAC-layer header
2607 * Note: all of the fields in the radiotap header are
2608 * little-endian, so we byte-swap all of the values
2609 * we test against, as they will be loaded as big-endian
2612 if (linktype
== DLT_IEEE802_11_RADIO
) {
2614 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2615 * in the presence flag?
2617 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2621 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2622 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2623 sappend(s
, sjset_radiotap_flags
);
2626 * If not, skip all of this.
2628 sjset_radiotap_flags
->s
.jf
= snext
;
2631 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2633 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2634 new_stmt(JMP(BPF_JSET
));
2635 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2636 sappend(s
, sjset_radiotap_tsft
);
2639 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2640 * at an offset of 16 from the beginning of the raw packet
2641 * data (8 bytes for the radiotap header and 8 bytes for
2644 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2647 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2651 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2652 sjset_tsft_datapad
->s
.k
= 0x20;
2653 sappend(s
, sjset_tsft_datapad
);
2656 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2657 * at an offset of 8 from the beginning of the raw packet
2658 * data (8 bytes for the radiotap header).
2660 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2663 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2667 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2668 sjset_notsft_datapad
->s
.k
= 0x20;
2669 sappend(s
, sjset_notsft_datapad
);
2672 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2673 * set, round the length of the 802.11 header to
2674 * a multiple of 4. Do that by adding 3 and then
2675 * dividing by and multiplying by 4, which we do by
2678 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2679 s_roundup
->s
.k
= reg_off_macpl
;
2680 sappend(s
, s_roundup
);
2681 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2684 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2687 s2
= new_stmt(BPF_ST
);
2688 s2
->s
.k
= reg_off_macpl
;
2691 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2692 sjset_tsft_datapad
->s
.jf
= snext
;
2693 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2694 sjset_notsft_datapad
->s
.jf
= snext
;
2696 sjset_qos
->s
.jf
= snext
;
2702 insert_compute_vloffsets(b
)
2708 * For link-layer types that have a variable-length header
2709 * preceding the link-layer header, generate code to load
2710 * the offset of the link-layer header into the register
2711 * assigned to that offset, if any.
2715 case DLT_PRISM_HEADER
:
2716 s
= gen_load_prism_llprefixlen();
2719 case DLT_IEEE802_11_RADIO_AVS
:
2720 s
= gen_load_avs_llprefixlen();
2723 case DLT_IEEE802_11_RADIO
:
2724 s
= gen_load_radiotap_llprefixlen();
2728 s
= gen_load_ppi_llprefixlen();
2737 * For link-layer types that have a variable-length link-layer
2738 * header, generate code to load the offset of the MAC-layer
2739 * payload into the register assigned to that offset, if any.
2743 case DLT_IEEE802_11
:
2744 case DLT_PRISM_HEADER
:
2745 case DLT_IEEE802_11_RADIO_AVS
:
2746 case DLT_IEEE802_11_RADIO
:
2748 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2753 * If we have any offset-loading code, append all the
2754 * existing statements in the block to those statements,
2755 * and make the resulting list the list of statements
2759 sappend(s
, b
->stmts
);
2764 static struct block
*
2765 gen_ppi_dlt_check(void)
2767 struct slist
*s_load_dlt
;
2770 if (linktype
== DLT_PPI
)
2772 /* Create the statements that check for the DLT
2774 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2775 s_load_dlt
->s
.k
= 4;
2777 b
= new_block(JMP(BPF_JEQ
));
2779 b
->stmts
= s_load_dlt
;
2780 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2790 static struct slist
*
2791 gen_prism_llprefixlen(void)
2795 if (reg_off_ll
== -1) {
2797 * We haven't yet assigned a register for the length
2798 * of the radio header; allocate one.
2800 reg_off_ll
= alloc_reg();
2804 * Load the register containing the radio length
2805 * into the X register.
2807 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2808 s
->s
.k
= reg_off_ll
;
2812 static struct slist
*
2813 gen_avs_llprefixlen(void)
2817 if (reg_off_ll
== -1) {
2819 * We haven't yet assigned a register for the length
2820 * of the AVS header; allocate one.
2822 reg_off_ll
= alloc_reg();
2826 * Load the register containing the AVS length
2827 * into the X register.
2829 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2830 s
->s
.k
= reg_off_ll
;
2834 static struct slist
*
2835 gen_radiotap_llprefixlen(void)
2839 if (reg_off_ll
== -1) {
2841 * We haven't yet assigned a register for the length
2842 * of the radiotap header; allocate one.
2844 reg_off_ll
= alloc_reg();
2848 * Load the register containing the radiotap length
2849 * into the X register.
2851 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2852 s
->s
.k
= reg_off_ll
;
2857 * At the moment we treat PPI as normal Radiotap encoded
2858 * packets. The difference is in the function that generates
2859 * the code at the beginning to compute the header length.
2860 * Since this code generator of PPI supports bare 802.11
2861 * encapsulation only (i.e. the encapsulated DLT should be
2862 * DLT_IEEE802_11) we generate code to check for this too.
2864 static struct slist
*
2865 gen_ppi_llprefixlen(void)
2869 if (reg_off_ll
== -1) {
2871 * We haven't yet assigned a register for the length
2872 * of the radiotap header; allocate one.
2874 reg_off_ll
= alloc_reg();
2878 * Load the register containing the PPI length
2879 * into the X register.
2881 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2882 s
->s
.k
= reg_off_ll
;
2887 * Generate code to compute the link-layer header length, if necessary,
2888 * putting it into the X register, and to return either a pointer to a
2889 * "struct slist" for the list of statements in that code, or NULL if
2890 * no code is necessary.
2892 static struct slist
*
2893 gen_llprefixlen(void)
2897 case DLT_PRISM_HEADER
:
2898 return gen_prism_llprefixlen();
2900 case DLT_IEEE802_11_RADIO_AVS
:
2901 return gen_avs_llprefixlen();
2903 case DLT_IEEE802_11_RADIO
:
2904 return gen_radiotap_llprefixlen();
2907 return gen_ppi_llprefixlen();
2915 * Generate code to load the register containing the offset of the
2916 * MAC-layer payload into the X register; if no register for that offset
2917 * has been allocated, allocate it first.
2919 static struct slist
*
2924 if (off_macpl_is_variable
) {
2925 if (reg_off_macpl
== -1) {
2927 * We haven't yet assigned a register for the offset
2928 * of the MAC-layer payload; allocate one.
2930 reg_off_macpl
= alloc_reg();
2934 * Load the register containing the offset of the MAC-layer
2935 * payload into the X register.
2937 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2938 s
->s
.k
= reg_off_macpl
;
2942 * That offset isn't variable, so we don't need to
2943 * generate any code.
2950 * Map an Ethernet type to the equivalent PPP type.
2953 ethertype_to_ppptype(proto
)
2963 case ETHERTYPE_IPV6
:
2972 case ETHERTYPE_ATALK
:
2986 * I'm assuming the "Bridging PDU"s that go
2987 * over PPP are Spanning Tree Protocol
3001 * Generate code to match a particular packet type by matching the
3002 * link-layer type field or fields in the 802.2 LLC header.
3004 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3005 * value, if <= ETHERMTU.
3007 static struct block
*
3011 struct block
*b0
, *b1
, *b2
;
3013 /* are we checking MPLS-encapsulated packets? */
3014 if (label_stack_depth
> 0) {
3018 /* FIXME add other L3 proto IDs */
3019 return gen_mpls_linktype(Q_IP
);
3021 case ETHERTYPE_IPV6
:
3023 /* FIXME add other L3 proto IDs */
3024 return gen_mpls_linktype(Q_IPV6
);
3027 bpf_error("unsupported protocol over mpls");
3033 * Are we testing PPPoE packets?
3037 * The PPPoE session header is part of the
3038 * MAC-layer payload, so all references
3039 * should be relative to the beginning of
3044 * We use Ethernet protocol types inside libpcap;
3045 * map them to the corresponding PPP protocol types.
3047 proto
= ethertype_to_ppptype(proto
);
3048 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3054 return gen_ether_linktype(proto
);
3062 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3066 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3073 case DLT_IEEE802_11
:
3074 case DLT_PRISM_HEADER
:
3075 case DLT_IEEE802_11_RADIO_AVS
:
3076 case DLT_IEEE802_11_RADIO
:
3079 * Check that we have a data frame.
3081 b0
= gen_check_802_11_data_frame();
3084 * Now check for the specified link-layer type.
3086 b1
= gen_llc_linktype(proto
);
3094 * XXX - check for asynchronous frames, as per RFC 1103.
3096 return gen_llc_linktype(proto
);
3102 * XXX - check for LLC PDUs, as per IEEE 802.5.
3104 return gen_llc_linktype(proto
);
3108 case DLT_ATM_RFC1483
:
3110 case DLT_IP_OVER_FC
:
3111 return gen_llc_linktype(proto
);
3117 * If "is_lane" is set, check for a LANE-encapsulated
3118 * version of this protocol, otherwise check for an
3119 * LLC-encapsulated version of this protocol.
3121 * We assume LANE means Ethernet, not Token Ring.
3125 * Check that the packet doesn't begin with an
3126 * LE Control marker. (We've already generated
3129 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3134 * Now generate an Ethernet test.
3136 b1
= gen_ether_linktype(proto
);
3141 * Check for LLC encapsulation and then check the
3144 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3145 b1
= gen_llc_linktype(proto
);
3153 return gen_linux_sll_linktype(proto
);
3158 case DLT_SLIP_BSDOS
:
3161 * These types don't provide any type field; packets
3162 * are always IPv4 or IPv6.
3164 * XXX - for IPv4, check for a version number of 4, and,
3165 * for IPv6, check for a version number of 6?
3170 /* Check for a version number of 4. */
3171 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3173 case ETHERTYPE_IPV6
:
3174 /* Check for a version number of 6. */
3175 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3179 return gen_false(); /* always false */
3186 * Raw IPv4, so no type field.
3188 if (proto
== ETHERTYPE_IP
)
3189 return gen_true(); /* always true */
3191 /* Checking for something other than IPv4; always false */
3198 * Raw IPv6, so no type field.
3201 if (proto
== ETHERTYPE_IPV6
)
3202 return gen_true(); /* always true */
3205 /* Checking for something other than IPv6; always false */
3212 case DLT_PPP_SERIAL
:
3215 * We use Ethernet protocol types inside libpcap;
3216 * map them to the corresponding PPP protocol types.
3218 proto
= ethertype_to_ppptype(proto
);
3219 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3225 * We use Ethernet protocol types inside libpcap;
3226 * map them to the corresponding PPP protocol types.
3232 * Also check for Van Jacobson-compressed IP.
3233 * XXX - do this for other forms of PPP?
3235 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3236 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3238 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3243 proto
= ethertype_to_ppptype(proto
);
3244 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3254 * For DLT_NULL, the link-layer header is a 32-bit
3255 * word containing an AF_ value in *host* byte order,
3256 * and for DLT_ENC, the link-layer header begins
3257 * with a 32-bit work containing an AF_ value in
3260 * In addition, if we're reading a saved capture file,
3261 * the host byte order in the capture may not be the
3262 * same as the host byte order on this machine.
3264 * For DLT_LOOP, the link-layer header is a 32-bit
3265 * word containing an AF_ value in *network* byte order.
3267 * XXX - AF_ values may, unfortunately, be platform-
3268 * dependent; for example, FreeBSD's AF_INET6 is 24
3269 * whilst NetBSD's and OpenBSD's is 26.
3271 * This means that, when reading a capture file, just
3272 * checking for our AF_INET6 value won't work if the
3273 * capture file came from another OS.
3282 case ETHERTYPE_IPV6
:
3289 * Not a type on which we support filtering.
3290 * XXX - support those that have AF_ values
3291 * #defined on this platform, at least?
3296 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3298 * The AF_ value is in host byte order, but
3299 * the BPF interpreter will convert it to
3300 * network byte order.
3302 * If this is a save file, and it's from a
3303 * machine with the opposite byte order to
3304 * ours, we byte-swap the AF_ value.
3306 * Then we run it through "htonl()", and
3307 * generate code to compare against the result.
3309 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3310 bpf_pcap
->sf
.swapped
)
3311 proto
= SWAPLONG(proto
);
3312 proto
= htonl(proto
);
3314 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3316 #ifdef HAVE_NET_PFVAR_H
3319 * af field is host byte order in contrast to the rest of
3322 if (proto
== ETHERTYPE_IP
)
3323 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3324 BPF_B
, (bpf_int32
)AF_INET
));
3326 else if (proto
== ETHERTYPE_IPV6
)
3327 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3328 BPF_B
, (bpf_int32
)AF_INET6
));
3334 #endif /* HAVE_NET_PFVAR_H */
3337 case DLT_ARCNET_LINUX
:
3339 * XXX should we check for first fragment if the protocol
3348 case ETHERTYPE_IPV6
:
3349 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3350 (bpf_int32
)ARCTYPE_INET6
));
3354 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3355 (bpf_int32
)ARCTYPE_IP
);
3356 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3357 (bpf_int32
)ARCTYPE_IP_OLD
);
3362 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3363 (bpf_int32
)ARCTYPE_ARP
);
3364 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3365 (bpf_int32
)ARCTYPE_ARP_OLD
);
3369 case ETHERTYPE_REVARP
:
3370 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3371 (bpf_int32
)ARCTYPE_REVARP
));
3373 case ETHERTYPE_ATALK
:
3374 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3375 (bpf_int32
)ARCTYPE_ATALK
));
3382 case ETHERTYPE_ATALK
:
3392 * XXX - assumes a 2-byte Frame Relay header with
3393 * DLCI and flags. What if the address is longer?
3399 * Check for the special NLPID for IP.
3401 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3404 case ETHERTYPE_IPV6
:
3406 * Check for the special NLPID for IPv6.
3408 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3413 * Check for several OSI protocols.
3415 * Frame Relay packets typically have an OSI
3416 * NLPID at the beginning; we check for each
3419 * What we check for is the NLPID and a frame
3420 * control field of UI, i.e. 0x03 followed
3423 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3424 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3425 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3437 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3439 case DLT_JUNIPER_MFR
:
3440 case DLT_JUNIPER_MLFR
:
3441 case DLT_JUNIPER_MLPPP
:
3442 case DLT_JUNIPER_ATM1
:
3443 case DLT_JUNIPER_ATM2
:
3444 case DLT_JUNIPER_PPPOE
:
3445 case DLT_JUNIPER_PPPOE_ATM
:
3446 case DLT_JUNIPER_GGSN
:
3447 case DLT_JUNIPER_ES
:
3448 case DLT_JUNIPER_MONITOR
:
3449 case DLT_JUNIPER_SERVICES
:
3450 case DLT_JUNIPER_ETHER
:
3451 case DLT_JUNIPER_PPP
:
3452 case DLT_JUNIPER_FRELAY
:
3453 case DLT_JUNIPER_CHDLC
:
3454 case DLT_JUNIPER_VP
:
3455 case DLT_JUNIPER_ST
:
3456 case DLT_JUNIPER_ISM
:
3457 /* just lets verify the magic number for now -
3458 * on ATM we may have up to 6 different encapsulations on the wire
3459 * and need a lot of heuristics to figure out that the payload
3462 * FIXME encapsulation specific BPF_ filters
3464 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3467 return gen_ipnet_linktype(proto
);
3469 case DLT_LINUX_IRDA
:
3470 bpf_error("IrDA link-layer type filtering not implemented");
3473 bpf_error("DOCSIS link-layer type filtering not implemented");
3476 case DLT_MTP2_WITH_PHDR
:
3477 bpf_error("MTP2 link-layer type filtering not implemented");
3480 bpf_error("ERF link-layer type filtering not implemented");
3484 bpf_error("PFSYNC link-layer type filtering not implemented");
3487 case DLT_LINUX_LAPD
:
3488 bpf_error("LAPD link-layer type filtering not implemented");
3492 case DLT_USB_LINUX_MMAPPED
:
3493 bpf_error("USB link-layer type filtering not implemented");
3495 case DLT_BLUETOOTH_HCI_H4
:
3496 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3497 bpf_error("Bluetooth link-layer type filtering not implemented");
3500 case DLT_CAN_SOCKETCAN
:
3501 bpf_error("CAN link-layer type filtering not implemented");
3503 case DLT_IEEE802_15_4
:
3504 case DLT_IEEE802_15_4_LINUX
:
3505 case DLT_IEEE802_15_4_NONASK_PHY
:
3506 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3508 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3509 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3512 bpf_error("SITA link-layer type filtering not implemented");
3515 bpf_error("RAIF1 link-layer type filtering not implemented");
3518 bpf_error("IPMB link-layer type filtering not implemented");
3521 bpf_error("AX.25 link-layer type filtering not implemented");
3525 * All the types that have no encapsulation should either be
3526 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3527 * all packets are IP packets, or should be handled in some
3528 * special case, if none of them are (if some are and some
3529 * aren't, the lack of encapsulation is a problem, as we'd
3530 * have to find some other way of determining the packet type).
3532 * Therefore, if "off_linktype" is -1, there's an error.
3534 if (off_linktype
== (u_int
)-1)
3538 * Any type not handled above should always have an Ethernet
3539 * type at an offset of "off_linktype".
3541 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3545 * Check for an LLC SNAP packet with a given organization code and
3546 * protocol type; we check the entire contents of the 802.2 LLC and
3547 * snap headers, checking for DSAP and SSAP of SNAP and a control
3548 * field of 0x03 in the LLC header, and for the specified organization
3549 * code and protocol type in the SNAP header.
3551 static struct block
*
3552 gen_snap(orgcode
, ptype
)
3553 bpf_u_int32 orgcode
;
3556 u_char snapblock
[8];
3558 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3559 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3560 snapblock
[2] = 0x03; /* control = UI */
3561 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3562 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3563 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3564 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3565 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3566 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3570 * Generate code to match a particular packet type, for link-layer types
3571 * using 802.2 LLC headers.
3573 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3574 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3576 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3577 * value, if <= ETHERMTU. We use that to determine whether to
3578 * match the DSAP or both DSAP and LSAP or to check the OUI and
3579 * protocol ID in a SNAP header.
3581 static struct block
*
3582 gen_llc_linktype(proto
)
3586 * XXX - handle token-ring variable-length header.
3592 case LLCSAP_NETBEUI
:
3594 * XXX - should we check both the DSAP and the
3595 * SSAP, like this, or should we check just the
3596 * DSAP, as we do for other types <= ETHERMTU
3597 * (i.e., other SAP values)?
3599 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3600 ((proto
<< 8) | proto
));
3604 * XXX - are there ever SNAP frames for IPX on
3605 * non-Ethernet 802.x networks?
3607 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3608 (bpf_int32
)LLCSAP_IPX
);
3610 case ETHERTYPE_ATALK
:
3612 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3613 * SNAP packets with an organization code of
3614 * 0x080007 (Apple, for Appletalk) and a protocol
3615 * type of ETHERTYPE_ATALK (Appletalk).
3617 * XXX - check for an organization code of
3618 * encapsulated Ethernet as well?
3620 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3624 * XXX - we don't have to check for IPX 802.3
3625 * here, but should we check for the IPX Ethertype?
3627 if (proto
<= ETHERMTU
) {
3629 * This is an LLC SAP value, so check
3632 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3635 * This is an Ethernet type; we assume that it's
3636 * unlikely that it'll appear in the right place
3637 * at random, and therefore check only the
3638 * location that would hold the Ethernet type
3639 * in a SNAP frame with an organization code of
3640 * 0x000000 (encapsulated Ethernet).
3642 * XXX - if we were to check for the SNAP DSAP and
3643 * LSAP, as per XXX, and were also to check for an
3644 * organization code of 0x000000 (encapsulated
3645 * Ethernet), we'd do
3647 * return gen_snap(0x000000, proto);
3649 * here; for now, we don't, as per the above.
3650 * I don't know whether it's worth the extra CPU
3651 * time to do the right check or not.
3653 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3658 static struct block
*
3659 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3663 u_int src_off
, dst_off
;
3665 struct block
*b0
, *b1
;
3679 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3680 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3686 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3687 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3694 b0
= gen_linktype(proto
);
3695 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3701 static struct block
*
3702 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3703 struct in6_addr
*addr
;
3704 struct in6_addr
*mask
;
3706 u_int src_off
, dst_off
;
3708 struct block
*b0
, *b1
;
3723 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3724 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3730 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3731 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3738 /* this order is important */
3739 a
= (u_int32_t
*)addr
;
3740 m
= (u_int32_t
*)mask
;
3741 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3742 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3744 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3746 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3748 b0
= gen_linktype(proto
);
3754 static struct block
*
3755 gen_ehostop(eaddr
, dir
)
3756 register const u_char
*eaddr
;
3759 register struct block
*b0
, *b1
;
3763 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3766 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3769 b0
= gen_ehostop(eaddr
, Q_SRC
);
3770 b1
= gen_ehostop(eaddr
, Q_DST
);
3776 b0
= gen_ehostop(eaddr
, Q_SRC
);
3777 b1
= gen_ehostop(eaddr
, Q_DST
);
3786 * Like gen_ehostop, but for DLT_FDDI
3788 static struct block
*
3789 gen_fhostop(eaddr
, dir
)
3790 register const u_char
*eaddr
;
3793 struct block
*b0
, *b1
;
3798 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3800 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3805 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3807 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3811 b0
= gen_fhostop(eaddr
, Q_SRC
);
3812 b1
= gen_fhostop(eaddr
, Q_DST
);
3818 b0
= gen_fhostop(eaddr
, Q_SRC
);
3819 b1
= gen_fhostop(eaddr
, Q_DST
);
3828 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3830 static struct block
*
3831 gen_thostop(eaddr
, dir
)
3832 register const u_char
*eaddr
;
3835 register struct block
*b0
, *b1
;
3839 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3842 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3845 b0
= gen_thostop(eaddr
, Q_SRC
);
3846 b1
= gen_thostop(eaddr
, Q_DST
);
3852 b0
= gen_thostop(eaddr
, Q_SRC
);
3853 b1
= gen_thostop(eaddr
, Q_DST
);
3862 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3863 * various 802.11 + radio headers.
3865 static struct block
*
3866 gen_wlanhostop(eaddr
, dir
)
3867 register const u_char
*eaddr
;
3870 register struct block
*b0
, *b1
, *b2
;
3871 register struct slist
*s
;
3873 #ifdef ENABLE_WLAN_FILTERING_PATCH
3876 * We need to disable the optimizer because the optimizer is buggy
3877 * and wipes out some LD instructions generated by the below
3878 * code to validate the Frame Control bits
3881 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3888 * For control frames, there is no SA.
3890 * For management frames, SA is at an
3891 * offset of 10 from the beginning of
3894 * For data frames, SA is at an offset
3895 * of 10 from the beginning of the packet
3896 * if From DS is clear, at an offset of
3897 * 16 from the beginning of the packet
3898 * if From DS is set and To DS is clear,
3899 * and an offset of 24 from the beginning
3900 * of the packet if From DS is set and To DS
3905 * Generate the tests to be done for data frames
3908 * First, check for To DS set, i.e. check "link[1] & 0x01".
3910 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3911 b1
= new_block(JMP(BPF_JSET
));
3912 b1
->s
.k
= 0x01; /* To DS */
3916 * If To DS is set, the SA is at 24.
3918 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3922 * Now, check for To DS not set, i.e. check
3923 * "!(link[1] & 0x01)".
3925 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3926 b2
= new_block(JMP(BPF_JSET
));
3927 b2
->s
.k
= 0x01; /* To DS */
3932 * If To DS is not set, the SA is at 16.
3934 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3938 * Now OR together the last two checks. That gives
3939 * the complete set of checks for data frames with
3945 * Now check for From DS being set, and AND that with
3946 * the ORed-together checks.
3948 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3949 b1
= new_block(JMP(BPF_JSET
));
3950 b1
->s
.k
= 0x02; /* From DS */
3955 * Now check for data frames with From DS not set.
3957 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3958 b2
= new_block(JMP(BPF_JSET
));
3959 b2
->s
.k
= 0x02; /* From DS */
3964 * If From DS isn't set, the SA is at 10.
3966 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3970 * Now OR together the checks for data frames with
3971 * From DS not set and for data frames with From DS
3972 * set; that gives the checks done for data frames.
3977 * Now check for a data frame.
3978 * I.e, check "link[0] & 0x08".
3980 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3981 b1
= new_block(JMP(BPF_JSET
));
3986 * AND that with the checks done for data frames.
3991 * If the high-order bit of the type value is 0, this
3992 * is a management frame.
3993 * I.e, check "!(link[0] & 0x08)".
3995 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3996 b2
= new_block(JMP(BPF_JSET
));
4002 * For management frames, the SA is at 10.
4004 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4008 * OR that with the checks done for data frames.
4009 * That gives the checks done for management and
4015 * If the low-order bit of the type value is 1,
4016 * this is either a control frame or a frame
4017 * with a reserved type, and thus not a
4020 * I.e., check "!(link[0] & 0x04)".
4022 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4023 b1
= new_block(JMP(BPF_JSET
));
4029 * AND that with the checks for data and management
4039 * For control frames, there is no DA.
4041 * For management frames, DA is at an
4042 * offset of 4 from the beginning of
4045 * For data frames, DA is at an offset
4046 * of 4 from the beginning of the packet
4047 * if To DS is clear and at an offset of
4048 * 16 from the beginning of the packet
4053 * Generate the tests to be done for data frames.
4055 * First, check for To DS set, i.e. "link[1] & 0x01".
4057 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4058 b1
= new_block(JMP(BPF_JSET
));
4059 b1
->s
.k
= 0x01; /* To DS */
4063 * If To DS is set, the DA is at 16.
4065 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4069 * Now, check for To DS not set, i.e. check
4070 * "!(link[1] & 0x01)".
4072 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4073 b2
= new_block(JMP(BPF_JSET
));
4074 b2
->s
.k
= 0x01; /* To DS */
4079 * If To DS is not set, the DA is at 4.
4081 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4085 * Now OR together the last two checks. That gives
4086 * the complete set of checks for data frames.
4091 * Now check for a data frame.
4092 * I.e, check "link[0] & 0x08".
4094 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4095 b1
= new_block(JMP(BPF_JSET
));
4100 * AND that with the checks done for data frames.
4105 * If the high-order bit of the type value is 0, this
4106 * is a management frame.
4107 * I.e, check "!(link[0] & 0x08)".
4109 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4110 b2
= new_block(JMP(BPF_JSET
));
4116 * For management frames, the DA is at 4.
4118 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4122 * OR that with the checks done for data frames.
4123 * That gives the checks done for management and
4129 * If the low-order bit of the type value is 1,
4130 * this is either a control frame or a frame
4131 * with a reserved type, and thus not a
4134 * I.e., check "!(link[0] & 0x04)".
4136 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4137 b1
= new_block(JMP(BPF_JSET
));
4143 * AND that with the checks for data and management
4150 * XXX - add RA, TA, and BSSID keywords?
4153 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4157 * Not present in CTS or ACK control frames.
4159 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4160 IEEE80211_FC0_TYPE_MASK
);
4162 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4163 IEEE80211_FC0_SUBTYPE_MASK
);
4165 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4166 IEEE80211_FC0_SUBTYPE_MASK
);
4170 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4176 * Not present in control frames.
4178 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4179 IEEE80211_FC0_TYPE_MASK
);
4181 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4187 * Present only if the direction mask has both "From DS"
4188 * and "To DS" set. Neither control frames nor management
4189 * frames should have both of those set, so we don't
4190 * check the frame type.
4192 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4193 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4194 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4199 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4200 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4206 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4207 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4216 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4217 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4218 * as the RFC states.)
4220 static struct block
*
4221 gen_ipfchostop(eaddr
, dir
)
4222 register const u_char
*eaddr
;
4225 register struct block
*b0
, *b1
;
4229 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4232 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4235 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4236 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4242 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4243 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4252 * This is quite tricky because there may be pad bytes in front of the
4253 * DECNET header, and then there are two possible data packet formats that
4254 * carry both src and dst addresses, plus 5 packet types in a format that
4255 * carries only the src node, plus 2 types that use a different format and
4256 * also carry just the src node.
4260 * Instead of doing those all right, we just look for data packets with
4261 * 0 or 1 bytes of padding. If you want to look at other packets, that
4262 * will require a lot more hacking.
4264 * To add support for filtering on DECNET "areas" (network numbers)
4265 * one would want to add a "mask" argument to this routine. That would
4266 * make the filter even more inefficient, although one could be clever
4267 * and not generate masking instructions if the mask is 0xFFFF.
4269 static struct block
*
4270 gen_dnhostop(addr
, dir
)
4274 struct block
*b0
, *b1
, *b2
, *tmp
;
4275 u_int offset_lh
; /* offset if long header is received */
4276 u_int offset_sh
; /* offset if short header is received */
4281 offset_sh
= 1; /* follows flags */
4282 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4286 offset_sh
= 3; /* follows flags, dstnode */
4287 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4291 /* Inefficient because we do our Calvinball dance twice */
4292 b0
= gen_dnhostop(addr
, Q_SRC
);
4293 b1
= gen_dnhostop(addr
, Q_DST
);
4299 /* Inefficient because we do our Calvinball dance twice */
4300 b0
= gen_dnhostop(addr
, Q_SRC
);
4301 b1
= gen_dnhostop(addr
, Q_DST
);
4306 bpf_error("ISO host filtering not implemented");
4311 b0
= gen_linktype(ETHERTYPE_DN
);
4312 /* Check for pad = 1, long header case */
4313 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4314 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4315 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4316 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4318 /* Check for pad = 0, long header case */
4319 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4320 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4323 /* Check for pad = 1, short header case */
4324 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4325 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4326 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4329 /* Check for pad = 0, short header case */
4330 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4331 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4335 /* Combine with test for linktype */
4341 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4342 * test the bottom-of-stack bit, and then check the version number
4343 * field in the IP header.
4345 static struct block
*
4346 gen_mpls_linktype(proto
)
4349 struct block
*b0
, *b1
;
4354 /* match the bottom-of-stack bit */
4355 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4356 /* match the IPv4 version number */
4357 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4362 /* match the bottom-of-stack bit */
4363 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4364 /* match the IPv4 version number */
4365 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4374 static struct block
*
4375 gen_host(addr
, mask
, proto
, dir
, type
)
4382 struct block
*b0
, *b1
;
4383 const char *typestr
;
4393 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4395 * Only check for non-IPv4 addresses if we're not
4396 * checking MPLS-encapsulated packets.
4398 if (label_stack_depth
== 0) {
4399 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4401 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4407 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4410 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4413 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4416 bpf_error("'tcp' modifier applied to %s", typestr
);
4419 bpf_error("'sctp' modifier applied to %s", typestr
);
4422 bpf_error("'udp' modifier applied to %s", typestr
);
4425 bpf_error("'icmp' modifier applied to %s", typestr
);
4428 bpf_error("'igmp' modifier applied to %s", typestr
);
4431 bpf_error("'igrp' modifier applied to %s", typestr
);
4434 bpf_error("'pim' modifier applied to %s", typestr
);
4437 bpf_error("'vrrp' modifier applied to %s", typestr
);
4440 bpf_error("ATALK host filtering not implemented");
4443 bpf_error("AARP host filtering not implemented");
4446 return gen_dnhostop(addr
, dir
);
4449 bpf_error("SCA host filtering not implemented");
4452 bpf_error("LAT host filtering not implemented");
4455 bpf_error("MOPDL host filtering not implemented");
4458 bpf_error("MOPRC host filtering not implemented");
4462 bpf_error("'ip6' modifier applied to ip host");
4465 bpf_error("'icmp6' modifier applied to %s", typestr
);
4469 bpf_error("'ah' modifier applied to %s", typestr
);
4472 bpf_error("'esp' modifier applied to %s", typestr
);
4475 bpf_error("ISO host filtering not implemented");
4478 bpf_error("'esis' modifier applied to %s", typestr
);
4481 bpf_error("'isis' modifier applied to %s", typestr
);
4484 bpf_error("'clnp' modifier applied to %s", typestr
);
4487 bpf_error("'stp' modifier applied to %s", typestr
);
4490 bpf_error("IPX host filtering not implemented");
4493 bpf_error("'netbeui' modifier applied to %s", typestr
);
4496 bpf_error("'radio' modifier applied to %s", typestr
);
4505 static struct block
*
4506 gen_host6(addr
, mask
, proto
, dir
, type
)
4507 struct in6_addr
*addr
;
4508 struct in6_addr
*mask
;
4513 const char *typestr
;
4523 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4526 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4529 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4532 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4535 bpf_error("'sctp' modifier applied to %s", typestr
);
4538 bpf_error("'tcp' modifier applied to %s", typestr
);
4541 bpf_error("'udp' modifier applied to %s", typestr
);
4544 bpf_error("'icmp' modifier applied to %s", typestr
);
4547 bpf_error("'igmp' modifier applied to %s", typestr
);
4550 bpf_error("'igrp' modifier applied to %s", typestr
);
4553 bpf_error("'pim' modifier applied to %s", typestr
);
4556 bpf_error("'vrrp' modifier applied to %s", typestr
);
4559 bpf_error("ATALK host filtering not implemented");
4562 bpf_error("AARP host filtering not implemented");
4565 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4568 bpf_error("SCA host filtering not implemented");
4571 bpf_error("LAT host filtering not implemented");
4574 bpf_error("MOPDL host filtering not implemented");
4577 bpf_error("MOPRC host filtering not implemented");
4580 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4583 bpf_error("'icmp6' modifier applied to %s", typestr
);
4586 bpf_error("'ah' modifier applied to %s", typestr
);
4589 bpf_error("'esp' modifier applied to %s", typestr
);
4592 bpf_error("ISO host filtering not implemented");
4595 bpf_error("'esis' modifier applied to %s", typestr
);
4598 bpf_error("'isis' modifier applied to %s", typestr
);
4601 bpf_error("'clnp' modifier applied to %s", typestr
);
4604 bpf_error("'stp' modifier applied to %s", typestr
);
4607 bpf_error("IPX host filtering not implemented");
4610 bpf_error("'netbeui' modifier applied to %s", typestr
);
4613 bpf_error("'radio' modifier applied to %s", typestr
);
4623 static struct block
*
4624 gen_gateway(eaddr
, alist
, proto
, dir
)
4625 const u_char
*eaddr
;
4626 bpf_u_int32
**alist
;
4630 struct block
*b0
, *b1
, *tmp
;
4633 bpf_error("direction applied to 'gateway'");
4642 b0
= gen_ehostop(eaddr
, Q_OR
);
4645 b0
= gen_fhostop(eaddr
, Q_OR
);
4648 b0
= gen_thostop(eaddr
, Q_OR
);
4650 case DLT_IEEE802_11
:
4651 case DLT_PRISM_HEADER
:
4652 case DLT_IEEE802_11_RADIO_AVS
:
4653 case DLT_IEEE802_11_RADIO
:
4655 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4660 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4662 * Check that the packet doesn't begin with an
4663 * LE Control marker. (We've already generated
4666 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4671 * Now check the MAC address.
4673 b0
= gen_ehostop(eaddr
, Q_OR
);
4676 case DLT_IP_OVER_FC
:
4677 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4681 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4683 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4685 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4694 bpf_error("illegal modifier of 'gateway'");
4700 gen_proto_abbrev(proto
)
4709 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4711 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4717 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4719 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4725 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4727 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4733 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4736 #ifndef IPPROTO_IGMP
4737 #define IPPROTO_IGMP 2
4741 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4744 #ifndef IPPROTO_IGRP
4745 #define IPPROTO_IGRP 9
4748 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4752 #define IPPROTO_PIM 103
4756 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4758 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4763 #ifndef IPPROTO_VRRP
4764 #define IPPROTO_VRRP 112
4768 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4772 b1
= gen_linktype(ETHERTYPE_IP
);
4776 b1
= gen_linktype(ETHERTYPE_ARP
);
4780 b1
= gen_linktype(ETHERTYPE_REVARP
);
4784 bpf_error("link layer applied in wrong context");
4787 b1
= gen_linktype(ETHERTYPE_ATALK
);
4791 b1
= gen_linktype(ETHERTYPE_AARP
);
4795 b1
= gen_linktype(ETHERTYPE_DN
);
4799 b1
= gen_linktype(ETHERTYPE_SCA
);
4803 b1
= gen_linktype(ETHERTYPE_LAT
);
4807 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4811 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4816 b1
= gen_linktype(ETHERTYPE_IPV6
);
4819 #ifndef IPPROTO_ICMPV6
4820 #define IPPROTO_ICMPV6 58
4823 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4828 #define IPPROTO_AH 51
4831 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4833 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4839 #define IPPROTO_ESP 50
4842 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4844 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4850 b1
= gen_linktype(LLCSAP_ISONS
);
4854 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4858 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4861 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4862 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4863 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4865 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4867 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4869 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4873 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4874 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4875 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4877 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4879 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4881 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4885 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4886 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4887 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4889 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4894 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4895 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4900 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4901 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4903 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4905 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4910 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4911 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4916 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4917 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4922 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4926 b1
= gen_linktype(LLCSAP_8021D
);
4930 b1
= gen_linktype(LLCSAP_IPX
);
4934 b1
= gen_linktype(LLCSAP_NETBEUI
);
4938 bpf_error("'radio' is not a valid protocol type");
4946 static struct block
*
4953 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4954 b
= new_block(JMP(BPF_JSET
));
4963 * Generate a comparison to a port value in the transport-layer header
4964 * at the specified offset from the beginning of that header.
4966 * XXX - this handles a variable-length prefix preceding the link-layer
4967 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4968 * variable-length link-layer headers (such as Token Ring or 802.11
4971 static struct block
*
4972 gen_portatom(off
, v
)
4976 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4980 static struct block
*
4981 gen_portatom6(off
, v
)
4985 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4990 gen_portop(port
, proto
, dir
)
4991 int port
, proto
, dir
;
4993 struct block
*b0
, *b1
, *tmp
;
4995 /* ip proto 'proto' */
4996 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5002 b1
= gen_portatom(0, (bpf_int32
)port
);
5006 b1
= gen_portatom(2, (bpf_int32
)port
);
5011 tmp
= gen_portatom(0, (bpf_int32
)port
);
5012 b1
= gen_portatom(2, (bpf_int32
)port
);
5017 tmp
= gen_portatom(0, (bpf_int32
)port
);
5018 b1
= gen_portatom(2, (bpf_int32
)port
);
5030 static struct block
*
5031 gen_port(port
, ip_proto
, dir
)
5036 struct block
*b0
, *b1
, *tmp
;
5041 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5042 * not LLC encapsulation with LLCSAP_IP.
5044 * For IEEE 802 networks - which includes 802.5 token ring
5045 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5046 * says that SNAP encapsulation is used, not LLC encapsulation
5049 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5050 * RFC 2225 say that SNAP encapsulation is used, not LLC
5051 * encapsulation with LLCSAP_IP.
5053 * So we always check for ETHERTYPE_IP.
5055 b0
= gen_linktype(ETHERTYPE_IP
);
5061 b1
= gen_portop(port
, ip_proto
, dir
);
5065 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5066 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5068 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5081 gen_portop6(port
, proto
, dir
)
5082 int port
, proto
, dir
;
5084 struct block
*b0
, *b1
, *tmp
;
5086 /* ip6 proto 'proto' */
5087 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5091 b1
= gen_portatom6(0, (bpf_int32
)port
);
5095 b1
= gen_portatom6(2, (bpf_int32
)port
);
5100 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5101 b1
= gen_portatom6(2, (bpf_int32
)port
);
5106 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5107 b1
= gen_portatom6(2, (bpf_int32
)port
);
5119 static struct block
*
5120 gen_port6(port
, ip_proto
, dir
)
5125 struct block
*b0
, *b1
, *tmp
;
5127 /* link proto ip6 */
5128 b0
= gen_linktype(ETHERTYPE_IPV6
);
5134 b1
= gen_portop6(port
, ip_proto
, dir
);
5138 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5139 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5141 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5153 /* gen_portrange code */
5154 static struct block
*
5155 gen_portrangeatom(off
, v1
, v2
)
5159 struct block
*b1
, *b2
;
5163 * Reverse the order of the ports, so v1 is the lower one.
5172 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5173 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5181 gen_portrangeop(port1
, port2
, proto
, dir
)
5186 struct block
*b0
, *b1
, *tmp
;
5188 /* ip proto 'proto' */
5189 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5195 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5199 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5204 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5205 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5210 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5211 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5223 static struct block
*
5224 gen_portrange(port1
, port2
, ip_proto
, dir
)
5229 struct block
*b0
, *b1
, *tmp
;
5232 b0
= gen_linktype(ETHERTYPE_IP
);
5238 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5242 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5243 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5245 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5257 static struct block
*
5258 gen_portrangeatom6(off
, v1
, v2
)
5262 struct block
*b1
, *b2
;
5266 * Reverse the order of the ports, so v1 is the lower one.
5275 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5276 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5284 gen_portrangeop6(port1
, port2
, proto
, dir
)
5289 struct block
*b0
, *b1
, *tmp
;
5291 /* ip6 proto 'proto' */
5292 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5296 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5300 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5305 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5306 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5311 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5312 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5324 static struct block
*
5325 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5330 struct block
*b0
, *b1
, *tmp
;
5332 /* link proto ip6 */
5333 b0
= gen_linktype(ETHERTYPE_IPV6
);
5339 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5343 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5344 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5346 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5359 lookup_proto(name
, proto
)
5360 register const char *name
;
5370 v
= pcap_nametoproto(name
);
5371 if (v
== PROTO_UNDEF
)
5372 bpf_error("unknown ip proto '%s'", name
);
5376 /* XXX should look up h/w protocol type based on linktype */
5377 v
= pcap_nametoeproto(name
);
5378 if (v
== PROTO_UNDEF
) {
5379 v
= pcap_nametollc(name
);
5380 if (v
== PROTO_UNDEF
)
5381 bpf_error("unknown ether proto '%s'", name
);
5386 if (strcmp(name
, "esis") == 0)
5388 else if (strcmp(name
, "isis") == 0)
5390 else if (strcmp(name
, "clnp") == 0)
5393 bpf_error("unknown osi proto '%s'", name
);
5413 static struct block
*
5414 gen_protochain(v
, proto
, dir
)
5419 #ifdef NO_PROTOCHAIN
5420 return gen_proto(v
, proto
, dir
);
5422 struct block
*b0
, *b
;
5423 struct slist
*s
[100];
5424 int fix2
, fix3
, fix4
, fix5
;
5425 int ahcheck
, again
, end
;
5427 int reg2
= alloc_reg();
5429 memset(s
, 0, sizeof(s
));
5430 fix2
= fix3
= fix4
= fix5
= 0;
5437 b0
= gen_protochain(v
, Q_IP
, dir
);
5438 b
= gen_protochain(v
, Q_IPV6
, dir
);
5442 bpf_error("bad protocol applied for 'protochain'");
5447 * We don't handle variable-length prefixes before the link-layer
5448 * header, or variable-length link-layer headers, here yet.
5449 * We might want to add BPF instructions to do the protochain
5450 * work, to simplify that and, on platforms that have a BPF
5451 * interpreter with the new instructions, let the filtering
5452 * be done in the kernel. (We already require a modified BPF
5453 * engine to do the protochain stuff, to support backward
5454 * branches, and backward branch support is unlikely to appear
5455 * in kernel BPF engines.)
5459 case DLT_IEEE802_11
:
5460 case DLT_PRISM_HEADER
:
5461 case DLT_IEEE802_11_RADIO_AVS
:
5462 case DLT_IEEE802_11_RADIO
:
5464 bpf_error("'protochain' not supported with 802.11");
5467 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5470 * s[0] is a dummy entry to protect other BPF insn from damage
5471 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5472 * hard to find interdependency made by jump table fixup.
5475 s
[i
] = new_stmt(0); /*dummy*/
5480 b0
= gen_linktype(ETHERTYPE_IP
);
5483 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5484 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5486 /* X = ip->ip_hl << 2 */
5487 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5488 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5493 b0
= gen_linktype(ETHERTYPE_IPV6
);
5495 /* A = ip6->ip_nxt */
5496 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5497 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5499 /* X = sizeof(struct ip6_hdr) */
5500 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5506 bpf_error("unsupported proto to gen_protochain");
5510 /* again: if (A == v) goto end; else fall through; */
5512 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5514 s
[i
]->s
.jt
= NULL
; /*later*/
5515 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5519 #ifndef IPPROTO_NONE
5520 #define IPPROTO_NONE 59
5522 /* if (A == IPPROTO_NONE) goto end */
5523 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5524 s
[i
]->s
.jt
= NULL
; /*later*/
5525 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5526 s
[i
]->s
.k
= IPPROTO_NONE
;
5527 s
[fix5
]->s
.jf
= s
[i
];
5532 if (proto
== Q_IPV6
) {
5533 int v6start
, v6end
, v6advance
, j
;
5536 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5537 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5538 s
[i
]->s
.jt
= NULL
; /*later*/
5539 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5540 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5541 s
[fix2
]->s
.jf
= s
[i
];
5543 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5544 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5545 s
[i
]->s
.jt
= NULL
; /*later*/
5546 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5547 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5549 /* if (A == IPPROTO_ROUTING) goto v6advance */
5550 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5551 s
[i
]->s
.jt
= NULL
; /*later*/
5552 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5553 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5555 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5556 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5557 s
[i
]->s
.jt
= NULL
; /*later*/
5558 s
[i
]->s
.jf
= NULL
; /*later*/
5559 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5570 * X = X + (P[X + 1] + 1) * 8;
5573 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5575 /* A = P[X + packet head] */
5576 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5577 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5580 s
[i
] = new_stmt(BPF_ST
);
5584 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5587 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5591 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5593 /* A = P[X + packet head]; */
5594 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5595 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5598 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5602 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5606 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5609 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5613 /* goto again; (must use BPF_JA for backward jump) */
5614 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5615 s
[i
]->s
.k
= again
- i
- 1;
5616 s
[i
- 1]->s
.jf
= s
[i
];
5620 for (j
= v6start
; j
<= v6end
; j
++)
5621 s
[j
]->s
.jt
= s
[v6advance
];
5626 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5628 s
[fix2
]->s
.jf
= s
[i
];
5634 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5635 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5636 s
[i
]->s
.jt
= NULL
; /*later*/
5637 s
[i
]->s
.jf
= NULL
; /*later*/
5638 s
[i
]->s
.k
= IPPROTO_AH
;
5640 s
[fix3
]->s
.jf
= s
[ahcheck
];
5647 * X = X + (P[X + 1] + 2) * 4;
5650 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5652 /* A = P[X + packet head]; */
5653 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5654 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5657 s
[i
] = new_stmt(BPF_ST
);
5661 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5664 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5668 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5670 /* A = P[X + packet head] */
5671 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5672 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5675 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5679 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5683 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5686 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5690 /* goto again; (must use BPF_JA for backward jump) */
5691 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5692 s
[i
]->s
.k
= again
- i
- 1;
5697 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5699 s
[fix2
]->s
.jt
= s
[end
];
5700 s
[fix4
]->s
.jf
= s
[end
];
5701 s
[fix5
]->s
.jt
= s
[end
];
5708 for (i
= 0; i
< max
- 1; i
++)
5709 s
[i
]->next
= s
[i
+ 1];
5710 s
[max
- 1]->next
= NULL
;
5715 b
= new_block(JMP(BPF_JEQ
));
5716 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5726 static struct block
*
5727 gen_check_802_11_data_frame()
5730 struct block
*b0
, *b1
;
5733 * A data frame has the 0x08 bit (b3) in the frame control field set
5734 * and the 0x04 bit (b2) clear.
5736 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5737 b0
= new_block(JMP(BPF_JSET
));
5741 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5742 b1
= new_block(JMP(BPF_JSET
));
5753 * Generate code that checks whether the packet is a packet for protocol
5754 * <proto> and whether the type field in that protocol's header has
5755 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5756 * IP packet and checks the protocol number in the IP header against <v>.
5758 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5759 * against Q_IP and Q_IPV6.
5761 static struct block
*
5762 gen_proto(v
, proto
, dir
)
5767 struct block
*b0
, *b1
;
5769 if (dir
!= Q_DEFAULT
)
5770 bpf_error("direction applied to 'proto'");
5775 b0
= gen_proto(v
, Q_IP
, dir
);
5776 b1
= gen_proto(v
, Q_IPV6
, dir
);
5784 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5785 * not LLC encapsulation with LLCSAP_IP.
5787 * For IEEE 802 networks - which includes 802.5 token ring
5788 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5789 * says that SNAP encapsulation is used, not LLC encapsulation
5792 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5793 * RFC 2225 say that SNAP encapsulation is used, not LLC
5794 * encapsulation with LLCSAP_IP.
5796 * So we always check for ETHERTYPE_IP.
5798 b0
= gen_linktype(ETHERTYPE_IP
);
5800 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5802 b1
= gen_protochain(v
, Q_IP
);
5812 * Frame Relay packets typically have an OSI
5813 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5814 * generates code to check for all the OSI
5815 * NLPIDs, so calling it and then adding a check
5816 * for the particular NLPID for which we're
5817 * looking is bogus, as we can just check for
5820 * What we check for is the NLPID and a frame
5821 * control field value of UI, i.e. 0x03 followed
5824 * XXX - assumes a 2-byte Frame Relay header with
5825 * DLCI and flags. What if the address is longer?
5827 * XXX - what about SNAP-encapsulated frames?
5829 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5835 * Cisco uses an Ethertype lookalike - for OSI,
5838 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5839 /* OSI in C-HDLC is stuffed with a fudge byte */
5840 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5845 b0
= gen_linktype(LLCSAP_ISONS
);
5846 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5852 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5854 * 4 is the offset of the PDU type relative to the IS-IS
5857 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5862 bpf_error("arp does not encapsulate another protocol");
5866 bpf_error("rarp does not encapsulate another protocol");
5870 bpf_error("atalk encapsulation is not specifiable");
5874 bpf_error("decnet encapsulation is not specifiable");
5878 bpf_error("sca does not encapsulate another protocol");
5882 bpf_error("lat does not encapsulate another protocol");
5886 bpf_error("moprc does not encapsulate another protocol");
5890 bpf_error("mopdl does not encapsulate another protocol");
5894 return gen_linktype(v
);
5897 bpf_error("'udp proto' is bogus");
5901 bpf_error("'tcp proto' is bogus");
5905 bpf_error("'sctp proto' is bogus");
5909 bpf_error("'icmp proto' is bogus");
5913 bpf_error("'igmp proto' is bogus");
5917 bpf_error("'igrp proto' is bogus");
5921 bpf_error("'pim proto' is bogus");
5925 bpf_error("'vrrp proto' is bogus");
5930 b0
= gen_linktype(ETHERTYPE_IPV6
);
5932 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5934 b1
= gen_protochain(v
, Q_IPV6
);
5940 bpf_error("'icmp6 proto' is bogus");
5944 bpf_error("'ah proto' is bogus");
5947 bpf_error("'ah proto' is bogus");
5950 bpf_error("'stp proto' is bogus");
5953 bpf_error("'ipx proto' is bogus");
5956 bpf_error("'netbeui proto' is bogus");
5959 bpf_error("'radio proto' is bogus");
5970 register const char *name
;
5973 int proto
= q
.proto
;
5977 bpf_u_int32 mask
, addr
;
5979 bpf_u_int32
**alist
;
5982 struct sockaddr_in
*sin4
;
5983 struct sockaddr_in6
*sin6
;
5984 struct addrinfo
*res
, *res0
;
5985 struct in6_addr mask128
;
5987 struct block
*b
, *tmp
;
5988 int port
, real_proto
;
5994 addr
= pcap_nametonetaddr(name
);
5996 bpf_error("unknown network '%s'", name
);
5997 /* Left justify network addr and calculate its network mask */
5999 while (addr
&& (addr
& 0xff000000) == 0) {
6003 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6007 if (proto
== Q_LINK
) {
6011 eaddr
= pcap_ether_hostton(name
);
6014 "unknown ether host '%s'", name
);
6015 b
= gen_ehostop(eaddr
, dir
);
6020 eaddr
= pcap_ether_hostton(name
);
6023 "unknown FDDI host '%s'", name
);
6024 b
= gen_fhostop(eaddr
, dir
);
6029 eaddr
= pcap_ether_hostton(name
);
6032 "unknown token ring host '%s'", name
);
6033 b
= gen_thostop(eaddr
, dir
);
6037 case DLT_IEEE802_11
:
6038 case DLT_PRISM_HEADER
:
6039 case DLT_IEEE802_11_RADIO_AVS
:
6040 case DLT_IEEE802_11_RADIO
:
6042 eaddr
= pcap_ether_hostton(name
);
6045 "unknown 802.11 host '%s'", name
);
6046 b
= gen_wlanhostop(eaddr
, dir
);
6050 case DLT_IP_OVER_FC
:
6051 eaddr
= pcap_ether_hostton(name
);
6054 "unknown Fibre Channel host '%s'", name
);
6055 b
= gen_ipfchostop(eaddr
, dir
);
6064 * Check that the packet doesn't begin
6065 * with an LE Control marker. (We've
6066 * already generated a test for LANE.)
6068 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6072 eaddr
= pcap_ether_hostton(name
);
6075 "unknown ether host '%s'", name
);
6076 b
= gen_ehostop(eaddr
, dir
);
6082 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6083 } else if (proto
== Q_DECNET
) {
6084 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6086 * I don't think DECNET hosts can be multihomed, so
6087 * there is no need to build up a list of addresses
6089 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6092 alist
= pcap_nametoaddr(name
);
6093 if (alist
== NULL
|| *alist
== NULL
)
6094 bpf_error("unknown host '%s'", name
);
6096 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6098 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6100 tmp
= gen_host(**alist
++, 0xffffffff,
6101 tproto
, dir
, q
.addr
);
6107 memset(&mask128
, 0xff, sizeof(mask128
));
6108 res0
= res
= pcap_nametoaddrinfo(name
);
6110 bpf_error("unknown host '%s'", name
);
6113 tproto
= tproto6
= proto
;
6114 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6118 for (res
= res0
; res
; res
= res
->ai_next
) {
6119 switch (res
->ai_family
) {
6121 if (tproto
== Q_IPV6
)
6124 sin4
= (struct sockaddr_in
*)
6126 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6127 0xffffffff, tproto
, dir
, q
.addr
);
6130 if (tproto6
== Q_IP
)
6133 sin6
= (struct sockaddr_in6
*)
6135 tmp
= gen_host6(&sin6
->sin6_addr
,
6136 &mask128
, tproto6
, dir
, q
.addr
);
6148 bpf_error("unknown host '%s'%s", name
,
6149 (proto
== Q_DEFAULT
)
6151 : " for specified address family");
6158 if (proto
!= Q_DEFAULT
&&
6159 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6160 bpf_error("illegal qualifier of 'port'");
6161 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6162 bpf_error("unknown port '%s'", name
);
6163 if (proto
== Q_UDP
) {
6164 if (real_proto
== IPPROTO_TCP
)
6165 bpf_error("port '%s' is tcp", name
);
6166 else if (real_proto
== IPPROTO_SCTP
)
6167 bpf_error("port '%s' is sctp", name
);
6169 /* override PROTO_UNDEF */
6170 real_proto
= IPPROTO_UDP
;
6172 if (proto
== Q_TCP
) {
6173 if (real_proto
== IPPROTO_UDP
)
6174 bpf_error("port '%s' is udp", name
);
6176 else if (real_proto
== IPPROTO_SCTP
)
6177 bpf_error("port '%s' is sctp", name
);
6179 /* override PROTO_UNDEF */
6180 real_proto
= IPPROTO_TCP
;
6182 if (proto
== Q_SCTP
) {
6183 if (real_proto
== IPPROTO_UDP
)
6184 bpf_error("port '%s' is udp", name
);
6186 else if (real_proto
== IPPROTO_TCP
)
6187 bpf_error("port '%s' is tcp", name
);
6189 /* override PROTO_UNDEF */
6190 real_proto
= IPPROTO_SCTP
;
6193 return gen_port(port
, real_proto
, dir
);
6195 b
= gen_port(port
, real_proto
, dir
);
6196 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6201 if (proto
!= Q_DEFAULT
&&
6202 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6203 bpf_error("illegal qualifier of 'portrange'");
6204 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6205 bpf_error("unknown port in range '%s'", name
);
6206 if (proto
== Q_UDP
) {
6207 if (real_proto
== IPPROTO_TCP
)
6208 bpf_error("port in range '%s' is tcp", name
);
6209 else if (real_proto
== IPPROTO_SCTP
)
6210 bpf_error("port in range '%s' is sctp", name
);
6212 /* override PROTO_UNDEF */
6213 real_proto
= IPPROTO_UDP
;
6215 if (proto
== Q_TCP
) {
6216 if (real_proto
== IPPROTO_UDP
)
6217 bpf_error("port in range '%s' is udp", name
);
6218 else if (real_proto
== IPPROTO_SCTP
)
6219 bpf_error("port in range '%s' is sctp", name
);
6221 /* override PROTO_UNDEF */
6222 real_proto
= IPPROTO_TCP
;
6224 if (proto
== Q_SCTP
) {
6225 if (real_proto
== IPPROTO_UDP
)
6226 bpf_error("port in range '%s' is udp", name
);
6227 else if (real_proto
== IPPROTO_TCP
)
6228 bpf_error("port in range '%s' is tcp", name
);
6230 /* override PROTO_UNDEF */
6231 real_proto
= IPPROTO_SCTP
;
6234 return gen_portrange(port1
, port2
, real_proto
, dir
);
6236 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6237 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6243 eaddr
= pcap_ether_hostton(name
);
6245 bpf_error("unknown ether host: %s", name
);
6247 alist
= pcap_nametoaddr(name
);
6248 if (alist
== NULL
|| *alist
== NULL
)
6249 bpf_error("unknown host '%s'", name
);
6250 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6254 bpf_error("'gateway' not supported in this configuration");
6258 real_proto
= lookup_proto(name
, proto
);
6259 if (real_proto
>= 0)
6260 return gen_proto(real_proto
, proto
, dir
);
6262 bpf_error("unknown protocol: %s", name
);
6265 real_proto
= lookup_proto(name
, proto
);
6266 if (real_proto
>= 0)
6267 return gen_protochain(real_proto
, proto
, dir
);
6269 bpf_error("unknown protocol: %s", name
);
6280 gen_mcode(s1
, s2
, masklen
, q
)
6281 register const char *s1
, *s2
;
6282 register int masklen
;
6285 register int nlen
, mlen
;
6288 nlen
= __pcap_atoin(s1
, &n
);
6289 /* Promote short ipaddr */
6293 mlen
= __pcap_atoin(s2
, &m
);
6294 /* Promote short ipaddr */
6297 bpf_error("non-network bits set in \"%s mask %s\"",
6300 /* Convert mask len to mask */
6302 bpf_error("mask length must be <= 32");
6305 * X << 32 is not guaranteed by C to be 0; it's
6310 m
= 0xffffffff << (32 - masklen
);
6312 bpf_error("non-network bits set in \"%s/%d\"",
6319 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6322 bpf_error("Mask syntax for networks only");
6331 register const char *s
;
6336 int proto
= q
.proto
;
6342 else if (q
.proto
== Q_DECNET
)
6343 vlen
= __pcap_atodn(s
, &v
);
6345 vlen
= __pcap_atoin(s
, &v
);
6352 if (proto
== Q_DECNET
)
6353 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6354 else if (proto
== Q_LINK
) {
6355 bpf_error("illegal link layer address");
6358 if (s
== NULL
&& q
.addr
== Q_NET
) {
6359 /* Promote short net number */
6360 while (v
&& (v
& 0xff000000) == 0) {
6365 /* Promote short ipaddr */
6369 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6374 proto
= IPPROTO_UDP
;
6375 else if (proto
== Q_TCP
)
6376 proto
= IPPROTO_TCP
;
6377 else if (proto
== Q_SCTP
)
6378 proto
= IPPROTO_SCTP
;
6379 else if (proto
== Q_DEFAULT
)
6380 proto
= PROTO_UNDEF
;
6382 bpf_error("illegal qualifier of 'port'");
6385 return gen_port((int)v
, proto
, dir
);
6389 b
= gen_port((int)v
, proto
, dir
);
6390 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6397 proto
= IPPROTO_UDP
;
6398 else if (proto
== Q_TCP
)
6399 proto
= IPPROTO_TCP
;
6400 else if (proto
== Q_SCTP
)
6401 proto
= IPPROTO_SCTP
;
6402 else if (proto
== Q_DEFAULT
)
6403 proto
= PROTO_UNDEF
;
6405 bpf_error("illegal qualifier of 'portrange'");
6408 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6412 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6413 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6419 bpf_error("'gateway' requires a name");
6423 return gen_proto((int)v
, proto
, dir
);
6426 return gen_protochain((int)v
, proto
, dir
);
6441 gen_mcode6(s1
, s2
, masklen
, q
)
6442 register const char *s1
, *s2
;
6443 register int masklen
;
6446 struct addrinfo
*res
;
6447 struct in6_addr
*addr
;
6448 struct in6_addr mask
;
6453 bpf_error("no mask %s supported", s2
);
6455 res
= pcap_nametoaddrinfo(s1
);
6457 bpf_error("invalid ip6 address %s", s1
);
6460 bpf_error("%s resolved to multiple address", s1
);
6461 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6463 if (sizeof(mask
) * 8 < masklen
)
6464 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6465 memset(&mask
, 0, sizeof(mask
));
6466 memset(&mask
, 0xff, masklen
/ 8);
6468 mask
.s6_addr
[masklen
/ 8] =
6469 (0xff << (8 - masklen
% 8)) & 0xff;
6472 a
= (u_int32_t
*)addr
;
6473 m
= (u_int32_t
*)&mask
;
6474 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6475 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6476 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6484 bpf_error("Mask syntax for networks only");
6488 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6494 bpf_error("invalid qualifier against IPv6 address");
6503 register const u_char
*eaddr
;
6506 struct block
*b
, *tmp
;
6508 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6511 return gen_ehostop(eaddr
, (int)q
.dir
);
6513 return gen_fhostop(eaddr
, (int)q
.dir
);
6515 return gen_thostop(eaddr
, (int)q
.dir
);
6516 case DLT_IEEE802_11
:
6517 case DLT_PRISM_HEADER
:
6518 case DLT_IEEE802_11_RADIO_AVS
:
6519 case DLT_IEEE802_11_RADIO
:
6521 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6525 * Check that the packet doesn't begin with an
6526 * LE Control marker. (We've already generated
6529 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6534 * Now check the MAC address.
6536 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6541 case DLT_IP_OVER_FC
:
6542 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6544 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6548 bpf_error("ethernet address used in non-ether expression");
6555 struct slist
*s0
, *s1
;
6558 * This is definitely not the best way to do this, but the
6559 * lists will rarely get long.
6566 static struct slist
*
6572 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6577 static struct slist
*
6583 s
= new_stmt(BPF_LD
|BPF_MEM
);
6589 * Modify "index" to use the value stored into its register as an
6590 * offset relative to the beginning of the header for the protocol
6591 * "proto", and allocate a register and put an item "size" bytes long
6592 * (1, 2, or 4) at that offset into that register, making it the register
6596 gen_load(proto
, inst
, size
)
6601 struct slist
*s
, *tmp
;
6603 int regno
= alloc_reg();
6605 free_reg(inst
->regno
);
6609 bpf_error("data size must be 1, 2, or 4");
6625 bpf_error("unsupported index operation");
6629 * The offset is relative to the beginning of the packet
6630 * data, if we have a radio header. (If we don't, this
6633 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6634 linktype
!= DLT_IEEE802_11_RADIO
&&
6635 linktype
!= DLT_PRISM_HEADER
)
6636 bpf_error("radio information not present in capture");
6639 * Load into the X register the offset computed into the
6640 * register specifed by "index".
6642 s
= xfer_to_x(inst
);
6645 * Load the item at that offset.
6647 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6649 sappend(inst
->s
, s
);
6654 * The offset is relative to the beginning of
6655 * the link-layer header.
6657 * XXX - what about ATM LANE? Should the index be
6658 * relative to the beginning of the AAL5 frame, so
6659 * that 0 refers to the beginning of the LE Control
6660 * field, or relative to the beginning of the LAN
6661 * frame, so that 0 refers, for Ethernet LANE, to
6662 * the beginning of the destination address?
6664 s
= gen_llprefixlen();
6667 * If "s" is non-null, it has code to arrange that the
6668 * X register contains the length of the prefix preceding
6669 * the link-layer header. Add to it the offset computed
6670 * into the register specified by "index", and move that
6671 * into the X register. Otherwise, just load into the X
6672 * register the offset computed into the register specifed
6676 sappend(s
, xfer_to_a(inst
));
6677 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6678 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6680 s
= xfer_to_x(inst
);
6683 * Load the item at the sum of the offset we've put in the
6684 * X register and the offset of the start of the link
6685 * layer header (which is 0 if the radio header is
6686 * variable-length; that header length is what we put
6687 * into the X register and then added to the index).
6689 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6692 sappend(inst
->s
, s
);
6708 * The offset is relative to the beginning of
6709 * the network-layer header.
6710 * XXX - are there any cases where we want
6713 s
= gen_off_macpl();
6716 * If "s" is non-null, it has code to arrange that the
6717 * X register contains the offset of the MAC-layer
6718 * payload. Add to it the offset computed into the
6719 * register specified by "index", and move that into
6720 * the X register. Otherwise, just load into the X
6721 * register the offset computed into the register specifed
6725 sappend(s
, xfer_to_a(inst
));
6726 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6727 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6729 s
= xfer_to_x(inst
);
6732 * Load the item at the sum of the offset we've put in the
6733 * X register, the offset of the start of the network
6734 * layer header from the beginning of the MAC-layer
6735 * payload, and the purported offset of the start of the
6736 * MAC-layer payload (which might be 0 if there's a
6737 * variable-length prefix before the link-layer header
6738 * or the link-layer header itself is variable-length;
6739 * the variable-length offset of the start of the
6740 * MAC-layer payload is what we put into the X register
6741 * and then added to the index).
6743 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6744 tmp
->s
.k
= off_macpl
+ off_nl
;
6746 sappend(inst
->s
, s
);
6749 * Do the computation only if the packet contains
6750 * the protocol in question.
6752 b
= gen_proto_abbrev(proto
);
6754 gen_and(inst
->b
, b
);
6767 * The offset is relative to the beginning of
6768 * the transport-layer header.
6770 * Load the X register with the length of the IPv4 header
6771 * (plus the offset of the link-layer header, if it's
6772 * a variable-length header), in bytes.
6774 * XXX - are there any cases where we want
6776 * XXX - we should, if we're built with
6777 * IPv6 support, generate code to load either
6778 * IPv4, IPv6, or both, as appropriate.
6780 s
= gen_loadx_iphdrlen();
6783 * The X register now contains the sum of the length
6784 * of any variable-length header preceding the link-layer
6785 * header, any variable-length link-layer header, and the
6786 * length of the network-layer header.
6788 * Load into the A register the offset relative to
6789 * the beginning of the transport layer header,
6790 * add the X register to that, move that to the
6791 * X register, and load with an offset from the
6792 * X register equal to the offset of the network
6793 * layer header relative to the beginning of
6794 * the MAC-layer payload plus the fixed-length
6795 * portion of the offset of the MAC-layer payload
6796 * from the beginning of the raw packet data.
6798 sappend(s
, xfer_to_a(inst
));
6799 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6800 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6801 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6802 tmp
->s
.k
= off_macpl
+ off_nl
;
6803 sappend(inst
->s
, s
);
6806 * Do the computation only if the packet contains
6807 * the protocol in question - which is true only
6808 * if this is an IP datagram and is the first or
6809 * only fragment of that datagram.
6811 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6813 gen_and(inst
->b
, b
);
6815 gen_and(gen_proto_abbrev(Q_IP
), b
);
6821 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6825 inst
->regno
= regno
;
6826 s
= new_stmt(BPF_ST
);
6828 sappend(inst
->s
, s
);
6834 gen_relation(code
, a0
, a1
, reversed
)
6836 struct arth
*a0
, *a1
;
6839 struct slist
*s0
, *s1
, *s2
;
6840 struct block
*b
, *tmp
;
6844 if (code
== BPF_JEQ
) {
6845 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6846 b
= new_block(JMP(code
));
6850 b
= new_block(BPF_JMP
|code
|BPF_X
);
6856 sappend(a0
->s
, a1
->s
);
6860 free_reg(a0
->regno
);
6861 free_reg(a1
->regno
);
6863 /* 'and' together protocol checks */
6866 gen_and(a0
->b
, tmp
= a1
->b
);
6882 int regno
= alloc_reg();
6883 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6886 s
= new_stmt(BPF_LD
|BPF_LEN
);
6887 s
->next
= new_stmt(BPF_ST
);
6888 s
->next
->s
.k
= regno
;
6903 a
= (struct arth
*)newchunk(sizeof(*a
));
6907 s
= new_stmt(BPF_LD
|BPF_IMM
);
6909 s
->next
= new_stmt(BPF_ST
);
6925 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6928 s
= new_stmt(BPF_ST
);
6936 gen_arth(code
, a0
, a1
)
6938 struct arth
*a0
, *a1
;
6940 struct slist
*s0
, *s1
, *s2
;
6944 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6949 sappend(a0
->s
, a1
->s
);
6951 free_reg(a0
->regno
);
6952 free_reg(a1
->regno
);
6954 s0
= new_stmt(BPF_ST
);
6955 a0
->regno
= s0
->s
.k
= alloc_reg();
6962 * Here we handle simple allocation of the scratch registers.
6963 * If too many registers are alloc'd, the allocator punts.
6965 static int regused
[BPF_MEMWORDS
];
6969 * Initialize the table of used registers and the current register.
6975 memset(regused
, 0, sizeof regused
);
6979 * Return the next free register.
6984 int n
= BPF_MEMWORDS
;
6987 if (regused
[curreg
])
6988 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6990 regused
[curreg
] = 1;
6994 bpf_error("too many registers needed to evaluate expression");
7000 * Return a register to the table so it can
7010 static struct block
*
7017 s
= new_stmt(BPF_LD
|BPF_LEN
);
7018 b
= new_block(JMP(jmp
));
7029 return gen_len(BPF_JGE
, n
);
7033 * Actually, this is less than or equal.
7041 b
= gen_len(BPF_JGT
, n
);
7048 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7049 * the beginning of the link-layer header.
7050 * XXX - that means you can't test values in the radiotap header, but
7051 * as that header is difficult if not impossible to parse generally
7052 * without a loop, that might not be a severe problem. A new keyword
7053 * "radio" could be added for that, although what you'd really want
7054 * would be a way of testing particular radio header values, which
7055 * would generate code appropriate to the radio header in question.
7058 gen_byteop(op
, idx
, val
)
7069 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7072 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7076 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7080 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7084 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7088 b
= new_block(JMP(BPF_JEQ
));
7095 static u_char abroadcast
[] = { 0x0 };
7098 gen_broadcast(proto
)
7101 bpf_u_int32 hostmask
;
7102 struct block
*b0
, *b1
, *b2
;
7103 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7111 case DLT_ARCNET_LINUX
:
7112 return gen_ahostop(abroadcast
, Q_DST
);
7114 return gen_ehostop(ebroadcast
, Q_DST
);
7116 return gen_fhostop(ebroadcast
, Q_DST
);
7118 return gen_thostop(ebroadcast
, Q_DST
);
7119 case DLT_IEEE802_11
:
7120 case DLT_PRISM_HEADER
:
7121 case DLT_IEEE802_11_RADIO_AVS
:
7122 case DLT_IEEE802_11_RADIO
:
7124 return gen_wlanhostop(ebroadcast
, Q_DST
);
7125 case DLT_IP_OVER_FC
:
7126 return gen_ipfchostop(ebroadcast
, Q_DST
);
7130 * Check that the packet doesn't begin with an
7131 * LE Control marker. (We've already generated
7134 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7139 * Now check the MAC address.
7141 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7147 bpf_error("not a broadcast link");
7153 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7154 * as an indication that we don't know the netmask, and fail
7157 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7158 bpf_error("netmask not known, so 'ip broadcast' not supported");
7159 b0
= gen_linktype(ETHERTYPE_IP
);
7160 hostmask
= ~netmask
;
7161 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7162 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7163 (bpf_int32
)(~0 & hostmask
), hostmask
);
7168 bpf_error("only link-layer/IP broadcast filters supported");
7174 * Generate code to test the low-order bit of a MAC address (that's
7175 * the bottom bit of the *first* byte).
7177 static struct block
*
7178 gen_mac_multicast(offset
)
7181 register struct block
*b0
;
7182 register struct slist
*s
;
7184 /* link[offset] & 1 != 0 */
7185 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7186 b0
= new_block(JMP(BPF_JSET
));
7193 gen_multicast(proto
)
7196 register struct block
*b0
, *b1
, *b2
;
7197 register struct slist
*s
;
7205 case DLT_ARCNET_LINUX
:
7206 /* all ARCnet multicasts use the same address */
7207 return gen_ahostop(abroadcast
, Q_DST
);
7209 /* ether[0] & 1 != 0 */
7210 return gen_mac_multicast(0);
7213 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7215 * XXX - was that referring to bit-order issues?
7217 /* fddi[1] & 1 != 0 */
7218 return gen_mac_multicast(1);
7220 /* tr[2] & 1 != 0 */
7221 return gen_mac_multicast(2);
7222 case DLT_IEEE802_11
:
7223 case DLT_PRISM_HEADER
:
7224 case DLT_IEEE802_11_RADIO_AVS
:
7225 case DLT_IEEE802_11_RADIO
:
7230 * For control frames, there is no DA.
7232 * For management frames, DA is at an
7233 * offset of 4 from the beginning of
7236 * For data frames, DA is at an offset
7237 * of 4 from the beginning of the packet
7238 * if To DS is clear and at an offset of
7239 * 16 from the beginning of the packet
7244 * Generate the tests to be done for data frames.
7246 * First, check for To DS set, i.e. "link[1] & 0x01".
7248 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7249 b1
= new_block(JMP(BPF_JSET
));
7250 b1
->s
.k
= 0x01; /* To DS */
7254 * If To DS is set, the DA is at 16.
7256 b0
= gen_mac_multicast(16);
7260 * Now, check for To DS not set, i.e. check
7261 * "!(link[1] & 0x01)".
7263 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7264 b2
= new_block(JMP(BPF_JSET
));
7265 b2
->s
.k
= 0x01; /* To DS */
7270 * If To DS is not set, the DA is at 4.
7272 b1
= gen_mac_multicast(4);
7276 * Now OR together the last two checks. That gives
7277 * the complete set of checks for data frames.
7282 * Now check for a data frame.
7283 * I.e, check "link[0] & 0x08".
7285 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7286 b1
= new_block(JMP(BPF_JSET
));
7291 * AND that with the checks done for data frames.
7296 * If the high-order bit of the type value is 0, this
7297 * is a management frame.
7298 * I.e, check "!(link[0] & 0x08)".
7300 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7301 b2
= new_block(JMP(BPF_JSET
));
7307 * For management frames, the DA is at 4.
7309 b1
= gen_mac_multicast(4);
7313 * OR that with the checks done for data frames.
7314 * That gives the checks done for management and
7320 * If the low-order bit of the type value is 1,
7321 * this is either a control frame or a frame
7322 * with a reserved type, and thus not a
7325 * I.e., check "!(link[0] & 0x04)".
7327 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7328 b1
= new_block(JMP(BPF_JSET
));
7334 * AND that with the checks for data and management
7339 case DLT_IP_OVER_FC
:
7340 b0
= gen_mac_multicast(2);
7345 * Check that the packet doesn't begin with an
7346 * LE Control marker. (We've already generated
7349 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7353 /* ether[off_mac] & 1 != 0 */
7354 b0
= gen_mac_multicast(off_mac
);
7362 /* Link not known to support multicasts */
7366 b0
= gen_linktype(ETHERTYPE_IP
);
7367 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7373 b0
= gen_linktype(ETHERTYPE_IPV6
);
7374 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7379 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7385 * generate command for inbound/outbound. It's here so we can
7386 * make it link-type specific. 'dir' = 0 implies "inbound",
7387 * = 1 implies "outbound".
7393 register struct block
*b0
;
7396 * Only some data link types support inbound/outbound qualifiers.
7400 b0
= gen_relation(BPF_JEQ
,
7401 gen_load(Q_LINK
, gen_loadi(0), 1),
7408 /* match outgoing packets */
7409 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7411 /* match incoming packets */
7412 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7419 * Match packets sent by this machine.
7421 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7424 * Match packets sent to this machine.
7425 * (No broadcast or multicast packets, or
7426 * packets sent to some other machine and
7427 * received promiscuously.)
7429 * XXX - packets sent to other machines probably
7430 * shouldn't be matched, but what about broadcast
7431 * or multicast packets we received?
7433 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7437 #ifdef HAVE_NET_PFVAR_H
7439 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7440 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7446 /* match outgoing packets */
7447 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7449 /* match incoming packets */
7450 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7454 case DLT_JUNIPER_MFR
:
7455 case DLT_JUNIPER_MLFR
:
7456 case DLT_JUNIPER_MLPPP
:
7457 case DLT_JUNIPER_ATM1
:
7458 case DLT_JUNIPER_ATM2
:
7459 case DLT_JUNIPER_PPPOE
:
7460 case DLT_JUNIPER_PPPOE_ATM
:
7461 case DLT_JUNIPER_GGSN
:
7462 case DLT_JUNIPER_ES
:
7463 case DLT_JUNIPER_MONITOR
:
7464 case DLT_JUNIPER_SERVICES
:
7465 case DLT_JUNIPER_ETHER
:
7466 case DLT_JUNIPER_PPP
:
7467 case DLT_JUNIPER_FRELAY
:
7468 case DLT_JUNIPER_CHDLC
:
7469 case DLT_JUNIPER_VP
:
7470 case DLT_JUNIPER_ST
:
7471 case DLT_JUNIPER_ISM
:
7472 /* juniper flags (including direction) are stored
7473 * the byte after the 3-byte magic number */
7475 /* match outgoing packets */
7476 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7478 /* match incoming packets */
7479 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7484 bpf_error("inbound/outbound not supported on linktype %d",
7492 #ifdef HAVE_NET_PFVAR_H
7493 /* PF firewall log matched interface */
7495 gen_pf_ifname(const char *ifname
)
7500 if (linktype
!= DLT_PFLOG
) {
7501 bpf_error("ifname supported only on PF linktype");
7504 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7505 off
= offsetof(struct pfloghdr
, ifname
);
7506 if (strlen(ifname
) >= len
) {
7507 bpf_error("ifname interface names can only be %d characters",
7511 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7515 /* PF firewall log ruleset name */
7517 gen_pf_ruleset(char *ruleset
)
7521 if (linktype
!= DLT_PFLOG
) {
7522 bpf_error("ruleset supported only on PF linktype");
7526 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7527 bpf_error("ruleset names can only be %ld characters",
7528 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7532 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7533 strlen(ruleset
), (const u_char
*)ruleset
);
7537 /* PF firewall log rule number */
7543 if (linktype
!= DLT_PFLOG
) {
7544 bpf_error("rnr supported only on PF linktype");
7548 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7553 /* PF firewall log sub-rule number */
7555 gen_pf_srnr(int srnr
)
7559 if (linktype
!= DLT_PFLOG
) {
7560 bpf_error("srnr supported only on PF linktype");
7564 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7569 /* PF firewall log reason code */
7571 gen_pf_reason(int reason
)
7575 if (linktype
!= DLT_PFLOG
) {
7576 bpf_error("reason supported only on PF linktype");
7580 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7585 /* PF firewall log action */
7587 gen_pf_action(int action
)
7591 if (linktype
!= DLT_PFLOG
) {
7592 bpf_error("action supported only on PF linktype");
7596 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7600 #else /* !HAVE_NET_PFVAR_H */
7602 gen_pf_ifname(const char *ifname
)
7604 bpf_error("libpcap was compiled without pf support");
7610 gen_pf_ruleset(char *ruleset
)
7612 bpf_error("libpcap was compiled on a machine without pf support");
7620 bpf_error("libpcap was compiled on a machine without pf support");
7626 gen_pf_srnr(int srnr
)
7628 bpf_error("libpcap was compiled on a machine without pf support");
7634 gen_pf_reason(int reason
)
7636 bpf_error("libpcap was compiled on a machine without pf support");
7642 gen_pf_action(int action
)
7644 bpf_error("libpcap was compiled on a machine without pf support");
7648 #endif /* HAVE_NET_PFVAR_H */
7650 /* IEEE 802.11 wireless header */
7652 gen_p80211_type(int type
, int mask
)
7658 case DLT_IEEE802_11
:
7659 case DLT_PRISM_HEADER
:
7660 case DLT_IEEE802_11_RADIO_AVS
:
7661 case DLT_IEEE802_11_RADIO
:
7662 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7667 bpf_error("802.11 link-layer types supported only on 802.11");
7675 gen_p80211_fcdir(int fcdir
)
7681 case DLT_IEEE802_11
:
7682 case DLT_PRISM_HEADER
:
7683 case DLT_IEEE802_11_RADIO_AVS
:
7684 case DLT_IEEE802_11_RADIO
:
7688 bpf_error("frame direction supported only with 802.11 headers");
7692 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7693 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7700 register const u_char
*eaddr
;
7706 case DLT_ARCNET_LINUX
:
7707 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7709 return (gen_ahostop(eaddr
, (int)q
.dir
));
7711 bpf_error("ARCnet address used in non-arc expression");
7717 bpf_error("aid supported only on ARCnet");
7720 bpf_error("ARCnet address used in non-arc expression");
7725 static struct block
*
7726 gen_ahostop(eaddr
, dir
)
7727 register const u_char
*eaddr
;
7730 register struct block
*b0
, *b1
;
7733 /* src comes first, different from Ethernet */
7735 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7738 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7741 b0
= gen_ahostop(eaddr
, Q_SRC
);
7742 b1
= gen_ahostop(eaddr
, Q_DST
);
7748 b0
= gen_ahostop(eaddr
, Q_SRC
);
7749 b1
= gen_ahostop(eaddr
, Q_DST
);
7758 * support IEEE 802.1Q VLAN trunk over ethernet
7764 struct block
*b0
, *b1
;
7766 /* can't check for VLAN-encapsulated packets inside MPLS */
7767 if (label_stack_depth
> 0)
7768 bpf_error("no VLAN match after MPLS");
7771 * Check for a VLAN packet, and then change the offsets to point
7772 * to the type and data fields within the VLAN packet. Just
7773 * increment the offsets, so that we can support a hierarchy, e.g.
7774 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7777 * XXX - this is a bit of a kludge. If we were to split the
7778 * compiler into a parser that parses an expression and
7779 * generates an expression tree, and a code generator that
7780 * takes an expression tree (which could come from our
7781 * parser or from some other parser) and generates BPF code,
7782 * we could perhaps make the offsets parameters of routines
7783 * and, in the handler for an "AND" node, pass to subnodes
7784 * other than the VLAN node the adjusted offsets.
7786 * This would mean that "vlan" would, instead of changing the
7787 * behavior of *all* tests after it, change only the behavior
7788 * of tests ANDed with it. That would change the documented
7789 * semantics of "vlan", which might break some expressions.
7790 * However, it would mean that "(vlan and ip) or ip" would check
7791 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7792 * checking only for VLAN-encapsulated IP, so that could still
7793 * be considered worth doing; it wouldn't break expressions
7794 * that are of the form "vlan and ..." or "vlan N and ...",
7795 * which I suspect are the most common expressions involving
7796 * "vlan". "vlan or ..." doesn't necessarily do what the user
7797 * would really want, now, as all the "or ..." tests would
7798 * be done assuming a VLAN, even though the "or" could be viewed
7799 * as meaning "or, if this isn't a VLAN packet...".
7806 /* check for VLAN */
7807 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7808 (bpf_int32
)ETHERTYPE_8021Q
);
7810 /* If a specific VLAN is requested, check VLAN id */
7811 if (vlan_num
>= 0) {
7812 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7813 (bpf_int32
)vlan_num
, 0x0fff);
7827 bpf_error("no VLAN support for data link type %d",
7842 struct block
*b0
,*b1
;
7845 * Change the offsets to point to the type and data fields within
7846 * the MPLS packet. Just increment the offsets, so that we
7847 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7848 * capture packets with an outer label of 100000 and an inner
7851 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7855 if (label_stack_depth
> 0) {
7856 /* just match the bottom-of-stack bit clear */
7857 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7860 * Indicate that we're checking MPLS-encapsulated headers,
7861 * to make sure higher level code generators don't try to
7862 * match against IP-related protocols such as Q_ARP, Q_RARP
7867 case DLT_C_HDLC
: /* fall through */
7869 b0
= gen_linktype(ETHERTYPE_MPLS
);
7873 b0
= gen_linktype(PPP_MPLS_UCAST
);
7876 /* FIXME add other DLT_s ...
7877 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7878 * leave it for now */
7881 bpf_error("no MPLS support for data link type %d",
7889 /* If a specific MPLS label is requested, check it */
7890 if (label_num
>= 0) {
7891 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7892 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7893 0xfffff000); /* only compare the first 20 bits */
7900 label_stack_depth
++;
7905 * Support PPPOE discovery and session.
7910 /* check for PPPoE discovery */
7911 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7920 * Test against the PPPoE session link-layer type.
7922 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7925 * Change the offsets to point to the type and data fields within
7926 * the PPP packet, and note that this is PPPoE rather than
7929 * XXX - this is a bit of a kludge. If we were to split the
7930 * compiler into a parser that parses an expression and
7931 * generates an expression tree, and a code generator that
7932 * takes an expression tree (which could come from our
7933 * parser or from some other parser) and generates BPF code,
7934 * we could perhaps make the offsets parameters of routines
7935 * and, in the handler for an "AND" node, pass to subnodes
7936 * other than the PPPoE node the adjusted offsets.
7938 * This would mean that "pppoes" would, instead of changing the
7939 * behavior of *all* tests after it, change only the behavior
7940 * of tests ANDed with it. That would change the documented
7941 * semantics of "pppoes", which might break some expressions.
7942 * However, it would mean that "(pppoes and ip) or ip" would check
7943 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7944 * checking only for VLAN-encapsulated IP, so that could still
7945 * be considered worth doing; it wouldn't break expressions
7946 * that are of the form "pppoes and ..." which I suspect are the
7947 * most common expressions involving "pppoes". "pppoes or ..."
7948 * doesn't necessarily do what the user would really want, now,
7949 * as all the "or ..." tests would be done assuming PPPoE, even
7950 * though the "or" could be viewed as meaning "or, if this isn't
7951 * a PPPoE packet...".
7953 orig_linktype
= off_linktype
; /* save original values */
7958 * The "network-layer" protocol is PPPoE, which has a 6-byte
7959 * PPPoE header, followed by a PPP packet.
7961 * There is no HDLC encapsulation for the PPP packet (it's
7962 * encapsulated in PPPoES instead), so the link-layer type
7963 * starts at the first byte of the PPP packet. For PPPoE,
7964 * that offset is relative to the beginning of the total
7965 * link-layer payload, including any 802.2 LLC header, so
7966 * it's 6 bytes past off_nl.
7968 off_linktype
= off_nl
+ 6;
7971 * The network-layer offsets are relative to the beginning
7972 * of the MAC-layer payload; that's past the 6-byte
7973 * PPPoE header and the 2-byte PPP header.
7976 off_nl_nosnap
= 6+2;
7982 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7994 bpf_error("'vpi' supported only on raw ATM");
7995 if (off_vpi
== (u_int
)-1)
7997 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8003 bpf_error("'vci' supported only on raw ATM");
8004 if (off_vci
== (u_int
)-1)
8006 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8011 if (off_proto
== (u_int
)-1)
8012 abort(); /* XXX - this isn't on FreeBSD */
8013 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8018 if (off_payload
== (u_int
)-1)
8020 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8021 0xffffffff, jtype
, reverse
, jvalue
);
8026 bpf_error("'callref' supported only on raw ATM");
8027 if (off_proto
== (u_int
)-1)
8029 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8030 jtype
, reverse
, jvalue
);
8040 gen_atmtype_abbrev(type
)
8043 struct block
*b0
, *b1
;
8048 /* Get all packets in Meta signalling Circuit */
8050 bpf_error("'metac' supported only on raw ATM");
8051 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8052 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8057 /* Get all packets in Broadcast Circuit*/
8059 bpf_error("'bcc' supported only on raw ATM");
8060 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8061 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8066 /* Get all cells in Segment OAM F4 circuit*/
8068 bpf_error("'oam4sc' supported only on raw ATM");
8069 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8070 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8075 /* Get all cells in End-to-End OAM F4 Circuit*/
8077 bpf_error("'oam4ec' supported only on raw ATM");
8078 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8079 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8084 /* Get all packets in connection Signalling Circuit */
8086 bpf_error("'sc' supported only on raw ATM");
8087 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8088 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8093 /* Get all packets in ILMI Circuit */
8095 bpf_error("'ilmic' supported only on raw ATM");
8096 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8097 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8102 /* Get all LANE packets */
8104 bpf_error("'lane' supported only on raw ATM");
8105 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8108 * Arrange that all subsequent tests assume LANE
8109 * rather than LLC-encapsulated packets, and set
8110 * the offsets appropriately for LANE-encapsulated
8113 * "off_mac" is the offset of the Ethernet header,
8114 * which is 2 bytes past the ATM pseudo-header
8115 * (skipping the pseudo-header and 2-byte LE Client
8116 * field). The other offsets are Ethernet offsets
8117 * relative to "off_mac".
8120 off_mac
= off_payload
+ 2; /* MAC header */
8121 off_linktype
= off_mac
+ 12;
8122 off_macpl
= off_mac
+ 14; /* Ethernet */
8123 off_nl
= 0; /* Ethernet II */
8124 off_nl_nosnap
= 3; /* 802.3+802.2 */
8128 /* Get all LLC-encapsulated packets */
8130 bpf_error("'llc' supported only on raw ATM");
8131 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8142 * Filtering for MTP2 messages based on li value
8143 * FISU, length is null
8144 * LSSU, length is 1 or 2
8145 * MSU, length is 3 or more
8148 gen_mtp2type_abbrev(type
)
8151 struct block
*b0
, *b1
;
8156 if ( (linktype
!= DLT_MTP2
) &&
8157 (linktype
!= DLT_ERF
) &&
8158 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8159 bpf_error("'fisu' supported only on MTP2");
8160 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8161 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8165 if ( (linktype
!= DLT_MTP2
) &&
8166 (linktype
!= DLT_ERF
) &&
8167 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8168 bpf_error("'lssu' supported only on MTP2");
8169 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8170 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8175 if ( (linktype
!= DLT_MTP2
) &&
8176 (linktype
!= DLT_ERF
) &&
8177 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8178 bpf_error("'msu' supported only on MTP2");
8179 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8189 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8196 bpf_u_int32 val1
, val2
, val3
;
8198 switch (mtp3field
) {
8201 if (off_sio
== (u_int
)-1)
8202 bpf_error("'sio' supported only on SS7");
8203 /* sio coded on 1 byte so max value 255 */
8205 bpf_error("sio value %u too big; max value = 255",
8207 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8208 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8212 if (off_opc
== (u_int
)-1)
8213 bpf_error("'opc' supported only on SS7");
8214 /* opc coded on 14 bits so max value 16383 */
8216 bpf_error("opc value %u too big; max value = 16383",
8218 /* the following instructions are made to convert jvalue
8219 * to the form used to write opc in an ss7 message*/
8220 val1
= jvalue
& 0x00003c00;
8222 val2
= jvalue
& 0x000003fc;
8224 val3
= jvalue
& 0x00000003;
8226 jvalue
= val1
+ val2
+ val3
;
8227 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8228 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8232 if (off_dpc
== (u_int
)-1)
8233 bpf_error("'dpc' supported only on SS7");
8234 /* dpc coded on 14 bits so max value 16383 */
8236 bpf_error("dpc value %u too big; max value = 16383",
8238 /* the following instructions are made to convert jvalue
8239 * to the forme used to write dpc in an ss7 message*/
8240 val1
= jvalue
& 0x000000ff;
8242 val2
= jvalue
& 0x00003f00;
8244 jvalue
= val1
+ val2
;
8245 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8246 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8250 if (off_sls
== (u_int
)-1)
8251 bpf_error("'sls' supported only on SS7");
8252 /* sls coded on 4 bits so max value 15 */
8254 bpf_error("sls value %u too big; max value = 15",
8256 /* the following instruction is made to convert jvalue
8257 * to the forme used to write sls in an ss7 message*/
8258 jvalue
= jvalue
<< 4;
8259 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8260 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8269 static struct block
*
8270 gen_msg_abbrev(type
)
8276 * Q.2931 signalling protocol messages for handling virtual circuits
8277 * establishment and teardown
8282 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8286 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8290 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8294 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8298 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8301 case A_RELEASE_DONE
:
8302 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8312 gen_atmmulti_abbrev(type
)
8315 struct block
*b0
, *b1
;
8321 bpf_error("'oam' supported only on raw ATM");
8322 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8327 bpf_error("'oamf4' supported only on raw ATM");
8329 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8330 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8332 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8338 * Get Q.2931 signalling messages for switched
8339 * virtual connection
8342 bpf_error("'connectmsg' supported only on raw ATM");
8343 b0
= gen_msg_abbrev(A_SETUP
);
8344 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8346 b0
= gen_msg_abbrev(A_CONNECT
);
8348 b0
= gen_msg_abbrev(A_CONNECTACK
);
8350 b0
= gen_msg_abbrev(A_RELEASE
);
8352 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8354 b0
= gen_atmtype_abbrev(A_SC
);
8360 bpf_error("'metaconnect' supported only on raw ATM");
8361 b0
= gen_msg_abbrev(A_SETUP
);
8362 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8364 b0
= gen_msg_abbrev(A_CONNECT
);
8366 b0
= gen_msg_abbrev(A_RELEASE
);
8368 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8370 b0
= gen_atmtype_abbrev(A_METAC
);