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"
78 #ifdef HAVE_NET_PFVAR_H
79 #include <sys/socket.h>
81 #include <net/pfvar.h>
82 #include <net/if_pflog.h>
85 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #include <netdb.h> /* for "struct addrinfo" */
92 #include <pcap/namedb.h>
97 #define IPPROTO_SCTP 132
100 #ifdef HAVE_OS_PROTO_H
101 #include "os-proto.h"
104 #define JMP(c) ((c)|BPF_JMP|BPF_K)
107 static jmp_buf top_ctx
;
108 static pcap_t
*bpf_pcap
;
110 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
112 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
114 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
119 static int pcap_fddipad
;
124 bpf_error(const char *fmt
, ...)
129 if (bpf_pcap
!= NULL
)
130 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
137 static void init_linktype(pcap_t
*);
139 static void init_regs(void);
140 static int alloc_reg(void);
141 static void free_reg(int);
143 static struct block
*root
;
146 * Value passed to gen_load_a() to indicate what the offset argument
150 OR_PACKET
, /* relative to the beginning of the packet */
151 OR_LINK
, /* relative to the beginning of the link-layer header */
152 OR_MACPL
, /* relative to the end of the MAC-layer header */
153 OR_NET
, /* relative to the network-layer header */
154 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
155 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
156 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
161 * As errors are handled by a longjmp, anything allocated must be freed
162 * in the longjmp handler, so it must be reachable from that handler.
163 * One thing that's allocated is the result of pcap_nametoaddrinfo();
164 * it must be freed with freeaddrinfo(). This variable points to any
165 * addrinfo structure that would need to be freed.
167 static struct addrinfo
*ai
;
171 * We divy out chunks of memory rather than call malloc each time so
172 * we don't have to worry about leaking memory. It's probably
173 * not a big deal if all this memory was wasted but if this ever
174 * goes into a library that would probably not be a good idea.
176 * XXX - this *is* in a library....
179 #define CHUNK0SIZE 1024
185 static struct chunk chunks
[NCHUNKS
];
186 static int cur_chunk
;
188 static void *newchunk(u_int
);
189 static void freechunks(void);
190 static inline struct block
*new_block(int);
191 static inline struct slist
*new_stmt(int);
192 static struct block
*gen_retblk(int);
193 static inline void syntax(void);
195 static void backpatch(struct block
*, struct block
*);
196 static void merge(struct block
*, struct block
*);
197 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
198 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
199 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
200 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
201 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
202 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
204 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
205 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
206 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
207 static struct slist
*gen_load_llrel(u_int
, u_int
);
208 static struct slist
*gen_load_macplrel(u_int
, u_int
);
209 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
210 static struct slist
*gen_loadx_iphdrlen(void);
211 static struct block
*gen_uncond(int);
212 static inline struct block
*gen_true(void);
213 static inline struct block
*gen_false(void);
214 static struct block
*gen_ether_linktype(int);
215 static struct block
*gen_linux_sll_linktype(int);
216 static struct slist
*gen_load_prism_llprefixlen(void);
217 static struct slist
*gen_load_avs_llprefixlen(void);
218 static struct slist
*gen_load_radiotap_llprefixlen(void);
219 static struct slist
*gen_load_ppi_llprefixlen(void);
220 static void insert_compute_vloffsets(struct block
*);
221 static struct slist
*gen_llprefixlen(void);
222 static struct slist
*gen_off_macpl(void);
223 static int ethertype_to_ppptype(int);
224 static struct block
*gen_linktype(int);
225 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
226 static struct block
*gen_llc_linktype(int);
227 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
229 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
231 static struct block
*gen_ahostop(const u_char
*, int);
232 static struct block
*gen_ehostop(const u_char
*, int);
233 static struct block
*gen_fhostop(const u_char
*, int);
234 static struct block
*gen_thostop(const u_char
*, int);
235 static struct block
*gen_wlanhostop(const u_char
*, int);
236 static struct block
*gen_ipfchostop(const u_char
*, int);
237 static struct block
*gen_dnhostop(bpf_u_int32
, int);
238 static struct block
*gen_mpls_linktype(int);
239 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
241 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
244 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
246 static struct block
*gen_ipfrag(void);
247 static struct block
*gen_portatom(int, bpf_int32
);
248 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
250 static struct block
*gen_portatom6(int, bpf_int32
);
251 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
253 struct block
*gen_portop(int, int, int);
254 static struct block
*gen_port(int, int, int);
255 struct block
*gen_portrangeop(int, int, int, int);
256 static struct block
*gen_portrange(int, int, int, int);
258 struct block
*gen_portop6(int, int, int);
259 static struct block
*gen_port6(int, int, int);
260 struct block
*gen_portrangeop6(int, int, int, int);
261 static struct block
*gen_portrange6(int, int, int, int);
263 static int lookup_proto(const char *, int);
264 static struct block
*gen_protochain(int, int, int);
265 static struct block
*gen_proto(int, int, int);
266 static struct slist
*xfer_to_x(struct arth
*);
267 static struct slist
*xfer_to_a(struct arth
*);
268 static struct block
*gen_mac_multicast(int);
269 static struct block
*gen_len(int, int);
270 static struct block
*gen_check_802_11_data_frame(void);
272 static struct block
*gen_ppi_dlt_check(void);
273 static struct block
*gen_msg_abbrev(int type
);
284 /* XXX Round up to nearest long. */
285 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
287 /* XXX Round up to structure boundary. */
291 cp
= &chunks
[cur_chunk
];
292 if (n
> cp
->n_left
) {
293 ++cp
, k
= ++cur_chunk
;
295 bpf_error("out of memory");
296 size
= CHUNK0SIZE
<< k
;
297 cp
->m
= (void *)malloc(size
);
299 bpf_error("out of memory");
300 memset((char *)cp
->m
, 0, size
);
303 bpf_error("out of memory");
306 return (void *)((char *)cp
->m
+ cp
->n_left
);
315 for (i
= 0; i
< NCHUNKS
; ++i
)
316 if (chunks
[i
].m
!= NULL
) {
323 * A strdup whose allocations are freed after code generation is over.
327 register const char *s
;
329 int n
= strlen(s
) + 1;
330 char *cp
= newchunk(n
);
336 static inline struct block
*
342 p
= (struct block
*)newchunk(sizeof(*p
));
349 static inline struct slist
*
355 p
= (struct slist
*)newchunk(sizeof(*p
));
361 static struct block
*
365 struct block
*b
= new_block(BPF_RET
|BPF_K
);
374 bpf_error("syntax error in filter expression");
377 static bpf_u_int32 netmask
;
382 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
383 const char *buf
, int optimize
, bpf_u_int32 mask
);
386 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
387 const char *buf
, int optimize
, bpf_u_int32 mask
)
391 EnterCriticalSection(&g_PcapCompileCriticalSection
);
393 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
395 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
401 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
402 const char *buf
, int optimize
, bpf_u_int32 mask
)
405 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
406 const char *buf
, int optimize
, bpf_u_int32 mask
)
410 const char * volatile xbuf
= buf
;
418 if (setjmp(top_ctx
)) {
432 snaplen
= pcap_snapshot(p
);
434 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
435 "snaplen of 0 rejects all packets");
439 lex_init(xbuf
? xbuf
: "");
447 root
= gen_retblk(snaplen
);
449 if (optimize
&& !no_optimize
) {
452 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
453 bpf_error("expression rejects all packets");
455 program
->bf_insns
= icode_to_fcode(root
, &len
);
456 program
->bf_len
= len
;
464 * entry point for using the compiler with no pcap open
465 * pass in all the stuff that is needed explicitly instead.
468 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
469 struct bpf_program
*program
,
470 const char *buf
, int optimize
, bpf_u_int32 mask
)
475 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
478 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
484 * Clean up a "struct bpf_program" by freeing all the memory allocated
488 pcap_freecode(struct bpf_program
*program
)
491 if (program
->bf_insns
!= NULL
) {
492 free((char *)program
->bf_insns
);
493 program
->bf_insns
= NULL
;
498 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
499 * which of the jt and jf fields has been resolved and which is a pointer
500 * back to another unresolved block (or nil). At least one of the fields
501 * in each block is already resolved.
504 backpatch(list
, target
)
505 struct block
*list
, *target
;
522 * Merge the lists in b0 and b1, using the 'sense' field to indicate
523 * which of jt and jf is the link.
527 struct block
*b0
, *b1
;
529 register struct block
**p
= &b0
;
531 /* Find end of list. */
533 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
535 /* Concatenate the lists. */
543 struct block
*ppi_dlt_check
;
546 * Insert before the statements of the first (root) block any
547 * statements needed to load the lengths of any variable-length
548 * headers into registers.
550 * XXX - a fancier strategy would be to insert those before the
551 * statements of all blocks that use those lengths and that
552 * have no predecessors that use them, so that we only compute
553 * the lengths if we need them. There might be even better
554 * approaches than that.
556 * However, those strategies would be more complicated, and
557 * as we don't generate code to compute a length if the
558 * program has no tests that use the length, and as most
559 * tests will probably use those lengths, we would just
560 * postpone computing the lengths so that it's not done
561 * for tests that fail early, and it's not clear that's
564 insert_compute_vloffsets(p
->head
);
567 * For DLT_PPI captures, generate a check of the per-packet
568 * DLT value to make sure it's DLT_IEEE802_11.
570 ppi_dlt_check
= gen_ppi_dlt_check();
571 if (ppi_dlt_check
!= NULL
)
572 gen_and(ppi_dlt_check
, p
);
574 backpatch(p
, gen_retblk(snaplen
));
575 p
->sense
= !p
->sense
;
576 backpatch(p
, gen_retblk(0));
582 struct block
*b0
, *b1
;
584 backpatch(b0
, b1
->head
);
585 b0
->sense
= !b0
->sense
;
586 b1
->sense
= !b1
->sense
;
588 b1
->sense
= !b1
->sense
;
594 struct block
*b0
, *b1
;
596 b0
->sense
= !b0
->sense
;
597 backpatch(b0
, b1
->head
);
598 b0
->sense
= !b0
->sense
;
607 b
->sense
= !b
->sense
;
610 static struct block
*
611 gen_cmp(offrel
, offset
, size
, v
)
612 enum e_offrel offrel
;
616 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
619 static struct block
*
620 gen_cmp_gt(offrel
, offset
, size
, v
)
621 enum e_offrel offrel
;
625 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
628 static struct block
*
629 gen_cmp_ge(offrel
, offset
, size
, v
)
630 enum e_offrel offrel
;
634 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
637 static struct block
*
638 gen_cmp_lt(offrel
, offset
, size
, v
)
639 enum e_offrel offrel
;
643 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
646 static struct block
*
647 gen_cmp_le(offrel
, offset
, size
, v
)
648 enum e_offrel offrel
;
652 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
655 static struct block
*
656 gen_mcmp(offrel
, offset
, size
, v
, mask
)
657 enum e_offrel offrel
;
662 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
665 static struct block
*
666 gen_bcmp(offrel
, offset
, size
, v
)
667 enum e_offrel offrel
;
668 register u_int offset
, size
;
669 register const u_char
*v
;
671 register struct block
*b
, *tmp
;
675 register const u_char
*p
= &v
[size
- 4];
676 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
677 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
679 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
686 register const u_char
*p
= &v
[size
- 2];
687 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
689 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
696 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
705 * AND the field of size "size" at offset "offset" relative to the header
706 * specified by "offrel" with "mask", and compare it with the value "v"
707 * with the test specified by "jtype"; if "reverse" is true, the test
708 * should test the opposite of "jtype".
710 static struct block
*
711 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
712 enum e_offrel offrel
;
714 bpf_u_int32 offset
, size
, mask
, jtype
;
717 struct slist
*s
, *s2
;
720 s
= gen_load_a(offrel
, offset
, size
);
722 if (mask
!= 0xffffffff) {
723 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
728 b
= new_block(JMP(jtype
));
731 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
737 * Various code constructs need to know the layout of the data link
738 * layer. These variables give the necessary offsets from the beginning
739 * of the packet data.
743 * This is the offset of the beginning of the link-layer header from
744 * the beginning of the raw packet data.
746 * It's usually 0, except for 802.11 with a fixed-length radio header.
747 * (For 802.11 with a variable-length radio header, we have to generate
748 * code to compute that offset; off_ll is 0 in that case.)
753 * If there's a variable-length header preceding the link-layer header,
754 * "reg_off_ll" is the register number for a register containing the
755 * length of that header, and therefore the offset of the link-layer
756 * header from the beginning of the raw packet data. Otherwise,
757 * "reg_off_ll" is -1.
759 static int reg_off_ll
;
762 * This is the offset of the beginning of the MAC-layer header from
763 * the beginning of the link-layer header.
764 * It's usually 0, except for ATM LANE, where it's the offset, relative
765 * to the beginning of the raw packet data, of the Ethernet header.
767 static u_int off_mac
;
770 * This is the offset of the beginning of the MAC-layer payload,
771 * from the beginning of the raw packet data.
773 * I.e., it's the sum of the length of the link-layer header (without,
774 * for example, any 802.2 LLC header, so it's the MAC-layer
775 * portion of that header), plus any prefix preceding the
778 static u_int off_macpl
;
781 * This is 1 if the offset of the beginning of the MAC-layer payload
782 * from the beginning of the link-layer header is variable-length.
784 static int off_macpl_is_variable
;
787 * If the link layer has variable_length headers, "reg_off_macpl"
788 * is the register number for a register containing the length of the
789 * link-layer header plus the length of any variable-length header
790 * preceding the link-layer header. Otherwise, "reg_off_macpl"
793 static int reg_off_macpl
;
796 * "off_linktype" is the offset to information in the link-layer header
797 * giving the packet type. This offset is relative to the beginning
798 * of the link-layer header (i.e., it doesn't include off_ll).
800 * For Ethernet, it's the offset of the Ethernet type field.
802 * For link-layer types that always use 802.2 headers, it's the
803 * offset of the LLC header.
805 * For PPP, it's the offset of the PPP type field.
807 * For Cisco HDLC, it's the offset of the CHDLC type field.
809 * For BSD loopback, it's the offset of the AF_ value.
811 * For Linux cooked sockets, it's the offset of the type field.
813 * It's set to -1 for no encapsulation, in which case, IP is assumed.
815 static u_int off_linktype
;
818 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
819 * checks to check the PPP header, assumed to follow a LAN-style link-
820 * layer header and a PPPoE session header.
822 static int is_pppoes
= 0;
825 * TRUE if the link layer includes an ATM pseudo-header.
827 static int is_atm
= 0;
830 * TRUE if "lane" appeared in the filter; it causes us to generate
831 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
833 static int is_lane
= 0;
836 * These are offsets for the ATM pseudo-header.
838 static u_int off_vpi
;
839 static u_int off_vci
;
840 static u_int off_proto
;
843 * These are offsets for the MTP2 fields.
848 * These are offsets for the MTP3 fields.
850 static u_int off_sio
;
851 static u_int off_opc
;
852 static u_int off_dpc
;
853 static u_int off_sls
;
856 * This is the offset of the first byte after the ATM pseudo_header,
857 * or -1 if there is no ATM pseudo-header.
859 static u_int off_payload
;
862 * These are offsets to the beginning of the network-layer header.
863 * They are relative to the beginning of the MAC-layer payload (i.e.,
864 * they don't include off_ll or off_macpl).
866 * If the link layer never uses 802.2 LLC:
868 * "off_nl" and "off_nl_nosnap" are the same.
870 * If the link layer always uses 802.2 LLC:
872 * "off_nl" is the offset if there's a SNAP header following
875 * "off_nl_nosnap" is the offset if there's no SNAP header.
877 * If the link layer is Ethernet:
879 * "off_nl" is the offset if the packet is an Ethernet II packet
880 * (we assume no 802.3+802.2+SNAP);
882 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
883 * with an 802.2 header following it.
886 static u_int off_nl_nosnap
;
894 linktype
= pcap_datalink(p
);
896 pcap_fddipad
= p
->fddipad
;
900 * Assume it's not raw ATM with a pseudo-header, for now.
911 * And that we're not doing PPPoE.
916 * And assume we're not doing SS7.
925 * Also assume it's not 802.11.
929 off_macpl_is_variable
= 0;
933 label_stack_depth
= 0;
943 off_nl
= 0; /* XXX in reality, variable! */
944 off_nl_nosnap
= 0; /* no 802.2 LLC */
947 case DLT_ARCNET_LINUX
:
950 off_nl
= 0; /* XXX in reality, variable! */
951 off_nl_nosnap
= 0; /* no 802.2 LLC */
956 off_macpl
= 14; /* Ethernet header length */
957 off_nl
= 0; /* Ethernet II */
958 off_nl_nosnap
= 3; /* 802.3+802.2 */
963 * SLIP doesn't have a link level type. The 16 byte
964 * header is hacked into our SLIP driver.
969 off_nl_nosnap
= 0; /* no 802.2 LLC */
973 /* XXX this may be the same as the DLT_PPP_BSDOS case */
978 off_nl_nosnap
= 0; /* no 802.2 LLC */
986 off_nl_nosnap
= 0; /* no 802.2 LLC */
993 off_nl_nosnap
= 0; /* no 802.2 LLC */
998 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
999 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1003 off_nl_nosnap
= 0; /* no 802.2 LLC */
1008 * This does no include the Ethernet header, and
1009 * only covers session state.
1014 off_nl_nosnap
= 0; /* no 802.2 LLC */
1021 off_nl_nosnap
= 0; /* no 802.2 LLC */
1026 * FDDI doesn't really have a link-level type field.
1027 * We set "off_linktype" to the offset of the LLC header.
1029 * To check for Ethernet types, we assume that SSAP = SNAP
1030 * is being used and pick out the encapsulated Ethernet type.
1031 * XXX - should we generate code to check for SNAP?
1035 off_linktype
+= pcap_fddipad
;
1037 off_macpl
= 13; /* FDDI MAC header length */
1039 off_macpl
+= pcap_fddipad
;
1041 off_nl
= 8; /* 802.2+SNAP */
1042 off_nl_nosnap
= 3; /* 802.2 */
1047 * Token Ring doesn't really have a link-level type field.
1048 * We set "off_linktype" to the offset of the LLC header.
1050 * To check for Ethernet types, we assume that SSAP = SNAP
1051 * is being used and pick out the encapsulated Ethernet type.
1052 * XXX - should we generate code to check for SNAP?
1054 * XXX - the header is actually variable-length.
1055 * Some various Linux patched versions gave 38
1056 * as "off_linktype" and 40 as "off_nl"; however,
1057 * if a token ring packet has *no* routing
1058 * information, i.e. is not source-routed, the correct
1059 * values are 20 and 22, as they are in the vanilla code.
1061 * A packet is source-routed iff the uppermost bit
1062 * of the first byte of the source address, at an
1063 * offset of 8, has the uppermost bit set. If the
1064 * packet is source-routed, the total number of bytes
1065 * of routing information is 2 plus bits 0x1F00 of
1066 * the 16-bit value at an offset of 14 (shifted right
1067 * 8 - figure out which byte that is).
1070 off_macpl
= 14; /* Token Ring MAC header length */
1071 off_nl
= 8; /* 802.2+SNAP */
1072 off_nl_nosnap
= 3; /* 802.2 */
1075 case DLT_IEEE802_11
:
1076 case DLT_PRISM_HEADER
:
1077 case DLT_IEEE802_11_RADIO_AVS
:
1078 case DLT_IEEE802_11_RADIO
:
1080 * 802.11 doesn't really have a link-level type field.
1081 * We set "off_linktype" to the offset of the LLC header.
1083 * To check for Ethernet types, we assume that SSAP = SNAP
1084 * is being used and pick out the encapsulated Ethernet type.
1085 * XXX - should we generate code to check for SNAP?
1087 * We also handle variable-length radio headers here.
1088 * The Prism header is in theory variable-length, but in
1089 * practice it's always 144 bytes long. However, some
1090 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1091 * sometimes or always supply an AVS header, so we
1092 * have to check whether the radio header is a Prism
1093 * header or an AVS header, so, in practice, it's
1097 off_macpl
= 0; /* link-layer header is variable-length */
1098 off_macpl_is_variable
= 1;
1099 off_nl
= 8; /* 802.2+SNAP */
1100 off_nl_nosnap
= 3; /* 802.2 */
1105 * At the moment we treat PPI the same way that we treat
1106 * normal Radiotap encoded packets. The difference is in
1107 * the function that generates the code at the beginning
1108 * to compute the header length. Since this code generator
1109 * of PPI supports bare 802.11 encapsulation only (i.e.
1110 * the encapsulated DLT should be DLT_IEEE802_11) we
1111 * generate code to check for this too.
1114 off_macpl
= 0; /* link-layer header is variable-length */
1115 off_macpl_is_variable
= 1;
1116 off_nl
= 8; /* 802.2+SNAP */
1117 off_nl_nosnap
= 3; /* 802.2 */
1120 case DLT_ATM_RFC1483
:
1121 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1123 * assume routed, non-ISO PDUs
1124 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1126 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1127 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1128 * latter would presumably be treated the way PPPoE
1129 * should be, so you can do "pppoe and udp port 2049"
1130 * or "pppoa and tcp port 80" and have it check for
1131 * PPPo{A,E} and a PPP protocol of IP and....
1134 off_macpl
= 0; /* packet begins with LLC header */
1135 off_nl
= 8; /* 802.2+SNAP */
1136 off_nl_nosnap
= 3; /* 802.2 */
1141 * Full Frontal ATM; you get AALn PDUs with an ATM
1145 off_vpi
= SUNATM_VPI_POS
;
1146 off_vci
= SUNATM_VCI_POS
;
1147 off_proto
= PROTO_POS
;
1148 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1149 off_payload
= SUNATM_PKT_BEGIN_POS
;
1150 off_linktype
= off_payload
;
1151 off_macpl
= off_payload
; /* if LLC-encapsulated */
1152 off_nl
= 8; /* 802.2+SNAP */
1153 off_nl_nosnap
= 3; /* 802.2 */
1160 off_nl_nosnap
= 0; /* no 802.2 LLC */
1163 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1167 off_nl_nosnap
= 0; /* no 802.2 LLC */
1172 * LocalTalk does have a 1-byte type field in the LLAP header,
1173 * but really it just indicates whether there is a "short" or
1174 * "long" DDP packet following.
1179 off_nl_nosnap
= 0; /* no 802.2 LLC */
1182 case DLT_IP_OVER_FC
:
1184 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1185 * link-level type field. We set "off_linktype" to the
1186 * offset of the LLC header.
1188 * To check for Ethernet types, we assume that SSAP = SNAP
1189 * is being used and pick out the encapsulated Ethernet type.
1190 * XXX - should we generate code to check for SNAP? RFC
1191 * 2625 says SNAP should be used.
1195 off_nl
= 8; /* 802.2+SNAP */
1196 off_nl_nosnap
= 3; /* 802.2 */
1201 * XXX - we should set this to handle SNAP-encapsulated
1202 * frames (NLPID of 0x80).
1207 off_nl_nosnap
= 0; /* no 802.2 LLC */
1211 * the only BPF-interesting FRF.16 frames are non-control frames;
1212 * Frame Relay has a variable length link-layer
1213 * so lets start with offset 4 for now and increments later on (FIXME);
1219 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1222 case DLT_APPLE_IP_OVER_IEEE1394
:
1226 off_nl_nosnap
= 0; /* no 802.2 LLC */
1229 case DLT_LINUX_IRDA
:
1231 * Currently, only raw "link[N:M]" filtering is supported.
1241 * Currently, only raw "link[N:M]" filtering is supported.
1249 case DLT_SYMANTEC_FIREWALL
:
1252 off_nl
= 0; /* Ethernet II */
1253 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1256 #ifdef HAVE_NET_PFVAR_H
1259 off_macpl
= PFLOG_HDRLEN
;
1261 off_nl_nosnap
= 0; /* no 802.2 LLC */
1265 case DLT_JUNIPER_MFR
:
1266 case DLT_JUNIPER_MLFR
:
1267 case DLT_JUNIPER_MLPPP
:
1268 case DLT_JUNIPER_PPP
:
1269 case DLT_JUNIPER_CHDLC
:
1270 case DLT_JUNIPER_FRELAY
:
1274 off_nl_nosnap
= -1; /* no 802.2 LLC */
1277 case DLT_JUNIPER_ATM1
:
1278 off_linktype
= 4; /* in reality variable between 4-8 */
1279 off_macpl
= 4; /* in reality variable between 4-8 */
1284 case DLT_JUNIPER_ATM2
:
1285 off_linktype
= 8; /* in reality variable between 8-12 */
1286 off_macpl
= 8; /* in reality variable between 8-12 */
1291 /* frames captured on a Juniper PPPoE service PIC
1292 * contain raw ethernet frames */
1293 case DLT_JUNIPER_PPPOE
:
1294 case DLT_JUNIPER_ETHER
:
1297 off_nl
= 18; /* Ethernet II */
1298 off_nl_nosnap
= 21; /* 802.3+802.2 */
1301 case DLT_JUNIPER_PPPOE_ATM
:
1305 off_nl_nosnap
= -1; /* no 802.2 LLC */
1308 case DLT_JUNIPER_GGSN
:
1312 off_nl_nosnap
= -1; /* no 802.2 LLC */
1315 case DLT_JUNIPER_ES
:
1317 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1318 off_nl
= -1; /* not really a network layer but raw IP addresses */
1319 off_nl_nosnap
= -1; /* no 802.2 LLC */
1322 case DLT_JUNIPER_MONITOR
:
1325 off_nl
= 0; /* raw IP/IP6 header */
1326 off_nl_nosnap
= -1; /* no 802.2 LLC */
1329 case DLT_JUNIPER_SERVICES
:
1331 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1332 off_nl
= -1; /* L3 proto location dep. on cookie type */
1333 off_nl_nosnap
= -1; /* no 802.2 LLC */
1336 case DLT_JUNIPER_VP
:
1343 case DLT_JUNIPER_ST
:
1350 case DLT_JUNIPER_ISM
:
1369 case DLT_MTP2_WITH_PHDR
:
1402 case DLT_LINUX_LAPD
:
1404 * Currently, only raw "link[N:M]" filtering is supported.
1414 * Currently, only raw "link[N:M]" filtering is supported.
1422 case DLT_BLUETOOTH_HCI_H4
:
1424 * Currently, only raw "link[N:M]" filtering is supported.
1434 * Currently, only raw "link[N:M]" filtering is supported.
1444 * Currently, only raw "link[N:M]" filtering is supported.
1452 case DLT_IEEE802_15_4_LINUX
:
1454 * Currently, only raw "link[N:M]" filtering is supported.
1462 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1464 * Currently, only raw "link[N:M]" filtering is supported.
1472 case DLT_IEEE802_15_4
:
1474 * Currently, only raw "link[N:M]" filtering is supported.
1484 * Currently, only raw "link[N:M]" filtering is supported.
1494 * Currently, only raw "link[N:M]" filtering is supported.
1504 * Currently, only raw "link[N:M]" filtering is supported.
1512 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1514 * Currently, only raw "link[N:M]" filtering is supported.
1524 * Currently, only raw "link[N:M]" filtering is supported.
1526 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1528 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1529 off_nl_nosnap
= -1; /* no 802.2 LLC */
1530 off_mac
= 1; /* step over the kiss length byte */
1533 case DLT_IEEE802_15_4_NONASK_PHY
:
1535 * Currently, only raw "link[N:M]" filtering is supported.
1545 * Currently, only raw "link[N:M]" filtering is supported.
1553 case DLT_USB_LINUX_MMAPPED
:
1555 * Currently, only raw "link[N:M]" filtering is supported.
1563 bpf_error("unknown data link type %d", linktype
);
1568 * Load a value relative to the beginning of the link-layer header.
1569 * The link-layer header doesn't necessarily begin at the beginning
1570 * of the packet data; there might be a variable-length prefix containing
1571 * radio information.
1573 static struct slist
*
1574 gen_load_llrel(offset
, size
)
1577 struct slist
*s
, *s2
;
1579 s
= gen_llprefixlen();
1582 * If "s" is non-null, it has code to arrange that the X register
1583 * contains the length of the prefix preceding the link-layer
1586 * Otherwise, the length of the prefix preceding the link-layer
1587 * header is "off_ll".
1591 * There's a variable-length prefix preceding the
1592 * link-layer header. "s" points to a list of statements
1593 * that put the length of that prefix into the X register.
1594 * do an indirect load, to use the X register as an offset.
1596 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1601 * There is no variable-length header preceding the
1602 * link-layer header; add in off_ll, which, if there's
1603 * a fixed-length header preceding the link-layer header,
1604 * is the length of that header.
1606 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1607 s
->s
.k
= offset
+ off_ll
;
1613 * Load a value relative to the beginning of the MAC-layer payload.
1615 static struct slist
*
1616 gen_load_macplrel(offset
, size
)
1619 struct slist
*s
, *s2
;
1621 s
= gen_off_macpl();
1624 * If s is non-null, the offset of the MAC-layer payload is
1625 * variable, and s points to a list of instructions that
1626 * arrange that the X register contains that offset.
1628 * Otherwise, the offset of the MAC-layer payload is constant,
1629 * and is in off_macpl.
1633 * The offset of the MAC-layer payload is in the X
1634 * register. Do an indirect load, to use the X register
1637 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1642 * The offset of the MAC-layer payload is constant,
1643 * and is in off_macpl; load the value at that offset
1644 * plus the specified offset.
1646 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1647 s
->s
.k
= off_macpl
+ offset
;
1653 * Load a value relative to the beginning of the specified header.
1655 static struct slist
*
1656 gen_load_a(offrel
, offset
, size
)
1657 enum e_offrel offrel
;
1660 struct slist
*s
, *s2
;
1665 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1670 s
= gen_load_llrel(offset
, size
);
1674 s
= gen_load_macplrel(offset
, size
);
1678 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1682 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1687 * Load the X register with the length of the IPv4 header
1688 * (plus the offset of the link-layer header, if it's
1689 * preceded by a variable-length header such as a radio
1690 * header), in bytes.
1692 s
= gen_loadx_iphdrlen();
1695 * Load the item at {offset of the MAC-layer payload} +
1696 * {offset, relative to the start of the MAC-layer
1697 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1698 * {specified offset}.
1700 * (If the offset of the MAC-layer payload is variable,
1701 * it's included in the value in the X register, and
1704 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1705 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1710 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1721 * Generate code to load into the X register the sum of the length of
1722 * the IPv4 header and any variable-length header preceding the link-layer
1725 static struct slist
*
1726 gen_loadx_iphdrlen()
1728 struct slist
*s
, *s2
;
1730 s
= gen_off_macpl();
1733 * There's a variable-length prefix preceding the
1734 * link-layer header, or the link-layer header is itself
1735 * variable-length. "s" points to a list of statements
1736 * that put the offset of the MAC-layer payload into
1739 * The 4*([k]&0xf) addressing mode can't be used, as we
1740 * don't have a constant offset, so we have to load the
1741 * value in question into the A register and add to it
1742 * the value from the X register.
1744 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1747 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1750 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1755 * The A register now contains the length of the
1756 * IP header. We need to add to it the offset of
1757 * the MAC-layer payload, which is still in the X
1758 * register, and move the result into the X register.
1760 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1761 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1764 * There is no variable-length header preceding the
1765 * link-layer header, and the link-layer header is
1766 * fixed-length; load the length of the IPv4 header,
1767 * which is at an offset of off_nl from the beginning
1768 * of the MAC-layer payload, and thus at an offset
1769 * of off_mac_pl + off_nl from the beginning of the
1772 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1773 s
->s
.k
= off_macpl
+ off_nl
;
1778 static struct block
*
1785 s
= new_stmt(BPF_LD
|BPF_IMM
);
1787 b
= new_block(JMP(BPF_JEQ
));
1793 static inline struct block
*
1796 return gen_uncond(1);
1799 static inline struct block
*
1802 return gen_uncond(0);
1806 * Byte-swap a 32-bit number.
1807 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1808 * big-endian platforms.)
1810 #define SWAPLONG(y) \
1811 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1814 * Generate code to match a particular packet type.
1816 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1817 * value, if <= ETHERMTU. We use that to determine whether to
1818 * match the type/length field or to check the type/length field for
1819 * a value <= ETHERMTU to see whether it's a type field and then do
1820 * the appropriate test.
1822 static struct block
*
1823 gen_ether_linktype(proto
)
1826 struct block
*b0
, *b1
;
1832 case LLCSAP_NETBEUI
:
1834 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1835 * so we check the DSAP and SSAP.
1837 * LLCSAP_IP checks for IP-over-802.2, rather
1838 * than IP-over-Ethernet or IP-over-SNAP.
1840 * XXX - should we check both the DSAP and the
1841 * SSAP, like this, or should we check just the
1842 * DSAP, as we do for other types <= ETHERMTU
1843 * (i.e., other SAP values)?
1845 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1847 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1848 ((proto
<< 8) | proto
));
1856 * Ethernet_II frames, which are Ethernet
1857 * frames with a frame type of ETHERTYPE_IPX;
1859 * Ethernet_802.3 frames, which are 802.3
1860 * frames (i.e., the type/length field is
1861 * a length field, <= ETHERMTU, rather than
1862 * a type field) with the first two bytes
1863 * after the Ethernet/802.3 header being
1866 * Ethernet_802.2 frames, which are 802.3
1867 * frames with an 802.2 LLC header and
1868 * with the IPX LSAP as the DSAP in the LLC
1871 * Ethernet_SNAP frames, which are 802.3
1872 * frames with an LLC header and a SNAP
1873 * header and with an OUI of 0x000000
1874 * (encapsulated Ethernet) and a protocol
1875 * ID of ETHERTYPE_IPX in the SNAP header.
1877 * XXX - should we generate the same code both
1878 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1882 * This generates code to check both for the
1883 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1885 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1886 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1890 * Now we add code to check for SNAP frames with
1891 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1893 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1897 * Now we generate code to check for 802.3
1898 * frames in general.
1900 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1904 * Now add the check for 802.3 frames before the
1905 * check for Ethernet_802.2 and Ethernet_802.3,
1906 * as those checks should only be done on 802.3
1907 * frames, not on Ethernet frames.
1912 * Now add the check for Ethernet_II frames, and
1913 * do that before checking for the other frame
1916 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1917 (bpf_int32
)ETHERTYPE_IPX
);
1921 case ETHERTYPE_ATALK
:
1922 case ETHERTYPE_AARP
:
1924 * EtherTalk (AppleTalk protocols on Ethernet link
1925 * layer) may use 802.2 encapsulation.
1929 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1930 * we check for an Ethernet type field less than
1931 * 1500, which means it's an 802.3 length field.
1933 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1937 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1938 * SNAP packets with an organization code of
1939 * 0x080007 (Apple, for Appletalk) and a protocol
1940 * type of ETHERTYPE_ATALK (Appletalk).
1942 * 802.2-encapsulated ETHERTYPE_AARP packets are
1943 * SNAP packets with an organization code of
1944 * 0x000000 (encapsulated Ethernet) and a protocol
1945 * type of ETHERTYPE_AARP (Appletalk ARP).
1947 if (proto
== ETHERTYPE_ATALK
)
1948 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1949 else /* proto == ETHERTYPE_AARP */
1950 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1954 * Check for Ethernet encapsulation (Ethertalk
1955 * phase 1?); we just check for the Ethernet
1958 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1964 if (proto
<= ETHERMTU
) {
1966 * This is an LLC SAP value, so the frames
1967 * that match would be 802.2 frames.
1968 * Check that the frame is an 802.2 frame
1969 * (i.e., that the length/type field is
1970 * a length field, <= ETHERMTU) and
1971 * then check the DSAP.
1973 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1975 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1981 * This is an Ethernet type, so compare
1982 * the length/type field with it (if
1983 * the frame is an 802.2 frame, the length
1984 * field will be <= ETHERMTU, and, as
1985 * "proto" is > ETHERMTU, this test
1986 * will fail and the frame won't match,
1987 * which is what we want).
1989 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1996 * Generate code to match a particular packet type.
1998 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1999 * value, if <= ETHERMTU. We use that to determine whether to
2000 * match the type field or to check the type field for the special
2001 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2003 static struct block
*
2004 gen_linux_sll_linktype(proto
)
2007 struct block
*b0
, *b1
;
2013 case LLCSAP_NETBEUI
:
2015 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2016 * so we check the DSAP and SSAP.
2018 * LLCSAP_IP checks for IP-over-802.2, rather
2019 * than IP-over-Ethernet or IP-over-SNAP.
2021 * XXX - should we check both the DSAP and the
2022 * SSAP, like this, or should we check just the
2023 * DSAP, as we do for other types <= ETHERMTU
2024 * (i.e., other SAP values)?
2026 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2027 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2028 ((proto
<< 8) | proto
));
2034 * Ethernet_II frames, which are Ethernet
2035 * frames with a frame type of ETHERTYPE_IPX;
2037 * Ethernet_802.3 frames, which have a frame
2038 * type of LINUX_SLL_P_802_3;
2040 * Ethernet_802.2 frames, which are 802.3
2041 * frames with an 802.2 LLC header (i.e, have
2042 * a frame type of LINUX_SLL_P_802_2) and
2043 * with the IPX LSAP as the DSAP in the LLC
2046 * Ethernet_SNAP frames, which are 802.3
2047 * frames with an LLC header and a SNAP
2048 * header and with an OUI of 0x000000
2049 * (encapsulated Ethernet) and a protocol
2050 * ID of ETHERTYPE_IPX in the SNAP header.
2052 * First, do the checks on LINUX_SLL_P_802_2
2053 * frames; generate the check for either
2054 * Ethernet_802.2 or Ethernet_SNAP frames, and
2055 * then put a check for LINUX_SLL_P_802_2 frames
2058 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2059 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2061 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2065 * Now check for 802.3 frames and OR that with
2066 * the previous test.
2068 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2072 * Now add the check for Ethernet_II frames, and
2073 * do that before checking for the other frame
2076 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2077 (bpf_int32
)ETHERTYPE_IPX
);
2081 case ETHERTYPE_ATALK
:
2082 case ETHERTYPE_AARP
:
2084 * EtherTalk (AppleTalk protocols on Ethernet link
2085 * layer) may use 802.2 encapsulation.
2089 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2090 * we check for the 802.2 protocol type in the
2091 * "Ethernet type" field.
2093 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2096 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2097 * SNAP packets with an organization code of
2098 * 0x080007 (Apple, for Appletalk) and a protocol
2099 * type of ETHERTYPE_ATALK (Appletalk).
2101 * 802.2-encapsulated ETHERTYPE_AARP packets are
2102 * SNAP packets with an organization code of
2103 * 0x000000 (encapsulated Ethernet) and a protocol
2104 * type of ETHERTYPE_AARP (Appletalk ARP).
2106 if (proto
== ETHERTYPE_ATALK
)
2107 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2108 else /* proto == ETHERTYPE_AARP */
2109 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2113 * Check for Ethernet encapsulation (Ethertalk
2114 * phase 1?); we just check for the Ethernet
2117 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2123 if (proto
<= ETHERMTU
) {
2125 * This is an LLC SAP value, so the frames
2126 * that match would be 802.2 frames.
2127 * Check for the 802.2 protocol type
2128 * in the "Ethernet type" field, and
2129 * then check the DSAP.
2131 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2133 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2139 * This is an Ethernet type, so compare
2140 * the length/type field with it (if
2141 * the frame is an 802.2 frame, the length
2142 * field will be <= ETHERMTU, and, as
2143 * "proto" is > ETHERMTU, this test
2144 * will fail and the frame won't match,
2145 * which is what we want).
2147 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2153 static struct slist
*
2154 gen_load_prism_llprefixlen()
2156 struct slist
*s1
, *s2
;
2157 struct slist
*sjeq_avs_cookie
;
2158 struct slist
*sjcommon
;
2161 * This code is not compatible with the optimizer, as
2162 * we are generating jmp instructions within a normal
2163 * slist of instructions
2168 * Generate code to load the length of the radio header into
2169 * the register assigned to hold that length, if one has been
2170 * assigned. (If one hasn't been assigned, no code we've
2171 * generated uses that prefix, so we don't need to generate any
2174 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2175 * or always use the AVS header rather than the Prism header.
2176 * We load a 4-byte big-endian value at the beginning of the
2177 * raw packet data, and see whether, when masked with 0xFFFFF000,
2178 * it's equal to 0x80211000. If so, that indicates that it's
2179 * an AVS header (the masked-out bits are the version number).
2180 * Otherwise, it's a Prism header.
2182 * XXX - the Prism header is also, in theory, variable-length,
2183 * but no known software generates headers that aren't 144
2186 if (reg_off_ll
!= -1) {
2190 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2194 * AND it with 0xFFFFF000.
2196 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2197 s2
->s
.k
= 0xFFFFF000;
2201 * Compare with 0x80211000.
2203 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2204 sjeq_avs_cookie
->s
.k
= 0x80211000;
2205 sappend(s1
, sjeq_avs_cookie
);
2210 * The 4 bytes at an offset of 4 from the beginning of
2211 * the AVS header are the length of the AVS header.
2212 * That field is big-endian.
2214 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2217 sjeq_avs_cookie
->s
.jt
= s2
;
2220 * Now jump to the code to allocate a register
2221 * into which to save the header length and
2222 * store the length there. (The "jump always"
2223 * instruction needs to have the k field set;
2224 * it's added to the PC, so, as we're jumping
2225 * over a single instruction, it should be 1.)
2227 sjcommon
= new_stmt(JMP(BPF_JA
));
2229 sappend(s1
, sjcommon
);
2232 * Now for the code that handles the Prism header.
2233 * Just load the length of the Prism header (144)
2234 * into the A register. Have the test for an AVS
2235 * header branch here if we don't have an AVS header.
2237 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2240 sjeq_avs_cookie
->s
.jf
= s2
;
2243 * Now allocate a register to hold that value and store
2244 * it. The code for the AVS header will jump here after
2245 * loading the length of the AVS header.
2247 s2
= new_stmt(BPF_ST
);
2248 s2
->s
.k
= reg_off_ll
;
2250 sjcommon
->s
.jf
= s2
;
2253 * Now move it into the X register.
2255 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2263 static struct slist
*
2264 gen_load_avs_llprefixlen()
2266 struct slist
*s1
, *s2
;
2269 * Generate code to load the length of the AVS header into
2270 * the register assigned to hold that length, if one has been
2271 * assigned. (If one hasn't been assigned, no code we've
2272 * generated uses that prefix, so we don't need to generate any
2275 if (reg_off_ll
!= -1) {
2277 * The 4 bytes at an offset of 4 from the beginning of
2278 * the AVS header are the length of the AVS header.
2279 * That field is big-endian.
2281 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2285 * Now allocate a register to hold that value and store
2288 s2
= new_stmt(BPF_ST
);
2289 s2
->s
.k
= reg_off_ll
;
2293 * Now move it into the X register.
2295 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2303 static struct slist
*
2304 gen_load_radiotap_llprefixlen()
2306 struct slist
*s1
, *s2
;
2309 * Generate code to load the length of the radiotap header into
2310 * the register assigned to hold that length, if one has been
2311 * assigned. (If one hasn't been assigned, no code we've
2312 * generated uses that prefix, so we don't need to generate any
2315 if (reg_off_ll
!= -1) {
2317 * The 2 bytes at offsets of 2 and 3 from the beginning
2318 * of the radiotap header are the length of the radiotap
2319 * header; unfortunately, it's little-endian, so we have
2320 * to load it a byte at a time and construct the value.
2324 * Load the high-order byte, at an offset of 3, shift it
2325 * left a byte, and put the result in the X register.
2327 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2329 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2332 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2336 * Load the next byte, at an offset of 2, and OR the
2337 * value from the X register into it.
2339 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2342 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2346 * Now allocate a register to hold that value and store
2349 s2
= new_stmt(BPF_ST
);
2350 s2
->s
.k
= reg_off_ll
;
2354 * Now move it into the X register.
2356 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2365 * At the moment we treat PPI as normal Radiotap encoded
2366 * packets. The difference is in the function that generates
2367 * the code at the beginning to compute the header length.
2368 * Since this code generator of PPI supports bare 802.11
2369 * encapsulation only (i.e. the encapsulated DLT should be
2370 * DLT_IEEE802_11) we generate code to check for this too;
2371 * that's done in finish_parse().
2373 static struct slist
*
2374 gen_load_ppi_llprefixlen()
2376 struct slist
*s1
, *s2
;
2379 * Generate code to load the length of the radiotap header
2380 * into the register assigned to hold that length, if one has
2383 if (reg_off_ll
!= -1) {
2385 * The 2 bytes at offsets of 2 and 3 from the beginning
2386 * of the radiotap header are the length of the radiotap
2387 * header; unfortunately, it's little-endian, so we have
2388 * to load it a byte at a time and construct the value.
2392 * Load the high-order byte, at an offset of 3, shift it
2393 * left a byte, and put the result in the X register.
2395 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2397 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2400 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2404 * Load the next byte, at an offset of 2, and OR the
2405 * value from the X register into it.
2407 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2410 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2414 * Now allocate a register to hold that value and store
2417 s2
= new_stmt(BPF_ST
);
2418 s2
->s
.k
= reg_off_ll
;
2422 * Now move it into the X register.
2424 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2433 * Load a value relative to the beginning of the link-layer header after the 802.11
2434 * header, i.e. LLC_SNAP.
2435 * The link-layer header doesn't necessarily begin at the beginning
2436 * of the packet data; there might be a variable-length prefix containing
2437 * radio information.
2439 static struct slist
*
2440 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2443 struct slist
*sjset_data_frame_1
;
2444 struct slist
*sjset_data_frame_2
;
2445 struct slist
*sjset_qos
;
2446 struct slist
*sjset_radiotap_flags
;
2447 struct slist
*sjset_radiotap_tsft
;
2448 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2449 struct slist
*s_roundup
;
2451 if (reg_off_macpl
== -1) {
2453 * No register has been assigned to the offset of
2454 * the MAC-layer payload, which means nobody needs
2455 * it; don't bother computing it - just return
2456 * what we already have.
2462 * This code is not compatible with the optimizer, as
2463 * we are generating jmp instructions within a normal
2464 * slist of instructions
2469 * If "s" is non-null, it has code to arrange that the X register
2470 * contains the length of the prefix preceding the link-layer
2473 * Otherwise, the length of the prefix preceding the link-layer
2474 * header is "off_ll".
2478 * There is no variable-length header preceding the
2479 * link-layer header.
2481 * Load the length of the fixed-length prefix preceding
2482 * the link-layer header (if any) into the X register,
2483 * and store it in the reg_off_macpl register.
2484 * That length is off_ll.
2486 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2491 * The X register contains the offset of the beginning of the
2492 * link-layer header; add 24, which is the minimum length
2493 * of the MAC header for a data frame, to that, and store it
2494 * in reg_off_macpl, and then load the Frame Control field,
2495 * which is at the offset in the X register, with an indexed load.
2497 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2499 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2502 s2
= new_stmt(BPF_ST
);
2503 s2
->s
.k
= reg_off_macpl
;
2506 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2511 * Check the Frame Control field to see if this is a data frame;
2512 * a data frame has the 0x08 bit (b3) in that field set and the
2513 * 0x04 bit (b2) clear.
2515 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2516 sjset_data_frame_1
->s
.k
= 0x08;
2517 sappend(s
, sjset_data_frame_1
);
2520 * If b3 is set, test b2, otherwise go to the first statement of
2521 * the rest of the program.
2523 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2524 sjset_data_frame_2
->s
.k
= 0x04;
2525 sappend(s
, sjset_data_frame_2
);
2526 sjset_data_frame_1
->s
.jf
= snext
;
2529 * If b2 is not set, this is a data frame; test the QoS bit.
2530 * Otherwise, go to the first statement of the rest of the
2533 sjset_data_frame_2
->s
.jt
= snext
;
2534 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2535 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2536 sappend(s
, sjset_qos
);
2539 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2541 * Otherwise, go to the first statement of the rest of the
2544 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2545 s2
->s
.k
= reg_off_macpl
;
2547 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2550 s2
= new_stmt(BPF_ST
);
2551 s2
->s
.k
= reg_off_macpl
;
2555 * If we have a radiotap header, look at it to see whether
2556 * there's Atheros padding between the MAC-layer header
2559 * Note: all of the fields in the radiotap header are
2560 * little-endian, so we byte-swap all of the values
2561 * we test against, as they will be loaded as big-endian
2564 if (linktype
== DLT_IEEE802_11_RADIO
) {
2566 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2567 * in the presence flag?
2569 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2573 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2574 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2575 sappend(s
, sjset_radiotap_flags
);
2578 * If not, skip all of this.
2580 sjset_radiotap_flags
->s
.jf
= snext
;
2583 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2585 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2586 new_stmt(JMP(BPF_JSET
));
2587 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2588 sappend(s
, sjset_radiotap_tsft
);
2591 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2592 * at an offset of 16 from the beginning of the raw packet
2593 * data (8 bytes for the radiotap header and 8 bytes for
2596 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2599 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2603 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2604 sjset_tsft_datapad
->s
.k
= 0x20;
2605 sappend(s
, sjset_tsft_datapad
);
2608 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2609 * at an offset of 8 from the beginning of the raw packet
2610 * data (8 bytes for the radiotap header).
2612 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2615 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2619 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2620 sjset_notsft_datapad
->s
.k
= 0x20;
2621 sappend(s
, sjset_notsft_datapad
);
2624 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2625 * set, round the length of the 802.11 header to
2626 * a multiple of 4. Do that by adding 3 and then
2627 * dividing by and multiplying by 4, which we do by
2630 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2631 s_roundup
->s
.k
= reg_off_macpl
;
2632 sappend(s
, s_roundup
);
2633 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2636 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2639 s2
= new_stmt(BPF_ST
);
2640 s2
->s
.k
= reg_off_macpl
;
2643 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2644 sjset_tsft_datapad
->s
.jf
= snext
;
2645 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2646 sjset_notsft_datapad
->s
.jf
= snext
;
2648 sjset_qos
->s
.jf
= snext
;
2654 insert_compute_vloffsets(b
)
2660 * For link-layer types that have a variable-length header
2661 * preceding the link-layer header, generate code to load
2662 * the offset of the link-layer header into the register
2663 * assigned to that offset, if any.
2667 case DLT_PRISM_HEADER
:
2668 s
= gen_load_prism_llprefixlen();
2671 case DLT_IEEE802_11_RADIO_AVS
:
2672 s
= gen_load_avs_llprefixlen();
2675 case DLT_IEEE802_11_RADIO
:
2676 s
= gen_load_radiotap_llprefixlen();
2680 s
= gen_load_ppi_llprefixlen();
2689 * For link-layer types that have a variable-length link-layer
2690 * header, generate code to load the offset of the MAC-layer
2691 * payload into the register assigned to that offset, if any.
2695 case DLT_IEEE802_11
:
2696 case DLT_PRISM_HEADER
:
2697 case DLT_IEEE802_11_RADIO_AVS
:
2698 case DLT_IEEE802_11_RADIO
:
2700 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2705 * If we have any offset-loading code, append all the
2706 * existing statements in the block to those statements,
2707 * and make the resulting list the list of statements
2711 sappend(s
, b
->stmts
);
2716 static struct block
*
2717 gen_ppi_dlt_check(void)
2719 struct slist
*s_load_dlt
;
2722 if (linktype
== DLT_PPI
)
2724 /* Create the statements that check for the DLT
2726 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2727 s_load_dlt
->s
.k
= 4;
2729 b
= new_block(JMP(BPF_JEQ
));
2731 b
->stmts
= s_load_dlt
;
2732 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2742 static struct slist
*
2743 gen_prism_llprefixlen(void)
2747 if (reg_off_ll
== -1) {
2749 * We haven't yet assigned a register for the length
2750 * of the radio header; allocate one.
2752 reg_off_ll
= alloc_reg();
2756 * Load the register containing the radio length
2757 * into the X register.
2759 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2760 s
->s
.k
= reg_off_ll
;
2764 static struct slist
*
2765 gen_avs_llprefixlen(void)
2769 if (reg_off_ll
== -1) {
2771 * We haven't yet assigned a register for the length
2772 * of the AVS header; allocate one.
2774 reg_off_ll
= alloc_reg();
2778 * Load the register containing the AVS length
2779 * into the X register.
2781 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2782 s
->s
.k
= reg_off_ll
;
2786 static struct slist
*
2787 gen_radiotap_llprefixlen(void)
2791 if (reg_off_ll
== -1) {
2793 * We haven't yet assigned a register for the length
2794 * of the radiotap header; allocate one.
2796 reg_off_ll
= alloc_reg();
2800 * Load the register containing the radiotap length
2801 * into the X register.
2803 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2804 s
->s
.k
= reg_off_ll
;
2809 * At the moment we treat PPI as normal Radiotap encoded
2810 * packets. The difference is in the function that generates
2811 * the code at the beginning to compute the header length.
2812 * Since this code generator of PPI supports bare 802.11
2813 * encapsulation only (i.e. the encapsulated DLT should be
2814 * DLT_IEEE802_11) we generate code to check for this too.
2816 static struct slist
*
2817 gen_ppi_llprefixlen(void)
2821 if (reg_off_ll
== -1) {
2823 * We haven't yet assigned a register for the length
2824 * of the radiotap header; allocate one.
2826 reg_off_ll
= alloc_reg();
2830 * Load the register containing the PPI length
2831 * into the X register.
2833 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2834 s
->s
.k
= reg_off_ll
;
2839 * Generate code to compute the link-layer header length, if necessary,
2840 * putting it into the X register, and to return either a pointer to a
2841 * "struct slist" for the list of statements in that code, or NULL if
2842 * no code is necessary.
2844 static struct slist
*
2845 gen_llprefixlen(void)
2849 case DLT_PRISM_HEADER
:
2850 return gen_prism_llprefixlen();
2852 case DLT_IEEE802_11_RADIO_AVS
:
2853 return gen_avs_llprefixlen();
2855 case DLT_IEEE802_11_RADIO
:
2856 return gen_radiotap_llprefixlen();
2859 return gen_ppi_llprefixlen();
2867 * Generate code to load the register containing the offset of the
2868 * MAC-layer payload into the X register; if no register for that offset
2869 * has been allocated, allocate it first.
2871 static struct slist
*
2876 if (off_macpl_is_variable
) {
2877 if (reg_off_macpl
== -1) {
2879 * We haven't yet assigned a register for the offset
2880 * of the MAC-layer payload; allocate one.
2882 reg_off_macpl
= alloc_reg();
2886 * Load the register containing the offset of the MAC-layer
2887 * payload into the X register.
2889 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2890 s
->s
.k
= reg_off_macpl
;
2894 * That offset isn't variable, so we don't need to
2895 * generate any code.
2902 * Map an Ethernet type to the equivalent PPP type.
2905 ethertype_to_ppptype(proto
)
2915 case ETHERTYPE_IPV6
:
2924 case ETHERTYPE_ATALK
:
2938 * I'm assuming the "Bridging PDU"s that go
2939 * over PPP are Spanning Tree Protocol
2953 * Generate code to match a particular packet type by matching the
2954 * link-layer type field or fields in the 802.2 LLC header.
2956 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2957 * value, if <= ETHERMTU.
2959 static struct block
*
2963 struct block
*b0
, *b1
, *b2
;
2965 /* are we checking MPLS-encapsulated packets? */
2966 if (label_stack_depth
> 0) {
2970 /* FIXME add other L3 proto IDs */
2971 return gen_mpls_linktype(Q_IP
);
2973 case ETHERTYPE_IPV6
:
2975 /* FIXME add other L3 proto IDs */
2976 return gen_mpls_linktype(Q_IPV6
);
2979 bpf_error("unsupported protocol over mpls");
2985 * Are we testing PPPoE packets?
2989 * The PPPoE session header is part of the
2990 * MAC-layer payload, so all references
2991 * should be relative to the beginning of
2996 * We use Ethernet protocol types inside libpcap;
2997 * map them to the corresponding PPP protocol types.
2999 proto
= ethertype_to_ppptype(proto
);
3000 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3006 return gen_ether_linktype(proto
);
3014 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3018 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3025 case DLT_IEEE802_11
:
3026 case DLT_PRISM_HEADER
:
3027 case DLT_IEEE802_11_RADIO_AVS
:
3028 case DLT_IEEE802_11_RADIO
:
3031 * Check that we have a data frame.
3033 b0
= gen_check_802_11_data_frame();
3036 * Now check for the specified link-layer type.
3038 b1
= gen_llc_linktype(proto
);
3046 * XXX - check for asynchronous frames, as per RFC 1103.
3048 return gen_llc_linktype(proto
);
3054 * XXX - check for LLC PDUs, as per IEEE 802.5.
3056 return gen_llc_linktype(proto
);
3060 case DLT_ATM_RFC1483
:
3062 case DLT_IP_OVER_FC
:
3063 return gen_llc_linktype(proto
);
3069 * If "is_lane" is set, check for a LANE-encapsulated
3070 * version of this protocol, otherwise check for an
3071 * LLC-encapsulated version of this protocol.
3073 * We assume LANE means Ethernet, not Token Ring.
3077 * Check that the packet doesn't begin with an
3078 * LE Control marker. (We've already generated
3081 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3086 * Now generate an Ethernet test.
3088 b1
= gen_ether_linktype(proto
);
3093 * Check for LLC encapsulation and then check the
3096 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3097 b1
= gen_llc_linktype(proto
);
3105 return gen_linux_sll_linktype(proto
);
3110 case DLT_SLIP_BSDOS
:
3113 * These types don't provide any type field; packets
3114 * are always IPv4 or IPv6.
3116 * XXX - for IPv4, check for a version number of 4, and,
3117 * for IPv6, check for a version number of 6?
3122 /* Check for a version number of 4. */
3123 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3125 case ETHERTYPE_IPV6
:
3126 /* Check for a version number of 6. */
3127 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3131 return gen_false(); /* always false */
3138 case DLT_PPP_SERIAL
:
3141 * We use Ethernet protocol types inside libpcap;
3142 * map them to the corresponding PPP protocol types.
3144 proto
= ethertype_to_ppptype(proto
);
3145 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3151 * We use Ethernet protocol types inside libpcap;
3152 * map them to the corresponding PPP protocol types.
3158 * Also check for Van Jacobson-compressed IP.
3159 * XXX - do this for other forms of PPP?
3161 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3162 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3164 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3169 proto
= ethertype_to_ppptype(proto
);
3170 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3180 * For DLT_NULL, the link-layer header is a 32-bit
3181 * word containing an AF_ value in *host* byte order,
3182 * and for DLT_ENC, the link-layer header begins
3183 * with a 32-bit work containing an AF_ value in
3186 * In addition, if we're reading a saved capture file,
3187 * the host byte order in the capture may not be the
3188 * same as the host byte order on this machine.
3190 * For DLT_LOOP, the link-layer header is a 32-bit
3191 * word containing an AF_ value in *network* byte order.
3193 * XXX - AF_ values may, unfortunately, be platform-
3194 * dependent; for example, FreeBSD's AF_INET6 is 24
3195 * whilst NetBSD's and OpenBSD's is 26.
3197 * This means that, when reading a capture file, just
3198 * checking for our AF_INET6 value won't work if the
3199 * capture file came from another OS.
3208 case ETHERTYPE_IPV6
:
3215 * Not a type on which we support filtering.
3216 * XXX - support those that have AF_ values
3217 * #defined on this platform, at least?
3222 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3224 * The AF_ value is in host byte order, but
3225 * the BPF interpreter will convert it to
3226 * network byte order.
3228 * If this is a save file, and it's from a
3229 * machine with the opposite byte order to
3230 * ours, we byte-swap the AF_ value.
3232 * Then we run it through "htonl()", and
3233 * generate code to compare against the result.
3235 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3236 bpf_pcap
->sf
.swapped
)
3237 proto
= SWAPLONG(proto
);
3238 proto
= htonl(proto
);
3240 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3242 #ifdef HAVE_NET_PFVAR_H
3245 * af field is host byte order in contrast to the rest of
3248 if (proto
== ETHERTYPE_IP
)
3249 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3250 BPF_B
, (bpf_int32
)AF_INET
));
3252 else if (proto
== ETHERTYPE_IPV6
)
3253 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3254 BPF_B
, (bpf_int32
)AF_INET6
));
3260 #endif /* HAVE_NET_PFVAR_H */
3263 case DLT_ARCNET_LINUX
:
3265 * XXX should we check for first fragment if the protocol
3274 case ETHERTYPE_IPV6
:
3275 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3276 (bpf_int32
)ARCTYPE_INET6
));
3280 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3281 (bpf_int32
)ARCTYPE_IP
);
3282 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3283 (bpf_int32
)ARCTYPE_IP_OLD
);
3288 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3289 (bpf_int32
)ARCTYPE_ARP
);
3290 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3291 (bpf_int32
)ARCTYPE_ARP_OLD
);
3295 case ETHERTYPE_REVARP
:
3296 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3297 (bpf_int32
)ARCTYPE_REVARP
));
3299 case ETHERTYPE_ATALK
:
3300 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3301 (bpf_int32
)ARCTYPE_ATALK
));
3308 case ETHERTYPE_ATALK
:
3318 * XXX - assumes a 2-byte Frame Relay header with
3319 * DLCI and flags. What if the address is longer?
3325 * Check for the special NLPID for IP.
3327 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3330 case ETHERTYPE_IPV6
:
3332 * Check for the special NLPID for IPv6.
3334 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3339 * Check for several OSI protocols.
3341 * Frame Relay packets typically have an OSI
3342 * NLPID at the beginning; we check for each
3345 * What we check for is the NLPID and a frame
3346 * control field of UI, i.e. 0x03 followed
3349 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3350 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3351 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3363 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3365 case DLT_JUNIPER_MFR
:
3366 case DLT_JUNIPER_MLFR
:
3367 case DLT_JUNIPER_MLPPP
:
3368 case DLT_JUNIPER_ATM1
:
3369 case DLT_JUNIPER_ATM2
:
3370 case DLT_JUNIPER_PPPOE
:
3371 case DLT_JUNIPER_PPPOE_ATM
:
3372 case DLT_JUNIPER_GGSN
:
3373 case DLT_JUNIPER_ES
:
3374 case DLT_JUNIPER_MONITOR
:
3375 case DLT_JUNIPER_SERVICES
:
3376 case DLT_JUNIPER_ETHER
:
3377 case DLT_JUNIPER_PPP
:
3378 case DLT_JUNIPER_FRELAY
:
3379 case DLT_JUNIPER_CHDLC
:
3380 case DLT_JUNIPER_VP
:
3381 case DLT_JUNIPER_ST
:
3382 case DLT_JUNIPER_ISM
:
3383 /* just lets verify the magic number for now -
3384 * on ATM we may have up to 6 different encapsulations on the wire
3385 * and need a lot of heuristics to figure out that the payload
3388 * FIXME encapsulation specific BPF_ filters
3390 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3392 case DLT_LINUX_IRDA
:
3393 bpf_error("IrDA link-layer type filtering not implemented");
3396 bpf_error("DOCSIS link-layer type filtering not implemented");
3399 case DLT_MTP2_WITH_PHDR
:
3400 bpf_error("MTP2 link-layer type filtering not implemented");
3403 bpf_error("ERF link-layer type filtering not implemented");
3407 bpf_error("PFSYNC link-layer type filtering not implemented");
3410 case DLT_LINUX_LAPD
:
3411 bpf_error("LAPD link-layer type filtering not implemented");
3415 case DLT_USB_LINUX_MMAPPED
:
3416 bpf_error("USB link-layer type filtering not implemented");
3418 case DLT_BLUETOOTH_HCI_H4
:
3419 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3420 bpf_error("Bluetooth link-layer type filtering not implemented");
3423 bpf_error("CAN20B link-layer type filtering not implemented");
3425 case DLT_IEEE802_15_4
:
3426 case DLT_IEEE802_15_4_LINUX
:
3427 case DLT_IEEE802_15_4_NONASK_PHY
:
3428 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3430 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3431 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3434 bpf_error("SITA link-layer type filtering not implemented");
3437 bpf_error("RAIF1 link-layer type filtering not implemented");
3440 bpf_error("IPMB link-layer type filtering not implemented");
3443 bpf_error("AX.25 link-layer type filtering not implemented");
3447 * All the types that have no encapsulation should either be
3448 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3449 * all packets are IP packets, or should be handled in some
3450 * special case, if none of them are (if some are and some
3451 * aren't, the lack of encapsulation is a problem, as we'd
3452 * have to find some other way of determining the packet type).
3454 * Therefore, if "off_linktype" is -1, there's an error.
3456 if (off_linktype
== (u_int
)-1)
3460 * Any type not handled above should always have an Ethernet
3461 * type at an offset of "off_linktype".
3463 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3467 * Check for an LLC SNAP packet with a given organization code and
3468 * protocol type; we check the entire contents of the 802.2 LLC and
3469 * snap headers, checking for DSAP and SSAP of SNAP and a control
3470 * field of 0x03 in the LLC header, and for the specified organization
3471 * code and protocol type in the SNAP header.
3473 static struct block
*
3474 gen_snap(orgcode
, ptype
)
3475 bpf_u_int32 orgcode
;
3478 u_char snapblock
[8];
3480 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3481 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3482 snapblock
[2] = 0x03; /* control = UI */
3483 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3484 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3485 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3486 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3487 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3488 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3492 * Generate code to match a particular packet type, for link-layer types
3493 * using 802.2 LLC headers.
3495 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3496 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3498 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3499 * value, if <= ETHERMTU. We use that to determine whether to
3500 * match the DSAP or both DSAP and LSAP or to check the OUI and
3501 * protocol ID in a SNAP header.
3503 static struct block
*
3504 gen_llc_linktype(proto
)
3508 * XXX - handle token-ring variable-length header.
3514 case LLCSAP_NETBEUI
:
3516 * XXX - should we check both the DSAP and the
3517 * SSAP, like this, or should we check just the
3518 * DSAP, as we do for other types <= ETHERMTU
3519 * (i.e., other SAP values)?
3521 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3522 ((proto
<< 8) | proto
));
3526 * XXX - are there ever SNAP frames for IPX on
3527 * non-Ethernet 802.x networks?
3529 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3530 (bpf_int32
)LLCSAP_IPX
);
3532 case ETHERTYPE_ATALK
:
3534 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3535 * SNAP packets with an organization code of
3536 * 0x080007 (Apple, for Appletalk) and a protocol
3537 * type of ETHERTYPE_ATALK (Appletalk).
3539 * XXX - check for an organization code of
3540 * encapsulated Ethernet as well?
3542 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3546 * XXX - we don't have to check for IPX 802.3
3547 * here, but should we check for the IPX Ethertype?
3549 if (proto
<= ETHERMTU
) {
3551 * This is an LLC SAP value, so check
3554 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3557 * This is an Ethernet type; we assume that it's
3558 * unlikely that it'll appear in the right place
3559 * at random, and therefore check only the
3560 * location that would hold the Ethernet type
3561 * in a SNAP frame with an organization code of
3562 * 0x000000 (encapsulated Ethernet).
3564 * XXX - if we were to check for the SNAP DSAP and
3565 * LSAP, as per XXX, and were also to check for an
3566 * organization code of 0x000000 (encapsulated
3567 * Ethernet), we'd do
3569 * return gen_snap(0x000000, proto);
3571 * here; for now, we don't, as per the above.
3572 * I don't know whether it's worth the extra CPU
3573 * time to do the right check or not.
3575 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3580 static struct block
*
3581 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3585 u_int src_off
, dst_off
;
3587 struct block
*b0
, *b1
;
3601 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3602 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3608 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3609 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3616 b0
= gen_linktype(proto
);
3617 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3623 static struct block
*
3624 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3625 struct in6_addr
*addr
;
3626 struct in6_addr
*mask
;
3628 u_int src_off
, dst_off
;
3630 struct block
*b0
, *b1
;
3645 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3646 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3652 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3653 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3660 /* this order is important */
3661 a
= (u_int32_t
*)addr
;
3662 m
= (u_int32_t
*)mask
;
3663 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3664 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3666 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3668 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3670 b0
= gen_linktype(proto
);
3676 static struct block
*
3677 gen_ehostop(eaddr
, dir
)
3678 register const u_char
*eaddr
;
3681 register struct block
*b0
, *b1
;
3685 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3688 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3691 b0
= gen_ehostop(eaddr
, Q_SRC
);
3692 b1
= gen_ehostop(eaddr
, Q_DST
);
3698 b0
= gen_ehostop(eaddr
, Q_SRC
);
3699 b1
= gen_ehostop(eaddr
, Q_DST
);
3708 * Like gen_ehostop, but for DLT_FDDI
3710 static struct block
*
3711 gen_fhostop(eaddr
, dir
)
3712 register const u_char
*eaddr
;
3715 struct block
*b0
, *b1
;
3720 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3722 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3727 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3729 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3733 b0
= gen_fhostop(eaddr
, Q_SRC
);
3734 b1
= gen_fhostop(eaddr
, Q_DST
);
3740 b0
= gen_fhostop(eaddr
, Q_SRC
);
3741 b1
= gen_fhostop(eaddr
, Q_DST
);
3750 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3752 static struct block
*
3753 gen_thostop(eaddr
, dir
)
3754 register const u_char
*eaddr
;
3757 register struct block
*b0
, *b1
;
3761 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3764 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3767 b0
= gen_thostop(eaddr
, Q_SRC
);
3768 b1
= gen_thostop(eaddr
, Q_DST
);
3774 b0
= gen_thostop(eaddr
, Q_SRC
);
3775 b1
= gen_thostop(eaddr
, Q_DST
);
3784 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3785 * various 802.11 + radio headers.
3787 static struct block
*
3788 gen_wlanhostop(eaddr
, dir
)
3789 register const u_char
*eaddr
;
3792 register struct block
*b0
, *b1
, *b2
;
3793 register struct slist
*s
;
3795 #ifdef ENABLE_WLAN_FILTERING_PATCH
3798 * We need to disable the optimizer because the optimizer is buggy
3799 * and wipes out some LD instructions generated by the below
3800 * code to validate the Frame Control bits
3803 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3810 * For control frames, there is no SA.
3812 * For management frames, SA is at an
3813 * offset of 10 from the beginning of
3816 * For data frames, SA is at an offset
3817 * of 10 from the beginning of the packet
3818 * if From DS is clear, at an offset of
3819 * 16 from the beginning of the packet
3820 * if From DS is set and To DS is clear,
3821 * and an offset of 24 from the beginning
3822 * of the packet if From DS is set and To DS
3827 * Generate the tests to be done for data frames
3830 * First, check for To DS set, i.e. check "link[1] & 0x01".
3832 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3833 b1
= new_block(JMP(BPF_JSET
));
3834 b1
->s
.k
= 0x01; /* To DS */
3838 * If To DS is set, the SA is at 24.
3840 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3844 * Now, check for To DS not set, i.e. check
3845 * "!(link[1] & 0x01)".
3847 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3848 b2
= new_block(JMP(BPF_JSET
));
3849 b2
->s
.k
= 0x01; /* To DS */
3854 * If To DS is not set, the SA is at 16.
3856 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3860 * Now OR together the last two checks. That gives
3861 * the complete set of checks for data frames with
3867 * Now check for From DS being set, and AND that with
3868 * the ORed-together checks.
3870 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3871 b1
= new_block(JMP(BPF_JSET
));
3872 b1
->s
.k
= 0x02; /* From DS */
3877 * Now check for data frames with From DS not set.
3879 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3880 b2
= new_block(JMP(BPF_JSET
));
3881 b2
->s
.k
= 0x02; /* From DS */
3886 * If From DS isn't set, the SA is at 10.
3888 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3892 * Now OR together the checks for data frames with
3893 * From DS not set and for data frames with From DS
3894 * set; that gives the checks done for data frames.
3899 * Now check for a data frame.
3900 * I.e, check "link[0] & 0x08".
3902 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3903 b1
= new_block(JMP(BPF_JSET
));
3908 * AND that with the checks done for data frames.
3913 * If the high-order bit of the type value is 0, this
3914 * is a management frame.
3915 * I.e, check "!(link[0] & 0x08)".
3917 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3918 b2
= new_block(JMP(BPF_JSET
));
3924 * For management frames, the SA is at 10.
3926 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3930 * OR that with the checks done for data frames.
3931 * That gives the checks done for management and
3937 * If the low-order bit of the type value is 1,
3938 * this is either a control frame or a frame
3939 * with a reserved type, and thus not a
3942 * I.e., check "!(link[0] & 0x04)".
3944 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3945 b1
= new_block(JMP(BPF_JSET
));
3951 * AND that with the checks for data and management
3961 * For control frames, there is no DA.
3963 * For management frames, DA is at an
3964 * offset of 4 from the beginning of
3967 * For data frames, DA is at an offset
3968 * of 4 from the beginning of the packet
3969 * if To DS is clear and at an offset of
3970 * 16 from the beginning of the packet
3975 * Generate the tests to be done for data frames.
3977 * First, check for To DS set, i.e. "link[1] & 0x01".
3979 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3980 b1
= new_block(JMP(BPF_JSET
));
3981 b1
->s
.k
= 0x01; /* To DS */
3985 * If To DS is set, the DA is at 16.
3987 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3991 * Now, check for To DS not set, i.e. check
3992 * "!(link[1] & 0x01)".
3994 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3995 b2
= new_block(JMP(BPF_JSET
));
3996 b2
->s
.k
= 0x01; /* To DS */
4001 * If To DS is not set, the DA is at 4.
4003 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4007 * Now OR together the last two checks. That gives
4008 * the complete set of checks for data frames.
4013 * Now check for a data frame.
4014 * I.e, check "link[0] & 0x08".
4016 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4017 b1
= new_block(JMP(BPF_JSET
));
4022 * AND that with the checks done for data frames.
4027 * If the high-order bit of the type value is 0, this
4028 * is a management frame.
4029 * I.e, check "!(link[0] & 0x08)".
4031 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4032 b2
= new_block(JMP(BPF_JSET
));
4038 * For management frames, the DA is at 4.
4040 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4044 * OR that with the checks done for data frames.
4045 * That gives the checks done for management and
4051 * If the low-order bit of the type value is 1,
4052 * this is either a control frame or a frame
4053 * with a reserved type, and thus not a
4056 * I.e., check "!(link[0] & 0x04)".
4058 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4059 b1
= new_block(JMP(BPF_JSET
));
4065 * AND that with the checks for data and management
4072 * XXX - add RA, TA, and BSSID keywords?
4075 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4079 * Not present in CTS or ACK control frames.
4081 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4082 IEEE80211_FC0_TYPE_MASK
);
4084 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4085 IEEE80211_FC0_SUBTYPE_MASK
);
4087 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4088 IEEE80211_FC0_SUBTYPE_MASK
);
4092 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4098 * Not present in control frames.
4100 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4101 IEEE80211_FC0_TYPE_MASK
);
4103 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4109 * Present only if the direction mask has both "From DS"
4110 * and "To DS" set. Neither control frames nor management
4111 * frames should have both of those set, so we don't
4112 * check the frame type.
4114 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4115 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4116 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4121 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4122 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4128 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4129 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4138 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4139 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4140 * as the RFC states.)
4142 static struct block
*
4143 gen_ipfchostop(eaddr
, dir
)
4144 register const u_char
*eaddr
;
4147 register struct block
*b0
, *b1
;
4151 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4154 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4157 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4158 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4164 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4165 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4174 * This is quite tricky because there may be pad bytes in front of the
4175 * DECNET header, and then there are two possible data packet formats that
4176 * carry both src and dst addresses, plus 5 packet types in a format that
4177 * carries only the src node, plus 2 types that use a different format and
4178 * also carry just the src node.
4182 * Instead of doing those all right, we just look for data packets with
4183 * 0 or 1 bytes of padding. If you want to look at other packets, that
4184 * will require a lot more hacking.
4186 * To add support for filtering on DECNET "areas" (network numbers)
4187 * one would want to add a "mask" argument to this routine. That would
4188 * make the filter even more inefficient, although one could be clever
4189 * and not generate masking instructions if the mask is 0xFFFF.
4191 static struct block
*
4192 gen_dnhostop(addr
, dir
)
4196 struct block
*b0
, *b1
, *b2
, *tmp
;
4197 u_int offset_lh
; /* offset if long header is received */
4198 u_int offset_sh
; /* offset if short header is received */
4203 offset_sh
= 1; /* follows flags */
4204 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4208 offset_sh
= 3; /* follows flags, dstnode */
4209 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4213 /* Inefficient because we do our Calvinball dance twice */
4214 b0
= gen_dnhostop(addr
, Q_SRC
);
4215 b1
= gen_dnhostop(addr
, Q_DST
);
4221 /* Inefficient because we do our Calvinball dance twice */
4222 b0
= gen_dnhostop(addr
, Q_SRC
);
4223 b1
= gen_dnhostop(addr
, Q_DST
);
4228 bpf_error("ISO host filtering not implemented");
4233 b0
= gen_linktype(ETHERTYPE_DN
);
4234 /* Check for pad = 1, long header case */
4235 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4236 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4237 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4238 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4240 /* Check for pad = 0, long header case */
4241 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4242 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4245 /* Check for pad = 1, short header case */
4246 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4247 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4248 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4251 /* Check for pad = 0, short header case */
4252 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4253 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4257 /* Combine with test for linktype */
4263 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4264 * test the bottom-of-stack bit, and then check the version number
4265 * field in the IP header.
4267 static struct block
*
4268 gen_mpls_linktype(proto
)
4271 struct block
*b0
, *b1
;
4276 /* match the bottom-of-stack bit */
4277 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4278 /* match the IPv4 version number */
4279 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4284 /* match the bottom-of-stack bit */
4285 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4286 /* match the IPv4 version number */
4287 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4296 static struct block
*
4297 gen_host(addr
, mask
, proto
, dir
, type
)
4304 struct block
*b0
, *b1
;
4305 const char *typestr
;
4315 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4317 * Only check for non-IPv4 addresses if we're not
4318 * checking MPLS-encapsulated packets.
4320 if (label_stack_depth
== 0) {
4321 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4323 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4329 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4332 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4335 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4338 bpf_error("'tcp' modifier applied to %s", typestr
);
4341 bpf_error("'sctp' modifier applied to %s", typestr
);
4344 bpf_error("'udp' modifier applied to %s", typestr
);
4347 bpf_error("'icmp' modifier applied to %s", typestr
);
4350 bpf_error("'igmp' modifier applied to %s", typestr
);
4353 bpf_error("'igrp' modifier applied to %s", typestr
);
4356 bpf_error("'pim' modifier applied to %s", typestr
);
4359 bpf_error("'vrrp' modifier applied to %s", typestr
);
4362 bpf_error("ATALK host filtering not implemented");
4365 bpf_error("AARP host filtering not implemented");
4368 return gen_dnhostop(addr
, dir
);
4371 bpf_error("SCA host filtering not implemented");
4374 bpf_error("LAT host filtering not implemented");
4377 bpf_error("MOPDL host filtering not implemented");
4380 bpf_error("MOPRC host filtering not implemented");
4384 bpf_error("'ip6' modifier applied to ip host");
4387 bpf_error("'icmp6' modifier applied to %s", typestr
);
4391 bpf_error("'ah' modifier applied to %s", typestr
);
4394 bpf_error("'esp' modifier applied to %s", typestr
);
4397 bpf_error("ISO host filtering not implemented");
4400 bpf_error("'esis' modifier applied to %s", typestr
);
4403 bpf_error("'isis' modifier applied to %s", typestr
);
4406 bpf_error("'clnp' modifier applied to %s", typestr
);
4409 bpf_error("'stp' modifier applied to %s", typestr
);
4412 bpf_error("IPX host filtering not implemented");
4415 bpf_error("'netbeui' modifier applied to %s", typestr
);
4418 bpf_error("'radio' modifier applied to %s", typestr
);
4427 static struct block
*
4428 gen_host6(addr
, mask
, proto
, dir
, type
)
4429 struct in6_addr
*addr
;
4430 struct in6_addr
*mask
;
4435 const char *typestr
;
4445 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4448 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4451 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4454 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4457 bpf_error("'sctp' modifier applied to %s", typestr
);
4460 bpf_error("'tcp' modifier applied to %s", typestr
);
4463 bpf_error("'udp' modifier applied to %s", typestr
);
4466 bpf_error("'icmp' modifier applied to %s", typestr
);
4469 bpf_error("'igmp' modifier applied to %s", typestr
);
4472 bpf_error("'igrp' modifier applied to %s", typestr
);
4475 bpf_error("'pim' modifier applied to %s", typestr
);
4478 bpf_error("'vrrp' modifier applied to %s", typestr
);
4481 bpf_error("ATALK host filtering not implemented");
4484 bpf_error("AARP host filtering not implemented");
4487 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4490 bpf_error("SCA host filtering not implemented");
4493 bpf_error("LAT host filtering not implemented");
4496 bpf_error("MOPDL host filtering not implemented");
4499 bpf_error("MOPRC host filtering not implemented");
4502 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4505 bpf_error("'icmp6' modifier applied to %s", typestr
);
4508 bpf_error("'ah' modifier applied to %s", typestr
);
4511 bpf_error("'esp' modifier applied to %s", typestr
);
4514 bpf_error("ISO host filtering not implemented");
4517 bpf_error("'esis' modifier applied to %s", typestr
);
4520 bpf_error("'isis' modifier applied to %s", typestr
);
4523 bpf_error("'clnp' modifier applied to %s", typestr
);
4526 bpf_error("'stp' modifier applied to %s", typestr
);
4529 bpf_error("IPX host filtering not implemented");
4532 bpf_error("'netbeui' modifier applied to %s", typestr
);
4535 bpf_error("'radio' modifier applied to %s", typestr
);
4545 static struct block
*
4546 gen_gateway(eaddr
, alist
, proto
, dir
)
4547 const u_char
*eaddr
;
4548 bpf_u_int32
**alist
;
4552 struct block
*b0
, *b1
, *tmp
;
4555 bpf_error("direction applied to 'gateway'");
4564 b0
= gen_ehostop(eaddr
, Q_OR
);
4567 b0
= gen_fhostop(eaddr
, Q_OR
);
4570 b0
= gen_thostop(eaddr
, Q_OR
);
4572 case DLT_IEEE802_11
:
4573 case DLT_PRISM_HEADER
:
4574 case DLT_IEEE802_11_RADIO_AVS
:
4575 case DLT_IEEE802_11_RADIO
:
4577 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4582 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4584 * Check that the packet doesn't begin with an
4585 * LE Control marker. (We've already generated
4588 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4593 * Now check the MAC address.
4595 b0
= gen_ehostop(eaddr
, Q_OR
);
4598 case DLT_IP_OVER_FC
:
4599 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4603 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4605 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4607 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4616 bpf_error("illegal modifier of 'gateway'");
4622 gen_proto_abbrev(proto
)
4631 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4633 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4639 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4641 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4647 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4649 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4655 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4658 #ifndef IPPROTO_IGMP
4659 #define IPPROTO_IGMP 2
4663 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4666 #ifndef IPPROTO_IGRP
4667 #define IPPROTO_IGRP 9
4670 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4674 #define IPPROTO_PIM 103
4678 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4680 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4685 #ifndef IPPROTO_VRRP
4686 #define IPPROTO_VRRP 112
4690 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4694 b1
= gen_linktype(ETHERTYPE_IP
);
4698 b1
= gen_linktype(ETHERTYPE_ARP
);
4702 b1
= gen_linktype(ETHERTYPE_REVARP
);
4706 bpf_error("link layer applied in wrong context");
4709 b1
= gen_linktype(ETHERTYPE_ATALK
);
4713 b1
= gen_linktype(ETHERTYPE_AARP
);
4717 b1
= gen_linktype(ETHERTYPE_DN
);
4721 b1
= gen_linktype(ETHERTYPE_SCA
);
4725 b1
= gen_linktype(ETHERTYPE_LAT
);
4729 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4733 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4738 b1
= gen_linktype(ETHERTYPE_IPV6
);
4741 #ifndef IPPROTO_ICMPV6
4742 #define IPPROTO_ICMPV6 58
4745 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4750 #define IPPROTO_AH 51
4753 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4755 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4761 #define IPPROTO_ESP 50
4764 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4766 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4772 b1
= gen_linktype(LLCSAP_ISONS
);
4776 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4780 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4783 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4784 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4785 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4787 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4789 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4791 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4795 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4796 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4797 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4799 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4801 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4803 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4807 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4808 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4809 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4811 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4816 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4817 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4822 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4823 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4825 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4827 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4832 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4833 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4838 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4839 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4844 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4848 b1
= gen_linktype(LLCSAP_8021D
);
4852 b1
= gen_linktype(LLCSAP_IPX
);
4856 b1
= gen_linktype(LLCSAP_NETBEUI
);
4860 bpf_error("'radio' is not a valid protocol type");
4868 static struct block
*
4875 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4876 b
= new_block(JMP(BPF_JSET
));
4885 * Generate a comparison to a port value in the transport-layer header
4886 * at the specified offset from the beginning of that header.
4888 * XXX - this handles a variable-length prefix preceding the link-layer
4889 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4890 * variable-length link-layer headers (such as Token Ring or 802.11
4893 static struct block
*
4894 gen_portatom(off
, v
)
4898 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4902 static struct block
*
4903 gen_portatom6(off
, v
)
4907 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4912 gen_portop(port
, proto
, dir
)
4913 int port
, proto
, dir
;
4915 struct block
*b0
, *b1
, *tmp
;
4917 /* ip proto 'proto' */
4918 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4924 b1
= gen_portatom(0, (bpf_int32
)port
);
4928 b1
= gen_portatom(2, (bpf_int32
)port
);
4933 tmp
= gen_portatom(0, (bpf_int32
)port
);
4934 b1
= gen_portatom(2, (bpf_int32
)port
);
4939 tmp
= gen_portatom(0, (bpf_int32
)port
);
4940 b1
= gen_portatom(2, (bpf_int32
)port
);
4952 static struct block
*
4953 gen_port(port
, ip_proto
, dir
)
4958 struct block
*b0
, *b1
, *tmp
;
4963 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4964 * not LLC encapsulation with LLCSAP_IP.
4966 * For IEEE 802 networks - which includes 802.5 token ring
4967 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4968 * says that SNAP encapsulation is used, not LLC encapsulation
4971 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4972 * RFC 2225 say that SNAP encapsulation is used, not LLC
4973 * encapsulation with LLCSAP_IP.
4975 * So we always check for ETHERTYPE_IP.
4977 b0
= gen_linktype(ETHERTYPE_IP
);
4983 b1
= gen_portop(port
, ip_proto
, dir
);
4987 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4988 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4990 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5003 gen_portop6(port
, proto
, dir
)
5004 int port
, proto
, dir
;
5006 struct block
*b0
, *b1
, *tmp
;
5008 /* ip6 proto 'proto' */
5009 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5013 b1
= gen_portatom6(0, (bpf_int32
)port
);
5017 b1
= gen_portatom6(2, (bpf_int32
)port
);
5022 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5023 b1
= gen_portatom6(2, (bpf_int32
)port
);
5028 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5029 b1
= gen_portatom6(2, (bpf_int32
)port
);
5041 static struct block
*
5042 gen_port6(port
, ip_proto
, dir
)
5047 struct block
*b0
, *b1
, *tmp
;
5049 /* link proto ip6 */
5050 b0
= gen_linktype(ETHERTYPE_IPV6
);
5056 b1
= gen_portop6(port
, ip_proto
, dir
);
5060 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5061 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5063 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5075 /* gen_portrange code */
5076 static struct block
*
5077 gen_portrangeatom(off
, v1
, v2
)
5081 struct block
*b1
, *b2
;
5085 * Reverse the order of the ports, so v1 is the lower one.
5094 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5095 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5103 gen_portrangeop(port1
, port2
, proto
, dir
)
5108 struct block
*b0
, *b1
, *tmp
;
5110 /* ip proto 'proto' */
5111 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5117 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5121 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5126 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5127 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5132 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5133 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5145 static struct block
*
5146 gen_portrange(port1
, port2
, ip_proto
, dir
)
5151 struct block
*b0
, *b1
, *tmp
;
5154 b0
= gen_linktype(ETHERTYPE_IP
);
5160 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5164 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5165 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5167 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5179 static struct block
*
5180 gen_portrangeatom6(off
, v1
, v2
)
5184 struct block
*b1
, *b2
;
5188 * Reverse the order of the ports, so v1 is the lower one.
5197 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5198 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5206 gen_portrangeop6(port1
, port2
, proto
, dir
)
5211 struct block
*b0
, *b1
, *tmp
;
5213 /* ip6 proto 'proto' */
5214 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5218 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5222 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5227 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5228 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5233 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5234 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5246 static struct block
*
5247 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5252 struct block
*b0
, *b1
, *tmp
;
5254 /* link proto ip6 */
5255 b0
= gen_linktype(ETHERTYPE_IPV6
);
5261 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5265 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5266 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5268 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5281 lookup_proto(name
, proto
)
5282 register const char *name
;
5292 v
= pcap_nametoproto(name
);
5293 if (v
== PROTO_UNDEF
)
5294 bpf_error("unknown ip proto '%s'", name
);
5298 /* XXX should look up h/w protocol type based on linktype */
5299 v
= pcap_nametoeproto(name
);
5300 if (v
== PROTO_UNDEF
) {
5301 v
= pcap_nametollc(name
);
5302 if (v
== PROTO_UNDEF
)
5303 bpf_error("unknown ether proto '%s'", name
);
5308 if (strcmp(name
, "esis") == 0)
5310 else if (strcmp(name
, "isis") == 0)
5312 else if (strcmp(name
, "clnp") == 0)
5315 bpf_error("unknown osi proto '%s'", name
);
5335 static struct block
*
5336 gen_protochain(v
, proto
, dir
)
5341 #ifdef NO_PROTOCHAIN
5342 return gen_proto(v
, proto
, dir
);
5344 struct block
*b0
, *b
;
5345 struct slist
*s
[100];
5346 int fix2
, fix3
, fix4
, fix5
;
5347 int ahcheck
, again
, end
;
5349 int reg2
= alloc_reg();
5351 memset(s
, 0, sizeof(s
));
5352 fix2
= fix3
= fix4
= fix5
= 0;
5359 b0
= gen_protochain(v
, Q_IP
, dir
);
5360 b
= gen_protochain(v
, Q_IPV6
, dir
);
5364 bpf_error("bad protocol applied for 'protochain'");
5369 * We don't handle variable-length prefixes before the link-layer
5370 * header, or variable-length link-layer headers, here yet.
5371 * We might want to add BPF instructions to do the protochain
5372 * work, to simplify that and, on platforms that have a BPF
5373 * interpreter with the new instructions, let the filtering
5374 * be done in the kernel. (We already require a modified BPF
5375 * engine to do the protochain stuff, to support backward
5376 * branches, and backward branch support is unlikely to appear
5377 * in kernel BPF engines.)
5381 case DLT_IEEE802_11
:
5382 case DLT_PRISM_HEADER
:
5383 case DLT_IEEE802_11_RADIO_AVS
:
5384 case DLT_IEEE802_11_RADIO
:
5386 bpf_error("'protochain' not supported with 802.11");
5389 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5392 * s[0] is a dummy entry to protect other BPF insn from damage
5393 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5394 * hard to find interdependency made by jump table fixup.
5397 s
[i
] = new_stmt(0); /*dummy*/
5402 b0
= gen_linktype(ETHERTYPE_IP
);
5405 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5406 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5408 /* X = ip->ip_hl << 2 */
5409 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5410 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5415 b0
= gen_linktype(ETHERTYPE_IPV6
);
5417 /* A = ip6->ip_nxt */
5418 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5419 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5421 /* X = sizeof(struct ip6_hdr) */
5422 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5428 bpf_error("unsupported proto to gen_protochain");
5432 /* again: if (A == v) goto end; else fall through; */
5434 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5436 s
[i
]->s
.jt
= NULL
; /*later*/
5437 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5441 #ifndef IPPROTO_NONE
5442 #define IPPROTO_NONE 59
5444 /* if (A == IPPROTO_NONE) goto end */
5445 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5446 s
[i
]->s
.jt
= NULL
; /*later*/
5447 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5448 s
[i
]->s
.k
= IPPROTO_NONE
;
5449 s
[fix5
]->s
.jf
= s
[i
];
5454 if (proto
== Q_IPV6
) {
5455 int v6start
, v6end
, v6advance
, j
;
5458 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5459 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5460 s
[i
]->s
.jt
= NULL
; /*later*/
5461 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5462 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5463 s
[fix2
]->s
.jf
= s
[i
];
5465 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5466 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5467 s
[i
]->s
.jt
= NULL
; /*later*/
5468 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5469 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5471 /* if (A == IPPROTO_ROUTING) goto v6advance */
5472 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5473 s
[i
]->s
.jt
= NULL
; /*later*/
5474 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5475 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5477 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5478 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5479 s
[i
]->s
.jt
= NULL
; /*later*/
5480 s
[i
]->s
.jf
= NULL
; /*later*/
5481 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5492 * X = X + (P[X + 1] + 1) * 8;
5495 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5497 /* A = P[X + packet head] */
5498 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5499 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5502 s
[i
] = new_stmt(BPF_ST
);
5506 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5509 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5513 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5515 /* A = P[X + packet head]; */
5516 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5517 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5520 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5524 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5528 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5531 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5535 /* goto again; (must use BPF_JA for backward jump) */
5536 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5537 s
[i
]->s
.k
= again
- i
- 1;
5538 s
[i
- 1]->s
.jf
= s
[i
];
5542 for (j
= v6start
; j
<= v6end
; j
++)
5543 s
[j
]->s
.jt
= s
[v6advance
];
5548 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5550 s
[fix2
]->s
.jf
= s
[i
];
5556 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5557 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5558 s
[i
]->s
.jt
= NULL
; /*later*/
5559 s
[i
]->s
.jf
= NULL
; /*later*/
5560 s
[i
]->s
.k
= IPPROTO_AH
;
5562 s
[fix3
]->s
.jf
= s
[ahcheck
];
5569 * X = X + (P[X + 1] + 2) * 4;
5572 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5574 /* A = P[X + packet head]; */
5575 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5576 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5579 s
[i
] = new_stmt(BPF_ST
);
5583 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5586 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5590 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5592 /* A = P[X + packet head] */
5593 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5594 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5597 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5601 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5605 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5608 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5612 /* goto again; (must use BPF_JA for backward jump) */
5613 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5614 s
[i
]->s
.k
= again
- i
- 1;
5619 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5621 s
[fix2
]->s
.jt
= s
[end
];
5622 s
[fix4
]->s
.jf
= s
[end
];
5623 s
[fix5
]->s
.jt
= s
[end
];
5630 for (i
= 0; i
< max
- 1; i
++)
5631 s
[i
]->next
= s
[i
+ 1];
5632 s
[max
- 1]->next
= NULL
;
5637 b
= new_block(JMP(BPF_JEQ
));
5638 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5648 static struct block
*
5649 gen_check_802_11_data_frame()
5652 struct block
*b0
, *b1
;
5655 * A data frame has the 0x08 bit (b3) in the frame control field set
5656 * and the 0x04 bit (b2) clear.
5658 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5659 b0
= new_block(JMP(BPF_JSET
));
5663 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5664 b1
= new_block(JMP(BPF_JSET
));
5675 * Generate code that checks whether the packet is a packet for protocol
5676 * <proto> and whether the type field in that protocol's header has
5677 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5678 * IP packet and checks the protocol number in the IP header against <v>.
5680 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5681 * against Q_IP and Q_IPV6.
5683 static struct block
*
5684 gen_proto(v
, proto
, dir
)
5689 struct block
*b0
, *b1
;
5691 if (dir
!= Q_DEFAULT
)
5692 bpf_error("direction applied to 'proto'");
5697 b0
= gen_proto(v
, Q_IP
, dir
);
5698 b1
= gen_proto(v
, Q_IPV6
, dir
);
5706 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5707 * not LLC encapsulation with LLCSAP_IP.
5709 * For IEEE 802 networks - which includes 802.5 token ring
5710 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5711 * says that SNAP encapsulation is used, not LLC encapsulation
5714 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5715 * RFC 2225 say that SNAP encapsulation is used, not LLC
5716 * encapsulation with LLCSAP_IP.
5718 * So we always check for ETHERTYPE_IP.
5720 b0
= gen_linktype(ETHERTYPE_IP
);
5722 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5724 b1
= gen_protochain(v
, Q_IP
);
5734 * Frame Relay packets typically have an OSI
5735 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5736 * generates code to check for all the OSI
5737 * NLPIDs, so calling it and then adding a check
5738 * for the particular NLPID for which we're
5739 * looking is bogus, as we can just check for
5742 * What we check for is the NLPID and a frame
5743 * control field value of UI, i.e. 0x03 followed
5746 * XXX - assumes a 2-byte Frame Relay header with
5747 * DLCI and flags. What if the address is longer?
5749 * XXX - what about SNAP-encapsulated frames?
5751 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5757 * Cisco uses an Ethertype lookalike - for OSI,
5760 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5761 /* OSI in C-HDLC is stuffed with a fudge byte */
5762 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5767 b0
= gen_linktype(LLCSAP_ISONS
);
5768 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5774 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5776 * 4 is the offset of the PDU type relative to the IS-IS
5779 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5784 bpf_error("arp does not encapsulate another protocol");
5788 bpf_error("rarp does not encapsulate another protocol");
5792 bpf_error("atalk encapsulation is not specifiable");
5796 bpf_error("decnet encapsulation is not specifiable");
5800 bpf_error("sca does not encapsulate another protocol");
5804 bpf_error("lat does not encapsulate another protocol");
5808 bpf_error("moprc does not encapsulate another protocol");
5812 bpf_error("mopdl does not encapsulate another protocol");
5816 return gen_linktype(v
);
5819 bpf_error("'udp proto' is bogus");
5823 bpf_error("'tcp proto' is bogus");
5827 bpf_error("'sctp proto' is bogus");
5831 bpf_error("'icmp proto' is bogus");
5835 bpf_error("'igmp proto' is bogus");
5839 bpf_error("'igrp proto' is bogus");
5843 bpf_error("'pim proto' is bogus");
5847 bpf_error("'vrrp proto' is bogus");
5852 b0
= gen_linktype(ETHERTYPE_IPV6
);
5854 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5856 b1
= gen_protochain(v
, Q_IPV6
);
5862 bpf_error("'icmp6 proto' is bogus");
5866 bpf_error("'ah proto' is bogus");
5869 bpf_error("'ah proto' is bogus");
5872 bpf_error("'stp proto' is bogus");
5875 bpf_error("'ipx proto' is bogus");
5878 bpf_error("'netbeui proto' is bogus");
5881 bpf_error("'radio proto' is bogus");
5892 register const char *name
;
5895 int proto
= q
.proto
;
5899 bpf_u_int32 mask
, addr
;
5901 bpf_u_int32
**alist
;
5904 struct sockaddr_in
*sin4
;
5905 struct sockaddr_in6
*sin6
;
5906 struct addrinfo
*res
, *res0
;
5907 struct in6_addr mask128
;
5909 struct block
*b
, *tmp
;
5910 int port
, real_proto
;
5916 addr
= pcap_nametonetaddr(name
);
5918 bpf_error("unknown network '%s'", name
);
5919 /* Left justify network addr and calculate its network mask */
5921 while (addr
&& (addr
& 0xff000000) == 0) {
5925 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5929 if (proto
== Q_LINK
) {
5933 eaddr
= pcap_ether_hostton(name
);
5936 "unknown ether host '%s'", name
);
5937 b
= gen_ehostop(eaddr
, dir
);
5942 eaddr
= pcap_ether_hostton(name
);
5945 "unknown FDDI host '%s'", name
);
5946 b
= gen_fhostop(eaddr
, dir
);
5951 eaddr
= pcap_ether_hostton(name
);
5954 "unknown token ring host '%s'", name
);
5955 b
= gen_thostop(eaddr
, dir
);
5959 case DLT_IEEE802_11
:
5960 case DLT_PRISM_HEADER
:
5961 case DLT_IEEE802_11_RADIO_AVS
:
5962 case DLT_IEEE802_11_RADIO
:
5964 eaddr
= pcap_ether_hostton(name
);
5967 "unknown 802.11 host '%s'", name
);
5968 b
= gen_wlanhostop(eaddr
, dir
);
5972 case DLT_IP_OVER_FC
:
5973 eaddr
= pcap_ether_hostton(name
);
5976 "unknown Fibre Channel host '%s'", name
);
5977 b
= gen_ipfchostop(eaddr
, dir
);
5986 * Check that the packet doesn't begin
5987 * with an LE Control marker. (We've
5988 * already generated a test for LANE.)
5990 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5994 eaddr
= pcap_ether_hostton(name
);
5997 "unknown ether host '%s'", name
);
5998 b
= gen_ehostop(eaddr
, dir
);
6004 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6005 } else if (proto
== Q_DECNET
) {
6006 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6008 * I don't think DECNET hosts can be multihomed, so
6009 * there is no need to build up a list of addresses
6011 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6014 alist
= pcap_nametoaddr(name
);
6015 if (alist
== NULL
|| *alist
== NULL
)
6016 bpf_error("unknown host '%s'", name
);
6018 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6020 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6022 tmp
= gen_host(**alist
++, 0xffffffff,
6023 tproto
, dir
, q
.addr
);
6029 memset(&mask128
, 0xff, sizeof(mask128
));
6030 res0
= res
= pcap_nametoaddrinfo(name
);
6032 bpf_error("unknown host '%s'", name
);
6035 tproto
= tproto6
= proto
;
6036 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6040 for (res
= res0
; res
; res
= res
->ai_next
) {
6041 switch (res
->ai_family
) {
6043 if (tproto
== Q_IPV6
)
6046 sin4
= (struct sockaddr_in
*)
6048 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6049 0xffffffff, tproto
, dir
, q
.addr
);
6052 if (tproto6
== Q_IP
)
6055 sin6
= (struct sockaddr_in6
*)
6057 tmp
= gen_host6(&sin6
->sin6_addr
,
6058 &mask128
, tproto6
, dir
, q
.addr
);
6070 bpf_error("unknown host '%s'%s", name
,
6071 (proto
== Q_DEFAULT
)
6073 : " for specified address family");
6080 if (proto
!= Q_DEFAULT
&&
6081 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6082 bpf_error("illegal qualifier of 'port'");
6083 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6084 bpf_error("unknown port '%s'", name
);
6085 if (proto
== Q_UDP
) {
6086 if (real_proto
== IPPROTO_TCP
)
6087 bpf_error("port '%s' is tcp", name
);
6088 else if (real_proto
== IPPROTO_SCTP
)
6089 bpf_error("port '%s' is sctp", name
);
6091 /* override PROTO_UNDEF */
6092 real_proto
= IPPROTO_UDP
;
6094 if (proto
== Q_TCP
) {
6095 if (real_proto
== IPPROTO_UDP
)
6096 bpf_error("port '%s' is udp", name
);
6098 else if (real_proto
== IPPROTO_SCTP
)
6099 bpf_error("port '%s' is sctp", name
);
6101 /* override PROTO_UNDEF */
6102 real_proto
= IPPROTO_TCP
;
6104 if (proto
== Q_SCTP
) {
6105 if (real_proto
== IPPROTO_UDP
)
6106 bpf_error("port '%s' is udp", name
);
6108 else if (real_proto
== IPPROTO_TCP
)
6109 bpf_error("port '%s' is tcp", name
);
6111 /* override PROTO_UNDEF */
6112 real_proto
= IPPROTO_SCTP
;
6115 return gen_port(port
, real_proto
, dir
);
6117 b
= gen_port(port
, real_proto
, dir
);
6118 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6123 if (proto
!= Q_DEFAULT
&&
6124 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6125 bpf_error("illegal qualifier of 'portrange'");
6126 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6127 bpf_error("unknown port in range '%s'", name
);
6128 if (proto
== Q_UDP
) {
6129 if (real_proto
== IPPROTO_TCP
)
6130 bpf_error("port in range '%s' is tcp", name
);
6131 else if (real_proto
== IPPROTO_SCTP
)
6132 bpf_error("port in range '%s' is sctp", name
);
6134 /* override PROTO_UNDEF */
6135 real_proto
= IPPROTO_UDP
;
6137 if (proto
== Q_TCP
) {
6138 if (real_proto
== IPPROTO_UDP
)
6139 bpf_error("port in range '%s' is udp", name
);
6140 else if (real_proto
== IPPROTO_SCTP
)
6141 bpf_error("port in range '%s' is sctp", name
);
6143 /* override PROTO_UNDEF */
6144 real_proto
= IPPROTO_TCP
;
6146 if (proto
== Q_SCTP
) {
6147 if (real_proto
== IPPROTO_UDP
)
6148 bpf_error("port in range '%s' is udp", name
);
6149 else if (real_proto
== IPPROTO_TCP
)
6150 bpf_error("port in range '%s' is tcp", name
);
6152 /* override PROTO_UNDEF */
6153 real_proto
= IPPROTO_SCTP
;
6156 return gen_portrange(port1
, port2
, real_proto
, dir
);
6158 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6159 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6165 eaddr
= pcap_ether_hostton(name
);
6167 bpf_error("unknown ether host: %s", name
);
6169 alist
= pcap_nametoaddr(name
);
6170 if (alist
== NULL
|| *alist
== NULL
)
6171 bpf_error("unknown host '%s'", name
);
6172 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6176 bpf_error("'gateway' not supported in this configuration");
6180 real_proto
= lookup_proto(name
, proto
);
6181 if (real_proto
>= 0)
6182 return gen_proto(real_proto
, proto
, dir
);
6184 bpf_error("unknown protocol: %s", name
);
6187 real_proto
= lookup_proto(name
, proto
);
6188 if (real_proto
>= 0)
6189 return gen_protochain(real_proto
, proto
, dir
);
6191 bpf_error("unknown protocol: %s", name
);
6202 gen_mcode(s1
, s2
, masklen
, q
)
6203 register const char *s1
, *s2
;
6204 register int masklen
;
6207 register int nlen
, mlen
;
6210 nlen
= __pcap_atoin(s1
, &n
);
6211 /* Promote short ipaddr */
6215 mlen
= __pcap_atoin(s2
, &m
);
6216 /* Promote short ipaddr */
6219 bpf_error("non-network bits set in \"%s mask %s\"",
6222 /* Convert mask len to mask */
6224 bpf_error("mask length must be <= 32");
6227 * X << 32 is not guaranteed by C to be 0; it's
6232 m
= 0xffffffff << (32 - masklen
);
6234 bpf_error("non-network bits set in \"%s/%d\"",
6241 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6244 bpf_error("Mask syntax for networks only");
6253 register const char *s
;
6258 int proto
= q
.proto
;
6264 else if (q
.proto
== Q_DECNET
)
6265 vlen
= __pcap_atodn(s
, &v
);
6267 vlen
= __pcap_atoin(s
, &v
);
6274 if (proto
== Q_DECNET
)
6275 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6276 else if (proto
== Q_LINK
) {
6277 bpf_error("illegal link layer address");
6280 if (s
== NULL
&& q
.addr
== Q_NET
) {
6281 /* Promote short net number */
6282 while (v
&& (v
& 0xff000000) == 0) {
6287 /* Promote short ipaddr */
6291 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6296 proto
= IPPROTO_UDP
;
6297 else if (proto
== Q_TCP
)
6298 proto
= IPPROTO_TCP
;
6299 else if (proto
== Q_SCTP
)
6300 proto
= IPPROTO_SCTP
;
6301 else if (proto
== Q_DEFAULT
)
6302 proto
= PROTO_UNDEF
;
6304 bpf_error("illegal qualifier of 'port'");
6307 return gen_port((int)v
, proto
, dir
);
6311 b
= gen_port((int)v
, proto
, dir
);
6312 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6319 proto
= IPPROTO_UDP
;
6320 else if (proto
== Q_TCP
)
6321 proto
= IPPROTO_TCP
;
6322 else if (proto
== Q_SCTP
)
6323 proto
= IPPROTO_SCTP
;
6324 else if (proto
== Q_DEFAULT
)
6325 proto
= PROTO_UNDEF
;
6327 bpf_error("illegal qualifier of 'portrange'");
6330 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6334 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6335 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6341 bpf_error("'gateway' requires a name");
6345 return gen_proto((int)v
, proto
, dir
);
6348 return gen_protochain((int)v
, proto
, dir
);
6363 gen_mcode6(s1
, s2
, masklen
, q
)
6364 register const char *s1
, *s2
;
6365 register int masklen
;
6368 struct addrinfo
*res
;
6369 struct in6_addr
*addr
;
6370 struct in6_addr mask
;
6375 bpf_error("no mask %s supported", s2
);
6377 res
= pcap_nametoaddrinfo(s1
);
6379 bpf_error("invalid ip6 address %s", s1
);
6382 bpf_error("%s resolved to multiple address", s1
);
6383 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6385 if (sizeof(mask
) * 8 < masklen
)
6386 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6387 memset(&mask
, 0, sizeof(mask
));
6388 memset(&mask
, 0xff, masklen
/ 8);
6390 mask
.s6_addr
[masklen
/ 8] =
6391 (0xff << (8 - masklen
% 8)) & 0xff;
6394 a
= (u_int32_t
*)addr
;
6395 m
= (u_int32_t
*)&mask
;
6396 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6397 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6398 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6406 bpf_error("Mask syntax for networks only");
6410 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6416 bpf_error("invalid qualifier against IPv6 address");
6425 register const u_char
*eaddr
;
6428 struct block
*b
, *tmp
;
6430 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6433 return gen_ehostop(eaddr
, (int)q
.dir
);
6435 return gen_fhostop(eaddr
, (int)q
.dir
);
6437 return gen_thostop(eaddr
, (int)q
.dir
);
6438 case DLT_IEEE802_11
:
6439 case DLT_PRISM_HEADER
:
6440 case DLT_IEEE802_11_RADIO_AVS
:
6441 case DLT_IEEE802_11_RADIO
:
6443 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6447 * Check that the packet doesn't begin with an
6448 * LE Control marker. (We've already generated
6451 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6456 * Now check the MAC address.
6458 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6463 case DLT_IP_OVER_FC
:
6464 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6466 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6470 bpf_error("ethernet address used in non-ether expression");
6477 struct slist
*s0
, *s1
;
6480 * This is definitely not the best way to do this, but the
6481 * lists will rarely get long.
6488 static struct slist
*
6494 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6499 static struct slist
*
6505 s
= new_stmt(BPF_LD
|BPF_MEM
);
6511 * Modify "index" to use the value stored into its register as an
6512 * offset relative to the beginning of the header for the protocol
6513 * "proto", and allocate a register and put an item "size" bytes long
6514 * (1, 2, or 4) at that offset into that register, making it the register
6518 gen_load(proto
, inst
, size
)
6523 struct slist
*s
, *tmp
;
6525 int regno
= alloc_reg();
6527 free_reg(inst
->regno
);
6531 bpf_error("data size must be 1, 2, or 4");
6547 bpf_error("unsupported index operation");
6551 * The offset is relative to the beginning of the packet
6552 * data, if we have a radio header. (If we don't, this
6555 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6556 linktype
!= DLT_IEEE802_11_RADIO
&&
6557 linktype
!= DLT_PRISM_HEADER
)
6558 bpf_error("radio information not present in capture");
6561 * Load into the X register the offset computed into the
6562 * register specifed by "index".
6564 s
= xfer_to_x(inst
);
6567 * Load the item at that offset.
6569 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6571 sappend(inst
->s
, s
);
6576 * The offset is relative to the beginning of
6577 * the link-layer header.
6579 * XXX - what about ATM LANE? Should the index be
6580 * relative to the beginning of the AAL5 frame, so
6581 * that 0 refers to the beginning of the LE Control
6582 * field, or relative to the beginning of the LAN
6583 * frame, so that 0 refers, for Ethernet LANE, to
6584 * the beginning of the destination address?
6586 s
= gen_llprefixlen();
6589 * If "s" is non-null, it has code to arrange that the
6590 * X register contains the length of the prefix preceding
6591 * the link-layer header. Add to it the offset computed
6592 * into the register specified by "index", and move that
6593 * into the X register. Otherwise, just load into the X
6594 * register the offset computed into the register specifed
6598 sappend(s
, xfer_to_a(inst
));
6599 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6600 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6602 s
= xfer_to_x(inst
);
6605 * Load the item at the sum of the offset we've put in the
6606 * X register and the offset of the start of the link
6607 * layer header (which is 0 if the radio header is
6608 * variable-length; that header length is what we put
6609 * into the X register and then added to the index).
6611 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6614 sappend(inst
->s
, s
);
6630 * The offset is relative to the beginning of
6631 * the network-layer header.
6632 * XXX - are there any cases where we want
6635 s
= gen_off_macpl();
6638 * If "s" is non-null, it has code to arrange that the
6639 * X register contains the offset of the MAC-layer
6640 * payload. Add to it the offset computed into the
6641 * register specified by "index", and move that into
6642 * the X register. Otherwise, just load into the X
6643 * register the offset computed into the register specifed
6647 sappend(s
, xfer_to_a(inst
));
6648 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6649 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6651 s
= xfer_to_x(inst
);
6654 * Load the item at the sum of the offset we've put in the
6655 * X register, the offset of the start of the network
6656 * layer header from the beginning of the MAC-layer
6657 * payload, and the purported offset of the start of the
6658 * MAC-layer payload (which might be 0 if there's a
6659 * variable-length prefix before the link-layer header
6660 * or the link-layer header itself is variable-length;
6661 * the variable-length offset of the start of the
6662 * MAC-layer payload is what we put into the X register
6663 * and then added to the index).
6665 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6666 tmp
->s
.k
= off_macpl
+ off_nl
;
6668 sappend(inst
->s
, s
);
6671 * Do the computation only if the packet contains
6672 * the protocol in question.
6674 b
= gen_proto_abbrev(proto
);
6676 gen_and(inst
->b
, b
);
6689 * The offset is relative to the beginning of
6690 * the transport-layer header.
6692 * Load the X register with the length of the IPv4 header
6693 * (plus the offset of the link-layer header, if it's
6694 * a variable-length header), in bytes.
6696 * XXX - are there any cases where we want
6698 * XXX - we should, if we're built with
6699 * IPv6 support, generate code to load either
6700 * IPv4, IPv6, or both, as appropriate.
6702 s
= gen_loadx_iphdrlen();
6705 * The X register now contains the sum of the length
6706 * of any variable-length header preceding the link-layer
6707 * header, any variable-length link-layer header, and the
6708 * length of the network-layer header.
6710 * Load into the A register the offset relative to
6711 * the beginning of the transport layer header,
6712 * add the X register to that, move that to the
6713 * X register, and load with an offset from the
6714 * X register equal to the offset of the network
6715 * layer header relative to the beginning of
6716 * the MAC-layer payload plus the fixed-length
6717 * portion of the offset of the MAC-layer payload
6718 * from the beginning of the raw packet data.
6720 sappend(s
, xfer_to_a(inst
));
6721 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6722 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6723 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6724 tmp
->s
.k
= off_macpl
+ off_nl
;
6725 sappend(inst
->s
, s
);
6728 * Do the computation only if the packet contains
6729 * the protocol in question - which is true only
6730 * if this is an IP datagram and is the first or
6731 * only fragment of that datagram.
6733 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6735 gen_and(inst
->b
, b
);
6737 gen_and(gen_proto_abbrev(Q_IP
), b
);
6743 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6747 inst
->regno
= regno
;
6748 s
= new_stmt(BPF_ST
);
6750 sappend(inst
->s
, s
);
6756 gen_relation(code
, a0
, a1
, reversed
)
6758 struct arth
*a0
, *a1
;
6761 struct slist
*s0
, *s1
, *s2
;
6762 struct block
*b
, *tmp
;
6766 if (code
== BPF_JEQ
) {
6767 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6768 b
= new_block(JMP(code
));
6772 b
= new_block(BPF_JMP
|code
|BPF_X
);
6778 sappend(a0
->s
, a1
->s
);
6782 free_reg(a0
->regno
);
6783 free_reg(a1
->regno
);
6785 /* 'and' together protocol checks */
6788 gen_and(a0
->b
, tmp
= a1
->b
);
6804 int regno
= alloc_reg();
6805 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6808 s
= new_stmt(BPF_LD
|BPF_LEN
);
6809 s
->next
= new_stmt(BPF_ST
);
6810 s
->next
->s
.k
= regno
;
6825 a
= (struct arth
*)newchunk(sizeof(*a
));
6829 s
= new_stmt(BPF_LD
|BPF_IMM
);
6831 s
->next
= new_stmt(BPF_ST
);
6847 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6850 s
= new_stmt(BPF_ST
);
6858 gen_arth(code
, a0
, a1
)
6860 struct arth
*a0
, *a1
;
6862 struct slist
*s0
, *s1
, *s2
;
6866 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6871 sappend(a0
->s
, a1
->s
);
6873 free_reg(a0
->regno
);
6874 free_reg(a1
->regno
);
6876 s0
= new_stmt(BPF_ST
);
6877 a0
->regno
= s0
->s
.k
= alloc_reg();
6884 * Here we handle simple allocation of the scratch registers.
6885 * If too many registers are alloc'd, the allocator punts.
6887 static int regused
[BPF_MEMWORDS
];
6891 * Initialize the table of used registers and the current register.
6897 memset(regused
, 0, sizeof regused
);
6901 * Return the next free register.
6906 int n
= BPF_MEMWORDS
;
6909 if (regused
[curreg
])
6910 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6912 regused
[curreg
] = 1;
6916 bpf_error("too many registers needed to evaluate expression");
6922 * Return a register to the table so it can
6932 static struct block
*
6939 s
= new_stmt(BPF_LD
|BPF_LEN
);
6940 b
= new_block(JMP(jmp
));
6951 return gen_len(BPF_JGE
, n
);
6955 * Actually, this is less than or equal.
6963 b
= gen_len(BPF_JGT
, n
);
6970 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6971 * the beginning of the link-layer header.
6972 * XXX - that means you can't test values in the radiotap header, but
6973 * as that header is difficult if not impossible to parse generally
6974 * without a loop, that might not be a severe problem. A new keyword
6975 * "radio" could be added for that, although what you'd really want
6976 * would be a way of testing particular radio header values, which
6977 * would generate code appropriate to the radio header in question.
6980 gen_byteop(op
, idx
, val
)
6991 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6994 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6998 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7002 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7006 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7010 b
= new_block(JMP(BPF_JEQ
));
7017 static u_char abroadcast
[] = { 0x0 };
7020 gen_broadcast(proto
)
7023 bpf_u_int32 hostmask
;
7024 struct block
*b0
, *b1
, *b2
;
7025 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7033 case DLT_ARCNET_LINUX
:
7034 return gen_ahostop(abroadcast
, Q_DST
);
7036 return gen_ehostop(ebroadcast
, Q_DST
);
7038 return gen_fhostop(ebroadcast
, Q_DST
);
7040 return gen_thostop(ebroadcast
, Q_DST
);
7041 case DLT_IEEE802_11
:
7042 case DLT_PRISM_HEADER
:
7043 case DLT_IEEE802_11_RADIO_AVS
:
7044 case DLT_IEEE802_11_RADIO
:
7046 return gen_wlanhostop(ebroadcast
, Q_DST
);
7047 case DLT_IP_OVER_FC
:
7048 return gen_ipfchostop(ebroadcast
, Q_DST
);
7052 * Check that the packet doesn't begin with an
7053 * LE Control marker. (We've already generated
7056 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7061 * Now check the MAC address.
7063 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7069 bpf_error("not a broadcast link");
7074 b0
= gen_linktype(ETHERTYPE_IP
);
7075 hostmask
= ~netmask
;
7076 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7077 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7078 (bpf_int32
)(~0 & hostmask
), hostmask
);
7083 bpf_error("only link-layer/IP broadcast filters supported");
7089 * Generate code to test the low-order bit of a MAC address (that's
7090 * the bottom bit of the *first* byte).
7092 static struct block
*
7093 gen_mac_multicast(offset
)
7096 register struct block
*b0
;
7097 register struct slist
*s
;
7099 /* link[offset] & 1 != 0 */
7100 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7101 b0
= new_block(JMP(BPF_JSET
));
7108 gen_multicast(proto
)
7111 register struct block
*b0
, *b1
, *b2
;
7112 register struct slist
*s
;
7120 case DLT_ARCNET_LINUX
:
7121 /* all ARCnet multicasts use the same address */
7122 return gen_ahostop(abroadcast
, Q_DST
);
7124 /* ether[0] & 1 != 0 */
7125 return gen_mac_multicast(0);
7128 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7130 * XXX - was that referring to bit-order issues?
7132 /* fddi[1] & 1 != 0 */
7133 return gen_mac_multicast(1);
7135 /* tr[2] & 1 != 0 */
7136 return gen_mac_multicast(2);
7137 case DLT_IEEE802_11
:
7138 case DLT_PRISM_HEADER
:
7139 case DLT_IEEE802_11_RADIO_AVS
:
7140 case DLT_IEEE802_11_RADIO
:
7145 * For control frames, there is no DA.
7147 * For management frames, DA is at an
7148 * offset of 4 from the beginning of
7151 * For data frames, DA is at an offset
7152 * of 4 from the beginning of the packet
7153 * if To DS is clear and at an offset of
7154 * 16 from the beginning of the packet
7159 * Generate the tests to be done for data frames.
7161 * First, check for To DS set, i.e. "link[1] & 0x01".
7163 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7164 b1
= new_block(JMP(BPF_JSET
));
7165 b1
->s
.k
= 0x01; /* To DS */
7169 * If To DS is set, the DA is at 16.
7171 b0
= gen_mac_multicast(16);
7175 * Now, check for To DS not set, i.e. check
7176 * "!(link[1] & 0x01)".
7178 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7179 b2
= new_block(JMP(BPF_JSET
));
7180 b2
->s
.k
= 0x01; /* To DS */
7185 * If To DS is not set, the DA is at 4.
7187 b1
= gen_mac_multicast(4);
7191 * Now OR together the last two checks. That gives
7192 * the complete set of checks for data frames.
7197 * Now check for a data frame.
7198 * I.e, check "link[0] & 0x08".
7200 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7201 b1
= new_block(JMP(BPF_JSET
));
7206 * AND that with the checks done for data frames.
7211 * If the high-order bit of the type value is 0, this
7212 * is a management frame.
7213 * I.e, check "!(link[0] & 0x08)".
7215 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7216 b2
= new_block(JMP(BPF_JSET
));
7222 * For management frames, the DA is at 4.
7224 b1
= gen_mac_multicast(4);
7228 * OR that with the checks done for data frames.
7229 * That gives the checks done for management and
7235 * If the low-order bit of the type value is 1,
7236 * this is either a control frame or a frame
7237 * with a reserved type, and thus not a
7240 * I.e., check "!(link[0] & 0x04)".
7242 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7243 b1
= new_block(JMP(BPF_JSET
));
7249 * AND that with the checks for data and management
7254 case DLT_IP_OVER_FC
:
7255 b0
= gen_mac_multicast(2);
7260 * Check that the packet doesn't begin with an
7261 * LE Control marker. (We've already generated
7264 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7268 /* ether[off_mac] & 1 != 0 */
7269 b0
= gen_mac_multicast(off_mac
);
7277 /* Link not known to support multicasts */
7281 b0
= gen_linktype(ETHERTYPE_IP
);
7282 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7288 b0
= gen_linktype(ETHERTYPE_IPV6
);
7289 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7294 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7300 * generate command for inbound/outbound. It's here so we can
7301 * make it link-type specific. 'dir' = 0 implies "inbound",
7302 * = 1 implies "outbound".
7308 register struct block
*b0
;
7311 * Only some data link types support inbound/outbound qualifiers.
7315 b0
= gen_relation(BPF_JEQ
,
7316 gen_load(Q_LINK
, gen_loadi(0), 1),
7324 * Match packets sent by this machine.
7326 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7329 * Match packets sent to this machine.
7330 * (No broadcast or multicast packets, or
7331 * packets sent to some other machine and
7332 * received promiscuously.)
7334 * XXX - packets sent to other machines probably
7335 * shouldn't be matched, but what about broadcast
7336 * or multicast packets we received?
7338 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7342 #ifdef HAVE_NET_PFVAR_H
7344 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7345 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7351 /* match outgoing packets */
7352 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7354 /* match incoming packets */
7355 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7359 case DLT_JUNIPER_MFR
:
7360 case DLT_JUNIPER_MLFR
:
7361 case DLT_JUNIPER_MLPPP
:
7362 case DLT_JUNIPER_ATM1
:
7363 case DLT_JUNIPER_ATM2
:
7364 case DLT_JUNIPER_PPPOE
:
7365 case DLT_JUNIPER_PPPOE_ATM
:
7366 case DLT_JUNIPER_GGSN
:
7367 case DLT_JUNIPER_ES
:
7368 case DLT_JUNIPER_MONITOR
:
7369 case DLT_JUNIPER_SERVICES
:
7370 case DLT_JUNIPER_ETHER
:
7371 case DLT_JUNIPER_PPP
:
7372 case DLT_JUNIPER_FRELAY
:
7373 case DLT_JUNIPER_CHDLC
:
7374 case DLT_JUNIPER_VP
:
7375 case DLT_JUNIPER_ST
:
7376 case DLT_JUNIPER_ISM
:
7377 /* juniper flags (including direction) are stored
7378 * the byte after the 3-byte magic number */
7380 /* match outgoing packets */
7381 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7383 /* match incoming packets */
7384 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7389 bpf_error("inbound/outbound not supported on linktype %d",
7397 #ifdef HAVE_NET_PFVAR_H
7398 /* PF firewall log matched interface */
7400 gen_pf_ifname(const char *ifname
)
7405 if (linktype
!= DLT_PFLOG
) {
7406 bpf_error("ifname supported only on PF linktype");
7409 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7410 off
= offsetof(struct pfloghdr
, ifname
);
7411 if (strlen(ifname
) >= len
) {
7412 bpf_error("ifname interface names can only be %d characters",
7416 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7420 /* PF firewall log ruleset name */
7422 gen_pf_ruleset(char *ruleset
)
7426 if (linktype
!= DLT_PFLOG
) {
7427 bpf_error("ruleset supported only on PF linktype");
7431 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7432 bpf_error("ruleset names can only be %ld characters",
7433 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7437 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7438 strlen(ruleset
), (const u_char
*)ruleset
);
7442 /* PF firewall log rule number */
7448 if (linktype
!= DLT_PFLOG
) {
7449 bpf_error("rnr supported only on PF linktype");
7453 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7458 /* PF firewall log sub-rule number */
7460 gen_pf_srnr(int srnr
)
7464 if (linktype
!= DLT_PFLOG
) {
7465 bpf_error("srnr supported only on PF linktype");
7469 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7474 /* PF firewall log reason code */
7476 gen_pf_reason(int reason
)
7480 if (linktype
!= DLT_PFLOG
) {
7481 bpf_error("reason supported only on PF linktype");
7485 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7490 /* PF firewall log action */
7492 gen_pf_action(int action
)
7496 if (linktype
!= DLT_PFLOG
) {
7497 bpf_error("action supported only on PF linktype");
7501 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7505 #else /* !HAVE_NET_PFVAR_H */
7507 gen_pf_ifname(const char *ifname
)
7509 bpf_error("libpcap was compiled without pf support");
7515 gen_pf_ruleset(char *ruleset
)
7517 bpf_error("libpcap was compiled on a machine without pf support");
7525 bpf_error("libpcap was compiled on a machine without pf support");
7531 gen_pf_srnr(int srnr
)
7533 bpf_error("libpcap was compiled on a machine without pf support");
7539 gen_pf_reason(int reason
)
7541 bpf_error("libpcap was compiled on a machine without pf support");
7547 gen_pf_action(int action
)
7549 bpf_error("libpcap was compiled on a machine without pf support");
7553 #endif /* HAVE_NET_PFVAR_H */
7555 /* IEEE 802.11 wireless header */
7557 gen_p80211_type(int type
, int mask
)
7563 case DLT_IEEE802_11
:
7564 case DLT_PRISM_HEADER
:
7565 case DLT_IEEE802_11_RADIO_AVS
:
7566 case DLT_IEEE802_11_RADIO
:
7567 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7572 bpf_error("802.11 link-layer types supported only on 802.11");
7580 gen_p80211_fcdir(int fcdir
)
7586 case DLT_IEEE802_11
:
7587 case DLT_PRISM_HEADER
:
7588 case DLT_IEEE802_11_RADIO_AVS
:
7589 case DLT_IEEE802_11_RADIO
:
7593 bpf_error("frame direction supported only with 802.11 headers");
7597 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7598 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7605 register const u_char
*eaddr
;
7611 case DLT_ARCNET_LINUX
:
7612 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7614 return (gen_ahostop(eaddr
, (int)q
.dir
));
7616 bpf_error("ARCnet address used in non-arc expression");
7622 bpf_error("aid supported only on ARCnet");
7625 bpf_error("ARCnet address used in non-arc expression");
7630 static struct block
*
7631 gen_ahostop(eaddr
, dir
)
7632 register const u_char
*eaddr
;
7635 register struct block
*b0
, *b1
;
7638 /* src comes first, different from Ethernet */
7640 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7643 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7646 b0
= gen_ahostop(eaddr
, Q_SRC
);
7647 b1
= gen_ahostop(eaddr
, Q_DST
);
7653 b0
= gen_ahostop(eaddr
, Q_SRC
);
7654 b1
= gen_ahostop(eaddr
, Q_DST
);
7663 * support IEEE 802.1Q VLAN trunk over ethernet
7669 struct block
*b0
, *b1
;
7671 /* can't check for VLAN-encapsulated packets inside MPLS */
7672 if (label_stack_depth
> 0)
7673 bpf_error("no VLAN match after MPLS");
7676 * Check for a VLAN packet, and then change the offsets to point
7677 * to the type and data fields within the VLAN packet. Just
7678 * increment the offsets, so that we can support a hierarchy, e.g.
7679 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7682 * XXX - this is a bit of a kludge. If we were to split the
7683 * compiler into a parser that parses an expression and
7684 * generates an expression tree, and a code generator that
7685 * takes an expression tree (which could come from our
7686 * parser or from some other parser) and generates BPF code,
7687 * we could perhaps make the offsets parameters of routines
7688 * and, in the handler for an "AND" node, pass to subnodes
7689 * other than the VLAN node the adjusted offsets.
7691 * This would mean that "vlan" would, instead of changing the
7692 * behavior of *all* tests after it, change only the behavior
7693 * of tests ANDed with it. That would change the documented
7694 * semantics of "vlan", which might break some expressions.
7695 * However, it would mean that "(vlan and ip) or ip" would check
7696 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7697 * checking only for VLAN-encapsulated IP, so that could still
7698 * be considered worth doing; it wouldn't break expressions
7699 * that are of the form "vlan and ..." or "vlan N and ...",
7700 * which I suspect are the most common expressions involving
7701 * "vlan". "vlan or ..." doesn't necessarily do what the user
7702 * would really want, now, as all the "or ..." tests would
7703 * be done assuming a VLAN, even though the "or" could be viewed
7704 * as meaning "or, if this isn't a VLAN packet...".
7711 /* check for VLAN */
7712 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7713 (bpf_int32
)ETHERTYPE_8021Q
);
7715 /* If a specific VLAN is requested, check VLAN id */
7716 if (vlan_num
>= 0) {
7717 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7718 (bpf_int32
)vlan_num
, 0x0fff);
7732 bpf_error("no VLAN support for data link type %d",
7747 struct block
*b0
,*b1
;
7750 * Change the offsets to point to the type and data fields within
7751 * the MPLS packet. Just increment the offsets, so that we
7752 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7753 * capture packets with an outer label of 100000 and an inner
7756 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7760 if (label_stack_depth
> 0) {
7761 /* just match the bottom-of-stack bit clear */
7762 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7765 * Indicate that we're checking MPLS-encapsulated headers,
7766 * to make sure higher level code generators don't try to
7767 * match against IP-related protocols such as Q_ARP, Q_RARP
7772 case DLT_C_HDLC
: /* fall through */
7774 b0
= gen_linktype(ETHERTYPE_MPLS
);
7778 b0
= gen_linktype(PPP_MPLS_UCAST
);
7781 /* FIXME add other DLT_s ...
7782 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7783 * leave it for now */
7786 bpf_error("no MPLS support for data link type %d",
7794 /* If a specific MPLS label is requested, check it */
7795 if (label_num
>= 0) {
7796 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7797 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7798 0xfffff000); /* only compare the first 20 bits */
7805 label_stack_depth
++;
7810 * Support PPPOE discovery and session.
7815 /* check for PPPoE discovery */
7816 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7825 * Test against the PPPoE session link-layer type.
7827 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7830 * Change the offsets to point to the type and data fields within
7831 * the PPP packet, and note that this is PPPoE rather than
7834 * XXX - this is a bit of a kludge. If we were to split the
7835 * compiler into a parser that parses an expression and
7836 * generates an expression tree, and a code generator that
7837 * takes an expression tree (which could come from our
7838 * parser or from some other parser) and generates BPF code,
7839 * we could perhaps make the offsets parameters of routines
7840 * and, in the handler for an "AND" node, pass to subnodes
7841 * other than the PPPoE node the adjusted offsets.
7843 * This would mean that "pppoes" would, instead of changing the
7844 * behavior of *all* tests after it, change only the behavior
7845 * of tests ANDed with it. That would change the documented
7846 * semantics of "pppoes", which might break some expressions.
7847 * However, it would mean that "(pppoes and ip) or ip" would check
7848 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7849 * checking only for VLAN-encapsulated IP, so that could still
7850 * be considered worth doing; it wouldn't break expressions
7851 * that are of the form "pppoes and ..." which I suspect are the
7852 * most common expressions involving "pppoes". "pppoes or ..."
7853 * doesn't necessarily do what the user would really want, now,
7854 * as all the "or ..." tests would be done assuming PPPoE, even
7855 * though the "or" could be viewed as meaning "or, if this isn't
7856 * a PPPoE packet...".
7858 orig_linktype
= off_linktype
; /* save original values */
7863 * The "network-layer" protocol is PPPoE, which has a 6-byte
7864 * PPPoE header, followed by a PPP packet.
7866 * There is no HDLC encapsulation for the PPP packet (it's
7867 * encapsulated in PPPoES instead), so the link-layer type
7868 * starts at the first byte of the PPP packet. For PPPoE,
7869 * that offset is relative to the beginning of the total
7870 * link-layer payload, including any 802.2 LLC header, so
7871 * it's 6 bytes past off_nl.
7873 off_linktype
= off_nl
+ 6;
7876 * The network-layer offsets are relative to the beginning
7877 * of the MAC-layer payload; that's past the 6-byte
7878 * PPPoE header and the 2-byte PPP header.
7881 off_nl_nosnap
= 6+2;
7887 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7899 bpf_error("'vpi' supported only on raw ATM");
7900 if (off_vpi
== (u_int
)-1)
7902 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7908 bpf_error("'vci' supported only on raw ATM");
7909 if (off_vci
== (u_int
)-1)
7911 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7916 if (off_proto
== (u_int
)-1)
7917 abort(); /* XXX - this isn't on FreeBSD */
7918 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7923 if (off_payload
== (u_int
)-1)
7925 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7926 0xffffffff, jtype
, reverse
, jvalue
);
7931 bpf_error("'callref' supported only on raw ATM");
7932 if (off_proto
== (u_int
)-1)
7934 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7935 jtype
, reverse
, jvalue
);
7945 gen_atmtype_abbrev(type
)
7948 struct block
*b0
, *b1
;
7953 /* Get all packets in Meta signalling Circuit */
7955 bpf_error("'metac' supported only on raw ATM");
7956 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7957 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7962 /* Get all packets in Broadcast Circuit*/
7964 bpf_error("'bcc' supported only on raw ATM");
7965 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7966 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7971 /* Get all cells in Segment OAM F4 circuit*/
7973 bpf_error("'oam4sc' supported only on raw ATM");
7974 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7975 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7980 /* Get all cells in End-to-End OAM F4 Circuit*/
7982 bpf_error("'oam4ec' supported only on raw ATM");
7983 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7984 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7989 /* Get all packets in connection Signalling Circuit */
7991 bpf_error("'sc' supported only on raw ATM");
7992 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7993 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7998 /* Get all packets in ILMI Circuit */
8000 bpf_error("'ilmic' supported only on raw ATM");
8001 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8002 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8007 /* Get all LANE packets */
8009 bpf_error("'lane' supported only on raw ATM");
8010 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8013 * Arrange that all subsequent tests assume LANE
8014 * rather than LLC-encapsulated packets, and set
8015 * the offsets appropriately for LANE-encapsulated
8018 * "off_mac" is the offset of the Ethernet header,
8019 * which is 2 bytes past the ATM pseudo-header
8020 * (skipping the pseudo-header and 2-byte LE Client
8021 * field). The other offsets are Ethernet offsets
8022 * relative to "off_mac".
8025 off_mac
= off_payload
+ 2; /* MAC header */
8026 off_linktype
= off_mac
+ 12;
8027 off_macpl
= off_mac
+ 14; /* Ethernet */
8028 off_nl
= 0; /* Ethernet II */
8029 off_nl_nosnap
= 3; /* 802.3+802.2 */
8033 /* Get all LLC-encapsulated packets */
8035 bpf_error("'llc' supported only on raw ATM");
8036 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8047 * Filtering for MTP2 messages based on li value
8048 * FISU, length is null
8049 * LSSU, length is 1 or 2
8050 * MSU, length is 3 or more
8053 gen_mtp2type_abbrev(type
)
8056 struct block
*b0
, *b1
;
8061 if ( (linktype
!= DLT_MTP2
) &&
8062 (linktype
!= DLT_ERF
) &&
8063 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8064 bpf_error("'fisu' supported only on MTP2");
8065 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8066 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8070 if ( (linktype
!= DLT_MTP2
) &&
8071 (linktype
!= DLT_ERF
) &&
8072 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8073 bpf_error("'lssu' supported only on MTP2");
8074 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8075 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8080 if ( (linktype
!= DLT_MTP2
) &&
8081 (linktype
!= DLT_ERF
) &&
8082 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8083 bpf_error("'msu' supported only on MTP2");
8084 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8094 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8101 bpf_u_int32 val1
, val2
, val3
;
8103 switch (mtp3field
) {
8106 if (off_sio
== (u_int
)-1)
8107 bpf_error("'sio' supported only on SS7");
8108 /* sio coded on 1 byte so max value 255 */
8110 bpf_error("sio value %u too big; max value = 255",
8112 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8113 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8117 if (off_opc
== (u_int
)-1)
8118 bpf_error("'opc' supported only on SS7");
8119 /* opc coded on 14 bits so max value 16383 */
8121 bpf_error("opc value %u too big; max value = 16383",
8123 /* the following instructions are made to convert jvalue
8124 * to the form used to write opc in an ss7 message*/
8125 val1
= jvalue
& 0x00003c00;
8127 val2
= jvalue
& 0x000003fc;
8129 val3
= jvalue
& 0x00000003;
8131 jvalue
= val1
+ val2
+ val3
;
8132 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8133 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8137 if (off_dpc
== (u_int
)-1)
8138 bpf_error("'dpc' supported only on SS7");
8139 /* dpc coded on 14 bits so max value 16383 */
8141 bpf_error("dpc value %u too big; max value = 16383",
8143 /* the following instructions are made to convert jvalue
8144 * to the forme used to write dpc in an ss7 message*/
8145 val1
= jvalue
& 0x000000ff;
8147 val2
= jvalue
& 0x00003f00;
8149 jvalue
= val1
+ val2
;
8150 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8151 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8155 if (off_sls
== (u_int
)-1)
8156 bpf_error("'sls' supported only on SS7");
8157 /* sls coded on 4 bits so max value 15 */
8159 bpf_error("sls value %u too big; max value = 15",
8161 /* the following instruction is made to convert jvalue
8162 * to the forme used to write sls in an ss7 message*/
8163 jvalue
= jvalue
<< 4;
8164 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8165 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8174 static struct block
*
8175 gen_msg_abbrev(type
)
8181 * Q.2931 signalling protocol messages for handling virtual circuits
8182 * establishment and teardown
8187 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8191 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8195 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8199 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8203 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8206 case A_RELEASE_DONE
:
8207 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8217 gen_atmmulti_abbrev(type
)
8220 struct block
*b0
, *b1
;
8226 bpf_error("'oam' supported only on raw ATM");
8227 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8232 bpf_error("'oamf4' supported only on raw ATM");
8234 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8235 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8237 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8243 * Get Q.2931 signalling messages for switched
8244 * virtual connection
8247 bpf_error("'connectmsg' supported only on raw ATM");
8248 b0
= gen_msg_abbrev(A_SETUP
);
8249 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8251 b0
= gen_msg_abbrev(A_CONNECT
);
8253 b0
= gen_msg_abbrev(A_CONNECTACK
);
8255 b0
= gen_msg_abbrev(A_RELEASE
);
8257 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8259 b0
= gen_atmtype_abbrev(A_SC
);
8265 bpf_error("'metaconnect' supported only on raw ATM");
8266 b0
= gen_msg_abbrev(A_SETUP
);
8267 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8269 b0
= gen_msg_abbrev(A_CONNECT
);
8271 b0
= gen_msg_abbrev(A_RELEASE
);
8273 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8275 b0
= gen_atmtype_abbrev(A_METAC
);