1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 * XXX - why was this included even on UNIX?
56 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
76 #include "ethertype.h"
80 #include "ieee80211.h"
82 #include "sunatmpos.h"
85 #include "pcap/ipnet.h"
87 #ifdef HAVE_NET_PFVAR_H
88 #include <sys/socket.h>
90 #include <net/pfvar.h>
91 #include <net/if_pflog.h>
94 #define offsetof(s, e) ((size_t)&((s *)0)->e)
98 #include <netdb.h> /* for "struct addrinfo" */
101 #include <pcap/namedb.h>
103 #define ETHERMTU 1500
106 #define IPPROTO_SCTP 132
109 #ifdef HAVE_OS_PROTO_H
110 #include "os-proto.h"
113 #define JMP(c) ((c)|BPF_JMP|BPF_K)
116 static jmp_buf top_ctx
;
117 static pcap_t
*bpf_pcap
;
119 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
121 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
123 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
128 static int pcap_fddipad
;
133 bpf_error(const char *fmt
, ...)
138 if (bpf_pcap
!= NULL
)
139 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
146 static void init_linktype(pcap_t
*);
148 static void init_regs(void);
149 static int alloc_reg(void);
150 static void free_reg(int);
152 static struct block
*root
;
155 * Value passed to gen_load_a() to indicate what the offset argument
159 OR_PACKET
, /* relative to the beginning of the packet */
160 OR_LINK
, /* relative to the beginning of the link-layer header */
161 OR_MACPL
, /* relative to the end of the MAC-layer header */
162 OR_NET
, /* relative to the network-layer header */
163 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
164 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
165 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
170 * As errors are handled by a longjmp, anything allocated must be freed
171 * in the longjmp handler, so it must be reachable from that handler.
172 * One thing that's allocated is the result of pcap_nametoaddrinfo();
173 * it must be freed with freeaddrinfo(). This variable points to any
174 * addrinfo structure that would need to be freed.
176 static struct addrinfo
*ai
;
180 * We divy out chunks of memory rather than call malloc each time so
181 * we don't have to worry about leaking memory. It's probably
182 * not a big deal if all this memory was wasted but if this ever
183 * goes into a library that would probably not be a good idea.
185 * XXX - this *is* in a library....
188 #define CHUNK0SIZE 1024
194 static struct chunk chunks
[NCHUNKS
];
195 static int cur_chunk
;
197 static void *newchunk(u_int
);
198 static void freechunks(void);
199 static inline struct block
*new_block(int);
200 static inline struct slist
*new_stmt(int);
201 static struct block
*gen_retblk(int);
202 static inline void syntax(void);
204 static void backpatch(struct block
*, struct block
*);
205 static void merge(struct block
*, struct block
*);
206 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
207 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
208 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
209 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
210 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
211 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
213 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
214 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
215 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
216 static struct slist
*gen_load_llrel(u_int
, u_int
);
217 static struct slist
*gen_load_macplrel(u_int
, u_int
);
218 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
219 static struct slist
*gen_loadx_iphdrlen(void);
220 static struct block
*gen_uncond(int);
221 static inline struct block
*gen_true(void);
222 static inline struct block
*gen_false(void);
223 static struct block
*gen_ether_linktype(int);
224 static struct block
*gen_ipnet_linktype(int);
225 static struct block
*gen_linux_sll_linktype(int);
226 static struct slist
*gen_load_prism_llprefixlen(void);
227 static struct slist
*gen_load_avs_llprefixlen(void);
228 static struct slist
*gen_load_radiotap_llprefixlen(void);
229 static struct slist
*gen_load_ppi_llprefixlen(void);
230 static void insert_compute_vloffsets(struct block
*);
231 static struct slist
*gen_llprefixlen(void);
232 static struct slist
*gen_off_macpl(void);
233 static int ethertype_to_ppptype(int);
234 static struct block
*gen_linktype(int);
235 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
236 static struct block
*gen_llc_linktype(int);
237 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
239 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
241 static struct block
*gen_ahostop(const u_char
*, int);
242 static struct block
*gen_ehostop(const u_char
*, int);
243 static struct block
*gen_fhostop(const u_char
*, int);
244 static struct block
*gen_thostop(const u_char
*, int);
245 static struct block
*gen_wlanhostop(const u_char
*, int);
246 static struct block
*gen_ipfchostop(const u_char
*, int);
247 static struct block
*gen_dnhostop(bpf_u_int32
, int);
248 static struct block
*gen_mpls_linktype(int);
249 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
251 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
254 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
256 static struct block
*gen_ipfrag(void);
257 static struct block
*gen_portatom(int, bpf_int32
);
258 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
260 static struct block
*gen_portatom6(int, bpf_int32
);
261 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
263 struct block
*gen_portop(int, int, int);
264 static struct block
*gen_port(int, int, int);
265 struct block
*gen_portrangeop(int, int, int, int);
266 static struct block
*gen_portrange(int, int, int, int);
268 struct block
*gen_portop6(int, int, int);
269 static struct block
*gen_port6(int, int, int);
270 struct block
*gen_portrangeop6(int, int, int, int);
271 static struct block
*gen_portrange6(int, int, int, int);
273 static int lookup_proto(const char *, int);
274 static struct block
*gen_protochain(int, int, int);
275 static struct block
*gen_proto(int, int, int);
276 static struct slist
*xfer_to_x(struct arth
*);
277 static struct slist
*xfer_to_a(struct arth
*);
278 static struct block
*gen_mac_multicast(int);
279 static struct block
*gen_len(int, int);
280 static struct block
*gen_check_802_11_data_frame(void);
282 static struct block
*gen_ppi_dlt_check(void);
283 static struct block
*gen_msg_abbrev(int type
);
294 /* XXX Round up to nearest long. */
295 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
297 /* XXX Round up to structure boundary. */
301 cp
= &chunks
[cur_chunk
];
302 if (n
> cp
->n_left
) {
303 ++cp
, k
= ++cur_chunk
;
305 bpf_error("out of memory");
306 size
= CHUNK0SIZE
<< k
;
307 cp
->m
= (void *)malloc(size
);
309 bpf_error("out of memory");
310 memset((char *)cp
->m
, 0, size
);
313 bpf_error("out of memory");
316 return (void *)((char *)cp
->m
+ cp
->n_left
);
325 for (i
= 0; i
< NCHUNKS
; ++i
)
326 if (chunks
[i
].m
!= NULL
) {
333 * A strdup whose allocations are freed after code generation is over.
337 register const char *s
;
339 int n
= strlen(s
) + 1;
340 char *cp
= newchunk(n
);
346 static inline struct block
*
352 p
= (struct block
*)newchunk(sizeof(*p
));
359 static inline struct slist
*
365 p
= (struct slist
*)newchunk(sizeof(*p
));
371 static struct block
*
375 struct block
*b
= new_block(BPF_RET
|BPF_K
);
384 bpf_error("syntax error in filter expression");
387 static bpf_u_int32 netmask
;
392 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
393 const char *buf
, int optimize
, bpf_u_int32 mask
);
396 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
397 const char *buf
, int optimize
, bpf_u_int32 mask
)
401 EnterCriticalSection(&g_PcapCompileCriticalSection
);
403 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
405 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
411 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
412 const char *buf
, int optimize
, bpf_u_int32 mask
)
415 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
416 const char *buf
, int optimize
, bpf_u_int32 mask
)
420 const char * volatile xbuf
= buf
;
428 if (setjmp(top_ctx
)) {
442 snaplen
= pcap_snapshot(p
);
444 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
445 "snaplen of 0 rejects all packets");
449 lex_init(xbuf
? xbuf
: "");
457 root
= gen_retblk(snaplen
);
459 if (optimize
&& !no_optimize
) {
462 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
463 bpf_error("expression rejects all packets");
465 program
->bf_insns
= icode_to_fcode(root
, &len
);
466 program
->bf_len
= len
;
474 * entry point for using the compiler with no pcap open
475 * pass in all the stuff that is needed explicitly instead.
478 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
479 struct bpf_program
*program
,
480 const char *buf
, int optimize
, bpf_u_int32 mask
)
485 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
488 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
494 * Clean up a "struct bpf_program" by freeing all the memory allocated
498 pcap_freecode(struct bpf_program
*program
)
501 if (program
->bf_insns
!= NULL
) {
502 free((char *)program
->bf_insns
);
503 program
->bf_insns
= NULL
;
508 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
509 * which of the jt and jf fields has been resolved and which is a pointer
510 * back to another unresolved block (or nil). At least one of the fields
511 * in each block is already resolved.
514 backpatch(list
, target
)
515 struct block
*list
, *target
;
532 * Merge the lists in b0 and b1, using the 'sense' field to indicate
533 * which of jt and jf is the link.
537 struct block
*b0
, *b1
;
539 register struct block
**p
= &b0
;
541 /* Find end of list. */
543 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
545 /* Concatenate the lists. */
553 struct block
*ppi_dlt_check
;
556 * Insert before the statements of the first (root) block any
557 * statements needed to load the lengths of any variable-length
558 * headers into registers.
560 * XXX - a fancier strategy would be to insert those before the
561 * statements of all blocks that use those lengths and that
562 * have no predecessors that use them, so that we only compute
563 * the lengths if we need them. There might be even better
564 * approaches than that.
566 * However, those strategies would be more complicated, and
567 * as we don't generate code to compute a length if the
568 * program has no tests that use the length, and as most
569 * tests will probably use those lengths, we would just
570 * postpone computing the lengths so that it's not done
571 * for tests that fail early, and it's not clear that's
574 insert_compute_vloffsets(p
->head
);
577 * For DLT_PPI captures, generate a check of the per-packet
578 * DLT value to make sure it's DLT_IEEE802_11.
580 ppi_dlt_check
= gen_ppi_dlt_check();
581 if (ppi_dlt_check
!= NULL
)
582 gen_and(ppi_dlt_check
, p
);
584 backpatch(p
, gen_retblk(snaplen
));
585 p
->sense
= !p
->sense
;
586 backpatch(p
, gen_retblk(0));
592 struct block
*b0
, *b1
;
594 backpatch(b0
, b1
->head
);
595 b0
->sense
= !b0
->sense
;
596 b1
->sense
= !b1
->sense
;
598 b1
->sense
= !b1
->sense
;
604 struct block
*b0
, *b1
;
606 b0
->sense
= !b0
->sense
;
607 backpatch(b0
, b1
->head
);
608 b0
->sense
= !b0
->sense
;
617 b
->sense
= !b
->sense
;
620 static struct block
*
621 gen_cmp(offrel
, offset
, size
, v
)
622 enum e_offrel offrel
;
626 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
629 static struct block
*
630 gen_cmp_gt(offrel
, offset
, size
, v
)
631 enum e_offrel offrel
;
635 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
638 static struct block
*
639 gen_cmp_ge(offrel
, offset
, size
, v
)
640 enum e_offrel offrel
;
644 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
647 static struct block
*
648 gen_cmp_lt(offrel
, offset
, size
, v
)
649 enum e_offrel offrel
;
653 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
656 static struct block
*
657 gen_cmp_le(offrel
, offset
, size
, v
)
658 enum e_offrel offrel
;
662 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
665 static struct block
*
666 gen_mcmp(offrel
, offset
, size
, v
, mask
)
667 enum e_offrel offrel
;
672 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
675 static struct block
*
676 gen_bcmp(offrel
, offset
, size
, v
)
677 enum e_offrel offrel
;
678 register u_int offset
, size
;
679 register const u_char
*v
;
681 register struct block
*b
, *tmp
;
685 register const u_char
*p
= &v
[size
- 4];
686 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
687 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
689 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
696 register const u_char
*p
= &v
[size
- 2];
697 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
699 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
706 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
715 * AND the field of size "size" at offset "offset" relative to the header
716 * specified by "offrel" with "mask", and compare it with the value "v"
717 * with the test specified by "jtype"; if "reverse" is true, the test
718 * should test the opposite of "jtype".
720 static struct block
*
721 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
722 enum e_offrel offrel
;
724 bpf_u_int32 offset
, size
, mask
, jtype
;
727 struct slist
*s
, *s2
;
730 s
= gen_load_a(offrel
, offset
, size
);
732 if (mask
!= 0xffffffff) {
733 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
738 b
= new_block(JMP(jtype
));
741 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
747 * Various code constructs need to know the layout of the data link
748 * layer. These variables give the necessary offsets from the beginning
749 * of the packet data.
753 * This is the offset of the beginning of the link-layer header from
754 * the beginning of the raw packet data.
756 * It's usually 0, except for 802.11 with a fixed-length radio header.
757 * (For 802.11 with a variable-length radio header, we have to generate
758 * code to compute that offset; off_ll is 0 in that case.)
763 * If there's a variable-length header preceding the link-layer header,
764 * "reg_off_ll" is the register number for a register containing the
765 * length of that header, and therefore the offset of the link-layer
766 * header from the beginning of the raw packet data. Otherwise,
767 * "reg_off_ll" is -1.
769 static int reg_off_ll
;
772 * This is the offset of the beginning of the MAC-layer header from
773 * the beginning of the link-layer header.
774 * It's usually 0, except for ATM LANE, where it's the offset, relative
775 * to the beginning of the raw packet data, of the Ethernet header.
777 static u_int off_mac
;
780 * This is the offset of the beginning of the MAC-layer payload,
781 * from the beginning of the raw packet data.
783 * I.e., it's the sum of the length of the link-layer header (without,
784 * for example, any 802.2 LLC header, so it's the MAC-layer
785 * portion of that header), plus any prefix preceding the
788 static u_int off_macpl
;
791 * This is 1 if the offset of the beginning of the MAC-layer payload
792 * from the beginning of the link-layer header is variable-length.
794 static int off_macpl_is_variable
;
797 * If the link layer has variable_length headers, "reg_off_macpl"
798 * is the register number for a register containing the length of the
799 * link-layer header plus the length of any variable-length header
800 * preceding the link-layer header. Otherwise, "reg_off_macpl"
803 static int reg_off_macpl
;
806 * "off_linktype" is the offset to information in the link-layer header
807 * giving the packet type. This offset is relative to the beginning
808 * of the link-layer header (i.e., it doesn't include off_ll).
810 * For Ethernet, it's the offset of the Ethernet type field.
812 * For link-layer types that always use 802.2 headers, it's the
813 * offset of the LLC header.
815 * For PPP, it's the offset of the PPP type field.
817 * For Cisco HDLC, it's the offset of the CHDLC type field.
819 * For BSD loopback, it's the offset of the AF_ value.
821 * For Linux cooked sockets, it's the offset of the type field.
823 * It's set to -1 for no encapsulation, in which case, IP is assumed.
825 static u_int off_linktype
;
828 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
829 * checks to check the PPP header, assumed to follow a LAN-style link-
830 * layer header and a PPPoE session header.
832 static int is_pppoes
= 0;
835 * TRUE if the link layer includes an ATM pseudo-header.
837 static int is_atm
= 0;
840 * TRUE if "lane" appeared in the filter; it causes us to generate
841 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
843 static int is_lane
= 0;
846 * These are offsets for the ATM pseudo-header.
848 static u_int off_vpi
;
849 static u_int off_vci
;
850 static u_int off_proto
;
853 * These are offsets for the MTP2 fields.
858 * These are offsets for the MTP3 fields.
860 static u_int off_sio
;
861 static u_int off_opc
;
862 static u_int off_dpc
;
863 static u_int off_sls
;
866 * This is the offset of the first byte after the ATM pseudo_header,
867 * or -1 if there is no ATM pseudo-header.
869 static u_int off_payload
;
872 * These are offsets to the beginning of the network-layer header.
873 * They are relative to the beginning of the MAC-layer payload (i.e.,
874 * they don't include off_ll or off_macpl).
876 * If the link layer never uses 802.2 LLC:
878 * "off_nl" and "off_nl_nosnap" are the same.
880 * If the link layer always uses 802.2 LLC:
882 * "off_nl" is the offset if there's a SNAP header following
885 * "off_nl_nosnap" is the offset if there's no SNAP header.
887 * If the link layer is Ethernet:
889 * "off_nl" is the offset if the packet is an Ethernet II packet
890 * (we assume no 802.3+802.2+SNAP);
892 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
893 * with an 802.2 header following it.
896 static u_int off_nl_nosnap
;
904 linktype
= pcap_datalink(p
);
906 pcap_fddipad
= p
->fddipad
;
910 * Assume it's not raw ATM with a pseudo-header, for now.
921 * And that we're not doing PPPoE.
926 * And assume we're not doing SS7.
935 * Also assume it's not 802.11.
939 off_macpl_is_variable
= 0;
943 label_stack_depth
= 0;
953 off_nl
= 0; /* XXX in reality, variable! */
954 off_nl_nosnap
= 0; /* no 802.2 LLC */
957 case DLT_ARCNET_LINUX
:
960 off_nl
= 0; /* XXX in reality, variable! */
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
966 off_macpl
= 14; /* Ethernet header length */
967 off_nl
= 0; /* Ethernet II */
968 off_nl_nosnap
= 3; /* 802.3+802.2 */
973 * SLIP doesn't have a link level type. The 16 byte
974 * header is hacked into our SLIP driver.
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 /* XXX this may be the same as the DLT_PPP_BSDOS case */
988 off_nl_nosnap
= 0; /* no 802.2 LLC */
996 off_nl_nosnap
= 0; /* no 802.2 LLC */
1003 off_nl_nosnap
= 0; /* no 802.2 LLC */
1008 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1009 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1018 * This does no include the Ethernet header, and
1019 * only covers session state.
1024 off_nl_nosnap
= 0; /* no 802.2 LLC */
1031 off_nl_nosnap
= 0; /* no 802.2 LLC */
1036 * FDDI doesn't really have a link-level type field.
1037 * We set "off_linktype" to the offset of the LLC header.
1039 * To check for Ethernet types, we assume that SSAP = SNAP
1040 * is being used and pick out the encapsulated Ethernet type.
1041 * XXX - should we generate code to check for SNAP?
1045 off_linktype
+= pcap_fddipad
;
1047 off_macpl
= 13; /* FDDI MAC header length */
1049 off_macpl
+= pcap_fddipad
;
1051 off_nl
= 8; /* 802.2+SNAP */
1052 off_nl_nosnap
= 3; /* 802.2 */
1057 * Token Ring doesn't really have a link-level type field.
1058 * We set "off_linktype" to the offset of the LLC header.
1060 * To check for Ethernet types, we assume that SSAP = SNAP
1061 * is being used and pick out the encapsulated Ethernet type.
1062 * XXX - should we generate code to check for SNAP?
1064 * XXX - the header is actually variable-length.
1065 * Some various Linux patched versions gave 38
1066 * as "off_linktype" and 40 as "off_nl"; however,
1067 * if a token ring packet has *no* routing
1068 * information, i.e. is not source-routed, the correct
1069 * values are 20 and 22, as they are in the vanilla code.
1071 * A packet is source-routed iff the uppermost bit
1072 * of the first byte of the source address, at an
1073 * offset of 8, has the uppermost bit set. If the
1074 * packet is source-routed, the total number of bytes
1075 * of routing information is 2 plus bits 0x1F00 of
1076 * the 16-bit value at an offset of 14 (shifted right
1077 * 8 - figure out which byte that is).
1080 off_macpl
= 14; /* Token Ring MAC header length */
1081 off_nl
= 8; /* 802.2+SNAP */
1082 off_nl_nosnap
= 3; /* 802.2 */
1085 case DLT_IEEE802_11
:
1086 case DLT_PRISM_HEADER
:
1087 case DLT_IEEE802_11_RADIO_AVS
:
1088 case DLT_IEEE802_11_RADIO
:
1090 * 802.11 doesn't really have a link-level type field.
1091 * We set "off_linktype" to the offset of the LLC header.
1093 * To check for Ethernet types, we assume that SSAP = SNAP
1094 * is being used and pick out the encapsulated Ethernet type.
1095 * XXX - should we generate code to check for SNAP?
1097 * We also handle variable-length radio headers here.
1098 * The Prism header is in theory variable-length, but in
1099 * practice it's always 144 bytes long. However, some
1100 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1101 * sometimes or always supply an AVS header, so we
1102 * have to check whether the radio header is a Prism
1103 * header or an AVS header, so, in practice, it's
1107 off_macpl
= 0; /* link-layer header is variable-length */
1108 off_macpl_is_variable
= 1;
1109 off_nl
= 8; /* 802.2+SNAP */
1110 off_nl_nosnap
= 3; /* 802.2 */
1115 * At the moment we treat PPI the same way that we treat
1116 * normal Radiotap encoded packets. The difference is in
1117 * the function that generates the code at the beginning
1118 * to compute the header length. Since this code generator
1119 * of PPI supports bare 802.11 encapsulation only (i.e.
1120 * the encapsulated DLT should be DLT_IEEE802_11) we
1121 * generate code to check for this too.
1124 off_macpl
= 0; /* link-layer header is variable-length */
1125 off_macpl_is_variable
= 1;
1126 off_nl
= 8; /* 802.2+SNAP */
1127 off_nl_nosnap
= 3; /* 802.2 */
1130 case DLT_ATM_RFC1483
:
1131 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1133 * assume routed, non-ISO PDUs
1134 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1136 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1137 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1138 * latter would presumably be treated the way PPPoE
1139 * should be, so you can do "pppoe and udp port 2049"
1140 * or "pppoa and tcp port 80" and have it check for
1141 * PPPo{A,E} and a PPP protocol of IP and....
1144 off_macpl
= 0; /* packet begins with LLC header */
1145 off_nl
= 8; /* 802.2+SNAP */
1146 off_nl_nosnap
= 3; /* 802.2 */
1151 * Full Frontal ATM; you get AALn PDUs with an ATM
1155 off_vpi
= SUNATM_VPI_POS
;
1156 off_vci
= SUNATM_VCI_POS
;
1157 off_proto
= PROTO_POS
;
1158 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1159 off_payload
= SUNATM_PKT_BEGIN_POS
;
1160 off_linktype
= off_payload
;
1161 off_macpl
= off_payload
; /* if LLC-encapsulated */
1162 off_nl
= 8; /* 802.2+SNAP */
1163 off_nl_nosnap
= 3; /* 802.2 */
1172 off_nl_nosnap
= 0; /* no 802.2 LLC */
1175 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1179 off_nl_nosnap
= 0; /* no 802.2 LLC */
1184 * LocalTalk does have a 1-byte type field in the LLAP header,
1185 * but really it just indicates whether there is a "short" or
1186 * "long" DDP packet following.
1191 off_nl_nosnap
= 0; /* no 802.2 LLC */
1194 case DLT_IP_OVER_FC
:
1196 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1197 * link-level type field. We set "off_linktype" to the
1198 * offset of the LLC header.
1200 * To check for Ethernet types, we assume that SSAP = SNAP
1201 * is being used and pick out the encapsulated Ethernet type.
1202 * XXX - should we generate code to check for SNAP? RFC
1203 * 2625 says SNAP should be used.
1207 off_nl
= 8; /* 802.2+SNAP */
1208 off_nl_nosnap
= 3; /* 802.2 */
1213 * XXX - we should set this to handle SNAP-encapsulated
1214 * frames (NLPID of 0x80).
1219 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 * the only BPF-interesting FRF.16 frames are non-control frames;
1224 * Frame Relay has a variable length link-layer
1225 * so lets start with offset 4 for now and increments later on (FIXME);
1231 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1234 case DLT_APPLE_IP_OVER_IEEE1394
:
1238 off_nl_nosnap
= 0; /* no 802.2 LLC */
1241 case DLT_LINUX_IRDA
:
1243 * Currently, only raw "link[N:M]" filtering is supported.
1253 * Currently, only raw "link[N:M]" filtering is supported.
1261 case DLT_SYMANTEC_FIREWALL
:
1264 off_nl
= 0; /* Ethernet II */
1265 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1268 #ifdef HAVE_NET_PFVAR_H
1271 off_macpl
= PFLOG_HDRLEN
;
1273 off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 case DLT_JUNIPER_MFR
:
1278 case DLT_JUNIPER_MLFR
:
1279 case DLT_JUNIPER_MLPPP
:
1280 case DLT_JUNIPER_PPP
:
1281 case DLT_JUNIPER_CHDLC
:
1282 case DLT_JUNIPER_FRELAY
:
1286 off_nl_nosnap
= -1; /* no 802.2 LLC */
1289 case DLT_JUNIPER_ATM1
:
1290 off_linktype
= 4; /* in reality variable between 4-8 */
1291 off_macpl
= 4; /* in reality variable between 4-8 */
1296 case DLT_JUNIPER_ATM2
:
1297 off_linktype
= 8; /* in reality variable between 8-12 */
1298 off_macpl
= 8; /* in reality variable between 8-12 */
1303 /* frames captured on a Juniper PPPoE service PIC
1304 * contain raw ethernet frames */
1305 case DLT_JUNIPER_PPPOE
:
1306 case DLT_JUNIPER_ETHER
:
1309 off_nl
= 18; /* Ethernet II */
1310 off_nl_nosnap
= 21; /* 802.3+802.2 */
1313 case DLT_JUNIPER_PPPOE_ATM
:
1317 off_nl_nosnap
= -1; /* no 802.2 LLC */
1320 case DLT_JUNIPER_GGSN
:
1324 off_nl_nosnap
= -1; /* no 802.2 LLC */
1327 case DLT_JUNIPER_ES
:
1329 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1330 off_nl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_JUNIPER_MONITOR
:
1337 off_nl
= 0; /* raw IP/IP6 header */
1338 off_nl_nosnap
= -1; /* no 802.2 LLC */
1341 case DLT_JUNIPER_SERVICES
:
1343 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1344 off_nl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl_nosnap
= -1; /* no 802.2 LLC */
1348 case DLT_JUNIPER_VP
:
1355 case DLT_JUNIPER_ST
:
1362 case DLT_JUNIPER_ISM
:
1381 case DLT_MTP2_WITH_PHDR
:
1414 case DLT_LINUX_LAPD
:
1416 * Currently, only raw "link[N:M]" filtering is supported.
1426 * Currently, only raw "link[N:M]" filtering is supported.
1434 case DLT_BLUETOOTH_HCI_H4
:
1436 * Currently, only raw "link[N:M]" filtering is supported.
1446 * Currently, only raw "link[N:M]" filtering is supported.
1456 * Currently, only raw "link[N:M]" filtering is supported.
1464 case DLT_IEEE802_15_4_LINUX
:
1466 * Currently, only raw "link[N:M]" filtering is supported.
1474 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1476 * Currently, only raw "link[N:M]" filtering is supported.
1484 case DLT_IEEE802_15_4
:
1486 * Currently, only raw "link[N:M]" filtering is supported.
1496 * Currently, only raw "link[N:M]" filtering is supported.
1506 * Currently, only raw "link[N:M]" filtering is supported.
1516 * Currently, only raw "link[N:M]" filtering is supported.
1524 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1526 * Currently, only raw "link[N:M]" filtering is supported.
1536 * Currently, only raw "link[N:M]" filtering is supported.
1538 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1540 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1541 off_nl_nosnap
= -1; /* no 802.2 LLC */
1542 off_mac
= 1; /* step over the kiss length byte */
1545 case DLT_IEEE802_15_4_NONASK_PHY
:
1547 * Currently, only raw "link[N:M]" filtering is supported.
1557 * Currently, only raw "link[N:M]" filtering is supported.
1565 case DLT_USB_LINUX_MMAPPED
:
1567 * Currently, only raw "link[N:M]" filtering is supported.
1575 case DLT_CAN_SOCKETCAN
:
1577 * Currently, only raw "link[N:M]" filtering is supported.
1587 off_macpl
= 24; /* ipnet header length */
1592 bpf_error("unknown data link type %d", linktype
);
1597 * Load a value relative to the beginning of the link-layer header.
1598 * The link-layer header doesn't necessarily begin at the beginning
1599 * of the packet data; there might be a variable-length prefix containing
1600 * radio information.
1602 static struct slist
*
1603 gen_load_llrel(offset
, size
)
1606 struct slist
*s
, *s2
;
1608 s
= gen_llprefixlen();
1611 * If "s" is non-null, it has code to arrange that the X register
1612 * contains the length of the prefix preceding the link-layer
1615 * Otherwise, the length of the prefix preceding the link-layer
1616 * header is "off_ll".
1620 * There's a variable-length prefix preceding the
1621 * link-layer header. "s" points to a list of statements
1622 * that put the length of that prefix into the X register.
1623 * do an indirect load, to use the X register as an offset.
1625 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1630 * There is no variable-length header preceding the
1631 * link-layer header; add in off_ll, which, if there's
1632 * a fixed-length header preceding the link-layer header,
1633 * is the length of that header.
1635 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1636 s
->s
.k
= offset
+ off_ll
;
1642 * Load a value relative to the beginning of the MAC-layer payload.
1644 static struct slist
*
1645 gen_load_macplrel(offset
, size
)
1648 struct slist
*s
, *s2
;
1650 s
= gen_off_macpl();
1653 * If s is non-null, the offset of the MAC-layer payload is
1654 * variable, and s points to a list of instructions that
1655 * arrange that the X register contains that offset.
1657 * Otherwise, the offset of the MAC-layer payload is constant,
1658 * and is in off_macpl.
1662 * The offset of the MAC-layer payload is in the X
1663 * register. Do an indirect load, to use the X register
1666 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1671 * The offset of the MAC-layer payload is constant,
1672 * and is in off_macpl; load the value at that offset
1673 * plus the specified offset.
1675 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1676 s
->s
.k
= off_macpl
+ offset
;
1682 * Load a value relative to the beginning of the specified header.
1684 static struct slist
*
1685 gen_load_a(offrel
, offset
, size
)
1686 enum e_offrel offrel
;
1689 struct slist
*s
, *s2
;
1694 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1699 s
= gen_load_llrel(offset
, size
);
1703 s
= gen_load_macplrel(offset
, size
);
1707 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1711 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1716 * Load the X register with the length of the IPv4 header
1717 * (plus the offset of the link-layer header, if it's
1718 * preceded by a variable-length header such as a radio
1719 * header), in bytes.
1721 s
= gen_loadx_iphdrlen();
1724 * Load the item at {offset of the MAC-layer payload} +
1725 * {offset, relative to the start of the MAC-layer
1726 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1727 * {specified offset}.
1729 * (If the offset of the MAC-layer payload is variable,
1730 * it's included in the value in the X register, and
1733 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1734 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1739 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1750 * Generate code to load into the X register the sum of the length of
1751 * the IPv4 header and any variable-length header preceding the link-layer
1754 static struct slist
*
1755 gen_loadx_iphdrlen()
1757 struct slist
*s
, *s2
;
1759 s
= gen_off_macpl();
1762 * There's a variable-length prefix preceding the
1763 * link-layer header, or the link-layer header is itself
1764 * variable-length. "s" points to a list of statements
1765 * that put the offset of the MAC-layer payload into
1768 * The 4*([k]&0xf) addressing mode can't be used, as we
1769 * don't have a constant offset, so we have to load the
1770 * value in question into the A register and add to it
1771 * the value from the X register.
1773 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1776 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1779 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1784 * The A register now contains the length of the
1785 * IP header. We need to add to it the offset of
1786 * the MAC-layer payload, which is still in the X
1787 * register, and move the result into the X register.
1789 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1790 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1793 * There is no variable-length header preceding the
1794 * link-layer header, and the link-layer header is
1795 * fixed-length; load the length of the IPv4 header,
1796 * which is at an offset of off_nl from the beginning
1797 * of the MAC-layer payload, and thus at an offset
1798 * of off_mac_pl + off_nl from the beginning of the
1801 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1802 s
->s
.k
= off_macpl
+ off_nl
;
1807 static struct block
*
1814 s
= new_stmt(BPF_LD
|BPF_IMM
);
1816 b
= new_block(JMP(BPF_JEQ
));
1822 static inline struct block
*
1825 return gen_uncond(1);
1828 static inline struct block
*
1831 return gen_uncond(0);
1835 * Byte-swap a 32-bit number.
1836 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1837 * big-endian platforms.)
1839 #define SWAPLONG(y) \
1840 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1843 * Generate code to match a particular packet type.
1845 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1846 * value, if <= ETHERMTU. We use that to determine whether to
1847 * match the type/length field or to check the type/length field for
1848 * a value <= ETHERMTU to see whether it's a type field and then do
1849 * the appropriate test.
1851 static struct block
*
1852 gen_ether_linktype(proto
)
1855 struct block
*b0
, *b1
;
1861 case LLCSAP_NETBEUI
:
1863 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1864 * so we check the DSAP and SSAP.
1866 * LLCSAP_IP checks for IP-over-802.2, rather
1867 * than IP-over-Ethernet or IP-over-SNAP.
1869 * XXX - should we check both the DSAP and the
1870 * SSAP, like this, or should we check just the
1871 * DSAP, as we do for other types <= ETHERMTU
1872 * (i.e., other SAP values)?
1874 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1876 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1877 ((proto
<< 8) | proto
));
1885 * Ethernet_II frames, which are Ethernet
1886 * frames with a frame type of ETHERTYPE_IPX;
1888 * Ethernet_802.3 frames, which are 802.3
1889 * frames (i.e., the type/length field is
1890 * a length field, <= ETHERMTU, rather than
1891 * a type field) with the first two bytes
1892 * after the Ethernet/802.3 header being
1895 * Ethernet_802.2 frames, which are 802.3
1896 * frames with an 802.2 LLC header and
1897 * with the IPX LSAP as the DSAP in the LLC
1900 * Ethernet_SNAP frames, which are 802.3
1901 * frames with an LLC header and a SNAP
1902 * header and with an OUI of 0x000000
1903 * (encapsulated Ethernet) and a protocol
1904 * ID of ETHERTYPE_IPX in the SNAP header.
1906 * XXX - should we generate the same code both
1907 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1911 * This generates code to check both for the
1912 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1914 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1915 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1919 * Now we add code to check for SNAP frames with
1920 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1922 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1926 * Now we generate code to check for 802.3
1927 * frames in general.
1929 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1933 * Now add the check for 802.3 frames before the
1934 * check for Ethernet_802.2 and Ethernet_802.3,
1935 * as those checks should only be done on 802.3
1936 * frames, not on Ethernet frames.
1941 * Now add the check for Ethernet_II frames, and
1942 * do that before checking for the other frame
1945 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1946 (bpf_int32
)ETHERTYPE_IPX
);
1950 case ETHERTYPE_ATALK
:
1951 case ETHERTYPE_AARP
:
1953 * EtherTalk (AppleTalk protocols on Ethernet link
1954 * layer) may use 802.2 encapsulation.
1958 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1959 * we check for an Ethernet type field less than
1960 * 1500, which means it's an 802.3 length field.
1962 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1966 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1967 * SNAP packets with an organization code of
1968 * 0x080007 (Apple, for Appletalk) and a protocol
1969 * type of ETHERTYPE_ATALK (Appletalk).
1971 * 802.2-encapsulated ETHERTYPE_AARP packets are
1972 * SNAP packets with an organization code of
1973 * 0x000000 (encapsulated Ethernet) and a protocol
1974 * type of ETHERTYPE_AARP (Appletalk ARP).
1976 if (proto
== ETHERTYPE_ATALK
)
1977 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1978 else /* proto == ETHERTYPE_AARP */
1979 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1983 * Check for Ethernet encapsulation (Ethertalk
1984 * phase 1?); we just check for the Ethernet
1987 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1993 if (proto
<= ETHERMTU
) {
1995 * This is an LLC SAP value, so the frames
1996 * that match would be 802.2 frames.
1997 * Check that the frame is an 802.2 frame
1998 * (i.e., that the length/type field is
1999 * a length field, <= ETHERMTU) and
2000 * then check the DSAP.
2002 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
2004 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
2010 * This is an Ethernet type, so compare
2011 * the length/type field with it (if
2012 * the frame is an 802.2 frame, the length
2013 * field will be <= ETHERMTU, and, as
2014 * "proto" is > ETHERMTU, this test
2015 * will fail and the frame won't match,
2016 * which is what we want).
2018 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2025 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2026 * or IPv6 then we have an error.
2028 static struct block
*
2029 gen_ipnet_linktype(proto
)
2035 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2036 (bpf_int32
)IPH_AF_INET
);
2039 case ETHERTYPE_IPV6
:
2040 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2041 (bpf_int32
)IPH_AF_INET6
);
2052 * Generate code to match a particular packet type.
2054 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2055 * value, if <= ETHERMTU. We use that to determine whether to
2056 * match the type field or to check the type field for the special
2057 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2059 static struct block
*
2060 gen_linux_sll_linktype(proto
)
2063 struct block
*b0
, *b1
;
2069 case LLCSAP_NETBEUI
:
2071 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2072 * so we check the DSAP and SSAP.
2074 * LLCSAP_IP checks for IP-over-802.2, rather
2075 * than IP-over-Ethernet or IP-over-SNAP.
2077 * XXX - should we check both the DSAP and the
2078 * SSAP, like this, or should we check just the
2079 * DSAP, as we do for other types <= ETHERMTU
2080 * (i.e., other SAP values)?
2082 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2083 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2084 ((proto
<< 8) | proto
));
2090 * Ethernet_II frames, which are Ethernet
2091 * frames with a frame type of ETHERTYPE_IPX;
2093 * Ethernet_802.3 frames, which have a frame
2094 * type of LINUX_SLL_P_802_3;
2096 * Ethernet_802.2 frames, which are 802.3
2097 * frames with an 802.2 LLC header (i.e, have
2098 * a frame type of LINUX_SLL_P_802_2) and
2099 * with the IPX LSAP as the DSAP in the LLC
2102 * Ethernet_SNAP frames, which are 802.3
2103 * frames with an LLC header and a SNAP
2104 * header and with an OUI of 0x000000
2105 * (encapsulated Ethernet) and a protocol
2106 * ID of ETHERTYPE_IPX in the SNAP header.
2108 * First, do the checks on LINUX_SLL_P_802_2
2109 * frames; generate the check for either
2110 * Ethernet_802.2 or Ethernet_SNAP frames, and
2111 * then put a check for LINUX_SLL_P_802_2 frames
2114 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2115 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2117 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2121 * Now check for 802.3 frames and OR that with
2122 * the previous test.
2124 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2128 * Now add the check for Ethernet_II frames, and
2129 * do that before checking for the other frame
2132 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2133 (bpf_int32
)ETHERTYPE_IPX
);
2137 case ETHERTYPE_ATALK
:
2138 case ETHERTYPE_AARP
:
2140 * EtherTalk (AppleTalk protocols on Ethernet link
2141 * layer) may use 802.2 encapsulation.
2145 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2146 * we check for the 802.2 protocol type in the
2147 * "Ethernet type" field.
2149 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2152 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2153 * SNAP packets with an organization code of
2154 * 0x080007 (Apple, for Appletalk) and a protocol
2155 * type of ETHERTYPE_ATALK (Appletalk).
2157 * 802.2-encapsulated ETHERTYPE_AARP packets are
2158 * SNAP packets with an organization code of
2159 * 0x000000 (encapsulated Ethernet) and a protocol
2160 * type of ETHERTYPE_AARP (Appletalk ARP).
2162 if (proto
== ETHERTYPE_ATALK
)
2163 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2164 else /* proto == ETHERTYPE_AARP */
2165 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2169 * Check for Ethernet encapsulation (Ethertalk
2170 * phase 1?); we just check for the Ethernet
2173 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2179 if (proto
<= ETHERMTU
) {
2181 * This is an LLC SAP value, so the frames
2182 * that match would be 802.2 frames.
2183 * Check for the 802.2 protocol type
2184 * in the "Ethernet type" field, and
2185 * then check the DSAP.
2187 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2189 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2195 * This is an Ethernet type, so compare
2196 * the length/type field with it (if
2197 * the frame is an 802.2 frame, the length
2198 * field will be <= ETHERMTU, and, as
2199 * "proto" is > ETHERMTU, this test
2200 * will fail and the frame won't match,
2201 * which is what we want).
2203 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2209 static struct slist
*
2210 gen_load_prism_llprefixlen()
2212 struct slist
*s1
, *s2
;
2213 struct slist
*sjeq_avs_cookie
;
2214 struct slist
*sjcommon
;
2217 * This code is not compatible with the optimizer, as
2218 * we are generating jmp instructions within a normal
2219 * slist of instructions
2224 * Generate code to load the length of the radio header into
2225 * the register assigned to hold that length, if one has been
2226 * assigned. (If one hasn't been assigned, no code we've
2227 * generated uses that prefix, so we don't need to generate any
2230 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2231 * or always use the AVS header rather than the Prism header.
2232 * We load a 4-byte big-endian value at the beginning of the
2233 * raw packet data, and see whether, when masked with 0xFFFFF000,
2234 * it's equal to 0x80211000. If so, that indicates that it's
2235 * an AVS header (the masked-out bits are the version number).
2236 * Otherwise, it's a Prism header.
2238 * XXX - the Prism header is also, in theory, variable-length,
2239 * but no known software generates headers that aren't 144
2242 if (reg_off_ll
!= -1) {
2246 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2250 * AND it with 0xFFFFF000.
2252 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2253 s2
->s
.k
= 0xFFFFF000;
2257 * Compare with 0x80211000.
2259 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2260 sjeq_avs_cookie
->s
.k
= 0x80211000;
2261 sappend(s1
, sjeq_avs_cookie
);
2266 * The 4 bytes at an offset of 4 from the beginning of
2267 * the AVS header are the length of the AVS header.
2268 * That field is big-endian.
2270 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2273 sjeq_avs_cookie
->s
.jt
= s2
;
2276 * Now jump to the code to allocate a register
2277 * into which to save the header length and
2278 * store the length there. (The "jump always"
2279 * instruction needs to have the k field set;
2280 * it's added to the PC, so, as we're jumping
2281 * over a single instruction, it should be 1.)
2283 sjcommon
= new_stmt(JMP(BPF_JA
));
2285 sappend(s1
, sjcommon
);
2288 * Now for the code that handles the Prism header.
2289 * Just load the length of the Prism header (144)
2290 * into the A register. Have the test for an AVS
2291 * header branch here if we don't have an AVS header.
2293 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2296 sjeq_avs_cookie
->s
.jf
= s2
;
2299 * Now allocate a register to hold that value and store
2300 * it. The code for the AVS header will jump here after
2301 * loading the length of the AVS header.
2303 s2
= new_stmt(BPF_ST
);
2304 s2
->s
.k
= reg_off_ll
;
2306 sjcommon
->s
.jf
= s2
;
2309 * Now move it into the X register.
2311 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2319 static struct slist
*
2320 gen_load_avs_llprefixlen()
2322 struct slist
*s1
, *s2
;
2325 * Generate code to load the length of the AVS header into
2326 * the register assigned to hold that length, if one has been
2327 * assigned. (If one hasn't been assigned, no code we've
2328 * generated uses that prefix, so we don't need to generate any
2331 if (reg_off_ll
!= -1) {
2333 * The 4 bytes at an offset of 4 from the beginning of
2334 * the AVS header are the length of the AVS header.
2335 * That field is big-endian.
2337 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2341 * Now allocate a register to hold that value and store
2344 s2
= new_stmt(BPF_ST
);
2345 s2
->s
.k
= reg_off_ll
;
2349 * Now move it into the X register.
2351 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2359 static struct slist
*
2360 gen_load_radiotap_llprefixlen()
2362 struct slist
*s1
, *s2
;
2365 * Generate code to load the length of the radiotap header into
2366 * the register assigned to hold that length, if one has been
2367 * assigned. (If one hasn't been assigned, no code we've
2368 * generated uses that prefix, so we don't need to generate any
2371 if (reg_off_ll
!= -1) {
2373 * The 2 bytes at offsets of 2 and 3 from the beginning
2374 * of the radiotap header are the length of the radiotap
2375 * header; unfortunately, it's little-endian, so we have
2376 * to load it a byte at a time and construct the value.
2380 * Load the high-order byte, at an offset of 3, shift it
2381 * left a byte, and put the result in the X register.
2383 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2385 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2388 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2392 * Load the next byte, at an offset of 2, and OR the
2393 * value from the X register into it.
2395 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2398 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2402 * Now allocate a register to hold that value and store
2405 s2
= new_stmt(BPF_ST
);
2406 s2
->s
.k
= reg_off_ll
;
2410 * Now move it into the X register.
2412 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2421 * At the moment we treat PPI as normal Radiotap encoded
2422 * packets. The difference is in the function that generates
2423 * the code at the beginning to compute the header length.
2424 * Since this code generator of PPI supports bare 802.11
2425 * encapsulation only (i.e. the encapsulated DLT should be
2426 * DLT_IEEE802_11) we generate code to check for this too;
2427 * that's done in finish_parse().
2429 static struct slist
*
2430 gen_load_ppi_llprefixlen()
2432 struct slist
*s1
, *s2
;
2435 * Generate code to load the length of the radiotap header
2436 * into the register assigned to hold that length, if one has
2439 if (reg_off_ll
!= -1) {
2441 * The 2 bytes at offsets of 2 and 3 from the beginning
2442 * of the radiotap header are the length of the radiotap
2443 * header; unfortunately, it's little-endian, so we have
2444 * to load it a byte at a time and construct the value.
2448 * Load the high-order byte, at an offset of 3, shift it
2449 * left a byte, and put the result in the X register.
2451 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2453 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2456 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2460 * Load the next byte, at an offset of 2, and OR the
2461 * value from the X register into it.
2463 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2466 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2470 * Now allocate a register to hold that value and store
2473 s2
= new_stmt(BPF_ST
);
2474 s2
->s
.k
= reg_off_ll
;
2478 * Now move it into the X register.
2480 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2489 * Load a value relative to the beginning of the link-layer header after the 802.11
2490 * header, i.e. LLC_SNAP.
2491 * The link-layer header doesn't necessarily begin at the beginning
2492 * of the packet data; there might be a variable-length prefix containing
2493 * radio information.
2495 static struct slist
*
2496 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2499 struct slist
*sjset_data_frame_1
;
2500 struct slist
*sjset_data_frame_2
;
2501 struct slist
*sjset_qos
;
2502 struct slist
*sjset_radiotap_flags
;
2503 struct slist
*sjset_radiotap_tsft
;
2504 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2505 struct slist
*s_roundup
;
2507 if (reg_off_macpl
== -1) {
2509 * No register has been assigned to the offset of
2510 * the MAC-layer payload, which means nobody needs
2511 * it; don't bother computing it - just return
2512 * what we already have.
2518 * This code is not compatible with the optimizer, as
2519 * we are generating jmp instructions within a normal
2520 * slist of instructions
2525 * If "s" is non-null, it has code to arrange that the X register
2526 * contains the length of the prefix preceding the link-layer
2529 * Otherwise, the length of the prefix preceding the link-layer
2530 * header is "off_ll".
2534 * There is no variable-length header preceding the
2535 * link-layer header.
2537 * Load the length of the fixed-length prefix preceding
2538 * the link-layer header (if any) into the X register,
2539 * and store it in the reg_off_macpl register.
2540 * That length is off_ll.
2542 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2547 * The X register contains the offset of the beginning of the
2548 * link-layer header; add 24, which is the minimum length
2549 * of the MAC header for a data frame, to that, and store it
2550 * in reg_off_macpl, and then load the Frame Control field,
2551 * which is at the offset in the X register, with an indexed load.
2553 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2555 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2558 s2
= new_stmt(BPF_ST
);
2559 s2
->s
.k
= reg_off_macpl
;
2562 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2567 * Check the Frame Control field to see if this is a data frame;
2568 * a data frame has the 0x08 bit (b3) in that field set and the
2569 * 0x04 bit (b2) clear.
2571 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2572 sjset_data_frame_1
->s
.k
= 0x08;
2573 sappend(s
, sjset_data_frame_1
);
2576 * If b3 is set, test b2, otherwise go to the first statement of
2577 * the rest of the program.
2579 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2580 sjset_data_frame_2
->s
.k
= 0x04;
2581 sappend(s
, sjset_data_frame_2
);
2582 sjset_data_frame_1
->s
.jf
= snext
;
2585 * If b2 is not set, this is a data frame; test the QoS bit.
2586 * Otherwise, go to the first statement of the rest of the
2589 sjset_data_frame_2
->s
.jt
= snext
;
2590 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2591 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2592 sappend(s
, sjset_qos
);
2595 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2597 * Otherwise, go to the first statement of the rest of the
2600 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2601 s2
->s
.k
= reg_off_macpl
;
2603 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2606 s2
= new_stmt(BPF_ST
);
2607 s2
->s
.k
= reg_off_macpl
;
2611 * If we have a radiotap header, look at it to see whether
2612 * there's Atheros padding between the MAC-layer header
2615 * Note: all of the fields in the radiotap header are
2616 * little-endian, so we byte-swap all of the values
2617 * we test against, as they will be loaded as big-endian
2620 if (linktype
== DLT_IEEE802_11_RADIO
) {
2622 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2623 * in the presence flag?
2625 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2629 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2630 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2631 sappend(s
, sjset_radiotap_flags
);
2634 * If not, skip all of this.
2636 sjset_radiotap_flags
->s
.jf
= snext
;
2639 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2641 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2642 new_stmt(JMP(BPF_JSET
));
2643 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2644 sappend(s
, sjset_radiotap_tsft
);
2647 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2648 * at an offset of 16 from the beginning of the raw packet
2649 * data (8 bytes for the radiotap header and 8 bytes for
2652 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2655 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2659 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2660 sjset_tsft_datapad
->s
.k
= 0x20;
2661 sappend(s
, sjset_tsft_datapad
);
2664 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2665 * at an offset of 8 from the beginning of the raw packet
2666 * data (8 bytes for the radiotap header).
2668 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2671 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2675 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2676 sjset_notsft_datapad
->s
.k
= 0x20;
2677 sappend(s
, sjset_notsft_datapad
);
2680 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2681 * set, round the length of the 802.11 header to
2682 * a multiple of 4. Do that by adding 3 and then
2683 * dividing by and multiplying by 4, which we do by
2686 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2687 s_roundup
->s
.k
= reg_off_macpl
;
2688 sappend(s
, s_roundup
);
2689 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2692 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2695 s2
= new_stmt(BPF_ST
);
2696 s2
->s
.k
= reg_off_macpl
;
2699 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2700 sjset_tsft_datapad
->s
.jf
= snext
;
2701 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2702 sjset_notsft_datapad
->s
.jf
= snext
;
2704 sjset_qos
->s
.jf
= snext
;
2710 insert_compute_vloffsets(b
)
2716 * For link-layer types that have a variable-length header
2717 * preceding the link-layer header, generate code to load
2718 * the offset of the link-layer header into the register
2719 * assigned to that offset, if any.
2723 case DLT_PRISM_HEADER
:
2724 s
= gen_load_prism_llprefixlen();
2727 case DLT_IEEE802_11_RADIO_AVS
:
2728 s
= gen_load_avs_llprefixlen();
2731 case DLT_IEEE802_11_RADIO
:
2732 s
= gen_load_radiotap_llprefixlen();
2736 s
= gen_load_ppi_llprefixlen();
2745 * For link-layer types that have a variable-length link-layer
2746 * header, generate code to load the offset of the MAC-layer
2747 * payload into the register assigned to that offset, if any.
2751 case DLT_IEEE802_11
:
2752 case DLT_PRISM_HEADER
:
2753 case DLT_IEEE802_11_RADIO_AVS
:
2754 case DLT_IEEE802_11_RADIO
:
2756 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2761 * If we have any offset-loading code, append all the
2762 * existing statements in the block to those statements,
2763 * and make the resulting list the list of statements
2767 sappend(s
, b
->stmts
);
2772 static struct block
*
2773 gen_ppi_dlt_check(void)
2775 struct slist
*s_load_dlt
;
2778 if (linktype
== DLT_PPI
)
2780 /* Create the statements that check for the DLT
2782 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2783 s_load_dlt
->s
.k
= 4;
2785 b
= new_block(JMP(BPF_JEQ
));
2787 b
->stmts
= s_load_dlt
;
2788 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2798 static struct slist
*
2799 gen_prism_llprefixlen(void)
2803 if (reg_off_ll
== -1) {
2805 * We haven't yet assigned a register for the length
2806 * of the radio header; allocate one.
2808 reg_off_ll
= alloc_reg();
2812 * Load the register containing the radio length
2813 * into the X register.
2815 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2816 s
->s
.k
= reg_off_ll
;
2820 static struct slist
*
2821 gen_avs_llprefixlen(void)
2825 if (reg_off_ll
== -1) {
2827 * We haven't yet assigned a register for the length
2828 * of the AVS header; allocate one.
2830 reg_off_ll
= alloc_reg();
2834 * Load the register containing the AVS length
2835 * into the X register.
2837 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2838 s
->s
.k
= reg_off_ll
;
2842 static struct slist
*
2843 gen_radiotap_llprefixlen(void)
2847 if (reg_off_ll
== -1) {
2849 * We haven't yet assigned a register for the length
2850 * of the radiotap header; allocate one.
2852 reg_off_ll
= alloc_reg();
2856 * Load the register containing the radiotap length
2857 * into the X register.
2859 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2860 s
->s
.k
= reg_off_ll
;
2865 * At the moment we treat PPI as normal Radiotap encoded
2866 * packets. The difference is in the function that generates
2867 * the code at the beginning to compute the header length.
2868 * Since this code generator of PPI supports bare 802.11
2869 * encapsulation only (i.e. the encapsulated DLT should be
2870 * DLT_IEEE802_11) we generate code to check for this too.
2872 static struct slist
*
2873 gen_ppi_llprefixlen(void)
2877 if (reg_off_ll
== -1) {
2879 * We haven't yet assigned a register for the length
2880 * of the radiotap header; allocate one.
2882 reg_off_ll
= alloc_reg();
2886 * Load the register containing the PPI length
2887 * into the X register.
2889 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2890 s
->s
.k
= reg_off_ll
;
2895 * Generate code to compute the link-layer header length, if necessary,
2896 * putting it into the X register, and to return either a pointer to a
2897 * "struct slist" for the list of statements in that code, or NULL if
2898 * no code is necessary.
2900 static struct slist
*
2901 gen_llprefixlen(void)
2905 case DLT_PRISM_HEADER
:
2906 return gen_prism_llprefixlen();
2908 case DLT_IEEE802_11_RADIO_AVS
:
2909 return gen_avs_llprefixlen();
2911 case DLT_IEEE802_11_RADIO
:
2912 return gen_radiotap_llprefixlen();
2915 return gen_ppi_llprefixlen();
2923 * Generate code to load the register containing the offset of the
2924 * MAC-layer payload into the X register; if no register for that offset
2925 * has been allocated, allocate it first.
2927 static struct slist
*
2932 if (off_macpl_is_variable
) {
2933 if (reg_off_macpl
== -1) {
2935 * We haven't yet assigned a register for the offset
2936 * of the MAC-layer payload; allocate one.
2938 reg_off_macpl
= alloc_reg();
2942 * Load the register containing the offset of the MAC-layer
2943 * payload into the X register.
2945 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2946 s
->s
.k
= reg_off_macpl
;
2950 * That offset isn't variable, so we don't need to
2951 * generate any code.
2958 * Map an Ethernet type to the equivalent PPP type.
2961 ethertype_to_ppptype(proto
)
2971 case ETHERTYPE_IPV6
:
2980 case ETHERTYPE_ATALK
:
2994 * I'm assuming the "Bridging PDU"s that go
2995 * over PPP are Spanning Tree Protocol
3009 * Generate code to match a particular packet type by matching the
3010 * link-layer type field or fields in the 802.2 LLC header.
3012 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3013 * value, if <= ETHERMTU.
3015 static struct block
*
3019 struct block
*b0
, *b1
, *b2
;
3021 /* are we checking MPLS-encapsulated packets? */
3022 if (label_stack_depth
> 0) {
3026 /* FIXME add other L3 proto IDs */
3027 return gen_mpls_linktype(Q_IP
);
3029 case ETHERTYPE_IPV6
:
3031 /* FIXME add other L3 proto IDs */
3032 return gen_mpls_linktype(Q_IPV6
);
3035 bpf_error("unsupported protocol over mpls");
3041 * Are we testing PPPoE packets?
3045 * The PPPoE session header is part of the
3046 * MAC-layer payload, so all references
3047 * should be relative to the beginning of
3052 * We use Ethernet protocol types inside libpcap;
3053 * map them to the corresponding PPP protocol types.
3055 proto
= ethertype_to_ppptype(proto
);
3056 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3062 return gen_ether_linktype(proto
);
3070 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3074 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3081 case DLT_IEEE802_11
:
3082 case DLT_PRISM_HEADER
:
3083 case DLT_IEEE802_11_RADIO_AVS
:
3084 case DLT_IEEE802_11_RADIO
:
3087 * Check that we have a data frame.
3089 b0
= gen_check_802_11_data_frame();
3092 * Now check for the specified link-layer type.
3094 b1
= gen_llc_linktype(proto
);
3102 * XXX - check for asynchronous frames, as per RFC 1103.
3104 return gen_llc_linktype(proto
);
3110 * XXX - check for LLC PDUs, as per IEEE 802.5.
3112 return gen_llc_linktype(proto
);
3116 case DLT_ATM_RFC1483
:
3118 case DLT_IP_OVER_FC
:
3119 return gen_llc_linktype(proto
);
3125 * If "is_lane" is set, check for a LANE-encapsulated
3126 * version of this protocol, otherwise check for an
3127 * LLC-encapsulated version of this protocol.
3129 * We assume LANE means Ethernet, not Token Ring.
3133 * Check that the packet doesn't begin with an
3134 * LE Control marker. (We've already generated
3137 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3142 * Now generate an Ethernet test.
3144 b1
= gen_ether_linktype(proto
);
3149 * Check for LLC encapsulation and then check the
3152 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3153 b1
= gen_llc_linktype(proto
);
3161 return gen_linux_sll_linktype(proto
);
3166 case DLT_SLIP_BSDOS
:
3169 * These types don't provide any type field; packets
3170 * are always IPv4 or IPv6.
3172 * XXX - for IPv4, check for a version number of 4, and,
3173 * for IPv6, check for a version number of 6?
3178 /* Check for a version number of 4. */
3179 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3181 case ETHERTYPE_IPV6
:
3182 /* Check for a version number of 6. */
3183 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3187 return gen_false(); /* always false */
3194 * Raw IPv4, so no type field.
3196 if (proto
== ETHERTYPE_IP
)
3197 return gen_true(); /* always true */
3199 /* Checking for something other than IPv4; always false */
3206 * Raw IPv6, so no type field.
3209 if (proto
== ETHERTYPE_IPV6
)
3210 return gen_true(); /* always true */
3213 /* Checking for something other than IPv6; always false */
3220 case DLT_PPP_SERIAL
:
3223 * We use Ethernet protocol types inside libpcap;
3224 * map them to the corresponding PPP protocol types.
3226 proto
= ethertype_to_ppptype(proto
);
3227 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3233 * We use Ethernet protocol types inside libpcap;
3234 * map them to the corresponding PPP protocol types.
3240 * Also check for Van Jacobson-compressed IP.
3241 * XXX - do this for other forms of PPP?
3243 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3244 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3246 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3251 proto
= ethertype_to_ppptype(proto
);
3252 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3262 * For DLT_NULL, the link-layer header is a 32-bit
3263 * word containing an AF_ value in *host* byte order,
3264 * and for DLT_ENC, the link-layer header begins
3265 * with a 32-bit work containing an AF_ value in
3268 * In addition, if we're reading a saved capture file,
3269 * the host byte order in the capture may not be the
3270 * same as the host byte order on this machine.
3272 * For DLT_LOOP, the link-layer header is a 32-bit
3273 * word containing an AF_ value in *network* byte order.
3275 * XXX - AF_ values may, unfortunately, be platform-
3276 * dependent; for example, FreeBSD's AF_INET6 is 24
3277 * whilst NetBSD's and OpenBSD's is 26.
3279 * This means that, when reading a capture file, just
3280 * checking for our AF_INET6 value won't work if the
3281 * capture file came from another OS.
3290 case ETHERTYPE_IPV6
:
3297 * Not a type on which we support filtering.
3298 * XXX - support those that have AF_ values
3299 * #defined on this platform, at least?
3304 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3306 * The AF_ value is in host byte order, but
3307 * the BPF interpreter will convert it to
3308 * network byte order.
3310 * If this is a save file, and it's from a
3311 * machine with the opposite byte order to
3312 * ours, we byte-swap the AF_ value.
3314 * Then we run it through "htonl()", and
3315 * generate code to compare against the result.
3317 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3318 bpf_pcap
->sf
.swapped
)
3319 proto
= SWAPLONG(proto
);
3320 proto
= htonl(proto
);
3322 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3324 #ifdef HAVE_NET_PFVAR_H
3327 * af field is host byte order in contrast to the rest of
3330 if (proto
== ETHERTYPE_IP
)
3331 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3332 BPF_B
, (bpf_int32
)AF_INET
));
3334 else if (proto
== ETHERTYPE_IPV6
)
3335 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3336 BPF_B
, (bpf_int32
)AF_INET6
));
3342 #endif /* HAVE_NET_PFVAR_H */
3345 case DLT_ARCNET_LINUX
:
3347 * XXX should we check for first fragment if the protocol
3356 case ETHERTYPE_IPV6
:
3357 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3358 (bpf_int32
)ARCTYPE_INET6
));
3362 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3363 (bpf_int32
)ARCTYPE_IP
);
3364 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3365 (bpf_int32
)ARCTYPE_IP_OLD
);
3370 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3371 (bpf_int32
)ARCTYPE_ARP
);
3372 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3373 (bpf_int32
)ARCTYPE_ARP_OLD
);
3377 case ETHERTYPE_REVARP
:
3378 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3379 (bpf_int32
)ARCTYPE_REVARP
));
3381 case ETHERTYPE_ATALK
:
3382 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3383 (bpf_int32
)ARCTYPE_ATALK
));
3390 case ETHERTYPE_ATALK
:
3400 * XXX - assumes a 2-byte Frame Relay header with
3401 * DLCI and flags. What if the address is longer?
3407 * Check for the special NLPID for IP.
3409 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3412 case ETHERTYPE_IPV6
:
3414 * Check for the special NLPID for IPv6.
3416 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3421 * Check for several OSI protocols.
3423 * Frame Relay packets typically have an OSI
3424 * NLPID at the beginning; we check for each
3427 * What we check for is the NLPID and a frame
3428 * control field of UI, i.e. 0x03 followed
3431 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3432 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3433 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3445 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3447 case DLT_JUNIPER_MFR
:
3448 case DLT_JUNIPER_MLFR
:
3449 case DLT_JUNIPER_MLPPP
:
3450 case DLT_JUNIPER_ATM1
:
3451 case DLT_JUNIPER_ATM2
:
3452 case DLT_JUNIPER_PPPOE
:
3453 case DLT_JUNIPER_PPPOE_ATM
:
3454 case DLT_JUNIPER_GGSN
:
3455 case DLT_JUNIPER_ES
:
3456 case DLT_JUNIPER_MONITOR
:
3457 case DLT_JUNIPER_SERVICES
:
3458 case DLT_JUNIPER_ETHER
:
3459 case DLT_JUNIPER_PPP
:
3460 case DLT_JUNIPER_FRELAY
:
3461 case DLT_JUNIPER_CHDLC
:
3462 case DLT_JUNIPER_VP
:
3463 case DLT_JUNIPER_ST
:
3464 case DLT_JUNIPER_ISM
:
3465 /* just lets verify the magic number for now -
3466 * on ATM we may have up to 6 different encapsulations on the wire
3467 * and need a lot of heuristics to figure out that the payload
3470 * FIXME encapsulation specific BPF_ filters
3472 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3475 return gen_ipnet_linktype(proto
);
3477 case DLT_LINUX_IRDA
:
3478 bpf_error("IrDA link-layer type filtering not implemented");
3481 bpf_error("DOCSIS link-layer type filtering not implemented");
3484 case DLT_MTP2_WITH_PHDR
:
3485 bpf_error("MTP2 link-layer type filtering not implemented");
3488 bpf_error("ERF link-layer type filtering not implemented");
3492 bpf_error("PFSYNC link-layer type filtering not implemented");
3495 case DLT_LINUX_LAPD
:
3496 bpf_error("LAPD link-layer type filtering not implemented");
3500 case DLT_USB_LINUX_MMAPPED
:
3501 bpf_error("USB link-layer type filtering not implemented");
3503 case DLT_BLUETOOTH_HCI_H4
:
3504 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3505 bpf_error("Bluetooth link-layer type filtering not implemented");
3508 case DLT_CAN_SOCKETCAN
:
3509 bpf_error("CAN link-layer type filtering not implemented");
3511 case DLT_IEEE802_15_4
:
3512 case DLT_IEEE802_15_4_LINUX
:
3513 case DLT_IEEE802_15_4_NONASK_PHY
:
3514 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3516 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3517 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3520 bpf_error("SITA link-layer type filtering not implemented");
3523 bpf_error("RAIF1 link-layer type filtering not implemented");
3526 bpf_error("IPMB link-layer type filtering not implemented");
3529 bpf_error("AX.25 link-layer type filtering not implemented");
3533 * All the types that have no encapsulation should either be
3534 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3535 * all packets are IP packets, or should be handled in some
3536 * special case, if none of them are (if some are and some
3537 * aren't, the lack of encapsulation is a problem, as we'd
3538 * have to find some other way of determining the packet type).
3540 * Therefore, if "off_linktype" is -1, there's an error.
3542 if (off_linktype
== (u_int
)-1)
3546 * Any type not handled above should always have an Ethernet
3547 * type at an offset of "off_linktype".
3549 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3553 * Check for an LLC SNAP packet with a given organization code and
3554 * protocol type; we check the entire contents of the 802.2 LLC and
3555 * snap headers, checking for DSAP and SSAP of SNAP and a control
3556 * field of 0x03 in the LLC header, and for the specified organization
3557 * code and protocol type in the SNAP header.
3559 static struct block
*
3560 gen_snap(orgcode
, ptype
)
3561 bpf_u_int32 orgcode
;
3564 u_char snapblock
[8];
3566 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3567 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3568 snapblock
[2] = 0x03; /* control = UI */
3569 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3570 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3571 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3572 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3573 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3574 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3578 * Generate code to match a particular packet type, for link-layer types
3579 * using 802.2 LLC headers.
3581 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3582 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3584 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3585 * value, if <= ETHERMTU. We use that to determine whether to
3586 * match the DSAP or both DSAP and LSAP or to check the OUI and
3587 * protocol ID in a SNAP header.
3589 static struct block
*
3590 gen_llc_linktype(proto
)
3594 * XXX - handle token-ring variable-length header.
3600 case LLCSAP_NETBEUI
:
3602 * XXX - should we check both the DSAP and the
3603 * SSAP, like this, or should we check just the
3604 * DSAP, as we do for other types <= ETHERMTU
3605 * (i.e., other SAP values)?
3607 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3608 ((proto
<< 8) | proto
));
3612 * XXX - are there ever SNAP frames for IPX on
3613 * non-Ethernet 802.x networks?
3615 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3616 (bpf_int32
)LLCSAP_IPX
);
3618 case ETHERTYPE_ATALK
:
3620 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3621 * SNAP packets with an organization code of
3622 * 0x080007 (Apple, for Appletalk) and a protocol
3623 * type of ETHERTYPE_ATALK (Appletalk).
3625 * XXX - check for an organization code of
3626 * encapsulated Ethernet as well?
3628 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3632 * XXX - we don't have to check for IPX 802.3
3633 * here, but should we check for the IPX Ethertype?
3635 if (proto
<= ETHERMTU
) {
3637 * This is an LLC SAP value, so check
3640 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3643 * This is an Ethernet type; we assume that it's
3644 * unlikely that it'll appear in the right place
3645 * at random, and therefore check only the
3646 * location that would hold the Ethernet type
3647 * in a SNAP frame with an organization code of
3648 * 0x000000 (encapsulated Ethernet).
3650 * XXX - if we were to check for the SNAP DSAP and
3651 * LSAP, as per XXX, and were also to check for an
3652 * organization code of 0x000000 (encapsulated
3653 * Ethernet), we'd do
3655 * return gen_snap(0x000000, proto);
3657 * here; for now, we don't, as per the above.
3658 * I don't know whether it's worth the extra CPU
3659 * time to do the right check or not.
3661 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3666 static struct block
*
3667 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3671 u_int src_off
, dst_off
;
3673 struct block
*b0
, *b1
;
3687 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3688 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3694 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3695 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3702 b0
= gen_linktype(proto
);
3703 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3709 static struct block
*
3710 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3711 struct in6_addr
*addr
;
3712 struct in6_addr
*mask
;
3714 u_int src_off
, dst_off
;
3716 struct block
*b0
, *b1
;
3731 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3732 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3738 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3739 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3746 /* this order is important */
3747 a
= (u_int32_t
*)addr
;
3748 m
= (u_int32_t
*)mask
;
3749 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3750 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3752 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3754 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3756 b0
= gen_linktype(proto
);
3762 static struct block
*
3763 gen_ehostop(eaddr
, dir
)
3764 register const u_char
*eaddr
;
3767 register struct block
*b0
, *b1
;
3771 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3774 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3777 b0
= gen_ehostop(eaddr
, Q_SRC
);
3778 b1
= gen_ehostop(eaddr
, Q_DST
);
3784 b0
= gen_ehostop(eaddr
, Q_SRC
);
3785 b1
= gen_ehostop(eaddr
, Q_DST
);
3794 * Like gen_ehostop, but for DLT_FDDI
3796 static struct block
*
3797 gen_fhostop(eaddr
, dir
)
3798 register const u_char
*eaddr
;
3801 struct block
*b0
, *b1
;
3806 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3808 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3813 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3815 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3819 b0
= gen_fhostop(eaddr
, Q_SRC
);
3820 b1
= gen_fhostop(eaddr
, Q_DST
);
3826 b0
= gen_fhostop(eaddr
, Q_SRC
);
3827 b1
= gen_fhostop(eaddr
, Q_DST
);
3836 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3838 static struct block
*
3839 gen_thostop(eaddr
, dir
)
3840 register const u_char
*eaddr
;
3843 register struct block
*b0
, *b1
;
3847 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3850 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3853 b0
= gen_thostop(eaddr
, Q_SRC
);
3854 b1
= gen_thostop(eaddr
, Q_DST
);
3860 b0
= gen_thostop(eaddr
, Q_SRC
);
3861 b1
= gen_thostop(eaddr
, Q_DST
);
3870 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3871 * various 802.11 + radio headers.
3873 static struct block
*
3874 gen_wlanhostop(eaddr
, dir
)
3875 register const u_char
*eaddr
;
3878 register struct block
*b0
, *b1
, *b2
;
3879 register struct slist
*s
;
3881 #ifdef ENABLE_WLAN_FILTERING_PATCH
3884 * We need to disable the optimizer because the optimizer is buggy
3885 * and wipes out some LD instructions generated by the below
3886 * code to validate the Frame Control bits
3889 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3896 * For control frames, there is no SA.
3898 * For management frames, SA is at an
3899 * offset of 10 from the beginning of
3902 * For data frames, SA is at an offset
3903 * of 10 from the beginning of the packet
3904 * if From DS is clear, at an offset of
3905 * 16 from the beginning of the packet
3906 * if From DS is set and To DS is clear,
3907 * and an offset of 24 from the beginning
3908 * of the packet if From DS is set and To DS
3913 * Generate the tests to be done for data frames
3916 * First, check for To DS set, i.e. check "link[1] & 0x01".
3918 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3919 b1
= new_block(JMP(BPF_JSET
));
3920 b1
->s
.k
= 0x01; /* To DS */
3924 * If To DS is set, the SA is at 24.
3926 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3930 * Now, check for To DS not set, i.e. check
3931 * "!(link[1] & 0x01)".
3933 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3934 b2
= new_block(JMP(BPF_JSET
));
3935 b2
->s
.k
= 0x01; /* To DS */
3940 * If To DS is not set, the SA is at 16.
3942 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3946 * Now OR together the last two checks. That gives
3947 * the complete set of checks for data frames with
3953 * Now check for From DS being set, and AND that with
3954 * the ORed-together checks.
3956 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3957 b1
= new_block(JMP(BPF_JSET
));
3958 b1
->s
.k
= 0x02; /* From DS */
3963 * Now check for data frames with From DS not set.
3965 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3966 b2
= new_block(JMP(BPF_JSET
));
3967 b2
->s
.k
= 0x02; /* From DS */
3972 * If From DS isn't set, the SA is at 10.
3974 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3978 * Now OR together the checks for data frames with
3979 * From DS not set and for data frames with From DS
3980 * set; that gives the checks done for data frames.
3985 * Now check for a data frame.
3986 * I.e, check "link[0] & 0x08".
3988 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3989 b1
= new_block(JMP(BPF_JSET
));
3994 * AND that with the checks done for data frames.
3999 * If the high-order bit of the type value is 0, this
4000 * is a management frame.
4001 * I.e, check "!(link[0] & 0x08)".
4003 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4004 b2
= new_block(JMP(BPF_JSET
));
4010 * For management frames, the SA is at 10.
4012 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4016 * OR that with the checks done for data frames.
4017 * That gives the checks done for management and
4023 * If the low-order bit of the type value is 1,
4024 * this is either a control frame or a frame
4025 * with a reserved type, and thus not a
4028 * I.e., check "!(link[0] & 0x04)".
4030 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4031 b1
= new_block(JMP(BPF_JSET
));
4037 * AND that with the checks for data and management
4047 * For control frames, there is no DA.
4049 * For management frames, DA is at an
4050 * offset of 4 from the beginning of
4053 * For data frames, DA is at an offset
4054 * of 4 from the beginning of the packet
4055 * if To DS is clear and at an offset of
4056 * 16 from the beginning of the packet
4061 * Generate the tests to be done for data frames.
4063 * First, check for To DS set, i.e. "link[1] & 0x01".
4065 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4066 b1
= new_block(JMP(BPF_JSET
));
4067 b1
->s
.k
= 0x01; /* To DS */
4071 * If To DS is set, the DA is at 16.
4073 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4077 * Now, check for To DS not set, i.e. check
4078 * "!(link[1] & 0x01)".
4080 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4081 b2
= new_block(JMP(BPF_JSET
));
4082 b2
->s
.k
= 0x01; /* To DS */
4087 * If To DS is not set, the DA is at 4.
4089 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4093 * Now OR together the last two checks. That gives
4094 * the complete set of checks for data frames.
4099 * Now check for a data frame.
4100 * I.e, check "link[0] & 0x08".
4102 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4103 b1
= new_block(JMP(BPF_JSET
));
4108 * AND that with the checks done for data frames.
4113 * If the high-order bit of the type value is 0, this
4114 * is a management frame.
4115 * I.e, check "!(link[0] & 0x08)".
4117 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4118 b2
= new_block(JMP(BPF_JSET
));
4124 * For management frames, the DA is at 4.
4126 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4130 * OR that with the checks done for data frames.
4131 * That gives the checks done for management and
4137 * If the low-order bit of the type value is 1,
4138 * this is either a control frame or a frame
4139 * with a reserved type, and thus not a
4142 * I.e., check "!(link[0] & 0x04)".
4144 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4145 b1
= new_block(JMP(BPF_JSET
));
4151 * AND that with the checks for data and management
4158 * XXX - add RA, TA, and BSSID keywords?
4161 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4165 * Not present in CTS or ACK control frames.
4167 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4168 IEEE80211_FC0_TYPE_MASK
);
4170 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4171 IEEE80211_FC0_SUBTYPE_MASK
);
4173 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4174 IEEE80211_FC0_SUBTYPE_MASK
);
4178 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4184 * Not present in control frames.
4186 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4187 IEEE80211_FC0_TYPE_MASK
);
4189 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4195 * Present only if the direction mask has both "From DS"
4196 * and "To DS" set. Neither control frames nor management
4197 * frames should have both of those set, so we don't
4198 * check the frame type.
4200 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4201 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4202 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4207 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4208 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4214 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4215 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4224 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4225 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4226 * as the RFC states.)
4228 static struct block
*
4229 gen_ipfchostop(eaddr
, dir
)
4230 register const u_char
*eaddr
;
4233 register struct block
*b0
, *b1
;
4237 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4240 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4243 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4244 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4250 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4251 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4260 * This is quite tricky because there may be pad bytes in front of the
4261 * DECNET header, and then there are two possible data packet formats that
4262 * carry both src and dst addresses, plus 5 packet types in a format that
4263 * carries only the src node, plus 2 types that use a different format and
4264 * also carry just the src node.
4268 * Instead of doing those all right, we just look for data packets with
4269 * 0 or 1 bytes of padding. If you want to look at other packets, that
4270 * will require a lot more hacking.
4272 * To add support for filtering on DECNET "areas" (network numbers)
4273 * one would want to add a "mask" argument to this routine. That would
4274 * make the filter even more inefficient, although one could be clever
4275 * and not generate masking instructions if the mask is 0xFFFF.
4277 static struct block
*
4278 gen_dnhostop(addr
, dir
)
4282 struct block
*b0
, *b1
, *b2
, *tmp
;
4283 u_int offset_lh
; /* offset if long header is received */
4284 u_int offset_sh
; /* offset if short header is received */
4289 offset_sh
= 1; /* follows flags */
4290 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4294 offset_sh
= 3; /* follows flags, dstnode */
4295 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4299 /* Inefficient because we do our Calvinball dance twice */
4300 b0
= gen_dnhostop(addr
, Q_SRC
);
4301 b1
= gen_dnhostop(addr
, Q_DST
);
4307 /* Inefficient because we do our Calvinball dance twice */
4308 b0
= gen_dnhostop(addr
, Q_SRC
);
4309 b1
= gen_dnhostop(addr
, Q_DST
);
4314 bpf_error("ISO host filtering not implemented");
4319 b0
= gen_linktype(ETHERTYPE_DN
);
4320 /* Check for pad = 1, long header case */
4321 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4322 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4323 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4324 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4326 /* Check for pad = 0, long header case */
4327 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4328 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4331 /* Check for pad = 1, short header case */
4332 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4333 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4334 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4337 /* Check for pad = 0, short header case */
4338 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4339 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4343 /* Combine with test for linktype */
4349 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4350 * test the bottom-of-stack bit, and then check the version number
4351 * field in the IP header.
4353 static struct block
*
4354 gen_mpls_linktype(proto
)
4357 struct block
*b0
, *b1
;
4362 /* match the bottom-of-stack bit */
4363 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4364 /* match the IPv4 version number */
4365 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4370 /* match the bottom-of-stack bit */
4371 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4372 /* match the IPv4 version number */
4373 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4382 static struct block
*
4383 gen_host(addr
, mask
, proto
, dir
, type
)
4390 struct block
*b0
, *b1
;
4391 const char *typestr
;
4401 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4403 * Only check for non-IPv4 addresses if we're not
4404 * checking MPLS-encapsulated packets.
4406 if (label_stack_depth
== 0) {
4407 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4409 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4415 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4418 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4421 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4424 bpf_error("'tcp' modifier applied to %s", typestr
);
4427 bpf_error("'sctp' modifier applied to %s", typestr
);
4430 bpf_error("'udp' modifier applied to %s", typestr
);
4433 bpf_error("'icmp' modifier applied to %s", typestr
);
4436 bpf_error("'igmp' modifier applied to %s", typestr
);
4439 bpf_error("'igrp' modifier applied to %s", typestr
);
4442 bpf_error("'pim' modifier applied to %s", typestr
);
4445 bpf_error("'vrrp' modifier applied to %s", typestr
);
4448 bpf_error("ATALK host filtering not implemented");
4451 bpf_error("AARP host filtering not implemented");
4454 return gen_dnhostop(addr
, dir
);
4457 bpf_error("SCA host filtering not implemented");
4460 bpf_error("LAT host filtering not implemented");
4463 bpf_error("MOPDL host filtering not implemented");
4466 bpf_error("MOPRC host filtering not implemented");
4470 bpf_error("'ip6' modifier applied to ip host");
4473 bpf_error("'icmp6' modifier applied to %s", typestr
);
4477 bpf_error("'ah' modifier applied to %s", typestr
);
4480 bpf_error("'esp' modifier applied to %s", typestr
);
4483 bpf_error("ISO host filtering not implemented");
4486 bpf_error("'esis' modifier applied to %s", typestr
);
4489 bpf_error("'isis' modifier applied to %s", typestr
);
4492 bpf_error("'clnp' modifier applied to %s", typestr
);
4495 bpf_error("'stp' modifier applied to %s", typestr
);
4498 bpf_error("IPX host filtering not implemented");
4501 bpf_error("'netbeui' modifier applied to %s", typestr
);
4504 bpf_error("'radio' modifier applied to %s", typestr
);
4513 static struct block
*
4514 gen_host6(addr
, mask
, proto
, dir
, type
)
4515 struct in6_addr
*addr
;
4516 struct in6_addr
*mask
;
4521 const char *typestr
;
4531 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4534 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4537 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4540 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4543 bpf_error("'sctp' modifier applied to %s", typestr
);
4546 bpf_error("'tcp' modifier applied to %s", typestr
);
4549 bpf_error("'udp' modifier applied to %s", typestr
);
4552 bpf_error("'icmp' modifier applied to %s", typestr
);
4555 bpf_error("'igmp' modifier applied to %s", typestr
);
4558 bpf_error("'igrp' modifier applied to %s", typestr
);
4561 bpf_error("'pim' modifier applied to %s", typestr
);
4564 bpf_error("'vrrp' modifier applied to %s", typestr
);
4567 bpf_error("ATALK host filtering not implemented");
4570 bpf_error("AARP host filtering not implemented");
4573 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4576 bpf_error("SCA host filtering not implemented");
4579 bpf_error("LAT host filtering not implemented");
4582 bpf_error("MOPDL host filtering not implemented");
4585 bpf_error("MOPRC host filtering not implemented");
4588 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4591 bpf_error("'icmp6' modifier applied to %s", typestr
);
4594 bpf_error("'ah' modifier applied to %s", typestr
);
4597 bpf_error("'esp' modifier applied to %s", typestr
);
4600 bpf_error("ISO host filtering not implemented");
4603 bpf_error("'esis' modifier applied to %s", typestr
);
4606 bpf_error("'isis' modifier applied to %s", typestr
);
4609 bpf_error("'clnp' modifier applied to %s", typestr
);
4612 bpf_error("'stp' modifier applied to %s", typestr
);
4615 bpf_error("IPX host filtering not implemented");
4618 bpf_error("'netbeui' modifier applied to %s", typestr
);
4621 bpf_error("'radio' modifier applied to %s", typestr
);
4631 static struct block
*
4632 gen_gateway(eaddr
, alist
, proto
, dir
)
4633 const u_char
*eaddr
;
4634 bpf_u_int32
**alist
;
4638 struct block
*b0
, *b1
, *tmp
;
4641 bpf_error("direction applied to 'gateway'");
4650 b0
= gen_ehostop(eaddr
, Q_OR
);
4653 b0
= gen_fhostop(eaddr
, Q_OR
);
4656 b0
= gen_thostop(eaddr
, Q_OR
);
4658 case DLT_IEEE802_11
:
4659 case DLT_PRISM_HEADER
:
4660 case DLT_IEEE802_11_RADIO_AVS
:
4661 case DLT_IEEE802_11_RADIO
:
4663 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4668 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4670 * Check that the packet doesn't begin with an
4671 * LE Control marker. (We've already generated
4674 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4679 * Now check the MAC address.
4681 b0
= gen_ehostop(eaddr
, Q_OR
);
4684 case DLT_IP_OVER_FC
:
4685 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4689 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4691 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4693 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4702 bpf_error("illegal modifier of 'gateway'");
4708 gen_proto_abbrev(proto
)
4717 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4719 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4725 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4727 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4733 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4735 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4741 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4744 #ifndef IPPROTO_IGMP
4745 #define IPPROTO_IGMP 2
4749 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4752 #ifndef IPPROTO_IGRP
4753 #define IPPROTO_IGRP 9
4756 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4760 #define IPPROTO_PIM 103
4764 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4766 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4771 #ifndef IPPROTO_VRRP
4772 #define IPPROTO_VRRP 112
4776 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4780 b1
= gen_linktype(ETHERTYPE_IP
);
4784 b1
= gen_linktype(ETHERTYPE_ARP
);
4788 b1
= gen_linktype(ETHERTYPE_REVARP
);
4792 bpf_error("link layer applied in wrong context");
4795 b1
= gen_linktype(ETHERTYPE_ATALK
);
4799 b1
= gen_linktype(ETHERTYPE_AARP
);
4803 b1
= gen_linktype(ETHERTYPE_DN
);
4807 b1
= gen_linktype(ETHERTYPE_SCA
);
4811 b1
= gen_linktype(ETHERTYPE_LAT
);
4815 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4819 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4824 b1
= gen_linktype(ETHERTYPE_IPV6
);
4827 #ifndef IPPROTO_ICMPV6
4828 #define IPPROTO_ICMPV6 58
4831 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4836 #define IPPROTO_AH 51
4839 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4841 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4847 #define IPPROTO_ESP 50
4850 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4852 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4858 b1
= gen_linktype(LLCSAP_ISONS
);
4862 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4866 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4869 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4870 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4871 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4873 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4875 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4877 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4881 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4882 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4883 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4885 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4887 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4889 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4893 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4894 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4895 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4897 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4902 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4903 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4908 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4909 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4911 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4913 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4918 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4919 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4924 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4925 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4930 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4934 b1
= gen_linktype(LLCSAP_8021D
);
4938 b1
= gen_linktype(LLCSAP_IPX
);
4942 b1
= gen_linktype(LLCSAP_NETBEUI
);
4946 bpf_error("'radio' is not a valid protocol type");
4954 static struct block
*
4961 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4962 b
= new_block(JMP(BPF_JSET
));
4971 * Generate a comparison to a port value in the transport-layer header
4972 * at the specified offset from the beginning of that header.
4974 * XXX - this handles a variable-length prefix preceding the link-layer
4975 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4976 * variable-length link-layer headers (such as Token Ring or 802.11
4979 static struct block
*
4980 gen_portatom(off
, v
)
4984 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4988 static struct block
*
4989 gen_portatom6(off
, v
)
4993 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4998 gen_portop(port
, proto
, dir
)
4999 int port
, proto
, dir
;
5001 struct block
*b0
, *b1
, *tmp
;
5003 /* ip proto 'proto' */
5004 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5010 b1
= gen_portatom(0, (bpf_int32
)port
);
5014 b1
= gen_portatom(2, (bpf_int32
)port
);
5019 tmp
= gen_portatom(0, (bpf_int32
)port
);
5020 b1
= gen_portatom(2, (bpf_int32
)port
);
5025 tmp
= gen_portatom(0, (bpf_int32
)port
);
5026 b1
= gen_portatom(2, (bpf_int32
)port
);
5038 static struct block
*
5039 gen_port(port
, ip_proto
, dir
)
5044 struct block
*b0
, *b1
, *tmp
;
5049 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5050 * not LLC encapsulation with LLCSAP_IP.
5052 * For IEEE 802 networks - which includes 802.5 token ring
5053 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5054 * says that SNAP encapsulation is used, not LLC encapsulation
5057 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5058 * RFC 2225 say that SNAP encapsulation is used, not LLC
5059 * encapsulation with LLCSAP_IP.
5061 * So we always check for ETHERTYPE_IP.
5063 b0
= gen_linktype(ETHERTYPE_IP
);
5069 b1
= gen_portop(port
, ip_proto
, dir
);
5073 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5074 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5076 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5089 gen_portop6(port
, proto
, dir
)
5090 int port
, proto
, dir
;
5092 struct block
*b0
, *b1
, *tmp
;
5094 /* ip6 proto 'proto' */
5095 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5099 b1
= gen_portatom6(0, (bpf_int32
)port
);
5103 b1
= gen_portatom6(2, (bpf_int32
)port
);
5108 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5109 b1
= gen_portatom6(2, (bpf_int32
)port
);
5114 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5115 b1
= gen_portatom6(2, (bpf_int32
)port
);
5127 static struct block
*
5128 gen_port6(port
, ip_proto
, dir
)
5133 struct block
*b0
, *b1
, *tmp
;
5135 /* link proto ip6 */
5136 b0
= gen_linktype(ETHERTYPE_IPV6
);
5142 b1
= gen_portop6(port
, ip_proto
, dir
);
5146 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5147 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5149 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5161 /* gen_portrange code */
5162 static struct block
*
5163 gen_portrangeatom(off
, v1
, v2
)
5167 struct block
*b1
, *b2
;
5171 * Reverse the order of the ports, so v1 is the lower one.
5180 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5181 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5189 gen_portrangeop(port1
, port2
, proto
, dir
)
5194 struct block
*b0
, *b1
, *tmp
;
5196 /* ip proto 'proto' */
5197 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5203 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5207 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5212 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5213 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5218 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5219 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5231 static struct block
*
5232 gen_portrange(port1
, port2
, ip_proto
, dir
)
5237 struct block
*b0
, *b1
, *tmp
;
5240 b0
= gen_linktype(ETHERTYPE_IP
);
5246 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5250 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5251 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5253 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5265 static struct block
*
5266 gen_portrangeatom6(off
, v1
, v2
)
5270 struct block
*b1
, *b2
;
5274 * Reverse the order of the ports, so v1 is the lower one.
5283 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5284 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5292 gen_portrangeop6(port1
, port2
, proto
, dir
)
5297 struct block
*b0
, *b1
, *tmp
;
5299 /* ip6 proto 'proto' */
5300 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5304 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5308 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5313 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5314 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5319 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5320 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5332 static struct block
*
5333 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5338 struct block
*b0
, *b1
, *tmp
;
5340 /* link proto ip6 */
5341 b0
= gen_linktype(ETHERTYPE_IPV6
);
5347 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5351 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5352 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5354 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5367 lookup_proto(name
, proto
)
5368 register const char *name
;
5378 v
= pcap_nametoproto(name
);
5379 if (v
== PROTO_UNDEF
)
5380 bpf_error("unknown ip proto '%s'", name
);
5384 /* XXX should look up h/w protocol type based on linktype */
5385 v
= pcap_nametoeproto(name
);
5386 if (v
== PROTO_UNDEF
) {
5387 v
= pcap_nametollc(name
);
5388 if (v
== PROTO_UNDEF
)
5389 bpf_error("unknown ether proto '%s'", name
);
5394 if (strcmp(name
, "esis") == 0)
5396 else if (strcmp(name
, "isis") == 0)
5398 else if (strcmp(name
, "clnp") == 0)
5401 bpf_error("unknown osi proto '%s'", name
);
5421 static struct block
*
5422 gen_protochain(v
, proto
, dir
)
5427 #ifdef NO_PROTOCHAIN
5428 return gen_proto(v
, proto
, dir
);
5430 struct block
*b0
, *b
;
5431 struct slist
*s
[100];
5432 int fix2
, fix3
, fix4
, fix5
;
5433 int ahcheck
, again
, end
;
5435 int reg2
= alloc_reg();
5437 memset(s
, 0, sizeof(s
));
5438 fix2
= fix3
= fix4
= fix5
= 0;
5445 b0
= gen_protochain(v
, Q_IP
, dir
);
5446 b
= gen_protochain(v
, Q_IPV6
, dir
);
5450 bpf_error("bad protocol applied for 'protochain'");
5455 * We don't handle variable-length prefixes before the link-layer
5456 * header, or variable-length link-layer headers, here yet.
5457 * We might want to add BPF instructions to do the protochain
5458 * work, to simplify that and, on platforms that have a BPF
5459 * interpreter with the new instructions, let the filtering
5460 * be done in the kernel. (We already require a modified BPF
5461 * engine to do the protochain stuff, to support backward
5462 * branches, and backward branch support is unlikely to appear
5463 * in kernel BPF engines.)
5467 case DLT_IEEE802_11
:
5468 case DLT_PRISM_HEADER
:
5469 case DLT_IEEE802_11_RADIO_AVS
:
5470 case DLT_IEEE802_11_RADIO
:
5472 bpf_error("'protochain' not supported with 802.11");
5475 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5478 * s[0] is a dummy entry to protect other BPF insn from damage
5479 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5480 * hard to find interdependency made by jump table fixup.
5483 s
[i
] = new_stmt(0); /*dummy*/
5488 b0
= gen_linktype(ETHERTYPE_IP
);
5491 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5492 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5494 /* X = ip->ip_hl << 2 */
5495 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5496 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5501 b0
= gen_linktype(ETHERTYPE_IPV6
);
5503 /* A = ip6->ip_nxt */
5504 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5505 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5507 /* X = sizeof(struct ip6_hdr) */
5508 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5514 bpf_error("unsupported proto to gen_protochain");
5518 /* again: if (A == v) goto end; else fall through; */
5520 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5522 s
[i
]->s
.jt
= NULL
; /*later*/
5523 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5527 #ifndef IPPROTO_NONE
5528 #define IPPROTO_NONE 59
5530 /* if (A == IPPROTO_NONE) goto end */
5531 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5532 s
[i
]->s
.jt
= NULL
; /*later*/
5533 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5534 s
[i
]->s
.k
= IPPROTO_NONE
;
5535 s
[fix5
]->s
.jf
= s
[i
];
5540 if (proto
== Q_IPV6
) {
5541 int v6start
, v6end
, v6advance
, j
;
5544 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5545 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5546 s
[i
]->s
.jt
= NULL
; /*later*/
5547 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5548 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5549 s
[fix2
]->s
.jf
= s
[i
];
5551 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5552 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5553 s
[i
]->s
.jt
= NULL
; /*later*/
5554 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5555 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5557 /* if (A == IPPROTO_ROUTING) goto v6advance */
5558 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5559 s
[i
]->s
.jt
= NULL
; /*later*/
5560 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5561 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5563 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5564 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5565 s
[i
]->s
.jt
= NULL
; /*later*/
5566 s
[i
]->s
.jf
= NULL
; /*later*/
5567 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5578 * X = X + (P[X + 1] + 1) * 8;
5581 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5583 /* A = P[X + packet head] */
5584 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5585 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5588 s
[i
] = new_stmt(BPF_ST
);
5592 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5595 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5599 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5601 /* A = P[X + packet head]; */
5602 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5603 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5606 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5610 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5614 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5617 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5621 /* goto again; (must use BPF_JA for backward jump) */
5622 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5623 s
[i
]->s
.k
= again
- i
- 1;
5624 s
[i
- 1]->s
.jf
= s
[i
];
5628 for (j
= v6start
; j
<= v6end
; j
++)
5629 s
[j
]->s
.jt
= s
[v6advance
];
5634 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5636 s
[fix2
]->s
.jf
= s
[i
];
5642 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5643 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5644 s
[i
]->s
.jt
= NULL
; /*later*/
5645 s
[i
]->s
.jf
= NULL
; /*later*/
5646 s
[i
]->s
.k
= IPPROTO_AH
;
5648 s
[fix3
]->s
.jf
= s
[ahcheck
];
5655 * X = X + (P[X + 1] + 2) * 4;
5658 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5660 /* A = P[X + packet head]; */
5661 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5662 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5665 s
[i
] = new_stmt(BPF_ST
);
5669 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5672 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5676 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5678 /* A = P[X + packet head] */
5679 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5680 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5683 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5687 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5691 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5694 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5698 /* goto again; (must use BPF_JA for backward jump) */
5699 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5700 s
[i
]->s
.k
= again
- i
- 1;
5705 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5707 s
[fix2
]->s
.jt
= s
[end
];
5708 s
[fix4
]->s
.jf
= s
[end
];
5709 s
[fix5
]->s
.jt
= s
[end
];
5716 for (i
= 0; i
< max
- 1; i
++)
5717 s
[i
]->next
= s
[i
+ 1];
5718 s
[max
- 1]->next
= NULL
;
5723 b
= new_block(JMP(BPF_JEQ
));
5724 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5734 static struct block
*
5735 gen_check_802_11_data_frame()
5738 struct block
*b0
, *b1
;
5741 * A data frame has the 0x08 bit (b3) in the frame control field set
5742 * and the 0x04 bit (b2) clear.
5744 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5745 b0
= new_block(JMP(BPF_JSET
));
5749 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5750 b1
= new_block(JMP(BPF_JSET
));
5761 * Generate code that checks whether the packet is a packet for protocol
5762 * <proto> and whether the type field in that protocol's header has
5763 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5764 * IP packet and checks the protocol number in the IP header against <v>.
5766 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5767 * against Q_IP and Q_IPV6.
5769 static struct block
*
5770 gen_proto(v
, proto
, dir
)
5775 struct block
*b0
, *b1
;
5777 if (dir
!= Q_DEFAULT
)
5778 bpf_error("direction applied to 'proto'");
5783 b0
= gen_proto(v
, Q_IP
, dir
);
5784 b1
= gen_proto(v
, Q_IPV6
, dir
);
5792 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5793 * not LLC encapsulation with LLCSAP_IP.
5795 * For IEEE 802 networks - which includes 802.5 token ring
5796 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5797 * says that SNAP encapsulation is used, not LLC encapsulation
5800 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5801 * RFC 2225 say that SNAP encapsulation is used, not LLC
5802 * encapsulation with LLCSAP_IP.
5804 * So we always check for ETHERTYPE_IP.
5806 b0
= gen_linktype(ETHERTYPE_IP
);
5808 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5810 b1
= gen_protochain(v
, Q_IP
);
5820 * Frame Relay packets typically have an OSI
5821 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5822 * generates code to check for all the OSI
5823 * NLPIDs, so calling it and then adding a check
5824 * for the particular NLPID for which we're
5825 * looking is bogus, as we can just check for
5828 * What we check for is the NLPID and a frame
5829 * control field value of UI, i.e. 0x03 followed
5832 * XXX - assumes a 2-byte Frame Relay header with
5833 * DLCI and flags. What if the address is longer?
5835 * XXX - what about SNAP-encapsulated frames?
5837 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5843 * Cisco uses an Ethertype lookalike - for OSI,
5846 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5847 /* OSI in C-HDLC is stuffed with a fudge byte */
5848 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5853 b0
= gen_linktype(LLCSAP_ISONS
);
5854 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5860 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5862 * 4 is the offset of the PDU type relative to the IS-IS
5865 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5870 bpf_error("arp does not encapsulate another protocol");
5874 bpf_error("rarp does not encapsulate another protocol");
5878 bpf_error("atalk encapsulation is not specifiable");
5882 bpf_error("decnet encapsulation is not specifiable");
5886 bpf_error("sca does not encapsulate another protocol");
5890 bpf_error("lat does not encapsulate another protocol");
5894 bpf_error("moprc does not encapsulate another protocol");
5898 bpf_error("mopdl does not encapsulate another protocol");
5902 return gen_linktype(v
);
5905 bpf_error("'udp proto' is bogus");
5909 bpf_error("'tcp proto' is bogus");
5913 bpf_error("'sctp proto' is bogus");
5917 bpf_error("'icmp proto' is bogus");
5921 bpf_error("'igmp proto' is bogus");
5925 bpf_error("'igrp proto' is bogus");
5929 bpf_error("'pim proto' is bogus");
5933 bpf_error("'vrrp proto' is bogus");
5938 b0
= gen_linktype(ETHERTYPE_IPV6
);
5940 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5942 b1
= gen_protochain(v
, Q_IPV6
);
5948 bpf_error("'icmp6 proto' is bogus");
5952 bpf_error("'ah proto' is bogus");
5955 bpf_error("'ah proto' is bogus");
5958 bpf_error("'stp proto' is bogus");
5961 bpf_error("'ipx proto' is bogus");
5964 bpf_error("'netbeui proto' is bogus");
5967 bpf_error("'radio proto' is bogus");
5978 register const char *name
;
5981 int proto
= q
.proto
;
5985 bpf_u_int32 mask
, addr
;
5987 bpf_u_int32
**alist
;
5990 struct sockaddr_in
*sin4
;
5991 struct sockaddr_in6
*sin6
;
5992 struct addrinfo
*res
, *res0
;
5993 struct in6_addr mask128
;
5995 struct block
*b
, *tmp
;
5996 int port
, real_proto
;
6002 addr
= pcap_nametonetaddr(name
);
6004 bpf_error("unknown network '%s'", name
);
6005 /* Left justify network addr and calculate its network mask */
6007 while (addr
&& (addr
& 0xff000000) == 0) {
6011 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6015 if (proto
== Q_LINK
) {
6019 eaddr
= pcap_ether_hostton(name
);
6022 "unknown ether host '%s'", name
);
6023 b
= gen_ehostop(eaddr
, dir
);
6028 eaddr
= pcap_ether_hostton(name
);
6031 "unknown FDDI host '%s'", name
);
6032 b
= gen_fhostop(eaddr
, dir
);
6037 eaddr
= pcap_ether_hostton(name
);
6040 "unknown token ring host '%s'", name
);
6041 b
= gen_thostop(eaddr
, dir
);
6045 case DLT_IEEE802_11
:
6046 case DLT_PRISM_HEADER
:
6047 case DLT_IEEE802_11_RADIO_AVS
:
6048 case DLT_IEEE802_11_RADIO
:
6050 eaddr
= pcap_ether_hostton(name
);
6053 "unknown 802.11 host '%s'", name
);
6054 b
= gen_wlanhostop(eaddr
, dir
);
6058 case DLT_IP_OVER_FC
:
6059 eaddr
= pcap_ether_hostton(name
);
6062 "unknown Fibre Channel host '%s'", name
);
6063 b
= gen_ipfchostop(eaddr
, dir
);
6072 * Check that the packet doesn't begin
6073 * with an LE Control marker. (We've
6074 * already generated a test for LANE.)
6076 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6080 eaddr
= pcap_ether_hostton(name
);
6083 "unknown ether host '%s'", name
);
6084 b
= gen_ehostop(eaddr
, dir
);
6090 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6091 } else if (proto
== Q_DECNET
) {
6092 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6094 * I don't think DECNET hosts can be multihomed, so
6095 * there is no need to build up a list of addresses
6097 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6100 alist
= pcap_nametoaddr(name
);
6101 if (alist
== NULL
|| *alist
== NULL
)
6102 bpf_error("unknown host '%s'", name
);
6104 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6106 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6108 tmp
= gen_host(**alist
++, 0xffffffff,
6109 tproto
, dir
, q
.addr
);
6115 memset(&mask128
, 0xff, sizeof(mask128
));
6116 res0
= res
= pcap_nametoaddrinfo(name
);
6118 bpf_error("unknown host '%s'", name
);
6121 tproto
= tproto6
= proto
;
6122 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6126 for (res
= res0
; res
; res
= res
->ai_next
) {
6127 switch (res
->ai_family
) {
6129 if (tproto
== Q_IPV6
)
6132 sin4
= (struct sockaddr_in
*)
6134 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6135 0xffffffff, tproto
, dir
, q
.addr
);
6138 if (tproto6
== Q_IP
)
6141 sin6
= (struct sockaddr_in6
*)
6143 tmp
= gen_host6(&sin6
->sin6_addr
,
6144 &mask128
, tproto6
, dir
, q
.addr
);
6156 bpf_error("unknown host '%s'%s", name
,
6157 (proto
== Q_DEFAULT
)
6159 : " for specified address family");
6166 if (proto
!= Q_DEFAULT
&&
6167 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6168 bpf_error("illegal qualifier of 'port'");
6169 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6170 bpf_error("unknown port '%s'", name
);
6171 if (proto
== Q_UDP
) {
6172 if (real_proto
== IPPROTO_TCP
)
6173 bpf_error("port '%s' is tcp", name
);
6174 else if (real_proto
== IPPROTO_SCTP
)
6175 bpf_error("port '%s' is sctp", name
);
6177 /* override PROTO_UNDEF */
6178 real_proto
= IPPROTO_UDP
;
6180 if (proto
== Q_TCP
) {
6181 if (real_proto
== IPPROTO_UDP
)
6182 bpf_error("port '%s' is udp", name
);
6184 else if (real_proto
== IPPROTO_SCTP
)
6185 bpf_error("port '%s' is sctp", name
);
6187 /* override PROTO_UNDEF */
6188 real_proto
= IPPROTO_TCP
;
6190 if (proto
== Q_SCTP
) {
6191 if (real_proto
== IPPROTO_UDP
)
6192 bpf_error("port '%s' is udp", name
);
6194 else if (real_proto
== IPPROTO_TCP
)
6195 bpf_error("port '%s' is tcp", name
);
6197 /* override PROTO_UNDEF */
6198 real_proto
= IPPROTO_SCTP
;
6201 return gen_port(port
, real_proto
, dir
);
6203 b
= gen_port(port
, real_proto
, dir
);
6204 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6209 if (proto
!= Q_DEFAULT
&&
6210 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6211 bpf_error("illegal qualifier of 'portrange'");
6212 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6213 bpf_error("unknown port in range '%s'", name
);
6214 if (proto
== Q_UDP
) {
6215 if (real_proto
== IPPROTO_TCP
)
6216 bpf_error("port in range '%s' is tcp", name
);
6217 else if (real_proto
== IPPROTO_SCTP
)
6218 bpf_error("port in range '%s' is sctp", name
);
6220 /* override PROTO_UNDEF */
6221 real_proto
= IPPROTO_UDP
;
6223 if (proto
== Q_TCP
) {
6224 if (real_proto
== IPPROTO_UDP
)
6225 bpf_error("port in range '%s' is udp", name
);
6226 else if (real_proto
== IPPROTO_SCTP
)
6227 bpf_error("port in range '%s' is sctp", name
);
6229 /* override PROTO_UNDEF */
6230 real_proto
= IPPROTO_TCP
;
6232 if (proto
== Q_SCTP
) {
6233 if (real_proto
== IPPROTO_UDP
)
6234 bpf_error("port in range '%s' is udp", name
);
6235 else if (real_proto
== IPPROTO_TCP
)
6236 bpf_error("port in range '%s' is tcp", name
);
6238 /* override PROTO_UNDEF */
6239 real_proto
= IPPROTO_SCTP
;
6242 return gen_portrange(port1
, port2
, real_proto
, dir
);
6244 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6245 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6251 eaddr
= pcap_ether_hostton(name
);
6253 bpf_error("unknown ether host: %s", name
);
6255 alist
= pcap_nametoaddr(name
);
6256 if (alist
== NULL
|| *alist
== NULL
)
6257 bpf_error("unknown host '%s'", name
);
6258 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6262 bpf_error("'gateway' not supported in this configuration");
6266 real_proto
= lookup_proto(name
, proto
);
6267 if (real_proto
>= 0)
6268 return gen_proto(real_proto
, proto
, dir
);
6270 bpf_error("unknown protocol: %s", name
);
6273 real_proto
= lookup_proto(name
, proto
);
6274 if (real_proto
>= 0)
6275 return gen_protochain(real_proto
, proto
, dir
);
6277 bpf_error("unknown protocol: %s", name
);
6288 gen_mcode(s1
, s2
, masklen
, q
)
6289 register const char *s1
, *s2
;
6290 register int masklen
;
6293 register int nlen
, mlen
;
6296 nlen
= __pcap_atoin(s1
, &n
);
6297 /* Promote short ipaddr */
6301 mlen
= __pcap_atoin(s2
, &m
);
6302 /* Promote short ipaddr */
6305 bpf_error("non-network bits set in \"%s mask %s\"",
6308 /* Convert mask len to mask */
6310 bpf_error("mask length must be <= 32");
6313 * X << 32 is not guaranteed by C to be 0; it's
6318 m
= 0xffffffff << (32 - masklen
);
6320 bpf_error("non-network bits set in \"%s/%d\"",
6327 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6330 bpf_error("Mask syntax for networks only");
6339 register const char *s
;
6344 int proto
= q
.proto
;
6350 else if (q
.proto
== Q_DECNET
)
6351 vlen
= __pcap_atodn(s
, &v
);
6353 vlen
= __pcap_atoin(s
, &v
);
6360 if (proto
== Q_DECNET
)
6361 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6362 else if (proto
== Q_LINK
) {
6363 bpf_error("illegal link layer address");
6366 if (s
== NULL
&& q
.addr
== Q_NET
) {
6367 /* Promote short net number */
6368 while (v
&& (v
& 0xff000000) == 0) {
6373 /* Promote short ipaddr */
6377 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6382 proto
= IPPROTO_UDP
;
6383 else if (proto
== Q_TCP
)
6384 proto
= IPPROTO_TCP
;
6385 else if (proto
== Q_SCTP
)
6386 proto
= IPPROTO_SCTP
;
6387 else if (proto
== Q_DEFAULT
)
6388 proto
= PROTO_UNDEF
;
6390 bpf_error("illegal qualifier of 'port'");
6393 return gen_port((int)v
, proto
, dir
);
6397 b
= gen_port((int)v
, proto
, dir
);
6398 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6405 proto
= IPPROTO_UDP
;
6406 else if (proto
== Q_TCP
)
6407 proto
= IPPROTO_TCP
;
6408 else if (proto
== Q_SCTP
)
6409 proto
= IPPROTO_SCTP
;
6410 else if (proto
== Q_DEFAULT
)
6411 proto
= PROTO_UNDEF
;
6413 bpf_error("illegal qualifier of 'portrange'");
6416 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6420 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6421 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6427 bpf_error("'gateway' requires a name");
6431 return gen_proto((int)v
, proto
, dir
);
6434 return gen_protochain((int)v
, proto
, dir
);
6449 gen_mcode6(s1
, s2
, masklen
, q
)
6450 register const char *s1
, *s2
;
6451 register int masklen
;
6454 struct addrinfo
*res
;
6455 struct in6_addr
*addr
;
6456 struct in6_addr mask
;
6461 bpf_error("no mask %s supported", s2
);
6463 res
= pcap_nametoaddrinfo(s1
);
6465 bpf_error("invalid ip6 address %s", s1
);
6468 bpf_error("%s resolved to multiple address", s1
);
6469 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6471 if (sizeof(mask
) * 8 < masklen
)
6472 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6473 memset(&mask
, 0, sizeof(mask
));
6474 memset(&mask
, 0xff, masklen
/ 8);
6476 mask
.s6_addr
[masklen
/ 8] =
6477 (0xff << (8 - masklen
% 8)) & 0xff;
6480 a
= (u_int32_t
*)addr
;
6481 m
= (u_int32_t
*)&mask
;
6482 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6483 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6484 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6492 bpf_error("Mask syntax for networks only");
6496 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6502 bpf_error("invalid qualifier against IPv6 address");
6511 register const u_char
*eaddr
;
6514 struct block
*b
, *tmp
;
6516 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6519 return gen_ehostop(eaddr
, (int)q
.dir
);
6521 return gen_fhostop(eaddr
, (int)q
.dir
);
6523 return gen_thostop(eaddr
, (int)q
.dir
);
6524 case DLT_IEEE802_11
:
6525 case DLT_PRISM_HEADER
:
6526 case DLT_IEEE802_11_RADIO_AVS
:
6527 case DLT_IEEE802_11_RADIO
:
6529 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6533 * Check that the packet doesn't begin with an
6534 * LE Control marker. (We've already generated
6537 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6542 * Now check the MAC address.
6544 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6549 case DLT_IP_OVER_FC
:
6550 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6552 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6556 bpf_error("ethernet address used in non-ether expression");
6563 struct slist
*s0
, *s1
;
6566 * This is definitely not the best way to do this, but the
6567 * lists will rarely get long.
6574 static struct slist
*
6580 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6585 static struct slist
*
6591 s
= new_stmt(BPF_LD
|BPF_MEM
);
6597 * Modify "index" to use the value stored into its register as an
6598 * offset relative to the beginning of the header for the protocol
6599 * "proto", and allocate a register and put an item "size" bytes long
6600 * (1, 2, or 4) at that offset into that register, making it the register
6604 gen_load(proto
, inst
, size
)
6609 struct slist
*s
, *tmp
;
6611 int regno
= alloc_reg();
6613 free_reg(inst
->regno
);
6617 bpf_error("data size must be 1, 2, or 4");
6633 bpf_error("unsupported index operation");
6637 * The offset is relative to the beginning of the packet
6638 * data, if we have a radio header. (If we don't, this
6641 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6642 linktype
!= DLT_IEEE802_11_RADIO
&&
6643 linktype
!= DLT_PRISM_HEADER
)
6644 bpf_error("radio information not present in capture");
6647 * Load into the X register the offset computed into the
6648 * register specifed by "index".
6650 s
= xfer_to_x(inst
);
6653 * Load the item at that offset.
6655 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6657 sappend(inst
->s
, s
);
6662 * The offset is relative to the beginning of
6663 * the link-layer header.
6665 * XXX - what about ATM LANE? Should the index be
6666 * relative to the beginning of the AAL5 frame, so
6667 * that 0 refers to the beginning of the LE Control
6668 * field, or relative to the beginning of the LAN
6669 * frame, so that 0 refers, for Ethernet LANE, to
6670 * the beginning of the destination address?
6672 s
= gen_llprefixlen();
6675 * If "s" is non-null, it has code to arrange that the
6676 * X register contains the length of the prefix preceding
6677 * the link-layer header. Add to it the offset computed
6678 * into the register specified by "index", and move that
6679 * into the X register. Otherwise, just load into the X
6680 * register the offset computed into the register specifed
6684 sappend(s
, xfer_to_a(inst
));
6685 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6686 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6688 s
= xfer_to_x(inst
);
6691 * Load the item at the sum of the offset we've put in the
6692 * X register and the offset of the start of the link
6693 * layer header (which is 0 if the radio header is
6694 * variable-length; that header length is what we put
6695 * into the X register and then added to the index).
6697 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6700 sappend(inst
->s
, s
);
6716 * The offset is relative to the beginning of
6717 * the network-layer header.
6718 * XXX - are there any cases where we want
6721 s
= gen_off_macpl();
6724 * If "s" is non-null, it has code to arrange that the
6725 * X register contains the offset of the MAC-layer
6726 * payload. Add to it the offset computed into the
6727 * register specified by "index", and move that into
6728 * the X register. Otherwise, just load into the X
6729 * register the offset computed into the register specifed
6733 sappend(s
, xfer_to_a(inst
));
6734 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6735 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6737 s
= xfer_to_x(inst
);
6740 * Load the item at the sum of the offset we've put in the
6741 * X register, the offset of the start of the network
6742 * layer header from the beginning of the MAC-layer
6743 * payload, and the purported offset of the start of the
6744 * MAC-layer payload (which might be 0 if there's a
6745 * variable-length prefix before the link-layer header
6746 * or the link-layer header itself is variable-length;
6747 * the variable-length offset of the start of the
6748 * MAC-layer payload is what we put into the X register
6749 * and then added to the index).
6751 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6752 tmp
->s
.k
= off_macpl
+ off_nl
;
6754 sappend(inst
->s
, s
);
6757 * Do the computation only if the packet contains
6758 * the protocol in question.
6760 b
= gen_proto_abbrev(proto
);
6762 gen_and(inst
->b
, b
);
6775 * The offset is relative to the beginning of
6776 * the transport-layer header.
6778 * Load the X register with the length of the IPv4 header
6779 * (plus the offset of the link-layer header, if it's
6780 * a variable-length header), in bytes.
6782 * XXX - are there any cases where we want
6784 * XXX - we should, if we're built with
6785 * IPv6 support, generate code to load either
6786 * IPv4, IPv6, or both, as appropriate.
6788 s
= gen_loadx_iphdrlen();
6791 * The X register now contains the sum of the length
6792 * of any variable-length header preceding the link-layer
6793 * header, any variable-length link-layer header, and the
6794 * length of the network-layer header.
6796 * Load into the A register the offset relative to
6797 * the beginning of the transport layer header,
6798 * add the X register to that, move that to the
6799 * X register, and load with an offset from the
6800 * X register equal to the offset of the network
6801 * layer header relative to the beginning of
6802 * the MAC-layer payload plus the fixed-length
6803 * portion of the offset of the MAC-layer payload
6804 * from the beginning of the raw packet data.
6806 sappend(s
, xfer_to_a(inst
));
6807 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6808 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6809 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6810 tmp
->s
.k
= off_macpl
+ off_nl
;
6811 sappend(inst
->s
, s
);
6814 * Do the computation only if the packet contains
6815 * the protocol in question - which is true only
6816 * if this is an IP datagram and is the first or
6817 * only fragment of that datagram.
6819 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6821 gen_and(inst
->b
, b
);
6823 gen_and(gen_proto_abbrev(Q_IP
), b
);
6829 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6833 inst
->regno
= regno
;
6834 s
= new_stmt(BPF_ST
);
6836 sappend(inst
->s
, s
);
6842 gen_relation(code
, a0
, a1
, reversed
)
6844 struct arth
*a0
, *a1
;
6847 struct slist
*s0
, *s1
, *s2
;
6848 struct block
*b
, *tmp
;
6852 if (code
== BPF_JEQ
) {
6853 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6854 b
= new_block(JMP(code
));
6858 b
= new_block(BPF_JMP
|code
|BPF_X
);
6864 sappend(a0
->s
, a1
->s
);
6868 free_reg(a0
->regno
);
6869 free_reg(a1
->regno
);
6871 /* 'and' together protocol checks */
6874 gen_and(a0
->b
, tmp
= a1
->b
);
6890 int regno
= alloc_reg();
6891 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6894 s
= new_stmt(BPF_LD
|BPF_LEN
);
6895 s
->next
= new_stmt(BPF_ST
);
6896 s
->next
->s
.k
= regno
;
6911 a
= (struct arth
*)newchunk(sizeof(*a
));
6915 s
= new_stmt(BPF_LD
|BPF_IMM
);
6917 s
->next
= new_stmt(BPF_ST
);
6933 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6936 s
= new_stmt(BPF_ST
);
6944 gen_arth(code
, a0
, a1
)
6946 struct arth
*a0
, *a1
;
6948 struct slist
*s0
, *s1
, *s2
;
6952 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6957 sappend(a0
->s
, a1
->s
);
6959 free_reg(a0
->regno
);
6960 free_reg(a1
->regno
);
6962 s0
= new_stmt(BPF_ST
);
6963 a0
->regno
= s0
->s
.k
= alloc_reg();
6970 * Here we handle simple allocation of the scratch registers.
6971 * If too many registers are alloc'd, the allocator punts.
6973 static int regused
[BPF_MEMWORDS
];
6977 * Initialize the table of used registers and the current register.
6983 memset(regused
, 0, sizeof regused
);
6987 * Return the next free register.
6992 int n
= BPF_MEMWORDS
;
6995 if (regused
[curreg
])
6996 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6998 regused
[curreg
] = 1;
7002 bpf_error("too many registers needed to evaluate expression");
7008 * Return a register to the table so it can
7018 static struct block
*
7025 s
= new_stmt(BPF_LD
|BPF_LEN
);
7026 b
= new_block(JMP(jmp
));
7037 return gen_len(BPF_JGE
, n
);
7041 * Actually, this is less than or equal.
7049 b
= gen_len(BPF_JGT
, n
);
7056 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7057 * the beginning of the link-layer header.
7058 * XXX - that means you can't test values in the radiotap header, but
7059 * as that header is difficult if not impossible to parse generally
7060 * without a loop, that might not be a severe problem. A new keyword
7061 * "radio" could be added for that, although what you'd really want
7062 * would be a way of testing particular radio header values, which
7063 * would generate code appropriate to the radio header in question.
7066 gen_byteop(op
, idx
, val
)
7077 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7080 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7084 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7088 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7092 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7096 b
= new_block(JMP(BPF_JEQ
));
7103 static u_char abroadcast
[] = { 0x0 };
7106 gen_broadcast(proto
)
7109 bpf_u_int32 hostmask
;
7110 struct block
*b0
, *b1
, *b2
;
7111 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7119 case DLT_ARCNET_LINUX
:
7120 return gen_ahostop(abroadcast
, Q_DST
);
7122 return gen_ehostop(ebroadcast
, Q_DST
);
7124 return gen_fhostop(ebroadcast
, Q_DST
);
7126 return gen_thostop(ebroadcast
, Q_DST
);
7127 case DLT_IEEE802_11
:
7128 case DLT_PRISM_HEADER
:
7129 case DLT_IEEE802_11_RADIO_AVS
:
7130 case DLT_IEEE802_11_RADIO
:
7132 return gen_wlanhostop(ebroadcast
, Q_DST
);
7133 case DLT_IP_OVER_FC
:
7134 return gen_ipfchostop(ebroadcast
, Q_DST
);
7138 * Check that the packet doesn't begin with an
7139 * LE Control marker. (We've already generated
7142 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7147 * Now check the MAC address.
7149 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7155 bpf_error("not a broadcast link");
7161 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7162 * as an indication that we don't know the netmask, and fail
7165 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7166 bpf_error("netmask not known, so 'ip broadcast' not supported");
7167 b0
= gen_linktype(ETHERTYPE_IP
);
7168 hostmask
= ~netmask
;
7169 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7170 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7171 (bpf_int32
)(~0 & hostmask
), hostmask
);
7176 bpf_error("only link-layer/IP broadcast filters supported");
7182 * Generate code to test the low-order bit of a MAC address (that's
7183 * the bottom bit of the *first* byte).
7185 static struct block
*
7186 gen_mac_multicast(offset
)
7189 register struct block
*b0
;
7190 register struct slist
*s
;
7192 /* link[offset] & 1 != 0 */
7193 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7194 b0
= new_block(JMP(BPF_JSET
));
7201 gen_multicast(proto
)
7204 register struct block
*b0
, *b1
, *b2
;
7205 register struct slist
*s
;
7213 case DLT_ARCNET_LINUX
:
7214 /* all ARCnet multicasts use the same address */
7215 return gen_ahostop(abroadcast
, Q_DST
);
7217 /* ether[0] & 1 != 0 */
7218 return gen_mac_multicast(0);
7221 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7223 * XXX - was that referring to bit-order issues?
7225 /* fddi[1] & 1 != 0 */
7226 return gen_mac_multicast(1);
7228 /* tr[2] & 1 != 0 */
7229 return gen_mac_multicast(2);
7230 case DLT_IEEE802_11
:
7231 case DLT_PRISM_HEADER
:
7232 case DLT_IEEE802_11_RADIO_AVS
:
7233 case DLT_IEEE802_11_RADIO
:
7238 * For control frames, there is no DA.
7240 * For management frames, DA is at an
7241 * offset of 4 from the beginning of
7244 * For data frames, DA is at an offset
7245 * of 4 from the beginning of the packet
7246 * if To DS is clear and at an offset of
7247 * 16 from the beginning of the packet
7252 * Generate the tests to be done for data frames.
7254 * First, check for To DS set, i.e. "link[1] & 0x01".
7256 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7257 b1
= new_block(JMP(BPF_JSET
));
7258 b1
->s
.k
= 0x01; /* To DS */
7262 * If To DS is set, the DA is at 16.
7264 b0
= gen_mac_multicast(16);
7268 * Now, check for To DS not set, i.e. check
7269 * "!(link[1] & 0x01)".
7271 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7272 b2
= new_block(JMP(BPF_JSET
));
7273 b2
->s
.k
= 0x01; /* To DS */
7278 * If To DS is not set, the DA is at 4.
7280 b1
= gen_mac_multicast(4);
7284 * Now OR together the last two checks. That gives
7285 * the complete set of checks for data frames.
7290 * Now check for a data frame.
7291 * I.e, check "link[0] & 0x08".
7293 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7294 b1
= new_block(JMP(BPF_JSET
));
7299 * AND that with the checks done for data frames.
7304 * If the high-order bit of the type value is 0, this
7305 * is a management frame.
7306 * I.e, check "!(link[0] & 0x08)".
7308 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7309 b2
= new_block(JMP(BPF_JSET
));
7315 * For management frames, the DA is at 4.
7317 b1
= gen_mac_multicast(4);
7321 * OR that with the checks done for data frames.
7322 * That gives the checks done for management and
7328 * If the low-order bit of the type value is 1,
7329 * this is either a control frame or a frame
7330 * with a reserved type, and thus not a
7333 * I.e., check "!(link[0] & 0x04)".
7335 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7336 b1
= new_block(JMP(BPF_JSET
));
7342 * AND that with the checks for data and management
7347 case DLT_IP_OVER_FC
:
7348 b0
= gen_mac_multicast(2);
7353 * Check that the packet doesn't begin with an
7354 * LE Control marker. (We've already generated
7357 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7361 /* ether[off_mac] & 1 != 0 */
7362 b0
= gen_mac_multicast(off_mac
);
7370 /* Link not known to support multicasts */
7374 b0
= gen_linktype(ETHERTYPE_IP
);
7375 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7381 b0
= gen_linktype(ETHERTYPE_IPV6
);
7382 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7387 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7393 * generate command for inbound/outbound. It's here so we can
7394 * make it link-type specific. 'dir' = 0 implies "inbound",
7395 * = 1 implies "outbound".
7401 register struct block
*b0
;
7404 * Only some data link types support inbound/outbound qualifiers.
7408 b0
= gen_relation(BPF_JEQ
,
7409 gen_load(Q_LINK
, gen_loadi(0), 1),
7416 /* match outgoing packets */
7417 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7419 /* match incoming packets */
7420 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7427 * Match packets sent by this machine.
7429 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7432 * Match packets sent to this machine.
7433 * (No broadcast or multicast packets, or
7434 * packets sent to some other machine and
7435 * received promiscuously.)
7437 * XXX - packets sent to other machines probably
7438 * shouldn't be matched, but what about broadcast
7439 * or multicast packets we received?
7441 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7445 #ifdef HAVE_NET_PFVAR_H
7447 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7448 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7454 /* match outgoing packets */
7455 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7457 /* match incoming packets */
7458 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7462 case DLT_JUNIPER_MFR
:
7463 case DLT_JUNIPER_MLFR
:
7464 case DLT_JUNIPER_MLPPP
:
7465 case DLT_JUNIPER_ATM1
:
7466 case DLT_JUNIPER_ATM2
:
7467 case DLT_JUNIPER_PPPOE
:
7468 case DLT_JUNIPER_PPPOE_ATM
:
7469 case DLT_JUNIPER_GGSN
:
7470 case DLT_JUNIPER_ES
:
7471 case DLT_JUNIPER_MONITOR
:
7472 case DLT_JUNIPER_SERVICES
:
7473 case DLT_JUNIPER_ETHER
:
7474 case DLT_JUNIPER_PPP
:
7475 case DLT_JUNIPER_FRELAY
:
7476 case DLT_JUNIPER_CHDLC
:
7477 case DLT_JUNIPER_VP
:
7478 case DLT_JUNIPER_ST
:
7479 case DLT_JUNIPER_ISM
:
7480 /* juniper flags (including direction) are stored
7481 * the byte after the 3-byte magic number */
7483 /* match outgoing packets */
7484 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7486 /* match incoming packets */
7487 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7492 bpf_error("inbound/outbound not supported on linktype %d",
7500 #ifdef HAVE_NET_PFVAR_H
7501 /* PF firewall log matched interface */
7503 gen_pf_ifname(const char *ifname
)
7508 if (linktype
!= DLT_PFLOG
) {
7509 bpf_error("ifname supported only on PF linktype");
7512 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7513 off
= offsetof(struct pfloghdr
, ifname
);
7514 if (strlen(ifname
) >= len
) {
7515 bpf_error("ifname interface names can only be %d characters",
7519 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7523 /* PF firewall log ruleset name */
7525 gen_pf_ruleset(char *ruleset
)
7529 if (linktype
!= DLT_PFLOG
) {
7530 bpf_error("ruleset supported only on PF linktype");
7534 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7535 bpf_error("ruleset names can only be %ld characters",
7536 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7540 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7541 strlen(ruleset
), (const u_char
*)ruleset
);
7545 /* PF firewall log rule number */
7551 if (linktype
!= DLT_PFLOG
) {
7552 bpf_error("rnr supported only on PF linktype");
7556 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7561 /* PF firewall log sub-rule number */
7563 gen_pf_srnr(int srnr
)
7567 if (linktype
!= DLT_PFLOG
) {
7568 bpf_error("srnr supported only on PF linktype");
7572 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7577 /* PF firewall log reason code */
7579 gen_pf_reason(int reason
)
7583 if (linktype
!= DLT_PFLOG
) {
7584 bpf_error("reason supported only on PF linktype");
7588 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7593 /* PF firewall log action */
7595 gen_pf_action(int action
)
7599 if (linktype
!= DLT_PFLOG
) {
7600 bpf_error("action supported only on PF linktype");
7604 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7608 #else /* !HAVE_NET_PFVAR_H */
7610 gen_pf_ifname(const char *ifname
)
7612 bpf_error("libpcap was compiled without pf support");
7618 gen_pf_ruleset(char *ruleset
)
7620 bpf_error("libpcap was compiled on a machine without pf support");
7628 bpf_error("libpcap was compiled on a machine without pf support");
7634 gen_pf_srnr(int srnr
)
7636 bpf_error("libpcap was compiled on a machine without pf support");
7642 gen_pf_reason(int reason
)
7644 bpf_error("libpcap was compiled on a machine without pf support");
7650 gen_pf_action(int action
)
7652 bpf_error("libpcap was compiled on a machine without pf support");
7656 #endif /* HAVE_NET_PFVAR_H */
7658 /* IEEE 802.11 wireless header */
7660 gen_p80211_type(int type
, int mask
)
7666 case DLT_IEEE802_11
:
7667 case DLT_PRISM_HEADER
:
7668 case DLT_IEEE802_11_RADIO_AVS
:
7669 case DLT_IEEE802_11_RADIO
:
7670 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7675 bpf_error("802.11 link-layer types supported only on 802.11");
7683 gen_p80211_fcdir(int fcdir
)
7689 case DLT_IEEE802_11
:
7690 case DLT_PRISM_HEADER
:
7691 case DLT_IEEE802_11_RADIO_AVS
:
7692 case DLT_IEEE802_11_RADIO
:
7696 bpf_error("frame direction supported only with 802.11 headers");
7700 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7701 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7708 register const u_char
*eaddr
;
7714 case DLT_ARCNET_LINUX
:
7715 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7717 return (gen_ahostop(eaddr
, (int)q
.dir
));
7719 bpf_error("ARCnet address used in non-arc expression");
7725 bpf_error("aid supported only on ARCnet");
7728 bpf_error("ARCnet address used in non-arc expression");
7733 static struct block
*
7734 gen_ahostop(eaddr
, dir
)
7735 register const u_char
*eaddr
;
7738 register struct block
*b0
, *b1
;
7741 /* src comes first, different from Ethernet */
7743 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7746 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7749 b0
= gen_ahostop(eaddr
, Q_SRC
);
7750 b1
= gen_ahostop(eaddr
, Q_DST
);
7756 b0
= gen_ahostop(eaddr
, Q_SRC
);
7757 b1
= gen_ahostop(eaddr
, Q_DST
);
7766 * support IEEE 802.1Q VLAN trunk over ethernet
7772 struct block
*b0
, *b1
;
7774 /* can't check for VLAN-encapsulated packets inside MPLS */
7775 if (label_stack_depth
> 0)
7776 bpf_error("no VLAN match after MPLS");
7779 * Check for a VLAN packet, and then change the offsets to point
7780 * to the type and data fields within the VLAN packet. Just
7781 * increment the offsets, so that we can support a hierarchy, e.g.
7782 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7785 * XXX - this is a bit of a kludge. If we were to split the
7786 * compiler into a parser that parses an expression and
7787 * generates an expression tree, and a code generator that
7788 * takes an expression tree (which could come from our
7789 * parser or from some other parser) and generates BPF code,
7790 * we could perhaps make the offsets parameters of routines
7791 * and, in the handler for an "AND" node, pass to subnodes
7792 * other than the VLAN node the adjusted offsets.
7794 * This would mean that "vlan" would, instead of changing the
7795 * behavior of *all* tests after it, change only the behavior
7796 * of tests ANDed with it. That would change the documented
7797 * semantics of "vlan", which might break some expressions.
7798 * However, it would mean that "(vlan and ip) or ip" would check
7799 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7800 * checking only for VLAN-encapsulated IP, so that could still
7801 * be considered worth doing; it wouldn't break expressions
7802 * that are of the form "vlan and ..." or "vlan N and ...",
7803 * which I suspect are the most common expressions involving
7804 * "vlan". "vlan or ..." doesn't necessarily do what the user
7805 * would really want, now, as all the "or ..." tests would
7806 * be done assuming a VLAN, even though the "or" could be viewed
7807 * as meaning "or, if this isn't a VLAN packet...".
7814 /* check for VLAN */
7815 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7816 (bpf_int32
)ETHERTYPE_8021Q
);
7818 /* If a specific VLAN is requested, check VLAN id */
7819 if (vlan_num
>= 0) {
7820 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7821 (bpf_int32
)vlan_num
, 0x0fff);
7835 bpf_error("no VLAN support for data link type %d",
7850 struct block
*b0
,*b1
;
7853 * Change the offsets to point to the type and data fields within
7854 * the MPLS packet. Just increment the offsets, so that we
7855 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7856 * capture packets with an outer label of 100000 and an inner
7859 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7863 if (label_stack_depth
> 0) {
7864 /* just match the bottom-of-stack bit clear */
7865 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7868 * Indicate that we're checking MPLS-encapsulated headers,
7869 * to make sure higher level code generators don't try to
7870 * match against IP-related protocols such as Q_ARP, Q_RARP
7875 case DLT_C_HDLC
: /* fall through */
7877 b0
= gen_linktype(ETHERTYPE_MPLS
);
7881 b0
= gen_linktype(PPP_MPLS_UCAST
);
7884 /* FIXME add other DLT_s ...
7885 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7886 * leave it for now */
7889 bpf_error("no MPLS support for data link type %d",
7897 /* If a specific MPLS label is requested, check it */
7898 if (label_num
>= 0) {
7899 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7900 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7901 0xfffff000); /* only compare the first 20 bits */
7908 label_stack_depth
++;
7913 * Support PPPOE discovery and session.
7918 /* check for PPPoE discovery */
7919 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7928 * Test against the PPPoE session link-layer type.
7930 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7933 * Change the offsets to point to the type and data fields within
7934 * the PPP packet, and note that this is PPPoE rather than
7937 * XXX - this is a bit of a kludge. If we were to split the
7938 * compiler into a parser that parses an expression and
7939 * generates an expression tree, and a code generator that
7940 * takes an expression tree (which could come from our
7941 * parser or from some other parser) and generates BPF code,
7942 * we could perhaps make the offsets parameters of routines
7943 * and, in the handler for an "AND" node, pass to subnodes
7944 * other than the PPPoE node the adjusted offsets.
7946 * This would mean that "pppoes" would, instead of changing the
7947 * behavior of *all* tests after it, change only the behavior
7948 * of tests ANDed with it. That would change the documented
7949 * semantics of "pppoes", which might break some expressions.
7950 * However, it would mean that "(pppoes and ip) or ip" would check
7951 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7952 * checking only for VLAN-encapsulated IP, so that could still
7953 * be considered worth doing; it wouldn't break expressions
7954 * that are of the form "pppoes and ..." which I suspect are the
7955 * most common expressions involving "pppoes". "pppoes or ..."
7956 * doesn't necessarily do what the user would really want, now,
7957 * as all the "or ..." tests would be done assuming PPPoE, even
7958 * though the "or" could be viewed as meaning "or, if this isn't
7959 * a PPPoE packet...".
7961 orig_linktype
= off_linktype
; /* save original values */
7966 * The "network-layer" protocol is PPPoE, which has a 6-byte
7967 * PPPoE header, followed by a PPP packet.
7969 * There is no HDLC encapsulation for the PPP packet (it's
7970 * encapsulated in PPPoES instead), so the link-layer type
7971 * starts at the first byte of the PPP packet. For PPPoE,
7972 * that offset is relative to the beginning of the total
7973 * link-layer payload, including any 802.2 LLC header, so
7974 * it's 6 bytes past off_nl.
7976 off_linktype
= off_nl
+ 6;
7979 * The network-layer offsets are relative to the beginning
7980 * of the MAC-layer payload; that's past the 6-byte
7981 * PPPoE header and the 2-byte PPP header.
7984 off_nl_nosnap
= 6+2;
7990 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8002 bpf_error("'vpi' supported only on raw ATM");
8003 if (off_vpi
== (u_int
)-1)
8005 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8011 bpf_error("'vci' supported only on raw ATM");
8012 if (off_vci
== (u_int
)-1)
8014 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8019 if (off_proto
== (u_int
)-1)
8020 abort(); /* XXX - this isn't on FreeBSD */
8021 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8026 if (off_payload
== (u_int
)-1)
8028 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8029 0xffffffff, jtype
, reverse
, jvalue
);
8034 bpf_error("'callref' supported only on raw ATM");
8035 if (off_proto
== (u_int
)-1)
8037 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8038 jtype
, reverse
, jvalue
);
8048 gen_atmtype_abbrev(type
)
8051 struct block
*b0
, *b1
;
8056 /* Get all packets in Meta signalling Circuit */
8058 bpf_error("'metac' supported only on raw ATM");
8059 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8060 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8065 /* Get all packets in Broadcast Circuit*/
8067 bpf_error("'bcc' supported only on raw ATM");
8068 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8069 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8074 /* Get all cells in Segment OAM F4 circuit*/
8076 bpf_error("'oam4sc' supported only on raw ATM");
8077 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8078 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8083 /* Get all cells in End-to-End OAM F4 Circuit*/
8085 bpf_error("'oam4ec' supported only on raw ATM");
8086 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8087 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8092 /* Get all packets in connection Signalling Circuit */
8094 bpf_error("'sc' supported only on raw ATM");
8095 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8096 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8101 /* Get all packets in ILMI Circuit */
8103 bpf_error("'ilmic' supported only on raw ATM");
8104 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8105 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8110 /* Get all LANE packets */
8112 bpf_error("'lane' supported only on raw ATM");
8113 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8116 * Arrange that all subsequent tests assume LANE
8117 * rather than LLC-encapsulated packets, and set
8118 * the offsets appropriately for LANE-encapsulated
8121 * "off_mac" is the offset of the Ethernet header,
8122 * which is 2 bytes past the ATM pseudo-header
8123 * (skipping the pseudo-header and 2-byte LE Client
8124 * field). The other offsets are Ethernet offsets
8125 * relative to "off_mac".
8128 off_mac
= off_payload
+ 2; /* MAC header */
8129 off_linktype
= off_mac
+ 12;
8130 off_macpl
= off_mac
+ 14; /* Ethernet */
8131 off_nl
= 0; /* Ethernet II */
8132 off_nl_nosnap
= 3; /* 802.3+802.2 */
8136 /* Get all LLC-encapsulated packets */
8138 bpf_error("'llc' supported only on raw ATM");
8139 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8150 * Filtering for MTP2 messages based on li value
8151 * FISU, length is null
8152 * LSSU, length is 1 or 2
8153 * MSU, length is 3 or more
8156 gen_mtp2type_abbrev(type
)
8159 struct block
*b0
, *b1
;
8164 if ( (linktype
!= DLT_MTP2
) &&
8165 (linktype
!= DLT_ERF
) &&
8166 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8167 bpf_error("'fisu' supported only on MTP2");
8168 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8169 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8173 if ( (linktype
!= DLT_MTP2
) &&
8174 (linktype
!= DLT_ERF
) &&
8175 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8176 bpf_error("'lssu' supported only on MTP2");
8177 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8178 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8183 if ( (linktype
!= DLT_MTP2
) &&
8184 (linktype
!= DLT_ERF
) &&
8185 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8186 bpf_error("'msu' supported only on MTP2");
8187 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8197 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8204 bpf_u_int32 val1
, val2
, val3
;
8206 switch (mtp3field
) {
8209 if (off_sio
== (u_int
)-1)
8210 bpf_error("'sio' supported only on SS7");
8211 /* sio coded on 1 byte so max value 255 */
8213 bpf_error("sio value %u too big; max value = 255",
8215 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8216 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8220 if (off_opc
== (u_int
)-1)
8221 bpf_error("'opc' supported only on SS7");
8222 /* opc coded on 14 bits so max value 16383 */
8224 bpf_error("opc value %u too big; max value = 16383",
8226 /* the following instructions are made to convert jvalue
8227 * to the form used to write opc in an ss7 message*/
8228 val1
= jvalue
& 0x00003c00;
8230 val2
= jvalue
& 0x000003fc;
8232 val3
= jvalue
& 0x00000003;
8234 jvalue
= val1
+ val2
+ val3
;
8235 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8236 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8240 if (off_dpc
== (u_int
)-1)
8241 bpf_error("'dpc' supported only on SS7");
8242 /* dpc coded on 14 bits so max value 16383 */
8244 bpf_error("dpc value %u too big; max value = 16383",
8246 /* the following instructions are made to convert jvalue
8247 * to the forme used to write dpc in an ss7 message*/
8248 val1
= jvalue
& 0x000000ff;
8250 val2
= jvalue
& 0x00003f00;
8252 jvalue
= val1
+ val2
;
8253 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8254 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8258 if (off_sls
== (u_int
)-1)
8259 bpf_error("'sls' supported only on SS7");
8260 /* sls coded on 4 bits so max value 15 */
8262 bpf_error("sls value %u too big; max value = 15",
8264 /* the following instruction is made to convert jvalue
8265 * to the forme used to write sls in an ss7 message*/
8266 jvalue
= jvalue
<< 4;
8267 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8268 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8277 static struct block
*
8278 gen_msg_abbrev(type
)
8284 * Q.2931 signalling protocol messages for handling virtual circuits
8285 * establishment and teardown
8290 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8294 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8298 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8302 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8306 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8309 case A_RELEASE_DONE
:
8310 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8320 gen_atmmulti_abbrev(type
)
8323 struct block
*b0
, *b1
;
8329 bpf_error("'oam' supported only on raw ATM");
8330 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8335 bpf_error("'oamf4' supported only on raw ATM");
8337 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8338 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8340 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8346 * Get Q.2931 signalling messages for switched
8347 * virtual connection
8350 bpf_error("'connectmsg' supported only on raw ATM");
8351 b0
= gen_msg_abbrev(A_SETUP
);
8352 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8354 b0
= gen_msg_abbrev(A_CONNECT
);
8356 b0
= gen_msg_abbrev(A_CONNECTACK
);
8358 b0
= gen_msg_abbrev(A_RELEASE
);
8360 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8362 b0
= gen_atmtype_abbrev(A_SC
);
8368 bpf_error("'metaconnect' supported only on raw ATM");
8369 b0
= gen_msg_abbrev(A_SETUP
);
8370 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8372 b0
= gen_msg_abbrev(A_CONNECT
);
8374 b0
= gen_msg_abbrev(A_RELEASE
);
8376 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8378 b0
= gen_atmtype_abbrev(A_METAC
);