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
);
392 OutputDebugString("Hello my dear, I locked myself\n");
394 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
396 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
402 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
403 const char *buf
, int optimize
, bpf_u_int32 mask
)
406 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
407 const char *buf
, int optimize
, bpf_u_int32 mask
)
411 const char * volatile xbuf
= buf
;
419 if (setjmp(top_ctx
)) {
433 snaplen
= pcap_snapshot(p
);
435 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
436 "snaplen of 0 rejects all packets");
440 lex_init(xbuf
? xbuf
: "");
448 root
= gen_retblk(snaplen
);
450 if (optimize
&& !no_optimize
) {
453 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
454 bpf_error("expression rejects all packets");
456 program
->bf_insns
= icode_to_fcode(root
, &len
);
457 program
->bf_len
= len
;
465 * entry point for using the compiler with no pcap open
466 * pass in all the stuff that is needed explicitly instead.
469 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
470 struct bpf_program
*program
,
471 const char *buf
, int optimize
, bpf_u_int32 mask
)
476 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
479 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
485 * Clean up a "struct bpf_program" by freeing all the memory allocated
489 pcap_freecode(struct bpf_program
*program
)
492 if (program
->bf_insns
!= NULL
) {
493 free((char *)program
->bf_insns
);
494 program
->bf_insns
= NULL
;
499 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
500 * which of the jt and jf fields has been resolved and which is a pointer
501 * back to another unresolved block (or nil). At least one of the fields
502 * in each block is already resolved.
505 backpatch(list
, target
)
506 struct block
*list
, *target
;
523 * Merge the lists in b0 and b1, using the 'sense' field to indicate
524 * which of jt and jf is the link.
528 struct block
*b0
, *b1
;
530 register struct block
**p
= &b0
;
532 /* Find end of list. */
534 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
536 /* Concatenate the lists. */
544 struct block
*ppi_dlt_check
;
547 * Insert before the statements of the first (root) block any
548 * statements needed to load the lengths of any variable-length
549 * headers into registers.
551 * XXX - a fancier strategy would be to insert those before the
552 * statements of all blocks that use those lengths and that
553 * have no predecessors that use them, so that we only compute
554 * the lengths if we need them. There might be even better
555 * approaches than that.
557 * However, those strategies would be more complicated, and
558 * as we don't generate code to compute a length if the
559 * program has no tests that use the length, and as most
560 * tests will probably use those lengths, we would just
561 * postpone computing the lengths so that it's not done
562 * for tests that fail early, and it's not clear that's
565 insert_compute_vloffsets(p
->head
);
568 * For DLT_PPI captures, generate a check of the per-packet
569 * DLT value to make sure it's DLT_IEEE802_11.
571 ppi_dlt_check
= gen_ppi_dlt_check();
572 if (ppi_dlt_check
!= NULL
)
573 gen_and(ppi_dlt_check
, p
);
575 backpatch(p
, gen_retblk(snaplen
));
576 p
->sense
= !p
->sense
;
577 backpatch(p
, gen_retblk(0));
583 struct block
*b0
, *b1
;
585 backpatch(b0
, b1
->head
);
586 b0
->sense
= !b0
->sense
;
587 b1
->sense
= !b1
->sense
;
589 b1
->sense
= !b1
->sense
;
595 struct block
*b0
, *b1
;
597 b0
->sense
= !b0
->sense
;
598 backpatch(b0
, b1
->head
);
599 b0
->sense
= !b0
->sense
;
608 b
->sense
= !b
->sense
;
611 static struct block
*
612 gen_cmp(offrel
, offset
, size
, v
)
613 enum e_offrel offrel
;
617 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
620 static struct block
*
621 gen_cmp_gt(offrel
, offset
, size
, v
)
622 enum e_offrel offrel
;
626 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
629 static struct block
*
630 gen_cmp_ge(offrel
, offset
, size
, v
)
631 enum e_offrel offrel
;
635 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
638 static struct block
*
639 gen_cmp_lt(offrel
, offset
, size
, v
)
640 enum e_offrel offrel
;
644 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
647 static struct block
*
648 gen_cmp_le(offrel
, offset
, size
, v
)
649 enum e_offrel offrel
;
653 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
656 static struct block
*
657 gen_mcmp(offrel
, offset
, size
, v
, mask
)
658 enum e_offrel offrel
;
663 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
666 static struct block
*
667 gen_bcmp(offrel
, offset
, size
, v
)
668 enum e_offrel offrel
;
669 register u_int offset
, size
;
670 register const u_char
*v
;
672 register struct block
*b
, *tmp
;
676 register const u_char
*p
= &v
[size
- 4];
677 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
678 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
680 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
687 register const u_char
*p
= &v
[size
- 2];
688 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
690 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
697 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
706 * AND the field of size "size" at offset "offset" relative to the header
707 * specified by "offrel" with "mask", and compare it with the value "v"
708 * with the test specified by "jtype"; if "reverse" is true, the test
709 * should test the opposite of "jtype".
711 static struct block
*
712 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
713 enum e_offrel offrel
;
715 bpf_u_int32 offset
, size
, mask
, jtype
;
718 struct slist
*s
, *s2
;
721 s
= gen_load_a(offrel
, offset
, size
);
723 if (mask
!= 0xffffffff) {
724 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
729 b
= new_block(JMP(jtype
));
732 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
738 * Various code constructs need to know the layout of the data link
739 * layer. These variables give the necessary offsets from the beginning
740 * of the packet data.
744 * This is the offset of the beginning of the link-layer header from
745 * the beginning of the raw packet data.
747 * It's usually 0, except for 802.11 with a fixed-length radio header.
748 * (For 802.11 with a variable-length radio header, we have to generate
749 * code to compute that offset; off_ll is 0 in that case.)
754 * If there's a variable-length header preceding the link-layer header,
755 * "reg_off_ll" is the register number for a register containing the
756 * length of that header, and therefore the offset of the link-layer
757 * header from the beginning of the raw packet data. Otherwise,
758 * "reg_off_ll" is -1.
760 static int reg_off_ll
;
763 * This is the offset of the beginning of the MAC-layer header from
764 * the beginning of the link-layer header.
765 * It's usually 0, except for ATM LANE, where it's the offset, relative
766 * to the beginning of the raw packet data, of the Ethernet header.
768 static u_int off_mac
;
771 * This is the offset of the beginning of the MAC-layer payload,
772 * from the beginning of the raw packet data.
774 * I.e., it's the sum of the length of the link-layer header (without,
775 * for example, any 802.2 LLC header, so it's the MAC-layer
776 * portion of that header), plus any prefix preceding the
779 static u_int off_macpl
;
782 * This is 1 if the offset of the beginning of the MAC-layer payload
783 * from the beginning of the link-layer header is variable-length.
785 static int off_macpl_is_variable
;
788 * If the link layer has variable_length headers, "reg_off_macpl"
789 * is the register number for a register containing the length of the
790 * link-layer header plus the length of any variable-length header
791 * preceding the link-layer header. Otherwise, "reg_off_macpl"
794 static int reg_off_macpl
;
797 * "off_linktype" is the offset to information in the link-layer header
798 * giving the packet type. This offset is relative to the beginning
799 * of the link-layer header (i.e., it doesn't include off_ll).
801 * For Ethernet, it's the offset of the Ethernet type field.
803 * For link-layer types that always use 802.2 headers, it's the
804 * offset of the LLC header.
806 * For PPP, it's the offset of the PPP type field.
808 * For Cisco HDLC, it's the offset of the CHDLC type field.
810 * For BSD loopback, it's the offset of the AF_ value.
812 * For Linux cooked sockets, it's the offset of the type field.
814 * It's set to -1 for no encapsulation, in which case, IP is assumed.
816 static u_int off_linktype
;
819 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
820 * checks to check the PPP header, assumed to follow a LAN-style link-
821 * layer header and a PPPoE session header.
823 static int is_pppoes
= 0;
826 * TRUE if the link layer includes an ATM pseudo-header.
828 static int is_atm
= 0;
831 * TRUE if "lane" appeared in the filter; it causes us to generate
832 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
834 static int is_lane
= 0;
837 * These are offsets for the ATM pseudo-header.
839 static u_int off_vpi
;
840 static u_int off_vci
;
841 static u_int off_proto
;
844 * These are offsets for the MTP2 fields.
849 * These are offsets for the MTP3 fields.
851 static u_int off_sio
;
852 static u_int off_opc
;
853 static u_int off_dpc
;
854 static u_int off_sls
;
857 * This is the offset of the first byte after the ATM pseudo_header,
858 * or -1 if there is no ATM pseudo-header.
860 static u_int off_payload
;
863 * These are offsets to the beginning of the network-layer header.
864 * They are relative to the beginning of the MAC-layer payload (i.e.,
865 * they don't include off_ll or off_macpl).
867 * If the link layer never uses 802.2 LLC:
869 * "off_nl" and "off_nl_nosnap" are the same.
871 * If the link layer always uses 802.2 LLC:
873 * "off_nl" is the offset if there's a SNAP header following
876 * "off_nl_nosnap" is the offset if there's no SNAP header.
878 * If the link layer is Ethernet:
880 * "off_nl" is the offset if the packet is an Ethernet II packet
881 * (we assume no 802.3+802.2+SNAP);
883 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
884 * with an 802.2 header following it.
887 static u_int off_nl_nosnap
;
895 linktype
= pcap_datalink(p
);
897 pcap_fddipad
= p
->fddipad
;
901 * Assume it's not raw ATM with a pseudo-header, for now.
912 * And that we're not doing PPPoE.
917 * And assume we're not doing SS7.
926 * Also assume it's not 802.11.
930 off_macpl_is_variable
= 0;
934 label_stack_depth
= 0;
944 off_nl
= 0; /* XXX in reality, variable! */
945 off_nl_nosnap
= 0; /* no 802.2 LLC */
948 case DLT_ARCNET_LINUX
:
951 off_nl
= 0; /* XXX in reality, variable! */
952 off_nl_nosnap
= 0; /* no 802.2 LLC */
957 off_macpl
= 14; /* Ethernet header length */
958 off_nl
= 0; /* Ethernet II */
959 off_nl_nosnap
= 3; /* 802.3+802.2 */
964 * SLIP doesn't have a link level type. The 16 byte
965 * header is hacked into our SLIP driver.
970 off_nl_nosnap
= 0; /* no 802.2 LLC */
974 /* XXX this may be the same as the DLT_PPP_BSDOS case */
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
987 off_nl_nosnap
= 0; /* no 802.2 LLC */
994 off_nl_nosnap
= 0; /* no 802.2 LLC */
999 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1000 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1004 off_nl_nosnap
= 0; /* no 802.2 LLC */
1009 * This does no include the Ethernet header, and
1010 * only covers session state.
1015 off_nl_nosnap
= 0; /* no 802.2 LLC */
1022 off_nl_nosnap
= 0; /* no 802.2 LLC */
1027 * FDDI doesn't really have a link-level type field.
1028 * We set "off_linktype" to the offset of the LLC header.
1030 * To check for Ethernet types, we assume that SSAP = SNAP
1031 * is being used and pick out the encapsulated Ethernet type.
1032 * XXX - should we generate code to check for SNAP?
1036 off_linktype
+= pcap_fddipad
;
1038 off_macpl
= 13; /* FDDI MAC header length */
1040 off_macpl
+= pcap_fddipad
;
1042 off_nl
= 8; /* 802.2+SNAP */
1043 off_nl_nosnap
= 3; /* 802.2 */
1048 * Token Ring doesn't really have a link-level type field.
1049 * We set "off_linktype" to the offset of the LLC header.
1051 * To check for Ethernet types, we assume that SSAP = SNAP
1052 * is being used and pick out the encapsulated Ethernet type.
1053 * XXX - should we generate code to check for SNAP?
1055 * XXX - the header is actually variable-length.
1056 * Some various Linux patched versions gave 38
1057 * as "off_linktype" and 40 as "off_nl"; however,
1058 * if a token ring packet has *no* routing
1059 * information, i.e. is not source-routed, the correct
1060 * values are 20 and 22, as they are in the vanilla code.
1062 * A packet is source-routed iff the uppermost bit
1063 * of the first byte of the source address, at an
1064 * offset of 8, has the uppermost bit set. If the
1065 * packet is source-routed, the total number of bytes
1066 * of routing information is 2 plus bits 0x1F00 of
1067 * the 16-bit value at an offset of 14 (shifted right
1068 * 8 - figure out which byte that is).
1071 off_macpl
= 14; /* Token Ring MAC header length */
1072 off_nl
= 8; /* 802.2+SNAP */
1073 off_nl_nosnap
= 3; /* 802.2 */
1076 case DLT_IEEE802_11
:
1077 case DLT_PRISM_HEADER
:
1078 case DLT_IEEE802_11_RADIO_AVS
:
1079 case DLT_IEEE802_11_RADIO
:
1081 * 802.11 doesn't really have a link-level type field.
1082 * We set "off_linktype" to the offset of the LLC header.
1084 * To check for Ethernet types, we assume that SSAP = SNAP
1085 * is being used and pick out the encapsulated Ethernet type.
1086 * XXX - should we generate code to check for SNAP?
1088 * We also handle variable-length radio headers here.
1089 * The Prism header is in theory variable-length, but in
1090 * practice it's always 144 bytes long. However, some
1091 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1092 * sometimes or always supply an AVS header, so we
1093 * have to check whether the radio header is a Prism
1094 * header or an AVS header, so, in practice, it's
1098 off_macpl
= 0; /* link-layer header is variable-length */
1099 off_macpl_is_variable
= 1;
1100 off_nl
= 8; /* 802.2+SNAP */
1101 off_nl_nosnap
= 3; /* 802.2 */
1106 * At the moment we treat PPI the same way that we treat
1107 * normal Radiotap encoded packets. The difference is in
1108 * the function that generates the code at the beginning
1109 * to compute the header length. Since this code generator
1110 * of PPI supports bare 802.11 encapsulation only (i.e.
1111 * the encapsulated DLT should be DLT_IEEE802_11) we
1112 * generate code to check for this too.
1115 off_macpl
= 0; /* link-layer header is variable-length */
1116 off_macpl_is_variable
= 1;
1117 off_nl
= 8; /* 802.2+SNAP */
1118 off_nl_nosnap
= 3; /* 802.2 */
1121 case DLT_ATM_RFC1483
:
1122 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1124 * assume routed, non-ISO PDUs
1125 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1127 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1128 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1129 * latter would presumably be treated the way PPPoE
1130 * should be, so you can do "pppoe and udp port 2049"
1131 * or "pppoa and tcp port 80" and have it check for
1132 * PPPo{A,E} and a PPP protocol of IP and....
1135 off_macpl
= 0; /* packet begins with LLC header */
1136 off_nl
= 8; /* 802.2+SNAP */
1137 off_nl_nosnap
= 3; /* 802.2 */
1142 * Full Frontal ATM; you get AALn PDUs with an ATM
1146 off_vpi
= SUNATM_VPI_POS
;
1147 off_vci
= SUNATM_VCI_POS
;
1148 off_proto
= PROTO_POS
;
1149 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1150 off_payload
= SUNATM_PKT_BEGIN_POS
;
1151 off_linktype
= off_payload
;
1152 off_macpl
= off_payload
; /* if LLC-encapsulated */
1153 off_nl
= 8; /* 802.2+SNAP */
1154 off_nl_nosnap
= 3; /* 802.2 */
1161 off_nl_nosnap
= 0; /* no 802.2 LLC */
1164 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1168 off_nl_nosnap
= 0; /* no 802.2 LLC */
1173 * LocalTalk does have a 1-byte type field in the LLAP header,
1174 * but really it just indicates whether there is a "short" or
1175 * "long" DDP packet following.
1180 off_nl_nosnap
= 0; /* no 802.2 LLC */
1183 case DLT_IP_OVER_FC
:
1185 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1186 * link-level type field. We set "off_linktype" to the
1187 * offset of the LLC header.
1189 * To check for Ethernet types, we assume that SSAP = SNAP
1190 * is being used and pick out the encapsulated Ethernet type.
1191 * XXX - should we generate code to check for SNAP? RFC
1192 * 2625 says SNAP should be used.
1196 off_nl
= 8; /* 802.2+SNAP */
1197 off_nl_nosnap
= 3; /* 802.2 */
1202 * XXX - we should set this to handle SNAP-encapsulated
1203 * frames (NLPID of 0x80).
1208 off_nl_nosnap
= 0; /* no 802.2 LLC */
1212 * the only BPF-interesting FRF.16 frames are non-control frames;
1213 * Frame Relay has a variable length link-layer
1214 * so lets start with offset 4 for now and increments later on (FIXME);
1220 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1223 case DLT_APPLE_IP_OVER_IEEE1394
:
1227 off_nl_nosnap
= 0; /* no 802.2 LLC */
1230 case DLT_LINUX_IRDA
:
1232 * Currently, only raw "link[N:M]" filtering is supported.
1242 * Currently, only raw "link[N:M]" filtering is supported.
1250 case DLT_SYMANTEC_FIREWALL
:
1253 off_nl
= 0; /* Ethernet II */
1254 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1257 #ifdef HAVE_NET_PFVAR_H
1260 off_macpl
= PFLOG_HDRLEN
;
1262 off_nl_nosnap
= 0; /* no 802.2 LLC */
1266 case DLT_JUNIPER_MFR
:
1267 case DLT_JUNIPER_MLFR
:
1268 case DLT_JUNIPER_MLPPP
:
1269 case DLT_JUNIPER_PPP
:
1270 case DLT_JUNIPER_CHDLC
:
1271 case DLT_JUNIPER_FRELAY
:
1275 off_nl_nosnap
= -1; /* no 802.2 LLC */
1278 case DLT_JUNIPER_ATM1
:
1279 off_linktype
= 4; /* in reality variable between 4-8 */
1280 off_macpl
= 4; /* in reality variable between 4-8 */
1285 case DLT_JUNIPER_ATM2
:
1286 off_linktype
= 8; /* in reality variable between 8-12 */
1287 off_macpl
= 8; /* in reality variable between 8-12 */
1292 /* frames captured on a Juniper PPPoE service PIC
1293 * contain raw ethernet frames */
1294 case DLT_JUNIPER_PPPOE
:
1295 case DLT_JUNIPER_ETHER
:
1298 off_nl
= 18; /* Ethernet II */
1299 off_nl_nosnap
= 21; /* 802.3+802.2 */
1302 case DLT_JUNIPER_PPPOE_ATM
:
1306 off_nl_nosnap
= -1; /* no 802.2 LLC */
1309 case DLT_JUNIPER_GGSN
:
1313 off_nl_nosnap
= -1; /* no 802.2 LLC */
1316 case DLT_JUNIPER_ES
:
1318 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1319 off_nl
= -1; /* not really a network layer but raw IP addresses */
1320 off_nl_nosnap
= -1; /* no 802.2 LLC */
1323 case DLT_JUNIPER_MONITOR
:
1326 off_nl
= 0; /* raw IP/IP6 header */
1327 off_nl_nosnap
= -1; /* no 802.2 LLC */
1330 case DLT_JUNIPER_SERVICES
:
1332 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1333 off_nl
= -1; /* L3 proto location dep. on cookie type */
1334 off_nl_nosnap
= -1; /* no 802.2 LLC */
1337 case DLT_JUNIPER_VP
:
1344 case DLT_JUNIPER_ST
:
1351 case DLT_JUNIPER_ISM
:
1370 case DLT_MTP2_WITH_PHDR
:
1403 case DLT_LINUX_LAPD
:
1405 * Currently, only raw "link[N:M]" filtering is supported.
1415 * Currently, only raw "link[N:M]" filtering is supported.
1423 case DLT_BLUETOOTH_HCI_H4
:
1425 * Currently, only raw "link[N:M]" filtering is supported.
1435 * Currently, only raw "link[N:M]" filtering is supported.
1445 * Currently, only raw "link[N:M]" filtering is supported.
1453 case DLT_IEEE802_15_4_LINUX
:
1455 * Currently, only raw "link[N:M]" filtering is supported.
1463 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1465 * Currently, only raw "link[N:M]" filtering is supported.
1473 case DLT_IEEE802_15_4
:
1475 * Currently, only raw "link[N:M]" filtering is supported.
1485 * Currently, only raw "link[N:M]" filtering is supported.
1495 * Currently, only raw "link[N:M]" filtering is supported.
1505 * Currently, only raw "link[N:M]" filtering is supported.
1513 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1515 * Currently, only raw "link[N:M]" filtering is supported.
1525 * Currently, only raw "link[N:M]" filtering is supported.
1527 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1529 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1530 off_nl_nosnap
= -1; /* no 802.2 LLC */
1531 off_mac
= 1; /* step over the kiss length byte */
1534 case DLT_IEEE802_15_4_NONASK_PHY
:
1536 * Currently, only raw "link[N:M]" filtering is supported.
1546 * Currently, only raw "link[N:M]" filtering is supported.
1554 case DLT_USB_LINUX_MMAPPED
:
1556 * Currently, only raw "link[N:M]" filtering is supported.
1564 bpf_error("unknown data link type %d", linktype
);
1569 * Load a value relative to the beginning of the link-layer header.
1570 * The link-layer header doesn't necessarily begin at the beginning
1571 * of the packet data; there might be a variable-length prefix containing
1572 * radio information.
1574 static struct slist
*
1575 gen_load_llrel(offset
, size
)
1578 struct slist
*s
, *s2
;
1580 s
= gen_llprefixlen();
1583 * If "s" is non-null, it has code to arrange that the X register
1584 * contains the length of the prefix preceding the link-layer
1587 * Otherwise, the length of the prefix preceding the link-layer
1588 * header is "off_ll".
1592 * There's a variable-length prefix preceding the
1593 * link-layer header. "s" points to a list of statements
1594 * that put the length of that prefix into the X register.
1595 * do an indirect load, to use the X register as an offset.
1597 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1602 * There is no variable-length header preceding the
1603 * link-layer header; add in off_ll, which, if there's
1604 * a fixed-length header preceding the link-layer header,
1605 * is the length of that header.
1607 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1608 s
->s
.k
= offset
+ off_ll
;
1614 * Load a value relative to the beginning of the MAC-layer payload.
1616 static struct slist
*
1617 gen_load_macplrel(offset
, size
)
1620 struct slist
*s
, *s2
;
1622 s
= gen_off_macpl();
1625 * If s is non-null, the offset of the MAC-layer payload is
1626 * variable, and s points to a list of instructions that
1627 * arrange that the X register contains that offset.
1629 * Otherwise, the offset of the MAC-layer payload is constant,
1630 * and is in off_macpl.
1634 * The offset of the MAC-layer payload is in the X
1635 * register. Do an indirect load, to use the X register
1638 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1643 * The offset of the MAC-layer payload is constant,
1644 * and is in off_macpl; load the value at that offset
1645 * plus the specified offset.
1647 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1648 s
->s
.k
= off_macpl
+ offset
;
1654 * Load a value relative to the beginning of the specified header.
1656 static struct slist
*
1657 gen_load_a(offrel
, offset
, size
)
1658 enum e_offrel offrel
;
1661 struct slist
*s
, *s2
;
1666 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1671 s
= gen_load_llrel(offset
, size
);
1675 s
= gen_load_macplrel(offset
, size
);
1679 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1683 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1688 * Load the X register with the length of the IPv4 header
1689 * (plus the offset of the link-layer header, if it's
1690 * preceded by a variable-length header such as a radio
1691 * header), in bytes.
1693 s
= gen_loadx_iphdrlen();
1696 * Load the item at {offset of the MAC-layer payload} +
1697 * {offset, relative to the start of the MAC-layer
1698 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1699 * {specified offset}.
1701 * (If the offset of the MAC-layer payload is variable,
1702 * it's included in the value in the X register, and
1705 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1706 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1711 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1722 * Generate code to load into the X register the sum of the length of
1723 * the IPv4 header and any variable-length header preceding the link-layer
1726 static struct slist
*
1727 gen_loadx_iphdrlen()
1729 struct slist
*s
, *s2
;
1731 s
= gen_off_macpl();
1734 * There's a variable-length prefix preceding the
1735 * link-layer header, or the link-layer header is itself
1736 * variable-length. "s" points to a list of statements
1737 * that put the offset of the MAC-layer payload into
1740 * The 4*([k]&0xf) addressing mode can't be used, as we
1741 * don't have a constant offset, so we have to load the
1742 * value in question into the A register and add to it
1743 * the value from the X register.
1745 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1748 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1751 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1756 * The A register now contains the length of the
1757 * IP header. We need to add to it the offset of
1758 * the MAC-layer payload, which is still in the X
1759 * register, and move the result into the X register.
1761 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1762 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1765 * There is no variable-length header preceding the
1766 * link-layer header, and the link-layer header is
1767 * fixed-length; load the length of the IPv4 header,
1768 * which is at an offset of off_nl from the beginning
1769 * of the MAC-layer payload, and thus at an offset
1770 * of off_mac_pl + off_nl from the beginning of the
1773 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1774 s
->s
.k
= off_macpl
+ off_nl
;
1779 static struct block
*
1786 s
= new_stmt(BPF_LD
|BPF_IMM
);
1788 b
= new_block(JMP(BPF_JEQ
));
1794 static inline struct block
*
1797 return gen_uncond(1);
1800 static inline struct block
*
1803 return gen_uncond(0);
1807 * Byte-swap a 32-bit number.
1808 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1809 * big-endian platforms.)
1811 #define SWAPLONG(y) \
1812 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1815 * Generate code to match a particular packet type.
1817 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1818 * value, if <= ETHERMTU. We use that to determine whether to
1819 * match the type/length field or to check the type/length field for
1820 * a value <= ETHERMTU to see whether it's a type field and then do
1821 * the appropriate test.
1823 static struct block
*
1824 gen_ether_linktype(proto
)
1827 struct block
*b0
, *b1
;
1833 case LLCSAP_NETBEUI
:
1835 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1836 * so we check the DSAP and SSAP.
1838 * LLCSAP_IP checks for IP-over-802.2, rather
1839 * than IP-over-Ethernet or IP-over-SNAP.
1841 * XXX - should we check both the DSAP and the
1842 * SSAP, like this, or should we check just the
1843 * DSAP, as we do for other types <= ETHERMTU
1844 * (i.e., other SAP values)?
1846 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1848 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1849 ((proto
<< 8) | proto
));
1857 * Ethernet_II frames, which are Ethernet
1858 * frames with a frame type of ETHERTYPE_IPX;
1860 * Ethernet_802.3 frames, which are 802.3
1861 * frames (i.e., the type/length field is
1862 * a length field, <= ETHERMTU, rather than
1863 * a type field) with the first two bytes
1864 * after the Ethernet/802.3 header being
1867 * Ethernet_802.2 frames, which are 802.3
1868 * frames with an 802.2 LLC header and
1869 * with the IPX LSAP as the DSAP in the LLC
1872 * Ethernet_SNAP frames, which are 802.3
1873 * frames with an LLC header and a SNAP
1874 * header and with an OUI of 0x000000
1875 * (encapsulated Ethernet) and a protocol
1876 * ID of ETHERTYPE_IPX in the SNAP header.
1878 * XXX - should we generate the same code both
1879 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1883 * This generates code to check both for the
1884 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1886 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1887 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1891 * Now we add code to check for SNAP frames with
1892 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1894 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1898 * Now we generate code to check for 802.3
1899 * frames in general.
1901 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1905 * Now add the check for 802.3 frames before the
1906 * check for Ethernet_802.2 and Ethernet_802.3,
1907 * as those checks should only be done on 802.3
1908 * frames, not on Ethernet frames.
1913 * Now add the check for Ethernet_II frames, and
1914 * do that before checking for the other frame
1917 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1918 (bpf_int32
)ETHERTYPE_IPX
);
1922 case ETHERTYPE_ATALK
:
1923 case ETHERTYPE_AARP
:
1925 * EtherTalk (AppleTalk protocols on Ethernet link
1926 * layer) may use 802.2 encapsulation.
1930 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1931 * we check for an Ethernet type field less than
1932 * 1500, which means it's an 802.3 length field.
1934 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1938 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1939 * SNAP packets with an organization code of
1940 * 0x080007 (Apple, for Appletalk) and a protocol
1941 * type of ETHERTYPE_ATALK (Appletalk).
1943 * 802.2-encapsulated ETHERTYPE_AARP packets are
1944 * SNAP packets with an organization code of
1945 * 0x000000 (encapsulated Ethernet) and a protocol
1946 * type of ETHERTYPE_AARP (Appletalk ARP).
1948 if (proto
== ETHERTYPE_ATALK
)
1949 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1950 else /* proto == ETHERTYPE_AARP */
1951 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1955 * Check for Ethernet encapsulation (Ethertalk
1956 * phase 1?); we just check for the Ethernet
1959 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1965 if (proto
<= ETHERMTU
) {
1967 * This is an LLC SAP value, so the frames
1968 * that match would be 802.2 frames.
1969 * Check that the frame is an 802.2 frame
1970 * (i.e., that the length/type field is
1971 * a length field, <= ETHERMTU) and
1972 * then check the DSAP.
1974 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1976 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1982 * This is an Ethernet type, so compare
1983 * the length/type field with it (if
1984 * the frame is an 802.2 frame, the length
1985 * field will be <= ETHERMTU, and, as
1986 * "proto" is > ETHERMTU, this test
1987 * will fail and the frame won't match,
1988 * which is what we want).
1990 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1997 * Generate code to match a particular packet type.
1999 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2000 * value, if <= ETHERMTU. We use that to determine whether to
2001 * match the type field or to check the type field for the special
2002 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2004 static struct block
*
2005 gen_linux_sll_linktype(proto
)
2008 struct block
*b0
, *b1
;
2014 case LLCSAP_NETBEUI
:
2016 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2017 * so we check the DSAP and SSAP.
2019 * LLCSAP_IP checks for IP-over-802.2, rather
2020 * than IP-over-Ethernet or IP-over-SNAP.
2022 * XXX - should we check both the DSAP and the
2023 * SSAP, like this, or should we check just the
2024 * DSAP, as we do for other types <= ETHERMTU
2025 * (i.e., other SAP values)?
2027 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2028 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2029 ((proto
<< 8) | proto
));
2035 * Ethernet_II frames, which are Ethernet
2036 * frames with a frame type of ETHERTYPE_IPX;
2038 * Ethernet_802.3 frames, which have a frame
2039 * type of LINUX_SLL_P_802_3;
2041 * Ethernet_802.2 frames, which are 802.3
2042 * frames with an 802.2 LLC header (i.e, have
2043 * a frame type of LINUX_SLL_P_802_2) and
2044 * with the IPX LSAP as the DSAP in the LLC
2047 * Ethernet_SNAP frames, which are 802.3
2048 * frames with an LLC header and a SNAP
2049 * header and with an OUI of 0x000000
2050 * (encapsulated Ethernet) and a protocol
2051 * ID of ETHERTYPE_IPX in the SNAP header.
2053 * First, do the checks on LINUX_SLL_P_802_2
2054 * frames; generate the check for either
2055 * Ethernet_802.2 or Ethernet_SNAP frames, and
2056 * then put a check for LINUX_SLL_P_802_2 frames
2059 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2060 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2062 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2066 * Now check for 802.3 frames and OR that with
2067 * the previous test.
2069 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2073 * Now add the check for Ethernet_II frames, and
2074 * do that before checking for the other frame
2077 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2078 (bpf_int32
)ETHERTYPE_IPX
);
2082 case ETHERTYPE_ATALK
:
2083 case ETHERTYPE_AARP
:
2085 * EtherTalk (AppleTalk protocols on Ethernet link
2086 * layer) may use 802.2 encapsulation.
2090 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2091 * we check for the 802.2 protocol type in the
2092 * "Ethernet type" field.
2094 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2097 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2098 * SNAP packets with an organization code of
2099 * 0x080007 (Apple, for Appletalk) and a protocol
2100 * type of ETHERTYPE_ATALK (Appletalk).
2102 * 802.2-encapsulated ETHERTYPE_AARP packets are
2103 * SNAP packets with an organization code of
2104 * 0x000000 (encapsulated Ethernet) and a protocol
2105 * type of ETHERTYPE_AARP (Appletalk ARP).
2107 if (proto
== ETHERTYPE_ATALK
)
2108 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2109 else /* proto == ETHERTYPE_AARP */
2110 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2114 * Check for Ethernet encapsulation (Ethertalk
2115 * phase 1?); we just check for the Ethernet
2118 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2124 if (proto
<= ETHERMTU
) {
2126 * This is an LLC SAP value, so the frames
2127 * that match would be 802.2 frames.
2128 * Check for the 802.2 protocol type
2129 * in the "Ethernet type" field, and
2130 * then check the DSAP.
2132 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2134 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2140 * This is an Ethernet type, so compare
2141 * the length/type field with it (if
2142 * the frame is an 802.2 frame, the length
2143 * field will be <= ETHERMTU, and, as
2144 * "proto" is > ETHERMTU, this test
2145 * will fail and the frame won't match,
2146 * which is what we want).
2148 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2154 static struct slist
*
2155 gen_load_prism_llprefixlen()
2157 struct slist
*s1
, *s2
;
2158 struct slist
*sjeq_avs_cookie
;
2159 struct slist
*sjcommon
;
2162 * This code is not compatible with the optimizer, as
2163 * we are generating jmp instructions within a normal
2164 * slist of instructions
2169 * Generate code to load the length of the radio header into
2170 * the register assigned to hold that length, if one has been
2171 * assigned. (If one hasn't been assigned, no code we've
2172 * generated uses that prefix, so we don't need to generate any
2175 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2176 * or always use the AVS header rather than the Prism header.
2177 * We load a 4-byte big-endian value at the beginning of the
2178 * raw packet data, and see whether, when masked with 0xFFFFF000,
2179 * it's equal to 0x80211000. If so, that indicates that it's
2180 * an AVS header (the masked-out bits are the version number).
2181 * Otherwise, it's a Prism header.
2183 * XXX - the Prism header is also, in theory, variable-length,
2184 * but no known software generates headers that aren't 144
2187 if (reg_off_ll
!= -1) {
2191 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2195 * AND it with 0xFFFFF000.
2197 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2198 s2
->s
.k
= 0xFFFFF000;
2202 * Compare with 0x80211000.
2204 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2205 sjeq_avs_cookie
->s
.k
= 0x80211000;
2206 sappend(s1
, sjeq_avs_cookie
);
2211 * The 4 bytes at an offset of 4 from the beginning of
2212 * the AVS header are the length of the AVS header.
2213 * That field is big-endian.
2215 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2218 sjeq_avs_cookie
->s
.jt
= s2
;
2221 * Now jump to the code to allocate a register
2222 * into which to save the header length and
2223 * store the length there. (The "jump always"
2224 * instruction needs to have the k field set;
2225 * it's added to the PC, so, as we're jumping
2226 * over a single instruction, it should be 1.)
2228 sjcommon
= new_stmt(JMP(BPF_JA
));
2230 sappend(s1
, sjcommon
);
2233 * Now for the code that handles the Prism header.
2234 * Just load the length of the Prism header (144)
2235 * into the A register. Have the test for an AVS
2236 * header branch here if we don't have an AVS header.
2238 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2241 sjeq_avs_cookie
->s
.jf
= s2
;
2244 * Now allocate a register to hold that value and store
2245 * it. The code for the AVS header will jump here after
2246 * loading the length of the AVS header.
2248 s2
= new_stmt(BPF_ST
);
2249 s2
->s
.k
= reg_off_ll
;
2251 sjcommon
->s
.jf
= s2
;
2254 * Now move it into the X register.
2256 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2264 static struct slist
*
2265 gen_load_avs_llprefixlen()
2267 struct slist
*s1
, *s2
;
2270 * Generate code to load the length of the AVS header into
2271 * the register assigned to hold that length, if one has been
2272 * assigned. (If one hasn't been assigned, no code we've
2273 * generated uses that prefix, so we don't need to generate any
2276 if (reg_off_ll
!= -1) {
2278 * The 4 bytes at an offset of 4 from the beginning of
2279 * the AVS header are the length of the AVS header.
2280 * That field is big-endian.
2282 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2286 * Now allocate a register to hold that value and store
2289 s2
= new_stmt(BPF_ST
);
2290 s2
->s
.k
= reg_off_ll
;
2294 * Now move it into the X register.
2296 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2304 static struct slist
*
2305 gen_load_radiotap_llprefixlen()
2307 struct slist
*s1
, *s2
;
2310 * Generate code to load the length of the radiotap header into
2311 * the register assigned to hold that length, if one has been
2312 * assigned. (If one hasn't been assigned, no code we've
2313 * generated uses that prefix, so we don't need to generate any
2316 if (reg_off_ll
!= -1) {
2318 * The 2 bytes at offsets of 2 and 3 from the beginning
2319 * of the radiotap header are the length of the radiotap
2320 * header; unfortunately, it's little-endian, so we have
2321 * to load it a byte at a time and construct the value.
2325 * Load the high-order byte, at an offset of 3, shift it
2326 * left a byte, and put the result in the X register.
2328 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2330 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2333 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2337 * Load the next byte, at an offset of 2, and OR the
2338 * value from the X register into it.
2340 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2343 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2347 * Now allocate a register to hold that value and store
2350 s2
= new_stmt(BPF_ST
);
2351 s2
->s
.k
= reg_off_ll
;
2355 * Now move it into the X register.
2357 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2366 * At the moment we treat PPI as normal Radiotap encoded
2367 * packets. The difference is in the function that generates
2368 * the code at the beginning to compute the header length.
2369 * Since this code generator of PPI supports bare 802.11
2370 * encapsulation only (i.e. the encapsulated DLT should be
2371 * DLT_IEEE802_11) we generate code to check for this too;
2372 * that's done in finish_parse().
2374 static struct slist
*
2375 gen_load_ppi_llprefixlen()
2377 struct slist
*s1
, *s2
;
2380 * Generate code to load the length of the radiotap header
2381 * into the register assigned to hold that length, if one has
2384 if (reg_off_ll
!= -1) {
2386 * The 2 bytes at offsets of 2 and 3 from the beginning
2387 * of the radiotap header are the length of the radiotap
2388 * header; unfortunately, it's little-endian, so we have
2389 * to load it a byte at a time and construct the value.
2393 * Load the high-order byte, at an offset of 3, shift it
2394 * left a byte, and put the result in the X register.
2396 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2398 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2401 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2405 * Load the next byte, at an offset of 2, and OR the
2406 * value from the X register into it.
2408 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2411 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2415 * Now allocate a register to hold that value and store
2418 s2
= new_stmt(BPF_ST
);
2419 s2
->s
.k
= reg_off_ll
;
2423 * Now move it into the X register.
2425 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2434 * Load a value relative to the beginning of the link-layer header after the 802.11
2435 * header, i.e. LLC_SNAP.
2436 * The link-layer header doesn't necessarily begin at the beginning
2437 * of the packet data; there might be a variable-length prefix containing
2438 * radio information.
2440 static struct slist
*
2441 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2444 struct slist
*sjset_data_frame_1
;
2445 struct slist
*sjset_data_frame_2
;
2446 struct slist
*sjset_qos
;
2447 struct slist
*sjset_radiotap_flags
;
2448 struct slist
*sjset_radiotap_tsft
;
2449 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2450 struct slist
*s_roundup
;
2452 if (reg_off_macpl
== -1) {
2454 * No register has been assigned to the offset of
2455 * the MAC-layer payload, which means nobody needs
2456 * it; don't bother computing it - just return
2457 * what we already have.
2463 * This code is not compatible with the optimizer, as
2464 * we are generating jmp instructions within a normal
2465 * slist of instructions
2470 * If "s" is non-null, it has code to arrange that the X register
2471 * contains the length of the prefix preceding the link-layer
2474 * Otherwise, the length of the prefix preceding the link-layer
2475 * header is "off_ll".
2479 * There is no variable-length header preceding the
2480 * link-layer header.
2482 * Load the length of the fixed-length prefix preceding
2483 * the link-layer header (if any) into the X register,
2484 * and store it in the reg_off_macpl register.
2485 * That length is off_ll.
2487 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2492 * The X register contains the offset of the beginning of the
2493 * link-layer header; add 24, which is the minimum length
2494 * of the MAC header for a data frame, to that, and store it
2495 * in reg_off_macpl, and then load the Frame Control field,
2496 * which is at the offset in the X register, with an indexed load.
2498 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2500 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2503 s2
= new_stmt(BPF_ST
);
2504 s2
->s
.k
= reg_off_macpl
;
2507 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2512 * Check the Frame Control field to see if this is a data frame;
2513 * a data frame has the 0x08 bit (b3) in that field set and the
2514 * 0x04 bit (b2) clear.
2516 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2517 sjset_data_frame_1
->s
.k
= 0x08;
2518 sappend(s
, sjset_data_frame_1
);
2521 * If b3 is set, test b2, otherwise go to the first statement of
2522 * the rest of the program.
2524 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2525 sjset_data_frame_2
->s
.k
= 0x04;
2526 sappend(s
, sjset_data_frame_2
);
2527 sjset_data_frame_1
->s
.jf
= snext
;
2530 * If b2 is not set, this is a data frame; test the QoS bit.
2531 * Otherwise, go to the first statement of the rest of the
2534 sjset_data_frame_2
->s
.jt
= snext
;
2535 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2536 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2537 sappend(s
, sjset_qos
);
2540 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2542 * Otherwise, go to the first statement of the rest of the
2545 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2546 s2
->s
.k
= reg_off_macpl
;
2548 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2551 s2
= new_stmt(BPF_ST
);
2552 s2
->s
.k
= reg_off_macpl
;
2556 * If we have a radiotap header, look at it to see whether
2557 * there's Atheros padding between the MAC-layer header
2560 * Note: all of the fields in the radiotap header are
2561 * little-endian, so we byte-swap all of the values
2562 * we test against, as they will be loaded as big-endian
2565 if (linktype
== DLT_IEEE802_11_RADIO
) {
2567 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2568 * in the presence flag?
2570 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2574 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2575 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2576 sappend(s
, sjset_radiotap_flags
);
2579 * If not, skip all of this.
2581 sjset_radiotap_flags
->s
.jf
= snext
;
2584 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2586 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2587 new_stmt(JMP(BPF_JSET
));
2588 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2589 sappend(s
, sjset_radiotap_tsft
);
2592 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2593 * at an offset of 16 from the beginning of the raw packet
2594 * data (8 bytes for the radiotap header and 8 bytes for
2597 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2600 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2604 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2605 sjset_tsft_datapad
->s
.k
= 0x20;
2606 sappend(s
, sjset_tsft_datapad
);
2609 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2610 * at an offset of 8 from the beginning of the raw packet
2611 * data (8 bytes for the radiotap header).
2613 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2616 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2620 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2621 sjset_notsft_datapad
->s
.k
= 0x20;
2622 sappend(s
, sjset_notsft_datapad
);
2625 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2626 * set, round the length of the 802.11 header to
2627 * a multiple of 4. Do that by adding 3 and then
2628 * dividing by and multiplying by 4, which we do by
2631 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2632 s_roundup
->s
.k
= reg_off_macpl
;
2633 sappend(s
, s_roundup
);
2634 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2637 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2640 s2
= new_stmt(BPF_ST
);
2641 s2
->s
.k
= reg_off_macpl
;
2644 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2645 sjset_tsft_datapad
->s
.jf
= snext
;
2646 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2647 sjset_notsft_datapad
->s
.jf
= snext
;
2649 sjset_qos
->s
.jf
= snext
;
2655 insert_compute_vloffsets(b
)
2661 * For link-layer types that have a variable-length header
2662 * preceding the link-layer header, generate code to load
2663 * the offset of the link-layer header into the register
2664 * assigned to that offset, if any.
2668 case DLT_PRISM_HEADER
:
2669 s
= gen_load_prism_llprefixlen();
2672 case DLT_IEEE802_11_RADIO_AVS
:
2673 s
= gen_load_avs_llprefixlen();
2676 case DLT_IEEE802_11_RADIO
:
2677 s
= gen_load_radiotap_llprefixlen();
2681 s
= gen_load_ppi_llprefixlen();
2690 * For link-layer types that have a variable-length link-layer
2691 * header, generate code to load the offset of the MAC-layer
2692 * payload into the register assigned to that offset, if any.
2696 case DLT_IEEE802_11
:
2697 case DLT_PRISM_HEADER
:
2698 case DLT_IEEE802_11_RADIO_AVS
:
2699 case DLT_IEEE802_11_RADIO
:
2701 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2706 * If we have any offset-loading code, append all the
2707 * existing statements in the block to those statements,
2708 * and make the resulting list the list of statements
2712 sappend(s
, b
->stmts
);
2717 static struct block
*
2718 gen_ppi_dlt_check(void)
2720 struct slist
*s_load_dlt
;
2723 if (linktype
== DLT_PPI
)
2725 /* Create the statements that check for the DLT
2727 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2728 s_load_dlt
->s
.k
= 4;
2730 b
= new_block(JMP(BPF_JEQ
));
2732 b
->stmts
= s_load_dlt
;
2733 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2743 static struct slist
*
2744 gen_prism_llprefixlen(void)
2748 if (reg_off_ll
== -1) {
2750 * We haven't yet assigned a register for the length
2751 * of the radio header; allocate one.
2753 reg_off_ll
= alloc_reg();
2757 * Load the register containing the radio length
2758 * into the X register.
2760 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2761 s
->s
.k
= reg_off_ll
;
2765 static struct slist
*
2766 gen_avs_llprefixlen(void)
2770 if (reg_off_ll
== -1) {
2772 * We haven't yet assigned a register for the length
2773 * of the AVS header; allocate one.
2775 reg_off_ll
= alloc_reg();
2779 * Load the register containing the AVS length
2780 * into the X register.
2782 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2783 s
->s
.k
= reg_off_ll
;
2787 static struct slist
*
2788 gen_radiotap_llprefixlen(void)
2792 if (reg_off_ll
== -1) {
2794 * We haven't yet assigned a register for the length
2795 * of the radiotap header; allocate one.
2797 reg_off_ll
= alloc_reg();
2801 * Load the register containing the radiotap length
2802 * into the X register.
2804 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2805 s
->s
.k
= reg_off_ll
;
2810 * At the moment we treat PPI as normal Radiotap encoded
2811 * packets. The difference is in the function that generates
2812 * the code at the beginning to compute the header length.
2813 * Since this code generator of PPI supports bare 802.11
2814 * encapsulation only (i.e. the encapsulated DLT should be
2815 * DLT_IEEE802_11) we generate code to check for this too.
2817 static struct slist
*
2818 gen_ppi_llprefixlen(void)
2822 if (reg_off_ll
== -1) {
2824 * We haven't yet assigned a register for the length
2825 * of the radiotap header; allocate one.
2827 reg_off_ll
= alloc_reg();
2831 * Load the register containing the PPI length
2832 * into the X register.
2834 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2835 s
->s
.k
= reg_off_ll
;
2840 * Generate code to compute the link-layer header length, if necessary,
2841 * putting it into the X register, and to return either a pointer to a
2842 * "struct slist" for the list of statements in that code, or NULL if
2843 * no code is necessary.
2845 static struct slist
*
2846 gen_llprefixlen(void)
2850 case DLT_PRISM_HEADER
:
2851 return gen_prism_llprefixlen();
2853 case DLT_IEEE802_11_RADIO_AVS
:
2854 return gen_avs_llprefixlen();
2856 case DLT_IEEE802_11_RADIO
:
2857 return gen_radiotap_llprefixlen();
2860 return gen_ppi_llprefixlen();
2868 * Generate code to load the register containing the offset of the
2869 * MAC-layer payload into the X register; if no register for that offset
2870 * has been allocated, allocate it first.
2872 static struct slist
*
2877 if (off_macpl_is_variable
) {
2878 if (reg_off_macpl
== -1) {
2880 * We haven't yet assigned a register for the offset
2881 * of the MAC-layer payload; allocate one.
2883 reg_off_macpl
= alloc_reg();
2887 * Load the register containing the offset of the MAC-layer
2888 * payload into the X register.
2890 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2891 s
->s
.k
= reg_off_macpl
;
2895 * That offset isn't variable, so we don't need to
2896 * generate any code.
2903 * Map an Ethernet type to the equivalent PPP type.
2906 ethertype_to_ppptype(proto
)
2916 case ETHERTYPE_IPV6
:
2925 case ETHERTYPE_ATALK
:
2939 * I'm assuming the "Bridging PDU"s that go
2940 * over PPP are Spanning Tree Protocol
2954 * Generate code to match a particular packet type by matching the
2955 * link-layer type field or fields in the 802.2 LLC header.
2957 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2958 * value, if <= ETHERMTU.
2960 static struct block
*
2964 struct block
*b0
, *b1
, *b2
;
2966 /* are we checking MPLS-encapsulated packets? */
2967 if (label_stack_depth
> 0) {
2971 /* FIXME add other L3 proto IDs */
2972 return gen_mpls_linktype(Q_IP
);
2974 case ETHERTYPE_IPV6
:
2976 /* FIXME add other L3 proto IDs */
2977 return gen_mpls_linktype(Q_IPV6
);
2980 bpf_error("unsupported protocol over mpls");
2986 * Are we testing PPPoE packets?
2990 * The PPPoE session header is part of the
2991 * MAC-layer payload, so all references
2992 * should be relative to the beginning of
2997 * We use Ethernet protocol types inside libpcap;
2998 * map them to the corresponding PPP protocol types.
3000 proto
= ethertype_to_ppptype(proto
);
3001 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3007 return gen_ether_linktype(proto
);
3015 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3019 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3026 case DLT_IEEE802_11
:
3027 case DLT_PRISM_HEADER
:
3028 case DLT_IEEE802_11_RADIO_AVS
:
3029 case DLT_IEEE802_11_RADIO
:
3032 * Check that we have a data frame.
3034 b0
= gen_check_802_11_data_frame();
3037 * Now check for the specified link-layer type.
3039 b1
= gen_llc_linktype(proto
);
3047 * XXX - check for asynchronous frames, as per RFC 1103.
3049 return gen_llc_linktype(proto
);
3055 * XXX - check for LLC PDUs, as per IEEE 802.5.
3057 return gen_llc_linktype(proto
);
3061 case DLT_ATM_RFC1483
:
3063 case DLT_IP_OVER_FC
:
3064 return gen_llc_linktype(proto
);
3070 * If "is_lane" is set, check for a LANE-encapsulated
3071 * version of this protocol, otherwise check for an
3072 * LLC-encapsulated version of this protocol.
3074 * We assume LANE means Ethernet, not Token Ring.
3078 * Check that the packet doesn't begin with an
3079 * LE Control marker. (We've already generated
3082 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3087 * Now generate an Ethernet test.
3089 b1
= gen_ether_linktype(proto
);
3094 * Check for LLC encapsulation and then check the
3097 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3098 b1
= gen_llc_linktype(proto
);
3106 return gen_linux_sll_linktype(proto
);
3111 case DLT_SLIP_BSDOS
:
3114 * These types don't provide any type field; packets
3115 * are always IPv4 or IPv6.
3117 * XXX - for IPv4, check for a version number of 4, and,
3118 * for IPv6, check for a version number of 6?
3123 /* Check for a version number of 4. */
3124 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3126 case ETHERTYPE_IPV6
:
3127 /* Check for a version number of 6. */
3128 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3132 return gen_false(); /* always false */
3139 case DLT_PPP_SERIAL
:
3142 * We use Ethernet protocol types inside libpcap;
3143 * map them to the corresponding PPP protocol types.
3145 proto
= ethertype_to_ppptype(proto
);
3146 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3152 * We use Ethernet protocol types inside libpcap;
3153 * map them to the corresponding PPP protocol types.
3159 * Also check for Van Jacobson-compressed IP.
3160 * XXX - do this for other forms of PPP?
3162 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3163 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3165 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3170 proto
= ethertype_to_ppptype(proto
);
3171 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3181 * For DLT_NULL, the link-layer header is a 32-bit
3182 * word containing an AF_ value in *host* byte order,
3183 * and for DLT_ENC, the link-layer header begins
3184 * with a 32-bit work containing an AF_ value in
3187 * In addition, if we're reading a saved capture file,
3188 * the host byte order in the capture may not be the
3189 * same as the host byte order on this machine.
3191 * For DLT_LOOP, the link-layer header is a 32-bit
3192 * word containing an AF_ value in *network* byte order.
3194 * XXX - AF_ values may, unfortunately, be platform-
3195 * dependent; for example, FreeBSD's AF_INET6 is 24
3196 * whilst NetBSD's and OpenBSD's is 26.
3198 * This means that, when reading a capture file, just
3199 * checking for our AF_INET6 value won't work if the
3200 * capture file came from another OS.
3209 case ETHERTYPE_IPV6
:
3216 * Not a type on which we support filtering.
3217 * XXX - support those that have AF_ values
3218 * #defined on this platform, at least?
3223 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3225 * The AF_ value is in host byte order, but
3226 * the BPF interpreter will convert it to
3227 * network byte order.
3229 * If this is a save file, and it's from a
3230 * machine with the opposite byte order to
3231 * ours, we byte-swap the AF_ value.
3233 * Then we run it through "htonl()", and
3234 * generate code to compare against the result.
3236 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3237 bpf_pcap
->sf
.swapped
)
3238 proto
= SWAPLONG(proto
);
3239 proto
= htonl(proto
);
3241 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3243 #ifdef HAVE_NET_PFVAR_H
3246 * af field is host byte order in contrast to the rest of
3249 if (proto
== ETHERTYPE_IP
)
3250 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3251 BPF_B
, (bpf_int32
)AF_INET
));
3253 else if (proto
== ETHERTYPE_IPV6
)
3254 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3255 BPF_B
, (bpf_int32
)AF_INET6
));
3261 #endif /* HAVE_NET_PFVAR_H */
3264 case DLT_ARCNET_LINUX
:
3266 * XXX should we check for first fragment if the protocol
3275 case ETHERTYPE_IPV6
:
3276 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3277 (bpf_int32
)ARCTYPE_INET6
));
3281 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3282 (bpf_int32
)ARCTYPE_IP
);
3283 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3284 (bpf_int32
)ARCTYPE_IP_OLD
);
3289 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3290 (bpf_int32
)ARCTYPE_ARP
);
3291 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3292 (bpf_int32
)ARCTYPE_ARP_OLD
);
3296 case ETHERTYPE_REVARP
:
3297 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3298 (bpf_int32
)ARCTYPE_REVARP
));
3300 case ETHERTYPE_ATALK
:
3301 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3302 (bpf_int32
)ARCTYPE_ATALK
));
3309 case ETHERTYPE_ATALK
:
3319 * XXX - assumes a 2-byte Frame Relay header with
3320 * DLCI and flags. What if the address is longer?
3326 * Check for the special NLPID for IP.
3328 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3331 case ETHERTYPE_IPV6
:
3333 * Check for the special NLPID for IPv6.
3335 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3340 * Check for several OSI protocols.
3342 * Frame Relay packets typically have an OSI
3343 * NLPID at the beginning; we check for each
3346 * What we check for is the NLPID and a frame
3347 * control field of UI, i.e. 0x03 followed
3350 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3351 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3352 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3364 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3366 case DLT_JUNIPER_MFR
:
3367 case DLT_JUNIPER_MLFR
:
3368 case DLT_JUNIPER_MLPPP
:
3369 case DLT_JUNIPER_ATM1
:
3370 case DLT_JUNIPER_ATM2
:
3371 case DLT_JUNIPER_PPPOE
:
3372 case DLT_JUNIPER_PPPOE_ATM
:
3373 case DLT_JUNIPER_GGSN
:
3374 case DLT_JUNIPER_ES
:
3375 case DLT_JUNIPER_MONITOR
:
3376 case DLT_JUNIPER_SERVICES
:
3377 case DLT_JUNIPER_ETHER
:
3378 case DLT_JUNIPER_PPP
:
3379 case DLT_JUNIPER_FRELAY
:
3380 case DLT_JUNIPER_CHDLC
:
3381 case DLT_JUNIPER_VP
:
3382 case DLT_JUNIPER_ST
:
3383 case DLT_JUNIPER_ISM
:
3384 /* just lets verify the magic number for now -
3385 * on ATM we may have up to 6 different encapsulations on the wire
3386 * and need a lot of heuristics to figure out that the payload
3389 * FIXME encapsulation specific BPF_ filters
3391 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3393 case DLT_LINUX_IRDA
:
3394 bpf_error("IrDA link-layer type filtering not implemented");
3397 bpf_error("DOCSIS link-layer type filtering not implemented");
3400 case DLT_MTP2_WITH_PHDR
:
3401 bpf_error("MTP2 link-layer type filtering not implemented");
3404 bpf_error("ERF link-layer type filtering not implemented");
3408 bpf_error("PFSYNC link-layer type filtering not implemented");
3411 case DLT_LINUX_LAPD
:
3412 bpf_error("LAPD link-layer type filtering not implemented");
3416 case DLT_USB_LINUX_MMAPPED
:
3417 bpf_error("USB link-layer type filtering not implemented");
3419 case DLT_BLUETOOTH_HCI_H4
:
3420 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3421 bpf_error("Bluetooth link-layer type filtering not implemented");
3424 bpf_error("CAN20B link-layer type filtering not implemented");
3426 case DLT_IEEE802_15_4
:
3427 case DLT_IEEE802_15_4_LINUX
:
3428 case DLT_IEEE802_15_4_NONASK_PHY
:
3429 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3431 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3432 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3435 bpf_error("SITA link-layer type filtering not implemented");
3438 bpf_error("RAIF1 link-layer type filtering not implemented");
3441 bpf_error("IPMB link-layer type filtering not implemented");
3444 bpf_error("AX.25 link-layer type filtering not implemented");
3448 * All the types that have no encapsulation should either be
3449 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3450 * all packets are IP packets, or should be handled in some
3451 * special case, if none of them are (if some are and some
3452 * aren't, the lack of encapsulation is a problem, as we'd
3453 * have to find some other way of determining the packet type).
3455 * Therefore, if "off_linktype" is -1, there's an error.
3457 if (off_linktype
== (u_int
)-1)
3461 * Any type not handled above should always have an Ethernet
3462 * type at an offset of "off_linktype".
3464 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3468 * Check for an LLC SNAP packet with a given organization code and
3469 * protocol type; we check the entire contents of the 802.2 LLC and
3470 * snap headers, checking for DSAP and SSAP of SNAP and a control
3471 * field of 0x03 in the LLC header, and for the specified organization
3472 * code and protocol type in the SNAP header.
3474 static struct block
*
3475 gen_snap(orgcode
, ptype
)
3476 bpf_u_int32 orgcode
;
3479 u_char snapblock
[8];
3481 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3482 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3483 snapblock
[2] = 0x03; /* control = UI */
3484 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3485 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3486 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3487 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3488 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3489 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3493 * Generate code to match a particular packet type, for link-layer types
3494 * using 802.2 LLC headers.
3496 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3497 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3499 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3500 * value, if <= ETHERMTU. We use that to determine whether to
3501 * match the DSAP or both DSAP and LSAP or to check the OUI and
3502 * protocol ID in a SNAP header.
3504 static struct block
*
3505 gen_llc_linktype(proto
)
3509 * XXX - handle token-ring variable-length header.
3515 case LLCSAP_NETBEUI
:
3517 * XXX - should we check both the DSAP and the
3518 * SSAP, like this, or should we check just the
3519 * DSAP, as we do for other types <= ETHERMTU
3520 * (i.e., other SAP values)?
3522 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3523 ((proto
<< 8) | proto
));
3527 * XXX - are there ever SNAP frames for IPX on
3528 * non-Ethernet 802.x networks?
3530 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3531 (bpf_int32
)LLCSAP_IPX
);
3533 case ETHERTYPE_ATALK
:
3535 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3536 * SNAP packets with an organization code of
3537 * 0x080007 (Apple, for Appletalk) and a protocol
3538 * type of ETHERTYPE_ATALK (Appletalk).
3540 * XXX - check for an organization code of
3541 * encapsulated Ethernet as well?
3543 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3547 * XXX - we don't have to check for IPX 802.3
3548 * here, but should we check for the IPX Ethertype?
3550 if (proto
<= ETHERMTU
) {
3552 * This is an LLC SAP value, so check
3555 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3558 * This is an Ethernet type; we assume that it's
3559 * unlikely that it'll appear in the right place
3560 * at random, and therefore check only the
3561 * location that would hold the Ethernet type
3562 * in a SNAP frame with an organization code of
3563 * 0x000000 (encapsulated Ethernet).
3565 * XXX - if we were to check for the SNAP DSAP and
3566 * LSAP, as per XXX, and were also to check for an
3567 * organization code of 0x000000 (encapsulated
3568 * Ethernet), we'd do
3570 * return gen_snap(0x000000, proto);
3572 * here; for now, we don't, as per the above.
3573 * I don't know whether it's worth the extra CPU
3574 * time to do the right check or not.
3576 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3581 static struct block
*
3582 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3586 u_int src_off
, dst_off
;
3588 struct block
*b0
, *b1
;
3602 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3603 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3609 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3610 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3617 b0
= gen_linktype(proto
);
3618 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3624 static struct block
*
3625 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3626 struct in6_addr
*addr
;
3627 struct in6_addr
*mask
;
3629 u_int src_off
, dst_off
;
3631 struct block
*b0
, *b1
;
3646 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3647 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3653 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3654 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3661 /* this order is important */
3662 a
= (u_int32_t
*)addr
;
3663 m
= (u_int32_t
*)mask
;
3664 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3665 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3667 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3669 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3671 b0
= gen_linktype(proto
);
3677 static struct block
*
3678 gen_ehostop(eaddr
, dir
)
3679 register const u_char
*eaddr
;
3682 register struct block
*b0
, *b1
;
3686 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3689 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3692 b0
= gen_ehostop(eaddr
, Q_SRC
);
3693 b1
= gen_ehostop(eaddr
, Q_DST
);
3699 b0
= gen_ehostop(eaddr
, Q_SRC
);
3700 b1
= gen_ehostop(eaddr
, Q_DST
);
3709 * Like gen_ehostop, but for DLT_FDDI
3711 static struct block
*
3712 gen_fhostop(eaddr
, dir
)
3713 register const u_char
*eaddr
;
3716 struct block
*b0
, *b1
;
3721 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3723 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3728 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3730 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3734 b0
= gen_fhostop(eaddr
, Q_SRC
);
3735 b1
= gen_fhostop(eaddr
, Q_DST
);
3741 b0
= gen_fhostop(eaddr
, Q_SRC
);
3742 b1
= gen_fhostop(eaddr
, Q_DST
);
3751 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3753 static struct block
*
3754 gen_thostop(eaddr
, dir
)
3755 register const u_char
*eaddr
;
3758 register struct block
*b0
, *b1
;
3762 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3765 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3768 b0
= gen_thostop(eaddr
, Q_SRC
);
3769 b1
= gen_thostop(eaddr
, Q_DST
);
3775 b0
= gen_thostop(eaddr
, Q_SRC
);
3776 b1
= gen_thostop(eaddr
, Q_DST
);
3785 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3786 * various 802.11 + radio headers.
3788 static struct block
*
3789 gen_wlanhostop(eaddr
, dir
)
3790 register const u_char
*eaddr
;
3793 register struct block
*b0
, *b1
, *b2
;
3794 register struct slist
*s
;
3796 #ifdef ENABLE_WLAN_FILTERING_PATCH
3799 * We need to disable the optimizer because the optimizer is buggy
3800 * and wipes out some LD instructions generated by the below
3801 * code to validate the Frame Control bits
3804 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3811 * For control frames, there is no SA.
3813 * For management frames, SA is at an
3814 * offset of 10 from the beginning of
3817 * For data frames, SA is at an offset
3818 * of 10 from the beginning of the packet
3819 * if From DS is clear, at an offset of
3820 * 16 from the beginning of the packet
3821 * if From DS is set and To DS is clear,
3822 * and an offset of 24 from the beginning
3823 * of the packet if From DS is set and To DS
3828 * Generate the tests to be done for data frames
3831 * First, check for To DS set, i.e. check "link[1] & 0x01".
3833 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3834 b1
= new_block(JMP(BPF_JSET
));
3835 b1
->s
.k
= 0x01; /* To DS */
3839 * If To DS is set, the SA is at 24.
3841 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3845 * Now, check for To DS not set, i.e. check
3846 * "!(link[1] & 0x01)".
3848 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3849 b2
= new_block(JMP(BPF_JSET
));
3850 b2
->s
.k
= 0x01; /* To DS */
3855 * If To DS is not set, the SA is at 16.
3857 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3861 * Now OR together the last two checks. That gives
3862 * the complete set of checks for data frames with
3868 * Now check for From DS being set, and AND that with
3869 * the ORed-together checks.
3871 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3872 b1
= new_block(JMP(BPF_JSET
));
3873 b1
->s
.k
= 0x02; /* From DS */
3878 * Now check for data frames with From DS not set.
3880 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3881 b2
= new_block(JMP(BPF_JSET
));
3882 b2
->s
.k
= 0x02; /* From DS */
3887 * If From DS isn't set, the SA is at 10.
3889 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3893 * Now OR together the checks for data frames with
3894 * From DS not set and for data frames with From DS
3895 * set; that gives the checks done for data frames.
3900 * Now check for a data frame.
3901 * I.e, check "link[0] & 0x08".
3903 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3904 b1
= new_block(JMP(BPF_JSET
));
3909 * AND that with the checks done for data frames.
3914 * If the high-order bit of the type value is 0, this
3915 * is a management frame.
3916 * I.e, check "!(link[0] & 0x08)".
3918 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3919 b2
= new_block(JMP(BPF_JSET
));
3925 * For management frames, the SA is at 10.
3927 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3931 * OR that with the checks done for data frames.
3932 * That gives the checks done for management and
3938 * If the low-order bit of the type value is 1,
3939 * this is either a control frame or a frame
3940 * with a reserved type, and thus not a
3943 * I.e., check "!(link[0] & 0x04)".
3945 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3946 b1
= new_block(JMP(BPF_JSET
));
3952 * AND that with the checks for data and management
3962 * For control frames, there is no DA.
3964 * For management frames, DA is at an
3965 * offset of 4 from the beginning of
3968 * For data frames, DA is at an offset
3969 * of 4 from the beginning of the packet
3970 * if To DS is clear and at an offset of
3971 * 16 from the beginning of the packet
3976 * Generate the tests to be done for data frames.
3978 * First, check for To DS set, i.e. "link[1] & 0x01".
3980 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3981 b1
= new_block(JMP(BPF_JSET
));
3982 b1
->s
.k
= 0x01; /* To DS */
3986 * If To DS is set, the DA is at 16.
3988 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3992 * Now, check for To DS not set, i.e. check
3993 * "!(link[1] & 0x01)".
3995 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3996 b2
= new_block(JMP(BPF_JSET
));
3997 b2
->s
.k
= 0x01; /* To DS */
4002 * If To DS is not set, the DA is at 4.
4004 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4008 * Now OR together the last two checks. That gives
4009 * the complete set of checks for data frames.
4014 * Now check for a data frame.
4015 * I.e, check "link[0] & 0x08".
4017 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4018 b1
= new_block(JMP(BPF_JSET
));
4023 * AND that with the checks done for data frames.
4028 * If the high-order bit of the type value is 0, this
4029 * is a management frame.
4030 * I.e, check "!(link[0] & 0x08)".
4032 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4033 b2
= new_block(JMP(BPF_JSET
));
4039 * For management frames, the DA is at 4.
4041 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4045 * OR that with the checks done for data frames.
4046 * That gives the checks done for management and
4052 * If the low-order bit of the type value is 1,
4053 * this is either a control frame or a frame
4054 * with a reserved type, and thus not a
4057 * I.e., check "!(link[0] & 0x04)".
4059 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4060 b1
= new_block(JMP(BPF_JSET
));
4066 * AND that with the checks for data and management
4073 * XXX - add RA, TA, and BSSID keywords?
4076 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4080 * Not present in CTS or ACK control frames.
4082 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4083 IEEE80211_FC0_TYPE_MASK
);
4085 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4086 IEEE80211_FC0_SUBTYPE_MASK
);
4088 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4089 IEEE80211_FC0_SUBTYPE_MASK
);
4093 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4099 * Not present in control frames.
4101 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4102 IEEE80211_FC0_TYPE_MASK
);
4104 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4110 * Present only if the direction mask has both "From DS"
4111 * and "To DS" set. Neither control frames nor management
4112 * frames should have both of those set, so we don't
4113 * check the frame type.
4115 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4116 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4117 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4122 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4123 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4129 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4130 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4139 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4140 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4141 * as the RFC states.)
4143 static struct block
*
4144 gen_ipfchostop(eaddr
, dir
)
4145 register const u_char
*eaddr
;
4148 register struct block
*b0
, *b1
;
4152 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4155 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4158 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4159 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4165 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4166 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4175 * This is quite tricky because there may be pad bytes in front of the
4176 * DECNET header, and then there are two possible data packet formats that
4177 * carry both src and dst addresses, plus 5 packet types in a format that
4178 * carries only the src node, plus 2 types that use a different format and
4179 * also carry just the src node.
4183 * Instead of doing those all right, we just look for data packets with
4184 * 0 or 1 bytes of padding. If you want to look at other packets, that
4185 * will require a lot more hacking.
4187 * To add support for filtering on DECNET "areas" (network numbers)
4188 * one would want to add a "mask" argument to this routine. That would
4189 * make the filter even more inefficient, although one could be clever
4190 * and not generate masking instructions if the mask is 0xFFFF.
4192 static struct block
*
4193 gen_dnhostop(addr
, dir
)
4197 struct block
*b0
, *b1
, *b2
, *tmp
;
4198 u_int offset_lh
; /* offset if long header is received */
4199 u_int offset_sh
; /* offset if short header is received */
4204 offset_sh
= 1; /* follows flags */
4205 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4209 offset_sh
= 3; /* follows flags, dstnode */
4210 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4214 /* Inefficient because we do our Calvinball dance twice */
4215 b0
= gen_dnhostop(addr
, Q_SRC
);
4216 b1
= gen_dnhostop(addr
, Q_DST
);
4222 /* Inefficient because we do our Calvinball dance twice */
4223 b0
= gen_dnhostop(addr
, Q_SRC
);
4224 b1
= gen_dnhostop(addr
, Q_DST
);
4229 bpf_error("ISO host filtering not implemented");
4234 b0
= gen_linktype(ETHERTYPE_DN
);
4235 /* Check for pad = 1, long header case */
4236 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4237 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4238 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4239 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4241 /* Check for pad = 0, long header case */
4242 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4243 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4246 /* Check for pad = 1, short header case */
4247 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4248 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4249 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4252 /* Check for pad = 0, short header case */
4253 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4254 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4258 /* Combine with test for linktype */
4264 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4265 * test the bottom-of-stack bit, and then check the version number
4266 * field in the IP header.
4268 static struct block
*
4269 gen_mpls_linktype(proto
)
4272 struct block
*b0
, *b1
;
4277 /* match the bottom-of-stack bit */
4278 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4279 /* match the IPv4 version number */
4280 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4285 /* match the bottom-of-stack bit */
4286 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4287 /* match the IPv4 version number */
4288 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4297 static struct block
*
4298 gen_host(addr
, mask
, proto
, dir
, type
)
4305 struct block
*b0
, *b1
;
4306 const char *typestr
;
4316 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4318 * Only check for non-IPv4 addresses if we're not
4319 * checking MPLS-encapsulated packets.
4321 if (label_stack_depth
== 0) {
4322 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4324 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4330 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4333 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4336 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4339 bpf_error("'tcp' modifier applied to %s", typestr
);
4342 bpf_error("'sctp' modifier applied to %s", typestr
);
4345 bpf_error("'udp' modifier applied to %s", typestr
);
4348 bpf_error("'icmp' modifier applied to %s", typestr
);
4351 bpf_error("'igmp' modifier applied to %s", typestr
);
4354 bpf_error("'igrp' modifier applied to %s", typestr
);
4357 bpf_error("'pim' modifier applied to %s", typestr
);
4360 bpf_error("'vrrp' modifier applied to %s", typestr
);
4363 bpf_error("ATALK host filtering not implemented");
4366 bpf_error("AARP host filtering not implemented");
4369 return gen_dnhostop(addr
, dir
);
4372 bpf_error("SCA host filtering not implemented");
4375 bpf_error("LAT host filtering not implemented");
4378 bpf_error("MOPDL host filtering not implemented");
4381 bpf_error("MOPRC host filtering not implemented");
4385 bpf_error("'ip6' modifier applied to ip host");
4388 bpf_error("'icmp6' modifier applied to %s", typestr
);
4392 bpf_error("'ah' modifier applied to %s", typestr
);
4395 bpf_error("'esp' modifier applied to %s", typestr
);
4398 bpf_error("ISO host filtering not implemented");
4401 bpf_error("'esis' modifier applied to %s", typestr
);
4404 bpf_error("'isis' modifier applied to %s", typestr
);
4407 bpf_error("'clnp' modifier applied to %s", typestr
);
4410 bpf_error("'stp' modifier applied to %s", typestr
);
4413 bpf_error("IPX host filtering not implemented");
4416 bpf_error("'netbeui' modifier applied to %s", typestr
);
4419 bpf_error("'radio' modifier applied to %s", typestr
);
4428 static struct block
*
4429 gen_host6(addr
, mask
, proto
, dir
, type
)
4430 struct in6_addr
*addr
;
4431 struct in6_addr
*mask
;
4436 const char *typestr
;
4446 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4449 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4452 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4455 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4458 bpf_error("'sctp' modifier applied to %s", typestr
);
4461 bpf_error("'tcp' modifier applied to %s", typestr
);
4464 bpf_error("'udp' modifier applied to %s", typestr
);
4467 bpf_error("'icmp' modifier applied to %s", typestr
);
4470 bpf_error("'igmp' modifier applied to %s", typestr
);
4473 bpf_error("'igrp' modifier applied to %s", typestr
);
4476 bpf_error("'pim' modifier applied to %s", typestr
);
4479 bpf_error("'vrrp' modifier applied to %s", typestr
);
4482 bpf_error("ATALK host filtering not implemented");
4485 bpf_error("AARP host filtering not implemented");
4488 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4491 bpf_error("SCA host filtering not implemented");
4494 bpf_error("LAT host filtering not implemented");
4497 bpf_error("MOPDL host filtering not implemented");
4500 bpf_error("MOPRC host filtering not implemented");
4503 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4506 bpf_error("'icmp6' modifier applied to %s", typestr
);
4509 bpf_error("'ah' modifier applied to %s", typestr
);
4512 bpf_error("'esp' modifier applied to %s", typestr
);
4515 bpf_error("ISO host filtering not implemented");
4518 bpf_error("'esis' modifier applied to %s", typestr
);
4521 bpf_error("'isis' modifier applied to %s", typestr
);
4524 bpf_error("'clnp' modifier applied to %s", typestr
);
4527 bpf_error("'stp' modifier applied to %s", typestr
);
4530 bpf_error("IPX host filtering not implemented");
4533 bpf_error("'netbeui' modifier applied to %s", typestr
);
4536 bpf_error("'radio' modifier applied to %s", typestr
);
4546 static struct block
*
4547 gen_gateway(eaddr
, alist
, proto
, dir
)
4548 const u_char
*eaddr
;
4549 bpf_u_int32
**alist
;
4553 struct block
*b0
, *b1
, *tmp
;
4556 bpf_error("direction applied to 'gateway'");
4565 b0
= gen_ehostop(eaddr
, Q_OR
);
4568 b0
= gen_fhostop(eaddr
, Q_OR
);
4571 b0
= gen_thostop(eaddr
, Q_OR
);
4573 case DLT_IEEE802_11
:
4574 case DLT_PRISM_HEADER
:
4575 case DLT_IEEE802_11_RADIO_AVS
:
4576 case DLT_IEEE802_11_RADIO
:
4578 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4583 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4585 * Check that the packet doesn't begin with an
4586 * LE Control marker. (We've already generated
4589 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4594 * Now check the MAC address.
4596 b0
= gen_ehostop(eaddr
, Q_OR
);
4599 case DLT_IP_OVER_FC
:
4600 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4604 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4606 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4608 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4617 bpf_error("illegal modifier of 'gateway'");
4623 gen_proto_abbrev(proto
)
4632 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4634 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4640 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4642 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4648 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4650 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4656 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4659 #ifndef IPPROTO_IGMP
4660 #define IPPROTO_IGMP 2
4664 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4667 #ifndef IPPROTO_IGRP
4668 #define IPPROTO_IGRP 9
4671 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4675 #define IPPROTO_PIM 103
4679 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4681 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4686 #ifndef IPPROTO_VRRP
4687 #define IPPROTO_VRRP 112
4691 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4695 b1
= gen_linktype(ETHERTYPE_IP
);
4699 b1
= gen_linktype(ETHERTYPE_ARP
);
4703 b1
= gen_linktype(ETHERTYPE_REVARP
);
4707 bpf_error("link layer applied in wrong context");
4710 b1
= gen_linktype(ETHERTYPE_ATALK
);
4714 b1
= gen_linktype(ETHERTYPE_AARP
);
4718 b1
= gen_linktype(ETHERTYPE_DN
);
4722 b1
= gen_linktype(ETHERTYPE_SCA
);
4726 b1
= gen_linktype(ETHERTYPE_LAT
);
4730 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4734 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4739 b1
= gen_linktype(ETHERTYPE_IPV6
);
4742 #ifndef IPPROTO_ICMPV6
4743 #define IPPROTO_ICMPV6 58
4746 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4751 #define IPPROTO_AH 51
4754 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4756 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4762 #define IPPROTO_ESP 50
4765 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4767 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4773 b1
= gen_linktype(LLCSAP_ISONS
);
4777 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4781 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4784 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4785 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4786 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4788 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4790 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4792 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4796 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4797 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4798 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4800 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4802 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4804 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4808 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4809 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4810 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4812 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4817 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4818 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4823 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4824 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4826 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4828 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4833 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4834 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4839 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4840 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4845 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4849 b1
= gen_linktype(LLCSAP_8021D
);
4853 b1
= gen_linktype(LLCSAP_IPX
);
4857 b1
= gen_linktype(LLCSAP_NETBEUI
);
4861 bpf_error("'radio' is not a valid protocol type");
4869 static struct block
*
4876 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4877 b
= new_block(JMP(BPF_JSET
));
4886 * Generate a comparison to a port value in the transport-layer header
4887 * at the specified offset from the beginning of that header.
4889 * XXX - this handles a variable-length prefix preceding the link-layer
4890 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4891 * variable-length link-layer headers (such as Token Ring or 802.11
4894 static struct block
*
4895 gen_portatom(off
, v
)
4899 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4903 static struct block
*
4904 gen_portatom6(off
, v
)
4908 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4913 gen_portop(port
, proto
, dir
)
4914 int port
, proto
, dir
;
4916 struct block
*b0
, *b1
, *tmp
;
4918 /* ip proto 'proto' */
4919 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4925 b1
= gen_portatom(0, (bpf_int32
)port
);
4929 b1
= gen_portatom(2, (bpf_int32
)port
);
4934 tmp
= gen_portatom(0, (bpf_int32
)port
);
4935 b1
= gen_portatom(2, (bpf_int32
)port
);
4940 tmp
= gen_portatom(0, (bpf_int32
)port
);
4941 b1
= gen_portatom(2, (bpf_int32
)port
);
4953 static struct block
*
4954 gen_port(port
, ip_proto
, dir
)
4959 struct block
*b0
, *b1
, *tmp
;
4964 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4965 * not LLC encapsulation with LLCSAP_IP.
4967 * For IEEE 802 networks - which includes 802.5 token ring
4968 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4969 * says that SNAP encapsulation is used, not LLC encapsulation
4972 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4973 * RFC 2225 say that SNAP encapsulation is used, not LLC
4974 * encapsulation with LLCSAP_IP.
4976 * So we always check for ETHERTYPE_IP.
4978 b0
= gen_linktype(ETHERTYPE_IP
);
4984 b1
= gen_portop(port
, ip_proto
, dir
);
4988 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4989 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4991 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5004 gen_portop6(port
, proto
, dir
)
5005 int port
, proto
, dir
;
5007 struct block
*b0
, *b1
, *tmp
;
5009 /* ip6 proto 'proto' */
5010 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5014 b1
= gen_portatom6(0, (bpf_int32
)port
);
5018 b1
= gen_portatom6(2, (bpf_int32
)port
);
5023 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5024 b1
= gen_portatom6(2, (bpf_int32
)port
);
5029 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5030 b1
= gen_portatom6(2, (bpf_int32
)port
);
5042 static struct block
*
5043 gen_port6(port
, ip_proto
, dir
)
5048 struct block
*b0
, *b1
, *tmp
;
5050 /* link proto ip6 */
5051 b0
= gen_linktype(ETHERTYPE_IPV6
);
5057 b1
= gen_portop6(port
, ip_proto
, dir
);
5061 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5062 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5064 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5076 /* gen_portrange code */
5077 static struct block
*
5078 gen_portrangeatom(off
, v1
, v2
)
5082 struct block
*b1
, *b2
;
5086 * Reverse the order of the ports, so v1 is the lower one.
5095 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5096 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5104 gen_portrangeop(port1
, port2
, proto
, dir
)
5109 struct block
*b0
, *b1
, *tmp
;
5111 /* ip proto 'proto' */
5112 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5118 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5122 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5127 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5128 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5133 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5134 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5146 static struct block
*
5147 gen_portrange(port1
, port2
, ip_proto
, dir
)
5152 struct block
*b0
, *b1
, *tmp
;
5155 b0
= gen_linktype(ETHERTYPE_IP
);
5161 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5165 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5166 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5168 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5180 static struct block
*
5181 gen_portrangeatom6(off
, v1
, v2
)
5185 struct block
*b1
, *b2
;
5189 * Reverse the order of the ports, so v1 is the lower one.
5198 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5199 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5207 gen_portrangeop6(port1
, port2
, proto
, dir
)
5212 struct block
*b0
, *b1
, *tmp
;
5214 /* ip6 proto 'proto' */
5215 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5219 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5223 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5228 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5229 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5234 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5235 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5247 static struct block
*
5248 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5253 struct block
*b0
, *b1
, *tmp
;
5255 /* link proto ip6 */
5256 b0
= gen_linktype(ETHERTYPE_IPV6
);
5262 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5266 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5267 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5269 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5282 lookup_proto(name
, proto
)
5283 register const char *name
;
5293 v
= pcap_nametoproto(name
);
5294 if (v
== PROTO_UNDEF
)
5295 bpf_error("unknown ip proto '%s'", name
);
5299 /* XXX should look up h/w protocol type based on linktype */
5300 v
= pcap_nametoeproto(name
);
5301 if (v
== PROTO_UNDEF
) {
5302 v
= pcap_nametollc(name
);
5303 if (v
== PROTO_UNDEF
)
5304 bpf_error("unknown ether proto '%s'", name
);
5309 if (strcmp(name
, "esis") == 0)
5311 else if (strcmp(name
, "isis") == 0)
5313 else if (strcmp(name
, "clnp") == 0)
5316 bpf_error("unknown osi proto '%s'", name
);
5336 static struct block
*
5337 gen_protochain(v
, proto
, dir
)
5342 #ifdef NO_PROTOCHAIN
5343 return gen_proto(v
, proto
, dir
);
5345 struct block
*b0
, *b
;
5346 struct slist
*s
[100];
5347 int fix2
, fix3
, fix4
, fix5
;
5348 int ahcheck
, again
, end
;
5350 int reg2
= alloc_reg();
5352 memset(s
, 0, sizeof(s
));
5353 fix2
= fix3
= fix4
= fix5
= 0;
5360 b0
= gen_protochain(v
, Q_IP
, dir
);
5361 b
= gen_protochain(v
, Q_IPV6
, dir
);
5365 bpf_error("bad protocol applied for 'protochain'");
5370 * We don't handle variable-length prefixes before the link-layer
5371 * header, or variable-length link-layer headers, here yet.
5372 * We might want to add BPF instructions to do the protochain
5373 * work, to simplify that and, on platforms that have a BPF
5374 * interpreter with the new instructions, let the filtering
5375 * be done in the kernel. (We already require a modified BPF
5376 * engine to do the protochain stuff, to support backward
5377 * branches, and backward branch support is unlikely to appear
5378 * in kernel BPF engines.)
5382 case DLT_IEEE802_11
:
5383 case DLT_PRISM_HEADER
:
5384 case DLT_IEEE802_11_RADIO_AVS
:
5385 case DLT_IEEE802_11_RADIO
:
5387 bpf_error("'protochain' not supported with 802.11");
5390 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5393 * s[0] is a dummy entry to protect other BPF insn from damage
5394 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5395 * hard to find interdependency made by jump table fixup.
5398 s
[i
] = new_stmt(0); /*dummy*/
5403 b0
= gen_linktype(ETHERTYPE_IP
);
5406 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5407 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5409 /* X = ip->ip_hl << 2 */
5410 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5411 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5416 b0
= gen_linktype(ETHERTYPE_IPV6
);
5418 /* A = ip6->ip_nxt */
5419 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5420 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5422 /* X = sizeof(struct ip6_hdr) */
5423 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5429 bpf_error("unsupported proto to gen_protochain");
5433 /* again: if (A == v) goto end; else fall through; */
5435 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5437 s
[i
]->s
.jt
= NULL
; /*later*/
5438 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5442 #ifndef IPPROTO_NONE
5443 #define IPPROTO_NONE 59
5445 /* if (A == IPPROTO_NONE) goto end */
5446 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5447 s
[i
]->s
.jt
= NULL
; /*later*/
5448 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5449 s
[i
]->s
.k
= IPPROTO_NONE
;
5450 s
[fix5
]->s
.jf
= s
[i
];
5455 if (proto
== Q_IPV6
) {
5456 int v6start
, v6end
, v6advance
, j
;
5459 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5460 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5461 s
[i
]->s
.jt
= NULL
; /*later*/
5462 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5463 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5464 s
[fix2
]->s
.jf
= s
[i
];
5466 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5467 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5468 s
[i
]->s
.jt
= NULL
; /*later*/
5469 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5470 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5472 /* if (A == IPPROTO_ROUTING) goto v6advance */
5473 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5474 s
[i
]->s
.jt
= NULL
; /*later*/
5475 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5476 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5478 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5479 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5480 s
[i
]->s
.jt
= NULL
; /*later*/
5481 s
[i
]->s
.jf
= NULL
; /*later*/
5482 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5493 * X = X + (P[X + 1] + 1) * 8;
5496 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5498 /* A = P[X + packet head] */
5499 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5500 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5503 s
[i
] = new_stmt(BPF_ST
);
5507 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5510 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5514 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5516 /* A = P[X + packet head]; */
5517 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5518 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5521 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5525 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5529 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5532 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5536 /* goto again; (must use BPF_JA for backward jump) */
5537 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5538 s
[i
]->s
.k
= again
- i
- 1;
5539 s
[i
- 1]->s
.jf
= s
[i
];
5543 for (j
= v6start
; j
<= v6end
; j
++)
5544 s
[j
]->s
.jt
= s
[v6advance
];
5549 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5551 s
[fix2
]->s
.jf
= s
[i
];
5557 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5558 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5559 s
[i
]->s
.jt
= NULL
; /*later*/
5560 s
[i
]->s
.jf
= NULL
; /*later*/
5561 s
[i
]->s
.k
= IPPROTO_AH
;
5563 s
[fix3
]->s
.jf
= s
[ahcheck
];
5570 * X = X + (P[X + 1] + 2) * 4;
5573 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5575 /* A = P[X + packet head]; */
5576 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5577 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5580 s
[i
] = new_stmt(BPF_ST
);
5584 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5587 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5591 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5593 /* A = P[X + packet head] */
5594 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5595 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5598 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5602 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5606 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5609 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5613 /* goto again; (must use BPF_JA for backward jump) */
5614 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5615 s
[i
]->s
.k
= again
- i
- 1;
5620 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5622 s
[fix2
]->s
.jt
= s
[end
];
5623 s
[fix4
]->s
.jf
= s
[end
];
5624 s
[fix5
]->s
.jt
= s
[end
];
5631 for (i
= 0; i
< max
- 1; i
++)
5632 s
[i
]->next
= s
[i
+ 1];
5633 s
[max
- 1]->next
= NULL
;
5638 b
= new_block(JMP(BPF_JEQ
));
5639 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5649 static struct block
*
5650 gen_check_802_11_data_frame()
5653 struct block
*b0
, *b1
;
5656 * A data frame has the 0x08 bit (b3) in the frame control field set
5657 * and the 0x04 bit (b2) clear.
5659 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5660 b0
= new_block(JMP(BPF_JSET
));
5664 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5665 b1
= new_block(JMP(BPF_JSET
));
5676 * Generate code that checks whether the packet is a packet for protocol
5677 * <proto> and whether the type field in that protocol's header has
5678 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5679 * IP packet and checks the protocol number in the IP header against <v>.
5681 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5682 * against Q_IP and Q_IPV6.
5684 static struct block
*
5685 gen_proto(v
, proto
, dir
)
5690 struct block
*b0
, *b1
;
5692 if (dir
!= Q_DEFAULT
)
5693 bpf_error("direction applied to 'proto'");
5698 b0
= gen_proto(v
, Q_IP
, dir
);
5699 b1
= gen_proto(v
, Q_IPV6
, dir
);
5707 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5708 * not LLC encapsulation with LLCSAP_IP.
5710 * For IEEE 802 networks - which includes 802.5 token ring
5711 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5712 * says that SNAP encapsulation is used, not LLC encapsulation
5715 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5716 * RFC 2225 say that SNAP encapsulation is used, not LLC
5717 * encapsulation with LLCSAP_IP.
5719 * So we always check for ETHERTYPE_IP.
5721 b0
= gen_linktype(ETHERTYPE_IP
);
5723 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5725 b1
= gen_protochain(v
, Q_IP
);
5735 * Frame Relay packets typically have an OSI
5736 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5737 * generates code to check for all the OSI
5738 * NLPIDs, so calling it and then adding a check
5739 * for the particular NLPID for which we're
5740 * looking is bogus, as we can just check for
5743 * What we check for is the NLPID and a frame
5744 * control field value of UI, i.e. 0x03 followed
5747 * XXX - assumes a 2-byte Frame Relay header with
5748 * DLCI and flags. What if the address is longer?
5750 * XXX - what about SNAP-encapsulated frames?
5752 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5758 * Cisco uses an Ethertype lookalike - for OSI,
5761 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5762 /* OSI in C-HDLC is stuffed with a fudge byte */
5763 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5768 b0
= gen_linktype(LLCSAP_ISONS
);
5769 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5775 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5777 * 4 is the offset of the PDU type relative to the IS-IS
5780 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5785 bpf_error("arp does not encapsulate another protocol");
5789 bpf_error("rarp does not encapsulate another protocol");
5793 bpf_error("atalk encapsulation is not specifiable");
5797 bpf_error("decnet encapsulation is not specifiable");
5801 bpf_error("sca does not encapsulate another protocol");
5805 bpf_error("lat does not encapsulate another protocol");
5809 bpf_error("moprc does not encapsulate another protocol");
5813 bpf_error("mopdl does not encapsulate another protocol");
5817 return gen_linktype(v
);
5820 bpf_error("'udp proto' is bogus");
5824 bpf_error("'tcp proto' is bogus");
5828 bpf_error("'sctp proto' is bogus");
5832 bpf_error("'icmp proto' is bogus");
5836 bpf_error("'igmp proto' is bogus");
5840 bpf_error("'igrp proto' is bogus");
5844 bpf_error("'pim proto' is bogus");
5848 bpf_error("'vrrp proto' is bogus");
5853 b0
= gen_linktype(ETHERTYPE_IPV6
);
5855 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5857 b1
= gen_protochain(v
, Q_IPV6
);
5863 bpf_error("'icmp6 proto' is bogus");
5867 bpf_error("'ah proto' is bogus");
5870 bpf_error("'ah proto' is bogus");
5873 bpf_error("'stp proto' is bogus");
5876 bpf_error("'ipx proto' is bogus");
5879 bpf_error("'netbeui proto' is bogus");
5882 bpf_error("'radio proto' is bogus");
5893 register const char *name
;
5896 int proto
= q
.proto
;
5900 bpf_u_int32 mask
, addr
;
5902 bpf_u_int32
**alist
;
5905 struct sockaddr_in
*sin4
;
5906 struct sockaddr_in6
*sin6
;
5907 struct addrinfo
*res
, *res0
;
5908 struct in6_addr mask128
;
5910 struct block
*b
, *tmp
;
5911 int port
, real_proto
;
5917 addr
= pcap_nametonetaddr(name
);
5919 bpf_error("unknown network '%s'", name
);
5920 /* Left justify network addr and calculate its network mask */
5922 while (addr
&& (addr
& 0xff000000) == 0) {
5926 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5930 if (proto
== Q_LINK
) {
5934 eaddr
= pcap_ether_hostton(name
);
5937 "unknown ether host '%s'", name
);
5938 b
= gen_ehostop(eaddr
, dir
);
5943 eaddr
= pcap_ether_hostton(name
);
5946 "unknown FDDI host '%s'", name
);
5947 b
= gen_fhostop(eaddr
, dir
);
5952 eaddr
= pcap_ether_hostton(name
);
5955 "unknown token ring host '%s'", name
);
5956 b
= gen_thostop(eaddr
, dir
);
5960 case DLT_IEEE802_11
:
5961 case DLT_PRISM_HEADER
:
5962 case DLT_IEEE802_11_RADIO_AVS
:
5963 case DLT_IEEE802_11_RADIO
:
5965 eaddr
= pcap_ether_hostton(name
);
5968 "unknown 802.11 host '%s'", name
);
5969 b
= gen_wlanhostop(eaddr
, dir
);
5973 case DLT_IP_OVER_FC
:
5974 eaddr
= pcap_ether_hostton(name
);
5977 "unknown Fibre Channel host '%s'", name
);
5978 b
= gen_ipfchostop(eaddr
, dir
);
5987 * Check that the packet doesn't begin
5988 * with an LE Control marker. (We've
5989 * already generated a test for LANE.)
5991 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5995 eaddr
= pcap_ether_hostton(name
);
5998 "unknown ether host '%s'", name
);
5999 b
= gen_ehostop(eaddr
, dir
);
6005 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6006 } else if (proto
== Q_DECNET
) {
6007 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6009 * I don't think DECNET hosts can be multihomed, so
6010 * there is no need to build up a list of addresses
6012 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6015 alist
= pcap_nametoaddr(name
);
6016 if (alist
== NULL
|| *alist
== NULL
)
6017 bpf_error("unknown host '%s'", name
);
6019 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6021 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6023 tmp
= gen_host(**alist
++, 0xffffffff,
6024 tproto
, dir
, q
.addr
);
6030 memset(&mask128
, 0xff, sizeof(mask128
));
6031 res0
= res
= pcap_nametoaddrinfo(name
);
6033 bpf_error("unknown host '%s'", name
);
6036 tproto
= tproto6
= proto
;
6037 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6041 for (res
= res0
; res
; res
= res
->ai_next
) {
6042 switch (res
->ai_family
) {
6044 if (tproto
== Q_IPV6
)
6047 sin4
= (struct sockaddr_in
*)
6049 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6050 0xffffffff, tproto
, dir
, q
.addr
);
6053 if (tproto6
== Q_IP
)
6056 sin6
= (struct sockaddr_in6
*)
6058 tmp
= gen_host6(&sin6
->sin6_addr
,
6059 &mask128
, tproto6
, dir
, q
.addr
);
6071 bpf_error("unknown host '%s'%s", name
,
6072 (proto
== Q_DEFAULT
)
6074 : " for specified address family");
6081 if (proto
!= Q_DEFAULT
&&
6082 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6083 bpf_error("illegal qualifier of 'port'");
6084 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6085 bpf_error("unknown port '%s'", name
);
6086 if (proto
== Q_UDP
) {
6087 if (real_proto
== IPPROTO_TCP
)
6088 bpf_error("port '%s' is tcp", name
);
6089 else if (real_proto
== IPPROTO_SCTP
)
6090 bpf_error("port '%s' is sctp", name
);
6092 /* override PROTO_UNDEF */
6093 real_proto
= IPPROTO_UDP
;
6095 if (proto
== Q_TCP
) {
6096 if (real_proto
== IPPROTO_UDP
)
6097 bpf_error("port '%s' is udp", name
);
6099 else if (real_proto
== IPPROTO_SCTP
)
6100 bpf_error("port '%s' is sctp", name
);
6102 /* override PROTO_UNDEF */
6103 real_proto
= IPPROTO_TCP
;
6105 if (proto
== Q_SCTP
) {
6106 if (real_proto
== IPPROTO_UDP
)
6107 bpf_error("port '%s' is udp", name
);
6109 else if (real_proto
== IPPROTO_TCP
)
6110 bpf_error("port '%s' is tcp", name
);
6112 /* override PROTO_UNDEF */
6113 real_proto
= IPPROTO_SCTP
;
6116 return gen_port(port
, real_proto
, dir
);
6118 b
= gen_port(port
, real_proto
, dir
);
6119 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6124 if (proto
!= Q_DEFAULT
&&
6125 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6126 bpf_error("illegal qualifier of 'portrange'");
6127 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6128 bpf_error("unknown port in range '%s'", name
);
6129 if (proto
== Q_UDP
) {
6130 if (real_proto
== IPPROTO_TCP
)
6131 bpf_error("port in range '%s' is tcp", name
);
6132 else if (real_proto
== IPPROTO_SCTP
)
6133 bpf_error("port in range '%s' is sctp", name
);
6135 /* override PROTO_UNDEF */
6136 real_proto
= IPPROTO_UDP
;
6138 if (proto
== Q_TCP
) {
6139 if (real_proto
== IPPROTO_UDP
)
6140 bpf_error("port in range '%s' is udp", name
);
6141 else if (real_proto
== IPPROTO_SCTP
)
6142 bpf_error("port in range '%s' is sctp", name
);
6144 /* override PROTO_UNDEF */
6145 real_proto
= IPPROTO_TCP
;
6147 if (proto
== Q_SCTP
) {
6148 if (real_proto
== IPPROTO_UDP
)
6149 bpf_error("port in range '%s' is udp", name
);
6150 else if (real_proto
== IPPROTO_TCP
)
6151 bpf_error("port in range '%s' is tcp", name
);
6153 /* override PROTO_UNDEF */
6154 real_proto
= IPPROTO_SCTP
;
6157 return gen_portrange(port1
, port2
, real_proto
, dir
);
6159 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6160 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6166 eaddr
= pcap_ether_hostton(name
);
6168 bpf_error("unknown ether host: %s", name
);
6170 alist
= pcap_nametoaddr(name
);
6171 if (alist
== NULL
|| *alist
== NULL
)
6172 bpf_error("unknown host '%s'", name
);
6173 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6177 bpf_error("'gateway' not supported in this configuration");
6181 real_proto
= lookup_proto(name
, proto
);
6182 if (real_proto
>= 0)
6183 return gen_proto(real_proto
, proto
, dir
);
6185 bpf_error("unknown protocol: %s", name
);
6188 real_proto
= lookup_proto(name
, proto
);
6189 if (real_proto
>= 0)
6190 return gen_protochain(real_proto
, proto
, dir
);
6192 bpf_error("unknown protocol: %s", name
);
6203 gen_mcode(s1
, s2
, masklen
, q
)
6204 register const char *s1
, *s2
;
6205 register int masklen
;
6208 register int nlen
, mlen
;
6211 nlen
= __pcap_atoin(s1
, &n
);
6212 /* Promote short ipaddr */
6216 mlen
= __pcap_atoin(s2
, &m
);
6217 /* Promote short ipaddr */
6220 bpf_error("non-network bits set in \"%s mask %s\"",
6223 /* Convert mask len to mask */
6225 bpf_error("mask length must be <= 32");
6228 * X << 32 is not guaranteed by C to be 0; it's
6233 m
= 0xffffffff << (32 - masklen
);
6235 bpf_error("non-network bits set in \"%s/%d\"",
6242 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6245 bpf_error("Mask syntax for networks only");
6254 register const char *s
;
6259 int proto
= q
.proto
;
6265 else if (q
.proto
== Q_DECNET
)
6266 vlen
= __pcap_atodn(s
, &v
);
6268 vlen
= __pcap_atoin(s
, &v
);
6275 if (proto
== Q_DECNET
)
6276 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6277 else if (proto
== Q_LINK
) {
6278 bpf_error("illegal link layer address");
6281 if (s
== NULL
&& q
.addr
== Q_NET
) {
6282 /* Promote short net number */
6283 while (v
&& (v
& 0xff000000) == 0) {
6288 /* Promote short ipaddr */
6292 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6297 proto
= IPPROTO_UDP
;
6298 else if (proto
== Q_TCP
)
6299 proto
= IPPROTO_TCP
;
6300 else if (proto
== Q_SCTP
)
6301 proto
= IPPROTO_SCTP
;
6302 else if (proto
== Q_DEFAULT
)
6303 proto
= PROTO_UNDEF
;
6305 bpf_error("illegal qualifier of 'port'");
6308 return gen_port((int)v
, proto
, dir
);
6312 b
= gen_port((int)v
, proto
, dir
);
6313 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6320 proto
= IPPROTO_UDP
;
6321 else if (proto
== Q_TCP
)
6322 proto
= IPPROTO_TCP
;
6323 else if (proto
== Q_SCTP
)
6324 proto
= IPPROTO_SCTP
;
6325 else if (proto
== Q_DEFAULT
)
6326 proto
= PROTO_UNDEF
;
6328 bpf_error("illegal qualifier of 'portrange'");
6331 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6335 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6336 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6342 bpf_error("'gateway' requires a name");
6346 return gen_proto((int)v
, proto
, dir
);
6349 return gen_protochain((int)v
, proto
, dir
);
6364 gen_mcode6(s1
, s2
, masklen
, q
)
6365 register const char *s1
, *s2
;
6366 register int masklen
;
6369 struct addrinfo
*res
;
6370 struct in6_addr
*addr
;
6371 struct in6_addr mask
;
6376 bpf_error("no mask %s supported", s2
);
6378 res
= pcap_nametoaddrinfo(s1
);
6380 bpf_error("invalid ip6 address %s", s1
);
6383 bpf_error("%s resolved to multiple address", s1
);
6384 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6386 if (sizeof(mask
) * 8 < masklen
)
6387 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6388 memset(&mask
, 0, sizeof(mask
));
6389 memset(&mask
, 0xff, masklen
/ 8);
6391 mask
.s6_addr
[masklen
/ 8] =
6392 (0xff << (8 - masklen
% 8)) & 0xff;
6395 a
= (u_int32_t
*)addr
;
6396 m
= (u_int32_t
*)&mask
;
6397 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6398 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6399 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6407 bpf_error("Mask syntax for networks only");
6411 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6417 bpf_error("invalid qualifier against IPv6 address");
6426 register const u_char
*eaddr
;
6429 struct block
*b
, *tmp
;
6431 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6434 return gen_ehostop(eaddr
, (int)q
.dir
);
6436 return gen_fhostop(eaddr
, (int)q
.dir
);
6438 return gen_thostop(eaddr
, (int)q
.dir
);
6439 case DLT_IEEE802_11
:
6440 case DLT_PRISM_HEADER
:
6441 case DLT_IEEE802_11_RADIO_AVS
:
6442 case DLT_IEEE802_11_RADIO
:
6444 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6448 * Check that the packet doesn't begin with an
6449 * LE Control marker. (We've already generated
6452 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6457 * Now check the MAC address.
6459 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6464 case DLT_IP_OVER_FC
:
6465 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6467 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6471 bpf_error("ethernet address used in non-ether expression");
6478 struct slist
*s0
, *s1
;
6481 * This is definitely not the best way to do this, but the
6482 * lists will rarely get long.
6489 static struct slist
*
6495 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6500 static struct slist
*
6506 s
= new_stmt(BPF_LD
|BPF_MEM
);
6512 * Modify "index" to use the value stored into its register as an
6513 * offset relative to the beginning of the header for the protocol
6514 * "proto", and allocate a register and put an item "size" bytes long
6515 * (1, 2, or 4) at that offset into that register, making it the register
6519 gen_load(proto
, inst
, size
)
6524 struct slist
*s
, *tmp
;
6526 int regno
= alloc_reg();
6528 free_reg(inst
->regno
);
6532 bpf_error("data size must be 1, 2, or 4");
6548 bpf_error("unsupported index operation");
6552 * The offset is relative to the beginning of the packet
6553 * data, if we have a radio header. (If we don't, this
6556 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6557 linktype
!= DLT_IEEE802_11_RADIO
&&
6558 linktype
!= DLT_PRISM_HEADER
)
6559 bpf_error("radio information not present in capture");
6562 * Load into the X register the offset computed into the
6563 * register specifed by "index".
6565 s
= xfer_to_x(inst
);
6568 * Load the item at that offset.
6570 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6572 sappend(inst
->s
, s
);
6577 * The offset is relative to the beginning of
6578 * the link-layer header.
6580 * XXX - what about ATM LANE? Should the index be
6581 * relative to the beginning of the AAL5 frame, so
6582 * that 0 refers to the beginning of the LE Control
6583 * field, or relative to the beginning of the LAN
6584 * frame, so that 0 refers, for Ethernet LANE, to
6585 * the beginning of the destination address?
6587 s
= gen_llprefixlen();
6590 * If "s" is non-null, it has code to arrange that the
6591 * X register contains the length of the prefix preceding
6592 * the link-layer header. Add to it the offset computed
6593 * into the register specified by "index", and move that
6594 * into the X register. Otherwise, just load into the X
6595 * register the offset computed into the register specifed
6599 sappend(s
, xfer_to_a(inst
));
6600 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6601 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6603 s
= xfer_to_x(inst
);
6606 * Load the item at the sum of the offset we've put in the
6607 * X register and the offset of the start of the link
6608 * layer header (which is 0 if the radio header is
6609 * variable-length; that header length is what we put
6610 * into the X register and then added to the index).
6612 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6615 sappend(inst
->s
, s
);
6631 * The offset is relative to the beginning of
6632 * the network-layer header.
6633 * XXX - are there any cases where we want
6636 s
= gen_off_macpl();
6639 * If "s" is non-null, it has code to arrange that the
6640 * X register contains the offset of the MAC-layer
6641 * payload. Add to it the offset computed into the
6642 * register specified by "index", and move that into
6643 * the X register. Otherwise, just load into the X
6644 * register the offset computed into the register specifed
6648 sappend(s
, xfer_to_a(inst
));
6649 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6650 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6652 s
= xfer_to_x(inst
);
6655 * Load the item at the sum of the offset we've put in the
6656 * X register, the offset of the start of the network
6657 * layer header from the beginning of the MAC-layer
6658 * payload, and the purported offset of the start of the
6659 * MAC-layer payload (which might be 0 if there's a
6660 * variable-length prefix before the link-layer header
6661 * or the link-layer header itself is variable-length;
6662 * the variable-length offset of the start of the
6663 * MAC-layer payload is what we put into the X register
6664 * and then added to the index).
6666 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6667 tmp
->s
.k
= off_macpl
+ off_nl
;
6669 sappend(inst
->s
, s
);
6672 * Do the computation only if the packet contains
6673 * the protocol in question.
6675 b
= gen_proto_abbrev(proto
);
6677 gen_and(inst
->b
, b
);
6690 * The offset is relative to the beginning of
6691 * the transport-layer header.
6693 * Load the X register with the length of the IPv4 header
6694 * (plus the offset of the link-layer header, if it's
6695 * a variable-length header), in bytes.
6697 * XXX - are there any cases where we want
6699 * XXX - we should, if we're built with
6700 * IPv6 support, generate code to load either
6701 * IPv4, IPv6, or both, as appropriate.
6703 s
= gen_loadx_iphdrlen();
6706 * The X register now contains the sum of the length
6707 * of any variable-length header preceding the link-layer
6708 * header, any variable-length link-layer header, and the
6709 * length of the network-layer header.
6711 * Load into the A register the offset relative to
6712 * the beginning of the transport layer header,
6713 * add the X register to that, move that to the
6714 * X register, and load with an offset from the
6715 * X register equal to the offset of the network
6716 * layer header relative to the beginning of
6717 * the MAC-layer payload plus the fixed-length
6718 * portion of the offset of the MAC-layer payload
6719 * from the beginning of the raw packet data.
6721 sappend(s
, xfer_to_a(inst
));
6722 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6723 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6724 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6725 tmp
->s
.k
= off_macpl
+ off_nl
;
6726 sappend(inst
->s
, s
);
6729 * Do the computation only if the packet contains
6730 * the protocol in question - which is true only
6731 * if this is an IP datagram and is the first or
6732 * only fragment of that datagram.
6734 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6736 gen_and(inst
->b
, b
);
6738 gen_and(gen_proto_abbrev(Q_IP
), b
);
6744 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6748 inst
->regno
= regno
;
6749 s
= new_stmt(BPF_ST
);
6751 sappend(inst
->s
, s
);
6757 gen_relation(code
, a0
, a1
, reversed
)
6759 struct arth
*a0
, *a1
;
6762 struct slist
*s0
, *s1
, *s2
;
6763 struct block
*b
, *tmp
;
6767 if (code
== BPF_JEQ
) {
6768 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6769 b
= new_block(JMP(code
));
6773 b
= new_block(BPF_JMP
|code
|BPF_X
);
6779 sappend(a0
->s
, a1
->s
);
6783 free_reg(a0
->regno
);
6784 free_reg(a1
->regno
);
6786 /* 'and' together protocol checks */
6789 gen_and(a0
->b
, tmp
= a1
->b
);
6805 int regno
= alloc_reg();
6806 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6809 s
= new_stmt(BPF_LD
|BPF_LEN
);
6810 s
->next
= new_stmt(BPF_ST
);
6811 s
->next
->s
.k
= regno
;
6826 a
= (struct arth
*)newchunk(sizeof(*a
));
6830 s
= new_stmt(BPF_LD
|BPF_IMM
);
6832 s
->next
= new_stmt(BPF_ST
);
6848 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6851 s
= new_stmt(BPF_ST
);
6859 gen_arth(code
, a0
, a1
)
6861 struct arth
*a0
, *a1
;
6863 struct slist
*s0
, *s1
, *s2
;
6867 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6872 sappend(a0
->s
, a1
->s
);
6874 free_reg(a0
->regno
);
6875 free_reg(a1
->regno
);
6877 s0
= new_stmt(BPF_ST
);
6878 a0
->regno
= s0
->s
.k
= alloc_reg();
6885 * Here we handle simple allocation of the scratch registers.
6886 * If too many registers are alloc'd, the allocator punts.
6888 static int regused
[BPF_MEMWORDS
];
6892 * Initialize the table of used registers and the current register.
6898 memset(regused
, 0, sizeof regused
);
6902 * Return the next free register.
6907 int n
= BPF_MEMWORDS
;
6910 if (regused
[curreg
])
6911 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6913 regused
[curreg
] = 1;
6917 bpf_error("too many registers needed to evaluate expression");
6923 * Return a register to the table so it can
6933 static struct block
*
6940 s
= new_stmt(BPF_LD
|BPF_LEN
);
6941 b
= new_block(JMP(jmp
));
6952 return gen_len(BPF_JGE
, n
);
6956 * Actually, this is less than or equal.
6964 b
= gen_len(BPF_JGT
, n
);
6971 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6972 * the beginning of the link-layer header.
6973 * XXX - that means you can't test values in the radiotap header, but
6974 * as that header is difficult if not impossible to parse generally
6975 * without a loop, that might not be a severe problem. A new keyword
6976 * "radio" could be added for that, although what you'd really want
6977 * would be a way of testing particular radio header values, which
6978 * would generate code appropriate to the radio header in question.
6981 gen_byteop(op
, idx
, val
)
6992 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6995 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6999 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7003 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7007 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7011 b
= new_block(JMP(BPF_JEQ
));
7018 static u_char abroadcast
[] = { 0x0 };
7021 gen_broadcast(proto
)
7024 bpf_u_int32 hostmask
;
7025 struct block
*b0
, *b1
, *b2
;
7026 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7034 case DLT_ARCNET_LINUX
:
7035 return gen_ahostop(abroadcast
, Q_DST
);
7037 return gen_ehostop(ebroadcast
, Q_DST
);
7039 return gen_fhostop(ebroadcast
, Q_DST
);
7041 return gen_thostop(ebroadcast
, Q_DST
);
7042 case DLT_IEEE802_11
:
7043 case DLT_PRISM_HEADER
:
7044 case DLT_IEEE802_11_RADIO_AVS
:
7045 case DLT_IEEE802_11_RADIO
:
7047 return gen_wlanhostop(ebroadcast
, Q_DST
);
7048 case DLT_IP_OVER_FC
:
7049 return gen_ipfchostop(ebroadcast
, Q_DST
);
7053 * Check that the packet doesn't begin with an
7054 * LE Control marker. (We've already generated
7057 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7062 * Now check the MAC address.
7064 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7070 bpf_error("not a broadcast link");
7075 b0
= gen_linktype(ETHERTYPE_IP
);
7076 hostmask
= ~netmask
;
7077 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7078 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7079 (bpf_int32
)(~0 & hostmask
), hostmask
);
7084 bpf_error("only link-layer/IP broadcast filters supported");
7090 * Generate code to test the low-order bit of a MAC address (that's
7091 * the bottom bit of the *first* byte).
7093 static struct block
*
7094 gen_mac_multicast(offset
)
7097 register struct block
*b0
;
7098 register struct slist
*s
;
7100 /* link[offset] & 1 != 0 */
7101 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7102 b0
= new_block(JMP(BPF_JSET
));
7109 gen_multicast(proto
)
7112 register struct block
*b0
, *b1
, *b2
;
7113 register struct slist
*s
;
7121 case DLT_ARCNET_LINUX
:
7122 /* all ARCnet multicasts use the same address */
7123 return gen_ahostop(abroadcast
, Q_DST
);
7125 /* ether[0] & 1 != 0 */
7126 return gen_mac_multicast(0);
7129 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7131 * XXX - was that referring to bit-order issues?
7133 /* fddi[1] & 1 != 0 */
7134 return gen_mac_multicast(1);
7136 /* tr[2] & 1 != 0 */
7137 return gen_mac_multicast(2);
7138 case DLT_IEEE802_11
:
7139 case DLT_PRISM_HEADER
:
7140 case DLT_IEEE802_11_RADIO_AVS
:
7141 case DLT_IEEE802_11_RADIO
:
7146 * For control frames, there is no DA.
7148 * For management frames, DA is at an
7149 * offset of 4 from the beginning of
7152 * For data frames, DA is at an offset
7153 * of 4 from the beginning of the packet
7154 * if To DS is clear and at an offset of
7155 * 16 from the beginning of the packet
7160 * Generate the tests to be done for data frames.
7162 * First, check for To DS set, i.e. "link[1] & 0x01".
7164 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7165 b1
= new_block(JMP(BPF_JSET
));
7166 b1
->s
.k
= 0x01; /* To DS */
7170 * If To DS is set, the DA is at 16.
7172 b0
= gen_mac_multicast(16);
7176 * Now, check for To DS not set, i.e. check
7177 * "!(link[1] & 0x01)".
7179 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7180 b2
= new_block(JMP(BPF_JSET
));
7181 b2
->s
.k
= 0x01; /* To DS */
7186 * If To DS is not set, the DA is at 4.
7188 b1
= gen_mac_multicast(4);
7192 * Now OR together the last two checks. That gives
7193 * the complete set of checks for data frames.
7198 * Now check for a data frame.
7199 * I.e, check "link[0] & 0x08".
7201 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7202 b1
= new_block(JMP(BPF_JSET
));
7207 * AND that with the checks done for data frames.
7212 * If the high-order bit of the type value is 0, this
7213 * is a management frame.
7214 * I.e, check "!(link[0] & 0x08)".
7216 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7217 b2
= new_block(JMP(BPF_JSET
));
7223 * For management frames, the DA is at 4.
7225 b1
= gen_mac_multicast(4);
7229 * OR that with the checks done for data frames.
7230 * That gives the checks done for management and
7236 * If the low-order bit of the type value is 1,
7237 * this is either a control frame or a frame
7238 * with a reserved type, and thus not a
7241 * I.e., check "!(link[0] & 0x04)".
7243 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7244 b1
= new_block(JMP(BPF_JSET
));
7250 * AND that with the checks for data and management
7255 case DLT_IP_OVER_FC
:
7256 b0
= gen_mac_multicast(2);
7261 * Check that the packet doesn't begin with an
7262 * LE Control marker. (We've already generated
7265 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7269 /* ether[off_mac] & 1 != 0 */
7270 b0
= gen_mac_multicast(off_mac
);
7278 /* Link not known to support multicasts */
7282 b0
= gen_linktype(ETHERTYPE_IP
);
7283 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7289 b0
= gen_linktype(ETHERTYPE_IPV6
);
7290 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7295 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7301 * generate command for inbound/outbound. It's here so we can
7302 * make it link-type specific. 'dir' = 0 implies "inbound",
7303 * = 1 implies "outbound".
7309 register struct block
*b0
;
7312 * Only some data link types support inbound/outbound qualifiers.
7316 b0
= gen_relation(BPF_JEQ
,
7317 gen_load(Q_LINK
, gen_loadi(0), 1),
7325 * Match packets sent by this machine.
7327 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7330 * Match packets sent to this machine.
7331 * (No broadcast or multicast packets, or
7332 * packets sent to some other machine and
7333 * received promiscuously.)
7335 * XXX - packets sent to other machines probably
7336 * shouldn't be matched, but what about broadcast
7337 * or multicast packets we received?
7339 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7343 #ifdef HAVE_NET_PFVAR_H
7345 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7346 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7352 /* match outgoing packets */
7353 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7355 /* match incoming packets */
7356 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7360 case DLT_JUNIPER_MFR
:
7361 case DLT_JUNIPER_MLFR
:
7362 case DLT_JUNIPER_MLPPP
:
7363 case DLT_JUNIPER_ATM1
:
7364 case DLT_JUNIPER_ATM2
:
7365 case DLT_JUNIPER_PPPOE
:
7366 case DLT_JUNIPER_PPPOE_ATM
:
7367 case DLT_JUNIPER_GGSN
:
7368 case DLT_JUNIPER_ES
:
7369 case DLT_JUNIPER_MONITOR
:
7370 case DLT_JUNIPER_SERVICES
:
7371 case DLT_JUNIPER_ETHER
:
7372 case DLT_JUNIPER_PPP
:
7373 case DLT_JUNIPER_FRELAY
:
7374 case DLT_JUNIPER_CHDLC
:
7375 case DLT_JUNIPER_VP
:
7376 case DLT_JUNIPER_ST
:
7377 case DLT_JUNIPER_ISM
:
7378 /* juniper flags (including direction) are stored
7379 * the byte after the 3-byte magic number */
7381 /* match outgoing packets */
7382 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7384 /* match incoming packets */
7385 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7390 bpf_error("inbound/outbound not supported on linktype %d",
7398 #ifdef HAVE_NET_PFVAR_H
7399 /* PF firewall log matched interface */
7401 gen_pf_ifname(const char *ifname
)
7406 if (linktype
!= DLT_PFLOG
) {
7407 bpf_error("ifname supported only on PF linktype");
7410 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7411 off
= offsetof(struct pfloghdr
, ifname
);
7412 if (strlen(ifname
) >= len
) {
7413 bpf_error("ifname interface names can only be %d characters",
7417 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7421 /* PF firewall log ruleset name */
7423 gen_pf_ruleset(char *ruleset
)
7427 if (linktype
!= DLT_PFLOG
) {
7428 bpf_error("ruleset supported only on PF linktype");
7432 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7433 bpf_error("ruleset names can only be %ld characters",
7434 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7438 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7439 strlen(ruleset
), (const u_char
*)ruleset
);
7443 /* PF firewall log rule number */
7449 if (linktype
!= DLT_PFLOG
) {
7450 bpf_error("rnr supported only on PF linktype");
7454 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7459 /* PF firewall log sub-rule number */
7461 gen_pf_srnr(int srnr
)
7465 if (linktype
!= DLT_PFLOG
) {
7466 bpf_error("srnr supported only on PF linktype");
7470 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7475 /* PF firewall log reason code */
7477 gen_pf_reason(int reason
)
7481 if (linktype
!= DLT_PFLOG
) {
7482 bpf_error("reason supported only on PF linktype");
7486 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7491 /* PF firewall log action */
7493 gen_pf_action(int action
)
7497 if (linktype
!= DLT_PFLOG
) {
7498 bpf_error("action supported only on PF linktype");
7502 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7506 #else /* !HAVE_NET_PFVAR_H */
7508 gen_pf_ifname(const char *ifname
)
7510 bpf_error("libpcap was compiled without pf support");
7516 gen_pf_ruleset(char *ruleset
)
7518 bpf_error("libpcap was compiled on a machine without pf support");
7526 bpf_error("libpcap was compiled on a machine without pf support");
7532 gen_pf_srnr(int srnr
)
7534 bpf_error("libpcap was compiled on a machine without pf support");
7540 gen_pf_reason(int reason
)
7542 bpf_error("libpcap was compiled on a machine without pf support");
7548 gen_pf_action(int action
)
7550 bpf_error("libpcap was compiled on a machine without pf support");
7554 #endif /* HAVE_NET_PFVAR_H */
7556 /* IEEE 802.11 wireless header */
7558 gen_p80211_type(int type
, int mask
)
7564 case DLT_IEEE802_11
:
7565 case DLT_PRISM_HEADER
:
7566 case DLT_IEEE802_11_RADIO_AVS
:
7567 case DLT_IEEE802_11_RADIO
:
7568 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7573 bpf_error("802.11 link-layer types supported only on 802.11");
7581 gen_p80211_fcdir(int fcdir
)
7587 case DLT_IEEE802_11
:
7588 case DLT_PRISM_HEADER
:
7589 case DLT_IEEE802_11_RADIO_AVS
:
7590 case DLT_IEEE802_11_RADIO
:
7594 bpf_error("frame direction supported only with 802.11 headers");
7598 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7599 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7606 register const u_char
*eaddr
;
7612 case DLT_ARCNET_LINUX
:
7613 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7615 return (gen_ahostop(eaddr
, (int)q
.dir
));
7617 bpf_error("ARCnet address used in non-arc expression");
7623 bpf_error("aid supported only on ARCnet");
7626 bpf_error("ARCnet address used in non-arc expression");
7631 static struct block
*
7632 gen_ahostop(eaddr
, dir
)
7633 register const u_char
*eaddr
;
7636 register struct block
*b0
, *b1
;
7639 /* src comes first, different from Ethernet */
7641 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7644 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7647 b0
= gen_ahostop(eaddr
, Q_SRC
);
7648 b1
= gen_ahostop(eaddr
, Q_DST
);
7654 b0
= gen_ahostop(eaddr
, Q_SRC
);
7655 b1
= gen_ahostop(eaddr
, Q_DST
);
7664 * support IEEE 802.1Q VLAN trunk over ethernet
7670 struct block
*b0
, *b1
;
7672 /* can't check for VLAN-encapsulated packets inside MPLS */
7673 if (label_stack_depth
> 0)
7674 bpf_error("no VLAN match after MPLS");
7677 * Check for a VLAN packet, and then change the offsets to point
7678 * to the type and data fields within the VLAN packet. Just
7679 * increment the offsets, so that we can support a hierarchy, e.g.
7680 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7683 * XXX - this is a bit of a kludge. If we were to split the
7684 * compiler into a parser that parses an expression and
7685 * generates an expression tree, and a code generator that
7686 * takes an expression tree (which could come from our
7687 * parser or from some other parser) and generates BPF code,
7688 * we could perhaps make the offsets parameters of routines
7689 * and, in the handler for an "AND" node, pass to subnodes
7690 * other than the VLAN node the adjusted offsets.
7692 * This would mean that "vlan" would, instead of changing the
7693 * behavior of *all* tests after it, change only the behavior
7694 * of tests ANDed with it. That would change the documented
7695 * semantics of "vlan", which might break some expressions.
7696 * However, it would mean that "(vlan and ip) or ip" would check
7697 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7698 * checking only for VLAN-encapsulated IP, so that could still
7699 * be considered worth doing; it wouldn't break expressions
7700 * that are of the form "vlan and ..." or "vlan N and ...",
7701 * which I suspect are the most common expressions involving
7702 * "vlan". "vlan or ..." doesn't necessarily do what the user
7703 * would really want, now, as all the "or ..." tests would
7704 * be done assuming a VLAN, even though the "or" could be viewed
7705 * as meaning "or, if this isn't a VLAN packet...".
7712 /* check for VLAN */
7713 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7714 (bpf_int32
)ETHERTYPE_8021Q
);
7716 /* If a specific VLAN is requested, check VLAN id */
7717 if (vlan_num
>= 0) {
7718 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7719 (bpf_int32
)vlan_num
, 0x0fff);
7733 bpf_error("no VLAN support for data link type %d",
7748 struct block
*b0
,*b1
;
7751 * Change the offsets to point to the type and data fields within
7752 * the MPLS packet. Just increment the offsets, so that we
7753 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7754 * capture packets with an outer label of 100000 and an inner
7757 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7761 if (label_stack_depth
> 0) {
7762 /* just match the bottom-of-stack bit clear */
7763 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7766 * Indicate that we're checking MPLS-encapsulated headers,
7767 * to make sure higher level code generators don't try to
7768 * match against IP-related protocols such as Q_ARP, Q_RARP
7773 case DLT_C_HDLC
: /* fall through */
7775 b0
= gen_linktype(ETHERTYPE_MPLS
);
7779 b0
= gen_linktype(PPP_MPLS_UCAST
);
7782 /* FIXME add other DLT_s ...
7783 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7784 * leave it for now */
7787 bpf_error("no MPLS support for data link type %d",
7795 /* If a specific MPLS label is requested, check it */
7796 if (label_num
>= 0) {
7797 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7798 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7799 0xfffff000); /* only compare the first 20 bits */
7806 label_stack_depth
++;
7811 * Support PPPOE discovery and session.
7816 /* check for PPPoE discovery */
7817 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7826 * Test against the PPPoE session link-layer type.
7828 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7831 * Change the offsets to point to the type and data fields within
7832 * the PPP packet, and note that this is PPPoE rather than
7835 * XXX - this is a bit of a kludge. If we were to split the
7836 * compiler into a parser that parses an expression and
7837 * generates an expression tree, and a code generator that
7838 * takes an expression tree (which could come from our
7839 * parser or from some other parser) and generates BPF code,
7840 * we could perhaps make the offsets parameters of routines
7841 * and, in the handler for an "AND" node, pass to subnodes
7842 * other than the PPPoE node the adjusted offsets.
7844 * This would mean that "pppoes" would, instead of changing the
7845 * behavior of *all* tests after it, change only the behavior
7846 * of tests ANDed with it. That would change the documented
7847 * semantics of "pppoes", which might break some expressions.
7848 * However, it would mean that "(pppoes and ip) or ip" would check
7849 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7850 * checking only for VLAN-encapsulated IP, so that could still
7851 * be considered worth doing; it wouldn't break expressions
7852 * that are of the form "pppoes and ..." which I suspect are the
7853 * most common expressions involving "pppoes". "pppoes or ..."
7854 * doesn't necessarily do what the user would really want, now,
7855 * as all the "or ..." tests would be done assuming PPPoE, even
7856 * though the "or" could be viewed as meaning "or, if this isn't
7857 * a PPPoE packet...".
7859 orig_linktype
= off_linktype
; /* save original values */
7864 * The "network-layer" protocol is PPPoE, which has a 6-byte
7865 * PPPoE header, followed by a PPP packet.
7867 * There is no HDLC encapsulation for the PPP packet (it's
7868 * encapsulated in PPPoES instead), so the link-layer type
7869 * starts at the first byte of the PPP packet. For PPPoE,
7870 * that offset is relative to the beginning of the total
7871 * link-layer payload, including any 802.2 LLC header, so
7872 * it's 6 bytes past off_nl.
7874 off_linktype
= off_nl
+ 6;
7877 * The network-layer offsets are relative to the beginning
7878 * of the MAC-layer payload; that's past the 6-byte
7879 * PPPoE header and the 2-byte PPP header.
7882 off_nl_nosnap
= 6+2;
7888 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7900 bpf_error("'vpi' supported only on raw ATM");
7901 if (off_vpi
== (u_int
)-1)
7903 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7909 bpf_error("'vci' supported only on raw ATM");
7910 if (off_vci
== (u_int
)-1)
7912 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7917 if (off_proto
== (u_int
)-1)
7918 abort(); /* XXX - this isn't on FreeBSD */
7919 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7924 if (off_payload
== (u_int
)-1)
7926 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7927 0xffffffff, jtype
, reverse
, jvalue
);
7932 bpf_error("'callref' supported only on raw ATM");
7933 if (off_proto
== (u_int
)-1)
7935 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7936 jtype
, reverse
, jvalue
);
7946 gen_atmtype_abbrev(type
)
7949 struct block
*b0
, *b1
;
7954 /* Get all packets in Meta signalling Circuit */
7956 bpf_error("'metac' supported only on raw ATM");
7957 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7958 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7963 /* Get all packets in Broadcast Circuit*/
7965 bpf_error("'bcc' supported only on raw ATM");
7966 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7967 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7972 /* Get all cells in Segment OAM F4 circuit*/
7974 bpf_error("'oam4sc' supported only on raw ATM");
7975 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7976 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7981 /* Get all cells in End-to-End OAM F4 Circuit*/
7983 bpf_error("'oam4ec' supported only on raw ATM");
7984 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7985 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7990 /* Get all packets in connection Signalling Circuit */
7992 bpf_error("'sc' supported only on raw ATM");
7993 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7994 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7999 /* Get all packets in ILMI Circuit */
8001 bpf_error("'ilmic' supported only on raw ATM");
8002 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8003 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8008 /* Get all LANE packets */
8010 bpf_error("'lane' supported only on raw ATM");
8011 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8014 * Arrange that all subsequent tests assume LANE
8015 * rather than LLC-encapsulated packets, and set
8016 * the offsets appropriately for LANE-encapsulated
8019 * "off_mac" is the offset of the Ethernet header,
8020 * which is 2 bytes past the ATM pseudo-header
8021 * (skipping the pseudo-header and 2-byte LE Client
8022 * field). The other offsets are Ethernet offsets
8023 * relative to "off_mac".
8026 off_mac
= off_payload
+ 2; /* MAC header */
8027 off_linktype
= off_mac
+ 12;
8028 off_macpl
= off_mac
+ 14; /* Ethernet */
8029 off_nl
= 0; /* Ethernet II */
8030 off_nl_nosnap
= 3; /* 802.3+802.2 */
8034 /* Get all LLC-encapsulated packets */
8036 bpf_error("'llc' supported only on raw ATM");
8037 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8048 * Filtering for MTP2 messages based on li value
8049 * FISU, length is null
8050 * LSSU, length is 1 or 2
8051 * MSU, length is 3 or more
8054 gen_mtp2type_abbrev(type
)
8057 struct block
*b0
, *b1
;
8062 if ( (linktype
!= DLT_MTP2
) &&
8063 (linktype
!= DLT_ERF
) &&
8064 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8065 bpf_error("'fisu' supported only on MTP2");
8066 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8067 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8071 if ( (linktype
!= DLT_MTP2
) &&
8072 (linktype
!= DLT_ERF
) &&
8073 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8074 bpf_error("'lssu' supported only on MTP2");
8075 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8076 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8081 if ( (linktype
!= DLT_MTP2
) &&
8082 (linktype
!= DLT_ERF
) &&
8083 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8084 bpf_error("'msu' supported only on MTP2");
8085 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8095 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8102 bpf_u_int32 val1
, val2
, val3
;
8104 switch (mtp3field
) {
8107 if (off_sio
== (u_int
)-1)
8108 bpf_error("'sio' supported only on SS7");
8109 /* sio coded on 1 byte so max value 255 */
8111 bpf_error("sio value %u too big; max value = 255",
8113 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8114 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8118 if (off_opc
== (u_int
)-1)
8119 bpf_error("'opc' supported only on SS7");
8120 /* opc coded on 14 bits so max value 16383 */
8122 bpf_error("opc value %u too big; max value = 16383",
8124 /* the following instructions are made to convert jvalue
8125 * to the form used to write opc in an ss7 message*/
8126 val1
= jvalue
& 0x00003c00;
8128 val2
= jvalue
& 0x000003fc;
8130 val3
= jvalue
& 0x00000003;
8132 jvalue
= val1
+ val2
+ val3
;
8133 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8134 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8138 if (off_dpc
== (u_int
)-1)
8139 bpf_error("'dpc' supported only on SS7");
8140 /* dpc coded on 14 bits so max value 16383 */
8142 bpf_error("dpc value %u too big; max value = 16383",
8144 /* the following instructions are made to convert jvalue
8145 * to the forme used to write dpc in an ss7 message*/
8146 val1
= jvalue
& 0x000000ff;
8148 val2
= jvalue
& 0x00003f00;
8150 jvalue
= val1
+ val2
;
8151 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8152 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8156 if (off_sls
== (u_int
)-1)
8157 bpf_error("'sls' supported only on SS7");
8158 /* sls coded on 4 bits so max value 15 */
8160 bpf_error("sls value %u too big; max value = 15",
8162 /* the following instruction is made to convert jvalue
8163 * to the forme used to write sls in an ss7 message*/
8164 jvalue
= jvalue
<< 4;
8165 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8166 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8175 static struct block
*
8176 gen_msg_abbrev(type
)
8182 * Q.2931 signalling protocol messages for handling virtual circuits
8183 * establishment and teardown
8188 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8192 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8196 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8200 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8204 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8207 case A_RELEASE_DONE
:
8208 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8218 gen_atmmulti_abbrev(type
)
8221 struct block
*b0
, *b1
;
8227 bpf_error("'oam' supported only on raw ATM");
8228 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8233 bpf_error("'oamf4' supported only on raw ATM");
8235 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8236 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8238 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8244 * Get Q.2931 signalling messages for switched
8245 * virtual connection
8248 bpf_error("'connectmsg' supported only on raw ATM");
8249 b0
= gen_msg_abbrev(A_SETUP
);
8250 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8252 b0
= gen_msg_abbrev(A_CONNECT
);
8254 b0
= gen_msg_abbrev(A_CONNECTACK
);
8256 b0
= gen_msg_abbrev(A_RELEASE
);
8258 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8260 b0
= gen_atmtype_abbrev(A_SC
);
8266 bpf_error("'metaconnect' supported only on raw ATM");
8267 b0
= gen_msg_abbrev(A_SETUP
);
8268 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8270 b0
= gen_msg_abbrev(A_CONNECT
);
8272 b0
= gen_msg_abbrev(A_RELEASE
);
8274 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8276 b0
= gen_atmtype_abbrev(A_METAC
);